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