blob: a2ddb99c335233702ff9e14139ac1160c5b2f228 [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;
Brian Osman29e013d2019-05-28 17:16:03 -0400168 case ByteCodeInstruction::kMatrixToMatrix: {
169 int srcCols = READ8();
170 int srcRows = READ8();
171 int dstCols = READ8();
172 int dstRows = READ8();
173 printf("matrixtomatrix %dx%d %dx%d", srcCols, srcRows, dstCols, dstRows);
174 break;
175 }
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400176 VECTOR_DISASSEMBLE(kMix, "mix")
Brian Osman3e833e12019-05-23 13:23:24 -0700177 VECTOR_DISASSEMBLE(kMultiplyF, "multiplyf")
178 VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi")
179 VECTOR_DISASSEMBLE(kNegateF, "negatef")
180 VECTOR_DISASSEMBLE(kNegateI, "negatei")
181 VECTOR_DISASSEMBLE(kNot, "not")
182 VECTOR_DISASSEMBLE(kOrB, "orb")
183 VECTOR_DISASSEMBLE(kOrI, "ori")
184 VECTOR_DISASSEMBLE(kPop, "pop")
185 case ByteCodeInstruction::kPopN: printf("popN %d", READ8()); break;
186 case ByteCodeInstruction::kPushImmediate: {
187 uint32_t v = READ32();
188 union { uint32_t u; float f; } pun = { v };
189 printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str());
190 break;
191 }
192 case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break;
193 case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break;
194 case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break;
195 case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break;
196 VECTOR_DISASSEMBLE(kRemainderF, "remainderf")
197 VECTOR_DISASSEMBLE(kRemainderS, "remainders")
198 VECTOR_DISASSEMBLE(kRemainderU, "remainderu")
199 case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break;
Brian Osman29e013d2019-05-28 17:16:03 -0400200 case ByteCodeInstruction::kScalarToMatrix: {
201 int cols = READ8();
202 int rows = READ8();
203 printf("scalartomatrix %dx%d", cols, rows);
204 break;
205 }
Brian Osman3e833e12019-05-23 13:23:24 -0700206 VECTOR_DISASSEMBLE(kSin, "sin")
207 VECTOR_DISASSEMBLE(kSqrt, "sqrt")
208 case ByteCodeInstruction::kStore: printf("store %d", READ8()); break;
209 case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break;
210 case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break;
211 case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break;
212 case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break;
213 case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break;
214 case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break;
215 case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break;
216 case ByteCodeInstruction::kStoreSwizzle: {
217 int target = READ8();
218 int count = READ8();
219 printf("storeswizzle %d %d", target, count);
220 for (int i = 0; i < count; ++i) {
221 printf(", %d", READ8());
222 }
223 break;
224 }
225 case ByteCodeInstruction::kStoreSwizzleGlobal: {
226 int target = READ8();
227 int count = READ8();
228 printf("storeswizzleglobal %d %d", target, count);
229 for (int i = 0; i < count; ++i) {
230 printf(", %d", READ8());
231 }
232 break;
233 }
234 case ByteCodeInstruction::kStoreSwizzleIndirect: {
235 int count = READ8();
236 printf("storeswizzleindirect %d", count);
237 for (int i = 0; i < count; ++i) {
238 printf(", %d", READ8());
239 }
240 break;
241 }
242 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
243 int count = READ8();
244 printf("storeswizzleindirectglobal %d", count);
245 for (int i = 0; i < count; ++i) {
246 printf(", %d", READ8());
247 }
248 break;
249 }
250 case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break;
251 case ByteCodeInstruction::kStoreExtendedGlobal: printf("storeextendedglobal %d", READ8());
252 break;
253 VECTOR_DISASSEMBLE(kSubtractF, "subtractf")
254 VECTOR_DISASSEMBLE(kSubtractI, "subtracti")
255 case ByteCodeInstruction::kSwizzle: {
256 printf("swizzle %d, ", READ8());
257 int count = READ8();
258 printf("%d", count);
259 for (int i = 0; i < count; ++i) {
260 printf(", %d", READ8());
261 }
262 break;
263 }
264 VECTOR_DISASSEMBLE(kTan, "tan")
265 case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break;
266 case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break;
267 case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break;
268 case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break;
269 default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false);
270 }
271 return ip;
272}
273
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400274void Interpreter::disassemble(const ByteCodeFunction& f) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400275 const uint8_t* ip = f.fCode.data();
276 while (ip < f.fCode.data() + f.fCode.size()) {
277 printf("%d: ", (int) (ip - f.fCode.data()));
Brian Osman3e833e12019-05-23 13:23:24 -0700278 ip = disassemble_instruction(ip);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400279 printf("\n");
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400280 }
281}
282
Mike Klein459aed12019-05-21 15:46:36 -0500283#define VECTOR_BINARY_OP(base, field, op) \
Mike Kleine7007382019-05-21 08:36:32 -0500284 case ByteCodeInstruction::base ## 4: \
Mike Klein459aed12019-05-21 15:46:36 -0500285 sp[-4] = sp[-4].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500286 POP(); \
287 /* fall through */ \
288 case ByteCodeInstruction::base ## 3: { \
289 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500290 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500291 POP(); \
292 } /* fall through */ \
293 case ByteCodeInstruction::base ## 2: { \
294 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500295 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500296 POP(); \
297 } /* fall through */ \
298 case ByteCodeInstruction::base: { \
299 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500300 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500301 POP(); \
302 break; \
303 }
Ethan Nicholasaeb71ce2019-05-20 09:55:44 -0400304
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400305#define VECTOR_BINARY_FN(base, field, fn) \
306 case ByteCodeInstruction::base ## 4: \
307 sp[-4] = fn(sp[-4].field, sp[0].field); \
308 POP(); \
309 /* fall through */ \
310 case ByteCodeInstruction::base ## 3: { \
311 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
312 sp[target] = fn(sp[target].field, sp[0].field); \
313 POP(); \
314 } /* fall through */ \
315 case ByteCodeInstruction::base ## 2: { \
316 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
317 sp[target] = fn(sp[target].field, sp[0].field); \
318 POP(); \
319 } /* fall through */ \
320 case ByteCodeInstruction::base: { \
321 int target = (int) ByteCodeInstruction::base - (int) inst - 1; \
322 sp[target] = fn(sp[target].field, sp[0].field); \
323 POP(); \
324 break; \
Mike Kleine7007382019-05-21 08:36:32 -0500325 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400326
Mike Klein459aed12019-05-21 15:46:36 -0500327#define VECTOR_UNARY_FN(base, fn, field) \
328 case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \
329 case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \
330 case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \
331 case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400332 break;
333
Brian Osman226668a2019-05-14 16:47:30 -0400334struct StackFrame {
335 const uint8_t* fCode;
336 const uint8_t* fIP;
337 Interpreter::Value* fStack;
338};
339
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400340static float mix(float start, float end, float t) {
341 return start * (1 - t) + end * t;
342}
343
Mike Kleine7007382019-05-21 08:36:32 -0500344void Interpreter::innerRun(const ByteCodeFunction& f, Value* stack, Value* outReturn) {
345 Value* sp = stack + f.fParameterCount + f.fLocalCount - 1;
346
347 auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); };
348 auto PUSH = [&](Value v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; };
349
Brian Osman226668a2019-05-14 16:47:30 -0400350 const uint8_t* code = f.fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400351 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400352 std::vector<StackFrame> frames;
353
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400354 for (;;) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400355#ifdef TRACE
Brian Osman3e833e12019-05-23 13:23:24 -0700356 printf("at %3d ", (int) (ip - code));
357 disassemble_instruction(ip);
358 printf("\n");
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400359#endif
Brian Osmane85b6a52019-05-22 14:50:59 -0700360 ByteCodeInstruction inst = (ByteCodeInstruction) READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400361 switch (inst) {
Mike Kleinc1999982019-05-21 13:03:49 -0500362 VECTOR_BINARY_OP(kAddI, fSigned, +)
363 VECTOR_BINARY_OP(kAddF, fFloat, +)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400364
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400365 case ByteCodeInstruction::kBranch:
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400366 ip = code + READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400367 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400368
Brian Osman226668a2019-05-14 16:47:30 -0400369 case ByteCodeInstruction::kCall: {
370 // Precursor code has pushed all parameters to the stack. Update our bottom of
371 // stack to point at the first parameter, and our sp to point past those parameters
372 // (plus space for locals).
Mike Kleine7007382019-05-21 08:36:32 -0500373 int target = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400374 const ByteCodeFunction* fun = fByteCode->fFunctions[target].get();
375 frames.push_back({ code, ip, stack });
376 ip = code = fun->fCode.data();
377 stack = sp - fun->fParameterCount + 1;
378 sp = stack + fun->fParameterCount + fun->fLocalCount - 1;
379 break;
380 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400381
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400382 case ByteCodeInstruction::kCallExternal: {
383 int argumentCount = READ8();
384 int returnCount = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500385 int target = READ8();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400386 ExternalValue* v = fByteCode->fExternalValues[target];
387 sp -= argumentCount - 1;
Mike Kleine7007382019-05-21 08:36:32 -0500388
389 Value tmp[4];
390 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmp));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400391 v->call(sp, tmp);
392 memcpy(sp, tmp, returnCount * sizeof(Value));
393 sp += returnCount - 1;
394 break;
395 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400396
Mike Kleinc1999982019-05-21 13:03:49 -0500397 VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==)
398 VECTOR_BINARY_OP(kCompareFEQ, fFloat, ==)
399 VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=)
400 VECTOR_BINARY_OP(kCompareFNEQ, fFloat, !=)
401 VECTOR_BINARY_OP(kCompareSGT, fSigned, >)
402 VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >)
403 VECTOR_BINARY_OP(kCompareFGT, fFloat, >)
404 VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=)
405 VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=)
406 VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=)
407 VECTOR_BINARY_OP(kCompareSLT, fSigned, <)
408 VECTOR_BINARY_OP(kCompareULT, fUnsigned, <)
409 VECTOR_BINARY_OP(kCompareFLT, fFloat, <)
410 VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=)
411 VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=)
412 VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400413
Mike Kleine7007382019-05-21 08:36:32 -0500414 case ByteCodeInstruction::kConditionalBranch: {
415 int target = READ16();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400416 if (POP().fBool) {
Mike Kleine7007382019-05-21 08:36:32 -0500417 ip = code + target;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400418 }
419 break;
Mike Kleine7007382019-05-21 08:36:32 -0500420 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400421
422 case ByteCodeInstruction::kConvertFtoI4: sp[-3].fSigned = (int)sp[-3].fFloat;
423 case ByteCodeInstruction::kConvertFtoI3: sp[-2].fSigned = (int)sp[-2].fFloat;
424 case ByteCodeInstruction::kConvertFtoI2: sp[-1].fSigned = (int)sp[-1].fFloat;
425 case ByteCodeInstruction::kConvertFtoI: sp[ 0].fSigned = (int)sp[ 0].fFloat;
426 break;
427
428 case ByteCodeInstruction::kConvertStoF4: sp[-3].fFloat = sp[-3].fSigned;
429 case ByteCodeInstruction::kConvertStoF3: sp[-2].fFloat = sp[-2].fSigned;
430 case ByteCodeInstruction::kConvertStoF2: sp[-1].fFloat = sp[-1].fSigned;
431 case ByteCodeInstruction::kConvertStoF : sp[ 0].fFloat = sp[ 0].fSigned;
432 break;
433
434 case ByteCodeInstruction::kConvertUtoF4: sp[-3].fFloat = sp[-3].fUnsigned;
435 case ByteCodeInstruction::kConvertUtoF3: sp[-2].fFloat = sp[-2].fUnsigned;
436 case ByteCodeInstruction::kConvertUtoF2: sp[-1].fFloat = sp[-1].fUnsigned;
437 case ByteCodeInstruction::kConvertUtoF : sp[ 0].fFloat = sp[ 0].fUnsigned;
438 break;
439
Mike Klein459aed12019-05-21 15:46:36 -0500440 VECTOR_UNARY_FN(kCos, cosf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400441
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400442 case ByteCodeInstruction::kCross: {
443 SkPoint3 cross = SkPoint3::CrossProduct(SkPoint3::Make(sp[-5].fFloat,
444 sp[-4].fFloat,
445 sp[-3].fFloat),
446 SkPoint3::Make(sp[-2].fFloat,
447 sp[-1].fFloat,
448 sp[ 0].fFloat));
449 sp -= 3;
450 sp[-2] = cross.fX;
451 sp[-1] = cross.fY;
452 sp[ 0] = cross.fZ;
453 break;
454 }
455
Mike Kleine7007382019-05-21 08:36:32 -0500456 case ByteCodeInstruction::kDebugPrint: {
457 Value v = POP();
458 printf("Debug: %d(int), %d(uint), %f(float)\n", v.fSigned, v.fUnsigned, v.fFloat);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400459 break;
Mike Kleine7007382019-05-21 08:36:32 -0500460 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400461
Mike Kleinc1999982019-05-21 13:03:49 -0500462 VECTOR_BINARY_OP(kDivideS, fSigned, /)
463 VECTOR_BINARY_OP(kDivideU, fUnsigned, /)
464 VECTOR_BINARY_OP(kDivideF, fFloat, /)
Mike Kleine7007382019-05-21 08:36:32 -0500465
466 case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
467 case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
468 case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
469 case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
470 break;
471
Brian Osman07c117b2019-05-23 12:51:06 -0700472 case ByteCodeInstruction::kDupN: {
473 int count = READ8();
474 memcpy(sp + 1, sp - count + 1, count * sizeof(Value));
475 sp += count;
476 break;
477 }
478
Mike Kleine7007382019-05-21 08:36:32 -0500479 case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3];
480 case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2];
481 case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1];
482 case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0];
483 ++ip;
484 sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1;
485 break;
486
487 case ByteCodeInstruction::kLoadGlobal4: sp[4] = fGlobals[*ip + 3];
488 case ByteCodeInstruction::kLoadGlobal3: sp[3] = fGlobals[*ip + 2];
489 case ByteCodeInstruction::kLoadGlobal2: sp[2] = fGlobals[*ip + 1];
490 case ByteCodeInstruction::kLoadGlobal : sp[1] = fGlobals[*ip + 0];
491 ++ip;
Brian Osman07c117b2019-05-23 12:51:06 -0700492 sp += (int)inst -
493 (int)ByteCodeInstruction::kLoadGlobal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500494 break;
495
Brian Osman07c117b2019-05-23 12:51:06 -0700496 case ByteCodeInstruction::kLoadExtended: {
497 int count = READ8();
498 int src = POP().fSigned;
499 memcpy(sp + 1, &stack[src], count * sizeof(Value));
500 sp += count;
501 break;
502 }
503
504 case ByteCodeInstruction::kLoadExtendedGlobal: {
505 int count = READ8();
506 int src = POP().fSigned;
507 SkASSERT(src + count <= (int) fGlobals.size());
508 memcpy(sp + 1, &fGlobals[src], count * sizeof(Value));
509 sp += count;
510 break;
511 }
512
Mike Kleine7007382019-05-21 08:36:32 -0500513 case ByteCodeInstruction::kLoadSwizzle: {
514 int src = READ8();
515 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400516 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400517 PUSH(stack[src + *(ip + i)]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400518 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400519 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400520 break;
Mike Kleine7007382019-05-21 08:36:32 -0500521 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400522
Mike Kleine7007382019-05-21 08:36:32 -0500523 case ByteCodeInstruction::kLoadSwizzleGlobal: {
524 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400525 SkASSERT(src < (int) fGlobals.size());
Mike Kleine7007382019-05-21 08:36:32 -0500526 int count = READ8();
Brian Osmanb7451292019-05-15 13:02:13 -0400527 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400528 PUSH(fGlobals[src + *(ip + i)]);
Brian Osmanb7451292019-05-15 13:02:13 -0400529 }
530 ip += count;
531 break;
Mike Kleine7007382019-05-21 08:36:32 -0500532 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400533
Brian Osman29e013d2019-05-28 17:16:03 -0400534 case ByteCodeInstruction::kMatrixToMatrix: {
535 int srcCols = READ8();
536 int srcRows = READ8();
537 int dstCols = READ8();
538 int dstRows = READ8();
539 SkASSERT(srcCols >= 2 && srcCols <= 4);
540 SkASSERT(srcRows >= 2 && srcRows <= 4);
541 SkASSERT(dstCols >= 2 && dstCols <= 4);
542 SkASSERT(dstRows >= 2 && dstRows <= 4);
543 SkMatrix44 m;
544 for (int c = srcCols - 1; c >= 0; --c) {
545 for (int r = srcRows - 1; r >= 0; --r) {
546 m.set(r, c, POP().fFloat);
547 }
548 }
549 for (int c = 0; c < dstCols; ++c) {
550 for (int r = 0; r < dstRows; ++r) {
551 PUSH(m.get(r, c));
552 }
553 }
554 break;
555 }
556
Ethan Nicholasae9633b2019-05-24 12:46:34 -0400557 // stack looks like: X1 Y1 Z1 W1 X2 Y2 Z2 W2 T
558 case ByteCodeInstruction::kMix4:
559 sp[-5] = mix(sp[-5].fFloat, sp[-1].fFloat, sp[0].fFloat);
560 // fall through
561 case ByteCodeInstruction::kMix3: {
562 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
563 int target = 2 - count * 2;
564 sp[target] = mix(sp[target].fFloat, sp[2 - count].fFloat, sp[0].fFloat);
565 // fall through
566 }
567 case ByteCodeInstruction::kMix2: {
568 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
569 int target = 1 - count * 2;
570 sp[target] = mix(sp[target].fFloat, sp[1 - count].fFloat, sp[0].fFloat);
571 // fall through
572 }
573 case ByteCodeInstruction::kMix: {
574 int count = (int) inst - (int) ByteCodeInstruction::kMix + 1;
575 int target = -count * 2;
576 sp[target] = mix(sp[target].fFloat, sp[-count].fFloat, sp[0].fFloat);
577 sp -= 1 + count;
578 break;
579 }
580
Mike Kleinc1999982019-05-21 13:03:49 -0500581 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
582 VECTOR_BINARY_OP(kMultiplyF, fFloat, *)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400583
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400584 case ByteCodeInstruction::kNot:
Mike Kleine7007382019-05-21 08:36:32 -0500585 sp[0].fBool = !sp[0].fBool;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400586 break;
Mike Kleine7007382019-05-21 08:36:32 -0500587
Mike Kleinc1999982019-05-21 13:03:49 -0500588 case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat;
589 case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat;
590 case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat;
591 case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat;
Mike Kleine7007382019-05-21 08:36:32 -0500592 break;
593
Mike Kleinc1999982019-05-21 13:03:49 -0500594 case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned;
595 case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned;
596 case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned;
597 case ByteCodeInstruction::kNegateI : sp[ 0] = -sp [0].fSigned;
Mike Kleine7007382019-05-21 08:36:32 -0500598 break;
599
600 case ByteCodeInstruction::kPop4: POP();
601 case ByteCodeInstruction::kPop3: POP();
602 case ByteCodeInstruction::kPop2: POP();
603 case ByteCodeInstruction::kPop : POP();
604 break;
605
Brian Osman07c117b2019-05-23 12:51:06 -0700606 case ByteCodeInstruction::kPopN:
607 sp -= READ8();
608 break;
609
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400610 case ByteCodeInstruction::kPushImmediate:
Mike Kleine7007382019-05-21 08:36:32 -0500611 PUSH(READ32());
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400612 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400613
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400614 case ByteCodeInstruction::kReadExternal: // fall through
615 case ByteCodeInstruction::kReadExternal2: // fall through
616 case ByteCodeInstruction::kReadExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500617 case ByteCodeInstruction::kReadExternal4: {
618 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400619 fByteCode->fExternalValues[src]->read(sp + 1);
Mike Kleine7007382019-05-21 08:36:32 -0500620 sp += (int) inst - (int) ByteCodeInstruction::kReadExternal + 1;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400621 break;
Mike Kleine7007382019-05-21 08:36:32 -0500622 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400623
Mike Kleinc1999982019-05-21 13:03:49 -0500624 VECTOR_BINARY_FN(kRemainderF, fFloat, fmodf)
625 VECTOR_BINARY_OP(kRemainderS, fSigned, %)
626 VECTOR_BINARY_OP(kRemainderU, fUnsigned, %)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400627
Mike Kleine7007382019-05-21 08:36:32 -0500628 case ByteCodeInstruction::kReturn: {
629 int count = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400630 if (frames.empty()) {
631 if (outReturn) {
632 memcpy(outReturn, sp - count + 1, count * sizeof(Value));
633 }
634 return;
635 } else {
636 // When we were called, 'stack' was positioned at the old top-of-stack (where
637 // our parameters were placed). So copy our return values to that same spot.
638 memmove(stack, sp - count + 1, count * sizeof(Value));
639
640 // Now move the stack pointer to the end of the just-pushed return values,
641 // and restore everything else.
642 const StackFrame& frame(frames.back());
643 sp = stack + count - 1;
644 stack = frame.fStack;
645 code = frame.fCode;
646 ip = frame.fIP;
647 frames.pop_back();
648 break;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400649 }
Mike Kleine7007382019-05-21 08:36:32 -0500650 }
651
Brian Osman29e013d2019-05-28 17:16:03 -0400652 case ByteCodeInstruction::kScalarToMatrix: {
653 int cols = READ8();
654 int rows = READ8();
655 Value v = POP();
656 for (int c = 0; c < cols; ++c) {
657 for (int r = 0; r < rows; ++r) {
658 PUSH(c == r ? v : 0.0f);
659 }
660 }
661 break;
662 }
663
Mike Klein459aed12019-05-21 15:46:36 -0500664 VECTOR_UNARY_FN(kSin, sinf, fFloat)
665 VECTOR_UNARY_FN(kSqrt, sqrtf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400666
Mike Kleine7007382019-05-21 08:36:32 -0500667 case ByteCodeInstruction::kStore4: stack[*ip + 3] = POP();
668 case ByteCodeInstruction::kStore3: stack[*ip + 2] = POP();
669 case ByteCodeInstruction::kStore2: stack[*ip + 1] = POP();
670 case ByteCodeInstruction::kStore : stack[*ip + 0] = POP();
671 ++ip;
672 break;
673
674 case ByteCodeInstruction::kStoreGlobal4: fGlobals[*ip + 3] = POP();
675 case ByteCodeInstruction::kStoreGlobal3: fGlobals[*ip + 2] = POP();
676 case ByteCodeInstruction::kStoreGlobal2: fGlobals[*ip + 1] = POP();
677 case ByteCodeInstruction::kStoreGlobal : fGlobals[*ip + 0] = POP();
678 ++ip;
679 break;
680
Brian Osman07c117b2019-05-23 12:51:06 -0700681 case ByteCodeInstruction::kStoreExtended: {
682 int count = READ8();
683 int target = POP().fSigned;
684 memcpy(&stack[target], sp - count + 1, count * sizeof(Value));
685 sp -= count;
686 break;
687 }
688 case ByteCodeInstruction::kStoreExtendedGlobal: {
689 int count = READ8();
690 int target = POP().fSigned;
691 SkASSERT(target + count <= (int) fGlobals.size());
692 memcpy(&fGlobals[target], sp - count + 1, count * sizeof(Value));
693 sp -= count;
694 break;
695 }
696
Mike Kleine7007382019-05-21 08:36:32 -0500697 case ByteCodeInstruction::kStoreSwizzle: {
698 int target = READ8();
699 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400700 for (int i = count - 1; i >= 0; --i) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400701 stack[target + *(ip + i)] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400702 }
Brian Osman1091f022019-05-16 09:42:16 -0400703 ip += count;
704 break;
Mike Kleine7007382019-05-21 08:36:32 -0500705 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400706
Mike Kleine7007382019-05-21 08:36:32 -0500707 case ByteCodeInstruction::kStoreSwizzleGlobal: {
708 int target = READ8();
709 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400710 for (int i = count - 1; i >= 0; --i) {
711 fGlobals[target + *(ip + i)] = POP();
712 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400713 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400714 break;
Mike Kleine7007382019-05-21 08:36:32 -0500715 }
Brian Osman07c117b2019-05-23 12:51:06 -0700716 case ByteCodeInstruction::kStoreSwizzleIndirect: {
717 int target = POP().fSigned;
718 int count = READ8();
719 for (int i = count - 1; i >= 0; --i) {
720 stack[target + *(ip + i)] = POP();
721 }
722 ip += count;
723 break;
724 }
725 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
726 int target = POP().fSigned;
727 int count = READ8();
728 for (int i = count - 1; i >= 0; --i) {
729 fGlobals[target + *(ip + i)] = POP();
730 }
731 ip += count;
732 break;
733 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400734
Mike Kleinc1999982019-05-21 13:03:49 -0500735 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
736 VECTOR_BINARY_OP(kSubtractF, fFloat, -)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400737
Mike Kleine7007382019-05-21 08:36:32 -0500738 case ByteCodeInstruction::kSwizzle: {
739 Value tmp[4];
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400740 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400741 tmp[i] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400742 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400743 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400744 PUSH(tmp[READ8()]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400745 }
746 break;
Mike Kleine7007382019-05-21 08:36:32 -0500747 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400748
Mike Klein459aed12019-05-21 15:46:36 -0500749 VECTOR_UNARY_FN(kTan, tanf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400750
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400751 case ByteCodeInstruction::kWriteExternal: // fall through
752 case ByteCodeInstruction::kWriteExternal2: // fall through
753 case ByteCodeInstruction::kWriteExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500754 case ByteCodeInstruction::kWriteExternal4: {
755 int count = (int) inst - (int) ByteCodeInstruction::kWriteExternal + 1;
756 int target = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400757 fByteCode->fExternalValues[target]->write(sp - count + 1);
758 sp -= count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400759 break;
Mike Kleine7007382019-05-21 08:36:32 -0500760 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400761
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400762 default:
Mike Kleine7007382019-05-21 08:36:32 -0500763 SkDEBUGFAILF("unsupported instruction %d\n", (int) inst);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400764 }
765#ifdef TRACE
Mike Kleine7007382019-05-21 08:36:32 -0500766 int stackSize = (int) (sp - stack + 1);
767 printf("STACK(%d):", stackSize);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400768 for (int i = 0; i < stackSize; ++i) {
Brian Osmane85b6a52019-05-22 14:50:59 -0700769 printf(" %d(%g)", stack[i].fSigned, stack[i].fFloat);
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400770 }
771 printf("\n");
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400772#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400773 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400774}
775
776} // namespace
777
778#endif