blob: 728429e5aa1f15da0562a5078d8a9deea0e2c27e [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 {
John Stilese1589a12020-10-08 13:56:46 -040065 dehydrator->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040066 }
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()) {
John Stilese1589a12020-10-08 13:56:46 -040079 this->writeCommand(Rehydrator::kDefaultLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040080 } else if (l == Layout::builtin(l.fBuiltin)) {
John Stilese1589a12020-10-08 13:56:46 -040081 this->writeCommand(Rehydrator::kBuiltinLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040082 this->writeS16(l.fBuiltin);
83 } else {
John Stilese1589a12020-10-08 13:56:46 -040084 this->writeCommand(Rehydrator::kLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040085 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()) {
John Stilese1589a12020-10-08 13:56:46 -0400106 this->writeCommand(Rehydrator::kDefaultModifiers_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400107 } else {
108 if (m.fFlags <= 255) {
John Stilese1589a12020-10-08 13:56:46 -0400109 this->writeCommand(Rehydrator::kModifiers8Bit_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400110 this->write(m.fLayout);
111 this->writeU8(m.fFlags);
112 } else {
John Stilese1589a12020-10-08 13:56:46 -0400113 this->writeCommand(Rehydrator::kModifiers_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400114 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);
John Stilese1589a12020-10-08 13:56:46 -0400131 fStringBreaks.add(fStringBuffer.bytesWritten());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400132 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) {
John Stilese1589a12020-10-08 13:56:46 -0400143 this->writeCommand(Rehydrator::kSymbolRef_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400144 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>();
John Stilese1589a12020-10-08 13:56:46 -0400150 this->writeCommand(Rehydrator::kFunctionDeclaration_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400151 this->writeId(&f);
Ethan Nicholased84b732020-10-08 11:45:44 -0400152 this->write(f.modifiers());
Ethan Nicholase2c49992020-10-05 11:49:11 -0400153 this->write(f.name());
Ethan Nicholased84b732020-10-08 11:45:44 -0400154 this->writeU8(f.parameters().size());
155 for (const Variable* p : f.parameters()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400156 this->writeU16(this->symbolId(p));
157 }
Ethan Nicholased84b732020-10-08 11:45:44 -0400158 this->write(f.returnType());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400159 break;
160 }
John Stiles49a547f2020-10-06 16:14:37 -0400161 case Symbol::Kind::kSymbolAlias: {
162 const SymbolAlias& alias = s.as<SymbolAlias>();
John Stilese1589a12020-10-08 13:56:46 -0400163 this->writeCommand(Rehydrator::kSymbolAlias_Command);
John Stiles49a547f2020-10-06 16:14:37 -0400164 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>();
John Stilese1589a12020-10-08 13:56:46 -0400171 this->writeCommand(Rehydrator::kUnresolvedFunction_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400172 this->writeId(&f);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400173 this->writeU8(f.functions().size());
174 for (const FunctionDeclaration* funcDecl : f.functions()) {
John Stilesf621e232020-08-25 13:33:02 -0400175 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:
John Stilese1589a12020-10-08 13:56:46 -0400183 this->writeCommand(Rehydrator::kArrayType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400184 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:
John Stilese1589a12020-10-08 13:56:46 -0400189 this->writeCommand(Rehydrator::kEnumType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400190 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:
John Stilese1589a12020-10-08 13:56:46 -0400194 this->writeCommand(Rehydrator::kNullableType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400195 this->writeId(&t);
196 this->write(t.componentType());
197 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400198 case Type::TypeKind::kStruct:
John Stilese1589a12020-10-08 13:56:46 -0400199 this->writeCommand(Rehydrator::kStructType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400200 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:
John Stilese1589a12020-10-08 13:56:46 -0400210 this->writeCommand(Rehydrator::kSystemType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400211 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>();
John Stilese1589a12020-10-08 13:56:46 -0400218 this->writeCommand(Rehydrator::kVariable_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400219 this->writeId(&v);
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400220 this->write(v.modifiers());
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 Nicholas453f67f2020-10-09 10:43:45 -0400223 this->writeU8((int8_t) v.storage());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400224 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>();
John Stilese1589a12020-10-08 13:56:46 -0400228 this->writeCommand(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) {
John Stilese1589a12020-10-08 13:56:46 -0400240 this->writeCommand(Rehydrator::kSymbolTable_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400241 this->writeU16(symbols.fOwnedSymbols.size());
242 for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
243 this->write(*s);
244 }
John Stilesefe767d2020-10-09 12:48:04 -0400245 this->writeU16(symbols.fSymbols.count());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400246 std::map<StringFragment, const Symbol*> ordered;
John Stilesefe767d2020-10-09 12:48:04 -0400247 symbols.foreach([&](StringFragment name, const Symbol* symbol) {
248 ordered.insert({name, symbol});
249 });
Ethan Nicholas7154b742020-07-31 13:18:02 -0400250 for (std::pair<StringFragment, const Symbol*> p : ordered) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400251 bool found = false;
252 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
253 if (symbols.fOwnedSymbols[i].get() == p.second) {
John Stilese1589a12020-10-08 13:56:46 -0400254 fCommandBreaks.add(fBody.bytesWritten());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400255 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>();
John Stilese1589a12020-10-08 13:56:46 -0400269 this->writeCommand(Rehydrator::kBinary_Command);
John Stiles2d4f9592020-10-30 10:29:12 -0400270 this->write(b.left().get());
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400271 this->writeU8((int) b.getOperator());
John Stiles2d4f9592020-10-30 10:29:12 -0400272 this->write(b.right().get());
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>();
John Stilese1589a12020-10-08 13:56:46 -0400278 this->writeCommand(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>();
John Stilese1589a12020-10-08 13:56:46 -0400284 this->writeCommand(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:
John Stilese1589a12020-10-08 13:56:46 -0400294 SkDEBUGFAIL("unimplemented--not expected to be used from within an include file");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400295 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>();
John Stilese1589a12020-10-08 13:56:46 -0400298 this->writeCommand(Rehydrator::kFieldAccess_Command);
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400299 this->write(f.base().get());
300 this->writeU8(f.fieldIndex());
301 this->writeU8((int8_t) f.ownerKind());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400302 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>();
John Stilese1589a12020-10-08 13:56:46 -0400306 this->writeCommand(Rehydrator::kFloatLiteral_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400307 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>();
John Stilese1589a12020-10-08 13:56:46 -0400314 this->writeCommand(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>();
John Stilese1589a12020-10-08 13:56:46 -0400325 this->writeCommand(Rehydrator::kIndex_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400326 this->write(i.base().get());
327 this->write(i.index().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400328 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>();
John Stilese1589a12020-10-08 13:56:46 -0400332 this->writeCommand(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:
John Stilese1589a12020-10-08 13:56:46 -0400337 this->writeCommand(Rehydrator::kNullLiteral_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400338 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>();
John Stilese1589a12020-10-08 13:56:46 -0400341 this->writeCommand(Rehydrator::kPostfix_Command);
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400342 this->writeU8((int) p.getOperator());
343 this->write(p.operand().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400344 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>();
John Stilese1589a12020-10-08 13:56:46 -0400348 this->writeCommand(Rehydrator::kPrefix_Command);
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400349 this->writeU8((int) p.getOperator());
350 this->write(p.operand().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400351 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>();
John Stilese1589a12020-10-08 13:56:46 -0400355 this->writeCommand(Rehydrator::kSetting_Command);
Ethan Nicholas01ec7e82020-10-08 12:10:12 -0400356 this->write(s.name());
357 this->write(s.type());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400358 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>();
John Stilese1589a12020-10-08 13:56:46 -0400362 this->writeCommand(Rehydrator::kSwizzle_Command);
Ethan Nicholas6b4d5812020-10-12 16:11:51 -0400363 this->write(s.base().get());
364 this->writeU8(s.components().size());
365 for (int c : s.components()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400366 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>();
John Stilese1589a12020-10-08 13:56:46 -0400372 this->writeCommand(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>();
John Stilese1589a12020-10-08 13:56:46 -0400380 this->writeCommand(Rehydrator::kVariableReference_Command);
Ethan Nicholas78686922020-10-08 06:46:27 -0400381 this->writeId(v.variable());
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400382 this->writeU8((int8_t) 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:
John Stilese1589a12020-10-08 13:56:46 -0400388 SkDEBUGFAIL("this expression shouldn't appear in finished code");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400389 break;
390 }
391 } else {
John Stilese1589a12020-10-08 13:56:46 -0400392 this->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400393 }
394}
395
396void Dehydrator::write(const Statement* s) {
397 if (s) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400398 switch (s->kind()) {
399 case Statement::Kind::kBlock: {
John Stiles26f98502020-08-18 09:30:51 -0400400 const Block& b = s->as<Block>();
John Stilese1589a12020-10-08 13:56:46 -0400401 this->writeCommand(Rehydrator::kBlock_Command);
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400402 AutoDehydratorSymbolTable symbols(this, b.symbolTable());
403 this->writeU8(b.children().size());
404 for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
John Stilesf621e232020-08-25 13:33:02 -0400405 this->write(blockStmt.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400406 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400407 this->writeU8(b.isScope());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400408 break;
409 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400410 case Statement::Kind::kBreak:
John Stilese1589a12020-10-08 13:56:46 -0400411 this->writeCommand(Rehydrator::kBreak_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400412 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400413 case Statement::Kind::kContinue:
John Stilese1589a12020-10-08 13:56:46 -0400414 this->writeCommand(Rehydrator::kContinue_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400415 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400416 case Statement::Kind::kDiscard:
John Stilese1589a12020-10-08 13:56:46 -0400417 this->writeCommand(Rehydrator::kDiscard_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400418 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400419 case Statement::Kind::kDo: {
John Stiles26f98502020-08-18 09:30:51 -0400420 const DoStatement& d = s->as<DoStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400421 this->writeCommand(Rehydrator::kDo_Command);
Ethan Nicholas1fd61162020-09-28 13:14:19 -0400422 this->write(d.statement().get());
423 this->write(d.test().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400424 break;
425 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400426 case Statement::Kind::kExpression: {
John Stiles26f98502020-08-18 09:30:51 -0400427 const ExpressionStatement& e = s->as<ExpressionStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400428 this->writeCommand(Rehydrator::kExpressionStatement_Command);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400429 this->write(e.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400430 break;
431 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400432 case Statement::Kind::kFor: {
John Stiles26f98502020-08-18 09:30:51 -0400433 const ForStatement& f = s->as<ForStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400434 this->writeCommand(Rehydrator::kFor_Command);
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400435 this->write(f.initializer().get());
436 this->write(f.test().get());
437 this->write(f.next().get());
438 this->write(f.statement().get());
John Stiles7c3515b2020-10-16 18:38:39 -0400439 this->write(*f.symbols());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400440 break;
441 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400442 case Statement::Kind::kIf: {
John Stiles26f98502020-08-18 09:30:51 -0400443 const IfStatement& i = s->as<IfStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400444 this->writeCommand(Rehydrator::kIf_Command);
Ethan Nicholas8c44eca2020-10-07 16:47:09 -0400445 this->writeU8(i.isStatic());
446 this->write(i.test().get());
447 this->write(i.ifTrue().get());
448 this->write(i.ifFalse().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400449 break;
450 }
John Stiles98c1f822020-09-09 14:18:53 -0400451 case Statement::Kind::kInlineMarker: {
452 const InlineMarker& i = s->as<InlineMarker>();
John Stilese1589a12020-10-08 13:56:46 -0400453 this->writeCommand(Rehydrator::kInlineMarker_Command);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400454 this->writeId(&i.function());
John Stiles98c1f822020-09-09 14:18:53 -0400455 break;
456 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400457 case Statement::Kind::kNop:
John Stilese1589a12020-10-08 13:56:46 -0400458 SkDEBUGFAIL("unexpected--nop statement in finished code");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400459 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400460 case Statement::Kind::kReturn: {
John Stiles26f98502020-08-18 09:30:51 -0400461 const ReturnStatement& r = s->as<ReturnStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400462 this->writeCommand(Rehydrator::kReturn_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400463 this->write(r.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400464 break;
465 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400466 case Statement::Kind::kSwitch: {
John Stiles26f98502020-08-18 09:30:51 -0400467 const SwitchStatement& ss = s->as<SwitchStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400468 this->writeCommand(Rehydrator::kSwitch_Command);
Ethan Nicholas01b05e52020-10-22 15:53:41 -0400469 this->writeU8(ss.isStatic());
470 AutoDehydratorSymbolTable symbols(this, ss.symbols());
471 this->write(ss.value().get());
John Stiles2d4f9592020-10-30 10:29:12 -0400472 this->writeU8(ss.cases().size());
473 for (const std::unique_ptr<SwitchCase>& sc : ss.cases()) {
474 this->write(sc->value().get());
475 this->writeU8(sc->statements().size());
476 for (const std::unique_ptr<Statement>& stmt : sc->statements()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400477 this->write(stmt.get());
478 }
479 }
480 break;
481 }
Brian Osman9eb848a2020-09-11 15:53:40 -0400482 case Statement::Kind::kSwitchCase:
John Stilese1589a12020-10-08 13:56:46 -0400483 SkDEBUGFAIL("SwitchCase statements shouldn't appear here");
Brian Osman9eb848a2020-09-11 15:53:40 -0400484 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400485 case Statement::Kind::kVarDeclaration: {
John Stiles26f98502020-08-18 09:30:51 -0400486 const VarDeclaration& v = s->as<VarDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400487 this->writeCommand(Rehydrator::kVarDeclaration_Command);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400488 this->writeU16(this->symbolId(&v.var()));
489 this->write(v.baseType());
John Stiles2d4f9592020-10-30 10:29:12 -0400490 this->writeU8(v.sizes().count());
491 for (const std::unique_ptr<Expression>& size : v.sizes()) {
492 this->write(size.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400493 }
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400494 this->write(v.value().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400495 break;
496 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400497 case Statement::Kind::kWhile: {
John Stiles26f98502020-08-18 09:30:51 -0400498 const WhileStatement& w = s->as<WhileStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400499 this->writeCommand(Rehydrator::kWhile_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400500 this->write(w.test().get());
501 this->write(w.statement().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400502 break;
503 }
504 }
505 } else {
John Stilese1589a12020-10-08 13:56:46 -0400506 this->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400507 }
508}
509
510void Dehydrator::write(const ProgramElement& e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400511 switch (e.kind()) {
512 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -0400513 const Enum& en = e.as<Enum>();
John Stilese1589a12020-10-08 13:56:46 -0400514 this->writeCommand(Rehydrator::kEnum_Command);
Ethan Nicholasd83ded82020-09-29 17:05:54 -0400515 this->write(en.typeName());
516 AutoDehydratorSymbolTable symbols(this, en.symbols());
517 for (const std::unique_ptr<const Symbol>& s : en.symbols()->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400518 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400519 Variable& v = (Variable&) *s;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400520 SkASSERT(v.initialValue());
521 const IntLiteral& i = v.initialValue()->as<IntLiteral>();
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400522 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400523 }
524 break;
525 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400526 case ProgramElement::Kind::kExtension:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400527 SkASSERT(false);
528 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400529 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -0400530 const FunctionDefinition& f = e.as<FunctionDefinition>();
John Stilese1589a12020-10-08 13:56:46 -0400531 this->writeCommand(Rehydrator::kFunctionDefinition_Command);
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400532 this->writeU16(this->symbolId(&f.declaration()));
533 this->write(f.body().get());
534 this->writeU8(f.referencedIntrinsics().size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400535 std::set<uint16_t> ordered;
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400536 for (const FunctionDeclaration* ref : f.referencedIntrinsics()) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400537 ordered.insert(this->symbolId(ref));
538 }
539 for (uint16_t ref : ordered) {
540 this->writeU16(ref);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400541 }
542 break;
543 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400544 case ProgramElement::Kind::kInterfaceBlock: {
John Stiles3dc0da62020-08-19 17:48:31 -0400545 const InterfaceBlock& i = e.as<InterfaceBlock>();
John Stilese1589a12020-10-08 13:56:46 -0400546 this->writeCommand(Rehydrator::kInterfaceBlock_Command);
Ethan Nicholaseaf47882020-10-15 10:10:08 -0400547 this->write(i.variable());
548 this->write(i.typeName());
549 this->write(i.instanceName());
550 this->writeU8(i.sizes().count());
551 for (const auto& s : i.sizes()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400552 this->write(s.get());
553 }
554 break;
555 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400556 case ProgramElement::Kind::kModifiers:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400557 SkASSERT(false);
558 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400559 case ProgramElement::Kind::kSection:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400560 SkASSERT(false);
561 break;
Brian Osmanc0213602020-10-06 14:43:32 -0400562 case ProgramElement::Kind::kGlobalVar: {
563 const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400564 this->writeCommand(Rehydrator::kVarDeclarations_Command);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400565 this->write(v.declaration().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400566 break;
567 }
568 }
569}
570
571void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
John Stilese1589a12020-10-08 13:56:46 -0400572 this->writeCommand(Rehydrator::kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400573 this->writeU8(elements.size());
574 for (const auto& e : elements) {
575 this->write(*e);
576 }
577}
578
579void Dehydrator::finish(OutputStream& out) {
John Stilese1589a12020-10-08 13:56:46 -0400580 String stringBuffer = fStringBuffer.str();
581 String commandBuffer = fBody.str();
582
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400583 out.write16(fStringBuffer.str().size());
John Stilese1589a12020-10-08 13:56:46 -0400584 fStringBufferStart = 2;
585 out.writeString(stringBuffer);
586 fCommandStart = fStringBufferStart + stringBuffer.size();
587 out.writeString(commandBuffer);
588}
589
590const char* Dehydrator::prefixAtOffset(size_t byte) {
591 if (byte >= fCommandStart) {
592 return fCommandBreaks.contains(byte - fCommandStart) ? "\n" : "";
593 }
594 if (byte >= fStringBufferStart) {
595 return fStringBreaks.contains(byte - fStringBufferStart) ? "\n" : "";
596 }
597 return "";
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400598}
599
600} // namespace
601
602#endif