1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
|
import std._str;
import std._vec;
import std.ebml;
import std.io;
import std.option;
import front.ast;
import middle.fold;
import middle.trans;
import middle.ty;
import back.x86;
import util.common;
import lib.llvm.llvm;
import lib.llvm.llvm.ValueRef;
import lib.llvm.False;
const uint tag_paths = 0x01u;
const uint tag_items = 0x02u;
const uint tag_paths_name = 0x03u;
const uint tag_paths_item = 0x04u;
const uint tag_paths_mod = 0x05u;
const uint tag_items_item = 0x06u;
const uint tag_items_def_id = 0x07u;
const uint tag_items_kind = 0x08u;
const uint tag_items_ty_param = 0x09u;
const uint tag_items_type = 0x0au;
const uint tag_items_symbol = 0x0bu;
const uint tag_items_variant = 0x0cu;
const uint tag_items_tag_id = 0x0du;
// Type encoding
// Compact string representation for ty.t values. API ty_str & parse_from_str.
// (The second has to be authed pure.) Extra parameters are for converting
// to/from def_ids in the string rep. Whatever format you choose should not
// contain pipe characters.
// Callback to translate defs to strs or back.
type def_str = fn(ast.def_id) -> str;
fn ty_str(@ty.t t, def_str ds) -> str {
ret sty_str(t.struct, ds);
}
fn mt_str(&ty.mt mt, def_str ds) -> str {
auto mut_str;
alt (mt.mut) {
case (ast.imm) { mut_str = ""; }
case (ast.mut) { mut_str = "m"; }
case (ast.maybe_mut) { mut_str = "?"; }
}
ret mut_str + ty_str(mt.ty, ds);
}
fn sty_str(ty.sty st, def_str ds) -> str {
alt (st) {
case (ty.ty_nil) {ret "n";}
case (ty.ty_bool) {ret "b";}
case (ty.ty_int) {ret "i";}
case (ty.ty_uint) {ret "u";}
case (ty.ty_float) {ret "l";}
case (ty.ty_machine(?mach)) {
alt (mach) {
case (common.ty_u8) {ret "Mb";}
case (common.ty_u16) {ret "Mw";}
case (common.ty_u32) {ret "Ml";}
case (common.ty_u64) {ret "Md";}
case (common.ty_i8) {ret "MB";}
case (common.ty_i16) {ret "MW";}
case (common.ty_i32) {ret "ML";}
case (common.ty_i64) {ret "MD";}
case (common.ty_f32) {ret "Mf";}
case (common.ty_f64) {ret "MF";}
}
}
case (ty.ty_char) {ret "c";}
case (ty.ty_str) {ret "s";}
case (ty.ty_tag(?def,?tys)) { // TODO restore def_id
auto acc = "t[" + ds(def) + "|";
for (@ty.t t in tys) {acc += ty_str(t, ds);}
ret acc + "]";
}
case (ty.ty_box(?mt)) {ret "@" + mt_str(mt, ds);}
case (ty.ty_vec(?mt)) {ret "V" + mt_str(mt, ds);}
case (ty.ty_port(?t)) {ret "P" + ty_str(t, ds);}
case (ty.ty_chan(?t)) {ret "C" + ty_str(t, ds);}
case (ty.ty_tup(?mts)) {
auto acc = "T[";
for (ty.mt mt in mts) {acc += mt_str(mt, ds);}
ret acc + "]";
}
case (ty.ty_rec(?fields)) {
auto acc = "R[";
for (ty.field field in fields) {
acc += field.ident + "=";
acc += mt_str(field.mt, ds);
}
ret acc + "]";
}
case (ty.ty_fn(?proto,?args,?out)) {
ret proto_str(proto) + ty_fn_str(args, out, ds);
}
case (ty.ty_native_fn(?abi,?args,?out)) {
auto abistr;
alt (abi) {
case (ast.native_abi_rust) {abistr = "r";}
case (ast.native_abi_cdecl) {abistr = "c";}
}
ret "N" + abistr + ty_fn_str(args, out, ds);
}
case (ty.ty_obj(?methods)) {
auto acc = "O[";
for (ty.method m in methods) {
acc += proto_str(m.proto);
acc += m.ident;
acc += ty_fn_str(m.inputs, m.output, ds);
}
ret acc + "]";
}
case (ty.ty_var(?id)) {ret "X" + common.istr(id);}
case (ty.ty_native) {ret "E";}
case (ty.ty_param(?def)) {ret "p" + ds(def);}
// TODO (maybe?) ty_type;
}
}
fn proto_str(ast.proto proto) -> str {
alt (proto) {
case (ast.proto_iter) {ret "W";}
case (ast.proto_fn) {ret "F";}
}
}
fn ty_fn_str(vec[ty.arg] args, @ty.t out, def_str ds) -> str {
auto acc = "[";
for (ty.arg arg in args) {
if (arg.mode == ast.alias) {acc += "&";}
acc += ty_str(arg.ty, ds);
}
ret acc + "]" + ty_str(out, ds);
}
// Returns a Plain Old LLVM String.
fn C_postr(str s) -> ValueRef {
ret llvm.LLVMConstString(_str.buf(s), _str.byte_len(s), False);
}
// Path table encoding
fn encode_name(&ebml.writer ebml_w, str name) {
ebml.start_tag(ebml_w, tag_paths_name);
ebml_w.writer.write(_str.bytes(name));
ebml.end_tag(ebml_w);
}
fn encode_def_id(&ebml.writer ebml_w, &ast.def_id id) {
ebml.start_tag(ebml_w, tag_items_def_id);
ebml_w.writer.write(_str.bytes(def_to_str(id)));
ebml.end_tag(ebml_w);
}
fn encode_tag_variant_paths(&ebml.writer ebml_w, vec[ast.variant] variants) {
for (ast.variant variant in variants) {
ebml.start_tag(ebml_w, tag_paths_item);
encode_name(ebml_w, variant.name);
encode_def_id(ebml_w, variant.id);
ebml.end_tag(ebml_w);
}
}
fn encode_native_module_item_paths(&ebml.writer ebml_w,
&ast.native_mod nmod) {
for (@ast.native_item nitem in nmod.items) {
alt (nitem.node) {
case (ast.native_item_ty(?id, ?did)) {
ebml.start_tag(ebml_w, tag_paths_item);
encode_name(ebml_w, id);
encode_def_id(ebml_w, did);
ebml.end_tag(ebml_w);
}
case (ast.native_item_fn(?id, _, _, _, ?did, _)) {
ebml.start_tag(ebml_w, tag_paths_item);
encode_name(ebml_w, id);
encode_def_id(ebml_w, did);
ebml.end_tag(ebml_w);
}
}
}
}
fn encode_module_item_paths(&ebml.writer ebml_w, &ast._mod module) {
// TODO: only encode exported items
for (@ast.item it in module.items) {
alt (it.node) {
case (ast.item_const(?id, _, ?tps, ?did, ?ann)) {
ebml.start_tag(ebml_w, tag_paths_item);
encode_name(ebml_w, id);
encode_def_id(ebml_w, did);
ebml.end_tag(ebml_w);
}
case (ast.item_fn(?id, _, ?tps, ?did, ?ann)) {
ebml.start_tag(ebml_w, tag_paths_item);
encode_name(ebml_w, id);
encode_def_id(ebml_w, did);
ebml.end_tag(ebml_w);
}
case (ast.item_mod(?id, ?_mod, _)) {
ebml.start_tag(ebml_w, tag_paths_mod);
encode_name(ebml_w, id);
encode_module_item_paths(ebml_w, _mod);
ebml.end_tag(ebml_w);
}
case (ast.item_native_mod(?id, ?nmod, _)) {
ebml.start_tag(ebml_w, tag_paths_mod);
encode_name(ebml_w, id);
encode_native_module_item_paths(ebml_w, nmod);
ebml.end_tag(ebml_w);
}
case (ast.item_ty(?id, _, ?tps, ?did, ?ann)) {
ebml.start_tag(ebml_w, tag_paths_item);
encode_name(ebml_w, id);
encode_def_id(ebml_w, did);
ebml.end_tag(ebml_w);
}
case (ast.item_tag(?id, ?variants, ?tps, ?did)) {
ebml.start_tag(ebml_w, tag_paths_item);
encode_name(ebml_w, id);
encode_tag_variant_paths(ebml_w, variants);
encode_def_id(ebml_w, did);
ebml.end_tag(ebml_w);
}
case (ast.item_obj(?id, _, ?tps, ?did, ?ann)) {
ebml.start_tag(ebml_w, tag_paths_item);
encode_name(ebml_w, id);
encode_def_id(ebml_w, did);
ebml.end_tag(ebml_w);
}
}
}
}
fn encode_item_paths(&ebml.writer ebml_w, @ast.crate crate) {
ebml.start_tag(ebml_w, tag_paths);
encode_module_item_paths(ebml_w, crate.node.module);
ebml.end_tag(ebml_w);
}
// Item info table encoding
fn encode_kind(&ebml.writer ebml_w, u8 c) {
ebml.start_tag(ebml_w, tag_items_kind);
ebml_w.writer.write(vec(c));
ebml.end_tag(ebml_w);
}
fn def_to_str(ast.def_id did) -> str {
ret #fmt("%d:%d", did._0, did._1);
}
// TODO: We need to encode the "crate numbers" somewhere for diamond imports.
fn encode_type_params(&ebml.writer ebml_w, vec[ast.ty_param] tps) {
for (ast.ty_param tp in tps) {
ebml.start_tag(ebml_w, tag_items_ty_param);
ebml_w.writer.write(_str.bytes(def_to_str(tp.id)));
ebml.end_tag(ebml_w);
}
}
fn encode_type(&ebml.writer ebml_w, @ty.t typ) {
ebml.start_tag(ebml_w, tag_items_type);
auto f = def_to_str;
ebml_w.writer.write(_str.bytes(ty_str(typ, f)));
ebml.end_tag(ebml_w);
}
fn encode_symbol(@trans.crate_ctxt cx, &ebml.writer ebml_w, ast.def_id did) {
ebml.start_tag(ebml_w, tag_items_symbol);
ebml_w.writer.write(_str.bytes(cx.item_symbols.get(did)));
ebml.end_tag(ebml_w);
}
fn encode_discriminant(@trans.crate_ctxt cx, &ebml.writer ebml_w,
ast.def_id did) {
ebml.start_tag(ebml_w, tag_items_symbol);
ebml_w.writer.write(_str.bytes(cx.discrim_symbols.get(did)));
ebml.end_tag(ebml_w);
}
fn encode_tag_id(&ebml.writer ebml_w, &ast.def_id id) {
ebml.start_tag(ebml_w, tag_items_tag_id);
ebml_w.writer.write(_str.bytes(def_to_str(id)));
ebml.end_tag(ebml_w);
}
fn encode_tag_variant_info(@trans.crate_ctxt cx, &ebml.writer ebml_w,
ast.def_id did, vec[ast.variant] variants) {
for (ast.variant variant in variants) {
ebml.start_tag(ebml_w, tag_items_variant);
encode_def_id(ebml_w, variant.id);
encode_tag_id(ebml_w, did);
encode_type(ebml_w, trans.node_ann_type(cx, variant.ann));
if (_vec.len[ast.variant_arg](variant.args) > 0u) {
encode_symbol(cx, ebml_w, variant.id);
}
encode_discriminant(cx, ebml_w, variant.id);
ebml.end_tag(ebml_w);
}
}
fn encode_info_for_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
@ast.item item) {
alt (item.node) {
case (ast.item_const(_, _, _, ?did, ?ann)) {
ebml.start_tag(ebml_w, tag_items_item);
encode_def_id(ebml_w, did);
encode_kind(ebml_w, 'c' as u8);
encode_type(ebml_w, trans.node_ann_type(cx, ann));
encode_symbol(cx, ebml_w, did);
ebml.end_tag(ebml_w);
}
case (ast.item_fn(_, _, ?tps, ?did, ?ann)) {
ebml.start_tag(ebml_w, tag_items_item);
encode_def_id(ebml_w, did);
encode_kind(ebml_w, 'f' as u8);
encode_type_params(ebml_w, tps);
encode_type(ebml_w, trans.node_ann_type(cx, ann));
encode_symbol(cx, ebml_w, did);
ebml.end_tag(ebml_w);
}
case (ast.item_mod(_, _, _)) {
// nothing to do
}
case (ast.item_native_mod(_, _, _)) {
// nothing to do
}
case (ast.item_ty(?id, _, ?tps, ?did, ?ann)) {
ebml.start_tag(ebml_w, tag_items_item);
encode_def_id(ebml_w, did);
encode_kind(ebml_w, 'y' as u8);
encode_type_params(ebml_w, tps);
encode_type(ebml_w, trans.node_ann_type(cx, ann));
ebml.end_tag(ebml_w);
}
case (ast.item_tag(?id, ?variants, ?tps, ?did)) {
ebml.start_tag(ebml_w, tag_items_item);
encode_def_id(ebml_w, did);
encode_kind(ebml_w, 't' as u8);
encode_type_params(ebml_w, tps);
ebml.end_tag(ebml_w);
encode_tag_variant_info(cx, ebml_w, did, variants);
}
case (ast.item_obj(?id, _, ?tps, ?did, ?ann)) {
ebml.start_tag(ebml_w, tag_items_item);
encode_def_id(ebml_w, did);
encode_kind(ebml_w, 'o' as u8);
encode_type_params(ebml_w, tps);
encode_type(ebml_w, trans.node_ann_type(cx, ann));
encode_symbol(cx, ebml_w, did);
ebml.end_tag(ebml_w);
}
}
}
fn encode_info_for_native_item(@trans.crate_ctxt cx, &ebml.writer ebml_w,
@ast.native_item nitem) {
ebml.start_tag(ebml_w, tag_items_item);
alt (nitem.node) {
case (ast.native_item_ty(_, ?did)) {
encode_def_id(ebml_w, did);
encode_kind(ebml_w, 'T' as u8);
}
case (ast.native_item_fn(_, _, _, ?tps, ?did, ?ann)) {
encode_def_id(ebml_w, did);
encode_kind(ebml_w, 'F' as u8);
encode_type_params(ebml_w, tps);
encode_type(ebml_w, trans.node_ann_type(cx, ann));
}
}
ebml.end_tag(ebml_w);
}
fn encode_info_for_items(@trans.crate_ctxt cx, &ebml.writer ebml_w) {
ebml.start_tag(ebml_w, tag_items);
for each (@tup(ast.def_id, @ast.item) kvp in cx.items.items()) {
encode_info_for_item(cx, ebml_w, kvp._1);
}
for each (@tup(ast.def_id, @ast.native_item) kvp in
cx.native_items.items()) {
encode_info_for_native_item(cx, ebml_w, kvp._1);
}
ebml.end_tag(ebml_w);
}
fn encode_metadata(@trans.crate_ctxt cx, @ast.crate crate) -> ValueRef {
auto string_w = io.string_writer();
auto buf_w = string_w.get_writer().get_buf_writer();
auto ebml_w = ebml.create_writer(buf_w);
encode_item_paths(ebml_w, crate);
encode_info_for_items(cx, ebml_w);
ret C_postr(string_w.get_str());
}
fn write_metadata(@trans.crate_ctxt cx, @ast.crate crate) {
auto llmeta = encode_metadata(cx, crate);
auto llconst = trans.C_struct(vec(llmeta));
auto llglobal = llvm.LLVMAddGlobal(cx.llmod, trans.val_ty(llconst),
_str.buf("rust_metadata"));
llvm.LLVMSetInitializer(llglobal, llconst);
llvm.LLVMSetSection(llglobal, _str.buf(x86.get_meta_sect_name()));
}
|