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