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