Skip to content

Commit d6e07cf

Browse files
committed
Encapsulate CValue and CPlace creation
1 parent 18b78d1 commit d6e07cf

File tree

7 files changed

+54
-30
lines changed

7 files changed

+54
-30
lines changed

src/abi.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,9 @@ impl<'a, 'tcx: 'a, B: Backend + 'a> FunctionCx<'a, 'tcx, B> {
223223
Some(self.clif_type(return_ty).unwrap())
224224
};
225225
if let Some(val) = self.lib_call(name, input_tys, return_ty, &args) {
226-
CValue::ByVal(val, return_layout)
226+
CValue::by_val(val, return_layout)
227227
} else {
228-
CValue::ByRef(
228+
CValue::by_ref(
229229
self.bcx
230230
.ins()
231231
.iconst(self.pointer_type, self.pointer_type.bytes() as i64),
@@ -291,9 +291,7 @@ fn local_place<'a, 'tcx: 'a>(
291291
is_ssa: bool,
292292
) -> CPlace<'tcx> {
293293
let place = if is_ssa {
294-
fx.bcx
295-
.declare_var(mir_var(local), fx.clif_type(layout.ty).unwrap());
296-
CPlace::Var(local, layout)
294+
CPlace::new_var(fx, local, layout)
297295
} else {
298296
let place = CPlace::new_stack_slot(fx, layout.ty);
299297

@@ -372,8 +370,8 @@ fn cvalue_for_param<'a, 'tcx: 'a>(
372370

373371
match pass_mode {
374372
PassMode::NoPass => unreachable!(),
375-
PassMode::ByVal(_) => Some(CValue::ByVal(ebb_param, layout)),
376-
PassMode::ByRef => Some(CValue::ByRef(ebb_param, layout)),
373+
PassMode::ByVal(_) => Some(CValue::by_val(ebb_param, layout)),
374+
PassMode::ByRef => Some(CValue::by_ref(ebb_param, layout)),
377375
}
378376
}
379377

@@ -460,7 +458,7 @@ pub fn codegen_fn_prelude<'a, 'tcx: 'a>(
460458
match output_pass_mode {
461459
PassMode::NoPass => {
462460
fx.local_map
463-
.insert(RETURN_PLACE, CPlace::NoPlace(ret_layout));
461+
.insert(RETURN_PLACE, CPlace::no_place(ret_layout));
464462
}
465463
PassMode::ByVal(_) => {
466464
let is_ssa = !ssa_analyzed
@@ -693,7 +691,7 @@ pub fn codegen_call_inner<'a, 'tcx: 'a>(
693691
PassMode::ByVal(_) => {
694692
if let Some(ret_place) = ret_place {
695693
let ret_val = fx.bcx.inst_results(call_inst)[0];
696-
ret_place.write_cvalue(fx, CValue::ByVal(ret_val, ret_layout));
694+
ret_place.write_cvalue(fx, CValue::by_val(ret_val, ret_layout));
697695
}
698696
}
699697
PassMode::ByRef => {}

src/base.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
483483
_ => unimplemented!("un op Neg for {:?}", layout.ty),
484484
},
485485
};
486-
lval.write_cvalue(fx, CValue::ByVal(res, layout));
486+
lval.write_cvalue(fx, CValue::by_val(res, layout));
487487
}
488488
Rvalue::Cast(CastKind::Pointer(PointerCast::ReifyFnPointer), operand, ty) => {
489489
let layout = fx.layout_of(ty);
@@ -497,7 +497,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
497497
.unwrap(),
498498
);
499499
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
500-
lval.write_cvalue(fx, CValue::ByVal(func_addr, layout));
500+
lval.write_cvalue(fx, CValue::by_val(func_addr, layout));
501501
}
502502
_ => bug!("Trying to ReifyFnPointer on non FnDef {:?}", ty),
503503
}
@@ -526,7 +526,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
526526
} else {
527527
// fat-ptr -> thin-ptr
528528
let (ptr, _extra) = operand.load_scalar_pair(fx);
529-
lval.write_cvalue(fx, CValue::ByVal(ptr, dest_layout))
529+
lval.write_cvalue(fx, CValue::by_val(ptr, dest_layout))
530530
}
531531
} else if let ty::Adt(adt_def, _substs) = from_ty.sty {
532532
// enum -> discriminant value
@@ -596,7 +596,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
596596
} else {
597597
unimpl!("rval misc {:?} {:?}", from_ty, to_ty)
598598
};
599-
lval.write_cvalue(fx, CValue::ByVal(res, dest_layout));
599+
lval.write_cvalue(fx, CValue::by_val(res, dest_layout));
600600
}
601601
}
602602
Rvalue::Cast(CastKind::Pointer(PointerCast::ClosureFnPointer(_)), operand, _ty) => {
@@ -611,7 +611,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
611611
);
612612
let func_ref = fx.get_function_ref(instance);
613613
let func_addr = fx.bcx.ins().func_addr(fx.pointer_type, func_ref);
614-
lval.write_cvalue(fx, CValue::ByVal(func_addr, lval.layout()));
614+
lval.write_cvalue(fx, CValue::by_val(func_addr, lval.layout()));
615615
}
616616
_ => {
617617
bug!("{} cannot be cast to a fn ptr", operand.layout().ty)
@@ -639,7 +639,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
639639
let place = trans_place(fx, place);
640640
let usize_layout = fx.layout_of(fx.tcx.types.usize);
641641
let len = codegen_array_len(fx, place);
642-
lval.write_cvalue(fx, CValue::ByVal(len, usize_layout));
642+
lval.write_cvalue(fx, CValue::by_val(len, usize_layout));
643643
}
644644
Rvalue::NullaryOp(NullOp::Box, content_ty) => {
645645
use rustc::middle::lang_items::ExchangeMallocFnLangItem;
@@ -666,7 +666,7 @@ fn trans_stmt<'a, 'tcx: 'a>(
666666
let func_ref = fx.get_function_ref(instance);
667667
let call = fx.bcx.ins().call(func_ref, &[llsize, llalign]);
668668
let ptr = fx.bcx.inst_results(call)[0];
669-
lval.write_cvalue(fx, CValue::ByVal(ptr, box_layout));
669+
lval.write_cvalue(fx, CValue::by_val(ptr, box_layout));
670670
}
671671
Rvalue::NullaryOp(NullOp::SizeOf, ty) => {
672672
assert!(lval
@@ -754,7 +754,7 @@ pub fn trans_get_discriminant<'a, 'tcx: 'a>(
754754
_ => false,
755755
};
756756
let val = clif_intcast(fx, lldiscr, fx.clif_type(dest_layout.ty).unwrap(), signed);
757-
return CValue::ByVal(val, dest_layout);
757+
return CValue::by_val(val, dest_layout);
758758
}
759759
layout::DiscriminantKind::Niche {
760760
dataful_variant,
@@ -777,7 +777,7 @@ pub fn trans_get_discriminant<'a, 'tcx: 'a>(
777777
.ins()
778778
.iconst(dest_clif_ty, dataful_variant.as_u32() as i64);
779779
let val = fx.bcx.ins().select(b, if_true, if_false);
780-
return CValue::ByVal(val, dest_layout);
780+
return CValue::by_val(val, dest_layout);
781781
} else {
782782
// Rebase from niche values to discriminant values.
783783
let delta = niche_start.wrapping_sub(niche_variants.start().as_u32() as u128);
@@ -795,7 +795,7 @@ pub fn trans_get_discriminant<'a, 'tcx: 'a>(
795795
.ins()
796796
.iconst(dest_clif_ty, dataful_variant.as_u32() as i64);
797797
let val = fx.bcx.ins().select(b, if_true, if_false);
798-
return CValue::ByVal(val, dest_layout);
798+
return CValue::by_val(val, dest_layout);
799799
}
800800
}
801801
}
@@ -810,20 +810,20 @@ macro_rules! binop_match {
810810
let ret_layout = $fx.layout_of($ret_ty);
811811

812812
let b = $fx.bcx.ins().icmp(IntCC::$cc, $lhs, $rhs);
813-
CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
813+
CValue::by_val($fx.bcx.ins().bint(types::I8, b), ret_layout)
814814
}};
815815
(@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, fcmp($cc:ident)) => {{
816816
assert_eq!($fx.tcx.types.bool, $ret_ty);
817817
let ret_layout = $fx.layout_of($ret_ty);
818818
let b = $fx.bcx.ins().fcmp(FloatCC::$cc, $lhs, $rhs);
819-
CValue::ByVal($fx.bcx.ins().bint(types::I8, b), ret_layout)
819+
CValue::by_val($fx.bcx.ins().bint(types::I8, b), ret_layout)
820820
}};
821821
(@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, custom(|| $body:expr)) => {{
822822
$body
823823
}};
824824
(@single $fx:expr, $bug_fmt:expr, $var:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $name:ident) => {{
825825
let ret_layout = $fx.layout_of($ret_ty);
826-
CValue::ByVal($fx.bcx.ins().$name($lhs, $rhs), ret_layout)
826+
CValue::by_val($fx.bcx.ins().$name($lhs, $rhs), ret_layout)
827827
}};
828828
(
829829
$fx:expr, $bin_op:expr, $signed:expr, $lhs:expr, $rhs:expr, $ret_ty:expr, $bug_fmt:expr;
@@ -1064,7 +1064,7 @@ fn trans_ptr_binop<'a, 'tcx: 'a>(
10641064
let ptr_diff = fx.bcx.ins().imul_imm(offset, pointee_size as i64);
10651065
let base_val = base.load_scalar(fx);
10661066
let res = fx.bcx.ins().iadd(base_val, ptr_diff);
1067-
return CValue::ByVal(res, base.layout());
1067+
return CValue::by_val(res, base.layout());
10681068
}
10691069

10701070
binop_match! {
@@ -1111,7 +1111,7 @@ fn trans_ptr_binop<'a, 'tcx: 'a>(
11111111

11121112
assert_eq!(fx.tcx.types.bool, ret_ty);
11131113
let ret_layout = fx.layout_of(ret_ty);
1114-
CValue::ByVal(fx.bcx.ins().bint(types::I8, res), ret_layout)
1114+
CValue::by_val(fx.bcx.ins().bint(types::I8, res), ret_layout)
11151115
}
11161116
}
11171117

src/common.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ pub enum CValue<'tcx> {
109109
}
110110

111111
impl<'tcx> CValue<'tcx> {
112+
pub fn by_ref(value: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
113+
CValue::ByRef(value, layout)
114+
}
115+
116+
pub fn by_val(value: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
117+
CValue::ByVal(value, layout)
118+
}
119+
120+
pub fn by_val_pair(value: Value, extra: Value, layout: TyLayout<'tcx>) -> CValue<'tcx> {
121+
CValue::ByValPair(value, extra, layout)
122+
}
123+
112124
pub fn layout(&self) -> TyLayout<'tcx> {
113125
match *self {
114126
CValue::ByRef(_, layout)
@@ -257,6 +269,10 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
257269
}
258270
}
259271

272+
pub fn no_place(layout: TyLayout<'tcx>) -> CPlace<'tcx> {
273+
CPlace::NoPlace(layout)
274+
}
275+
260276
pub fn new_stack_slot(
261277
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
262278
ty: Ty<'tcx>,
@@ -275,6 +291,16 @@ impl<'a, 'tcx: 'a> CPlace<'tcx> {
275291
CPlace::Stack(stack_slot, layout)
276292
}
277293

294+
pub fn new_var(
295+
fx: &mut FunctionCx<'a, 'tcx, impl Backend>,
296+
local: Local,
297+
layout: TyLayout<'tcx>,
298+
) -> CPlace<'tcx> {
299+
fx.bcx
300+
.declare_var(mir_var(local), fx.clif_type(layout.ty).unwrap());
301+
CPlace::Var(local, layout)
302+
}
303+
278304
pub fn to_cvalue(self, fx: &mut FunctionCx<'a, 'tcx, impl Backend>) -> CValue<'tcx> {
279305
match self {
280306
CPlace::Var(var, layout) => CValue::ByVal(fx.bcx.use_var(mir_var(var)), layout),

src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn trans_const_value<'a, 'tcx: 'a>(
125125
let bits = const_.val.try_to_bits(layout.size).unwrap();
126126
CValue::const_val(fx, ty, rustc::mir::interpret::sign_extend(bits, layout.size) as i128 as i64)
127127
}
128-
ty::FnDef(_def_id, _substs) => CValue::ByRef(
128+
ty::FnDef(_def_id, _substs) => CValue::by_ref(
129129
fx.bcx
130130
.ins()
131131
.iconst(fx.pointer_type, fx.pointer_type.bytes() as i64),

src/intrinsics.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
335335
assert_eq!(from.layout().ty, src_ty);
336336
let addr = from.force_stack(fx);
337337
let dst_layout = fx.layout_of(dst_ty);
338-
ret.write_cvalue(fx, CValue::ByRef(addr, dst_layout))
338+
ret.write_cvalue(fx, CValue::by_ref(addr, dst_layout))
339339
};
340340
init, () {
341341
if ret.layout().abi == Abi::Uninhabited {
@@ -422,7 +422,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
422422
_ if intrinsic.starts_with("atomic_load"), (c ptr) {
423423
let inner_layout =
424424
fx.layout_of(ptr.layout().ty.builtin_deref(true).unwrap().ty);
425-
let val = CValue::ByRef(ptr.load_scalar(fx), inner_layout);
425+
let val = CValue::by_ref(ptr.load_scalar(fx), inner_layout);
426426
ret.write_cvalue(fx, val);
427427
};
428428
_ if intrinsic.starts_with("atomic_store"), (v ptr, c val) {
@@ -451,7 +451,7 @@ pub fn codegen_intrinsic_call<'a, 'tcx: 'a>(
451451
// Write new
452452
fx.bcx.ins().store(MemFlags::new(), new, ptr, 0);
453453

454-
let ret_val = CValue::ByValPair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
454+
let ret_val = CValue::by_val_pair(old, fx.bcx.ins().bint(types::I8, is_eq), ret.layout());
455455
ret.write_cvalue(fx, ret_val);
456456
};
457457

src/trap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub fn trap_unreachable_ret_value<'tcx>(fx: &mut FunctionCx<'_, 'tcx, impl crane
4040
let true_ = fx.bcx.ins().iconst(types::I32, 1);
4141
fx.bcx.ins().trapnz(true_, TrapCode::User(!0));
4242
let zero = fx.bcx.ins().iconst(fx.pointer_type, 0);
43-
CValue::ByRef(zero, dest_layout)
43+
CValue::by_ref(zero, dest_layout)
4444
}
4545

4646
/// Trap code: user65535

src/unsize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ pub fn coerce_unsized_into<'a, 'tcx: 'a>(
101101
let base = src.load_scalar(fx);
102102
unsize_thin_ptr(fx, base, src_ty, dst_ty)
103103
};
104-
dst.write_cvalue(fx, CValue::ByValPair(base, info, dst.layout()));
104+
dst.write_cvalue(fx, CValue::by_val_pair(base, info, dst.layout()));
105105
};
106106
match (&src_ty.sty, &dst_ty.sty) {
107107
(&ty::Ref(..), &ty::Ref(..))

0 commit comments

Comments
 (0)