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