blob: c97ecae63af068fd256c6d30a99edfa4de95271e [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkRasterPipeline.h"
Brian Osman07c117b2019-05-23 12:51:06 -070011#include "src/sksl/SkSLByteCodeGenerator.h"
Ethan Nicholas91164d12019-05-15 15:29:54 -040012#include "src/sksl/SkSLExternalValue.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "src/sksl/SkSLInterpreter.h"
14#include "src/sksl/ir/SkSLBinaryExpression.h"
15#include "src/sksl/ir/SkSLExpressionStatement.h"
16#include "src/sksl/ir/SkSLForStatement.h"
17#include "src/sksl/ir/SkSLFunctionCall.h"
18#include "src/sksl/ir/SkSLFunctionReference.h"
19#include "src/sksl/ir/SkSLIfStatement.h"
20#include "src/sksl/ir/SkSLIndexExpression.h"
21#include "src/sksl/ir/SkSLPostfixExpression.h"
22#include "src/sksl/ir/SkSLPrefixExpression.h"
23#include "src/sksl/ir/SkSLProgram.h"
24#include "src/sksl/ir/SkSLStatement.h"
25#include "src/sksl/ir/SkSLTernaryExpression.h"
26#include "src/sksl/ir/SkSLVarDeclarations.h"
27#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
28#include "src/sksl/ir/SkSLVariableReference.h"
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040029
30namespace SkSL {
31
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040032static constexpr int UNINITIALIZED = 0xDEADBEEF;
33
Mike Kleine7007382019-05-21 08:36:32 -050034Interpreter::Interpreter(std::unique_ptr<Program> program,
35 std::unique_ptr<ByteCode> byteCode,
Ethan Nicholasdfcad062019-05-07 12:53:34 -040036 Interpreter::Value inputs[])
37 : fProgram(std::move(program))
Mike Kleine7007382019-05-21 08:36:32 -050038 , fByteCode(std::move(byteCode))
39 , fGlobals(fByteCode->fGlobalCount, UNINITIALIZED) {
Brian Osmand369a5e2019-05-09 13:13:25 -040040 this->setInputs(inputs);
41}
42
43void Interpreter::setInputs(Interpreter::Value inputs[]) {
Mike Kleine7007382019-05-21 08:36:32 -050044 for (uint8_t slot : fByteCode->fInputSlots) {
45 fGlobals[slot] = *inputs++;
Ethan Nicholasdfcad062019-05-07 12:53:34 -040046 }
47}
48
49void Interpreter::run(const ByteCodeFunction& f, Interpreter::Value args[],
50 Interpreter::Value* outReturn) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040051#ifdef TRACE
52 this->disassemble(f);
53#endif
Ethan Nicholasdfcad062019-05-07 12:53:34 -040054 Value smallStack[128];
55 std::unique_ptr<Value[]> largeStack;
56 Value* stack = smallStack;
Brian Osman226668a2019-05-14 16:47:30 -040057 if ((int) SK_ARRAY_COUNT(smallStack) < f.fStackCount) {
58 largeStack.reset(new Value[f.fStackCount]);
Ethan Nicholasdfcad062019-05-07 12:53:34 -040059 stack = largeStack.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040060 }
Mike Kleine7007382019-05-21 08:36:32 -050061
62 if (f.fParameterCount) {
63 memcpy(stack, args, f.fParameterCount * sizeof(Value));
64 }
65 this->innerRun(f, stack, outReturn);
66
67 for (const Variable* p : f.fDeclaration.fParameters) {
Brian Osman07c117b2019-05-23 12:51:06 -070068 const int nvalues = ByteCodeGenerator::SlotCount(p->fType);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040069 if (p->fModifiers.fFlags & Modifiers::kOut_Flag) {
Mike Kleine7007382019-05-21 08:36:32 -050070 memcpy(args, stack, nvalues * sizeof(Value));
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040071 }
Mike Kleine7007382019-05-21 08:36:32 -050072 args += nvalues;
73 stack += nvalues;
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040074 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040075}
76
Mike Klein76346ac2019-05-17 11:57:10 -050077template <typename T>
78static T unaligned_load(const void* ptr) {
79 T val;
80 memcpy(&val, ptr, sizeof(val));
81 return val;
82}
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040083
Mike Kleine7007382019-05-21 08:36:32 -050084#define READ8() (*(ip++))
Mike Klein76346ac2019-05-17 11:57:10 -050085#define READ16() (ip += 2, unaligned_load<uint16_t>(ip - 2))
86#define READ32() (ip += 4, unaligned_load<uint32_t>(ip - 4))
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040087
Brian Osman3e833e12019-05-23 13:23:24 -070088#define VECTOR_DISASSEMBLE(op, text) \
Ethan Nicholas48a75aa2019-05-16 17:15:56 -040089 case ByteCodeInstruction::op: printf(text); break; \
90 case ByteCodeInstruction::op##2: printf(text "2"); break; \
91 case ByteCodeInstruction::op##3: printf(text "3"); break; \
92 case ByteCodeInstruction::op##4: printf(text "4"); break;
93
Brian Osman3e833e12019-05-23 13:23:24 -070094static const uint8_t* disassemble_instruction(const uint8_t* ip) {
95 switch ((ByteCodeInstruction) READ16()) {
96 VECTOR_DISASSEMBLE(kAddF, "addf")
97 VECTOR_DISASSEMBLE(kAddI, "addi")
98 case ByteCodeInstruction::kAndB: printf("andb"); break;
99 case ByteCodeInstruction::kAndI: printf("andi"); break;
100 case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break;
101 case ByteCodeInstruction::kCall: printf("call %d", READ8()); break;
102 case ByteCodeInstruction::kCallExternal: {
103 int argumentCount = READ8();
104 int returnCount = READ8();
105 int externalValue = READ8();
106 printf("callexternal %d, %d, %d", argumentCount, returnCount, externalValue);
107 break;
108 }
109 VECTOR_DISASSEMBLE(kCompareIEQ, "compareieq")
110 VECTOR_DISASSEMBLE(kCompareINEQ, "compareineq")
111 VECTOR_DISASSEMBLE(kCompareFEQ, "comparefeq")
112 VECTOR_DISASSEMBLE(kCompareFNEQ, "comparefneq")
113 VECTOR_DISASSEMBLE(kCompareFGT, "comparefgt")
114 VECTOR_DISASSEMBLE(kCompareFGTEQ, "comparefgteq")
115 VECTOR_DISASSEMBLE(kCompareFLT, "compareflt")
116 VECTOR_DISASSEMBLE(kCompareFLTEQ, "compareflteq")
117 VECTOR_DISASSEMBLE(kCompareSGT, "comparesgt")
118 VECTOR_DISASSEMBLE(kCompareSGTEQ, "comparesgteq")
119 VECTOR_DISASSEMBLE(kCompareSLT, "compareslt")
120 VECTOR_DISASSEMBLE(kCompareSLTEQ, "compareslteq")
121 VECTOR_DISASSEMBLE(kCompareUGT, "compareugt")
122 VECTOR_DISASSEMBLE(kCompareUGTEQ, "compareugteq")
123 VECTOR_DISASSEMBLE(kCompareULT, "compareult")
124 VECTOR_DISASSEMBLE(kCompareULTEQ, "compareulteq")
125 case ByteCodeInstruction::kConditionalBranch:
126 printf("conditionalbranch %d", READ16());
127 break;
128 VECTOR_DISASSEMBLE(kConvertFtoI, "convertftoi")
129 VECTOR_DISASSEMBLE(kConvertStoF, "convertstof")
130 VECTOR_DISASSEMBLE(kConvertUtoF, "convertutof")
131 VECTOR_DISASSEMBLE(kCos, "cos")
132 case ByteCodeInstruction::kDebugPrint: printf("debugprint"); break;
133 VECTOR_DISASSEMBLE(kDivideF, "dividef")
134 VECTOR_DISASSEMBLE(kDivideS, "divideS")
135 VECTOR_DISASSEMBLE(kDivideU, "divideu")
136 VECTOR_DISASSEMBLE(kDup, "dup")
137 case ByteCodeInstruction::kDupN: printf("dupN %d", READ8()); break;
138 case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break;
139 case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break;
140 case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break;
141 case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break;
142 case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break;
143 case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break;
144 case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break;
145 case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break;
146 case ByteCodeInstruction::kLoadSwizzle: {
147 int target = READ8();
148 int count = READ8();
149 printf("loadswizzle %d %d", target, count);
150 for (int i = 0; i < count; ++i) {
151 printf(", %d", READ8());
152 }
153 break;
154 }
155 case ByteCodeInstruction::kLoadSwizzleGlobal: {
156 int target = READ8();
157 int count = READ8();
158 printf("loadswizzleglobal %d %d", target, count);
159 for (int i = 0; i < count; ++i) {
160 printf(", %d", READ8());
161 }
162 break;
163 }
164 case ByteCodeInstruction::kLoadExtended: printf("loadextended %d", READ8()); break;
165 case ByteCodeInstruction::kLoadExtendedGlobal: printf("loadextendedglobal %d", READ8());
166 break;
167 VECTOR_DISASSEMBLE(kMultiplyF, "multiplyf")
168 VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi")
169 VECTOR_DISASSEMBLE(kNegateF, "negatef")
170 VECTOR_DISASSEMBLE(kNegateI, "negatei")
171 VECTOR_DISASSEMBLE(kNot, "not")
172 VECTOR_DISASSEMBLE(kOrB, "orb")
173 VECTOR_DISASSEMBLE(kOrI, "ori")
174 VECTOR_DISASSEMBLE(kPop, "pop")
175 case ByteCodeInstruction::kPopN: printf("popN %d", READ8()); break;
176 case ByteCodeInstruction::kPushImmediate: {
177 uint32_t v = READ32();
178 union { uint32_t u; float f; } pun = { v };
179 printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str());
180 break;
181 }
182 case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break;
183 case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break;
184 case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break;
185 case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break;
186 VECTOR_DISASSEMBLE(kRemainderF, "remainderf")
187 VECTOR_DISASSEMBLE(kRemainderS, "remainders")
188 VECTOR_DISASSEMBLE(kRemainderU, "remainderu")
189 case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break;
190 VECTOR_DISASSEMBLE(kSin, "sin")
191 VECTOR_DISASSEMBLE(kSqrt, "sqrt")
192 case ByteCodeInstruction::kStore: printf("store %d", READ8()); break;
193 case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break;
194 case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break;
195 case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break;
196 case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break;
197 case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break;
198 case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break;
199 case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break;
200 case ByteCodeInstruction::kStoreSwizzle: {
201 int target = READ8();
202 int count = READ8();
203 printf("storeswizzle %d %d", target, count);
204 for (int i = 0; i < count; ++i) {
205 printf(", %d", READ8());
206 }
207 break;
208 }
209 case ByteCodeInstruction::kStoreSwizzleGlobal: {
210 int target = READ8();
211 int count = READ8();
212 printf("storeswizzleglobal %d %d", target, count);
213 for (int i = 0; i < count; ++i) {
214 printf(", %d", READ8());
215 }
216 break;
217 }
218 case ByteCodeInstruction::kStoreSwizzleIndirect: {
219 int count = READ8();
220 printf("storeswizzleindirect %d", count);
221 for (int i = 0; i < count; ++i) {
222 printf(", %d", READ8());
223 }
224 break;
225 }
226 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
227 int count = READ8();
228 printf("storeswizzleindirectglobal %d", count);
229 for (int i = 0; i < count; ++i) {
230 printf(", %d", READ8());
231 }
232 break;
233 }
234 case ByteCodeInstruction::kStoreExtended: printf("storeextended %d", READ8()); break;
235 case ByteCodeInstruction::kStoreExtendedGlobal: printf("storeextendedglobal %d", READ8());
236 break;
237 VECTOR_DISASSEMBLE(kSubtractF, "subtractf")
238 VECTOR_DISASSEMBLE(kSubtractI, "subtracti")
239 case ByteCodeInstruction::kSwizzle: {
240 printf("swizzle %d, ", READ8());
241 int count = READ8();
242 printf("%d", count);
243 for (int i = 0; i < count; ++i) {
244 printf(", %d", READ8());
245 }
246 break;
247 }
248 VECTOR_DISASSEMBLE(kTan, "tan")
249 case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break;
250 case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break;
251 case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break;
252 case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break;
253 default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false);
254 }
255 return ip;
256}
257
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400258void Interpreter::disassemble(const ByteCodeFunction& f) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400259 const uint8_t* ip = f.fCode.data();
260 while (ip < f.fCode.data() + f.fCode.size()) {
261 printf("%d: ", (int) (ip - f.fCode.data()));
Brian Osman3e833e12019-05-23 13:23:24 -0700262 ip = disassemble_instruction(ip);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400263 printf("\n");
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400264 }
265}
266
Mike Klein459aed12019-05-21 15:46:36 -0500267#define VECTOR_BINARY_OP(base, field, op) \
Mike Kleine7007382019-05-21 08:36:32 -0500268 case ByteCodeInstruction::base ## 4: \
Mike Klein459aed12019-05-21 15:46:36 -0500269 sp[-4] = sp[-4].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500270 POP(); \
271 /* fall through */ \
272 case ByteCodeInstruction::base ## 3: { \
273 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500274 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500275 POP(); \
276 } /* fall through */ \
277 case ByteCodeInstruction::base ## 2: { \
278 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500279 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500280 POP(); \
281 } /* fall through */ \
282 case ByteCodeInstruction::base: { \
283 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500284 sp[count] = sp[count].field op sp[0].field; \
Mike Kleine7007382019-05-21 08:36:32 -0500285 POP(); \
286 break; \
287 }
Ethan Nicholasaeb71ce2019-05-20 09:55:44 -0400288
Mike Klein459aed12019-05-21 15:46:36 -0500289#define VECTOR_BINARY_FN(base, field, fn) \
Mike Kleine7007382019-05-21 08:36:32 -0500290 case ByteCodeInstruction::base ## 4: \
Mike Klein459aed12019-05-21 15:46:36 -0500291 sp[-4] = fn(sp[-4].field, sp[0].field); \
Mike Kleine7007382019-05-21 08:36:32 -0500292 POP(); \
293 /* fall through */ \
294 case ByteCodeInstruction::base ## 3: { \
295 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500296 sp[count] = fn(sp[count].field, sp[0].field); \
Mike Kleine7007382019-05-21 08:36:32 -0500297 POP(); \
298 } /* fall through */ \
299 case ByteCodeInstruction::base ## 2: { \
300 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500301 sp[count] = fn(sp[count].field, sp[0].field); \
Mike Kleine7007382019-05-21 08:36:32 -0500302 POP(); \
303 } /* fall through */ \
304 case ByteCodeInstruction::base: { \
305 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Klein459aed12019-05-21 15:46:36 -0500306 sp[count] = fn(sp[count].field, sp[0].field); \
Mike Kleine7007382019-05-21 08:36:32 -0500307 POP(); \
308 break; \
309 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400310
Mike Klein459aed12019-05-21 15:46:36 -0500311#define VECTOR_UNARY_FN(base, fn, field) \
312 case ByteCodeInstruction::base ## 4: sp[-3] = fn(sp[-3].field); \
313 case ByteCodeInstruction::base ## 3: sp[-2] = fn(sp[-2].field); \
314 case ByteCodeInstruction::base ## 2: sp[-1] = fn(sp[-1].field); \
315 case ByteCodeInstruction::base: sp[ 0] = fn(sp[ 0].field); \
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400316 break;
317
Brian Osman226668a2019-05-14 16:47:30 -0400318struct StackFrame {
319 const uint8_t* fCode;
320 const uint8_t* fIP;
321 Interpreter::Value* fStack;
322};
323
Mike Kleine7007382019-05-21 08:36:32 -0500324void Interpreter::innerRun(const ByteCodeFunction& f, Value* stack, Value* outReturn) {
325 Value* sp = stack + f.fParameterCount + f.fLocalCount - 1;
326
327 auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); };
328 auto PUSH = [&](Value v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; };
329
Brian Osman226668a2019-05-14 16:47:30 -0400330 const uint8_t* code = f.fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400331 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400332 std::vector<StackFrame> frames;
333
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400334 for (;;) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400335#ifdef TRACE
Brian Osman3e833e12019-05-23 13:23:24 -0700336 printf("at %3d ", (int) (ip - code));
337 disassemble_instruction(ip);
338 printf("\n");
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400339#endif
Brian Osmane85b6a52019-05-22 14:50:59 -0700340 ByteCodeInstruction inst = (ByteCodeInstruction) READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400341 switch (inst) {
Mike Kleinc1999982019-05-21 13:03:49 -0500342 VECTOR_BINARY_OP(kAddI, fSigned, +)
343 VECTOR_BINARY_OP(kAddF, fFloat, +)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400344
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400345 case ByteCodeInstruction::kBranch:
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400346 ip = code + READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400347 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400348
Brian Osman226668a2019-05-14 16:47:30 -0400349 case ByteCodeInstruction::kCall: {
350 // Precursor code has pushed all parameters to the stack. Update our bottom of
351 // stack to point at the first parameter, and our sp to point past those parameters
352 // (plus space for locals).
Mike Kleine7007382019-05-21 08:36:32 -0500353 int target = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400354 const ByteCodeFunction* fun = fByteCode->fFunctions[target].get();
355 frames.push_back({ code, ip, stack });
356 ip = code = fun->fCode.data();
357 stack = sp - fun->fParameterCount + 1;
358 sp = stack + fun->fParameterCount + fun->fLocalCount - 1;
359 break;
360 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400361
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400362 case ByteCodeInstruction::kCallExternal: {
363 int argumentCount = READ8();
364 int returnCount = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500365 int target = READ8();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400366 ExternalValue* v = fByteCode->fExternalValues[target];
367 sp -= argumentCount - 1;
Mike Kleine7007382019-05-21 08:36:32 -0500368
369 Value tmp[4];
370 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmp));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400371 v->call(sp, tmp);
372 memcpy(sp, tmp, returnCount * sizeof(Value));
373 sp += returnCount - 1;
374 break;
375 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400376
Mike Kleinc1999982019-05-21 13:03:49 -0500377 VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==)
378 VECTOR_BINARY_OP(kCompareFEQ, fFloat, ==)
379 VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=)
380 VECTOR_BINARY_OP(kCompareFNEQ, fFloat, !=)
381 VECTOR_BINARY_OP(kCompareSGT, fSigned, >)
382 VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >)
383 VECTOR_BINARY_OP(kCompareFGT, fFloat, >)
384 VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=)
385 VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=)
386 VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=)
387 VECTOR_BINARY_OP(kCompareSLT, fSigned, <)
388 VECTOR_BINARY_OP(kCompareULT, fUnsigned, <)
389 VECTOR_BINARY_OP(kCompareFLT, fFloat, <)
390 VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=)
391 VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=)
392 VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400393
Mike Kleine7007382019-05-21 08:36:32 -0500394 case ByteCodeInstruction::kConditionalBranch: {
395 int target = READ16();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400396 if (POP().fBool) {
Mike Kleine7007382019-05-21 08:36:32 -0500397 ip = code + target;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400398 }
399 break;
Mike Kleine7007382019-05-21 08:36:32 -0500400 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400401
402 case ByteCodeInstruction::kConvertFtoI4: sp[-3].fSigned = (int)sp[-3].fFloat;
403 case ByteCodeInstruction::kConvertFtoI3: sp[-2].fSigned = (int)sp[-2].fFloat;
404 case ByteCodeInstruction::kConvertFtoI2: sp[-1].fSigned = (int)sp[-1].fFloat;
405 case ByteCodeInstruction::kConvertFtoI: sp[ 0].fSigned = (int)sp[ 0].fFloat;
406 break;
407
408 case ByteCodeInstruction::kConvertStoF4: sp[-3].fFloat = sp[-3].fSigned;
409 case ByteCodeInstruction::kConvertStoF3: sp[-2].fFloat = sp[-2].fSigned;
410 case ByteCodeInstruction::kConvertStoF2: sp[-1].fFloat = sp[-1].fSigned;
411 case ByteCodeInstruction::kConvertStoF : sp[ 0].fFloat = sp[ 0].fSigned;
412 break;
413
414 case ByteCodeInstruction::kConvertUtoF4: sp[-3].fFloat = sp[-3].fUnsigned;
415 case ByteCodeInstruction::kConvertUtoF3: sp[-2].fFloat = sp[-2].fUnsigned;
416 case ByteCodeInstruction::kConvertUtoF2: sp[-1].fFloat = sp[-1].fUnsigned;
417 case ByteCodeInstruction::kConvertUtoF : sp[ 0].fFloat = sp[ 0].fUnsigned;
418 break;
419
Mike Klein459aed12019-05-21 15:46:36 -0500420 VECTOR_UNARY_FN(kCos, cosf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400421
Mike Kleine7007382019-05-21 08:36:32 -0500422 case ByteCodeInstruction::kDebugPrint: {
423 Value v = POP();
424 printf("Debug: %d(int), %d(uint), %f(float)\n", v.fSigned, v.fUnsigned, v.fFloat);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400425 break;
Mike Kleine7007382019-05-21 08:36:32 -0500426 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400427
Mike Kleinc1999982019-05-21 13:03:49 -0500428 VECTOR_BINARY_OP(kDivideS, fSigned, /)
429 VECTOR_BINARY_OP(kDivideU, fUnsigned, /)
430 VECTOR_BINARY_OP(kDivideF, fFloat, /)
Mike Kleine7007382019-05-21 08:36:32 -0500431
432 case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
433 case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
434 case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
435 case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
436 break;
437
Brian Osman07c117b2019-05-23 12:51:06 -0700438 case ByteCodeInstruction::kDupN: {
439 int count = READ8();
440 memcpy(sp + 1, sp - count + 1, count * sizeof(Value));
441 sp += count;
442 break;
443 }
444
Mike Kleine7007382019-05-21 08:36:32 -0500445 case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3];
446 case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2];
447 case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1];
448 case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0];
449 ++ip;
450 sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1;
451 break;
452
453 case ByteCodeInstruction::kLoadGlobal4: sp[4] = fGlobals[*ip + 3];
454 case ByteCodeInstruction::kLoadGlobal3: sp[3] = fGlobals[*ip + 2];
455 case ByteCodeInstruction::kLoadGlobal2: sp[2] = fGlobals[*ip + 1];
456 case ByteCodeInstruction::kLoadGlobal : sp[1] = fGlobals[*ip + 0];
457 ++ip;
Brian Osman07c117b2019-05-23 12:51:06 -0700458 sp += (int)inst -
459 (int)ByteCodeInstruction::kLoadGlobal + 1;
Mike Kleine7007382019-05-21 08:36:32 -0500460 break;
461
Brian Osman07c117b2019-05-23 12:51:06 -0700462 case ByteCodeInstruction::kLoadExtended: {
463 int count = READ8();
464 int src = POP().fSigned;
465 memcpy(sp + 1, &stack[src], count * sizeof(Value));
466 sp += count;
467 break;
468 }
469
470 case ByteCodeInstruction::kLoadExtendedGlobal: {
471 int count = READ8();
472 int src = POP().fSigned;
473 SkASSERT(src + count <= (int) fGlobals.size());
474 memcpy(sp + 1, &fGlobals[src], count * sizeof(Value));
475 sp += count;
476 break;
477 }
478
Mike Kleine7007382019-05-21 08:36:32 -0500479 case ByteCodeInstruction::kLoadSwizzle: {
480 int src = READ8();
481 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400482 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400483 PUSH(stack[src + *(ip + i)]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400484 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400485 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400486 break;
Mike Kleine7007382019-05-21 08:36:32 -0500487 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400488
Mike Kleine7007382019-05-21 08:36:32 -0500489 case ByteCodeInstruction::kLoadSwizzleGlobal: {
490 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400491 SkASSERT(src < (int) fGlobals.size());
Mike Kleine7007382019-05-21 08:36:32 -0500492 int count = READ8();
Brian Osmanb7451292019-05-15 13:02:13 -0400493 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400494 PUSH(fGlobals[src + *(ip + i)]);
Brian Osmanb7451292019-05-15 13:02:13 -0400495 }
496 ip += count;
497 break;
Mike Kleine7007382019-05-21 08:36:32 -0500498 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400499
Mike Kleinc1999982019-05-21 13:03:49 -0500500 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
501 VECTOR_BINARY_OP(kMultiplyF, fFloat, *)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400502
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400503 case ByteCodeInstruction::kNot:
Mike Kleine7007382019-05-21 08:36:32 -0500504 sp[0].fBool = !sp[0].fBool;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400505 break;
Mike Kleine7007382019-05-21 08:36:32 -0500506
Mike Kleinc1999982019-05-21 13:03:49 -0500507 case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat;
508 case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat;
509 case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat;
510 case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat;
Mike Kleine7007382019-05-21 08:36:32 -0500511 break;
512
Mike Kleinc1999982019-05-21 13:03:49 -0500513 case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned;
514 case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned;
515 case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned;
516 case ByteCodeInstruction::kNegateI : sp[ 0] = -sp [0].fSigned;
Mike Kleine7007382019-05-21 08:36:32 -0500517 break;
518
519 case ByteCodeInstruction::kPop4: POP();
520 case ByteCodeInstruction::kPop3: POP();
521 case ByteCodeInstruction::kPop2: POP();
522 case ByteCodeInstruction::kPop : POP();
523 break;
524
Brian Osman07c117b2019-05-23 12:51:06 -0700525 case ByteCodeInstruction::kPopN:
526 sp -= READ8();
527 break;
528
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400529 case ByteCodeInstruction::kPushImmediate:
Mike Kleine7007382019-05-21 08:36:32 -0500530 PUSH(READ32());
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400531 break;
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400532
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400533 case ByteCodeInstruction::kReadExternal: // fall through
534 case ByteCodeInstruction::kReadExternal2: // fall through
535 case ByteCodeInstruction::kReadExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500536 case ByteCodeInstruction::kReadExternal4: {
537 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400538 fByteCode->fExternalValues[src]->read(sp + 1);
Mike Kleine7007382019-05-21 08:36:32 -0500539 sp += (int) inst - (int) ByteCodeInstruction::kReadExternal + 1;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400540 break;
Mike Kleine7007382019-05-21 08:36:32 -0500541 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400542
Mike Kleinc1999982019-05-21 13:03:49 -0500543 VECTOR_BINARY_FN(kRemainderF, fFloat, fmodf)
544 VECTOR_BINARY_OP(kRemainderS, fSigned, %)
545 VECTOR_BINARY_OP(kRemainderU, fUnsigned, %)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400546
Mike Kleine7007382019-05-21 08:36:32 -0500547 case ByteCodeInstruction::kReturn: {
548 int count = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400549 if (frames.empty()) {
550 if (outReturn) {
551 memcpy(outReturn, sp - count + 1, count * sizeof(Value));
552 }
553 return;
554 } else {
555 // When we were called, 'stack' was positioned at the old top-of-stack (where
556 // our parameters were placed). So copy our return values to that same spot.
557 memmove(stack, sp - count + 1, count * sizeof(Value));
558
559 // Now move the stack pointer to the end of the just-pushed return values,
560 // and restore everything else.
561 const StackFrame& frame(frames.back());
562 sp = stack + count - 1;
563 stack = frame.fStack;
564 code = frame.fCode;
565 ip = frame.fIP;
566 frames.pop_back();
567 break;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400568 }
Mike Kleine7007382019-05-21 08:36:32 -0500569 }
570
Mike Klein459aed12019-05-21 15:46:36 -0500571 VECTOR_UNARY_FN(kSin, sinf, fFloat)
572 VECTOR_UNARY_FN(kSqrt, sqrtf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400573
Mike Kleine7007382019-05-21 08:36:32 -0500574 case ByteCodeInstruction::kStore4: stack[*ip + 3] = POP();
575 case ByteCodeInstruction::kStore3: stack[*ip + 2] = POP();
576 case ByteCodeInstruction::kStore2: stack[*ip + 1] = POP();
577 case ByteCodeInstruction::kStore : stack[*ip + 0] = POP();
578 ++ip;
579 break;
580
581 case ByteCodeInstruction::kStoreGlobal4: fGlobals[*ip + 3] = POP();
582 case ByteCodeInstruction::kStoreGlobal3: fGlobals[*ip + 2] = POP();
583 case ByteCodeInstruction::kStoreGlobal2: fGlobals[*ip + 1] = POP();
584 case ByteCodeInstruction::kStoreGlobal : fGlobals[*ip + 0] = POP();
585 ++ip;
586 break;
587
Brian Osman07c117b2019-05-23 12:51:06 -0700588 case ByteCodeInstruction::kStoreExtended: {
589 int count = READ8();
590 int target = POP().fSigned;
591 memcpy(&stack[target], sp - count + 1, count * sizeof(Value));
592 sp -= count;
593 break;
594 }
595 case ByteCodeInstruction::kStoreExtendedGlobal: {
596 int count = READ8();
597 int target = POP().fSigned;
598 SkASSERT(target + count <= (int) fGlobals.size());
599 memcpy(&fGlobals[target], sp - count + 1, count * sizeof(Value));
600 sp -= count;
601 break;
602 }
603
Mike Kleine7007382019-05-21 08:36:32 -0500604 case ByteCodeInstruction::kStoreSwizzle: {
605 int target = READ8();
606 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400607 for (int i = count - 1; i >= 0; --i) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400608 stack[target + *(ip + i)] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400609 }
Brian Osman1091f022019-05-16 09:42:16 -0400610 ip += count;
611 break;
Mike Kleine7007382019-05-21 08:36:32 -0500612 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400613
Mike Kleine7007382019-05-21 08:36:32 -0500614 case ByteCodeInstruction::kStoreSwizzleGlobal: {
615 int target = READ8();
616 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400617 for (int i = count - 1; i >= 0; --i) {
618 fGlobals[target + *(ip + i)] = POP();
619 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400620 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400621 break;
Mike Kleine7007382019-05-21 08:36:32 -0500622 }
Brian Osman07c117b2019-05-23 12:51:06 -0700623 case ByteCodeInstruction::kStoreSwizzleIndirect: {
624 int target = POP().fSigned;
625 int count = READ8();
626 for (int i = count - 1; i >= 0; --i) {
627 stack[target + *(ip + i)] = POP();
628 }
629 ip += count;
630 break;
631 }
632 case ByteCodeInstruction::kStoreSwizzleIndirectGlobal: {
633 int target = POP().fSigned;
634 int count = READ8();
635 for (int i = count - 1; i >= 0; --i) {
636 fGlobals[target + *(ip + i)] = POP();
637 }
638 ip += count;
639 break;
640 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400641
Mike Kleinc1999982019-05-21 13:03:49 -0500642 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
643 VECTOR_BINARY_OP(kSubtractF, fFloat, -)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400644
Mike Kleine7007382019-05-21 08:36:32 -0500645 case ByteCodeInstruction::kSwizzle: {
646 Value tmp[4];
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400647 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400648 tmp[i] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400649 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400650 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400651 PUSH(tmp[READ8()]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400652 }
653 break;
Mike Kleine7007382019-05-21 08:36:32 -0500654 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400655
Mike Klein459aed12019-05-21 15:46:36 -0500656 VECTOR_UNARY_FN(kTan, tanf, fFloat)
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400657
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400658 case ByteCodeInstruction::kWriteExternal: // fall through
659 case ByteCodeInstruction::kWriteExternal2: // fall through
660 case ByteCodeInstruction::kWriteExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500661 case ByteCodeInstruction::kWriteExternal4: {
662 int count = (int) inst - (int) ByteCodeInstruction::kWriteExternal + 1;
663 int target = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400664 fByteCode->fExternalValues[target]->write(sp - count + 1);
665 sp -= count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400666 break;
Mike Kleine7007382019-05-21 08:36:32 -0500667 }
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400668
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400669 default:
Mike Kleine7007382019-05-21 08:36:32 -0500670 SkDEBUGFAILF("unsupported instruction %d\n", (int) inst);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400671 }
672#ifdef TRACE
Mike Kleine7007382019-05-21 08:36:32 -0500673 int stackSize = (int) (sp - stack + 1);
674 printf("STACK(%d):", stackSize);
Ethan Nicholas82162ee2019-05-21 16:05:08 -0400675 for (int i = 0; i < stackSize; ++i) {
Brian Osmane85b6a52019-05-22 14:50:59 -0700676 printf(" %d(%g)", stack[i].fSigned, stack[i].fFloat);
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400677 }
678 printf("\n");
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400679#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400680 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400681}
682
683} // namespace
684
685#endif