| 1 | // SPDX-License-Identifier: MPL-2.0 |
| 2 | // Copyright (c) Yuxuan Shui <[email protected]> |
| 3 | |
| 4 | #pragma once |
| 5 | |
| 6 | #include <uthash.h> |
| 7 | |
| 8 | #include "curve.h" |
| 9 | |
| 10 | #define OPERATORS \ |
| 11 | X(OP_ADD) \ |
| 12 | X(OP_SUB) \ |
| 13 | X(OP_MUL) \ |
| 14 | X(OP_DIV) \ |
| 15 | /* Exponent */ \ |
| 16 | X(OP_EXP) \ |
| 17 | /* Negation */ \ |
| 18 | X(OP_NEG) \ |
| 19 | X(OP_MAX) |
| 20 | |
| 21 | #define X(x) x, |
| 22 | enum op { OPERATORS }; |
| 23 | #undef X |
| 24 | |
| 25 | enum instruction_type { |
| 26 | /// Push an immediate value to the top of the stack |
| 27 | INST_IMM = 0, |
| 28 | /// Pop two values from the top of the stack, apply operator, |
| 29 | /// and push the result to the top of the stack |
| 30 | INST_OP, |
| 31 | /// Load a memory slot and push its value to the top of the stack. |
| 32 | INST_LOAD, |
| 33 | /// Load from evaluation context and push the value to the top of the stack. |
| 34 | INST_LOAD_CTX, |
| 35 | /// Pop one value from the top of the stack, and store it into a memory slot. |
| 36 | INST_STORE, |
| 37 | /// Pop one value from the top of the stack, if the memory slot contains NaN, |
| 38 | /// store it into the memory slot; otherwise discard the value. |
| 39 | INST_STORE_OVER_NAN, |
| 40 | /// Pop a value from the top of the stack, clamp its value to [0, 1], then |
| 41 | /// evaluate a curve at that point, push the result to the top of the stack. |
| 42 | INST_CURVE, |
| 43 | /// Jump to the branch target only when the script is evaluated for the first |
| 44 | /// time. Used to perform initialization and such. |
| 45 | INST_BRANCH_ONCE, |
| 46 | /// Unconditional branch |
| 47 | INST_BRANCH, |
| 48 | INST_HALT, |
| 49 | }; |
| 50 | |
| 51 | /// Store metadata about where the result of a variable is stored |
| 52 | struct variable_allocation { |
| 53 | UT_hash_handle hh; |
| 54 | char *name; |
| 55 | unsigned index; |
| 56 | /// The memory slot for variable named `name` |
| 57 | unsigned slot; |
| 58 | }; |
| 59 | |
| 60 | struct instruction { |
| 61 | enum instruction_type type; |
| 62 | union { |
| 63 | double imm; |
| 64 | enum op op; |
| 65 | /// Memory slot for load and store |
| 66 | unsigned slot; |
| 67 | /// Context offset for load_ctx |
| 68 | ptrdiff_t ctx; |
| 69 | /// Relative PC change for branching |
| 70 | int rel; |
| 71 | /// The curve |
| 72 | struct curve curve; |
| 73 | }; |
| 74 | }; |
| 75 | |
| 76 | /// When interrupting an already executing script and starting a new script, |
| 77 | /// we might want to inherit some of the existing values of variables as starting points, |
| 78 | /// i.e. we want to "resume" animation for the current state. This is configurable, and |
| 79 | /// can be disabled by enabling the `reset` property on a transition. This struct store |
| 80 | /// where the `start` variables of those "resumable" transition variables, which can be |
| 81 | /// overridden at the start of execution for this use case. |
| 82 | struct overridable_slot { |
| 83 | UT_hash_handle hh; |
| 84 | char *name; |
| 85 | unsigned slot; |
| 86 | }; |
| 87 | |
| 88 | struct script { |
| 89 | unsigned len; |
| 90 | unsigned n_slots; |
| 91 | /// The memory slot for storing the elapsed time. |
| 92 | /// The next slot after this is used for storing the total duration of the script. |
| 93 | unsigned elapsed_slot; |
| 94 | unsigned stack_size; |
| 95 | struct variable_allocation *vars; |
| 96 | struct overridable_slot *overrides; |
| 97 | struct instruction instrs[]; |
| 98 | }; |