Alogorithm used by gcc to make room for local variables in procedure prologs

Alogorithm used by gcc to make room for local variables in procedure prologs

Post by Andrew Giert » Sun, 04 Nov 2001 07:33:49



 Don> gcc allocates the following stack space for one local variable
 Don> with a hard coded length that varies from 0 to 8:

 Don> length        bytes
 Don> of local     reserved
 Don> variable      by gcc
 Don> --------    ---------

 Don>                 0
 Don>    1            4
 Don>    2            4
 Don>    3           24
 Don>    4            4
 Don>    5           24
 Don>    6           24
 Don>    7           24
 Don>    8            8

without seeing the actual code of the function, and the compiler options
used, there is no way to explain this. But consider that if optimisation
is enabled, gcc will use registers whenever possible, and the reserved
stack space may also be needed for temporary values, so a lot depends on
what the body of the function actually does (and what the actual type of
the variable is).

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>

 
 
 

Alogorithm used by gcc to make room for local variables in procedure prologs

Post by Andrew Giert » Sun, 04 Nov 2001 11:13:24



 >> without seeing the actual code of the function, and the compiler
 >> options used, there is no way to explain this.

 Don> I concur that gcc may allocate registers to hold local
 Don> variables.  Do you know of a compiler option to prevent that?

no, but variables declared as 'volatile' will always be allocated
stack space, as will variables whose addresses are taken.

 Don> -O0 should turn off optimization, yes?

yes

 Don> .ident  "GCC: (GNU) 2.96 20000731 (Red Hat Linux 7.1 2.96-81)"

ah. There is no gcc version 2.96, Red Hat actually took a snapshot of
a development branch of gcc and released it (a move which did not
impress the gcc developers much, see http://gcc.gnu.org/gcc-2.96.html).
gcc 2.95.2 compiles all of your functions other than function0 with
"subl $24,%esp"; this is partly a stack alignment issue (default stack
alignment is 16 bytes, and 8 bytes are already used by the return
address and frame pointer), though I'm not sure why it uses 24 rather
than 8.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>