Test generation of global initializers in Subzero bitcode reader.

Adds workaround that uses IceConverter's convertGlobals to
generate global initializers. This should complete the initial
implementation of Subzero's bitcode reader.

BUG= https://code.google.com/p/nativeclient/issues/detail?id=3892
R=stichnot@chromium.org

Review URL: https://codereview.chromium.org/587893003
diff --git a/src/PNaClTranslator.cpp b/src/PNaClTranslator.cpp
index e87e0aa..5b7c0d2 100644
--- a/src/PNaClTranslator.cpp
+++ b/src/PNaClTranslator.cpp
@@ -351,7 +351,9 @@
         Context(EnclosingParser->Context) {}
 
   // Gets the translator associated with the bitcode parser.
-  Ice::Translator &getTranslator() { return Context->getTranslator(); }
+  Ice::Translator &getTranslator() const { return Context->getTranslator(); }
+
+  const Ice::ClFlags &getFlags() const { return getTranslator().getFlags(); }
 
   // Generates an error Message with the bit address prefixed to it.
   virtual bool Error(const std::string &Message) LLVM_OVERRIDE {
@@ -1376,7 +1378,7 @@
 };
 
 FunctionParser::~FunctionParser() {
-  if (getTranslator().getFlags().SubzeroTimingEnabled) {
+  if (getFlags().SubzeroTimingEnabled) {
     errs() << "[Subzero timing] Convert function " << Func->getFunctionName()
            << ": " << TConvert.getElapsedSec() << " sec\n";
   }
@@ -2216,16 +2218,36 @@
 class ModuleParser : public BlockParserBaseClass {
 public:
   ModuleParser(unsigned BlockID, TopLevelParser *Context)
-      : BlockParserBaseClass(BlockID, Context), FoundFirstFunctionBlock(false) {
-  }
+      : BlockParserBaseClass(BlockID, Context),
+        GlobalAddressNamesAndInitializersInstalled(false) {}
 
   virtual ~ModuleParser() LLVM_OVERRIDE {}
 
 private:
-  // True if we have parsed a function block.
-  bool FoundFirstFunctionBlock;
+  // True if we have already instaledl names for unnamed global addresses,
+  // and generated global constant initializers.
+  bool GlobalAddressNamesAndInitializersInstalled;
+
+  // Temporary hack to generate names for unnamed global addresses,
+  // and generate global constant initializers. May be called multiple
+  // times. Only the first call will do the installation.
+  // NOTE: Doesn't handle relocations for global constant initializers.
+  void InstallGlobalAddressNamesAndInitializers() {
+    if (!GlobalAddressNamesAndInitializersInstalled) {
+      getTranslator().nameUnnamedGlobalAddresses(Context->getModule());
+      if (!getFlags().DisableGlobals)
+        getTranslator().convertGlobals(Context->getModule());
+      GlobalAddressNamesAndInitializersInstalled = true;
+    }
+  }
+
   virtual bool ParseBlock(unsigned BlockID) LLVM_OVERRIDE;
 
+  virtual void ExitBlock() LLVM_OVERRIDE {
+    InstallGlobalAddressNamesAndInitializers();
+    getTranslator().emitConstants();
+  }
+
   virtual void ProcessRecord() LLVM_OVERRIDE;
 };
 
@@ -2282,10 +2304,7 @@
     return Parser.ParseThisBlock();
   }
   case naclbitc::FUNCTION_BLOCK_ID: {
-    if (!FoundFirstFunctionBlock) {
-      getTranslator().nameUnnamedGlobalAddresses(Context->getModule());
-      FoundFirstFunctionBlock = true;
-    }
+    InstallGlobalAddressNamesAndInitializers();
     FunctionParser Parser(BlockID, this);
     return Parser.ParseThisBlock();
   }