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