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; |
Shih-wei Liao | 931501a | 2010-12-16 04:43:04 -0800 | [diff] [blame] | 61 | static const char *resNames[64]; |
| 62 | static int resNamesMmaped[64]; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 63 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 64 | // If given, this will be the name of the target triple to compile for. |
| 65 | // If not given, the initial values defined in this file will be used. |
| 66 | static std::string Triple; |
| 67 | |
| 68 | static llvm::CodeGenOpt::Level CodeGenOptLevel; |
| 69 | |
| 70 | // End of section of GlobalInitializing variables |
| 71 | ///////////////////////////////////////////////////////////////////////// |
| 72 | // If given, the name of the target CPU to generate code for. |
| 73 | static std::string CPU; |
| 74 | |
| 75 | // The list of target specific features to enable or disable -- this should |
| 76 | // be a list of strings starting with '+' (enable) or '-' (disable). |
| 77 | static std::vector<std::string> Features; |
| 78 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 79 | static void GlobalInitialization(); |
| 80 | |
| 81 | static void LLVMErrorHandler(void *UserData, const std::string &Message); |
| 82 | |
| 83 | static const llvm::StringRef PragmaMetadataName; |
| 84 | static const llvm::StringRef ExportVarMetadataName; |
| 85 | static const llvm::StringRef ExportFuncMetadataName; |
| 86 | |
| 87 | friend class CodeEmitter; |
| 88 | friend class CodeMemoryManager; |
| 89 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 90 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 91 | private: |
| 92 | std::string mError; |
| 93 | |
Shih-wei Liao | 931501a | 2010-12-16 04:43:04 -0800 | [diff] [blame] | 94 | int mResId; // Set by readBC() |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 95 | bool mUseCache; // Set by readBC() |
| 96 | bool mCacheNew; // Set by readBC() |
| 97 | int mCacheFd; // Set by readBC() |
Logan | b9b0416 | 2010-12-20 16:36:57 +0800 | [diff] [blame] | 98 | long mSourceModTime; // Set by readBC() |
| 99 | long mSourceCRC32; // Set by readBC(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 100 | char *mCacheMapAddr; // Set by loadCacheFile() if mCacheNew is false |
| 101 | oBCCHeader *mCacheHdr; // Set by loadCacheFile() |
| 102 | size_t mCacheSize; // Set by loadCacheFile() |
| 103 | ptrdiff_t mCacheDiff; // Set by loadCacheFile() |
| 104 | char *mCodeDataAddr; // Set by CodeMemoryManager if mCacheNew is true. |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 105 | // Used by genCacheFile() for dumping |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 106 | |
Logan | 8b77a77 | 2010-12-21 09:11:01 +0800 | [diff] [blame^] | 107 | unsigned char mSourceSHA1[20]; // Set by readBC() |
| 108 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 109 | PragmaList mPragmas; |
| 110 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 111 | ExportVarList mExportVars; |
| 112 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 113 | ExportFuncList mExportFuncs; |
| 114 | |
| 115 | // The memory manager for code emitter |
| 116 | llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 117 | |
| 118 | // The CodeEmitter |
| 119 | llvm::OwningPtr<CodeEmitter> mCodeEmitter; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 120 | |
| 121 | BCCSymbolLookupFn mpSymbolLookupFn; |
| 122 | void *mpSymbolLookupContext; |
| 123 | |
| 124 | llvm::LLVMContext *mContext; |
| 125 | llvm::Module *mModule; |
| 126 | |
| 127 | bool mHasLinked; |
| 128 | |
| 129 | public: |
| 130 | Compiler(); |
| 131 | |
| 132 | // interface for BCCscript::registerSymbolCallback() |
| 133 | void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
| 134 | mpSymbolLookupFn = pFn; |
| 135 | mpSymbolLookupContext = pContext; |
| 136 | } |
| 137 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 138 | CodeMemoryManager *createCodeMemoryManager(); |
| 139 | |
| 140 | CodeEmitter *createCodeEmitter(); |
| 141 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 142 | int readModule(llvm::Module *module) { |
| 143 | GlobalInitialization(); |
| 144 | mModule = module; |
| 145 | return hasError(); |
| 146 | } |
| 147 | |
Shih-wei Liao | e6a1851 | 2010-12-09 12:38:10 -0800 | [diff] [blame] | 148 | int readBC(const char *bitcode, |
| 149 | size_t bitcodeSize, |
Logan | b9b0416 | 2010-12-20 16:36:57 +0800 | [diff] [blame] | 150 | long bitcodeFileModTime, |
| 151 | long bitcodeFileCRC32, |
Shih-wei Liao | e6a1851 | 2010-12-09 12:38:10 -0800 | [diff] [blame] | 152 | const BCCchar *resName, |
| 153 | const BCCchar *cacheDir); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 154 | |
| 155 | int linkBC(const char *bitcode, size_t bitcodeSize); |
| 156 | |
| 157 | // interface for bccLoadBinary() |
| 158 | int loadCacheFile(); |
| 159 | |
| 160 | // interace for bccCompileBC() |
| 161 | int compile(); |
| 162 | |
| 163 | // interface for bccGetScriptInfoLog() |
| 164 | char *getErrorMessage() { |
| 165 | return const_cast<char*>(mError.c_str()); |
| 166 | } |
| 167 | |
| 168 | // interface for bccGetScriptLabel() |
| 169 | void *lookup(const char *name); |
| 170 | |
| 171 | // Interface for bccGetExportVars() |
| 172 | void getExportVars(BCCsizei *actualVarCount, |
| 173 | BCCsizei maxVarCount, |
| 174 | BCCvoid **vars); |
| 175 | |
| 176 | // Interface for bccGetExportFuncs() |
| 177 | void getExportFuncs(BCCsizei *actualFuncCount, |
| 178 | BCCsizei maxFuncCount, |
| 179 | BCCvoid **funcs); |
| 180 | |
| 181 | // Interface for bccGetPragmas() |
| 182 | void getPragmas(BCCsizei *actualStringCount, |
| 183 | BCCsizei maxStringCount, |
| 184 | BCCchar **strings); |
| 185 | |
| 186 | // Interface for bccGetFunctions() |
| 187 | void getFunctions(BCCsizei *actualFunctionCount, |
| 188 | BCCsizei maxFunctionCount, |
| 189 | BCCchar **functions); |
| 190 | |
| 191 | // Interface for bccGetFunctionBinary() |
| 192 | void getFunctionBinary(BCCchar *function, |
| 193 | BCCvoid **base, |
| 194 | BCCsizei *length); |
| 195 | |
| 196 | const llvm::Module *getModule() const { |
| 197 | return mModule; |
| 198 | } |
| 199 | |
| 200 | ~Compiler(); |
| 201 | |
| 202 | private: |
Logan | 8b77a77 | 2010-12-21 09:11:01 +0800 | [diff] [blame^] | 203 | void computeSourceSHA1(char const *bitcode, size_t size); |
| 204 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 205 | // Note: loadCacheFile() and genCacheFile() go hand in hand |
| 206 | void genCacheFile(); |
| 207 | |
| 208 | // OpenCacheFile() returns fd of the cache file. |
| 209 | // Input: |
| 210 | // BCCchar *resName: Used to genCacheFileName() |
| 211 | // bool createIfMissing: If false, turn off caching |
| 212 | // Output: |
| 213 | // returns fd: If -1: Failed |
| 214 | // mCacheNew: If true, the returned fd is new. Otherwise, the fd is the |
| 215 | // cache file's file descriptor |
| 216 | // Note: openCacheFile() will check the cache file's validity, |
| 217 | // such as Magic number, sourceWhen... dependencies. |
Shih-wei Liao | e6a1851 | 2010-12-09 12:38:10 -0800 | [diff] [blame] | 218 | int openCacheFile(const BCCchar *resName, |
| 219 | const BCCchar *cacheDir, |
| 220 | bool createIfMissing); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 221 | |
Shih-wei Liao | e6a1851 | 2010-12-09 12:38:10 -0800 | [diff] [blame] | 222 | char *genCacheFileName(const char *cacheDir, |
| 223 | const char *fileName, |
| 224 | const char *subFileName); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 225 | |
| 226 | /* |
| 227 | * Read the oBCC header, verify it, then read the dependent section |
| 228 | * and verify that data as well. |
| 229 | * |
| 230 | * On successful return, the file will be seeked immediately past the |
| 231 | * oBCC header. |
| 232 | */ |
| 233 | bool checkHeaderAndDependencies(int fd, |
Logan | b9b0416 | 2010-12-20 16:36:57 +0800 | [diff] [blame] | 234 | long sourceWhen, |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 235 | uint32_t rslibWhen, |
| 236 | uint32_t libRSWhen, |
| 237 | uint32_t libbccWhen); |
| 238 | |
Shih-wei Liao | 2417cef | 2010-12-16 05:51:40 -0800 | [diff] [blame] | 239 | |
| 240 | struct { |
| 241 | bool mNoCache; |
| 242 | } props; |
| 243 | |
| 244 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 245 | private: |
| 246 | |
| 247 | bool hasError() const { |
| 248 | return !mError.empty(); |
| 249 | } |
| 250 | |
| 251 | void setError(const char *Error) { |
| 252 | mError.assign(Error); // Copying |
| 253 | } |
| 254 | |
| 255 | void setError(const std::string &Error) { |
| 256 | mError = Error; |
| 257 | } |
| 258 | |
Shih-wei Liao | 2417cef | 2010-12-16 05:51:40 -0800 | [diff] [blame] | 259 | }; // End of class Compiler |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 260 | |
Logan | 9b504eb | 2010-11-27 18:19:35 +0800 | [diff] [blame] | 261 | } // namespace bcc |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 262 | |
| 263 | #endif // BCC_COMPILER_H |