Ensure that statics are always ByRef

This commit is contained in:
Oliver Schneider 2018-05-12 18:47:20 +02:00 committed by Oliver Schneider
parent c95e1cccc9
commit 8b99c61701
3 changed files with 69 additions and 133 deletions

View file

@ -81,49 +81,6 @@ pub fn primval_to_llvm(cx: &CodegenCx,
}
}
fn const_value_to_llvm<'tcx>(cx: &CodegenCx<'_, 'tcx>, val: ConstValue, ty: Ty<'tcx>) -> ValueRef {
let layout = cx.layout_of(ty);
if layout.is_zst() {
return C_undef(layout.immediate_llvm_type(cx));
}
match val {
ConstValue::ByVal(x) => {
let scalar = match layout.abi {
layout::Abi::Scalar(ref x) => x,
_ => bug!("const_value_to_llvm: invalid ByVal layout: {:#?}", layout)
};
primval_to_llvm(
cx,
x,
scalar,
layout.immediate_llvm_type(cx),
)
},
ConstValue::ByValPair(a, b) => {
let (a_scalar, b_scalar) = match layout.abi {
layout::Abi::ScalarPair(ref a, ref b) => (a, b),
_ => bug!("const_value_to_llvm: invalid ByValPair layout: {:#?}", layout)
};
let a_llval = primval_to_llvm(
cx,
a,
a_scalar,
layout.scalar_pair_element_llvm_type(cx, 0),
);
let b_llval = primval_to_llvm(
cx,
b,
b_scalar,
layout.scalar_pair_element_llvm_type(cx, 1),
);
C_struct(cx, &[a_llval, b_llval], false)
},
ConstValue::ByRef(alloc) => const_alloc_to_llvm(cx, alloc),
}
}
pub fn const_alloc_to_llvm(cx: &CodegenCx, alloc: &Allocation) -> ValueRef {
let mut llvals = Vec::with_capacity(alloc.relocations.len() + 1);
let layout = cx.data_layout();
@ -171,11 +128,11 @@ pub fn codegen_static_initializer<'a, 'tcx>(
let param_env = ty::ParamEnv::reveal_all();
let static_ = cx.tcx.const_eval(param_env.and(cid))?;
let val = match static_.val {
ConstVal::Value(val) => val,
let alloc = match static_.val {
ConstVal::Value(ConstValue::ByRef(alloc)) => alloc,
_ => bug!("static const eval returned {:#?}", static_),
};
Ok(const_value_to_llvm(cx, val, static_.ty))
Ok(const_alloc_to_llvm(cx, alloc))
}
impl<'a, 'tcx> FunctionCx<'a, 'tcx> {