changeset 59:1d6f4ad38a91 new_gen

Make most of the tests pass again Still have two bugs, one is that type conversion has been diabled for return statements, the other is at line ~540 of CodeGen.d, were the following error is emitted: Can't find an implicit conversion between { i32 } and i32
author Anders Halager <halager@gmail.com>
date Tue, 29 Apr 2008 00:31:56 +0200
parents fc62c5296a1c
children 2451f0904bf6
files ast/Exp.d ast/Stmt.d gen/CodeGen.d sema/SymbolTable.d sema/SymbolTableBuilder.d
diffstat 5 files changed, 45 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Exp.d	Mon Apr 28 21:51:39 2008 +0200
+++ b/ast/Exp.d	Tue Apr 29 00:31:56 2008 +0200
@@ -54,7 +54,7 @@
     override DType type()
     {
         DFunction f = cast(DFunction)exp.type();
-        assert(f, "Can only call functions");
+        assert(f !is null, "Can only call functions");
         return f.return_type;
     }
 
@@ -65,6 +65,11 @@
 
     Exp simplify()
     {
+        DFunction func_t = cast(DFunction)exp.type();
+        assert(func_t !is null, "Calling on something that isn't a function");
+        if (cast(DStruct)func_t.return_type is null)
+            return this;
+
         auto call = cast(Identifier)exp;
         FuncDecl f = env.parentFunction;
         auto i = new Identifier("temp.var");
@@ -224,7 +229,6 @@
     }
     Exp simplify()
     {
-        child.simplify;
         target = target.simplify;
         return this;
     }
@@ -280,7 +284,7 @@
 
     override DType type()
     {
-        if (myType)
+        if (myType !is null)
             return myType;
         myType = env.find(this).type;
         return myType;
--- a/ast/Stmt.d	Mon Apr 28 21:51:39 2008 +0200
+++ b/ast/Stmt.d	Tue Apr 29 00:31:56 2008 +0200
@@ -65,7 +65,7 @@
     void simplify()
     {
         FuncDecl f = env.parentFunction;
-        if(f.sret)
+        if(f !is null && f.sret)
         {
             auto i = new Identifier("ret.val");
             i.env = f.env;
@@ -139,7 +139,8 @@
     {
         cond.simplify;
         then_body.simplify;
-        else_body.simplify;
+        if (else_body)
+            else_body.simplify;
     }
 
     Exp cond;
--- a/gen/CodeGen.d	Mon Apr 28 21:51:39 2008 +0200
+++ b/gen/CodeGen.d	Tue Apr 29 00:31:56 2008 +0200
@@ -83,12 +83,12 @@
                     else
                         param_types ~= llvm(t);
                 }
-                auto ret_t = llvm(fd.env.find(fd.identifier).type);
-                if(auto st = cast(StructType)ret_t)
-                {
-                    ret_t = PointerType.Get(ret_t);
-                }
-                auto func_t = FunctionType.Get(ret_t, param_types);
+                auto ret_t = fd.env.find(fd.identifier).type;
+                if(auto st = cast(DStruct)ret_t)
+                    ret_t = DType.Void;
+                else if(auto f = cast(DFunction)ret_t)
+                    ret_t = f.return_type;
+                auto func_t = FunctionType.Get(llvm(ret_t), param_types);
                 auto llfunc = m.addFunction(func_t, fd.identifier.get);
 
                 foreach (i, p; fd.funcArgs)
@@ -281,13 +281,7 @@
                 foreach (arg; callExp.args)
                 {
                     Value v = genExpression(arg);
-
-                    if(auto ptr = cast(PointerType)v.type)
-                    {
-                        args ~= v;
-                    }
-                    else
-                        args ~= v;
+                    args ~= v;
 
                 }
                 // BUG: doesn't do implicit type-conversion
@@ -599,7 +593,18 @@
             type_map[t] = res;
             return res;
         }
-        assert(0, "Only integers are supported");
+        else if (auto f = cast(DFunction)t)
+        {
+            SmallArray!(Type, 8) params;
+            foreach(param; f.params)
+                params ~= llvm(param);
+
+            Type ret_t = llvm(f.return_type);
+            Type res = FunctionType.Get(ret_t, params.unsafe());
+            type_map[t] = res;
+            return res;
+        }
+        assert(0, "Only integers, structs and functions are supported");
     }
 
     // Might as well insert all the basic types from the start
--- a/sema/SymbolTable.d	Mon Apr 28 21:51:39 2008 +0200
+++ b/sema/SymbolTable.d	Tue Apr 29 00:31:56 2008 +0200
@@ -51,10 +51,12 @@
     char[][] names()
     {
         char[][] res;
+        if (parentFunction() !is null)
+            res ~= "pf: " ~ parentFunction().identifier.get;
         if (enclosing)
             res = enclosing.names;
         foreach (id, sym; symbols)
-            res ~= sym.id.name ~ " : " ~ sym.type.name;
+            res ~= sym.id.name ~ " : " ~ (sym.type is null? "?" : sym.type.name);
         return res;
     }
 
--- a/sema/SymbolTableBuilder.d	Mon Apr 28 21:51:39 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Tue Apr 29 00:31:56 2008 +0200
@@ -6,7 +6,8 @@
 public
 import sema.SymbolTable;
 
-import sema.Visitor;
+import sema.Visitor,
+       basic.SmallArray;
 
 class SymbolTableBuilder : Visitor!(void)
 {
@@ -20,13 +21,23 @@
 
     override void visitFuncDecl(FuncDecl d)
     {
-        d.env.find(d.identifier).type = typeOf(d.type, d.env);
         visitExp(d.type);
         visitExp(d.identifier);
+        SmallArray!(DType, 8) arg_types;
         foreach (arg; d.funcArgs)
+        {
             visitDecl(arg);
+            arg_types ~= d.env.find(arg.identifier).type;
+        }
         foreach (stmt; d.statements)
             visitStmt(stmt);
+
+        auto func_t = new DFunction(d.identifier);
+        func_t.return_type = d.env.findType(d.type);
+        func_t.params = arg_types.safe();
+
+        auto sym = d.env.find(d.identifier);
+        sym.type = func_t;
     }
 
     override void visitVarDecl(VarDecl d)