blob: dd2a50c87c04339f48f3e4b86e2eec612ce89db5 [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;
107#endif
Logan1f028c02010-11-27 01:02:48 +0800108
109 BCCSymbolLookupFn mpSymbolLookupFn;
110 void *mpSymbolLookupContext;
111
112 llvm::LLVMContext *mContext;
113 llvm::Module *mModule;
114
115 bool mHasLinked;
116
117 public:
Logan2a6dc822011-01-06 04:05:20 +0800118 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800119
Loganecf4cbd2011-01-06 05:34:11 +0800120 static void GlobalInitialization();
121
Shih-wei Liao5c00f4f2011-05-20 04:14:54 -0700122 void setCachePath(const char *cachePath) {
123 mCachePath = cachePath;
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700124 return;
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
Shih-wei Liaode0ba062011-05-19 03:16:33 -0700139 bool getObjPath(std::string &objPath);
Shih-wei Liao898c5a92011-05-18 07:02:39 -0700140
Logan Chienda5e0c32011-06-13 03:47:21 +0800141 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700142
143 const llvm::SmallVector<char, 1024> &getELF() const {
144 return mEmittedELFExecutable;
145 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800146#endif
147
Logan474cbd22011-01-31 01:47:44 +0800148 llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM);
149
Logan1f028c02010-11-27 01:02:48 +0800150 int readModule(llvm::Module *module) {
Logan1f028c02010-11-27 01:02:48 +0800151 mModule = module;
152 return hasError();
153 }
154
Logan474cbd22011-01-31 01:47:44 +0800155 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800156
Logan1f028c02010-11-27 01:02:48 +0800157 int compile();
158
Logan38d06072010-12-29 00:16:39 +0800159 char const *getErrorMessage() {
160 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800161 }
162
Logan1f028c02010-11-27 01:02:48 +0800163 const llvm::Module *getModule() const {
164 return mModule;
165 }
166
167 ~Compiler();
168
169 private:
Logande2ca792010-11-27 22:15:39 +0800170
Logan Chienda5e0c32011-06-13 03:47:21 +0800171 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
172 llvm::NamedMDNode const *ExportVarMetadata,
173 llvm::NamedMDNode const *ExportFuncMetadata);
174
175 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
176 llvm::NamedMDNode const *ExportVarMetadata,
177 llvm::NamedMDNode const *ExportFuncMetadata);
178
179#if USE_MCJIT
180 static void *resolveSymbolAdapter(void *context, char const *name);
181#endif
182
183 int runLTO(llvm::TargetData *TD,
184 llvm::NamedMDNode const *ExportVarMetadata,
185 llvm::NamedMDNode const *ExportFuncMetadata);
186
187 int writeELFExecToFile();
Logan Chien4cc00332011-06-12 14:00:46 +0800188
Logande2ca792010-11-27 22:15:39 +0800189 bool hasError() const {
190 return !mError.empty();
191 }
192
193 void setError(const char *Error) {
194 mError.assign(Error); // Copying
195 }
196
197 void setError(const std::string &Error) {
198 mError = Error;
199 }
200
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800201 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800202
Logan9b504eb2010-11-27 18:19:35 +0800203} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800204
205#endif // BCC_COMPILER_H