1
Fork 0

Fix cranelift build

This commit is contained in:
oli 2020-10-29 13:30:47 +00:00
parent c478574786
commit 3a7970848c
6 changed files with 28 additions and 10 deletions

View file

@ -499,7 +499,7 @@ fn codegen_stmt<'tcx>(
UnOp::Neg => match layout.ty.kind() { UnOp::Neg => match layout.ty.kind() {
ty::Int(IntTy::I128) => { ty::Int(IntTy::I128) => {
// FIXME remove this case once ineg.i128 works // FIXME remove this case once ineg.i128 works
let zero = CValue::const_val(fx, layout, 0); let zero = CValue::const_val(fx, layout, ty::ScalarInt::null(layout.size));
crate::num::codegen_int_binop(fx, BinOp::Sub, zero, operand) crate::num::codegen_int_binop(fx, BinOp::Sub, zero, operand)
} }
ty::Int(_) => CValue::by_val(fx.bcx.ins().ineg(val), layout), ty::Int(_) => CValue::by_val(fx.bcx.ins().ineg(val), layout),
@ -592,6 +592,7 @@ fn codegen_stmt<'tcx>(
} else { } else {
discr.val discr.val
}; };
let discr = discr.into();
let discr = CValue::const_val(fx, fx.layout_of(to_ty), discr); let discr = CValue::const_val(fx, fx.layout_of(to_ty), discr);
lval.write_cvalue(fx, discr); lval.write_cvalue(fx, discr);

View file

@ -186,9 +186,8 @@ pub(crate) fn codegen_const_value<'tcx>(
} }
match x { match x {
Scalar::Raw { data, size } => { Scalar::Raw(int) => {
assert_eq!(u64::from(size), layout.size.bytes()); CValue::const_val(fx, layout, int)
CValue::const_val(fx, layout, data)
} }
Scalar::Ptr(ptr) => { Scalar::Ptr(ptr) => {
let alloc_kind = fx.tcx.get_global_alloc(ptr.alloc_id); let alloc_kind = fx.tcx.get_global_alloc(ptr.alloc_id);

View file

@ -30,7 +30,8 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
.ty .ty
.discriminant_for_variant(fx.tcx, variant_index) .discriminant_for_variant(fx.tcx, variant_index)
.unwrap() .unwrap()
.val; .val
.into();
let discr = CValue::const_val(fx, ptr.layout(), to); let discr = CValue::const_val(fx, ptr.layout(), to);
ptr.write_cvalue(fx, discr); ptr.write_cvalue(fx, discr);
} }
@ -49,7 +50,7 @@ pub(crate) fn codegen_set_discriminant<'tcx>(
let niche = place.place_field(fx, mir::Field::new(tag_field)); let niche = place.place_field(fx, mir::Field::new(tag_field));
let niche_value = variant_index.as_u32() - niche_variants.start().as_u32(); let niche_value = variant_index.as_u32() - niche_variants.start().as_u32();
let niche_value = u128::from(niche_value).wrapping_add(niche_start); let niche_value = u128::from(niche_value).wrapping_add(niche_start);
let niche_llval = CValue::const_val(fx, niche.layout(), niche_value); let niche_llval = CValue::const_val(fx, niche.layout(), niche_value.into());
niche.write_cvalue(fx, niche_llval); niche.write_cvalue(fx, niche_llval);
} }
} }
@ -77,7 +78,7 @@ pub(crate) fn codegen_get_discriminant<'tcx>(
.ty .ty
.discriminant_for_variant(fx.tcx, *index) .discriminant_for_variant(fx.tcx, *index)
.map_or(u128::from(index.as_u32()), |discr| discr.val); .map_or(u128::from(index.as_u32()), |discr| discr.val);
return CValue::const_val(fx, dest_layout, discr_val); return CValue::const_val(fx, dest_layout, discr_val.into());
} }
Variants::Multiple { Variants::Multiple {
tag, tag,

View file

@ -1064,7 +1064,8 @@ pub(crate) fn codegen_intrinsic_call<'tcx>(
fx.bcx.ins().call_indirect(f_sig, f, &[data]); fx.bcx.ins().call_indirect(f_sig, f, &[data]);
let ret_val = CValue::const_val(fx, ret.layout(), 0); let layout = ret.layout();
let ret_val = CValue::const_val(fx, layout, ty::ScalarInt::null(layout.size));
ret.write_cvalue(fx, ret_val); ret.write_cvalue(fx, ret_val);
}; };

View file

@ -231,15 +231,16 @@ impl<'tcx> CValue<'tcx> {
pub(crate) fn const_val( pub(crate) fn const_val(
fx: &mut FunctionCx<'_, 'tcx, impl Module>, fx: &mut FunctionCx<'_, 'tcx, impl Module>,
layout: TyAndLayout<'tcx>, layout: TyAndLayout<'tcx>,
const_val: u128, const_val: ty::ScalarInt,
) -> CValue<'tcx> { ) -> CValue<'tcx> {
assert_eq!(const_val.size(), layout.size);
use cranelift_codegen::ir::immediates::{Ieee32, Ieee64}; use cranelift_codegen::ir::immediates::{Ieee32, Ieee64};
let clif_ty = fx.clif_type(layout.ty).unwrap(); let clif_ty = fx.clif_type(layout.ty).unwrap();
if let ty::Bool = layout.ty.kind() { if let ty::Bool = layout.ty.kind() {
assert!( assert!(
const_val == 0 || const_val == 1, const_val == ty::ScalarInt::FALSE || const_val == ty::ScalarInt::TRUE,
"Invalid bool 0x{:032X}", "Invalid bool 0x{:032X}",
const_val const_val
); );
@ -247,6 +248,7 @@ impl<'tcx> CValue<'tcx> {
let val = match layout.ty.kind() { let val = match layout.ty.kind() {
ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => { ty::Uint(UintTy::U128) | ty::Int(IntTy::I128) => {
let const_val = const_val.to_bits(layout.size).unwrap();
let lsb = fx.bcx.ins().iconst(types::I64, const_val as u64 as i64); let lsb = fx.bcx.ins().iconst(types::I64, const_val as u64 as i64);
let msb = fx let msb = fx
.bcx .bcx

View file

@ -353,3 +353,17 @@ impl fmt::LowerHex for ScalarInt {
write!(f, "{:01$x}", { self.data }, self.size as usize * 2) write!(f, "{:01$x}", { self.data }, self.size as usize * 2)
} }
} }
impl fmt::UpperHex for ScalarInt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.check_data();
// Format as hex number wide enough to fit any value of the given `size`.
// So data=20, size=1 will be "0x14", but with size=4 it'll be "0x00000014".
// Using a block `{self.data}` here to force a copy instead of using `self.data`
// directly, because `write!` takes references to its formatting arguments and
// would thus borrow `self.data`. Since `Self`
// is a packed struct, that would create a possibly unaligned reference, which
// is UB on a lot of platforms.
write!(f, "{:01$X}", { self.data }, self.size as usize * 2)
}
}