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