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