about summary refs log tree commit diff
diff options
context:
space:
mode:
authorPatrick Walton <pcwalton@mimiga.net>2011-03-23 15:43:18 -0700
committerPatrick Walton <pcwalton@mimiga.net>2011-03-23 15:44:17 -0700
commitc669f2023109b400aa4a7297cb151ebfb71ae44e (patch)
tree3606f0720f12c35f68c9957c13a3b78c6dd6ef5e
parent0a42d5ae62771cbcdc4589ea9a019d8dbecb0ae7 (diff)
downloadrust-c669f2023109b400aa4a7297cb151ebfb71ae44e.tar.gz
rust-c669f2023109b400aa4a7297cb151ebfb71ae44e.zip
rustc: Write out crate metadata
-rw-r--r--src/comp/middle/metadata.rs287
-rw-r--r--src/comp/middle/trans.rs12
2 files changed, 293 insertions, 6 deletions
diff --git a/src/comp/middle/metadata.rs b/src/comp/middle/metadata.rs
index 06b527de89e..ce33658f44c 100644
--- a/src/comp/middle/metadata.rs
+++ b/src/comp/middle/metadata.rs
@@ -1,8 +1,11 @@
 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;
@@ -12,6 +15,22 @@ 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.
@@ -102,7 +121,8 @@ fn sty_str(ty.sty st, def_str ds) -> str {
         }
         case (ty.ty_var(?id)) {ret "X" + common.istr(id);}
         case (ty.ty_native) {ret "E";}
-        // TODO (maybe?)   ty_param(ast.def_id), ty_type;
+        case (ty.ty_param(?def)) {ret "p" + ds(def);}
+        // TODO (maybe?)   ty_type;
     }
 }
 
@@ -128,13 +148,270 @@ fn C_postr(str s) -> ValueRef {
     ret llvm.LLVMConstString(_str.buf(s), _str.byte_len(s), False);
 }
 
-fn collect_meta_directives(@trans.crate_ctxt cx, @ast.crate crate)
-        -> ValueRef {
-    ret C_postr("Hello world!");    // TODO
+
+// 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 = collect_meta_directives(cx, crate);
+    auto llmeta = encode_metadata(cx, crate);
 
     auto llconst = trans.C_struct(vec(llmeta));
     auto llglobal = llvm.LLVMAddGlobal(cx.llmod, trans.val_ty(llconst),
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs
index b7fb0687d1d..945394239f1 100644
--- a/src/comp/middle/trans.rs
+++ b/src/comp/middle/trans.rs
@@ -76,9 +76,11 @@ state type crate_ctxt = rec(session.session sess,
                             hashmap[ast.def_id, @ast.item] items,
                             hashmap[ast.def_id,
                                     @ast.native_item] native_items,
+                            hashmap[ast.def_id, str] item_symbols,
                             // TODO: hashmap[tup(tag_id,subtys), @tag_info]
                             hashmap[@ty.t, uint] tag_sizes,
                             hashmap[ast.def_id, ValueRef] discrims,
+                            hashmap[ast.def_id, str] discrim_symbols,
                             hashmap[ast.def_id, ValueRef] fn_pairs,
                             hashmap[ast.def_id, ValueRef] consts,
                             hashmap[ast.def_id,()] obj_methods,
@@ -5332,6 +5334,7 @@ fn trans_vtbl(@crate_ctxt cx, TypeRef self_ty,
         let str s = cx.names.next("_rust_method") + sep() + mcx.path;
         let ValueRef llfn = decl_fastcall_fn(cx.llmod, s, llfnty);
         cx.item_ids.insert(m.node.id, llfn);
+        cx.item_symbols.insert(m.node.id, s);
 
         trans_fn(mcx, m.node.meth, m.node.id, some[TypeRef](self_ty),
                  ty_params, m.node.ann);
@@ -5663,6 +5666,7 @@ fn register_fn_pair(@crate_ctxt cx, str ps, TypeRef llpairty, ValueRef llfn,
                         as llvm.Linkage);
 
     cx.item_ids.insert(id, llfn);
+    cx.item_symbols.insert(id, ps);
     cx.fn_pairs.insert(id, gvar);
 }
 
@@ -5884,8 +5888,9 @@ fn trans_constant(&@crate_ctxt cx, @ast.item it) -> @crate_ctxt {
                 auto discrim_val = C_int(i as int);
 
                 // FIXME: better name.
+                auto s = cx.names.next("_rust_tag_discrim");
                 auto discrim_gvar = llvm.LLVMAddGlobal(cx.llmod, T_int(),
-                    _str.buf("tag_discrim"));
+                                                       _str.buf(s));
 
                 // FIXME: Eventually we do want to export these, but we need
                 // to figure out what name they get first!
@@ -5895,6 +5900,7 @@ fn trans_constant(&@crate_ctxt cx, @ast.item it) -> @crate_ctxt {
                                     as llvm.Linkage);
 
                 cx.discrims.insert(variant.id, discrim_gvar);
+                cx.discrim_symbols.insert(variant.id, s);
 
                 i += 1u;
             }
@@ -5905,6 +5911,8 @@ fn trans_constant(&@crate_ctxt cx, @ast.item it) -> @crate_ctxt {
             // with consts.
             auto v = C_int(1);
             cx.item_ids.insert(cid, v);
+            auto s = cx.names.next("_rust_const") + sep() + name;
+            cx.item_symbols.insert(cid, s);
         }
 
         case (_) {
@@ -6485,8 +6493,10 @@ fn trans_crate(session.session sess, @ast.crate crate, str output,
                    item_ids = new_def_hash[ValueRef](),
                    items = new_def_hash[@ast.item](),
                    native_items = new_def_hash[@ast.native_item](),
+                   item_symbols = new_def_hash[str](),
                    tag_sizes = tag_sizes,
                    discrims = new_def_hash[ValueRef](),
+                   discrim_symbols = new_def_hash[str](),
                    fn_pairs = new_def_hash[ValueRef](),
                    consts = new_def_hash[ValueRef](),
                    obj_methods = new_def_hash[()](),