Make some headway towards compiling all of LLVM.
[oota-llvm.git] / tools / lto / LTOModule.h
1 //===-LTOModule.h - LLVM Link Time Optimizer ------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the LTOModule class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LTO_MODULE_H
15 #define LTO_MODULE_H
16
17 #include "llvm/Module.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/Target/Mangler.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/ADT/OwningPtr.h"
22 #include "llvm/ADT/StringMap.h"
23
24 #include "llvm-c/lto.h"
25
26 #include <vector>
27 #include <string>
28
29
30 // Forward references to llvm classes.
31 namespace llvm {
32   class Function;
33   class GlobalValue;
34   class MemoryBuffer;
35   class Value;
36 }
37
38 //
39 // C++ class which implements the opaque lto_module_t type.
40 //
41 struct LTOModule {
42 private:
43   typedef llvm::StringMap<uint8_t> StringSet;
44
45   struct NameAndAttributes {
46     const char        *name;
47     uint32_t           attributes;
48     bool               isFunction;
49     llvm::GlobalValue *symbol;
50   };
51
52   llvm::OwningPtr<llvm::Module>           _module;
53   llvm::OwningPtr<llvm::TargetMachine>    _target;
54   std::vector<NameAndAttributes>          _symbols;
55
56   // _defines and _undefines only needed to disambiguate tentative definitions
57   StringSet                               _defines;
58   llvm::StringMap<NameAndAttributes>      _undefines;
59   std::vector<const char*>                _asm_undefines;
60   llvm::MCContext                         _context;
61
62   // Use mangler to add GlobalPrefix to names to match linker names.
63   llvm::Mangler                           _mangler;
64
65   LTOModule(llvm::Module *m, llvm::TargetMachine *t);
66 public:
67   /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM
68   /// bitcode.
69   static bool isBitcodeFile(const void *mem, size_t length);
70   static bool isBitcodeFile(const char *path);
71
72   /// isBitcodeFileForTarget - Returns 'true' if the file or memory contents
73   /// is LLVM bitcode for the specified triple.
74   static bool isBitcodeFileForTarget(const void *mem,
75                                      size_t length,
76                                      const char *triplePrefix);
77   static bool isBitcodeFileForTarget(const char *path,
78                                      const char *triplePrefix);
79
80   /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership
81   /// of the buffer.
82   static LTOModule *makeLTOModule(const char* path,
83                                   std::string &errMsg);
84   static LTOModule *makeLTOModule(int fd, const char *path,
85                                   size_t size, std::string &errMsg);
86   static LTOModule *makeLTOModule(int fd, const char *path,
87                                   size_t file_size,
88                                   size_t map_size,
89                                   off_t offset,
90                                   std::string& errMsg);
91   static LTOModule *makeLTOModule(const void *mem, size_t length,
92                                   std::string &errMsg);
93
94   /// getTargetTriple - Return the Module's target triple.
95   const char *getTargetTriple() {
96     return _module->getTargetTriple().c_str();
97   }
98
99   /// setTargetTriple - Set the Module's target triple.
100   void setTargetTriple(const char *triple) {
101     _module->setTargetTriple(triple);
102   }
103
104   /// getSymbolCount - Get the number of symbols
105   uint32_t getSymbolCount() {
106     return _symbols.size();
107   }
108
109   /// getSymbolAttributes - Get the attributes for a symbol at the specified
110   /// index.
111   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
112     if (index < _symbols.size())
113       return lto_symbol_attributes(_symbols[index].attributes);
114     return lto_symbol_attributes(0);
115   }
116
117   /// getSymbolName - Get the name of the symbol at the specified index.
118   const char *getSymbolName(uint32_t index) {
119     if (index < _symbols.size())
120       return _symbols[index].name;
121     return NULL;
122   }
123
124   /// getLLVVMModule - Return the Module.
125   llvm::Module *getLLVVMModule() { return _module.get(); }
126
127   /// getAsmUndefinedRefs -
128   const std::vector<const char*> &getAsmUndefinedRefs() {
129     return _asm_undefines;
130   }
131
132 private:
133   /// parseSymbols - Parse the symbols from the module and model-level ASM and
134   /// add them to either the defined or undefined lists.
135   bool parseSymbols(std::string &errMsg);
136
137   /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet
138   /// to a list to be resolved later.
139   void addPotentialUndefinedSymbol(llvm::GlobalValue *dcl, bool isFunc);
140
141   /// addDefinedSymbol - Add a defined symbol to the list.
142   void addDefinedSymbol(llvm::GlobalValue *def, bool isFunction);
143
144   /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
145   void addDefinedFunctionSymbol(llvm::Function *f);
146
147   /// addDefinedDataSymbol - Add a data symbol as defined to the list.
148   void addDefinedDataSymbol(llvm::GlobalValue *v);
149
150   /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
151   /// defined or undefined lists.
152   bool addAsmGlobalSymbols(std::string &errMsg);
153
154   /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
155   /// defined list.
156   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
157
158   /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
159   /// the undefined list.
160   void addAsmGlobalSymbolUndef(const char *);
161
162   /// addObjCClass - Parse i386/ppc ObjC class data structure.
163   void addObjCClass(llvm::GlobalVariable *clgv);
164
165   /// addObjCCategory - Parse i386/ppc ObjC category data structure.
166   void addObjCCategory(llvm::GlobalVariable *clgv);
167
168   /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
169   void addObjCClassRef(llvm::GlobalVariable *clgv);
170
171   /// objcClassNameFromExpression - Get string that the data pointer points
172   /// to.
173   bool objcClassNameFromExpression(llvm::Constant* c, std::string &name);
174
175   /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
176   /// target triple.
177   static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
178                             const char *triplePrefix);
179
180   /// makeLTOModule - Create an LTOModule (private version). N.B. This
181   /// method takes ownership of the buffer.
182   static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
183                                   std::string &errMsg);
184
185   /// makeBuffer - Create a MemoryBuffer from a memory range.
186   static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length);
187 };
188
189 #endif // LTO_MODULE_H