changeset 1643:8f121883bce8

Apply patch from klickverbot. This is his 'proper fix' patch for bug #395.
author Kelly Wilson <wilsonk cpsc.ucalgary.ca>
date Mon, 08 Mar 2010 23:37:40 -0700
parents f49cb50c6064
children 9176437d98be
files gen/aa.cpp gen/arrays.cpp gen/arrays.h gen/llvmhelpers.cpp gen/llvmhelpers.h gen/toir.cpp
diffstat 6 files changed, 45 insertions(+), 51 deletions(-) [+]
line wrap: on
line diff
--- a/gen/aa.cpp	Mon Mar 08 23:10:26 2010 -0700
+++ b/gen/aa.cpp	Mon Mar 08 23:37:40 2010 -0700
@@ -14,41 +14,6 @@
 #include "gen/dvalue.h"
 #include "ir/irmodule.h"
 
-// makes sure the key value lives in memory so it can be passed to the runtime functions without problems
-// returns the pointer
-static LLValue* to_pkey(Loc& loc, DValue* key)
-{
-    Type* keytype = key->getType();
-    bool needmem = !DtoIsPassedByRef(keytype);
-    LLValue* pkey;
-    if (key->isIm()) {
-        pkey = key->getRVal();
-    }
-    else if (DVarValue* var = key->isVar()) {
-        pkey = key->getLVal();
-        needmem = false;
-    }
-    else if (key->isConst()) {
-        needmem = true;
-        pkey = key->getRVal();
-    }
-    else {
-        LLValue* tmp = DtoAlloca(keytype, "aatmpkeystorage");
-        DVarValue var(keytype, tmp);
-        DtoAssign(loc, &var, key);
-        return tmp;
-    }
-
-    // give memory
-    if (needmem) {
-        LLValue* tmp = DtoAlloca(keytype, "aatmpkeystorage");
-        DtoStore(pkey, tmp);
-        pkey = tmp;
-    }
-
-    return pkey;
-}
-
 // returns the keytype typeinfo
 static LLValue* to_keyti(DValue* key)
 {
@@ -79,7 +44,7 @@
     keyti = DtoBitCast(keyti, funcTy->getParamType(1));
 
     // pkey param
-    LLValue* pkey = to_pkey(loc, key);
+    LLValue* pkey = makeLValue(loc, key);
     pkey = DtoBitCast(pkey, funcTy->getParamType(lvalue ? 3 : 2));
 
     // call runtime
@@ -164,7 +129,7 @@
     keyti = DtoBitCast(keyti, funcTy->getParamType(1));
 
     // pkey param
-    LLValue* pkey = to_pkey(loc, key);
+    LLValue* pkey = makeLValue(loc, key);
     pkey = DtoBitCast(pkey, funcTy->getParamType(2));
 
     // call runtime
@@ -206,7 +171,7 @@
     keyti = DtoBitCast(keyti, funcTy->getParamType(1));
 
     // pkey param
-    LLValue* pkey = to_pkey(loc, key);
+    LLValue* pkey = makeLValue(loc, key);
     pkey = DtoBitCast(pkey, funcTy->getParamType(2));
 
     // build arg vector
--- a/gen/arrays.cpp	Mon Mar 08 23:10:26 2010 -0700
+++ b/gen/arrays.cpp	Mon Mar 08 23:37:40 2010 -0700
@@ -532,23 +532,14 @@
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////
-void DtoCatAssignElement(Type* arrayType, DValue* array, Expression* exp)
+void DtoCatAssignElement(Loc& loc, Type* arrayType, DValue* array, Expression* exp)
 {
     Logger::println("DtoCatAssignElement");
     LOG_SCOPE;
 
     assert(array);
 
-    DValue *expVal = exp->toElem(gIR);
-    LLValue *valueToAppend;
-    if (expVal->isLVal())
-        valueToAppend = expVal->getLVal();
-    else {
-        valueToAppend = DtoAlloca(expVal->getType(), ".appendingElementOnStack");
-        DVarValue lval(expVal->getType(), valueToAppend);
-        Loc loc;
-        DtoAssign(loc, &lval, expVal);
-    }
+    LLValue *valueToAppend = makeLValue(loc, exp->toElem(gIR));
 
     LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_arrayappendcT");
     LLSmallVector<LLValue*,3> args;
--- a/gen/arrays.h	Mon Mar 08 23:10:26 2010 -0700
+++ b/gen/arrays.h	Mon Mar 08 23:37:40 2010 -0700
@@ -24,7 +24,7 @@
 DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size_t ndims, bool defaultInit=true);
 DSliceValue* DtoResizeDynArray(Type* arrayType, DValue* array, DValue* newdim);
 
-void DtoCatAssignElement(Type* type, DValue* arr, Expression* exp);
+void DtoCatAssignElement(Loc& loc, Type* type, DValue* arr, Expression* exp);
 DSliceValue* DtoCatAssignArray(DValue* arr, Expression* exp);
 DSliceValue* DtoCatArrays(Type* type, Expression* e1, Expression* e2);
 DSliceValue* DtoCatArrayElement(Type* type, Expression* exp1, Expression* exp2);
--- a/gen/llvmhelpers.cpp	Mon Mar 08 23:10:26 2010 -0700
+++ b/gen/llvmhelpers.cpp	Mon Mar 08 23:37:40 2010 -0700
@@ -1572,3 +1572,36 @@
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////
+
+LLValue* makeLValue(Loc& loc, DValue* value)
+{
+    Type* valueType = value->getType();
+    bool needsMemory;
+    LLValue* valuePointer;
+    if (value->isIm()) {
+        valuePointer = value->getRVal();
+        needsMemory = !DtoIsPassedByRef(valueType);
+    }
+    else if (DVarValue* var = value->isVar()) {
+        valuePointer = value->getLVal();
+        needsMemory = false;
+    }
+    else if (value->isConst()) {
+        valuePointer = value->getRVal();
+        needsMemory = true;
+    }
+    else {
+        valuePointer = DtoAlloca(valueType, ".makelvaluetmp");
+        DVarValue var(valueType, valuePointer);
+        DtoAssign(loc, &var, value);
+        needsMemory = false;
+    }
+
+    if (needsMemory) {
+        LLValue* tmp = DtoAlloca(valueType, ".makelvaluetmp");
+        DtoStore(valuePointer, tmp);
+        valuePointer = tmp;
+    }
+
+    return valuePointer;
+}
--- a/gen/llvmhelpers.h	Mon Mar 08 23:10:26 2010 -0700
+++ b/gen/llvmhelpers.h	Mon Mar 08 23:37:40 2010 -0700
@@ -146,6 +146,11 @@
 /// Returns the offset rounded up to the closest safely aligned offset.
 size_t realignOffset(size_t offset, Type* type);
 
+/// Returns the llvm::Value of the passed DValue, making sure that it is an
+/// lvalue (has a memory address), so it can be passed to the D runtime
+/// functions without problems.
+LLValue* makeLValue(Loc& loc, DValue* value);
+
 ////////////////////////////////////////////
 // gen/tocall.cpp stuff below
 ////////////////////////////////////////////
--- a/gen/toir.cpp	Mon Mar 08 23:10:26 2010 -0700
+++ b/gen/toir.cpp	Mon Mar 08 23:37:40 2010 -0700
@@ -2234,7 +2234,7 @@
     Type* e2type = e2->type->toBasetype();
 
     if (e2type == elemtype) {
-        DtoCatAssignElement(e1type, l, e2);
+        DtoCatAssignElement(loc, e1type, l, e2);
     }
     else if (e1type == e2type) {
         DSliceValue* slice = DtoCatAssignArray(l,e2);