Skip to content

Commit b99038c

Browse files
committed
rustc: Put all boxes into addrspace 1
1 parent 0e43e8c commit b99038c

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

src/rustc/lib/llvm.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,15 @@ fn type_to_str_inner(names: type_names, outer0: [TypeRef], ty: TypeRef) ->
10341034
ret "*\\" + int::str(n as int);
10351035
}
10361036
}
1037-
ret "*" +
1037+
let addrstr = {
1038+
let addrspace = llvm::LLVMGetPointerAddressSpace(ty) as uint;
1039+
if addrspace == 0u {
1040+
""
1041+
} else {
1042+
#fmt("addrspace(%u)", addrspace)
1043+
}
1044+
};
1045+
ret addrstr + "*" +
10381046
type_to_str_inner(names, outer, llvm::LLVMGetElementType(ty));
10391047
}
10401048
13 { ret "Vector"; }

src/rustc/middle/trans/alt.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,9 @@ fn compile_submatch(bcx: block, m: match, vals: [ValueRef],
436436
// Unbox in case of a box field
437437
if any_box_pat(m, col) {
438438
let box = Load(bcx, val);
439-
let unboxed = GEPi(bcx, box, [0u, abi::box_field_body]);
439+
let box_ty = node_id_type(bcx, pat_id);
440+
let box_no_addrspace = non_gc_box_cast(bcx, box, box_ty);
441+
let unboxed = GEPi(bcx, box_no_addrspace, [0u, abi::box_field_body]);
440442
compile_submatch(bcx, enter_box(dm, m, col, val), [unboxed]
441443
+ vals_left, chk, exits);
442444
ret;

src/rustc/middle/trans/base.rs

+21-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ resource icx_popper(ccx: @crate_ctxt) {
8888

8989
impl ccx_icx for @crate_ctxt {
9090
fn insn_ctxt(s: str) -> icx_popper {
91+
#debug("new insn_ctxt: %s", s);
9192
if (self.sess.opts.count_llvm_insns) {
9293
*self.stats.llvm_insn_ctxt += [s];
9394
}
@@ -356,7 +357,9 @@ fn malloc_boxed(bcx: block, t: ty::t) -> {box: ValueRef, body: ValueRef} {
356357
let _icx = bcx.insn_ctxt("trans_malloc_boxed");
357358
let mut ti = none;
358359
let box = malloc_boxed_raw(bcx, t, ti);
359-
let body = GEPi(bcx, box, [0u, abi::box_field_body]);
360+
let box_no_addrspace = non_gc_box_cast(
361+
bcx, box, ty::mk_imm_box(bcx.tcx(), t));
362+
let body = GEPi(bcx, box_no_addrspace, [0u, abi::box_field_body]);
360363
ret {box: box, body: body};
361364
}
362365

@@ -2399,7 +2402,8 @@ fn trans_lval(cx: block, e: @ast::expr) -> lval_result {
23992402
let t = expr_ty(cx, base);
24002403
let val = alt check ty::get(t).struct {
24012404
ty::ty_box(_) {
2402-
GEPi(sub.bcx, sub.val, [0u, abi::box_field_body])
2405+
let non_gc_val = non_gc_box_cast(sub.bcx, sub.val, t);
2406+
GEPi(sub.bcx, non_gc_val, [0u, abi::box_field_body])
24032407
}
24042408
ty::ty_res(_, _, _) {
24052409
GEPi(sub.bcx, sub.val, [0u, 1u])
@@ -2417,6 +2421,21 @@ fn trans_lval(cx: block, e: @ast::expr) -> lval_result {
24172421
}
24182422
}
24192423

2424+
#[doc = "
2425+
Get the type of a box in the default address space.
2426+
2427+
Shared box pointers live in address space 1 so the GC strategy can find them.
2428+
Before taking a pointer to the inside of a box it should be cast into address
2429+
space 0. Otherwise the resulting (non-box) pointer will be in the wrong
2430+
address space and thus be the wrong type.
2431+
"]
2432+
fn non_gc_box_cast(cx: block, val: ValueRef, t: ty::t) -> ValueRef {
2433+
#debug("non_gc_box_cast");
2434+
add_comment(cx, "non_gc_box_cast");
2435+
let non_gc_t = type_of_non_gc_box(cx.ccx(), t);
2436+
PointerCast(cx, val, non_gc_t)
2437+
}
2438+
24202439
fn lval_maybe_callee_to_lval(c: lval_maybe_callee, ty: ty::t) -> lval_result {
24212440
let must_bind = alt c.env { self_env(_, _, _) { true } _ { false } };
24222441
if must_bind {

src/rustc/middle/trans/common.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -642,12 +642,17 @@ fn T_box(cx: @crate_ctxt, t: TypeRef) -> TypeRef {
642642
ret T_struct(T_box_header_fields(cx) + [t]);
643643
}
644644

645+
fn T_box_ptr(t: TypeRef) -> TypeRef {
646+
const box_addrspace: uint = 1u;
647+
ret llvm::LLVMPointerType(t, box_addrspace as c_uint);
648+
}
649+
645650
fn T_opaque_box(cx: @crate_ctxt) -> TypeRef {
646651
ret T_box(cx, T_i8());
647652
}
648653

649654
fn T_opaque_box_ptr(cx: @crate_ctxt) -> TypeRef {
650-
ret T_ptr(T_opaque_box(cx));
655+
ret T_box_ptr(T_opaque_box(cx));
651656
}
652657

653658
fn T_port(cx: @crate_ctxt, _t: TypeRef) -> TypeRef {

src/rustc/middle/trans/type_of.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type_of;
1111
export type_of_explicit_args;
1212
export type_of_fn_from_ty;
1313
export type_of_fn;
14+
export type_of_non_gc_box;
1415

1516
fn type_of_explicit_args(cx: @crate_ctxt, inputs: [ty::arg]) -> [TypeRef] {
1617
vec::map(inputs) {|arg|
@@ -42,6 +43,24 @@ fn type_of_fn_from_ty(cx: @crate_ctxt, fty: ty::t) -> TypeRef {
4243
type_of_fn(cx, ty::ty_fn_args(fty), ty::ty_fn_ret(fty))
4344
}
4445

46+
fn type_of_non_gc_box(cx: @crate_ctxt, t: ty::t) -> TypeRef {
47+
assert !ty::type_has_vars(t);
48+
49+
let t_norm = ty::normalize_ty(cx.tcx, t);
50+
if t != t_norm {
51+
type_of_non_gc_box(cx, t_norm)
52+
} else {
53+
alt ty::get(t).struct {
54+
ty::ty_box(mt) {
55+
T_ptr(T_box(cx, type_of(cx, mt.ty)))
56+
}
57+
_ {
58+
cx.sess.bug("non-box in type_of_non_gc_box");
59+
}
60+
}
61+
}
62+
}
63+
4564
fn type_of(cx: @crate_ctxt, t: ty::t) -> TypeRef {
4665
assert !ty::type_has_vars(t);
4766

@@ -68,10 +87,10 @@ fn type_of(cx: @crate_ctxt, t: ty::t) -> TypeRef {
6887
ty::ty_estr(ty::vstore_uniq) |
6988
ty::ty_str { T_ptr(T_vec(cx, T_i8())) }
7089
ty::ty_enum(did, _) { type_of_enum(cx, did, t) }
71-
ty::ty_estr(ty::vstore_box) { T_ptr(T_box(cx, T_i8())) }
90+
ty::ty_estr(ty::vstore_box) { T_box_ptr(T_box(cx, T_i8())) }
7291
ty::ty_evec(mt, ty::vstore_box) |
73-
ty::ty_box(mt) { T_ptr(T_box(cx, type_of(cx, mt.ty))) }
74-
ty::ty_opaque_box { T_ptr(T_box(cx, T_i8())) }
92+
ty::ty_box(mt) { T_box_ptr(T_box(cx, type_of(cx, mt.ty))) }
93+
ty::ty_opaque_box { T_box_ptr(T_box(cx, T_i8())) }
7594
ty::ty_uniq(mt) { T_ptr(type_of(cx, mt.ty)) }
7695
ty::ty_evec(mt, ty::vstore_uniq) |
7796
ty::ty_vec(mt) { T_ptr(T_vec(cx, type_of(cx, mt.ty))) }

0 commit comments

Comments
 (0)