0

Is there a way to use the .irp directive with a range of values rather than a list?

For example, when saving the context in RISC-V, one needs to store 31 registers to the context frame, like in

sw x1, 0*4(sp)
sw x2, 1*4(sp)
...
sw x31, 30*4(sp)

This can be replaced by .irp syntax:

.irp num 1,2,3,4,<more numbers>,31
 sw x\num, (num-1)*4(sp)
.endr

Is there any way to avoid providing the long list of explicit numbers and have something like range with start and end number instead?

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
  • @PeterCordes Yep. Just found this question: https://stackoverflow.com/questions/30764210/calling-a-table-of-function-using-repeat-blocks-or-macros-in-gnu-assembly `.altmacro` and recursive macro is the way to go. – Eugene Sh. Feb 06 '23 at 20:34
  • GAS can use recursive macros, or a `.rept` loop with `.set i, i+1` – Peter Cordes Feb 06 '23 at 20:34
  • oh.. recursive .rept is interesting too – Eugene Sh. Feb 06 '23 at 20:35
  • `.rept` isn't *recursive*, it's just iterating like NASM `%rep`. Apparently `.set` inside it gets re-evaluated every iteration so you can use it like NASM `%assign`. – Peter Cordes Feb 06 '23 at 20:36
  • @PeterCordes I can't get the `set` symbol to evaluate with `%` (as in `%i`) for some reason. Recursive macro works though – Eugene Sh. Feb 06 '23 at 20:59
  • Oh right, you need to get it as a string to paste with `x` as the register name, not *just* as the addressing-mode offset. `sw x1, i*4(sp)` / `.set i, i+1` works for me with `clang -target riscv32`. But I don't know how to stringify `i` in that case, in a `.rept` not a macro. – Peter Cordes Feb 06 '23 at 21:07

0 Answers0