Question on registers backup behaviors

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi folks,

I see the Call-Used registers must be saved by the caller (by definition) in avr-libc doc (pag 73). As the caller does not know (does it?) which of these registers shall be needed by the invoked subroutine, it should (?) backup all of them? Is this correct? Cause I've never seen this in the produced assembly.
If not, how does the compiler tell which must be saved and which cannot?

I also have a second question regarding the Registers backup behavior: I've experienced that in at least 2 target architectures, the IRQ always backs up the Program Counter (in the stack). Is this a MUST practice or are there other arch cpus that do not behave this way?

Thanks in advance,

RM

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

If not, how does the compiler tell which must be saved and which cannot?

You are not *required* to save them, unless you still want to retain their value after the call. If you aren't using them in your own function, you don't need to save them since you don't care if they get trashed.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I also have a second question regarding the Registers backup behavior: I've experienced that in at least 2 target architectures, the IRQ always backs up the Program Counter (in the stack). Is this a MUST practice or are there other arch cpus that do not behave this way?

As an interrupt can occur at any PC value on any CPU architecture how could the core restore execution after the interrupt if PC were not saved?

On the other hand many RISC architectures have some kind of "branch and link" operation used in place of CALL which does not put PC on the stack but into a link register (usually called LR). The return is simply made by moving LR back to PC. The value in that register is only committed to the stack by the programmer if this function then goes on to BL to a further function. ARM is such an architecture and I seem to remember MIPS has something similar. It's very efficient for compiler implementation as the majority of CALLs are only one level deep.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

riccardomanfrin wrote:
I see the Call-Used registers must be saved by the caller (by definition) in avr-libc doc (pag 73). As the caller does not know (does it?) which of these registers shall be needed by the invoked subroutine, it should (?) backup all of them? Is this correct? Cause I've never seen this in the produced assembly.
call-saved registers in avr-gcc are callee-saved, not caller-saved, i.e. the callee cares for the registers it clobbers.

A caller can assume that a value in a register survives calling the callee: Either the callee does not touch the register, or it has to save the value in its prologue and restore it in its epilogue. call-used registers, sometimes allso called call-clobbered, can be used without restoring. If a caller has to use a value around a call, it won't store it in a call-clobbered register. The caller has to used a callee-saved register, and because the caller itself is a callee, he has to save-restore that register.

For example,

void callee (char);

void caller (char c)
{
    callee (c);
    callee (c);
}

can be translated as:

caller:
    push r28
    /* prologue: function */
    mov r28,r24
    rcall callee
    mov r24,r28
    /* epilogue start */
    pop r28
    rjmp callee

c is passed in r24 and must be saved around the first call of callee because the second call of callee also needs c, but the first call clobbers r24.

avr-gcc choses r28 to backup r28 and has to save it in the prologue and restore it in the epilogue.

Quote:
I also have a second question regarding the Registers backup behavior: I've experienced that in at least 2 target architectures, the IRQ always backs up the Program Counter (in the stack). Is this a MUST practice or are there other arch cpus that do not behave this way?
There are architectures that don't save on the stack. For example some Infineon chips use a Context Save Area that cannot be accessed directly. And doesn't PowerPC save the address in a hard register?

avrfreaks does not support Opera. Profile inactive.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

SprinterSB wrote:
call-saved registers in avr-gcc are callee-saved, not caller-saved, i.e. the callee cares for the registers it clobbers.
A caller can assume that a value in a register survives calling the callee: Either the callee does not touch the register, or it has to save the value in its prologue and restore it in its epilogue.

Ok.. this is the definition of call-saved; if used they must be backed up prior using and restored prior returning (whithin the function that uses them).

SprinterSB wrote:
call-used registers, sometimes allso called call-clobbered, can be used without restoring. If a caller has to use a value around a call, it won't store it in a call-clobbered register. The caller has to used a callee-saved register, and because the caller itself is a callee, he has to save-restore that register.

.. and this is the definition of call-used: so this means that it is not about which call-used registers the invoked subroutine shall use, but rather which of them the caller is using (and hence needs to save).

I think I got the point (?).