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 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 17 | #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 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 39 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 40 | namespace llvm { |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 41 | class Module; |
| 42 | class NamedMDNode; |
| 43 | class TargetData; |
| 44 | } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 45 | |
| 46 | |
| 47 | namespace bcc { |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 48 | class ScriptCompiled; |
| 49 | struct CompilerOption; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 50 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 51 | class Compiler { |
| 52 | private: |
| 53 | ////////////////////////////////////////////////////////////////////////// |
| 54 | // The variable section below (e.g., Triple, CodeGenOptLevel) |
| 55 | // is initialized in GlobalInitialization() |
| 56 | // |
| 57 | static bool GlobalInitialized; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 58 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 59 | // 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; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 63 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 64 | static llvm::CodeGenOpt::Level CodeGenOptLevel; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 65 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 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; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 70 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 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; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 74 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 75 | static void LLVMErrorHandler(void *UserData, const std::string &Message); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 76 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 77 | friend class CodeEmitter; |
| 78 | friend class CodeMemoryManager; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 79 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 80 | private: |
| 81 | ScriptCompiled *mpResult; |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 82 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 83 | std::string mError; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 84 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 85 | // Compilation buffer for MC |
| 86 | llvm::SmallVector<char, 1024> mEmittedELFExecutable; |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 87 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 88 | // Loaded and relocated executable |
| 89 | RSExecRef mRSExecutable; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 90 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 91 | BCCSymbolLookupFn mpSymbolLookupFn; |
| 92 | void *mpSymbolLookupContext; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 93 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 94 | llvm::Module *mModule; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 95 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 96 | public: |
| 97 | Compiler(ScriptCompiled *result); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 98 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 99 | static void GlobalInitialization(); |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 100 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 101 | static std::string const &getTargetTriple() { |
| 102 | return Triple; |
| 103 | } |
Logan Chien | 9347e0b | 2011-07-07 19:51:47 +0800 | [diff] [blame] | 104 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 105 | static llvm::Triple::ArchType getTargetArchType() { |
| 106 | return ArchType; |
| 107 | } |
Shih-wei Liao | 4deffde | 2012-01-17 20:38:17 +0800 | [diff] [blame] | 108 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 109 | void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) { |
| 110 | mpSymbolLookupFn = pFn; |
| 111 | mpSymbolLookupContext = pContext; |
| 112 | } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 113 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 114 | void *getSymbolAddress(char const *name); |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 115 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 116 | const llvm::SmallVector<char, 1024> &getELF() const { |
| 117 | return mEmittedELFExecutable; |
| 118 | } |
Logan Chien | da5e0c3 | 2011-06-13 03:47:21 +0800 | [diff] [blame] | 119 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 120 | int readModule(llvm::Module &pModule); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 121 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 122 | int compile(const CompilerOption &option); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 123 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 124 | char const *getErrorMessage() { |
| 125 | return mError.c_str(); |
| 126 | } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 127 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 128 | const llvm::Module *getModule() const { |
| 129 | return mModule; |
| 130 | } |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 131 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 132 | ~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 Hines | 09ebd17 | 2012-05-03 12:28:26 -0700 | [diff] [blame] | 142 | int runInternalPasses(std::vector<std::string>& Names, |
| 143 | std::vector<uint32_t>& Signatures); |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 144 | |
| 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 |