Change inline asms to be uniqued like constants, not embedded in a Module.
[oota-llvm.git] / include / llvm / Module.h
1 //===-- llvm/Module.h - C++ class to represent a VM module ------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the declarations for the Module class that is used to
11 // maintain all the information related to a VM module.
12 //
13 // A module also maintains a GlobalValRefMap object that is used to hold all
14 // constant references to global variables in the module.  When a global
15 // variable is destroyed, it should have no entries in the GlobalValueRefMap.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #ifndef LLVM_MODULE_H
20 #define LLVM_MODULE_H
21
22 #include "llvm/Function.h"
23 #include "llvm/GlobalVariable.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/Support/DataTypes.h"
26
27 namespace llvm {
28
29 class GlobalVariable;
30 class GlobalValueRefMap;   // Used by ConstantVals.cpp
31 class FunctionType;
32 class SymbolTable;
33
34 template<> struct ilist_traits<Function>
35   : public SymbolTableListTraits<Function, Module, Module> {
36   // createSentinel is used to create a node that marks the end of the list.
37   static Function *createSentinel();
38   static void destroySentinel(Function *F) { delete F; }
39   static iplist<Function> &getList(Module *M);
40 };
41 template<> struct ilist_traits<GlobalVariable>
42   : public SymbolTableListTraits<GlobalVariable, Module, Module> {
43   // createSentinel is used to create a node that marks the end of the list.
44   static GlobalVariable *createSentinel();
45   static void destroySentinel(GlobalVariable *GV) { delete GV; }
46   static iplist<GlobalVariable> &getList(Module *M);
47 };
48
49 class Module {
50 public:
51   typedef iplist<GlobalVariable> GlobalListType;
52   typedef iplist<Function> FunctionListType;
53   typedef SetVector<std::string> LibraryListType;
54
55   // Global Variable iterators.
56   typedef GlobalListType::iterator                     global_iterator;
57   typedef GlobalListType::const_iterator         const_global_iterator;
58
59   // Function iterators.
60   typedef FunctionListType::iterator                          iterator;
61   typedef FunctionListType::const_iterator              const_iterator;
62
63   // Library list iterators.
64   typedef LibraryListType::const_iterator lib_iterator;
65
66   enum Endianness  { AnyEndianness, LittleEndian, BigEndian };
67   enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
68
69 private:
70   GlobalListType GlobalList;     // The Global Variables in the module
71   FunctionListType FunctionList; // The Functions in the module
72   LibraryListType LibraryList;   // The Libraries needed by the module
73   std::string GlobalScopeAsm;    // Inline Asm at global scope.
74   SymbolTable *SymTab;           // Symbol Table for the module
75   std::string ModuleID;          // Human readable identifier for the module
76   std::string TargetTriple;      // Platform target triple Module compiled on
77   Endianness  Endian;     // Endianness assumed in the module
78   PointerSize PtrSize;    // Pointer size assumed in the module
79
80   friend class Constant;
81
82 public:
83   Module(const std::string &ModuleID);
84   ~Module();
85
86   const std::string &getModuleIdentifier() const { return ModuleID; }
87   void setModuleIdentifier(const std::string &ID) { ModuleID = ID; }
88
89   const std::string &getTargetTriple() const { return TargetTriple; }
90   void setTargetTriple(const std::string &T) { TargetTriple = T; }
91
92   /// Target endian information...
93   Endianness getEndianness() const { return Endian; }
94   void setEndianness(Endianness E) { Endian = E; }
95
96   /// Target Pointer Size information...
97   PointerSize getPointerSize() const { return PtrSize; }
98   void setPointerSize(PointerSize PS) { PtrSize = PS; }
99
100   // Access to any module-scope inline asm blocks.
101   const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
102   void setModuleInlineAsm(const std::string &Asm) { GlobalScopeAsm = Asm; }
103   
104   //===--------------------------------------------------------------------===//
105   // Methods for easy access to the functions in the module.
106   //
107
108   /// getOrInsertFunction - Look up the specified function in the module symbol
109   /// table.  If it does not exist, add a prototype for the function and return
110   /// it.
111   Function *getOrInsertFunction(const std::string &Name, const FunctionType *T);
112
113   /// getOrInsertFunction - Look up the specified function in the module symbol
114   /// table.  If it does not exist, add a prototype for the function and return
115   /// it.  This version of the method takes a null terminated list of function
116   /// arguments, which makes it easier for clients to use.
117   Function *getOrInsertFunction(const std::string &Name, const Type *RetTy,...)
118     END_WITH_NULL;
119
120   /// getFunction - Look up the specified function in the module symbol table.
121   /// If it does not exist, return null.
122   ///
123   Function *getFunction(const std::string &Name, const FunctionType *Ty);
124
125   /// getMainFunction - This function looks up main efficiently.  This is such a
126   /// common case, that it is a method in Module.  If main cannot be found, a
127   /// null pointer is returned.
128   ///
129   Function *getMainFunction();
130
131   /// getNamedFunction - Return the first function in the module with the
132   /// specified name, of arbitrary type.  This method returns null if a function
133   /// with the specified name is not found.
134   ///
135   Function *getNamedFunction(const std::string &Name);
136
137   //===--------------------------------------------------------------------===//
138   // Methods for easy access to the global variables in the module.
139   //
140
141   /// getGlobalVariable - Look up the specified global variable in the module
142   /// symbol table.  If it does not exist, return null.  The type argument
143   /// should be the underlying type of the global, i.e., it should not have
144   /// the top-level PointerType, which represents the address of the global.
145   /// If AllowInternal is set to true, this function will return types that
146   /// have InternalLinkage. By default, these types are not returned.
147   ///
148   GlobalVariable *getGlobalVariable(const std::string &Name, const Type *Ty,
149                                     bool AllowInternal = false);
150
151
152   //===--------------------------------------------------------------------===//
153   // Methods for easy access to the types in the module.
154   //
155
156   /// addTypeName - Insert an entry in the symbol table mapping Str to Type.  If
157   /// there is already an entry for this name, true is returned and the symbol
158   /// table is not modified.
159   ///
160   bool addTypeName(const std::string &Name, const Type *Ty);
161
162   /// getTypeName - If there is at least one entry in the symbol table for the
163   /// specified type, return it.
164   ///
165   std::string getTypeName(const Type *Ty) const;
166
167   /// getTypeByName - Return the type with the specified name in this module, or
168   /// null if there is none by that name.
169   const Type *getTypeByName(const std::string &Name) const;
170
171
172   //===--------------------------------------------------------------------===//
173   // Methods for direct access to the globals list, functions list, and symbol
174   // table.
175   //
176
177   // Get the underlying elements of the Module.
178   const GlobalListType &getGlobalList() const       { return GlobalList; }
179         GlobalListType &getGlobalList()             { return GlobalList; }
180   const FunctionListType &getFunctionList() const   { return FunctionList; }
181         FunctionListType &getFunctionList()         { return FunctionList; }
182
183   /// getSymbolTable() - Get access to the symbol table for the module, where
184   /// global variables and functions are identified.
185   ///
186         SymbolTable &getSymbolTable()       { return *SymTab; }
187   const SymbolTable &getSymbolTable() const { return *SymTab; }
188
189
190   //===--------------------------------------------------------------------===//
191   // Module iterator forwarding functions
192   //
193   // Globals list interface
194   global_iterator       global_begin()       { return GlobalList.begin(); }
195   const_global_iterator global_begin() const { return GlobalList.begin(); }
196   global_iterator       global_end  ()       { return GlobalList.end(); }
197   const_global_iterator global_end  () const { return GlobalList.end(); }
198   bool                  global_empty() const { return GlobalList.empty(); }
199
200   // FunctionList interface
201   iterator                begin()       { return FunctionList.begin(); }
202   const_iterator          begin() const { return FunctionList.begin(); }
203   iterator                end  ()       { return FunctionList.end();   }
204   const_iterator          end  () const { return FunctionList.end();   }
205
206   size_t                   size() const { return FunctionList.size(); }
207   bool                    empty() const { return FunctionList.empty(); }
208
209   //===--------------------------------------------------------------------===//
210   // List of dependent library access functions
211
212   /// @brief Get a constant iterator to beginning of dependent library list.
213   inline lib_iterator lib_begin() const { return LibraryList.begin(); }
214
215   /// @brief Get a constant iterator to end of dependent library list.
216   inline lib_iterator lib_end() const { return LibraryList.end(); }
217
218   /// @brief Returns the number of items in the list of libraries.
219   inline size_t lib_size() const { return LibraryList.size(); }
220
221   /// @brief Add a library to the list of dependent libraries
222   inline void addLibrary(const std::string& Lib){ LibraryList.insert(Lib); }
223
224   /// @brief Remove a library from the list of dependent libraries
225   inline void removeLibrary(const std::string& Lib) { LibraryList.remove(Lib); }
226
227   /// @brief Get all the libraries
228   inline const LibraryListType& getLibraries() const { return LibraryList; }
229
230   //===--------------------------------------------------------------------===//
231   // Utility functions for printing and dumping Module objects
232
233   void print(std::ostream &OS) const { print(OS, 0); }
234   void print(std::ostream &OS, AssemblyAnnotationWriter *AAW) const;
235
236   void dump() const;
237
238   /// dropAllReferences() - This function causes all the subinstructions to "let
239   /// go" of all references that they are maintaining.  This allows one to
240   /// 'delete' a whole class at a time, even though there may be circular
241   /// references... first all references are dropped, and all use counts go to
242   /// zero.  Then everything is delete'd for real.  Note that no operations are
243   /// valid on an object that has "dropped all references", except operator
244   /// delete.
245   ///
246   void dropAllReferences();
247 };
248
249 inline std::ostream &operator<<(std::ostream &O, const Module &M) {
250   M.print(O);
251   return O;
252 }
253
254 } // End llvm namespace
255
256 #endif