blob: b5872d6cd6fed0f72c1ed86520eb514db2420ef8 [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 Hines4a68b1c2012-05-03 12:28:14 -070096 public:
97 Compiler(ScriptCompiled *result);
Logan1f028c02010-11-27 01:02:48 +080098
Stephen Hines4a68b1c2012-05-03 12:28:14 -070099 static void GlobalInitialization();
Loganecf4cbd2011-01-06 05:34:11 +0800100
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700101 static std::string const &getTargetTriple() {
102 return Triple;
103 }
Logan Chien9347e0b2011-07-07 19:51:47 +0800104
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700105 static llvm::Triple::ArchType getTargetArchType() {
106 return ArchType;
107 }
Shih-wei Liao4deffde2012-01-17 20:38:17 +0800108
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700109 void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) {
110 mpSymbolLookupFn = pFn;
111 mpSymbolLookupContext = pContext;
112 }
Logan1f028c02010-11-27 01:02:48 +0800113
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700114 void *getSymbolAddress(char const *name);
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700115
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700116 const llvm::SmallVector<char, 1024> &getELF() const {
117 return mEmittedELFExecutable;
118 }
Logan Chienda5e0c32011-06-13 03:47:21 +0800119
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700120 int readModule(llvm::Module &pModule);
Logan1f028c02010-11-27 01:02:48 +0800121
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700122 int compile(const CompilerOption &option);
Logan1f028c02010-11-27 01:02:48 +0800123
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700124 char const *getErrorMessage() {
125 return mError.c_str();
126 }
Logan1f028c02010-11-27 01:02:48 +0800127
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700128 const llvm::Module *getModule() const {
129 return mModule;
130 }
Logan1f028c02010-11-27 01:02:48 +0800131
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700132 ~Compiler();
133
134 private:
135
136 int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM,
137 llvm::NamedMDNode const *ExportVarMetadata,
138 llvm::NamedMDNode const *ExportFuncMetadata);
139
140 int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM);
141
Stephen Hines09ebd172012-05-03 12:28:26 -0700142 int runInternalPasses(std::vector<std::string>& Names,
143 std::vector<uint32_t>& Signatures);
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700144
145 int runLTO(llvm::TargetData *TD,
146 std::vector<const char*>& ExportSymbols,
147 llvm::CodeGenOpt::Level OptimizationLevel);
148
149 bool hasError() const {
150 return !mError.empty();
151 }
152
153 void setError(const char *Error) {
154 mError.assign(Error); // Copying
155 }
156
157 void setError(const std::string &Error) {
158 mError = Error;
159 }
160
161 }; // End of class Compiler
162
163} // namespace bcc
164
165#endif // BCC_COMPILER_H