]> git.lizzy.rs Git - rust.git/blob - src/intrinsics.rs
Improve dsl
[rust.git] / src / intrinsics.rs
1
2 use crate::prelude::*;
3
4 macro_rules! intrinsic_pat {
5     (_) => {
6         _
7     };
8     ($name:ident) => {
9         stringify!($name)
10     }
11 }
12
13 macro_rules! intrinsic_arg {
14     (c $fx:expr, $arg:ident) => {
15         $arg
16     };
17     (v $fx:expr, $arg:ident) => {
18         $arg.load_value($fx)
19     };
20 }
21
22 macro_rules! intrinsic_substs {
23     ($substs:expr, $index:expr,) => {};
24     ($substs:expr, $index:expr, $first:ident $(,$rest:ident)*) => {
25         let $first = $substs.type_at($index);
26         intrinsic_substs!($substs, $index+1, $($rest),*);
27     };
28 }
29
30 macro_rules! intrinsic_match {
31     ($fx:expr, $intrinsic:expr, $substs:expr, $args:expr, $(
32         $($name:tt)|+ $(if $cond:expr)?, $(<$($subst:ident),*>)? ($($a:ident $arg:ident),*) $content:block;
33     )*) => {
34         match $intrinsic {
35             $(
36                 $(intrinsic_pat!($name))|* $(if $cond)? => {
37                     #[allow(unused_parens, non_snake_case)]
38                     {
39                         $(
40                             intrinsic_substs!($substs, 0, $($subst),*);
41                         )?
42                         if let [$($arg),*] = *$args {
43                             let ($($arg),*) = (
44                                 $(intrinsic_arg!($a $fx, $arg)),*
45                             );
46                             #[warn(unused_parens, non_snake_case)]
47                             {
48                                 $content
49                             }
50                         } else {
51                             bug!("wrong number of args for intrinsic {:?}", $intrinsic);
52                         }
53                     }
54                 }
55             )*
56             _ => unimpl!("unsupported intrinsic {}", $intrinsic),
57         }
58     };
59 }
60
61 pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
62     fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
63     def_id: DefId,
64     substs: &'tcx Substs,
65     args: Vec<CValue<'tcx>>,
66     destination: Option<(CPlace<'tcx>, BasicBlock)>,
67 ) {
68     let intrinsic = fx.tcx.item_name(def_id).as_str();
69     let intrinsic = &intrinsic[..];
70
71     let ret = match destination {
72         Some((place, _)) => place,
73         None => {
74             // Insert non returning intrinsics here
75             match intrinsic {
76                 "abort" => {
77                     fx.bcx.ins().trap(TrapCode::User(!0 - 1));
78                 }
79                 "unreachable" => {
80                     fx.bcx.ins().trap(TrapCode::User(!0 - 1));
81                 }
82                 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
83             }
84             return;
85         }
86     };
87
88     let u64_layout = fx.layout_of(fx.tcx.types.u64);
89     let usize_layout = fx.layout_of(fx.tcx.types.usize);
90
91     intrinsic_match! {
92         fx, intrinsic, substs, args,
93
94         assume, (c _a) {};
95         arith_offset, (v base, v offset) {
96             let res = fx.bcx.ins().iadd(base, offset);
97             let res = CValue::ByVal(res, ret.layout());
98             ret.write_cvalue(fx, res);
99         };
100         likely | unlikely, (c a) {
101             ret.write_cvalue(fx, a);
102         };
103         copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
104             let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
105             let elem_size = fx
106                 .bcx
107                 .ins()
108                 .iconst(fx.module.pointer_type(), elem_size as i64);
109             assert_eq!(args.len(), 3);
110             let byte_amount = fx.bcx.ins().imul(count, elem_size);
111
112             if intrinsic.ends_with("_nonoverlapping") {
113                 fx.bcx.call_memcpy(fx.isa, dst, src, byte_amount);
114             } else {
115                 fx.bcx.call_memmove(fx.isa, dst, src, byte_amount);
116             }
117         };
118         discriminant_value, (c val) {
119             let discr = crate::base::trans_get_discriminant(fx, val, ret.layout());
120             ret.write_cvalue(fx, discr);
121         };
122         size_of, <T> () {
123             let size_of = fx.layout_of(T).size.bytes();
124             let size_of = CValue::const_val(fx, usize_layout.ty, size_of as i64);
125             ret.write_cvalue(fx, size_of);
126         };
127         size_of_val, <T> (c ptr) {
128             let layout = fx.layout_of(T);
129             let size = match &layout.ty.sty {
130                 _ if !layout.is_unsized() => fx
131                     .bcx
132                     .ins()
133                     .iconst(fx.module.pointer_type(), layout.size.bytes() as i64),
134                 ty::Slice(elem) => {
135                     let len = ptr.load_value_pair(fx).1;
136                     let elem_size = fx.layout_of(elem).size.bytes();
137                     fx.bcx.ins().imul_imm(len, elem_size as i64)
138                 }
139                 ty::Dynamic(..) => crate::vtable::size_of_obj(fx, ptr),
140                 ty => bug!("size_of_val for unknown unsized type {:?}", ty),
141             };
142             ret.write_cvalue(fx, CValue::ByVal(size, usize_layout));
143         };
144         min_align_of, <T> () {
145             let min_align = fx.layout_of(T).align.abi();
146             let min_align = CValue::const_val(fx, usize_layout.ty, min_align as i64);
147             ret.write_cvalue(fx, min_align);
148         };
149         min_align_of_val, <T> (c ptr) {
150             let layout = fx.layout_of(T);
151             let align = match &layout.ty.sty {
152                 _ if !layout.is_unsized() => fx
153                     .bcx
154                     .ins()
155                     .iconst(fx.module.pointer_type(), layout.align.abi() as i64),
156                 ty::Slice(elem) => {
157                     let align = fx.layout_of(elem).align.abi() as i64;
158                     fx.bcx.ins().iconst(fx.module.pointer_type(), align)
159                 }
160                 ty::Dynamic(..) => crate::vtable::min_align_of_obj(fx, ptr),
161                 ty => unimplemented!("min_align_of_val for {:?}", ty),
162             };
163             ret.write_cvalue(fx, CValue::ByVal(align, usize_layout));
164         };
165         type_id, <T> () {
166             let type_id = fx.tcx.type_id_hash(T);
167             let type_id = CValue::const_val(fx, u64_layout.ty, type_id as i64);
168             ret.write_cvalue(fx, type_id);
169         };
170         _ if intrinsic.starts_with("unchecked_"), (c x, c y) {
171             let bin_op = match intrinsic {
172                 "unchecked_div" => BinOp::Div,
173                 "unchecked_rem" => BinOp::Rem,
174                 "unchecked_shl" => BinOp::Shl,
175                 "unchecked_shr" => BinOp::Shr,
176                 _ => unimplemented!("intrinsic {}", intrinsic),
177             };
178             let res = match ret.layout().ty.sty {
179                 ty::Uint(_) => crate::base::trans_int_binop(
180                     fx,
181                     bin_op,
182                     x,
183                     y,
184                     ret.layout().ty,
185                     false,
186                 ),
187                 ty::Int(_) => crate::base::trans_int_binop(
188                     fx,
189                     bin_op,
190                     x,
191                     y,
192                     ret.layout().ty,
193                     true,
194                 ),
195                 _ => panic!(),
196             };
197             ret.write_cvalue(fx, res);
198         };
199         _ if intrinsic.ends_with("_with_overflow"), <T> (c x, c y) {
200             assert_eq!(x.layout().ty, y.layout().ty);
201             let bin_op = match intrinsic {
202                 "add_with_overflow" => BinOp::Add,
203                 "sub_with_overflow" => BinOp::Sub,
204                 "mul_with_overflow" => BinOp::Mul,
205                 _ => unimplemented!("intrinsic {}", intrinsic),
206             };
207             let res = match T.sty {
208                 ty::Uint(_) => crate::base::trans_checked_int_binop(
209                     fx,
210                     bin_op,
211                     x,
212                     y,
213                     ret.layout().ty,
214                     false,
215                 ),
216                 ty::Int(_) => crate::base::trans_checked_int_binop(
217                     fx,
218                     bin_op,
219                     x,
220                     y,
221                     ret.layout().ty,
222                     true,
223                 ),
224                 _ => panic!(),
225             };
226             ret.write_cvalue(fx, res);
227         };
228         _ if intrinsic.starts_with("overflowing_"), <T> (c x, c y) {
229             assert_eq!(x.layout().ty, y.layout().ty);
230             let bin_op = match intrinsic {
231                 "overflowing_add" => BinOp::Add,
232                 "overflowing_sub" => BinOp::Sub,
233                 "overflowing_mul" => BinOp::Mul,
234                 _ => unimplemented!("intrinsic {}", intrinsic),
235             };
236             let res = match T.sty {
237                 ty::Uint(_) => crate::base::trans_int_binop(
238                     fx,
239                     bin_op,
240                     x,
241                     y,
242                     ret.layout().ty,
243                     false,
244                 ),
245                 ty::Int(_) => crate::base::trans_int_binop(
246                     fx,
247                     bin_op,
248                     x,
249                     y,
250                     ret.layout().ty,
251                     true,
252                 ),
253                 _ => panic!(),
254             };
255             ret.write_cvalue(fx, res);
256         };
257         offset, (v base, v offset) {
258             let res = fx.bcx.ins().iadd(base, offset);
259             ret.write_cvalue(fx, CValue::ByVal(res, args[0].layout()));
260         };
261         transmute, <src_ty, dst_ty> (c from) {
262             assert_eq!(from.layout().ty, src_ty);
263             let addr = from.force_stack(fx);
264             let dst_layout = fx.layout_of(dst_ty);
265             ret.write_cvalue(fx, CValue::ByRef(addr, dst_layout))
266         };
267         init, <T> () {
268             let layout = fx.layout_of(T);
269             let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
270                 kind: StackSlotKind::ExplicitSlot,
271                 size: layout.size.bytes() as u32,
272                 offset: None,
273             });
274             let addr = fx.bcx.ins().stack_addr(pointer_ty(fx.tcx), stack_slot, 0);
275             let zero_val = fx.bcx.ins().iconst(types::I8, 0);
276             let len_val = fx.bcx.ins().iconst(pointer_ty(fx.tcx), layout.size.bytes() as i64);
277             fx.bcx.call_memset(fx.isa, addr, zero_val, len_val);
278
279             let uninit_place = CPlace::from_stack_slot(fx, stack_slot, T);
280             let uninit_val = uninit_place.to_cvalue(fx);
281             ret.write_cvalue(fx, uninit_val);
282         };
283         uninit, <T> () {
284             let layout = fx.layout_of(T);
285             let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
286                 kind: StackSlotKind::ExplicitSlot,
287                 size: layout.size.bytes() as u32,
288                 offset: None,
289             });
290
291             let uninit_place = CPlace::from_stack_slot(fx, stack_slot, T);
292             let uninit_val = uninit_place.to_cvalue(fx);
293             ret.write_cvalue(fx, uninit_val);
294         };
295         ctlz | ctlz_nonzero, <T> (v arg) {
296             let res = CValue::ByVal(fx.bcx.ins().clz(arg), fx.layout_of(T));
297             ret.write_cvalue(fx, res);
298         };
299         cttz | cttz_nonzero, <T> (v arg) {
300             let res = CValue::ByVal(fx.bcx.ins().clz(arg), fx.layout_of(T));
301             ret.write_cvalue(fx, res);
302         };
303         ctpop, <T> (v arg) {
304             let res = CValue::ByVal(fx.bcx.ins().popcnt(arg), fx.layout_of(T));
305             ret.write_cvalue(fx, res);
306         };
307         bitreverse, <T> (v arg) {
308             let res = CValue::ByVal(fx.bcx.ins().bitrev(arg), fx.layout_of(T));
309             ret.write_cvalue(fx, res);
310         };
311         needs_drop, <T> () {
312             let needs_drop = if T.needs_drop(fx.tcx, ParamEnv::reveal_all()) {
313                 1
314             } else {
315                 0
316             };
317             let needs_drop = CValue::const_val(fx, fx.tcx.types.bool, needs_drop);
318             ret.write_cvalue(fx, needs_drop);
319         };
320         _ if intrinsic.starts_with("atomic_fence"), () {};
321         _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
322         _ if intrinsic.starts_with("atomic_load"), (c ptr) {
323             let inner_layout =
324                 fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
325             let val = CValue::ByRef(ptr.load_value(fx), inner_layout);
326             ret.write_cvalue(fx, val);
327         };
328         _ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
329             let dest = CPlace::Addr(ptr, None, val.layout());
330             dest.write_cvalue(fx, val);
331         };
332         _ if intrinsic.starts_with("atomic_xadd"), <T> (v ptr, v amount) {
333             let clif_ty = fx.cton_type(T).unwrap();
334             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
335             let new = fx.bcx.ins().iadd(old, amount);
336             fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
337             ret.write_cvalue(fx, CValue::ByVal(old, fx.layout_of(T)));
338         };
339         _ if intrinsic.starts_with("atomic_xsub"), <T> (v ptr, v amount) {
340             let clif_ty = fx.cton_type(T).unwrap();
341             let old = fx.bcx.ins().load(clif_ty, MemFlags::new(), ptr, 0);
342             let new = fx.bcx.ins().isub(old, amount);
343             fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
344             ret.write_cvalue(fx, CValue::ByVal(old, fx.layout_of(T)));
345         };
346     }
347
348     if let Some((_, dest)) = destination {
349         let ret_ebb = fx.get_ebb(dest);
350         fx.bcx.ins().jump(ret_ebb, &[]);
351     } else {
352         fx.bcx.ins().trap(TrapCode::User(!0));
353     }
354 }