[comp.sys.mips] Can some one explain this compiler output?

ham@Neon.Stanford.EDU (Peter R. Ham) (11/12/89)

Question: Why does cc allocate 24 bytes of stack for a non-leaf procedure
with no local variables when calling another procedure that accepts no
arguments?

Given this C code:

extern void bar();

void
foo()
{
  bar();
}

cc produces:

	.verstamp	1 31
	.text	
	.align	2
	.file	2 "test.c"
	.globl	foo
	.loc	2 6
 #   1	
 #   2	extern void bar();
 #   3	
 #   4	void
 #   5	foo()
 #   6	{
	.ent	foo 2
foo:
	.option	O2
	subu	$sp, 24
	sw	$31, 20($sp)
	.mask	0x80000000, -4
	.frame	$sp, 24, $31
	.loc	2 7
 #   7	  bar();
	jal	bar
	.loc	2 8
 #   8	}
	lw	$31, 20($sp)
	addu	$sp, 24
	j	$31
	.end	foo

--
Peter Ham			PO Box 3430	(h)(415) 322-4390
MS Computer Science Student	Stanford, CA	ham@cs.stanford.edu
Stanford University 		94309		(o)(415) 723-2067

fred@mips.COM (Fred Chow) (11/14/89)

In article <HAM.89Nov11161238@Neon.Stanford.EDU> ham@Neon.Stanford.EDU (Peter R. Ham) writes:
>Question: Why does cc allocate 24 bytes of stack for a non-leaf procedure
>with no local variables when calling another procedure that accepts no
>arguments?
>
>Given this C code:
>
>extern void bar();
>
>void
>foo()
>{
>  bar();
>}

	For a non-leaf, 4 words are always allocated as argument build area
for callees.  This is done even if the callee has 0 parameter because we
always pass the first four parameters in registers 4 thru 7.  And "bar" can
be a function with variable number of parameters (vararg).  Such functions 
will always home registers 4 thru 7 to the incoming argument build area and 
then access the parameters thru a pointer.  So the space for 4 parameters
is always allocated.  As you can see in the code, another word is allocated
to store the return address in r31.  Altogether 6 words are allocated
because the stack pointer always points to a double word boundary.

---------
Fred Chow (fred@mips.com)