[net.bugs.4bsd] Waste of space for local initialized character data

laman@sdcsvax.UUCP (Mike Laman) (08/09/84)

The following two functions show a "trivial" problem with the 4.2BSD compiler
wasting space on the frame for the local variables.  The first function
allocates 4 bytes on the stack for local data, but the second function, "bug",
allocates 8 bytes on the stack for local data.  The compiler is consistent in
data access, so there is no REAL bug; just a waste of stack space.

good() {
	char a, b;

	a = 'a'; b = 'b'; /* Show where each local variable is on the frame */
}

bug() {
	char a = 0, b;

	a = 'a'; b = 'b'; /* Show where each local variable is on the frame */
}

This is the beautified assembly output:

	.text
	.align	1
	.globl	_good
_good:	.word	0x0
	subl2	$4,sp
	movb	$97,-1(fp)
	movb	$98,-2(fp)
	ret

	.align	1
	.globl	_bug
_bug:	.word	0x0
	subl2	$8,sp
	# Opps.  Why are 8 bytes reserved?  Notice below that the first one
	# is the only variable in the first word.  Some how the internal
	# location counter got rounded up to a word boundary by the
	# initialization.
	clrb	-1(fp)
	movb	$97,-1(fp)
	movb	$98,-5(fp)
	ret

End of assembly.

I thought I would point this out to the network.  I'm not going to lose any
sleep over it, as I'm sure others won't.  Who knows, maybe someone will
fix this up.  I would think that this is closely tied to the initialization
of global data, where global characters are allocated the same way.
The waste of stack space may be in other compiler versions, too.


		Mike Laman
		UUCP: {ucbvax,philabs,sdccsu3,sdcsla}!sdcsvax!laman