Skip to content

Commit 8ea72fa

Browse files
committed
Move more non-value-returning things to trans_expr_dps.
Issue #667
1 parent b49f468 commit 8ea72fa

File tree

2 files changed

+90
-87
lines changed

2 files changed

+90
-87
lines changed

src/comp/middle/trans.rs

Lines changed: 88 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,6 +2298,43 @@ fn trans_eager_binop(cx: @block_ctxt, op: ast::binop, lhs: ValueRef,
22982298
}
22992299
}
23002300

2301+
fn trans_assign_op(bcx: @block_ctxt, op: ast::binop, dst: @ast::expr,
2302+
src: @ast::expr) -> @block_ctxt {
2303+
let tcx = bcx_tcx(bcx);
2304+
let t = ty::expr_ty(tcx, src);
2305+
let lhs_res = trans_lval(bcx, dst);
2306+
assert (lhs_res.is_mem);
2307+
// Special case for `+= [x]`
2308+
alt ty::struct(tcx, t) {
2309+
ty::ty_vec(_) {
2310+
alt src.node {
2311+
ast::expr_vec(args, _) {
2312+
ret tvec::trans_append_literal(lhs_res.bcx,
2313+
lhs_res.val, t, args);
2314+
}
2315+
_ { }
2316+
}
2317+
}
2318+
_ { }
2319+
}
2320+
let rhs_res = trans_expr(lhs_res.bcx, src);
2321+
if ty::type_is_sequence(tcx, t) {
2322+
alt op {
2323+
ast::add. {
2324+
ret tvec::trans_append(rhs_res.bcx, t, lhs_res.val,
2325+
rhs_res.val);
2326+
}
2327+
_ { }
2328+
}
2329+
}
2330+
let lhs_val = load_if_immediate(rhs_res.bcx, lhs_res.val, t);
2331+
let v = trans_eager_binop(rhs_res.bcx, op, lhs_val, t, rhs_res.val, t);
2332+
// FIXME: calculate copy init-ness in typestate.
2333+
// This is always a temporary, so can always be safely moved
2334+
ret move_val(v.bcx, DROP_EXISTING, lhs_res.val,
2335+
lval_val(v.bcx, v.val), t);
2336+
}
2337+
23012338
fn autoderef(cx: @block_ctxt, v: ValueRef, t: ty::t) -> result_t {
23022339
let v1: ValueRef = v;
23032340
let t1: ty::t = t;
@@ -2540,7 +2577,7 @@ fn trans_if(cx: @block_ctxt, cond: @ast::expr, thn: ast::blk,
25402577
}
25412578

25422579
fn trans_for(cx: @block_ctxt, local: @ast::local, seq: @ast::expr,
2543-
body: ast::blk) -> result {
2580+
body: ast::blk) -> @block_ctxt {
25442581
fn inner(bcx: @block_ctxt, local: @ast::local, curr: ValueRef, t: ty::t,
25452582
body: ast::blk, outer_next_cx: @block_ctxt) -> @block_ctxt {
25462583
let next_cx = new_sub_block_ctxt(bcx, "next");
@@ -2567,7 +2604,7 @@ fn trans_for(cx: @block_ctxt, local: @ast::local, seq: @ast::expr,
25672604
tvec::iter_vec_raw(bcx, seq, seq_ty, fill,
25682605
bind inner(_, local, _, _, body, next_cx));
25692606
Br(bcx, next_cx.llbb);
2570-
ret rslt(next_cx, C_nil());
2607+
ret next_cx;
25712608
}
25722609

25732610

@@ -2789,7 +2826,7 @@ fn load_environment(enclosing_cx: @block_ctxt, fcx: @fn_ctxt, envty: ty::t,
27892826
}
27902827

27912828
fn trans_for_each(cx: @block_ctxt, local: @ast::local, seq: @ast::expr,
2792-
body: ast::blk) -> result {
2829+
body: ast::blk) -> @block_ctxt {
27932830
/*
27942831
* The translation is a little .. complex here. Code like:
27952832
*
@@ -2865,12 +2902,13 @@ fn trans_for_each(cx: @block_ctxt, local: @ast::local, seq: @ast::expr,
28652902
let pair =
28662903
create_real_fn_pair(cx, iter_body_llty, lliterbody, llenv.ptr);
28672904
let r = trans_call(cx, f, some(pair), args, seq.id);
2868-
ret rslt(r.res.bcx, C_nil());
2905+
ret r.res.bcx;
28692906
}
28702907
}
28712908
}
28722909

2873-
fn trans_while(cx: @block_ctxt, cond: @ast::expr, body: ast::blk) -> result {
2910+
fn trans_while(cx: @block_ctxt, cond: @ast::expr, body: ast::blk)
2911+
-> @block_ctxt {
28742912
let next_cx = new_sub_block_ctxt(cx, "while next");
28752913
let cond_cx =
28762914
new_loop_scope_block_ctxt(cx, option::none::<@block_ctxt>, next_cx,
@@ -2882,11 +2920,11 @@ fn trans_while(cx: @block_ctxt, cond: @ast::expr, body: ast::blk) -> result {
28822920
let cond_bcx = trans_block_cleanups(cond_res.bcx, cond_cx);
28832921
CondBr(cond_bcx, cond_res.val, body_cx.llbb, next_cx.llbb);
28842922
Br(cx, cond_cx.llbb);
2885-
ret rslt(next_cx, C_nil());
2923+
ret next_cx;
28862924
}
28872925

28882926
fn trans_do_while(cx: @block_ctxt, body: ast::blk, cond: @ast::expr) ->
2889-
result {
2927+
@block_ctxt {
28902928
let next_cx = new_sub_block_ctxt(cx, "next");
28912929
let body_cx =
28922930
new_loop_scope_block_ctxt(cx, option::none::<@block_ctxt>, next_cx,
@@ -2895,7 +2933,7 @@ fn trans_do_while(cx: @block_ctxt, body: ast::blk, cond: @ast::expr) ->
28952933
let cond_res = trans_expr(body_res.bcx, cond);
28962934
CondBr(cond_res.bcx, cond_res.val, body_cx.llbb, next_cx.llbb);
28972935
Br(cx, body_cx.llbb);
2898-
ret rslt(next_cx, body_res.val);
2936+
ret next_cx;
28992937
}
29002938

29012939
type generic_info =
@@ -4093,12 +4131,6 @@ fn trans_expr(cx: @block_ctxt, e: @ast::expr) -> result {
40934131
alt e.node {
40944132
ast::expr_lit(lit) { ret trans_lit(cx, *lit); }
40954133
ast::expr_binary(op, x, y) { ret trans_binary(cx, op, x, y); }
4096-
ast::expr_for(decl, seq, body) { ret trans_for(cx, decl, seq, body); }
4097-
ast::expr_for_each(decl, seq, body) {
4098-
ret trans_for_each(cx, decl, seq, body);
4099-
}
4100-
ast::expr_while(cond, body) { ret trans_while(cx, cond, body); }
4101-
ast::expr_do_while(body, cond) { ret trans_do_while(cx, body, cond); }
41024134
ast::expr_fn(f) {
41034135
let ccx = bcx_ccx(cx);
41044136
let fty = node_id_type(ccx, e.id);
@@ -4152,77 +4184,6 @@ fn trans_expr(cx: @block_ctxt, e: @ast::expr) -> result {
41524184
t);
41534185
ret rslt(bcx, C_nil());
41544186
}
4155-
ast::expr_assign(dst, src) {
4156-
let lhs_res = trans_lval(cx, dst);
4157-
assert (lhs_res.is_mem);
4158-
// FIXME Fill in lhs_res.bcx.sp
4159-
let rhs = trans_lval(lhs_res.bcx, src);
4160-
let t = ty::expr_ty(bcx_tcx(cx), src);
4161-
// FIXME: calculate copy init-ness in typestate.
4162-
let bcx =
4163-
move_val_if_temp(rhs.bcx, DROP_EXISTING, lhs_res.val, rhs,
4164-
t);
4165-
ret rslt(bcx, C_nil());
4166-
}
4167-
ast::expr_swap(dst, src) {
4168-
let lhs_res = trans_lval(cx, dst);
4169-
assert (lhs_res.is_mem);
4170-
// FIXME Fill in lhs_res.bcx.sp
4171-
4172-
let rhs_res = trans_lval(lhs_res.bcx, src);
4173-
let t = ty::expr_ty(bcx_tcx(cx), src);
4174-
let {bcx: bcx, val: tmp_alloc} = alloc_ty(rhs_res.bcx, t);
4175-
// Swap through a temporary.
4176-
4177-
bcx = move_val(bcx, INIT, tmp_alloc, lhs_res, t);
4178-
bcx = move_val(bcx, INIT, lhs_res.val, rhs_res, t);
4179-
bcx =
4180-
move_val(bcx, INIT, rhs_res.val, lval_mem(bcx, tmp_alloc), t);
4181-
ret rslt(bcx, C_nil());
4182-
}
4183-
ast::expr_assign_op(op, dst, src) {
4184-
let tcx = bcx_tcx(cx);
4185-
let t = ty::expr_ty(tcx, src);
4186-
let lhs_res = trans_lval(cx, dst);
4187-
assert (lhs_res.is_mem);
4188-
4189-
// Special case for `+= [x]`
4190-
alt ty::struct(tcx, t) {
4191-
ty::ty_vec(_) {
4192-
alt src.node {
4193-
ast::expr_vec(args, _) {
4194-
let bcx =
4195-
tvec::trans_append_literal(lhs_res.bcx,
4196-
lhs_res.val, t, args);
4197-
ret rslt(bcx, C_nil());
4198-
}
4199-
_ { }
4200-
}
4201-
}
4202-
_ { }
4203-
}
4204-
4205-
// FIXME Fill in lhs_res.bcx.sp
4206-
let rhs_res = trans_expr(lhs_res.bcx, src);
4207-
if ty::type_is_sequence(tcx, t) {
4208-
alt op {
4209-
ast::add. {
4210-
ret tvec::trans_append(rhs_res.bcx, t, lhs_res.val,
4211-
rhs_res.val);
4212-
}
4213-
_ { }
4214-
}
4215-
}
4216-
let lhs_val = load_if_immediate(rhs_res.bcx, lhs_res.val, t);
4217-
let v =
4218-
trans_eager_binop(rhs_res.bcx, op, lhs_val, t, rhs_res.val, t);
4219-
// FIXME: calculate copy init-ness in typestate.
4220-
// This is always a temporary, so can always be safely moved
4221-
let bcx =
4222-
move_val(v.bcx, DROP_EXISTING, lhs_res.val,
4223-
lval_val(v.bcx, v.val), t);
4224-
ret rslt(bcx, C_nil());
4225-
}
42264187
ast::expr_bind(f, args) { ret trans_bind(cx, f, args, e.id); }
42274188
ast::expr_cast(val, _) { ret trans_cast(cx, val, e.id); }
42284189
ast::expr_vec(args, _) { ret tvec::trans_vec(cx, args, e.id); }
@@ -4360,6 +4321,48 @@ fn trans_expr_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest)
43604321
ret join_branches(bcx, [rslt(check_cx, C_nil()),
43614322
rslt(else_cx, C_nil())]);
43624323
}
4324+
ast::expr_for(decl, seq, body) {
4325+
assert dest == ignore;
4326+
ret trans_for(bcx, decl, seq, body);
4327+
}
4328+
ast::expr_for_each(decl, seq, body) {
4329+
assert dest == ignore;
4330+
ret trans_for_each(bcx, decl, seq, body);
4331+
}
4332+
ast::expr_while(cond, body) {
4333+
assert dest == ignore;
4334+
ret trans_while(bcx, cond, body);
4335+
}
4336+
ast::expr_do_while(body, cond) {
4337+
assert dest == ignore;
4338+
ret trans_do_while(bcx, body, cond);
4339+
}
4340+
ast::expr_assign(dst, src) {
4341+
assert dest == ignore;
4342+
let lhs_res = trans_lval(bcx, dst);
4343+
assert (lhs_res.is_mem);
4344+
let rhs = trans_lval(lhs_res.bcx, src);
4345+
let t = ty::expr_ty(bcx_tcx(bcx), src);
4346+
// FIXME: calculate copy init-ness in typestate.
4347+
ret move_val_if_temp(rhs.bcx, DROP_EXISTING, lhs_res.val,
4348+
rhs, t);
4349+
}
4350+
ast::expr_swap(dst, src) {
4351+
assert dest == ignore;
4352+
let lhs_res = trans_lval(bcx, dst);
4353+
assert (lhs_res.is_mem);
4354+
let rhs_res = trans_lval(lhs_res.bcx, src);
4355+
let t = ty::expr_ty(bcx_tcx(bcx), src);
4356+
let {bcx: bcx, val: tmp_alloc} = alloc_ty(rhs_res.bcx, t);
4357+
// Swap through a temporary.
4358+
bcx = move_val(bcx, INIT, tmp_alloc, lhs_res, t);
4359+
bcx = move_val(bcx, INIT, lhs_res.val, rhs_res, t);
4360+
ret move_val(bcx, INIT, rhs_res.val, lval_mem(bcx, tmp_alloc), t);
4361+
}
4362+
ast::expr_assign_op(op, dst, src) {
4363+
assert dest == ignore;
4364+
ret trans_assign_op(bcx, op, dst, src);
4365+
}
43634366

43644367
ast::expr_mac(_) { ret bcx_ccx(bcx).sess.bug("unexpanded macro"); }
43654368
// Convert back from result to DPS

src/comp/middle/trans_vec.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ fn trans_str(bcx: @block_ctxt, s: str) -> result {
141141
}
142142

143143
fn trans_append(cx: @block_ctxt, vec_ty: ty::t, lhsptr: ValueRef,
144-
rhsptr: ValueRef) -> result {
144+
rhsptr: ValueRef) -> @block_ctxt {
145145
// Cast to opaque interior vector types if necessary.
146146
let unit_ty = ty::sequence_element_type(bcx_tcx(cx), vec_ty);
147147
let dynamic = ty::type_has_dynamic_size(bcx_tcx(cx), unit_ty);
@@ -190,7 +190,7 @@ fn trans_append(cx: @block_ctxt, vec_ty: ty::t, lhsptr: ValueRef,
190190
write_ptr_ptr);
191191
ret bcx;
192192
});
193-
ret rslt(bcx, C_nil());
193+
ret bcx;
194194
}
195195

196196
fn trans_append_literal(bcx: @block_ctxt, vptrptr: ValueRef, vec_ty: ty::t,

0 commit comments

Comments
 (0)