Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 Google LLC. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
| 7 | |
| 8 | #ifndef SKSL_REHYDRATOR |
| 9 | #define SKSL_REHYDRATOR |
| 10 | |
Ethan Nicholas | daed259 | 2021-03-04 14:30:25 -0500 | [diff] [blame] | 11 | #include "include/private/SkSLDefines.h" |
| 12 | #include "include/private/SkSLModifiers.h" |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 13 | #include "include/private/SkSLSymbol.h" |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 14 | #include "src/sksl/SkSLContext.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 15 | |
| 16 | #include <vector> |
| 17 | |
| 18 | namespace SkSL { |
| 19 | |
| 20 | class Context; |
| 21 | class ErrorReporter; |
Ethan Nicholas | 1e9f7f3 | 2020-10-08 05:28:32 -0400 | [diff] [blame] | 22 | class Expression; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 23 | class IRGenerator; |
Ethan Nicholas | 1e9f7f3 | 2020-10-08 05:28:32 -0400 | [diff] [blame] | 24 | class ProgramElement; |
| 25 | class Statement; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 26 | class SymbolTable; |
| 27 | class Type; |
| 28 | |
| 29 | union FloatIntUnion { |
| 30 | float fFloat; |
| 31 | int32_t fInt; |
| 32 | }; |
| 33 | |
| 34 | /** |
| 35 | * Interprets a simple bytecode format that encodes the structure of an SkSL IR tree. This is used |
| 36 | * to process the .sksl files representing SkSL's core include files, so that they can be quickly |
| 37 | * reconstituted at runtime. |
| 38 | */ |
| 39 | class Rehydrator { |
| 40 | public: |
| 41 | enum Command { |
| 42 | // uint16 id, Type componentType, uint8 count |
| 43 | kArrayType_Command, |
| 44 | // Expression left, uint8 op, Expression right, Type type |
| 45 | kBinary_Command, |
| 46 | // SymbolTable symbolTable, uint8 statementCount, Statement[] statements, bool isScope |
| 47 | kBlock_Command, |
| 48 | // bool value |
| 49 | kBoolLiteral_Command, |
| 50 | kBreak_Command, |
| 51 | // int16 builtin |
| 52 | kBuiltinLayout_Command, |
John Stiles | 988b704 | 2021-04-01 16:54:02 -0400 | [diff] [blame] | 53 | // (All constructors) Type type, uint8 argCount, Expression[] arguments |
John Stiles | 92f2d93 | 2021-04-01 13:39:33 -0400 | [diff] [blame] | 54 | kConstructorArray_Command, |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 55 | kConstructorCompound_Command, |
| 56 | kConstructorCompoundCast_Command, |
John Stiles | 9f5ad49 | 2021-03-31 11:34:13 -0400 | [diff] [blame] | 57 | kConstructorDiagonalMatrix_Command, |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 58 | kConstructorMatrixResize_Command, |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 59 | kConstructorScalarCast_Command, |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 60 | kConstructorSplat_Command, |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 61 | kConstructorStruct_Command, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 62 | kContinue_Command, |
| 63 | kDefaultLayout_Command, |
| 64 | kDefaultModifiers_Command, |
| 65 | kDiscard_Command, |
| 66 | // Statement stmt, Expression test |
| 67 | kDo_Command, |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 68 | // ProgramElement[] elements (reads until command `kElementsComplete_Command` is found) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 69 | kElements_Command, |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 70 | // no arguments--indicates end of Elements list |
| 71 | kElementsComplete_Command, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 72 | // String typeName, SymbolTable symbols, int32[] values |
| 73 | kEnum_Command, |
| 74 | // uint16 id, String name |
| 75 | kEnumType_Command, |
| 76 | // Expression expression |
| 77 | kExpressionStatement_Command, |
| 78 | // uint16 ownerId, uint8 index |
| 79 | kField_Command, |
| 80 | // Expression base, uint8 index, uint8 ownerKind |
| 81 | kFieldAccess_Command, |
| 82 | // float value |
| 83 | kFloatLiteral_Command, |
| 84 | // Statement initializer, Expression test, Expression next, Statement body, |
| 85 | // SymbolTable symbols |
| 86 | kFor_Command, |
| 87 | // Type type, uint16 function, uint8 argCount, Expression[] arguments |
| 88 | kFunctionCall_Command, |
| 89 | // uint16 declaration, Statement body, uint8 refCount, uint16[] referencedIntrinsics |
| 90 | kFunctionDefinition_Command, |
| 91 | // uint16 id, Modifiers modifiers, String name, uint8 parameterCount, uint16[] parameterIds, |
| 92 | // Type returnType |
| 93 | kFunctionDeclaration_Command, |
| 94 | // bool isStatic, Expression test, Statement ifTrue, Statement ifFalse |
| 95 | kIf_Command, |
| 96 | // Expression base, Expression index |
| 97 | kIndex_Command, |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 98 | // FunctionDeclaration function |
| 99 | kInlineMarker_Command, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 100 | // Variable* var, String typeName, String instanceName, uint8 sizeCount, Expression[] sizes |
| 101 | kInterfaceBlock_Command, |
| 102 | // int32 value |
| 103 | kIntLiteral_Command, |
| 104 | // int32 flags, int8 location, int8 offset, int8 binding, int8 index, int8 set, |
| 105 | // int16 builtin, int8 inputAttachmentIndex, int8 format, int8 primitive, int8 maxVertices, |
| 106 | // int8 invocations, String marker, String when, int8 key, int8 ctype |
| 107 | kLayout_Command, |
| 108 | // Layout layout, uint8 flags |
| 109 | kModifiers8Bit_Command, |
| 110 | // Layout layout, uint32 flags |
| 111 | kModifiers_Command, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 112 | // uint8 op, Expression operand |
| 113 | kPostfix_Command, |
| 114 | // uint8 op, Expression operand |
| 115 | kPrefix_Command, |
| 116 | // Expression value |
| 117 | kReturn_Command, |
| 118 | // String name, Expression value |
| 119 | kSetting_Command, |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 120 | // uint16 id, Type structType |
| 121 | kStructDefinition_Command, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 122 | // uint16 id, String name, uint8 fieldCount, (Modifiers, String, Type)[] fields |
| 123 | kStructType_Command, |
| 124 | // bool isStatic, SymbolTable symbols, Expression value, uint8 caseCount, |
| 125 | // (Expression value, uint8 statementCount, Statement[] statements)[] cases |
| 126 | kSwitch_Command, |
| 127 | // Expression base, uint8 componentCount, uint8[] components |
| 128 | kSwizzle_Command, |
| 129 | // uint16 id |
| 130 | kSymbolRef_Command, |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 131 | // String name, uint16 origSymbolId |
| 132 | kSymbolAlias_Command, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 133 | // uint16 owned symbol count, Symbol[] ownedSymbols, uint16 symbol count, |
| 134 | // (String, uint16/*index*/)[]. |
| 135 | kSymbolTable_Command, |
| 136 | // uint16 id, String name |
| 137 | kSystemType_Command, |
| 138 | // Expression test, Expression ifTrue, Expression ifFalse |
| 139 | kTernary_Command, |
| 140 | // uint16 id, FunctionDeclaration[] functions |
| 141 | kUnresolvedFunction_Command, |
| 142 | // uint16 id, Modifiers modifiers, String name, Type type, uint8 storage |
| 143 | kVariable_Command, |
| 144 | // uint16 varId, uint8 sizeCount, Expression[] sizes, Expression? value |
| 145 | kVarDeclaration_Command, |
| 146 | // Type baseType, uint8 varCount, VarDeclaration vars |
| 147 | kVarDeclarations_Command, |
| 148 | // uint16 varId, uint8 refKind |
| 149 | kVariableReference_Command, |
| 150 | kVoid_Command, |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | // src must remain in memory as long as the objects created from it do |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 154 | Rehydrator(const Context* context, ModifiersPool* modifiers, |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 155 | std::shared_ptr<SymbolTable> symbolTable, |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 156 | const uint8_t* src, size_t length); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 157 | |
| 158 | std::vector<std::unique_ptr<ProgramElement>> elements(); |
| 159 | |
Brian Osman | 1313d1a | 2020-09-08 10:34:30 -0400 | [diff] [blame] | 160 | std::shared_ptr<SymbolTable> symbolTable(bool inherit = true); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 161 | |
| 162 | private: |
| 163 | int8_t readS8() { |
| 164 | SkASSERT(fIP < fEnd); |
| 165 | return *(fIP++); |
| 166 | } |
| 167 | |
| 168 | uint8_t readU8() { |
| 169 | return this->readS8(); |
| 170 | } |
| 171 | |
| 172 | int16_t readS16() { |
| 173 | uint8_t b1 = this->readU8(); |
| 174 | uint8_t b2 = this->readU8(); |
| 175 | return (b2 << 8) + b1; |
| 176 | } |
| 177 | |
| 178 | uint16_t readU16() { |
| 179 | return this->readS16(); |
| 180 | } |
| 181 | |
| 182 | int32_t readS32() { |
| 183 | uint8_t b1 = this->readU8(); |
| 184 | uint8_t b2 = this->readU8(); |
| 185 | uint8_t b3 = this->readU8(); |
| 186 | uint8_t b4 = this->readU8(); |
| 187 | return (b4 << 24) + (b3 << 16) + (b2 << 8) + b1; |
| 188 | } |
| 189 | |
| 190 | uint32_t readU32() { |
| 191 | return this->readS32(); |
| 192 | } |
| 193 | |
| 194 | StringFragment readString() { |
| 195 | uint16_t offset = this->readU16(); |
| 196 | uint8_t length = *(uint8_t*) (fStart + offset); |
| 197 | const char* chars = (const char*) fStart + offset + 1; |
| 198 | return StringFragment(chars, length); |
| 199 | } |
| 200 | |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 201 | void addSymbol(int id, const Symbol* symbol) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 202 | while ((size_t) id >= fSymbols.size()) { |
| 203 | fSymbols.push_back(nullptr); |
| 204 | } |
| 205 | fSymbols[id] = symbol; |
| 206 | } |
| 207 | |
| 208 | template<typename T> |
| 209 | T* symbolRef(Symbol::Kind kind) { |
| 210 | uint16_t result = this->readU16(); |
| 211 | SkASSERT(fSymbols.size() > result); |
| 212 | return (T*) fSymbols[result]; |
| 213 | } |
| 214 | |
| 215 | Layout layout(); |
| 216 | |
| 217 | Modifiers modifiers(); |
| 218 | |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 219 | const Symbol* symbol(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 220 | |
| 221 | std::unique_ptr<ProgramElement> element(); |
| 222 | |
| 223 | std::unique_ptr<Statement> statement(); |
| 224 | |
| 225 | std::unique_ptr<Expression> expression(); |
| 226 | |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 227 | ExpressionArray expressionArray(); |
| 228 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 229 | const Type* type(); |
| 230 | |
John Stiles | f2872e6 | 2021-05-04 11:38:43 -0400 | [diff] [blame] | 231 | ErrorReporter* errorReporter() { return &fContext.fErrors; } |
| 232 | |
| 233 | ModifiersPool& modifiersPool() const { return fModifiers; } |
| 234 | |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 235 | const Context& fContext; |
| 236 | ModifiersPool& fModifiers; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 237 | std::shared_ptr<SymbolTable> fSymbolTable; |
Brian Osman | 5bf3e20 | 2020-10-13 10:34:18 -0400 | [diff] [blame] | 238 | std::vector<const Symbol*> fSymbols; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 239 | |
| 240 | const uint8_t* fStart; |
| 241 | const uint8_t* fIP; |
| 242 | SkDEBUGCODE(const uint8_t* fEnd;) |
| 243 | |
| 244 | friend class AutoRehydratorSymbolTable; |
| 245 | }; |
| 246 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 247 | } // namespace SkSL |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 248 | |
| 249 | #endif |