blob: cdcad1d57570f60345133b2e96db395ba0b7ef1f [file] [log] [blame]
Ethan Nicholasc18bb512020-07-28 14:46:53 -04001/*
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 Nicholasdaed2592021-03-04 14:30:25 -050011#include "include/private/SkSLDefines.h"
12#include "include/private/SkSLModifiers.h"
Ethan Nicholas24c17722021-03-09 13:10:59 -050013#include "include/private/SkSLSymbol.h"
John Stilesf2872e62021-05-04 11:38:43 -040014#include "src/sksl/SkSLContext.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040015
16#include <vector>
17
18namespace SkSL {
19
20class Context;
21class ErrorReporter;
Ethan Nicholas1e9f7f32020-10-08 05:28:32 -040022class Expression;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -040023class IRGenerator;
Ethan Nicholas1e9f7f32020-10-08 05:28:32 -040024class ProgramElement;
25class Statement;
Ethan Nicholasc18bb512020-07-28 14:46:53 -040026class SymbolTable;
27class Type;
28
29union 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 */
39class Rehydrator {
40public:
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 Stiles988b7042021-04-01 16:54:02 -040053 // (All constructors) Type type, uint8 argCount, Expression[] arguments
John Stiles92f2d932021-04-01 13:39:33 -040054 kConstructorArray_Command,
John Stiles8cad6372021-04-07 12:31:13 -040055 kConstructorCompound_Command,
56 kConstructorCompoundCast_Command,
John Stiles9f5ad492021-03-31 11:34:13 -040057 kConstructorDiagonalMatrix_Command,
John Stiles5abb9e12021-04-06 13:47:19 -040058 kConstructorMatrixResize_Command,
John Stilesfd7252f2021-04-04 22:24:40 -040059 kConstructorScalarCast_Command,
John Stiles5abb9e12021-04-06 13:47:19 -040060 kConstructorSplat_Command,
John Stilesd47330f2021-04-08 23:25:52 -040061 kConstructorStruct_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -040062 kContinue_Command,
63 kDefaultLayout_Command,
64 kDefaultModifiers_Command,
65 kDiscard_Command,
66 // Statement stmt, Expression test
67 kDo_Command,
John Stiles1ea7f542020-11-02 13:07:23 -050068 // ProgramElement[] elements (reads until command `kElementsComplete_Command` is found)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040069 kElements_Command,
John Stiles1ea7f542020-11-02 13:07:23 -050070 // no arguments--indicates end of Elements list
71 kElementsComplete_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -040072 // 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 Stiles98c1f822020-09-09 14:18:53 -040098 // FunctionDeclaration function
99 kInlineMarker_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400100 // 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 Nicholasc18bb512020-07-28 14:46:53 -0400112 // 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 Stilesdc75a972020-11-25 16:24:55 -0500120 // uint16 id, Type structType
121 kStructDefinition_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400122 // 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 Stiles49a547f2020-10-06 16:14:37 -0400131 // String name, uint16 origSymbolId
132 kSymbolAlias_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400133 // 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 Nicholasc18bb512020-07-28 14:46:53 -0400151 };
152
153 // src must remain in memory as long as the objects created from it do
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400154 Rehydrator(const Context* context, ModifiersPool* modifiers,
John Stilesf2872e62021-05-04 11:38:43 -0400155 std::shared_ptr<SymbolTable> symbolTable,
John Stiles7c3515b2020-10-16 18:38:39 -0400156 const uint8_t* src, size_t length);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400157
158 std::vector<std::unique_ptr<ProgramElement>> elements();
159
Brian Osman1313d1a2020-09-08 10:34:30 -0400160 std::shared_ptr<SymbolTable> symbolTable(bool inherit = true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400161
162private:
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 Osman5bf3e202020-10-13 10:34:18 -0400201 void addSymbol(int id, const Symbol* symbol) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400202 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 Osman5bf3e202020-10-13 10:34:18 -0400219 const Symbol* symbol();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400220
221 std::unique_ptr<ProgramElement> element();
222
223 std::unique_ptr<Statement> statement();
224
225 std::unique_ptr<Expression> expression();
226
John Stilesd8eb8752021-04-01 11:49:10 -0400227 ExpressionArray expressionArray();
228
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400229 const Type* type();
230
John Stilesf2872e62021-05-04 11:38:43 -0400231 ErrorReporter* errorReporter() { return &fContext.fErrors; }
232
233 ModifiersPool& modifiersPool() const { return fModifiers; }
234
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400235 const Context& fContext;
236 ModifiersPool& fModifiers;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400237 std::shared_ptr<SymbolTable> fSymbolTable;
Brian Osman5bf3e202020-10-13 10:34:18 -0400238 std::vector<const Symbol*> fSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400239
240 const uint8_t* fStart;
241 const uint8_t* fIP;
242 SkDEBUGCODE(const uint8_t* fEnd;)
243
244 friend class AutoRehydratorSymbolTable;
245};
246
John Stilesa6841be2020-08-06 14:11:56 -0400247} // namespace SkSL
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400248
249#endif