#/************************************************************************/
#/*                                                                      */
#/* Copyright (c) 1988 -                                                 */
#/*     The Rector and Visitors of the University of Virginia            */
#/*                                                                      */
#/* This code may not be distributed further without permission          */
#/*      from the University of Virginia.                                */
#/* This code is distributed WITHOUT ANY WARRANTY.  No claims are        */
#/*      made as to whether it serves any particular purpose or even     */
#/*      works at all.                                                   */
#/*                                                                      */
#/************************************************************************/

# RT_goto implements a Pascal non-local goto.  RT_goto takes two
# arguments:  the target label and the frame pointer of the most
# recent activation of the procedure/function containing the label.
# RT_goto rolls back the stack control pointers %ap, %fp, %sp to
# the target activation record; restores old display entries;
# closes intervening local files; and jumps to the target label.

	.text
	.align	4
	.globl	RT_goto

RT_goto:
	save	&0

	# save target label in %r8

	movw	(%ap),%r8

	# save frame pointer in %r0

	movw	4(%ap),%r0

	# roll back %fp and %ap to the activation record just above
	# the target activation record.  Do not move %sp, yet.

.NL1:

	movw	-32(%fp),%ap
	movw	-28(%fp),%fp

	# restore the _DISPLAY entries

	movw	0(%fp),%r1
	movw	4(%fp),(%r1)
	movw	8(%fp),4(%r1)

	# if next %fp is not the target, repeat

	cmpw	-28(%fp),%r0
	jne	.NL1

	# we are now at the activation record just above the target.
	# call RT_fclose2 to close any files between %fp and %sp.

	pushw	%fp
	pushw	%sp
	call	&2,RT_fclose2

	# restore %fp, %ap, %sp to target activation record.
	# NOTE:  we must restore %sp last since any memory above %sp
	# may be overwritten by the system.

	movw	%ap,%r0
	movw	-32(%fp),%ap
	movw	-28(%fp),%fp
	movw	%r0,%sp

	# jump to target label

	jmp	(%r8)
