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 | |
| 25 | #include "llvm/ADT/OwningPtr.h" |
| 26 | #include "llvm/ADT/StringRef.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
| 28 | |
| 29 | #include <stddef.h> |
| 30 | |
| 31 | #include <list> |
| 32 | #include <string> |
| 33 | #include <vector> |
| 34 | #include <utility> |
| 35 | |
| 36 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 37 | namespace llvm { |
| 38 | class LLVMContext; |
| 39 | class Module; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | namespace bcc { |
| 44 | |
| 45 | class Compiler { |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 46 | private: |
| 47 | typedef std::list< std::pair<std::string, std::string> > PragmaList; |
| 48 | typedef std::list<void*> ExportVarList; |
| 49 | typedef std::list<void*> ExportFuncList; |
| 50 | |
| 51 | |
| 52 | private: |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 53 | // This part is designed to be orthogonal to those exported bcc*() functions |
| 54 | // implementation and internal struct BCCscript. |
| 55 | |
| 56 | ////////////////////////////////////////////////////////////////////////// |
| 57 | // The variable section below (e.g., Triple, CodeGenOptLevel) |
| 58 | // is initialized in GlobalInitialization() |
| 59 | // |
| 60 | static bool GlobalInitialized; |
| 61 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 62 | // If given, this will be the name of the target triple to compile for. |
| 63 | // If not given, the initial values defined in this file will be used. |
| 64 | static std::string Triple; |
| 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 GlobalInitialization(); |
| 78 | |
| 79 | static void LLVMErrorHandler(void *UserData, const std::string &Message); |
| 80 | |
| 81 | static const llvm::StringRef PragmaMetadataName; |
| 82 | static const llvm::StringRef ExportVarMetadataName; |
| 83 | static const llvm::StringRef ExportFuncMetadataName; |
| 84 | |
| 85 | friend class CodeEmitter; |
| 86 | friend class CodeMemoryManager; |
| 87 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 88 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 89 | private: |
| 90 | std::string mError; |
| 91 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 92 | bool mUseCache; // Set by readBC() |
| 93 | bool mCacheNew; // Set by readBC() |
| 94 | int mCacheFd; // Set by readBC() |
| 95 | char *mCacheMapAddr; // Set by loadCacheFile() if mCacheNew is false |
| 96 | oBCCHeader *mCacheHdr; // Set by loadCacheFile() |
| 97 | size_t mCacheSize; // Set by loadCacheFile() |
| 98 | ptrdiff_t mCacheDiff; // Set by loadCacheFile() |
| 99 | char *mCodeDataAddr; // Set by CodeMemoryManager if mCacheNew is true. |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 100 | // Used by genCacheFile() for dumping |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 101 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 102 | PragmaList mPragmas; |
| 103 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 104 | ExportVarList mExportVars; |
| 105 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 106 | ExportFuncList mExportFuncs; |
| 107 | |
| 108 | // The memory manager for code emitter |
| 109 | llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 110 | |
| 111 | // The CodeEmitter |
| 112 | llvm::OwningPtr<CodeEmitter> mCodeEmitter; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 113 | |
| 114 | BCCSymbolLookupFn mpSymbolLookupFn; |
| 115 | void *mpSymbolLookupContext; |
| 116 | |
| 117 | llvm::LLVMContext *mContext; |
| 118 | llvm::Module *mModule; |
| 119 | |
| 120 | bool mHasLinked; |
| 121 | |
| 122 | public: |
| 123 | Compiler(); |
| 124 | |
| 125 | // interface for BCCscript::registerSymbolCallback() |
| 126 | void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
| 127 | mpSymbolLookupFn = pFn; |
| 128 | mpSymbolLookupContext = pContext; |
| 129 | } |
| 130 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 131 | CodeMemoryManager *createCodeMemoryManager(); |
| 132 | |
| 133 | CodeEmitter *createCodeEmitter(); |
| 134 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 135 | int readModule(llvm::Module *module) { |
| 136 | GlobalInitialization(); |
| 137 | mModule = module; |
| 138 | return hasError(); |
| 139 | } |
| 140 | |
Shih-wei Liao | e6a1851 | 2010-12-09 12:38:10 -0800 | [diff] [blame] | 141 | int readBC(const char *bitcode, |
| 142 | size_t bitcodeSize, |
| 143 | const BCCchar *resName, |
| 144 | const BCCchar *cacheDir); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 145 | |
| 146 | int linkBC(const char *bitcode, size_t bitcodeSize); |
| 147 | |
| 148 | // interface for bccLoadBinary() |
| 149 | int loadCacheFile(); |
| 150 | |
| 151 | // interace for bccCompileBC() |
| 152 | int compile(); |
| 153 | |
| 154 | // interface for bccGetScriptInfoLog() |
| 155 | char *getErrorMessage() { |
| 156 | return const_cast<char*>(mError.c_str()); |
| 157 | } |
| 158 | |
| 159 | // interface for bccGetScriptLabel() |
| 160 | void *lookup(const char *name); |
| 161 | |
| 162 | // Interface for bccGetExportVars() |
| 163 | void getExportVars(BCCsizei *actualVarCount, |
| 164 | BCCsizei maxVarCount, |
| 165 | BCCvoid **vars); |
| 166 | |
| 167 | // Interface for bccGetExportFuncs() |
| 168 | void getExportFuncs(BCCsizei *actualFuncCount, |
| 169 | BCCsizei maxFuncCount, |
| 170 | BCCvoid **funcs); |
| 171 | |
| 172 | // Interface for bccGetPragmas() |
| 173 | void getPragmas(BCCsizei *actualStringCount, |
| 174 | BCCsizei maxStringCount, |
| 175 | BCCchar **strings); |
| 176 | |
| 177 | // Interface for bccGetFunctions() |
| 178 | void getFunctions(BCCsizei *actualFunctionCount, |
| 179 | BCCsizei maxFunctionCount, |
| 180 | BCCchar **functions); |
| 181 | |
| 182 | // Interface for bccGetFunctionBinary() |
| 183 | void getFunctionBinary(BCCchar *function, |
| 184 | BCCvoid **base, |
| 185 | BCCsizei *length); |
| 186 | |
| 187 | const llvm::Module *getModule() const { |
| 188 | return mModule; |
| 189 | } |
| 190 | |
| 191 | ~Compiler(); |
| 192 | |
| 193 | private: |
| 194 | // Note: loadCacheFile() and genCacheFile() go hand in hand |
| 195 | void genCacheFile(); |
| 196 | |
| 197 | // OpenCacheFile() returns fd of the cache file. |
| 198 | // Input: |
| 199 | // BCCchar *resName: Used to genCacheFileName() |
| 200 | // bool createIfMissing: If false, turn off caching |
| 201 | // Output: |
| 202 | // returns fd: If -1: Failed |
| 203 | // mCacheNew: If true, the returned fd is new. Otherwise, the fd is the |
| 204 | // cache file's file descriptor |
| 205 | // Note: openCacheFile() will check the cache file's validity, |
| 206 | // such as Magic number, sourceWhen... dependencies. |
Shih-wei Liao | e6a1851 | 2010-12-09 12:38:10 -0800 | [diff] [blame] | 207 | int openCacheFile(const BCCchar *resName, |
| 208 | const BCCchar *cacheDir, |
| 209 | bool createIfMissing); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 210 | |
Shih-wei Liao | e6a1851 | 2010-12-09 12:38:10 -0800 | [diff] [blame] | 211 | char *genCacheFileName(const char *cacheDir, |
| 212 | const char *fileName, |
| 213 | const char *subFileName); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 214 | |
| 215 | /* |
| 216 | * Read the oBCC header, verify it, then read the dependent section |
| 217 | * and verify that data as well. |
| 218 | * |
| 219 | * On successful return, the file will be seeked immediately past the |
| 220 | * oBCC header. |
| 221 | */ |
| 222 | bool checkHeaderAndDependencies(int fd, |
| 223 | uint32_t sourceWhen, |
| 224 | uint32_t rslibWhen, |
| 225 | uint32_t libRSWhen, |
| 226 | uint32_t libbccWhen); |
| 227 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 228 | private: |
| 229 | |
| 230 | bool hasError() const { |
| 231 | return !mError.empty(); |
| 232 | } |
| 233 | |
| 234 | void setError(const char *Error) { |
| 235 | mError.assign(Error); // Copying |
| 236 | } |
| 237 | |
| 238 | void setError(const std::string &Error) { |
| 239 | mError = Error; |
| 240 | } |
| 241 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 242 | }; |
| 243 | |
| 244 | |
| 245 | |
Logan | 9b504eb | 2010-11-27 18:19:35 +0800 | [diff] [blame] | 246 | } // namespace bcc |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 247 | |
| 248 | #endif // BCC_COMPILER_H |