blob: 8cc3d2ea943fd4ed6994b50f850e0429a0988176 [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"
Shih-wei Liao4deffde2012-01-17 20:38:17 +080032#include "llvm/ADT/Triple.h"
Logan1f028c02010-11-27 01:02:48 +080033#include "llvm/Target/TargetMachine.h"
34
35#include <stddef.h>
36
37#include <list>
38#include <string>
39#include <vector>
40#include <utility>
41
42
Logan1f028c02010-11-27 01:02:48 +080043namespace llvm {
Logan1f028c02010-11-27 01:02:48 +080044 class Module;
Logan Chien4cc00332011-06-12 14:00:46 +080045 class NamedMDNode;
46 class TargetData;
Logan1f028c02010-11-27 01:02:48 +080047}
48
49
50namespace bcc {
Logan2a6dc822011-01-06 04:05:20 +080051 class ScriptCompiled;
Zonr Chang2fcbd022012-01-06 21:04:31 +080052 struct CompilerOption;
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;
Shih-wei Liao4deffde2012-01-17 20:38:17 +080065 static llvm::Triple::ArchType ArchType;
Logan1f028c02010-11-27 01:02:48 +080066
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 Hinescc366e52012-02-21 17:22:04 -080083 static const llvm::StringRef ExportForEachNameMetadataName;
84 static const llvm::StringRef ExportForEachMetadataName;
Stephen Hines071288a2011-01-27 14:38:26 -080085 static const llvm::StringRef ObjectSlotMetadataName;
Logan1f028c02010-11-27 01:02:48 +080086
87 friend class CodeEmitter;
88 friend class CodeMemoryManager;
89
Logande2ca792010-11-27 22:15:39 +080090
Logan1f028c02010-11-27 01:02:48 +080091 private:
Logan2a6dc822011-01-06 04:05:20 +080092 ScriptCompiled *mpResult;
93
Logan1f028c02010-11-27 01:02:48 +080094 std::string mError;
95
Logan Chienda5e0c32011-06-13 03:47:21 +080096#if USE_OLD_JIT
Logan1f028c02010-11-27 01:02:48 +080097 // The memory manager for code emitter
98 llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr;
Logan1f028c02010-11-27 01:02:48 +080099
100 // The CodeEmitter
101 llvm::OwningPtr<CodeEmitter> mCodeEmitter;
Logan Chienda5e0c32011-06-13 03:47:21 +0800102#endif
103
104#if USE_MCJIT
105 // Compilation buffer for MCJIT
106 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
107
108 // Loaded and relocated executable
109 RSExecRef mRSExecutable;
110#endif
Logan1f028c02010-11-27 01:02:48 +0800111
112 BCCSymbolLookupFn mpSymbolLookupFn;
113 void *mpSymbolLookupContext;
114
Logan1f028c02010-11-27 01:02:48 +0800115 llvm::Module *mModule;
116
117 bool mHasLinked;
118
119 public:
Logan2a6dc822011-01-06 04:05:20 +0800120 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800121
Loganecf4cbd2011-01-06 05:34:11 +0800122 static void GlobalInitialization();
123
Logan Chien9347e0b2011-07-07 19:51:47 +0800124 static std::string const &getTargetTriple() {
125 return Triple;
126 }
127
Shih-wei Liao4deffde2012-01-17 20:38:17 +0800128 static llvm::Triple::ArchType getTargetArchType() {
129 return ArchType;
130 }
131
Loganf340bf72011-01-14 17:51:40 +0800132 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logan1f028c02010-11-27 01:02:48 +0800133 mpSymbolLookupFn = pFn;
134 mpSymbolLookupContext = pContext;
135 }
136
Logan Chienda5e0c32011-06-13 03:47:21 +0800137#if USE_OLD_JIT
Logande2ca792010-11-27 22:15:39 +0800138 CodeMemoryManager *createCodeMemoryManager();
139
140 CodeEmitter *createCodeEmitter();
Logan Chienda5e0c32011-06-13 03:47:21 +0800141#endif
Logande2ca792010-11-27 22:15:39 +0800142
Logan Chienda5e0c32011-06-13 03:47:21 +0800143#if USE_MCJIT
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
Logan1f028c02010-11-27 01:02:48 +0800151 int readModule(llvm::Module *module) {
Logan1f028c02010-11-27 01:02:48 +0800152 mModule = module;
153 return hasError();
154 }
155
Logan474cbd22011-01-31 01:47:44 +0800156 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800157
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800158 int compile(const CompilerOption &option);
Logan1f028c02010-11-27 01:02:48 +0800159
Logan38d06072010-12-29 00:16:39 +0800160 char const *getErrorMessage() {
161 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800162 }
163
Logan1f028c02010-11-27 01:02:48 +0800164 const llvm::Module *getModule() const {
165 return mModule;
166 }
167
168 ~Compiler();
169
170 private:
Logande2ca792010-11-27 22:15:39 +0800171
Logan Chienda5e0c32011-06-13 03:47:21 +0800172 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
173 llvm::NamedMDNode const *ExportVarMetadata,
174 llvm::NamedMDNode const *ExportFuncMetadata);
175
Joseph Wen34c600a2011-07-25 17:59:17 -0700176 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
Logan Chienda5e0c32011-06-13 03:47:21 +0800177
178#if USE_MCJIT
179 static void *resolveSymbolAdapter(void *context, char const *name);
180#endif
Stephen Hinescc366e52012-02-21 17:22:04 -0800181 int runInternalPasses(std::vector<std::string>& Names,
182 std::vector<uint32_t>& Signatures);
Logan Chienda5e0c32011-06-13 03:47:21 +0800183
184 int runLTO(llvm::TargetData *TD,
185 llvm::NamedMDNode const *ExportVarMetadata,
Stephen Hinescc366e52012-02-21 17:22:04 -0800186 llvm::NamedMDNode const *ExportFuncMetadata,
187 std::vector<std::string>& ForEachExpandList);
Logan Chienda5e0c32011-06-13 03:47:21 +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