Skip to content

Commit

Permalink
Add support for .equ in asm
Browse files Browse the repository at this point in the history
  • Loading branch information
mkannwischer committed Nov 6, 2024
1 parent c077093 commit 3ecb619
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions slothy/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,14 @@ def _extract_core(source, lbl_start=None, lbl_end=None):
class AsmAllocation():
"""Helper for tracking register aliases via .req and .unreq"""

# TODO: This is conceptionally different and should be
# handled in its own class.
_REGEXP_EQU_TXT = r"\s*\.equ\s+(?P<key>[A-Za-z0-9\_]+)\s*,\s*(?P<val>[A-Za-z0-9()*/+-]+)"

_REGEXP_REQ_TXT = r"\s*(?P<alias>\w+)\s+\.req\s+(?P<reg>\w+)"
_REGEXP_UNREQ_TXT = r"\s*\.unreq\s+(?P<alias>\w+)"

_REGEXP_EQU = re.compile(_REGEXP_EQU_TXT)
_REGEXP_REQ = re.compile(_REGEXP_REQ_TXT)
_REGEXP_UNREQ = re.compile(_REGEXP_UNREQ_TXT)

Expand Down Expand Up @@ -625,6 +630,12 @@ def check_allocation(line):
reg = p.group("reg")
return alias, reg

p = AsmAllocation._REGEXP_EQU.match(line.text)
if p is not None:
key = p.group("key")
val = p.group("val")
return key, val

return None

@staticmethod
Expand Down Expand Up @@ -683,10 +694,17 @@ def parse_allocs(src):
def unfold_all_aliases(aliases, src):
"""Unfold aliases in assembly source"""
def _apply_single_alias_to_line(alias_from, alias_to, src):
return re.sub(f"(\\W){alias_from}(\\W|\\Z)", f"\\1{alias_to}\\2", src)
res = re.sub(f"(\\W){alias_from}(\\W|\\Z)", f"\\g<1>{alias_to}\\2", src)
return res
def _apply_multiple_aliases_to_line(line):
for (alias_from, alias_to) in aliases.items():
line = _apply_single_alias_to_line(alias_from, alias_to, line)
do_again = True
while do_again:
do_again = False
for (alias_from, alias_to) in aliases.items():
line_new = _apply_single_alias_to_line(alias_from, alias_to, line)
if line_new != line:
do_again = True
line = line_new
return line
res = []
for line in src:
Expand Down

0 comments on commit 3ecb619

Please sign in to comment.