Skip to content

Commit 2c7a95a

Browse files
committed
---
yaml --- r: 1002 b: refs/heads/master c: 966cfed h: refs/heads/master v: v3
1 parent 07d150e commit 2c7a95a

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 1e9c8b923d78e6169d1d99bfccf675a10f904a76
2+
refs/heads/master: 966cfed07ca1514736335365d8f631872c1cd002

trunk/src/comp/middle/trans.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ tag cleanup {
7373
state type block_ctxt = rec(BasicBlockRef llbb,
7474
builder build,
7575
block_parent parent,
76+
bool is_scope,
7677
mutable vec[cleanup] cleanups,
7778
@fn_ctxt fcx);
7879

@@ -450,15 +451,30 @@ fn trans_non_gc_free(@block_ctxt cx, ValueRef v) -> result {
450451
C_int(0)));
451452
}
452453

454+
fn find_scope_cx(@block_ctxt cx) -> @block_ctxt {
455+
if (cx.is_scope) {
456+
ret cx;
457+
}
458+
alt (cx.parent) {
459+
case (parent_some(?b)) {
460+
be find_scope_cx(b);
461+
}
462+
case (parent_none) {
463+
fail;
464+
}
465+
}
466+
}
467+
453468
fn trans_malloc(@block_ctxt cx, @typeck.ty t) -> result {
469+
auto scope_cx = find_scope_cx(cx);
454470
auto ptr_ty = type_of(cx.fcx.ccx, t);
455471
auto body_ty = lib.llvm.llvm.LLVMGetElementType(ptr_ty);
456472
// FIXME: need a table to collect tydesc globals.
457473
auto tydesc = C_int(0);
458474
auto sz = cx.build.IntCast(lib.llvm.llvm.LLVMSizeOf(body_ty), T_int());
459475
auto sub = trans_upcall(cx, "upcall_malloc", vec(sz, tydesc));
460476
sub.val = sub.bcx.build.IntToPtr(sub.val, ptr_ty);
461-
sub.bcx.cleanups += clean(bind drop_ty(_, sub.val, t));
477+
scope_cx.cleanups += clean(bind drop_ty(_, sub.val, t));
462478
ret sub;
463479
}
464480

@@ -575,8 +591,8 @@ fn iter_sequence(@block_ctxt cx,
575591

576592
auto r = res(cx, C_nil());
577593

578-
auto cond_cx = new_sub_block_ctxt(cx, "sequence-iter cond");
579-
auto body_cx = new_sub_block_ctxt(cx, "sequence-iter body");
594+
auto cond_cx = new_scope_block_ctxt(cx, "sequence-iter cond");
595+
auto body_cx = new_scope_block_ctxt(cx, "sequence-iter body");
580596
auto next_cx = new_sub_block_ctxt(cx, "next");
581597

582598
auto ix = cond_cx.build.Phi(T_int(), vec(C_int(0)), vec(cx.llbb));
@@ -1081,10 +1097,10 @@ impure fn trans_if(@block_ctxt cx, @ast.expr cond,
10811097

10821098
auto cond_res = trans_expr(cx, cond);
10831099

1084-
auto then_cx = new_sub_block_ctxt(cx, "then");
1100+
auto then_cx = new_scope_block_ctxt(cx, "then");
10851101
auto then_res = trans_block(then_cx, thn);
10861102

1087-
auto else_cx = new_sub_block_ctxt(cx, "else");
1103+
auto else_cx = new_scope_block_ctxt(cx, "else");
10881104
auto else_res = res(else_cx, C_nil());
10891105

10901106
alt (els) {
@@ -1106,8 +1122,8 @@ impure fn trans_if(@block_ctxt cx, @ast.expr cond,
11061122
impure fn trans_while(@block_ctxt cx, @ast.expr cond,
11071123
&ast.block body) -> result {
11081124

1109-
auto cond_cx = new_sub_block_ctxt(cx, "while cond");
1110-
auto body_cx = new_sub_block_ctxt(cx, "while loop body");
1125+
auto cond_cx = new_scope_block_ctxt(cx, "while cond");
1126+
auto body_cx = new_scope_block_ctxt(cx, "while loop body");
11111127
auto next_cx = new_sub_block_ctxt(cx, "next");
11121128

11131129
auto body_res = trans_block(body_cx, body);
@@ -1125,7 +1141,7 @@ impure fn trans_while(@block_ctxt cx, @ast.expr cond,
11251141
impure fn trans_do_while(@block_ctxt cx, &ast.block body,
11261142
@ast.expr cond) -> result {
11271143

1128-
auto body_cx = new_sub_block_ctxt(cx, "do-while loop body");
1144+
auto body_cx = new_scope_block_ctxt(cx, "do-while loop body");
11291145
auto next_cx = new_sub_block_ctxt(cx, "next");
11301146

11311147
auto body_res = trans_block(body_cx, body);
@@ -1353,7 +1369,7 @@ impure fn trans_expr(@block_ctxt cx, @ast.expr e) -> result {
13531369
}
13541370

13551371
case (ast.expr_block(?blk, _)) {
1356-
auto sub_cx = new_sub_block_ctxt(cx, "block-expr body");
1372+
auto sub_cx = new_scope_block_ctxt(cx, "block-expr body");
13571373
auto next_cx = new_sub_block_ctxt(cx, "next");
13581374
auto sub = trans_block(sub_cx, blk);
13591375

@@ -1542,36 +1558,45 @@ fn new_builder(BasicBlockRef llbb, str name) -> builder {
15421558
// You probably don't want to use this one. See the
15431559
// next three functions instead.
15441560
fn new_block_ctxt(@fn_ctxt cx, block_parent parent,
1545-
vec[cleanup] cleanups,
1561+
bool is_scope,
15461562
str name) -> @block_ctxt {
1563+
let vec[cleanup] cleanups = vec();
15471564
let BasicBlockRef llbb =
15481565
llvm.LLVMAppendBasicBlock(cx.llfn,
15491566
_str.buf(cx.ccx.names.next(name)));
15501567

15511568
ret @rec(llbb=llbb,
15521569
build=new_builder(llbb, name),
15531570
parent=parent,
1571+
is_scope=is_scope,
15541572
mutable cleanups=cleanups,
15551573
fcx=cx);
15561574
}
15571575

15581576
// Use this when you're at the top block of a function or the like.
15591577
fn new_top_block_ctxt(@fn_ctxt fcx) -> @block_ctxt {
1560-
let vec[cleanup] cleanups = vec();
1561-
ret new_block_ctxt(fcx, parent_none, cleanups, "function top level");
1578+
ret new_block_ctxt(fcx, parent_none, true, "function top level");
1579+
}
15621580

1581+
// Use this when you're at a curly-brace or similar lexical scope.
1582+
fn new_scope_block_ctxt(@block_ctxt bcx, str n) -> @block_ctxt {
1583+
ret new_block_ctxt(bcx.fcx, parent_some(bcx), true, n);
15631584
}
15641585

1565-
// Use this when you're making a block-within-a-block.
1586+
// Use this when you're making a general CFG BB within a scope.
15661587
fn new_sub_block_ctxt(@block_ctxt bcx, str n) -> @block_ctxt {
1567-
let vec[cleanup] cleanups = vec();
1568-
ret new_block_ctxt(bcx.fcx, parent_some(bcx), cleanups, n);
1588+
ret new_block_ctxt(bcx.fcx, parent_some(bcx), false, n);
15691589
}
15701590

15711591

15721592
fn trans_block_cleanups(@block_ctxt cx,
15731593
@block_ctxt cleanup_cx) -> @block_ctxt {
15741594
auto bcx = cx;
1595+
1596+
if (!cleanup_cx.is_scope) {
1597+
check (_vec.len[cleanup](cleanup_cx.cleanups) == 0u);
1598+
}
1599+
15751600
for (cleanup c in cleanup_cx.cleanups) {
15761601
alt (c) {
15771602
case (clean(?cfn)) {

0 commit comments

Comments
 (0)