diff options
| author | Graydon Hoare <[email protected]> | 2010-09-23 15:46:31 -0700 |
|---|---|---|
| committer | Graydon Hoare <[email protected]> | 2010-09-23 15:46:31 -0700 |
| commit | 46e46d0b49de8e245d091f7062dfc28ab71e869e (patch) | |
| tree | 5ca0d7ab10eb2a89b9c2a299ff3490eac912bf5d /src/comp/middle | |
| parent | More fleshing-out on rustc.me.trans. Emitting modules and fns corresponding t... (diff) | |
| download | rust-46e46d0b49de8e245d091f7062dfc28ab71e869e.tar.xz rust-46e46d0b49de8e245d091f7062dfc28ab71e869e.zip | |
Translate a bunch of the material (lltrans, llasm, abi) from rustboot to rustc, and move files around.
Diffstat (limited to 'src/comp/middle')
| -rw-r--r-- | src/comp/middle/trans.rs | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/comp/middle/trans.rs b/src/comp/middle/trans.rs new file mode 100644 index 00000000..1b1c5b2d --- /dev/null +++ b/src/comp/middle/trans.rs @@ -0,0 +1,122 @@ +import std._str; +import std._vec; +import std._str.rustrt.sbuf; +import std._vec.rustrt.vbuf; + +import front.ast; +import driver.session; +import back.x86; + +import lib.llvm.llvm; +import lib.llvm.builder; +import lib.llvm.llvm.ModuleRef; +import lib.llvm.llvm.ValueRef; +import lib.llvm.llvm.TypeRef; +import lib.llvm.llvm.BuilderRef; +import lib.llvm.llvm.BasicBlockRef; + +import lib.llvm.False; +import lib.llvm.True; + +type trans_ctxt = rec(session.session sess, + ModuleRef llmod, + str path); + +fn T_nil() -> TypeRef { + ret llvm.LLVMVoidType(); +} + +fn T_int() -> TypeRef { + ret llvm.LLVMInt32Type(); +} + +fn T_fn(vec[TypeRef] inputs, TypeRef output) -> TypeRef { + ret llvm.LLVMFunctionType(output, + _vec.buf[TypeRef](inputs), + _vec.len[TypeRef](inputs), + False()); +} + +type terminator = fn(&trans_ctxt cx, builder b); + +fn trans_log(&trans_ctxt cx, builder b, &ast.atom a) { +} + +fn trans_stmt(&trans_ctxt cx, builder b, &ast.stmt s, terminator t) { + alt (s) { + case (ast.stmt_log(?a)) { + trans_log(cx, b, *a); + } + case (_) { + cx.sess.unimpl("stmt variant"); + } + } +} + +fn default_terminate(&trans_ctxt cx, builder b) { + b.RetVoid(); +} + +fn trans_block(&trans_ctxt cx, ValueRef llfn, &ast.block b, terminator t) { + let BasicBlockRef llbb = + llvm.LLVMAppendBasicBlock(llfn, _str.buf("")); + let BuilderRef llbuild = llvm.LLVMCreateBuilder(); + llvm.LLVMPositionBuilderAtEnd(llbuild, llbb); + auto bld = builder(llbuild); + for (@ast.stmt s in b) { + trans_stmt(cx, bld, *s, t); + } + t(cx, bld); +} + +fn trans_fn(&trans_ctxt cx, &ast._fn f) { + let vec[TypeRef] args = vec(); + let TypeRef llty = T_fn(args, T_nil()); + let ValueRef llfn = + llvm.LLVMAddFunction(cx.llmod, _str.buf(cx.path), llty); + auto term = default_terminate; + trans_block(cx, llfn, f.body, term); +} + +fn trans_item(&trans_ctxt cx, &str name, &ast.item item) { + auto sub_cx = rec(path=cx.path + "." + name with cx); + alt (item) { + case (ast.item_fn(?f)) { + trans_fn(sub_cx, *f); + } + case (ast.item_mod(?m)) { + trans_mod(sub_cx, *m); + } + } +} + +fn trans_mod(&trans_ctxt cx, &ast._mod m) { + for each (tup(str, ast.item) pair in m.items()) { + trans_item(cx, pair._0, pair._1); + } +} + +fn trans_crate(session.session sess, ast.crate crate) { + auto llmod = + llvm.LLVMModuleCreateWithNameInContext(_str.buf("rust_out"), + llvm.LLVMGetGlobalContext()); + + llvm.LLVMSetModuleInlineAsm(llmod, _str.buf(x86.get_module_asm())); + + auto cx = rec(sess=sess, llmod=llmod, path=""); + trans_mod(cx, crate.module); + + llvm.LLVMWriteBitcodeToFile(llmod, _str.buf("rust_out.bc")); + llvm.LLVMDisposeModule(llmod); +} + +// +// Local Variables: +// mode: rust +// fill-column: 78; +// indent-tabs-mode: nil +// c-basic-offset: 4 +// buffer-file-coding-system: utf-8-unix +// compile-command: "make -k -C ../.. 2>&1 | sed -e 's/\\/x\\//x:\\//g'"; +// End: +// |