Clean up a bunch of box related code.

This commit is contained in:
Michael Sullivan 2012-06-20 17:29:13 -07:00
parent b9d3ad0736
commit bacf9e9887
2 changed files with 27 additions and 38 deletions

View File

@ -398,18 +398,16 @@ fn malloc_general_dyn(bcx: block, t: ty::t, heap: heap, size: ValueRef) ->
ret {box: box, body: body};
}
fn malloc_boxed(bcx: block, t: ty::t) -> {box: ValueRef, body: ValueRef} {
malloc_general_dyn(bcx, t, heap_shared,
fn malloc_general(bcx: block, t: ty::t, heap: heap) ->
{box: ValueRef, body: ValueRef} {
malloc_general_dyn(bcx, t, heap,
llsize_of(bcx.ccx(), type_of(bcx.ccx(), t)))
}
fn malloc_boxed(bcx: block, t: ty::t) -> {box: ValueRef, body: ValueRef} {
malloc_general(bcx, t, heap_shared)
}
fn malloc_unique(bcx: block, t: ty::t) -> {box: ValueRef, body: ValueRef} {
malloc_general_dyn(bcx, t, heap_exchange,
llsize_of(bcx.ccx(), type_of(bcx.ccx(), t)))
}
fn malloc_unique_dyn(bcx: block, t: ty::t, size: ValueRef
) -> {box: ValueRef, body: ValueRef} {
malloc_general_dyn(bcx, t, heap_exchange, size)
malloc_general(bcx, t, heap_exchange)
}
// Type descriptor and type glue stuff
@ -1487,6 +1485,19 @@ fn trans_lit(cx: block, e: @ast::expr, lit: ast::lit, dest: dest) -> block {
}
}
fn trans_boxed_expr(bcx: block, contents: @ast::expr,
t: ty::t, heap: heap,
dest: dest) -> block {
let _icx = bcx.insn_ctxt("trans_boxed_expr");
let {box, body} = malloc_general(bcx, t, heap);
add_clean_free(bcx, box, true);
let bcx = trans_expr_save_in(bcx, contents, body);
revoke_clean(bcx, box);
ret store_in_dest(bcx, box, dest);
}
fn trans_unary(bcx: block, op: ast::unop, e: @ast::expr,
un_expr: @ast::expr, dest: dest) -> block {
let _icx = bcx.insn_ctxt("trans_unary");
@ -1509,35 +1520,25 @@ fn trans_unary(bcx: block, op: ast::unop, e: @ast::expr,
alt op {
ast::not {
let {bcx, val} = trans_temp_expr(bcx, e);
ret store_in_dest(bcx, Not(bcx, val), dest);
store_in_dest(bcx, Not(bcx, val), dest)
}
ast::neg {
let {bcx, val} = trans_temp_expr(bcx, e);
let neg = if ty::type_is_fp(e_ty) {
FNeg(bcx, val)
} else { Neg(bcx, val) };
ret store_in_dest(bcx, neg, dest);
store_in_dest(bcx, neg, dest)
}
ast::box(_) {
let mut {box, body} = malloc_boxed(bcx, e_ty);
add_clean_free(bcx, box, false);
// Cast the body type to the type of the value. This is needed to
// make enums work, since enums have a different LLVM type depending
// on whether they're boxed or not
let ccx = bcx.ccx();
let llety = T_ptr(type_of(ccx, e_ty));
body = PointerCast(bcx, body, llety);
let bcx = trans_expr_save_in(bcx, e, body);
revoke_clean(bcx, box);
ret store_in_dest(bcx, box, dest);
trans_boxed_expr(bcx, e, e_ty, heap_shared, dest)
}
ast::uniq(_) {
ret uniq::trans_uniq(bcx, e, un_expr.id, dest);
trans_boxed_expr(bcx, e, e_ty, heap_exchange, dest)
}
ast::deref {
bcx.sess().bug("deref expressions should have been \
translated using trans_lval(), not \
trans_unary()");
trans_unary()")
}
}
}

View File

@ -5,19 +5,7 @@ import build::*;
import base::*;
import shape::llsize_of;
export trans_uniq, make_free_glue, autoderef, duplicate;
fn trans_uniq(bcx: block, contents: @ast::expr,
node_id: ast::node_id, dest: dest) -> block {
let _icx = bcx.insn_ctxt("uniq::trans_uniq");
let uniq_ty = node_id_type(bcx, node_id);
let contents_ty = content_ty(uniq_ty);
let {box, body} = malloc_unique(bcx, contents_ty);
add_clean_free(bcx, box, true);
let bcx = trans_expr_save_in(bcx, contents, body);
revoke_clean(bcx, box);
ret store_in_dest(bcx, box, dest);
}
export make_free_glue, autoderef, duplicate;
fn make_free_glue(bcx: block, vptr: ValueRef, t: ty::t)
-> block {
@ -64,4 +52,4 @@ fn duplicate(bcx: block, v: ValueRef, t: ty::t) -> result {
Store(bcx, td, dst_tydesc_ptr);
ret rslt(bcx, dst_box);
}
}