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