[net.micro.68k] Does anyone have nargs

dave@utcsrgv.UUCP (Dave Sherman) (12/22/83)

I am planning to port the legal education CAI I have developed to
an as-yet unidentified 68000 machine running 20-30 ports. Right
now it's on an 11/23 running v7. I make fairly heavy use of nargs(),
which officially doesn't exist in v7 - I borrowed the PDP-11 assembler
code from an old v6 machine.

The nargs() routine returns the number of arguments which its calling
procedure was called with. It's a very handy thing to have - takes about
50 lines in PDP-11 assembler.

I don't know the first thing about Motorola assembler, but I assume
there is such a thing, and that someone out there may have written
nargs() in it. The machine we'll be getting will probably run Unisoft's
UniPlus, or possibly Xenix.

Can anyone help? Thanks.

Dave Sherman
The Law Society of Upper Canada
Toronto
-- 
 {allegra,cornell,decvax,ihnp4,linus,utzoo}!utcsrgv!dave

shah@fortune.UUCP (12/25/83)

#R:utcsrgv:-298800:fortune:6600004:000:1593
fortune!shah    Dec 25 10:46:00 1983

    Here is one version of nargs.  You may have to tweak it
a little to keep your assembler/compiler happy.  The idea
is to check for the instruction after "jsr my_caller".
If it is a stack pointer adjusting instruction the caller
was called with one or more arguments.

					-- Bakul Shah

	{sri-unix,amd70,hpda,harpo,ihnp4,allegra}!fortune!shah

=-=-=-=-=-=-=-=-=
; nargs()
; -- returns the number of arguments supplied to the caller
;    assumes that our caller has done a link %a6,xxx

#define     ADDQ_8_A7   0x508f          /*      addql   #8,%a7              */
#define     ADDQ_4_A7   0x588f          /*      addql   #4,%a7              */
#define     ADDL_XX_A7  0xdffc          /*      addl    #xxxx,%a7           */

	.text
	.globl  nargs

nargs:
	movl    %a6@(4),%a0             ; %a0 = caller's caller's pc
	movw    %a0@,%d0                ; %d0 = next inst (1st word)
	cmpw    #ADDL_XX_A7,%d0         ; is this addl #xxxx,%a7 ?
	bne     1$                      ;   no, skip to next test
	movl    %a0@(2),%d0             ; #xxxx from addl #xxxx,%a7 to %d0
	asrl    #2,%d0                  ; div by 4 to get NUMBER of args
	rts

1$:     cmpw    #ADDQ_8_A7,%d0          ; is this addql #8,%a7 ?
	bne     2$                      ;   no, skip to next test
	moveq   #2,%d0                  ; 2 args
	rts

2$:     cmpw    #ADDQ_4_A7,%d0          ; is this addql #4,%a7 ?
	bne     3$                      ;   no, this routine has no args
	moveq   #1,%d0                  ; 1 arg
	rts

3$:     clrl    %d0                     ; no args (or the compiler fooled us!)
	rts
=-=-=-=-=-=-=-=-=

jack@hp-dcde.UUCP (12/31/83)

#R:utcsrgv:-298800:hp-dcde:21100001:000:926
hp-dcde!jack    Dec 28 11:16:00 1983

re: nargs

Remember, though, it won't always be an ADD instruction after the jsr.
For example, our optimizer makes the transformation:

	move.l	d0,-(sp)	=>	move.l	d0,-(sp)
	move.l	d1,-(sp)	=>	move.l	d1,-(sp)
	move.l	d2,-(sp)	=>	move.l	d2,-(sp)
	jbsr	gamma		=>	jbsr	gamma
	add.l	#12,sp		=>	lea	12(sp),sp

Or perhaps:

	move.l	d0,-(sp)	=>	move.l	d0,-(sp)
	jbsr	alpha		=>	jbsr	alpha
	addq.l	#4,sp		=>
	move.l	d0,-(sp)	=>	move.l	d0,(sp)		no decrement
	jbsr	beta		=>	jbsr	beta
	addq.l	#4,sp		=>	addq.l	#4,sp

Timings:
	add.l	#12,sp		8 cycles, 6 bytes	(really adda)
	add.w	#12,sp		12 cycles, 4 bytes	(really adda)
	lea	12(sp),sp	8 cycles, 4 bytes

Of course, nargs could be enhanced to check for lea also,
and hence work for the gamma case, but the alpha case looks hopeless.


					      -Jack Applin
					      Optimizing himself to death at:	
					      Hewlett-Packard
					      Ft. Collins, CO
					      (hplabs!hp-dcd!jack)