From f6eafe18c004c55082de40d20cad084ef9dd3db7 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Wed, 6 Oct 2021 13:05:45 +0100 Subject: Normalize jumps in compiler. All forward jumps to use JUMP_FORWARD. (GH-28755) --- Python/compile.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'Python/compile.c') diff --git a/Python/compile.c b/Python/compile.c index 694da29b77..2d82d6a1e5 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -7220,6 +7220,26 @@ assemble_emit(struct assembler *a, struct instr *i) return 1; } +static void +normalize_jumps(struct assembler *a) +{ + for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { + b->b_visited = 0; + } + for (basicblock *b = a->a_entry; b != NULL; b = b->b_next) { + b->b_visited = 1; + if (b->b_iused == 0) { + continue; + } + struct instr *last = &b->b_instr[b->b_iused-1]; + if (last->i_opcode == JUMP_ABSOLUTE && + last->i_target->b_visited == 0 + ) { + last->i_opcode = JUMP_FORWARD; + } + } +} + static void assemble_jump_offsets(struct assembler *a, struct compiler *c) { @@ -7897,6 +7917,9 @@ assemble(struct compiler *c, int addNone) clean_basic_block(b); } + /* Order of basic blocks must have been determined by now */ + normalize_jumps(&a); + /* Can't modify the bytecode after computing jump offsets. */ assemble_jump_offsets(&a, c); -- cgit v1.2.1