blob: 66f9cc9632e2610b380dd7abafc89786855c3f0e [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"
John Stilesdc75a972020-11-25 16:24:55 -050039#include "src/sksl/ir/SkSLStructDefinition.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040040#include "src/sksl/ir/SkSLSwitchCase.h"
41#include "src/sksl/ir/SkSLSwitchStatement.h"
42#include "src/sksl/ir/SkSLSwizzle.h"
43#include "src/sksl/ir/SkSLSymbol.h"
John Stiles49a547f2020-10-06 16:14:37 -040044#include "src/sksl/ir/SkSLSymbolAlias.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040045#include "src/sksl/ir/SkSLSymbolTable.h"
46#include "src/sksl/ir/SkSLTernaryExpression.h"
47#include "src/sksl/ir/SkSLUnresolvedFunction.h"
48#include "src/sksl/ir/SkSLVarDeclarations.h"
Ethan Nicholasc18bb512020-07-28 14:46:53 -040049#include "src/sksl/ir/SkSLVariable.h"
50#include "src/sksl/ir/SkSLWhileStatement.h"
51
52#ifdef SKSL_STANDALONE
53
54namespace SkSL {
55
56static constexpr int HEADER_SIZE = 2;
57
58class AutoDehydratorSymbolTable {
59public:
60 AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols)
61 : fDehydrator(dehydrator) {
62 dehydrator->fSymbolMap.emplace_back();
63 if (symbols) {
64 dehydrator->write(*symbols);
65 } else {
John Stilese1589a12020-10-08 13:56:46 -040066 dehydrator->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040067 }
68 }
69
70 ~AutoDehydratorSymbolTable() {
71 fDehydrator->fSymbolMap.pop_back();
72 }
73
74private:
75 Dehydrator* fDehydrator;
76};
77
78void Dehydrator::write(Layout l) {
79 if (l == Layout()) {
John Stilese1589a12020-10-08 13:56:46 -040080 this->writeCommand(Rehydrator::kDefaultLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040081 } else if (l == Layout::builtin(l.fBuiltin)) {
John Stilese1589a12020-10-08 13:56:46 -040082 this->writeCommand(Rehydrator::kBuiltinLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040083 this->writeS16(l.fBuiltin);
84 } else {
John Stilese1589a12020-10-08 13:56:46 -040085 this->writeCommand(Rehydrator::kLayout_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -040086 fBody.write32(l.fFlags);
87 this->writeS8(l.fLocation);
88 this->writeS8(l.fOffset);
89 this->writeS8(l.fBinding);
90 this->writeS8(l.fIndex);
91 this->writeS8(l.fSet);
92 this->writeS16(l.fBuiltin);
93 this->writeS8(l.fInputAttachmentIndex);
94 this->writeS8((int) l.fFormat);
95 this->writeS8(l.fPrimitive);
96 this->writeS8(l.fMaxVertices);
97 this->writeS8(l.fInvocations);
98 this->write(l.fMarker);
99 this->write(l.fWhen);
100 this->writeS8(l.fKey);
101 this->writeS8((int) l.fCType);
102 }
103}
104
105void Dehydrator::write(Modifiers m) {
106 if (m == Modifiers()) {
John Stilese1589a12020-10-08 13:56:46 -0400107 this->writeCommand(Rehydrator::kDefaultModifiers_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400108 } else {
109 if (m.fFlags <= 255) {
John Stilese1589a12020-10-08 13:56:46 -0400110 this->writeCommand(Rehydrator::kModifiers8Bit_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400111 this->write(m.fLayout);
112 this->writeU8(m.fFlags);
113 } else {
John Stilese1589a12020-10-08 13:56:46 -0400114 this->writeCommand(Rehydrator::kModifiers_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400115 this->write(m.fLayout);
116 this->writeS32(m.fFlags);
117 }
118 }
119}
120
121void Dehydrator::write(StringFragment s) {
122 this->write(String(s));
123}
124
125void Dehydrator::write(String s) {
126 auto found = fStrings.find(s);
127 int offset;
128 if (found == fStrings.end()) {
129 offset = fStringBuffer.str().length() + HEADER_SIZE;
130 fStrings.insert({ s, offset });
131 SkASSERT(s.length() <= 255);
John Stilese1589a12020-10-08 13:56:46 -0400132 fStringBreaks.add(fStringBuffer.bytesWritten());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400133 fStringBuffer.write8(s.length());
134 fStringBuffer.writeString(s);
135 } else {
136 offset = found->second;
137 }
138 this->writeU16(offset);
139}
140
141void Dehydrator::write(const Symbol& s) {
142 uint16_t id = this->symbolId(&s, false);
143 if (id) {
John Stilese1589a12020-10-08 13:56:46 -0400144 this->writeCommand(Rehydrator::kSymbolRef_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400145 this->writeU16(id);
146 return;
147 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400148 switch (s.kind()) {
149 case Symbol::Kind::kFunctionDeclaration: {
John Stiles17c5b702020-08-18 10:40:03 -0400150 const FunctionDeclaration& f = s.as<FunctionDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400151 this->writeCommand(Rehydrator::kFunctionDeclaration_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400152 this->writeId(&f);
Ethan Nicholased84b732020-10-08 11:45:44 -0400153 this->write(f.modifiers());
Ethan Nicholase2c49992020-10-05 11:49:11 -0400154 this->write(f.name());
Ethan Nicholased84b732020-10-08 11:45:44 -0400155 this->writeU8(f.parameters().size());
156 for (const Variable* p : f.parameters()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400157 this->writeU16(this->symbolId(p));
158 }
Ethan Nicholased84b732020-10-08 11:45:44 -0400159 this->write(f.returnType());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400160 break;
161 }
John Stiles49a547f2020-10-06 16:14:37 -0400162 case Symbol::Kind::kSymbolAlias: {
163 const SymbolAlias& alias = s.as<SymbolAlias>();
John Stilese1589a12020-10-08 13:56:46 -0400164 this->writeCommand(Rehydrator::kSymbolAlias_Command);
John Stiles49a547f2020-10-06 16:14:37 -0400165 this->writeId(&alias);
166 this->write(alias.name());
167 this->write(*alias.origSymbol());
168 break;
169 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400170 case Symbol::Kind::kUnresolvedFunction: {
John Stiles17c5b702020-08-18 10:40:03 -0400171 const UnresolvedFunction& f = s.as<UnresolvedFunction>();
John Stilese1589a12020-10-08 13:56:46 -0400172 this->writeCommand(Rehydrator::kUnresolvedFunction_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400173 this->writeId(&f);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400174 this->writeU8(f.functions().size());
175 for (const FunctionDeclaration* funcDecl : f.functions()) {
John Stilesf621e232020-08-25 13:33:02 -0400176 this->write(*funcDecl);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400177 }
178 break;
179 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400180 case Symbol::Kind::kType: {
John Stiles17c5b702020-08-18 10:40:03 -0400181 const Type& t = s.as<Type>();
Ethan Nicholase6592142020-09-08 10:22:09 -0400182 switch (t.typeKind()) {
183 case Type::TypeKind::kArray:
John Stilese1589a12020-10-08 13:56:46 -0400184 this->writeCommand(Rehydrator::kArrayType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400185 this->writeId(&t);
186 this->write(t.componentType());
Brian Osmane8c26082020-10-01 17:22:45 -0400187 this->writeS8(t.columns());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400188 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400189 case Type::TypeKind::kEnum:
John Stilese1589a12020-10-08 13:56:46 -0400190 this->writeCommand(Rehydrator::kEnumType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400191 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400192 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400193 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400194 case Type::TypeKind::kNullable:
John Stilese1589a12020-10-08 13:56:46 -0400195 this->writeCommand(Rehydrator::kNullableType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400196 this->writeId(&t);
197 this->write(t.componentType());
198 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400199 case Type::TypeKind::kStruct:
John Stilese1589a12020-10-08 13:56:46 -0400200 this->writeCommand(Rehydrator::kStructType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400201 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400202 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400203 this->writeU8(t.fields().size());
204 for (const Type::Field& f : t.fields()) {
205 this->write(f.fModifiers);
206 this->write(f.fName);
207 this->write(*f.fType);
208 }
209 break;
210 default:
John Stilese1589a12020-10-08 13:56:46 -0400211 this->writeCommand(Rehydrator::kSystemType_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400212 this->writeId(&t);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400213 this->write(t.name());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400214 }
215 break;
216 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400217 case Symbol::Kind::kVariable: {
John Stiles17c5b702020-08-18 10:40:03 -0400218 const Variable& v = s.as<Variable>();
John Stilese1589a12020-10-08 13:56:46 -0400219 this->writeCommand(Rehydrator::kVariable_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400220 this->writeId(&v);
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400221 this->write(v.modifiers());
Ethan Nicholase2c49992020-10-05 11:49:11 -0400222 this->write(v.name());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400223 this->write(v.type());
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400224 this->writeU8((int8_t) v.storage());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400225 break;
226 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400227 case Symbol::Kind::kField: {
John Stiles17c5b702020-08-18 10:40:03 -0400228 const Field& f = s.as<Field>();
John Stilese1589a12020-10-08 13:56:46 -0400229 this->writeCommand(Rehydrator::kField_Command);
Ethan Nicholase2c49992020-10-05 11:49:11 -0400230 this->writeU16(this->symbolId(&f.owner()));
231 this->writeU8(f.fieldIndex());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400232 break;
233 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400234 case Symbol::Kind::kExternal:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400235 SkASSERT(false);
236 break;
237 }
238}
239
240void Dehydrator::write(const SymbolTable& symbols) {
John Stilese1589a12020-10-08 13:56:46 -0400241 this->writeCommand(Rehydrator::kSymbolTable_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400242 this->writeU16(symbols.fOwnedSymbols.size());
243 for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) {
244 this->write(*s);
245 }
John Stilesefe767d2020-10-09 12:48:04 -0400246 this->writeU16(symbols.fSymbols.count());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400247 std::map<StringFragment, const Symbol*> ordered;
John Stilesefe767d2020-10-09 12:48:04 -0400248 symbols.foreach([&](StringFragment name, const Symbol* symbol) {
249 ordered.insert({name, symbol});
250 });
Ethan Nicholas7154b742020-07-31 13:18:02 -0400251 for (std::pair<StringFragment, const Symbol*> p : ordered) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400252 bool found = false;
253 for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) {
254 if (symbols.fOwnedSymbols[i].get() == p.second) {
John Stilese1589a12020-10-08 13:56:46 -0400255 fCommandBreaks.add(fBody.bytesWritten());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400256 this->writeU16(i);
257 found = true;
258 break;
259 }
260 }
261 SkASSERT(found);
262 }
263}
264
265void Dehydrator::write(const Expression* e) {
266 if (e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400267 switch (e->kind()) {
268 case Expression::Kind::kBinary: {
John Stiles81365af2020-08-18 09:24:00 -0400269 const BinaryExpression& b = e->as<BinaryExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400270 this->writeCommand(Rehydrator::kBinary_Command);
John Stiles2d4f9592020-10-30 10:29:12 -0400271 this->write(b.left().get());
Ethan Nicholasc8d9c8e2020-09-22 15:05:37 -0400272 this->writeU8((int) b.getOperator());
John Stiles2d4f9592020-10-30 10:29:12 -0400273 this->write(b.right().get());
Ethan Nicholas30d30222020-09-11 12:27:26 -0400274 this->write(b.type());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400275 break;
276 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400277 case Expression::Kind::kBoolLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400278 const BoolLiteral& b = e->as<BoolLiteral>();
John Stilese1589a12020-10-08 13:56:46 -0400279 this->writeCommand(Rehydrator::kBoolLiteral_Command);
Ethan Nicholas59d660c2020-09-28 09:18:15 -0400280 this->writeU8(b.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400281 break;
282 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400283 case Expression::Kind::kConstructor: {
John Stiles81365af2020-08-18 09:24:00 -0400284 const Constructor& c = e->as<Constructor>();
John Stilese1589a12020-10-08 13:56:46 -0400285 this->writeCommand(Rehydrator::kConstructor_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400286 this->write(c.type());
Ethan Nicholasf70f0442020-09-29 12:41:35 -0400287 this->writeU8(c.arguments().size());
288 for (const auto& a : c.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400289 this->write(a.get());
290 }
291 break;
292 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400293 case Expression::Kind::kExternalFunctionCall:
294 case Expression::Kind::kExternalValue:
John Stilese1589a12020-10-08 13:56:46 -0400295 SkDEBUGFAIL("unimplemented--not expected to be used from within an include file");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400296 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400297 case Expression::Kind::kFieldAccess: {
John Stiles81365af2020-08-18 09:24:00 -0400298 const FieldAccess& f = e->as<FieldAccess>();
John Stilese1589a12020-10-08 13:56:46 -0400299 this->writeCommand(Rehydrator::kFieldAccess_Command);
Ethan Nicholas7a95b202020-10-09 11:55:40 -0400300 this->write(f.base().get());
301 this->writeU8(f.fieldIndex());
302 this->writeU8((int8_t) f.ownerKind());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400303 break;
304 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400305 case Expression::Kind::kFloatLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400306 const FloatLiteral& f = e->as<FloatLiteral>();
John Stilese1589a12020-10-08 13:56:46 -0400307 this->writeCommand(Rehydrator::kFloatLiteral_Command);
Brian Osmanfb964a42020-11-18 10:45:52 -0500308 this->write(f.type());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400309 FloatIntUnion u;
Ethan Nicholasa3f22f12020-10-01 12:13:17 -0400310 u.fFloat = f.value();
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400311 this->writeS32(u.fInt);
312 break;
313 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400314 case Expression::Kind::kFunctionCall: {
John Stiles81365af2020-08-18 09:24:00 -0400315 const FunctionCall& f = e->as<FunctionCall>();
John Stilese1589a12020-10-08 13:56:46 -0400316 this->writeCommand(Rehydrator::kFunctionCall_Command);
Ethan Nicholas30d30222020-09-11 12:27:26 -0400317 this->write(f.type());
Ethan Nicholas0dec9922020-10-05 15:51:52 -0400318 this->writeId(&f.function());
319 this->writeU8(f.arguments().size());
320 for (const auto& a : f.arguments()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400321 this->write(a.get());
322 }
323 break;
324 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400325 case Expression::Kind::kIndex: {
John Stiles81365af2020-08-18 09:24:00 -0400326 const IndexExpression& i = e->as<IndexExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400327 this->writeCommand(Rehydrator::kIndex_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400328 this->write(i.base().get());
329 this->write(i.index().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400330 break;
331 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400332 case Expression::Kind::kIntLiteral: {
John Stiles81365af2020-08-18 09:24:00 -0400333 const IntLiteral& i = e->as<IntLiteral>();
John Stilese1589a12020-10-08 13:56:46 -0400334 this->writeCommand(Rehydrator::kIntLiteral_Command);
Brian Osmanfb964a42020-11-18 10:45:52 -0500335 this->write(i.type());
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400336 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400337 break;
338 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400339 case Expression::Kind::kNullLiteral:
John Stilese1589a12020-10-08 13:56:46 -0400340 this->writeCommand(Rehydrator::kNullLiteral_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400341 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400342 case Expression::Kind::kPostfix: {
John Stiles81365af2020-08-18 09:24:00 -0400343 const PostfixExpression& p = e->as<PostfixExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400344 this->writeCommand(Rehydrator::kPostfix_Command);
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400345 this->writeU8((int) p.getOperator());
346 this->write(p.operand().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400347 break;
348 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400349 case Expression::Kind::kPrefix: {
John Stiles81365af2020-08-18 09:24:00 -0400350 const PrefixExpression& p = e->as<PrefixExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400351 this->writeCommand(Rehydrator::kPrefix_Command);
Ethan Nicholas444ccc62020-10-09 10:16:22 -0400352 this->writeU8((int) p.getOperator());
353 this->write(p.operand().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400354 break;
355 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400356 case Expression::Kind::kSetting: {
John Stiles81365af2020-08-18 09:24:00 -0400357 const Setting& s = e->as<Setting>();
John Stilese1589a12020-10-08 13:56:46 -0400358 this->writeCommand(Rehydrator::kSetting_Command);
Ethan Nicholas01ec7e82020-10-08 12:10:12 -0400359 this->write(s.name());
360 this->write(s.type());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400361 break;
362 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400363 case Expression::Kind::kSwizzle: {
John Stiles81365af2020-08-18 09:24:00 -0400364 const Swizzle& s = e->as<Swizzle>();
John Stilese1589a12020-10-08 13:56:46 -0400365 this->writeCommand(Rehydrator::kSwizzle_Command);
Ethan Nicholas6b4d5812020-10-12 16:11:51 -0400366 this->write(s.base().get());
367 this->writeU8(s.components().size());
368 for (int c : s.components()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400369 this->writeU8(c);
370 }
371 break;
372 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400373 case Expression::Kind::kTernary: {
John Stiles81365af2020-08-18 09:24:00 -0400374 const TernaryExpression& t = e->as<TernaryExpression>();
John Stilese1589a12020-10-08 13:56:46 -0400375 this->writeCommand(Rehydrator::kTernary_Command);
Ethan Nicholasdd218162020-10-08 05:48:01 -0400376 this->write(t.test().get());
377 this->write(t.ifTrue().get());
378 this->write(t.ifFalse().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400379 break;
380 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400381 case Expression::Kind::kVariableReference: {
John Stiles81365af2020-08-18 09:24:00 -0400382 const VariableReference& v = e->as<VariableReference>();
John Stilese1589a12020-10-08 13:56:46 -0400383 this->writeCommand(Rehydrator::kVariableReference_Command);
Ethan Nicholas78686922020-10-08 06:46:27 -0400384 this->writeId(v.variable());
Ethan Nicholas453f67f2020-10-09 10:43:45 -0400385 this->writeU8((int8_t) v.refKind());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400386 break;
387 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400388 case Expression::Kind::kFunctionReference:
389 case Expression::Kind::kTypeReference:
390 case Expression::Kind::kDefined:
John Stilese1589a12020-10-08 13:56:46 -0400391 SkDEBUGFAIL("this expression shouldn't appear in finished code");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400392 break;
393 }
394 } else {
John Stilese1589a12020-10-08 13:56:46 -0400395 this->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400396 }
397}
398
399void Dehydrator::write(const Statement* s) {
400 if (s) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400401 switch (s->kind()) {
402 case Statement::Kind::kBlock: {
John Stiles26f98502020-08-18 09:30:51 -0400403 const Block& b = s->as<Block>();
John Stilese1589a12020-10-08 13:56:46 -0400404 this->writeCommand(Rehydrator::kBlock_Command);
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400405 AutoDehydratorSymbolTable symbols(this, b.symbolTable());
406 this->writeU8(b.children().size());
407 for (const std::unique_ptr<Statement>& blockStmt : b.children()) {
John Stilesf621e232020-08-25 13:33:02 -0400408 this->write(blockStmt.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400409 }
Ethan Nicholas7bd60432020-09-25 14:31:59 -0400410 this->writeU8(b.isScope());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400411 break;
412 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400413 case Statement::Kind::kBreak:
John Stilese1589a12020-10-08 13:56:46 -0400414 this->writeCommand(Rehydrator::kBreak_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400415 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400416 case Statement::Kind::kContinue:
John Stilese1589a12020-10-08 13:56:46 -0400417 this->writeCommand(Rehydrator::kContinue_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400418 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400419 case Statement::Kind::kDiscard:
John Stilese1589a12020-10-08 13:56:46 -0400420 this->writeCommand(Rehydrator::kDiscard_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400421 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400422 case Statement::Kind::kDo: {
John Stiles26f98502020-08-18 09:30:51 -0400423 const DoStatement& d = s->as<DoStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400424 this->writeCommand(Rehydrator::kDo_Command);
Ethan Nicholas1fd61162020-09-28 13:14:19 -0400425 this->write(d.statement().get());
426 this->write(d.test().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400427 break;
428 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400429 case Statement::Kind::kExpression: {
John Stiles26f98502020-08-18 09:30:51 -0400430 const ExpressionStatement& e = s->as<ExpressionStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400431 this->writeCommand(Rehydrator::kExpressionStatement_Command);
Ethan Nicholasd503a5a2020-09-30 09:29:55 -0400432 this->write(e.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400433 break;
434 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400435 case Statement::Kind::kFor: {
John Stiles26f98502020-08-18 09:30:51 -0400436 const ForStatement& f = s->as<ForStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400437 this->writeCommand(Rehydrator::kFor_Command);
Ethan Nicholas0d31ed52020-10-05 14:47:09 -0400438 this->write(f.initializer().get());
439 this->write(f.test().get());
440 this->write(f.next().get());
441 this->write(f.statement().get());
John Stiles7c3515b2020-10-16 18:38:39 -0400442 this->write(*f.symbols());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400443 break;
444 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400445 case Statement::Kind::kIf: {
John Stiles26f98502020-08-18 09:30:51 -0400446 const IfStatement& i = s->as<IfStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400447 this->writeCommand(Rehydrator::kIf_Command);
Ethan Nicholas8c44eca2020-10-07 16:47:09 -0400448 this->writeU8(i.isStatic());
449 this->write(i.test().get());
450 this->write(i.ifTrue().get());
451 this->write(i.ifFalse().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400452 break;
453 }
John Stiles98c1f822020-09-09 14:18:53 -0400454 case Statement::Kind::kInlineMarker: {
455 const InlineMarker& i = s->as<InlineMarker>();
John Stilese1589a12020-10-08 13:56:46 -0400456 this->writeCommand(Rehydrator::kInlineMarker_Command);
Ethan Nicholasceb62142020-10-09 16:51:18 -0400457 this->writeId(&i.function());
John Stiles98c1f822020-09-09 14:18:53 -0400458 break;
459 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400460 case Statement::Kind::kNop:
John Stilese1589a12020-10-08 13:56:46 -0400461 SkDEBUGFAIL("unexpected--nop statement in finished code");
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400462 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400463 case Statement::Kind::kReturn: {
John Stiles26f98502020-08-18 09:30:51 -0400464 const ReturnStatement& r = s->as<ReturnStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400465 this->writeCommand(Rehydrator::kReturn_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400466 this->write(r.expression().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400467 break;
468 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400469 case Statement::Kind::kSwitch: {
John Stiles26f98502020-08-18 09:30:51 -0400470 const SwitchStatement& ss = s->as<SwitchStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400471 this->writeCommand(Rehydrator::kSwitch_Command);
Ethan Nicholas01b05e52020-10-22 15:53:41 -0400472 this->writeU8(ss.isStatic());
473 AutoDehydratorSymbolTable symbols(this, ss.symbols());
474 this->write(ss.value().get());
John Stiles2d4f9592020-10-30 10:29:12 -0400475 this->writeU8(ss.cases().size());
476 for (const std::unique_ptr<SwitchCase>& sc : ss.cases()) {
477 this->write(sc->value().get());
478 this->writeU8(sc->statements().size());
479 for (const std::unique_ptr<Statement>& stmt : sc->statements()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400480 this->write(stmt.get());
481 }
482 }
483 break;
484 }
Brian Osman9eb848a2020-09-11 15:53:40 -0400485 case Statement::Kind::kSwitchCase:
John Stilese1589a12020-10-08 13:56:46 -0400486 SkDEBUGFAIL("SwitchCase statements shouldn't appear here");
Brian Osman9eb848a2020-09-11 15:53:40 -0400487 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400488 case Statement::Kind::kVarDeclaration: {
John Stiles26f98502020-08-18 09:30:51 -0400489 const VarDeclaration& v = s->as<VarDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400490 this->writeCommand(Rehydrator::kVarDeclaration_Command);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400491 this->writeU16(this->symbolId(&v.var()));
492 this->write(v.baseType());
John Stiles2d4f9592020-10-30 10:29:12 -0400493 this->writeU8(v.sizes().count());
494 for (const std::unique_ptr<Expression>& size : v.sizes()) {
495 this->write(size.get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400496 }
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400497 this->write(v.value().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400498 break;
499 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400500 case Statement::Kind::kWhile: {
John Stiles26f98502020-08-18 09:30:51 -0400501 const WhileStatement& w = s->as<WhileStatement>();
John Stilese1589a12020-10-08 13:56:46 -0400502 this->writeCommand(Rehydrator::kWhile_Command);
Ethan Nicholas2a4952d2020-10-08 15:35:56 -0400503 this->write(w.test().get());
504 this->write(w.statement().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400505 break;
506 }
507 }
508 } else {
John Stilese1589a12020-10-08 13:56:46 -0400509 this->writeCommand(Rehydrator::kVoid_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400510 }
511}
512
513void Dehydrator::write(const ProgramElement& e) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400514 switch (e.kind()) {
515 case ProgramElement::Kind::kEnum: {
John Stiles3dc0da62020-08-19 17:48:31 -0400516 const Enum& en = e.as<Enum>();
John Stilese1589a12020-10-08 13:56:46 -0400517 this->writeCommand(Rehydrator::kEnum_Command);
Ethan Nicholasd83ded82020-09-29 17:05:54 -0400518 this->write(en.typeName());
519 AutoDehydratorSymbolTable symbols(this, en.symbols());
520 for (const std::unique_ptr<const Symbol>& s : en.symbols()->fOwnedSymbols) {
Ethan Nicholase6592142020-09-08 10:22:09 -0400521 SkASSERT(s->kind() == Symbol::Kind::kVariable);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400522 Variable& v = (Variable&) *s;
Ethan Nicholas041fd0a2020-10-07 16:42:04 -0400523 SkASSERT(v.initialValue());
524 const IntLiteral& i = v.initialValue()->as<IntLiteral>();
Ethan Nicholase96cdd12020-09-28 16:27:18 -0400525 this->writeS32(i.value());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400526 }
527 break;
528 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400529 case ProgramElement::Kind::kExtension:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400530 SkASSERT(false);
531 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400532 case ProgramElement::Kind::kFunction: {
John Stiles3dc0da62020-08-19 17:48:31 -0400533 const FunctionDefinition& f = e.as<FunctionDefinition>();
John Stilese1589a12020-10-08 13:56:46 -0400534 this->writeCommand(Rehydrator::kFunctionDefinition_Command);
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400535 this->writeU16(this->symbolId(&f.declaration()));
536 this->write(f.body().get());
537 this->writeU8(f.referencedIntrinsics().size());
Ethan Nicholas7154b742020-07-31 13:18:02 -0400538 std::set<uint16_t> ordered;
Ethan Nicholas0a5d0962020-10-14 13:33:18 -0400539 for (const FunctionDeclaration* ref : f.referencedIntrinsics()) {
Ethan Nicholas7154b742020-07-31 13:18:02 -0400540 ordered.insert(this->symbolId(ref));
541 }
542 for (uint16_t ref : ordered) {
543 this->writeU16(ref);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400544 }
545 break;
546 }
John Stiles569249b2020-11-03 12:18:22 -0500547 case ProgramElement::Kind::kFunctionPrototype: {
548 // We don't need to emit function prototypes into the dehydrated data, because we don't
549 // ever need to re-emit the intrinsics files as raw GLSL/Metal. As long as the symbols
550 // exist in the symbol table, we're in good shape.
551 break;
552 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400553 case ProgramElement::Kind::kInterfaceBlock: {
John Stiles3dc0da62020-08-19 17:48:31 -0400554 const InterfaceBlock& i = e.as<InterfaceBlock>();
John Stilese1589a12020-10-08 13:56:46 -0400555 this->writeCommand(Rehydrator::kInterfaceBlock_Command);
Ethan Nicholaseaf47882020-10-15 10:10:08 -0400556 this->write(i.variable());
557 this->write(i.typeName());
558 this->write(i.instanceName());
559 this->writeU8(i.sizes().count());
560 for (const auto& s : i.sizes()) {
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400561 this->write(s.get());
562 }
563 break;
564 }
Ethan Nicholase6592142020-09-08 10:22:09 -0400565 case ProgramElement::Kind::kModifiers:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400566 SkASSERT(false);
567 break;
Ethan Nicholase6592142020-09-08 10:22:09 -0400568 case ProgramElement::Kind::kSection:
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400569 SkASSERT(false);
570 break;
John Stilesdc75a972020-11-25 16:24:55 -0500571 case ProgramElement::Kind::kStructDefinition: {
572 const StructDefinition& structDef = e.as<StructDefinition>();
573 this->writeCommand(Rehydrator::kStructDefinition_Command);
574 this->write(structDef.type());
575 break;
576 }
Brian Osmanc0213602020-10-06 14:43:32 -0400577 case ProgramElement::Kind::kGlobalVar: {
578 const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>();
John Stilese1589a12020-10-08 13:56:46 -0400579 this->writeCommand(Rehydrator::kVarDeclarations_Command);
Ethan Nicholasc51f33e2020-10-13 13:49:44 -0400580 this->write(v.declaration().get());
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400581 break;
582 }
583 }
584}
585
586void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) {
John Stilese1589a12020-10-08 13:56:46 -0400587 this->writeCommand(Rehydrator::kElements_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400588 for (const auto& e : elements) {
589 this->write(*e);
590 }
John Stiles1ea7f542020-11-02 13:07:23 -0500591 this->writeCommand(Rehydrator::kElementsComplete_Command);
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400592}
593
594void Dehydrator::finish(OutputStream& out) {
John Stilese1589a12020-10-08 13:56:46 -0400595 String stringBuffer = fStringBuffer.str();
596 String commandBuffer = fBody.str();
597
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400598 out.write16(fStringBuffer.str().size());
John Stilese1589a12020-10-08 13:56:46 -0400599 fStringBufferStart = 2;
600 out.writeString(stringBuffer);
601 fCommandStart = fStringBufferStart + stringBuffer.size();
602 out.writeString(commandBuffer);
603}
604
605const char* Dehydrator::prefixAtOffset(size_t byte) {
606 if (byte >= fCommandStart) {
607 return fCommandBreaks.contains(byte - fCommandStart) ? "\n" : "";
608 }
609 if (byte >= fStringBufferStart) {
610 return fStringBreaks.contains(byte - fStringBufferStart) ? "\n" : "";
611 }
612 return "";
Ethan Nicholasc18bb512020-07-28 14:46:53 -0400613}
614
615} // namespace
616
617#endif