Skip to content

Commit

Permalink
simplify _stack_reorder
Browse files Browse the repository at this point in the history
add some tracing things in the comments
  • Loading branch information
charles-cooper committed Oct 28, 2023
1 parent 0eea288 commit c10e964
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 9 additions & 9 deletions vyper/codegen/dfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions vyper/compiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = (
Expand Down

0 comments on commit c10e964

Please sign in to comment.