2018-11-07 13:29:38 +01:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
2020-01-06 20:11:03 +01:00
|
|
|
use rustc_span::DUMMY_SP;
|
2020-01-04 17:49:00 +01:00
|
|
|
|
2020-03-14 14:39:29 +01:00
|
|
|
use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
2018-11-07 13:29:38 +01:00
|
|
|
use rustc::mir::interpret::{
|
2019-12-23 13:25:22 +01:00
|
|
|
read_target_uint, AllocId, Allocation, ConstValue, GlobalAlloc, InterpResult, Scalar,
|
2018-08-30 20:14:56 +02:00
|
|
|
};
|
2019-11-15 20:47:22 +01:00
|
|
|
use rustc::ty::{layout::Align, Const, ConstKind};
|
2020-03-14 16:36:55 +01:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2018-11-07 13:32:02 +01:00
|
|
|
use rustc_mir::interpret::{
|
2020-02-14 18:20:34 +01:00
|
|
|
ImmTy, InterpCx, Machine, Memory, MemoryKind, OpTy, PlaceTy, Pointer,
|
2020-03-17 14:52:06 +01:00
|
|
|
StackPopCleanup, StackPopJump,
|
2018-11-07 13:32:02 +01:00
|
|
|
};
|
2018-11-07 13:29:38 +01:00
|
|
|
|
2020-01-17 14:01:51 +01:00
|
|
|
use cranelift_codegen::ir::GlobalValue;
|
2018-11-07 13:29:38 +01:00
|
|
|
use cranelift_module::*;
|
|
|
|
|
|
|
|
use crate::prelude::*;
|
2018-08-13 12:13:43 +02:00
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct ConstantCx {
|
2020-03-14 16:36:55 +01:00
|
|
|
todo: Vec<TodoItem>,
|
|
|
|
done: FxHashSet<DataId>,
|
2018-08-13 12:13:43 +02:00
|
|
|
}
|
|
|
|
|
2020-03-14 16:36:55 +01:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-08-13 18:31:26 +02:00
|
|
|
enum TodoItem {
|
|
|
|
Alloc(AllocId),
|
|
|
|
Static(DefId),
|
|
|
|
}
|
|
|
|
|
2018-08-13 12:13:43 +02:00
|
|
|
impl ConstantCx {
|
2019-08-31 22:58:09 +05:30
|
|
|
pub fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut Module<impl Backend>) {
|
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-09-09 14:45:23 +02:00
|
|
|
self.done.clear();
|
2018-08-13 12:13:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 16:52:07 +02:00
|
|
|
pub fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
|
2020-03-14 16:36:55 +01:00
|
|
|
constants_cx.todo.push(TodoItem::Static(def_id));
|
2018-08-13 12:13:43 +02:00
|
|
|
}
|
|
|
|
|
2020-01-13 21:38:46 +01:00
|
|
|
fn codegen_static_ref<'tcx>(
|
2019-08-18 16:52:07 +02:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-03-27 17:45:20 +01:00
|
|
|
def_id: DefId,
|
2020-01-15 13:17:09 +01:00
|
|
|
layout: TyLayout<'tcx>,
|
2018-08-13 12:13:43 +02:00
|
|
|
) -> CPlace<'tcx> {
|
2019-03-27 17:45:20 +01:00
|
|
|
let linkage = crate::linkage::get_static_ref_linkage(fx.tcx, def_id);
|
|
|
|
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, linkage);
|
2020-01-17 14:01:51 +01:00
|
|
|
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 12:18:40 +01:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", def_id));
|
2020-01-17 14:01:51 +01:00
|
|
|
cplace_for_dataid(fx, layout, local_data_id)
|
2018-08-13 12:13:43 +02:00
|
|
|
}
|
2018-07-14 16:45:20 +02:00
|
|
|
|
2019-08-18 16:52:07 +02:00
|
|
|
pub fn trans_constant<'tcx>(
|
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2018-07-31 12:25:16 +02:00
|
|
|
constant: &Constant<'tcx>,
|
|
|
|
) -> CValue<'tcx> {
|
2020-01-13 21:38:46 +01:00
|
|
|
let const_ = match constant.literal.val {
|
|
|
|
ConstKind::Unevaluated(def_id, ref substs, promoted) if fx.tcx.is_static(def_id) => {
|
|
|
|
assert!(substs.is_empty());
|
|
|
|
assert!(promoted.is_none());
|
|
|
|
|
|
|
|
return codegen_static_ref(
|
|
|
|
fx,
|
|
|
|
def_id,
|
2020-01-15 13:17:09 +01:00
|
|
|
fx.layout_of(fx.monomorphize(&constant.literal.ty)),
|
2020-01-13 21:38:46 +01:00
|
|
|
).to_cvalue(fx);
|
|
|
|
}
|
2020-02-22 14:20:37 +01:00
|
|
|
_ => fx.monomorphize(&constant.literal).eval(fx.tcx, ParamEnv::reveal_all()),
|
2020-01-13 21:38:46 +01:00
|
|
|
};
|
|
|
|
|
2018-07-30 16:57:40 +02:00
|
|
|
trans_const_value(fx, const_)
|
|
|
|
}
|
|
|
|
|
2019-08-18 16:52:07 +02:00
|
|
|
pub fn trans_const_value<'tcx>(
|
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-07-13 11:07:07 +02:00
|
|
|
const_: &'tcx Const<'tcx>,
|
2018-07-31 12:25:16 +02:00
|
|
|
) -> CValue<'tcx> {
|
2018-07-18 13:46:56 +02:00
|
|
|
let ty = fx.monomorphize(&const_.ty);
|
|
|
|
let layout = fx.layout_of(ty);
|
2020-01-13 21:38:46 +01:00
|
|
|
|
|
|
|
if layout.is_zst() {
|
|
|
|
return CValue::by_ref(
|
|
|
|
crate::Pointer::const_addr(fx, i64::try_from(layout.align.pref.bytes()).unwrap()),
|
|
|
|
layout,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
let const_val = match const_.val {
|
|
|
|
ConstKind::Value(const_val) => const_val,
|
|
|
|
_ => unreachable!("Const {:?} should have been evaluated", const_),
|
|
|
|
};
|
|
|
|
|
|
|
|
match const_val {
|
|
|
|
ConstValue::Scalar(x) => {
|
2020-01-17 14:01:51 +01:00
|
|
|
if fx.clif_type(layout.ty).is_none() {
|
|
|
|
return trans_const_place(fx, const_).to_cvalue(fx);
|
|
|
|
}
|
2020-01-13 21:38:46 +01:00
|
|
|
|
2020-01-17 14:01:51 +01:00
|
|
|
match x {
|
|
|
|
Scalar::Raw { data, size } => {
|
|
|
|
assert_eq!(u64::from(size), layout.size.bytes());
|
|
|
|
return CValue::const_val(fx, layout, data);
|
2020-01-13 21:38:46 +01:00
|
|
|
}
|
2020-01-17 14:01:51 +01:00
|
|
|
Scalar::Ptr(ptr) => {
|
|
|
|
let alloc_kind = fx.tcx.alloc_map.lock().get(ptr.alloc_id);
|
|
|
|
let base_addr = match alloc_kind {
|
|
|
|
Some(GlobalAlloc::Memory(alloc)) => {
|
2020-03-14 16:36:55 +01:00
|
|
|
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
|
2020-01-17 14:01:51 +01:00
|
|
|
let data_id = data_id_for_alloc_id(fx.module, ptr.alloc_id, alloc.align);
|
|
|
|
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 12:18:40 +01:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id));
|
2020-01-17 14:01:51 +01:00
|
|
|
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
|
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Function(instance)) => {
|
|
|
|
let func_id = crate::abi::import_function(fx.tcx, fx.module, instance);
|
|
|
|
let local_func_id = fx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
|
|
|
|
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
|
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Static(def_id)) => {
|
|
|
|
assert!(fx.tcx.is_static(def_id));
|
|
|
|
let linkage = crate::linkage::get_static_ref_linkage(fx.tcx, def_id);
|
|
|
|
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, linkage);
|
|
|
|
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 12:18:40 +01:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", def_id));
|
2020-01-17 14:01:51 +01:00
|
|
|
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
|
|
|
|
}
|
|
|
|
None => bug!("missing allocation {:?}", ptr.alloc_id),
|
2020-01-13 21:38:46 +01:00
|
|
|
};
|
2020-01-17 14:01:51 +01:00
|
|
|
let val = fx.bcx.ins().iadd_imm(base_addr, i64::try_from(ptr.offset.bytes()).unwrap());
|
|
|
|
return CValue::by_val(val, layout);
|
2020-01-13 21:38:46 +01:00
|
|
|
}
|
|
|
|
}
|
2018-07-14 16:45:20 +02:00
|
|
|
}
|
2020-01-13 21:38:46 +01:00
|
|
|
ConstValue::ByRef { alloc, offset } => {
|
|
|
|
let alloc_id = fx.tcx.alloc_map.lock().create_memory_alloc(alloc);
|
2020-03-14 16:36:55 +01:00
|
|
|
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
|
2020-01-13 21:38:46 +01:00
|
|
|
let data_id = data_id_for_alloc_id(fx.module, alloc_id, alloc.align);
|
|
|
|
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
|
|
|
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
|
|
|
|
assert!(!layout.is_unsized(), "unsized ConstValue::ByRef not supported");
|
|
|
|
CValue::by_ref(
|
|
|
|
crate::pointer::Pointer::new(global_ptr)
|
|
|
|
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
|
|
|
|
layout,
|
2019-08-31 22:58:09 +05:30
|
|
|
)
|
2018-07-14 16:45:20 +02:00
|
|
|
}
|
2020-01-13 21:38:46 +01:00
|
|
|
ConstValue::Slice { data: _, start: _, end: _ } => {
|
|
|
|
trans_const_place(fx, const_).to_cvalue(fx)
|
2019-08-28 16:38:53 +02:00
|
|
|
}
|
2018-07-16 15:13:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-18 16:52:07 +02:00
|
|
|
fn trans_const_place<'tcx>(
|
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-07-13 11:07:07 +02:00
|
|
|
const_: &'tcx Const<'tcx>,
|
2018-07-31 12:25:16 +02:00
|
|
|
) -> CPlace<'tcx> {
|
2018-08-30 20:14:56 +02:00
|
|
|
// Adapted from https://github.com/rust-lang/rust/pull/53671/files#diff-e0b58bb6712edaa8595ad7237542c958L551
|
2019-06-09 15:16:45 +02:00
|
|
|
let result = || -> InterpResult<'tcx, &'tcx Allocation> {
|
2019-07-07 11:59:11 +02:00
|
|
|
let mut ecx = InterpCx::new(
|
2018-08-30 20:14:56 +02:00
|
|
|
fx.tcx.at(DUMMY_SP),
|
|
|
|
ty::ParamEnv::reveal_all(),
|
2018-09-24 19:31:46 +02:00
|
|
|
TransPlaceInterpreter,
|
2019-07-07 11:59:11 +02:00
|
|
|
(),
|
2018-08-30 20:14:56 +02:00
|
|
|
);
|
2018-11-24 12:47:53 +01:00
|
|
|
ecx.push_stack_frame(
|
|
|
|
fx.instance,
|
|
|
|
DUMMY_SP,
|
|
|
|
fx.mir,
|
|
|
|
None,
|
|
|
|
StackPopCleanup::None { cleanup: false },
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let op = ecx.eval_operand(
|
|
|
|
&Operand::Constant(Box::new(Constant {
|
|
|
|
span: DUMMY_SP,
|
|
|
|
user_ty: None,
|
2019-07-13 11:07:07 +02:00
|
|
|
literal: const_,
|
2018-11-24 12:47:53 +01:00
|
|
|
})),
|
|
|
|
None,
|
|
|
|
)?;
|
2018-12-24 14:50:18 +01:00
|
|
|
let ptr = ecx.allocate(op.layout, MemoryKind::Stack);
|
2018-08-30 20:14:56 +02:00
|
|
|
ecx.copy_op(op, ptr.into())?;
|
2019-08-31 22:58:09 +05:30
|
|
|
let alloc = ecx
|
2019-10-18 18:31:26 +02:00
|
|
|
.memory
|
2019-12-28 10:57:49 +01:00
|
|
|
.get_raw(ptr.to_ref().to_scalar()?.assert_ptr().alloc_id)?;
|
2018-08-30 20:14:56 +02:00
|
|
|
Ok(fx.tcx.intern_const_alloc(alloc.clone()))
|
|
|
|
};
|
2019-11-15 20:47:22 +01:00
|
|
|
let alloc = result().expect("unable to convert ConstKind to Allocation");
|
2018-08-30 20:14:56 +02:00
|
|
|
|
2018-07-30 16:57:40 +02:00
|
|
|
//println!("const value: {:?} allocation: {:?}", value, alloc);
|
2019-06-02 12:06:02 +02:00
|
|
|
let alloc_id = fx.tcx.alloc_map.lock().create_memory_alloc(alloc);
|
2020-03-14 16:36:55 +01:00
|
|
|
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
|
2019-03-30 18:22:43 +01:00
|
|
|
let data_id = data_id_for_alloc_id(fx.module, alloc_id, alloc.align);
|
2020-01-17 14:01:51 +01:00
|
|
|
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 12:18:40 +01:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
|
2020-01-17 14:01:51 +01:00
|
|
|
cplace_for_dataid(fx, fx.layout_of(const_.ty), local_data_id)
|
2018-07-30 16:57:40 +02:00
|
|
|
}
|
|
|
|
|
2019-08-31 22:58:09 +05:30
|
|
|
fn data_id_for_alloc_id<B: Backend>(
|
|
|
|
module: &mut Module<B>,
|
|
|
|
alloc_id: AllocId,
|
|
|
|
align: Align,
|
|
|
|
) -> DataId {
|
2018-08-13 16:58:07 +02:00
|
|
|
module
|
2019-08-31 22:58:09 +05:30
|
|
|
.declare_data(
|
|
|
|
&format!("__alloc_{}", alloc_id.0),
|
|
|
|
Linkage::Local,
|
|
|
|
false,
|
2019-10-27 16:55:35 +01:00
|
|
|
false,
|
2019-08-31 22:58:09 +05:30
|
|
|
Some(align.bytes() as u8),
|
|
|
|
)
|
2018-08-13 16:58:07 +02:00
|
|
|
.unwrap()
|
2018-07-16 15:13:37 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 11:13:49 +02:00
|
|
|
fn data_id_for_static(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
module: &mut Module<impl Backend>,
|
2018-08-22 12:31:45 +02:00
|
|
|
def_id: DefId,
|
2018-11-16 20:37:45 +01:00
|
|
|
linkage: Linkage,
|
2018-08-22 12:31:45 +02:00
|
|
|
) -> DataId {
|
2019-08-17 12:31:10 +02:00
|
|
|
let instance = Instance::mono(tcx, def_id);
|
2019-09-07 10:51:00 +02:00
|
|
|
let symbol_name = tcx.symbol_name(instance).name.as_str();
|
2020-01-09 17:43:10 +01:00
|
|
|
let ty = instance.monomorphic_ty(tcx);
|
2019-04-24 16:35:00 +02:00
|
|
|
let is_mutable = if tcx.is_mutable_static(def_id) {
|
2018-11-09 17:54:28 +01:00
|
|
|
true
|
|
|
|
} else {
|
2019-05-04 16:15:20 +02:00
|
|
|
!ty.is_freeze(tcx, ParamEnv::reveal_all(), DUMMY_SP)
|
2018-11-09 17:54:28 +01:00
|
|
|
};
|
2019-08-31 22:58:09 +05:30
|
|
|
let align = tcx
|
|
|
|
.layout_of(ParamEnv::reveal_all().and(ty))
|
|
|
|
.unwrap()
|
|
|
|
.align
|
|
|
|
.pref
|
|
|
|
.bytes();
|
2019-03-11 20:36:29 +01:00
|
|
|
|
2019-10-27 16:55:35 +01:00
|
|
|
let attrs = tcx.codegen_fn_attrs(def_id);
|
|
|
|
|
2019-03-11 20:36:29 +01:00
|
|
|
let data_id = module
|
2019-08-31 22:58:09 +05:30
|
|
|
.declare_data(
|
|
|
|
&*symbol_name,
|
|
|
|
linkage,
|
|
|
|
is_mutable,
|
2020-03-14 14:39:29 +01:00
|
|
|
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
|
2019-08-31 22:58:09 +05:30
|
|
|
Some(align.try_into().unwrap()),
|
|
|
|
)
|
2019-03-11 20:36:29 +01:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
if linkage == Linkage::Preemptible {
|
2019-09-28 11:13:40 +02:00
|
|
|
if let ty::RawPtr(_) = ty.kind {
|
2019-03-11 20:36:29 +01:00
|
|
|
} else {
|
2019-08-31 22:58:09 +05:30
|
|
|
tcx.sess.span_fatal(
|
|
|
|
tcx.def_span(def_id),
|
|
|
|
"must have type `*const T` or `*mut T` due to `#[linkage]` attribute",
|
|
|
|
)
|
2019-03-11 20:36:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut data_ctx = DataContext::new();
|
2020-03-07 11:27:49 +01:00
|
|
|
data_ctx.define_zeroinit(pointer_ty(tcx).bytes() as usize);
|
2019-03-11 20:36:29 +01:00
|
|
|
match module.define_data(data_id, &data_ctx) {
|
|
|
|
// Everytime a weak static is referenced, there will be a zero pointer definition,
|
|
|
|
// so duplicate definitions are expected and allowed.
|
|
|
|
Err(ModuleError::DuplicateDefinition(_)) => {}
|
|
|
|
res => res.unwrap(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data_id
|
2018-08-13 18:31:26 +02:00
|
|
|
}
|
|
|
|
|
2019-08-18 16:52:07 +02:00
|
|
|
fn cplace_for_dataid<'tcx>(
|
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2020-01-15 13:17:09 +01:00
|
|
|
layout: TyLayout<'tcx>,
|
2020-01-17 14:01:51 +01:00
|
|
|
local_data_id: GlobalValue,
|
2018-08-13 18:31:26 +02:00
|
|
|
) -> CPlace<'tcx> {
|
2018-11-07 13:32:02 +01:00
|
|
|
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
|
2018-08-22 15:38:10 +02:00
|
|
|
assert!(!layout.is_unsized(), "unsized statics aren't supported");
|
2019-12-20 16:02:47 +01:00
|
|
|
CPlace::for_ptr(crate::pointer::Pointer::new(global_ptr), layout)
|
2018-08-13 16:58:07 +02:00
|
|
|
}
|
2018-07-16 15:13:37 +02:00
|
|
|
|
2019-08-31 22:58:09 +05:30
|
|
|
fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut Module<impl Backend>, cx: &mut ConstantCx) {
|
2019-07-07 11:59:11 +02:00
|
|
|
let memory = Memory::<TransPlaceInterpreter>::new(tcx.at(DUMMY_SP), ());
|
2018-08-13 12:13:43 +02:00
|
|
|
|
2020-03-14 16:36:55 +01:00
|
|
|
while let Some(todo_item) = cx.todo.pop() {
|
2018-08-13 18:31:26 +02:00
|
|
|
let (data_id, alloc) = match todo_item {
|
|
|
|
TodoItem::Alloc(alloc_id) => {
|
2018-08-15 15:28:08 +02:00
|
|
|
//println!("alloc_id {}", alloc_id);
|
2019-11-09 11:14:18 +01:00
|
|
|
let alloc = memory.get_raw(alloc_id).unwrap();
|
2019-03-30 18:22:43 +01:00
|
|
|
let data_id = data_id_for_alloc_id(module, alloc_id, alloc.align);
|
2018-08-13 18:31:26 +02:00
|
|
|
(data_id, alloc)
|
|
|
|
}
|
|
|
|
TodoItem::Static(def_id) => {
|
2018-08-15 15:28:08 +02:00
|
|
|
//println!("static {:?}", def_id);
|
2019-03-11 20:36:29 +01:00
|
|
|
|
|
|
|
if tcx.is_foreign_item(def_id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-12-23 13:25:22 +01:00
|
|
|
let const_ = tcx.const_eval_poly(def_id).unwrap();
|
2018-08-13 18:31:26 +02:00
|
|
|
|
2020-02-22 14:20:37 +01:00
|
|
|
let alloc = match const_ {
|
|
|
|
ConstValue::ByRef { alloc, offset } if offset.bytes() == 0 => alloc,
|
2018-08-13 18:31:26 +02:00
|
|
|
_ => bug!("static const eval returned {:#?}", const_),
|
|
|
|
};
|
|
|
|
|
2019-08-31 22:58:09 +05:30
|
|
|
let data_id = data_id_for_static(
|
|
|
|
tcx,
|
|
|
|
module,
|
|
|
|
def_id,
|
|
|
|
if tcx.is_reachable_non_generic(def_id) {
|
|
|
|
Linkage::Export
|
|
|
|
} else {
|
2020-03-09 11:21:40 +01:00
|
|
|
Linkage::Export // FIXME Set hidden visibility
|
2019-08-31 22:58:09 +05:30
|
|
|
},
|
|
|
|
);
|
2018-08-13 18:31:26 +02:00
|
|
|
(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();
|
|
|
|
|
2020-01-04 12:57:38 +01:00
|
|
|
let bytes = alloc.inspect_with_undef_and_ptr_outside_interpreter(0..alloc.len()).to_vec();
|
2019-08-07 15:26:29 +02:00
|
|
|
data_ctx.define(bytes.into_boxed_slice());
|
2018-07-16 15:13:37 +02:00
|
|
|
|
2019-09-07 10:51:00 +02:00
|
|
|
for &(offset, (_tag, reloc)) in alloc.relocations().iter() {
|
2019-01-06 17:51:21 +01:00
|
|
|
let addend = {
|
2018-09-16 16:21:07 +02:00
|
|
|
let endianness = tcx.data_layout.endian;
|
|
|
|
let offset = offset.bytes() as usize;
|
|
|
|
let ptr_size = tcx.data_layout.pointer_size;
|
2019-09-07 10:51:00 +02:00
|
|
|
let bytes = &alloc.inspect_with_undef_and_ptr_outside_interpreter(offset..offset + ptr_size.bytes() as usize);
|
2018-09-16 16:21:07 +02:00
|
|
|
read_target_uint(endianness, bytes).unwrap()
|
|
|
|
};
|
|
|
|
|
2019-08-19 15:36:03 +02:00
|
|
|
// Don't inline `reloc_target_alloc` into the match. That would cause `tcx.alloc_map`
|
|
|
|
// to be locked for the duration of the match. `data_id_for_static` however may try
|
|
|
|
// to lock `tcx.alloc_map` itself while calculating the layout of the target static.
|
|
|
|
// This would cause a panic in single threaded rustc and a deadlock for parallel rustc.
|
2019-08-17 12:31:10 +02:00
|
|
|
let reloc_target_alloc = tcx.alloc_map.lock().get(reloc).unwrap();
|
|
|
|
let data_id = match reloc_target_alloc {
|
2019-06-02 12:06:02 +02:00
|
|
|
GlobalAlloc::Function(instance) => {
|
2019-01-06 17:51:21 +01:00
|
|
|
assert_eq!(addend, 0);
|
2019-01-02 12:20:32 +01:00
|
|
|
let func_id = crate::abi::import_function(tcx, module, instance);
|
2018-09-16 16:21:07 +02:00
|
|
|
let local_func_id = module.declare_func_in_data(func_id, &mut data_ctx);
|
2019-01-06 17:51:21 +01:00
|
|
|
data_ctx.write_function_addr(offset.bytes() as u32, local_func_id);
|
2018-09-16 16:21:07 +02:00
|
|
|
continue;
|
2018-09-26 15:40:11 +02:00
|
|
|
}
|
2019-06-02 12:06:02 +02:00
|
|
|
GlobalAlloc::Memory(_) => {
|
2020-03-14 16:36:55 +01:00
|
|
|
cx.todo.push(TodoItem::Alloc(reloc));
|
2019-03-30 18:22:43 +01:00
|
|
|
data_id_for_alloc_id(module, reloc, alloc.align)
|
2018-08-13 19:02:42 +02:00
|
|
|
}
|
2019-06-02 12:06:02 +02:00
|
|
|
GlobalAlloc::Static(def_id) => {
|
2020-03-14 14:39:29 +01:00
|
|
|
if tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
|
|
|
|
tcx.sess.fatal(&format!("Allocation {:?} contains reference to TLS value {:?}", alloc, def_id));
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:36:03 +02:00
|
|
|
// Don't push a `TodoItem::Static` here, as it will cause statics used by
|
|
|
|
// multiple crates to be duplicated between them. It isn't necessary anyway,
|
|
|
|
// as it will get pushed by `codegen_static` when necessary.
|
2019-11-24 16:30:15 +01:00
|
|
|
data_id_for_static(
|
|
|
|
tcx,
|
|
|
|
module,
|
|
|
|
def_id,
|
|
|
|
crate::linkage::get_static_ref_linkage(tcx, def_id),
|
|
|
|
)
|
2018-08-13 19:02:42 +02:00
|
|
|
}
|
|
|
|
};
|
2018-07-16 15:13:37 +02:00
|
|
|
|
2018-08-13 12:13:43 +02:00
|
|
|
let global_value = module.declare_data_in_data(data_id, &mut data_ctx);
|
2019-01-06 17:51:21 +01:00
|
|
|
data_ctx.write_data_addr(offset.bytes() as u32, global_value, addend as i64);
|
2018-07-16 15:13:37 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-09-24 19:31:46 +02:00
|
|
|
struct TransPlaceInterpreter;
|
|
|
|
|
2019-06-13 20:44:40 +02:00
|
|
|
impl<'mir, 'tcx> Machine<'mir, 'tcx> for TransPlaceInterpreter {
|
2018-10-22 11:06:09 +02:00
|
|
|
type MemoryKinds = !;
|
2019-07-07 11:59:11 +02:00
|
|
|
type ExtraFnVal = !;
|
2018-10-12 19:23:24 +02:00
|
|
|
type PointerTag = ();
|
2018-10-22 11:06:09 +02:00
|
|
|
type AllocExtra = ();
|
2018-11-30 18:08:08 +01:00
|
|
|
type MemoryExtra = ();
|
|
|
|
type FrameExtra = ();
|
2018-10-22 11:06:09 +02:00
|
|
|
type MemoryMap = FxHashMap<AllocId, (MemoryKind<!>, Allocation<()>)>;
|
2019-07-07 11:59:11 +02:00
|
|
|
|
2019-08-07 12:35:49 +02:00
|
|
|
const CHECK_ALIGN: bool = true;
|
2018-10-22 11:06:09 +02:00
|
|
|
const STATIC_KIND: Option<!> = None;
|
2018-10-16 14:16:37 +02:00
|
|
|
|
2019-07-07 11:59:11 +02:00
|
|
|
fn enforce_validity(_: &InterpCx<'mir, 'tcx, Self>) -> bool {
|
2018-10-16 14:16:37 +02:00
|
|
|
false
|
|
|
|
}
|
2018-09-24 19:31:46 +02:00
|
|
|
|
2019-07-07 11:59:11 +02:00
|
|
|
fn before_terminator(_: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
|
2018-09-24 19:31:46 +02:00
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
2019-12-05 21:00:57 +01:00
|
|
|
fn find_mir_or_eval_fn(
|
2019-07-07 11:59:11 +02:00
|
|
|
_: &mut InterpCx<'mir, 'tcx, Self>,
|
2020-01-06 20:11:03 +01:00
|
|
|
_: Span,
|
2018-09-24 19:31:46 +02:00
|
|
|
_: Instance<'tcx>,
|
|
|
|
_: &[OpTy<'tcx>],
|
2019-11-28 21:35:03 +01:00
|
|
|
_: Option<(PlaceTy<'tcx>, BasicBlock)>,
|
2019-11-14 21:13:40 +01:00
|
|
|
_: Option<BasicBlock>,
|
2019-06-09 15:16:45 +02:00
|
|
|
) -> InterpResult<'tcx, Option<&'mir Body<'tcx>>> {
|
2018-09-24 19:31:46 +02:00
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_intrinsic(
|
2019-07-07 11:59:11 +02:00
|
|
|
_: &mut InterpCx<'mir, 'tcx, Self>,
|
2019-11-01 20:22:59 +01:00
|
|
|
_: Span,
|
2018-09-24 19:31:46 +02:00
|
|
|
_: Instance<'tcx>,
|
|
|
|
_: &[OpTy<'tcx>],
|
2019-11-28 21:35:03 +01:00
|
|
|
_: Option<(PlaceTy<'tcx>, BasicBlock)>,
|
2019-11-14 21:13:40 +01:00
|
|
|
_: Option<BasicBlock>,
|
2019-06-09 15:16:45 +02:00
|
|
|
) -> InterpResult<'tcx> {
|
2018-09-24 19:31:46 +02:00
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
2019-08-04 13:42:40 +02:00
|
|
|
fn binary_ptr_op(
|
2019-07-07 11:59:11 +02:00
|
|
|
_: &InterpCx<'mir, 'tcx, Self>,
|
2018-09-24 19:31:46 +02:00
|
|
|
_: mir::BinOp,
|
2019-02-16 12:35:45 +01:00
|
|
|
_: ImmTy<'tcx>,
|
|
|
|
_: ImmTy<'tcx>,
|
2019-08-18 11:30:13 +02:00
|
|
|
) -> InterpResult<'tcx, (Scalar, bool, Ty<'tcx>)> {
|
2018-09-24 19:31:46 +02:00
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
2019-08-31 22:58:09 +05:30
|
|
|
fn ptr_to_int(_: &Memory<'mir, 'tcx, Self>, _: Pointer<()>) -> InterpResult<'tcx, u64> {
|
2019-08-04 13:42:40 +02:00
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
|
2019-07-07 11:59:11 +02:00
|
|
|
fn box_alloc(_: &mut InterpCx<'mir, 'tcx, Self>, _: PlaceTy<'tcx>) -> InterpResult<'tcx> {
|
2018-09-24 19:31:46 +02:00
|
|
|
panic!();
|
|
|
|
}
|
2018-10-22 11:06:09 +02:00
|
|
|
|
2019-12-05 21:00:57 +01:00
|
|
|
fn init_allocation_extra<'b>(
|
2019-07-07 11:59:11 +02:00
|
|
|
_: &(),
|
2019-06-06 20:31:09 +02:00
|
|
|
_: AllocId,
|
|
|
|
alloc: Cow<'b, Allocation>,
|
|
|
|
_: Option<MemoryKind<!>>,
|
|
|
|
) -> (Cow<'b, Allocation<(), ()>>, ()) {
|
|
|
|
(alloc, ())
|
2018-10-30 17:58:42 +01:00
|
|
|
}
|
|
|
|
|
2019-07-07 11:59:11 +02:00
|
|
|
fn tag_static_base_pointer(_: &(), _: AllocId) -> Self::PointerTag {
|
2019-06-06 20:31:09 +02:00
|
|
|
()
|
2018-10-30 17:58:42 +01:00
|
|
|
}
|
2018-11-30 18:08:08 +01:00
|
|
|
|
2019-07-07 11:59:11 +02:00
|
|
|
fn call_extra_fn(
|
|
|
|
_: &mut InterpCx<'mir, 'tcx, Self>,
|
|
|
|
_: !,
|
|
|
|
_: &[OpTy<'tcx, ()>],
|
2019-11-28 21:35:03 +01:00
|
|
|
_: Option<(PlaceTy<'tcx, ()>, BasicBlock)>,
|
2019-07-07 11:59:11 +02:00
|
|
|
_: Option<BasicBlock>,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stack_push(_: &mut InterpCx<'mir, 'tcx, Self>) -> InterpResult<'tcx> {
|
2018-11-30 18:08:08 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-03-17 14:52:06 +01:00
|
|
|
fn stack_pop(_: &mut InterpCx<'mir, 'tcx, Self>, _: (), _: bool) -> InterpResult<'tcx, StackPopJump> {
|
|
|
|
Ok(StackPopJump::Normal)
|
2018-11-30 18:08:08 +01:00
|
|
|
}
|
2019-12-05 21:00:57 +01:00
|
|
|
|
|
|
|
fn assert_panic(
|
|
|
|
_: &mut InterpCx<'mir, 'tcx, Self>,
|
2020-02-14 18:20:34 +01:00
|
|
|
_: &mir::AssertKind<Operand<'tcx>>,
|
2019-12-05 21:00:57 +01:00
|
|
|
_: Option<BasicBlock>,
|
|
|
|
) -> InterpResult<'tcx> {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2018-09-24 19:31:46 +02:00
|
|
|
}
|
2019-07-30 14:37:20 +02:00
|
|
|
|
|
|
|
pub fn mir_operand_get_const_val<'tcx>(
|
|
|
|
fx: &FunctionCx<'_, 'tcx, impl Backend>,
|
|
|
|
operand: &Operand<'tcx>,
|
2019-08-16 16:04:50 +02:00
|
|
|
) -> Option<&'tcx Const<'tcx>> {
|
2020-01-13 21:38:46 +01:00
|
|
|
match operand {
|
2020-02-22 14:20:37 +01:00
|
|
|
Operand::Copy(_) | Operand::Move(_) => None,
|
|
|
|
Operand::Constant(const_) => {
|
|
|
|
Some(fx.monomorphize(&const_.literal).eval(fx.tcx, ParamEnv::reveal_all()))
|
|
|
|
}
|
2020-01-13 21:38:46 +01:00
|
|
|
}
|
2019-07-30 14:37:20 +02:00
|
|
|
}
|