blob: ecd7235506a8008bf23d554b15d7d4b52b25caad [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
Logan Chienda5e0c32011-06-13 03:47:21 +080025#include "librsloader.h"
26
Logan1f028c02010-11-27 01:02:48 +080027#include "llvm/ADT/OwningPtr.h"
28#include "llvm/ADT/StringRef.h"
Logan Chienda5e0c32011-06-13 03:47:21 +080029#include "llvm/ADT/SmallVector.h"
Logan1f028c02010-11-27 01:02:48 +080030#include "llvm/Target/TargetMachine.h"
31
32#include <stddef.h>
33
34#include <list>
35#include <string>
36#include <vector>
37#include <utility>
38
39
Logan1f028c02010-11-27 01:02:48 +080040namespace llvm {
41 class LLVMContext;
42 class Module;
Logan474cbd22011-01-31 01:47:44 +080043 class MemoryBuffer;
Logan Chien4cc00332011-06-12 14:00:46 +080044 class NamedMDNode;
45 class TargetData;
Logan1f028c02010-11-27 01:02:48 +080046}
47
48
49namespace bcc {
Logan2a6dc822011-01-06 04:05:20 +080050 class ScriptCompiled;
Logan1f028c02010-11-27 01:02:48 +080051
52 class Compiler {
Logande2ca792010-11-27 22:15:39 +080053 private:
Logan1f028c02010-11-27 01:02:48 +080054 //////////////////////////////////////////////////////////////////////////
55 // The variable section below (e.g., Triple, CodeGenOptLevel)
56 // is initialized in GlobalInitialization()
57 //
58 static bool GlobalInitialized;
59
Logan1f028c02010-11-27 01:02:48 +080060 // If given, this will be the name of the target triple to compile for.
61 // If not given, the initial values defined in this file will be used.
62 static std::string Triple;
63
64 static llvm::CodeGenOpt::Level CodeGenOptLevel;
65
66 // End of section of GlobalInitializing variables
67 /////////////////////////////////////////////////////////////////////////
68 // If given, the name of the target CPU to generate code for.
69 static std::string CPU;
70
71 // The list of target specific features to enable or disable -- this should
72 // be a list of strings starting with '+' (enable) or '-' (disable).
73 static std::vector<std::string> Features;
74
Logan1f028c02010-11-27 01:02:48 +080075 static void LLVMErrorHandler(void *UserData, const std::string &Message);
76
77 static const llvm::StringRef PragmaMetadataName;
78 static const llvm::StringRef ExportVarMetadataName;
79 static const llvm::StringRef ExportFuncMetadataName;
Stephen Hines071288a2011-01-27 14:38:26 -080080 static const llvm::StringRef ObjectSlotMetadataName;
Logan1f028c02010-11-27 01:02:48 +080081
82 friend class CodeEmitter;
83 friend class CodeMemoryManager;
84
Logande2ca792010-11-27 22:15:39 +080085
Logan1f028c02010-11-27 01:02:48 +080086 private:
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -070087 const char *mCachePath;
Shih-wei Liao898c5a92011-05-18 07:02:39 -070088
Logan2a6dc822011-01-06 04:05:20 +080089 ScriptCompiled *mpResult;
90
Logan1f028c02010-11-27 01:02:48 +080091 std::string mError;
92
Logan Chienda5e0c32011-06-13 03:47:21 +080093#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +080094 // The memory manager for code emitter
95 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +080096
97 // The CodeEmitter
98 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan Chienda5e0c32011-06-13 03:47:21 +080099#endif
100
101#if USE_MCJIT
102 // Compilation buffer for MCJIT
103 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
104
105 // Loaded and relocated executable
106 RSExecRef mRSExecutable;
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700107
108#if USE_DISASSEMBLER
109 void Disassemble(llvm::Target const *Target,
110 llvm::TargetMachine *TM,
111 std::string const &name,
112 unsigned char const *code,
113 size_t size);
114#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800115#endif
Logan1f028c02010-11-27 01:02:48 +0800116
117 BCCSymbolLookupFn mpSymbolLookupFn;
118 void *mpSymbolLookupContext;
119
120 llvm::LLVMContext *mContext;
121 llvm::Module *mModule;
122
123 bool mHasLinked;
124
125 public:
Logan2a6dc822011-01-06 04:05:20 +0800126 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800127
Loganecf4cbd2011-01-06 05:34:11 +0800128 static void GlobalInitialization();
129
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700130 void setCachePath(const char *cachePath) {
131 mCachePath = cachePath;
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700132 return;
133 }
134
Loganf340bf72011-01-14 17:51:40 +0800135 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logan1f028c02010-11-27 01:02:48 +0800136 mpSymbolLookupFn = pFn;
137 mpSymbolLookupContext = pContext;
138 }
139
Logan Chienda5e0c32011-06-13 03:47:21 +0800140#if USE_OLD_JIT
Logande2ca792010-11-27 22:15:39 +0800141 CodeMemoryManager *createCodeMemoryManager();
142
143 CodeEmitter *createCodeEmitter();
Logan Chienda5e0c32011-06-13 03:47:21 +0800144#endif
Logande2ca792010-11-27 22:15:39 +0800145
Logan Chienda5e0c32011-06-13 03:47:21 +0800146#if USE_MCJIT
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700147 bool getObjPath(std::string &objPath);
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700148
Logan Chienda5e0c32011-06-13 03:47:21 +0800149 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700150
151 const llvm::SmallVector<char, 1024> &getELF() const {
152 return mEmittedELFExecutable;
153 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800154#endif
155
Logan474cbd22011-01-31 01:47:44 +0800156 llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM);
157
Logan1f028c02010-11-27 01:02:48 +0800158 int readModule(llvm::Module *module) {
Logan1f028c02010-11-27 01:02:48 +0800159 mModule = module;
160 return hasError();
161 }
162
Logan474cbd22011-01-31 01:47:44 +0800163 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800164
Logan1f028c02010-11-27 01:02:48 +0800165 int compile();
166
Logan38d06072010-12-29 00:16:39 +0800167 char const *getErrorMessage() {
168 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800169 }
170
Logan1f028c02010-11-27 01:02:48 +0800171 const llvm::Module *getModule() const {
172 return mModule;
173 }
174
175 ~Compiler();
176
177 private:
Logande2ca792010-11-27 22:15:39 +0800178
Logan Chienda5e0c32011-06-13 03:47:21 +0800179 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
180 llvm::NamedMDNode const *ExportVarMetadata,
181 llvm::NamedMDNode const *ExportFuncMetadata);
182
183 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
184 llvm::NamedMDNode const *ExportVarMetadata,
185 llvm::NamedMDNode const *ExportFuncMetadata);
186
187#if USE_MCJIT
188 static void *resolveSymbolAdapter(void *context, char const *name);
189#endif
190
191 int runLTO(llvm::TargetData *TD,
192 llvm::NamedMDNode const *ExportVarMetadata,
193 llvm::NamedMDNode const *ExportFuncMetadata);
194
195 int writeELFExecToFile();
Logan Chien4cc00332011-06-12 14:00:46 +0800196
Logande2ca792010-11-27 22:15:39 +0800197 bool hasError() const {
198 return !mError.empty();
199 }
200
201 void setError(const char *Error) {
202 mError.assign(Error); // Copying
203 }
204
205 void setError(const std::string &Error) {
206 mError = Error;
207 }
208
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800209 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800210
Logan9b504eb2010-11-27 18:19:35 +0800211} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800212
213#endif // BCC_COMPILER_H