changeset 160:6cb2f4201e2a

Improved static arrays Here is a list of some stuff that works char[3] s = "hey" char[3] s2 = s; s2[1] = 98 // no support for chars, but 98 = 'b' :) int[2] i; i[0] = 2; Still can't pass static arrays to functions
author Anders Halager <halager@gmail.com>
date Tue, 22 Jul 2008 13:29:20 +0200
parents a2d9121d6dff
children 0e10479623f6
files ast/Exp.d gen/CodeGen.d parser/Parser.d sema/DType.d sema/LiteralInterpreter.d sema/TypeCheck.d test.td tests/parser/string_1.d
diffstat 8 files changed, 32 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/ast/Exp.d	Tue Jul 22 00:34:49 2008 +0200
+++ b/ast/Exp.d	Tue Jul 22 13:29:20 2008 +0200
@@ -485,9 +485,10 @@
         this.str = str;
     }
 
-    override DType type() { return null; }
+    override DType type() { return DType.Char.getAsStaticArray(data.length); }
 
     char[] str;
+    ubyte[] data;
 }
 
 class NewExp : Exp
--- a/gen/CodeGen.d	Tue Jul 22 00:34:49 2008 +0200
+++ b/gen/CodeGen.d	Tue Jul 22 13:29:20 2008 +0200
@@ -295,6 +295,14 @@
                     case NumberType.Real:
                         return RValue(ConstantReal.Get(Type.X86_FP80, integerLit.number.floating));
                 }
+            case ExpType.StringExp:
+                auto stringExp = cast(StringExp)exp;
+                char[] data = cast(char[])stringExp.data;
+                auto string_constant = ConstantArray.GetString(data, true);
+                auto gv = m.addGlobal(string_constant, "string");
+                gv.linkage = Linkage.Internal;
+                gv.globalConstant = true;
+                return RValue(gv);
             case ExpType.Negate:
                 auto negateExp = cast(NegateExp)exp;
                 auto target = genExpression(negateExp.exp);
@@ -346,7 +354,9 @@
 
             case ExpType.Identifier:
                 auto id = cast(Identifier)exp;
-                if(id.type.isStruct() || id.type.isArray())
+                if (id.type.isStruct()
+                        || id.type.isArray()
+                        || id.type.isStaticArray())
                     return RValue(table.find(id.get));
                 else
                     return RValue(b.buildLoad(table.find(id.get), id.get));
@@ -701,6 +711,8 @@
 
         if (auto st = t.asStruct())
             genMemcpy(to, from, t);
+        else if (auto sa = t.asStaticArray())
+            genMemcpy(to, from, t, sa.arrayOf.byteSize);
         else
             b.buildStore(from, to);
     }
@@ -709,7 +721,7 @@
       Copy from src into dst. The values are assumed to have the same size,
       and the amount of bytes to copy is taken from t.
      **/
-    void genMemcpy(Value dst, Value src, DType t)
+    void genMemcpy(Value dst, Value src, DType t, int alignment = 16)
     {
         Value from = b.buildBitCast(src, BytePtr, ".copy_from");
         Value to = b.buildBitCast(dst, BytePtr, ".copy_to");
@@ -717,7 +729,7 @@
         args[0] = to;
         args[1] = from;
         args[2] = ConstantInt.GetS(Type.Int32, t.byteSize());
-        args[3] = ConstantInt.GetS(Type.Int32, 32);
+        args[3] = ConstantInt.GetS(Type.Int32, alignment);
         b.buildCall(llvm_memcpy, args[], null);
     }
 
--- a/parser/Parser.d	Tue Jul 22 00:34:49 2008 +0200
+++ b/parser/Parser.d	Tue Jul 22 13:29:20 2008 +0200
@@ -706,7 +706,7 @@
     {
         // manually hardcoded to only support "type id [= exp];"
         // as that is the only thing the codegen understands
-        Id type = parseType;
+        Id type = parseType();
         Id id = Id(next());
         Exp init;
         if (skip(Tok.Assign))
--- a/sema/DType.d	Tue Jul 22 00:34:49 2008 +0200
+++ b/sema/DType.d	Tue Jul 22 13:29:20 2008 +0200
@@ -100,6 +100,11 @@
         return cast(hash_t)(cast(void*)this);
     }
 
+    char[] toString()
+    {
+        return id;
+    }
+
     char[] name() { return id; }
     SourceLocation getLoc() { return loc; }
     int byteSize() { return 0; }
@@ -473,7 +478,7 @@
 {
     this(DType arrayOf, int size, DType actual = null)
     {
-        super(id, actual);
+        super(arrayOf.id ~ "[" ~ Integer.toString(size) ~ "]", actual);
         this.arrayOf = arrayOf;
         this.size = size;
     }
--- a/sema/LiteralInterpreter.d	Tue Jul 22 00:34:49 2008 +0200
+++ b/sema/LiteralInterpreter.d	Tue Jul 22 13:29:20 2008 +0200
@@ -20,7 +20,8 @@
     
     void visitStringExp(StringExp exp)
     {
-        auto type = parseString(exp.str, exp.loc, messages);
+        auto str = parseString(exp.str, exp.loc, messages);
+        exp.data = str.data;
     }
 
     void visitIntegerLit(IntegerLit exp)
--- a/sema/TypeCheck.d	Tue Jul 22 00:34:49 2008 +0200
+++ b/sema/TypeCheck.d	Tue Jul 22 13:29:20 2008 +0200
@@ -258,7 +258,7 @@
 
         if(decl.init)
         {
-            auto varType = decl.type;
+            auto varType = decl.identifier.type;
             auto expType = decl.init.type;
             if(varType.byteSize != expType.byteSize)
             {
--- a/test.td	Tue Jul 22 00:34:49 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-
-
-
-int main()
-{
-    int y = 4;
-
-    karina k;
-
-
-    while (y > 0)
-        y = y - 1;
-
-    return x;
-}
-
-int x = 4;
-
-
-struct karina
-{
-    anders a;
-    int age;
-    int width;
-    int height;
-    int lovers;
-}
-struct anders
-{
-    int hej;
-}
--- a/tests/parser/string_1.d	Tue Jul 22 00:34:49 2008 +0200
+++ b/tests/parser/string_1.d	Tue Jul 22 13:29:20 2008 +0200
@@ -26,16 +26,14 @@
 
     char[8]     s16 = "\x61\u05D0\U000201A4";
     char[2]     s17 = "\122\522";
-    char[6]     s15 = x"61 62 63 64
+    char[6]     s18 = x"61 62 63 64
                         65 66 67 68";
 
-    char[4]     s16 = "\&reg;\&amp;";
+    char[4]     s19 = "\&reg;\&amp;";
 
-    char[4]     s16 = "\&reg;\&amp;"c;
-    wchar[2]    s16 = "\&reg;\&amp;"w;
-    dchar[2]    s16 = "\&reg;\&amp;"d;
-    
-
+    char[4]     s20 = "\&reg;\&amp;"c;
+    wchar[2]    s21 = "\&reg;\&amp;"w;
+    dchar[2]    s22 = "\&reg;\&amp;"d;
 
     return 0;
 }