changeset 96:438e6ed4cda1 new_gen

Now D-Mangling the function types. Still need to mangle "scopes" - by that i mean structs, classes and modules.
author Anders Johnsen <skabet@gmail.com>
date Tue, 06 May 2008 18:51:08 +0200
parents 6aecbe5a7706
children 198ad05f3ace
files ast/Exp.d gen/CodeGen.d sema/DType.d tests/code/array_1.d
diffstat 4 files changed, 75 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Exp.d	Tue May 06 17:01:00 2008 +0200
+++ b/ast/Exp.d	Tue May 06 18:51:08 2008 +0200
@@ -505,6 +505,16 @@
     {
         return name;
     }
+    
+    char[] getMangled()
+    {
+        DType t = type;
+
+        if(name == "main")
+            return "_Dmain";
+
+        return "_D"~name~t.mangle;
+    }
 
     hash_t toHash()
     {
--- a/gen/CodeGen.d	Tue May 06 17:01:00 2008 +0200
+++ b/gen/CodeGen.d	Tue May 06 18:51:08 2008 +0200
@@ -118,7 +118,7 @@
                 else if(auto f = cast(DFunction)ret_t)
                     ret_t = f.returnType;
                 auto func_t = FunctionType.Get(llvm(ret_t), param_types);
-                auto llfunc = m.addFunction(func_t, fd.identifier.get);
+                auto llfunc = m.addFunction(func_t, fd.identifier.getMangled);
 
                 foreach (i, p; fd.funcArgs)
                 {
@@ -171,7 +171,7 @@
                     return;
 
                 llvm(funcDecl.type);
-                auto llfunc = m.getNamedFunction(funcDecl.type.name);
+                auto llfunc = m.getNamedFunction(funcDecl.identifier.getMangled);
                 auto func_tp = cast(PointerType)llfunc.type;
                 auto func_t = cast(FunctionType)func_tp.elementType();
                 auto ret_t = func_t.returnType();
--- a/sema/DType.d	Tue May 06 17:01:00 2008 +0200
+++ b/sema/DType.d	Tue May 06 18:51:08 2008 +0200
@@ -97,6 +97,15 @@
     }
     private DPointer myPointer;
 
+    /** 
+      Mangle the DType following the specs at http://digitalmars.com/d/1.0/abi.html
+     **/
+    char[] mangle()
+    {
+        /// expects to be void
+        return "v";
+    }
+
     /**
       Get a type representing a static array of this type with length 'size'
      */
@@ -137,6 +146,24 @@
  */
 class DInteger : DType
 {
+    private static char[][DInteger] mangle_types;
+
+    static this()
+    {
+        mangle_types = 
+        [
+            Bool    : "b",
+            Byte    : "g",
+            UByte   : "h",
+            Short   : "s",
+            UShort  : "t",
+            Int     : "i",
+            UInt    : "k",
+            Long    : "l",
+            ULong   : "m"
+        ];
+    }
+
     this(char[] name, int bits, bool unsigned)
     {
         super(name, null);
@@ -157,6 +184,11 @@
     override bool isInteger() { return true; }
     override DInteger asInteger() { return this; }
 
+    override char[] mangle()
+    {
+        return mangle_types[this];
+    }
+
     int bits;
     bool unsigned;
 }
@@ -199,6 +231,11 @@
     DStructMember[char[]] members;
     private int bytes_total;
 
+    override char[] mangle()
+    {
+        return "S"~Integer.toString(name.length)~name;
+    }
+
     struct DStructMember
     {
         DType type;
@@ -220,6 +257,11 @@
 
     int byteSize() { return arrayOf.byteSize * size; }
 
+    override char[] mangle()
+    {
+        return "G"~Integer.toString(size)~arrayOf.mangle;
+    }
+
     DType arrayOf;
     const int size;
 }
@@ -237,6 +279,11 @@
 
     int byteSize() { return DType.Int.byteSize; }
 
+    override char[] mangle()
+    {
+        return "P"~pointerOf.mangle;
+    }
+
     DType pointerOf;
 }
 
@@ -250,6 +297,19 @@
     override bool isFunction() { return true; }
     override DFunction asFunction() { return this; }
 
+    override char[] mangle()
+    {
+        char[] res;
+        res ~= "F";
+
+        foreach(param ; params)
+            res ~= "J" ~ param.mangle;
+
+        res ~= "Z" ~ returnType.mangle;
+
+        return res;
+    }
+
     DType[] params;
     DType returnType;
     bool firstParamIsReturnValue = false;
--- a/tests/code/array_1.d	Tue May 06 17:01:00 2008 +0200
+++ b/tests/code/array_1.d	Tue May 06 18:51:08 2008 +0200
@@ -2,7 +2,7 @@
 struct Array
 {
     int[] data;
-    int length
+    int length;
 }
 
 void insert(Array a, int v)
@@ -13,10 +13,10 @@
 
 int main()
 {
-    array a;
+    Array a;
     a.length = 0;
 
     insert(a, 5);
     
-    return a.data[0]
+    return a.data[0];
 }