blob: 927d988b3eb2683d89d2966e4aac00705ebcad47 [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
John Stiles10d39d92021-05-04 16:13:14 -0400154 Rehydrator(const Context* context, std::shared_ptr<SymbolTable> symbolTable,
John Stiles7c3515b2020-10-16 18:38:39 -0400155 const uint8_t* src, size_t length);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400156
157 std::vector<std::unique_ptr<ProgramElement>> elements();
158
Brian Osman1313d1a2020-09-08 10:34:30 -0400159 std::shared_ptr<SymbolTable> symbolTable(bool inherit = true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400160
161private:
162 int8_t readS8() {
163 SkASSERT(fIP < fEnd);
164 return *(fIP++);
165 }
166
167 uint8_t readU8() {
168 return this->readS8();
169 }
170
171 int16_t readS16() {
172 uint8_t b1 = this->readU8();
173 uint8_t b2 = this->readU8();
174 return (b2 << 8) + b1;
175 }
176
177 uint16_t readU16() {
178 return this->readS16();
179 }
180
181 int32_t readS32() {
182 uint8_t b1 = this->readU8();
183 uint8_t b2 = this->readU8();
184 uint8_t b3 = this->readU8();
185 uint8_t b4 = this->readU8();
186 return (b4 << 24) + (b3 << 16) + (b2 << 8) + b1;
187 }
188
189 uint32_t readU32() {
190 return this->readS32();
191 }
192
193 StringFragment readString() {
194 uint16_t offset = this->readU16();
195 uint8_t length = *(uint8_t*) (fStart + offset);
196 const char* chars = (const char*) fStart + offset + 1;
197 return StringFragment(chars, length);
198 }
199
Brian Osman5bf3e202020-10-13 10:34:18 -0400200 void addSymbol(int id, const Symbol* symbol) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400201 while ((size_t) id >= fSymbols.size()) {
202 fSymbols.push_back(nullptr);
203 }
204 fSymbols[id] = symbol;
205 }
206
207 template<typename T>
208 T* symbolRef(Symbol::Kind kind) {
209 uint16_t result = this->readU16();
210 SkASSERT(fSymbols.size() > result);
211 return (T*) fSymbols[result];
212 }
213
214 Layout layout();
215
216 Modifiers modifiers();
217
Brian Osman5bf3e202020-10-13 10:34:18 -0400218 const Symbol* symbol();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400219
220 std::unique_ptr<ProgramElement> element();
221
222 std::unique_ptr<Statement> statement();
223
224 std::unique_ptr<Expression> expression();
225
John Stilesd8eb8752021-04-01 11:49:10 -0400226 ExpressionArray expressionArray();
227
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400228 const Type* type();
229
John Stilesf2872e62021-05-04 11:38:43 -0400230 ErrorReporter* errorReporter() { return &fContext.fErrors; }
231
John Stiles10d39d92021-05-04 16:13:14 -0400232 ModifiersPool& modifiersPool() const { return *fContext.fModifiersPool; }
John Stilesf2872e62021-05-04 11:38:43 -0400233
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400234 const Context& fContext;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400235 std::shared_ptr<SymbolTable> fSymbolTable;
Brian Osman5bf3e202020-10-13 10:34:18 -0400236 std::vector<const Symbol*> fSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400237
238 const uint8_t* fStart;
239 const uint8_t* fIP;
240 SkDEBUGCODE(const uint8_t* fEnd;)
241
242 friend class AutoRehydratorSymbolTable;
243};
244
John Stilesa6841be2020-08-06 14:11:56 -0400245} // namespace SkSL
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400246
247#endif