Skip to content

Commit 3c1de96

Browse files
committed
rustc: Use the crate index to look up defs
1 parent ef0c903 commit 3c1de96

File tree

3 files changed

+58
-29
lines changed

3 files changed

+58
-29
lines changed

src/comp/front/creader.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -328,43 +328,40 @@ impure fn resolve_path(vec[ast.ident] path, vec[u8] data) -> resolve_result {
328328
}
329329

330330
impure fn move_to_item(&ebml.reader ebml_r, int item_id) {
331-
while (ebml.bytes_left(ebml_r) > 0u) {
332-
auto outer_ebml_tag = ebml.peek(ebml_r);
333-
if (outer_ebml_tag.id == metadata.tag_items) {
334-
ebml.move_to_first_child(ebml_r);
335-
336-
while (ebml.bytes_left(ebml_r) > 0u) {
337-
auto inner_ebml_tag = ebml.peek(ebml_r);
338-
if (inner_ebml_tag.id == metadata.tag_items_data_item) {
339-
ebml.move_to_first_child(ebml_r);
331+
ebml.move_to_sibling_with_id(ebml_r, metadata.tag_items);
332+
ebml.move_to_child_with_id(ebml_r, metadata.tag_items_index);
333+
ebml.move_to_child_with_id(ebml_r, metadata.tag_items_index_table);
334+
ebml.move_to_first_child(ebml_r);
340335

341-
while (ebml.bytes_left(ebml_r) > 0u) {
342-
auto innermost_ebml_tag = ebml.peek(ebml_r);
343-
if (innermost_ebml_tag.id == metadata.tag_def_id) {
344-
ebml.move_to_first_child(ebml_r);
345-
auto did_data = ebml.read_data(ebml_r);
346-
ebml.move_to_parent(ebml_r);
336+
// Move to the bucket.
337+
auto bucket_index = metadata.hash_def_num(item_id) % 256u;
338+
auto buf_reader = ebml_r.reader.get_buf_reader();
339+
buf_reader.seek((bucket_index * 4u) as int, io.seek_cur);
340+
auto bucket_pos = ebml_r.reader.read_be_uint(4u);
341+
ebml.reset_reader(ebml_r, bucket_pos);
347342

348-
auto this_did = parse_def_id(did_data);
349-
if (this_did._1 == item_id) {
350-
// Move to the start of this item's data.
351-
ebml.move_to_parent(ebml_r);
352-
ebml.move_to_first_child(ebml_r);
353-
ret;
354-
}
355-
}
356-
ebml.move_to_next_sibling(ebml_r);
357-
}
358-
ebml.move_to_parent(ebml_r);
359-
}
360-
ebml.move_to_next_sibling(ebml_r);
343+
// Search to find the item ID in the bucket.
344+
check (ebml.peek(ebml_r).id == metadata.tag_items_index_buckets_bucket);
345+
ebml.move_to_first_child(ebml_r);
346+
while (ebml.bytes_left(ebml_r) > 0u) {
347+
if (ebml.peek(ebml_r).id ==
348+
metadata.tag_items_index_buckets_bucket_elt) {
349+
ebml.move_to_first_child(ebml_r);
350+
auto pos = ebml_r.reader.read_be_uint(4u);
351+
auto this_item_id = ebml_r.reader.read_be_uint(4u) as int;
352+
if (item_id == this_item_id) {
353+
// Found the item. Move to its data and return.
354+
ebml.reset_reader(ebml_r, pos);
355+
check (ebml.peek(ebml_r).id == metadata.tag_items_data_item);
356+
ebml.move_to_first_child(ebml_r);
357+
ret;
361358
}
362359
ebml.move_to_parent(ebml_r);
363360
}
364361
ebml.move_to_next_sibling(ebml_r);
365362
}
366363

367-
log #fmt("move_to_item: item not found: %d", item_id);
364+
log #fmt("item %d not found in bucket at pos %u", item_id, bucket_pos);
368365
fail;
369366
}
370367

src/lib/ebml.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,32 @@ impure fn move_to_parent(&reader r) {
9090
r.reader.seek(st.tag_pos as int, io.seek_set);
9191
}
9292

93+
// Moves to the sibling of the current item with the given tag ID.
94+
impure fn move_to_sibling_with_id(&reader r, uint tag_id) {
95+
while (peek(r).id != tag_id) {
96+
move_to_next_sibling(r);
97+
}
98+
}
99+
100+
// Moves to the first child of the current item with the given tag ID.
101+
impure fn move_to_child_with_id(&reader r, uint tag_id) {
102+
move_to_first_child(r);
103+
move_to_sibling_with_id(r, tag_id);
104+
}
105+
93106
// Reads the data segment of a tag.
94107
impure fn read_data(&reader r) -> vec[u8] {
95108
ret r.reader.read_bytes(bytes_left(r));
96109
}
97110

111+
// Blows away the tag stack and moves the reader to the given byte position.
112+
impure fn reset_reader(&reader r, uint pos) {
113+
// FIXME: rustc "ty_var in trans.type_of" bug
114+
let vec[ebml_state] states = vec();
115+
r.states = states;
116+
r.reader.seek(pos as int, io.seek_set);
117+
}
118+
98119
impure fn peek(&reader r) -> ebml_tag {
99120
check (bytes_left(r) > 0u);
100121
auto pos = r.reader.tell();

src/lib/io.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type reader =
3939
impure fn read_c_str() -> str;
4040
impure fn read_le_uint(uint size) -> uint;
4141
impure fn read_le_int(uint size) -> int;
42+
impure fn read_be_uint(uint size) -> uint;
4243

4344
impure fn seek(int offset, seek_style whence);
4445
impure fn tell() -> uint; // FIXME: eventually u64
@@ -167,6 +168,16 @@ state obj new_reader(buf_reader rdr) {
167168
}
168169
ret val as int;
169170
}
171+
// FIXME deal with eof?
172+
impure fn read_be_uint(uint size) -> uint {
173+
auto val = 0u;
174+
auto sz = size; // FIXME: trans.ml bug workaround
175+
while (sz > 0u) {
176+
sz -= 1u;
177+
val += (read_byte_from_buf_reader(rdr) as uint) << (sz * 8u);
178+
}
179+
ret val;
180+
}
170181
impure fn seek(int offset, seek_style whence) {
171182
ret rdr.seek(offset, whence);
172183
}

0 commit comments

Comments
 (0)