Merge pull request #72 from adafruit/public-labels-etc

Add public_labels, test of labels
This commit is contained in:
Jeff Epler 2024-09-24 16:58:45 -05:00 committed by GitHub
commit 11ddcb5327
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 79 additions and 2 deletions

View file

@ -12,7 +12,7 @@ Simple assembler to convert pioasm to bytes
"""
try:
from typing import List, MutableSequence
from typing import List, Sequence, Any
except ImportError:
pass
@ -55,12 +55,20 @@ class Program: # pylint: disable=too-few-public-methods
"""
assembled: array.array
"""The assembled PIO program instructions"""
public_labels: dict[str, int]
"""The offset of any labels declared public"""
pio_kwargs: dict[str, Any]
"""Settings from assembler directives to pass to the StateMachine constructor"""
def __init__(self, text_program: str, *, build_debuginfo: bool = False) -> None:
"""Converts pioasm text to encoded instruction bytes"""
# pylint: disable=too-many-branches,too-many-statements,too-many-locals
assembled: List[int] = []
program_name = None
labels = {}
public_labels = {}
linemap = []
instructions: List[str] = []
sideset_count = 0
@ -219,6 +227,9 @@ class Program: # pylint: disable=too-few-public-methods
elif line.endswith(":"):
label = line[:-1]
if line.startswith("public "):
label = label[7:]
public_labels[label] = len(instructions)
if label in labels:
raise SyntaxError(f"Duplicate label {repr(label)}")
labels[label] = len(instructions)
@ -227,6 +238,7 @@ class Program: # pylint: disable=too-few-public-methods
instructions.append(line)
linemap.append(i)
mov_destinations: Sequence[str | None]
if pio_version >= 1:
mov_destinations = MOV_DESTINATIONS_V1
else:
@ -502,6 +514,8 @@ class Program: # pylint: disable=too-few-public-methods
self.debuginfo = (linemap, text_program) if build_debuginfo else None
self.public_labels = public_labels
@classmethod
def from_file(cls, filename: str, **kwargs) -> "Program":
"""Assemble a PIO program in a file"""
@ -557,7 +571,7 @@ class Program: # pylint: disable=too-few-public-methods
print()
def assemble(program_text: str) -> MutableSequence[int]:
def assemble(program_text: str) -> array.array:
"""Converts pioasm text to encoded instruction bytes
In new code, prefer to use the `Program` class so that the extra arguments

63
tests/test_label.py Normal file
View file

@ -0,0 +1,63 @@
# SPDX-FileCopyrightText: 2024 Jeff Epler for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Tests out
"""
from pytest_helpers import assert_assembly_fails
import adafruit_pioasm
def test_label() -> None:
source = [
" jmp label1",
"label1:",
" jmp label2",
"public label2:",
" nop",
]
program = adafruit_pioasm.Program("\n".join(source))
assert program.public_labels == {"label2": 2}
# Test each combination of public/privagte label duplication
source = [
"label1:\n",
"nop\n",
"public label1:\n",
"nop\n",
]
assert_assembly_fails(
"\n".join(source), match="Duplicate label", errtype=SyntaxError
)
source = [
"label1:\n",
" nop\n",
"label1:\n",
" nop\n",
]
assert_assembly_fails(
"\n".join(source), match="Duplicate label", errtype=SyntaxError
)
source = [
"public label1:\n",
" nop\n",
"label1:\n",
" nop\n",
]
assert_assembly_fails(
"\n".join(source), match="Duplicate label", errtype=SyntaxError
)
source = [
"public label1:\n",
" nop\n",
"public label1:\n",
" nop\n",
]
assert_assembly_fails(
"\n".join(source), match="Duplicate label", errtype=SyntaxError
)