[pkg-d-commits] [ldc] 48/149: Handle static arrays rewritten to slices in a more generic fashion

Matthias Klumpp mak at moszumanska.debian.org
Sun Apr 23 22:36:56 UTC 2017


This is an automated email from the git hooks/post-receive script.

mak pushed a commit to annotated tag v1.2.0
in repository ldc.

commit 5fcfd965604f123cda360bb93ed3767c2a3cca10
Author: Martin <noone at nowhere.com>
Date:   Sat Jan 21 19:38:53 2017 +0100

    Handle static arrays rewritten to slices in a more generic fashion
    
    ... if they are rewritten as left-hand-side of assignments.
---
 gen/toir.cpp | 57 +++++++++++++++++++++++++++++++++------------------------
 1 file changed, 33 insertions(+), 24 deletions(-)

diff --git a/gen/toir.cpp b/gen/toir.cpp
index 8c08062..988a028 100644
--- a/gen/toir.cpp
+++ b/gen/toir.cpp
@@ -28,6 +28,7 @@
 #include "gen/optimizer.h"
 #include "gen/pragma.h"
 #include "gen/runtime.h"
+#include "gen/scope_exit.h"
 #include "gen/structs.h"
 #include "gen/tollvm.h"
 #include "gen/typinf.h"
@@ -486,31 +487,38 @@ public:
       }
     }
 
+    // The front-end sometimes rewrites a static-array-lhs to a slice, e.g.,
+    // when initializing a static array with an array literal.
+    // Use the static array as lhs in that case.
+    DValue *rewrittenLhsStaticArray = nullptr;
     if (e->e1->op == TOKslice) {
-      // Check if this is an initialization of a static array with an array
-      // literal that the frontend has foolishly rewritten into an
-      // assignment of a dynamic array literal to a slice.
-      Logger::println("performing static array literal assignment");
-      SliceExp *const se = static_cast<SliceExp *>(e->e1);
-      Type *const t2 = e->e2->type->toBasetype();
-      Type *const ta = se->e1->type->toBasetype();
-
-      if (se->lwr == nullptr && ta->ty == Tsarray &&
-          e->e2->op == TOKarrayliteral &&
-          e->op == TOKconstruct && // DMD Bugzilla 11238: avoid aliasing issue
-          t2->nextOf()->mutableOf()->implicitConvTo(ta->nextOf())) {
-        ArrayLiteralExp *const ale = static_cast<ArrayLiteralExp *>(e->e2);
-        initializeArrayLiteral(p, ale, DtoLVal(se->e1));
-        result = toElem(e->e1);
-        return;
+      SliceExp *se = static_cast<SliceExp *>(e->e1);
+      Type *sliceeBaseType = se->e1->type->toBasetype();
+      if (se->lwr == nullptr && sliceeBaseType->ty == Tsarray &&
+          se->type->toBasetype()->nextOf() == sliceeBaseType->nextOf())
+        rewrittenLhsStaticArray = toElem(se->e1, true);
+    }
+
+    DValue *const lhs = (rewrittenLhsStaticArray ? rewrittenLhsStaticArray
+                                                 : toElem(e->e1, true));
+
+    // Set the result of the AssignExp to the lhs.
+    // Defer this to the end of this function, so that static arrays are
+    // rewritten (converted to a slice) after the assignment, primarily for a
+    // more intuitive IR order.
+    SCOPE_EXIT {
+      if (rewrittenLhsStaticArray) {
+        result =
+            new DSliceValue(e->e1->type, DtoArrayLen(rewrittenLhsStaticArray),
+                            DtoArrayPtr(rewrittenLhsStaticArray));
+      } else {
+        result = lhs;
       }
-    }
-
-    result = toElem(e->e1, true);
+    };
 
     // try to construct the lhs in-place
-    if (result->isLVal() && e->op == TOKconstruct &&
-        toInPlaceConstruction(result->isLVal(), e->e2)) {
+    if (lhs->isLVal() && e->op == TOKconstruct &&
+        toInPlaceConstruction(lhs->isLVal(), e->e2)) {
       return;
     }
 
@@ -519,10 +527,11 @@ public:
     if (e->e1->type->toBasetype()->ty == Tstruct && e->e2->op == TOKint64) {
       Logger::println("performing aggregate zero initialization");
       assert(e->e2->toInteger() == 0);
-      DtoMemSetZero(DtoLVal(result));
+      LLValue *lval = DtoLVal(lhs);
+      DtoMemSetZero(lval);
       TypeStruct *ts = static_cast<TypeStruct *>(e->e1->type);
       if (ts->sym->isNested() && ts->sym->vthis)
-        DtoResolveNestedContext(e->loc, ts->sym, DtoLVal(result));
+        DtoResolveNestedContext(e->loc, ts->sym, lval);
       return;
     }
 
@@ -540,7 +549,7 @@ public:
 
     Logger::println("performing normal assignment (rhs has lvalue elems = %d)",
                     lvalueElem);
-    DtoAssign(e->loc, result, r, e->op, !lvalueElem);
+    DtoAssign(e->loc, lhs, r, e->op, !lvalueElem);
   }
 
   //////////////////////////////////////////////////////////////////////////////

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-d/ldc.git



More information about the pkg-d-commits mailing list