blob: 6e4cd9f624233e8ffa869bf0bdaa23dcd517b935 [file] [log] [blame]
Zonr Chang19218c02012-04-05 10:44:53 +08001/*
2 * Copyright 2012, 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_EXECUTION_ENGINE_RS_SCRIPT_H
18#define BCC_EXECUTION_ENGINE_RS_SCRIPT_H
19
20#include <vector>
21#include <string>
22
23#include <stdint.h>
24#include <stddef.h>
25
26#include <llvm/ADT/SmallVector.h>
27
28#include <bcc/bcc.h>
29#include <bcc/bcc_mccache.h>
30#include "bcc_internal.h"
31
32#include "Compiler.h"
33#include "Script.h"
34
35namespace llvm {
36 class Module;
37 class GDBJITRegistrar;
38}
39
40namespace bcc {
41 class ScriptCompiled;
42 class ScriptCached;
43 class Source;
44 struct CompilerOption;
45
46 namespace ScriptStatus {
47 enum StatusType {
48 Unknown,
49 Compiled,
50 Cached
51 };
52 }
53
54 namespace ScriptObject {
55 enum ObjectType {
56 Unknown,
57 Relocatable,
58 SharedObject,
59 Executable,
60 };
61 }
62
63 class RSScript : public Script {
64 public:
65 class SourceDependency {
66 private:
67 MCO_ResourceType mSourceType;
68 std::string mSourceName;
69 uint8_t mSHA1[20];
70
71 public:
72 SourceDependency(MCO_ResourceType pSourceType,
73 const std::string &pSourceName,
74 const uint8_t *pSHA1);
75
76 inline MCO_ResourceType getSourceType() const
77 { return mSourceType; }
78
79 inline const std::string &getSourceName() const
80 { return mSourceName; }
81
82 inline const uint8_t *getSHA1Checksum() const
83 { return mSHA1; }
84 };
85 typedef llvm::SmallVectorImpl<SourceDependency *> SourceDependencyListTy;
86
87 private:
88 int mErrorCode;
89
90 ScriptStatus::StatusType mStatus;
91 // The type of the object behind this script after compilation. For
92 // example, after returning from a successful call to prepareRelocatable(),
93 // the value of mObjectType will be ScriptObject::Relocatable.
94 ScriptObject::ObjectType mObjectType;
95
96 union {
97 ScriptCompiled *mCompiled;
98 ScriptCached *mCached;
99 };
100
101 std::string mCacheDir;
102 std::string mCacheName;
103
104 inline std::string getCachedObjectPath() const {
105 return std::string(mCacheDir + mCacheName + ".o");
106 }
107
108 inline std::string getCacheInfoPath() const {
109 return getCachedObjectPath().append(".info");
110 }
111
112 bool mIsContextSlotNotAvail;
113
114 llvm::SmallVector<SourceDependency *, 4> mSourceDependencies;
115
116 // External Function List
117 std::vector<char const *> mUserDefinedExternalSymbols;
118
119 // Register Symbol Lookup Function
120 BCCSymbolLookupFn mpExtSymbolLookupFn;
121 void *mpExtSymbolLookupFnContext;
122
123 // This will be invoked when the containing source has been reset.
124 virtual bool doReset();
125
126 // Reset the state of this script object
127 void resetState();
128
129 public:
130 RSScript(Source &pSource);
131
132 ~RSScript();
133
134 // Add dependency information for this script given the source named
135 // pSourceName. pSHA1 is the SHA-1 checksum of the given source. Return
136 // false on error.
137 bool addSourceDependency(MCO_ResourceType pSourceType,
138 const std::string &pSourceName,
139 const uint8_t *pSHA1);
140
141 const SourceDependencyListTy &getSourceDependencies() const
142 { return mSourceDependencies; }
143
144 void markExternalSymbol(char const *name) {
145 mUserDefinedExternalSymbols.push_back(name);
146 }
147
148 std::vector<char const *> const &getUserDefinedExternalSymbols() const {
149 return mUserDefinedExternalSymbols;
150 }
151
152 int prepareExecutable(char const *cacheDir,
153 char const *cacheName,
154 unsigned long flags);
155 int writeCache();
156
157 /*
158 * Link the given bitcodes in mSource to shared object (.so).
159 *
160 * Currently, it requires one to provide the relocatable object files with
161 * given bitcodes to output a shared object.
162 *
163 * The usage of this function is flexible. You can have a relocatable object
164 * compiled before and pass it in objPath to generate shared object. If the
165 * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if
166 * you haven't done that yet) and then link the output relocatable object
167 * file to .so in dsoPath.
168 *
169 * TODO: Currently, we only support to link a bitcode (i.e., mSource.)
170 *
171 */
172 int prepareSharedObject(char const *objPath,
173 char const *dsoPath,
174 unsigned long flags);
175
176 int prepareRelocatable(char const *objPath,
177 llvm::Reloc::Model RelocModel,
178 unsigned long flags);
179
180 char const *getCompilerErrorMessage();
181
182 void *lookup(const char *name);
183
184 size_t getExportVarCount() const;
185
186 size_t getExportFuncCount() const;
187
188 size_t getExportForEachCount() const;
189
190 size_t getPragmaCount() const;
191
192 size_t getFuncCount() const;
193
194 size_t getObjectSlotCount() const;
195
196 void getExportVarList(size_t size, void **list);
197
198 void getExportFuncList(size_t size, void **list);
199
200 void getExportForEachList(size_t size, void **list);
201
202 void getExportVarNameList(std::vector<std::string> &list);
203
204 void getExportFuncNameList(std::vector<std::string> &list);
205
206 void getExportForEachNameList(std::vector<std::string> &list);
207
208 void getPragmaList(size_t size,
209 char const **keyList,
210 char const **valueList);
211
212 void getFuncInfoList(size_t size, FuncInfo *list);
213
214 void getObjectSlotList(size_t size, uint32_t *list);
215
216 size_t getELFSize() const;
217
218 const char *getELF() const;
219
220 int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext);
221
222 bool isCacheable() const;
223
224 void setError(int error) {
225 if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) {
226 mErrorCode = error;
227 }
228 }
229
230 int getError() {
231 int result = mErrorCode;
232 mErrorCode = BCC_NO_ERROR;
233 return result;
234 }
235
236 private:
237 //
238 // It returns 0 if there's a cache hit.
239 //
240 // Side effect: it will set mCacheDir, mCacheName.
241 int internalLoadCache(char const *cacheDir, char const *cacheName,
242 bool checkOnly);
243
244 int internalCompile(const CompilerOption&);
245 };
246
247} // namespace bcc
248
249#endif // BCC_EXECUTION_ENGINE_RS_SCRIPT_H