Skip to content

Commit 6d11c69

Browse files
committed
py: Change jump-if-x-or-pop opcodes to have unsigned offset argument.
These jumps are always forwards, and it's more efficient in the VM to decode an unsigned argument. These opcodes are already optimised versions of the sequence "dup-top pop-jump-if-x pop" so it doesn't hurt generality to optimise them further. Signed-off-by: Damien George <[email protected]>
1 parent acd2c5c commit 6d11c69

File tree

5 files changed

+11
-13
lines changed

5 files changed

+11
-13
lines changed

py/bc0.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@
117117
#define MP_BC_JUMP (MP_BC_BASE_JUMP_E + 0x02) // signed relative bytecode offset
118118
#define MP_BC_POP_JUMP_IF_TRUE (MP_BC_BASE_JUMP_E + 0x03) // signed relative bytecode offset
119119
#define MP_BC_POP_JUMP_IF_FALSE (MP_BC_BASE_JUMP_E + 0x04) // signed relative bytecode offset
120-
#define MP_BC_JUMP_IF_TRUE_OR_POP (MP_BC_BASE_JUMP_E + 0x05) // signed relative bytecode offset
121-
#define MP_BC_JUMP_IF_FALSE_OR_POP (MP_BC_BASE_JUMP_E + 0x06) // signed relative bytecode offset
120+
#define MP_BC_JUMP_IF_TRUE_OR_POP (MP_BC_BASE_JUMP_E + 0x05) // unsigned relative bytecode offset
121+
#define MP_BC_JUMP_IF_FALSE_OR_POP (MP_BC_BASE_JUMP_E + 0x06) // unsigned relative bytecode offset
122122
#define MP_BC_SETUP_WITH (MP_BC_BASE_JUMP_E + 0x07) // unsigned relative bytecode offset
123123
#define MP_BC_SETUP_EXCEPT (MP_BC_BASE_JUMP_E + 0x08) // unsigned relative bytecode offset
124124
#define MP_BC_SETUP_FINALLY (MP_BC_BASE_JUMP_E + 0x09) // unsigned relative bytecode offset

py/emitbc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ STATIC void emit_write_bytecode_byte_label(emit_t *emit, int stack_adj, byte b1,
224224
mp_emit_bc_adjust_stack_size(emit, stack_adj);
225225

226226
// Determine if the jump offset is signed or unsigned, based on the opcode.
227-
const bool is_signed = b1 <= MP_BC_JUMP_IF_FALSE_OR_POP;
227+
const bool is_signed = b1 <= MP_BC_POP_JUMP_IF_FALSE;
228228

229229
// Default to a 2-byte encoding (the largest) with an unknown jump offset.
230230
unsigned int jump_encoding_size = 1;

py/showbc.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,12 @@ const byte *mp_bytecode_print_str(const mp_print_t *print, const byte *ip_start,
338338
break;
339339

340340
case MP_BC_JUMP_IF_TRUE_OR_POP:
341-
DECODE_SLABEL;
341+
DECODE_ULABEL;
342342
mp_printf(print, "JUMP_IF_TRUE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - ip_start));
343343
break;
344344

345345
case MP_BC_JUMP_IF_FALSE_OR_POP:
346-
DECODE_SLABEL;
346+
DECODE_ULABEL;
347347
mp_printf(print, "JUMP_IF_FALSE_OR_POP " UINT_FMT, (mp_uint_t)(ip + unum - ip_start));
348348
break;
349349

py/vm.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -560,21 +560,21 @@ FRAME_SETUP();
560560
}
561561

562562
ENTRY(MP_BC_JUMP_IF_TRUE_OR_POP): {
563-
DECODE_SLABEL;
563+
DECODE_ULABEL;
564564
if (mp_obj_is_true(TOP())) {
565-
ip += slab;
565+
ip += ulab;
566566
} else {
567567
sp--;
568568
}
569569
DISPATCH_WITH_PEND_EXC_CHECK();
570570
}
571571

572572
ENTRY(MP_BC_JUMP_IF_FALSE_OR_POP): {
573-
DECODE_SLABEL;
573+
DECODE_ULABEL;
574574
if (mp_obj_is_true(TOP())) {
575575
sp--;
576576
} else {
577-
ip += slab;
577+
ip += ulab;
578578
}
579579
DISPATCH_WITH_PEND_EXC_CHECK();
580580
}

tools/mpy-tool.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,8 @@ class Opcodes:
246246
MP_BC_JUMP = (MP_BC_BASE_JUMP_E + 0x02) # signed relative bytecode offset
247247
MP_BC_POP_JUMP_IF_TRUE = (MP_BC_BASE_JUMP_E + 0x03) # signed relative bytecode offset
248248
MP_BC_POP_JUMP_IF_FALSE = (MP_BC_BASE_JUMP_E + 0x04) # signed relative bytecode offset
249-
MP_BC_JUMP_IF_TRUE_OR_POP = (MP_BC_BASE_JUMP_E + 0x05) # signed relative bytecode offset
250-
MP_BC_JUMP_IF_FALSE_OR_POP = (MP_BC_BASE_JUMP_E + 0x06) # signed relative bytecode offset
249+
MP_BC_JUMP_IF_TRUE_OR_POP = (MP_BC_BASE_JUMP_E + 0x05) # unsigned relative bytecode offset
250+
MP_BC_JUMP_IF_FALSE_OR_POP = (MP_BC_BASE_JUMP_E + 0x06) # unsigned relative bytecode offset
251251
MP_BC_SETUP_WITH = (MP_BC_BASE_JUMP_E + 0x07) # unsigned relative bytecode offset
252252
MP_BC_SETUP_EXCEPT = (MP_BC_BASE_JUMP_E + 0x08) # unsigned relative bytecode offset
253253
MP_BC_SETUP_FINALLY = (MP_BC_BASE_JUMP_E + 0x09) # unsigned relative bytecode offset
@@ -295,8 +295,6 @@ class Opcodes:
295295
MP_BC_JUMP,
296296
MP_BC_POP_JUMP_IF_TRUE,
297297
MP_BC_POP_JUMP_IF_FALSE,
298-
MP_BC_JUMP_IF_TRUE_OR_POP,
299-
MP_BC_JUMP_IF_FALSE_OR_POP,
300298
)
301299

302300
# Create a dict mapping opcode value to opcode name.

0 commit comments

Comments
 (0)