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; |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame^] | 322 | case TypeCategory::kFloat: { |
| 323 | if (count > 4) { |
| 324 | this->write((ByteCodeInstruction)((int)f + 4)); |
| 325 | this->write8(count); |
| 326 | } else { |
| 327 | this->write(vector_instruction(f, count)); |
| 328 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 329 | break; |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame^] | 330 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 331 | default: |
| 332 | SkASSERT(false); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | void ByteCodeGenerator::writeBinaryExpression(const BinaryExpression& b) { |
| 337 | if (b.fOperator == Token::Kind::EQ) { |
| 338 | std::unique_ptr<LValue> lvalue = this->getLValue(*b.fLeft); |
| 339 | this->writeExpression(*b.fRight); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 340 | lvalue->store(); |
| 341 | return; |
| 342 | } |
Brian Osman | 16e6fd5 | 2019-05-29 11:19:00 -0400 | [diff] [blame] | 343 | const Type& lType = b.fLeft->fType; |
| 344 | const Type& rType = b.fRight->fType; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 345 | Token::Kind op; |
| 346 | std::unique_ptr<LValue> lvalue; |
| 347 | if (is_assignment(b.fOperator)) { |
| 348 | lvalue = this->getLValue(*b.fLeft); |
| 349 | lvalue->load(); |
| 350 | op = remove_assignment(b.fOperator); |
| 351 | } else { |
| 352 | this->writeExpression(*b.fLeft); |
| 353 | op = b.fOperator; |
Brian Osman | 16e6fd5 | 2019-05-29 11:19:00 -0400 | [diff] [blame] | 354 | if (lType.kind() == Type::kScalar_Kind && |
| 355 | (rType.kind() == Type::kVector_Kind || rType.kind() == Type::kMatrix_Kind)) { |
| 356 | for (int i = SlotCount(rType); i > 1; --i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 357 | this->write(ByteCodeInstruction::kDup); |
| 358 | } |
| 359 | } |
| 360 | } |
| 361 | this->writeExpression(*b.fRight); |
Brian Osman | 16e6fd5 | 2019-05-29 11:19:00 -0400 | [diff] [blame] | 362 | if ((lType.kind() == Type::kVector_Kind || lType.kind() == Type::kMatrix_Kind) && |
| 363 | rType.kind() == Type::kScalar_Kind) { |
| 364 | for (int i = SlotCount(lType); i > 1; --i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 365 | this->write(ByteCodeInstruction::kDup); |
| 366 | } |
| 367 | } |
Brian Osman | 7df8fff | 2019-05-29 14:33:57 -0400 | [diff] [blame] | 368 | int count = std::max(SlotCount(lType), SlotCount(rType)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 369 | switch (op) { |
| 370 | case Token::Kind::EQEQ: |
| 371 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareIEQ, |
| 372 | ByteCodeInstruction::kCompareIEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 373 | ByteCodeInstruction::kCompareFEQ, |
| 374 | count); |
Brian Osman | 16e6fd5 | 2019-05-29 11:19:00 -0400 | [diff] [blame] | 375 | // Collapse to a single bool |
| 376 | for (int i = count; i > 1; --i) { |
| 377 | this->write(ByteCodeInstruction::kAndB); |
| 378 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 379 | break; |
| 380 | case Token::Kind::GT: |
| 381 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGT, |
| 382 | ByteCodeInstruction::kCompareUGT, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 383 | ByteCodeInstruction::kCompareFGT, |
| 384 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 385 | break; |
| 386 | case Token::Kind::GTEQ: |
| 387 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSGTEQ, |
| 388 | ByteCodeInstruction::kCompareUGTEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 389 | ByteCodeInstruction::kCompareFGTEQ, |
| 390 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 391 | break; |
| 392 | case Token::Kind::LT: |
| 393 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLT, |
| 394 | ByteCodeInstruction::kCompareULT, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 395 | ByteCodeInstruction::kCompareFLT, |
| 396 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 397 | break; |
| 398 | case Token::Kind::LTEQ: |
| 399 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareSLTEQ, |
| 400 | ByteCodeInstruction::kCompareULTEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 401 | ByteCodeInstruction::kCompareFLTEQ, |
| 402 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 403 | break; |
| 404 | case Token::Kind::MINUS: |
| 405 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kSubtractI, |
| 406 | ByteCodeInstruction::kSubtractI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 407 | ByteCodeInstruction::kSubtractF, |
| 408 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 409 | break; |
| 410 | case Token::Kind::NEQ: |
| 411 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kCompareINEQ, |
| 412 | ByteCodeInstruction::kCompareINEQ, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 413 | ByteCodeInstruction::kCompareFNEQ, |
| 414 | count); |
Brian Osman | 16e6fd5 | 2019-05-29 11:19:00 -0400 | [diff] [blame] | 415 | // Collapse to a single bool |
| 416 | for (int i = count; i > 1; --i) { |
| 417 | this->write(ByteCodeInstruction::kOrB); |
| 418 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 419 | break; |
| 420 | case Token::Kind::PERCENT: |
| 421 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kRemainderS, |
| 422 | ByteCodeInstruction::kRemainderU, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 423 | ByteCodeInstruction::kRemainderF, |
| 424 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 425 | break; |
| 426 | case Token::Kind::PLUS: |
| 427 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kAddI, |
| 428 | ByteCodeInstruction::kAddI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 429 | ByteCodeInstruction::kAddF, |
| 430 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 431 | break; |
| 432 | case Token::Kind::SLASH: |
| 433 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kDivideS, |
| 434 | ByteCodeInstruction::kDivideU, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 435 | ByteCodeInstruction::kDivideF, |
| 436 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 437 | break; |
| 438 | case Token::Kind::STAR: |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 439 | this->writeTypedInstruction(b.fLeft->fType, ByteCodeInstruction::kMultiplyI, |
| 440 | ByteCodeInstruction::kMultiplyI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 441 | ByteCodeInstruction::kMultiplyF, |
| 442 | count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 443 | break; |
| 444 | default: |
| 445 | SkASSERT(false); |
| 446 | } |
| 447 | if (lvalue) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 448 | lvalue->store(); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
| 453 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | 44d4476 | 2019-05-13 14:19:12 -0400 | [diff] [blame] | 454 | this->write32(b.fValue ? 1 : 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | void ByteCodeGenerator::writeConstructor(const Constructor& c) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 458 | for (const auto& arg : c.fArguments) { |
| 459 | this->writeExpression(*arg); |
| 460 | } |
| 461 | if (c.fArguments.size() == 1) { |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 462 | const Type& inType = c.fArguments[0]->fType; |
| 463 | const Type& outType = c.fType; |
| 464 | TypeCategory inCategory = type_category(inType); |
| 465 | TypeCategory outCategory = type_category(outType); |
| 466 | int inCount = SlotCount(inType); |
| 467 | int outCount = SlotCount(outType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 468 | if (inCategory != outCategory) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 469 | SkASSERT(inCount == outCount); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 470 | if (inCategory == TypeCategory::kFloat) { |
| 471 | SkASSERT(outCategory == TypeCategory::kSigned || |
| 472 | outCategory == TypeCategory::kUnsigned); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 473 | this->write(vector_instruction(ByteCodeInstruction::kConvertFtoI, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 474 | } else if (outCategory == TypeCategory::kFloat) { |
| 475 | if (inCategory == TypeCategory::kSigned) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 476 | this->write(vector_instruction(ByteCodeInstruction::kConvertStoF, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 477 | } else { |
| 478 | SkASSERT(inCategory == TypeCategory::kUnsigned); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 479 | this->write(vector_instruction(ByteCodeInstruction::kConvertUtoF, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 480 | } |
| 481 | } else { |
| 482 | SkASSERT(false); |
| 483 | } |
| 484 | } |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 485 | if (inType.kind() == Type::kMatrix_Kind && outType.kind() == Type::kMatrix_Kind) { |
| 486 | this->write(ByteCodeInstruction::kMatrixToMatrix); |
| 487 | this->write8(inType.columns()); |
| 488 | this->write8(inType.rows()); |
| 489 | this->write8(outType.columns()); |
| 490 | this->write8(outType.rows()); |
| 491 | } else if (inCount != outCount) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 492 | SkASSERT(inCount == 1); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 493 | if (outType.kind() == Type::kMatrix_Kind) { |
| 494 | this->write(ByteCodeInstruction::kScalarToMatrix); |
| 495 | this->write8(outType.columns()); |
| 496 | this->write8(outType.rows()); |
| 497 | } else { |
| 498 | SkASSERT(outType.kind() == Type::kVector_Kind); |
| 499 | for (; inCount != outCount; ++inCount) { |
| 500 | this->write(ByteCodeInstruction::kDup); |
| 501 | } |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 504 | } |
| 505 | } |
| 506 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 507 | void ByteCodeGenerator::writeExternalFunctionCall(const ExternalFunctionCall& f) { |
| 508 | int argumentCount = 0; |
| 509 | for (const auto& arg : f.fArguments) { |
| 510 | this->writeExpression(*arg); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 511 | argumentCount += SlotCount(arg->fType); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 512 | } |
| 513 | this->write(ByteCodeInstruction::kCallExternal); |
| 514 | SkASSERT(argumentCount <= 255); |
| 515 | this->write8(argumentCount); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 516 | this->write8(SlotCount(f.fType)); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 517 | int index = fOutput->fExternalValues.size(); |
| 518 | fOutput->fExternalValues.push_back(f.fFunction); |
| 519 | SkASSERT(index <= 255); |
| 520 | this->write8(index); |
| 521 | } |
| 522 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 523 | void ByteCodeGenerator::writeExternalValue(const ExternalValueReference& e) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 524 | this->write(vector_instruction(ByteCodeInstruction::kReadExternal, |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 525 | SlotCount(e.fValue->type()))); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 526 | int index = fOutput->fExternalValues.size(); |
| 527 | fOutput->fExternalValues.push_back(e.fValue); |
| 528 | SkASSERT(index <= 255); |
| 529 | this->write8(index); |
| 530 | } |
| 531 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 532 | void ByteCodeGenerator::writeVariableExpression(const Expression& expr) { |
| 533 | Variable::Storage storage; |
| 534 | int location = this->getLocation(expr, &storage); |
| 535 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 536 | int count = SlotCount(expr.fType); |
| 537 | if (location < 0 || count > 4) { |
| 538 | if (location >= 0) { |
| 539 | this->write(ByteCodeInstruction::kPushImmediate); |
| 540 | this->write32(location); |
| 541 | } |
| 542 | this->write(isGlobal ? ByteCodeInstruction::kLoadExtendedGlobal |
| 543 | : ByteCodeInstruction::kLoadExtended); |
| 544 | this->write8(count); |
| 545 | } else { |
| 546 | this->write(vector_instruction(isGlobal ? ByteCodeInstruction::kLoadGlobal |
| 547 | : ByteCodeInstruction::kLoad, |
| 548 | count)); |
| 549 | this->write8(location); |
| 550 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void ByteCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
| 554 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 555 | this->write32(Interpreter::Value((float) f.fValue).fUnsigned); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 558 | void ByteCodeGenerator::writeIntrinsicCall(const FunctionCall& c) { |
| 559 | auto found = fIntrinsics.find(c.fFunction.fName); |
| 560 | if (found == fIntrinsics.end()) { |
| 561 | fErrors.error(c.fOffset, "unsupported intrinsic function"); |
| 562 | return; |
| 563 | } |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 564 | if (found->second.fIsSpecial) { |
| 565 | SkASSERT(found->second.fValue.fSpecial == SpecialIntrinsic::kDot); |
| 566 | SkASSERT(c.fArguments.size() == 2); |
| 567 | SkASSERT(SlotCount(c.fArguments[0]->fType) == SlotCount(c.fArguments[1]->fType)); |
| 568 | this->write((ByteCodeInstruction) ((int) ByteCodeInstruction::kMultiplyF + |
| 569 | SlotCount(c.fArguments[0]->fType) - 1)); |
| 570 | for (int i = SlotCount(c.fArguments[0]->fType); i > 1; --i) { |
| 571 | this->write(ByteCodeInstruction::kAddF); |
| 572 | } |
| 573 | } else { |
| 574 | switch (found->second.fValue.fInstruction) { |
| 575 | case ByteCodeInstruction::kCos: |
| 576 | case ByteCodeInstruction::kMix: |
| 577 | case ByteCodeInstruction::kSin: |
| 578 | case ByteCodeInstruction::kSqrt: |
| 579 | case ByteCodeInstruction::kTan: |
| 580 | SkASSERT(c.fArguments.size() > 0); |
| 581 | this->write((ByteCodeInstruction) ((int) found->second.fValue.fInstruction + |
| 582 | SlotCount(c.fArguments[0]->fType) - 1)); |
| 583 | break; |
| 584 | case ByteCodeInstruction::kCross: |
| 585 | this->write(found->second.fValue.fInstruction); |
| 586 | break; |
| 587 | default: |
| 588 | SkASSERT(false); |
| 589 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 590 | } |
| 591 | } |
| 592 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 593 | void ByteCodeGenerator::writeFunctionCall(const FunctionCall& f) { |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 594 | for (const auto& arg : f.fArguments) { |
| 595 | this->writeExpression(*arg); |
| 596 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 597 | if (f.fFunction.fBuiltin) { |
| 598 | this->writeIntrinsicCall(f); |
| 599 | return; |
| 600 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 601 | this->write(ByteCodeInstruction::kCall); |
| 602 | fCallTargets.emplace_back(this, f.fFunction); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 603 | } |
| 604 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 605 | void ByteCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
| 606 | this->write(ByteCodeInstruction::kPushImmediate); |
| 607 | this->write32(i.fValue); |
| 608 | } |
| 609 | |
| 610 | void ByteCodeGenerator::writeNullLiteral(const NullLiteral& n) { |
| 611 | // not yet implemented |
| 612 | abort(); |
| 613 | } |
| 614 | |
| 615 | void ByteCodeGenerator::writePrefixExpression(const PrefixExpression& p) { |
| 616 | switch (p.fOperator) { |
| 617 | case Token::Kind::PLUSPLUS: // fall through |
| 618 | case Token::Kind::MINUSMINUS: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 619 | SkASSERT(SlotCount(p.fOperand->fType) == 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 620 | std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand); |
| 621 | lvalue->load(); |
| 622 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 623 | this->write32(type_category(p.fType) == TypeCategory::kFloat |
| 624 | ? Interpreter::Value(1.0f).fUnsigned : 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 625 | if (p.fOperator == Token::Kind::PLUSPLUS) { |
| 626 | this->writeTypedInstruction(p.fType, |
| 627 | ByteCodeInstruction::kAddI, |
| 628 | ByteCodeInstruction::kAddI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 629 | ByteCodeInstruction::kAddF, |
| 630 | 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 631 | } else { |
| 632 | this->writeTypedInstruction(p.fType, |
| 633 | ByteCodeInstruction::kSubtractI, |
| 634 | ByteCodeInstruction::kSubtractI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 635 | ByteCodeInstruction::kSubtractF, |
| 636 | 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 637 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 638 | lvalue->store(); |
| 639 | break; |
| 640 | } |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 641 | case Token::Kind::MINUS: { |
| 642 | this->writeExpression(*p.fOperand); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 643 | this->writeTypedInstruction(p.fType, |
Mike Klein | 1271091 | 2019-05-21 11:04:59 -0500 | [diff] [blame] | 644 | ByteCodeInstruction::kNegateI, |
| 645 | ByteCodeInstruction::kNegateI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 646 | ByteCodeInstruction::kNegateF, |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 647 | SlotCount(p.fOperand->fType)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 648 | break; |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 649 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 650 | default: |
| 651 | SkASSERT(false); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | void ByteCodeGenerator::writePostfixExpression(const PostfixExpression& p) { |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 656 | switch (p.fOperator) { |
| 657 | case Token::Kind::PLUSPLUS: // fall through |
| 658 | case Token::Kind::MINUSMINUS: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 659 | SkASSERT(SlotCount(p.fOperand->fType) == 1); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 660 | std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand); |
| 661 | lvalue->load(); |
| 662 | this->write(ByteCodeInstruction::kDup); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 663 | this->write(ByteCodeInstruction::kPushImmediate); |
| 664 | this->write32(type_category(p.fType) == TypeCategory::kFloat |
| 665 | ? Interpreter::Value(1.0f).fUnsigned : 1); |
| 666 | if (p.fOperator == Token::Kind::PLUSPLUS) { |
| 667 | this->writeTypedInstruction(p.fType, |
| 668 | ByteCodeInstruction::kAddI, |
| 669 | ByteCodeInstruction::kAddI, |
| 670 | ByteCodeInstruction::kAddF, |
| 671 | 1); |
| 672 | } else { |
| 673 | this->writeTypedInstruction(p.fType, |
| 674 | ByteCodeInstruction::kSubtractI, |
| 675 | ByteCodeInstruction::kSubtractI, |
| 676 | ByteCodeInstruction::kSubtractF, |
| 677 | 1); |
| 678 | } |
| 679 | lvalue->store(); |
| 680 | this->write(ByteCodeInstruction::kPop); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 681 | break; |
| 682 | } |
| 683 | default: |
| 684 | SkASSERT(false); |
| 685 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | void ByteCodeGenerator::writeSwizzle(const Swizzle& s) { |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame] | 689 | if (swizzle_is_simple(s)) { |
| 690 | this->writeVariableExpression(s); |
| 691 | return; |
| 692 | } |
| 693 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 694 | switch (s.fBase->fKind) { |
| 695 | case Expression::kVariableReference_Kind: { |
| 696 | const Variable& var = ((VariableReference&) *s.fBase).fVariable; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 697 | this->write(var.fStorage == Variable::kGlobal_Storage |
| 698 | ? ByteCodeInstruction::kLoadSwizzleGlobal |
| 699 | : ByteCodeInstruction::kLoadSwizzle); |
| 700 | this->write8(this->getLocation(var)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 701 | this->write8(s.fComponents.size()); |
| 702 | for (int c : s.fComponents) { |
| 703 | this->write8(c); |
| 704 | } |
| 705 | break; |
| 706 | } |
| 707 | default: |
| 708 | this->writeExpression(*s.fBase); |
| 709 | this->write(ByteCodeInstruction::kSwizzle); |
| 710 | this->write8(s.fBase->fType.columns()); |
| 711 | this->write8(s.fComponents.size()); |
| 712 | for (int c : s.fComponents) { |
| 713 | this->write8(c); |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 718 | void ByteCodeGenerator::writeTernaryExpression(const TernaryExpression& t) { |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 719 | this->writeExpression(*t.fTest); |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 720 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 721 | DeferredLocation trueLocation(this); |
| 722 | this->writeExpression(*t.fIfFalse); |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 723 | this->write(ByteCodeInstruction::kBranch); |
| 724 | DeferredLocation endLocation(this); |
| 725 | trueLocation.set(); |
| 726 | this->writeExpression(*t.fIfTrue); |
| 727 | endLocation.set(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | void ByteCodeGenerator::writeExpression(const Expression& e) { |
| 731 | switch (e.fKind) { |
| 732 | case Expression::kBinary_Kind: |
| 733 | this->writeBinaryExpression((BinaryExpression&) e); |
| 734 | break; |
| 735 | case Expression::kBoolLiteral_Kind: |
| 736 | this->writeBoolLiteral((BoolLiteral&) e); |
| 737 | break; |
| 738 | case Expression::kConstructor_Kind: |
| 739 | this->writeConstructor((Constructor&) e); |
| 740 | break; |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 741 | case Expression::kExternalFunctionCall_Kind: |
| 742 | this->writeExternalFunctionCall((ExternalFunctionCall&) e); |
| 743 | break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 744 | case Expression::kExternalValue_Kind: |
| 745 | this->writeExternalValue((ExternalValueReference&) e); |
| 746 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 747 | case Expression::kFieldAccess_Kind: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 748 | case Expression::kIndex_Kind: |
| 749 | case Expression::kVariableReference_Kind: |
| 750 | this->writeVariableExpression(e); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 751 | break; |
| 752 | case Expression::kFloatLiteral_Kind: |
| 753 | this->writeFloatLiteral((FloatLiteral&) e); |
| 754 | break; |
| 755 | case Expression::kFunctionCall_Kind: |
| 756 | this->writeFunctionCall((FunctionCall&) e); |
| 757 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 758 | case Expression::kIntLiteral_Kind: |
| 759 | this->writeIntLiteral((IntLiteral&) e); |
| 760 | break; |
| 761 | case Expression::kNullLiteral_Kind: |
| 762 | this->writeNullLiteral((NullLiteral&) e); |
| 763 | break; |
| 764 | case Expression::kPrefix_Kind: |
| 765 | this->writePrefixExpression((PrefixExpression&) e); |
| 766 | break; |
| 767 | case Expression::kPostfix_Kind: |
| 768 | this->writePostfixExpression((PostfixExpression&) e); |
| 769 | break; |
| 770 | case Expression::kSwizzle_Kind: |
| 771 | this->writeSwizzle((Swizzle&) e); |
| 772 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 773 | case Expression::kTernary_Kind: |
| 774 | this->writeTernaryExpression((TernaryExpression&) e); |
| 775 | break; |
| 776 | default: |
| 777 | printf("unsupported expression %s\n", e.description().c_str()); |
| 778 | SkASSERT(false); |
| 779 | } |
| 780 | } |
| 781 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 782 | class ByteCodeExternalValueLValue : public ByteCodeGenerator::LValue { |
| 783 | public: |
| 784 | ByteCodeExternalValueLValue(ByteCodeGenerator* generator, ExternalValue& value, int index) |
| 785 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 786 | , fCount(ByteCodeGenerator::SlotCount(value.type())) |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 787 | , fIndex(index) {} |
| 788 | |
| 789 | void load() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 790 | fGenerator.write(vector_instruction(ByteCodeInstruction::kReadExternal, fCount)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 791 | fGenerator.write8(fIndex); |
| 792 | } |
| 793 | |
| 794 | void store() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 795 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, fCount)); |
| 796 | fGenerator.write(vector_instruction(ByteCodeInstruction::kWriteExternal, fCount)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 797 | fGenerator.write8(fIndex); |
| 798 | } |
| 799 | |
| 800 | private: |
| 801 | typedef LValue INHERITED; |
| 802 | |
| 803 | int fCount; |
| 804 | |
| 805 | int fIndex; |
| 806 | }; |
| 807 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 808 | class ByteCodeSwizzleLValue : public ByteCodeGenerator::LValue { |
| 809 | public: |
| 810 | ByteCodeSwizzleLValue(ByteCodeGenerator* generator, const Swizzle& swizzle) |
| 811 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 812 | , fSwizzle(swizzle) {} |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 813 | |
| 814 | void load() override { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 815 | fGenerator.writeSwizzle(fSwizzle); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | void store() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 819 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, |
| 820 | fSwizzle.fComponents.size())); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 821 | Variable::Storage storage; |
| 822 | int location = fGenerator.getLocation(*fSwizzle.fBase, &storage); |
| 823 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 824 | if (location < 0) { |
| 825 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleIndirectGlobal |
| 826 | : ByteCodeInstruction::kStoreSwizzleIndirect); |
| 827 | } else { |
| 828 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleGlobal |
| 829 | : ByteCodeInstruction::kStoreSwizzle); |
| 830 | fGenerator.write8(location); |
| 831 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 832 | fGenerator.write8(fSwizzle.fComponents.size()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 833 | for (int c : fSwizzle.fComponents) { |
| 834 | fGenerator.write8(c); |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | private: |
| 839 | const Swizzle& fSwizzle; |
| 840 | |
| 841 | typedef LValue INHERITED; |
| 842 | }; |
| 843 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 844 | class ByteCodeExpressionLValue : public ByteCodeGenerator::LValue { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 845 | public: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 846 | ByteCodeExpressionLValue(ByteCodeGenerator* generator, const Expression& expr) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 847 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 848 | , fExpression(expr) {} |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 849 | |
| 850 | void load() override { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 851 | fGenerator.writeVariableExpression(fExpression); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | void store() override { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 855 | int count = ByteCodeGenerator::SlotCount(fExpression.fType); |
| 856 | if (count > 4) { |
| 857 | fGenerator.write(ByteCodeInstruction::kDupN); |
| 858 | fGenerator.write8(count); |
| 859 | } else { |
| 860 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count)); |
| 861 | } |
| 862 | Variable::Storage storage; |
| 863 | int location = fGenerator.getLocation(fExpression, &storage); |
| 864 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 865 | if (location < 0 || count > 4) { |
| 866 | if (location >= 0) { |
| 867 | fGenerator.write(ByteCodeInstruction::kPushImmediate); |
| 868 | fGenerator.write32(location); |
| 869 | } |
| 870 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreExtendedGlobal |
| 871 | : ByteCodeInstruction::kStoreExtended); |
| 872 | fGenerator.write8(count); |
| 873 | } else { |
| 874 | fGenerator.write(vector_instruction(isGlobal ? ByteCodeInstruction::kStoreGlobal |
| 875 | : ByteCodeInstruction::kStore, |
| 876 | count)); |
| 877 | fGenerator.write8(location); |
| 878 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | private: |
| 882 | typedef LValue INHERITED; |
| 883 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 884 | const Expression& fExpression; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 885 | }; |
| 886 | |
| 887 | std::unique_ptr<ByteCodeGenerator::LValue> ByteCodeGenerator::getLValue(const Expression& e) { |
| 888 | switch (e.fKind) { |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 889 | case Expression::kExternalValue_Kind: { |
| 890 | ExternalValue* value = ((ExternalValueReference&) e).fValue; |
| 891 | int index = fOutput->fExternalValues.size(); |
| 892 | fOutput->fExternalValues.push_back(value); |
| 893 | SkASSERT(index <= 255); |
| 894 | return std::unique_ptr<LValue>(new ByteCodeExternalValueLValue(this, *value, index)); |
| 895 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 896 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 897 | case Expression::kIndex_Kind: |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 898 | case Expression::kVariableReference_Kind: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 899 | return std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e)); |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame] | 900 | case Expression::kSwizzle_Kind: { |
| 901 | const Swizzle& s = (const Swizzle&) e; |
| 902 | return swizzle_is_simple(s) |
| 903 | ? std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e)) |
| 904 | : std::unique_ptr<LValue>(new ByteCodeSwizzleLValue(this, s)); |
| 905 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 906 | case Expression::kTernary_Kind: |
| 907 | default: |
| 908 | printf("unsupported lvalue %s\n", e.description().c_str()); |
| 909 | return nullptr; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | void ByteCodeGenerator::writeBlock(const Block& b) { |
| 914 | for (const auto& s : b.fStatements) { |
| 915 | this->writeStatement(*s); |
| 916 | } |
| 917 | } |
| 918 | |
| 919 | void ByteCodeGenerator::setBreakTargets() { |
| 920 | std::vector<DeferredLocation>& breaks = fBreakTargets.top(); |
| 921 | for (DeferredLocation& b : breaks) { |
| 922 | b.set(); |
| 923 | } |
| 924 | fBreakTargets.pop(); |
| 925 | } |
| 926 | |
| 927 | void ByteCodeGenerator::setContinueTargets() { |
| 928 | std::vector<DeferredLocation>& continues = fContinueTargets.top(); |
| 929 | for (DeferredLocation& c : continues) { |
| 930 | c.set(); |
| 931 | } |
| 932 | fContinueTargets.pop(); |
| 933 | } |
| 934 | |
| 935 | void ByteCodeGenerator::writeBreakStatement(const BreakStatement& b) { |
| 936 | this->write(ByteCodeInstruction::kBranch); |
| 937 | fBreakTargets.top().emplace_back(this); |
| 938 | } |
| 939 | |
| 940 | void ByteCodeGenerator::writeContinueStatement(const ContinueStatement& c) { |
| 941 | this->write(ByteCodeInstruction::kBranch); |
| 942 | fContinueTargets.top().emplace_back(this); |
| 943 | } |
| 944 | |
| 945 | void ByteCodeGenerator::writeDoStatement(const DoStatement& d) { |
| 946 | fContinueTargets.emplace(); |
| 947 | fBreakTargets.emplace(); |
| 948 | size_t start = fCode->size(); |
| 949 | this->writeStatement(*d.fStatement); |
| 950 | this->setContinueTargets(); |
| 951 | this->writeExpression(*d.fTest); |
| 952 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 953 | this->write16(start); |
| 954 | this->setBreakTargets(); |
| 955 | } |
| 956 | |
| 957 | void ByteCodeGenerator::writeForStatement(const ForStatement& f) { |
| 958 | fContinueTargets.emplace(); |
| 959 | fBreakTargets.emplace(); |
| 960 | if (f.fInitializer) { |
| 961 | this->writeStatement(*f.fInitializer); |
| 962 | } |
| 963 | size_t start = fCode->size(); |
| 964 | if (f.fTest) { |
| 965 | this->writeExpression(*f.fTest); |
| 966 | this->write(ByteCodeInstruction::kNot); |
| 967 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 968 | DeferredLocation endLocation(this); |
| 969 | this->writeStatement(*f.fStatement); |
| 970 | this->setContinueTargets(); |
| 971 | if (f.fNext) { |
| 972 | this->writeExpression(*f.fNext); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 973 | this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType))); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 974 | } |
| 975 | this->write(ByteCodeInstruction::kBranch); |
| 976 | this->write16(start); |
| 977 | endLocation.set(); |
| 978 | } else { |
| 979 | this->writeStatement(*f.fStatement); |
| 980 | this->setContinueTargets(); |
| 981 | if (f.fNext) { |
| 982 | this->writeExpression(*f.fNext); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 983 | this->write(vector_instruction(ByteCodeInstruction::kPop, SlotCount(f.fNext->fType))); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 984 | } |
| 985 | this->write(ByteCodeInstruction::kBranch); |
| 986 | this->write16(start); |
| 987 | } |
| 988 | this->setBreakTargets(); |
| 989 | } |
| 990 | |
| 991 | void ByteCodeGenerator::writeIfStatement(const IfStatement& i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 992 | if (i.fIfFalse) { |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 993 | // if (test) { ..ifTrue.. } else { .. ifFalse .. } |
| 994 | this->writeExpression(*i.fTest); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 995 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 996 | DeferredLocation trueLocation(this); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 997 | this->writeStatement(*i.fIfFalse); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 998 | this->write(ByteCodeInstruction::kBranch); |
| 999 | DeferredLocation endLocation(this); |
| 1000 | trueLocation.set(); |
| 1001 | this->writeStatement(*i.fIfTrue); |
| 1002 | endLocation.set(); |
| 1003 | } else { |
| 1004 | // if (test) { ..ifTrue.. } |
| 1005 | this->writeExpression(*i.fTest); |
| 1006 | this->write(ByteCodeInstruction::kNot); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 1007 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 1008 | DeferredLocation endLocation(this); |
| 1009 | this->writeStatement(*i.fIfTrue); |
| 1010 | endLocation.set(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1011 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | void ByteCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 1015 | this->writeExpression(*r.fExpression); |
| 1016 | this->write(ByteCodeInstruction::kReturn); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1017 | this->write8(SlotCount(r.fExpression->fType)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | void ByteCodeGenerator::writeSwitchStatement(const SwitchStatement& r) { |
| 1021 | // not yet implemented |
| 1022 | abort(); |
| 1023 | } |
| 1024 | |
| 1025 | void ByteCodeGenerator::writeVarDeclarations(const VarDeclarations& v) { |
| 1026 | for (const auto& declStatement : v.fVars) { |
| 1027 | const VarDeclaration& decl = (VarDeclaration&) *declStatement; |
| 1028 | // we need to grab the location even if we don't use it, to ensure it |
| 1029 | // has been allocated |
| 1030 | int location = getLocation(*decl.fVar); |
| 1031 | if (decl.fValue) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1032 | this->writeExpression(*decl.fValue); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1033 | int count = SlotCount(decl.fValue->fType); |
| 1034 | if (count > 4) { |
| 1035 | this->write(ByteCodeInstruction::kPushImmediate); |
| 1036 | this->write32(location); |
| 1037 | this->write(ByteCodeInstruction::kStoreExtended); |
| 1038 | this->write8(count); |
| 1039 | } else { |
| 1040 | this->write(vector_instruction(ByteCodeInstruction::kStore, count)); |
| 1041 | this->write8(location); |
| 1042 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1043 | } |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | void ByteCodeGenerator::writeWhileStatement(const WhileStatement& w) { |
| 1048 | fContinueTargets.emplace(); |
| 1049 | fBreakTargets.emplace(); |
| 1050 | size_t start = fCode->size(); |
| 1051 | this->writeExpression(*w.fTest); |
| 1052 | this->write(ByteCodeInstruction::kNot); |
| 1053 | this->write(ByteCodeInstruction::kConditionalBranch); |
| 1054 | DeferredLocation endLocation(this); |
| 1055 | this->writeStatement(*w.fStatement); |
| 1056 | this->setContinueTargets(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1057 | this->write(ByteCodeInstruction::kBranch); |
| 1058 | this->write16(start); |
| 1059 | endLocation.set(); |
| 1060 | this->setBreakTargets(); |
| 1061 | } |
| 1062 | |
| 1063 | void ByteCodeGenerator::writeStatement(const Statement& s) { |
| 1064 | switch (s.fKind) { |
| 1065 | case Statement::kBlock_Kind: |
| 1066 | this->writeBlock((Block&) s); |
| 1067 | break; |
| 1068 | case Statement::kBreak_Kind: |
| 1069 | this->writeBreakStatement((BreakStatement&) s); |
| 1070 | break; |
| 1071 | case Statement::kContinue_Kind: |
| 1072 | this->writeContinueStatement((ContinueStatement&) s); |
| 1073 | break; |
| 1074 | case Statement::kDiscard_Kind: |
| 1075 | // not yet implemented |
| 1076 | abort(); |
| 1077 | case Statement::kDo_Kind: |
| 1078 | this->writeDoStatement((DoStatement&) s); |
| 1079 | break; |
| 1080 | case Statement::kExpression_Kind: { |
| 1081 | const Expression& expr = *((ExpressionStatement&) s).fExpression; |
| 1082 | this->writeExpression(expr); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1083 | int count = SlotCount(expr.fType); |
| 1084 | if (count > 4) { |
| 1085 | this->write(ByteCodeInstruction::kPopN); |
| 1086 | this->write8(count); |
| 1087 | } else { |
| 1088 | this->write(vector_instruction(ByteCodeInstruction::kPop, count)); |
| 1089 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1090 | break; |
| 1091 | } |
| 1092 | case Statement::kFor_Kind: |
| 1093 | this->writeForStatement((ForStatement&) s); |
| 1094 | break; |
| 1095 | case Statement::kIf_Kind: |
| 1096 | this->writeIfStatement((IfStatement&) s); |
| 1097 | break; |
| 1098 | case Statement::kNop_Kind: |
| 1099 | break; |
| 1100 | case Statement::kReturn_Kind: |
| 1101 | this->writeReturnStatement((ReturnStatement&) s); |
| 1102 | break; |
| 1103 | case Statement::kSwitch_Kind: |
| 1104 | this->writeSwitchStatement((SwitchStatement&) s); |
| 1105 | break; |
| 1106 | case Statement::kVarDeclarations_Kind: |
| 1107 | this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration); |
| 1108 | break; |
| 1109 | case Statement::kWhile_Kind: |
| 1110 | this->writeWhileStatement((WhileStatement&) s); |
| 1111 | break; |
| 1112 | default: |
| 1113 | SkASSERT(false); |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | } |