changeset 30:881158a93592 trunk

[svn r34] * Fixed passing a struct literal as function argument
author lindquist
date Thu, 04 Oct 2007 14:15:54 +0200
parents 253a5fc4033a
children 2841234d2aea
files gen/toir.c test/structs6.d test/unittest1.d
diffstat 3 files changed, 56 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/gen/toir.c	Thu Oct 04 13:45:22 2007 +0200
+++ b/gen/toir.c	Thu Oct 04 14:15:54 2007 +0200
@@ -933,6 +933,11 @@
     {
         Expression* argexp = (Expression*)arguments->data[i];
         elem* arg = argexp->toElem(p);
+        if (arg->inplace) {
+            assert(arg->mem);
+            llargs[j] = arg->mem;
+            continue;
+        }
 
         Argument* fnarg = Argument::getNth(tf->parameters, i);
 
@@ -1385,42 +1390,46 @@
     LOG_SCOPE;
     elem* e = new elem;
 
-    // if there is no lval, this must be a static initializer for a global. correct?
+    llvm::Value* sptr = 0;
+
+    // if there is no lval, this is probably a temporary struct literal. correct?
     if (p->lvals.empty())
     {
-        // TODO
-        assert(0);
+        sptr = new llvm::AllocaInst(LLVM_DtoType(type),"tmpstructliteral",p->topallocapoint());
+        e->mem = sptr;
+        e->type = elem::VAR;
     }
-    // otherwise write directly in the lvalue
+    // already has memory
     else
     {
-        llvm::Value* sptr = p->toplval();
-        assert(sptr);
+        sptr = p->toplval();
+    }
 
-        llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
+    assert(sptr);
 
-        unsigned n = elements->dim;
-        for (unsigned i=0; i<n; ++i)
-        {
-            llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false);
-            llvm::Value* arrptr = LLVM_DtoGEP(sptr,zero,offset,"tmp",p->scopebb());
+    llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
+
+    unsigned n = elements->dim;
+    for (unsigned i=0; i<n; ++i)
+    {
+        llvm::Value* offset = llvm::ConstantInt::get(llvm::Type::Int32Ty, i, false);
+        llvm::Value* arrptr = LLVM_DtoGEP(sptr,zero,offset,"tmp",p->scopebb());
 
-            Expression* vx = (Expression*)elements->data[i];
-            if (vx != 0) {
-                elem* ve = vx->toElem(p);
-                llvm::Value* val = ve->getValue();
-                Logger::cout() << *val << " | " << *arrptr << '\n';
-                if (vx->type->ty == Tstruct) {
-                    TypeStruct* ts = (TypeStruct*)vx->type;
-                    LLVM_DtoStructCopy(ts,arrptr,val);
-                }
-                else
-                    new llvm::StoreInst(val, arrptr, p->scopebb());
-                delete ve;
+        Expression* vx = (Expression*)elements->data[i];
+        if (vx != 0) {
+            elem* ve = vx->toElem(p);
+            llvm::Value* val = ve->getValue();
+            Logger::cout() << *val << " | " << *arrptr << '\n';
+            if (vx->type->ty == Tstruct) {
+                TypeStruct* ts = (TypeStruct*)vx->type;
+                LLVM_DtoStructCopy(ts,arrptr,val);
             }
-            else {
-                assert(0);
-            }
+            else
+                new llvm::StoreInst(val, arrptr, p->scopebb());
+            delete ve;
+        }
+        else {
+            assert(0);
         }
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/structs6.d	Thu Oct 04 14:15:54 2007 +0200
@@ -0,0 +1,15 @@
+module structs6;
+
+struct S
+{
+    float f;
+}
+
+void func(S s)
+{
+}
+
+void main()
+{
+    func(S());
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/unittest1.d	Thu Oct 04 14:15:54 2007 +0200
@@ -0,0 +1,5 @@
+module unittest1;
+
+unittest
+{
+}