Skip to content

Commit 0b7af40

Browse files
committed
rustc: Use explicit return blocks
1 parent df51fe3 commit 0b7af40

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

src/comp/middle/trans.rs

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,8 +1269,7 @@ fn make_copy_glue(cx: &@block_ctxt, v: ValueRef, t: &ty::t) {
12691269
bcx = iter_structural_ty(bcx, v, t, bind copy_ty(_, _, _)).bcx;
12701270
} else { bcx = cx; }
12711271

1272-
bcx = trans_fn_cleanups(bcx);
1273-
bcx.build.RetVoid();
1272+
build_return(bcx);
12741273
}
12751274

12761275
fn incr_refcnt_of_boxed(cx: &@block_ctxt, box_ptr: ValueRef) -> result {
@@ -1376,8 +1375,7 @@ fn make_free_glue(cx: &@block_ctxt, v0: ValueRef, t: &ty::t) {
13761375
_ { rslt(cx, C_nil()) }
13771376
};
13781377

1379-
let bcx = trans_fn_cleanups(rs.bcx);
1380-
bcx.build.RetVoid();
1378+
build_return(rs.bcx);
13811379
}
13821380

13831381
fn maybe_free_ivec_heap_part(cx: &@block_ctxt, v0: ValueRef, unit_ty: ty::t)
@@ -1446,8 +1444,7 @@ fn make_drop_glue(cx: &@block_ctxt, v0: ValueRef, t: &ty::t) {
14461444
}
14471445
};
14481446

1449-
let bcx = trans_fn_cleanups(rs.bcx);
1450-
bcx.build.RetVoid();
1447+
build_return(rs.bcx);
14511448
}
14521449

14531450
fn trans_res_drop(cx: @block_ctxt, rs: ValueRef, did: &ast::def_id,
@@ -3727,8 +3724,7 @@ fn trans_for_each(cx: &@block_ctxt, local: &@ast::local, seq: &@ast::expr,
37273724

37283725
if !r.bcx.build.is_terminated() {
37293726
// if terminated is true, no need for the ret-fail
3730-
let bcx = trans_fn_cleanups(r.bcx);
3731-
bcx.build.RetVoid();
3727+
build_return(r.bcx);
37323728
}
37333729

37343730
// Step 3: Call iter passing [lliterbody, llenv], plus other args.
@@ -4407,7 +4403,7 @@ fn trans_bind_thunk(cx: &@local_ctxt, sp: &span, incoming_fty: &ty::t,
44074403
lltargetfn = bcx.build.PointerCast(lltargetfn, T_ptr(T_ptr(lltargetty)));
44084404
lltargetfn = bcx.build.Load(lltargetfn);
44094405
llvm::LLVMSetTailCall(bcx.build.FastCall(lltargetfn, llargs), 1);
4410-
bcx.build.RetVoid();
4406+
build_return(bcx);
44114407
finish_fn(fcx, lltop);
44124408
ret {val: llthunk, ty: llthunk_ty};
44134409
}
@@ -5448,11 +5444,14 @@ fn trans_ret(cx: &@block_ctxt, e: &option::t<@ast::expr>) -> result {
54485444
parent_none. { more_cleanups = false; }
54495445
}
54505446
}
5451-
bcx = trans_fn_cleanups(bcx);
5452-
bcx.build.RetVoid();
5447+
build_return(bcx);
54535448
ret rslt(new_sub_block_ctxt(bcx, "ret.unreachable"), C_nil());
54545449
}
54555450

5451+
fn build_return(bcx: &@block_ctxt) {
5452+
bcx.build.Br(bcx_fcx(bcx).llreturn);
5453+
}
5454+
54565455
fn trans_be(cx: &@block_ctxt, e: &@ast::expr) -> result {
54575456
// FIXME: This should be a typestate precondition
54585457

@@ -5610,16 +5609,15 @@ fn trans_block_cleanups(cx: &@block_ctxt, cleanup_cx: &@block_ctxt) ->
56105609
ret bcx;
56115610
}
56125611

5613-
fn trans_fn_cleanups(bcx: &@block_ctxt) -> @block_ctxt {
5614-
alt bcx_fcx(bcx).llobstacktoken {
5612+
fn trans_fn_cleanups(fcx: &@fn_ctxt, build: &lib::llvm::builder) {
5613+
alt fcx.llobstacktoken {
56155614
some(lltoken_) {
56165615
let lltoken = lltoken_; // satisfy alias checker
5617-
bcx.build.Call(bcx_ccx(bcx).upcalls.dynastack_free,
5618-
~[bcx_fcx(bcx).lltaskptr, lltoken]);
5616+
build.Call(fcx_ccx(fcx).upcalls.dynastack_free, ~[fcx.lltaskptr,
5617+
lltoken]);
56195618
}
56205619
none. { /* nothing to do */ }
56215620
}
5622-
ret bcx;
56235621
}
56245622

56255623
iter block_locals(b: &ast::blk) -> @ast::local {
@@ -5778,11 +5776,13 @@ fn mk_standard_basic_blocks(llfn: ValueRef) ->
57785776
{sa: BasicBlockRef,
57795777
ca: BasicBlockRef,
57805778
dt: BasicBlockRef,
5781-
da: BasicBlockRef} {
5779+
da: BasicBlockRef,
5780+
rt: BasicBlockRef} {
57825781
ret {sa: llvm::LLVMAppendBasicBlock(llfn, str::buf("static_allocas")),
57835782
ca: llvm::LLVMAppendBasicBlock(llfn, str::buf("copy_args")),
57845783
dt: llvm::LLVMAppendBasicBlock(llfn, str::buf("derived_tydescs")),
5785-
da: llvm::LLVMAppendBasicBlock(llfn, str::buf("dynamic_allocas"))};
5784+
da: llvm::LLVMAppendBasicBlock(llfn, str::buf("dynamic_allocas")),
5785+
rt: llvm::LLVMAppendBasicBlock(llfn, str::buf("return"))};
57865786
}
57875787

57885788

@@ -5816,6 +5816,7 @@ fn new_fn_ctxt_w_id(cx: @local_ctxt, sp: &span, llfndecl: ValueRef,
58165816
mutable llderivedtydescs_first: llbbs.dt,
58175817
mutable llderivedtydescs: llbbs.dt,
58185818
mutable lldynamicallocas: llbbs.da,
5819+
mutable llreturn: llbbs.rt,
58195820
mutable llobstacktoken: none::<ValueRef>,
58205821
mutable llself: none::<val_self_pair>,
58215822
mutable lliterbody: none::<ValueRef>,
@@ -5998,12 +5999,16 @@ fn populate_fn_ctxt_from_llself(fcx: @fn_ctxt, llself: val_self_pair) {
59985999

59996000

60006001
// Ties up the llstaticallocas -> llcopyargs -> llderivedtydescs ->
6001-
// lldynamicallocas -> lltop edges.
6002+
// lldynamicallocas -> lltop edges, and builds the return block.
60026003
fn finish_fn(fcx: &@fn_ctxt, lltop: BasicBlockRef) {
60036004
new_builder(fcx.llstaticallocas).Br(fcx.llcopyargs);
60046005
new_builder(fcx.llcopyargs).Br(fcx.llderivedtydescs_first);
60056006
new_builder(fcx.llderivedtydescs).Br(fcx.lldynamicallocas);
60066007
new_builder(fcx.lldynamicallocas).Br(lltop);
6008+
6009+
let ret_builder = new_builder(fcx.llreturn);
6010+
trans_fn_cleanups(fcx, ret_builder);
6011+
ret_builder.RetVoid();
60076012
}
60086013

60096014
// trans_closure: Builds an LLVM function out of a source function.
@@ -6070,8 +6075,7 @@ fn trans_closure(bcx_maybe: &option::t<@block_ctxt>,
60706075
if !is_terminated(bcx) {
60716076
// FIXME: until LLVM has a unit type, we are moving around
60726077
// C_nil values rather than their void type.
6073-
bcx = trans_fn_cleanups(bcx);
6074-
bcx.build.RetVoid();
6078+
build_return(bcx);
60756079
}
60766080

60776081
// Insert the mandatory first few basic blocks before lltop.
@@ -6137,7 +6141,7 @@ fn trans_res_ctor(cx: @local_ctxt, sp: &span, dtor: &ast::_fn,
61376141
let flag = GEP_tup_like(bcx, tup_t, llretptr, ~[0, 0]);
61386142
bcx = flag.bcx;
61396143
bcx.build.Store(C_int(1), flag.val);
6140-
bcx.build.RetVoid();
6144+
build_return(bcx);
61416145
finish_fn(fcx, lltop);
61426146
}
61436147

@@ -6227,8 +6231,7 @@ fn trans_tag_variant(cx: @local_ctxt, tag_id: ast::node_id,
62276231
i += 1u;
62286232
}
62296233
bcx = trans_block_cleanups(bcx, find_scope_cx(bcx));
6230-
bcx = trans_fn_cleanups(bcx);
6231-
bcx.build.RetVoid();
6234+
build_return(bcx);
62326235
finish_fn(fcx, lltop);
62336236
}
62346237

@@ -6433,7 +6436,7 @@ fn create_main_wrapper(ccx: &@crate_ctxt, sp: &span,
64336436
};
64346437
bcx.build.FastCall(main_llfn, args);
64356438
}
6436-
bcx.build.RetVoid();
6439+
build_return(bcx);
64376440

64386441
let lltop = bcx.llbb;
64396442
finish_fn(fcx, lltop);
@@ -6473,7 +6476,7 @@ fn create_main_wrapper(ccx: &@crate_ctxt, sp: &span,
64736476
llargvarg];
64746477
bcx.build.FastCall(main_llfn, args);
64756478
}
6476-
bcx.build.RetVoid();
6479+
build_return(bcx);
64776480

64786481
let lltop = bcx.llbb;
64796482
finish_fn(fcx, lltop);
@@ -6754,7 +6757,7 @@ fn decl_native_fn_and_pair(ccx: &@crate_ctxt, sp: &span, path: &[str],
67546757
for d: {val: ValueRef, ty: ty::t} in drop_args {
67556758
bcx = drop_ty(bcx, d.val, d.ty).bcx;
67566759
}
6757-
bcx.build.RetVoid();
6760+
build_return(bcx);
67586761
finish_fn(fcx, lltop);
67596762
}
67606763

src/comp/middle/trans_common.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ type fn_ctxt = {
214214
// alloca'd by code in llallocas?
215215
mutable lldynamicallocas: BasicBlockRef,
216216

217+
mutable llreturn: BasicBlockRef,
218+
217219
// The token used to clear the dynamic allocas at the end of this frame.
218220
// Will be |none| if there are no dynamic allocas.
219221
mutable llobstacktoken: option::t<ValueRef>,
@@ -451,6 +453,7 @@ fn bcx_tcx(bcx: &@block_ctxt) -> ty::ctxt { ret bcx.fcx.lcx.ccx.tcx; }
451453
fn bcx_ccx(bcx: &@block_ctxt) -> @crate_ctxt { ret bcx.fcx.lcx.ccx; }
452454
fn bcx_lcx(bcx: &@block_ctxt) -> @local_ctxt { ret bcx.fcx.lcx; }
453455
fn bcx_fcx(bcx: &@block_ctxt) -> @fn_ctxt { ret bcx.fcx; }
456+
fn fcx_ccx(fcx: &@fn_ctxt) -> @crate_ctxt { ret fcx.lcx.ccx; }
454457
fn fcx_tcx(fcx: &@fn_ctxt) -> ty::ctxt { ret fcx.lcx.ccx.tcx; }
455458
fn lcx_ccx(lcx: &@local_ctxt) -> @crate_ctxt { ret lcx.ccx; }
456459
fn ccx_tcx(ccx: &@crate_ctxt) -> ty::ctxt { ret ccx.tcx; }

src/comp/middle/trans_objects.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ fn trans_obj(cx: @local_ctxt, sp: &span, ob: &ast::_obj,
192192
let p = bcx.build.PointerCast(box.box, llbox_ty);
193193
bcx.build.Store(p, pair_box);
194194
}
195-
bcx.build.RetVoid();
195+
build_return(bcx);
196196

197197
// Insert the mandatory first few basic blocks before lltop.
198198
finish_fn(fcx, lltop);
@@ -710,7 +710,7 @@ fn process_bkwding_mthd(cx: @local_ctxt, sp: &span, m: @ty::method,
710710
// And, finally, call the outer method.
711711
bcx.build.FastCall(llouter_mthd, llouter_mthd_args);
712712

713-
bcx.build.RetVoid();
713+
build_return(bcx);
714714
finish_fn(fcx, lltop);
715715

716716
ret llbackwarding_fn;
@@ -882,7 +882,7 @@ fn process_fwding_mthd(cx: @local_ctxt, sp: &span, m: @ty::method,
882882
// And, finally, call the original (inner) method.
883883
bcx.build.FastCall(llorig_mthd, llorig_mthd_args);
884884

885-
bcx.build.RetVoid();
885+
build_return(bcx);
886886
finish_fn(fcx, lltop);
887887

888888
ret llforwarding_fn;

0 commit comments

Comments
 (0)