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 | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 140 | case ByteCodeInstruction::kFloatToInt: printf("floattoint"); break; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 141 | case ByteCodeInstruction::kLoad: printf("load %d", READ8()); 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: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 144 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 145 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 146 | printf("loadswizzle %d %d", target, count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 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: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 153 | int target = READ8(); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 154 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 155 | printf("loadswizzleglobal %d %d", target, count); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 156 | for (int i = 0; i < count; ++i) { |
| 157 | printf(", %d", READ8()); |
| 158 | } |
| 159 | break; |
| 160 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 161 | case ByteCodeInstruction::kMultiplyF: printf("multiplyf"); break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 162 | case ByteCodeInstruction::kMultiplyI: printf("multiplyi"); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 163 | case ByteCodeInstruction::kNegateF: printf("negatef"); break; |
| 164 | case ByteCodeInstruction::kNegateS: printf("negates"); break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 165 | case ByteCodeInstruction::kNop1: printf("nop1"); break; |
| 166 | case ByteCodeInstruction::kNop2: printf("nop2"); break; |
| 167 | case ByteCodeInstruction::kNop3: printf("nop3"); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 168 | case ByteCodeInstruction::kNot: printf("not"); break; |
| 169 | case ByteCodeInstruction::kOrB: printf("orb"); break; |
| 170 | case ByteCodeInstruction::kOrI: printf("ori"); break; |
| 171 | case ByteCodeInstruction::kParameter: printf("parameter"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 172 | case ByteCodeInstruction::kPop: printf("pop %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 173 | case ByteCodeInstruction::kPushImmediate: |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 174 | printf("pushimmediate %s", value_string(READ32()).c_str()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 175 | break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 176 | case ByteCodeInstruction::kReadExternal: printf("readexternal %d", READ8()); break; |
Brian Osman | 3b41baf | 2019-05-08 09:24:46 -0400 | [diff] [blame] | 177 | case ByteCodeInstruction::kRemainderF: printf("remainderf"); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 178 | case ByteCodeInstruction::kRemainderS: printf("remainders"); break; |
| 179 | case ByteCodeInstruction::kRemainderU: printf("remainderu"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 180 | case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 181 | case ByteCodeInstruction::kSignedToFloat: printf("signedtofloat"); break; |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 182 | case ByteCodeInstruction::kStore: printf("store %d", READ8()); break; |
| 183 | case ByteCodeInstruction::kStoreGlobal: printf("storeglobal %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 184 | case ByteCodeInstruction::kStoreSwizzle: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 185 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 186 | int count = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 187 | printf("storeswizzle %d %d", target, count); |
| 188 | for (int i = 0; i < count; ++i) { |
| 189 | printf(", %d", READ8()); |
| 190 | } |
| 191 | break; |
| 192 | } |
| 193 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 194 | int target = READ8(); |
| 195 | int count = READ8(); |
| 196 | printf("storeswizzleglobal %d %d", target, count); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 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::kSubtractF: printf("subtractf"); break; |
| 203 | case ByteCodeInstruction::kSubtractI: printf("subtracti"); break; |
| 204 | case ByteCodeInstruction::kSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 205 | printf("swizzle %d, ", READ8()); |
| 206 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 207 | printf("%d", count); |
| 208 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 209 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 210 | } |
| 211 | break; |
| 212 | } |
| 213 | case ByteCodeInstruction::kUnsignedToFloat: printf("unsignedtofloat"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 214 | case ByteCodeInstruction::kVector: printf("vector%d", READ8()); break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 215 | case ByteCodeInstruction::kWriteExternal: printf("writeexternal %d", READ8()); break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 216 | default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 217 | } |
| 218 | printf("\n"); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 219 | } |
| 220 | } |
| 221 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 222 | #define BINARY_OP(inst, type, field, op) \ |
| 223 | case ByteCodeInstruction::inst: { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 224 | type b = POP().field; \ |
| 225 | Value* a = &TOP(); \ |
Ethan Nicholas | 9585947 | 2019-04-24 12:55:51 -0400 | [diff] [blame] | 226 | *a = Value(a->field op b); \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 227 | break; \ |
| 228 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 229 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 230 | static constexpr int VECTOR_MAX = 16; |
| 231 | |
| 232 | #define VECTOR_BINARY_OP(inst, type, field, op) \ |
| 233 | case ByteCodeInstruction::inst: { \ |
| 234 | Value result[VECTOR_MAX]; \ |
| 235 | for (int i = count - 1; i >= 0; --i) { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 236 | result[i] = POP(); \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 237 | } \ |
| 238 | for (int i = count - 1; i >= 0; --i) { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 239 | result[i] = POP().field op result[i].field; \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 240 | } \ |
| 241 | for (int i = 0; i < count; ++i) { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 242 | PUSH(result[i]); \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 243 | } \ |
| 244 | break; \ |
| 245 | } |
| 246 | |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 247 | struct StackFrame { |
| 248 | const uint8_t* fCode; |
| 249 | const uint8_t* fIP; |
| 250 | Interpreter::Value* fStack; |
| 251 | }; |
| 252 | |
| 253 | void Interpreter::run(const ByteCodeFunction& f, Value* stack, Value args[], Value* outReturn) { |
| 254 | const uint8_t* code = f.fCode.data(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 255 | const uint8_t* ip = code; |
Ethan Nicholas | f9ef1b8 | 2019-05-15 16:50:20 -0400 | [diff] [blame] | 256 | if (f.fParameterCount) { |
| 257 | memcpy(stack, args, f.fParameterCount * sizeof(Value)); |
| 258 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 259 | Value* sp = stack + f.fParameterCount + f.fLocalCount - 1; |
| 260 | std::vector<StackFrame> frames; |
| 261 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 262 | for (;;) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 263 | ByteCodeInstruction inst = (ByteCodeInstruction) READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 264 | #ifdef TRACE |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 265 | printf("at %d\n", (int) (ip - code - 1)); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 266 | #endif |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 267 | switch (inst) { |
| 268 | BINARY_OP(kAddI, int32_t, fSigned, +) |
| 269 | BINARY_OP(kAddF, float, fFloat, +) |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 270 | case ByteCodeInstruction::kBranch: { |
| 271 | ip = code + READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 272 | break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 273 | } |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 274 | case ByteCodeInstruction::kCall: { |
| 275 | // Precursor code has pushed all parameters to the stack. Update our bottom of |
| 276 | // stack to point at the first parameter, and our sp to point past those parameters |
| 277 | // (plus space for locals). |
| 278 | int target = READ8(); |
| 279 | const ByteCodeFunction* fun = fByteCode->fFunctions[target].get(); |
| 280 | frames.push_back({ code, ip, stack }); |
| 281 | ip = code = fun->fCode.data(); |
| 282 | stack = sp - fun->fParameterCount + 1; |
| 283 | sp = stack + fun->fParameterCount + fun->fLocalCount - 1; |
| 284 | break; |
| 285 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 286 | BINARY_OP(kCompareIEQ, int32_t, fSigned, ==) |
| 287 | BINARY_OP(kCompareFEQ, float, fFloat, ==) |
| 288 | BINARY_OP(kCompareINEQ, int32_t, fSigned, !=) |
| 289 | BINARY_OP(kCompareFNEQ, float, fFloat, !=) |
| 290 | BINARY_OP(kCompareSGT, int32_t, fSigned, >) |
| 291 | BINARY_OP(kCompareUGT, uint32_t, fUnsigned, >) |
| 292 | BINARY_OP(kCompareFGT, float, fFloat, >) |
| 293 | BINARY_OP(kCompareSGTEQ, int32_t, fSigned, >=) |
| 294 | BINARY_OP(kCompareUGTEQ, uint32_t, fUnsigned, >=) |
| 295 | BINARY_OP(kCompareFGTEQ, float, fFloat, >=) |
| 296 | BINARY_OP(kCompareSLT, int32_t, fSigned, <) |
| 297 | BINARY_OP(kCompareULT, uint32_t, fUnsigned, <) |
| 298 | BINARY_OP(kCompareFLT, float, fFloat, <) |
| 299 | BINARY_OP(kCompareSLTEQ, int32_t, fSigned, <=) |
| 300 | BINARY_OP(kCompareULTEQ, uint32_t, fUnsigned, <=) |
| 301 | BINARY_OP(kCompareFLTEQ, float, fFloat, <=) |
| 302 | case ByteCodeInstruction::kConditionalBranch: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 303 | int target = READ16(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 304 | if (POP().fBool) { |
| 305 | ip = code + target; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 306 | } |
| 307 | break; |
| 308 | } |
| 309 | case ByteCodeInstruction::kDebugPrint: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 310 | Value v = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 311 | printf("Debug: %d(int), %d(uint), %f(float)\n", v.fSigned, v.fUnsigned, v.fFloat); |
| 312 | break; |
| 313 | } |
| 314 | BINARY_OP(kDivideS, int32_t, fSigned, /) |
| 315 | BINARY_OP(kDivideU, uint32_t, fUnsigned, /) |
| 316 | BINARY_OP(kDivideF, float, fFloat, /) |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 317 | case ByteCodeInstruction::kDup: { |
| 318 | Value& top = TOP(); |
| 319 | PUSH(top); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 320 | break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 321 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 322 | case ByteCodeInstruction::kFloatToInt: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 323 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 324 | top.fSigned = (int) top.fFloat; |
| 325 | break; |
| 326 | } |
| 327 | case ByteCodeInstruction::kSignedToFloat: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 328 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 329 | top.fFloat = (float) top.fSigned; |
| 330 | break; |
| 331 | } |
| 332 | case ByteCodeInstruction::kUnsignedToFloat: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 333 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 334 | top.fFloat = (float) top.fUnsigned; |
| 335 | break; |
| 336 | } |
| 337 | case ByteCodeInstruction::kLoad: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 338 | int target = READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 339 | SkASSERT(target < STACK_SIZE()); |
| 340 | PUSH(stack[target]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 341 | break; |
| 342 | } |
| 343 | case ByteCodeInstruction::kLoadGlobal: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 344 | int target = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 345 | SkASSERT(target < (int) fGlobals.size()); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 346 | PUSH(fGlobals[target]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 347 | break; |
| 348 | } |
| 349 | case ByteCodeInstruction::kLoadSwizzle: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 350 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 351 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 352 | for (int i = 0; i < count; ++i) { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 353 | PUSH(stack[target + *(ip + i)]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 354 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 355 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 356 | break; |
| 357 | } |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 358 | case ByteCodeInstruction::kLoadSwizzleGlobal: { |
| 359 | int target = READ8(); |
| 360 | SkASSERT(target < (int) fGlobals.size()); |
| 361 | int count = READ8(); |
| 362 | for (int i = 0; i < count; ++i) { |
| 363 | PUSH(fGlobals[target + *(ip + i)]); |
| 364 | } |
| 365 | ip += count; |
| 366 | break; |
| 367 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 368 | BINARY_OP(kMultiplyI, int32_t, fSigned, *) |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 369 | BINARY_OP(kMultiplyF, float, fFloat, *) |
| 370 | case ByteCodeInstruction::kNot: { |
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.fBool = !top.fBool; |
| 373 | break; |
| 374 | } |
| 375 | case ByteCodeInstruction::kNegateF: { |
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.fFloat = -top.fFloat; |
| 378 | break; |
| 379 | } |
| 380 | case ByteCodeInstruction::kNegateS: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 381 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 382 | top.fSigned = -top.fSigned; |
| 383 | break; |
| 384 | } |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 385 | case ByteCodeInstruction::kNop1: |
| 386 | continue; |
| 387 | case ByteCodeInstruction::kNop2: |
| 388 | ++ip; |
| 389 | continue; |
| 390 | case ByteCodeInstruction::kNop3: |
| 391 | ip += 2; |
| 392 | continue; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 393 | case ByteCodeInstruction::kPop: |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 394 | for (int i = READ8(); i > 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 395 | POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 396 | } |
| 397 | break; |
| 398 | case ByteCodeInstruction::kPushImmediate: |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 399 | PUSH(Value((int) READ32())); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 400 | break; |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 401 | case ByteCodeInstruction::kReadExternal: { |
| 402 | int target = READ8(); |
| 403 | ++sp; |
| 404 | fByteCode->fExternalValues[target]->read(sp); |
| 405 | break; |
| 406 | } |
Brian Osman | 3b41baf | 2019-05-08 09:24:46 -0400 | [diff] [blame] | 407 | case ByteCodeInstruction::kRemainderF: { |
| 408 | float b = POP().fFloat; |
| 409 | Value* a = &TOP(); |
| 410 | *a = Value(fmodf(a->fFloat, b)); |
| 411 | break; |
| 412 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 413 | BINARY_OP(kRemainderS, int32_t, fSigned, %) |
| 414 | BINARY_OP(kRemainderU, uint32_t, fUnsigned, %) |
| 415 | case ByteCodeInstruction::kReturn: { |
Brian Osman | 226668a | 2019-05-14 16:47:30 -0400 | [diff] [blame] | 416 | int count = READ8(); |
| 417 | if (frames.empty()) { |
| 418 | if (outReturn) { |
| 419 | memcpy(outReturn, sp - count + 1, count * sizeof(Value)); |
| 420 | } |
| 421 | return; |
| 422 | } else { |
| 423 | // When we were called, 'stack' was positioned at the old top-of-stack (where |
| 424 | // our parameters were placed). So copy our return values to that same spot. |
| 425 | memmove(stack, sp - count + 1, count * sizeof(Value)); |
| 426 | |
| 427 | // Now move the stack pointer to the end of the just-pushed return values, |
| 428 | // and restore everything else. |
| 429 | const StackFrame& frame(frames.back()); |
| 430 | sp = stack + count - 1; |
| 431 | stack = frame.fStack; |
| 432 | code = frame.fCode; |
| 433 | ip = frame.fIP; |
| 434 | frames.pop_back(); |
| 435 | break; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 436 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 437 | } |
| 438 | case ByteCodeInstruction::kStore: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 439 | Value value = POP(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 440 | int target = READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 441 | SkASSERT(target < STACK_SIZE()); |
| 442 | stack[target] = value; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 443 | break; |
| 444 | } |
| 445 | case ByteCodeInstruction::kStoreGlobal: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 446 | Value value = POP(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 447 | int target = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 448 | SkASSERT(target < (int) fGlobals.size()); |
| 449 | fGlobals[target] = value; |
| 450 | break; |
| 451 | } |
| 452 | case ByteCodeInstruction::kStoreSwizzle: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 453 | int target = READ8(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 454 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 455 | for (int i = count - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 456 | stack[target + *(ip + i)] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 457 | } |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 458 | ip += count; |
| 459 | break; |
| 460 | } |
| 461 | case ByteCodeInstruction::kStoreSwizzleGlobal: { |
| 462 | int target = READ8(); |
| 463 | int count = READ8(); |
| 464 | for (int i = count - 1; i >= 0; --i) { |
| 465 | fGlobals[target + *(ip + i)] = POP(); |
| 466 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 467 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | BINARY_OP(kSubtractI, int32_t, fSigned, -) |
| 471 | BINARY_OP(kSubtractF, float, fFloat, -) |
| 472 | case ByteCodeInstruction::kSwizzle: { |
| 473 | Value vec[4]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 474 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 475 | vec[i] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 476 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 477 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 478 | PUSH(vec[READ8()]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 479 | } |
| 480 | break; |
| 481 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 482 | case ByteCodeInstruction::kVector: { |
| 483 | uint8_t count = READ8(); |
| 484 | ByteCodeInstruction inst = (ByteCodeInstruction) READ8(); |
| 485 | switch (inst) { |
| 486 | VECTOR_BINARY_OP(kAddI, int32_t, fSigned, +) |
| 487 | VECTOR_BINARY_OP(kAddF, float, fFloat, +) |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 488 | VECTOR_BINARY_OP(kCompareIEQ, int32_t, fSigned, ==) |
| 489 | VECTOR_BINARY_OP(kCompareFEQ, float, fFloat, ==) |
| 490 | VECTOR_BINARY_OP(kCompareINEQ, int32_t, fSigned, !=) |
| 491 | VECTOR_BINARY_OP(kCompareFNEQ, float, fFloat, !=) |
| 492 | VECTOR_BINARY_OP(kCompareSGT, int32_t, fSigned, >) |
| 493 | VECTOR_BINARY_OP(kCompareUGT, uint32_t, fUnsigned, >) |
| 494 | VECTOR_BINARY_OP(kCompareFGT, float, fFloat, >) |
| 495 | VECTOR_BINARY_OP(kCompareSGTEQ, int32_t, fSigned, >=) |
| 496 | VECTOR_BINARY_OP(kCompareUGTEQ, uint32_t, fUnsigned, >=) |
| 497 | VECTOR_BINARY_OP(kCompareFGTEQ, float, fFloat, >=) |
| 498 | VECTOR_BINARY_OP(kCompareSLT, int32_t, fSigned, <) |
| 499 | VECTOR_BINARY_OP(kCompareULT, uint32_t, fUnsigned, <) |
| 500 | VECTOR_BINARY_OP(kCompareFLT, float, fFloat, <) |
| 501 | VECTOR_BINARY_OP(kCompareSLTEQ, int32_t, fSigned, <=) |
| 502 | VECTOR_BINARY_OP(kCompareULTEQ, uint32_t, fUnsigned, <=) |
| 503 | VECTOR_BINARY_OP(kCompareFLTEQ, float, fFloat, <=) |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 504 | VECTOR_BINARY_OP(kDivideS, int32_t, fSigned, /) |
| 505 | VECTOR_BINARY_OP(kDivideU, uint32_t, fUnsigned, /) |
| 506 | VECTOR_BINARY_OP(kDivideF, float, fFloat, /) |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 507 | case ByteCodeInstruction::kDup: { |
| 508 | SkASSERT(sp - count + 1 >= stack); |
| 509 | memcpy(sp + 1, sp - count + 1, count * sizeof(Value)); |
| 510 | sp += count; |
| 511 | break; |
| 512 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 513 | case ByteCodeInstruction::kFloatToInt: { |
| 514 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 515 | Value& v = sp[-i]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 516 | v.fSigned = (int) v.fFloat; |
| 517 | } |
| 518 | break; |
| 519 | } |
| 520 | case ByteCodeInstruction::kSignedToFloat: { |
| 521 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 522 | Value& v = sp[-i]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 523 | v.fFloat = (float) v.fSigned; |
| 524 | } |
| 525 | break; |
| 526 | } |
| 527 | case ByteCodeInstruction::kUnsignedToFloat: { |
| 528 | for (int i = 0; i < count; ++i) { |
Brian Osman | 4167215 | 2019-05-14 13:37:30 -0400 | [diff] [blame] | 529 | Value& v = sp[-i]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 530 | v.fFloat = (float) v.fUnsigned; |
| 531 | } |
| 532 | break; |
| 533 | } |
| 534 | case ByteCodeInstruction::kLoad: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 535 | int target = READ8(); |
| 536 | SkASSERT(target + count <= STACK_SIZE()); |
| 537 | memcpy(sp + 1, &stack[target], count * sizeof(Value)); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 538 | sp += count; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 539 | break; |
| 540 | } |
| 541 | case ByteCodeInstruction::kLoadGlobal: { |
| 542 | int target = READ8(); |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 543 | SkASSERT(target + count <= (int) fGlobals.size()); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 544 | memcpy(sp + 1, &fGlobals[target], count * sizeof(Value)); |
| 545 | sp += count; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 546 | break; |
| 547 | } |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame] | 548 | case ByteCodeInstruction::kNegateS: { |
| 549 | for (int i = 0; i < count; ++i) { |
| 550 | Value& v = sp[-i]; |
| 551 | v.fSigned = -v.fSigned; |
| 552 | } |
| 553 | break; |
| 554 | } |
| 555 | case ByteCodeInstruction::kNegateF: { |
| 556 | for (int i = 0; i < count; ++i) { |
| 557 | Value& v = sp[-i]; |
| 558 | v.fFloat = -v.fFloat; |
| 559 | } |
| 560 | break; |
| 561 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 562 | VECTOR_BINARY_OP(kMultiplyI, int32_t, fSigned, *) |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 563 | VECTOR_BINARY_OP(kMultiplyF, float, fFloat, *) |
Brian Osman | 3b41baf | 2019-05-08 09:24:46 -0400 | [diff] [blame] | 564 | case ByteCodeInstruction::kRemainderF: { |
| 565 | Value result[VECTOR_MAX]; |
| 566 | for (int i = count - 1; i >= 0; --i) { |
| 567 | result[i] = POP(); |
| 568 | } |
| 569 | for (int i = count - 1; i >= 0; --i) { |
| 570 | result[i] = fmodf(POP().fFloat, result[i].fFloat); |
| 571 | } |
| 572 | for (int i = 0; i < count; ++i) { |
| 573 | PUSH(result[i]); |
| 574 | } |
| 575 | break; |
| 576 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 577 | case ByteCodeInstruction::kReadExternal: { |
| 578 | int target = READ8(); |
| 579 | // We read all the values in a single read() call, because the external |
| 580 | // value should know how many bytes to transfer. The vector op prefix just |
| 581 | // tells the interpreter how many stack slots to allocate. |
| 582 | fByteCode->fExternalValues[target]->read(sp + 1); |
| 583 | sp += count; |
| 584 | break; |
| 585 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 586 | VECTOR_BINARY_OP(kRemainderS, int32_t, fSigned, %) |
| 587 | VECTOR_BINARY_OP(kRemainderU, uint32_t, fUnsigned, %) |
| 588 | case ByteCodeInstruction::kStore: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 589 | int target = READ8(); |
| 590 | SkASSERT(target + count <= STACK_SIZE()); |
| 591 | memcpy(&stack[target], sp - count + 1, count * sizeof(Value)); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 592 | sp -= count; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 593 | break; |
| 594 | } |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 595 | case ByteCodeInstruction::kStoreGlobal: { |
Brian Osman | 1091f02 | 2019-05-16 09:42:16 -0400 | [diff] [blame^] | 596 | int target = READ8(); |
| 597 | SkASSERT(target + count <= (int)fGlobals.size()); |
Brian Osman | b745129 | 2019-05-15 13:02:13 -0400 | [diff] [blame] | 598 | memcpy(&fGlobals[target], sp - count + 1, count * sizeof(Value)); |
| 599 | sp -= count; |
| 600 | break; |
| 601 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 602 | VECTOR_BINARY_OP(kSubtractI, int32_t, fSigned, -) |
| 603 | VECTOR_BINARY_OP(kSubtractF, float, fFloat, -) |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 604 | case ByteCodeInstruction::kWriteExternal: { |
| 605 | int target = READ8(); |
| 606 | // We write all the values in a single write() call, because the external |
| 607 | // value should know how many bytes to transfer. The vector op prefix just |
| 608 | // tells the interpreter how many stack slots to deallocate. |
| 609 | fByteCode->fExternalValues[target]->write(sp - count + 1); |
| 610 | sp -= count; |
| 611 | break; |
| 612 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 613 | default: |
| 614 | printf("unsupported instruction %d\n", (int) inst); |
| 615 | SkASSERT(false); |
| 616 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 617 | break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 618 | } |
Ethan Nicholas | 91164d1 | 2019-05-15 15:29:54 -0400 | [diff] [blame] | 619 | case ByteCodeInstruction::kWriteExternal: { |
| 620 | int target = READ8(); |
| 621 | fByteCode->fExternalValues[target]->write(sp); |
| 622 | POP(); |
| 623 | break; |
| 624 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 625 | default: |
| 626 | printf("unsupported instruction %d\n", (int) inst); |
| 627 | SkASSERT(false); |
| 628 | } |
| 629 | #ifdef TRACE |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 630 | printf("STACK:"); |
| 631 | for (int i = 0; i < STACK_SIZE(); ++i) { |
| 632 | printf(" %d(%f)", stack[i].fSigned, stack[i].fFloat); |
| 633 | } |
| 634 | printf("\n"); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 635 | #endif |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 636 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | } // namespace |
| 640 | |
| 641 | #endif |