Bug 31752 - gas: Support \+ in .rept/.irp/.irpc directives
Summary: gas: Support \+ in .rept/.irp/.irpc directives
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: gas (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-05-17 17:50 UTC by Fangrui Song
Modified: 2024-06-10 07:08 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2024-05-17 17:50:59 UTC
(https://sourceware.org/pipermail/binutils/2024-May/134009.html RFC: Maintaining a per-macro invocation count)

gas recently introduced \+ for per-macro invocation counts within .macro/.endm directives.

Building on discussions at https://sourceware.org/pipermail/binutils/2024-May/134089.html , extending the feature to loop directives would be beneficial.
This would allow us to replace .byte 0, 1, 2 with:

  .rept 3
    .byte \+
  .endr

(.irpc i,0123456789   \i   .endr) works for a loop when the count is <= 10 but is cumbersome for larger loop counts.

For nested loops, \+ could indicate the outermost loop for implementation convenience.
Such constructs are rare and we can rely on clear documentation.

  .rept 2
    .rept 2
      .byte \+
    .endr
  .endr

  # 0 0 1 1
Comment 1 Fangrui Song 2024-05-17 18:07:32 UTC
In the absence of the feature, there are a few ways, but none achieves great efficiency or convenience.

For example, we can define a macro that expands to a sequence of strings: 0, (0+1), ((0+1)+1), (((0+1)+1)+1), ...

% cat a.s
.data
.macro iota n, i=0
.if \n-\i
.byte \i
iota \n, "(\i+1)"
.endif
.endm

iota 16
% as a.s -o a.o
% readelf -x .data a.o

Hex dump of section '.data':
  0x00000000 00010203 04050607 08090a0b 0c0d0e0f ................

---

% in altmacro allows:

% cat a.s
.data
.altmacro
.macro iota n, i=0
.if \n-\i
.byte \i
iota \n, %(\i+1)
.endif
.endm

iota 16
% as a.s -o a.o
% readelf -x .data a.o

Hex dump of section '.data':
  0x00000000 00010203 04050607 08090a0b 0c0d0e0f ................
Comment 2 Sourceware Commits 2024-05-24 10:23:41 UTC
The master branch has been updated by Jan Beulich <jbeulich@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f29ebbe3be5c2a4400ad8b89a8f399ead4448148

commit f29ebbe3be5c2a4400ad8b89a8f399ead4448148
Author: Jan Beulich <jbeulich@suse.com>
Date:   Fri May 24 12:23:22 2024 +0200

    gas: extend \+ support to .irp / .irpc
    
    PR gas/31752
    
    These are effectively macro-like, without any separate macro definition.
    They already support \@, so they would better also support \+. This
    allows, where desired, to get away without maintaining an explicit count
    variable in source code.
    
    With this the recently introduced testcase doesn't need any xfails
    anymore.
Comment 3 Sourceware Commits 2024-06-10 07:06:59 UTC
The master branch has been updated by Jan Beulich <jbeulich@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=1e3c814459d83247707f8c9840ac660726cfaae0

commit 1e3c814459d83247707f8c9840ac660726cfaae0
Author: Jan Beulich <jbeulich@suse.com>
Date:   Mon Jun 10 09:06:37 2024 +0200

    gas: extend \+ support to .rept
    
    PR gas/31752
    
    While not quite as macro-like as .irp / .irpc, this perhaps benefits from
    supporting \+ even more than those: It allows, where desired, to get away
    without maintaining an explicit count variable in source code.
    
    Keep .rep (and custom per-arch uses of s_rept() / do_repeat()) behavior
    unaltered.
Comment 4 Jan Beulich 2024-06-10 07:08:35 UTC
Should be all set now.