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_match {
23 ($fx:expr, $intrinsic:expr, $args:expr, $(
24 $($name:tt)|+ $(if $cond:expr)?, |$($a:ident $arg:ident),*| $content:block;
28 $(intrinsic_pat!($name))|* $(if $cond)? => {
29 if let [$($arg),*] = *$args {
30 #[allow(unused_parens)]
33 $(intrinsic_arg!($a $fx, $arg)),*
38 bug!("wrong number of args for intrinsic {:?}", $intrinsic);
42 _ => unimpl!("unsupported intrinsic {}", $intrinsic),
47 pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
48 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
51 args: Vec<CValue<'tcx>>,
52 destination: Option<(CPlace<'tcx>, BasicBlock)>,
54 let intrinsic = fx.tcx.item_name(def_id).as_str();
55 let intrinsic = &intrinsic[..];
57 let ret = match destination {
58 Some((place, _)) => place,
60 // Insert non returning intrinsics here
63 fx.bcx.ins().trap(TrapCode::User(!0 - 1));
66 fx.bcx.ins().trap(TrapCode::User(!0 - 1));
68 _ => unimplemented!("unsupported instrinsic {}", intrinsic),
74 let u64_layout = fx.layout_of(fx.tcx.types.u64);
75 let usize_layout = fx.layout_of(fx.tcx.types.usize);
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);
86 likely | unlikely, |c a| {
87 ret.write_cvalue(fx, a);
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();
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);
99 if intrinsic.ends_with("_nonoverlapping") {
100 fx.bcx.call_memcpy(fx.isa, dst, src, byte_amount);
102 fx.bcx.call_memmove(fx.isa, dst, src, byte_amount);
105 discriminant_value, |c val| {
106 let discr = crate::base::trans_get_discriminant(fx, args[0], ret.layout());
107 ret.write_cvalue(fx, discr);
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);
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
120 .iconst(fx.module.pointer_type(), layout.size.bytes() as i64),
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)
126 ty::Dynamic(..) => crate::vtable::size_of_obj(fx, ptr),
127 ty => bug!("size_of_val for unknown unsized type {:?}", ty),
129 ret.write_cvalue(fx, CValue::ByVal(size, usize_layout));
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);
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
142 .iconst(fx.module.pointer_type(), layout.align.abi() as i64),
144 let align = fx.layout_of(elem).align.abi() as i64;
145 fx.bcx.ins().iconst(fx.module.pointer_type(), align)
147 ty::Dynamic(..) => crate::vtable::min_align_of_obj(fx, ptr),
148 ty => unimplemented!("min_align_of_val for {:?}", ty),
150 ret.write_cvalue(fx, CValue::ByVal(align, usize_layout));
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);
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),
165 let res = match ret.layout().ty.sty {
166 ty::Uint(_) => crate::base::trans_int_binop(
174 ty::Int(_) => crate::base::trans_int_binop(
184 ret.write_cvalue(fx, res);
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),
194 let res = match args[0].layout().ty.sty {
195 ty::Uint(_) => crate::base::trans_checked_int_binop(
203 ty::Int(_) => crate::base::trans_checked_int_binop(
213 ret.write_cvalue(fx, res);
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),
223 let res = match x.layout().ty.sty {
224 ty::Uint(_) => crate::base::trans_int_binop(
232 ty::Int(_) => crate::base::trans_int_binop(
242 ret.write_cvalue(fx, res);
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()));
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))
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,
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);
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);
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,
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);
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);
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);
295 let res = CValue::ByVal(fx.bcx.ins().popcnt(arg), args[0].layout());
296 ret.write_cvalue(fx, res);
298 bitreverse, |v arg| {
299 let res = CValue::ByVal(fx.bcx.ins().bitrev(arg), args[0].layout());
300 ret.write_cvalue(fx, res);
303 let ty = substs.type_at(0);
304 let needs_drop = if ty.needs_drop(fx.tcx, ParamEnv::reveal_all()) {
309 let needs_drop = CValue::const_val(fx, fx.tcx.types.bool, needs_drop);
310 ret.write_cvalue(fx, needs_drop);
312 _ if intrinsic.starts_with("atomic_fence"), | | {};
313 _ if intrinsic.starts_with("atomic_singlethreadfence"), | | {};
314 _ if intrinsic.starts_with("atomic_load"), |c ptr| {
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);
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);
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))));
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))));
340 if let Some((_, dest)) = destination {
341 let ret_ebb = fx.get_ebb(dest);
342 fx.bcx.ins().jump(ret_ebb, &[]);
344 fx.bcx.ins().trap(TrapCode::User(!0));