Skip to content

Commit

Permalink
fixed bug with parsing labels starting with BYTE
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkamprath committed Dec 17, 2024
1 parent daa964c commit 5675692
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ Changes that are planned but not implemented yet:
* unknown labels
* Add named label scopes. This would allow a label to be defined in a specific scope that can be shared across files.
* Create a "align if needed" preprocessor directive paid that generates an `.align` directive if the bytecode in between the pair isn't naturally on the same page and can fit on the same page if aligned. An error would be benerated if the block of code can't fit on the same page regardless of alignment.
* Update the `#ifdef` and related preprocessor directives to include detection of labels and constants.

## [Unreleased]
* Upgrade python version requirements to 3.11
* Fixed a bug where embedded stringsd weren't properly parsed if they contained a newline character or there were multiple embedded strings per line
* Fixed a bug where parsing properly discriminate between labels starting with `BYTE` string and the `BYTEx()` operator.

## [0.4.2]
* Added support for The Minimal 64x4 Home Computer with an example and updated assembler functionality to support it.
Expand Down
2 changes: 1 addition & 1 deletion src/bespokeasm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
BESPOKEASM_VERSION_STR = '0.4.3b1'
BESPOKEASM_VERSION_STR = '0.4.3b2'

# if a cconfig file requires a certain bespoke ASM version, it should be at least this version.
BESPOKEASM_MIN_REQUIRED_STR = '0.3.0'
2 changes: 1 addition & 1 deletion src/bespokeasm/assembler/bytecode/generator/macro.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def generate_bytecode_parts(
if assembled_instruction is not None:
return assembled_instruction

sys.exit(f'ERROR: {line_id} - Instruction "{mnemonic}" has no valid operands configured.')
sys.exit(f'ERROR: {line_id} - Macro "{mnemonic}" has no valid operands configured.')

@classmethod
def generate_variant_bytecode_parts(
Expand Down
2 changes: 1 addition & 1 deletion src/bespokeasm/expression/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def _lexical_analysis(line_id: LineIdentifier, s: str) -> list[ExpressionNode]:
if part in TOKEN_MAPPINGS:
token_type = TOKEN_MAPPINGS[part]
token = ExpressionNode(token_type, value=part)
elif part.startswith('BYTE'):
elif re.match(r'^BYTE\d\(', part):
token = ExpressionNode(TokenType.T_BYTE, value=part)
elif is_string_numeric(part):
token = ExpressionNode(TokenType.T_NUM, value=parse_numeric_string(part))
Expand Down
19 changes: 19 additions & 0 deletions test/config_files/test_instruction_operands.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ operand_sets:
bytecode:
value: 3
size: 4
immediate_8bit:
# one byte is interpreted as an immediate value
operand_values:
int8:
type: numeric
argument:
size: 8
byte_align: true
endian: little
enums:
operand_values:
register_enum:
Expand Down Expand Up @@ -141,3 +150,13 @@ instructions:
byte_align: true
max: 128
min: -127
adi:
# Add immediate to A: A += imm
bytecode:
value: 0xa2
size: 8
operands:
count: 1
operand_sets:
list:
- immediate_8bit
14 changes: 10 additions & 4 deletions test/test_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ class TestExpression(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.label_values = GlobalLabelScope(set())
cls.label_values.set_label_value('value_1', 12, 1)
cls.label_values.set_label_value('the_8_ball', 8, 2)
cls.label_values.set_label_value('MixedCase', 2, 3)
cls.label_values.set_label_value('MAX_N', 20, 1)
line = LineIdentifier(0, 'setUpClass')
cls.label_values.set_label_value('value_1', 12, line)
cls.label_values.set_label_value('the_8_ball', 8, line)
cls.label_values.set_label_value('MixedCase', 2, line)
cls.label_values.set_label_value('MAX_N', 20, line)
cls.label_values.set_label_value('BYTECODE_LL0', 0x07, line)

def test_expression_parsing(self):
line_id = LineIdentifier(1212, 'test_expression_parsing')
Expand Down Expand Up @@ -59,6 +61,10 @@ def test_expression_parsing(self):
parse_expression(line_id, '(MAX_N + 3) % %101').get_value(TestExpression.label_values, line_id),
3, 'test handling of modulo: (MAX_N + 3) % %101 = 3'
)
self.assertEqual(
parse_expression(line_id, 'BYTECODE_LL0').get_value(TestExpression.label_values, line_id),
0x07, 'test handling of label with prefix: BYTECODE_LL0'
)

with self.assertRaises(SyntaxError, msg='only integer numeric values are supported'):
parse_expression(1212, '(value_1/5)/2.4').get_value(TestExpression.label_values, 100)
Expand Down
24 changes: 24 additions & 0 deletions test/test_instruction_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,30 @@ def test_operand_order(self):
t3.generate_bytes()
self.assertEqual(list(t3.get_bytes()), [0x81, 0x32, 0x10], 'instruction byte should match')

def test_operand_expression(self):
fp = pkg_resources.files(config_files).joinpath('test_instruction_operands.yaml')
isa_model = AssemblerModel(str(fp), 0)
memzone_mngr = MemoryZoneManager(
isa_model.address_size,
isa_model.default_origin,
isa_model.predefined_memory_zones,
)

lineid = LineIdentifier(42, 'test_operand_order')

t1 = InstructionLine.factory(
lineid,
'ADI var1w+0',
'comment',
isa_model,
memzone_mngr.global_zone,
memzone_mngr,
)

t1.label_scope = TestInstructionParsing.label_values
self.assertIsInstance(t1, InstructionLine)
self.assertEqual(t1.byte_size, 2, 'has 2 bytes')

def test_relative_address_operand(self):
fp = pkg_resources.files(config_files).joinpath('test_instruction_operands.yaml')
isa_model = AssemblerModel(str(fp), 0)
Expand Down

0 comments on commit 5675692

Please sign in to comment.