[comp.sys.sun] sun4

corey@blake.acs.washington.edu (Corey Satten) (10/11/89)

===============================================================================
	Details of varargs with the SUN4 (SPARC) compiler revealed
		    October 6, 1989 / SUNOS 4.0.3c
===============================================================================

Forgive me if this is common knowledge -- I just discovered it while
trying to get RCS to work on a sparc.  (RCS re-defines fprintf).

The SUN4 C compiler does not normally pass arguments to subroutines in the
stack frame, instead it uses the overlapping registers in the sliding
register window frame.

If you have a subroutine with a variable number of arguments, for
portability, you are supposed to use the standard macros defined in
/usr/include/varargs.h to access the argument list.  If you do so, your
program will work.  If you don't, it won't.  However, if you look at the
actual varargs macros, you see that they really don't do anything
differently than you would have if you hadn't used them.  Hmm, you wonder,
and look more closely.

There is this one line in /usr/include/varargs.h which does nothing but
rename the va_alist variable (which you are supposed to use as the
parameter name in your variable argument subroutine) to
"__builtin_va_alist".  Could it be that the compiler checks for that
particular variable name and does something different?!!

Running strings on /lib/ccom and grepping for "__builtin_va_alist" finds
it!  Furthermore, simply renaming your parameter name to that symbol and
doing your varargs the old fashioned (non-portable) way confirms it.  Use
that special variable and your program works -- change the spelling and it
doesn't.  Examining the generated code in both cases reveals that the
arguments are copied into the stack frame in the case that works.

What other such variables lurk in the compiler?  Running strings and
grepping for "__" shows up "__builtin_alloca" too.  I anticipate being
bitten by that one too someday, but now I'm expecting it.

Hope this saves someone some time.

Corey Satten
Networks and Distributed Computing
University of Washington