Logan | 474cbd2 | 2011-01-31 01:47:44 +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 | |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 17 | #include "SourceInfo.h" |
| 18 | |
| 19 | #if USE_CACHE |
Logan Chien | d2a5f30 | 2011-07-19 20:32:25 +0800 | [diff] [blame] | 20 | #if USE_OLD_JIT |
| 21 | #include "OldJIT/CacheReader.h" |
| 22 | #include "OldJIT/CacheWriter.h" |
| 23 | #endif |
| 24 | #if USE_MCJIT |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 25 | #include "MCCacheWriter.h" |
| 26 | #include "MCCacheReader.h" |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 27 | #endif |
Logan Chien | d2a5f30 | 2011-07-19 20:32:25 +0800 | [diff] [blame] | 28 | #endif |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 29 | |
Logan | 4dcd679 | 2011-02-28 05:12:00 +0800 | [diff] [blame] | 30 | #include "DebugHelper.h" |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 31 | #include "ScriptCompiled.h" |
| 32 | #include "Sha1Helper.h" |
| 33 | |
| 34 | #include <bcc/bcc.h> |
| 35 | #include <bcc/bcc_cache.h> |
| 36 | |
| 37 | #include <llvm/ADT/OwningPtr.h> |
| 38 | #include <llvm/ADT/StringRef.h> |
| 39 | #include <llvm/Support/MemoryBuffer.h> |
Logan Chien | c4ea07f | 2011-03-09 17:27:50 +0800 | [diff] [blame] | 40 | #include <llvm/Support/system_error.h> |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 41 | |
| 42 | #include <stddef.h> |
| 43 | #include <string.h> |
| 44 | |
| 45 | namespace bcc { |
| 46 | |
| 47 | |
| 48 | SourceInfo *SourceInfo::createFromBuffer(char const *resName, |
| 49 | char const *bitcode, |
| 50 | size_t bitcodeSize, |
| 51 | unsigned long flags) { |
| 52 | SourceInfo *result = new SourceInfo(); |
| 53 | |
| 54 | if (!result) { |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | result->type = SourceKind::Buffer; |
| 59 | result->buffer.resName = resName; |
| 60 | result->buffer.bitcode = bitcode; |
| 61 | result->buffer.bitcodeSize = bitcodeSize; |
| 62 | result->flags = flags; |
| 63 | |
| 64 | #if USE_CACHE |
| 65 | if (!resName && !(flags & BCC_SKIP_DEP_SHA1)) { |
| 66 | result->flags |= BCC_SKIP_DEP_SHA1; |
| 67 | |
Steve Block | 10c5145 | 2012-01-05 23:23:07 +0000 | [diff] [blame] | 68 | ALOGW("It is required to give resName for sha1 dependency check.\n"); |
| 69 | ALOGW("Sha1sum dependency check will be skipped.\n"); |
| 70 | ALOGW("Set BCC_SKIP_DEP_SHA1 for flags to surpress this warning.\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | if (result->flags & BCC_SKIP_DEP_SHA1) { |
| 74 | memset(result->sha1, '\0', 20); |
| 75 | } else { |
| 76 | calcSHA1(result->sha1, bitcode, bitcodeSize); |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | return result; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | SourceInfo *SourceInfo::createFromFile(char const *path, |
| 85 | unsigned long flags) { |
| 86 | SourceInfo *result = new SourceInfo(); |
| 87 | |
| 88 | if (!result) { |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | result->type = SourceKind::File; |
| 93 | result->file.path = path; |
| 94 | result->flags = flags; |
| 95 | |
| 96 | #if USE_CACHE |
| 97 | memset(result->sha1, '\0', 20); |
| 98 | |
| 99 | if (!(result->flags & BCC_SKIP_DEP_SHA1)) { |
| 100 | calcFileSHA1(result->sha1, path); |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | return result; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | SourceInfo *SourceInfo::createFromModule(llvm::Module *module, |
| 109 | unsigned long flags) { |
| 110 | SourceInfo *result = new SourceInfo(); |
| 111 | |
| 112 | if (!result) { |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | result->type = SourceKind::Module; |
| 117 | result->module.reset(module); |
| 118 | result->flags = flags; |
| 119 | |
| 120 | #if USE_CACHE |
| 121 | if (! (flags & BCC_SKIP_DEP_SHA1)) { |
| 122 | result->flags |= BCC_SKIP_DEP_SHA1; |
| 123 | |
Steve Block | 10c5145 | 2012-01-05 23:23:07 +0000 | [diff] [blame] | 124 | ALOGW("Unable to calculate sha1sum for llvm::Module.\n"); |
| 125 | ALOGW("Sha1sum dependency check will be skipped.\n"); |
| 126 | ALOGW("Set BCC_SKIP_DEP_SHA1 for flags to surpress this warning.\n"); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | memset(result->sha1, '\0', 20); |
| 130 | #endif |
| 131 | |
| 132 | return result; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | int SourceInfo::prepareModule(ScriptCompiled *SC) { |
| 137 | switch (type) { |
| 138 | case SourceKind::Buffer: |
| 139 | { |
| 140 | llvm::OwningPtr<llvm::MemoryBuffer> MEM( |
| 141 | llvm::MemoryBuffer::getMemBuffer( |
| 142 | llvm::StringRef(buffer.bitcode, buffer.bitcodeSize))); |
| 143 | |
| 144 | if (!MEM.get()) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame^] | 145 | ALOGE("Unable to MemoryBuffer::getMemBuffer(addr=%p, size=%lu)\n", |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 146 | buffer.bitcode, (unsigned long)buffer.bitcodeSize); |
| 147 | return 1; |
| 148 | } |
| 149 | |
| 150 | module.reset(SC->parseBitcodeFile(MEM.get())); |
| 151 | } |
| 152 | break; |
| 153 | |
| 154 | case SourceKind::File: |
| 155 | { |
Logan Chien | c4ea07f | 2011-03-09 17:27:50 +0800 | [diff] [blame] | 156 | llvm::OwningPtr<llvm::MemoryBuffer> MEM; |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 157 | |
Logan Chien | c4ea07f | 2011-03-09 17:27:50 +0800 | [diff] [blame] | 158 | if (llvm::error_code ec = llvm::MemoryBuffer::getFile(file.path, MEM)) { |
Steve Block | 10c1412 | 2012-01-08 10:15:06 +0000 | [diff] [blame^] | 159 | ALOGE("Unable to MemoryBuffer::getFile(path=%s)\n", file.path); |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 160 | return 1; |
| 161 | } |
| 162 | |
| 163 | module.reset(SC->parseBitcodeFile(MEM.get())); |
| 164 | } |
| 165 | break; |
| 166 | |
| 167 | default: |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | return (module.get()) ? 0 : 1; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | #if USE_CACHE |
| 176 | template <typename T> void SourceInfo::introDependency(T &checker) { |
| 177 | if (flags & BCC_SKIP_DEP_SHA1) { |
| 178 | return; |
| 179 | } |
| 180 | |
| 181 | switch (type) { |
| 182 | case SourceKind::Buffer: |
| 183 | checker.addDependency(BCC_APK_RESOURCE, buffer.resName, sha1); |
| 184 | break; |
| 185 | |
| 186 | case SourceKind::File: |
| 187 | checker.addDependency(BCC_FILE_RESOURCE, file.path, sha1); |
| 188 | break; |
| 189 | |
| 190 | default: |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | |
Logan Chien | d2a5f30 | 2011-07-19 20:32:25 +0800 | [diff] [blame] | 195 | #if USE_OLD_JIT |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 196 | template void SourceInfo::introDependency<CacheReader>(CacheReader &); |
| 197 | template void SourceInfo::introDependency<CacheWriter>(CacheWriter &); |
Logan Chien | d2a5f30 | 2011-07-19 20:32:25 +0800 | [diff] [blame] | 198 | #endif |
| 199 | |
| 200 | #if USE_MCJIT |
Shih-wei Liao | 5e3e0ce | 2011-06-17 13:59:46 -0700 | [diff] [blame] | 201 | template void SourceInfo::introDependency<MCCacheWriter>(MCCacheWriter &); |
| 202 | template void SourceInfo::introDependency<MCCacheReader>(MCCacheReader &); |
Logan Chien | d2a5f30 | 2011-07-19 20:32:25 +0800 | [diff] [blame] | 203 | #endif |
Logan | 474cbd2 | 2011-01-31 01:47:44 +0800 | [diff] [blame] | 204 | #endif // USE_CACHE |
| 205 | |
| 206 | |
| 207 | } // namespace bcc |