Example:
|
So when you allocate 4 bytes for one additional local variable, you must add 4 more bytes for padding (to keep the stack pointer divisible by 8) |
void myFunc()
{
int a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11;
....
}
|
So we must add 4 more bytes for padding:
myFunc: save %sp, -(92 + 16 + 4), %sp ... rest of the function body ret restore |
%l0 - local variable a0 %l1 - local variable a1 %l2 - local variable a2 %l3 - local variable a3 %l4 - local variable a4 %l5 - local variable a5 %l6 - local variable a6 %l7 - local variable a7 |
The following picture shows the manner in whcih SPARC store the local variables:
MEMORY
%sp --------> +-----------------------+
| save %l0 | 8 local registers
%sp + 4 ----> +-----------------------+
| save %l1 | (parameters to subroutine)
%sp + 8 ----> +-----------------------+
| ......... |
| ......... |
| ......... |
%sp + 32 ---> +-----------------------+
| save %i0 | 8 input registers
%sp + 36 ---> +-----------------------+
| save %i1 | (local variables of subroutine)
%sp + 40 ---> +-----------------------+
| ......... |
| ......... |
| ......... |
%sp + 64 ---> +-----------------------+
| structure pointer !! | for a function that returns struct
%sp + 68 ---> +-----------------------+
| save %o0 | 6 output registers: o0 - o5
%sp + 72 ---> +-----------------------+
| save %o1 |
%sp + 76 ---> +-----------------------+
| ......... |
| ......... |
%sp + 88 ---> +-----------------------+
| save %o5 |
%sp + 92 ---> +-----------------------+
| padding |
%sp + 96 ---> +-----------------------+ <--- %fp - 16
| a11 |
%sp + 100 --> +-----------------------+ <--- %fp - 12
| a10 |
%sp + 104 --> +-----------------------+ <--- %fp - 8
| a9 |
%sp + 108 --> +-----------------------+ <--- %fp - 4
| a8 |
%sp + 112 --> +-----------------------+ <--- %fp
|
a8 ld [%fp - 4], ... a9 ld [%fp - 8], ... a10 ld [%fp - 12], ... a11 ld [%fp - 16], ... |
a8 st ..., [%fp - 4] a9 st ..., [%fp - 8] a10 st ..., [%fp - 12] a11 st ..., [%fp - 16] |
Compile it with:
Run it inside EGTAPI. Scroll the stack area down to the bottom and you will see how C uses local variables.
The C compiler seems to allocate 12 local variables on the stack and does not use any local register at all... Not very optimal, but still works...