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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkRasterPipeline.h" |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 11 | #include "src/sksl/SkSLExternalValue.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "src/sksl/SkSLInterpreter.h" |
| 13 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 14 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 15 | #include "src/sksl/ir/SkSLForStatement.h" |
| 16 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 17 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 18 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 19 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 20 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 21 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 22 | #include "src/sksl/ir/SkSLProgram.h" |
| 23 | #include "src/sksl/ir/SkSLStatement.h" |
| 24 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 25 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 26 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 27 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 28 | |
| 29 | namespace SkSL { |
| 30 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 31 | static constexpr int UNINITIALIZED = 0xDEADBEEF; |
| 32 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 33 | Interpreter::Interpreter(std::unique_ptr<Program> program, |
| 34 | std::unique_ptr<ByteCode> byteCode, |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 35 | Interpreter::Value inputs[]) |
| 36 | : fProgram(std::move(program)) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 37 | , fByteCode(std::move(byteCode)) |
| 38 | , fGlobals(fByteCode->fGlobalCount, UNINITIALIZED) { |
Brian Osman | d369a5e | 2019-05-09 13:13:25 -0400 | [diff] [blame] | 39 | this->setInputs(inputs); |
| 40 | } |
| 41 | |
| 42 | void Interpreter::setInputs(Interpreter::Value inputs[]) { |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 43 | for (uint8_t slot : fByteCode->fInputSlots) { |
| 44 | fGlobals[slot] = *inputs++; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | void Interpreter::run(const ByteCodeFunction& f, Interpreter::Value args[], |
| 49 | Interpreter::Value* outReturn) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 50 | #ifdef TRACE |
| 51 | this->disassemble(f); |
| 52 | #endif |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 53 | Value smallStack[128]; |
| 54 | std::unique_ptr<Value[]> largeStack; |
| 55 | Value* stack = smallStack; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 56 | if ((int) SK_ARRAY_COUNT(smallStack) < f.fStackCount) { |
| 57 | largeStack.reset(new Value[f.fStackCount]); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 58 | stack = largeStack.get(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 59 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 60 | |
| 61 | if (f.fParameterCount) { |
| 62 | memcpy(stack, args, f.fParameterCount * sizeof(Value)); |
| 63 | } |
| 64 | this->innerRun(f, stack, outReturn); |
| 65 | |
| 66 | for (const Variable* p : f.fDeclaration.fParameters) { |
| 67 | const int nvalues = p->fType.columns() |
| 68 | * p->fType.rows(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 69 | if (p->fModifiers.fFlags & Modifiers::kOut_Flag) { |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 70 | memcpy(args, stack, nvalues * sizeof(Value)); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 71 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 72 | args += nvalues; |
| 73 | stack += nvalues; |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 74 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 75 | } |
| 76 | |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 77 | template <typename T> |
| 78 | static T unaligned_load(const void* ptr) { |
| 79 | T val; |
| 80 | memcpy(&val, ptr, sizeof(val)); |
| 81 | return val; |
| 82 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 83 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 84 | #define READ8() (*(ip++)) |
Mike Klein | 76346ac | 2019-05-17 11:57:10 -0500 | [diff] [blame] | 85 | #define READ16() (ip += 2, unaligned_load<uint16_t>(ip - 2)) |
| 86 | #define READ32() (ip += 4, unaligned_load<uint32_t>(ip - 4)) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 87 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 88 | #define VECTOR_DISASSEMBLE(op, text) \ |
| 89 | case ByteCodeInstruction::op: printf(text); break; \ |
| 90 | case ByteCodeInstruction::op##2: printf(text "2"); break; \ |
| 91 | case ByteCodeInstruction::op##3: printf(text "3"); break; \ |
| 92 | case ByteCodeInstruction::op##4: printf(text "4"); break; |
| 93 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 94 | void Interpreter::disassemble(const ByteCodeFunction& f) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 95 | const uint8_t* ip = f.fCode.data(); |
| 96 | while (ip < f.fCode.data() + f.fCode.size()) { |
| 97 | printf("%d: ", (int) (ip - f.fCode.data())); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 98 | switch ((ByteCodeInstruction) READ8()) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 99 | VECTOR_DISASSEMBLE(kAddF, "addf") |
| 100 | VECTOR_DISASSEMBLE(kAddI, "addi") |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 101 | case ByteCodeInstruction::kAndB: printf("andb"); break; |
| 102 | case ByteCodeInstruction::kAndI: printf("andi"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 103 | case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 104 | case ByteCodeInstruction::kCall: printf("call %d", READ8()); break; |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 105 | case ByteCodeInstruction::kCallExternal: { |
| 106 | int argumentCount = READ8(); |
| 107 | int returnCount = READ8(); |
| 108 | int externalValue = READ8(); |
| 109 | printf("callexternal %d, %d, %d", argumentCount, returnCount, externalValue); |
| 110 | break; |
| 111 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 112 | VECTOR_DISASSEMBLE(kCompareIEQ, "compareieq") |
| 113 | VECTOR_DISASSEMBLE(kCompareINEQ, "compareineq") |
| 114 | VECTOR_DISASSEMBLE(kCompareFEQ, "comparefeq") |
| 115 | VECTOR_DISASSEMBLE(kCompareFNEQ, "comparefneq") |
| 116 | VECTOR_DISASSEMBLE(kCompareFGT, "comparefgt") |
| 117 | VECTOR_DISASSEMBLE(kCompareFGTEQ, "comparefgteq") |
| 118 | VECTOR_DISASSEMBLE(kCompareFLT, "compareflt") |
| 119 | VECTOR_DISASSEMBLE(kCompareFLTEQ, "compareflteq") |
| 120 | VECTOR_DISASSEMBLE(kCompareSGT, "comparesgt") |
| 121 | VECTOR_DISASSEMBLE(kCompareSGTEQ, "comparesgteq") |
| 122 | VECTOR_DISASSEMBLE(kCompareSLT, "compareslt") |
| 123 | VECTOR_DISASSEMBLE(kCompareSLTEQ, "compareslteq") |
| 124 | VECTOR_DISASSEMBLE(kCompareUGT, "compareugt") |
| 125 | VECTOR_DISASSEMBLE(kCompareUGTEQ, "compareugteq") |
| 126 | VECTOR_DISASSEMBLE(kCompareULT, "compareult") |
| 127 | VECTOR_DISASSEMBLE(kCompareULTEQ, "compareulteq") |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 128 | case ByteCodeInstruction::kConditionalBranch: |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 129 | printf("conditionalbranch %d", READ16()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 130 | break; |
| 131 | case ByteCodeInstruction::kDebugPrint: printf("debugprint"); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 132 | VECTOR_DISASSEMBLE(kDivideF, "dividef") |
| 133 | VECTOR_DISASSEMBLE(kDivideS, "divideS") |
| 134 | VECTOR_DISASSEMBLE(kDivideU, "divideu") |
| 135 | VECTOR_DISASSEMBLE(kDup, "dup") |
| 136 | VECTOR_DISASSEMBLE(kFloatToInt, "floattoint") |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 137 | case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 138 | case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break; |
| 139 | case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break; |
| 140 | case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 141 | case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 142 | case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break; |
| 143 | case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break; |
| 144 | case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 145 | case ByteCodeInstruction::kLoadSwizzle: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 146 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 147 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 148 | printf("loadswizzle %d %d", target, count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 149 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 150 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 151 | } |
| 152 | break; |
| 153 | } |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 154 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 155 | int target = READ8(); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 156 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 157 | printf("loadswizzleglobal %d %d", target, count); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 158 | for (int i = 0; i < count; ++i) { |
| 159 | printf(", %d", READ8()); |
| 160 | } |
| 161 | break; |
| 162 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 163 | VECTOR_DISASSEMBLE(kMultiplyF, "multiplyf") |
| 164 | VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi") |
| 165 | VECTOR_DISASSEMBLE(kNegateF, "negatef") |
| 166 | VECTOR_DISASSEMBLE(kNegateS, "negates") |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 167 | VECTOR_DISASSEMBLE(kNot, "not") |
| 168 | VECTOR_DISASSEMBLE(kOrB, "orb") |
| 169 | VECTOR_DISASSEMBLE(kOrI, "ori") |
| 170 | VECTOR_DISASSEMBLE(kPop, "pop") |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 171 | case ByteCodeInstruction::kPushImmediate: { |
| 172 | uint32_t v = READ32(); |
| 173 | union { uint32_t u; float f; } pun = { v }; |
| 174 | printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 175 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 176 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 177 | case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 178 | case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break; |
| 179 | case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break; |
| 180 | case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break; |
| 181 | VECTOR_DISASSEMBLE(kRemainderF, "remainderf") |
| 182 | VECTOR_DISASSEMBLE(kRemainderS, "remainders") |
| 183 | VECTOR_DISASSEMBLE(kRemainderU, "remainderu") |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 184 | case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 185 | VECTOR_DISASSEMBLE(kSignedToFloat, "signedtofloat") |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 186 | case ByteCodeInstruction::kStore: printf("store %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 187 | case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break; |
| 188 | case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break; |
| 189 | case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 190 | case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 191 | case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break; |
| 192 | case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break; |
| 193 | case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 194 | case ByteCodeInstruction::kStoreSwizzle: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 195 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 196 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 197 | printf("storeswizzle %d %d", target, count); |
| 198 | for (int i = 0; i < count; ++i) { |
| 199 | printf(", %d", READ8()); |
| 200 | } |
| 201 | break; |
| 202 | } |
| 203 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 204 | int target = READ8(); |
| 205 | int count = READ8(); |
| 206 | printf("storeswizzleglobal %d %d", target, count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 207 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 208 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 209 | } |
| 210 | break; |
| 211 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 212 | VECTOR_DISASSEMBLE(kSubtractF, "subtractf") |
| 213 | VECTOR_DISASSEMBLE(kSubtractI, "subtracti") |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 214 | case ByteCodeInstruction::kSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 215 | printf("swizzle %d, ", READ8()); |
| 216 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 217 | printf("%d", count); |
| 218 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 219 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 220 | } |
| 221 | break; |
| 222 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 223 | VECTOR_DISASSEMBLE(kUnsignedToFloat, "unsignedtofloat") |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 224 | case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 225 | case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break; |
| 226 | case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break; |
| 227 | case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 228 | default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 229 | } |
| 230 | printf("\n"); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 231 | } |
| 232 | } |
| 233 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 234 | #define VECTOR_BINARY_OP(base, type, src, target, op) \ |
| 235 | case ByteCodeInstruction::base ## 4: \ |
| 236 | sp[-4].target = sp[-4].src op sp[0].src; \ |
| 237 | POP(); \ |
| 238 | /* fall through */ \ |
| 239 | case ByteCodeInstruction::base ## 3: { \ |
| 240 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 241 | sp[count].target = sp[count].src op sp[0].src; \ |
| 242 | POP(); \ |
| 243 | } /* fall through */ \ |
| 244 | case ByteCodeInstruction::base ## 2: { \ |
| 245 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 246 | sp[count].target = sp[count].src op sp[0].src; \ |
| 247 | POP(); \ |
| 248 | } /* fall through */ \ |
| 249 | case ByteCodeInstruction::base: { \ |
| 250 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 251 | sp[count].target = sp[count].src op sp[0].src; \ |
| 252 | POP(); \ |
| 253 | break; \ |
| 254 | } |
Ethan Nicholas | aeb71ce | 2019-05-20 09:55:44 -0400 | [diff] [blame] | 255 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 256 | #define VECTOR_BINARY_FN(base, type, src, target, fn) \ |
| 257 | case ByteCodeInstruction::base ## 4: \ |
| 258 | sp[-4].target = fn(sp[-4].src, sp[0].src); \ |
| 259 | POP(); \ |
| 260 | /* fall through */ \ |
| 261 | case ByteCodeInstruction::base ## 3: { \ |
| 262 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 263 | sp[count].target = fn(sp[count].src, sp[0].src); \ |
| 264 | POP(); \ |
| 265 | } /* fall through */ \ |
| 266 | case ByteCodeInstruction::base ## 2: { \ |
| 267 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 268 | sp[count].target = fn(sp[count].src, sp[0].src); \ |
| 269 | POP(); \ |
| 270 | } /* fall through */ \ |
| 271 | case ByteCodeInstruction::base: { \ |
| 272 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
| 273 | sp[count].target = fn(sp[count].src, sp[0].src); \ |
| 274 | POP(); \ |
| 275 | break; \ |
| 276 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 277 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 278 | struct StackFrame { |
| 279 | const uint8_t* fCode; |
| 280 | const uint8_t* fIP; |
| 281 | Interpreter::Value* fStack; |
| 282 | }; |
| 283 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 284 | void Interpreter::innerRun(const ByteCodeFunction& f, Value* stack, Value* outReturn) { |
| 285 | Value* sp = stack + f.fParameterCount + f.fLocalCount - 1; |
| 286 | |
| 287 | auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); }; |
| 288 | auto PUSH = [&](Value v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; }; |
| 289 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 290 | const uint8_t* code = f.fCode.data(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 291 | const uint8_t* ip = code; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 292 | std::vector<StackFrame> frames; |
| 293 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 294 | for (;;) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 295 | ByteCodeInstruction inst = (ByteCodeInstruction) READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 296 | #ifdef TRACE |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 297 | printf("at %d\n", (int) (ip - code - 1)); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 298 | #endif |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 299 | switch (inst) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 300 | VECTOR_BINARY_OP(kAddI, int32_t, fSigned, fSigned, +) |
| 301 | VECTOR_BINARY_OP(kAddF, float, fFloat, fFloat, +) |
| 302 | case ByteCodeInstruction::kBranch: |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 303 | ip = code + READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 304 | break; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 305 | case ByteCodeInstruction::kCall: { |
| 306 | // Precursor code has pushed all parameters to the stack. Update our bottom of |
| 307 | // stack to point at the first parameter, and our sp to point past those parameters |
| 308 | // (plus space for locals). |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 309 | int target = READ8(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 310 | const ByteCodeFunction* fun = fByteCode->fFunctions[target].get(); |
| 311 | frames.push_back({ code, ip, stack }); |
| 312 | ip = code = fun->fCode.data(); |
| 313 | stack = sp - fun->fParameterCount + 1; |
| 314 | sp = stack + fun->fParameterCount + fun->fLocalCount - 1; |
| 315 | break; |
| 316 | } |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 317 | case ByteCodeInstruction::kCallExternal: { |
| 318 | int argumentCount = READ8(); |
| 319 | int returnCount = READ8(); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 320 | int target = READ8(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 321 | ExternalValue* v = fByteCode->fExternalValues[target]; |
| 322 | sp -= argumentCount - 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 323 | |
| 324 | Value tmp[4]; |
| 325 | SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmp)); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 326 | v->call(sp, tmp); |
| 327 | memcpy(sp, tmp, returnCount * sizeof(Value)); |
| 328 | sp += returnCount - 1; |
| 329 | break; |
| 330 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 331 | VECTOR_BINARY_OP(kCompareIEQ, int32_t, fSigned, fBool, ==) |
| 332 | VECTOR_BINARY_OP(kCompareFEQ, float, fFloat, fBool, ==) |
| 333 | VECTOR_BINARY_OP(kCompareINEQ, int32_t, fSigned, fBool, !=) |
| 334 | VECTOR_BINARY_OP(kCompareFNEQ, float, fFloat, fBool, !=) |
| 335 | VECTOR_BINARY_OP(kCompareSGT, int32_t, fSigned, fBool, >) |
| 336 | VECTOR_BINARY_OP(kCompareUGT, uint32_t, fUnsigned, fBool, >) |
| 337 | VECTOR_BINARY_OP(kCompareFGT, float, fFloat, fBool, >) |
| 338 | VECTOR_BINARY_OP(kCompareSGTEQ, int32_t, fSigned, fBool, >=) |
| 339 | VECTOR_BINARY_OP(kCompareUGTEQ, uint32_t, fUnsigned, fBool, >=) |
| 340 | VECTOR_BINARY_OP(kCompareFGTEQ, float, fFloat, fBool, >=) |
| 341 | VECTOR_BINARY_OP(kCompareSLT, int32_t, fSigned, fBool, <) |
| 342 | VECTOR_BINARY_OP(kCompareULT, uint32_t, fUnsigned, fBool, <) |
| 343 | VECTOR_BINARY_OP(kCompareFLT, float, fFloat, fBool, <) |
| 344 | VECTOR_BINARY_OP(kCompareSLTEQ, int32_t, fSigned, fBool, <=) |
| 345 | VECTOR_BINARY_OP(kCompareULTEQ, uint32_t, fUnsigned, fBool, <=) |
| 346 | VECTOR_BINARY_OP(kCompareFLTEQ, float, fFloat, fBool, <=) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 347 | case ByteCodeInstruction::kConditionalBranch: { |
| 348 | int target = READ16(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 349 | if (POP().fBool) { |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 350 | ip = code + target; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 351 | } |
| 352 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 353 | } |
| 354 | case ByteCodeInstruction::kDebugPrint: { |
| 355 | Value v = POP(); |
| 356 | 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] | 357 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 358 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 359 | VECTOR_BINARY_OP(kDivideS, int32_t, fSigned, fSigned, /) |
| 360 | VECTOR_BINARY_OP(kDivideU, uint32_t, fUnsigned, fUnsigned, /) |
| 361 | VECTOR_BINARY_OP(kDivideF, float, fFloat, fFloat, /) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 362 | |
| 363 | case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 364 | case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 365 | case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 366 | case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 367 | break; |
| 368 | |
| 369 | case ByteCodeInstruction::kFloatToInt4: sp[-3].fSigned = (int)sp[-3].fFloat; |
| 370 | case ByteCodeInstruction::kFloatToInt3: sp[-2].fSigned = (int)sp[-2].fFloat; |
| 371 | case ByteCodeInstruction::kFloatToInt2: sp[-1].fSigned = (int)sp[-1].fFloat; |
| 372 | case ByteCodeInstruction::kFloatToInt: sp[ 0].fSigned = (int)sp[ 0].fFloat; |
| 373 | break; |
| 374 | |
| 375 | case ByteCodeInstruction::kSignedToFloat4: sp[-3].fFloat = sp[-3].fSigned; |
| 376 | case ByteCodeInstruction::kSignedToFloat3: sp[-2].fFloat = sp[-2].fSigned; |
| 377 | case ByteCodeInstruction::kSignedToFloat2: sp[-1].fFloat = sp[-1].fSigned; |
| 378 | case ByteCodeInstruction::kSignedToFloat : sp[ 0].fFloat = sp[ 0].fSigned; |
| 379 | break; |
| 380 | |
| 381 | case ByteCodeInstruction::kUnsignedToFloat4: sp[-3].fFloat = sp[-3].fUnsigned; |
| 382 | case ByteCodeInstruction::kUnsignedToFloat3: sp[-2].fFloat = sp[-2].fUnsigned; |
| 383 | case ByteCodeInstruction::kUnsignedToFloat2: sp[-1].fFloat = sp[-1].fUnsigned; |
| 384 | case ByteCodeInstruction::kUnsignedToFloat : sp[ 0].fFloat = sp[ 0].fUnsigned; |
| 385 | break; |
| 386 | |
| 387 | case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3]; |
| 388 | case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2]; |
| 389 | case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1]; |
| 390 | case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0]; |
| 391 | ++ip; |
| 392 | sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1; |
| 393 | break; |
| 394 | |
| 395 | case ByteCodeInstruction::kLoadGlobal4: sp[4] = fGlobals[*ip + 3]; |
| 396 | case ByteCodeInstruction::kLoadGlobal3: sp[3] = fGlobals[*ip + 2]; |
| 397 | case ByteCodeInstruction::kLoadGlobal2: sp[2] = fGlobals[*ip + 1]; |
| 398 | case ByteCodeInstruction::kLoadGlobal : sp[1] = fGlobals[*ip + 0]; |
| 399 | ++ip; |
| 400 | sp += (int)inst - (int)ByteCodeInstruction::kLoadGlobal + 1; |
| 401 | break; |
| 402 | |
| 403 | case ByteCodeInstruction::kLoadSwizzle: { |
| 404 | int src = READ8(); |
| 405 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 406 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 407 | PUSH(stack[src + *(ip + i)]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 408 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 409 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 410 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 411 | } |
| 412 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 413 | int src = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 414 | SkASSERT(src < (int) fGlobals.size()); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 415 | int count = READ8(); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 416 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 417 | PUSH(fGlobals[src + *(ip + i)]); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 418 | } |
| 419 | ip += count; |
| 420 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 421 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 422 | VECTOR_BINARY_OP(kMultiplyI, int32_t, fSigned, fSigned, *) |
| 423 | VECTOR_BINARY_OP(kMultiplyF, float, fFloat, fFloat, *) |
| 424 | case ByteCodeInstruction::kNot: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 425 | sp[0].fBool = !sp[0].fBool; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 426 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 427 | |
| 428 | case ByteCodeInstruction::kNegateF4: sp[-3].fFloat = -sp[-3].fFloat; |
| 429 | case ByteCodeInstruction::kNegateF3: sp[-2].fFloat = -sp[-2].fFloat; |
| 430 | case ByteCodeInstruction::kNegateF2: sp[-1].fFloat = -sp[-1].fFloat; |
| 431 | case ByteCodeInstruction::kNegateF : sp[ 0].fFloat = -sp[ 0].fFloat; |
| 432 | break; |
| 433 | |
| 434 | case ByteCodeInstruction::kNegateS4: sp[-3].fSigned = -sp[-3].fSigned; |
| 435 | case ByteCodeInstruction::kNegateS3: sp[-2].fSigned = -sp[-2].fSigned; |
| 436 | case ByteCodeInstruction::kNegateS2: sp[-1].fSigned = -sp[-1].fSigned; |
| 437 | case ByteCodeInstruction::kNegateS : sp[ 0].fSigned = -sp [0].fSigned; |
| 438 | break; |
| 439 | |
| 440 | case ByteCodeInstruction::kPop4: POP(); |
| 441 | case ByteCodeInstruction::kPop3: POP(); |
| 442 | case ByteCodeInstruction::kPop2: POP(); |
| 443 | case ByteCodeInstruction::kPop : POP(); |
| 444 | break; |
| 445 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 446 | case ByteCodeInstruction::kPushImmediate: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 447 | PUSH(READ32()); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 448 | break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 449 | case ByteCodeInstruction::kReadExternal: // fall through |
| 450 | case ByteCodeInstruction::kReadExternal2: // fall through |
| 451 | case ByteCodeInstruction::kReadExternal3: // fall through |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 452 | case ByteCodeInstruction::kReadExternal4: { |
| 453 | int src = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 454 | fByteCode->fExternalValues[src]->read(sp + 1); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 455 | sp += (int) inst - (int) ByteCodeInstruction::kReadExternal + 1; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 456 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 457 | } |
Ethan Nicholas | aeb71ce | 2019-05-20 09:55:44 -0400 | [diff] [blame] | 458 | VECTOR_BINARY_FN(kRemainderF, int32_t, fFloat, fFloat, fmodf) |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 459 | VECTOR_BINARY_OP(kRemainderS, int32_t, fSigned, fSigned, %) |
| 460 | VECTOR_BINARY_OP(kRemainderU, uint32_t, fUnsigned, fUnsigned, %) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 461 | case ByteCodeInstruction::kReturn: { |
| 462 | int count = READ8(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 463 | if (frames.empty()) { |
| 464 | if (outReturn) { |
| 465 | memcpy(outReturn, sp - count + 1, count * sizeof(Value)); |
| 466 | } |
| 467 | return; |
| 468 | } else { |
| 469 | // When we were called, 'stack' was positioned at the old top-of-stack (where |
| 470 | // our parameters were placed). So copy our return values to that same spot. |
| 471 | memmove(stack, sp - count + 1, count * sizeof(Value)); |
| 472 | |
| 473 | // Now move the stack pointer to the end of the just-pushed return values, |
| 474 | // and restore everything else. |
| 475 | const StackFrame& frame(frames.back()); |
| 476 | sp = stack + count - 1; |
| 477 | stack = frame.fStack; |
| 478 | code = frame.fCode; |
| 479 | ip = frame.fIP; |
| 480 | frames.pop_back(); |
| 481 | break; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 482 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 483 | } |
| 484 | |
| 485 | case ByteCodeInstruction::kStore4: stack[*ip + 3] = POP(); |
| 486 | case ByteCodeInstruction::kStore3: stack[*ip + 2] = POP(); |
| 487 | case ByteCodeInstruction::kStore2: stack[*ip + 1] = POP(); |
| 488 | case ByteCodeInstruction::kStore : stack[*ip + 0] = POP(); |
| 489 | ++ip; |
| 490 | break; |
| 491 | |
| 492 | case ByteCodeInstruction::kStoreGlobal4: fGlobals[*ip + 3] = POP(); |
| 493 | case ByteCodeInstruction::kStoreGlobal3: fGlobals[*ip + 2] = POP(); |
| 494 | case ByteCodeInstruction::kStoreGlobal2: fGlobals[*ip + 1] = POP(); |
| 495 | case ByteCodeInstruction::kStoreGlobal : fGlobals[*ip + 0] = POP(); |
| 496 | ++ip; |
| 497 | break; |
| 498 | |
| 499 | case ByteCodeInstruction::kStoreSwizzle: { |
| 500 | int target = READ8(); |
| 501 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 502 | for (int i = count - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 503 | stack[target + *(ip + i)] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 504 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 505 | ip += count; |
| 506 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 507 | } |
| 508 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 509 | int target = READ8(); |
| 510 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 511 | for (int i = count - 1; i >= 0; --i) { |
| 512 | fGlobals[target + *(ip + i)] = POP(); |
| 513 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 514 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 515 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 516 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 517 | VECTOR_BINARY_OP(kSubtractI, int32_t, fSigned, fSigned, -) |
| 518 | VECTOR_BINARY_OP(kSubtractF, float, fFloat, fFloat, -) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 519 | case ByteCodeInstruction::kSwizzle: { |
| 520 | Value tmp[4]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 521 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 522 | tmp[i] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 523 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 524 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 525 | PUSH(tmp[READ8()]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 526 | } |
| 527 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 528 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 529 | case ByteCodeInstruction::kWriteExternal: // fall through |
| 530 | case ByteCodeInstruction::kWriteExternal2: // fall through |
| 531 | case ByteCodeInstruction::kWriteExternal3: // fall through |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 532 | case ByteCodeInstruction::kWriteExternal4: { |
| 533 | int count = (int) inst - (int) ByteCodeInstruction::kWriteExternal + 1; |
| 534 | int target = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 535 | fByteCode->fExternalValues[target]->write(sp - count + 1); |
| 536 | sp -= count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 537 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 538 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 539 | default: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 540 | SkDEBUGFAILF("unsupported instruction %d\n", (int) inst); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 541 | } |
| 542 | #ifdef TRACE |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame^] | 543 | int stackSize = (int) (sp - stack + 1); |
| 544 | printf("STACK(%d):", stackSize); |
| 545 | for (int i = 0; i < stackSize(); ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 546 | printf(" %d(%f)", stack[i].fSigned, stack[i].fFloat); |
| 547 | } |
| 548 | printf("\n"); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 549 | #endif |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 550 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | } // namespace |
| 554 | |
| 555 | #endif |