blob: 8fee17cc06a809b23c8eb1f36c93527f32f5fb1c [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
Logan1f028c02010-11-27 01:02:48 +080096 public:
Logan2a6dc822011-01-06 04:05:20 +080097 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +080098
Loganecf4cbd2011-01-06 05:34:11 +080099 static void GlobalInitialization();
100
Logan Chien9347e0b2011-07-07 19:51:47 +0800101 static std::string const &getTargetTriple() {
102 return Triple;
103 }
104
Shih-wei Liao4deffde2012-01-17 20:38:17 +0800105 static llvm::Triple::ArchType getTargetArchType() {
106 return ArchType;
107 }
108
Loganf340bf72011-01-14 17:51:40 +0800109 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
Logan1f028c02010-11-27 01:02:48 +0800110 mpSymbolLookupFn = pFn;
111 mpSymbolLookupContext = pContext;
112 }
113
Logan Chienda5e0c32011-06-13 03:47:21 +0800114 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700115
116 const llvm::SmallVector<char, 1024> &getELF() const {
117 return mEmittedELFExecutable;
118 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800119
Shih-wei Liao4ce024b2012-04-25 03:40:50 -0700120 int readModule(llvm::Module &pModule);
Logan1f028c02010-11-27 01:02:48 +0800121
Shih-wei Liao9e81e372012-01-17 16:38:40 -0800122 int compile(const CompilerOption &option);
Logan1f028c02010-11-27 01:02:48 +0800123
Logan38d06072010-12-29 00:16:39 +0800124 char const *getErrorMessage() {
125 return mError.c_str();
Logan1f028c02010-11-27 01:02:48 +0800126 }
127
Logan1f028c02010-11-27 01:02:48 +0800128 const llvm::Module *getModule() const {
129 return mModule;
130 }
131
132 ~Compiler();
133
134 private:
Logande2ca792010-11-27 22:15:39 +0800135
Logan Chienda5e0c32011-06-13 03:47:21 +0800136 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
137 llvm::NamedMDNode const *ExportVarMetadata,
138 llvm::NamedMDNode const *ExportFuncMetadata);
139
Joseph Wen34c600a2011-07-25 17:59:17 -0700140 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
Logan Chienda5e0c32011-06-13 03:47:21 +0800141
Logan Chienda5e0c32011-06-13 03:47:21 +0800142 static void *resolveSymbolAdapter(void *context, char const *name);
Stephen Hines36999622012-03-11 19:15:51 -0700143
Stephen Hinescc366e52012-02-21 17:22:04 -0800144 int runInternalPasses(std::vector<std::string>& Names,
145 std::vector<uint32_t>& Signatures);
Logan Chienda5e0c32011-06-13 03:47:21 +0800146
147 int runLTO(llvm::TargetData *TD,
Stephen Hines569986d2012-03-09 19:58:45 -0800148 std::vector<const char*>& ExportSymbols,
Daniel Malea094881f2011-12-14 17:39:16 -0500149 llvm::CodeGenOpt::Level OptimizationLevel);
Logan Chienda5e0c32011-06-13 03:47:21 +0800150
Logande2ca792010-11-27 22:15:39 +0800151 bool hasError() const {
152 return !mError.empty();
153 }
154
155 void setError(const char *Error) {
156 mError.assign(Error); // Copying
157 }
158
159 void setError(const std::string &Error) {
160 mError = Error;
161 }
162
Shih-wei Liao2417cef2010-12-16 05:51:40 -0800163 }; // End of class Compiler
Logan1f028c02010-11-27 01:02:48 +0800164
Logan9b504eb2010-11-27 18:19:35 +0800165} // namespace bcc
Logan1f028c02010-11-27 01:02:48 +0800166
167#endif // BCC_COMPILER_H