changeset 27:9031487e97d7 new_gen

Various changes related to DType * Drop the typeToLLVM table from LLVMGen * Removed circular dependency * Added basic types to DType
author Anders Halager <halager@gmail.com>
date Sun, 20 Apr 2008 01:08:50 +0200
parents b4dc2b2c0e38
children 69464d465284
files gen/LLVMGen.d sema/DType.d sema/Symbol.d sema/SymbolTable.d sema/SymbolTableBuilder.d
diffstat 5 files changed, 114 insertions(+), 56 deletions(-) [+]
line wrap: on
line diff
--- a/gen/LLVMGen.d	Sat Apr 19 22:19:14 2008 +0200
+++ b/gen/LLVMGen.d	Sun Apr 20 01:08:50 2008 +0200
@@ -29,17 +29,6 @@
 public:
     this()
     {
-        typeToLLVM =
-        [
-            "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;
         b = new Builder;
         
@@ -73,14 +62,14 @@
         table.enterScope;
 
         auto registerFunc =
-            (FuncDecl funcDecl)
+            (FuncDecl fd)
             {
                 Type[] param_types;
-                foreach (param; funcDecl.funcArgs)
-                    param_types ~= typeToLLVM[param.type.get];
-                auto ret_t = typeToLLVM[funcDecl.type.get];
+                foreach (p; fd.funcArgs)
+                    param_types ~= p.env.find(p.identifier).type.llvm();
+                auto ret_t = fd.env.find(fd.identifier).type.llvm();
                 auto func_t = FunctionType.Get(ret_t, param_types);
-                auto llfunc = m.addFunction(func_t, funcDecl.identifier.get);
+                auto llfunc = m.addFunction(func_t, fd.identifier.get);
             };
         auto visitor = new VisitFuncDecls(registerFunc);
         visitor.visit(decls);
@@ -107,7 +96,9 @@
                 FuncDecl funcDecl = cast(FuncDecl)decl;
 
                 auto llfunc = m.getNamedFunction(funcDecl.identifier.get);
-                auto ret_t = typeToLLVM[funcDecl.type.get];
+                auto func_tp = cast(PointerType)llfunc.type;
+                auto func_t = cast(FunctionType)func_tp.elementType();
+                auto ret_t = func_t.returnType();
 
                 auto bb = llfunc.appendBasicBlock("entry");
                 b.positionAtEnd(bb);
@@ -138,8 +129,9 @@
 
             case DeclType.VarDecl:
                 auto varDecl = cast(VarDecl)decl;
-                Type t = typeToLLVM[varDecl.type.get];
-                GlobalVariable g = m.addGlobal(t, varDecl.identifier.get);
+                auto sym = varDecl.env.find(varDecl.identifier);
+                Type t = sym.type.llvm();
+                GlobalVariable g = m.addGlobal(t, sym.id.get);
                 g.initializer = ConstantInt.GetS(t, 0);
                 table[varDecl.identifier.get] = g;
                 break;
@@ -155,12 +147,12 @@
         {
             case DeclType.VarDecl:
                 auto varDecl = cast(VarDecl)decl;
-                Type t = typeToLLVM[varDecl.type.get];
                 auto name = varDecl.identifier.get;
-                auto AI = b.buildAlloca(t, name);
+                auto sym = varDecl.env.find(varDecl.identifier);
+                auto AI = b.buildAlloca(sym.type.llvm(), name);
                 table[name] = AI;
                 if (varDecl.init)
-                    buildAssign(varDecl.env.find(varDecl.identifier), varDecl.init);
+                    buildAssign(sym, varDecl.init);
                 break;
         
             default:
@@ -237,7 +229,7 @@
             case StmtType.Return:
                 auto ret = cast(ReturnStmt)stmt;
                 auto sym = stmt.env.parentFunction();
-                Type t = typeToLLVM[sym.type.name];
+                Type t = sym.type.llvm();
                 Value v = genExpression(ret.exp);
                 if (v.type != t)
                 {
@@ -329,7 +321,7 @@
 
     private Value buildAssign(Symbol sym, Exp exp)
     {
-        Type t = typeToLLVM[sym.type.name];
+        Type t = sym.type.llvm();
         auto name = sym.id.get;
         auto AI = table.find(name);
         Value v = genExpression(exp);
@@ -358,7 +350,6 @@
     FuncDecl[char[]] functions;
 
     SimpleSymbolTable table;
-    static Type[char[]] typeToLLVM;
     alias Value delegate(Value, Value, char[]) OpBuilder;
     static OpBuilder[BinaryExp.Operator] opToLLVM;
 }
--- a/sema/DType.d	Sat Apr 19 22:19:14 2008 +0200
+++ b/sema/DType.d	Sun Apr 20 01:08:50 2008 +0200
@@ -2,21 +2,32 @@
 
 import tango.text.Util : jhash;
 
-import sema.SymbolTable;
+import LLVM = llvm.llvm;
+
+import lexer.Token,
+       ast.Exp;
 
 class DType
 {
-    Identifier id;
-    Scope sc;
-    DType actual;
+    private char[] id;
+    private Location loc;
+    public DType actual;
+    private LLVM.Type llvmType;
 
-    this(Identifier id, Scope sc, DType actual = null)
+    this(Identifier id, DType actual = null)
+    {
+        Token temp = id.token;
+        this.id = temp.get;
+        this.loc = temp.location;
+        if (actual !is null)
+            this.actual = this;
+    }
+
+    this(char[] id, DType actual = null)
     {
         this.id = id;
-        this.sc = sc;
-        if (actual is null)
+        if (actual !is null)
             this.actual = this;
-        _name = id.get;
     }
 
     int opEquals(Object o)
@@ -38,26 +49,60 @@
         return cast(hash_t)(cast(void*)this);
     }
 
-    char[] name() { return _name; }
+    char[] name() { return id; }
+    LLVM.Type llvm() { return llvmType; }
+
+    static DInteger
+        Bool,
+        Byte, UByte, Short, UShort,
+        Int, UInt, Long, ULong;
+
+    static DType Void;
+
+    static this()
+    {
+        Void   = new DType("void");
 
-private:
-    char[] _name;
+        Bool   = new DInteger("bool",    1, false);
+        Byte   = new DInteger("byte",    8, false);
+        UByte  = new DInteger("ubyte",   8, true);
+        Short  = new DInteger("short",  16, false);
+        UShort = new DInteger("ushort", 16, true);
+        Int    = new DInteger("int",    32, false);
+        UInt   = new DInteger("uint",   32, true);
+        Long   = new DInteger("long",   64, false);
+        ULong  = new DInteger("ulong",  64, true);
+    }
+}
+
+class DInteger : DType
+{
+    this(char[] name, int bits, bool unsigned)
+    {
+        super(name, null);
+        this.bits = bits;
+        this.unsigned = unsigned;
+        llvmType = LLVM.IntegerType.Get(bits);
+    }
+
+    int bits;
+    bool unsigned;
 }
 
 class DStruct : DType
 {
-    this(Identifier id, Scope sc, DType actual = null)
+    this(Identifier id, DType actual = null)
     {
-        super(id, sc, actual);
+        super(id, actual);
     }
     DType[] members;
 }
 
 class DFunction : DType
 {
-    this(Identifier id, Scope sc, DType actual = null)
+    this(Identifier id, DType actual = null)
     {
-        super(id, sc, actual);
+        super(id, actual);
     }
     DType[] params;
     DType return_type;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sema/Symbol.d	Sun Apr 20 01:08:50 2008 +0200
@@ -0,0 +1,12 @@
+module sema.Symbol;
+
+public
+import sema.DType,
+       ast.Exp;
+
+class Symbol
+{
+    Identifier id;
+    DType type;
+}
+
--- a/sema/SymbolTable.d	Sat Apr 19 22:19:14 2008 +0200
+++ b/sema/SymbolTable.d	Sun Apr 20 01:08:50 2008 +0200
@@ -3,13 +3,10 @@
 import tango.io.Stdout;
 
 import lexer.Token,
-       sema.DType;
-
-import ast.Exp : Identifier;
+       ast.Exp;
 
-class SymbolTable
-{
-}
+public
+import sema.Symbol;
 
 class Scope
 {
@@ -91,9 +88,3 @@
     Symbol func;
 }
 
-class Symbol
-{
-    Identifier id;
-    DType type;
-}
-
--- a/sema/SymbolTableBuilder.d	Sat Apr 19 22:19:14 2008 +0200
+++ b/sema/SymbolTableBuilder.d	Sun Apr 20 01:08:50 2008 +0200
@@ -4,8 +4,7 @@
        tango.core.Array : find;
 
 public
-import sema.SymbolTable,
-       sema.DType;
+import sema.SymbolTable;
 
 import sema.Visitor;
 
@@ -14,6 +13,16 @@
     this()
     {
         table ~= new Scope;
+        types["void"]    = DType.Void;
+        types["bool"]    = DType.Bool;
+        types["byte"]    = DType.Byte;
+        types["ubyte"]   = DType.UByte;
+        types["short"]   = DType.Short;
+        types["ushort"]  = DType.UShort;
+        types["int"]     = DType.Int;
+        types["uint"]    = DType.UInt;
+        types["long"]    = DType.Long;
+        types["ulong"]   = DType.ULong;
     }
 
     override void visit(Decl[] decls)
@@ -43,7 +52,7 @@
     override void visitFuncDecl(FuncDecl d)
     {
         auto sym = current().add(d.identifier);
-        sym.type = new DType(d.type, current());
+        sym.type = typeOf(d.type);
         visitExp(d.type);
         visitExp(d.identifier);
         d.env = current();
@@ -68,7 +77,7 @@
 
         auto sc = current();
         auto sym = sc.add(d.identifier);
-        sym.type = new DType(d.type, sc);
+        sym.type = typeOf(d.type);
         d.env = sc;
         visitExp(d.type);
         visitExp(d.identifier);
@@ -139,5 +148,15 @@
     {
         return table[$ - 1];
     }
+
+    DType typeOf(Identifier id)
+    {
+        if (auto type = id.get in types)
+            return *type;
+        DType res = new DType(id);
+        types[id.get] = res;
+        return res;
+    }
+    DType[char[]] types;
 }