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 | } |
| 39 | for (int i = fByteCode->fInputSlots.size() - 1; i >= 0; --i) { |
| 40 | fGlobals[fByteCode->fInputSlots[i]] = inputs[i]; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | void Interpreter::run(const ByteCodeFunction& f, Interpreter::Value args[], |
| 45 | Interpreter::Value* outReturn) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 46 | fCurrentFunction = &f; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 47 | #ifdef TRACE |
| 48 | this->disassemble(f); |
| 49 | #endif |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 50 | Value smallStack[128]; |
| 51 | std::unique_ptr<Value[]> largeStack; |
| 52 | Value* stack = smallStack; |
| 53 | if ((int) SK_ARRAY_COUNT(smallStack) < fCurrentFunction->fStackCount) { |
| 54 | largeStack.reset(new Value[fCurrentFunction->fStackCount]); |
| 55 | stack = largeStack.get(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 56 | } |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 57 | run(stack, args, outReturn); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 58 | int offset = 0; |
| 59 | for (const auto& p : f.fDeclaration.fParameters) { |
| 60 | if (p->fModifiers.fFlags & Modifiers::kOut_Flag) { |
| 61 | 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] | 62 | args[offset] = stack[offset]; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 63 | ++offset; |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 64 | } |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 65 | } else { |
| 66 | offset += p->fType.columns() * p->fType.rows(); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 67 | } |
| 68 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Mike Klein | b11ab57 | 2018-10-24 06:42:14 -0400 | [diff] [blame] | 71 | struct CallbackCtx : public SkRasterPipeline_CallbackCtx { |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 72 | Interpreter* fInterpreter; |
| 73 | const FunctionDefinition* fFunction; |
| 74 | }; |
| 75 | |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 76 | #define STACK_SIZE() (int) (sp - stack + 1) |
| 77 | |
| 78 | #define TOP() (*sp) |
| 79 | |
| 80 | #define POP() (*(sp--)) |
| 81 | |
| 82 | #define PUSH(v) (*(++sp) = v) |
| 83 | |
| 84 | #define READ8() (*(ip++)) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 85 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 86 | #define READ16() \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 87 | (SkASSERT((intptr_t) ip % 2 == 0), \ |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 88 | ip += 2, \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 89 | *(uint16_t*) (ip - 2)) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 90 | |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 91 | #define READ32() \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 92 | (SkASSERT((intptr_t) ip % 4 == 0), \ |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 93 | ip += 4, \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 94 | *(uint32_t*) (ip - 4)) |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 95 | |
| 96 | static String value_string(uint32_t v) { |
| 97 | union { uint32_t u; float f; } pun = { v }; |
| 98 | return to_string(v) + "(" + to_string(pun.f) + ")"; |
| 99 | } |
| 100 | |
| 101 | void Interpreter::disassemble(const ByteCodeFunction& f) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 102 | const uint8_t* ip = f.fCode.data(); |
| 103 | while (ip < f.fCode.data() + f.fCode.size()) { |
| 104 | printf("%d: ", (int) (ip - f.fCode.data())); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 105 | switch ((ByteCodeInstruction) READ8()) { |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 106 | case ByteCodeInstruction::kAddF: printf("addf"); break; |
| 107 | case ByteCodeInstruction::kAddI: printf("addi"); break; |
| 108 | case ByteCodeInstruction::kAndB: printf("andb"); break; |
| 109 | case ByteCodeInstruction::kAndI: printf("andi"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 110 | case ByteCodeInstruction::kBranch: printf("branch %d", READ16()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 111 | case ByteCodeInstruction::kCompareIEQ: printf("comparei eq"); break; |
| 112 | case ByteCodeInstruction::kCompareINEQ: printf("comparei neq"); break; |
| 113 | case ByteCodeInstruction::kCompareFEQ: printf("comparef eq"); break; |
| 114 | case ByteCodeInstruction::kCompareFGT: printf("comparef gt"); break; |
| 115 | case ByteCodeInstruction::kCompareFGTEQ: printf("comparef gteq"); break; |
| 116 | case ByteCodeInstruction::kCompareFLT: printf("comparef lt"); break; |
| 117 | case ByteCodeInstruction::kCompareFLTEQ: printf("comparef lteq"); break; |
| 118 | case ByteCodeInstruction::kCompareFNEQ: printf("comparef neq"); break; |
| 119 | case ByteCodeInstruction::kCompareSGT: printf("compares sgt"); break; |
| 120 | case ByteCodeInstruction::kCompareSGTEQ: printf("compares sgteq"); break; |
| 121 | case ByteCodeInstruction::kCompareSLT: printf("compares lt"); break; |
| 122 | case ByteCodeInstruction::kCompareSLTEQ: printf("compares lteq"); break; |
| 123 | case ByteCodeInstruction::kCompareUGT: printf("compareu gt"); break; |
| 124 | case ByteCodeInstruction::kCompareUGTEQ: printf("compareu gteq"); break; |
| 125 | case ByteCodeInstruction::kCompareULT: printf("compareu lt"); break; |
| 126 | case ByteCodeInstruction::kCompareULTEQ: printf("compareu lteq"); break; |
| 127 | case ByteCodeInstruction::kConditionalBranch: |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 128 | printf("conditionalbranch %d", READ16()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 129 | break; |
| 130 | case ByteCodeInstruction::kDebugPrint: printf("debugprint"); break; |
| 131 | case ByteCodeInstruction::kDivideF: printf("dividef"); break; |
| 132 | case ByteCodeInstruction::kDivideS: printf("divides"); break; |
| 133 | case ByteCodeInstruction::kDivideU: printf("divideu"); break; |
| 134 | case ByteCodeInstruction::kDup: printf("dup"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 135 | case ByteCodeInstruction::kDupDown: printf("dupdown %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 136 | case ByteCodeInstruction::kFloatToInt: printf("floattoint"); break; |
| 137 | case ByteCodeInstruction::kLoad: printf("load"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 138 | case ByteCodeInstruction::kLoadGlobal: printf("loadglobal %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 139 | case ByteCodeInstruction::kLoadSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 140 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 141 | printf("loadswizzle %d", count); |
| 142 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 143 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 144 | } |
| 145 | break; |
| 146 | } |
| 147 | case ByteCodeInstruction::kMultiplyF: printf("multiplyf"); break; |
| 148 | case ByteCodeInstruction::kMultiplyS: printf("multiplys"); break; |
| 149 | case ByteCodeInstruction::kMultiplyU: printf("multiplyu"); break; |
| 150 | case ByteCodeInstruction::kNegateF: printf("negatef"); break; |
| 151 | case ByteCodeInstruction::kNegateS: printf("negates"); break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 152 | case ByteCodeInstruction::kNop1: printf("nop1"); break; |
| 153 | case ByteCodeInstruction::kNop2: printf("nop2"); break; |
| 154 | case ByteCodeInstruction::kNop3: printf("nop3"); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 155 | case ByteCodeInstruction::kNot: printf("not"); break; |
| 156 | case ByteCodeInstruction::kOrB: printf("orb"); break; |
| 157 | case ByteCodeInstruction::kOrI: printf("ori"); break; |
| 158 | case ByteCodeInstruction::kParameter: printf("parameter"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 159 | case ByteCodeInstruction::kPop: printf("pop %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 160 | case ByteCodeInstruction::kPushImmediate: |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 161 | printf("pushimmediate %s", value_string(READ32()).c_str()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 162 | break; |
| 163 | case ByteCodeInstruction::kRemainderS: printf("remainders"); break; |
| 164 | case ByteCodeInstruction::kRemainderU: printf("remainderu"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 165 | case ByteCodeInstruction::kReturn: printf("return %d", READ8()); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 166 | case ByteCodeInstruction::kSignedToFloat: printf("signedtofloat"); break; |
| 167 | case ByteCodeInstruction::kStore: printf("store"); break; |
Ethan Nicholas | 746035a | 2019-04-23 13:31:09 -0400 | [diff] [blame] | 168 | case ByteCodeInstruction::kStoreGlobal: printf("storeglobal"); break; |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 169 | case ByteCodeInstruction::kStoreSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 170 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 171 | printf("storeswizzle %d", count); |
| 172 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 173 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 174 | } |
| 175 | break; |
| 176 | } |
| 177 | case ByteCodeInstruction::kSubtractF: printf("subtractf"); break; |
| 178 | case ByteCodeInstruction::kSubtractI: printf("subtracti"); break; |
| 179 | case ByteCodeInstruction::kSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 180 | printf("swizzle %d, ", READ8()); |
| 181 | int count = READ8(); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 182 | printf("%d", count); |
| 183 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 184 | printf(", %d", READ8()); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 185 | } |
| 186 | break; |
| 187 | } |
| 188 | case ByteCodeInstruction::kUnsignedToFloat: printf("unsignedtofloat"); break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 189 | case ByteCodeInstruction::kVector: printf("vector%d", READ8()); break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 190 | default: printf("unknown(%d)\n", *(ip - 1)); SkASSERT(false); |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 191 | } |
| 192 | printf("\n"); |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 196 | #define BINARY_OP(inst, type, field, op) \ |
| 197 | case ByteCodeInstruction::inst: { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 198 | type b = POP().field; \ |
| 199 | Value* a = &TOP(); \ |
Ethan Nicholas | 9585947 | 2019-04-24 12:55:51 -0400 | [diff] [blame] | 200 | *a = Value(a->field op b); \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 201 | break; \ |
| 202 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 203 | |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 204 | static constexpr int VECTOR_MAX = 16; |
| 205 | |
| 206 | #define VECTOR_BINARY_OP(inst, type, field, op) \ |
| 207 | case ByteCodeInstruction::inst: { \ |
| 208 | Value result[VECTOR_MAX]; \ |
| 209 | for (int i = count - 1; i >= 0; --i) { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 210 | result[i] = POP(); \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 211 | } \ |
| 212 | for (int i = count - 1; i >= 0; --i) { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 213 | result[i] = POP().field op result[i].field; \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 214 | } \ |
| 215 | for (int i = 0; i < count; ++i) { \ |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 216 | PUSH(result[i]); \ |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 217 | } \ |
| 218 | break; \ |
| 219 | } |
| 220 | |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 221 | void Interpreter::run(Value* stack, Value args[], Value* outReturn) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 222 | const uint8_t* code = fCurrentFunction->fCode.data(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 223 | const uint8_t* ip = code; |
| 224 | memcpy(stack, args, fCurrentFunction->fParameterCount * sizeof(Value)); |
| 225 | Value* sp = stack + fCurrentFunction->fParameterCount + fCurrentFunction->fLocalCount - 1; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 226 | for (;;) { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 227 | ByteCodeInstruction inst = (ByteCodeInstruction) READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 228 | #ifdef TRACE |
| 229 | printf("at %d\n", (int) (ip - fCurrentFunction->fCode.data() - 1)); |
| 230 | #endif |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 231 | switch (inst) { |
| 232 | BINARY_OP(kAddI, int32_t, fSigned, +) |
| 233 | BINARY_OP(kAddF, float, fFloat, +) |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 234 | case ByteCodeInstruction::kBranch: { |
| 235 | ip = code + READ16(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 236 | break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 237 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 238 | BINARY_OP(kCompareIEQ, int32_t, fSigned, ==) |
| 239 | BINARY_OP(kCompareFEQ, float, fFloat, ==) |
| 240 | BINARY_OP(kCompareINEQ, int32_t, fSigned, !=) |
| 241 | BINARY_OP(kCompareFNEQ, float, fFloat, !=) |
| 242 | BINARY_OP(kCompareSGT, int32_t, fSigned, >) |
| 243 | BINARY_OP(kCompareUGT, uint32_t, fUnsigned, >) |
| 244 | BINARY_OP(kCompareFGT, float, fFloat, >) |
| 245 | BINARY_OP(kCompareSGTEQ, int32_t, fSigned, >=) |
| 246 | BINARY_OP(kCompareUGTEQ, uint32_t, fUnsigned, >=) |
| 247 | BINARY_OP(kCompareFGTEQ, float, fFloat, >=) |
| 248 | BINARY_OP(kCompareSLT, int32_t, fSigned, <) |
| 249 | BINARY_OP(kCompareULT, uint32_t, fUnsigned, <) |
| 250 | BINARY_OP(kCompareFLT, float, fFloat, <) |
| 251 | BINARY_OP(kCompareSLTEQ, int32_t, fSigned, <=) |
| 252 | BINARY_OP(kCompareULTEQ, uint32_t, fUnsigned, <=) |
| 253 | BINARY_OP(kCompareFLTEQ, float, fFloat, <=) |
| 254 | case ByteCodeInstruction::kConditionalBranch: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 255 | int target = READ16(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 256 | if (POP().fBool) { |
| 257 | ip = code + target; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 258 | } |
| 259 | break; |
| 260 | } |
| 261 | case ByteCodeInstruction::kDebugPrint: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 262 | Value v = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 263 | printf("Debug: %d(int), %d(uint), %f(float)\n", v.fSigned, v.fUnsigned, v.fFloat); |
| 264 | break; |
| 265 | } |
| 266 | BINARY_OP(kDivideS, int32_t, fSigned, /) |
| 267 | BINARY_OP(kDivideU, uint32_t, fUnsigned, /) |
| 268 | BINARY_OP(kDivideF, float, fFloat, /) |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 269 | case ByteCodeInstruction::kDup: { |
| 270 | Value& top = TOP(); |
| 271 | PUSH(top); |
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 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 274 | case ByteCodeInstruction::kDupDown: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 275 | int count = READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 276 | // before dupdown 4: X A B C D |
| 277 | // after dupdown 4: A B C D X A B C D |
| 278 | memmove(sp, sp - count, sizeof(Value) * (count + 1)); |
| 279 | sp += count; |
| 280 | memcpy(sp - count * 2, sp - count + 1, sizeof(Value) * count); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 281 | break; |
| 282 | } |
| 283 | case ByteCodeInstruction::kFloatToInt: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 284 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 285 | top.fSigned = (int) top.fFloat; |
| 286 | break; |
| 287 | } |
| 288 | case ByteCodeInstruction::kSignedToFloat: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 289 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 290 | top.fFloat = (float) top.fSigned; |
| 291 | break; |
| 292 | } |
| 293 | case ByteCodeInstruction::kUnsignedToFloat: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 294 | Value& top = TOP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 295 | top.fFloat = (float) top.fUnsigned; |
| 296 | break; |
| 297 | } |
| 298 | case ByteCodeInstruction::kLoad: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 299 | int target = POP().fSigned; |
| 300 | SkASSERT(target < STACK_SIZE()); |
| 301 | PUSH(stack[target]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 302 | break; |
| 303 | } |
| 304 | case ByteCodeInstruction::kLoadGlobal: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 305 | int target = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 306 | SkASSERT(target < (int) fGlobals.size()); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 307 | PUSH(fGlobals[target]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 308 | break; |
| 309 | } |
| 310 | case ByteCodeInstruction::kLoadSwizzle: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 311 | Value target = POP(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 312 | int count = READ8(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 313 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 314 | PUSH(stack[target.fSigned + *(ip + i)]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 315 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 316 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | BINARY_OP(kMultiplyS, int32_t, fSigned, *) |
| 320 | BINARY_OP(kMultiplyU, uint32_t, fUnsigned, *) |
| 321 | BINARY_OP(kMultiplyF, float, fFloat, *) |
| 322 | case ByteCodeInstruction::kNot: { |
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.fBool = !top.fBool; |
| 325 | break; |
| 326 | } |
| 327 | case ByteCodeInstruction::kNegateF: { |
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 = -top.fFloat; |
| 330 | break; |
| 331 | } |
| 332 | case ByteCodeInstruction::kNegateS: { |
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.fSigned = -top.fSigned; |
| 335 | break; |
| 336 | } |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 337 | case ByteCodeInstruction::kNop1: |
| 338 | continue; |
| 339 | case ByteCodeInstruction::kNop2: |
| 340 | ++ip; |
| 341 | continue; |
| 342 | case ByteCodeInstruction::kNop3: |
| 343 | ip += 2; |
| 344 | continue; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 345 | case ByteCodeInstruction::kPop: |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 346 | for (int i = READ8(); i > 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 347 | POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 348 | } |
| 349 | break; |
| 350 | case ByteCodeInstruction::kPushImmediate: |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 351 | PUSH(Value((int) READ32())); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 352 | break; |
| 353 | BINARY_OP(kRemainderS, int32_t, fSigned, %) |
| 354 | BINARY_OP(kRemainderU, uint32_t, fUnsigned, %) |
| 355 | case ByteCodeInstruction::kReturn: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 356 | if (outReturn) { |
| 357 | int count = READ8(); |
| 358 | memcpy(outReturn, sp - count + 1, count * sizeof(Value)); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 359 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 360 | return; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 361 | } |
| 362 | case ByteCodeInstruction::kStore: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 363 | Value value = POP(); |
| 364 | int target = POP().fSigned; |
| 365 | SkASSERT(target < STACK_SIZE()); |
| 366 | stack[target] = value; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 367 | break; |
| 368 | } |
| 369 | case ByteCodeInstruction::kStoreGlobal: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 370 | Value value = POP(); |
| 371 | int target = POP().fSigned; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 372 | SkASSERT(target < (int) fGlobals.size()); |
| 373 | fGlobals[target] = value; |
| 374 | break; |
| 375 | } |
| 376 | case ByteCodeInstruction::kStoreSwizzle: { |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 377 | int count = READ8(); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 378 | int target = (sp - count)->fSigned; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 379 | for (int i = count - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 380 | stack[target + *(ip + i)] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 381 | } |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 382 | POP(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 383 | ip += count; |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 384 | break; |
| 385 | } |
| 386 | BINARY_OP(kSubtractI, int32_t, fSigned, -) |
| 387 | BINARY_OP(kSubtractF, float, fFloat, -) |
| 388 | case ByteCodeInstruction::kSwizzle: { |
| 389 | Value vec[4]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 390 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 391 | vec[i] = POP(); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 392 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 393 | for (int i = READ8() - 1; i >= 0; --i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 394 | PUSH(vec[READ8()]); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 395 | } |
| 396 | break; |
| 397 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 398 | case ByteCodeInstruction::kVector: { |
| 399 | uint8_t count = READ8(); |
| 400 | ByteCodeInstruction inst = (ByteCodeInstruction) READ8(); |
| 401 | switch (inst) { |
| 402 | VECTOR_BINARY_OP(kAddI, int32_t, fSigned, +) |
| 403 | VECTOR_BINARY_OP(kAddF, float, fFloat, +) |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 404 | case ByteCodeInstruction::kBranch: { |
| 405 | ip = code + READ16(); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 406 | break; |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 407 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 408 | VECTOR_BINARY_OP(kCompareIEQ, int32_t, fSigned, ==) |
| 409 | VECTOR_BINARY_OP(kCompareFEQ, float, fFloat, ==) |
| 410 | VECTOR_BINARY_OP(kCompareINEQ, int32_t, fSigned, !=) |
| 411 | VECTOR_BINARY_OP(kCompareFNEQ, float, fFloat, !=) |
| 412 | VECTOR_BINARY_OP(kCompareSGT, int32_t, fSigned, >) |
| 413 | VECTOR_BINARY_OP(kCompareUGT, uint32_t, fUnsigned, >) |
| 414 | VECTOR_BINARY_OP(kCompareFGT, float, fFloat, >) |
| 415 | VECTOR_BINARY_OP(kCompareSGTEQ, int32_t, fSigned, >=) |
| 416 | VECTOR_BINARY_OP(kCompareUGTEQ, uint32_t, fUnsigned, >=) |
| 417 | VECTOR_BINARY_OP(kCompareFGTEQ, float, fFloat, >=) |
| 418 | VECTOR_BINARY_OP(kCompareSLT, int32_t, fSigned, <) |
| 419 | VECTOR_BINARY_OP(kCompareULT, uint32_t, fUnsigned, <) |
| 420 | VECTOR_BINARY_OP(kCompareFLT, float, fFloat, <) |
| 421 | VECTOR_BINARY_OP(kCompareSLTEQ, int32_t, fSigned, <=) |
| 422 | VECTOR_BINARY_OP(kCompareULTEQ, uint32_t, fUnsigned, <=) |
| 423 | VECTOR_BINARY_OP(kCompareFLTEQ, float, fFloat, <=) |
| 424 | case ByteCodeInstruction::kConditionalBranch: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 425 | uint16_t target = READ16(); |
| 426 | if (POP().fBool) { |
| 427 | ip = code + target; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 428 | } |
| 429 | break; |
| 430 | } |
| 431 | VECTOR_BINARY_OP(kDivideS, int32_t, fSigned, /) |
| 432 | VECTOR_BINARY_OP(kDivideU, uint32_t, fUnsigned, /) |
| 433 | VECTOR_BINARY_OP(kDivideF, float, fFloat, /) |
| 434 | case ByteCodeInstruction::kFloatToInt: { |
| 435 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 436 | Value& v = sp[-i]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 437 | v.fSigned = (int) v.fFloat; |
| 438 | } |
| 439 | break; |
| 440 | } |
| 441 | case ByteCodeInstruction::kSignedToFloat: { |
| 442 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 443 | Value& v = sp[-i]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 444 | v.fFloat = (float) v.fSigned; |
| 445 | } |
| 446 | break; |
| 447 | } |
| 448 | case ByteCodeInstruction::kUnsignedToFloat: { |
| 449 | for (int i = 0; i < count; ++i) { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 450 | Value& v = stack[-i]; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 451 | v.fFloat = (float) v.fUnsigned; |
| 452 | } |
| 453 | break; |
| 454 | } |
| 455 | case ByteCodeInstruction::kLoad: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 456 | int src = POP().fSigned; |
| 457 | memcpy(sp + 1, &stack[src], count * sizeof(Value)); |
| 458 | sp += count; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 459 | break; |
| 460 | } |
| 461 | case ByteCodeInstruction::kLoadGlobal: { |
| 462 | int target = READ8(); |
| 463 | SkASSERT(target < (int) fGlobals.size()); |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 464 | PUSH(fGlobals[target]); |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 465 | break; |
| 466 | } |
Ethan Nicholas | 354ecf3 | 2019-05-07 16:13:02 -0400 | [diff] [blame^] | 467 | case ByteCodeInstruction::kNegateS: { |
| 468 | for (int i = 0; i < count; ++i) { |
| 469 | Value& v = sp[-i]; |
| 470 | v.fSigned = -v.fSigned; |
| 471 | } |
| 472 | break; |
| 473 | } |
| 474 | case ByteCodeInstruction::kNegateF: { |
| 475 | for (int i = 0; i < count; ++i) { |
| 476 | Value& v = sp[-i]; |
| 477 | v.fFloat = -v.fFloat; |
| 478 | } |
| 479 | break; |
| 480 | } |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 481 | VECTOR_BINARY_OP(kMultiplyS, int32_t, fSigned, *) |
| 482 | VECTOR_BINARY_OP(kMultiplyU, uint32_t, fUnsigned, *) |
| 483 | VECTOR_BINARY_OP(kMultiplyF, float, fFloat, *) |
| 484 | VECTOR_BINARY_OP(kRemainderS, int32_t, fSigned, %) |
| 485 | VECTOR_BINARY_OP(kRemainderU, uint32_t, fUnsigned, %) |
| 486 | case ByteCodeInstruction::kStore: { |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 487 | memcpy(&stack[(sp - count)->fSigned], sp - count + 1, |
| 488 | count * sizeof(Value)); |
| 489 | sp -= count; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | VECTOR_BINARY_OP(kSubtractI, int32_t, fSigned, -) |
| 493 | VECTOR_BINARY_OP(kSubtractF, float, fFloat, -) |
| 494 | default: |
| 495 | printf("unsupported instruction %d\n", (int) inst); |
| 496 | SkASSERT(false); |
| 497 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 498 | break; |
Ethan Nicholas | 7e603db | 2019-05-03 12:57:47 -0400 | [diff] [blame] | 499 | } |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 500 | default: |
| 501 | printf("unsupported instruction %d\n", (int) inst); |
| 502 | SkASSERT(false); |
| 503 | } |
| 504 | #ifdef TRACE |
Ethan Nicholas | dfcad06 | 2019-05-07 12:53:34 -0400 | [diff] [blame] | 505 | printf("STACK:"); |
| 506 | for (int i = 0; i < STACK_SIZE(); ++i) { |
| 507 | printf(" %d(%f)", stack[i].fSigned, stack[i].fFloat); |
| 508 | } |
| 509 | printf("\n"); |
Ethan Nicholas | 9764ebd | 2019-05-01 14:43:54 -0400 | [diff] [blame] | 510 | #endif |
Ethan Nicholas | 0e9401d | 2019-03-21 11:05:37 -0400 | [diff] [blame] | 511 | } |
Ethan Nicholas | 26a9aad | 2018-03-27 14:10:52 -0400 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | } // namespace |
| 515 | |
| 516 | #endif |