• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ADT/OwningPtr.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "llvm/ADT/StringMap.h"
21 
22 #include "llvm-c/lto.h"
23 
24 #include <vector>
25 #include <string>
26 
27 
28 // forward references to llvm classes
29 namespace llvm {
30     class Mangler;
31     class MemoryBuffer;
32     class GlobalValue;
33     class Value;
34     class Function;
35 }
36 
37 
38 //
39 // C++ class which implements the opaque lto_module_t
40 //
41 struct LTOModule {
42 
43     static bool              isBitcodeFile(const void* mem, size_t length);
44     static bool              isBitcodeFile(const char* path);
45 
46     static bool              isBitcodeFileForTarget(const void* mem,
47                                     size_t length, const char* triplePrefix);
48 
49     static bool              isBitcodeFileForTarget(const char* path,
50                                                     const char* triplePrefix);
51 
52     static LTOModule*        makeLTOModule(const char* path,
53                                           std::string& errMsg);
54     static LTOModule*        makeLTOModule(int fd, const char *path,
55                                            size_t size,
56                                            std::string& errMsg);
57     static LTOModule*        makeLTOModule(int fd, const char *path,
58                                            size_t file_size,
59                                            size_t map_size,
60                                            off_t offset,
61                                            std::string& errMsg);
62     static LTOModule*        makeLTOModule(const void* mem, size_t length,
63                                            std::string& errMsg);
64 
65     const char*              getTargetTriple();
66     void                     setTargetTriple(const char*);
67     uint32_t                 getSymbolCount();
68     lto_symbol_attributes    getSymbolAttributes(uint32_t index);
69     const char*              getSymbolName(uint32_t index);
70 
getLLVVMModuleLTOModule71     llvm::Module *           getLLVVMModule() { return _module.get(); }
getAsmUndefinedRefsLTOModule72     const std::vector<const char*> &getAsmUndefinedRefs() {
73             return _asm_undefines;
74     }
75 
76 private:
77                             LTOModule(llvm::Module* m, llvm::TargetMachine* t);
78 
79     bool                    ParseSymbols(std::string &errMsg);
80     void                    addDefinedSymbol(llvm::GlobalValue* def,
81                                                     llvm::Mangler& mangler,
82                                                     bool isFunction);
83     void                    addPotentialUndefinedSymbol(llvm::GlobalValue* decl,
84                                                         llvm::Mangler &mangler);
85     void                    addDefinedFunctionSymbol(llvm::Function* f,
86                                                         llvm::Mangler &mangler);
87     void                    addDefinedDataSymbol(llvm::GlobalValue* v,
88                                                         llvm::Mangler &mangler);
89     bool                    addAsmGlobalSymbols(llvm::MCContext &Context,
90                                                 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 };
122 
123 #endif // LTO_MODULE_H
124 
125