Encapsulate CPlace constructors

This commit is contained in:
bjorn3 2019-06-11 16:30:47 +02:00
parent 3f04e73b40
commit 089e75ec42
6 changed files with 19 additions and 12 deletions

View File

@ -330,7 +330,7 @@ fn local_place<'a, 'tcx: 'a>(
}
// Take stack_addr in advance to avoid many duplicate instructions
CPlace::Addr(place.to_addr(fx), None, layout)
CPlace::for_addr(place.to_addr(fx), layout)
};
let prev_place = fx.local_map.insert(local, place);
@ -471,7 +471,7 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(
PassMode::ByRef => {
fx.local_map.insert(
RETURN_PLACE,
CPlace::Addr(ret_param.unwrap(), None, ret_layout),
CPlace::for_addr(ret_param.unwrap(), ret_layout),
);
}
}

View File

@ -538,7 +538,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
// FIXME avoid forcing to stack
let place =
CPlace::Addr(operand.force_stack(fx), None, operand.layout());
CPlace::for_addr(operand.force_stack(fx), operand.layout());
let discr = trans_get_discriminant(fx, place, fx.layout_of(to_ty));
lval.write_cvalue(fx, discr);
} else {
@ -1162,9 +1162,8 @@ pub fn trans_place<'a, 'tcx: 'a>(
let elem_layout = fx.layout_of(elem_ty);
let ptr = base.to_addr(fx);
let len = crate::constant::force_eval_const(fx, len).unwrap_usize(fx.tcx);
CPlace::Addr(
CPlace::for_addr(
fx.bcx.ins().iadd_imm(ptr, elem_layout.size.bytes() as i64 * from as i64),
None,
fx.layout_of(fx.tcx.mk_array(elem_ty, len - from as u64 - to as u64)),
)
}
@ -1172,9 +1171,9 @@ pub fn trans_place<'a, 'tcx: 'a>(
let elem_layout = fx.layout_of(elem_ty);
let (ptr, len) = base.to_addr_maybe_unsized(fx);
let len = len.unwrap();
CPlace::Addr(
CPlace::for_addr_with_extra(
fx.bcx.ins().iadd_imm(ptr, elem_layout.size.bytes() as i64 * from as i64),
Some(fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64))),
fx.bcx.ins().iadd_imm(len, -(from as i64 + to as i64)),
base.layout(),
)
}

View File

@ -234,7 +234,7 @@ fn cplace_for_dataid<'a, 'tcx: 'a>(
let global_ptr = fx.bcx.ins().global_value(fx.pointer_type, local_data_id);
let layout = fx.layout_of(fx.monomorphize(&ty));
assert!(!layout.is_unsized(), "unsized statics aren't supported");
CPlace::Addr(global_ptr, None, layout)
CPlace::for_addr(global_ptr, layout)
}
fn define_all_allocs<'a, 'tcx: 'a, B: Backend + 'a>(

View File

@ -144,7 +144,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
};
discriminant_value, (c val) {
let pointee_layout = fx.layout_of(val.layout().ty.builtin_deref(true).unwrap().ty);
let place = CPlace::Addr(val.load_scalar(fx), None, pointee_layout);
let place = CPlace::for_addr(val.load_scalar(fx), pointee_layout);
let discr = crate::base::trans_get_discriminant(fx, place, ret.layout());
ret.write_cvalue(fx, discr);
};
@ -426,7 +426,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
ret.write_cvalue(fx, val);
};
_ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
let dest = CPlace::Addr(ptr, None, val.layout());
let dest = CPlace::for_addr(ptr, val.layout());
dest.write_cvalue(fx, val);
};
_ if intrinsic.starts_with("atomic_xchg"), <T> (v ptr, c src) {
@ -436,7 +436,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
ret.write_cvalue(fx, CValue::by_val(old, fx.layout_of(T)));
// Write new
let dest = CPlace::Addr(ptr, None, src.layout());
let dest = CPlace::for_addr(ptr, src.layout());
dest.write_cvalue(fx, src);
};
_ if intrinsic.starts_with("atomic_cxchg"), <T> (v ptr, v test_old, v new) { // both atomic_cxchg_* and atomic_cxchgweak_*

View File

@ -49,5 +49,5 @@ pub fn trap_unreachable_ret_place<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl crane
let true_ = fx.bcx.ins().iconst(types::I32, 1);
fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
CPlace::Addr(zero, None, dest_layout)
CPlace::for_addr(zero, dest_layout)
}

View File

@ -219,6 +219,14 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
CPlace::Var(local, layout)
}
pub fn for_addr(addr: Value, layout: TyLayout<'tcx>) -> CPlace<'tcx> {
CPlace::Addr(addr, None, layout)
}
pub fn for_addr_with_extra(addr: Value, extra: Value, layout: TyLayout<'tcx>) -> CPlace<'tcx> {
CPlace::Addr(addr, Some(extra), layout)
}
pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CValue<'tcx> {
match self {
CPlace::Var(var, layout) => CValue::ByVal(fx.bcx.use_var(mir_var(var)), layout),