blob: 91dfd764e0335e2ab385165a73a80cf97b6d179b [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
Ethan Nicholasc18bb512020-07-28 14:46:53 -040029/**
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 */
34class Rehydrator {
35public:
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 Stiles988b7042021-04-01 16:54:02 -040048 // (All constructors) Type type, uint8 argCount, Expression[] arguments
John Stiles92f2d932021-04-01 13:39:33 -040049 kConstructorArray_Command,
John Stilese3ae9682021-08-05 10:35:01 -040050 kConstructorArrayCast_Command,
John Stiles8cad6372021-04-07 12:31:13 -040051 kConstructorCompound_Command,
52 kConstructorCompoundCast_Command,
John Stiles9f5ad492021-03-31 11:34:13 -040053 kConstructorDiagonalMatrix_Command,
John Stiles5abb9e12021-04-06 13:47:19 -040054 kConstructorMatrixResize_Command,
John Stilesfd7252f2021-04-04 22:24:40 -040055 kConstructorScalarCast_Command,
John Stiles5abb9e12021-04-06 13:47:19 -040056 kConstructorSplat_Command,
John Stilesd47330f2021-04-08 23:25:52 -040057 kConstructorStruct_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -040058 kContinue_Command,
59 kDefaultLayout_Command,
60 kDefaultModifiers_Command,
61 kDiscard_Command,
62 // Statement stmt, Expression test
63 kDo_Command,
John Stiles1ea7f542020-11-02 13:07:23 -050064 // ProgramElement[] elements (reads until command `kElementsComplete_Command` is found)
Ethan Nicholasc18bb512020-07-28 14:46:53 -040065 kElements_Command,
John Stiles1ea7f542020-11-02 13:07:23 -050066 // no arguments--indicates end of Elements list
67 kElementsComplete_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -040068 // 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 Stiles3b204892021-08-27 17:35:35 -040081 // uint16 declaration, Statement body, uint8 refCount
Ethan Nicholasc18bb512020-07-28 14:46:53 -040082 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 Stiles98c1f822020-09-09 14:18:53 -040090 // FunctionDeclaration function
91 kInlineMarker_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -040092 // 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 Osman99ddd2a2021-08-27 11:21:12 -040097 // int16 builtin, int8 inputAttachmentIndex
Ethan Nicholasc18bb512020-07-28 14:46:53 -040098 kLayout_Command,
99 // Layout layout, uint8 flags
100 kModifiers8Bit_Command,
101 // Layout layout, uint32 flags
102 kModifiers_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400103 // 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 Stilesdc75a972020-11-25 16:24:55 -0500111 // uint16 id, Type structType
112 kStructDefinition_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400113 // 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 Stiles49a547f2020-10-06 16:14:37 -0400122 // String name, uint16 origSymbolId
123 kSymbolAlias_Command,
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400124 // 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 Nicholasc18bb512020-07-28 14:46:53 -0400142 };
143
144 // src must remain in memory as long as the objects created from it do
John Stiles10d39d92021-05-04 16:13:14 -0400145 Rehydrator(const Context* context, std::shared_ptr<SymbolTable> symbolTable,
John Stiles7c3515b2020-10-16 18:38:39 -0400146 const uint8_t* src, size_t length);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400147
148 std::vector<std::unique_ptr<ProgramElement>> elements();
149
Brian Osman1313d1a2020-09-08 10:34:30 -0400150 std::shared_ptr<SymbolTable> symbolTable(bool inherit = true);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400151
152private:
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 Nicholas962dec42021-06-10 13:06:39 -0400184 skstd::string_view readString() {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400185 uint16_t offset = this->readU16();
186 uint8_t length = *(uint8_t*) (fStart + offset);
187 const char* chars = (const char*) fStart + offset + 1;
Ethan Nicholas962dec42021-06-10 13:06:39 -0400188 return skstd::string_view(chars, length);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400189 }
190
Brian Osman5bf3e202020-10-13 10:34:18 -0400191 void addSymbol(int id, const Symbol* symbol) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400192 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 Osman5bf3e202020-10-13 10:34:18 -0400209 const Symbol* symbol();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400210
211 std::unique_ptr<ProgramElement> element();
212
213 std::unique_ptr<Statement> statement();
214
215 std::unique_ptr<Expression> expression();
216
John Stilesd8eb8752021-04-01 11:49:10 -0400217 ExpressionArray expressionArray();
218
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400219 const Type* type();
220
Ethan Nicholas39f6da42021-08-23 13:10:07 -0400221 ErrorReporter* errorReporter() { return fContext.fErrors; }
John Stilesf2872e62021-05-04 11:38:43 -0400222
John Stiles10d39d92021-05-04 16:13:14 -0400223 ModifiersPool& modifiersPool() const { return *fContext.fModifiersPool; }
John Stilesf2872e62021-05-04 11:38:43 -0400224
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400225 const Context& fContext;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400226 std::shared_ptr<SymbolTable> fSymbolTable;
Brian Osman5bf3e202020-10-13 10:34:18 -0400227 std::vector<const Symbol*> fSymbols;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400228
229 const uint8_t* fStart;
230 const uint8_t* fIP;
231 SkDEBUGCODE(const uint8_t* fEnd;)
232
233 friend class AutoRehydratorSymbolTable;
234};
235
John Stilesa6841be2020-08-06 14:11:56 -0400236} // namespace SkSL
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400237
238#endif