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