xref: /csrg-svn/old/pcc/mip/pftn.c (revision 32817)
1 #ifndef lint
2 static char *sccsid ="@(#)pftn.c	1.19 (Berkeley) 12/10/87";
3 #endif lint
4 
5 # include "pass1.h"
6 
7 unsigned int offsz;
8 
9 struct symtab *schain[MAXSCOPES];	/* sym chains for clearst */
10 int chaintop;				/* highest active entry */
11 
12 struct instk {
13 	int in_sz;   /* size of array element */
14 	int in_x;    /* current index for structure member in structure initializations */
15 	int in_n;    /* number of initializations seen */
16 	int in_s;    /* sizoff */
17 	int in_d;    /* dimoff */
18 	TWORD in_t;    /* type */
19 	int in_id;   /* stab index */
20 	int in_fl;   /* flag which says if this level is controlled by {} */
21 	OFFSZ in_off;  /* offset of the beginning of this level */
22 	}
23 instack[10],
24 *pstk;
25 
26 	/* defines used for getting things off of the initialization stack */
27 
28 
29 struct symtab *relook();
30 
31 
32 int ddebug = 0;
33 
34 struct symtab * mknonuniq();
35 
36 defid( q, class ) register NODE *q; register int class; {
37 	register struct symtab *p;
38 	int idp;
39 	register TWORD type;
40 	TWORD stp;
41 	register int scl;
42 	int dsym, ddef;
43 	int slev, temp;
44 	int changed;
45 
46 	if( q == NIL ) return;  /* an error was detected */
47 
48 	if( q < node || q >= &node[TREESZ] ) cerror( "defid call" );
49 
50 	idp = q->tn.rval;
51 
52 	if( idp < 0 ) cerror( "tyreduce" );
53 	p = &stab[idp];
54 
55 # ifndef BUG1
56 	if( ddebug ){
57 #ifndef FLEXNAMES
58 		printf( "defid( %.8s (%d), ", p->sname, idp );
59 #else
60 		printf( "defid( %s (%d), ", p->sname, idp );
61 #endif
62 		tprint( q->in.type );
63 		printf( ", %s, (%d,%d) ), level %d\n", scnames(class), q->fn.cdim, q->fn.csiz, blevel );
64 		}
65 # endif
66 
67 	fixtype( q, class );
68 
69 	type = q->in.type;
70 	class = fixclass( class, type );
71 
72 	stp = p->stype;
73 	slev = p->slevel;
74 
75 # ifndef BUG1
76 	if( ddebug ){
77 		printf( "	modified to " );
78 		tprint( type );
79 		printf( ", %s\n", scnames(class) );
80 		printf( "	previous def'n: " );
81 		tprint( stp );
82 		printf( ", %s, (%d,%d) ), level %d\n", scnames(p->sclass), p->dimoff, p->sizoff, slev );
83 		}
84 # endif
85 
86 	if( stp == FTN && p->sclass == SNULL )goto enter;
87 	if( blevel==1 && stp!=FARG ) switch( class ){
88 
89 		default:
90 #ifndef FLEXNAMES
91 			if(!(class&FIELD)) uerror( "declared argument %.8s is missing", p->sname );
92 #else
93 			if(!(class&FIELD)) uerror( "declared argument %s is missing", p->sname );
94 #endif
95 		case MOS:
96 		case STNAME:
97 		case MOU:
98 		case UNAME:
99 		case MOE:
100 		case ENAME:
101 		case TYPEDEF:
102 			;
103 			}
104 	if( stp == UNDEF|| stp == FARG ) goto enter;
105 
106 	if( type != stp ) goto mismatch;
107 	/* test (and possibly adjust) dimensions */
108 	dsym = p->dimoff;
109 	ddef = q->fn.cdim;
110 	changed = 0;
111 	for( temp=type; temp&TMASK; temp = DECREF(temp) ){
112 		if( ISARY(temp) ){
113 			if (dimtab[dsym] == 0) {
114 				dimtab[dsym] = dimtab[ddef];
115 				changed = 1;
116 				}
117 			else if (dimtab[ddef]!=0&&dimtab[dsym]!=dimtab[ddef]) {
118 				goto mismatch;
119 				}
120 			++dsym;
121 			++ddef;
122 			}
123 		}
124 
125 	if (changed) {
126 		FIXDEF(p);
127 		}
128 
129 	/* check that redeclarations are to the same structure */
130 	if( (temp==STRTY||temp==UNIONTY||temp==ENUMTY) && p->sizoff != q->fn.csiz
131 		 && class!=STNAME && class!=UNAME && class!=ENAME ){
132 		goto mismatch;
133 		}
134 
135 	scl = ( p->sclass );
136 
137 # ifndef BUG1
138 	if( ddebug ){
139 		printf( "	previous class: %s\n", scnames(scl) );
140 		}
141 # endif
142 
143 	if( class&FIELD ){
144 		/* redefinition */
145 		if( !falloc( p, class&FLDSIZ, 1, NIL ) ) {
146 			/* successful allocation */
147 			psave( idp );
148 			return;
149 			}
150 		/* blew it: resume at end of switch... */
151 		}
152 
153 	else switch( class ){
154 
155 	case EXTERN:
156 		switch( scl ){
157 		case STATIC:
158 		case USTATIC:
159 			if( slev==0 ) return;
160 			break;
161 		case EXTDEF:
162 		case EXTERN:
163 		case FORTRAN:
164 		case UFORTRAN:
165 			return;
166 			}
167 		break;
168 
169 	case STATIC:
170 		if( scl==USTATIC || (scl==EXTERN && blevel==0) ){
171 			p->sclass = STATIC;
172 			if( ISFTN(type) ) curftn = idp;
173 			return;
174 			}
175 		break;
176 
177 	case USTATIC:
178 		if( scl==STATIC || scl==USTATIC ) return;
179 		break;
180 
181 	case LABEL:
182 		if( scl == ULABEL ){
183 			p->sclass = LABEL;
184 			deflab( p->offset );
185 			return;
186 			}
187 		break;
188 
189 	case TYPEDEF:
190 		if( scl == class ) return;
191 		break;
192 
193 	case UFORTRAN:
194 		if( scl == UFORTRAN || scl == FORTRAN ) return;
195 		break;
196 
197 	case FORTRAN:
198 		if( scl == UFORTRAN ){
199 			p->sclass = FORTRAN;
200 			if( ISFTN(type) ) curftn = idp;
201 			return;
202 			}
203 		break;
204 
205 	case MOU:
206 	case MOS:
207 		if( scl == class ) {
208 			if( oalloc( p, &strucoff ) ) break;
209 			if( class == MOU ) strucoff = 0;
210 			psave( idp );
211 			return;
212 			}
213 		break;
214 
215 	case MOE:
216 		if( scl == class ){
217 			if( p->offset!= strucoff++ ) break;
218 			psave( idp );
219 			}
220 		break;
221 
222 	case EXTDEF:
223 		if( scl == EXTERN ) {
224 			p->sclass = EXTDEF;
225 			if( ISFTN(type) ) curftn = idp;
226 			return;
227 			}
228 		break;
229 
230 	case STNAME:
231 	case UNAME:
232 	case ENAME:
233 		if( scl != class ) break;
234 		if( dimtab[p->sizoff] == 0 ) return;  /* previous entry just a mention */
235 		break;
236 
237 	case ULABEL:
238 		if( scl == LABEL || scl == ULABEL ) return;
239 	case PARAM:
240 	case AUTO:
241 	case REGISTER:
242 		;  /* mismatch.. */
243 
244 		}
245 
246 	mismatch:
247 	/* allow nonunique structure/union member names */
248 
249 	if( class==MOU || class==MOS || class & FIELD ){/* make a new entry */
250 		register int *memp;
251 		p->sflags |= SNONUNIQ;  /* old entry is nonunique */
252 		/* determine if name has occurred in this structure/union */
253 		if (paramno > 0) for( memp = &paramstk[paramno-1];
254 			/* while */ *memp>=0 && stab[*memp].sclass != STNAME
255 				&& stab[*memp].sclass != UNAME;
256 			/* iterate */ --memp){ char *cname, *oname;
257 			if( stab[*memp].sflags & SNONUNIQ ){
258 				cname=p->sname;
259 				oname=stab[*memp].sname;
260 #ifndef FLEXNAMES
261 				for(temp=1; temp<=NCHNAM; ++temp){
262 					if(*cname++ != *oname)goto diff;
263 					if(!*oname++)break;
264 					}
265 #else
266 				if (cname != oname) goto diff;
267 #endif
268 				uerror("redeclaration of: %s",p->sname);
269 				break;
270 				diff: continue;
271 				}
272 			}
273 		p = mknonuniq( &idp ); /* update p and idp to new entry */
274 		goto enter;
275 		}
276 	if( blevel > slev && class != EXTERN && class != FORTRAN &&
277 		class != UFORTRAN && !( class == LABEL && slev >= 2 ) ){
278 		q->tn.rval = idp = hide( p );
279 		p = &stab[idp];
280 		goto enter;
281 		}
282 #ifndef FLEXNAMES
283 	uerror( "redeclaration of %.8s", p->sname );
284 #else
285 	uerror( "redeclaration of %s", p->sname );
286 #endif
287 	if( class==EXTDEF && ISFTN(type) ) curftn = idp;
288 	return;
289 
290 	enter:  /* make a new entry */
291 
292 # ifndef BUG1
293 	if( ddebug ) printf( "	new entry made\n" );
294 # endif
295 	if( type == UNDEF ) uerror("void type for %s",p->sname);
296 	p->stype = type;
297 	p->sclass = class;
298 	p->slevel = blevel;
299 	p->offset = NOOFFSET;
300 	p->suse = lineno;
301 	if( class == STNAME || class == UNAME || class == ENAME ) {
302 		p->sizoff = curdim;
303 		dstash( 0 );  /* size */
304 		dstash( -1 ); /* index to members of str or union */
305 		dstash( ALSTRUCT );  /* alignment */
306 		dstash( idp );
307 		}
308 	else {
309 		switch( BTYPE(type) ){
310 		case STRTY:
311 		case UNIONTY:
312 		case ENUMTY:
313 			p->sizoff = q->fn.csiz;
314 			break;
315 		default:
316 			p->sizoff = BTYPE(type);
317 			}
318 		}
319 
320 	/* copy dimensions */
321 
322 	p->dimoff = q->fn.cdim;
323 
324 	/* allocate offsets */
325 	if( class&FIELD ){
326 		(void) falloc( p, class&FLDSIZ, 0, NIL );  /* new entry */
327 		psave( idp );
328 		}
329 	else switch( class ){
330 
331 	case AUTO:
332 		(void) oalloc( p, &autooff );
333 		break;
334 	case STATIC:
335 	case EXTDEF:
336 		p->offset = getlab();
337 		if( ISFTN(type) ) curftn = idp;
338 		break;
339 	case ULABEL:
340 	case LABEL:
341 		p->offset = getlab();
342 		p->slevel = 2;
343 		if( class == LABEL ){
344 			(void) locctr( PROG );
345 			deflab( p->offset );
346 			}
347 		break;
348 
349 	case EXTERN:
350 	case UFORTRAN:
351 	case FORTRAN:
352 		p->offset = getlab();
353 		p->slevel = 0;
354 		break;
355 	case MOU:
356 	case MOS:
357 		(void) oalloc( p, &strucoff );
358 		if( class == MOU ) strucoff = 0;
359 		psave( idp );
360 		break;
361 
362 	case MOE:
363 		p->offset = strucoff++;
364 		psave( idp );
365 		break;
366 	case REGISTER:
367 		p->offset = regvar--;
368 		if( blevel == 1 ) p->sflags |= SSET;
369 		if( regvar < minrvar ) minrvar = regvar;
370 		break;
371 		}
372 
373 	{
374 		register int l = p->slevel;
375 
376 		if( l >= MAXSCOPES )
377 			cerror( "scopes nested too deep" );
378 
379 		p->snext = schain[l];
380 		schain[l] = p;
381 		if( l >= chaintop )
382 			chaintop = l + 1;
383 		}
384 
385 	/* user-supplied routine to fix up new definitions */
386 
387 	FIXDEF(p);
388 
389 # ifndef BUG1
390 	if( ddebug ) printf( "	dimoff, sizoff, offset: %d, %d, %d\n", p->dimoff, p->sizoff, p->offset );
391 # endif
392 
393 	}
394 
395 psave( i ){
396 	if( paramno >= PARAMSZ ){
397 		cerror( "parameter stack overflow");
398 		}
399 	paramstk[ paramno++ ] = i;
400 	}
401 
402 ftnend(){ /* end of function */
403 	if( retlab != NOLAB && nerrors == 0 ){ /* inside a real function */
404 		efcode();
405 		}
406 	checkst(0);
407 	retstat = 0;
408 	tcheck();
409 	curclass = SNULL;
410 	brklab = contlab = retlab = NOLAB;
411 	flostat = 0;
412 	if( nerrors == 0 ){
413 		if( psavbc != & asavbc[0] ) cerror("bcsave error");
414 		if( paramno != 0 ) cerror("parameter reset error");
415 		if( swx != 0 ) cerror( "switch error");
416 		}
417 	psavbc = &asavbc[0];
418 	paramno = 0;
419 	autooff = AUTOINIT;
420 	minrvar = regvar = MAXRVAR;
421 	reached = 1;
422 	swx = 0;
423 	swp = swtab;
424 	(void) locctr(DATA);
425 	}
426 
427 dclargs(){
428 	register i, j;
429 	register struct symtab *p;
430 	register NODE *q;
431 	argoff = ARGINIT;
432 # ifndef BUG1
433 	if( ddebug > 2) printf("dclargs()\n");
434 # endif
435 	for( i=0; i<paramno; ++i ){
436 		if( (j = paramstk[i]) < 0 ) continue;
437 		p = &stab[j];
438 # ifndef BUG1
439 		if( ddebug > 2 ){
440 			printf("\t%s (%d) ",p->sname, j);
441 			tprint(p->stype);
442 			printf("\n");
443 			}
444 # endif
445 		if( p->stype == FARG ) {
446 			q = block(FREE,NIL,NIL,INT,0,INT);
447 			q->tn.rval = j;
448 			defid( q, PARAM );
449 			}
450 		FIXARG(p); /* local arg hook, eg. for sym. debugger */
451 		oalloc( p, &argoff );  /* always set aside space, even for register arguments */
452 		}
453 	cendarg();
454 	(void) locctr(PROG);
455 	defalign(ALINT);
456 	ftnno = getlab();
457 	bfcode( paramstk, paramno );
458 	paramno = 0;
459 	}
460 
461 NODE *
462 rstruct( idn, soru ){ /* reference to a structure or union, with no definition */
463 	register struct symtab *p;
464 	register NODE *q;
465 	p = &stab[idn];
466 	switch( p->stype ){
467 
468 	case UNDEF:
469 	def:
470 		q = block( FREE, NIL, NIL, 0, 0, 0 );
471 		q->tn.rval = idn;
472 		q->in.type = (soru&INSTRUCT) ? STRTY : ( (soru&INUNION) ? UNIONTY : ENUMTY );
473 		defid( q, (soru&INSTRUCT) ? STNAME : ( (soru&INUNION) ? UNAME : ENAME ) );
474 		break;
475 
476 	case STRTY:
477 		if( soru & INSTRUCT ) break;
478 		goto def;
479 
480 	case UNIONTY:
481 		if( soru & INUNION ) break;
482 		goto def;
483 
484 	case ENUMTY:
485 		if( !(soru&(INUNION|INSTRUCT)) ) break;
486 		goto def;
487 
488 		}
489 	stwart = instruct;
490 	return( mkty( p->stype, 0, p->sizoff ) );
491 	}
492 
493 moedef( idn ){
494 	register NODE *q;
495 
496 	q = block( FREE, NIL, NIL, MOETY, 0, 0 );
497 	q->tn.rval = idn;
498 	if( idn>=0 ) defid( q, MOE );
499 	}
500 
501 bstruct( idn, soru ){ /* begining of structure or union declaration */
502 	register NODE *q;
503 
504 	psave( instruct );
505 	psave( curclass );
506 	psave( strucoff );
507 	strucoff = 0;
508 	instruct = soru;
509 	q = block( FREE, NIL, NIL, 0, 0, 0 );
510 	q->tn.rval = idn;
511 	if( instruct==INSTRUCT ){
512 		curclass = MOS;
513 		q->in.type = STRTY;
514 		if( idn >= 0 ) defid( q, STNAME );
515 		}
516 	else if( instruct == INUNION ) {
517 		curclass = MOU;
518 		q->in.type = UNIONTY;
519 		if( idn >= 0 ) defid( q, UNAME );
520 		}
521 	else { /* enum */
522 		curclass = MOE;
523 		q->in.type = ENUMTY;
524 		if( idn >= 0 ) defid( q, ENAME );
525 		}
526 	psave( idn = q->tn.rval );
527 	/* the "real" definition is where the members are seen */
528 	if ( idn >= 0 ) stab[idn].suse = lineno;
529 	return( paramno-4 );
530 	}
531 
532 NODE *
533 dclstruct( oparam ){
534 	register struct symtab *p;
535 	register i, al, sa, j, sz, szindex;
536 	register TWORD temp;
537 	register high, low;
538 
539 	/* paramstk contains:
540 		paramstk[ oparam ] = previous instruct
541 		paramstk[ oparam+1 ] = previous class
542 		paramstk[ oparam+2 ] = previous strucoff
543 		paramstk[ oparam+3 ] = structure name
544 
545 		paramstk[ oparam+4, ... ]  = member stab indices
546 
547 		*/
548 
549 
550 	if( (i=paramstk[oparam+3]) < 0 ){
551 		szindex = curdim;
552 		dstash( 0 );  /* size */
553 		dstash( -1 );  /* index to member names */
554 		dstash( ALSTRUCT );  /* alignment */
555 		dstash( -lineno );	/* name of structure */
556 		}
557 	else {
558 		szindex = stab[i].sizoff;
559 		}
560 
561 # ifndef BUG1
562 	if( ddebug ){
563 #ifndef FLEXNAMES
564 		printf( "dclstruct( %.8s ), szindex = %d\n", (i>=0)? stab[i].sname : "??", szindex );
565 #else
566 		printf( "dclstruct( %s ), szindex = %d\n", (i>=0)? stab[i].sname : "??", szindex );
567 #endif
568 		}
569 # endif
570 	temp = (instruct&INSTRUCT)?STRTY:((instruct&INUNION)?UNIONTY:ENUMTY);
571 	stwart = instruct = paramstk[ oparam ];
572 	curclass = paramstk[ oparam+1 ];
573 	dimtab[ szindex+1 ] = curdim;
574 	al = ALSTRUCT;
575 
576 	high = low = 0;
577 
578 	for( i = oparam+4;  i< paramno; ++i ){
579 		dstash( j=paramstk[i] );
580 		if( j<0 || j>= SYMTSZ ) cerror( "gummy structure member" );
581 		p = &stab[j];
582 		if( temp == ENUMTY ){
583 			if( p->offset < low ) low = p->offset;
584 			if( p->offset > high ) high = p->offset;
585 			p->sizoff = szindex;
586 			continue;
587 			}
588 		sa = talign( p->stype, p->sizoff );
589 		if( p->sclass & FIELD ){
590 			sz = p->sclass&FLDSIZ;
591 			}
592 		else {
593 			sz = tsize( p->stype, p->dimoff, p->sizoff );
594 			}
595 		if( sz == 0 ){
596 #ifndef FLEXNAMES
597 			werror( "illegal zero sized structure member: %.8s", p->sname );
598 #else
599 			werror( "illegal zero sized structure member: %s", p->sname );
600 #endif
601 			}
602 		if( sz > strucoff ) strucoff = sz;  /* for use with unions */
603 		SETOFF( al, sa );
604 		/* set al, the alignment, to the lcm of the alignments of the members */
605 		}
606 	dstash( -1 );  /* endmarker */
607 	SETOFF( strucoff, al );
608 
609 	if( temp == ENUMTY ){
610 		register TWORD ty;
611 
612 # ifdef ENUMSIZE
613 		ty = ENUMSIZE(high,low);
614 # else
615 		if( (char)high == high && (char)low == low ) ty = ctype( CHAR );
616 		else if( (short)high == high && (short)low == low ) ty = ctype( SHORT );
617 		else ty = ctype(INT);
618 #endif
619 		strucoff = tsize( ty, 0, (int)ty );
620 		dimtab[ szindex+2 ] = al = talign( ty, (int)ty );
621 		}
622 
623 	if( strucoff == 0 ) uerror( "zero sized structure" );
624 	dimtab[ szindex ] = strucoff;
625 	dimtab[ szindex+2 ] = al;
626 	dimtab[ szindex+3 ] = paramstk[ oparam+3 ];  /* name index */
627 
628 	FIXSTRUCT( szindex, oparam ); /* local hook, eg. for sym debugger */
629 # ifndef BUG1
630 	if( ddebug>1 ){
631 		printf( "\tdimtab[%d,%d,%d] = %d,%d,%d\n", szindex,szindex+1,szindex+2,
632 				dimtab[szindex],dimtab[szindex+1],dimtab[szindex+2] );
633 		for( i = dimtab[szindex+1]; dimtab[i] >= 0; ++i ){
634 #ifndef FLEXNAMES
635 			printf( "\tmember %.8s(%d)\n", stab[dimtab[i]].sname, dimtab[i] );
636 #else
637 			printf( "\tmember %s(%d)\n", stab[dimtab[i]].sname, dimtab[i] );
638 #endif
639 			}
640 		}
641 # endif
642 
643 	strucoff = paramstk[ oparam+2 ];
644 	paramno = oparam;
645 
646 	return( mkty( temp, 0, szindex ) );
647 	}
648 
649 	/* VARARGS */
650 yyerror( s ) char *s; { /* error printing routine in parser */
651 
652 	uerror( s );
653 
654 	}
655 
656 yyaccpt(){
657 	ftnend();
658 	}
659 
660 ftnarg( idn ) {
661 	switch( stab[idn].stype ){
662 
663 	case UNDEF:
664 		/* this parameter, entered at scan */
665 		break;
666 	case FARG:
667 #ifndef FLEXNAMES
668 		uerror("redeclaration of formal parameter, %.8s",
669 #else
670 		uerror("redeclaration of formal parameter, %s",
671 #endif
672 			stab[idn].sname);
673 		/* fall thru */
674 	case FTN:
675 		/* the name of this function matches parm */
676 		/* fall thru */
677 	default:
678 		idn = hide( &stab[idn]);
679 		break;
680 	case TNULL:
681 		/* unused entry, fill it */
682 		;
683 		}
684 	stab[idn].stype = FARG;
685 	stab[idn].sclass = PARAM;
686 	psave( idn );
687 	}
688 
689 talign( ty, s) register unsigned ty; register s; {
690 	/* compute the alignment of an object with type ty, sizeoff index s */
691 
692 	register i;
693 	if( s<0 && ty!=INT && ty!=CHAR && ty!=SHORT && ty!=UNSIGNED && ty!=UCHAR && ty!=USHORT
694 #ifdef LONGFIELDS
695 		&& ty!=LONG && ty!=ULONG
696 #endif
697 					){
698 		return( fldal( ty ) );
699 		}
700 
701 	for( i=0; i<=(SZINT-BTSHIFT-1); i+=TSHIFT ){
702 		switch( (ty>>i)&TMASK ){
703 
704 		case FTN:
705 			cerror( "compiler takes alignment of function");
706 		case PTR:
707 			return( ALPOINT );
708 		case ARY:
709 			continue;
710 		case 0:
711 			break;
712 			}
713 		}
714 
715 	switch( BTYPE(ty) ){
716 
717 	case UNIONTY:
718 	case ENUMTY:
719 	case STRTY:
720 		return( (unsigned int) dimtab[ s+2 ] );
721 	case CHAR:
722 	case UCHAR:
723 		return( ALCHAR );
724 	case FLOAT:
725 		return( ALFLOAT );
726 	case DOUBLE:
727 		return( ALDOUBLE );
728 	case LONG:
729 	case ULONG:
730 		return( ALLONG );
731 	case SHORT:
732 	case USHORT:
733 		return( ALSHORT );
734 	default:
735 		return( ALINT );
736 		}
737 	}
738 
739 OFFSZ
740 tsize( ty, d, s )  TWORD ty; {
741 	/* compute the size associated with type ty,
742 	    dimoff d, and sizoff s */
743 	/* BETTER NOT BE CALLED WHEN t, d, and s REFER TO A BIT FIELD... */
744 
745 	int i;
746 	OFFSZ mult;
747 
748 	mult = 1;
749 
750 	for( i=0; i<=(SZINT-BTSHIFT-1); i+=TSHIFT ){
751 		switch( (ty>>i)&TMASK ){
752 
753 		case FTN:
754 			cerror( "compiler takes size of function");
755 		case PTR:
756 			return( SZPOINT * mult );
757 		case ARY:
758 			mult *= (unsigned int) dimtab[ d++ ];
759 			continue;
760 		case 0:
761 			break;
762 
763 			}
764 		}
765 
766 	if( dimtab[s]==0 ) {
767 		if( ty == STRTY )
768 			uerror( "undefined structure" );
769 		else
770 			uerror( "unknown size");
771 		return( SZINT );
772 		}
773 	return( (unsigned int) dimtab[ s ] * mult );
774 	}
775 
776 inforce( n ) OFFSZ n; {  /* force inoff to have the value n */
777 	/* inoff is updated to have the value n */
778 	OFFSZ wb;
779 	register rest;
780 	/* rest is used to do a lot of conversion to ints... */
781 
782 	if( inoff == n ) return;
783 	if( inoff > n ) {
784 		cerror( "initialization alignment error");
785 		}
786 
787 	wb = inoff;
788 	SETOFF( wb, SZINT );
789 
790 	/* wb now has the next higher word boundary */
791 
792 	if( wb >= n ){ /* in the same word */
793 		rest = n - inoff;
794 		vfdzero( rest );
795 		return;
796 		}
797 
798 	/* otherwise, extend inoff to be word aligned */
799 
800 	rest = wb - inoff;
801 	vfdzero( rest );
802 
803 	/* now, skip full words until near to n */
804 
805 	rest = (n-inoff)/SZINT;
806 	zecode( rest );
807 
808 	/* now, the remainder of the last word */
809 
810 	rest = n-inoff;
811 	vfdzero( rest );
812 	if( inoff != n ) cerror( "inoff error");
813 
814 	}
815 
816 vfdalign( n ){ /* make inoff have the offset the next alignment of n */
817 	OFFSZ m;
818 
819 	m = inoff;
820 	SETOFF( m, n );
821 	inforce( m );
822 	}
823 
824 
825 int idebug = 0;
826 
827 int ibseen = 0;  /* the number of } constructions which have been filled */
828 
829 int ifull = 0; /* 1 if all initializers have been seen */
830 
831 int iclass;  /* storage class of thing being initialized */
832 
833 int ilocctr = 0;  /* location counter for current initialization */
834 
835 beginit(curid){
836 	/* beginning of initilization; set location ctr and set type */
837 	register struct symtab *p;
838 
839 # ifndef BUG1
840 	if( idebug >= 3 ) printf( "beginit(), curid = %d\n", curid );
841 # endif
842 
843 	p = &stab[curid];
844 
845 	iclass = p->sclass;
846 	if( curclass == EXTERN || curclass == FORTRAN ) iclass = EXTERN;
847 	switch( iclass ){
848 
849 	case UNAME:
850 	case EXTERN:
851 		return;
852 	case AUTO:
853 	case REGISTER:
854 		break;
855 	case EXTDEF:
856 	case STATIC:
857 		ilocctr = ISARY(p->stype)?ADATA:DATA;
858 		if( nerrors == 0 ){
859 			(void) locctr( ilocctr );
860 			defalign( talign( p->stype, p->sizoff ) );
861 			defnam( p );
862 			}
863 
864 		}
865 
866 	inoff = 0;
867 	ibseen = 0;
868 	ifull = 0;
869 
870 	pstk = 0;
871 
872 	instk( curid, p->stype, p->dimoff, p->sizoff, inoff );
873 
874 	}
875 
876 instk( id, t, d, s, off ) OFFSZ off; TWORD t; {
877 	/* make a new entry on the parameter stack to initialize id */
878 
879 	register struct symtab *p;
880 
881 	for(;;){
882 # ifndef BUG1
883 		if( idebug ) printf( "instk((%d, %o,%d,%d, %d)\n", id, t, d, s, off );
884 # endif
885 
886 		/* save information on the stack */
887 
888 		if( !pstk ) pstk = instack;
889 		else ++pstk;
890 
891 		pstk->in_fl = 0;	/* { flag */
892 		pstk->in_id =  id ;
893 		pstk->in_t =  t ;
894 		pstk->in_d =  d ;
895 		pstk->in_s =  s ;
896 		pstk->in_n = 0;  /* number seen */
897 		pstk->in_x =  t==STRTY ?dimtab[s+1] : 0 ;
898 		pstk->in_off =  off;   /* offset at the beginning of this element */
899 		/* if t is an array, DECREF(t) can't be a field */
900 		/* INS_sz has size of array elements, and -size for fields */
901 		if( ISARY(t) ){
902 			pstk->in_sz = tsize( DECREF(t), d+1, s );
903 			}
904 		else if( stab[id].sclass & FIELD ){
905 			pstk->in_sz = - ( stab[id].sclass & FLDSIZ );
906 			}
907 		else {
908 			pstk->in_sz = 0;
909 			}
910 
911 		if( (iclass==AUTO || iclass == REGISTER ) &&
912 			(ISARY(t) || t==STRTY) ) uerror( "no automatic aggregate initialization" );
913 
914 		/* now, if this is not a scalar, put on another element */
915 
916 		if( ISARY(t) ){
917 			t = DECREF(t);
918 			++d;
919 			continue;
920 			}
921 		else if( t == STRTY ){
922 			if( dimtab[pstk->in_s] == 0 ){
923 				uerror( "can't initialize undefined structure" );
924 				iclass = -1;
925 				return;
926 				}
927 			id = dimtab[pstk->in_x];
928 			p = &stab[id];
929 			if( p->sclass != MOS && !(p->sclass&FIELD) ) cerror( "insane structure member list" );
930 			t = p->stype;
931 			d = p->dimoff;
932 			s = p->sizoff;
933 			off += p->offset;
934 			continue;
935 			}
936 		else return;
937 		}
938 	}
939 
940 NODE *
941 getstr(){ /* decide if the string is external or an initializer, and get the contents accordingly */
942 
943 	register l, temp;
944 	register NODE *p;
945 
946 	if( (iclass==EXTDEF||iclass==STATIC) && (pstk->in_t == CHAR || pstk->in_t == UCHAR) &&
947 			pstk!=instack && ISARY( pstk[-1].in_t ) ){
948 		/* treat "abc" as { 'a', 'b', 'c', 0 } */
949 		strflg = 1;
950 		ilbrace();  /* simulate { */
951 		inforce( pstk->in_off );
952 		/* if the array is inflexible (not top level), pass in the size and
953 			be prepared to throw away unwanted initializers */
954 		lxstr((pstk-1)!=instack?dimtab[(pstk-1)->in_d]:0);  /* get the contents */
955 		irbrace();  /* simulate } */
956 		return( NIL );
957 		}
958 	else { /* make a label, and get the contents and stash them away */
959 		if( iclass != SNULL ){ /* initializing */
960 			/* fill out previous word, to permit pointer */
961 			vfdalign( ALPOINT );
962 			}
963 		temp = locctr( blevel==0?ISTRNG:STRNG ); /* set up location counter */
964 		deflab( l = getlab() );
965 		strflg = 0;
966 		lxstr(0); /* get the contents */
967 		(void) locctr( blevel==0?ilocctr:temp );
968 		p = buildtree( STRING, NIL, NIL );
969 		p->tn.rval = -l;
970 		return(p);
971 		}
972 	}
973 
974 putbyte( v ){ /* simulate byte v appearing in a list of integer values */
975 	register NODE *p;
976 	p = bcon(v);
977 	incode( p, SZCHAR );
978 	tfree( p );
979 	gotscal();
980 	}
981 
982 endinit(){
983 	register TWORD t;
984 	register d, s, n, d1;
985 
986 # ifndef BUG1
987 	if( idebug ) printf( "endinit(), inoff = %d\n", inoff );
988 # endif
989 
990 	switch( iclass ){
991 
992 	case EXTERN:
993 	case AUTO:
994 	case REGISTER:
995 	case -1:
996 		return;
997 		}
998 
999 	pstk = instack;
1000 
1001 	t = pstk->in_t;
1002 	d = pstk->in_d;
1003 	s = pstk->in_s;
1004 	n = pstk->in_n;
1005 
1006 	if( ISARY(t) ){
1007 		d1 = dimtab[d];
1008 
1009 		vfdalign( pstk->in_sz );  /* fill out part of the last element, if needed */
1010 		n = inoff/pstk->in_sz;  /* real number of initializers */
1011 		if( d1 >= n ){
1012 			/* once again, t is an array, so no fields */
1013 			inforce( tsize( t, d, s ) );
1014 			n = d1;
1015 			}
1016 		if( d1!=0 && d1!=n ) uerror( "too many initializers");
1017 		if( n==0 ) werror( "empty array declaration");
1018 		dimtab[d] = n;
1019 		if( d1==0 ) FIXDEF(&stab[pstk->in_id]);
1020 		}
1021 
1022 	else if( t == STRTY || t == UNIONTY ){
1023 		/* clearly not fields either */
1024 		inforce( tsize( t, d, s ) );
1025 		}
1026 	else if( n > 1 ) uerror( "bad scalar initialization");
1027 	/* this will never be called with a field element... */
1028 	else inforce( tsize(t,d,s) );
1029 
1030 	paramno = 0;
1031 	vfdalign( AL_INIT );
1032 	inoff = 0;
1033 	iclass = SNULL;
1034 
1035 	}
1036 
1037 doinit( p ) register NODE *p; {
1038 
1039 	/* take care of generating a value for the initializer p */
1040 	/* inoff has the current offset (last bit written)
1041 		in the current word being generated */
1042 
1043 	register sz, d, s;
1044 	register TWORD t;
1045 	int o;
1046 
1047 	/* note: size of an individual initializer is assumed to fit into an int */
1048 
1049 	if( iclass < 0 ) goto leave;
1050 	if( iclass == EXTERN || iclass == UNAME ){
1051 		uerror( "cannot initialize extern or union" );
1052 		iclass = -1;
1053 		goto leave;
1054 		}
1055 
1056 	if( iclass == AUTO || iclass == REGISTER ){
1057 		/* do the initialization and get out, without regard
1058 		    for filing out the variable with zeros, etc. */
1059 		bccode();
1060 		idname = pstk->in_id;
1061 		p = buildtree( ASSIGN, buildtree( NAME, NIL, NIL ), p );
1062 		ecomp(p);
1063 		return;
1064 		}
1065 
1066 	if( p == NIL ) return;  /* for throwing away strings that have been turned into lists */
1067 
1068 	if( ifull ){
1069 		uerror( "too many initializers" );
1070 		iclass = -1;
1071 		goto leave;
1072 		}
1073 	if( ibseen ){
1074 		uerror( "} expected");
1075 		goto leave;
1076 		}
1077 
1078 # ifndef BUG1
1079 	if( idebug > 1 ) printf( "doinit(%o)\n", p );
1080 # endif
1081 
1082 	t = pstk->in_t;  /* type required */
1083 	d = pstk->in_d;
1084 	s = pstk->in_s;
1085 	if( pstk->in_sz < 0 ){  /* bit field */
1086 		sz = -pstk->in_sz;
1087 		}
1088 	else {
1089 		sz = tsize( t, d, s );
1090 		}
1091 
1092 	inforce( pstk->in_off );
1093 
1094 	p = buildtree( ASSIGN, block( NAME, NIL,NIL, t, d, s ), p );
1095 	p->in.left->in.op = FREE;
1096 	p->in.left = p->in.right;
1097 	p->in.right = NIL;
1098 	p->in.left = optim( p->in.left );
1099 	o = p->in.left->in.op;
1100 	if( o == UNARY AND ){
1101 		o = p->in.left->in.op = FREE;
1102 		p->in.left = p->in.left->in.left;
1103 		}
1104 	p->in.op = INIT;
1105 
1106 	if( sz < SZINT ){ /* special case: bit fields, etc. */
1107 		if( o != ICON || p->in.left->tn.rval != NONAME )
1108 			uerror( "illegal initialization" );
1109 		else incode( p->in.left, sz );
1110 		}
1111 	else if( o == FCON ){
1112 		fincode( p->in.left->fpn.fval, sz );
1113 		}
1114 	else if( o == DCON ){
1115 		fincode( p->in.left->dpn.dval, sz );
1116 		}
1117 	else {
1118 		p = optim(p);
1119 		if( p->in.left->in.op != ICON ) uerror( "illegal initialization" );
1120 		else cinit( p, sz );
1121 		}
1122 
1123 	gotscal();
1124 
1125 	leave:
1126 	tfree(p);
1127 	}
1128 
1129 gotscal(){
1130 	register t, ix;
1131 	register n, id;
1132 	struct symtab *p;
1133 	OFFSZ temp;
1134 
1135 	for( ; pstk > instack; ) {
1136 
1137 		if( pstk->in_fl ) ++ibseen;
1138 
1139 		--pstk;
1140 
1141 		t = pstk->in_t;
1142 
1143 		if( t == STRTY ){
1144 			ix = ++pstk->in_x;
1145 			if( (id=dimtab[ix]) < 0 ) continue;
1146 
1147 			/* otherwise, put next element on the stack */
1148 
1149 			p = &stab[id];
1150 			instk( id, p->stype, p->dimoff, p->sizoff, p->offset+pstk->in_off );
1151 			return;
1152 			}
1153 		else if( ISARY(t) ){
1154 			n = ++pstk->in_n;
1155 			if( n >= dimtab[pstk->in_d] && pstk > instack ) continue;
1156 
1157 			/* put the new element onto the stack */
1158 
1159 			temp = pstk->in_sz;
1160 			instk( pstk->in_id, (TWORD)DECREF(pstk->in_t), pstk->in_d+1, pstk->in_s,
1161 				pstk->in_off+n*temp );
1162 			return;
1163 			}
1164 
1165 		}
1166 	ifull = 1;
1167 	}
1168 
1169 ilbrace(){ /* process an initializer's left brace */
1170 	register t;
1171 	struct instk *temp;
1172 
1173 	temp = pstk;
1174 
1175 	for( ; pstk > instack; --pstk ){
1176 
1177 		t = pstk->in_t;
1178 		if( t != STRTY && !ISARY(t) ) continue; /* not an aggregate */
1179 		if( pstk->in_fl ){ /* already associated with a { */
1180 			if( pstk->in_n ) uerror( "illegal {");
1181 			continue;
1182 			}
1183 
1184 		/* we have one ... */
1185 		pstk->in_fl = 1;
1186 		break;
1187 		}
1188 
1189 	/* cannot find one */
1190 	/* ignore such right braces */
1191 
1192 	pstk = temp;
1193 	}
1194 
1195 irbrace(){
1196 	/* called when a '}' is seen */
1197 
1198 # ifndef BUG1
1199 	if( idebug ) printf( "irbrace(): paramno = %d on entry\n", paramno );
1200 # endif
1201 
1202 	if( ibseen ) {
1203 		--ibseen;
1204 		return;
1205 		}
1206 
1207 	for( ; pstk > instack; --pstk ){
1208 		if( !pstk->in_fl ) continue;
1209 
1210 		/* we have one now */
1211 
1212 		pstk->in_fl = 0;  /* cancel { */
1213 		gotscal();  /* take it away... */
1214 		return;
1215 		}
1216 
1217 	/* these right braces match ignored left braces: throw out */
1218 	ifull = 1;
1219 
1220 	}
1221 
1222 upoff( size, alignment, poff ) register alignment, *poff; {
1223 	/* update the offset pointed to by poff; return the
1224 	/* offset of a value of size `size', alignment `alignment',
1225 	/* given that off is increasing */
1226 
1227 	register off;
1228 
1229 	off = *poff;
1230 	SETOFF( off, alignment );
1231 	if( (offsz-off) <  size ){
1232 		if( instruct!=INSTRUCT )cerror("too many local variables");
1233 		else cerror("Structure too large");
1234 		}
1235 	*poff = off+size;
1236 	return( off );
1237 	}
1238 
1239 oalloc( p, poff ) register struct symtab *p; register *poff; {
1240 	/* allocate p with offset *poff, and update *poff */
1241 	register al, off, tsz;
1242 	int noff;
1243 
1244 	al = talign( p->stype, p->sizoff );
1245 	noff = off = *poff;
1246 	tsz = tsize( p->stype, p->dimoff, p->sizoff );
1247 #ifdef BACKAUTO
1248 	if( p->sclass == AUTO ){
1249 		if( (offsz-off) < tsz ) cerror("too many local variables");
1250 		noff = off + tsz;
1251 		SETOFF( noff, al );
1252 		off = -noff;
1253 		}
1254 	else
1255 #endif
1256 		if( p->sclass == PARAM && ( tsz < SZINT ) ){
1257 			off = upoff( SZINT, ALINT, &noff );
1258 # ifndef RTOLBYTES
1259 			off = noff - tsz;
1260 #endif
1261 			}
1262 		else
1263 		{
1264 		off = upoff( tsz, al, &noff );
1265 		}
1266 
1267 	if( p->sclass != REGISTER ){ /* in case we are allocating stack space for register arguments */
1268 		if( p->offset == NOOFFSET ) p->offset = off;
1269 		else if( off != p->offset ) return(1);
1270 		}
1271 
1272 	*poff = noff;
1273 	return(0);
1274 	}
1275 
1276 falloc( p, w, new, pty )  register struct symtab *p; NODE *pty; {
1277 	/* allocate a field of width w */
1278 	/* new is 0 if new entry, 1 if redefinition, -1 if alignment */
1279 
1280 	register al,sz,type;
1281 
1282 	type = (new<0)? pty->in.type : p->stype;
1283 
1284 	/* this must be fixed to use the current type in alignments */
1285 	switch( new<0?pty->in.type:p->stype ){
1286 
1287 	case ENUMTY:
1288 		{
1289 			int s;
1290 			s = new<0 ? pty->fn.csiz : p->sizoff;
1291 			al = dimtab[s+2];
1292 			sz = dimtab[s];
1293 			break;
1294 			}
1295 
1296 	case CHAR:
1297 	case UCHAR:
1298 		al = ALCHAR;
1299 		sz = SZCHAR;
1300 		break;
1301 
1302 	case SHORT:
1303 	case USHORT:
1304 		al = ALSHORT;
1305 		sz = SZSHORT;
1306 		break;
1307 
1308 	case INT:
1309 	case UNSIGNED:
1310 		al = ALINT;
1311 		sz = SZINT;
1312 		break;
1313 #ifdef LONGFIELDS
1314 
1315 	case LONG:
1316 	case ULONG:
1317 		al = ALLONG;
1318 		sz = SZLONG;
1319 		break;
1320 #endif
1321 
1322 	default:
1323 		if( new < 0 ) {
1324 			uerror( "illegal field type" );
1325 			al = ALINT;
1326 			}
1327 		else {
1328 			al = fldal( p->stype );
1329 			sz =SZINT;
1330 			}
1331 		}
1332 
1333 	if( w > sz ) {
1334 		uerror( "field too big");
1335 		w = sz;
1336 		}
1337 
1338 	if( w == 0 ){ /* align only */
1339 		SETOFF( strucoff, al );
1340 		if( new >= 0 ) uerror( "zero size field");
1341 		return(0);
1342 		}
1343 
1344 	if( strucoff%al + w > sz ) SETOFF( strucoff, al );
1345 	if( new < 0 ) {
1346 		if( (offsz-strucoff) < w )
1347 			cerror("structure too large");
1348 		strucoff += w;  /* we know it will fit */
1349 		return(0);
1350 		}
1351 
1352 	/* establish the field */
1353 
1354 	if( new == 1 ) { /* previous definition */
1355 		if( p->offset != strucoff || p->sclass != (FIELD|w) ) return(1);
1356 		}
1357 	p->offset = strucoff;
1358 	if( (offsz-strucoff) < w ) cerror("structure too large");
1359 	strucoff += w;
1360 	p->stype = type;
1361 	fldty( p );
1362 	return(0);
1363 	}
1364 
1365 nidcl( p ) NODE *p; { /* handle unitialized declarations */
1366 	/* assumed to be not functions */
1367 	register class;
1368 	register commflag;  /* flag for labelled common declarations */
1369 
1370 	commflag = 0;
1371 
1372 	/* compute class */
1373 	if( (class=curclass) == SNULL ){
1374 		if( blevel > 1 ) class = AUTO;
1375 		else if( blevel != 0 || instruct ) cerror( "nidcl error" );
1376 		else { /* blevel = 0 */
1377 			class = noinit();
1378 			if( class == EXTERN ) commflag = 1;
1379 			}
1380 		}
1381 #ifdef LCOMM
1382 	/* hack so stab will come out as LCSYM rather than STSYM */
1383 	if (class == STATIC) {
1384 		extern int stabLCSYM;
1385 		stabLCSYM = 1;
1386 	}
1387 #endif
1388 
1389 	defid( p, class );
1390 
1391 	/* if an array is not initialized, no empty dimension */
1392 	if( class!=EXTERN && ISARY(p->in.type) && dimtab[p->fn.cdim]==0 )
1393 		uerror("null storage definition");
1394 
1395 #ifndef LCOMM
1396 	if( class==EXTDEF || class==STATIC )
1397 #else
1398 	if (class==STATIC) {
1399 		register struct symtab *s = &stab[p->tn.rval];
1400 		extern int stabLCSYM;
1401 		int sz = tsize(s->stype, s->dimoff, s->sizoff)/SZCHAR;
1402 
1403 		stabLCSYM = 0;
1404 		if (sz % sizeof (int))
1405 			sz += sizeof (int) - (sz % sizeof (int));
1406 		if (s->slevel > 1)
1407 			printf("	.lcomm	L%d,%d\n", s->offset, sz);
1408 		else
1409 			printf("	.lcomm	%s,%d\n", exname(s->sname), sz);
1410 	}else if (class == EXTDEF)
1411 #endif
1412 		{
1413 		/* simulate initialization by 0 */
1414 		beginit(p->tn.rval);
1415 		endinit();
1416 		}
1417 	if( commflag ) commdec( p->tn.rval );
1418 	}
1419 
1420 TWORD
1421 types( t1, t2, t3 ) TWORD t1, t2, t3; {
1422 	/* return a basic type from basic types t1, t2, and t3 */
1423 
1424 	TWORD t[3], noun, adj, unsg;
1425 	register i;
1426 
1427 	t[0] = t1;
1428 	t[1] = t2;
1429 	t[2] = t3;
1430 
1431 	unsg = INT;  /* INT or UNSIGNED */
1432 	noun = UNDEF;  /* INT, CHAR, or FLOAT */
1433 	adj = INT;  /* INT, LONG, or SHORT */
1434 
1435 	for( i=0; i<3; ++i ){
1436 		switch( t[i] ){
1437 
1438 		default:
1439 		bad:
1440 			uerror( "illegal type combination" );
1441 			return( INT );
1442 
1443 		case UNDEF:
1444 			continue;
1445 
1446 		case UNSIGNED:
1447 			if( unsg != INT ) goto bad;
1448 			unsg = UNSIGNED;
1449 			continue;
1450 
1451 		case LONG:
1452 		case SHORT:
1453 			if( adj != INT ) goto bad;
1454 			adj = t[i];
1455 			continue;
1456 
1457 		case INT:
1458 		case CHAR:
1459 		case FLOAT:
1460 			if( noun != UNDEF ) goto bad;
1461 			noun = t[i];
1462 			continue;
1463 			}
1464 		}
1465 
1466 	/* now, construct final type */
1467 	if( noun == UNDEF ) noun = INT;
1468 	else if( noun == FLOAT ){
1469 		if( unsg != INT || adj == SHORT ) goto bad;
1470 		return( adj==LONG ? DOUBLE : FLOAT );
1471 		}
1472 	else if( noun == CHAR && adj != INT ) goto bad;
1473 
1474 	/* now, noun is INT or CHAR */
1475 	if( adj != INT ) noun = adj;
1476 	if( unsg == UNSIGNED ) return( noun + (UNSIGNED-INT) );
1477 	else return( noun );
1478 	}
1479 
1480 NODE *
1481 tymerge( typ, idp ) NODE *typ, *idp; {
1482 	/* merge type typ with identifier idp  */
1483 
1484 	register unsigned t;
1485 	register i;
1486 	extern int eprint();
1487 
1488 	if( typ->in.op != TYPE ) cerror( "tymerge: arg 1" );
1489 	if(idp == NIL ) return( NIL );
1490 
1491 # ifndef BUG1
1492 	if( ddebug > 2 ) fwalk( idp, eprint, 0 );
1493 # endif
1494 
1495 	idp->in.type = typ->in.type;
1496 	idp->fn.cdim = curdim;
1497 	tyreduce( idp );
1498 	idp->fn.csiz = typ->fn.csiz;
1499 
1500 	for( t=typ->in.type, i=typ->fn.cdim; t&TMASK; t = DECREF(t) ){
1501 		if( ISARY(t) ) dstash( dimtab[i++] );
1502 		}
1503 
1504 	/* now idp is a single node: fix up type */
1505 
1506 	idp->in.type = ctype( idp->in.type );
1507 
1508 	if( (t = BTYPE(idp->in.type)) != STRTY && t != UNIONTY && t != ENUMTY ){
1509 		idp->fn.csiz = t;  /* in case ctype has rewritten things */
1510 		}
1511 
1512 	return( idp );
1513 	}
1514 
1515 tyreduce( p ) register NODE *p; {
1516 
1517 	/* build a type, and stash away dimensions, from a parse tree of the declaration */
1518 	/* the type is build top down, the dimensions bottom up */
1519 	register o, temp;
1520 	register unsigned t;
1521 
1522 	o = p->in.op;
1523 	p->in.op = FREE;
1524 
1525 	if( o == NAME ) return;
1526 
1527 	t = INCREF( p->in.type );
1528 	if( o == UNARY CALL ) t += (FTN-PTR);
1529 	else if( o == LB ){
1530 		t += (ARY-PTR);
1531 		temp = p->in.right->tn.lval;
1532 		p->in.right->in.op = FREE;
1533 		if( temp == 0 && p->in.left->tn.op == LB )
1534 			uerror( "null dimension" );
1535 		}
1536 
1537 	p->in.left->in.type = t;
1538 	tyreduce( p->in.left );
1539 
1540 	if( o == LB ) dstash( temp );
1541 
1542 	p->tn.rval = p->in.left->tn.rval;
1543 	p->in.type = p->in.left->in.type;
1544 
1545 	}
1546 
1547 fixtype( p, class ) register NODE *p; {
1548 	register unsigned t, type;
1549 	register mod1, mod2;
1550 	/* fix up the types, and check for legality */
1551 
1552 	if( (type = p->in.type) == UNDEF ) return;
1553 	if( mod2 = (type&TMASK) ){
1554 		t = DECREF(type);
1555 		while( mod1=mod2, mod2 = (t&TMASK) ){
1556 			if( mod1 == ARY && mod2 == FTN ){
1557 				uerror( "array of functions is illegal" );
1558 				type = 0;
1559 				}
1560 			else if( mod1 == FTN && ( mod2 == ARY || mod2 == FTN ) ){
1561 				uerror( "function returns illegal type" );
1562 				type = 0;
1563 				}
1564 			t = DECREF(t);
1565 			}
1566 		}
1567 
1568 	/* detect function arguments, watching out for structure declarations */
1569 	/* for example, beware of f(x) struct { int a[10]; } *x; { ... } */
1570 	/* the danger is that "a" will be converted to a pointer */
1571 
1572 	if( class==SNULL && blevel==1 && !(instruct&(INSTRUCT|INUNION)) )
1573 		class = PARAM;
1574 	if( class == PARAM || ( class==REGISTER && blevel==1 ) ){
1575 		if( type == FLOAT ) type = DOUBLE;
1576 		else if( ISARY(type) ){
1577 #ifdef LINT
1578 			if( hflag && dimtab[p->fn.cdim]!=0 )
1579 				werror("array[%d] type changed to pointer",
1580 					dimtab[p->fn.cdim]);
1581 #endif
1582 			++p->fn.cdim;
1583 			type += (PTR-ARY);
1584 			}
1585 		else if( ISFTN(type) ){
1586 			werror( "a function is declared as an argument" );
1587 			type = INCREF(type);
1588 			}
1589 
1590 		}
1591 
1592 	if( instruct && ISFTN(type) ){
1593 		uerror( "function illegal in structure or union" );
1594 		type = INCREF(type);
1595 		}
1596 	p->in.type = type;
1597 	}
1598 
1599 uclass( class ) register class; {
1600 	/* give undefined version of class */
1601 	if( class == SNULL ) return( EXTERN );
1602 	else if( class == STATIC ) return( USTATIC );
1603 	else if( class == FORTRAN ) return( UFORTRAN );
1604 	else return( class );
1605 	}
1606 
1607 fixclass( class, type ) TWORD type; {
1608 
1609 	/* first, fix null class */
1610 
1611 	if( class == SNULL ){
1612 		if( instruct&INSTRUCT ) class = MOS;
1613 		else if( instruct&INUNION ) class = MOU;
1614 		else if( blevel == 0 ) class = EXTDEF;
1615 		else if( blevel == 1 ) class = PARAM;
1616 		else class = AUTO;
1617 
1618 		}
1619 
1620 	/* now, do general checking */
1621 
1622 	if( ISFTN( type ) ){
1623 		switch( class ) {
1624 		default:
1625 			uerror( "function has illegal storage class" );
1626 		case AUTO:
1627 			class = EXTERN;
1628 		case EXTERN:
1629 		case EXTDEF:
1630 		case FORTRAN:
1631 		case TYPEDEF:
1632 		case STATIC:
1633 		case UFORTRAN:
1634 		case USTATIC:
1635 			;
1636 			}
1637 		}
1638 
1639 	if( class&FIELD ){
1640 		if( !(instruct&INSTRUCT) ) uerror( "illegal use of field" );
1641 		return( class );
1642 		}
1643 
1644 	switch( class ){
1645 
1646 	case MOU:
1647 		if( !(instruct&INUNION) ) uerror( "illegal class" );
1648 		return( class );
1649 
1650 	case MOS:
1651 		if( !(instruct&INSTRUCT) ) uerror( "illegal class" );
1652 		return( class );
1653 
1654 	case MOE:
1655 		if( instruct & (INSTRUCT|INUNION) ) uerror( "illegal class" );
1656 		return( class );
1657 
1658 	case REGISTER:
1659 		if( blevel == 0 ) uerror( "illegal register declaration" );
1660 		else if( regvar >= MINRVAR && cisreg( type ) ) return( class );
1661 		if( blevel == 1 ) return( PARAM );
1662 		else return( AUTO );
1663 
1664 	case AUTO:
1665 	case LABEL:
1666 	case ULABEL:
1667 		if( blevel < 2 ) uerror( "illegal class" );
1668 		return( class );
1669 
1670 	case PARAM:
1671 		if( blevel != 1 ) uerror( "illegal class" );
1672 		return( class );
1673 
1674 	case UFORTRAN:
1675 	case FORTRAN:
1676 # ifdef NOFORTRAN
1677 			NOFORTRAN;    /* a condition which can regulate the FORTRAN usage */
1678 # endif
1679 		if( !ISFTN(type) ) uerror( "fortran declaration must apply to function" );
1680 		else {
1681 			type = DECREF(type);
1682 			if( ISFTN(type) || ISARY(type) || ISPTR(type) ) {
1683 				uerror( "fortran function has wrong type" );
1684 				}
1685 			}
1686 	case EXTERN:
1687 	case STATIC:
1688 	case EXTDEF:
1689 	case TYPEDEF:
1690 	case USTATIC:
1691 		if( blevel == 1 ){
1692 			uerror( "illegal class" );
1693 			return( PARAM );
1694 			}
1695 	case STNAME:
1696 	case UNAME:
1697 	case ENAME:
1698 		return( class );
1699 
1700 	default:
1701 		cerror( "illegal class: %d", class );
1702 		/* NOTREACHED */
1703 
1704 		}
1705 	}
1706 
1707 struct symtab *
1708 mknonuniq(idindex) int *idindex; {/* locate a symbol table entry for */
1709 	/* an occurrence of a nonunique structure member name */
1710 	/* or field */
1711 	register i;
1712 	register struct symtab * sp;
1713 	char *q;
1714 
1715 	sp = & stab[ i= *idindex ]; /* position search at old entry */
1716 	while( sp->stype != TNULL ){ /* locate unused entry */
1717 		if( ++i >= SYMTSZ ){/* wrap around symbol table */
1718 			i = 0;
1719 			sp = stab;
1720 			}
1721 		else ++sp;
1722 		if( i == *idindex ) cerror("Symbol table full");
1723 		}
1724 	sp->sflags = SNONUNIQ | SMOS;
1725 	q = stab[*idindex].sname; /* old entry name */
1726 #ifdef FLEXNAMES
1727 	sp->sname = stab[*idindex].sname;
1728 #endif
1729 # ifndef BUG1
1730 	if( ddebug ){
1731 		printf("\tnonunique entry for %s from %d to %d\n",
1732 			q, *idindex, i );
1733 		}
1734 # endif
1735 	*idindex = i;
1736 #ifndef FLEXNAMES
1737 	{
1738 		char *p = sp->sname;
1739 		for( i=1; i<=NCHNAM; ++i ) /* copy name */
1740 			if( *p++ = *q /* assign */ ) ++q;
1741 		}
1742 #endif
1743 	return ( sp );
1744 	}
1745 
1746 lookup( name, s) char *name; {
1747 	/* look up name: must agree with s w.r.t. STAG, SMOS and SHIDDEN */
1748 
1749 	register char *p, *q;
1750 	int i, ii;
1751 #ifndef FLEXNAMES
1752 	int j;
1753 #endif
1754 	register struct symtab *sp;
1755 
1756 	/* compute initial hash index */
1757 # ifndef BUG1
1758 	if( ddebug > 2 ){
1759 		printf( "lookup( %s, %d ), stwart=%d, instruct=%d\n", name, s, stwart, instruct );
1760 		}
1761 # endif
1762 
1763 	i = 0;
1764 #ifndef FLEXNAMES
1765 	for( p=name, j=0; *p != '\0'; ++p ){
1766 		i += *p;
1767 		if( ++j >= NCHNAM ) break;
1768 		}
1769 #else
1770 	i = (int)name;
1771 #endif
1772 	i = i%SYMTSZ;
1773 	sp = &stab[ii=i];
1774 
1775 	for(;;){ /* look for name */
1776 
1777 		if( sp->stype == TNULL ){ /* empty slot */
1778 			sp->sflags = s;  /* set STAG, SMOS if needed, turn off all others */
1779 #ifndef FLEXNAMES
1780 			p = sp->sname;
1781 			for( j=0; j<NCHNAM; ++j ) if( *p++ = *name ) ++name;
1782 #else
1783 			sp->sname = name;
1784 #endif
1785 			sp->stype = UNDEF;
1786 			sp->sclass = SNULL;
1787 			return( i );
1788 			}
1789 		if( (sp->sflags & (STAG|SMOS|SHIDDEN)) != s ) goto next;
1790 		p = sp->sname;
1791 		q = name;
1792 #ifndef FLEXNAMES
1793 		for( j=0; j<NCHNAM;++j ){
1794 			if( *p++ != *q ) goto next;
1795 			if( !*q++ ) break;
1796 			}
1797 		return( i );
1798 #else
1799 		if (p == q)
1800 			return ( i );
1801 #endif
1802 	next:
1803 		if( ++i >= SYMTSZ ){
1804 			i = 0;
1805 			sp = stab;
1806 			}
1807 		else ++sp;
1808 		if( i == ii ) cerror( "symbol table full" );
1809 		}
1810 	}
1811 
1812 #ifndef checkst
1813 /* if not debugging, make checkst a macro */
1814 checkst(lev){
1815 	register int s, i, j;
1816 	register struct symtab *p, *q;
1817 
1818 	for( i=0, p=stab; i<SYMTSZ; ++i, ++p ){
1819 		if( p->stype == TNULL ) continue;
1820 		j = lookup( p->sname, p->sflags&(SMOS|STAG) );
1821 		if( j != i ){
1822 			q = &stab[j];
1823 			if( q->stype == UNDEF ||
1824 			    q->slevel <= p->slevel ){
1825 #ifndef FLEXNAMES
1826 				cerror( "check error: %.8s", q->sname );
1827 #else
1828 				cerror( "check error: %s", q->sname );
1829 #endif
1830 				}
1831 			}
1832 #ifndef FLEXNAMES
1833 		else if( p->slevel > lev ) cerror( "%.8s check at level %d", p->sname, lev );
1834 #else
1835 		else if( p->slevel > lev ) cerror( "%s check at level %d", p->sname, lev );
1836 #endif
1837 		}
1838 	}
1839 #endif
1840 
1841 struct symtab *
1842 relook(p) register struct symtab *p; {  /* look up p again, and see where it lies */
1843 
1844 	register struct symtab *q;
1845 
1846 	/* I'm not sure that this handles towers of several hidden definitions in all cases */
1847 	q = &stab[lookup( p->sname, p->sflags&(STAG|SMOS|SHIDDEN) )];
1848 	/* make relook always point to either p or an empty cell */
1849 	if( q->stype == UNDEF ){
1850 		q->stype = TNULL;
1851 		return(q);
1852 		}
1853 	while( q != p ){
1854 		if( q->stype == TNULL ) break;
1855 		if( ++q >= &stab[SYMTSZ] ) q=stab;
1856 		}
1857 	return(q);
1858 	}
1859 
1860 clearst( lev ) register int lev; {
1861 	register struct symtab *p, *q;
1862 	register int temp;
1863 	struct symtab *clist = 0;
1864 
1865 	temp = lineno;
1866 	aobeg();
1867 
1868 	/* step 1: remove entries */
1869 	while( chaintop-1 > lev ){
1870 		p = schain[--chaintop];
1871 		schain[chaintop] = 0;
1872 		for( ; p; p = q ){
1873 			q = p->snext;
1874 			if( p->stype == TNULL || p->slevel <= lev )
1875 				cerror( "schain botch" );
1876 			lineno = p->suse < 0 ? -p->suse : p->suse;
1877 			if( p->stype==UNDEF || ( p->sclass==ULABEL && lev<2 ) ){
1878 				lineno = temp;
1879 #ifndef FLEXNAMES
1880 				uerror( "%.8s undefined", p->sname );
1881 #else
1882 				uerror( "%s undefined", p->sname );
1883 #endif
1884 				}
1885 			else aocode(p);
1886 # ifndef BUG1
1887 			if( ddebug ){
1888 #ifndef FLEXNAMES
1889 				printf( "removing %.8s", p->sname );
1890 #else
1891 				printf( "removing %s", p->sname );
1892 #endif
1893 				printf( " from stab[%d], flags %o level %d\n",
1894 					p-stab, p->sflags, p->slevel);
1895 				}
1896 # endif
1897 			if( p->sflags & SHIDES )unhide( p );
1898 			p->stype = TNULL;
1899 			p->snext = clist;
1900 			clist = p;
1901 			}
1902 		}
1903 
1904 	/* step 2: fix any mishashed entries */
1905 	p = clist;
1906 	while( p ){
1907 		register struct symtab *r, *next;
1908 
1909 		q = p;
1910 		next = p->snext;
1911 		for(;;){
1912 			if( ++q >= &stab[SYMTSZ] )q = stab;
1913 			if( q == p || q->stype == TNULL )break;
1914 			if( (r = relook(q)) != q ) {
1915 				*r = *q;
1916 				q->stype = TNULL;
1917 				}
1918 			}
1919 		p = next;
1920 		}
1921 
1922 	lineno = temp;
1923 	aoend();
1924 	}
1925 
1926 hide( p ) register struct symtab *p; {
1927 	register struct symtab *q;
1928 	for( q=p+1; ; ++q ){
1929 		if( q >= &stab[SYMTSZ] ) q = stab;
1930 		if( q == p ) cerror( "symbol table full" );
1931 		if( q->stype == TNULL ) break;
1932 		}
1933 	*q = *p;
1934 	p->sflags |= SHIDDEN;
1935 	q->sflags = (p->sflags&(SMOS|STAG)) | SHIDES;
1936 #ifndef FLEXNAMES
1937 	if( hflag ) werror( "%.8s redefinition hides earlier one", p->sname );
1938 #else
1939 	if( hflag ) werror( "%s redefinition hides earlier one", p->sname );
1940 #endif
1941 # ifndef BUG1
1942 	if( ddebug ) printf( "	%d hidden in %d\n", p-stab, q-stab );
1943 # endif
1944 	return( idname = q-stab );
1945 	}
1946 
1947 unhide( p ) register struct symtab *p; {
1948 	register struct symtab *q;
1949 	register s;
1950 
1951 	s = p->sflags & (SMOS|STAG);
1952 	q = p;
1953 
1954 	for(;;){
1955 
1956 		if( q == stab ) q = &stab[SYMTSZ-1];
1957 		else --q;
1958 
1959 		if( q == p ) break;
1960 
1961 		if( (q->sflags&(SMOS|STAG)) == s ){
1962 #ifndef FLEXNAMES
1963 			register j;
1964 			for( j =0; j<NCHNAM; ++j ) if( p->sname[j] != q->sname[j] ) break;
1965 			if( j == NCHNAM ){ /* found the name */
1966 #else
1967 			if (p->sname == q->sname) {
1968 #endif
1969 				q->sflags &= ~SHIDDEN;
1970 # ifndef BUG1
1971 				if( ddebug ) printf( "unhide uncovered %d from %d\n", q-stab,p-stab);
1972 # endif
1973 				return;
1974 				}
1975 			}
1976 
1977 		}
1978 	cerror( "unhide fails" );
1979 	}
1980