Exception Handlers: Save/Restore r4 to r7 with SYSTEM.LDREG

General discussions about working with the Astrobe IDE and programming ARM Cortex-M0, M3, M4 and M7 microcontrollers.
Post Reply
gray
Posts: 121
Joined: Tue Feb 12, 2019 2:59 am
Location: Mauritius

Exception Handlers: Save/Restore r4 to r7 with SYSTEM.LDREG

Post by gray » Fri May 03, 2024 1:27 am

Compiling this exception handler

Code: Select all

  PROCEDURE h[0];
  BEGIN
    SYSTEM.LDREG(4, 13)
  END h;
generates this code

Code: Select all

  PROCEDURE h[0];
  BEGIN
.  1584   0630H  0B500H          push     { lr }
    SYSTEM.LDREG(4, 13)
  END h;
.  1586   0632H  0200DH          movs     r0,#13
.  1588   0634H  04604H          mov      r4,r0
.  1590   0636H  0BD00H          pop      { pc }
That is, register r4 is not saved and restored, even though it's mutated, as it would be if r4 were allocated by the compiler.

I guess there are two basic views on this:

1) If you do crazy stuff like this, you're on your own. Save and restore yourself.
2) Exception handlers are more likely to use this kind of "close to the metal" operations, so the compiler could help here.

I don't know what 2) would mean for the compiler design and implementation. Maybe not worth the effort. I have come across this question in the context of evaluating and implementing trap handlers, in particular passing parameters to them.

Thoughts?


gray
Posts: 121
Joined: Tue Feb 12, 2019 2:59 am
Location: Mauritius

Re: Exception Handlers: Save/Restore r4 to r7 with SYSTEM.LDREG

Post by gray » Fri May 03, 2024 3:07 pm

Oops! Seems I was so impressed by the optimisation that I had blanked out the last part. :)

Post Reply