diff --git a/vyper/codegen/dfg.py b/vyper/codegen/dfg.py index 5fb4d77191..773a96aaef 100644 --- a/vyper/codegen/dfg.py +++ b/vyper/codegen/dfg.py @@ -215,22 +215,22 @@ def _stack_duplications( def _stack_reorder(assembly: list, stack_map: StackMap, stack_ops: list[IRValueBase]) -> None: stack_ops = [x.value for x in stack_ops] + #print("ENTER reorder", stack_map.stack_map, stack_ops) + #start_len = len(assembly) for i in range(len(stack_ops)): op = stack_ops[i] final_stack_depth = -(len(stack_ops) - i - 1) depth = stack_map.get_depth_in(op) assert depth is not StackMap.NOT_IN_STACK, f"{op} not in stack: {stack_map.stack_map}" - is_in_place = depth == final_stack_depth + if depth == final_stack_depth: + continue - if not is_in_place: - if final_stack_depth == 0 and depth != 0: - stack_map.swap(assembly, depth) - elif final_stack_depth != 0 and depth == 0: - stack_map.swap(assembly, final_stack_depth) - else: - stack_map.swap(assembly, depth) - stack_map.swap(assembly, final_stack_depth) + #print("trace", depth, final_stack_depth) + stack_map.swap(assembly, depth) + stack_map.swap(assembly, final_stack_depth) + #print("INSTRUCTIONS", assembly[start_len:]) + #print("EXIT reorder", stack_map.stack_map, stack_ops) def _generate_evm_for_basicblock_r( ctx: IRFunction, asm: list, basicblock: IRBasicBlock, stack_map: StackMap diff --git a/vyper/compiler/utils.py b/vyper/compiler/utils.py index 4056575260..572a339e6a 100644 --- a/vyper/compiler/utils.py +++ b/vyper/compiler/utils.py @@ -136,6 +136,10 @@ def swap(self, assembly: list[str], depth: int) -> None: """ Swaps the operand at the given depth in the stack map with the top of the stack. """ + # convenience, avoids branching in caller + if depth == 0: + return + assert depth < 0, "Cannot swap positive depth" assembly.append(f"SWAP{-depth}") self.stack_map[depth - 1], self.stack_map[-1] = (