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