LLVM API Documentation
00001 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 /// @file This file contains the declarations for the Module class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_MODULE_H 00015 #define LLVM_MODULE_H 00016 00017 #include "llvm/Function.h" 00018 #include "llvm/GlobalVariable.h" 00019 #include "llvm/GlobalAlias.h" 00020 #include "llvm/Support/DataTypes.h" 00021 #include <vector> 00022 00023 namespace llvm { 00024 00025 class GlobalValueRefMap; // Used by ConstantVals.cpp 00026 class FunctionType; 00027 00028 template<> struct ilist_traits<Function> 00029 : public SymbolTableListTraits<Function, Module> { 00030 // createSentinel is used to create a node that marks the end of the list. 00031 static Function *createSentinel(); 00032 static void destroySentinel(Function *F) { delete F; } 00033 static iplist<Function> &getList(Module *M); 00034 static inline ValueSymbolTable *getSymTab(Module *M); 00035 static int getListOffset(); 00036 }; 00037 template<> struct ilist_traits<GlobalVariable> 00038 : public SymbolTableListTraits<GlobalVariable, Module> { 00039 // createSentinel is used to create a node that marks the end of the list. 00040 static GlobalVariable *createSentinel(); 00041 static void destroySentinel(GlobalVariable *GV) { delete GV; } 00042 static iplist<GlobalVariable> &getList(Module *M); 00043 static inline ValueSymbolTable *getSymTab(Module *M); 00044 static int getListOffset(); 00045 }; 00046 template<> struct ilist_traits<GlobalAlias> 00047 : public SymbolTableListTraits<GlobalAlias, Module> { 00048 // createSentinel is used to create a node that marks the end of the list. 00049 static GlobalAlias *createSentinel(); 00050 static void destroySentinel(GlobalAlias *GA) { delete GA; } 00051 static iplist<GlobalAlias> &getList(Module *M); 00052 static inline ValueSymbolTable *getSymTab(Module *M); 00053 static int getListOffset(); 00054 }; 00055 00056 /// A Module instance is used to store all the information related to an 00057 /// LLVM module. Modules are the top level container of all other LLVM 00058 /// Intermediate Representation (IR) objects. Each module directly contains a 00059 /// list of globals variables, a list of functions, a list of libraries (or 00060 /// other modules) this module depends on, a symbol table, and various data 00061 /// about the target's characteristics. 00062 /// 00063 /// A module maintains a GlobalValRefMap object that is used to hold all 00064 /// constant references to global variables in the module. When a global 00065 /// variable is destroyed, it should have no entries in the GlobalValueRefMap. 00066 /// @brief The main container class for the LLVM Intermediate Representation. 00067 class Module { 00068 /// @name Types And Enumerations 00069 /// @{ 00070 public: 00071 /// The type for the list of global variables. 00072 typedef iplist<GlobalVariable> GlobalListType; 00073 /// The type for the list of functions. 00074 typedef iplist<Function> FunctionListType; 00075 /// The type for the list of aliases. 00076 typedef iplist<GlobalAlias> AliasListType; 00077 00078 /// The type for the list of dependent libraries. 00079 typedef std::vector<std::string> LibraryListType; 00080 00081 /// The Global Variable iterator. 00082 typedef GlobalListType::iterator global_iterator; 00083 /// The Global Variable constant iterator. 00084 typedef GlobalListType::const_iterator const_global_iterator; 00085 00086 /// The Function iterators. 00087 typedef FunctionListType::iterator iterator; 00088 /// The Function constant iterator 00089 typedef FunctionListType::const_iterator const_iterator; 00090 00091 /// The Global Alias iterators. 00092 typedef AliasListType::iterator alias_iterator; 00093 /// The Global Alias constant iterator 00094 typedef AliasListType::const_iterator const_alias_iterator; 00095 00096 /// The Library list iterator. 00097 typedef LibraryListType::const_iterator lib_iterator; 00098 00099 /// An enumeration for describing the endianess of the target machine. 00100 enum Endianness { AnyEndianness, LittleEndian, BigEndian }; 00101 00102 /// An enumeration for describing the size of a pointer on the target machine. 00103 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 }; 00104 00105 /// @} 00106 /// @name Member Variables 00107 /// @{ 00108 private: 00109 GlobalListType GlobalList; ///< The Global Variables in the module 00110 FunctionListType FunctionList; ///< The Functions in the module 00111 AliasListType AliasList; ///< The Aliases in the module 00112 LibraryListType LibraryList; ///< The Libraries needed by the module 00113 std::string GlobalScopeAsm; ///< Inline Asm at global scope. 00114 ValueSymbolTable *ValSymTab; ///< Symbol table for values 00115 TypeSymbolTable *TypeSymTab; ///< Symbol table for types 00116 std::string ModuleID; ///< Human readable identifier for the module 00117 std::string TargetTriple; ///< Platform target triple Module compiled on 00118 std::string DataLayout; ///< Target data description 00119 00120 friend class Constant; 00121 00122 /// @} 00123 /// @name Constructors 00124 /// @{ 00125 public: 00126 /// The Module constructor. Note that there is no default constructor. You 00127 /// must provide a name for the module upon construction. 00128 explicit Module(const std::string &ModuleID); 00129 /// The module destructor. This will dropAllReferences. 00130 ~Module(); 00131 00132 /// @} 00133 /// @name Module Level Accessors 00134 /// @{ 00135 public: 00136 /// Get the module identifier which is, essentially, the name of the module. 00137 /// @returns the module identifier as a string 00138 const std::string &getModuleIdentifier() const { return ModuleID; } 00139 00140 /// Get the data layout string for the module's target platform. This encodes 00141 /// the type sizes and alignments expected by this module. 00142 /// @returns the data layout as a string 00143 const std::string& getDataLayout() const { return DataLayout; } 00144 00145 /// Get the target triple which is a string describing the target host. 00146 /// @returns a string containing the target triple. 00147 const std::string &getTargetTriple() const { return TargetTriple; } 00148 00149 /// Get the target endian information. 00150 /// @returns Endianess - an enumeration for the endianess of the target 00151 Endianness getEndianness() const; 00152 00153 /// Get the target pointer size. 00154 /// @returns PointerSize - an enumeration for the size of the target's pointer 00155 PointerSize getPointerSize() const; 00156 00157 /// Get any module-scope inline assembly blocks. 00158 /// @returns a string containing the module-scope inline assembly blocks. 00159 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; } 00160 /// @} 00161 /// @name Module Level Mutators 00162 /// @{ 00163 public: 00164 00165 /// Set the module identifier. 00166 void setModuleIdentifier(const std::string &ID) { ModuleID = ID; } 00167 00168 /// Set the data layout 00169 void setDataLayout(const std::string& DL) { DataLayout = DL; } 00170 00171 /// Set the target triple. 00172 void setTargetTriple(const std::string &T) { TargetTriple = T; } 00173 00174 /// Set the module-scope inline assembly blocks. 00175 void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; } 00176 00177 /// Append to the module-scope inline assembly blocks, automatically 00178 /// appending a newline to the end. 00179 void appendModuleInlineAsm(const std::string &Asm) { 00180 GlobalScopeAsm += Asm; 00181 GlobalScopeAsm += '\n'; 00182 } 00183 00184 /// @} 00185 /// @name Function Accessors 00186 /// @{ 00187 public: 00188 /// getOrInsertFunction - Look up the specified function in the module symbol 00189 /// table. Four possibilities: 00190 /// 1. If it does not exist, add a prototype for the function and return it. 00191 /// 2. If it exists, and has internal linkage, the existing function is 00192 /// renamed and a new one is inserted. 00193 /// 3. Otherwise, if the existing function has the correct prototype, return 00194 /// the existing function. 00195 /// 4. Finally, the function exists but has the wrong prototype: return the 00196 /// function with a constantexpr cast to the right prototype. 00197 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T, 00198 AttrListPtr AttributeList); 00199 00200 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T); 00201 00202 /// getOrInsertFunction - Look up the specified function in the module symbol 00203 /// table. If it does not exist, add a prototype for the function and return 00204 /// it. This function guarantees to return a constant of pointer to the 00205 /// specified function type or a ConstantExpr BitCast of that type if the 00206 /// named function has a different type. This version of the method takes a 00207 /// null terminated list of function arguments, which makes it easier for 00208 /// clients to use. 00209 Constant *getOrInsertFunction(const std::string &Name, 00210 AttrListPtr AttributeList, 00211 const Type *RetTy, ...) END_WITH_NULL; 00212 00213 Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy, ...) 00214 END_WITH_NULL; 00215 00216 /// getFunction - Look up the specified function in the module symbol table. 00217 /// If it does not exist, return null. 00218 Function *getFunction(const std::string &Name) const; 00219 Function *getFunction(const char *Name) const; 00220 00221 /// @} 00222 /// @name Global Variable Accessors 00223 /// @{ 00224 public: 00225 /// getGlobalVariable - Look up the specified global variable in the module 00226 /// symbol table. If it does not exist, return null. If AllowInternal is set 00227 /// to true, this function will return types that have InternalLinkage. By 00228 /// default, these types are not returned. 00229 GlobalVariable *getGlobalVariable(const std::string &Name, 00230 bool AllowInternal = false) const; 00231 00232 /// getNamedGlobal - Return the first global variable in the module with the 00233 /// specified name, of arbitrary type. This method returns null if a global 00234 /// with the specified name is not found. 00235 GlobalVariable *getNamedGlobal(const std::string &Name) const { 00236 return getGlobalVariable(Name, true); 00237 } 00238 00239 /// getOrInsertGlobal - Look up the specified global in the module symbol 00240 /// table. 00241 /// 1. If it does not exist, add a declaration of the global and return it. 00242 /// 2. Else, the global exists but has the wrong type: return the function 00243 /// with a constantexpr cast to the right type. 00244 /// 3. Finally, if the existing global is the correct delclaration, return 00245 /// the existing global. 00246 Constant *getOrInsertGlobal(const std::string &Name, const Type *Ty); 00247 00248 /// @} 00249 /// @name Global Alias Accessors 00250 /// @{ 00251 public: 00252 /// getNamedAlias - Return the first global alias in the module with the 00253 /// specified name, of arbitrary type. This method returns null if a global 00254 /// with the specified name is not found. 00255 GlobalAlias *getNamedAlias(const std::string &Name) const; 00256 00257 /// @} 00258 /// @name Type Accessors 00259 /// @{ 00260 public: 00261 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If 00262 /// there is already an entry for this name, true is returned and the symbol 00263 /// table is not modified. 00264 bool addTypeName(const std::string &Name, const Type *Ty); 00265 00266 /// getTypeName - If there is at least one entry in the symbol table for the 00267 /// specified type, return it. 00268 std::string getTypeName(const Type *Ty) const; 00269 00270 /// getTypeByName - Return the type with the specified name in this module, or 00271 /// null if there is none by that name. 00272 const Type *getTypeByName(const std::string &Name) const; 00273 00274 /// @} 00275 /// @name Direct access to the globals list, functions list, and symbol table 00276 /// @{ 00277 public: 00278 /// Get the Module's list of global variables (constant). 00279 const GlobalListType &getGlobalList() const { return GlobalList; } 00280 /// Get the Module's list of global variables. 00281 GlobalListType &getGlobalList() { return GlobalList; } 00282 /// Get the Module's list of functions (constant). 00283 const FunctionListType &getFunctionList() const { return FunctionList; } 00284 /// Get the Module's list of functions. 00285 FunctionListType &getFunctionList() { return FunctionList; } 00286 /// Get the Module's list of aliases (constant). 00287 const AliasListType &getAliasList() const { return AliasList; } 00288 /// Get the Module's list of aliases. 00289 AliasListType &getAliasList() { return AliasList; } 00290 /// Get the symbol table of global variable and function identifiers 00291 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; } 00292 /// Get the Module's symbol table of global variable and function identifiers. 00293 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; } 00294 /// Get the symbol table of types 00295 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; } 00296 /// Get the Module's symbol table of types 00297 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; } 00298 00299 /// @} 00300 /// @name Global Variable Iteration 00301 /// @{ 00302 public: 00303 /// Get an iterator to the first global variable 00304 global_iterator global_begin() { return GlobalList.begin(); } 00305 /// Get a constant iterator to the first global variable 00306 const_global_iterator global_begin() const { return GlobalList.begin(); } 00307 /// Get an iterator to the last global variable 00308 global_iterator global_end () { return GlobalList.end(); } 00309 /// Get a constant iterator to the last global variable 00310 const_global_iterator global_end () const { return GlobalList.end(); } 00311 /// Determine if the list of globals is empty. 00312 bool global_empty() const { return GlobalList.empty(); } 00313 00314 /// @} 00315 /// @name Function Iteration 00316 /// @{ 00317 public: 00318 /// Get an iterator to the first function. 00319 iterator begin() { return FunctionList.begin(); } 00320 /// Get a constant iterator to the first function. 00321 const_iterator begin() const { return FunctionList.begin(); } 00322 /// Get an iterator to the last function. 00323 iterator end () { return FunctionList.end(); } 00324 /// Get a constant iterator to the last function. 00325 const_iterator end () const { return FunctionList.end(); } 00326 /// Determine how many functions are in the Module's list of functions. 00327 size_t size() const { return FunctionList.size(); } 00328 /// Determine if the list of functions is empty. 00329 bool empty() const { return FunctionList.empty(); } 00330 00331 /// @} 00332 /// @name Dependent Library Iteration 00333 /// @{ 00334 public: 00335 /// @brief Get a constant iterator to beginning of dependent library list. 00336 inline lib_iterator lib_begin() const { return LibraryList.begin(); } 00337 /// @brief Get a constant iterator to end of dependent library list. 00338 inline lib_iterator lib_end() const { return LibraryList.end(); } 00339 /// @brief Returns the number of items in the list of libraries. 00340 inline size_t lib_size() const { return LibraryList.size(); } 00341 /// @brief Add a library to the list of dependent libraries 00342 void addLibrary(const std::string& Lib); 00343 /// @brief Remove a library from the list of dependent libraries 00344 void removeLibrary(const std::string& Lib); 00345 /// @brief Get all the libraries 00346 inline const LibraryListType& getLibraries() const { return LibraryList; } 00347 00348 /// @} 00349 /// @name Alias Iteration 00350 /// @{ 00351 public: 00352 /// Get an iterator to the first alias. 00353 alias_iterator alias_begin() { return AliasList.begin(); } 00354 /// Get a constant iterator to the first alias. 00355 const_alias_iterator alias_begin() const { return AliasList.begin(); } 00356 /// Get an iterator to the last alias. 00357 alias_iterator alias_end () { return AliasList.end(); } 00358 /// Get a constant iterator to the last alias. 00359 const_alias_iterator alias_end () const { return AliasList.end(); } 00360 /// Determine how many functions are in the Module's list of aliases. 00361 size_t alias_size () const { return AliasList.size(); } 00362 /// Determine if the list of aliases is empty. 00363 bool alias_empty() const { return AliasList.empty(); } 00364 00365 /// @} 00366 /// @name Utility functions for printing and dumping Module objects 00367 /// @{ 00368 public: 00369 /// Print the module to an output stream with AssemblyAnnotationWriter. 00370 void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW) const; 00371 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const; 00372 00373 /// Dump the module to stderr (for debugging). 00374 void dump() const; 00375 /// This function causes all the subinstructions to "let go" of all references 00376 /// that they are maintaining. This allows one to 'delete' a whole class at 00377 /// a time, even though there may be circular references... first all 00378 /// references are dropped, and all use counts go to zero. Then everything 00379 /// is delete'd for real. Note that no operations are valid on an object 00380 /// that has "dropped all references", except operator delete. 00381 void dropAllReferences(); 00382 /// @} 00383 00384 static unsigned getFunctionListOffset() { 00385 Module *Obj = 0; 00386 return unsigned(reinterpret_cast<uintptr_t>(&Obj->FunctionList)); 00387 } 00388 static unsigned getGlobalVariableListOffset() { 00389 Module *Obj = 0; 00390 return unsigned(reinterpret_cast<uintptr_t>(&Obj->GlobalList)); 00391 } 00392 static unsigned getAliasListOffset() { 00393 Module *Obj = 0; 00394 return unsigned(reinterpret_cast<uintptr_t>(&Obj->AliasList)); 00395 } 00396 }; 00397 00398 /// An iostream inserter for modules. 00399 inline std::ostream &operator<<(std::ostream &O, const Module &M) { 00400 M.print(O, 0); 00401 return O; 00402 } 00403 inline raw_ostream &operator<<(raw_ostream &O, const Module &M) { 00404 M.print(O, 0); 00405 return O; 00406 } 00407 00408 00409 inline ValueSymbolTable * 00410 ilist_traits<Function>::getSymTab(Module *M) { 00411 return M ? &M->getValueSymbolTable() : 0; 00412 } 00413 00414 inline ValueSymbolTable * 00415 ilist_traits<GlobalVariable>::getSymTab(Module *M) { 00416 return M ? &M->getValueSymbolTable() : 0; 00417 } 00418 00419 inline ValueSymbolTable * 00420 ilist_traits<GlobalAlias>::getSymTab(Module *M) { 00421 return M ? &M->getValueSymbolTable() : 0; 00422 } 00423 00424 inline int 00425 ilist_traits<Function>::getListOffset() { 00426 return Module::getFunctionListOffset(); 00427 } 00428 00429 inline int 00430 ilist_traits<GlobalVariable>::getListOffset() { 00431 return Module::getGlobalVariableListOffset(); 00432 } 00433 00434 inline int 00435 ilist_traits<GlobalAlias>::getListOffset() { 00436 return Module::getAliasListOffset(); 00437 } 00438 00439 } // End llvm namespace 00440 00441 #endif
This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.