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" |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 29 | #include "src/sksl/ir/SkSLInlineMarker.h" |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 30 | #include "src/sksl/ir/SkSLIntLiteral.h" |
| 31 | #include "src/sksl/ir/SkSLInterfaceBlock.h" |
| 32 | #include "src/sksl/ir/SkSLNullLiteral.h" |
| 33 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 34 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 35 | #include "src/sksl/ir/SkSLProgramElement.h" |
| 36 | #include "src/sksl/ir/SkSLReturnStatement.h" |
| 37 | #include "src/sksl/ir/SkSLSetting.h" |
| 38 | #include "src/sksl/ir/SkSLStatement.h" |
| 39 | #include "src/sksl/ir/SkSLSwitchCase.h" |
| 40 | #include "src/sksl/ir/SkSLSwitchStatement.h" |
| 41 | #include "src/sksl/ir/SkSLSwizzle.h" |
| 42 | #include "src/sksl/ir/SkSLSymbol.h" |
| 43 | #include "src/sksl/ir/SkSLSymbolTable.h" |
| 44 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 45 | #include "src/sksl/ir/SkSLUnresolvedFunction.h" |
| 46 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 47 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 48 | #include "src/sksl/ir/SkSLVariable.h" |
| 49 | #include "src/sksl/ir/SkSLWhileStatement.h" |
| 50 | |
| 51 | #ifdef SKSL_STANDALONE |
| 52 | |
| 53 | namespace SkSL { |
| 54 | |
| 55 | static constexpr int HEADER_SIZE = 2; |
| 56 | |
| 57 | class AutoDehydratorSymbolTable { |
| 58 | public: |
| 59 | AutoDehydratorSymbolTable(Dehydrator* dehydrator, const std::shared_ptr<SymbolTable>& symbols) |
| 60 | : fDehydrator(dehydrator) { |
| 61 | dehydrator->fSymbolMap.emplace_back(); |
| 62 | if (symbols) { |
| 63 | dehydrator->write(*symbols); |
| 64 | } else { |
| 65 | dehydrator->writeU8(Rehydrator::kVoid_Command); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | ~AutoDehydratorSymbolTable() { |
| 70 | fDehydrator->fSymbolMap.pop_back(); |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | Dehydrator* fDehydrator; |
| 75 | }; |
| 76 | |
| 77 | void Dehydrator::write(Layout l) { |
| 78 | if (l == Layout()) { |
| 79 | this->writeU8(Rehydrator::kDefaultLayout_Command); |
| 80 | } else if (l == Layout::builtin(l.fBuiltin)) { |
| 81 | this->writeS8(Rehydrator::kBuiltinLayout_Command); |
| 82 | this->writeS16(l.fBuiltin); |
| 83 | } else { |
| 84 | this->writeS8(Rehydrator::kLayout_Command); |
| 85 | fBody.write32(l.fFlags); |
| 86 | this->writeS8(l.fLocation); |
| 87 | this->writeS8(l.fOffset); |
| 88 | this->writeS8(l.fBinding); |
| 89 | this->writeS8(l.fIndex); |
| 90 | this->writeS8(l.fSet); |
| 91 | this->writeS16(l.fBuiltin); |
| 92 | this->writeS8(l.fInputAttachmentIndex); |
| 93 | this->writeS8((int) l.fFormat); |
| 94 | this->writeS8(l.fPrimitive); |
| 95 | this->writeS8(l.fMaxVertices); |
| 96 | this->writeS8(l.fInvocations); |
| 97 | this->write(l.fMarker); |
| 98 | this->write(l.fWhen); |
| 99 | this->writeS8(l.fKey); |
| 100 | this->writeS8((int) l.fCType); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void Dehydrator::write(Modifiers m) { |
| 105 | if (m == Modifiers()) { |
| 106 | this->writeU8(Rehydrator::kDefaultModifiers_Command); |
| 107 | } else { |
| 108 | if (m.fFlags <= 255) { |
| 109 | this->writeU8(Rehydrator::kModifiers8Bit_Command); |
| 110 | this->write(m.fLayout); |
| 111 | this->writeU8(m.fFlags); |
| 112 | } else { |
| 113 | this->writeU8(Rehydrator::kModifiers_Command); |
| 114 | this->write(m.fLayout); |
| 115 | this->writeS32(m.fFlags); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | void Dehydrator::write(StringFragment s) { |
| 121 | this->write(String(s)); |
| 122 | } |
| 123 | |
| 124 | void Dehydrator::write(String s) { |
| 125 | auto found = fStrings.find(s); |
| 126 | int offset; |
| 127 | if (found == fStrings.end()) { |
| 128 | offset = fStringBuffer.str().length() + HEADER_SIZE; |
| 129 | fStrings.insert({ s, offset }); |
| 130 | SkASSERT(s.length() <= 255); |
| 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) { |
| 142 | this->writeU8(Rehydrator::kSymbolRef_Command); |
| 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>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 149 | this->writeU8(Rehydrator::kFunctionDeclaration_Command); |
| 150 | this->writeId(&f); |
| 151 | this->write(f.fModifiers); |
| 152 | this->write(f.fName); |
| 153 | this->writeU8(f.fParameters.size()); |
| 154 | for (const Variable* p : f.fParameters) { |
| 155 | this->writeU16(this->symbolId(p)); |
| 156 | } |
| 157 | this->write(f.fReturnType); |
| 158 | break; |
| 159 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 160 | case Symbol::Kind::kUnresolvedFunction: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 161 | const UnresolvedFunction& f = s.as<UnresolvedFunction>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 162 | this->writeU8(Rehydrator::kUnresolvedFunction_Command); |
| 163 | this->writeId(&f); |
| 164 | this->writeU8(f.fFunctions.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 165 | for (const FunctionDeclaration* funcDecl : f.fFunctions) { |
| 166 | this->write(*funcDecl); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 167 | } |
| 168 | break; |
| 169 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 170 | case Symbol::Kind::kType: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 171 | const Type& t = s.as<Type>(); |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 172 | switch (t.typeKind()) { |
| 173 | case Type::TypeKind::kArray: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 174 | this->writeU8(Rehydrator::kArrayType_Command); |
| 175 | this->writeId(&t); |
| 176 | this->write(t.componentType()); |
| 177 | this->writeU8(t.columns()); |
| 178 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 179 | case Type::TypeKind::kEnum: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 180 | this->writeU8(Rehydrator::kEnumType_Command); |
| 181 | this->writeId(&t); |
| 182 | this->write(t.fName); |
| 183 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 184 | case Type::TypeKind::kNullable: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 185 | this->writeU8(Rehydrator::kNullableType_Command); |
| 186 | this->writeId(&t); |
| 187 | this->write(t.componentType()); |
| 188 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 189 | case Type::TypeKind::kStruct: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 190 | this->writeU8(Rehydrator::kStructType_Command); |
| 191 | this->writeId(&t); |
| 192 | this->write(t.fName); |
| 193 | this->writeU8(t.fields().size()); |
| 194 | for (const Type::Field& f : t.fields()) { |
| 195 | this->write(f.fModifiers); |
| 196 | this->write(f.fName); |
| 197 | this->write(*f.fType); |
| 198 | } |
| 199 | break; |
| 200 | default: |
| 201 | this->writeU8(Rehydrator::kSystemType_Command); |
| 202 | this->writeId(&t); |
| 203 | this->write(t.fName); |
| 204 | } |
| 205 | break; |
| 206 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 207 | case Symbol::Kind::kVariable: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 208 | const Variable& v = s.as<Variable>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 209 | this->writeU8(Rehydrator::kVariable_Command); |
| 210 | this->writeId(&v); |
| 211 | this->write(v.fModifiers); |
| 212 | this->write(v.fName); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 213 | this->write(v.type()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 214 | this->writeU8(v.fStorage); |
| 215 | break; |
| 216 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 217 | case Symbol::Kind::kField: { |
John Stiles | 17c5b70 | 2020-08-18 10:40:03 -0400 | [diff] [blame] | 218 | const Field& f = s.as<Field>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 219 | this->writeU8(Rehydrator::kField_Command); |
| 220 | this->writeU16(this->symbolId(&f.fOwner)); |
| 221 | this->writeU8(f.fFieldIndex); |
| 222 | break; |
| 223 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 224 | case Symbol::Kind::kExternal: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 225 | SkASSERT(false); |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void Dehydrator::write(const SymbolTable& symbols) { |
| 231 | this->writeU8(Rehydrator::kSymbolTable_Command); |
| 232 | this->writeU16(symbols.fOwnedSymbols.size()); |
| 233 | for (const std::unique_ptr<const Symbol>& s : symbols.fOwnedSymbols) { |
| 234 | this->write(*s); |
| 235 | } |
| 236 | this->writeU16(symbols.fSymbols.size()); |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 237 | std::map<StringFragment, const Symbol*> ordered; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 238 | for (std::pair<StringFragment, const Symbol*> p : symbols.fSymbols) { |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 239 | ordered.insert(p); |
| 240 | } |
| 241 | for (std::pair<StringFragment, const Symbol*> p : ordered) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 242 | this->write(p.first); |
| 243 | bool found = false; |
| 244 | for (size_t i = 0; i < symbols.fOwnedSymbols.size(); ++i) { |
| 245 | if (symbols.fOwnedSymbols[i].get() == p.second) { |
| 246 | this->writeU16(i); |
| 247 | found = true; |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | SkASSERT(found); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void Dehydrator::write(const Expression* e) { |
| 256 | if (e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 257 | switch (e->kind()) { |
| 258 | case Expression::Kind::kBinary: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 259 | const BinaryExpression& b = e->as<BinaryExpression>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 260 | this->writeU8(Rehydrator::kBinary_Command); |
Ethan Nicholas | c8d9c8e | 2020-09-22 15:05:37 -0400 | [diff] [blame] | 261 | this->write(&b.left()); |
| 262 | this->writeU8((int) b.getOperator()); |
| 263 | this->write(&b.right()); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 264 | this->write(b.type()); |
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>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 269 | this->writeU8(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 | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 273 | case Expression::Kind::kConstructor: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 274 | const Constructor& c = e->as<Constructor>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 275 | this->writeU8(Rehydrator::kConstructor_Command); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 276 | this->write(c.type()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 277 | this->writeU8(c.fArguments.size()); |
| 278 | for (const auto& a : c.fArguments) { |
| 279 | this->write(a.get()); |
| 280 | } |
| 281 | break; |
| 282 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 283 | case Expression::Kind::kExternalFunctionCall: |
| 284 | case Expression::Kind::kExternalValue: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 285 | // not implemented; doesn't seem like we'll ever need them from within an include |
| 286 | // file |
| 287 | SkASSERT(false); |
| 288 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 289 | case Expression::Kind::kFieldAccess: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 290 | const FieldAccess& f = e->as<FieldAccess>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 291 | this->writeU8(Rehydrator::kFieldAccess_Command); |
| 292 | this->write(f.fBase.get()); |
| 293 | this->writeU8(f.fFieldIndex); |
| 294 | this->writeU8(f.fOwnerKind); |
| 295 | break; |
| 296 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 297 | case Expression::Kind::kFloatLiteral: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 298 | const FloatLiteral& f = e->as<FloatLiteral>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 299 | this->writeU8(Rehydrator::kFloatLiteral_Command); |
| 300 | FloatIntUnion u; |
| 301 | u.fFloat = f.fValue; |
| 302 | this->writeS32(u.fInt); |
| 303 | break; |
| 304 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 305 | case Expression::Kind::kFunctionCall: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 306 | const FunctionCall& f = e->as<FunctionCall>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 307 | this->writeU8(Rehydrator::kFunctionCall_Command); |
Ethan Nicholas | 30d3022 | 2020-09-11 12:27:26 -0400 | [diff] [blame] | 308 | this->write(f.type()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 309 | this->writeId(&f.fFunction); |
| 310 | this->writeU8(f.fArguments.size()); |
| 311 | for (const auto& a : f.fArguments) { |
| 312 | this->write(a.get()); |
| 313 | } |
| 314 | break; |
| 315 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 316 | case Expression::Kind::kIndex: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 317 | const IndexExpression& i = e->as<IndexExpression>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 318 | this->writeU8(Rehydrator::kIndex_Command); |
| 319 | this->write(i.fBase.get()); |
| 320 | this->write(i.fIndex.get()); |
| 321 | break; |
| 322 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 323 | case Expression::Kind::kIntLiteral: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 324 | const IntLiteral& i = e->as<IntLiteral>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 325 | this->writeU8(Rehydrator::kIntLiteral_Command); |
| 326 | this->writeS32(i.fValue); |
| 327 | break; |
| 328 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 329 | case Expression::Kind::kNullLiteral: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 330 | this->writeU8(Rehydrator::kNullLiteral_Command); |
| 331 | break; |
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>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 334 | this->writeU8(Rehydrator::kPostfix_Command); |
| 335 | this->writeU8((int) p.fOperator); |
| 336 | this->write(p.fOperand.get()); |
| 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>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 341 | this->writeU8(Rehydrator::kPrefix_Command); |
| 342 | this->writeU8((int) p.fOperator); |
| 343 | this->write(p.fOperand.get()); |
| 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>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 348 | this->writeU8(Rehydrator::kSetting_Command); |
| 349 | this->write(s.fName); |
| 350 | this->write(s.fValue.get()); |
| 351 | break; |
| 352 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 353 | case Expression::Kind::kSwizzle: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 354 | const Swizzle& s = e->as<Swizzle>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 355 | this->writeU8(Rehydrator::kSwizzle_Command); |
| 356 | this->write(s.fBase.get()); |
| 357 | this->writeU8(s.fComponents.size()); |
| 358 | for (int c : s.fComponents) { |
| 359 | this->writeU8(c); |
| 360 | } |
| 361 | break; |
| 362 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 363 | case Expression::Kind::kTernary: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 364 | const TernaryExpression& t = e->as<TernaryExpression>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 365 | this->writeU8(Rehydrator::kTernary_Command); |
| 366 | this->write(t.fTest.get()); |
| 367 | this->write(t.fIfTrue.get()); |
| 368 | this->write(t.fIfFalse.get()); |
| 369 | break; |
| 370 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 371 | case Expression::Kind::kVariableReference: { |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 372 | const VariableReference& v = e->as<VariableReference>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 373 | this->writeU8(Rehydrator::kVariableReference_Command); |
Brian Osman | 79457ef | 2020-09-24 15:01:27 -0400 | [diff] [blame] | 374 | this->writeId(v.fVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 375 | this->writeU8(v.fRefKind); |
| 376 | break; |
| 377 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 378 | case Expression::Kind::kFunctionReference: |
| 379 | case Expression::Kind::kTypeReference: |
| 380 | case Expression::Kind::kDefined: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 381 | // shouldn't appear in finished code |
| 382 | SkASSERT(false); |
| 383 | break; |
| 384 | } |
| 385 | } else { |
| 386 | this->writeU8(Rehydrator::kVoid_Command); |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | void Dehydrator::write(const Statement* s) { |
| 391 | if (s) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 392 | switch (s->kind()) { |
| 393 | case Statement::Kind::kBlock: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 394 | const Block& b = s->as<Block>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 395 | this->writeU8(Rehydrator::kBlock_Command); |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 396 | AutoDehydratorSymbolTable symbols(this, b.symbolTable()); |
| 397 | this->writeU8(b.children().size()); |
| 398 | for (const std::unique_ptr<Statement>& blockStmt : b.children()) { |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 399 | this->write(blockStmt.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 400 | } |
Ethan Nicholas | 7bd6043 | 2020-09-25 14:31:59 -0400 | [diff] [blame] | 401 | this->writeU8(b.isScope()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 402 | break; |
| 403 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 404 | case Statement::Kind::kBreak: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 405 | this->writeU8(Rehydrator::kBreak_Command); |
| 406 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 407 | case Statement::Kind::kContinue: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 408 | this->writeU8(Rehydrator::kContinue_Command); |
| 409 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 410 | case Statement::Kind::kDiscard: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 411 | this->writeU8(Rehydrator::kDiscard_Command); |
| 412 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 413 | case Statement::Kind::kDo: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 414 | const DoStatement& d = s->as<DoStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 415 | this->writeU8(Rehydrator::kDo_Command); |
| 416 | this->write(d.fStatement.get()); |
| 417 | this->write(d.fTest.get()); |
| 418 | break; |
| 419 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 420 | case Statement::Kind::kExpression: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 421 | const ExpressionStatement& e = s->as<ExpressionStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 422 | this->writeU8(Rehydrator::kExpressionStatement_Command); |
| 423 | this->write(e.fExpression.get()); |
| 424 | break; |
| 425 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 426 | case Statement::Kind::kFor: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 427 | const ForStatement& f = s->as<ForStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 428 | this->writeU8(Rehydrator::kFor_Command); |
| 429 | this->write(f.fInitializer.get()); |
| 430 | this->write(f.fTest.get()); |
| 431 | this->write(f.fNext.get()); |
| 432 | this->write(f.fStatement.get()); |
| 433 | this->write(f.fSymbols); |
| 434 | break; |
| 435 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 436 | case Statement::Kind::kIf: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 437 | const IfStatement& i = s->as<IfStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 438 | this->writeU8(Rehydrator::kIf_Command); |
| 439 | this->writeU8(i.fIsStatic); |
| 440 | this->write(i.fTest.get()); |
| 441 | this->write(i.fIfTrue.get()); |
| 442 | this->write(i.fIfFalse.get()); |
| 443 | break; |
| 444 | } |
John Stiles | 98c1f82 | 2020-09-09 14:18:53 -0400 | [diff] [blame] | 445 | case Statement::Kind::kInlineMarker: { |
| 446 | const InlineMarker& i = s->as<InlineMarker>(); |
| 447 | this->writeU8(Rehydrator::kInlineMarker_Command); |
| 448 | this->writeId(i.fFuncDecl); |
| 449 | break; |
| 450 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 451 | case Statement::Kind::kNop: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 452 | SkASSERT(false); |
| 453 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 454 | case Statement::Kind::kReturn: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 455 | const ReturnStatement& r = s->as<ReturnStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 456 | this->writeU8(Rehydrator::kReturn_Command); |
| 457 | this->write(r.fExpression.get()); |
| 458 | break; |
| 459 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 460 | case Statement::Kind::kSwitch: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 461 | const SwitchStatement& ss = s->as<SwitchStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 462 | this->writeU8(Rehydrator::kSwitch_Command); |
| 463 | this->writeU8(ss.fIsStatic); |
| 464 | AutoDehydratorSymbolTable symbols(this, ss.fSymbols); |
| 465 | this->write(ss.fValue.get()); |
| 466 | this->writeU8(ss.fCases.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 467 | for (const std::unique_ptr<SwitchCase>& sc : ss.fCases) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 468 | this->write(sc->fValue.get()); |
| 469 | this->writeU8(sc->fStatements.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 470 | for (const std::unique_ptr<Statement>& stmt : sc->fStatements) { |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 471 | this->write(stmt.get()); |
| 472 | } |
| 473 | } |
| 474 | break; |
| 475 | } |
Brian Osman | 9eb848a | 2020-09-11 15:53:40 -0400 | [diff] [blame] | 476 | case Statement::Kind::kSwitchCase: |
| 477 | SkASSERT(false); |
| 478 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 479 | case Statement::Kind::kVarDeclaration: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 480 | const VarDeclaration& v = s->as<VarDeclaration>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 481 | this->writeU8(Rehydrator::kVarDeclaration_Command); |
| 482 | this->writeU16(this->symbolId(v.fVar)); |
| 483 | this->writeU8(v.fSizes.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 484 | for (const std::unique_ptr<Expression>& sizeExpr : v.fSizes) { |
| 485 | this->write(sizeExpr.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 486 | } |
| 487 | this->write(v.fValue.get()); |
| 488 | break; |
| 489 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 490 | case Statement::Kind::kVarDeclarations: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 491 | const VarDeclarationsStatement& v = s->as<VarDeclarationsStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 492 | this->write(*v.fDeclaration); |
| 493 | break; |
| 494 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 495 | case Statement::Kind::kWhile: { |
John Stiles | 26f9850 | 2020-08-18 09:30:51 -0400 | [diff] [blame] | 496 | const WhileStatement& w = s->as<WhileStatement>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 497 | this->writeU8(Rehydrator::kWhile_Command); |
| 498 | this->write(w.fTest.get()); |
| 499 | this->write(w.fStatement.get()); |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | } else { |
| 504 | this->writeU8(Rehydrator::kVoid_Command); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | void Dehydrator::write(const ProgramElement& e) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 509 | switch (e.kind()) { |
| 510 | case ProgramElement::Kind::kEnum: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 511 | const Enum& en = e.as<Enum>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 512 | this->writeU8(Rehydrator::kEnum_Command); |
| 513 | this->write(en.fTypeName); |
| 514 | AutoDehydratorSymbolTable symbols(this, en.fSymbols); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 515 | for (const std::unique_ptr<const Symbol>& s : en.fSymbols->fOwnedSymbols) { |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 516 | SkASSERT(s->kind() == Symbol::Kind::kVariable); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 517 | Variable& v = (Variable&) *s; |
John Stiles | 81365af | 2020-08-18 09:24:00 -0400 | [diff] [blame] | 518 | SkASSERT(v.fInitialValue); |
| 519 | const IntLiteral& i = v.fInitialValue->as<IntLiteral>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 520 | this->writeS32(i.fValue); |
| 521 | } |
| 522 | break; |
| 523 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 524 | case ProgramElement::Kind::kExtension: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 525 | SkASSERT(false); |
| 526 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 527 | case ProgramElement::Kind::kFunction: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 528 | const FunctionDefinition& f = e.as<FunctionDefinition>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 529 | this->writeU8(Rehydrator::kFunctionDefinition_Command); |
| 530 | this->writeU16(this->symbolId(&f.fDeclaration)); |
| 531 | this->write(f.fBody.get()); |
| 532 | this->writeU8(f.fReferencedIntrinsics.size()); |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 533 | std::set<uint16_t> ordered; |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 534 | for (const FunctionDeclaration* ref : f.fReferencedIntrinsics) { |
Ethan Nicholas | 7154b74 | 2020-07-31 13:18:02 -0400 | [diff] [blame] | 535 | ordered.insert(this->symbolId(ref)); |
| 536 | } |
| 537 | for (uint16_t ref : ordered) { |
| 538 | this->writeU16(ref); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 539 | } |
| 540 | break; |
| 541 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 542 | case ProgramElement::Kind::kInterfaceBlock: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 543 | const InterfaceBlock& i = e.as<InterfaceBlock>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 544 | this->writeU8(Rehydrator::kInterfaceBlock_Command); |
| 545 | this->write(i.fVariable); |
| 546 | this->write(i.fTypeName); |
| 547 | this->write(i.fInstanceName); |
| 548 | this->writeU8(i.fSizes.size()); |
| 549 | for (const auto& s : i.fSizes) { |
| 550 | this->write(s.get()); |
| 551 | } |
| 552 | break; |
| 553 | } |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 554 | case ProgramElement::Kind::kModifiers: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 555 | SkASSERT(false); |
| 556 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 557 | case ProgramElement::Kind::kSection: |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 558 | SkASSERT(false); |
| 559 | break; |
Ethan Nicholas | e659214 | 2020-09-08 10:22:09 -0400 | [diff] [blame] | 560 | case ProgramElement::Kind::kVar: { |
John Stiles | 3dc0da6 | 2020-08-19 17:48:31 -0400 | [diff] [blame] | 561 | const VarDeclarations& v = e.as<VarDeclarations>(); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 562 | this->writeU8(Rehydrator::kVarDeclarations_Command); |
| 563 | this->write(v.fBaseType); |
| 564 | this->writeU8(v.fVars.size()); |
John Stiles | f621e23 | 2020-08-25 13:33:02 -0400 | [diff] [blame] | 565 | for (const auto& var : v.fVars) { |
| 566 | this->write(var.get()); |
Ethan Nicholas | c18bb51 | 2020-07-28 14:46:53 -0400 | [diff] [blame] | 567 | } |
| 568 | break; |
| 569 | } |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | void Dehydrator::write(const std::vector<std::unique_ptr<ProgramElement>>& elements) { |
| 574 | this->writeU8(Rehydrator::kElements_Command); |
| 575 | this->writeU8(elements.size()); |
| 576 | for (const auto& e : elements) { |
| 577 | this->write(*e); |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void Dehydrator::finish(OutputStream& out) { |
| 582 | out.write16(fStringBuffer.str().size()); |
| 583 | out.writeString(fStringBuffer.str()); |
| 584 | out.writeString(fBody.str()); |
| 585 | } |
| 586 | |
| 587 | } // namespace |
| 588 | |
| 589 | #endif |