Skip to content

Commit 760ddb3

Browse files
committed
auto merge of #11723 : eddyb/rust/more-trans-cleanup, r=pcwalton
2 parents feacb59 + e81ab41 commit 760ddb3

File tree

8 files changed

+62
-146
lines changed

8 files changed

+62
-146
lines changed

src/librustc/back/abi.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ pub static general_code_alignment: uint = 16u;
4242

4343
pub static tydesc_field_size: uint = 0u;
4444
pub static tydesc_field_align: uint = 1u;
45-
pub static tydesc_field_take_glue: uint = 2u;
46-
pub static tydesc_field_drop_glue: uint = 3u;
47-
pub static tydesc_field_visit_glue: uint = 4u;
48-
pub static tydesc_field_name_offset: uint = 5u;
49-
pub static n_tydesc_fields: uint = 6u;
45+
pub static tydesc_field_drop_glue: uint = 2u;
46+
pub static tydesc_field_visit_glue: uint = 3u;
47+
pub static tydesc_field_name_offset: uint = 4u;
48+
pub static n_tydesc_fields: uint = 5u;
5049

5150
// The two halves of a closure: code and environment.
5251
pub static fn_field_code: uint = 0u;

src/librustc/middle/trans/base.rs

+16-24
Original file line numberDiff line numberDiff line change
@@ -777,16 +777,16 @@ pub fn iter_structural_ty<'r,
777777
let variant_cx =
778778
fcx.new_temp_block(~"enum-iter-variant-" +
779779
variant.disr_val.to_str());
780-
let variant_cx =
781-
iter_variant(variant_cx, repr, av, *variant,
782-
substs.tps, |x,y,z| f(x,y,z));
783780
match adt::trans_case(cx, repr, variant.disr_val) {
784781
_match::single_result(r) => {
785782
AddCase(llswitch, r.val, variant_cx.llbb)
786783
}
787784
_ => ccx.sess.unimpl("value from adt::trans_case \
788785
in iter_structural_ty")
789786
}
787+
let variant_cx =
788+
iter_variant(variant_cx, repr, av, *variant,
789+
substs.tps, |x,y,z| f(x,y,z));
790790
Br(variant_cx, next_cx.llbb);
791791
}
792792
cx = next_cx;
@@ -1458,16 +1458,16 @@ pub fn build_return_block(fcx: &FunctionContext, ret_cx: &Block) {
14581458
// trans_closure: Builds an LLVM function out of a source function.
14591459
// If the function closes over its environment a closure will be
14601460
// returned.
1461-
pub fn trans_closure(ccx: @CrateContext,
1462-
path: ast_map::Path,
1463-
decl: &ast::FnDecl,
1464-
body: &ast::Block,
1465-
llfndecl: ValueRef,
1466-
param_substs: Option<@param_substs>,
1467-
id: ast::NodeId,
1468-
_attributes: &[ast::Attribute],
1469-
output_type: ty::t,
1470-
maybe_load_env: |&FunctionContext|) {
1461+
pub fn trans_closure<'a>(ccx: @CrateContext,
1462+
path: ast_map::Path,
1463+
decl: &ast::FnDecl,
1464+
body: &ast::Block,
1465+
llfndecl: ValueRef,
1466+
param_substs: Option<@param_substs>,
1467+
id: ast::NodeId,
1468+
_attributes: &[ast::Attribute],
1469+
output_type: ty::t,
1470+
maybe_load_env: |&'a Block<'a>| -> &'a Block<'a>) {
14711471
ccx.stats.n_closures.set(ccx.stats.n_closures.get() + 1);
14721472

14731473
let _icx = push_ctxt("trans_closure");
@@ -1500,7 +1500,7 @@ pub fn trans_closure(ccx: @CrateContext,
15001500

15011501
bcx = copy_args_to_allocas(&fcx, arg_scope, bcx, decl.inputs, arg_datums);
15021502

1503-
maybe_load_env(&fcx);
1503+
bcx = maybe_load_env(bcx);
15041504

15051505
// Up until here, IR instructions for this function have explicitly not been annotated with
15061506
// source code location, so we don't step into call setup code. From here on, source location
@@ -1558,16 +1558,8 @@ pub fn trans_fn(ccx: @CrateContext,
15581558
debug!("trans_fn(param_substs={})", param_substs.repr(ccx.tcx));
15591559
let _icx = push_ctxt("trans_fn");
15601560
let output_type = ty::ty_fn_ret(ty::node_id_to_type(ccx.tcx, id));
1561-
trans_closure(ccx,
1562-
path.clone(),
1563-
decl,
1564-
body,
1565-
llfndecl,
1566-
param_substs,
1567-
id,
1568-
attrs,
1569-
output_type,
1570-
|_fcx| { });
1561+
trans_closure(ccx, path.clone(), decl, body, llfndecl,
1562+
param_substs, id, attrs, output_type, |bcx| bcx);
15711563
}
15721564

15731565
pub fn trans_enum_variant(ccx: @CrateContext,

src/librustc/middle/trans/closure.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -282,23 +282,22 @@ fn build_closure<'a>(bcx0: &'a Block<'a>,
282282
// Given an enclosing block context, a new function context, a closure type,
283283
// and a list of upvars, generate code to load and populate the environment
284284
// with the upvars and type descriptors.
285-
fn load_environment(fcx: &FunctionContext, cdata_ty: ty::t,
286-
cap_vars: &[moves::CaptureVar], sigil: ast::Sigil) {
285+
fn load_environment<'a>(bcx: &'a Block<'a>, cdata_ty: ty::t,
286+
cap_vars: &[moves::CaptureVar],
287+
sigil: ast::Sigil) -> &'a Block<'a> {
287288
let _icx = push_ctxt("closure::load_environment");
288289

289290
// Don't bother to create the block if there's nothing to load
290291
if cap_vars.len() == 0 {
291-
return;
292+
return bcx;
292293
}
293294

294-
let bcx = fcx.entry_bcx.get().unwrap();
295-
296295
// Load a pointer to the closure data, skipping over the box header:
297-
let llcdata = at_box_body(bcx, cdata_ty, fcx.llenv.unwrap());
296+
let llcdata = at_box_body(bcx, cdata_ty, bcx.fcx.llenv.unwrap());
298297

299298
// Store the pointer to closure data in an alloca for debug info because that's what the
300299
// llvm.dbg.declare intrinsic expects
301-
let env_pointer_alloca = if fcx.ccx.sess.opts.extra_debuginfo {
300+
let env_pointer_alloca = if bcx.ccx().sess.opts.extra_debuginfo {
302301
let alloc = alloc_ty(bcx, ty::mk_mut_ptr(bcx.tcx(), cdata_ty), "__debuginfo_env_ptr");
303302
Store(bcx, llcdata, alloc);
304303
Some(alloc)
@@ -317,7 +316,7 @@ fn load_environment(fcx: &FunctionContext, cdata_ty: ty::t,
317316
let def_id = ast_util::def_id_of_def(cap_var.def);
318317

319318
{
320-
let mut llupvars = fcx.llupvars.borrow_mut();
319+
let mut llupvars = bcx.fcx.llupvars.borrow_mut();
321320
llupvars.get().insert(def_id.node, upvarptr);
322321
}
323322

@@ -334,6 +333,8 @@ fn load_environment(fcx: &FunctionContext, cdata_ty: ty::t,
334333

335334
i += 1u;
336335
}
336+
337+
bcx
337338
}
338339

339340
fn fill_fn_pair(bcx: &Block, pair: ValueRef, llfn: ValueRef, llenvptr: ValueRef) {
@@ -405,7 +406,7 @@ pub fn trans_expr_fn<'a>(
405406
trans_closure(ccx, sub_path, decl, body, llfn,
406407
bcx.fcx.param_substs, user_id,
407408
[], ty::ty_fn_ret(fty),
408-
|fcx| load_environment(fcx, cdata_ty, cap_vars, sigil));
409+
|bcx| load_environment(bcx, cdata_ty, cap_vars, sigil));
409410
fill_fn_pair(bcx, dest_addr, llfn, llbox);
410411

411412
bcx

src/librustc/middle/trans/common.rs

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ pub struct tydesc_info {
114114
size: ValueRef,
115115
align: ValueRef,
116116
name: ValueRef,
117-
take_glue: Cell<Option<ValueRef>>,
118117
drop_glue: Cell<Option<ValueRef>>,
119118
visit_glue: Cell<Option<ValueRef>>,
120119
}

src/librustc/middle/trans/datum.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ impl Datum<Expr> {
491491
}
492492
ByValue => {
493493
let v = load(bcx, l.val, l.ty);
494-
l.kind.post_store(bcx, l.val, l.ty);
494+
bcx = l.kind.post_store(bcx, l.val, l.ty);
495495
DatumBlock(bcx, Datum(v, l.ty, Rvalue(ByValue)))
496496
}
497497
}

src/librustc/middle/trans/glue.rs

+29-105
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,24 @@ pub fn trans_exchange_free<'a>(cx: &'a Block<'a>, v: ValueRef)
5858
Some(expr::Ignore)).bcx
5959
}
6060

61-
pub fn take_ty<'a>(cx: &'a Block<'a>, v: ValueRef, t: ty::t)
61+
pub fn take_ty<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
6262
-> &'a Block<'a> {
6363
// NB: v is an *alias* of type t here, not a direct value.
6464
let _icx = push_ctxt("take_ty");
65-
if ty::type_needs_drop(cx.tcx(), t) {
66-
return call_tydesc_glue(cx, v, t, abi::tydesc_field_take_glue);
65+
match ty::get(t).sty {
66+
ty::ty_box(_) |
67+
ty::ty_vec(_, ty::vstore_box) | ty::ty_str(ty::vstore_box) => {
68+
incr_refcnt_of_boxed(bcx, v)
69+
}
70+
ty::ty_trait(_, _, ty::BoxTraitStore, _, _) => {
71+
incr_refcnt_of_boxed(bcx, GEPi(bcx, v, [0u, abi::trt_field_box]))
72+
}
73+
_ if ty::type_is_structural(t)
74+
&& ty::type_needs_drop(bcx.tcx(), t) => {
75+
iter_structural_ty(bcx, v, t, take_ty)
76+
}
77+
_ => bcx
6778
}
68-
return cx;
6979
}
7080

7181
pub fn drop_ty<'a>(cx: &'a Block<'a>, v: ValueRef, t: ty::t)
@@ -88,30 +98,15 @@ pub fn drop_ty_immediate<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t)
8898

8999
pub fn lazily_emit_all_tydesc_glue(ccx: @CrateContext,
90100
static_ti: @tydesc_info) {
91-
lazily_emit_tydesc_glue(ccx, abi::tydesc_field_take_glue, static_ti);
92101
lazily_emit_tydesc_glue(ccx, abi::tydesc_field_drop_glue, static_ti);
93102
lazily_emit_tydesc_glue(ccx, abi::tydesc_field_visit_glue, static_ti);
94103
}
95104

96105
fn simplified_glue_type(tcx: ty::ctxt, field: uint, t: ty::t) -> ty::t {
97-
if (field == abi::tydesc_field_take_glue || field == abi::tydesc_field_drop_glue)
98-
&& !ty::type_needs_drop(tcx, t) {
99-
return ty::mk_nil();
100-
}
101-
102-
if field == abi::tydesc_field_take_glue {
103-
match ty::get(t).sty {
104-
ty::ty_str(ty::vstore_uniq) | ty::ty_vec(_, ty::vstore_uniq) |
105-
ty::ty_unboxed_vec(..) | ty::ty_uniq(..) => return ty::mk_nil(),
106-
_ => {}
107-
}
108-
}
109-
110-
if field == abi::tydesc_field_take_glue && ty::type_is_boxed(t) {
111-
return ty::mk_box(tcx, ty::mk_nil());
112-
}
113-
114106
if field == abi::tydesc_field_drop_glue {
107+
if !ty::type_needs_drop(tcx, t) {
108+
return ty::mk_nil();
109+
}
115110
match ty::get(t).sty {
116111
ty::ty_box(typ)
117112
if !ty::type_needs_drop(tcx, typ) =>
@@ -145,9 +140,7 @@ fn lazily_emit_tydesc_glue(ccx: @CrateContext, field: uint, ti: @tydesc_info) {
145140
let simpl_ti = get_tydesc(ccx, simpl);
146141
lazily_emit_tydesc_glue(ccx, field, simpl_ti);
147142

148-
if field == abi::tydesc_field_take_glue {
149-
ti.take_glue.set(simpl_ti.take_glue.get());
150-
} else if field == abi::tydesc_field_drop_glue {
143+
if field == abi::tydesc_field_drop_glue {
151144
ti.drop_glue.set(simpl_ti.drop_glue.get());
152145
} else if field == abi::tydesc_field_visit_glue {
153146
ti.visit_glue.set(simpl_ti.visit_glue.get());
@@ -158,20 +151,7 @@ fn lazily_emit_tydesc_glue(ccx: @CrateContext, field: uint, ti: @tydesc_info) {
158151

159152
let llfnty = Type::glue_fn(type_of(ccx, ti.ty).ptr_to());
160153

161-
if field == abi::tydesc_field_take_glue {
162-
match ti.take_glue.get() {
163-
Some(_) => (),
164-
None => {
165-
debug!("+++ lazily_emit_tydesc_glue TAKE {}",
166-
ppaux::ty_to_str(ccx.tcx, ti.ty));
167-
let glue_fn = declare_generic_glue(ccx, ti.ty, llfnty, "take");
168-
ti.take_glue.set(Some(glue_fn));
169-
make_generic_glue(ccx, ti.ty, glue_fn, make_take_glue, "take");
170-
debug!("--- lazily_emit_tydesc_glue TAKE {}",
171-
ppaux::ty_to_str(ccx.tcx, ti.ty));
172-
}
173-
}
174-
} else if field == abi::tydesc_field_drop_glue {
154+
if field == abi::tydesc_field_drop_glue {
175155
match ti.drop_glue.get() {
176156
Some(_) => (),
177157
None => {
@@ -213,9 +193,7 @@ pub fn call_tydesc_glue_full(bcx: &Block, v: ValueRef, tydesc: ValueRef,
213193
None => None,
214194
Some(sti) => {
215195
lazily_emit_tydesc_glue(ccx, field, sti);
216-
if field == abi::tydesc_field_take_glue {
217-
sti.take_glue.get()
218-
} else if field == abi::tydesc_field_drop_glue {
196+
if field == abi::tydesc_field_drop_glue {
219197
sti.drop_glue.get()
220198
} else if field == abi::tydesc_field_visit_glue {
221199
sti.visit_glue.get()
@@ -472,53 +450,16 @@ fn decr_refcnt_maybe_free<'a>(bcx: &'a Block<'a>, box_ptr_ptr: ValueRef,
472450
next_bcx
473451
}
474452

475-
fn make_take_glue<'a>(bcx: &'a Block<'a>, v: ValueRef, t: ty::t) -> &'a Block<'a> {
476-
let _icx = push_ctxt("make_take_glue");
477-
// NB: v is a *pointer* to type t here, not a direct value.
478-
match ty::get(t).sty {
479-
ty::ty_box(_) |
480-
ty::ty_vec(_, ty::vstore_box) | ty::ty_str(ty::vstore_box) => {
481-
incr_refcnt_of_boxed(bcx, Load(bcx, v)); bcx
482-
}
483-
ty::ty_vec(_, ty::vstore_slice(_))
484-
| ty::ty_str(ty::vstore_slice(_)) => {
485-
bcx
486-
}
487-
ty::ty_closure(_) => bcx,
488-
ty::ty_trait(_, _, ty::BoxTraitStore, _, _) => {
489-
let llbox = Load(bcx, GEPi(bcx, v, [0u, abi::trt_field_box]));
490-
incr_refcnt_of_boxed(bcx, llbox);
491-
bcx
492-
}
493-
ty::ty_trait(_, _, ty::UniqTraitStore, _, _) => {
494-
let lluniquevalue = GEPi(bcx, v, [0, abi::trt_field_box]);
495-
let llvtable = Load(bcx, GEPi(bcx, v, [0, abi::trt_field_vtable]));
496-
497-
// Cast the vtable to a pointer to a pointer to a tydesc.
498-
let llvtable = PointerCast(bcx, llvtable,
499-
bcx.ccx().tydesc_type.ptr_to().ptr_to());
500-
let lltydesc = Load(bcx, llvtable);
501-
call_tydesc_glue_full(bcx,
502-
lluniquevalue,
503-
lltydesc,
504-
abi::tydesc_field_take_glue,
505-
None);
506-
bcx
507-
}
508-
_ if ty::type_is_structural(t) => {
509-
iter_structural_ty(bcx, v, t, take_ty)
510-
}
511-
_ => bcx
512-
}
513-
}
514-
515-
fn incr_refcnt_of_boxed(cx: &Block, box_ptr: ValueRef) {
453+
fn incr_refcnt_of_boxed<'a>(bcx: &'a Block<'a>,
454+
box_ptr_ptr: ValueRef) -> &'a Block<'a> {
516455
let _icx = push_ctxt("incr_refcnt_of_boxed");
517-
let ccx = cx.ccx();
518-
let rc_ptr = GEPi(cx, box_ptr, [0u, abi::box_field_refcnt]);
519-
let rc = Load(cx, rc_ptr);
520-
let rc = Add(cx, rc, C_int(ccx, 1));
521-
Store(cx, rc, rc_ptr);
456+
let ccx = bcx.ccx();
457+
let box_ptr = Load(bcx, box_ptr_ptr);
458+
let rc_ptr = GEPi(bcx, box_ptr, [0u, abi::box_field_refcnt]);
459+
let rc = Load(bcx, rc_ptr);
460+
let rc = Add(bcx, rc, C_int(ccx, 1));
461+
Store(bcx, rc, rc_ptr);
462+
bcx
522463
}
523464

524465

@@ -554,7 +495,6 @@ pub fn declare_tydesc(ccx: &CrateContext, t: ty::t) -> @tydesc_info {
554495
size: llsize,
555496
align: llalign,
556497
name: ty_name,
557-
take_glue: Cell::new(None),
558498
drop_glue: Cell::new(None),
559499
visit_glue: Cell::new(None),
560500
};
@@ -616,21 +556,6 @@ pub fn emit_tydescs(ccx: &CrateContext) {
616556
// before being put into the tydesc because we only have a singleton
617557
// tydesc type. Then we'll recast each function to its real type when
618558
// calling it.
619-
let take_glue =
620-
match ti.take_glue.get() {
621-
None => {
622-
ccx.stats.n_null_glues.set(ccx.stats.n_null_glues.get() +
623-
1);
624-
C_null(glue_fn_ty)
625-
}
626-
Some(v) => {
627-
unsafe {
628-
ccx.stats.n_real_glues.set(ccx.stats.n_real_glues.get() +
629-
1);
630-
llvm::LLVMConstPointerCast(v, glue_fn_ty.to_ref())
631-
}
632-
}
633-
};
634559
let drop_glue =
635560
match ti.drop_glue.get() {
636561
None => {
@@ -665,7 +590,6 @@ pub fn emit_tydescs(ccx: &CrateContext) {
665590
let tydesc = C_named_struct(ccx.tydesc_type,
666591
[ti.size, // size
667592
ti.align, // align
668-
take_glue, // take_glue
669593
drop_glue, // drop_glue
670594
visit_glue, // visit_glue
671595
ti.name]); // name

src/librustc/middle/trans/type_.rs

-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ impl Type {
211211

212212
let elems = [int_ty, // size
213213
int_ty, // align
214-
glue_fn_ty, // take
215214
glue_fn_ty, // drop
216215
glue_fn_ty, // visit
217216
Type::struct_([Type::i8p(), Type::int(arch)], false)]; // name

src/libstd/unstable/intrinsics.rs

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub struct TyDesc {
5757
align: uint,
5858

5959
// Called on a copy of a value of type `T` *after* memcpy
60+
// NOTE remove after next snapshot
61+
#[cfg(stage0)]
6062
take_glue: GlueFn,
6163

6264
// Called when a value of type `T` is no longer needed

0 commit comments

Comments
 (0)