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