Skip to content

Commit b1d904b

Browse files
committed
---
yaml --- r: 1820 b: refs/heads/master c: c669f20 h: refs/heads/master v: v3
1 parent d578ef7 commit b1d904b

File tree

3 files changed

+294
-7
lines changed

3 files changed

+294
-7
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 0a42d5ae62771cbcdc4589ea9a019d8dbecb0ae7
2+
refs/heads/master: c669f2023109b400aa4a7297cb151ebfb71ae44e

trunk/src/comp/middle/metadata.rs

Lines changed: 282 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import std._str;
22
import std._vec;
3+
import std.ebml;
4+
import std.io;
35
import std.option;
46

57
import front.ast;
8+
import middle.fold;
69
import middle.trans;
710
import middle.ty;
811
import back.x86;
@@ -12,6 +15,22 @@ import lib.llvm.llvm;
1215
import lib.llvm.llvm.ValueRef;
1316
import lib.llvm.False;
1417

18+
const uint tag_paths = 0x01u;
19+
const uint tag_items = 0x02u;
20+
21+
const uint tag_paths_name = 0x03u;
22+
const uint tag_paths_item = 0x04u;
23+
const uint tag_paths_mod = 0x05u;
24+
25+
const uint tag_items_item = 0x06u;
26+
const uint tag_items_def_id = 0x07u;
27+
const uint tag_items_kind = 0x08u;
28+
const uint tag_items_ty_param = 0x09u;
29+
const uint tag_items_type = 0x0au;
30+
const uint tag_items_symbol = 0x0bu;
31+
const uint tag_items_variant = 0x0cu;
32+
const uint tag_items_tag_id = 0x0du;
33+
1534
// Type encoding
1635

1736
// Compact string representation for ty.t values. API ty_str & parse_from_str.
@@ -102,7 +121,8 @@ fn sty_str(ty.sty st, def_str ds) -> str {
102121
}
103122
case (ty.ty_var(?id)) {ret "X" + common.istr(id);}
104123
case (ty.ty_native) {ret "E";}
105-
// TODO (maybe?) ty_param(ast.def_id), ty_type;
124+
case (ty.ty_param(?def)) {ret "p" + ds(def);}
125+
// TODO (maybe?) ty_type;
106126
}
107127
}
108128

@@ -128,13 +148,270 @@ fn C_postr(str s) -> ValueRef {
128148
ret llvm.LLVMConstString(_str.buf(s), _str.byte_len(s), False);
129149
}
130150

131-
fn collect_meta_directives(@trans.crate_ctxt cx, @ast.crate crate)
132-
-> ValueRef {
133-
ret C_postr("Hello world!"); // TODO
151+
152+
// Path table encoding
153+
154+
fn encode_name(&ebml.writer ebml_w, str name) {
155+
ebml.start_tag(ebml_w, tag_paths_name);
156+
ebml_w.writer.write(_str.bytes(name));
157+
ebml.end_tag(ebml_w);
158+
}
159+
160+
fn encode_def_id(&ebml.writer ebml_w, &ast.def_id id) {
161+
ebml.start_tag(ebml_w, tag_items_def_id);
162+
ebml_w.writer.write(_str.bytes(def_to_str(id)));
163+
ebml.end_tag(ebml_w);
164+
}
165+
166+
fn encode_tag_variant_paths(&ebml.writer ebml_w, vec[ast.variant] variants) {
167+
for (ast.variant variant in variants) {
168+
ebml.start_tag(ebml_w, tag_paths_item);
169+
encode_name(ebml_w, variant.name);
170+
encode_def_id(ebml_w, variant.id);
171+
ebml.end_tag(ebml_w);
172+
}
173+
}
174+
175+
fn encode_native_module_item_paths(&ebml.writer ebml_w,
176+
&ast.native_mod nmod) {
177+
for (@ast.native_item nitem in nmod.items) {
178+
alt (nitem.node) {
179+
case (ast.native_item_ty(?id, ?did)) {
180+
ebml.start_tag(ebml_w, tag_paths_item);
181+
encode_name(ebml_w, id);
182+
encode_def_id(ebml_w, did);
183+
ebml.end_tag(ebml_w);
184+
}
185+
case (ast.native_item_fn(?id, _, _, _, ?did, _)) {
186+
ebml.start_tag(ebml_w, tag_paths_item);
187+
encode_name(ebml_w, id);
188+
encode_def_id(ebml_w, did);
189+
ebml.end_tag(ebml_w);
190+
}
191+
}
192+
}
193+
}
194+
195+
fn encode_module_item_paths(&ebml.writer ebml_w, &ast._mod module) {
196+
// TODO: only encode exported items
197+
for (@ast.item it in module.items) {
198+
alt (it.node) {
199+
case (ast.item_const(?id, _, ?tps, ?did, ?ann)) {
200+
ebml.start_tag(ebml_w, tag_paths_item);
201+
encode_name(ebml_w, id);
202+
encode_def_id(ebml_w, did);
203+
ebml.end_tag(ebml_w);
204+
}
205+
case (ast.item_fn(?id, _, ?tps, ?did, ?ann)) {
206+
ebml.start_tag(ebml_w, tag_paths_item);
207+
encode_name(ebml_w, id);
208+
encode_def_id(ebml_w, did);
209+
ebml.end_tag(ebml_w);
210+
}
211+
case (ast.item_mod(?id, ?_mod, _)) {
212+
ebml.start_tag(ebml_w, tag_paths_mod);
213+
encode_name(ebml_w, id);
214+
encode_module_item_paths(ebml_w, _mod);
215+
ebml.end_tag(ebml_w);
216+
}
217+
case (ast.item_native_mod(?id, ?nmod, _)) {
218+
ebml.start_tag(ebml_w, tag_paths_mod);
219+
encode_name(ebml_w, id);
220+
encode_native_module_item_paths(ebml_w, nmod);
221+
ebml.end_tag(ebml_w);
222+
}
223+
case (ast.item_ty(?id, _, ?tps, ?did, ?ann)) {
224+
ebml.start_tag(ebml_w, tag_paths_item);
225+
encode_name(ebml_w, id);
226+
encode_def_id(ebml_w, did);
227+
ebml.end_tag(ebml_w);
228+
}
229+
case (ast.item_tag(?id, ?variants, ?tps, ?did)) {
230+
ebml.start_tag(ebml_w, tag_paths_item);
231+
encode_name(ebml_w, id);
232+
encode_tag_variant_paths(ebml_w, variants);
233+
encode_def_id(ebml_w, did);
234+
ebml.end_tag(ebml_w);
235+
}
236+
case (ast.item_obj(?id, _, ?tps, ?did, ?ann)) {
237+
ebml.start_tag(ebml_w, tag_paths_item);
238+
encode_name(ebml_w, id);
239+
encode_def_id(ebml_w, did);
240+
ebml.end_tag(ebml_w);
241+
}
242+
}
243+
}
244+
}
245+
246+
fn encode_item_paths(&ebml.writer ebml_w, @ast.crate crate) {
247+
ebml.start_tag(ebml_w, tag_paths);
248+
encode_module_item_paths(ebml_w, crate.node.module);
249+
ebml.end_tag(ebml_w);
250+
}
251+
252+
253+
// Item info table encoding
254+
255+
fn encode_kind(&ebml.writer ebml_w, u8 c) {
256+
ebml.start_tag(ebml_w, tag_items_kind);
257+
ebml_w.writer.write(vec(c));
258+
ebml.end_tag(ebml_w);
259+
}
260+
261+
fn def_to_str(ast.def_id did) -> str {
262+
ret #fmt("%d:%d", did._0, did._1);
263+
}
264+
265+
// TODO: We need to encode the "crate numbers" somewhere for diamond imports.
266+
fn encode_type_params(&ebml.writer ebml_w, vec[ast.ty_param] tps) {
267+
for (ast.ty_param tp in tps) {
268+
ebml.start_tag(ebml_w, tag_items_ty_param);
269+
ebml_w.writer.write(_str.bytes(def_to_str(tp.id)));
270+
ebml.end_tag(ebml_w);
271+
}
272+
}
273+
274+
fn encode_type(&ebml.writer ebml_w, @ty.t typ) {
275+
ebml.start_tag(ebml_w, tag_items_type);
276+
auto f = def_to_str;
277+
ebml_w.writer.write(_str.bytes(ty_str(typ, f)));
278+
ebml.end_tag(ebml_w);
279+
}
280+
281+
fn encode_symbol(@trans.crate_ctxt cx, &ebml.writer ebml_w, ast.def_id did) {
282+
ebml.start_tag(ebml_w, tag_items_symbol);
283+
ebml_w.writer.write(_str.bytes(cx.item_symbols.get(did)));
284+
ebml.end_tag(ebml_w);
285+
}
286+
287+
fn encode_discriminant(@trans.crate_ctxt cx, &ebml.writer ebml_w,
288+
ast.def_id did) {
289+
ebml.start_tag(ebml_w, tag_items_symbol);
290+
ebml_w.writer.write(_str.bytes(cx.discrim_symbols.get(did)));
291+
ebml.end_tag(ebml_w);
292+
}
293+
294+
fn encode_tag_id(&ebml.writer ebml_w, &ast.def_id id) {
295+
ebml.start_tag(ebml_w, tag_items_tag_id);
296+
ebml_w.writer.write(_str.bytes(def_to_str(id)));
297+
ebml.end_tag(ebml_w);
298+
}
299+
300+
301+
fn encode_tag_variant_info(@trans.crate_ctxt cx, &ebml.writer ebml_w,
302+
ast.def_id did, vec[ast.variant] variants) {
303+
for (ast.variant variant in variants) {
304+
ebml.start_tag(ebml_w, tag_items_variant);
305+
encode_def_id(ebml_w, variant.id);
306+
encode_tag_id(ebml_w, did);
307+
encode_type(ebml_w, trans.node_ann_type(cx, variant.ann));
308+
if (_vec.len[ast.variant_arg](variant.args) > 0u) {
309+
encode_symbol(cx, ebml_w, variant.id);
310+
}
311+
encode_discriminant(cx, ebml_w, variant.id);
312+
ebml.end_tag(ebml_w);
313+
}
314+
}
315+
316+
fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
317+
@ast.item item) {
318+
alt (item.node) {
319+
case (ast.item_const(_, _, _, ?did, ?ann)) {
320+
ebml.start_tag(ebml_w, tag_items_item);
321+
encode_def_id(ebml_w, did);
322+
encode_kind(ebml_w, 'c' as u8);
323+
encode_type(ebml_w, trans.node_ann_type(cx, ann));
324+
encode_symbol(cx, ebml_w, did);
325+
ebml.end_tag(ebml_w);
326+
}
327+
case (ast.item_fn(_, _, ?tps, ?did, ?ann)) {
328+
ebml.start_tag(ebml_w, tag_items_item);
329+
encode_def_id(ebml_w, did);
330+
encode_kind(ebml_w, 'f' as u8);
331+
encode_type_params(ebml_w, tps);
332+
encode_type(ebml_w, trans.node_ann_type(cx, ann));
333+
encode_symbol(cx, ebml_w, did);
334+
ebml.end_tag(ebml_w);
335+
}
336+
case (ast.item_mod(_, _, _)) {
337+
// nothing to do
338+
}
339+
case (ast.item_native_mod(_, _, _)) {
340+
// nothing to do
341+
}
342+
case (ast.item_ty(?id, _, ?tps, ?did, ?ann)) {
343+
ebml.start_tag(ebml_w, tag_items_item);
344+
encode_def_id(ebml_w, did);
345+
encode_kind(ebml_w, 'y' as u8);
346+
encode_type_params(ebml_w, tps);
347+
encode_type(ebml_w, trans.node_ann_type(cx, ann));
348+
ebml.end_tag(ebml_w);
349+
}
350+
case (ast.item_tag(?id, ?variants, ?tps, ?did)) {
351+
ebml.start_tag(ebml_w, tag_items_item);
352+
encode_def_id(ebml_w, did);
353+
encode_kind(ebml_w, 't' as u8);
354+
encode_type_params(ebml_w, tps);
355+
ebml.end_tag(ebml_w);
356+
357+
encode_tag_variant_info(cx, ebml_w, did, variants);
358+
}
359+
case (ast.item_obj(?id, _, ?tps, ?did, ?ann)) {
360+
ebml.start_tag(ebml_w, tag_items_item);
361+
encode_def_id(ebml_w, did);
362+
encode_kind(ebml_w, 'o' as u8);
363+
encode_type_params(ebml_w, tps);
364+
encode_type(ebml_w, trans.node_ann_type(cx, ann));
365+
encode_symbol(cx, ebml_w, did);
366+
ebml.end_tag(ebml_w);
367+
}
368+
}
369+
}
370+
371+
fn encode_info_for_native_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
372+
@ast.native_item nitem) {
373+
ebml.start_tag(ebml_w, tag_items_item);
374+
alt (nitem.node) {
375+
case (ast.native_item_ty(_, ?did)) {
376+
encode_def_id(ebml_w, did);
377+
encode_kind(ebml_w, 'T' as u8);
378+
}
379+
case (ast.native_item_fn(_, _, _, ?tps, ?did, ?ann)) {
380+
encode_def_id(ebml_w, did);
381+
encode_kind(ebml_w, 'F' as u8);
382+
encode_type_params(ebml_w, tps);
383+
encode_type(ebml_w, trans.node_ann_type(cx, ann));
384+
}
385+
}
386+
ebml.end_tag(ebml_w);
387+
}
388+
389+
fn encode_info_for_items(@trans.crate_ctxt cx, &ebml.writer ebml_w) {
390+
ebml.start_tag(ebml_w, tag_items);
391+
for each (@tup(ast.def_id, @ast.item) kvp in cx.items.items()) {
392+
encode_info_for_item(cx, ebml_w, kvp._1);
393+
}
394+
for each (@tup(ast.def_id, @ast.native_item) kvp in
395+
cx.native_items.items()) {
396+
encode_info_for_native_item(cx, ebml_w, kvp._1);
397+
}
398+
ebml.end_tag(ebml_w);
399+
}
400+
401+
402+
fn encode_metadata(@trans.crate_ctxt cx, @ast.crate crate) -> ValueRef {
403+
auto string_w = io.string_writer();
404+
auto buf_w = string_w.get_writer().get_buf_writer();
405+
auto ebml_w = ebml.create_writer(buf_w);
406+
407+
encode_item_paths(ebml_w, crate);
408+
encode_info_for_items(cx, ebml_w);
409+
410+
ret C_postr(string_w.get_str());
134411
}
135412

136413
fn write_metadata(@trans.crate_ctxt cx, @ast.crate crate) {
137-
auto llmeta = collect_meta_directives(cx, crate);
414+
auto llmeta = encode_metadata(cx, crate);
138415

139416
auto llconst = trans.C_struct(vec(llmeta));
140417
auto llglobal = llvm.LLVMAddGlobal(cx.llmod, trans.val_ty(llconst),

trunk/src/comp/middle/trans.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,11 @@ state type crate_ctxt = rec(session.session sess,
7676
hashmap[ast.def_id, @ast.item] items,
7777
hashmap[ast.def_id,
7878
@ast.native_item] native_items,
79+
hashmap[ast.def_id, str] item_symbols,
7980
// TODO: hashmap[tup(tag_id,subtys), @tag_info]
8081
hashmap[@ty.t, uint] tag_sizes,
8182
hashmap[ast.def_id, ValueRef] discrims,
83+
hashmap[ast.def_id, str] discrim_symbols,
8284
hashmap[ast.def_id, ValueRef] fn_pairs,
8385
hashmap[ast.def_id, ValueRef] consts,
8486
hashmap[ast.def_id,()] obj_methods,
@@ -5332,6 +5334,7 @@ fn trans_vtbl(@crate_ctxt cx, TypeRef self_ty,
53325334
let str s = cx.names.next("_rust_method") + sep() + mcx.path;
53335335
let ValueRef llfn = decl_fastcall_fn(cx.llmod, s, llfnty);
53345336
cx.item_ids.insert(m.node.id, llfn);
5337+
cx.item_symbols.insert(m.node.id, s);
53355338

53365339
trans_fn(mcx, m.node.meth, m.node.id, some[TypeRef](self_ty),
53375340
ty_params, m.node.ann);
@@ -5663,6 +5666,7 @@ fn register_fn_pair(@crate_ctxt cx, str ps, TypeRef llpairty, ValueRef llfn,
56635666
as llvm.Linkage);
56645667

56655668
cx.item_ids.insert(id, llfn);
5669+
cx.item_symbols.insert(id, ps);
56665670
cx.fn_pairs.insert(id, gvar);
56675671
}
56685672

@@ -5884,8 +5888,9 @@ fn trans_constant(&@crate_ctxt cx, @ast.item it) -> @crate_ctxt {
58845888
auto discrim_val = C_int(i as int);
58855889

58865890
// FIXME: better name.
5891+
auto s = cx.names.next("_rust_tag_discrim");
58875892
auto discrim_gvar = llvm.LLVMAddGlobal(cx.llmod, T_int(),
5888-
_str.buf("tag_discrim"));
5893+
_str.buf(s));
58895894

58905895
// FIXME: Eventually we do want to export these, but we need
58915896
// to figure out what name they get first!
@@ -5895,6 +5900,7 @@ fn trans_constant(&@crate_ctxt cx, @ast.item it) -> @crate_ctxt {
58955900
as llvm.Linkage);
58965901

58975902
cx.discrims.insert(variant.id, discrim_gvar);
5903+
cx.discrim_symbols.insert(variant.id, s);
58985904

58995905
i += 1u;
59005906
}
@@ -5905,6 +5911,8 @@ fn trans_constant(&@crate_ctxt cx, @ast.item it) -> @crate_ctxt {
59055911
// with consts.
59065912
auto v = C_int(1);
59075913
cx.item_ids.insert(cid, v);
5914+
auto s = cx.names.next("_rust_const") + sep() + name;
5915+
cx.item_symbols.insert(cid, s);
59085916
}
59095917

59105918
case (_) {
@@ -6485,8 +6493,10 @@ fn trans_crate(session.session sess, @ast.crate crate, str output,
64856493
item_ids = new_def_hash[ValueRef](),
64866494
items = new_def_hash[@ast.item](),
64876495
native_items = new_def_hash[@ast.native_item](),
6496+
item_symbols = new_def_hash[str](),
64886497
tag_sizes = tag_sizes,
64896498
discrims = new_def_hash[ValueRef](),
6499+
discrim_symbols = new_def_hash[str](),
64906500
fn_pairs = new_def_hash[ValueRef](),
64916501
consts = new_def_hash[ValueRef](),
64926502
obj_methods = new_def_hash[()](),

0 commit comments

Comments
 (0)