blob: cc1880eb0ba4defb4ce30c0c56380e9bb8b12d97 [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 Stilese3ae9682021-08-05 10:35:01 -040020#include "src/sksl/ir/SkSLConstructorArrayCast.h"
John Stiles8cad6372021-04-07 12:31:13 -040021#include "src/sksl/ir/SkSLConstructorCompound.h"
22#include "src/sksl/ir/SkSLConstructorCompoundCast.h"
John Stilese1182782021-03-30 22:09:37 -040023#include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h"
John Stiles5abb9e12021-04-06 13:47:19 -040024#include "src/sksl/ir/SkSLConstructorMatrixResize.h"
John Stilesfd7252f2021-04-04 22:24:40 -040025#include "src/sksl/ir/SkSLConstructorScalarCast.h"
John Stiles2938eea2021-04-01 18:58:25 -040026#include "src/sksl/ir/SkSLConstructorSplat.h"
John Stilesd47330f2021-04-08 23:25:52 -040027#include "src/sksl/ir/SkSLConstructorStruct.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040028#include "src/sksl/ir/SkSLContinueStatement.h"
29#include "src/sksl/ir/SkSLDiscardStatement.h"
30#include "src/sksl/ir/SkSLDoStatement.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040031#include "src/sksl/ir/SkSLExpressionStatement.h"
32#include "src/sksl/ir/SkSLField.h"
33#include "src/sksl/ir/SkSLFieldAccess.h"
34#include "src/sksl/ir/SkSLForStatement.h"
35#include "src/sksl/ir/SkSLFunctionCall.h"
36#include "src/sksl/ir/SkSLFunctionDeclaration.h"
37#include "src/sksl/ir/SkSLFunctionDefinition.h"
38#include "src/sksl/ir/SkSLIfStatement.h"
39#include "src/sksl/ir/SkSLIndexExpression.h"
John Stiles98c1f822020-09-09 14:18:53 -040040#include "src/sksl/ir/SkSLInlineMarker.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040041#include "src/sksl/ir/SkSLInterfaceBlock.h"
John Stiles7591d4b2021-09-13 13:32:06 -040042#include "src/sksl/ir/SkSLLiteral.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040043#include "src/sksl/ir/SkSLPostfixExpression.h"
44#include "src/sksl/ir/SkSLPrefixExpression.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040045#include "src/sksl/ir/SkSLReturnStatement.h"
46#include "src/sksl/ir/SkSLSetting.h"
John Stilesdc75a972020-11-25 16:24:55 -050047#include "src/sksl/ir/SkSLStructDefinition.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040048#include "src/sksl/ir/SkSLSwitchCase.h"
49#include "src/sksl/ir/SkSLSwitchStatement.h"
50#include "src/sksl/ir/SkSLSwizzle.h"
John Stiles49a547f2020-10-06 16:14:37 -040051#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040052#include "src/sksl/ir/SkSLSymbolTable.h"
53#include "src/sksl/ir/SkSLTernaryExpression.h"
54#include "src/sksl/ir/SkSLUnresolvedFunction.h"
55#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040056#include "src/sksl/ir/SkSLVariable.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040057
58#ifdef SKSL_STANDALONE
59
60namespace SkSL {
61
62static constexpr int HEADER_SIZE = 2;
63
64class AutoDehydratorSymbolTable {
65public:
66 AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
67 : fDehydrator(dehydrator) {
68 dehydrator->fSymbolMap.emplace_back();
69 if (symbols) {
70 dehydrator->write(*symbols);
71 } else {
John Stilese1589a12020-10-08 13:56:46 -040072 dehydrator->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040073 }
74 }
75
76 ~AutoDehydratorSymbolTable() {
77 fDehydrator->fSymbolMap.pop_back();
78 }
79
80private:
81 Dehydrator* fDehydrator;
82};
83
84void Dehydrator::write(Layout l) {
85 if (l == Layout()) {
John Stilese1589a12020-10-08 13:56:46 -040086 this->writeCommand(Rehydrator::kDefaultLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040087 } else if (l == Layout::builtin(l.fBuiltin)) {
John Stilese1589a12020-10-08 13:56:46 -040088 this->writeCommand(Rehydrator::kBuiltinLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040089 this->writeS16(l.fBuiltin);
90 } else {
John Stilese1589a12020-10-08 13:56:46 -040091 this->writeCommand(Rehydrator::kLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040092 fBody.write32(l.fFlags);
93 this->writeS8(l.fLocation);
94 this->writeS8(l.fOffset);
95 this->writeS8(l.fBinding);
96 this->writeS8(l.fIndex);
97 this->writeS8(l.fSet);
98 this->writeS16(l.fBuiltin);
99 this->writeS8(l.fInputAttachmentIndex);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400100 }
101}
102
103void Dehydrator::write(Modifiers m) {
104 if (m == Modifiers()) {
John Stilese1589a12020-10-08 13:56:46 -0400105 this->writeCommand(Rehydrator::kDefaultModifiers_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400106 } else {
107 if (m.fFlags <= 255) {
John Stilese1589a12020-10-08 13:56:46 -0400108 this->writeCommand(Rehydrator::kModifiers8Bit_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400109 this->write(m.fLayout);
110 this->writeU8(m.fFlags);
111 } else {
John Stilese1589a12020-10-08 13:56:46 -0400112 this->writeCommand(Rehydrator::kModifiers_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400113 this->write(m.fLayout);
114 this->writeS32(m.fFlags);
115 }
116 }
117}
118
Ethan Nicholas962dec42021-06-10 13:06:39 -0400119void Dehydrator::write(skstd::string_view s) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400120 this->write(String(s));
121}
122
123void Dehydrator::write(String s) {
124 auto found = fStrings.find(s);
125 int offset;
126 if (found == fStrings.end()) {
127 offset = fStringBuffer.str().length() + HEADER_SIZE;
128 fStrings.insert({ s, offset });
129 SkASSERT(s.length() <= 255);
John Stilese1589a12020-10-08 13:56:46 -0400130 fStringBreaks.add(fStringBuffer.bytesWritten());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400131 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) {
John Stilese1589a12020-10-08 13:56:46 -0400142 this->writeCommand(Rehydrator::kSymbolRef_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400143 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>();
John Stilese1589a12020-10-08 13:56:46 -0400149 this->writeCommand(Rehydrator::kFunctionDeclaration_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400150 this->writeId(&f);
Ethan Nicholased84b732020-10-08 11:45:44 -0400151 this->write(f.modifiers());
Ethan Nicholase2c49992020-10-05 11:49:11 -0400152 this->write(f.name());
Ethan Nicholased84b732020-10-08 11:45:44 -0400153 this->writeU8(f.parameters().size());
154 for (const Variable* p : f.parameters()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400155 this->writeU16(this->symbolId(p));
156 }
Ethan Nicholased84b732020-10-08 11:45:44 -0400157 this->write(f.returnType());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400158 break;
159 }
John Stiles49a547f2020-10-06 16:14:37 -0400160 case Symbol::Kind::kSymbolAlias: {
161 const SymbolAlias& alias = s.as<SymbolAlias>();
John Stilese1589a12020-10-08 13:56:46 -0400162 this->writeCommand(Rehydrator::kSymbolAlias_Command);
John Stiles49a547f2020-10-06 16:14:37 -0400163 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>();
John Stilese1589a12020-10-08 13:56:46 -0400170 this->writeCommand(Rehydrator::kUnresolvedFunction_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400171 this->writeId(&f);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400172 this->writeU8(f.functions().size());
173 for (const FunctionDeclaration* funcDecl : f.functions()) {
John Stilesf621e232020-08-25 13:33:02 -0400174 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:
John Stilese1589a12020-10-08 13:56:46 -0400182 this->writeCommand(Rehydrator::kArrayType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400183 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::kStruct:
John Stilese1589a12020-10-08 13:56:46 -0400188 this->writeCommand(Rehydrator::kStructType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400189 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400190 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400191 this->writeU8(t.fields().size());
192 for (const Type::Field& f : t.fields()) {
193 this->write(f.fModifiers);
194 this->write(f.fName);
195 this->write(*f.fType);
196 }
197 break;
198 default:
John Stilese1589a12020-10-08 13:56:46 -0400199 this->writeCommand(Rehydrator::kSystemType_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 Nicholasb13f3692021-09-10 16:49:42 -0400202 break;
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400203 }
204 break;
205 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400206 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400207 const Variable& v = s.as<Variable>();
John Stilese1589a12020-10-08 13:56:46 -0400208 this->writeCommand(Rehydrator::kVariable_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400209 this->writeId(&v);
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400210 this->write(v.modifiers());
Ethan Nicholase2c49992020-10-05 11:49:11 -0400211 this->write(v.name());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400212 this->write(v.type());
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400213 this->writeU8((int8_t) v.storage());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400214 break;
215 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400216 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400217 const Field& f = s.as<Field>();
John Stilese1589a12020-10-08 13:56:46 -0400218 this->writeCommand(Rehydrator::kField_Command);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400219 this->writeU16(this->symbolId(&f.owner()));
220 this->writeU8(f.fieldIndex());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400221 break;
222 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400223 case Symbol::Kind::kExternal:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400224 SkASSERT(false);
225 break;
226 }
227}
228
229void Dehydrator::write(const SymbolTable& symbols) {
John Stilese1589a12020-10-08 13:56:46 -0400230 this->writeCommand(Rehydrator::kSymbolTable_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400231 this->writeU16(symbols.fOwnedSymbols.size());
232 for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
233 this->write(*s);
234 }
John Stilesefe767d2020-10-09 12:48:04 -0400235 this->writeU16(symbols.fSymbols.count());
Ethan Nicholas962dec42021-06-10 13:06:39 -0400236 std::map<skstd::string_view, const Symbol*> ordered;
237 symbols.foreach([&](skstd::string_view name, const Symbol* symbol) {
John Stilesefe767d2020-10-09 12:48:04 -0400238 ordered.insert({name, symbol});
239 });
Ethan Nicholas962dec42021-06-10 13:06:39 -0400240 for (std::pair<skstd::string_view, const Symbol*> p : ordered) {
Brian Salomon54fd96b2021-08-05 12:16:07 -0400241 SkDEBUGCODE(bool found = false;)
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400242 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
243 if (symbols.fOwnedSymbols[i].get() == p.second) {
John Stilese1589a12020-10-08 13:56:46 -0400244 fCommandBreaks.add(fBody.bytesWritten());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400245 this->writeU16(i);
Brian Salomon54fd96b2021-08-05 12:16:07 -0400246 SkDEBUGCODE(found = true;)
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400247 break;
248 }
249 }
250 SkASSERT(found);
251 }
252}
253
John Stilesd8eb8752021-04-01 11:49:10 -0400254void Dehydrator::writeExpressionSpan(const SkSpan<const std::unique_ptr<Expression>>& span) {
255 this->writeU8(span.size());
256 for (const auto& expr : span) {
257 this->write(expr.get());
John Stiles626b62e2021-03-31 22:06:07 -0400258 }
259}
260
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400261void Dehydrator::write(const Expression* e) {
262 if (e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400263 switch (e->kind()) {
264 case Expression::Kind::kBinary: {
John Stiles81365af2020-08-18 09:24:00 -0400265 const BinaryExpression& b = e->as<BinaryExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400266 this->writeCommand(Rehydrator::kBinary_Command);
John Stiles2d4f9592020-10-30 10:29:12 -0400267 this->write(b.left().get());
John Stiles45990502021-02-16 10:55:27 -0500268 this->writeU8((int) b.getOperator().kind());
John Stiles2d4f9592020-10-30 10:29:12 -0400269 this->write(b.right().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400270 break;
271 }
Brian Osmaneb0f29d2021-08-04 11:34:16 -0400272 case Expression::Kind::kChildCall:
273 SkDEBUGFAIL("unimplemented--not expected to be used from within an include file");
274 break;
275
Ethan Nicholas840f5812021-02-16 13:02:57 -0500276 case Expression::Kind::kCodeString:
277 SkDEBUGFAIL("shouldn't be able to receive kCodeString here");
278 break;
John Stiles626b62e2021-03-31 22:06:07 -0400279
John Stiles7384b372021-04-01 13:48:15 -0400280 case Expression::Kind::kConstructorArray:
281 this->writeCommand(Rehydrator::kConstructorArray_Command);
282 this->write(e->type());
283 this->writeExpressionSpan(e->as<ConstructorArray>().argumentSpan());
284 break;
285
John Stilese3ae9682021-08-05 10:35:01 -0400286 case Expression::Kind::kConstructorArrayCast:
287 this->writeCommand(Rehydrator::kConstructorArrayCast_Command);
288 this->write(e->type());
289 this->writeExpressionSpan(e->as<ConstructorArrayCast>().argumentSpan());
290 break;
291
John Stiles8cad6372021-04-07 12:31:13 -0400292 case Expression::Kind::kConstructorCompound:
293 this->writeCommand(Rehydrator::kConstructorCompound_Command);
John Stiles2bec8ab2021-04-06 18:40:04 -0400294 this->write(e->type());
John Stiles8cad6372021-04-07 12:31:13 -0400295 this->writeExpressionSpan(e->as<ConstructorCompound>().argumentSpan());
John Stiles2bec8ab2021-04-06 18:40:04 -0400296 break;
297
John Stiles8cad6372021-04-07 12:31:13 -0400298 case Expression::Kind::kConstructorCompoundCast:
299 this->writeCommand(Rehydrator::kConstructorCompoundCast_Command);
John Stiles268a73f2021-04-07 12:30:22 -0400300 this->write(e->type());
John Stiles8cad6372021-04-07 12:31:13 -0400301 this->writeExpressionSpan(e->as<ConstructorCompoundCast>().argumentSpan());
John Stiles268a73f2021-04-07 12:30:22 -0400302 break;
303
John Stiles626b62e2021-03-31 22:06:07 -0400304 case Expression::Kind::kConstructorDiagonalMatrix:
John Stilese1182782021-03-30 22:09:37 -0400305 this->writeCommand(Rehydrator::kConstructorDiagonalMatrix_Command);
John Stilesd8eb8752021-04-01 11:49:10 -0400306 this->write(e->type());
307 this->writeExpressionSpan(e->as<ConstructorDiagonalMatrix>().argumentSpan());
John Stilese1182782021-03-30 22:09:37 -0400308 break;
John Stiles626b62e2021-03-31 22:06:07 -0400309
John Stiles5abb9e12021-04-06 13:47:19 -0400310 case Expression::Kind::kConstructorMatrixResize:
311 this->writeCommand(Rehydrator::kConstructorMatrixResize_Command);
312 this->write(e->type());
313 this->writeExpressionSpan(e->as<ConstructorMatrixResize>().argumentSpan());
314 break;
315
John Stilesfd7252f2021-04-04 22:24:40 -0400316 case Expression::Kind::kConstructorScalarCast:
317 this->writeCommand(Rehydrator::kConstructorScalarCast_Command);
318 this->write(e->type());
319 this->writeExpressionSpan(e->as<ConstructorScalarCast>().argumentSpan());
320 break;
321
John Stiles2938eea2021-04-01 18:58:25 -0400322 case Expression::Kind::kConstructorSplat:
323 this->writeCommand(Rehydrator::kConstructorSplat_Command);
324 this->write(e->type());
325 this->writeExpressionSpan(e->as<ConstructorSplat>().argumentSpan());
326 break;
327
John Stilesd47330f2021-04-08 23:25:52 -0400328 case Expression::Kind::kConstructorStruct:
329 this->writeCommand(Rehydrator::kConstructorStruct_Command);
330 this->write(e->type());
331 this->writeExpressionSpan(e->as<ConstructorStruct>().argumentSpan());
332 break;
333
Ethan Nicholase6592142020-09-08 10:22:09 -0400334 case Expression::Kind::kExternalFunctionCall:
Brian Osmanbe0b3b72021-01-06 14:27:35 -0500335 case Expression::Kind::kExternalFunctionReference:
John Stilese1589a12020-10-08 13:56:46 -0400336 SkDEBUGFAIL("unimplemented--not expected to be used from within an include file");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400337 break;
John Stiles626b62e2021-03-31 22:06:07 -0400338
Ethan Nicholase6592142020-09-08 10:22:09 -0400339 case Expression::Kind::kFieldAccess: {
John Stiles81365af2020-08-18 09:24:00 -0400340 const FieldAccess& f = e->as<FieldAccess>();
John Stilese1589a12020-10-08 13:56:46 -0400341 this->writeCommand(Rehydrator::kFieldAccess_Command);
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400342 this->write(f.base().get());
343 this->writeU8(f.fieldIndex());
344 this->writeU8((int8_t) f.ownerKind());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400345 break;
346 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400347 case Expression::Kind::kFunctionCall: {
John Stiles81365af2020-08-18 09:24:00 -0400348 const FunctionCall& f = e->as<FunctionCall>();
John Stilese1589a12020-10-08 13:56:46 -0400349 this->writeCommand(Rehydrator::kFunctionCall_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400350 this->write(f.type());
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400351 this->writeId(&f.function());
352 this->writeU8(f.arguments().size());
353 for (const auto& a : f.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400354 this->write(a.get());
355 }
356 break;
357 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400358 case Expression::Kind::kIndex: {
John Stiles81365af2020-08-18 09:24:00 -0400359 const IndexExpression& i = e->as<IndexExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400360 this->writeCommand(Rehydrator::kIndex_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400361 this->write(i.base().get());
362 this->write(i.index().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400363 break;
364 }
John Stiles7591d4b2021-09-13 13:32:06 -0400365 case Expression::Kind::kLiteral: {
366 const Literal& l = e->as<Literal>();
367 if (l.type().isFloat()) {
368 float value = l.floatValue();
369 int32_t floatBits;
370 memcpy(&floatBits, &value, sizeof(floatBits));
371 this->writeCommand(Rehydrator::kFloatLiteral_Command);
372 this->write(l.type());
373 this->writeS32(floatBits);
374 } else if (l.type().isBoolean()) {
375 this->writeCommand(Rehydrator::kBoolLiteral_Command);
376 this->writeU8(l.boolValue());
377 } else {
378 SkASSERT(l.type().isInteger());
379 this->writeCommand(Rehydrator::kIntLiteral_Command);
380 this->write(l.type());
381 this->writeS32(l.intValue());
382 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400383 break;
384 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400385 case Expression::Kind::kPostfix: {
John Stiles81365af2020-08-18 09:24:00 -0400386 const PostfixExpression& p = e->as<PostfixExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400387 this->writeCommand(Rehydrator::kPostfix_Command);
John Stiles45990502021-02-16 10:55:27 -0500388 this->writeU8((int) p.getOperator().kind());
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400389 this->write(p.operand().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400390 break;
391 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400392 case Expression::Kind::kPrefix: {
John Stiles81365af2020-08-18 09:24:00 -0400393 const PrefixExpression& p = e->as<PrefixExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400394 this->writeCommand(Rehydrator::kPrefix_Command);
John Stiles45990502021-02-16 10:55:27 -0500395 this->writeU8((int) p.getOperator().kind());
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400396 this->write(p.operand().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400397 break;
398 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400399 case Expression::Kind::kSetting: {
John Stiles81365af2020-08-18 09:24:00 -0400400 const Setting& s = e->as<Setting>();
John Stilese1589a12020-10-08 13:56:46 -0400401 this->writeCommand(Rehydrator::kSetting_Command);
Ethan Nicholas01ec7e82020-10-08 12:10:12 -0400402 this->write(s.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400403 break;
404 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400405 case Expression::Kind::kSwizzle: {
John Stiles81365af2020-08-18 09:24:00 -0400406 const Swizzle& s = e->as<Swizzle>();
John Stilese1589a12020-10-08 13:56:46 -0400407 this->writeCommand(Rehydrator::kSwizzle_Command);
Ethan Nicholas6b4d5812020-10-12 16:11:51 -0400408 this->write(s.base().get());
409 this->writeU8(s.components().size());
410 for (int c : s.components()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400411 this->writeU8(c);
412 }
413 break;
414 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400415 case Expression::Kind::kTernary: {
John Stiles81365af2020-08-18 09:24:00 -0400416 const TernaryExpression& t = e->as<TernaryExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400417 this->writeCommand(Rehydrator::kTernary_Command);
Ethan Nicholasdd218162020-10-08 05:48:01 -0400418 this->write(t.test().get());
419 this->write(t.ifTrue().get());
420 this->write(t.ifFalse().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400421 break;
422 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400423 case Expression::Kind::kVariableReference: {
John Stiles81365af2020-08-18 09:24:00 -0400424 const VariableReference& v = e->as<VariableReference>();
John Stilese1589a12020-10-08 13:56:46 -0400425 this->writeCommand(Rehydrator::kVariableReference_Command);
Ethan Nicholas78686922020-10-08 06:46:27 -0400426 this->writeId(v.variable());
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400427 this->writeU8((int8_t) v.refKind());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400428 break;
429 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400430 case Expression::Kind::kFunctionReference:
Brian Osman3099f792021-09-01 13:12:16 -0400431 case Expression::Kind::kMethodReference:
Ethan Nicholas549c6b82021-06-25 12:31:44 -0400432 case Expression::Kind::kPoison:
Ethan Nicholase6592142020-09-08 10:22:09 -0400433 case Expression::Kind::kTypeReference:
John Stilese1589a12020-10-08 13:56:46 -0400434 SkDEBUGFAIL("this expression shouldn't appear in finished code");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400435 break;
436 }
437 } else {
John Stilese1589a12020-10-08 13:56:46 -0400438 this->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400439 }
440}
441
442void Dehydrator::write(const Statement* s) {
443 if (s) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400444 switch (s->kind()) {
445 case Statement::Kind::kBlock: {
John Stiles26f98502020-08-18 09:30:51 -0400446 const Block& b = s->as<Block>();
John Stilese1589a12020-10-08 13:56:46 -0400447 this->writeCommand(Rehydrator::kBlock_Command);
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400448 AutoDehydratorSymbolTable symbols(this, b.symbolTable());
449 this->writeU8(b.children().size());
450 for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
John Stilesf621e232020-08-25 13:33:02 -0400451 this->write(blockStmt.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400452 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400453 this->writeU8(b.isScope());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400454 break;
455 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400456 case Statement::Kind::kBreak:
John Stilese1589a12020-10-08 13:56:46 -0400457 this->writeCommand(Rehydrator::kBreak_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400458 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400459 case Statement::Kind::kContinue:
John Stilese1589a12020-10-08 13:56:46 -0400460 this->writeCommand(Rehydrator::kContinue_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400461 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400462 case Statement::Kind::kDiscard:
John Stilese1589a12020-10-08 13:56:46 -0400463 this->writeCommand(Rehydrator::kDiscard_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400464 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400465 case Statement::Kind::kDo: {
John Stiles26f98502020-08-18 09:30:51 -0400466 const DoStatement& d = s->as<DoStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400467 this->writeCommand(Rehydrator::kDo_Command);
Ethan Nicholas1fd61162020-09-28 13:14:19 -0400468 this->write(d.statement().get());
469 this->write(d.test().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400470 break;
471 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400472 case Statement::Kind::kExpression: {
John Stiles26f98502020-08-18 09:30:51 -0400473 const ExpressionStatement& e = s->as<ExpressionStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400474 this->writeCommand(Rehydrator::kExpressionStatement_Command);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400475 this->write(e.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400476 break;
477 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400478 case Statement::Kind::kFor: {
John Stiles26f98502020-08-18 09:30:51 -0400479 const ForStatement& f = s->as<ForStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400480 this->writeCommand(Rehydrator::kFor_Command);
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400481 this->write(f.initializer().get());
482 this->write(f.test().get());
483 this->write(f.next().get());
484 this->write(f.statement().get());
John Stiles7c3515b2020-10-16 18:38:39 -0400485 this->write(*f.symbols());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400486 break;
487 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400488 case Statement::Kind::kIf: {
John Stiles26f98502020-08-18 09:30:51 -0400489 const IfStatement& i = s->as<IfStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400490 this->writeCommand(Rehydrator::kIf_Command);
Ethan Nicholas8c44eca2020-10-07 16:47:09 -0400491 this->writeU8(i.isStatic());
492 this->write(i.test().get());
493 this->write(i.ifTrue().get());
494 this->write(i.ifFalse().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400495 break;
496 }
John Stiles98c1f822020-09-09 14:18:53 -0400497 case Statement::Kind::kInlineMarker: {
498 const InlineMarker& i = s->as<InlineMarker>();
John Stilese1589a12020-10-08 13:56:46 -0400499 this->writeCommand(Rehydrator::kInlineMarker_Command);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400500 this->writeId(&i.function());
John Stiles98c1f822020-09-09 14:18:53 -0400501 break;
502 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400503 case Statement::Kind::kNop:
John Stilese1589a12020-10-08 13:56:46 -0400504 SkDEBUGFAIL("unexpected--nop statement in finished code");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400505 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400506 case Statement::Kind::kReturn: {
John Stiles26f98502020-08-18 09:30:51 -0400507 const ReturnStatement& r = s->as<ReturnStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400508 this->writeCommand(Rehydrator::kReturn_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400509 this->write(r.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400510 break;
511 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400512 case Statement::Kind::kSwitch: {
John Stiles26f98502020-08-18 09:30:51 -0400513 const SwitchStatement& ss = s->as<SwitchStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400514 this->writeCommand(Rehydrator::kSwitch_Command);
Ethan Nicholas01b05e52020-10-22 15:53:41 -0400515 this->writeU8(ss.isStatic());
516 AutoDehydratorSymbolTable symbols(this, ss.symbols());
517 this->write(ss.value().get());
John Stiles2d4f9592020-10-30 10:29:12 -0400518 this->writeU8(ss.cases().size());
John Stilesb23a64b2021-03-11 08:27:59 -0500519 for (const std::unique_ptr<Statement>& stmt : ss.cases()) {
520 const SwitchCase& sc = stmt->as<SwitchCase>();
521 this->write(sc.value().get());
522 this->write(sc.statement().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400523 }
524 break;
525 }
Brian Osman9eb848a2020-09-11 15:53:40 -0400526 case Statement::Kind::kSwitchCase:
John Stilese1589a12020-10-08 13:56:46 -0400527 SkDEBUGFAIL("SwitchCase statements shouldn't appear here");
Brian Osman9eb848a2020-09-11 15:53:40 -0400528 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400529 case Statement::Kind::kVarDeclaration: {
John Stiles26f98502020-08-18 09:30:51 -0400530 const VarDeclaration& v = s->as<VarDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400531 this->writeCommand(Rehydrator::kVarDeclaration_Command);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400532 this->writeU16(this->symbolId(&v.var()));
533 this->write(v.baseType());
John Stiles62a56462020-12-03 10:41:58 -0500534 this->writeS8(v.arraySize());
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400535 this->write(v.value().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400536 break;
537 }
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400538 }
539 } else {
John Stilese1589a12020-10-08 13:56:46 -0400540 this->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400541 }
542}
543
544void Dehydrator::write(const ProgramElement& e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400545 switch (e.kind()) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400546 case ProgramElement::Kind::kExtension:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400547 SkASSERT(false);
548 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400549 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -0400550 const FunctionDefinition& f = e.as<FunctionDefinition>();
John Stilese1589a12020-10-08 13:56:46 -0400551 this->writeCommand(Rehydrator::kFunctionDefinition_Command);
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400552 this->writeU16(this->symbolId(&f.declaration()));
553 this->write(f.body().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400554 break;
555 }
John Stiles569249b2020-11-03 12:18:22 -0500556 case ProgramElement::Kind::kFunctionPrototype: {
557 // We don't need to emit function prototypes into the dehydrated data, because we don't
558 // ever need to re-emit the intrinsics files as raw GLSL/Metal. As long as the symbols
559 // exist in the symbol table, we're in good shape.
560 break;
561 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400562 case ProgramElement::Kind::kInterfaceBlock: {
John Stiles3dc0da62020-08-19 17:48:31 -0400563 const InterfaceBlock& i = e.as<InterfaceBlock>();
John Stilese1589a12020-10-08 13:56:46 -0400564 this->writeCommand(Rehydrator::kInterfaceBlock_Command);
Ethan Nicholaseaf47882020-10-15 10:10:08 -0400565 this->write(i.variable());
566 this->write(i.typeName());
567 this->write(i.instanceName());
John Stilesd39aec02020-12-03 10:42:26 -0500568 this->writeS8(i.arraySize());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400569 break;
570 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400571 case ProgramElement::Kind::kModifiers:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400572 SkASSERT(false);
573 break;
John Stilesdc75a972020-11-25 16:24:55 -0500574 case ProgramElement::Kind::kStructDefinition: {
575 const StructDefinition& structDef = e.as<StructDefinition>();
576 this->writeCommand(Rehydrator::kStructDefinition_Command);
577 this->write(structDef.type());
578 break;
579 }
Brian Osmanc0213602020-10-06 14:43:32 -0400580 case ProgramElement::Kind::kGlobalVar: {
581 const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400582 this->writeCommand(Rehydrator::kVarDeclarations_Command);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400583 this->write(v.declaration().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400584 break;
585 }
586 }
587}
588
589void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
John Stilese1589a12020-10-08 13:56:46 -0400590 this->writeCommand(Rehydrator::kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400591 for (const auto& e : elements) {
592 this->write(*e);
593 }
John Stiles1ea7f542020-11-02 13:07:23 -0500594 this->writeCommand(Rehydrator::kElementsComplete_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400595}
596
597void Dehydrator::finish(OutputStream& out) {
John Stilese1589a12020-10-08 13:56:46 -0400598 String stringBuffer = fStringBuffer.str();
599 String commandBuffer = fBody.str();
600
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400601 out.write16(fStringBuffer.str().size());
John Stilese1589a12020-10-08 13:56:46 -0400602 fStringBufferStart = 2;
603 out.writeString(stringBuffer);
604 fCommandStart = fStringBufferStart + stringBuffer.size();
605 out.writeString(commandBuffer);
606}
607
608const char* Dehydrator::prefixAtOffset(size_t byte) {
609 if (byte >= fCommandStart) {
610 return fCommandBreaks.contains(byte - fCommandStart) ? "\n" : "";
611 }
612 if (byte >= fStringBufferStart) {
613 return fStringBreaks.contains(byte - fStringBufferStart) ? "\n" : "";
614 }
615 return "";
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400616}
617
618} // namespace
619
620#endif