blob: 35771c428271544c4b6beb8afd295e13c0ffc6a9 [file] [log] [blame]
Ethan Nicholas26a9aad2018-03-27 14:10:52 -04001/*
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 Nicholasae9633b2019-05-24 12:46:34 -040010#include "include/core/SkPoint3.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "src/core/SkRasterPipeline.h"
Brian Osman07c117b2019-05-23 12:51:06 -070012#include "src/sksl/SkSLByteCodeGenerator.h"
Ethan Nicholas91164d12019-05-15 15:29:54 -040013#include "src/sksl/SkSLExternalValue.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#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 Nicholas26a9aad2018-03-27 14:10:52 -040030
31namespace SkSL {
32
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040033static constexpr int UNINITIALIZED = 0xDEADBEEF;
34
Mike Kleine7007382019-05-21 08:36:32 -050035Interpreter::Interpreter(std::unique_ptr<Program> program,
36 std::unique_ptr<ByteCode> byteCode,
Ethan Nicholasdfcad062019-05-07 12:53:34 -040037 Interpreter::Value inputs[])
38 : fProgram(std::move(program))
Mike Kleine7007382019-05-21 08:36:32 -050039 , fByteCode(std::move(byteCode))
40 , fGlobals(fByteCode->fGlobalCount, UNINITIALIZED) {
Brian Osmand369a5e2019-05-09 13:13:25 -040041 this->setInputs(inputs);
42}
43
44void Interpreter::setInputs(Interpreter::Value inputs[]) {
Mike Kleine7007382019-05-21 08:36:32 -050045 for (uint8_t slot : fByteCode->fInputSlots) {
46 fGlobals[slot] = *inputs++;
Ethan Nicholasdfcad062019-05-07 12:53:34 -040047 }
48}
49
50void Interpreter::run(const ByteCodeFunction& f, Interpreter::Value args[],
51 Interpreter::Value* outReturn) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040052#ifdef TRACE
53 this->disassemble(f);
54#endif
Ethan Nicholasdfcad062019-05-07 12:53:34 -040055 Value smallStack[128];
56 std::unique_ptr<Value[]> largeStack;
57 Value* stack = smallStack;
Brian Osman226668a2019-05-14 16:47:30 -040058 if ((int) SK_ARRAY_COUNT(smallStack) < f.fStackCount) {
59 largeStack.reset(new Value[f.fStackCount]);
Ethan Nicholasdfcad062019-05-07 12:53:34 -040060 stack = largeStack.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040061 }
Mike Kleine7007382019-05-21 08:36:32 -050062
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 Osman07c117b2019-05-23 12:51:06 -070069 const int nvalues = ByteCodeGenerator::SlotCount(p->fType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040070 if (p->fModifiers.fFlags & Modifiers::kOut_Flag) {
Mike Kleine7007382019-05-21 08:36:32 -050071 memcpy(args, stack, nvalues * sizeof(Value));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040072 }
Mike Kleine7007382019-05-21 08:36:32 -050073 args += nvalues;
74 stack += nvalues;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040075 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040076}
77
Mike Klein76346ac2019-05-17 11:57:10 -050078template <typename T>
79static T unaligned_load(const void* ptr) {
80 T val;
81 memcpy(&val, ptr, sizeof(val));
82 return val;
83}
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040084
Mike Kleine7007382019-05-21 08:36:32 -050085#define READ8() (*(ip++))
Mike Klein76346ac2019-05-17 11:57:10 -050086#define READ16() (ip += 2, unaligned_load<uint16_t>(ip - 2))
87#define READ32() (ip += 4, unaligned_load<uint32_t>(ip - 4))
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040088
Brian Osman3e833e12019-05-23 13:23:24 -070089#define VECTOR_DISASSEMBLE(op, text) \
Ethan Nicholas48a75aa2019-05-16 17:15:56 -040090 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 Osman1e855b22019-05-29 15:21:52 -040095#define VECTOR_MATRIX_DISASSEMBLE(op, text) \
96 case ByteCodeInstruction::op: printf(text); break; \
97 case ByteCodeInstruction::op##2: printf(text "2"); break; \
98 case ByteCodeInstruction::op##3: printf(text "3"); break; \
99 case ByteCodeInstruction::op##4: printf(text "4"); break; \
100 case ByteCodeInstruction::op##N: printf(text "N %d", READ8()); break;
101
Brian Osman3e833e12019-05-23 13:23:24 -0700102static const uint8_t* disassemble_instruction(const uint8_t* ip) {
103 switch ((ByteCodeInstruction) READ16()) {
Brian Osman1e855b22019-05-29 15:21:52 -0400104 VECTOR_MATRIX_DISASSEMBLE(kAddF, "addf")
Brian Osman3e833e12019-05-23 13:23:24 -0700105 VECTOR_DISASSEMBLE(kAddI, "addi")
Brian Osman32c526b2019-06-03 16:13:52 -0400106 case ByteCodeInstruction::kAndB: printf("andb"); break;
Brian Osman3e833e12019-05-23 13:23:24 -0700107 case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break;
108 case ByteCodeInstruction::kCall: printf("call %d", READ8()); break;
109 case ByteCodeInstruction::kCallExternal: {
110 int argumentCount = READ8();
111 int returnCount = READ8();
112 int externalValue = READ8();
113 printf("callexternal %d, %d, %d", argumentCount, returnCount, externalValue);
114 break;
115 }
116 VECTOR_DISASSEMBLE(kCompareIEQ, "compareieq")
117 VECTOR_DISASSEMBLE(kCompareINEQ, "compareineq")
Brian Osman1e855b22019-05-29 15:21:52 -0400118 VECTOR_MATRIX_DISASSEMBLE(kCompareFEQ, "comparefeq")
119 VECTOR_MATRIX_DISASSEMBLE(kCompareFNEQ, "comparefneq")
Brian Osman3e833e12019-05-23 13:23:24 -0700120 VECTOR_DISASSEMBLE(kCompareFGT, "comparefgt")
121 VECTOR_DISASSEMBLE(kCompareFGTEQ, "comparefgteq")
122 VECTOR_DISASSEMBLE(kCompareFLT, "compareflt")
123 VECTOR_DISASSEMBLE(kCompareFLTEQ, "compareflteq")
124 VECTOR_DISASSEMBLE(kCompareSGT, "comparesgt")
125 VECTOR_DISASSEMBLE(kCompareSGTEQ, "comparesgteq")
126 VECTOR_DISASSEMBLE(kCompareSLT, "compareslt")
127 VECTOR_DISASSEMBLE(kCompareSLTEQ, "compareslteq")
128 VECTOR_DISASSEMBLE(kCompareUGT, "compareugt")
129 VECTOR_DISASSEMBLE(kCompareUGTEQ, "compareugteq")
130 VECTOR_DISASSEMBLE(kCompareULT, "compareult")
131 VECTOR_DISASSEMBLE(kCompareULTEQ, "compareulteq")
132 case ByteCodeInstruction::kConditionalBranch:
133 printf("conditionalbranch %d", READ16());
134 break;
135 VECTOR_DISASSEMBLE(kConvertFtoI, "convertftoi")
136 VECTOR_DISASSEMBLE(kConvertStoF, "convertstof")
137 VECTOR_DISASSEMBLE(kConvertUtoF, "convertutof")
138 VECTOR_DISASSEMBLE(kCos, "cos")
Brian Osman1e855b22019-05-29 15:21:52 -0400139 VECTOR_MATRIX_DISASSEMBLE(kDivideF, "dividef")
Brian Osman3e833e12019-05-23 13:23:24 -0700140 VECTOR_DISASSEMBLE(kDivideS, "divideS")
141 VECTOR_DISASSEMBLE(kDivideU, "divideu")
Brian Osman1e855b22019-05-29 15:21:52 -0400142 VECTOR_MATRIX_DISASSEMBLE(kDup, "dup")
Brian Osman3e833e12019-05-23 13:23:24 -0700143 case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break;
144 case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break;
145 case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break;
146 case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break;
147 case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break;
148 case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break;
149 case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break;
150 case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break;
151 case ByteCodeInstruction::kLoadSwizzle: {
152 int target = READ8();
153 int count = READ8();
154 printf("loadswizzle %d %d", target, count);
155 for (int i = 0; i < count; ++i) {
156 printf(", %d", READ8());
157 }
158 break;
159 }
160 case ByteCodeInstruction::kLoadSwizzleGlobal: {
161 int target = READ8();
162 int count = READ8();
163 printf("loadswizzleglobal %d %d", target, count);
164 for (int i = 0; i < count; ++i) {
165 printf(", %d", READ8());
166 }
167 break;
168 }
169 case ByteCodeInstruction::kLoadExtended: printf("loadextended %d", READ8()); break;
170 case ByteCodeInstruction::kLoadExtendedGlobal: printf("loadextendedglobal %d", READ8());
171 break;
Brian Osman29e013d2019-05-28 17:16:03 -0400172 case ByteCodeInstruction::kMatrixToMatrix: {
173 int srcCols = READ8();
174 int srcRows = READ8();
175 int dstCols = READ8();
176 int dstRows = READ8();
177 printf("matrixtomatrix %dx%d %dx%d", srcCols, srcRows, dstCols, dstRows);
178 break;
179 }
Brian Osman909231c2019-05-29 15:34:36 -0400180 case ByteCodeInstruction::kMatrixMultiply: {
181 int lCols = READ8();
182 int lRows = READ8();
183 int rCols = READ8();
184 printf("matrixmultiply %dx%d %dx%d", lCols, lRows, rCols, lCols);
185 break;
186 }
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400187 VECTOR_DISASSEMBLE(kMix, "mix")
Brian Osman1e855b22019-05-29 15:21:52 -0400188 VECTOR_MATRIX_DISASSEMBLE(kMultiplyF, "multiplyf")
Brian Osman3e833e12019-05-23 13:23:24 -0700189 VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi")
Brian Osman1e855b22019-05-29 15:21:52 -0400190 VECTOR_MATRIX_DISASSEMBLE(kNegateF, "negatef")
Brian Osman3e833e12019-05-23 13:23:24 -0700191 VECTOR_DISASSEMBLE(kNegateI, "negatei")
Brian Osman32c526b2019-06-03 16:13:52 -0400192 case ByteCodeInstruction::kNot: printf("not"); break;
193 case ByteCodeInstruction::kOrB: printf("orb"); break;
Brian Osman1e855b22019-05-29 15:21:52 -0400194 VECTOR_MATRIX_DISASSEMBLE(kPop, "pop")
Brian Osman3e833e12019-05-23 13:23:24 -0700195 case ByteCodeInstruction::kPushImmediate: {
196 uint32_t v = READ32();
197 union { uint32_t u; float f; } pun = { v };
198 printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str());
199 break;
200 }
201 case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break;
202 case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break;
203 case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break;
204 case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break;
205 VECTOR_DISASSEMBLE(kRemainderF, "remainderf")
206 VECTOR_DISASSEMBLE(kRemainderS, "remainders")
207 VECTOR_DISASSEMBLE(kRemainderU, "remainderu")
208 case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break;
Brian Osman29e013d2019-05-28 17:16:03 -0400209 case ByteCodeInstruction::kScalarToMatrix: {
210 int cols = READ8();
211 int rows = READ8();
212 printf("scalartomatrix %dx%d", cols, rows);
213 break;
214 }
Brian Osman3e833e12019-05-23 13:23:24 -0700215 VECTOR_DISASSEMBLE(kSin, "sin")
216 VECTOR_DISASSEMBLE(kSqrt, "sqrt")
217 case ByteCodeInstruction::kStore: printf("store %d", READ8()); break;
218 case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break;
219 case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break;
220 case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break;
221 case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break;
222 case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break;
223 case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break;
224 case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break;
225 case ByteCodeInstruction::kStoreSwizzle: {
226 int target = READ8();
227 int count = READ8();
228 printf("storeswizzle %d %d", target, count);
229 for (int i = 0; i < count; ++i) {
230 printf(", %d", READ8());
231 }
232 break;
233 }
234 case ByteCodeInstruction::kStoreSwizzleGlobal: {
235 int target = READ8();
236 int count = READ8();
237 printf("storeswizzleglobal %d %d", target, count);
238 for (int i = 0; i < count; ++i) {
239 printf(", %d", READ8());
240 }
241 break;
242 }
243 case ByteCodeInstruction::kStoreSwizzleIndirect: {
244 int count = READ8();
245 printf("storeswizzleindirect %d", count);
246 for (int i = 0; i < count; ++i) {
247 printf(", %d", READ8());
248 }
249 break;
250 }
251 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
252 int count = READ8();
253 printf("storeswizzleindirectglobal %d", count);
254 for (int i = 0; i < count; ++i) {
255 printf(", %d", READ8());
256 }
257 break;
258 }
259 case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break;
260 case ByteCodeInstruction::kStoreExtendedGlobal: printf("storeextendedglobal %d", READ8());
261 break;
Brian Osman1e855b22019-05-29 15:21:52 -0400262 VECTOR_MATRIX_DISASSEMBLE(kSubtractF, "subtractf")
Brian Osman3e833e12019-05-23 13:23:24 -0700263 VECTOR_DISASSEMBLE(kSubtractI, "subtracti")
264 case ByteCodeInstruction::kSwizzle: {
265 printf("swizzle %d, ", READ8());
266 int count = READ8();
267 printf("%d", count);
268 for (int i = 0; i < count; ++i) {
269 printf(", %d", READ8());
270 }
271 break;
272 }
273 VECTOR_DISASSEMBLE(kTan, "tan")
274 case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break;
275 case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break;
276 case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break;
277 case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break;
278 default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false);
279 }
280 return ip;
281}
282
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400283void Interpreter::disassemble(const ByteCodeFunction& f) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400284 const uint8_t* ip = f.fCode.data();
285 while (ip < f.fCode.data() + f.fCode.size()) {
286 printf("%d: ", (int) (ip - f.fCode.data()));
Brian Osman3e833e12019-05-23 13:23:24 -0700287 ip = disassemble_instruction(ip);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400288 printf("\n");
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400289 }
290}
291
Mike Klein459aed12019-05-21 15:46:36 -0500292#define VECTOR_BINARY_OP(base, field, op) \
Mike Kleine7007382019-05-21 08:36:32 -0500293 case ByteCodeInstruction::base ## 4: \
Mike Klein459aed12019-05-21 15:46:36 -0500294 sp[-4] = sp[-4].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500295 POP(); \
296 /* fall through */ \
297 case ByteCodeInstruction::base ## 3: { \
298 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500299 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500300 POP(); \
301 } /* fall through */ \
302 case ByteCodeInstruction::base ## 2: { \
303 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500304 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500305 POP(); \
306 } /* fall through */ \
307 case ByteCodeInstruction::base: { \
308 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500309 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500310 POP(); \
311 break; \
312 }
Ethan Nicholasaeb71ce2019-05-20 09:55:44 -0400313
Brian Osman1e855b22019-05-29 15:21:52 -0400314#define VECTOR_MATRIX_BINARY_OP(base, field, op) \
315 VECTOR_BINARY_OP(base, field, op) \
316 case ByteCodeInstruction::base ## N: { \
317 int count = READ8(); \
318 for (int i = count; i > 0; --i) { \
319 sp[-count] = sp[-count].field op sp[0].field; \
320 POP(); \
321 } \
322 break; \
323 }
324
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400325#define VECTOR_BINARY_FN(base, field, fn) \
326 case ByteCodeInstruction::base ## 4: \
327 sp[-4] = fn(sp[-4].field, sp[0].field); \
328 POP(); \
329 /* fall through */ \
330 case ByteCodeInstruction::base ## 3: { \
331 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
332 sp[target] = fn(sp[target].field, sp[0].field); \
333 POP(); \
334 } /* fall through */ \
335 case ByteCodeInstruction::base ## 2: { \
336 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
337 sp[target] = fn(sp[target].field, sp[0].field); \
338 POP(); \
339 } /* fall through */ \
340 case ByteCodeInstruction::base: { \
341 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
342 sp[target] = fn(sp[target].field, sp[0].field); \
343 POP(); \
344 break; \
Mike Kleine7007382019-05-21 08:36:32 -0500345 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400346
Mike Klein459aed12019-05-21 15:46:36 -0500347#define VECTOR_UNARY_FN(base, fn, field) \
348 case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \
349 case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \
350 case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \
351 case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400352 break;
353
Brian Osman226668a2019-05-14 16:47:30 -0400354struct StackFrame {
355 const uint8_t* fCode;
356 const uint8_t* fIP;
357 Interpreter::Value* fStack;
358};
359
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400360static float mix(float start, float end, float t) {
361 return start * (1 - t) + end * t;
362}
363
Mike Kleine7007382019-05-21 08:36:32 -0500364void Interpreter::innerRun(const ByteCodeFunction& f, Value* stack, Value* outReturn) {
365 Value* sp = stack + f.fParameterCount + f.fLocalCount - 1;
366
367 auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); };
368 auto PUSH = [&](Value v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; };
369
Brian Osman226668a2019-05-14 16:47:30 -0400370 const uint8_t* code = f.fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400371 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400372 std::vector<StackFrame> frames;
373
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400374 for (;;) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400375#ifdef TRACE
Brian Osman3e833e12019-05-23 13:23:24 -0700376 printf("at %3d ", (int) (ip - code));
377 disassemble_instruction(ip);
378 printf("\n");
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400379#endif
Brian Osmane85b6a52019-05-22 14:50:59 -0700380 ByteCodeInstruction inst = (ByteCodeInstruction) READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400381 switch (inst) {
Mike Kleinc1999982019-05-21 13:03:49 -0500382 VECTOR_BINARY_OP(kAddI, fSigned, +)
Brian Osman1e855b22019-05-29 15:21:52 -0400383 VECTOR_MATRIX_BINARY_OP(kAddF, fFloat, +)
Brian Osman32c526b2019-06-03 16:13:52 -0400384 case ByteCodeInstruction::kAndB:
385 sp[-1] = sp[-1].fBool && sp[0].fBool;
386 POP();
387 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400388
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400389 case ByteCodeInstruction::kBranch:
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400390 ip = code + READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400391 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400392
Brian Osman226668a2019-05-14 16:47:30 -0400393 case ByteCodeInstruction::kCall: {
394 // Precursor code has pushed all parameters to the stack. Update our bottom of
395 // stack to point at the first parameter, and our sp to point past those parameters
396 // (plus space for locals).
Mike Kleine7007382019-05-21 08:36:32 -0500397 int target = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400398 const ByteCodeFunction* fun = fByteCode->fFunctions[target].get();
399 frames.push_back({ code, ip, stack });
400 ip = code = fun->fCode.data();
401 stack = sp - fun->fParameterCount + 1;
402 sp = stack + fun->fParameterCount + fun->fLocalCount - 1;
403 break;
404 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400405
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400406 case ByteCodeInstruction::kCallExternal: {
407 int argumentCount = READ8();
408 int returnCount = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500409 int target = READ8();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400410 ExternalValue* v = fByteCode->fExternalValues[target];
411 sp -= argumentCount - 1;
Mike Kleine7007382019-05-21 08:36:32 -0500412
413 Value tmp[4];
414 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmp));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400415 v->call(sp, tmp);
416 memcpy(sp, tmp, returnCount * sizeof(Value));
417 sp += returnCount - 1;
418 break;
419 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400420
Mike Kleinc1999982019-05-21 13:03:49 -0500421 VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==)
Brian Osman1e855b22019-05-29 15:21:52 -0400422 VECTOR_MATRIX_BINARY_OP(kCompareFEQ, fFloat, ==)
Mike Kleinc1999982019-05-21 13:03:49 -0500423 VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=)
Brian Osman1e855b22019-05-29 15:21:52 -0400424 VECTOR_MATRIX_BINARY_OP(kCompareFNEQ, fFloat, !=)
Mike Kleinc1999982019-05-21 13:03:49 -0500425 VECTOR_BINARY_OP(kCompareSGT, fSigned, >)
426 VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >)
427 VECTOR_BINARY_OP(kCompareFGT, fFloat, >)
428 VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=)
429 VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=)
430 VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=)
431 VECTOR_BINARY_OP(kCompareSLT, fSigned, <)
432 VECTOR_BINARY_OP(kCompareULT, fUnsigned, <)
433 VECTOR_BINARY_OP(kCompareFLT, fFloat, <)
434 VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=)
435 VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=)
436 VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400437
Mike Kleine7007382019-05-21 08:36:32 -0500438 case ByteCodeInstruction::kConditionalBranch: {
439 int target = READ16();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400440 if (POP().fBool) {
Mike Kleine7007382019-05-21 08:36:32 -0500441 ip = code + target;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400442 }
443 break;
Mike Kleine7007382019-05-21 08:36:32 -0500444 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400445
446 case ByteCodeInstruction::kConvertFtoI4: sp[-3].fSigned = (int)sp[-3].fFloat;
447 case ByteCodeInstruction::kConvertFtoI3: sp[-2].fSigned = (int)sp[-2].fFloat;
448 case ByteCodeInstruction::kConvertFtoI2: sp[-1].fSigned = (int)sp[-1].fFloat;
449 case ByteCodeInstruction::kConvertFtoI: sp[ 0].fSigned = (int)sp[ 0].fFloat;
450 break;
451
452 case ByteCodeInstruction::kConvertStoF4: sp[-3].fFloat = sp[-3].fSigned;
453 case ByteCodeInstruction::kConvertStoF3: sp[-2].fFloat = sp[-2].fSigned;
454 case ByteCodeInstruction::kConvertStoF2: sp[-1].fFloat = sp[-1].fSigned;
455 case ByteCodeInstruction::kConvertStoF : sp[ 0].fFloat = sp[ 0].fSigned;
456 break;
457
458 case ByteCodeInstruction::kConvertUtoF4: sp[-3].fFloat = sp[-3].fUnsigned;
459 case ByteCodeInstruction::kConvertUtoF3: sp[-2].fFloat = sp[-2].fUnsigned;
460 case ByteCodeInstruction::kConvertUtoF2: sp[-1].fFloat = sp[-1].fUnsigned;
461 case ByteCodeInstruction::kConvertUtoF : sp[ 0].fFloat = sp[ 0].fUnsigned;
462 break;
463
Mike Klein459aed12019-05-21 15:46:36 -0500464 VECTOR_UNARY_FN(kCos, cosf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400465
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400466 case ByteCodeInstruction::kCross: {
467 SkPoint3 cross = SkPoint3::CrossProduct(SkPoint3::Make(sp[-5].fFloat,
468 sp[-4].fFloat,
469 sp[-3].fFloat),
470 SkPoint3::Make(sp[-2].fFloat,
471 sp[-1].fFloat,
472 sp[ 0].fFloat));
473 sp -= 3;
474 sp[-2] = cross.fX;
475 sp[-1] = cross.fY;
476 sp[ 0] = cross.fZ;
477 break;
478 }
479
Mike Kleinc1999982019-05-21 13:03:49 -0500480 VECTOR_BINARY_OP(kDivideS, fSigned, /)
481 VECTOR_BINARY_OP(kDivideU, fUnsigned, /)
Brian Osman1e855b22019-05-29 15:21:52 -0400482 VECTOR_MATRIX_BINARY_OP(kDivideF, fFloat, /)
Mike Kleine7007382019-05-21 08:36:32 -0500483
484 case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
485 case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
486 case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
487 case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
488 break;
489
Brian Osman07c117b2019-05-23 12:51:06 -0700490 case ByteCodeInstruction::kDupN: {
491 int count = READ8();
492 memcpy(sp + 1, sp - count + 1, count * sizeof(Value));
493 sp += count;
494 break;
495 }
496
Mike Kleine7007382019-05-21 08:36:32 -0500497 case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3];
498 case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2];
499 case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1];
500 case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0];
501 ++ip;
502 sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1;
503 break;
504
505 case ByteCodeInstruction::kLoadGlobal4: sp[4] = fGlobals[*ip + 3];
506 case ByteCodeInstruction::kLoadGlobal3: sp[3] = fGlobals[*ip + 2];
507 case ByteCodeInstruction::kLoadGlobal2: sp[2] = fGlobals[*ip + 1];
508 case ByteCodeInstruction::kLoadGlobal : sp[1] = fGlobals[*ip + 0];
509 ++ip;
Brian Osman07c117b2019-05-23 12:51:06 -0700510 sp += (int)inst -
511 (int)ByteCodeInstruction::kLoadGlobal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500512 break;
513
Brian Osman07c117b2019-05-23 12:51:06 -0700514 case ByteCodeInstruction::kLoadExtended: {
515 int count = READ8();
516 int src = POP().fSigned;
517 memcpy(sp + 1, &stack[src], count * sizeof(Value));
518 sp += count;
519 break;
520 }
521
522 case ByteCodeInstruction::kLoadExtendedGlobal: {
523 int count = READ8();
524 int src = POP().fSigned;
525 SkASSERT(src + count <= (int) fGlobals.size());
526 memcpy(sp + 1, &fGlobals[src], count * sizeof(Value));
527 sp += count;
528 break;
529 }
530
Mike Kleine7007382019-05-21 08:36:32 -0500531 case ByteCodeInstruction::kLoadSwizzle: {
532 int src = READ8();
533 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400534 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400535 PUSH(stack[src + *(ip + i)]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400536 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400537 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400538 break;
Mike Kleine7007382019-05-21 08:36:32 -0500539 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400540
Mike Kleine7007382019-05-21 08:36:32 -0500541 case ByteCodeInstruction::kLoadSwizzleGlobal: {
542 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400543 SkASSERT(src < (int) fGlobals.size());
Mike Kleine7007382019-05-21 08:36:32 -0500544 int count = READ8();
Brian Osmanb7451292019-05-15 13:02:13 -0400545 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400546 PUSH(fGlobals[src + *(ip + i)]);
Brian Osmanb7451292019-05-15 13:02:13 -0400547 }
548 ip += count;
549 break;
Mike Kleine7007382019-05-21 08:36:32 -0500550 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400551
Brian Osman29e013d2019-05-28 17:16:03 -0400552 case ByteCodeInstruction::kMatrixToMatrix: {
553 int srcCols = READ8();
554 int srcRows = READ8();
555 int dstCols = READ8();
556 int dstRows = READ8();
557 SkASSERT(srcCols >= 2 && srcCols <= 4);
558 SkASSERT(srcRows >= 2 && srcRows <= 4);
559 SkASSERT(dstCols >= 2 && dstCols <= 4);
560 SkASSERT(dstRows >= 2 && dstRows <= 4);
561 SkMatrix44 m;
562 for (int c = srcCols - 1; c >= 0; --c) {
563 for (int r = srcRows - 1; r >= 0; --r) {
564 m.set(r, c, POP().fFloat);
565 }
566 }
567 for (int c = 0; c < dstCols; ++c) {
568 for (int r = 0; r < dstRows; ++r) {
569 PUSH(m.get(r, c));
570 }
571 }
572 break;
573 }
574
Brian Osman909231c2019-05-29 15:34:36 -0400575 case ByteCodeInstruction::kMatrixMultiply: {
576 int lCols = READ8();
577 int lRows = READ8();
578 int rCols = READ8();
579 int rRows = lCols;
580 float tmp[16] = { 0.0f };
581 float* B = &(sp - (rCols * rRows) + 1)->fFloat;
582 float* A = B - (lCols * lRows);
583 for (int c = 0; c < rCols; ++c) {
584 for (int r = 0; r < lRows; ++r) {
585 for (int j = 0; j < lCols; ++j) {
586 tmp[c*lRows + r] += A[j*lRows + r] * B[c*rRows + j];
587 }
588 }
589 }
590 sp -= (lCols * lRows) + (rCols * rRows);
591 memcpy(sp + 1, tmp, rCols * lRows * sizeof(Value));
592 sp += (rCols * lRows);
593 break;
594 }
595
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400596 // stack looks like: X1 Y1 Z1 W1 X2 Y2 Z2 W2 T
597 case ByteCodeInstruction::kMix4:
598 sp[-5] = mix(sp[-5].fFloat, sp[-1].fFloat, sp[0].fFloat);
599 // fall through
600 case ByteCodeInstruction::kMix3: {
601 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
602 int target = 2 - count * 2;
603 sp[target] = mix(sp[target].fFloat, sp[2 - count].fFloat, sp[0].fFloat);
604 // fall through
605 }
606 case ByteCodeInstruction::kMix2: {
607 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
608 int target = 1 - count * 2;
609 sp[target] = mix(sp[target].fFloat, sp[1 - count].fFloat, sp[0].fFloat);
610 // fall through
611 }
612 case ByteCodeInstruction::kMix: {
613 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
614 int target = -count * 2;
615 sp[target] = mix(sp[target].fFloat, sp[-count].fFloat, sp[0].fFloat);
616 sp -= 1 + count;
617 break;
618 }
619
Mike Kleinc1999982019-05-21 13:03:49 -0500620 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
Brian Osman1e855b22019-05-29 15:21:52 -0400621 VECTOR_MATRIX_BINARY_OP(kMultiplyF, fFloat, *)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400622
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400623 case ByteCodeInstruction::kNot:
Mike Kleine7007382019-05-21 08:36:32 -0500624 sp[0].fBool = !sp[0].fBool;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400625 break;
Mike Kleine7007382019-05-21 08:36:32 -0500626
Mike Kleinc1999982019-05-21 13:03:49 -0500627 case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat;
628 case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat;
629 case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat;
630 case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat;
Mike Kleine7007382019-05-21 08:36:32 -0500631 break;
632
Brian Osman1e855b22019-05-29 15:21:52 -0400633 case ByteCodeInstruction::kNegateFN: {
634 int count = READ8();
635 for (int i = count - 1; i >= 0; --i) {
636 sp[-i] = -sp[-i].fFloat;
637 }
638 break;
639 }
640
Mike Kleinc1999982019-05-21 13:03:49 -0500641 case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned;
642 case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned;
643 case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned;
644 case ByteCodeInstruction::kNegateI : sp[ 0] = -sp [0].fSigned;
Mike Kleine7007382019-05-21 08:36:32 -0500645 break;
646
Brian Osman32c526b2019-06-03 16:13:52 -0400647 case ByteCodeInstruction::kOrB:
648 sp[-1] = sp[-1].fBool || sp[0].fBool;
649 POP();
650 break;
Brian Osman16e6fd52019-05-29 11:19:00 -0400651
Mike Kleine7007382019-05-21 08:36:32 -0500652 case ByteCodeInstruction::kPop4: POP();
653 case ByteCodeInstruction::kPop3: POP();
654 case ByteCodeInstruction::kPop2: POP();
655 case ByteCodeInstruction::kPop : POP();
656 break;
657
Brian Osman07c117b2019-05-23 12:51:06 -0700658 case ByteCodeInstruction::kPopN:
659 sp -= READ8();
660 break;
661
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400662 case ByteCodeInstruction::kPushImmediate:
Mike Kleine7007382019-05-21 08:36:32 -0500663 PUSH(READ32());
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400664 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400665
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400666 case ByteCodeInstruction::kReadExternal: // fall through
667 case ByteCodeInstruction::kReadExternal2: // fall through
668 case ByteCodeInstruction::kReadExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500669 case ByteCodeInstruction::kReadExternal4: {
670 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400671 fByteCode->fExternalValues[src]->read(sp + 1);
Mike Kleine7007382019-05-21 08:36:32 -0500672 sp += (int) inst - (int) ByteCodeInstruction::kReadExternal + 1;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400673 break;
Mike Kleine7007382019-05-21 08:36:32 -0500674 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400675
Mike Kleinc1999982019-05-21 13:03:49 -0500676 VECTOR_BINARY_FN(kRemainderF, fFloat, fmodf)
677 VECTOR_BINARY_OP(kRemainderS, fSigned, %)
678 VECTOR_BINARY_OP(kRemainderU, fUnsigned, %)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400679
Mike Kleine7007382019-05-21 08:36:32 -0500680 case ByteCodeInstruction::kReturn: {
681 int count = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400682 if (frames.empty()) {
683 if (outReturn) {
684 memcpy(outReturn, sp - count + 1, count * sizeof(Value));
685 }
686 return;
687 } else {
688 // When we were called, 'stack' was positioned at the old top-of-stack (where
689 // our parameters were placed). So copy our return values to that same spot.
690 memmove(stack, sp - count + 1, count * sizeof(Value));
691
692 // Now move the stack pointer to the end of the just-pushed return values,
693 // and restore everything else.
694 const StackFrame& frame(frames.back());
695 sp = stack + count - 1;
696 stack = frame.fStack;
697 code = frame.fCode;
698 ip = frame.fIP;
699 frames.pop_back();
700 break;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400701 }
Mike Kleine7007382019-05-21 08:36:32 -0500702 }
703
Brian Osman29e013d2019-05-28 17:16:03 -0400704 case ByteCodeInstruction::kScalarToMatrix: {
705 int cols = READ8();
706 int rows = READ8();
707 Value v = POP();
708 for (int c = 0; c < cols; ++c) {
709 for (int r = 0; r < rows; ++r) {
710 PUSH(c == r ? v : 0.0f);
711 }
712 }
713 break;
714 }
715
Mike Klein459aed12019-05-21 15:46:36 -0500716 VECTOR_UNARY_FN(kSin, sinf, fFloat)
717 VECTOR_UNARY_FN(kSqrt, sqrtf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400718
Mike Kleine7007382019-05-21 08:36:32 -0500719 case ByteCodeInstruction::kStore4: stack[*ip + 3] = POP();
720 case ByteCodeInstruction::kStore3: stack[*ip + 2] = POP();
721 case ByteCodeInstruction::kStore2: stack[*ip + 1] = POP();
722 case ByteCodeInstruction::kStore : stack[*ip + 0] = POP();
723 ++ip;
724 break;
725
726 case ByteCodeInstruction::kStoreGlobal4: fGlobals[*ip + 3] = POP();
727 case ByteCodeInstruction::kStoreGlobal3: fGlobals[*ip + 2] = POP();
728 case ByteCodeInstruction::kStoreGlobal2: fGlobals[*ip + 1] = POP();
729 case ByteCodeInstruction::kStoreGlobal : fGlobals[*ip + 0] = POP();
730 ++ip;
731 break;
732
Brian Osman07c117b2019-05-23 12:51:06 -0700733 case ByteCodeInstruction::kStoreExtended: {
734 int count = READ8();
735 int target = POP().fSigned;
736 memcpy(&stack[target], sp - count + 1, count * sizeof(Value));
737 sp -= count;
738 break;
739 }
740 case ByteCodeInstruction::kStoreExtendedGlobal: {
741 int count = READ8();
742 int target = POP().fSigned;
743 SkASSERT(target + count <= (int) fGlobals.size());
744 memcpy(&fGlobals[target], sp - count + 1, count * sizeof(Value));
745 sp -= count;
746 break;
747 }
748
Mike Kleine7007382019-05-21 08:36:32 -0500749 case ByteCodeInstruction::kStoreSwizzle: {
750 int target = READ8();
751 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400752 for (int i = count - 1; i >= 0; --i) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400753 stack[target + *(ip + i)] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400754 }
Brian Osman1091f022019-05-16 09:42:16 -0400755 ip += count;
756 break;
Mike Kleine7007382019-05-21 08:36:32 -0500757 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400758
Mike Kleine7007382019-05-21 08:36:32 -0500759 case ByteCodeInstruction::kStoreSwizzleGlobal: {
760 int target = READ8();
761 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400762 for (int i = count - 1; i >= 0; --i) {
763 fGlobals[target + *(ip + i)] = POP();
764 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400765 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400766 break;
Mike Kleine7007382019-05-21 08:36:32 -0500767 }
Brian Osman07c117b2019-05-23 12:51:06 -0700768 case ByteCodeInstruction::kStoreSwizzleIndirect: {
769 int target = POP().fSigned;
770 int count = READ8();
771 for (int i = count - 1; i >= 0; --i) {
772 stack[target + *(ip + i)] = POP();
773 }
774 ip += count;
775 break;
776 }
777 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
778 int target = POP().fSigned;
779 int count = READ8();
780 for (int i = count - 1; i >= 0; --i) {
781 fGlobals[target + *(ip + i)] = POP();
782 }
783 ip += count;
784 break;
785 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400786
Mike Kleinc1999982019-05-21 13:03:49 -0500787 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
Brian Osman1e855b22019-05-29 15:21:52 -0400788 VECTOR_MATRIX_BINARY_OP(kSubtractF, fFloat, -)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400789
Mike Kleine7007382019-05-21 08:36:32 -0500790 case ByteCodeInstruction::kSwizzle: {
791 Value tmp[4];
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400792 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400793 tmp[i] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400794 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400795 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400796 PUSH(tmp[READ8()]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400797 }
798 break;
Mike Kleine7007382019-05-21 08:36:32 -0500799 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400800
Mike Klein459aed12019-05-21 15:46:36 -0500801 VECTOR_UNARY_FN(kTan, tanf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400802
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400803 case ByteCodeInstruction::kWriteExternal: // fall through
804 case ByteCodeInstruction::kWriteExternal2: // fall through
805 case ByteCodeInstruction::kWriteExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500806 case ByteCodeInstruction::kWriteExternal4: {
807 int count = (int) inst - (int) ByteCodeInstruction::kWriteExternal + 1;
808 int target = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400809 fByteCode->fExternalValues[target]->write(sp - count + 1);
810 sp -= count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400811 break;
Mike Kleine7007382019-05-21 08:36:32 -0500812 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400813
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400814 default:
Mike Kleine7007382019-05-21 08:36:32 -0500815 SkDEBUGFAILF("unsupported instruction %d\n", (int) inst);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400816 }
817#ifdef TRACE
Mike Kleine7007382019-05-21 08:36:32 -0500818 int stackSize = (int) (sp - stack + 1);
819 printf("STACK(%d):", stackSize);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400820 for (int i = 0; i < stackSize; ++i) {
Brian Osmane85b6a52019-05-22 14:50:59 -0700821 printf(" %d(%g)", stack[i].fSigned, stack[i].fFloat);
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400822 }
823 printf("\n");
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400824#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400825 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400826}
827
828} // namespace
829
830#endif