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