Add check to verify alignment on global variables.

Moves the alignment check method from the function block parser, to the
top-level parser, and then uses it to also check alignment on global
variables.

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

Review URL: https://codereview.chromium.org/1378163002 .
diff --git a/src/PNaClTranslator.cpp b/src/PNaClTranslator.cpp
index ba9eabb..fee1048 100644
--- a/src/PNaClTranslator.cpp
+++ b/src/PNaClTranslator.cpp
@@ -352,6 +352,26 @@
     return std::move(VariableDeclarations);
   }
 
+  // Upper limit of alignment power allowed by LLVM
+  static constexpr uint32_t AlignPowerLimit = 29;
+
+  // Extracts the corresponding Alignment to use, given the AlignPower (i.e.
+  // 2**(AlignPower-1), or 0 if AlignPower == 0). Parser defines the block
+  // context the alignment check appears in, and Prefix defines the context the
+  // alignment appears in.
+  uint32_t extractAlignment(NaClBitcodeParser *Parser, const char *Prefix,
+                            uint32_t AlignPower) {
+    if (AlignPower <= AlignPowerLimit + 1)
+      return (1 << AlignPower) >> 1;
+    std::string Buffer;
+    raw_string_ostream StrBuf(Buffer);
+    StrBuf << Prefix << " alignment greater than 2**" << AlignPowerLimit
+           << ". Found: 2**" << (AlignPower - 1);
+    Parser->Error(StrBuf.str());
+    // Error recover with value that is always acceptable.
+    return 1;
+  }
+
 private:
   // The translator associated with the parser.
   Ice::Translator &Translator;
@@ -1061,10 +1081,12 @@
     // Always build the global variable, even if IR generation is turned off.
     // This is needed because we need a placeholder in the top-level context
     // when no IR is generated.
+    uint32_t Alignment =
+        Context->extractAlignment(this, "Global variable", Values[0]);
     CurGlobalVar = getGlobalVarByID(NextGlobalID);
     if (!isIRGenerationDisabled()) {
       InitializersNeeded = 1;
-      CurGlobalVar->setAlignment((1 << Values[0]) >> 1);
+      CurGlobalVar->setAlignment(Alignment);
       CurGlobalVar->setIsConstant(Values[1] != 0);
     }
     ++NextGlobalID;
@@ -1381,26 +1403,6 @@
   NaClBcIndexSize_t NextLocalInstIndex;
   // True if the last processed instruction was a terminating instruction.
   bool InstIsTerminating = false;
-  // Upper limit of alignment power allowed by LLVM
-  static const uint32_t AlignPowerLimit = 29;
-
-  // Extracts the corresponding Alignment to use, given the AlignPower (i.e.
-  // 2**(AlignPower-1), or 0 if AlignPower == 0). InstName is the name of the
-  // instruction the alignment appears in.
-  void extractAlignment(const char *InstName, uint32_t AlignPower,
-                        uint32_t &Alignment) {
-    if (AlignPower <= AlignPowerLimit + 1) {
-      Alignment = (1 << AlignPower) >> 1;
-      return;
-    }
-    std::string Buffer;
-    raw_string_ostream StrBuf(Buffer);
-    StrBuf << InstName << " alignment greater than 2**" << AlignPowerLimit
-           << ". Found: 2**" << (AlignPower - 1);
-    Error(StrBuf.str());
-    // Error recover with value that is always acceptable.
-    Alignment = 1;
-  }
 
   bool ParseBlock(unsigned BlockID) override;
 
@@ -2592,8 +2594,7 @@
     if (!isValidRecordSize(2, "alloca"))
       return;
     Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex);
-    uint32_t Alignment;
-    extractAlignment("Alloca", Values[1], Alignment);
+    uint32_t Alignment = Context->extractAlignment(this, "Alloca", Values[1]);
     if (isIRGenerationDisabled()) {
       assert(ByteCount == nullptr);
       setNextLocalInstIndex(nullptr);
@@ -2618,8 +2619,7 @@
       return;
     Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex);
     Ice::Type Ty = Context->getSimpleTypeByID(Values[2]);
-    uint32_t Alignment;
-    extractAlignment("Load", Values[1], Alignment);
+    uint32_t Alignment = Context->extractAlignment(this, "Load", Values[1]);
     if (isIRGenerationDisabled()) {
       assert(Address == nullptr);
       setNextLocalInstIndex(nullptr);
@@ -2643,8 +2643,7 @@
       return;
     Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex);
     Ice::Operand *Value = getRelativeOperand(Values[1], BaseIndex);
-    uint32_t Alignment;
-    extractAlignment("Store", Values[2], Alignment);
+    uint32_t Alignment = Context->extractAlignment(this, "Store", Values[2]);
     if (isIRGenerationDisabled()) {
       assert(Address == nullptr && Value == nullptr);
       return;
diff --git a/tests_lit/parse_errs/Inputs/bad-global-alignment.tbc b/tests_lit/parse_errs/Inputs/bad-global-alignment.tbc
new file mode 100644
index 0000000..aa4bfe6
--- /dev/null
+++ b/tests_lit/parse_errs/Inputs/bad-global-alignment.tbc
@@ -0,0 +1,8 @@
+65535,8,2;
+1,1;
+65535,19,2;
+5,1;
+0,31,0;
+2,4;
+65534;
+65534;
diff --git a/tests_lit/parse_errs/bad-global-alignment.test b/tests_lit/parse_errs/bad-global-alignment.test
new file mode 100644
index 0000000..20cab35
--- /dev/null
+++ b/tests_lit/parse_errs/bad-global-alignment.test
@@ -0,0 +1,12 @@
+; Test that we check that alignment on global variables can't be greater
+; than 2**29.
+
+
+; REQUIRES: no_minimal_build
+
+; RUN: not %pnacl_sz -bitcode-as-text %p/Inputs/bad-global-alignment.tbc \
+; RUN:     -bitcode-format=pnacl -notranslate -no-ir-gen -build-on-read 2>&1 \
+; RUN:   | FileCheck %s
+
+; CHECK: Global variable alignment greater than 2**29. Found: 2**30
+