1 use rustc::ty::layout::{Integer, Primitive};
2 use rustc_target::spec::{HasTargetSpec, Target};
4 use cranelift_codegen::ir::{InstructionData, Opcode, ValueDef};
8 pub fn mir_var(loc: Local) -> Variable {
9 Variable::with_u32(loc.index() as u32)
12 pub fn pointer_ty(tcx: TyCtxt) -> types::Type {
13 match tcx.data_layout.pointer_size.bits() {
17 bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
21 pub fn scalar_to_clif_type(tcx: TyCtxt, scalar: Scalar) -> Type {
23 Primitive::Int(int, _sign) => match int {
24 Integer::I8 => types::I8,
25 Integer::I16 => types::I16,
26 Integer::I32 => types::I32,
27 Integer::I64 => types::I64,
28 Integer::I128 => types::I128,
30 Primitive::F32 => types::F32,
31 Primitive::F64 => types::F64,
32 Primitive::Pointer => pointer_ty(tcx),
36 pub fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Type> {
38 ty::Bool => types::I8,
39 ty::Uint(size) => match size {
40 UintTy::U8 => types::I8,
41 UintTy::U16 => types::I16,
42 UintTy::U32 => types::I32,
43 UintTy::U64 => types::I64,
44 UintTy::U128 => types::I128,
45 UintTy::Usize => pointer_ty(tcx),
47 ty::Int(size) => match size {
48 IntTy::I8 => types::I8,
49 IntTy::I16 => types::I16,
50 IntTy::I32 => types::I32,
51 IntTy::I64 => types::I64,
52 IntTy::I128 => types::I128,
53 IntTy::Isize => pointer_ty(tcx),
55 ty::Char => types::I32,
56 ty::Float(size) => match size {
57 FloatTy::F32 => types::F32,
58 FloatTy::F64 => types::F64,
60 ty::FnPtr(_) => pointer_ty(tcx),
61 ty::RawPtr(TypeAndMut { ty: pointee_ty, mutbl: _ }) | ty::Ref(_, pointee_ty, _) => {
62 if has_ptr_meta(tcx, pointee_ty) {
68 ty::Param(_) => bug!("ty param {:?}", ty),
69 _ if ty.is_simd() => {
70 let (lane_type, lane_count) = crate::intrinsics::lane_type_and_count(
72 tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap(),
74 let lane_type = clif_type_from_ty(tcx, lane_type.ty)?;
75 let simd_type = lane_type.by(u16::try_from(lane_count).unwrap());
76 if simd_type.map(|t| t.bits()) == Some(128) {
79 return None; // Not yet implemented
86 /// Is a pointer to this type a fat ptr?
87 pub fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
88 let ptr_ty = tcx.mk_ptr(TypeAndMut { ty, mutbl: rustc::hir::Mutability::Not });
89 match &tcx.layout_of(ParamEnv::reveal_all().and(ptr_ty)).unwrap().abi {
90 Abi::Scalar(_) => false,
91 Abi::ScalarPair(_, _) => true,
92 abi => unreachable!("Abi of ptr to {:?} is {:?}???", ty, abi),
97 fx: &mut FunctionCx<'_, '_, impl Backend>,
102 let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
103 let rhs_ty = fx.bcx.func.dfg.value_type(rhs);
104 assert_eq!(lhs_ty, rhs_ty);
105 if lhs_ty == types::I128 {
106 // FIXME legalize `icmp.i128` in Cranelift
108 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
109 let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs);
113 let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
114 let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
115 fx.bcx.ins().band(lsb_eq, msb_eq)
118 let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
119 let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
120 fx.bcx.ins().bor(lsb_ne, msb_ne)
129 let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
130 let lsb_cc = fx.bcx.ins().icmp(intcc, lhs_lsb, rhs_lsb);
131 let msb_cc = fx.bcx.ins().icmp(intcc, lhs_msb, rhs_msb);
133 fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
137 fx.bcx.ins().icmp(intcc, lhs, rhs)
141 pub fn codegen_icmp_imm(
142 fx: &mut FunctionCx<'_, '_, impl Backend>,
147 let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
148 if lhs_ty == types::I128 {
149 // FIXME legalize `icmp_imm.i128` in Cranelift
151 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
152 let (rhs_lsb, rhs_msb) = (rhs as u128 as u64 as i64, (rhs as u128 >> 64) as u64 as i64);
156 let lsb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_lsb, rhs_lsb);
157 let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb);
158 fx.bcx.ins().band(lsb_eq, msb_eq)
161 let lsb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_lsb, rhs_lsb);
162 let msb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_msb, rhs_msb);
163 fx.bcx.ins().bor(lsb_ne, msb_ne)
172 let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb);
173 let lsb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_lsb, rhs_lsb);
174 let msb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_msb, rhs_msb);
176 fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
180 let rhs = i64::try_from(rhs).expect("codegen_icmp_imm rhs out of range for <128bit int");
181 fx.bcx.ins().icmp_imm(intcc, lhs, rhs)
185 fn resolve_normal_value_imm(func: &Function, val: Value) -> Option<i64> {
186 if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
187 if let InstructionData::UnaryImm {
188 opcode: Opcode::Iconst,
201 fn resolve_128bit_value_imm(func: &Function, val: Value) -> Option<u128> {
202 let (lsb, msb) = if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
203 if let InstructionData::Binary {
204 opcode: Opcode::Iconcat,
216 let lsb = resolve_normal_value_imm(func, lsb)? as u64 as u128;
217 let msb = resolve_normal_value_imm(func, msb)? as u64 as u128;
219 Some(msb << 64 | lsb)
222 pub fn resolve_value_imm(func: &Function, val: Value) -> Option<u128> {
223 if func.dfg.value_type(val) == types::I128 {
224 resolve_128bit_value_imm(func, val)
226 resolve_normal_value_imm(func, val).map(|imm| imm as u64 as u128)
230 pub fn type_min_max_value(ty: Type, signed: bool) -> (i64, i64) {
231 assert!(ty.is_int());
232 let min = match (ty, signed) {
233 (types::I8, false) | (types::I16, false) | (types::I32, false) | (types::I64, false) => {
236 (types::I8, true) => i8::min_value() as i64,
237 (types::I16, true) => i16::min_value() as i64,
238 (types::I32, true) => i32::min_value() as i64,
239 (types::I64, true) => i64::min_value(),
240 (types::I128, _) => unimplemented!(),
244 let max = match (ty, signed) {
245 (types::I8, false) => u8::max_value() as i64,
246 (types::I16, false) => u16::max_value() as i64,
247 (types::I32, false) => u32::max_value() as i64,
248 (types::I64, false) => u64::max_value() as i64,
249 (types::I8, true) => i8::max_value() as i64,
250 (types::I16, true) => i16::max_value() as i64,
251 (types::I32, true) => i32::max_value() as i64,
252 (types::I64, true) => i64::max_value(),
253 (types::I128, _) => unimplemented!(),
260 pub fn type_sign(ty: Ty<'_>) -> bool {
262 ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Char | ty::Uint(..) | ty::Bool => false,
264 ty::Float(..) => false, // `signed` is unused for floats
265 _ => panic!("{}", ty),
269 pub struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
270 // FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
271 pub tcx: TyCtxt<'tcx>,
272 pub module: &'clif mut Module<B>,
273 pub pointer_type: Type, // Cached from module
275 pub instance: Instance<'tcx>,
276 pub mir: &'tcx Body<'tcx>,
278 pub bcx: FunctionBuilder<'clif>,
279 pub ebb_map: HashMap<BasicBlock, Ebb>,
280 pub local_map: HashMap<Local, CPlace<'tcx>>,
282 pub clif_comments: crate::pretty_clif::CommentWriter,
283 pub constants_cx: &'clif mut crate::constant::ConstantCx,
284 pub caches: &'clif mut Caches<'tcx>,
286 pub source_info_set: indexmap::IndexSet<SourceInfo>,
289 impl<'tcx, B: Backend> LayoutOf for FunctionCx<'_, 'tcx, B> {
291 type TyLayout = TyLayout<'tcx>;
293 fn layout_of(&self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
294 assert!(!ty.needs_subst());
296 .layout_of(ParamEnv::reveal_all().and(&ty))
297 .unwrap_or_else(|e| {
298 if let layout::LayoutError::SizeOverflow(_) = e {
299 self.tcx.sess.fatal(&e.to_string())
301 bug!("failed to get layout for `{}`: {}", ty, e)
307 impl<'tcx, B: Backend + 'static> layout::HasTyCtxt<'tcx> for FunctionCx<'_, 'tcx, B> {
308 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
313 impl<'tcx, B: Backend + 'static> layout::HasDataLayout for FunctionCx<'_, 'tcx, B> {
314 fn data_layout(&self) -> &layout::TargetDataLayout {
315 &self.tcx.data_layout
319 impl<'tcx, B: Backend + 'static> layout::HasParamEnv<'tcx> for FunctionCx<'_, 'tcx, B> {
320 fn param_env(&self) -> ParamEnv<'tcx> {
321 ParamEnv::reveal_all()
325 impl<'tcx, B: Backend + 'static> HasTargetSpec for FunctionCx<'_, 'tcx, B> {
326 fn target_spec(&self) -> &Target {
327 &self.tcx.sess.target.target
331 impl<'tcx, B: Backend> BackendTypes for FunctionCx<'_, 'tcx, B> {
333 type Function = Value;
334 type BasicBlock = Ebb;
340 impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
341 pub fn monomorphize<T>(&self, value: &T) -> T
343 T: TypeFoldable<'tcx>,
345 self.tcx.subst_and_normalize_erasing_regions(
346 self.instance.substs,
347 ty::ParamEnv::reveal_all(),
352 pub fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
353 clif_type_from_ty(self.tcx, self.monomorphize(&ty))
356 pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
357 *self.ebb_map.get(&bb).unwrap()
360 pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
361 *self.local_map.get(&local).unwrap()
364 pub fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
365 let (index, _) = self.source_info_set.insert_full(source_info);
366 self.bcx.set_srcloc(SourceLoc::new(index as u32));
369 pub fn get_caller_location(&mut self, span: Span) -> CValue<'tcx> {
370 let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
371 let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
372 let const_loc = self.tcx.const_caller_location((
373 syntax::symbol::Symbol::intern(&caller.file.name.to_string()),
375 caller.col_display as u32 + 1,
377 crate::constant::trans_const_value(self, const_loc)
380 pub fn triple(&self) -> &target_lexicon::Triple {
381 self.module.isa().triple()