Stack allocation on amd64

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

 



Hello,

Consider the following program:

void toto(int *tab);

void foo1(void)
{
	int tab[N];
	toto(tab);
}

I checked how much space gcc allocates on the stack for various values of N:

for ((N=1; N<=12; ++N)); do
  echo -n N = $N
  gcc-7 -O3 -march=haswell -Wall -S -fno-stack-protector -D N=$N alloc.c
  grep 'subq' alloc.s
done

N = 1	subq	$24, %rsp
N = 2	subq	$24, %rsp
N = 3	subq	$24, %rsp
N = 4	subq	$24, %rsp
N = 5	subq	$40, %rsp
N = 6	subq	$40, %rsp
N = 7	subq	$40, %rsp
N = 8	subq	$40, %rsp
N = 9	subq	$56, %rsp
N = 10	subq	$56, %rsp
N = 11	subq	$56, %rsp
N = 12	subq	$56, %rsp


The call instruction will push the return address (8 bytes)
on the stack, therefore gcc is allocating
{1,2,3,4}    = 24 + 8 = 32
{5,6,7,8}    = 40 + 8 = 48
{9,10,11,12} = 56 + 8 = 64

16-byte alignment, because SIMD, right?

However, it seems gcc could allocate less in some cases:

{1,2}      =  8 + 8 = 16
{3,4,5,6}  = 24 + 8 = 32
{7,8,9,10} = 40 + 8 = 48

What am I missing?

For example, why is gcc allocating 40 bytes for N=5 and N=6?

Hmmm... reading up on https://wiki.osdev.org/System_V_ABI I see that
"The stack is 16-byte aligned just before the call instruction is called."

IIUC, it's the callee's responsibility to align the stack.
I suppose that doesn't change the reasoning above, though.

Regards.



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux