Skip to content

Commit f2fce30

Browse files
committed
rustc_trans: don't special-case Box<T> to have T* LLVM types.
1 parent c770397 commit f2fce30

File tree

6 files changed

+28
-40
lines changed

6 files changed

+28
-40
lines changed

src/librustc_trans/intrinsic.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
248248
},
249249
"volatile_store" => {
250250
let tp_ty = substs.type_at(0);
251-
let dst = args[0].deref(bcx.ccx);
251+
let dst = args[0].deref(bcx);
252252
if let OperandValue::Pair(a, b) = args[1].val {
253253
bcx.volatile_store(a, dst.project_field(bcx, 0).llval);
254254
bcx.volatile_store(b, dst.project_field(bcx, 1).llval);
@@ -394,7 +394,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
394394
},
395395

396396
"discriminant_value" => {
397-
args[0].deref(bcx.ccx).trans_get_discr(bcx, ret_ty)
397+
args[0].deref(bcx).trans_get_discr(bcx, ret_ty)
398398
}
399399

400400
"align_offset" => {
@@ -542,7 +542,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
542542

543543
"nontemporal_store" => {
544544
let tp_ty = substs.type_at(0);
545-
let dst = args[0].deref(bcx.ccx);
545+
let dst = args[0].deref(bcx);
546546
let val = if let OperandValue::Ref(ptr, align) = args[1].val {
547547
bcx.load(ptr, align)
548548
} else {

src/librustc_trans/mir/block.rs

+6-26
Original file line numberDiff line numberDiff line change
@@ -592,9 +592,12 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
592592

593593
self.trans_argument(&bcx, op, &mut llargs, &fn_ty.args[i]);
594594
}
595-
if let Some(tup) = untuple {
596-
self.trans_arguments_untupled(&bcx, tup, &mut llargs,
597-
&fn_ty.args[first_args.len()..])
595+
if let Some(tuple) = untuple {
596+
let tuple = self.trans_operand(&bcx, tuple);
597+
for (i, arg) in fn_ty.args[first_args.len()..].iter().enumerate() {
598+
let op = tuple.extract_field(&bcx, i);
599+
self.trans_argument(&bcx, op, &mut llargs, arg);
600+
}
598601
}
599602

600603
let fn_ptr = match (llfn, instance) {
@@ -692,29 +695,6 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
692695
llargs.push(llval);
693696
}
694697

695-
fn trans_arguments_untupled(&mut self,
696-
bcx: &Builder<'a, 'tcx>,
697-
operand: &mir::Operand<'tcx>,
698-
llargs: &mut Vec<ValueRef>,
699-
args: &[ArgType<'tcx>]) {
700-
let tuple = self.trans_operand(bcx, operand);
701-
702-
// Handle both by-ref and immediate tuples.
703-
if let Ref(llval, align) = tuple.val {
704-
let tuple_ptr = PlaceRef::new_sized(llval, tuple.layout, align);
705-
for i in 0..tuple.layout.fields.count() {
706-
let field_ptr = tuple_ptr.project_field(bcx, i);
707-
self.trans_argument(bcx, field_ptr.load(bcx), llargs, &args[i]);
708-
}
709-
} else {
710-
// If the tuple is immediate, the elements are as well.
711-
for i in 0..tuple.layout.fields.count() {
712-
let op = tuple.extract_field(bcx, i);
713-
self.trans_argument(bcx, op, llargs, &args[i]);
714-
}
715-
}
716-
}
717-
718698
fn get_personality_slot(&mut self, bcx: &Builder<'a, 'tcx>) -> PlaceRef<'tcx> {
719699
let ccx = bcx.ccx;
720700
if let Some(slot) = self.personality_slot {

src/librustc_trans/mir/operand.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,24 @@ impl<'a, 'tcx> OperandRef<'tcx> {
9999
}
100100
}
101101

102-
pub fn deref(self, ccx: &CrateContext<'a, 'tcx>) -> PlaceRef<'tcx> {
102+
pub fn deref(mut self, bcx: &Builder<'a, 'tcx>) -> PlaceRef<'tcx> {
103+
if let ty::TyAdt(def, _) = self.layout.ty.sty {
104+
if def.is_box() {
105+
// HACK(eddyb) Get the raw pointer, assuming it's always
106+
// the first field of the first field of ... of `struct Box`.
107+
while !self.layout.ty.is_unsafe_ptr() {
108+
self = self.extract_field(bcx, 0);
109+
}
110+
}
111+
}
103112
let projected_ty = self.layout.ty.builtin_deref(true, ty::NoPreference)
104113
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", self)).ty;
105114
let (llptr, llextra) = match self.val {
106115
OperandValue::Immediate(llptr) => (llptr, ptr::null_mut()),
107116
OperandValue::Pair(llptr, llextra) => (llptr, llextra),
108117
OperandValue::Ref(..) => bug!("Deref of by-Ref operand {:?}", self)
109118
};
110-
let layout = ccx.layout_of(projected_ty);
119+
let layout = bcx.ccx.layout_of(projected_ty);
111120
PlaceRef {
112121
llval: llptr,
113122
llextra,
@@ -152,6 +161,12 @@ impl<'a, 'tcx> OperandRef<'tcx> {
152161
}
153162

154163
pub fn extract_field(&self, bcx: &Builder<'a, 'tcx>, i: usize) -> OperandRef<'tcx> {
164+
// For indirect values, we can just go through `PlaceRef::project_field`.
165+
if let OperandValue::Ref(ptr, align) = self.val {
166+
let place = PlaceRef::new_sized(ptr, self.layout, align);
167+
return place.project_field(bcx, i).load(bcx);
168+
}
169+
155170
let field = self.layout.field(bcx.ccx, i);
156171
let offset = self.layout.fields.offset(i);
157172

src/librustc_trans/mir/place.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
431431
elem: mir::ProjectionElem::Deref
432432
}) => {
433433
// Load the pointer from its location.
434-
self.trans_consume(bcx, base).deref(bcx.ccx)
434+
self.trans_consume(bcx, base).deref(bcx)
435435
}
436436
mir::Place::Projection(ref projection) => {
437437
let tr_base = self.trans_place(bcx, &projection.base);

src/librustc_trans/type_of.rs

-7
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,6 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> {
252252
ty::TyRawPtr(ty::TypeAndMut { ty, .. }) => {
253253
ccx.layout_of(ty).llvm_type(ccx).ptr_to()
254254
}
255-
ty::TyAdt(def, _) if def.is_box() => {
256-
ccx.layout_of(self.ty.boxed_ty()).llvm_type(ccx).ptr_to()
257-
}
258255
ty::TyFnPtr(sig) => {
259256
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&sig);
260257
FnType::new(ccx, sig, &[]).llvm_type(ccx).ptr_to()
@@ -341,10 +338,6 @@ impl<'tcx> LayoutLlvmExt<'tcx> for TyLayout<'tcx> {
341338
ty::TyRawPtr(_) => {
342339
return self.field(ccx, index).llvm_type(ccx);
343340
}
344-
ty::TyAdt(def, _) if def.is_box() => {
345-
let ptr_ty = ccx.tcx().mk_mut_ptr(self.ty.boxed_ty());
346-
return ccx.layout_of(ptr_ty).scalar_pair_element_llvm_type(ccx, index);
347-
}
348341
_ => {}
349342
}
350343

src/test/codegen/function-arguments.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub fn str(_: &[u8]) {
128128
pub fn trait_borrow(_: &Drop) {
129129
}
130130

131-
// CHECK: @trait_box(%"core::ops::drop::Drop"* noalias nonnull, {}* noalias nonnull readonly)
131+
// CHECK: @trait_box(i8* noalias nonnull, i8* noalias nonnull readonly)
132132
#[no_mangle]
133133
pub fn trait_box(_: Box<Drop>) {
134134
}

0 commit comments

Comments
 (0)