Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkRasterPipeline.h" |
| 11 | #include "src/sksl/SkSLInterpreter.h" |
| 12 | #include "src/sksl/ir/SkSLBinaryExpression.h" |
| 13 | #include "src/sksl/ir/SkSLExpressionStatement.h" |
| 14 | #include "src/sksl/ir/SkSLForStatement.h" |
| 15 | #include "src/sksl/ir/SkSLFunctionCall.h" |
| 16 | #include "src/sksl/ir/SkSLFunctionReference.h" |
| 17 | #include "src/sksl/ir/SkSLIfStatement.h" |
| 18 | #include "src/sksl/ir/SkSLIndexExpression.h" |
| 19 | #include "src/sksl/ir/SkSLPostfixExpression.h" |
| 20 | #include "src/sksl/ir/SkSLPrefixExpression.h" |
| 21 | #include "src/sksl/ir/SkSLProgram.h" |
| 22 | #include "src/sksl/ir/SkSLStatement.h" |
| 23 | #include "src/sksl/ir/SkSLTernaryExpression.h" |
| 24 | #include "src/sksl/ir/SkSLVarDeclarations.h" |
| 25 | #include "src/sksl/ir/SkSLVarDeclarationsStatement.h" |
| 26 | #include "src/sksl/ir/SkSLVariableReference.h" |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 27 | |
| 28 | namespace SkSL { |
| 29 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 30 | static constexpr int UNINITIALIZED = 0xDEADBEEF; |
| 31 | |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 32 | Interpreter::Interpreter(std::unique_ptr<Program> program, std::unique_ptr<ByteCode> byteCode, |
| 33 | Interpreter::Value inputs[]) |
| 34 | : fProgram(std::move(program)) |
| 35 | , fByteCode(std::move(byteCode)) { |
| 36 | for (int i = 0; i < fByteCode->fGlobalCount; ++i) { |
| 37 | fGlobals.push_back(Value((int) UNINITIALIZED)); |
| 38 | } |
Brian Osman | d369a5e | 2019-05-09 13:13:25 -0400 | [diff] [blame] | 39 | this->setInputs(inputs); |
| 40 | } |
| 41 | |
| 42 | void Interpreter::setInputs(Interpreter::Value inputs[]) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 43 | for (int i = fByteCode->fInputSlots.size() - 1; i >= 0; --i) { |
| 44 | fGlobals[fByteCode->fInputSlots[i]] = inputs[i]; |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | void Interpreter::run(const ByteCodeFunction& f, Interpreter::Value args[], |
| 49 | Interpreter::Value* outReturn) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 50 | #ifdef TRACE |
| 51 | this->disassemble(f); |
| 52 | #endif |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 53 | Value smallStack[128]; |
| 54 | std::unique_ptr<Value[]> largeStack; |
| 55 | Value* stack = smallStack; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 56 | if ((int) SK_ARRAY_COUNT(smallStack) < f.fStackCount) { |
| 57 | largeStack.reset(new Value[f.fStackCount]); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 58 | stack = largeStack.get(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 59 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 60 | run(f, stack, args, outReturn); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 61 | int offset = 0; |
| 62 | for (const auto& p : f.fDeclaration.fParameters) { |
| 63 | if (p->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 64 | for (int i = p->fType.columns() * p->fType.rows() - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 65 | args[offset] = stack[offset]; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 66 | ++offset; |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 67 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 68 | } else { |
| 69 | offset += p->fType.columns() * p->fType.rows(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 70 | } |
| 71 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 72 | } |
| 73 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 74 | struct CallbackCtx : public SkRasterPipeline_CallbackCtx { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 75 | Interpreter* fInterpreter; |
| 76 | const FunctionDefinition* fFunction; |
| 77 | }; |
| 78 | |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 79 | #define STACK_SIZE() (int) (sp - stack + 1) |
| 80 | |
| 81 | #define TOP() (*sp) |
| 82 | |
| 83 | #define POP() (*(sp--)) |
| 84 | |
| 85 | #define PUSH(v) (*(++sp) = v) |
| 86 | |
| 87 | #define READ8() (*(ip++)) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 88 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 89 | #define READ16() \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 90 | (SkASSERT((intptr_t) ip % 2 == 0), \ |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 91 | ip += 2, \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 92 | *(uint16_t*) (ip - 2)) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 93 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 94 | #define READ32() \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 95 | (SkASSERT((intptr_t) ip % 4 == 0), \ |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 96 | ip += 4, \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 97 | *(uint32_t*) (ip - 4)) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 98 | |
| 99 | static String value_string(uint32_t v) { |
| 100 | union { uint32_t u; float f; } pun = { v }; |
| 101 | return to_string(v) + "(" + to_string(pun.f) + ")"; |
| 102 | } |
| 103 | |
| 104 | void Interpreter::disassemble(const ByteCodeFunction& f) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 105 | const uint8_t* ip = f.fCode.data(); |
| 106 | while (ip < f.fCode.data() + f.fCode.size()) { |
| 107 | printf("%d: ", (int) (ip - f.fCode.data())); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 108 | switch ((ByteCodeInstruction) READ8()) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 109 | case ByteCodeInstruction::kAddF: printf("addf"); break; |
| 110 | case ByteCodeInstruction::kAddI: printf("addi"); break; |
| 111 | case ByteCodeInstruction::kAndB: printf("andb"); break; |
| 112 | case ByteCodeInstruction::kAndI: printf("andi"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 113 | case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 114 | case ByteCodeInstruction::kCall: printf("call %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 115 | case ByteCodeInstruction::kCompareIEQ: printf("comparei eq"); break; |
| 116 | case ByteCodeInstruction::kCompareINEQ: printf("comparei neq"); break; |
| 117 | case ByteCodeInstruction::kCompareFEQ: printf("comparef eq"); break; |
| 118 | case ByteCodeInstruction::kCompareFGT: printf("comparef gt"); break; |
| 119 | case ByteCodeInstruction::kCompareFGTEQ: printf("comparef gteq"); break; |
| 120 | case ByteCodeInstruction::kCompareFLT: printf("comparef lt"); break; |
| 121 | case ByteCodeInstruction::kCompareFLTEQ: printf("comparef lteq"); break; |
| 122 | case ByteCodeInstruction::kCompareFNEQ: printf("comparef neq"); break; |
| 123 | case ByteCodeInstruction::kCompareSGT: printf("compares sgt"); break; |
| 124 | case ByteCodeInstruction::kCompareSGTEQ: printf("compares sgteq"); break; |
| 125 | case ByteCodeInstruction::kCompareSLT: printf("compares lt"); break; |
| 126 | case ByteCodeInstruction::kCompareSLTEQ: printf("compares lteq"); break; |
| 127 | case ByteCodeInstruction::kCompareUGT: printf("compareu gt"); break; |
| 128 | case ByteCodeInstruction::kCompareUGTEQ: printf("compareu gteq"); break; |
| 129 | case ByteCodeInstruction::kCompareULT: printf("compareu lt"); break; |
| 130 | case ByteCodeInstruction::kCompareULTEQ: printf("compareu lteq"); break; |
| 131 | case ByteCodeInstruction::kConditionalBranch: |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 132 | printf("conditionalbranch %d", READ16()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 133 | break; |
| 134 | case ByteCodeInstruction::kDebugPrint: printf("debugprint"); break; |
| 135 | case ByteCodeInstruction::kDivideF: printf("dividef"); break; |
| 136 | case ByteCodeInstruction::kDivideS: printf("divides"); break; |
| 137 | case ByteCodeInstruction::kDivideU: printf("divideu"); break; |
| 138 | case ByteCodeInstruction::kDup: printf("dup"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 139 | case ByteCodeInstruction::kDupDown: printf("dupdown %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 140 | case ByteCodeInstruction::kFloatToInt: printf("floattoint"); break; |
| 141 | case ByteCodeInstruction::kLoad: printf("load"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 142 | case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 143 | case ByteCodeInstruction::kLoadSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 144 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 145 | printf("loadswizzle %d", count); |
| 146 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 147 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 148 | } |
| 149 | break; |
| 150 | } |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame^] | 151 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 152 | int count = READ8(); |
| 153 | printf("loadswizzleglobal %d", count); |
| 154 | for (int i = 0; i < count; ++i) { |
| 155 | printf(", %d", READ8()); |
| 156 | } |
| 157 | break; |
| 158 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 159 | case ByteCodeInstruction::kMultiplyF: printf("multiplyf"); break; |
| 160 | case ByteCodeInstruction::kMultiplyS: printf("multiplys"); break; |
| 161 | case ByteCodeInstruction::kMultiplyU: printf("multiplyu"); break; |
| 162 | case ByteCodeInstruction::kNegateF: printf("negatef"); break; |
| 163 | case ByteCodeInstruction::kNegateS: printf("negates"); break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 164 | case ByteCodeInstruction::kNop1: printf("nop1"); break; |
| 165 | case ByteCodeInstruction::kNop2: printf("nop2"); break; |
| 166 | case ByteCodeInstruction::kNop3: printf("nop3"); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 167 | case ByteCodeInstruction::kNot: printf("not"); break; |
| 168 | case ByteCodeInstruction::kOrB: printf("orb"); break; |
| 169 | case ByteCodeInstruction::kOrI: printf("ori"); break; |
| 170 | case ByteCodeInstruction::kParameter: printf("parameter"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 171 | case ByteCodeInstruction::kPop: printf("pop %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 172 | case ByteCodeInstruction::kPushImmediate: |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 173 | printf("pushimmediate %s", value_string(READ32()).c_str()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 174 | break; |
Brian Osman | 3b41baf | 2019-05-08 09:24:46 -0400 | [diff] [blame] | 175 | case ByteCodeInstruction::kRemainderF: printf("remainderf"); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 176 | case ByteCodeInstruction::kRemainderS: printf("remainders"); break; |
| 177 | case ByteCodeInstruction::kRemainderU: printf("remainderu"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 178 | case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 179 | case ByteCodeInstruction::kSignedToFloat: printf("signedtofloat"); break; |
| 180 | case ByteCodeInstruction::kStore: printf("store"); break; |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 181 | case ByteCodeInstruction::kStoreGlobal: printf("storeglobal"); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 182 | case ByteCodeInstruction::kStoreSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 183 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 184 | printf("storeswizzle %d", count); |
| 185 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 186 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 187 | } |
| 188 | break; |
| 189 | } |
| 190 | case ByteCodeInstruction::kSubtractF: printf("subtractf"); break; |
| 191 | case ByteCodeInstruction::kSubtractI: printf("subtracti"); break; |
| 192 | case ByteCodeInstruction::kSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 193 | printf("swizzle %d, ", READ8()); |
| 194 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 195 | printf("%d", count); |
| 196 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 197 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 198 | } |
| 199 | break; |
| 200 | } |
| 201 | case ByteCodeInstruction::kUnsignedToFloat: printf("unsignedtofloat"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 202 | case ByteCodeInstruction::kVector: printf("vector%d", READ8()); break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 203 | default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 204 | } |
| 205 | printf("\n"); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 209 | #define BINARY_OP(inst, type, field, op) \ |
| 210 | case ByteCodeInstruction::inst: { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 211 | type b = POP().field; \ |
| 212 | Value* a = &TOP(); \ |
Ethan Nicholas | 9585947 | 2019-04-24 12:55:51 -0400 | [diff] [blame] | 213 | *a = Value(a->field op b); \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 214 | break; \ |
| 215 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 216 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 217 | static constexpr int VECTOR_MAX = 16; |
| 218 | |
| 219 | #define VECTOR_BINARY_OP(inst, type, field, op) \ |
| 220 | case ByteCodeInstruction::inst: { \ |
| 221 | Value result[VECTOR_MAX]; \ |
| 222 | for (int i = count - 1; i >= 0; --i) { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 223 | result[i] = POP(); \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 224 | } \ |
| 225 | for (int i = count - 1; i >= 0; --i) { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 226 | result[i] = POP().field op result[i].field; \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 227 | } \ |
| 228 | for (int i = 0; i < count; ++i) { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 229 | PUSH(result[i]); \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 230 | } \ |
| 231 | break; \ |
| 232 | } |
| 233 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 234 | struct StackFrame { |
| 235 | const uint8_t* fCode; |
| 236 | const uint8_t* fIP; |
| 237 | Interpreter::Value* fStack; |
| 238 | }; |
| 239 | |
| 240 | void Interpreter::run(const ByteCodeFunction& f, Value* stack, Value args[], Value* outReturn) { |
| 241 | const uint8_t* code = f.fCode.data(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 242 | const uint8_t* ip = code; |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 243 | memcpy(stack, args, f.fParameterCount * sizeof(Value)); |
| 244 | Value* sp = stack + f.fParameterCount + f.fLocalCount - 1; |
| 245 | std::vector<StackFrame> frames; |
| 246 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 247 | for (;;) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 248 | ByteCodeInstruction inst = (ByteCodeInstruction) READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 249 | #ifdef TRACE |
| 250 | printf("at %d\n", (int) (ip - fCurrentFunction->fCode.data() - 1)); |
| 251 | #endif |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 252 | switch (inst) { |
| 253 | BINARY_OP(kAddI, int32_t, fSigned, +) |
| 254 | BINARY_OP(kAddF, float, fFloat, +) |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 255 | case ByteCodeInstruction::kBranch: { |
| 256 | ip = code + READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 257 | break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 258 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 259 | case ByteCodeInstruction::kCall: { |
| 260 | // Precursor code has pushed all parameters to the stack. Update our bottom of |
| 261 | // stack to point at the first parameter, and our sp to point past those parameters |
| 262 | // (plus space for locals). |
| 263 | int target = READ8(); |
| 264 | const ByteCodeFunction* fun = fByteCode->fFunctions[target].get(); |
| 265 | frames.push_back({ code, ip, stack }); |
| 266 | ip = code = fun->fCode.data(); |
| 267 | stack = sp - fun->fParameterCount + 1; |
| 268 | sp = stack + fun->fParameterCount + fun->fLocalCount - 1; |
| 269 | break; |
| 270 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 271 | BINARY_OP(kCompareIEQ, int32_t, fSigned, ==) |
| 272 | BINARY_OP(kCompareFEQ, float, fFloat, ==) |
| 273 | BINARY_OP(kCompareINEQ, int32_t, fSigned, !=) |
| 274 | BINARY_OP(kCompareFNEQ, float, fFloat, !=) |
| 275 | BINARY_OP(kCompareSGT, int32_t, fSigned, >) |
| 276 | BINARY_OP(kCompareUGT, uint32_t, fUnsigned, >) |
| 277 | BINARY_OP(kCompareFGT, float, fFloat, >) |
| 278 | BINARY_OP(kCompareSGTEQ, int32_t, fSigned, >=) |
| 279 | BINARY_OP(kCompareUGTEQ, uint32_t, fUnsigned, >=) |
| 280 | BINARY_OP(kCompareFGTEQ, float, fFloat, >=) |
| 281 | BINARY_OP(kCompareSLT, int32_t, fSigned, <) |
| 282 | BINARY_OP(kCompareULT, uint32_t, fUnsigned, <) |
| 283 | BINARY_OP(kCompareFLT, float, fFloat, <) |
| 284 | BINARY_OP(kCompareSLTEQ, int32_t, fSigned, <=) |
| 285 | BINARY_OP(kCompareULTEQ, uint32_t, fUnsigned, <=) |
| 286 | BINARY_OP(kCompareFLTEQ, float, fFloat, <=) |
| 287 | case ByteCodeInstruction::kConditionalBranch: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 288 | int target = READ16(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 289 | if (POP().fBool) { |
| 290 | ip = code + target; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 291 | } |
| 292 | break; |
| 293 | } |
| 294 | case ByteCodeInstruction::kDebugPrint: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 295 | Value v = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 296 | printf("Debug: %d(int), %d(uint), %f(float)\n", v.fSigned, v.fUnsigned, v.fFloat); |
| 297 | break; |
| 298 | } |
| 299 | BINARY_OP(kDivideS, int32_t, fSigned, /) |
| 300 | BINARY_OP(kDivideU, uint32_t, fUnsigned, /) |
| 301 | BINARY_OP(kDivideF, float, fFloat, /) |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 302 | case ByteCodeInstruction::kDup: { |
| 303 | Value& top = TOP(); |
| 304 | PUSH(top); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 305 | break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 306 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 307 | case ByteCodeInstruction::kDupDown: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 308 | int count = READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 309 | // before dupdown 4: X A B C D |
| 310 | // after dupdown 4: A B C D X A B C D |
| 311 | memmove(sp, sp - count, sizeof(Value) * (count + 1)); |
| 312 | sp += count; |
| 313 | memcpy(sp - count * 2, sp - count + 1, sizeof(Value) * count); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | case ByteCodeInstruction::kFloatToInt: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 317 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 318 | top.fSigned = (int) top.fFloat; |
| 319 | break; |
| 320 | } |
| 321 | case ByteCodeInstruction::kSignedToFloat: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 322 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 323 | top.fFloat = (float) top.fSigned; |
| 324 | break; |
| 325 | } |
| 326 | case ByteCodeInstruction::kUnsignedToFloat: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 327 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 328 | top.fFloat = (float) top.fUnsigned; |
| 329 | break; |
| 330 | } |
| 331 | case ByteCodeInstruction::kLoad: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 332 | int target = POP().fSigned; |
| 333 | SkASSERT(target < STACK_SIZE()); |
| 334 | PUSH(stack[target]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | case ByteCodeInstruction::kLoadGlobal: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 338 | int target = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 339 | SkASSERT(target < (int) fGlobals.size()); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 340 | PUSH(fGlobals[target]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | case ByteCodeInstruction::kLoadSwizzle: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 344 | Value target = POP(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 345 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 346 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 347 | PUSH(stack[target.fSigned + *(ip + i)]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 348 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 349 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 350 | break; |
| 351 | } |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame^] | 352 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 353 | int target = READ8(); |
| 354 | SkASSERT(target < (int) fGlobals.size()); |
| 355 | int count = READ8(); |
| 356 | for (int i = 0; i < count; ++i) { |
| 357 | PUSH(fGlobals[target + *(ip + i)]); |
| 358 | } |
| 359 | ip += count; |
| 360 | break; |
| 361 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 362 | BINARY_OP(kMultiplyS, int32_t, fSigned, *) |
| 363 | BINARY_OP(kMultiplyU, uint32_t, fUnsigned, *) |
| 364 | BINARY_OP(kMultiplyF, float, fFloat, *) |
| 365 | case ByteCodeInstruction::kNot: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 366 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 367 | top.fBool = !top.fBool; |
| 368 | break; |
| 369 | } |
| 370 | case ByteCodeInstruction::kNegateF: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 371 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 372 | top.fFloat = -top.fFloat; |
| 373 | break; |
| 374 | } |
| 375 | case ByteCodeInstruction::kNegateS: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 376 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 377 | top.fSigned = -top.fSigned; |
| 378 | break; |
| 379 | } |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 380 | case ByteCodeInstruction::kNop1: |
| 381 | continue; |
| 382 | case ByteCodeInstruction::kNop2: |
| 383 | ++ip; |
| 384 | continue; |
| 385 | case ByteCodeInstruction::kNop3: |
| 386 | ip += 2; |
| 387 | continue; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 388 | case ByteCodeInstruction::kPop: |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 389 | for (int i = READ8(); i > 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 390 | POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 391 | } |
| 392 | break; |
| 393 | case ByteCodeInstruction::kPushImmediate: |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 394 | PUSH(Value((int) READ32())); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 395 | break; |
Brian Osman | 3b41baf | 2019-05-08 09:24:46 -0400 | [diff] [blame] | 396 | case ByteCodeInstruction::kRemainderF: { |
| 397 | float b = POP().fFloat; |
| 398 | Value* a = &TOP(); |
| 399 | *a = Value(fmodf(a->fFloat, b)); |
| 400 | break; |
| 401 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 402 | BINARY_OP(kRemainderS, int32_t, fSigned, %) |
| 403 | BINARY_OP(kRemainderU, uint32_t, fUnsigned, %) |
| 404 | case ByteCodeInstruction::kReturn: { |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 405 | int count = READ8(); |
| 406 | if (frames.empty()) { |
| 407 | if (outReturn) { |
| 408 | memcpy(outReturn, sp - count + 1, count * sizeof(Value)); |
| 409 | } |
| 410 | return; |
| 411 | } else { |
| 412 | // When we were called, 'stack' was positioned at the old top-of-stack (where |
| 413 | // our parameters were placed). So copy our return values to that same spot. |
| 414 | memmove(stack, sp - count + 1, count * sizeof(Value)); |
| 415 | |
| 416 | // Now move the stack pointer to the end of the just-pushed return values, |
| 417 | // and restore everything else. |
| 418 | const StackFrame& frame(frames.back()); |
| 419 | sp = stack + count - 1; |
| 420 | stack = frame.fStack; |
| 421 | code = frame.fCode; |
| 422 | ip = frame.fIP; |
| 423 | frames.pop_back(); |
| 424 | break; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 425 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 426 | } |
| 427 | case ByteCodeInstruction::kStore: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 428 | Value value = POP(); |
| 429 | int target = POP().fSigned; |
| 430 | SkASSERT(target < STACK_SIZE()); |
| 431 | stack[target] = value; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 432 | break; |
| 433 | } |
| 434 | case ByteCodeInstruction::kStoreGlobal: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 435 | Value value = POP(); |
| 436 | int target = POP().fSigned; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 437 | SkASSERT(target < (int) fGlobals.size()); |
| 438 | fGlobals[target] = value; |
| 439 | break; |
| 440 | } |
| 441 | case ByteCodeInstruction::kStoreSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 442 | int count = READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 443 | int target = (sp - count)->fSigned; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 444 | for (int i = count - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 445 | stack[target + *(ip + i)] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 446 | } |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 447 | POP(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 448 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 449 | break; |
| 450 | } |
| 451 | BINARY_OP(kSubtractI, int32_t, fSigned, -) |
| 452 | BINARY_OP(kSubtractF, float, fFloat, -) |
| 453 | case ByteCodeInstruction::kSwizzle: { |
| 454 | Value vec[4]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 455 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 456 | vec[i] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 457 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 458 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 459 | PUSH(vec[READ8()]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 460 | } |
| 461 | break; |
| 462 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 463 | case ByteCodeInstruction::kVector: { |
| 464 | uint8_t count = READ8(); |
| 465 | ByteCodeInstruction inst = (ByteCodeInstruction) READ8(); |
| 466 | switch (inst) { |
| 467 | VECTOR_BINARY_OP(kAddI, int32_t, fSigned, +) |
| 468 | VECTOR_BINARY_OP(kAddF, float, fFloat, +) |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 469 | VECTOR_BINARY_OP(kCompareIEQ, int32_t, fSigned, ==) |
| 470 | VECTOR_BINARY_OP(kCompareFEQ, float, fFloat, ==) |
| 471 | VECTOR_BINARY_OP(kCompareINEQ, int32_t, fSigned, !=) |
| 472 | VECTOR_BINARY_OP(kCompareFNEQ, float, fFloat, !=) |
| 473 | VECTOR_BINARY_OP(kCompareSGT, int32_t, fSigned, >) |
| 474 | VECTOR_BINARY_OP(kCompareUGT, uint32_t, fUnsigned, >) |
| 475 | VECTOR_BINARY_OP(kCompareFGT, float, fFloat, >) |
| 476 | VECTOR_BINARY_OP(kCompareSGTEQ, int32_t, fSigned, >=) |
| 477 | VECTOR_BINARY_OP(kCompareUGTEQ, uint32_t, fUnsigned, >=) |
| 478 | VECTOR_BINARY_OP(kCompareFGTEQ, float, fFloat, >=) |
| 479 | VECTOR_BINARY_OP(kCompareSLT, int32_t, fSigned, <) |
| 480 | VECTOR_BINARY_OP(kCompareULT, uint32_t, fUnsigned, <) |
| 481 | VECTOR_BINARY_OP(kCompareFLT, float, fFloat, <) |
| 482 | VECTOR_BINARY_OP(kCompareSLTEQ, int32_t, fSigned, <=) |
| 483 | VECTOR_BINARY_OP(kCompareULTEQ, uint32_t, fUnsigned, <=) |
| 484 | VECTOR_BINARY_OP(kCompareFLTEQ, float, fFloat, <=) |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 485 | VECTOR_BINARY_OP(kDivideS, int32_t, fSigned, /) |
| 486 | VECTOR_BINARY_OP(kDivideU, uint32_t, fUnsigned, /) |
| 487 | VECTOR_BINARY_OP(kDivideF, float, fFloat, /) |
| 488 | case ByteCodeInstruction::kFloatToInt: { |
| 489 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 490 | Value& v = sp[-i]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 491 | v.fSigned = (int) v.fFloat; |
| 492 | } |
| 493 | break; |
| 494 | } |
| 495 | case ByteCodeInstruction::kSignedToFloat: { |
| 496 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 497 | Value& v = sp[-i]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 498 | v.fFloat = (float) v.fSigned; |
| 499 | } |
| 500 | break; |
| 501 | } |
| 502 | case ByteCodeInstruction::kUnsignedToFloat: { |
| 503 | for (int i = 0; i < count; ++i) { |
Brian Osman | 4167215 | 2019-05-14 13:37:30 -0400 | [diff] [blame] | 504 | Value& v = sp[-i]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 505 | v.fFloat = (float) v.fUnsigned; |
| 506 | } |
| 507 | break; |
| 508 | } |
| 509 | case ByteCodeInstruction::kLoad: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 510 | int src = POP().fSigned; |
| 511 | memcpy(sp + 1, &stack[src], count * sizeof(Value)); |
| 512 | sp += count; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 513 | break; |
| 514 | } |
| 515 | case ByteCodeInstruction::kLoadGlobal: { |
| 516 | int target = READ8(); |
| 517 | SkASSERT(target < (int) fGlobals.size()); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame^] | 518 | memcpy(sp + 1, &fGlobals[target], count * sizeof(Value)); |
| 519 | sp += count; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 520 | break; |
| 521 | } |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 522 | case ByteCodeInstruction::kNegateS: { |
| 523 | for (int i = 0; i < count; ++i) { |
| 524 | Value& v = sp[-i]; |
| 525 | v.fSigned = -v.fSigned; |
| 526 | } |
| 527 | break; |
| 528 | } |
| 529 | case ByteCodeInstruction::kNegateF: { |
| 530 | for (int i = 0; i < count; ++i) { |
| 531 | Value& v = sp[-i]; |
| 532 | v.fFloat = -v.fFloat; |
| 533 | } |
| 534 | break; |
| 535 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 536 | VECTOR_BINARY_OP(kMultiplyS, int32_t, fSigned, *) |
| 537 | VECTOR_BINARY_OP(kMultiplyU, uint32_t, fUnsigned, *) |
| 538 | VECTOR_BINARY_OP(kMultiplyF, float, fFloat, *) |
Brian Osman | 3b41baf | 2019-05-08 09:24:46 -0400 | [diff] [blame] | 539 | case ByteCodeInstruction::kRemainderF: { |
| 540 | Value result[VECTOR_MAX]; |
| 541 | for (int i = count - 1; i >= 0; --i) { |
| 542 | result[i] = POP(); |
| 543 | } |
| 544 | for (int i = count - 1; i >= 0; --i) { |
| 545 | result[i] = fmodf(POP().fFloat, result[i].fFloat); |
| 546 | } |
| 547 | for (int i = 0; i < count; ++i) { |
| 548 | PUSH(result[i]); |
| 549 | } |
| 550 | break; |
| 551 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 552 | VECTOR_BINARY_OP(kRemainderS, int32_t, fSigned, %) |
| 553 | VECTOR_BINARY_OP(kRemainderU, uint32_t, fUnsigned, %) |
| 554 | case ByteCodeInstruction::kStore: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 555 | memcpy(&stack[(sp - count)->fSigned], sp - count + 1, |
| 556 | count * sizeof(Value)); |
| 557 | sp -= count; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 558 | break; |
| 559 | } |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame^] | 560 | case ByteCodeInstruction::kStoreGlobal: { |
| 561 | int target = (sp - count)->fSigned; |
| 562 | SkASSERT(target < (int)fGlobals.size()); |
| 563 | memcpy(&fGlobals[target], sp - count + 1, count * sizeof(Value)); |
| 564 | sp -= count; |
| 565 | break; |
| 566 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 567 | VECTOR_BINARY_OP(kSubtractI, int32_t, fSigned, -) |
| 568 | VECTOR_BINARY_OP(kSubtractF, float, fFloat, -) |
| 569 | default: |
| 570 | printf("unsupported instruction %d\n", (int) inst); |
| 571 | SkASSERT(false); |
| 572 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 573 | break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 574 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 575 | default: |
| 576 | printf("unsupported instruction %d\n", (int) inst); |
| 577 | SkASSERT(false); |
| 578 | } |
| 579 | #ifdef TRACE |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 580 | printf("STACK:"); |
| 581 | for (int i = 0; i < STACK_SIZE(); ++i) { |
| 582 | printf(" %d(%f)", stack[i].fSigned, stack[i].fFloat); |
| 583 | } |
| 584 | printf("\n"); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 585 | #endif |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 586 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | } // namespace |
| 590 | |
| 591 | #endif |