Subject: Re: example for k&r being bad (was: Re: Bluetooth protocol code)
To: Hubert Feyrer <feyrer@cs.stevens.edu>
From: Greg Troxel <gdt@ir.bbn.com>
List: tech-userlevel
Date: 12/20/2005 08:24:51
On NetBSD/sparc64 2.1RC2, gcc 3.3.3:
1 2

But that's because the values are passed in registers.  One needs to
use a lot of variables to exceed the available registers.  The
following program produces 2 1076945920 (but still produces 1 2 on
i386):

int pr(a,b,c,d,e,f,g,h,i,j,x,y)
int a,b,c,d,e;
long int x;
int y;
{
 printf("%ld %d\n",x,y);
}
n
int main(ac,av)
int ac;
char **av;
{
 pr(65,66,67,68.69,70,71,72,73,74,1,2);
 return(0);
}

sparc64 assembly:

	.file	"krarg.c"
	.section	".rodata"
	.align 8
.LLC0:
	.asciz	"%ld %d\n"
	.section	".text"
	.align 4
	.global pr
	.type	pr, #function
	.proc	04
pr:
	!#PROLOGUE# 0
	save	%sp, -192, %sp
	!#PROLOGUE# 1
	mov	%i0, %g1
	mov	%i1, %g2
	mov	%i2, %g3
	mov	%i3, %g4
	mov	%i4, %g5
	mov	%i5, %o5
	st	%g1, [%fp+2175]
	st	%g2, [%fp+2183]
	st	%g3, [%fp+2191]
	st	%g4, [%fp+2199]
	st	%g5, [%fp+2207]
	st	%o5, [%fp+2215]
	ld	[%fp+2267], %g1
	sra	%g1, 0, %g2
	sethi	%hi(.LLC0), %g1
	or	%g1, %lo(.LLC0), %o0
	ldx	[%fp+2255], %o1
	mov	%g2, %o2
	call	printf, 0
	 nop
	mov	%g1, %i0
	return	%i7+8
	nop
	.size	pr, .-pr
	.section	".rodata"
	.align 8
.LLC1:
	.long	1079061544
	.long	4123168604
	.section	".text"
	.align 4
	.global main
	.type	main, #function
	.proc	04
main:
	!#PROLOGUE# 0
	save	%sp, -256, %sp
	!#PROLOGUE# 1
	mov	%i0, %g1
	stx	%i1, [%fp+2183]
	st	%g1, [%fp+2175]
	sethi	%hi(.LLC1), %g1
	or	%g1, %lo(.LLC1), %g1
	ldd	[%g1], %f10
	mov	72, %g1
	stx	%g1, [%sp+2223]
	mov	73, %g1
	stx	%g1, [%sp+2231]
	mov	74, %g1
	stx	%g1, [%sp+2239]
	mov	1, %g1
	stx	%g1, [%sp+2247]
	mov	2, %g1
	stx	%g1, [%sp+2255]
	mov	65, %o0
	mov	66, %o1
	mov	67, %o2
	fmovd	%f10, %f8
	fmovd	%f8, %f6
	std	%f10, [%fp+2023]
	ldx	[%fp+2023], %o3
	mov	70, %o4
	mov	71, %o5
	call	pr, 0
	 nop
	mov	0, %g1
	mov	%g1, %i0
	return	%i7+8
	nop
	.size	main, .-main
	.ident	"GCC: (GNU) 3.3.3 (NetBSD nb3 20040520)"


sparc64 assembly with O2

	.file	"krarg.c"
	.section	.rodata.str1.8,"aMS",@progbits,1
	.align 8
.LLC0:
	.asciz	"%ld %d\n"
	.section	".text"
	.align 4
	.global pr
	.type	pr, #function
	.proc	04
pr:
	!#PROLOGUE# 0
	!#PROLOGUE# 1
	sethi	%hi(.LLC0), %o0
	ldsw	[%sp+2267], %o2
	ldx	[%sp+2255], %o1
	sethi	%hi(printf), %g1
	jmpl	%g1 + %lo(printf), %g0
	or	%o0, %lo(.LLC0), %o0
	nop
	.size	pr, .-pr
	.section	.rodata.cst8,"aM",@progbits,8
	.align 8
.LLC1:
	.long	1079061544
	.long	4123168604
	.section	".text"
	.align 4
	.global main
	.type	main, #function
	.proc	04
main:
	!#PROLOGUE# 0
	save	%sp, -256, %sp
	!#PROLOGUE# 1
	sethi	%hi(.LLC1), %g1
	mov	72, %g2
	ldd	[%g1+%lo(.LLC1)], %f6
	stx	%g2, [%sp+2223]
	mov	73, %g1
	mov	74, %g2
	stx	%g1, [%sp+2231]
	stx	%g2, [%sp+2239]
	mov	1, %g1
	mov	2, %g2
	std	%f6, [%fp+2023]
	mov	0, %i0
	stx	%g1, [%sp+2247]
	stx	%g2, [%sp+2255]
	mov	65, %o0
	mov	66, %o1
	mov	67, %o2
	ldx	[%fp+2023], %o3
	mov	70, %o4
	call	pr, 0
	mov	71, %o5
	return	%i7+8
	nop
	.size	main, .-main
	.ident	"GCC: (GNU) 3.3.3 (NetBSD nb3 20040520)"

-- 
        Greg Troxel <gdt@ir.bbn.com>