Skip to content

Commit 8492eed

Browse files
committed
Move uniq trans code to trans_uniq module
Issue #409
1 parent 0f0ba33 commit 8492eed

File tree

3 files changed

+77
-54
lines changed

3 files changed

+77
-54
lines changed

src/comp/middle/trans.rs

+2-54
Original file line numberDiff line numberDiff line change
@@ -1317,19 +1317,7 @@ fn make_free_glue(bcx: @block_ctxt, v0: ValueRef, t: ty::t) {
13171317
} else { bcx }
13181318
}
13191319
ty::ty_uniq(content_mt) {
1320-
let free_cx = new_sub_block_ctxt(bcx, "uniq_free");
1321-
let next_cx = new_sub_block_ctxt(bcx, "uniq_free_next");
1322-
let vptr = Load(bcx, v0);
1323-
let null_test = IsNull(bcx, vptr);
1324-
CondBr(bcx, null_test, next_cx.llbb, free_cx.llbb);
1325-
1326-
let bcx = free_cx;
1327-
let bcx = drop_ty(bcx, vptr, content_mt.ty);
1328-
let bcx = trans_shared_free(bcx, vptr);
1329-
Store(bcx, C_null(val_ty(vptr)), v0);
1330-
Br(bcx, next_cx.llbb);
1331-
1332-
next_cx
1320+
trans_uniq::make_free_glue(bcx, v0, t)
13331321
}
13341322
ty::ty_obj(_) {
13351323
// Call through the obj's own fields-drop glue first.
@@ -2198,7 +2186,7 @@ fn trans_unary(cx: @block_ctxt, op: ast::unop, e: @ast::expr,
21982186
ret rslt(bcx, sub.box);
21992187
}
22002188
ast::uniq(_) {
2201-
ret trans_uniq(cx, e, id);
2189+
ret trans_uniq::trans_uniq(cx, e, id);
22022190
}
22032191
ast::deref. {
22042192
bcx_ccx(cx).sess.bug("deref expressions should have been \
@@ -4511,46 +4499,6 @@ fn trans_put(in_cx: @block_ctxt, e: option::t<@ast::expr>) -> result {
45114499
ret rslt(next_cx, C_nil());
45124500
}
45134501

4514-
fn trans_uniq(cx: @block_ctxt, contents: @ast::expr,
4515-
node_id: ast::node_id) -> result {
4516-
let bcx = cx;
4517-
4518-
let lv = trans_lval(bcx, contents);
4519-
bcx = lv.bcx;
4520-
4521-
let uniq_ty = node_id_type(bcx_ccx(cx), node_id);
4522-
let {bcx, val: llptr} = alloc_uniq(bcx, uniq_ty);
4523-
4524-
bcx = move_val_if_temp(bcx, INIT, llptr, lv,
4525-
ty_uniq_contents(bcx, uniq_ty));
4526-
4527-
ret rslt(bcx, llptr);
4528-
}
4529-
4530-
fn ty_uniq_contents(cx: @block_ctxt, uniq_ty: ty::t) -> ty::t {
4531-
alt ty::struct(bcx_tcx(cx), uniq_ty) {
4532-
ty::ty_uniq({ty: ct, _}) { ct }
4533-
}
4534-
}
4535-
4536-
fn alloc_uniq(cx: @block_ctxt, uniq_ty: ty::t) -> result {
4537-
let bcx = cx;
4538-
let contents_ty = ty_uniq_contents(cx, uniq_ty);
4539-
let r = size_of(bcx, contents_ty);
4540-
bcx = r.bcx;
4541-
let llsz = r.val;
4542-
4543-
let llptrty = T_ptr(type_of_or_i8(bcx, contents_ty));
4544-
4545-
r = trans_shared_malloc(bcx, llptrty, llsz);
4546-
bcx = r.bcx;
4547-
let llptr = r.val;
4548-
4549-
add_clean_temp(bcx, llptr, uniq_ty);
4550-
4551-
ret rslt(bcx, llptr);
4552-
}
4553-
45544502
fn trans_break_cont(sp: span, cx: @block_ctxt, to_end: bool) -> result {
45554503
let bcx = cx;
45564504
// Locate closest loop block, outputting cleanup as we go.

src/comp/middle/trans_uniq.rs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import syntax::ast;
2+
import lib::llvm::llvm::ValueRef;
3+
import trans_common::*;
4+
import trans_build::*;
5+
import trans::{
6+
trans_shared_malloc,
7+
type_of_or_i8,
8+
size_of,
9+
move_val_if_temp,
10+
node_id_type,
11+
trans_lval,
12+
INIT,
13+
trans_shared_free,
14+
drop_ty,
15+
new_sub_block_ctxt
16+
};
17+
18+
export trans_uniq, make_free_glue;
19+
20+
fn trans_uniq(cx: @block_ctxt, contents: @ast::expr,
21+
node_id: ast::node_id) -> result {
22+
let bcx = cx;
23+
24+
let lv = trans_lval(bcx, contents);
25+
bcx = lv.bcx;
26+
27+
let uniq_ty = node_id_type(bcx_ccx(cx), node_id);
28+
let {bcx, val: llptr} = alloc_uniq(bcx, uniq_ty);
29+
30+
bcx = move_val_if_temp(bcx, INIT, llptr, lv,
31+
content_ty(bcx, uniq_ty));
32+
33+
ret rslt(bcx, llptr);
34+
}
35+
36+
fn alloc_uniq(cx: @block_ctxt, uniq_ty: ty::t) -> result {
37+
let bcx = cx;
38+
let contents_ty = content_ty(bcx, uniq_ty);
39+
let r = size_of(bcx, contents_ty);
40+
bcx = r.bcx;
41+
let llsz = r.val;
42+
43+
let llptrty = T_ptr(type_of_or_i8(bcx, contents_ty));
44+
45+
r = trans_shared_malloc(bcx, llptrty, llsz);
46+
bcx = r.bcx;
47+
let llptr = r.val;
48+
49+
add_clean_temp(bcx, llptr, uniq_ty);
50+
51+
ret rslt(bcx, llptr);
52+
}
53+
54+
fn make_free_glue(bcx: @block_ctxt, v: ValueRef, t: ty::t) -> @block_ctxt {
55+
let free_cx = new_sub_block_ctxt(bcx, "uniq_free");
56+
let next_cx = new_sub_block_ctxt(bcx, "uniq_free_next");
57+
let vptr = Load(bcx, v);
58+
let null_test = IsNull(bcx, vptr);
59+
CondBr(bcx, null_test, next_cx.llbb, free_cx.llbb);
60+
61+
let bcx = free_cx;
62+
let bcx = drop_ty(bcx, vptr, content_ty(bcx, t));
63+
let bcx = trans_shared_free(bcx, vptr);
64+
Store(bcx, C_null(val_ty(vptr)), v);
65+
Br(bcx, next_cx.llbb);
66+
67+
next_cx
68+
}
69+
70+
fn content_ty(bcx: @block_ctxt, t: ty::t) -> ty::t {
71+
alt ty::struct(bcx_tcx(bcx), t) {
72+
ty::ty_uniq({ty: ct, _}) { ct }
73+
}
74+
}

src/comp/rustc.rc

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ mod middle {
1919
mod trans;
2020
mod trans_alt;
2121
mod trans_objects;
22+
mod trans_uniq;
2223
mod trans_vec;
2324
mod ty;
2425
mod ast_map;

0 commit comments

Comments
 (0)