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