Mercurial > projects > dang
changeset 23:dd18654b5131 new_gen
Trying to use Tomas Lindquist Olsen's LLVM bindings instead of raw text-output
You need the bindings from dsource.org/projects/llvmdc
Then you just dsss build and install them
author | Anders Halager <halager@gmail.com> |
---|---|
date | Sat, 19 Apr 2008 11:40:20 +0200 |
parents | 0fb2d13dce37 |
children | 2d28b21faad6 |
files | dsss.conf gen/LLVMGen.d |
diffstat | 2 files changed, 87 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/dsss.conf Fri Apr 18 21:39:17 2008 +0200 +++ b/dsss.conf Sat Apr 19 11:40:20 2008 +0200 @@ -1,9 +1,20 @@ [lexer] [parser] [ast] +[gen] + [dang/compiler.d] Target = Dang +buildflags = -llllvm-c-ext -llstdc++ \ + -llLLVMCore -llLLVMBitWriter -llLLVMBitReader -llLLVMAnalysis -llLLVMTarget \ + -llLLVMTransformUtils -llLLVMScalarOpts -llLLVMipa -llLLVMipo \ + -llLLVMInstrumentation -llLLVMSystem -llLLVMSupport [tests/run.d] Target = tests/run +buildflags = -llllvm-c-ext -llstdc++ \ + -llLLVMCore -llLLVMBitWriter -llLLVMBitReader -llLLVMAnalysis -llLLVMTarget \ + -llLLVMTransformUtils -llLLVMScalarOpts -llLLVMipa -llLLVMipo \ + -llLLVMInstrumentation -llLLVMSystem -llLLVMSupport +
--- a/gen/LLVMGen.d Fri Apr 18 21:39:17 2008 +0200 +++ b/gen/LLVMGen.d Sat Apr 19 11:40:20 2008 +0200 @@ -4,6 +4,8 @@ Int = tango.text.convert.Integer; import tango.core.Array : find; +import llvm.llvm; + import ast.Decl, ast.Stmt, ast.Exp; @@ -19,14 +21,14 @@ { typeToLLVM = [ - "int"[] : "i32"[], - "byte" : "i8", - "short" : "i16", - "long" : "i64", - "bool" : "i1", - "float" : "float", - "double" : "double", - "void" : "void" + "bool"[] : cast(Type) Type.Int1, + "byte" : Type.Int8, + "short" : Type.Int16, + "int" : Type.Int32, + "long" : Type.Int64, + "float" : Type.Float, + "double" : Type.Double, + "void" : Type.Void ]; alias BinaryExp.Operator op; opToLLVM = [ @@ -46,7 +48,12 @@ void gen(Decl[] decls) { - // Fill in scopes + // create module + m = new Module("main_module"); + scope(exit) m.dispose(); + + b = new Builder; + scope(exit) b.dispose(); table.enterScope; @@ -54,6 +61,12 @@ genRootDecl(decl); table.leaveScope; + + char[] err; + m.verify(err); + Stderr(err).newline; + + m.writeBitcodeToFile("test.bc"); } void genRootDecl(Decl decl) @@ -62,6 +75,26 @@ { case DeclType.FuncDecl: FuncDecl funcDecl = cast(FuncDecl)decl; + + Type[] param_types; + foreach (param; funcDecl.funcArgs) + param_types ~= typeToLLVM[param.type.get]; + auto ret_t = typeToLLVM[funcDecl.type.get]; + auto func_t = FunctionType.Get(ret_t, param_types); + auto llfunc = m.addFunction(func_t, funcDecl.identifier.get); + + foreach (i, v; funcDecl.funcArgs) + llfunc.getParam(i).name = v.identifier.get; + + auto bb = llfunc.appendBasicBlock("entry"); + b.positionAtEnd(bb); + + if (ret_t is Type.Void) + b.buildRetVoid(); + else + b.buildRet(ConstantInt.GetS(ret_t, 0)); + + /* auto return_type = typeToLLVM[funcDecl.type.token.get]; printBeginLine("define "); @@ -112,11 +145,12 @@ dedent; printBeginLine("}"); printEndLine(); - + */ break; case DeclType.VarDecl: auto varDecl = cast(VarDecl)decl; + /* printBeginLine("@"); genIdentifier(varDecl.identifier); @@ -132,8 +166,11 @@ printEndLine("i32 0"); printEndLine(); + */ + break; default: + break; } } @@ -143,6 +180,7 @@ { case DeclType.VarDecl: auto varDecl = cast(VarDecl)decl; + /* printBeginLine("%"); print(table.find(varDecl.identifier.get)); print(" = alloca "); @@ -154,6 +192,8 @@ assignExp.identifier.env = decl.env; genExpression(assignExp); } + */ + break; default: } @@ -197,7 +237,9 @@ unify(&left, &right); + auto res = Ref(left.type, table.find); + /* printBeginLine(res.name); print(" = "~opToLLVM[binaryExp.op]~" "); print(left); @@ -209,6 +251,7 @@ if (binaryExp.resultType) res.type = typeToLLVM[binaryExp.resultType]; + */ return res; case ExpType.IntegerLit: auto integetLit = cast(IntegerLit)exp; @@ -218,14 +261,17 @@ auto negateExp = cast(NegateExp)exp; auto target = genExpression(negateExp.exp); auto res = table.find; + /* printBeginLine(res); print(" = sub "~target.type~" 0, "); printEndLine(target.name); + */ return Ref(target.type, res); case ExpType.AssignExp: auto assignExp = cast(AssignExp)exp; auto sym = exp.env.find(assignExp.identifier); + /* Ref val = genExpression(assignExp.exp); Ref r = Ref(typeToLLVM[sym.type.get], val.name); @@ -245,10 +291,12 @@ print(", "); print(r.type ~ "* %"); printEndLine(assignExp.identifier.get); + */ break; case ExpType.CallExp: auto callExp = cast(CallExp)exp; auto func_sym = exp.env.find(cast(Identifier)callExp.exp); + /* auto func_type = typeToLLVM[func_sym.type.get]; Ref[] args; foreach(i, arg ; callExp.args) @@ -277,16 +325,19 @@ print(", "); } printEndLine(")"); - return Ref(func_sym.type.get, res); + */ + return Ref(func_sym.type.get, ""); case ExpType.Identifier: auto identifier = cast(Identifier)exp; auto sym = exp.env.find(identifier); char[] res = table.find; + /* printBeginLine(res); print(" = load "); print(typeToLLVM[sym.type.get]); print("* %"); printEndLine(sym.id.name); + */ return Ref(sym.type.get, res); } return Ref(); @@ -299,6 +350,7 @@ case StmtType.Return: auto ret = cast(ReturnStmt)stmt; auto sym = stmt.env.parentFunction(); + /* auto type = typeToLLVM[sym.type.get]; Ref res = genExpression(ret.exp); @@ -316,6 +368,7 @@ } printBeginLine("ret "); printEndLine(res); + */ break; case StmtType.Decl: auto declStmt = cast(DeclStmt)stmt; @@ -328,6 +381,7 @@ case StmtType.If: auto ifStmt = cast(IfStmt)stmt; Ref val = genExpression(ifStmt.cond); + /* auto cond = table.find("%.cond"); printBeginLine(cond); print(" = icmp ne "); @@ -369,11 +423,13 @@ printBeginLine(done_label); printEndLine(":"); + */ break; case StmtType.While: auto wStmt = cast(WhileStmt)stmt; + /* auto body_label = table.find("while_body"); auto cond_label = table.find("while_cond"); auto done_label = table.find("while_done"); @@ -416,6 +472,7 @@ printBeginLine(done_label); printEndLine(":"); + */ break; } } @@ -484,13 +541,17 @@ private: + // llvm stuff + Module m; + Builder b; + char[] tabIndex; const char[] tabType = " "; // 4 spaces FuncDecl[char[]] functions; SimpleSymbolTable table; SymbolTable symbolTable; - static char[][char[]] typeToLLVM; + static Type[char[]] typeToLLVM; static char[][BinaryExp.Operator] opToLLVM; static char[][] intTypes = [ "i1", "i8", "i16", "i32", "i64" ]; @@ -504,9 +565,9 @@ static Ref opCall(char[] type = "void", char[] name = "", bool atomic = false) { Ref r; - if(auto llvm_t = type in LLVMGen.typeToLLVM) - r.type = *llvm_t; - else + //if(auto llvm_t = type in LLVMGen.typeToLLVM) + // r.type = *llvm_t; + //else r.type = type; r.name = name; r.atomic = atomic;