Tweaking the stack analyzer

This commit is contained in:
Reid 'arrdem' McKenzie 2022-08-19 00:43:42 -06:00
parent 71a60ae079
commit fbe837797a
4 changed files with 19 additions and 21 deletions

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python3
import logging
from random import choices
from string import ascii_lowercase, digits
from typing import List, Optional, Sequence, Union
@ -7,6 +8,9 @@ from typing import List, Optional, Sequence, Union
from ichor import isa
log = logging.getLogger(__name__)
def gensym(prefix = None) -> isa.Label:
frag = "".join(choices(ascii_lowercase + digits, k=8))
return isa.Label(f"{prefix or 'gensym'}_{frag}")
@ -26,22 +30,14 @@ class FuncBuilder(object):
if not self._straightline:
return
start_stack = self._stack
match op:
case isa.Label(_):
# Labels abort
self._straightline = False
case isa.GOTO(_) | isa.ATEST(_):
# Control ops abort
case isa.Label(_) | isa.GOTO(_) | isa.ATEST(_):
# Labels and control ops abort
self._straightline = False
case isa.CLOSUREC(n) | isa.CLOSUREF(n):
self._stack -= n
case isa.TYPEREF():
pass
case isa.ARMREF():
self._stack -= 1
@ -51,14 +47,15 @@ class FuncBuilder(object):
case isa.DUP(n):
self._stack += n
case isa.ROT(_):
pass
case _:
case isa.IDENTIFIERC(_) | isa.SLOT(_) | isa.CALLF(_) | isa.CALLC(_) | isa.ARM(_):
self._stack -= getattr(op, "nargs", 0)
self._stack += 1
print(op, "pre", start_stack, "post", self._stack)
case isa.ROT(_) | isa.FUNREF(_) | isa.TYPEREF() | isa.ALOAD():
pass
case _:
log.debug("No stack manipulation known for %s, assuming no change", op)
def write(self, op: Union[isa.Opcode, isa.Label, Sequence[isa.Opcode]]):

View file

@ -2,6 +2,7 @@
from abc import ABC
from dataclasses import dataclass
from typing import Union
@dataclass
@ -26,7 +27,7 @@ class GOTO(Opcode):
"""
target: int
target: Union[int, Label]
################################################################################
# Stack manipulation
@ -77,7 +78,7 @@ class SLOT(Opcode):
"""
target: int
target: Union[int, Label]
################################################################################
# Functional abstraction
@ -227,7 +228,7 @@ class ATEST(Opcode):
"""
target: int
target: Union[int, Label]
@dataclass

View file

@ -49,7 +49,7 @@ def test_self_label(builder: FuncBuilder):
def test_local_label(builder: FuncBuilder):
s0 = builder.mark_slot()
s0 = builder.mark_slot(target=0)
builder.write(isa.SLOT(s0))
s999 = builder.mark_slot(target=999)

View file

@ -4,13 +4,13 @@ from .fixtures import * # noqa
from ichor import isa
from ichor.bootstrap import (
AND2,
AND3,
FALSE,
NOT1,
OR2,
OR3,
TRUE,
AND2,
AND3
)
import pytest