Search code examples
cinterruptriscv

is there any use of __attribute__ ((interrupt)) for riscv compilers?


we can read here that the interrupt attribute keyword is use for ARM, AVR, CR16, Epiphany, M32C, M32R/D, m68k, MeP, MIPS, RL78, RX and Xstormy16.

does it have any impact on riscv compilation using riscv32-***-elf-gcc compilers?


Solution

  • There is a separate page for RISC-V which claims it works. You can find it here. Also you could probably verify it by compiling code with and without the attribute set.

    I don't have riscv32 toolchain installed, but i managed to verify it using the riscv64 toolchain. You should reproduce the same steps using the riscv32 toolchain to make sure it works.

    Using a simple test.c file:

    __attribute__((interrupt))
    void test() {}
    

    Compiling it with riscv64-linux-gnu-gcc -c -o test.o test.c and disassembling with riscv64-linux-gnu-objdump -D -j.text test.o we can see it generates mret instruction at the end of the function:

       0:   1141                    addi    sp,sp,-16
       2:   e422                    sd  s0,8(sp)
       4:   0800                    addi    s0,sp,16
       6:   0001                    nop
       8:   6422                    ld  s0,8(sp)
       a:   0141                    addi    sp,sp,16
       c:   30200073                mret
    

    After removing the interrupt attribute the instruction changes to regular ret. According to this SO answer this seems like correct behaviour.