blob: 2d4faf7fb4fce214136061091592a15fcf8907b5 [file] [log] [blame]
Logan1f028c02010-11-27 01:02:48 +08001/*
Stephen Hinesdb169182012-01-05 18:46:36 -08002 * Copyright 2010-2012, The Android Open Source Project
Logan1f028c02010-11-27 01:02:48 +08003 *
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
Logan Chien8d3b5e12011-07-12 16:19:21 +080022#include "CodeGen/CodeEmitter.h"
23#include "CodeGen/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
Logan1f028c02010-11-27 01:02:48 +080029#include "llvm/ADT/OwningPtr.h"
30#include "llvm/ADT/StringRef.h"
Logan Chienda5e0c32011-06-13 03:47:21 +080031#include "llvm/ADT/SmallVector.h"
Logan1f028c02010-11-27 01:02:48 +080032#include "llvm/Target/TargetMachine.h"
33
34#include <stddef.h>
35
36#include <list>
37#include <string>
38#include <vector>
39#include <utility>
40
41
Logan1f028c02010-11-27 01:02:48 +080042namespace llvm {
43 class LLVMContext;
44 class Module;
Logan474cbd22011-01-31 01:47:44 +080045 class MemoryBuffer;
Logan Chien4cc00332011-06-12 14:00:46 +080046 class NamedMDNode;
47 class TargetData;
Logan1f028c02010-11-27 01:02:48 +080048}
49
50
51namespace bcc {
Logan2a6dc822011-01-06 04:05:20 +080052 class ScriptCompiled;
Zonr Chang2fcbd022012-01-06 21:04:31 +080053 struct CompilerOption;
Logan1f028c02010-11-27 01:02:48 +080054
55 class Compiler {
Logande2ca792010-11-27 22:15:39 +080056 private:
Logan1f028c02010-11-27 01:02:48 +080057 //////////////////////////////////////////////////////////////////////////
58 // The variable section below (e.g., Triple, CodeGenOptLevel)
59 // is initialized in GlobalInitialization()
60 //
61 static bool GlobalInitialized;
62
Logan1f028c02010-11-27 01:02:48 +080063 // If given, this will be the name of the target triple to compile for.
64 // If not given, the initial values defined in this file will be used.
65 static std::string Triple;
66
67 static llvm::CodeGenOpt::Level CodeGenOptLevel;
68
69 // End of section of GlobalInitializing variables
70 /////////////////////////////////////////////////////////////////////////
71 // If given, the name of the target CPU to generate code for.
72 static std::string CPU;
73
74 // The list of target specific features to enable or disable -- this should
75 // be a list of strings starting with '+' (enable) or '-' (disable).
76 static std::vector<std::string> Features;
77
Logan1f028c02010-11-27 01:02:48 +080078 static void LLVMErrorHandler(void *UserData, const std::string &Message);
79
80 static const llvm::StringRef PragmaMetadataName;
81 static const llvm::StringRef ExportVarMetadataName;
82 static const llvm::StringRef ExportFuncMetadataName;
Stephen Hines071288a2011-01-27 14:38:26 -080083 static const llvm::StringRef ObjectSlotMetadataName;
Logan1f028c02010-11-27 01:02:48 +080084
85 friend class CodeEmitter;
86 friend class CodeMemoryManager;
87
Logande2ca792010-11-27 22:15:39 +080088
Logan1f028c02010-11-27 01:02:48 +080089 private:
Logan2a6dc822011-01-06 04:05:20 +080090 ScriptCompiled *mpResult;
91
Logan1f028c02010-11-27 01:02:48 +080092 std::string mError;
93
Logan Chienda5e0c32011-06-13 03:47:21 +080094#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +080095 // The memory manager for code emitter
96 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +080097
98 // The CodeEmitter
99 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan Chienda5e0c32011-06-13 03:47:21 +0800100#endif
101
102#if USE_MCJIT
103 // Compilation buffer for MCJIT
104 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
105
106 // Loaded and relocated executable
107 RSExecRef mRSExecutable;
108#endif
Logan1f028c02010-11-27 01:02:48 +0800109
110 BCCSymbolLookupFn mpSymbolLookupFn;
111 void *mpSymbolLookupContext;
112
113 llvm::LLVMContext *mContext;
114 llvm::Module *mModule;
115
116 bool mHasLinked;
117
118 public:
Logan2a6dc822011-01-06 04:05:20 +0800119 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800120
Loganecf4cbd2011-01-06 05:34:11 +0800121 static void GlobalInitialization();
122
Logan Chien9347e0b2011-07-07 19:51:47 +0800123 static std::string const &getTargetTriple() {
124 return Triple;
125 }
126
Loganf340bf72011-01-14 17:51:40 +0800127 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logan1f028c02010-11-27 01:02:48 +0800128 mpSymbolLookupFn = pFn;
129 mpSymbolLookupContext = pContext;
130 }
131
Logan Chienda5e0c32011-06-13 03:47:21 +0800132#if USE_OLD_JIT
Logande2ca792010-11-27 22:15:39 +0800133 CodeMemoryManager *createCodeMemoryManager();
134
135 CodeEmitter *createCodeEmitter();
Logan Chienda5e0c32011-06-13 03:47:21 +0800136#endif
Logande2ca792010-11-27 22:15:39 +0800137
Logan Chienda5e0c32011-06-13 03:47:21 +0800138#if USE_MCJIT
Logan Chienda5e0c32011-06-13 03:47:21 +0800139 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700140
141 const llvm::SmallVector<char, 1024> &getELF() const {
142 return mEmittedELFExecutable;
143 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800144#endif
145
Logan474cbd22011-01-31 01:47:44 +0800146 llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM);
147
Logan1f028c02010-11-27 01:02:48 +0800148 int readModule(llvm::Module *module) {
Logan1f028c02010-11-27 01:02:48 +0800149 mModule = module;
150 return hasError();
151 }
152
Logan474cbd22011-01-31 01:47:44 +0800153 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800154
Zonr Chang2fcbd022012-01-06 21:04:31 +0800155 int compile(CompilerOption &option);
Logan1f028c02010-11-27 01:02:48 +0800156
Logan38d06072010-12-29 00:16:39 +0800157 char const *getErrorMessage() {
158 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800159 }
160
Logan1f028c02010-11-27 01:02:48 +0800161 const llvm::Module *getModule() const {
162 return mModule;
163 }
164
165 ~Compiler();
166
167 private:
Logande2ca792010-11-27 22:15:39 +0800168
Logan Chienda5e0c32011-06-13 03:47:21 +0800169 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
170 llvm::NamedMDNode const *ExportVarMetadata,
171 llvm::NamedMDNode const *ExportFuncMetadata);
172
Joseph Wen34c600a2011-07-25 17:59:17 -0700173 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
Logan Chienda5e0c32011-06-13 03:47:21 +0800174
175#if USE_MCJIT
176 static void *resolveSymbolAdapter(void *context, char const *name);
177#endif
Stephen Hinesdb169182012-01-05 18:46:36 -0800178 int runInternalPasses();
Logan Chienda5e0c32011-06-13 03:47:21 +0800179
180 int runLTO(llvm::TargetData *TD,
181 llvm::NamedMDNode const *ExportVarMetadata,
182 llvm::NamedMDNode const *ExportFuncMetadata);
183
Logande2ca792010-11-27 22:15:39 +0800184 bool hasError() const {
185 return !mError.empty();
186 }
187
188 void setError(const char *Error) {
189 mError.assign(Error); // Copying
190 }
191
192 void setError(const std::string &Error) {
193 mError = Error;
194 }
195
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800196 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800197
Logan9b504eb2010-11-27 18:19:35 +0800198} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800199
200#endif // BCC_COMPILER_H