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())); |
Mike Klein | 108e935 | 2019-05-21 11:05:17 -0500 | [diff] [blame] | 98 | switch ((ByteCodeInstruction) READ16()) { |
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; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 131 | VECTOR_DISASSEMBLE(kConvertFtoI, "convertftoi") |
| 132 | VECTOR_DISASSEMBLE(kConvertStoF, "convertstof") |
| 133 | VECTOR_DISASSEMBLE(kConvertUtoF, "convertutof") |
| 134 | VECTOR_DISASSEMBLE(kCos, "cos") |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 135 | case ByteCodeInstruction::kDebugPrint: printf("debugprint"); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 136 | VECTOR_DISASSEMBLE(kDivideF, "dividef") |
| 137 | VECTOR_DISASSEMBLE(kDivideS, "divideS") |
| 138 | VECTOR_DISASSEMBLE(kDivideU, "divideu") |
| 139 | VECTOR_DISASSEMBLE(kDup, "dup") |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 140 | case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 141 | case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break; |
| 142 | case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break; |
| 143 | case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 144 | case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 145 | case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break; |
| 146 | case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break; |
| 147 | case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 148 | case ByteCodeInstruction::kLoadSwizzle: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 149 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 150 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 151 | printf("loadswizzle %d %d", target, count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 152 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 153 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 154 | } |
| 155 | break; |
| 156 | } |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 157 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 158 | int target = READ8(); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 159 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 160 | printf("loadswizzleglobal %d %d", target, count); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 161 | for (int i = 0; i < count; ++i) { |
| 162 | printf(", %d", READ8()); |
| 163 | } |
| 164 | break; |
| 165 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 166 | VECTOR_DISASSEMBLE(kMultiplyF, "multiplyf") |
| 167 | VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi") |
| 168 | VECTOR_DISASSEMBLE(kNegateF, "negatef") |
Mike Klein | 1271091 | 2019-05-21 11:04:59 -0500 | [diff] [blame] | 169 | VECTOR_DISASSEMBLE(kNegateI, "negatei") |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 170 | VECTOR_DISASSEMBLE(kNot, "not") |
| 171 | VECTOR_DISASSEMBLE(kOrB, "orb") |
| 172 | VECTOR_DISASSEMBLE(kOrI, "ori") |
| 173 | VECTOR_DISASSEMBLE(kPop, "pop") |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 174 | case ByteCodeInstruction::kPushImmediate: { |
| 175 | uint32_t v = READ32(); |
| 176 | union { uint32_t u; float f; } pun = { v }; |
| 177 | printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 178 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 179 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 180 | case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 181 | case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break; |
| 182 | case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break; |
| 183 | case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break; |
| 184 | VECTOR_DISASSEMBLE(kRemainderF, "remainderf") |
| 185 | VECTOR_DISASSEMBLE(kRemainderS, "remainders") |
| 186 | VECTOR_DISASSEMBLE(kRemainderU, "remainderu") |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 187 | case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 188 | VECTOR_DISASSEMBLE(kSin, "sin") |
| 189 | VECTOR_DISASSEMBLE(kSqrt, "sqrt") |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 190 | case ByteCodeInstruction::kStore: printf("store %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 191 | case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break; |
| 192 | case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break; |
| 193 | case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 194 | case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 195 | case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break; |
| 196 | case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break; |
| 197 | case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 198 | case ByteCodeInstruction::kStoreSwizzle: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 199 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 200 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 201 | printf("storeswizzle %d %d", target, count); |
| 202 | for (int i = 0; i < count; ++i) { |
| 203 | printf(", %d", READ8()); |
| 204 | } |
| 205 | break; |
| 206 | } |
| 207 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 208 | int target = READ8(); |
| 209 | int count = READ8(); |
| 210 | printf("storeswizzleglobal %d %d", target, count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 211 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 212 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 213 | } |
| 214 | break; |
| 215 | } |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 216 | VECTOR_DISASSEMBLE(kSubtractF, "subtractf") |
| 217 | VECTOR_DISASSEMBLE(kSubtractI, "subtracti") |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 218 | case ByteCodeInstruction::kSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 219 | printf("swizzle %d, ", READ8()); |
| 220 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 221 | printf("%d", count); |
| 222 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 223 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 224 | } |
| 225 | break; |
| 226 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 227 | VECTOR_DISASSEMBLE(kTan, "tan") |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 228 | case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 229 | case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break; |
| 230 | case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break; |
| 231 | case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 232 | default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 233 | } |
| 234 | printf("\n"); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 235 | } |
| 236 | } |
| 237 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 238 | #define VECTOR_BINARY_OP(base, field, op) \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 239 | case ByteCodeInstruction::base ## 4: \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 240 | sp[-4] = sp[-4].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 241 | POP(); \ |
| 242 | /* fall through */ \ |
| 243 | case ByteCodeInstruction::base ## 3: { \ |
| 244 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 245 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 246 | POP(); \ |
| 247 | } /* fall through */ \ |
| 248 | case ByteCodeInstruction::base ## 2: { \ |
| 249 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 250 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 251 | POP(); \ |
| 252 | } /* fall through */ \ |
| 253 | case ByteCodeInstruction::base: { \ |
| 254 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 255 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 256 | POP(); \ |
| 257 | break; \ |
| 258 | } |
Ethan Nicholas | aeb71ce | 2019-05-20 09:55:44 -0400 | [diff] [blame] | 259 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 260 | #define VECTOR_BINARY_FN(base, field, fn) \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 261 | case ByteCodeInstruction::base ## 4: \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 262 | sp[-4] = fn(sp[-4].field, sp[0].field); \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 263 | POP(); \ |
| 264 | /* fall through */ \ |
| 265 | case ByteCodeInstruction::base ## 3: { \ |
| 266 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 267 | sp[count] = fn(sp[count].field, sp[0].field); \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 268 | POP(); \ |
| 269 | } /* fall through */ \ |
| 270 | case ByteCodeInstruction::base ## 2: { \ |
| 271 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 272 | sp[count] = fn(sp[count].field, sp[0].field); \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 273 | POP(); \ |
| 274 | } /* fall through */ \ |
| 275 | case ByteCodeInstruction::base: { \ |
| 276 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 277 | sp[count] = fn(sp[count].field, sp[0].field); \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 278 | POP(); \ |
| 279 | break; \ |
| 280 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 281 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 282 | #define VECTOR_UNARY_FN(base, fn, field) \ |
| 283 | case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \ |
| 284 | case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \ |
| 285 | case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \ |
| 286 | case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \ |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 287 | break; |
| 288 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 289 | struct StackFrame { |
| 290 | const uint8_t* fCode; |
| 291 | const uint8_t* fIP; |
| 292 | Interpreter::Value* fStack; |
| 293 | }; |
| 294 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 295 | void Interpreter::innerRun(const ByteCodeFunction& f, Value* stack, Value* outReturn) { |
| 296 | Value* sp = stack + f.fParameterCount + f.fLocalCount - 1; |
| 297 | |
| 298 | auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); }; |
| 299 | auto PUSH = [&](Value v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; }; |
| 300 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 301 | const uint8_t* code = f.fCode.data(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 302 | const uint8_t* ip = code; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 303 | std::vector<StackFrame> frames; |
| 304 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 305 | for (;;) { |
Mike Klein | 108e935 | 2019-05-21 11:05:17 -0500 | [diff] [blame] | 306 | ByteCodeInstruction inst = (ByteCodeInstruction) READ16(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 307 | #ifdef TRACE |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 308 | printf("at %d\n", (int) (ip - code - 1)); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 309 | #endif |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 310 | switch (inst) { |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 311 | VECTOR_BINARY_OP(kAddI, fSigned, +) |
| 312 | VECTOR_BINARY_OP(kAddF, fFloat, +) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 313 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 314 | case ByteCodeInstruction::kBranch: |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 315 | ip = code + READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 316 | break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 317 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 318 | case ByteCodeInstruction::kCall: { |
| 319 | // Precursor code has pushed all parameters to the stack. Update our bottom of |
| 320 | // stack to point at the first parameter, and our sp to point past those parameters |
| 321 | // (plus space for locals). |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 322 | int target = READ8(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 323 | const ByteCodeFunction* fun = fByteCode->fFunctions[target].get(); |
| 324 | frames.push_back({ code, ip, stack }); |
| 325 | ip = code = fun->fCode.data(); |
| 326 | stack = sp - fun->fParameterCount + 1; |
| 327 | sp = stack + fun->fParameterCount + fun->fLocalCount - 1; |
| 328 | break; |
| 329 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 330 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 331 | case ByteCodeInstruction::kCallExternal: { |
| 332 | int argumentCount = READ8(); |
| 333 | int returnCount = READ8(); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 334 | int target = READ8(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 335 | ExternalValue* v = fByteCode->fExternalValues[target]; |
| 336 | sp -= argumentCount - 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 337 | |
| 338 | Value tmp[4]; |
| 339 | SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmp)); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 340 | v->call(sp, tmp); |
| 341 | memcpy(sp, tmp, returnCount * sizeof(Value)); |
| 342 | sp += returnCount - 1; |
| 343 | break; |
| 344 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 345 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 346 | VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==) |
| 347 | VECTOR_BINARY_OP(kCompareFEQ, fFloat, ==) |
| 348 | VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=) |
| 349 | VECTOR_BINARY_OP(kCompareFNEQ, fFloat, !=) |
| 350 | VECTOR_BINARY_OP(kCompareSGT, fSigned, >) |
| 351 | VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >) |
| 352 | VECTOR_BINARY_OP(kCompareFGT, fFloat, >) |
| 353 | VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=) |
| 354 | VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=) |
| 355 | VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=) |
| 356 | VECTOR_BINARY_OP(kCompareSLT, fSigned, <) |
| 357 | VECTOR_BINARY_OP(kCompareULT, fUnsigned, <) |
| 358 | VECTOR_BINARY_OP(kCompareFLT, fFloat, <) |
| 359 | VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=) |
| 360 | VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=) |
| 361 | VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 362 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 363 | case ByteCodeInstruction::kConditionalBranch: { |
| 364 | int target = READ16(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 365 | if (POP().fBool) { |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 366 | ip = code + target; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 367 | } |
| 368 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 369 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 370 | |
| 371 | case ByteCodeInstruction::kConvertFtoI4: sp[-3].fSigned = (int)sp[-3].fFloat; |
| 372 | case ByteCodeInstruction::kConvertFtoI3: sp[-2].fSigned = (int)sp[-2].fFloat; |
| 373 | case ByteCodeInstruction::kConvertFtoI2: sp[-1].fSigned = (int)sp[-1].fFloat; |
| 374 | case ByteCodeInstruction::kConvertFtoI: sp[ 0].fSigned = (int)sp[ 0].fFloat; |
| 375 | break; |
| 376 | |
| 377 | case ByteCodeInstruction::kConvertStoF4: sp[-3].fFloat = sp[-3].fSigned; |
| 378 | case ByteCodeInstruction::kConvertStoF3: sp[-2].fFloat = sp[-2].fSigned; |
| 379 | case ByteCodeInstruction::kConvertStoF2: sp[-1].fFloat = sp[-1].fSigned; |
| 380 | case ByteCodeInstruction::kConvertStoF : sp[ 0].fFloat = sp[ 0].fSigned; |
| 381 | break; |
| 382 | |
| 383 | case ByteCodeInstruction::kConvertUtoF4: sp[-3].fFloat = sp[-3].fUnsigned; |
| 384 | case ByteCodeInstruction::kConvertUtoF3: sp[-2].fFloat = sp[-2].fUnsigned; |
| 385 | case ByteCodeInstruction::kConvertUtoF2: sp[-1].fFloat = sp[-1].fUnsigned; |
| 386 | case ByteCodeInstruction::kConvertUtoF : sp[ 0].fFloat = sp[ 0].fUnsigned; |
| 387 | break; |
| 388 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 389 | VECTOR_UNARY_FN(kCos, cosf, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 390 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 391 | case ByteCodeInstruction::kDebugPrint: { |
| 392 | Value v = POP(); |
| 393 | 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] | 394 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 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(kDivideS, fSigned, /) |
| 398 | VECTOR_BINARY_OP(kDivideU, fUnsigned, /) |
| 399 | VECTOR_BINARY_OP(kDivideF, fFloat, /) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 400 | |
| 401 | case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 402 | case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 403 | case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 404 | case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 405 | break; |
| 406 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 407 | case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3]; |
| 408 | case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2]; |
| 409 | case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1]; |
| 410 | case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0]; |
| 411 | ++ip; |
| 412 | sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1; |
| 413 | break; |
| 414 | |
| 415 | case ByteCodeInstruction::kLoadGlobal4: sp[4] = fGlobals[*ip + 3]; |
| 416 | case ByteCodeInstruction::kLoadGlobal3: sp[3] = fGlobals[*ip + 2]; |
| 417 | case ByteCodeInstruction::kLoadGlobal2: sp[2] = fGlobals[*ip + 1]; |
| 418 | case ByteCodeInstruction::kLoadGlobal : sp[1] = fGlobals[*ip + 0]; |
| 419 | ++ip; |
| 420 | sp += (int)inst - (int)ByteCodeInstruction::kLoadGlobal + 1; |
| 421 | break; |
| 422 | |
| 423 | case ByteCodeInstruction::kLoadSwizzle: { |
| 424 | int src = READ8(); |
| 425 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 426 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 427 | PUSH(stack[src + *(ip + i)]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 428 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 429 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 430 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 431 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 432 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 433 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 434 | int src = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 435 | SkASSERT(src < (int) fGlobals.size()); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 436 | int count = READ8(); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 437 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 438 | PUSH(fGlobals[src + *(ip + i)]); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 439 | } |
| 440 | ip += count; |
| 441 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 442 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 443 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 444 | VECTOR_BINARY_OP(kMultiplyI, fSigned, *) |
| 445 | VECTOR_BINARY_OP(kMultiplyF, fFloat, *) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 446 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 447 | case ByteCodeInstruction::kNot: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 448 | sp[0].fBool = !sp[0].fBool; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 449 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 450 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 451 | case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat; |
| 452 | case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat; |
| 453 | case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat; |
| 454 | case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 455 | break; |
| 456 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 457 | case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned; |
| 458 | case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned; |
| 459 | case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned; |
| 460 | case ByteCodeInstruction::kNegateI : sp[ 0] = -sp [0].fSigned; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 461 | break; |
| 462 | |
| 463 | case ByteCodeInstruction::kPop4: POP(); |
| 464 | case ByteCodeInstruction::kPop3: POP(); |
| 465 | case ByteCodeInstruction::kPop2: POP(); |
| 466 | case ByteCodeInstruction::kPop : POP(); |
| 467 | break; |
| 468 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 469 | case ByteCodeInstruction::kPushImmediate: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 470 | PUSH(READ32()); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 471 | break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 472 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 473 | case ByteCodeInstruction::kReadExternal: // fall through |
| 474 | case ByteCodeInstruction::kReadExternal2: // fall through |
| 475 | case ByteCodeInstruction::kReadExternal3: // fall through |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 476 | case ByteCodeInstruction::kReadExternal4: { |
| 477 | int src = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 478 | fByteCode->fExternalValues[src]->read(sp + 1); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 479 | sp += (int) inst - (int) ByteCodeInstruction::kReadExternal + 1; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 480 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 481 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 482 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 483 | VECTOR_BINARY_FN(kRemainderF, fFloat, fmodf) |
| 484 | VECTOR_BINARY_OP(kRemainderS, fSigned, %) |
| 485 | VECTOR_BINARY_OP(kRemainderU, fUnsigned, %) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 486 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 487 | case ByteCodeInstruction::kReturn: { |
| 488 | int count = READ8(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 489 | if (frames.empty()) { |
| 490 | if (outReturn) { |
| 491 | memcpy(outReturn, sp - count + 1, count * sizeof(Value)); |
| 492 | } |
| 493 | return; |
| 494 | } else { |
| 495 | // When we were called, 'stack' was positioned at the old top-of-stack (where |
| 496 | // our parameters were placed). So copy our return values to that same spot. |
| 497 | memmove(stack, sp - count + 1, count * sizeof(Value)); |
| 498 | |
| 499 | // Now move the stack pointer to the end of the just-pushed return values, |
| 500 | // and restore everything else. |
| 501 | const StackFrame& frame(frames.back()); |
| 502 | sp = stack + count - 1; |
| 503 | stack = frame.fStack; |
| 504 | code = frame.fCode; |
| 505 | ip = frame.fIP; |
| 506 | frames.pop_back(); |
| 507 | break; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 508 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 509 | } |
| 510 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 511 | VECTOR_UNARY_FN(kSin, sinf, fFloat) |
| 512 | VECTOR_UNARY_FN(kSqrt, sqrtf, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 513 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 514 | case ByteCodeInstruction::kStore4: stack[*ip + 3] = POP(); |
| 515 | case ByteCodeInstruction::kStore3: stack[*ip + 2] = POP(); |
| 516 | case ByteCodeInstruction::kStore2: stack[*ip + 1] = POP(); |
| 517 | case ByteCodeInstruction::kStore : stack[*ip + 0] = POP(); |
| 518 | ++ip; |
| 519 | break; |
| 520 | |
| 521 | case ByteCodeInstruction::kStoreGlobal4: fGlobals[*ip + 3] = POP(); |
| 522 | case ByteCodeInstruction::kStoreGlobal3: fGlobals[*ip + 2] = POP(); |
| 523 | case ByteCodeInstruction::kStoreGlobal2: fGlobals[*ip + 1] = POP(); |
| 524 | case ByteCodeInstruction::kStoreGlobal : fGlobals[*ip + 0] = POP(); |
| 525 | ++ip; |
| 526 | break; |
| 527 | |
| 528 | case ByteCodeInstruction::kStoreSwizzle: { |
| 529 | int target = READ8(); |
| 530 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 531 | for (int i = count - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 532 | stack[target + *(ip + i)] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 533 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 534 | ip += count; |
| 535 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 536 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 537 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 538 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 539 | int target = READ8(); |
| 540 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 541 | for (int i = count - 1; i >= 0; --i) { |
| 542 | fGlobals[target + *(ip + i)] = POP(); |
| 543 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 544 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 545 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 546 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 547 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 548 | VECTOR_BINARY_OP(kSubtractI, fSigned, -) |
| 549 | VECTOR_BINARY_OP(kSubtractF, fFloat, -) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 550 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 551 | case ByteCodeInstruction::kSwizzle: { |
| 552 | Value tmp[4]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 553 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 554 | tmp[i] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 555 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 556 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 557 | PUSH(tmp[READ8()]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 558 | } |
| 559 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 560 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 561 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame^] | 562 | VECTOR_UNARY_FN(kTan, tanf, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 563 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 564 | case ByteCodeInstruction::kWriteExternal: // fall through |
| 565 | case ByteCodeInstruction::kWriteExternal2: // fall through |
| 566 | case ByteCodeInstruction::kWriteExternal3: // fall through |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 567 | case ByteCodeInstruction::kWriteExternal4: { |
| 568 | int count = (int) inst - (int) ByteCodeInstruction::kWriteExternal + 1; |
| 569 | int target = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 570 | fByteCode->fExternalValues[target]->write(sp - count + 1); |
| 571 | sp -= count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 572 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 573 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 574 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 575 | default: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 576 | SkDEBUGFAILF("unsupported instruction %d\n", (int) inst); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 577 | } |
| 578 | #ifdef TRACE |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 579 | int stackSize = (int) (sp - stack + 1); |
| 580 | printf("STACK(%d):", stackSize); |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 581 | for (int i = 0; i < stackSize; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 582 | printf(" %d(%f)", stack[i].fSigned, stack[i].fFloat); |
| 583 | } |
| 584 | printf("\n"); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 585 | #endif |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 586 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | } // namespace |
| 590 | |
| 591 | #endif |