2018-07-14 05:21:45 -05:00
|
|
|
use std::fmt;
|
2018-07-14 04:59:42 -05:00
|
|
|
|
2018-11-07 06:29:38 -06: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-08-18 10:10:02 -05:00
|
|
|
pub fn pointer_ty(tcx: TyCtxt) -> types::Type {
|
|
|
|
match tcx.data_layout.pointer_size.bits() {
|
|
|
|
16 => types::I16,
|
|
|
|
32 => types::I32,
|
|
|
|
64 => types::I64,
|
|
|
|
bits => bug!("ptr_sized_integer: unknown pointer bit size {}", bits),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 09:23:39 -06:00
|
|
|
fn scalar_to_clif_type(tcx: TyCtxt, scalar: &Scalar) -> Type {
|
2018-11-05 11:03:47 -06:00
|
|
|
match scalar.value.size(&tcx).bits() {
|
2018-08-22 08:38:10 -05:00
|
|
|
8 => types::I8,
|
|
|
|
16 => types::I16,
|
|
|
|
32 => types::I32,
|
|
|
|
64 => types::I64,
|
|
|
|
size => bug!("Unsupported scalar size {}", size),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-12 09:23:39 -06:00
|
|
|
pub fn clif_type_from_ty<'a, 'tcx: 'a>(
|
2018-07-31 05:25:16 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Option<types::Type> {
|
2018-06-22 12:18:53 -05:00
|
|
|
Some(match ty.sty {
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::Bool => types::I8,
|
|
|
|
ty::Uint(size) => match size {
|
2018-07-31 05:25:16 -05:00
|
|
|
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-08-18 10:10:02 -05:00
|
|
|
UintTy::Usize => pointer_ty(tcx),
|
2018-07-31 05:25:16 -05:00
|
|
|
},
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::Int(size) => match size {
|
2018-07-31 05:25:16 -05:00
|
|
|
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-08-22 05:31:45 -05:00
|
|
|
IntTy::Isize => pointer_ty(tcx),
|
2018-07-31 05:25:16 -05:00
|
|
|
},
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::Char => types::I32,
|
|
|
|
ty::Float(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-08-24 07:51:02 -05:00
|
|
|
ty::FnPtr(_) => pointer_ty(tcx),
|
|
|
|
ty::RawPtr(TypeAndMut { ty, mutbl: _ }) | ty::Ref(_, ty, _) => {
|
2018-07-14 09:39:49 -05:00
|
|
|
if ty.is_sized(tcx.at(DUMMY_SP), ParamEnv::reveal_all()) {
|
2018-08-18 10:10:02 -05:00
|
|
|
pointer_ty(tcx)
|
2018-07-14 09:39:49 -05:00
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 07:51:02 -05:00
|
|
|
ty::Param(_) => bug!("{:?}: {:?}", ty, ty.sty),
|
2018-06-22 12:18:53 -05:00
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-10-07 04:18:08 -05:00
|
|
|
pub fn codegen_select(bcx: &mut FunctionBuilder, cond: Value, lhs: Value, rhs: Value) -> Value {
|
|
|
|
let lhs_ty = bcx.func.dfg.value_type(lhs);
|
|
|
|
let rhs_ty = bcx.func.dfg.value_type(rhs);
|
|
|
|
assert_eq!(lhs_ty, rhs_ty);
|
|
|
|
if lhs_ty == types::I8 || lhs_ty == types::I16 {
|
|
|
|
// FIXME workaround for missing enocding for select.i8
|
|
|
|
let lhs = bcx.ins().uextend(types::I32, lhs);
|
|
|
|
let rhs = bcx.ins().uextend(types::I32, rhs);
|
|
|
|
let res = bcx.ins().select(cond, lhs, rhs);
|
|
|
|
bcx.ins().ireduce(lhs_ty, res)
|
|
|
|
} else {
|
|
|
|
bcx.ins().select(cond, lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-08-22 05:31:45 -05:00
|
|
|
(
|
|
|
|
fx.bcx.ins().iadd_imm(base, field_offset.bytes() as i64),
|
|
|
|
field_ty,
|
|
|
|
)
|
2018-07-27 12:27:20 -05:00
|
|
|
} 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-08-22 05:35:07 -05:00
|
|
|
ByValPair(Value, 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-22 05:35:07 -05:00
|
|
|
CValue::ByRef(_, layout)
|
|
|
|
| CValue::ByVal(_, layout)
|
|
|
|
| CValue::ByValPair(_, _, 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,
|
|
|
|
});
|
2018-11-07 06:32:02 -06:00
|
|
|
let addr = fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0);
|
2018-09-30 09:33:55 -05:00
|
|
|
fx.bcx.ins().store(MemFlags::new(), value, addr, 0);
|
|
|
|
addr
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
2018-08-22 05:35:07 -05:00
|
|
|
CValue::ByValPair(value, extra, layout) => {
|
|
|
|
let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
|
|
|
|
kind: StackSlotKind::ExplicitSlot,
|
|
|
|
size: layout.size.bytes() as u32,
|
|
|
|
offset: None,
|
|
|
|
});
|
|
|
|
let base = fx.bcx.ins().stack_addr(types::I64, stack_slot, 0);
|
|
|
|
let a_addr = codegen_field(fx, base, layout, mir::Field::new(0)).0;
|
|
|
|
let b_addr = codegen_field(fx, base, layout, mir::Field::new(1)).0;
|
|
|
|
fx.bcx.ins().store(MemFlags::new(), value, a_addr, 0);
|
|
|
|
fx.bcx.ins().store(MemFlags::new(), extra, b_addr, 0);
|
|
|
|
base
|
|
|
|
}
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 06:37:56 -06:00
|
|
|
/// Load a value with layout.abi of scalar
|
|
|
|
pub fn load_scalar<'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) => {
|
2019-01-02 06:37:56 -06:00
|
|
|
let scalar = match layout.abi {
|
|
|
|
layout::Abi::Scalar(ref scalar) => scalar.clone(),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let clif_ty = crate::abi::scalar_to_clif_type(fx.tcx, scalar);
|
2018-11-12 09:23:39 -06:00
|
|
|
fx.bcx.ins().load(clif_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,
|
2019-03-02 14:09:28 -06:00
|
|
|
CValue::ByValPair(_, _, _layout) => bug!("Please use load_scalar_pair for ByValPair"),
|
2018-08-22 05:35:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 14:09:28 -06:00
|
|
|
/// Load a value pair with layout.abi of scalar pair
|
|
|
|
pub fn load_scalar_pair<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> (Value, Value)
|
2018-08-22 05:35:07 -05:00
|
|
|
where
|
|
|
|
'tcx: 'a,
|
|
|
|
{
|
|
|
|
match self {
|
|
|
|
CValue::ByRef(addr, layout) => {
|
2019-03-02 14:09:28 -06:00
|
|
|
let (a, b) = match &layout.abi {
|
|
|
|
layout::Abi::ScalarPair(a, b) => (a.clone(), b.clone()),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
let clif_ty1 = crate::abi::scalar_to_clif_type(fx.tcx, a.clone());
|
|
|
|
let clif_ty2 = crate::abi::scalar_to_clif_type(fx.tcx, b);
|
|
|
|
let val1 = fx.bcx.ins().load(clif_ty1, MemFlags::new(), addr, 0);
|
|
|
|
let val2 = fx.bcx.ins().load(
|
|
|
|
clif_ty2,
|
|
|
|
MemFlags::new(),
|
|
|
|
addr,
|
|
|
|
a.value.size(&fx.tcx).bytes() as i32,
|
2018-08-22 05:35:07 -05:00
|
|
|
);
|
|
|
|
(val1, val2)
|
|
|
|
}
|
2019-03-02 14:09:28 -06:00
|
|
|
CValue::ByVal(_, _layout) => bug!("Please use load_scalar for ByVal"),
|
2018-08-22 05:35:07 -05:00
|
|
|
CValue::ByValPair(val1, val2, _layout) => (val1, val2),
|
2018-06-22 12:18:53 -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-08-22 08:38:56 -05:00
|
|
|
pub fn unsize_value<'a>(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, dest: CPlace<'tcx>) {
|
2018-12-29 08:33:34 -06:00
|
|
|
crate::unsize::coerce_unsized_into(fx, self, dest);
|
2018-08-22 08:38:56 -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-11-12 09:23:39 -06:00
|
|
|
let clif_ty = fx.clif_type(ty).unwrap();
|
2018-06-26 12:44:19 -05:00
|
|
|
let layout = fx.layout_of(ty);
|
2018-11-12 09:23:39 -06:00
|
|
|
CValue::ByVal(fx.bcx.ins().iconst(clif_ty, const_val), layout)
|
2018-06-26 12:44:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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-08-22 05:35:07 -05:00
|
|
|
CValue::ByValPair(val, extra, _) => CValue::ByValPair(val, extra, layout),
|
2018-06-26 12:44:19 -05:00
|
|
|
}
|
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-08-22 08:38:10 -05:00
|
|
|
Addr(Value, Option<Value>, TyLayout<'tcx>),
|
2019-02-04 12:27:58 -06:00
|
|
|
Stack(StackSlot, TyLayout<'tcx>),
|
2019-02-21 08:06:09 -06:00
|
|
|
NoPlace(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 {
|
2019-02-04 12:27:58 -06:00
|
|
|
CPlace::Var(_, layout)
|
|
|
|
| CPlace::Addr(_, _, layout)
|
2019-02-04 12:58:07 -06:00
|
|
|
| CPlace::Stack(_, layout)
|
2019-02-21 08:06:09 -06:00
|
|
|
| CPlace::NoPlace(layout) => layout,
|
2018-06-26 12:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 08:06:09 -06:00
|
|
|
pub fn new_stack_slot(
|
|
|
|
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);
|
2018-08-22 08:38:10 -05:00
|
|
|
assert!(!layout.is_unsized());
|
2019-02-04 12:58:07 -06:00
|
|
|
if layout.size.bytes() == 0 {
|
|
|
|
return CPlace::NoPlace(layout);
|
|
|
|
}
|
|
|
|
|
2018-08-08 02:34:56 -05:00
|
|
|
let stack_slot = fx.bcx.create_stack_slot(StackSlotData {
|
|
|
|
kind: StackSlotKind::ExplicitSlot,
|
|
|
|
size: layout.size.bytes() as u32,
|
|
|
|
offset: None,
|
|
|
|
});
|
2019-02-04 12:27:58 -06:00
|
|
|
CPlace::Stack(stack_slot, 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-08-22 08:38:10 -05:00
|
|
|
CPlace::Addr(addr, extra, layout) => {
|
|
|
|
assert!(extra.is_none(), "unsized values are not yet supported");
|
|
|
|
CValue::ByRef(addr, layout)
|
|
|
|
}
|
2019-02-21 08:06:09 -06:00
|
|
|
CPlace::Stack(stack_slot, layout) => CValue::ByRef(
|
|
|
|
fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
|
|
|
|
layout,
|
|
|
|
),
|
|
|
|
CPlace::NoPlace(layout) => CValue::ByRef(
|
|
|
|
fx.bcx
|
|
|
|
.ins()
|
|
|
|
.iconst(fx.pointer_type, fx.pointer_type.bytes() as i64),
|
|
|
|
layout,
|
|
|
|
),
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:27:58 -06:00
|
|
|
pub fn to_addr(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> Value {
|
2019-02-04 12:18:39 -06:00
|
|
|
match self.to_addr_maybe_unsized(fx) {
|
|
|
|
(addr, None) => addr,
|
|
|
|
(_, Some(_)) => bug!("Expected sized cplace, found {:?}", self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn to_addr_maybe_unsized(
|
|
|
|
self,
|
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
) -> (Value, Option<Value>) {
|
2018-06-22 12:18:53 -05:00
|
|
|
match self {
|
2019-02-04 12:18:39 -06:00
|
|
|
CPlace::Addr(addr, extra, _layout) => (addr, extra),
|
2019-02-21 08:06:09 -06:00
|
|
|
CPlace::Stack(stack_slot, _layout) => (
|
|
|
|
fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
|
|
|
|
None,
|
|
|
|
),
|
2019-02-13 07:32:30 -06:00
|
|
|
CPlace::NoPlace(_) => (fx.bcx.ins().iconst(fx.pointer_type, 45), None),
|
2018-06-26 12:44:19 -05:00
|
|
|
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>) {
|
2019-03-03 09:33:46 -06:00
|
|
|
use rustc::hir::Mutability::*;
|
|
|
|
|
2018-11-17 04:41:20 -06:00
|
|
|
let from_ty = from.layout().ty;
|
|
|
|
let to_ty = self.layout().ty;
|
2019-03-03 09:33:46 -06:00
|
|
|
|
|
|
|
fn assert_assignable<'a, 'tcx: 'a>(fx: &FunctionCx<'a, 'tcx, impl Backend>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) {
|
|
|
|
match (&from_ty.sty, &to_ty.sty) {
|
|
|
|
(ty::Ref(_, t, MutImmutable), ty::Ref(_, u, MutImmutable))
|
|
|
|
| (ty::Ref(_, t, MutMutable), ty::Ref(_, u, MutImmutable))
|
|
|
|
| (ty::Ref(_, t, MutMutable), ty::Ref(_, u, MutMutable)) => {
|
|
|
|
assert_assignable(fx, t, u);
|
|
|
|
// &mut T -> &T is allowed
|
|
|
|
// &'a T -> &'b T is allowed
|
|
|
|
}
|
|
|
|
(ty::Ref(_, _, MutImmutable), ty::Ref(_, _, MutMutable)) => {
|
|
|
|
panic!("Cant assign value of type {} to place of type {}", from_ty.sty, to_ty.sty)
|
|
|
|
}
|
|
|
|
(ty::FnPtr(_), ty::FnPtr(_)) => {
|
|
|
|
let from_sig = fx.tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
&from_ty.fn_sig(fx.tcx),
|
|
|
|
);
|
|
|
|
let to_sig = fx.tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
&to_ty.fn_sig(fx.tcx),
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
from_sig, to_sig,
|
|
|
|
"Can't write fn ptr with incompatible sig {:?} to place with sig {:?}\n\n{:#?}",
|
|
|
|
from_sig, to_sig, fx,
|
|
|
|
);
|
|
|
|
// fn(&T) -> for<'l> fn(&'l T) is allowed
|
|
|
|
}
|
|
|
|
(ty::Dynamic(from_traits, _), ty::Dynamic(to_traits, _)) => {
|
|
|
|
let from_traits = fx.tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
from_traits,
|
|
|
|
);
|
|
|
|
let to_traits = fx.tcx.normalize_erasing_late_bound_regions(
|
|
|
|
ParamEnv::reveal_all(),
|
|
|
|
to_traits,
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
from_traits, to_traits,
|
|
|
|
"Can't write trait object of incompatible traits {:?} to place with traits {:?}\n\n{:#?}",
|
|
|
|
from_traits, to_traits, fx,
|
|
|
|
);
|
|
|
|
// dyn for<'r> Trait<'r> -> dyn Trait<'_> is allowed
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
assert_eq!(
|
|
|
|
from_ty,
|
|
|
|
to_ty,
|
|
|
|
"Can't write value with incompatible type {:?} to place with type {:?}\n\n{:#?}",
|
|
|
|
from_ty.sty,
|
|
|
|
to_ty.sty,
|
|
|
|
fx,
|
|
|
|
);
|
|
|
|
}
|
2018-06-29 11:57:59 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-26 12:44:19 -05:00
|
|
|
|
2019-03-03 09:33:46 -06:00
|
|
|
assert_assignable(fx, from_ty, to_ty);
|
|
|
|
|
2019-02-04 12:18:39 -06:00
|
|
|
let (addr, dst_layout) = match self {
|
2018-06-26 12:44:19 -05:00
|
|
|
CPlace::Var(var, _) => {
|
2019-01-02 06:37:56 -06:00
|
|
|
let data = from.load_scalar(fx);
|
2019-02-04 12:18:39 -06:00
|
|
|
fx.bcx.def_var(mir_var(var), data);
|
|
|
|
return;
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|
2019-02-04 12:18:39 -06:00
|
|
|
CPlace::Addr(addr, None, dst_layout) => (addr, dst_layout),
|
2019-02-21 08:06:09 -06:00
|
|
|
CPlace::Stack(stack_slot, dst_layout) => (
|
|
|
|
fx.bcx.ins().stack_addr(fx.pointer_type, stack_slot, 0),
|
|
|
|
dst_layout,
|
|
|
|
),
|
2019-02-04 12:58:07 -06:00
|
|
|
CPlace::NoPlace(layout) => {
|
2019-03-11 14:45:59 -05:00
|
|
|
assert_eq!(layout.size.bytes(), 0);
|
|
|
|
assert_eq!(from.layout().size.bytes(), 0);
|
2019-02-04 12:58:07 -06:00
|
|
|
return;
|
|
|
|
}
|
2018-08-22 08:38:10 -05:00
|
|
|
CPlace::Addr(_, _, _) => bug!("Can't write value to unsized place {:?}", self),
|
2019-02-04 12:18:39 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
match from {
|
|
|
|
CValue::ByVal(val, _src_layout) => {
|
|
|
|
fx.bcx.ins().store(MemFlags::new(), val, addr, 0);
|
|
|
|
}
|
|
|
|
CValue::ByValPair(val1, val2, _src_layout) => {
|
|
|
|
let val1_offset = dst_layout.fields.offset(0).bytes() as i32;
|
|
|
|
let val2_offset = dst_layout.fields.offset(1).bytes() as i32;
|
|
|
|
fx.bcx.ins().store(MemFlags::new(), val1, addr, val1_offset);
|
|
|
|
fx.bcx.ins().store(MemFlags::new(), val2, addr, val2_offset);
|
|
|
|
}
|
|
|
|
CValue::ByRef(from, src_layout) => {
|
|
|
|
let size = dst_layout.size.bytes();
|
|
|
|
let src_align = src_layout.align.abi.bytes() as u8;
|
|
|
|
let dst_align = dst_layout.align.abi.bytes() as u8;
|
2019-02-21 08:06:09 -06:00
|
|
|
fx.bcx.emit_small_memcpy(
|
|
|
|
fx.module.target_config(),
|
|
|
|
addr,
|
|
|
|
from,
|
|
|
|
size,
|
|
|
|
dst_align,
|
|
|
|
src_align,
|
|
|
|
);
|
2019-02-04 12:18:39 -06:00
|
|
|
}
|
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> {
|
2019-02-04 12:18:39 -06:00
|
|
|
let layout = self.layout();
|
|
|
|
let (base, extra) = self.to_addr_maybe_unsized(fx);
|
|
|
|
|
|
|
|
let (field_ptr, field_layout) = codegen_field(fx, base, layout, field);
|
|
|
|
let extra = if field_layout.is_unsized() {
|
|
|
|
assert!(extra.is_some());
|
|
|
|
extra
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
CPlace::Addr(field_ptr, extra, 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-25 04:22:48 -05:00
|
|
|
let (elem_layout, addr) = match self.layout().ty.sty {
|
2019-02-04 12:27:58 -06:00
|
|
|
ty::Array(elem_ty, _) => (fx.layout_of(elem_ty), self.to_addr(fx)),
|
2019-02-21 08:06:09 -06:00
|
|
|
ty::Slice(elem_ty) => (fx.layout_of(elem_ty), self.to_addr_maybe_unsized(fx).0),
|
2018-08-25 04:22:48 -05:00
|
|
|
_ => bug!("place_index({:?})", self.layout().ty),
|
|
|
|
};
|
2018-08-22 10:58:25 -05:00
|
|
|
|
2018-08-25 04:22:48 -05:00
|
|
|
let offset = fx
|
|
|
|
.bcx
|
|
|
|
.ins()
|
|
|
|
.imul_imm(index, elem_layout.size.bytes() as i64);
|
2018-08-22 10:58:25 -05:00
|
|
|
|
2018-08-25 04:22:48 -05:00
|
|
|
CPlace::Addr(fx.bcx.ins().iadd(addr, offset), None, elem_layout)
|
2018-08-08 05:22:16 -05:00
|
|
|
}
|
|
|
|
|
2018-08-22 08:38:10 -05:00
|
|
|
pub fn place_deref(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CPlace<'tcx> {
|
2018-09-04 12:04:25 -05:00
|
|
|
let inner_layout = fx.layout_of(self.layout().ty.builtin_deref(true).unwrap().ty);
|
2018-08-22 08:38:10 -05:00
|
|
|
if !inner_layout.is_unsized() {
|
2019-01-02 06:37:56 -06:00
|
|
|
CPlace::Addr(self.to_cvalue(fx).load_scalar(fx), None, inner_layout)
|
2018-08-22 08:38:10 -05:00
|
|
|
} else {
|
|
|
|
match self.layout().abi {
|
|
|
|
Abi::ScalarPair(ref a, ref b) => {
|
2019-02-04 12:27:58 -06:00
|
|
|
let addr = self.to_addr(fx);
|
2018-08-22 08:38:10 -05:00
|
|
|
let ptr =
|
|
|
|
fx.bcx
|
|
|
|
.ins()
|
2018-11-12 09:23:39 -06:00
|
|
|
.load(scalar_to_clif_type(fx.tcx, a), MemFlags::new(), addr, 0);
|
2018-08-22 08:38:10 -05:00
|
|
|
let extra = fx.bcx.ins().load(
|
2018-11-12 09:23:39 -06:00
|
|
|
scalar_to_clif_type(fx.tcx, b),
|
2018-08-22 08:38:10 -05:00
|
|
|
MemFlags::new(),
|
|
|
|
addr,
|
2018-11-05 11:03:47 -06:00
|
|
|
a.value.size(&fx.tcx).bytes() as u32 as i32,
|
2018-08-22 08:38:10 -05:00
|
|
|
);
|
|
|
|
CPlace::Addr(ptr, Some(extra), inner_layout)
|
|
|
|
}
|
|
|
|
_ => bug!(
|
|
|
|
"Fat ptr doesn't have abi ScalarPair, but it has {:?}",
|
|
|
|
self.layout().abi
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn write_place_ref(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>, dest: CPlace<'tcx>) {
|
|
|
|
if !self.layout().is_unsized() {
|
2019-02-04 12:27:58 -06:00
|
|
|
let ptr = CValue::ByVal(self.to_addr(fx), dest.layout());
|
2018-08-22 08:38:10 -05:00
|
|
|
dest.write_cvalue(fx, ptr);
|
|
|
|
} else {
|
2019-02-04 12:18:39 -06:00
|
|
|
let (value, extra) = self.to_addr_maybe_unsized(fx);
|
|
|
|
|
|
|
|
match dest.layout().abi {
|
|
|
|
Abi::ScalarPair(ref a, _) => {
|
2019-02-04 12:27:58 -06:00
|
|
|
let dest_addr = dest.to_addr(fx);
|
2019-02-21 08:06:09 -06:00
|
|
|
fx.bcx.ins().store(MemFlags::new(), value, dest_addr, 0);
|
2019-02-04 12:18:39 -06:00
|
|
|
fx.bcx.ins().store(
|
|
|
|
MemFlags::new(),
|
|
|
|
extra.expect("unsized type without metadata"),
|
|
|
|
dest_addr,
|
|
|
|
a.value.size(&fx.tcx).bytes() as u32 as i32,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
_ => bug!(
|
|
|
|
"Non ScalarPair abi {:?} in write_place_ref dest",
|
|
|
|
dest.layout().abi
|
|
|
|
),
|
2018-08-22 08:38:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 12:44:19 -05:00
|
|
|
pub fn unchecked_cast_to(self, layout: TyLayout<'tcx>) -> Self {
|
2019-02-04 12:58:07 -06:00
|
|
|
assert!(!self.layout().is_unsized());
|
2018-06-26 12:44:19 -05:00
|
|
|
match self {
|
|
|
|
CPlace::Var(var, _) => CPlace::Var(var, layout),
|
2019-02-21 08:06:09 -06:00
|
|
|
CPlace::Addr(addr, extra, _) => CPlace::Addr(addr, extra, layout),
|
|
|
|
CPlace::Stack(stack_slot, _) => CPlace::Stack(stack_slot, layout),
|
2019-02-04 12:58:07 -06:00
|
|
|
CPlace::NoPlace(_) => {
|
|
|
|
assert!(layout.size.bytes() == 0);
|
|
|
|
CPlace::NoPlace(layout)
|
|
|
|
}
|
2018-06-26 12:44:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-24 05:47:53 -06:00
|
|
|
pub fn downcast_variant(
|
|
|
|
self,
|
|
|
|
fx: &FunctionCx<'a, 'tcx, impl Backend>,
|
|
|
|
variant: VariantIdx,
|
|
|
|
) -> 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-11-12 09:23:39 -06:00
|
|
|
pub fn clif_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-11-17 11:23:52 -06:00
|
|
|
pub struct FunctionCx<'a, 'tcx: 'a, B: Backend> {
|
2018-12-18 11:28:02 -06:00
|
|
|
// FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
|
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-11-03 07:14:28 -05:00
|
|
|
pub pointer_type: Type, // Cached from module
|
2018-12-01 04:49:44 -06:00
|
|
|
|
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-12-01 04:49:44 -06:00
|
|
|
|
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-12-01 04:49:44 -06:00
|
|
|
|
2018-12-21 14:42:29 -06:00
|
|
|
pub clif_comments: crate::pretty_clif::CommentWriter,
|
2018-08-13 05:13:43 -05:00
|
|
|
pub constants: &'a mut crate::constant::ConstantCx,
|
2018-09-08 11:00:06 -05:00
|
|
|
pub caches: &'a mut Caches<'tcx>,
|
2019-01-19 05:18:39 -06:00
|
|
|
pub source_info_set: indexmap::IndexSet<SourceInfo>,
|
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 {
|
2018-12-01 04:49:44 -06:00
|
|
|
writeln!(f, "{:?}", self.instance.substs)?;
|
2018-07-14 05:21:45 -05:00
|
|
|
writeln!(f, "{:?}", self.local_map)?;
|
|
|
|
|
|
|
|
let mut clif = String::new();
|
|
|
|
::cranelift::codegen::write::decorate_function(
|
2018-12-21 14:42:29 -06:00
|
|
|
&mut &self.clif_comments,
|
2018-07-14 05:21:45 -05:00
|
|
|
&mut clif,
|
|
|
|
&self.bcx.func,
|
|
|
|
None,
|
2018-10-10 12:07:13 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
2018-07-14 05:21:45 -05:00
|
|
|
writeln!(f, "\n{}", clif)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 11:03:47 -06:00
|
|
|
impl<'a, 'tcx: 'a, B: Backend> LayoutOf for FunctionCx<'a, 'tcx, B> {
|
2018-06-24 07:01:41 -05:00
|
|
|
type Ty = Ty<'tcx>;
|
|
|
|
type TyLayout = TyLayout<'tcx>;
|
|
|
|
|
2018-11-05 11:03:47 -06:00
|
|
|
fn layout_of(&self, ty: Ty<'tcx>) -> TyLayout<'tcx> {
|
2018-06-24 07:01:41 -05:00
|
|
|
let ty = self.monomorphize(&ty);
|
|
|
|
self.tcx.layout_of(ParamEnv::reveal_all().and(&ty)).unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 11:03:47 -06:00
|
|
|
impl<'a, 'tcx, B: Backend + 'a> layout::HasTyCtxt<'tcx> for FunctionCx<'a, 'tcx, B> {
|
2018-06-24 07:25:29 -05:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'b, 'tcx, 'tcx> {
|
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 11:03:47 -06:00
|
|
|
impl<'a, 'tcx, B: Backend + 'a> layout::HasDataLayout for FunctionCx<'a, 'tcx, B> {
|
2018-06-24 07:25:29 -05:00
|
|
|
fn data_layout(&self) -> &layout::TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-05 11:03:47 -06:00
|
|
|
impl<'a, 'tcx, B: Backend + 'a> HasTargetSpec for FunctionCx<'a, 'tcx, B> {
|
2018-06-24 07:25:29 -05:00
|
|
|
fn target_spec(&self) -> &Target {
|
|
|
|
&self.tcx.sess.target.target
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 09:01:33 -06:00
|
|
|
impl<'a, 'tcx, B: Backend> BackendTypes for FunctionCx<'a, 'tcx, B> {
|
|
|
|
type Value = Value;
|
|
|
|
type BasicBlock = Ebb;
|
|
|
|
type Type = Type;
|
|
|
|
type Funclet = !;
|
|
|
|
type DIScope = !;
|
|
|
|
}
|
|
|
|
|
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(
|
2018-12-01 04:49:44 -06:00
|
|
|
self.instance.substs,
|
2018-06-23 11:54:15 -05:00
|
|
|
ty::ParamEnv::reveal_all(),
|
|
|
|
value,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-11-12 09:23:39 -06:00
|
|
|
pub fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
|
|
|
|
clif_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()
|
|
|
|
}
|
2019-01-17 11:07:27 -06:00
|
|
|
|
|
|
|
pub fn set_debug_loc(&mut self, source_info: mir::SourceInfo) {
|
2019-01-19 05:18:39 -06:00
|
|
|
let (index, _) = self.source_info_set.insert_full(source_info);
|
|
|
|
self.bcx.set_srcloc(SourceLoc::new(index as u32));
|
2019-01-17 11:07:27 -06:00
|
|
|
}
|
2018-06-22 12:18:53 -05:00
|
|
|
}
|