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
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
Logan Chienda5e0c32011-06-13 03:47:21 +080024#include "librsloader.h"
25
Logan1f028c02010-11-27 01:02:48 +080026#include "llvm/ADT/OwningPtr.h"
27#include "llvm/ADT/StringRef.h"
Logan Chienda5e0c32011-06-13 03:47:21 +080028#include "llvm/ADT/SmallVector.h"
Shih-wei Liao4deffde2012-01-17 20:38:17 +080029#include "llvm/ADT/Triple.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 {
Logan1f028c02010-11-27 01:02:48 +080041 class Module;
Logan Chien4cc00332011-06-12 14:00:46 +080042 class NamedMDNode;
43 class TargetData;
Logan1f028c02010-11-27 01:02:48 +080044}
45
46
47namespace bcc {
Logan2a6dc822011-01-06 04:05:20 +080048 class ScriptCompiled;
Zonr Chang2fcbd022012-01-06 21:04:31 +080049 struct CompilerOption;
Logan1f028c02010-11-27 01:02:48 +080050
51 class Compiler {
Logande2ca792010-11-27 22:15:39 +080052 private:
Logan1f028c02010-11-27 01:02:48 +080053 //////////////////////////////////////////////////////////////////////////
54 // The variable section below (e.g., Triple, CodeGenOptLevel)
55 // is initialized in GlobalInitialization()
56 //
57 static bool GlobalInitialized;
58
Logan1f028c02010-11-27 01:02:48 +080059 // 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;
Shih-wei Liao4deffde2012-01-17 20:38:17 +080062 static llvm::Triple::ArchType ArchType;
Logan1f028c02010-11-27 01:02:48 +080063
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
Logan1f028c02010-11-27 01:02:48 +080077 friend class CodeEmitter;
78 friend class CodeMemoryManager;
79
80 private:
Logan2a6dc822011-01-06 04:05:20 +080081 ScriptCompiled *mpResult;
82
Logan1f028c02010-11-27 01:02:48 +080083 std::string mError;
84
Stephen Hines36999622012-03-11 19:15:51 -070085 // Compilation buffer for MC
Logan Chienda5e0c32011-06-13 03:47:21 +080086 llvm::SmallVector<char, 1024> mEmittedELFExecutable;
87
88 // Loaded and relocated executable
89 RSExecRef mRSExecutable;
Logan1f028c02010-11-27 01:02:48 +080090
91 BCCSymbolLookupFn mpSymbolLookupFn;
92 void *mpSymbolLookupContext;
93
Logan1f028c02010-11-27 01:02:48 +080094 llvm::Module *mModule;
95
96 bool mHasLinked;
97
98 public:
Logan2a6dc822011-01-06 04:05:20 +080099 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +0800100
Loganecf4cbd2011-01-06 05:34:11 +0800101 static void GlobalInitialization();
102
Logan Chien9347e0b2011-07-07 19:51:47 +0800103 static std::string const &getTargetTriple() {
104 return Triple;
105 }
106
Shih-wei Liao4deffde2012-01-17 20:38:17 +0800107 static llvm::Triple::ArchType getTargetArchType() {
108 return ArchType;
109 }
110
Loganf340bf72011-01-14 17:51:40 +0800111 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logan1f028c02010-11-27 01:02:48 +0800112 mpSymbolLookupFn = pFn;
113 mpSymbolLookupContext = pContext;
114 }
115
Logan Chienda5e0c32011-06-13 03:47:21 +0800116 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700117
118 const llvm::SmallVector<char, 1024> &getELF() const {
119 return mEmittedELFExecutable;
120 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800121
Logan1f028c02010-11-27 01:02:48 +0800122 int readModule(llvm::Module *module) {
Logan1f028c02010-11-27 01:02:48 +0800123 mModule = module;
124 return hasError();
125 }
126
Logan474cbd22011-01-31 01:47:44 +0800127 int linkModule(llvm::Module *module);
Logan1f028c02010-11-27 01:02:48 +0800128
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800129 int compile(const CompilerOption &option);
Logan1f028c02010-11-27 01:02:48 +0800130
Logan38d06072010-12-29 00:16:39 +0800131 char const *getErrorMessage() {
132 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800133 }
134
Logan1f028c02010-11-27 01:02:48 +0800135 const llvm::Module *getModule() const {
136 return mModule;
137 }
138
139 ~Compiler();
140
141 private:
Logande2ca792010-11-27 22:15:39 +0800142
Logan Chienda5e0c32011-06-13 03:47:21 +0800143 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
144 llvm::NamedMDNode const *ExportVarMetadata,
145 llvm::NamedMDNode const *ExportFuncMetadata);
146
Joseph Wen34c600a2011-07-25 17:59:17 -0700147 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
Logan Chienda5e0c32011-06-13 03:47:21 +0800148
Logan Chienda5e0c32011-06-13 03:47:21 +0800149 static void *resolveSymbolAdapter(void *context, char const *name);
Stephen Hines36999622012-03-11 19:15:51 -0700150
Stephen Hinescc366e52012-02-21 17:22:04 -0800151 int runInternalPasses(std::vector<std::string>& Names,
152 std::vector<uint32_t>& Signatures);
Logan Chienda5e0c32011-06-13 03:47:21 +0800153
154 int runLTO(llvm::TargetData *TD,
Stephen Hines569986d2012-03-09 19:58:45 -0800155 std::vector<const char*>& ExportSymbols,
Daniel Malea094881f2011-12-14 17:39:16 -0500156 llvm::CodeGenOpt::Level OptimizationLevel);
Logan Chienda5e0c32011-06-13 03:47:21 +0800157
Logande2ca792010-11-27 22:15:39 +0800158 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
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800170 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800171
Logan9b504eb2010-11-27 18:19:35 +0800172} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800173
174#endif // BCC_COMPILER_H