blob: ec039d8fe8bdaabcfe3432d04e9a37667c1313e9 [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"
John Stiles98c1f822020-09-09 14:18:53 -040029#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040030#include "src/sksl/ir/SkSLIntLiteral.h"
31#include "src/sksl/ir/SkSLInterfaceBlock.h"
32#include "src/sksl/ir/SkSLNullLiteral.h"
33#include "src/sksl/ir/SkSLPostfixExpression.h"
34#include "src/sksl/ir/SkSLPrefixExpression.h"
35#include "src/sksl/ir/SkSLProgramElement.h"
36#include "src/sksl/ir/SkSLReturnStatement.h"
37#include "src/sksl/ir/SkSLSetting.h"
38#include "src/sksl/ir/SkSLStatement.h"
39#include "src/sksl/ir/SkSLSwitchCase.h"
40#include "src/sksl/ir/SkSLSwitchStatement.h"
41#include "src/sksl/ir/SkSLSwizzle.h"
42#include "src/sksl/ir/SkSLSymbol.h"
John Stiles49a547f2020-10-06 16:14:37 -040043#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040044#include "src/sksl/ir/SkSLSymbolTable.h"
45#include "src/sksl/ir/SkSLTernaryExpression.h"
46#include "src/sksl/ir/SkSLUnresolvedFunction.h"
47#include "src/sksl/ir/SkSLVarDeclarations.h"
48#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
49#include "src/sksl/ir/SkSLVariable.h"
50#include "src/sksl/ir/SkSLWhileStatement.h"
51
52#ifdef SKSL_STANDALONE
53
54namespace SkSL {
55
56static constexpr int HEADER_SIZE = 2;
57
58class AutoDehydratorSymbolTable {
59public:
60 AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
61 : fDehydrator(dehydrator) {
62 dehydrator->fSymbolMap.emplace_back();
63 if (symbols) {
64 dehydrator->write(*symbols);
65 } else {
66 dehydrator->writeU8(Rehydrator::kVoid_Command);
67 }
68 }
69
70 ~AutoDehydratorSymbolTable() {
71 fDehydrator->fSymbolMap.pop_back();
72 }
73
74private:
75 Dehydrator* fDehydrator;
76};
77
78void Dehydrator::write(Layout l) {
79 if (l == Layout()) {
80 this->writeU8(Rehydrator::kDefaultLayout_Command);
81 } else if (l == Layout::builtin(l.fBuiltin)) {
82 this->writeS8(Rehydrator::kBuiltinLayout_Command);
83 this->writeS16(l.fBuiltin);
84 } else {
85 this->writeS8(Rehydrator::kLayout_Command);
86 fBody.write32(l.fFlags);
87 this->writeS8(l.fLocation);
88 this->writeS8(l.fOffset);
89 this->writeS8(l.fBinding);
90 this->writeS8(l.fIndex);
91 this->writeS8(l.fSet);
92 this->writeS16(l.fBuiltin);
93 this->writeS8(l.fInputAttachmentIndex);
94 this->writeS8((int) l.fFormat);
95 this->writeS8(l.fPrimitive);
96 this->writeS8(l.fMaxVertices);
97 this->writeS8(l.fInvocations);
98 this->write(l.fMarker);
99 this->write(l.fWhen);
100 this->writeS8(l.fKey);
101 this->writeS8((int) l.fCType);
102 }
103}
104
105void Dehydrator::write(Modifiers m) {
106 if (m == Modifiers()) {
107 this->writeU8(Rehydrator::kDefaultModifiers_Command);
108 } else {
109 if (m.fFlags <= 255) {
110 this->writeU8(Rehydrator::kModifiers8Bit_Command);
111 this->write(m.fLayout);
112 this->writeU8(m.fFlags);
113 } else {
114 this->writeU8(Rehydrator::kModifiers_Command);
115 this->write(m.fLayout);
116 this->writeS32(m.fFlags);
117 }
118 }
119}
120
121void Dehydrator::write(StringFragment s) {
122 this->write(String(s));
123}
124
125void Dehydrator::write(String s) {
126 auto found = fStrings.find(s);
127 int offset;
128 if (found == fStrings.end()) {
129 offset = fStringBuffer.str().length() + HEADER_SIZE;
130 fStrings.insert({ s, offset });
131 SkASSERT(s.length() <= 255);
132 fStringBuffer.write8(s.length());
133 fStringBuffer.writeString(s);
134 } else {
135 offset = found->second;
136 }
137 this->writeU16(offset);
138}
139
140void Dehydrator::write(const Symbol& s) {
141 uint16_t id = this->symbolId(&s, false);
142 if (id) {
143 this->writeU8(Rehydrator::kSymbolRef_Command);
144 this->writeU16(id);
145 return;
146 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400147 switch (s.kind()) {
148 case Symbol::Kind::kFunctionDeclaration: {
John Stiles17c5b702020-08-18 10:40:03 -0400149 const FunctionDeclaration& f = s.as<FunctionDeclaration>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400150 this->writeU8(Rehydrator::kFunctionDeclaration_Command);
151 this->writeId(&f);
152 this->write(f.fModifiers);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400153 this->write(f.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400154 this->writeU8(f.fParameters.size());
155 for (const Variable* p : f.fParameters) {
156 this->writeU16(this->symbolId(p));
157 }
158 this->write(f.fReturnType);
159 break;
160 }
John Stiles49a547f2020-10-06 16:14:37 -0400161 case Symbol::Kind::kSymbolAlias: {
162 const SymbolAlias& alias = s.as<SymbolAlias>();
163 this->writeU8(Rehydrator::kSymbolAlias_Command);
164 this->writeId(&alias);
165 this->write(alias.name());
166 this->write(*alias.origSymbol());
167 break;
168 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400169 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400170 const UnresolvedFunction& f = s.as<UnresolvedFunction>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400171 this->writeU8(Rehydrator::kUnresolvedFunction_Command);
172 this->writeId(&f);
173 this->writeU8(f.fFunctions.size());
John Stilesf621e232020-08-25 13:33:02 -0400174 for (const FunctionDeclaration* funcDecl : f.fFunctions) {
175 this->write(*funcDecl);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400176 }
177 break;
178 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400179 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -0400180 const Type& t = s.as<Type>();
Ethan Nicholase6592142020-09-08 10:22:09 -0400181 switch (t.typeKind()) {
182 case Type::TypeKind::kArray:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400183 this->writeU8(Rehydrator::kArrayType_Command);
184 this->writeId(&t);
185 this->write(t.componentType());
Brian Osmane8c26082020-10-01 17:22:45 -0400186 this->writeS8(t.columns());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400187 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400188 case Type::TypeKind::kEnum:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400189 this->writeU8(Rehydrator::kEnumType_Command);
190 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400191 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400192 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400193 case Type::TypeKind::kNullable:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400194 this->writeU8(Rehydrator::kNullableType_Command);
195 this->writeId(&t);
196 this->write(t.componentType());
197 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400198 case Type::TypeKind::kStruct:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400199 this->writeU8(Rehydrator::kStructType_Command);
200 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400201 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400202 this->writeU8(t.fields().size());
203 for (const Type::Field& f : t.fields()) {
204 this->write(f.fModifiers);
205 this->write(f.fName);
206 this->write(*f.fType);
207 }
208 break;
209 default:
210 this->writeU8(Rehydrator::kSystemType_Command);
211 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400212 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400213 }
214 break;
215 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400216 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400217 const Variable& v = s.as<Variable>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400218 this->writeU8(Rehydrator::kVariable_Command);
219 this->writeId(&v);
220 this->write(v.fModifiers);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400221 this->write(v.name());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400222 this->write(v.type());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400223 this->writeU8(v.fStorage);
224 break;
225 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400226 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400227 const Field& f = s.as<Field>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400228 this->writeU8(Rehydrator::kField_Command);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400229 this->writeU16(this->symbolId(&f.owner()));
230 this->writeU8(f.fieldIndex());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400231 break;
232 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400233 case Symbol::Kind::kExternal:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400234 SkASSERT(false);
235 break;
236 }
237}
238
239void Dehydrator::write(const SymbolTable& symbols) {
240 this->writeU8(Rehydrator::kSymbolTable_Command);
241 this->writeU16(symbols.fOwnedSymbols.size());
242 for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
243 this->write(*s);
244 }
245 this->writeU16(symbols.fSymbols.size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400246 std::map<StringFragment, const Symbol*> ordered;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400247 for (std::pair<StringFragment, const Symbol*> p : symbols.fSymbols) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400248 ordered.insert(p);
249 }
250 for (std::pair<StringFragment, const Symbol*> p : ordered) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400251 this->write(p.first);
252 bool found = false;
253 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
254 if (symbols.fOwnedSymbols[i].get() == p.second) {
255 this->writeU16(i);
256 found = true;
257 break;
258 }
259 }
260 SkASSERT(found);
261 }
262}
263
264void Dehydrator::write(const Expression* e) {
265 if (e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400266 switch (e->kind()) {
267 case Expression::Kind::kBinary: {
John Stiles81365af2020-08-18 09:24:00 -0400268 const BinaryExpression& b = e->as<BinaryExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400269 this->writeU8(Rehydrator::kBinary_Command);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400270 this->write(&b.left());
271 this->writeU8((int) b.getOperator());
272 this->write(&b.right());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400273 this->write(b.type());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400274 break;
275 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400276 case Expression::Kind::kBoolLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400277 const BoolLiteral& b = e->as<BoolLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400278 this->writeU8(Rehydrator::kBoolLiteral_Command);
Ethan Nicholas59d660c2020-09-28 09:18:15 -0400279 this->writeU8(b.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400280 break;
281 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400282 case Expression::Kind::kConstructor: {
John Stiles81365af2020-08-18 09:24:00 -0400283 const Constructor& c = e->as<Constructor>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400284 this->writeU8(Rehydrator::kConstructor_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400285 this->write(c.type());
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400286 this->writeU8(c.arguments().size());
287 for (const auto& a : c.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400288 this->write(a.get());
289 }
290 break;
291 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400292 case Expression::Kind::kExternalFunctionCall:
293 case Expression::Kind::kExternalValue:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400294 // not implemented; doesn't seem like we'll ever need them from within an include
295 // file
296 SkASSERT(false);
297 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400298 case Expression::Kind::kFieldAccess: {
John Stiles81365af2020-08-18 09:24:00 -0400299 const FieldAccess& f = e->as<FieldAccess>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400300 this->writeU8(Rehydrator::kFieldAccess_Command);
301 this->write(f.fBase.get());
302 this->writeU8(f.fFieldIndex);
303 this->writeU8(f.fOwnerKind);
304 break;
305 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400306 case Expression::Kind::kFloatLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400307 const FloatLiteral& f = e->as<FloatLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400308 this->writeU8(Rehydrator::kFloatLiteral_Command);
309 FloatIntUnion u;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400310 u.fFloat = f.value();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400311 this->writeS32(u.fInt);
312 break;
313 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400314 case Expression::Kind::kFunctionCall: {
John Stiles81365af2020-08-18 09:24:00 -0400315 const FunctionCall& f = e->as<FunctionCall>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400316 this->writeU8(Rehydrator::kFunctionCall_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400317 this->write(f.type());
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400318 this->writeId(&f.function());
319 this->writeU8(f.arguments().size());
320 for (const auto& a : f.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400321 this->write(a.get());
322 }
323 break;
324 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400325 case Expression::Kind::kIndex: {
John Stiles81365af2020-08-18 09:24:00 -0400326 const IndexExpression& i = e->as<IndexExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400327 this->writeU8(Rehydrator::kIndex_Command);
328 this->write(i.fBase.get());
329 this->write(i.fIndex.get());
330 break;
331 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400332 case Expression::Kind::kIntLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400333 const IntLiteral& i = e->as<IntLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400334 this->writeU8(Rehydrator::kIntLiteral_Command);
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400335 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400336 break;
337 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400338 case Expression::Kind::kNullLiteral:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400339 this->writeU8(Rehydrator::kNullLiteral_Command);
340 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400341 case Expression::Kind::kPostfix: {
John Stiles81365af2020-08-18 09:24:00 -0400342 const PostfixExpression& p = e->as<PostfixExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400343 this->writeU8(Rehydrator::kPostfix_Command);
344 this->writeU8((int) p.fOperator);
345 this->write(p.fOperand.get());
346 break;
347 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400348 case Expression::Kind::kPrefix: {
John Stiles81365af2020-08-18 09:24:00 -0400349 const PrefixExpression& p = e->as<PrefixExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400350 this->writeU8(Rehydrator::kPrefix_Command);
351 this->writeU8((int) p.fOperator);
352 this->write(p.fOperand.get());
353 break;
354 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400355 case Expression::Kind::kSetting: {
John Stiles81365af2020-08-18 09:24:00 -0400356 const Setting& s = e->as<Setting>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400357 this->writeU8(Rehydrator::kSetting_Command);
358 this->write(s.fName);
359 this->write(s.fValue.get());
360 break;
361 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400362 case Expression::Kind::kSwizzle: {
John Stiles81365af2020-08-18 09:24:00 -0400363 const Swizzle& s = e->as<Swizzle>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400364 this->writeU8(Rehydrator::kSwizzle_Command);
365 this->write(s.fBase.get());
366 this->writeU8(s.fComponents.size());
367 for (int c : s.fComponents) {
368 this->writeU8(c);
369 }
370 break;
371 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400372 case Expression::Kind::kTernary: {
John Stiles81365af2020-08-18 09:24:00 -0400373 const TernaryExpression& t = e->as<TernaryExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400374 this->writeU8(Rehydrator::kTernary_Command);
375 this->write(t.fTest.get());
376 this->write(t.fIfTrue.get());
377 this->write(t.fIfFalse.get());
378 break;
379 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400380 case Expression::Kind::kVariableReference: {
John Stiles81365af2020-08-18 09:24:00 -0400381 const VariableReference& v = e->as<VariableReference>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400382 this->writeU8(Rehydrator::kVariableReference_Command);
Brian Osman79457ef2020-09-24 15:01:27 -0400383 this->writeId(v.fVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400384 this->writeU8(v.fRefKind);
385 break;
386 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400387 case Expression::Kind::kFunctionReference:
388 case Expression::Kind::kTypeReference:
389 case Expression::Kind::kDefined:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400390 // shouldn't appear in finished code
391 SkASSERT(false);
392 break;
393 }
394 } else {
395 this->writeU8(Rehydrator::kVoid_Command);
396 }
397}
398
399void Dehydrator::write(const Statement* s) {
400 if (s) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400401 switch (s->kind()) {
402 case Statement::Kind::kBlock: {
John Stiles26f98502020-08-18 09:30:51 -0400403 const Block& b = s->as<Block>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400404 this->writeU8(Rehydrator::kBlock_Command);
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400405 AutoDehydratorSymbolTable symbols(this, b.symbolTable());
406 this->writeU8(b.children().size());
407 for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
John Stilesf621e232020-08-25 13:33:02 -0400408 this->write(blockStmt.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400409 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400410 this->writeU8(b.isScope());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400411 break;
412 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400413 case Statement::Kind::kBreak:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400414 this->writeU8(Rehydrator::kBreak_Command);
415 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400416 case Statement::Kind::kContinue:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400417 this->writeU8(Rehydrator::kContinue_Command);
418 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400419 case Statement::Kind::kDiscard:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400420 this->writeU8(Rehydrator::kDiscard_Command);
421 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400422 case Statement::Kind::kDo: {
John Stiles26f98502020-08-18 09:30:51 -0400423 const DoStatement& d = s->as<DoStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400424 this->writeU8(Rehydrator::kDo_Command);
Ethan Nicholas1fd61162020-09-28 13:14:19 -0400425 this->write(d.statement().get());
426 this->write(d.test().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400427 break;
428 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400429 case Statement::Kind::kExpression: {
John Stiles26f98502020-08-18 09:30:51 -0400430 const ExpressionStatement& e = s->as<ExpressionStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400431 this->writeU8(Rehydrator::kExpressionStatement_Command);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400432 this->write(e.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400433 break;
434 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400435 case Statement::Kind::kFor: {
John Stiles26f98502020-08-18 09:30:51 -0400436 const ForStatement& f = s->as<ForStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400437 this->writeU8(Rehydrator::kFor_Command);
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400438 this->write(f.initializer().get());
439 this->write(f.test().get());
440 this->write(f.next().get());
441 this->write(f.statement().get());
442 this->write(f.symbols());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400443 break;
444 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400445 case Statement::Kind::kIf: {
John Stiles26f98502020-08-18 09:30:51 -0400446 const IfStatement& i = s->as<IfStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400447 this->writeU8(Rehydrator::kIf_Command);
448 this->writeU8(i.fIsStatic);
449 this->write(i.fTest.get());
450 this->write(i.fIfTrue.get());
451 this->write(i.fIfFalse.get());
452 break;
453 }
John Stiles98c1f822020-09-09 14:18:53 -0400454 case Statement::Kind::kInlineMarker: {
455 const InlineMarker& i = s->as<InlineMarker>();
456 this->writeU8(Rehydrator::kInlineMarker_Command);
457 this->writeId(i.fFuncDecl);
458 break;
459 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400460 case Statement::Kind::kNop:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400461 SkASSERT(false);
462 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400463 case Statement::Kind::kReturn: {
John Stiles26f98502020-08-18 09:30:51 -0400464 const ReturnStatement& r = s->as<ReturnStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400465 this->writeU8(Rehydrator::kReturn_Command);
466 this->write(r.fExpression.get());
467 break;
468 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400469 case Statement::Kind::kSwitch: {
John Stiles26f98502020-08-18 09:30:51 -0400470 const SwitchStatement& ss = s->as<SwitchStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400471 this->writeU8(Rehydrator::kSwitch_Command);
472 this->writeU8(ss.fIsStatic);
473 AutoDehydratorSymbolTable symbols(this, ss.fSymbols);
474 this->write(ss.fValue.get());
475 this->writeU8(ss.fCases.size());
John Stilesf621e232020-08-25 13:33:02 -0400476 for (const std::unique_ptr<SwitchCase>& sc : ss.fCases) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400477 this->write(sc->fValue.get());
478 this->writeU8(sc->fStatements.size());
John Stilesf621e232020-08-25 13:33:02 -0400479 for (const std::unique_ptr<Statement>& stmt : sc->fStatements) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400480 this->write(stmt.get());
481 }
482 }
483 break;
484 }
Brian Osman9eb848a2020-09-11 15:53:40 -0400485 case Statement::Kind::kSwitchCase:
486 SkASSERT(false);
487 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400488 case Statement::Kind::kVarDeclaration: {
John Stiles26f98502020-08-18 09:30:51 -0400489 const VarDeclaration& v = s->as<VarDeclaration>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400490 this->writeU8(Rehydrator::kVarDeclaration_Command);
491 this->writeU16(this->symbolId(v.fVar));
492 this->writeU8(v.fSizes.size());
John Stilesf621e232020-08-25 13:33:02 -0400493 for (const std::unique_ptr<Expression>& sizeExpr : v.fSizes) {
494 this->write(sizeExpr.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400495 }
496 this->write(v.fValue.get());
497 break;
498 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400499 case Statement::Kind::kVarDeclarations: {
John Stiles26f98502020-08-18 09:30:51 -0400500 const VarDeclarationsStatement& v = s->as<VarDeclarationsStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400501 this->write(*v.fDeclaration);
502 break;
503 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400504 case Statement::Kind::kWhile: {
John Stiles26f98502020-08-18 09:30:51 -0400505 const WhileStatement& w = s->as<WhileStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400506 this->writeU8(Rehydrator::kWhile_Command);
507 this->write(w.fTest.get());
508 this->write(w.fStatement.get());
509 break;
510 }
511 }
512 } else {
513 this->writeU8(Rehydrator::kVoid_Command);
514 }
515}
516
517void Dehydrator::write(const ProgramElement& e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400518 switch (e.kind()) {
519 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -0400520 const Enum& en = e.as<Enum>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400521 this->writeU8(Rehydrator::kEnum_Command);
Ethan Nicholasd83ded82020-09-29 17:05:54 -0400522 this->write(en.typeName());
523 AutoDehydratorSymbolTable symbols(this, en.symbols());
524 for (const std::unique_ptr<const Symbol>& s : en.symbols()->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400525 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400526 Variable& v = (Variable&) *s;
John Stiles81365af2020-08-18 09:24:00 -0400527 SkASSERT(v.fInitialValue);
528 const IntLiteral& i = v.fInitialValue->as<IntLiteral>();
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400529 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400530 }
531 break;
532 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400533 case ProgramElement::Kind::kExtension:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400534 SkASSERT(false);
535 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400536 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -0400537 const FunctionDefinition& f = e.as<FunctionDefinition>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400538 this->writeU8(Rehydrator::kFunctionDefinition_Command);
539 this->writeU16(this->symbolId(&f.fDeclaration));
540 this->write(f.fBody.get());
541 this->writeU8(f.fReferencedIntrinsics.size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400542 std::set<uint16_t> ordered;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400543 for (const FunctionDeclaration* ref : f.fReferencedIntrinsics) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400544 ordered.insert(this->symbolId(ref));
545 }
546 for (uint16_t ref : ordered) {
547 this->writeU16(ref);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400548 }
549 break;
550 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400551 case ProgramElement::Kind::kInterfaceBlock: {
John Stiles3dc0da62020-08-19 17:48:31 -0400552 const InterfaceBlock& i = e.as<InterfaceBlock>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400553 this->writeU8(Rehydrator::kInterfaceBlock_Command);
554 this->write(i.fVariable);
555 this->write(i.fTypeName);
556 this->write(i.fInstanceName);
557 this->writeU8(i.fSizes.size());
558 for (const auto& s : i.fSizes) {
559 this->write(s.get());
560 }
561 break;
562 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400563 case ProgramElement::Kind::kModifiers:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400564 SkASSERT(false);
565 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400566 case ProgramElement::Kind::kSection:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400567 SkASSERT(false);
568 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400569 case ProgramElement::Kind::kVar: {
John Stiles3dc0da62020-08-19 17:48:31 -0400570 const VarDeclarations& v = e.as<VarDeclarations>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400571 this->writeU8(Rehydrator::kVarDeclarations_Command);
572 this->write(v.fBaseType);
573 this->writeU8(v.fVars.size());
John Stilesf621e232020-08-25 13:33:02 -0400574 for (const auto& var : v.fVars) {
575 this->write(var.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400576 }
577 break;
578 }
579 }
580}
581
582void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
583 this->writeU8(Rehydrator::kElements_Command);
584 this->writeU8(elements.size());
585 for (const auto& e : elements) {
586 this->write(*e);
587 }
588}
589
590void Dehydrator::finish(OutputStream& out) {
591 out.write16(fStringBuffer.str().size());
592 out.writeString(fStringBuffer.str());
593 out.writeString(fBody.str());
594}
595
596} // namespace
597
598#endif