xref: /csrg-svn/usr.bin/pascal/src/fend.c (revision 5677)
13192Smckusick /* Copyright (c) 1979 Regents of the University of California */
23192Smckusick 
3*5677Smckusic static char sccsid[] = "@(#)fend.c 1.10 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 );
123*5677Smckusic 	    if ( opt ( 't' ) ) {
124*5677Smckusic 	        putprintf( "	pushl	$1" , 0 );
125*5677Smckusic 	    } else {
126*5677Smckusic 	        putprintf( "	pushl	$0" , 0 );
127*5677Smckusic 	    }
128*5677Smckusic 	    putprintf( "	calls	$1,_PCSTART" , 0 );
1293192Smckusick 	    putprintf( "	movl	4(ap),__argc" , 0 );
1303192Smckusick 	    putprintf( "	movl	8(ap),__argv" , 0 );
1313192Smckusick 	    putprintf( "	calls	$0,_program" , 0 );
1323192Smckusick 	    putprintf( "	calls	$0,_PCEXIT" , 0 );
1333192Smckusick 	    ftnno = fp -> entloc;
1343192Smckusick 	    putprintf( "	.text" , 0 );
1353192Smckusick 	    putprintf( "	.align	1" , 0 );
1363192Smckusick 	    putprintf( "	.globl	_program" , 0 );
1373192Smckusick 	    putprintf( "_program:" , 0 );
1383192Smckusick 	    stabfunc( "program" , fp -> class , bundle[1] , 0 );
1393192Smckusick 	} else {
1403192Smckusick 	    ftnno = fp -> entloc;
1413192Smckusick 	    putprintf( "	.text" , 0 );
1423192Smckusick 	    putprintf( "	.align	1" , 0 );
1433367Speter 	    sextname( extname , fp -> symbol , cbn - 1 );
1443428Speter 	    putprintf( "	.globl	%s%s" , 0 , FORMALPREFIX , extname );
1453363Speter 	    putprintf( "	.globl	%s" , 0 , extname );
1463363Speter 	    putprintf( "%s:" , 0 , extname );
1473192Smckusick 	    stabfunc( fp -> symbol , fp -> class , bundle[1] , cbn - 1 );
1483192Smckusick 	    for ( p = fp -> chain ; p != NIL ; p = p -> chain ) {
1493192Smckusick 		stabparam( p -> symbol , p2type( p -> type )
1503192Smckusick 			    , p -> value[ NL_OFFS ] , lwidth( p -> type ) );
1513192Smckusick 	    }
1523192Smckusick 	    if ( fp -> class == FUNC ) {
1533192Smckusick 		    /*
1543192Smckusick 		     *	stab the function variable
1553192Smckusick 		     */
1563192Smckusick 		p = fp -> ptr[ NL_FVAR ];
1573192Smckusick 		stablvar( p -> symbol , p2type( p -> type ) , cbn
1583192Smckusick 			, p -> value[ NL_OFFS ] , lwidth( p -> type ) );
1593192Smckusick 	    }
1603192Smckusick 		/*
1613192Smckusick 		 *	stab local variables
1623192Smckusick 		 *	rummage down hash chain links.
1633192Smckusick 		 */
1643192Smckusick 	    for ( i = 0 ; i <= 077 ; i++ ) {
1653192Smckusick 		for ( p = disptab[ i ] ; p != NIL ; p = p->nl_next) {
1663192Smckusick 		    if ( ( p -> nl_block & 037 ) != cbn ) {
1673192Smckusick 			break;
1683192Smckusick 		    }
1693192Smckusick 		    /*
1703192Smckusick 		     *	stab local variables
1713192Smckusick 		     *	that's named variables, but not params
1723192Smckusick 		     */
1733192Smckusick 		    if (   ( p -> symbol != NIL )
1743192Smckusick 			&& ( p -> class == VAR )
1753192Smckusick 			&& ( p -> value[ NL_OFFS ] < 0 ) ) {
1763192Smckusick 			stablvar( p -> symbol , p2type( p -> type ) , cbn
1773192Smckusick 			    , p -> value[ NL_OFFS ] , lwidth( p -> type ) );
1783192Smckusick 		    }
1793192Smckusick 		}
1803192Smckusick 	    }
1813192Smckusick 	}
1823192Smckusick 	stablbrac( cbn );
1833192Smckusick 	    /*
1843192Smckusick 	     *	register save mask
1853192Smckusick 	     */
1863279Smckusic 	putprintf( "	.word	" , 1 );
1873279Smckusic         putprintf( PREFIXFORMAT , 0 , LABELPREFIX , savlabel );
1883192Smckusick 	putjbr( botlabel );
1893192Smckusick 	putlab( toplabel );
1903192Smckusick 	if ( profflag ) {
1913192Smckusick 		/*
1923192Smckusick 		 *	call mcount for profiling
1933192Smckusick 		 */
1943649Speter 	    putprintf( "	moval	" , 1 );
1953649Speter 	    putprintf( PREFIXFORMAT , 1 , LABELPREFIX , proflabel );
1963649Speter 	    putprintf( ",r0" , 0 );
1973192Smckusick 	    putprintf( "	jsb	mcount" , 0 );
1983192Smckusick 	    putprintf( "	.data" , 0 );
1993192Smckusick 	    putprintf( "	.align	2" , 0 );
2003649Speter 	    putlab( proflabel );
2013192Smckusick 	    putprintf( "	.long	0" , 0 );
2023192Smckusick 	    putprintf( "	.text" , 0 );
2033192Smckusick 	}
2043192Smckusick 	    /*
2053192Smckusick 	     *	set up unwind exception vector.
2063192Smckusick 	     */
2073192Smckusick 	putprintf( "	moval	%s,%d(%s)" , 0
2083192Smckusick 		, UNWINDNAME , UNWINDOFFSET , P2FPNAME );
2093192Smckusick 	    /*
2103192Smckusick 	     *	save address of display entry, for unwind.
2113192Smckusick 	     */
2123192Smckusick 	putprintf( "	moval	%s+%d,%d(%s)" , 0
2133192Smckusick 		, DISPLAYNAME , cbn * sizeof(struct dispsave)
2143192Smckusick 		, DPTROFFSET , P2FPNAME );
2153192Smckusick 	    /*
2163192Smckusick 	     *	save old display
2173192Smckusick 	     */
2183192Smckusick 	putprintf( "	movq	%s+%d,%d(%s)" , 0
2193192Smckusick 		, DISPLAYNAME , cbn * sizeof(struct dispsave)
2203192Smckusick 		, DSAVEOFFSET , P2FPNAME );
2213192Smckusick 	    /*
2223192Smckusick 	     *	set up new display by saving AP and FP in appropriate
2233192Smckusick 	     *	slot in display structure.
2243192Smckusick 	     */
2253192Smckusick 	putprintf( "	movq	%s,%s+%d" , 0
2263192Smckusick 		, P2APNAME , DISPLAYNAME , cbn * sizeof(struct dispsave) );
2273192Smckusick 	    /*
228*5677Smckusic 	     *	set underflow checking if runtime tests
229*5677Smckusic 	     */
230*5677Smckusic 	if ( opt( 't' ) ) {
231*5677Smckusic 	    putprintf( "	bispsw	$0xe0" , 0 );
232*5677Smckusic 	}
233*5677Smckusic 	    /*
2343192Smckusick 	     *	ask second pass to allocate known locals
2353192Smckusick 	     */
2363192Smckusick 	putlbracket( ftnno , -sizes[ cbn ].om_max );
2373192Smckusick 	    /*
2383192Smckusick 	     *	and zero them if checking is on
2393192Smckusick 	     *	by calling blkclr( bytes of locals , starting local address );
2403192Smckusick 	     */
2413301Smckusic 	if ( opt( 't' ) && ( -sizes[ cbn ].om_max ) > DPOFF1 ) {
2423301Smckusic 	    putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR )
2433301Smckusic 		    , "_blkclr" );
2443301Smckusic 	    putleaf( P2ICON ,  ( -sizes[ cbn ].om_max ) - DPOFF1
2453301Smckusic 		    , 0 , P2INT , 0 );
2463837Speter 	    putLV( 0 , cbn , sizes[ cbn ].om_max , NLOCAL , P2CHAR );
2473301Smckusic 	    putop( P2LISTOP , P2INT );
2483301Smckusic 	    putop( P2CALL , P2INT );
2493301Smckusic 	    putdot( filename , line );
2503192Smckusick 	}
2513192Smckusick #endif PC
2523192Smckusick 	if ( monflg ) {
2533192Smckusick 		if ( fp -> value[ NL_CNTR ] != 0 ) {
2543192Smckusick 			inccnt( fp -> value [ NL_CNTR ] );
2553192Smckusick 		}
2563192Smckusick 		inccnt( bodycnts[ fp -> nl_block & 037 ] );
2573192Smckusick 	}
2583192Smckusick 	if (fp->class == PROG) {
2593192Smckusick 		/*
2603192Smckusick 		 * The glorious buffers option.
2613192Smckusick 		 *          0 = don't buffer output
2623192Smckusick 		 *          1 = line buffer output
2633192Smckusick 		 *          2 = 512 byte buffer output
2643192Smckusick 		 */
2653192Smckusick #		ifdef OBJ
2663192Smckusick 		    if (opt('b') != 1)
2673192Smckusick 			    put(1, O_BUFF | opt('b') << 8);
2683192Smckusick #		endif OBJ
2693192Smckusick #		ifdef PC
2703192Smckusick 		    if ( opt( 'b' ) != 1 ) {
2713192Smckusick 			putleaf( P2ICON , 0 , 0
2723192Smckusick 				, ADDTYPE( P2FTN | P2INT , P2PTR ) , "_BUFF" );
2733192Smckusick 			putleaf( P2ICON , opt( 'b' ) , 0 , P2INT , 0 );
2743192Smckusick 			putop( P2CALL , P2INT );
2753192Smckusick 			putdot( filename , line );
2763192Smckusick 		    }
2773192Smckusick #		endif PC
2783192Smckusick 		out = 0;
2793192Smckusick 		for (p = fp->chain; p != NIL; p = p->chain) {
2803192Smckusick 			if (strcmp(p->symbol, "input") == 0) {
2813192Smckusick 				inp++;
2823192Smckusick 				continue;
2833192Smckusick 			}
2843192Smckusick 			if (strcmp(p->symbol, "output") == 0) {
2853192Smckusick 				out++;
2863192Smckusick 				continue;
2873192Smckusick 			}
2883192Smckusick 			iop = lookup1(p->symbol);
2893192Smckusick 			if (iop == NIL || bn != cbn) {
2903192Smckusick 				error("File %s listed in program statement but not declared", p->symbol);
2913192Smckusick 				continue;
2923192Smckusick 			}
2933192Smckusick 			if (iop->class != VAR) {
2943192Smckusick 				error("File %s listed in program statement but declared as a %s", p->symbol, classes[iop->class]);
2953192Smckusick 				continue;
2963192Smckusick 			}
2973192Smckusick 			if (iop->type == NIL)
2983192Smckusick 				continue;
2993192Smckusick 			if (iop->type->class != FILET) {
3003192Smckusick 				error("File %s listed in program statement but defined as %s",
3013192Smckusick 					p->symbol, nameof(iop->type));
3023192Smckusick 				continue;
3033192Smckusick 			}
3043192Smckusick #			ifdef OBJ
3053192Smckusick 			    put(2, O_CON24, text(iop->type) ? 0 : width(iop->type->type));
3063192Smckusick 			    i = lenstr(p->symbol,0);
3073192Smckusick 			    put(2, O_CON24, i);
3083192Smckusick 			    put(2, O_LVCON, i);
3093192Smckusick 			    putstr(p->symbol, 0);
3103192Smckusick 			    put(2, O_LV | bn<<8+INDX, (int)iop->value[NL_OFFS]);
3113192Smckusick 			    put(1, O_DEFNAME);
3123192Smckusick #			endif OBJ
3133192Smckusick #			ifdef PC
3143192Smckusick 			    putleaf( P2ICON , 0 , 0
3153192Smckusick 				    , ADDTYPE( P2FTN | P2INT , P2PTR )
3163192Smckusick 				    , "_DEFNAME" );
3173837Speter 			    putLV( p -> symbol , bn , iop -> value[NL_OFFS] ,
3183837Speter 				    iop -> extra_flags , p2type( iop ) );
3193192Smckusick 			    putCONG( p -> symbol , strlen( p -> symbol )
3203192Smckusick 				    , LREQ );
3213192Smckusick 			    putop( P2LISTOP , P2INT );
3223192Smckusick 			    putleaf( P2ICON , strlen( p -> symbol )
3233192Smckusick 				    , 0 , P2INT , 0 );
3243192Smckusick 			    putop( P2LISTOP , P2INT );
3253192Smckusick 			    putleaf( P2ICON
3263192Smckusick 				, text(iop->type) ? 0 : width(iop->type->type)
3273192Smckusick 				, 0 , P2INT , 0 );
3283192Smckusick 			    putop( P2LISTOP , P2INT );
3293192Smckusick 			    putop( P2CALL , P2INT );
3303192Smckusick 			    putdot( filename , line );
3313192Smckusick #			endif PC
3323192Smckusick 		}
3333192Smckusick 		if (out == 0 && fp->chain != NIL) {
3343192Smckusick 			recovered();
3353192Smckusick 			error("The file output must appear in the program statement file list");
3363192Smckusick 		}
3373192Smckusick 	}
3383192Smckusick 	/*
3393192Smckusick 	 * Process the prog/proc/func body
3403192Smckusick 	 */
3413192Smckusick 	noreach = 0;
3423192Smckusick 	line = bundle[1];
3433192Smckusick 	statlist(blk);
3443192Smckusick #	ifdef PTREE
3453192Smckusick 	    {
3463192Smckusick 		pPointer Body = tCopy( blk );
3473192Smckusick 
3483192Smckusick 		pDEF( PorFHeader[ nesting -- ] ).PorFBody = Body;
3493192Smckusick 	    }
3503192Smckusick #	endif PTREE
3513192Smckusick #	ifdef OBJ
3523192Smckusick 	    if (cbn== 1 && monflg != 0) {
3533192Smckusick 		    patchfil(cntpatch - 2, (long)cnts, 2);
3543192Smckusick 		    patchfil(nfppatch - 2, (long)pfcnt, 2);
3553192Smckusick 	    }
3563192Smckusick #	endif OBJ
3573192Smckusick #	ifdef PC
3583192Smckusick 	    if ( fp -> class == PROG && monflg ) {
3593192Smckusick 		putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR )
3603192Smckusick 			, "_PMFLUSH" );
3613192Smckusick 		putleaf( P2ICON , cnts , 0 , P2INT , 0 );
3623192Smckusick 		putleaf( P2ICON , pfcnt , 0 , P2INT , 0 );
3633192Smckusick 		putop( P2LISTOP , P2INT );
3643837Speter 		putLV( PCPCOUNT , 0 , 0 , NGLOBAL , P2INT );
3653192Smckusick 		putop( P2LISTOP , P2INT );
3663192Smckusick 		putop( P2CALL , P2INT );
3673192Smckusick 		putdot( filename , line );
3683192Smckusick 	    }
3693192Smckusick #	endif PC
3703192Smckusick 	if (fp->class == PROG && inp == 0 && (input->nl_flags & (NUSED|NMOD)) != 0) {
3713192Smckusick 		recovered();
3723192Smckusick 		error("Input is used but not defined in the program statement");
3733192Smckusick 	}
3743192Smckusick 	/*
3753192Smckusick 	 * Clean up the symbol table displays and check for unresolves
3763192Smckusick 	 */
3773192Smckusick 	line = endline;
3783192Smckusick 	b = cbn;
3793192Smckusick 	Fp = fp;
3803192Smckusick 	chkref = syneflg == errcnt[cbn] && opt('w') == 0;
3813192Smckusick 	for (i = 0; i <= 077; i++) {
3823192Smckusick 		for (p = disptab[i]; p != NIL && (p->nl_block & 037) == b; p = p->nl_next) {
3833192Smckusick 			/*
3843192Smckusick 			 * Check for variables defined
3853192Smckusick 			 * but not referenced
3863192Smckusick 			 */
3873192Smckusick 			if (chkref && p->symbol != NIL)
3883192Smckusick 			switch (p->class) {
3893192Smckusick 				case FIELD:
3903192Smckusick 					/*
3913192Smckusick 					 * If the corresponding record is
3923192Smckusick 					 * unused, we shouldn't complain about
3933192Smckusick 					 * the fields.
3943192Smckusick 					 */
3953192Smckusick 				default:
3963192Smckusick 					if ((p->nl_flags & (NUSED|NMOD)) == 0) {
3973192Smckusick 						warning();
3983192Smckusick 						nerror("%s %s is neither used nor set", classes[p->class], p->symbol);
3993192Smckusick 						break;
4003192Smckusick 					}
4013192Smckusick 					/*
4023192Smckusick 					 * If a var parameter is either
4033192Smckusick 					 * modified or used that is enough.
4043192Smckusick 					 */
4053192Smckusick 					if (p->class == REF)
4063192Smckusick 						continue;
4073192Smckusick #					ifdef OBJ
4083192Smckusick 					    if ((p->nl_flags & NUSED) == 0) {
4093192Smckusick 						warning();
4103192Smckusick 						nerror("%s %s is never used", classes[p->class], p->symbol);
4113192Smckusick 						break;
4123192Smckusick 					    }
4133192Smckusick #					endif OBJ
4143192Smckusick #					ifdef PC
4153837Speter 					    if (((p->nl_flags & NUSED) == 0) && ((p->extra_flags & NEXTERN) == 0)) {
4163192Smckusick 						warning();
4173192Smckusick 						nerror("%s %s is never used", classes[p->class], p->symbol);
4183192Smckusick 						break;
4193192Smckusick 					    }
4203192Smckusick #					endif PC
4213192Smckusick 					if ((p->nl_flags & NMOD) == 0) {
4223192Smckusick 						warning();
4233192Smckusick 						nerror("%s %s is used but never set", classes[p->class], p->symbol);
4243192Smckusick 						break;
4253192Smckusick 					}
4263192Smckusick 				case LABEL:
4273192Smckusick 				case FVAR:
4283192Smckusick 				case BADUSE:
4293192Smckusick 					break;
4303192Smckusick 			}
4313192Smckusick 			switch (p->class) {
4323192Smckusick 				case BADUSE:
4333192Smckusick 					cp = "s";
4343192Smckusick 					if (p->chain->ud_next == NIL)
4353192Smckusick 						cp++;
4363192Smckusick 					eholdnl();
4373192Smckusick 					if (p->value[NL_KINDS] & ISUNDEF)
4383192Smckusick 						nerror("%s undefined on line%s", p->symbol, cp);
4393192Smckusick 					else
4403192Smckusick 						nerror("%s improperly used on line%s", p->symbol, cp);
4413192Smckusick 					pnumcnt = 10;
4423192Smckusick 					pnums(p->chain);
4433192Smckusick 					pchr('\n');
4443192Smckusick 					break;
4453192Smckusick 
4463192Smckusick 				case FUNC:
4473192Smckusick 				case PROC:
4483192Smckusick #					ifdef OBJ
4493192Smckusick 					    if ((p->nl_flags & NFORWD))
4503192Smckusick 						nerror("Unresolved forward declaration of %s %s", classes[p->class], p->symbol);
4513192Smckusick #					endif OBJ
4523192Smckusick #					ifdef PC
4533837Speter 					    if ((p->nl_flags & NFORWD) && ((p->extra_flags & NEXTERN) == 0))
4543192Smckusick 						nerror("Unresolved forward declaration of %s %s", classes[p->class], p->symbol);
4553192Smckusick #					endif PC
4563192Smckusick 					break;
4573192Smckusick 
4583192Smckusick 				case LABEL:
4593192Smckusick 					if (p->nl_flags & NFORWD)
4603192Smckusick 						nerror("label %s was declared but not defined", p->symbol);
4613192Smckusick 					break;
4623192Smckusick 				case FVAR:
4633192Smckusick 					if ((p->nl_flags & NMOD) == 0)
4643192Smckusick 						nerror("No assignment to the function variable");
4653192Smckusick 					break;
4663192Smckusick 			}
4673192Smckusick 		}
4683192Smckusick 		/*
4693192Smckusick 		 * Pop this symbol
4703192Smckusick 		 * table slot
4713192Smckusick 		 */
4723192Smckusick 		disptab[i] = p;
4733192Smckusick 	}
4743192Smckusick 
4753192Smckusick #	ifdef OBJ
4763192Smckusick 	    put(1, O_END);
4773192Smckusick #	endif OBJ
4783192Smckusick #	ifdef PC
4793192Smckusick 		/*
4803192Smckusick 		 *	if there were file variables declared at this level
4813192Smckusick 		 *	call pclose( &__disply[ cbn ] ) to clean them up.
4823192Smckusick 		 */
4833192Smckusick 	    if ( dfiles[ cbn ] ) {
4843192Smckusick 		putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR )
4853192Smckusick 			, "_PCLOSE" );
4863837Speter 		putRV( DISPLAYNAME , 0 , cbn * sizeof( struct dispsave ) ,
4873837Speter 			NGLOBAL , P2PTR | P2CHAR );
4883192Smckusick 		putop( P2CALL , P2INT );
4893192Smckusick 		putdot( filename , line );
4903192Smckusick 	    }
4913192Smckusick 		/*
4923192Smckusick 		 *	if this is a function,
4933192Smckusick 		 *	the function variable is the return value.
4943192Smckusick 		 *	if it's a scalar valued function, return scalar,
4953192Smckusick 		 *	else, return a pointer to the structure value.
4963192Smckusick 		 */
4973192Smckusick 	    if ( fp -> class == FUNC ) {
4983192Smckusick 		struct nl	*fvar = fp -> ptr[ NL_FVAR ];
4993192Smckusick 		long		fvartype = p2type( fvar -> type );
5003192Smckusick 		long		label;
5013192Smckusick 		char		labelname[ BUFSIZ ];
5023192Smckusick 
5033192Smckusick 		switch ( classify( fvar -> type ) ) {
5043192Smckusick 		    case TBOOL:
5053192Smckusick 		    case TCHAR:
5063192Smckusick 		    case TINT:
5073192Smckusick 		    case TSCAL:
5083192Smckusick 		    case TDOUBLE:
5093192Smckusick 		    case TPTR:
5103837Speter 			putRV( fvar -> symbol , ( fvar -> nl_block ) & 037 ,
5113837Speter 				fvar -> value[ NL_OFFS ] ,
5123837Speter 				fvar -> extra_flags ,
5133837Speter 				fvartype );
5143192Smckusick 			break;
5153192Smckusick 		    default:
5163192Smckusick 			label = getlab();
5173192Smckusick 			sprintf( labelname , PREFIXFORMAT ,
5183192Smckusick 				LABELPREFIX , label );
5193192Smckusick 			putprintf( "	.data" , 0 );
5203192Smckusick 			putprintf( "	.lcomm	%s,%d" , 0 ,
5213192Smckusick 				    labelname , lwidth( fvar -> type ) );
5223192Smckusick 			putprintf( "	.text" , 0 );
5233192Smckusick 			putleaf( P2NAME , 0 , 0 , fvartype , labelname );
5243837Speter 			putLV( fvar -> symbol , ( fvar -> nl_block ) & 037 ,
5253837Speter 				fvar -> value[ NL_OFFS ] ,
5263837Speter 				fvar -> extra_flags ,
5273837Speter 				fvartype );
5283192Smckusick 			putstrop( P2STASG , fvartype , lwidth( fvar -> type ) ,
5293192Smckusick 				align( fvar -> type ) );
5303192Smckusick 			putdot( filename , line );
5313192Smckusick 			putleaf( P2ICON , 0 , 0 , fvartype , labelname );
5323192Smckusick 			break;
5333192Smckusick 		}
5343192Smckusick 		putop( P2FORCE , fvartype );
5353192Smckusick 		putdot( filename , line );
5363192Smckusick 	    }
5373192Smckusick 		/*
5383192Smckusick 		 *	restore old display entry from save area
5393192Smckusick 		 */
5403192Smckusick 
5413192Smckusick 	    putprintf( "	movq	%d(%s),%s+%d" , 0
5423192Smckusick 		, DSAVEOFFSET , P2FPNAME
5433192Smckusick 		, DISPLAYNAME , cbn * sizeof(struct dispsave) );
5443192Smckusick 	    stabrbrac( cbn );
5453192Smckusick 	    putprintf( "	ret" , 0 );
5463192Smckusick 		/*
5473192Smckusick 		 *	let the second pass allocate locals
5483279Smckusic 		 * 	and registers
5493192Smckusick 		 */
5503279Smckusic 	    putprintf( "	.set	" , 1 );
5513279Smckusic 	    putprintf( PREFIXFORMAT , 1 , LABELPREFIX , savlabel );
5523279Smckusic 	    putprintf( ", 0x%x" , 0 , savmask() );
5533192Smckusick 	    putlab( botlabel );
5543192Smckusick 	    putprintf( "	subl2	$LF%d,sp" , 0 , ftnno );
5553192Smckusick 	    putrbracket( ftnno );
5563192Smckusick 	    putjbr( toplabel );
5573192Smckusick 		/*
5583428Speter 		 *  put down the entry point for formal calls
5593428Speter 		 *  the arguments for FCALL have been passed to us
5603428Speter 		 *  as hidden parameters after the regular arguments.
5613428Speter 		 */
5623428Speter 	    if ( fp -> class != PROG ) {
5633428Speter 		putprintf( "%s%s:" , 0 , FORMALPREFIX , extname );
5643428Speter 		putprintf( "	.word	" , 1 );
5653428Speter 		putprintf( PREFIXFORMAT , 0 , LABELPREFIX , savlabel );
5663428Speter 		putleaf( P2ICON , 0 , 0 , ADDTYPE( P2FTN | P2INT , P2PTR ) ,
5673428Speter 			"_FCALL" );
5683428Speter 		putRV( 0 , cbn ,
5693428Speter 		    fp -> value[ NL_OFFS ] + sizeof( struct formalrtn * ) ,
5703837Speter 		    NPARAM ,
5713428Speter 		    P2PTR | P2STRTY );
5723837Speter 		putRV( 0 , cbn , fp -> value[ NL_OFFS ] ,
5733837Speter 			NPARAM , P2PTR|P2STRTY );
5743428Speter 		putop( P2LISTOP , P2INT );
5753428Speter 		putop( P2CALL , P2INT );
5763428Speter 		putdot( filename , line );
5773428Speter 		putjbr( botlabel );
5783428Speter 	    }
5793428Speter 		/*
5803192Smckusick 		 *	declare pcp counters, if any
5813192Smckusick 		 */
5823192Smckusick 	    if ( monflg && fp -> class == PROG ) {
5833192Smckusick 		putprintf( "	.data" , 0 );
5843192Smckusick 		putprintf( "	.comm	" , 1 );
5853192Smckusick 		putprintf( PCPCOUNT , 1 );
5863192Smckusick 		putprintf( ",%d" , 0 , ( cnts + 1 ) * sizeof (long) );
5873192Smckusick 		putprintf( "	.text" , 0 );
5883192Smckusick 	    }
5893192Smckusick #	endif PC
5903192Smckusick #ifdef DEBUG
5913192Smckusick 	dumpnl(fp->ptr[2], fp->symbol);
5923192Smckusick #endif
5935654Slinton 
5945654Slinton #ifdef OBJ
5953192Smckusick 	/*
5965654Slinton 	 * save the namelist for the debugger pdx
5975654Slinton 	 */
5985654Slinton 
5995654Slinton 	savenl(fp->ptr[2], fp->symbol);
6005654Slinton #endif
6015654Slinton 
6025654Slinton 	/*
6033192Smckusick 	 * Restore the
6043192Smckusick 	 * (virtual) name list
6053192Smckusick 	 * position
6063192Smckusick 	 */
6073192Smckusick 	nlfree(fp->ptr[2]);
6083192Smckusick 	/*
6093192Smckusick 	 * Proc/func has been
6103192Smckusick 	 * resolved
6113192Smckusick 	 */
6123192Smckusick 	fp->nl_flags &= ~NFORWD;
6133192Smckusick 	/*
6143192Smckusick 	 * Patch the beg
6153192Smckusick 	 * of the proc/func to
6163192Smckusick 	 * the proper variable size
6173192Smckusick 	 */
6183192Smckusick 	if (Fp == NIL)
6193192Smckusick 		elineon();
6203192Smckusick #	ifdef OBJ
6213192Smckusick 	    patchfil(var, (long)(-sizes[cbn].om_max), 2);
6223192Smckusick #	endif OBJ
6233192Smckusick 	cbn--;
6243192Smckusick 	if (inpflist(fp->symbol)) {
6253192Smckusick 		opop('l');
6263192Smckusick 	}
6273192Smckusick }
6283363Speter 
6293363Speter #ifdef PC
6303363Speter     /*
6313363Speter      *	construct the long name of a function based on it's static nesting.
6323363Speter      *	into a caller-supplied buffer (that should be about BUFSIZ big).
6333363Speter      */
6343363Speter sextname( buffer , name , level )
6353363Speter     char	buffer[];
6363363Speter     char	*name;
6373363Speter     int		level;
6383363Speter {
6393363Speter     char	*starthere;
6403363Speter     int	i;
6413363Speter 
6423363Speter     starthere = &buffer[0];
6433363Speter     for ( i = 1 ; i < level ; i++ ) {
6443363Speter 	sprintf( starthere , EXTFORMAT , enclosing[ i ] );
6453363Speter 	starthere += strlen( enclosing[ i ] ) + 1;
6463363Speter     }
6473367Speter     sprintf( starthere , EXTFORMAT , name );
6483367Speter     starthere += strlen( name ) + 1;
6493363Speter     if ( starthere >= &buffer[ BUFSIZ ] ) {
6503363Speter 	panic( "sextname" );
6513363Speter     }
6523363Speter }
6533363Speter #endif PC
654