Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | db16918 | 2012-01-05 18:46:36 -0800 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 3 | * |
| 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 Hines | 9ddeb6b | 2012-03-11 18:41:03 -0700 | [diff] [blame^] | 22 | #include <Config.h> |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 23 | |
Shih-wei Liao | 320b549 | 2011-06-20 22:53:33 -0700 | [diff] [blame] | 24 | #if USE_MCJIT |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 25 | #include "librsloader.h" |
Shih-wei Liao | 320b549 | 2011-06-20 22:53:33 -0700 | [diff] [blame] | 26 | #endif |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 27 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 28 | #include "llvm/ADT/OwningPtr.h" |
| 29 | #include "llvm/ADT/StringRef.h" |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 30 | #include "llvm/ADT/SmallVector.h" |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 31 | #include "llvm/ADT/Triple.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 32 | #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 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 42 | namespace llvm { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 43 | class Module; |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 44 | class NamedMDNode; |
| 45 | class TargetData; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | namespace bcc { |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 50 | class ScriptCompiled; |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 51 | struct CompilerOption; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 52 | |
| 53 | class Compiler { |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 54 | private: |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 55 | ////////////////////////////////////////////////////////////////////////// |
| 56 | // The variable section below (e.g., Triple, CodeGenOptLevel) |
| 57 | // is initialized in GlobalInitialization() |
| 58 | // |
| 59 | static bool GlobalInitialized; |
| 60 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 61 | // 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 Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 64 | static llvm::Triple::ArchType ArchType; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 65 | |
| 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 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 77 | static void LLVMErrorHandler(void *UserData, const std::string &Message); |
| 78 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 79 | friend class CodeEmitter; |
| 80 | friend class CodeMemoryManager; |
| 81 | |
| 82 | private: |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 83 | ScriptCompiled *mpResult; |
| 84 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 85 | std::string mError; |
| 86 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 87 | #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 |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 94 | |
| 95 | BCCSymbolLookupFn mpSymbolLookupFn; |
| 96 | void *mpSymbolLookupContext; |
| 97 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 98 | llvm::Module *mModule; |
| 99 | |
| 100 | bool mHasLinked; |
| 101 | |
| 102 | public: |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 103 | Compiler(ScriptCompiled *result); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 104 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 105 | static void GlobalInitialization(); |
| 106 | |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 107 | static std::string const &getTargetTriple() { |
| 108 | return Triple; |
| 109 | } |
| 110 | |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 111 | static llvm::Triple::ArchType getTargetArchType() { |
| 112 | return ArchType; |
| 113 | } |
| 114 | |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 115 | void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 116 | mpSymbolLookupFn = pFn; |
| 117 | mpSymbolLookupContext = pContext; |
| 118 | } |
| 119 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 120 | #if USE_MCJIT |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 121 | void *getSymbolAddress(char const *name); |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 122 | |
| 123 | const llvm::SmallVector<char, 1024> &getELF() const { |
| 124 | return mEmittedELFExecutable; |
| 125 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 126 | #endif |
| 127 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 128 | int readModule(llvm::Module *module) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 129 | mModule = module; |
| 130 | return hasError(); |
| 131 | } |
| 132 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 133 | int linkModule(llvm::Module *module); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 134 | |
Shih-wei Liao | 9e81e37 | 2012-01-17 16:38:40 -0800 | [diff] [blame] | 135 | int compile(const CompilerOption &option); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 136 | |
Logan | 38d0607 | 2010-12-29 00:16:39 +0800 | [diff] [blame] | 137 | char const *getErrorMessage() { |
| 138 | return mError.c_str(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 141 | const llvm::Module *getModule() const { |
| 142 | return mModule; |
| 143 | } |
| 144 | |
| 145 | ~Compiler(); |
| 146 | |
| 147 | private: |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 148 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 149 | int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM, |
| 150 | llvm::NamedMDNode const *ExportVarMetadata, |
| 151 | llvm::NamedMDNode const *ExportFuncMetadata); |
| 152 | |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 153 | int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 154 | |
| 155 | #if USE_MCJIT |
| 156 | static void *resolveSymbolAdapter(void *context, char const *name); |
| 157 | #endif |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 158 | int runInternalPasses(std::vector<std::string>& Names, |
| 159 | std::vector<uint32_t>& Signatures); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 160 | |
| 161 | int runLTO(llvm::TargetData *TD, |
Stephen Hines | 569986d | 2012-03-09 19:58:45 -0800 | [diff] [blame] | 162 | std::vector<const char*>& ExportSymbols, |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 163 | llvm::CodeGenOpt::Level OptimizationLevel); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 164 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 165 | 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 Liao | 2417cef | 2010-12-16 05:51:40 -0800 | [diff] [blame] | 177 | }; // End of class Compiler |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 178 | |
Logan | 9b504eb | 2010-11-27 18:19:35 +0800 | [diff] [blame] | 179 | } // namespace bcc |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 180 | |
| 181 | #endif // BCC_COMPILER_H |