xref: /csrg-svn/old/pcc/mip/pftn.c (revision 32815)
117746Sralph #ifndef lint
2*32815Sdonn static char *sccsid ="@(#)pftn.c	1.17 (Berkeley) 12/10/87";
317746Sralph #endif lint
417746Sralph 
518398Sralph # include "pass1.h"
613939Slinton 
713939Slinton unsigned int offsz;
813939Slinton 
924405Smckusick struct symtab *schain[MAXSCOPES];	/* sym chains for clearst */
1024405Smckusick int chaintop;				/* highest active entry */
1124405Smckusick 
1213939Slinton struct instk {
1313939Slinton 	int in_sz;   /* size of array element */
1413939Slinton 	int in_x;    /* current index for structure member in structure initializations */
1513939Slinton 	int in_n;    /* number of initializations seen */
1613939Slinton 	int in_s;    /* sizoff */
1713939Slinton 	int in_d;    /* dimoff */
1813939Slinton 	TWORD in_t;    /* type */
1913939Slinton 	int in_id;   /* stab index */
2013939Slinton 	int in_fl;   /* flag which says if this level is controlled by {} */
2113939Slinton 	OFFSZ in_off;  /* offset of the beginning of this level */
2213939Slinton 	}
2313939Slinton instack[10],
2413939Slinton *pstk;
2513939Slinton 
2613939Slinton 	/* defines used for getting things off of the initialization stack */
2713939Slinton 
2813939Slinton 
2913939Slinton struct symtab *relook();
3013939Slinton 
3113939Slinton 
3213939Slinton int ddebug = 0;
3313939Slinton 
3413939Slinton struct symtab * mknonuniq();
3513939Slinton 
3624405Smckusick defid( q, class ) register NODE *q; register int class; {
3713939Slinton 	register struct symtab *p;
3813939Slinton 	int idp;
3924405Smckusick 	register TWORD type;
4013939Slinton 	TWORD stp;
4124405Smckusick 	register int scl;
4213939Slinton 	int dsym, ddef;
4313939Slinton 	int slev, temp;
4413940Slinton 	int changed;
4513939Slinton 
4613939Slinton 	if( q == NIL ) return;  /* an error was detected */
4713939Slinton 
4813939Slinton 	if( q < node || q >= &node[TREESZ] ) cerror( "defid call" );
4913939Slinton 
5013939Slinton 	idp = q->tn.rval;
5113939Slinton 
5213939Slinton 	if( idp < 0 ) cerror( "tyreduce" );
5313939Slinton 	p = &stab[idp];
5413939Slinton 
5513939Slinton # ifndef BUG1
5613939Slinton 	if( ddebug ){
5713939Slinton #ifndef FLEXNAMES
5813939Slinton 		printf( "defid( %.8s (%d), ", p->sname, idp );
5913939Slinton #else
6013939Slinton 		printf( "defid( %s (%d), ", p->sname, idp );
6113939Slinton #endif
6213939Slinton 		tprint( q->in.type );
6313939Slinton 		printf( ", %s, (%d,%d) ), level %d\n", scnames(class), q->fn.cdim, q->fn.csiz, blevel );
6413939Slinton 		}
6513939Slinton # endif
6613939Slinton 
6713939Slinton 	fixtype( q, class );
6813939Slinton 
6913939Slinton 	type = q->in.type;
7013939Slinton 	class = fixclass( class, type );
7113939Slinton 
7213939Slinton 	stp = p->stype;
7313939Slinton 	slev = p->slevel;
7413939Slinton 
7513939Slinton # ifndef BUG1
7613939Slinton 	if( ddebug ){
7713939Slinton 		printf( "	modified to " );
7813939Slinton 		tprint( type );
7913939Slinton 		printf( ", %s\n", scnames(class) );
8013939Slinton 		printf( "	previous def'n: " );
8113939Slinton 		tprint( stp );
8213939Slinton 		printf( ", %s, (%d,%d) ), level %d\n", scnames(p->sclass), p->dimoff, p->sizoff, slev );
8313939Slinton 		}
8413939Slinton # endif
8513939Slinton 
8613939Slinton 	if( stp == FTN && p->sclass == SNULL )goto enter;
8713939Slinton 		/* name encountered as function, not yet defined */
8813939Slinton 	if( stp == UNDEF|| stp == FARG ){
8913939Slinton 		if( blevel==1 && stp!=FARG ) switch( class ){
9013939Slinton 
9113939Slinton 		default:
9213939Slinton #ifndef FLEXNAMES
9313939Slinton 			if(!(class&FIELD)) uerror( "declared argument %.8s is missing", p->sname );
9413939Slinton #else
9513939Slinton 			if(!(class&FIELD)) uerror( "declared argument %s is missing", p->sname );
9613939Slinton #endif
9713939Slinton 		case MOS:
9813939Slinton 		case STNAME:
9913939Slinton 		case MOU:
10013939Slinton 		case UNAME:
10113939Slinton 		case MOE:
10213939Slinton 		case ENAME:
10313939Slinton 		case TYPEDEF:
10413939Slinton 			;
10513939Slinton 			}
10613939Slinton 		goto enter;
10713939Slinton 		}
10813939Slinton 
10913939Slinton 	if( type != stp ) goto mismatch;
11013939Slinton 	/* test (and possibly adjust) dimensions */
11113939Slinton 	dsym = p->dimoff;
11213939Slinton 	ddef = q->fn.cdim;
11313940Slinton 	changed = 0;
11413939Slinton 	for( temp=type; temp&TMASK; temp = DECREF(temp) ){
11513939Slinton 		if( ISARY(temp) ){
11613940Slinton 			if (dimtab[dsym] == 0) {
11713940Slinton 				dimtab[dsym] = dimtab[ddef];
11813940Slinton 				changed = 1;
11913940Slinton 				}
12013940Slinton 			else if (dimtab[ddef]!=0&&dimtab[dsym]!=dimtab[ddef]) {
12113939Slinton 				goto mismatch;
12213939Slinton 				}
12313939Slinton 			++dsym;
12413939Slinton 			++ddef;
12513939Slinton 			}
12613939Slinton 		}
12713939Slinton 
12813940Slinton 	if (changed) {
12913940Slinton 		FIXDEF(p);
13013940Slinton 		}
13113940Slinton 
13213939Slinton 	/* check that redeclarations are to the same structure */
13313939Slinton 	if( (temp==STRTY||temp==UNIONTY||temp==ENUMTY) && p->sizoff != q->fn.csiz
13413939Slinton 		 && class!=STNAME && class!=UNAME && class!=ENAME ){
13513939Slinton 		goto mismatch;
13613939Slinton 		}
13713939Slinton 
13813939Slinton 	scl = ( p->sclass );
13913939Slinton 
14013939Slinton # ifndef BUG1
14113939Slinton 	if( ddebug ){
14213939Slinton 		printf( "	previous class: %s\n", scnames(scl) );
14313939Slinton 		}
14413939Slinton # endif
14513939Slinton 
14613939Slinton 	if( class&FIELD ){
14713939Slinton 		/* redefinition */
14813939Slinton 		if( !falloc( p, class&FLDSIZ, 1, NIL ) ) {
14913939Slinton 			/* successful allocation */
15013939Slinton 			psave( idp );
15113939Slinton 			return;
15213939Slinton 			}
15313939Slinton 		/* blew it: resume at end of switch... */
15413939Slinton 		}
15513939Slinton 
15613939Slinton 	else switch( class ){
15713939Slinton 
15813939Slinton 	case EXTERN:
15913939Slinton 		switch( scl ){
16013939Slinton 		case STATIC:
16113939Slinton 		case USTATIC:
16213939Slinton 			if( slev==0 ) return;
16313939Slinton 			break;
16413939Slinton 		case EXTDEF:
16513939Slinton 		case EXTERN:
16613939Slinton 		case FORTRAN:
16713939Slinton 		case UFORTRAN:
16813939Slinton 			return;
16913939Slinton 			}
17013939Slinton 		break;
17113939Slinton 
17213939Slinton 	case STATIC:
17313939Slinton 		if( scl==USTATIC || (scl==EXTERN && blevel==0) ){
17413939Slinton 			p->sclass = STATIC;
17513939Slinton 			if( ISFTN(type) ) curftn = idp;
17613939Slinton 			return;
17713939Slinton 			}
17813939Slinton 		break;
17913939Slinton 
18013939Slinton 	case USTATIC:
18113939Slinton 		if( scl==STATIC || scl==USTATIC ) return;
18213939Slinton 		break;
18313939Slinton 
18413939Slinton 	case LABEL:
18513939Slinton 		if( scl == ULABEL ){
18613939Slinton 			p->sclass = LABEL;
18713939Slinton 			deflab( p->offset );
18813939Slinton 			return;
18913939Slinton 			}
19013939Slinton 		break;
19113939Slinton 
19213939Slinton 	case TYPEDEF:
19313939Slinton 		if( scl == class ) return;
19413939Slinton 		break;
19513939Slinton 
19613939Slinton 	case UFORTRAN:
19713939Slinton 		if( scl == UFORTRAN || scl == FORTRAN ) return;
19813939Slinton 		break;
19913939Slinton 
20013939Slinton 	case FORTRAN:
20113939Slinton 		if( scl == UFORTRAN ){
20213939Slinton 			p->sclass = FORTRAN;
20313939Slinton 			if( ISFTN(type) ) curftn = idp;
20413939Slinton 			return;
20513939Slinton 			}
20613939Slinton 		break;
20713939Slinton 
20813939Slinton 	case MOU:
20913939Slinton 	case MOS:
21013939Slinton 		if( scl == class ) {
21113939Slinton 			if( oalloc( p, &strucoff ) ) break;
21213939Slinton 			if( class == MOU ) strucoff = 0;
21313939Slinton 			psave( idp );
21413939Slinton 			return;
21513939Slinton 			}
21613939Slinton 		break;
21713939Slinton 
21813939Slinton 	case MOE:
21913939Slinton 		if( scl == class ){
22013939Slinton 			if( p->offset!= strucoff++ ) break;
22113939Slinton 			psave( idp );
22213939Slinton 			}
22313939Slinton 		break;
22413939Slinton 
22513939Slinton 	case EXTDEF:
22613939Slinton 		if( scl == EXTERN ) {
22713939Slinton 			p->sclass = EXTDEF;
22813939Slinton 			if( ISFTN(type) ) curftn = idp;
22913939Slinton 			return;
23013939Slinton 			}
23113939Slinton 		break;
23213939Slinton 
23313939Slinton 	case STNAME:
23413939Slinton 	case UNAME:
23513939Slinton 	case ENAME:
23613939Slinton 		if( scl != class ) break;
23713939Slinton 		if( dimtab[p->sizoff] == 0 ) return;  /* previous entry just a mention */
23813939Slinton 		break;
23913939Slinton 
24013939Slinton 	case ULABEL:
24113939Slinton 		if( scl == LABEL || scl == ULABEL ) return;
24213939Slinton 	case PARAM:
24313939Slinton 	case AUTO:
24413939Slinton 	case REGISTER:
24513939Slinton 		;  /* mismatch.. */
24613939Slinton 
24713939Slinton 		}
24813939Slinton 
24913939Slinton 	mismatch:
25013939Slinton 	/* allow nonunique structure/union member names */
25113939Slinton 
25213939Slinton 	if( class==MOU || class==MOS || class & FIELD ){/* make a new entry */
25324405Smckusick 		register int *memp;
25413939Slinton 		p->sflags |= SNONUNIQ;  /* old entry is nonunique */
25513939Slinton 		/* determine if name has occurred in this structure/union */
25617981Sralph 		if (paramno > 0) for( memp = &paramstk[paramno-1];
25713939Slinton 			/* while */ *memp>=0 && stab[*memp].sclass != STNAME
25813939Slinton 				&& stab[*memp].sclass != UNAME;
25917981Sralph 			/* iterate */ --memp){ char *cname, *oname;
26013939Slinton 			if( stab[*memp].sflags & SNONUNIQ ){int k;
26113939Slinton 				cname=p->sname;
26213939Slinton 				oname=stab[*memp].sname;
26313939Slinton #ifndef FLEXNAMES
26413939Slinton 				for(k=1; k<=NCHNAM; ++k){
26513939Slinton 					if(*cname++ != *oname)goto diff;
26613939Slinton 					if(!*oname++)break;
26713939Slinton 					}
26813939Slinton #else
26913939Slinton 				if (cname != oname) goto diff;
27013939Slinton #endif
27113939Slinton 				uerror("redeclaration of: %s",p->sname);
27213939Slinton 				break;
27313939Slinton 				diff: continue;
27413939Slinton 				}
27513939Slinton 			}
27613939Slinton 		p = mknonuniq( &idp ); /* update p and idp to new entry */
27713939Slinton 		goto enter;
27813939Slinton 		}
27913939Slinton 	if( blevel > slev && class != EXTERN && class != FORTRAN &&
28013939Slinton 		class != UFORTRAN && !( class == LABEL && slev >= 2 ) ){
28113939Slinton 		q->tn.rval = idp = hide( p );
28213939Slinton 		p = &stab[idp];
28313939Slinton 		goto enter;
28413939Slinton 		}
28513939Slinton #ifndef FLEXNAMES
28613939Slinton 	uerror( "redeclaration of %.8s", p->sname );
28713939Slinton #else
28813939Slinton 	uerror( "redeclaration of %s", p->sname );
28913939Slinton #endif
29013939Slinton 	if( class==EXTDEF && ISFTN(type) ) curftn = idp;
29113939Slinton 	return;
29213939Slinton 
29313939Slinton 	enter:  /* make a new entry */
29413939Slinton 
29513939Slinton # ifndef BUG1
29613939Slinton 	if( ddebug ) printf( "	new entry made\n" );
29713939Slinton # endif
29813939Slinton 	if( type == UNDEF ) uerror("void type for %s",p->sname);
29913939Slinton 	p->stype = type;
30013939Slinton 	p->sclass = class;
30113939Slinton 	p->slevel = blevel;
30213939Slinton 	p->offset = NOOFFSET;
30313939Slinton 	p->suse = lineno;
30413939Slinton 	if( class == STNAME || class == UNAME || class == ENAME ) {
30513939Slinton 		p->sizoff = curdim;
30613939Slinton 		dstash( 0 );  /* size */
30713939Slinton 		dstash( -1 ); /* index to members of str or union */
30813939Slinton 		dstash( ALSTRUCT );  /* alignment */
30913939Slinton 		dstash( idp );
31013939Slinton 		}
31113939Slinton 	else {
31213939Slinton 		switch( BTYPE(type) ){
31313939Slinton 		case STRTY:
31413939Slinton 		case UNIONTY:
31513939Slinton 		case ENUMTY:
31613939Slinton 			p->sizoff = q->fn.csiz;
31713939Slinton 			break;
31813939Slinton 		default:
31913939Slinton 			p->sizoff = BTYPE(type);
32013939Slinton 			}
32113939Slinton 		}
32213939Slinton 
32313939Slinton 	/* copy dimensions */
32413939Slinton 
32513939Slinton 	p->dimoff = q->fn.cdim;
32613939Slinton 
32713939Slinton 	/* allocate offsets */
32813939Slinton 	if( class&FIELD ){
32913939Slinton 		falloc( p, class&FLDSIZ, 0, NIL );  /* new entry */
33013939Slinton 		psave( idp );
33113939Slinton 		}
33213939Slinton 	else switch( class ){
33313939Slinton 
33413939Slinton 	case AUTO:
33513939Slinton 		oalloc( p, &autooff );
33613939Slinton 		break;
33713939Slinton 	case STATIC:
33813939Slinton 	case EXTDEF:
33913939Slinton 		p->offset = getlab();
34013939Slinton 		if( ISFTN(type) ) curftn = idp;
34113939Slinton 		break;
34213939Slinton 	case ULABEL:
34313939Slinton 	case LABEL:
34413939Slinton 		p->offset = getlab();
34513939Slinton 		p->slevel = 2;
34613939Slinton 		if( class == LABEL ){
34713939Slinton 			locctr( PROG );
34813939Slinton 			deflab( p->offset );
34913939Slinton 			}
35013939Slinton 		break;
35113939Slinton 
35213939Slinton 	case EXTERN:
35313939Slinton 	case UFORTRAN:
35413939Slinton 	case FORTRAN:
35513939Slinton 		p->offset = getlab();
35613939Slinton 		p->slevel = 0;
35713939Slinton 		break;
35813939Slinton 	case MOU:
35913939Slinton 	case MOS:
36013939Slinton 		oalloc( p, &strucoff );
36113939Slinton 		if( class == MOU ) strucoff = 0;
36213939Slinton 		psave( idp );
36313939Slinton 		break;
36413939Slinton 
36513939Slinton 	case MOE:
36613939Slinton 		p->offset = strucoff++;
36713939Slinton 		psave( idp );
36813939Slinton 		break;
36913939Slinton 	case REGISTER:
37013939Slinton 		p->offset = regvar--;
37113939Slinton 		if( blevel == 1 ) p->sflags |= SSET;
37213939Slinton 		if( regvar < minrvar ) minrvar = regvar;
37313939Slinton 		break;
37413939Slinton 		}
37513939Slinton 
37624405Smckusick 	{
37724405Smckusick 		register int l = p->slevel;
37824405Smckusick 
37924405Smckusick 		if( l >= MAXSCOPES )
38024405Smckusick 			cerror( "scopes nested too deep" );
38124405Smckusick 
38224405Smckusick 		p->snext = schain[l];
38324405Smckusick 		schain[l] = p;
38424405Smckusick 		if( l >= chaintop )
38524405Smckusick 			chaintop = l + 1;
38624405Smckusick 		}
38724405Smckusick 
38813939Slinton 	/* user-supplied routine to fix up new definitions */
38913939Slinton 
39013939Slinton 	FIXDEF(p);
39113939Slinton 
39213939Slinton # ifndef BUG1
39313939Slinton 	if( ddebug ) printf( "	dimoff, sizoff, offset: %d, %d, %d\n", p->dimoff, p->sizoff, p->offset );
39413939Slinton # endif
39513939Slinton 
39613939Slinton 	}
39713939Slinton 
39813939Slinton psave( i ){
39913939Slinton 	if( paramno >= PARAMSZ ){
40013939Slinton 		cerror( "parameter stack overflow");
40113939Slinton 		}
40213939Slinton 	paramstk[ paramno++ ] = i;
40313939Slinton 	}
40413939Slinton 
40513939Slinton ftnend(){ /* end of function */
40632814Sdonn 	if( retlab != NOLAB && nerrors == 0 ){ /* inside a real function */
40713939Slinton 		efcode();
40813939Slinton 		}
40913939Slinton 	checkst(0);
41013939Slinton 	retstat = 0;
41113939Slinton 	tcheck();
41213939Slinton 	curclass = SNULL;
41313939Slinton 	brklab = contlab = retlab = NOLAB;
41413939Slinton 	flostat = 0;
41513939Slinton 	if( nerrors == 0 ){
41613939Slinton 		if( psavbc != & asavbc[0] ) cerror("bcsave error");
41713939Slinton 		if( paramno != 0 ) cerror("parameter reset error");
41813939Slinton 		if( swx != 0 ) cerror( "switch error");
41913939Slinton 		}
42013939Slinton 	psavbc = &asavbc[0];
42113939Slinton 	paramno = 0;
42213939Slinton 	autooff = AUTOINIT;
42313939Slinton 	minrvar = regvar = MAXRVAR;
42413939Slinton 	reached = 1;
42513939Slinton 	swx = 0;
42613939Slinton 	swp = swtab;
42713939Slinton 	locctr(DATA);
42813939Slinton 	}
42913939Slinton 
43013939Slinton dclargs(){
43113939Slinton 	register i, j;
43213939Slinton 	register struct symtab *p;
43313939Slinton 	register NODE *q;
43413939Slinton 	argoff = ARGINIT;
43513939Slinton # ifndef BUG1
43613939Slinton 	if( ddebug > 2) printf("dclargs()\n");
43713939Slinton # endif
43813939Slinton 	for( i=0; i<paramno; ++i ){
43913939Slinton 		if( (j = paramstk[i]) < 0 ) continue;
44013939Slinton 		p = &stab[j];
44113939Slinton # ifndef BUG1
44213939Slinton 		if( ddebug > 2 ){
44313939Slinton 			printf("\t%s (%d) ",p->sname, j);
44413939Slinton 			tprint(p->stype);
44513939Slinton 			printf("\n");
44613939Slinton 			}
44713939Slinton # endif
44813939Slinton 		if( p->stype == FARG ) {
44913939Slinton 			q = block(FREE,NIL,NIL,INT,0,INT);
45013939Slinton 			q->tn.rval = j;
45113939Slinton 			defid( q, PARAM );
45213939Slinton 			}
45313939Slinton 		FIXARG(p); /* local arg hook, eg. for sym. debugger */
45413939Slinton 		oalloc( p, &argoff );  /* always set aside space, even for register arguments */
45513939Slinton 		}
45613939Slinton 	cendarg();
45713939Slinton 	locctr(PROG);
45813939Slinton 	defalign(ALINT);
45913939Slinton 	ftnno = getlab();
46013939Slinton 	bfcode( paramstk, paramno );
46113939Slinton 	paramno = 0;
46213939Slinton 	}
46313939Slinton 
46413939Slinton NODE *
46513939Slinton rstruct( idn, soru ){ /* reference to a structure or union, with no definition */
46613939Slinton 	register struct symtab *p;
46713939Slinton 	register NODE *q;
46813939Slinton 	p = &stab[idn];
46913939Slinton 	switch( p->stype ){
47013939Slinton 
47113939Slinton 	case UNDEF:
47213939Slinton 	def:
47313939Slinton 		q = block( FREE, NIL, NIL, 0, 0, 0 );
47413939Slinton 		q->tn.rval = idn;
47513939Slinton 		q->in.type = (soru&INSTRUCT) ? STRTY : ( (soru&INUNION) ? UNIONTY : ENUMTY );
47613939Slinton 		defid( q, (soru&INSTRUCT) ? STNAME : ( (soru&INUNION) ? UNAME : ENAME ) );
47713939Slinton 		break;
47813939Slinton 
47913939Slinton 	case STRTY:
48013939Slinton 		if( soru & INSTRUCT ) break;
48113939Slinton 		goto def;
48213939Slinton 
48313939Slinton 	case UNIONTY:
48413939Slinton 		if( soru & INUNION ) break;
48513939Slinton 		goto def;
48613939Slinton 
48713939Slinton 	case ENUMTY:
48813939Slinton 		if( !(soru&(INUNION|INSTRUCT)) ) break;
48913939Slinton 		goto def;
49013939Slinton 
49113939Slinton 		}
49213939Slinton 	stwart = instruct;
49313939Slinton 	return( mkty( p->stype, 0, p->sizoff ) );
49413939Slinton 	}
49513939Slinton 
49613939Slinton moedef( idn ){
49713939Slinton 	register NODE *q;
49813939Slinton 
49913939Slinton 	q = block( FREE, NIL, NIL, MOETY, 0, 0 );
50013939Slinton 	q->tn.rval = idn;
50113939Slinton 	if( idn>=0 ) defid( q, MOE );
50213939Slinton 	}
50313939Slinton 
50413939Slinton bstruct( idn, soru ){ /* begining of structure or union declaration */
50513939Slinton 	register NODE *q;
50613939Slinton 
50713939Slinton 	psave( instruct );
50813939Slinton 	psave( curclass );
50913939Slinton 	psave( strucoff );
51013939Slinton 	strucoff = 0;
51113939Slinton 	instruct = soru;
51213939Slinton 	q = block( FREE, NIL, NIL, 0, 0, 0 );
51313939Slinton 	q->tn.rval = idn;
51413939Slinton 	if( instruct==INSTRUCT ){
51513939Slinton 		curclass = MOS;
51613939Slinton 		q->in.type = STRTY;
51713939Slinton 		if( idn >= 0 ) defid( q, STNAME );
51813939Slinton 		}
51913939Slinton 	else if( instruct == INUNION ) {
52013939Slinton 		curclass = MOU;
52113939Slinton 		q->in.type = UNIONTY;
52213939Slinton 		if( idn >= 0 ) defid( q, UNAME );
52313939Slinton 		}
52413939Slinton 	else { /* enum */
52513939Slinton 		curclass = MOE;
52613939Slinton 		q->in.type = ENUMTY;
52713939Slinton 		if( idn >= 0 ) defid( q, ENAME );
52813939Slinton 		}
52913939Slinton 	psave( idn = q->tn.rval );
53013939Slinton 	/* the "real" definition is where the members are seen */
53113939Slinton 	if ( idn >= 0 ) stab[idn].suse = lineno;
53213939Slinton 	return( paramno-4 );
53313939Slinton 	}
53413939Slinton 
53513939Slinton NODE *
53613939Slinton dclstruct( oparam ){
53713939Slinton 	register struct symtab *p;
53813939Slinton 	register i, al, sa, j, sz, szindex;
53913939Slinton 	register TWORD temp;
54013939Slinton 	register high, low;
54113939Slinton 
54213939Slinton 	/* paramstack contains:
54313939Slinton 		paramstack[ oparam ] = previous instruct
54413939Slinton 		paramstack[ oparam+1 ] = previous class
54513939Slinton 		paramstk[ oparam+2 ] = previous strucoff
54613939Slinton 		paramstk[ oparam+3 ] = structure name
54713939Slinton 
54813939Slinton 		paramstk[ oparam+4, ... ]  = member stab indices
54913939Slinton 
55013939Slinton 		*/
55113939Slinton 
55213939Slinton 
55313939Slinton 	if( (i=paramstk[oparam+3]) < 0 ){
55413939Slinton 		szindex = curdim;
55513939Slinton 		dstash( 0 );  /* size */
55613939Slinton 		dstash( -1 );  /* index to member names */
55713939Slinton 		dstash( ALSTRUCT );  /* alignment */
55813939Slinton 		dstash( -lineno );	/* name of structure */
55913939Slinton 		}
56013939Slinton 	else {
56113939Slinton 		szindex = stab[i].sizoff;
56213939Slinton 		}
56313939Slinton 
56413939Slinton # ifndef BUG1
56513939Slinton 	if( ddebug ){
56613939Slinton #ifndef FLEXNAMES
56713939Slinton 		printf( "dclstruct( %.8s ), szindex = %d\n", (i>=0)? stab[i].sname : "??", szindex );
56813939Slinton #else
56913939Slinton 		printf( "dclstruct( %s ), szindex = %d\n", (i>=0)? stab[i].sname : "??", szindex );
57013939Slinton #endif
57113939Slinton 		}
57213939Slinton # endif
57313939Slinton 	temp = (instruct&INSTRUCT)?STRTY:((instruct&INUNION)?UNIONTY:ENUMTY);
57413939Slinton 	stwart = instruct = paramstk[ oparam ];
57513939Slinton 	curclass = paramstk[ oparam+1 ];
57613939Slinton 	dimtab[ szindex+1 ] = curdim;
57713939Slinton 	al = ALSTRUCT;
57813939Slinton 
57913939Slinton 	high = low = 0;
58013939Slinton 
58113939Slinton 	for( i = oparam+4;  i< paramno; ++i ){
58213939Slinton 		dstash( j=paramstk[i] );
58313939Slinton 		if( j<0 || j>= SYMTSZ ) cerror( "gummy structure member" );
58413939Slinton 		p = &stab[j];
58513939Slinton 		if( temp == ENUMTY ){
58613939Slinton 			if( p->offset < low ) low = p->offset;
58713939Slinton 			if( p->offset > high ) high = p->offset;
58813939Slinton 			p->sizoff = szindex;
58913939Slinton 			continue;
59013939Slinton 			}
59113939Slinton 		sa = talign( p->stype, p->sizoff );
59213939Slinton 		if( p->sclass & FIELD ){
59313939Slinton 			sz = p->sclass&FLDSIZ;
59413939Slinton 			}
59513939Slinton 		else {
59613939Slinton 			sz = tsize( p->stype, p->dimoff, p->sizoff );
59713939Slinton 			}
59813939Slinton 		if( sz == 0 ){
59913939Slinton #ifndef FLEXNAMES
60013939Slinton 			werror( "illegal zero sized structure member: %.8s", p->sname );
60113939Slinton #else
60213939Slinton 			werror( "illegal zero sized structure member: %s", p->sname );
60313939Slinton #endif
60413939Slinton 			}
60513939Slinton 		if( sz > strucoff ) strucoff = sz;  /* for use with unions */
60613939Slinton 		SETOFF( al, sa );
60713939Slinton 		/* set al, the alignment, to the lcm of the alignments of the members */
60813939Slinton 		}
60913939Slinton 	dstash( -1 );  /* endmarker */
61013939Slinton 	SETOFF( strucoff, al );
61113939Slinton 
61213939Slinton 	if( temp == ENUMTY ){
61313939Slinton 		register TWORD ty;
61413939Slinton 
61513939Slinton # ifdef ENUMSIZE
61613939Slinton 		ty = ENUMSIZE(high,low);
61713939Slinton # else
61813939Slinton 		if( (char)high == high && (char)low == low ) ty = ctype( CHAR );
61913939Slinton 		else if( (short)high == high && (short)low == low ) ty = ctype( SHORT );
62013939Slinton 		else ty = ctype(INT);
62113939Slinton #endif
62213939Slinton 		strucoff = tsize( ty, 0, (int)ty );
62313939Slinton 		dimtab[ szindex+2 ] = al = talign( ty, (int)ty );
62413939Slinton 		}
62513939Slinton 
62613939Slinton 	if( strucoff == 0 ) uerror( "zero sized structure" );
62713939Slinton 	dimtab[ szindex ] = strucoff;
62813939Slinton 	dimtab[ szindex+2 ] = al;
62913939Slinton 	dimtab[ szindex+3 ] = paramstk[ oparam+3 ];  /* name index */
63013939Slinton 
63113939Slinton 	FIXSTRUCT( szindex, oparam ); /* local hook, eg. for sym debugger */
63213939Slinton # ifndef BUG1
63313939Slinton 	if( ddebug>1 ){
63413939Slinton 		printf( "\tdimtab[%d,%d,%d] = %d,%d,%d\n", szindex,szindex+1,szindex+2,
63513939Slinton 				dimtab[szindex],dimtab[szindex+1],dimtab[szindex+2] );
63613939Slinton 		for( i = dimtab[szindex+1]; dimtab[i] >= 0; ++i ){
63713939Slinton #ifndef FLEXNAMES
63813939Slinton 			printf( "\tmember %.8s(%d)\n", stab[dimtab[i]].sname, dimtab[i] );
63913939Slinton #else
64013939Slinton 			printf( "\tmember %s(%d)\n", stab[dimtab[i]].sname, dimtab[i] );
64113939Slinton #endif
64213939Slinton 			}
64313939Slinton 		}
64413939Slinton # endif
64513939Slinton 
64613939Slinton 	strucoff = paramstk[ oparam+2 ];
64713939Slinton 	paramno = oparam;
64813939Slinton 
64913939Slinton 	return( mkty( temp, 0, szindex ) );
65013939Slinton 	}
65113939Slinton 
65213939Slinton 	/* VARARGS */
65313939Slinton yyerror( s ) char *s; { /* error printing routine in parser */
65413939Slinton 
65513939Slinton 	uerror( s );
65613939Slinton 
65713939Slinton 	}
65813939Slinton 
65913939Slinton yyaccpt(){
66013939Slinton 	ftnend();
66113939Slinton 	}
66213939Slinton 
66313939Slinton ftnarg( idn ) {
66413939Slinton 	switch( stab[idn].stype ){
66513939Slinton 
66613939Slinton 	case UNDEF:
66713939Slinton 		/* this parameter, entered at scan */
66813939Slinton 		break;
66913939Slinton 	case FARG:
67013939Slinton #ifndef FLEXNAMES
67113939Slinton 		uerror("redeclaration of formal parameter, %.8s",
67213939Slinton #else
67313939Slinton 		uerror("redeclaration of formal parameter, %s",
67413939Slinton #endif
67513939Slinton 			stab[idn].sname);
67613939Slinton 		/* fall thru */
67713939Slinton 	case FTN:
67813939Slinton 		/* the name of this function matches parm */
67913939Slinton 		/* fall thru */
68013939Slinton 	default:
68113939Slinton 		idn = hide( &stab[idn]);
68213939Slinton 		break;
68313939Slinton 	case TNULL:
68413939Slinton 		/* unused entry, fill it */
68513939Slinton 		;
68613939Slinton 		}
68713939Slinton 	stab[idn].stype = FARG;
68813939Slinton 	stab[idn].sclass = PARAM;
68913939Slinton 	psave( idn );
69013939Slinton 	}
69113939Slinton 
69213939Slinton talign( ty, s) register unsigned ty; register s; {
69313939Slinton 	/* compute the alignment of an object with type ty, sizeoff index s */
69413939Slinton 
69513939Slinton 	register i;
69613939Slinton 	if( s<0 && ty!=INT && ty!=CHAR && ty!=SHORT && ty!=UNSIGNED && ty!=UCHAR && ty!=USHORT
69713939Slinton #ifdef LONGFIELDS
69813939Slinton 		&& ty!=LONG && ty!=ULONG
69913939Slinton #endif
70013939Slinton 					){
70113939Slinton 		return( fldal( ty ) );
70213939Slinton 		}
70313939Slinton 
70413939Slinton 	for( i=0; i<=(SZINT-BTSHIFT-1); i+=TSHIFT ){
70513939Slinton 		switch( (ty>>i)&TMASK ){
70613939Slinton 
70713939Slinton 		case FTN:
70813939Slinton 			cerror( "compiler takes alignment of function");
70913939Slinton 		case PTR:
71013939Slinton 			return( ALPOINT );
71113939Slinton 		case ARY:
71213939Slinton 			continue;
71313939Slinton 		case 0:
71413939Slinton 			break;
71513939Slinton 			}
71613939Slinton 		}
71713939Slinton 
71813939Slinton 	switch( BTYPE(ty) ){
71913939Slinton 
72013939Slinton 	case UNIONTY:
72113939Slinton 	case ENUMTY:
72213939Slinton 	case STRTY:
72313939Slinton 		return( (unsigned int) dimtab[ s+2 ] );
72413939Slinton 	case CHAR:
72513939Slinton 	case UCHAR:
72613939Slinton 		return( ALCHAR );
72713939Slinton 	case FLOAT:
72813939Slinton 		return( ALFLOAT );
72913939Slinton 	case DOUBLE:
73013939Slinton 		return( ALDOUBLE );
73113939Slinton 	case LONG:
73213939Slinton 	case ULONG:
73313939Slinton 		return( ALLONG );
73413939Slinton 	case SHORT:
73513939Slinton 	case USHORT:
73613939Slinton 		return( ALSHORT );
73713939Slinton 	default:
73813939Slinton 		return( ALINT );
73913939Slinton 		}
74013939Slinton 	}
74113939Slinton 
74213939Slinton OFFSZ
74313939Slinton tsize( ty, d, s )  TWORD ty; {
74413939Slinton 	/* compute the size associated with type ty,
74513939Slinton 	    dimoff d, and sizoff s */
74613939Slinton 	/* BETTER NOT BE CALLED WHEN t, d, and s REFER TO A BIT FIELD... */
74713939Slinton 
74813939Slinton 	int i;
74913939Slinton 	OFFSZ mult;
75013939Slinton 
75113939Slinton 	mult = 1;
75213939Slinton 
75313939Slinton 	for( i=0; i<=(SZINT-BTSHIFT-1); i+=TSHIFT ){
75413939Slinton 		switch( (ty>>i)&TMASK ){
75513939Slinton 
75613939Slinton 		case FTN:
75713939Slinton 			cerror( "compiler takes size of function");
75813939Slinton 		case PTR:
75913939Slinton 			return( SZPOINT * mult );
76013939Slinton 		case ARY:
76113939Slinton 			mult *= (unsigned int) dimtab[ d++ ];
76213939Slinton 			continue;
76313939Slinton 		case 0:
76413939Slinton 			break;
76513939Slinton 
76613939Slinton 			}
76713939Slinton 		}
76813939Slinton 
76913939Slinton 	if( dimtab[s]==0 ) {
77025749Sdonn 		if( ty == STRTY )
77125749Sdonn 			uerror( "undefined structure" );
77225749Sdonn 		else
77325749Sdonn 			uerror( "unknown size");
77413939Slinton 		return( SZINT );
77513939Slinton 		}
77613939Slinton 	return( (unsigned int) dimtab[ s ] * mult );
77713939Slinton 	}
77813939Slinton 
77913939Slinton inforce( n ) OFFSZ n; {  /* force inoff to have the value n */
78013939Slinton 	/* inoff is updated to have the value n */
78113939Slinton 	OFFSZ wb;
78213939Slinton 	register rest;
78313939Slinton 	/* rest is used to do a lot of conversion to ints... */
78413939Slinton 
78513939Slinton 	if( inoff == n ) return;
78613939Slinton 	if( inoff > n ) {
78713939Slinton 		cerror( "initialization alignment error");
78813939Slinton 		}
78913939Slinton 
79013939Slinton 	wb = inoff;
79113939Slinton 	SETOFF( wb, SZINT );
79213939Slinton 
79313939Slinton 	/* wb now has the next higher word boundary */
79413939Slinton 
79513939Slinton 	if( wb >= n ){ /* in the same word */
79613939Slinton 		rest = n - inoff;
79713939Slinton 		vfdzero( rest );
79813939Slinton 		return;
79913939Slinton 		}
80013939Slinton 
80113939Slinton 	/* otherwise, extend inoff to be word aligned */
80213939Slinton 
80313939Slinton 	rest = wb - inoff;
80413939Slinton 	vfdzero( rest );
80513939Slinton 
80613939Slinton 	/* now, skip full words until near to n */
80713939Slinton 
80813939Slinton 	rest = (n-inoff)/SZINT;
80913939Slinton 	zecode( rest );
81013939Slinton 
81113939Slinton 	/* now, the remainder of the last word */
81213939Slinton 
81313939Slinton 	rest = n-inoff;
81413939Slinton 	vfdzero( rest );
81513939Slinton 	if( inoff != n ) cerror( "inoff error");
81613939Slinton 
81713939Slinton 	}
81813939Slinton 
81913939Slinton vfdalign( n ){ /* make inoff have the offset the next alignment of n */
82013939Slinton 	OFFSZ m;
82113939Slinton 
82213939Slinton 	m = inoff;
82313939Slinton 	SETOFF( m, n );
82413939Slinton 	inforce( m );
82513939Slinton 	}
82613939Slinton 
82713939Slinton 
82813939Slinton int idebug = 0;
82913939Slinton 
83013939Slinton int ibseen = 0;  /* the number of } constructions which have been filled */
83113939Slinton 
83227247Sdonn int ifull = 0; /* 1 if all initializers have been seen */
83327247Sdonn 
83413939Slinton int iclass;  /* storage class of thing being initialized */
83513939Slinton 
83613939Slinton int ilocctr = 0;  /* location counter for current initialization */
83713939Slinton 
83813939Slinton beginit(curid){
83913939Slinton 	/* beginning of initilization; set location ctr and set type */
84013939Slinton 	register struct symtab *p;
84113939Slinton 
84213939Slinton # ifndef BUG1
84313939Slinton 	if( idebug >= 3 ) printf( "beginit(), curid = %d\n", curid );
84413939Slinton # endif
84513939Slinton 
84613939Slinton 	p = &stab[curid];
84713939Slinton 
84813939Slinton 	iclass = p->sclass;
84913939Slinton 	if( curclass == EXTERN || curclass == FORTRAN ) iclass = EXTERN;
85013939Slinton 	switch( iclass ){
85113939Slinton 
85213939Slinton 	case UNAME:
85313939Slinton 	case EXTERN:
85413939Slinton 		return;
85513939Slinton 	case AUTO:
85613939Slinton 	case REGISTER:
85713939Slinton 		break;
85813939Slinton 	case EXTDEF:
85913939Slinton 	case STATIC:
86013939Slinton 		ilocctr = ISARY(p->stype)?ADATA:DATA;
86132814Sdonn 		if( nerrors == 0 ){
86232814Sdonn 			locctr( ilocctr );
86332814Sdonn 			defalign( talign( p->stype, p->sizoff ) );
86432814Sdonn 			defnam( p );
86532814Sdonn 			}
86613939Slinton 
86713939Slinton 		}
86813939Slinton 
86913939Slinton 	inoff = 0;
87013939Slinton 	ibseen = 0;
87127247Sdonn 	ifull = 0;
87213939Slinton 
87313939Slinton 	pstk = 0;
87413939Slinton 
87513939Slinton 	instk( curid, p->stype, p->dimoff, p->sizoff, inoff );
87613939Slinton 
87713939Slinton 	}
87813939Slinton 
87913939Slinton instk( id, t, d, s, off ) OFFSZ off; TWORD t; {
88013939Slinton 	/* make a new entry on the parameter stack to initialize id */
88113939Slinton 
88213939Slinton 	register struct symtab *p;
88313939Slinton 
88413939Slinton 	for(;;){
88513939Slinton # ifndef BUG1
88613939Slinton 		if( idebug ) printf( "instk((%d, %o,%d,%d, %d)\n", id, t, d, s, off );
88713939Slinton # endif
88813939Slinton 
88913939Slinton 		/* save information on the stack */
89013939Slinton 
89113939Slinton 		if( !pstk ) pstk = instack;
89213939Slinton 		else ++pstk;
89313939Slinton 
89413939Slinton 		pstk->in_fl = 0;	/* { flag */
89513939Slinton 		pstk->in_id =  id ;
89613939Slinton 		pstk->in_t =  t ;
89713939Slinton 		pstk->in_d =  d ;
89813939Slinton 		pstk->in_s =  s ;
89913939Slinton 		pstk->in_n = 0;  /* number seen */
90013939Slinton 		pstk->in_x =  t==STRTY ?dimtab[s+1] : 0 ;
90113939Slinton 		pstk->in_off =  off;   /* offset at the beginning of this element */
90213939Slinton 		/* if t is an array, DECREF(t) can't be a field */
90313939Slinton 		/* INS_sz has size of array elements, and -size for fields */
90413939Slinton 		if( ISARY(t) ){
90513939Slinton 			pstk->in_sz = tsize( DECREF(t), d+1, s );
90613939Slinton 			}
90713939Slinton 		else if( stab[id].sclass & FIELD ){
90813939Slinton 			pstk->in_sz = - ( stab[id].sclass & FLDSIZ );
90913939Slinton 			}
91013939Slinton 		else {
91113939Slinton 			pstk->in_sz = 0;
91213939Slinton 			}
91313939Slinton 
91413939Slinton 		if( (iclass==AUTO || iclass == REGISTER ) &&
91513939Slinton 			(ISARY(t) || t==STRTY) ) uerror( "no automatic aggregate initialization" );
91613939Slinton 
91713939Slinton 		/* now, if this is not a scalar, put on another element */
91813939Slinton 
91913939Slinton 		if( ISARY(t) ){
92013939Slinton 			t = DECREF(t);
92113939Slinton 			++d;
92213939Slinton 			continue;
92313939Slinton 			}
92413939Slinton 		else if( t == STRTY ){
92525749Sdonn 			if( dimtab[pstk->in_s] == 0 ){
92625749Sdonn 				uerror( "can't initialize undefined structure" );
92725749Sdonn 				iclass = -1;
92825749Sdonn 				return;
92925749Sdonn 				}
93013939Slinton 			id = dimtab[pstk->in_x];
93113939Slinton 			p = &stab[id];
93213939Slinton 			if( p->sclass != MOS && !(p->sclass&FIELD) ) cerror( "insane structure member list" );
93313939Slinton 			t = p->stype;
93413939Slinton 			d = p->dimoff;
93513939Slinton 			s = p->sizoff;
93613939Slinton 			off += p->offset;
93713939Slinton 			continue;
93813939Slinton 			}
93913939Slinton 		else return;
94013939Slinton 		}
94113939Slinton 	}
94213939Slinton 
94313939Slinton NODE *
94413939Slinton getstr(){ /* decide if the string is external or an initializer, and get the contents accordingly */
94513939Slinton 
94613939Slinton 	register l, temp;
94713939Slinton 	register NODE *p;
94813939Slinton 
94913939Slinton 	if( (iclass==EXTDEF||iclass==STATIC) && (pstk->in_t == CHAR || pstk->in_t == UCHAR) &&
95013939Slinton 			pstk!=instack && ISARY( pstk[-1].in_t ) ){
95113939Slinton 		/* treat "abc" as { 'a', 'b', 'c', 0 } */
95213939Slinton 		strflg = 1;
95313939Slinton 		ilbrace();  /* simulate { */
95413939Slinton 		inforce( pstk->in_off );
95513939Slinton 		/* if the array is inflexible (not top level), pass in the size and
95613939Slinton 			be prepared to throw away unwanted initializers */
95713939Slinton 		lxstr((pstk-1)!=instack?dimtab[(pstk-1)->in_d]:0);  /* get the contents */
95813939Slinton 		irbrace();  /* simulate } */
95913939Slinton 		return( NIL );
96013939Slinton 		}
96113939Slinton 	else { /* make a label, and get the contents and stash them away */
96213939Slinton 		if( iclass != SNULL ){ /* initializing */
96313939Slinton 			/* fill out previous word, to permit pointer */
96413939Slinton 			vfdalign( ALPOINT );
96513939Slinton 			}
96613939Slinton 		temp = locctr( blevel==0?ISTRNG:STRNG ); /* set up location counter */
96713939Slinton 		deflab( l = getlab() );
96813939Slinton 		strflg = 0;
96913939Slinton 		lxstr(0); /* get the contents */
97013939Slinton 		locctr( blevel==0?ilocctr:temp );
97113939Slinton 		p = buildtree( STRING, NIL, NIL );
97213939Slinton 		p->tn.rval = -l;
97313939Slinton 		return(p);
97413939Slinton 		}
97513939Slinton 	}
97613939Slinton 
97713939Slinton putbyte( v ){ /* simulate byte v appearing in a list of integer values */
97813939Slinton 	register NODE *p;
97913939Slinton 	p = bcon(v);
98013939Slinton 	incode( p, SZCHAR );
98113939Slinton 	tfree( p );
98213939Slinton 	gotscal();
98313939Slinton 	}
98413939Slinton 
98513939Slinton endinit(){
98613939Slinton 	register TWORD t;
98713939Slinton 	register d, s, n, d1;
98813939Slinton 
98913939Slinton # ifndef BUG1
99013939Slinton 	if( idebug ) printf( "endinit(), inoff = %d\n", inoff );
99113939Slinton # endif
99213939Slinton 
99313939Slinton 	switch( iclass ){
99413939Slinton 
99513939Slinton 	case EXTERN:
99613939Slinton 	case AUTO:
99713939Slinton 	case REGISTER:
99825749Sdonn 	case -1:
99913939Slinton 		return;
100013939Slinton 		}
100113939Slinton 
100213939Slinton 	pstk = instack;
100313939Slinton 
100413939Slinton 	t = pstk->in_t;
100513939Slinton 	d = pstk->in_d;
100613939Slinton 	s = pstk->in_s;
100713939Slinton 	n = pstk->in_n;
100813939Slinton 
100913939Slinton 	if( ISARY(t) ){
101013939Slinton 		d1 = dimtab[d];
101113939Slinton 
101213939Slinton 		vfdalign( pstk->in_sz );  /* fill out part of the last element, if needed */
101313939Slinton 		n = inoff/pstk->in_sz;  /* real number of initializers */
101413939Slinton 		if( d1 >= n ){
101513939Slinton 			/* once again, t is an array, so no fields */
101613939Slinton 			inforce( tsize( t, d, s ) );
101713939Slinton 			n = d1;
101813939Slinton 			}
101913939Slinton 		if( d1!=0 && d1!=n ) uerror( "too many initializers");
102013939Slinton 		if( n==0 ) werror( "empty array declaration");
102113939Slinton 		dimtab[d] = n;
102213942Slinton 		if( d1==0 ) FIXDEF(&stab[pstk->in_id]);
102313939Slinton 		}
102413939Slinton 
102513939Slinton 	else if( t == STRTY || t == UNIONTY ){
102613939Slinton 		/* clearly not fields either */
102713939Slinton 		inforce( tsize( t, d, s ) );
102813939Slinton 		}
102913939Slinton 	else if( n > 1 ) uerror( "bad scalar initialization");
103013939Slinton 	/* this will never be called with a field element... */
103113939Slinton 	else inforce( tsize(t,d,s) );
103213939Slinton 
103313939Slinton 	paramno = 0;
103413939Slinton 	vfdalign( AL_INIT );
103513939Slinton 	inoff = 0;
103613939Slinton 	iclass = SNULL;
103713939Slinton 
103813939Slinton 	}
103913939Slinton 
104013939Slinton doinit( p ) register NODE *p; {
104113939Slinton 
104213939Slinton 	/* take care of generating a value for the initializer p */
104313939Slinton 	/* inoff has the current offset (last bit written)
104413939Slinton 		in the current word being generated */
104513939Slinton 
104613939Slinton 	register sz, d, s;
104713939Slinton 	register TWORD t;
104817746Sralph 	int o;
104913939Slinton 
105013939Slinton 	/* note: size of an individual initializer is assumed to fit into an int */
105113939Slinton 
105232814Sdonn 	if( iclass < 0 ) goto leave;
105313939Slinton 	if( iclass == EXTERN || iclass == UNAME ){
105413939Slinton 		uerror( "cannot initialize extern or union" );
105513939Slinton 		iclass = -1;
105613939Slinton 		goto leave;
105713939Slinton 		}
105813939Slinton 
105913939Slinton 	if( iclass == AUTO || iclass == REGISTER ){
106013939Slinton 		/* do the initialization and get out, without regard
106113939Slinton 		    for filing out the variable with zeros, etc. */
106213939Slinton 		bccode();
106313939Slinton 		idname = pstk->in_id;
106413939Slinton 		p = buildtree( ASSIGN, buildtree( NAME, NIL, NIL ), p );
106513939Slinton 		ecomp(p);
106613939Slinton 		return;
106713939Slinton 		}
106813939Slinton 
106913939Slinton 	if( p == NIL ) return;  /* for throwing away strings that have been turned into lists */
107013939Slinton 
107127247Sdonn 	if( ifull ){
107227247Sdonn 		uerror( "too many initializers" );
107327247Sdonn 		iclass = -1;
107427247Sdonn 		goto leave;
107527247Sdonn 		}
107613939Slinton 	if( ibseen ){
107713939Slinton 		uerror( "} expected");
107813939Slinton 		goto leave;
107913939Slinton 		}
108013939Slinton 
108113939Slinton # ifndef BUG1
108213939Slinton 	if( idebug > 1 ) printf( "doinit(%o)\n", p );
108313939Slinton # endif
108413939Slinton 
108513939Slinton 	t = pstk->in_t;  /* type required */
108613939Slinton 	d = pstk->in_d;
108713939Slinton 	s = pstk->in_s;
108813939Slinton 	if( pstk->in_sz < 0 ){  /* bit field */
108913939Slinton 		sz = -pstk->in_sz;
109013939Slinton 		}
109113939Slinton 	else {
109213939Slinton 		sz = tsize( t, d, s );
109313939Slinton 		}
109413939Slinton 
109513939Slinton 	inforce( pstk->in_off );
109613939Slinton 
109713939Slinton 	p = buildtree( ASSIGN, block( NAME, NIL,NIL, t, d, s ), p );
109813939Slinton 	p->in.left->in.op = FREE;
109913939Slinton 	p->in.left = p->in.right;
110013939Slinton 	p->in.right = NIL;
110113939Slinton 	p->in.left = optim( p->in.left );
110217746Sralph 	o = p->in.left->in.op;
110317746Sralph 	if( o == UNARY AND ){
110417746Sralph 		o = p->in.left->in.op = FREE;
110513939Slinton 		p->in.left = p->in.left->in.left;
110613939Slinton 		}
110713939Slinton 	p->in.op = INIT;
110813939Slinton 
110913939Slinton 	if( sz < SZINT ){ /* special case: bit fields, etc. */
1110*32815Sdonn 		if( o != ICON || p->in.left->tn.rval != NONAME )
1111*32815Sdonn 			uerror( "illegal initialization" );
111213939Slinton 		else incode( p->in.left, sz );
111313939Slinton 		}
111417746Sralph 	else if( o == FCON ){
111517746Sralph 		fincode( p->in.left->fpn.fval, sz );
111613939Slinton 		}
111717746Sralph 	else if( o == DCON ){
111817746Sralph 		fincode( p->in.left->dpn.dval, sz );
111917746Sralph 		}
112013939Slinton 	else {
112116178Sralph 		p = optim(p);
112216178Sralph 		if( p->in.left->in.op != ICON ) uerror( "illegal initialization" );
112316178Sralph 		else cinit( p, sz );
112413939Slinton 		}
112513939Slinton 
112613939Slinton 	gotscal();
112713939Slinton 
112813939Slinton 	leave:
112913939Slinton 	tfree(p);
113013939Slinton 	}
113113939Slinton 
113213939Slinton gotscal(){
113313939Slinton 	register t, ix;
113413939Slinton 	register n, id;
113513939Slinton 	struct symtab *p;
113613939Slinton 	OFFSZ temp;
113713939Slinton 
113813939Slinton 	for( ; pstk > instack; ) {
113913939Slinton 
114013939Slinton 		if( pstk->in_fl ) ++ibseen;
114113939Slinton 
114213939Slinton 		--pstk;
114313939Slinton 
114413939Slinton 		t = pstk->in_t;
114513939Slinton 
114613939Slinton 		if( t == STRTY ){
114713939Slinton 			ix = ++pstk->in_x;
114813939Slinton 			if( (id=dimtab[ix]) < 0 ) continue;
114913939Slinton 
115013939Slinton 			/* otherwise, put next element on the stack */
115113939Slinton 
115213939Slinton 			p = &stab[id];
115313939Slinton 			instk( id, p->stype, p->dimoff, p->sizoff, p->offset+pstk->in_off );
115413939Slinton 			return;
115513939Slinton 			}
115613939Slinton 		else if( ISARY(t) ){
115713939Slinton 			n = ++pstk->in_n;
115813939Slinton 			if( n >= dimtab[pstk->in_d] && pstk > instack ) continue;
115913939Slinton 
116013939Slinton 			/* put the new element onto the stack */
116113939Slinton 
116213939Slinton 			temp = pstk->in_sz;
116313939Slinton 			instk( pstk->in_id, (TWORD)DECREF(pstk->in_t), pstk->in_d+1, pstk->in_s,
116413939Slinton 				pstk->in_off+n*temp );
116513939Slinton 			return;
116613939Slinton 			}
116713939Slinton 
116813939Slinton 		}
116927247Sdonn 	ifull = 1;
117013939Slinton 	}
117113939Slinton 
117213939Slinton ilbrace(){ /* process an initializer's left brace */
117313939Slinton 	register t;
117413939Slinton 	struct instk *temp;
117513939Slinton 
117613939Slinton 	temp = pstk;
117713939Slinton 
117813939Slinton 	for( ; pstk > instack; --pstk ){
117913939Slinton 
118013939Slinton 		t = pstk->in_t;
118113939Slinton 		if( t != STRTY && !ISARY(t) ) continue; /* not an aggregate */
118213939Slinton 		if( pstk->in_fl ){ /* already associated with a { */
118313939Slinton 			if( pstk->in_n ) uerror( "illegal {");
118413939Slinton 			continue;
118513939Slinton 			}
118613939Slinton 
118713939Slinton 		/* we have one ... */
118813939Slinton 		pstk->in_fl = 1;
118913939Slinton 		break;
119013939Slinton 		}
119113939Slinton 
119213939Slinton 	/* cannot find one */
119313939Slinton 	/* ignore such right braces */
119413939Slinton 
119513939Slinton 	pstk = temp;
119613939Slinton 	}
119713939Slinton 
119813939Slinton irbrace(){
119913939Slinton 	/* called when a '}' is seen */
120013939Slinton 
120113939Slinton # ifndef BUG1
120213939Slinton 	if( idebug ) printf( "irbrace(): paramno = %d on entry\n", paramno );
120313939Slinton # endif
120413939Slinton 
120513939Slinton 	if( ibseen ) {
120613939Slinton 		--ibseen;
120713939Slinton 		return;
120813939Slinton 		}
120913939Slinton 
121013939Slinton 	for( ; pstk > instack; --pstk ){
121113939Slinton 		if( !pstk->in_fl ) continue;
121213939Slinton 
121313939Slinton 		/* we have one now */
121413939Slinton 
121513939Slinton 		pstk->in_fl = 0;  /* cancel { */
121613939Slinton 		gotscal();  /* take it away... */
121713939Slinton 		return;
121813939Slinton 		}
121913939Slinton 
122013939Slinton 	/* these right braces match ignored left braces: throw out */
122127247Sdonn 	ifull = 1;
122213939Slinton 
122313939Slinton 	}
122413939Slinton 
122513939Slinton upoff( size, alignment, poff ) register alignment, *poff; {
122613939Slinton 	/* update the offset pointed to by poff; return the
122713939Slinton 	/* offset of a value of size `size', alignment `alignment',
122813939Slinton 	/* given that off is increasing */
122913939Slinton 
123013939Slinton 	register off;
123113939Slinton 
123213939Slinton 	off = *poff;
123313939Slinton 	SETOFF( off, alignment );
123413939Slinton 	if( (offsz-off) <  size ){
123513939Slinton 		if( instruct!=INSTRUCT )cerror("too many local variables");
123613939Slinton 		else cerror("Structure too large");
123713939Slinton 		}
123813939Slinton 	*poff = off+size;
123913939Slinton 	return( off );
124013939Slinton 	}
124113939Slinton 
124213939Slinton oalloc( p, poff ) register struct symtab *p; register *poff; {
124313939Slinton 	/* allocate p with offset *poff, and update *poff */
124413939Slinton 	register al, off, tsz;
124513939Slinton 	int noff;
124613939Slinton 
124713939Slinton 	al = talign( p->stype, p->sizoff );
124813939Slinton 	noff = off = *poff;
124913939Slinton 	tsz = tsize( p->stype, p->dimoff, p->sizoff );
125013939Slinton #ifdef BACKAUTO
125113939Slinton 	if( p->sclass == AUTO ){
125213939Slinton 		if( (offsz-off) < tsz ) cerror("too many local variables");
125313939Slinton 		noff = off + tsz;
125413939Slinton 		SETOFF( noff, al );
125513939Slinton 		off = -noff;
125613939Slinton 		}
125713939Slinton 	else
125813939Slinton #endif
125913939Slinton 		if( p->sclass == PARAM && ( tsz < SZINT ) ){
126013939Slinton 			off = upoff( SZINT, ALINT, &noff );
126113939Slinton # ifndef RTOLBYTES
126213939Slinton 			off = noff - tsz;
126313939Slinton #endif
126413939Slinton 			}
126513939Slinton 		else
126613939Slinton 		{
126713939Slinton 		off = upoff( tsz, al, &noff );
126813939Slinton 		}
126913939Slinton 
127013939Slinton 	if( p->sclass != REGISTER ){ /* in case we are allocating stack space for register arguments */
127113939Slinton 		if( p->offset == NOOFFSET ) p->offset = off;
127213939Slinton 		else if( off != p->offset ) return(1);
127313939Slinton 		}
127413939Slinton 
127513939Slinton 	*poff = noff;
127613939Slinton 	return(0);
127713939Slinton 	}
127813939Slinton 
127913939Slinton falloc( p, w, new, pty )  register struct symtab *p; NODE *pty; {
128013939Slinton 	/* allocate a field of width w */
128113939Slinton 	/* new is 0 if new entry, 1 if redefinition, -1 if alignment */
128213939Slinton 
128313939Slinton 	register al,sz,type;
128413939Slinton 
128513939Slinton 	type = (new<0)? pty->in.type : p->stype;
128613939Slinton 
128713939Slinton 	/* this must be fixed to use the current type in alignments */
128813939Slinton 	switch( new<0?pty->in.type:p->stype ){
128913939Slinton 
129013939Slinton 	case ENUMTY:
129113939Slinton 		{
129213939Slinton 			int s;
129313939Slinton 			s = new<0 ? pty->fn.csiz : p->sizoff;
129413939Slinton 			al = dimtab[s+2];
129513939Slinton 			sz = dimtab[s];
129613939Slinton 			break;
129713939Slinton 			}
129813939Slinton 
129913939Slinton 	case CHAR:
130013939Slinton 	case UCHAR:
130113939Slinton 		al = ALCHAR;
130213939Slinton 		sz = SZCHAR;
130313939Slinton 		break;
130413939Slinton 
130513939Slinton 	case SHORT:
130613939Slinton 	case USHORT:
130713939Slinton 		al = ALSHORT;
130813939Slinton 		sz = SZSHORT;
130913939Slinton 		break;
131013939Slinton 
131113939Slinton 	case INT:
131213939Slinton 	case UNSIGNED:
131313939Slinton 		al = ALINT;
131413939Slinton 		sz = SZINT;
131513939Slinton 		break;
131613939Slinton #ifdef LONGFIELDS
131713939Slinton 
131813939Slinton 	case LONG:
131913939Slinton 	case ULONG:
132013939Slinton 		al = ALLONG;
132113939Slinton 		sz = SZLONG;
132213939Slinton 		break;
132313939Slinton #endif
132413939Slinton 
132513939Slinton 	default:
132613939Slinton 		if( new < 0 ) {
132713939Slinton 			uerror( "illegal field type" );
132813939Slinton 			al = ALINT;
132913939Slinton 			}
133013939Slinton 		else {
133113939Slinton 			al = fldal( p->stype );
133213939Slinton 			sz =SZINT;
133313939Slinton 			}
133413939Slinton 		}
133513939Slinton 
133613939Slinton 	if( w > sz ) {
133713939Slinton 		uerror( "field too big");
133813939Slinton 		w = sz;
133913939Slinton 		}
134013939Slinton 
134113939Slinton 	if( w == 0 ){ /* align only */
134213939Slinton 		SETOFF( strucoff, al );
134313939Slinton 		if( new >= 0 ) uerror( "zero size field");
134413939Slinton 		return(0);
134513939Slinton 		}
134613939Slinton 
134713939Slinton 	if( strucoff%al + w > sz ) SETOFF( strucoff, al );
134813939Slinton 	if( new < 0 ) {
134913939Slinton 		if( (offsz-strucoff) < w )
135013939Slinton 			cerror("structure too large");
135113939Slinton 		strucoff += w;  /* we know it will fit */
135213939Slinton 		return(0);
135313939Slinton 		}
135413939Slinton 
135513939Slinton 	/* establish the field */
135613939Slinton 
135713939Slinton 	if( new == 1 ) { /* previous definition */
135813939Slinton 		if( p->offset != strucoff || p->sclass != (FIELD|w) ) return(1);
135913939Slinton 		}
136013939Slinton 	p->offset = strucoff;
136113939Slinton 	if( (offsz-strucoff) < w ) cerror("structure too large");
136213939Slinton 	strucoff += w;
136313939Slinton 	p->stype = type;
136413939Slinton 	fldty( p );
136513939Slinton 	return(0);
136613939Slinton 	}
136713939Slinton 
136813939Slinton nidcl( p ) NODE *p; { /* handle unitialized declarations */
136913939Slinton 	/* assumed to be not functions */
137013939Slinton 	register class;
137113939Slinton 	register commflag;  /* flag for labelled common declarations */
137213939Slinton 
137313939Slinton 	commflag = 0;
137413939Slinton 
137513939Slinton 	/* compute class */
137613939Slinton 	if( (class=curclass) == SNULL ){
137713939Slinton 		if( blevel > 1 ) class = AUTO;
137813939Slinton 		else if( blevel != 0 || instruct ) cerror( "nidcl error" );
137913939Slinton 		else { /* blevel = 0 */
138013939Slinton 			class = noinit();
138113939Slinton 			if( class == EXTERN ) commflag = 1;
138213939Slinton 			}
138313939Slinton 		}
138413939Slinton #ifdef LCOMM
138513939Slinton 	/* hack so stab will come at as LCSYM rather than STSYM */
138613939Slinton 	if (class == STATIC) {
138713939Slinton 		extern int stabLCSYM;
138813939Slinton 		stabLCSYM = 1;
138913939Slinton 	}
139013939Slinton #endif
139113939Slinton 
139213939Slinton 	defid( p, class );
139313939Slinton 
139413939Slinton #ifndef LCOMM
139524405Smckusick 	if( class==EXTDEF || class==STATIC )
139613939Slinton #else
139713939Slinton 	if (class==STATIC) {
139813939Slinton 		register struct symtab *s = &stab[p->tn.rval];
139913939Slinton 		extern int stabLCSYM;
140013939Slinton 		int sz = tsize(s->stype, s->dimoff, s->sizoff)/SZCHAR;
140113939Slinton 
140213939Slinton 		stabLCSYM = 0;
140313939Slinton 		if (sz % sizeof (int))
140413939Slinton 			sz += sizeof (int) - (sz % sizeof (int));
140513939Slinton 		if (s->slevel > 1)
140613939Slinton 			printf("	.lcomm	L%d,%d\n", s->offset, sz);
140713939Slinton 		else
140813939Slinton 			printf("	.lcomm	%s,%d\n", exname(s->sname), sz);
140924405Smckusick 	}else if (class == EXTDEF)
141013939Slinton #endif
141124405Smckusick 		{
141213939Slinton 		/* simulate initialization by 0 */
141313939Slinton 		beginit(p->tn.rval);
141413939Slinton 		endinit();
141513939Slinton 		}
141613939Slinton 	if( commflag ) commdec( p->tn.rval );
141713939Slinton 	}
141813939Slinton 
141913939Slinton TWORD
142013939Slinton types( t1, t2, t3 ) TWORD t1, t2, t3; {
142113939Slinton 	/* return a basic type from basic types t1, t2, and t3 */
142213939Slinton 
142313939Slinton 	TWORD t[3], noun, adj, unsg;
142413939Slinton 	register i;
142513939Slinton 
142613939Slinton 	t[0] = t1;
142713939Slinton 	t[1] = t2;
142813939Slinton 	t[2] = t3;
142913939Slinton 
143013939Slinton 	unsg = INT;  /* INT or UNSIGNED */
143113939Slinton 	noun = UNDEF;  /* INT, CHAR, or FLOAT */
143213939Slinton 	adj = INT;  /* INT, LONG, or SHORT */
143313939Slinton 
143413939Slinton 	for( i=0; i<3; ++i ){
143513939Slinton 		switch( t[i] ){
143613939Slinton 
143713939Slinton 		default:
143813939Slinton 		bad:
143913939Slinton 			uerror( "illegal type combination" );
144013939Slinton 			return( INT );
144113939Slinton 
144213939Slinton 		case UNDEF:
144313939Slinton 			continue;
144413939Slinton 
144513939Slinton 		case UNSIGNED:
144613939Slinton 			if( unsg != INT ) goto bad;
144713939Slinton 			unsg = UNSIGNED;
144813939Slinton 			continue;
144913939Slinton 
145013939Slinton 		case LONG:
145113939Slinton 		case SHORT:
145213939Slinton 			if( adj != INT ) goto bad;
145313939Slinton 			adj = t[i];
145413939Slinton 			continue;
145513939Slinton 
145613939Slinton 		case INT:
145713939Slinton 		case CHAR:
145813939Slinton 		case FLOAT:
145913939Slinton 			if( noun != UNDEF ) goto bad;
146013939Slinton 			noun = t[i];
146113939Slinton 			continue;
146213939Slinton 			}
146313939Slinton 		}
146413939Slinton 
146513939Slinton 	/* now, construct final type */
146613939Slinton 	if( noun == UNDEF ) noun = INT;
146713939Slinton 	else if( noun == FLOAT ){
146813939Slinton 		if( unsg != INT || adj == SHORT ) goto bad;
146913939Slinton 		return( adj==LONG ? DOUBLE : FLOAT );
147013939Slinton 		}
147113939Slinton 	else if( noun == CHAR && adj != INT ) goto bad;
147213939Slinton 
147313939Slinton 	/* now, noun is INT or CHAR */
147413939Slinton 	if( adj != INT ) noun = adj;
147513939Slinton 	if( unsg == UNSIGNED ) return( noun + (UNSIGNED-INT) );
147613939Slinton 	else return( noun );
147713939Slinton 	}
147813939Slinton 
147913939Slinton NODE *
148013939Slinton tymerge( typ, idp ) NODE *typ, *idp; {
148113939Slinton 	/* merge type typ with identifier idp  */
148213939Slinton 
148313939Slinton 	register unsigned t;
148413939Slinton 	register i;
148513939Slinton 	extern int eprint();
148613939Slinton 
148713939Slinton 	if( typ->in.op != TYPE ) cerror( "tymerge: arg 1" );
148813939Slinton 	if(idp == NIL ) return( NIL );
148913939Slinton 
149013939Slinton # ifndef BUG1
149113939Slinton 	if( ddebug > 2 ) fwalk( idp, eprint, 0 );
149213939Slinton # endif
149313939Slinton 
149413939Slinton 	idp->in.type = typ->in.type;
149513939Slinton 	idp->fn.cdim = curdim;
149613939Slinton 	tyreduce( idp );
149713939Slinton 	idp->fn.csiz = typ->fn.csiz;
149813939Slinton 
149913939Slinton 	for( t=typ->in.type, i=typ->fn.cdim; t&TMASK; t = DECREF(t) ){
150013939Slinton 		if( ISARY(t) ) dstash( dimtab[i++] );
150113939Slinton 		}
150213939Slinton 
150313939Slinton 	/* now idp is a single node: fix up type */
150413939Slinton 
150513939Slinton 	idp->in.type = ctype( idp->in.type );
150613939Slinton 
150713939Slinton 	if( (t = BTYPE(idp->in.type)) != STRTY && t != UNIONTY && t != ENUMTY ){
150813939Slinton 		idp->fn.csiz = t;  /* in case ctype has rewritten things */
150913939Slinton 		}
151013939Slinton 
151113939Slinton 	return( idp );
151213939Slinton 	}
151313939Slinton 
151413939Slinton tyreduce( p ) register NODE *p; {
151513939Slinton 
151613939Slinton 	/* build a type, and stash away dimensions, from a parse tree of the declaration */
151713939Slinton 	/* the type is build top down, the dimensions bottom up */
151813939Slinton 	register o, temp;
151913939Slinton 	register unsigned t;
152013939Slinton 
152113939Slinton 	o = p->in.op;
152213939Slinton 	p->in.op = FREE;
152313939Slinton 
152413939Slinton 	if( o == NAME ) return;
152513939Slinton 
152613939Slinton 	t = INCREF( p->in.type );
152713939Slinton 	if( o == UNARY CALL ) t += (FTN-PTR);
152813939Slinton 	else if( o == LB ){
152913939Slinton 		t += (ARY-PTR);
153013939Slinton 		temp = p->in.right->tn.lval;
153113939Slinton 		p->in.right->in.op = FREE;
153213939Slinton 		if( ( temp == 0 ) & ( p->in.left->tn.op == LB ) )
153313939Slinton 			uerror( "Null dimension" );
153413939Slinton 		}
153513939Slinton 
153613939Slinton 	p->in.left->in.type = t;
153713939Slinton 	tyreduce( p->in.left );
153813939Slinton 
153913939Slinton 	if( o == LB ) dstash( temp );
154013939Slinton 
154113939Slinton 	p->tn.rval = p->in.left->tn.rval;
154213939Slinton 	p->in.type = p->in.left->in.type;
154313939Slinton 
154413939Slinton 	}
154513939Slinton 
154613939Slinton fixtype( p, class ) register NODE *p; {
154713939Slinton 	register unsigned t, type;
154813939Slinton 	register mod1, mod2;
154913939Slinton 	/* fix up the types, and check for legality */
155013939Slinton 
155113939Slinton 	if( (type = p->in.type) == UNDEF ) return;
155213939Slinton 	if( mod2 = (type&TMASK) ){
155313939Slinton 		t = DECREF(type);
155413939Slinton 		while( mod1=mod2, mod2 = (t&TMASK) ){
155513939Slinton 			if( mod1 == ARY && mod2 == FTN ){
155613939Slinton 				uerror( "array of functions is illegal" );
155713939Slinton 				type = 0;
155813939Slinton 				}
155913939Slinton 			else if( mod1 == FTN && ( mod2 == ARY || mod2 == FTN ) ){
156013939Slinton 				uerror( "function returns illegal type" );
156113939Slinton 				type = 0;
156213939Slinton 				}
156313939Slinton 			t = DECREF(t);
156413939Slinton 			}
156513939Slinton 		}
156613939Slinton 
156713939Slinton 	/* detect function arguments, watching out for structure declarations */
156813939Slinton 	/* for example, beware of f(x) struct [ int a[10]; } *x; { ... } */
156913939Slinton 	/* the danger is that "a" will be converted to a pointer */
157013939Slinton 
157113939Slinton 	if( class==SNULL && blevel==1 && !(instruct&(INSTRUCT|INUNION)) ) class = PARAM;
157213939Slinton 	if( class == PARAM || ( class==REGISTER && blevel==1 ) ){
157313939Slinton 		if( type == FLOAT ) type = DOUBLE;
157413939Slinton 		else if( ISARY(type) ){
157513939Slinton 			++p->fn.cdim;
157613939Slinton 			type += (PTR-ARY);
157713939Slinton 			}
157813939Slinton 		else if( ISFTN(type) ){
157913939Slinton 			werror( "a function is declared as an argument" );
158013939Slinton 			type = INCREF(type);
158113939Slinton 			}
158213939Slinton 
158313939Slinton 		}
158413939Slinton 
158513939Slinton 	if( instruct && ISFTN(type) ){
158613939Slinton 		uerror( "function illegal in structure or union" );
158713939Slinton 		type = INCREF(type);
158813939Slinton 		}
158913939Slinton 	p->in.type = type;
159013939Slinton 	}
159113939Slinton 
159213939Slinton uclass( class ) register class; {
159313939Slinton 	/* give undefined version of class */
159413939Slinton 	if( class == SNULL ) return( EXTERN );
159513939Slinton 	else if( class == STATIC ) return( USTATIC );
159613939Slinton 	else if( class == FORTRAN ) return( UFORTRAN );
159713939Slinton 	else return( class );
159813939Slinton 	}
159913939Slinton 
160013939Slinton fixclass( class, type ) TWORD type; {
160113939Slinton 
160213939Slinton 	/* first, fix null class */
160313939Slinton 
160413939Slinton 	if( class == SNULL ){
160513939Slinton 		if( instruct&INSTRUCT ) class = MOS;
160613939Slinton 		else if( instruct&INUNION ) class = MOU;
160713939Slinton 		else if( blevel == 0 ) class = EXTDEF;
160813939Slinton 		else if( blevel == 1 ) class = PARAM;
160913939Slinton 		else class = AUTO;
161013939Slinton 
161113939Slinton 		}
161213939Slinton 
161313939Slinton 	/* now, do general checking */
161413939Slinton 
161513939Slinton 	if( ISFTN( type ) ){
161613939Slinton 		switch( class ) {
161713939Slinton 		default:
161813939Slinton 			uerror( "function has illegal storage class" );
161913939Slinton 		case AUTO:
162013939Slinton 			class = EXTERN;
162113939Slinton 		case EXTERN:
162213939Slinton 		case EXTDEF:
162313939Slinton 		case FORTRAN:
162413939Slinton 		case TYPEDEF:
162513939Slinton 		case STATIC:
162613939Slinton 		case UFORTRAN:
162713939Slinton 		case USTATIC:
162813939Slinton 			;
162913939Slinton 			}
163013939Slinton 		}
163113939Slinton 
163213939Slinton 	if( class&FIELD ){
163313939Slinton 		if( !(instruct&INSTRUCT) ) uerror( "illegal use of field" );
163413939Slinton 		return( class );
163513939Slinton 		}
163613939Slinton 
163713939Slinton 	switch( class ){
163813939Slinton 
163913939Slinton 	case MOU:
164013939Slinton 		if( !(instruct&INUNION) ) uerror( "illegal class" );
164113939Slinton 		return( class );
164213939Slinton 
164313939Slinton 	case MOS:
164413939Slinton 		if( !(instruct&INSTRUCT) ) uerror( "illegal class" );
164513939Slinton 		return( class );
164613939Slinton 
164713939Slinton 	case MOE:
164813939Slinton 		if( instruct & (INSTRUCT|INUNION) ) uerror( "illegal class" );
164913939Slinton 		return( class );
165013939Slinton 
165113939Slinton 	case REGISTER:
165213939Slinton 		if( blevel == 0 ) uerror( "illegal register declaration" );
165313939Slinton 		else if( regvar >= MINRVAR && cisreg( type ) ) return( class );
165413939Slinton 		if( blevel == 1 ) return( PARAM );
165513939Slinton 		else return( AUTO );
165613939Slinton 
165713939Slinton 	case AUTO:
165813939Slinton 	case LABEL:
165913939Slinton 	case ULABEL:
166013939Slinton 		if( blevel < 2 ) uerror( "illegal class" );
166113939Slinton 		return( class );
166213939Slinton 
166313939Slinton 	case PARAM:
166413939Slinton 		if( blevel != 1 ) uerror( "illegal class" );
166513939Slinton 		return( class );
166613939Slinton 
166713939Slinton 	case UFORTRAN:
166813939Slinton 	case FORTRAN:
166913939Slinton # ifdef NOFORTRAN
167013939Slinton 			NOFORTRAN;    /* a condition which can regulate the FORTRAN usage */
167113939Slinton # endif
167213939Slinton 		if( !ISFTN(type) ) uerror( "fortran declaration must apply to function" );
167313939Slinton 		else {
167413939Slinton 			type = DECREF(type);
167513939Slinton 			if( ISFTN(type) || ISARY(type) || ISPTR(type) ) {
167613939Slinton 				uerror( "fortran function has wrong type" );
167713939Slinton 				}
167813939Slinton 			}
167913939Slinton 	case EXTERN:
168013939Slinton 	case STATIC:
168113939Slinton 	case EXTDEF:
168213939Slinton 	case TYPEDEF:
168313939Slinton 	case USTATIC:
168417151Sralph 		if( blevel == 1 ){
168517151Sralph 			uerror( "illegal class" );
168617151Sralph 			return( PARAM );
168717151Sralph 			}
168817151Sralph 	case STNAME:
168917151Sralph 	case UNAME:
169017151Sralph 	case ENAME:
169113939Slinton 		return( class );
169213939Slinton 
169313939Slinton 	default:
169413939Slinton 		cerror( "illegal class: %d", class );
169513939Slinton 		/* NOTREACHED */
169613939Slinton 
169713939Slinton 		}
169813939Slinton 	}
169913939Slinton 
170013939Slinton struct symtab *
170113939Slinton mknonuniq(idindex) int *idindex; {/* locate a symbol table entry for */
170213939Slinton 	/* an occurrence of a nonunique structure member name */
170313939Slinton 	/* or field */
170413939Slinton 	register i;
170513939Slinton 	register struct symtab * sp;
170613939Slinton 	char *p,*q;
170713939Slinton 
170813939Slinton 	sp = & stab[ i= *idindex ]; /* position search at old entry */
170913939Slinton 	while( sp->stype != TNULL ){ /* locate unused entry */
171013939Slinton 		if( ++i >= SYMTSZ ){/* wrap around symbol table */
171113939Slinton 			i = 0;
171213939Slinton 			sp = stab;
171313939Slinton 			}
171413939Slinton 		else ++sp;
171513939Slinton 		if( i == *idindex ) cerror("Symbol table full");
171613939Slinton 		}
171713939Slinton 	sp->sflags = SNONUNIQ | SMOS;
171813939Slinton 	p = sp->sname;
171913939Slinton 	q = stab[*idindex].sname; /* old entry name */
172013939Slinton #ifdef FLEXNAMES
172113939Slinton 	sp->sname = stab[*idindex].sname;
172213939Slinton #endif
172313939Slinton # ifndef BUG1
172413939Slinton 	if( ddebug ){
172513939Slinton 		printf("\tnonunique entry for %s from %d to %d\n",
172613939Slinton 			q, *idindex, i );
172713939Slinton 		}
172813939Slinton # endif
172913939Slinton 	*idindex = i;
173013939Slinton #ifndef FLEXNAMES
173113939Slinton 	for( i=1; i<=NCHNAM; ++i ){ /* copy name */
173213939Slinton 		if( *p++ = *q /* assign */ ) ++q;
173313939Slinton 		}
173413939Slinton #endif
173513939Slinton 	return ( sp );
173613939Slinton 	}
173713939Slinton 
173813939Slinton lookup( name, s) char *name; {
173913939Slinton 	/* look up name: must agree with s w.r.t. STAG, SMOS and SHIDDEN */
174013939Slinton 
174113939Slinton 	register char *p, *q;
174213939Slinton 	int i, j, ii;
174313939Slinton 	register struct symtab *sp;
174413939Slinton 
174513939Slinton 	/* compute initial hash index */
174613939Slinton # ifndef BUG1
174713939Slinton 	if( ddebug > 2 ){
174813939Slinton 		printf( "lookup( %s, %d ), stwart=%d, instruct=%d\n", name, s, stwart, instruct );
174913939Slinton 		}
175013939Slinton # endif
175113939Slinton 
175213939Slinton 	i = 0;
175313939Slinton #ifndef FLEXNAMES
175413939Slinton 	for( p=name, j=0; *p != '\0'; ++p ){
175513939Slinton 		i += *p;
175613939Slinton 		if( ++j >= NCHNAM ) break;
175713939Slinton 		}
175813939Slinton #else
175913939Slinton 	i = (int)name;
176013939Slinton #endif
176113939Slinton 	i = i%SYMTSZ;
176213939Slinton 	sp = &stab[ii=i];
176313939Slinton 
176413939Slinton 	for(;;){ /* look for name */
176513939Slinton 
176613939Slinton 		if( sp->stype == TNULL ){ /* empty slot */
176713939Slinton 			sp->sflags = s;  /* set STAG, SMOS if needed, turn off all others */
176813939Slinton #ifndef FLEXNAMES
176913939Slinton 			p = sp->sname;
177013939Slinton 			for( j=0; j<NCHNAM; ++j ) if( *p++ = *name ) ++name;
177113939Slinton #else
177213939Slinton 			sp->sname = name;
177313939Slinton #endif
177413939Slinton 			sp->stype = UNDEF;
177513939Slinton 			sp->sclass = SNULL;
177613939Slinton 			return( i );
177713939Slinton 			}
177813939Slinton 		if( (sp->sflags & (STAG|SMOS|SHIDDEN)) != s ) goto next;
177913939Slinton 		p = sp->sname;
178013939Slinton 		q = name;
178113939Slinton #ifndef FLEXNAMES
178213939Slinton 		for( j=0; j<NCHNAM;++j ){
178313939Slinton 			if( *p++ != *q ) goto next;
178413939Slinton 			if( !*q++ ) break;
178513939Slinton 			}
178613939Slinton 		return( i );
178713939Slinton #else
178813939Slinton 		if (p == q)
178913939Slinton 			return ( i );
179013939Slinton #endif
179113939Slinton 	next:
179213939Slinton 		if( ++i >= SYMTSZ ){
179313939Slinton 			i = 0;
179413939Slinton 			sp = stab;
179513939Slinton 			}
179613939Slinton 		else ++sp;
179713939Slinton 		if( i == ii ) cerror( "symbol table full" );
179813939Slinton 		}
179913939Slinton 	}
180013939Slinton 
180113939Slinton #ifndef checkst
180213939Slinton /* if not debugging, make checkst a macro */
180313939Slinton checkst(lev){
180413939Slinton 	register int s, i, j;
180513939Slinton 	register struct symtab *p, *q;
180613939Slinton 
180713939Slinton 	for( i=0, p=stab; i<SYMTSZ; ++i, ++p ){
180813939Slinton 		if( p->stype == TNULL ) continue;
180913939Slinton 		j = lookup( p->sname, p->sflags&(SMOS|STAG) );
181013939Slinton 		if( j != i ){
181113939Slinton 			q = &stab[j];
181213939Slinton 			if( q->stype == UNDEF ||
181313939Slinton 			    q->slevel <= p->slevel ){
181413939Slinton #ifndef FLEXNAMES
181513939Slinton 				cerror( "check error: %.8s", q->sname );
181613939Slinton #else
181713939Slinton 				cerror( "check error: %s", q->sname );
181813939Slinton #endif
181913939Slinton 				}
182013939Slinton 			}
182113939Slinton #ifndef FLEXNAMES
182213939Slinton 		else if( p->slevel > lev ) cerror( "%.8s check at level %d", p->sname, lev );
182313939Slinton #else
182413939Slinton 		else if( p->slevel > lev ) cerror( "%s check at level %d", p->sname, lev );
182513939Slinton #endif
182613939Slinton 		}
182713939Slinton 	}
182813939Slinton #endif
182913939Slinton 
183013939Slinton struct symtab *
183113939Slinton relook(p) register struct symtab *p; {  /* look up p again, and see where it lies */
183213939Slinton 
183313939Slinton 	register struct symtab *q;
183413939Slinton 
183513939Slinton 	/* I'm not sure that this handles towers of several hidden definitions in all cases */
183613939Slinton 	q = &stab[lookup( p->sname, p->sflags&(STAG|SMOS|SHIDDEN) )];
183713939Slinton 	/* make relook always point to either p or an empty cell */
183813939Slinton 	if( q->stype == UNDEF ){
183913939Slinton 		q->stype = TNULL;
184013939Slinton 		return(q);
184113939Slinton 		}
184213939Slinton 	while( q != p ){
184313939Slinton 		if( q->stype == TNULL ) break;
184413939Slinton 		if( ++q >= &stab[SYMTSZ] ) q=stab;
184513939Slinton 		}
184613939Slinton 	return(q);
184713939Slinton 	}
184813939Slinton 
184924405Smckusick clearst( lev ) register int lev; {
185024405Smckusick 	register struct symtab *p, *q;
185124405Smckusick 	register int temp;
185224405Smckusick 	struct symtab *clist = 0;
185313939Slinton 
185413939Slinton 	temp = lineno;
185513939Slinton 	aobeg();
185613939Slinton 
185724405Smckusick 	/* step 1: remove entries */
185824405Smckusick 	while( chaintop-1 > lev ){
185924405Smckusick 		register int type;
186013939Slinton 
186124405Smckusick 		p = schain[--chaintop];
186224405Smckusick 		schain[chaintop] = 0;
186324405Smckusick 		for( ; p; p = q ){
186424405Smckusick 			q = p->snext;
186524405Smckusick 			type = p->stype;
186624405Smckusick 			if( p->stype == TNULL || p->slevel <= lev )
186724405Smckusick 				cerror( "schain botch" );
186824405Smckusick 			lineno = p->suse < 0 ? -p->suse : p->suse;
186924405Smckusick 			if( p->stype==UNDEF || ( p->sclass==ULABEL && lev<2 ) ){
187013939Slinton 				lineno = temp;
187113939Slinton #ifndef FLEXNAMES
187213939Slinton 				uerror( "%.8s undefined", p->sname );
187313939Slinton #else
187413939Slinton 				uerror( "%s undefined", p->sname );
187513939Slinton #endif
187613939Slinton 				}
187713939Slinton 			else aocode(p);
187813939Slinton # ifndef BUG1
187924405Smckusick 			if( ddebug ){
188013939Slinton #ifndef FLEXNAMES
188124405Smckusick 				printf( "removing %.8s", p->sname );
188213939Slinton #else
188324405Smckusick 				printf( "removing %s", p->sname );
188413939Slinton #endif
188524405Smckusick 				printf( " from stab[%d], flags %o level %d\n",
188624405Smckusick 					p-stab, p->sflags, p->slevel);
188724405Smckusick 				}
188813939Slinton # endif
188924405Smckusick 			if( p->sflags & SHIDES )unhide( p );
189013939Slinton 			p->stype = TNULL;
189124405Smckusick 			p->snext = clist;
189224405Smckusick 			clist = p;
189313939Slinton 			}
189424405Smckusick 		}
189524405Smckusick 
189624405Smckusick 	/* step 2: fix any mishashed entries */
189724405Smckusick 	p = clist;
189824405Smckusick 	while( p ){
189927247Sdonn 		register struct symtab *r, *next;
190024405Smckusick 
190124405Smckusick 		q = p;
190227247Sdonn 		next = p->snext;
190324405Smckusick 		for(;;){
190424405Smckusick 			if( ++q >= &stab[SYMTSZ] )q = stab;
190524405Smckusick 			if( q == p || q->stype == TNULL )break;
190624405Smckusick 			if( (r = relook(q)) != q ) {
190724405Smckusick 				*r = *q;
190826822Sdonn 				q->stype = TNULL;
190913939Slinton 				}
191013939Slinton 			}
191127247Sdonn 		p = next;
191213939Slinton 		}
191324405Smckusick 
191413939Slinton 	lineno = temp;
191513939Slinton 	aoend();
191613939Slinton 	}
191713939Slinton 
191813939Slinton hide( p ) register struct symtab *p; {
191913939Slinton 	register struct symtab *q;
192013939Slinton 	for( q=p+1; ; ++q ){
192113939Slinton 		if( q >= &stab[SYMTSZ] ) q = stab;
192213939Slinton 		if( q == p ) cerror( "symbol table full" );
192313939Slinton 		if( q->stype == TNULL ) break;
192413939Slinton 		}
192524405Smckusick 	*q = *p;
192613939Slinton 	p->sflags |= SHIDDEN;
192713939Slinton 	q->sflags = (p->sflags&(SMOS|STAG)) | SHIDES;
192813939Slinton #ifndef FLEXNAMES
192913939Slinton 	if( hflag ) werror( "%.8s redefinition hides earlier one", p->sname );
193013939Slinton #else
193113939Slinton 	if( hflag ) werror( "%s redefinition hides earlier one", p->sname );
193213939Slinton #endif
193313939Slinton # ifndef BUG1
193413939Slinton 	if( ddebug ) printf( "	%d hidden in %d\n", p-stab, q-stab );
193513939Slinton # endif
193613939Slinton 	return( idname = q-stab );
193713939Slinton 	}
193813939Slinton 
193913939Slinton unhide( p ) register struct symtab *p; {
194013939Slinton 	register struct symtab *q;
194113939Slinton 	register s, j;
194213939Slinton 
194313939Slinton 	s = p->sflags & (SMOS|STAG);
194413939Slinton 	q = p;
194513939Slinton 
194613939Slinton 	for(;;){
194713939Slinton 
194813939Slinton 		if( q == stab ) q = &stab[SYMTSZ-1];
194913939Slinton 		else --q;
195013939Slinton 
195113939Slinton 		if( q == p ) break;
195213939Slinton 
195313939Slinton 		if( (q->sflags&(SMOS|STAG)) == s ){
195413939Slinton #ifndef FLEXNAMES
195513939Slinton 			for( j =0; j<NCHNAM; ++j ) if( p->sname[j] != q->sname[j] ) break;
195613939Slinton 			if( j == NCHNAM ){ /* found the name */
195713939Slinton #else
195813939Slinton 			if (p->sname == q->sname) {
195913939Slinton #endif
196013939Slinton 				q->sflags &= ~SHIDDEN;
196113939Slinton # ifndef BUG1
196213939Slinton 				if( ddebug ) printf( "unhide uncovered %d from %d\n", q-stab,p-stab);
196313939Slinton # endif
196413939Slinton 				return;
196513939Slinton 				}
196613939Slinton 			}
196713939Slinton 
196813939Slinton 		}
196913939Slinton 	cerror( "unhide fails" );
197013939Slinton 	}
1971