]> git.lizzy.rs Git - rust.git/blob - src/intrinsics.rs
Rustup to rustc 1.32.0-nightly (1f57e4841 2018-11-23)
[rust.git] / src / intrinsics.rs
1 use crate::prelude::*;
2
3 macro_rules! intrinsic_pat {
4     (_) => {
5         _
6     };
7     ($name:ident) => {
8         stringify!($name)
9     }
10 }
11
12 macro_rules! intrinsic_arg {
13     (c $fx:expr, $arg:ident) => {
14         $arg
15     };
16     (v $fx:expr, $arg:ident) => {
17         $arg.load_value($fx)
18     };
19 }
20
21 macro_rules! intrinsic_substs {
22     ($substs:expr, $index:expr,) => {};
23     ($substs:expr, $index:expr, $first:ident $(,$rest:ident)*) => {
24         let $first = $substs.type_at($index);
25         intrinsic_substs!($substs, $index+1, $($rest),*);
26     };
27 }
28
29 macro_rules! intrinsic_match {
30     ($fx:expr, $intrinsic:expr, $substs:expr, $args:expr, $(
31         $($name:tt)|+ $(if $cond:expr)?, $(<$($subst:ident),*>)? ($($a:ident $arg:ident),*) $content:block;
32     )*) => {
33         match $intrinsic {
34             $(
35                 $(intrinsic_pat!($name))|* $(if $cond)? => {
36                     #[allow(unused_parens, non_snake_case)]
37                     {
38                         $(
39                             intrinsic_substs!($substs, 0, $($subst),*);
40                         )?
41                         if let [$($arg),*] = *$args {
42                             let ($($arg),*) = (
43                                 $(intrinsic_arg!($a $fx, $arg)),*
44                             );
45                             #[warn(unused_parens, non_snake_case)]
46                             {
47                                 $content
48                             }
49                         } else {
50                             bug!("wrong number of args for intrinsic {:?}", $intrinsic);
51                         }
52                     }
53                 }
54             )*
55             _ => unimpl!("unsupported intrinsic {}", $intrinsic),
56         }
57     };
58 }
59
60 macro_rules! atomic_binop_return_old {
61     ($fx:expr, $op:ident<$T:ident>($ptr:ident, $src:ident) -> $ret:ident) => {
62         let clif_ty = $fx.clif_type($T).unwrap();
63         let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
64         let new = $fx.bcx.ins().band(old, $src);
65         $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
66         $ret.write_cvalue($fx, CValue::ByVal(old, $fx.layout_of($T)));
67     };
68 }
69
70 macro_rules! atomic_minmax {
71     ($fx:expr, $cc:expr, <$T:ident> ($ptr:ident, $src:ident) -> $ret:ident) => {
72         // Read old
73         let clif_ty = $fx.clif_type($T).unwrap();
74         let old = $fx.bcx.ins().load(clif_ty, MemFlags::new(), $ptr, 0);
75
76         // Compare
77         let is_eq = $fx.bcx.ins().icmp(IntCC::SignedGreaterThan, old, $src);
78         let new = crate::common::codegen_select(&mut $fx.bcx, is_eq, old, $src);
79
80         // Write new
81         $fx.bcx.ins().store(MemFlags::new(), new, $ptr, 0);
82
83         let ret_val = CValue::ByVal(old, $ret.layout());
84         $ret.write_cvalue($fx, ret_val);
85     };
86 }
87
88 pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
89     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
90     def_id: DefId,
91     substs: &'tcx Substs,
92     args: Vec<CValue<'tcx>>,
93     destination: Option<(CPlace<'tcx>, BasicBlock)>,
94 ) {
95     let intrinsic = fx.tcx.item_name(def_id).as_str();
96     let intrinsic = &intrinsic[..];
97
98     let ret = match destination {
99         Some((place, _)) => place,
100         None => {
101             // Insert non returning intrinsics here
102             match intrinsic {
103                 "abort" => {
104                     trap_panic(&mut fx.bcx);
105                 }
106                 "unreachable" => {
107                     trap_unreachable(&mut fx.bcx);
108                 }
109                 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
110             }
111             return;
112         }
113     };
114
115     let u64_layout = fx.layout_of(fx.tcx.types.u64);
116     let usize_layout = fx.layout_of(fx.tcx.types.usize);
117
118     intrinsic_match! {
119         fx, intrinsic, substs, args,
120
121         assume, (c _a) {};
122         arith_offset, (v base, v offset) {
123             let res = fx.bcx.ins().iadd(base, offset);
124             let res = CValue::ByVal(res, ret.layout());
125             ret.write_cvalue(fx, res);
126         };
127         likely | unlikely, (c a) {
128             ret.write_cvalue(fx, a);
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 discr = crate::base::trans_get_discriminant(fx, val, ret.layout());
147             ret.write_cvalue(fx, discr);
148         };
149         size_of, <T> () {
150             let size_of = fx.layout_of(T).size.bytes();
151             let size_of = CValue::const_val(fx, usize_layout.ty, size_of as i64);
152             ret.write_cvalue(fx, size_of);
153         };
154         size_of_val, <T> (c ptr) {
155             let layout = fx.layout_of(T);
156             let size = match &layout.ty.sty {
157                 _ if !layout.is_unsized() => fx
158                     .bcx
159                     .ins()
160                     .iconst(fx.pointer_type, layout.size.bytes() as i64),
161                 ty::Slice(elem) => {
162                     let len = ptr.load_value_pair(fx).1;
163                     let elem_size = fx.layout_of(elem).size.bytes();
164                     fx.bcx.ins().imul_imm(len, elem_size as i64)
165                 }
166                 ty::Dynamic(..) => crate::vtable::size_of_obj(fx, ptr),
167                 ty => bug!("size_of_val for unknown unsized type {:?}", ty),
168             };
169             ret.write_cvalue(fx, CValue::ByVal(size, usize_layout));
170         };
171         min_align_of, <T> () {
172             let min_align = fx.layout_of(T).align.abi.bytes();
173             let min_align = CValue::const_val(fx, usize_layout.ty, min_align as i64);
174             ret.write_cvalue(fx, min_align);
175         };
176         min_align_of_val, <T> (c ptr) {
177             let layout = fx.layout_of(T);
178             let align = match &layout.ty.sty {
179                 _ if !layout.is_unsized() => fx
180                     .bcx
181                     .ins()
182                     .iconst(fx.pointer_type, layout.align.abi.bytes() as i64),
183                 ty::Slice(elem) => {
184                     let align = fx.layout_of(elem).align.abi.bytes() as i64;
185                     fx.bcx.ins().iconst(fx.pointer_type, align)
186                 }
187                 ty::Dynamic(..) => crate::vtable::min_align_of_obj(fx, ptr),
188                 ty => unimplemented!("min_align_of_val for {:?}", ty),
189             };
190             ret.write_cvalue(fx, CValue::ByVal(align, usize_layout));
191         };
192         type_id, <T> () {
193             let type_id = fx.tcx.type_id_hash(T);
194             let type_id = CValue::const_val(fx, u64_layout.ty, type_id as i64);
195             ret.write_cvalue(fx, type_id);
196         };
197         _ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
198             let bin_op = match intrinsic {
199                 "unchecked_div" | "exact_div" => BinOp::Div,
200                 "unchecked_rem" => BinOp::Rem,
201                 "unchecked_shl" => BinOp::Shl,
202                 "unchecked_shr" => BinOp::Shr,
203                 _ => unimplemented!("intrinsic {}", intrinsic),
204             };
205             let res = match ret.layout().ty.sty {
206                 ty::Uint(_) => crate::base::trans_int_binop(
207                     fx,
208                     bin_op,
209                     x,
210                     y,
211                     ret.layout().ty,
212                     false,
213                 ),
214                 ty::Int(_) => crate::base::trans_int_binop(
215                     fx,
216                     bin_op,
217                     x,
218                     y,
219                     ret.layout().ty,
220                     true,
221                 ),
222                 _ => panic!(),
223             };
224             ret.write_cvalue(fx, res);
225         };
226         _ if intrinsic.ends_with("_with_overflow"), <T> (c x, c y) {
227             assert_eq!(x.layout().ty, y.layout().ty);
228             let bin_op = match intrinsic {
229                 "add_with_overflow" => BinOp::Add,
230                 "sub_with_overflow" => BinOp::Sub,
231                 "mul_with_overflow" => BinOp::Mul,
232                 _ => unimplemented!("intrinsic {}", intrinsic),
233             };
234             let res = match T.sty {
235                 ty::Uint(_) => crate::base::trans_checked_int_binop(
236                     fx,
237                     bin_op,
238                     x,
239                     y,
240                     ret.layout().ty,
241                     false,
242                 ),
243                 ty::Int(_) => crate::base::trans_checked_int_binop(
244                     fx,
245                     bin_op,
246                     x,
247                     y,
248                     ret.layout().ty,
249                     true,
250                 ),
251                 _ => panic!(),
252             };
253             ret.write_cvalue(fx, res);
254         };
255         _ if intrinsic.starts_with("overflowing_"), <T> (c x, c y) {
256             assert_eq!(x.layout().ty, y.layout().ty);
257             let bin_op = match intrinsic {
258                 "overflowing_add" => BinOp::Add,
259                 "overflowing_sub" => BinOp::Sub,
260                 "overflowing_mul" => BinOp::Mul,
261                 _ => unimplemented!("intrinsic {}", intrinsic),
262             };
263             let res = match T.sty {
264                 ty::Uint(_) => crate::base::trans_int_binop(
265                     fx,
266                     bin_op,
267                     x,
268                     y,
269                     ret.layout().ty,
270                     false,
271                 ),
272                 ty::Int(_) => crate::base::trans_int_binop(
273                     fx,
274                     bin_op,
275                     x,
276                     y,
277                     ret.layout().ty,
278                     true,
279                 ),
280                 _ => panic!(),
281             };
282             ret.write_cvalue(fx, res);
283         };
284         rotate_left, <T>(v x, v y) {
285             let layout = fx.layout_of(T);
286             let res = fx.bcx.ins().rotl(x, y);
287             ret.write_cvalue(fx, CValue::ByVal(res, layout));
288         };
289         rotate_right, <T>(v x, v y) {
290             let layout = fx.layout_of(T);
291             let res = fx.bcx.ins().rotr(x, y);
292             ret.write_cvalue(fx, CValue::ByVal(res, layout));
293         };
294         offset, (v base, v offset) {
295             let res = fx.bcx.ins().iadd(base, offset);
296             ret.write_cvalue(fx, CValue::ByVal(res, args[0].layout()));
297         };
298         transmute, <src_ty, dst_ty> (c from) {
299             assert_eq!(from.layout().ty, src_ty);
300             let addr = from.force_stack(fx);
301             let dst_layout = fx.layout_of(dst_ty);
302             ret.write_cvalue(fx, CValue::ByRef(addr, dst_layout))
303         };
304         init, <T> () {
305             let layout = fx.layout_of(T);
306             let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
307                 kind: StackSlotKind::ExplicitSlot,
308                 size: layout.size.bytes() as u32,
309                 offset: None,
310             });
311             let addr = fx.bcx.ins().stack_addr(pointer_ty(fx.tcx), stack_slot, 0);
312             let zero_val = fx.bcx.ins().iconst(types::I8, 0);
313             let len_val = fx.bcx.ins().iconst(pointer_ty(fx.tcx), layout.size.bytes() as i64);
314             fx.bcx.call_memset(fx.module.target_config(), addr, zero_val, len_val);
315
316             let uninit_place = CPlace::from_stack_slot(fx, stack_slot, T);
317             let uninit_val = uninit_place.to_cvalue(fx);
318             ret.write_cvalue(fx, uninit_val);
319         };
320         write_bytes, (v dst, v val, v count) {
321             fx.bcx.call_memset(fx.module.target_config(), dst, val, count);
322         };
323         uninit, <T> () {
324             let layout = fx.layout_of(T);
325             let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
326                 kind: StackSlotKind::ExplicitSlot,
327                 size: layout.size.bytes() as u32,
328                 offset: None,
329             });
330
331             let uninit_place = CPlace::from_stack_slot(fx, stack_slot, T);
332             let uninit_val = uninit_place.to_cvalue(fx);
333             ret.write_cvalue(fx, uninit_val);
334         };
335         ctlz | ctlz_nonzero, <T> (v arg) {
336             let res = CValue::ByVal(fx.bcx.ins().clz(arg), fx.layout_of(T));
337             ret.write_cvalue(fx, res);
338         };
339         cttz | cttz_nonzero, <T> (v arg) {
340             let res = CValue::ByVal(fx.bcx.ins().clz(arg), fx.layout_of(T));
341             ret.write_cvalue(fx, res);
342         };
343         ctpop, <T> (v arg) {
344             let res = CValue::ByVal(fx.bcx.ins().popcnt(arg), fx.layout_of(T));
345             ret.write_cvalue(fx, res);
346         };
347         bitreverse, <T> (v arg) {
348             let res = CValue::ByVal(fx.bcx.ins().bitrev(arg), fx.layout_of(T));
349             ret.write_cvalue(fx, res);
350         };
351         needs_drop, <T> () {
352             let needs_drop = if T.needs_drop(fx.tcx, ParamEnv::reveal_all()) {
353                 1
354             } else {
355                 0
356             };
357             let needs_drop = CValue::const_val(fx, fx.tcx.types.bool, needs_drop);
358             ret.write_cvalue(fx, needs_drop);
359         };
360
361         _ if intrinsic.starts_with("atomic_fence"), () {};
362         _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
363         _ if intrinsic.starts_with("atomic_load"), (c ptr) {
364             let inner_layout =
365                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
366             let val = CValue::ByRef(ptr.load_value(fx), inner_layout);
367             ret.write_cvalue(fx, val);
368         };
369         _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
370             let dest = CPlace::Addr(ptr, None, val.layout());
371             dest.write_cvalue(fx, val);
372         };
373         _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
374             // Read old
375             let clif_ty = fx.clif_type(T).unwrap();
376             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
377             ret.write_cvalue(fx, CValue::ByVal(old, fx.layout_of(T)));
378
379             // Write new
380             let dest = CPlace::Addr(ptr, None, src.layout());
381             dest.write_cvalue(fx, src);
382         };
383         _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
384             // Read old
385             let clif_ty = fx.clif_type(T).unwrap();
386             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
387
388             // Compare
389             let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
390             let new = crate::common::codegen_select(&mut fx.bcx, is_eq, old, new); // Keep old if not equal to test_old
391
392             // Write new
393             fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
394
395             let ret_val = CValue::ByValPair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
396             ret.write_cvalue(fx, ret_val);
397         };
398
399         _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
400             atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
401         };
402         _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
403             atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
404         };
405         _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
406             atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
407         };
408         _ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, v src) {
409             atomic_binop_return_old! (fx, bnand<T>(ptr, src) -> ret);
410         };
411         _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
412             atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
413         };
414         _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
415             atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
416         };
417
418         _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
419             atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
420         };
421         _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
422             atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
423         };
424         _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
425             atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
426         };
427         _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
428             atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
429         };
430     }
431
432     if let Some((_, dest)) = destination {
433         let ret_ebb = fx.get_ebb(dest);
434         fx.bcx.ins().jump(ret_ebb, &[]);
435     } else {
436         trap_unreachable(&mut fx.bcx);
437     }
438 }