Skip to content

Commit 56330ce

Browse files
committed
bpo-46724: Use JUMP_ABSOLUTE for all backward jumps. (GH-31326)
Backport of GH-31326, plus supporting code from 3.11.
1 parent b271953 commit 56330ce

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

Lib/test/test_dis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ def jumpy():
10441044
Instruction(opname='COMPARE_OP', opcode=107, arg=4, argval='>', argrepr='>', offset=34, starts_line=None, is_jump_target=False),
10451045
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=21, argval=42, argrepr='to 42', offset=36, starts_line=None, is_jump_target=False),
10461046
Instruction(opname='POP_TOP', opcode=1, arg=None, argval=None, argrepr='', offset=38, starts_line=8, is_jump_target=False),
1047-
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=26, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False),
1047+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=5, argval=52, argrepr='to 52', offset=40, starts_line=None, is_jump_target=False),
10481048
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=4, argval=8, argrepr='to 8', offset=42, starts_line=7, is_jump_target=True),
10491049
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=44, starts_line=10, is_jump_target=True),
10501050
Instruction(opname='LOAD_CONST', opcode=100, arg=4, argval='I can haz else clause?', argrepr="'I can haz else clause?'", offset=46, starts_line=None, is_jump_target=False),
@@ -1069,7 +1069,7 @@ def jumpy():
10691069
Instruction(opname='LOAD_CONST', opcode=100, arg=2, argval=4, argrepr='4', offset=84, starts_line=None, is_jump_target=False),
10701070
Instruction(opname='COMPARE_OP', opcode=107, arg=0, argval='<', argrepr='<', offset=86, starts_line=None, is_jump_target=False),
10711071
Instruction(opname='POP_JUMP_IF_FALSE', opcode=114, arg=46, argval=92, argrepr='to 92', offset=88, starts_line=None, is_jump_target=False),
1072-
Instruction(opname='JUMP_ABSOLUTE', opcode=113, arg=52, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False),
1072+
Instruction(opname='JUMP_FORWARD', opcode=110, arg=6, argval=104, argrepr='to 104', offset=90, starts_line=17, is_jump_target=False),
10731073
Instruction(opname='LOAD_FAST', opcode=124, arg=0, argval='i', argrepr='i', offset=92, starts_line=11, is_jump_target=True),
10741074
Instruction(opname='POP_JUMP_IF_TRUE', opcode=115, arg=28, argval=56, argrepr='to 56', offset=94, starts_line=None, is_jump_target=False),
10751075
Instruction(opname='LOAD_GLOBAL', opcode=116, arg=1, argval='print', argrepr='print', offset=96, starts_line=19, is_jump_target=True),
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Make sure that all backwards jumps use the ``JUMP_ABSOLUTE`` instruction, rather
2+
than ``JUMP_FORWARD`` with an argument of ``(2**32)+offset``.

Python/compile.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ typedef struct basicblock_ {
128128
unsigned b_nofallthrough : 1;
129129
/* Basic block exits scope (it ends with a return or raise) */
130130
unsigned b_exit : 1;
131+
/* Used by compiler passes to mark whether they have visited a basic block. */
132+
unsigned b_visited : 1;
131133
/* depth of stack upon entry of block, computed by stackdepth() */
132134
int b_startdepth;
133135
/* instruction offset for block, computed by assemble_jump_offsets() */
@@ -6701,6 +6703,31 @@ assemble_emit(struct assembler *a, struct instr *i)
67016703
return 1;
67026704
}
67036705

6706+
static void
6707+
normalize_jumps(struct assembler *a)
6708+
{
6709+
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6710+
b->b_visited = 0;
6711+
}
6712+
for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) {
6713+
b->b_visited = 1;
6714+
if (b->b_iused == 0) {
6715+
continue;
6716+
}
6717+
struct instr *last = &b->b_instr[b->b_iused-1];
6718+
if (last->i_opcode == JUMP_ABSOLUTE) {
6719+
if (last->i_target->b_visited == 0) {
6720+
last->i_opcode = JUMP_FORWARD;
6721+
}
6722+
}
6723+
if (last->i_opcode == JUMP_FORWARD) {
6724+
if (last->i_target->b_visited == 1) {
6725+
last->i_opcode = JUMP_ABSOLUTE;
6726+
}
6727+
}
6728+
}
6729+
}
6730+
67046731
static void
67056732
assemble_jump_offsets(struct assembler *a, struct compiler *c)
67066733
{
@@ -7137,6 +7164,10 @@ assemble(struct compiler *c, int addNone)
71377164
}
71387165
propagate_line_numbers(&a);
71397166
guarantee_lineno_for_exits(&a, c->u->u_firstlineno);
7167+
7168+
/* Order of basic blocks must have been determined by now */
7169+
normalize_jumps(&a);
7170+
71407171
/* Can't modify the bytecode after computing jump offsets. */
71417172
assemble_jump_offsets(&a, c);
71427173

0 commit comments

Comments
 (0)