blob: 863cda67cb48040d67694ca42ee2b03c8d78608a [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
Stephen Hines4a68b1c2012-05-03 12:28:14 -070017#ifndef BCC_COMPILER_H
18#define BCC_COMPILER_H
19
20#include <bcc/bcc.h>
21
22#include <Config.h>
23
24#include "librsloader.h"
25
26#include "llvm/ADT/OwningPtr.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/ADT/Triple.h"
30#include "llvm/Target/TargetMachine.h"
31
32#include <stddef.h>
33
34#include <list>
35#include <string>
36#include <vector>
37#include <utility>
38
Logan1f028c02010-11-27 01:02:48 +080039
Logan1f028c02010-11-27 01:02:48 +080040namespace llvm {
Stephen Hines4a68b1c2012-05-03 12:28:14 -070041 class Module;
42 class NamedMDNode;
43 class TargetData;
44}
Logan1f028c02010-11-27 01:02:48 +080045
46
47namespace bcc {
Stephen Hines4a68b1c2012-05-03 12:28:14 -070048 class ScriptCompiled;
49 struct CompilerOption;
Logan1f028c02010-11-27 01:02:48 +080050
Stephen Hines4a68b1c2012-05-03 12:28:14 -070051 class Compiler {
52 private:
53 //////////////////////////////////////////////////////////////////////////
54 // The variable section below (e.g., Triple, CodeGenOptLevel)
55 // is initialized in GlobalInitialization()
56 //
57 static bool GlobalInitialized;
Logan1f028c02010-11-27 01:02:48 +080058
Stephen Hines4a68b1c2012-05-03 12:28:14 -070059 // If given, this will be the name of the target triple to compile for.
60 // If not given, the initial values defined in this file will be used.
61 static std::string Triple;
62 static llvm::Triple::ArchType ArchType;
Logan1f028c02010-11-27 01:02:48 +080063
Stephen Hines4a68b1c2012-05-03 12:28:14 -070064 static llvm::CodeGenOpt::Level CodeGenOptLevel;
Logan1f028c02010-11-27 01:02:48 +080065
Stephen Hines4a68b1c2012-05-03 12:28:14 -070066 // 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;
Logan1f028c02010-11-27 01:02:48 +080070
Stephen Hines4a68b1c2012-05-03 12:28:14 -070071 // 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;
Logan1f028c02010-11-27 01:02:48 +080074
Stephen Hines4a68b1c2012-05-03 12:28:14 -070075 static void LLVMErrorHandler(void *UserData, const std::string &Message);
Logan1f028c02010-11-27 01:02:48 +080076
Stephen Hines4a68b1c2012-05-03 12:28:14 -070077 friend class CodeEmitter;
78 friend class CodeMemoryManager;
Logan1f028c02010-11-27 01:02:48 +080079
Stephen Hines4a68b1c2012-05-03 12:28:14 -070080 private:
81 ScriptCompiled *mpResult;
Logan2a6dc822011-01-06 04:05:20 +080082
Stephen Hines4a68b1c2012-05-03 12:28:14 -070083 std::string mError;
Logan1f028c02010-11-27 01:02:48 +080084
Stephen Hines4a68b1c2012-05-03 12:28:14 -070085 // Compilation buffer for MC
86 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
Logan Chienda5e0c32011-06-13 03:47:21 +080087
Stephen Hines4a68b1c2012-05-03 12:28:14 -070088 // Loaded and relocated executable
89 RSExecRef mRSExecutable;
Logan1f028c02010-11-27 01:02:48 +080090
Stephen Hines4a68b1c2012-05-03 12:28:14 -070091 BCCSymbolLookupFn mpSymbolLookupFn;
92 void *mpSymbolLookupContext;
Logan1f028c02010-11-27 01:02:48 +080093
Stephen Hines4a68b1c2012-05-03 12:28:14 -070094 llvm::Module *mModule;
Logan1f028c02010-11-27 01:02:48 +080095
Stephen Hinesead5ccb2012-05-03 12:30:38 -070096 bool mHasLinked;
97
Stephen Hines4a68b1c2012-05-03 12:28:14 -070098 public:
99 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800100
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700101 static void GlobalInitialization();
Loganecf4cbd2011-01-06 05:34:11 +0800102
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700103 static std::string const &getTargetTriple() {
104 return Triple;
105 }
Logan Chien9347e0b2011-07-07 19:51:47 +0800106
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700107 static llvm::Triple::ArchType getTargetArchType() {
108 return ArchType;
109 }
Shih-wei Liao4deffde2012-01-17 20:38:17 +0800110
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700111 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
112 mpSymbolLookupFn = pFn;
113 mpSymbolLookupContext = pContext;
114 }
Logan1f028c02010-11-27 01:02:48 +0800115
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700116 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700117
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700118 const llvm::SmallVector<char, 1024> &getELF() const {
119 return mEmittedELFExecutable;
120 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800121
Stephen Hinesead5ccb2012-05-03 12:30:38 -0700122 int readModule(llvm::Module *module) {
123 mModule = module;
124 return hasError();
125 }
126
127 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800128
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700129 int compile(const CompilerOption &option);
Logan1f028c02010-11-27 01:02:48 +0800130
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700131 char const *getErrorMessage() {
132 return mError.c_str();
133 }
Logan1f028c02010-11-27 01:02:48 +0800134
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700135 const llvm::Module *getModule() const {
136 return mModule;
137 }
Logan1f028c02010-11-27 01:02:48 +0800138
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700139 ~Compiler();
140
141 private:
142
143 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
144 llvm::NamedMDNode const *ExportVarMetadata,
145 llvm::NamedMDNode const *ExportFuncMetadata);
146
147 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
148
Stephen Hines5fb14742012-05-03 12:29:50 -0700149 static void *resolveSymbolAdapter(void *context, char const *name);
150
Stephen Hines09ebd172012-05-03 12:28:26 -0700151 int runInternalPasses(std::vector<std::string>& Names,
152 std::vector<uint32_t>& Signatures);
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700153
154 int runLTO(llvm::TargetData *TD,
155 std::vector<const char*>& ExportSymbols,
156 llvm::CodeGenOpt::Level OptimizationLevel);
157
158 bool hasError() const {
159 return !mError.empty();
160 }
161
162 void setError(const char *Error) {
163 mError.assign(Error); // Copying
164 }
165
166 void setError(const std::string &Error) {
167 mError = Error;
168 }
169
170 }; // End of class Compiler
171
172} // namespace bcc
173
174#endif // BCC_COMPILER_H