2018-07-14 05:21:45 -05:00
|
|
|
use std::fmt;
|
2018-07-14 04:59:42 -05:00
|
|
|
|
2018-07-20 06:36:53 -05:00
|
|
|
use rustc_target::spec::{HasTargetSpec, Target};
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2018-08-13 05:13:43 -05:00
|
|
|
use cranelift_module::Module;
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2018-07-30 09:57:40 -05:00
|
|
|
use crate::prelude::*;
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2018-08-15 05:36:13 -05:00
|
|
|
pub fn mir_var(loc: Local) -> Variable {
|
|
|
|
Variable::with_u32(loc.index() as u32)
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
|
2018-07-31 05:25:16 -05:00
|
|
|
pub fn cton_type_from_ty<'a, 'tcx: 'a>(
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Option<types::Type> {
|
2018-06-22 12:18:53 -05:00
|
|
|
Some(match ty.sty {
|
|
|
|
TypeVariants::TyBool => types::I8,
|
2018-07-31 05:25:16 -05:00
|
|
|
TypeVariants::TyUint(size) => match size {
|
|
|
|
UintTy::U8 => types::I8,
|
|
|
|
UintTy::U16 => types::I16,
|
|
|
|
UintTy::U32 => types::I32,
|
|
|
|
UintTy::U64 => types::I64,
|
2018-08-09 08:08:54 -05:00
|
|
|
UintTy::U128 => unimpl!("u128"),
|
2018-07-31 05:25:16 -05:00
|
|
|
UintTy::Usize => types::I64,
|
|
|
|
},
|
|
|
|
TypeVariants::TyInt(size) => match size {
|
|
|
|
IntTy::I8 => types::I8,
|
|
|
|
IntTy::I16 => types::I16,
|
|
|
|
IntTy::I32 => types::I32,
|
|
|
|
IntTy::I64 => types::I64,
|
2018-08-09 08:08:54 -05:00
|
|
|
IntTy::I128 => unimpl!("i128"),
|
2018-07-31 05:25:16 -05:00
|
|
|
IntTy::Isize => types::I64,
|
|
|
|
},
|
2018-07-19 12:37:34 -05:00
|
|
|
TypeVariants::TyChar => types::I32,
|
2018-07-31 05:25:16 -05:00
|
|
|
TypeVariants::TyFloat(size) => match size {
|
2018-08-08 03:26:25 -05:00
|
|
|
FloatTy::F32 => types::F32,
|
|
|
|
FloatTy::F64 => types::F64,
|
2018-07-31 05:25:16 -05:00
|
|
|
},
|
2018-06-22 12:18:53 -05:00
|
|
|
TypeVariants::TyFnPtr(_) => types::I64,
|
2018-07-14 09:39:49 -05:00
|
|
|
TypeVariants::TyRawPtr(TypeAndMut { ty, mutbl: _ }) | TypeVariants::TyRef(_, ty, _) => {
|
|
|
|
if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
|
|
|
|
types::I64
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2018-07-31 05:25:16 -05:00
|
|
|
TypeVariants::TyParam(_) => bug!("{:?}: {:?}", ty, ty.sty),
|
2018-06-22 12:18:53 -05:00
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-07-27 12:27:20 -05:00
|
|
|
fn codegen_field<'a, 'tcx: 'a>(
|
2018-08-14 13:31:16 -05:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-27 12:27:20 -05:00
|
|
|
base: Value,
|
|
|
|
layout: TyLayout<'tcx>,
|
2018-07-31 05:25:16 -05:00
|
|
|
field: mir::Field,
|
2018-07-27 12:27:20 -05:00
|
|
|
) -> (Value, TyLayout<'tcx>) {
|
|
|
|
let field_offset = layout.fields.offset(field.index());
|
|
|
|
let field_ty = layout.field(&*fx, field.index());
|
|
|
|
if field_offset.bytes() > 0 {
|
|
|
|
let field_offset = fx.bcx.ins().iconst(types::I64, field_offset.bytes() as i64);
|
|
|
|
(fx.bcx.ins().iadd(base, field_offset), field_ty)
|
|
|
|
} else {
|
|
|
|
(base, field_ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 04:55:32 -05:00
|
|
|
/// A read-only value
|
2018-06-23 11:26:54 -05:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2018-06-26 12:44:19 -05:00
|
|
|
pub enum CValue<'tcx> {
|
|
|
|
ByRef(Value, TyLayout<'tcx>),
|
|
|
|
ByVal(Value, TyLayout<'tcx>),
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
|
2018-06-26 12:44:19 -05:00
|
|
|
impl<'tcx> CValue<'tcx> {
|
|
|
|
pub fn layout(&self) -> TyLayout<'tcx> {
|
|
|
|
match *self {
|
2018-08-19 03:50:39 -05:00
|
|
|
CValue::ByRef(_, layout) | CValue::ByVal(_, layout) => layout,
|
2018-06-26 12:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn force_stack<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
|
2018-07-31 05:25:16 -05:00
|
|
|
where
|
|
|
|
'tcx: 'a,
|
|
|
|
{
|
2018-06-22 12:18:53 -05:00
|
|
|
match self {
|
2018-06-26 12:44:19 -05:00
|
|
|
CValue::ByRef(value, _layout) => value,
|
|
|
|
CValue::ByVal(value, layout) => {
|
2018-06-22 12:18:53 -05:00
|
|
|
let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
|
|
|
|
kind: StackSlotKind::ExplicitSlot,
|
|
|
|
size: layout.size.bytes() as u32,
|
|
|
|
offset: None,
|
|
|
|
});
|
|
|
|
fx.bcx.ins().stack_store(value, stack_slot, 0);
|
|
|
|
fx.bcx.ins().stack_addr(types::I64, stack_slot, 0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn load_value<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value
|
2018-07-31 05:25:16 -05:00
|
|
|
where
|
|
|
|
'tcx: 'a,
|
|
|
|
{
|
2018-06-22 12:18:53 -05:00
|
|
|
match self {
|
2018-06-30 09:27:11 -05:00
|
|
|
CValue::ByRef(addr, layout) => {
|
2018-07-31 05:25:16 -05:00
|
|
|
let cton_ty = fx
|
|
|
|
.cton_type(layout.ty)
|
|
|
|
.expect(&format!("load_value of type {:?}", layout.ty));
|
2018-06-30 09:27:11 -05:00
|
|
|
fx.bcx.ins().load(cton_ty, MemFlags::new(), addr, 0)
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
2018-06-26 12:44:19 -05:00
|
|
|
CValue::ByVal(value, _layout) => value,
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 12:44:19 -05:00
|
|
|
pub fn expect_byref(self) -> (Value, TyLayout<'tcx>) {
|
2018-06-22 12:18:53 -05:00
|
|
|
match self {
|
2018-06-26 12:44:19 -05:00
|
|
|
CValue::ByRef(value, layout) => (value, layout),
|
2018-08-08 02:34:56 -05:00
|
|
|
CValue::ByVal(_, _) => bug!("Expected CValue::ByRef, found CValue::ByVal: {:?}", self),
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-23 11:26:54 -05:00
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn value_field<'a>(
|
|
|
|
self,
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
field: mir::Field,
|
|
|
|
) -> CValue<'tcx>
|
2018-07-31 05:25:16 -05:00
|
|
|
where
|
|
|
|
'tcx: 'a,
|
|
|
|
{
|
2018-06-26 12:44:19 -05:00
|
|
|
let (base, layout) = match self {
|
|
|
|
CValue::ByRef(addr, layout) => (addr, layout),
|
2018-06-23 11:26:54 -05:00
|
|
|
_ => bug!("place_field for {:?}", self),
|
|
|
|
};
|
2018-07-27 12:27:20 -05:00
|
|
|
|
|
|
|
let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
|
|
|
|
CValue::ByRef(field_ptr, field_layout)
|
2018-06-23 11:26:54 -05:00
|
|
|
}
|
|
|
|
|
2018-07-31 05:25:16 -05:00
|
|
|
pub fn const_val<'a>(
|
2018-08-14 13:31:16 -05:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-31 05:25:16 -05:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
const_val: i64,
|
|
|
|
) -> CValue<'tcx>
|
|
|
|
where
|
|
|
|
'tcx: 'a,
|
|
|
|
{
|
2018-06-26 12:44:19 -05:00
|
|
|
let cton_ty = fx.cton_type(ty).unwrap();
|
|
|
|
let layout = fx.layout_of(ty);
|
|
|
|
CValue::ByVal(fx.bcx.ins().iconst(cton_ty, const_val), layout)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
|
|
|
|
match self {
|
|
|
|
CValue::ByRef(addr, _) => CValue::ByRef(addr, layout),
|
|
|
|
CValue::ByVal(val, _) => CValue::ByVal(val, layout),
|
|
|
|
}
|
2018-06-23 11:26:54 -05:00
|
|
|
}
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
|
2018-07-18 04:55:32 -05:00
|
|
|
/// A place where you can write a value to or read a value from
|
2018-06-23 11:26:54 -05:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
2018-06-26 12:44:19 -05:00
|
|
|
pub enum CPlace<'tcx> {
|
2018-08-15 05:36:13 -05:00
|
|
|
Var(Local, TyLayout<'tcx>),
|
2018-06-26 12:44:19 -05:00
|
|
|
Addr(Value, TyLayout<'tcx>),
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
|
2018-06-26 12:44:19 -05:00
|
|
|
impl<'a, 'tcx: 'a> CPlace<'tcx> {
|
|
|
|
pub fn layout(&self) -> TyLayout<'tcx> {
|
|
|
|
match *self {
|
2018-07-31 05:25:16 -05:00
|
|
|
CPlace::Var(_, layout) | CPlace::Addr(_, layout) => layout,
|
2018-06-26 12:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn temp(fx: &mut FunctionCx<'a, 'tcx, impl Backend>, ty: Ty<'tcx>) -> CPlace<'tcx> {
|
2018-08-08 02:34:56 -05:00
|
|
|
let layout = fx.layout_of(ty);
|
|
|
|
let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
|
|
|
|
kind: StackSlotKind::ExplicitSlot,
|
|
|
|
size: layout.size.bytes() as u32,
|
|
|
|
offset: None,
|
|
|
|
});
|
|
|
|
CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
|
|
|
|
}
|
|
|
|
|
2018-07-31 05:25:16 -05:00
|
|
|
pub fn from_stack_slot(
|
2018-08-14 13:31:16 -05:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-31 05:25:16 -05:00
|
|
|
stack_slot: StackSlot,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> CPlace<'tcx> {
|
2018-06-26 12:44:19 -05:00
|
|
|
let layout = fx.layout_of(ty);
|
|
|
|
CPlace::Addr(fx.bcx.ins().stack_addr(types::I64, stack_slot, 0), layout)
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CValue<'tcx> {
|
2018-06-22 12:18:53 -05:00
|
|
|
match self {
|
2018-08-15 05:36:13 -05:00
|
|
|
CPlace::Var(var, layout) => CValue::ByVal(fx.bcx.use_var(mir_var(var)), layout),
|
2018-06-26 12:44:19 -05:00
|
|
|
CPlace::Addr(addr, layout) => CValue::ByRef(addr, layout),
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn expect_addr(self) -> Value {
|
|
|
|
match self {
|
2018-06-26 12:44:19 -05:00
|
|
|
CPlace::Addr(addr, _layout) => addr,
|
|
|
|
CPlace::Var(_, _) => bug!("Expected CPlace::Addr, found CPlace::Var"),
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn write_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, from: CValue<'tcx>) {
|
2018-06-29 11:57:59 -05:00
|
|
|
match (&self.layout().ty.sty, &from.layout().ty.sty) {
|
2018-07-31 05:25:16 -05:00
|
|
|
(TypeVariants::TyRef(_, t, dest_mut), TypeVariants::TyRef(_, u, src_mut))
|
|
|
|
if (if *dest_mut != ::rustc::hir::Mutability::MutImmutable && src_mut != dest_mut {
|
2018-06-29 11:57:59 -05:00
|
|
|
false
|
|
|
|
} else if t != u {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
2018-07-31 05:25:16 -05:00
|
|
|
}) =>
|
|
|
|
{
|
2018-06-29 11:57:59 -05:00
|
|
|
// &mut T -> &T is allowed
|
|
|
|
// &'a T -> &'b T is allowed
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
assert_eq!(
|
2018-07-31 05:25:16 -05:00
|
|
|
self.layout().ty,
|
|
|
|
from.layout().ty,
|
2018-06-29 11:57:59 -05:00
|
|
|
"Can't write value of incompatible type to place {:?} {:?}\n\n{:#?}",
|
2018-07-31 05:25:16 -05:00
|
|
|
self.layout().ty.sty,
|
|
|
|
from.layout().ty.sty,
|
2018-06-29 11:57:59 -05:00
|
|
|
fx,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2018-06-26 12:44:19 -05:00
|
|
|
|
2018-06-22 12:18:53 -05:00
|
|
|
match self {
|
2018-06-26 12:44:19 -05:00
|
|
|
CPlace::Var(var, _) => {
|
|
|
|
let data = from.load_value(fx);
|
2018-08-15 05:36:13 -05:00
|
|
|
fx.bcx.def_var(mir_var(var), data)
|
2018-07-31 05:25:16 -05:00
|
|
|
}
|
2018-06-26 12:44:19 -05:00
|
|
|
CPlace::Addr(addr, layout) => {
|
|
|
|
let size = layout.size.bytes() as i32;
|
|
|
|
|
2018-06-30 09:38:49 -05:00
|
|
|
if let Some(_) = fx.cton_type(layout.ty) {
|
2018-06-26 12:44:19 -05:00
|
|
|
let data = from.load_value(fx);
|
2018-06-30 09:27:11 -05:00
|
|
|
fx.bcx.ins().store(MemFlags::new(), data, addr, 0);
|
2018-06-22 12:18:53 -05:00
|
|
|
} else {
|
2018-07-14 09:39:49 -05:00
|
|
|
let from = from.expect_byref();
|
|
|
|
let mut offset = 0;
|
|
|
|
while size - offset >= 8 {
|
2018-07-31 05:25:16 -05:00
|
|
|
let byte = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.load(types::I64, MemFlags::new(), from.0, offset);
|
2018-07-14 09:39:49 -05:00
|
|
|
fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
|
|
|
|
offset += 8;
|
|
|
|
}
|
|
|
|
while size - offset >= 4 {
|
2018-07-31 05:25:16 -05:00
|
|
|
let byte = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.load(types::I32, MemFlags::new(), from.0, offset);
|
2018-07-14 09:39:49 -05:00
|
|
|
fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
|
|
|
|
offset += 4;
|
|
|
|
}
|
|
|
|
while offset < size {
|
2018-07-31 05:25:16 -05:00
|
|
|
let byte = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.load(types::I8, MemFlags::new(), from.0, offset);
|
2018-07-14 09:39:49 -05:00
|
|
|
fx.bcx.ins().store(MemFlags::new(), byte, addr, offset);
|
|
|
|
offset += 1;
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn place_field(
|
|
|
|
self,
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
field: mir::Field,
|
|
|
|
) -> CPlace<'tcx> {
|
2018-06-23 11:26:54 -05:00
|
|
|
let base = self.expect_addr();
|
2018-06-26 12:44:19 -05:00
|
|
|
let layout = self.layout();
|
2018-07-27 12:27:20 -05:00
|
|
|
|
|
|
|
let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
|
|
|
|
CPlace::Addr(field_ptr, field_layout)
|
2018-06-23 11:26:54 -05:00
|
|
|
}
|
2018-06-26 12:44:19 -05:00
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn place_index(
|
|
|
|
self,
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
index: Value,
|
|
|
|
) -> CPlace<'tcx> {
|
2018-08-08 05:22:16 -05:00
|
|
|
let addr = self.expect_addr();
|
|
|
|
let layout = self.layout();
|
|
|
|
match layout.ty.sty {
|
|
|
|
TypeVariants::TyArray(elem_ty, _) => {
|
|
|
|
let elem_layout = fx.layout_of(elem_ty);
|
2018-08-08 05:45:34 -05:00
|
|
|
let size = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.iconst(types::I64, elem_layout.size.bytes() as i64);
|
2018-08-08 05:22:16 -05:00
|
|
|
let offset = fx.bcx.ins().imul(size, index);
|
|
|
|
CPlace::Addr(fx.bcx.ins().iadd(addr, offset), elem_layout)
|
|
|
|
}
|
|
|
|
TypeVariants::TySlice(_elem_ty) => unimplemented!("place_index(TySlice)"),
|
|
|
|
_ => bug!("place_index({:?})", layout.ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 12:44:19 -05:00
|
|
|
pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
|
|
|
|
match self {
|
|
|
|
CPlace::Var(var, _) => CPlace::Var(var, layout),
|
|
|
|
CPlace::Addr(addr, _) => CPlace::Addr(addr, layout),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub fn downcast_variant(self, fx: &FunctionCx<'a, 'tcx, impl Backend>, variant: usize) -> Self {
|
2018-06-26 12:44:19 -05:00
|
|
|
let layout = self.layout().for_variant(fx, variant);
|
|
|
|
self.unchecked_cast_to(layout)
|
|
|
|
}
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
|
2018-07-31 05:25:16 -05:00
|
|
|
pub fn cton_intcast<'a, 'tcx: 'a>(
|
2018-08-14 13:31:16 -05:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-31 05:25:16 -05:00
|
|
|
val: Value,
|
2018-08-14 04:58:39 -05:00
|
|
|
to: Type,
|
2018-07-31 05:25:16 -05:00
|
|
|
signed: bool,
|
|
|
|
) -> Value {
|
2018-08-14 04:58:39 -05:00
|
|
|
let from = fx.bcx.func.dfg.value_type(val);
|
2018-06-23 11:26:54 -05:00
|
|
|
if from == to {
|
|
|
|
return val;
|
|
|
|
}
|
2018-07-18 09:22:29 -05:00
|
|
|
if to.wider_or_equal(from) {
|
2018-06-23 11:26:54 -05:00
|
|
|
if signed {
|
|
|
|
fx.bcx.ins().sextend(to, val)
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().uextend(to, val)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().ireduce(to, val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
pub struct FunctionCx<'a, 'tcx: 'a, B: Backend + 'a> {
|
2018-06-22 12:18:53 -05:00
|
|
|
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2018-08-14 13:31:16 -05:00
|
|
|
pub module: &'a mut Module<B>,
|
2018-06-23 11:54:15 -05:00
|
|
|
pub instance: Instance<'tcx>,
|
2018-06-22 12:18:53 -05:00
|
|
|
pub mir: &'tcx Mir<'tcx>,
|
2018-06-23 11:54:15 -05:00
|
|
|
pub param_substs: &'tcx Substs<'tcx>,
|
2018-08-15 05:36:13 -05:00
|
|
|
pub bcx: FunctionBuilder<'a>,
|
2018-06-22 12:18:53 -05:00
|
|
|
pub ebb_map: HashMap<BasicBlock, Ebb>,
|
2018-06-26 12:44:19 -05:00
|
|
|
pub local_map: HashMap<Local, CPlace<'tcx>>,
|
2018-07-14 04:59:42 -05:00
|
|
|
pub comments: HashMap<Inst, String>,
|
2018-08-13 05:13:43 -05:00
|
|
|
pub constants: &'a mut crate::constant::ConstantCx,
|
2018-08-15 09:17:59 -05:00
|
|
|
|
|
|
|
/// add_global_comment inserts a comment here
|
|
|
|
pub top_nop: Option<Inst>,
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
impl<'a, 'tcx: 'a, B: Backend + 'a> fmt::Debug for FunctionCx<'a, 'tcx, B> {
|
2018-07-14 05:21:45 -05:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
writeln!(f, "{:?}", self.param_substs)?;
|
|
|
|
writeln!(f, "{:?}", self.local_map)?;
|
|
|
|
|
|
|
|
let mut clif = String::new();
|
2018-07-30 09:57:40 -05:00
|
|
|
let mut writer = crate::pretty_clif::CommentWriter(self.comments.clone());
|
2018-07-14 05:21:45 -05:00
|
|
|
::cranelift::codegen::write::decorate_function(
|
|
|
|
&mut writer,
|
|
|
|
&mut clif,
|
|
|
|
&self.bcx.func,
|
|
|
|
None,
|
|
|
|
).unwrap();
|
|
|
|
writeln!(f, "\n{}", clif)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
impl<'a, 'tcx: 'a, B: Backend> LayoutOf for &'a FunctionCx<'a, 'tcx, B> {
|
2018-06-24 07:01:41 -05:00
|
|
|
type Ty = Ty<'tcx>;
|
|
|
|
type TyLayout = TyLayout<'tcx>;
|
|
|
|
|
|
|
|
fn layout_of(self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
|
|
|
|
let ty = self.monomorphize(&ty);
|
|
|
|
self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
impl<'a, 'tcx, B: Backend + 'a> layout::HasTyCtxt<'tcx> for &'a FunctionCx<'a, 'tcx, B> {
|
2018-06-24 07:25:29 -05:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
impl<'a, 'tcx, B: Backend + 'a> layout::HasDataLayout for &'a FunctionCx<'a, 'tcx, B> {
|
2018-06-24 07:25:29 -05:00
|
|
|
fn data_layout(&self) -> &layout::TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
impl<'a, 'tcx, B: Backend + 'a> HasTargetSpec for &'a FunctionCx<'a, 'tcx, B> {
|
2018-06-24 07:25:29 -05:00
|
|
|
fn target_spec(&self) -> &Target {
|
|
|
|
&self.tcx.sess.target.target
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 13:31:16 -05:00
|
|
|
impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
|
2018-06-23 11:54:15 -05:00
|
|
|
pub fn monomorphize<T>(&self, value: &T) -> T
|
2018-07-31 05:25:16 -05:00
|
|
|
where
|
|
|
|
T: TypeFoldable<'tcx>,
|
2018-06-23 11:54:15 -05:00
|
|
|
{
|
|
|
|
self.tcx.subst_and_normalize_erasing_regions(
|
|
|
|
self.param_substs,
|
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-06-24 07:01:41 -05:00
|
|
|
pub fn cton_type(&self, ty: Ty<'tcx>) -> Option<Type> {
|
2018-07-14 09:39:49 -05:00
|
|
|
cton_type_from_ty(self.tcx, self.monomorphize(&ty))
|
2018-06-24 07:01:41 -05:00
|
|
|
}
|
|
|
|
|
2018-06-22 12:18:53 -05:00
|
|
|
pub fn get_ebb(&self, bb: BasicBlock) -> Ebb {
|
|
|
|
*self.ebb_map.get(&bb).unwrap()
|
|
|
|
}
|
|
|
|
|
2018-06-26 12:44:19 -05:00
|
|
|
pub fn get_local_place(&mut self, local: Local) -> CPlace<'tcx> {
|
2018-06-22 12:18:53 -05:00
|
|
|
*self.local_map.get(&local).unwrap()
|
|
|
|
}
|
|
|
|
}
|