4 macro_rules! intrinsic_pat {
13 macro_rules! intrinsic_arg {
14 (c $fx:expr, $arg:ident) => {
17 (v $fx:expr, $arg:ident) => {
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),*);
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;
36 $(intrinsic_pat!($name))|* $(if $cond)? => {
37 #[allow(unused_parens, non_snake_case)]
40 intrinsic_substs!($substs, 0, $($subst),*);
42 if let [$($arg),*] = *$args {
44 $(intrinsic_arg!($a $fx, $arg)),*
46 #[warn(unused_parens, non_snake_case)]
51 bug!("wrong number of args for intrinsic {:?}", $intrinsic);
56 _ => unimpl!("unsupported intrinsic {}", $intrinsic),
61 pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
62 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
65 args: Vec<CValue<'tcx>>,
66 destination: Option<(CPlace<'tcx>, BasicBlock)>,
68 let intrinsic = fx.tcx.item_name(def_id).as_str();
69 let intrinsic = &intrinsic[..];
71 let ret = match destination {
72 Some((place, _)) => place,
74 // Insert non returning intrinsics here
77 fx.bcx.ins().trap(TrapCode::User(!0 - 1));
80 fx.bcx.ins().trap(TrapCode::User(!0 - 1));
82 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
88 let u64_layout = fx.layout_of(fx.tcx.types.u64);
89 let usize_layout = fx.layout_of(fx.tcx.types.usize);
92 fx, intrinsic, substs, args,
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);
100 likely | unlikely, (c a) {
101 ret.write_cvalue(fx, a);
103 copy | copy_nonoverlapping, <elem_ty> (v src, v dst, v count) {
104 let elem_size: u64 = fx.layout_of(elem_ty).size.bytes();
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);
112 if intrinsic.ends_with("_nonoverlapping") {
113 fx.bcx.call_memcpy(fx.isa, dst, src, byte_amount);
115 fx.bcx.call_memmove(fx.isa, dst, src, byte_amount);
118 discriminant_value, (c val) {
119 let discr = crate::base::trans_get_discriminant(fx, val, ret.layout());
120 ret.write_cvalue(fx, discr);
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);
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
133 .iconst(fx.module.pointer_type(), layout.size.bytes() as i64),
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)
139 ty::Dynamic(..) => crate::vtable::size_of_obj(fx, ptr),
140 ty => bug!("size_of_val for unknown unsized type {:?}", ty),
142 ret.write_cvalue(fx, CValue::ByVal(size, usize_layout));
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);
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
155 .iconst(fx.module.pointer_type(), layout.align.abi() as i64),
157 let align = fx.layout_of(elem).align.abi() as i64;
158 fx.bcx.ins().iconst(fx.module.pointer_type(), align)
160 ty::Dynamic(..) => crate::vtable::min_align_of_obj(fx, ptr),
161 ty => unimplemented!("min_align_of_val for {:?}", ty),
163 ret.write_cvalue(fx, CValue::ByVal(align, usize_layout));
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);
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),
178 let res = match ret.layout().ty.sty {
179 ty::Uint(_) => crate::base::trans_int_binop(
187 ty::Int(_) => crate::base::trans_int_binop(
197 ret.write_cvalue(fx, res);
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),
207 let res = match T.sty {
208 ty::Uint(_) => crate::base::trans_checked_int_binop(
216 ty::Int(_) => crate::base::trans_checked_int_binop(
226 ret.write_cvalue(fx, res);
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),
236 let res = match T.sty {
237 ty::Uint(_) => crate::base::trans_int_binop(
245 ty::Int(_) => crate::base::trans_int_binop(
255 ret.write_cvalue(fx, res);
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()));
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))
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,
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);
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);
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,
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);
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);
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);
304 let res = CValue::ByVal(fx.bcx.ins().popcnt(arg), fx.layout_of(T));
305 ret.write_cvalue(fx, res);
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);
312 let needs_drop = if T.needs_drop(fx.tcx, ParamEnv::reveal_all()) {
317 let needs_drop = CValue::const_val(fx, fx.tcx.types.bool, needs_drop);
318 ret.write_cvalue(fx, needs_drop);
320 _ if intrinsic.starts_with("atomic_fence"), () {};
321 _ if intrinsic.starts_with("atomic_singlethreadfence"), () {};
322 _ if intrinsic.starts_with("atomic_load"), (c ptr) {
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);
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);
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)));
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)));
348 if let Some((_, dest)) = destination {
349 let ret_ebb = fx.get_ebb(dest);
350 fx.bcx.ins().jump(ret_ebb, &[]);
352 fx.bcx.ins().trap(TrapCode::User(!0));