diff --git a/projects/shoggoth/src/python/ichor/assembler.py b/projects/shoggoth/src/python/ichor/assembler.py index fb36db9..2aa2c1c 100644 --- a/projects/shoggoth/src/python/ichor/assembler.py +++ b/projects/shoggoth/src/python/ichor/assembler.py @@ -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]]): diff --git a/projects/shoggoth/src/python/ichor/isa.py b/projects/shoggoth/src/python/ichor/isa.py index 3667ffc..ea06037 100644 --- a/projects/shoggoth/src/python/ichor/isa.py +++ b/projects/shoggoth/src/python/ichor/isa.py @@ -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 diff --git a/projects/shoggoth/test/python/ichor/test_assembler.py b/projects/shoggoth/test/python/ichor/test_assembler.py index a6feb69..8ed747f 100644 --- a/projects/shoggoth/test/python/ichor/test_assembler.py +++ b/projects/shoggoth/test/python/ichor/test_assembler.py @@ -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) diff --git a/projects/shoggoth/test/python/ichor/test_bootstrap.py b/projects/shoggoth/test/python/ichor/test_bootstrap.py index c3f793d..596fd38 100644 --- a/projects/shoggoth/test/python/ichor/test_bootstrap.py +++ b/projects/shoggoth/test/python/ichor/test_bootstrap.py @@ -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