Logan | cf3e521 | 2010-12-29 01:44:55 +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 | #define LOG_TAG "bcc" |
| 18 | #include <cutils/log.h> |
| 19 | |
| 20 | #include "Script.h" |
| 21 | |
| 22 | #include "ScriptCompiled.h" |
| 23 | |
| 24 | #include <new> |
| 25 | |
| 26 | namespace bcc { |
| 27 | |
| 28 | Script::~Script() { |
| 29 | if (mStatus == ScriptStatus::Compiled) { |
| 30 | delete mCompiled; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | |
| 35 | int Script::readBC(const char *bitcode, |
| 36 | size_t bitcodeSize, |
| 37 | long bitcodeFileModTime, |
| 38 | long bitcodeFileCRC32, |
| 39 | const BCCchar *resName, |
| 40 | const BCCchar *cacheDir) { |
Logan | 3a098f9 | 2011-01-01 03:46:30 +0800 | [diff] [blame] | 41 | if (mStatus == ScriptStatus::Unknown) { |
| 42 | mCompiled = new (nothrow) ScriptCompiled(this); |
| 43 | |
| 44 | if (!mCompiled) { |
| 45 | mErrorCode = BCC_OUT_OF_MEMORY; |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | mStatus = ScriptStatus::Compiled; |
| 50 | } |
| 51 | |
| 52 | if (mStatus != ScriptStatus::Compiled) { |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 53 | mErrorCode = BCC_INVALID_OPERATION; |
| 54 | return 1; |
| 55 | } |
| 56 | |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 57 | if (mpExtSymbolLookupFn) { |
| 58 | mCompiled->registerSymbolCallback(mpExtSymbolLookupFn, |
| 59 | mpExtSymbolLookupFnContext); |
| 60 | } |
| 61 | |
| 62 | return mCompiled->readBC(bitcode, bitcodeSize, |
| 63 | bitcodeFileModTime, bitcodeFileCRC32, |
| 64 | resName, cacheDir); |
| 65 | } |
| 66 | |
| 67 | |
| 68 | int Script::readModule(llvm::Module *module) { |
| 69 | if (mStatus != ScriptStatus::Unknown) { |
| 70 | mErrorCode = BCC_INVALID_OPERATION; |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | mCompiled = new (nothrow) ScriptCompiled(this); |
| 75 | |
| 76 | if (!mCompiled) { |
| 77 | mErrorCode = BCC_OUT_OF_MEMORY; |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | mStatus = ScriptStatus::Compiled; |
| 82 | |
| 83 | if (mpExtSymbolLookupFn) { |
| 84 | mCompiled->registerSymbolCallback(mpExtSymbolLookupFn, |
| 85 | mpExtSymbolLookupFnContext); |
| 86 | } |
| 87 | |
| 88 | return mCompiled->readModule(module); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | int Script::linkBC(const char *bitcode, size_t bitcodeSize) { |
| 93 | if (mStatus != ScriptStatus::Compiled) { |
| 94 | mErrorCode = BCC_INVALID_OPERATION; |
| 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | return mCompiled->linkBC(bitcode, bitcodeSize); |
| 99 | } |
| 100 | |
| 101 | |
| 102 | int Script::loadCacheFile() { |
| 103 | if (mStatus != ScriptStatus::Compiled) { |
| 104 | mErrorCode = BCC_INVALID_OPERATION; |
| 105 | return 1; |
| 106 | } |
| 107 | |
| 108 | return mCompiled->loadCacheFile(); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | int Script::compile() { |
| 113 | if (mStatus != ScriptStatus::Compiled) { |
| 114 | mErrorCode = BCC_INVALID_OPERATION; |
| 115 | return 1; |
| 116 | } |
| 117 | |
| 118 | return mCompiled->compile(); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | char const *Script::getCompilerErrorMessage() { |
| 123 | if (mStatus != ScriptStatus::Compiled) { |
| 124 | mErrorCode = BCC_INVALID_OPERATION; |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | return mCompiled->getCompilerErrorMessage(); |
| 129 | } |
| 130 | |
| 131 | |
| 132 | void *Script::lookup(const char *name) { |
| 133 | if (mStatus != ScriptStatus::Compiled) { |
| 134 | mErrorCode = BCC_INVALID_OPERATION; |
| 135 | return NULL; |
| 136 | } |
| 137 | |
| 138 | return mCompiled->lookup(name); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | void Script::getExportVars(BCCsizei *actualVarCount, |
| 143 | BCCsizei maxVarCount, |
| 144 | BCCvoid **vars) { |
| 145 | if (mStatus != ScriptStatus::Compiled) { |
| 146 | mErrorCode = BCC_INVALID_OPERATION; |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | mCompiled->getExportVars(actualVarCount, maxVarCount, vars); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | void Script::getExportFuncs(BCCsizei *actualFuncCount, |
| 155 | BCCsizei maxFuncCount, |
| 156 | BCCvoid **funcs) { |
| 157 | if (mStatus != ScriptStatus::Compiled) { |
| 158 | mErrorCode = BCC_INVALID_OPERATION; |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | mCompiled->getExportFuncs(actualFuncCount, maxFuncCount, funcs); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | void Script::getPragmas(BCCsizei *actualStringCount, |
| 167 | BCCsizei maxStringCount, |
| 168 | BCCchar **strings) { |
| 169 | if (mStatus != ScriptStatus::Compiled) { |
| 170 | mErrorCode = BCC_INVALID_OPERATION; |
| 171 | return; |
| 172 | } |
| 173 | |
| 174 | mCompiled->getPragmas(actualStringCount, maxStringCount, strings); |
| 175 | } |
| 176 | |
| 177 | |
| 178 | void Script::getFunctions(BCCsizei *actualFunctionCount, |
| 179 | BCCsizei maxFunctionCount, |
| 180 | BCCchar **functions) { |
| 181 | if (mStatus != ScriptStatus::Compiled) { |
| 182 | mErrorCode = BCC_INVALID_OPERATION; |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | mCompiled->getFunctions(actualFunctionCount, maxFunctionCount, functions); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | void Script::getFunctionBinary(BCCchar *function, |
| 191 | BCCvoid **base, |
| 192 | BCCsizei *length) { |
| 193 | if (mStatus != ScriptStatus::Compiled) { |
| 194 | mErrorCode = BCC_INVALID_OPERATION; |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | mCompiled->getFunctionBinary(function, base, length); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | void Script::registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext) { |
| 203 | mpExtSymbolLookupFn = pFn; |
| 204 | mpExtSymbolLookupFnContext = pContext; |
| 205 | |
| 206 | if (mStatus != ScriptStatus::Compiled) { |
| 207 | mErrorCode = BCC_INVALID_OPERATION; |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | mCompiled->registerSymbolCallback(pFn, pContext); |
| 212 | } |
| 213 | |
| 214 | } // namespace bcc |