Visitar URL original
GH-123391: Code generators: Track stack and locals in `if` statements. by markshannon · Pull Request #123397 · python/cpython · GitHub
Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add copy and == support to Stack class
  • Loading branch information
markshannon committed Aug 7, 2024
commit 75bda2803addd6513c2187ed754ff73adbd1ff77
36 changes: 36 additions & 0 deletions Tools/cases_generator/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,19 @@ def condition(self) -> str | None:
def is_array(self) -> bool:
return self.item.is_array()

def copy(self) -> Local:
return Local(self.item, self.cached, self.in_memory, self.defined)

def __eq__(self, other: object) -> bool:
if not isinstance(other, Local):
return NotImplemented
return (
self.item is other.item and
self.cached is other.cached and
self.in_memory is other.in_memory and
self.defined is other.defined
)

@dataclass
class StackOffset:
"The stack offset of the virtual base of the stack from the physical stack pointer"
Expand Down Expand Up @@ -159,12 +172,17 @@ def clear(self) -> None:
self.popped = []
self.pushed = []

def __eq__(self, other: object) -> bool:
if not isinstance(other, StackOffset):
return NotImplemented
return self.to_c() == other.to_c()

class StackError(Exception):
pass


class Stack:

def __init__(self) -> None:
self.top_offset = StackOffset.empty()
self.base_offset = StackOffset.empty()
Expand Down Expand Up @@ -296,6 +314,24 @@ def peek_offset(self) -> str:
def as_comment(self) -> str:
return f"/* Variables: {[v.name for v in self.variables]}. Base offset: {self.base_offset.to_c()}. Top offset: {self.top_offset.to_c()} */"

def copy(self) -> Stack:
other = Stack()
other.top_offset = self.top_offset.copy()
other.base_offset = self.base_offset.copy()
other.variables = [ var.copy() for var in self.variables ]
other.defined = set(self.defined)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
other.defined = set(self.defined)
other.defined = self.defined.copy()

return other

def __eq__(self, other: object) -> bool:
if not isinstance(other, Stack):
return NotImplemented
return (
self.top_offset == other.top_offset and
self.base_offset == other.base_offset and
self.variables == other.variables and
self.defined == other.defined
)


def get_stack_effect(inst: Instruction | PseudoInstruction) -> Stack:
stack = Stack()
Expand Down