avr-libc-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-libc-dev] Single page stack suggestion


From: David Brown
Subject: [avr-libc-dev] Single page stack suggestion
Date: Wed, 11 Jul 2007 10:52:34 +0200
User-agent: Thunderbird 2.0.0.4 (Windows/20070604)

This is just an idea, inspired by my examination of assembly listing code recently.

When a stack frame is needed on the AVR, there is a substantial amount of code needed to safely decrement the stack pointer in the prologue (and increment it in the epilogue) - a total of 19 words overhead. The reason for this is that the stack pointer must be adjusted as a 16-bit value. But if the stack were always within one 256-byte page, which is probably the case in the majority of avr programs (certainly on small AVRs), then only the SP_L register needs to updated. Then the prologue and epilogue would be reduced to 4 and 3 instructions respectively.

I don't know if such a feature would be worth having - stack frames are not often needed, and it would have to be enabled with a compiler flag rather than being used by default.


An alternative change (more suited to bigger AVRs) could be made if it is safe to assume that the stack is given a whole number of pages, plus some extra for interrupt stack space. Then there would be no need to disable interrupts during the prologue and epilogue:

        in r28,__SP_L__
        in r29,__SP_H__
        sbiw r28,XX
        out __SP_H__,r29
        out __SP_L__,r28
        ...
        adiw r28,XX
        out __SP_L__,r28
        out __SP_H__,r29

The important thing to consider is what happens if an interrupt occurs in the middle of the change to the stack pointer registers. In the prologue, either the SP_H register remains the same (and thus the write to SP is effectively atomic), or it has decreased. For example, if the original stack pointer were at 0x1008 and the frame size is 16 bytes, the new stack pointer will be 0xfff8. If the interrupt comes in the middle, the stack pointer will be 0xff08 - given the assumption of enough stack space (the complete page plus enough for the interrupt), this is perfectly safe. In the epilogue, the order of the writes is reversed, so again the intermediary value is 0xff08.


mvh.,

David





reply via email to

[Prev in Thread] Current Thread [Next in Thread]