Page 1 of 1

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

Posted: Fri May 03, 2024 1:27 am
by gray
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?

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

Posted: Fri May 03, 2024 11:37 am
by cfbsoftware

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

Posted: Fri May 03, 2024 3:07 pm
by gray
Oops! Seems I was so impressed by the optimisation that I had blanked out the last part. :)