1 use rustc::ty::layout::{Integer, Primitive};
2 use rustc_target::spec::{HasTargetSpec, Target};
3 use rustc_index::vec::IndexVec;
5 use cranelift_codegen::ir::{InstructionData, Opcode, ValueDef};
9 pub fn mir_var(loc: Local) -> Variable {
10 Variable::with_u32(loc.index() as u32)
13 pub fn pointer_ty(tcx: TyCtxt) -> types::Type {
14 match tcx.data_layout.pointer_size.bits() {
18 bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
22 pub fn scalar_to_clif_type(tcx: TyCtxt, scalar: Scalar) -> Type {
24 Primitive::Int(int, _sign) => match int {
25 Integer::I8 => types::I8,
26 Integer::I16 => types::I16,
27 Integer::I32 => types::I32,
28 Integer::I64 => types::I64,
29 Integer::I128 => types::I128,
31 Primitive::F32 => types::F32,
32 Primitive::F64 => types::F64,
33 Primitive::Pointer => pointer_ty(tcx),
37 pub fn clif_type_from_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<types::Type> {
39 ty::Bool => types::I8,
40 ty::Uint(size) => match size {
41 UintTy::U8 => types::I8,
42 UintTy::U16 => types::I16,
43 UintTy::U32 => types::I32,
44 UintTy::U64 => types::I64,
45 UintTy::U128 => types::I128,
46 UintTy::Usize => pointer_ty(tcx),
48 ty::Int(size) => match size {
49 IntTy::I8 => types::I8,
50 IntTy::I16 => types::I16,
51 IntTy::I32 => types::I32,
52 IntTy::I64 => types::I64,
53 IntTy::I128 => types::I128,
54 IntTy::Isize => pointer_ty(tcx),
56 ty::Char => types::I32,
57 ty::Float(size) => match size {
58 FloatTy::F32 => types::F32,
59 FloatTy::F64 => types::F64,
61 ty::FnPtr(_) => pointer_ty(tcx),
62 ty::RawPtr(TypeAndMut { ty: pointee_ty, mutbl: _ }) | ty::Ref(_, pointee_ty, _) => {
63 if has_ptr_meta(tcx, pointee_ty) {
69 ty::Param(_) => bug!("ty param {:?}", ty),
74 /// Is a pointer to this type a fat ptr?
75 pub fn has_ptr_meta<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> bool {
76 let ptr_ty = tcx.mk_ptr(TypeAndMut { ty, mutbl: rustc::hir::Mutability::Not });
77 match &tcx.layout_of(ParamEnv::reveal_all().and(ptr_ty)).unwrap().abi {
78 Abi::Scalar(_) => false,
79 Abi::ScalarPair(_, _) => true,
80 abi => unreachable!("Abi of ptr to {:?} is {:?}???", ty, abi),
85 fx: &mut FunctionCx<'_, '_, impl Backend>,
90 let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
91 let rhs_ty = fx.bcx.func.dfg.value_type(rhs);
92 assert_eq!(lhs_ty, rhs_ty);
93 if lhs_ty == types::I128 {
94 // FIXME legalize `icmp.i128` in Cranelift
96 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
97 let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs);
101 let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
102 let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
103 fx.bcx.ins().band(lsb_eq, msb_eq)
106 let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
107 let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
108 fx.bcx.ins().bor(lsb_ne, msb_ne)
117 let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
118 let lsb_cc = fx.bcx.ins().icmp(intcc, lhs_lsb, rhs_lsb);
119 let msb_cc = fx.bcx.ins().icmp(intcc, lhs_msb, rhs_msb);
121 fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
125 fx.bcx.ins().icmp(intcc, lhs, rhs)
129 pub fn codegen_icmp_imm(
130 fx: &mut FunctionCx<'_, '_, impl Backend>,
135 let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
136 if lhs_ty == types::I128 {
137 // FIXME legalize `icmp_imm.i128` in Cranelift
139 let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
140 let (rhs_lsb, rhs_msb) = (rhs as u128 as u64 as i64, (rhs as u128 >> 64) as u64 as i64);
144 let lsb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_lsb, rhs_lsb);
145 let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb);
146 fx.bcx.ins().band(lsb_eq, msb_eq)
149 let lsb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_lsb, rhs_lsb);
150 let msb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_msb, rhs_msb);
151 fx.bcx.ins().bor(lsb_ne, msb_ne)
160 let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb);
161 let lsb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_lsb, rhs_lsb);
162 let msb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_msb, rhs_msb);
164 fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
168 let rhs = i64::try_from(rhs).expect("codegen_icmp_imm rhs out of range for <128bit int");
169 fx.bcx.ins().icmp_imm(intcc, lhs, rhs)
173 fn resolve_normal_value_imm(func: &Function, val: Value) -> Option<i64> {
174 if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
175 if let InstructionData::UnaryImm {
176 opcode: Opcode::Iconst,
189 fn resolve_128bit_value_imm(func: &Function, val: Value) -> Option<u128> {
190 let (lsb, msb) = if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
191 if let InstructionData::Binary {
192 opcode: Opcode::Iconcat,
204 let lsb = resolve_normal_value_imm(func, lsb)? as u64 as u128;
205 let msb = resolve_normal_value_imm(func, msb)? as u64 as u128;
207 Some(msb << 64 | lsb)
210 pub fn resolve_value_imm(func: &Function, val: Value) -> Option<u128> {
211 if func.dfg.value_type(val) == types::I128 {
212 resolve_128bit_value_imm(func, val)
214 resolve_normal_value_imm(func, val).map(|imm| imm as u64 as u128)
218 pub fn type_min_max_value(ty: Type, signed: bool) -> (i64, i64) {
219 assert!(ty.is_int());
220 let min = match (ty, signed) {
221 (types::I8, false) | (types::I16, false) | (types::I32, false) | (types::I64, false) => {
224 (types::I8, true) => i8::min_value() as i64,
225 (types::I16, true) => i16::min_value() as i64,
226 (types::I32, true) => i32::min_value() as i64,
227 (types::I64, true) => i64::min_value(),
228 (types::I128, _) => unimplemented!(),
232 let max = match (ty, signed) {
233 (types::I8, false) => u8::max_value() as i64,
234 (types::I16, false) => u16::max_value() as i64,
235 (types::I32, false) => u32::max_value() as i64,
236 (types::I64, false) => u64::max_value() as i64,
237 (types::I8, true) => i8::max_value() as i64,
238 (types::I16, true) => i16::max_value() as i64,
239 (types::I32, true) => i32::max_value() as i64,
240 (types::I64, true) => i64::max_value(),
241 (types::I128, _) => unimplemented!(),
248 pub fn type_sign(ty: Ty<'_>) -> bool {
250 ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Char | ty::Uint(..) | ty::Bool => false,
252 ty::Float(..) => false, // `signed` is unused for floats
253 _ => panic!("{}", ty),
257 pub struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
258 // FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
259 pub tcx: TyCtxt<'tcx>,
260 pub module: &'clif mut Module<B>,
261 pub pointer_type: Type, // Cached from module
263 pub instance: Instance<'tcx>,
264 pub mir: &'tcx Body<'tcx>,
266 pub bcx: FunctionBuilder<'clif>,
267 pub ebb_map: IndexVec<BasicBlock, Ebb>,
268 pub local_map: HashMap<Local, CPlace<'tcx>>,
270 pub clif_comments: crate::pretty_clif::CommentWriter,
271 pub constants_cx: &'clif mut crate::constant::ConstantCx,
272 pub vtables: &'clif mut HashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), DataId>,
274 pub source_info_set: indexmap::IndexSet<SourceInfo>,
277 impl<'tcx, B: Backend> LayoutOf for FunctionCx<'_, 'tcx, B> {
279 type TyLayout = TyLayout<'tcx>;
281 fn layout_of(&self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
282 assert!(!ty.needs_subst());
284 .layout_of(ParamEnv::reveal_all().and(&ty))
285 .unwrap_or_else(|e| {
286 if let layout::LayoutError::SizeOverflow(_) = e {
287 self.tcx.sess.fatal(&e.to_string())
289 bug!("failed to get layout for `{}`: {}", ty, e)
295 impl<'tcx, B: Backend + 'static> layout::HasTyCtxt<'tcx> for FunctionCx<'_, 'tcx, B> {
296 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
301 impl<'tcx, B: Backend + 'static> layout::HasDataLayout for FunctionCx<'_, 'tcx, B> {
302 fn data_layout(&self) -> &layout::TargetDataLayout {
303 &self.tcx.data_layout
307 impl<'tcx, B: Backend + 'static> layout::HasParamEnv<'tcx> for FunctionCx<'_, 'tcx, B> {
308 fn param_env(&self) -> ParamEnv<'tcx> {
309 ParamEnv::reveal_all()
313 impl<'tcx, B: Backend + 'static> HasTargetSpec for FunctionCx<'_, 'tcx, B> {
314 fn target_spec(&self) -> &Target {
315 &self.tcx.sess.target.target
319 impl<'tcx, B: Backend> BackendTypes for FunctionCx<'_, 'tcx, B> {
321 type Function = Value;
322 type BasicBlock = Ebb;
328 impl<'tcx, B: Backend + 'static> FunctionCx<'_, 'tcx, B> {
329 pub fn monomorphize<T>(&self, value: &T) -> T
331 T: TypeFoldable<'tcx>,
333 self.tcx.subst_and_normalize_erasing_regions(
334 self.instance.substs,
335 ty::ParamEnv::reveal_all(),
340 pub fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
341 clif_type_from_ty(self.tcx, self.monomorphize(&ty))
344 pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
345 *self.ebb_map.get(bb).unwrap()
348 pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
349 *self.local_map.get(&local).unwrap()
352 pub fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
353 let (index, _) = self.source_info_set.insert_full(source_info);
354 self.bcx.set_srcloc(SourceLoc::new(index as u32));
357 pub fn get_caller_location(&mut self, span: Span) -> CValue<'tcx> {
358 let topmost = span.ctxt().outer_expn().expansion_cause().unwrap_or(span);
359 let caller = self.tcx.sess.source_map().lookup_char_pos(topmost.lo());
360 let const_loc = self.tcx.const_caller_location((
361 syntax::symbol::Symbol::intern(&caller.file.name.to_string()),
363 caller.col_display as u32 + 1,
365 crate::constant::trans_const_value(self, const_loc)
368 pub fn triple(&self) -> &target_lexicon::Triple {
369 self.module.isa().triple()