* PIC codegen for X86/Linux has been implemented
* PIC-aware internal structures in X86 Codegen have been refactored
* Visibility (default/weak) has been added
* Docs fixes (external weak linkage, visibility, formatting)


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@33136 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Bytecode/Reader/Analyzer.cpp b/lib/Bytecode/Reader/Analyzer.cpp
index 899a534..465e3b0 100644
--- a/lib/Bytecode/Reader/Analyzer.cpp
+++ b/lib/Bytecode/Reader/Analyzer.cpp
@@ -162,6 +162,7 @@
     const Type* ElemType,
     bool isConstant,
     GlobalValue::LinkageTypes Linkage,
+    GlobalValue::VisibilityTypes Visibility,
     unsigned SlotNum,
     unsigned initSlot
   ) {
@@ -169,7 +170,9 @@
       *os << "      GV: "
           << ( initSlot == 0 ? "Uni" : "I" ) << "nitialized, "
           << ( isConstant? "Constant, " : "Variable, ")
-          << " Linkage=" << Linkage << " Type=";
+          << " Linkage=" << Linkage
+          << " Visibility="<< Visibility
+          << " Type=";
       WriteTypeSymbolic(*os, ElemType, M);
       *os << " Slot=" << SlotNum << " InitSlot=" << initSlot
           << "\n";
@@ -206,6 +209,7 @@
       *os << "      Function Decl: ";
       WriteTypeSymbolic(*os,Func->getType(),M);
       *os <<", Linkage=" << Func->getLinkage();
+      *os <<", Visibility=" << Func->getVisibility();
       *os << "\n";
     }
   }
@@ -311,6 +315,7 @@
     if (os) {
       *os << "    BLOCK: Function {\n"
           << "      Linkage: " << Func->getLinkage() << "\n"
+          << "      Visibility: " << Func->getVisibility() << "\n"
           << "      Type: ";
       WriteTypeSymbolic(*os,Func->getType(),M);
       *os << "\n";
diff --git a/lib/Bytecode/Reader/Reader.cpp b/lib/Bytecode/Reader/Reader.cpp
index ce3826c..b91604a 100644
--- a/lib/Bytecode/Reader/Reader.cpp
+++ b/lib/Bytecode/Reader/Reader.cpp
@@ -1628,9 +1628,12 @@
 
   unsigned FuncSize = BlockEnd - At;
   GlobalValue::LinkageTypes Linkage = GlobalValue::ExternalLinkage;
+  GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
 
-  unsigned LinkageType = read_vbr_uint();
-  switch (LinkageType) {
+  unsigned rWord = read_vbr_uint();
+  unsigned LinkageID =  rWord & 65535;
+  unsigned VisibilityID = rWord >> 16;
+  switch (LinkageID) {
   case 0: Linkage = GlobalValue::ExternalLinkage; break;
   case 1: Linkage = GlobalValue::WeakLinkage; break;
   case 2: Linkage = GlobalValue::AppendingLinkage; break;
@@ -1644,8 +1647,17 @@
     Linkage = GlobalValue::InternalLinkage;
     break;
   }
+  switch (VisibilityID) {
+  case 0: Visibility = GlobalValue::DefaultVisibility; break;
+  case 1: Visibility = GlobalValue::HiddenVisibility; break;
+  default:
+   error("Unknown visibility type: " + utostr(VisibilityID));
+   Visibility = GlobalValue::DefaultVisibility;
+   break;
+  }
 
   F->setLinkage(Linkage);
+  F->setVisibility(Visibility);
   if (Handler) Handler->handleFunctionBegin(F,FuncSize);
 
   // Keep track of how many basic blocks we have read in...
@@ -1844,6 +1856,7 @@
     // Linkage, bit4+ = slot#
     unsigned SlotNo = VarType >> 5;
     unsigned LinkageID = (VarType >> 2) & 7;
+    unsigned VisibilityID = 0;
     bool isConstant = VarType & 1;
     bool hasInitializer = (VarType & 2) != 0;
     unsigned Alignment = 0;
@@ -1853,10 +1866,12 @@
     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.
+      // linkage, bit 4-8 = alignment (log2), bit 9 = has section,
+      // bits 10-12 = visibility, bits 13+ = future use.
       hasInitializer = ExtWord & 1;
       LinkageID = (ExtWord >> 1) & 7;
       Alignment = (1 << ((ExtWord >> 4) & 31)) >> 1;
+      VisibilityID = (ExtWord >> 10) & 7;
       
       if (ExtWord & (1 << 9))  // Has a section ID.
         GlobalSectionID = read_vbr_uint();
@@ -1877,7 +1892,16 @@
       Linkage = GlobalValue::InternalLinkage;
       break;
     }
-
+    GlobalValue::VisibilityTypes Visibility;
+    switch (VisibilityID) {
+    case 0: Visibility = GlobalValue::DefaultVisibility; break;
+    case 1: Visibility = GlobalValue::HiddenVisibility; break;
+    default:
+      error("Unknown visibility type: " + utostr(VisibilityID));
+      Visibility = GlobalValue::DefaultVisibility;
+      break;
+    }
+    
     const Type *Ty = getType(SlotNo);
     if (!Ty)
       error("Global has no type! SlotNo=" + utostr(SlotNo));
@@ -1891,6 +1915,7 @@
     GlobalVariable *GV = new GlobalVariable(ElTy, isConstant, Linkage,
                                             0, "", TheModule);
     GV->setAlignment(Alignment);
+    GV->setVisibility(Visibility);
     insertValue(GV, SlotNo, ModuleValues);
 
     if (GlobalSectionID != 0)
@@ -1904,7 +1929,8 @@
 
     // Notify handler about the global value.
     if (Handler)
-      Handler->handleGlobalVariable(ElTy, isConstant, Linkage, SlotNo,initSlot);
+      Handler->handleGlobalVariable(ElTy, isConstant, Linkage, Visibility,
+                                    SlotNo, initSlot);
 
     // Get next item
     VarType = read_vbr_uint();
diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp
index c7003cd..10a151d 100644
--- a/lib/Bytecode/Writer/Writer.cpp
+++ b/lib/Bytecode/Writer/Writer.cpp
@@ -960,6 +960,14 @@
   }
 }
 
+static unsigned getEncodedVisibility(const GlobalValue *GV) {
+  switch (GV->getVisibility()) {
+  default: assert(0 && "Invalid visibility!");
+  case GlobalValue::DefaultVisibility: return 0;
+  case GlobalValue::HiddenVisibility:  return 1;
+  }
+}
+
 void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
   BytecodeBlock ModuleInfoBlock(BytecodeFormat::ModuleGlobalInfoBlockID, *this);
 
@@ -979,7 +987,9 @@
     
     // Fields: bit0 = isConstant, bit1 = hasInitializer, bit2-4=Linkage,
     // bit5+ = Slot # for type.
-    bool HasExtensionWord = (I->getAlignment() != 0) || I->hasSection();
+    bool HasExtensionWord = (I->getAlignment() != 0) ||
+                            I->hasSection() ||
+      (I->getVisibility() != GlobalValue::DefaultVisibility);
     
     // If we need to use the extension byte, set linkage=3(internal) and
     // initializer = 0 (impossible!).
@@ -993,12 +1003,13 @@
       output_vbr(oSlot);
       
       // The extension word has this format: bit 0 = has initializer, bit 1-3 =
-      // linkage, bit 4-8 = alignment (log2), bit 9 = has SectionID, 
-      // bits 10+ = future use.
+      // linkage, bit 4-8 = alignment (log2), bit 9 = has SectionID,
+      // bits 10-12 = visibility, bits 13+ = future use.
       unsigned ExtWord = (unsigned)I->hasInitializer() |
                          (getEncodedLinkage(I) << 1) |
                          ((Log2_32(I->getAlignment())+1) << 4) |
-                         ((unsigned)I->hasSection() << 9);
+                         ((unsigned)I->hasSection() << 9) |
+                         (getEncodedVisibility(I) << 10);
       output_vbr(ExtWord);
       if (I->hasSection()) {
         // Give section names unique ID's.
@@ -1102,7 +1113,8 @@
   if (F->isExternal()) return;
 
   BytecodeBlock FunctionBlock(BytecodeFormat::FunctionBlockID, *this);
-  output_vbr(getEncodedLinkage(F));
+  unsigned rWord = (getEncodedVisibility(F) << 16) | getEncodedLinkage(F);
+  output_vbr(rWord);
 
   // Get slot information about the function...
   Table.incorporateFunction(F);