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