Skip to content

Commit f89d659

Browse files
committed
py: In VM, for selective ip saving, store 1 byte past last opcode.
This is for efficiency, so we don't need to subtract 1 from the ip before storing it to code_state->ip. It saves a lot of ROM bytes on unix and stmhal.
1 parent 23f1b5f commit f89d659

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

py/vm.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ typedef enum {
103103
mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_obj_t inject_exc) {
104104
#define SELECTIVE_EXC_IP (0)
105105
#if SELECTIVE_EXC_IP
106-
#define MARK_EXC_IP_SELECTIVE() { code_state->ip = ip - 1; }
106+
#define MARK_EXC_IP_SELECTIVE() { code_state->ip = ip; } /* stores ip 1 byte past last opcode */
107107
#define MARK_EXC_IP_GLOBAL()
108108
#else
109109
#define MARK_EXC_IP_SELECTIVE()
110-
#define MARK_EXC_IP_GLOBAL() { code_state->ip = ip; }
110+
#define MARK_EXC_IP_GLOBAL() { code_state->ip = ip; } /* stores ip pointing to last opcode */
111111
#endif
112112
#if MICROPY_OPT_COMPUTED_GOTO
113113
#include "vmentrytable.h"
@@ -545,7 +545,11 @@ unwind_jump:;
545545
ENTRY(MP_BC_SETUP_EXCEPT):
546546
ENTRY(MP_BC_SETUP_FINALLY): {
547547
MARK_EXC_IP_SELECTIVE();
548-
PUSH_EXC_BLOCK((*code_state->ip == MP_BC_SETUP_FINALLY) ? 1 : 0);
548+
#if SELECTIVE_EXC_IP
549+
PUSH_EXC_BLOCK((code_state->ip[-1] == MP_BC_SETUP_FINALLY) ? 1 : 0);
550+
#else
551+
PUSH_EXC_BLOCK((code_state->ip[0] == MP_BC_SETUP_FINALLY) ? 1 : 0);
552+
#endif
549553
DISPATCH();
550554
}
551555

@@ -1005,6 +1009,11 @@ unwind_jump:;
10051009
exception_handler:
10061010
// exception occurred
10071011

1012+
#if SELECTIVE_EXC_IP
1013+
// with selective ip, we store the ip 1 byte past the opcode, so move ptr back
1014+
code_state->ip -= 1;
1015+
#endif
1016+
10081017
// check if it's a StopIteration within a for block
10091018
if (*code_state->ip == MP_BC_FOR_ITER && mp_obj_is_subclass_fast(mp_obj_get_type(nlr.ret_val), &mp_type_StopIteration)) {
10101019
const byte *ip = code_state->ip + 1;

0 commit comments

Comments
 (0)