Refactor the MCContext so that it's an ivar instead of a local which is passed
[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/TargetMachine.h"
20 #include "llvm/ADT/OwningPtr.h"
21 #include "llvm/ADT/StringMap.h"
22
23 #include "llvm-c/lto.h"
24
25 #include <vector>
26 #include <string>
27
28
29 // forward references to llvm classes
30 namespace llvm {
31     class Mangler;
32     class MemoryBuffer;
33     class GlobalValue;
34     class Value;
35     class Function;
36 }
37
38
39 //
40 // C++ class which implements the opaque lto_module_t
41 //
42 struct LTOModule {
43
44     static bool              isBitcodeFile(const void* mem, size_t length);
45     static bool              isBitcodeFile(const char* path);
46
47     static bool              isBitcodeFileForTarget(const void* mem, 
48                                     size_t length, const char* triplePrefix);
49
50     static bool              isBitcodeFileForTarget(const char* path, 
51                                                     const char* triplePrefix);
52
53     static LTOModule*        makeLTOModule(const char* path,
54                                           std::string& errMsg);
55     static LTOModule*        makeLTOModule(int fd, const char *path,
56                                            size_t size,
57                                            std::string& errMsg);
58     static LTOModule*        makeLTOModule(int fd, const char *path,
59                                            size_t file_size,
60                                            size_t map_size,
61                                            off_t offset,
62                                            std::string& errMsg);
63     static LTOModule*        makeLTOModule(const void* mem, size_t length,
64                                            std::string& errMsg);
65
66     const char*              getTargetTriple();
67     void                     setTargetTriple(const char*);
68     uint32_t                 getSymbolCount();
69     lto_symbol_attributes    getSymbolAttributes(uint32_t index);
70     const char*              getSymbolName(uint32_t index);
71     
72     llvm::Module *           getLLVVMModule() { return _module.get(); }
73     const std::vector<const char*> &getAsmUndefinedRefs() {
74             return _asm_undefines;
75     }
76
77 private:
78                             LTOModule(llvm::Module* m, llvm::TargetMachine* t);
79
80     bool                    ParseSymbols(std::string &errMsg);
81     void                    addDefinedSymbol(llvm::GlobalValue* def, 
82                                                     llvm::Mangler& mangler, 
83                                                     bool isFunction);
84     void                    addPotentialUndefinedSymbol(llvm::GlobalValue* decl, 
85                                                         llvm::Mangler &mangler);
86     void                    addDefinedFunctionSymbol(llvm::Function* f, 
87                                                      llvm::Mangler &mangler);
88     void                    addDefinedDataSymbol(llvm::GlobalValue* v, 
89                                                  llvm::Mangler &mangler);
90     bool                    addAsmGlobalSymbols(std::string &errMsg);
91     void                    addAsmGlobalSymbol(const char *,
92                                                lto_symbol_attributes scope);
93     void                    addAsmGlobalSymbolUndef(const char *);
94     void                    addObjCClass(llvm::GlobalVariable* clgv);
95     void                    addObjCCategory(llvm::GlobalVariable* clgv);
96     void                    addObjCClassRef(llvm::GlobalVariable* clgv);
97     bool                    objcClassNameFromExpression(llvm::Constant* c, 
98                                                     std::string& name);
99
100     static bool             isTargetMatch(llvm::MemoryBuffer* memBuffer,
101                                                     const char* triplePrefix);
102
103     static LTOModule*       makeLTOModule(llvm::MemoryBuffer* buffer,
104                                                         std::string& errMsg);
105     static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
106
107     typedef llvm::StringMap<uint8_t> StringSet;
108     
109     struct NameAndAttributes { 
110         const char*            name; 
111         lto_symbol_attributes  attributes; 
112     };
113
114     llvm::OwningPtr<llvm::Module>           _module;
115     llvm::OwningPtr<llvm::TargetMachine>    _target;
116     std::vector<NameAndAttributes>          _symbols;
117     // _defines and _undefines only needed to disambiguate tentative definitions
118     StringSet                               _defines;    
119     llvm::StringMap<NameAndAttributes>      _undefines;
120     std::vector<const char*>                _asm_undefines;
121     llvm::MCContext                         _context;
122 };
123
124 #endif // LTO_MODULE_H
125