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