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
17 // Bitcode compiler (bcc) for Android:
18 // This is an eager-compilation JIT running on Android.
19
20 #include <bcc/bcc.h>
21 #include "bcc_internal.h"
22
23 #include "Config.h"
24
25 #include "Compiler.h"
26 #include "DebugHelper.h"
27 #include "Script.h"
28
29 #include <string>
30
31 #include <utils/StopWatch.h>
32
33 using namespace bcc;
34
35 namespace llvm {
36 class Module;
37 }
38
39 static bool bccBuildStampPrinted = false;
40
bccPrintBuildStamp()41 static void bccPrintBuildStamp() {
42 if (!bccBuildStampPrinted) {
43 LOGI("LIBBCC build time: %s", bccGetBuildTime());
44 LOGI("LIBBCC build revision: %s", bccGetBuildRev());
45 bccBuildStampPrinted = true;
46 }
47 }
48
bccCreateScript()49 extern "C" BCCScriptRef bccCreateScript() {
50 BCC_FUNC_LOGGER();
51 bccPrintBuildStamp();
52 return wrap(new bcc::Script());
53 }
54
55
bccDisposeScript(BCCScriptRef script)56 extern "C" void bccDisposeScript(BCCScriptRef script) {
57 BCC_FUNC_LOGGER();
58 delete unwrap(script);
59 }
60
61
bccRegisterSymbolCallback(BCCScriptRef script,BCCSymbolLookupFn pFn,void * pContext)62 extern "C" int bccRegisterSymbolCallback(BCCScriptRef script,
63 BCCSymbolLookupFn pFn,
64 void *pContext) {
65 BCC_FUNC_LOGGER();
66 return unwrap(script)->registerSymbolCallback(pFn, pContext);
67 }
68
69
bccGetError(BCCScriptRef script)70 extern "C" int bccGetError(BCCScriptRef script) {
71 BCC_FUNC_LOGGER();
72 return unwrap(script)->getError();
73 }
74
bccReadBC(BCCScriptRef script,char const * resName,char const * bitcode,size_t bitcodeSize,unsigned long flags)75 extern "C" int bccReadBC(BCCScriptRef script,
76 char const *resName,
77 char const *bitcode,
78 size_t bitcodeSize,
79 unsigned long flags) {
80 BCC_FUNC_LOGGER();
81 return unwrap(script)->addSourceBC(0, resName, bitcode, bitcodeSize, flags);
82 }
83
84
bccReadModule(BCCScriptRef script,char const * resName,LLVMModuleRef module,unsigned long flags)85 extern "C" int bccReadModule(BCCScriptRef script,
86 char const *resName /* deprecated */,
87 LLVMModuleRef module,
88 unsigned long flags) {
89 BCC_FUNC_LOGGER();
90 return unwrap(script)->addSourceModule(0, unwrap(module), flags);
91 }
92
93
bccReadFile(BCCScriptRef script,char const * path,unsigned long flags)94 extern "C" int bccReadFile(BCCScriptRef script,
95 char const *path,
96 unsigned long flags) {
97 BCC_FUNC_LOGGER();
98 return unwrap(script)->addSourceFile(0, path, flags);
99 }
100
101
bccLinkBC(BCCScriptRef script,char const * resName,char const * bitcode,size_t bitcodeSize,unsigned long flags)102 extern "C" int bccLinkBC(BCCScriptRef script,
103 char const *resName,
104 char const *bitcode,
105 size_t bitcodeSize,
106 unsigned long flags) {
107 BCC_FUNC_LOGGER();
108 return unwrap(script)->addSourceBC(1, resName, bitcode, bitcodeSize, flags);
109 }
110
111
bccLinkFile(BCCScriptRef script,char const * path,unsigned long flags)112 extern "C" int bccLinkFile(BCCScriptRef script,
113 char const *path,
114 unsigned long flags) {
115 BCC_FUNC_LOGGER();
116 return unwrap(script)->addSourceFile(1, path, flags);
117 }
118
119
bccMarkExternalSymbol(BCCScriptRef script,char const * name)120 extern "C" void bccMarkExternalSymbol(BCCScriptRef script, char const *name) {
121 BCC_FUNC_LOGGER();
122 unwrap(script)->markExternalSymbol(name);
123 }
124
125
bccPrepareSharedObject(BCCScriptRef script,char const * cacheDir,char const * cacheName,unsigned long flags)126 extern "C" int bccPrepareSharedObject(BCCScriptRef script,
127 char const *cacheDir,
128 char const *cacheName,
129 unsigned long flags) {
130 return unwrap(script)->prepareSharedObject(cacheDir, cacheName, flags);
131 }
132
133
bccPrepareExecutable(BCCScriptRef script,char const * cacheDir,char const * cacheName,unsigned long flags)134 extern "C" int bccPrepareExecutable(BCCScriptRef script,
135 char const *cacheDir,
136 char const *cacheName,
137 unsigned long flags) {
138 BCC_FUNC_LOGGER();
139
140 #if defined(__arm__)
141 android::StopWatch compileTimer("bcc: PrepareExecutable time");
142 #endif
143
144 return unwrap(script)->prepareExecutable(cacheDir, cacheName, flags);
145 }
146
147
bccGetFuncAddr(BCCScriptRef script,char const * funcname)148 extern "C" void *bccGetFuncAddr(BCCScriptRef script, char const *funcname) {
149 BCC_FUNC_LOGGER();
150
151 void *addr = unwrap(script)->lookup(funcname);
152
153 #if DEBUG_BCC_REFLECT
154 LOGD("Function Address: %s --> %p\n", funcname, addr);
155 #endif
156
157 return addr;
158 }
159
160
bccGetExportVarList(BCCScriptRef script,size_t varListSize,void ** varList)161 extern "C" void bccGetExportVarList(BCCScriptRef script,
162 size_t varListSize,
163 void **varList) {
164 BCC_FUNC_LOGGER();
165
166 if (varList) {
167 unwrap(script)->getExportVarList(varListSize, varList);
168
169 #if DEBUG_BCC_REFLECT
170 size_t count = unwrap(script)->getExportVarCount();
171 LOGD("ExportVarCount = %lu\n", (unsigned long)count);
172
173 if (count > varListSize) {
174 count = varListSize;
175 }
176
177 for (size_t i = 0; i < count; ++i) {
178 LOGD("ExportVarList[%lu] = %p\n", (unsigned long)i, varList[i]);
179 }
180 #endif
181 }
182 }
183
184
bccGetExportFuncList(BCCScriptRef script,size_t funcListSize,void ** funcList)185 extern "C" void bccGetExportFuncList(BCCScriptRef script,
186 size_t funcListSize,
187 void **funcList) {
188 BCC_FUNC_LOGGER();
189
190 if (funcList) {
191 unwrap(script)->getExportFuncList(funcListSize, funcList);
192
193 #if DEBUG_BCC_REFLECT
194 size_t count = unwrap(script)->getExportFuncCount();
195 LOGD("ExportFuncCount = %lu\n", (unsigned long)count);
196
197 if (count > funcListSize) {
198 count = funcListSize;
199 }
200
201 for (size_t i = 0; i < count; ++i) {
202 LOGD("ExportFuncList[%lu] = %p\n", (unsigned long)i, funcList[i]);
203 }
204 #endif
205 }
206 }
207
208