1 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// @file This file contains the declarations for the Module class.
12 //===----------------------------------------------------------------------===//
17 #include "llvm/Function.h"
18 #include "llvm/GlobalVariable.h"
19 #include "llvm/Support/DataTypes.h"
25 class GlobalValueRefMap; // Used by ConstantVals.cpp
28 template<> struct ilist_traits<Function>
29 : public SymbolTableListTraits<Function, Module> {
30 // createSentinel is used to create a node that marks the end of the list.
31 static Function *createSentinel();
32 static void destroySentinel(Function *F) { delete F; }
33 static iplist<Function> &getList(Module *M);
34 static inline ValueSymbolTable *getSymTab(Module *M);
35 static int getListOffset();
37 template<> struct ilist_traits<GlobalVariable>
38 : public SymbolTableListTraits<GlobalVariable, Module> {
39 // createSentinel is used to create a node that marks the end of the list.
40 static GlobalVariable *createSentinel();
41 static void destroySentinel(GlobalVariable *GV) { delete GV; }
42 static iplist<GlobalVariable> &getList(Module *M);
43 static inline ValueSymbolTable *getSymTab(Module *M);
44 static int getListOffset();
47 /// A Module instance is used to store all the information related to an
48 /// LLVM module. Modules are the top level container of all other LLVM
49 /// Intermediate Representation (IR) objects. Each module directly contains a
50 /// list of globals variables, a list of functions, a list of libraries (or
51 /// other modules) this module depends on, a symbol table, and various data
52 /// about the target's characteristics.
54 /// A module maintains a GlobalValRefMap object that is used to hold all
55 /// constant references to global variables in the module. When a global
56 /// variable is destroyed, it should have no entries in the GlobalValueRefMap.
57 /// @brief The main container class for the LLVM Intermediate Representation.
59 /// @name Types And Enumerations
62 /// The type for the list of global variables.
63 typedef iplist<GlobalVariable> GlobalListType;
64 /// The type for the list of functions.
65 typedef iplist<Function> FunctionListType;
67 /// The type for the list of dependent libraries.
68 typedef std::vector<std::string> LibraryListType;
70 /// The Global Variable iterator.
71 typedef GlobalListType::iterator global_iterator;
72 /// The Global Variable constant iterator.
73 typedef GlobalListType::const_iterator const_global_iterator;
75 /// The Function iterators.
76 typedef FunctionListType::iterator iterator;
77 /// The Function constant iterator
78 typedef FunctionListType::const_iterator const_iterator;
80 /// The Library list iterator.
81 typedef LibraryListType::const_iterator lib_iterator;
83 /// An enumeration for describing the endianess of the target machine.
84 enum Endianness { AnyEndianness, LittleEndian, BigEndian };
86 /// An enumeration for describing the size of a pointer on the target machine.
87 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
90 /// @name Member Variables
93 GlobalListType GlobalList; ///< The Global Variables in the module
94 FunctionListType FunctionList; ///< The Functions in the module
95 LibraryListType LibraryList; ///< The Libraries needed by the module
96 std::string GlobalScopeAsm; ///< Inline Asm at global scope.
97 ValueSymbolTable *ValSymTab; ///< Symbol table for values
98 TypeSymbolTable *TypeSymTab; ///< Symbol table for types
99 std::string ModuleID; ///< Human readable identifier for the module
100 std::string TargetTriple; ///< Platform target triple Module compiled on
101 std::string DataLayout; ///< Target data description
103 friend class Constant;
106 /// @name Constructors
109 /// The Module constructor. Note that there is no default constructor. You
110 /// must provide a name for the module upon construction.
111 explicit Module(const std::string &ModuleID);
112 /// The module destructor. This will dropAllReferences.
116 /// @name Module Level Accessors
119 /// Get the module identifier which is, essentially, the name of the module.
120 /// @returns the module identifier as a string
121 const std::string &getModuleIdentifier() const { return ModuleID; }
123 /// Get the data layout string for the module's target platform. This encodes
124 /// the type sizes and alignments expected by this module.
125 /// @returns the data layout as a string
126 const std::string& getDataLayout() const { return DataLayout; }
128 /// Get the target triple which is a string describing the target host.
129 /// @returns a string containing the target triple.
130 const std::string &getTargetTriple() const { return TargetTriple; }
132 /// Get the target endian information.
133 /// @returns Endianess - an enumeration for the endianess of the target
134 Endianness getEndianness() const;
136 /// Get the target pointer size.
137 /// @returns PointerSize - an enumeration for the size of the target's pointer
138 PointerSize getPointerSize() const;
140 /// Get any module-scope inline assembly blocks.
141 /// @returns a string containing the module-scope inline assembly blocks.
142 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
144 /// @name Module Level Mutators
148 /// Set the module identifier.
149 void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
151 /// Set the data layout
152 void setDataLayout(const std::string& DL) { DataLayout = DL; }
154 /// Set the target triple.
155 void setTargetTriple(const std::string &T) { TargetTriple = T; }
157 /// Set the module-scope inline assembly blocks.
158 void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
161 /// @name Function Accessors
164 /// getOrInsertFunction - Look up the specified function in the module symbol
165 /// table. Four possibilities:
166 /// 1. If it does not exist, add a prototype for the function and return it.
167 /// 2. If it exists, and has internal linkage, the existing function is
168 /// renamed and a new one is inserted.
169 /// 3. Otherwise, if the existing function has the correct prototype, return
170 /// the existing function.
171 /// 4. Finally, the function exists but has the wrong prototype: return the
172 /// function with a constantexpr cast to the right prototype.
173 Constant *getOrInsertFunction(const std::string &Name, const FunctionType *T);
175 /// getOrInsertFunction - Look up the specified function in the module symbol
176 /// table. If it does not exist, add a prototype for the function and return
177 /// it. This function guarantees to return a constant of pointer to the
178 /// specified function type or a ConstantExpr BitCast of that type if the
179 /// named /// function has a different type. This version of the method
180 /// takes a null terminated list of function arguments, which makes it
181 /// easier for clients to use.
182 Constant *getOrInsertFunction(const std::string &Name, const Type *RetTy,...)
185 /// getFunction - Look up the specified function in the module symbol table.
186 /// If it does not exist, return null.
187 Function *getFunction(const std::string &Name) const;
190 /// @name Global Variable Accessors
193 /// getGlobalVariable - Look up the specified global variable in the module
194 /// symbol table. If it does not exist, return null. The type argument
195 /// should be the underlying type of the global, i.e., it should not have
196 /// the top-level PointerType, which represents the address of the global.
197 /// If AllowInternal is set to true, this function will return types that
198 /// have InternalLinkage. By default, these types are not returned.
199 GlobalVariable *getGlobalVariable(const std::string &Name,
200 bool AllowInternal = false) const;
202 /// getNamedGlobal - Return the first global variable in the module with the
203 /// specified name, of arbitrary type. This method returns null if a global
204 /// with the specified name is not found.
205 GlobalVariable *getNamedGlobal(const std::string &Name) const {
206 return getGlobalVariable(Name, true);
210 /// @name Type Accessors
213 /// addTypeName - Insert an entry in the symbol table mapping Str to Type. If
214 /// there is already an entry for this name, true is returned and the symbol
215 /// table is not modified.
216 bool addTypeName(const std::string &Name, const Type *Ty);
218 /// getTypeName - If there is at least one entry in the symbol table for the
219 /// specified type, return it.
220 std::string getTypeName(const Type *Ty) const;
222 /// getTypeByName - Return the type with the specified name in this module, or
223 /// null if there is none by that name.
224 const Type *getTypeByName(const std::string &Name) const;
227 /// @name Direct access to the globals list, functions list, and symbol table
230 /// Get the Module's list of global variables (constant).
231 const GlobalListType &getGlobalList() const { return GlobalList; }
232 /// Get the Module's list of global variables.
233 GlobalListType &getGlobalList() { return GlobalList; }
234 /// Get the Module's list of functions (constant).
235 const FunctionListType &getFunctionList() const { return FunctionList; }
236 /// Get the Module's list of functions.
237 FunctionListType &getFunctionList() { return FunctionList; }
238 /// Get the symbol table of global variable and function identifiers
239 const ValueSymbolTable &getValueSymbolTable() const { return *ValSymTab; }
240 /// Get the Module's symbol table of global variable and function identifiers.
241 ValueSymbolTable &getValueSymbolTable() { return *ValSymTab; }
242 /// Get the symbol table of types
243 const TypeSymbolTable &getTypeSymbolTable() const { return *TypeSymTab; }
244 /// Get the Module's symbol table of types
245 TypeSymbolTable &getTypeSymbolTable() { return *TypeSymTab; }
248 /// @name Global Variable Iteration
251 /// Get an iterator to the first global variable
252 global_iterator global_begin() { return GlobalList.begin(); }
253 /// Get a constant iterator to the first global variable
254 const_global_iterator global_begin() const { return GlobalList.begin(); }
255 /// Get an iterator to the last global variable
256 global_iterator global_end () { return GlobalList.end(); }
257 /// Get a constant iterator to the last global variable
258 const_global_iterator global_end () const { return GlobalList.end(); }
259 /// Determine if the list of globals is empty.
260 bool global_empty() const { return GlobalList.empty(); }
263 /// @name Function Iteration
266 /// Get an iterator to the first function.
267 iterator begin() { return FunctionList.begin(); }
268 /// Get a constant iterator to the first function.
269 const_iterator begin() const { return FunctionList.begin(); }
270 /// Get an iterator to the last function.
271 iterator end () { return FunctionList.end(); }
272 /// Get a constant iterator to the last function.
273 const_iterator end () const { return FunctionList.end(); }
274 /// Determine how many functions are in the Module's list of functions.
275 size_t size() const { return FunctionList.size(); }
276 /// Determine if the list of functions is empty.
277 bool empty() const { return FunctionList.empty(); }
280 /// @name Dependent Library Iteration
283 /// @brief Get a constant iterator to beginning of dependent library list.
284 inline lib_iterator lib_begin() const { return LibraryList.begin(); }
285 /// @brief Get a constant iterator to end of dependent library list.
286 inline lib_iterator lib_end() const { return LibraryList.end(); }
287 /// @brief Returns the number of items in the list of libraries.
288 inline size_t lib_size() const { return LibraryList.size(); }
289 /// @brief Add a library to the list of dependent libraries
290 void addLibrary(const std::string& Lib);
291 /// @brief Remove a library from the list of dependent libraries
292 void removeLibrary(const std::string& Lib);
293 /// @brief Get all the libraries
294 inline const LibraryListType& getLibraries() const { return LibraryList; }
297 /// @name Utility functions for printing and dumping Module objects
300 /// Print the module to an output stream
301 void print(std::ostream &OS) const { print(OS, 0); }
302 void print(std::ostream *OS) const { if (OS) print(*OS); }
303 /// Print the module to an output stream with AssemblyAnnotationWriter.
304 void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
305 void print(std::ostream *OS, AssemblyAnnotationWriter *AAW) const {
306 if (OS) print(*OS, AAW);
308 /// Dump the module to std::cerr (for debugging).
310 /// This function causes all the subinstructions to "let go" of all references
311 /// that they are maintaining. This allows one to 'delete' a whole class at
312 /// a time, even though there may be circular references... first all
313 /// references are dropped, and all use counts go to zero. Then everything
314 /// is delete'd for real. Note that no operations are valid on an object
315 /// that has "dropped all references", except operator delete.
316 void dropAllReferences();
319 static unsigned getFunctionListOffset() {
321 return unsigned(reinterpret_cast<uintptr_t>(&Obj->FunctionList));
323 static unsigned getGlobalVariableListOffset() {
325 return unsigned(reinterpret_cast<uintptr_t>(&Obj->GlobalList));
329 /// An iostream inserter for modules.
330 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
335 inline ValueSymbolTable *
336 ilist_traits<Function>::getSymTab(Module *M) {
337 return M ? &M->getValueSymbolTable() : 0;
340 inline ValueSymbolTable *
341 ilist_traits<GlobalVariable>::getSymTab(Module *M) {
342 return M ? &M->getValueSymbolTable() : 0;
346 ilist_traits<Function>::getListOffset() {
347 return Module::getFunctionListOffset();
351 ilist_traits<GlobalVariable>::getListOffset() {
352 return Module::getGlobalVariableListOffset();
355 } // End llvm namespace