[comp.sys.pyramid] alloca

csg@pyramid.pyramid.com (Carl S. Gutekunst) (01/06/88)

In a recent battle to port gcc to a Pyramid 98x, I cobbled up my own version
of the alloca(3) function. Since it doesn't work very well, I decided to post
it to the net. :-)

See the caveats in the comments; it is not possible to make alloca() work like
it does on a VAX or a 680x0, but this works pretty well in large programs that
declare lots of automatic arrays and structs. The difference between my version
and the one provided with GNU Emacs is that mine tries to prevent objects from
straddling the Pyramid's cache boundaries; this is a big win when allocating
large numbers of small blocks (8 to 32 bytes).

DISCLAIMER: I am posting this as a Pyramid user, and NOT as a representative
of Pyramid Technology Corp. This code *will* fail in many applications that
work just fine on a VAX or 680x0, and is intended only as a stopgap measure
to get code running until it can be modified to not use alloca(), or someone
defines a way for C to dynamically declare automatic arrays. (Hope! :-))

Problems -> csg@pyramid.com, NOT to pyramid!bugs.

<csg>
_______________________________________________________________________________

	.text	1
_RCSid:	.ascii	"@(#) alloca.s $Revision: 1.1 $ $Date: 88/01/04 13:53:05 $"
#
#	alloca.s -- a crude implementation of alloca() for the Pyramid 90x
#
#	This function performs the basic function of the VAX alloca(3) call:
#	allocate a requested block of memory on the data stack, and return
#	the starting address to the caller. The assumption is that the space
#	requested will be released upon function exit.
#
#	The Pyramid CLE code generator does not normally save and restore the
#	data stack frame on C functions (that is, a plain "ret" is done instead
#	of an "adsf" and "retd"), so this space might *not* go away on exit.
#	Fortunately, a "retd" instruction executed by a parent of the calling
#	function will release the space, and the code generator always produces
#	addresses relative to CFP, not SP. Hence the worst consequence of this
#	function is stack overflow.
#
#	In general, this function can be used successfully if:
#
#	- The calling function or its parent declares local structures or
#	  arrays, which forces generation of "adsf" and "retd";
#
#	- The calling function is not called recursively or inside a loop;
#
#	- The calling function is not running inside a signal handler.
#
#	NOTE: To improve performance, this function attempts to keep objects
#	from straddling 32-byte cache boundaries. This means that there may be
#	"holes" between consecutively allocated objects.
#
#	NOTE: This function may leave the SP on a non-32-byte boundary. The
#	CLE code generator, for optimization purposes, assumes that the SP is
#	always kept 32-byte aligned. This may affect performance of functions
#	called by the parent function of alloca().

	.text	0
	.globl _alloca
_alloca:
	addw	$3,pr0			# Round the request to an int boundary
	andw	$-4,pr0			# /
	cmpw	$32,pr0			# Is the request >= 32?
	bge	alloca_big		# Yes, return SP 32-byte aligned

	movw	sp,lr0			# Truncate SP to cache boundary
	andw	$-32,lr0		# /
	movw	sp,lr1			# Calculate distance to boundary
	subw	lr0,lr1			# /
	subw	pr0,sp			# Subtract the request from the SP
	cmpw	lr0,sp			# Cross a boundary?
	bge	alloca_ret		# Nope, so we're done.
	subw	lr1,sp			# Yes, move down the minimum distance
alloca_ret:
	movw	sp,pr0			# Return the new SP
	ret

alloca_big:
	subw	pr0,sp			# Allocate requested space on Stack
	andw	$-32,sp			# Truncate to 32-byte boundary
	movw	sp,pr0			# Return the new SP
	ret

karl@triceratops.cis.ohio-state.edu (Karl Kleinpaste) (03/21/89)

pete@sun360.Online.Nixdorf.De (pete delany) writes:
   Could someone send a copy of alloca() for OSX 4.1;

You've already got it:

[78] [8:09am] tut:/n/dinosaur/0/karl> nm /usr/.attlib/libPW.a | grep alloca
         U _alloca
         U _alloca
alloca.o:
00000000 T _alloca

--Karl

zap@nada.kth.se (Svante Lindahl) (03/22/89)

In article <KARL.89Mar21080920@triceratops.cis.ohio-state.edu> karl@triceratops.cis.ohio-state.edu (Karl Kleinpaste) writes:
>pete@sun360.Online.Nixdorf.De (pete delany) writes:
>   Could someone send a copy of alloca() for OSX 4.1;
>
>You've already got it:

>[78] [8:09am] tut:/n/dinosaur/0/karl> nm /usr/.attlib/libPW.a | grep alloca

That version differs slightly from the one that is distributed with
Gnu Emacs (in file src/alloca.s). From that file:

.globl _alloca

_alloca: addw $3,pr0	# add 3 (dec) to first argument
	bicw $3,pr0	# then clear its last 2 bits
	subw pr0,sp	# subtract from SP the val in PR0
	andw $-32,sp	# keep sp aligned on multiple of 32.
	movw sp,pr0	# ret. current SP
	ret


Svante