]> git.lizzy.rs Git - rust.git/blob - src/intrinsics.rs
Fix some more stuff
[rust.git] / src / intrinsics.rs
1 use crate::prelude::*;
2
3 use rustc::ty::subst::SubstsRef;
4
5 macro_rules! intrinsic_pat {
6     (_) => {
7         _
8     };
9     ($name:ident) => {
10         stringify!($name)
11     }
12 }
13
14 macro_rules! intrinsic_arg {
15     (c $fx:expr, $arg:ident) => {
16         $arg
17     };
18     (v $fx:expr, $arg:ident) => {
19         $arg.load_scalar($fx)
20     };
21 }
22
23 macro_rules! intrinsic_substs {
24     ($substs:expr, $index:expr,) => {};
25     ($substs:expr, $index:expr, $first:ident $(,$rest:ident)*) => {
26         let $first = $substs.type_at($index);
27         intrinsic_substs!($substs, $index+1, $($rest),*);
28     };
29 }
30
31 macro_rules! intrinsic_match {
32     ($fx:expr, $intrinsic:expr, $substs:expr, $args:expr, $(
33         $($name:tt)|+ $(if $cond:expr)?, $(<$($subst:ident),*>)? ($($a:ident $arg:ident),*) $content:block;
34     )*) => {
35         match $intrinsic {
36             $(
37                 $(intrinsic_pat!($name))|* $(if $cond)? => {
38                     #[allow(unused_parens, non_snake_case)]
39                     {
40                         $(
41                             intrinsic_substs!($substs, 0, $($subst),*);
42                         )?
43                         if let [$($arg),*] = *$args {
44                             let ($($arg),*) = (
45                                 $(intrinsic_arg!($a $fx, $arg)),*
46                             );
47                             #[warn(unused_parens, non_snake_case)]
48                             {
49                                 $content
50                             }
51                         } else {
52                             bug!("wrong number of args for intrinsic {:?}", $intrinsic);
53                         }
54                     }
55                 }
56             )*
57             _ => unimpl!("unsupported intrinsic {}", $intrinsic),
58         }
59     };
60 }
61
62 macro_rules! atomic_binop_return_old {
63     ($fx:expr, $op:ident<$T:ident>($ptr:ident, $src:ident) -> $ret:ident) => {
64         let clif_ty = $fx.clif_type($T).unwrap();
65         let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
66         let new = $fx.bcx.ins().$op(old, $src);
67         $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
68         $ret.write_cvalue($fx, CValue::by_val(old, $fx.layout_of($T)));
69     };
70 }
71
72 macro_rules! atomic_minmax {
73     ($fx:expr, $cc:expr, <$T:ident> ($ptr:ident, $src:ident) -> $ret:ident) => {
74         // Read old
75         let clif_ty = $fx.clif_type($T).unwrap();
76         let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
77
78         // Compare
79         let is_eq = $fx.bcx.ins().icmp(IntCC::SignedGreaterThan, old, $src);
80         let new = crate::common::codegen_select(&mut $fx.bcx, is_eq, old, $src);
81
82         // Write new
83         $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
84
85         let ret_val = CValue::by_val(old, $ret.layout());
86         $ret.write_cvalue($fx, ret_val);
87     };
88 }
89
90 pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
91     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
92     def_id: DefId,
93     substs: SubstsRef<'tcx>,
94     args: Vec<CValue<'tcx>>,
95     destination: Option<(CPlace<'tcx>, BasicBlock)>,
96 ) {
97     let intrinsic = fx.tcx.item_name(def_id).as_str();
98     let intrinsic = &intrinsic[..];
99
100     let ret = match destination {
101         Some((place, _)) => place,
102         None => {
103             // Insert non returning intrinsics here
104             match intrinsic {
105                 "abort" => {
106                     trap_panic(fx, "Called intrinsic::abort.");
107                 }
108                 "unreachable" => {
109                     trap_unreachable(fx, "[corruption] Called intrinsic::unreachable.");
110                 }
111                 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
112             }
113             return;
114         }
115     };
116
117     let u64_layout = fx.layout_of(fx.tcx.types.u64);
118     let usize_layout = fx.layout_of(fx.tcx.types.usize);
119
120     intrinsic_match! {
121         fx, intrinsic, substs, args,
122
123         assume, (c _a) {};
124         likely | unlikely, (c a) {
125             ret.write_cvalue(fx, a);
126         };
127         breakpoint, () {
128             fx.bcx.ins().debugtrap();
129         };
130         copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
131             let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
132             let elem_size = fx
133                 .bcx
134                 .ins()
135                 .iconst(fx.pointer_type, elem_size as i64);
136             assert_eq!(args.len(), 3);
137             let byte_amount = fx.bcx.ins().imul(count, elem_size);
138
139             if intrinsic.ends_with("_nonoverlapping") {
140                 fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
141             } else {
142                 fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
143             }
144         };
145         discriminant_value, (c val) {
146             let pointee_layout = fx.layout_of(val.layout().ty.builtin_deref(true).unwrap().ty);
147             let place = CPlace::for_addr(val.load_scalar(fx), pointee_layout);
148             let discr = crate::base::trans_get_discriminant(fx, place, ret.layout());
149             ret.write_cvalue(fx, discr);
150         };
151         size_of, <T> () {
152             let size_of = fx.layout_of(T).size.bytes();
153             let size_of = CValue::const_val(fx, usize_layout.ty, size_of as i64);
154             ret.write_cvalue(fx, size_of);
155         };
156         size_of_val, <T> (c ptr) {
157             let layout = fx.layout_of(T);
158             let size = if layout.is_unsized() {
159                 let (_ptr, info) = ptr.load_scalar_pair(fx);
160                 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
161                 size
162             } else {
163                 fx
164                     .bcx
165                     .ins()
166                     .iconst(fx.pointer_type, layout.size.bytes() as i64)
167             };
168             ret.write_cvalue(fx, CValue::by_val(size, usize_layout));
169         };
170         min_align_of, <T> () {
171             let min_align = fx.layout_of(T).align.abi.bytes();
172             let min_align = CValue::const_val(fx, usize_layout.ty, min_align as i64);
173             ret.write_cvalue(fx, min_align);
174         };
175         min_align_of_val, <T> (c ptr) {
176             let layout = fx.layout_of(T);
177             let align = if layout.is_unsized() {
178                 let (_ptr, info) = ptr.load_scalar_pair(fx);
179                 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
180                 align
181             } else {
182                 fx
183                     .bcx
184                     .ins()
185                     .iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
186             };
187             ret.write_cvalue(fx, CValue::by_val(align, usize_layout));
188         };
189         pref_align_of, <T> () {
190             let pref_align = fx.layout_of(T).align.pref.bytes();
191             let pref_align = CValue::const_val(fx, usize_layout.ty, pref_align as i64);
192             ret.write_cvalue(fx, pref_align);
193         };
194
195
196         type_id, <T> () {
197             let type_id = fx.tcx.type_id_hash(T);
198             let type_id = CValue::const_val(fx, u64_layout.ty, type_id as i64);
199             ret.write_cvalue(fx, type_id);
200         };
201         type_name, <T> () {
202             let type_name = fx.tcx.type_name(T);
203             let type_name = crate::constant::trans_const_value(fx, type_name);
204             ret.write_cvalue(fx, type_name);
205         };
206
207         _ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
208             // FIXME trap on overflow
209             let bin_op = match intrinsic {
210                 "unchecked_sub" => BinOp::Sub,
211                 "unchecked_div" | "exact_div" => BinOp::Div,
212                 "unchecked_rem" => BinOp::Rem,
213                 "unchecked_shl" => BinOp::Shl,
214                 "unchecked_shr" => BinOp::Shr,
215                 _ => unimplemented!("intrinsic {}", intrinsic),
216             };
217             let res = match ret.layout().ty.sty {
218                 ty::Uint(_) => crate::base::trans_int_binop(
219                     fx,
220                     bin_op,
221                     x,
222                     y,
223                     ret.layout().ty,
224                     false,
225                 ),
226                 ty::Int(_) => crate::base::trans_int_binop(
227                     fx,
228                     bin_op,
229                     x,
230                     y,
231                     ret.layout().ty,
232                     true,
233                 ),
234                 _ => panic!(),
235             };
236             ret.write_cvalue(fx, res);
237         };
238         _ if intrinsic.ends_with("_with_overflow"), <T> (c x, c y) {
239             assert_eq!(x.layout().ty, y.layout().ty);
240             let bin_op = match intrinsic {
241                 "add_with_overflow" => BinOp::Add,
242                 "sub_with_overflow" => BinOp::Sub,
243                 "mul_with_overflow" => BinOp::Mul,
244                 _ => unimplemented!("intrinsic {}", intrinsic),
245             };
246             let res = match T.sty {
247                 ty::Uint(_) => crate::base::trans_checked_int_binop(
248                     fx,
249                     bin_op,
250                     x,
251                     y,
252                     ret.layout().ty,
253                     false,
254                 ),
255                 ty::Int(_) => crate::base::trans_checked_int_binop(
256                     fx,
257                     bin_op,
258                     x,
259                     y,
260                     ret.layout().ty,
261                     true,
262                 ),
263                 _ => panic!(),
264             };
265             ret.write_cvalue(fx, res);
266         };
267         _ if intrinsic.starts_with("overflowing_"), <T> (c x, c y) {
268             assert_eq!(x.layout().ty, y.layout().ty);
269             let bin_op = match intrinsic {
270                 "overflowing_add" => BinOp::Add,
271                 "overflowing_sub" => BinOp::Sub,
272                 "overflowing_mul" => BinOp::Mul,
273                 _ => unimplemented!("intrinsic {}", intrinsic),
274             };
275             let res = match T.sty {
276                 ty::Uint(_) => crate::base::trans_int_binop(
277                     fx,
278                     bin_op,
279                     x,
280                     y,
281                     ret.layout().ty,
282                     false,
283                 ),
284                 ty::Int(_) => crate::base::trans_int_binop(
285                     fx,
286                     bin_op,
287                     x,
288                     y,
289                     ret.layout().ty,
290                     true,
291                 ),
292                 _ => panic!(),
293             };
294             ret.write_cvalue(fx, res);
295         };
296         _ if intrinsic.starts_with("saturating_"), <T> (c x, c y) {
297             // FIXME implement saturating behavior
298             assert_eq!(x.layout().ty, y.layout().ty);
299             let bin_op = match intrinsic {
300                 "saturating_add" => BinOp::Add,
301                 "saturating_sub" => BinOp::Sub,
302                 "saturating_mul" => BinOp::Mul,
303                 _ => unimplemented!("intrinsic {}", intrinsic),
304             };
305             let res = match T.sty {
306                 ty::Uint(_) => crate::base::trans_int_binop(
307                     fx,
308                     bin_op,
309                     x,
310                     y,
311                     ret.layout().ty,
312                     false,
313                 ),
314                 ty::Int(_) => crate::base::trans_int_binop(
315                     fx,
316                     bin_op,
317                     x,
318                     y,
319                     ret.layout().ty,
320                     true,
321                 ),
322                 _ => panic!(),
323             };
324             ret.write_cvalue(fx, res);
325         };
326         rotate_left, <T>(v x, v y) {
327             let layout = fx.layout_of(T);
328             let res = fx.bcx.ins().rotl(x, y);
329             ret.write_cvalue(fx, CValue::by_val(res, layout));
330         };
331         rotate_right, <T>(v x, v y) {
332             let layout = fx.layout_of(T);
333             let res = fx.bcx.ins().rotr(x, y);
334             ret.write_cvalue(fx, CValue::by_val(res, layout));
335         };
336
337         // The only difference between offset and arith_offset is regarding UB. Because Cranelift
338         // doesn't have UB both are codegen'ed the same way
339         offset | arith_offset, (c base, v offset) {
340             let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
341             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
342             let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
343             let base_val = base.load_scalar(fx);
344             let res = fx.bcx.ins().iadd(base_val, ptr_diff);
345             ret.write_cvalue(fx, CValue::by_val(res, args[0].layout()));
346         };
347
348         transmute, <src_ty, dst_ty> (c from) {
349             assert_eq!(from.layout().ty, src_ty);
350             let addr = from.force_stack(fx);
351             let dst_layout = fx.layout_of(dst_ty);
352             ret.write_cvalue(fx, CValue::by_ref(addr, dst_layout))
353         };
354         init, () {
355             if ret.layout().abi == Abi::Uninhabited {
356                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::init for uninhabited type.");
357                 return;
358             }
359
360             match ret {
361                 CPlace::NoPlace(_layout) => {}
362                 CPlace::Var(var, layout) => {
363                     let clif_ty = fx.clif_type(layout.ty).unwrap();
364                     let val = match clif_ty {
365                         types::I8 | types::I16 | types::I32 | types::I64 => fx.bcx.ins().iconst(clif_ty, 0),
366                         types::F32 => {
367                             let zero = fx.bcx.ins().iconst(types::I32, 0);
368                             fx.bcx.ins().bitcast(types::F32, zero)
369                         }
370                         types::F64 => {
371                             let zero = fx.bcx.ins().iconst(types::I64, 0);
372                             fx.bcx.ins().bitcast(types::F64, zero)
373                         }
374                         _ => panic!("clif_type returned {}", clif_ty),
375                     };
376                     fx.bcx.def_var(mir_var(var), val);
377                 }
378                 _ => {
379                     let addr = ret.to_addr(fx);
380                     let layout = ret.layout();
381                     fx.bcx.emit_small_memset(fx.module.target_config(), addr, 0, layout.size.bytes(), 1);
382                 }
383             }
384         };
385         write_bytes, (c dst, v val, v count) {
386             let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
387             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
388             let count = fx.bcx.ins().imul_imm(count, pointee_size as i64);
389             let dst_ptr = dst.load_scalar(fx);
390             fx.bcx.call_memset(fx.module.target_config(), dst_ptr, val, count);
391         };
392         ctlz | ctlz_nonzero, <T> (v arg) {
393             let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
394                 // FIXME verify this algorithm is correct
395                 let (lsb, msb) = fx.bcx.ins().isplit(arg);
396                 let lsb_lz = fx.bcx.ins().clz(lsb);
397                 let msb_lz = fx.bcx.ins().clz(msb);
398                 let msb_lz_is_64 = fx.bcx.ins().icmp_imm(IntCC::Equal, msb_lz, 64);
399                 let lsb_lz_plus_64 = fx.bcx.ins().iadd_imm(lsb_lz, 64);
400                 fx.bcx.ins().select(msb_lz_is_64, lsb_lz_plus_64, msb_lz)
401             } else {
402                 fx.bcx.ins().clz(arg)
403             };
404             let res = CValue::by_val(res, fx.layout_of(T));
405             ret.write_cvalue(fx, res);
406         };
407         cttz | cttz_nonzero, <T> (v arg) {
408             let res = if T == fx.tcx.types.u128 || T == fx.tcx.types.i128 {
409                 // FIXME verify this algorithm is correct
410                 let (lsb, msb) = fx.bcx.ins().isplit(arg);
411                 let lsb_tz = fx.bcx.ins().ctz(lsb);
412                 let msb_tz = fx.bcx.ins().ctz(msb);
413                 let lsb_tz_is_64 = fx.bcx.ins().icmp_imm(IntCC::Equal, lsb_tz, 64);
414                 let msb_lz_plus_64 = fx.bcx.ins().iadd_imm(msb_tz, 64);
415                 fx.bcx.ins().select(lsb_tz_is_64, msb_lz_plus_64, lsb_tz)
416             } else {
417                 fx.bcx.ins().ctz(arg)
418             };
419             let res = CValue::by_val(res, fx.layout_of(T));
420             ret.write_cvalue(fx, res);
421         };
422         ctpop, <T> (v arg) {
423             let res = CValue::by_val(fx.bcx.ins().popcnt(arg), fx.layout_of(T));
424             ret.write_cvalue(fx, res);
425         };
426         bitreverse, <T> (v arg) {
427             let res = CValue::by_val(fx.bcx.ins().bitrev(arg), fx.layout_of(T));
428             ret.write_cvalue(fx, res);
429         };
430         bswap, <T> (v arg) {
431             // FIXME(CraneStation/cranelift#794) add bswap instruction to cranelift
432             fn swap(bcx: &mut FunctionBuilder, v: Value) -> Value {
433                 match bcx.func.dfg.value_type(v) {
434                     types::I8 => v,
435
436                     // https://code.woboq.org/gcc/include/bits/byteswap.h.html
437                     types::I16 => {
438                         let tmp1 = bcx.ins().ishl_imm(v, 8);
439                         let n1 = bcx.ins().band_imm(tmp1, 0xFF00);
440
441                         let tmp2 = bcx.ins().ushr_imm(v, 8);
442                         let n2 = bcx.ins().band_imm(tmp2, 0x00FF);
443
444                         bcx.ins().bor(n1, n2)
445                     }
446                     types::I32 => {
447                         let tmp1 = bcx.ins().ishl_imm(v, 24);
448                         let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000);
449
450                         let tmp2 = bcx.ins().ishl_imm(v, 8);
451                         let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000);
452
453                         let tmp3 = bcx.ins().ushr_imm(v, 8);
454                         let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00);
455
456                         let tmp4 = bcx.ins().ushr_imm(v, 24);
457                         let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF);
458
459                         let or_tmp1 = bcx.ins().bor(n1, n2);
460                         let or_tmp2 = bcx.ins().bor(n3, n4);
461                         bcx.ins().bor(or_tmp1, or_tmp2)
462                     }
463                     types::I64 => {
464                         let tmp1 = bcx.ins().ishl_imm(v, 56);
465                         let n1 = bcx.ins().band_imm(tmp1, 0xFF00_0000_0000_0000u64 as i64);
466
467                         let tmp2 = bcx.ins().ishl_imm(v, 40);
468                         let n2 = bcx.ins().band_imm(tmp2, 0x00FF_0000_0000_0000u64 as i64);
469
470                         let tmp3 = bcx.ins().ishl_imm(v, 24);
471                         let n3 = bcx.ins().band_imm(tmp3, 0x0000_FF00_0000_0000u64 as i64);
472
473                         let tmp4 = bcx.ins().ishl_imm(v, 8);
474                         let n4 = bcx.ins().band_imm(tmp4, 0x0000_00FF_0000_0000u64 as i64);
475
476                         let tmp5 = bcx.ins().ushr_imm(v, 8);
477                         let n5 = bcx.ins().band_imm(tmp5, 0x0000_0000_FF00_0000u64 as i64);
478
479                         let tmp6 = bcx.ins().ushr_imm(v, 24);
480                         let n6 = bcx.ins().band_imm(tmp6, 0x0000_0000_00FF_0000u64 as i64);
481
482                         let tmp7 = bcx.ins().ushr_imm(v, 40);
483                         let n7 = bcx.ins().band_imm(tmp7, 0x0000_0000_0000_FF00u64 as i64);
484
485                         let tmp8 = bcx.ins().ushr_imm(v, 56);
486                         let n8 = bcx.ins().band_imm(tmp8, 0x0000_0000_0000_00FFu64 as i64);
487
488                         let or_tmp1 = bcx.ins().bor(n1, n2);
489                         let or_tmp2 = bcx.ins().bor(n3, n4);
490                         let or_tmp3 = bcx.ins().bor(n5, n6);
491                         let or_tmp4 = bcx.ins().bor(n7, n8);
492
493                         let or_tmp5 = bcx.ins().bor(or_tmp1, or_tmp2);
494                         let or_tmp6 = bcx.ins().bor(or_tmp3, or_tmp4);
495                         bcx.ins().bor(or_tmp5, or_tmp6)
496                     }
497                     ty => unimplemented!("bwap {}", ty),
498                 }
499             };
500             let res = CValue::by_val(swap(&mut fx.bcx, arg), fx.layout_of(T));
501             ret.write_cvalue(fx, res);
502         };
503         needs_drop, <T> () {
504             let needs_drop = if T.needs_drop(fx.tcx, ParamEnv::reveal_all()) {
505                 1
506             } else {
507                 0
508             };
509             let needs_drop = CValue::const_val(fx, fx.tcx.types.bool, needs_drop);
510             ret.write_cvalue(fx, needs_drop);
511         };
512         panic_if_uninhabited, <T> () {
513             if fx.layout_of(T).abi.is_uninhabited() {
514                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::panic_if_uninhabited for uninhabited type.");
515                 return;
516             }
517         };
518
519         volatile_load, (c ptr) {
520             // Cranelift treats loads as volatile by default
521             let inner_layout =
522                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
523             let val = CValue::by_ref(ptr.load_scalar(fx), inner_layout);
524             ret.write_cvalue(fx, val);
525         };
526         volatile_store, (v ptr, c val) {
527             // Cranelift treats stores as volatile by default
528             let dest = CPlace::for_addr(ptr, val.layout());
529             dest.write_cvalue(fx, val);
530         };
531
532         _ if intrinsic.starts_with("atomic_fence"), () {};
533         _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
534         _ if intrinsic.starts_with("atomic_load"), (c ptr) {
535             let inner_layout =
536                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
537             let val = CValue::by_ref(ptr.load_scalar(fx), inner_layout);
538             ret.write_cvalue(fx, val);
539         };
540         _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
541             let dest = CPlace::for_addr(ptr, val.layout());
542             dest.write_cvalue(fx, val);
543         };
544         _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
545             // Read old
546             let clif_ty = fx.clif_type(T).unwrap();
547             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
548             ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
549
550             // Write new
551             let dest = CPlace::for_addr(ptr, src.layout());
552             dest.write_cvalue(fx, src);
553         };
554         _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
555             // Read old
556             let clif_ty = fx.clif_type(T).unwrap();
557             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
558
559             // Compare
560             let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
561             let new = crate::common::codegen_select(&mut fx.bcx, is_eq, new, old); // Keep old if not equal to test_old
562
563             // Write new
564             fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
565
566             let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
567             ret.write_cvalue(fx, ret_val);
568         };
569
570         _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
571             atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
572         };
573         _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
574             atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
575         };
576         _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
577             atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
578         };
579         _ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, v src) {
580             atomic_binop_return_old! (fx, band_not<T>(ptr, src) -> ret);
581         };
582         _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
583             atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
584         };
585         _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
586             atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
587         };
588
589         _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
590             atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
591         };
592         _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
593             atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
594         };
595         _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
596             atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
597         };
598         _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
599             atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
600         };
601
602         expf32, (c flt) {
603             let res = fx.easy_call("expf", &[flt], fx.tcx.types.f32);
604             ret.write_cvalue(fx, res);
605         };
606         expf64, (c flt) {
607             let res = fx.easy_call("exp", &[flt], fx.tcx.types.f64);
608             ret.write_cvalue(fx, res);
609         };
610         exp2f32, (c flt) {
611             let res = fx.easy_call("exp2f", &[flt], fx.tcx.types.f32);
612             ret.write_cvalue(fx, res);
613         };
614         exp2f64, (c flt) {
615             let res = fx.easy_call("exp2", &[flt], fx.tcx.types.f64);
616             ret.write_cvalue(fx, res);
617         };
618         fabsf32, (c flt) {
619             let res = fx.easy_call("fabsf", &[flt], fx.tcx.types.f32);
620             ret.write_cvalue(fx, res);
621         };
622         fabsf64, (c flt) {
623             let res = fx.easy_call("fabs", &[flt], fx.tcx.types.f64);
624             ret.write_cvalue(fx, res);
625         };
626         sqrtf32, (c flt) {
627             let res = fx.easy_call("sqrtf", &[flt], fx.tcx.types.f32);
628             ret.write_cvalue(fx, res);
629         };
630         sqrtf64, (c flt) {
631             let res = fx.easy_call("sqrt", &[flt], fx.tcx.types.f64);
632             ret.write_cvalue(fx, res);
633         };
634         floorf32, (c flt) {
635             let res = fx.easy_call("floorf", &[flt], fx.tcx.types.f32);
636             ret.write_cvalue(fx, res);
637         };
638         floorf64, (c flt) {
639             let res = fx.easy_call("floor", &[flt], fx.tcx.types.f64);
640             ret.write_cvalue(fx, res);
641         };
642         ceilf32, (c flt) {
643             let res = fx.easy_call("ceilf", &[flt], fx.tcx.types.f32);
644             ret.write_cvalue(fx, res);
645         };
646         ceilf64, (c flt) {
647             let res = fx.easy_call("ceil", &[flt], fx.tcx.types.f64);
648             ret.write_cvalue(fx, res);
649         };
650
651         minnumf32, (c a, c b) {
652             let res = fx.easy_call("fminf", &[a, b], fx.tcx.types.f32);
653             ret.write_cvalue(fx, res);
654         };
655         minnumf64, (c a, c b) {
656             let res = fx.easy_call("fmin", &[a, b], fx.tcx.types.f64);
657             ret.write_cvalue(fx, res);
658         };
659         maxnumf32, (c a, c b) {
660             let res = fx.easy_call("fmaxf", &[a, b], fx.tcx.types.f32);
661             ret.write_cvalue(fx, res);
662         };
663         maxnumf64, (c a, c b) {
664             let res = fx.easy_call("fmax", &[a, b], fx.tcx.types.f64);
665             ret.write_cvalue(fx, res);
666         };
667
668     }
669
670     if let Some((_, dest)) = destination {
671         let ret_ebb = fx.get_ebb(dest);
672         fx.bcx.ins().jump(ret_ebb, &[]);
673     } else {
674         trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
675     }
676 }