117746Sralph #ifndef lint 2*32820Sdonn static char *sccsid ="@(#)pftn.c 1.22 (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: 75432819Sdonn /* cerror( "compiler takes size of function"); */ 75532819Sdonn uerror( "can't take size of function" ); 75632819Sdonn return( SZCHAR ); 75713939Slinton case PTR: 75813939Slinton return( SZPOINT * mult ); 75913939Slinton case ARY: 76013939Slinton mult *= (unsigned int) dimtab[ d++ ]; 76113939Slinton continue; 76213939Slinton case 0: 76313939Slinton break; 76413939Slinton 76513939Slinton } 76613939Slinton } 76713939Slinton 76813939Slinton if( dimtab[s]==0 ) { 76925749Sdonn if( ty == STRTY ) 77025749Sdonn uerror( "undefined structure" ); 77125749Sdonn else 77225749Sdonn uerror( "unknown size"); 77313939Slinton return( SZINT ); 77413939Slinton } 77513939Slinton return( (unsigned int) dimtab[ s ] * mult ); 77613939Slinton } 77713939Slinton 77813939Slinton inforce( n ) OFFSZ n; { /* force inoff to have the value n */ 77913939Slinton /* inoff is updated to have the value n */ 78013939Slinton OFFSZ wb; 78113939Slinton register rest; 78213939Slinton /* rest is used to do a lot of conversion to ints... */ 78313939Slinton 78413939Slinton if( inoff == n ) return; 78513939Slinton if( inoff > n ) { 78613939Slinton cerror( "initialization alignment error"); 78713939Slinton } 78813939Slinton 78913939Slinton wb = inoff; 79013939Slinton SETOFF( wb, SZINT ); 79113939Slinton 79213939Slinton /* wb now has the next higher word boundary */ 79313939Slinton 79413939Slinton if( wb >= n ){ /* in the same word */ 79513939Slinton rest = n - inoff; 79613939Slinton vfdzero( rest ); 79713939Slinton return; 79813939Slinton } 79913939Slinton 80013939Slinton /* otherwise, extend inoff to be word aligned */ 80113939Slinton 80213939Slinton rest = wb - inoff; 80313939Slinton vfdzero( rest ); 80413939Slinton 80513939Slinton /* now, skip full words until near to n */ 80613939Slinton 80713939Slinton rest = (n-inoff)/SZINT; 80813939Slinton zecode( rest ); 80913939Slinton 81013939Slinton /* now, the remainder of the last word */ 81113939Slinton 81213939Slinton rest = n-inoff; 81313939Slinton vfdzero( rest ); 81413939Slinton if( inoff != n ) cerror( "inoff error"); 81513939Slinton 81613939Slinton } 81713939Slinton 81813939Slinton vfdalign( n ){ /* make inoff have the offset the next alignment of n */ 81913939Slinton OFFSZ m; 82013939Slinton 82113939Slinton m = inoff; 82213939Slinton SETOFF( m, n ); 82313939Slinton inforce( m ); 82413939Slinton } 82513939Slinton 82613939Slinton 82713939Slinton int idebug = 0; 82813939Slinton 82913939Slinton int ibseen = 0; /* the number of } constructions which have been filled */ 83013939Slinton 83127247Sdonn int ifull = 0; /* 1 if all initializers have been seen */ 83227247Sdonn 83313939Slinton int iclass; /* storage class of thing being initialized */ 83413939Slinton 83513939Slinton int ilocctr = 0; /* location counter for current initialization */ 83613939Slinton 83713939Slinton beginit(curid){ 83813939Slinton /* beginning of initilization; set location ctr and set type */ 83913939Slinton register struct symtab *p; 84013939Slinton 84113939Slinton # ifndef BUG1 84213939Slinton if( idebug >= 3 ) printf( "beginit(), curid = %d\n", curid ); 84313939Slinton # endif 84413939Slinton 84513939Slinton p = &stab[curid]; 84613939Slinton 84713939Slinton iclass = p->sclass; 84813939Slinton if( curclass == EXTERN || curclass == FORTRAN ) iclass = EXTERN; 84913939Slinton switch( iclass ){ 85013939Slinton 85113939Slinton case UNAME: 85213939Slinton case EXTERN: 85313939Slinton return; 85413939Slinton case AUTO: 85513939Slinton case REGISTER: 85613939Slinton break; 85713939Slinton case EXTDEF: 85813939Slinton case STATIC: 85913939Slinton ilocctr = ISARY(p->stype)?ADATA:DATA; 86032814Sdonn if( nerrors == 0 ){ 86132817Sdonn (void) locctr( ilocctr ); 86232814Sdonn defalign( talign( p->stype, p->sizoff ) ); 86332814Sdonn defnam( p ); 86432814Sdonn } 86513939Slinton 86613939Slinton } 86713939Slinton 86813939Slinton inoff = 0; 86913939Slinton ibseen = 0; 87027247Sdonn ifull = 0; 87113939Slinton 87213939Slinton pstk = 0; 87313939Slinton 87413939Slinton instk( curid, p->stype, p->dimoff, p->sizoff, inoff ); 87513939Slinton 87613939Slinton } 87713939Slinton 87813939Slinton instk( id, t, d, s, off ) OFFSZ off; TWORD t; { 87913939Slinton /* make a new entry on the parameter stack to initialize id */ 88013939Slinton 88113939Slinton register struct symtab *p; 88213939Slinton 88313939Slinton for(;;){ 88413939Slinton # ifndef BUG1 88513939Slinton if( idebug ) printf( "instk((%d, %o,%d,%d, %d)\n", id, t, d, s, off ); 88613939Slinton # endif 88713939Slinton 88813939Slinton /* save information on the stack */ 88913939Slinton 89013939Slinton if( !pstk ) pstk = instack; 89113939Slinton else ++pstk; 89213939Slinton 89313939Slinton pstk->in_fl = 0; /* { flag */ 89413939Slinton pstk->in_id = id ; 89513939Slinton pstk->in_t = t ; 89613939Slinton pstk->in_d = d ; 89713939Slinton pstk->in_s = s ; 89813939Slinton pstk->in_n = 0; /* number seen */ 89913939Slinton pstk->in_x = t==STRTY ?dimtab[s+1] : 0 ; 90013939Slinton pstk->in_off = off; /* offset at the beginning of this element */ 90113939Slinton /* if t is an array, DECREF(t) can't be a field */ 90213939Slinton /* INS_sz has size of array elements, and -size for fields */ 90313939Slinton if( ISARY(t) ){ 90413939Slinton pstk->in_sz = tsize( DECREF(t), d+1, s ); 90513939Slinton } 90613939Slinton else if( stab[id].sclass & FIELD ){ 90713939Slinton pstk->in_sz = - ( stab[id].sclass & FLDSIZ ); 90813939Slinton } 90913939Slinton else { 91013939Slinton pstk->in_sz = 0; 91113939Slinton } 91213939Slinton 91313939Slinton if( (iclass==AUTO || iclass == REGISTER ) && 91413939Slinton (ISARY(t) || t==STRTY) ) uerror( "no automatic aggregate initialization" ); 91513939Slinton 91613939Slinton /* now, if this is not a scalar, put on another element */ 91713939Slinton 91813939Slinton if( ISARY(t) ){ 91913939Slinton t = DECREF(t); 92013939Slinton ++d; 92113939Slinton continue; 92213939Slinton } 92313939Slinton else if( t == STRTY ){ 92425749Sdonn if( dimtab[pstk->in_s] == 0 ){ 92525749Sdonn uerror( "can't initialize undefined structure" ); 92625749Sdonn iclass = -1; 92725749Sdonn return; 92825749Sdonn } 92913939Slinton id = dimtab[pstk->in_x]; 93013939Slinton p = &stab[id]; 93113939Slinton if( p->sclass != MOS && !(p->sclass&FIELD) ) cerror( "insane structure member list" ); 93213939Slinton t = p->stype; 93313939Slinton d = p->dimoff; 93413939Slinton s = p->sizoff; 93513939Slinton off += p->offset; 93613939Slinton continue; 93713939Slinton } 93813939Slinton else return; 93913939Slinton } 94013939Slinton } 94113939Slinton 94213939Slinton NODE * 94313939Slinton getstr(){ /* decide if the string is external or an initializer, and get the contents accordingly */ 94413939Slinton 94513939Slinton register l, temp; 94613939Slinton register NODE *p; 94713939Slinton 94813939Slinton if( (iclass==EXTDEF||iclass==STATIC) && (pstk->in_t == CHAR || pstk->in_t == UCHAR) && 94913939Slinton pstk!=instack && ISARY( pstk[-1].in_t ) ){ 95013939Slinton /* treat "abc" as { 'a', 'b', 'c', 0 } */ 95113939Slinton strflg = 1; 95213939Slinton ilbrace(); /* simulate { */ 95313939Slinton inforce( pstk->in_off ); 95413939Slinton /* if the array is inflexible (not top level), pass in the size and 95513939Slinton be prepared to throw away unwanted initializers */ 95613939Slinton lxstr((pstk-1)!=instack?dimtab[(pstk-1)->in_d]:0); /* get the contents */ 95713939Slinton irbrace(); /* simulate } */ 95813939Slinton return( NIL ); 95913939Slinton } 96013939Slinton else { /* make a label, and get the contents and stash them away */ 96113939Slinton if( iclass != SNULL ){ /* initializing */ 96213939Slinton /* fill out previous word, to permit pointer */ 96313939Slinton vfdalign( ALPOINT ); 96413939Slinton } 96513939Slinton temp = locctr( blevel==0?ISTRNG:STRNG ); /* set up location counter */ 96613939Slinton deflab( l = getlab() ); 96713939Slinton strflg = 0; 96813939Slinton lxstr(0); /* get the contents */ 96932817Sdonn (void) locctr( blevel==0?ilocctr:temp ); 97013939Slinton p = buildtree( STRING, NIL, NIL ); 97113939Slinton p->tn.rval = -l; 97213939Slinton return(p); 97313939Slinton } 97413939Slinton } 97513939Slinton 97613939Slinton putbyte( v ){ /* simulate byte v appearing in a list of integer values */ 97713939Slinton register NODE *p; 97813939Slinton p = bcon(v); 97913939Slinton incode( p, SZCHAR ); 98013939Slinton tfree( p ); 98113939Slinton gotscal(); 98213939Slinton } 98313939Slinton 98413939Slinton endinit(){ 98513939Slinton register TWORD t; 98613939Slinton register d, s, n, d1; 98713939Slinton 98813939Slinton # ifndef BUG1 98913939Slinton if( idebug ) printf( "endinit(), inoff = %d\n", inoff ); 99013939Slinton # endif 99113939Slinton 99213939Slinton switch( iclass ){ 99313939Slinton 99413939Slinton case EXTERN: 99513939Slinton case AUTO: 99613939Slinton case REGISTER: 99725749Sdonn case -1: 99813939Slinton return; 99913939Slinton } 100013939Slinton 100113939Slinton pstk = instack; 100213939Slinton 100313939Slinton t = pstk->in_t; 100413939Slinton d = pstk->in_d; 100513939Slinton s = pstk->in_s; 100613939Slinton n = pstk->in_n; 100713939Slinton 100813939Slinton if( ISARY(t) ){ 100913939Slinton d1 = dimtab[d]; 101013939Slinton 101113939Slinton vfdalign( pstk->in_sz ); /* fill out part of the last element, if needed */ 101213939Slinton n = inoff/pstk->in_sz; /* real number of initializers */ 101313939Slinton if( d1 >= n ){ 101413939Slinton /* once again, t is an array, so no fields */ 101513939Slinton inforce( tsize( t, d, s ) ); 101613939Slinton n = d1; 101713939Slinton } 101813939Slinton if( d1!=0 && d1!=n ) uerror( "too many initializers"); 101913939Slinton if( n==0 ) werror( "empty array declaration"); 102013939Slinton dimtab[d] = n; 102113942Slinton if( d1==0 ) FIXDEF(&stab[pstk->in_id]); 102213939Slinton } 102313939Slinton 102413939Slinton else if( t == STRTY || t == UNIONTY ){ 102513939Slinton /* clearly not fields either */ 102613939Slinton inforce( tsize( t, d, s ) ); 102713939Slinton } 102813939Slinton else if( n > 1 ) uerror( "bad scalar initialization"); 102913939Slinton /* this will never be called with a field element... */ 103013939Slinton else inforce( tsize(t,d,s) ); 103113939Slinton 103213939Slinton paramno = 0; 103313939Slinton vfdalign( AL_INIT ); 103413939Slinton inoff = 0; 103513939Slinton iclass = SNULL; 103613939Slinton 103713939Slinton } 103813939Slinton 103932818Sdonn fixinit(){ 104032818Sdonn /* called from the grammar if we must punt during initialization */ 104132818Sdonn /* stolen from endinit() */ 104232818Sdonn pstk = instack; 104332818Sdonn paramno = 0; 104432818Sdonn vfdalign( AL_INIT ); 104532818Sdonn inoff = 0; 104632818Sdonn iclass = SNULL; 104732818Sdonn } 104832818Sdonn 104913939Slinton doinit( p ) register NODE *p; { 105013939Slinton 105113939Slinton /* take care of generating a value for the initializer p */ 105213939Slinton /* inoff has the current offset (last bit written) 105313939Slinton in the current word being generated */ 105413939Slinton 105513939Slinton register sz, d, s; 105613939Slinton register TWORD t; 105717746Sralph int o; 105813939Slinton 105913939Slinton /* note: size of an individual initializer is assumed to fit into an int */ 106013939Slinton 106132814Sdonn if( iclass < 0 ) goto leave; 106213939Slinton if( iclass == EXTERN || iclass == UNAME ){ 106313939Slinton uerror( "cannot initialize extern or union" ); 106413939Slinton iclass = -1; 106513939Slinton goto leave; 106613939Slinton } 106713939Slinton 106813939Slinton if( iclass == AUTO || iclass == REGISTER ){ 106913939Slinton /* do the initialization and get out, without regard 107013939Slinton for filing out the variable with zeros, etc. */ 107113939Slinton bccode(); 107213939Slinton idname = pstk->in_id; 107313939Slinton p = buildtree( ASSIGN, buildtree( NAME, NIL, NIL ), p ); 107413939Slinton ecomp(p); 107513939Slinton return; 107613939Slinton } 107713939Slinton 107813939Slinton if( p == NIL ) return; /* for throwing away strings that have been turned into lists */ 107913939Slinton 108027247Sdonn if( ifull ){ 108127247Sdonn uerror( "too many initializers" ); 108227247Sdonn iclass = -1; 108327247Sdonn goto leave; 108427247Sdonn } 108513939Slinton if( ibseen ){ 108613939Slinton uerror( "} expected"); 108713939Slinton goto leave; 108813939Slinton } 108913939Slinton 109013939Slinton # ifndef BUG1 109113939Slinton if( idebug > 1 ) printf( "doinit(%o)\n", p ); 109213939Slinton # endif 109313939Slinton 109413939Slinton t = pstk->in_t; /* type required */ 109513939Slinton d = pstk->in_d; 109613939Slinton s = pstk->in_s; 109713939Slinton if( pstk->in_sz < 0 ){ /* bit field */ 109813939Slinton sz = -pstk->in_sz; 109913939Slinton } 110013939Slinton else { 110113939Slinton sz = tsize( t, d, s ); 110213939Slinton } 110313939Slinton 110413939Slinton inforce( pstk->in_off ); 110513939Slinton 110613939Slinton p = buildtree( ASSIGN, block( NAME, NIL,NIL, t, d, s ), p ); 110713939Slinton p->in.left->in.op = FREE; 110813939Slinton p->in.left = p->in.right; 110913939Slinton p->in.right = NIL; 111013939Slinton p->in.left = optim( p->in.left ); 111117746Sralph o = p->in.left->in.op; 111217746Sralph if( o == UNARY AND ){ 111317746Sralph o = p->in.left->in.op = FREE; 111413939Slinton p->in.left = p->in.left->in.left; 111513939Slinton } 111613939Slinton p->in.op = INIT; 111713939Slinton 111813939Slinton if( sz < SZINT ){ /* special case: bit fields, etc. */ 111932815Sdonn if( o != ICON || p->in.left->tn.rval != NONAME ) 112032815Sdonn uerror( "illegal initialization" ); 112113939Slinton else incode( p->in.left, sz ); 112213939Slinton } 112317746Sralph else if( o == FCON ){ 112417746Sralph fincode( p->in.left->fpn.fval, sz ); 112513939Slinton } 112617746Sralph else if( o == DCON ){ 112717746Sralph fincode( p->in.left->dpn.dval, sz ); 112817746Sralph } 112913939Slinton else { 113016178Sralph p = optim(p); 113116178Sralph if( p->in.left->in.op != ICON ) uerror( "illegal initialization" ); 113216178Sralph else cinit( p, sz ); 113313939Slinton } 113413939Slinton 113513939Slinton gotscal(); 113613939Slinton 113713939Slinton leave: 113813939Slinton tfree(p); 113913939Slinton } 114013939Slinton 114113939Slinton gotscal(){ 114213939Slinton register t, ix; 114313939Slinton register n, id; 114413939Slinton struct symtab *p; 114513939Slinton OFFSZ temp; 114613939Slinton 114713939Slinton for( ; pstk > instack; ) { 114813939Slinton 114913939Slinton if( pstk->in_fl ) ++ibseen; 115013939Slinton 115113939Slinton --pstk; 115213939Slinton 115313939Slinton t = pstk->in_t; 115413939Slinton 115513939Slinton if( t == STRTY ){ 115613939Slinton ix = ++pstk->in_x; 115713939Slinton if( (id=dimtab[ix]) < 0 ) continue; 115813939Slinton 115913939Slinton /* otherwise, put next element on the stack */ 116013939Slinton 116113939Slinton p = &stab[id]; 116213939Slinton instk( id, p->stype, p->dimoff, p->sizoff, p->offset+pstk->in_off ); 116313939Slinton return; 116413939Slinton } 116513939Slinton else if( ISARY(t) ){ 116613939Slinton n = ++pstk->in_n; 116713939Slinton if( n >= dimtab[pstk->in_d] && pstk > instack ) continue; 116813939Slinton 116913939Slinton /* put the new element onto the stack */ 117013939Slinton 117113939Slinton temp = pstk->in_sz; 117213939Slinton instk( pstk->in_id, (TWORD)DECREF(pstk->in_t), pstk->in_d+1, pstk->in_s, 117313939Slinton pstk->in_off+n*temp ); 117413939Slinton return; 117513939Slinton } 117613939Slinton 117713939Slinton } 117827247Sdonn ifull = 1; 117913939Slinton } 118013939Slinton 118113939Slinton ilbrace(){ /* process an initializer's left brace */ 118213939Slinton register t; 118313939Slinton struct instk *temp; 118413939Slinton 118513939Slinton temp = pstk; 118613939Slinton 118713939Slinton for( ; pstk > instack; --pstk ){ 118813939Slinton 118913939Slinton t = pstk->in_t; 119013939Slinton if( t != STRTY && !ISARY(t) ) continue; /* not an aggregate */ 119113939Slinton if( pstk->in_fl ){ /* already associated with a { */ 119213939Slinton if( pstk->in_n ) uerror( "illegal {"); 119313939Slinton continue; 119413939Slinton } 119513939Slinton 119613939Slinton /* we have one ... */ 119713939Slinton pstk->in_fl = 1; 119813939Slinton break; 119913939Slinton } 120013939Slinton 120113939Slinton /* cannot find one */ 120213939Slinton /* ignore such right braces */ 120313939Slinton 120413939Slinton pstk = temp; 120513939Slinton } 120613939Slinton 120713939Slinton irbrace(){ 120813939Slinton /* called when a '}' is seen */ 120913939Slinton 121013939Slinton # ifndef BUG1 121113939Slinton if( idebug ) printf( "irbrace(): paramno = %d on entry\n", paramno ); 121213939Slinton # endif 121313939Slinton 121413939Slinton if( ibseen ) { 121513939Slinton --ibseen; 121613939Slinton return; 121713939Slinton } 121813939Slinton 121913939Slinton for( ; pstk > instack; --pstk ){ 122013939Slinton if( !pstk->in_fl ) continue; 122113939Slinton 122213939Slinton /* we have one now */ 122313939Slinton 122413939Slinton pstk->in_fl = 0; /* cancel { */ 122513939Slinton gotscal(); /* take it away... */ 122613939Slinton return; 122713939Slinton } 122813939Slinton 122913939Slinton /* these right braces match ignored left braces: throw out */ 123027247Sdonn ifull = 1; 123113939Slinton 123213939Slinton } 123313939Slinton 123413939Slinton upoff( size, alignment, poff ) register alignment, *poff; { 123513939Slinton /* update the offset pointed to by poff; return the 123613939Slinton /* offset of a value of size `size', alignment `alignment', 123713939Slinton /* given that off is increasing */ 123813939Slinton 123913939Slinton register off; 124013939Slinton 124113939Slinton off = *poff; 124213939Slinton SETOFF( off, alignment ); 124313939Slinton if( (offsz-off) < size ){ 124413939Slinton if( instruct!=INSTRUCT )cerror("too many local variables"); 124513939Slinton else cerror("Structure too large"); 124613939Slinton } 124713939Slinton *poff = off+size; 124813939Slinton return( off ); 124913939Slinton } 125013939Slinton 125113939Slinton oalloc( p, poff ) register struct symtab *p; register *poff; { 125213939Slinton /* allocate p with offset *poff, and update *poff */ 125313939Slinton register al, off, tsz; 125413939Slinton int noff; 125513939Slinton 125613939Slinton al = talign( p->stype, p->sizoff ); 125713939Slinton noff = off = *poff; 125813939Slinton tsz = tsize( p->stype, p->dimoff, p->sizoff ); 125913939Slinton #ifdef BACKAUTO 126013939Slinton if( p->sclass == AUTO ){ 126113939Slinton if( (offsz-off) < tsz ) cerror("too many local variables"); 126213939Slinton noff = off + tsz; 126313939Slinton SETOFF( noff, al ); 126413939Slinton off = -noff; 126513939Slinton } 126613939Slinton else 126713939Slinton #endif 126813939Slinton if( p->sclass == PARAM && ( tsz < SZINT ) ){ 126913939Slinton off = upoff( SZINT, ALINT, &noff ); 127013939Slinton # ifndef RTOLBYTES 127113939Slinton off = noff - tsz; 127213939Slinton #endif 127313939Slinton } 127413939Slinton else 127513939Slinton { 127613939Slinton off = upoff( tsz, al, &noff ); 127713939Slinton } 127813939Slinton 127913939Slinton if( p->sclass != REGISTER ){ /* in case we are allocating stack space for register arguments */ 128013939Slinton if( p->offset == NOOFFSET ) p->offset = off; 128113939Slinton else if( off != p->offset ) return(1); 128213939Slinton } 128313939Slinton 128413939Slinton *poff = noff; 128513939Slinton return(0); 128613939Slinton } 128713939Slinton 128813939Slinton falloc( p, w, new, pty ) register struct symtab *p; NODE *pty; { 128913939Slinton /* allocate a field of width w */ 129013939Slinton /* new is 0 if new entry, 1 if redefinition, -1 if alignment */ 129113939Slinton 129213939Slinton register al,sz,type; 129313939Slinton 129413939Slinton type = (new<0)? pty->in.type : p->stype; 129513939Slinton 129613939Slinton /* this must be fixed to use the current type in alignments */ 129713939Slinton switch( new<0?pty->in.type:p->stype ){ 129813939Slinton 129913939Slinton case ENUMTY: 130013939Slinton { 130113939Slinton int s; 130213939Slinton s = new<0 ? pty->fn.csiz : p->sizoff; 130313939Slinton al = dimtab[s+2]; 130413939Slinton sz = dimtab[s]; 130513939Slinton break; 130613939Slinton } 130713939Slinton 130813939Slinton case CHAR: 130913939Slinton case UCHAR: 131013939Slinton al = ALCHAR; 131113939Slinton sz = SZCHAR; 131213939Slinton break; 131313939Slinton 131413939Slinton case SHORT: 131513939Slinton case USHORT: 131613939Slinton al = ALSHORT; 131713939Slinton sz = SZSHORT; 131813939Slinton break; 131913939Slinton 132013939Slinton case INT: 132113939Slinton case UNSIGNED: 132213939Slinton al = ALINT; 132313939Slinton sz = SZINT; 132413939Slinton break; 132513939Slinton #ifdef LONGFIELDS 132613939Slinton 132713939Slinton case LONG: 132813939Slinton case ULONG: 132913939Slinton al = ALLONG; 133013939Slinton sz = SZLONG; 133113939Slinton break; 133213939Slinton #endif 133313939Slinton 133413939Slinton default: 133513939Slinton if( new < 0 ) { 133613939Slinton uerror( "illegal field type" ); 133713939Slinton al = ALINT; 133813939Slinton } 133913939Slinton else { 134013939Slinton al = fldal( p->stype ); 134113939Slinton sz =SZINT; 134213939Slinton } 134313939Slinton } 134413939Slinton 134513939Slinton if( w > sz ) { 134613939Slinton uerror( "field too big"); 134713939Slinton w = sz; 134813939Slinton } 134913939Slinton 135013939Slinton if( w == 0 ){ /* align only */ 135113939Slinton SETOFF( strucoff, al ); 135213939Slinton if( new >= 0 ) uerror( "zero size field"); 135313939Slinton return(0); 135413939Slinton } 135513939Slinton 135613939Slinton if( strucoff%al + w > sz ) SETOFF( strucoff, al ); 135713939Slinton if( new < 0 ) { 135813939Slinton if( (offsz-strucoff) < w ) 135913939Slinton cerror("structure too large"); 136013939Slinton strucoff += w; /* we know it will fit */ 136113939Slinton return(0); 136213939Slinton } 136313939Slinton 136413939Slinton /* establish the field */ 136513939Slinton 136613939Slinton if( new == 1 ) { /* previous definition */ 136713939Slinton if( p->offset != strucoff || p->sclass != (FIELD|w) ) return(1); 136813939Slinton } 136913939Slinton p->offset = strucoff; 137013939Slinton if( (offsz-strucoff) < w ) cerror("structure too large"); 137113939Slinton strucoff += w; 137213939Slinton p->stype = type; 137313939Slinton fldty( p ); 137413939Slinton return(0); 137513939Slinton } 137613939Slinton 137713939Slinton nidcl( p ) NODE *p; { /* handle unitialized declarations */ 137813939Slinton /* assumed to be not functions */ 137913939Slinton register class; 138013939Slinton register commflag; /* flag for labelled common declarations */ 138113939Slinton 138213939Slinton commflag = 0; 138313939Slinton 138413939Slinton /* compute class */ 138513939Slinton if( (class=curclass) == SNULL ){ 138613939Slinton if( blevel > 1 ) class = AUTO; 138713939Slinton else if( blevel != 0 || instruct ) cerror( "nidcl error" ); 138813939Slinton else { /* blevel = 0 */ 138913939Slinton class = noinit(); 139013939Slinton if( class == EXTERN ) commflag = 1; 139113939Slinton } 139213939Slinton } 139313939Slinton #ifdef LCOMM 139432817Sdonn /* hack so stab will come out as LCSYM rather than STSYM */ 139513939Slinton if (class == STATIC) { 139613939Slinton extern int stabLCSYM; 139713939Slinton stabLCSYM = 1; 139813939Slinton } 139913939Slinton #endif 140013939Slinton 140113939Slinton defid( p, class ); 140213939Slinton 140332816Sdonn /* if an array is not initialized, no empty dimension */ 1404*32820Sdonn if( class!=EXTERN && class!=TYPEDEF && 1405*32820Sdonn ISARY(p->in.type) && dimtab[p->fn.cdim]==0 ) 140632816Sdonn uerror("null storage definition"); 140732816Sdonn 140813939Slinton #ifndef LCOMM 140924405Smckusick if( class==EXTDEF || class==STATIC ) 141013939Slinton #else 141113939Slinton if (class==STATIC) { 141213939Slinton register struct symtab *s = &stab[p->tn.rval]; 141313939Slinton extern int stabLCSYM; 141413939Slinton int sz = tsize(s->stype, s->dimoff, s->sizoff)/SZCHAR; 141513939Slinton 141613939Slinton stabLCSYM = 0; 141713939Slinton if (sz % sizeof (int)) 141813939Slinton sz += sizeof (int) - (sz % sizeof (int)); 141913939Slinton if (s->slevel > 1) 142013939Slinton printf(" .lcomm L%d,%d\n", s->offset, sz); 142113939Slinton else 142213939Slinton printf(" .lcomm %s,%d\n", exname(s->sname), sz); 142324405Smckusick }else if (class == EXTDEF) 142413939Slinton #endif 142524405Smckusick { 142613939Slinton /* simulate initialization by 0 */ 142713939Slinton beginit(p->tn.rval); 142813939Slinton endinit(); 142913939Slinton } 143013939Slinton if( commflag ) commdec( p->tn.rval ); 143113939Slinton } 143213939Slinton 143313939Slinton TWORD 143413939Slinton types( t1, t2, t3 ) TWORD t1, t2, t3; { 143513939Slinton /* return a basic type from basic types t1, t2, and t3 */ 143613939Slinton 143713939Slinton TWORD t[3], noun, adj, unsg; 143813939Slinton register i; 143913939Slinton 144013939Slinton t[0] = t1; 144113939Slinton t[1] = t2; 144213939Slinton t[2] = t3; 144313939Slinton 144413939Slinton unsg = INT; /* INT or UNSIGNED */ 144513939Slinton noun = UNDEF; /* INT, CHAR, or FLOAT */ 144613939Slinton adj = INT; /* INT, LONG, or SHORT */ 144713939Slinton 144813939Slinton for( i=0; i<3; ++i ){ 144913939Slinton switch( t[i] ){ 145013939Slinton 145113939Slinton default: 145213939Slinton bad: 145313939Slinton uerror( "illegal type combination" ); 145413939Slinton return( INT ); 145513939Slinton 145613939Slinton case UNDEF: 145713939Slinton continue; 145813939Slinton 145913939Slinton case UNSIGNED: 146013939Slinton if( unsg != INT ) goto bad; 146113939Slinton unsg = UNSIGNED; 146213939Slinton continue; 146313939Slinton 146413939Slinton case LONG: 146513939Slinton case SHORT: 146613939Slinton if( adj != INT ) goto bad; 146713939Slinton adj = t[i]; 146813939Slinton continue; 146913939Slinton 147013939Slinton case INT: 147113939Slinton case CHAR: 147213939Slinton case FLOAT: 147313939Slinton if( noun != UNDEF ) goto bad; 147413939Slinton noun = t[i]; 147513939Slinton continue; 147613939Slinton } 147713939Slinton } 147813939Slinton 147913939Slinton /* now, construct final type */ 148013939Slinton if( noun == UNDEF ) noun = INT; 148113939Slinton else if( noun == FLOAT ){ 148213939Slinton if( unsg != INT || adj == SHORT ) goto bad; 148313939Slinton return( adj==LONG ? DOUBLE : FLOAT ); 148413939Slinton } 148513939Slinton else if( noun == CHAR && adj != INT ) goto bad; 148613939Slinton 148713939Slinton /* now, noun is INT or CHAR */ 148813939Slinton if( adj != INT ) noun = adj; 148913939Slinton if( unsg == UNSIGNED ) return( noun + (UNSIGNED-INT) ); 149013939Slinton else return( noun ); 149113939Slinton } 149213939Slinton 149313939Slinton NODE * 149413939Slinton tymerge( typ, idp ) NODE *typ, *idp; { 149513939Slinton /* merge type typ with identifier idp */ 149613939Slinton 149713939Slinton register unsigned t; 149813939Slinton register i; 149913939Slinton extern int eprint(); 150013939Slinton 150113939Slinton if( typ->in.op != TYPE ) cerror( "tymerge: arg 1" ); 150213939Slinton if(idp == NIL ) return( NIL ); 150313939Slinton 150413939Slinton # ifndef BUG1 150513939Slinton if( ddebug > 2 ) fwalk( idp, eprint, 0 ); 150613939Slinton # endif 150713939Slinton 150813939Slinton idp->in.type = typ->in.type; 150913939Slinton idp->fn.cdim = curdim; 151013939Slinton tyreduce( idp ); 151113939Slinton idp->fn.csiz = typ->fn.csiz; 151213939Slinton 151313939Slinton for( t=typ->in.type, i=typ->fn.cdim; t&TMASK; t = DECREF(t) ){ 151413939Slinton if( ISARY(t) ) dstash( dimtab[i++] ); 151513939Slinton } 151613939Slinton 151713939Slinton /* now idp is a single node: fix up type */ 151813939Slinton 151913939Slinton idp->in.type = ctype( idp->in.type ); 152013939Slinton 152113939Slinton if( (t = BTYPE(idp->in.type)) != STRTY && t != UNIONTY && t != ENUMTY ){ 152213939Slinton idp->fn.csiz = t; /* in case ctype has rewritten things */ 152313939Slinton } 152413939Slinton 152513939Slinton return( idp ); 152613939Slinton } 152713939Slinton 152813939Slinton tyreduce( p ) register NODE *p; { 152913939Slinton 153013939Slinton /* build a type, and stash away dimensions, from a parse tree of the declaration */ 153113939Slinton /* the type is build top down, the dimensions bottom up */ 153213939Slinton register o, temp; 153313939Slinton register unsigned t; 153413939Slinton 153513939Slinton o = p->in.op; 153613939Slinton p->in.op = FREE; 153713939Slinton 153813939Slinton if( o == NAME ) return; 153913939Slinton 154013939Slinton t = INCREF( p->in.type ); 154113939Slinton if( o == UNARY CALL ) t += (FTN-PTR); 154213939Slinton else if( o == LB ){ 154313939Slinton t += (ARY-PTR); 154413939Slinton temp = p->in.right->tn.lval; 154513939Slinton p->in.right->in.op = FREE; 154632816Sdonn if( temp == 0 && p->in.left->tn.op == LB ) 154732816Sdonn uerror( "null dimension" ); 154813939Slinton } 154913939Slinton 155013939Slinton p->in.left->in.type = t; 155113939Slinton tyreduce( p->in.left ); 155213939Slinton 155313939Slinton if( o == LB ) dstash( temp ); 155413939Slinton 155513939Slinton p->tn.rval = p->in.left->tn.rval; 155613939Slinton p->in.type = p->in.left->in.type; 155713939Slinton 155813939Slinton } 155913939Slinton 156013939Slinton fixtype( p, class ) register NODE *p; { 156113939Slinton register unsigned t, type; 156213939Slinton register mod1, mod2; 156313939Slinton /* fix up the types, and check for legality */ 156413939Slinton 156513939Slinton if( (type = p->in.type) == UNDEF ) return; 156613939Slinton if( mod2 = (type&TMASK) ){ 156713939Slinton t = DECREF(type); 156813939Slinton while( mod1=mod2, mod2 = (t&TMASK) ){ 156913939Slinton if( mod1 == ARY && mod2 == FTN ){ 157013939Slinton uerror( "array of functions is illegal" ); 157113939Slinton type = 0; 157213939Slinton } 157313939Slinton else if( mod1 == FTN && ( mod2 == ARY || mod2 == FTN ) ){ 157413939Slinton uerror( "function returns illegal type" ); 157513939Slinton type = 0; 157613939Slinton } 157713939Slinton t = DECREF(t); 157813939Slinton } 157913939Slinton } 158013939Slinton 158113939Slinton /* detect function arguments, watching out for structure declarations */ 158232816Sdonn /* for example, beware of f(x) struct { int a[10]; } *x; { ... } */ 158313939Slinton /* the danger is that "a" will be converted to a pointer */ 158413939Slinton 158532816Sdonn if( class==SNULL && blevel==1 && !(instruct&(INSTRUCT|INUNION)) ) 158632816Sdonn class = PARAM; 158713939Slinton if( class == PARAM || ( class==REGISTER && blevel==1 ) ){ 158813939Slinton if( type == FLOAT ) type = DOUBLE; 158913939Slinton else if( ISARY(type) ){ 159032816Sdonn #ifdef LINT 159132816Sdonn if( hflag && dimtab[p->fn.cdim]!=0 ) 159232816Sdonn werror("array[%d] type changed to pointer", 159332816Sdonn dimtab[p->fn.cdim]); 159432816Sdonn #endif 159513939Slinton ++p->fn.cdim; 159613939Slinton type += (PTR-ARY); 159713939Slinton } 159813939Slinton else if( ISFTN(type) ){ 159913939Slinton werror( "a function is declared as an argument" ); 160013939Slinton type = INCREF(type); 160113939Slinton } 160213939Slinton 160313939Slinton } 160413939Slinton 160513939Slinton if( instruct && ISFTN(type) ){ 160613939Slinton uerror( "function illegal in structure or union" ); 160713939Slinton type = INCREF(type); 160813939Slinton } 160913939Slinton p->in.type = type; 161013939Slinton } 161113939Slinton 161213939Slinton uclass( class ) register class; { 161313939Slinton /* give undefined version of class */ 161413939Slinton if( class == SNULL ) return( EXTERN ); 161513939Slinton else if( class == STATIC ) return( USTATIC ); 161613939Slinton else if( class == FORTRAN ) return( UFORTRAN ); 161713939Slinton else return( class ); 161813939Slinton } 161913939Slinton 162013939Slinton fixclass( class, type ) TWORD type; { 162113939Slinton 162213939Slinton /* first, fix null class */ 162313939Slinton 162413939Slinton if( class == SNULL ){ 162513939Slinton if( instruct&INSTRUCT ) class = MOS; 162613939Slinton else if( instruct&INUNION ) class = MOU; 162713939Slinton else if( blevel == 0 ) class = EXTDEF; 162813939Slinton else if( blevel == 1 ) class = PARAM; 162913939Slinton else class = AUTO; 163013939Slinton 163113939Slinton } 163213939Slinton 163313939Slinton /* now, do general checking */ 163413939Slinton 163513939Slinton if( ISFTN( type ) ){ 163613939Slinton switch( class ) { 163713939Slinton default: 163813939Slinton uerror( "function has illegal storage class" ); 163913939Slinton case AUTO: 164013939Slinton class = EXTERN; 164113939Slinton case EXTERN: 164213939Slinton case EXTDEF: 164313939Slinton case FORTRAN: 164413939Slinton case TYPEDEF: 164513939Slinton case STATIC: 164613939Slinton case UFORTRAN: 164713939Slinton case USTATIC: 164813939Slinton ; 164913939Slinton } 165013939Slinton } 165113939Slinton 165213939Slinton if( class&FIELD ){ 165313939Slinton if( !(instruct&INSTRUCT) ) uerror( "illegal use of field" ); 165413939Slinton return( class ); 165513939Slinton } 165613939Slinton 165713939Slinton switch( class ){ 165813939Slinton 165913939Slinton case MOU: 166013939Slinton if( !(instruct&INUNION) ) uerror( "illegal class" ); 166113939Slinton return( class ); 166213939Slinton 166313939Slinton case MOS: 166413939Slinton if( !(instruct&INSTRUCT) ) uerror( "illegal class" ); 166513939Slinton return( class ); 166613939Slinton 166713939Slinton case MOE: 166813939Slinton if( instruct & (INSTRUCT|INUNION) ) uerror( "illegal class" ); 166913939Slinton return( class ); 167013939Slinton 167113939Slinton case REGISTER: 167213939Slinton if( blevel == 0 ) uerror( "illegal register declaration" ); 167313939Slinton else if( regvar >= MINRVAR && cisreg( type ) ) return( class ); 167413939Slinton if( blevel == 1 ) return( PARAM ); 167513939Slinton else return( AUTO ); 167613939Slinton 167713939Slinton case AUTO: 167813939Slinton case LABEL: 167913939Slinton case ULABEL: 168013939Slinton if( blevel < 2 ) uerror( "illegal class" ); 168113939Slinton return( class ); 168213939Slinton 168313939Slinton case PARAM: 168413939Slinton if( blevel != 1 ) uerror( "illegal class" ); 168513939Slinton return( class ); 168613939Slinton 168713939Slinton case UFORTRAN: 168813939Slinton case FORTRAN: 168913939Slinton # ifdef NOFORTRAN 169013939Slinton NOFORTRAN; /* a condition which can regulate the FORTRAN usage */ 169113939Slinton # endif 169213939Slinton if( !ISFTN(type) ) uerror( "fortran declaration must apply to function" ); 169313939Slinton else { 169413939Slinton type = DECREF(type); 169513939Slinton if( ISFTN(type) || ISARY(type) || ISPTR(type) ) { 169613939Slinton uerror( "fortran function has wrong type" ); 169713939Slinton } 169813939Slinton } 169913939Slinton case EXTERN: 170013939Slinton case STATIC: 170113939Slinton case EXTDEF: 170213939Slinton case TYPEDEF: 170313939Slinton case USTATIC: 170417151Sralph if( blevel == 1 ){ 170517151Sralph uerror( "illegal class" ); 170617151Sralph return( PARAM ); 170717151Sralph } 170817151Sralph case STNAME: 170917151Sralph case UNAME: 171017151Sralph case ENAME: 171113939Slinton return( class ); 171213939Slinton 171313939Slinton default: 171413939Slinton cerror( "illegal class: %d", class ); 171513939Slinton /* NOTREACHED */ 171613939Slinton 171713939Slinton } 171813939Slinton } 171913939Slinton 172013939Slinton struct symtab * 172113939Slinton mknonuniq(idindex) int *idindex; {/* locate a symbol table entry for */ 172213939Slinton /* an occurrence of a nonunique structure member name */ 172313939Slinton /* or field */ 172413939Slinton register i; 172513939Slinton register struct symtab * sp; 172632817Sdonn char *q; 172713939Slinton 172813939Slinton sp = & stab[ i= *idindex ]; /* position search at old entry */ 172913939Slinton while( sp->stype != TNULL ){ /* locate unused entry */ 173013939Slinton if( ++i >= SYMTSZ ){/* wrap around symbol table */ 173113939Slinton i = 0; 173213939Slinton sp = stab; 173313939Slinton } 173413939Slinton else ++sp; 173513939Slinton if( i == *idindex ) cerror("Symbol table full"); 173613939Slinton } 173713939Slinton sp->sflags = SNONUNIQ | SMOS; 173813939Slinton q = stab[*idindex].sname; /* old entry name */ 173913939Slinton #ifdef FLEXNAMES 174013939Slinton sp->sname = stab[*idindex].sname; 174113939Slinton #endif 174213939Slinton # ifndef BUG1 174313939Slinton if( ddebug ){ 174413939Slinton printf("\tnonunique entry for %s from %d to %d\n", 174513939Slinton q, *idindex, i ); 174613939Slinton } 174713939Slinton # endif 174813939Slinton *idindex = i; 174913939Slinton #ifndef FLEXNAMES 175032817Sdonn { 175132817Sdonn char *p = sp->sname; 175232817Sdonn for( i=1; i<=NCHNAM; ++i ) /* copy name */ 175332817Sdonn if( *p++ = *q /* assign */ ) ++q; 175413939Slinton } 175513939Slinton #endif 175613939Slinton return ( sp ); 175713939Slinton } 175813939Slinton 175913939Slinton lookup( name, s) char *name; { 176013939Slinton /* look up name: must agree with s w.r.t. STAG, SMOS and SHIDDEN */ 176113939Slinton 176213939Slinton register char *p, *q; 176332817Sdonn int i, ii; 176432817Sdonn #ifndef FLEXNAMES 176532817Sdonn int j; 176632817Sdonn #endif 176713939Slinton register struct symtab *sp; 176813939Slinton 176913939Slinton /* compute initial hash index */ 177013939Slinton # ifndef BUG1 177113939Slinton if( ddebug > 2 ){ 177213939Slinton printf( "lookup( %s, %d ), stwart=%d, instruct=%d\n", name, s, stwart, instruct ); 177313939Slinton } 177413939Slinton # endif 177513939Slinton 177613939Slinton i = 0; 177713939Slinton #ifndef FLEXNAMES 177813939Slinton for( p=name, j=0; *p != '\0'; ++p ){ 177913939Slinton i += *p; 178013939Slinton if( ++j >= NCHNAM ) break; 178113939Slinton } 178213939Slinton #else 178313939Slinton i = (int)name; 178413939Slinton #endif 178513939Slinton i = i%SYMTSZ; 178613939Slinton sp = &stab[ii=i]; 178713939Slinton 178813939Slinton for(;;){ /* look for name */ 178913939Slinton 179013939Slinton if( sp->stype == TNULL ){ /* empty slot */ 179113939Slinton sp->sflags = s; /* set STAG, SMOS if needed, turn off all others */ 179213939Slinton #ifndef FLEXNAMES 179313939Slinton p = sp->sname; 179413939Slinton for( j=0; j<NCHNAM; ++j ) if( *p++ = *name ) ++name; 179513939Slinton #else 179613939Slinton sp->sname = name; 179713939Slinton #endif 179813939Slinton sp->stype = UNDEF; 179913939Slinton sp->sclass = SNULL; 180013939Slinton return( i ); 180113939Slinton } 180213939Slinton if( (sp->sflags & (STAG|SMOS|SHIDDEN)) != s ) goto next; 180313939Slinton p = sp->sname; 180413939Slinton q = name; 180513939Slinton #ifndef FLEXNAMES 180613939Slinton for( j=0; j<NCHNAM;++j ){ 180713939Slinton if( *p++ != *q ) goto next; 180813939Slinton if( !*q++ ) break; 180913939Slinton } 181013939Slinton return( i ); 181113939Slinton #else 181213939Slinton if (p == q) 181313939Slinton return ( i ); 181413939Slinton #endif 181513939Slinton next: 181613939Slinton if( ++i >= SYMTSZ ){ 181713939Slinton i = 0; 181813939Slinton sp = stab; 181913939Slinton } 182013939Slinton else ++sp; 182113939Slinton if( i == ii ) cerror( "symbol table full" ); 182213939Slinton } 182313939Slinton } 182413939Slinton 182513939Slinton #ifndef checkst 182613939Slinton /* if not debugging, make checkst a macro */ 182713939Slinton checkst(lev){ 182813939Slinton register int s, i, j; 182913939Slinton register struct symtab *p, *q; 183013939Slinton 183113939Slinton for( i=0, p=stab; i<SYMTSZ; ++i, ++p ){ 183213939Slinton if( p->stype == TNULL ) continue; 183313939Slinton j = lookup( p->sname, p->sflags&(SMOS|STAG) ); 183413939Slinton if( j != i ){ 183513939Slinton q = &stab[j]; 183613939Slinton if( q->stype == UNDEF || 183713939Slinton q->slevel <= p->slevel ){ 183813939Slinton #ifndef FLEXNAMES 183913939Slinton cerror( "check error: %.8s", q->sname ); 184013939Slinton #else 184113939Slinton cerror( "check error: %s", q->sname ); 184213939Slinton #endif 184313939Slinton } 184413939Slinton } 184513939Slinton #ifndef FLEXNAMES 184613939Slinton else if( p->slevel > lev ) cerror( "%.8s check at level %d", p->sname, lev ); 184713939Slinton #else 184813939Slinton else if( p->slevel > lev ) cerror( "%s check at level %d", p->sname, lev ); 184913939Slinton #endif 185013939Slinton } 185113939Slinton } 185213939Slinton #endif 185313939Slinton 185413939Slinton struct symtab * 185513939Slinton relook(p) register struct symtab *p; { /* look up p again, and see where it lies */ 185613939Slinton 185713939Slinton register struct symtab *q; 185813939Slinton 185913939Slinton /* I'm not sure that this handles towers of several hidden definitions in all cases */ 186013939Slinton q = &stab[lookup( p->sname, p->sflags&(STAG|SMOS|SHIDDEN) )]; 186113939Slinton /* make relook always point to either p or an empty cell */ 186213939Slinton if( q->stype == UNDEF ){ 186313939Slinton q->stype = TNULL; 186413939Slinton return(q); 186513939Slinton } 186613939Slinton while( q != p ){ 186713939Slinton if( q->stype == TNULL ) break; 186813939Slinton if( ++q >= &stab[SYMTSZ] ) q=stab; 186913939Slinton } 187013939Slinton return(q); 187113939Slinton } 187213939Slinton 187324405Smckusick clearst( lev ) register int lev; { 187424405Smckusick register struct symtab *p, *q; 187524405Smckusick register int temp; 187624405Smckusick struct symtab *clist = 0; 187713939Slinton 187813939Slinton temp = lineno; 187913939Slinton aobeg(); 188013939Slinton 188124405Smckusick /* step 1: remove entries */ 188224405Smckusick while( chaintop-1 > lev ){ 188324405Smckusick p = schain[--chaintop]; 188424405Smckusick schain[chaintop] = 0; 188524405Smckusick for( ; p; p = q ){ 188624405Smckusick q = p->snext; 188724405Smckusick if( p->stype == TNULL || p->slevel <= lev ) 188824405Smckusick cerror( "schain botch" ); 188924405Smckusick lineno = p->suse < 0 ? -p->suse : p->suse; 189024405Smckusick if( p->stype==UNDEF || ( p->sclass==ULABEL && lev<2 ) ){ 189113939Slinton lineno = temp; 189213939Slinton #ifndef FLEXNAMES 189313939Slinton uerror( "%.8s undefined", p->sname ); 189413939Slinton #else 189513939Slinton uerror( "%s undefined", p->sname ); 189613939Slinton #endif 189713939Slinton } 189813939Slinton else aocode(p); 189913939Slinton # ifndef BUG1 190024405Smckusick if( ddebug ){ 190113939Slinton #ifndef FLEXNAMES 190224405Smckusick printf( "removing %.8s", p->sname ); 190313939Slinton #else 190424405Smckusick printf( "removing %s", p->sname ); 190513939Slinton #endif 190624405Smckusick printf( " from stab[%d], flags %o level %d\n", 190724405Smckusick p-stab, p->sflags, p->slevel); 190824405Smckusick } 190913939Slinton # endif 191024405Smckusick if( p->sflags & SHIDES )unhide( p ); 191113939Slinton p->stype = TNULL; 191224405Smckusick p->snext = clist; 191324405Smckusick clist = p; 191413939Slinton } 191524405Smckusick } 191624405Smckusick 191724405Smckusick /* step 2: fix any mishashed entries */ 191824405Smckusick p = clist; 191924405Smckusick while( p ){ 192027247Sdonn register struct symtab *r, *next; 192124405Smckusick 192224405Smckusick q = p; 192327247Sdonn next = p->snext; 192424405Smckusick for(;;){ 192524405Smckusick if( ++q >= &stab[SYMTSZ] )q = stab; 192624405Smckusick if( q == p || q->stype == TNULL )break; 192724405Smckusick if( (r = relook(q)) != q ) { 192824405Smckusick *r = *q; 192926822Sdonn q->stype = TNULL; 193013939Slinton } 193113939Slinton } 193227247Sdonn p = next; 193313939Slinton } 193424405Smckusick 193513939Slinton lineno = temp; 193613939Slinton aoend(); 193713939Slinton } 193813939Slinton 193913939Slinton hide( p ) register struct symtab *p; { 194013939Slinton register struct symtab *q; 194113939Slinton for( q=p+1; ; ++q ){ 194213939Slinton if( q >= &stab[SYMTSZ] ) q = stab; 194313939Slinton if( q == p ) cerror( "symbol table full" ); 194413939Slinton if( q->stype == TNULL ) break; 194513939Slinton } 194624405Smckusick *q = *p; 194713939Slinton p->sflags |= SHIDDEN; 194813939Slinton q->sflags = (p->sflags&(SMOS|STAG)) | SHIDES; 194913939Slinton #ifndef FLEXNAMES 195013939Slinton if( hflag ) werror( "%.8s redefinition hides earlier one", p->sname ); 195113939Slinton #else 195213939Slinton if( hflag ) werror( "%s redefinition hides earlier one", p->sname ); 195313939Slinton #endif 195413939Slinton # ifndef BUG1 195513939Slinton if( ddebug ) printf( " %d hidden in %d\n", p-stab, q-stab ); 195613939Slinton # endif 195713939Slinton return( idname = q-stab ); 195813939Slinton } 195913939Slinton 196013939Slinton unhide( p ) register struct symtab *p; { 196113939Slinton register struct symtab *q; 196232817Sdonn register s; 196313939Slinton 196413939Slinton s = p->sflags & (SMOS|STAG); 196513939Slinton q = p; 196613939Slinton 196713939Slinton for(;;){ 196813939Slinton 196913939Slinton if( q == stab ) q = &stab[SYMTSZ-1]; 197013939Slinton else --q; 197113939Slinton 197213939Slinton if( q == p ) break; 197313939Slinton 197413939Slinton if( (q->sflags&(SMOS|STAG)) == s ){ 197513939Slinton #ifndef FLEXNAMES 197632817Sdonn register j; 197713939Slinton for( j =0; j<NCHNAM; ++j ) if( p->sname[j] != q->sname[j] ) break; 197813939Slinton if( j == NCHNAM ){ /* found the name */ 197913939Slinton #else 198013939Slinton if (p->sname == q->sname) { 198113939Slinton #endif 198213939Slinton q->sflags &= ~SHIDDEN; 198313939Slinton # ifndef BUG1 198413939Slinton if( ddebug ) printf( "unhide uncovered %d from %d\n", q-stab,p-stab); 198513939Slinton # endif 198613939Slinton return; 198713939Slinton } 198813939Slinton } 198913939Slinton 199013939Slinton } 199113939Slinton cerror( "unhide fails" ); 199213939Slinton } 1993