Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 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 | |
Logan | 02286cb | 2011-01-07 00:30:47 +0800 | [diff] [blame] | 22 | #include "ContextManager.h" |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 23 | #include "EmittedFuncInfo.h" |
| 24 | |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 25 | namespace bcc { |
| 26 | |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 27 | ScriptCompiled::~ScriptCompiled() { |
Logan | 02286cb | 2011-01-07 00:30:47 +0800 | [diff] [blame] | 28 | // Deallocate the BCC context |
| 29 | if (mContext) { |
| 30 | deallocateContext(mContext); |
| 31 | } |
| 32 | |
| 33 | // Delete the emitted function information |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 34 | for (EmittedFunctionsMapTy::iterator I = mEmittedFunctions.begin(), |
| 35 | E = mEmittedFunctions.end(); I != E; I++) { |
| 36 | if (I->second != NULL) { |
| 37 | delete I->second; |
| 38 | } |
| 39 | } |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 40 | } |
| 41 | |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 42 | void ScriptCompiled::getExportVars(BCCsizei *actualVarCount, |
| 43 | BCCsizei maxVarCount, |
| 44 | BCCvoid **vars) { |
Logan | 02286cb | 2011-01-07 00:30:47 +0800 | [diff] [blame] | 45 | int varCount = mExportVars.size(); |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 46 | if (actualVarCount) |
| 47 | *actualVarCount = varCount; |
| 48 | if (varCount > maxVarCount) |
| 49 | varCount = maxVarCount; |
| 50 | if (vars) { |
| 51 | for (ExportVarList::const_iterator |
| 52 | I = mExportVars.begin(), E = mExportVars.end(); I != E; I++) { |
| 53 | *vars++ = *I; |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | void ScriptCompiled::getExportFuncs(BCCsizei *actualFuncCount, |
| 60 | BCCsizei maxFuncCount, |
| 61 | BCCvoid **funcs) { |
Logan | 02286cb | 2011-01-07 00:30:47 +0800 | [diff] [blame] | 62 | int funcCount = mExportFuncs.size(); |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 63 | if (actualFuncCount) |
| 64 | *actualFuncCount = funcCount; |
| 65 | if (funcCount > maxFuncCount) |
| 66 | funcCount = maxFuncCount; |
| 67 | if (funcs) { |
| 68 | for (ExportFuncList::const_iterator |
| 69 | I = mExportFuncs.begin(), E = mExportFuncs.end(); I != E; I++) { |
| 70 | *funcs++ = *I; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | void ScriptCompiled::getPragmas(BCCsizei *actualStringCount, |
| 77 | BCCsizei maxStringCount, |
| 78 | BCCchar **strings) { |
Logan | 02286cb | 2011-01-07 00:30:47 +0800 | [diff] [blame] | 79 | int stringCount = mPragmas.size() * 2; |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 80 | |
| 81 | if (actualStringCount) |
| 82 | *actualStringCount = stringCount; |
| 83 | if (stringCount > maxStringCount) |
| 84 | stringCount = maxStringCount; |
| 85 | if (strings) { |
| 86 | size_t i = 0; |
| 87 | for (PragmaList::const_iterator it = mPragmas.begin(); |
| 88 | stringCount >= 2; stringCount -= 2, it++, ++i) { |
| 89 | *strings++ = const_cast<BCCchar*>(it->first.c_str()); |
| 90 | *strings++ = const_cast<BCCchar*>(it->second.c_str()); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 95 | |
| 96 | void *ScriptCompiled::lookup(const char *name) { |
Logan | 7dcaac9 | 2011-01-06 04:26:23 +0800 | [diff] [blame] | 97 | EmittedFunctionsMapTy::const_iterator I = mEmittedFunctions.find(name); |
| 98 | return (I == mEmittedFunctions.end()) ? NULL : I->second->Code; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | void ScriptCompiled::getFunctions(BCCsizei *actualFunctionCount, |
| 103 | BCCsizei maxFunctionCount, |
| 104 | BCCchar **functions) { |
| 105 | |
| 106 | int functionCount = mEmittedFunctions.size(); |
| 107 | |
| 108 | if (actualFunctionCount) |
| 109 | *actualFunctionCount = functionCount; |
| 110 | if (functionCount > maxFunctionCount) |
| 111 | functionCount = maxFunctionCount; |
| 112 | if (functions) { |
| 113 | for (EmittedFunctionsMapTy::const_iterator |
| 114 | I = mEmittedFunctions.begin(), E = mEmittedFunctions.end(); |
| 115 | I != E && (functionCount > 0); I++, functionCount--) { |
| 116 | *functions++ = const_cast<BCCchar*>(I->first.c_str()); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | |
| 122 | void ScriptCompiled::getFunctionBinary(BCCchar *funcname, |
| 123 | BCCvoid **base, |
| 124 | BCCsizei *length) { |
| 125 | EmittedFunctionsMapTy::const_iterator I = mEmittedFunctions.find(funcname); |
| 126 | if (I == mEmittedFunctions.end()) { |
| 127 | *base = NULL; |
| 128 | *length = 0; |
| 129 | } else { |
| 130 | *base = I->second->Code; |
| 131 | *length = I->second->Size; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
Logan | 2a6dc82 | 2011-01-06 04:05:20 +0800 | [diff] [blame] | 136 | } // namespace bcc |