blob: 301daed22af9b902347ff912f5d86462bd54edc1 [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
Stephen Hines9ddeb6b2012-03-11 18:41:03 -070022#include <Config.h>
Logan1f028c02010-11-27 01:02:48 +080023
Shih-wei Liao320b5492011-06-20 22:53:33 -070024#if USE_MCJIT
Logan Chienda5e0c32011-06-13 03:47:21 +080025#include "librsloader.h"
Shih-wei Liao320b5492011-06-20 22:53:33 -070026#endif
Logan Chienda5e0c32011-06-13 03:47:21 +080027
Logan1f028c02010-11-27 01:02:48 +080028#include "llvm/ADT/OwningPtr.h"
29#include "llvm/ADT/StringRef.h"
Logan Chienda5e0c32011-06-13 03:47:21 +080030#include "llvm/ADT/SmallVector.h"
Shih-wei Liao4deffde2012-01-17 20:38:17 +080031#include "llvm/ADT/Triple.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 {
Logan1f028c02010-11-27 01:02:48 +080043 class Module;
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;
Zonr Chang2fcbd022012-01-06 21:04:31 +080051 struct CompilerOption;
Logan1f028c02010-11-27 01:02:48 +080052
53 class Compiler {
Logande2ca792010-11-27 22:15:39 +080054 private:
Logan1f028c02010-11-27 01:02:48 +080055 //////////////////////////////////////////////////////////////////////////
56 // The variable section below (e.g., Triple, CodeGenOptLevel)
57 // is initialized in GlobalInitialization()
58 //
59 static bool GlobalInitialized;
60
Logan1f028c02010-11-27 01:02:48 +080061 // If given, this will be the name of the target triple to compile for.
62 // If not given, the initial values defined in this file will be used.
63 static std::string Triple;
Shih-wei Liao4deffde2012-01-17 20:38:17 +080064 static llvm::Triple::ArchType ArchType;
Logan1f028c02010-11-27 01:02:48 +080065
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
Logan1f028c02010-11-27 01:02:48 +080079 friend class CodeEmitter;
80 friend class CodeMemoryManager;
81
82 private:
Logan2a6dc822011-01-06 04:05:20 +080083 ScriptCompiled *mpResult;
84
Logan1f028c02010-11-27 01:02:48 +080085 std::string mError;
86
Logan Chienda5e0c32011-06-13 03:47:21 +080087#if USE_MCJIT
88 // Compilation buffer for MCJIT
89 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
90
91 // Loaded and relocated executable
92 RSExecRef mRSExecutable;
93#endif
Logan1f028c02010-11-27 01:02:48 +080094
95 BCCSymbolLookupFn mpSymbolLookupFn;
96 void *mpSymbolLookupContext;
97
Logan1f028c02010-11-27 01:02:48 +080098 llvm::Module *mModule;
99
100 bool mHasLinked;
101
102 public:
Logan2a6dc822011-01-06 04:05:20 +0800103 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800104
Loganecf4cbd2011-01-06 05:34:11 +0800105 static void GlobalInitialization();
106
Logan Chien9347e0b2011-07-07 19:51:47 +0800107 static std::string const &getTargetTriple() {
108 return Triple;
109 }
110
Shih-wei Liao4deffde2012-01-17 20:38:17 +0800111 static llvm::Triple::ArchType getTargetArchType() {
112 return ArchType;
113 }
114
Loganf340bf72011-01-14 17:51:40 +0800115 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logan1f028c02010-11-27 01:02:48 +0800116 mpSymbolLookupFn = pFn;
117 mpSymbolLookupContext = pContext;
118 }
119
Logan Chienda5e0c32011-06-13 03:47:21 +0800120#if USE_MCJIT
Logan Chienda5e0c32011-06-13 03:47:21 +0800121 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700122
123 const llvm::SmallVector<char, 1024> &getELF() const {
124 return mEmittedELFExecutable;
125 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800126#endif
127
Logan1f028c02010-11-27 01:02:48 +0800128 int readModule(llvm::Module *module) {
Logan1f028c02010-11-27 01:02:48 +0800129 mModule = module;
130 return hasError();
131 }
132
Logan474cbd22011-01-31 01:47:44 +0800133 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800134
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800135 int compile(const CompilerOption &option);
Logan1f028c02010-11-27 01:02:48 +0800136
Logan38d06072010-12-29 00:16:39 +0800137 char const *getErrorMessage() {
138 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800139 }
140
Logan1f028c02010-11-27 01:02:48 +0800141 const llvm::Module *getModule() const {
142 return mModule;
143 }
144
145 ~Compiler();
146
147 private:
Logande2ca792010-11-27 22:15:39 +0800148
Logan Chienda5e0c32011-06-13 03:47:21 +0800149 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
150 llvm::NamedMDNode const *ExportVarMetadata,
151 llvm::NamedMDNode const *ExportFuncMetadata);
152
Joseph Wen34c600a2011-07-25 17:59:17 -0700153 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
Logan Chienda5e0c32011-06-13 03:47:21 +0800154
155#if USE_MCJIT
156 static void *resolveSymbolAdapter(void *context, char const *name);
157#endif
Stephen Hinescc366e52012-02-21 17:22:04 -0800158 int runInternalPasses(std::vector<std::string>& Names,
159 std::vector<uint32_t>& Signatures);
Logan Chienda5e0c32011-06-13 03:47:21 +0800160
161 int runLTO(llvm::TargetData *TD,
Stephen Hines569986d2012-03-09 19:58:45 -0800162 std::vector<const char*>& ExportSymbols,
Daniel Malea094881f2011-12-14 17:39:16 -0500163 llvm::CodeGenOpt::Level OptimizationLevel);
Logan Chienda5e0c32011-06-13 03:47:21 +0800164
Logande2ca792010-11-27 22:15:39 +0800165 bool hasError() const {
166 return !mError.empty();
167 }
168
169 void setError(const char *Error) {
170 mError.assign(Error); // Copying
171 }
172
173 void setError(const std::string &Error) {
174 mError = Error;
175 }
176
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800177 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800178
Logan9b504eb2010-11-27 18:19:35 +0800179} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800180
181#endif // BCC_COMPILER_H