2018-11-07 06:29:38 -06:00
|
|
|
use rustc_target::spec::{HasTargetSpec, Target};
|
2018-06-22 12:18:53 -05:00
|
|
|
|
2019-07-20 10:44:41 -05:00
|
|
|
use cranelift::codegen::ir::{Opcode, InstructionData, ValueDef};
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
pub fn clif_type_from_ty<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2018-07-31 05:25:16 -05:00
|
|
|
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,
|
2019-06-12 13:54:38 -05:00
|
|
|
UintTy::U128 => types::I128,
|
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,
|
2019-06-12 13:54:38 -05:00
|
|
|
IntTy::I128 => types::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;
|
|
|
|
}
|
|
|
|
}
|
2019-03-16 04:54:02 -05:00
|
|
|
ty::Param(_) => bug!("ty param {:?}", ty),
|
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 {
|
2019-07-20 10:44:41 -05:00
|
|
|
// FIXME workaround for missing encoding for select.i8
|
2018-10-07 04:18:08 -05:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn codegen_icmp(
|
|
|
|
fx: &mut FunctionCx<'_, '_, impl Backend>,
|
2019-08-15 04:36:06 -05:00
|
|
|
intcc: IntCC,
|
|
|
|
lhs: Value,
|
|
|
|
rhs: Value,
|
|
|
|
) -> Value {
|
|
|
|
let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
|
|
|
|
let rhs_ty = fx.bcx.func.dfg.value_type(rhs);
|
|
|
|
assert_eq!(lhs_ty, rhs_ty);
|
|
|
|
if lhs_ty == types::I128 {
|
|
|
|
// FIXME legalize `icmp.i128` in Cranelift
|
|
|
|
|
|
|
|
let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
|
|
|
|
let (rhs_lsb, rhs_msb) = fx.bcx.ins().isplit(rhs);
|
|
|
|
|
|
|
|
match intcc {
|
|
|
|
IntCC::Equal => {
|
|
|
|
let lsb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_lsb, rhs_lsb);
|
|
|
|
let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
|
|
|
|
fx.bcx.ins().band(lsb_eq, msb_eq)
|
|
|
|
}
|
|
|
|
IntCC::NotEqual => {
|
|
|
|
let lsb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_lsb, rhs_lsb);
|
|
|
|
let msb_ne = fx.bcx.ins().icmp(IntCC::NotEqual, lhs_msb, rhs_msb);
|
|
|
|
fx.bcx.ins().bor(lsb_ne, msb_ne)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// if msb_eq {
|
|
|
|
// lsb_cc
|
|
|
|
// } else {
|
|
|
|
// msb_cc
|
|
|
|
// }
|
|
|
|
|
|
|
|
let msb_eq = fx.bcx.ins().icmp(IntCC::Equal, lhs_msb, rhs_msb);
|
|
|
|
let lsb_cc = fx.bcx.ins().icmp(intcc, lhs_lsb, rhs_lsb);
|
|
|
|
let msb_cc = fx.bcx.ins().icmp(intcc, lhs_msb, rhs_msb);
|
|
|
|
|
|
|
|
fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fx.bcx.ins().icmp(intcc, lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub fn codegen_icmp_imm(
|
|
|
|
fx: &mut FunctionCx<'_, '_, impl Backend>,
|
2019-08-15 04:36:06 -05:00
|
|
|
intcc: IntCC,
|
|
|
|
lhs: Value,
|
|
|
|
rhs: i128,
|
|
|
|
) -> Value {
|
|
|
|
let lhs_ty = fx.bcx.func.dfg.value_type(lhs);
|
|
|
|
if lhs_ty == types::I128 {
|
|
|
|
// FIXME legalize `icmp_imm.i128` in Cranelift
|
|
|
|
|
|
|
|
let (lhs_lsb, lhs_msb) = fx.bcx.ins().isplit(lhs);
|
|
|
|
let (rhs_lsb, rhs_msb) = (rhs as u128 as u64 as i64, (rhs as u128 >> 64) as u64 as i64);
|
|
|
|
|
|
|
|
match intcc {
|
|
|
|
IntCC::Equal => {
|
|
|
|
let lsb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_lsb, rhs_lsb);
|
|
|
|
let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb);
|
|
|
|
fx.bcx.ins().band(lsb_eq, msb_eq)
|
|
|
|
}
|
|
|
|
IntCC::NotEqual => {
|
|
|
|
let lsb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_lsb, rhs_lsb);
|
|
|
|
let msb_ne = fx.bcx.ins().icmp_imm(IntCC::NotEqual, lhs_msb, rhs_msb);
|
|
|
|
fx.bcx.ins().bor(lsb_ne, msb_ne)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// if msb_eq {
|
|
|
|
// lsb_cc
|
|
|
|
// } else {
|
|
|
|
// msb_cc
|
|
|
|
// }
|
|
|
|
|
|
|
|
let msb_eq = fx.bcx.ins().icmp_imm(IntCC::Equal, lhs_msb, rhs_msb);
|
|
|
|
let lsb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_lsb, rhs_lsb);
|
|
|
|
let msb_cc = fx.bcx.ins().icmp_imm(intcc, lhs_msb, rhs_msb);
|
|
|
|
|
|
|
|
fx.bcx.ins().select(msb_eq, lsb_cc, msb_cc)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let rhs = i64::try_from(rhs).expect("codegen_icmp_imm rhs out of range for <128bit int");
|
|
|
|
fx.bcx.ins().icmp_imm(intcc, lhs, rhs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:44:41 -05:00
|
|
|
fn resolve_normal_value_imm(func: &Function, val: Value) -> Option<i64> {
|
|
|
|
if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
|
|
|
|
if let InstructionData::UnaryImm {
|
|
|
|
opcode: Opcode::Iconst,
|
|
|
|
imm,
|
|
|
|
} = func.dfg[inst] {
|
|
|
|
Some(imm.into())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn resolve_128bit_value_imm(func: &Function, val: Value) -> Option<u128> {
|
|
|
|
let (lsb, msb) = if let ValueDef::Result(inst, 0 /*param*/) = func.dfg.value_def(val) {
|
|
|
|
if let InstructionData::Binary {
|
|
|
|
opcode: Opcode::Iconcat,
|
|
|
|
args: [lsb, msb],
|
|
|
|
} = func.dfg[inst] {
|
|
|
|
(lsb, msb)
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
|
|
|
|
let lsb = resolve_normal_value_imm(func, lsb)? as u64 as u128;
|
|
|
|
let msb = resolve_normal_value_imm(func, msb)? as u64 as u128;
|
|
|
|
|
|
|
|
Some(msb << 64 | lsb)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resolve_value_imm(func: &Function, val: Value) -> Option<u128> {
|
|
|
|
if func.dfg.value_type(val) == types::I128 {
|
|
|
|
resolve_128bit_value_imm(func, val)
|
|
|
|
} else {
|
|
|
|
resolve_normal_value_imm(func, val).map(|imm| imm as u64 as u128)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:25:16 -05:00
|
|
|
pub fn type_min_max_value(ty: Type, signed: bool) -> (i64, i64) {
|
|
|
|
assert!(ty.is_int());
|
|
|
|
let min = match (ty, signed) {
|
|
|
|
(types::I8 , false)
|
|
|
|
| (types::I16, false)
|
|
|
|
| (types::I32, false)
|
|
|
|
| (types::I64, false) => 0i64,
|
|
|
|
(types::I8, true) => i8::min_value() as i64,
|
|
|
|
(types::I16, true) => i16::min_value() as i64,
|
|
|
|
(types::I32, true) => i32::min_value() as i64,
|
|
|
|
(types::I64, true) => i64::min_value(),
|
|
|
|
(types::I128, _) => unimplemented!(),
|
2019-08-08 08:54:13 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
2019-08-12 10:25:16 -05:00
|
|
|
let max = match (ty, signed) {
|
|
|
|
(types::I8, false) => u8::max_value() as i64,
|
|
|
|
(types::I16, false) => u16::max_value() as i64,
|
|
|
|
(types::I32, false) => u32::max_value() as i64,
|
|
|
|
(types::I64, false) => u64::max_value() as i64,
|
|
|
|
(types::I8, true) => i8::max_value() as i64,
|
|
|
|
(types::I16, true) => i16::max_value() as i64,
|
|
|
|
(types::I32, true) => i32::max_value() as i64,
|
|
|
|
(types::I64, true) => i64::max_value(),
|
|
|
|
(types::I128, _) => unimplemented!(),
|
2019-08-08 08:54:13 -05:00
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
(min, max)
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:25:16 -05:00
|
|
|
pub fn type_sign(ty: Ty<'_>) -> bool {
|
|
|
|
match ty.sty {
|
|
|
|
ty::Ref(..) | ty::RawPtr(..) | ty::FnPtr(..) | ty::Char | ty::Uint(..) | ty::Bool => false,
|
|
|
|
ty::Int(..) => true,
|
|
|
|
ty::Float(..) => false, // `signed` is unused for floats
|
|
|
|
_ => panic!("{}", ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub struct FunctionCx<'clif, 'tcx, B: Backend + 'static> {
|
2018-12-18 11:28:02 -06:00
|
|
|
// FIXME use a reference to `CodegenCx` instead of `tcx`, `module` and `constants` and `caches`
|
2019-06-16 04:13:49 -05:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2019-08-18 09:52:07 -05:00
|
|
|
pub module: &'clif 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>,
|
2019-05-31 03:56:55 -05:00
|
|
|
pub mir: &'tcx Body<'tcx>,
|
2018-12-01 04:49:44 -06:00
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
pub bcx: FunctionBuilder<'clif>,
|
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,
|
2019-08-18 09:52:07 -05:00
|
|
|
pub constants_cx: &'clif mut crate::constant::ConstantCx,
|
|
|
|
pub caches: &'clif 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
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
impl<'tcx, B: Backend> LayoutOf for FunctionCx<'_, '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);
|
2019-08-01 07:58:27 -05:00
|
|
|
self.tcx.layout_of(ParamEnv::reveal_all().and(&ty))
|
|
|
|
.unwrap_or_else(|e| if let layout::LayoutError::SizeOverflow(_) = e {
|
|
|
|
self.tcx.sess.fatal(&e.to_string())
|
|
|
|
} else {
|
|
|
|
bug!("failed to get layout for `{}`: {}", ty, e)
|
|
|
|
})
|
2018-06-24 07:01:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
impl<'tcx, B: Backend + 'static> layout::HasTyCtxt<'tcx> for FunctionCx<'_, 'tcx, B> {
|
2019-06-16 04:13:49 -05:00
|
|
|
fn tcx<'b>(&'b self) -> TyCtxt<'tcx> {
|
2018-06-24 07:25:29 -05:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
impl<'tcx, B: Backend + 'static> layout::HasDataLayout for FunctionCx<'_, 'tcx, B> {
|
2018-06-24 07:25:29 -05:00
|
|
|
fn data_layout(&self) -> &layout::TargetDataLayout {
|
|
|
|
&self.tcx.data_layout
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
impl<'tcx, B: Backend + 'static> layout::HasParamEnv<'tcx> for FunctionCx<'_, 'tcx, B> {
|
2019-05-08 13:05:01 -05:00
|
|
|
fn param_env(&self) -> ParamEnv<'tcx> {
|
|
|
|
ParamEnv::reveal_all()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
impl<'tcx, B: Backend + 'static> HasTargetSpec for FunctionCx<'_, 'tcx, B> {
|
2018-06-24 07:25:29 -05:00
|
|
|
fn target_spec(&self) -> &Target {
|
|
|
|
&self.tcx.sess.target.target
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
impl<'tcx, B: Backend> BackendTypes for FunctionCx<'_, 'tcx, B> {
|
2018-11-21 09:01:33 -06:00
|
|
|
type Value = Value;
|
|
|
|
type BasicBlock = Ebb;
|
|
|
|
type Type = Type;
|
|
|
|
type Funclet = !;
|
|
|
|
type DIScope = !;
|
|
|
|
}
|
|
|
|
|
2019-08-18 09:52:07 -05:00
|
|
|
impl<'tcx, B: Backend + 'static> FunctionCx<'_, '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
|
|
|
}
|