1 use rustc_target::spec::{HasTargetSpec, Target};
3 use cranelift_module::Module;
7 pub fn mir_var(loc: Local) -> Variable {
8 Variable::with_u32(loc.index() as u32)
11 pub fn pointer_ty(tcx: TyCtxt) -> types::Type {
12 match tcx.data_layout.pointer_size.bits() {
16 bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
20 pub fn clif_type_from_ty<'a, 'tcx: 'a>(
21 tcx: TyCtxt<'a, 'tcx, 'tcx>,
23 ) -> Option<types::Type> {
25 ty::Bool => types::I8,
26 ty::Uint(size) => match size {
27 UintTy::U8 => types::I8,
28 UintTy::U16 => types::I16,
29 UintTy::U32 => types::I32,
30 UintTy::U64 => types::I64,
31 UintTy::U128 => unimpl!("u128"),
32 UintTy::Usize => pointer_ty(tcx),
34 ty::Int(size) => match size {
35 IntTy::I8 => types::I8,
36 IntTy::I16 => types::I16,
37 IntTy::I32 => types::I32,
38 IntTy::I64 => types::I64,
39 IntTy::I128 => unimpl!("i128"),
40 IntTy::Isize => pointer_ty(tcx),
42 ty::Char => types::I32,
43 ty::Float(size) => match size {
44 FloatTy::F32 => types::F32,
45 FloatTy::F64 => types::F64,
47 ty::FnPtr(_) => pointer_ty(tcx),
48 ty::RawPtr(TypeAndMut { ty, mutbl: _ }) | ty::Ref(_, ty, _) => {
49 if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
55 ty::Param(_) => bug!("ty param {:?}", ty),
60 pub fn codegen_select(bcx: &mut FunctionBuilder, cond: Value, lhs: Value, rhs: Value) -> Value {
61 let lhs_ty = bcx.func.dfg.value_type(lhs);
62 let rhs_ty = bcx.func.dfg.value_type(rhs);
63 assert_eq!(lhs_ty, rhs_ty);
64 if lhs_ty == types::I8 || lhs_ty == types::I16 {
65 // FIXME workaround for missing enocding for select.i8
66 let lhs = bcx.ins().uextend(types::I32, lhs);
67 let rhs = bcx.ins().uextend(types::I32, rhs);
68 let res = bcx.ins().select(cond, lhs, rhs);
69 bcx.ins().ireduce(lhs_ty, res)
71 bcx.ins().select(cond, lhs, rhs)
75 pub fn clif_intcast<'a, 'tcx: 'a>(
76 fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
81 let from = fx.bcx.func.dfg.value_type(val);
85 if to.wider_or_equal(from) {
87 fx.bcx.ins().sextend(to, val)
89 fx.bcx.ins().uextend(to, val)
92 fx.bcx.ins().ireduce(to, val)
96 pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
97 // FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
98 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
99 pub module: &'a mut Module<B>,
100 pub pointer_type: Type, // Cached from module
102 pub instance: Instance<'tcx>,
103 pub mir: &'tcx Body<'tcx>,
105 pub bcx: FunctionBuilder<'a>,
106 pub ebb_map: HashMap<BasicBlock, Ebb>,
107 pub local_map: HashMap<Local, CPlace<'tcx>>,
109 pub clif_comments: crate::pretty_clif::CommentWriter,
110 pub constants: &'a mut crate::constant::ConstantCx,
111 pub caches: &'a mut Caches<'tcx>,
112 pub source_info_set: indexmap::IndexSet<SourceInfo>,
115 impl<'a, 'tcx: 'a, B: Backend> LayoutOf for FunctionCx<'a, 'tcx, B> {
117 type TyLayout = TyLayout<'tcx>;
119 fn layout_of(&self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
120 let ty = self.monomorphize(&ty);
121 self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
125 impl<'a, 'tcx, B: Backend + 'a> layout::HasTyCtxt<'tcx> for FunctionCx<'a, 'tcx, B> {
126 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
131 impl<'a, 'tcx, B: Backend + 'a> layout::HasDataLayout for FunctionCx<'a, 'tcx, B> {
132 fn data_layout(&self) -> &layout::TargetDataLayout {
133 &self.tcx.data_layout
137 impl<'a, 'tcx, B: Backend + 'a> layout::HasParamEnv<'tcx> for FunctionCx<'a, 'tcx, B> {
138 fn param_env(&self) -> ParamEnv<'tcx> {
139 ParamEnv::reveal_all()
143 impl<'a, 'tcx, B: Backend + 'a> HasTargetSpec for FunctionCx<'a, 'tcx, B> {
144 fn target_spec(&self) -> &Target {
145 &self.tcx.sess.target.target
149 impl<'a, 'tcx, B: Backend> BackendTypes for FunctionCx<'a, 'tcx, B> {
151 type BasicBlock = Ebb;
157 impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
158 pub fn monomorphize<T>(&self, value: &T) -> T
160 T: TypeFoldable<'tcx>,
162 self.tcx.subst_and_normalize_erasing_regions(
163 self.instance.substs,
164 ty::ParamEnv::reveal_all(),
169 pub fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
170 clif_type_from_ty(self.tcx, self.monomorphize(&ty))
173 pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
174 *self.ebb_map.get(&bb).unwrap()
177 pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
178 *self.local_map.get(&local).unwrap()
181 pub fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
182 let (index, _) = self.source_info_set.insert_full(source_info);
183 self.bcx.set_srcloc(SourceLoc::new(index as u32));