Skip to content

Commit f239ecb

Browse files
committed
rustc: Switch from storing nullary tags as constants to storing their discriminants
1 parent 8d8b3d9 commit f239ecb

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

src/comp/middle/trans.rs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ type glue_fns = rec(ValueRef activate_glue,
5959
ValueRef memcpy_glue,
6060
ValueRef bzero_glue);
6161

62-
type tag_info = rec(type_handle th, mutable uint size);
62+
type tag_info = rec(
63+
type_handle th,
64+
mutable uint size,
65+
mutable @hashmap[ast.def_id,ValueRef] lldiscrims
66+
);
6367

6468
state type crate_ctxt = rec(session.session sess,
6569
ModuleRef llmod,
@@ -2774,9 +2778,23 @@ fn trans_path(@block_ctxt cx, &ast.path p, &option.t[ast.def] dopt,
27742778
}
27752779
ret lval_generic_fn(cx, tup(params, fty), vid, ann);
27762780
} else {
2777-
// Nullary variants are just scalar constants.
2778-
check (cx.fcx.ccx.item_ids.contains_key(vid));
2779-
ret lval_val(cx, cx.fcx.ccx.item_ids.get(vid));
2781+
// Nullary variant.
2782+
auto tag_ty = node_ann_type(cx.fcx.ccx, ann);
2783+
auto info = cx.fcx.ccx.tags.get(tag_ty);
2784+
check (info.lldiscrims.contains_key(vid));
2785+
auto lldiscrim_gv = info.lldiscrims.get(vid);
2786+
auto lldiscrim = cx.build.Load(lldiscrim_gv);
2787+
2788+
auto alloc_result = alloc_ty(cx, tag_ty);
2789+
auto lltagblob = alloc_result.val;
2790+
auto lltagptr = alloc_result.bcx.build.PointerCast(
2791+
lltagblob, T_ptr(type_of(cx.fcx.ccx, tag_ty)));
2792+
2793+
auto lldiscrimptr = alloc_result.bcx.build.GEP(
2794+
lltagptr, vec(C_int(0), C_int(0)));
2795+
alloc_result.bcx.build.Store(lldiscrim, lldiscrimptr);
2796+
2797+
ret lval_val(alloc_result.bcx, lltagptr);
27802798
}
27812799
}
27822800
case (ast.def_const(?did)) {
@@ -4637,8 +4655,14 @@ fn collect_item(&@crate_ctxt cx, @ast.item i) -> @crate_ctxt {
46374655
case (ast.item_tag(_, ?variants, ?tps, ?tag_id)) {
46384656
auto vi = new_def_hash[uint]();
46394657
auto navi = new_def_hash[uint]();
4640-
cx.tags.insert(mk_plain_tag(tag_id), @rec(th=mk_type_handle(),
4641-
mutable size=0u));
4658+
4659+
auto info = @rec(
4660+
th=mk_type_handle(),
4661+
mutable size=0u,
4662+
mutable lldiscrims=@new_def_hash[ValueRef]()
4663+
);
4664+
4665+
cx.tags.insert(mk_plain_tag(tag_id), info);
46424666
cx.items.insert(tag_id, i);
46434667
}
46444668

@@ -4772,22 +4796,7 @@ fn trans_constant(&@crate_ctxt cx, @ast.item it) -> @crate_ctxt {
47724796
llvm.LLVMSetLinkage(discrim_gvar, lib.llvm.LLVMPrivateLinkage
47734797
as llvm.Linkage);
47744798

4775-
if (_vec.len[ast.variant_arg](variant.args) == 0u) {
4776-
// Nullary tags become constants. (N-ary tags are treated
4777-
// as functions and generated later.)
4778-
4779-
auto union_val = C_zero_byte_arr(info.size as uint);
4780-
auto val = C_struct(vec(discrim_val, union_val));
4781-
4782-
// FIXME: better name
4783-
auto gvar = llvm.LLVMAddGlobal(cx.llmod, val_ty(val),
4784-
_str.buf("tag"));
4785-
llvm.LLVMSetInitializer(gvar, val);
4786-
llvm.LLVMSetGlobalConstant(gvar, True);
4787-
llvm.LLVMSetLinkage(gvar, lib.llvm.LLVMPrivateLinkage
4788-
as llvm.Linkage);
4789-
cx.item_ids.insert(variant.id, gvar);
4790-
}
4799+
info.lldiscrims.insert(variant.id, discrim_gvar);
47914800

47924801
i += 1u;
47934802
}

0 commit comments

Comments
 (0)