2023-12-21 08:46:28 -04:00
|
|
|
import re
|
2023-12-20 10:27:25 -04:00
|
|
|
from analyzer import StackItem, Instruction, Uop
|
2023-12-07 08:49:40 -04:00
|
|
|
from dataclasses import dataclass
|
2023-12-12 08:12:17 -04:00
|
|
|
from cwriter import CWriter
|
2023-12-07 08:49:40 -04:00
|
|
|
|
2024-01-12 13:30:27 -04:00
|
|
|
UNUSED = {"unused"}
|
|
|
|
|
2023-12-07 08:49:40 -04:00
|
|
|
|
2023-12-21 08:46:28 -04:00
|
|
|
def maybe_parenthesize(sym: str) -> str:
|
|
|
|
"""Add parentheses around a string if it contains an operator
|
|
|
|
and is not already parenthesized.
|
|
|
|
|
|
|
|
An exception is made for '*' which is common and harmless
|
|
|
|
in the context where the symbolic size is used.
|
|
|
|
"""
|
|
|
|
if sym.startswith("(") and sym.endswith(")"):
|
|
|
|
return sym
|
|
|
|
if re.match(r"^[\s\w*]+$", sym):
|
|
|
|
return sym
|
|
|
|
else:
|
|
|
|
return f"({sym})"
|
|
|
|
|
|
|
|
|
2023-12-07 08:49:40 -04:00
|
|
|
def var_size(var: StackItem) -> str:
|
|
|
|
if var.condition:
|
|
|
|
# Special case simplification
|
|
|
|
if var.condition == "oparg & 1" and var.size == "1":
|
|
|
|
return f"({var.condition})"
|
|
|
|
else:
|
|
|
|
return f"(({var.condition}) ? {var.size} : 0)"
|
|
|
|
else:
|
|
|
|
return var.size
|
|
|
|
|
2024-01-12 13:30:27 -04:00
|
|
|
|
2023-12-20 10:27:25 -04:00
|
|
|
@dataclass
|
2023-12-07 08:49:40 -04:00
|
|
|
class StackOffset:
|
|
|
|
"The stack offset of the virtual base of the stack from the physical stack pointer"
|
|
|
|
|
2023-12-20 10:27:25 -04:00
|
|
|
popped: list[str]
|
|
|
|
pushed: list[str]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def empty() -> "StackOffset":
|
|
|
|
return StackOffset([], [])
|
2023-12-07 08:49:40 -04:00
|
|
|
|
|
|
|
def pop(self, item: StackItem) -> None:
|
|
|
|
self.popped.append(var_size(item))
|
|
|
|
|
|
|
|
def push(self, item: StackItem) -> None:
|
|
|
|
self.pushed.append(var_size(item))
|
|
|
|
|
2023-12-20 10:27:25 -04:00
|
|
|
def __sub__(self, other: "StackOffset") -> "StackOffset":
|
2024-01-12 13:30:27 -04:00
|
|
|
return StackOffset(self.popped + other.pushed, self.pushed + other.popped)
|
2023-12-20 10:27:25 -04:00
|
|
|
|
|
|
|
def __neg__(self) -> "StackOffset":
|
|
|
|
return StackOffset(self.pushed, self.popped)
|
|
|
|
|
2023-12-07 08:49:40 -04:00
|
|
|
def simplify(self) -> None:
|
|
|
|
"Remove matching values from both the popped and pushed list"
|
|
|
|
if not self.popped or not self.pushed:
|
|
|
|
return
|
|
|
|
# Sort the list so the lexically largest element is last.
|
|
|
|
popped = sorted(self.popped)
|
|
|
|
pushed = sorted(self.pushed)
|
|
|
|
self.popped = []
|
|
|
|
self.pushed = []
|
|
|
|
while popped and pushed:
|
|
|
|
pop = popped.pop()
|
|
|
|
push = pushed.pop()
|
|
|
|
if pop == push:
|
|
|
|
pass
|
|
|
|
elif pop > push:
|
|
|
|
# if pop > push, there can be no element in pushed matching pop.
|
|
|
|
self.popped.append(pop)
|
|
|
|
pushed.append(push)
|
|
|
|
else:
|
|
|
|
self.pushed.append(push)
|
|
|
|
popped.append(pop)
|
|
|
|
self.popped.extend(popped)
|
|
|
|
self.pushed.extend(pushed)
|
|
|
|
|
|
|
|
def to_c(self) -> str:
|
|
|
|
self.simplify()
|
|
|
|
int_offset = 0
|
|
|
|
symbol_offset = ""
|
|
|
|
for item in self.popped:
|
|
|
|
try:
|
|
|
|
int_offset -= int(item)
|
|
|
|
except ValueError:
|
|
|
|
symbol_offset += f" - {maybe_parenthesize(item)}"
|
|
|
|
for item in self.pushed:
|
|
|
|
try:
|
|
|
|
int_offset += int(item)
|
|
|
|
except ValueError:
|
|
|
|
symbol_offset += f" + {maybe_parenthesize(item)}"
|
|
|
|
if symbol_offset and not int_offset:
|
|
|
|
res = symbol_offset
|
|
|
|
else:
|
|
|
|
res = f"{int_offset}{symbol_offset}"
|
|
|
|
if res.startswith(" + "):
|
|
|
|
res = res[3:]
|
|
|
|
if res.startswith(" - "):
|
|
|
|
res = "-" + res[3:]
|
|
|
|
return res
|
|
|
|
|
|
|
|
def clear(self) -> None:
|
|
|
|
self.popped = []
|
|
|
|
self.pushed = []
|
2023-12-12 08:12:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
class SizeMismatch(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class Stack:
|
|
|
|
def __init__(self) -> None:
|
2023-12-20 10:27:25 -04:00
|
|
|
self.top_offset = StackOffset.empty()
|
|
|
|
self.base_offset = StackOffset.empty()
|
|
|
|
self.peek_offset = StackOffset.empty()
|
2023-12-12 08:12:17 -04:00
|
|
|
self.variables: list[StackItem] = []
|
|
|
|
self.defined: set[str] = set()
|
|
|
|
|
|
|
|
def pop(self, var: StackItem) -> str:
|
|
|
|
self.top_offset.pop(var)
|
|
|
|
if not var.peek:
|
|
|
|
self.peek_offset.pop(var)
|
|
|
|
indirect = "&" if var.is_array() else ""
|
|
|
|
if self.variables:
|
|
|
|
popped = self.variables.pop()
|
|
|
|
if popped.size != var.size:
|
|
|
|
raise SizeMismatch(
|
|
|
|
f"Size mismatch when popping '{popped.name}' from stack to assign to {var.name}. "
|
|
|
|
f"Expected {var.size} got {popped.size}"
|
|
|
|
)
|
|
|
|
if popped.name == var.name:
|
|
|
|
return ""
|
2024-01-12 13:30:27 -04:00
|
|
|
elif popped.name in UNUSED:
|
2023-12-12 08:12:17 -04:00
|
|
|
self.defined.add(var.name)
|
|
|
|
return (
|
|
|
|
f"{var.name} = {indirect}stack_pointer[{self.top_offset.to_c()}];\n"
|
|
|
|
)
|
2024-01-12 13:30:27 -04:00
|
|
|
elif var.name in UNUSED:
|
2023-12-12 08:12:17 -04:00
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
self.defined.add(var.name)
|
|
|
|
return f"{var.name} = {popped.name};\n"
|
|
|
|
self.base_offset.pop(var)
|
2024-01-12 13:30:27 -04:00
|
|
|
if var.name in UNUSED:
|
2023-12-12 08:12:17 -04:00
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
self.defined.add(var.name)
|
|
|
|
cast = f"({var.type})" if (not indirect and var.type) else ""
|
|
|
|
assign = (
|
|
|
|
f"{var.name} = {cast}{indirect}stack_pointer[{self.base_offset.to_c()}];"
|
|
|
|
)
|
|
|
|
if var.condition:
|
|
|
|
return f"if ({var.condition}) {{ {assign} }}\n"
|
|
|
|
return f"{assign}\n"
|
|
|
|
|
|
|
|
def push(self, var: StackItem) -> str:
|
|
|
|
self.variables.append(var)
|
2024-01-12 13:30:27 -04:00
|
|
|
if var.is_array() and var.name not in self.defined and var.name not in UNUSED:
|
2023-12-12 08:12:17 -04:00
|
|
|
c_offset = self.top_offset.to_c()
|
|
|
|
self.top_offset.push(var)
|
|
|
|
self.defined.add(var.name)
|
|
|
|
return f"{var.name} = &stack_pointer[{c_offset}];\n"
|
|
|
|
else:
|
|
|
|
self.top_offset.push(var)
|
|
|
|
return ""
|
|
|
|
|
2024-02-13 09:24:48 -04:00
|
|
|
def flush(self, out: CWriter, cast_type: str = "PyObject *") -> None:
|
2024-01-15 07:41:06 -04:00
|
|
|
out.start_line()
|
2023-12-12 08:12:17 -04:00
|
|
|
for var in self.variables:
|
|
|
|
if not var.peek:
|
2024-02-13 09:24:48 -04:00
|
|
|
cast = f"({cast_type})" if var.type else ""
|
2024-01-12 13:30:27 -04:00
|
|
|
if var.name not in UNUSED and not var.is_array():
|
2023-12-12 08:12:17 -04:00
|
|
|
if var.condition:
|
2023-12-18 07:14:40 -04:00
|
|
|
out.emit(f"if ({var.condition}) ")
|
2023-12-12 08:12:17 -04:00
|
|
|
out.emit(
|
|
|
|
f"stack_pointer[{self.base_offset.to_c()}] = {cast}{var.name};\n"
|
|
|
|
)
|
|
|
|
self.base_offset.push(var)
|
|
|
|
if self.base_offset.to_c() != self.top_offset.to_c():
|
|
|
|
print("base", self.base_offset.to_c(), "top", self.top_offset.to_c())
|
|
|
|
assert False
|
|
|
|
number = self.base_offset.to_c()
|
|
|
|
if number != "0":
|
|
|
|
out.emit(f"stack_pointer += {number};\n")
|
|
|
|
self.variables = []
|
|
|
|
self.base_offset.clear()
|
|
|
|
self.top_offset.clear()
|
|
|
|
self.peek_offset.clear()
|
2024-01-15 07:41:06 -04:00
|
|
|
out.start_line()
|
2023-12-12 08:12:17 -04:00
|
|
|
|
|
|
|
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()} */"
|
2023-12-20 10:27:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_stack_effect(inst: Instruction) -> Stack:
|
|
|
|
stack = Stack()
|
|
|
|
for uop in inst.parts:
|
|
|
|
if not isinstance(uop, Uop):
|
|
|
|
continue
|
|
|
|
for var in reversed(uop.stack.inputs):
|
|
|
|
stack.pop(var)
|
|
|
|
for i, var in enumerate(uop.stack.outputs):
|
|
|
|
stack.push(var)
|
|
|
|
return stack
|