]> git.lizzy.rs Git - rust.git/blob - src/intrinsics.rs
Fix cttz{,_nonzero} intrinsics
[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_scalar($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         likely | unlikely, (c a) {
123             ret.write_cvalue(fx, a);
124         };
125         breakpoint, () {
126             fx.bcx.ins().debugtrap();
127         };
128         copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
129             let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
130             let elem_size = fx
131                 .bcx
132                 .ins()
133                 .iconst(fx.pointer_type, elem_size as i64);
134             assert_eq!(args.len(), 3);
135             let byte_amount = fx.bcx.ins().imul(count, elem_size);
136
137             if intrinsic.ends_with("_nonoverlapping") {
138                 fx.bcx.call_memcpy(fx.module.target_config(), dst, src, byte_amount);
139             } else {
140                 fx.bcx.call_memmove(fx.module.target_config(), dst, src, byte_amount);
141             }
142         };
143         discriminant_value, (c val) {
144             let discr = crate::base::trans_get_discriminant(fx, val, ret.layout());
145             ret.write_cvalue(fx, discr);
146         };
147         size_of, <T> () {
148             let size_of = fx.layout_of(T).size.bytes();
149             let size_of = CValue::const_val(fx, usize_layout.ty, size_of as i64);
150             ret.write_cvalue(fx, size_of);
151         };
152         size_of_val, <T> (c ptr) {
153             let layout = fx.layout_of(T);
154             let size = if layout.is_unsized() {
155                 let (_ptr, info) = ptr.load_value_pair(fx);
156                 let (size, _align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
157                 size
158             } else {
159                 fx
160                     .bcx
161                     .ins()
162                     .iconst(fx.pointer_type, layout.size.bytes() as i64)
163             };
164             ret.write_cvalue(fx, CValue::ByVal(size, usize_layout));
165         };
166         min_align_of, <T> () {
167             let min_align = fx.layout_of(T).align.abi.bytes();
168             let min_align = CValue::const_val(fx, usize_layout.ty, min_align as i64);
169             ret.write_cvalue(fx, min_align);
170         };
171         min_align_of_val, <T> (c ptr) {
172             let layout = fx.layout_of(T);
173             let align = if layout.is_unsized() {
174                 let (_ptr, info) = ptr.load_value_pair(fx);
175                 let (_size, align) = crate::unsize::size_and_align_of_dst(fx, layout.ty, info);
176                 align
177             } else {
178                 fx
179                     .bcx
180                     .ins()
181                     .iconst(fx.pointer_type, layout.align.abi.bytes() as i64)
182             };
183             ret.write_cvalue(fx, CValue::ByVal(align, usize_layout));
184         };
185         type_id, <T> () {
186             let type_id = fx.tcx.type_id_hash(T);
187             let type_id = CValue::const_val(fx, u64_layout.ty, type_id as i64);
188             ret.write_cvalue(fx, type_id);
189         };
190         _ if intrinsic.starts_with("unchecked_") || intrinsic == "exact_div", (c x, c y) {
191             let bin_op = match intrinsic {
192                 "unchecked_div" | "exact_div" => BinOp::Div,
193                 "unchecked_rem" => BinOp::Rem,
194                 "unchecked_shl" => BinOp::Shl,
195                 "unchecked_shr" => BinOp::Shr,
196                 _ => unimplemented!("intrinsic {}", intrinsic),
197             };
198             let res = match ret.layout().ty.sty {
199                 ty::Uint(_) => crate::base::trans_int_binop(
200                     fx,
201                     bin_op,
202                     x,
203                     y,
204                     ret.layout().ty,
205                     false,
206                 ),
207                 ty::Int(_) => crate::base::trans_int_binop(
208                     fx,
209                     bin_op,
210                     x,
211                     y,
212                     ret.layout().ty,
213                     true,
214                 ),
215                 _ => panic!(),
216             };
217             ret.write_cvalue(fx, res);
218         };
219         _ if intrinsic.ends_with("_with_overflow"), <T> (c x, c y) {
220             assert_eq!(x.layout().ty, y.layout().ty);
221             let bin_op = match intrinsic {
222                 "add_with_overflow" => BinOp::Add,
223                 "sub_with_overflow" => BinOp::Sub,
224                 "mul_with_overflow" => BinOp::Mul,
225                 _ => unimplemented!("intrinsic {}", intrinsic),
226             };
227             let res = match T.sty {
228                 ty::Uint(_) => crate::base::trans_checked_int_binop(
229                     fx,
230                     bin_op,
231                     x,
232                     y,
233                     ret.layout().ty,
234                     false,
235                 ),
236                 ty::Int(_) => crate::base::trans_checked_int_binop(
237                     fx,
238                     bin_op,
239                     x,
240                     y,
241                     ret.layout().ty,
242                     true,
243                 ),
244                 _ => panic!(),
245             };
246             ret.write_cvalue(fx, res);
247         };
248         _ if intrinsic.starts_with("overflowing_"), <T> (c x, c y) {
249             assert_eq!(x.layout().ty, y.layout().ty);
250             let bin_op = match intrinsic {
251                 "overflowing_add" => BinOp::Add,
252                 "overflowing_sub" => BinOp::Sub,
253                 "overflowing_mul" => BinOp::Mul,
254                 _ => unimplemented!("intrinsic {}", intrinsic),
255             };
256             let res = match T.sty {
257                 ty::Uint(_) => crate::base::trans_int_binop(
258                     fx,
259                     bin_op,
260                     x,
261                     y,
262                     ret.layout().ty,
263                     false,
264                 ),
265                 ty::Int(_) => crate::base::trans_int_binop(
266                     fx,
267                     bin_op,
268                     x,
269                     y,
270                     ret.layout().ty,
271                     true,
272                 ),
273                 _ => panic!(),
274             };
275             ret.write_cvalue(fx, res);
276         };
277         rotate_left, <T>(v x, v y) {
278             let layout = fx.layout_of(T);
279             let res = fx.bcx.ins().rotl(x, y);
280             ret.write_cvalue(fx, CValue::ByVal(res, layout));
281         };
282         rotate_right, <T>(v x, v y) {
283             let layout = fx.layout_of(T);
284             let res = fx.bcx.ins().rotr(x, y);
285             ret.write_cvalue(fx, CValue::ByVal(res, layout));
286         };
287
288         // The only difference between offset and arith_offset is regarding UB. Because Cranelift
289         // doesn't have UB both are codegen'ed the same way
290         offset | arith_offset, (c base, v offset) {
291             let pointee_ty = base.layout().ty.builtin_deref(true).unwrap().ty;
292             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
293             let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
294             let base_val = base.load_scalar(fx);
295             let res = fx.bcx.ins().iadd(base_val, ptr_diff);
296             ret.write_cvalue(fx, CValue::ByVal(res, args[0].layout()));
297         };
298
299         transmute, <src_ty, dst_ty> (c from) {
300             assert_eq!(from.layout().ty, src_ty);
301             let addr = from.force_stack(fx);
302             let dst_layout = fx.layout_of(dst_ty);
303             ret.write_cvalue(fx, CValue::ByRef(addr, dst_layout))
304         };
305         init, <T> () {
306             let layout = fx.layout_of(T);
307             let inited_place = CPlace::new_stack_slot(fx, T);
308             let addr = inited_place.to_addr(fx);
309             let zero_val = fx.bcx.ins().iconst(types::I8, 0);
310             let len_val = fx.bcx.ins().iconst(pointer_ty(fx.tcx), layout.size.bytes() as i64);
311             fx.bcx.call_memset(fx.module.target_config(), addr, zero_val, len_val);
312
313             let inited_val = inited_place.to_cvalue(fx);
314             ret.write_cvalue(fx, inited_val);
315         };
316         write_bytes, (c dst, v val, v count) {
317             let pointee_ty = dst.layout().ty.builtin_deref(true).unwrap().ty;
318             let pointee_size = fx.layout_of(pointee_ty).size.bytes();
319             let count = fx.bcx.ins().imul_imm(count, pointee_size as i64);
320             let dst_ptr = dst.load_scalar(fx);
321             fx.bcx.call_memset(fx.module.target_config(), dst_ptr, val, count);
322         };
323         uninit, <T> () {
324             let uninit_place = CPlace::new_stack_slot(fx, T);
325             let uninit_val = uninit_place.to_cvalue(fx);
326             ret.write_cvalue(fx, uninit_val);
327         };
328         ctlz | ctlz_nonzero, <T> (v arg) {
329             let res = CValue::ByVal(fx.bcx.ins().clz(arg), fx.layout_of(T));
330             ret.write_cvalue(fx, res);
331         };
332         cttz | cttz_nonzero, <T> (v arg) {
333             let res = CValue::ByVal(fx.bcx.ins().ctz(arg), fx.layout_of(T));
334             ret.write_cvalue(fx, res);
335         };
336         ctpop, <T> (v arg) {
337             let res = CValue::ByVal(fx.bcx.ins().popcnt(arg), fx.layout_of(T));
338             ret.write_cvalue(fx, res);
339         };
340         bitreverse, <T> (v arg) {
341             let res = CValue::ByVal(fx.bcx.ins().bitrev(arg), fx.layout_of(T));
342             ret.write_cvalue(fx, res);
343         };
344         needs_drop, <T> () {
345             let needs_drop = if T.needs_drop(fx.tcx, ParamEnv::reveal_all()) {
346                 1
347             } else {
348                 0
349             };
350             let needs_drop = CValue::const_val(fx, fx.tcx.types.bool, needs_drop);
351             ret.write_cvalue(fx, needs_drop);
352         };
353         panic_if_uninhabited, <T> () {
354             if fx.layout_of(T).abi.is_uninhabited() {
355                 crate::trap::trap_panic(&mut fx.bcx);
356                 return;
357             }
358         };
359
360         _ if intrinsic.starts_with("atomic_fence"), () {};
361         _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
362         _ if intrinsic.starts_with("atomic_load"), (c ptr) {
363             let inner_layout =
364                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
365             let val = CValue::ByRef(ptr.load_scalar(fx), inner_layout);
366             ret.write_cvalue(fx, val);
367         };
368         _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
369             let dest = CPlace::Addr(ptr, None, val.layout());
370             dest.write_cvalue(fx, val);
371         };
372         _ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
373             // Read old
374             let clif_ty = fx.clif_type(T).unwrap();
375             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
376             ret.write_cvalue(fx, CValue::ByVal(old, fx.layout_of(T)));
377
378             // Write new
379             let dest = CPlace::Addr(ptr, None, src.layout());
380             dest.write_cvalue(fx, src);
381         };
382         _ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*
383             // Read old
384             let clif_ty = fx.clif_type(T).unwrap();
385             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
386
387             // Compare
388             let is_eq = fx.bcx.ins().icmp(IntCC::Equal, old, test_old);
389             let new = crate::common::codegen_select(&mut fx.bcx, is_eq, old, new); // Keep old if not equal to test_old
390
391             // Write new
392             fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
393
394             let ret_val = CValue::ByValPair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
395             ret.write_cvalue(fx, ret_val);
396         };
397
398         _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
399             atomic_binop_return_old! (fx, iadd<T>(ptr, amount) -> ret);
400         };
401         _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
402             atomic_binop_return_old! (fx, isub<T>(ptr, amount) -> ret);
403         };
404         _ if intrinsic.starts_with("atomic_and"), <T> (v ptr, v src) {
405             atomic_binop_return_old! (fx, band<T>(ptr, src) -> ret);
406         };
407         _ if intrinsic.starts_with("atomic_nand"), <T> (v ptr, v src) {
408             atomic_binop_return_old! (fx, bnand<T>(ptr, src) -> ret);
409         };
410         _ if intrinsic.starts_with("atomic_or"), <T> (v ptr, v src) {
411             atomic_binop_return_old! (fx, bor<T>(ptr, src) -> ret);
412         };
413         _ if intrinsic.starts_with("atomic_xor"), <T> (v ptr, v src) {
414             atomic_binop_return_old! (fx, bxor<T>(ptr, src) -> ret);
415         };
416
417         _ if intrinsic.starts_with("atomic_max"), <T> (v ptr, v src) {
418             atomic_minmax!(fx, IntCC::SignedGreaterThan, <T> (ptr, src) -> ret);
419         };
420         _ if intrinsic.starts_with("atomic_umax"), <T> (v ptr, v src) {
421             atomic_minmax!(fx, IntCC::UnsignedGreaterThan, <T> (ptr, src) -> ret);
422         };
423         _ if intrinsic.starts_with("atomic_min"), <T> (v ptr, v src) {
424             atomic_minmax!(fx, IntCC::SignedLessThan, <T> (ptr, src) -> ret);
425         };
426         _ if intrinsic.starts_with("atomic_umin"), <T> (v ptr, v src) {
427             atomic_minmax!(fx, IntCC::UnsignedLessThan, <T> (ptr, src) -> ret);
428         };
429     }
430
431     if let Some((_, dest)) = destination {
432         let ret_ebb = fx.get_ebb(dest);
433         fx.bcx.ins().jump(ret_ebb, &[]);
434     } else {
435         trap_unreachable(&mut fx.bcx);
436     }
437 }