blob: 744a657c431221c603b56d3e27c1001b61ad3995 [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 Osman3e833e12019-05-23 13:23:24 -070095static 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 Nicholasae9633b2019-05-24 12:46:34 -0400168 VECTOR_DISASSEMBLE(kMix, "mix")
Brian Osman3e833e12019-05-23 13:23:24 -0700169 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 Nicholas0e9401d2019-03-21 11:05:37 -0400260void Interpreter::disassemble(const ByteCodeFunction& f) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400261 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 Osman3e833e12019-05-23 13:23:24 -0700264 ip = disassemble_instruction(ip);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400265 printf("\n");
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400266 }
267}
268
Mike Klein459aed12019-05-21 15:46:36 -0500269#define VECTOR_BINARY_OP(base, field, op) \
Mike Kleine7007382019-05-21 08:36:32 -0500270 case ByteCodeInstruction::base ## 4: \
Mike Klein459aed12019-05-21 15:46:36 -0500271 sp[-4] = sp[-4].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500272 POP(); \
273 /* fall through */ \
274 case ByteCodeInstruction::base ## 3: { \
275 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500276 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500277 POP(); \
278 } /* fall through */ \
279 case ByteCodeInstruction::base ## 2: { \
280 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500281 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500282 POP(); \
283 } /* fall through */ \
284 case ByteCodeInstruction::base: { \
285 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500286 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500287 POP(); \
288 break; \
289 }
Ethan Nicholasaeb71ce2019-05-20 09:55:44 -0400290
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400291#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 Kleine7007382019-05-21 08:36:32 -0500311 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400312
Mike Klein459aed12019-05-21 15:46:36 -0500313#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 Nicholas82162ee2019-05-21 16:05:08 -0400318 break;
319
Brian Osman226668a2019-05-14 16:47:30 -0400320struct StackFrame {
321 const uint8_t* fCode;
322 const uint8_t* fIP;
323 Interpreter::Value* fStack;
324};
325
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400326static float mix(float start, float end, float t) {
327 return start * (1 - t) + end * t;
328}
329
Mike Kleine7007382019-05-21 08:36:32 -0500330void 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 Osman226668a2019-05-14 16:47:30 -0400336 const uint8_t* code = f.fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400337 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400338 std::vector<StackFrame> frames;
339
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400340 for (;;) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400341#ifdef TRACE
Brian Osman3e833e12019-05-23 13:23:24 -0700342 printf("at %3d ", (int) (ip - code));
343 disassemble_instruction(ip);
344 printf("\n");
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400345#endif
Brian Osmane85b6a52019-05-22 14:50:59 -0700346 ByteCodeInstruction inst = (ByteCodeInstruction) READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400347 switch (inst) {
Mike Kleinc1999982019-05-21 13:03:49 -0500348 VECTOR_BINARY_OP(kAddI, fSigned, +)
349 VECTOR_BINARY_OP(kAddF, fFloat, +)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400350
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400351 case ByteCodeInstruction::kBranch:
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400352 ip = code + READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400353 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400354
Brian Osman226668a2019-05-14 16:47:30 -0400355 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 Kleine7007382019-05-21 08:36:32 -0500359 int target = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400360 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 Nicholas82162ee2019-05-21 16:05:08 -0400367
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400368 case ByteCodeInstruction::kCallExternal: {
369 int argumentCount = READ8();
370 int returnCount = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500371 int target = READ8();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400372 ExternalValue* v = fByteCode->fExternalValues[target];
373 sp -= argumentCount - 1;
Mike Kleine7007382019-05-21 08:36:32 -0500374
375 Value tmp[4];
376 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmp));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400377 v->call(sp, tmp);
378 memcpy(sp, tmp, returnCount * sizeof(Value));
379 sp += returnCount - 1;
380 break;
381 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400382
Mike Kleinc1999982019-05-21 13:03:49 -0500383 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 Nicholas82162ee2019-05-21 16:05:08 -0400399
Mike Kleine7007382019-05-21 08:36:32 -0500400 case ByteCodeInstruction::kConditionalBranch: {
401 int target = READ16();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400402 if (POP().fBool) {
Mike Kleine7007382019-05-21 08:36:32 -0500403 ip = code + target;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400404 }
405 break;
Mike Kleine7007382019-05-21 08:36:32 -0500406 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400407
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 Klein459aed12019-05-21 15:46:36 -0500426 VECTOR_UNARY_FN(kCos, cosf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400427
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400428 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 Kleine7007382019-05-21 08:36:32 -0500442 case ByteCodeInstruction::kDebugPrint: {
443 Value v = POP();
444 printf("Debug: %d(int), %d(uint), %f(float)\n", v.fSigned, v.fUnsigned, v.fFloat);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400445 break;
Mike Kleine7007382019-05-21 08:36:32 -0500446 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400447
Mike Kleinc1999982019-05-21 13:03:49 -0500448 VECTOR_BINARY_OP(kDivideS, fSigned, /)
449 VECTOR_BINARY_OP(kDivideU, fUnsigned, /)
450 VECTOR_BINARY_OP(kDivideF, fFloat, /)
Mike Kleine7007382019-05-21 08:36:32 -0500451
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 Osman07c117b2019-05-23 12:51:06 -0700458 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 Kleine7007382019-05-21 08:36:32 -0500465 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 Osman07c117b2019-05-23 12:51:06 -0700478 sp += (int)inst -
479 (int)ByteCodeInstruction::kLoadGlobal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500480 break;
481
Brian Osman07c117b2019-05-23 12:51:06 -0700482 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 Kleine7007382019-05-21 08:36:32 -0500499 case ByteCodeInstruction::kLoadSwizzle: {
500 int src = READ8();
501 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400502 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400503 PUSH(stack[src + *(ip + i)]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400504 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400505 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400506 break;
Mike Kleine7007382019-05-21 08:36:32 -0500507 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400508
Mike Kleine7007382019-05-21 08:36:32 -0500509 case ByteCodeInstruction::kLoadSwizzleGlobal: {
510 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400511 SkASSERT(src < (int) fGlobals.size());
Mike Kleine7007382019-05-21 08:36:32 -0500512 int count = READ8();
Brian Osmanb7451292019-05-15 13:02:13 -0400513 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400514 PUSH(fGlobals[src + *(ip + i)]);
Brian Osmanb7451292019-05-15 13:02:13 -0400515 }
516 ip += count;
517 break;
Mike Kleine7007382019-05-21 08:36:32 -0500518 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400519
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400520 // 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 Kleinc1999982019-05-21 13:03:49 -0500544 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
545 VECTOR_BINARY_OP(kMultiplyF, fFloat, *)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400546
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400547 case ByteCodeInstruction::kNot:
Mike Kleine7007382019-05-21 08:36:32 -0500548 sp[0].fBool = !sp[0].fBool;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400549 break;
Mike Kleine7007382019-05-21 08:36:32 -0500550
Mike Kleinc1999982019-05-21 13:03:49 -0500551 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 Kleine7007382019-05-21 08:36:32 -0500555 break;
556
Mike Kleinc1999982019-05-21 13:03:49 -0500557 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 Kleine7007382019-05-21 08:36:32 -0500561 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 Osman07c117b2019-05-23 12:51:06 -0700569 case ByteCodeInstruction::kPopN:
570 sp -= READ8();
571 break;
572
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400573 case ByteCodeInstruction::kPushImmediate:
Mike Kleine7007382019-05-21 08:36:32 -0500574 PUSH(READ32());
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400575 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400576
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400577 case ByteCodeInstruction::kReadExternal: // fall through
578 case ByteCodeInstruction::kReadExternal2: // fall through
579 case ByteCodeInstruction::kReadExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500580 case ByteCodeInstruction::kReadExternal4: {
581 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400582 fByteCode->fExternalValues[src]->read(sp + 1);
Mike Kleine7007382019-05-21 08:36:32 -0500583 sp += (int) inst - (int) ByteCodeInstruction::kReadExternal + 1;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400584 break;
Mike Kleine7007382019-05-21 08:36:32 -0500585 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400586
Mike Kleinc1999982019-05-21 13:03:49 -0500587 VECTOR_BINARY_FN(kRemainderF, fFloat, fmodf)
588 VECTOR_BINARY_OP(kRemainderS, fSigned, %)
589 VECTOR_BINARY_OP(kRemainderU, fUnsigned, %)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400590
Mike Kleine7007382019-05-21 08:36:32 -0500591 case ByteCodeInstruction::kReturn: {
592 int count = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400593 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 Nicholas9764ebd2019-05-01 14:43:54 -0400612 }
Mike Kleine7007382019-05-21 08:36:32 -0500613 }
614
Mike Klein459aed12019-05-21 15:46:36 -0500615 VECTOR_UNARY_FN(kSin, sinf, fFloat)
616 VECTOR_UNARY_FN(kSqrt, sqrtf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400617
Mike Kleine7007382019-05-21 08:36:32 -0500618 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 Osman07c117b2019-05-23 12:51:06 -0700632 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 Kleine7007382019-05-21 08:36:32 -0500648 case ByteCodeInstruction::kStoreSwizzle: {
649 int target = READ8();
650 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400651 for (int i = count - 1; i >= 0; --i) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400652 stack[target + *(ip + i)] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400653 }
Brian Osman1091f022019-05-16 09:42:16 -0400654 ip += count;
655 break;
Mike Kleine7007382019-05-21 08:36:32 -0500656 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400657
Mike Kleine7007382019-05-21 08:36:32 -0500658 case ByteCodeInstruction::kStoreSwizzleGlobal: {
659 int target = READ8();
660 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400661 for (int i = count - 1; i >= 0; --i) {
662 fGlobals[target + *(ip + i)] = POP();
663 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400664 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400665 break;
Mike Kleine7007382019-05-21 08:36:32 -0500666 }
Brian Osman07c117b2019-05-23 12:51:06 -0700667 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 Nicholas82162ee2019-05-21 16:05:08 -0400685
Mike Kleinc1999982019-05-21 13:03:49 -0500686 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
687 VECTOR_BINARY_OP(kSubtractF, fFloat, -)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400688
Mike Kleine7007382019-05-21 08:36:32 -0500689 case ByteCodeInstruction::kSwizzle: {
690 Value tmp[4];
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400691 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400692 tmp[i] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400693 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400694 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400695 PUSH(tmp[READ8()]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400696 }
697 break;
Mike Kleine7007382019-05-21 08:36:32 -0500698 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400699
Mike Klein459aed12019-05-21 15:46:36 -0500700 VECTOR_UNARY_FN(kTan, tanf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400701
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400702 case ByteCodeInstruction::kWriteExternal: // fall through
703 case ByteCodeInstruction::kWriteExternal2: // fall through
704 case ByteCodeInstruction::kWriteExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500705 case ByteCodeInstruction::kWriteExternal4: {
706 int count = (int) inst - (int) ByteCodeInstruction::kWriteExternal + 1;
707 int target = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400708 fByteCode->fExternalValues[target]->write(sp - count + 1);
709 sp -= count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400710 break;
Mike Kleine7007382019-05-21 08:36:32 -0500711 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400712
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400713 default:
Mike Kleine7007382019-05-21 08:36:32 -0500714 SkDEBUGFAILF("unsupported instruction %d\n", (int) inst);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400715 }
716#ifdef TRACE
Mike Kleine7007382019-05-21 08:36:32 -0500717 int stackSize = (int) (sp - stack + 1);
718 printf("STACK(%d):", stackSize);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400719 for (int i = 0; i < stackSize; ++i) {
Brian Osmane85b6a52019-05-22 14:50:59 -0700720 printf(" %d(%g)", stack[i].fSigned, stack[i].fFloat);
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400721 }
722 printf("\n");
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400723#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400724 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400725}
726
727} // namespace
728
729#endif