changeset 56:4ae365eff712 new_gen

Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
author Anders Johnsen <skabet@gmail.com>
date Mon, 28 Apr 2008 21:40:00 +0200
parents 79cb0afafabe
children 43bb0a36b869
files ast/Decl.d ast/Exp.d ast/Stmt.d dang/compiler.d gen/CodeGen.d misc/Error.d sema/SymbolTable.d sema/SymbolTableBuilder.d
diffstat 8 files changed, 259 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Decl.d	Sat Apr 26 23:11:13 2008 +0200
+++ b/ast/Decl.d	Mon Apr 28 21:40:00 2008 +0200
@@ -5,6 +5,8 @@
 
 import lexer.Token;
 
+import tango.io.Stdout;
+
 import sema.SymbolTable;
 
 enum DeclType
@@ -21,6 +23,10 @@
         this.declType = declType;
     }
 
+    void simplify()
+    {
+    }
+
     DeclType declType;
     Scope env;
 }
@@ -36,6 +42,10 @@
         this.init = e;
     }
 
+    void simplify()
+    {
+    }
+
     Identifier type, identifier;
     Exp init;
 }
@@ -59,9 +69,34 @@
         statements = stmts.statements;
     }
 
+    void simplify()
+    {
+        if(auto t = cast(DStruct)env.find(identifier).type)
+        {
+            VarDecl[] funcArgs;
+            auto i = new Identifier("ret.val");
+            i.env = env;
+            i.env.add(i);
+            i.env.find(i).type = i.env.find(identifier).type;
+            auto var = new VarDecl(type, i);
+            var.env = env;
+            funcArgs ~= var;
+            funcArgs ~= this.funcArgs;
+            this.funcArgs = funcArgs;
+            env.find(this.identifier).type = DType.Void;
+            sret = true;
+        }
+
+        foreach ( funcArg ; funcArgs )
+            funcArg.simplify();
+        foreach ( stmt ; statements )
+            stmt.simplify();
+    }
+
     Identifier type, identifier;
     VarDecl[] funcArgs;
     Stmt[] statements;
+    bool sret = false;
 }
 
 class StructDecl : Decl
@@ -78,6 +113,10 @@
         vars ~= new VarDecl(type, name, exp);
     }
 
+    void simplify()
+    {
+    }
+
     Identifier identifier;
     VarDecl[] vars;
 }
--- a/ast/Exp.d	Sat Apr 26 23:11:13 2008 +0200
+++ b/ast/Exp.d	Mon Apr 28 21:40:00 2008 +0200
@@ -1,6 +1,10 @@
 module ast.Exp;
 
 import tango.text.Util : jhash;
+import tango.io.Stdout;
+
+import ast.Decl,
+       ast.Stmt;
 
 import lexer.Token;
 
@@ -27,6 +31,12 @@
 
     ExpType expType;
     Scope env;
+    int stmtIndex;
+
+    Exp simplify()
+    {
+        return this;
+    }
 }
 
 class CallExp : Exp
@@ -40,6 +50,43 @@
 
     Exp exp;
     Exp[] args;
+    bool sret = false;
+
+
+    Exp simplify()
+    {
+        auto call = cast(Identifier)exp;
+        FuncDecl f = env.parentFunction;
+        auto i = new Identifier("temp.var");
+        i.env = f.env;
+        f.env.add(i);
+        f.env.find(i).type = f.env.find(call).type;
+        auto var = new VarDecl(f.type, i, null);
+        Exp[] args; 
+        args ~= i;
+        args ~= this.args;
+        auto callExp = new CallExp(exp, args);
+        callExp.env = f.env;
+//        auto ass = new AssignExp(i, callExp);
+        var.env = f.env;
+//        ass.env = f.env;
+        auto stmtVar = new DeclStmt(var);
+        auto stmtCall = new ExpStmt(callExp);
+        Stmt[] stmts;
+        foreach( index, s ; f.statements)
+        {
+            if(stmtIndex == index)
+            {
+                stmts ~= stmtVar;
+                stmts ~= stmtCall;
+            }
+            stmts ~= s;
+        }
+        f.statements = stmts;
+        callExp.sret = true;
+
+        return i;
+    }
 }
 
 class AssignExp : Exp
@@ -50,6 +97,13 @@
         this.identifier = identifier;
         this.exp = exp;
     }
+    Exp simplify()
+    {
+        identifier = identifier.simplify;
+        exp = exp.simplify;
+
+        return this;
+    }
 
     Exp identifier;
     Exp exp;
@@ -84,6 +138,12 @@
             return "bool";
         return null;
     }
+    Exp simplify()
+    {
+        left = left.simplify;
+        right = right.simplify;
+        return this;
+    }
 
     Operator op;
     Exp left, right;
@@ -96,6 +156,11 @@
         super(ExpType.Negate);
         this.exp = exp;
     }
+    Exp simplify()
+    {
+        exp = exp.simplify;
+        return this;
+    }
 
     public Exp exp;
 }
@@ -107,6 +172,10 @@
         super(ExpType.IntegerLit);
         this.token = t;
     }
+    Exp simplify()
+    {
+        return this;
+    }
 
     Token token;
 }
@@ -119,6 +188,12 @@
         this.target = target;
         this.child = child;
     }
+    Exp simplify()
+    {
+        child.simplify;
+        target = target.simplify;
+        return this;
+    }
 
     Identifier child;
     Exp target;
@@ -133,6 +208,13 @@
         this.pos = pos;
     }
 
+    Exp simplify()
+    {
+        target = target.simplify;
+        pos.simplify;
+        return this;
+    }
+
     Exp target;
     IntegerLit pos;
 }
@@ -146,6 +228,12 @@
         name = t.get;
     }
 
+    this(char[] name)
+    {
+        super(ExpType.Identifier);
+        this.name = name;
+    }
+
     char[] get()
     {
         return name;
@@ -170,6 +258,11 @@
         return 0;
     }
 
+    Exp simplify()
+    {
+        return this;
+    }
+
     Token token;
     char[] name;
 }
--- a/ast/Stmt.d	Sat Apr 26 23:11:13 2008 +0200
+++ b/ast/Stmt.d	Mon Apr 28 21:40:00 2008 +0200
@@ -1,7 +1,8 @@
 module ast.Stmt;
 
 import Array = tango.core.Array,
-       Integer = tango.text.convert.Integer;
+       Integer = tango.text.convert.Integer,
+       tango.io.Stdout;
 
 import ast.Exp,
        ast.Decl;
@@ -28,8 +29,13 @@
         this.stmtType = stmtType;
     }
 
+    void simplify()
+    {
+    }
+
     StmtType stmtType;
     Scope env;
+    int stmtIndex;
 }
 
 class CompoundStatement : Stmt
@@ -40,6 +46,12 @@
         this.statements = stmts;
     }
 
+    void simplify()
+    {
+        foreach ( stmt ; statements )
+            stmt.simplify;
+    }
+
     Stmt[] statements;
 }
 
@@ -50,6 +62,35 @@
         super(StmtType.Return);
     }
 
+    void simplify()
+    {
+        FuncDecl f = env.parentFunction;
+        if(f.sret)
+        {
+            auto i = new Identifier("ret.val");
+            i.env = f.env;
+            auto ass = new AssignExp(i, exp);
+            ass.env = f.env;
+            auto assStmt = new ExpStmt(ass);
+            assStmt.env = f.env;
+
+            Stmt[] stmts;
+            Stdout(stmtIndex).newline;
+            foreach(index, stmt ; f.statements)
+            {
+                if(stmtIndex == index)
+                    stmts ~= assStmt;
+
+                stmts ~= stmt;
+            }
+            f.statements = stmts;
+            
+            exp = null;
+        }
+        if(exp)
+           exp.simplify;
+    }
+
     public Exp exp;
 }
 
@@ -61,6 +102,11 @@
         this.decl = decl;
     }
 
+    void simplify()
+    {
+        decl.simplify;
+    }
+
     public Decl decl;
 }
 
@@ -72,6 +118,11 @@
         this.exp = exp;
     }
 
+    void simplify()
+    {
+        exp = exp.simplify;
+    }
+
     public Exp exp;
 }
 
@@ -85,6 +136,13 @@
         this.else_body = el;
     }
 
+    void simplify()
+    {
+        cond.simplify;
+        then_body.simplify;
+        else_body.simplify;
+    }
+
     Exp cond;
     Stmt then_body;
     Stmt else_body;
@@ -99,6 +157,12 @@
         this.whileBody = stmts;
     }
 
+    void simplify()
+    {
+        cond.simplify;
+        whileBody.simplify;
+    }
+
     Exp cond;
     Stmt whileBody;
 }
@@ -156,6 +220,16 @@
             cases[$ - 1].followedByDefault = true;
     }
 
+    void simplify()
+    {
+        cond.simplify;
+        foreach ( stmt ; defaultBlock )
+            stmt.simplify;
+        foreach ( c ; cases )
+            foreach ( stmt ; c.stmts )
+                stmt.simplify;
+    }
+
     Exp cond;
     Case[] cases;
     Stmt[] defaultBlock;
--- a/dang/compiler.d	Sat Apr 26 23:11:13 2008 +0200
+++ b/dang/compiler.d	Mon Apr 28 21:40:00 2008 +0200
@@ -169,6 +169,9 @@
         (new SymbolTableBuilder).visit(decls);
         (new Declarations).visit(decls);
 
+        foreach(decl ; decls)
+            decl.simplify();
+
         postParse(decls, src);
     }
 
--- a/gen/CodeGen.d	Sat Apr 26 23:11:13 2008 +0200
+++ b/gen/CodeGen.d	Mon Apr 28 21:40:00 2008 +0200
@@ -71,7 +71,7 @@
             (FuncDecl fd)
             {
                 Type[] param_types;
-                foreach (p; fd.funcArgs)
+                foreach (i, p; fd.funcArgs)
                 {
                     DType t = p.env.find(p.identifier).type;
                     if(auto st = cast(DStruct)t)
@@ -89,7 +89,20 @@
                 }
                 auto func_t = FunctionType.Get(ret_t, param_types);
                 auto llfunc = m.addFunction(func_t, fd.identifier.get);
-                Stdout("uhh").newline;
+
+                foreach (i, p; fd.funcArgs)
+                {
+                    if(i == 0 && fd.sret)
+                        llfunc.addParamAttr(0, ParamAttr.StructRet);
+
+                    DType t = p.env.find(p.identifier).type;
+                    if(auto st = cast(DStruct)t)
+                    {
+                        if(i == 0 && fd.sret)
+                            continue;
+                        llfunc.addParamAttr(i,ParamAttr.ByVal);
+                    }
+                }
             };
         auto visitor = new VisitFuncDecls(registerFunc);
         visitor.visit(decls);
@@ -131,6 +144,7 @@
                 b.positionAtEnd(bb);
 
                 table.enterScope;
+                    Stdout(funcDecl.funcArgs.length).newline;
                 foreach (i, v; funcDecl.funcArgs)
                 {
                     llfunc.getParam(i).name = v.identifier.get;
@@ -279,6 +293,8 @@
 
                 }
                 // BUG: doesn't do implicit type-conversion
+                if(callExp.sret)
+                    return b.buildCall(m.getNamedFunction(func_sym.id.get), args, "");
                 return b.buildCall(m.getNamedFunction(func_sym.id.get), args, ".call");
             case ExpType.Identifier:
                 auto identifier = cast(Identifier)exp;
@@ -289,8 +305,8 @@
                     return b.buildLoad(table.find(sym.id.get), sym.id.get);
             case ExpType.MemberReference:
                 auto v = getPointer(exp);
-                return v;
-//                return b.buildLoad(v, v.name);
+//                return v;
+                return b.buildLoad(v, v.name);
         }
         assert(0, "Reached end of switch in genExpression");
         return null;
@@ -308,7 +324,7 @@
             case StmtType.Return:
                 auto ret = cast(ReturnStmt)stmt;
                 auto sym = stmt.env.parentFunction();
-                Type t = llvm(sym.type);
+                Type t = llvm(sym.env.find(sym.identifier).type);
                 if (ret.exp is null)
                     if (t is Type.Void)
                     {
@@ -351,7 +367,7 @@
                     Value False = ConstantInt.GetS(cond.type, 0);
                     cond = b.buildICmp(IntPredicate.NE, cond, False, ".cond");
                 }
-                auto func_name = stmt.env.parentFunction().id.get;
+                auto func_name = stmt.env.parentFunction().identifier.get;
                 Function func = m.getNamedFunction(func_name);
                 bool has_else = (ifStmt.else_body !is null);
 
@@ -379,7 +395,7 @@
                 break;
             case StmtType.While:
                 auto wStmt = cast(WhileStmt)stmt;
-                auto func_name = stmt.env.parentFunction().id.get;
+                auto func_name = stmt.env.parentFunction().identifier.get;
                 Function func = m.getNamedFunction(func_name);
 
                 auto condBB = func.appendBasicBlock("cond");
@@ -407,7 +423,7 @@
                 auto sw = cast(SwitchStmt)stmt;
                 Value cond = genExpression(sw.cond);
 
-                auto func_name = stmt.env.parentFunction().id.get;
+                auto func_name = stmt.env.parentFunction().identifier.get;
                 Function func = m.getNamedFunction(func_name);
 
                 BasicBlock oldBB = b.getInsertBlock();
@@ -520,8 +536,8 @@
                 Value from = b.buildBitCast(v, BytePtr, ".copy_from");
                 // bitcast "to" to i8*
                 Value to = b.buildBitCast(t, BytePtr, ".copy_to");
-                // call llvm.memcpy.i32( "from", "to", type_size, alignment (32 in clang) );
-                b.buildCall(llvm_memcpy, [from, to, ConstantInt.GetS(Type.Int32, 8), ConstantInt.GetS(Type.Int32, 32)], null);
+                // call llvm.memcpy.i32( "to", "from", type_size, alignment (32 in clang) );
+                b.buildCall(llvm_memcpy, [to, from, ConstantInt.GetS(Type.Int32, 4), ConstantInt.GetS(Type.Int32, 32)], null);
                 // return "to"
                 return t;
             }
--- a/misc/Error.d	Sat Apr 26 23:11:13 2008 +0200
+++ b/misc/Error.d	Mon Apr 28 21:40:00 2008 +0200
@@ -7,6 +7,7 @@
 import llvm.type;
 
 import lexer.Token,
+       sema.DType,
        sema.Symbol;
 
 class Error : Exception
--- a/sema/SymbolTable.d	Sat Apr 26 23:11:13 2008 +0200
+++ b/sema/SymbolTable.d	Mon Apr 28 21:40:00 2008 +0200
@@ -3,6 +3,7 @@
 import tango.io.Stdout;
 
 import lexer.Token,
+       ast.Decl,
        ast.Exp;
 
 import sema.DType;
@@ -57,7 +58,7 @@
         return res;
     }
 
-    Symbol parentFunction()
+    FuncDecl parentFunction()
     {
         if (func !is null)
             return func;
@@ -67,6 +68,16 @@
             return null;
     }
 
+    int stmtIndex()
+    {
+        if (currentStmtIndex != -1)
+            return currentStmtIndex;
+        else if (enclosing !is null)
+            return enclosing.stmtIndex();
+        else
+            return -1;
+    }
+
     int opEquals(Object o)
     {
         return this is o;
@@ -75,18 +86,19 @@
     char[] toString()
     {
         if (func)
-            return Stdout.layout.convert("{}: {}", func.id.get, symbols.length);
+            return Stdout.layout.convert("{}: {}", func.identifier.get, symbols.length);
         return Stdout.layout.convert("root: {}", symbols.length);
     }
 
-    Symbol parentFunction(Symbol f)
+    FuncDecl parentFunction(FuncDecl f)
     {
         func = f;
         return f;
     }
     DType[char[]] types;
+    int currentStmtIndex = -1;
 private:
     Symbol[Identifier] symbols;
-    Symbol func;
+    FuncDecl func;
 }
 
--- a/sema/SymbolTableBuilder.d	Sat Apr 26 23:11:13 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Mon Apr 28 21:40:00 2008 +0200
@@ -97,12 +97,14 @@
     override void visitStmt(Stmt s)
     {
         s.env = current();
+        s.stmtIndex = s.env.stmtIndex;
         super.visitStmt(s);
     }
 
     override void visitExp(Exp e)
     {
         e.env = current();
+        e.stmtIndex = e.env.stmtIndex;
         super.visitExp(e);
     }
 
@@ -114,11 +116,14 @@
         visitExp(d.type);
         visitExp(d.identifier);
         d.env = current();
-        sc.parentFunction = sym;
+        sc.parentFunction = d;
         foreach (arg; d.funcArgs)
             visitDecl(arg);
         foreach (stmt; d.statements)
+        {
+            sc.currentStmtIndex++;
             visitStmt(stmt);
+        }
         pop(sc);
     }