Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "src/sksl/SkSLByteCodeGenerator.h" |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 9 | #include "src/sksl/SkSLInterpreter.h" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 10 | |
| 11 | namespace SkSL { |
| 12 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 13 | ByteCodeGenerator::ByteCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
| 14 | ByteCode* output) |
| 15 | : INHERITED(program, errors, nullptr) |
| 16 | , fContext(*context) |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 17 | , fOutput(output) |
| 18 | , fIntrinsics { |
| 19 | { "cos", ByteCodeInstruction::kCos }, |
| 20 | { "cross", ByteCodeInstruction::kCross }, |
| 21 | { "dot", SpecialIntrinsic::kDot }, |
| 22 | { "sin", ByteCodeInstruction::kSin }, |
| 23 | { "sqrt", ByteCodeInstruction::kSqrt }, |
| 24 | { "tan", ByteCodeInstruction::kTan }, |
| 25 | { "mix", ByteCodeInstruction::kMix }, |
| 26 | } {} |
| 27 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 28 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 29 | int ByteCodeGenerator::SlotCount(const Type& type) { |
| 30 | if (type.kind() == Type::kStruct_Kind) { |
| 31 | int slots = 0; |
| 32 | for (const auto& f : type.fields()) { |
| 33 | slots += SlotCount(*f.fType); |
| 34 | } |
| 35 | SkASSERT(slots <= 255); |
| 36 | return slots; |
| 37 | } else if (type.kind() == Type::kArray_Kind) { |
| 38 | int columns = type.columns(); |
| 39 | SkASSERT(columns >= 0); |
| 40 | int slots = columns * SlotCount(type.componentType()); |
| 41 | SkASSERT(slots <= 255); |
| 42 | return slots; |
| 43 | } else { |
| 44 | return type.columns() * type.rows(); |
| 45 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | bool ByteCodeGenerator::generateCode() { |
| 49 | for (const auto& e : fProgram) { |
| 50 | switch (e.fKind) { |
| 51 | case ProgramElement::kFunction_Kind: { |
| 52 | std::unique_ptr<ByteCodeFunction> f = this->writeFunction((FunctionDefinition&) e); |
| 53 | if (!f) { |
| 54 | return false; |
| 55 | } |
| 56 | fOutput->fFunctions.push_back(std::move(f)); |
| 57 | break; |
| 58 | } |
| 59 | case ProgramElement::kVar_Kind: { |
| 60 | VarDeclarations& decl = (VarDeclarations&) e; |
| 61 | for (const auto& v : decl.fVars) { |
| 62 | const Variable* declVar = ((VarDeclaration&) *v).fVar; |
| 63 | if (declVar->fModifiers.fLayout.fBuiltin >= 0) { |
| 64 | continue; |
| 65 | } |
| 66 | if (declVar->fModifiers.fFlags & Modifiers::kIn_Flag) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 67 | for (int i = SlotCount(declVar->fType); i > 0; --i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 68 | fOutput->fInputSlots.push_back(fOutput->fGlobalCount++); |
| 69 | } |
| 70 | } else { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 71 | fOutput->fGlobalCount += SlotCount(declVar->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | break; |
| 75 | } |
| 76 | default: |
| 77 | ; // ignore |
| 78 | } |
| 79 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 80 | for (auto& call : fCallTargets) { |
| 81 | if (!call.set()) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 85 | return true; |
| 86 | } |
| 87 | |
| 88 | std::unique_ptr<ByteCodeFunction> ByteCodeGenerator::writeFunction(const FunctionDefinition& f) { |
| 89 | fFunction = &f; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 90 | std::unique_ptr<ByteCodeFunction> result(new ByteCodeFunction(&f.fDeclaration)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 91 | fParameterCount = 0; |
| 92 | for (const auto& p : f.fDeclaration.fParameters) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 93 | fParameterCount += SlotCount(p->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 94 | } |
| 95 | fCode = &result->fCode; |
| 96 | this->writeStatement(*f.fBody); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 97 | this->write(ByteCodeInstruction::kReturn); |
| 98 | this->write8(0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 99 | result->fParameterCount = fParameterCount; |
| 100 | result->fLocalCount = fLocals.size(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 101 | const Type& returnType = f.fDeclaration.fReturnType; |
| 102 | if (returnType != *fContext.fVoid_Type) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 103 | result->fReturnCount = SlotCount(returnType); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 104 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 105 | fLocals.clear(); |
| 106 | fFunction = nullptr; |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | enum class TypeCategory { |
| 111 | kBool, |
| 112 | kSigned, |
| 113 | kUnsigned, |
| 114 | kFloat, |
| 115 | }; |
| 116 | |
| 117 | static TypeCategory type_category(const Type& type) { |
| 118 | switch (type.kind()) { |
| 119 | case Type::Kind::kVector_Kind: |
| 120 | case Type::Kind::kMatrix_Kind: |
| 121 | return type_category(type.componentType()); |
| 122 | default: |
| 123 | if (type.fName == "bool") { |
| 124 | return TypeCategory::kBool; |
| 125 | } else if (type.fName == "int" || type.fName == "short") { |
| 126 | return TypeCategory::kSigned; |
| 127 | } else if (type.fName == "uint" || type.fName == "ushort") { |
| 128 | return TypeCategory::kUnsigned; |
| 129 | } else { |
| 130 | SkASSERT(type.fName == "float" || type.fName == "half"); |
| 131 | return TypeCategory::kFloat; |
| 132 | } |
| 133 | ABORT("unsupported type: %s\n", type.description().c_str()); |
| 134 | } |
| 135 | } |
| 136 | |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame^] | 137 | // A "simple" Swizzle is based on a variable (or a compound variable like a struct or array), and |
| 138 | // that references consecutive values, such that it can be implemented using normal load/store ops |
| 139 | // with an offset. Note that all single-component swizzles (of suitable base types) are simple. |
| 140 | static bool swizzle_is_simple(const Swizzle& s) { |
| 141 | switch (s.fBase->fKind) { |
| 142 | case Expression::kFieldAccess_Kind: |
| 143 | case Expression::kIndex_Kind: |
| 144 | case Expression::kVariableReference_Kind: |
| 145 | break; |
| 146 | default: |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | for (size_t i = 1; i < s.fComponents.size(); ++i) { |
| 151 | if (s.fComponents[i] != s.fComponents[i - 1] + 1) { |
| 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 158 | int ByteCodeGenerator::getLocation(const Variable& var) { |
| 159 | // given that we seldom have more than a couple of variables, linear search is probably the most |
| 160 | // efficient way to handle lookups |
| 161 | switch (var.fStorage) { |
| 162 | case Variable::kLocal_Storage: { |
| 163 | for (int i = fLocals.size() - 1; i >= 0; --i) { |
| 164 | if (fLocals[i] == &var) { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 165 | SkASSERT(fParameterCount + i <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 166 | return fParameterCount + i; |
| 167 | } |
| 168 | } |
| 169 | int result = fParameterCount + fLocals.size(); |
| 170 | fLocals.push_back(&var); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 171 | for (int i = 0; i < SlotCount(var.fType) - 1; ++i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 172 | fLocals.push_back(nullptr); |
| 173 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 174 | SkASSERT(result <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 175 | return result; |
| 176 | } |
| 177 | case Variable::kParameter_Storage: { |
| 178 | int offset = 0; |
| 179 | for (const auto& p : fFunction->fDeclaration.fParameters) { |
| 180 | if (p == &var) { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 181 | SkASSERT(offset <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 182 | return offset; |
| 183 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 184 | offset += SlotCount(p->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 185 | } |
| 186 | SkASSERT(false); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 187 | return 0; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 188 | } |
| 189 | case Variable::kGlobal_Storage: { |
| 190 | int offset = 0; |
| 191 | for (const auto& e : fProgram) { |
| 192 | if (e.fKind == ProgramElement::kVar_Kind) { |
| 193 | VarDeclarations& decl = (VarDeclarations&) e; |
| 194 | for (const auto& v : decl.fVars) { |
| 195 | const Variable* declVar = ((VarDeclaration&) *v).fVar; |
| 196 | if (declVar->fModifiers.fLayout.fBuiltin >= 0) { |
| 197 | continue; |
| 198 | } |
| 199 | if (declVar == &var) { |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 200 | SkASSERT(offset <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 201 | return offset; |
| 202 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 203 | offset += SlotCount(declVar->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | SkASSERT(false); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 208 | return 0; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 209 | } |
| 210 | default: |
| 211 | SkASSERT(false); |
| 212 | return 0; |
| 213 | } |
| 214 | } |
| 215 | |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame^] | 216 | // TODO: Elide Add 0 and Mul 1 sequences |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 217 | int ByteCodeGenerator::getLocation(const Expression& expr, Variable::Storage* storage) { |
| 218 | switch (expr.fKind) { |
| 219 | case Expression::kFieldAccess_Kind: { |
| 220 | const FieldAccess& f = (const FieldAccess&)expr; |
| 221 | int baseAddr = this->getLocation(*f.fBase, storage); |
| 222 | int offset = 0; |
| 223 | for (int i = 0; i < f.fFieldIndex; ++i) { |
| 224 | offset += SlotCount(*f.fBase->fType.fields()[i].fType); |
| 225 | } |
| 226 | if (baseAddr < 0) { |
| 227 | this->write(ByteCodeInstruction::kPushImmediate); |
| 228 | this->write32(offset); |
| 229 | this->write(ByteCodeInstruction::kAddI); |
| 230 | return -1; |
| 231 | } else { |
| 232 | return baseAddr + offset; |
| 233 | } |
| 234 | } |
| 235 | case Expression::kIndex_Kind: { |
| 236 | const IndexExpression& i = (const IndexExpression&)expr; |
| 237 | int stride = SlotCount(i.fType); |
| 238 | int offset = -1; |
| 239 | if (i.fIndex->isConstant()) { |
| 240 | offset = i.fIndex->getConstantInt() * stride; |
| 241 | } else { |
| 242 | this->writeExpression(*i.fIndex); |
| 243 | this->write(ByteCodeInstruction::kPushImmediate); |
| 244 | this->write32(stride); |
| 245 | this->write(ByteCodeInstruction::kMultiplyI); |
| 246 | } |
| 247 | int baseAddr = this->getLocation(*i.fBase, storage); |
| 248 | if (baseAddr >= 0 && offset >= 0) { |
| 249 | return baseAddr + offset; |
| 250 | } |
| 251 | if (baseAddr >= 0) { |
| 252 | this->write(ByteCodeInstruction::kPushImmediate); |
| 253 | this->write32(baseAddr); |
| 254 | } |
| 255 | if (offset >= 0) { |
| 256 | this->write(ByteCodeInstruction::kPushImmediate); |
| 257 | this->write32(offset); |
| 258 | } |
| 259 | this->write(ByteCodeInstruction::kAddI); |
| 260 | return -1; |
| 261 | } |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame^] | 262 | case Expression::kSwizzle_Kind: { |
| 263 | const Swizzle& s = (const Swizzle&)expr; |
| 264 | SkASSERT(swizzle_is_simple(s)); |
| 265 | int baseAddr = this->getLocation(*s.fBase, storage); |
| 266 | int offset = s.fComponents[0]; |
| 267 | if (baseAddr < 0) { |
| 268 | this->write(ByteCodeInstruction::kPushImmediate); |
| 269 | this->write32(offset); |
| 270 | this->write(ByteCodeInstruction::kAddI); |
| 271 | return -1; |
| 272 | } else { |
| 273 | return baseAddr + offset; |
| 274 | } |
| 275 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 276 | case Expression::kVariableReference_Kind: { |
| 277 | const Variable& var = ((const VariableReference&)expr).fVariable; |
| 278 | *storage = var.fStorage; |
| 279 | return this->getLocation(var); |
| 280 | } |
| 281 | default: |
| 282 | SkASSERT(false); |
| 283 | return 0; |
| 284 | } |
| 285 | } |
| 286 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 287 | void ByteCodeGenerator::write8(uint8_t b) { |
| 288 | fCode->push_back(b); |
| 289 | } |
| 290 | |
| 291 | void ByteCodeGenerator::write16(uint16_t i) { |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 292 | size_t n = fCode->size(); |
| 293 | fCode->resize(n+2); |
| 294 | memcpy(fCode->data() + n, &i, 2); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | void ByteCodeGenerator::write32(uint32_t i) { |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 298 | size_t n = fCode->size(); |
| 299 | fCode->resize(n+4); |
| 300 | memcpy(fCode->data() + n, &i, 4); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | void ByteCodeGenerator::write(ByteCodeInstruction i) { |
Mike Klein | 108e935 | 2019-05-21 11:05:17 -0500 | [diff] [blame] | 304 | this->write16((uint16_t)i); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 307 | static ByteCodeInstruction vector_instruction(ByteCodeInstruction base, int count) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 308 | SkASSERT(count >= 1 && count <= 4); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 309 | return ((ByteCodeInstruction) ((int) base + count - 1)); |
| 310 | } |
| 311 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 312 | void ByteCodeGenerator::writeTypedInstruction(const Type& type, ByteCodeInstruction s, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 313 | ByteCodeInstruction u, ByteCodeInstruction f, |
| 314 | int count) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 315 | switch (type_category(type)) { |
| 316 | case TypeCategory::kSigned: |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 317 | this->write(vector_instruction(s, count)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 318 | break; |
| 319 | case TypeCategory::kUnsigned: |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 320 | this->write(vector_instruction(u, count)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 321 | break; |
| 322 | case TypeCategory::kFloat: |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 323 | this->write(vector_instruction(f, count)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 324 | break; |
| 325 | default: |
| 326 | SkASSERT(false); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void ByteCodeGenerator::writeBinaryExpression(const BinaryExpression& b) { |
| 331 | if (b.fOperator == Token::Kind::EQ) { |
| 332 | std::unique_ptr<LValue> lvalue = this->getLValue(*b.fLeft); |
| 333 | this->writeExpression(*b.fRight); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 334 | lvalue->store(); |
| 335 | return; |
| 336 | } |
| 337 | Token::Kind op; |
| 338 | std::unique_ptr<LValue> lvalue; |
| 339 | if (is_assignment(b.fOperator)) { |
| 340 | lvalue = this->getLValue(*b.fLeft); |
| 341 | lvalue->load(); |
| 342 | op = remove_assignment(b.fOperator); |
| 343 | } else { |
| 344 | this->writeExpression(*b.fLeft); |
| 345 | op = b.fOperator; |
| 346 | if (b.fLeft->fType.kind() == Type::kScalar_Kind && |
| 347 | b.fRight->fType.kind() == Type::kVector_Kind) { |
| 348 | for (int i = b.fRight->fType.columns(); i > 1; --i) { |
| 349 | this->write(ByteCodeInstruction::kDup); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | this->writeExpression(*b.fRight); |
| 354 | if (b.fLeft->fType.kind() == Type::kVector_Kind && |
| 355 | b.fRight->fType.kind() == Type::kScalar_Kind) { |
| 356 | for (int i = b.fLeft->fType.columns(); i > 1; --i) { |
| 357 | this->write(ByteCodeInstruction::kDup); |
| 358 | } |
| 359 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 360 | int count = SlotCount(b.fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 361 | switch (op) { |
| 362 | case Token::Kind::EQEQ: |
| 363 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareIEQ, |
| 364 | ByteCodeInstruction::kCompareIEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 365 | ByteCodeInstruction::kCompareFEQ, |
| 366 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 367 | break; |
| 368 | case Token::Kind::GT: |
| 369 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGT, |
| 370 | ByteCodeInstruction::kCompareUGT, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 371 | ByteCodeInstruction::kCompareFGT, |
| 372 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 373 | break; |
| 374 | case Token::Kind::GTEQ: |
| 375 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGTEQ, |
| 376 | ByteCodeInstruction::kCompareUGTEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 377 | ByteCodeInstruction::kCompareFGTEQ, |
| 378 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 379 | break; |
| 380 | case Token::Kind::LT: |
| 381 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLT, |
| 382 | ByteCodeInstruction::kCompareULT, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 383 | ByteCodeInstruction::kCompareFLT, |
| 384 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 385 | break; |
| 386 | case Token::Kind::LTEQ: |
| 387 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLTEQ, |
| 388 | ByteCodeInstruction::kCompareULTEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 389 | ByteCodeInstruction::kCompareFLTEQ, |
| 390 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 391 | break; |
| 392 | case Token::Kind::MINUS: |
| 393 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kSubtractI, |
| 394 | ByteCodeInstruction::kSubtractI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 395 | ByteCodeInstruction::kSubtractF, |
| 396 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 397 | break; |
| 398 | case Token::Kind::NEQ: |
| 399 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareINEQ, |
| 400 | ByteCodeInstruction::kCompareINEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 401 | ByteCodeInstruction::kCompareFNEQ, |
| 402 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 403 | break; |
| 404 | case Token::Kind::PERCENT: |
| 405 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kRemainderS, |
| 406 | ByteCodeInstruction::kRemainderU, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 407 | ByteCodeInstruction::kRemainderF, |
| 408 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 409 | break; |
| 410 | case Token::Kind::PLUS: |
| 411 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kAddI, |
| 412 | ByteCodeInstruction::kAddI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 413 | ByteCodeInstruction::kAddF, |
| 414 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 415 | break; |
| 416 | case Token::Kind::SLASH: |
| 417 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kDivideS, |
| 418 | ByteCodeInstruction::kDivideU, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 419 | ByteCodeInstruction::kDivideF, |
| 420 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 421 | break; |
| 422 | case Token::Kind::STAR: |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 423 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kMultiplyI, |
| 424 | ByteCodeInstruction::kMultiplyI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 425 | ByteCodeInstruction::kMultiplyF, |
| 426 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 427 | break; |
| 428 | default: |
| 429 | SkASSERT(false); |
| 430 | } |
| 431 | if (lvalue) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 432 | lvalue->store(); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
| 437 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | 44d4476 | 2019-05-13 14:19:12 -0400 | [diff] [blame] | 438 | this->write32(b.fValue ? 1 : 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | void ByteCodeGenerator::writeConstructor(const Constructor& c) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 442 | for (const auto& arg : c.fArguments) { |
| 443 | this->writeExpression(*arg); |
| 444 | } |
| 445 | if (c.fArguments.size() == 1) { |
| 446 | TypeCategory inCategory = type_category(c.fArguments[0]->fType); |
| 447 | TypeCategory outCategory = type_category(c.fType); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 448 | int inCount = c.fArguments[0]->fType.columns(); |
| 449 | int outCount = c.fType.columns(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 450 | if (inCategory != outCategory) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 451 | SkASSERT(inCount == outCount); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 452 | if (inCategory == TypeCategory::kFloat) { |
| 453 | SkASSERT(outCategory == TypeCategory::kSigned || |
| 454 | outCategory == TypeCategory::kUnsigned); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 455 | this->write(vector_instruction(ByteCodeInstruction::kConvertFtoI, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 456 | } else if (outCategory == TypeCategory::kFloat) { |
| 457 | if (inCategory == TypeCategory::kSigned) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 458 | this->write(vector_instruction(ByteCodeInstruction::kConvertStoF, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 459 | } else { |
| 460 | SkASSERT(inCategory == TypeCategory::kUnsigned); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 461 | this->write(vector_instruction(ByteCodeInstruction::kConvertUtoF, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 462 | } |
| 463 | } else { |
| 464 | SkASSERT(false); |
| 465 | } |
| 466 | } |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 467 | if (inCount != outCount) { |
| 468 | SkASSERT(inCount == 1); |
| 469 | for (; inCount != outCount; ++inCount) { |
| 470 | this->write(ByteCodeInstruction::kDup); |
| 471 | } |
| 472 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 476 | void ByteCodeGenerator::writeExternalFunctionCall(const ExternalFunctionCall& f) { |
| 477 | int argumentCount = 0; |
| 478 | for (const auto& arg : f.fArguments) { |
| 479 | this->writeExpression(*arg); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 480 | argumentCount += SlotCount(arg->fType); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 481 | } |
| 482 | this->write(ByteCodeInstruction::kCallExternal); |
| 483 | SkASSERT(argumentCount <= 255); |
| 484 | this->write8(argumentCount); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 485 | this->write8(SlotCount(f.fType)); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 486 | int index = fOutput->fExternalValues.size(); |
| 487 | fOutput->fExternalValues.push_back(f.fFunction); |
| 488 | SkASSERT(index <= 255); |
| 489 | this->write8(index); |
| 490 | } |
| 491 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 492 | void ByteCodeGenerator::writeExternalValue(const ExternalValueReference& e) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 493 | this->write(vector_instruction(ByteCodeInstruction::kReadExternal, |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 494 | SlotCount(e.fValue->type()))); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 495 | int index = fOutput->fExternalValues.size(); |
| 496 | fOutput->fExternalValues.push_back(e.fValue); |
| 497 | SkASSERT(index <= 255); |
| 498 | this->write8(index); |
| 499 | } |
| 500 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 501 | void ByteCodeGenerator::writeVariableExpression(const Expression& expr) { |
| 502 | Variable::Storage storage; |
| 503 | int location = this->getLocation(expr, &storage); |
| 504 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 505 | int count = SlotCount(expr.fType); |
| 506 | if (location < 0 || count > 4) { |
| 507 | if (location >= 0) { |
| 508 | this->write(ByteCodeInstruction::kPushImmediate); |
| 509 | this->write32(location); |
| 510 | } |
| 511 | this->write(isGlobal ? ByteCodeInstruction::kLoadExtendedGlobal |
| 512 | : ByteCodeInstruction::kLoadExtended); |
| 513 | this->write8(count); |
| 514 | } else { |
| 515 | this->write(vector_instruction(isGlobal ? ByteCodeInstruction::kLoadGlobal |
| 516 | : ByteCodeInstruction::kLoad, |
| 517 | count)); |
| 518 | this->write8(location); |
| 519 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | void ByteCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
| 523 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 524 | this->write32(Interpreter::Value((float) f.fValue).fUnsigned); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 525 | } |
| 526 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 527 | void ByteCodeGenerator::writeIntrinsicCall(const FunctionCall& c) { |
| 528 | auto found = fIntrinsics.find(c.fFunction.fName); |
| 529 | if (found == fIntrinsics.end()) { |
| 530 | fErrors.error(c.fOffset, "unsupported intrinsic function"); |
| 531 | return; |
| 532 | } |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 533 | if (found->second.fIsSpecial) { |
| 534 | SkASSERT(found->second.fValue.fSpecial == SpecialIntrinsic::kDot); |
| 535 | SkASSERT(c.fArguments.size() == 2); |
| 536 | SkASSERT(SlotCount(c.fArguments[0]->fType) == SlotCount(c.fArguments[1]->fType)); |
| 537 | this->write((ByteCodeInstruction) ((int) ByteCodeInstruction::kMultiplyF + |
| 538 | SlotCount(c.fArguments[0]->fType) - 1)); |
| 539 | for (int i = SlotCount(c.fArguments[0]->fType); i > 1; --i) { |
| 540 | this->write(ByteCodeInstruction::kAddF); |
| 541 | } |
| 542 | } else { |
| 543 | switch (found->second.fValue.fInstruction) { |
| 544 | case ByteCodeInstruction::kCos: |
| 545 | case ByteCodeInstruction::kMix: |
| 546 | case ByteCodeInstruction::kSin: |
| 547 | case ByteCodeInstruction::kSqrt: |
| 548 | case ByteCodeInstruction::kTan: |
| 549 | SkASSERT(c.fArguments.size() > 0); |
| 550 | this->write((ByteCodeInstruction) ((int) found->second.fValue.fInstruction + |
| 551 | SlotCount(c.fArguments[0]->fType) - 1)); |
| 552 | break; |
| 553 | case ByteCodeInstruction::kCross: |
| 554 | this->write(found->second.fValue.fInstruction); |
| 555 | break; |
| 556 | default: |
| 557 | SkASSERT(false); |
| 558 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 559 | } |
| 560 | } |
| 561 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 562 | void ByteCodeGenerator::writeFunctionCall(const FunctionCall& f) { |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 563 | for (const auto& arg : f.fArguments) { |
| 564 | this->writeExpression(*arg); |
| 565 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 566 | if (f.fFunction.fBuiltin) { |
| 567 | this->writeIntrinsicCall(f); |
| 568 | return; |
| 569 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 570 | this->write(ByteCodeInstruction::kCall); |
| 571 | fCallTargets.emplace_back(this, f.fFunction); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 572 | } |
| 573 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 574 | void ByteCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
| 575 | this->write(ByteCodeInstruction::kPushImmediate); |
| 576 | this->write32(i.fValue); |
| 577 | } |
| 578 | |
| 579 | void ByteCodeGenerator::writeNullLiteral(const NullLiteral& n) { |
| 580 | // not yet implemented |
| 581 | abort(); |
| 582 | } |
| 583 | |
| 584 | void ByteCodeGenerator::writePrefixExpression(const PrefixExpression& p) { |
| 585 | switch (p.fOperator) { |
| 586 | case Token::Kind::PLUSPLUS: // fall through |
| 587 | case Token::Kind::MINUSMINUS: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 588 | SkASSERT(SlotCount(p.fOperand->fType) == 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 589 | std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand); |
| 590 | lvalue->load(); |
| 591 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 592 | this->write32(type_category(p.fType) == TypeCategory::kFloat |
| 593 | ? Interpreter::Value(1.0f).fUnsigned : 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 594 | if (p.fOperator == Token::Kind::PLUSPLUS) { |
| 595 | this->writeTypedInstruction(p.fType, |
| 596 | ByteCodeInstruction::kAddI, |
| 597 | ByteCodeInstruction::kAddI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 598 | ByteCodeInstruction::kAddF, |
| 599 | 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 600 | } else { |
| 601 | this->writeTypedInstruction(p.fType, |
| 602 | ByteCodeInstruction::kSubtractI, |
| 603 | ByteCodeInstruction::kSubtractI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 604 | ByteCodeInstruction::kSubtractF, |
| 605 | 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 606 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 607 | lvalue->store(); |
| 608 | break; |
| 609 | } |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 610 | case Token::Kind::MINUS: { |
| 611 | this->writeExpression(*p.fOperand); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 612 | this->writeTypedInstruction(p.fType, |
Mike Klein | 1271091 | 2019-05-21 11:04:59 -0500 | [diff] [blame] | 613 | ByteCodeInstruction::kNegateI, |
| 614 | ByteCodeInstruction::kNegateI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 615 | ByteCodeInstruction::kNegateF, |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 616 | SlotCount(p.fOperand->fType)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 617 | break; |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 618 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 619 | default: |
| 620 | SkASSERT(false); |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | void ByteCodeGenerator::writePostfixExpression(const PostfixExpression& p) { |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 625 | switch (p.fOperator) { |
| 626 | case Token::Kind::PLUSPLUS: // fall through |
| 627 | case Token::Kind::MINUSMINUS: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 628 | SkASSERT(SlotCount(p.fOperand->fType) == 1); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 629 | std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand); |
| 630 | lvalue->load(); |
| 631 | this->write(ByteCodeInstruction::kDup); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 632 | this->write(ByteCodeInstruction::kPushImmediate); |
| 633 | this->write32(type_category(p.fType) == TypeCategory::kFloat |
| 634 | ? Interpreter::Value(1.0f).fUnsigned : 1); |
| 635 | if (p.fOperator == Token::Kind::PLUSPLUS) { |
| 636 | this->writeTypedInstruction(p.fType, |
| 637 | ByteCodeInstruction::kAddI, |
| 638 | ByteCodeInstruction::kAddI, |
| 639 | ByteCodeInstruction::kAddF, |
| 640 | 1); |
| 641 | } else { |
| 642 | this->writeTypedInstruction(p.fType, |
| 643 | ByteCodeInstruction::kSubtractI, |
| 644 | ByteCodeInstruction::kSubtractI, |
| 645 | ByteCodeInstruction::kSubtractF, |
| 646 | 1); |
| 647 | } |
| 648 | lvalue->store(); |
| 649 | this->write(ByteCodeInstruction::kPop); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | default: |
| 653 | SkASSERT(false); |
| 654 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void ByteCodeGenerator::writeSwizzle(const Swizzle& s) { |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame^] | 658 | if (swizzle_is_simple(s)) { |
| 659 | this->writeVariableExpression(s); |
| 660 | return; |
| 661 | } |
| 662 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 663 | switch (s.fBase->fKind) { |
| 664 | case Expression::kVariableReference_Kind: { |
| 665 | const Variable& var = ((VariableReference&) *s.fBase).fVariable; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 666 | this->write(var.fStorage == Variable::kGlobal_Storage |
| 667 | ? ByteCodeInstruction::kLoadSwizzleGlobal |
| 668 | : ByteCodeInstruction::kLoadSwizzle); |
| 669 | this->write8(this->getLocation(var)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 670 | this->write8(s.fComponents.size()); |
| 671 | for (int c : s.fComponents) { |
| 672 | this->write8(c); |
| 673 | } |
| 674 | break; |
| 675 | } |
| 676 | default: |
| 677 | this->writeExpression(*s.fBase); |
| 678 | this->write(ByteCodeInstruction::kSwizzle); |
| 679 | this->write8(s.fBase->fType.columns()); |
| 680 | this->write8(s.fComponents.size()); |
| 681 | for (int c : s.fComponents) { |
| 682 | this->write8(c); |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 687 | void ByteCodeGenerator::writeTernaryExpression(const TernaryExpression& t) { |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 688 | this->writeExpression(*t.fTest); |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 689 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 690 | DeferredLocation trueLocation(this); |
| 691 | this->writeExpression(*t.fIfFalse); |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 692 | this->write(ByteCodeInstruction::kBranch); |
| 693 | DeferredLocation endLocation(this); |
| 694 | trueLocation.set(); |
| 695 | this->writeExpression(*t.fIfTrue); |
| 696 | endLocation.set(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 697 | } |
| 698 | |
| 699 | void ByteCodeGenerator::writeExpression(const Expression& e) { |
| 700 | switch (e.fKind) { |
| 701 | case Expression::kBinary_Kind: |
| 702 | this->writeBinaryExpression((BinaryExpression&) e); |
| 703 | break; |
| 704 | case Expression::kBoolLiteral_Kind: |
| 705 | this->writeBoolLiteral((BoolLiteral&) e); |
| 706 | break; |
| 707 | case Expression::kConstructor_Kind: |
| 708 | this->writeConstructor((Constructor&) e); |
| 709 | break; |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 710 | case Expression::kExternalFunctionCall_Kind: |
| 711 | this->writeExternalFunctionCall((ExternalFunctionCall&) e); |
| 712 | break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 713 | case Expression::kExternalValue_Kind: |
| 714 | this->writeExternalValue((ExternalValueReference&) e); |
| 715 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 716 | case Expression::kFieldAccess_Kind: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 717 | case Expression::kIndex_Kind: |
| 718 | case Expression::kVariableReference_Kind: |
| 719 | this->writeVariableExpression(e); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 720 | break; |
| 721 | case Expression::kFloatLiteral_Kind: |
| 722 | this->writeFloatLiteral((FloatLiteral&) e); |
| 723 | break; |
| 724 | case Expression::kFunctionCall_Kind: |
| 725 | this->writeFunctionCall((FunctionCall&) e); |
| 726 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 727 | case Expression::kIntLiteral_Kind: |
| 728 | this->writeIntLiteral((IntLiteral&) e); |
| 729 | break; |
| 730 | case Expression::kNullLiteral_Kind: |
| 731 | this->writeNullLiteral((NullLiteral&) e); |
| 732 | break; |
| 733 | case Expression::kPrefix_Kind: |
| 734 | this->writePrefixExpression((PrefixExpression&) e); |
| 735 | break; |
| 736 | case Expression::kPostfix_Kind: |
| 737 | this->writePostfixExpression((PostfixExpression&) e); |
| 738 | break; |
| 739 | case Expression::kSwizzle_Kind: |
| 740 | this->writeSwizzle((Swizzle&) e); |
| 741 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 742 | case Expression::kTernary_Kind: |
| 743 | this->writeTernaryExpression((TernaryExpression&) e); |
| 744 | break; |
| 745 | default: |
| 746 | printf("unsupported expression %s\n", e.description().c_str()); |
| 747 | SkASSERT(false); |
| 748 | } |
| 749 | } |
| 750 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 751 | class ByteCodeExternalValueLValue : public ByteCodeGenerator::LValue { |
| 752 | public: |
| 753 | ByteCodeExternalValueLValue(ByteCodeGenerator* generator, ExternalValue& value, int index) |
| 754 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 755 | , fCount(ByteCodeGenerator::SlotCount(value.type())) |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 756 | , fIndex(index) {} |
| 757 | |
| 758 | void load() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 759 | fGenerator.write(vector_instruction(ByteCodeInstruction::kReadExternal, fCount)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 760 | fGenerator.write8(fIndex); |
| 761 | } |
| 762 | |
| 763 | void store() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 764 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, fCount)); |
| 765 | fGenerator.write(vector_instruction(ByteCodeInstruction::kWriteExternal, fCount)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 766 | fGenerator.write8(fIndex); |
| 767 | } |
| 768 | |
| 769 | private: |
| 770 | typedef LValue INHERITED; |
| 771 | |
| 772 | int fCount; |
| 773 | |
| 774 | int fIndex; |
| 775 | }; |
| 776 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 777 | class ByteCodeSwizzleLValue : public ByteCodeGenerator::LValue { |
| 778 | public: |
| 779 | ByteCodeSwizzleLValue(ByteCodeGenerator* generator, const Swizzle& swizzle) |
| 780 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 781 | , fSwizzle(swizzle) {} |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 782 | |
| 783 | void load() override { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 784 | fGenerator.writeSwizzle(fSwizzle); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | void store() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 788 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, |
| 789 | fSwizzle.fComponents.size())); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 790 | Variable::Storage storage; |
| 791 | int location = fGenerator.getLocation(*fSwizzle.fBase, &storage); |
| 792 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 793 | if (location < 0) { |
| 794 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleIndirectGlobal |
| 795 | : ByteCodeInstruction::kStoreSwizzleIndirect); |
| 796 | } else { |
| 797 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleGlobal |
| 798 | : ByteCodeInstruction::kStoreSwizzle); |
| 799 | fGenerator.write8(location); |
| 800 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 801 | fGenerator.write8(fSwizzle.fComponents.size()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 802 | for (int c : fSwizzle.fComponents) { |
| 803 | fGenerator.write8(c); |
| 804 | } |
| 805 | } |
| 806 | |
| 807 | private: |
| 808 | const Swizzle& fSwizzle; |
| 809 | |
| 810 | typedef LValue INHERITED; |
| 811 | }; |
| 812 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 813 | class ByteCodeExpressionLValue : public ByteCodeGenerator::LValue { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 814 | public: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 815 | ByteCodeExpressionLValue(ByteCodeGenerator* generator, const Expression& expr) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 816 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 817 | , fExpression(expr) {} |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 818 | |
| 819 | void load() override { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 820 | fGenerator.writeVariableExpression(fExpression); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | void store() override { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 824 | int count = ByteCodeGenerator::SlotCount(fExpression.fType); |
| 825 | if (count > 4) { |
| 826 | fGenerator.write(ByteCodeInstruction::kDupN); |
| 827 | fGenerator.write8(count); |
| 828 | } else { |
| 829 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count)); |
| 830 | } |
| 831 | Variable::Storage storage; |
| 832 | int location = fGenerator.getLocation(fExpression, &storage); |
| 833 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 834 | if (location < 0 || count > 4) { |
| 835 | if (location >= 0) { |
| 836 | fGenerator.write(ByteCodeInstruction::kPushImmediate); |
| 837 | fGenerator.write32(location); |
| 838 | } |
| 839 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreExtendedGlobal |
| 840 | : ByteCodeInstruction::kStoreExtended); |
| 841 | fGenerator.write8(count); |
| 842 | } else { |
| 843 | fGenerator.write(vector_instruction(isGlobal ? ByteCodeInstruction::kStoreGlobal |
| 844 | : ByteCodeInstruction::kStore, |
| 845 | count)); |
| 846 | fGenerator.write8(location); |
| 847 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | private: |
| 851 | typedef LValue INHERITED; |
| 852 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 853 | const Expression& fExpression; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 854 | }; |
| 855 | |
| 856 | std::unique_ptr<ByteCodeGenerator::LValue> ByteCodeGenerator::getLValue(const Expression& e) { |
| 857 | switch (e.fKind) { |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 858 | case Expression::kExternalValue_Kind: { |
| 859 | ExternalValue* value = ((ExternalValueReference&) e).fValue; |
| 860 | int index = fOutput->fExternalValues.size(); |
| 861 | fOutput->fExternalValues.push_back(value); |
| 862 | SkASSERT(index <= 255); |
| 863 | return std::unique_ptr<LValue>(new ByteCodeExternalValueLValue(this, *value, index)); |
| 864 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 865 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 866 | case Expression::kIndex_Kind: |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 867 | case Expression::kVariableReference_Kind: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 868 | return std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e)); |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame^] | 869 | case Expression::kSwizzle_Kind: { |
| 870 | const Swizzle& s = (const Swizzle&) e; |
| 871 | return swizzle_is_simple(s) |
| 872 | ? std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e)) |
| 873 | : std::unique_ptr<LValue>(new ByteCodeSwizzleLValue(this, s)); |
| 874 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 875 | case Expression::kTernary_Kind: |
| 876 | default: |
| 877 | printf("unsupported lvalue %s\n", e.description().c_str()); |
| 878 | return nullptr; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | void ByteCodeGenerator::writeBlock(const Block& b) { |
| 883 | for (const auto& s : b.fStatements) { |
| 884 | this->writeStatement(*s); |
| 885 | } |
| 886 | } |
| 887 | |
| 888 | void ByteCodeGenerator::setBreakTargets() { |
| 889 | std::vector<DeferredLocation>& breaks = fBreakTargets.top(); |
| 890 | for (DeferredLocation& b : breaks) { |
| 891 | b.set(); |
| 892 | } |
| 893 | fBreakTargets.pop(); |
| 894 | } |
| 895 | |
| 896 | void ByteCodeGenerator::setContinueTargets() { |
| 897 | std::vector<DeferredLocation>& continues = fContinueTargets.top(); |
| 898 | for (DeferredLocation& c : continues) { |
| 899 | c.set(); |
| 900 | } |
| 901 | fContinueTargets.pop(); |
| 902 | } |
| 903 | |
| 904 | void ByteCodeGenerator::writeBreakStatement(const BreakStatement& b) { |
| 905 | this->write(ByteCodeInstruction::kBranch); |
| 906 | fBreakTargets.top().emplace_back(this); |
| 907 | } |
| 908 | |
| 909 | void ByteCodeGenerator::writeContinueStatement(const ContinueStatement& c) { |
| 910 | this->write(ByteCodeInstruction::kBranch); |
| 911 | fContinueTargets.top().emplace_back(this); |
| 912 | } |
| 913 | |
| 914 | void ByteCodeGenerator::writeDoStatement(const DoStatement& d) { |
| 915 | fContinueTargets.emplace(); |
| 916 | fBreakTargets.emplace(); |
| 917 | size_t start = fCode->size(); |
| 918 | this->writeStatement(*d.fStatement); |
| 919 | this->setContinueTargets(); |
| 920 | this->writeExpression(*d.fTest); |
| 921 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 922 | this->write16(start); |
| 923 | this->setBreakTargets(); |
| 924 | } |
| 925 | |
| 926 | void ByteCodeGenerator::writeForStatement(const ForStatement& f) { |
| 927 | fContinueTargets.emplace(); |
| 928 | fBreakTargets.emplace(); |
| 929 | if (f.fInitializer) { |
| 930 | this->writeStatement(*f.fInitializer); |
| 931 | } |
| 932 | size_t start = fCode->size(); |
| 933 | if (f.fTest) { |
| 934 | this->writeExpression(*f.fTest); |
| 935 | this->write(ByteCodeInstruction::kNot); |
| 936 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 937 | DeferredLocation endLocation(this); |
| 938 | this->writeStatement(*f.fStatement); |
| 939 | this->setContinueTargets(); |
| 940 | if (f.fNext) { |
| 941 | this->writeExpression(*f.fNext); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 942 | this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType))); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 943 | } |
| 944 | this->write(ByteCodeInstruction::kBranch); |
| 945 | this->write16(start); |
| 946 | endLocation.set(); |
| 947 | } else { |
| 948 | this->writeStatement(*f.fStatement); |
| 949 | this->setContinueTargets(); |
| 950 | if (f.fNext) { |
| 951 | this->writeExpression(*f.fNext); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 952 | this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType))); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 953 | } |
| 954 | this->write(ByteCodeInstruction::kBranch); |
| 955 | this->write16(start); |
| 956 | } |
| 957 | this->setBreakTargets(); |
| 958 | } |
| 959 | |
| 960 | void ByteCodeGenerator::writeIfStatement(const IfStatement& i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 961 | if (i.fIfFalse) { |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 962 | // if (test) { ..ifTrue.. } else { .. ifFalse .. } |
| 963 | this->writeExpression(*i.fTest); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 964 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 965 | DeferredLocation trueLocation(this); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 966 | this->writeStatement(*i.fIfFalse); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 967 | this->write(ByteCodeInstruction::kBranch); |
| 968 | DeferredLocation endLocation(this); |
| 969 | trueLocation.set(); |
| 970 | this->writeStatement(*i.fIfTrue); |
| 971 | endLocation.set(); |
| 972 | } else { |
| 973 | // if (test) { ..ifTrue.. } |
| 974 | this->writeExpression(*i.fTest); |
| 975 | this->write(ByteCodeInstruction::kNot); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 976 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 977 | DeferredLocation endLocation(this); |
| 978 | this->writeStatement(*i.fIfTrue); |
| 979 | endLocation.set(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 980 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 981 | } |
| 982 | |
| 983 | void ByteCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 984 | this->writeExpression(*r.fExpression); |
| 985 | this->write(ByteCodeInstruction::kReturn); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 986 | this->write8(SlotCount(r.fExpression->fType)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | void ByteCodeGenerator::writeSwitchStatement(const SwitchStatement& r) { |
| 990 | // not yet implemented |
| 991 | abort(); |
| 992 | } |
| 993 | |
| 994 | void ByteCodeGenerator::writeVarDeclarations(const VarDeclarations& v) { |
| 995 | for (const auto& declStatement : v.fVars) { |
| 996 | const VarDeclaration& decl = (VarDeclaration&) *declStatement; |
| 997 | // we need to grab the location even if we don't use it, to ensure it |
| 998 | // has been allocated |
| 999 | int location = getLocation(*decl.fVar); |
| 1000 | if (decl.fValue) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1001 | this->writeExpression(*decl.fValue); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1002 | int count = SlotCount(decl.fValue->fType); |
| 1003 | if (count > 4) { |
| 1004 | this->write(ByteCodeInstruction::kPushImmediate); |
| 1005 | this->write32(location); |
| 1006 | this->write(ByteCodeInstruction::kStoreExtended); |
| 1007 | this->write8(count); |
| 1008 | } else { |
| 1009 | this->write(vector_instruction(ByteCodeInstruction::kStore, count)); |
| 1010 | this->write8(location); |
| 1011 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | void ByteCodeGenerator::writeWhileStatement(const WhileStatement& w) { |
| 1017 | fContinueTargets.emplace(); |
| 1018 | fBreakTargets.emplace(); |
| 1019 | size_t start = fCode->size(); |
| 1020 | this->writeExpression(*w.fTest); |
| 1021 | this->write(ByteCodeInstruction::kNot); |
| 1022 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 1023 | DeferredLocation endLocation(this); |
| 1024 | this->writeStatement(*w.fStatement); |
| 1025 | this->setContinueTargets(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1026 | this->write(ByteCodeInstruction::kBranch); |
| 1027 | this->write16(start); |
| 1028 | endLocation.set(); |
| 1029 | this->setBreakTargets(); |
| 1030 | } |
| 1031 | |
| 1032 | void ByteCodeGenerator::writeStatement(const Statement& s) { |
| 1033 | switch (s.fKind) { |
| 1034 | case Statement::kBlock_Kind: |
| 1035 | this->writeBlock((Block&) s); |
| 1036 | break; |
| 1037 | case Statement::kBreak_Kind: |
| 1038 | this->writeBreakStatement((BreakStatement&) s); |
| 1039 | break; |
| 1040 | case Statement::kContinue_Kind: |
| 1041 | this->writeContinueStatement((ContinueStatement&) s); |
| 1042 | break; |
| 1043 | case Statement::kDiscard_Kind: |
| 1044 | // not yet implemented |
| 1045 | abort(); |
| 1046 | case Statement::kDo_Kind: |
| 1047 | this->writeDoStatement((DoStatement&) s); |
| 1048 | break; |
| 1049 | case Statement::kExpression_Kind: { |
| 1050 | const Expression& expr = *((ExpressionStatement&) s).fExpression; |
| 1051 | this->writeExpression(expr); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1052 | int count = SlotCount(expr.fType); |
| 1053 | if (count > 4) { |
| 1054 | this->write(ByteCodeInstruction::kPopN); |
| 1055 | this->write8(count); |
| 1056 | } else { |
| 1057 | this->write(vector_instruction(ByteCodeInstruction::kPop, count)); |
| 1058 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1059 | break; |
| 1060 | } |
| 1061 | case Statement::kFor_Kind: |
| 1062 | this->writeForStatement((ForStatement&) s); |
| 1063 | break; |
| 1064 | case Statement::kIf_Kind: |
| 1065 | this->writeIfStatement((IfStatement&) s); |
| 1066 | break; |
| 1067 | case Statement::kNop_Kind: |
| 1068 | break; |
| 1069 | case Statement::kReturn_Kind: |
| 1070 | this->writeReturnStatement((ReturnStatement&) s); |
| 1071 | break; |
| 1072 | case Statement::kSwitch_Kind: |
| 1073 | this->writeSwitchStatement((SwitchStatement&) s); |
| 1074 | break; |
| 1075 | case Statement::kVarDeclarations_Kind: |
| 1076 | this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration); |
| 1077 | break; |
| 1078 | case Statement::kWhile_Kind: |
| 1079 | this->writeWhileStatement((WhileStatement&) s); |
| 1080 | break; |
| 1081 | default: |
| 1082 | SkASSERT(false); |
| 1083 | } |
| 1084 | } |
| 1085 | |
| 1086 | } |