Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010, The Android Open Source Project |
| 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 | |
Logan | c439523 | 2010-11-27 18:54:17 +0800 | [diff] [blame] | 22 | #include "CodeEmitter.h" |
| 23 | #include "CodeMemoryManager.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 24 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 25 | #include "librsloader.h" |
| 26 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 27 | #include "llvm/ADT/OwningPtr.h" |
| 28 | #include "llvm/ADT/StringRef.h" |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 29 | #include "llvm/ADT/SmallVector.h" |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 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 | |
| 39 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 40 | namespace llvm { |
| 41 | class LLVMContext; |
| 42 | class Module; |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 43 | class MemoryBuffer; |
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; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 51 | |
| 52 | class Compiler { |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 53 | private: |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 54 | ////////////////////////////////////////////////////////////////////////// |
| 55 | // The variable section below (e.g., Triple, CodeGenOptLevel) |
| 56 | // is initialized in GlobalInitialization() |
| 57 | // |
| 58 | static bool GlobalInitialized; |
| 59 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 60 | // If given, this will be the name of the target triple to compile for. |
| 61 | // If not given, the initial values defined in this file will be used. |
| 62 | static std::string Triple; |
| 63 | |
| 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 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 75 | static void LLVMErrorHandler(void *UserData, const std::string &Message); |
| 76 | |
| 77 | static const llvm::StringRef PragmaMetadataName; |
| 78 | static const llvm::StringRef ExportVarMetadataName; |
| 79 | static const llvm::StringRef ExportFuncMetadataName; |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 80 | static const llvm::StringRef ObjectSlotMetadataName; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 81 | |
| 82 | friend class CodeEmitter; |
| 83 | friend class CodeMemoryManager; |
| 84 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 85 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 86 | private: |
Shih-wei Liao | 5c00f4f | 2011-05-20 04:14:54 -0700 | [diff] [blame] | 87 | const char *mCachePath; |
Shih-wei Liao | 898c5a9 | 2011-05-18 07:02:39 -0700 | [diff] [blame] | 88 | |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 89 | ScriptCompiled *mpResult; |
| 90 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 91 | std::string mError; |
| 92 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 93 | #if USE_OLD_JIT |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 94 | // The memory manager for code emitter |
| 95 | llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 96 | |
| 97 | // The CodeEmitter |
| 98 | llvm::OwningPtr<CodeEmitter> mCodeEmitter; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 99 | #endif |
| 100 | |
| 101 | #if USE_MCJIT |
| 102 | // Compilation buffer for MCJIT |
| 103 | llvm::SmallVector<char, 1024> mEmittedELFExecutable; |
| 104 | |
| 105 | // Loaded and relocated executable |
| 106 | RSExecRef mRSExecutable; |
| 107 | #endif |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 108 | |
| 109 | BCCSymbolLookupFn mpSymbolLookupFn; |
| 110 | void *mpSymbolLookupContext; |
| 111 | |
| 112 | llvm::LLVMContext *mContext; |
| 113 | llvm::Module *mModule; |
| 114 | |
| 115 | bool mHasLinked; |
| 116 | |
| 117 | public: |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 118 | Compiler(ScriptCompiled *result); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 119 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 120 | static void GlobalInitialization(); |
| 121 | |
Shih-wei Liao | 5c00f4f | 2011-05-20 04:14:54 -0700 | [diff] [blame] | 122 | void setCachePath(const char *cachePath) { |
| 123 | mCachePath = cachePath; |
Shih-wei Liao | 898c5a9 | 2011-05-18 07:02:39 -0700 | [diff] [blame] | 124 | return; |
| 125 | } |
| 126 | |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 127 | void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 128 | mpSymbolLookupFn = pFn; |
| 129 | mpSymbolLookupContext = pContext; |
| 130 | } |
| 131 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 132 | #if USE_OLD_JIT |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 133 | CodeMemoryManager *createCodeMemoryManager(); |
| 134 | |
| 135 | CodeEmitter *createCodeEmitter(); |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 136 | #endif |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 137 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 138 | #if USE_MCJIT |
Shih-wei Liao | de0ba06 | 2011-05-19 03:16:33 -0700 | [diff] [blame] | 139 | bool getObjPath(std::string &objPath); |
Shih-wei Liao | 898c5a9 | 2011-05-18 07:02:39 -0700 | [diff] [blame] | 140 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 141 | void *getSymbolAddress(char const *name); |
| 142 | #endif |
| 143 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 144 | llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM); |
| 145 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 146 | int readModule(llvm::Module *module) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 147 | mModule = module; |
| 148 | return hasError(); |
| 149 | } |
| 150 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 151 | int linkModule(llvm::Module *module); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 152 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 153 | int compile(); |
| 154 | |
Logan | 38d0607 | 2010-12-29 00:16:39 +0800 | [diff] [blame] | 155 | char const *getErrorMessage() { |
| 156 | return mError.c_str(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 157 | } |
| 158 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 159 | const llvm::Module *getModule() const { |
| 160 | return mModule; |
| 161 | } |
| 162 | |
| 163 | ~Compiler(); |
| 164 | |
| 165 | private: |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 166 | |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 167 | int runCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM, |
| 168 | llvm::NamedMDNode const *ExportVarMetadata, |
| 169 | llvm::NamedMDNode const *ExportFuncMetadata); |
| 170 | |
| 171 | int runMCCodeGen(llvm::TargetData *TD, llvm::TargetMachine *TM, |
| 172 | llvm::NamedMDNode const *ExportVarMetadata, |
| 173 | llvm::NamedMDNode const *ExportFuncMetadata); |
| 174 | |
| 175 | #if USE_MCJIT |
| 176 | static void *resolveSymbolAdapter(void *context, char const *name); |
| 177 | #endif |
| 178 | |
| 179 | int runLTO(llvm::TargetData *TD, |
| 180 | llvm::NamedMDNode const *ExportVarMetadata, |
| 181 | llvm::NamedMDNode const *ExportFuncMetadata); |
| 182 | |
| 183 | int writeELFExecToFile(); |
Logan Chien | 4cc0033 | 2011-06-12 14:00:46 +0800 | [diff] [blame] | 184 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 185 | bool hasError() const { |
| 186 | return !mError.empty(); |
| 187 | } |
| 188 | |
| 189 | void setError(const char *Error) { |
| 190 | mError.assign(Error); // Copying |
| 191 | } |
| 192 | |
| 193 | void setError(const std::string &Error) { |
| 194 | mError = Error; |
| 195 | } |
| 196 | |
Shih-wei Liao | 2417cef | 2010-12-16 05:51:40 -0800 | [diff] [blame] | 197 | }; // End of class Compiler |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 198 | |
Logan | 9b504eb | 2010-11-27 18:19:35 +0800 | [diff] [blame] | 199 | } // namespace bcc |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 200 | |
| 201 | #endif // BCC_COMPILER_H |