blob: 234a14334a12796702a1d9173ef1c1420d5102b5 [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"
Ethan Nicholas91164d12019-05-15 15:29:54 -040011#include "src/sksl/SkSLExternalValue.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/sksl/SkSLInterpreter.h"
13#include "src/sksl/ir/SkSLBinaryExpression.h"
14#include "src/sksl/ir/SkSLExpressionStatement.h"
15#include "src/sksl/ir/SkSLForStatement.h"
16#include "src/sksl/ir/SkSLFunctionCall.h"
17#include "src/sksl/ir/SkSLFunctionReference.h"
18#include "src/sksl/ir/SkSLIfStatement.h"
19#include "src/sksl/ir/SkSLIndexExpression.h"
20#include "src/sksl/ir/SkSLPostfixExpression.h"
21#include "src/sksl/ir/SkSLPrefixExpression.h"
22#include "src/sksl/ir/SkSLProgram.h"
23#include "src/sksl/ir/SkSLStatement.h"
24#include "src/sksl/ir/SkSLTernaryExpression.h"
25#include "src/sksl/ir/SkSLVarDeclarations.h"
26#include "src/sksl/ir/SkSLVarDeclarationsStatement.h"
27#include "src/sksl/ir/SkSLVariableReference.h"
Ethan Nicholas26a9aad2018-03-27 14:10:52 -040028
29namespace SkSL {
30
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040031static constexpr int UNINITIALIZED = 0xDEADBEEF;
32
Mike Kleine7007382019-05-21 08:36:32 -050033Interpreter::Interpreter(std::unique_ptr<Program> program,
34 std::unique_ptr<ByteCode> byteCode,
Ethan Nicholasdfcad062019-05-07 12:53:34 -040035 Interpreter::Value inputs[])
36 : fProgram(std::move(program))
Mike Kleine7007382019-05-21 08:36:32 -050037 , fByteCode(std::move(byteCode))
38 , fGlobals(fByteCode->fGlobalCount, UNINITIALIZED) {
Brian Osmand369a5e2019-05-09 13:13:25 -040039 this->setInputs(inputs);
40}
41
42void Interpreter::setInputs(Interpreter::Value inputs[]) {
Mike Kleine7007382019-05-21 08:36:32 -050043 for (uint8_t slot : fByteCode->fInputSlots) {
44 fGlobals[slot] = *inputs++;
Ethan Nicholasdfcad062019-05-07 12:53:34 -040045 }
46}
47
48void Interpreter::run(const ByteCodeFunction& f, Interpreter::Value args[],
49 Interpreter::Value* outReturn) {
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040050#ifdef TRACE
51 this->disassemble(f);
52#endif
Ethan Nicholasdfcad062019-05-07 12:53:34 -040053 Value smallStack[128];
54 std::unique_ptr<Value[]> largeStack;
55 Value* stack = smallStack;
Brian Osman226668a2019-05-14 16:47:30 -040056 if ((int) SK_ARRAY_COUNT(smallStack) < f.fStackCount) {
57 largeStack.reset(new Value[f.fStackCount]);
Ethan Nicholasdfcad062019-05-07 12:53:34 -040058 stack = largeStack.get();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040059 }
Mike Kleine7007382019-05-21 08:36:32 -050060
61 if (f.fParameterCount) {
62 memcpy(stack, args, f.fParameterCount * sizeof(Value));
63 }
64 this->innerRun(f, stack, outReturn);
65
66 for (const Variable* p : f.fDeclaration.fParameters) {
67 const int nvalues = p->fType.columns()
68 * p->fType.rows();
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
Ethan Nicholas48a75aa2019-05-16 17:15:56 -040088#define VECTOR_DISASSEMBLE(op, text) \
89 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
Ethan Nicholas0e9401d2019-03-21 11:05:37 -040094void Interpreter::disassemble(const ByteCodeFunction& f) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -040095 const uint8_t* ip = f.fCode.data();
96 while (ip < f.fCode.data() + f.fCode.size()) {
97 printf("%d: ", (int) (ip - f.fCode.data()));
Mike Klein108e9352019-05-21 11:05:17 -050098 switch ((ByteCodeInstruction) READ16()) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -040099 VECTOR_DISASSEMBLE(kAddF, "addf")
100 VECTOR_DISASSEMBLE(kAddI, "addi")
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400101 case ByteCodeInstruction::kAndB: printf("andb"); break;
102 case ByteCodeInstruction::kAndI: printf("andi"); break;
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400103 case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break;
Brian Osman226668a2019-05-14 16:47:30 -0400104 case ByteCodeInstruction::kCall: printf("call %d", READ8()); break;
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400105 case ByteCodeInstruction::kCallExternal: {
106 int argumentCount = READ8();
107 int returnCount = READ8();
108 int externalValue = READ8();
109 printf("callexternal %d, %d, %d", argumentCount, returnCount, externalValue);
110 break;
111 }
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400112 VECTOR_DISASSEMBLE(kCompareIEQ, "compareieq")
113 VECTOR_DISASSEMBLE(kCompareINEQ, "compareineq")
114 VECTOR_DISASSEMBLE(kCompareFEQ, "comparefeq")
115 VECTOR_DISASSEMBLE(kCompareFNEQ, "comparefneq")
116 VECTOR_DISASSEMBLE(kCompareFGT, "comparefgt")
117 VECTOR_DISASSEMBLE(kCompareFGTEQ, "comparefgteq")
118 VECTOR_DISASSEMBLE(kCompareFLT, "compareflt")
119 VECTOR_DISASSEMBLE(kCompareFLTEQ, "compareflteq")
120 VECTOR_DISASSEMBLE(kCompareSGT, "comparesgt")
121 VECTOR_DISASSEMBLE(kCompareSGTEQ, "comparesgteq")
122 VECTOR_DISASSEMBLE(kCompareSLT, "compareslt")
123 VECTOR_DISASSEMBLE(kCompareSLTEQ, "compareslteq")
124 VECTOR_DISASSEMBLE(kCompareUGT, "compareugt")
125 VECTOR_DISASSEMBLE(kCompareUGTEQ, "compareugteq")
126 VECTOR_DISASSEMBLE(kCompareULT, "compareult")
127 VECTOR_DISASSEMBLE(kCompareULTEQ, "compareulteq")
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400128 case ByteCodeInstruction::kConditionalBranch:
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400129 printf("conditionalbranch %d", READ16());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400130 break;
131 case ByteCodeInstruction::kDebugPrint: printf("debugprint"); break;
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400132 VECTOR_DISASSEMBLE(kDivideF, "dividef")
133 VECTOR_DISASSEMBLE(kDivideS, "divideS")
134 VECTOR_DISASSEMBLE(kDivideU, "divideu")
135 VECTOR_DISASSEMBLE(kDup, "dup")
136 VECTOR_DISASSEMBLE(kFloatToInt, "floattoint")
Brian Osman1091f022019-05-16 09:42:16 -0400137 case ByteCodeInstruction::kLoad: printf("load %d", READ8()); break;
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400138 case ByteCodeInstruction::kLoad2: printf("load2 %d", READ8()); break;
139 case ByteCodeInstruction::kLoad3: printf("load3 %d", READ8()); break;
140 case ByteCodeInstruction::kLoad4: printf("load4 %d", READ8()); break;
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400141 case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break;
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400142 case ByteCodeInstruction::kLoadGlobal2: printf("loadglobal2 %d", READ8()); break;
143 case ByteCodeInstruction::kLoadGlobal3: printf("loadglobal3 %d", READ8()); break;
144 case ByteCodeInstruction::kLoadGlobal4: printf("loadglobal4 %d", READ8()); break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400145 case ByteCodeInstruction::kLoadSwizzle: {
Brian Osman1091f022019-05-16 09:42:16 -0400146 int target = READ8();
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400147 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400148 printf("loadswizzle %d %d", target, count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400149 for (int i = 0; i < count; ++i) {
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400150 printf(", %d", READ8());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400151 }
152 break;
153 }
Brian Osmanb7451292019-05-15 13:02:13 -0400154 case ByteCodeInstruction::kLoadSwizzleGlobal: {
Brian Osman1091f022019-05-16 09:42:16 -0400155 int target = READ8();
Brian Osmanb7451292019-05-15 13:02:13 -0400156 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400157 printf("loadswizzleglobal %d %d", target, count);
Brian Osmanb7451292019-05-15 13:02:13 -0400158 for (int i = 0; i < count; ++i) {
159 printf(", %d", READ8());
160 }
161 break;
162 }
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400163 VECTOR_DISASSEMBLE(kMultiplyF, "multiplyf")
164 VECTOR_DISASSEMBLE(kMultiplyI, "multiplyi")
165 VECTOR_DISASSEMBLE(kNegateF, "negatef")
Mike Klein12710912019-05-21 11:04:59 -0500166 VECTOR_DISASSEMBLE(kNegateI, "negatei")
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400167 VECTOR_DISASSEMBLE(kNot, "not")
168 VECTOR_DISASSEMBLE(kOrB, "orb")
169 VECTOR_DISASSEMBLE(kOrI, "ori")
170 VECTOR_DISASSEMBLE(kPop, "pop")
Mike Kleine7007382019-05-21 08:36:32 -0500171 case ByteCodeInstruction::kPushImmediate: {
172 uint32_t v = READ32();
173 union { uint32_t u; float f; } pun = { v };
174 printf("pushimmediate %s", (to_string(v) + "(" + to_string(pun.f) + ")").c_str());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400175 break;
Mike Kleine7007382019-05-21 08:36:32 -0500176 }
Ethan Nicholas91164d12019-05-15 15:29:54 -0400177 case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break;
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400178 case ByteCodeInstruction::kReadExternal2: printf("readexternal2 %d", READ8()); break;
179 case ByteCodeInstruction::kReadExternal3: printf("readexternal3 %d", READ8()); break;
180 case ByteCodeInstruction::kReadExternal4: printf("readexternal4 %d", READ8()); break;
181 VECTOR_DISASSEMBLE(kRemainderF, "remainderf")
182 VECTOR_DISASSEMBLE(kRemainderS, "remainders")
183 VECTOR_DISASSEMBLE(kRemainderU, "remainderu")
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400184 case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break;
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400185 VECTOR_DISASSEMBLE(kSignedToFloat, "signedtofloat")
Brian Osman1091f022019-05-16 09:42:16 -0400186 case ByteCodeInstruction::kStore: printf("store %d", READ8()); break;
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400187 case ByteCodeInstruction::kStore2: printf("store2 %d", READ8()); break;
188 case ByteCodeInstruction::kStore3: printf("store3 %d", READ8()); break;
189 case ByteCodeInstruction::kStore4: printf("store4 %d", READ8()); break;
Brian Osman1091f022019-05-16 09:42:16 -0400190 case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break;
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400191 case ByteCodeInstruction::kStoreGlobal2: printf("storeglobal2 %d", READ8()); break;
192 case ByteCodeInstruction::kStoreGlobal3: printf("storeglobal3 %d", READ8()); break;
193 case ByteCodeInstruction::kStoreGlobal4: printf("storeglobal4 %d", READ8()); break;
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400194 case ByteCodeInstruction::kStoreSwizzle: {
Brian Osman1091f022019-05-16 09:42:16 -0400195 int target = READ8();
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400196 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400197 printf("storeswizzle %d %d", target, count);
198 for (int i = 0; i < count; ++i) {
199 printf(", %d", READ8());
200 }
201 break;
202 }
203 case ByteCodeInstruction::kStoreSwizzleGlobal: {
204 int target = READ8();
205 int count = READ8();
206 printf("storeswizzleglobal %d %d", target, count);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400207 for (int i = 0; i < count; ++i) {
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400208 printf(", %d", READ8());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400209 }
210 break;
211 }
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400212 VECTOR_DISASSEMBLE(kSubtractF, "subtractf")
213 VECTOR_DISASSEMBLE(kSubtractI, "subtracti")
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400214 case ByteCodeInstruction::kSwizzle: {
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400215 printf("swizzle %d, ", READ8());
216 int count = READ8();
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400217 printf("%d", count);
218 for (int i = 0; i < count; ++i) {
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400219 printf(", %d", READ8());
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400220 }
221 break;
222 }
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400223 VECTOR_DISASSEMBLE(kUnsignedToFloat, "unsignedtofloat")
Ethan Nicholas91164d12019-05-15 15:29:54 -0400224 case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break;
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400225 case ByteCodeInstruction::kWriteExternal2: printf("writeexternal2 %d", READ8()); break;
226 case ByteCodeInstruction::kWriteExternal3: printf("writeexternal3 %d", READ8()); break;
227 case ByteCodeInstruction::kWriteExternal4: printf("writeexternal4 %d", READ8()); break;
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400228 default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false);
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400229 }
230 printf("\n");
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400231 }
232}
233
Mike Kleinc1999982019-05-21 13:03:49 -0500234#define VECTOR_BINARY_OP(base, src, op) \
Mike Kleine7007382019-05-21 08:36:32 -0500235 case ByteCodeInstruction::base ## 4: \
Mike Kleinc1999982019-05-21 13:03:49 -0500236 sp[-4] = sp[-4].src op sp[0].src; \
Mike Kleine7007382019-05-21 08:36:32 -0500237 POP(); \
238 /* fall through */ \
239 case ByteCodeInstruction::base ## 3: { \
240 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Kleinc1999982019-05-21 13:03:49 -0500241 sp[count] = sp[count].src op sp[0].src; \
Mike Kleine7007382019-05-21 08:36:32 -0500242 POP(); \
243 } /* fall through */ \
244 case ByteCodeInstruction::base ## 2: { \
245 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Kleinc1999982019-05-21 13:03:49 -0500246 sp[count] = sp[count].src op sp[0].src; \
Mike Kleine7007382019-05-21 08:36:32 -0500247 POP(); \
248 } /* fall through */ \
249 case ByteCodeInstruction::base: { \
250 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Kleinc1999982019-05-21 13:03:49 -0500251 sp[count] = sp[count].src op sp[0].src; \
Mike Kleine7007382019-05-21 08:36:32 -0500252 POP(); \
253 break; \
254 }
Ethan Nicholasaeb71ce2019-05-20 09:55:44 -0400255
Mike Kleinc1999982019-05-21 13:03:49 -0500256#define VECTOR_BINARY_FN(base, src, fn) \
Mike Kleine7007382019-05-21 08:36:32 -0500257 case ByteCodeInstruction::base ## 4: \
Mike Kleinc1999982019-05-21 13:03:49 -0500258 sp[-4] = fn(sp[-4].src, sp[0].src); \
Mike Kleine7007382019-05-21 08:36:32 -0500259 POP(); \
260 /* fall through */ \
261 case ByteCodeInstruction::base ## 3: { \
262 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Kleinc1999982019-05-21 13:03:49 -0500263 sp[count] = fn(sp[count].src, sp[0].src); \
Mike Kleine7007382019-05-21 08:36:32 -0500264 POP(); \
265 } /* fall through */ \
266 case ByteCodeInstruction::base ## 2: { \
267 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Kleinc1999982019-05-21 13:03:49 -0500268 sp[count] = fn(sp[count].src, sp[0].src); \
Mike Kleine7007382019-05-21 08:36:32 -0500269 POP(); \
270 } /* fall through */ \
271 case ByteCodeInstruction::base: { \
272 int count = (int) ByteCodeInstruction::base - (int) inst - 1; \
Mike Kleinc1999982019-05-21 13:03:49 -0500273 sp[count] = fn(sp[count].src, sp[0].src); \
Mike Kleine7007382019-05-21 08:36:32 -0500274 POP(); \
275 break; \
276 }
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400277
Brian Osman226668a2019-05-14 16:47:30 -0400278struct StackFrame {
279 const uint8_t* fCode;
280 const uint8_t* fIP;
281 Interpreter::Value* fStack;
282};
283
Mike Kleine7007382019-05-21 08:36:32 -0500284void Interpreter::innerRun(const ByteCodeFunction& f, Value* stack, Value* outReturn) {
285 Value* sp = stack + f.fParameterCount + f.fLocalCount - 1;
286
287 auto POP = [&] { SkASSERT(sp >= stack); return *(sp--); };
288 auto PUSH = [&](Value v) { SkASSERT(sp + 1 >= stack); *(++sp) = v; };
289
Brian Osman226668a2019-05-14 16:47:30 -0400290 const uint8_t* code = f.fCode.data();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400291 const uint8_t* ip = code;
Brian Osman226668a2019-05-14 16:47:30 -0400292 std::vector<StackFrame> frames;
293
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400294 for (;;) {
Mike Klein108e9352019-05-21 11:05:17 -0500295 ByteCodeInstruction inst = (ByteCodeInstruction) READ16();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400296#ifdef TRACE
Ethan Nicholas91164d12019-05-15 15:29:54 -0400297 printf("at %d\n", (int) (ip - code - 1));
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400298#endif
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400299 switch (inst) {
Mike Kleinc1999982019-05-21 13:03:49 -0500300 VECTOR_BINARY_OP(kAddI, fSigned, +)
301 VECTOR_BINARY_OP(kAddF, fFloat, +)
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400302 case ByteCodeInstruction::kBranch:
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400303 ip = code + READ16();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400304 break;
Brian Osman226668a2019-05-14 16:47:30 -0400305 case ByteCodeInstruction::kCall: {
306 // Precursor code has pushed all parameters to the stack. Update our bottom of
307 // stack to point at the first parameter, and our sp to point past those parameters
308 // (plus space for locals).
Mike Kleine7007382019-05-21 08:36:32 -0500309 int target = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400310 const ByteCodeFunction* fun = fByteCode->fFunctions[target].get();
311 frames.push_back({ code, ip, stack });
312 ip = code = fun->fCode.data();
313 stack = sp - fun->fParameterCount + 1;
314 sp = stack + fun->fParameterCount + fun->fLocalCount - 1;
315 break;
316 }
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400317 case ByteCodeInstruction::kCallExternal: {
318 int argumentCount = READ8();
319 int returnCount = READ8();
Mike Kleine7007382019-05-21 08:36:32 -0500320 int target = READ8();
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400321 ExternalValue* v = fByteCode->fExternalValues[target];
322 sp -= argumentCount - 1;
Mike Kleine7007382019-05-21 08:36:32 -0500323
324 Value tmp[4];
325 SkASSERT(returnCount <= (int)SK_ARRAY_COUNT(tmp));
Ethan Nicholas9e6a3932019-05-17 16:31:21 -0400326 v->call(sp, tmp);
327 memcpy(sp, tmp, returnCount * sizeof(Value));
328 sp += returnCount - 1;
329 break;
330 }
Mike Kleinc1999982019-05-21 13:03:49 -0500331 VECTOR_BINARY_OP(kCompareIEQ, fSigned, ==)
332 VECTOR_BINARY_OP(kCompareFEQ, fFloat, ==)
333 VECTOR_BINARY_OP(kCompareINEQ, fSigned, !=)
334 VECTOR_BINARY_OP(kCompareFNEQ, fFloat, !=)
335 VECTOR_BINARY_OP(kCompareSGT, fSigned, >)
336 VECTOR_BINARY_OP(kCompareUGT, fUnsigned, >)
337 VECTOR_BINARY_OP(kCompareFGT, fFloat, >)
338 VECTOR_BINARY_OP(kCompareSGTEQ, fSigned, >=)
339 VECTOR_BINARY_OP(kCompareUGTEQ, fUnsigned, >=)
340 VECTOR_BINARY_OP(kCompareFGTEQ, fFloat, >=)
341 VECTOR_BINARY_OP(kCompareSLT, fSigned, <)
342 VECTOR_BINARY_OP(kCompareULT, fUnsigned, <)
343 VECTOR_BINARY_OP(kCompareFLT, fFloat, <)
344 VECTOR_BINARY_OP(kCompareSLTEQ, fSigned, <=)
345 VECTOR_BINARY_OP(kCompareULTEQ, fUnsigned, <=)
346 VECTOR_BINARY_OP(kCompareFLTEQ, fFloat, <=)
Mike Kleine7007382019-05-21 08:36:32 -0500347 case ByteCodeInstruction::kConditionalBranch: {
348 int target = READ16();
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400349 if (POP().fBool) {
Mike Kleine7007382019-05-21 08:36:32 -0500350 ip = code + target;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400351 }
352 break;
Mike Kleine7007382019-05-21 08:36:32 -0500353 }
354 case ByteCodeInstruction::kDebugPrint: {
355 Value v = POP();
356 printf("Debug: %d(int), %d(uint), %f(float)\n", v.fSigned, v.fUnsigned, v.fFloat);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400357 break;
Mike Kleine7007382019-05-21 08:36:32 -0500358 }
Mike Kleinc1999982019-05-21 13:03:49 -0500359 VECTOR_BINARY_OP(kDivideS, fSigned, /)
360 VECTOR_BINARY_OP(kDivideU, fUnsigned, /)
361 VECTOR_BINARY_OP(kDivideF, fFloat, /)
Mike Kleine7007382019-05-21 08:36:32 -0500362
363 case ByteCodeInstruction::kDup4: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
364 case ByteCodeInstruction::kDup3: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
365 case ByteCodeInstruction::kDup2: PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
366 case ByteCodeInstruction::kDup : PUSH(sp[(int)ByteCodeInstruction::kDup - (int)inst]);
367 break;
368
369 case ByteCodeInstruction::kFloatToInt4: sp[-3].fSigned = (int)sp[-3].fFloat;
370 case ByteCodeInstruction::kFloatToInt3: sp[-2].fSigned = (int)sp[-2].fFloat;
371 case ByteCodeInstruction::kFloatToInt2: sp[-1].fSigned = (int)sp[-1].fFloat;
372 case ByteCodeInstruction::kFloatToInt: sp[ 0].fSigned = (int)sp[ 0].fFloat;
373 break;
374
375 case ByteCodeInstruction::kSignedToFloat4: sp[-3].fFloat = sp[-3].fSigned;
376 case ByteCodeInstruction::kSignedToFloat3: sp[-2].fFloat = sp[-2].fSigned;
377 case ByteCodeInstruction::kSignedToFloat2: sp[-1].fFloat = sp[-1].fSigned;
378 case ByteCodeInstruction::kSignedToFloat : sp[ 0].fFloat = sp[ 0].fSigned;
379 break;
380
381 case ByteCodeInstruction::kUnsignedToFloat4: sp[-3].fFloat = sp[-3].fUnsigned;
382 case ByteCodeInstruction::kUnsignedToFloat3: sp[-2].fFloat = sp[-2].fUnsigned;
383 case ByteCodeInstruction::kUnsignedToFloat2: sp[-1].fFloat = sp[-1].fUnsigned;
384 case ByteCodeInstruction::kUnsignedToFloat : sp[ 0].fFloat = sp[ 0].fUnsigned;
385 break;
386
387 case ByteCodeInstruction::kLoad4: sp[4] = stack[*ip + 3];
388 case ByteCodeInstruction::kLoad3: sp[3] = stack[*ip + 2];
389 case ByteCodeInstruction::kLoad2: sp[2] = stack[*ip + 1];
390 case ByteCodeInstruction::kLoad : sp[1] = stack[*ip + 0];
391 ++ip;
392 sp += (int)inst - (int)ByteCodeInstruction::kLoad + 1;
393 break;
394
395 case ByteCodeInstruction::kLoadGlobal4: sp[4] = fGlobals[*ip + 3];
396 case ByteCodeInstruction::kLoadGlobal3: sp[3] = fGlobals[*ip + 2];
397 case ByteCodeInstruction::kLoadGlobal2: sp[2] = fGlobals[*ip + 1];
398 case ByteCodeInstruction::kLoadGlobal : sp[1] = fGlobals[*ip + 0];
399 ++ip;
400 sp += (int)inst - (int)ByteCodeInstruction::kLoadGlobal + 1;
401 break;
402
403 case ByteCodeInstruction::kLoadSwizzle: {
404 int src = READ8();
405 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400406 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400407 PUSH(stack[src + *(ip + i)]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400408 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400409 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400410 break;
Mike Kleine7007382019-05-21 08:36:32 -0500411 }
412 case ByteCodeInstruction::kLoadSwizzleGlobal: {
413 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400414 SkASSERT(src < (int) fGlobals.size());
Mike Kleine7007382019-05-21 08:36:32 -0500415 int count = READ8();
Brian Osmanb7451292019-05-15 13:02:13 -0400416 for (int i = 0; i < count; ++i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400417 PUSH(fGlobals[src + *(ip + i)]);
Brian Osmanb7451292019-05-15 13:02:13 -0400418 }
419 ip += count;
420 break;
Mike Kleine7007382019-05-21 08:36:32 -0500421 }
Mike Kleinc1999982019-05-21 13:03:49 -0500422 VECTOR_BINARY_OP(kMultiplyI, fSigned, *)
423 VECTOR_BINARY_OP(kMultiplyF, fFloat, *)
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400424 case ByteCodeInstruction::kNot:
Mike Kleine7007382019-05-21 08:36:32 -0500425 sp[0].fBool = !sp[0].fBool;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400426 break;
Mike Kleine7007382019-05-21 08:36:32 -0500427
Mike Kleinc1999982019-05-21 13:03:49 -0500428 case ByteCodeInstruction::kNegateF4: sp[-3] = -sp[-3].fFloat;
429 case ByteCodeInstruction::kNegateF3: sp[-2] = -sp[-2].fFloat;
430 case ByteCodeInstruction::kNegateF2: sp[-1] = -sp[-1].fFloat;
431 case ByteCodeInstruction::kNegateF : sp[ 0] = -sp[ 0].fFloat;
Mike Kleine7007382019-05-21 08:36:32 -0500432 break;
433
Mike Kleinc1999982019-05-21 13:03:49 -0500434 case ByteCodeInstruction::kNegateI4: sp[-3] = -sp[-3].fSigned;
435 case ByteCodeInstruction::kNegateI3: sp[-2] = -sp[-2].fSigned;
436 case ByteCodeInstruction::kNegateI2: sp[-1] = -sp[-1].fSigned;
437 case ByteCodeInstruction::kNegateI : sp[ 0] = -sp [0].fSigned;
Mike Kleine7007382019-05-21 08:36:32 -0500438 break;
439
440 case ByteCodeInstruction::kPop4: POP();
441 case ByteCodeInstruction::kPop3: POP();
442 case ByteCodeInstruction::kPop2: POP();
443 case ByteCodeInstruction::kPop : POP();
444 break;
445
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400446 case ByteCodeInstruction::kPushImmediate:
Mike Kleine7007382019-05-21 08:36:32 -0500447 PUSH(READ32());
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400448 break;
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400449 case ByteCodeInstruction::kReadExternal: // fall through
450 case ByteCodeInstruction::kReadExternal2: // fall through
451 case ByteCodeInstruction::kReadExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500452 case ByteCodeInstruction::kReadExternal4: {
453 int src = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400454 fByteCode->fExternalValues[src]->read(sp + 1);
Mike Kleine7007382019-05-21 08:36:32 -0500455 sp += (int) inst - (int) ByteCodeInstruction::kReadExternal + 1;
Ethan Nicholas91164d12019-05-15 15:29:54 -0400456 break;
Mike Kleine7007382019-05-21 08:36:32 -0500457 }
Mike Kleinc1999982019-05-21 13:03:49 -0500458 VECTOR_BINARY_FN(kRemainderF, fFloat, fmodf)
459 VECTOR_BINARY_OP(kRemainderS, fSigned, %)
460 VECTOR_BINARY_OP(kRemainderU, fUnsigned, %)
Mike Kleine7007382019-05-21 08:36:32 -0500461 case ByteCodeInstruction::kReturn: {
462 int count = READ8();
Brian Osman226668a2019-05-14 16:47:30 -0400463 if (frames.empty()) {
464 if (outReturn) {
465 memcpy(outReturn, sp - count + 1, count * sizeof(Value));
466 }
467 return;
468 } else {
469 // When we were called, 'stack' was positioned at the old top-of-stack (where
470 // our parameters were placed). So copy our return values to that same spot.
471 memmove(stack, sp - count + 1, count * sizeof(Value));
472
473 // Now move the stack pointer to the end of the just-pushed return values,
474 // and restore everything else.
475 const StackFrame& frame(frames.back());
476 sp = stack + count - 1;
477 stack = frame.fStack;
478 code = frame.fCode;
479 ip = frame.fIP;
480 frames.pop_back();
481 break;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400482 }
Mike Kleine7007382019-05-21 08:36:32 -0500483 }
484
485 case ByteCodeInstruction::kStore4: stack[*ip + 3] = POP();
486 case ByteCodeInstruction::kStore3: stack[*ip + 2] = POP();
487 case ByteCodeInstruction::kStore2: stack[*ip + 1] = POP();
488 case ByteCodeInstruction::kStore : stack[*ip + 0] = POP();
489 ++ip;
490 break;
491
492 case ByteCodeInstruction::kStoreGlobal4: fGlobals[*ip + 3] = POP();
493 case ByteCodeInstruction::kStoreGlobal3: fGlobals[*ip + 2] = POP();
494 case ByteCodeInstruction::kStoreGlobal2: fGlobals[*ip + 1] = POP();
495 case ByteCodeInstruction::kStoreGlobal : fGlobals[*ip + 0] = POP();
496 ++ip;
497 break;
498
499 case ByteCodeInstruction::kStoreSwizzle: {
500 int target = READ8();
501 int count = READ8();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400502 for (int i = count - 1; i >= 0; --i) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400503 stack[target + *(ip + i)] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400504 }
Brian Osman1091f022019-05-16 09:42:16 -0400505 ip += count;
506 break;
Mike Kleine7007382019-05-21 08:36:32 -0500507 }
508 case ByteCodeInstruction::kStoreSwizzleGlobal: {
509 int target = READ8();
510 int count = READ8();
Brian Osman1091f022019-05-16 09:42:16 -0400511 for (int i = count - 1; i >= 0; --i) {
512 fGlobals[target + *(ip + i)] = POP();
513 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400514 ip += count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400515 break;
Mike Kleine7007382019-05-21 08:36:32 -0500516 }
Mike Kleinc1999982019-05-21 13:03:49 -0500517 VECTOR_BINARY_OP(kSubtractI, fSigned, -)
518 VECTOR_BINARY_OP(kSubtractF, fFloat, -)
Mike Kleine7007382019-05-21 08:36:32 -0500519 case ByteCodeInstruction::kSwizzle: {
520 Value tmp[4];
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400521 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400522 tmp[i] = POP();
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400523 }
Ethan Nicholas7e603db2019-05-03 12:57:47 -0400524 for (int i = READ8() - 1; i >= 0; --i) {
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400525 PUSH(tmp[READ8()]);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400526 }
527 break;
Mike Kleine7007382019-05-21 08:36:32 -0500528 }
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400529 case ByteCodeInstruction::kWriteExternal: // fall through
530 case ByteCodeInstruction::kWriteExternal2: // fall through
531 case ByteCodeInstruction::kWriteExternal3: // fall through
Mike Kleine7007382019-05-21 08:36:32 -0500532 case ByteCodeInstruction::kWriteExternal4: {
533 int count = (int) inst - (int) ByteCodeInstruction::kWriteExternal + 1;
534 int target = READ8();
Ethan Nicholas48a75aa2019-05-16 17:15:56 -0400535 fByteCode->fExternalValues[target]->write(sp - count + 1);
536 sp -= count;
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400537 break;
Mike Kleine7007382019-05-21 08:36:32 -0500538 }
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400539 default:
Mike Kleine7007382019-05-21 08:36:32 -0500540 SkDEBUGFAILF("unsupported instruction %d\n", (int) inst);
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400541 }
542#ifdef TRACE
Mike Kleine7007382019-05-21 08:36:32 -0500543 int stackSize = (int) (sp - stack + 1);
544 printf("STACK(%d):", stackSize);
545 for (int i = 0; i < stackSize(); ++i) {
Ethan Nicholasdfcad062019-05-07 12:53:34 -0400546 printf(" %d(%f)", stack[i].fSigned, stack[i].fFloat);
547 }
548 printf("\n");
Ethan Nicholas9764ebd2019-05-01 14:43:54 -0400549#endif
Ethan Nicholas0e9401d2019-03-21 11:05:37 -0400550 }
Ethan Nicholas26a9aad2018-03-27 14:10:52 -0400551}
552
553} // namespace
554
555#endif