xref: /csrg-svn/usr.bin/pascal/src/fend.c (revision 5654)
13192Smckusick /* Copyright (c) 1979 Regents of the University of California */
23192Smckusick 
3*5654Slinton static char sccsid[] = "@(#)fend.c 1.9 02/02/82";
43192Smckusick 
53192Smckusick #include "whoami.h"
63192Smckusick #include "0.h"
73192Smckusick #include "tree.h"
83192Smckusick #include "opcode.h"
93192Smckusick #include "objfmt.h"
103192Smckusick #include "align.h"
113192Smckusick 
123192Smckusick /*
133192Smckusick  * this array keeps the pxp counters associated with
143192Smckusick  * functions and procedures, so that they can be output
153192Smckusick  * when their bodies are encountered
163192Smckusick  */
173192Smckusick int	bodycnts[ DSPLYSZ ];
183192Smckusick 
193192Smckusick #ifdef PC
203192Smckusick #   include "pc.h"
213192Smckusick #   include "pcops.h"
223192Smckusick #endif PC
233192Smckusick 
243192Smckusick #ifdef OBJ
253192Smckusick int	cntpatch;
263192Smckusick int	nfppatch;
273192Smckusick #endif OBJ
283192Smckusick 
293192Smckusick struct	nl *Fp;
303192Smckusick int	pnumcnt;
313192Smckusick /*
323192Smckusick  * Funcend is called to
333192Smckusick  * finish a block by generating
343192Smckusick  * the code for the statements.
353192Smckusick  * It then looks for unresolved declarations
363192Smckusick  * of labels, procedures and functions,
373192Smckusick  * and cleans up the name list.
383192Smckusick  * For the program, it checks the
393192Smckusick  * semantics of the program
403192Smckusick  * statement (yuchh).
413192Smckusick  */
423192Smckusick funcend(fp, bundle, endline)
433192Smckusick 	struct nl *fp;
443192Smckusick 	int *bundle;
453192Smckusick 	int endline;
463192Smckusick {
473192Smckusick 	register struct nl *p;
483192Smckusick 	register int i, b;
493192Smckusick 	int var, inp, out, *blk;
503192Smckusick 	bool chkref;
513192Smckusick 	struct nl *iop;
523192Smckusick 	char *cp;
533192Smckusick 	extern int cntstat;
543192Smckusick #	ifdef PC
553363Speter 	    int		savlabel = getlab();
563363Speter 	    int		toplabel = getlab();
573363Speter 	    int		botlabel = getlab();
583649Speter 	    int		proflabel = getlab();
593363Speter 	    char	extname[ BUFSIZ ];
603192Smckusick #	endif PC
613192Smckusick 
623192Smckusick 	cntstat = 0;
633192Smckusick /*
643192Smckusick  *	yyoutline();
653192Smckusick  */
663192Smckusick 	if (program != NIL)
673192Smckusick 		line = program->value[3];
683192Smckusick 	blk = bundle[2];
693192Smckusick 	if (fp == NIL) {
703192Smckusick 		cbn--;
713192Smckusick #		ifdef PTREE
723192Smckusick 		    nesting--;
733192Smckusick #		endif PTREE
743192Smckusick 		return;
753192Smckusick 	}
763192Smckusick #ifdef OBJ
773192Smckusick 	/*
783192Smckusick 	 * Patch the branch to the
793192Smckusick 	 * entry point of the function
803192Smckusick 	 */
813192Smckusick 	patch4(fp->entloc);
823192Smckusick 	/*
833192Smckusick 	 * Put out the block entrance code and the block name.
843192Smckusick 	 * HDRSZE is the number of bytes of info in the static
853192Smckusick 	 * BEG data area exclusive of the proc name. It is
863192Smckusick 	 * currently defined as:
873192Smckusick 	/*	struct hdr {
883192Smckusick 	/*		long framesze;	/* number of bytes of local vars */
893192Smckusick 	/*		long nargs;	/* number of bytes of arguments */
903192Smckusick 	/*		bool tests;	/* TRUE => perform runtime tests */
913192Smckusick 	/*		short offset;	/* offset of procedure in source file */
923192Smckusick 	/*		char name[1];	/* name of active procedure */
933192Smckusick 	/*	};
943192Smckusick 	 */
953192Smckusick #	define HDRSZE (2 * sizeof(long) + sizeof(short) + sizeof(bool))
963192Smckusick 	var = put(2, ((lenstr(fp->symbol,0) + HDRSZE) << 8)
973192Smckusick 		| (cbn == 1 && opt('p') == 0 ? O_NODUMP: O_BEG), (long)0);
983192Smckusick 	    /*
993192Smckusick 	     *  output the number of bytes of arguments
1003192Smckusick 	     *  this is only checked on formal calls.
1013192Smckusick 	     */
1023192Smckusick 	put(2, O_CASE4, cbn == 1 ? (long)0 : (long)(fp->value[NL_OFFS]-DPOFF2));
1033192Smckusick 	    /*
1043192Smckusick 	     *	Output the runtime test mode for the routine
1053192Smckusick 	     */
1063192Smckusick 	put(2, sizeof(bool) == 2 ? O_CASE2 : O_CASE4, opt('t') ? TRUE : FALSE);
1073192Smckusick 	    /*
1083192Smckusick 	     *	Output line number and routine name
1093192Smckusick 	     */
1103192Smckusick 	put(2, O_CASE2, bundle[1]);
1113192Smckusick 	putstr(fp->symbol, 0);
1123192Smckusick #endif OBJ
1133192Smckusick #ifdef PC
1143192Smckusick 	/*
1153192Smckusick 	 * put out the procedure entry code
1163192Smckusick 	 */
1173192Smckusick 	if ( fp -> class == PROG ) {
1183192Smckusick 	    putprintf( "	.text" , 0 );
1193192Smckusick 	    putprintf( "	.align	1" , 0 );
1203192Smckusick 	    putprintf( "	.globl	_main" , 0 );
1213192Smckusick 	    putprintf( "_main:" , 0 );
1223192Smckusick 	    putprintf( "	.word	0" , 0 );
1233192Smckusick 	    putprintf( "	calls	$0,_PCSTART" , 0 );
1243192Smckusick 	    putprintf( "	movl	4(ap),__argc" , 0 );
1253192Smckusick 	    putprintf( "	movl	8(ap),__argv" , 0 );
1263192Smckusick 	    putprintf( "	calls	$0,_program" , 0 );
1273192Smckusick 	    putprintf( "	calls	$0,_PCEXIT" , 0 );
1283192Smckusick 	    ftnno = fp -> entloc;
1293192Smckusick 	    putprintf( "	.text" , 0 );
1303192Smckusick 	    putprintf( "	.align	1" , 0 );
1313192Smckusick 	    putprintf( "	.globl	_program" , 0 );
1323192Smckusick 	    putprintf( "_program:" , 0 );
1333192Smckusick 	    stabfunc( "program" , fp -> class , bundle[1] , 0 );
1343192Smckusick 	} else {
1353192Smckusick 	    ftnno = fp -> entloc;
1363192Smckusick 	    putprintf( "	.text" , 0 );
1373192Smckusick 	    putprintf( "	.align	1" , 0 );
1383367Speter 	    sextname( extname , fp -> symbol , cbn - 1 );
1393428Speter 	    putprintf( "	.globl	%s%s" , 0 , FORMALPREFIX , extname );
1403363Speter 	    putprintf( "	.globl	%s" , 0 , extname );
1413363Speter 	    putprintf( "%s:" , 0 , extname );
1423192Smckusick 	    stabfunc( fp -> symbol , fp -> class , bundle[1] , cbn - 1 );
1433192Smckusick 	    for ( p = fp -> chain ; p != NIL ; p = p -> chain ) {
1443192Smckusick 		stabparam( p -> symbol , p2type( p -> type )
1453192Smckusick 			    , p -> value[ NL_OFFS ] , lwidth( p -> type ) );
1463192Smckusick 	    }
1473192Smckusick 	    if ( fp -> class == FUNC ) {
1483192Smckusick 		    /*
1493192Smckusick 		     *	stab the function variable
1503192Smckusick 		     */
1513192Smckusick 		p = fp -> ptr[ NL_FVAR ];
1523192Smckusick 		stablvar( p -> symbol , p2type( p -> type ) , cbn
1533192Smckusick 			, p -> value[ NL_OFFS ] , lwidth( p -> type ) );
1543192Smckusick 	    }
1553192Smckusick 		/*
1563192Smckusick 		 *	stab local variables
1573192Smckusick 		 *	rummage down hash chain links.
1583192Smckusick 		 */
1593192Smckusick 	    for ( i = 0 ; i <= 077 ; i++ ) {
1603192Smckusick 		for ( p = disptab[ i ] ; p != NIL ; p = p->nl_next) {
1613192Smckusick 		    if ( ( p -> nl_block & 037 ) != cbn ) {
1623192Smckusick 			break;
1633192Smckusick 		    }
1643192Smckusick 		    /*
1653192Smckusick 		     *	stab local variables
1663192Smckusick 		     *	that's named variables, but not params
1673192Smckusick 		     */
1683192Smckusick 		    if (   ( p -> symbol != NIL )
1693192Smckusick 			&& ( p -> class == VAR )
1703192Smckusick 			&& ( p -> value[ NL_OFFS ] < 0 ) ) {
1713192Smckusick 			stablvar( p -> symbol , p2type( p -> type ) , cbn
1723192Smckusick 			    , p -> value[ NL_OFFS ] , lwidth( p -> type ) );
1733192Smckusick 		    }
1743192Smckusick 		}
1753192Smckusick 	    }
1763192Smckusick 	}
1773192Smckusick 	stablbrac( cbn );
1783192Smckusick 	    /*
1793192Smckusick 	     *	register save mask
1803192Smckusick 	     */
1813279Smckusic 	putprintf( "	.word	" , 1 );
1823279Smckusic         putprintf( PREFIXFORMAT , 0 , LABELPREFIX , savlabel );
1833192Smckusick 	putjbr( botlabel );
1843192Smckusick 	putlab( toplabel );
1853192Smckusick 	if ( profflag ) {
1863192Smckusick 		/*
1873192Smckusick 		 *	call mcount for profiling
1883192Smckusick 		 */
1893649Speter 	    putprintf( "	moval	" , 1 );
1903649Speter 	    putprintf( PREFIXFORMAT , 1 , LABELPREFIX , proflabel );
1913649Speter 	    putprintf( ",r0" , 0 );
1923192Smckusick 	    putprintf( "	jsb	mcount" , 0 );
1933192Smckusick 	    putprintf( "	.data" , 0 );
1943192Smckusick 	    putprintf( "	.align	2" , 0 );
1953649Speter 	    putlab( proflabel );
1963192Smckusick 	    putprintf( "	.long	0" , 0 );
1973192Smckusick 	    putprintf( "	.text" , 0 );
1983192Smckusick 	}
1993192Smckusick 	    /*
2003192Smckusick 	     *	set up unwind exception vector.
2013192Smckusick 	     */
2023192Smckusick 	putprintf( "	moval	%s,%d(%s)" , 0
2033192Smckusick 		, UNWINDNAME , UNWINDOFFSET , P2FPNAME );
2043192Smckusick 	    /*
2053192Smckusick 	     *	save address of display entry, for unwind.
2063192Smckusick 	     */
2073192Smckusick 	putprintf( "	moval	%s+%d,%d(%s)" , 0
2083192Smckusick 		, DISPLAYNAME , cbn * sizeof(struct dispsave)
2093192Smckusick 		, DPTROFFSET , P2FPNAME );
2103192Smckusick 	    /*
2113192Smckusick 	     *	save old display
2123192Smckusick 	     */
2133192Smckusick 	putprintf( "	movq	%s+%d,%d(%s)" , 0
2143192Smckusick 		, DISPLAYNAME , cbn * sizeof(struct dispsave)
2153192Smckusick 		, DSAVEOFFSET , P2FPNAME );
2163192Smckusick 	    /*
2173192Smckusick 	     *	set up new display by saving AP and FP in appropriate
2183192Smckusick 	     *	slot in display structure.
2193192Smckusick 	     */
2203192Smckusick 	putprintf( "	movq	%s,%s+%d" , 0
2213192Smckusick 		, P2APNAME , DISPLAYNAME , cbn * sizeof(struct dispsave) );
2223192Smckusick 	    /*
2233192Smckusick 	     *	ask second pass to allocate known locals
2243192Smckusick 	     */
2253192Smckusick 	putlbracket( ftnno , -sizes[ cbn ].om_max );
2263192Smckusick 	    /*
2273192Smckusick 	     *	and zero them if checking is on
2283192Smckusick 	     *	by calling blkclr( bytes of locals , starting local address );
2293192Smckusick 	     */
2303301Smckusic 	if ( opt( 't' ) && ( -sizes[ cbn ].om_max ) > DPOFF1 ) {
2313301Smckusic 	    putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR )
2323301Smckusic 		    , "_blkclr" );
2333301Smckusic 	    putleaf( P2ICON ,  ( -sizes[ cbn ].om_max ) - DPOFF1
2343301Smckusic 		    , 0 , P2INT , 0 );
2353837Speter 	    putLV( 0 , cbn , sizes[ cbn ].om_max , NLOCAL , P2CHAR );
2363301Smckusic 	    putop( P2LISTOP , P2INT );
2373301Smckusic 	    putop( P2CALL , P2INT );
2383301Smckusic 	    putdot( filename , line );
2393192Smckusick 	}
2403192Smckusick #endif PC
2413192Smckusick 	if ( monflg ) {
2423192Smckusick 		if ( fp -> value[ NL_CNTR ] != 0 ) {
2433192Smckusick 			inccnt( fp -> value [ NL_CNTR ] );
2443192Smckusick 		}
2453192Smckusick 		inccnt( bodycnts[ fp -> nl_block & 037 ] );
2463192Smckusick 	}
2473192Smckusick 	if (fp->class == PROG) {
2483192Smckusick 		/*
2493192Smckusick 		 * The glorious buffers option.
2503192Smckusick 		 *          0 = don't buffer output
2513192Smckusick 		 *          1 = line buffer output
2523192Smckusick 		 *          2 = 512 byte buffer output
2533192Smckusick 		 */
2543192Smckusick #		ifdef OBJ
2553192Smckusick 		    if (opt('b') != 1)
2563192Smckusick 			    put(1, O_BUFF | opt('b') << 8);
2573192Smckusick #		endif OBJ
2583192Smckusick #		ifdef PC
2593192Smckusick 		    if ( opt( 'b' ) != 1 ) {
2603192Smckusick 			putleaf( P2ICON , 0 , 0
2613192Smckusick 				, ADDTYPE( P2FTN | P2INT , P2PTR ) , "_BUFF" );
2623192Smckusick 			putleaf( P2ICON , opt( 'b' ) , 0 , P2INT , 0 );
2633192Smckusick 			putop( P2CALL , P2INT );
2643192Smckusick 			putdot( filename , line );
2653192Smckusick 		    }
2663192Smckusick #		endif PC
2673192Smckusick 		out = 0;
2683192Smckusick 		for (p = fp->chain; p != NIL; p = p->chain) {
2693192Smckusick 			if (strcmp(p->symbol, "input") == 0) {
2703192Smckusick 				inp++;
2713192Smckusick 				continue;
2723192Smckusick 			}
2733192Smckusick 			if (strcmp(p->symbol, "output") == 0) {
2743192Smckusick 				out++;
2753192Smckusick 				continue;
2763192Smckusick 			}
2773192Smckusick 			iop = lookup1(p->symbol);
2783192Smckusick 			if (iop == NIL || bn != cbn) {
2793192Smckusick 				error("File %s listed in program statement but not declared", p->symbol);
2803192Smckusick 				continue;
2813192Smckusick 			}
2823192Smckusick 			if (iop->class != VAR) {
2833192Smckusick 				error("File %s listed in program statement but declared as a %s", p->symbol, classes[iop->class]);
2843192Smckusick 				continue;
2853192Smckusick 			}
2863192Smckusick 			if (iop->type == NIL)
2873192Smckusick 				continue;
2883192Smckusick 			if (iop->type->class != FILET) {
2893192Smckusick 				error("File %s listed in program statement but defined as %s",
2903192Smckusick 					p->symbol, nameof(iop->type));
2913192Smckusick 				continue;
2923192Smckusick 			}
2933192Smckusick #			ifdef OBJ
2943192Smckusick 			    put(2, O_CON24, text(iop->type) ? 0 : width(iop->type->type));
2953192Smckusick 			    i = lenstr(p->symbol,0);
2963192Smckusick 			    put(2, O_CON24, i);
2973192Smckusick 			    put(2, O_LVCON, i);
2983192Smckusick 			    putstr(p->symbol, 0);
2993192Smckusick 			    put(2, O_LV | bn<<8+INDX, (int)iop->value[NL_OFFS]);
3003192Smckusick 			    put(1, O_DEFNAME);
3013192Smckusick #			endif OBJ
3023192Smckusick #			ifdef PC
3033192Smckusick 			    putleaf( P2ICON , 0 , 0
3043192Smckusick 				    , ADDTYPE( P2FTN | P2INT , P2PTR )
3053192Smckusick 				    , "_DEFNAME" );
3063837Speter 			    putLV( p -> symbol , bn , iop -> value[NL_OFFS] ,
3073837Speter 				    iop -> extra_flags , p2type( iop ) );
3083192Smckusick 			    putCONG( p -> symbol , strlen( p -> symbol )
3093192Smckusick 				    , LREQ );
3103192Smckusick 			    putop( P2LISTOP , P2INT );
3113192Smckusick 			    putleaf( P2ICON , strlen( p -> symbol )
3123192Smckusick 				    , 0 , P2INT , 0 );
3133192Smckusick 			    putop( P2LISTOP , P2INT );
3143192Smckusick 			    putleaf( P2ICON
3153192Smckusick 				, text(iop->type) ? 0 : width(iop->type->type)
3163192Smckusick 				, 0 , P2INT , 0 );
3173192Smckusick 			    putop( P2LISTOP , P2INT );
3183192Smckusick 			    putop( P2CALL , P2INT );
3193192Smckusick 			    putdot( filename , line );
3203192Smckusick #			endif PC
3213192Smckusick 		}
3223192Smckusick 		if (out == 0 && fp->chain != NIL) {
3233192Smckusick 			recovered();
3243192Smckusick 			error("The file output must appear in the program statement file list");
3253192Smckusick 		}
3263192Smckusick 	}
3273192Smckusick 	/*
3283192Smckusick 	 * Process the prog/proc/func body
3293192Smckusick 	 */
3303192Smckusick 	noreach = 0;
3313192Smckusick 	line = bundle[1];
3323192Smckusick 	statlist(blk);
3333192Smckusick #	ifdef PTREE
3343192Smckusick 	    {
3353192Smckusick 		pPointer Body = tCopy( blk );
3363192Smckusick 
3373192Smckusick 		pDEF( PorFHeader[ nesting -- ] ).PorFBody = Body;
3383192Smckusick 	    }
3393192Smckusick #	endif PTREE
3403192Smckusick #	ifdef OBJ
3413192Smckusick 	    if (cbn== 1 && monflg != 0) {
3423192Smckusick 		    patchfil(cntpatch - 2, (long)cnts, 2);
3433192Smckusick 		    patchfil(nfppatch - 2, (long)pfcnt, 2);
3443192Smckusick 	    }
3453192Smckusick #	endif OBJ
3463192Smckusick #	ifdef PC
3473192Smckusick 	    if ( fp -> class == PROG && monflg ) {
3483192Smckusick 		putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR )
3493192Smckusick 			, "_PMFLUSH" );
3503192Smckusick 		putleaf( P2ICON , cnts , 0 , P2INT , 0 );
3513192Smckusick 		putleaf( P2ICON , pfcnt , 0 , P2INT , 0 );
3523192Smckusick 		putop( P2LISTOP , P2INT );
3533837Speter 		putLV( PCPCOUNT , 0 , 0 , NGLOBAL , P2INT );
3543192Smckusick 		putop( P2LISTOP , P2INT );
3553192Smckusick 		putop( P2CALL , P2INT );
3563192Smckusick 		putdot( filename , line );
3573192Smckusick 	    }
3583192Smckusick #	endif PC
3593192Smckusick 	if (fp->class == PROG && inp == 0 && (input->nl_flags & (NUSED|NMOD)) != 0) {
3603192Smckusick 		recovered();
3613192Smckusick 		error("Input is used but not defined in the program statement");
3623192Smckusick 	}
3633192Smckusick 	/*
3643192Smckusick 	 * Clean up the symbol table displays and check for unresolves
3653192Smckusick 	 */
3663192Smckusick 	line = endline;
3673192Smckusick 	b = cbn;
3683192Smckusick 	Fp = fp;
3693192Smckusick 	chkref = syneflg == errcnt[cbn] && opt('w') == 0;
3703192Smckusick 	for (i = 0; i <= 077; i++) {
3713192Smckusick 		for (p = disptab[i]; p != NIL && (p->nl_block & 037) == b; p = p->nl_next) {
3723192Smckusick 			/*
3733192Smckusick 			 * Check for variables defined
3743192Smckusick 			 * but not referenced
3753192Smckusick 			 */
3763192Smckusick 			if (chkref && p->symbol != NIL)
3773192Smckusick 			switch (p->class) {
3783192Smckusick 				case FIELD:
3793192Smckusick 					/*
3803192Smckusick 					 * If the corresponding record is
3813192Smckusick 					 * unused, we shouldn't complain about
3823192Smckusick 					 * the fields.
3833192Smckusick 					 */
3843192Smckusick 				default:
3853192Smckusick 					if ((p->nl_flags & (NUSED|NMOD)) == 0) {
3863192Smckusick 						warning();
3873192Smckusick 						nerror("%s %s is neither used nor set", classes[p->class], p->symbol);
3883192Smckusick 						break;
3893192Smckusick 					}
3903192Smckusick 					/*
3913192Smckusick 					 * If a var parameter is either
3923192Smckusick 					 * modified or used that is enough.
3933192Smckusick 					 */
3943192Smckusick 					if (p->class == REF)
3953192Smckusick 						continue;
3963192Smckusick #					ifdef OBJ
3973192Smckusick 					    if ((p->nl_flags & NUSED) == 0) {
3983192Smckusick 						warning();
3993192Smckusick 						nerror("%s %s is never used", classes[p->class], p->symbol);
4003192Smckusick 						break;
4013192Smckusick 					    }
4023192Smckusick #					endif OBJ
4033192Smckusick #					ifdef PC
4043837Speter 					    if (((p->nl_flags & NUSED) == 0) && ((p->extra_flags & NEXTERN) == 0)) {
4053192Smckusick 						warning();
4063192Smckusick 						nerror("%s %s is never used", classes[p->class], p->symbol);
4073192Smckusick 						break;
4083192Smckusick 					    }
4093192Smckusick #					endif PC
4103192Smckusick 					if ((p->nl_flags & NMOD) == 0) {
4113192Smckusick 						warning();
4123192Smckusick 						nerror("%s %s is used but never set", classes[p->class], p->symbol);
4133192Smckusick 						break;
4143192Smckusick 					}
4153192Smckusick 				case LABEL:
4163192Smckusick 				case FVAR:
4173192Smckusick 				case BADUSE:
4183192Smckusick 					break;
4193192Smckusick 			}
4203192Smckusick 			switch (p->class) {
4213192Smckusick 				case BADUSE:
4223192Smckusick 					cp = "s";
4233192Smckusick 					if (p->chain->ud_next == NIL)
4243192Smckusick 						cp++;
4253192Smckusick 					eholdnl();
4263192Smckusick 					if (p->value[NL_KINDS] & ISUNDEF)
4273192Smckusick 						nerror("%s undefined on line%s", p->symbol, cp);
4283192Smckusick 					else
4293192Smckusick 						nerror("%s improperly used on line%s", p->symbol, cp);
4303192Smckusick 					pnumcnt = 10;
4313192Smckusick 					pnums(p->chain);
4323192Smckusick 					pchr('\n');
4333192Smckusick 					break;
4343192Smckusick 
4353192Smckusick 				case FUNC:
4363192Smckusick 				case PROC:
4373192Smckusick #					ifdef OBJ
4383192Smckusick 					    if ((p->nl_flags & NFORWD))
4393192Smckusick 						nerror("Unresolved forward declaration of %s %s", classes[p->class], p->symbol);
4403192Smckusick #					endif OBJ
4413192Smckusick #					ifdef PC
4423837Speter 					    if ((p->nl_flags & NFORWD) && ((p->extra_flags & NEXTERN) == 0))
4433192Smckusick 						nerror("Unresolved forward declaration of %s %s", classes[p->class], p->symbol);
4443192Smckusick #					endif PC
4453192Smckusick 					break;
4463192Smckusick 
4473192Smckusick 				case LABEL:
4483192Smckusick 					if (p->nl_flags & NFORWD)
4493192Smckusick 						nerror("label %s was declared but not defined", p->symbol);
4503192Smckusick 					break;
4513192Smckusick 				case FVAR:
4523192Smckusick 					if ((p->nl_flags & NMOD) == 0)
4533192Smckusick 						nerror("No assignment to the function variable");
4543192Smckusick 					break;
4553192Smckusick 			}
4563192Smckusick 		}
4573192Smckusick 		/*
4583192Smckusick 		 * Pop this symbol
4593192Smckusick 		 * table slot
4603192Smckusick 		 */
4613192Smckusick 		disptab[i] = p;
4623192Smckusick 	}
4633192Smckusick 
4643192Smckusick #	ifdef OBJ
4653192Smckusick 	    put(1, O_END);
4663192Smckusick #	endif OBJ
4673192Smckusick #	ifdef PC
4683192Smckusick 		/*
4693192Smckusick 		 *	if there were file variables declared at this level
4703192Smckusick 		 *	call pclose( &__disply[ cbn ] ) to clean them up.
4713192Smckusick 		 */
4723192Smckusick 	    if ( dfiles[ cbn ] ) {
4733192Smckusick 		putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR )
4743192Smckusick 			, "_PCLOSE" );
4753837Speter 		putRV( DISPLAYNAME , 0 , cbn * sizeof( struct dispsave ) ,
4763837Speter 			NGLOBAL , P2PTR | P2CHAR );
4773192Smckusick 		putop( P2CALL , P2INT );
4783192Smckusick 		putdot( filename , line );
4793192Smckusick 	    }
4803192Smckusick 		/*
4813192Smckusick 		 *	if this is a function,
4823192Smckusick 		 *	the function variable is the return value.
4833192Smckusick 		 *	if it's a scalar valued function, return scalar,
4843192Smckusick 		 *	else, return a pointer to the structure value.
4853192Smckusick 		 */
4863192Smckusick 	    if ( fp -> class == FUNC ) {
4873192Smckusick 		struct nl	*fvar = fp -> ptr[ NL_FVAR ];
4883192Smckusick 		long		fvartype = p2type( fvar -> type );
4893192Smckusick 		long		label;
4903192Smckusick 		char		labelname[ BUFSIZ ];
4913192Smckusick 
4923192Smckusick 		switch ( classify( fvar -> type ) ) {
4933192Smckusick 		    case TBOOL:
4943192Smckusick 		    case TCHAR:
4953192Smckusick 		    case TINT:
4963192Smckusick 		    case TSCAL:
4973192Smckusick 		    case TDOUBLE:
4983192Smckusick 		    case TPTR:
4993837Speter 			putRV( fvar -> symbol , ( fvar -> nl_block ) & 037 ,
5003837Speter 				fvar -> value[ NL_OFFS ] ,
5013837Speter 				fvar -> extra_flags ,
5023837Speter 				fvartype );
5033192Smckusick 			break;
5043192Smckusick 		    default:
5053192Smckusick 			label = getlab();
5063192Smckusick 			sprintf( labelname , PREFIXFORMAT ,
5073192Smckusick 				LABELPREFIX , label );
5083192Smckusick 			putprintf( "	.data" , 0 );
5093192Smckusick 			putprintf( "	.lcomm	%s,%d" , 0 ,
5103192Smckusick 				    labelname , lwidth( fvar -> type ) );
5113192Smckusick 			putprintf( "	.text" , 0 );
5123192Smckusick 			putleaf( P2NAME , 0 , 0 , fvartype , labelname );
5133837Speter 			putLV( fvar -> symbol , ( fvar -> nl_block ) & 037 ,
5143837Speter 				fvar -> value[ NL_OFFS ] ,
5153837Speter 				fvar -> extra_flags ,
5163837Speter 				fvartype );
5173192Smckusick 			putstrop( P2STASG , fvartype , lwidth( fvar -> type ) ,
5183192Smckusick 				align( fvar -> type ) );
5193192Smckusick 			putdot( filename , line );
5203192Smckusick 			putleaf( P2ICON , 0 , 0 , fvartype , labelname );
5213192Smckusick 			break;
5223192Smckusick 		}
5233192Smckusick 		putop( P2FORCE , fvartype );
5243192Smckusick 		putdot( filename , line );
5253192Smckusick 	    }
5263192Smckusick 		/*
5273192Smckusick 		 *	restore old display entry from save area
5283192Smckusick 		 */
5293192Smckusick 
5303192Smckusick 	    putprintf( "	movq	%d(%s),%s+%d" , 0
5313192Smckusick 		, DSAVEOFFSET , P2FPNAME
5323192Smckusick 		, DISPLAYNAME , cbn * sizeof(struct dispsave) );
5333192Smckusick 	    stabrbrac( cbn );
5343192Smckusick 	    putprintf( "	ret" , 0 );
5353192Smckusick 		/*
5363192Smckusick 		 *	let the second pass allocate locals
5373279Smckusic 		 * 	and registers
5383192Smckusick 		 */
5393279Smckusic 	    putprintf( "	.set	" , 1 );
5403279Smckusic 	    putprintf( PREFIXFORMAT , 1 , LABELPREFIX , savlabel );
5413279Smckusic 	    putprintf( ", 0x%x" , 0 , savmask() );
5423192Smckusick 	    putlab( botlabel );
5433192Smckusick 	    putprintf( "	subl2	$LF%d,sp" , 0 , ftnno );
5443192Smckusick 	    putrbracket( ftnno );
5453192Smckusick 	    putjbr( toplabel );
5463192Smckusick 		/*
5473428Speter 		 *  put down the entry point for formal calls
5483428Speter 		 *  the arguments for FCALL have been passed to us
5493428Speter 		 *  as hidden parameters after the regular arguments.
5503428Speter 		 */
5513428Speter 	    if ( fp -> class != PROG ) {
5523428Speter 		putprintf( "%s%s:" , 0 , FORMALPREFIX , extname );
5533428Speter 		putprintf( "	.word	" , 1 );
5543428Speter 		putprintf( PREFIXFORMAT , 0 , LABELPREFIX , savlabel );
5553428Speter 		putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) ,
5563428Speter 			"_FCALL" );
5573428Speter 		putRV( 0 , cbn ,
5583428Speter 		    fp -> value[ NL_OFFS ] + sizeof( struct formalrtn * ) ,
5593837Speter 		    NPARAM ,
5603428Speter 		    P2PTR | P2STRTY );
5613837Speter 		putRV( 0 , cbn , fp -> value[ NL_OFFS ] ,
5623837Speter 			NPARAM , P2PTR|P2STRTY );
5633428Speter 		putop( P2LISTOP , P2INT );
5643428Speter 		putop( P2CALL , P2INT );
5653428Speter 		putdot( filename , line );
5663428Speter 		putjbr( botlabel );
5673428Speter 	    }
5683428Speter 		/*
5693192Smckusick 		 *	declare pcp counters, if any
5703192Smckusick 		 */
5713192Smckusick 	    if ( monflg && fp -> class == PROG ) {
5723192Smckusick 		putprintf( "	.data" , 0 );
5733192Smckusick 		putprintf( "	.comm	" , 1 );
5743192Smckusick 		putprintf( PCPCOUNT , 1 );
5753192Smckusick 		putprintf( ",%d" , 0 , ( cnts + 1 ) * sizeof (long) );
5763192Smckusick 		putprintf( "	.text" , 0 );
5773192Smckusick 	    }
5783192Smckusick #	endif PC
5793192Smckusick #ifdef DEBUG
5803192Smckusick 	dumpnl(fp->ptr[2], fp->symbol);
5813192Smckusick #endif
582*5654Slinton 
583*5654Slinton #ifdef OBJ
5843192Smckusick 	/*
585*5654Slinton 	 * save the namelist for the debugger pdx
586*5654Slinton 	 */
587*5654Slinton 
588*5654Slinton 	savenl(fp->ptr[2], fp->symbol);
589*5654Slinton #endif
590*5654Slinton 
591*5654Slinton 	/*
5923192Smckusick 	 * Restore the
5933192Smckusick 	 * (virtual) name list
5943192Smckusick 	 * position
5953192Smckusick 	 */
5963192Smckusick 	nlfree(fp->ptr[2]);
5973192Smckusick 	/*
5983192Smckusick 	 * Proc/func has been
5993192Smckusick 	 * resolved
6003192Smckusick 	 */
6013192Smckusick 	fp->nl_flags &= ~NFORWD;
6023192Smckusick 	/*
6033192Smckusick 	 * Patch the beg
6043192Smckusick 	 * of the proc/func to
6053192Smckusick 	 * the proper variable size
6063192Smckusick 	 */
6073192Smckusick 	if (Fp == NIL)
6083192Smckusick 		elineon();
6093192Smckusick #	ifdef OBJ
6103192Smckusick 	    patchfil(var, (long)(-sizes[cbn].om_max), 2);
6113192Smckusick #	endif OBJ
6123192Smckusick 	cbn--;
6133192Smckusick 	if (inpflist(fp->symbol)) {
6143192Smckusick 		opop('l');
6153192Smckusick 	}
6163192Smckusick }
6173363Speter 
6183363Speter #ifdef PC
6193363Speter     /*
6203363Speter      *	construct the long name of a function based on it's static nesting.
6213363Speter      *	into a caller-supplied buffer (that should be about BUFSIZ big).
6223363Speter      */
6233363Speter sextname( buffer , name , level )
6243363Speter     char	buffer[];
6253363Speter     char	*name;
6263363Speter     int		level;
6273363Speter {
6283363Speter     char	*starthere;
6293363Speter     int	i;
6303363Speter 
6313363Speter     starthere = &buffer[0];
6323363Speter     for ( i = 1 ; i < level ; i++ ) {
6333363Speter 	sprintf( starthere , EXTFORMAT , enclosing[ i ] );
6343363Speter 	starthere += strlen( enclosing[ i ] ) + 1;
6353363Speter     }
6363367Speter     sprintf( starthere , EXTFORMAT , name );
6373367Speter     starthere += strlen( name ) + 1;
6383363Speter     if ( starthere >= &buffer[ BUFSIZ ] ) {
6393363Speter 	panic( "sextname" );
6403363Speter     }
6413363Speter }
6423363Speter #endif PC
643