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" |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 9 | |
Brian Osman | 95253bd | 2019-06-05 10:28:45 -0400 | [diff] [blame] | 10 | #include <algorithm> |
| 11 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 12 | namespace SkSL { |
| 13 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 14 | ByteCodeGenerator::ByteCodeGenerator(const Context* context, const Program* program, ErrorReporter* errors, |
| 15 | ByteCode* output) |
| 16 | : INHERITED(program, errors, nullptr) |
| 17 | , fContext(*context) |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 18 | , fOutput(output) |
| 19 | , fIntrinsics { |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame] | 20 | { "cos", ByteCodeInstruction::kCos }, |
| 21 | { "cross", ByteCodeInstruction::kCross }, |
| 22 | { "dot", SpecialIntrinsic::kDot }, |
| 23 | { "inverse", ByteCodeInstruction::kInverse2x2 }, |
| 24 | { "sin", ByteCodeInstruction::kSin }, |
| 25 | { "sqrt", ByteCodeInstruction::kSqrt }, |
| 26 | { "tan", ByteCodeInstruction::kTan }, |
| 27 | { "mix", ByteCodeInstruction::kMix }, |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 28 | } {} |
| 29 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 30 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 31 | int ByteCodeGenerator::SlotCount(const Type& type) { |
Brian Osman | fba386b | 2019-06-20 14:54:15 -0400 | [diff] [blame] | 32 | if (type.kind() == Type::kOther_Kind) { |
| 33 | return 0; |
| 34 | } else if (type.kind() == Type::kStruct_Kind) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 35 | int slots = 0; |
| 36 | for (const auto& f : type.fields()) { |
| 37 | slots += SlotCount(*f.fType); |
| 38 | } |
| 39 | SkASSERT(slots <= 255); |
| 40 | return slots; |
| 41 | } else if (type.kind() == Type::kArray_Kind) { |
| 42 | int columns = type.columns(); |
| 43 | SkASSERT(columns >= 0); |
| 44 | int slots = columns * SlotCount(type.componentType()); |
| 45 | SkASSERT(slots <= 255); |
| 46 | return slots; |
| 47 | } else { |
| 48 | return type.columns() * type.rows(); |
| 49 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | bool ByteCodeGenerator::generateCode() { |
| 53 | for (const auto& e : fProgram) { |
| 54 | switch (e.fKind) { |
| 55 | case ProgramElement::kFunction_Kind: { |
| 56 | std::unique_ptr<ByteCodeFunction> f = this->writeFunction((FunctionDefinition&) e); |
| 57 | if (!f) { |
| 58 | return false; |
| 59 | } |
| 60 | fOutput->fFunctions.push_back(std::move(f)); |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 61 | fFunctions.push_back(&(FunctionDefinition&)e); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 62 | break; |
| 63 | } |
| 64 | case ProgramElement::kVar_Kind: { |
| 65 | VarDeclarations& decl = (VarDeclarations&) e; |
| 66 | for (const auto& v : decl.fVars) { |
| 67 | const Variable* declVar = ((VarDeclaration&) *v).fVar; |
| 68 | if (declVar->fModifiers.fLayout.fBuiltin >= 0) { |
| 69 | continue; |
| 70 | } |
| 71 | if (declVar->fModifiers.fFlags & Modifiers::kIn_Flag) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 72 | for (int i = SlotCount(declVar->fType); i > 0; --i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 73 | fOutput->fInputSlots.push_back(fOutput->fGlobalCount++); |
| 74 | } |
| 75 | } else { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 76 | fOutput->fGlobalCount += SlotCount(declVar->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | break; |
| 80 | } |
| 81 | default: |
| 82 | ; // ignore |
| 83 | } |
| 84 | } |
Brian Osman | 6f5358f | 2019-07-09 14:17:23 -0400 | [diff] [blame] | 85 | return 0 == fErrors.errorCount(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 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)); |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 91 | fParameterCount = result->fParameterCount; |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 92 | fLoopCount = fMaxLoopCount = 0; |
| 93 | fConditionCount = fMaxConditionCount = 0; |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 94 | fStackCount = fMaxStackCount = 0; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 95 | fCode = &result->fCode; |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 96 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 97 | this->writeStatement(*f.fBody); |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 98 | if (0 == fErrors.errorCount()) { |
| 99 | SkASSERT(fLoopCount == 0); |
| 100 | SkASSERT(fConditionCount == 0); |
| 101 | SkASSERT(fStackCount == 0); |
| 102 | } |
| 103 | this->write(ByteCodeInstruction::kReturn, 0); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 104 | this->write8(0); |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 105 | |
| 106 | result->fLocalCount = fLocals.size(); |
| 107 | result->fConditionCount = fMaxConditionCount; |
| 108 | result->fLoopCount = fMaxLoopCount; |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 109 | result->fStackCount = fMaxStackCount; |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 110 | |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 111 | const Type& returnType = f.fDeclaration.fReturnType; |
| 112 | if (returnType != *fContext.fVoid_Type) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 113 | result->fReturnCount = SlotCount(returnType); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 114 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 115 | fLocals.clear(); |
| 116 | fFunction = nullptr; |
| 117 | return result; |
| 118 | } |
| 119 | |
| 120 | enum class TypeCategory { |
| 121 | kBool, |
| 122 | kSigned, |
| 123 | kUnsigned, |
| 124 | kFloat, |
| 125 | }; |
| 126 | |
| 127 | static TypeCategory type_category(const Type& type) { |
| 128 | switch (type.kind()) { |
| 129 | case Type::Kind::kVector_Kind: |
| 130 | case Type::Kind::kMatrix_Kind: |
| 131 | return type_category(type.componentType()); |
| 132 | default: |
| 133 | if (type.fName == "bool") { |
| 134 | return TypeCategory::kBool; |
| 135 | } else if (type.fName == "int" || type.fName == "short") { |
| 136 | return TypeCategory::kSigned; |
| 137 | } else if (type.fName == "uint" || type.fName == "ushort") { |
| 138 | return TypeCategory::kUnsigned; |
| 139 | } else { |
| 140 | SkASSERT(type.fName == "float" || type.fName == "half"); |
| 141 | return TypeCategory::kFloat; |
| 142 | } |
| 143 | ABORT("unsupported type: %s\n", type.description().c_str()); |
| 144 | } |
| 145 | } |
| 146 | |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame] | 147 | // A "simple" Swizzle is based on a variable (or a compound variable like a struct or array), and |
| 148 | // that references consecutive values, such that it can be implemented using normal load/store ops |
| 149 | // with an offset. Note that all single-component swizzles (of suitable base types) are simple. |
| 150 | static bool swizzle_is_simple(const Swizzle& s) { |
| 151 | switch (s.fBase->fKind) { |
| 152 | case Expression::kFieldAccess_Kind: |
| 153 | case Expression::kIndex_Kind: |
| 154 | case Expression::kVariableReference_Kind: |
| 155 | break; |
| 156 | default: |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | for (size_t i = 1; i < s.fComponents.size(); ++i) { |
| 161 | if (s.fComponents[i] != s.fComponents[i - 1] + 1) { |
| 162 | return false; |
| 163 | } |
| 164 | } |
| 165 | return true; |
| 166 | } |
| 167 | |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 168 | int ByteCodeGenerator::StackUsage(ByteCodeInstruction inst, int count_) { |
| 169 | // Ensures that we use count iff we're passed a non-default value. Most instructions have an |
| 170 | // implicit count, so the caller shouldn't need to worry about it (or count makes no sense). |
| 171 | // The asserts avoids callers thinking they're supplying useful information in that scenario, |
| 172 | // or failing to supply necessary information for the ops that need a count. |
| 173 | struct CountValue { |
| 174 | operator int() { |
| 175 | SkASSERT(val != ByteCodeGenerator::kUnusedStackCount); |
| 176 | SkDEBUGCODE(used = true); |
| 177 | return val; |
| 178 | } |
| 179 | ~CountValue() { |
| 180 | SkASSERT(used || val == ByteCodeGenerator::kUnusedStackCount); |
| 181 | } |
| 182 | int val; |
| 183 | SkDEBUGCODE(bool used = false;) |
| 184 | } count = { count_ }; |
| 185 | |
| 186 | switch (inst) { |
| 187 | // Unary functions/operators that don't change stack depth at all: |
| 188 | #define VECTOR_UNARY_OP(base) \ |
| 189 | case ByteCodeInstruction::base: \ |
| 190 | case ByteCodeInstruction::base ## 2: \ |
| 191 | case ByteCodeInstruction::base ## 3: \ |
| 192 | case ByteCodeInstruction::base ## 4: \ |
| 193 | return 0; |
| 194 | |
| 195 | VECTOR_UNARY_OP(kConvertFtoI) |
| 196 | VECTOR_UNARY_OP(kConvertStoF) |
| 197 | VECTOR_UNARY_OP(kConvertUtoF) |
| 198 | |
| 199 | VECTOR_UNARY_OP(kCos) |
| 200 | VECTOR_UNARY_OP(kSin) |
| 201 | VECTOR_UNARY_OP(kSqrt) |
| 202 | VECTOR_UNARY_OP(kTan) |
| 203 | |
| 204 | VECTOR_UNARY_OP(kNegateF) |
| 205 | VECTOR_UNARY_OP(kNegateI) |
| 206 | |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame] | 207 | case ByteCodeInstruction::kInverse2x2: |
| 208 | case ByteCodeInstruction::kInverse3x3: |
| 209 | case ByteCodeInstruction::kInverse4x4: return 0; |
| 210 | |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 211 | case ByteCodeInstruction::kClampIndex: return 0; |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 212 | case ByteCodeInstruction::kNotB: return 0; |
| 213 | case ByteCodeInstruction::kNegateFN: return 0; |
| 214 | |
| 215 | #undef VECTOR_UNARY_OP |
| 216 | |
| 217 | // Binary functions/operators that do a 2 -> 1 reduction (possibly N times) |
| 218 | #define VECTOR_BINARY_OP(base) \ |
| 219 | case ByteCodeInstruction::base: return -1; \ |
| 220 | case ByteCodeInstruction::base ## 2: return -2; \ |
| 221 | case ByteCodeInstruction::base ## 3: return -3; \ |
| 222 | case ByteCodeInstruction::base ## 4: return -4; |
| 223 | |
| 224 | #define VECTOR_MATRIX_BINARY_OP(base) \ |
| 225 | VECTOR_BINARY_OP(base) \ |
| 226 | case ByteCodeInstruction::base ## N: return -count; |
| 227 | |
| 228 | case ByteCodeInstruction::kAndB: return -1; |
| 229 | case ByteCodeInstruction::kOrB: return -1; |
| 230 | case ByteCodeInstruction::kXorB: return -1; |
| 231 | |
| 232 | VECTOR_BINARY_OP(kAddI) |
| 233 | VECTOR_MATRIX_BINARY_OP(kAddF) |
| 234 | |
| 235 | VECTOR_BINARY_OP(kCompareIEQ) |
| 236 | VECTOR_MATRIX_BINARY_OP(kCompareFEQ) |
| 237 | VECTOR_BINARY_OP(kCompareINEQ) |
| 238 | VECTOR_MATRIX_BINARY_OP(kCompareFNEQ) |
| 239 | VECTOR_BINARY_OP(kCompareSGT) |
| 240 | VECTOR_BINARY_OP(kCompareUGT) |
| 241 | VECTOR_BINARY_OP(kCompareFGT) |
| 242 | VECTOR_BINARY_OP(kCompareSGTEQ) |
| 243 | VECTOR_BINARY_OP(kCompareUGTEQ) |
| 244 | VECTOR_BINARY_OP(kCompareFGTEQ) |
| 245 | VECTOR_BINARY_OP(kCompareSLT) |
| 246 | VECTOR_BINARY_OP(kCompareULT) |
| 247 | VECTOR_BINARY_OP(kCompareFLT) |
| 248 | VECTOR_BINARY_OP(kCompareSLTEQ) |
| 249 | VECTOR_BINARY_OP(kCompareULTEQ) |
| 250 | VECTOR_BINARY_OP(kCompareFLTEQ) |
| 251 | |
| 252 | VECTOR_BINARY_OP(kDivideS) |
| 253 | VECTOR_BINARY_OP(kDivideU) |
| 254 | VECTOR_MATRIX_BINARY_OP(kDivideF) |
| 255 | VECTOR_BINARY_OP(kMultiplyI) |
| 256 | VECTOR_MATRIX_BINARY_OP(kMultiplyF) |
| 257 | VECTOR_BINARY_OP(kRemainderF) |
| 258 | VECTOR_BINARY_OP(kRemainderS) |
| 259 | VECTOR_BINARY_OP(kRemainderU) |
| 260 | VECTOR_BINARY_OP(kSubtractI) |
| 261 | VECTOR_MATRIX_BINARY_OP(kSubtractF) |
| 262 | |
| 263 | #undef VECTOR_BINARY_OP |
| 264 | #undef VECTOR_MATRIX_BINARY_OP |
| 265 | |
| 266 | // Strange math operations with other behavior: |
| 267 | case ByteCodeInstruction::kCross: return -3; |
| 268 | // Binary, but also consumes T: |
| 269 | case ByteCodeInstruction::kMix: return -2; |
| 270 | case ByteCodeInstruction::kMix2: return -3; |
| 271 | case ByteCodeInstruction::kMix3: return -4; |
| 272 | case ByteCodeInstruction::kMix4: return -5; |
| 273 | |
| 274 | // Ops that push or load data to grow the stack: |
| 275 | case ByteCodeInstruction::kDup: |
| 276 | case ByteCodeInstruction::kLoad: |
| 277 | case ByteCodeInstruction::kLoadGlobal: |
| 278 | case ByteCodeInstruction::kReadExternal: |
| 279 | case ByteCodeInstruction::kPushImmediate: |
| 280 | return 1; |
| 281 | |
| 282 | case ByteCodeInstruction::kDup2: |
| 283 | case ByteCodeInstruction::kLoad2: |
| 284 | case ByteCodeInstruction::kLoadGlobal2: |
| 285 | case ByteCodeInstruction::kReadExternal2: |
| 286 | return 2; |
| 287 | |
| 288 | case ByteCodeInstruction::kDup3: |
| 289 | case ByteCodeInstruction::kLoad3: |
| 290 | case ByteCodeInstruction::kLoadGlobal3: |
| 291 | case ByteCodeInstruction::kReadExternal3: |
| 292 | return 3; |
| 293 | |
| 294 | case ByteCodeInstruction::kDup4: |
| 295 | case ByteCodeInstruction::kLoad4: |
| 296 | case ByteCodeInstruction::kLoadGlobal4: |
| 297 | case ByteCodeInstruction::kReadExternal4: |
| 298 | return 4; |
| 299 | |
| 300 | case ByteCodeInstruction::kDupN: |
| 301 | case ByteCodeInstruction::kLoadSwizzle: |
| 302 | case ByteCodeInstruction::kLoadSwizzleGlobal: |
| 303 | return count; |
| 304 | |
| 305 | // Pushes 'count' values, minus one for the 'address' that's consumed first |
| 306 | case ByteCodeInstruction::kLoadExtended: |
| 307 | case ByteCodeInstruction::kLoadExtendedGlobal: |
| 308 | return count - 1; |
| 309 | |
| 310 | // Ops that pop or store data to shrink the stack: |
| 311 | case ByteCodeInstruction::kPop: |
| 312 | case ByteCodeInstruction::kStore: |
| 313 | case ByteCodeInstruction::kStoreGlobal: |
| 314 | case ByteCodeInstruction::kWriteExternal: |
| 315 | return -1; |
| 316 | |
| 317 | case ByteCodeInstruction::kPop2: |
| 318 | case ByteCodeInstruction::kStore2: |
| 319 | case ByteCodeInstruction::kStoreGlobal2: |
| 320 | case ByteCodeInstruction::kWriteExternal2: |
| 321 | return -2; |
| 322 | |
| 323 | case ByteCodeInstruction::kPop3: |
| 324 | case ByteCodeInstruction::kStore3: |
| 325 | case ByteCodeInstruction::kStoreGlobal3: |
| 326 | case ByteCodeInstruction::kWriteExternal3: |
| 327 | return -3; |
| 328 | |
| 329 | case ByteCodeInstruction::kPop4: |
| 330 | case ByteCodeInstruction::kStore4: |
| 331 | case ByteCodeInstruction::kStoreGlobal4: |
| 332 | case ByteCodeInstruction::kWriteExternal4: |
| 333 | return -4; |
| 334 | |
| 335 | case ByteCodeInstruction::kPopN: |
| 336 | case ByteCodeInstruction::kStoreSwizzle: |
| 337 | case ByteCodeInstruction::kStoreSwizzleGlobal: |
| 338 | return -count; |
| 339 | |
| 340 | // Consumes 'count' values, plus one for the 'address' |
| 341 | case ByteCodeInstruction::kStoreExtended: |
| 342 | case ByteCodeInstruction::kStoreExtendedGlobal: |
| 343 | case ByteCodeInstruction::kStoreSwizzleIndirect: |
| 344 | case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: |
| 345 | return -count - 1; |
| 346 | |
| 347 | // Strange ops where the caller computes the delta for us: |
| 348 | case ByteCodeInstruction::kCallExternal: |
| 349 | case ByteCodeInstruction::kMatrixToMatrix: |
| 350 | case ByteCodeInstruction::kMatrixMultiply: |
| 351 | case ByteCodeInstruction::kReserve: |
| 352 | case ByteCodeInstruction::kReturn: |
| 353 | case ByteCodeInstruction::kScalarToMatrix: |
| 354 | case ByteCodeInstruction::kSwizzle: |
| 355 | return count; |
| 356 | |
| 357 | // Miscellaneous |
| 358 | |
| 359 | // kCall is net-zero. Max stack depth is adjusted in writeFunctionCall. |
| 360 | case ByteCodeInstruction::kCall: return 0; |
| 361 | case ByteCodeInstruction::kBranch: return 0; |
| 362 | case ByteCodeInstruction::kBranchIfAllFalse: return 0; |
| 363 | |
| 364 | case ByteCodeInstruction::kMaskPush: return -1; |
| 365 | case ByteCodeInstruction::kMaskPop: return 0; |
| 366 | case ByteCodeInstruction::kMaskNegate: return 0; |
| 367 | case ByteCodeInstruction::kMaskBlend: return -count; |
| 368 | |
| 369 | case ByteCodeInstruction::kLoopBegin: return 0; |
| 370 | case ByteCodeInstruction::kLoopNext: return 0; |
| 371 | case ByteCodeInstruction::kLoopMask: return -1; |
| 372 | case ByteCodeInstruction::kLoopEnd: return 0; |
| 373 | case ByteCodeInstruction::kLoopBreak: return 0; |
| 374 | case ByteCodeInstruction::kLoopContinue: return 0; |
| 375 | |
| 376 | default: |
Brian Osman | c7ec9e2 | 2019-07-16 08:49:11 -0400 | [diff] [blame] | 377 | ABORT("unsupported instruction %d\n", (int)inst); |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 378 | return 0; |
| 379 | } |
| 380 | } |
| 381 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 382 | int ByteCodeGenerator::getLocation(const Variable& var) { |
| 383 | // given that we seldom have more than a couple of variables, linear search is probably the most |
| 384 | // efficient way to handle lookups |
| 385 | switch (var.fStorage) { |
| 386 | case Variable::kLocal_Storage: { |
| 387 | for (int i = fLocals.size() - 1; i >= 0; --i) { |
| 388 | if (fLocals[i] == &var) { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 389 | SkASSERT(fParameterCount + i <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 390 | return fParameterCount + i; |
| 391 | } |
| 392 | } |
| 393 | int result = fParameterCount + fLocals.size(); |
| 394 | fLocals.push_back(&var); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 395 | for (int i = 0; i < SlotCount(var.fType) - 1; ++i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 396 | fLocals.push_back(nullptr); |
| 397 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 398 | SkASSERT(result <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 399 | return result; |
| 400 | } |
| 401 | case Variable::kParameter_Storage: { |
| 402 | int offset = 0; |
| 403 | for (const auto& p : fFunction->fDeclaration.fParameters) { |
| 404 | if (p == &var) { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 405 | SkASSERT(offset <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 406 | return offset; |
| 407 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 408 | offset += SlotCount(p->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 409 | } |
| 410 | SkASSERT(false); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 411 | return 0; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 412 | } |
| 413 | case Variable::kGlobal_Storage: { |
| 414 | int offset = 0; |
| 415 | for (const auto& e : fProgram) { |
| 416 | if (e.fKind == ProgramElement::kVar_Kind) { |
| 417 | VarDeclarations& decl = (VarDeclarations&) e; |
| 418 | for (const auto& v : decl.fVars) { |
| 419 | const Variable* declVar = ((VarDeclaration&) *v).fVar; |
| 420 | if (declVar->fModifiers.fLayout.fBuiltin >= 0) { |
| 421 | continue; |
| 422 | } |
| 423 | if (declVar == &var) { |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 424 | SkASSERT(offset <= 255); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 425 | return offset; |
| 426 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 427 | offset += SlotCount(declVar->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | } |
| 431 | SkASSERT(false); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 432 | return 0; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 433 | } |
| 434 | default: |
| 435 | SkASSERT(false); |
| 436 | return 0; |
| 437 | } |
| 438 | } |
| 439 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 440 | int ByteCodeGenerator::getLocation(const Expression& expr, Variable::Storage* storage) { |
| 441 | switch (expr.fKind) { |
| 442 | case Expression::kFieldAccess_Kind: { |
| 443 | const FieldAccess& f = (const FieldAccess&)expr; |
| 444 | int baseAddr = this->getLocation(*f.fBase, storage); |
| 445 | int offset = 0; |
| 446 | for (int i = 0; i < f.fFieldIndex; ++i) { |
| 447 | offset += SlotCount(*f.fBase->fType.fields()[i].fType); |
| 448 | } |
| 449 | if (baseAddr < 0) { |
Brian Osman | 8676929 | 2019-06-21 11:05:47 -0400 | [diff] [blame] | 450 | if (offset != 0) { |
| 451 | this->write(ByteCodeInstruction::kPushImmediate); |
| 452 | this->write32(offset); |
| 453 | this->write(ByteCodeInstruction::kAddI); |
| 454 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 455 | return -1; |
| 456 | } else { |
| 457 | return baseAddr + offset; |
| 458 | } |
| 459 | } |
| 460 | case Expression::kIndex_Kind: { |
| 461 | const IndexExpression& i = (const IndexExpression&)expr; |
| 462 | int stride = SlotCount(i.fType); |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 463 | int length = i.fBase->fType.columns(); |
| 464 | SkASSERT(length <= 255); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 465 | int offset = -1; |
| 466 | if (i.fIndex->isConstant()) { |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 467 | int64_t index = i.fIndex->getConstantInt(); |
| 468 | if (index < 0 || index >= length) { |
| 469 | fErrors.error(i.fIndex->fOffset, "Array index out of bounds."); |
| 470 | return 0; |
| 471 | } |
| 472 | offset = index * stride; |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 473 | } else { |
Brian Osman | 8676929 | 2019-06-21 11:05:47 -0400 | [diff] [blame] | 474 | if (i.fIndex->hasSideEffects()) { |
| 475 | // Having a side-effect in an indexer is technically safe for an rvalue, |
| 476 | // but with lvalues we have to evaluate the indexer twice, so make it an error. |
| 477 | fErrors.error(i.fIndex->fOffset, |
| 478 | "Index expressions with side-effects not supported in byte code."); |
| 479 | return 0; |
| 480 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 481 | this->writeExpression(*i.fIndex); |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 482 | this->write(ByteCodeInstruction::kClampIndex); |
| 483 | this->write8(length); |
Brian Osman | 8676929 | 2019-06-21 11:05:47 -0400 | [diff] [blame] | 484 | if (stride != 1) { |
| 485 | this->write(ByteCodeInstruction::kPushImmediate); |
| 486 | this->write32(stride); |
| 487 | this->write(ByteCodeInstruction::kMultiplyI); |
| 488 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 489 | } |
| 490 | int baseAddr = this->getLocation(*i.fBase, storage); |
Brian Osman | 8676929 | 2019-06-21 11:05:47 -0400 | [diff] [blame] | 491 | |
| 492 | // Are both components known statically? |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 493 | if (baseAddr >= 0 && offset >= 0) { |
| 494 | return baseAddr + offset; |
| 495 | } |
Brian Osman | 8676929 | 2019-06-21 11:05:47 -0400 | [diff] [blame] | 496 | |
| 497 | // At least one component is dynamic (and on the stack). |
| 498 | |
| 499 | // If the other component is zero, we're done |
| 500 | if (baseAddr == 0 || offset == 0) { |
| 501 | return -1; |
| 502 | } |
| 503 | |
| 504 | // Push the non-dynamic component (if any) to the stack, then add the two |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 505 | if (baseAddr >= 0) { |
| 506 | this->write(ByteCodeInstruction::kPushImmediate); |
| 507 | this->write32(baseAddr); |
| 508 | } |
| 509 | if (offset >= 0) { |
| 510 | this->write(ByteCodeInstruction::kPushImmediate); |
| 511 | this->write32(offset); |
| 512 | } |
| 513 | this->write(ByteCodeInstruction::kAddI); |
| 514 | return -1; |
| 515 | } |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame] | 516 | case Expression::kSwizzle_Kind: { |
| 517 | const Swizzle& s = (const Swizzle&)expr; |
| 518 | SkASSERT(swizzle_is_simple(s)); |
| 519 | int baseAddr = this->getLocation(*s.fBase, storage); |
| 520 | int offset = s.fComponents[0]; |
| 521 | if (baseAddr < 0) { |
Brian Osman | 8676929 | 2019-06-21 11:05:47 -0400 | [diff] [blame] | 522 | if (offset != 0) { |
| 523 | this->write(ByteCodeInstruction::kPushImmediate); |
| 524 | this->write32(offset); |
| 525 | this->write(ByteCodeInstruction::kAddI); |
| 526 | } |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame] | 527 | return -1; |
| 528 | } else { |
| 529 | return baseAddr + offset; |
| 530 | } |
| 531 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 532 | case Expression::kVariableReference_Kind: { |
| 533 | const Variable& var = ((const VariableReference&)expr).fVariable; |
| 534 | *storage = var.fStorage; |
| 535 | return this->getLocation(var); |
| 536 | } |
| 537 | default: |
| 538 | SkASSERT(false); |
| 539 | return 0; |
| 540 | } |
| 541 | } |
| 542 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 543 | void ByteCodeGenerator::write8(uint8_t b) { |
| 544 | fCode->push_back(b); |
| 545 | } |
| 546 | |
| 547 | void ByteCodeGenerator::write16(uint16_t i) { |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 548 | size_t n = fCode->size(); |
| 549 | fCode->resize(n+2); |
| 550 | memcpy(fCode->data() + n, &i, 2); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void ByteCodeGenerator::write32(uint32_t i) { |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 554 | size_t n = fCode->size(); |
| 555 | fCode->resize(n+4); |
| 556 | memcpy(fCode->data() + n, &i, 4); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 557 | } |
| 558 | |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 559 | void ByteCodeGenerator::write(ByteCodeInstruction i, int count) { |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 560 | switch (i) { |
| 561 | case ByteCodeInstruction::kLoopBegin: this->enterLoop(); break; |
| 562 | case ByteCodeInstruction::kLoopEnd: this->exitLoop(); break; |
| 563 | |
| 564 | case ByteCodeInstruction::kMaskPush: this->enterCondition(); break; |
| 565 | case ByteCodeInstruction::kMaskPop: |
| 566 | case ByteCodeInstruction::kMaskBlend: this->exitCondition(); break; |
| 567 | default: /* Do nothing */ break; |
| 568 | } |
Mike Klein | 108e935 | 2019-05-21 11:05:17 -0500 | [diff] [blame] | 569 | this->write16((uint16_t)i); |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 570 | fStackCount += StackUsage(i, count); |
| 571 | fMaxStackCount = std::max(fMaxStackCount, fStackCount); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 572 | } |
| 573 | |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 574 | static ByteCodeInstruction vector_instruction(ByteCodeInstruction base, int count) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 575 | SkASSERT(count >= 1 && count <= 4); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 576 | return ((ByteCodeInstruction) ((int) base + count - 1)); |
| 577 | } |
| 578 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 579 | void ByteCodeGenerator::writeTypedInstruction(const Type& type, ByteCodeInstruction s, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 580 | ByteCodeInstruction u, ByteCodeInstruction f, |
| 581 | int count) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 582 | switch (type_category(type)) { |
| 583 | case TypeCategory::kSigned: |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 584 | this->write(vector_instruction(s, count)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 585 | break; |
| 586 | case TypeCategory::kUnsigned: |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 587 | this->write(vector_instruction(u, count)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 588 | break; |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 589 | case TypeCategory::kFloat: { |
| 590 | if (count > 4) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 591 | this->write((ByteCodeInstruction)((int)f + 4), count); |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 592 | this->write8(count); |
| 593 | } else { |
| 594 | this->write(vector_instruction(f, count)); |
| 595 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 596 | break; |
Brian Osman | 1e855b2 | 2019-05-29 15:21:52 -0400 | [diff] [blame] | 597 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 598 | default: |
| 599 | SkASSERT(false); |
| 600 | } |
| 601 | } |
| 602 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 603 | bool ByteCodeGenerator::writeBinaryExpression(const BinaryExpression& b, bool discard) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 604 | if (b.fOperator == Token::Kind::EQ) { |
| 605 | std::unique_ptr<LValue> lvalue = this->getLValue(*b.fLeft); |
| 606 | this->writeExpression(*b.fRight); |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 607 | lvalue->store(discard); |
| 608 | discard = false; |
| 609 | return discard; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 610 | } |
Brian Osman | 16e6fd5 | 2019-05-29 11:19:00 -0400 | [diff] [blame] | 611 | const Type& lType = b.fLeft->fType; |
| 612 | const Type& rType = b.fRight->fType; |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 613 | bool lVecOrMtx = (lType.kind() == Type::kVector_Kind || lType.kind() == Type::kMatrix_Kind); |
| 614 | bool rVecOrMtx = (rType.kind() == Type::kVector_Kind || rType.kind() == Type::kMatrix_Kind); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 615 | Token::Kind op; |
| 616 | std::unique_ptr<LValue> lvalue; |
| 617 | if (is_assignment(b.fOperator)) { |
| 618 | lvalue = this->getLValue(*b.fLeft); |
| 619 | lvalue->load(); |
| 620 | op = remove_assignment(b.fOperator); |
| 621 | } else { |
| 622 | this->writeExpression(*b.fLeft); |
| 623 | op = b.fOperator; |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 624 | if (!lVecOrMtx && rVecOrMtx) { |
Brian Osman | 16e6fd5 | 2019-05-29 11:19:00 -0400 | [diff] [blame] | 625 | for (int i = SlotCount(rType); i > 1; --i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 626 | this->write(ByteCodeInstruction::kDup); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | this->writeExpression(*b.fRight); |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 631 | if (lVecOrMtx && !rVecOrMtx) { |
Brian Osman | 16e6fd5 | 2019-05-29 11:19:00 -0400 | [diff] [blame] | 632 | for (int i = SlotCount(lType); i > 1; --i) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 633 | this->write(ByteCodeInstruction::kDup); |
| 634 | } |
| 635 | } |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 636 | // Special case for M*V, V*M, M*M (but not V*V!) |
| 637 | if (op == Token::Kind::STAR && lVecOrMtx && rVecOrMtx && |
| 638 | !(lType.kind() == Type::kVector_Kind && rType.kind() == Type::kVector_Kind)) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 639 | this->write(ByteCodeInstruction::kMatrixMultiply, |
| 640 | SlotCount(b.fType) - (SlotCount(lType) + SlotCount(rType))); |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 641 | int rCols = rType.columns(), |
| 642 | rRows = rType.rows(), |
| 643 | lCols = lType.columns(), |
| 644 | lRows = lType.rows(); |
| 645 | // M*V treats the vector as a column |
| 646 | if (rType.kind() == Type::kVector_Kind) { |
| 647 | std::swap(rCols, rRows); |
| 648 | } |
| 649 | SkASSERT(lCols == rRows); |
| 650 | SkASSERT(SlotCount(b.fType) == lRows * rCols); |
| 651 | this->write8(lCols); |
| 652 | this->write8(lRows); |
| 653 | this->write8(rCols); |
| 654 | } else { |
| 655 | int count = std::max(SlotCount(lType), SlotCount(rType)); |
| 656 | switch (op) { |
| 657 | case Token::Kind::EQEQ: |
| 658 | this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareIEQ, |
| 659 | ByteCodeInstruction::kCompareIEQ, |
| 660 | ByteCodeInstruction::kCompareFEQ, |
| 661 | count); |
| 662 | // Collapse to a single bool |
| 663 | for (int i = count; i > 1; --i) { |
| 664 | this->write(ByteCodeInstruction::kAndB); |
| 665 | } |
| 666 | break; |
| 667 | case Token::Kind::GT: |
| 668 | this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSGT, |
| 669 | ByteCodeInstruction::kCompareUGT, |
| 670 | ByteCodeInstruction::kCompareFGT, |
| 671 | count); |
| 672 | break; |
| 673 | case Token::Kind::GTEQ: |
| 674 | this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSGTEQ, |
| 675 | ByteCodeInstruction::kCompareUGTEQ, |
| 676 | ByteCodeInstruction::kCompareFGTEQ, |
| 677 | count); |
| 678 | break; |
| 679 | case Token::Kind::LT: |
| 680 | this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSLT, |
| 681 | ByteCodeInstruction::kCompareULT, |
| 682 | ByteCodeInstruction::kCompareFLT, |
| 683 | count); |
| 684 | break; |
| 685 | case Token::Kind::LTEQ: |
| 686 | this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareSLTEQ, |
| 687 | ByteCodeInstruction::kCompareULTEQ, |
| 688 | ByteCodeInstruction::kCompareFLTEQ, |
| 689 | count); |
| 690 | break; |
| 691 | case Token::Kind::MINUS: |
| 692 | this->writeTypedInstruction(lType, ByteCodeInstruction::kSubtractI, |
| 693 | ByteCodeInstruction::kSubtractI, |
| 694 | ByteCodeInstruction::kSubtractF, |
| 695 | count); |
| 696 | break; |
| 697 | case Token::Kind::NEQ: |
| 698 | this->writeTypedInstruction(lType, ByteCodeInstruction::kCompareINEQ, |
| 699 | ByteCodeInstruction::kCompareINEQ, |
| 700 | ByteCodeInstruction::kCompareFNEQ, |
| 701 | count); |
| 702 | // Collapse to a single bool |
| 703 | for (int i = count; i > 1; --i) { |
| 704 | this->write(ByteCodeInstruction::kOrB); |
| 705 | } |
| 706 | break; |
| 707 | case Token::Kind::PERCENT: |
| 708 | this->writeTypedInstruction(lType, ByteCodeInstruction::kRemainderS, |
| 709 | ByteCodeInstruction::kRemainderU, |
| 710 | ByteCodeInstruction::kRemainderF, |
| 711 | count); |
| 712 | break; |
| 713 | case Token::Kind::PLUS: |
| 714 | this->writeTypedInstruction(lType, ByteCodeInstruction::kAddI, |
| 715 | ByteCodeInstruction::kAddI, |
| 716 | ByteCodeInstruction::kAddF, |
| 717 | count); |
| 718 | break; |
| 719 | case Token::Kind::SLASH: |
| 720 | this->writeTypedInstruction(lType, ByteCodeInstruction::kDivideS, |
| 721 | ByteCodeInstruction::kDivideU, |
| 722 | ByteCodeInstruction::kDivideF, |
| 723 | count); |
| 724 | break; |
| 725 | case Token::Kind::STAR: |
| 726 | this->writeTypedInstruction(lType, ByteCodeInstruction::kMultiplyI, |
| 727 | ByteCodeInstruction::kMultiplyI, |
| 728 | ByteCodeInstruction::kMultiplyF, |
| 729 | count); |
| 730 | break; |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 731 | |
| 732 | case Token::Kind::LOGICALAND: |
| 733 | SkASSERT(type_category(lType) == SkSL::TypeCategory::kBool && count == 1); |
| 734 | this->write(ByteCodeInstruction::kAndB); |
| 735 | break; |
| 736 | case Token::Kind::LOGICALNOT: |
| 737 | SkASSERT(type_category(lType) == SkSL::TypeCategory::kBool && count == 1); |
| 738 | this->write(ByteCodeInstruction::kNotB); |
| 739 | break; |
| 740 | case Token::Kind::LOGICALOR: |
| 741 | SkASSERT(type_category(lType) == SkSL::TypeCategory::kBool && count == 1); |
| 742 | this->write(ByteCodeInstruction::kOrB); |
| 743 | break; |
| 744 | case Token::Kind::LOGICALXOR: |
| 745 | SkASSERT(type_category(lType) == SkSL::TypeCategory::kBool && count == 1); |
| 746 | this->write(ByteCodeInstruction::kXorB); |
| 747 | break; |
| 748 | |
Brian Osman | 909231c | 2019-05-29 15:34:36 -0400 | [diff] [blame] | 749 | default: |
| 750 | SkASSERT(false); |
| 751 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 752 | } |
| 753 | if (lvalue) { |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 754 | lvalue->store(discard); |
| 755 | discard = false; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 756 | } |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 757 | return discard; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | void ByteCodeGenerator::writeBoolLiteral(const BoolLiteral& b) { |
| 761 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 762 | this->write32(b.fValue ? ~0 : 0); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | void ByteCodeGenerator::writeConstructor(const Constructor& c) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 766 | for (const auto& arg : c.fArguments) { |
| 767 | this->writeExpression(*arg); |
| 768 | } |
| 769 | if (c.fArguments.size() == 1) { |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 770 | const Type& inType = c.fArguments[0]->fType; |
| 771 | const Type& outType = c.fType; |
| 772 | TypeCategory inCategory = type_category(inType); |
| 773 | TypeCategory outCategory = type_category(outType); |
| 774 | int inCount = SlotCount(inType); |
| 775 | int outCount = SlotCount(outType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 776 | if (inCategory != outCategory) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 777 | SkASSERT(inCount == outCount); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 778 | if (inCategory == TypeCategory::kFloat) { |
| 779 | SkASSERT(outCategory == TypeCategory::kSigned || |
| 780 | outCategory == TypeCategory::kUnsigned); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 781 | this->write(vector_instruction(ByteCodeInstruction::kConvertFtoI, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 782 | } else if (outCategory == TypeCategory::kFloat) { |
| 783 | if (inCategory == TypeCategory::kSigned) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 784 | this->write(vector_instruction(ByteCodeInstruction::kConvertStoF, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 785 | } else { |
| 786 | SkASSERT(inCategory == TypeCategory::kUnsigned); |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 787 | this->write(vector_instruction(ByteCodeInstruction::kConvertUtoF, outCount)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 788 | } |
| 789 | } else { |
| 790 | SkASSERT(false); |
| 791 | } |
| 792 | } |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 793 | if (inType.kind() == Type::kMatrix_Kind && outType.kind() == Type::kMatrix_Kind) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 794 | this->write(ByteCodeInstruction::kMatrixToMatrix, |
| 795 | SlotCount(outType) - SlotCount(inType)); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 796 | this->write8(inType.columns()); |
| 797 | this->write8(inType.rows()); |
| 798 | this->write8(outType.columns()); |
| 799 | this->write8(outType.rows()); |
| 800 | } else if (inCount != outCount) { |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 801 | SkASSERT(inCount == 1); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 802 | if (outType.kind() == Type::kMatrix_Kind) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 803 | this->write(ByteCodeInstruction::kScalarToMatrix, SlotCount(outType) - 1); |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame] | 804 | this->write8(outType.columns()); |
| 805 | this->write8(outType.rows()); |
| 806 | } else { |
| 807 | SkASSERT(outType.kind() == Type::kVector_Kind); |
| 808 | for (; inCount != outCount; ++inCount) { |
| 809 | this->write(ByteCodeInstruction::kDup); |
| 810 | } |
Brian Osman | c51d791 | 2019-05-22 15:16:16 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 813 | } |
| 814 | } |
| 815 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 816 | void ByteCodeGenerator::writeExternalFunctionCall(const ExternalFunctionCall& f) { |
| 817 | int argumentCount = 0; |
| 818 | for (const auto& arg : f.fArguments) { |
| 819 | this->writeExpression(*arg); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 820 | argumentCount += SlotCount(arg->fType); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 821 | } |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 822 | this->write(ByteCodeInstruction::kCallExternal, SlotCount(f.fType) - argumentCount); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 823 | SkASSERT(argumentCount <= 255); |
| 824 | this->write8(argumentCount); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 825 | this->write8(SlotCount(f.fType)); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 826 | int index = fOutput->fExternalValues.size(); |
| 827 | fOutput->fExternalValues.push_back(f.fFunction); |
| 828 | SkASSERT(index <= 255); |
| 829 | this->write8(index); |
| 830 | } |
| 831 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 832 | void ByteCodeGenerator::writeExternalValue(const ExternalValueReference& e) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 833 | this->write(vector_instruction(ByteCodeInstruction::kReadExternal, |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 834 | SlotCount(e.fValue->type()))); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 835 | int index = fOutput->fExternalValues.size(); |
| 836 | fOutput->fExternalValues.push_back(e.fValue); |
| 837 | SkASSERT(index <= 255); |
| 838 | this->write8(index); |
| 839 | } |
| 840 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 841 | void ByteCodeGenerator::writeVariableExpression(const Expression& expr) { |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 842 | Variable::Storage storage = Variable::kLocal_Storage; |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 843 | int location = this->getLocation(expr, &storage); |
| 844 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 845 | int count = SlotCount(expr.fType); |
| 846 | if (location < 0 || count > 4) { |
| 847 | if (location >= 0) { |
| 848 | this->write(ByteCodeInstruction::kPushImmediate); |
| 849 | this->write32(location); |
| 850 | } |
| 851 | this->write(isGlobal ? ByteCodeInstruction::kLoadExtendedGlobal |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 852 | : ByteCodeInstruction::kLoadExtended, |
| 853 | count); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 854 | this->write8(count); |
| 855 | } else { |
| 856 | this->write(vector_instruction(isGlobal ? ByteCodeInstruction::kLoadGlobal |
| 857 | : ByteCodeInstruction::kLoad, |
| 858 | count)); |
| 859 | this->write8(location); |
| 860 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 861 | } |
| 862 | |
Brian Osman | d30e039 | 2019-06-14 14:05:14 -0400 | [diff] [blame] | 863 | static inline uint32_t float_to_bits(float x) { |
| 864 | uint32_t u; |
| 865 | memcpy(&u, &x, sizeof(uint32_t)); |
| 866 | return u; |
| 867 | } |
| 868 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 869 | void ByteCodeGenerator::writeFloatLiteral(const FloatLiteral& f) { |
| 870 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | d30e039 | 2019-06-14 14:05:14 -0400 | [diff] [blame] | 871 | this->write32(float_to_bits(f.fValue)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 872 | } |
| 873 | |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 874 | void ByteCodeGenerator::writeIntrinsicCall(const FunctionCall& c) { |
| 875 | auto found = fIntrinsics.find(c.fFunction.fName); |
| 876 | if (found == fIntrinsics.end()) { |
| 877 | fErrors.error(c.fOffset, "unsupported intrinsic function"); |
| 878 | return; |
| 879 | } |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 880 | int count = SlotCount(c.fArguments[0]->fType); |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 881 | if (found->second.fIsSpecial) { |
| 882 | SkASSERT(found->second.fValue.fSpecial == SpecialIntrinsic::kDot); |
| 883 | SkASSERT(c.fArguments.size() == 2); |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 884 | SkASSERT(count == SlotCount(c.fArguments[1]->fType)); |
| 885 | this->write((ByteCodeInstruction)((int)ByteCodeInstruction::kMultiplyF + count - 1)); |
| 886 | for (int i = count; i > 1; --i) { |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 887 | this->write(ByteCodeInstruction::kAddF); |
| 888 | } |
| 889 | } else { |
| 890 | switch (found->second.fValue.fInstruction) { |
| 891 | case ByteCodeInstruction::kCos: |
| 892 | case ByteCodeInstruction::kMix: |
| 893 | case ByteCodeInstruction::kSin: |
| 894 | case ByteCodeInstruction::kSqrt: |
| 895 | case ByteCodeInstruction::kTan: |
| 896 | SkASSERT(c.fArguments.size() > 0); |
| 897 | this->write((ByteCodeInstruction) ((int) found->second.fValue.fInstruction + |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 898 | count - 1)); |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 899 | break; |
| 900 | case ByteCodeInstruction::kCross: |
| 901 | this->write(found->second.fValue.fInstruction); |
| 902 | break; |
Mike Reed | 634c941 | 2019-07-18 13:20:04 -0400 | [diff] [blame] | 903 | case ByteCodeInstruction::kInverse2x2: { |
| 904 | SkASSERT(c.fArguments.size() > 0); |
| 905 | auto op = ByteCodeInstruction::kInverse2x2; |
| 906 | switch (count) { |
| 907 | case 4: break; // float2x2 |
| 908 | case 9: op = ByteCodeInstruction::kInverse3x3; break; |
| 909 | case 16: op = ByteCodeInstruction::kInverse4x4; break; |
| 910 | default: SkASSERT(false); |
| 911 | } |
| 912 | this->write(op); |
| 913 | } break; |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 914 | default: |
| 915 | SkASSERT(false); |
| 916 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 917 | } |
| 918 | } |
| 919 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 920 | void ByteCodeGenerator::writeFunctionCall(const FunctionCall& f) { |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 921 | // Builtins have simple signatures... |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 922 | if (f.fFunction.fBuiltin) { |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 923 | for (const auto& arg : f.fArguments) { |
| 924 | this->writeExpression(*arg); |
| 925 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 926 | this->writeIntrinsicCall(f); |
| 927 | return; |
| 928 | } |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 929 | |
Brian Osman | 6f5358f | 2019-07-09 14:17:23 -0400 | [diff] [blame] | 930 | // Find the index of the function we're calling. We explicitly do not allow calls to functions |
| 931 | // before they're defined. This is an easy-to-understand rule that prevents recursion. |
| 932 | size_t idx; |
| 933 | for (idx = 0; idx < fFunctions.size(); ++idx) { |
| 934 | if (f.fFunction.matches(fFunctions[idx]->fDeclaration)) { |
| 935 | break; |
| 936 | } |
| 937 | } |
| 938 | if (idx > 255) { |
| 939 | fErrors.error(f.fOffset, "Function count limit exceeded"); |
| 940 | return; |
| 941 | } else if (idx >= fFunctions.size()) { |
| 942 | fErrors.error(f.fOffset, "Call to undefined function"); |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | // We may need to deal with out parameters, so the sequence is tricky |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 947 | if (int returnCount = SlotCount(f.fType)) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 948 | this->write(ByteCodeInstruction::kReserve, returnCount); |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 949 | this->write8(returnCount); |
| 950 | } |
| 951 | |
| 952 | int argCount = f.fArguments.size(); |
| 953 | std::vector<std::unique_ptr<LValue>> lvalues; |
| 954 | for (int i = 0; i < argCount; ++i) { |
| 955 | const auto& param = f.fFunction.fParameters[i]; |
| 956 | const auto& arg = f.fArguments[i]; |
| 957 | if (param->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 958 | lvalues.emplace_back(this->getLValue(*arg)); |
| 959 | lvalues.back()->load(); |
| 960 | } else { |
| 961 | this->writeExpression(*arg); |
| 962 | } |
| 963 | } |
| 964 | |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 965 | // The space used by the call is based on the callee, but it also unwinds all of that before |
| 966 | // we continue execution. We adjust our max stack depths below. |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 967 | this->write(ByteCodeInstruction::kCall); |
Brian Osman | 6f5358f | 2019-07-09 14:17:23 -0400 | [diff] [blame] | 968 | this->write8(idx); |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 969 | |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 970 | const ByteCodeFunction* callee = fOutput->fFunctions[idx].get(); |
| 971 | fMaxLoopCount = std::max(fMaxLoopCount, fLoopCount + callee->fLoopCount); |
| 972 | fMaxConditionCount = std::max(fMaxConditionCount, fConditionCount + callee->fConditionCount); |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 973 | fMaxStackCount = std::max(fMaxStackCount, fStackCount + callee->fLocalCount |
| 974 | + callee->fStackCount); |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 975 | |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 976 | // After the called function returns, the stack will still contain our arguments. We have to |
| 977 | // pop them (storing any out parameters back to their lvalues as we go). We glob together slot |
| 978 | // counts for all parameters that aren't out-params, so we can pop them in one big chunk. |
| 979 | int popCount = 0; |
| 980 | auto pop = [&]() { |
| 981 | if (popCount > 4) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 982 | this->write(ByteCodeInstruction::kPopN, popCount); |
Brian Osman | d3494ed | 2019-06-20 15:41:34 -0400 | [diff] [blame] | 983 | this->write8(popCount); |
| 984 | } else if (popCount > 0) { |
| 985 | this->write(vector_instruction(ByteCodeInstruction::kPop, popCount)); |
| 986 | } |
| 987 | popCount = 0; |
| 988 | }; |
| 989 | |
| 990 | for (int i = argCount - 1; i >= 0; --i) { |
| 991 | const auto& param = f.fFunction.fParameters[i]; |
| 992 | const auto& arg = f.fArguments[i]; |
| 993 | if (param->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 994 | pop(); |
| 995 | lvalues.back()->store(true); |
| 996 | lvalues.pop_back(); |
| 997 | } else { |
| 998 | popCount += SlotCount(arg->fType); |
| 999 | } |
| 1000 | } |
| 1001 | pop(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1002 | } |
| 1003 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1004 | void ByteCodeGenerator::writeIntLiteral(const IntLiteral& i) { |
| 1005 | this->write(ByteCodeInstruction::kPushImmediate); |
| 1006 | this->write32(i.fValue); |
| 1007 | } |
| 1008 | |
| 1009 | void ByteCodeGenerator::writeNullLiteral(const NullLiteral& n) { |
| 1010 | // not yet implemented |
| 1011 | abort(); |
| 1012 | } |
| 1013 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1014 | bool ByteCodeGenerator::writePrefixExpression(const PrefixExpression& p, bool discard) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1015 | switch (p.fOperator) { |
| 1016 | case Token::Kind::PLUSPLUS: // fall through |
| 1017 | case Token::Kind::MINUSMINUS: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1018 | SkASSERT(SlotCount(p.fOperand->fType) == 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1019 | std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand); |
| 1020 | lvalue->load(); |
| 1021 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | d30e039 | 2019-06-14 14:05:14 -0400 | [diff] [blame] | 1022 | this->write32(type_category(p.fType) == TypeCategory::kFloat ? float_to_bits(1.0f) : 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1023 | if (p.fOperator == Token::Kind::PLUSPLUS) { |
| 1024 | this->writeTypedInstruction(p.fType, |
| 1025 | ByteCodeInstruction::kAddI, |
| 1026 | ByteCodeInstruction::kAddI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 1027 | ByteCodeInstruction::kAddF, |
| 1028 | 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1029 | } else { |
| 1030 | this->writeTypedInstruction(p.fType, |
| 1031 | ByteCodeInstruction::kSubtractI, |
| 1032 | ByteCodeInstruction::kSubtractI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 1033 | ByteCodeInstruction::kSubtractF, |
| 1034 | 1); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1035 | } |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1036 | lvalue->store(discard); |
| 1037 | discard = false; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1038 | break; |
| 1039 | } |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 1040 | case Token::Kind::MINUS: { |
| 1041 | this->writeExpression(*p.fOperand); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1042 | this->writeTypedInstruction(p.fType, |
Mike Klein | 1271091 | 2019-05-21 11:04:59 -0500 | [diff] [blame] | 1043 | ByteCodeInstruction::kNegateI, |
| 1044 | ByteCodeInstruction::kNegateI, |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 1045 | ByteCodeInstruction::kNegateF, |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1046 | SlotCount(p.fOperand->fType)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1047 | break; |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 1048 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1049 | default: |
| 1050 | SkASSERT(false); |
| 1051 | } |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1052 | return discard; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1055 | bool ByteCodeGenerator::writePostfixExpression(const PostfixExpression& p, bool discard) { |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 1056 | switch (p.fOperator) { |
| 1057 | case Token::Kind::PLUSPLUS: // fall through |
| 1058 | case Token::Kind::MINUSMINUS: { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1059 | SkASSERT(SlotCount(p.fOperand->fType) == 1); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 1060 | std::unique_ptr<LValue> lvalue = this->getLValue(*p.fOperand); |
| 1061 | lvalue->load(); |
Brian Osman | 52c1bf1 | 2019-07-18 13:12:19 -0400 | [diff] [blame] | 1062 | // If we're not supposed to discard the result, then make a copy *before* the +/- |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1063 | if (!discard) { |
| 1064 | this->write(ByteCodeInstruction::kDup); |
| 1065 | } |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 1066 | this->write(ByteCodeInstruction::kPushImmediate); |
Brian Osman | d30e039 | 2019-06-14 14:05:14 -0400 | [diff] [blame] | 1067 | this->write32(type_category(p.fType) == TypeCategory::kFloat ? float_to_bits(1.0f) : 1); |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 1068 | if (p.fOperator == Token::Kind::PLUSPLUS) { |
| 1069 | this->writeTypedInstruction(p.fType, |
| 1070 | ByteCodeInstruction::kAddI, |
| 1071 | ByteCodeInstruction::kAddI, |
| 1072 | ByteCodeInstruction::kAddF, |
| 1073 | 1); |
| 1074 | } else { |
| 1075 | this->writeTypedInstruction(p.fType, |
| 1076 | ByteCodeInstruction::kSubtractI, |
| 1077 | ByteCodeInstruction::kSubtractI, |
| 1078 | ByteCodeInstruction::kSubtractF, |
| 1079 | 1); |
| 1080 | } |
Brian Osman | 52c1bf1 | 2019-07-18 13:12:19 -0400 | [diff] [blame] | 1081 | // Always consume the result as part of the store |
| 1082 | lvalue->store(true); |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1083 | discard = false; |
Brian Osman | f3fa600 | 2019-05-17 14:26:53 -0400 | [diff] [blame] | 1084 | break; |
| 1085 | } |
| 1086 | default: |
| 1087 | SkASSERT(false); |
| 1088 | } |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1089 | return discard; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | void ByteCodeGenerator::writeSwizzle(const Swizzle& s) { |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame] | 1093 | if (swizzle_is_simple(s)) { |
| 1094 | this->writeVariableExpression(s); |
| 1095 | return; |
| 1096 | } |
| 1097 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1098 | switch (s.fBase->fKind) { |
| 1099 | case Expression::kVariableReference_Kind: { |
| 1100 | const Variable& var = ((VariableReference&) *s.fBase).fVariable; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 1101 | this->write(var.fStorage == Variable::kGlobal_Storage |
| 1102 | ? ByteCodeInstruction::kLoadSwizzleGlobal |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1103 | : ByteCodeInstruction::kLoadSwizzle, |
| 1104 | s.fComponents.size()); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 1105 | this->write8(this->getLocation(var)); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1106 | this->write8(s.fComponents.size()); |
| 1107 | for (int c : s.fComponents) { |
| 1108 | this->write8(c); |
| 1109 | } |
| 1110 | break; |
| 1111 | } |
| 1112 | default: |
| 1113 | this->writeExpression(*s.fBase); |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1114 | this->write(ByteCodeInstruction::kSwizzle, |
| 1115 | s.fComponents.size() - s.fBase->fType.columns()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1116 | this->write8(s.fBase->fType.columns()); |
| 1117 | this->write8(s.fComponents.size()); |
| 1118 | for (int c : s.fComponents) { |
| 1119 | this->write8(c); |
| 1120 | } |
| 1121 | } |
| 1122 | } |
| 1123 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1124 | void ByteCodeGenerator::writeTernaryExpression(const TernaryExpression& t) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1125 | int count = SlotCount(t.fType); |
| 1126 | SkASSERT(count == SlotCount(t.fIfTrue->fType)); |
| 1127 | SkASSERT(count == SlotCount(t.fIfFalse->fType)); |
| 1128 | |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 1129 | this->writeExpression(*t.fTest); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1130 | this->write(ByteCodeInstruction::kMaskPush); |
Brian Osman | 4e93feb | 2019-05-16 15:38:00 -0400 | [diff] [blame] | 1131 | this->writeExpression(*t.fIfTrue); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1132 | this->write(ByteCodeInstruction::kMaskNegate); |
| 1133 | this->writeExpression(*t.fIfFalse); |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1134 | this->write(ByteCodeInstruction::kMaskBlend, count); |
| 1135 | this->write8(count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1136 | } |
| 1137 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1138 | void ByteCodeGenerator::writeExpression(const Expression& e, bool discard) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1139 | switch (e.fKind) { |
| 1140 | case Expression::kBinary_Kind: |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1141 | discard = this->writeBinaryExpression((BinaryExpression&) e, discard); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1142 | break; |
| 1143 | case Expression::kBoolLiteral_Kind: |
| 1144 | this->writeBoolLiteral((BoolLiteral&) e); |
| 1145 | break; |
| 1146 | case Expression::kConstructor_Kind: |
| 1147 | this->writeConstructor((Constructor&) e); |
| 1148 | break; |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 1149 | case Expression::kExternalFunctionCall_Kind: |
| 1150 | this->writeExternalFunctionCall((ExternalFunctionCall&) e); |
| 1151 | break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1152 | case Expression::kExternalValue_Kind: |
| 1153 | this->writeExternalValue((ExternalValueReference&) e); |
| 1154 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1155 | case Expression::kFieldAccess_Kind: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1156 | case Expression::kIndex_Kind: |
| 1157 | case Expression::kVariableReference_Kind: |
| 1158 | this->writeVariableExpression(e); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1159 | break; |
| 1160 | case Expression::kFloatLiteral_Kind: |
| 1161 | this->writeFloatLiteral((FloatLiteral&) e); |
| 1162 | break; |
| 1163 | case Expression::kFunctionCall_Kind: |
| 1164 | this->writeFunctionCall((FunctionCall&) e); |
| 1165 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1166 | case Expression::kIntLiteral_Kind: |
| 1167 | this->writeIntLiteral((IntLiteral&) e); |
| 1168 | break; |
| 1169 | case Expression::kNullLiteral_Kind: |
| 1170 | this->writeNullLiteral((NullLiteral&) e); |
| 1171 | break; |
| 1172 | case Expression::kPrefix_Kind: |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1173 | discard = this->writePrefixExpression((PrefixExpression&) e, discard); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1174 | break; |
| 1175 | case Expression::kPostfix_Kind: |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1176 | discard = this->writePostfixExpression((PostfixExpression&) e, discard); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1177 | break; |
| 1178 | case Expression::kSwizzle_Kind: |
| 1179 | this->writeSwizzle((Swizzle&) e); |
| 1180 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1181 | case Expression::kTernary_Kind: |
| 1182 | this->writeTernaryExpression((TernaryExpression&) e); |
| 1183 | break; |
| 1184 | default: |
| 1185 | printf("unsupported expression %s\n", e.description().c_str()); |
| 1186 | SkASSERT(false); |
| 1187 | } |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1188 | if (discard) { |
| 1189 | int count = SlotCount(e.fType); |
| 1190 | if (count > 4) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1191 | this->write(ByteCodeInstruction::kPopN, count); |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1192 | this->write8(count); |
Brian Osman | fba386b | 2019-06-20 14:54:15 -0400 | [diff] [blame] | 1193 | } else if (count != 0) { |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1194 | this->write(vector_instruction(ByteCodeInstruction::kPop, count)); |
| 1195 | } |
| 1196 | discard = false; |
| 1197 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1198 | } |
| 1199 | |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1200 | class ByteCodeExternalValueLValue : public ByteCodeGenerator::LValue { |
| 1201 | public: |
| 1202 | ByteCodeExternalValueLValue(ByteCodeGenerator* generator, ExternalValue& value, int index) |
| 1203 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1204 | , fCount(ByteCodeGenerator::SlotCount(value.type())) |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1205 | , fIndex(index) {} |
| 1206 | |
| 1207 | void load() override { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 1208 | fGenerator.write(vector_instruction(ByteCodeInstruction::kReadExternal, fCount)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1209 | fGenerator.write8(fIndex); |
| 1210 | } |
| 1211 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1212 | void store(bool discard) override { |
| 1213 | if (!discard) { |
| 1214 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, fCount)); |
| 1215 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 1216 | fGenerator.write(vector_instruction(ByteCodeInstruction::kWriteExternal, fCount)); |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1217 | fGenerator.write8(fIndex); |
| 1218 | } |
| 1219 | |
| 1220 | private: |
| 1221 | typedef LValue INHERITED; |
| 1222 | |
| 1223 | int fCount; |
| 1224 | |
| 1225 | int fIndex; |
| 1226 | }; |
| 1227 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1228 | class ByteCodeSwizzleLValue : public ByteCodeGenerator::LValue { |
| 1229 | public: |
| 1230 | ByteCodeSwizzleLValue(ByteCodeGenerator* generator, const Swizzle& swizzle) |
| 1231 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1232 | , fSwizzle(swizzle) {} |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1233 | |
| 1234 | void load() override { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 1235 | fGenerator.writeSwizzle(fSwizzle); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1236 | } |
| 1237 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1238 | void store(bool discard) override { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1239 | int count = fSwizzle.fComponents.size(); |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1240 | if (!discard) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1241 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count)); |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1242 | } |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 1243 | Variable::Storage storage = Variable::kLocal_Storage; |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1244 | int location = fGenerator.getLocation(*fSwizzle.fBase, &storage); |
| 1245 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 1246 | if (location < 0) { |
| 1247 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleIndirectGlobal |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1248 | : ByteCodeInstruction::kStoreSwizzleIndirect, |
| 1249 | count); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1250 | } else { |
| 1251 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreSwizzleGlobal |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1252 | : ByteCodeInstruction::kStoreSwizzle, |
| 1253 | count); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1254 | fGenerator.write8(location); |
| 1255 | } |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1256 | fGenerator.write8(count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1257 | for (int c : fSwizzle.fComponents) { |
| 1258 | fGenerator.write8(c); |
| 1259 | } |
| 1260 | } |
| 1261 | |
| 1262 | private: |
| 1263 | const Swizzle& fSwizzle; |
| 1264 | |
| 1265 | typedef LValue INHERITED; |
| 1266 | }; |
| 1267 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1268 | class ByteCodeExpressionLValue : public ByteCodeGenerator::LValue { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1269 | public: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1270 | ByteCodeExpressionLValue(ByteCodeGenerator* generator, const Expression& expr) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1271 | : INHERITED(*generator) |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1272 | , fExpression(expr) {} |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1273 | |
| 1274 | void load() override { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1275 | fGenerator.writeVariableExpression(fExpression); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 1276 | } |
| 1277 | |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1278 | void store(bool discard) override { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1279 | int count = ByteCodeGenerator::SlotCount(fExpression.fType); |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1280 | if (!discard) { |
| 1281 | if (count > 4) { |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1282 | fGenerator.write(ByteCodeInstruction::kDupN, count); |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1283 | fGenerator.write8(count); |
| 1284 | } else { |
| 1285 | fGenerator.write(vector_instruction(ByteCodeInstruction::kDup, count)); |
| 1286 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1287 | } |
Brian Osman | 869a3e8 | 2019-07-18 17:00:34 -0400 | [diff] [blame] | 1288 | Variable::Storage storage = Variable::kLocal_Storage; |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1289 | int location = fGenerator.getLocation(fExpression, &storage); |
| 1290 | bool isGlobal = storage == Variable::kGlobal_Storage; |
| 1291 | if (location < 0 || count > 4) { |
| 1292 | if (location >= 0) { |
| 1293 | fGenerator.write(ByteCodeInstruction::kPushImmediate); |
| 1294 | fGenerator.write32(location); |
| 1295 | } |
| 1296 | fGenerator.write(isGlobal ? ByteCodeInstruction::kStoreExtendedGlobal |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1297 | : ByteCodeInstruction::kStoreExtended, |
| 1298 | count); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1299 | fGenerator.write8(count); |
| 1300 | } else { |
| 1301 | fGenerator.write(vector_instruction(isGlobal ? ByteCodeInstruction::kStoreGlobal |
| 1302 | : ByteCodeInstruction::kStore, |
| 1303 | count)); |
| 1304 | fGenerator.write8(location); |
| 1305 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | private: |
| 1309 | typedef LValue INHERITED; |
| 1310 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1311 | const Expression& fExpression; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1312 | }; |
| 1313 | |
| 1314 | std::unique_ptr<ByteCodeGenerator::LValue> ByteCodeGenerator::getLValue(const Expression& e) { |
| 1315 | switch (e.fKind) { |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 1316 | case Expression::kExternalValue_Kind: { |
| 1317 | ExternalValue* value = ((ExternalValueReference&) e).fValue; |
| 1318 | int index = fOutput->fExternalValues.size(); |
| 1319 | fOutput->fExternalValues.push_back(value); |
| 1320 | SkASSERT(index <= 255); |
| 1321 | return std::unique_ptr<LValue>(new ByteCodeExternalValueLValue(this, *value, index)); |
| 1322 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1323 | case Expression::kFieldAccess_Kind: |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1324 | case Expression::kIndex_Kind: |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1325 | case Expression::kVariableReference_Kind: |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1326 | return std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e)); |
Brian Osman | 0785db0 | 2019-05-24 14:19:11 -0400 | [diff] [blame] | 1327 | case Expression::kSwizzle_Kind: { |
| 1328 | const Swizzle& s = (const Swizzle&) e; |
| 1329 | return swizzle_is_simple(s) |
| 1330 | ? std::unique_ptr<LValue>(new ByteCodeExpressionLValue(this, e)) |
| 1331 | : std::unique_ptr<LValue>(new ByteCodeSwizzleLValue(this, s)); |
| 1332 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1333 | case Expression::kTernary_Kind: |
| 1334 | default: |
| 1335 | printf("unsupported lvalue %s\n", e.description().c_str()); |
| 1336 | return nullptr; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | void ByteCodeGenerator::writeBlock(const Block& b) { |
| 1341 | for (const auto& s : b.fStatements) { |
| 1342 | this->writeStatement(*s); |
| 1343 | } |
| 1344 | } |
| 1345 | |
| 1346 | void ByteCodeGenerator::setBreakTargets() { |
| 1347 | std::vector<DeferredLocation>& breaks = fBreakTargets.top(); |
| 1348 | for (DeferredLocation& b : breaks) { |
| 1349 | b.set(); |
| 1350 | } |
| 1351 | fBreakTargets.pop(); |
| 1352 | } |
| 1353 | |
| 1354 | void ByteCodeGenerator::setContinueTargets() { |
| 1355 | std::vector<DeferredLocation>& continues = fContinueTargets.top(); |
| 1356 | for (DeferredLocation& c : continues) { |
| 1357 | c.set(); |
| 1358 | } |
| 1359 | fContinueTargets.pop(); |
| 1360 | } |
| 1361 | |
| 1362 | void ByteCodeGenerator::writeBreakStatement(const BreakStatement& b) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1363 | // TODO: Include BranchIfAllFalse to top-most LoopNext |
| 1364 | this->write(ByteCodeInstruction::kLoopBreak); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1365 | } |
| 1366 | |
| 1367 | void ByteCodeGenerator::writeContinueStatement(const ContinueStatement& c) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1368 | // TODO: Include BranchIfAllFalse to top-most LoopNext |
| 1369 | this->write(ByteCodeInstruction::kLoopContinue); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1370 | } |
| 1371 | |
| 1372 | void ByteCodeGenerator::writeDoStatement(const DoStatement& d) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1373 | this->write(ByteCodeInstruction::kLoopBegin); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1374 | size_t start = fCode->size(); |
| 1375 | this->writeStatement(*d.fStatement); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1376 | this->write(ByteCodeInstruction::kLoopNext); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1377 | this->writeExpression(*d.fTest); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1378 | this->write(ByteCodeInstruction::kLoopMask); |
| 1379 | // TODO: Could shorten this with kBranchIfAnyTrue |
| 1380 | this->write(ByteCodeInstruction::kBranchIfAllFalse); |
| 1381 | DeferredLocation endLocation(this); |
| 1382 | this->write(ByteCodeInstruction::kBranch); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1383 | this->write16(start); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1384 | endLocation.set(); |
| 1385 | this->write(ByteCodeInstruction::kLoopEnd); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | void ByteCodeGenerator::writeForStatement(const ForStatement& f) { |
| 1389 | fContinueTargets.emplace(); |
| 1390 | fBreakTargets.emplace(); |
| 1391 | if (f.fInitializer) { |
| 1392 | this->writeStatement(*f.fInitializer); |
| 1393 | } |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1394 | this->write(ByteCodeInstruction::kLoopBegin); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1395 | size_t start = fCode->size(); |
| 1396 | if (f.fTest) { |
| 1397 | this->writeExpression(*f.fTest); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1398 | this->write(ByteCodeInstruction::kLoopMask); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1399 | } |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1400 | this->write(ByteCodeInstruction::kBranchIfAllFalse); |
| 1401 | DeferredLocation endLocation(this); |
| 1402 | this->writeStatement(*f.fStatement); |
| 1403 | this->write(ByteCodeInstruction::kLoopNext); |
| 1404 | if (f.fNext) { |
| 1405 | this->writeExpression(*f.fNext, true); |
| 1406 | } |
| 1407 | this->write(ByteCodeInstruction::kBranch); |
| 1408 | this->write16(start); |
| 1409 | endLocation.set(); |
| 1410 | this->write(ByteCodeInstruction::kLoopEnd); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | void ByteCodeGenerator::writeIfStatement(const IfStatement& i) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1414 | this->writeExpression(*i.fTest); |
| 1415 | this->write(ByteCodeInstruction::kMaskPush); |
| 1416 | this->write(ByteCodeInstruction::kBranchIfAllFalse); |
| 1417 | DeferredLocation falseLocation(this); |
| 1418 | this->writeStatement(*i.fIfTrue); |
| 1419 | falseLocation.set(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1420 | if (i.fIfFalse) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1421 | this->write(ByteCodeInstruction::kMaskNegate); |
| 1422 | this->write(ByteCodeInstruction::kBranchIfAllFalse); |
| 1423 | DeferredLocation endLocation(this); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1424 | this->writeStatement(*i.fIfFalse); |
Mike Klein | b45ee83 | 2019-05-17 11:11:11 -0500 | [diff] [blame] | 1425 | endLocation.set(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1426 | } |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1427 | this->write(ByteCodeInstruction::kMaskPop); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | void ByteCodeGenerator::writeReturnStatement(const ReturnStatement& r) { |
Brian Osman | 4a47da7 | 2019-07-12 11:30:32 -0400 | [diff] [blame] | 1431 | if (fLoopCount || fConditionCount) { |
| 1432 | fErrors.error(r.fOffset, "return not allowed inside conditional or loop"); |
| 1433 | return; |
| 1434 | } |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1435 | int count = SlotCount(r.fExpression->fType); |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 1436 | this->writeExpression(*r.fExpression); |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1437 | |
| 1438 | // Technically, the kReturn also pops fOutput->fLocalCount values from the stack, too, but we |
| 1439 | // haven't counted pushing those (they're outside the scope of our stack tracking). Instead, |
| 1440 | // we account for those in writeFunction(). |
| 1441 | |
| 1442 | // This is all fine because we don't allow conditional returns, so we only return once anyway. |
| 1443 | this->write(ByteCodeInstruction::kReturn, -count); |
| 1444 | this->write8(count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | void ByteCodeGenerator::writeSwitchStatement(const SwitchStatement& r) { |
| 1448 | // not yet implemented |
| 1449 | abort(); |
| 1450 | } |
| 1451 | |
| 1452 | void ByteCodeGenerator::writeVarDeclarations(const VarDeclarations& v) { |
| 1453 | for (const auto& declStatement : v.fVars) { |
| 1454 | const VarDeclaration& decl = (VarDeclaration&) *declStatement; |
| 1455 | // we need to grab the location even if we don't use it, to ensure it |
| 1456 | // has been allocated |
| 1457 | int location = getLocation(*decl.fVar); |
| 1458 | if (decl.fValue) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1459 | this->writeExpression(*decl.fValue); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1460 | int count = SlotCount(decl.fValue->fType); |
| 1461 | if (count > 4) { |
| 1462 | this->write(ByteCodeInstruction::kPushImmediate); |
| 1463 | this->write32(location); |
Brian Osman | aa2ca3f | 2019-07-15 13:24:48 -0400 | [diff] [blame] | 1464 | this->write(ByteCodeInstruction::kStoreExtended, count); |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 1465 | this->write8(count); |
| 1466 | } else { |
| 1467 | this->write(vector_instruction(ByteCodeInstruction::kStore, count)); |
| 1468 | this->write8(location); |
| 1469 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | void ByteCodeGenerator::writeWhileStatement(const WhileStatement& w) { |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1475 | this->write(ByteCodeInstruction::kLoopBegin); |
| 1476 | size_t cond = fCode->size(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1477 | this->writeExpression(*w.fTest); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1478 | this->write(ByteCodeInstruction::kLoopMask); |
| 1479 | this->write(ByteCodeInstruction::kBranchIfAllFalse); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1480 | DeferredLocation endLocation(this); |
| 1481 | this->writeStatement(*w.fStatement); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1482 | this->write(ByteCodeInstruction::kLoopNext); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1483 | this->write(ByteCodeInstruction::kBranch); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1484 | this->write16(cond); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1485 | endLocation.set(); |
Brian Osman | 569f12f | 2019-06-13 11:23:57 -0400 | [diff] [blame] | 1486 | this->write(ByteCodeInstruction::kLoopEnd); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | void ByteCodeGenerator::writeStatement(const Statement& s) { |
| 1490 | switch (s.fKind) { |
| 1491 | case Statement::kBlock_Kind: |
| 1492 | this->writeBlock((Block&) s); |
| 1493 | break; |
| 1494 | case Statement::kBreak_Kind: |
| 1495 | this->writeBreakStatement((BreakStatement&) s); |
| 1496 | break; |
| 1497 | case Statement::kContinue_Kind: |
| 1498 | this->writeContinueStatement((ContinueStatement&) s); |
| 1499 | break; |
| 1500 | case Statement::kDiscard_Kind: |
| 1501 | // not yet implemented |
| 1502 | abort(); |
| 1503 | case Statement::kDo_Kind: |
| 1504 | this->writeDoStatement((DoStatement&) s); |
| 1505 | break; |
Brian Osman | 3e29f1d | 2019-05-28 09:35:05 -0400 | [diff] [blame] | 1506 | case Statement::kExpression_Kind: |
| 1507 | this->writeExpression(*((ExpressionStatement&) s).fExpression, true); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1508 | break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1509 | case Statement::kFor_Kind: |
| 1510 | this->writeForStatement((ForStatement&) s); |
| 1511 | break; |
| 1512 | case Statement::kIf_Kind: |
| 1513 | this->writeIfStatement((IfStatement&) s); |
| 1514 | break; |
| 1515 | case Statement::kNop_Kind: |
| 1516 | break; |
| 1517 | case Statement::kReturn_Kind: |
| 1518 | this->writeReturnStatement((ReturnStatement&) s); |
| 1519 | break; |
| 1520 | case Statement::kSwitch_Kind: |
| 1521 | this->writeSwitchStatement((SwitchStatement&) s); |
| 1522 | break; |
| 1523 | case Statement::kVarDeclarations_Kind: |
| 1524 | this->writeVarDeclarations(*((VarDeclarationsStatement&) s).fDeclaration); |
| 1525 | break; |
| 1526 | case Statement::kWhile_Kind: |
| 1527 | this->writeWhileStatement((WhileStatement&) s); |
| 1528 | break; |
| 1529 | default: |
| 1530 | SkASSERT(false); |
| 1531 | } |
| 1532 | } |
| 1533 | |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 1534 | ByteCodeFunction::ByteCodeFunction(const FunctionDeclaration* declaration) |
| 1535 | : fName(declaration->fName) { |
| 1536 | fParameterCount = 0; |
| 1537 | for (const auto& p : declaration->fParameters) { |
| 1538 | int slots = ByteCodeGenerator::SlotCount(p->fType); |
| 1539 | fParameters.push_back({ slots, (bool)(p->fModifiers.fFlags & Modifiers::kOut_Flag) }); |
| 1540 | fParameterCount += slots; |
| 1541 | } |
| 1542 | } |
| 1543 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 1544 | } |