changeset 503:7148a3f2b44b

merge
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Wed, 13 Aug 2008 15:43:34 +0200
parents 837af2a63564 (diff) 9aac7cff8342 (current diff)
children 577211114d6d
files gen/llvmhelpers.cpp
diffstat 24 files changed, 208 insertions(+), 171 deletions(-) [+]
line wrap: on
line diff
--- a/gen/llvmhelpers.cpp	Tue Aug 12 19:50:41 2008 +0200
+++ b/gen/llvmhelpers.cpp	Wed Aug 13 15:43:34 2008 +0200
@@ -956,6 +956,9 @@
     llvm::GlobalVariable* gvar = llvm::cast<llvm::GlobalVariable>(vd->ir.irGlobal->value);
     if (!(vd->storage_class & STCextern) && (vd->getModule() == gIR->dmodule || istempl))
     {
+        Logger::println("setting initializer");
+        Logger::cout() << "global: " << *gvar << '\n';
+        Logger::cout() << "init:   " << *_init << '\n';
         gvar->setInitializer(_init);
         // do debug info
         if (global.params.symdebug)
@@ -1253,22 +1256,7 @@
     if (!init)
     {
         Logger::println("const default initializer for %s", type->toChars());
-
-        if(type->ty == Tsarray)
-        {
-            Logger::println("type is a static array, building constant array initializer");
-            TypeSArray* arrtype = (TypeSArray*)type;
-            Type* elemtype = type->next;
-
-            integer_t arraydim;
-            arraydim = arrtype->dim->toInteger();
-
-            std::vector<LLConstant*> inits(arraydim, elemtype->defaultInit()->toConstElem(gIR));
-            const LLArrayType* arrty = LLArrayType::get(DtoType(elemtype),arraydim);
-            _init = LLConstantArray::get(arrty, inits);
-        }
-        else
-            _init = type->defaultInit()->toConstElem(gIR);
+        _init = DtoDefaultInit(type);
     }
     else if (ExpInitializer* ex = init->isExpInitializer())
     {
@@ -1391,6 +1379,74 @@
     return 0;
 }
 
+//////////////////////////////////////////////////////////////////////////////////////////
+
+static LLConstant* expand_to_sarray(Type *base, Expression* exp)
+{
+    Logger::println("building type %s from expression (%s) of type %s", base->toChars(), exp->toChars(), exp->type->toChars());
+    const LLType* dstTy = DtoType(base);
+    Logger::cout() << "final llvm type requested: " << *dstTy << '\n';
+    
+    LLConstant* val = exp->toConstElem(gIR);
+    
+    Type* expbase = exp->type->toBasetype();
+    Type* t = base;
+    
+    LLSmallVector<size_t, 4> dims;
+
+    while(1)
+    {
+        if (t->equals(expbase))
+            break;
+        assert(t->ty == Tsarray);
+        TypeSArray* tsa = (TypeSArray*)t;
+        dims.push_back(tsa->dim->toInteger());
+        assert(t->next);
+        t = t->next->toBasetype();
+    }
+    
+    size_t i = dims.size();
+    assert(i);
+
+    std::vector<LLConstant*> inits;
+    while (i--)
+    {
+        const LLArrayType* arrty = LLArrayType::get(val->getType(), dims[i]);
+        inits.clear();
+        inits.insert(inits.end(), dims[i], val);
+        val = LLConstantArray::get(arrty, inits);
+    }
+    
+    return val;
+}
+
+LLConstant* DtoDefaultInit(Type* type)
+{
+    Expression* exp = type->defaultInit();
+    
+    Type* expbase = exp->type->toBasetype();
+    Type* base = type->toBasetype();
+    
+    // if not the same basetypes, we won't get the same llvm types either
+    if (!expbase->equals(base))
+    {
+        if (base->ty == Tsarray)
+        {
+            Logger::println("type is a static array, building constant array initializer from single value");
+            return expand_to_sarray(base, exp);
+        }
+        else
+        {
+            error("cannot yet convert default initializer %s from type %s to %s", exp->toChars(), exp->type->toChars(), type->toChars());
+            fatal();
+        }
+        assert(0);
+        
+    }
+    
+    return exp->toConstElem(gIR);
+}
+
 
 //////////////////////////////////////////////////////////////////////////////////////////
 
--- a/gen/llvmhelpers.h	Tue Aug 12 19:50:41 2008 +0200
+++ b/gen/llvmhelpers.h	Wed Aug 13 15:43:34 2008 +0200
@@ -105,6 +105,9 @@
 /// Converts any value to a boolean (llvm i1)
 LLValue* DtoBoolean(Loc& loc, DValue* dval);
 
+/// get the default initializer of the type
+LLConstant* DtoDefaultInit(Type* t);
+
 ////////////////////////////////////////////
 // gen/tocall.cpp stuff below
 ////////////////////////////////////////////
--- a/tests/mini/bug26.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,8 +0,0 @@
-module bug26;
-
-extern int i;
-
-void main()
-{
-    int j = i;
-}
--- a/tests/mini/bug57.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-import std.stdio;
-class Foo {}
-void func3()
-{
-    Foo[1] test=[new Foo];
-    writefln(test);
-}
-void main() {
-    func3();
-}
--- a/tests/mini/bug58.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,10 +0,0 @@
-module bug58;
-import std.stdio;
-void main()
-{
-    int[16] arr = [1,16,2,15,3,14,4,13,5,12,6,11,7,10,8,9];
-    writefln("arr = ",arr);
-    arr.sort;
-    writefln("arr.sort = ",arr);
-    assert(arr == [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
-}
--- a/tests/mini/bug66.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,5 +0,0 @@
-module bug66;
-import std.stdio;
-class Scene { string name() { return "Scene"; } }
-class Group : Scene { this () { } }
-void main() { writefln((new Group).name); }
--- a/tests/mini/bug71.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,20 +0,0 @@
-module bug71;
-
-void main()
-{
-    static TypeInfo skipCI(TypeInfo valti)
-    {
-      while (1)
-      {
-    if (valti.classinfo.name.length == 18 &&
-        valti.classinfo.name[9..18] == "Invariant")
-        valti = (cast(TypeInfo_Invariant)valti).next;
-    else if (valti.classinfo.name.length == 14 &&
-        valti.classinfo.name[9..14] == "Const")
-        valti = (cast(TypeInfo_Const)valti).next;
-    else
-        break;
-      }
-      return valti;
-    }
-}
\ No newline at end of file
--- a/tests/mini/bug79.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,9 +0,0 @@
-module bug79;
-import std.c.linux.linux;
-void main()
-{
-    timespec ts; 
-    ts.tv_nsec -= 1;
-    //auto t = ts.tv_nsec - 1;
-    //t -= 1;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/compile_bug26.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,8 @@
+module bug26;
+
+extern int i;
+
+void main()
+{
+    int j = i;
+}
--- a/tests/mini/imports2.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,6 +0,0 @@
-module imports2;
-import std.stdio;
-
-void main() {
-    writefln("Hello world!"[]);
-}
--- a/tests/mini/imports_1of2.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,11 +0,0 @@
-module imports_1of2;
-
-import imports_2of2;
-
-void main()
-{
-    assert(func() == 42);
-    S s;
-    s.l = 32;
-    assert(s.l == 32);
-}
--- a/tests/mini/imports_2of2.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,11 +0,0 @@
-module imports_2of2;
-
-int func()
-{
-    return 42;
-}
-
-struct S
-{
-    long l;
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/phobos/bug57.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,10 @@
+import std.stdio;
+class Foo {}
+void func3()
+{
+    Foo[1] test=[new Foo];
+    writefln(test);
+}
+void main() {
+    func3();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/phobos/bug58.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,10 @@
+module bug58;
+import std.stdio;
+void main()
+{
+    int[16] arr = [1,16,2,15,3,14,4,13,5,12,6,11,7,10,8,9];
+    writefln("arr = ",arr);
+    arr.sort;
+    writefln("arr.sort = ",arr);
+    assert(arr == [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/phobos/bug66.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,5 @@
+module bug66;
+import std.stdio;
+class Scene { string name() { return "Scene"; } }
+class Group : Scene { this () { } }
+void main() { writefln((new Group).name); }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/phobos/bug71.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,20 @@
+module bug71;
+
+void main()
+{
+    static TypeInfo skipCI(TypeInfo valti)
+    {
+      while (1)
+      {
+    if (valti.classinfo.name.length == 18 &&
+        valti.classinfo.name[9..18] == "Invariant")
+        valti = (cast(TypeInfo_Invariant)valti).next;
+    else if (valti.classinfo.name.length == 14 &&
+        valti.classinfo.name[9..14] == "Const")
+        valti = (cast(TypeInfo_Const)valti).next;
+    else
+        break;
+      }
+      return valti;
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/phobos/bug79.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,9 @@
+module bug79;
+import std.c.linux.linux;
+void main()
+{
+    timespec ts; 
+    ts.tv_nsec -= 1;
+    //auto t = ts.tv_nsec - 1;
+    //t -= 1;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/phobos/imports2.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,6 @@
+module imports2;
+import std.stdio;
+
+void main() {
+    writefln("Hello world!"[]);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/phobos/stdiotest.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,28 @@
+module stdiotest;
+
+import std.stdio;
+
+T typed(T)(T x)
+{
+    return x;
+}
+
+void main()
+{
+    /*char[] str = "hello";
+    writefln(str);
+
+    writefln("hello world");*/
+
+    char[] fmt = "%s";
+    writefln(2.0f);
+
+    /*{writefln(typed!(byte)(1));}
+    {writefln(typed!(short)(2));}
+    {writefln(typed!(int)(3));}
+    {writefln(typed!(long)(-4));}
+    {writefln(typed!(ulong)(5));}
+    {writefln("%f", typed!(float)(6));}
+    {writefln("%f", typed!(double)(7));}
+    {writefln("%f", typed!(real)(8));}*/
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/phobos/stdiotest2.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,22 @@
+module stdiotest2;
+import std.stdio;
+void main()
+{
+    int[4] v = [1,2,3,4];
+    {
+        writefln("%s", v);
+        {
+            int[] dv = v;
+            {writefln("%s", dv);}
+        }
+    }
+
+    {
+        writefln(v);
+        {
+            //int[] dv = v;
+            //{writefln(dv);}
+        }
+    }
+    //writefln(1,2,3,4.56,"hello",v);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/mini/phobos/strings2.d	Wed Aug 13 15:43:34 2008 +0200
@@ -0,0 +1,15 @@
+module strings2;
+
+import std.string;
+import std.stdio;
+
+void main()
+{
+    int i = 32;
+    auto str = format(i);
+    writefln(str);
+
+    long l = 123123;
+    str = format(l);
+    writefln(str);
+}
--- a/tests/mini/stdiotest.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-module stdiotest;
-
-import std.stdio;
-
-T typed(T)(T x)
-{
-    return x;
-}
-
-void main()
-{
-    /*char[] str = "hello";
-    writefln(str);
-
-    writefln("hello world");*/
-
-    char[] fmt = "%s";
-    writefln(2.0f);
-
-    /*{writefln(typed!(byte)(1));}
-    {writefln(typed!(short)(2));}
-    {writefln(typed!(int)(3));}
-    {writefln(typed!(long)(-4));}
-    {writefln(typed!(ulong)(5));}
-    {writefln("%f", typed!(float)(6));}
-    {writefln("%f", typed!(double)(7));}
-    {writefln("%f", typed!(real)(8));}*/
-}
--- a/tests/mini/stdiotest2.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,22 +0,0 @@
-module stdiotest2;
-import std.stdio;
-void main()
-{
-    int[4] v = [1,2,3,4];
-    {
-        writefln("%s", v);
-        {
-            int[] dv = v;
-            {writefln("%s", dv);}
-        }
-    }
-
-    {
-        writefln(v);
-        {
-            //int[] dv = v;
-            //{writefln(dv);}
-        }
-    }
-    //writefln(1,2,3,4.56,"hello",v);
-}
--- a/tests/mini/strings2.d	Tue Aug 12 19:50:41 2008 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,15 +0,0 @@
-module strings2;
-
-import std.string;
-import std.stdio;
-
-void main()
-{
-    int i = 32;
-    auto str = format(i);
-    writefln(str);
-
-    long l = 123123;
-    str = format(l);
-    writefln(str);
-}