xref: /csrg-svn/old/pcc/lint/lpass2/lpass2.c (revision 30938)
17990Srrh #ifndef lint
2*30938Sbostic static char sccsid[] = "@(#)lpass2.c	1.7	(Berkeley)	04/22/87";
37990Srrh #endif lint
47990Srrh 
518599Sralph # include "macdefs.h"
618599Sralph # include "manifest.h"
718599Sralph # include "lmanifest.h"
87990Srrh 
97990Srrh # define USED 01
107990Srrh # define VUSED 02
117990Srrh # define EUSED 04
127990Srrh # define RVAL 010
137990Srrh # define VARARGS 0100
147990Srrh 
1526319Skarels # define NSZ 4096
167990Srrh # define TYSZ 3500
1711550Ssam # define FSZ 500
187990Srrh # define NTY 50
197990Srrh 
207990Srrh typedef struct sty STYPE;
217990Srrh struct sty { ATYPE t; STYPE *next; };
227990Srrh 
237990Srrh typedef struct sym {
247990Srrh #ifndef FLEXNAMES
257990Srrh 	char name[LCHNM];
267990Srrh #else
277990Srrh 	char *name;
287990Srrh #endif
297990Srrh 	short nargs;
307990Srrh 	int decflag;
317990Srrh 	int fline;
327990Srrh 	STYPE symty;
337990Srrh 	int fno;
347990Srrh 	int use;
357990Srrh 	} STAB;
367990Srrh 
377990Srrh STAB stab[NSZ];
387990Srrh STAB *find();
397990Srrh 
407990Srrh STYPE tary[TYSZ];
417990Srrh STYPE *tget();
427990Srrh 
437990Srrh #ifndef FLEXNAMES
447990Srrh char fnm[FSZ][LFNM];
457990Srrh #else
467990Srrh char *fnm[FSZ];
477990Srrh #endif
487990Srrh 
497990Srrh #ifdef FLEXNAMES
507990Srrh char *getstr();
517990Srrh #endif
527990Srrh 
537990Srrh int tfree;  /* used to allocate types */
547990Srrh int ffree;  /* used to save filenames */
557990Srrh 
567990Srrh struct ty atyp[NTY];
577990Srrh 	/* r is where all the input ends up */
587990Srrh union rec r;
597990Srrh 
607990Srrh int hflag = 0;
617990Srrh int pflag = 0;
627990Srrh int xflag = 0;
637990Srrh int uflag = 1;
647990Srrh int ddddd = 0;
6511762Sedward int zflag = 0;
6611762Sedward int Pflag = 0;
677990Srrh 
687990Srrh int cfno;  /* current file number */
697990Srrh 
707990Srrh main( argc, argv ) char *argv[]; {
717990Srrh 	register char *p;
727990Srrh 
737990Srrh 	/* first argument is intermediate file */
747990Srrh 	/* second argument is - options */
757990Srrh 
767990Srrh 	for( ; argc>2 && argv[argc-1][0] == '-' ; --argc ){
777990Srrh 		for( p=argv[argc-1]; *p; ++p ){
787990Srrh 			switch( *p ){
797990Srrh 
807990Srrh 			case 'h':
817990Srrh 				hflag = 1;
827990Srrh 				break;
837990Srrh 
847990Srrh 			case 'p':
857990Srrh 				pflag = 1;
867990Srrh 				break;
877990Srrh 
887990Srrh 			case 'x':
897990Srrh 				xflag = 1;
907990Srrh 				break;
917990Srrh 
927990Srrh 			case 'X':
937990Srrh 				ddddd = 1;
947990Srrh 				break;
957990Srrh 
967990Srrh 			case 'u':
977990Srrh 				uflag = 0;
987990Srrh 				break;
997990Srrh 
10011762Sedward 			case 'z':
10111762Sedward 				zflag = 1;
10211762Sedward 				break;
10311762Sedward 
10411762Sedward 			case 'P':
10511762Sedward 				Pflag = 1;
10611762Sedward 				break;
10711762Sedward 
1087990Srrh 				}
1097990Srrh 			}
1107990Srrh 		}
1117990Srrh 
1127990Srrh 	if( argc < 2 || !freopen( argv[1], "r", stdin ) ){
1137990Srrh 		error( "cannot open intermediate file" );
1147990Srrh 		exit( 1 );
1157990Srrh 		}
11611762Sedward 	if( Pflag ){
11711762Sedward 		pfile();
11811762Sedward 		return( 0 );
11911762Sedward 		}
12011762Sedward 	mloop( LDI|LIB|LST );
1217990Srrh 	rewind( stdin );
1227990Srrh 	mloop( LDC|LDX );
1237990Srrh 	rewind( stdin );
1247990Srrh 	mloop( LRV|LUV|LUE|LUM );
1257990Srrh 	cleanup();
1267990Srrh 	return(0);
1277990Srrh 	}
1287990Srrh 
1297990Srrh mloop( m ){
1307990Srrh 	/* do the main loop */
1317990Srrh 	register STAB *q;
1327990Srrh 
1337990Srrh 	while( lread(m) ){
1347990Srrh 		q = find();
1357990Srrh 		if( q->decflag ) chkcompat(q);
1367990Srrh 		else setuse(q);
1377990Srrh 		}
1387990Srrh 	}
1397990Srrh 
1407990Srrh lread(m){ /* read a line into r.l */
1417990Srrh 
1427990Srrh 	register n;
1437990Srrh 
1447990Srrh 	for(;;) {
1457990Srrh 		if( fread( (char *)&r, sizeof(r), 1, stdin ) <= 0 ) return(0);
1467990Srrh 		if( r.l.decflag & LFN ){
1477990Srrh 			/* new filename */
1487990Srrh #ifdef FLEXNAMES
149*30938Sbostic 			r.f.fn = getstr(0);
1507990Srrh #endif
15111762Sedward 			if( Pflag ) return( 1 );
1527990Srrh 			setfno( r.f.fn );
1537990Srrh 			continue;
1547990Srrh 			}
1557990Srrh #ifdef FLEXNAMES
156*30938Sbostic 		r.l.name = getstr(1);
157*30938Sbostic #else /* !FLEXNAMES */
158*30938Sbostic 		portify(r.l.name);
159*30938Sbostic #endif /* !FLEXNAMES */
1607990Srrh 		n = r.l.nargs;
1617990Srrh 		if( n<0 ) n = -n;
16211762Sedward 		if( n>=NTY ) error( "more than %d args?", n );
16311762Sedward 		fread( (char *)atyp, sizeof(ATYPE), n, stdin );
1647990Srrh 		if( ( r.l.decflag & m ) ) return( 1 );
1657990Srrh 		}
1667990Srrh 	}
1677990Srrh 
1687990Srrh setfno( s ) char *s; {
1697990Srrh 	/* look up current file names */
1707990Srrh 	/* first, strip backwards to the beginning or to the first / */
1717990Srrh 	int i;
1727990Srrh 
1737990Srrh 	/* now look up s */
1747990Srrh 	for( i=0; i<ffree; ++i ){
1757990Srrh #ifndef FLEXNAMES
17611762Sedward 		if( !strncmp( s, fnm[i], LFNM ) )
1777990Srrh #else
17811762Sedward 		if (fnm[i] == s)
1797990Srrh #endif
18011762Sedward 			{
1817990Srrh 			cfno = i;
1827990Srrh 			return;
1837990Srrh 			}
1847990Srrh 		}
1857990Srrh 	/* make a new entry */
1867990Srrh 	if( ffree >= FSZ ) error( "more than %d files", FSZ );
1877990Srrh #ifndef FLEXNAMES
1887990Srrh 	strncpy( fnm[ffree], s, LFNM );
1897990Srrh #else
1907990Srrh 	fnm[ffree] = s;
1917990Srrh #endif
1927990Srrh 	cfno = ffree++;
1937990Srrh 	}
1947990Srrh 
1957990Srrh /* VARARGS */
1967990Srrh error( s, a ) char *s; {
1977990Srrh 
1987990Srrh #ifndef FLEXNAMES
1997990Srrh 	fprintf( stderr, "pass 2 error:(file %.*s) ", LFNM, fnm[cfno] );
2007990Srrh #else
2017990Srrh 	fprintf( stderr, "pass 2 error:(file %s) ", fnm[cfno] );
2027990Srrh #endif
2037990Srrh 	fprintf( stderr, s, a );
2047990Srrh 	fprintf( stderr, "\n" );
2057990Srrh 	exit(1);
2067990Srrh 	}
2077990Srrh 
2087990Srrh STAB *
2097990Srrh find(){
2107990Srrh 	register h=0;
2117990Srrh #ifndef FLEXNAMES
21211762Sedward 	h = hashstr(r.l.name, LCHNM) % NSZ;
2137990Srrh #else
21411762Sedward 	h = (int)r.l.name % NSZ;
2157990Srrh #endif
2167990Srrh 	{	register STAB *p, *q;
2177990Srrh 		for( p=q= &stab[h]; q->decflag; ){
2187990Srrh #ifndef FLEXNAMES
21911762Sedward 			if( !strncmp( r.l.name, q->name, LCHNM))
2207990Srrh #else
22111762Sedward 			if (r.l.name == q->name)
2227990Srrh #endif
22311762Sedward 				if( ((q->decflag|r.l.decflag)&LST)==0 || q->fno==cfno )
22411762Sedward 					return(q);
2257990Srrh 			if( ++q >= &stab[NSZ] ) q = stab;
2267990Srrh 			if( q == p ) error( "too many names defined" );
2277990Srrh 			}
2287990Srrh #ifndef FLEXNAMES
2297990Srrh 		strncpy( q->name, r.l.name, LCHNM );
2307990Srrh #else
2317990Srrh 		q->name = r.l.name;
2327990Srrh #endif
2337990Srrh 		return( q );
2347990Srrh 		}
2357990Srrh 	}
2367990Srrh 
2377990Srrh STYPE *
2387990Srrh tget(){
2397990Srrh 	if( tfree >= TYSZ ){
2407990Srrh 		error( "too many types needed" );
2417990Srrh 		}
2427990Srrh 	return( &tary[tfree++] );
2437990Srrh 	}
2447990Srrh 
2457990Srrh chkcompat(q) STAB *q; {
2467990Srrh 	/* are the types, etc. in r.l and q compatible */
2477990Srrh 	register int i;
2487990Srrh 	STYPE *qq;
2497990Srrh 
2507990Srrh 	setuse(q);
2517990Srrh 
2527990Srrh 	/* argument check */
2537990Srrh 
25411762Sedward 	if( q->decflag & (LDI|LIB|LUV|LUE|LST) ){
2557990Srrh 		if( r.l.decflag & (LUV|LIB|LUE) ){
2567990Srrh 			if( q->nargs != r.l.nargs ){
2577990Srrh 				if( !(q->use&VARARGS) ){
2587990Srrh #ifndef FLEXNAMES
2597990Srrh 					printf( "%.8s: variable # of args.", q->name );
2607990Srrh #else
2617990Srrh 					printf( "%s: variable # of args.", q->name );
2627990Srrh #endif
2637990Srrh 					viceversa(q);
2647990Srrh 					}
2657990Srrh 				if( r.l.nargs > q->nargs ) r.l.nargs = q->nargs;
26611762Sedward 				if( !(q->decflag & (LDI|LIB|LST) ) ) {
2677990Srrh 					q->nargs = r.l.nargs;
2687990Srrh 					q->use |= VARARGS;
2697990Srrh 					}
2707990Srrh 				}
2717990Srrh 			for( i=0,qq=q->symty.next; i<r.l.nargs; ++i,qq=qq->next){
2727990Srrh 				if( chktype( &qq->t, &atyp[i] ) ){
2737990Srrh #ifndef FLEXNAMES
2747990Srrh 					printf( "%.8s, arg. %d used inconsistently",
2757990Srrh #else
2767990Srrh 					printf( "%s, arg. %d used inconsistently",
2777990Srrh #endif
2787990Srrh 						q->name, i+1 );
2797990Srrh 					viceversa(q);
2807990Srrh 					}
2817990Srrh 				}
2827990Srrh 			}
2837990Srrh 		}
2847990Srrh 
28511762Sedward 	if( (q->decflag&(LDI|LIB|LUV|LST)) && r.l.decflag==LUV ){
2867990Srrh 		if( chktype( &r.l.type, &q->symty.t ) ){
2877990Srrh #ifndef FLEXNAMES
2887990Srrh 			printf( "%.8s value used inconsistently", q->name );
2897990Srrh #else
2907990Srrh 			printf( "%s value used inconsistently", q->name );
2917990Srrh #endif
2927990Srrh 			viceversa(q);
2937990Srrh 			}
2947990Srrh 		}
2957990Srrh 
2967990Srrh 	/* check for multiple declaration */
2977990Srrh 
29811762Sedward 	if( (q->decflag&(LDI|LST)) && (r.l.decflag&(LDI|LIB|LST)) ){
2997990Srrh #ifndef FLEXNAMES
3007990Srrh 		printf( "%.8s multiply declared", q->name );
3017990Srrh #else
3027990Srrh 		printf( "%s multiply declared", q->name );
3037990Srrh #endif
3047990Srrh 		viceversa(q);
3057990Srrh 		}
3067990Srrh 
3077990Srrh 	/* do a bit of checking of definitions and uses... */
3087990Srrh 
30911762Sedward 	if( (q->decflag & (LDI|LIB|LDX|LDC|LUM|LST)) && (r.l.decflag & (LDX|LDC|LUM)) && q->symty.t.aty != r.l.type.aty ){
3107990Srrh #ifndef FLEXNAMES
3117990Srrh 		printf( "%.8s value declared inconsistently", q->name );
3127990Srrh #else
3137990Srrh 		printf( "%s value declared inconsistently", q->name );
3147990Srrh #endif
3157990Srrh 		viceversa(q);
3167990Srrh 		}
3177990Srrh 
3187990Srrh 	/* better not call functions which are declared to be structure or union returning */
3197990Srrh 
32011762Sedward 	if( (q->decflag & (LDI|LIB|LDX|LDC|LST)) && (r.l.decflag & LUE) && q->symty.t.aty != r.l.type.aty ){
3217990Srrh 		/* only matters if the function returns union or structure */
3227990Srrh 		TWORD ty;
3237990Srrh 		ty = q->symty.t.aty;
3247990Srrh 		if( ISFTN(ty) && ((ty = DECREF(ty))==STRTY || ty==UNIONTY ) ){
3257990Srrh #ifndef FLEXNAMES
3267990Srrh 			printf( "%.8s function value type must be declared before use", q->name );
3277990Srrh #else
3287990Srrh 			printf( "%s function value type must be declared before use", q->name );
3297990Srrh #endif
3307990Srrh 			viceversa(q);
3317990Srrh 			}
3327990Srrh 		}
3337990Srrh 
3347990Srrh 	if( pflag && q->decflag==LDX && r.l.decflag == LUM && !ISFTN(q->symty.t.aty) ){
3357990Srrh 		/* make the external declaration go away */
3367990Srrh 		/* in effect, it was used without being defined */
3377990Srrh 		}
3387990Srrh 	}
3397990Srrh 
3407990Srrh viceversa(q) STAB *q; {
3417990Srrh 	/* print out file comparison */
3427990Srrh #ifndef FLEXNAMES
3437990Srrh 	printf( "	%.*s(%d)  ::  %.*s(%d)\n",
3447990Srrh 		LFNM, fnm[q->fno], q->fline,
3457990Srrh 		LFNM, fnm[cfno], r.l.fline );
3467990Srrh #else
3477990Srrh 	printf( "	%s(%d)  ::  %s(%d)\n",
3487990Srrh 		fnm[q->fno], q->fline,
3497990Srrh 		fnm[cfno], r.l.fline );
3507990Srrh #endif
3517990Srrh 	}
3527990Srrh 
3537990Srrh 	/* messages for defintion/use */
3547990Srrh char *
3557990Srrh mess[2][2] ={
3567990Srrh 	"",
3577990Srrh #ifndef FLEXNAMES
3587990Srrh 	"%.8s used( %.*s(%d) ), but not defined\n",
3597990Srrh 	"%.8s defined( %.*s(%d) ), but never used\n",
3607990Srrh 	"%.8s declared( %.*s(%d) ), but never used or defined\n"
3617990Srrh #else
3627990Srrh 	"%s used( %s(%d) ), but not defined\n",
3637990Srrh 	"%s defined( %s(%d) ), but never used\n",
3647990Srrh 	"%s declared( %s(%d) ), but never used or defined\n"
3657990Srrh #endif
3667990Srrh 	};
3677990Srrh 
3687990Srrh lastone(q) STAB *q; {
3697990Srrh 
3707990Srrh 	register nu, nd, uses;
3717990Srrh 
3727990Srrh 	if( ddddd ) pst(q);
3737990Srrh 
3747990Srrh 	nu = nd = 0;
3757990Srrh 	uses = q->use;
3767990Srrh 
3777990Srrh 	if( !(uses&USED) && q->decflag != LIB ) {
3787990Srrh #ifndef FLEXNAMES
3797990Srrh 		if( strncmp(q->name,"main",7) )
3807990Srrh #else
3817990Srrh 		if (strcmp(q->name, "main"))
3827990Srrh #endif
3837990Srrh 			nu = 1;
3847990Srrh 		}
3857990Srrh 
3867990Srrh 	if( !ISFTN(q->symty.t.aty) ){
3877990Srrh 		switch( q->decflag ){
3887990Srrh 
3897990Srrh 		case LIB:
3907990Srrh 			nu = nd = 0;  /* don't complain about uses on libraries */
3917990Srrh 			break;
3927990Srrh 		case LDX:
3937990Srrh 			if( !xflag ) break;
3947990Srrh 		case LUV:
3957990Srrh 		case LUE:
3967990Srrh /* 01/04/80 */	case LUV | LUE:
3977990Srrh 		case LUM:
3987990Srrh 			nd = 1;
3997990Srrh 			}
4007990Srrh 		}
40111762Sedward 	if( uflag && ( nu || nd ) )
4027990Srrh #ifndef FLEXNAMES
40311762Sedward 		printf( mess[nu][nd], q->name, LFNM, fnm[q->fno], q->fline );
4047990Srrh #else
40511762Sedward 		printf( mess[nu][nd], q->name, fnm[q->fno], q->fline );
4067990Srrh #endif
4077990Srrh 
4087990Srrh 	if( (uses&(RVAL+EUSED)) == (RVAL+EUSED) ){
40911762Sedward 		/* if functions is static, then print the file name too */
41011762Sedward 		if( q->decflag & LST )
4117990Srrh #ifndef FLEXNAMES
41211762Sedward 			printf( "%.*s(%d):", LFNM, fnm[q->fno], q->fline );
4137990Srrh #else
41411762Sedward 			printf( "%s(%d):", fnm[q->fno], q->fline );
4157990Srrh #endif
41611762Sedward #ifndef FLEXNAMES
41711762Sedward 		printf( "%.*s returns value which is %s ignored\n",
41811762Sedward 			LCHNM, q->name, uses&VUSED ? "sometimes" : "always" );
41911762Sedward #else
42011762Sedward 		printf( "%s returns value which is %s ignored\n",
42111762Sedward 			q->name, uses&VUSED ? "sometimes" : "always" );
42211762Sedward #endif
4237990Srrh 		}
4247990Srrh 
42511762Sedward 	if( (uses&(RVAL+VUSED)) == (VUSED) && (q->decflag&(LDI|LIB|LST)) ){
42611762Sedward 		if( q->decflag & LST )
4277990Srrh #ifndef FLEXNAMES
42811762Sedward 			printf( "%.*s(%d):", LFNM, fnm[q->fno], q->fline );
4297990Srrh #else
43011762Sedward 			printf( "%s(%d):", fnm[q->fno], q->fline );
4317990Srrh #endif
43211762Sedward #ifndef FLEXNAMES
43311762Sedward 		printf( "%.*s value is used, but none returned\n",
43411762Sedward 			LCHNM, q->name);
43511762Sedward #else
43611762Sedward 		printf( "%s value is used, but none returned\n", q->name);
43711762Sedward #endif
4387990Srrh 		}
4397990Srrh 	}
4407990Srrh 
4417990Srrh cleanup(){ /* call lastone and die gracefully */
4427990Srrh 	STAB *q;
4437990Srrh 	for( q=stab; q< &stab[NSZ]; ++q ){
4447990Srrh 		if( q->decflag ) lastone(q);
4457990Srrh 		}
4467990Srrh 	exit(0);
4477990Srrh 	}
4487990Srrh 
4497990Srrh setuse(q) STAB *q; { /* check new type to ensure that it is used */
4507990Srrh 
4517990Srrh 	if( !q->decflag ){ /* new one */
4527990Srrh 		q->decflag = r.l.decflag;
4537990Srrh 		q->symty.t = r.l.type;
4547990Srrh 		if( r.l.nargs < 0 ){
4557990Srrh 			q->nargs = -r.l.nargs;
4567990Srrh 			q->use = VARARGS;
4577990Srrh 			}
4587990Srrh 		else {
4597990Srrh 			q->nargs = r.l.nargs;
4607990Srrh 			q->use = 0;
4617990Srrh 			}
4627990Srrh 		q->fline = r.l.fline;
4637990Srrh 		q->fno = cfno;
4647990Srrh 		if( q->nargs ){
4657990Srrh 			int i;
4667990Srrh 			STYPE *qq;
4677990Srrh 			for( i=0,qq= &q->symty; i<q->nargs; ++i,qq=qq->next ){
4687990Srrh 				qq->next = tget();
4697990Srrh 				qq->next->t = atyp[i];
4707990Srrh 				}
4717990Srrh 			}
4727990Srrh 		}
4737990Srrh 
4747990Srrh 	switch( r.l.decflag ){
4757990Srrh 
4767990Srrh 	case LRV:
4777990Srrh 		q->use |= RVAL;
4787990Srrh 		return;
4797990Srrh 	case LUV:
4807990Srrh 		q->use |= VUSED+USED;
4817990Srrh 		return;
4827990Srrh 	case LUE:
4837990Srrh 		q->use |= EUSED+USED;
4847990Srrh 		return;
4857990Srrh /* 01/04/80 */	case LUV | LUE:
4867990Srrh 	case LUM:
4877990Srrh 		q->use |= USED;
4887990Srrh 		return;
4897990Srrh 
4907990Srrh 		}
4917990Srrh 	}
4927990Srrh 
4937990Srrh chktype( pt1, pt2 ) register ATYPE *pt1, *pt2; {
4947990Srrh 	TWORD t;
4957990Srrh 
4967990Srrh 	/* check the two type words to see if they are compatible */
4977990Srrh 	/* for the moment, enums are turned into ints, and should be checked as such */
4987990Srrh 	if( pt1->aty == ENUMTY ) pt1->aty =  INT;
4997990Srrh 	if( pt2->aty == ENUMTY ) pt2->aty = INT;
5007990Srrh 
5017990Srrh 	if( (t=BTYPE(pt1->aty)==STRTY) || t==UNIONTY ){
50211762Sedward 		if( pt1->aty != pt2->aty || pt1->extra1 != pt2->extra1 )
50311762Sedward 			return 1;
50411762Sedward 		/* if -z then don't worry about undefined structures,
50511762Sedward 		   as long as the names match */
50611762Sedward 		if( zflag && (pt1->extra == 0 || pt2->extra == 0) ) return 0;
50711762Sedward 		return pt1->extra != pt2->extra;
5087990Srrh 		}
5097990Srrh 
5107990Srrh 	if( pt2->extra ){ /* constant passed in */
5117990Srrh 		if( pt1->aty == UNSIGNED && pt2->aty == INT ) return( 0 );
5127990Srrh 		else if( pt1->aty == ULONG && pt2->aty == LONG ) return( 0 );
5137990Srrh 		}
5147990Srrh 	else if( pt1->extra ){ /* for symmetry */
5157990Srrh 		if( pt2->aty == UNSIGNED && pt1->aty == INT ) return( 0 );
5167990Srrh 		else if( pt2->aty == ULONG && pt1->aty == LONG ) return( 0 );
5177990Srrh 		}
5187990Srrh 
5197990Srrh 	return( pt1->aty != pt2->aty );
5207990Srrh 	}
5217990Srrh 
5227990Srrh struct tb { int m; char * nm };
52311762Sedward 
52411762Sedward struct tb dfs[] = {
52511762Sedward 	LDI, "LDI",
52611762Sedward 	LIB, "LIB",
52711762Sedward 	LDC, "LDC",
52811762Sedward 	LDX, "LDX",
52911762Sedward 	LRV, "LRV",
53011762Sedward 	LUV, "LUV",
53111762Sedward 	LUE, "LUE",
53211762Sedward 	LUM, "LUM",
53311762Sedward 	LST, "LST",
53411762Sedward 	LFN, "LFN",
53511762Sedward 	0, "" };
53611762Sedward 
53711762Sedward struct tb us[] = {
53811762Sedward 	USED, "USED",
53911762Sedward 	VUSED, "VUSED",
54011762Sedward 	EUSED, "EUSED",
54111762Sedward 	RVAL, "RVAL",
54211762Sedward 	VARARGS, "VARARGS",
54311762Sedward 	0, "" };
54411762Sedward 
5457990Srrh ptb( v, tp ) struct tb *tp; {
5467990Srrh 	/* print a value from the table */
5477990Srrh 	int flag;
5487990Srrh 	flag = 0;
5497990Srrh 	for( ; tp->m; ++tp ){
5507990Srrh 		if( v&tp->m ){
5517990Srrh 			if( flag++ ) putchar( '|' );
5527990Srrh 			printf( "%s", tp->nm );
5537990Srrh 			}
5547990Srrh 		}
5557990Srrh 	}
5567990Srrh 
5577990Srrh pst( q ) STAB *q; {
5587990Srrh 	/* give a debugging output for q */
5597990Srrh 
5607990Srrh #ifndef FLEXNAMES
5617990Srrh 	printf( "%.8s (", q->name );
5627990Srrh #else
5637990Srrh 	printf( "%s (", q->name );
5647990Srrh #endif
5657990Srrh 	ptb( q->decflag, dfs );
5667990Srrh 	printf( "), use= " );
5677990Srrh 	ptb( q->use, us );
5687990Srrh 	printf( ", line %d, nargs=%d\n", q->fline, q->nargs );
5697990Srrh 	}
5707990Srrh 
57111762Sedward pfile() {
57211762Sedward 	/* print the input file in readable form */
57311762Sedward 	while( lread( LDI|LIB|LDC|LDX|LRV|LUV|LUE|LUM|LST|LFN ) )
57411762Sedward 		prc();
57511762Sedward 	}
57611762Sedward 
57711762Sedward prc() {
57811762Sedward 	/* print out 'r' for debugging */
57911762Sedward 	register i, j, k;
58011762Sedward 
58111762Sedward 	printf( "decflag\t" );
58211762Sedward 	ptb( r.l.decflag, dfs );
58311762Sedward 	putchar( '\n' );
58411762Sedward 	if( r.l.decflag & LFN ){
5857990Srrh #ifdef FLEXNAMES
58611762Sedward 		printf( "fn\t\t%s\n", r.f.fn );
58711762Sedward #else
58811762Sedward 		printf( "fn\t%\t.*s\n", LFNM, r.f.fn );
58911762Sedward #endif
59011762Sedward 		}
59111762Sedward 	else {
59211762Sedward #ifdef FLEXNAMES
59311762Sedward 		printf( "name\t%s\n", r.l.name );
59411762Sedward #else
59511762Sedward 		printf( "name\t%.*s\n", LCHNM, r.l.name );
59611762Sedward #endif
59711762Sedward 		printf( "nargs\t%d\n", r.l.nargs );
59811762Sedward 		printf( "fline\t%d\n", r.l.fline );
59911762Sedward 		printf( "type.aty\t0%o (", r.l.type.aty );
60011762Sedward 		pty( r.l.type.aty, r.l.name );
60111762Sedward 		printf( ")\ntype.extra\t%d\n", r.l.type.extra );
60211762Sedward 		j = r.l.type.extra1;
60311762Sedward 		printf( "type.extra1\t0x%x (%d,%d)\n",
60411762Sedward 			j, j & X_NONAME ? 1 : 0, j & ~X_NONAME );
60511762Sedward 		k = r.l.nargs;
60611762Sedward 		if( k < 0 ) k = -k;
60711762Sedward 		for( i = 0; i < k; i++ ){
60811762Sedward 			printf( "atyp[%d].aty\t0%o (", i, atyp[i].aty );
60911762Sedward 			pty( atyp[i].aty, "" );
61011762Sedward 			printf( ")\natyp[%d].extra\t%d\n", i, atyp[i].extra);
61111762Sedward 			j = atyp[i].extra1;
61211762Sedward 			printf( "atyp[%d].extra1\t0x%x (%d,%d)\n",
61311762Sedward 				i, j, j & X_NONAME ? 1 : 0, j & ~X_NONAME );
61411762Sedward 			}
61511762Sedward 		}
61611762Sedward 		putchar( '\n' );
61711762Sedward 	}
61811762Sedward 
61911762Sedward pty( t, name )  TWORD t; {
62011762Sedward 	static char * tnames[] = {
62111762Sedward 		"void", "farg", "char", "short",
62211762Sedward 		"int", "long", "float", "double",
62311762Sedward 		"struct xxx", "union %s", "enum", "moety",
62411762Sedward 		"unsigned char", "unsigned short", "unsigned", "unsigned long",
62511762Sedward 		"?", "?"
62611762Sedward 		};
62711762Sedward 
62811762Sedward 	printf( "%s ", tnames[BTYPE(t)] );
62911762Sedward 	pty1( t, name, (8 * sizeof (int) - BTSHIFT) / TSHIFT );
63011762Sedward 	}
63111762Sedward 
63211762Sedward pty1( t, name, level ) TWORD t; {
63311762Sedward 	register TWORD u;
63411762Sedward 
63511762Sedward 	if( level < 0 ){
63611762Sedward 		printf( "%s", name );
63711762Sedward 		return;
63811762Sedward 		}
63911762Sedward 	u = t >> level * TSHIFT;
64011762Sedward 	if( ISPTR(u) ){
64111762Sedward 		printf( "*" );
64211762Sedward 		pty1( t, name, level-1 );
64311762Sedward 		}
64411762Sedward 	else if( ISFTN(u) ){
64511762Sedward 		if( level > 0 && ISPTR(u << TSHIFT) ){
64611762Sedward 			printf( "(" );
64711762Sedward 			pty1( t, name, level-1 );
64811762Sedward 			printf( ")()" );
64911762Sedward 			}
65011762Sedward 		else {
65111762Sedward 			pty1( t, name, level-1 );
65211762Sedward 			printf( "()" );
65311762Sedward 			}
65411762Sedward 		}
65511762Sedward 	else if( ISARY(u) ){
65611762Sedward 		if( level > 0 && ISPTR(u << TSHIFT) ){
65711762Sedward 			printf( "(" );
65811762Sedward 			pty1( t, name, level-1 );
65911762Sedward 			printf( ")[]" );
66011762Sedward 			}
66111762Sedward 		else {
66211762Sedward 			pty1( t, name, level-1 );
66311762Sedward 			printf( "[]" );
66411762Sedward 			}
66511762Sedward 		}
66611762Sedward 	else {
66711762Sedward 		pty1( t, name, level-1 );
66811762Sedward 		}
66911762Sedward 	}
67011762Sedward 
67111762Sedward #ifdef FLEXNAMES
6727990Srrh char *
673*30938Sbostic getstr(doport)
6747990Srrh {
6757990Srrh 	char buf[BUFSIZ];
6767990Srrh 	register char *cp = buf;
6777990Srrh 	register int c;
6787990Srrh 
6797990Srrh 	if (feof(stdin) || ferror(stdin))
6807990Srrh 		return("");
6817990Srrh 	while ((c = getchar()) > 0)
6827990Srrh 		*cp++ = c;
6837990Srrh 	if (c < 0) {
68411762Sedward 		error("intermediate file format error (getstr)");
6857990Srrh 		exit(1);
6867990Srrh 	}
6877990Srrh 	*cp++ = 0;
688*30938Sbostic 	if (doport)
689*30938Sbostic 		portify(buf);
6907990Srrh 	return (hash(buf));
6917990Srrh }
6927990Srrh 
6937990Srrh #define	NSAVETAB	4096
6947990Srrh char	*savetab;
6957990Srrh int	saveleft;
6967990Srrh 
6977990Srrh char *
6987990Srrh savestr(cp)
6997990Srrh 	register char *cp;
7007990Srrh {
7017990Srrh 	register int len;
7027990Srrh 
7037990Srrh 	len = strlen(cp) + 1;
7047990Srrh 	if (len > saveleft) {
7057990Srrh 		saveleft = NSAVETAB;
7067990Srrh 		if (len > saveleft)
7077990Srrh 			saveleft = len;
7087990Srrh 		savetab = (char *)malloc(saveleft);
7097990Srrh 		if (savetab == 0) {
71011762Sedward 			error("ran out of memory (savestr)");
7117990Srrh 			exit(1);
7127990Srrh 		}
7137990Srrh 	}
7147990Srrh 	strncpy(savetab, cp, len);
7157990Srrh 	cp = savetab;
7167990Srrh 	savetab += len;
7177990Srrh 	saveleft -= len;
7187990Srrh 	return (cp);
7197990Srrh }
7207990Srrh 
7217990Srrh /*
7227990Srrh  * The definition for the segmented hash tables.
7237990Srrh  */
7247990Srrh #define	MAXHASH	20
7257990Srrh #define	HASHINC	1013
7267990Srrh struct ht {
7277990Srrh 	char	**ht_low;
7287990Srrh 	char	**ht_high;
7297990Srrh 	int	ht_used;
7307990Srrh } htab[MAXHASH];
7317990Srrh 
7327990Srrh char *
7337990Srrh hash(s)
7347990Srrh 	char *s;
7357990Srrh {
7367990Srrh 	register char **h;
7377990Srrh 	register i;
7387990Srrh 	register char *cp;
7397990Srrh 	struct ht *htp;
7407990Srrh 	int sh;
7417990Srrh 
74211762Sedward 	sh = hashstr(s) % HASHINC;
7437990Srrh 	cp = s;
7447990Srrh 	/*
7457990Srrh 	 * There are as many as MAXHASH active
7467990Srrh 	 * hash tables at any given point in time.
7477990Srrh 	 * The search starts with the first table
7487990Srrh 	 * and continues through the active tables
7497990Srrh 	 * as necessary.
7507990Srrh 	 */
7517990Srrh 	for (htp = htab; htp < &htab[MAXHASH]; htp++) {
7527990Srrh 		if (htp->ht_low == 0) {
7537990Srrh 			register char **hp =
7547990Srrh 			    (char **) calloc(sizeof (char **), HASHINC);
7557990Srrh 			if (hp == 0) {
75611762Sedward 				error("ran out of memory (hash)");
7577990Srrh 				exit(1);
7587990Srrh 			}
7597990Srrh 			htp->ht_low = hp;
7607990Srrh 			htp->ht_high = htp->ht_low + HASHINC;
7617990Srrh 		}
7627990Srrh 		h = htp->ht_low + sh;
7637990Srrh 		/*
7647990Srrh 		 * quadratic rehash increment
7657990Srrh 		 * starts at 1 and incremented
7667990Srrh 		 * by two each rehash.
7677990Srrh 		 */
7687990Srrh 		i = 1;
7697990Srrh 		do {
7707990Srrh 			if (*h == 0) {
7717990Srrh 				if (htp->ht_used > (HASHINC * 3)/4)
7727990Srrh 					break;
7737990Srrh 				htp->ht_used++;
7747990Srrh 				*h = savestr(cp);
7757990Srrh 				return (*h);
7767990Srrh 			}
7777990Srrh 			if (**h == *cp && strcmp(*h, cp) == 0)
7787990Srrh 				return (*h);
7797990Srrh 			h += i;
7807990Srrh 			i += 2;
7817990Srrh 			if (h >= htp->ht_high)
7827990Srrh 				h -= HASHINC;
7837990Srrh 		} while (i < HASHINC);
7847990Srrh 	}
78511762Sedward 	error("ran out of hash tables");
7867990Srrh 	exit(1);
7877990Srrh }
7887990Srrh char	*tstrbuf[1];
7897990Srrh #endif
790*30938Sbostic 
791*30938Sbostic #include "ctype.h"
792*30938Sbostic 
793*30938Sbostic portify(cp)
794*30938Sbostic register char *	cp;
795*30938Sbostic {
796*30938Sbostic 	register int	i;
797*30938Sbostic 
798*30938Sbostic 	if (!pflag)
799*30938Sbostic 		return;
800*30938Sbostic 	for (i = 0; i < 6; ++i)
801*30938Sbostic 		if (cp[i] == '\0')
802*30938Sbostic 			return;
803*30938Sbostic 		else if (isascii(cp[i]) && isupper(cp[i]))
804*30938Sbostic 			cp[i] = tolower(cp[i]);
805*30938Sbostic 	cp[i] = '\0';
806*30938Sbostic }
807