blob: a53377a7a074410d5fff340402958d89415da649 [file] [log] [blame]
Logancf3e5212010-12-29 01:44:55 +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
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
26namespace bcc {
27
28Script::~Script() {
29 if (mStatus == ScriptStatus::Compiled) {
30 delete mCompiled;
31 }
32}
33
34
35int Script::readBC(const char *bitcode,
36 size_t bitcodeSize,
37 long bitcodeFileModTime,
38 long bitcodeFileCRC32,
39 const BCCchar *resName,
40 const BCCchar *cacheDir) {
Logan3a098f92011-01-01 03:46:30 +080041 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) {
Logancf3e5212010-12-29 01:44:55 +080053 mErrorCode = BCC_INVALID_OPERATION;
54 return 1;
55 }
56
Logancf3e5212010-12-29 01:44:55 +080057 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
68int 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
92int 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
102int Script::loadCacheFile() {
103 if (mStatus != ScriptStatus::Compiled) {
104 mErrorCode = BCC_INVALID_OPERATION;
105 return 1;
106 }
107
108 return mCompiled->loadCacheFile();
109}
110
111
112int Script::compile() {
113 if (mStatus != ScriptStatus::Compiled) {
114 mErrorCode = BCC_INVALID_OPERATION;
115 return 1;
116 }
117
118 return mCompiled->compile();
119}
120
121
122char 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
132void *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
142void 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
154void 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
166void 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
178void 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
190void 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
202void 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