# HG changeset patch # User Anders Halager # Date 1208478667 -7200 # Node ID ae5bbe4e7fd66043d360a2f9c52deadf985bd55a # Parent 2168f4cb73f1be167839890f60a18c74e1ad494f Lots of stuff, here are the git comments: Lots of changes. Now supports various integer-types in signed mode (byte, short, int, long) Will cast back and forth when necessary Doesnt check types and return type of functions is still hardcoded Function return-types. * Returned expressions are converted to the correct type for the function * Callers get the correct type back * Void functions work, and get an automatic "ret void" in the end diff -r 2168f4cb73f1 -r ae5bbe4e7fd6 dsss.conf --- a/dsss.conf Fri Apr 18 02:01:38 2008 +0200 +++ b/dsss.conf Fri Apr 18 02:31:07 2008 +0200 @@ -4,6 +4,3 @@ [dang/compiler.d] Target = Dang - -[tests/run.d] -Target = tests/run diff -r 2168f4cb73f1 -r ae5bbe4e7fd6 gen/LLVMGen.d --- a/gen/LLVMGen.d Fri Apr 18 02:01:38 2008 +0200 +++ b/gen/LLVMGen.d Fri Apr 18 02:31:07 2008 +0200 @@ -53,9 +53,10 @@ { case DeclType.FuncDecl: FuncDecl funcDecl = cast(FuncDecl)decl; + auto return_type = typeToLLVM[funcDecl.type.token.get]; printBeginLine("define "); - print(typeToLLVM[funcDecl.type.token.get]); + print(return_type); print(" @"); genIdentifier(funcDecl.identifier); print("("); @@ -79,7 +80,7 @@ foreach(i, arg ; args) { - auto sym = funcDecl.env.find(arg); + auto sym = arg.env.find(arg); auto type = typeToLLVM[sym.type.get]; printBeginLine("%"~arg.get); printEndLine(" = alloca " ~ type); @@ -91,9 +92,12 @@ printEndLine(); - foreach(stmt ; funcDecl.statements) + foreach (stmt; funcDecl.statements) + genStmt(stmt); + if (return_type == "void") { - genStmt(stmt); + printBeginLine("ret void"); + printEndLine(); } table.leaveScope; dedent; @@ -235,9 +239,16 @@ foreach(i, arg ; callExp.args) args ~= genExpression(arg); - char[] res = table.find; - printBeginLine(res); - print(" = call "); + char[] res = ""; + if (func_type != "void") + { + res = table.find; + printBeginLine(res); + print(" = call "); + } + else + printBeginLine("call "); + print(func_type); print(" @"); @@ -272,7 +283,22 @@ { case StmtType.Return: auto ret = cast(ReturnStmt)stmt; + auto sym = stmt.env.parentFunction(); + auto type = typeToLLVM[sym.type.get]; + Ref res = genExpression(ret.exp); + + if (type != res.type) + { + auto cast_res = table.find("%.cast"); + printBeginLine(cast_res); + printCastFromTo(res.type, type); + print(res); + print(" to "); + printEndLine(type); + res.name = cast_res; + res.type = type; + } printBeginLine("ret "); printEndLine(res); break; diff -r 2168f4cb73f1 -r ae5bbe4e7fd6 lexer/Lexer.d --- a/lexer/Lexer.d Fri Apr 18 02:01:38 2008 +0200 +++ b/lexer/Lexer.d Fri Apr 18 02:31:07 2008 +0200 @@ -103,7 +103,6 @@ return Token(Tok.EOF, Location(position, this.source), 0); case '*': - position += 2; while(getNextChar != CharType.EOF) { ++position; @@ -114,27 +113,12 @@ return Token(Tok.EOF, Location(position, this.source), 0); case '+': - position += 2; - int nesting = 1; while(getNextChar != CharType.EOF) { ++position; if(source.data[position-2] == '+') if(source.data[position-1] == '/') - { - position++; - nesting--; - } - - if(source.data[position-2] == '/') - if(source.data[position-1] == '+') - { - nesting++; - position++; - } - - if(nesting == 0) - return this.next; + return this.next; } return Token(Tok.EOF, Location(position, this.source), 0); diff -r 2168f4cb73f1 -r ae5bbe4e7fd6 sema/SymbolTable.d --- a/sema/SymbolTable.d Fri Apr 18 02:01:38 2008 +0200 +++ b/sema/SymbolTable.d Fri Apr 18 02:31:07 2008 +0200 @@ -45,8 +45,23 @@ return res; } + Symbol parentFunction() + { + if (func !is null) + return func; + else if (enclosing !is null) + return enclosing.parentFunction(); + else + return null; + } + Symbol parentFunction(Symbol f) + { + func = f; + return f; + } private: Symbol[Identifier] symbols; + Symbol func; } class Symbol diff -r 2168f4cb73f1 -r ae5bbe4e7fd6 sema/SymbolTableBuilder.d --- a/sema/SymbolTableBuilder.d Fri Apr 18 02:01:38 2008 +0200 +++ b/sema/SymbolTableBuilder.d Fri Apr 18 02:31:07 2008 +0200 @@ -41,9 +41,15 @@ { auto sym = current().add(d.identifier); sym.type = d.type; - push(); + visitExp(d.type); + visitExp(d.identifier); d.env = current(); - super.visitFuncDecl(d); + push(); + current().parentFunction = sym; + foreach (arg; d.funcArgs) + visitDecl(arg); + foreach (stmt; d.statements) + visitStmt(stmt); pop(); } diff -r 2168f4cb73f1 -r ae5bbe4e7fd6 test.td --- a/test.td Fri Apr 18 02:01:38 2008 +0200 +++ b/test.td Fri Apr 18 02:31:07 2008 +0200 @@ -1,17 +1,17 @@ int x = 4; -byte main() +int main() { long var1 = 1; short var2 = 2; - return nice(var1, var2); + nice(var1, var2); + return var2; } int nice(long s, short t) { byte x = 5 + t; - // int f(float z) { return -1 * z; } t = 5 + 1 * 5 * s + t; return 2 * (t + -1) - x; }