blob: 5e8d24f0f3475ced9289ea479716ab1326e11370 [file] [log] [blame]
Logan3f3d31f2010-11-27 13:52:03 +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#ifndef BCC_SCRIPT_H
18#define BCC_SCRIPT_H
19
20#include <bcc/bcc.h>
Logan3f3d31f2010-11-27 13:52:03 +080021
Loganecf4cbd2011-01-06 05:34:11 +080022#include "Compiler.h"
23
Loganeaa0cc32010-12-29 01:04:20 +080024namespace llvm {
25 class Module;
26}
27
Logan3f3d31f2010-11-27 13:52:03 +080028namespace bcc {
Logancf3e5212010-12-29 01:44:55 +080029 class ScriptCompiled;
30 class ScriptCached;
31
32 namespace ScriptStatus {
33 enum StatusType {
34 Unknown,
35 Compiled,
36 //Cached,
37 };
38 }
Logan3f3d31f2010-11-27 13:52:03 +080039
Logan0647e9e2010-12-29 00:21:31 +080040 class Script {
Logan39736412010-12-29 00:24:04 +080041 private:
42 BCCenum mErrorCode;
43
Logancf3e5212010-12-29 01:44:55 +080044 ScriptStatus::StatusType mStatus;
45
46 union {
47 ScriptCompiled *mCompiled;
48 ScriptCached *mCached;
49 };
50
Loganecf4cbd2011-01-06 05:34:11 +080051 char *cacheFile;
52
53 // ReadBC
54 char const *sourceBC;
55 char const *sourceResName;
Loganf30a6712011-01-06 05:55:34 +080056 size_t sourceSize;
Loganecf4cbd2011-01-06 05:34:11 +080057
58 // ReadModule
59 llvm::Module *sourceModule;
60
61 // Register Symbol Lookup Function
Logancf3e5212010-12-29 01:44:55 +080062 BCCSymbolLookupFn mpExtSymbolLookupFn;
63 BCCvoid *mpExtSymbolLookupFnContext;
Loganeaa0cc32010-12-29 01:04:20 +080064
Logan3f3d31f2010-11-27 13:52:03 +080065 public:
Logan033f46e2011-01-06 05:51:24 +080066 Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
67 cacheFile(NULL), sourceBC(NULL), sourceResName(NULL),
68 sourceSize(0), sourceModule(NULL), mpExtSymbolLookupFn(NULL),
69 mpExtSymbolLookupFnContext(NULL) {
Loganecf4cbd2011-01-06 05:34:11 +080070 Compiler::GlobalInitialization();
Logan3f3d31f2010-11-27 13:52:03 +080071 }
72
Logancf3e5212010-12-29 01:44:55 +080073 ~Script();
Loganeaa0cc32010-12-29 01:04:20 +080074
75 int readBC(const char *bitcode,
76 size_t bitcodeSize,
Loganeaa0cc32010-12-29 01:04:20 +080077 const BCCchar *resName,
Logancf3e5212010-12-29 01:44:55 +080078 const BCCchar *cacheDir);
Loganeaa0cc32010-12-29 01:04:20 +080079
Loganecf4cbd2011-01-06 05:34:11 +080080 int readModule(llvm::Module *module);
81
Logancf3e5212010-12-29 01:44:55 +080082 int linkBC(const char *bitcode, size_t bitcodeSize);
Loganeaa0cc32010-12-29 01:04:20 +080083
Loganecf4cbd2011-01-06 05:34:11 +080084 int compile(); // deprecated
Loganeaa0cc32010-12-29 01:04:20 +080085
Logancf3e5212010-12-29 01:44:55 +080086 char const *getCompilerErrorMessage();
Loganeaa0cc32010-12-29 01:04:20 +080087
Logancf3e5212010-12-29 01:44:55 +080088 void *lookup(const char *name);
Loganeaa0cc32010-12-29 01:04:20 +080089
90 void getExportVars(BCCsizei *actualVarCount,
91 BCCsizei maxVarCount,
Logancf3e5212010-12-29 01:44:55 +080092 BCCvoid **vars);
Loganeaa0cc32010-12-29 01:04:20 +080093
94 void getExportFuncs(BCCsizei *actualFuncCount,
95 BCCsizei maxFuncCount,
Logancf3e5212010-12-29 01:44:55 +080096 BCCvoid **funcs);
Loganeaa0cc32010-12-29 01:04:20 +080097
98 void getPragmas(BCCsizei *actualStringCount,
99 BCCsizei maxStringCount,
Logancf3e5212010-12-29 01:44:55 +0800100 BCCchar **strings);
Loganeaa0cc32010-12-29 01:04:20 +0800101
102 void getFunctions(BCCsizei *actualFunctionCount,
103 BCCsizei maxFunctionCount,
Logancf3e5212010-12-29 01:44:55 +0800104 BCCchar **functions);
Loganeaa0cc32010-12-29 01:04:20 +0800105
106 void getFunctionBinary(BCCchar *function,
107 BCCvoid **base,
Logancf3e5212010-12-29 01:44:55 +0800108 BCCsizei *length);
Loganeaa0cc32010-12-29 01:04:20 +0800109
Logancf3e5212010-12-29 01:44:55 +0800110 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext);
Loganeaa0cc32010-12-29 01:04:20 +0800111
Loganeaa0cc32010-12-29 01:04:20 +0800112
Logan3f3d31f2010-11-27 13:52:03 +0800113 void setError(BCCenum error) {
Logan39736412010-12-29 00:24:04 +0800114 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
115 mErrorCode = error;
Logan3f3d31f2010-11-27 13:52:03 +0800116 }
117 }
118
119 BCCenum getError() {
Logan39736412010-12-29 00:24:04 +0800120 BCCenum result = mErrorCode;
121 mErrorCode = BCC_NO_ERROR;
Logan3f3d31f2010-11-27 13:52:03 +0800122 return result;
123 }
Logan033f46e2011-01-06 05:51:24 +0800124
125 private:
126 int internalLoadCache();
127 int internalCompile();
128
Logan3f3d31f2010-11-27 13:52:03 +0800129 };
130
131} // namespace bcc
132
133#endif // BCC_SCRIPT_H