blob: e76f900adb657285b1c30ecb4456f50f824af856 [file] [log] [blame]
Logan1f028c02010-11-27 01:02:48 +08001/*
2 * Copyright 2010, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef BCC_COMPILER_H
18#define BCC_COMPILER_H
19
20#include <bcc/bcc.h>
21
Loganc4395232010-11-27 18:54:17 +080022#include "CodeEmitter.h"
23#include "CodeMemoryManager.h"
Logan1f028c02010-11-27 01:02:48 +080024
Shih-wei Liao320b5492011-06-20 22:53:33 -070025#if USE_MCJIT
Logan Chienda5e0c32011-06-13 03:47:21 +080026#include "librsloader.h"
Shih-wei Liao320b5492011-06-20 22:53:33 -070027#endif
Logan Chienda5e0c32011-06-13 03:47:21 +080028
Logan Chien9347e0b2011-07-07 19:51:47 +080029#if USE_DISASSEMBLER
30#include "Disassembler/Disassembler.h"
31#endif
32
Logan1f028c02010-11-27 01:02:48 +080033#include "llvm/ADT/OwningPtr.h"
34#include "llvm/ADT/StringRef.h"
Logan Chienda5e0c32011-06-13 03:47:21 +080035#include "llvm/ADT/SmallVector.h"
Logan1f028c02010-11-27 01:02:48 +080036#include "llvm/Target/TargetMachine.h"
37
38#include <stddef.h>
39
40#include <list>
41#include <string>
42#include <vector>
43#include <utility>
44
45
Logan1f028c02010-11-27 01:02:48 +080046namespace llvm {
47 class LLVMContext;
48 class Module;
Logan474cbd22011-01-31 01:47:44 +080049 class MemoryBuffer;
Logan Chien4cc00332011-06-12 14:00:46 +080050 class NamedMDNode;
51 class TargetData;
Logan1f028c02010-11-27 01:02:48 +080052}
53
54
55namespace bcc {
Logan2a6dc822011-01-06 04:05:20 +080056 class ScriptCompiled;
Logan1f028c02010-11-27 01:02:48 +080057
58 class Compiler {
Logande2ca792010-11-27 22:15:39 +080059 private:
Logan1f028c02010-11-27 01:02:48 +080060 //////////////////////////////////////////////////////////////////////////
61 // The variable section below (e.g., Triple, CodeGenOptLevel)
62 // is initialized in GlobalInitialization()
63 //
64 static bool GlobalInitialized;
65
Logan1f028c02010-11-27 01:02:48 +080066 // If given, this will be the name of the target triple to compile for.
67 // If not given, the initial values defined in this file will be used.
68 static std::string Triple;
69
70 static llvm::CodeGenOpt::Level CodeGenOptLevel;
71
72 // End of section of GlobalInitializing variables
73 /////////////////////////////////////////////////////////////////////////
74 // If given, the name of the target CPU to generate code for.
75 static std::string CPU;
76
77 // The list of target specific features to enable or disable -- this should
78 // be a list of strings starting with '+' (enable) or '-' (disable).
79 static std::vector<std::string> Features;
80
Logan1f028c02010-11-27 01:02:48 +080081 static void LLVMErrorHandler(void *UserData, const std::string &Message);
82
83 static const llvm::StringRef PragmaMetadataName;
84 static const llvm::StringRef ExportVarMetadataName;
85 static const llvm::StringRef ExportFuncMetadataName;
Stephen Hines071288a2011-01-27 14:38:26 -080086 static const llvm::StringRef ObjectSlotMetadataName;
Logan1f028c02010-11-27 01:02:48 +080087
88 friend class CodeEmitter;
89 friend class CodeMemoryManager;
90
Logande2ca792010-11-27 22:15:39 +080091
Logan1f028c02010-11-27 01:02:48 +080092 private:
Logan2a6dc822011-01-06 04:05:20 +080093 ScriptCompiled *mpResult;
94
Logan1f028c02010-11-27 01:02:48 +080095 std::string mError;
96
Logan Chienda5e0c32011-06-13 03:47:21 +080097#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +080098 // The memory manager for code emitter
99 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +0800100
101 // The CodeEmitter
102 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan Chienda5e0c32011-06-13 03:47:21 +0800103#endif
104
105#if USE_MCJIT
106 // Compilation buffer for MCJIT
107 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
108
109 // Loaded and relocated executable
110 RSExecRef mRSExecutable;
111#endif
Logan1f028c02010-11-27 01:02:48 +0800112
113 BCCSymbolLookupFn mpSymbolLookupFn;
114 void *mpSymbolLookupContext;
115
116 llvm::LLVMContext *mContext;
117 llvm::Module *mModule;
118
119 bool mHasLinked;
120
121 public:
Logan2a6dc822011-01-06 04:05:20 +0800122 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800123
Loganecf4cbd2011-01-06 05:34:11 +0800124 static void GlobalInitialization();
125
Logan Chien9347e0b2011-07-07 19:51:47 +0800126 static std::string const &getTargetTriple() {
127 return Triple;
128 }
129
Loganf340bf72011-01-14 17:51:40 +0800130 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logan1f028c02010-11-27 01:02:48 +0800131 mpSymbolLookupFn = pFn;
132 mpSymbolLookupContext = pContext;
133 }
134
Logan Chienda5e0c32011-06-13 03:47:21 +0800135#if USE_OLD_JIT
Logande2ca792010-11-27 22:15:39 +0800136 CodeMemoryManager *createCodeMemoryManager();
137
138 CodeEmitter *createCodeEmitter();
Logan Chienda5e0c32011-06-13 03:47:21 +0800139#endif
Logande2ca792010-11-27 22:15:39 +0800140
Logan Chienda5e0c32011-06-13 03:47:21 +0800141#if USE_MCJIT
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700142 bool getObjPath(std::string &objPath);
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700143
Logan Chienda5e0c32011-06-13 03:47:21 +0800144 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700145
146 const llvm::SmallVector<char, 1024> &getELF() const {
147 return mEmittedELFExecutable;
148 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800149#endif
150
Logan474cbd22011-01-31 01:47:44 +0800151 llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM);
152
Logan1f028c02010-11-27 01:02:48 +0800153 int readModule(llvm::Module *module) {
Logan1f028c02010-11-27 01:02:48 +0800154 mModule = module;
155 return hasError();
156 }
157
Logan474cbd22011-01-31 01:47:44 +0800158 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800159
Logan1f028c02010-11-27 01:02:48 +0800160 int compile();
161
Logan38d06072010-12-29 00:16:39 +0800162 char const *getErrorMessage() {
163 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800164 }
165
Logan1f028c02010-11-27 01:02:48 +0800166 const llvm::Module *getModule() const {
167 return mModule;
168 }
169
170 ~Compiler();
171
172 private:
Logande2ca792010-11-27 22:15:39 +0800173
Logan Chienda5e0c32011-06-13 03:47:21 +0800174 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
175 llvm::NamedMDNode const *ExportVarMetadata,
176 llvm::NamedMDNode const *ExportFuncMetadata);
177
178 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
179 llvm::NamedMDNode const *ExportVarMetadata,
180 llvm::NamedMDNode const *ExportFuncMetadata);
181
182#if USE_MCJIT
183 static void *resolveSymbolAdapter(void *context, char const *name);
184#endif
185
186 int runLTO(llvm::TargetData *TD,
187 llvm::NamedMDNode const *ExportVarMetadata,
188 llvm::NamedMDNode const *ExportFuncMetadata);
189
Logande2ca792010-11-27 22:15:39 +0800190 bool hasError() const {
191 return !mError.empty();
192 }
193
194 void setError(const char *Error) {
195 mError.assign(Error); // Copying
196 }
197
198 void setError(const std::string &Error) {
199 mError = Error;
200 }
201
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800202 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800203
Logan9b504eb2010-11-27 18:19:35 +0800204} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800205
206#endif // BCC_COMPILER_H