diff --git a/tests/unit/compiler/venom/test_duplicate_operands.py b/tests/unit/compiler/venom/test_duplicate_operands.py index b96c7f3351..437185cc72 100644 --- a/tests/unit/compiler/venom/test_duplicate_operands.py +++ b/tests/unit/compiler/venom/test_duplicate_operands.py @@ -22,6 +22,6 @@ def test_duplicate_operands(): bb.append_instruction("mul", sum_, op) bb.append_instruction("stop") - asm = generate_assembly_experimental(ctx, optimize=OptimizationLevel.CODESIZE) + asm = generate_assembly_experimental(ctx, optimize=OptimizationLevel.GAS) - assert asm == ["PUSH1", 10, "DUP1", "DUP1", "DUP1", "ADD", "MUL", "STOP", "REVERT"] + assert asm == ["PUSH1", 10, "DUP1", "DUP1", "DUP1", "ADD", "MUL", "STOP"] diff --git a/vyper/ir/compile_ir.py b/vyper/ir/compile_ir.py index 8ce8c887f1..8b09ae454f 100644 --- a/vyper/ir/compile_ir.py +++ b/vyper/ir/compile_ir.py @@ -809,18 +809,26 @@ def _prune_unreachable_code(assembly): # unreachable changed = False i = 0 - while i < len(assembly) - 2: - instr = assembly[i] - if isinstance(instr, list): - instr = assembly[i][-1] + while i < len(assembly) - 1: + if assembly[i] in _TERMINAL_OPS: + # find the next jumpdest or sublist + for j in range(i + 1, len(assembly)): + next_is_jumpdest = ( + j < len(assembly) - 1 + and is_symbol(assembly[j]) + and assembly[j + 1] == "JUMPDEST" + ) + next_is_list = isinstance(assembly[j], list) + if next_is_jumpdest or next_is_list: + break + else: + # fixup an off-by-one if we made it to the end of the assembly + # without finding an jumpdest or sublist + j = len(assembly) + changed = j > i + 1 + del assembly[i + 1 : j] - if assembly[i] in _TERMINAL_OPS and not ( - is_symbol(assembly[i + 1]) or isinstance(assembly[i + 1], list) - ): - changed = True - del assembly[i + 1] - else: - i += 1 + i += 1 return changed @@ -1230,7 +1238,6 @@ def assembly_to_evm_with_symbol_map(assembly, pc_ofst=0, insert_compiler_metadat if is_symbol_map_indicator(assembly[i + 1]): # Don't increment pc as the symbol itself doesn't go into code if item in symbol_map: - print(assembly) raise CompilerPanic(f"duplicate jumpdest {item}") symbol_map[item] = pc