1 use syntax::ast::{IntTy, UintTy};
3 use cretonne_module::{Module, Linkage, FuncId};
7 pub type CurrentBackend = ::cretonne_simplejit::SimpleJITBackend;
9 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
10 pub struct Variable(Local);
12 impl EntityRef for Variable {
13 fn new(u: usize) -> Self {
14 Variable(Local::new(u))
17 fn index(self) -> usize {
22 fn cton_type_from_ty(ty: Ty) -> Option<types::Type> {
24 TypeVariants::TyBool => types::I8,
25 TypeVariants::TyUint(size) => {
27 UintTy::U8 => types::I8,
28 UintTy::U16 => types::I16,
29 UintTy::U32 => types::I32,
30 UintTy::U64 => types::I64,
31 UintTy::U128 => types::I64X2,
32 UintTy::Usize => types::I64,
35 TypeVariants::TyInt(size) => {
37 IntTy::I8 => types::I8,
38 IntTy::I16 => types::I16,
39 IntTy::I32 => types::I32,
40 IntTy::I64 => types::I64,
41 IntTy::I128 => types::I64X2,
42 IntTy::Isize => types::I64,
45 TypeVariants::TyFnPtr(_) => types::I64,
46 TypeVariants::TyRef(..) | TypeVariants::TyRawPtr(..) => types::I64,
51 // FIXME(cretonne) fix types smaller than I32
52 pub fn fixup_cton_ty(ty: Type) -> Type {
54 types::I64X2 | types::I64 | types::I32 => ty,
59 // FIXME(cretonne) fix load.i8
60 fn load_workaround(fx: &mut FunctionCx, ty: Type, addr: Value, offset: i32) -> Value {
61 use cretonne::codegen::ir::types::*;
63 I8 => fx.bcx.ins().uload8(I32, MemFlags::new(), addr, offset),
64 I16 => fx.bcx.ins().uload16(I32, MemFlags::new(), addr, offset),
66 _ => fx.bcx.ins().load(ty, MemFlags::new(), addr, offset),
70 // FIXME(cretonne) fix store.i8
71 fn store_workaround(fx: &mut FunctionCx, ty: Type, addr: Value, val: Value, offset: i32) {
72 use cretonne::codegen::ir::types::*;
74 I8 => fx.bcx.ins().istore8(MemFlags::new(), val, addr, offset),
75 I16 => fx.bcx.ins().istore16(MemFlags::new(), val, addr, offset),
77 _ => fx.bcx.ins().store(MemFlags::new(), val, addr, offset),
81 #[derive(Debug, Copy, Clone)]
89 pub fn force_stack<'a, 'tcx: 'a>(self, fx: &mut FunctionCx<'a, 'tcx>, ty: Ty<'tcx>) -> Value {
91 CValue::ByRef(value) => value,
92 CValue::ByVal(value) => {
93 let layout = fx.layout_of(ty);
94 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
95 kind: StackSlotKind::ExplicitSlot,
96 size: layout.size.bytes() as u32,
99 fx.bcx.ins().stack_store(value, stack_slot, 0);
100 fx.bcx.ins().stack_addr(types::I64, stack_slot, 0)
102 CValue::Func(func) => {
103 let func = fx.bcx.ins().func_addr(types::I64, func);
104 CValue::ByVal(func).force_stack(fx, ty)
109 pub fn load_value<'a, 'tcx: 'a>(self, fx: &mut FunctionCx<'a, 'tcx>, ty: Ty<'tcx>) -> Value {
111 CValue::ByRef(value) => {
112 let cton_ty = fx.cton_type(ty).expect(&format!("{:?}", ty));
113 load_workaround(fx, cton_ty, value, 0)
115 CValue::ByVal(value) => value,
116 CValue::Func(func) => {
117 fx.bcx.ins().func_addr(types::I64, func)
122 pub fn expect_byref(self) -> Value {
124 CValue::ByRef(value) => value,
125 CValue::ByVal(_) => bug!("Expected CValue::ByRef, found CValue::ByVal"),
126 CValue::Func(_) => bug!("Expected CValue::ByRef, found CValue::Func"),
130 pub fn value_field<'a, 'tcx: 'a>(self, fx: &mut FunctionCx<'a, 'tcx>, field: mir::Field, ty: Ty<'tcx>) -> CValue {
131 let base = match self {
132 CValue::ByRef(addr) => addr,
133 _ => bug!("place_field for {:?}", self),
135 let layout = fx.layout_of(ty);
136 let field_offset = layout.fields.offset(field.index());
137 if field_offset.bytes() > 0 {
138 let field_offset = fx.bcx.ins().iconst(types::I64, field_offset.bytes() as i64);
139 CValue::ByRef(fx.bcx.ins().iadd(base, field_offset))
145 pub fn const_val<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, ty: Ty<'tcx>, const_val: i64) -> CValue {
146 let ty = fx.cton_type(ty).unwrap();
147 CValue::ByVal(fx.bcx.ins().iconst(ty, const_val))
151 #[derive(Debug, Copy, Clone)]
157 impl<'a, 'tcx: 'a> CPlace {
158 pub fn from_stack_slot(fx: &mut FunctionCx<'a, 'tcx>, stack_slot: StackSlot) -> CPlace {
159 CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0))
162 pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx>) -> CValue {
164 CPlace::Var(var) => CValue::ByVal(fx.bcx.use_var(var)),
165 CPlace::Addr(addr) => CValue::ByRef(addr),
169 pub fn expect_addr(self) -> Value {
171 CPlace::Addr(addr) => addr,
172 CPlace::Var(_) => bug!("Expected CPlace::Addr, found CPlace::Var"),
176 pub fn write_cvalue(self, fx: &mut FunctionCx<'a, 'tcx>, from: CValue, ty: Ty<'tcx>) {
177 let layout = fx.layout_of(ty);
178 let size = layout.size.bytes() as i32;
180 CPlace::Var(var) => {
181 let data = from.load_value(fx, ty);
182 fx.bcx.def_var(var, data)
184 CPlace::Addr(addr) => {
185 if let Some(cton_ty) = fx.cton_type(ty) {
186 let data = from.load_value(fx, ty);
187 store_workaround(fx, cton_ty, addr, data, 0);
190 let from = from.expect_byref();
191 let byte = load_workaround(fx, types::I8, from, i);
192 store_workaround(fx, types::I8, addr, byte, i);
199 pub fn place_field(self, fx: &mut FunctionCx<'a, 'tcx>, field: mir::Field, ty: Ty<'tcx>) -> (CPlace, layout::TyLayout<'tcx>) {
200 let base = self.expect_addr();
201 let layout = fx.layout_of(ty);
202 let field_offset = layout.fields.offset(field.index());
203 if field_offset.bytes() > 0 {
204 let field_offset = fx.bcx.ins().iconst(types::I64, field_offset.bytes() as i64);
205 (CPlace::Addr(fx.bcx.ins().iadd(base, field_offset)), layout)
207 (CPlace::Addr(base), layout)
212 pub fn cton_sig_from_fn_sig<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sig: PolyFnSig<'tcx>, substs: &Substs<'tcx>) -> Signature {
213 let sig = tcx.subst_and_normalize_erasing_regions(substs, ParamEnv::reveal_all(), &sig);
214 cton_sig_from_mono_fn_sig(sig)
217 pub fn cton_sig_from_instance<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, inst: Instance<'tcx>) -> Signature {
218 let fn_ty = inst.ty(tcx);
219 let sig = fn_ty.fn_sig(tcx);
220 cton_sig_from_mono_fn_sig(sig)
223 pub fn cton_sig_from_mono_fn_sig<'a ,'tcx: 'a>(sig: PolyFnSig<'tcx>) -> Signature {
224 let sig = sig.skip_binder();
225 let inputs = sig.inputs();
226 let _output = sig.output();
227 assert!(!sig.variadic, "Variadic function are not yet supported");
228 let call_conv = match sig.abi {
229 _ => CallConv::SystemV,
232 params: Some(types::I64).into_iter() // First param is place to put return val
233 .chain(inputs.into_iter().map(|ty| fixup_cton_ty(cton_type_from_ty(ty).unwrap_or(types::I64))))
234 .map(AbiParam::new).collect(),
237 argument_bytes: None,
241 pub fn cton_intcast<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, val: Value, from: Ty<'tcx>, to: Ty<'tcx>, signed: bool) -> Value {
242 let from = fx.cton_type(from).unwrap();
243 let to = fx.cton_type(to).unwrap();
247 if from.wider_or_equal(to) {
249 fx.bcx.ins().sextend(to, val)
251 fx.bcx.ins().uextend(to, val)
254 fx.bcx.ins().ireduce(to, val)
258 pub struct FunctionCx<'a, 'tcx: 'a> {
259 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
260 pub module: &'a mut Module<CurrentBackend>,
261 pub def_id_fn_id_map: &'a mut HashMap<Instance<'tcx>, FuncId>,
262 pub instance: Instance<'tcx>,
263 pub mir: &'tcx Mir<'tcx>,
264 pub param_substs: &'tcx Substs<'tcx>,
265 pub bcx: FunctionBuilder<'a, Variable>,
266 pub ebb_map: HashMap<BasicBlock, Ebb>,
267 pub local_map: HashMap<Local, CPlace>,
270 impl<'a, 'tcx: 'a> LayoutOf for &'a FunctionCx<'a, 'tcx> {
272 type TyLayout = TyLayout<'tcx>;
274 fn layout_of(self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
275 let ty = self.monomorphize(&ty);
276 self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
280 impl<'f, 'tcx> FunctionCx<'f, 'tcx> {
281 pub fn monomorphize<T>(&self, value: &T) -> T
282 where T: TypeFoldable<'tcx>
284 self.tcx.subst_and_normalize_erasing_regions(
286 ty::ParamEnv::reveal_all(),
291 pub fn cton_type(&self, ty: Ty<'tcx>) -> Option<Type> {
292 cton_type_from_ty(self.monomorphize(&ty))
295 pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
296 *self.ebb_map.get(&bb).unwrap()
299 pub fn get_local_place(&mut self, local: Local) -> CPlace {
300 *self.local_map.get(&local).unwrap()
303 pub fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
305 let module = &mut self.module;
306 let func_id = *self.def_id_fn_id_map.entry(inst).or_insert_with(|| {
307 let sig = cton_sig_from_instance(tcx, inst);
308 module.declare_function(&tcx.absolute_item_path_str(inst.def_id()), Linkage::Local, &sig).unwrap()
310 module.declare_func_in_func(func_id, &mut self.bcx.func)