Article 131777 of comp.os.vms:
Path: nntpd.lkg.dec.com!lead.zk3.dec.com!zk2nws.zko.dec.com!usenet
From: mccarthy@hndymn.enet.dec.com (Core Technology Group - Run Time Libraries)
Newsgroups: comp.os.vms
Subject: Re: DECC uses SP to access local variables
Date: 18 Oct 1995 10:23:20 GMT
Organization: Core Technology Group
Lines: 87
Distribution: world
Message-ID: <462kio$gnn@zk2nws.zko.dec.com>
References: <01HWIS0F2EKG8WWVUP@kopc.hhs.dk>
Reply-To: mccarthy@hndymn.enet.dec.com (Core Technology Group - Run Time Libraries)
NNTP-Posting-Host: hndymn.zko.dec.com
X-Newsreader: mxrn 6.18-32



Below is another VAX alloca hack called allocate_stack.  Its a callable 
routine that uses RET instead of jmp to get back to the caller (letting the 
machine unwind the frame).

But as stated in other postings, local variables in the routine this is called
from may not be safe.

As for OpenVMS Alpha and DEC C's alloca(), don't be confused by thinking your
stack allocation is made 'at run time' when the alloca() routine is executed.  
The allocation is done upon routine entry.  Check out the machine code (the
routine prologe portion) to verify this.  At least this was the case with
'older' compilers.  I have not looked into it in over a year.

















	.ENTRY  allocate_stack, ^M<R2,R3,R4,R5>
;
; What we want to do here is adjust the stack pointer ahead by the value
; in 4(ap) and return the pointer to the adjusted stack to the caller in r0
; Note:
;	We do not add the size of this call frame to the 
;	adjusted value because when this routine is called, 
;	this frame is created on the stack, that means we already
;	have 44 bytes allocated (that is:
; 		Handler - Address	 0
;  		PSL			 4
;  		AP			 8
;  		FP			16
;  		PC			20
;  		R2 SAVE			24
;  		R3 SAVE			28
;  		R4 SAVE			32
;  		R5 SAVE			36
;  		ARG COUNT		40
;  		FIRST ARG		44
;
; 	When we move the SP down
;	by the requested amount, and then point FP at that new location,
;	when the RET pops things off the stack, it is taking that
;	44 bytes back off off from the newly adjusted stack.  We are left with
;	exactly the amount requested (plus any padding)
;

	;
	; Add 3 to requested value, then use the BICL2 instruction to
	; make sure the request is a multiple of 4 (longword aligned).  
	; Me, think of using the BICL2 instruction myself? nope.  I checked 
	; other sources for the word align and found several examples.  
	;
	ADDL3	#3, 4(AP), R0	
	BICL2	#3, R0
	; 
	; Move the stack down requested amount
	;
	SUBL	R0, SP
	;
	; Move the contents of the current frame into the area we just 
	; allocated on the stack. 
	;
	MOVC3	#44, (FP), (SP)
	;
	; Subtract the size of this frame (44 bytes) from the current FP and
	; that is the address of the stack storage requested. (since this 
	; routine has no local storage, FP and SP were equal when we entered)
	;
        SUBL3	#44, FP, R0
	;
	; The last thing to do is to change the frame pointer to be where we
	; moved the SP to.  This will cause the RET statement to clear this
	; "new" stack off the frame and set SP to the location at the bottom
	; of the stack.
	; 
	MOVL	SP, FP

	RET

	.END


-- 
--------------------------------------------------------------------------------
Brian J. McCarthy		| Internet: mccarthy@hndymn.enet.dec.com
Digital Equipment Corp.	        | DEC CRTL/DEC C++ Class Libraries
Nashua, NH
--------------------------------------------------------------------------------



