Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. |
| 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 | |
| 8 | #ifndef SKSL_STANDALONE |
| 9 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 10 | #include "include/core/SkPoint3.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "src/core/SkRasterPipeline.h" |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 12 | #include "src/sksl/SkSLByteCodeGenerator.h" |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 13 | #include "src/sksl/SkSLExternalValue.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/sksl/SkSLInterpreter.h" |
| 15 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 16 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 17 | #include "src/sksl/ir/SkSLForStatement.h" |
| 18 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 19 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 20 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 21 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 22 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 23 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 24 | #include "src/sksl/ir/SkSLProgram.h" |
| 25 | #include "src/sksl/ir/SkSLStatement.h" |
| 26 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 27 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 28 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 29 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 30 | |
| 31 | namespace SkSL { |
| 32 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 33 | static constexpr int UNINITIALIZED = 0xDEADBEEF; |
| 34 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 35 | Interpreter::Interpreter(std::unique_ptr<Program> program, |
| 36 | std::unique_ptr<ByteCode> byteCode, |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 37 | Interpreter::Value inputs[]) |
| 38 | : fProgram(std::move(program)) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 39 | , fByteCode(std::move(byteCode)) |
| 40 | , fGlobals(fByteCode->fGlobalCount, UNINITIALIZED) { |
Brian Osman | d369a5e | 2019-05-09 13:13:25 -0400 | [diff] [blame] | 41 | this->setInputs(inputs); |
| 42 | } |
| 43 | |
| 44 | void Interpreter::setInputs(Interpreter::Value inputs[]) { |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 45 | for (uint8_t slot : fByteCode->fInputSlots) { |
| 46 | fGlobals[slot] = *inputs++; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | void Interpreter::run(const ByteCodeFunction& f, Interpreter::Value args[], |
| 51 | Interpreter::Value* outReturn) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 52 | #ifdef TRACE |
| 53 | this->disassemble(f); |
| 54 | #endif |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 55 | Value smallStack[128]; |
| 56 | std::unique_ptr<Value[]> largeStack; |
| 57 | Value* stack = smallStack; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 58 | if ((int) SK_ARRAY_COUNT(smallStack) < f.fStackCount) { |
| 59 | largeStack.reset(new Value[f.fStackCount]); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 60 | stack = largeStack.get(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 61 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 62 | |
| 63 | if (f.fParameterCount) { |
| 64 | memcpy(stack, args, f.fParameterCount * sizeof(Value)); |
| 65 | } |
| 66 | this->innerRun(f, stack, outReturn); |
| 67 | |
| 68 | for (const Variable* p : f.fDeclaration.fParameters) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 69 | const int nvalues = ByteCodeGenerator::SlotCount(p->fType); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 70 | if (p->fModifiers.fFlags & Modifiers::kOut_Flag) { |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 71 | memcpy(args, stack, nvalues * sizeof(Value)); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 72 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 73 | args += nvalues; |
| 74 | stack += nvalues; |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 75 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 78 | template <typename T> |
| 79 | static T unaligned_load(const void* ptr) { |
| 80 | T val; |
| 81 | memcpy(&val, ptr, sizeof(val)); |
| 82 | return val; |
| 83 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 84 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 85 | #define READ8() (*(ip++)) |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 86 | #define READ16() (ip += 2, unaligned_load<uint16_t>(ip - 2)) |
| 87 | #define READ32() (ip += 4, unaligned_load<uint32_t>(ip - 4)) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 88 | |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 89 | #define VECTOR_DISASSEMBLE(op, text) \ |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 90 | case ByteCodeInstruction::op: printf(text); break; \ |
| 91 | case ByteCodeInstruction::op##2: printf(text "2"); break; \ |
| 92 | case ByteCodeInstruction::op##3: printf(text "3"); break; \ |
| 93 | case ByteCodeInstruction::op##4: printf(text "4"); break; |
| 94 | |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 95 | static const uint8_t* disassemble_instruction(const uint8_t* ip) { |
| 96 | switch ((ByteCodeInstruction) READ16()) { |
| 97 | VECTOR_DISASSEMBLE(kAddF, "addf") |
| 98 | VECTOR_DISASSEMBLE(kAddI, "addi") |
| 99 | case ByteCodeInstruction::kAndB: printf("andb"); break; |
| 100 | case ByteCodeInstruction::kAndI: printf("andi"); break; |
| 101 | case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break; |
| 102 | case ByteCodeInstruction::kCall: printf("call %d", READ8()); break; |
| 103 | case ByteCodeInstruction::kCallExternal: { |
| 104 | int argumentCount = READ8(); |
| 105 | int returnCount = READ8(); |
| 106 | int externalValue = READ8(); |
| 107 | printf("callexternal %d, %d, %d", argumentCount, returnCount, externalValue); |
| 108 | break; |
| 109 | } |
| 110 | VECTOR_DISASSEMBLE(kCompareIEQ, "compareieq") |
| 111 | VECTOR_DISASSEMBLE(kCompareINEQ, "compareineq") |
| 112 | VECTOR_DISASSEMBLE(kCompareFEQ, "comparefeq") |
| 113 | VECTOR_DISASSEMBLE(kCompareFNEQ, "comparefneq") |
| 114 | VECTOR_DISASSEMBLE(kCompareFGT, "comparefgt") |
| 115 | VECTOR_DISASSEMBLE(kCompareFGTEQ, "comparefgteq") |
| 116 | VECTOR_DISASSEMBLE(kCompareFLT, "compareflt") |
| 117 | VECTOR_DISASSEMBLE(kCompareFLTEQ, "compareflteq") |
| 118 | VECTOR_DISASSEMBLE(kCompareSGT, "comparesgt") |
| 119 | VECTOR_DISASSEMBLE(kCompareSGTEQ, "comparesgteq") |
| 120 | VECTOR_DISASSEMBLE(kCompareSLT, "compareslt") |
| 121 | VECTOR_DISASSEMBLE(kCompareSLTEQ, "compareslteq") |
| 122 | VECTOR_DISASSEMBLE(kCompareUGT, "compareugt") |
| 123 | VECTOR_DISASSEMBLE(kCompareUGTEQ, "compareugteq") |
| 124 | VECTOR_DISASSEMBLE(kCompareULT, "compareult") |
| 125 | VECTOR_DISASSEMBLE(kCompareULTEQ, "compareulteq") |
| 126 | case ByteCodeInstruction::kConditionalBranch: |
| 127 | printf("conditionalbranch %d", READ16()); |
| 128 | break; |
| 129 | VECTOR_DISASSEMBLE(kConvertFtoI, "convertftoi") |
| 130 | VECTOR_DISASSEMBLE(kConvertStoF, "convertstof") |
| 131 | VECTOR_DISASSEMBLE(kConvertUtoF, "convertutof") |
| 132 | VECTOR_DISASSEMBLE(kCos, "cos") |
| 133 | case ByteCodeInstruction::kDebugPrint: printf("debugprint"); break; |
| 134 | VECTOR_DISASSEMBLE(kDivideF, "dividef") |
| 135 | VECTOR_DISASSEMBLE(kDivideS, "divideS") |
| 136 | VECTOR_DISASSEMBLE(kDivideU, "divideu") |
| 137 | VECTOR_DISASSEMBLE(kDup, "dup") |
| 138 | case ByteCodeInstruction::kDupN: printf("dupN %d", READ8()); break; |
| 139 | case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break; |
| 140 | case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break; |
| 141 | case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break; |
| 142 | case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break; |
| 143 | case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break; |
| 144 | case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break; |
| 145 | case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break; |
| 146 | case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break; |
| 147 | case ByteCodeInstruction::kLoadSwizzle: { |
| 148 | int target = READ8(); |
| 149 | int count = READ8(); |
| 150 | printf("loadswizzle %d %d", target, count); |
| 151 | for (int i = 0; i < count; ++i) { |
| 152 | printf(", %d", READ8()); |
| 153 | } |
| 154 | break; |
| 155 | } |
| 156 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 157 | int target = READ8(); |
| 158 | int count = READ8(); |
| 159 | printf("loadswizzleglobal %d %d", target, count); |
| 160 | for (int i = 0; i < count; ++i) { |
| 161 | printf(", %d", READ8()); |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | case ByteCodeInstruction::kLoadExtended: printf("loadextended %d", READ8()); break; |
| 166 | case ByteCodeInstruction::kLoadExtendedGlobal: printf("loadextendedglobal %d", READ8()); |
| 167 | break; |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame^] | 168 | case ByteCodeInstruction::kMatrixToMatrix: { |
| 169 | int srcCols = READ8(); |
| 170 | int srcRows = READ8(); |
| 171 | int dstCols = READ8(); |
| 172 | int dstRows = READ8(); |
| 173 | printf("matrixtomatrix %dx%d %dx%d", srcCols, srcRows, dstCols, dstRows); |
| 174 | break; |
| 175 | } |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 176 | VECTOR_DISASSEMBLE(kMix, "mix") |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 177 | VECTOR_DISASSEMBLE(kMultiplyF, "multiplyf") |
| 178 | VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi") |
| 179 | VECTOR_DISASSEMBLE(kNegateF, "negatef") |
| 180 | VECTOR_DISASSEMBLE(kNegateI, "negatei") |
| 181 | VECTOR_DISASSEMBLE(kNot, "not") |
| 182 | VECTOR_DISASSEMBLE(kOrB, "orb") |
| 183 | VECTOR_DISASSEMBLE(kOrI, "ori") |
| 184 | VECTOR_DISASSEMBLE(kPop, "pop") |
| 185 | case ByteCodeInstruction::kPopN: printf("popN %d", READ8()); break; |
| 186 | case ByteCodeInstruction::kPushImmediate: { |
| 187 | uint32_t v = READ32(); |
| 188 | union { uint32_t u; float f; } pun = { v }; |
| 189 | printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str()); |
| 190 | break; |
| 191 | } |
| 192 | case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break; |
| 193 | case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break; |
| 194 | case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break; |
| 195 | case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break; |
| 196 | VECTOR_DISASSEMBLE(kRemainderF, "remainderf") |
| 197 | VECTOR_DISASSEMBLE(kRemainderS, "remainders") |
| 198 | VECTOR_DISASSEMBLE(kRemainderU, "remainderu") |
| 199 | case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break; |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame^] | 200 | case ByteCodeInstruction::kScalarToMatrix: { |
| 201 | int cols = READ8(); |
| 202 | int rows = READ8(); |
| 203 | printf("scalartomatrix %dx%d", cols, rows); |
| 204 | break; |
| 205 | } |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 206 | VECTOR_DISASSEMBLE(kSin, "sin") |
| 207 | VECTOR_DISASSEMBLE(kSqrt, "sqrt") |
| 208 | case ByteCodeInstruction::kStore: printf("store %d", READ8()); break; |
| 209 | case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break; |
| 210 | case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break; |
| 211 | case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break; |
| 212 | case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break; |
| 213 | case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break; |
| 214 | case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break; |
| 215 | case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break; |
| 216 | case ByteCodeInstruction::kStoreSwizzle: { |
| 217 | int target = READ8(); |
| 218 | int count = READ8(); |
| 219 | printf("storeswizzle %d %d", target, count); |
| 220 | for (int i = 0; i < count; ++i) { |
| 221 | printf(", %d", READ8()); |
| 222 | } |
| 223 | break; |
| 224 | } |
| 225 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 226 | int target = READ8(); |
| 227 | int count = READ8(); |
| 228 | printf("storeswizzleglobal %d %d", target, count); |
| 229 | for (int i = 0; i < count; ++i) { |
| 230 | printf(", %d", READ8()); |
| 231 | } |
| 232 | break; |
| 233 | } |
| 234 | case ByteCodeInstruction::kStoreSwizzleIndirect: { |
| 235 | int count = READ8(); |
| 236 | printf("storeswizzleindirect %d", count); |
| 237 | for (int i = 0; i < count; ++i) { |
| 238 | printf(", %d", READ8()); |
| 239 | } |
| 240 | break; |
| 241 | } |
| 242 | case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: { |
| 243 | int count = READ8(); |
| 244 | printf("storeswizzleindirectglobal %d", count); |
| 245 | for (int i = 0; i < count; ++i) { |
| 246 | printf(", %d", READ8()); |
| 247 | } |
| 248 | break; |
| 249 | } |
| 250 | case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break; |
| 251 | case ByteCodeInstruction::kStoreExtendedGlobal: printf("storeextendedglobal %d", READ8()); |
| 252 | break; |
| 253 | VECTOR_DISASSEMBLE(kSubtractF, "subtractf") |
| 254 | VECTOR_DISASSEMBLE(kSubtractI, "subtracti") |
| 255 | case ByteCodeInstruction::kSwizzle: { |
| 256 | printf("swizzle %d, ", READ8()); |
| 257 | int count = READ8(); |
| 258 | printf("%d", count); |
| 259 | for (int i = 0; i < count; ++i) { |
| 260 | printf(", %d", READ8()); |
| 261 | } |
| 262 | break; |
| 263 | } |
| 264 | VECTOR_DISASSEMBLE(kTan, "tan") |
| 265 | case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break; |
| 266 | case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break; |
| 267 | case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break; |
| 268 | case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break; |
| 269 | default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false); |
| 270 | } |
| 271 | return ip; |
| 272 | } |
| 273 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 274 | void Interpreter::disassemble(const ByteCodeFunction& f) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 275 | const uint8_t* ip = f.fCode.data(); |
| 276 | while (ip < f.fCode.data() + f.fCode.size()) { |
| 277 | printf("%d: ", (int) (ip - f.fCode.data())); |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 278 | ip = disassemble_instruction(ip); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 279 | printf("\n"); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 283 | #define VECTOR_BINARY_OP(base, field, op) \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 284 | case ByteCodeInstruction::base ## 4: \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 285 | sp[-4] = sp[-4].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 286 | POP(); \ |
| 287 | /* fall through */ \ |
| 288 | case ByteCodeInstruction::base ## 3: { \ |
| 289 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 290 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 291 | POP(); \ |
| 292 | } /* fall through */ \ |
| 293 | case ByteCodeInstruction::base ## 2: { \ |
| 294 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 295 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 296 | POP(); \ |
| 297 | } /* fall through */ \ |
| 298 | case ByteCodeInstruction::base: { \ |
| 299 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 300 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 301 | POP(); \ |
| 302 | break; \ |
| 303 | } |
Ethan Nicholas | aeb71ce | 2019-05-20 09:55:44 -0400 | [diff] [blame] | 304 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 305 | #define VECTOR_BINARY_FN(base, field, fn) \ |
| 306 | case ByteCodeInstruction::base ## 4: \ |
| 307 | sp[-4] = fn(sp[-4].field, sp[0].field); \ |
| 308 | POP(); \ |
| 309 | /* fall through */ \ |
| 310 | case ByteCodeInstruction::base ## 3: { \ |
| 311 | int target = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 312 | sp[target] = fn(sp[target].field, sp[0].field); \ |
| 313 | POP(); \ |
| 314 | } /* fall through */ \ |
| 315 | case ByteCodeInstruction::base ## 2: { \ |
| 316 | int target = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 317 | sp[target] = fn(sp[target].field, sp[0].field); \ |
| 318 | POP(); \ |
| 319 | } /* fall through */ \ |
| 320 | case ByteCodeInstruction::base: { \ |
| 321 | int target = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 322 | sp[target] = fn(sp[target].field, sp[0].field); \ |
| 323 | POP(); \ |
| 324 | break; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 325 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 326 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 327 | #define VECTOR_UNARY_FN(base, fn, field) \ |
| 328 | case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \ |
| 329 | case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \ |
| 330 | case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \ |
| 331 | case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \ |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 332 | break; |
| 333 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 334 | struct StackFrame { |
| 335 | const uint8_t* fCode; |
| 336 | const uint8_t* fIP; |
| 337 | Interpreter::Value* fStack; |
| 338 | }; |
| 339 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 340 | static float mix(float start, float end, float t) { |
| 341 | return start * (1 - t) + end * t; |
| 342 | } |
| 343 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 344 | void Interpreter::innerRun(const ByteCodeFunction& f, Value* stack, Value* outReturn) { |
| 345 | Value* sp = stack + f.fParameterCount + f.fLocalCount - 1; |
| 346 | |
| 347 | auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); }; |
| 348 | auto PUSH = [&](Value v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; }; |
| 349 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 350 | const uint8_t* code = f.fCode.data(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 351 | const uint8_t* ip = code; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 352 | std::vector<StackFrame> frames; |
| 353 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 354 | for (;;) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 355 | #ifdef TRACE |
Brian Osman | 3e833e1 | 2019-05-23 13:23:24 -0700 | [diff] [blame] | 356 | printf("at %3d ", (int) (ip - code)); |
| 357 | disassemble_instruction(ip); |
| 358 | printf("\n"); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 359 | #endif |
Brian Osman | e85b6a5 | 2019-05-22 14:50:59 -0700 | [diff] [blame] | 360 | ByteCodeInstruction inst = (ByteCodeInstruction) READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 361 | switch (inst) { |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 362 | VECTOR_BINARY_OP(kAddI, fSigned, +) |
| 363 | VECTOR_BINARY_OP(kAddF, fFloat, +) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 364 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 365 | case ByteCodeInstruction::kBranch: |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 366 | ip = code + READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 367 | break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 368 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 369 | case ByteCodeInstruction::kCall: { |
| 370 | // Precursor code has pushed all parameters to the stack. Update our bottom of |
| 371 | // stack to point at the first parameter, and our sp to point past those parameters |
| 372 | // (plus space for locals). |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 373 | int target = READ8(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 374 | const ByteCodeFunction* fun = fByteCode->fFunctions[target].get(); |
| 375 | frames.push_back({ code, ip, stack }); |
| 376 | ip = code = fun->fCode.data(); |
| 377 | stack = sp - fun->fParameterCount + 1; |
| 378 | sp = stack + fun->fParameterCount + fun->fLocalCount - 1; |
| 379 | break; |
| 380 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 381 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 382 | case ByteCodeInstruction::kCallExternal: { |
| 383 | int argumentCount = READ8(); |
| 384 | int returnCount = READ8(); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 385 | int target = READ8(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 386 | ExternalValue* v = fByteCode->fExternalValues[target]; |
| 387 | sp -= argumentCount - 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 388 | |
| 389 | Value tmp[4]; |
| 390 | SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmp)); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 391 | v->call(sp, tmp); |
| 392 | memcpy(sp, tmp, returnCount * sizeof(Value)); |
| 393 | sp += returnCount - 1; |
| 394 | break; |
| 395 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 396 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 397 | VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==) |
| 398 | VECTOR_BINARY_OP(kCompareFEQ, fFloat, ==) |
| 399 | VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=) |
| 400 | VECTOR_BINARY_OP(kCompareFNEQ, fFloat, !=) |
| 401 | VECTOR_BINARY_OP(kCompareSGT, fSigned, >) |
| 402 | VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >) |
| 403 | VECTOR_BINARY_OP(kCompareFGT, fFloat, >) |
| 404 | VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=) |
| 405 | VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=) |
| 406 | VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=) |
| 407 | VECTOR_BINARY_OP(kCompareSLT, fSigned, <) |
| 408 | VECTOR_BINARY_OP(kCompareULT, fUnsigned, <) |
| 409 | VECTOR_BINARY_OP(kCompareFLT, fFloat, <) |
| 410 | VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=) |
| 411 | VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=) |
| 412 | VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 413 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 414 | case ByteCodeInstruction::kConditionalBranch: { |
| 415 | int target = READ16(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 416 | if (POP().fBool) { |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 417 | ip = code + target; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 418 | } |
| 419 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 420 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 421 | |
| 422 | case ByteCodeInstruction::kConvertFtoI4: sp[-3].fSigned = (int)sp[-3].fFloat; |
| 423 | case ByteCodeInstruction::kConvertFtoI3: sp[-2].fSigned = (int)sp[-2].fFloat; |
| 424 | case ByteCodeInstruction::kConvertFtoI2: sp[-1].fSigned = (int)sp[-1].fFloat; |
| 425 | case ByteCodeInstruction::kConvertFtoI: sp[ 0].fSigned = (int)sp[ 0].fFloat; |
| 426 | break; |
| 427 | |
| 428 | case ByteCodeInstruction::kConvertStoF4: sp[-3].fFloat = sp[-3].fSigned; |
| 429 | case ByteCodeInstruction::kConvertStoF3: sp[-2].fFloat = sp[-2].fSigned; |
| 430 | case ByteCodeInstruction::kConvertStoF2: sp[-1].fFloat = sp[-1].fSigned; |
| 431 | case ByteCodeInstruction::kConvertStoF : sp[ 0].fFloat = sp[ 0].fSigned; |
| 432 | break; |
| 433 | |
| 434 | case ByteCodeInstruction::kConvertUtoF4: sp[-3].fFloat = sp[-3].fUnsigned; |
| 435 | case ByteCodeInstruction::kConvertUtoF3: sp[-2].fFloat = sp[-2].fUnsigned; |
| 436 | case ByteCodeInstruction::kConvertUtoF2: sp[-1].fFloat = sp[-1].fUnsigned; |
| 437 | case ByteCodeInstruction::kConvertUtoF : sp[ 0].fFloat = sp[ 0].fUnsigned; |
| 438 | break; |
| 439 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 440 | VECTOR_UNARY_FN(kCos, cosf, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 441 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 442 | case ByteCodeInstruction::kCross: { |
| 443 | SkPoint3 cross = SkPoint3::CrossProduct(SkPoint3::Make(sp[-5].fFloat, |
| 444 | sp[-4].fFloat, |
| 445 | sp[-3].fFloat), |
| 446 | SkPoint3::Make(sp[-2].fFloat, |
| 447 | sp[-1].fFloat, |
| 448 | sp[ 0].fFloat)); |
| 449 | sp -= 3; |
| 450 | sp[-2] = cross.fX; |
| 451 | sp[-1] = cross.fY; |
| 452 | sp[ 0] = cross.fZ; |
| 453 | break; |
| 454 | } |
| 455 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 456 | case ByteCodeInstruction::kDebugPrint: { |
| 457 | Value v = POP(); |
| 458 | printf("Debug: %d(int), %d(uint), %f(float)\n", v.fSigned, v.fUnsigned, v.fFloat); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 459 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 460 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 461 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 462 | VECTOR_BINARY_OP(kDivideS, fSigned, /) |
| 463 | VECTOR_BINARY_OP(kDivideU, fUnsigned, /) |
| 464 | VECTOR_BINARY_OP(kDivideF, fFloat, /) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 465 | |
| 466 | case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 467 | case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 468 | case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 469 | case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 470 | break; |
| 471 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 472 | case ByteCodeInstruction::kDupN: { |
| 473 | int count = READ8(); |
| 474 | memcpy(sp + 1, sp - count + 1, count * sizeof(Value)); |
| 475 | sp += count; |
| 476 | break; |
| 477 | } |
| 478 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 479 | case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3]; |
| 480 | case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2]; |
| 481 | case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1]; |
| 482 | case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0]; |
| 483 | ++ip; |
| 484 | sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1; |
| 485 | break; |
| 486 | |
| 487 | case ByteCodeInstruction::kLoadGlobal4: sp[4] = fGlobals[*ip + 3]; |
| 488 | case ByteCodeInstruction::kLoadGlobal3: sp[3] = fGlobals[*ip + 2]; |
| 489 | case ByteCodeInstruction::kLoadGlobal2: sp[2] = fGlobals[*ip + 1]; |
| 490 | case ByteCodeInstruction::kLoadGlobal : sp[1] = fGlobals[*ip + 0]; |
| 491 | ++ip; |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 492 | sp += (int)inst - |
| 493 | (int)ByteCodeInstruction::kLoadGlobal + 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 494 | break; |
| 495 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 496 | case ByteCodeInstruction::kLoadExtended: { |
| 497 | int count = READ8(); |
| 498 | int src = POP().fSigned; |
| 499 | memcpy(sp + 1, &stack[src], count * sizeof(Value)); |
| 500 | sp += count; |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | case ByteCodeInstruction::kLoadExtendedGlobal: { |
| 505 | int count = READ8(); |
| 506 | int src = POP().fSigned; |
| 507 | SkASSERT(src + count <= (int) fGlobals.size()); |
| 508 | memcpy(sp + 1, &fGlobals[src], count * sizeof(Value)); |
| 509 | sp += count; |
| 510 | break; |
| 511 | } |
| 512 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 513 | case ByteCodeInstruction::kLoadSwizzle: { |
| 514 | int src = READ8(); |
| 515 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 516 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 517 | PUSH(stack[src + *(ip + i)]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 518 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 519 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 520 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 521 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 522 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 523 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 524 | int src = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 525 | SkASSERT(src < (int) fGlobals.size()); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 526 | int count = READ8(); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 527 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 528 | PUSH(fGlobals[src + *(ip + i)]); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 529 | } |
| 530 | ip += count; |
| 531 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 532 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 533 | |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame^] | 534 | case ByteCodeInstruction::kMatrixToMatrix: { |
| 535 | int srcCols = READ8(); |
| 536 | int srcRows = READ8(); |
| 537 | int dstCols = READ8(); |
| 538 | int dstRows = READ8(); |
| 539 | SkASSERT(srcCols >= 2 && srcCols <= 4); |
| 540 | SkASSERT(srcRows >= 2 && srcRows <= 4); |
| 541 | SkASSERT(dstCols >= 2 && dstCols <= 4); |
| 542 | SkASSERT(dstRows >= 2 && dstRows <= 4); |
| 543 | SkMatrix44 m; |
| 544 | for (int c = srcCols - 1; c >= 0; --c) { |
| 545 | for (int r = srcRows - 1; r >= 0; --r) { |
| 546 | m.set(r, c, POP().fFloat); |
| 547 | } |
| 548 | } |
| 549 | for (int c = 0; c < dstCols; ++c) { |
| 550 | for (int r = 0; r < dstRows; ++r) { |
| 551 | PUSH(m.get(r, c)); |
| 552 | } |
| 553 | } |
| 554 | break; |
| 555 | } |
| 556 | |
Ethan Nicholas | ae9633b | 2019-05-24 12:46:34 -0400 | [diff] [blame] | 557 | // stack looks like: X1 Y1 Z1 W1 X2 Y2 Z2 W2 T |
| 558 | case ByteCodeInstruction::kMix4: |
| 559 | sp[-5] = mix(sp[-5].fFloat, sp[-1].fFloat, sp[0].fFloat); |
| 560 | // fall through |
| 561 | case ByteCodeInstruction::kMix3: { |
| 562 | int count = (int) inst - (int) ByteCodeInstruction::kMix + 1; |
| 563 | int target = 2 - count * 2; |
| 564 | sp[target] = mix(sp[target].fFloat, sp[2 - count].fFloat, sp[0].fFloat); |
| 565 | // fall through |
| 566 | } |
| 567 | case ByteCodeInstruction::kMix2: { |
| 568 | int count = (int) inst - (int) ByteCodeInstruction::kMix + 1; |
| 569 | int target = 1 - count * 2; |
| 570 | sp[target] = mix(sp[target].fFloat, sp[1 - count].fFloat, sp[0].fFloat); |
| 571 | // fall through |
| 572 | } |
| 573 | case ByteCodeInstruction::kMix: { |
| 574 | int count = (int) inst - (int) ByteCodeInstruction::kMix + 1; |
| 575 | int target = -count * 2; |
| 576 | sp[target] = mix(sp[target].fFloat, sp[-count].fFloat, sp[0].fFloat); |
| 577 | sp -= 1 + count; |
| 578 | break; |
| 579 | } |
| 580 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 581 | VECTOR_BINARY_OP(kMultiplyI, fSigned, *) |
| 582 | VECTOR_BINARY_OP(kMultiplyF, fFloat, *) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 583 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 584 | case ByteCodeInstruction::kNot: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 585 | sp[0].fBool = !sp[0].fBool; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 586 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 587 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 588 | case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat; |
| 589 | case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat; |
| 590 | case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat; |
| 591 | case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 592 | break; |
| 593 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 594 | case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned; |
| 595 | case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned; |
| 596 | case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned; |
| 597 | case ByteCodeInstruction::kNegateI : sp[ 0] = -sp [0].fSigned; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 598 | break; |
| 599 | |
| 600 | case ByteCodeInstruction::kPop4: POP(); |
| 601 | case ByteCodeInstruction::kPop3: POP(); |
| 602 | case ByteCodeInstruction::kPop2: POP(); |
| 603 | case ByteCodeInstruction::kPop : POP(); |
| 604 | break; |
| 605 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 606 | case ByteCodeInstruction::kPopN: |
| 607 | sp -= READ8(); |
| 608 | break; |
| 609 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 610 | case ByteCodeInstruction::kPushImmediate: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 611 | PUSH(READ32()); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 612 | break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 613 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 614 | case ByteCodeInstruction::kReadExternal: // fall through |
| 615 | case ByteCodeInstruction::kReadExternal2: // fall through |
| 616 | case ByteCodeInstruction::kReadExternal3: // fall through |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 617 | case ByteCodeInstruction::kReadExternal4: { |
| 618 | int src = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 619 | fByteCode->fExternalValues[src]->read(sp + 1); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 620 | sp += (int) inst - (int) ByteCodeInstruction::kReadExternal + 1; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 621 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 622 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 623 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 624 | VECTOR_BINARY_FN(kRemainderF, fFloat, fmodf) |
| 625 | VECTOR_BINARY_OP(kRemainderS, fSigned, %) |
| 626 | VECTOR_BINARY_OP(kRemainderU, fUnsigned, %) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 627 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 628 | case ByteCodeInstruction::kReturn: { |
| 629 | int count = READ8(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 630 | if (frames.empty()) { |
| 631 | if (outReturn) { |
| 632 | memcpy(outReturn, sp - count + 1, count * sizeof(Value)); |
| 633 | } |
| 634 | return; |
| 635 | } else { |
| 636 | // When we were called, 'stack' was positioned at the old top-of-stack (where |
| 637 | // our parameters were placed). So copy our return values to that same spot. |
| 638 | memmove(stack, sp - count + 1, count * sizeof(Value)); |
| 639 | |
| 640 | // Now move the stack pointer to the end of the just-pushed return values, |
| 641 | // and restore everything else. |
| 642 | const StackFrame& frame(frames.back()); |
| 643 | sp = stack + count - 1; |
| 644 | stack = frame.fStack; |
| 645 | code = frame.fCode; |
| 646 | ip = frame.fIP; |
| 647 | frames.pop_back(); |
| 648 | break; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 649 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 650 | } |
| 651 | |
Brian Osman | 29e013d | 2019-05-28 17:16:03 -0400 | [diff] [blame^] | 652 | case ByteCodeInstruction::kScalarToMatrix: { |
| 653 | int cols = READ8(); |
| 654 | int rows = READ8(); |
| 655 | Value v = POP(); |
| 656 | for (int c = 0; c < cols; ++c) { |
| 657 | for (int r = 0; r < rows; ++r) { |
| 658 | PUSH(c == r ? v : 0.0f); |
| 659 | } |
| 660 | } |
| 661 | break; |
| 662 | } |
| 663 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 664 | VECTOR_UNARY_FN(kSin, sinf, fFloat) |
| 665 | VECTOR_UNARY_FN(kSqrt, sqrtf, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 666 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 667 | case ByteCodeInstruction::kStore4: stack[*ip + 3] = POP(); |
| 668 | case ByteCodeInstruction::kStore3: stack[*ip + 2] = POP(); |
| 669 | case ByteCodeInstruction::kStore2: stack[*ip + 1] = POP(); |
| 670 | case ByteCodeInstruction::kStore : stack[*ip + 0] = POP(); |
| 671 | ++ip; |
| 672 | break; |
| 673 | |
| 674 | case ByteCodeInstruction::kStoreGlobal4: fGlobals[*ip + 3] = POP(); |
| 675 | case ByteCodeInstruction::kStoreGlobal3: fGlobals[*ip + 2] = POP(); |
| 676 | case ByteCodeInstruction::kStoreGlobal2: fGlobals[*ip + 1] = POP(); |
| 677 | case ByteCodeInstruction::kStoreGlobal : fGlobals[*ip + 0] = POP(); |
| 678 | ++ip; |
| 679 | break; |
| 680 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 681 | case ByteCodeInstruction::kStoreExtended: { |
| 682 | int count = READ8(); |
| 683 | int target = POP().fSigned; |
| 684 | memcpy(&stack[target], sp - count + 1, count * sizeof(Value)); |
| 685 | sp -= count; |
| 686 | break; |
| 687 | } |
| 688 | case ByteCodeInstruction::kStoreExtendedGlobal: { |
| 689 | int count = READ8(); |
| 690 | int target = POP().fSigned; |
| 691 | SkASSERT(target + count <= (int) fGlobals.size()); |
| 692 | memcpy(&fGlobals[target], sp - count + 1, count * sizeof(Value)); |
| 693 | sp -= count; |
| 694 | break; |
| 695 | } |
| 696 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 697 | case ByteCodeInstruction::kStoreSwizzle: { |
| 698 | int target = READ8(); |
| 699 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 700 | for (int i = count - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 701 | stack[target + *(ip + i)] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 702 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 703 | ip += count; |
| 704 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 705 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 706 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 707 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 708 | int target = READ8(); |
| 709 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 710 | for (int i = count - 1; i >= 0; --i) { |
| 711 | fGlobals[target + *(ip + i)] = POP(); |
| 712 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 713 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 714 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 715 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame] | 716 | case ByteCodeInstruction::kStoreSwizzleIndirect: { |
| 717 | int target = POP().fSigned; |
| 718 | int count = READ8(); |
| 719 | for (int i = count - 1; i >= 0; --i) { |
| 720 | stack[target + *(ip + i)] = POP(); |
| 721 | } |
| 722 | ip += count; |
| 723 | break; |
| 724 | } |
| 725 | case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: { |
| 726 | int target = POP().fSigned; |
| 727 | int count = READ8(); |
| 728 | for (int i = count - 1; i >= 0; --i) { |
| 729 | fGlobals[target + *(ip + i)] = POP(); |
| 730 | } |
| 731 | ip += count; |
| 732 | break; |
| 733 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 734 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 735 | VECTOR_BINARY_OP(kSubtractI, fSigned, -) |
| 736 | VECTOR_BINARY_OP(kSubtractF, fFloat, -) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 737 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 738 | case ByteCodeInstruction::kSwizzle: { |
| 739 | Value tmp[4]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 740 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 741 | tmp[i] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 742 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 743 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 744 | PUSH(tmp[READ8()]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 745 | } |
| 746 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 747 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 748 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 749 | VECTOR_UNARY_FN(kTan, tanf, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 750 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 751 | case ByteCodeInstruction::kWriteExternal: // fall through |
| 752 | case ByteCodeInstruction::kWriteExternal2: // fall through |
| 753 | case ByteCodeInstruction::kWriteExternal3: // fall through |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 754 | case ByteCodeInstruction::kWriteExternal4: { |
| 755 | int count = (int) inst - (int) ByteCodeInstruction::kWriteExternal + 1; |
| 756 | int target = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 757 | fByteCode->fExternalValues[target]->write(sp - count + 1); |
| 758 | sp -= count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 759 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 760 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 761 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 762 | default: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 763 | SkDEBUGFAILF("unsupported instruction %d\n", (int) inst); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 764 | } |
| 765 | #ifdef TRACE |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 766 | int stackSize = (int) (sp - stack + 1); |
| 767 | printf("STACK(%d):", stackSize); |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 768 | for (int i = 0; i < stackSize; ++i) { |
Brian Osman | e85b6a5 | 2019-05-22 14:50:59 -0700 | [diff] [blame] | 769 | printf(" %d(%g)", stack[i].fSigned, stack[i].fFloat); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 770 | } |
| 771 | printf("\n"); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 772 | #endif |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 773 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | } // namespace |
| 777 | |
| 778 | #endif |