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