Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that generates bnn* branches in cdm8e #69

Merged
merged 2 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions cocas/assembler/targets/cdm8e/branches.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from dataclasses import dataclass


@dataclass
class BranchCode:
condition: list[str]
code: int
inverse: list[str]
inv_code: int


branch_codes: list[BranchCode] = [BranchCode(['eq', 'z'], 0, ['ne', 'nz', 'neq'], 1),
BranchCode(['hs', 'cs', 'nlo', 'ncc'], 2, ['lo', 'cc', 'nhs', 'ncs'], 3),
BranchCode(['mi', 'npl'], 4, ['pl', 'nmi'], 5),
BranchCode(['vs', 'nvc'], 6, ['vc', 'nvs'], 7),
BranchCode(['hi', 'nls'], 8, ['ls', 'nhi'], 9),
BranchCode(['ge', 'nlt'], 10, ['lt', 'nge'], 11),
BranchCode(['gt', 'nle'], 12, ['le', 'ngt'], 13),
BranchCode(['r', 'anything', 'true'], 14, ['false', 'nanything'], 15)]


def check_inverse_branch(cond: str, inverse=False):
for pair in branch_codes:
if cond in pair.condition:
return pair.condition[0] if not inverse else pair.inverse[0]
elif cond in pair.inverse:
return pair.inverse[0] if not inverse else pair.condition[0]
else:
return None
6 changes: 4 additions & 2 deletions cocas/assembler/targets/cdm8e/code_segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from ...ast_nodes import LabelNode, RelocatableExpressionNode, TemplateFieldNode
from ...exceptions import AssemblerException, AssemblerExceptionTag
from .. import ICodeSegment, IVaryingLengthSegment
from .simple_instructions import simple_instructions
from .branches import check_inverse_branch
from .instruction_codes import simple_instructions

TAG = AssemblerExceptionTag.ASM

Expand Down Expand Up @@ -187,8 +188,9 @@ def fill(self, object_record: "ObjectSectionRecord", section: "Section", labels:
if mnemonic not in simple_instructions['branch']:
_error(self, f'Invalid branch mnemonic: {mnemonic}')
if self.is_expanded:

branch_opcode = simple_instructions['branch'][
f'bn{self.branch_mnemonic}']
f'b{check_inverse_branch(self.branch_mnemonic, inverse=True)}']
jmp_opcode = simple_instructions['long']['jmp']
object_record.data += bytearray([branch_opcode, 4, jmp_opcode])
LongExpressionSegment(self.expr).fill(object_record, section, labels, templates)
Expand Down
15 changes: 10 additions & 5 deletions cocas/assembler/targets/cdm8e/target_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

import bitstruct

from cocas.object_module import CodeLocation

from ...ast_nodes import InstructionNode, LabelNode, RegisterNode, RelocatableExpressionNode
from ...exceptions import AssemblerException, AssemblerExceptionTag, CdmTempException
from .. import ICodeSegment
from .branches import check_inverse_branch
from .code_segments import (
BytesSegment,
ConstExpressionSegment,
Expand All @@ -13,7 +16,7 @@
OffsetExpressionSegment,
ShortExpressionSegment,
)
from .simple_instructions import simple_instructions
from .instruction_codes import simple_instructions


def assert_args(args, *types, single_type=False):
Expand Down Expand Up @@ -58,12 +61,14 @@ def finish(temp_storage: dict):
raise CdmTempException("Expected restore statement")


def make_branch_instruction(location, branch_mnemonic: str, label_name: str, inverse: bool) \
def make_branch_instruction(location: CodeLocation, branch_mnemonic: str, label_name: str, inverse: bool) \
-> list[ICodeSegment]:
arg2 = RelocatableExpressionNode(None, [LabelNode(label_name)], [], 0)
if inverse:
branch_mnemonic = 'n' + branch_mnemonic
seg = GotoSegment(branch_mnemonic, arg2)
proper_mnemonic = check_inverse_branch(branch_mnemonic, inverse)
if proper_mnemonic is None:
raise AssemblerException(AssemblerExceptionTag.ASM, location.file, location.line,
f"Invalid branch mnemonic: {branch_mnemonic}")
seg = GotoSegment(proper_mnemonic, arg2)
seg.location = location
return [seg]

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "cdm-devkit"
version = "0.2.0.post0"
version = "0.2.0.post1"
description = "Developer tools for CdM processors"
authors = [
"CdM Processors",
Expand Down
Loading