Skip to content

Commit ce9e0fc

Browse files
committed
Revert "Get rid of 'overwrite' destination kind"
This reverts commit 6e65258.
1 parent 11e407a commit ce9e0fc

File tree

4 files changed

+102
-62
lines changed

4 files changed

+102
-62
lines changed

src/comp/middle/trans.rs

Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,7 +2112,6 @@ fn move_val(cx: @block_ctxt, action: copy_action, dst: ValueRef,
21122112
ty_to_str(tcx, t));
21132113
}
21142114

2115-
// FIXME[DPS] rename to store_temp_expr
21162115
fn move_val_if_temp(cx: @block_ctxt, action: copy_action, dst: ValueRef,
21172116
src: lval_result, t: ty::t) -> @block_ctxt {
21182117
// Lvals in memory are not temporaries. Copy them.
@@ -2214,7 +2213,7 @@ fn trans_unary(bcx: @block_ctxt, op: ast::unop, e: @ast::expr,
22142213
let llety = T_ptr(type_of(ccx, e_sp, e_ty));
22152214
body = PointerCast(bcx, body, llety);
22162215
}
2217-
bcx = trans_expr_save_in(bcx, e, body);
2216+
bcx = trans_expr_save_in(bcx, e, body, INIT);
22182217
revoke_clean(bcx, box);
22192218
ret store_in_dest(bcx, box, dest);
22202219
}
@@ -2256,7 +2255,8 @@ fn trans_expr_fn(bcx: @block_ctxt, f: ast::_fn, sp: span,
22562255
trans_closure(sub_cx, sp, f, llfn, none, [], id, {|_fcx|});
22572256
}
22582257
};
2259-
fill_fn_pair(bcx, get_dest_addr(dest), llfn, env);
2258+
let {bcx, val: addr} = get_dest_addr(bcx, dest);
2259+
fill_fn_pair(bcx, addr, llfn, env);
22602260
ret bcx;
22612261
}
22622262

@@ -2357,18 +2357,19 @@ fn trans_assign_op(bcx: @block_ctxt, op: ast::binop, dst: @ast::expr,
23572357
}
23582358
_ { }
23592359
}
2360-
let {bcx, val: rhs_val} = trans_expr(lhs_res.bcx, src);
2360+
let rhs_res = trans_expr(lhs_res.bcx, src);
23612361
if ty::type_is_sequence(tcx, t) {
23622362
alt op {
23632363
ast::add. {
2364-
ret tvec::trans_append(bcx, t, lhs_res.val, rhs_val);
2364+
ret tvec::trans_append(rhs_res.bcx, t, lhs_res.val,
2365+
rhs_res.val);
23652366
}
23662367
_ { }
23672368
}
23682369
}
2369-
2370-
ret trans_eager_binop(bcx, op, Load(bcx, lhs_res.val), t, rhs_val, t,
2371-
save_in(lhs_res.val));
2370+
let lhs_val = load_if_immediate(rhs_res.bcx, lhs_res.val, t);
2371+
ret trans_eager_binop(rhs_res.bcx, op, lhs_val, t, rhs_res.val, t,
2372+
overwrite(lhs_res.val, t));
23722373
}
23732374

23742375
fn autoderef(cx: @block_ctxt, v: ValueRef, t: ty::t) -> result_t {
@@ -2484,6 +2485,7 @@ tag dest {
24842485
by_val(@mutable ValueRef);
24852486
by_ref(@mutable ValueRef);
24862487
save_in(ValueRef);
2488+
overwrite(ValueRef, ty::t);
24872489
ignore;
24882490
}
24892491

@@ -2536,12 +2538,19 @@ fn store_in_dest(bcx: @block_ctxt, val: ValueRef, dest: dest) -> @block_ctxt {
25362538
ignore. {}
25372539
by_val(cell) { *cell = val; }
25382540
save_in(addr) { Store(bcx, val, addr); }
2541+
overwrite(addr, tp) {
2542+
bcx = drop_ty(bcx, addr, tp);
2543+
Store(bcx, val, addr);
2544+
}
25392545
}
25402546
ret bcx;
25412547
}
25422548

2543-
fn get_dest_addr(dest: dest) -> ValueRef {
2544-
alt dest { save_in(a) { a } }
2549+
fn get_dest_addr(bcx: @block_ctxt, dest: dest) -> result {
2550+
alt dest {
2551+
save_in(a) { rslt(bcx, a) }
2552+
overwrite(a, t) { rslt(drop_ty(bcx, a, t), a) }
2553+
}
25452554
}
25462555

25472556
// Wrapper through which legacy non-DPS code can use DPS functions
@@ -2724,7 +2733,7 @@ fn build_environment(bcx: @block_ctxt, lltydescs: [ValueRef],
27242733
bcx = bound.bcx;
27252734
alt bv {
27262735
env_expr(e) {
2727-
bcx = trans_expr_save_in(bcx, e, bound.val);
2736+
bcx = trans_expr_save_in(bcx, e, bound.val, INIT);
27282737
add_clean_temp_mem(bcx, bound.val, bound_tys[i]);
27292738
temp_cleanups += [bound.val];
27302739
}
@@ -3689,7 +3698,8 @@ fn trans_bind_1(cx: @block_ctxt, outgoing_fty: ty::t,
36893698
let lv = lval_maybe_callee_to_lval(f_res, pair_ty);
36903699
bcx = lv.bcx;
36913700
// FIXME[DPS] factor this out
3692-
ret memmove_ty(bcx, get_dest_addr(dest), lv.val, pair_ty);
3701+
let {bcx, val: addr} = get_dest_addr(bcx, dest);
3702+
ret memmove_ty(bcx, addr, lv.val, pair_ty);
36933703
}
36943704
let closure = alt f_res.env {
36953705
null_env. { none }
@@ -3726,7 +3736,8 @@ fn trans_bind_1(cx: @block_ctxt, outgoing_fty: ty::t,
37263736
closure.ptrty, ty_param_count, target_res);
37273737

37283738
// Fill the function pair
3729-
fill_fn_pair(bcx, get_dest_addr(dest), llthunk.val, closure.ptr);
3739+
let {bcx, val: addr} = get_dest_addr(bcx, dest);
3740+
fill_fn_pair(bcx, addr, llthunk.val, closure.ptr);
37303741
ret bcx;
37313742
}
37323743

@@ -3834,7 +3845,7 @@ fn trans_args(cx: @block_ctxt, outer_cx: @block_ctxt, llenv: ValueRef,
38343845
} else { alloca(cx, llretty) }
38353846
}
38363847
save_in(dst) { dst }
3837-
by_val(_) { alloca(cx, llretty) }
3848+
overwrite(_, _) | by_val(_) { alloca(cx, llretty) }
38383849
by_ref(_) { dest_ref = true; alloca(cx, T_ptr(llretty)) }
38393850
};
38403851
// FIXME[DSP] does this always hold?
@@ -3959,6 +3970,10 @@ fn trans_call(in_cx: @block_ctxt, f: @ast::expr,
39593970
}
39603971
}
39613972
save_in(_) { } // Already saved by callee
3973+
overwrite(a, t) {
3974+
bcx = drop_ty(bcx, a, t);
3975+
bcx = memmove_ty(bcx, a, llretslot, ret_ty);
3976+
}
39623977
by_ref(cell) | by_val(cell) {
39633978
*cell = Load(bcx, llretslot);
39643979
}
@@ -4152,38 +4167,55 @@ fn trans_landing_pad(bcx: @block_ctxt,
41524167
fn trans_tup(bcx: @block_ctxt, elts: [@ast::expr], id: ast::node_id,
41534168
dest: dest) -> @block_ctxt {
41544169
let t = node_id_type(bcx.fcx.lcx.ccx, id);
4155-
let addr = alt dest {
4170+
let (addr, overwrite) = alt dest {
41564171
ignore. {
41574172
for ex in elts { bcx = trans_expr_dps(bcx, ex, ignore); }
41584173
ret bcx;
41594174
}
4160-
save_in(pos) { pos }
4175+
save_in(pos) { (pos, none) }
4176+
overwrite(pos, _) {
4177+
let scratch = alloca(bcx, llvm::LLVMGetElementType(val_ty(pos)));
4178+
(scratch, some(pos))
4179+
}
41614180
};
41624181
let temp_cleanups = [], i = 0;
41634182
for e in elts {
41644183
let dst = GEP_tup_like_1(bcx, t, addr, [0, i]);
41654184
let e_ty = ty::expr_ty(bcx_tcx(bcx), e);
4166-
bcx = trans_expr_save_in(dst.bcx, e, dst.val);
4185+
bcx = trans_expr_save_in(dst.bcx, e, dst.val, INIT);
41674186
add_clean_temp_mem(bcx, dst.val, e_ty);
41684187
temp_cleanups += [dst.val];
41694188
i += 1;
41704189
}
41714190
for cleanup in temp_cleanups { revoke_clean(bcx, cleanup); }
4191+
alt overwrite {
4192+
some(pos) {
4193+
bcx = drop_ty(bcx, pos, t);
4194+
bcx = memmove_ty(bcx, pos, addr, t);
4195+
}
4196+
none. {}
4197+
}
41724198
ret bcx;
41734199
}
41744200

41754201
fn trans_rec(bcx: @block_ctxt, fields: [ast::field],
41764202
base: option::t<@ast::expr>, id: ast::node_id,
41774203
dest: dest) -> @block_ctxt {
41784204
let t = node_id_type(bcx_ccx(bcx), id);
4179-
let addr = alt dest {
4205+
let (addr, overwrite) = alt dest {
41804206
ignore. {
41814207
for fld in fields {
41824208
bcx = trans_expr_dps(bcx, fld.node.expr, ignore);
41834209
}
41844210
ret bcx;
41854211
}
4186-
save_in(pos) { pos }
4212+
save_in(pos) { (pos, none) }
4213+
// The expressions that populate the fields might still use the old
4214+
// record, so we build the new on in a scratch area
4215+
overwrite(pos, _) {
4216+
let scratch = alloca(bcx, llvm::LLVMGetElementType(val_ty(pos)));
4217+
(scratch, some(pos))
4218+
}
41874219
};
41884220

41894221
let base_val = alt base {
@@ -4202,7 +4234,7 @@ fn trans_rec(bcx: @block_ctxt, fields: [ast::field],
42024234
bcx = dst.bcx;
42034235
alt vec::find({|f| str::eq(f.node.ident, tf.ident)}, fields) {
42044236
some(f) {
4205-
bcx = trans_expr_save_in(bcx, f.node.expr, dst.val);
4237+
bcx = trans_expr_save_in(bcx, f.node.expr, dst.val, INIT);
42064238
}
42074239
none. {
42084240
let base = GEP_tup_like_1(bcx, t, base_val, [0, i]);
@@ -4217,6 +4249,13 @@ fn trans_rec(bcx: @block_ctxt, fields: [ast::field],
42174249
// Now revoke the cleanups as we pass responsibility for the data
42184250
// structure on to the caller
42194251
for cleanup in temp_cleanups { revoke_clean(bcx, cleanup); }
4252+
alt overwrite {
4253+
some(pos) {
4254+
bcx = drop_ty(bcx, pos, t);
4255+
bcx = memmove_ty(bcx, pos, addr, t);
4256+
}
4257+
none. {}
4258+
}
42204259
ret bcx;
42214260
}
42224261

@@ -4235,37 +4274,19 @@ fn trans_expr(cx: @block_ctxt, e: @ast::expr) -> result {
42354274
}
42364275
}
42374276

4238-
fn trans_expr_save_in(bcx: @block_ctxt, e: @ast::expr, dest: ValueRef)
4239-
-> @block_ctxt {
4277+
fn trans_expr_save_in(bcx: @block_ctxt, e: @ast::expr, dest: ValueRef,
4278+
kind: copy_action) -> @block_ctxt {
42404279
let tcx = bcx_tcx(bcx), t = ty::expr_ty(tcx, e);
42414280
let dst = if ty::type_is_bot(tcx, t) || ty::type_is_nil(tcx, t) {
42424281
ignore
4243-
} else { save_in(dest) };
4244-
ret trans_expr_dps(bcx, e, dst);
4245-
}
4246-
4247-
fn trans_temp_expr(bcx: @block_ctxt, e: @ast::expr) -> lval_result {
4248-
if expr_is_lval(bcx_tcx(bcx), e) {
4249-
ret trans_lval(bcx, e);
4282+
} else if kind == INIT {
4283+
save_in(dest)
42504284
} else {
4251-
let tcx = bcx_tcx(bcx);
4252-
let ty = ty::expr_ty(tcx, e);
4253-
if ty::type_is_nil(tcx, ty) || ty::type_is_bot(tcx, ty) {
4254-
bcx = trans_expr_dps(bcx, e, ignore);
4255-
ret {bcx: bcx, val: C_nil(), is_mem: false};
4256-
} else if type_is_immediate(bcx_ccx(bcx), ty) {
4257-
let cell = empty_dest_cell();
4258-
bcx = trans_expr_dps(bcx, e, by_val(cell));
4259-
ret {bcx: bcx, val: *cell, is_mem: false};
4260-
} else {
4261-
let {bcx, val: scratch} = alloc_ty(bcx, ty);
4262-
bcx = trans_expr_dps(bcx, e, save_in(scratch));
4263-
ret {bcx: bcx, val: scratch, is_mem: false};
4264-
}
4265-
}
4285+
overwrite(dest, t)
4286+
};
4287+
ret trans_expr_dps(bcx, e, dst);
42664288
}
42674289

4268-
// FIXME[DPS] supersede by trans_temp_expr, get rid of by_ref dests
42694290
fn trans_expr_by_ref(bcx: @block_ctxt, e: @ast::expr) -> result {
42704291
let cell = empty_dest_cell();
42714292
bcx = trans_expr_dps(bcx, e, by_ref(cell));
@@ -4409,20 +4430,22 @@ fn trans_expr_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest)
44094430
}
44104431
ast::expr_assign(dst, src) {
44114432
assert dest == ignore;
4412-
let src_r = trans_temp_expr(bcx, src);
4413-
let {bcx, val: addr, is_mem} = trans_lval(src_r.bcx, dst);
4433+
let {bcx, val: lhs_addr, is_mem} = trans_lval(bcx, dst);
44144434
assert is_mem;
4415-
ret move_val_if_temp(bcx, DROP_EXISTING, addr, src_r,
4416-
ty::expr_ty(bcx_tcx(bcx), src));
4435+
ret trans_expr_save_in(bcx, src, lhs_addr, DROP_EXISTING);
44174436
}
44184437
ast::expr_move(dst, src) {
4419-
// FIXME: calculate copy init-ness in typestate.
44204438
assert dest == ignore;
4421-
let src_r = trans_temp_expr(bcx, src);
4422-
let {bcx, val: addr, is_mem} = trans_lval(src_r.bcx, dst);
4439+
let {bcx, val: addr, is_mem} = trans_lval(bcx, dst);
44234440
assert is_mem;
4424-
ret move_val(bcx, DROP_EXISTING, addr, src_r,
4425-
ty::expr_ty(bcx_tcx(bcx), src));
4441+
// FIXME: calculate copy init-ness in typestate.
4442+
if expr_is_lval(tcx, src) {
4443+
ret trans_expr_save_in(bcx, src, addr, DROP_EXISTING);
4444+
} else {
4445+
let srclv = trans_lval(bcx, src);
4446+
let t = ty::expr_ty(tcx, src);
4447+
ret move_val(srclv.bcx, DROP_EXISTING, addr, srclv, t);
4448+
}
44264449
}
44274450
ast::expr_swap(dst, src) {
44284451
assert dest == ignore;
@@ -4469,6 +4492,9 @@ fn lval_to_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest) -> @block_ctxt {
44694492
*cell = val;
44704493
}
44714494
save_in(loc) { bcx = move_val_if_temp(bcx, INIT, loc, lv, ty); }
4495+
overwrite(loc, _) {
4496+
bcx = move_val_if_temp(bcx, DROP_EXISTING, loc, lv, ty);
4497+
}
44724498
ignore. {}
44734499
}
44744500
ret bcx;
@@ -4720,7 +4746,7 @@ fn trans_ret(bcx: @block_ctxt, e: option::t<@ast::expr>) -> @block_ctxt {
47204746
Store(cx, val, bcx.fcx.llretptr);
47214747
bcx = cx;
47224748
} else {
4723-
bcx = trans_expr_save_in(bcx, x, bcx.fcx.llretptr);
4749+
bcx = trans_expr_save_in(bcx, x, bcx.fcx.llretptr, INIT);
47244750
}
47254751
}
47264752
_ {}
@@ -4758,7 +4784,7 @@ fn init_local(bcx: @block_ctxt, local: @ast::local) -> @block_ctxt {
47584784
some(init) {
47594785
if init.op == ast::init_assign ||
47604786
!expr_is_lval(bcx_tcx(bcx), init.expr) {
4761-
bcx = trans_expr_save_in(bcx, init.expr, llptr);
4787+
bcx = trans_expr_save_in(bcx, init.expr, llptr, INIT);
47624788
} else { // This is a move from an lval, must perform an actual move
47634789
let sub = trans_lval(bcx, init.expr);
47644790
bcx = move_val(sub.bcx, INIT, llptr, sub, ty);

src/comp/middle/trans_objects.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ fn trans_anon_obj(bcx: @block_ctxt, sp: span, anon_obj: ast::anon_obj,
378378
revoke_clean(bcx, box);
379379
box = PointerCast(bcx, box, llbox_ty);
380380
}
381-
let pair = trans::get_dest_addr(dest);
381+
let {bcx, val: pair} = trans::get_dest_addr(bcx, dest);
382382
let pair_vtbl = GEP(bcx, pair, [C_int(0), C_int(abi::obj_field_vtbl)]);
383383
Store(bcx, vtbl, pair_vtbl);
384384
let pair_box = GEP(bcx, pair, [C_int(0), C_int(abi::obj_field_box)]);

src/comp/middle/trans_uniq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn trans_uniq(bcx: @block_ctxt, contents: @ast::expr,
2929
check type_is_unique_box(bcx, uniq_ty);
3030
let {bcx, val: llptr} = alloc_uniq(bcx, uniq_ty);
3131
add_clean_free(bcx, llptr, true);
32-
bcx = trans::trans_expr_save_in(bcx, contents, llptr);
32+
bcx = trans::trans_expr_save_in(bcx, contents, llptr, INIT);
3333
revoke_clean(bcx, llptr);
3434
ret trans::store_in_dest(bcx, llptr, dest);
3535
}

src/comp/middle/trans_vec.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,17 @@ fn trans_vec(bcx: @block_ctxt, args: [@ast::expr], id: ast::node_id,
124124
let lleltptr = if ty::type_has_dynamic_size(bcx_tcx(bcx), unit_ty) {
125125
InBoundsGEP(bcx, dataptr, [Mul(bcx, C_uint(i), llunitsz)])
126126
} else { InBoundsGEP(bcx, dataptr, [C_uint(i)]) };
127-
bcx = trans::trans_expr_save_in(bcx, e, lleltptr);
127+
bcx = trans::trans_expr_save_in(bcx, e, lleltptr, INIT);
128128
add_clean_temp_mem(bcx, lleltptr, unit_ty);
129129
temp_cleanups += [lleltptr];
130130
i += 1u;
131131
}
132132
for clean in temp_cleanups { revoke_clean(bcx, clean); }
133-
Store(bcx, vptr, trans::get_dest_addr(dest));
133+
let vptrptr = alt dest {
134+
trans::save_in(a) { a }
135+
trans::overwrite(a, t) { bcx = trans::drop_ty(bcx, a, t); a }
136+
};
137+
Store(bcx, vptr, vptrptr);
134138
ret bcx;
135139
}
136140

@@ -143,7 +147,11 @@ fn trans_str(bcx: @block_ctxt, s: str, dest: dest) -> @block_ctxt {
143147
let bcx =
144148
call_memmove(bcx, get_dataptr_simple(bcx, sptr, T_i8()), llcstr,
145149
C_uint(veclen)).bcx;
146-
Store(bcx, sptr, trans::get_dest_addr(dest));
150+
let sptrptr = alt dest {
151+
trans::save_in(a) { a }
152+
trans::overwrite(a, t) { bcx = trans::drop_ty(bcx, a, t); a }
153+
};
154+
Store(bcx, sptr, sptrptr);
147155
ret bcx;
148156
}
149157

@@ -258,7 +266,13 @@ fn trans_add(bcx: @block_ctxt, vec_ty: ty::t, lhsptr: ValueRef,
258266

259267
let bcx = iter_vec_raw(bcx, lhsptr, vec_ty, lhs_fill, copy_fn);
260268
bcx = iter_vec_raw(bcx, rhsptr, vec_ty, rhs_fill, copy_fn);
261-
Store(bcx, new_vec_ptr, trans::get_dest_addr(dest));
269+
alt dest {
270+
trans::save_in(a) { Store(bcx, new_vec_ptr, a); }
271+
trans::overwrite(a, t) {
272+
bcx = trans::drop_ty(bcx, a, t);
273+
Store(bcx, new_vec_ptr, a);
274+
}
275+
}
262276
ret bcx;
263277
}
264278

0 commit comments

Comments
 (0)