Flesh out rustc.me.trans to construct functions, basic blocks and builders off the AST.

This commit is contained in:
Graydon Hoare 2010-09-22 17:05:38 -07:00
parent 4d17283371
commit 04a55df54b
3 changed files with 63 additions and 3 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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);