Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 1 | /* |
| 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 Stiles | 810c8cf | 2020-08-26 19:46:27 -0400 | [diff] [blame] | 10 | #include <map> |
| 11 | |
Ethan Nicholas | 24c1772 | 2021-03-09 13:10:59 -0500 | [diff] [blame] | 12 | #include "include/private/SkSLProgramElement.h" |
| 13 | #include "include/private/SkSLStatement.h" |
| 14 | #include "include/private/SkSLSymbol.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 15 | #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 Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 19 | #include "src/sksl/ir/SkSLConstructorArray.h" |
John Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 20 | #include "src/sksl/ir/SkSLConstructorArrayCast.h" |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 21 | #include "src/sksl/ir/SkSLConstructorCompound.h" |
| 22 | #include "src/sksl/ir/SkSLConstructorCompoundCast.h" |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 23 | #include "src/sksl/ir/SkSLConstructorDiagonalMatrix.h" |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 24 | #include "src/sksl/ir/SkSLConstructorMatrixResize.h" |
John Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 25 | #include "src/sksl/ir/SkSLConstructorScalarCast.h" |
John Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 26 | #include "src/sksl/ir/SkSLConstructorSplat.h" |
John Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 27 | #include "src/sksl/ir/SkSLConstructorStruct.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 28 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 29 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 30 | #include "src/sksl/ir/SkSLDoStatement.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 31 | #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 Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 40 | #include "src/sksl/ir/SkSLInlineMarker.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 41 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 42 | #include "src/sksl/ir/SkSLLiteral.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 43 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 44 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 45 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 46 | #include "src/sksl/ir/SkSLSetting.h" |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 47 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 48 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 49 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 50 | #include "src/sksl/ir/SkSLSwizzle.h" |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 51 | #include "src/sksl/ir/SkSLSymbolAlias.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 52 | #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 Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 56 | #include "src/sksl/ir/SkSLVariable.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 57 | |
| 58 | #ifdef SKSL_STANDALONE |
| 59 | |
| 60 | namespace SkSL { |
| 61 | |
| 62 | static constexpr int HEADER_SIZE = 2; |
| 63 | |
| 64 | class AutoDehydratorSymbolTable { |
| 65 | public: |
| 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 Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 72 | dehydrator->writeCommand(Rehydrator::kVoid_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | |
| 76 | ~AutoDehydratorSymbolTable() { |
| 77 | fDehydrator->fSymbolMap.pop_back(); |
| 78 | } |
| 79 | |
| 80 | private: |
| 81 | Dehydrator* fDehydrator; |
| 82 | }; |
| 83 | |
| 84 | void Dehydrator::write(Layout l) { |
| 85 | if (l == Layout()) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 86 | this->writeCommand(Rehydrator::kDefaultLayout_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 87 | } else if (l == Layout::builtin(l.fBuiltin)) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 88 | this->writeCommand(Rehydrator::kBuiltinLayout_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 89 | this->writeS16(l.fBuiltin); |
| 90 | } else { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 91 | this->writeCommand(Rehydrator::kLayout_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 92 | 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 Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | void Dehydrator::write(Modifiers m) { |
| 104 | if (m == Modifiers()) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 105 | this->writeCommand(Rehydrator::kDefaultModifiers_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 106 | } else { |
| 107 | if (m.fFlags <= 255) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 108 | this->writeCommand(Rehydrator::kModifiers8Bit_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 109 | this->write(m.fLayout); |
| 110 | this->writeU8(m.fFlags); |
| 111 | } else { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 112 | this->writeCommand(Rehydrator::kModifiers_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 113 | this->write(m.fLayout); |
| 114 | this->writeS32(m.fFlags); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 119 | void Dehydrator::write(skstd::string_view s) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 120 | this->write(String(s)); |
| 121 | } |
| 122 | |
| 123 | void 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 Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 130 | fStringBreaks.add(fStringBuffer.bytesWritten()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 131 | fStringBuffer.write8(s.length()); |
| 132 | fStringBuffer.writeString(s); |
| 133 | } else { |
| 134 | offset = found->second; |
| 135 | } |
| 136 | this->writeU16(offset); |
| 137 | } |
| 138 | |
| 139 | void Dehydrator::write(const Symbol& s) { |
| 140 | uint16_t id = this->symbolId(&s, false); |
| 141 | if (id) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 142 | this->writeCommand(Rehydrator::kSymbolRef_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 143 | this->writeU16(id); |
| 144 | return; |
| 145 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 146 | switch (s.kind()) { |
| 147 | case Symbol::Kind::kFunctionDeclaration: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 148 | const FunctionDeclaration& f = s.as<FunctionDeclaration>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 149 | this->writeCommand(Rehydrator::kFunctionDeclaration_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 150 | this->writeId(&f); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 151 | this->write(f.modifiers()); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 152 | this->write(f.name()); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 153 | this->writeU8(f.parameters().size()); |
| 154 | for (const Variable* p : f.parameters()) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 155 | this->writeU16(this->symbolId(p)); |
| 156 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 157 | this->write(f.returnType()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 158 | break; |
| 159 | } |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 160 | case Symbol::Kind::kSymbolAlias: { |
| 161 | const SymbolAlias& alias = s.as<SymbolAlias>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 162 | this->writeCommand(Rehydrator::kSymbolAlias_Command); |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 163 | this->writeId(&alias); |
| 164 | this->write(alias.name()); |
| 165 | this->write(*alias.origSymbol()); |
| 166 | break; |
| 167 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 168 | case Symbol::Kind::kUnresolvedFunction: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 169 | const UnresolvedFunction& f = s.as<UnresolvedFunction>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 170 | this->writeCommand(Rehydrator::kUnresolvedFunction_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 171 | this->writeId(&f); |
Ethan Nicholas | ceb6214 | 2020-10-09 16:51:18 -0400 | [diff] [blame] | 172 | this->writeU8(f.functions().size()); |
| 173 | for (const FunctionDeclaration* funcDecl : f.functions()) { |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 174 | this->write(*funcDecl); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 175 | } |
| 176 | break; |
| 177 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 178 | case Symbol::Kind::kType: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 179 | const Type& t = s.as<Type>(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 180 | switch (t.typeKind()) { |
| 181 | case Type::TypeKind::kArray: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 182 | this->writeCommand(Rehydrator::kArrayType_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 183 | this->writeId(&t); |
| 184 | this->write(t.componentType()); |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 185 | this->writeS8(t.columns()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 186 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 187 | case Type::TypeKind::kStruct: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 188 | this->writeCommand(Rehydrator::kStructType_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 189 | this->writeId(&t); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 190 | this->write(t.name()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 191 | 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 Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 199 | this->writeCommand(Rehydrator::kSystemType_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 200 | this->writeId(&t); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 201 | this->write(t.name()); |
Ethan Nicholas | b13f369 | 2021-09-10 16:49:42 -0400 | [diff] [blame] | 202 | break; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 203 | } |
| 204 | break; |
| 205 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 206 | case Symbol::Kind::kVariable: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 207 | const Variable& v = s.as<Variable>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 208 | this->writeCommand(Rehydrator::kVariable_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 209 | this->writeId(&v); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 210 | this->write(v.modifiers()); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 211 | this->write(v.name()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 212 | this->write(v.type()); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 213 | this->writeU8((int8_t) v.storage()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 214 | break; |
| 215 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 216 | case Symbol::Kind::kField: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 217 | const Field& f = s.as<Field>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 218 | this->writeCommand(Rehydrator::kField_Command); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 219 | this->writeU16(this->symbolId(&f.owner())); |
| 220 | this->writeU8(f.fieldIndex()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 221 | break; |
| 222 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 223 | case Symbol::Kind::kExternal: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 224 | SkASSERT(false); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void Dehydrator::write(const SymbolTable& symbols) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 230 | this->writeCommand(Rehydrator::kSymbolTable_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 231 | this->writeU16(symbols.fOwnedSymbols.size()); |
| 232 | for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) { |
| 233 | this->write(*s); |
| 234 | } |
John Stiles | efe767d | 2020-10-09 12:48:04 -0400 | [diff] [blame] | 235 | this->writeU16(symbols.fSymbols.count()); |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 236 | std::map<skstd::string_view, const Symbol*> ordered; |
| 237 | symbols.foreach([&](skstd::string_view name, const Symbol* symbol) { |
John Stiles | efe767d | 2020-10-09 12:48:04 -0400 | [diff] [blame] | 238 | ordered.insert({name, symbol}); |
| 239 | }); |
Ethan Nicholas | 962dec4 | 2021-06-10 13:06:39 -0400 | [diff] [blame] | 240 | for (std::pair<skstd::string_view, const Symbol*> p : ordered) { |
Brian Salomon | 54fd96b | 2021-08-05 12:16:07 -0400 | [diff] [blame] | 241 | SkDEBUGCODE(bool found = false;) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 242 | for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) { |
| 243 | if (symbols.fOwnedSymbols[i].get() == p.second) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 244 | fCommandBreaks.add(fBody.bytesWritten()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 245 | this->writeU16(i); |
Brian Salomon | 54fd96b | 2021-08-05 12:16:07 -0400 | [diff] [blame] | 246 | SkDEBUGCODE(found = true;) |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 247 | break; |
| 248 | } |
| 249 | } |
| 250 | SkASSERT(found); |
| 251 | } |
| 252 | } |
| 253 | |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 254 | void 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 Stiles | 626b62e | 2021-03-31 22:06:07 -0400 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 261 | void Dehydrator::write(const Expression* e) { |
| 262 | if (e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 263 | switch (e->kind()) { |
| 264 | case Expression::Kind::kBinary: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 265 | const BinaryExpression& b = e->as<BinaryExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 266 | this->writeCommand(Rehydrator::kBinary_Command); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 267 | this->write(b.left().get()); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 268 | this->writeU8((int) b.getOperator().kind()); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 269 | this->write(b.right().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 270 | break; |
| 271 | } |
Brian Osman | eb0f29d | 2021-08-04 11:34:16 -0400 | [diff] [blame] | 272 | case Expression::Kind::kChildCall: |
| 273 | SkDEBUGFAIL("unimplemented--not expected to be used from within an include file"); |
| 274 | break; |
| 275 | |
Ethan Nicholas | 840f581 | 2021-02-16 13:02:57 -0500 | [diff] [blame] | 276 | case Expression::Kind::kCodeString: |
| 277 | SkDEBUGFAIL("shouldn't be able to receive kCodeString here"); |
| 278 | break; |
John Stiles | 626b62e | 2021-03-31 22:06:07 -0400 | [diff] [blame] | 279 | |
John Stiles | 7384b37 | 2021-04-01 13:48:15 -0400 | [diff] [blame] | 280 | 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 Stiles | e3ae968 | 2021-08-05 10:35:01 -0400 | [diff] [blame] | 286 | 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 Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 292 | case Expression::Kind::kConstructorCompound: |
| 293 | this->writeCommand(Rehydrator::kConstructorCompound_Command); |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 294 | this->write(e->type()); |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 295 | this->writeExpressionSpan(e->as<ConstructorCompound>().argumentSpan()); |
John Stiles | 2bec8ab | 2021-04-06 18:40:04 -0400 | [diff] [blame] | 296 | break; |
| 297 | |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 298 | case Expression::Kind::kConstructorCompoundCast: |
| 299 | this->writeCommand(Rehydrator::kConstructorCompoundCast_Command); |
John Stiles | 268a73f | 2021-04-07 12:30:22 -0400 | [diff] [blame] | 300 | this->write(e->type()); |
John Stiles | 8cad637 | 2021-04-07 12:31:13 -0400 | [diff] [blame] | 301 | this->writeExpressionSpan(e->as<ConstructorCompoundCast>().argumentSpan()); |
John Stiles | 268a73f | 2021-04-07 12:30:22 -0400 | [diff] [blame] | 302 | break; |
| 303 | |
John Stiles | 626b62e | 2021-03-31 22:06:07 -0400 | [diff] [blame] | 304 | case Expression::Kind::kConstructorDiagonalMatrix: |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 305 | this->writeCommand(Rehydrator::kConstructorDiagonalMatrix_Command); |
John Stiles | d8eb875 | 2021-04-01 11:49:10 -0400 | [diff] [blame] | 306 | this->write(e->type()); |
| 307 | this->writeExpressionSpan(e->as<ConstructorDiagonalMatrix>().argumentSpan()); |
John Stiles | e118278 | 2021-03-30 22:09:37 -0400 | [diff] [blame] | 308 | break; |
John Stiles | 626b62e | 2021-03-31 22:06:07 -0400 | [diff] [blame] | 309 | |
John Stiles | 5abb9e1 | 2021-04-06 13:47:19 -0400 | [diff] [blame] | 310 | 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 Stiles | fd7252f | 2021-04-04 22:24:40 -0400 | [diff] [blame] | 316 | 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 Stiles | 2938eea | 2021-04-01 18:58:25 -0400 | [diff] [blame] | 322 | 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 Stiles | d47330f | 2021-04-08 23:25:52 -0400 | [diff] [blame] | 328 | 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 Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 334 | case Expression::Kind::kExternalFunctionCall: |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 335 | case Expression::Kind::kExternalFunctionReference: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 336 | SkDEBUGFAIL("unimplemented--not expected to be used from within an include file"); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 337 | break; |
John Stiles | 626b62e | 2021-03-31 22:06:07 -0400 | [diff] [blame] | 338 | |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 339 | case Expression::Kind::kFieldAccess: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 340 | const FieldAccess& f = e->as<FieldAccess>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 341 | this->writeCommand(Rehydrator::kFieldAccess_Command); |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 342 | this->write(f.base().get()); |
| 343 | this->writeU8(f.fieldIndex()); |
| 344 | this->writeU8((int8_t) f.ownerKind()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 345 | break; |
| 346 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 347 | case Expression::Kind::kFunctionCall: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 348 | const FunctionCall& f = e->as<FunctionCall>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 349 | this->writeCommand(Rehydrator::kFunctionCall_Command); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 350 | this->write(f.type()); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 351 | this->writeId(&f.function()); |
| 352 | this->writeU8(f.arguments().size()); |
| 353 | for (const auto& a : f.arguments()) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 354 | this->write(a.get()); |
| 355 | } |
| 356 | break; |
| 357 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 358 | case Expression::Kind::kIndex: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 359 | const IndexExpression& i = e->as<IndexExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 360 | this->writeCommand(Rehydrator::kIndex_Command); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 361 | this->write(i.base().get()); |
| 362 | this->write(i.index().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 363 | break; |
| 364 | } |
John Stiles | 7591d4b | 2021-09-13 13:32:06 -0400 | [diff] [blame] | 365 | 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 Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 383 | break; |
| 384 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 385 | case Expression::Kind::kPostfix: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 386 | const PostfixExpression& p = e->as<PostfixExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 387 | this->writeCommand(Rehydrator::kPostfix_Command); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 388 | this->writeU8((int) p.getOperator().kind()); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 389 | this->write(p.operand().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 390 | break; |
| 391 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 392 | case Expression::Kind::kPrefix: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 393 | const PrefixExpression& p = e->as<PrefixExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 394 | this->writeCommand(Rehydrator::kPrefix_Command); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 395 | this->writeU8((int) p.getOperator().kind()); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 396 | this->write(p.operand().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 397 | break; |
| 398 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 399 | case Expression::Kind::kSetting: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 400 | const Setting& s = e->as<Setting>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 401 | this->writeCommand(Rehydrator::kSetting_Command); |
Ethan Nicholas | 01ec7e8 | 2020-10-08 12:10:12 -0400 | [diff] [blame] | 402 | this->write(s.name()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 403 | break; |
| 404 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 405 | case Expression::Kind::kSwizzle: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 406 | const Swizzle& s = e->as<Swizzle>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 407 | this->writeCommand(Rehydrator::kSwizzle_Command); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 408 | this->write(s.base().get()); |
| 409 | this->writeU8(s.components().size()); |
| 410 | for (int c : s.components()) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 411 | this->writeU8(c); |
| 412 | } |
| 413 | break; |
| 414 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 415 | case Expression::Kind::kTernary: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 416 | const TernaryExpression& t = e->as<TernaryExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 417 | this->writeCommand(Rehydrator::kTernary_Command); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 418 | this->write(t.test().get()); |
| 419 | this->write(t.ifTrue().get()); |
| 420 | this->write(t.ifFalse().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 421 | break; |
| 422 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 423 | case Expression::Kind::kVariableReference: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 424 | const VariableReference& v = e->as<VariableReference>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 425 | this->writeCommand(Rehydrator::kVariableReference_Command); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 426 | this->writeId(v.variable()); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 427 | this->writeU8((int8_t) v.refKind()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 428 | break; |
| 429 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 430 | case Expression::Kind::kFunctionReference: |
Brian Osman | 3099f79 | 2021-09-01 13:12:16 -0400 | [diff] [blame] | 431 | case Expression::Kind::kMethodReference: |
Ethan Nicholas | 549c6b8 | 2021-06-25 12:31:44 -0400 | [diff] [blame] | 432 | case Expression::Kind::kPoison: |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 433 | case Expression::Kind::kTypeReference: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 434 | SkDEBUGFAIL("this expression shouldn't appear in finished code"); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 435 | break; |
| 436 | } |
| 437 | } else { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 438 | this->writeCommand(Rehydrator::kVoid_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | void Dehydrator::write(const Statement* s) { |
| 443 | if (s) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 444 | switch (s->kind()) { |
| 445 | case Statement::Kind::kBlock: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 446 | const Block& b = s->as<Block>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 447 | this->writeCommand(Rehydrator::kBlock_Command); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 448 | AutoDehydratorSymbolTable symbols(this, b.symbolTable()); |
| 449 | this->writeU8(b.children().size()); |
| 450 | for (const std::unique_ptr<Statement>& blockStmt : b.children()) { |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 451 | this->write(blockStmt.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 452 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 453 | this->writeU8(b.isScope()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 454 | break; |
| 455 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 456 | case Statement::Kind::kBreak: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 457 | this->writeCommand(Rehydrator::kBreak_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 458 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 459 | case Statement::Kind::kContinue: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 460 | this->writeCommand(Rehydrator::kContinue_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 461 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 462 | case Statement::Kind::kDiscard: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 463 | this->writeCommand(Rehydrator::kDiscard_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 464 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 465 | case Statement::Kind::kDo: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 466 | const DoStatement& d = s->as<DoStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 467 | this->writeCommand(Rehydrator::kDo_Command); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 468 | this->write(d.statement().get()); |
| 469 | this->write(d.test().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 470 | break; |
| 471 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 472 | case Statement::Kind::kExpression: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 473 | const ExpressionStatement& e = s->as<ExpressionStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 474 | this->writeCommand(Rehydrator::kExpressionStatement_Command); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 475 | this->write(e.expression().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 476 | break; |
| 477 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 478 | case Statement::Kind::kFor: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 479 | const ForStatement& f = s->as<ForStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 480 | this->writeCommand(Rehydrator::kFor_Command); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 481 | this->write(f.initializer().get()); |
| 482 | this->write(f.test().get()); |
| 483 | this->write(f.next().get()); |
| 484 | this->write(f.statement().get()); |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 485 | this->write(*f.symbols()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 486 | break; |
| 487 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 488 | case Statement::Kind::kIf: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 489 | const IfStatement& i = s->as<IfStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 490 | this->writeCommand(Rehydrator::kIf_Command); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 491 | this->writeU8(i.isStatic()); |
| 492 | this->write(i.test().get()); |
| 493 | this->write(i.ifTrue().get()); |
| 494 | this->write(i.ifFalse().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 495 | break; |
| 496 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 497 | case Statement::Kind::kInlineMarker: { |
| 498 | const InlineMarker& i = s->as<InlineMarker>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 499 | this->writeCommand(Rehydrator::kInlineMarker_Command); |
Ethan Nicholas | ceb6214 | 2020-10-09 16:51:18 -0400 | [diff] [blame] | 500 | this->writeId(&i.function()); |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 501 | break; |
| 502 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 503 | case Statement::Kind::kNop: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 504 | SkDEBUGFAIL("unexpected--nop statement in finished code"); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 505 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 506 | case Statement::Kind::kReturn: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 507 | const ReturnStatement& r = s->as<ReturnStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 508 | this->writeCommand(Rehydrator::kReturn_Command); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 509 | this->write(r.expression().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 510 | break; |
| 511 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 512 | case Statement::Kind::kSwitch: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 513 | const SwitchStatement& ss = s->as<SwitchStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 514 | this->writeCommand(Rehydrator::kSwitch_Command); |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 515 | this->writeU8(ss.isStatic()); |
| 516 | AutoDehydratorSymbolTable symbols(this, ss.symbols()); |
| 517 | this->write(ss.value().get()); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 518 | this->writeU8(ss.cases().size()); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame] | 519 | 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 Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 523 | } |
| 524 | break; |
| 525 | } |
Brian Osman | 9eb848a | 2020-09-11 15:53:40 -0400 | [diff] [blame] | 526 | case Statement::Kind::kSwitchCase: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 527 | SkDEBUGFAIL("SwitchCase statements shouldn't appear here"); |
Brian Osman | 9eb848a | 2020-09-11 15:53:40 -0400 | [diff] [blame] | 528 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 529 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 530 | const VarDeclaration& v = s->as<VarDeclaration>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 531 | this->writeCommand(Rehydrator::kVarDeclaration_Command); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 532 | this->writeU16(this->symbolId(&v.var())); |
| 533 | this->write(v.baseType()); |
John Stiles | 62a5646 | 2020-12-03 10:41:58 -0500 | [diff] [blame] | 534 | this->writeS8(v.arraySize()); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 535 | this->write(v.value().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 536 | break; |
| 537 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 538 | } |
| 539 | } else { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 540 | this->writeCommand(Rehydrator::kVoid_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | void Dehydrator::write(const ProgramElement& e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 545 | switch (e.kind()) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 546 | case ProgramElement::Kind::kExtension: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 547 | SkASSERT(false); |
| 548 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 549 | case ProgramElement::Kind::kFunction: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 550 | const FunctionDefinition& f = e.as<FunctionDefinition>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 551 | this->writeCommand(Rehydrator::kFunctionDefinition_Command); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 552 | this->writeU16(this->symbolId(&f.declaration())); |
| 553 | this->write(f.body().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 554 | break; |
| 555 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 556 | 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 Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 562 | case ProgramElement::Kind::kInterfaceBlock: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 563 | const InterfaceBlock& i = e.as<InterfaceBlock>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 564 | this->writeCommand(Rehydrator::kInterfaceBlock_Command); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 565 | this->write(i.variable()); |
| 566 | this->write(i.typeName()); |
| 567 | this->write(i.instanceName()); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 568 | this->writeS8(i.arraySize()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 569 | break; |
| 570 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 571 | case ProgramElement::Kind::kModifiers: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 572 | SkASSERT(false); |
| 573 | break; |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 574 | 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 Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 580 | case ProgramElement::Kind::kGlobalVar: { |
| 581 | const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 582 | this->writeCommand(Rehydrator::kVarDeclarations_Command); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 583 | this->write(v.declaration().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 590 | this->writeCommand(Rehydrator::kElements_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 591 | for (const auto& e : elements) { |
| 592 | this->write(*e); |
| 593 | } |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 594 | this->writeCommand(Rehydrator::kElementsComplete_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | void Dehydrator::finish(OutputStream& out) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 598 | String stringBuffer = fStringBuffer.str(); |
| 599 | String commandBuffer = fBody.str(); |
| 600 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 601 | out.write16(fStringBuffer.str().size()); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 602 | fStringBufferStart = 2; |
| 603 | out.writeString(stringBuffer); |
| 604 | fCommandStart = fStringBufferStart + stringBuffer.size(); |
| 605 | out.writeString(commandBuffer); |
| 606 | } |
| 607 | |
| 608 | const 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 Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | } // namespace |
| 619 | |
| 620 | #endif |