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
33using namespace bcc;
34
35namespace llvm {
36  class Module;
37}
38
39static bool bccBuildStampPrinted = false;
40
41static 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
49extern "C" BCCScriptRef bccCreateScript() {
50  BCC_FUNC_LOGGER();
51  bccPrintBuildStamp();
52  return wrap(new bcc::Script());
53}
54
55
56extern "C" void bccDisposeScript(BCCScriptRef script) {
57  BCC_FUNC_LOGGER();
58  delete unwrap(script);
59}
60
61
62extern "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
70extern "C" int bccGetError(BCCScriptRef script) {
71  BCC_FUNC_LOGGER();
72  return unwrap(script)->getError();
73}
74
75extern "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
85extern "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
94extern "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
102extern "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
112extern "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
120extern "C" void bccMarkExternalSymbol(BCCScriptRef script, char const *name) {
121  BCC_FUNC_LOGGER();
122  unwrap(script)->markExternalSymbol(name);
123}
124
125
126extern "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
134extern "C" int bccPrepareExecutable(BCCScriptRef script,
135                                    char const *cacheDir,
136                                    char const *cacheName,
137                                    unsigned long flags) {
138  BCC_FUNC_LOGGER();
139
140  android::StopWatch compileTimer("bcc: PrepareExecutable time");
141
142  return unwrap(script)->prepareExecutable(cacheDir, cacheName, flags);
143}
144
145
146extern "C" void *bccGetFuncAddr(BCCScriptRef script, char const *funcname) {
147  BCC_FUNC_LOGGER();
148
149  void *addr = unwrap(script)->lookup(funcname);
150
151#if DEBUG_BCC_REFLECT
152  LOGD("Function Address: %s --> %p\n", funcname, addr);
153#endif
154
155  return addr;
156}
157
158
159extern "C" void bccGetExportVarList(BCCScriptRef script,
160                                    size_t varListSize,
161                                    void **varList) {
162  BCC_FUNC_LOGGER();
163
164  if (varList) {
165    unwrap(script)->getExportVarList(varListSize, varList);
166
167#if DEBUG_BCC_REFLECT
168    size_t count = unwrap(script)->getExportVarCount();
169    LOGD("ExportVarCount = %lu\n", (unsigned long)count);
170
171    if (count > varListSize) {
172      count = varListSize;
173    }
174
175    for (size_t i = 0; i < count; ++i) {
176      LOGD("ExportVarList[%lu] = %p\n", (unsigned long)i, varList[i]);
177    }
178#endif
179  }
180}
181
182
183extern "C" void bccGetExportFuncList(BCCScriptRef script,
184                                     size_t funcListSize,
185                                     void **funcList) {
186  BCC_FUNC_LOGGER();
187
188  if (funcList) {
189    unwrap(script)->getExportFuncList(funcListSize, funcList);
190
191#if DEBUG_BCC_REFLECT
192    size_t count = unwrap(script)->getExportFuncCount();
193    LOGD("ExportFuncCount = %lu\n", (unsigned long)count);
194
195    if (count > funcListSize) {
196      count = funcListSize;
197    }
198
199    for (size_t i = 0; i < count; ++i) {
200      LOGD("ExportFuncList[%lu] = %p\n", (unsigned long)i, funcList[i]);
201    }
202#endif
203  }
204}
205
206