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