[llvm-commits] CVS: llvm/lib/VMCore/AsmWriter.cpp Globals.cpp Module.cpp Verifier.cpp

Anton Korobeynikov asl at math.spbu.ru
Wed Apr 25 07:28:10 PDT 2007



Changes in directory llvm/lib/VMCore:

AsmWriter.cpp updated: 1.274 -> 1.275
Globals.cpp updated: 1.19 -> 1.20
Module.cpp updated: 1.80 -> 1.81
Verifier.cpp updated: 1.205 -> 1.206
---
Log message:

Implement aliases. This fixes PR1017: http://llvm.org/PR1017  and it's dependent bugs. CFE part 
will follow.


---
Diffs of the changes:  (+150 -11)

 AsmWriter.cpp |   74 ++++++++++++++++++++++++++++++++++++++++++++++++++--------
 Globals.cpp   |   45 ++++++++++++++++++++++++++++++++++-
 Module.cpp    |   23 ++++++++++++++++++
 Verifier.cpp  |   19 ++++++++++++++
 4 files changed, 150 insertions(+), 11 deletions(-)


Index: llvm/lib/VMCore/AsmWriter.cpp
diff -u llvm/lib/VMCore/AsmWriter.cpp:1.274 llvm/lib/VMCore/AsmWriter.cpp:1.275
--- llvm/lib/VMCore/AsmWriter.cpp:1.274	Sun Apr 22 14:24:39 2007
+++ llvm/lib/VMCore/AsmWriter.cpp	Wed Apr 25 09:27:10 2007
@@ -166,6 +166,8 @@
     return new SlotMachine(BB->getParent());
   } else if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)){
     return new SlotMachine(GV->getParent());
+  } else if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)){
+    return new SlotMachine(GA->getParent());    
   } else if (const Function *Func = dyn_cast<Function>(V)) {
     return new SlotMachine(Func);
   }
@@ -683,12 +685,13 @@
     fillTypeNameTable(M, TypeNames);
   }
 
-  inline void write(const Module *M)         { printModule(M);      }
-  inline void write(const GlobalVariable *G) { printGlobal(G);      }
-  inline void write(const Function *F)       { printFunction(F);    }
-  inline void write(const BasicBlock *BB)    { printBasicBlock(BB); }
+  inline void write(const Module *M)         { printModule(M);       }
+  inline void write(const GlobalVariable *G) { printGlobal(G);       }
+  inline void write(const GlobalAlias *G)    { printAlias(G);        }
+  inline void write(const Function *F)       { printFunction(F);     }
+  inline void write(const BasicBlock *BB)    { printBasicBlock(BB);  }
   inline void write(const Instruction *I)    { printInstruction(*I); }
-  inline void write(const Type *Ty)          { printType(Ty);       }
+  inline void write(const Type *Ty)          { printType(Ty);        }
 
   void writeOperand(const Value *Op, bool PrintType);
 
@@ -698,6 +701,7 @@
   void printModule(const Module *M);
   void printTypeSymbolTable(const TypeSymbolTable &ST);
   void printGlobal(const GlobalVariable *GV);
+  void printAlias(const GlobalAlias *GV);
   void printFunction(const Function *F);
   void printArgument(const Argument *FA, uint16_t ParamAttrs);
   void printBasicBlock(const BasicBlock *BB);
@@ -848,6 +852,11 @@
   // Output all of the functions.
   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
     printFunction(I);
+
+  // Output all aliases
+  for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
+       I != E; ++I)
+    printAlias(I);
 }
 
 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
@@ -888,16 +897,55 @@
     assert(C &&  "GlobalVar initializer isn't constant?");
     writeOperand(GV->getInitializer(), false);
   }
-  
+
   if (GV->hasSection())
     Out << ", section \"" << GV->getSection() << '"';
   if (GV->getAlignment())
     Out << ", align " << GV->getAlignment();
-  
+
   printInfoComment(*GV);
   Out << "\n";
 }
 
+void AssemblyWriter::printAlias(const GlobalAlias *GA) {
+  Out << getLLVMName(GA->getName(), GlobalPrefix) << " = ";
+  switch (GA->getVisibility()) {
+  default: assert(0 && "Invalid visibility style!");
+  case GlobalValue::DefaultVisibility: break;
+  case GlobalValue::HiddenVisibility: Out << "hidden "; break;
+  }
+
+  Out << "alias ";
+
+  switch (GA->getLinkage()) {
+  case GlobalValue::WeakLinkage: Out << "weak "; break;
+  case GlobalValue::InternalLinkage: Out << "internal "; break;
+  case GlobalValue::ExternalLinkage: break;
+  default:
+   assert(0 && "Invalid alias linkage");
+  }
+  
+  const GlobalValue *Aliasee = GA->getAliasee();
+  assert(Aliasee && "Aliasee cannot be null");
+    
+  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Aliasee)) {
+    printType(GV->getType());
+    Out << " " << getLLVMName(GV->getName(), GlobalPrefix);
+  } else if (const Function *F = dyn_cast<Function>(Aliasee)) {
+    printType(F->getFunctionType());
+    Out << "* ";
+
+    if (!F->getName().empty())
+      Out << getLLVMName(F->getName(), GlobalPrefix);
+    else
+      Out << "@\"\"";
+  } else
+    assert(0 && "Unsupported aliasee");
+
+  printInfoComment(*GA);
+  Out << "\n";
+}
+
 void AssemblyWriter::printTypeSymbolTable(const TypeSymbolTable &ST) {
   // Print the types.
   for (TypeSymbolTable::const_iterator TI = ST.begin(), TE = ST.end();
@@ -1336,6 +1384,12 @@
   W.write(this);
 }
 
+void GlobalAlias::print(std::ostream &o) const {
+  SlotMachine SlotTable(getParent());
+  AssemblyWriter W(o, SlotTable, getParent(), 0);
+  W.write(this);
+}
+
 void Function::print(std::ostream &o, AssemblyAnnotationWriter *AAW) const {
   SlotMachine SlotTable(getParent());
   AssemblyWriter W(o, SlotTable, getParent(), AAW);
@@ -1538,8 +1592,10 @@
   
   SC_DEBUG("  Inserting value [" << V->getType() << "] = " << V << " slot=" <<
            DestSlot << " [");
-  // G = Global, F = Function, o = other
-  SC_DEBUG((isa<GlobalVariable>(V) ? 'G' : 'F') << "]\n");
+  // G = Global, F = Function, A = Alias, o = other
+  SC_DEBUG((isa<GlobalVariable>(V) ? 'G' :
+            (isa<Function> ? 'F' :
+             (isa<GlobalAlias> ? 'A' : 'o'))) << "]\n");
 }
 
 


Index: llvm/lib/VMCore/Globals.cpp
diff -u llvm/lib/VMCore/Globals.cpp:1.19 llvm/lib/VMCore/Globals.cpp:1.20
--- llvm/lib/VMCore/Globals.cpp:1.19	Thu Apr 12 13:32:50 2007
+++ llvm/lib/VMCore/Globals.cpp	Wed Apr 25 09:27:10 2007
@@ -13,6 +13,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/GlobalVariable.h"
+#include "llvm/GlobalAlias.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Module.h"
 #include "llvm/Support/LeakDetector.h"
@@ -76,6 +77,7 @@
   assert(0 && "You can't GV->destroyConstant()!");
   abort();
 }
+  
 //===----------------------------------------------------------------------===//
 // GlobalVariable Implementation
 //===----------------------------------------------------------------------===//
@@ -120,7 +122,6 @@
     Before->getParent()->getGlobalList().insert(Before, this);
 }
 
-
 void GlobalVariable::setParent(Module *parent) {
   if (getParent())
     LeakDetector::addGarbageObject(this);
@@ -156,3 +157,45 @@
   // Okay, preconditions out of the way, replace the constant initializer.
   this->setOperand(0, cast<Constant>(To));
 }
+
+//===----------------------------------------------------------------------===//
+// GlobalAlias Implementation
+//===----------------------------------------------------------------------===//
+
+GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link,
+                         const std::string &Name, const GlobalValue* aliasee,
+                         Module *ParentModule)
+  : GlobalValue(Ty, Value::GlobalAliasVal, 0, 0,
+                Link, Name), Aliasee(aliasee) {
+  LeakDetector::addGarbageObject(this);
+
+  if (ParentModule)
+    ParentModule->getAliasList().push_back(this);
+}
+
+void GlobalAlias::setParent(Module *parent) {
+  if (getParent())
+    LeakDetector::addGarbageObject(this);
+  Parent = parent;
+  if (getParent())
+    LeakDetector::removeGarbageObject(this);
+}
+
+void GlobalAlias::removeFromParent() {
+  getParent()->getAliasList().remove(this);
+}
+
+void GlobalAlias::eraseFromParent() {
+  getParent()->getAliasList().erase(this);
+}
+
+bool GlobalAlias::isDeclaration() const {
+  return (Aliasee && Aliasee->isDeclaration());
+}
+
+void GlobalAlias::setAliasee(const GlobalValue *GV) 
+{
+  // FIXME: Some checks?
+  Aliasee = GV;
+}
+


Index: llvm/lib/VMCore/Module.cpp
diff -u llvm/lib/VMCore/Module.cpp:1.80 llvm/lib/VMCore/Module.cpp:1.81
--- llvm/lib/VMCore/Module.cpp:1.80	Mon Apr 16 23:04:14 2007
+++ llvm/lib/VMCore/Module.cpp	Wed Apr 25 09:27:10 2007
@@ -45,6 +45,12 @@
   LeakDetector::removeGarbageObject(Ret);
   return Ret;
 }
+GlobalAlias *ilist_traits<GlobalAlias>::createSentinel() {
+  GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty, GlobalValue::ExternalLinkage);
+  // This should not be garbage monitored.
+  LeakDetector::removeGarbageObject(Ret);
+  return Ret;
+}
 
 iplist<Function> &ilist_traits<Function>::getList(Module *M) {
   return M->getFunctionList();
@@ -52,11 +58,15 @@
 iplist<GlobalVariable> &ilist_traits<GlobalVariable>::getList(Module *M) {
   return M->getGlobalList();
 }
+iplist<GlobalAlias> &ilist_traits<GlobalAlias>::getList(Module *M) {
+  return M->getAliasList();
+}
 
 // Explicit instantiations of SymbolTableListTraits since some of the methods
 // are not in the public header file.
 template class SymbolTableListTraits<GlobalVariable, Module>;
 template class SymbolTableListTraits<Function, Module>;
+template class SymbolTableListTraits<GlobalAlias, Module>;
 
 //===----------------------------------------------------------------------===//
 // Primitive Module methods.
@@ -72,6 +82,7 @@
   dropAllReferences();
   GlobalList.clear();
   FunctionList.clear();
+  AliasList.clear();
   LibraryList.clear();
   delete ValSymTab;
   delete TypeSymTab;
@@ -212,6 +223,18 @@
 }
 
 //===----------------------------------------------------------------------===//
+// Methods for easy access to the global variables in the module.
+//
+
+// getNamedAlias - Look up the specified global in the module symbol table.
+// If it does not exist, return null.
+//
+GlobalAlias *Module::getNamedAlias(const std::string &Name) const {
+  const ValueSymbolTable &SymTab = getValueSymbolTable();
+  return dyn_cast_or_null<GlobalAlias>(SymTab.lookup(Name));
+}
+
+//===----------------------------------------------------------------------===//
 // Methods for easy access to the types in the module.
 //
 


Index: llvm/lib/VMCore/Verifier.cpp
diff -u llvm/lib/VMCore/Verifier.cpp:1.205 llvm/lib/VMCore/Verifier.cpp:1.206
--- llvm/lib/VMCore/Verifier.cpp:1.205	Fri Apr 20 18:59:29 2007
+++ llvm/lib/VMCore/Verifier.cpp	Wed Apr 25 09:27:10 2007
@@ -141,6 +141,10 @@
            I != E; ++I)
         visitGlobalVariable(*I);
 
+      for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); 
+           I != E; ++I)
+        visitGlobalAlias(*I);
+
       // If the module is broken, abort at this time.
       return abortIfBroken();
     }
@@ -179,6 +183,7 @@
     void verifyTypeSymbolTable(TypeSymbolTable &ST);
     void visitGlobalValue(GlobalValue &GV);
     void visitGlobalVariable(GlobalVariable &GV);
+    void visitGlobalAlias(GlobalAlias &GA);
     void visitFunction(Function &F);
     void visitBasicBlock(BasicBlock &BB);
     void visitTruncInst(TruncInst &I);
@@ -277,7 +282,9 @@
   Assert1(!GV.isDeclaration() ||
           GV.hasExternalLinkage() ||
           GV.hasDLLImportLinkage() ||
-          GV.hasExternalWeakLinkage(),
+          GV.hasExternalWeakLinkage() ||
+          (isa<GlobalAlias>(GV) &&
+           (GV.hasInternalLinkage() || GV.hasWeakLinkage())),
   "Global is external, but doesn't have external or dllimport or weak linkage!",
           &GV);
 
@@ -303,6 +310,16 @@
   visitGlobalValue(GV);
 }
 
+void Verifier::visitGlobalAlias(GlobalAlias &GA) {
+  Assert1(!GA.getName().empty(),
+          "Alias name cannot be empty!", &GA);
+  Assert1(GA.hasExternalLinkage() || GA.hasInternalLinkage() ||
+          GA.hasWeakLinkage(),
+          "Alias should have external or external weak linkage!", &GA);
+
+  visitGlobalValue(GA);
+}
+
 void Verifier::verifyTypeSymbolTable(TypeSymbolTable &ST) {
 }
 






More information about the llvm-commits mailing list