blob: 5018db3dd3a47eb5718c2437fcdddefebea2fd59 [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
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;
Logan1f028c02010-11-27 01:02:48 +080053
54 class Compiler {
Logande2ca792010-11-27 22:15:39 +080055 private:
Logan1f028c02010-11-27 01:02:48 +080056 //////////////////////////////////////////////////////////////////////////
57 // The variable section below (e.g., Triple, CodeGenOptLevel)
58 // is initialized in GlobalInitialization()
59 //
60 static bool GlobalInitialized;
61
Logan1f028c02010-11-27 01:02:48 +080062 // If given, this will be the name of the target triple to compile for.
63 // If not given, the initial values defined in this file will be used.
64 static std::string Triple;
65
66 static llvm::CodeGenOpt::Level CodeGenOptLevel;
67
68 // End of section of GlobalInitializing variables
69 /////////////////////////////////////////////////////////////////////////
70 // If given, the name of the target CPU to generate code for.
71 static std::string CPU;
72
73 // The list of target specific features to enable or disable -- this should
74 // be a list of strings starting with '+' (enable) or '-' (disable).
75 static std::vector<std::string> Features;
76
Logan1f028c02010-11-27 01:02:48 +080077 static void LLVMErrorHandler(void *UserData, const std::string &Message);
78
79 static const llvm::StringRef PragmaMetadataName;
80 static const llvm::StringRef ExportVarMetadataName;
81 static const llvm::StringRef ExportFuncMetadataName;
Stephen Hines071288a2011-01-27 14:38:26 -080082 static const llvm::StringRef ObjectSlotMetadataName;
Logan1f028c02010-11-27 01:02:48 +080083
84 friend class CodeEmitter;
85 friend class CodeMemoryManager;
86
Logande2ca792010-11-27 22:15:39 +080087
Logan1f028c02010-11-27 01:02:48 +080088 private:
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -070089 const char *mCachePath;
Shih-wei Liao898c5a92011-05-18 07:02:39 -070090
Logan2a6dc822011-01-06 04:05:20 +080091 ScriptCompiled *mpResult;
92
Logan1f028c02010-11-27 01:02:48 +080093 std::string mError;
94
Logan Chienda5e0c32011-06-13 03:47:21 +080095#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +080096 // The memory manager for code emitter
97 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +080098
99 // The CodeEmitter
100 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan Chienda5e0c32011-06-13 03:47:21 +0800101#endif
102
103#if USE_MCJIT
104 // Compilation buffer for MCJIT
105 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
106
107 // Loaded and relocated executable
108 RSExecRef mRSExecutable;
Shih-wei Liao90cd3d12011-06-20 15:43:34 -0700109
110#if USE_DISASSEMBLER
111 void Disassemble(llvm::Target const *Target,
112 llvm::TargetMachine *TM,
113 std::string const &name,
114 unsigned char const *code,
115 size_t size);
116#endif
Logan Chienda5e0c32011-06-13 03:47:21 +0800117#endif
Logan1f028c02010-11-27 01:02:48 +0800118
119 BCCSymbolLookupFn mpSymbolLookupFn;
120 void *mpSymbolLookupContext;
121
122 llvm::LLVMContext *mContext;
123 llvm::Module *mModule;
124
125 bool mHasLinked;
126
127 public:
Logan2a6dc822011-01-06 04:05:20 +0800128 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800129
Loganecf4cbd2011-01-06 05:34:11 +0800130 static void GlobalInitialization();
131
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700132 void setCachePath(const char *cachePath) {
133 mCachePath = cachePath;
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700134 return;
135 }
136
Loganf340bf72011-01-14 17:51:40 +0800137 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logan1f028c02010-11-27 01:02:48 +0800138 mpSymbolLookupFn = pFn;
139 mpSymbolLookupContext = pContext;
140 }
141
Logan Chienda5e0c32011-06-13 03:47:21 +0800142#if USE_OLD_JIT
Logande2ca792010-11-27 22:15:39 +0800143 CodeMemoryManager *createCodeMemoryManager();
144
145 CodeEmitter *createCodeEmitter();
Logan Chienda5e0c32011-06-13 03:47:21 +0800146#endif
Logande2ca792010-11-27 22:15:39 +0800147
Logan Chienda5e0c32011-06-13 03:47:21 +0800148#if USE_MCJIT
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700149 bool getObjPath(std::string &objPath);
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700150
Logan Chienda5e0c32011-06-13 03:47:21 +0800151 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700152
153 const llvm::SmallVector<char, 1024> &getELF() const {
154 return mEmittedELFExecutable;
155 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800156#endif
157
Logan474cbd22011-01-31 01:47:44 +0800158 llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM);
159
Logan1f028c02010-11-27 01:02:48 +0800160 int readModule(llvm::Module *module) {
Logan1f028c02010-11-27 01:02:48 +0800161 mModule = module;
162 return hasError();
163 }
164
Logan474cbd22011-01-31 01:47:44 +0800165 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800166
Logan1f028c02010-11-27 01:02:48 +0800167 int compile();
168
Logan38d06072010-12-29 00:16:39 +0800169 char const *getErrorMessage() {
170 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800171 }
172
Logan1f028c02010-11-27 01:02:48 +0800173 const llvm::Module *getModule() const {
174 return mModule;
175 }
176
177 ~Compiler();
178
179 private:
Logande2ca792010-11-27 22:15:39 +0800180
Logan Chienda5e0c32011-06-13 03:47:21 +0800181 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
182 llvm::NamedMDNode const *ExportVarMetadata,
183 llvm::NamedMDNode const *ExportFuncMetadata);
184
185 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
186 llvm::NamedMDNode const *ExportVarMetadata,
187 llvm::NamedMDNode const *ExportFuncMetadata);
188
189#if USE_MCJIT
190 static void *resolveSymbolAdapter(void *context, char const *name);
191#endif
192
193 int runLTO(llvm::TargetData *TD,
194 llvm::NamedMDNode const *ExportVarMetadata,
195 llvm::NamedMDNode const *ExportFuncMetadata);
196
197 int writeELFExecToFile();
Logan Chien4cc00332011-06-12 14:00:46 +0800198
Logande2ca792010-11-27 22:15:39 +0800199 bool hasError() const {
200 return !mError.empty();
201 }
202
203 void setError(const char *Error) {
204 mError.assign(Error); // Copying
205 }
206
207 void setError(const std::string &Error) {
208 mError = Error;
209 }
210
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800211 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800212
Logan9b504eb2010-11-27 18:19:35 +0800213} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800214
215#endif // BCC_COMPILER_H