Skip to content

Commit b1b202d

Browse files
committed
Use DPS for assignment and local initialization
Issue #667
1 parent e42f3b8 commit b1b202d

File tree

3 files changed

+56
-51
lines changed

3 files changed

+56
-51
lines changed

src/comp/middle/trans.rs

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,7 +2208,7 @@ fn trans_unary(bcx: @block_ctxt, op: ast::unop, e: @ast::expr,
22082208
let llety = T_ptr(type_of(ccx, e_sp, e_ty));
22092209
body = PointerCast(bcx, body, llety);
22102210
}
2211-
bcx = trans_expr_save_in(bcx, e, body);
2211+
bcx = trans_expr_save_in(bcx, e, body, INIT);
22122212
revoke_clean(bcx, box);
22132213
ret store_in_dest(bcx, box, dest);
22142214
}
@@ -4033,15 +4033,15 @@ fn trans_tup(bcx: @block_ctxt, elts: [@ast::expr], id: ast::node_id,
40334033
}
40344034
save_in(pos) { (pos, none) }
40354035
overwrite(pos, _) {
4036-
let scratch = alloca(bcx, val_ty(pos));
4036+
let scratch = alloca(bcx, llvm::LLVMGetElementType(val_ty(pos)));
40374037
(scratch, some(pos))
40384038
}
40394039
};
40404040
let temp_cleanups = [], i = 0;
40414041
for e in elts {
40424042
let dst = GEP_tup_like_1(bcx, t, addr, [0, i]);
40434043
let e_ty = ty::expr_ty(bcx_tcx(bcx), e);
4044-
bcx = trans_expr_save_in(dst.bcx, e, dst.val);
4044+
bcx = trans_expr_save_in(dst.bcx, e, dst.val, INIT);
40454045
add_clean_temp_mem(bcx, dst.val, e_ty);
40464046
temp_cleanups += [dst.val];
40474047
i += 1;
@@ -4072,7 +4072,7 @@ fn trans_rec(bcx: @block_ctxt, fields: [ast::field],
40724072
// The expressions that populate the fields might still use the old
40734073
// record, so we build the new on in a scratch area
40744074
overwrite(pos, _) {
4075-
let scratch = alloca(bcx, val_ty(pos));
4075+
let scratch = alloca(bcx, llvm::LLVMGetElementType(val_ty(pos)));
40764076
(scratch, some(pos))
40774077
}
40784078
};
@@ -4096,7 +4096,7 @@ fn trans_rec(bcx: @block_ctxt, fields: [ast::field],
40964096
fn test(n: str, f: ast::field) -> bool { str::eq(f.node.ident, n) }
40974097
alt vec::find(bind test(tf.ident, _), fields) {
40984098
some(f) {
4099-
bcx = trans_expr_save_in(bcx, f.node.expr, dst.val);
4099+
bcx = trans_expr_save_in(bcx, f.node.expr, dst.val, INIT);
41004100
}
41014101
none. {
41024102
let base = GEP_tup_like_1(bcx, t, base_val, [0, i]);
@@ -4198,20 +4198,17 @@ fn trans_expr(cx: @block_ctxt, e: @ast::expr) -> result {
41984198
}
41994199
}
42004200

4201-
// FIXME add support for INIT/DROP_EXISTING
4202-
fn trans_expr_save_in(bcx: @block_ctxt, e: @ast::expr, dest: ValueRef)
4203-
-> @block_ctxt {
4201+
fn trans_expr_save_in(bcx: @block_ctxt, e: @ast::expr, dest: ValueRef,
4202+
kind: copy_action) -> @block_ctxt {
42044203
let tcx = bcx_tcx(bcx), t = ty::expr_ty(tcx, e);
4205-
if ty::type_is_bot(tcx, t) || ty::type_is_nil(tcx, t) {
4206-
ret trans_expr_dps(bcx, e, ignore);
4207-
} else if type_is_immediate(bcx_ccx(bcx), t) {
4208-
let cell = empty_dest_cell();
4209-
bcx = trans_expr_dps(bcx, e, by_val(cell));
4210-
Store(bcx, *cell, dest);
4211-
ret bcx;
4204+
let dst = if ty::type_is_bot(tcx, t) || ty::type_is_nil(tcx, t) {
4205+
ignore
4206+
} else if kind == INIT {
4207+
save_in(dest)
42124208
} else {
4213-
ret trans_expr_dps(bcx, e, save_in(dest));
4214-
}
4209+
overwrite(dest, t)
4210+
};
4211+
ret trans_expr_dps(bcx, e, dst);
42154212
}
42164213

42174214
fn trans_expr_by_ref(bcx: @block_ctxt, e: @ast::expr) -> result {
@@ -4338,13 +4335,9 @@ fn trans_expr_dps(bcx: @block_ctxt, e: @ast::expr, dest: dest)
43384335
}
43394336
ast::expr_assign(dst, src) {
43404337
assert dest == ignore;
4341-
let lhs_res = trans_lval(bcx, dst);
4342-
assert (lhs_res.is_mem);
4343-
let rhs = trans_lval(lhs_res.bcx, src);
4344-
let t = ty::expr_ty(bcx_tcx(bcx), src);
4345-
// FIXME: calculate copy init-ness in typestate.
4346-
ret move_val_if_temp(rhs.bcx, DROP_EXISTING, lhs_res.val,
4347-
rhs, t);
4338+
let {bcx, val: lhs_addr, is_mem} = trans_lval(bcx, dst);
4339+
assert is_mem;
4340+
ret trans_expr_save_in(bcx, src, lhs_addr, DROP_EXISTING);
43484341
}
43494342
ast::expr_swap(dst, src) {
43504343
assert dest == ignore;
@@ -4650,7 +4643,7 @@ fn trans_ret(bcx: @block_ctxt, e: option::t<@ast::expr>) -> @block_ctxt {
46504643
Store(cx, val, bcx.fcx.llretptr);
46514644
bcx = cx;
46524645
} else {
4653-
bcx = trans_expr_save_in(bcx, x, bcx.fcx.llretptr);
4646+
bcx = trans_expr_save_in(bcx, x, bcx.fcx.llretptr, INIT);
46544647
}
46554648
}
46564649
_ {}
@@ -4694,13 +4687,9 @@ fn init_local(bcx: @block_ctxt, local: @ast::local) -> @block_ctxt {
46944687
some(init) {
46954688
alt init.op {
46964689
ast::init_assign. {
4697-
// Use the type of the RHS because if it's _|_, the LHS
4698-
// type might be something else, but we don't want to copy
4699-
// the value.
4700-
ty = node_id_type(bcx_ccx(bcx), init.expr.id);
4701-
let sub = trans_lval(bcx, init.expr);
4702-
bcx = move_val_if_temp(sub.bcx, INIT, llptr, sub, ty);
4690+
bcx = trans_expr_save_in(bcx, init.expr, llptr, INIT);
47034691
}
4692+
// FIXME[DPS] do a save_in when expr isn't lval
47044693
ast::init_move. {
47054694
let sub = trans_lval(bcx, init.expr);
47064695
bcx = move_val(sub.bcx, INIT, llptr, sub, ty);

src/comp/middle/trans_uniq.rs

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

src/comp/middle/trans_vec.rs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ fn get_alloc(bcx: @block_ctxt, vptrptr: ValueRef) -> ValueRef {
2020
}
2121
fn get_dataptr(bcx: @block_ctxt, vptrptr: ValueRef, unit_ty: TypeRef) ->
2222
ValueRef {
23-
let ptr = GEPi(bcx, Load(bcx, vptrptr), [0, abi::vec_elt_elems as int]);
23+
ret get_dataptr_simple(bcx, Load(bcx, vptrptr), unit_ty);
24+
}
25+
fn get_dataptr_simple(bcx: @block_ctxt, vptr: ValueRef, unit_ty: TypeRef)
26+
-> ValueRef {
27+
let ptr = GEPi(bcx, vptr, [0, abi::vec_elt_elems as int]);
2428
PointerCast(bcx, ptr, T_ptr(unit_ty))
2529
}
2630

@@ -49,8 +53,7 @@ type alloc_result =
4953
llunitsz: ValueRef,
5054
llunitty: TypeRef};
5155

52-
fn alloc(bcx: @block_ctxt, vec_ty: ty::t, elts: uint, dest: dest)
53-
-> alloc_result {
56+
fn alloc(bcx: @block_ctxt, vec_ty: ty::t, elts: uint) -> alloc_result {
5457
let unit_ty = ty::sequence_element_type(bcx_tcx(bcx), vec_ty);
5558
let llunitty = type_of_or_i8(bcx, unit_ty);
5659
let llvecty = T_vec(llunitty);
@@ -61,11 +64,8 @@ fn alloc(bcx: @block_ctxt, vec_ty: ty::t, elts: uint, dest: dest)
6164
let {bcx: bcx, val: vptr} = alloc_raw(bcx, fill, alloc);
6265
let vptr = PointerCast(bcx, vptr, T_ptr(llvecty));
6366

64-
let vptrptr = alt dest { trans::save_in(a) { a } };
65-
Store(bcx, vptr, vptrptr);
66-
// add_clean_temp(bcx, vptrptr, vec_ty);
6767
ret {bcx: bcx,
68-
val: vptrptr,
68+
val: vptr,
6969
unit_ty: unit_ty,
7070
llunitsz: unit_sz,
7171
llunitty: llunitty};
@@ -110,16 +110,15 @@ fn trans_vec(bcx: @block_ctxt, args: [@ast::expr], id: ast::node_id,
110110
}
111111
let vec_ty = node_id_type(bcx_ccx(bcx), id);
112112
let {bcx: bcx,
113-
val: vptrptr,
113+
val: vptr,
114114
llunitsz: llunitsz,
115115
unit_ty: unit_ty,
116116
llunitty: llunitty} =
117-
alloc(bcx, vec_ty, vec::len(args), dest);
117+
alloc(bcx, vec_ty, vec::len(args));
118118

119-
let vptr = Load(bcx, vptrptr);
120119
add_clean_free(bcx, vptr, true);
121120
// Store the individual elements.
122-
let dataptr = get_dataptr(bcx, vptrptr, llunitty);
121+
let dataptr = get_dataptr_simple(bcx, vptr, llunitty);
123122
let i = 0u, temp_cleanups = [vptr];
124123
for e in args {
125124
let lv = trans_lval(bcx, e);
@@ -133,17 +132,28 @@ fn trans_vec(bcx: @block_ctxt, args: [@ast::expr], id: ast::node_id,
133132
i += 1u;
134133
}
135134
for clean in temp_cleanups { revoke_clean(bcx, clean); }
135+
let vptrptr = alt dest {
136+
trans::save_in(a) { a }
137+
trans::overwrite(a, t) { bcx = trans::drop_ty(bcx, a, t); a }
138+
};
139+
Store(bcx, vptr, vptrptr);
136140
ret bcx;
137141
}
142+
138143
fn trans_str(bcx: @block_ctxt, s: str, dest: dest) -> @block_ctxt {
139144
let veclen = std::str::byte_len(s) + 1u; // +1 for \0
140-
let {bcx: bcx, val: sptrptr, _} =
141-
alloc(bcx, ty::mk_str(bcx_tcx(bcx)), veclen, dest);
145+
let {bcx: bcx, val: sptr, _} =
146+
alloc(bcx, ty::mk_str(bcx_tcx(bcx)), veclen);
142147

143148
let llcstr = C_cstr(bcx_ccx(bcx), s);
144149
let bcx =
145-
call_memmove(bcx, get_dataptr(bcx, sptrptr, T_i8()), llcstr,
150+
call_memmove(bcx, get_dataptr_simple(bcx, sptr, T_i8()), llcstr,
146151
C_uint(veclen)).bcx;
152+
let sptrptr = alt dest {
153+
trans::save_in(a) { a }
154+
trans::overwrite(a, t) { bcx = trans::drop_ty(bcx, a, t); a }
155+
};
156+
Store(bcx, sptr, sptrptr);
147157
ret bcx;
148158
}
149159

@@ -237,11 +247,9 @@ fn trans_add(bcx: @block_ctxt, vec_ty: ty::t, lhsptr: ValueRef,
237247
let new_fill = Add(bcx, lhs_fill, rhs_fill);
238248
let {bcx: bcx, val: new_vec_ptr} = alloc_raw(bcx, new_fill, new_fill);
239249
new_vec_ptr = PointerCast(bcx, new_vec_ptr, T_ptr(T_vec(llunitty)));
240-
let new_vec_ptr_ptr = alt dest { trans::save_in(a) { a } };
241-
Store(bcx, new_vec_ptr, new_vec_ptr_ptr);
242250

243-
let write_ptr_ptr =
244-
do_spill_noroot(bcx, get_dataptr(bcx, new_vec_ptr_ptr, llunitty));
251+
let write_ptr_ptr = do_spill_noroot
252+
(bcx, get_dataptr_simple(bcx, new_vec_ptr, llunitty));
245253
let copy_fn =
246254
bind fn (bcx: @block_ctxt, addr: ValueRef, _ty: ty::t,
247255
write_ptr_ptr: ValueRef, unit_ty: ty::t, llunitsz: ValueRef)
@@ -259,7 +267,15 @@ fn trans_add(bcx: @block_ctxt, vec_ty: ty::t, lhsptr: ValueRef,
259267
}(_, _, _, write_ptr_ptr, unit_ty, llunitsz);
260268

261269
let bcx = iter_vec_raw(bcx, lhsptr, vec_ty, lhs_fill, copy_fn);
262-
ret iter_vec_raw(bcx, rhsptr, vec_ty, rhs_fill, copy_fn);
270+
bcx = iter_vec_raw(bcx, rhsptr, vec_ty, rhs_fill, copy_fn);
271+
alt dest {
272+
trans::save_in(a) { Store(bcx, new_vec_ptr, a); }
273+
trans::overwrite(a, t) {
274+
bcx = trans::drop_ty(bcx, a, t);
275+
Store(bcx, new_vec_ptr, a);
276+
}
277+
}
278+
ret bcx;
263279
}
264280

265281
type val_and_ty_fn = fn(@block_ctxt, ValueRef, ty::t) -> result;

0 commit comments

Comments
 (0)