blob: 6c59409ed84f4aa3faf567080358219750b9b576 [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"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040048#include "src/sksl/ir/SkSLVariable.h"
49#include "src/sksl/ir/SkSLWhileStatement.h"
50
51#ifdef SKSL_STANDALONE
52
53namespace SkSL {
54
55static constexpr int HEADER_SIZE = 2;
56
57class AutoDehydratorSymbolTable {
58public:
59 AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
60 : fDehydrator(dehydrator) {
61 dehydrator->fSymbolMap.emplace_back();
62 if (symbols) {
63 dehydrator->write(*symbols);
64 } else {
65 dehydrator->writeU8(Rehydrator::kVoid_Command);
66 }
67 }
68
69 ~AutoDehydratorSymbolTable() {
70 fDehydrator->fSymbolMap.pop_back();
71 }
72
73private:
74 Dehydrator* fDehydrator;
75};
76
77void Dehydrator::write(Layout l) {
78 if (l == Layout()) {
79 this->writeU8(Rehydrator::kDefaultLayout_Command);
80 } else if (l == Layout::builtin(l.fBuiltin)) {
81 this->writeS8(Rehydrator::kBuiltinLayout_Command);
82 this->writeS16(l.fBuiltin);
83 } else {
84 this->writeS8(Rehydrator::kLayout_Command);
85 fBody.write32(l.fFlags);
86 this->writeS8(l.fLocation);
87 this->writeS8(l.fOffset);
88 this->writeS8(l.fBinding);
89 this->writeS8(l.fIndex);
90 this->writeS8(l.fSet);
91 this->writeS16(l.fBuiltin);
92 this->writeS8(l.fInputAttachmentIndex);
93 this->writeS8((int) l.fFormat);
94 this->writeS8(l.fPrimitive);
95 this->writeS8(l.fMaxVertices);
96 this->writeS8(l.fInvocations);
97 this->write(l.fMarker);
98 this->write(l.fWhen);
99 this->writeS8(l.fKey);
100 this->writeS8((int) l.fCType);
101 }
102}
103
104void Dehydrator::write(Modifiers m) {
105 if (m == Modifiers()) {
106 this->writeU8(Rehydrator::kDefaultModifiers_Command);
107 } else {
108 if (m.fFlags <= 255) {
109 this->writeU8(Rehydrator::kModifiers8Bit_Command);
110 this->write(m.fLayout);
111 this->writeU8(m.fFlags);
112 } else {
113 this->writeU8(Rehydrator::kModifiers_Command);
114 this->write(m.fLayout);
115 this->writeS32(m.fFlags);
116 }
117 }
118}
119
120void Dehydrator::write(StringFragment s) {
121 this->write(String(s));
122}
123
124void Dehydrator::write(String s) {
125 auto found = fStrings.find(s);
126 int offset;
127 if (found == fStrings.end()) {
128 offset = fStringBuffer.str().length() + HEADER_SIZE;
129 fStrings.insert({ s, offset });
130 SkASSERT(s.length() <= 255);
131 fStringBuffer.write8(s.length());
132 fStringBuffer.writeString(s);
133 } else {
134 offset = found->second;
135 }
136 this->writeU16(offset);
137}
138
139void Dehydrator::write(const Symbol& s) {
140 uint16_t id = this->symbolId(&s, false);
141 if (id) {
142 this->writeU8(Rehydrator::kSymbolRef_Command);
143 this->writeU16(id);
144 return;
145 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400146 switch (s.kind()) {
147 case Symbol::Kind::kFunctionDeclaration: {
John Stiles17c5b702020-08-18 10:40:03 -0400148 const FunctionDeclaration& f = s.as<FunctionDeclaration>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400149 this->writeU8(Rehydrator::kFunctionDeclaration_Command);
150 this->writeId(&f);
151 this->write(f.fModifiers);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400152 this->write(f.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400153 this->writeU8(f.fParameters.size());
154 for (const Variable* p : f.fParameters) {
155 this->writeU16(this->symbolId(p));
156 }
157 this->write(f.fReturnType);
158 break;
159 }
John Stiles49a547f2020-10-06 16:14:37 -0400160 case Symbol::Kind::kSymbolAlias: {
161 const SymbolAlias& alias = s.as<SymbolAlias>();
162 this->writeU8(Rehydrator::kSymbolAlias_Command);
163 this->writeId(&alias);
164 this->write(alias.name());
165 this->write(*alias.origSymbol());
166 break;
167 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400168 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400169 const UnresolvedFunction& f = s.as<UnresolvedFunction>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400170 this->writeU8(Rehydrator::kUnresolvedFunction_Command);
171 this->writeId(&f);
172 this->writeU8(f.fFunctions.size());
John Stilesf621e232020-08-25 13:33:02 -0400173 for (const FunctionDeclaration* funcDecl : f.fFunctions) {
174 this->write(*funcDecl);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400175 }
176 break;
177 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400178 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -0400179 const Type& t = s.as<Type>();
Ethan Nicholase6592142020-09-08 10:22:09 -0400180 switch (t.typeKind()) {
181 case Type::TypeKind::kArray:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400182 this->writeU8(Rehydrator::kArrayType_Command);
183 this->writeId(&t);
184 this->write(t.componentType());
Brian Osmane8c26082020-10-01 17:22:45 -0400185 this->writeS8(t.columns());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400186 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400187 case Type::TypeKind::kEnum:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400188 this->writeU8(Rehydrator::kEnumType_Command);
189 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400190 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400191 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400192 case Type::TypeKind::kNullable:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400193 this->writeU8(Rehydrator::kNullableType_Command);
194 this->writeId(&t);
195 this->write(t.componentType());
196 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400197 case Type::TypeKind::kStruct:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400198 this->writeU8(Rehydrator::kStructType_Command);
199 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400200 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400201 this->writeU8(t.fields().size());
202 for (const Type::Field& f : t.fields()) {
203 this->write(f.fModifiers);
204 this->write(f.fName);
205 this->write(*f.fType);
206 }
207 break;
208 default:
209 this->writeU8(Rehydrator::kSystemType_Command);
210 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400211 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400212 }
213 break;
214 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400215 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400216 const Variable& v = s.as<Variable>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400217 this->writeU8(Rehydrator::kVariable_Command);
218 this->writeId(&v);
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400219 this->write(v.modifiers());
Ethan Nicholase2c49992020-10-05 11:49:11 -0400220 this->write(v.name());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400221 this->write(v.type());
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400222 this->writeU8(v.storage());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400223 break;
224 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400225 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400226 const Field& f = s.as<Field>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400227 this->writeU8(Rehydrator::kField_Command);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400228 this->writeU16(this->symbolId(&f.owner()));
229 this->writeU8(f.fieldIndex());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400230 break;
231 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400232 case Symbol::Kind::kExternal:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400233 SkASSERT(false);
234 break;
235 }
236}
237
238void Dehydrator::write(const SymbolTable& symbols) {
239 this->writeU8(Rehydrator::kSymbolTable_Command);
240 this->writeU16(symbols.fOwnedSymbols.size());
241 for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
242 this->write(*s);
243 }
244 this->writeU16(symbols.fSymbols.size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400245 std::map<StringFragment, const Symbol*> ordered;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400246 for (std::pair<StringFragment, const Symbol*> p : symbols.fSymbols) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400247 ordered.insert(p);
248 }
249 for (std::pair<StringFragment, const Symbol*> p : ordered) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400250 bool found = false;
251 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
252 if (symbols.fOwnedSymbols[i].get() == p.second) {
253 this->writeU16(i);
254 found = true;
255 break;
256 }
257 }
258 SkASSERT(found);
259 }
260}
261
262void Dehydrator::write(const Expression* e) {
263 if (e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400264 switch (e->kind()) {
265 case Expression::Kind::kBinary: {
John Stiles81365af2020-08-18 09:24:00 -0400266 const BinaryExpression& b = e->as<BinaryExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400267 this->writeU8(Rehydrator::kBinary_Command);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400268 this->write(&b.left());
269 this->writeU8((int) b.getOperator());
270 this->write(&b.right());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400271 this->write(b.type());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400272 break;
273 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400274 case Expression::Kind::kBoolLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400275 const BoolLiteral& b = e->as<BoolLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400276 this->writeU8(Rehydrator::kBoolLiteral_Command);
Ethan Nicholas59d660c2020-09-28 09:18:15 -0400277 this->writeU8(b.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400278 break;
279 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400280 case Expression::Kind::kConstructor: {
John Stiles81365af2020-08-18 09:24:00 -0400281 const Constructor& c = e->as<Constructor>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400282 this->writeU8(Rehydrator::kConstructor_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400283 this->write(c.type());
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400284 this->writeU8(c.arguments().size());
285 for (const auto& a : c.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400286 this->write(a.get());
287 }
288 break;
289 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400290 case Expression::Kind::kExternalFunctionCall:
291 case Expression::Kind::kExternalValue:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400292 // not implemented; doesn't seem like we'll ever need them from within an include
293 // file
294 SkASSERT(false);
295 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400296 case Expression::Kind::kFieldAccess: {
John Stiles81365af2020-08-18 09:24:00 -0400297 const FieldAccess& f = e->as<FieldAccess>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400298 this->writeU8(Rehydrator::kFieldAccess_Command);
299 this->write(f.fBase.get());
300 this->writeU8(f.fFieldIndex);
301 this->writeU8(f.fOwnerKind);
302 break;
303 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400304 case Expression::Kind::kFloatLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400305 const FloatLiteral& f = e->as<FloatLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400306 this->writeU8(Rehydrator::kFloatLiteral_Command);
307 FloatIntUnion u;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400308 u.fFloat = f.value();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400309 this->writeS32(u.fInt);
310 break;
311 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400312 case Expression::Kind::kFunctionCall: {
John Stiles81365af2020-08-18 09:24:00 -0400313 const FunctionCall& f = e->as<FunctionCall>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400314 this->writeU8(Rehydrator::kFunctionCall_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400315 this->write(f.type());
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400316 this->writeId(&f.function());
317 this->writeU8(f.arguments().size());
318 for (const auto& a : f.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400319 this->write(a.get());
320 }
321 break;
322 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400323 case Expression::Kind::kIndex: {
John Stiles81365af2020-08-18 09:24:00 -0400324 const IndexExpression& i = e->as<IndexExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400325 this->writeU8(Rehydrator::kIndex_Command);
326 this->write(i.fBase.get());
327 this->write(i.fIndex.get());
328 break;
329 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400330 case Expression::Kind::kIntLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400331 const IntLiteral& i = e->as<IntLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400332 this->writeU8(Rehydrator::kIntLiteral_Command);
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400333 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400334 break;
335 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400336 case Expression::Kind::kNullLiteral:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400337 this->writeU8(Rehydrator::kNullLiteral_Command);
338 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400339 case Expression::Kind::kPostfix: {
John Stiles81365af2020-08-18 09:24:00 -0400340 const PostfixExpression& p = e->as<PostfixExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400341 this->writeU8(Rehydrator::kPostfix_Command);
342 this->writeU8((int) p.fOperator);
343 this->write(p.fOperand.get());
344 break;
345 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400346 case Expression::Kind::kPrefix: {
John Stiles81365af2020-08-18 09:24:00 -0400347 const PrefixExpression& p = e->as<PrefixExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400348 this->writeU8(Rehydrator::kPrefix_Command);
349 this->writeU8((int) p.fOperator);
350 this->write(p.fOperand.get());
351 break;
352 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400353 case Expression::Kind::kSetting: {
John Stiles81365af2020-08-18 09:24:00 -0400354 const Setting& s = e->as<Setting>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400355 this->writeU8(Rehydrator::kSetting_Command);
356 this->write(s.fName);
357 this->write(s.fValue.get());
358 break;
359 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400360 case Expression::Kind::kSwizzle: {
John Stiles81365af2020-08-18 09:24:00 -0400361 const Swizzle& s = e->as<Swizzle>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400362 this->writeU8(Rehydrator::kSwizzle_Command);
363 this->write(s.fBase.get());
364 this->writeU8(s.fComponents.size());
365 for (int c : s.fComponents) {
366 this->writeU8(c);
367 }
368 break;
369 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400370 case Expression::Kind::kTernary: {
John Stiles81365af2020-08-18 09:24:00 -0400371 const TernaryExpression& t = e->as<TernaryExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400372 this->writeU8(Rehydrator::kTernary_Command);
Ethan Nicholasdd218162020-10-08 05:48:01 -0400373 this->write(t.test().get());
374 this->write(t.ifTrue().get());
375 this->write(t.ifFalse().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400376 break;
377 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400378 case Expression::Kind::kVariableReference: {
John Stiles81365af2020-08-18 09:24:00 -0400379 const VariableReference& v = e->as<VariableReference>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400380 this->writeU8(Rehydrator::kVariableReference_Command);
Ethan Nicholas78686922020-10-08 06:46:27 -0400381 this->writeId(v.variable());
382 this->writeU8(v.refKind());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400383 break;
384 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400385 case Expression::Kind::kFunctionReference:
386 case Expression::Kind::kTypeReference:
387 case Expression::Kind::kDefined:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400388 // shouldn't appear in finished code
389 SkASSERT(false);
390 break;
391 }
392 } else {
393 this->writeU8(Rehydrator::kVoid_Command);
394 }
395}
396
397void Dehydrator::write(const Statement* s) {
398 if (s) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400399 switch (s->kind()) {
400 case Statement::Kind::kBlock: {
John Stiles26f98502020-08-18 09:30:51 -0400401 const Block& b = s->as<Block>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400402 this->writeU8(Rehydrator::kBlock_Command);
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400403 AutoDehydratorSymbolTable symbols(this, b.symbolTable());
404 this->writeU8(b.children().size());
405 for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
John Stilesf621e232020-08-25 13:33:02 -0400406 this->write(blockStmt.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400407 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400408 this->writeU8(b.isScope());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400409 break;
410 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400411 case Statement::Kind::kBreak:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400412 this->writeU8(Rehydrator::kBreak_Command);
413 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400414 case Statement::Kind::kContinue:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400415 this->writeU8(Rehydrator::kContinue_Command);
416 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400417 case Statement::Kind::kDiscard:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400418 this->writeU8(Rehydrator::kDiscard_Command);
419 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400420 case Statement::Kind::kDo: {
John Stiles26f98502020-08-18 09:30:51 -0400421 const DoStatement& d = s->as<DoStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400422 this->writeU8(Rehydrator::kDo_Command);
Ethan Nicholas1fd61162020-09-28 13:14:19 -0400423 this->write(d.statement().get());
424 this->write(d.test().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400425 break;
426 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400427 case Statement::Kind::kExpression: {
John Stiles26f98502020-08-18 09:30:51 -0400428 const ExpressionStatement& e = s->as<ExpressionStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400429 this->writeU8(Rehydrator::kExpressionStatement_Command);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400430 this->write(e.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400431 break;
432 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400433 case Statement::Kind::kFor: {
John Stiles26f98502020-08-18 09:30:51 -0400434 const ForStatement& f = s->as<ForStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400435 this->writeU8(Rehydrator::kFor_Command);
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400436 this->write(f.initializer().get());
437 this->write(f.test().get());
438 this->write(f.next().get());
439 this->write(f.statement().get());
440 this->write(f.symbols());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400441 break;
442 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400443 case Statement::Kind::kIf: {
John Stiles26f98502020-08-18 09:30:51 -0400444 const IfStatement& i = s->as<IfStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400445 this->writeU8(Rehydrator::kIf_Command);
Ethan Nicholas8c44eca2020-10-07 16:47:09 -0400446 this->writeU8(i.isStatic());
447 this->write(i.test().get());
448 this->write(i.ifTrue().get());
449 this->write(i.ifFalse().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400450 break;
451 }
John Stiles98c1f822020-09-09 14:18:53 -0400452 case Statement::Kind::kInlineMarker: {
453 const InlineMarker& i = s->as<InlineMarker>();
454 this->writeU8(Rehydrator::kInlineMarker_Command);
455 this->writeId(i.fFuncDecl);
456 break;
457 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400458 case Statement::Kind::kNop:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400459 SkASSERT(false);
460 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400461 case Statement::Kind::kReturn: {
John Stiles26f98502020-08-18 09:30:51 -0400462 const ReturnStatement& r = s->as<ReturnStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400463 this->writeU8(Rehydrator::kReturn_Command);
464 this->write(r.fExpression.get());
465 break;
466 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400467 case Statement::Kind::kSwitch: {
John Stiles26f98502020-08-18 09:30:51 -0400468 const SwitchStatement& ss = s->as<SwitchStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400469 this->writeU8(Rehydrator::kSwitch_Command);
470 this->writeU8(ss.fIsStatic);
471 AutoDehydratorSymbolTable symbols(this, ss.fSymbols);
472 this->write(ss.fValue.get());
473 this->writeU8(ss.fCases.size());
John Stilesf621e232020-08-25 13:33:02 -0400474 for (const std::unique_ptr<SwitchCase>& sc : ss.fCases) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400475 this->write(sc->fValue.get());
476 this->writeU8(sc->fStatements.size());
John Stilesf621e232020-08-25 13:33:02 -0400477 for (const std::unique_ptr<Statement>& stmt : sc->fStatements) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400478 this->write(stmt.get());
479 }
480 }
481 break;
482 }
Brian Osman9eb848a2020-09-11 15:53:40 -0400483 case Statement::Kind::kSwitchCase:
484 SkASSERT(false);
485 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400486 case Statement::Kind::kVarDeclaration: {
John Stiles26f98502020-08-18 09:30:51 -0400487 const VarDeclaration& v = s->as<VarDeclaration>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400488 this->writeU8(Rehydrator::kVarDeclaration_Command);
489 this->writeU16(this->symbolId(v.fVar));
Brian Osmanc0213602020-10-06 14:43:32 -0400490 this->write(v.fBaseType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400491 this->writeU8(v.fSizes.size());
John Stilesf621e232020-08-25 13:33:02 -0400492 for (const std::unique_ptr<Expression>& sizeExpr : v.fSizes) {
493 this->write(sizeExpr.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400494 }
495 this->write(v.fValue.get());
496 break;
497 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400498 case Statement::Kind::kWhile: {
John Stiles26f98502020-08-18 09:30:51 -0400499 const WhileStatement& w = s->as<WhileStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400500 this->writeU8(Rehydrator::kWhile_Command);
501 this->write(w.fTest.get());
502 this->write(w.fStatement.get());
503 break;
504 }
505 }
506 } else {
507 this->writeU8(Rehydrator::kVoid_Command);
508 }
509}
510
511void Dehydrator::write(const ProgramElement& e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400512 switch (e.kind()) {
513 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -0400514 const Enum& en = e.as<Enum>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400515 this->writeU8(Rehydrator::kEnum_Command);
Ethan Nicholasd83ded82020-09-29 17:05:54 -0400516 this->write(en.typeName());
517 AutoDehydratorSymbolTable symbols(this, en.symbols());
518 for (const std::unique_ptr<const Symbol>& s : en.symbols()->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400519 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400520 Variable& v = (Variable&) *s;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400521 SkASSERT(v.initialValue());
522 const IntLiteral& i = v.initialValue()->as<IntLiteral>();
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400523 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400524 }
525 break;
526 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400527 case ProgramElement::Kind::kExtension:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400528 SkASSERT(false);
529 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400530 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -0400531 const FunctionDefinition& f = e.as<FunctionDefinition>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400532 this->writeU8(Rehydrator::kFunctionDefinition_Command);
533 this->writeU16(this->symbolId(&f.fDeclaration));
534 this->write(f.fBody.get());
535 this->writeU8(f.fReferencedIntrinsics.size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400536 std::set<uint16_t> ordered;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400537 for (const FunctionDeclaration* ref : f.fReferencedIntrinsics) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400538 ordered.insert(this->symbolId(ref));
539 }
540 for (uint16_t ref : ordered) {
541 this->writeU16(ref);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400542 }
543 break;
544 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400545 case ProgramElement::Kind::kInterfaceBlock: {
John Stiles3dc0da62020-08-19 17:48:31 -0400546 const InterfaceBlock& i = e.as<InterfaceBlock>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400547 this->writeU8(Rehydrator::kInterfaceBlock_Command);
Brian Osman6a204db2020-10-08 09:29:02 -0400548 this->write(*i.fVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400549 this->write(i.fTypeName);
550 this->write(i.fInstanceName);
551 this->writeU8(i.fSizes.size());
552 for (const auto& s : i.fSizes) {
553 this->write(s.get());
554 }
555 break;
556 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400557 case ProgramElement::Kind::kModifiers:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400558 SkASSERT(false);
559 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400560 case ProgramElement::Kind::kSection:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400561 SkASSERT(false);
562 break;
Brian Osmanc0213602020-10-06 14:43:32 -0400563 case ProgramElement::Kind::kGlobalVar: {
564 const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400565 this->writeU8(Rehydrator::kVarDeclarations_Command);
Brian Osmanc0213602020-10-06 14:43:32 -0400566 this->write(v.fDecl.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400567 break;
568 }
569 }
570}
571
572void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
573 this->writeU8(Rehydrator::kElements_Command);
574 this->writeU8(elements.size());
575 for (const auto& e : elements) {
576 this->write(*e);
577 }
578}
579
580void Dehydrator::finish(OutputStream& out) {
581 out.write16(fStringBuffer.str().size());
582 out.writeString(fStringBuffer.str());
583 out.writeString(fBody.str());
584}
585
586} // namespace
587
588#endif