1

I have defined 2 functions:

void func1(uint32_t val){
    asm("csrsi mstatus, 0x8");
    asm("csrsi mstatus, 0x8");
}

void func2(uint32_t val){
    asm("csrw mie, %0" : "=r"(val));
    asm("csrsi mstatus, 0x8");
}

If I compile them with -O0 in gcc, I can still see those instructions in objdump

000004a4 <func1>:
 4a4:   ff810113            addi    sp,sp,-8
 4a8:   00812223            sw  s0,4(sp)
 4ac:   00810413            addi    s0,sp,8
 4b0:   fea42c23            sw  a0,-8(s0)
 4b4:   30046073            csrsi   mstatus,8           <--
 4b8:   30046073            csrsi   mstatus,8           <--
 4bc:   00000013            nop
 4c0:   00412403            lw  s0,4(sp)
 4c4:   00810113            addi    sp,sp,8
 4c8:   00008067            ret

000004cc <func2>:
 4cc:   ff810113            addi    sp,sp,-8
 4d0:   00812223            sw  s0,4(sp)
 4d4:   00810413            addi    s0,sp,8
 4d8:   fea42c23            sw  a0,-8(s0)
 4dc:   30479073            csrw    mie,a5              <--
 4e0:   fef42c23            sw  a5,-8(s0)
 4e4:   30046073            csrsi   mstatus,8           <--
 4e8:   00000013            nop
 4ec:   00412403            lw  s0,4(sp)
 4f0:   00810113            addi    sp,sp,8
 4f4:   00008067            ret

However, if I use -O3, the csrw mie instruction gets optimized out:

000002f4 <func1>:
 2f4:   30046073            csrsi   mstatus,8
 2f8:   30046073            csrsi   mstatus,8
 2fc:   00008067            ret

00000300 <func2>:
 300:   30046073            csrsi   mstatus,8
 304:   00008067            ret

I think I should add something to the clobber list, but which register name should I use? For example, I tried:

asm("csrw mie, %0" : "=r"(val) : : "mie");

and get the following error:

src/filename.c:70:5: error: unknown register name 'mie' in 'asm'
   70 |     asm("csrw mie, %0" : "=r"(val) : : "mie");
      |     ^~~
Eric Stdlib
  • 1,292
  • 1
  • 18
  • 32

0 Answers0