2018-07-31 12:25:16 +02:00
|
|
|
use cranelift_module::*;
|
2018-07-30 16:57:40 +02:00
|
|
|
use crate::prelude::*;
|
2018-08-13 19:02:42 +02:00
|
|
|
use rustc::mir::interpret::{read_target_uint, AllocId, AllocType, ConstValue, GlobalId};
|
2018-07-30 16:57:40 +02:00
|
|
|
use rustc::ty::Const;
|
2018-08-13 12:13:43 +02:00
|
|
|
use rustc_mir::interpret::{CompileTimeEvaluator, Memory};
|
2018-08-13 19:03:30 +02:00
|
|
|
use syntax::ast::Mutability as AstMutability;
|
2018-08-13 12:13:43 +02:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ConstantCx {
|
2018-08-13 18:31:26 +02:00
|
|
|
todo: HashSet<TodoItem>,
|
2018-08-13 12:13:43 +02:00
|
|
|
done: HashSet<DataId>,
|
|
|
|
}
|
|
|
|
|
2018-08-13 18:31:26 +02:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
|
|
|
|
enum TodoItem {
|
|
|
|
Alloc(AllocId),
|
|
|
|
Static(DefId),
|
|
|
|
}
|
|
|
|
|
2018-08-13 12:13:43 +02:00
|
|
|
impl ConstantCx {
|
2018-08-13 18:04:39 +02:00
|
|
|
pub fn finalize<'a, 'tcx: 'a, B: Backend>(
|
|
|
|
mut self,
|
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
module: &mut Module<B>,
|
|
|
|
) {
|
2018-08-15 15:28:08 +02:00
|
|
|
//println!("todo {:?}", self.todo);
|
2018-08-13 16:58:07 +02:00
|
|
|
define_all_allocs(tcx, module, &mut self);
|
2018-08-15 15:28:08 +02:00
|
|
|
//println!("done {:?}", self.done);
|
2018-08-13 12:13:43 +02:00
|
|
|
for data_id in self.done.drain() {
|
|
|
|
module.finalize_data(data_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-14 18:52:43 +02:00
|
|
|
pub fn codegen_static<'a, 'tcx: 'a>(ccx: &mut ConstantCx, def_id: DefId) {
|
|
|
|
ccx.todo.insert(TodoItem::Static(def_id));
|
2018-08-13 12:13:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn codegen_static_ref<'a, 'tcx: 'a>(
|
2018-08-14 20:31:16 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-08-13 12:13:43 +02:00
|
|
|
static_: &Static<'tcx>,
|
|
|
|
) -> CPlace<'tcx> {
|
2018-08-13 18:31:26 +02:00
|
|
|
let data_id = data_id_for_static(fx.tcx, fx.module, static_.def_id);
|
|
|
|
cplace_for_dataid(fx, static_.ty, data_id)
|
2018-08-13 12:13:43 +02:00
|
|
|
}
|
2018-07-14 16:45:20 +02:00
|
|
|
|
2018-07-31 12:25:16 +02:00
|
|
|
pub fn trans_promoted<'a, 'tcx: 'a>(
|
2018-08-14 20:31:16 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-31 12:25:16 +02:00
|
|
|
promoted: Promoted,
|
|
|
|
) -> CPlace<'tcx> {
|
2018-07-30 16:57:40 +02:00
|
|
|
let const_ = fx
|
|
|
|
.tcx
|
|
|
|
.const_eval(ParamEnv::reveal_all().and(GlobalId {
|
|
|
|
instance: fx.instance,
|
|
|
|
promoted: Some(promoted),
|
2018-07-31 12:25:16 +02:00
|
|
|
})).unwrap();
|
2018-07-21 18:44:34 +02:00
|
|
|
|
2018-07-30 16:57:40 +02:00
|
|
|
let const_ = force_eval_const(fx, const_);
|
|
|
|
trans_const_place(fx, const_)
|
|
|
|
}
|
|
|
|
|
2018-07-31 12:25:16 +02:00
|
|
|
pub fn trans_constant<'a, 'tcx: 'a>(
|
2018-08-14 20:31:16 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-31 12:25:16 +02:00
|
|
|
constant: &Constant<'tcx>,
|
|
|
|
) -> CValue<'tcx> {
|
2018-07-30 16:57:40 +02:00
|
|
|
let const_ = fx.monomorphize(&constant.literal);
|
|
|
|
let const_ = force_eval_const(fx, const_);
|
|
|
|
trans_const_value(fx, const_)
|
|
|
|
}
|
|
|
|
|
2018-07-31 12:25:16 +02:00
|
|
|
fn force_eval_const<'a, 'tcx: 'a>(
|
2018-08-14 20:31:16 +02:00
|
|
|
fx: &FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-31 12:25:16 +02:00
|
|
|
const_: &'tcx Const<'tcx>,
|
|
|
|
) -> &'tcx Const<'tcx> {
|
2018-07-30 16:57:40 +02:00
|
|
|
match const_.val {
|
2018-07-21 18:44:34 +02:00
|
|
|
ConstValue::Unevaluated(def_id, ref substs) => {
|
|
|
|
let param_env = ParamEnv::reveal_all();
|
|
|
|
let instance = Instance::resolve(fx.tcx, param_env, def_id, substs).unwrap();
|
|
|
|
let cid = GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: None,
|
|
|
|
};
|
|
|
|
fx.tcx.const_eval(param_env.and(cid)).unwrap()
|
2018-07-31 12:25:16 +02:00
|
|
|
}
|
2018-07-30 16:57:40 +02:00
|
|
|
_ => const_,
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 16:45:20 +02:00
|
|
|
|
2018-07-31 12:25:16 +02:00
|
|
|
fn trans_const_value<'a, 'tcx: 'a>(
|
2018-08-14 20:31:16 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-31 12:25:16 +02:00
|
|
|
const_: &'tcx Const<'tcx>,
|
|
|
|
) -> CValue<'tcx> {
|
2018-07-18 13:46:56 +02:00
|
|
|
let ty = fx.monomorphize(&const_.ty);
|
|
|
|
let layout = fx.layout_of(ty);
|
|
|
|
match ty.sty {
|
2018-07-14 16:45:20 +02:00
|
|
|
TypeVariants::TyBool => {
|
2018-07-21 18:44:34 +02:00
|
|
|
let bits = const_.val.to_bits(layout.size).unwrap();
|
2018-07-18 13:46:56 +02:00
|
|
|
CValue::const_val(fx, ty, bits as u64 as i64)
|
2018-07-14 16:45:20 +02:00
|
|
|
}
|
|
|
|
TypeVariants::TyUint(_) => {
|
2018-07-21 18:44:34 +02:00
|
|
|
let bits = const_.val.to_bits(layout.size).unwrap();
|
2018-07-18 13:46:56 +02:00
|
|
|
CValue::const_val(fx, ty, bits as u64 as i64)
|
2018-07-14 16:45:20 +02:00
|
|
|
}
|
|
|
|
TypeVariants::TyInt(_) => {
|
2018-07-21 18:44:34 +02:00
|
|
|
let bits = const_.val.to_bits(layout.size).unwrap();
|
2018-07-18 13:46:56 +02:00
|
|
|
CValue::const_val(fx, ty, bits as i128 as i64)
|
2018-07-14 16:45:20 +02:00
|
|
|
}
|
|
|
|
TypeVariants::TyFnDef(def_id, substs) => {
|
2018-08-16 20:09:26 +02:00
|
|
|
let func_ref = fx.get_function_ref(
|
|
|
|
Instance::resolve(fx.tcx, ParamEnv::reveal_all(), def_id, substs).unwrap(),
|
|
|
|
);
|
2018-07-18 13:46:56 +02:00
|
|
|
CValue::Func(func_ref, layout)
|
2018-07-14 16:45:20 +02:00
|
|
|
}
|
2018-07-31 12:25:16 +02:00
|
|
|
_ => trans_const_place(fx, const_).to_cvalue(fx),
|
2018-07-16 15:13:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 12:25:16 +02:00
|
|
|
fn trans_const_place<'a, 'tcx: 'a>(
|
2018-08-14 20:31:16 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-07-31 12:25:16 +02:00
|
|
|
const_: &'tcx Const<'tcx>,
|
|
|
|
) -> CPlace<'tcx> {
|
2018-07-30 16:57:40 +02:00
|
|
|
let alloc = fx.tcx.const_value_to_allocation(const_);
|
|
|
|
//println!("const value: {:?} allocation: {:?}", value, alloc);
|
2018-08-13 12:13:43 +02:00
|
|
|
let alloc_id = fx.tcx.alloc_map.lock().allocate(alloc);
|
2018-08-13 18:31:26 +02:00
|
|
|
fx.constants.todo.insert(TodoItem::Alloc(alloc_id));
|
|
|
|
let data_id = data_id_for_alloc_id(fx.module, alloc_id);
|
|
|
|
cplace_for_dataid(fx, const_.ty, data_id)
|
2018-07-30 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2018-08-13 18:31:26 +02:00
|
|
|
fn data_id_for_alloc_id<B: Backend>(module: &mut Module<B>, alloc_id: AllocId) -> DataId {
|
2018-08-13 16:58:07 +02:00
|
|
|
module
|
|
|
|
.declare_data(&alloc_id.0.to_string(), Linkage::Local, false)
|
|
|
|
.unwrap()
|
2018-07-16 15:13:37 +02:00
|
|
|
}
|
|
|
|
|
2018-08-18 10:46:29 +02:00
|
|
|
fn data_id_for_static<'a, 'tcx: 'a, B: Backend>(tcx: TyCtxt<'a, 'tcx, 'tcx>, module: &mut Module<B>, def_id: DefId) -> DataId {
|
2018-08-13 18:31:26 +02:00
|
|
|
let symbol_name = tcx.symbol_name(Instance::mono(tcx, def_id)).as_str();
|
2018-08-18 10:46:29 +02:00
|
|
|
let is_mutable = if let ::rustc::hir::Mutability::MutMutable = tcx.is_static(def_id).unwrap() {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
!tcx.type_of(def_id).is_freeze(tcx, ParamEnv::reveal_all(), DUMMY_SP)
|
|
|
|
};
|
2018-08-13 18:31:26 +02:00
|
|
|
module
|
2018-08-18 10:46:29 +02:00
|
|
|
.declare_data(&*symbol_name, Linkage::Export, is_mutable)
|
2018-08-13 18:31:26 +02:00
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn cplace_for_dataid<'a, 'tcx: 'a>(
|
2018-08-14 20:31:16 +02:00
|
|
|
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
|
2018-08-13 18:31:26 +02:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
data_id: DataId,
|
|
|
|
) -> CPlace<'tcx> {
|
|
|
|
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
|
|
|
let global_ptr = fx.bcx.ins().global_value(types::I64, local_data_id);
|
|
|
|
let layout = fx.layout_of(fx.monomorphize(&ty));
|
|
|
|
CPlace::Addr(global_ptr, layout)
|
2018-08-13 16:58:07 +02:00
|
|
|
}
|
2018-07-16 15:13:37 +02:00
|
|
|
|
2018-08-13 18:04:39 +02:00
|
|
|
fn define_all_allocs<'a, 'tcx: 'a, B: Backend + 'a>(
|
2018-08-13 16:58:07 +02:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
|
|
|
module: &mut Module<B>,
|
|
|
|
cx: &mut ConstantCx,
|
|
|
|
) {
|
2018-08-13 12:13:43 +02:00
|
|
|
let memory = Memory::<CompileTimeEvaluator>::new(tcx.at(DUMMY_SP), ());
|
|
|
|
|
2018-08-13 18:31:26 +02:00
|
|
|
while let Some(todo_item) = pop_set(&mut cx.todo) {
|
|
|
|
let (data_id, alloc) = match todo_item {
|
|
|
|
TodoItem::Alloc(alloc_id) => {
|
2018-08-15 15:28:08 +02:00
|
|
|
//println!("alloc_id {}", alloc_id);
|
2018-08-13 18:31:26 +02:00
|
|
|
let data_id = data_id_for_alloc_id(module, alloc_id);
|
|
|
|
let alloc = memory.get(alloc_id).unwrap();
|
|
|
|
(data_id, alloc)
|
|
|
|
}
|
|
|
|
TodoItem::Static(def_id) => {
|
2018-08-15 15:28:08 +02:00
|
|
|
//println!("static {:?}", def_id);
|
2018-08-13 18:31:26 +02:00
|
|
|
let instance = ty::Instance::mono(tcx, def_id);
|
|
|
|
let cid = GlobalId {
|
|
|
|
instance,
|
|
|
|
promoted: None,
|
|
|
|
};
|
|
|
|
let const_ = tcx.const_eval(ParamEnv::reveal_all().and(cid)).unwrap();
|
|
|
|
|
|
|
|
let alloc = match const_.val {
|
|
|
|
ConstValue::ByRef(alloc, n) if n.bytes() == 0 => alloc,
|
|
|
|
_ => bug!("static const eval returned {:#?}", const_),
|
|
|
|
};
|
|
|
|
|
|
|
|
let data_id = data_id_for_static(tcx, module, def_id);
|
|
|
|
(data_id, alloc)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-15 15:28:08 +02:00
|
|
|
//("data_id {}", data_id);
|
2018-08-13 16:26:30 +02:00
|
|
|
if cx.done.contains(&data_id) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-16 15:13:37 +02:00
|
|
|
|
|
|
|
let mut data_ctx = DataContext::new();
|
|
|
|
|
2018-07-31 12:25:16 +02:00
|
|
|
data_ctx.define(
|
|
|
|
alloc.bytes.to_vec().into_boxed_slice(),
|
2018-08-13 19:02:42 +02:00
|
|
|
match alloc.runtime_mutability {
|
|
|
|
AstMutability::Mutable => Writability::Writable,
|
|
|
|
AstMutability::Immutable => Writability::Readonly,
|
|
|
|
},
|
2018-07-31 12:25:16 +02:00
|
|
|
);
|
2018-07-16 15:13:37 +02:00
|
|
|
|
|
|
|
for &(offset, reloc) in alloc.relocations.iter() {
|
2018-08-13 19:02:42 +02:00
|
|
|
let data_id = match tcx.alloc_map.lock().get(reloc).unwrap() {
|
|
|
|
AllocType::Memory(_) => {
|
|
|
|
cx.todo.insert(TodoItem::Alloc(reloc));
|
|
|
|
data_id_for_alloc_id(module, reloc)
|
|
|
|
}
|
|
|
|
AllocType::Function(_) => unimplemented!("function static reference"),
|
|
|
|
AllocType::Static(def_id) => {
|
|
|
|
cx.todo.insert(TodoItem::Static(def_id));
|
|
|
|
data_id_for_static(tcx, module, def_id)
|
|
|
|
}
|
|
|
|
};
|
2018-07-16 15:13:37 +02:00
|
|
|
|
|
|
|
let reloc_offset = {
|
|
|
|
let endianness = memory.endianness();
|
|
|
|
let offset = offset.bytes() as usize;
|
2018-08-13 12:13:43 +02:00
|
|
|
let ptr_size = tcx.data_layout.pointer_size;
|
2018-07-16 15:13:37 +02:00
|
|
|
let bytes = &alloc.bytes[offset..offset + ptr_size.bytes() as usize];
|
|
|
|
read_target_uint(endianness, bytes).unwrap()
|
|
|
|
};
|
|
|
|
|
2018-08-13 12:13:43 +02:00
|
|
|
let global_value = module.declare_data_in_data(data_id, &mut data_ctx);
|
2018-07-16 15:13:37 +02:00
|
|
|
data_ctx.write_data_addr(reloc_offset as u32, global_value, 0);
|
|
|
|
}
|
|
|
|
|
2018-08-13 12:13:43 +02:00
|
|
|
module.define_data(data_id, &data_ctx).unwrap();
|
|
|
|
cx.done.insert(data_id);
|
2018-07-14 16:45:20 +02:00
|
|
|
}
|
2018-08-13 16:58:07 +02:00
|
|
|
|
2018-08-13 18:31:26 +02:00
|
|
|
assert!(cx.todo.is_empty(), "{:?}", cx.todo);
|
2018-07-16 15:13:37 +02:00
|
|
|
}
|
2018-08-13 17:22:24 +02:00
|
|
|
|
|
|
|
fn pop_set<T: Copy + Eq + ::std::hash::Hash>(set: &mut HashSet<T>) -> Option<T> {
|
|
|
|
if let Some(elem) = set.iter().next().map(|elem| *elem) {
|
|
|
|
set.remove(&elem);
|
|
|
|
Some(elem)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-08-13 18:04:39 +02:00
|
|
|
}
|