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