diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AsmParser/Lexer.l | 1 | ||||
-rw-r--r-- | lib/AsmParser/llvmAsmParser.y | 52 | ||||
-rw-r--r-- | lib/Bytecode/Reader/Reader.cpp | 51 | ||||
-rw-r--r-- | lib/Bytecode/Writer/Writer.cpp | 27 | ||||
-rw-r--r-- | lib/CodeGen/AsmPrinter.cpp | 26 | ||||
-rw-r--r-- | lib/Target/X86/X86TargetAsmInfo.cpp | 2 | ||||
-rw-r--r-- | lib/Transforms/IPO/GlobalDCE.cpp | 13 | ||||
-rw-r--r-- | lib/VMCore/AsmWriter.cpp | 74 | ||||
-rw-r--r-- | lib/VMCore/Globals.cpp | 45 | ||||
-rw-r--r-- | lib/VMCore/Module.cpp | 23 | ||||
-rw-r--r-- | lib/VMCore/Verifier.cpp | 19 |
11 files changed, 310 insertions, 23 deletions
diff --git a/lib/AsmParser/Lexer.l b/lib/AsmParser/Lexer.l index d75d07008b..382ce24462 100644 --- a/lib/AsmParser/Lexer.l +++ b/lib/AsmParser/Lexer.l @@ -222,6 +222,7 @@ datalayout { return DATALAYOUT; } volatile { return VOLATILE; } align { return ALIGN; } section { return SECTION; } +alias { return ALIAS; } module { return MODULE; } asm { return ASM_TOK; } sideeffect { return SIDEEFFECT; } diff --git a/lib/AsmParser/llvmAsmParser.y b/lib/AsmParser/llvmAsmParser.y index 2b08d3afa1..17ca06f664 100644 --- a/lib/AsmParser/llvmAsmParser.y +++ b/lib/AsmParser/llvmAsmParser.y @@ -1004,6 +1004,7 @@ Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) { %type <BoolVal> OptSideEffect // 'sideeffect' or not. %type <Linkage> GVInternalLinkage GVExternalLinkage %type <Linkage> FunctionDefineLinkage FunctionDeclareLinkage +%type <Linkage> AliasLinkage %type <Visibility> GVVisibilityStyle // ValueRef - Unresolved reference to a definition or BB @@ -1035,12 +1036,12 @@ Module *llvm::RunVMAsmParser(const char * AsmString, Module * M) { %token<StrVal> LOCALVAR GLOBALVAR LABELSTR STRINGCONSTANT ATSTRINGCONSTANT %type <StrVal> LocalName OptLocalName OptLocalAssign -%type <StrVal> GlobalName OptGlobalAssign +%type <StrVal> GlobalName OptGlobalAssign GlobalAssign %type <UIntVal> OptAlign OptCAlign %type <StrVal> OptSection SectionString %token ZEROINITIALIZER TRUETOK FALSETOK BEGINTOK ENDTOK -%token DECLARE DEFINE GLOBAL CONSTANT SECTION VOLATILE THREAD_LOCAL +%token DECLARE DEFINE GLOBAL CONSTANT SECTION ALIAS VOLATILE THREAD_LOCAL %token TO DOTDOTDOT NULL_TOK UNDEF INTERNAL LINKONCE WEAK APPENDING %token DLLIMPORT DLLEXPORT EXTERN_WEAK %token OPAQUE EXTERNAL TARGET TRIPLE ALIGN @@ -1136,15 +1137,17 @@ OptLocalAssign : LocalName '=' { GlobalName : GLOBALVAR | ATSTRINGCONSTANT; -OptGlobalAssign : GlobalName '=' { - $$ = $1; - CHECK_FOR_ERROR - } +OptGlobalAssign : GlobalAssign | /*empty*/ { $$ = 0; CHECK_FOR_ERROR }; +GlobalAssign : GlobalName '=' { + $$ = $1; + CHECK_FOR_ERROR + } + GVInternalLinkage : INTERNAL { $$ = GlobalValue::InternalLinkage; } | WEAK { $$ = GlobalValue::WeakLinkage; } @@ -1161,6 +1164,7 @@ GVExternalLinkage GVVisibilityStyle : /*empty*/ { $$ = GlobalValue::DefaultVisibility; } + | DEFAULT { $$ = GlobalValue::DefaultVisibility; } | HIDDEN { $$ = GlobalValue::HiddenVisibility; } ; @@ -1170,7 +1174,7 @@ FunctionDeclareLinkage | EXTERN_WEAK { $$ = GlobalValue::ExternalWeakLinkage; } ; -FunctionDefineLinkage +FunctionDefineLinkage : /*empty*/ { $$ = GlobalValue::ExternalLinkage; } | INTERNAL { $$ = GlobalValue::InternalLinkage; } | LINKONCE { $$ = GlobalValue::LinkOnceLinkage; } @@ -1178,6 +1182,12 @@ FunctionDefineLinkage | DLLEXPORT { $$ = GlobalValue::DLLExportLinkage; } ; +AliasLinkage + : /*empty*/ { $$ = GlobalValue::ExternalLinkage; } + | WEAK { $$ = GlobalValue::WeakLinkage; } + | INTERNAL { $$ = GlobalValue::InternalLinkage; } + ; + OptCallingConv : /*empty*/ { $$ = CallingConv::C; } | CCC_TOK { $$ = CallingConv::C; } | FASTCC_TOK { $$ = CallingConv::Fast; } | @@ -2031,6 +2041,34 @@ Definition CurGV = 0; CHECK_FOR_ERROR } + | OptGlobalAssign GVVisibilityStyle ALIAS AliasLinkage ResultTypes SymbolicValueRef { + std::string Name($1); + if (Name.empty()) + GEN_ERROR("Alias name cannot be empty") + const PointerType *PFTy = 0; + const FunctionType *Ty = 0; + Value* V = 0; + const Type* VTy = 0; + if (!(PFTy = dyn_cast<PointerType>($5->get())) || + !(Ty = dyn_cast<FunctionType>(PFTy->getElementType()))) { + VTy = $5->get(); + V = getExistingVal(VTy, $6); + } else { + VTy = PFTy; + V = getExistingVal(PFTy, $6); + } + if (V == 0) + GEN_ERROR(std::string("Invalid aliasee for alias: ") + $1); + GlobalValue* Aliasee; + if (Aliasee = dyn_cast<GlobalValue>(V)) { + GlobalAlias* GA = new GlobalAlias(VTy, $4, Name, Aliasee, CurModule.CurrentModule); + GA->setVisibility($2); + InsertValue(GA, CurModule.Values); + } else + GEN_ERROR("Aliases can be created only to global values"); + CHECK_FOR_ERROR + delete $5; + } | TARGET TargetDefinition { CHECK_FOR_ERROR } diff --git a/lib/Bytecode/Reader/Reader.cpp b/lib/Bytecode/Reader/Reader.cpp index ee6d9e6208..f7606c6e5a 100644 --- a/lib/Bytecode/Reader/Reader.cpp +++ b/lib/Bytecode/Reader/Reader.cpp @@ -348,7 +348,7 @@ Constant* BytecodeReader::getConstantValue(unsigned TypeSlot, unsigned Slot) { /// with this method. The ValueTable argument must be one of ModuleValues /// or FunctionValues data members of this class. unsigned BytecodeReader::insertValue(Value *Val, unsigned type, - ValueTable &ValueTab) { + ValueTable &ValueTab) { if (ValueTab.size() <= type) ValueTab.resize(type+1); @@ -1855,7 +1855,7 @@ void BytecodeReader::ParseModuleGlobalInfo() { case 1: Func->setLinkage(Function::DLLImportLinkage); break; case 2: Func->setLinkage(Function::ExternalWeakLinkage); break; default: assert(0 && "Unsupported external linkage"); - } + } } Func->setCallingConv(CC-1); @@ -1919,6 +1919,53 @@ void BytecodeReader::ParseModuleGlobalInfo() { I->first->setSection(SectionNames[I->second-1]); } + if (At != BlockEnd) { + // Read aliases... + unsigned VarType = read_vbr_uint(); + while (VarType != Type::VoidTyID) { // List is terminated by Void + unsigned TypeSlotNo = VarType >> 2; + unsigned EncodedLinkage = VarType & 3; + unsigned AliaseeTypeSlotNo, AliaseeSlotNo; + + AliaseeTypeSlotNo = read_vbr_uint(); + AliaseeSlotNo = read_vbr_uint(); + + const Type *Ty = getType(TypeSlotNo); + if (!Ty) + error("Alias has no type! SlotNo=" + utostr(TypeSlotNo)); + + if (!isa<PointerType>(Ty)) + error("Alias not a pointer type! Ty= " + Ty->getDescription()); + + Value* V = getValue(AliaseeTypeSlotNo, AliaseeSlotNo, false); + if (!V) + error("Invalid aliasee! TypeSlotNo=" + utostr(AliaseeTypeSlotNo) + + " SlotNo=" + utostr(AliaseeSlotNo)); + if (!isa<GlobalValue>(V)) + error("Aliasee is not global value! SlotNo=" + utostr(AliaseeSlotNo)); + + GlobalValue::LinkageTypes Linkage; + switch (EncodedLinkage) { + case 0: + Linkage = GlobalValue::ExternalLinkage; + break; + case 1: + Linkage = GlobalValue::InternalLinkage; + break; + case 2: + Linkage = GlobalValue::WeakLinkage; + break; + default: + assert(0 && "Unsupported encoded alias linkage"); + } + + GlobalAlias *GA = new GlobalAlias(Ty, Linkage, "", + dyn_cast<GlobalValue>(V), TheModule); + insertValue(GA, TypeSlotNo, ModuleValues); + VarType = read_vbr_uint(); + } + } + // This is for future proofing... in the future extra fields may be added that // we don't understand, so we transparently ignore them. // diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp index 12724dd6be..7295239fdd 100644 --- a/lib/Bytecode/Writer/Writer.cpp +++ b/lib/Bytecode/Writer/Writer.cpp @@ -1088,9 +1088,34 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) { output_vbr((unsigned)SectionNames.size()); for (unsigned i = 0, e = SectionNames.size(); i != e; ++i) output(SectionNames[i]); - + // Output the inline asm string. output(M->getModuleInlineAsm()); + + // Output aliases + for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end(); + I != E; ++I) { + unsigned Slot = Table.getTypeSlot(I->getType()); + assert(((Slot << 2) >> 2) == Slot && "Slot # too big!"); + unsigned aliasLinkage = 0; + switch (I->getLinkage()) { + case GlobalValue::ExternalLinkage: + aliasLinkage = 0; + break; + case GlobalValue::InternalLinkage: + aliasLinkage = 1; + break; + case GlobalValue::WeakLinkage: + aliasLinkage = 2; + break; + default: + assert(0 && "Invalid alias linkage"); + } + output_vbr((Slot << 2) | aliasLinkage); + output_vbr(Table.getTypeSlot(I->getAliasee()->getType())); + output_vbr(Table.getSlot(I->getAliasee())); + } + output_typeid(Table.getTypeSlot(Type::VoidTy)); } void BytecodeWriter::outputInstructions(const Function *F) { diff --git a/lib/CodeGen/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter.cpp index 16c478dcdc..90a98cb5a7 100644 --- a/lib/CodeGen/AsmPrinter.cpp +++ b/lib/CodeGen/AsmPrinter.cpp @@ -111,7 +111,7 @@ bool AsmPrinter::doInitialization(Module &M) { bool AsmPrinter::doFinalization(Module &M) { if (TAI->getWeakRefDirective()) { - if (ExtWeakSymbols.begin() != ExtWeakSymbols.end()) + if (!ExtWeakSymbols.empty()) SwitchToDataSection(""); for (std::set<const GlobalValue*>::iterator i = ExtWeakSymbols.begin(), @@ -122,6 +122,30 @@ bool AsmPrinter::doFinalization(Module &M) { } } + if (TAI->getSetDirective()) { + if (M.alias_size()) + SwitchToTextSection(TAI->getTextSection()); + + O << "\n"; + for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end(); + I!=E; ++I) { + const GlobalValue *Aliasee = I->getAliasee(); + assert(Aliasee && "Aliasee cannot be null!"); + std::string Target = Mang->getValueName(Aliasee); + std::string Name = Mang->getValueName(I); + + // Aliases with external weak linkage was emitted already + if (I->hasExternalLinkage()) + O << "\t.globl\t" << Name << "\n"; + else if (I->hasWeakLinkage()) + O << TAI->getWeakRefDirective() << Name << "\n"; + else if (!I->hasInternalLinkage()) + assert(0 && "Invalid alias linkage"); + + O << TAI->getSetDirective() << Name << ", " << Target << "\n"; + } + } + delete Mang; Mang = 0; return false; } diff --git a/lib/Target/X86/X86TargetAsmInfo.cpp b/lib/Target/X86/X86TargetAsmInfo.cpp index 8b0ad03b0a..fd8af4f69a 100644 --- a/lib/Target/X86/X86TargetAsmInfo.cpp +++ b/lib/Target/X86/X86TargetAsmInfo.cpp @@ -108,6 +108,7 @@ X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) { ReadOnlySection = "\t.section\t.rodata\n"; PrivateGlobalPrefix = ".L"; WeakRefDirective = "\t.weak\t"; + SetDirective = "\t.set\t"; DwarfRequiresFrameSection = false; DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"\",@progbits"; DwarfInfoSection = "\t.section\t.debug_info,\"\",@progbits"; @@ -137,6 +138,7 @@ X86TargetAsmInfo::X86TargetAsmInfo(const X86TargetMachine &TM) { AbsoluteSectionOffsets = true; PrivateGlobalPrefix = "L"; // Prefix for private global symbols WeakRefDirective = "\t.weak\t"; + SetDirective = "\t.set\t"; DwarfRequiresFrameSection = false; DwarfSectionOffsetDirective = "\t.secrel32\t"; DwarfAbbrevSection = "\t.section\t.debug_abbrev,\"dr\""; diff --git a/lib/Transforms/IPO/GlobalDCE.cpp b/lib/Transforms/IPO/GlobalDCE.cpp index 7fd110208f..560bcb56b0 100644 --- a/lib/Transforms/IPO/GlobalDCE.cpp +++ b/lib/Transforms/IPO/GlobalDCE.cpp @@ -62,7 +62,8 @@ bool GlobalDCE::runOnModule(Module &M) { GlobalIsNeeded(I); } - for (Module::global_iterator I = M.global_begin(), E = M.global_end(); I != E; ++I) { + for (Module::global_iterator I = M.global_begin(), E = M.global_end(); + I != E; ++I) { Changed |= RemoveUnusedGlobalValue(*I); // Externally visible & appending globals are needed, if they have an // initializer. @@ -72,6 +73,13 @@ bool GlobalDCE::runOnModule(Module &M) { } + for (Module::alias_iterator I = M.alias_begin(), E = M.alias_end(); + I != E; ++I) { + Changed |= RemoveUnusedGlobalValue(*I); + // Aliases are always needed even if they are not used. + GlobalIsNeeded(I); + } + // Now that all globals which are needed are in the AliveGlobals set, we loop // through the program, deleting those which are not alive. // @@ -135,6 +143,9 @@ void GlobalDCE::GlobalIsNeeded(GlobalValue *G) { // referenced by the initializer to the alive set. if (GV->hasInitializer()) MarkUsedGlobalsAsNeeded(GV->getInitializer()); + } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(G)) { + // If this is a global alias we also need it's aliasee + GlobalIsNeeded(const_cast<GlobalValue*>(GA->getAliasee())); } else { // Otherwise this must be a function object. We have to scan the body of // the function looking for constants and global values which are used as diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp index 90f0198b57..ec9a5539b9 100644 --- a/lib/VMCore/AsmWriter.cpp +++ b/lib/VMCore/AsmWriter.cpp @@ -166,6 +166,8 @@ static SlotMachine *createSlotMachine(const Value *V) { 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 @@ public: 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 @@ private: 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 @@ void AssemblyWriter::printModule(const Module *M) { // 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 @@ void AssemblyWriter::printGlobal(const GlobalVariable *GV) { 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 @@ void GlobalVariable::print(std::ostream &o) const { 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 @@ void SlotMachine::CreateModuleSlot(const GlobalValue *V) { 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"); } diff --git a/lib/VMCore/Globals.cpp b/lib/VMCore/Globals.cpp index 61d9de3a23..c64b719095 100644 --- a/lib/VMCore/Globals.cpp +++ b/lib/VMCore/Globals.cpp @@ -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 @@ void GlobalValue::destroyConstant() { assert(0 && "You can't GV->destroyConstant()!"); abort(); } + //===----------------------------------------------------------------------===// // GlobalVariable Implementation //===----------------------------------------------------------------------===// @@ -120,7 +122,6 @@ GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, Before->getParent()->getGlobalList().insert(Before, this); } - void GlobalVariable::setParent(Module *parent) { if (getParent()) LeakDetector::addGarbageObject(this); @@ -156,3 +157,45 @@ void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, // 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; +} + diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp index ddd503dea1..c66032388b 100644 --- a/lib/VMCore/Module.cpp +++ b/lib/VMCore/Module.cpp @@ -45,6 +45,12 @@ GlobalVariable *ilist_traits<GlobalVariable>::createSentinel() { 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<Function> &ilist_traits<Function>::getList(Module *M) { 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 @@ Module::~Module() { dropAllReferences(); GlobalList.clear(); FunctionList.clear(); + AliasList.clear(); LibraryList.clear(); delete ValSymTab; delete TypeSymTab; @@ -212,6 +223,18 @@ GlobalVariable *Module::getGlobalVariable(const std::string &Name, } //===----------------------------------------------------------------------===// +// 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. // diff --git a/lib/VMCore/Verifier.cpp b/lib/VMCore/Verifier.cpp index 19367662d2..1578c2ddd6 100644 --- a/lib/VMCore/Verifier.cpp +++ b/lib/VMCore/Verifier.cpp @@ -141,6 +141,10 @@ namespace { // Anonymous namespace for class 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 @@ namespace { // Anonymous namespace for class 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 @@ void Verifier::visitGlobalValue(GlobalValue &GV) { 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 @@ void Verifier::visitGlobalVariable(GlobalVariable &GV) { 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) { } |