Zonr Chang | 19218c0 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 1 | /* |
| 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 Hines | 2f6a493 | 2012-05-03 12:27:13 -0700 | [diff] [blame] | 17 | #ifndef BCC_EXECUTION_ENGINE_RS_SCRIPT_H |
| 18 | #define BCC_EXECUTION_ENGINE_RS_SCRIPT_H |
Zonr Chang | 19218c0 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 19 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 20 | #include <vector> |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stddef.h> |
| 25 | |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 26 | #include <llvm/ADT/SmallVector.h> |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 27 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 28 | #include <bcc/bcc.h> |
| 29 | #include <bcc/bcc_mccache.h> |
| 30 | #include "bcc_internal.h" |
| 31 | |
Stephen Hines | 09ebd17 | 2012-05-03 12:28:26 -0700 | [diff] [blame] | 32 | #include "Compiler.h" |
Stephen Hines | 2f6a493 | 2012-05-03 12:27:13 -0700 | [diff] [blame] | 33 | #include "Script.h" |
Zonr Chang | 19218c0 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 34 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 35 | namespace llvm { |
| 36 | class Module; |
| 37 | class GDBJITRegistrar; |
| 38 | } |
| 39 | |
Zonr Chang | 19218c0 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 40 | namespace bcc { |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 41 | class ScriptCompiled; |
| 42 | class ScriptCached; |
| 43 | class Source; |
| 44 | struct CompilerOption; |
Zonr Chang | 19218c0 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 45 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 46 | namespace ScriptStatus { |
| 47 | enum StatusType { |
| 48 | Unknown, |
| 49 | Compiled, |
| 50 | Cached |
| 51 | }; |
| 52 | } |
Zonr Chang | 19218c0 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 53 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 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: |
Stephen Hines | 0f6b1d3 | 2012-05-03 12:29:33 -0700 | [diff] [blame] | 67 | MCO_ResourceType mSourceType; |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 68 | std::string mSourceName; |
| 69 | uint8_t mSHA1[20]; |
| 70 | |
| 71 | public: |
Stephen Hines | 0f6b1d3 | 2012-05-03 12:29:33 -0700 | [diff] [blame] | 72 | SourceDependency(MCO_ResourceType pSourceType, |
| 73 | const std::string &pSourceName, |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 74 | const uint8_t *pSHA1); |
| 75 | |
Stephen Hines | 0f6b1d3 | 2012-05-03 12:29:33 -0700 | [diff] [blame] | 76 | inline MCO_ResourceType getSourceType() const |
| 77 | { return mSourceType; } |
| 78 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 87 | private: |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 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 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 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(); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 128 | |
| 129 | public: |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 130 | RSScript(Source &pSource); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 131 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 132 | ~RSScript(); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 133 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 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. |
Stephen Hines | 0f6b1d3 | 2012-05-03 12:29:33 -0700 | [diff] [blame] | 137 | bool addSourceDependency(MCO_ResourceType pSourceType, |
| 138 | const std::string &pSourceName, |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 139 | const uint8_t *pSHA1); |
Stephen Hines | 7dfc4d8 | 2012-05-03 12:25:21 -0700 | [diff] [blame] | 140 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 141 | const SourceDependencyListTy &getSourceDependencies() const |
| 142 | { return mSourceDependencies; } |
| 143 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 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&); |
Zonr Chang | 19218c0 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 245 | }; |
| 246 | |
Stephen Hines | 4a68b1c | 2012-05-03 12:28:14 -0700 | [diff] [blame] | 247 | } // namespace bcc |
Zonr Chang | 19218c0 | 2012-04-05 10:44:53 +0800 | [diff] [blame] | 248 | |
Stephen Hines | 2f6a493 | 2012-05-03 12:27:13 -0700 | [diff] [blame] | 249 | #endif // BCC_EXECUTION_ENGINE_RS_SCRIPT_H |