1 extern crate rustc_target;
6 use syntax::ast::{IntTy, UintTy};
7 use self::rustc_target::spec::{HasTargetSpec, Target};
9 use cranelift_module::{Module, Linkage, FuncId, DataId};
13 pub type CurrentBackend = ::cranelift_simplejit::SimpleJITBackend;
15 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
16 pub struct Variable(Local);
18 impl EntityRef for Variable {
19 fn new(u: usize) -> Self {
20 Variable(Local::new(u))
23 fn index(self) -> usize {
28 fn cton_type_from_ty<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ty: Ty<'tcx>) -> Option<types::Type> {
30 TypeVariants::TyBool => types::I8,
31 TypeVariants::TyUint(size) => {
33 UintTy::U8 => types::I8,
34 UintTy::U16 => types::I16,
35 UintTy::U32 => types::I32,
36 UintTy::U64 => types::I64,
37 UintTy::U128 => unimplemented!("u128"),
38 UintTy::Usize => types::I64,
41 TypeVariants::TyInt(size) => {
43 IntTy::I8 => types::I8,
44 IntTy::I16 => types::I16,
45 IntTy::I32 => types::I32,
46 IntTy::I64 => types::I64,
47 IntTy::I128 => unimplemented!("i128"),
48 IntTy::Isize => types::I64,
51 TypeVariants::TyFnPtr(_) => types::I64,
52 TypeVariants::TyRawPtr(TypeAndMut { ty, mutbl: _ }) | TypeVariants::TyRef(_, ty, _) => {
53 if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
59 TypeVariants::TyParam(_) => bug!("{:?}: {:?}", ty, ty.sty),
65 #[derive(Debug, Copy, Clone)]
66 pub enum CValue<'tcx> {
67 ByRef(Value, TyLayout<'tcx>),
68 ByVal(Value, TyLayout<'tcx>),
69 Func(FuncRef, TyLayout<'tcx>),
72 impl<'tcx> CValue<'tcx> {
73 pub fn layout(&self) -> TyLayout<'tcx> {
75 CValue::ByRef(_, layout) |
76 CValue::ByVal(_, layout) |
77 CValue::Func(_, layout) => layout
81 pub fn force_stack<'a>(self, fx: &mut FunctionCx<'a, 'tcx>) -> Value where 'tcx: 'a {
83 CValue::ByRef(value, _layout) => value,
84 CValue::ByVal(value, layout) => {
85 let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
86 kind: StackSlotKind::ExplicitSlot,
87 size: layout.size.bytes() as u32,
90 fx.bcx.ins().stack_store(value, stack_slot, 0);
91 fx.bcx.ins().stack_addr(types::I64, stack_slot, 0)
93 CValue::Func(func, ty) => {
94 let func = fx.bcx.ins().func_addr(types::I64, func);
95 CValue::ByVal(func, ty).force_stack(fx)
100 pub fn load_value<'a>(self, fx: &mut FunctionCx<'a, 'tcx>) -> Value where 'tcx: 'a{
102 CValue::ByRef(addr, layout) => {
103 let cton_ty = fx.cton_type(layout.ty).expect(&format!("{:?}", layout.ty));
104 fx.bcx.ins().load(cton_ty, MemFlags::new(), addr, 0)
106 CValue::ByVal(value, _layout) => value,
107 CValue::Func(func, _layout) => {
108 fx.bcx.ins().func_addr(types::I64, func)
113 pub fn expect_byref(self) -> (Value, TyLayout<'tcx>) {
115 CValue::ByRef(value, layout) => (value, layout),
116 CValue::ByVal(_, _) => bug!("Expected CValue::ByRef, found CValue::ByVal"),
117 CValue::Func(_, _) => bug!("Expected CValue::ByRef, found CValue::Func"),
121 pub fn value_field<'a>(self, fx: &mut FunctionCx<'a, 'tcx>, field: mir::Field) -> CValue<'tcx> where 'tcx: 'a {
122 use rustc::ty::util::IntTypeExt;
124 let (base, layout) = match self {
125 CValue::ByRef(addr, layout) => (addr, layout),
126 _ => bug!("place_field for {:?}", self),
128 let field_offset = layout.fields.offset(field.index());
129 let field_layout = if field.index() == 0 {
130 fx.layout_of(if let ty::TyAdt(adt_def, _) = layout.ty.sty {
131 adt_def.repr.discr_type().to_ty(fx.tcx)
133 // This can only be `0`, for now, so `u8` will suffice.
137 layout.field(&*fx, field.index())
139 if field_offset.bytes() > 0 {
140 let field_offset = fx.bcx.ins().iconst(types::I64, field_offset.bytes() as i64);
141 CValue::ByRef(fx.bcx.ins().iadd(base, field_offset), field_layout)
143 CValue::ByRef(base, field_layout)
147 pub fn const_val<'a>(fx: &mut FunctionCx<'a, 'tcx>, ty: Ty<'tcx>, const_val: i64) -> CValue<'tcx> where 'tcx: 'a {
148 let cton_ty = fx.cton_type(ty).unwrap();
149 let layout = fx.layout_of(ty);
150 CValue::ByVal(fx.bcx.ins().iconst(cton_ty, const_val), layout)
153 pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
155 CValue::ByRef(addr, _) => CValue::ByRef(addr, layout),
156 CValue::ByVal(val, _) => CValue::ByVal(val, layout),
157 CValue::Func(fun, _) => CValue::Func(fun, layout),
162 /// A place where you can write a value to or read a value from
163 #[derive(Debug, Copy, Clone)]
164 pub enum CPlace<'tcx> {
165 Var(Variable, TyLayout<'tcx>),
166 Addr(Value, TyLayout<'tcx>),
169 impl<'a, 'tcx: 'a> CPlace<'tcx> {
170 pub fn layout(&self) -> TyLayout<'tcx> {
172 CPlace::Var(_, layout) |
173 CPlace::Addr(_, layout) => layout
177 pub fn from_stack_slot(fx: &mut FunctionCx<'a, 'tcx>, stack_slot: StackSlot, ty: Ty<'tcx>) -> CPlace<'tcx> {
178 let layout = fx.layout_of(ty);
179 CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
182 pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx>) -> CValue<'tcx> {
184 CPlace::Var(var, layout) => CValue::ByVal(fx.bcx.use_var(var), layout),
185 CPlace::Addr(addr, layout) => CValue::ByRef(addr, layout),
189 pub fn expect_addr(self) -> Value {
191 CPlace::Addr(addr, _layout) => addr,
192 CPlace::Var(_, _) => bug!("Expected CPlace::Addr, found CPlace::Var"),
196 pub fn write_cvalue(self, fx: &mut FunctionCx<'a, 'tcx>, from: CValue<'tcx>) {
197 match (&self.layout().ty.sty, &from.layout().ty.sty) {
198 (TypeVariants::TyRef(_, t, dest_mut), TypeVariants::TyRef(_, u, src_mut)) if (
199 if *dest_mut != ::rustc::hir::Mutability::MutImmutable && src_mut != dest_mut {
207 // &mut T -> &T is allowed
208 // &'a T -> &'b T is allowed
212 self.layout().ty, from.layout().ty,
213 "Can't write value of incompatible type to place {:?} {:?}\n\n{:#?}",
214 self.layout().ty.sty, from.layout().ty.sty,
221 CPlace::Var(var, _) => {
222 let data = from.load_value(fx);
223 fx.bcx.def_var(var, data)
225 CPlace::Addr(addr, layout) => {
226 let size = layout.size.bytes() as i32;
228 if let Some(_) = fx.cton_type(layout.ty) {
229 let data = from.load_value(fx);
230 fx.bcx.ins().store(MemFlags::new(), data, addr, 0);
232 let from = from.expect_byref();
234 while size - offset >= 8 {
235 let byte = fx.bcx.ins().load(types::I64, MemFlags::new(), from.0, offset);
236 fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
239 while size - offset >= 4 {
240 let byte = fx.bcx.ins().load(types::I32, MemFlags::new(), from.0, offset);
241 fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
244 while offset < size {
245 let byte = fx.bcx.ins().load(types::I8, MemFlags::new(), from.0, offset);
246 fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
254 pub fn place_field(self, fx: &mut FunctionCx<'a, 'tcx>, field: mir::Field) -> CPlace<'tcx> {
255 let base = self.expect_addr();
256 let layout = self.layout();
257 let field_offset = layout.fields.offset(field.index());
258 let field_ty = layout.field(&*fx, field.index());
259 if field_offset.bytes() > 0 {
260 let field_offset = fx.bcx.ins().iconst(types::I64, field_offset.bytes() as i64);
261 CPlace::Addr(fx.bcx.ins().iadd(base, field_offset), field_ty)
263 CPlace::Addr(base, field_ty)
267 pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
269 CPlace::Var(var, _) => CPlace::Var(var, layout),
270 CPlace::Addr(addr, _) => CPlace::Addr(addr, layout),
274 pub fn downcast_variant(self, fx: &FunctionCx<'a, 'tcx>, variant: usize) -> Self {
275 let layout = self.layout().for_variant(fx, variant);
276 self.unchecked_cast_to(layout)
280 pub fn cton_sig_from_fn_sig<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sig: PolyFnSig<'tcx>, substs: &Substs<'tcx>) -> Signature {
281 let sig = tcx.subst_and_normalize_erasing_regions(substs, ParamEnv::reveal_all(), &sig);
282 cton_sig_from_mono_fn_sig(tcx, sig)
285 pub fn cton_sig_from_instance<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, inst: Instance<'tcx>) -> Signature {
286 let fn_ty = inst.ty(tcx);
287 let sig = fn_ty.fn_sig(tcx);
288 cton_sig_from_mono_fn_sig(tcx, sig)
291 pub fn cton_sig_from_mono_fn_sig<'a, 'tcx: 'a>(tcx: TyCtxt<'a, 'tcx, 'tcx>, sig: PolyFnSig<'tcx>) -> Signature {
292 // TODO: monomorphize signature
293 // TODO: this should likely not use skip_binder()
295 let sig = sig.skip_binder();
296 let inputs = sig.inputs();
297 let _output = sig.output();
298 assert!(!sig.variadic, "Variadic function are not yet supported");
299 let call_conv = match sig.abi {
300 _ => CallConv::SystemV,
303 params: Some(types::I64).into_iter() // First param is place to put return val
304 .chain(inputs.into_iter().map(|ty| cton_type_from_ty(tcx, ty).unwrap_or(types::I64)))
305 .map(AbiParam::new).collect(),
308 argument_bytes: None,
312 pub fn cton_intcast<'a, 'tcx: 'a>(fx: &mut FunctionCx<'a, 'tcx>, val: Value, from: Ty<'tcx>, to: Ty<'tcx>, signed: bool) -> Value {
313 let from = fx.cton_type(from).unwrap();
314 let to = fx.cton_type(to).unwrap();
318 if to.wider_or_equal(from) {
320 fx.bcx.ins().sextend(to, val)
322 fx.bcx.ins().uextend(to, val)
325 fx.bcx.ins().ireduce(to, val)
329 pub struct FunctionCx<'a, 'tcx: 'a> {
330 pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
331 pub module: &'a mut Module<CurrentBackend>,
332 pub def_id_fn_id_map: &'a mut HashMap<Instance<'tcx>, FuncId>,
333 pub instance: Instance<'tcx>,
334 pub mir: &'tcx Mir<'tcx>,
335 pub param_substs: &'tcx Substs<'tcx>,
336 pub bcx: FunctionBuilder<'a, Variable>,
337 pub ebb_map: HashMap<BasicBlock, Ebb>,
338 pub local_map: HashMap<Local, CPlace<'tcx>>,
339 pub comments: HashMap<Inst, String>,
340 pub constants: &'a mut HashMap<AllocId, DataId>,
343 impl<'a, 'tcx: 'a> fmt::Debug for FunctionCx<'a, 'tcx> {
344 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
345 writeln!(f, "{:?}", self.def_id_fn_id_map)?;
346 writeln!(f, "{:?}", self.param_substs)?;
347 writeln!(f, "{:?}", self.local_map)?;
349 let mut clif = String::new();
350 let mut writer = ::pretty_clif::CommentWriter(self.comments.clone());
351 ::cranelift::codegen::write::decorate_function(
357 writeln!(f, "\n{}", clif)
361 impl<'a, 'tcx: 'a> LayoutOf for &'a FunctionCx<'a, 'tcx> {
363 type TyLayout = TyLayout<'tcx>;
365 fn layout_of(self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
366 let ty = self.monomorphize(&ty);
367 self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
371 impl<'a, 'tcx> layout::HasTyCtxt<'tcx> for &'a FunctionCx<'a, 'tcx> {
372 fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
377 impl<'a, 'tcx> layout::HasDataLayout for &'a FunctionCx<'a, 'tcx> {
378 fn data_layout(&self) -> &layout::TargetDataLayout {
379 &self.tcx.data_layout
383 impl<'a, 'tcx> HasTargetSpec for &'a FunctionCx<'a, 'tcx> {
384 fn target_spec(&self) -> &Target {
385 &self.tcx.sess.target.target
389 impl<'a, 'tcx: 'a> FunctionCx<'a, 'tcx> {
390 pub fn monomorphize<T>(&self, value: &T) -> T
391 where T: TypeFoldable<'tcx>
393 self.tcx.subst_and_normalize_erasing_regions(
395 ty::ParamEnv::reveal_all(),
400 pub fn cton_type(&self, ty: Ty<'tcx>) -> Option<Type> {
401 cton_type_from_ty(self.tcx, self.monomorphize(&ty))
404 pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
405 *self.ebb_map.get(&bb).unwrap()
408 pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
409 *self.local_map.get(&local).unwrap()
412 pub fn get_function_ref(&mut self, inst: Instance<'tcx>) -> FuncRef {
414 let module = &mut self.module;
415 let func_id = *self.def_id_fn_id_map.entry(inst).or_insert_with(|| {
416 let sig = cton_sig_from_instance(tcx, inst);
417 module.declare_function(&tcx.absolute_item_path_str(inst.def_id()), Linkage::Local, &sig).unwrap()
419 module.declare_func_in_func(func_id, &mut self.bcx.func)
422 pub fn add_comment<'s, S: Into<Cow<'s, str>>>(&mut self, inst: Inst, comment: S) {
423 use std::collections::hash_map::Entry;
424 match self.comments.entry(inst) {
425 Entry::Occupied(mut occ) => {
426 occ.get_mut().push('\n');
427 occ.get_mut().push_str(comment.into().as_ref());
429 Entry::Vacant(vac) => {
430 vac.insert(comment.into().into_owned());