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