assemblygnu-assemblerarmv8

How can I expand the macro in ARM assembly?


.macro f x,y
    ...
.endm
....
    f x10,x11

How can I expand the usage of macro f x10,x11? gcc -E doesn't seem to help, FWIW.


Solution

  • Assuming from your mention of gcc that you are using the GNU assembler, you can generate a listing file containing .macro expansions. It's not mentioned in the manual but does show up in as --help:

      -a[sub-option...]   turn on listings
                              Sub-options [default hls]:
                              c      omit false conditionals
                              d      omit debugging directives
                              g      include general info
                              h      include high-level source
                              l      include assembly
                              m      include macro expansions
                              n      omit forms processing
                              s      include symbols
                              =FILE  list to FILE (must be last sub-option)
    

    It would seem like -am would be what you want, but in fact that produces an empty listing file. However, -alm does work.

    So if your source code is

        .macro square dst, src
        mul \dst, \src, \src
        .endm
        
        square x0, x1
        
    

    you can do

    as -alm=foo.lst foo.s
    

    and get the following in foo.lst:

    AARCH64 GAS  foo.s          page 1
    
    
       1                        .macro square dst, src
       2                        mul \dst, \src, \src
       3                        .endm
       4                        
       5                        square x0, x1
       5 0000 207C019B  >  mul x0,x1,x1
       6                        
    

    The macro expansion is shown on line 5.


    Regarding your comment about gcc -E, it helps to understand that the GNU toolchain provides two entirely separate and independent ways to use macros in assembly: