2020-09-23 13:13:49 +00:00
|
|
|
//! Handling of `static`s, `const`s and promoted allocations
|
|
|
|
|
2020-01-06 19:11:03 +00:00
|
|
|
use rustc_span::DUMMY_SP;
|
2020-01-04 16:49:00 +00:00
|
|
|
|
2020-08-28 10:10:48 +00:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-08-20 14:51:01 +00:00
|
|
|
use rustc_errors::ErrorReported;
|
2020-03-31 11:20:19 +00:00
|
|
|
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
|
|
|
use rustc_middle::mir::interpret::{
|
2020-08-20 14:51:01 +00:00
|
|
|
read_target_uint, AllocId, Allocation, ConstValue, ErrorHandled, GlobalAlloc, Pointer, Scalar,
|
2018-08-30 18:14:56 +00:00
|
|
|
};
|
2020-04-03 09:54:18 +00:00
|
|
|
use rustc_middle::ty::{Const, ConstKind};
|
2018-11-07 12:29:38 +00:00
|
|
|
|
2020-06-04 17:57:12 +00:00
|
|
|
use cranelift_codegen::ir::GlobalValueData;
|
2018-11-07 12:29:38 +00:00
|
|
|
use cranelift_module::*;
|
|
|
|
|
|
|
|
use crate::prelude::*;
|
2018-08-13 10:13:43 +00:00
|
|
|
|
|
|
|
#[derive(Default)]
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) struct ConstantCx {
|
2020-03-14 15:36:55 +00:00
|
|
|
todo: Vec<TodoItem>,
|
|
|
|
done: FxHashSet<DataId>,
|
2018-08-13 10:13:43 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 15:36:55 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2018-08-13 16:31:26 +00:00
|
|
|
enum TodoItem {
|
|
|
|
Alloc(AllocId),
|
|
|
|
Static(DefId),
|
|
|
|
}
|
|
|
|
|
2018-08-13 10:13:43 +00:00
|
|
|
impl ConstantCx {
|
2020-10-01 08:38:23 +00:00
|
|
|
pub(crate) fn finalize(mut self, tcx: TyCtxt<'_>, module: &mut impl Module) {
|
2018-08-15 13:28:08 +00:00
|
|
|
//println!("todo {:?}", self.todo);
|
2018-08-13 14:58:07 +00:00
|
|
|
define_all_allocs(tcx, module, &mut self);
|
2018-08-15 13:28:08 +00:00
|
|
|
//println!("done {:?}", self.done);
|
2018-09-09 12:45:23 +00:00
|
|
|
self.done.clear();
|
2018-08-13 10:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-01 08:38:23 +00:00
|
|
|
pub(crate) fn check_constants(fx: &mut FunctionCx<'_, '_, impl Module>) {
|
2020-08-20 14:51:01 +00:00
|
|
|
for constant in &fx.mir.required_consts {
|
2020-10-28 07:25:06 +00:00
|
|
|
let const_ = fx.monomorphize(constant.literal);
|
2020-08-20 14:51:01 +00:00
|
|
|
match const_.val {
|
|
|
|
ConstKind::Value(_) => {}
|
|
|
|
ConstKind::Unevaluated(def, ref substs, promoted) => {
|
2020-08-28 10:10:48 +00:00
|
|
|
if let Err(err) =
|
|
|
|
fx.tcx
|
|
|
|
.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None)
|
|
|
|
{
|
2020-08-20 14:51:01 +00:00
|
|
|
match err {
|
|
|
|
ErrorHandled::Reported(ErrorReported) | ErrorHandled::Linted => {
|
2020-08-28 10:10:48 +00:00
|
|
|
fx.tcx
|
|
|
|
.sess
|
|
|
|
.span_err(constant.span, "erroneous constant encountered");
|
2020-08-20 14:51:01 +00:00
|
|
|
}
|
|
|
|
ErrorHandled::TooGeneric => {
|
2020-08-28 10:10:48 +00:00
|
|
|
span_bug!(
|
|
|
|
constant.span,
|
|
|
|
"codgen encountered polymorphic constant: {:?}",
|
|
|
|
err
|
|
|
|
);
|
2020-08-20 14:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 10:10:48 +00:00
|
|
|
ConstKind::Param(_)
|
|
|
|
| ConstKind::Infer(_)
|
|
|
|
| ConstKind::Bound(_, _)
|
|
|
|
| ConstKind::Placeholder(_)
|
|
|
|
| ConstKind::Error(_) => unreachable!("{:?}", const_),
|
2020-08-20 14:51:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn codegen_static(constants_cx: &mut ConstantCx, def_id: DefId) {
|
2020-03-14 15:36:55 +00:00
|
|
|
constants_cx.todo.push(TodoItem::Static(def_id));
|
2018-08-13 10:13:43 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 17:57:12 +00:00
|
|
|
pub(crate) fn codegen_tls_ref<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Module>,
|
2020-06-04 17:57:12 +00:00
|
|
|
def_id: DefId,
|
|
|
|
layout: TyAndLayout<'tcx>,
|
|
|
|
) -> CValue<'tcx> {
|
2020-08-22 14:47:31 +00:00
|
|
|
let data_id = data_id_for_static(fx.tcx, &mut fx.cx.module, def_id, false);
|
2020-08-22 14:17:58 +00:00
|
|
|
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
2020-06-04 17:57:12 +00:00
|
|
|
#[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 20:38:46 +00:00
|
|
|
fn codegen_static_ref<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Module>,
|
2019-03-27 16:45:20 +00:00
|
|
|
def_id: DefId,
|
2020-03-30 17:00:24 +00:00
|
|
|
layout: TyAndLayout<'tcx>,
|
2018-08-13 10:13:43 +00:00
|
|
|
) -> CPlace<'tcx> {
|
2020-08-22 14:47:31 +00:00
|
|
|
let data_id = data_id_for_static(fx.tcx, &mut fx.cx.module, def_id, false);
|
2020-08-22 14:17:58 +00:00
|
|
|
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
2020-01-17 13:01:51 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 11:18:40 +00:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", def_id));
|
2020-06-04 17:57:12 +00:00
|
|
|
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
|
|
|
|
assert!(!layout.is_unsized(), "unsized statics aren't supported");
|
2020-08-28 10:10:48 +00:00
|
|
|
assert!(
|
|
|
|
matches!(fx.bcx.func.global_values[local_data_id], GlobalValueData::Symbol { tls: false, ..}),
|
|
|
|
"tls static referenced without Rvalue::ThreadLocalRef"
|
|
|
|
);
|
2020-06-04 17:57:12 +00:00
|
|
|
CPlace::for_ptr(crate::pointer::Pointer::new(global_ptr), layout)
|
2018-08-13 10:13:43 +00:00
|
|
|
}
|
2018-07-14 14:45:20 +00:00
|
|
|
|
2020-11-03 10:00:04 +00:00
|
|
|
pub(crate) fn codegen_constant<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Module>,
|
2018-07-31 10:25:16 +00:00
|
|
|
constant: &Constant<'tcx>,
|
|
|
|
) -> CValue<'tcx> {
|
2020-10-28 07:25:06 +00:00
|
|
|
let const_ = fx.monomorphize(constant.literal);
|
2020-04-30 17:00:25 +00:00
|
|
|
let const_val = match const_.val {
|
|
|
|
ConstKind::Value(const_val) => const_val,
|
2020-08-22 14:47:31 +00:00
|
|
|
ConstKind::Unevaluated(def, ref substs, promoted) if fx.tcx.is_static(def.did) => {
|
2020-01-13 20:38:46 +00:00
|
|
|
assert!(substs.is_empty());
|
|
|
|
assert!(promoted.is_none());
|
|
|
|
|
|
|
|
return codegen_static_ref(
|
|
|
|
fx,
|
2020-07-17 17:15:33 +00:00
|
|
|
def.did,
|
2020-01-15 12:17:09 +00:00
|
|
|
fx.layout_of(fx.monomorphize(&constant.literal.ty)),
|
2020-08-28 10:10:48 +00:00
|
|
|
)
|
|
|
|
.to_cvalue(fx);
|
2020-01-13 20:38:46 +00:00
|
|
|
}
|
2020-07-17 17:15:33 +00:00
|
|
|
ConstKind::Unevaluated(def, ref substs, promoted) => {
|
2020-08-28 10:10:48 +00:00
|
|
|
match fx
|
|
|
|
.tcx
|
|
|
|
.const_eval_resolve(ParamEnv::reveal_all(), def, substs, promoted, None)
|
|
|
|
{
|
2020-04-30 17:00:25 +00:00
|
|
|
Ok(const_val) => const_val,
|
|
|
|
Err(_) => {
|
|
|
|
if promoted.is_none() {
|
2020-08-28 10:10:48 +00:00
|
|
|
fx.tcx
|
|
|
|
.sess
|
|
|
|
.span_err(constant.span, "erroneous constant encountered");
|
2020-04-30 17:00:25 +00:00
|
|
|
}
|
|
|
|
return crate::trap::trap_unreachable_ret_value(
|
|
|
|
fx,
|
|
|
|
fx.layout_of(const_.ty),
|
|
|
|
"erroneous constant encountered",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 10:10:48 +00:00
|
|
|
ConstKind::Param(_)
|
|
|
|
| ConstKind::Infer(_)
|
|
|
|
| ConstKind::Bound(_, _)
|
|
|
|
| ConstKind::Placeholder(_)
|
|
|
|
| ConstKind::Error(_) => unreachable!("{:?}", const_),
|
2020-01-13 20:38:46 +00:00
|
|
|
};
|
|
|
|
|
2020-11-03 10:00:04 +00:00
|
|
|
codegen_const_value(fx, const_val, const_.ty)
|
2018-07-30 14:57:40 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 10:00:04 +00:00
|
|
|
pub(crate) fn codegen_const_value<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Module>,
|
2020-04-30 17:00:25 +00:00
|
|
|
const_val: ConstValue<'tcx>,
|
2020-08-28 10:10:48 +00:00
|
|
|
ty: Ty<'tcx>,
|
2018-07-31 10:25:16 +00:00
|
|
|
) -> CValue<'tcx> {
|
2018-07-18 11:46:56 +00:00
|
|
|
let layout = fx.layout_of(ty);
|
2020-04-04 17:06:07 +00:00
|
|
|
assert!(!layout.is_unsized(), "sized const value");
|
2020-01-13 20:38:46 +00:00
|
|
|
|
|
|
|
if layout.is_zst() {
|
|
|
|
return CValue::by_ref(
|
2020-11-03 10:00:04 +00:00
|
|
|
crate::Pointer::dangling(layout.align.pref),
|
2020-01-13 20:38:46 +00:00
|
|
|
layout,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
match const_val {
|
|
|
|
ConstValue::Scalar(x) => {
|
2020-01-17 13:01:51 +00:00
|
|
|
if fx.clif_type(layout.ty).is_none() {
|
2020-04-04 17:06:07 +00:00
|
|
|
let (size, align) = (layout.size, layout.align.pref);
|
|
|
|
let mut alloc = Allocation::from_bytes(
|
2020-08-28 10:10:48 +00:00
|
|
|
std::iter::repeat(0)
|
|
|
|
.take(size.bytes_usize())
|
|
|
|
.collect::<Vec<u8>>(),
|
2020-04-04 17:06:07 +00:00
|
|
|
align,
|
|
|
|
);
|
|
|
|
let ptr = Pointer::new(AllocId(!0), Size::ZERO); // The alloc id is never used
|
|
|
|
alloc.write_scalar(fx, ptr, x.into(), size).unwrap();
|
2020-08-22 14:47:31 +00:00
|
|
|
let alloc = fx.tcx.intern_const_alloc(alloc);
|
2020-04-04 17:06:07 +00:00
|
|
|
return CValue::by_ref(pointer_for_allocation(fx, alloc), layout);
|
2020-01-17 13:01:51 +00:00
|
|
|
}
|
2020-01-13 20:38:46 +00:00
|
|
|
|
2020-01-17 13:01:51 +00:00
|
|
|
match x {
|
2020-11-01 16:57:03 +00:00
|
|
|
Scalar::Int(int) => {
|
2020-10-29 13:30:47 +00:00
|
|
|
CValue::const_val(fx, layout, int)
|
2020-01-13 20:38:46 +00:00
|
|
|
}
|
2020-01-17 13:01:51 +00:00
|
|
|
Scalar::Ptr(ptr) => {
|
2020-08-22 14:47:31 +00:00
|
|
|
let alloc_kind = fx.tcx.get_global_alloc(ptr.alloc_id);
|
2020-01-17 13:01:51 +00:00
|
|
|
let base_addr = match alloc_kind {
|
|
|
|
Some(GlobalAlloc::Memory(alloc)) => {
|
2020-08-22 14:17:58 +00:00
|
|
|
fx.cx.constants_cx.todo.push(TodoItem::Alloc(ptr.alloc_id));
|
2020-08-28 10:10:48 +00:00
|
|
|
let data_id = data_id_for_alloc_id(
|
|
|
|
&mut fx.cx.module,
|
|
|
|
ptr.alloc_id,
|
|
|
|
alloc.mutability,
|
|
|
|
);
|
|
|
|
let local_data_id =
|
|
|
|
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
2020-01-17 13:01:51 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 11:18:40 +00:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", ptr.alloc_id));
|
2020-01-17 13:01:51 +00:00
|
|
|
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
|
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Function(instance)) => {
|
2020-08-28 10:10:48 +00:00
|
|
|
let func_id =
|
|
|
|
crate::abi::import_function(fx.tcx, &mut fx.cx.module, instance);
|
|
|
|
let local_func_id =
|
|
|
|
fx.cx.module.declare_func_in_func(func_id, &mut fx.bcx.func);
|
2020-01-17 13:01:51 +00:00
|
|
|
fx.bcx.ins().func_addr(fx.pointer_type, local_func_id)
|
|
|
|
}
|
|
|
|
Some(GlobalAlloc::Static(def_id)) => {
|
2020-08-22 14:47:31 +00:00
|
|
|
assert!(fx.tcx.is_static(def_id));
|
2020-08-28 10:10:48 +00:00
|
|
|
let data_id =
|
|
|
|
data_id_for_static(fx.tcx, &mut fx.cx.module, def_id, false);
|
|
|
|
let local_data_id =
|
|
|
|
fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
2020-01-17 13:01:51 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 11:18:40 +00:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", def_id));
|
2020-01-17 13:01:51 +00:00
|
|
|
fx.bcx.ins().global_value(fx.pointer_type, local_data_id)
|
|
|
|
}
|
|
|
|
None => bug!("missing allocation {:?}", ptr.alloc_id),
|
2020-01-13 20:38:46 +00:00
|
|
|
};
|
2020-10-12 10:03:18 +00:00
|
|
|
let val = if ptr.offset.bytes() != 0 {
|
|
|
|
fx.bcx
|
|
|
|
.ins()
|
|
|
|
.iadd_imm(base_addr, i64::try_from(ptr.offset.bytes()).unwrap())
|
|
|
|
} else {
|
|
|
|
base_addr
|
|
|
|
};
|
2020-11-03 10:00:04 +00:00
|
|
|
CValue::by_val(val, layout)
|
2020-01-13 20:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-14 14:45:20 +00:00
|
|
|
}
|
2020-08-28 10:10:48 +00:00
|
|
|
ConstValue::ByRef { alloc, offset } => CValue::by_ref(
|
|
|
|
pointer_for_allocation(fx, alloc)
|
|
|
|
.offset_i64(fx, i64::try_from(offset.bytes()).unwrap()),
|
|
|
|
layout,
|
|
|
|
),
|
2020-04-04 17:06:07 +00:00
|
|
|
ConstValue::Slice { data, start, end } => {
|
|
|
|
let ptr = pointer_for_allocation(fx, data)
|
|
|
|
.offset_i64(fx, i64::try_from(start).unwrap())
|
|
|
|
.get_addr(fx);
|
2020-08-28 10:10:48 +00:00
|
|
|
let len = fx.bcx.ins().iconst(
|
|
|
|
fx.pointer_type,
|
|
|
|
i64::try_from(end.checked_sub(start).unwrap()).unwrap(),
|
|
|
|
);
|
2020-04-04 17:06:07 +00:00
|
|
|
CValue::by_val_pair(ptr, len, layout)
|
2019-08-28 14:38:53 +00:00
|
|
|
}
|
2018-07-16 13:13:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-04 17:06:07 +00:00
|
|
|
fn pointer_for_allocation<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &mut FunctionCx<'_, 'tcx, impl Module>,
|
2020-04-04 17:06:07 +00:00
|
|
|
alloc: &'tcx Allocation,
|
|
|
|
) -> crate::pointer::Pointer {
|
2020-08-22 14:47:31 +00:00
|
|
|
let alloc_id = fx.tcx.create_memory_alloc(alloc);
|
2020-08-22 14:17:58 +00:00
|
|
|
fx.cx.constants_cx.todo.push(TodoItem::Alloc(alloc_id));
|
2020-10-01 08:38:23 +00:00
|
|
|
let data_id = data_id_for_alloc_id(&mut fx.cx.module, alloc_id, alloc.mutability);
|
2020-04-04 17:06:07 +00:00
|
|
|
|
2020-08-22 14:17:58 +00:00
|
|
|
let local_data_id = fx.cx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
|
2020-01-17 13:01:51 +00:00
|
|
|
#[cfg(debug_assertions)]
|
2020-03-20 11:18:40 +00:00
|
|
|
fx.add_comment(local_data_id, format!("{:?}", alloc_id));
|
2020-04-04 17:06:07 +00:00
|
|
|
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
|
|
|
|
crate::pointer::Pointer::new(global_ptr)
|
2018-07-30 14:57:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 08:38:23 +00:00
|
|
|
fn data_id_for_alloc_id(
|
|
|
|
module: &mut impl Module,
|
2019-08-31 17:28:09 +00:00
|
|
|
alloc_id: AllocId,
|
2020-05-01 10:15:11 +00:00
|
|
|
mutability: rustc_hir::Mutability,
|
2019-08-31 17:28:09 +00:00
|
|
|
) -> DataId {
|
2018-08-13 14:58:07 +00:00
|
|
|
module
|
2019-08-31 17:28:09 +00:00
|
|
|
.declare_data(
|
2020-11-03 10:00:04 +00:00
|
|
|
&format!(".L__alloc_{:x}", alloc_id.0),
|
2019-08-31 17:28:09 +00:00
|
|
|
Linkage::Local,
|
2020-05-01 10:15:11 +00:00
|
|
|
mutability == rustc_hir::Mutability::Mut,
|
2019-10-27 15:55:35 +00:00
|
|
|
false,
|
2019-08-31 17:28:09 +00:00
|
|
|
)
|
2018-08-13 14:58:07 +00:00
|
|
|
.unwrap()
|
2018-07-16 13:13:37 +00:00
|
|
|
}
|
|
|
|
|
2019-06-16 09:13:49 +00:00
|
|
|
fn data_id_for_static(
|
|
|
|
tcx: TyCtxt<'_>,
|
2020-10-01 08:38:23 +00:00
|
|
|
module: &mut impl Module,
|
2018-08-22 10:31:45 +00:00
|
|
|
def_id: DefId,
|
2020-08-16 19:20:12 +00:00
|
|
|
definition: bool,
|
2018-08-22 10:31:45 +00:00
|
|
|
) -> DataId {
|
2020-08-16 19:20:12 +00:00
|
|
|
let rlinkage = tcx.codegen_fn_attrs(def_id).linkage;
|
|
|
|
let linkage = if definition {
|
|
|
|
crate::linkage::get_static_linkage(tcx, def_id)
|
2020-11-03 10:00:04 +00:00
|
|
|
} else if rlinkage == Some(rustc_middle::mir::mono::Linkage::ExternalWeak)
|
|
|
|
|| rlinkage == Some(rustc_middle::mir::mono::Linkage::WeakAny)
|
|
|
|
{
|
|
|
|
Linkage::Preemptible
|
2020-08-16 19:20:12 +00:00
|
|
|
} else {
|
2020-11-03 10:00:04 +00:00
|
|
|
Linkage::Import
|
2020-08-16 19:20:12 +00:00
|
|
|
};
|
|
|
|
|
2020-07-23 16:07:38 +00:00
|
|
|
let instance = Instance::mono(tcx, def_id).polymorphize(tcx);
|
2020-07-17 17:15:33 +00:00
|
|
|
let symbol_name = tcx.symbol_name(instance).name;
|
2020-07-23 09:42:44 +00:00
|
|
|
let ty = instance.ty(tcx, ParamEnv::reveal_all());
|
2019-04-24 14:35:00 +00:00
|
|
|
let is_mutable = if tcx.is_mutable_static(def_id) {
|
2018-11-09 16:54:28 +00:00
|
|
|
true
|
|
|
|
} else {
|
2020-06-27 09:29:39 +00:00
|
|
|
!ty.is_freeze(tcx.at(DUMMY_SP), ParamEnv::reveal_all())
|
2018-11-09 16:54:28 +00:00
|
|
|
};
|
2019-08-31 17:28:09 +00:00
|
|
|
let align = tcx
|
|
|
|
.layout_of(ParamEnv::reveal_all().and(ty))
|
|
|
|
.unwrap()
|
|
|
|
.align
|
|
|
|
.pref
|
|
|
|
.bytes();
|
2019-03-11 19:36:29 +00:00
|
|
|
|
2019-10-27 15:55:35 +00:00
|
|
|
let attrs = tcx.codegen_fn_attrs(def_id);
|
|
|
|
|
2019-03-11 19:36:29 +00:00
|
|
|
let data_id = module
|
2019-08-31 17:28:09 +00:00
|
|
|
.declare_data(
|
|
|
|
&*symbol_name,
|
|
|
|
linkage,
|
|
|
|
is_mutable,
|
2020-03-14 13:39:29 +00:00
|
|
|
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
|
2019-08-31 17:28:09 +00:00
|
|
|
)
|
2019-03-11 19:36:29 +00:00
|
|
|
.unwrap();
|
|
|
|
|
2020-08-16 19:20:12 +00:00
|
|
|
if rlinkage.is_some() {
|
|
|
|
// Comment copied from https://github.com/rust-lang/rust/blob/45060c2a66dfd667f88bd8b94261b28a58d85bd5/src/librustc_codegen_llvm/consts.rs#L141
|
|
|
|
// Declare an internal global `extern_with_linkage_foo` which
|
|
|
|
// is initialized with the address of `foo`. If `foo` is
|
|
|
|
// discarded during linking (for example, if `foo` has weak
|
|
|
|
// linkage and there are no definitions), then
|
|
|
|
// `extern_with_linkage_foo` will instead be initialized to
|
|
|
|
// zero.
|
|
|
|
|
|
|
|
let ref_name = format!("_rust_extern_with_linkage_{}", symbol_name);
|
|
|
|
let ref_data_id = module
|
2020-10-12 10:02:38 +00:00
|
|
|
.declare_data(&ref_name, Linkage::Local, false, false)
|
2020-08-16 19:20:12 +00:00
|
|
|
.unwrap();
|
2019-03-11 19:36:29 +00:00
|
|
|
let mut data_ctx = DataContext::new();
|
2020-10-01 08:38:23 +00:00
|
|
|
data_ctx.set_align(align);
|
2020-08-16 19:20:12 +00:00
|
|
|
let data = module.declare_data_in_data(data_id, &mut data_ctx);
|
2020-08-28 10:10:48 +00:00
|
|
|
data_ctx.define(
|
|
|
|
std::iter::repeat(0)
|
|
|
|
.take(pointer_ty(tcx).bytes() as usize)
|
|
|
|
.collect(),
|
|
|
|
);
|
2020-08-16 19:20:12 +00:00
|
|
|
data_ctx.write_data_addr(0, data, 0);
|
|
|
|
match module.define_data(ref_data_id, &data_ctx) {
|
|
|
|
// Every time the static is referenced there will be another definition of this global,
|
2019-03-11 19:36:29 +00:00
|
|
|
// so duplicate definitions are expected and allowed.
|
|
|
|
Err(ModuleError::DuplicateDefinition(_)) => {}
|
|
|
|
res => res.unwrap(),
|
|
|
|
}
|
2020-08-16 19:20:12 +00:00
|
|
|
ref_data_id
|
|
|
|
} else {
|
|
|
|
data_id
|
2019-03-11 19:36:29 +00:00
|
|
|
}
|
2018-08-13 16:31:26 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 08:38:23 +00:00
|
|
|
fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut impl Module, cx: &mut ConstantCx) {
|
2020-03-14 15:36:55 +00:00
|
|
|
while let Some(todo_item) = cx.todo.pop() {
|
2020-06-20 10:01:24 +00:00
|
|
|
let (data_id, alloc, section_name) = match todo_item {
|
2018-08-13 16:31:26 +00:00
|
|
|
TodoItem::Alloc(alloc_id) => {
|
2018-08-15 13:28:08 +00:00
|
|
|
//println!("alloc_id {}", alloc_id);
|
2020-05-18 09:35:23 +00:00
|
|
|
let alloc = match tcx.get_global_alloc(alloc_id).unwrap() {
|
2020-04-04 17:06:07 +00:00
|
|
|
GlobalAlloc::Memory(alloc) => alloc,
|
|
|
|
GlobalAlloc::Function(_) | GlobalAlloc::Static(_) => unreachable!(),
|
|
|
|
};
|
2020-10-01 08:38:23 +00:00
|
|
|
let data_id = data_id_for_alloc_id(module, alloc_id, alloc.mutability);
|
2020-06-20 10:01:24 +00:00
|
|
|
(data_id, alloc, None)
|
2018-08-13 16:31:26 +00:00
|
|
|
}
|
|
|
|
TodoItem::Static(def_id) => {
|
2018-08-15 13:28:08 +00:00
|
|
|
//println!("static {:?}", def_id);
|
2019-03-11 19:36:29 +00:00
|
|
|
|
2020-08-28 10:10:48 +00:00
|
|
|
let section_name = tcx
|
|
|
|
.codegen_fn_attrs(def_id)
|
|
|
|
.link_section
|
|
|
|
.map(|s| s.as_str());
|
2019-03-11 19:36:29 +00:00
|
|
|
|
2020-09-29 11:50:06 +00:00
|
|
|
let alloc = tcx.eval_static_initializer(def_id).unwrap();
|
2018-08-13 16:31:26 +00:00
|
|
|
|
2020-08-16 19:20:12 +00:00
|
|
|
let data_id = data_id_for_static(tcx, module, def_id, true);
|
2020-06-20 10:01:24 +00:00
|
|
|
(data_id, alloc, section_name)
|
2018-08-13 16:31:26 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-15 13:28:08 +00:00
|
|
|
//("data_id {}", data_id);
|
2018-08-13 14:26:30 +00:00
|
|
|
if cx.done.contains(&data_id) {
|
|
|
|
continue;
|
|
|
|
}
|
2018-07-16 13:13:37 +00:00
|
|
|
|
|
|
|
let mut data_ctx = DataContext::new();
|
2020-10-01 08:38:23 +00:00
|
|
|
data_ctx.set_align(alloc.align.bytes());
|
2018-07-16 13:13:37 +00:00
|
|
|
|
2020-06-20 10:01:24 +00:00
|
|
|
if let Some(section_name) = section_name {
|
|
|
|
// FIXME set correct segment for Mach-O files
|
|
|
|
data_ctx.set_segment_section("", &*section_name);
|
|
|
|
}
|
|
|
|
|
2020-08-28 10:10:48 +00:00
|
|
|
let bytes = alloc
|
|
|
|
.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len())
|
|
|
|
.to_vec();
|
2019-08-07 13:26:29 +00:00
|
|
|
data_ctx.define(bytes.into_boxed_slice());
|
2018-07-16 13:13:37 +00:00
|
|
|
|
2019-09-07 08:51:00 +00:00
|
|
|
for &(offset, (_tag, reloc)) in alloc.relocations().iter() {
|
2019-01-06 16:51:21 +00:00
|
|
|
let addend = {
|
2018-09-16 14:21:07 +00:00
|
|
|
let endianness = tcx.data_layout.endian;
|
|
|
|
let offset = offset.bytes() as usize;
|
|
|
|
let ptr_size = tcx.data_layout.pointer_size;
|
2020-08-28 10:10:48 +00:00
|
|
|
let bytes = &alloc.inspect_with_uninit_and_ptr_outside_interpreter(
|
|
|
|
offset..offset + ptr_size.bytes() as usize,
|
|
|
|
);
|
2018-09-16 14:21:07 +00:00
|
|
|
read_target_uint(endianness, bytes).unwrap()
|
|
|
|
};
|
|
|
|
|
2020-05-18 09:35:23 +00:00
|
|
|
let reloc_target_alloc = tcx.get_global_alloc(reloc).unwrap();
|
2019-08-17 10:31:10 +00:00
|
|
|
let data_id = match reloc_target_alloc {
|
2019-06-02 10:06:02 +00:00
|
|
|
GlobalAlloc::Function(instance) => {
|
2019-01-06 16:51:21 +00:00
|
|
|
assert_eq!(addend, 0);
|
2019-01-02 11:20:32 +00:00
|
|
|
let func_id = crate::abi::import_function(tcx, module, instance);
|
2018-09-16 14:21:07 +00:00
|
|
|
let local_func_id = module.declare_func_in_data(func_id, &mut data_ctx);
|
2019-01-06 16:51:21 +00:00
|
|
|
data_ctx.write_function_addr(offset.bytes() as u32, local_func_id);
|
2018-09-16 14:21:07 +00:00
|
|
|
continue;
|
2018-09-26 13:40:11 +00:00
|
|
|
}
|
2020-05-01 10:15:11 +00:00
|
|
|
GlobalAlloc::Memory(target_alloc) => {
|
2020-03-14 15:36:55 +00:00
|
|
|
cx.todo.push(TodoItem::Alloc(reloc));
|
2020-10-01 08:38:23 +00:00
|
|
|
data_id_for_alloc_id(module, reloc, target_alloc.mutability)
|
2018-08-13 17:02:42 +00:00
|
|
|
}
|
2019-06-02 10:06:02 +00:00
|
|
|
GlobalAlloc::Static(def_id) => {
|
2020-08-28 10:10:48 +00: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
|
|
|
|
));
|
2020-03-14 13:39:29 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 13:36:03 +00: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.
|
2020-08-16 19:20:12 +00:00
|
|
|
data_id_for_static(tcx, module, def_id, false)
|
2018-08-13 17:02:42 +00:00
|
|
|
}
|
|
|
|
};
|
2018-07-16 13:13:37 +00:00
|
|
|
|
2018-08-13 10:13:43 +00:00
|
|
|
let global_value = module.declare_data_in_data(data_id, &mut data_ctx);
|
2019-01-06 16:51:21 +00:00
|
|
|
data_ctx.write_data_addr(offset.bytes() as u32, global_value, addend as i64);
|
2018-07-16 13:13:37 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 10:13:43 +00:00
|
|
|
module.define_data(data_id, &data_ctx).unwrap();
|
|
|
|
cx.done.insert(data_id);
|
2018-07-14 14:45:20 +00:00
|
|
|
}
|
2018-08-13 14:58:07 +00:00
|
|
|
|
2018-08-13 16:31:26 +00:00
|
|
|
assert!(cx.todo.is_empty(), "{:?}", cx.todo);
|
2018-07-16 13:13:37 +00:00
|
|
|
}
|
2018-08-13 15:22:24 +00:00
|
|
|
|
2020-03-27 11:14:45 +00:00
|
|
|
pub(crate) fn mir_operand_get_const_val<'tcx>(
|
2020-10-01 08:38:23 +00:00
|
|
|
fx: &FunctionCx<'_, 'tcx, impl Module>,
|
2019-07-30 12:37:20 +00:00
|
|
|
operand: &Operand<'tcx>,
|
2019-08-16 14:04:50 +00:00
|
|
|
) -> Option<&'tcx Const<'tcx>> {
|
2020-01-13 20:38:46 +00:00
|
|
|
match operand {
|
2020-02-22 13:20:37 +00:00
|
|
|
Operand::Copy(_) | Operand::Move(_) => None,
|
2020-08-28 10:10:48 +00:00
|
|
|
Operand::Constant(const_) => Some(
|
2020-10-28 07:25:06 +00:00
|
|
|
fx.monomorphize(const_.literal)
|
2020-08-28 10:10:48 +00:00
|
|
|
.eval(fx.tcx, ParamEnv::reveal_all()),
|
|
|
|
),
|
2020-01-13 20:38:46 +00:00
|
|
|
}
|
2019-07-30 12:37:20 +00:00
|
|
|
}
|