blob: 306cf239f5fec07d467c5ac9590149682a427015 [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,
Loganf7f0ac52011-01-07 03:53:43 +080036 Cached,
Logancf3e5212010-12-29 01:44:55 +080037 };
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;
Logan75cc8a52011-01-07 06:06:52 +080056 unsigned char sourceSHA1[20];
Loganf30a6712011-01-06 05:55:34 +080057 size_t sourceSize;
Loganecf4cbd2011-01-06 05:34:11 +080058
59 // ReadModule
60 llvm::Module *sourceModule;
61
Logan3133c412011-01-06 06:15:40 +080062 // LinkBC
63 char const *libraryBC;
64 size_t librarySize;
65
Loganecf4cbd2011-01-06 05:34:11 +080066 // Register Symbol Lookup Function
Logancf3e5212010-12-29 01:44:55 +080067 BCCSymbolLookupFn mpExtSymbolLookupFn;
68 BCCvoid *mpExtSymbolLookupFnContext;
Loganeaa0cc32010-12-29 01:04:20 +080069
Logan3f3d31f2010-11-27 13:52:03 +080070 public:
Logan033f46e2011-01-06 05:51:24 +080071 Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown),
Logan65719812011-01-07 11:17:14 +080072 cacheFile(NULL),
73 sourceBC(NULL), sourceResName(NULL), sourceSize(0),
74 sourceModule(NULL), libraryBC(NULL), librarySize(0),
75 mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) {
Loganecf4cbd2011-01-06 05:34:11 +080076 Compiler::GlobalInitialization();
Logan3f3d31f2010-11-27 13:52:03 +080077 }
78
Logancf3e5212010-12-29 01:44:55 +080079 ~Script();
Loganeaa0cc32010-12-29 01:04:20 +080080
81 int readBC(const char *bitcode,
82 size_t bitcodeSize,
Loganeaa0cc32010-12-29 01:04:20 +080083 const BCCchar *resName,
Logancf3e5212010-12-29 01:44:55 +080084 const BCCchar *cacheDir);
Loganeaa0cc32010-12-29 01:04:20 +080085
Loganecf4cbd2011-01-06 05:34:11 +080086 int readModule(llvm::Module *module);
87
Logancf3e5212010-12-29 01:44:55 +080088 int linkBC(const char *bitcode, size_t bitcodeSize);
Loganeaa0cc32010-12-29 01:04:20 +080089
Loganecf4cbd2011-01-06 05:34:11 +080090 int compile(); // deprecated
Loganeaa0cc32010-12-29 01:04:20 +080091
Logancf3e5212010-12-29 01:44:55 +080092 char const *getCompilerErrorMessage();
Loganeaa0cc32010-12-29 01:04:20 +080093
Logancf3e5212010-12-29 01:44:55 +080094 void *lookup(const char *name);
Loganeaa0cc32010-12-29 01:04:20 +080095
96 void getExportVars(BCCsizei *actualVarCount,
97 BCCsizei maxVarCount,
Logancf3e5212010-12-29 01:44:55 +080098 BCCvoid **vars);
Loganeaa0cc32010-12-29 01:04:20 +080099
100 void getExportFuncs(BCCsizei *actualFuncCount,
101 BCCsizei maxFuncCount,
Logancf3e5212010-12-29 01:44:55 +0800102 BCCvoid **funcs);
Loganeaa0cc32010-12-29 01:04:20 +0800103
104 void getPragmas(BCCsizei *actualStringCount,
105 BCCsizei maxStringCount,
Logancf3e5212010-12-29 01:44:55 +0800106 BCCchar **strings);
Loganeaa0cc32010-12-29 01:04:20 +0800107
108 void getFunctions(BCCsizei *actualFunctionCount,
109 BCCsizei maxFunctionCount,
Logancf3e5212010-12-29 01:44:55 +0800110 BCCchar **functions);
Loganeaa0cc32010-12-29 01:04:20 +0800111
112 void getFunctionBinary(BCCchar *function,
113 BCCvoid **base,
Logancf3e5212010-12-29 01:44:55 +0800114 BCCsizei *length);
Loganeaa0cc32010-12-29 01:04:20 +0800115
Logana27a83f2011-01-07 10:25:48 +0800116 char *getContext();
Logan02286cb2011-01-07 00:30:47 +0800117
Logancf3e5212010-12-29 01:44:55 +0800118 void registerSymbolCallback(BCCSymbolLookupFn pFn, BCCvoid *pContext);
Loganeaa0cc32010-12-29 01:04:20 +0800119
Loganeaa0cc32010-12-29 01:04:20 +0800120
Logan3f3d31f2010-11-27 13:52:03 +0800121 void setError(BCCenum error) {
Logan39736412010-12-29 00:24:04 +0800122 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
123 mErrorCode = error;
Logan3f3d31f2010-11-27 13:52:03 +0800124 }
125 }
126
127 BCCenum getError() {
Logan39736412010-12-29 00:24:04 +0800128 BCCenum result = mErrorCode;
129 mErrorCode = BCC_NO_ERROR;
Logan3f3d31f2010-11-27 13:52:03 +0800130 return result;
131 }
Logan033f46e2011-01-06 05:51:24 +0800132
133 private:
134 int internalLoadCache();
135 int internalCompile();
136
Logan3f3d31f2010-11-27 13:52:03 +0800137 };
138
139} // namespace bcc
140
141#endif // BCC_SCRIPT_H