diff --git a/slothy/helper.py b/slothy/helper.py index 0aebad98..5ef6c50c 100644 --- a/slothy/helper.py +++ b/slothy/helper.py @@ -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[A-Za-z0-9\_]+)\s*,\s*(?P[A-Za-z0-9()*/+-]+)" + _REGEXP_REQ_TXT = r"\s*(?P\w+)\s+\.req\s+(?P\w+)" _REGEXP_UNREQ_TXT = r"\s*\.unreq\s+(?P\w+)" + _REGEXP_EQU = re.compile(_REGEXP_EQU_TXT) _REGEXP_REQ = re.compile(_REGEXP_REQ_TXT) _REGEXP_UNREQ = re.compile(_REGEXP_UNREQ_TXT) @@ -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 @@ -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: