blob: b34fe04d7fe135d4ff3b2f0402f34a17de05d6ae [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#include "src/sksl/SkSLDehydrator.h"
9
10#include "src/sksl/SkSLRehydrator.h"
11#include "src/sksl/ir/SkSLBinaryExpression.h"
12#include "src/sksl/ir/SkSLBreakStatement.h"
13#include "src/sksl/ir/SkSLConstructor.h"
14#include "src/sksl/ir/SkSLContinueStatement.h"
15#include "src/sksl/ir/SkSLDiscardStatement.h"
16#include "src/sksl/ir/SkSLDoStatement.h"
17#include "src/sksl/ir/SkSLEnum.h"
18#include "src/sksl/ir/SkSLExpressionStatement.h"
19#include "src/sksl/ir/SkSLField.h"
20#include "src/sksl/ir/SkSLFieldAccess.h"
21#include "src/sksl/ir/SkSLForStatement.h"
22#include "src/sksl/ir/SkSLFunctionCall.h"
23#include "src/sksl/ir/SkSLFunctionDeclaration.h"
24#include "src/sksl/ir/SkSLFunctionDefinition.h"
25#include "src/sksl/ir/SkSLIfStatement.h"
26#include "src/sksl/ir/SkSLIndexExpression.h"
27#include "src/sksl/ir/SkSLIntLiteral.h"
28#include "src/sksl/ir/SkSLInterfaceBlock.h"
29#include "src/sksl/ir/SkSLNullLiteral.h"
30#include "src/sksl/ir/SkSLPostfixExpression.h"
31#include "src/sksl/ir/SkSLPrefixExpression.h"
32#include "src/sksl/ir/SkSLProgramElement.h"
33#include "src/sksl/ir/SkSLReturnStatement.h"
34#include "src/sksl/ir/SkSLSetting.h"
35#include "src/sksl/ir/SkSLStatement.h"
36#include "src/sksl/ir/SkSLSwitchCase.h"
37#include "src/sksl/ir/SkSLSwitchStatement.h"
38#include "src/sksl/ir/SkSLSwizzle.h"
39#include "src/sksl/ir/SkSLSymbol.h"
40#include "src/sksl/ir/SkSLSymbolTable.h"
41#include "src/sksl/ir/SkSLTernaryExpression.h"
42#include "src/sksl/ir/SkSLUnresolvedFunction.h"
43#include "src/sksl/ir/SkSLVarDeclarations.h"
44#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
45#include "src/sksl/ir/SkSLVariable.h"
46#include "src/sksl/ir/SkSLWhileStatement.h"
47
48#ifdef SKSL_STANDALONE
49
50namespace SkSL {
51
52static constexpr int HEADER_SIZE = 2;
53
54class AutoDehydratorSymbolTable {
55public:
56 AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
57 : fDehydrator(dehydrator) {
58 dehydrator->fSymbolMap.emplace_back();
59 if (symbols) {
60 dehydrator->write(*symbols);
61 } else {
62 dehydrator->writeU8(Rehydrator::kVoid_Command);
63 }
64 }
65
66 ~AutoDehydratorSymbolTable() {
67 fDehydrator->fSymbolMap.pop_back();
68 }
69
70private:
71 Dehydrator* fDehydrator;
72};
73
74void Dehydrator::write(Layout l) {
75 if (l == Layout()) {
76 this->writeU8(Rehydrator::kDefaultLayout_Command);
77 } else if (l == Layout::builtin(l.fBuiltin)) {
78 this->writeS8(Rehydrator::kBuiltinLayout_Command);
79 this->writeS16(l.fBuiltin);
80 } else {
81 this->writeS8(Rehydrator::kLayout_Command);
82 fBody.write32(l.fFlags);
83 this->writeS8(l.fLocation);
84 this->writeS8(l.fOffset);
85 this->writeS8(l.fBinding);
86 this->writeS8(l.fIndex);
87 this->writeS8(l.fSet);
88 this->writeS16(l.fBuiltin);
89 this->writeS8(l.fInputAttachmentIndex);
90 this->writeS8((int) l.fFormat);
91 this->writeS8(l.fPrimitive);
92 this->writeS8(l.fMaxVertices);
93 this->writeS8(l.fInvocations);
94 this->write(l.fMarker);
95 this->write(l.fWhen);
96 this->writeS8(l.fKey);
97 this->writeS8((int) l.fCType);
98 }
99}
100
101void Dehydrator::write(Modifiers m) {
102 if (m == Modifiers()) {
103 this->writeU8(Rehydrator::kDefaultModifiers_Command);
104 } else {
105 if (m.fFlags <= 255) {
106 this->writeU8(Rehydrator::kModifiers8Bit_Command);
107 this->write(m.fLayout);
108 this->writeU8(m.fFlags);
109 } else {
110 this->writeU8(Rehydrator::kModifiers_Command);
111 this->write(m.fLayout);
112 this->writeS32(m.fFlags);
113 }
114 }
115}
116
117void Dehydrator::write(StringFragment s) {
118 this->write(String(s));
119}
120
121void Dehydrator::write(String s) {
122 auto found = fStrings.find(s);
123 int offset;
124 if (found == fStrings.end()) {
125 offset = fStringBuffer.str().length() + HEADER_SIZE;
126 fStrings.insert({ s, offset });
127 SkASSERT(s.length() <= 255);
128 fStringBuffer.write8(s.length());
129 fStringBuffer.writeString(s);
130 } else {
131 offset = found->second;
132 }
133 this->writeU16(offset);
134}
135
136void Dehydrator::write(const Symbol& s) {
137 uint16_t id = this->symbolId(&s, false);
138 if (id) {
139 this->writeU8(Rehydrator::kSymbolRef_Command);
140 this->writeU16(id);
141 return;
142 }
143 switch (s.fKind) {
144 case Symbol::kFunctionDeclaration_Kind: {
145 const FunctionDeclaration& f = (const FunctionDeclaration&) s;
146 this->writeU8(Rehydrator::kFunctionDeclaration_Command);
147 this->writeId(&f);
148 this->write(f.fModifiers);
149 this->write(f.fName);
150 this->writeU8(f.fParameters.size());
151 for (const Variable* p : f.fParameters) {
152 this->writeU16(this->symbolId(p));
153 }
154 this->write(f.fReturnType);
155 break;
156 }
157 case Symbol::kUnresolvedFunction_Kind: {
158 const UnresolvedFunction& f = (const UnresolvedFunction&) s;
159 this->writeU8(Rehydrator::kUnresolvedFunction_Command);
160 this->writeId(&f);
161 this->writeU8(f.fFunctions.size());
162 for (const FunctionDeclaration* f : f.fFunctions) {
163 this->write(*f);
164 }
165 break;
166 }
167 case Symbol::kType_Kind: {
168 const Type& t = (const Type&) s;
169 switch (t.kind()) {
170 case Type::kArray_Kind:
171 this->writeU8(Rehydrator::kArrayType_Command);
172 this->writeId(&t);
173 this->write(t.componentType());
174 this->writeU8(t.columns());
175 break;
176 case Type::kEnum_Kind:
177 this->writeU8(Rehydrator::kEnumType_Command);
178 this->writeId(&t);
179 this->write(t.fName);
180 break;
181 case Type::kNullable_Kind:
182 this->writeU8(Rehydrator::kNullableType_Command);
183 this->writeId(&t);
184 this->write(t.componentType());
185 break;
186 case Type::kStruct_Kind:
187 this->writeU8(Rehydrator::kStructType_Command);
188 this->writeId(&t);
189 this->write(t.fName);
190 this->writeU8(t.fields().size());
191 for (const Type::Field& f : t.fields()) {
192 this->write(f.fModifiers);
193 this->write(f.fName);
194 this->write(*f.fType);
195 }
196 break;
197 default:
198 this->writeU8(Rehydrator::kSystemType_Command);
199 this->writeId(&t);
200 this->write(t.fName);
201 }
202 break;
203 }
204 case Symbol::kVariable_Kind: {
205 Variable& v = (Variable&) s;
206 this->writeU8(Rehydrator::kVariable_Command);
207 this->writeId(&v);
208 this->write(v.fModifiers);
209 this->write(v.fName);
210 this->write(v.fType);
211 this->writeU8(v.fStorage);
212 break;
213 }
214 case Symbol::kField_Kind: {
215 Field& f = (Field&) s;
216 this->writeU8(Rehydrator::kField_Command);
217 this->writeU16(this->symbolId(&f.fOwner));
218 this->writeU8(f.fFieldIndex);
219 break;
220 }
221 case Symbol::kExternal_Kind:
222 SkASSERT(false);
223 break;
224 }
225}
226
227void Dehydrator::write(const SymbolTable& symbols) {
228 this->writeU8(Rehydrator::kSymbolTable_Command);
229 this->writeU16(symbols.fOwnedSymbols.size());
230 for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
231 this->write(*s);
232 }
233 this->writeU16(symbols.fSymbols.size());
234 for (std::pair<StringFragment, const Symbol*> p : symbols.fSymbols) {
235 this->write(p.first);
236 bool found = false;
237 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
238 if (symbols.fOwnedSymbols[i].get() == p.second) {
239 this->writeU16(i);
240 found = true;
241 break;
242 }
243 }
244 SkASSERT(found);
245 }
246}
247
248void Dehydrator::write(const Expression* e) {
249 if (e) {
250 switch (e->fKind) {
251 case Expression::kBinary_Kind: {
252 BinaryExpression& b = (BinaryExpression&) *e;
253 this->writeU8(Rehydrator::kBinary_Command);
254 this->write(b.fLeft.get());
255 this->writeU8((int) b.fOperator);
256 this->write(b.fRight.get());
257 this->write(b.fType);
258 break;
259 }
260 case Expression::kBoolLiteral_Kind: {
261 BoolLiteral& b = (BoolLiteral&) *e;
262 this->writeU8(Rehydrator::kBoolLiteral_Command);
263 this->writeU8(b.fValue);
264 break;
265 }
266 case Expression::kConstructor_Kind: {
267 Constructor& c = (Constructor&) *e;
268 this->writeU8(Rehydrator::kConstructor_Command);
269 this->write(c.fType);
270 this->writeU8(c.fArguments.size());
271 for (const auto& a : c.fArguments) {
272 this->write(a.get());
273 }
274 break;
275 }
276 case Expression::kExternalFunctionCall_Kind:
277 case Expression::kExternalValue_Kind:
278 // not implemented; doesn't seem like we'll ever need them from within an include
279 // file
280 SkASSERT(false);
281 break;
282 case Expression::kFieldAccess_Kind: {
283 FieldAccess& f = (FieldAccess&) *e;
284 this->writeU8(Rehydrator::kFieldAccess_Command);
285 this->write(f.fBase.get());
286 this->writeU8(f.fFieldIndex);
287 this->writeU8(f.fOwnerKind);
288 break;
289 }
290 case Expression::kFloatLiteral_Kind: {
291 FloatLiteral& f = (FloatLiteral&) *e;
292 this->writeU8(Rehydrator::kFloatLiteral_Command);
293 FloatIntUnion u;
294 u.fFloat = f.fValue;
295 this->writeS32(u.fInt);
296 break;
297 }
298 case Expression::kFunctionCall_Kind: {
299 FunctionCall& f = (FunctionCall&) *e;
300 this->writeU8(Rehydrator::kFunctionCall_Command);
301 this->write(f.fType);
302 this->writeId(&f.fFunction);
303 this->writeU8(f.fArguments.size());
304 for (const auto& a : f.fArguments) {
305 this->write(a.get());
306 }
307 break;
308 }
309 case Expression::kIndex_Kind: {
310 IndexExpression& i = (IndexExpression&) *e;
311 this->writeU8(Rehydrator::kIndex_Command);
312 this->write(i.fBase.get());
313 this->write(i.fIndex.get());
314 break;
315 }
316 case Expression::kIntLiteral_Kind: {
317 IntLiteral& i = (IntLiteral&) *e;
318 this->writeU8(Rehydrator::kIntLiteral_Command);
319 this->writeS32(i.fValue);
320 break;
321 }
322 case Expression::kNullLiteral_Kind:
323 this->writeU8(Rehydrator::kNullLiteral_Command);
324 break;
325 case Expression::kPostfix_Kind: {
326 PostfixExpression& p = (PostfixExpression&) *e;
327 this->writeU8(Rehydrator::kPostfix_Command);
328 this->writeU8((int) p.fOperator);
329 this->write(p.fOperand.get());
330 break;
331 }
332 case Expression::kPrefix_Kind: {
333 PrefixExpression& p = (PrefixExpression&) *e;
334 this->writeU8(Rehydrator::kPrefix_Command);
335 this->writeU8((int) p.fOperator);
336 this->write(p.fOperand.get());
337 break;
338 }
339 case Expression::kSetting_Kind: {
340 Setting& s = (Setting&) *e;
341 this->writeU8(Rehydrator::kSetting_Command);
342 this->write(s.fName);
343 this->write(s.fValue.get());
344 break;
345 }
346 case Expression::kSwizzle_Kind: {
347 Swizzle& s = (Swizzle&) *e;
348 this->writeU8(Rehydrator::kSwizzle_Command);
349 this->write(s.fBase.get());
350 this->writeU8(s.fComponents.size());
351 for (int c : s.fComponents) {
352 this->writeU8(c);
353 }
354 break;
355 }
356 case Expression::kTernary_Kind: {
357 TernaryExpression& t = (TernaryExpression&) *e;
358 this->writeU8(Rehydrator::kTernary_Command);
359 this->write(t.fTest.get());
360 this->write(t.fIfTrue.get());
361 this->write(t.fIfFalse.get());
362 break;
363 }
364 case Expression::kVariableReference_Kind: {
365 VariableReference& v = (VariableReference&) *e;
366 this->writeU8(Rehydrator::kVariableReference_Command);
367 this->writeId(&v.fVariable);
368 this->writeU8(v.fRefKind);
369 break;
370 }
371 case Expression::kFunctionReference_Kind:
372 case Expression::kTypeReference_Kind:
373 case Expression::kDefined_Kind:
374 // shouldn't appear in finished code
375 SkASSERT(false);
376 break;
377 }
378 } else {
379 this->writeU8(Rehydrator::kVoid_Command);
380 }
381}
382
383void Dehydrator::write(const Statement* s) {
384 if (s) {
385 switch (s->fKind) {
386 case Statement::kBlock_Kind: {
387 Block& b = (Block&) *s;
388 this->writeU8(Rehydrator::kBlock_Command);
389 AutoDehydratorSymbolTable symbols(this, b.fSymbols);
390 this->writeU8(b.fStatements.size());
391 for (const auto& s : b.fStatements) {
392 this->write(s.get());
393 }
394 this->writeU8(b.fIsScope);
395 break;
396 }
397 case Statement::kBreak_Kind:
398 this->writeU8(Rehydrator::kBreak_Command);
399 break;
400 case Statement::kContinue_Kind:
401 this->writeU8(Rehydrator::kContinue_Command);
402 break;
403 case Statement::kDiscard_Kind:
404 this->writeU8(Rehydrator::kDiscard_Command);
405 break;
406 case Statement::kDo_Kind: {
407 DoStatement& d = (DoStatement&) *s;
408 this->writeU8(Rehydrator::kDo_Command);
409 this->write(d.fStatement.get());
410 this->write(d.fTest.get());
411 break;
412 }
413 case Statement::kExpression_Kind: {
414 ExpressionStatement& e = (ExpressionStatement&) *s;
415 this->writeU8(Rehydrator::kExpressionStatement_Command);
416 this->write(e.fExpression.get());
417 break;
418 }
419 case Statement::kFor_Kind: {
420 ForStatement& f = (ForStatement&) *s;
421 this->writeU8(Rehydrator::kFor_Command);
422 this->write(f.fInitializer.get());
423 this->write(f.fTest.get());
424 this->write(f.fNext.get());
425 this->write(f.fStatement.get());
426 this->write(f.fSymbols);
427 break;
428 }
429 case Statement::kIf_Kind: {
430 IfStatement& i = (IfStatement&) *s;
431 this->writeU8(Rehydrator::kIf_Command);
432 this->writeU8(i.fIsStatic);
433 this->write(i.fTest.get());
434 this->write(i.fIfTrue.get());
435 this->write(i.fIfFalse.get());
436 break;
437 }
438 case Statement::kNop_Kind:
439 SkASSERT(false);
440 break;
441 case Statement::kReturn_Kind: {
442 ReturnStatement& r = (ReturnStatement&) *s;
443 this->writeU8(Rehydrator::kReturn_Command);
444 this->write(r.fExpression.get());
445 break;
446 }
447 case Statement::kSwitch_Kind: {
448 SwitchStatement& ss = (SwitchStatement&) *s;
449 this->writeU8(Rehydrator::kSwitch_Command);
450 this->writeU8(ss.fIsStatic);
451 AutoDehydratorSymbolTable symbols(this, ss.fSymbols);
452 this->write(ss.fValue.get());
453 this->writeU8(ss.fCases.size());
454 for (const auto& sc : ss.fCases) {
455 this->write(sc->fValue.get());
456 this->writeU8(sc->fStatements.size());
457 for (const auto& stmt : sc->fStatements) {
458 this->write(stmt.get());
459 }
460 }
461 break;
462 }
463 case Statement::kVarDeclaration_Kind: {
464 VarDeclaration& v = (VarDeclaration&) *s;
465 this->writeU8(Rehydrator::kVarDeclaration_Command);
466 this->writeU16(this->symbolId(v.fVar));
467 this->writeU8(v.fSizes.size());
468 for (const auto& s : v.fSizes) {
469 this->write(s.get());
470 }
471 this->write(v.fValue.get());
472 break;
473 }
474 case Statement::kVarDeclarations_Kind: {
475 VarDeclarationsStatement& v = (VarDeclarationsStatement&) *s;
476 this->write(*v.fDeclaration);
477 break;
478 }
479 case Statement::kWhile_Kind: {
480 WhileStatement& w = (WhileStatement&) *s;
481 this->writeU8(Rehydrator::kWhile_Command);
482 this->write(w.fTest.get());
483 this->write(w.fStatement.get());
484 break;
485 }
486 }
487 } else {
488 this->writeU8(Rehydrator::kVoid_Command);
489 }
490}
491
492void Dehydrator::write(const ProgramElement& e) {
493 switch (e.fKind) {
494 case ProgramElement::kEnum_Kind: {
495 Enum& en = (Enum&) e;
496 this->writeU8(Rehydrator::kEnum_Command);
497 this->write(en.fTypeName);
498 AutoDehydratorSymbolTable symbols(this, en.fSymbols);
499 for (const auto& s : en.fSymbols->fOwnedSymbols) {
500 SkASSERT(s->fKind == Symbol::kVariable_Kind);
501 Variable& v = (Variable&) *s;
502 SkASSERT(v.fInitialValue &&
503 v.fInitialValue->fKind == Expression::kIntLiteral_Kind);
504 IntLiteral& i = (IntLiteral&) *v.fInitialValue;
505 this->writeS32(i.fValue);
506 }
507 break;
508 }
509 case ProgramElement::kExtension_Kind:
510 SkASSERT(false);
511 break;
512 case ProgramElement::kFunction_Kind: {
513 FunctionDefinition& f = (FunctionDefinition&) e;
514 this->writeU8(Rehydrator::kFunctionDefinition_Command);
515 this->writeU16(this->symbolId(&f.fDeclaration));
516 this->write(f.fBody.get());
517 this->writeU8(f.fReferencedIntrinsics.size());
518 for (const FunctionDeclaration* ref : f.fReferencedIntrinsics) {
519 this->writeU16(this->symbolId(ref));
520 }
521 break;
522 }
523 case ProgramElement::kInterfaceBlock_Kind: {
524 InterfaceBlock& i = (InterfaceBlock&) e;
525 this->writeU8(Rehydrator::kInterfaceBlock_Command);
526 this->write(i.fVariable);
527 this->write(i.fTypeName);
528 this->write(i.fInstanceName);
529 this->writeU8(i.fSizes.size());
530 for (const auto& s : i.fSizes) {
531 this->write(s.get());
532 }
533 break;
534 }
535 case ProgramElement::kModifiers_Kind:
536 SkASSERT(false);
537 break;
538 case ProgramElement::kSection_Kind:
539 SkASSERT(false);
540 break;
541 case ProgramElement::kVar_Kind: {
542 VarDeclarations& v = (VarDeclarations&) e;
543 this->writeU8(Rehydrator::kVarDeclarations_Command);
544 this->write(v.fBaseType);
545 this->writeU8(v.fVars.size());
546 for (const auto& v : v.fVars) {
547 this->write(v.get());
548 }
549 break;
550 }
551 }
552}
553
554void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
555 this->writeU8(Rehydrator::kElements_Command);
556 this->writeU8(elements.size());
557 for (const auto& e : elements) {
558 this->write(*e);
559 }
560}
561
562void Dehydrator::finish(OutputStream& out) {
563 out.write16(fStringBuffer.str().size());
564 out.writeString(fStringBuffer.str());
565 out.writeString(fBody.str());
566}
567
568} // namespace
569
570#endif