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