Skip to content

Commit e43729c

Browse files
committed
rustc: Refactor metadata.Encode.* to not require a type abbreviation table if abbreviation isn't enabled
1 parent 3f317ed commit e43729c

File tree

3 files changed

+53
-45
lines changed

3 files changed

+53
-45
lines changed

src/comp/middle/metadata.rs

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -53,59 +53,72 @@ const uint tag_index_table = 0x15u;
5353

5454
type ty_abbrev = rec(uint pos, uint len, str s);
5555

56+
tag abbrev_ctxt {
57+
ac_no_abbrevs;
58+
ac_use_abbrevs(hashmap[ty.t, ty_abbrev]);
59+
}
60+
5661
mod Encode {
5762

5863
type ctxt = rec(
5964
fn(ast.def_id) -> str ds, // Def -> str Callback.
6065
ty.ctxt tcx, // The type context.
61-
bool use_abbrevs,
62-
hashmap[ty.t, ty_abbrev] abbrevs // Type abbrevs.
66+
abbrev_ctxt abbrevs
6367
);
6468

69+
fn cx_uses_abbrevs(@ctxt cx) -> bool {
70+
alt (cx.abbrevs) {
71+
case (ac_no_abbrevs) { ret false; }
72+
case (ac_use_abbrevs(_)) { ret true; }
73+
}
74+
}
75+
6576
fn ty_str(@ctxt cx, ty.t t) -> str {
66-
assert (! cx.use_abbrevs);
77+
assert (!cx_uses_abbrevs(cx));
6778
auto sw = io.string_writer();
6879
enc_ty(sw.get_writer(), cx, t);
6980
ret sw.get_str();
7081
}
7182

7283
fn enc_ty(io.writer w, @ctxt cx, ty.t t) {
73-
if (cx.use_abbrevs) {
74-
alt (cx.abbrevs.find(t)) {
75-
case (some[ty_abbrev](?a)) {
76-
w.write_str(a.s);
77-
ret;
78-
}
79-
case (none[ty_abbrev]) {
80-
auto pos = w.get_buf_writer().tell();
81-
auto ss = enc_sty(w, cx, ty.struct(cx.tcx, t));
82-
auto end = w.get_buf_writer().tell();
83-
auto len = end-pos;
84-
fn estimate_sz(uint u) -> uint {
85-
auto n = u;
86-
auto len = 0u;
87-
while (n != 0u) {
88-
len += 1u;
89-
n = n >> 4u;
90-
}
91-
ret len;
84+
alt (cx.abbrevs) {
85+
case (ac_no_abbrevs) { enc_sty(w, cx, ty.struct(cx.tcx, t)); }
86+
case (ac_use_abbrevs(?abbrevs)) {
87+
alt (abbrevs.find(t)) {
88+
case (some[ty_abbrev](?a)) {
89+
w.write_str(a.s);
90+
ret;
9291
}
93-
auto abbrev_len =
94-
3u + estimate_sz(pos) + estimate_sz(len);
95-
96-
if (abbrev_len < len) {
97-
// I.e. it's actually an abbreviation.
98-
auto s = ("#"
99-
+ _uint.to_str(pos, 16u) + ":"
100-
+ _uint.to_str(len, 16u) + "#");
101-
auto a = rec(pos=pos, len=len, s=s);
102-
cx.abbrevs.insert(t, a);
92+
case (none[ty_abbrev]) {
93+
auto pos = w.get_buf_writer().tell();
94+
auto ss = enc_sty(w, cx, ty.struct(cx.tcx, t));
95+
auto end = w.get_buf_writer().tell();
96+
auto len = end-pos;
97+
fn estimate_sz(uint u) -> uint {
98+
auto n = u;
99+
auto len = 0u;
100+
while (n != 0u) {
101+
len += 1u;
102+
n = n >> 4u;
103+
}
104+
ret len;
105+
}
106+
auto abbrev_len =
107+
3u + estimate_sz(pos) + estimate_sz(len);
108+
109+
if (abbrev_len < len) {
110+
// I.e. it's actually an abbreviation.
111+
auto s = ("#"
112+
+ _uint.to_str(pos, 16u) + ":"
113+
+ _uint.to_str(len, 16u) + "#");
114+
auto a = rec(pos=pos, len=len, s=s);
115+
abbrevs.insert(t, a);
116+
}
117+
ret;
103118
}
104-
ret;
105119
}
106120
}
107121
}
108-
enc_sty(w, cx, ty.struct(cx.tcx, t));
109122
}
110123

111124
fn enc_mt(io.writer w, @ctxt cx, &ty.mt mt) {
@@ -406,7 +419,7 @@ fn encode_type(@trans.crate_ctxt cx, &ebml.writer ebml_w, ty.t typ) {
406419

407420
auto f = def_to_str;
408421
auto ty_str_ctxt = @rec(ds=f, tcx=cx.tcx,
409-
use_abbrevs=true, abbrevs=cx.type_abbrevs);
422+
abbrevs=ac_use_abbrevs(cx.type_abbrevs));
410423
Encode.enc_ty(io.new_writer_(ebml_w.writer), ty_str_ctxt, typ);
411424
ebml.end_tag(ebml_w);
412425
}

src/comp/middle/trans.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ fn get_type_sha1(@crate_ctxt ccx, ty.t t) -> str {
193193
auto f = metadata.def_to_str;
194194
// NB: do *not* use abbrevs here as we want the symbol names
195195
// to be independent of one another in the crate.
196-
auto cx = @rec(ds=f, tcx=ccx.tcx,
197-
use_abbrevs=false, abbrevs=ccx.type_abbrevs);
196+
auto cx = @rec(ds=f, tcx=ccx.tcx, abbrevs=metadata.ac_no_abbrevs);
198197
ccx.sha.input_str(metadata.Encode.ty_str(cx, t));
199198
hash = _str.substr(ccx.sha.result_str(), 0u, 16u);
200199
ccx.type_sha1s.insert(t, hash);
@@ -210,8 +209,7 @@ fn mangle_name_by_type(@crate_ctxt ccx, vec[str] path, ty.t t) -> str {
210209

211210
fn mangle_name_by_type_only(@crate_ctxt ccx, ty.t t, str name) -> str {
212211
auto f = metadata.def_to_str;
213-
auto cx = @rec(ds=f, tcx=ccx.tcx,
214-
use_abbrevs=false, abbrevs=ccx.type_abbrevs);
212+
auto cx = @rec(ds=f, tcx=ccx.tcx, abbrevs=metadata.ac_no_abbrevs);
215213
auto s = metadata.Encode.ty_str(cx, t);
216214

217215
auto hash = get_type_sha1(ccx, t);
@@ -816,9 +814,7 @@ fn type_of_inner(@crate_ctxt cx, ty.t t) -> TypeRef {
816814
}
817815

818816
assert (llty as int != 0);
819-
llvm.LLVMAddTypeName(cx.llmod,
820-
_str.buf(ty.ty_to_short_str(cx.tcx,
821-
cx.type_abbrevs, t)),
817+
llvm.LLVMAddTypeName(cx.llmod, _str.buf(ty.ty_to_short_str(cx.tcx, t)),
822818
llty);
823819
cx.lltypes.insert(t, llty);
824820
ret llty;

src/comp/middle/ty.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -648,10 +648,9 @@ fn ty_to_str(ctxt cx, &t typ) -> str {
648648
ret s;
649649
}
650650

651-
fn ty_to_short_str(ctxt cx, hashmap[ty.t, metadata.ty_abbrev] abbrevs,
652-
t typ) -> str {
651+
fn ty_to_short_str(ctxt cx, t typ) -> str {
653652
auto f = def_to_str;
654-
auto ecx = @rec(ds=f, tcx=cx, use_abbrevs=false, abbrevs=abbrevs);
653+
auto ecx = @rec(ds=f, tcx=cx, abbrevs=metadata.ac_no_abbrevs);
655654
auto s = metadata.Encode.ty_str(ecx, typ);
656655
if (_str.byte_len(s) >= 64u) { s = _str.substr(s, 0u, 64u); }
657656
ret s;

0 commit comments

Comments
 (0)