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
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>
Stephen Hines7dfc4d82012-05-03 12:25:21 -070027
Stephen Hines4a68b1c2012-05-03 12:28:14 -070028#include <bcc/bcc.h>
29#include <bcc/bcc_mccache.h>
30#include "bcc_internal.h"
31
Stephen Hines09ebd172012-05-03 12:28:26 -070032#include "Compiler.h"
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 ScriptCompiled;
42 class ScriptCached;
43 class Source;
44 struct CompilerOption;
Zonr Chang19218c02012-04-05 10:44:53 +080045
Stephen Hines4a68b1c2012-05-03 12:28:14 -070046 namespace ScriptStatus {
47 enum StatusType {
48 Unknown,
49 Compiled,
50 Cached
51 };
52 }
Zonr Chang19218c02012-04-05 10:44:53 +080053
Stephen Hines4a68b1c2012-05-03 12:28:14 -070054 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:
Stephen Hines0f6b1d32012-05-03 12:29:33 -070067 MCO_ResourceType mSourceType;
Stephen Hines4a68b1c2012-05-03 12:28:14 -070068 std::string mSourceName;
69 uint8_t mSHA1[20];
70
71 public:
Stephen Hines0f6b1d32012-05-03 12:29:33 -070072 SourceDependency(MCO_ResourceType pSourceType,
73 const std::string &pSourceName,
Stephen Hines4a68b1c2012-05-03 12:28:14 -070074 const uint8_t *pSHA1);
75
Stephen Hines0f6b1d32012-05-03 12:29:33 -070076 inline MCO_ResourceType getSourceType() const
77 { return mSourceType; }
78
Stephen Hines4a68b1c2012-05-03 12:28:14 -070079 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
Stephen Hines7dfc4d82012-05-03 12:25:21 -070087 private:
Stephen Hines4a68b1c2012-05-03 12:28:14 -070088 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
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700116 // 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();
Stephen Hines7dfc4d82012-05-03 12:25:21 -0700128
129 public:
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700130 RSScript(Source &pSource);
Stephen Hines7dfc4d82012-05-03 12:25:21 -0700131
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700132 ~RSScript();
Stephen Hines7dfc4d82012-05-03 12:25:21 -0700133
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700134 // 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.
Stephen Hines0f6b1d32012-05-03 12:29:33 -0700137 bool addSourceDependency(MCO_ResourceType pSourceType,
138 const std::string &pSourceName,
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700139 const uint8_t *pSHA1);
Stephen Hines7dfc4d82012-05-03 12:25:21 -0700140
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700141 const SourceDependencyListTy &getSourceDependencies() const
142 { return mSourceDependencies; }
143
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700144 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&);
Zonr Chang19218c02012-04-05 10:44:53 +0800245 };
246
Stephen Hines4a68b1c2012-05-03 12:28:14 -0700247} // namespace bcc
Zonr Chang19218c02012-04-05 10:44:53 +0800248
Stephen Hines2f6a4932012-05-03 12:27:13 -0700249#endif // BCC_EXECUTION_ENGINE_RS_SCRIPT_H