117746Sralph #ifndef lint 2*25749Sdonn static char *sccsid ="@(#)pftn.c 1.10 (Berkeley) 01/08/86"; 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 */ 40613939Slinton if( retlab != NOLAB ){ /* 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 ) { 770*25749Sdonn if( ty == STRTY ) 771*25749Sdonn uerror( "undefined structure" ); 772*25749Sdonn else 773*25749Sdonn 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 83213939Slinton int iclass; /* storage class of thing being initialized */ 83313939Slinton 83413939Slinton int ilocctr = 0; /* location counter for current initialization */ 83513939Slinton 83613939Slinton beginit(curid){ 83713939Slinton /* beginning of initilization; set location ctr and set type */ 83813939Slinton register struct symtab *p; 83913939Slinton 84013939Slinton # ifndef BUG1 84113939Slinton if( idebug >= 3 ) printf( "beginit(), curid = %d\n", curid ); 84213939Slinton # endif 84313939Slinton 84413939Slinton p = &stab[curid]; 84513939Slinton 84613939Slinton iclass = p->sclass; 84713939Slinton if( curclass == EXTERN || curclass == FORTRAN ) iclass = EXTERN; 84813939Slinton switch( iclass ){ 84913939Slinton 85013939Slinton case UNAME: 85113939Slinton case EXTERN: 85213939Slinton return; 85313939Slinton case AUTO: 85413939Slinton case REGISTER: 85513939Slinton break; 85613939Slinton case EXTDEF: 85713939Slinton case STATIC: 85813939Slinton ilocctr = ISARY(p->stype)?ADATA:DATA; 85913939Slinton locctr( ilocctr ); 86013939Slinton defalign( talign( p->stype, p->sizoff ) ); 86113939Slinton defnam( p ); 86213939Slinton 86313939Slinton } 86413939Slinton 86513939Slinton inoff = 0; 86613939Slinton ibseen = 0; 86713939Slinton 86813939Slinton pstk = 0; 86913939Slinton 87013939Slinton instk( curid, p->stype, p->dimoff, p->sizoff, inoff ); 87113939Slinton 87213939Slinton } 87313939Slinton 87413939Slinton instk( id, t, d, s, off ) OFFSZ off; TWORD t; { 87513939Slinton /* make a new entry on the parameter stack to initialize id */ 87613939Slinton 87713939Slinton register struct symtab *p; 87813939Slinton 87913939Slinton for(;;){ 88013939Slinton # ifndef BUG1 88113939Slinton if( idebug ) printf( "instk((%d, %o,%d,%d, %d)\n", id, t, d, s, off ); 88213939Slinton # endif 88313939Slinton 88413939Slinton /* save information on the stack */ 88513939Slinton 88613939Slinton if( !pstk ) pstk = instack; 88713939Slinton else ++pstk; 88813939Slinton 88913939Slinton pstk->in_fl = 0; /* { flag */ 89013939Slinton pstk->in_id = id ; 89113939Slinton pstk->in_t = t ; 89213939Slinton pstk->in_d = d ; 89313939Slinton pstk->in_s = s ; 89413939Slinton pstk->in_n = 0; /* number seen */ 89513939Slinton pstk->in_x = t==STRTY ?dimtab[s+1] : 0 ; 89613939Slinton pstk->in_off = off; /* offset at the beginning of this element */ 89713939Slinton /* if t is an array, DECREF(t) can't be a field */ 89813939Slinton /* INS_sz has size of array elements, and -size for fields */ 89913939Slinton if( ISARY(t) ){ 90013939Slinton pstk->in_sz = tsize( DECREF(t), d+1, s ); 90113939Slinton } 90213939Slinton else if( stab[id].sclass & FIELD ){ 90313939Slinton pstk->in_sz = - ( stab[id].sclass & FLDSIZ ); 90413939Slinton } 90513939Slinton else { 90613939Slinton pstk->in_sz = 0; 90713939Slinton } 90813939Slinton 90913939Slinton if( (iclass==AUTO || iclass == REGISTER ) && 91013939Slinton (ISARY(t) || t==STRTY) ) uerror( "no automatic aggregate initialization" ); 91113939Slinton 91213939Slinton /* now, if this is not a scalar, put on another element */ 91313939Slinton 91413939Slinton if( ISARY(t) ){ 91513939Slinton t = DECREF(t); 91613939Slinton ++d; 91713939Slinton continue; 91813939Slinton } 91913939Slinton else if( t == STRTY ){ 920*25749Sdonn if( dimtab[pstk->in_s] == 0 ){ 921*25749Sdonn uerror( "can't initialize undefined structure" ); 922*25749Sdonn iclass = -1; 923*25749Sdonn return; 924*25749Sdonn } 92513939Slinton id = dimtab[pstk->in_x]; 92613939Slinton p = &stab[id]; 92713939Slinton if( p->sclass != MOS && !(p->sclass&FIELD) ) cerror( "insane structure member list" ); 92813939Slinton t = p->stype; 92913939Slinton d = p->dimoff; 93013939Slinton s = p->sizoff; 93113939Slinton off += p->offset; 93213939Slinton continue; 93313939Slinton } 93413939Slinton else return; 93513939Slinton } 93613939Slinton } 93713939Slinton 93813939Slinton NODE * 93913939Slinton getstr(){ /* decide if the string is external or an initializer, and get the contents accordingly */ 94013939Slinton 94113939Slinton register l, temp; 94213939Slinton register NODE *p; 94313939Slinton 94413939Slinton if( (iclass==EXTDEF||iclass==STATIC) && (pstk->in_t == CHAR || pstk->in_t == UCHAR) && 94513939Slinton pstk!=instack && ISARY( pstk[-1].in_t ) ){ 94613939Slinton /* treat "abc" as { 'a', 'b', 'c', 0 } */ 94713939Slinton strflg = 1; 94813939Slinton ilbrace(); /* simulate { */ 94913939Slinton inforce( pstk->in_off ); 95013939Slinton /* if the array is inflexible (not top level), pass in the size and 95113939Slinton be prepared to throw away unwanted initializers */ 95213939Slinton lxstr((pstk-1)!=instack?dimtab[(pstk-1)->in_d]:0); /* get the contents */ 95313939Slinton irbrace(); /* simulate } */ 95413939Slinton return( NIL ); 95513939Slinton } 95613939Slinton else { /* make a label, and get the contents and stash them away */ 95713939Slinton if( iclass != SNULL ){ /* initializing */ 95813939Slinton /* fill out previous word, to permit pointer */ 95913939Slinton vfdalign( ALPOINT ); 96013939Slinton } 96113939Slinton temp = locctr( blevel==0?ISTRNG:STRNG ); /* set up location counter */ 96213939Slinton deflab( l = getlab() ); 96313939Slinton strflg = 0; 96413939Slinton lxstr(0); /* get the contents */ 96513939Slinton locctr( blevel==0?ilocctr:temp ); 96613939Slinton p = buildtree( STRING, NIL, NIL ); 96713939Slinton p->tn.rval = -l; 96813939Slinton return(p); 96913939Slinton } 97013939Slinton } 97113939Slinton 97213939Slinton putbyte( v ){ /* simulate byte v appearing in a list of integer values */ 97313939Slinton register NODE *p; 97413939Slinton p = bcon(v); 97513939Slinton incode( p, SZCHAR ); 97613939Slinton tfree( p ); 97713939Slinton gotscal(); 97813939Slinton } 97913939Slinton 98013939Slinton endinit(){ 98113939Slinton register TWORD t; 98213939Slinton register d, s, n, d1; 98313939Slinton 98413939Slinton # ifndef BUG1 98513939Slinton if( idebug ) printf( "endinit(), inoff = %d\n", inoff ); 98613939Slinton # endif 98713939Slinton 98813939Slinton switch( iclass ){ 98913939Slinton 99013939Slinton case EXTERN: 99113939Slinton case AUTO: 99213939Slinton case REGISTER: 993*25749Sdonn case -1: 99413939Slinton return; 99513939Slinton } 99613939Slinton 99713939Slinton pstk = instack; 99813939Slinton 99913939Slinton t = pstk->in_t; 100013939Slinton d = pstk->in_d; 100113939Slinton s = pstk->in_s; 100213939Slinton n = pstk->in_n; 100313939Slinton 100413939Slinton if( ISARY(t) ){ 100513939Slinton d1 = dimtab[d]; 100613939Slinton 100713939Slinton vfdalign( pstk->in_sz ); /* fill out part of the last element, if needed */ 100813939Slinton n = inoff/pstk->in_sz; /* real number of initializers */ 100913939Slinton if( d1 >= n ){ 101013939Slinton /* once again, t is an array, so no fields */ 101113939Slinton inforce( tsize( t, d, s ) ); 101213939Slinton n = d1; 101313939Slinton } 101413939Slinton if( d1!=0 && d1!=n ) uerror( "too many initializers"); 101513939Slinton if( n==0 ) werror( "empty array declaration"); 101613939Slinton dimtab[d] = n; 101713942Slinton if( d1==0 ) FIXDEF(&stab[pstk->in_id]); 101813939Slinton } 101913939Slinton 102013939Slinton else if( t == STRTY || t == UNIONTY ){ 102113939Slinton /* clearly not fields either */ 102213939Slinton inforce( tsize( t, d, s ) ); 102313939Slinton } 102413939Slinton else if( n > 1 ) uerror( "bad scalar initialization"); 102513939Slinton /* this will never be called with a field element... */ 102613939Slinton else inforce( tsize(t,d,s) ); 102713939Slinton 102813939Slinton paramno = 0; 102913939Slinton vfdalign( AL_INIT ); 103013939Slinton inoff = 0; 103113939Slinton iclass = SNULL; 103213939Slinton 103313939Slinton } 103413939Slinton 103513939Slinton doinit( p ) register NODE *p; { 103613939Slinton 103713939Slinton /* take care of generating a value for the initializer p */ 103813939Slinton /* inoff has the current offset (last bit written) 103913939Slinton in the current word being generated */ 104013939Slinton 104113939Slinton register sz, d, s; 104213939Slinton register TWORD t; 104317746Sralph int o; 104413939Slinton 104513939Slinton /* note: size of an individual initializer is assumed to fit into an int */ 104613939Slinton 104713939Slinton if( iclass < 0 ) goto leave; 104813939Slinton if( iclass == EXTERN || iclass == UNAME ){ 104913939Slinton uerror( "cannot initialize extern or union" ); 105013939Slinton iclass = -1; 105113939Slinton goto leave; 105213939Slinton } 105313939Slinton 105413939Slinton if( iclass == AUTO || iclass == REGISTER ){ 105513939Slinton /* do the initialization and get out, without regard 105613939Slinton for filing out the variable with zeros, etc. */ 105713939Slinton bccode(); 105813939Slinton idname = pstk->in_id; 105913939Slinton p = buildtree( ASSIGN, buildtree( NAME, NIL, NIL ), p ); 106013939Slinton ecomp(p); 106113939Slinton return; 106213939Slinton } 106313939Slinton 106413939Slinton if( p == NIL ) return; /* for throwing away strings that have been turned into lists */ 106513939Slinton 106613939Slinton if( ibseen ){ 106713939Slinton uerror( "} expected"); 106813939Slinton goto leave; 106913939Slinton } 107013939Slinton 107113939Slinton # ifndef BUG1 107213939Slinton if( idebug > 1 ) printf( "doinit(%o)\n", p ); 107313939Slinton # endif 107413939Slinton 107513939Slinton t = pstk->in_t; /* type required */ 107613939Slinton d = pstk->in_d; 107713939Slinton s = pstk->in_s; 107813939Slinton if( pstk->in_sz < 0 ){ /* bit field */ 107913939Slinton sz = -pstk->in_sz; 108013939Slinton } 108113939Slinton else { 108213939Slinton sz = tsize( t, d, s ); 108313939Slinton } 108413939Slinton 108513939Slinton inforce( pstk->in_off ); 108613939Slinton 108713939Slinton p = buildtree( ASSIGN, block( NAME, NIL,NIL, t, d, s ), p ); 108813939Slinton p->in.left->in.op = FREE; 108913939Slinton p->in.left = p->in.right; 109013939Slinton p->in.right = NIL; 109113939Slinton p->in.left = optim( p->in.left ); 109217746Sralph o = p->in.left->in.op; 109317746Sralph if( o == UNARY AND ){ 109417746Sralph o = p->in.left->in.op = FREE; 109513939Slinton p->in.left = p->in.left->in.left; 109613939Slinton } 109713939Slinton p->in.op = INIT; 109813939Slinton 109913939Slinton if( sz < SZINT ){ /* special case: bit fields, etc. */ 110017746Sralph if( o != ICON ) uerror( "illegal initialization" ); 110113939Slinton else incode( p->in.left, sz ); 110213939Slinton } 110317746Sralph else if( o == FCON ){ 110417746Sralph fincode( p->in.left->fpn.fval, sz ); 110513939Slinton } 110617746Sralph else if( o == DCON ){ 110717746Sralph fincode( p->in.left->dpn.dval, sz ); 110817746Sralph } 110913939Slinton else { 111016178Sralph p = optim(p); 111116178Sralph if( p->in.left->in.op != ICON ) uerror( "illegal initialization" ); 111216178Sralph else cinit( p, sz ); 111313939Slinton } 111413939Slinton 111513939Slinton gotscal(); 111613939Slinton 111713939Slinton leave: 111813939Slinton tfree(p); 111913939Slinton } 112013939Slinton 112113939Slinton gotscal(){ 112213939Slinton register t, ix; 112313939Slinton register n, id; 112413939Slinton struct symtab *p; 112513939Slinton OFFSZ temp; 112613939Slinton 112713939Slinton for( ; pstk > instack; ) { 112813939Slinton 112913939Slinton if( pstk->in_fl ) ++ibseen; 113013939Slinton 113113939Slinton --pstk; 113213939Slinton 113313939Slinton t = pstk->in_t; 113413939Slinton 113513939Slinton if( t == STRTY ){ 113613939Slinton ix = ++pstk->in_x; 113713939Slinton if( (id=dimtab[ix]) < 0 ) continue; 113813939Slinton 113913939Slinton /* otherwise, put next element on the stack */ 114013939Slinton 114113939Slinton p = &stab[id]; 114213939Slinton instk( id, p->stype, p->dimoff, p->sizoff, p->offset+pstk->in_off ); 114313939Slinton return; 114413939Slinton } 114513939Slinton else if( ISARY(t) ){ 114613939Slinton n = ++pstk->in_n; 114713939Slinton if( n >= dimtab[pstk->in_d] && pstk > instack ) continue; 114813939Slinton 114913939Slinton /* put the new element onto the stack */ 115013939Slinton 115113939Slinton temp = pstk->in_sz; 115213939Slinton instk( pstk->in_id, (TWORD)DECREF(pstk->in_t), pstk->in_d+1, pstk->in_s, 115313939Slinton pstk->in_off+n*temp ); 115413939Slinton return; 115513939Slinton } 115613939Slinton 115713939Slinton } 115813939Slinton 115913939Slinton } 116013939Slinton 116113939Slinton ilbrace(){ /* process an initializer's left brace */ 116213939Slinton register t; 116313939Slinton struct instk *temp; 116413939Slinton 116513939Slinton temp = pstk; 116613939Slinton 116713939Slinton for( ; pstk > instack; --pstk ){ 116813939Slinton 116913939Slinton t = pstk->in_t; 117013939Slinton if( t != STRTY && !ISARY(t) ) continue; /* not an aggregate */ 117113939Slinton if( pstk->in_fl ){ /* already associated with a { */ 117213939Slinton if( pstk->in_n ) uerror( "illegal {"); 117313939Slinton continue; 117413939Slinton } 117513939Slinton 117613939Slinton /* we have one ... */ 117713939Slinton pstk->in_fl = 1; 117813939Slinton break; 117913939Slinton } 118013939Slinton 118113939Slinton /* cannot find one */ 118213939Slinton /* ignore such right braces */ 118313939Slinton 118413939Slinton pstk = temp; 118513939Slinton } 118613939Slinton 118713939Slinton irbrace(){ 118813939Slinton /* called when a '}' is seen */ 118913939Slinton 119013939Slinton # ifndef BUG1 119113939Slinton if( idebug ) printf( "irbrace(): paramno = %d on entry\n", paramno ); 119213939Slinton # endif 119313939Slinton 119413939Slinton if( ibseen ) { 119513939Slinton --ibseen; 119613939Slinton return; 119713939Slinton } 119813939Slinton 119913939Slinton for( ; pstk > instack; --pstk ){ 120013939Slinton if( !pstk->in_fl ) continue; 120113939Slinton 120213939Slinton /* we have one now */ 120313939Slinton 120413939Slinton pstk->in_fl = 0; /* cancel { */ 120513939Slinton gotscal(); /* take it away... */ 120613939Slinton return; 120713939Slinton } 120813939Slinton 120913939Slinton /* these right braces match ignored left braces: throw out */ 121013939Slinton 121113939Slinton } 121213939Slinton 121313939Slinton upoff( size, alignment, poff ) register alignment, *poff; { 121413939Slinton /* update the offset pointed to by poff; return the 121513939Slinton /* offset of a value of size `size', alignment `alignment', 121613939Slinton /* given that off is increasing */ 121713939Slinton 121813939Slinton register off; 121913939Slinton 122013939Slinton off = *poff; 122113939Slinton SETOFF( off, alignment ); 122213939Slinton if( (offsz-off) < size ){ 122313939Slinton if( instruct!=INSTRUCT )cerror("too many local variables"); 122413939Slinton else cerror("Structure too large"); 122513939Slinton } 122613939Slinton *poff = off+size; 122713939Slinton return( off ); 122813939Slinton } 122913939Slinton 123013939Slinton oalloc( p, poff ) register struct symtab *p; register *poff; { 123113939Slinton /* allocate p with offset *poff, and update *poff */ 123213939Slinton register al, off, tsz; 123313939Slinton int noff; 123413939Slinton 123513939Slinton al = talign( p->stype, p->sizoff ); 123613939Slinton noff = off = *poff; 123713939Slinton tsz = tsize( p->stype, p->dimoff, p->sizoff ); 123813939Slinton #ifdef BACKAUTO 123913939Slinton if( p->sclass == AUTO ){ 124013939Slinton if( (offsz-off) < tsz ) cerror("too many local variables"); 124113939Slinton noff = off + tsz; 124213939Slinton SETOFF( noff, al ); 124313939Slinton off = -noff; 124413939Slinton } 124513939Slinton else 124613939Slinton #endif 124713939Slinton if( p->sclass == PARAM && ( tsz < SZINT ) ){ 124813939Slinton off = upoff( SZINT, ALINT, &noff ); 124913939Slinton # ifndef RTOLBYTES 125013939Slinton off = noff - tsz; 125113939Slinton #endif 125213939Slinton } 125313939Slinton else 125413939Slinton { 125513939Slinton off = upoff( tsz, al, &noff ); 125613939Slinton } 125713939Slinton 125813939Slinton if( p->sclass != REGISTER ){ /* in case we are allocating stack space for register arguments */ 125913939Slinton if( p->offset == NOOFFSET ) p->offset = off; 126013939Slinton else if( off != p->offset ) return(1); 126113939Slinton } 126213939Slinton 126313939Slinton *poff = noff; 126413939Slinton return(0); 126513939Slinton } 126613939Slinton 126713939Slinton falloc( p, w, new, pty ) register struct symtab *p; NODE *pty; { 126813939Slinton /* allocate a field of width w */ 126913939Slinton /* new is 0 if new entry, 1 if redefinition, -1 if alignment */ 127013939Slinton 127113939Slinton register al,sz,type; 127213939Slinton 127313939Slinton type = (new<0)? pty->in.type : p->stype; 127413939Slinton 127513939Slinton /* this must be fixed to use the current type in alignments */ 127613939Slinton switch( new<0?pty->in.type:p->stype ){ 127713939Slinton 127813939Slinton case ENUMTY: 127913939Slinton { 128013939Slinton int s; 128113939Slinton s = new<0 ? pty->fn.csiz : p->sizoff; 128213939Slinton al = dimtab[s+2]; 128313939Slinton sz = dimtab[s]; 128413939Slinton break; 128513939Slinton } 128613939Slinton 128713939Slinton case CHAR: 128813939Slinton case UCHAR: 128913939Slinton al = ALCHAR; 129013939Slinton sz = SZCHAR; 129113939Slinton break; 129213939Slinton 129313939Slinton case SHORT: 129413939Slinton case USHORT: 129513939Slinton al = ALSHORT; 129613939Slinton sz = SZSHORT; 129713939Slinton break; 129813939Slinton 129913939Slinton case INT: 130013939Slinton case UNSIGNED: 130113939Slinton al = ALINT; 130213939Slinton sz = SZINT; 130313939Slinton break; 130413939Slinton #ifdef LONGFIELDS 130513939Slinton 130613939Slinton case LONG: 130713939Slinton case ULONG: 130813939Slinton al = ALLONG; 130913939Slinton sz = SZLONG; 131013939Slinton break; 131113939Slinton #endif 131213939Slinton 131313939Slinton default: 131413939Slinton if( new < 0 ) { 131513939Slinton uerror( "illegal field type" ); 131613939Slinton al = ALINT; 131713939Slinton } 131813939Slinton else { 131913939Slinton al = fldal( p->stype ); 132013939Slinton sz =SZINT; 132113939Slinton } 132213939Slinton } 132313939Slinton 132413939Slinton if( w > sz ) { 132513939Slinton uerror( "field too big"); 132613939Slinton w = sz; 132713939Slinton } 132813939Slinton 132913939Slinton if( w == 0 ){ /* align only */ 133013939Slinton SETOFF( strucoff, al ); 133113939Slinton if( new >= 0 ) uerror( "zero size field"); 133213939Slinton return(0); 133313939Slinton } 133413939Slinton 133513939Slinton if( strucoff%al + w > sz ) SETOFF( strucoff, al ); 133613939Slinton if( new < 0 ) { 133713939Slinton if( (offsz-strucoff) < w ) 133813939Slinton cerror("structure too large"); 133913939Slinton strucoff += w; /* we know it will fit */ 134013939Slinton return(0); 134113939Slinton } 134213939Slinton 134313939Slinton /* establish the field */ 134413939Slinton 134513939Slinton if( new == 1 ) { /* previous definition */ 134613939Slinton if( p->offset != strucoff || p->sclass != (FIELD|w) ) return(1); 134713939Slinton } 134813939Slinton p->offset = strucoff; 134913939Slinton if( (offsz-strucoff) < w ) cerror("structure too large"); 135013939Slinton strucoff += w; 135113939Slinton p->stype = type; 135213939Slinton fldty( p ); 135313939Slinton return(0); 135413939Slinton } 135513939Slinton 135613939Slinton nidcl( p ) NODE *p; { /* handle unitialized declarations */ 135713939Slinton /* assumed to be not functions */ 135813939Slinton register class; 135913939Slinton register commflag; /* flag for labelled common declarations */ 136013939Slinton 136113939Slinton commflag = 0; 136213939Slinton 136313939Slinton /* compute class */ 136413939Slinton if( (class=curclass) == SNULL ){ 136513939Slinton if( blevel > 1 ) class = AUTO; 136613939Slinton else if( blevel != 0 || instruct ) cerror( "nidcl error" ); 136713939Slinton else { /* blevel = 0 */ 136813939Slinton class = noinit(); 136913939Slinton if( class == EXTERN ) commflag = 1; 137013939Slinton } 137113939Slinton } 137213939Slinton #ifdef LCOMM 137313939Slinton /* hack so stab will come at as LCSYM rather than STSYM */ 137413939Slinton if (class == STATIC) { 137513939Slinton extern int stabLCSYM; 137613939Slinton stabLCSYM = 1; 137713939Slinton } 137813939Slinton #endif 137913939Slinton 138013939Slinton defid( p, class ); 138113939Slinton 138213939Slinton #ifndef LCOMM 138324405Smckusick if( class==EXTDEF || class==STATIC ) 138413939Slinton #else 138513939Slinton if (class==STATIC) { 138613939Slinton register struct symtab *s = &stab[p->tn.rval]; 138713939Slinton extern int stabLCSYM; 138813939Slinton int sz = tsize(s->stype, s->dimoff, s->sizoff)/SZCHAR; 138913939Slinton 139013939Slinton stabLCSYM = 0; 139113939Slinton if (sz % sizeof (int)) 139213939Slinton sz += sizeof (int) - (sz % sizeof (int)); 139313939Slinton if (s->slevel > 1) 139413939Slinton printf(" .lcomm L%d,%d\n", s->offset, sz); 139513939Slinton else 139613939Slinton printf(" .lcomm %s,%d\n", exname(s->sname), sz); 139724405Smckusick }else if (class == EXTDEF) 139813939Slinton #endif 139924405Smckusick { 140013939Slinton /* simulate initialization by 0 */ 140113939Slinton beginit(p->tn.rval); 140213939Slinton endinit(); 140313939Slinton } 140413939Slinton if( commflag ) commdec( p->tn.rval ); 140513939Slinton } 140613939Slinton 140713939Slinton TWORD 140813939Slinton types( t1, t2, t3 ) TWORD t1, t2, t3; { 140913939Slinton /* return a basic type from basic types t1, t2, and t3 */ 141013939Slinton 141113939Slinton TWORD t[3], noun, adj, unsg; 141213939Slinton register i; 141313939Slinton 141413939Slinton t[0] = t1; 141513939Slinton t[1] = t2; 141613939Slinton t[2] = t3; 141713939Slinton 141813939Slinton unsg = INT; /* INT or UNSIGNED */ 141913939Slinton noun = UNDEF; /* INT, CHAR, or FLOAT */ 142013939Slinton adj = INT; /* INT, LONG, or SHORT */ 142113939Slinton 142213939Slinton for( i=0; i<3; ++i ){ 142313939Slinton switch( t[i] ){ 142413939Slinton 142513939Slinton default: 142613939Slinton bad: 142713939Slinton uerror( "illegal type combination" ); 142813939Slinton return( INT ); 142913939Slinton 143013939Slinton case UNDEF: 143113939Slinton continue; 143213939Slinton 143313939Slinton case UNSIGNED: 143413939Slinton if( unsg != INT ) goto bad; 143513939Slinton unsg = UNSIGNED; 143613939Slinton continue; 143713939Slinton 143813939Slinton case LONG: 143913939Slinton case SHORT: 144013939Slinton if( adj != INT ) goto bad; 144113939Slinton adj = t[i]; 144213939Slinton continue; 144313939Slinton 144413939Slinton case INT: 144513939Slinton case CHAR: 144613939Slinton case FLOAT: 144713939Slinton if( noun != UNDEF ) goto bad; 144813939Slinton noun = t[i]; 144913939Slinton continue; 145013939Slinton } 145113939Slinton } 145213939Slinton 145313939Slinton /* now, construct final type */ 145413939Slinton if( noun == UNDEF ) noun = INT; 145513939Slinton else if( noun == FLOAT ){ 145613939Slinton if( unsg != INT || adj == SHORT ) goto bad; 145713939Slinton return( adj==LONG ? DOUBLE : FLOAT ); 145813939Slinton } 145913939Slinton else if( noun == CHAR && adj != INT ) goto bad; 146013939Slinton 146113939Slinton /* now, noun is INT or CHAR */ 146213939Slinton if( adj != INT ) noun = adj; 146313939Slinton if( unsg == UNSIGNED ) return( noun + (UNSIGNED-INT) ); 146413939Slinton else return( noun ); 146513939Slinton } 146613939Slinton 146713939Slinton NODE * 146813939Slinton tymerge( typ, idp ) NODE *typ, *idp; { 146913939Slinton /* merge type typ with identifier idp */ 147013939Slinton 147113939Slinton register unsigned t; 147213939Slinton register i; 147313939Slinton extern int eprint(); 147413939Slinton 147513939Slinton if( typ->in.op != TYPE ) cerror( "tymerge: arg 1" ); 147613939Slinton if(idp == NIL ) return( NIL ); 147713939Slinton 147813939Slinton # ifndef BUG1 147913939Slinton if( ddebug > 2 ) fwalk( idp, eprint, 0 ); 148013939Slinton # endif 148113939Slinton 148213939Slinton idp->in.type = typ->in.type; 148313939Slinton idp->fn.cdim = curdim; 148413939Slinton tyreduce( idp ); 148513939Slinton idp->fn.csiz = typ->fn.csiz; 148613939Slinton 148713939Slinton for( t=typ->in.type, i=typ->fn.cdim; t&TMASK; t = DECREF(t) ){ 148813939Slinton if( ISARY(t) ) dstash( dimtab[i++] ); 148913939Slinton } 149013939Slinton 149113939Slinton /* now idp is a single node: fix up type */ 149213939Slinton 149313939Slinton idp->in.type = ctype( idp->in.type ); 149413939Slinton 149513939Slinton if( (t = BTYPE(idp->in.type)) != STRTY && t != UNIONTY && t != ENUMTY ){ 149613939Slinton idp->fn.csiz = t; /* in case ctype has rewritten things */ 149713939Slinton } 149813939Slinton 149913939Slinton return( idp ); 150013939Slinton } 150113939Slinton 150213939Slinton tyreduce( p ) register NODE *p; { 150313939Slinton 150413939Slinton /* build a type, and stash away dimensions, from a parse tree of the declaration */ 150513939Slinton /* the type is build top down, the dimensions bottom up */ 150613939Slinton register o, temp; 150713939Slinton register unsigned t; 150813939Slinton 150913939Slinton o = p->in.op; 151013939Slinton p->in.op = FREE; 151113939Slinton 151213939Slinton if( o == NAME ) return; 151313939Slinton 151413939Slinton t = INCREF( p->in.type ); 151513939Slinton if( o == UNARY CALL ) t += (FTN-PTR); 151613939Slinton else if( o == LB ){ 151713939Slinton t += (ARY-PTR); 151813939Slinton temp = p->in.right->tn.lval; 151913939Slinton p->in.right->in.op = FREE; 152013939Slinton if( ( temp == 0 ) & ( p->in.left->tn.op == LB ) ) 152113939Slinton uerror( "Null dimension" ); 152213939Slinton } 152313939Slinton 152413939Slinton p->in.left->in.type = t; 152513939Slinton tyreduce( p->in.left ); 152613939Slinton 152713939Slinton if( o == LB ) dstash( temp ); 152813939Slinton 152913939Slinton p->tn.rval = p->in.left->tn.rval; 153013939Slinton p->in.type = p->in.left->in.type; 153113939Slinton 153213939Slinton } 153313939Slinton 153413939Slinton fixtype( p, class ) register NODE *p; { 153513939Slinton register unsigned t, type; 153613939Slinton register mod1, mod2; 153713939Slinton /* fix up the types, and check for legality */ 153813939Slinton 153913939Slinton if( (type = p->in.type) == UNDEF ) return; 154013939Slinton if( mod2 = (type&TMASK) ){ 154113939Slinton t = DECREF(type); 154213939Slinton while( mod1=mod2, mod2 = (t&TMASK) ){ 154313939Slinton if( mod1 == ARY && mod2 == FTN ){ 154413939Slinton uerror( "array of functions is illegal" ); 154513939Slinton type = 0; 154613939Slinton } 154713939Slinton else if( mod1 == FTN && ( mod2 == ARY || mod2 == FTN ) ){ 154813939Slinton uerror( "function returns illegal type" ); 154913939Slinton type = 0; 155013939Slinton } 155113939Slinton t = DECREF(t); 155213939Slinton } 155313939Slinton } 155413939Slinton 155513939Slinton /* detect function arguments, watching out for structure declarations */ 155613939Slinton /* for example, beware of f(x) struct [ int a[10]; } *x; { ... } */ 155713939Slinton /* the danger is that "a" will be converted to a pointer */ 155813939Slinton 155913939Slinton if( class==SNULL && blevel==1 && !(instruct&(INSTRUCT|INUNION)) ) class = PARAM; 156013939Slinton if( class == PARAM || ( class==REGISTER && blevel==1 ) ){ 156113939Slinton if( type == FLOAT ) type = DOUBLE; 156213939Slinton else if( ISARY(type) ){ 156313939Slinton ++p->fn.cdim; 156413939Slinton type += (PTR-ARY); 156513939Slinton } 156613939Slinton else if( ISFTN(type) ){ 156713939Slinton werror( "a function is declared as an argument" ); 156813939Slinton type = INCREF(type); 156913939Slinton } 157013939Slinton 157113939Slinton } 157213939Slinton 157313939Slinton if( instruct && ISFTN(type) ){ 157413939Slinton uerror( "function illegal in structure or union" ); 157513939Slinton type = INCREF(type); 157613939Slinton } 157713939Slinton p->in.type = type; 157813939Slinton } 157913939Slinton 158013939Slinton uclass( class ) register class; { 158113939Slinton /* give undefined version of class */ 158213939Slinton if( class == SNULL ) return( EXTERN ); 158313939Slinton else if( class == STATIC ) return( USTATIC ); 158413939Slinton else if( class == FORTRAN ) return( UFORTRAN ); 158513939Slinton else return( class ); 158613939Slinton } 158713939Slinton 158813939Slinton fixclass( class, type ) TWORD type; { 158913939Slinton 159013939Slinton /* first, fix null class */ 159113939Slinton 159213939Slinton if( class == SNULL ){ 159313939Slinton if( instruct&INSTRUCT ) class = MOS; 159413939Slinton else if( instruct&INUNION ) class = MOU; 159513939Slinton else if( blevel == 0 ) class = EXTDEF; 159613939Slinton else if( blevel == 1 ) class = PARAM; 159713939Slinton else class = AUTO; 159813939Slinton 159913939Slinton } 160013939Slinton 160113939Slinton /* now, do general checking */ 160213939Slinton 160313939Slinton if( ISFTN( type ) ){ 160413939Slinton switch( class ) { 160513939Slinton default: 160613939Slinton uerror( "function has illegal storage class" ); 160713939Slinton case AUTO: 160813939Slinton class = EXTERN; 160913939Slinton case EXTERN: 161013939Slinton case EXTDEF: 161113939Slinton case FORTRAN: 161213939Slinton case TYPEDEF: 161313939Slinton case STATIC: 161413939Slinton case UFORTRAN: 161513939Slinton case USTATIC: 161613939Slinton ; 161713939Slinton } 161813939Slinton } 161913939Slinton 162013939Slinton if( class&FIELD ){ 162113939Slinton if( !(instruct&INSTRUCT) ) uerror( "illegal use of field" ); 162213939Slinton return( class ); 162313939Slinton } 162413939Slinton 162513939Slinton switch( class ){ 162613939Slinton 162713939Slinton case MOU: 162813939Slinton if( !(instruct&INUNION) ) uerror( "illegal class" ); 162913939Slinton return( class ); 163013939Slinton 163113939Slinton case MOS: 163213939Slinton if( !(instruct&INSTRUCT) ) uerror( "illegal class" ); 163313939Slinton return( class ); 163413939Slinton 163513939Slinton case MOE: 163613939Slinton if( instruct & (INSTRUCT|INUNION) ) uerror( "illegal class" ); 163713939Slinton return( class ); 163813939Slinton 163913939Slinton case REGISTER: 164013939Slinton if( blevel == 0 ) uerror( "illegal register declaration" ); 164113939Slinton else if( regvar >= MINRVAR && cisreg( type ) ) return( class ); 164213939Slinton if( blevel == 1 ) return( PARAM ); 164313939Slinton else return( AUTO ); 164413939Slinton 164513939Slinton case AUTO: 164613939Slinton case LABEL: 164713939Slinton case ULABEL: 164813939Slinton if( blevel < 2 ) uerror( "illegal class" ); 164913939Slinton return( class ); 165013939Slinton 165113939Slinton case PARAM: 165213939Slinton if( blevel != 1 ) uerror( "illegal class" ); 165313939Slinton return( class ); 165413939Slinton 165513939Slinton case UFORTRAN: 165613939Slinton case FORTRAN: 165713939Slinton # ifdef NOFORTRAN 165813939Slinton NOFORTRAN; /* a condition which can regulate the FORTRAN usage */ 165913939Slinton # endif 166013939Slinton if( !ISFTN(type) ) uerror( "fortran declaration must apply to function" ); 166113939Slinton else { 166213939Slinton type = DECREF(type); 166313939Slinton if( ISFTN(type) || ISARY(type) || ISPTR(type) ) { 166413939Slinton uerror( "fortran function has wrong type" ); 166513939Slinton } 166613939Slinton } 166713939Slinton case EXTERN: 166813939Slinton case STATIC: 166913939Slinton case EXTDEF: 167013939Slinton case TYPEDEF: 167113939Slinton case USTATIC: 167217151Sralph if( blevel == 1 ){ 167317151Sralph uerror( "illegal class" ); 167417151Sralph return( PARAM ); 167517151Sralph } 167617151Sralph case STNAME: 167717151Sralph case UNAME: 167817151Sralph case ENAME: 167913939Slinton return( class ); 168013939Slinton 168113939Slinton default: 168213939Slinton cerror( "illegal class: %d", class ); 168313939Slinton /* NOTREACHED */ 168413939Slinton 168513939Slinton } 168613939Slinton } 168713939Slinton 168813939Slinton struct symtab * 168913939Slinton mknonuniq(idindex) int *idindex; {/* locate a symbol table entry for */ 169013939Slinton /* an occurrence of a nonunique structure member name */ 169113939Slinton /* or field */ 169213939Slinton register i; 169313939Slinton register struct symtab * sp; 169413939Slinton char *p,*q; 169513939Slinton 169613939Slinton sp = & stab[ i= *idindex ]; /* position search at old entry */ 169713939Slinton while( sp->stype != TNULL ){ /* locate unused entry */ 169813939Slinton if( ++i >= SYMTSZ ){/* wrap around symbol table */ 169913939Slinton i = 0; 170013939Slinton sp = stab; 170113939Slinton } 170213939Slinton else ++sp; 170313939Slinton if( i == *idindex ) cerror("Symbol table full"); 170413939Slinton } 170513939Slinton sp->sflags = SNONUNIQ | SMOS; 170613939Slinton p = sp->sname; 170713939Slinton q = stab[*idindex].sname; /* old entry name */ 170813939Slinton #ifdef FLEXNAMES 170913939Slinton sp->sname = stab[*idindex].sname; 171013939Slinton #endif 171113939Slinton # ifndef BUG1 171213939Slinton if( ddebug ){ 171313939Slinton printf("\tnonunique entry for %s from %d to %d\n", 171413939Slinton q, *idindex, i ); 171513939Slinton } 171613939Slinton # endif 171713939Slinton *idindex = i; 171813939Slinton #ifndef FLEXNAMES 171913939Slinton for( i=1; i<=NCHNAM; ++i ){ /* copy name */ 172013939Slinton if( *p++ = *q /* assign */ ) ++q; 172113939Slinton } 172213939Slinton #endif 172313939Slinton return ( sp ); 172413939Slinton } 172513939Slinton 172613939Slinton lookup( name, s) char *name; { 172713939Slinton /* look up name: must agree with s w.r.t. STAG, SMOS and SHIDDEN */ 172813939Slinton 172913939Slinton register char *p, *q; 173013939Slinton int i, j, ii; 173113939Slinton register struct symtab *sp; 173213939Slinton 173313939Slinton /* compute initial hash index */ 173413939Slinton # ifndef BUG1 173513939Slinton if( ddebug > 2 ){ 173613939Slinton printf( "lookup( %s, %d ), stwart=%d, instruct=%d\n", name, s, stwart, instruct ); 173713939Slinton } 173813939Slinton # endif 173913939Slinton 174013939Slinton i = 0; 174113939Slinton #ifndef FLEXNAMES 174213939Slinton for( p=name, j=0; *p != '\0'; ++p ){ 174313939Slinton i += *p; 174413939Slinton if( ++j >= NCHNAM ) break; 174513939Slinton } 174613939Slinton #else 174713939Slinton i = (int)name; 174813939Slinton #endif 174913939Slinton i = i%SYMTSZ; 175013939Slinton sp = &stab[ii=i]; 175113939Slinton 175213939Slinton for(;;){ /* look for name */ 175313939Slinton 175413939Slinton if( sp->stype == TNULL ){ /* empty slot */ 175513939Slinton sp->sflags = s; /* set STAG, SMOS if needed, turn off all others */ 175613939Slinton #ifndef FLEXNAMES 175713939Slinton p = sp->sname; 175813939Slinton for( j=0; j<NCHNAM; ++j ) if( *p++ = *name ) ++name; 175913939Slinton #else 176013939Slinton sp->sname = name; 176113939Slinton #endif 176213939Slinton sp->stype = UNDEF; 176313939Slinton sp->sclass = SNULL; 176413939Slinton return( i ); 176513939Slinton } 176613939Slinton if( (sp->sflags & (STAG|SMOS|SHIDDEN)) != s ) goto next; 176713939Slinton p = sp->sname; 176813939Slinton q = name; 176913939Slinton #ifndef FLEXNAMES 177013939Slinton for( j=0; j<NCHNAM;++j ){ 177113939Slinton if( *p++ != *q ) goto next; 177213939Slinton if( !*q++ ) break; 177313939Slinton } 177413939Slinton return( i ); 177513939Slinton #else 177613939Slinton if (p == q) 177713939Slinton return ( i ); 177813939Slinton #endif 177913939Slinton next: 178013939Slinton if( ++i >= SYMTSZ ){ 178113939Slinton i = 0; 178213939Slinton sp = stab; 178313939Slinton } 178413939Slinton else ++sp; 178513939Slinton if( i == ii ) cerror( "symbol table full" ); 178613939Slinton } 178713939Slinton } 178813939Slinton 178913939Slinton #ifndef checkst 179013939Slinton /* if not debugging, make checkst a macro */ 179113939Slinton checkst(lev){ 179213939Slinton register int s, i, j; 179313939Slinton register struct symtab *p, *q; 179413939Slinton 179513939Slinton for( i=0, p=stab; i<SYMTSZ; ++i, ++p ){ 179613939Slinton if( p->stype == TNULL ) continue; 179713939Slinton j = lookup( p->sname, p->sflags&(SMOS|STAG) ); 179813939Slinton if( j != i ){ 179913939Slinton q = &stab[j]; 180013939Slinton if( q->stype == UNDEF || 180113939Slinton q->slevel <= p->slevel ){ 180213939Slinton #ifndef FLEXNAMES 180313939Slinton cerror( "check error: %.8s", q->sname ); 180413939Slinton #else 180513939Slinton cerror( "check error: %s", q->sname ); 180613939Slinton #endif 180713939Slinton } 180813939Slinton } 180913939Slinton #ifndef FLEXNAMES 181013939Slinton else if( p->slevel > lev ) cerror( "%.8s check at level %d", p->sname, lev ); 181113939Slinton #else 181213939Slinton else if( p->slevel > lev ) cerror( "%s check at level %d", p->sname, lev ); 181313939Slinton #endif 181413939Slinton } 181513939Slinton } 181613939Slinton #endif 181713939Slinton 181813939Slinton struct symtab * 181913939Slinton relook(p) register struct symtab *p; { /* look up p again, and see where it lies */ 182013939Slinton 182113939Slinton register struct symtab *q; 182213939Slinton 182313939Slinton /* I'm not sure that this handles towers of several hidden definitions in all cases */ 182413939Slinton q = &stab[lookup( p->sname, p->sflags&(STAG|SMOS|SHIDDEN) )]; 182513939Slinton /* make relook always point to either p or an empty cell */ 182613939Slinton if( q->stype == UNDEF ){ 182713939Slinton q->stype = TNULL; 182813939Slinton return(q); 182913939Slinton } 183013939Slinton while( q != p ){ 183113939Slinton if( q->stype == TNULL ) break; 183213939Slinton if( ++q >= &stab[SYMTSZ] ) q=stab; 183313939Slinton } 183413939Slinton return(q); 183513939Slinton } 183613939Slinton 183724405Smckusick clearst( lev ) register int lev; { 183824405Smckusick register struct symtab *p, *q; 183924405Smckusick register int temp; 184024405Smckusick struct symtab *clist = 0; 184113939Slinton 184213939Slinton temp = lineno; 184313939Slinton aobeg(); 184413939Slinton 184524405Smckusick /* step 1: remove entries */ 184624405Smckusick while( chaintop-1 > lev ){ 184724405Smckusick register int type; 184813939Slinton 184924405Smckusick p = schain[--chaintop]; 185024405Smckusick schain[chaintop] = 0; 185124405Smckusick for( ; p; p = q ){ 185224405Smckusick q = p->snext; 185324405Smckusick type = p->stype; 185424405Smckusick if( p->stype == TNULL || p->slevel <= lev ) 185524405Smckusick cerror( "schain botch" ); 185624405Smckusick lineno = p->suse < 0 ? -p->suse : p->suse; 185724405Smckusick if( p->stype==UNDEF || ( p->sclass==ULABEL && lev<2 ) ){ 185813939Slinton lineno = temp; 185913939Slinton #ifndef FLEXNAMES 186013939Slinton uerror( "%.8s undefined", p->sname ); 186113939Slinton #else 186213939Slinton uerror( "%s undefined", p->sname ); 186313939Slinton #endif 186413939Slinton } 186513939Slinton else aocode(p); 186613939Slinton # ifndef BUG1 186724405Smckusick if( ddebug ){ 186813939Slinton #ifndef FLEXNAMES 186924405Smckusick printf( "removing %.8s", p->sname ); 187013939Slinton #else 187124405Smckusick printf( "removing %s", p->sname ); 187213939Slinton #endif 187324405Smckusick printf( " from stab[%d], flags %o level %d\n", 187424405Smckusick p-stab, p->sflags, p->slevel); 187524405Smckusick } 187613939Slinton # endif 187724405Smckusick if( p->sflags & SHIDES )unhide( p ); 187813939Slinton p->stype = TNULL; 187924405Smckusick p->snext = clist; 188024405Smckusick clist = p; 188113939Slinton } 188224405Smckusick } 188324405Smckusick 188424405Smckusick /* step 2: fix any mishashed entries */ 188524405Smckusick p = clist; 188624405Smckusick while( p ){ 188724405Smckusick register struct symtab *r; 188824405Smckusick 188924405Smckusick q = p; 189024405Smckusick for(;;){ 189124405Smckusick if( ++q >= &stab[SYMTSZ] )q = stab; 189224405Smckusick if( q == p || q->stype == TNULL )break; 189324405Smckusick if( (r = relook(q)) != q ) { 189424405Smckusick *r = *q; 189524405Smckusick q->stype = NULL; 189613939Slinton } 189713939Slinton } 189824405Smckusick p = p->snext; 189913939Slinton } 190024405Smckusick 190113939Slinton lineno = temp; 190213939Slinton aoend(); 190313939Slinton } 190413939Slinton 190513939Slinton hide( p ) register struct symtab *p; { 190613939Slinton register struct symtab *q; 190713939Slinton for( q=p+1; ; ++q ){ 190813939Slinton if( q >= &stab[SYMTSZ] ) q = stab; 190913939Slinton if( q == p ) cerror( "symbol table full" ); 191013939Slinton if( q->stype == TNULL ) break; 191113939Slinton } 191224405Smckusick *q = *p; 191313939Slinton p->sflags |= SHIDDEN; 191413939Slinton q->sflags = (p->sflags&(SMOS|STAG)) | SHIDES; 191513939Slinton #ifndef FLEXNAMES 191613939Slinton if( hflag ) werror( "%.8s redefinition hides earlier one", p->sname ); 191713939Slinton #else 191813939Slinton if( hflag ) werror( "%s redefinition hides earlier one", p->sname ); 191913939Slinton #endif 192013939Slinton # ifndef BUG1 192113939Slinton if( ddebug ) printf( " %d hidden in %d\n", p-stab, q-stab ); 192213939Slinton # endif 192313939Slinton return( idname = q-stab ); 192413939Slinton } 192513939Slinton 192613939Slinton unhide( p ) register struct symtab *p; { 192713939Slinton register struct symtab *q; 192813939Slinton register s, j; 192913939Slinton 193013939Slinton s = p->sflags & (SMOS|STAG); 193113939Slinton q = p; 193213939Slinton 193313939Slinton for(;;){ 193413939Slinton 193513939Slinton if( q == stab ) q = &stab[SYMTSZ-1]; 193613939Slinton else --q; 193713939Slinton 193813939Slinton if( q == p ) break; 193913939Slinton 194013939Slinton if( (q->sflags&(SMOS|STAG)) == s ){ 194113939Slinton #ifndef FLEXNAMES 194213939Slinton for( j =0; j<NCHNAM; ++j ) if( p->sname[j] != q->sname[j] ) break; 194313939Slinton if( j == NCHNAM ){ /* found the name */ 194413939Slinton #else 194513939Slinton if (p->sname == q->sname) { 194613939Slinton #endif 194713939Slinton q->sflags &= ~SHIDDEN; 194813939Slinton # ifndef BUG1 194913939Slinton if( ddebug ) printf( "unhide uncovered %d from %d\n", q-stab,p-stab); 195013939Slinton # endif 195113939Slinton return; 195213939Slinton } 195313939Slinton } 195413939Slinton 195513939Slinton } 195613939Slinton cerror( "unhide fails" ); 195713939Slinton } 1958