Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 2 | * copyright 2010-2012, the android open source project |
Logan | cf3e521 | 2010-12-29 01:44:55 +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 | |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 17 | #include "Script.h" |
| 18 | |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 19 | #include "Config.h" |
Stephen Hines | b67c9e7 | 2012-03-22 11:02:48 -0700 | [diff] [blame] | 20 | #include "bcinfo/BitcodeWrapper.h" |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 21 | |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 22 | #include "MCCacheReader.h" |
| 23 | #include "MCCacheWriter.h" |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 24 | #include "CompilerOption.h" |
Logan Chien | d2a5f30 | 2011-07-19 20:32:25 +0800 | [diff] [blame] | 25 | |
Logan | 4dcd679 | 2011-02-28 05:12:00 +0800 | [diff] [blame] | 26 | #include "DebugHelper.h" |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 27 | #include "FileHandle.h" |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 28 | #include "GDBJITRegistrar.h" |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 29 | #include "ScriptCompiled.h" |
Logan | 9a5f868 | 2011-01-07 06:09:57 +0800 | [diff] [blame] | 30 | #include "ScriptCached.h" |
| 31 | #include "Sha1Helper.h" |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 32 | #include "SourceInfo.h" |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 33 | |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 34 | #include <errno.h> |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 35 | #include <sys/stat.h> |
| 36 | #include <sys/types.h> |
Bhanu Chetlapalli | eb4509b | 2012-01-08 20:42:56 -0800 | [diff] [blame] | 37 | #include <unistd.h> |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 38 | |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 39 | #include <new> |
| 40 | #include <string.h> |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 41 | #include <cutils/properties.h> |
| 42 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 43 | namespace { |
| 44 | |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 45 | bool getBooleanProp(const char *str) { |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 46 | char buf[PROPERTY_VALUE_MAX]; |
| 47 | property_get(str, buf, "0"); |
| 48 | return strcmp(buf, "0") != 0; |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 49 | } |
| 50 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 51 | } // namespace anonymous |
| 52 | |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 53 | namespace bcc { |
| 54 | |
| 55 | Script::~Script() { |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 56 | switch (mStatus) { |
| 57 | case ScriptStatus::Compiled: |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 58 | delete mCompiled; |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 59 | break; |
| 60 | |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 61 | case ScriptStatus::Cached: |
Shih-wei Liao | c4cf654 | 2011-01-13 01:43:01 -0800 | [diff] [blame] | 62 | delete mCached; |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 63 | break; |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 64 | |
| 65 | default: |
| 66 | break; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 67 | } |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 68 | |
Shih-wei Liao | 6a60f4e | 2012-01-17 02:58:40 -0800 | [diff] [blame] | 69 | for (size_t i = 0; i < 2; ++i) { |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 70 | delete mSourceList[i]; |
Shih-wei Liao | 6a60f4e | 2012-01-17 02:58:40 -0800 | [diff] [blame] | 71 | } |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 75 | int Script::addSourceBC(size_t idx, |
| 76 | char const *resName, |
| 77 | const char *bitcode, |
| 78 | size_t bitcodeSize, |
| 79 | unsigned long flags) { |
Shih-wei Liao | 898c5a9 | 2011-05-18 07:02:39 -0700 | [diff] [blame] | 80 | |
| 81 | if (!resName) { |
| 82 | mErrorCode = BCC_INVALID_VALUE; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 83 | ALOGE("Invalid argument: resName = NULL\n"); |
Shih-wei Liao | 898c5a9 | 2011-05-18 07:02:39 -0700 | [diff] [blame] | 84 | return 1; |
| 85 | } |
| 86 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 87 | if (mStatus != ScriptStatus::Unknown) { |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 88 | mErrorCode = BCC_INVALID_OPERATION; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 89 | ALOGE("Bad operation: Adding source after bccPrepareExecutable\n"); |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 90 | return 1; |
| 91 | } |
| 92 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 93 | if (!bitcode) { |
| 94 | mErrorCode = BCC_INVALID_VALUE; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 95 | ALOGE("Invalid argument: bitcode = NULL\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 96 | return 1; |
| 97 | } |
| 98 | |
Stephen Hines | b67c9e7 | 2012-03-22 11:02:48 -0700 | [diff] [blame] | 99 | bcinfo::BitcodeWrapper wrapper(bitcode, bitcodeSize); |
| 100 | mCompilerVersion = wrapper.getCompilerVersion(); |
| 101 | mOptimizationLevel = wrapper.getOptimizationLevel(); |
| 102 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 103 | mSourceList[idx] = SourceInfo::createFromBuffer(resName, |
| 104 | bitcode, bitcodeSize, |
| 105 | flags); |
| 106 | |
| 107 | if (!mSourceList[idx]) { |
| 108 | mErrorCode = BCC_OUT_OF_MEMORY; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 109 | ALOGE("Out of memory while adding source bitcode\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 110 | return 1; |
| 111 | } |
| 112 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 113 | return 0; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 117 | int Script::addSourceModule(size_t idx, |
| 118 | llvm::Module *module, |
| 119 | unsigned long flags) { |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 120 | if (mStatus != ScriptStatus::Unknown) { |
| 121 | mErrorCode = BCC_INVALID_OPERATION; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 122 | ALOGE("Bad operation: Adding source after bccPrepareExecutable\n"); |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 123 | return 1; |
| 124 | } |
| 125 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 126 | if (!module) { |
| 127 | mErrorCode = BCC_INVALID_VALUE; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 128 | ALOGE("Invalid argument: module = NULL\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 129 | return 1; |
| 130 | } |
| 131 | |
| 132 | mSourceList[idx] = SourceInfo::createFromModule(module, flags); |
| 133 | |
| 134 | if (!mSourceList[idx]) { |
| 135 | mErrorCode = BCC_OUT_OF_MEMORY; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 136 | ALOGE("Out of memory when add source module\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 137 | return 1; |
| 138 | } |
| 139 | |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 140 | return 0; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 144 | int Script::addSourceFile(size_t idx, |
| 145 | char const *path, |
| 146 | unsigned long flags) { |
Logan | 3133c41 | 2011-01-06 06:15:40 +0800 | [diff] [blame] | 147 | if (mStatus != ScriptStatus::Unknown) { |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 148 | mErrorCode = BCC_INVALID_OPERATION; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 149 | ALOGE("Bad operation: Adding source after bccPrepareExecutable\n"); |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 150 | return 1; |
| 151 | } |
| 152 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 153 | if (!path) { |
| 154 | mErrorCode = BCC_INVALID_VALUE; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 155 | ALOGE("Invalid argument: path = NULL\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | struct stat sb; |
| 160 | if (stat(path, &sb) != 0) { |
| 161 | mErrorCode = BCC_INVALID_VALUE; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 162 | ALOGE("File not found: %s\n", path); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 163 | return 1; |
| 164 | } |
| 165 | |
| 166 | mSourceList[idx] = SourceInfo::createFromFile(path, flags); |
| 167 | |
| 168 | if (!mSourceList[idx]) { |
| 169 | mErrorCode = BCC_OUT_OF_MEMORY; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 170 | ALOGE("Out of memory while adding source file\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 171 | return 1; |
| 172 | } |
| 173 | |
Logan | 3133c41 | 2011-01-06 06:15:40 +0800 | [diff] [blame] | 174 | return 0; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 175 | } |
| 176 | |
Shih-wei Liao | d8ed6a9 | 2012-03-03 01:44:29 -0800 | [diff] [blame] | 177 | int Script::prepareRelocatable(char const *objPath, |
Shih-wei Liao | 6a60f4e | 2012-01-17 02:58:40 -0800 | [diff] [blame] | 178 | llvm::Reloc::Model RelocModel, |
| 179 | unsigned long flags) { |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 180 | CompilerOption option; |
Shih-wei Liao | 8afed38 | 2012-01-10 15:57:24 +0800 | [diff] [blame] | 181 | option.RelocModelOpt = RelocModel; |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 182 | option.LoadAfterCompile = false; |
Shih-wei Liao | 8454a3a | 2012-03-03 01:50:08 -0800 | [diff] [blame] | 183 | |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 184 | int status = internalCompile(option); |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 185 | if (status != 0) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 186 | ALOGE("LLVM error message: %s\n", getCompilerErrorMessage()); |
Shih-wei Liao | a0ed34e | 2012-03-03 01:33:30 -0800 | [diff] [blame] | 187 | return status; |
| 188 | } |
Shih-wei Liao | d8ed6a9 | 2012-03-03 01:44:29 -0800 | [diff] [blame] | 189 | |
| 190 | FileHandle objFile; |
| 191 | if (objFile.open(objPath, OpenMode::Write) < 0) { |
| 192 | ALOGE("Failed to open %s for write.\n", objPath); |
| 193 | return 1; |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 194 | } |
Shih-wei Liao | d8ed6a9 | 2012-03-03 01:44:29 -0800 | [diff] [blame] | 195 | |
| 196 | if (static_cast<size_t>(objFile.write(getELF(), |
| 197 | getELFSize())) != getELFSize()) { |
| 198 | objFile.close(); |
| 199 | ::unlink(objPath); |
| 200 | ALOGE("Unable to write ELF to file %s.\n", objPath); |
| 201 | return false; |
| 202 | } |
| 203 | |
Shih-wei Liao | 8454a3a | 2012-03-03 01:50:08 -0800 | [diff] [blame] | 204 | mObjectType = ScriptObject::Relocatable; |
| 205 | |
Shih-wei Liao | d8ed6a9 | 2012-03-03 01:44:29 -0800 | [diff] [blame] | 206 | return 0; |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 209 | |
Shih-wei Liao | 6934174 | 2012-03-03 01:45:36 -0800 | [diff] [blame] | 210 | int Script::prepareSharedObject(char const *objPath, |
Shih-wei Liao | a471ebb | 2012-02-05 00:49:58 -0800 | [diff] [blame] | 211 | char const *dsoPath, |
| 212 | unsigned long flags) { |
| 213 | // TODO: Support cached shared object. |
| 214 | return 1; |
| 215 | } |
| 216 | |
| 217 | |
Logan Chien | 311c26f | 2011-07-11 14:30:34 +0800 | [diff] [blame] | 218 | int Script::prepareExecutable(char const *cacheDir, |
| 219 | char const *cacheName, |
| 220 | unsigned long flags) { |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 221 | if (mStatus != ScriptStatus::Unknown) { |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 222 | mErrorCode = BCC_INVALID_OPERATION; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 223 | ALOGE("Invalid operation: %s\n", __func__); |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 224 | return 1; |
| 225 | } |
| 226 | |
Stephen Hines | 0e56786 | 2012-03-11 20:26:40 -0700 | [diff] [blame] | 227 | int status = internalLoadCache(cacheDir, cacheName, /* checkOnly */ false); |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 228 | |
Stephen Hines | e0918ac | 2012-03-01 23:28:09 -0800 | [diff] [blame] | 229 | if (status != 0) { |
| 230 | CompilerOption option; |
| 231 | status = internalCompile(option); |
Stephen Hines | e0918ac | 2012-03-01 23:28:09 -0800 | [diff] [blame] | 232 | |
Shih-wei Liao | a0ed34e | 2012-03-03 01:33:30 -0800 | [diff] [blame] | 233 | if (status != 0) { |
| 234 | ALOGE("LLVM error message: %s\n", getCompilerErrorMessage()); |
| 235 | return status; |
| 236 | } |
| 237 | |
| 238 | status = writeCache(); |
| 239 | if (status != 0) { |
| 240 | ALOGE("Failed to write the cache for %s\n", cacheName); |
| 241 | return status; |
| 242 | } |
Stephen Hines | 27b3510 | 2011-05-11 17:58:48 -0700 | [diff] [blame] | 243 | } |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 244 | |
| 245 | // FIXME: Registration can be conditional on the presence of debug metadata |
Shih-wei Liao | a0ed34e | 2012-03-03 01:33:30 -0800 | [diff] [blame] | 246 | registerObjectWithGDB(getELF(), getELFSize()); // thread-safe registration |
| 247 | |
Shih-wei Liao | 8454a3a | 2012-03-03 01:50:08 -0800 | [diff] [blame] | 248 | mObjectType = ScriptObject::Executable; |
| 249 | |
Stephen Hines | 27b3510 | 2011-05-11 17:58:48 -0700 | [diff] [blame] | 250 | return status; |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 251 | } |
| 252 | |
Zonr Chang | 743dd71 | 2012-01-19 10:13:52 +0800 | [diff] [blame] | 253 | int Script::internalLoadCache(char const *cacheDir, char const *cacheName, |
Zonr Chang | 743dd71 | 2012-01-19 10:13:52 +0800 | [diff] [blame] | 254 | bool checkOnly) { |
Shih-wei Liao | 32ef88b | 2012-02-04 23:33:11 -0800 | [diff] [blame] | 255 | if ((cacheDir == NULL) || (cacheName == NULL)) { |
Zonr Chang | 743dd71 | 2012-01-19 10:13:52 +0800 | [diff] [blame] | 256 | return 1; |
Shih-wei Liao | 32ef88b | 2012-02-04 23:33:11 -0800 | [diff] [blame] | 257 | } |
Zonr Chang | 743dd71 | 2012-01-19 10:13:52 +0800 | [diff] [blame] | 258 | |
| 259 | // Set cache file Name |
| 260 | mCacheName = cacheName; |
| 261 | |
Shih-wei Liao | 32ef88b | 2012-02-04 23:33:11 -0800 | [diff] [blame] | 262 | // Santize mCacheDir. Ensure that mCacheDir ends with '/'. |
Zonr Chang | 743dd71 | 2012-01-19 10:13:52 +0800 | [diff] [blame] | 263 | mCacheDir = cacheDir; |
Shih-wei Liao | 32ef88b | 2012-02-04 23:33:11 -0800 | [diff] [blame] | 264 | if (!mCacheDir.empty() && *mCacheDir.rbegin() != '/') { |
Zonr Chang | 743dd71 | 2012-01-19 10:13:52 +0800 | [diff] [blame] | 265 | mCacheDir.push_back('/'); |
Shih-wei Liao | 32ef88b | 2012-02-04 23:33:11 -0800 | [diff] [blame] | 266 | } |
Zonr Chang | 743dd71 | 2012-01-19 10:13:52 +0800 | [diff] [blame] | 267 | |
Shih-wei Liao | 32ef88b | 2012-02-04 23:33:11 -0800 | [diff] [blame] | 268 | if (!isCacheable()) { |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 269 | return 1; |
Shih-wei Liao | 32ef88b | 2012-02-04 23:33:11 -0800 | [diff] [blame] | 270 | } |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 271 | |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 272 | std::string objPath = getCachedObjectPath(); |
| 273 | std::string infoPath = getCacheInfoPath(); |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 274 | |
Logan Chien | 311c26f | 2011-07-11 14:30:34 +0800 | [diff] [blame] | 275 | FileHandle objFile; |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 276 | if (objFile.open(objPath.c_str(), OpenMode::Read) < 0) { |
Logan Chien | 03a2e30 | 2011-07-13 21:46:32 +0800 | [diff] [blame] | 277 | // Unable to open the executable file in read mode. |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 278 | return 1; |
| 279 | } |
| 280 | |
Logan Chien | 311c26f | 2011-07-11 14:30:34 +0800 | [diff] [blame] | 281 | FileHandle infoFile; |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 282 | if (infoFile.open(infoPath.c_str(), OpenMode::Read) < 0) { |
Logan Chien | 03a2e30 | 2011-07-13 21:46:32 +0800 | [diff] [blame] | 283 | // Unable to open the metadata information file in read mode. |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 284 | return 1; |
| 285 | } |
| 286 | |
| 287 | MCCacheReader reader; |
| 288 | |
| 289 | // Register symbol lookup function |
| 290 | if (mpExtSymbolLookupFn) { |
| 291 | reader.registerSymbolCallback(mpExtSymbolLookupFn, |
| 292 | mpExtSymbolLookupFnContext); |
| 293 | } |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 294 | |
Logan | 9a5f868 | 2011-01-07 06:09:57 +0800 | [diff] [blame] | 295 | // Dependencies |
Joseph Wen | 2ca6e57 | 2011-06-24 14:12:23 -0700 | [diff] [blame] | 296 | reader.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1); |
Joseph Wen | 7691907 | 2011-07-07 23:06:15 -0700 | [diff] [blame] | 297 | reader.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS); |
Logan | 9a5f868 | 2011-01-07 06:09:57 +0800 | [diff] [blame] | 298 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 299 | for (size_t i = 0; i < 2; ++i) { |
| 300 | if (mSourceList[i]) { |
| 301 | mSourceList[i]->introDependency(reader); |
| 302 | } |
Logan | 9a5f868 | 2011-01-07 06:09:57 +0800 | [diff] [blame] | 303 | } |
| 304 | |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 305 | if (checkOnly) |
Joseph Wen | 4928104 | 2011-07-26 10:04:09 -0700 | [diff] [blame] | 306 | return !reader.checkCacheFile(&objFile, &infoFile, this); |
Joseph Wen | 34c600a | 2011-07-25 17:59:17 -0700 | [diff] [blame] | 307 | |
Logan | 9a5f868 | 2011-01-07 06:09:57 +0800 | [diff] [blame] | 308 | // Read cache file |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 309 | ScriptCached *cached = reader.readCacheFile(&objFile, &infoFile, this); |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 310 | |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 311 | if (!cached) { |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 312 | mIsContextSlotNotAvail = reader.isContextSlotNotAvail(); |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | mCached = cached; |
| 317 | mStatus = ScriptStatus::Cached; |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 318 | |
Logan | f3c83ce | 2011-01-07 06:36:33 +0800 | [diff] [blame] | 319 | // Dirty hack for libRS. |
| 320 | // TODO(all): This dirty hack should be removed in the future. |
Shih-wei Liao | 8eb5fe9 | 2011-02-01 04:17:38 -0800 | [diff] [blame] | 321 | if (!cached->isLibRSThreadable() && mpExtSymbolLookupFn) { |
Logan | f3c83ce | 2011-01-07 06:36:33 +0800 | [diff] [blame] | 322 | mpExtSymbolLookupFn(mpExtSymbolLookupFnContext, "__clearThreadable"); |
| 323 | } |
| 324 | |
Logan | f7f0ac5 | 2011-01-07 03:53:43 +0800 | [diff] [blame] | 325 | return 0; |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 326 | } |
| 327 | |
Shih-wei Liao | 9e81e37 | 2012-01-17 16:38:40 -0800 | [diff] [blame] | 328 | int Script::internalCompile(const CompilerOption &option) { |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 329 | // Create the ScriptCompiled object |
Nowar Gu | 09b6c1c | 2011-05-24 23:49:07 +0800 | [diff] [blame] | 330 | mCompiled = new (std::nothrow) ScriptCompiled(this); |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 331 | |
| 332 | if (!mCompiled) { |
| 333 | mErrorCode = BCC_OUT_OF_MEMORY; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 334 | ALOGE("Out of memory: %s %d\n", __FILE__, __LINE__); |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 335 | return 1; |
| 336 | } |
| 337 | |
| 338 | mStatus = ScriptStatus::Compiled; |
| 339 | |
Logan | 033f46e | 2011-01-06 05:51:24 +0800 | [diff] [blame] | 340 | // Register symbol lookup function |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 341 | if (mpExtSymbolLookupFn) { |
| 342 | mCompiled->registerSymbolCallback(mpExtSymbolLookupFn, |
| 343 | mpExtSymbolLookupFnContext); |
| 344 | } |
| 345 | |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 346 | if (!mSourceList[0]) { |
| 347 | ALOGE("Source bitcode is not set.\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 348 | return 1; |
| 349 | } |
| 350 | |
Shih-wei Liao | d88c0d1 | 2012-01-17 20:32:58 -0800 | [diff] [blame] | 351 | // Parse Source bitcode file (if necessary) |
| 352 | if (mSourceList[0]->prepareModule() != 0) { |
| 353 | ALOGE("Unable to setup source module\n"); |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 354 | return 1; |
Shih-wei Liao | d88c0d1 | 2012-01-17 20:32:58 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | // Parse Library bitcode file (if necessary) |
| 358 | if (mSourceList[1]) { |
| 359 | if (mSourceList[1]->prepareModule(mSourceList[0]->getContext()) != 0) { |
| 360 | ALOGE("Unable to setup library module\n"); |
| 361 | return 1; |
| 362 | } |
| 363 | } |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 364 | |
| 365 | // Set the main source module |
| 366 | if (mCompiled->readModule(mSourceList[0]->getModule()) != 0) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 367 | ALOGE("Unable to read source module\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 368 | return 1; |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 369 | } |
| 370 | |
Logan | 3133c41 | 2011-01-06 06:15:40 +0800 | [diff] [blame] | 371 | // Link the source module with the library module |
Shih-wei Liao | d88c0d1 | 2012-01-17 20:32:58 -0800 | [diff] [blame] | 372 | if (mSourceList[1]) { |
Zonr Chang | df3fee4 | 2012-01-10 15:58:36 +0800 | [diff] [blame] | 373 | if (mCompiled->linkModule(mSourceList[1]->getModule()) != 0) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 374 | ALOGE("Unable to link library module\n"); |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 375 | return 1; |
| 376 | } |
| 377 | } |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 378 | |
Logan | 3133c41 | 2011-01-06 06:15:40 +0800 | [diff] [blame] | 379 | // Compile and JIT the code |
Zonr Chang | 2fcbd02 | 2012-01-06 21:04:31 +0800 | [diff] [blame] | 380 | if (mCompiled->compile(option) != 0) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 381 | ALOGE("Unable to compile.\n"); |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 382 | return 1; |
| 383 | } |
| 384 | |
Shih-wei Liao | a0ed34e | 2012-03-03 01:33:30 -0800 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
| 388 | int Script::writeCache() { |
Stephen Hines | 3e36c69 | 2012-03-05 12:39:45 -0800 | [diff] [blame] | 389 | // Not compiled script or encountered error during the compilation. |
Shih-wei Liao | a0ed34e | 2012-03-03 01:33:30 -0800 | [diff] [blame] | 390 | if ((mStatus != ScriptStatus::Compiled) || |
| 391 | (getCompilerErrorMessage() == NULL)) |
| 392 | return 1; |
| 393 | |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 394 | // Note: If we re-compile the script because the cached context slot not |
| 395 | // available, then we don't have to write the cache. |
| 396 | |
| 397 | // Note: If the address of the context is not in the context slot, then |
| 398 | // we don't have to cache it. |
| 399 | |
Stephen Hines | 9ddeb6b | 2012-03-11 18:41:03 -0700 | [diff] [blame] | 400 | if (isCacheable()) { |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 401 | |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 402 | std::string objPath = getCachedObjectPath(); |
| 403 | std::string infoPath = getCacheInfoPath(); |
Logan Chien | 311c26f | 2011-07-11 14:30:34 +0800 | [diff] [blame] | 404 | |
Jeff Brown | 937a0bc | 2011-01-26 23:20:14 -0800 | [diff] [blame] | 405 | // Remove the file if it already exists before writing the new file. |
| 406 | // The old file may still be mapped elsewhere in memory and we do not want |
| 407 | // to modify its contents. (The same script may be running concurrently in |
| 408 | // the same process or a different process!) |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 409 | ::unlink(objPath.c_str()); |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 410 | ::unlink(infoPath.c_str()); |
Jeff Brown | 937a0bc | 2011-01-26 23:20:14 -0800 | [diff] [blame] | 411 | |
Logan Chien | 03a2e30 | 2011-07-13 21:46:32 +0800 | [diff] [blame] | 412 | FileHandle objFile; |
| 413 | FileHandle infoFile; |
| 414 | |
| 415 | if (objFile.open(objPath.c_str(), OpenMode::Write) >= 0 && |
| 416 | infoFile.open(infoPath.c_str(), OpenMode::Write) >= 0) { |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 417 | |
Logan Chien | 311c26f | 2011-07-11 14:30:34 +0800 | [diff] [blame] | 418 | MCCacheWriter writer; |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 419 | |
Joseph Wen | 2ca6e57 | 2011-06-24 14:12:23 -0700 | [diff] [blame] | 420 | #ifdef TARGET_BUILD |
Logan | a2e15af | 2011-01-07 11:46:08 +0800 | [diff] [blame] | 421 | // Dependencies |
Joseph Wen | 2ca6e57 | 2011-06-24 14:12:23 -0700 | [diff] [blame] | 422 | writer.addDependency(BCC_FILE_RESOURCE, pathLibBCC_SHA1, sha1LibBCC_SHA1); |
Joseph Wen | 7691907 | 2011-07-07 23:06:15 -0700 | [diff] [blame] | 423 | writer.addDependency(BCC_FILE_RESOURCE, pathLibRS, sha1LibRS); |
Logan | e132399 | 2011-01-12 04:47:13 +0800 | [diff] [blame] | 424 | #endif |
Logan | a2e15af | 2011-01-07 11:46:08 +0800 | [diff] [blame] | 425 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 426 | for (size_t i = 0; i < 2; ++i) { |
Logan | 825c3b2 | 2011-02-28 05:05:48 +0800 | [diff] [blame] | 427 | if (mSourceList[i]) { |
| 428 | mSourceList[i]->introDependency(writer); |
| 429 | } |
Logan | a2e15af | 2011-01-07 11:46:08 +0800 | [diff] [blame] | 430 | } |
| 431 | |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 432 | // libRS is threadable dirty hack |
| 433 | // TODO: This should be removed in the future |
| 434 | uint32_t libRS_threadable = 0; |
| 435 | if (mpExtSymbolLookupFn) { |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 436 | libRS_threadable = |
| 437 | (uint32_t)mpExtSymbolLookupFn(mpExtSymbolLookupFnContext, |
| 438 | "__isThreadable"); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 439 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 440 | |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 441 | if (!writer.writeCacheFile(&objFile, &infoFile, this, libRS_threadable)) { |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 442 | objFile.truncate(); |
| 443 | objFile.close(); |
Logan | a27a83f | 2011-01-07 10:25:48 +0800 | [diff] [blame] | 444 | |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 445 | if (unlink(objPath.c_str()) != 0) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 446 | ALOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n", |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 447 | objPath.c_str(), strerror(errno)); |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 448 | } |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 449 | |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 450 | infoFile.truncate(); |
| 451 | infoFile.close(); |
| 452 | |
| 453 | if (unlink(infoPath.c_str()) != 0) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 454 | ALOGE("Unable to remove the invalid cache file: %s. (reason: %s)\n", |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 455 | infoPath.c_str(), strerror(errno)); |
| 456 | } |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 457 | } |
| 458 | } |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 459 | } |
Logan | 0432971 | 2011-01-06 06:10:57 +0800 | [diff] [blame] | 460 | |
| 461 | return 0; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | |
| 465 | char const *Script::getCompilerErrorMessage() { |
| 466 | if (mStatus != ScriptStatus::Compiled) { |
| 467 | mErrorCode = BCC_INVALID_OPERATION; |
| 468 | return NULL; |
| 469 | } |
| 470 | |
| 471 | return mCompiled->getCompilerErrorMessage(); |
| 472 | } |
| 473 | |
| 474 | |
| 475 | void *Script::lookup(const char *name) { |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 476 | switch (mStatus) { |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 477 | case ScriptStatus::Compiled: { |
| 478 | return mCompiled->lookup(name); |
| 479 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 480 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 481 | case ScriptStatus::Cached: { |
| 482 | return mCached->lookup(name); |
| 483 | } |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 484 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 485 | default: { |
| 486 | mErrorCode = BCC_INVALID_OPERATION; |
| 487 | return NULL; |
| 488 | } |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 489 | } |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 493 | size_t Script::getExportVarCount() const { |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 494 | switch (mStatus) { |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 495 | case ScriptStatus::Compiled: { |
| 496 | return mCompiled->getExportVarCount(); |
| 497 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 498 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 499 | case ScriptStatus::Cached: { |
| 500 | return mCached->getExportVarCount(); |
| 501 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 502 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 503 | default: { |
| 504 | return 0; |
| 505 | } |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
| 509 | |
| 510 | size_t Script::getExportFuncCount() const { |
| 511 | switch (mStatus) { |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 512 | case ScriptStatus::Compiled: { |
| 513 | return mCompiled->getExportFuncCount(); |
| 514 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 515 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 516 | case ScriptStatus::Cached: { |
| 517 | return mCached->getExportFuncCount(); |
| 518 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 519 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 520 | default: { |
| 521 | return 0; |
| 522 | } |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 527 | size_t Script::getExportForEachCount() const { |
| 528 | switch (mStatus) { |
| 529 | case ScriptStatus::Compiled: { |
| 530 | return mCompiled->getExportForEachCount(); |
| 531 | } |
| 532 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 533 | case ScriptStatus::Cached: { |
| 534 | return mCached->getExportForEachCount(); |
| 535 | } |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 536 | |
| 537 | default: { |
| 538 | return 0; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 544 | size_t Script::getPragmaCount() const { |
| 545 | switch (mStatus) { |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 546 | case ScriptStatus::Compiled: { |
| 547 | return mCompiled->getPragmaCount(); |
| 548 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 549 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 550 | case ScriptStatus::Cached: { |
| 551 | return mCached->getPragmaCount(); |
| 552 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 553 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 554 | default: { |
| 555 | return 0; |
| 556 | } |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | |
| 561 | size_t Script::getFuncCount() const { |
| 562 | switch (mStatus) { |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 563 | case ScriptStatus::Compiled: { |
| 564 | return mCompiled->getFuncCount(); |
| 565 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 566 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 567 | case ScriptStatus::Cached: { |
| 568 | return mCached->getFuncCount(); |
| 569 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 570 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 571 | default: { |
| 572 | return 0; |
| 573 | } |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 578 | size_t Script::getObjectSlotCount() const { |
| 579 | switch (mStatus) { |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 580 | case ScriptStatus::Compiled: { |
| 581 | return mCompiled->getObjectSlotCount(); |
| 582 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 583 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 584 | case ScriptStatus::Cached: { |
| 585 | return mCached->getObjectSlotCount(); |
| 586 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 587 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 588 | default: { |
| 589 | return 0; |
| 590 | } |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | |
| 594 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 595 | void Script::getExportVarList(size_t varListSize, void **varList) { |
| 596 | switch (mStatus) { |
| 597 | #define DELEGATE(STATUS) \ |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 598 | case ScriptStatus::STATUS: \ |
| 599 | m##STATUS->getExportVarList(varListSize, varList); \ |
| 600 | break; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 601 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 602 | DELEGATE(Cached); |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 603 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 604 | DELEGATE(Compiled); |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 605 | #undef DELEGATE |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 606 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 607 | default: { |
| 608 | mErrorCode = BCC_INVALID_OPERATION; |
| 609 | } |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 610 | } |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 611 | } |
| 612 | |
Joseph Wen | f36637f | 2011-07-06 18:27:12 -0700 | [diff] [blame] | 613 | void Script::getExportVarNameList(std::vector<std::string> &varList) { |
| 614 | switch (mStatus) { |
| 615 | case ScriptStatus::Compiled: { |
| 616 | return mCompiled->getExportVarNameList(varList); |
| 617 | } |
| 618 | |
| 619 | default: { |
| 620 | mErrorCode = BCC_INVALID_OPERATION; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 625 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 626 | void Script::getExportFuncList(size_t funcListSize, void **funcList) { |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 627 | switch (mStatus) { |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 628 | #define DELEGATE(STATUS) \ |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 629 | case ScriptStatus::STATUS: \ |
| 630 | m##STATUS->getExportFuncList(funcListSize, funcList); \ |
| 631 | break; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 632 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 633 | DELEGATE(Cached); |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 634 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 635 | DELEGATE(Compiled); |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 636 | #undef DELEGATE |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 637 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 638 | default: { |
| 639 | mErrorCode = BCC_INVALID_OPERATION; |
| 640 | } |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 641 | } |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 642 | } |
| 643 | |
Joseph Wen | f36637f | 2011-07-06 18:27:12 -0700 | [diff] [blame] | 644 | void Script::getExportFuncNameList(std::vector<std::string> &funcList) { |
| 645 | switch (mStatus) { |
| 646 | case ScriptStatus::Compiled: { |
| 647 | return mCompiled->getExportFuncNameList(funcList); |
| 648 | } |
| 649 | |
| 650 | default: { |
| 651 | mErrorCode = BCC_INVALID_OPERATION; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 656 | void Script::getExportForEachList(size_t funcListSize, void **funcList) { |
| 657 | switch (mStatus) { |
| 658 | #define DELEGATE(STATUS) \ |
| 659 | case ScriptStatus::STATUS: \ |
| 660 | m##STATUS->getExportForEachList(funcListSize, funcList); \ |
| 661 | break; |
| 662 | |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 663 | DELEGATE(Cached); |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 664 | |
| 665 | DELEGATE(Compiled); |
| 666 | #undef DELEGATE |
| 667 | |
| 668 | default: { |
| 669 | mErrorCode = BCC_INVALID_OPERATION; |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | void Script::getExportForEachNameList(std::vector<std::string> &forEachList) { |
| 675 | switch (mStatus) { |
| 676 | case ScriptStatus::Compiled: { |
| 677 | return mCompiled->getExportForEachNameList(forEachList); |
| 678 | } |
| 679 | |
| 680 | default: { |
| 681 | mErrorCode = BCC_INVALID_OPERATION; |
| 682 | } |
| 683 | } |
| 684 | } |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 685 | |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 686 | void Script::getPragmaList(size_t pragmaListSize, |
| 687 | char const **keyList, |
| 688 | char const **valueList) { |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 689 | switch (mStatus) { |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 690 | #define DELEGATE(STATUS) \ |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 691 | case ScriptStatus::STATUS: \ |
| 692 | m##STATUS->getPragmaList(pragmaListSize, keyList, valueList); \ |
| 693 | break; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 694 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 695 | DELEGATE(Cached); |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 696 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 697 | DELEGATE(Compiled); |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 698 | #undef DELEGATE |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 699 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 700 | default: { |
| 701 | mErrorCode = BCC_INVALID_OPERATION; |
| 702 | } |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 703 | } |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | |
Logan | f340bf7 | 2011-01-14 17:51:40 +0800 | [diff] [blame] | 707 | void Script::getFuncInfoList(size_t funcInfoListSize, |
| 708 | FuncInfo *funcInfoList) { |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 709 | switch (mStatus) { |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 710 | #define DELEGATE(STATUS) \ |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 711 | case ScriptStatus::STATUS: \ |
| 712 | m##STATUS->getFuncInfoList(funcInfoListSize, funcInfoList); \ |
| 713 | break; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 714 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 715 | DELEGATE(Cached); |
Logan | 3584900 | 2011-01-15 07:30:43 +0800 | [diff] [blame] | 716 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 717 | DELEGATE(Compiled); |
Logan | be79ada | 2011-01-13 01:33:45 +0800 | [diff] [blame] | 718 | #undef DELEGATE |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 719 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 720 | default: { |
| 721 | mErrorCode = BCC_INVALID_OPERATION; |
| 722 | } |
Logan | 89eb47f | 2011-01-07 10:45:16 +0800 | [diff] [blame] | 723 | } |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 724 | } |
| 725 | |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 726 | |
| 727 | void Script::getObjectSlotList(size_t objectSlotListSize, |
| 728 | uint32_t *objectSlotList) { |
| 729 | switch (mStatus) { |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 730 | #define DELEGATE(STATUS) \ |
| 731 | case ScriptStatus::STATUS: \ |
| 732 | m##STATUS->getObjectSlotList(objectSlotListSize, objectSlotList); \ |
| 733 | break; |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 734 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 735 | DELEGATE(Cached); |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 736 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 737 | DELEGATE(Compiled); |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 738 | #undef DELEGATE |
| 739 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 740 | default: { |
| 741 | mErrorCode = BCC_INVALID_OPERATION; |
| 742 | } |
Stephen Hines | 071288a | 2011-01-27 14:38:26 -0800 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | |
Shih-wei Liao | ce82d49 | 2011-01-20 12:34:03 -0800 | [diff] [blame] | 747 | int Script::registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext) { |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 748 | mpExtSymbolLookupFn = pFn; |
| 749 | mpExtSymbolLookupFnContext = pContext; |
| 750 | |
Logan | 7d2219f | 2011-01-06 06:19:25 +0800 | [diff] [blame] | 751 | if (mStatus != ScriptStatus::Unknown) { |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 752 | mErrorCode = BCC_INVALID_OPERATION; |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame] | 753 | ALOGE("Invalid operation: %s\n", __func__); |
Shih-wei Liao | ce82d49 | 2011-01-20 12:34:03 -0800 | [diff] [blame] | 754 | return 1; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 755 | } |
Shih-wei Liao | ce82d49 | 2011-01-20 12:34:03 -0800 | [diff] [blame] | 756 | return 0; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 757 | } |
Shih-wei Liao | b65410d | 2011-06-19 11:11:48 -0700 | [diff] [blame] | 758 | |
Shih-wei Liao | abc7f51 | 2012-01-18 00:34:07 -0800 | [diff] [blame] | 759 | bool Script::isCacheable() const { |
Shih-wei Liao | abc7f51 | 2012-01-18 00:34:07 -0800 | [diff] [blame] | 760 | if (getBooleanProp("debug.bcc.nocache")) { |
| 761 | // Android system environment property: Disables the cache mechanism by |
| 762 | // setting "debug.bcc.nocache". So we will not load the cache file any |
| 763 | // way. |
| 764 | return false; |
| 765 | } |
| 766 | |
| 767 | if (mCacheDir.empty() || mCacheName.empty()) { |
| 768 | // The application developer has not specified the cachePath, so |
| 769 | // we don't know where to open the cache file. |
| 770 | return false; |
| 771 | } |
| 772 | |
| 773 | return true; |
Shih-wei Liao | abc7f51 | 2012-01-18 00:34:07 -0800 | [diff] [blame] | 774 | } |
| 775 | |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 776 | size_t Script::getELFSize() const { |
| 777 | switch (mStatus) { |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 778 | case ScriptStatus::Compiled: { |
| 779 | return mCompiled->getELFSize(); |
| 780 | } |
Stephen Hines | 0e56786 | 2012-03-11 20:26:40 -0700 | [diff] [blame] | 781 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 782 | case ScriptStatus::Cached: { |
| 783 | return mCached->getELFSize(); |
| 784 | } |
Stephen Hines | 0e56786 | 2012-03-11 20:26:40 -0700 | [diff] [blame] | 785 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 786 | default: { |
| 787 | return 0; |
| 788 | } |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 789 | } |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | const char *Script::getELF() const { |
| 793 | switch (mStatus) { |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 794 | case ScriptStatus::Compiled: { |
| 795 | return mCompiled->getELF(); |
| 796 | } |
Stephen Hines | 0e56786 | 2012-03-11 20:26:40 -0700 | [diff] [blame] | 797 | |
Daniel Malea | 094881f | 2011-12-14 17:39:16 -0500 | [diff] [blame] | 798 | case ScriptStatus::Cached: { |
| 799 | return mCached->getELF(); |
| 800 | } |
Stephen Hines | 0e56786 | 2012-03-11 20:26:40 -0700 | [diff] [blame] | 801 | |
Shih-wei Liao | d50be32 | 2011-07-01 22:53:31 -0700 | [diff] [blame] | 802 | default: { |
| 803 | return NULL; |
| 804 | } |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 805 | } |
| 806 | } |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 807 | |
| 808 | } // namespace bcc |