[pkg-d-commits] [ldc] 01/149: Consider pointer literals as constant in isConstLiteral.

Matthias Klumpp mak at moszumanska.debian.org
Sun Apr 23 22:36:52 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 3eab44ae8563afbe4acbead5fb1770858e3d9198
Author: Johan Engelen <jbc.engelen at gmail.com>
Date:   Sun Nov 13 20:34:53 2016 +0100

    Consider pointer literals as constant in isConstLiteral.
---
 gen/arrays.cpp                      | 13 +++++++++++++
 tests/codegen/const_struct.d        | 12 ++++++++++++
 tests/codegen/const_struct_export.d | 20 ++++++++++++++++++++
 3 files changed, 45 insertions(+)

diff --git a/gen/arrays.cpp b/gen/arrays.cpp
index da87788..daa0556 100644
--- a/gen/arrays.cpp
+++ b/gen/arrays.cpp
@@ -526,6 +526,19 @@ bool isConstLiteral(Expression *e) {
   case TOKstring:
     return true;
 
+  case TOKsymoff: {
+    // Note: dllimported symbols are not link-time constant.
+    auto soe = static_cast<SymOffExp *>(e);
+    if (VarDeclaration *vd = soe->var->isVarDeclaration()) {
+       return vd->isDataseg() && !vd->isImportedSymbol();
+    }
+    if (FuncDeclaration *fd = soe->var->isFuncDeclaration()) {
+        return !fd->isImportedSymbol();
+    }
+    // Assume the symbol is non-const if we can't prove it is const.
+    return false;
+  } break;
+
   default:
     if (e->isConst() != 1)
       return false;
diff --git a/tests/codegen/const_struct.d b/tests/codegen/const_struct.d
index b73e7d1..4cb6b1e 100644
--- a/tests/codegen/const_struct.d
+++ b/tests/codegen/const_struct.d
@@ -21,6 +21,8 @@ void testNested() {
 // CHECK: @.immutablearray{{.*}} = internal constant [2 x double]
 // CHECK: @.immutablearray{{.*}} = internal constant [2 x { i{{32|64}}, i8* }]
 // CHECK: @.immutablearray{{.*}} = internal constant [1 x %const_struct.S2]
+// CHECK: @.immutablearray{{.*}} = internal constant [2 x i32*] {{.*}}globVar
+// CHECK: @.immutablearray{{.*}} = internal constant [2 x void ()*] {{.*}}Dmain
 
 void main () {
     // Simple types
@@ -36,5 +38,15 @@ void main () {
     // CHECK-SAME: @_D29TypeInfo_yAS12const_struct2C06__initZ
     immutable C0[] aF = [ { new int(42) }, { new int(24) } ];
 
+    // Pointer types
+    static immutable int globVar;
+    immutable auto globalVariables = [ &globVar, &globVar ];
+    immutable auto functionPointers = [ &main, &main ];
+    // Pointer arrays with non-const initializer
+    immutable int localVar;
+    immutable auto locA = [ &localVar, &localVar ];
+    // CHECK: %.gc_mem{{.*}} = call { i{{32|64}}, i8* } @_d_newarrayU
+    // CHECK-SAME: @_D13TypeInfo_yAPi6__initZ
+
     testNested();
 }
diff --git a/tests/codegen/const_struct_export.d b/tests/codegen/const_struct_export.d
new file mode 100644
index 0000000..191fc58
--- /dev/null
+++ b/tests/codegen/const_struct_export.d
@@ -0,0 +1,20 @@
+// RUN: %ldc -c -output-ll -of=%t.ll %s && FileCheck %s < %t.ll
+
+// CHECK:     @.immutablearray{{.*}} = internal constant [2 x void ()*] {{.*}}exportedFunction
+// CHECK-NOT: @.immutablearray{{.*}} [2 x void ()*] {{.*}}importedFunction
+// CHECK:     @.immutablearray{{.*}} = internal constant [2 x i32*] {{.*}}exportedVariable
+// CHECK-NOT: @.immutablearray{{.*}} [2 x i32*] {{.*}}importedVariable
+
+export void exportedFunction() {}
+export void importedFunction();
+export immutable int exportedVariable = 1;
+export immutable int importedVariable;
+
+void foo () {
+    immutable auto exportedFuncs = [ &exportedFunction, &exportedFunction ];
+    immutable auto importedFuncs = [ &importedFunction, &importedFunction ];
+    // CHECK: store void ()* @{{.*}}D19const_struct_export16importedFunctionFZv
+    immutable auto exportedVars = [ &exportedVariable, &exportedVariable ];
+    immutable auto importedVars = [ &importedVariable, &importedVariable ];
+    // CHECK: store i32* @{{.*}}D19const_struct_export16importedVariable
+}

-- 
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