Fix global variable linkage for LLVM

The LLVM backend, like the C backend, has supported compilation-unit local
functions (ie 'static' for any not-exported proc). This patch adds that
functionality to global variables, which previously were always code generated
with external linkage.

Verified with CHPL_LLVM=none make check; verified symbols had appropriate types
with 'nm' for --llvm; verified Hello World works with --llvm. Full local/LLVM
testing ongoing.

(Note that currently 'export var x:int' fails with a syntax error;
 the way I tested exporting a variable was with pragma "export".)
Index: compiler/AST/symbol.cpp
===================================================================
--- compiler/AST/symbol.cpp	(revision 23009)
+++ compiler/AST/symbol.cpp	(working copy)
@@ -656,12 +656,27 @@
         }
       }
     } else {
-      // Anything that we don't have already
-      //
-      llvm::GlobalVariable *gVar = llvm::cast<llvm::GlobalVariable>(
-          info->module->getOrInsertGlobal(cname, type->codegen().type));
-      gVar->setInitializer(llvm::Constant::getNullValue(type->codegen().type));
+      bool existing;
 
+      existing = (info->module->getNamedValue(cname) != NULL);
+
+      if( existing )
+        INT_FATAL(this, "Redefinition of a global variable %s", cname);
+
+      // Now, create a global variable with appropriate linkage.
+      llvm::Type* llTy = type->codegen().type;
+      INT_ASSERT(llTy);
+
+      llvm::GlobalVariable *gVar =
+        new llvm::GlobalVariable(
+            *info->module,
+            llTy,
+            false, /* is constant */
+            hasFlag(FLAG_EXPORT) ? llvm::GlobalVariable::ExternalLinkage
+                                 : llvm::GlobalVariable::InternalLinkage,
+            llvm::Constant::getNullValue(llTy), /* initializer, */
+            cname);
+
       info->lvt->addGlobalValue(cname, gVar, GEN_PTR, ! is_signed(type) );
     }
 #endif
------------------------------------------------------------------------------
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers

Reply via email to