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" |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 11 | #include "src/sksl/SkSLByteCodeGenerator.h" |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 12 | #include "src/sksl/SkSLExternalValue.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/sksl/SkSLInterpreter.h" |
| 14 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 15 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 16 | #include "src/sksl/ir/SkSLForStatement.h" |
| 17 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 18 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 19 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 20 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 21 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 22 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 23 | #include "src/sksl/ir/SkSLProgram.h" |
| 24 | #include "src/sksl/ir/SkSLStatement.h" |
| 25 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 26 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 27 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 28 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 29 | |
| 30 | namespace SkSL { |
| 31 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 32 | static constexpr int UNINITIALIZED = 0xDEADBEEF; |
| 33 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 34 | Interpreter::Interpreter(std::unique_ptr<Program> program, |
| 35 | std::unique_ptr<ByteCode> byteCode, |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 36 | Interpreter::Value inputs[]) |
| 37 | : fProgram(std::move(program)) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 38 | , fByteCode(std::move(byteCode)) |
| 39 | , fGlobals(fByteCode->fGlobalCount, UNINITIALIZED) { |
Brian Osman | d369a5e | 2019-05-09 13:13:25 -0400 | [diff] [blame] | 40 | this->setInputs(inputs); |
| 41 | } |
| 42 | |
| 43 | void Interpreter::setInputs(Interpreter::Value inputs[]) { |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 44 | for (uint8_t slot : fByteCode->fInputSlots) { |
| 45 | fGlobals[slot] = *inputs++; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | void Interpreter::run(const ByteCodeFunction& f, Interpreter::Value args[], |
| 50 | Interpreter::Value* outReturn) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 51 | #ifdef TRACE |
| 52 | this->disassemble(f); |
| 53 | #endif |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 54 | Value smallStack[128]; |
| 55 | std::unique_ptr<Value[]> largeStack; |
| 56 | Value* stack = smallStack; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 57 | if ((int) SK_ARRAY_COUNT(smallStack) < f.fStackCount) { |
| 58 | largeStack.reset(new Value[f.fStackCount]); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 59 | stack = largeStack.get(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 60 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 61 | |
| 62 | if (f.fParameterCount) { |
| 63 | memcpy(stack, args, f.fParameterCount * sizeof(Value)); |
| 64 | } |
| 65 | this->innerRun(f, stack, outReturn); |
| 66 | |
| 67 | for (const Variable* p : f.fDeclaration.fParameters) { |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 68 | const int nvalues = ByteCodeGenerator::SlotCount(p->fType); |
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 | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 140 | case ByteCodeInstruction::kDupN: printf("dupN %d", READ8()); break; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 141 | case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 142 | case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break; |
| 143 | case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break; |
| 144 | case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 145 | case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 146 | case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break; |
| 147 | case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break; |
| 148 | case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 149 | case ByteCodeInstruction::kLoadSwizzle: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 150 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 151 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 152 | printf("loadswizzle %d %d", target, count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 153 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 154 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 155 | } |
| 156 | break; |
| 157 | } |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 158 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 159 | int target = READ8(); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 160 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 161 | printf("loadswizzleglobal %d %d", target, count); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 162 | for (int i = 0; i < count; ++i) { |
| 163 | printf(", %d", READ8()); |
| 164 | } |
| 165 | break; |
| 166 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 167 | case ByteCodeInstruction::kLoadExtended: printf("loadextended %d", READ8()); break; |
| 168 | case ByteCodeInstruction::kLoadExtendedGlobal: printf("loadextendedglobal %d", READ8()); |
| 169 | break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 170 | VECTOR_DISASSEMBLE(kMultiplyF, "multiplyf") |
| 171 | VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi") |
| 172 | VECTOR_DISASSEMBLE(kNegateF, "negatef") |
Mike Klein | 1271091 | 2019-05-21 11:04:59 -0500 | [diff] [blame] | 173 | VECTOR_DISASSEMBLE(kNegateI, "negatei") |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 174 | VECTOR_DISASSEMBLE(kNot, "not") |
| 175 | VECTOR_DISASSEMBLE(kOrB, "orb") |
| 176 | VECTOR_DISASSEMBLE(kOrI, "ori") |
| 177 | VECTOR_DISASSEMBLE(kPop, "pop") |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 178 | case ByteCodeInstruction::kPopN: printf("popN %d", READ8()); break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 179 | case ByteCodeInstruction::kPushImmediate: { |
| 180 | uint32_t v = READ32(); |
| 181 | union { uint32_t u; float f; } pun = { v }; |
| 182 | printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 183 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 184 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 185 | case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 186 | case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break; |
| 187 | case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break; |
| 188 | case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break; |
| 189 | VECTOR_DISASSEMBLE(kRemainderF, "remainderf") |
| 190 | VECTOR_DISASSEMBLE(kRemainderS, "remainders") |
| 191 | VECTOR_DISASSEMBLE(kRemainderU, "remainderu") |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 192 | case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 193 | VECTOR_DISASSEMBLE(kSin, "sin") |
| 194 | VECTOR_DISASSEMBLE(kSqrt, "sqrt") |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 195 | case ByteCodeInstruction::kStore: printf("store %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 196 | case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break; |
| 197 | case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break; |
| 198 | case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 199 | case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 200 | case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break; |
| 201 | case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break; |
| 202 | case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 203 | case ByteCodeInstruction::kStoreSwizzle: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 204 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 205 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 206 | printf("storeswizzle %d %d", target, count); |
| 207 | for (int i = 0; i < count; ++i) { |
| 208 | printf(", %d", READ8()); |
| 209 | } |
| 210 | break; |
| 211 | } |
| 212 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 213 | int target = READ8(); |
| 214 | int count = READ8(); |
| 215 | printf("storeswizzleglobal %d %d", target, count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 216 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 217 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 218 | } |
| 219 | break; |
| 220 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 221 | case ByteCodeInstruction::kStoreSwizzleIndirect: { |
| 222 | int count = READ8(); |
| 223 | printf("storeswizzleindirect %d", count); |
| 224 | for (int i = 0; i < count; ++i) { |
| 225 | printf(", %d", READ8()); |
| 226 | } |
| 227 | break; |
| 228 | } |
| 229 | case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: { |
| 230 | int count = READ8(); |
| 231 | printf("storeswizzleindirectglobal %d", count); |
| 232 | for (int i = 0; i < count; ++i) { |
| 233 | printf(", %d", READ8()); |
| 234 | } |
| 235 | break; |
| 236 | } |
| 237 | case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break; |
| 238 | case ByteCodeInstruction::kStoreExtendedGlobal: |
| 239 | printf("storeextendedglobal %d", READ8()); |
| 240 | break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 241 | VECTOR_DISASSEMBLE(kSubtractF, "subtractf") |
| 242 | VECTOR_DISASSEMBLE(kSubtractI, "subtracti") |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 243 | case ByteCodeInstruction::kSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 244 | printf("swizzle %d, ", READ8()); |
| 245 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 246 | printf("%d", count); |
| 247 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 248 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 249 | } |
| 250 | break; |
| 251 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 252 | VECTOR_DISASSEMBLE(kTan, "tan") |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 253 | case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break; |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 254 | case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break; |
| 255 | case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break; |
| 256 | case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 257 | default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 258 | } |
| 259 | printf("\n"); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 263 | #define VECTOR_BINARY_OP(base, field, op) \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 264 | case ByteCodeInstruction::base ## 4: \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 265 | sp[-4] = sp[-4].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 266 | POP(); \ |
| 267 | /* fall through */ \ |
| 268 | case ByteCodeInstruction::base ## 3: { \ |
| 269 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 270 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 271 | POP(); \ |
| 272 | } /* fall through */ \ |
| 273 | case ByteCodeInstruction::base ## 2: { \ |
| 274 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 275 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 276 | POP(); \ |
| 277 | } /* fall through */ \ |
| 278 | case ByteCodeInstruction::base: { \ |
| 279 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 280 | sp[count] = sp[count].field op sp[0].field; \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 281 | POP(); \ |
| 282 | break; \ |
| 283 | } |
Ethan Nicholas | aeb71ce | 2019-05-20 09:55:44 -0400 | [diff] [blame] | 284 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 285 | #define VECTOR_BINARY_FN(base, field, fn) \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 286 | case ByteCodeInstruction::base ## 4: \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 287 | sp[-4] = fn(sp[-4].field, sp[0].field); \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 288 | POP(); \ |
| 289 | /* fall through */ \ |
| 290 | case ByteCodeInstruction::base ## 3: { \ |
| 291 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 292 | sp[count] = fn(sp[count].field, sp[0].field); \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 293 | POP(); \ |
| 294 | } /* fall through */ \ |
| 295 | case ByteCodeInstruction::base ## 2: { \ |
| 296 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 297 | sp[count] = fn(sp[count].field, sp[0].field); \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 298 | POP(); \ |
| 299 | } /* fall through */ \ |
| 300 | case ByteCodeInstruction::base: { \ |
| 301 | int count = (int) ByteCodeInstruction::base - (int) inst - 1; \ |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 302 | sp[count] = fn(sp[count].field, sp[0].field); \ |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 303 | POP(); \ |
| 304 | break; \ |
| 305 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 306 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 307 | #define VECTOR_UNARY_FN(base, fn, field) \ |
| 308 | case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \ |
| 309 | case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \ |
| 310 | case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \ |
| 311 | case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \ |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 312 | break; |
| 313 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 314 | struct StackFrame { |
| 315 | const uint8_t* fCode; |
| 316 | const uint8_t* fIP; |
| 317 | Interpreter::Value* fStack; |
| 318 | }; |
| 319 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 320 | void Interpreter::innerRun(const ByteCodeFunction& f, Value* stack, Value* outReturn) { |
| 321 | Value* sp = stack + f.fParameterCount + f.fLocalCount - 1; |
| 322 | |
| 323 | auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); }; |
| 324 | auto PUSH = [&](Value v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; }; |
| 325 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 326 | const uint8_t* code = f.fCode.data(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 327 | const uint8_t* ip = code; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 328 | std::vector<StackFrame> frames; |
| 329 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 330 | for (;;) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 331 | #ifdef TRACE |
Brian Osman | e85b6a5 | 2019-05-22 14:50:59 -0700 | [diff] [blame] | 332 | printf("at %d\n", (int) (ip - code)); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 333 | #endif |
Brian Osman | e85b6a5 | 2019-05-22 14:50:59 -0700 | [diff] [blame] | 334 | ByteCodeInstruction inst = (ByteCodeInstruction) READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 335 | switch (inst) { |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 336 | VECTOR_BINARY_OP(kAddI, fSigned, +) |
| 337 | VECTOR_BINARY_OP(kAddF, fFloat, +) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 338 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 339 | case ByteCodeInstruction::kBranch: |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 340 | ip = code + READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 341 | break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 342 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 343 | case ByteCodeInstruction::kCall: { |
| 344 | // Precursor code has pushed all parameters to the stack. Update our bottom of |
| 345 | // stack to point at the first parameter, and our sp to point past those parameters |
| 346 | // (plus space for locals). |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 347 | int target = READ8(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 348 | const ByteCodeFunction* fun = fByteCode->fFunctions[target].get(); |
| 349 | frames.push_back({ code, ip, stack }); |
| 350 | ip = code = fun->fCode.data(); |
| 351 | stack = sp - fun->fParameterCount + 1; |
| 352 | sp = stack + fun->fParameterCount + fun->fLocalCount - 1; |
| 353 | break; |
| 354 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 355 | |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 356 | case ByteCodeInstruction::kCallExternal: { |
| 357 | int argumentCount = READ8(); |
| 358 | int returnCount = READ8(); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 359 | int target = READ8(); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 360 | ExternalValue* v = fByteCode->fExternalValues[target]; |
| 361 | sp -= argumentCount - 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 362 | |
| 363 | Value tmp[4]; |
| 364 | SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmp)); |
Ethan Nicholas | 9e6a393 | 2019-05-17 16:31:21 -0400 | [diff] [blame] | 365 | v->call(sp, tmp); |
| 366 | memcpy(sp, tmp, returnCount * sizeof(Value)); |
| 367 | sp += returnCount - 1; |
| 368 | break; |
| 369 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 370 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 371 | VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==) |
| 372 | VECTOR_BINARY_OP(kCompareFEQ, fFloat, ==) |
| 373 | VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=) |
| 374 | VECTOR_BINARY_OP(kCompareFNEQ, fFloat, !=) |
| 375 | VECTOR_BINARY_OP(kCompareSGT, fSigned, >) |
| 376 | VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >) |
| 377 | VECTOR_BINARY_OP(kCompareFGT, fFloat, >) |
| 378 | VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=) |
| 379 | VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=) |
| 380 | VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=) |
| 381 | VECTOR_BINARY_OP(kCompareSLT, fSigned, <) |
| 382 | VECTOR_BINARY_OP(kCompareULT, fUnsigned, <) |
| 383 | VECTOR_BINARY_OP(kCompareFLT, fFloat, <) |
| 384 | VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=) |
| 385 | VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=) |
| 386 | VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 387 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 388 | case ByteCodeInstruction::kConditionalBranch: { |
| 389 | int target = READ16(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 390 | if (POP().fBool) { |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 391 | ip = code + target; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 392 | } |
| 393 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 394 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 395 | |
| 396 | case ByteCodeInstruction::kConvertFtoI4: sp[-3].fSigned = (int)sp[-3].fFloat; |
| 397 | case ByteCodeInstruction::kConvertFtoI3: sp[-2].fSigned = (int)sp[-2].fFloat; |
| 398 | case ByteCodeInstruction::kConvertFtoI2: sp[-1].fSigned = (int)sp[-1].fFloat; |
| 399 | case ByteCodeInstruction::kConvertFtoI: sp[ 0].fSigned = (int)sp[ 0].fFloat; |
| 400 | break; |
| 401 | |
| 402 | case ByteCodeInstruction::kConvertStoF4: sp[-3].fFloat = sp[-3].fSigned; |
| 403 | case ByteCodeInstruction::kConvertStoF3: sp[-2].fFloat = sp[-2].fSigned; |
| 404 | case ByteCodeInstruction::kConvertStoF2: sp[-1].fFloat = sp[-1].fSigned; |
| 405 | case ByteCodeInstruction::kConvertStoF : sp[ 0].fFloat = sp[ 0].fSigned; |
| 406 | break; |
| 407 | |
| 408 | case ByteCodeInstruction::kConvertUtoF4: sp[-3].fFloat = sp[-3].fUnsigned; |
| 409 | case ByteCodeInstruction::kConvertUtoF3: sp[-2].fFloat = sp[-2].fUnsigned; |
| 410 | case ByteCodeInstruction::kConvertUtoF2: sp[-1].fFloat = sp[-1].fUnsigned; |
| 411 | case ByteCodeInstruction::kConvertUtoF : sp[ 0].fFloat = sp[ 0].fUnsigned; |
| 412 | break; |
| 413 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 414 | VECTOR_UNARY_FN(kCos, cosf, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 415 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 416 | case ByteCodeInstruction::kDebugPrint: { |
| 417 | Value v = POP(); |
| 418 | 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] | 419 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 420 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 421 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 422 | VECTOR_BINARY_OP(kDivideS, fSigned, /) |
| 423 | VECTOR_BINARY_OP(kDivideU, fUnsigned, /) |
| 424 | VECTOR_BINARY_OP(kDivideF, fFloat, /) |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 425 | |
| 426 | case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 427 | case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 428 | case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 429 | case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]); |
| 430 | break; |
| 431 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 432 | case ByteCodeInstruction::kDupN: { |
| 433 | int count = READ8(); |
| 434 | memcpy(sp + 1, sp - count + 1, count * sizeof(Value)); |
| 435 | sp += count; |
| 436 | break; |
| 437 | } |
| 438 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 439 | case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3]; |
| 440 | case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2]; |
| 441 | case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1]; |
| 442 | case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0]; |
| 443 | ++ip; |
| 444 | sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1; |
| 445 | break; |
| 446 | |
| 447 | case ByteCodeInstruction::kLoadGlobal4: sp[4] = fGlobals[*ip + 3]; |
| 448 | case ByteCodeInstruction::kLoadGlobal3: sp[3] = fGlobals[*ip + 2]; |
| 449 | case ByteCodeInstruction::kLoadGlobal2: sp[2] = fGlobals[*ip + 1]; |
| 450 | case ByteCodeInstruction::kLoadGlobal : sp[1] = fGlobals[*ip + 0]; |
| 451 | ++ip; |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 452 | sp += (int)inst - |
| 453 | (int)ByteCodeInstruction::kLoadGlobal + 1; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 454 | break; |
| 455 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 456 | case ByteCodeInstruction::kLoadExtended: { |
| 457 | int count = READ8(); |
| 458 | int src = POP().fSigned; |
| 459 | memcpy(sp + 1, &stack[src], count * sizeof(Value)); |
| 460 | sp += count; |
| 461 | break; |
| 462 | } |
| 463 | |
| 464 | case ByteCodeInstruction::kLoadExtendedGlobal: { |
| 465 | int count = READ8(); |
| 466 | int src = POP().fSigned; |
| 467 | SkASSERT(src + count <= (int) fGlobals.size()); |
| 468 | memcpy(sp + 1, &fGlobals[src], count * sizeof(Value)); |
| 469 | sp += count; |
| 470 | break; |
| 471 | } |
| 472 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 473 | case ByteCodeInstruction::kLoadSwizzle: { |
| 474 | int src = READ8(); |
| 475 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 476 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 477 | PUSH(stack[src + *(ip + i)]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 478 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 479 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43: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 | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 483 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 484 | int src = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 485 | SkASSERT(src < (int) fGlobals.size()); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 486 | int count = READ8(); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 487 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 488 | PUSH(fGlobals[src + *(ip + i)]); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 489 | } |
| 490 | ip += count; |
| 491 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 492 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 493 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 494 | VECTOR_BINARY_OP(kMultiplyI, fSigned, *) |
| 495 | VECTOR_BINARY_OP(kMultiplyF, fFloat, *) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 496 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 497 | case ByteCodeInstruction::kNot: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 498 | sp[0].fBool = !sp[0].fBool; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 499 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 500 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 501 | case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat; |
| 502 | case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat; |
| 503 | case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat; |
| 504 | case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 505 | break; |
| 506 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 507 | case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned; |
| 508 | case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned; |
| 509 | case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned; |
| 510 | case ByteCodeInstruction::kNegateI : sp[ 0] = -sp [0].fSigned; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 511 | break; |
| 512 | |
| 513 | case ByteCodeInstruction::kPop4: POP(); |
| 514 | case ByteCodeInstruction::kPop3: POP(); |
| 515 | case ByteCodeInstruction::kPop2: POP(); |
| 516 | case ByteCodeInstruction::kPop : POP(); |
| 517 | break; |
| 518 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 519 | case ByteCodeInstruction::kPopN: |
| 520 | sp -= READ8(); |
| 521 | break; |
| 522 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 523 | case ByteCodeInstruction::kPushImmediate: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 524 | PUSH(READ32()); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 525 | break; |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 526 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 527 | case ByteCodeInstruction::kReadExternal: // fall through |
| 528 | case ByteCodeInstruction::kReadExternal2: // fall through |
| 529 | case ByteCodeInstruction::kReadExternal3: // fall through |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 530 | case ByteCodeInstruction::kReadExternal4: { |
| 531 | int src = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 532 | fByteCode->fExternalValues[src]->read(sp + 1); |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 533 | sp += (int) inst - (int) ByteCodeInstruction::kReadExternal + 1; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 534 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 535 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 536 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 537 | VECTOR_BINARY_FN(kRemainderF, fFloat, fmodf) |
| 538 | VECTOR_BINARY_OP(kRemainderS, fSigned, %) |
| 539 | VECTOR_BINARY_OP(kRemainderU, fUnsigned, %) |
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::kReturn: { |
| 542 | int count = READ8(); |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 543 | if (frames.empty()) { |
| 544 | if (outReturn) { |
| 545 | memcpy(outReturn, sp - count + 1, count * sizeof(Value)); |
| 546 | } |
| 547 | return; |
| 548 | } else { |
| 549 | // When we were called, 'stack' was positioned at the old top-of-stack (where |
| 550 | // our parameters were placed). So copy our return values to that same spot. |
| 551 | memmove(stack, sp - count + 1, count * sizeof(Value)); |
| 552 | |
| 553 | // Now move the stack pointer to the end of the just-pushed return values, |
| 554 | // and restore everything else. |
| 555 | const StackFrame& frame(frames.back()); |
| 556 | sp = stack + count - 1; |
| 557 | stack = frame.fStack; |
| 558 | code = frame.fCode; |
| 559 | ip = frame.fIP; |
| 560 | frames.pop_back(); |
| 561 | break; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 562 | } |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 563 | } |
| 564 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 565 | VECTOR_UNARY_FN(kSin, sinf, fFloat) |
| 566 | VECTOR_UNARY_FN(kSqrt, sqrtf, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 567 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 568 | case ByteCodeInstruction::kStore4: stack[*ip + 3] = POP(); |
| 569 | case ByteCodeInstruction::kStore3: stack[*ip + 2] = POP(); |
| 570 | case ByteCodeInstruction::kStore2: stack[*ip + 1] = POP(); |
| 571 | case ByteCodeInstruction::kStore : stack[*ip + 0] = POP(); |
| 572 | ++ip; |
| 573 | break; |
| 574 | |
| 575 | case ByteCodeInstruction::kStoreGlobal4: fGlobals[*ip + 3] = POP(); |
| 576 | case ByteCodeInstruction::kStoreGlobal3: fGlobals[*ip + 2] = POP(); |
| 577 | case ByteCodeInstruction::kStoreGlobal2: fGlobals[*ip + 1] = POP(); |
| 578 | case ByteCodeInstruction::kStoreGlobal : fGlobals[*ip + 0] = POP(); |
| 579 | ++ip; |
| 580 | break; |
| 581 | |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 582 | case ByteCodeInstruction::kStoreExtended: { |
| 583 | int count = READ8(); |
| 584 | int target = POP().fSigned; |
| 585 | memcpy(&stack[target], sp - count + 1, count * sizeof(Value)); |
| 586 | sp -= count; |
| 587 | break; |
| 588 | } |
| 589 | case ByteCodeInstruction::kStoreExtendedGlobal: { |
| 590 | int count = READ8(); |
| 591 | int target = POP().fSigned; |
| 592 | SkASSERT(target + count <= (int) fGlobals.size()); |
| 593 | memcpy(&fGlobals[target], sp - count + 1, count * sizeof(Value)); |
| 594 | sp -= count; |
| 595 | break; |
| 596 | } |
| 597 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 598 | case ByteCodeInstruction::kStoreSwizzle: { |
| 599 | int target = READ8(); |
| 600 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 601 | for (int i = count - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 602 | stack[target + *(ip + i)] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 603 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 604 | ip += count; |
| 605 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 606 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 607 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 608 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 609 | int target = READ8(); |
| 610 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame] | 611 | for (int i = count - 1; i >= 0; --i) { |
| 612 | fGlobals[target + *(ip + i)] = POP(); |
| 613 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 614 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 615 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 616 | } |
Brian Osman | 07c117b | 2019-05-23 12:51:06 -0700 | [diff] [blame^] | 617 | case ByteCodeInstruction::kStoreSwizzleIndirect: { |
| 618 | int target = POP().fSigned; |
| 619 | int count = READ8(); |
| 620 | for (int i = count - 1; i >= 0; --i) { |
| 621 | stack[target + *(ip + i)] = POP(); |
| 622 | } |
| 623 | ip += count; |
| 624 | break; |
| 625 | } |
| 626 | case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: { |
| 627 | int target = POP().fSigned; |
| 628 | int count = READ8(); |
| 629 | for (int i = count - 1; i >= 0; --i) { |
| 630 | fGlobals[target + *(ip + i)] = POP(); |
| 631 | } |
| 632 | ip += count; |
| 633 | break; |
| 634 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 635 | |
Mike Klein | c199998 | 2019-05-21 13:03:49 -0500 | [diff] [blame] | 636 | VECTOR_BINARY_OP(kSubtractI, fSigned, -) |
| 637 | VECTOR_BINARY_OP(kSubtractF, fFloat, -) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 638 | |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 639 | case ByteCodeInstruction::kSwizzle: { |
| 640 | Value tmp[4]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 641 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 642 | tmp[i] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 643 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 644 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 645 | PUSH(tmp[READ8()]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 646 | } |
| 647 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 648 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 649 | |
Mike Klein | 459aed1 | 2019-05-21 15:46:36 -0500 | [diff] [blame] | 650 | VECTOR_UNARY_FN(kTan, tanf, fFloat) |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 651 | |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 652 | case ByteCodeInstruction::kWriteExternal: // fall through |
| 653 | case ByteCodeInstruction::kWriteExternal2: // fall through |
| 654 | case ByteCodeInstruction::kWriteExternal3: // fall through |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 655 | case ByteCodeInstruction::kWriteExternal4: { |
| 656 | int count = (int) inst - (int) ByteCodeInstruction::kWriteExternal + 1; |
| 657 | int target = READ8(); |
Ethan Nicholas | 48a75aa | 2019-05-16 17:15:56 -0400 | [diff] [blame] | 658 | fByteCode->fExternalValues[target]->write(sp - count + 1); |
| 659 | sp -= count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 660 | break; |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 661 | } |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 662 | |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 663 | default: |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 664 | SkDEBUGFAILF("unsupported instruction %d\n", (int) inst); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 665 | } |
| 666 | #ifdef TRACE |
Mike Klein | e700738 | 2019-05-21 08:36:32 -0500 | [diff] [blame] | 667 | int stackSize = (int) (sp - stack + 1); |
| 668 | printf("STACK(%d):", stackSize); |
Ethan Nicholas | 82162ee | 2019-05-21 16:05:08 -0400 | [diff] [blame] | 669 | for (int i = 0; i < stackSize; ++i) { |
Brian Osman | e85b6a5 | 2019-05-22 14:50:59 -0700 | [diff] [blame] | 670 | printf(" %d(%g)", stack[i].fSigned, stack[i].fFloat); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 671 | } |
| 672 | printf("\n"); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 673 | #endif |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 674 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | } // namespace |
| 678 | |
| 679 | #endif |