blob: 6488969e9c0a9eba4480a79afcb8090b3560b3c6 [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
John Stiles810c8cf2020-08-26 19:46:27 -040010#include <map>
11
Ethan Nicholasc18bb512020-07-28 14:46:53 -040012#include "src/sksl/SkSLRehydrator.h"
13#include "src/sksl/ir/SkSLBinaryExpression.h"
14#include "src/sksl/ir/SkSLBreakStatement.h"
15#include "src/sksl/ir/SkSLConstructor.h"
16#include "src/sksl/ir/SkSLContinueStatement.h"
17#include "src/sksl/ir/SkSLDiscardStatement.h"
18#include "src/sksl/ir/SkSLDoStatement.h"
19#include "src/sksl/ir/SkSLEnum.h"
20#include "src/sksl/ir/SkSLExpressionStatement.h"
21#include "src/sksl/ir/SkSLField.h"
22#include "src/sksl/ir/SkSLFieldAccess.h"
23#include "src/sksl/ir/SkSLForStatement.h"
24#include "src/sksl/ir/SkSLFunctionCall.h"
25#include "src/sksl/ir/SkSLFunctionDeclaration.h"
26#include "src/sksl/ir/SkSLFunctionDefinition.h"
27#include "src/sksl/ir/SkSLIfStatement.h"
28#include "src/sksl/ir/SkSLIndexExpression.h"
29#include "src/sksl/ir/SkSLIntLiteral.h"
30#include "src/sksl/ir/SkSLInterfaceBlock.h"
31#include "src/sksl/ir/SkSLNullLiteral.h"
32#include "src/sksl/ir/SkSLPostfixExpression.h"
33#include "src/sksl/ir/SkSLPrefixExpression.h"
34#include "src/sksl/ir/SkSLProgramElement.h"
35#include "src/sksl/ir/SkSLReturnStatement.h"
36#include "src/sksl/ir/SkSLSetting.h"
37#include "src/sksl/ir/SkSLStatement.h"
38#include "src/sksl/ir/SkSLSwitchCase.h"
39#include "src/sksl/ir/SkSLSwitchStatement.h"
40#include "src/sksl/ir/SkSLSwizzle.h"
41#include "src/sksl/ir/SkSLSymbol.h"
42#include "src/sksl/ir/SkSLSymbolTable.h"
43#include "src/sksl/ir/SkSLTernaryExpression.h"
44#include "src/sksl/ir/SkSLUnresolvedFunction.h"
45#include "src/sksl/ir/SkSLVarDeclarations.h"
46#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
47#include "src/sksl/ir/SkSLVariable.h"
48#include "src/sksl/ir/SkSLWhileStatement.h"
49
50#ifdef SKSL_STANDALONE
51
52namespace SkSL {
53
54static constexpr int HEADER_SIZE = 2;
55
56class AutoDehydratorSymbolTable {
57public:
58 AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
59 : fDehydrator(dehydrator) {
60 dehydrator->fSymbolMap.emplace_back();
61 if (symbols) {
62 dehydrator->write(*symbols);
63 } else {
64 dehydrator->writeU8(Rehydrator::kVoid_Command);
65 }
66 }
67
68 ~AutoDehydratorSymbolTable() {
69 fDehydrator->fSymbolMap.pop_back();
70 }
71
72private:
73 Dehydrator* fDehydrator;
74};
75
76void Dehydrator::write(Layout l) {
77 if (l == Layout()) {
78 this->writeU8(Rehydrator::kDefaultLayout_Command);
79 } else if (l == Layout::builtin(l.fBuiltin)) {
80 this->writeS8(Rehydrator::kBuiltinLayout_Command);
81 this->writeS16(l.fBuiltin);
82 } else {
83 this->writeS8(Rehydrator::kLayout_Command);
84 fBody.write32(l.fFlags);
85 this->writeS8(l.fLocation);
86 this->writeS8(l.fOffset);
87 this->writeS8(l.fBinding);
88 this->writeS8(l.fIndex);
89 this->writeS8(l.fSet);
90 this->writeS16(l.fBuiltin);
91 this->writeS8(l.fInputAttachmentIndex);
92 this->writeS8((int) l.fFormat);
93 this->writeS8(l.fPrimitive);
94 this->writeS8(l.fMaxVertices);
95 this->writeS8(l.fInvocations);
96 this->write(l.fMarker);
97 this->write(l.fWhen);
98 this->writeS8(l.fKey);
99 this->writeS8((int) l.fCType);
100 }
101}
102
103void Dehydrator::write(Modifiers m) {
104 if (m == Modifiers()) {
105 this->writeU8(Rehydrator::kDefaultModifiers_Command);
106 } else {
107 if (m.fFlags <= 255) {
108 this->writeU8(Rehydrator::kModifiers8Bit_Command);
109 this->write(m.fLayout);
110 this->writeU8(m.fFlags);
111 } else {
112 this->writeU8(Rehydrator::kModifiers_Command);
113 this->write(m.fLayout);
114 this->writeS32(m.fFlags);
115 }
116 }
117}
118
119void Dehydrator::write(StringFragment s) {
120 this->write(String(s));
121}
122
123void Dehydrator::write(String s) {
124 auto found = fStrings.find(s);
125 int offset;
126 if (found == fStrings.end()) {
127 offset = fStringBuffer.str().length() + HEADER_SIZE;
128 fStrings.insert({ s, offset });
129 SkASSERT(s.length() <= 255);
130 fStringBuffer.write8(s.length());
131 fStringBuffer.writeString(s);
132 } else {
133 offset = found->second;
134 }
135 this->writeU16(offset);
136}
137
138void Dehydrator::write(const Symbol& s) {
139 uint16_t id = this->symbolId(&s, false);
140 if (id) {
141 this->writeU8(Rehydrator::kSymbolRef_Command);
142 this->writeU16(id);
143 return;
144 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400145 switch (s.kind()) {
146 case Symbol::Kind::kFunctionDeclaration: {
John Stiles17c5b702020-08-18 10:40:03 -0400147 const FunctionDeclaration& f = s.as<FunctionDeclaration>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400148 this->writeU8(Rehydrator::kFunctionDeclaration_Command);
149 this->writeId(&f);
150 this->write(f.fModifiers);
151 this->write(f.fName);
152 this->writeU8(f.fParameters.size());
153 for (const Variable* p : f.fParameters) {
154 this->writeU16(this->symbolId(p));
155 }
156 this->write(f.fReturnType);
157 break;
158 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400159 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400160 const UnresolvedFunction& f = s.as<UnresolvedFunction>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400161 this->writeU8(Rehydrator::kUnresolvedFunction_Command);
162 this->writeId(&f);
163 this->writeU8(f.fFunctions.size());
John Stilesf621e232020-08-25 13:33:02 -0400164 for (const FunctionDeclaration* funcDecl : f.fFunctions) {
165 this->write(*funcDecl);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400166 }
167 break;
168 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400169 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -0400170 const Type& t = s.as<Type>();
Ethan Nicholase6592142020-09-08 10:22:09 -0400171 switch (t.typeKind()) {
172 case Type::TypeKind::kArray:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400173 this->writeU8(Rehydrator::kArrayType_Command);
174 this->writeId(&t);
175 this->write(t.componentType());
176 this->writeU8(t.columns());
177 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400178 case Type::TypeKind::kEnum:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400179 this->writeU8(Rehydrator::kEnumType_Command);
180 this->writeId(&t);
181 this->write(t.fName);
182 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400183 case Type::TypeKind::kNullable:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400184 this->writeU8(Rehydrator::kNullableType_Command);
185 this->writeId(&t);
186 this->write(t.componentType());
187 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400188 case Type::TypeKind::kStruct:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400189 this->writeU8(Rehydrator::kStructType_Command);
190 this->writeId(&t);
191 this->write(t.fName);
192 this->writeU8(t.fields().size());
193 for (const Type::Field& f : t.fields()) {
194 this->write(f.fModifiers);
195 this->write(f.fName);
196 this->write(*f.fType);
197 }
198 break;
199 default:
200 this->writeU8(Rehydrator::kSystemType_Command);
201 this->writeId(&t);
202 this->write(t.fName);
203 }
204 break;
205 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400206 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400207 const Variable& v = s.as<Variable>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400208 this->writeU8(Rehydrator::kVariable_Command);
209 this->writeId(&v);
210 this->write(v.fModifiers);
211 this->write(v.fName);
212 this->write(v.fType);
213 this->writeU8(v.fStorage);
214 break;
215 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400216 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400217 const Field& f = s.as<Field>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400218 this->writeU8(Rehydrator::kField_Command);
219 this->writeU16(this->symbolId(&f.fOwner));
220 this->writeU8(f.fFieldIndex);
221 break;
222 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400223 case Symbol::Kind::kExternal:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400224 SkASSERT(false);
225 break;
226 }
227}
228
229void Dehydrator::write(const SymbolTable& symbols) {
230 this->writeU8(Rehydrator::kSymbolTable_Command);
231 this->writeU16(symbols.fOwnedSymbols.size());
232 for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
233 this->write(*s);
234 }
235 this->writeU16(symbols.fSymbols.size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400236 std::map<StringFragment, const Symbol*> ordered;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400237 for (std::pair<StringFragment, const Symbol*> p : symbols.fSymbols) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400238 ordered.insert(p);
239 }
240 for (std::pair<StringFragment, const Symbol*> p : ordered) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400241 this->write(p.first);
242 bool found = false;
243 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
244 if (symbols.fOwnedSymbols[i].get() == p.second) {
245 this->writeU16(i);
246 found = true;
247 break;
248 }
249 }
250 SkASSERT(found);
251 }
252}
253
254void Dehydrator::write(const Expression* e) {
255 if (e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400256 switch (e->kind()) {
257 case Expression::Kind::kBinary: {
John Stiles81365af2020-08-18 09:24:00 -0400258 const BinaryExpression& b = e->as<BinaryExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400259 this->writeU8(Rehydrator::kBinary_Command);
260 this->write(b.fLeft.get());
261 this->writeU8((int) b.fOperator);
262 this->write(b.fRight.get());
263 this->write(b.fType);
264 break;
265 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400266 case Expression::Kind::kBoolLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400267 const BoolLiteral& b = e->as<BoolLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400268 this->writeU8(Rehydrator::kBoolLiteral_Command);
269 this->writeU8(b.fValue);
270 break;
271 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400272 case Expression::Kind::kConstructor: {
John Stiles81365af2020-08-18 09:24:00 -0400273 const Constructor& c = e->as<Constructor>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400274 this->writeU8(Rehydrator::kConstructor_Command);
275 this->write(c.fType);
276 this->writeU8(c.fArguments.size());
277 for (const auto& a : c.fArguments) {
278 this->write(a.get());
279 }
280 break;
281 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400282 case Expression::Kind::kExternalFunctionCall:
283 case Expression::Kind::kExternalValue:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400284 // not implemented; doesn't seem like we'll ever need them from within an include
285 // file
286 SkASSERT(false);
287 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400288 case Expression::Kind::kFieldAccess: {
John Stiles81365af2020-08-18 09:24:00 -0400289 const FieldAccess& f = e->as<FieldAccess>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400290 this->writeU8(Rehydrator::kFieldAccess_Command);
291 this->write(f.fBase.get());
292 this->writeU8(f.fFieldIndex);
293 this->writeU8(f.fOwnerKind);
294 break;
295 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400296 case Expression::Kind::kFloatLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400297 const FloatLiteral& f = e->as<FloatLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400298 this->writeU8(Rehydrator::kFloatLiteral_Command);
299 FloatIntUnion u;
300 u.fFloat = f.fValue;
301 this->writeS32(u.fInt);
302 break;
303 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400304 case Expression::Kind::kFunctionCall: {
John Stiles81365af2020-08-18 09:24:00 -0400305 const FunctionCall& f = e->as<FunctionCall>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400306 this->writeU8(Rehydrator::kFunctionCall_Command);
307 this->write(f.fType);
308 this->writeId(&f.fFunction);
309 this->writeU8(f.fArguments.size());
310 for (const auto& a : f.fArguments) {
311 this->write(a.get());
312 }
313 break;
314 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400315 case Expression::Kind::kIndex: {
John Stiles81365af2020-08-18 09:24:00 -0400316 const IndexExpression& i = e->as<IndexExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400317 this->writeU8(Rehydrator::kIndex_Command);
318 this->write(i.fBase.get());
319 this->write(i.fIndex.get());
320 break;
321 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400322 case Expression::Kind::kIntLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400323 const IntLiteral& i = e->as<IntLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400324 this->writeU8(Rehydrator::kIntLiteral_Command);
325 this->writeS32(i.fValue);
326 break;
327 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400328 case Expression::Kind::kNullLiteral:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400329 this->writeU8(Rehydrator::kNullLiteral_Command);
330 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400331 case Expression::Kind::kPostfix: {
John Stiles81365af2020-08-18 09:24:00 -0400332 const PostfixExpression& p = e->as<PostfixExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400333 this->writeU8(Rehydrator::kPostfix_Command);
334 this->writeU8((int) p.fOperator);
335 this->write(p.fOperand.get());
336 break;
337 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400338 case Expression::Kind::kPrefix: {
John Stiles81365af2020-08-18 09:24:00 -0400339 const PrefixExpression& p = e->as<PrefixExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400340 this->writeU8(Rehydrator::kPrefix_Command);
341 this->writeU8((int) p.fOperator);
342 this->write(p.fOperand.get());
343 break;
344 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400345 case Expression::Kind::kSetting: {
John Stiles81365af2020-08-18 09:24:00 -0400346 const Setting& s = e->as<Setting>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400347 this->writeU8(Rehydrator::kSetting_Command);
348 this->write(s.fName);
349 this->write(s.fValue.get());
350 break;
351 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400352 case Expression::Kind::kSwizzle: {
John Stiles81365af2020-08-18 09:24:00 -0400353 const Swizzle& s = e->as<Swizzle>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400354 this->writeU8(Rehydrator::kSwizzle_Command);
355 this->write(s.fBase.get());
356 this->writeU8(s.fComponents.size());
357 for (int c : s.fComponents) {
358 this->writeU8(c);
359 }
360 break;
361 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400362 case Expression::Kind::kTernary: {
John Stiles81365af2020-08-18 09:24:00 -0400363 const TernaryExpression& t = e->as<TernaryExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400364 this->writeU8(Rehydrator::kTernary_Command);
365 this->write(t.fTest.get());
366 this->write(t.fIfTrue.get());
367 this->write(t.fIfFalse.get());
368 break;
369 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400370 case Expression::Kind::kVariableReference: {
John Stiles81365af2020-08-18 09:24:00 -0400371 const VariableReference& v = e->as<VariableReference>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400372 this->writeU8(Rehydrator::kVariableReference_Command);
373 this->writeId(&v.fVariable);
374 this->writeU8(v.fRefKind);
375 break;
376 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400377 case Expression::Kind::kFunctionReference:
378 case Expression::Kind::kTypeReference:
379 case Expression::Kind::kDefined:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400380 // shouldn't appear in finished code
381 SkASSERT(false);
382 break;
383 }
384 } else {
385 this->writeU8(Rehydrator::kVoid_Command);
386 }
387}
388
389void Dehydrator::write(const Statement* s) {
390 if (s) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400391 switch (s->kind()) {
392 case Statement::Kind::kBlock: {
John Stiles26f98502020-08-18 09:30:51 -0400393 const Block& b = s->as<Block>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400394 this->writeU8(Rehydrator::kBlock_Command);
395 AutoDehydratorSymbolTable symbols(this, b.fSymbols);
396 this->writeU8(b.fStatements.size());
John Stilesf621e232020-08-25 13:33:02 -0400397 for (const std::unique_ptr<Statement>& blockStmt : b.fStatements) {
398 this->write(blockStmt.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400399 }
400 this->writeU8(b.fIsScope);
401 break;
402 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400403 case Statement::Kind::kBreak:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400404 this->writeU8(Rehydrator::kBreak_Command);
405 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400406 case Statement::Kind::kContinue:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400407 this->writeU8(Rehydrator::kContinue_Command);
408 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400409 case Statement::Kind::kDiscard:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400410 this->writeU8(Rehydrator::kDiscard_Command);
411 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400412 case Statement::Kind::kDo: {
John Stiles26f98502020-08-18 09:30:51 -0400413 const DoStatement& d = s->as<DoStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400414 this->writeU8(Rehydrator::kDo_Command);
415 this->write(d.fStatement.get());
416 this->write(d.fTest.get());
417 break;
418 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400419 case Statement::Kind::kExpression: {
John Stiles26f98502020-08-18 09:30:51 -0400420 const ExpressionStatement& e = s->as<ExpressionStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400421 this->writeU8(Rehydrator::kExpressionStatement_Command);
422 this->write(e.fExpression.get());
423 break;
424 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400425 case Statement::Kind::kFor: {
John Stiles26f98502020-08-18 09:30:51 -0400426 const ForStatement& f = s->as<ForStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400427 this->writeU8(Rehydrator::kFor_Command);
428 this->write(f.fInitializer.get());
429 this->write(f.fTest.get());
430 this->write(f.fNext.get());
431 this->write(f.fStatement.get());
432 this->write(f.fSymbols);
433 break;
434 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400435 case Statement::Kind::kIf: {
John Stiles26f98502020-08-18 09:30:51 -0400436 const IfStatement& i = s->as<IfStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400437 this->writeU8(Rehydrator::kIf_Command);
438 this->writeU8(i.fIsStatic);
439 this->write(i.fTest.get());
440 this->write(i.fIfTrue.get());
441 this->write(i.fIfFalse.get());
442 break;
443 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400444 case Statement::Kind::kNop:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400445 SkASSERT(false);
446 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400447 case Statement::Kind::kReturn: {
John Stiles26f98502020-08-18 09:30:51 -0400448 const ReturnStatement& r = s->as<ReturnStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400449 this->writeU8(Rehydrator::kReturn_Command);
450 this->write(r.fExpression.get());
451 break;
452 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400453 case Statement::Kind::kSwitch: {
John Stiles26f98502020-08-18 09:30:51 -0400454 const SwitchStatement& ss = s->as<SwitchStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400455 this->writeU8(Rehydrator::kSwitch_Command);
456 this->writeU8(ss.fIsStatic);
457 AutoDehydratorSymbolTable symbols(this, ss.fSymbols);
458 this->write(ss.fValue.get());
459 this->writeU8(ss.fCases.size());
John Stilesf621e232020-08-25 13:33:02 -0400460 for (const std::unique_ptr<SwitchCase>& sc : ss.fCases) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400461 this->write(sc->fValue.get());
462 this->writeU8(sc->fStatements.size());
John Stilesf621e232020-08-25 13:33:02 -0400463 for (const std::unique_ptr<Statement>& stmt : sc->fStatements) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400464 this->write(stmt.get());
465 }
466 }
467 break;
468 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400469 case Statement::Kind::kVarDeclaration: {
John Stiles26f98502020-08-18 09:30:51 -0400470 const VarDeclaration& v = s->as<VarDeclaration>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400471 this->writeU8(Rehydrator::kVarDeclaration_Command);
472 this->writeU16(this->symbolId(v.fVar));
473 this->writeU8(v.fSizes.size());
John Stilesf621e232020-08-25 13:33:02 -0400474 for (const std::unique_ptr<Expression>& sizeExpr : v.fSizes) {
475 this->write(sizeExpr.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400476 }
477 this->write(v.fValue.get());
478 break;
479 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400480 case Statement::Kind::kVarDeclarations: {
John Stiles26f98502020-08-18 09:30:51 -0400481 const VarDeclarationsStatement& v = s->as<VarDeclarationsStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400482 this->write(*v.fDeclaration);
483 break;
484 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400485 case Statement::Kind::kWhile: {
John Stiles26f98502020-08-18 09:30:51 -0400486 const WhileStatement& w = s->as<WhileStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400487 this->writeU8(Rehydrator::kWhile_Command);
488 this->write(w.fTest.get());
489 this->write(w.fStatement.get());
490 break;
491 }
492 }
493 } else {
494 this->writeU8(Rehydrator::kVoid_Command);
495 }
496}
497
498void Dehydrator::write(const ProgramElement& e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400499 switch (e.kind()) {
500 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -0400501 const Enum& en = e.as<Enum>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400502 this->writeU8(Rehydrator::kEnum_Command);
503 this->write(en.fTypeName);
504 AutoDehydratorSymbolTable symbols(this, en.fSymbols);
John Stilesf621e232020-08-25 13:33:02 -0400505 for (const std::unique_ptr<const Symbol>& s : en.fSymbols->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400506 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400507 Variable& v = (Variable&) *s;
John Stiles81365af2020-08-18 09:24:00 -0400508 SkASSERT(v.fInitialValue);
509 const IntLiteral& i = v.fInitialValue->as<IntLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400510 this->writeS32(i.fValue);
511 }
512 break;
513 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400514 case ProgramElement::Kind::kExtension:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400515 SkASSERT(false);
516 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400517 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -0400518 const FunctionDefinition& f = e.as<FunctionDefinition>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400519 this->writeU8(Rehydrator::kFunctionDefinition_Command);
520 this->writeU16(this->symbolId(&f.fDeclaration));
521 this->write(f.fBody.get());
522 this->writeU8(f.fReferencedIntrinsics.size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400523 std::set<uint16_t> ordered;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400524 for (const FunctionDeclaration* ref : f.fReferencedIntrinsics) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400525 ordered.insert(this->symbolId(ref));
526 }
527 for (uint16_t ref : ordered) {
528 this->writeU16(ref);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400529 }
530 break;
531 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400532 case ProgramElement::Kind::kInterfaceBlock: {
John Stiles3dc0da62020-08-19 17:48:31 -0400533 const InterfaceBlock& i = e.as<InterfaceBlock>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400534 this->writeU8(Rehydrator::kInterfaceBlock_Command);
535 this->write(i.fVariable);
536 this->write(i.fTypeName);
537 this->write(i.fInstanceName);
538 this->writeU8(i.fSizes.size());
539 for (const auto& s : i.fSizes) {
540 this->write(s.get());
541 }
542 break;
543 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400544 case ProgramElement::Kind::kModifiers:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400545 SkASSERT(false);
546 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400547 case ProgramElement::Kind::kSection:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400548 SkASSERT(false);
549 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400550 case ProgramElement::Kind::kVar: {
John Stiles3dc0da62020-08-19 17:48:31 -0400551 const VarDeclarations& v = e.as<VarDeclarations>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400552 this->writeU8(Rehydrator::kVarDeclarations_Command);
553 this->write(v.fBaseType);
554 this->writeU8(v.fVars.size());
John Stilesf621e232020-08-25 13:33:02 -0400555 for (const auto& var : v.fVars) {
556 this->write(var.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400557 }
558 break;
559 }
560 }
561}
562
563void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
564 this->writeU8(Rehydrator::kElements_Command);
565 this->writeU8(elements.size());
566 for (const auto& e : elements) {
567 this->write(*e);
568 }
569}
570
571void Dehydrator::finish(OutputStream& out) {
572 out.write16(fStringBuffer.str().size());
573 out.writeString(fStringBuffer.str());
574 out.writeString(fBody.str());
575}
576
577} // namespace
578
579#endif