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; |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame^] | 40 | class MemoryBuffer; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | |
| 44 | namespace bcc { |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 45 | class ScriptCompiled; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 46 | |
| 47 | class Compiler { |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 48 | private: |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 49 | ////////////////////////////////////////////////////////////////////////// |
| 50 | // The variable section below (e.g., Triple, CodeGenOptLevel) |
| 51 | // is initialized in GlobalInitialization() |
| 52 | // |
| 53 | static bool GlobalInitialized; |
| 54 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 55 | // If given, this will be the name of the target triple to compile for. |
| 56 | // If not given, the initial values defined in this file will be used. |
| 57 | static std::string Triple; |
| 58 | |
| 59 | static llvm::CodeGenOpt::Level CodeGenOptLevel; |
| 60 | |
| 61 | // End of section of GlobalInitializing variables |
| 62 | ///////////////////////////////////////////////////////////////////////// |
| 63 | // If given, the name of the target CPU to generate code for. |
| 64 | static std::string CPU; |
| 65 | |
| 66 | // The list of target specific features to enable or disable -- this should |
| 67 | // be a list of strings starting with '+' (enable) or '-' (disable). |
| 68 | static std::vector<std::string> Features; |
| 69 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 70 | static void LLVMErrorHandler(void *UserData, const std::string &Message); |
| 71 | |
| 72 | static const llvm::StringRef PragmaMetadataName; |
| 73 | static const llvm::StringRef ExportVarMetadataName; |
| 74 | static const llvm::StringRef ExportFuncMetadataName; |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 75 | static const llvm::StringRef ObjectSlotMetadataName; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 76 | |
| 77 | friend class CodeEmitter; |
| 78 | friend class CodeMemoryManager; |
| 79 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 80 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 81 | private: |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 82 | ScriptCompiled *mpResult; |
| 83 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 84 | std::string mError; |
| 85 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 86 | // The memory manager for code emitter |
| 87 | llvm::OwningPtr<CodeMemoryManager> mCodeMemMgr; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 88 | |
| 89 | // The CodeEmitter |
| 90 | llvm::OwningPtr<CodeEmitter> mCodeEmitter; |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 91 | |
| 92 | BCCSymbolLookupFn mpSymbolLookupFn; |
| 93 | void *mpSymbolLookupContext; |
| 94 | |
| 95 | llvm::LLVMContext *mContext; |
| 96 | llvm::Module *mModule; |
| 97 | |
| 98 | bool mHasLinked; |
| 99 | |
| 100 | public: |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 101 | Compiler(ScriptCompiled *result); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 102 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 103 | static void GlobalInitialization(); |
| 104 | |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 105 | void registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 106 | mpSymbolLookupFn = pFn; |
| 107 | mpSymbolLookupContext = pContext; |
| 108 | } |
| 109 | |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 110 | CodeMemoryManager *createCodeMemoryManager(); |
| 111 | |
| 112 | CodeEmitter *createCodeEmitter(); |
| 113 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame^] | 114 | llvm::Module *parseBitcodeFile(llvm::MemoryBuffer *MEM); |
| 115 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 116 | int readModule(llvm::Module *module) { |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 117 | mModule = module; |
| 118 | return hasError(); |
| 119 | } |
| 120 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame^] | 121 | int linkModule(llvm::Module *module); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 122 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 123 | int compile(); |
| 124 | |
Logan | 38d0607 | 2010-12-29 00:16:39 +0800 | [diff] [blame] | 125 | char const *getErrorMessage() { |
| 126 | return mError.c_str(); |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 127 | } |
| 128 | |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 129 | const llvm::Module *getModule() const { |
| 130 | return mModule; |
| 131 | } |
| 132 | |
| 133 | ~Compiler(); |
| 134 | |
| 135 | private: |
Logan | de2ca79 | 2010-11-27 22:15:39 +0800 | [diff] [blame] | 136 | |
| 137 | bool hasError() const { |
| 138 | return !mError.empty(); |
| 139 | } |
| 140 | |
| 141 | void setError(const char *Error) { |
| 142 | mError.assign(Error); // Copying |
| 143 | } |
| 144 | |
| 145 | void setError(const std::string &Error) { |
| 146 | mError = Error; |
| 147 | } |
| 148 | |
Shih-wei Liao | 2417cef | 2010-12-16 05:51:40 -0800 | [diff] [blame] | 149 | }; // End of class Compiler |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 150 | |
Logan | 9b504eb | 2010-11-27 18:19:35 +0800 | [diff] [blame] | 151 | } // namespace bcc |
Logan | 1f028c0 | 2010-11-27 01:02:48 +0800 | [diff] [blame] | 152 | |
| 153 | #endif // BCC_COMPILER_H |