blob: b67bf654911d21a3814323116735dbeff543d459 [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 this->write(p.first);
251 bool found = false;
252 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
253 if (symbols.fOwnedSymbols[i].get() == p.second) {
254 this->writeU16(i);
255 found = true;
256 break;
257 }
258 }
259 SkASSERT(found);
260 }
261}
262
263void Dehydrator::write(const Expression* e) {
264 if (e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400265 switch (e->kind()) {
266 case Expression::Kind::kBinary: {
John Stiles81365af2020-08-18 09:24:00 -0400267 const BinaryExpression& b = e->as<BinaryExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400268 this->writeU8(Rehydrator::kBinary_Command);
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400269 this->write(&b.left());
270 this->writeU8((int) b.getOperator());
271 this->write(&b.right());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400272 this->write(b.type());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400273 break;
274 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400275 case Expression::Kind::kBoolLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400276 const BoolLiteral& b = e->as<BoolLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400277 this->writeU8(Rehydrator::kBoolLiteral_Command);
Ethan Nicholas59d660c2020-09-28 09:18:15 -0400278 this->writeU8(b.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400279 break;
280 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400281 case Expression::Kind::kConstructor: {
John Stiles81365af2020-08-18 09:24:00 -0400282 const Constructor& c = e->as<Constructor>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400283 this->writeU8(Rehydrator::kConstructor_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400284 this->write(c.type());
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400285 this->writeU8(c.arguments().size());
286 for (const auto& a : c.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400287 this->write(a.get());
288 }
289 break;
290 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400291 case Expression::Kind::kExternalFunctionCall:
292 case Expression::Kind::kExternalValue:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400293 // not implemented; doesn't seem like we'll ever need them from within an include
294 // file
295 SkASSERT(false);
296 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400297 case Expression::Kind::kFieldAccess: {
John Stiles81365af2020-08-18 09:24:00 -0400298 const FieldAccess& f = e->as<FieldAccess>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400299 this->writeU8(Rehydrator::kFieldAccess_Command);
300 this->write(f.fBase.get());
301 this->writeU8(f.fFieldIndex);
302 this->writeU8(f.fOwnerKind);
303 break;
304 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400305 case Expression::Kind::kFloatLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400306 const FloatLiteral& f = e->as<FloatLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400307 this->writeU8(Rehydrator::kFloatLiteral_Command);
308 FloatIntUnion u;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400309 u.fFloat = f.value();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400310 this->writeS32(u.fInt);
311 break;
312 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400313 case Expression::Kind::kFunctionCall: {
John Stiles81365af2020-08-18 09:24:00 -0400314 const FunctionCall& f = e->as<FunctionCall>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400315 this->writeU8(Rehydrator::kFunctionCall_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400316 this->write(f.type());
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400317 this->writeId(&f.function());
318 this->writeU8(f.arguments().size());
319 for (const auto& a : f.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400320 this->write(a.get());
321 }
322 break;
323 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400324 case Expression::Kind::kIndex: {
John Stiles81365af2020-08-18 09:24:00 -0400325 const IndexExpression& i = e->as<IndexExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400326 this->writeU8(Rehydrator::kIndex_Command);
327 this->write(i.fBase.get());
328 this->write(i.fIndex.get());
329 break;
330 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400331 case Expression::Kind::kIntLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400332 const IntLiteral& i = e->as<IntLiteral>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400333 this->writeU8(Rehydrator::kIntLiteral_Command);
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400334 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400335 break;
336 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400337 case Expression::Kind::kNullLiteral:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400338 this->writeU8(Rehydrator::kNullLiteral_Command);
339 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400340 case Expression::Kind::kPostfix: {
John Stiles81365af2020-08-18 09:24:00 -0400341 const PostfixExpression& p = e->as<PostfixExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400342 this->writeU8(Rehydrator::kPostfix_Command);
343 this->writeU8((int) p.fOperator);
344 this->write(p.fOperand.get());
345 break;
346 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400347 case Expression::Kind::kPrefix: {
John Stiles81365af2020-08-18 09:24:00 -0400348 const PrefixExpression& p = e->as<PrefixExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400349 this->writeU8(Rehydrator::kPrefix_Command);
350 this->writeU8((int) p.fOperator);
351 this->write(p.fOperand.get());
352 break;
353 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400354 case Expression::Kind::kSetting: {
John Stiles81365af2020-08-18 09:24:00 -0400355 const Setting& s = e->as<Setting>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400356 this->writeU8(Rehydrator::kSetting_Command);
357 this->write(s.fName);
358 this->write(s.fValue.get());
359 break;
360 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400361 case Expression::Kind::kSwizzle: {
John Stiles81365af2020-08-18 09:24:00 -0400362 const Swizzle& s = e->as<Swizzle>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400363 this->writeU8(Rehydrator::kSwizzle_Command);
364 this->write(s.fBase.get());
365 this->writeU8(s.fComponents.size());
366 for (int c : s.fComponents) {
367 this->writeU8(c);
368 }
369 break;
370 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400371 case Expression::Kind::kTernary: {
John Stiles81365af2020-08-18 09:24:00 -0400372 const TernaryExpression& t = e->as<TernaryExpression>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400373 this->writeU8(Rehydrator::kTernary_Command);
374 this->write(t.fTest.get());
375 this->write(t.fIfTrue.get());
376 this->write(t.fIfFalse.get());
377 break;
378 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400379 case Expression::Kind::kVariableReference: {
John Stiles81365af2020-08-18 09:24:00 -0400380 const VariableReference& v = e->as<VariableReference>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400381 this->writeU8(Rehydrator::kVariableReference_Command);
Brian Osman79457ef2020-09-24 15:01:27 -0400382 this->writeId(v.fVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400383 this->writeU8(v.fRefKind);
384 break;
385 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400386 case Expression::Kind::kFunctionReference:
387 case Expression::Kind::kTypeReference:
388 case Expression::Kind::kDefined:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400389 // shouldn't appear in finished code
390 SkASSERT(false);
391 break;
392 }
393 } else {
394 this->writeU8(Rehydrator::kVoid_Command);
395 }
396}
397
398void Dehydrator::write(const Statement* s) {
399 if (s) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400400 switch (s->kind()) {
401 case Statement::Kind::kBlock: {
John Stiles26f98502020-08-18 09:30:51 -0400402 const Block& b = s->as<Block>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400403 this->writeU8(Rehydrator::kBlock_Command);
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400404 AutoDehydratorSymbolTable symbols(this, b.symbolTable());
405 this->writeU8(b.children().size());
406 for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
John Stilesf621e232020-08-25 13:33:02 -0400407 this->write(blockStmt.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400408 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400409 this->writeU8(b.isScope());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400410 break;
411 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400412 case Statement::Kind::kBreak:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400413 this->writeU8(Rehydrator::kBreak_Command);
414 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400415 case Statement::Kind::kContinue:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400416 this->writeU8(Rehydrator::kContinue_Command);
417 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400418 case Statement::Kind::kDiscard:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400419 this->writeU8(Rehydrator::kDiscard_Command);
420 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400421 case Statement::Kind::kDo: {
John Stiles26f98502020-08-18 09:30:51 -0400422 const DoStatement& d = s->as<DoStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400423 this->writeU8(Rehydrator::kDo_Command);
Ethan Nicholas1fd61162020-09-28 13:14:19 -0400424 this->write(d.statement().get());
425 this->write(d.test().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400426 break;
427 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400428 case Statement::Kind::kExpression: {
John Stiles26f98502020-08-18 09:30:51 -0400429 const ExpressionStatement& e = s->as<ExpressionStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400430 this->writeU8(Rehydrator::kExpressionStatement_Command);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400431 this->write(e.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400432 break;
433 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400434 case Statement::Kind::kFor: {
John Stiles26f98502020-08-18 09:30:51 -0400435 const ForStatement& f = s->as<ForStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400436 this->writeU8(Rehydrator::kFor_Command);
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400437 this->write(f.initializer().get());
438 this->write(f.test().get());
439 this->write(f.next().get());
440 this->write(f.statement().get());
441 this->write(f.symbols());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400442 break;
443 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400444 case Statement::Kind::kIf: {
John Stiles26f98502020-08-18 09:30:51 -0400445 const IfStatement& i = s->as<IfStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400446 this->writeU8(Rehydrator::kIf_Command);
447 this->writeU8(i.fIsStatic);
448 this->write(i.fTest.get());
449 this->write(i.fIfTrue.get());
450 this->write(i.fIfFalse.get());
451 break;
452 }
John Stiles98c1f822020-09-09 14:18:53 -0400453 case Statement::Kind::kInlineMarker: {
454 const InlineMarker& i = s->as<InlineMarker>();
455 this->writeU8(Rehydrator::kInlineMarker_Command);
456 this->writeId(i.fFuncDecl);
457 break;
458 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400459 case Statement::Kind::kNop:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400460 SkASSERT(false);
461 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400462 case Statement::Kind::kReturn: {
John Stiles26f98502020-08-18 09:30:51 -0400463 const ReturnStatement& r = s->as<ReturnStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400464 this->writeU8(Rehydrator::kReturn_Command);
465 this->write(r.fExpression.get());
466 break;
467 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400468 case Statement::Kind::kSwitch: {
John Stiles26f98502020-08-18 09:30:51 -0400469 const SwitchStatement& ss = s->as<SwitchStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400470 this->writeU8(Rehydrator::kSwitch_Command);
471 this->writeU8(ss.fIsStatic);
472 AutoDehydratorSymbolTable symbols(this, ss.fSymbols);
473 this->write(ss.fValue.get());
474 this->writeU8(ss.fCases.size());
John Stilesf621e232020-08-25 13:33:02 -0400475 for (const std::unique_ptr<SwitchCase>& sc : ss.fCases) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400476 this->write(sc->fValue.get());
477 this->writeU8(sc->fStatements.size());
John Stilesf621e232020-08-25 13:33:02 -0400478 for (const std::unique_ptr<Statement>& stmt : sc->fStatements) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400479 this->write(stmt.get());
480 }
481 }
482 break;
483 }
Brian Osman9eb848a2020-09-11 15:53:40 -0400484 case Statement::Kind::kSwitchCase:
485 SkASSERT(false);
486 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400487 case Statement::Kind::kVarDeclaration: {
John Stiles26f98502020-08-18 09:30:51 -0400488 const VarDeclaration& v = s->as<VarDeclaration>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400489 this->writeU8(Rehydrator::kVarDeclaration_Command);
490 this->writeU16(this->symbolId(v.fVar));
Brian Osmanc0213602020-10-06 14:43:32 -0400491 this->write(v.fBaseType);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400492 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::kWhile: {
John Stiles26f98502020-08-18 09:30:51 -0400500 const WhileStatement& w = s->as<WhileStatement>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400501 this->writeU8(Rehydrator::kWhile_Command);
502 this->write(w.fTest.get());
503 this->write(w.fStatement.get());
504 break;
505 }
506 }
507 } else {
508 this->writeU8(Rehydrator::kVoid_Command);
509 }
510}
511
512void Dehydrator::write(const ProgramElement& e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400513 switch (e.kind()) {
514 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -0400515 const Enum& en = e.as<Enum>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400516 this->writeU8(Rehydrator::kEnum_Command);
Ethan Nicholasd83ded82020-09-29 17:05:54 -0400517 this->write(en.typeName());
518 AutoDehydratorSymbolTable symbols(this, en.symbols());
519 for (const std::unique_ptr<const Symbol>& s : en.symbols()->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400520 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400521 Variable& v = (Variable&) *s;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400522 SkASSERT(v.initialValue());
523 const IntLiteral& i = v.initialValue()->as<IntLiteral>();
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400524 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400525 }
526 break;
527 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400528 case ProgramElement::Kind::kExtension:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400529 SkASSERT(false);
530 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400531 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -0400532 const FunctionDefinition& f = e.as<FunctionDefinition>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400533 this->writeU8(Rehydrator::kFunctionDefinition_Command);
534 this->writeU16(this->symbolId(&f.fDeclaration));
535 this->write(f.fBody.get());
536 this->writeU8(f.fReferencedIntrinsics.size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400537 std::set<uint16_t> ordered;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400538 for (const FunctionDeclaration* ref : f.fReferencedIntrinsics) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400539 ordered.insert(this->symbolId(ref));
540 }
541 for (uint16_t ref : ordered) {
542 this->writeU16(ref);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400543 }
544 break;
545 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400546 case ProgramElement::Kind::kInterfaceBlock: {
John Stiles3dc0da62020-08-19 17:48:31 -0400547 const InterfaceBlock& i = e.as<InterfaceBlock>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400548 this->writeU8(Rehydrator::kInterfaceBlock_Command);
549 this->write(i.fVariable);
550 this->write(i.fTypeName);
551 this->write(i.fInstanceName);
552 this->writeU8(i.fSizes.size());
553 for (const auto& s : i.fSizes) {
554 this->write(s.get());
555 }
556 break;
557 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400558 case ProgramElement::Kind::kModifiers:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400559 SkASSERT(false);
560 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400561 case ProgramElement::Kind::kSection:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400562 SkASSERT(false);
563 break;
Brian Osmanc0213602020-10-06 14:43:32 -0400564 case ProgramElement::Kind::kGlobalVar: {
565 const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400566 this->writeU8(Rehydrator::kVarDeclarations_Command);
Brian Osmanc0213602020-10-06 14:43:32 -0400567 this->write(v.fDecl.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400568 break;
569 }
570 }
571}
572
573void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
574 this->writeU8(Rehydrator::kElements_Command);
575 this->writeU8(elements.size());
576 for (const auto& e : elements) {
577 this->write(*e);
578 }
579}
580
581void Dehydrator::finish(OutputStream& out) {
582 out.write16(fStringBuffer.str().size());
583 out.writeString(fStringBuffer.str());
584 out.writeString(fBody.str());
585}
586
587} // namespace
588
589#endif