[comp.sys.sun] HELP! What's wrong with my program?

ye@henri.ucsb.edu (Hong Ye) (03/19/91)

Following piece of code looks to be perfectly innocent. It runs perfectly
on Sun-3 indeed. But it produces weird results on Sun-4 and DEC2100. I
guess the compiler on Sun-4 or DEC uses a different calling convention.

Could anybody tell me what I am doing wrong? Please reply through mail.

#include <stdio.h>

#define	argA	0xAAAAAAAA
#define	argB	0xBBBBBBBB
#define	argC	0xCCCCCCCC

main() {
	printf("Original:\n");
	printf("\t%08X\n\t%08X\n\t%08X\n",argA,argB,argC);
	fake_printf(argA,argB,argC,0);
}	/* main */

fake_printf(args)
int args;
{
	internal_printf(&args);
}	/* fake_printf */

internal_printf(args)
int *args;
{
	printf("Inside Printf:\n");
	while (*args) {
	    printf("\t%08X\n",*args);
	    args++;
	}
}	/* internal_printf */

basile@soleil.cea.fr (Basile STARYNKEVITCH) (03/28/91)

Your code is implicitly built on the assumption that arguments are passed
on a stack (such as Mc680x0 machines, eg. Sun3s). Actually this is false
on many RISC machines, such as Sun4s. On SPARC machines, arguments are
passed in registers (each calling frame is actually a set of registers -
there are 32 sets of 24 (overlapping) registers), so calls (especially to
leaf routines) is really fast. The system flushs the register set to the
(memory) stack when it is full.  On Vaxen, a calling frame is supported by
hardware (on the calling stack).  By the way, your code is neither lint
nor Ansi-C (or C++) compliant.

For this reason, varargs (with setjmp, etc) facilities are peculiar to
each compiler&machine (and their interface should be and is normalized in
Ansi-C or K&R-C or C++). On Sun3, varargs are plain C (machine and
compiler dependant) macros. On Sun4, varargs use  special identifiers
__builtin_va_alist & __builtin_va_arg_incr (see /usr/include/varargs.h)
known by the compiler. On Vaxen, varargs call a special routine (assembly
coded in libc.a).

By the way, C is lacking of the varargs reciprocial: calling a routine
(given by its adress) with a variable sized list of arguments. More
specifically, i'm seeking for a "varapply" capability to call a routine
whose argument list is known at run time - e.g. given by a null terminated
array of (argument size, argument adress) tuples.

   	Basile STARYNKEVITCH
   	Commissariat a l' Energie Atomique
   	DRN/DMT/SERMA
   	C.E. Saclay
   	91191 GIF/YVETTE CEDEX
   	France

     email: basile@soleil.saclay.cea.fr