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-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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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`
|
2019-06-16 04:13:49 -05:00
|
|
|
pub tcx: TyCtxt<'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>,
|
2019-05-31 03:56:55 -05:00
|
|
|
pub mir: &'tcx Body<'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-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> {
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 13:05:01 -05:00
|
|
|
impl<'a, 'tcx, B: Backend + 'a> layout::HasParamEnv<'tcx> for FunctionCx<'a, 'tcx, B> {
|
|
|
|
fn param_env(&self) -> ParamEnv<'tcx> {
|
|
|
|
ParamEnv::reveal_all()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|