/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident	"$Header $"

/*
 * _nargs.c
 *	Implement C-callable get-number-arguments function.
 *
 * Returns # words (32-bit) of arguments caller was called with.
 *
 * This function is machine/compiler/etc dependent.
 */

#define	ADDL_IMM8	0xc483
#define ADDL_IMM32	0xc481
#define POPL_ECX	0x59

asm getpc(pc)
{
%reg pc;
	movl	0x0(%ebp),pc	/* follow frame pointer back */
	addl	$0x4,pc		/* skip one word for return addr */
	movl	(pc),pc		/* load return addr into pc */
}

_nargs()
{
	register char	*pc;		/* we know this is edi */

	/*
	 * Get callers return pc (this proc'c FP -> fp in callers return
	 * stack frame).
	 */

	getpc(pc);

	/*
	 * Is either "popl %ecx", "addl $imm,%esp", or caller had no arguments.
	 */

	if ((*pc & 0xff) == POPL_ECX)
		return(1);

	switch(*(unsigned short *)pc) {
	
	case ADDL_IMM8:
		return(*(pc+2)/4);

	case ADDL_IMM32:
		return((*(int *)(pc+2))/4);

	default:
		return(0);
	}
}
