Cleanup some whitespaces.
[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     enum name_type { IsFunction, IsData };
47     const char*            name;
48     lto_symbol_attributes  attributes;
49   };
50
51   llvm::OwningPtr<llvm::Module>           _module;
52   llvm::OwningPtr<llvm::TargetMachine>    _target;
53   std::vector<NameAndAttributes>          _symbols;
54
55   // _defines and _undefines only needed to disambiguate tentative definitions
56   StringSet                               _defines;    
57   llvm::StringMap<NameAndAttributes>      _undefines;
58   std::vector<const char*>                _asm_undefines;
59   llvm::MCContext                         _context;
60
61   // Use mangler to add GlobalPrefix to names to match linker names.
62   llvm::Mangler                           _mangler;
63
64   LTOModule(llvm::Module *m, llvm::TargetMachine *t);
65 public:
66   /// isBitcodeFile - Returns 'true' if the file or memory contents is LLVM
67   /// bitcode.
68   static bool isBitcodeFile(const void *mem, size_t length);
69   static bool isBitcodeFile(const char *path);
70
71   /// isBitcodeFileForTarget - Returns 'true' if the file or memory contents
72   /// is LLVM bitcode for the specified triple.
73   static bool isBitcodeFileForTarget(const void *mem, 
74                                      size_t length,
75                                      const char *triplePrefix);
76   static bool isBitcodeFileForTarget(const char *path, 
77                                      const char *triplePrefix);
78
79   /// makeLTOModule - Create an LTOModule. N.B. These methods take ownership
80   /// of the buffer.
81   static LTOModule *makeLTOModule(const char* path,
82                                   std::string &errMsg);
83   static LTOModule *makeLTOModule(int fd, const char *path,
84                                   size_t size, std::string &errMsg);
85   static LTOModule *makeLTOModule(int fd, const char *path,
86                                   size_t file_size,
87                                   size_t map_size,
88                                   off_t offset,
89                                   std::string& errMsg);
90   static LTOModule *makeLTOModule(const void *mem, size_t length,
91                                   std::string &errMsg);
92
93   /// getTargetTriple - Return the Module's target triple.
94   const char *getTargetTriple() {
95     return _module->getTargetTriple().c_str();
96   }
97
98   /// setTargetTriple - Set the Module's target triple.
99   void setTargetTriple(const char *triple) {
100     _module->setTargetTriple(triple);
101   }
102
103   /// getSymbolCount - Get the number of symbols
104   uint32_t getSymbolCount() {
105     return _symbols.size();
106   }
107
108   /// getSymbolAttributes - Get the attributes for a symbol at the specified
109   /// index.
110   lto_symbol_attributes getSymbolAttributes(uint32_t index) {
111     if (index < _symbols.size())
112       return _symbols[index].attributes;
113     else
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     else
122       return NULL;
123   }
124
125   /// getLLVVMModule - Return the Module.
126   llvm::Module *getLLVVMModule() { return _module.get(); }
127
128   /// getAsmUndefinedRefs -
129   const std::vector<const char*> &getAsmUndefinedRefs() {
130     return _asm_undefines;
131   }
132
133 private:
134   /// parseSymbols - Parse the symbols from the module and model-level ASM and
135   /// add them to either the defined or undefined lists.
136   bool parseSymbols(std::string &errMsg);
137
138   /// addPotentialUndefinedSymbol - Add a symbol which isn't defined just yet
139   /// to a list to be resolved later.
140   void addPotentialUndefinedSymbol(llvm::GlobalValue *dcl);
141
142   /// addDefinedSymbol - Add a defined symbol to the list.
143   void addDefinedSymbol(llvm::GlobalValue *def, bool isFunction);
144
145   /// addDefinedFunctionSymbol - Add a function symbol as defined to the list.
146   void addDefinedFunctionSymbol(llvm::Function *f);
147
148   /// addDefinedDataSymbol - Add a data symbol as defined to the list.
149   void addDefinedDataSymbol(llvm::GlobalValue *v);
150
151   /// addAsmGlobalSymbols - Add global symbols from module-level ASM to the
152   /// defined or undefined lists.
153   bool addAsmGlobalSymbols(std::string &errMsg);
154
155   /// addAsmGlobalSymbol - Add a global symbol from module-level ASM to the
156   /// defined list.
157   void addAsmGlobalSymbol(const char *, lto_symbol_attributes scope);
158
159   /// addAsmGlobalSymbolUndef - Add a global symbol from module-level ASM to
160   /// the undefined list.
161   void addAsmGlobalSymbolUndef(const char *);
162
163   /// addObjCClass - Parse i386/ppc ObjC class data structure.
164   void addObjCClass(llvm::GlobalVariable *clgv);
165
166   /// addObjCCategory - Parse i386/ppc ObjC category data structure.
167   void addObjCCategory(llvm::GlobalVariable *clgv);
168
169   /// addObjCClassRef - Parse i386/ppc ObjC class list data structure.
170   void addObjCClassRef(llvm::GlobalVariable *clgv);
171
172   /// objcClassNameFromExpression - Get string that the data pointer points
173   /// to.
174   bool objcClassNameFromExpression(llvm::Constant* c, std::string &name);
175
176   /// isTargetMatch - Returns 'true' if the memory buffer is for the specified
177   /// target triple.
178   static bool isTargetMatch(llvm::MemoryBuffer *memBuffer,
179                             const char *triplePrefix);
180
181   /// makeLTOModule - Create an LTOModule (private version). N.B. This
182   /// method takes ownership of the buffer.
183   static LTOModule *makeLTOModule(llvm::MemoryBuffer *buffer,
184                                   std::string &errMsg);
185
186   /// makeBuffer - Create a MemoryBuffer from a memory range.
187   static llvm::MemoryBuffer *makeBuffer(const void *mem, size_t length);
188 };
189
190 #endif // LTO_MODULE_H