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