Skip to content

Commit f876fdb

Browse files
fix: off by one error in forward jump calculation (python#19)
* fix: off by one error in forward jump calculation * Test: fixed test for python#18 * test: remove broken test --------- Co-authored-by: Jules <[email protected]>
1 parent 3fd00f7 commit f876fdb

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

Python/tier2.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ _PyTier2_Code_DetectAndEmitBB(
16941694
prev_type_guard->op.code == CHECK_LIST
16951695
);
16961696
#define END() goto end;
1697-
#define JUMPBY(x) i += x + 1;
1697+
#define JUMPBY(x) i += x;
16981698
#define DISPATCH() write_i = emit_i(write_i, specop, curr->op.arg); \
16991699
write_i = copy_cache_entries(write_i, curr+1, caches); \
17001700
i += caches; \
@@ -2067,8 +2067,14 @@ _PyTier2_Code_DetectAndEmitBB(
20672067

20682068
// Jumps may be the end of a basic block if they are conditional (a branch).
20692069
if (IS_JUMP_OPCODE(opcode)) {
2070+
#if BB_DEBUG
2071+
fprintf(stderr, "Encountered a forward jump\n");
2072+
#endif
20702073
// Unconditional forward jump... continue with the BB without writing the jump.
20712074
if (opcode == JUMP_FORWARD) {
2075+
#if BB_DEBUG
2076+
fprintf(stderr, "Encountered an unconditional forward jump\n");
2077+
#endif
20722078
// JUMP offset (oparg) + current instruction + cache entries
20732079
JUMPBY(oparg);
20742080
continue;

tier2_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,5 +462,19 @@ def f(y,z,w):
462462

463463
trigger_tier2(f, (1,1,1.))
464464

465+
# As long as it doesn't crash, everything's good.
466+
467+
######################################################################
468+
# Tests for: Tier 2 unconditional forward jump #
469+
######################################################################
470+
with TestInfo("tier 2 unconditional forward jumps"):
471+
# See https://github.com/pylbbv/pylbbv/issues/17 for more information.
472+
def f(x):
473+
for _ in [1]:
474+
break
475+
x+x # Force it to be optimisable
476+
477+
trigger_tier2(f, (1,))
478+
465479
# As long as it doesn't crash, everything's good.
466480
print("Tests completed ^-^")

0 commit comments

Comments
 (0)