/* --- Copyright University of Sussex 1998. All rights reserved. ----------
 * File:	    C.power/src/amain.s
 * Purpose:	    Entry routine for Pop
 * Author:	    John Gibson, Mar 17 1998
 */

;;; ------------ ENTRY POINT FOR THE POPLOG SYSTEM ------------------------

#_<

#_INCLUDE 'asm.ph'

vars $-Sys$- _init_args, Sys$-Extern$- _invocation_fp;

>_#

ASM_START_FILE



#_IF DEF AIX


	.csect[PR]

.globl .main
.main:
	mfcr	rt0
	stW	rt0, _WOFFS*1(rsp)	;;; save condition reg in caller frame
	mflr	rt0
	stW	rt0, _WOFFS*2(rsp)	;;; save link reg in caller frame
	mr	rt0, rsp

	;;; save AIX local registers (13-31)
	la	rsp, -_WOFFS*19(rsp)	;;; alloc space to save regs
	stW	13, _WOFFS*0(rsp)
	stW	14, _WOFFS*1(rsp)
	stW	15, _WOFFS*2(rsp)
	stW	16, _WOFFS*3(rsp)
	stW	17, _WOFFS*4(rsp)
	stW	18, _WOFFS*5(rsp)
	stW	19, _WOFFS*6(rsp)
	stW	20, _WOFFS*7(rsp)
	stW	21, _WOFFS*8(rsp)
	stW	22, _WOFFS*9(rsp)
	stW	23, _WOFFS*10(rsp)
	stW	24, _WOFFS*11(rsp)
	stW	25, _WOFFS*12(rsp)
	stW	26, _WOFFS*13(rsp)
	stW	27, _WOFFS*14(rsp)
	stW	28, _WOFFS*15(rsp)
	stW	29, _WOFFS*16(rsp)
	stW	30, _WOFFS*17(rsp)
	stW	31, _WOFFS*18(rsp)

	;;; 2nd arg (R4 = rt7) is the address of arg string vec
	ldW	rt1, T.init_args(rtoc)
	stW	rt7, 0(rt1)		;;; store in _init_args

	;;; set up _special_var_block in rsvp
	ldW	rsvb, T.special_var_block(rtoc)
	;;; save fp in _invocation_fp for unwinding after an error signal
	stW	rt0, _svb_INVOC_FP	;;; save original sp

	ldW	rfalse, _svb_FALSE	;;; set rfalse to false
	INIT_POP_REGISTERS		;;; set pop lvar registers to false
	li	R0, 0
	stW	R0, _svb_IN_USER_EXTERN	;;; clear _in_user_extern
	ldW	rusp, _svb_SAVED_USP	;;; temp user stack pointer in rusp
	ldW	rpb, T.setpop(rtoc)	;;; get setpop
	bl	XC_LAB(setpop)		;;; call setpop

	;;; restore AIX local registers
	ldW	13, _WOFFS*0(rsp)
	ldW	14, _WOFFS*1(rsp)
	ldW	15, _WOFFS*2(rsp)
	ldW	16, _WOFFS*3(rsp)
	ldW	17, _WOFFS*4(rsp)
	ldW	18, _WOFFS*5(rsp)
	ldW	19, _WOFFS*6(rsp)
	ldW	20, _WOFFS*7(rsp)
	ldW	21, _WOFFS*8(rsp)
	ldW	22, _WOFFS*9(rsp)
	ldW	23, _WOFFS*10(rsp)
	ldW	24, _WOFFS*11(rsp)
	ldW	25, _WOFFS*12(rsp)
	ldW	26, _WOFFS*13(rsp)
	ldW	27, _WOFFS*14(rsp)
	ldW	28, _WOFFS*15(rsp)
	ldW	29, _WOFFS*16(rsp)
	ldW	30, _WOFFS*17(rsp)
	ldW	31, _WOFFS*18(rsp)
	la	rsp, _WOFFS*19(rsp)

	ldW	rt0, _WOFFS*1(rsp)	;;; restore condition reg
	mtcrf	0xff, rt0
	ldW	rt0, _WOFFS*2(rsp)	;;; restore link reg
	mtlr	rt0
	li	rchain, 0		;;; normal exit status (= R3)
	blr				;;; return


	.toc

T.init_args:
	.tc	a1[TC], I_LAB(Sys$- _init_args)
T.special_var_block:
	.tc	a2[TC], C_LAB(_special_var_block)
T.setpop:
	.tc	a3[TC], C_LAB(setpop)



.globl main[DS]
	.csect	main[DS]
DEF_C_LAB(Sys$- _entry_point)
	.long	.main
	.long	TOC[TC0]
	.long	0x00000000

#_ENDIF

ASM_END_FILE
