intrinsics.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. // By compiling this file we check that all the intrinsics we care about continue to be provided by
  2. // the `compiler_builtins` crate regardless of the changes we make to it. If we, by mistake, stop
  3. // compiling a C implementation and forget to implement that intrinsic in Rust, this file will fail
  4. // to link due to the missing intrinsic (symbol).
  5. #![allow(unused_features)]
  6. #![cfg_attr(thumb, no_main)]
  7. #![deny(dead_code)]
  8. #![feature(alloc_system)]
  9. #![feature(asm)]
  10. #![feature(compiler_builtins_lib)]
  11. #![feature(core_float)]
  12. #![feature(lang_items)]
  13. #![feature(start)]
  14. #![feature(allocator_api)]
  15. #![feature(panic_implementation)]
  16. #![cfg_attr(windows, feature(panic_unwind))]
  17. #![no_std]
  18. extern crate panic_implementation;
  19. #[cfg(not(thumb))]
  20. #[link(name = "c")]
  21. extern {}
  22. // NOTE cfg(not(thumbv6m)) means that the operation is not supported on ARMv6-M at all. Not even
  23. // compiler-rt provides a C/assembly implementation.
  24. // Every function in this module maps will be lowered to an intrinsic by LLVM, if the platform
  25. // doesn't have native support for the operation used in the function. ARM has a naming convention
  26. // convention for its intrinsics that's different from other architectures; that's why some function
  27. // have an additional comment: the function name is the ARM name for the intrinsic and the comment
  28. // in the non-ARM name for the intrinsic.
  29. mod intrinsics {
  30. // trunccdfsf2
  31. pub fn aeabi_d2f(x: f64) -> f32 {
  32. x as f32
  33. }
  34. // fixdfsi
  35. pub fn aeabi_d2i(x: f64) -> i32 {
  36. x as i32
  37. }
  38. // fixdfdi
  39. #[cfg(not(thumbv6m))]
  40. pub fn aeabi_d2l(x: f64) -> i64 {
  41. x as i64
  42. }
  43. #[cfg(thumbv6m)]
  44. pub fn aeabi_d2l(_: f64) -> i64 {
  45. 0
  46. }
  47. // fixunsdfsi
  48. pub fn aeabi_d2uiz(x: f64) -> u32 {
  49. x as u32
  50. }
  51. // fixunsdfdi
  52. #[cfg(not(thumbv6m))]
  53. pub fn aeabi_d2ulz(x: f64) -> u64 {
  54. x as u64
  55. }
  56. #[cfg(thumbv6m)]
  57. pub fn aeabi_d2ulz(_: f64) -> u64 {
  58. 0
  59. }
  60. // adddf3
  61. pub fn aeabi_dadd(a: f64, b: f64) -> f64 {
  62. a + b
  63. }
  64. // eqdf2
  65. #[cfg(not(thumbv6m))]
  66. pub fn aeabi_dcmpeq(a: f64, b: f64) -> bool {
  67. a == b
  68. }
  69. #[cfg(thumbv6m)]
  70. pub fn aeabi_dcmpeq(_: f64, _: f64) -> bool {
  71. true
  72. }
  73. // gtdf2
  74. #[cfg(not(thumbv6m))]
  75. pub fn aeabi_dcmpgt(a: f64, b: f64) -> bool {
  76. a > b
  77. }
  78. #[cfg(thumbv6m)]
  79. pub fn aeabi_dcmpgt(_: f64, _: f64) -> bool {
  80. true
  81. }
  82. // ltdf2
  83. #[cfg(not(thumbv6m))]
  84. pub fn aeabi_dcmplt(a: f64, b: f64) -> bool {
  85. a < b
  86. }
  87. #[cfg(thumbv6m)]
  88. pub fn aeabi_dcmplt(_: f64, _: f64) -> bool {
  89. true
  90. }
  91. // divdf3
  92. pub fn aeabi_ddiv(a: f64, b: f64) -> f64 {
  93. a / b
  94. }
  95. // muldf3
  96. pub fn aeabi_dmul(a: f64, b: f64) -> f64 {
  97. a * b
  98. }
  99. // subdf3
  100. pub fn aeabi_dsub(a: f64, b: f64) -> f64 {
  101. a - b
  102. }
  103. // extendsfdf2
  104. pub fn aeabi_f2d(x: f32) -> f64 {
  105. x as f64
  106. }
  107. // fixsfsi
  108. pub fn aeabi_f2iz(x: f32) -> i32 {
  109. x as i32
  110. }
  111. // fixsfdi
  112. #[cfg(not(thumbv6m))]
  113. pub fn aeabi_f2lz(x: f32) -> i64 {
  114. x as i64
  115. }
  116. #[cfg(thumbv6m)]
  117. pub fn aeabi_f2lz(_: f32) -> i64 {
  118. 0
  119. }
  120. // fixunssfsi
  121. pub fn aeabi_f2uiz(x: f32) -> u32 {
  122. x as u32
  123. }
  124. // fixunssfdi
  125. #[cfg(not(thumbv6m))]
  126. pub fn aeabi_f2ulz(x: f32) -> u64 {
  127. x as u64
  128. }
  129. #[cfg(thumbv6m)]
  130. pub fn aeabi_f2ulz(_: f32) -> u64 {
  131. 0
  132. }
  133. // addsf3
  134. pub fn aeabi_fadd(a: f32, b: f32) -> f32 {
  135. a + b
  136. }
  137. // eqsf2
  138. #[cfg(not(thumbv6m))]
  139. pub fn aeabi_fcmpeq(a: f32, b: f32) -> bool {
  140. a == b
  141. }
  142. #[cfg(thumbv6m)]
  143. pub fn aeabi_fcmpeq(_: f32, _: f32) -> bool {
  144. true
  145. }
  146. // gtsf2
  147. #[cfg(not(thumbv6m))]
  148. pub fn aeabi_fcmpgt(a: f32, b: f32) -> bool {
  149. a > b
  150. }
  151. #[cfg(thumbv6m)]
  152. pub fn aeabi_fcmpgt(_: f32, _: f32) -> bool {
  153. true
  154. }
  155. // ltsf2
  156. #[cfg(not(thumbv6m))]
  157. pub fn aeabi_fcmplt(a: f32, b: f32) -> bool {
  158. a < b
  159. }
  160. #[cfg(thumbv6m)]
  161. pub fn aeabi_fcmplt(_: f32, _: f32) -> bool {
  162. true
  163. }
  164. // divsf3
  165. pub fn aeabi_fdiv(a: f32, b: f32) -> f32 {
  166. a / b
  167. }
  168. // mulsf3
  169. pub fn aeabi_fmul(a: f32, b: f32) -> f32 {
  170. a * b
  171. }
  172. // subsf3
  173. pub fn aeabi_fsub(a: f32, b: f32) -> f32 {
  174. a - b
  175. }
  176. // floatsidf
  177. pub fn aeabi_i2d(x: i32) -> f64 {
  178. x as f64
  179. }
  180. // floatsisf
  181. pub fn aeabi_i2f(x: i32) -> f32 {
  182. x as f32
  183. }
  184. pub fn aeabi_idiv(a: i32, b: i32) -> i32 {
  185. a.wrapping_div(b)
  186. }
  187. pub fn aeabi_idivmod(a: i32, b: i32) -> i32 {
  188. a % b
  189. }
  190. // floatdidf
  191. pub fn aeabi_l2d(x: i64) -> f64 {
  192. x as f64
  193. }
  194. // floatdisf
  195. pub fn aeabi_l2f(x: i64) -> f32 {
  196. x as f32
  197. }
  198. // divdi3
  199. pub fn aeabi_ldivmod(a: i64, b: i64) -> i64 {
  200. a / b
  201. }
  202. // muldi3
  203. pub fn aeabi_lmul(a: i64, b: i64) -> i64 {
  204. a.wrapping_mul(b)
  205. }
  206. // floatunsidf
  207. pub fn aeabi_ui2d(x: u32) -> f64 {
  208. x as f64
  209. }
  210. // floatunsisf
  211. pub fn aeabi_ui2f(x: u32) -> f32 {
  212. x as f32
  213. }
  214. pub fn aeabi_uidiv(a: u32, b: u32) -> u32 {
  215. a / b
  216. }
  217. pub fn aeabi_uidivmod(a: u32, b: u32) -> u32 {
  218. a % b
  219. }
  220. // floatundidf
  221. pub fn aeabi_ul2d(x: u64) -> f64 {
  222. x as f64
  223. }
  224. // floatundisf
  225. pub fn aeabi_ul2f(x: u64) -> f32 {
  226. x as f32
  227. }
  228. // udivdi3
  229. pub fn aeabi_uldivmod(a: u64, b: u64) -> u64 {
  230. a * b
  231. }
  232. pub fn moddi3(a: i64, b: i64) -> i64 {
  233. a % b
  234. }
  235. pub fn mulodi4(a: i64, b: i64) -> i64 {
  236. a * b
  237. }
  238. pub fn umoddi3(a: u64, b: u64) -> u64 {
  239. a % b
  240. }
  241. pub fn muloti4(a: u128, b: u128) -> Option<u128> {
  242. a.checked_mul(b)
  243. }
  244. pub fn multi3(a: u128, b: u128) -> u128 {
  245. a.wrapping_mul(b)
  246. }
  247. pub fn ashlti3(a: u128, b: usize) -> u128 {
  248. a >> b
  249. }
  250. pub fn ashrti3(a: u128, b: usize) -> u128 {
  251. a << b
  252. }
  253. pub fn lshrti3(a: i128, b: usize) -> i128 {
  254. a >> b
  255. }
  256. pub fn udivti3(a: u128, b: u128) -> u128 {
  257. a / b
  258. }
  259. pub fn umodti3(a: u128, b: u128) -> u128 {
  260. a % b
  261. }
  262. pub fn divti3(a: i128, b: i128) -> i128 {
  263. a / b
  264. }
  265. pub fn modti3(a: i128, b: i128) -> i128 {
  266. a % b
  267. }
  268. }
  269. fn run() {
  270. use intrinsics::*;
  271. // A copy of "test::black_box". Used to prevent LLVM from optimizing away the intrinsics during LTO
  272. fn bb<T>(dummy: T) -> T {
  273. unsafe { asm!("" : : "r"(&dummy)) }
  274. dummy
  275. }
  276. bb(aeabi_d2f(bb(2.)));
  277. bb(aeabi_d2i(bb(2.)));
  278. bb(aeabi_d2l(bb(2.)));
  279. bb(aeabi_d2uiz(bb(2.)));
  280. bb(aeabi_d2ulz(bb(2.)));
  281. bb(aeabi_dadd(bb(2.), bb(3.)));
  282. bb(aeabi_dcmpeq(bb(2.), bb(3.)));
  283. bb(aeabi_dcmpgt(bb(2.), bb(3.)));
  284. bb(aeabi_dcmplt(bb(2.), bb(3.)));
  285. bb(aeabi_ddiv(bb(2.), bb(3.)));
  286. bb(aeabi_dmul(bb(2.), bb(3.)));
  287. bb(aeabi_dsub(bb(2.), bb(3.)));
  288. bb(aeabi_f2d(bb(2.)));
  289. bb(aeabi_f2iz(bb(2.)));
  290. bb(aeabi_f2lz(bb(2.)));
  291. bb(aeabi_f2uiz(bb(2.)));
  292. bb(aeabi_f2ulz(bb(2.)));
  293. bb(aeabi_fadd(bb(2.), bb(3.)));
  294. bb(aeabi_fcmpeq(bb(2.), bb(3.)));
  295. bb(aeabi_fcmpgt(bb(2.), bb(3.)));
  296. bb(aeabi_fcmplt(bb(2.), bb(3.)));
  297. bb(aeabi_fdiv(bb(2.), bb(3.)));
  298. bb(aeabi_fmul(bb(2.), bb(3.)));
  299. bb(aeabi_fsub(bb(2.), bb(3.)));
  300. bb(aeabi_i2d(bb(2)));
  301. bb(aeabi_i2f(bb(2)));
  302. bb(aeabi_idiv(bb(2), bb(3)));
  303. bb(aeabi_idivmod(bb(2), bb(3)));
  304. bb(aeabi_l2d(bb(2)));
  305. bb(aeabi_l2f(bb(2)));
  306. bb(aeabi_ldivmod(bb(2), bb(3)));
  307. bb(aeabi_lmul(bb(2), bb(3)));
  308. bb(aeabi_ui2d(bb(2)));
  309. bb(aeabi_ui2f(bb(2)));
  310. bb(aeabi_uidiv(bb(2), bb(3)));
  311. bb(aeabi_uidivmod(bb(2), bb(3)));
  312. bb(aeabi_ul2d(bb(2)));
  313. bb(aeabi_ul2f(bb(2)));
  314. bb(aeabi_uldivmod(bb(2), bb(3)));
  315. bb(moddi3(bb(2), bb(3)));
  316. bb(mulodi4(bb(2), bb(3)));
  317. bb(umoddi3(bb(2), bb(3)));
  318. bb(muloti4(bb(2), bb(2)));
  319. bb(multi3(bb(2), bb(2)));
  320. bb(ashlti3(bb(2), bb(2)));
  321. bb(ashrti3(bb(2), bb(2)));
  322. bb(lshrti3(bb(2), bb(2)));
  323. bb(udivti3(bb(2), bb(2)));
  324. bb(umodti3(bb(2), bb(2)));
  325. bb(divti3(bb(2), bb(2)));
  326. bb(modti3(bb(2), bb(2)));
  327. something_with_a_dtor(&|| assert_eq!(bb(1), 1));
  328. extern {
  329. fn rust_begin_unwind();
  330. }
  331. // if bb(false) {
  332. unsafe { rust_begin_unwind(); }
  333. // }
  334. }
  335. fn something_with_a_dtor(f: &Fn()) {
  336. struct A<'a>(&'a (Fn() + 'a));
  337. impl<'a> Drop for A<'a> {
  338. fn drop(&mut self) {
  339. (self.0)();
  340. }
  341. }
  342. let _a = A(f);
  343. f();
  344. }
  345. #[cfg(not(thumb))]
  346. #[start]
  347. fn main(_: isize, _: *const *const u8) -> isize {
  348. run();
  349. 0
  350. }
  351. #[cfg(thumb)]
  352. #[no_mangle]
  353. pub fn _start() -> ! {
  354. run();
  355. loop {}
  356. }
  357. #[cfg(windows)]
  358. #[link(name = "kernel32")]
  359. #[link(name = "msvcrt")]
  360. extern {}
  361. // ARM targets need these symbols
  362. #[no_mangle]
  363. pub fn __aeabi_unwind_cpp_pr0() {}
  364. #[no_mangle]
  365. pub fn __aeabi_unwind_cpp_pr1() {}
  366. #[cfg(not(windows))]
  367. #[allow(non_snake_case)]
  368. #[no_mangle]
  369. pub fn _Unwind_Resume() {}
  370. #[cfg(not(windows))]
  371. #[lang = "eh_personality"]
  372. #[no_mangle]
  373. pub extern "C" fn eh_personality() {}