Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83; site sdcsvax.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxl!houxm!houxz!vax135!cornell!uw-beaver!tektronix!hplabs!sdcrdcf!sdcsvax!laman
From: laman@sdcsvax.UUCP (Mike Laman)
Newsgroups: net.bugs.4bsd,net.bugs
Subject: Waste of space for local initialized character data
Message-ID: <1100@sdcsvax.UUCP>
Date: Wed, 8-Aug-84 17:53:53 EDT
Article-I.D.: sdcsvax.1100
Posted: Wed Aug  8 17:53:53 1984
Date-Received: Fri, 10-Aug-84 02:36:27 EDT
Organization: EECS Dept. U.C. San Diego
Lines: 53

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