blob: 263dda2e0bea29ff41b943bcabc9cf3d36478bdb [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 Nicholas24c17722021-03-09 13:10:59 -050012#include "include/private/SkSLProgramElement.h"
13#include "include/private/SkSLStatement.h"
14#include "include/private/SkSLSymbol.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040015#include "src/sksl/SkSLRehydrator.h"
16#include "src/sksl/ir/SkSLBinaryExpression.h"
17#include "src/sksl/ir/SkSLBreakStatement.h"
18#include "src/sksl/ir/SkSLConstructor.h"
John Stiles7384b372021-04-01 13:48:15 -040019#include "src/sksl/ir/SkSLConstructorArray.h"
John Stilese1182782021-03-30 22:09:37 -040020#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stiles5abb9e12021-04-06 13:47:19 -040021#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
John Stilesfd7252f2021-04-04 22:24:40 -040022#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040023#include "src/sksl/ir/SkSLConstructorSplat.h"
John Stilesb14a8192021-04-05 11:40:46 -040024#include "src/sksl/ir/SkSLConstructorVectorCast.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040025#include "src/sksl/ir/SkSLContinueStatement.h"
26#include "src/sksl/ir/SkSLDiscardStatement.h"
27#include "src/sksl/ir/SkSLDoStatement.h"
28#include "src/sksl/ir/SkSLEnum.h"
29#include "src/sksl/ir/SkSLExpressionStatement.h"
30#include "src/sksl/ir/SkSLField.h"
31#include "src/sksl/ir/SkSLFieldAccess.h"
32#include "src/sksl/ir/SkSLForStatement.h"
33#include "src/sksl/ir/SkSLFunctionCall.h"
34#include "src/sksl/ir/SkSLFunctionDeclaration.h"
35#include "src/sksl/ir/SkSLFunctionDefinition.h"
36#include "src/sksl/ir/SkSLIfStatement.h"
37#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040038#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040039#include "src/sksl/ir/SkSLIntLiteral.h"
40#include "src/sksl/ir/SkSLInterfaceBlock.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040041#include "src/sksl/ir/SkSLPostfixExpression.h"
42#include "src/sksl/ir/SkSLPrefixExpression.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040043#include "src/sksl/ir/SkSLReturnStatement.h"
44#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050045#include "src/sksl/ir/SkSLStructDefinition.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040046#include "src/sksl/ir/SkSLSwitchCase.h"
47#include "src/sksl/ir/SkSLSwitchStatement.h"
48#include "src/sksl/ir/SkSLSwizzle.h"
John Stiles49a547f2020-10-06 16:14:37 -040049#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040050#include "src/sksl/ir/SkSLSymbolTable.h"
51#include "src/sksl/ir/SkSLTernaryExpression.h"
52#include "src/sksl/ir/SkSLUnresolvedFunction.h"
53#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040054#include "src/sksl/ir/SkSLVariable.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040055
56#ifdef SKSL_STANDALONE
57
58namespace SkSL {
59
60static constexpr int HEADER_SIZE = 2;
61
62class AutoDehydratorSymbolTable {
63public:
64 AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
65 : fDehydrator(dehydrator) {
66 dehydrator->fSymbolMap.emplace_back();
67 if (symbols) {
68 dehydrator->write(*symbols);
69 } else {
John Stilese1589a12020-10-08 13:56:46 -040070 dehydrator->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040071 }
72 }
73
74 ~AutoDehydratorSymbolTable() {
75 fDehydrator->fSymbolMap.pop_back();
76 }
77
78private:
79 Dehydrator* fDehydrator;
80};
81
82void Dehydrator::write(Layout l) {
83 if (l == Layout()) {
John Stilese1589a12020-10-08 13:56:46 -040084 this->writeCommand(Rehydrator::kDefaultLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040085 } else if (l == Layout::builtin(l.fBuiltin)) {
John Stilese1589a12020-10-08 13:56:46 -040086 this->writeCommand(Rehydrator::kBuiltinLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040087 this->writeS16(l.fBuiltin);
88 } else {
John Stilese1589a12020-10-08 13:56:46 -040089 this->writeCommand(Rehydrator::kLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040090 fBody.write32(l.fFlags);
91 this->writeS8(l.fLocation);
92 this->writeS8(l.fOffset);
93 this->writeS8(l.fBinding);
94 this->writeS8(l.fIndex);
95 this->writeS8(l.fSet);
96 this->writeS16(l.fBuiltin);
97 this->writeS8(l.fInputAttachmentIndex);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040098 this->writeS8(l.fPrimitive);
99 this->writeS8(l.fMaxVertices);
100 this->writeS8(l.fInvocations);
101 this->write(l.fMarker);
102 this->write(l.fWhen);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400103 this->writeS8((int) l.fCType);
104 }
105}
106
107void Dehydrator::write(Modifiers m) {
108 if (m == Modifiers()) {
John Stilese1589a12020-10-08 13:56:46 -0400109 this->writeCommand(Rehydrator::kDefaultModifiers_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400110 } else {
111 if (m.fFlags <= 255) {
John Stilese1589a12020-10-08 13:56:46 -0400112 this->writeCommand(Rehydrator::kModifiers8Bit_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400113 this->write(m.fLayout);
114 this->writeU8(m.fFlags);
115 } else {
John Stilese1589a12020-10-08 13:56:46 -0400116 this->writeCommand(Rehydrator::kModifiers_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400117 this->write(m.fLayout);
118 this->writeS32(m.fFlags);
119 }
120 }
121}
122
123void Dehydrator::write(StringFragment s) {
124 this->write(String(s));
125}
126
127void Dehydrator::write(String s) {
128 auto found = fStrings.find(s);
129 int offset;
130 if (found == fStrings.end()) {
131 offset = fStringBuffer.str().length() + HEADER_SIZE;
132 fStrings.insert({ s, offset });
133 SkASSERT(s.length() <= 255);
John Stilese1589a12020-10-08 13:56:46 -0400134 fStringBreaks.add(fStringBuffer.bytesWritten());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400135 fStringBuffer.write8(s.length());
136 fStringBuffer.writeString(s);
137 } else {
138 offset = found->second;
139 }
140 this->writeU16(offset);
141}
142
143void Dehydrator::write(const Symbol& s) {
144 uint16_t id = this->symbolId(&s, false);
145 if (id) {
John Stilese1589a12020-10-08 13:56:46 -0400146 this->writeCommand(Rehydrator::kSymbolRef_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400147 this->writeU16(id);
148 return;
149 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400150 switch (s.kind()) {
151 case Symbol::Kind::kFunctionDeclaration: {
John Stiles17c5b702020-08-18 10:40:03 -0400152 const FunctionDeclaration& f = s.as<FunctionDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400153 this->writeCommand(Rehydrator::kFunctionDeclaration_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400154 this->writeId(&f);
Ethan Nicholased84b732020-10-08 11:45:44 -0400155 this->write(f.modifiers());
Ethan Nicholase2c49992020-10-05 11:49:11 -0400156 this->write(f.name());
Ethan Nicholased84b732020-10-08 11:45:44 -0400157 this->writeU8(f.parameters().size());
158 for (const Variable* p : f.parameters()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400159 this->writeU16(this->symbolId(p));
160 }
Ethan Nicholased84b732020-10-08 11:45:44 -0400161 this->write(f.returnType());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400162 break;
163 }
John Stiles49a547f2020-10-06 16:14:37 -0400164 case Symbol::Kind::kSymbolAlias: {
165 const SymbolAlias& alias = s.as<SymbolAlias>();
John Stilese1589a12020-10-08 13:56:46 -0400166 this->writeCommand(Rehydrator::kSymbolAlias_Command);
John Stiles49a547f2020-10-06 16:14:37 -0400167 this->writeId(&alias);
168 this->write(alias.name());
169 this->write(*alias.origSymbol());
170 break;
171 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400172 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400173 const UnresolvedFunction& f = s.as<UnresolvedFunction>();
John Stilese1589a12020-10-08 13:56:46 -0400174 this->writeCommand(Rehydrator::kUnresolvedFunction_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400175 this->writeId(&f);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400176 this->writeU8(f.functions().size());
177 for (const FunctionDeclaration* funcDecl : f.functions()) {
John Stilesf621e232020-08-25 13:33:02 -0400178 this->write(*funcDecl);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400179 }
180 break;
181 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400182 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -0400183 const Type& t = s.as<Type>();
Ethan Nicholase6592142020-09-08 10:22:09 -0400184 switch (t.typeKind()) {
185 case Type::TypeKind::kArray:
John Stilese1589a12020-10-08 13:56:46 -0400186 this->writeCommand(Rehydrator::kArrayType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400187 this->writeId(&t);
188 this->write(t.componentType());
Brian Osmane8c26082020-10-01 17:22:45 -0400189 this->writeS8(t.columns());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400190 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400191 case Type::TypeKind::kEnum:
John Stilese1589a12020-10-08 13:56:46 -0400192 this->writeCommand(Rehydrator::kEnumType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400193 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400194 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400195 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400196 case Type::TypeKind::kStruct:
John Stilese1589a12020-10-08 13:56:46 -0400197 this->writeCommand(Rehydrator::kStructType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400198 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400199 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400200 this->writeU8(t.fields().size());
201 for (const Type::Field& f : t.fields()) {
202 this->write(f.fModifiers);
203 this->write(f.fName);
204 this->write(*f.fType);
205 }
206 break;
207 default:
John Stilese1589a12020-10-08 13:56:46 -0400208 this->writeCommand(Rehydrator::kSystemType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400209 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400210 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400211 }
212 break;
213 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400214 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400215 const Variable& v = s.as<Variable>();
John Stilese1589a12020-10-08 13:56:46 -0400216 this->writeCommand(Rehydrator::kVariable_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400217 this->writeId(&v);
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400218 this->write(v.modifiers());
Ethan Nicholase2c49992020-10-05 11:49:11 -0400219 this->write(v.name());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400220 this->write(v.type());
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400221 this->writeU8((int8_t) v.storage());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400222 break;
223 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400224 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400225 const Field& f = s.as<Field>();
John Stilese1589a12020-10-08 13:56:46 -0400226 this->writeCommand(Rehydrator::kField_Command);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400227 this->writeU16(this->symbolId(&f.owner()));
228 this->writeU8(f.fieldIndex());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400229 break;
230 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400231 case Symbol::Kind::kExternal:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400232 SkASSERT(false);
233 break;
234 }
235}
236
237void Dehydrator::write(const SymbolTable& symbols) {
John Stilese1589a12020-10-08 13:56:46 -0400238 this->writeCommand(Rehydrator::kSymbolTable_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400239 this->writeU16(symbols.fOwnedSymbols.size());
240 for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
241 this->write(*s);
242 }
John Stilesefe767d2020-10-09 12:48:04 -0400243 this->writeU16(symbols.fSymbols.count());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400244 std::map<StringFragment, const Symbol*> ordered;
John Stilesefe767d2020-10-09 12:48:04 -0400245 symbols.foreach([&](StringFragment name, const Symbol* symbol) {
246 ordered.insert({name, symbol});
247 });
Ethan Nicholas7154b742020-07-31 13:18:02 -0400248 for (std::pair<StringFragment, const Symbol*> p : ordered) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400249 bool found = false;
250 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
251 if (symbols.fOwnedSymbols[i].get() == p.second) {
John Stilese1589a12020-10-08 13:56:46 -0400252 fCommandBreaks.add(fBody.bytesWritten());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400253 this->writeU16(i);
254 found = true;
255 break;
256 }
257 }
258 SkASSERT(found);
259 }
260}
261
John Stilesd8eb8752021-04-01 11:49:10 -0400262void Dehydrator::writeExpressionSpan(const SkSpan<const std::unique_ptr<Expression>>& span) {
263 this->writeU8(span.size());
264 for (const auto& expr : span) {
265 this->write(expr.get());
John Stiles626b62e2021-03-31 22:06:07 -0400266 }
267}
268
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400269void Dehydrator::write(const Expression* e) {
270 if (e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400271 switch (e->kind()) {
272 case Expression::Kind::kBinary: {
John Stiles81365af2020-08-18 09:24:00 -0400273 const BinaryExpression& b = e->as<BinaryExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400274 this->writeCommand(Rehydrator::kBinary_Command);
John Stiles2d4f9592020-10-30 10:29:12 -0400275 this->write(b.left().get());
John Stiles45990502021-02-16 10:55:27 -0500276 this->writeU8((int) b.getOperator().kind());
John Stiles2d4f9592020-10-30 10:29:12 -0400277 this->write(b.right().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400278 break;
279 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400280 case Expression::Kind::kBoolLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400281 const BoolLiteral& b = e->as<BoolLiteral>();
John Stilese1589a12020-10-08 13:56:46 -0400282 this->writeCommand(Rehydrator::kBoolLiteral_Command);
Ethan Nicholas59d660c2020-09-28 09:18:15 -0400283 this->writeU8(b.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400284 break;
285 }
Ethan Nicholas840f5812021-02-16 13:02:57 -0500286 case Expression::Kind::kCodeString:
287 SkDEBUGFAIL("shouldn't be able to receive kCodeString here");
288 break;
John Stiles626b62e2021-03-31 22:06:07 -0400289
290 case Expression::Kind::kConstructor:
John Stilese1589a12020-10-08 13:56:46 -0400291 this->writeCommand(Rehydrator::kConstructor_Command);
John Stilesd8eb8752021-04-01 11:49:10 -0400292 this->write(e->type());
293 this->writeExpressionSpan(e->as<Constructor>().argumentSpan());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400294 break;
John Stiles626b62e2021-03-31 22:06:07 -0400295
John Stiles7384b372021-04-01 13:48:15 -0400296 case Expression::Kind::kConstructorArray:
297 this->writeCommand(Rehydrator::kConstructorArray_Command);
298 this->write(e->type());
299 this->writeExpressionSpan(e->as<ConstructorArray>().argumentSpan());
300 break;
301
John Stiles626b62e2021-03-31 22:06:07 -0400302 case Expression::Kind::kConstructorDiagonalMatrix:
John Stilese1182782021-03-30 22:09:37 -0400303 this->writeCommand(Rehydrator::kConstructorDiagonalMatrix_Command);
John Stilesd8eb8752021-04-01 11:49:10 -0400304 this->write(e->type());
305 this->writeExpressionSpan(e->as<ConstructorDiagonalMatrix>().argumentSpan());
John Stilese1182782021-03-30 22:09:37 -0400306 break;
John Stiles626b62e2021-03-31 22:06:07 -0400307
John Stiles5abb9e12021-04-06 13:47:19 -0400308 case Expression::Kind::kConstructorMatrixResize:
309 this->writeCommand(Rehydrator::kConstructorMatrixResize_Command);
310 this->write(e->type());
311 this->writeExpressionSpan(e->as<ConstructorMatrixResize>().argumentSpan());
312 break;
313
John Stilesfd7252f2021-04-04 22:24:40 -0400314 case Expression::Kind::kConstructorScalarCast:
315 this->writeCommand(Rehydrator::kConstructorScalarCast_Command);
316 this->write(e->type());
317 this->writeExpressionSpan(e->as<ConstructorScalarCast>().argumentSpan());
318 break;
319
John Stiles2938eea2021-04-01 18:58:25 -0400320 case Expression::Kind::kConstructorSplat:
321 this->writeCommand(Rehydrator::kConstructorSplat_Command);
322 this->write(e->type());
323 this->writeExpressionSpan(e->as<ConstructorSplat>().argumentSpan());
324 break;
325
John Stilesb14a8192021-04-05 11:40:46 -0400326 case Expression::Kind::kConstructorVectorCast:
327 this->writeCommand(Rehydrator::kConstructorVectorCast_Command);
328 this->write(e->type());
329 this->writeExpressionSpan(e->as<ConstructorVectorCast>().argumentSpan());
330 break;
331
Ethan Nicholase6592142020-09-08 10:22:09 -0400332 case Expression::Kind::kExternalFunctionCall:
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500333 case Expression::Kind::kExternalFunctionReference:
John Stilese1589a12020-10-08 13:56:46 -0400334 SkDEBUGFAIL("unimplemented--not expected to be used from within an include file");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400335 break;
John Stiles626b62e2021-03-31 22:06:07 -0400336
Ethan Nicholase6592142020-09-08 10:22:09 -0400337 case Expression::Kind::kFieldAccess: {
John Stiles81365af2020-08-18 09:24:00 -0400338 const FieldAccess& f = e->as<FieldAccess>();
John Stilese1589a12020-10-08 13:56:46 -0400339 this->writeCommand(Rehydrator::kFieldAccess_Command);
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400340 this->write(f.base().get());
341 this->writeU8(f.fieldIndex());
342 this->writeU8((int8_t) f.ownerKind());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400343 break;
344 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400345 case Expression::Kind::kFloatLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400346 const FloatLiteral& f = e->as<FloatLiteral>();
John Stilese1589a12020-10-08 13:56:46 -0400347 this->writeCommand(Rehydrator::kFloatLiteral_Command);
Brian Osmanfb964a42020-11-18 10:45:52 -0500348 this->write(f.type());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400349 FloatIntUnion u;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400350 u.fFloat = f.value();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400351 this->writeS32(u.fInt);
352 break;
353 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400354 case Expression::Kind::kFunctionCall: {
John Stiles81365af2020-08-18 09:24:00 -0400355 const FunctionCall& f = e->as<FunctionCall>();
John Stilese1589a12020-10-08 13:56:46 -0400356 this->writeCommand(Rehydrator::kFunctionCall_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400357 this->write(f.type());
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400358 this->writeId(&f.function());
359 this->writeU8(f.arguments().size());
360 for (const auto& a : f.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400361 this->write(a.get());
362 }
363 break;
364 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400365 case Expression::Kind::kIndex: {
John Stiles81365af2020-08-18 09:24:00 -0400366 const IndexExpression& i = e->as<IndexExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400367 this->writeCommand(Rehydrator::kIndex_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400368 this->write(i.base().get());
369 this->write(i.index().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400370 break;
371 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400372 case Expression::Kind::kIntLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400373 const IntLiteral& i = e->as<IntLiteral>();
John Stilese1589a12020-10-08 13:56:46 -0400374 this->writeCommand(Rehydrator::kIntLiteral_Command);
Brian Osmanfb964a42020-11-18 10:45:52 -0500375 this->write(i.type());
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400376 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400377 break;
378 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400379 case Expression::Kind::kPostfix: {
John Stiles81365af2020-08-18 09:24:00 -0400380 const PostfixExpression& p = e->as<PostfixExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400381 this->writeCommand(Rehydrator::kPostfix_Command);
John Stiles45990502021-02-16 10:55:27 -0500382 this->writeU8((int) p.getOperator().kind());
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400383 this->write(p.operand().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400384 break;
385 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400386 case Expression::Kind::kPrefix: {
John Stiles81365af2020-08-18 09:24:00 -0400387 const PrefixExpression& p = e->as<PrefixExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400388 this->writeCommand(Rehydrator::kPrefix_Command);
John Stiles45990502021-02-16 10:55:27 -0500389 this->writeU8((int) p.getOperator().kind());
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400390 this->write(p.operand().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400391 break;
392 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400393 case Expression::Kind::kSetting: {
John Stiles81365af2020-08-18 09:24:00 -0400394 const Setting& s = e->as<Setting>();
John Stilese1589a12020-10-08 13:56:46 -0400395 this->writeCommand(Rehydrator::kSetting_Command);
Ethan Nicholas01ec7e82020-10-08 12:10:12 -0400396 this->write(s.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400397 break;
398 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400399 case Expression::Kind::kSwizzle: {
John Stiles81365af2020-08-18 09:24:00 -0400400 const Swizzle& s = e->as<Swizzle>();
John Stilese1589a12020-10-08 13:56:46 -0400401 this->writeCommand(Rehydrator::kSwizzle_Command);
Ethan Nicholas6b4d5812020-10-12 16:11:51 -0400402 this->write(s.base().get());
403 this->writeU8(s.components().size());
404 for (int c : s.components()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400405 this->writeU8(c);
406 }
407 break;
408 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400409 case Expression::Kind::kTernary: {
John Stiles81365af2020-08-18 09:24:00 -0400410 const TernaryExpression& t = e->as<TernaryExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400411 this->writeCommand(Rehydrator::kTernary_Command);
Ethan Nicholasdd218162020-10-08 05:48:01 -0400412 this->write(t.test().get());
413 this->write(t.ifTrue().get());
414 this->write(t.ifFalse().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400415 break;
416 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400417 case Expression::Kind::kVariableReference: {
John Stiles81365af2020-08-18 09:24:00 -0400418 const VariableReference& v = e->as<VariableReference>();
John Stilese1589a12020-10-08 13:56:46 -0400419 this->writeCommand(Rehydrator::kVariableReference_Command);
Ethan Nicholas78686922020-10-08 06:46:27 -0400420 this->writeId(v.variable());
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400421 this->writeU8((int8_t) v.refKind());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400422 break;
423 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400424 case Expression::Kind::kFunctionReference:
425 case Expression::Kind::kTypeReference:
426 case Expression::Kind::kDefined:
John Stilese1589a12020-10-08 13:56:46 -0400427 SkDEBUGFAIL("this expression shouldn't appear in finished code");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400428 break;
429 }
430 } else {
John Stilese1589a12020-10-08 13:56:46 -0400431 this->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400432 }
433}
434
435void Dehydrator::write(const Statement* s) {
436 if (s) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400437 switch (s->kind()) {
438 case Statement::Kind::kBlock: {
John Stiles26f98502020-08-18 09:30:51 -0400439 const Block& b = s->as<Block>();
John Stilese1589a12020-10-08 13:56:46 -0400440 this->writeCommand(Rehydrator::kBlock_Command);
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400441 AutoDehydratorSymbolTable symbols(this, b.symbolTable());
442 this->writeU8(b.children().size());
443 for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
John Stilesf621e232020-08-25 13:33:02 -0400444 this->write(blockStmt.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400445 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400446 this->writeU8(b.isScope());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400447 break;
448 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400449 case Statement::Kind::kBreak:
John Stilese1589a12020-10-08 13:56:46 -0400450 this->writeCommand(Rehydrator::kBreak_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400451 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400452 case Statement::Kind::kContinue:
John Stilese1589a12020-10-08 13:56:46 -0400453 this->writeCommand(Rehydrator::kContinue_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400454 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400455 case Statement::Kind::kDiscard:
John Stilese1589a12020-10-08 13:56:46 -0400456 this->writeCommand(Rehydrator::kDiscard_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400457 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400458 case Statement::Kind::kDo: {
John Stiles26f98502020-08-18 09:30:51 -0400459 const DoStatement& d = s->as<DoStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400460 this->writeCommand(Rehydrator::kDo_Command);
Ethan Nicholas1fd61162020-09-28 13:14:19 -0400461 this->write(d.statement().get());
462 this->write(d.test().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400463 break;
464 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400465 case Statement::Kind::kExpression: {
John Stiles26f98502020-08-18 09:30:51 -0400466 const ExpressionStatement& e = s->as<ExpressionStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400467 this->writeCommand(Rehydrator::kExpressionStatement_Command);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400468 this->write(e.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400469 break;
470 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400471 case Statement::Kind::kFor: {
John Stiles26f98502020-08-18 09:30:51 -0400472 const ForStatement& f = s->as<ForStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400473 this->writeCommand(Rehydrator::kFor_Command);
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400474 this->write(f.initializer().get());
475 this->write(f.test().get());
476 this->write(f.next().get());
477 this->write(f.statement().get());
John Stiles7c3515b2020-10-16 18:38:39 -0400478 this->write(*f.symbols());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400479 break;
480 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400481 case Statement::Kind::kIf: {
John Stiles26f98502020-08-18 09:30:51 -0400482 const IfStatement& i = s->as<IfStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400483 this->writeCommand(Rehydrator::kIf_Command);
Ethan Nicholas8c44eca2020-10-07 16:47:09 -0400484 this->writeU8(i.isStatic());
485 this->write(i.test().get());
486 this->write(i.ifTrue().get());
487 this->write(i.ifFalse().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400488 break;
489 }
John Stiles98c1f822020-09-09 14:18:53 -0400490 case Statement::Kind::kInlineMarker: {
491 const InlineMarker& i = s->as<InlineMarker>();
John Stilese1589a12020-10-08 13:56:46 -0400492 this->writeCommand(Rehydrator::kInlineMarker_Command);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400493 this->writeId(&i.function());
John Stiles98c1f822020-09-09 14:18:53 -0400494 break;
495 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400496 case Statement::Kind::kNop:
John Stilese1589a12020-10-08 13:56:46 -0400497 SkDEBUGFAIL("unexpected--nop statement in finished code");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400498 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400499 case Statement::Kind::kReturn: {
John Stiles26f98502020-08-18 09:30:51 -0400500 const ReturnStatement& r = s->as<ReturnStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400501 this->writeCommand(Rehydrator::kReturn_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400502 this->write(r.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400503 break;
504 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400505 case Statement::Kind::kSwitch: {
John Stiles26f98502020-08-18 09:30:51 -0400506 const SwitchStatement& ss = s->as<SwitchStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400507 this->writeCommand(Rehydrator::kSwitch_Command);
Ethan Nicholas01b05e52020-10-22 15:53:41 -0400508 this->writeU8(ss.isStatic());
509 AutoDehydratorSymbolTable symbols(this, ss.symbols());
510 this->write(ss.value().get());
John Stiles2d4f9592020-10-30 10:29:12 -0400511 this->writeU8(ss.cases().size());
John Stilesb23a64b2021-03-11 08:27:59 -0500512 for (const std::unique_ptr<Statement>& stmt : ss.cases()) {
513 const SwitchCase& sc = stmt->as<SwitchCase>();
514 this->write(sc.value().get());
515 this->write(sc.statement().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400516 }
517 break;
518 }
Brian Osman9eb848a2020-09-11 15:53:40 -0400519 case Statement::Kind::kSwitchCase:
John Stilese1589a12020-10-08 13:56:46 -0400520 SkDEBUGFAIL("SwitchCase statements shouldn't appear here");
Brian Osman9eb848a2020-09-11 15:53:40 -0400521 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400522 case Statement::Kind::kVarDeclaration: {
John Stiles26f98502020-08-18 09:30:51 -0400523 const VarDeclaration& v = s->as<VarDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400524 this->writeCommand(Rehydrator::kVarDeclaration_Command);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400525 this->writeU16(this->symbolId(&v.var()));
526 this->write(v.baseType());
John Stiles62a56462020-12-03 10:41:58 -0500527 this->writeS8(v.arraySize());
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400528 this->write(v.value().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400529 break;
530 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400531 }
532 } else {
John Stilese1589a12020-10-08 13:56:46 -0400533 this->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400534 }
535}
536
537void Dehydrator::write(const ProgramElement& e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400538 switch (e.kind()) {
539 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -0400540 const Enum& en = e.as<Enum>();
John Stilese1589a12020-10-08 13:56:46 -0400541 this->writeCommand(Rehydrator::kEnum_Command);
Ethan Nicholasd83ded82020-09-29 17:05:54 -0400542 this->write(en.typeName());
543 AutoDehydratorSymbolTable symbols(this, en.symbols());
544 for (const std::unique_ptr<const Symbol>& s : en.symbols()->fOwnedSymbols) {
John Stilesca65f8f2021-03-29 17:14:39 -0400545 const Variable& v = s->as<Variable>();
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400546 SkASSERT(v.initialValue());
547 const IntLiteral& i = v.initialValue()->as<IntLiteral>();
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400548 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400549 }
550 break;
551 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400552 case ProgramElement::Kind::kExtension:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400553 SkASSERT(false);
554 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400555 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -0400556 const FunctionDefinition& f = e.as<FunctionDefinition>();
John Stilese1589a12020-10-08 13:56:46 -0400557 this->writeCommand(Rehydrator::kFunctionDefinition_Command);
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400558 this->writeU16(this->symbolId(&f.declaration()));
559 this->write(f.body().get());
560 this->writeU8(f.referencedIntrinsics().size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400561 std::set<uint16_t> ordered;
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400562 for (const FunctionDeclaration* ref : f.referencedIntrinsics()) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400563 ordered.insert(this->symbolId(ref));
564 }
565 for (uint16_t ref : ordered) {
566 this->writeU16(ref);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400567 }
568 break;
569 }
John Stiles569249b2020-11-03 12:18:22 -0500570 case ProgramElement::Kind::kFunctionPrototype: {
571 // We don't need to emit function prototypes into the dehydrated data, because we don't
572 // ever need to re-emit the intrinsics files as raw GLSL/Metal. As long as the symbols
573 // exist in the symbol table, we're in good shape.
574 break;
575 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400576 case ProgramElement::Kind::kInterfaceBlock: {
John Stiles3dc0da62020-08-19 17:48:31 -0400577 const InterfaceBlock& i = e.as<InterfaceBlock>();
John Stilese1589a12020-10-08 13:56:46 -0400578 this->writeCommand(Rehydrator::kInterfaceBlock_Command);
Ethan Nicholaseaf47882020-10-15 10:10:08 -0400579 this->write(i.variable());
580 this->write(i.typeName());
581 this->write(i.instanceName());
John Stilesd39aec02020-12-03 10:42:26 -0500582 this->writeS8(i.arraySize());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400583 break;
584 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400585 case ProgramElement::Kind::kModifiers:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400586 SkASSERT(false);
587 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400588 case ProgramElement::Kind::kSection:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400589 SkASSERT(false);
590 break;
John Stilesdc75a972020-11-25 16:24:55 -0500591 case ProgramElement::Kind::kStructDefinition: {
592 const StructDefinition& structDef = e.as<StructDefinition>();
593 this->writeCommand(Rehydrator::kStructDefinition_Command);
594 this->write(structDef.type());
595 break;
596 }
Brian Osmanc0213602020-10-06 14:43:32 -0400597 case ProgramElement::Kind::kGlobalVar: {
598 const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400599 this->writeCommand(Rehydrator::kVarDeclarations_Command);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400600 this->write(v.declaration().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400601 break;
602 }
603 }
604}
605
606void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
John Stilese1589a12020-10-08 13:56:46 -0400607 this->writeCommand(Rehydrator::kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400608 for (const auto& e : elements) {
609 this->write(*e);
610 }
John Stiles1ea7f542020-11-02 13:07:23 -0500611 this->writeCommand(Rehydrator::kElementsComplete_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400612}
613
614void Dehydrator::finish(OutputStream& out) {
John Stilese1589a12020-10-08 13:56:46 -0400615 String stringBuffer = fStringBuffer.str();
616 String commandBuffer = fBody.str();
617
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400618 out.write16(fStringBuffer.str().size());
John Stilese1589a12020-10-08 13:56:46 -0400619 fStringBufferStart = 2;
620 out.writeString(stringBuffer);
621 fCommandStart = fStringBufferStart + stringBuffer.size();
622 out.writeString(commandBuffer);
623}
624
625const char* Dehydrator::prefixAtOffset(size_t byte) {
626 if (byte >= fCommandStart) {
627 return fCommandBreaks.contains(byte - fCommandStart) ? "\n" : "";
628 }
629 if (byte >= fStringBufferStart) {
630 return fStringBreaks.contains(byte - fStringBufferStart) ? "\n" : "";
631 }
632 return "";
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400633}
634
635} // namespace
636
637#endif