From 04a55df54bb389abf95c0639bd6246f06cd6c38f Mon Sep 17 00:00:00 2001 From: Graydon Hoare Date: Wed, 22 Sep 2010 17:05:38 -0700 Subject: [PATCH] Flesh out rustc.me.trans to construct functions, basic blocks and builders off the AST. --- src/comp/driver/rustc.rs | 2 +- src/comp/lib/llvm.rs | 4 +++ src/comp/me/trans.rs | 60 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/src/comp/driver/rustc.rs b/src/comp/driver/rustc.rs index 758e9145c2a..c59d5b566e4 100644 --- a/src/comp/driver/rustc.rs +++ b/src/comp/driver/rustc.rs @@ -18,7 +18,7 @@ fn main(vec[str] args) { auto p = parser.new_parser(sess, filename); log "opened file: " + filename; auto crate = parser.parse_crate(p); - trans.translate_crate(sess, crate); + trans.trans_crate(sess, crate); } i += 1; } diff --git a/src/comp/lib/llvm.rs b/src/comp/lib/llvm.rs index 9a31a1934b9..f0c4428c513 100644 --- a/src/comp/lib/llvm.rs +++ b/src/comp/lib/llvm.rs @@ -25,6 +25,10 @@ type LongLong = i64; type Long = i32; type Bool = int; +fn True() -> Bool { ret 1; } +fn False() -> Bool { ret 0; } + + native mod llvm = llvm_lib { type ModuleRef; diff --git a/src/comp/me/trans.rs b/src/comp/me/trans.rs index d8555a95c52..d8e587c5b8f 100644 --- a/src/comp/me/trans.rs +++ b/src/comp/me/trans.rs @@ -1,18 +1,74 @@ import std._str; +import std._vec; +import std._str.rustrt.sbuf; +import std._vec.rustrt.vbuf; import fe.ast; import driver.session; 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; -fn translate_crate(session.session sess, ast.crate crate) { +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()); +} + +fn trans_fn(ModuleRef llmod, str name, &ast._fn f) { + let vec[TypeRef] args = vec(); + let TypeRef llty = T_fn(args, T_nil()); + let ValueRef llfn = + llvm.LLVMAddFunction(llmod, _str.buf(name), llty); +} + +fn trans_block(ast.block b, ValueRef llfn) { + let BasicBlockRef llbb = + llvm.LLVMAppendBasicBlock(llfn, 0 as sbuf); + let BuilderRef llbuild = llvm.LLVMCreateBuilder(); + llvm.LLVMPositionBuilderAtEnd(llbuild, llbb); + auto b = builder(llbuild); +} + +fn trans_mod_item(ModuleRef llmod, str name, &ast.item item) { + alt (item) { + case (ast.item_fn(?f)) { + trans_fn(llmod, name, *f); + } + case (ast.item_mod(?m)) { + trans_mod(llmod, name, *m); + } + } +} + +fn trans_mod(ModuleRef llmod, str name, &ast._mod m) { + for each (tup(str, ast.item) pair in m.items()) { + trans_mod_item(llmod, name + "." + pair._0, pair._1); + } +} + +fn trans_crate(session.session sess, ast.crate crate) { auto llmod = llvm.LLVMModuleCreateWithNameInContext(_str.buf("rust_out"), llvm.LLVMGetGlobalContext()); - auto b = builder(llvm.LLVMCreateBuilder()); llvm.LLVMWriteBitcodeToFile(llmod, _str.buf("rust_out.bc")); llvm.LLVMDisposeModule(llmod);