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 | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 12 | #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" |
| 29 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 30 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 31 | #include "src/sksl/ir/SkSLNullLiteral.h" |
| 32 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 33 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 34 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 35 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 36 | #include "src/sksl/ir/SkSLSetting.h" |
| 37 | #include "src/sksl/ir/SkSLStatement.h" |
| 38 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 39 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 40 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 41 | #include "src/sksl/ir/SkSLSymbol.h" |
| 42 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 43 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 44 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 45 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 46 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 47 | #include "src/sksl/ir/SkSLVariable.h" |
| 48 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 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 { |
| 64 | dehydrator->writeU8(Rehydrator::kVoid_Command); |
| 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()) { |
| 78 | this->writeU8(Rehydrator::kDefaultLayout_Command); |
| 79 | } else if (l == Layout::builtin(l.fBuiltin)) { |
| 80 | this->writeS8(Rehydrator::kBuiltinLayout_Command); |
| 81 | this->writeS16(l.fBuiltin); |
| 82 | } else { |
| 83 | this->writeS8(Rehydrator::kLayout_Command); |
| 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); |
| 92 | this->writeS8((int) l.fFormat); |
| 93 | this->writeS8(l.fPrimitive); |
| 94 | this->writeS8(l.fMaxVertices); |
| 95 | this->writeS8(l.fInvocations); |
| 96 | this->write(l.fMarker); |
| 97 | this->write(l.fWhen); |
| 98 | this->writeS8(l.fKey); |
| 99 | this->writeS8((int) l.fCType); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void Dehydrator::write(Modifiers m) { |
| 104 | if (m == Modifiers()) { |
| 105 | this->writeU8(Rehydrator::kDefaultModifiers_Command); |
| 106 | } else { |
| 107 | if (m.fFlags <= 255) { |
| 108 | this->writeU8(Rehydrator::kModifiers8Bit_Command); |
| 109 | this->write(m.fLayout); |
| 110 | this->writeU8(m.fFlags); |
| 111 | } else { |
| 112 | this->writeU8(Rehydrator::kModifiers_Command); |
| 113 | this->write(m.fLayout); |
| 114 | this->writeS32(m.fFlags); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void Dehydrator::write(StringFragment s) { |
| 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); |
| 130 | fStringBuffer.write8(s.length()); |
| 131 | fStringBuffer.writeString(s); |
| 132 | } else { |
| 133 | offset = found->second; |
| 134 | } |
| 135 | this->writeU16(offset); |
| 136 | } |
| 137 | |
| 138 | void Dehydrator::write(const Symbol& s) { |
| 139 | uint16_t id = this->symbolId(&s, false); |
| 140 | if (id) { |
| 141 | this->writeU8(Rehydrator::kSymbolRef_Command); |
| 142 | this->writeU16(id); |
| 143 | return; |
| 144 | } |
| 145 | switch (s.fKind) { |
| 146 | case Symbol::kFunctionDeclaration_Kind: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 147 | const FunctionDeclaration& f = s.as<FunctionDeclaration>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 148 | this->writeU8(Rehydrator::kFunctionDeclaration_Command); |
| 149 | this->writeId(&f); |
| 150 | this->write(f.fModifiers); |
| 151 | this->write(f.fName); |
| 152 | this->writeU8(f.fParameters.size()); |
| 153 | for (const Variable* p : f.fParameters) { |
| 154 | this->writeU16(this->symbolId(p)); |
| 155 | } |
| 156 | this->write(f.fReturnType); |
| 157 | break; |
| 158 | } |
| 159 | case Symbol::kUnresolvedFunction_Kind: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 160 | const UnresolvedFunction& f = s.as<UnresolvedFunction>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 161 | this->writeU8(Rehydrator::kUnresolvedFunction_Command); |
| 162 | this->writeId(&f); |
| 163 | this->writeU8(f.fFunctions.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 164 | for (const FunctionDeclaration* funcDecl : f.fFunctions) { |
| 165 | this->write(*funcDecl); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 166 | } |
| 167 | break; |
| 168 | } |
| 169 | case Symbol::kType_Kind: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 170 | const Type& t = s.as<Type>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 171 | switch (t.kind()) { |
| 172 | case Type::kArray_Kind: |
| 173 | this->writeU8(Rehydrator::kArrayType_Command); |
| 174 | this->writeId(&t); |
| 175 | this->write(t.componentType()); |
| 176 | this->writeU8(t.columns()); |
| 177 | break; |
| 178 | case Type::kEnum_Kind: |
| 179 | this->writeU8(Rehydrator::kEnumType_Command); |
| 180 | this->writeId(&t); |
| 181 | this->write(t.fName); |
| 182 | break; |
| 183 | case Type::kNullable_Kind: |
| 184 | this->writeU8(Rehydrator::kNullableType_Command); |
| 185 | this->writeId(&t); |
| 186 | this->write(t.componentType()); |
| 187 | break; |
| 188 | case Type::kStruct_Kind: |
| 189 | this->writeU8(Rehydrator::kStructType_Command); |
| 190 | this->writeId(&t); |
| 191 | this->write(t.fName); |
| 192 | this->writeU8(t.fields().size()); |
| 193 | for (const Type::Field& f : t.fields()) { |
| 194 | this->write(f.fModifiers); |
| 195 | this->write(f.fName); |
| 196 | this->write(*f.fType); |
| 197 | } |
| 198 | break; |
| 199 | default: |
| 200 | this->writeU8(Rehydrator::kSystemType_Command); |
| 201 | this->writeId(&t); |
| 202 | this->write(t.fName); |
| 203 | } |
| 204 | break; |
| 205 | } |
| 206 | case Symbol::kVariable_Kind: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 207 | const Variable& v = s.as<Variable>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 208 | this->writeU8(Rehydrator::kVariable_Command); |
| 209 | this->writeId(&v); |
| 210 | this->write(v.fModifiers); |
| 211 | this->write(v.fName); |
| 212 | this->write(v.fType); |
| 213 | this->writeU8(v.fStorage); |
| 214 | break; |
| 215 | } |
| 216 | case Symbol::kField_Kind: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 217 | const Field& f = s.as<Field>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 218 | this->writeU8(Rehydrator::kField_Command); |
| 219 | this->writeU16(this->symbolId(&f.fOwner)); |
| 220 | this->writeU8(f.fFieldIndex); |
| 221 | break; |
| 222 | } |
| 223 | case Symbol::kExternal_Kind: |
| 224 | SkASSERT(false); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void Dehydrator::write(const SymbolTable& symbols) { |
| 230 | this->writeU8(Rehydrator::kSymbolTable_Command); |
| 231 | this->writeU16(symbols.fOwnedSymbols.size()); |
| 232 | for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) { |
| 233 | this->write(*s); |
| 234 | } |
| 235 | this->writeU16(symbols.fSymbols.size()); |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 236 | std::map<StringFragment, const Symbol*> ordered; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 237 | for (std::pair<StringFragment, const Symbol*> p : symbols.fSymbols) { |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 238 | ordered.insert(p); |
| 239 | } |
| 240 | for (std::pair<StringFragment, const Symbol*> p : ordered) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 241 | this->write(p.first); |
| 242 | bool found = false; |
| 243 | for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) { |
| 244 | if (symbols.fOwnedSymbols[i].get() == p.second) { |
| 245 | this->writeU16(i); |
| 246 | found = true; |
| 247 | break; |
| 248 | } |
| 249 | } |
| 250 | SkASSERT(found); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void Dehydrator::write(const Expression* e) { |
| 255 | if (e) { |
| 256 | switch (e->fKind) { |
| 257 | case Expression::kBinary_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 258 | const BinaryExpression& b = e->as<BinaryExpression>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 259 | this->writeU8(Rehydrator::kBinary_Command); |
| 260 | this->write(b.fLeft.get()); |
| 261 | this->writeU8((int) b.fOperator); |
| 262 | this->write(b.fRight.get()); |
| 263 | this->write(b.fType); |
| 264 | break; |
| 265 | } |
| 266 | case Expression::kBoolLiteral_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 267 | const BoolLiteral& b = e->as<BoolLiteral>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 268 | this->writeU8(Rehydrator::kBoolLiteral_Command); |
| 269 | this->writeU8(b.fValue); |
| 270 | break; |
| 271 | } |
| 272 | case Expression::kConstructor_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 273 | const Constructor& c = e->as<Constructor>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 274 | this->writeU8(Rehydrator::kConstructor_Command); |
| 275 | this->write(c.fType); |
| 276 | this->writeU8(c.fArguments.size()); |
| 277 | for (const auto& a : c.fArguments) { |
| 278 | this->write(a.get()); |
| 279 | } |
| 280 | break; |
| 281 | } |
| 282 | case Expression::kExternalFunctionCall_Kind: |
| 283 | case Expression::kExternalValue_Kind: |
| 284 | // not implemented; doesn't seem like we'll ever need them from within an include |
| 285 | // file |
| 286 | SkASSERT(false); |
| 287 | break; |
| 288 | case Expression::kFieldAccess_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 289 | const FieldAccess& f = e->as<FieldAccess>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 290 | this->writeU8(Rehydrator::kFieldAccess_Command); |
| 291 | this->write(f.fBase.get()); |
| 292 | this->writeU8(f.fFieldIndex); |
| 293 | this->writeU8(f.fOwnerKind); |
| 294 | break; |
| 295 | } |
| 296 | case Expression::kFloatLiteral_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 297 | const FloatLiteral& f = e->as<FloatLiteral>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 298 | this->writeU8(Rehydrator::kFloatLiteral_Command); |
| 299 | FloatIntUnion u; |
| 300 | u.fFloat = f.fValue; |
| 301 | this->writeS32(u.fInt); |
| 302 | break; |
| 303 | } |
| 304 | case Expression::kFunctionCall_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 305 | const FunctionCall& f = e->as<FunctionCall>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 306 | this->writeU8(Rehydrator::kFunctionCall_Command); |
| 307 | this->write(f.fType); |
| 308 | this->writeId(&f.fFunction); |
| 309 | this->writeU8(f.fArguments.size()); |
| 310 | for (const auto& a : f.fArguments) { |
| 311 | this->write(a.get()); |
| 312 | } |
| 313 | break; |
| 314 | } |
| 315 | case Expression::kIndex_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 316 | const IndexExpression& i = e->as<IndexExpression>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 317 | this->writeU8(Rehydrator::kIndex_Command); |
| 318 | this->write(i.fBase.get()); |
| 319 | this->write(i.fIndex.get()); |
| 320 | break; |
| 321 | } |
| 322 | case Expression::kIntLiteral_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 323 | const IntLiteral& i = e->as<IntLiteral>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 324 | this->writeU8(Rehydrator::kIntLiteral_Command); |
| 325 | this->writeS32(i.fValue); |
| 326 | break; |
| 327 | } |
| 328 | case Expression::kNullLiteral_Kind: |
| 329 | this->writeU8(Rehydrator::kNullLiteral_Command); |
| 330 | break; |
| 331 | case Expression::kPostfix_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 332 | const PostfixExpression& p = e->as<PostfixExpression>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 333 | this->writeU8(Rehydrator::kPostfix_Command); |
| 334 | this->writeU8((int) p.fOperator); |
| 335 | this->write(p.fOperand.get()); |
| 336 | break; |
| 337 | } |
| 338 | case Expression::kPrefix_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 339 | const PrefixExpression& p = e->as<PrefixExpression>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 340 | this->writeU8(Rehydrator::kPrefix_Command); |
| 341 | this->writeU8((int) p.fOperator); |
| 342 | this->write(p.fOperand.get()); |
| 343 | break; |
| 344 | } |
| 345 | case Expression::kSetting_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 346 | const Setting& s = e->as<Setting>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 347 | this->writeU8(Rehydrator::kSetting_Command); |
| 348 | this->write(s.fName); |
| 349 | this->write(s.fValue.get()); |
| 350 | break; |
| 351 | } |
| 352 | case Expression::kSwizzle_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 353 | const Swizzle& s = e->as<Swizzle>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 354 | this->writeU8(Rehydrator::kSwizzle_Command); |
| 355 | this->write(s.fBase.get()); |
| 356 | this->writeU8(s.fComponents.size()); |
| 357 | for (int c : s.fComponents) { |
| 358 | this->writeU8(c); |
| 359 | } |
| 360 | break; |
| 361 | } |
| 362 | case Expression::kTernary_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 363 | const TernaryExpression& t = e->as<TernaryExpression>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 364 | this->writeU8(Rehydrator::kTernary_Command); |
| 365 | this->write(t.fTest.get()); |
| 366 | this->write(t.fIfTrue.get()); |
| 367 | this->write(t.fIfFalse.get()); |
| 368 | break; |
| 369 | } |
| 370 | case Expression::kVariableReference_Kind: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 371 | const VariableReference& v = e->as<VariableReference>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 372 | this->writeU8(Rehydrator::kVariableReference_Command); |
| 373 | this->writeId(&v.fVariable); |
| 374 | this->writeU8(v.fRefKind); |
| 375 | break; |
| 376 | } |
| 377 | case Expression::kFunctionReference_Kind: |
| 378 | case Expression::kTypeReference_Kind: |
| 379 | case Expression::kDefined_Kind: |
| 380 | // shouldn't appear in finished code |
| 381 | SkASSERT(false); |
| 382 | break; |
| 383 | } |
| 384 | } else { |
| 385 | this->writeU8(Rehydrator::kVoid_Command); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | void Dehydrator::write(const Statement* s) { |
| 390 | if (s) { |
| 391 | switch (s->fKind) { |
| 392 | case Statement::kBlock_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 393 | const Block& b = s->as<Block>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 394 | this->writeU8(Rehydrator::kBlock_Command); |
| 395 | AutoDehydratorSymbolTable symbols(this, b.fSymbols); |
| 396 | this->writeU8(b.fStatements.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 397 | for (const std::unique_ptr<Statement>& blockStmt : b.fStatements) { |
| 398 | this->write(blockStmt.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 399 | } |
| 400 | this->writeU8(b.fIsScope); |
| 401 | break; |
| 402 | } |
| 403 | case Statement::kBreak_Kind: |
| 404 | this->writeU8(Rehydrator::kBreak_Command); |
| 405 | break; |
| 406 | case Statement::kContinue_Kind: |
| 407 | this->writeU8(Rehydrator::kContinue_Command); |
| 408 | break; |
| 409 | case Statement::kDiscard_Kind: |
| 410 | this->writeU8(Rehydrator::kDiscard_Command); |
| 411 | break; |
| 412 | case Statement::kDo_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 413 | const DoStatement& d = s->as<DoStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 414 | this->writeU8(Rehydrator::kDo_Command); |
| 415 | this->write(d.fStatement.get()); |
| 416 | this->write(d.fTest.get()); |
| 417 | break; |
| 418 | } |
| 419 | case Statement::kExpression_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 420 | const ExpressionStatement& e = s->as<ExpressionStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 421 | this->writeU8(Rehydrator::kExpressionStatement_Command); |
| 422 | this->write(e.fExpression.get()); |
| 423 | break; |
| 424 | } |
| 425 | case Statement::kFor_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 426 | const ForStatement& f = s->as<ForStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 427 | this->writeU8(Rehydrator::kFor_Command); |
| 428 | this->write(f.fInitializer.get()); |
| 429 | this->write(f.fTest.get()); |
| 430 | this->write(f.fNext.get()); |
| 431 | this->write(f.fStatement.get()); |
| 432 | this->write(f.fSymbols); |
| 433 | break; |
| 434 | } |
| 435 | case Statement::kIf_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 436 | const IfStatement& i = s->as<IfStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 437 | this->writeU8(Rehydrator::kIf_Command); |
| 438 | this->writeU8(i.fIsStatic); |
| 439 | this->write(i.fTest.get()); |
| 440 | this->write(i.fIfTrue.get()); |
| 441 | this->write(i.fIfFalse.get()); |
| 442 | break; |
| 443 | } |
| 444 | case Statement::kNop_Kind: |
| 445 | SkASSERT(false); |
| 446 | break; |
| 447 | case Statement::kReturn_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 448 | const ReturnStatement& r = s->as<ReturnStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 449 | this->writeU8(Rehydrator::kReturn_Command); |
| 450 | this->write(r.fExpression.get()); |
| 451 | break; |
| 452 | } |
| 453 | case Statement::kSwitch_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 454 | const SwitchStatement& ss = s->as<SwitchStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 455 | this->writeU8(Rehydrator::kSwitch_Command); |
| 456 | this->writeU8(ss.fIsStatic); |
| 457 | AutoDehydratorSymbolTable symbols(this, ss.fSymbols); |
| 458 | this->write(ss.fValue.get()); |
| 459 | this->writeU8(ss.fCases.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 460 | for (const std::unique_ptr<SwitchCase>& sc : ss.fCases) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 461 | this->write(sc->fValue.get()); |
| 462 | this->writeU8(sc->fStatements.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 463 | for (const std::unique_ptr<Statement>& stmt : sc->fStatements) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 464 | this->write(stmt.get()); |
| 465 | } |
| 466 | } |
| 467 | break; |
| 468 | } |
| 469 | case Statement::kVarDeclaration_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 470 | const VarDeclaration& v = s->as<VarDeclaration>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 471 | this->writeU8(Rehydrator::kVarDeclaration_Command); |
| 472 | this->writeU16(this->symbolId(v.fVar)); |
| 473 | this->writeU8(v.fSizes.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 474 | for (const std::unique_ptr<Expression>& sizeExpr : v.fSizes) { |
| 475 | this->write(sizeExpr.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 476 | } |
| 477 | this->write(v.fValue.get()); |
| 478 | break; |
| 479 | } |
| 480 | case Statement::kVarDeclarations_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 481 | const VarDeclarationsStatement& v = s->as<VarDeclarationsStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 482 | this->write(*v.fDeclaration); |
| 483 | break; |
| 484 | } |
| 485 | case Statement::kWhile_Kind: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 486 | const WhileStatement& w = s->as<WhileStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 487 | this->writeU8(Rehydrator::kWhile_Command); |
| 488 | this->write(w.fTest.get()); |
| 489 | this->write(w.fStatement.get()); |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | } else { |
| 494 | this->writeU8(Rehydrator::kVoid_Command); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | void Dehydrator::write(const ProgramElement& e) { |
| 499 | switch (e.fKind) { |
| 500 | case ProgramElement::kEnum_Kind: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 501 | const Enum& en = e.as<Enum>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 502 | this->writeU8(Rehydrator::kEnum_Command); |
| 503 | this->write(en.fTypeName); |
| 504 | AutoDehydratorSymbolTable symbols(this, en.fSymbols); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 505 | for (const std::unique_ptr<const Symbol>& s : en.fSymbols->fOwnedSymbols) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 506 | SkASSERT(s->fKind == Symbol::kVariable_Kind); |
| 507 | Variable& v = (Variable&) *s; |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 508 | SkASSERT(v.fInitialValue); |
| 509 | const IntLiteral& i = v.fInitialValue->as<IntLiteral>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 510 | this->writeS32(i.fValue); |
| 511 | } |
| 512 | break; |
| 513 | } |
| 514 | case ProgramElement::kExtension_Kind: |
| 515 | SkASSERT(false); |
| 516 | break; |
| 517 | case ProgramElement::kFunction_Kind: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 518 | const FunctionDefinition& f = e.as<FunctionDefinition>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 519 | this->writeU8(Rehydrator::kFunctionDefinition_Command); |
| 520 | this->writeU16(this->symbolId(&f.fDeclaration)); |
| 521 | this->write(f.fBody.get()); |
| 522 | this->writeU8(f.fReferencedIntrinsics.size()); |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 523 | std::set<uint16_t> ordered; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 524 | for (const FunctionDeclaration* ref : f.fReferencedIntrinsics) { |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 525 | ordered.insert(this->symbolId(ref)); |
| 526 | } |
| 527 | for (uint16_t ref : ordered) { |
| 528 | this->writeU16(ref); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 529 | } |
| 530 | break; |
| 531 | } |
| 532 | case ProgramElement::kInterfaceBlock_Kind: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 533 | const InterfaceBlock& i = e.as<InterfaceBlock>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 534 | this->writeU8(Rehydrator::kInterfaceBlock_Command); |
| 535 | this->write(i.fVariable); |
| 536 | this->write(i.fTypeName); |
| 537 | this->write(i.fInstanceName); |
| 538 | this->writeU8(i.fSizes.size()); |
| 539 | for (const auto& s : i.fSizes) { |
| 540 | this->write(s.get()); |
| 541 | } |
| 542 | break; |
| 543 | } |
| 544 | case ProgramElement::kModifiers_Kind: |
| 545 | SkASSERT(false); |
| 546 | break; |
| 547 | case ProgramElement::kSection_Kind: |
| 548 | SkASSERT(false); |
| 549 | break; |
| 550 | case ProgramElement::kVar_Kind: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 551 | const VarDeclarations& v = e.as<VarDeclarations>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 552 | this->writeU8(Rehydrator::kVarDeclarations_Command); |
| 553 | this->write(v.fBaseType); |
| 554 | this->writeU8(v.fVars.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 555 | for (const auto& var : v.fVars) { |
| 556 | this->write(var.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 557 | } |
| 558 | break; |
| 559 | } |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) { |
| 564 | this->writeU8(Rehydrator::kElements_Command); |
| 565 | this->writeU8(elements.size()); |
| 566 | for (const auto& e : elements) { |
| 567 | this->write(*e); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | void Dehydrator::finish(OutputStream& out) { |
| 572 | out.write16(fStringBuffer.str().size()); |
| 573 | out.writeString(fStringBuffer.str()); |
| 574 | out.writeString(fBody.str()); |
| 575 | } |
| 576 | |
| 577 | } // namespace |
| 578 | |
| 579 | #endif |