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