1 use rustc_target::spec::{HasTargetSpec, Target};
3 use cranelift::codegen::ir::{Opcode, InstructionData, ValueDef};
4 use cranelift_module::Module;
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 clif_type_from_ty<'tcx>(
24 ) -> Option<types::Type> {
26 ty::Bool => types::I8,
27 ty::Uint(size) => match size {
28 UintTy::U8 => types::I8,
29 UintTy::U16 => types::I16,
30 UintTy::U32 => types::I32,
31 UintTy::U64 => types::I64,
32 UintTy::U128 => types::I128,
33 UintTy::Usize => pointer_ty(tcx),
35 ty::Int(size) => match size {
36 IntTy::I8 => types::I8,
37 IntTy::I16 => types::I16,
38 IntTy::I32 => types::I32,
39 IntTy::I64 => types::I64,
40 IntTy::I128 => types::I128,
41 IntTy::Isize => pointer_ty(tcx),
43 ty::Char => types::I32,
44 ty::Float(size) => match size {
45 FloatTy::F32 => types::F32,
46 FloatTy::F64 => types::F64,
48 ty::FnPtr(_) => pointer_ty(tcx),
49 ty::RawPtr(TypeAndMut { ty, mutbl: _ }) | ty::Ref(_, ty, _) => {
50 if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
56 ty::Param(_) => bug!("ty param {:?}", ty),
61 pub fn codegen_select(bcx: &mut FunctionBuilder, cond: Value, lhs: Value, rhs: Value) -> Value {
62 let lhs_ty = bcx.func.dfg.value_type(lhs);
63 let rhs_ty = bcx.func.dfg.value_type(rhs);
64 assert_eq!(lhs_ty, rhs_ty);
65 if lhs_ty == types::I8 || lhs_ty == types::I16 {
66 // FIXME workaround for missing encoding for select.i8
67 let lhs = bcx.ins().uextend(types::I32, lhs);
68 let rhs = bcx.ins().uextend(types::I32, rhs);
69 let res = bcx.ins().select(cond, lhs, rhs);
70 bcx.ins().ireduce(lhs_ty, res)
72 bcx.ins().select(cond, lhs, rhs)
76 fn resolve_normal_value_imm(func: &Function, val: Value) -> Option<i64> {
77 if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
78 if let InstructionData::UnaryImm {
79 opcode: Opcode::Iconst,
91 fn resolve_128bit_value_imm(func: &Function, val: Value) -> Option<u128> {
92 let (lsb, msb) = if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
93 if let InstructionData::Binary {
94 opcode: Opcode::Iconcat,
105 let lsb = resolve_normal_value_imm(func, lsb)? as u64 as u128;
106 let msb = resolve_normal_value_imm(func, msb)? as u64 as u128;
108 Some(msb << 64 | lsb)
111 pub fn resolve_value_imm(func: &Function, val: Value) -> Option<u128> {
112 if func.dfg.value_type(val) == types::I128 {
113 resolve_128bit_value_imm(func, val)
115 resolve_normal_value_imm(func, val).map(|imm| imm as u64 as u128)
119 pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
120 // FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
121 pub tcx: TyCtxt<'tcx>,
122 pub module: &'a mut Module<B>,
123 pub pointer_type: Type, // Cached from module
125 pub instance: Instance<'tcx>,
126 pub mir: &'tcx Body<'tcx>,
128 pub bcx: FunctionBuilder<'a>,
129 pub ebb_map: HashMap<BasicBlock, Ebb>,
130 pub local_map: HashMap<Local, CPlace<'tcx>>,
132 pub clif_comments: crate::pretty_clif::CommentWriter,
133 pub constants: &'a mut crate::constant::ConstantCx,
134 pub caches: &'a mut Caches<'tcx>,
135 pub source_info_set: indexmap::IndexSet<SourceInfo>,
138 impl<'a, 'tcx: 'a, B: Backend> LayoutOf for FunctionCx<'a, 'tcx, B> {
140 type TyLayout = TyLayout<'tcx>;
142 fn layout_of(&self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
143 let ty = self.monomorphize(&ty);
144 self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
148 impl<'a, 'tcx, B: Backend + 'a> layout::HasTyCtxt<'tcx> for FunctionCx<'a, 'tcx, B> {
149 fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
154 impl<'a, 'tcx, B: Backend + 'a> layout::HasDataLayout for FunctionCx<'a, 'tcx, B> {
155 fn data_layout(&self) -> &layout::TargetDataLayout {
156 &self.tcx.data_layout
160 impl<'a, 'tcx, B: Backend + 'a> layout::HasParamEnv<'tcx> for FunctionCx<'a, 'tcx, B> {
161 fn param_env(&self) -> ParamEnv<'tcx> {
162 ParamEnv::reveal_all()
166 impl<'a, 'tcx, B: Backend + 'a> HasTargetSpec for FunctionCx<'a, 'tcx, B> {
167 fn target_spec(&self) -> &Target {
168 &self.tcx.sess.target.target
172 impl<'a, 'tcx, B: Backend> BackendTypes for FunctionCx<'a, 'tcx, B> {
174 type BasicBlock = Ebb;
180 impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
181 pub fn monomorphize<T>(&self, value: &T) -> T
183 T: TypeFoldable<'tcx>,
185 self.tcx.subst_and_normalize_erasing_regions(
186 self.instance.substs,
187 ty::ParamEnv::reveal_all(),
192 pub fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
193 clif_type_from_ty(self.tcx, self.monomorphize(&ty))
196 pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
197 *self.ebb_map.get(&bb).unwrap()
200 pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
201 *self.local_map.get(&local).unwrap()
204 pub fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
205 let (index, _) = self.source_info_set.insert_full(source_info);
206 self.bcx.set_srcloc(SourceLoc::new(index as u32));