From fac21e232706daf3944d2d0b0c190972d9e8e3d8 Mon Sep 17 00:00:00 2001 From: Amin Abdulrahman Date: Tue, 15 Oct 2024 15:15:20 +0200 Subject: [PATCH] Add Loop parser + examples for armv7m --- example.py | 35 ++++++++ examples/naive/armv7m/loop_cmp.s | 10 +++ examples/naive/armv7m/loop_subs.s | 4 + slothy/targets/arm_v7m/arch_v7m.py | 129 ++++++++++------------------- 4 files changed, 91 insertions(+), 87 deletions(-) create mode 100644 examples/naive/armv7m/loop_cmp.s create mode 100644 examples/naive/armv7m/loop_subs.s diff --git a/example.py b/example.py index d7dbd347..1240e9e3 100644 --- a/example.py +++ b/example.py @@ -293,6 +293,39 @@ def __init__(self, var="", arch=AArch64_Neon, target=Target_CortexA55): def core(self,slothy): slothy.config.variable_size=True slothy.optimize_loop("start") + +class Armv7mLoopSubs(Example): + def __init__(self, var="", arch=Arch_Armv7M, target=Target_CortexM7): + name = "loop_subs" + infile = name + + if var != "": + name += f"_{var}" + infile += f"_{var}" + name += f"_{target_label_dict[target]}" + + super().__init__(infile, name, rename=True, arch=arch, target=target) + + def core(self,slothy): + slothy.config.variable_size=True + slothy.optimize_loop("start") + +class Armv7mLoopCmp(Example): + def __init__(self, var="", arch=Arch_Armv7M, target=Target_CortexM7): + name = "loop_cmp" + infile = name + + if var != "": + name += f"_{var}" + infile += f"_{var}" + name += f"_{target_label_dict[target]}" + + super().__init__(infile, name, rename=True, arch=arch, target=target) + + def core(self,slothy): + slothy.config.variable_size=True + slothy.config.outputs = ["r6"] + slothy.optimize_loop("start") class CRT(Example): def __init__(self): @@ -2106,6 +2139,8 @@ def main(): # Loop examples AArch64LoopSubs(), LoopLe(), + Armv7mLoopSubs(), + Armv7mLoopCmp(), CRT(), diff --git a/examples/naive/armv7m/loop_cmp.s b/examples/naive/armv7m/loop_cmp.s new file mode 100644 index 00000000..be8d09a7 --- /dev/null +++ b/examples/naive/armv7m/loop_cmp.s @@ -0,0 +1,10 @@ +/* For example, r5 represents an address where we will stop iterating and r6 is +the actual pointer which is incremented inside the loop. */ + +mov.w r6, #0 +add.w r5, r6, #64 + +start: + add r6, r6, #4 + cmp.w r6, r5 + bne.w start \ No newline at end of file diff --git a/examples/naive/armv7m/loop_subs.s b/examples/naive/armv7m/loop_subs.s new file mode 100644 index 00000000..5dd05c9c --- /dev/null +++ b/examples/naive/armv7m/loop_subs.s @@ -0,0 +1,4 @@ +movw r5, #16 +start: + subs.w r5, #1 + bne.w start \ No newline at end of file diff --git a/slothy/targets/arm_v7m/arch_v7m.py b/slothy/targets/arm_v7m/arch_v7m.py index 1dfc4ae6..1abc41b1 100644 --- a/slothy/targets/arm_v7m/arch_v7m.py +++ b/slothy/targets/arm_v7m/arch_v7m.py @@ -5,7 +5,7 @@ from enum import Enum from functools import cache -from slothy.helper import SourceLine +from slothy.helper import SourceLine, Loop from sympy import simplify llvm_mca_arch = "arm" # TODO @@ -126,16 +126,13 @@ def unconditional(lbl): yield f"b {lbl}" -class Loop: - """Helper functions for parsing and writing simple loops in armv7m - - TODO: Generalize; current implementation too specific about shape of loop""" - - def __init__(self, lbl_start="1", lbl_end="2", loop_init="lr"): - self.lbl_start = lbl_start - self.lbl_end = lbl_end - self.loop_init = loop_init - +class CmpLoop(Loop): + def __init__(self, lbl="lbl", lbl_start="1", lbl_end="2", loop_init="lr") -> None: + super().__init__(lbl_start=lbl_start, lbl_end=lbl_end, loop_init=loop_init) + self.lbl_regex = r"^\s*(?P