c5f83ee3021cea0cd86e4c1a80f90e03d3999bcf
[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 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                                              bool isFunction);
83     void                    addPotentialUndefinedSymbol(llvm::GlobalValue* decl);
84     void                    addDefinedFunctionSymbol(llvm::Function* f);
85   void                    addDefinedDataSymbol(llvm::GlobalValue* v);
86     bool                    addAsmGlobalSymbols(std::string &errMsg);
87     void                    addAsmGlobalSymbol(const char *,
88                                                lto_symbol_attributes scope);
89     void                    addAsmGlobalSymbolUndef(const char *);
90     void                    addObjCClass(llvm::GlobalVariable* clgv);
91     void                    addObjCCategory(llvm::GlobalVariable* clgv);
92     void                    addObjCClassRef(llvm::GlobalVariable* clgv);
93     bool                    objcClassNameFromExpression(llvm::Constant* c, 
94                                                     std::string& name);
95
96     static bool             isTargetMatch(llvm::MemoryBuffer* memBuffer,
97                                                     const char* triplePrefix);
98
99     static LTOModule*       makeLTOModule(llvm::MemoryBuffer* buffer,
100                                                         std::string& errMsg);
101     static llvm::MemoryBuffer* makeBuffer(const void* mem, size_t length);
102
103     typedef llvm::StringMap<uint8_t> StringSet;
104     
105     struct NameAndAttributes { 
106         const char*            name; 
107         lto_symbol_attributes  attributes; 
108     };
109
110     llvm::OwningPtr<llvm::Module>           _module;
111     llvm::OwningPtr<llvm::TargetMachine>    _target;
112     std::vector<NameAndAttributes>          _symbols;
113     // _defines and _undefines only needed to disambiguate tentative definitions
114     StringSet                               _defines;    
115     llvm::StringMap<NameAndAttributes>      _undefines;
116     std::vector<const char*>                _asm_undefines;
117     llvm::MCContext                         _context;
118     llvm::Mangler                           _mangler;
119 };
120
121 #endif // LTO_MODULE_H
122