ScriptCompiled.cpp revision 7dcaac9898741e701098537808296f4e2f2fb422
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#define LOG_TAG "bcc"
18#include <cutils/log.h>
19
20#include "ScriptCompiled.h"
21
22#include "EmittedFuncInfo.h"
23
24namespace bcc {
25
26ScriptCompiled::~ScriptCompiled() {
27  for (EmittedFunctionsMapTy::iterator I = mEmittedFunctions.begin(),
28       E = mEmittedFunctions.end(); I != E; I++) {
29    if (I->second != NULL) {
30      delete I->second;
31    }
32  }
33
34  mEmittedFunctions.clear();
35}
36
37void ScriptCompiled::getExportVars(BCCsizei *actualVarCount,
38                                   BCCsizei maxVarCount,
39                                   BCCvoid **vars) {
40  int varCount;
41
42#if 0
43  if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
44    varCount = static_cast<int>(mCacheHdr->exportVarsCount);
45    if (actualVarCount)
46      *actualVarCount = varCount;
47    if (varCount > maxVarCount)
48      varCount = maxVarCount;
49    if (vars) {
50      uint32_t *cachedVars = (uint32_t *)(mCacheMapAddr +
51                                          mCacheHdr->exportVarsOffset);
52
53      for (int i = 0; i < varCount; i++) {
54        *vars = (BCCvoid *)((char *)(*cachedVars) + mCacheDiff);
55        vars++;
56        cachedVars++;
57      }
58    }
59    return;
60  }
61#endif
62
63  varCount = mExportVars.size();
64  if (actualVarCount)
65    *actualVarCount = varCount;
66  if (varCount > maxVarCount)
67    varCount = maxVarCount;
68  if (vars) {
69    for (ExportVarList::const_iterator
70         I = mExportVars.begin(), E = mExportVars.end(); I != E; I++) {
71      *vars++ = *I;
72    }
73  }
74}
75
76
77void ScriptCompiled::getExportFuncs(BCCsizei *actualFuncCount,
78                                    BCCsizei maxFuncCount,
79                                    BCCvoid **funcs) {
80  int funcCount;
81
82#if 0
83  if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
84    funcCount = static_cast<int>(mCacheHdr->exportFuncsCount);
85    if (actualFuncCount)
86      *actualFuncCount = funcCount;
87    if (funcCount > maxFuncCount)
88      funcCount = maxFuncCount;
89    if (funcs) {
90      uint32_t *cachedFuncs = (uint32_t *)(mCacheMapAddr +
91                                           mCacheHdr->exportFuncsOffset);
92
93      for (int i = 0; i < funcCount; i++) {
94        *funcs = (BCCvoid *)((char *)(*cachedFuncs) + mCacheDiff);
95        funcs++;
96        cachedFuncs++;
97      }
98    }
99    return;
100  }
101#endif
102
103  funcCount = mExportFuncs.size();
104  if (actualFuncCount)
105    *actualFuncCount = funcCount;
106  if (funcCount > maxFuncCount)
107    funcCount = maxFuncCount;
108  if (funcs) {
109    for (ExportFuncList::const_iterator
110         I = mExportFuncs.begin(), E = mExportFuncs.end(); I != E; I++) {
111      *funcs++ = *I;
112    }
113  }
114}
115
116
117void ScriptCompiled::getPragmas(BCCsizei *actualStringCount,
118                                BCCsizei maxStringCount,
119                                BCCchar **strings) {
120  int stringCount;
121
122#if 0
123  if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
124    stringCount = static_cast<int>(mCacheHdr->exportPragmasCount) * 2;
125
126    if (actualStringCount)
127      *actualStringCount = stringCount;
128
129    if (stringCount > maxStringCount)
130      stringCount = maxStringCount;
131
132    if (strings) {
133      char *pragmaTab = mCacheMapAddr + mCacheHdr->exportPragmasOffset;
134
135      oBCCPragmaEntry *cachedPragmaEntries = (oBCCPragmaEntry *)pragmaTab;
136
137      for (int i = 0; stringCount >= 2; stringCount -= 2, i++) {
138        *strings++ = pragmaTab + cachedPragmaEntries[i].pragmaNameOffset;
139        *strings++ = pragmaTab + cachedPragmaEntries[i].pragmaValueOffset;
140      }
141    }
142
143    return;
144  }
145#endif
146
147  stringCount = mPragmas.size() * 2;
148
149  if (actualStringCount)
150    *actualStringCount = stringCount;
151  if (stringCount > maxStringCount)
152    stringCount = maxStringCount;
153  if (strings) {
154    size_t i = 0;
155    for (PragmaList::const_iterator it = mPragmas.begin();
156         stringCount >= 2; stringCount -= 2, it++, ++i) {
157      *strings++ = const_cast<BCCchar*>(it->first.c_str());
158      *strings++ = const_cast<BCCchar*>(it->second.c_str());
159    }
160  }
161}
162
163
164void *ScriptCompiled::lookup(const char *name) {
165#if 0
166  if (mUseCache && mCacheFd >= 0 && !mCacheNew) {
167    if (!strcmp(name, "root")) {
168      addr = reinterpret_cast<void *>(mCacheHdr->rootAddr);
169    } else if (!strcmp(name, "init")) {
170      addr = reinterpret_cast<void *>(mCacheHdr->initAddr);
171    }
172    return addr;
173  }
174#endif
175
176  EmittedFunctionsMapTy::const_iterator I = mEmittedFunctions.find(name);
177  return (I == mEmittedFunctions.end()) ? NULL : I->second->Code;
178}
179
180
181void ScriptCompiled::getFunctions(BCCsizei *actualFunctionCount,
182                                  BCCsizei maxFunctionCount,
183                                  BCCchar **functions) {
184
185  int functionCount = mEmittedFunctions.size();
186
187  if (actualFunctionCount)
188    *actualFunctionCount = functionCount;
189  if (functionCount > maxFunctionCount)
190    functionCount = maxFunctionCount;
191  if (functions) {
192    for (EmittedFunctionsMapTy::const_iterator
193         I = mEmittedFunctions.begin(), E = mEmittedFunctions.end();
194         I != E && (functionCount > 0); I++, functionCount--) {
195      *functions++ = const_cast<BCCchar*>(I->first.c_str());
196    }
197  }
198}
199
200
201void ScriptCompiled::getFunctionBinary(BCCchar *funcname,
202                                       BCCvoid **base,
203                                       BCCsizei *length) {
204  EmittedFunctionsMapTy::const_iterator I = mEmittedFunctions.find(funcname);
205  if (I == mEmittedFunctions.end()) {
206    *base = NULL;
207    *length = 0;
208  } else {
209    *base = I->second->Code;
210    *length = I->second->Size;
211  }
212}
213
214
215} // namespace bcc
216