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