blob: 019ecb1c24de80e3244d33b049fe71e88ef11069 [file] [log] [blame]
Logan474cbd22011-01-31 01:47:44 +08001/*
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
Logan474cbd22011-01-31 01:47:44 +080017#include "SourceInfo.h"
18
19#if USE_CACHE
Logan Chiend2a5f302011-07-19 20:32:25 +080020#if USE_OLD_JIT
21#include "OldJIT/CacheReader.h"
22#include "OldJIT/CacheWriter.h"
23#endif
24#if USE_MCJIT
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -070025#include "MCCacheWriter.h"
26#include "MCCacheReader.h"
Logan474cbd22011-01-31 01:47:44 +080027#endif
Logan Chiend2a5f302011-07-19 20:32:25 +080028#endif
Logan474cbd22011-01-31 01:47:44 +080029
Logan4dcd6792011-02-28 05:12:00 +080030#include "DebugHelper.h"
Logan474cbd22011-01-31 01:47:44 +080031#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 Chienc4ea07f2011-03-09 17:27:50 +080040#include <llvm/Support/system_error.h>
Logan474cbd22011-01-31 01:47:44 +080041
42#include <stddef.h>
43#include <string.h>
44
45namespace bcc {
46
47
48SourceInfo *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 Block10c51452012-01-05 23:23:07 +000068 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");
Logan474cbd22011-01-31 01:47:44 +080071 }
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
84SourceInfo *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
108SourceInfo *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 Block10c51452012-01-05 23:23:07 +0000124 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");
Logan474cbd22011-01-31 01:47:44 +0800127 }
128
129 memset(result->sha1, '\0', 20);
130#endif
131
132 return result;
133}
134
135
136int 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 Block10c14122012-01-08 10:15:06 +0000145 ALOGE("Unable to MemoryBuffer::getMemBuffer(addr=%p, size=%lu)\n",
Logan474cbd22011-01-31 01:47:44 +0800146 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 Chienc4ea07f2011-03-09 17:27:50 +0800156 llvm::OwningPtr<llvm::MemoryBuffer> MEM;
Logan474cbd22011-01-31 01:47:44 +0800157
Logan Chienc4ea07f2011-03-09 17:27:50 +0800158 if (llvm::error_code ec = llvm::MemoryBuffer::getFile(file.path, MEM)) {
Steve Block10c14122012-01-08 10:15:06 +0000159 ALOGE("Unable to MemoryBuffer::getFile(path=%s)\n", file.path);
Logan474cbd22011-01-31 01:47:44 +0800160 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
176template <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 Chiend2a5f302011-07-19 20:32:25 +0800195#if USE_OLD_JIT
Logan474cbd22011-01-31 01:47:44 +0800196template void SourceInfo::introDependency<CacheReader>(CacheReader &);
197template void SourceInfo::introDependency<CacheWriter>(CacheWriter &);
Logan Chiend2a5f302011-07-19 20:32:25 +0800198#endif
199
200#if USE_MCJIT
Shih-wei Liao5e3e0ce2011-06-17 13:59:46 -0700201template void SourceInfo::introDependency<MCCacheWriter>(MCCacheWriter &);
202template void SourceInfo::introDependency<MCCacheReader>(MCCacheReader &);
Logan Chiend2a5f302011-07-19 20:32:25 +0800203#endif
Logan474cbd22011-01-31 01:47:44 +0800204#endif // USE_CACHE
205
206
207} // namespace bcc