]> git.lizzy.rs Git - rust.git/blob - src/intrinsics.rs
Rustup to rustc 1.37.0-nightly (de02101e6 2019-06-22)
[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 intrinisc::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         type_id, <T> () {
190             let type_id = fx.tcx.type_id_hash(T);
191             let type_id = CValue::const_val(fx, u64_layout.ty, type_id as i64);
192             ret.write_cvalue(fx, type_id);
193         };
194         _ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
195             // FIXME trap on overflow
196             let bin_op = match intrinsic {
197                 "unchecked_sub" => BinOp::Sub,
198                 "unchecked_div" | "exact_div" => BinOp::Div,
199                 "unchecked_rem" => BinOp::Rem,
200                 "unchecked_shl" => BinOp::Shl,
201                 "unchecked_shr" => BinOp::Shr,
202                 _ => unimplemented!("intrinsic {}", intrinsic),
203             };
204             let res = match ret.layout().ty.sty {
205                 ty::Uint(_) => crate::base::trans_int_binop(
206                     fx,
207                     bin_op,
208                     x,
209                     y,
210                     ret.layout().ty,
211                     false,
212                 ),
213                 ty::Int(_) => crate::base::trans_int_binop(
214                     fx,
215                     bin_op,
216                     x,
217                     y,
218                     ret.layout().ty,
219                     true,
220                 ),
221                 _ => panic!(),
222             };
223             ret.write_cvalue(fx, res);
224         };
225         _ if intrinsic.ends_with("_with_overflow"), <T> (c x, c y) {
226             assert_eq!(x.layout().ty, y.layout().ty);
227             let bin_op = match intrinsic {
228                 "add_with_overflow" => BinOp::Add,
229                 "sub_with_overflow" => BinOp::Sub,
230                 "mul_with_overflow" => BinOp::Mul,
231                 _ => unimplemented!("intrinsic {}", intrinsic),
232             };
233             let res = match T.sty {
234                 ty::Uint(_) => crate::base::trans_checked_int_binop(
235                     fx,
236                     bin_op,
237                     x,
238                     y,
239                     ret.layout().ty,
240                     false,
241                 ),
242                 ty::Int(_) => crate::base::trans_checked_int_binop(
243                     fx,
244                     bin_op,
245                     x,
246                     y,
247                     ret.layout().ty,
248                     true,
249                 ),
250                 _ => panic!(),
251             };
252             ret.write_cvalue(fx, res);
253         };
254         _ if intrinsic.starts_with("overflowing_"), <T> (c x, c y) {
255             assert_eq!(x.layout().ty, y.layout().ty);
256             let bin_op = match intrinsic {
257                 "overflowing_add" => BinOp::Add,
258                 "overflowing_sub" => BinOp::Sub,
259                 "overflowing_mul" => BinOp::Mul,
260                 _ => unimplemented!("intrinsic {}", intrinsic),
261             };
262             let res = match T.sty {
263                 ty::Uint(_) => crate::base::trans_int_binop(
264                     fx,
265                     bin_op,
266                     x,
267                     y,
268                     ret.layout().ty,
269                     false,
270                 ),
271                 ty::Int(_) => crate::base::trans_int_binop(
272                     fx,
273                     bin_op,
274                     x,
275                     y,
276                     ret.layout().ty,
277                     true,
278                 ),
279                 _ => panic!(),
280             };
281             ret.write_cvalue(fx, res);
282         };
283         _ if intrinsic.starts_with("saturating_"), <T> (c x, c y) {
284             // FIXME implement saturating behavior
285             assert_eq!(x.layout().ty, y.layout().ty);
286             let bin_op = match intrinsic {
287                 "saturating_add" => BinOp::Add,
288                 "saturating_sub" => BinOp::Sub,
289                 "saturating_mul" => BinOp::Mul,
290                 _ => unimplemented!("intrinsic {}", intrinsic),
291             };
292             let res = match T.sty {
293                 ty::Uint(_) => crate::base::trans_int_binop(
294                     fx,
295                     bin_op,
296                     x,
297                     y,
298                     ret.layout().ty,
299                     false,
300                 ),
301                 ty::Int(_) => crate::base::trans_int_binop(
302                     fx,
303                     bin_op,
304                     x,
305                     y,
306                     ret.layout().ty,
307                     true,
308                 ),
309                 _ => panic!(),
310             };
311             ret.write_cvalue(fx, res);
312         };
313         rotate_left, <T>(v x, v y) {
314             let layout = fx.layout_of(T);
315             let res = fx.bcx.ins().rotl(x, y);
316             ret.write_cvalue(fx, CValue::by_val(res, layout));
317         };
318         rotate_right, <T>(v x, v y) {
319             let layout = fx.layout_of(T);
320             let res = fx.bcx.ins().rotr(x, y);
321             ret.write_cvalue(fx, CValue::by_val(res, layout));
322         };
323
324         // The only difference between offset and arith_offset is regarding UB. Because Cranelift
325         // doesn't have UB both are codegen'ed the same way
326         offset | arith_offset, (c base, v offset) {
327             let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
328             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
329             let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
330             let base_val = base.load_scalar(fx);
331             let res = fx.bcx.ins().iadd(base_val, ptr_diff);
332             ret.write_cvalue(fx, CValue::by_val(res, args[0].layout()));
333         };
334
335         transmute, <src_ty, dst_ty> (c from) {
336             assert_eq!(from.layout().ty, src_ty);
337             let addr = from.force_stack(fx);
338             let dst_layout = fx.layout_of(dst_ty);
339             ret.write_cvalue(fx, CValue::by_ref(addr, dst_layout))
340         };
341         init, () {
342             if ret.layout().abi == Abi::Uninhabited {
343                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::init for uninhabited type.");
344                 return;
345             }
346
347             match ret {
348                 CPlace::NoPlace(_layout) => {}
349                 CPlace::Var(var, layout) => {
350                     let clif_ty = fx.clif_type(layout.ty).unwrap();
351                     let val = match clif_ty {
352                         types::I8 | types::I16 | types::I32 | types::I64 => fx.bcx.ins().iconst(clif_ty, 0),
353                         types::F32 => {
354                             let zero = fx.bcx.ins().iconst(types::I32, 0);
355                             fx.bcx.ins().bitcast(types::F32, zero)
356                         }
357                         types::F64 => {
358                             let zero = fx.bcx.ins().iconst(types::I64, 0);
359                             fx.bcx.ins().bitcast(types::F64, zero)
360                         }
361                         _ => panic!("clif_type returned {}", clif_ty),
362                     };
363                     fx.bcx.def_var(mir_var(var), val);
364                 }
365                 _ => {
366                     let addr = ret.to_addr(fx);
367                     let layout = ret.layout();
368                     fx.bcx.emit_small_memset(fx.module.target_config(), addr, 0, layout.size.bytes(), 1);
369                 }
370             }
371         };
372         write_bytes, (c dst, v val, v count) {
373             let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
374             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
375             let count = fx.bcx.ins().imul_imm(count, pointee_size as i64);
376             let dst_ptr = dst.load_scalar(fx);
377             fx.bcx.call_memset(fx.module.target_config(), dst_ptr, val, count);
378         };
379         uninit, <T> () {
380             if ret.layout().abi == Abi::Uninhabited {
381                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::uninit for uninhabited type.");
382                 return;
383             }
384
385             let uninit_place = CPlace::new_stack_slot(fx, T);
386             let uninit_val = uninit_place.to_cvalue(fx);
387             ret.write_cvalue(fx, uninit_val);
388         };
389         ctlz | ctlz_nonzero, <T> (v arg) {
390             let res = CValue::by_val(fx.bcx.ins().clz(arg), fx.layout_of(T));
391             ret.write_cvalue(fx, res);
392         };
393         cttz | cttz_nonzero, <T> (v arg) {
394             let res = CValue::by_val(fx.bcx.ins().ctz(arg), fx.layout_of(T));
395             ret.write_cvalue(fx, res);
396         };
397         ctpop, <T> (v arg) {
398             let res = CValue::by_val(fx.bcx.ins().popcnt(arg), fx.layout_of(T));
399             ret.write_cvalue(fx, res);
400         };
401         bitreverse, <T> (v arg) {
402             let res = CValue::by_val(fx.bcx.ins().bitrev(arg), fx.layout_of(T));
403             ret.write_cvalue(fx, res);
404         };
405         needs_drop, <T> () {
406             let needs_drop = if T.needs_drop(fx.tcx, ParamEnv::reveal_all()) {
407                 1
408             } else {
409                 0
410             };
411             let needs_drop = CValue::const_val(fx, fx.tcx.types.bool, needs_drop);
412             ret.write_cvalue(fx, needs_drop);
413         };
414         panic_if_uninhabited, <T> () {
415             if fx.layout_of(T).abi.is_uninhabited() {
416                 crate::trap::trap_panic(fx, "[panic] Called intrinsic::panic_if_uninhabited for uninhabited type.");
417                 return;
418             }
419         };
420
421         _ if intrinsic.starts_with("atomic_fence"), () {};
422         _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
423         _ if intrinsic.starts_with("atomic_load"), (c ptr) {
424             let inner_layout =
425                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
426             let val = CValue::by_ref(ptr.load_scalar(fx), inner_layout);
427             ret.write_cvalue(fx, val);
428         };
429         _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
430             let dest = CPlace::for_addr(ptr, val.layout());
431             dest.write_cvalue(fx, val);
432         };
433         _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
434             // Read old
435             let clif_ty = fx.clif_type(T).unwrap();
436             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
437             ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
438
439             // Write new
440             let dest = CPlace::for_addr(ptr, src.layout());
441             dest.write_cvalue(fx, src);
442         };
443         _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
444             // Read old
445             let clif_ty = fx.clif_type(T).unwrap();
446             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
447
448             // Compare
449             let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
450             let new = crate::common::codegen_select(&mut fx.bcx, is_eq, new, old); // Keep old if not equal to test_old
451
452             // Write new
453             fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
454
455             let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
456             ret.write_cvalue(fx, ret_val);
457         };
458
459         _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
460             atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
461         };
462         _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
463             atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
464         };
465         _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
466             atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
467         };
468         _ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, v src) {
469             atomic_binop_return_old! (fx, band_not<T>(ptr, src) -> ret);
470         };
471         _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
472             atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
473         };
474         _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
475             atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
476         };
477
478         _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
479             atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
480         };
481         _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
482             atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
483         };
484         _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
485             atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
486         };
487         _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
488             atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
489         };
490     }
491
492     if let Some((_, dest)) = destination {
493         let ret_ebb = fx.get_ebb(dest);
494         fx.bcx.ins().jump(ret_ebb, &[]);
495     } else {
496         trap_unreachable(fx, "[corruption] Diverging intrinsic returned.");
497     }
498 }