Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 1 | /* |
Stephen Hines | cc366e5 | 2012-02-21 17:22:04 -0800 | [diff] [blame] | 2 | * Copyright 2010-2012, The Android Open Source Project |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 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 | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 17 | #ifndef BCC_SCRIPT_H |
| 18 | #define BCC_SCRIPT_H |
| 19 | |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 20 | #include <bcc/bcc.h> |
| 21 | #include "bcc_internal.h" |
| 22 | |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 23 | #include "Compiler.h" |
| 24 | |
| 25 | #include <llvm/Support/CodeGen.h> |
| 26 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | #include <string> |
| 29 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 30 | #include <stddef.h> |
| 31 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 32 | namespace llvm { |
| 33 | class Module; |
| 34 | class GDBJITRegistrar; |
| 35 | } |
Logan | eaa0cc3 | 2010-12-29 01:04:20 +0800 | [diff] [blame] | 36 | |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 37 | namespace bcc { |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 38 | class ScriptCompiled; |
| 39 | class ScriptCached; |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 40 | class SourceInfo; |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 41 | struct CompilerOption; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 42 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 43 | namespace ScriptStatus { |
| 44 | enum StatusType { |
| 45 | Unknown, |
| 46 | Compiled, |
| 47 | Cached |
| 48 | }; |
| 49 | } |
Logan | 3f3d31f | 2010-11-27 13:52:03 +0800 | [diff] [blame] | 50 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 51 | namespace ScriptObject { |
| 52 | enum ObjectType { |
| 53 | Unknown, |
| 54 | Relocatable, |
| 55 | SharedObject, |
| 56 | Executable, |
| 57 | }; |
| 58 | } |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 59 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 60 | class Script { |
| 61 | private: |
| 62 | int mErrorCode; |
Logan | 3973641 | 2010-12-29 00:24:04 +0800 | [diff] [blame] | 63 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 64 | ScriptStatus::StatusType mStatus; |
| 65 | // The type of the object behind this script after compilation. For |
| 66 | // example, after returning from a successful call to prepareRelocatable(), |
| 67 | // the value of mObjectType will be ScriptObject::Relocatable. |
| 68 | ScriptObject::ObjectType mObjectType; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 69 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 70 | union { |
| 71 | ScriptCompiled *mCompiled; |
| 72 | ScriptCached *mCached; |
| 73 | }; |
Logan | cf3e521 | 2010-12-29 01:44:55 +0800 | [diff] [blame] | 74 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 75 | std::string mCacheDir; |
| 76 | std::string mCacheName; |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 77 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 78 | inline std::string getCachedObjectPath() const { |
| 79 | return std::string(mCacheDir + mCacheName + ".o"); |
| 80 | } |
Zonr Chang | 4ea0886 | 2012-01-17 17:26:49 +0800 | [diff] [blame] | 81 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 82 | inline std::string getCacheInfoPath() const { |
| 83 | return getCachedObjectPath().append(".info"); |
| 84 | } |
Logan | ecf4cbd | 2011-01-06 05:34:11 +0800 | [diff] [blame] | 85 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 86 | bool mIsContextSlotNotAvail; |
Logan | 4259805 | 2011-01-26 22:41:13 +0800 | [diff] [blame] | 87 | |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 88 | // Source List |
| 89 | SourceInfo *mSourceList[2]; |
| 90 | // Note: mSourceList[0] (main source) |
| 91 | // Note: mSourceList[1] (library source) |
| 92 | // TODO(logan): Generalize this, use vector or SmallVector instead! |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 93 | |
| 94 | // External Function List |
| 95 | std::vector<char const *> mUserDefinedExternalSymbols; |
| 96 | |
| 97 | // Register Symbol Lookup Function |
| 98 | BCCSymbolLookupFn mpExtSymbolLookupFn; |
| 99 | void *mpExtSymbolLookupFnContext; |
| 100 | |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 101 | public: |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 102 | Script() : mErrorCode(BCC_NO_ERROR), mStatus(ScriptStatus::Unknown), |
| 103 | mObjectType(ScriptObject::Unknown), |
| 104 | mIsContextSlotNotAvail(false), |
| 105 | mpExtSymbolLookupFn(NULL), mpExtSymbolLookupFnContext(NULL) { |
| 106 | Compiler::GlobalInitialization(); |
| 107 | |
| 108 | mSourceList[0] = NULL; |
| 109 | mSourceList[1] = NULL; |
| 110 | } |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 111 | |
| 112 | ~Script(); |
| 113 | |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 114 | int addSourceBC(size_t idx, |
| 115 | char const *resName, |
| 116 | const char *bitcode, |
| 117 | size_t bitcodeSize, |
| 118 | unsigned long flags); |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 119 | |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 120 | int addSourceModule(size_t idx, |
| 121 | llvm::Module *module, |
| 122 | unsigned long flags); |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 123 | |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 124 | int addSourceFile(size_t idx, |
| 125 | char const *path, |
| 126 | unsigned long flags); |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 127 | |
| 128 | void markExternalSymbol(char const *name) { |
| 129 | mUserDefinedExternalSymbols.push_back(name); |
| 130 | } |
| 131 | |
| 132 | std::vector<char const *> const &getUserDefinedExternalSymbols() const { |
| 133 | return mUserDefinedExternalSymbols; |
| 134 | } |
| 135 | |
| 136 | int prepareExecutable(char const *cacheDir, |
| 137 | char const *cacheName, |
| 138 | unsigned long flags); |
| 139 | int writeCache(); |
| 140 | |
| 141 | /* |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 142 | * Link the given bitcodes in mSourceList to shared object (.so). |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 143 | * |
| 144 | * Currently, it requires one to provide the relocatable object files with |
| 145 | * given bitcodes to output a shared object. |
| 146 | * |
| 147 | * The usage of this function is flexible. You can have a relocatable object |
| 148 | * compiled before and pass it in objPath to generate shared object. If the |
| 149 | * objPath is NULL, we'll invoke prepareRelocatable() to get .o first (if |
| 150 | * you haven't done that yet) and then link the output relocatable object |
| 151 | * file to .so in dsoPath. |
| 152 | * |
Stephen Hines | ead5ccb | 2012-05-03 12:30:38 -0700 | [diff] [blame] | 153 | * TODO: Currently, we only support to link the bitcodes in mSourceList[0]. |
Stephen Hines | 97c92c2 | 2012-05-03 12:30:24 -0700 | [diff] [blame] | 154 | * |
| 155 | */ |
| 156 | int prepareSharedObject(char const *objPath, |
| 157 | char const *dsoPath, |
| 158 | unsigned long flags); |
| 159 | |
| 160 | int prepareRelocatable(char const *objPath, |
| 161 | llvm::Reloc::Model RelocModel, |
| 162 | unsigned long flags); |
| 163 | |
| 164 | char const *getCompilerErrorMessage(); |
| 165 | |
| 166 | void *lookup(const char *name); |
| 167 | |
| 168 | size_t getExportVarCount() const; |
| 169 | |
| 170 | size_t getExportFuncCount() const; |
| 171 | |
| 172 | size_t getExportForEachCount() const; |
| 173 | |
| 174 | size_t getPragmaCount() const; |
| 175 | |
| 176 | size_t getFuncCount() const; |
| 177 | |
| 178 | size_t getObjectSlotCount() const; |
| 179 | |
| 180 | void getExportVarList(size_t size, void **list); |
| 181 | |
| 182 | void getExportFuncList(size_t size, void **list); |
| 183 | |
| 184 | void getExportForEachList(size_t size, void **list); |
| 185 | |
| 186 | void getExportVarNameList(std::vector<std::string> &list); |
| 187 | |
| 188 | void getExportFuncNameList(std::vector<std::string> &list); |
| 189 | |
| 190 | void getExportForEachNameList(std::vector<std::string> &list); |
| 191 | |
| 192 | void getPragmaList(size_t size, |
| 193 | char const **keyList, |
| 194 | char const **valueList); |
| 195 | |
| 196 | void getFuncInfoList(size_t size, FuncInfo *list); |
| 197 | |
| 198 | void getObjectSlotList(size_t size, uint32_t *list); |
| 199 | |
| 200 | size_t getELFSize() const; |
| 201 | |
| 202 | const char *getELF() const; |
| 203 | |
| 204 | int registerSymbolCallback(BCCSymbolLookupFn pFn, void *pContext); |
| 205 | |
| 206 | bool isCacheable() const; |
| 207 | |
| 208 | void setError(int error) { |
| 209 | if (mErrorCode == BCC_NO_ERROR && error != BCC_NO_ERROR) { |
| 210 | mErrorCode = error; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | int getError() { |
| 215 | int result = mErrorCode; |
| 216 | mErrorCode = BCC_NO_ERROR; |
| 217 | return result; |
| 218 | } |
| 219 | |
| 220 | private: |
| 221 | // |
| 222 | // It returns 0 if there's a cache hit. |
| 223 | // |
| 224 | // Side effect: it will set mCacheDir, mCacheName. |
| 225 | int internalLoadCache(char const *cacheDir, char const *cacheName, |
| 226 | bool checkOnly); |
| 227 | |
| 228 | int internalCompile(const CompilerOption&); |
| 229 | }; |
| 230 | |
| 231 | } // namespace bcc |
| 232 | |
| 233 | #endif // BCC_SCRIPT_H |