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" |
| 19 | #include "src/sksl/ir/SkSLContinueStatement.h" |
| 20 | #include "src/sksl/ir/SkSLDiscardStatement.h" |
| 21 | #include "src/sksl/ir/SkSLDoStatement.h" |
| 22 | #include "src/sksl/ir/SkSLEnum.h" |
| 23 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 24 | #include "src/sksl/ir/SkSLField.h" |
| 25 | #include "src/sksl/ir/SkSLFieldAccess.h" |
| 26 | #include "src/sksl/ir/SkSLForStatement.h" |
| 27 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 28 | #include "src/sksl/ir/SkSLFunctionDeclaration.h" |
| 29 | #include "src/sksl/ir/SkSLFunctionDefinition.h" |
| 30 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 31 | #include "src/sksl/ir/SkSLIndexExpression.h" |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 32 | #include "src/sksl/ir/SkSLInlineMarker.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 33 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 34 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 35 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 36 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 37 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 38 | #include "src/sksl/ir/SkSLSetting.h" |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 39 | #include "src/sksl/ir/SkSLStructDefinition.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 40 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 41 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 42 | #include "src/sksl/ir/SkSLSwizzle.h" |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 43 | #include "src/sksl/ir/SkSLSymbolAlias.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 44 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 45 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 46 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 47 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 48 | #include "src/sksl/ir/SkSLVariable.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 49 | |
| 50 | #ifdef SKSL_STANDALONE |
| 51 | |
| 52 | namespace SkSL { |
| 53 | |
| 54 | static constexpr int HEADER_SIZE = 2; |
| 55 | |
| 56 | class AutoDehydratorSymbolTable { |
| 57 | public: |
| 58 | AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols) |
| 59 | : fDehydrator(dehydrator) { |
| 60 | dehydrator->fSymbolMap.emplace_back(); |
| 61 | if (symbols) { |
| 62 | dehydrator->write(*symbols); |
| 63 | } else { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 64 | dehydrator->writeCommand(Rehydrator::kVoid_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | ~AutoDehydratorSymbolTable() { |
| 69 | fDehydrator->fSymbolMap.pop_back(); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | Dehydrator* fDehydrator; |
| 74 | }; |
| 75 | |
| 76 | void Dehydrator::write(Layout l) { |
| 77 | if (l == Layout()) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 78 | this->writeCommand(Rehydrator::kDefaultLayout_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 79 | } else if (l == Layout::builtin(l.fBuiltin)) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 80 | this->writeCommand(Rehydrator::kBuiltinLayout_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 81 | this->writeS16(l.fBuiltin); |
| 82 | } else { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 83 | this->writeCommand(Rehydrator::kLayout_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 84 | fBody.write32(l.fFlags); |
| 85 | this->writeS8(l.fLocation); |
| 86 | this->writeS8(l.fOffset); |
| 87 | this->writeS8(l.fBinding); |
| 88 | this->writeS8(l.fIndex); |
| 89 | this->writeS8(l.fSet); |
| 90 | this->writeS16(l.fBuiltin); |
| 91 | this->writeS8(l.fInputAttachmentIndex); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 92 | this->writeS8(l.fPrimitive); |
| 93 | this->writeS8(l.fMaxVertices); |
| 94 | this->writeS8(l.fInvocations); |
| 95 | this->write(l.fMarker); |
| 96 | this->write(l.fWhen); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 97 | this->writeS8((int) l.fCType); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void Dehydrator::write(Modifiers m) { |
| 102 | if (m == Modifiers()) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 103 | this->writeCommand(Rehydrator::kDefaultModifiers_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 104 | } else { |
| 105 | if (m.fFlags <= 255) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 106 | this->writeCommand(Rehydrator::kModifiers8Bit_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 107 | this->write(m.fLayout); |
| 108 | this->writeU8(m.fFlags); |
| 109 | } else { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 110 | this->writeCommand(Rehydrator::kModifiers_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 111 | this->write(m.fLayout); |
| 112 | this->writeS32(m.fFlags); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void Dehydrator::write(StringFragment s) { |
| 118 | this->write(String(s)); |
| 119 | } |
| 120 | |
| 121 | void Dehydrator::write(String s) { |
| 122 | auto found = fStrings.find(s); |
| 123 | int offset; |
| 124 | if (found == fStrings.end()) { |
| 125 | offset = fStringBuffer.str().length() + HEADER_SIZE; |
| 126 | fStrings.insert({ s, offset }); |
| 127 | SkASSERT(s.length() <= 255); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 128 | fStringBreaks.add(fStringBuffer.bytesWritten()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 129 | fStringBuffer.write8(s.length()); |
| 130 | fStringBuffer.writeString(s); |
| 131 | } else { |
| 132 | offset = found->second; |
| 133 | } |
| 134 | this->writeU16(offset); |
| 135 | } |
| 136 | |
| 137 | void Dehydrator::write(const Symbol& s) { |
| 138 | uint16_t id = this->symbolId(&s, false); |
| 139 | if (id) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 140 | this->writeCommand(Rehydrator::kSymbolRef_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 141 | this->writeU16(id); |
| 142 | return; |
| 143 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 144 | switch (s.kind()) { |
| 145 | case Symbol::Kind::kFunctionDeclaration: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 146 | const FunctionDeclaration& f = s.as<FunctionDeclaration>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 147 | this->writeCommand(Rehydrator::kFunctionDeclaration_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 148 | this->writeId(&f); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 149 | this->write(f.modifiers()); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 150 | this->write(f.name()); |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 151 | this->writeU8(f.parameters().size()); |
| 152 | for (const Variable* p : f.parameters()) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 153 | this->writeU16(this->symbolId(p)); |
| 154 | } |
Ethan Nicholas | ed84b73 | 2020-10-08 11:45:44 -0400 | [diff] [blame] | 155 | this->write(f.returnType()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 156 | break; |
| 157 | } |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 158 | case Symbol::Kind::kSymbolAlias: { |
| 159 | const SymbolAlias& alias = s.as<SymbolAlias>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 160 | this->writeCommand(Rehydrator::kSymbolAlias_Command); |
John Stiles | 49a547f | 2020-10-06 16:14:37 -0400 | [diff] [blame] | 161 | this->writeId(&alias); |
| 162 | this->write(alias.name()); |
| 163 | this->write(*alias.origSymbol()); |
| 164 | break; |
| 165 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 166 | case Symbol::Kind::kUnresolvedFunction: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 167 | const UnresolvedFunction& f = s.as<UnresolvedFunction>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 168 | this->writeCommand(Rehydrator::kUnresolvedFunction_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 169 | this->writeId(&f); |
Ethan Nicholas | ceb6214 | 2020-10-09 16:51:18 -0400 | [diff] [blame] | 170 | this->writeU8(f.functions().size()); |
| 171 | for (const FunctionDeclaration* funcDecl : f.functions()) { |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 172 | this->write(*funcDecl); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 173 | } |
| 174 | break; |
| 175 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 176 | case Symbol::Kind::kType: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 177 | const Type& t = s.as<Type>(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 178 | switch (t.typeKind()) { |
| 179 | case Type::TypeKind::kArray: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 180 | this->writeCommand(Rehydrator::kArrayType_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 181 | this->writeId(&t); |
| 182 | this->write(t.componentType()); |
Brian Osman | e8c2608 | 2020-10-01 17:22:45 -0400 | [diff] [blame] | 183 | this->writeS8(t.columns()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 184 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 185 | case Type::TypeKind::kEnum: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 186 | this->writeCommand(Rehydrator::kEnumType_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 187 | this->writeId(&t); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 188 | this->write(t.name()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 189 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 190 | case Type::TypeKind::kStruct: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 191 | this->writeCommand(Rehydrator::kStructType_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 192 | this->writeId(&t); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 193 | this->write(t.name()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 194 | this->writeU8(t.fields().size()); |
| 195 | for (const Type::Field& f : t.fields()) { |
| 196 | this->write(f.fModifiers); |
| 197 | this->write(f.fName); |
| 198 | this->write(*f.fType); |
| 199 | } |
| 200 | break; |
| 201 | default: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 202 | this->writeCommand(Rehydrator::kSystemType_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 203 | this->writeId(&t); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 204 | this->write(t.name()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 205 | } |
| 206 | break; |
| 207 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 208 | case Symbol::Kind::kVariable: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 209 | const Variable& v = s.as<Variable>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 210 | this->writeCommand(Rehydrator::kVariable_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 211 | this->writeId(&v); |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 212 | this->write(v.modifiers()); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 213 | this->write(v.name()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 214 | this->write(v.type()); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 215 | this->writeU8((int8_t) v.storage()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 216 | break; |
| 217 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 218 | case Symbol::Kind::kField: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 219 | const Field& f = s.as<Field>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 220 | this->writeCommand(Rehydrator::kField_Command); |
Ethan Nicholas | e2c4999 | 2020-10-05 11:49:11 -0400 | [diff] [blame] | 221 | this->writeU16(this->symbolId(&f.owner())); |
| 222 | this->writeU8(f.fieldIndex()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 223 | break; |
| 224 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 225 | case Symbol::Kind::kExternal: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 226 | SkASSERT(false); |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void Dehydrator::write(const SymbolTable& symbols) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 232 | this->writeCommand(Rehydrator::kSymbolTable_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 233 | this->writeU16(symbols.fOwnedSymbols.size()); |
| 234 | for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) { |
| 235 | this->write(*s); |
| 236 | } |
John Stiles | efe767d | 2020-10-09 12:48:04 -0400 | [diff] [blame] | 237 | this->writeU16(symbols.fSymbols.count()); |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 238 | std::map<StringFragment, const Symbol*> ordered; |
John Stiles | efe767d | 2020-10-09 12:48:04 -0400 | [diff] [blame] | 239 | symbols.foreach([&](StringFragment name, const Symbol* symbol) { |
| 240 | ordered.insert({name, symbol}); |
| 241 | }); |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 242 | for (std::pair<StringFragment, const Symbol*> p : ordered) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 243 | bool found = false; |
| 244 | for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) { |
| 245 | if (symbols.fOwnedSymbols[i].get() == p.second) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 246 | fCommandBreaks.add(fBody.bytesWritten()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 247 | this->writeU16(i); |
| 248 | found = true; |
| 249 | break; |
| 250 | } |
| 251 | } |
| 252 | SkASSERT(found); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | void Dehydrator::write(const Expression* e) { |
| 257 | if (e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 258 | switch (e->kind()) { |
| 259 | case Expression::Kind::kBinary: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 260 | const BinaryExpression& b = e->as<BinaryExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 261 | this->writeCommand(Rehydrator::kBinary_Command); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 262 | this->write(b.left().get()); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 263 | this->writeU8((int) b.getOperator().kind()); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 264 | this->write(b.right().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 265 | break; |
| 266 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 267 | case Expression::Kind::kBoolLiteral: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 268 | const BoolLiteral& b = e->as<BoolLiteral>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 269 | this->writeCommand(Rehydrator::kBoolLiteral_Command); |
Ethan Nicholas | 59d660c | 2020-09-28 09:18:15 -0400 | [diff] [blame] | 270 | this->writeU8(b.value()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 271 | break; |
| 272 | } |
Ethan Nicholas | 840f581 | 2021-02-16 13:02:57 -0500 | [diff] [blame] | 273 | case Expression::Kind::kCodeString: |
| 274 | SkDEBUGFAIL("shouldn't be able to receive kCodeString here"); |
| 275 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 276 | case Expression::Kind::kConstructor: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 277 | const Constructor& c = e->as<Constructor>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 278 | this->writeCommand(Rehydrator::kConstructor_Command); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 279 | this->write(c.type()); |
Ethan Nicholas | f70f044 | 2020-09-29 12:41:35 -0400 | [diff] [blame] | 280 | this->writeU8(c.arguments().size()); |
| 281 | for (const auto& a : c.arguments()) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 282 | this->write(a.get()); |
| 283 | } |
| 284 | break; |
| 285 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 286 | case Expression::Kind::kExternalFunctionCall: |
Brian Osman | be0b3b7 | 2021-01-06 14:27:35 -0500 | [diff] [blame] | 287 | case Expression::Kind::kExternalFunctionReference: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 288 | SkDEBUGFAIL("unimplemented--not expected to be used from within an include file"); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 289 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 290 | case Expression::Kind::kFieldAccess: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 291 | const FieldAccess& f = e->as<FieldAccess>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 292 | this->writeCommand(Rehydrator::kFieldAccess_Command); |
Ethan Nicholas | 7a95b20 | 2020-10-09 11:55:40 -0400 | [diff] [blame] | 293 | this->write(f.base().get()); |
| 294 | this->writeU8(f.fieldIndex()); |
| 295 | this->writeU8((int8_t) f.ownerKind()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 296 | break; |
| 297 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 298 | case Expression::Kind::kFloatLiteral: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 299 | const FloatLiteral& f = e->as<FloatLiteral>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 300 | this->writeCommand(Rehydrator::kFloatLiteral_Command); |
Brian Osman | fb964a4 | 2020-11-18 10:45:52 -0500 | [diff] [blame] | 301 | this->write(f.type()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 302 | FloatIntUnion u; |
Ethan Nicholas | a3f22f1 | 2020-10-01 12:13:17 -0400 | [diff] [blame] | 303 | u.fFloat = f.value(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 304 | this->writeS32(u.fInt); |
| 305 | break; |
| 306 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 307 | case Expression::Kind::kFunctionCall: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 308 | const FunctionCall& f = e->as<FunctionCall>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 309 | this->writeCommand(Rehydrator::kFunctionCall_Command); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 310 | this->write(f.type()); |
Ethan Nicholas | 0dec992 | 2020-10-05 15:51:52 -0400 | [diff] [blame] | 311 | this->writeId(&f.function()); |
| 312 | this->writeU8(f.arguments().size()); |
| 313 | for (const auto& a : f.arguments()) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 314 | this->write(a.get()); |
| 315 | } |
| 316 | break; |
| 317 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 318 | case Expression::Kind::kIndex: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 319 | const IndexExpression& i = e->as<IndexExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 320 | this->writeCommand(Rehydrator::kIndex_Command); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 321 | this->write(i.base().get()); |
| 322 | this->write(i.index().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 323 | break; |
| 324 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 325 | case Expression::Kind::kIntLiteral: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 326 | const IntLiteral& i = e->as<IntLiteral>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 327 | this->writeCommand(Rehydrator::kIntLiteral_Command); |
Brian Osman | fb964a4 | 2020-11-18 10:45:52 -0500 | [diff] [blame] | 328 | this->write(i.type()); |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 329 | this->writeS32(i.value()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 330 | break; |
| 331 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 332 | case Expression::Kind::kPostfix: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 333 | const PostfixExpression& p = e->as<PostfixExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 334 | this->writeCommand(Rehydrator::kPostfix_Command); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 335 | this->writeU8((int) p.getOperator().kind()); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 336 | this->write(p.operand().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 337 | break; |
| 338 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 339 | case Expression::Kind::kPrefix: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 340 | const PrefixExpression& p = e->as<PrefixExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 341 | this->writeCommand(Rehydrator::kPrefix_Command); |
John Stiles | 4599050 | 2021-02-16 10:55:27 -0500 | [diff] [blame] | 342 | this->writeU8((int) p.getOperator().kind()); |
Ethan Nicholas | 444ccc6 | 2020-10-09 10:16:22 -0400 | [diff] [blame] | 343 | this->write(p.operand().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 344 | break; |
| 345 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 346 | case Expression::Kind::kSetting: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 347 | const Setting& s = e->as<Setting>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 348 | this->writeCommand(Rehydrator::kSetting_Command); |
Ethan Nicholas | 01ec7e8 | 2020-10-08 12:10:12 -0400 | [diff] [blame] | 349 | this->write(s.name()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 350 | break; |
| 351 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 352 | case Expression::Kind::kSwizzle: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 353 | const Swizzle& s = e->as<Swizzle>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 354 | this->writeCommand(Rehydrator::kSwizzle_Command); |
Ethan Nicholas | 6b4d581 | 2020-10-12 16:11:51 -0400 | [diff] [blame] | 355 | this->write(s.base().get()); |
| 356 | this->writeU8(s.components().size()); |
| 357 | for (int c : s.components()) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 358 | this->writeU8(c); |
| 359 | } |
| 360 | break; |
| 361 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 362 | case Expression::Kind::kTernary: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 363 | const TernaryExpression& t = e->as<TernaryExpression>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 364 | this->writeCommand(Rehydrator::kTernary_Command); |
Ethan Nicholas | dd21816 | 2020-10-08 05:48:01 -0400 | [diff] [blame] | 365 | this->write(t.test().get()); |
| 366 | this->write(t.ifTrue().get()); |
| 367 | this->write(t.ifFalse().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 368 | break; |
| 369 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 370 | case Expression::Kind::kVariableReference: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 371 | const VariableReference& v = e->as<VariableReference>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 372 | this->writeCommand(Rehydrator::kVariableReference_Command); |
Ethan Nicholas | 7868692 | 2020-10-08 06:46:27 -0400 | [diff] [blame] | 373 | this->writeId(v.variable()); |
Ethan Nicholas | 453f67f | 2020-10-09 10:43:45 -0400 | [diff] [blame] | 374 | this->writeU8((int8_t) v.refKind()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 375 | break; |
| 376 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 377 | case Expression::Kind::kFunctionReference: |
| 378 | case Expression::Kind::kTypeReference: |
| 379 | case Expression::Kind::kDefined: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 380 | SkDEBUGFAIL("this expression shouldn't appear in finished code"); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | } else { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 384 | this->writeCommand(Rehydrator::kVoid_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 385 | } |
| 386 | } |
| 387 | |
| 388 | void Dehydrator::write(const Statement* s) { |
| 389 | if (s) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 390 | switch (s->kind()) { |
| 391 | case Statement::Kind::kBlock: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 392 | const Block& b = s->as<Block>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 393 | this->writeCommand(Rehydrator::kBlock_Command); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 394 | AutoDehydratorSymbolTable symbols(this, b.symbolTable()); |
| 395 | this->writeU8(b.children().size()); |
| 396 | for (const std::unique_ptr<Statement>& blockStmt : b.children()) { |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 397 | this->write(blockStmt.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 398 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 399 | this->writeU8(b.isScope()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 400 | break; |
| 401 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 402 | case Statement::Kind::kBreak: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 403 | this->writeCommand(Rehydrator::kBreak_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 404 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 405 | case Statement::Kind::kContinue: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 406 | this->writeCommand(Rehydrator::kContinue_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 407 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 408 | case Statement::Kind::kDiscard: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 409 | this->writeCommand(Rehydrator::kDiscard_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 410 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 411 | case Statement::Kind::kDo: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 412 | const DoStatement& d = s->as<DoStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 413 | this->writeCommand(Rehydrator::kDo_Command); |
Ethan Nicholas | 1fd6116 | 2020-09-28 13:14:19 -0400 | [diff] [blame] | 414 | this->write(d.statement().get()); |
| 415 | this->write(d.test().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 416 | break; |
| 417 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 418 | case Statement::Kind::kExpression: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 419 | const ExpressionStatement& e = s->as<ExpressionStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 420 | this->writeCommand(Rehydrator::kExpressionStatement_Command); |
Ethan Nicholas | d503a5a | 2020-09-30 09:29:55 -0400 | [diff] [blame] | 421 | this->write(e.expression().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 422 | break; |
| 423 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 424 | case Statement::Kind::kFor: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 425 | const ForStatement& f = s->as<ForStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 426 | this->writeCommand(Rehydrator::kFor_Command); |
Ethan Nicholas | 0d31ed5 | 2020-10-05 14:47:09 -0400 | [diff] [blame] | 427 | this->write(f.initializer().get()); |
| 428 | this->write(f.test().get()); |
| 429 | this->write(f.next().get()); |
| 430 | this->write(f.statement().get()); |
John Stiles | 7c3515b | 2020-10-16 18:38:39 -0400 | [diff] [blame] | 431 | this->write(*f.symbols()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 432 | break; |
| 433 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 434 | case Statement::Kind::kIf: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 435 | const IfStatement& i = s->as<IfStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 436 | this->writeCommand(Rehydrator::kIf_Command); |
Ethan Nicholas | 8c44eca | 2020-10-07 16:47:09 -0400 | [diff] [blame] | 437 | this->writeU8(i.isStatic()); |
| 438 | this->write(i.test().get()); |
| 439 | this->write(i.ifTrue().get()); |
| 440 | this->write(i.ifFalse().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 441 | break; |
| 442 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 443 | case Statement::Kind::kInlineMarker: { |
| 444 | const InlineMarker& i = s->as<InlineMarker>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 445 | this->writeCommand(Rehydrator::kInlineMarker_Command); |
Ethan Nicholas | ceb6214 | 2020-10-09 16:51:18 -0400 | [diff] [blame] | 446 | this->writeId(&i.function()); |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 447 | break; |
| 448 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 449 | case Statement::Kind::kNop: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 450 | SkDEBUGFAIL("unexpected--nop statement in finished code"); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 451 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 452 | case Statement::Kind::kReturn: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 453 | const ReturnStatement& r = s->as<ReturnStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 454 | this->writeCommand(Rehydrator::kReturn_Command); |
Ethan Nicholas | 2a4952d | 2020-10-08 15:35:56 -0400 | [diff] [blame] | 455 | this->write(r.expression().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 456 | break; |
| 457 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 458 | case Statement::Kind::kSwitch: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 459 | const SwitchStatement& ss = s->as<SwitchStatement>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 460 | this->writeCommand(Rehydrator::kSwitch_Command); |
Ethan Nicholas | 01b05e5 | 2020-10-22 15:53:41 -0400 | [diff] [blame] | 461 | this->writeU8(ss.isStatic()); |
| 462 | AutoDehydratorSymbolTable symbols(this, ss.symbols()); |
| 463 | this->write(ss.value().get()); |
John Stiles | 2d4f959 | 2020-10-30 10:29:12 -0400 | [diff] [blame] | 464 | this->writeU8(ss.cases().size()); |
John Stiles | b23a64b | 2021-03-11 08:27:59 -0500 | [diff] [blame^] | 465 | for (const std::unique_ptr<Statement>& stmt : ss.cases()) { |
| 466 | const SwitchCase& sc = stmt->as<SwitchCase>(); |
| 467 | this->write(sc.value().get()); |
| 468 | this->write(sc.statement().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 469 | } |
| 470 | break; |
| 471 | } |
Brian Osman | 9eb848a | 2020-09-11 15:53:40 -0400 | [diff] [blame] | 472 | case Statement::Kind::kSwitchCase: |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 473 | SkDEBUGFAIL("SwitchCase statements shouldn't appear here"); |
Brian Osman | 9eb848a | 2020-09-11 15:53:40 -0400 | [diff] [blame] | 474 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 475 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 476 | const VarDeclaration& v = s->as<VarDeclaration>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 477 | this->writeCommand(Rehydrator::kVarDeclaration_Command); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 478 | this->writeU16(this->symbolId(&v.var())); |
| 479 | this->write(v.baseType()); |
John Stiles | 62a5646 | 2020-12-03 10:41:58 -0500 | [diff] [blame] | 480 | this->writeS8(v.arraySize()); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 481 | this->write(v.value().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 482 | break; |
| 483 | } |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 484 | } |
| 485 | } else { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 486 | this->writeCommand(Rehydrator::kVoid_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | void Dehydrator::write(const ProgramElement& e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 491 | switch (e.kind()) { |
| 492 | case ProgramElement::Kind::kEnum: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 493 | const Enum& en = e.as<Enum>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 494 | this->writeCommand(Rehydrator::kEnum_Command); |
Ethan Nicholas | d83ded8 | 2020-09-29 17:05:54 -0400 | [diff] [blame] | 495 | this->write(en.typeName()); |
| 496 | AutoDehydratorSymbolTable symbols(this, en.symbols()); |
| 497 | for (const std::unique_ptr<const Symbol>& s : en.symbols()->fOwnedSymbols) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 498 | SkASSERT(s->kind() == Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 499 | Variable& v = (Variable&) *s; |
Ethan Nicholas | 041fd0a | 2020-10-07 16:42:04 -0400 | [diff] [blame] | 500 | SkASSERT(v.initialValue()); |
| 501 | const IntLiteral& i = v.initialValue()->as<IntLiteral>(); |
Ethan Nicholas | e96cdd1 | 2020-09-28 16:27:18 -0400 | [diff] [blame] | 502 | this->writeS32(i.value()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 503 | } |
| 504 | break; |
| 505 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 506 | case ProgramElement::Kind::kExtension: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 507 | SkASSERT(false); |
| 508 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 509 | case ProgramElement::Kind::kFunction: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 510 | const FunctionDefinition& f = e.as<FunctionDefinition>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 511 | this->writeCommand(Rehydrator::kFunctionDefinition_Command); |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 512 | this->writeU16(this->symbolId(&f.declaration())); |
| 513 | this->write(f.body().get()); |
| 514 | this->writeU8(f.referencedIntrinsics().size()); |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 515 | std::set<uint16_t> ordered; |
Ethan Nicholas | 0a5d096 | 2020-10-14 13:33:18 -0400 | [diff] [blame] | 516 | for (const FunctionDeclaration* ref : f.referencedIntrinsics()) { |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 517 | ordered.insert(this->symbolId(ref)); |
| 518 | } |
| 519 | for (uint16_t ref : ordered) { |
| 520 | this->writeU16(ref); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 521 | } |
| 522 | break; |
| 523 | } |
John Stiles | 569249b | 2020-11-03 12:18:22 -0500 | [diff] [blame] | 524 | case ProgramElement::Kind::kFunctionPrototype: { |
| 525 | // We don't need to emit function prototypes into the dehydrated data, because we don't |
| 526 | // ever need to re-emit the intrinsics files as raw GLSL/Metal. As long as the symbols |
| 527 | // exist in the symbol table, we're in good shape. |
| 528 | break; |
| 529 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 530 | case ProgramElement::Kind::kInterfaceBlock: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 531 | const InterfaceBlock& i = e.as<InterfaceBlock>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 532 | this->writeCommand(Rehydrator::kInterfaceBlock_Command); |
Ethan Nicholas | eaf4788 | 2020-10-15 10:10:08 -0400 | [diff] [blame] | 533 | this->write(i.variable()); |
| 534 | this->write(i.typeName()); |
| 535 | this->write(i.instanceName()); |
John Stiles | d39aec0 | 2020-12-03 10:42:26 -0500 | [diff] [blame] | 536 | this->writeS8(i.arraySize()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 537 | break; |
| 538 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 539 | case ProgramElement::Kind::kModifiers: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 540 | SkASSERT(false); |
| 541 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 542 | case ProgramElement::Kind::kSection: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 543 | SkASSERT(false); |
| 544 | break; |
John Stiles | dc75a97 | 2020-11-25 16:24:55 -0500 | [diff] [blame] | 545 | case ProgramElement::Kind::kStructDefinition: { |
| 546 | const StructDefinition& structDef = e.as<StructDefinition>(); |
| 547 | this->writeCommand(Rehydrator::kStructDefinition_Command); |
| 548 | this->write(structDef.type()); |
| 549 | break; |
| 550 | } |
Brian Osman | c021360 | 2020-10-06 14:43:32 -0400 | [diff] [blame] | 551 | case ProgramElement::Kind::kGlobalVar: { |
| 552 | const GlobalVarDeclaration& v = e.as<GlobalVarDeclaration>(); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 553 | this->writeCommand(Rehydrator::kVarDeclarations_Command); |
Ethan Nicholas | c51f33e | 2020-10-13 13:49:44 -0400 | [diff] [blame] | 554 | this->write(v.declaration().get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 561 | this->writeCommand(Rehydrator::kElements_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 562 | for (const auto& e : elements) { |
| 563 | this->write(*e); |
| 564 | } |
John Stiles | 1ea7f54 | 2020-11-02 13:07:23 -0500 | [diff] [blame] | 565 | this->writeCommand(Rehydrator::kElementsComplete_Command); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void Dehydrator::finish(OutputStream& out) { |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 569 | String stringBuffer = fStringBuffer.str(); |
| 570 | String commandBuffer = fBody.str(); |
| 571 | |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 572 | out.write16(fStringBuffer.str().size()); |
John Stiles | e1589a1 | 2020-10-08 13:56:46 -0400 | [diff] [blame] | 573 | fStringBufferStart = 2; |
| 574 | out.writeString(stringBuffer); |
| 575 | fCommandStart = fStringBufferStart + stringBuffer.size(); |
| 576 | out.writeString(commandBuffer); |
| 577 | } |
| 578 | |
| 579 | const char* Dehydrator::prefixAtOffset(size_t byte) { |
| 580 | if (byte >= fCommandStart) { |
| 581 | return fCommandBreaks.contains(byte - fCommandStart) ? "\n" : ""; |
| 582 | } |
| 583 | if (byte >= fStringBufferStart) { |
| 584 | return fStringBreaks.contains(byte - fStringBufferStart) ? "\n" : ""; |
| 585 | } |
| 586 | return ""; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | } // namespace |
| 590 | |
| 591 | #endif |