Read/write global variable alignments if present
authorChris Lattner <sabre@nondot.org>
Sun, 6 Nov 2005 07:11:04 +0000 (07:11 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 6 Nov 2005 07:11:04 +0000 (07:11 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@24216 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Bytecode/Reader/Reader.cpp
lib/Bytecode/Writer/Writer.cpp

index 917727098e764d72c14e40a1ce71f65d37d8e607..26bd68c344c7021392a1eb6fd3d3d365e6fa686e 100644 (file)
@@ -1903,6 +1903,17 @@ void BytecodeReader::ParseModuleGlobalInfo() {
     bool isConstant = VarType & 1;
     bool hasInitializer = VarType & 2;
     GlobalValue::LinkageTypes Linkage;
+    unsigned Alignment = 0;
+    
+    // An extension word is present when linkage = 3 (internal) and hasinit = 0.
+    if (LinkageID == 3 && !hasInitializer) {
+      unsigned ExtWord = read_vbr_uint();
+      // The extension word has this format: bit 0 = has initializer, bit 1-3 =
+      // linkage, bit 4-8 = alignment (log2), bits 10+ = future use.
+      hasInitializer = ExtWord & 1;
+      LinkageID = (ExtWord >> 1) & 7;
+      Alignment = (1 << ((ExtWord >> 4) & 31)) >> 1;
+    }
 
     switch (LinkageID) {
     case 0: Linkage = GlobalValue::ExternalLinkage;  break;
@@ -1930,6 +1941,7 @@ void BytecodeReader::ParseModuleGlobalInfo() {
     // Create the global variable...
     GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage,
                                             0, "", TheModule);
+    GV->setAlignment(Alignment);
     insertValue(GV, SlotNo, ModuleValues);
 
     unsigned initSlot = 0;
index d314231828029ddec74bff53e0afad2ae76a5cde..b66ab55792b4959a20a1f956a5a33c917d6e253a 100644 (file)
@@ -416,7 +416,6 @@ void BytecodeWriter::outputConstantStrings() {
 //===----------------------------------------------------------------------===//
 //===                           Instruction Output                         ===//
 //===----------------------------------------------------------------------===//
-typedef unsigned char uchar;
 
 // outputInstructionFormat0 - Output those weird instructions that have a large
 // number of operands or have large operands themselves.
@@ -925,15 +924,35 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
 
   // Output the types for the global variables in the module...
   for (Module::const_global_iterator I = M->global_begin(),
-         End = M->global_end(); I != End;++I) {
+         End = M->global_end(); I != End; ++I) {
     int Slot = Table.getSlot(I->getType());
     assert(Slot != -1 && "Module global vars is broken!");
 
+    assert((I->hasInitializer() || !I->hasInternalLinkage()) &&
+           "Global must have an initializer or have external linkage!");
+    
     // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
-    // bit5+ = Slot # for type
-    unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) |
-                     (I->hasInitializer() << 1) | (unsigned)I->isConstant();
-    output_vbr(oSlot);
+    // bit5+ = Slot # for type.
+    bool HasExtensionWord = I->getAlignment() != 0;
+    
+    // If we need to use the extension byte, set linkage=3(internal) and
+    // initializer = 0 (impossible!).
+    if (!HasExtensionWord) {
+      unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) |
+                        (I->hasInitializer() << 1) | (unsigned)I->isConstant();
+      output_vbr(oSlot);
+    } else {
+      unsigned oSlot = ((unsigned)Slot << 5) | (3 << 2) |
+                        (0 << 1) | (unsigned)I->isConstant();
+      output_vbr(oSlot);
+      
+      // The extension word has this format: bit 0 = has initializer, bit 1-3 =
+      // linkage, bit 4-8 = alignment (log2), bits 10+ = future use.
+      unsigned ExtWord = I->hasInitializer() | (getEncodedLinkage(I) << 1) |
+                         (Log2_32(I->getAlignment())+1) << 4;
+      output_vbr(ExtWord);
+      
+    }
 
     // If we have an initializer, output it now.
     if (I->hasInitializer()) {