2020-01-06 13:11:03 -06:00
|
|
|
use rustc_span::DUMMY_SP;
|
2020-01-04 10:49:00 -06:00
|
|
|
|
2020-03-31 06:20:19 -05:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
|
|
|
use rustc_middle::mir::interpret::{
|
2020-04-04 12:06:07 -05:00
|
|
|
read_target_uint, AllocId, Allocation, ConstValue, GlobalAlloc, Pointer, Scalar,
|
2018-08-30 13:14:56 -05:00
|
|
|
};
|
2020-04-03 04:54:18 -05:00
|
|
|
use rustc_middle::ty::{Const, ConstKind};
|
|
|
|
use rustc_target::abi::Align;
|
2020-03-14 10:36:55 -05:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2018-11-07 06:29:38 -06:00
|
|
|
|
2020-06-04 12:57:12 -05:00
|
|
|
use cranelift_codegen::ir::GlobalValueData;
|
2018-11-07 06:29:38 -06:00
|
|
|
use cranelift_module::*;
|
|
|
|
|
|
|
|
use crate::prelude::*;
|
2018-08-13 05:13:43 -05:00
|
|
|
|
|
|
|
#[derive(Default)]
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) struct ConstantCx {
|
2020-03-14 10:36:55 -05:00
|
|
|
todo: Vec<TodoItem>,
|
|
|
|
done: FxHashSet<DataId>,
|
2018-08-13 05:13:43 -05:00
|
|
|
}
|
|
|
|
|
2020-03-14 10:36:55 -05:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-08-13 11:31:26 -05:00
|
|
|
enum TodoItem {
|
|
|
|
Alloc(AllocId),
|
|
|
|
Static(DefId),
|
|
|
|
}
|
|
|
|
|
2018-08-13 05:13:43 -05:00
|
|
|
impl ConstantCx {
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut Module<impl Backend>) {
|
2018-08-15 08:28:08 -05:00
|
|
|
//println!("todo {:?}", self.todo);
|
2018-08-13 09:58:07 -05:00
|
|
|
define_all_allocs(tcx, module, &mut self);
|
2018-08-15 08:28:08 -05:00
|
|
|
//println!("done {:?}", self.done);
|
2018-09-09 07:45:23 -05:00
|
|
|
self.done.clear();
|
2018-08-13 05:13:43 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
|
2020-03-14 10:36:55 -05:00
|
|
|
constants_cx.todo.push(TodoItem::Static(def_id));
|
2018-08-13 05:13:43 -05:00
|
|
|
}
|
|
|
|
|
2020-06-04 12:57:12 -05:00
|
|
|
pub(crate) fn codegen_tls_ref<'tcx>(
|
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
|
|
|
def_id: DefId,
|
|
|
|
layout: TyAndLayout<'tcx>,
|
|
|
|
) -> CValue<'tcx> {
|
|
|
|
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)]
|
|
|
|
fx.add_comment(local_data_id, format!("tls {:?}", def_id));
|
|
|
|
let tls_ptr = fx.bcx.ins().tls_value(fx.pointer_type, local_data_id);
|
|
|
|
CValue::by_val(tls_ptr, layout)
|
|
|
|
}
|
|
|
|
|
2020-01-13 14:38:46 -06:00
|
|
|
fn codegen_static_ref<'tcx>(
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2019-03-27 11:45:20 -05:00
|
|
|
def_id: DefId,
|
2020-03-30 12:00:24 -05:00
|
|
|
layout: TyAndLayout<'tcx>,
|
2018-08-13 05:13:43 -05:00
|
|
|
) -> CPlace<'tcx> {
|
2019-03-27 11:45:20 -05: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 07:01:51 -06:00
|
|
|
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 06:18:40 -05:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", def_id));
|
2020-06-04 12:57:12 -05:00
|
|
|
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
|
|
|
|
assert!(!layout.is_unsized(), "unsized statics aren't supported");
|
|
|
|
assert!(matches!(fx.bcx.func.global_values[local_data_id], GlobalValueData::Symbol { tls: false, ..}), "tls static referenced without Rvalue::ThreadLocalRef");
|
|
|
|
CPlace::for_ptr(crate::pointer::Pointer::new(global_ptr), layout)
|
2018-08-13 05:13:43 -05:00
|
|
|
}
|
2018-07-14 09:45:20 -05:00
|
|
|
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn trans_constant<'tcx>(
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2018-07-31 05:25:16 -05:00
|
|
|
constant: &Constant<'tcx>,
|
|
|
|
) -> CValue<'tcx> {
|
2020-04-30 12:00:25 -05:00
|
|
|
let const_ = fx.monomorphize(&constant.literal);
|
|
|
|
let const_val = match const_.val {
|
|
|
|
ConstKind::Value(const_val) => const_val,
|
2020-07-17 12:15:33 -05:00
|
|
|
ConstKind::Unevaluated(def, ref substs, promoted) if fx.tcx.is_static(def.did) => {
|
2020-01-13 14:38:46 -06:00
|
|
|
assert!(substs.is_empty());
|
|
|
|
assert!(promoted.is_none());
|
|
|
|
|
|
|
|
return codegen_static_ref(
|
|
|
|
fx,
|
2020-07-17 12:15:33 -05:00
|
|
|
def.did,
|
2020-01-15 06:17:09 -06:00
|
|
|
fx.layout_of(fx.monomorphize(&constant.literal.ty)),
|
2020-01-13 14:38:46 -06:00
|
|
|
).to_cvalue(fx);
|
|
|
|
}
|
2020-07-17 12:15:33 -05:00
|
|
|
ConstKind::Unevaluated(def, ref substs, promoted) => {
|
|
|
|
match fx.tcx.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None) {
|
2020-04-30 12:00:25 -05:00
|
|
|
Ok(const_val) => const_val,
|
|
|
|
Err(_) => {
|
|
|
|
if promoted.is_none() {
|
|
|
|
fx.tcx.sess.span_err(constant.span, "erroneous constant encountered");
|
|
|
|
}
|
|
|
|
return crate::trap::trap_unreachable_ret_value(
|
|
|
|
fx,
|
|
|
|
fx.layout_of(const_.ty),
|
|
|
|
"erroneous constant encountered",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ConstKind::Param(_) | ConstKind::Infer(_) | ConstKind::Bound(_, _)
|
|
|
|
| ConstKind::Placeholder(_) | ConstKind::Error(_) => unreachable!("{:?}", const_),
|
2020-01-13 14:38:46 -06:00
|
|
|
};
|
|
|
|
|
2020-04-30 12:00:25 -05:00
|
|
|
trans_const_value(fx, const_val, const_.ty)
|
2018-07-30 09:57:40 -05:00
|
|
|
}
|
|
|
|
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn trans_const_value<'tcx>(
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2020-04-30 12:00:25 -05:00
|
|
|
const_val: ConstValue<'tcx>,
|
|
|
|
ty: Ty<'tcx>
|
2018-07-31 05:25:16 -05:00
|
|
|
) -> CValue<'tcx> {
|
2018-07-18 06:46:56 -05:00
|
|
|
let layout = fx.layout_of(ty);
|
2020-04-04 12:06:07 -05:00
|
|
|
assert!(!layout.is_unsized(), "sized const value");
|
2020-01-13 14:38:46 -06:00
|
|
|
|
|
|
|
if layout.is_zst() {
|
|
|
|
return CValue::by_ref(
|
|
|
|
crate::Pointer::const_addr(fx, i64::try_from(layout.align.pref.bytes()).unwrap()),
|
|
|
|
layout,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
match const_val {
|
|
|
|
ConstValue::Scalar(x) => {
|
2020-01-17 07:01:51 -06:00
|
|
|
if fx.clif_type(layout.ty).is_none() {
|
2020-04-04 12:06:07 -05:00
|
|
|
let (size, align) = (layout.size, layout.align.pref);
|
|
|
|
let mut alloc = Allocation::from_bytes(
|
|
|
|
std::iter::repeat(0).take(size.bytes_usize()).collect::<Vec<u8>>(),
|
|
|
|
align,
|
|
|
|
);
|
|
|
|
let ptr = Pointer::new(AllocId(!0), Size::ZERO); // The alloc id is never used
|
|
|
|
alloc.write_scalar(fx, ptr, x.into(), size).unwrap();
|
|
|
|
let alloc = fx.tcx.intern_const_alloc(alloc);
|
|
|
|
return CValue::by_ref(pointer_for_allocation(fx, alloc), layout);
|
2020-01-17 07:01:51 -06:00
|
|
|
}
|
2020-01-13 14:38:46 -06:00
|
|
|
|
2020-01-17 07:01:51 -06: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 14:38:46 -06:00
|
|
|
}
|
2020-01-17 07:01:51 -06:00
|
|
|
Scalar::Ptr(ptr) => {
|
2020-05-18 04:35:23 -05:00
|
|
|
let alloc_kind = fx.tcx.get_global_alloc(ptr.alloc_id);
|
2020-01-17 07:01:51 -06:00
|
|
|
let base_addr = match alloc_kind {
|
|
|
|
Some(GlobalAlloc::Memory(alloc)) => {
|
2020-03-14 10:36:55 -05:00
|
|
|
fx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
|
2020-05-01 05:15:11 -05:00
|
|
|
let data_id = data_id_for_alloc_id(fx.module, ptr.alloc_id, alloc.align, alloc.mutability);
|
2020-01-17 07:01:51 -06:00
|
|
|
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 06:18:40 -05:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id));
|
2020-01-17 07:01:51 -06: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 06:18:40 -05:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", def_id));
|
2020-01-17 07:01:51 -06:00
|
|
|
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
|
|
|
|
}
|
|
|
|
None => bug!("missing allocation {:?}", ptr.alloc_id),
|
2020-01-13 14:38:46 -06:00
|
|
|
};
|
2020-01-17 07:01:51 -06: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 14:38:46 -06:00
|
|
|
}
|
|
|
|
}
|
2018-07-14 09:45:20 -05:00
|
|
|
}
|
2020-01-13 14:38:46 -06:00
|
|
|
ConstValue::ByRef { alloc, offset } => {
|
|
|
|
CValue::by_ref(
|
2020-04-04 12:06:07 -05:00
|
|
|
pointer_for_allocation(fx, alloc)
|
2020-01-13 14:38:46 -06:00
|
|
|
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
|
|
|
|
layout,
|
2019-08-31 12:28:09 -05:00
|
|
|
)
|
2018-07-14 09:45:20 -05:00
|
|
|
}
|
2020-04-04 12:06:07 -05:00
|
|
|
ConstValue::Slice { data, start, end } => {
|
|
|
|
let ptr = pointer_for_allocation(fx, data)
|
|
|
|
.offset_i64(fx, i64::try_from(start).unwrap())
|
|
|
|
.get_addr(fx);
|
|
|
|
let len = fx.bcx.ins().iconst(fx.pointer_type, i64::try_from(end.checked_sub(start).unwrap()).unwrap());
|
|
|
|
CValue::by_val_pair(ptr, len, layout)
|
2019-08-28 09:38:53 -05:00
|
|
|
}
|
2018-07-16 08:13:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 12:06:07 -05:00
|
|
|
fn pointer_for_allocation<'tcx>(
|
2019-08-18 09:52:07 -05:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Backend>,
|
2020-04-04 12:06:07 -05:00
|
|
|
alloc: &'tcx Allocation,
|
|
|
|
) -> crate::pointer::Pointer {
|
2020-05-18 04:35:23 -05:00
|
|
|
let alloc_id = fx.tcx.create_memory_alloc(alloc);
|
2020-03-14 10:36:55 -05:00
|
|
|
fx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
|
2020-05-01 05:15:11 -05:00
|
|
|
let data_id = data_id_for_alloc_id(fx.module, alloc_id, alloc.align, alloc.mutability);
|
2020-04-04 12:06:07 -05:00
|
|
|
|
2020-01-17 07:01:51 -06:00
|
|
|
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 06:18:40 -05:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
|
2020-04-04 12:06:07 -05:00
|
|
|
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
|
|
|
|
crate::pointer::Pointer::new(global_ptr)
|
2018-07-30 09:57:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-31 12:28:09 -05:00
|
|
|
fn data_id_for_alloc_id<B: Backend>(
|
|
|
|
module: &mut Module<B>,
|
|
|
|
alloc_id: AllocId,
|
|
|
|
align: Align,
|
2020-05-01 05:15:11 -05:00
|
|
|
mutability: rustc_hir::Mutability,
|
2019-08-31 12:28:09 -05:00
|
|
|
) -> DataId {
|
2018-08-13 09:58:07 -05:00
|
|
|
module
|
2019-08-31 12:28:09 -05:00
|
|
|
.declare_data(
|
|
|
|
&format!("__alloc_{}", alloc_id.0),
|
|
|
|
Linkage::Local,
|
2020-05-01 05:15:11 -05:00
|
|
|
mutability == rustc_hir::Mutability::Mut,
|
2019-10-27 10:55:35 -05:00
|
|
|
false,
|
2019-08-31 12:28:09 -05:00
|
|
|
Some(align.bytes() as u8),
|
|
|
|
)
|
2018-08-13 09:58:07 -05:00
|
|
|
.unwrap()
|
2018-07-16 08:13:37 -05:00
|
|
|
}
|
|
|
|
|
2019-06-16 04:13:49 -05:00
|
|
|
fn data_id_for_static(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
module: &mut Module<impl Backend>,
|
2018-08-22 05:31:45 -05:00
|
|
|
def_id: DefId,
|
2018-11-16 13:37:45 -06:00
|
|
|
linkage: Linkage,
|
2018-08-22 05:31:45 -05:00
|
|
|
) -> DataId {
|
2019-08-17 05:31:10 -05:00
|
|
|
let instance = Instance::mono(tcx, def_id);
|
2020-07-17 12:15:33 -05:00
|
|
|
let symbol_name = tcx.symbol_name(instance).name;
|
2020-01-09 10:43:10 -06:00
|
|
|
let ty = instance.monomorphic_ty(tcx);
|
2019-04-24 09:35:00 -05:00
|
|
|
let is_mutable = if tcx.is_mutable_static(def_id) {
|
2018-11-09 10:54:28 -06:00
|
|
|
true
|
|
|
|
} else {
|
2020-06-27 04:29:39 -05:00
|
|
|
!ty.is_freeze(tcx.at(DUMMY_SP), ParamEnv::reveal_all())
|
2018-11-09 10:54:28 -06:00
|
|
|
};
|
2019-08-31 12:28:09 -05:00
|
|
|
let align = tcx
|
|
|
|
.layout_of(ParamEnv::reveal_all().and(ty))
|
|
|
|
.unwrap()
|
|
|
|
.align
|
|
|
|
.pref
|
|
|
|
.bytes();
|
2019-03-11 14:36:29 -05:00
|
|
|
|
2019-10-27 10:55:35 -05:00
|
|
|
let attrs = tcx.codegen_fn_attrs(def_id);
|
|
|
|
|
2019-03-11 14:36:29 -05:00
|
|
|
let data_id = module
|
2019-08-31 12:28:09 -05:00
|
|
|
.declare_data(
|
|
|
|
&*symbol_name,
|
|
|
|
linkage,
|
|
|
|
is_mutable,
|
2020-03-14 08:39:29 -05:00
|
|
|
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
|
2019-08-31 12:28:09 -05:00
|
|
|
Some(align.try_into().unwrap()),
|
|
|
|
)
|
2019-03-11 14:36:29 -05:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
if linkage == Linkage::Preemptible {
|
2019-09-28 04:13:40 -05:00
|
|
|
if let ty::RawPtr(_) = ty.kind {
|
2019-03-11 14:36:29 -05:00
|
|
|
} else {
|
2019-08-31 12:28:09 -05:00
|
|
|
tcx.sess.span_fatal(
|
|
|
|
tcx.def_span(def_id),
|
|
|
|
"must have type `*const T` or `*mut T` due to `#[linkage]` attribute",
|
|
|
|
)
|
2019-03-11 14:36:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut data_ctx = DataContext::new();
|
2020-03-07 04:27:49 -06:00
|
|
|
data_ctx.define_zeroinit(pointer_ty(tcx).bytes() as usize);
|
2019-03-11 14:36:29 -05: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 11:31:26 -05:00
|
|
|
}
|
|
|
|
|
2019-08-31 12:28:09 -05:00
|
|
|
fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut Module<impl Backend>, cx: &mut ConstantCx) {
|
2020-03-14 10:36:55 -05:00
|
|
|
while let Some(todo_item) = cx.todo.pop() {
|
2020-06-20 05:01:24 -05:00
|
|
|
let (data_id, alloc, section_name) = match todo_item {
|
2018-08-13 11:31:26 -05:00
|
|
|
TodoItem::Alloc(alloc_id) => {
|
2018-08-15 08:28:08 -05:00
|
|
|
//println!("alloc_id {}", alloc_id);
|
2020-05-18 04:35:23 -05:00
|
|
|
let alloc = match tcx.get_global_alloc(alloc_id).unwrap() {
|
2020-04-04 12:06:07 -05:00
|
|
|
GlobalAlloc::Memory(alloc) => alloc,
|
|
|
|
GlobalAlloc::Function(_) | GlobalAlloc::Static(_) => unreachable!(),
|
|
|
|
};
|
2020-05-01 05:15:11 -05:00
|
|
|
let data_id = data_id_for_alloc_id(module, alloc_id, alloc.align, alloc.mutability);
|
2020-06-20 05:01:24 -05:00
|
|
|
(data_id, alloc, None)
|
2018-08-13 11:31:26 -05:00
|
|
|
}
|
|
|
|
TodoItem::Static(def_id) => {
|
2018-08-15 08:28:08 -05:00
|
|
|
//println!("static {:?}", def_id);
|
2019-03-11 14:36:29 -05:00
|
|
|
|
2020-06-20 05:01:24 -05:00
|
|
|
let section_name = tcx.codegen_fn_attrs(def_id).link_section.map(|s| s.as_str());
|
2019-03-11 14:36:29 -05:00
|
|
|
|
2019-12-23 06:25:22 -06:00
|
|
|
let const_ = tcx.const_eval_poly(def_id).unwrap();
|
2018-08-13 11:31:26 -05:00
|
|
|
|
2020-02-22 07:20:37 -06:00
|
|
|
let alloc = match const_ {
|
|
|
|
ConstValue::ByRef { alloc, offset } if offset.bytes() == 0 => alloc,
|
2018-08-13 11:31:26 -05:00
|
|
|
_ => bug!("static const eval returned {:#?}", const_),
|
|
|
|
};
|
|
|
|
|
2019-08-31 12:28:09 -05:00
|
|
|
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 05:21:40 -05:00
|
|
|
Linkage::Export // FIXME Set hidden visibility
|
2019-08-31 12:28:09 -05:00
|
|
|
},
|
|
|
|
);
|
2020-06-20 05:01:24 -05:00
|
|
|
(data_id, alloc, section_name)
|
2018-08-13 11:31:26 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-15 08:28:08 -05:00
|
|
|
//("data_id {}", data_id);
|
2018-08-13 09:26:30 -05:00
|
|
|
if cx.done.contains(&data_id) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-16 08:13:37 -05:00
|
|
|
|
|
|
|
let mut data_ctx = DataContext::new();
|
|
|
|
|
2020-06-20 05:01:24 -05:00
|
|
|
if let Some(section_name) = section_name {
|
|
|
|
// FIXME set correct segment for Mach-O files
|
|
|
|
data_ctx.set_segment_section("", &*section_name);
|
|
|
|
}
|
|
|
|
|
2020-01-04 05:57:38 -06:00
|
|
|
let bytes = alloc.inspect_with_undef_and_ptr_outside_interpreter(0..alloc.len()).to_vec();
|
2019-08-07 08:26:29 -05:00
|
|
|
data_ctx.define(bytes.into_boxed_slice());
|
2018-07-16 08:13:37 -05:00
|
|
|
|
2019-09-07 03:51:00 -05:00
|
|
|
for &(offset, (_tag, reloc)) in alloc.relocations().iter() {
|
2019-01-06 10:51:21 -06:00
|
|
|
let addend = {
|
2018-09-16 09:21:07 -05:00
|
|
|
let endianness = tcx.data_layout.endian;
|
|
|
|
let offset = offset.bytes() as usize;
|
|
|
|
let ptr_size = tcx.data_layout.pointer_size;
|
2019-09-07 03:51:00 -05:00
|
|
|
let bytes = &alloc.inspect_with_undef_and_ptr_outside_interpreter(offset..offset + ptr_size.bytes() as usize);
|
2018-09-16 09:21:07 -05:00
|
|
|
read_target_uint(endianness, bytes).unwrap()
|
|
|
|
};
|
|
|
|
|
2020-05-18 04:35:23 -05:00
|
|
|
let reloc_target_alloc = tcx.get_global_alloc(reloc).unwrap();
|
2019-08-17 05:31:10 -05:00
|
|
|
let data_id = match reloc_target_alloc {
|
2019-06-02 05:06:02 -05:00
|
|
|
GlobalAlloc::Function(instance) => {
|
2019-01-06 10:51:21 -06:00
|
|
|
assert_eq!(addend, 0);
|
2019-01-02 05:20:32 -06:00
|
|
|
let func_id = crate::abi::import_function(tcx, module, instance);
|
2018-09-16 09:21:07 -05:00
|
|
|
let local_func_id = module.declare_func_in_data(func_id, &mut data_ctx);
|
2019-01-06 10:51:21 -06:00
|
|
|
data_ctx.write_function_addr(offset.bytes() as u32, local_func_id);
|
2018-09-16 09:21:07 -05:00
|
|
|
continue;
|
2018-09-26 08:40:11 -05:00
|
|
|
}
|
2020-05-01 05:15:11 -05:00
|
|
|
GlobalAlloc::Memory(target_alloc) => {
|
2020-03-14 10:36:55 -05:00
|
|
|
cx.todo.push(TodoItem::Alloc(reloc));
|
2020-05-01 05:15:11 -05:00
|
|
|
data_id_for_alloc_id(module, reloc, target_alloc.align, target_alloc.mutability)
|
2018-08-13 12:02:42 -05:00
|
|
|
}
|
2019-06-02 05:06:02 -05:00
|
|
|
GlobalAlloc::Static(def_id) => {
|
2020-03-14 08:39:29 -05: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 08:36:03 -05: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 09:30:15 -06:00
|
|
|
data_id_for_static(
|
|
|
|
tcx,
|
|
|
|
module,
|
|
|
|
def_id,
|
|
|
|
crate::linkage::get_static_ref_linkage(tcx, def_id),
|
|
|
|
)
|
2018-08-13 12:02:42 -05:00
|
|
|
}
|
|
|
|
};
|
2018-07-16 08:13:37 -05:00
|
|
|
|
2018-08-13 05:13:43 -05:00
|
|
|
let global_value = module.declare_data_in_data(data_id, &mut data_ctx);
|
2019-01-06 10:51:21 -06:00
|
|
|
data_ctx.write_data_addr(offset.bytes() as u32, global_value, addend as i64);
|
2018-07-16 08:13:37 -05:00
|
|
|
}
|
|
|
|
|
2018-08-13 05:13:43 -05:00
|
|
|
module.define_data(data_id, &data_ctx).unwrap();
|
|
|
|
cx.done.insert(data_id);
|
2018-07-14 09:45:20 -05:00
|
|
|
}
|
2018-08-13 09:58:07 -05:00
|
|
|
|
2018-08-13 11:31:26 -05:00
|
|
|
assert!(cx.todo.is_empty(), "{:?}", cx.todo);
|
2018-07-16 08:13:37 -05:00
|
|
|
}
|
2018-08-13 10:22:24 -05:00
|
|
|
|
2020-03-27 06:14:45 -05:00
|
|
|
pub(crate) fn mir_operand_get_const_val<'tcx>(
|
2019-07-30 07:37:20 -05:00
|
|
|
fx: &FunctionCx<'_, 'tcx, impl Backend>,
|
|
|
|
operand: &Operand<'tcx>,
|
2019-08-16 09:04:50 -05:00
|
|
|
) -> Option<&'tcx Const<'tcx>> {
|
2020-01-13 14:38:46 -06:00
|
|
|
match operand {
|
2020-02-22 07:20:37 -06:00
|
|
|
Operand::Copy(_) | Operand::Move(_) => None,
|
|
|
|
Operand::Constant(const_) => {
|
|
|
|
Some(fx.monomorphize(&const_.literal).eval(fx.tcx, ParamEnv::reveal_all()))
|
|
|
|
}
|
2020-01-13 14:38:46 -06:00
|
|
|
}
|
2019-07-30 07:37:20 -05:00
|
|
|
}
|