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
Stephen Hines2f6a4932012-05-03 12:27:13 -070017#ifndef BCC_EXECUTION_ENGINE_RS_SCRIPT_H
18#define BCC_EXECUTION_ENGINE_RS_SCRIPT_H
Zonr Chang19218c02012-04-05 10:44:53 +080019
Stephen Hines4a68b1c2012-05-03 12:28:14 -070020#include <vector>
Stephen Hines7dfc4d82012-05-03 12:25:21 -070021#include <string>
22
Stephen Hines4a68b1c2012-05-03 12:28:14 -070023#include <stdint.h>
24#include <stddef.h>
25
Stephen Hines7dfc4d82012-05-03 12:25:21 -070026#include <llvm/ADT/SmallVector.h>
27#include <llvm/Support/CodeGen.h>
28
Stephen Hines4a68b1c2012-05-03 12:28:14 -070029#include <bcc/bcc.h>
30#include <bcc/bcc_mccache.h>
31#include "bcc_internal.h"
32
Stephen Hines2f6a4932012-05-03 12:27:13 -070033#include "Script.h"
Zonr Chang19218c02012-04-05 10:44:53 +080034
Stephen Hines4a68b1c2012-05-03 12:28:14 -070035namespace llvm {
36 class Module;
37 class GDBJITRegistrar;
38}
39
Zonr Chang19218c02012-04-05 10:44:53 +080040namespace bcc {
Stephen Hines4a68b1c2012-05-03 12:28:14 -070041 class RSInfo;
42 class ScriptCompiled;
43 class ScriptCached;
44 class Source;
45 struct CompilerOption;
Zonr Chang19218c02012-04-05 10:44:53 +080046
Stephen Hines4a68b1c2012-05-03 12:28:14 -070047 namespace ScriptStatus {
48 enum StatusType {
49 Unknown,
50 Compiled,
51 Cached
52 };
53 }
Zonr Chang19218c02012-04-05 10:44:53 +080054
Stephen Hines4a68b1c2012-05-03 12:28:14 -070055 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:
68 std::string mSourceName;
69 uint8_t mSHA1[20];
70
71 public:
72 SourceDependency(const std::string &pSourceName,
73 const uint8_t *pSHA1);
74
75 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
Stephen Hines7dfc4d82012-05-03 12:25:21 -070083 private:
Stephen Hines4a68b1c2012-05-03 12:28:14 -070084 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
112 const RSInfo *mInfo;
113
114 // 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();
Stephen Hines7dfc4d82012-05-03 12:25:21 -0700126
127 public:
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700128 RSScript(Source &pSource);
Stephen Hines7dfc4d82012-05-03 12:25:21 -0700129
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700130 ~RSScript();
Stephen Hines7dfc4d82012-05-03 12:25:21 -0700131
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700132 // 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.
135 bool addSourceDependency(const std::string &pSourceName,
136 const uint8_t *pSHA1);
Stephen Hines7dfc4d82012-05-03 12:25:21 -0700137
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700138 const SourceDependencyListTy &getSourceDependencies() const
139 { return mSourceDependencies; }
140
141 // 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
148 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&);
Zonr Chang19218c02012-04-05 10:44:53 +0800249 };
250
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700251} // namespace bcc
Zonr Chang19218c02012-04-05 10:44:53 +0800252
Stephen Hines2f6a4932012-05-03 12:27:13 -0700253#endif // BCC_EXECUTION_ENGINE_RS_SCRIPT_H