The offset of a pointer from the reference pointer The 'reference pointer' is the address of the physical stack pointer at the start of the code section, as if each code section started with `const PyStackRef *reference = stack_pointer`
| 33 | |
| 34 | @dataclass |
| 35 | class PointerOffset: |
| 36 | """The offset of a pointer from the reference pointer |
| 37 | The 'reference pointer' is the address of the physical stack pointer |
| 38 | at the start of the code section, as if each code section started with |
| 39 | `const PyStackRef *reference = stack_pointer` |
| 40 | """ |
| 41 | numeric: int |
| 42 | positive: tuple[str, ...] |
| 43 | negative: tuple[str, ...] |
| 44 | |
| 45 | @staticmethod |
| 46 | def zero() -> "PointerOffset": |
| 47 | return PointerOffset(0, (), ()) |
| 48 | |
| 49 | def pop(self, item: StackItem) -> "PointerOffset": |
| 50 | return self - PointerOffset.from_item(item) |
| 51 | |
| 52 | def push(self, item: StackItem) -> "PointerOffset": |
| 53 | return self + PointerOffset.from_item(item) |
| 54 | |
| 55 | @staticmethod |
| 56 | def from_item(item: StackItem) -> "PointerOffset": |
| 57 | if not item.size: |
| 58 | return PointerOffset(1, (), ()) |
| 59 | txt = item.size.strip() |
| 60 | n: tuple[str, ...] = () |
| 61 | p: tuple[str, ...] = () |
| 62 | try: |
| 63 | i = int(txt) |
| 64 | except ValueError: |
| 65 | i = 0 |
| 66 | if txt[0] == "+": |
| 67 | txt = txt[1:] |
| 68 | if txt[0] == "-": |
| 69 | n = (txt[1:],) |
| 70 | else: |
| 71 | p = (txt,) |
| 72 | return PointerOffset(i, p, n) |
| 73 | |
| 74 | @staticmethod |
| 75 | def create(numeric: int, positive: tuple[str, ...], negative: tuple[str, ...]) -> "PointerOffset": |
| 76 | positive, negative = PointerOffset._simplify(positive, negative) |
| 77 | return PointerOffset(numeric, positive, negative) |
| 78 | |
| 79 | def __sub__(self, other: "PointerOffset") -> "PointerOffset": |
| 80 | return PointerOffset.create( |
| 81 | self.numeric - other.numeric, |
| 82 | self.positive + other.negative, |
| 83 | self.negative + other.positive |
| 84 | ) |
| 85 | |
| 86 | def __add__(self, other: "PointerOffset") -> "PointerOffset": |
| 87 | return PointerOffset.create( |
| 88 | self.numeric + other.numeric, |
| 89 | self.positive + other.positive, |
| 90 | self.negative + other.negative |
| 91 | ) |
| 92 |