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