Skip to content

Commit d49654c

Browse files
committed
py/vm.c: Document SELECTIVE_EXC_IP.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
1 parent 07cae91 commit d49654c

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

py/vm.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,30 @@
202202
// MP_VM_RETURN_YIELD, ip, sp valid, yielded value in *sp
203203
// MP_VM_RETURN_EXCEPTION, exception in state[0]
204204
mp_vm_return_kind_t MICROPY_WRAP_MP_EXECUTE_BYTECODE(mp_execute_bytecode)(mp_code_state_t *code_state, volatile mp_obj_t inject_exc) {
205+
205206
#define SELECTIVE_EXC_IP (0)
207+
// When disabled, code_state->ip is updated unconditionally during op
208+
// dispatch, and this is subsequently used in the exception handler
209+
// (either NLR jump or direct RAISE). This is good for code size because it
210+
// happens in a single place but is more work than necessary, as many opcodes
211+
// cannot raise. Enabling SELECTIVE_EXC_IP means that code_state->ip
212+
// is "selectively" updated only during handling of opcodes that might raise.
213+
// This costs about 360 bytes on PYBV11 for a 1-3% performance gain (e.g. 3%
214+
// in bm_fft.py). On rp2040, there is zero code size diff for a 0-1% gain.
215+
// (Both with computed goto enabled).
206216
#if SELECTIVE_EXC_IP
207-
#define MARK_EXC_IP_SELECTIVE() { code_state->ip = ip; } /* stores ip 1 byte past last opcode */
217+
// Note: Because ip has already been advanced by one byte in the dispatch, the
218+
// value of ip here is one byte past the last opcode.
219+
#define MARK_EXC_IP_SELECTIVE() { code_state->ip = ip; }
220+
// No need to update in dispatch.
208221
#define MARK_EXC_IP_GLOBAL()
209222
#else
210223
#define MARK_EXC_IP_SELECTIVE()
211-
#define MARK_EXC_IP_GLOBAL() { code_state->ip = ip; } /* stores ip pointing to last opcode */
224+
// Immediately before dispatch, save the current ip, which will be the opcode
225+
// about to be dispatched.
226+
#define MARK_EXC_IP_GLOBAL() { code_state->ip = ip; }
212227
#endif
228+
213229
#if MICROPY_OPT_COMPUTED_GOTO
214230
#include "py/vmentrytable.h"
215231
#define DISPATCH() do { \

0 commit comments

Comments
 (0)