1 #ifndef lint 2 static char *sccsid ="@(#)pftn.c 1.20 (Berkeley) 12/10/87"; 3 #endif lint 4 5 # include "pass1.h" 6 7 unsigned int offsz; 8 9 struct symtab *schain[MAXSCOPES]; /* sym chains for clearst */ 10 int chaintop; /* highest active entry */ 11 12 struct instk { 13 int in_sz; /* size of array element */ 14 int in_x; /* current index for structure member in structure initializations */ 15 int in_n; /* number of initializations seen */ 16 int in_s; /* sizoff */ 17 int in_d; /* dimoff */ 18 TWORD in_t; /* type */ 19 int in_id; /* stab index */ 20 int in_fl; /* flag which says if this level is controlled by {} */ 21 OFFSZ in_off; /* offset of the beginning of this level */ 22 } 23 instack[10], 24 *pstk; 25 26 /* defines used for getting things off of the initialization stack */ 27 28 29 struct symtab *relook(); 30 31 32 int ddebug = 0; 33 34 struct symtab * mknonuniq(); 35 36 defid( q, class ) register NODE *q; register int class; { 37 register struct symtab *p; 38 int idp; 39 register TWORD type; 40 TWORD stp; 41 register int scl; 42 int dsym, ddef; 43 int slev, temp; 44 int changed; 45 46 if( q == NIL ) return; /* an error was detected */ 47 48 if( q < node || q >= &node[TREESZ] ) cerror( "defid call" ); 49 50 idp = q->tn.rval; 51 52 if( idp < 0 ) cerror( "tyreduce" ); 53 p = &stab[idp]; 54 55 # ifndef BUG1 56 if( ddebug ){ 57 #ifndef FLEXNAMES 58 printf( "defid( %.8s (%d), ", p->sname, idp ); 59 #else 60 printf( "defid( %s (%d), ", p->sname, idp ); 61 #endif 62 tprint( q->in.type ); 63 printf( ", %s, (%d,%d) ), level %d\n", scnames(class), q->fn.cdim, q->fn.csiz, blevel ); 64 } 65 # endif 66 67 fixtype( q, class ); 68 69 type = q->in.type; 70 class = fixclass( class, type ); 71 72 stp = p->stype; 73 slev = p->slevel; 74 75 # ifndef BUG1 76 if( ddebug ){ 77 printf( " modified to " ); 78 tprint( type ); 79 printf( ", %s\n", scnames(class) ); 80 printf( " previous def'n: " ); 81 tprint( stp ); 82 printf( ", %s, (%d,%d) ), level %d\n", scnames(p->sclass), p->dimoff, p->sizoff, slev ); 83 } 84 # endif 85 86 if( stp == FTN && p->sclass == SNULL )goto enter; 87 if( blevel==1 && stp!=FARG ) switch( class ){ 88 89 default: 90 #ifndef FLEXNAMES 91 if(!(class&FIELD)) uerror( "declared argument %.8s is missing", p->sname ); 92 #else 93 if(!(class&FIELD)) uerror( "declared argument %s is missing", p->sname ); 94 #endif 95 case MOS: 96 case STNAME: 97 case MOU: 98 case UNAME: 99 case MOE: 100 case ENAME: 101 case TYPEDEF: 102 ; 103 } 104 if( stp == UNDEF|| stp == FARG ) goto enter; 105 106 if( type != stp ) goto mismatch; 107 /* test (and possibly adjust) dimensions */ 108 dsym = p->dimoff; 109 ddef = q->fn.cdim; 110 changed = 0; 111 for( temp=type; temp&TMASK; temp = DECREF(temp) ){ 112 if( ISARY(temp) ){ 113 if (dimtab[dsym] == 0) { 114 dimtab[dsym] = dimtab[ddef]; 115 changed = 1; 116 } 117 else if (dimtab[ddef]!=0&&dimtab[dsym]!=dimtab[ddef]) { 118 goto mismatch; 119 } 120 ++dsym; 121 ++ddef; 122 } 123 } 124 125 if (changed) { 126 FIXDEF(p); 127 } 128 129 /* check that redeclarations are to the same structure */ 130 if( (temp==STRTY||temp==UNIONTY||temp==ENUMTY) && p->sizoff != q->fn.csiz 131 && class!=STNAME && class!=UNAME && class!=ENAME ){ 132 goto mismatch; 133 } 134 135 scl = ( p->sclass ); 136 137 # ifndef BUG1 138 if( ddebug ){ 139 printf( " previous class: %s\n", scnames(scl) ); 140 } 141 # endif 142 143 if( class&FIELD ){ 144 /* redefinition */ 145 if( !falloc( p, class&FLDSIZ, 1, NIL ) ) { 146 /* successful allocation */ 147 psave( idp ); 148 return; 149 } 150 /* blew it: resume at end of switch... */ 151 } 152 153 else switch( class ){ 154 155 case EXTERN: 156 switch( scl ){ 157 case STATIC: 158 case USTATIC: 159 if( slev==0 ) return; 160 break; 161 case EXTDEF: 162 case EXTERN: 163 case FORTRAN: 164 case UFORTRAN: 165 return; 166 } 167 break; 168 169 case STATIC: 170 if( scl==USTATIC || (scl==EXTERN && blevel==0) ){ 171 p->sclass = STATIC; 172 if( ISFTN(type) ) curftn = idp; 173 return; 174 } 175 break; 176 177 case USTATIC: 178 if( scl==STATIC || scl==USTATIC ) return; 179 break; 180 181 case LABEL: 182 if( scl == ULABEL ){ 183 p->sclass = LABEL; 184 deflab( p->offset ); 185 return; 186 } 187 break; 188 189 case TYPEDEF: 190 if( scl == class ) return; 191 break; 192 193 case UFORTRAN: 194 if( scl == UFORTRAN || scl == FORTRAN ) return; 195 break; 196 197 case FORTRAN: 198 if( scl == UFORTRAN ){ 199 p->sclass = FORTRAN; 200 if( ISFTN(type) ) curftn = idp; 201 return; 202 } 203 break; 204 205 case MOU: 206 case MOS: 207 if( scl == class ) { 208 if( oalloc( p, &strucoff ) ) break; 209 if( class == MOU ) strucoff = 0; 210 psave( idp ); 211 return; 212 } 213 break; 214 215 case MOE: 216 if( scl == class ){ 217 if( p->offset!= strucoff++ ) break; 218 psave( idp ); 219 } 220 break; 221 222 case EXTDEF: 223 if( scl == EXTERN ) { 224 p->sclass = EXTDEF; 225 if( ISFTN(type) ) curftn = idp; 226 return; 227 } 228 break; 229 230 case STNAME: 231 case UNAME: 232 case ENAME: 233 if( scl != class ) break; 234 if( dimtab[p->sizoff] == 0 ) return; /* previous entry just a mention */ 235 break; 236 237 case ULABEL: 238 if( scl == LABEL || scl == ULABEL ) return; 239 case PARAM: 240 case AUTO: 241 case REGISTER: 242 ; /* mismatch.. */ 243 244 } 245 246 mismatch: 247 /* allow nonunique structure/union member names */ 248 249 if( class==MOU || class==MOS || class & FIELD ){/* make a new entry */ 250 register int *memp; 251 p->sflags |= SNONUNIQ; /* old entry is nonunique */ 252 /* determine if name has occurred in this structure/union */ 253 if (paramno > 0) for( memp = ¶mstk[paramno-1]; 254 /* while */ *memp>=0 && stab[*memp].sclass != STNAME 255 && stab[*memp].sclass != UNAME; 256 /* iterate */ --memp){ char *cname, *oname; 257 if( stab[*memp].sflags & SNONUNIQ ){ 258 cname=p->sname; 259 oname=stab[*memp].sname; 260 #ifndef FLEXNAMES 261 for(temp=1; temp<=NCHNAM; ++temp){ 262 if(*cname++ != *oname)goto diff; 263 if(!*oname++)break; 264 } 265 #else 266 if (cname != oname) goto diff; 267 #endif 268 uerror("redeclaration of: %s",p->sname); 269 break; 270 diff: continue; 271 } 272 } 273 p = mknonuniq( &idp ); /* update p and idp to new entry */ 274 goto enter; 275 } 276 if( blevel > slev && class != EXTERN && class != FORTRAN && 277 class != UFORTRAN && !( class == LABEL && slev >= 2 ) ){ 278 q->tn.rval = idp = hide( p ); 279 p = &stab[idp]; 280 goto enter; 281 } 282 #ifndef FLEXNAMES 283 uerror( "redeclaration of %.8s", p->sname ); 284 #else 285 uerror( "redeclaration of %s", p->sname ); 286 #endif 287 if( class==EXTDEF && ISFTN(type) ) curftn = idp; 288 return; 289 290 enter: /* make a new entry */ 291 292 # ifndef BUG1 293 if( ddebug ) printf( " new entry made\n" ); 294 # endif 295 if( type == UNDEF ) uerror("void type for %s",p->sname); 296 p->stype = type; 297 p->sclass = class; 298 p->slevel = blevel; 299 p->offset = NOOFFSET; 300 p->suse = lineno; 301 if( class == STNAME || class == UNAME || class == ENAME ) { 302 p->sizoff = curdim; 303 dstash( 0 ); /* size */ 304 dstash( -1 ); /* index to members of str or union */ 305 dstash( ALSTRUCT ); /* alignment */ 306 dstash( idp ); 307 } 308 else { 309 switch( BTYPE(type) ){ 310 case STRTY: 311 case UNIONTY: 312 case ENUMTY: 313 p->sizoff = q->fn.csiz; 314 break; 315 default: 316 p->sizoff = BTYPE(type); 317 } 318 } 319 320 /* copy dimensions */ 321 322 p->dimoff = q->fn.cdim; 323 324 /* allocate offsets */ 325 if( class&FIELD ){ 326 (void) falloc( p, class&FLDSIZ, 0, NIL ); /* new entry */ 327 psave( idp ); 328 } 329 else switch( class ){ 330 331 case AUTO: 332 (void) oalloc( p, &autooff ); 333 break; 334 case STATIC: 335 case EXTDEF: 336 p->offset = getlab(); 337 if( ISFTN(type) ) curftn = idp; 338 break; 339 case ULABEL: 340 case LABEL: 341 p->offset = getlab(); 342 p->slevel = 2; 343 if( class == LABEL ){ 344 (void) locctr( PROG ); 345 deflab( p->offset ); 346 } 347 break; 348 349 case EXTERN: 350 case UFORTRAN: 351 case FORTRAN: 352 p->offset = getlab(); 353 p->slevel = 0; 354 break; 355 case MOU: 356 case MOS: 357 (void) oalloc( p, &strucoff ); 358 if( class == MOU ) strucoff = 0; 359 psave( idp ); 360 break; 361 362 case MOE: 363 p->offset = strucoff++; 364 psave( idp ); 365 break; 366 case REGISTER: 367 p->offset = regvar--; 368 if( blevel == 1 ) p->sflags |= SSET; 369 if( regvar < minrvar ) minrvar = regvar; 370 break; 371 } 372 373 { 374 register int l = p->slevel; 375 376 if( l >= MAXSCOPES ) 377 cerror( "scopes nested too deep" ); 378 379 p->snext = schain[l]; 380 schain[l] = p; 381 if( l >= chaintop ) 382 chaintop = l + 1; 383 } 384 385 /* user-supplied routine to fix up new definitions */ 386 387 FIXDEF(p); 388 389 # ifndef BUG1 390 if( ddebug ) printf( " dimoff, sizoff, offset: %d, %d, %d\n", p->dimoff, p->sizoff, p->offset ); 391 # endif 392 393 } 394 395 psave( i ){ 396 if( paramno >= PARAMSZ ){ 397 cerror( "parameter stack overflow"); 398 } 399 paramstk[ paramno++ ] = i; 400 } 401 402 ftnend(){ /* end of function */ 403 if( retlab != NOLAB && nerrors == 0 ){ /* inside a real function */ 404 efcode(); 405 } 406 checkst(0); 407 retstat = 0; 408 tcheck(); 409 curclass = SNULL; 410 brklab = contlab = retlab = NOLAB; 411 flostat = 0; 412 if( nerrors == 0 ){ 413 if( psavbc != & asavbc[0] ) cerror("bcsave error"); 414 if( paramno != 0 ) cerror("parameter reset error"); 415 if( swx != 0 ) cerror( "switch error"); 416 } 417 psavbc = &asavbc[0]; 418 paramno = 0; 419 autooff = AUTOINIT; 420 minrvar = regvar = MAXRVAR; 421 reached = 1; 422 swx = 0; 423 swp = swtab; 424 (void) locctr(DATA); 425 } 426 427 dclargs(){ 428 register i, j; 429 register struct symtab *p; 430 register NODE *q; 431 argoff = ARGINIT; 432 # ifndef BUG1 433 if( ddebug > 2) printf("dclargs()\n"); 434 # endif 435 for( i=0; i<paramno; ++i ){ 436 if( (j = paramstk[i]) < 0 ) continue; 437 p = &stab[j]; 438 # ifndef BUG1 439 if( ddebug > 2 ){ 440 printf("\t%s (%d) ",p->sname, j); 441 tprint(p->stype); 442 printf("\n"); 443 } 444 # endif 445 if( p->stype == FARG ) { 446 q = block(FREE,NIL,NIL,INT,0,INT); 447 q->tn.rval = j; 448 defid( q, PARAM ); 449 } 450 FIXARG(p); /* local arg hook, eg. for sym. debugger */ 451 oalloc( p, &argoff ); /* always set aside space, even for register arguments */ 452 } 453 cendarg(); 454 (void) locctr(PROG); 455 defalign(ALINT); 456 ftnno = getlab(); 457 bfcode( paramstk, paramno ); 458 paramno = 0; 459 } 460 461 NODE * 462 rstruct( idn, soru ){ /* reference to a structure or union, with no definition */ 463 register struct symtab *p; 464 register NODE *q; 465 p = &stab[idn]; 466 switch( p->stype ){ 467 468 case UNDEF: 469 def: 470 q = block( FREE, NIL, NIL, 0, 0, 0 ); 471 q->tn.rval = idn; 472 q->in.type = (soru&INSTRUCT) ? STRTY : ( (soru&INUNION) ? UNIONTY : ENUMTY ); 473 defid( q, (soru&INSTRUCT) ? STNAME : ( (soru&INUNION) ? UNAME : ENAME ) ); 474 break; 475 476 case STRTY: 477 if( soru & INSTRUCT ) break; 478 goto def; 479 480 case UNIONTY: 481 if( soru & INUNION ) break; 482 goto def; 483 484 case ENUMTY: 485 if( !(soru&(INUNION|INSTRUCT)) ) break; 486 goto def; 487 488 } 489 stwart = instruct; 490 return( mkty( p->stype, 0, p->sizoff ) ); 491 } 492 493 moedef( idn ){ 494 register NODE *q; 495 496 q = block( FREE, NIL, NIL, MOETY, 0, 0 ); 497 q->tn.rval = idn; 498 if( idn>=0 ) defid( q, MOE ); 499 } 500 501 bstruct( idn, soru ){ /* begining of structure or union declaration */ 502 register NODE *q; 503 504 psave( instruct ); 505 psave( curclass ); 506 psave( strucoff ); 507 strucoff = 0; 508 instruct = soru; 509 q = block( FREE, NIL, NIL, 0, 0, 0 ); 510 q->tn.rval = idn; 511 if( instruct==INSTRUCT ){ 512 curclass = MOS; 513 q->in.type = STRTY; 514 if( idn >= 0 ) defid( q, STNAME ); 515 } 516 else if( instruct == INUNION ) { 517 curclass = MOU; 518 q->in.type = UNIONTY; 519 if( idn >= 0 ) defid( q, UNAME ); 520 } 521 else { /* enum */ 522 curclass = MOE; 523 q->in.type = ENUMTY; 524 if( idn >= 0 ) defid( q, ENAME ); 525 } 526 psave( idn = q->tn.rval ); 527 /* the "real" definition is where the members are seen */ 528 if ( idn >= 0 ) stab[idn].suse = lineno; 529 return( paramno-4 ); 530 } 531 532 NODE * 533 dclstruct( oparam ){ 534 register struct symtab *p; 535 register i, al, sa, j, sz, szindex; 536 register TWORD temp; 537 register high, low; 538 539 /* paramstk contains: 540 paramstk[ oparam ] = previous instruct 541 paramstk[ oparam+1 ] = previous class 542 paramstk[ oparam+2 ] = previous strucoff 543 paramstk[ oparam+3 ] = structure name 544 545 paramstk[ oparam+4, ... ] = member stab indices 546 547 */ 548 549 550 if( (i=paramstk[oparam+3]) < 0 ){ 551 szindex = curdim; 552 dstash( 0 ); /* size */ 553 dstash( -1 ); /* index to member names */ 554 dstash( ALSTRUCT ); /* alignment */ 555 dstash( -lineno ); /* name of structure */ 556 } 557 else { 558 szindex = stab[i].sizoff; 559 } 560 561 # ifndef BUG1 562 if( ddebug ){ 563 #ifndef FLEXNAMES 564 printf( "dclstruct( %.8s ), szindex = %d\n", (i>=0)? stab[i].sname : "??", szindex ); 565 #else 566 printf( "dclstruct( %s ), szindex = %d\n", (i>=0)? stab[i].sname : "??", szindex ); 567 #endif 568 } 569 # endif 570 temp = (instruct&INSTRUCT)?STRTY:((instruct&INUNION)?UNIONTY:ENUMTY); 571 stwart = instruct = paramstk[ oparam ]; 572 curclass = paramstk[ oparam+1 ]; 573 dimtab[ szindex+1 ] = curdim; 574 al = ALSTRUCT; 575 576 high = low = 0; 577 578 for( i = oparam+4; i< paramno; ++i ){ 579 dstash( j=paramstk[i] ); 580 if( j<0 || j>= SYMTSZ ) cerror( "gummy structure member" ); 581 p = &stab[j]; 582 if( temp == ENUMTY ){ 583 if( p->offset < low ) low = p->offset; 584 if( p->offset > high ) high = p->offset; 585 p->sizoff = szindex; 586 continue; 587 } 588 sa = talign( p->stype, p->sizoff ); 589 if( p->sclass & FIELD ){ 590 sz = p->sclass&FLDSIZ; 591 } 592 else { 593 sz = tsize( p->stype, p->dimoff, p->sizoff ); 594 } 595 if( sz == 0 ){ 596 #ifndef FLEXNAMES 597 werror( "illegal zero sized structure member: %.8s", p->sname ); 598 #else 599 werror( "illegal zero sized structure member: %s", p->sname ); 600 #endif 601 } 602 if( sz > strucoff ) strucoff = sz; /* for use with unions */ 603 SETOFF( al, sa ); 604 /* set al, the alignment, to the lcm of the alignments of the members */ 605 } 606 dstash( -1 ); /* endmarker */ 607 SETOFF( strucoff, al ); 608 609 if( temp == ENUMTY ){ 610 register TWORD ty; 611 612 # ifdef ENUMSIZE 613 ty = ENUMSIZE(high,low); 614 # else 615 if( (char)high == high && (char)low == low ) ty = ctype( CHAR ); 616 else if( (short)high == high && (short)low == low ) ty = ctype( SHORT ); 617 else ty = ctype(INT); 618 #endif 619 strucoff = tsize( ty, 0, (int)ty ); 620 dimtab[ szindex+2 ] = al = talign( ty, (int)ty ); 621 } 622 623 if( strucoff == 0 ) uerror( "zero sized structure" ); 624 dimtab[ szindex ] = strucoff; 625 dimtab[ szindex+2 ] = al; 626 dimtab[ szindex+3 ] = paramstk[ oparam+3 ]; /* name index */ 627 628 FIXSTRUCT( szindex, oparam ); /* local hook, eg. for sym debugger */ 629 # ifndef BUG1 630 if( ddebug>1 ){ 631 printf( "\tdimtab[%d,%d,%d] = %d,%d,%d\n", szindex,szindex+1,szindex+2, 632 dimtab[szindex],dimtab[szindex+1],dimtab[szindex+2] ); 633 for( i = dimtab[szindex+1]; dimtab[i] >= 0; ++i ){ 634 #ifndef FLEXNAMES 635 printf( "\tmember %.8s(%d)\n", stab[dimtab[i]].sname, dimtab[i] ); 636 #else 637 printf( "\tmember %s(%d)\n", stab[dimtab[i]].sname, dimtab[i] ); 638 #endif 639 } 640 } 641 # endif 642 643 strucoff = paramstk[ oparam+2 ]; 644 paramno = oparam; 645 646 return( mkty( temp, 0, szindex ) ); 647 } 648 649 /* VARARGS */ 650 yyerror( s ) char *s; { /* error printing routine in parser */ 651 652 uerror( s ); 653 654 } 655 656 yyaccpt(){ 657 ftnend(); 658 } 659 660 ftnarg( idn ) { 661 switch( stab[idn].stype ){ 662 663 case UNDEF: 664 /* this parameter, entered at scan */ 665 break; 666 case FARG: 667 #ifndef FLEXNAMES 668 uerror("redeclaration of formal parameter, %.8s", 669 #else 670 uerror("redeclaration of formal parameter, %s", 671 #endif 672 stab[idn].sname); 673 /* fall thru */ 674 case FTN: 675 /* the name of this function matches parm */ 676 /* fall thru */ 677 default: 678 idn = hide( &stab[idn]); 679 break; 680 case TNULL: 681 /* unused entry, fill it */ 682 ; 683 } 684 stab[idn].stype = FARG; 685 stab[idn].sclass = PARAM; 686 psave( idn ); 687 } 688 689 talign( ty, s) register unsigned ty; register s; { 690 /* compute the alignment of an object with type ty, sizeoff index s */ 691 692 register i; 693 if( s<0 && ty!=INT && ty!=CHAR && ty!=SHORT && ty!=UNSIGNED && ty!=UCHAR && ty!=USHORT 694 #ifdef LONGFIELDS 695 && ty!=LONG && ty!=ULONG 696 #endif 697 ){ 698 return( fldal( ty ) ); 699 } 700 701 for( i=0; i<=(SZINT-BTSHIFT-1); i+=TSHIFT ){ 702 switch( (ty>>i)&TMASK ){ 703 704 case FTN: 705 cerror( "compiler takes alignment of function"); 706 case PTR: 707 return( ALPOINT ); 708 case ARY: 709 continue; 710 case 0: 711 break; 712 } 713 } 714 715 switch( BTYPE(ty) ){ 716 717 case UNIONTY: 718 case ENUMTY: 719 case STRTY: 720 return( (unsigned int) dimtab[ s+2 ] ); 721 case CHAR: 722 case UCHAR: 723 return( ALCHAR ); 724 case FLOAT: 725 return( ALFLOAT ); 726 case DOUBLE: 727 return( ALDOUBLE ); 728 case LONG: 729 case ULONG: 730 return( ALLONG ); 731 case SHORT: 732 case USHORT: 733 return( ALSHORT ); 734 default: 735 return( ALINT ); 736 } 737 } 738 739 OFFSZ 740 tsize( ty, d, s ) TWORD ty; { 741 /* compute the size associated with type ty, 742 dimoff d, and sizoff s */ 743 /* BETTER NOT BE CALLED WHEN t, d, and s REFER TO A BIT FIELD... */ 744 745 int i; 746 OFFSZ mult; 747 748 mult = 1; 749 750 for( i=0; i<=(SZINT-BTSHIFT-1); i+=TSHIFT ){ 751 switch( (ty>>i)&TMASK ){ 752 753 case FTN: 754 cerror( "compiler takes size of function"); 755 case PTR: 756 return( SZPOINT * mult ); 757 case ARY: 758 mult *= (unsigned int) dimtab[ d++ ]; 759 continue; 760 case 0: 761 break; 762 763 } 764 } 765 766 if( dimtab[s]==0 ) { 767 if( ty == STRTY ) 768 uerror( "undefined structure" ); 769 else 770 uerror( "unknown size"); 771 return( SZINT ); 772 } 773 return( (unsigned int) dimtab[ s ] * mult ); 774 } 775 776 inforce( n ) OFFSZ n; { /* force inoff to have the value n */ 777 /* inoff is updated to have the value n */ 778 OFFSZ wb; 779 register rest; 780 /* rest is used to do a lot of conversion to ints... */ 781 782 if( inoff == n ) return; 783 if( inoff > n ) { 784 cerror( "initialization alignment error"); 785 } 786 787 wb = inoff; 788 SETOFF( wb, SZINT ); 789 790 /* wb now has the next higher word boundary */ 791 792 if( wb >= n ){ /* in the same word */ 793 rest = n - inoff; 794 vfdzero( rest ); 795 return; 796 } 797 798 /* otherwise, extend inoff to be word aligned */ 799 800 rest = wb - inoff; 801 vfdzero( rest ); 802 803 /* now, skip full words until near to n */ 804 805 rest = (n-inoff)/SZINT; 806 zecode( rest ); 807 808 /* now, the remainder of the last word */ 809 810 rest = n-inoff; 811 vfdzero( rest ); 812 if( inoff != n ) cerror( "inoff error"); 813 814 } 815 816 vfdalign( n ){ /* make inoff have the offset the next alignment of n */ 817 OFFSZ m; 818 819 m = inoff; 820 SETOFF( m, n ); 821 inforce( m ); 822 } 823 824 825 int idebug = 0; 826 827 int ibseen = 0; /* the number of } constructions which have been filled */ 828 829 int ifull = 0; /* 1 if all initializers have been seen */ 830 831 int iclass; /* storage class of thing being initialized */ 832 833 int ilocctr = 0; /* location counter for current initialization */ 834 835 beginit(curid){ 836 /* beginning of initilization; set location ctr and set type */ 837 register struct symtab *p; 838 839 # ifndef BUG1 840 if( idebug >= 3 ) printf( "beginit(), curid = %d\n", curid ); 841 # endif 842 843 p = &stab[curid]; 844 845 iclass = p->sclass; 846 if( curclass == EXTERN || curclass == FORTRAN ) iclass = EXTERN; 847 switch( iclass ){ 848 849 case UNAME: 850 case EXTERN: 851 return; 852 case AUTO: 853 case REGISTER: 854 break; 855 case EXTDEF: 856 case STATIC: 857 ilocctr = ISARY(p->stype)?ADATA:DATA; 858 if( nerrors == 0 ){ 859 (void) locctr( ilocctr ); 860 defalign( talign( p->stype, p->sizoff ) ); 861 defnam( p ); 862 } 863 864 } 865 866 inoff = 0; 867 ibseen = 0; 868 ifull = 0; 869 870 pstk = 0; 871 872 instk( curid, p->stype, p->dimoff, p->sizoff, inoff ); 873 874 } 875 876 instk( id, t, d, s, off ) OFFSZ off; TWORD t; { 877 /* make a new entry on the parameter stack to initialize id */ 878 879 register struct symtab *p; 880 881 for(;;){ 882 # ifndef BUG1 883 if( idebug ) printf( "instk((%d, %o,%d,%d, %d)\n", id, t, d, s, off ); 884 # endif 885 886 /* save information on the stack */ 887 888 if( !pstk ) pstk = instack; 889 else ++pstk; 890 891 pstk->in_fl = 0; /* { flag */ 892 pstk->in_id = id ; 893 pstk->in_t = t ; 894 pstk->in_d = d ; 895 pstk->in_s = s ; 896 pstk->in_n = 0; /* number seen */ 897 pstk->in_x = t==STRTY ?dimtab[s+1] : 0 ; 898 pstk->in_off = off; /* offset at the beginning of this element */ 899 /* if t is an array, DECREF(t) can't be a field */ 900 /* INS_sz has size of array elements, and -size for fields */ 901 if( ISARY(t) ){ 902 pstk->in_sz = tsize( DECREF(t), d+1, s ); 903 } 904 else if( stab[id].sclass & FIELD ){ 905 pstk->in_sz = - ( stab[id].sclass & FLDSIZ ); 906 } 907 else { 908 pstk->in_sz = 0; 909 } 910 911 if( (iclass==AUTO || iclass == REGISTER ) && 912 (ISARY(t) || t==STRTY) ) uerror( "no automatic aggregate initialization" ); 913 914 /* now, if this is not a scalar, put on another element */ 915 916 if( ISARY(t) ){ 917 t = DECREF(t); 918 ++d; 919 continue; 920 } 921 else if( t == STRTY ){ 922 if( dimtab[pstk->in_s] == 0 ){ 923 uerror( "can't initialize undefined structure" ); 924 iclass = -1; 925 return; 926 } 927 id = dimtab[pstk->in_x]; 928 p = &stab[id]; 929 if( p->sclass != MOS && !(p->sclass&FIELD) ) cerror( "insane structure member list" ); 930 t = p->stype; 931 d = p->dimoff; 932 s = p->sizoff; 933 off += p->offset; 934 continue; 935 } 936 else return; 937 } 938 } 939 940 NODE * 941 getstr(){ /* decide if the string is external or an initializer, and get the contents accordingly */ 942 943 register l, temp; 944 register NODE *p; 945 946 if( (iclass==EXTDEF||iclass==STATIC) && (pstk->in_t == CHAR || pstk->in_t == UCHAR) && 947 pstk!=instack && ISARY( pstk[-1].in_t ) ){ 948 /* treat "abc" as { 'a', 'b', 'c', 0 } */ 949 strflg = 1; 950 ilbrace(); /* simulate { */ 951 inforce( pstk->in_off ); 952 /* if the array is inflexible (not top level), pass in the size and 953 be prepared to throw away unwanted initializers */ 954 lxstr((pstk-1)!=instack?dimtab[(pstk-1)->in_d]:0); /* get the contents */ 955 irbrace(); /* simulate } */ 956 return( NIL ); 957 } 958 else { /* make a label, and get the contents and stash them away */ 959 if( iclass != SNULL ){ /* initializing */ 960 /* fill out previous word, to permit pointer */ 961 vfdalign( ALPOINT ); 962 } 963 temp = locctr( blevel==0?ISTRNG:STRNG ); /* set up location counter */ 964 deflab( l = getlab() ); 965 strflg = 0; 966 lxstr(0); /* get the contents */ 967 (void) locctr( blevel==0?ilocctr:temp ); 968 p = buildtree( STRING, NIL, NIL ); 969 p->tn.rval = -l; 970 return(p); 971 } 972 } 973 974 putbyte( v ){ /* simulate byte v appearing in a list of integer values */ 975 register NODE *p; 976 p = bcon(v); 977 incode( p, SZCHAR ); 978 tfree( p ); 979 gotscal(); 980 } 981 982 endinit(){ 983 register TWORD t; 984 register d, s, n, d1; 985 986 # ifndef BUG1 987 if( idebug ) printf( "endinit(), inoff = %d\n", inoff ); 988 # endif 989 990 switch( iclass ){ 991 992 case EXTERN: 993 case AUTO: 994 case REGISTER: 995 case -1: 996 return; 997 } 998 999 pstk = instack; 1000 1001 t = pstk->in_t; 1002 d = pstk->in_d; 1003 s = pstk->in_s; 1004 n = pstk->in_n; 1005 1006 if( ISARY(t) ){ 1007 d1 = dimtab[d]; 1008 1009 vfdalign( pstk->in_sz ); /* fill out part of the last element, if needed */ 1010 n = inoff/pstk->in_sz; /* real number of initializers */ 1011 if( d1 >= n ){ 1012 /* once again, t is an array, so no fields */ 1013 inforce( tsize( t, d, s ) ); 1014 n = d1; 1015 } 1016 if( d1!=0 && d1!=n ) uerror( "too many initializers"); 1017 if( n==0 ) werror( "empty array declaration"); 1018 dimtab[d] = n; 1019 if( d1==0 ) FIXDEF(&stab[pstk->in_id]); 1020 } 1021 1022 else if( t == STRTY || t == UNIONTY ){ 1023 /* clearly not fields either */ 1024 inforce( tsize( t, d, s ) ); 1025 } 1026 else if( n > 1 ) uerror( "bad scalar initialization"); 1027 /* this will never be called with a field element... */ 1028 else inforce( tsize(t,d,s) ); 1029 1030 paramno = 0; 1031 vfdalign( AL_INIT ); 1032 inoff = 0; 1033 iclass = SNULL; 1034 1035 } 1036 1037 fixinit(){ 1038 /* called from the grammar if we must punt during initialization */ 1039 /* stolen from endinit() */ 1040 pstk = instack; 1041 paramno = 0; 1042 vfdalign( AL_INIT ); 1043 inoff = 0; 1044 iclass = SNULL; 1045 } 1046 1047 doinit( p ) register NODE *p; { 1048 1049 /* take care of generating a value for the initializer p */ 1050 /* inoff has the current offset (last bit written) 1051 in the current word being generated */ 1052 1053 register sz, d, s; 1054 register TWORD t; 1055 int o; 1056 1057 /* note: size of an individual initializer is assumed to fit into an int */ 1058 1059 if( iclass < 0 ) goto leave; 1060 if( iclass == EXTERN || iclass == UNAME ){ 1061 uerror( "cannot initialize extern or union" ); 1062 iclass = -1; 1063 goto leave; 1064 } 1065 1066 if( iclass == AUTO || iclass == REGISTER ){ 1067 /* do the initialization and get out, without regard 1068 for filing out the variable with zeros, etc. */ 1069 bccode(); 1070 idname = pstk->in_id; 1071 p = buildtree( ASSIGN, buildtree( NAME, NIL, NIL ), p ); 1072 ecomp(p); 1073 return; 1074 } 1075 1076 if( p == NIL ) return; /* for throwing away strings that have been turned into lists */ 1077 1078 if( ifull ){ 1079 uerror( "too many initializers" ); 1080 iclass = -1; 1081 goto leave; 1082 } 1083 if( ibseen ){ 1084 uerror( "} expected"); 1085 goto leave; 1086 } 1087 1088 # ifndef BUG1 1089 if( idebug > 1 ) printf( "doinit(%o)\n", p ); 1090 # endif 1091 1092 t = pstk->in_t; /* type required */ 1093 d = pstk->in_d; 1094 s = pstk->in_s; 1095 if( pstk->in_sz < 0 ){ /* bit field */ 1096 sz = -pstk->in_sz; 1097 } 1098 else { 1099 sz = tsize( t, d, s ); 1100 } 1101 1102 inforce( pstk->in_off ); 1103 1104 p = buildtree( ASSIGN, block( NAME, NIL,NIL, t, d, s ), p ); 1105 p->in.left->in.op = FREE; 1106 p->in.left = p->in.right; 1107 p->in.right = NIL; 1108 p->in.left = optim( p->in.left ); 1109 o = p->in.left->in.op; 1110 if( o == UNARY AND ){ 1111 o = p->in.left->in.op = FREE; 1112 p->in.left = p->in.left->in.left; 1113 } 1114 p->in.op = INIT; 1115 1116 if( sz < SZINT ){ /* special case: bit fields, etc. */ 1117 if( o != ICON || p->in.left->tn.rval != NONAME ) 1118 uerror( "illegal initialization" ); 1119 else incode( p->in.left, sz ); 1120 } 1121 else if( o == FCON ){ 1122 fincode( p->in.left->fpn.fval, sz ); 1123 } 1124 else if( o == DCON ){ 1125 fincode( p->in.left->dpn.dval, sz ); 1126 } 1127 else { 1128 p = optim(p); 1129 if( p->in.left->in.op != ICON ) uerror( "illegal initialization" ); 1130 else cinit( p, sz ); 1131 } 1132 1133 gotscal(); 1134 1135 leave: 1136 tfree(p); 1137 } 1138 1139 gotscal(){ 1140 register t, ix; 1141 register n, id; 1142 struct symtab *p; 1143 OFFSZ temp; 1144 1145 for( ; pstk > instack; ) { 1146 1147 if( pstk->in_fl ) ++ibseen; 1148 1149 --pstk; 1150 1151 t = pstk->in_t; 1152 1153 if( t == STRTY ){ 1154 ix = ++pstk->in_x; 1155 if( (id=dimtab[ix]) < 0 ) continue; 1156 1157 /* otherwise, put next element on the stack */ 1158 1159 p = &stab[id]; 1160 instk( id, p->stype, p->dimoff, p->sizoff, p->offset+pstk->in_off ); 1161 return; 1162 } 1163 else if( ISARY(t) ){ 1164 n = ++pstk->in_n; 1165 if( n >= dimtab[pstk->in_d] && pstk > instack ) continue; 1166 1167 /* put the new element onto the stack */ 1168 1169 temp = pstk->in_sz; 1170 instk( pstk->in_id, (TWORD)DECREF(pstk->in_t), pstk->in_d+1, pstk->in_s, 1171 pstk->in_off+n*temp ); 1172 return; 1173 } 1174 1175 } 1176 ifull = 1; 1177 } 1178 1179 ilbrace(){ /* process an initializer's left brace */ 1180 register t; 1181 struct instk *temp; 1182 1183 temp = pstk; 1184 1185 for( ; pstk > instack; --pstk ){ 1186 1187 t = pstk->in_t; 1188 if( t != STRTY && !ISARY(t) ) continue; /* not an aggregate */ 1189 if( pstk->in_fl ){ /* already associated with a { */ 1190 if( pstk->in_n ) uerror( "illegal {"); 1191 continue; 1192 } 1193 1194 /* we have one ... */ 1195 pstk->in_fl = 1; 1196 break; 1197 } 1198 1199 /* cannot find one */ 1200 /* ignore such right braces */ 1201 1202 pstk = temp; 1203 } 1204 1205 irbrace(){ 1206 /* called when a '}' is seen */ 1207 1208 # ifndef BUG1 1209 if( idebug ) printf( "irbrace(): paramno = %d on entry\n", paramno ); 1210 # endif 1211 1212 if( ibseen ) { 1213 --ibseen; 1214 return; 1215 } 1216 1217 for( ; pstk > instack; --pstk ){ 1218 if( !pstk->in_fl ) continue; 1219 1220 /* we have one now */ 1221 1222 pstk->in_fl = 0; /* cancel { */ 1223 gotscal(); /* take it away... */ 1224 return; 1225 } 1226 1227 /* these right braces match ignored left braces: throw out */ 1228 ifull = 1; 1229 1230 } 1231 1232 upoff( size, alignment, poff ) register alignment, *poff; { 1233 /* update the offset pointed to by poff; return the 1234 /* offset of a value of size `size', alignment `alignment', 1235 /* given that off is increasing */ 1236 1237 register off; 1238 1239 off = *poff; 1240 SETOFF( off, alignment ); 1241 if( (offsz-off) < size ){ 1242 if( instruct!=INSTRUCT )cerror("too many local variables"); 1243 else cerror("Structure too large"); 1244 } 1245 *poff = off+size; 1246 return( off ); 1247 } 1248 1249 oalloc( p, poff ) register struct symtab *p; register *poff; { 1250 /* allocate p with offset *poff, and update *poff */ 1251 register al, off, tsz; 1252 int noff; 1253 1254 al = talign( p->stype, p->sizoff ); 1255 noff = off = *poff; 1256 tsz = tsize( p->stype, p->dimoff, p->sizoff ); 1257 #ifdef BACKAUTO 1258 if( p->sclass == AUTO ){ 1259 if( (offsz-off) < tsz ) cerror("too many local variables"); 1260 noff = off + tsz; 1261 SETOFF( noff, al ); 1262 off = -noff; 1263 } 1264 else 1265 #endif 1266 if( p->sclass == PARAM && ( tsz < SZINT ) ){ 1267 off = upoff( SZINT, ALINT, &noff ); 1268 # ifndef RTOLBYTES 1269 off = noff - tsz; 1270 #endif 1271 } 1272 else 1273 { 1274 off = upoff( tsz, al, &noff ); 1275 } 1276 1277 if( p->sclass != REGISTER ){ /* in case we are allocating stack space for register arguments */ 1278 if( p->offset == NOOFFSET ) p->offset = off; 1279 else if( off != p->offset ) return(1); 1280 } 1281 1282 *poff = noff; 1283 return(0); 1284 } 1285 1286 falloc( p, w, new, pty ) register struct symtab *p; NODE *pty; { 1287 /* allocate a field of width w */ 1288 /* new is 0 if new entry, 1 if redefinition, -1 if alignment */ 1289 1290 register al,sz,type; 1291 1292 type = (new<0)? pty->in.type : p->stype; 1293 1294 /* this must be fixed to use the current type in alignments */ 1295 switch( new<0?pty->in.type:p->stype ){ 1296 1297 case ENUMTY: 1298 { 1299 int s; 1300 s = new<0 ? pty->fn.csiz : p->sizoff; 1301 al = dimtab[s+2]; 1302 sz = dimtab[s]; 1303 break; 1304 } 1305 1306 case CHAR: 1307 case UCHAR: 1308 al = ALCHAR; 1309 sz = SZCHAR; 1310 break; 1311 1312 case SHORT: 1313 case USHORT: 1314 al = ALSHORT; 1315 sz = SZSHORT; 1316 break; 1317 1318 case INT: 1319 case UNSIGNED: 1320 al = ALINT; 1321 sz = SZINT; 1322 break; 1323 #ifdef LONGFIELDS 1324 1325 case LONG: 1326 case ULONG: 1327 al = ALLONG; 1328 sz = SZLONG; 1329 break; 1330 #endif 1331 1332 default: 1333 if( new < 0 ) { 1334 uerror( "illegal field type" ); 1335 al = ALINT; 1336 } 1337 else { 1338 al = fldal( p->stype ); 1339 sz =SZINT; 1340 } 1341 } 1342 1343 if( w > sz ) { 1344 uerror( "field too big"); 1345 w = sz; 1346 } 1347 1348 if( w == 0 ){ /* align only */ 1349 SETOFF( strucoff, al ); 1350 if( new >= 0 ) uerror( "zero size field"); 1351 return(0); 1352 } 1353 1354 if( strucoff%al + w > sz ) SETOFF( strucoff, al ); 1355 if( new < 0 ) { 1356 if( (offsz-strucoff) < w ) 1357 cerror("structure too large"); 1358 strucoff += w; /* we know it will fit */ 1359 return(0); 1360 } 1361 1362 /* establish the field */ 1363 1364 if( new == 1 ) { /* previous definition */ 1365 if( p->offset != strucoff || p->sclass != (FIELD|w) ) return(1); 1366 } 1367 p->offset = strucoff; 1368 if( (offsz-strucoff) < w ) cerror("structure too large"); 1369 strucoff += w; 1370 p->stype = type; 1371 fldty( p ); 1372 return(0); 1373 } 1374 1375 nidcl( p ) NODE *p; { /* handle unitialized declarations */ 1376 /* assumed to be not functions */ 1377 register class; 1378 register commflag; /* flag for labelled common declarations */ 1379 1380 commflag = 0; 1381 1382 /* compute class */ 1383 if( (class=curclass) == SNULL ){ 1384 if( blevel > 1 ) class = AUTO; 1385 else if( blevel != 0 || instruct ) cerror( "nidcl error" ); 1386 else { /* blevel = 0 */ 1387 class = noinit(); 1388 if( class == EXTERN ) commflag = 1; 1389 } 1390 } 1391 #ifdef LCOMM 1392 /* hack so stab will come out as LCSYM rather than STSYM */ 1393 if (class == STATIC) { 1394 extern int stabLCSYM; 1395 stabLCSYM = 1; 1396 } 1397 #endif 1398 1399 defid( p, class ); 1400 1401 /* if an array is not initialized, no empty dimension */ 1402 if( class!=EXTERN && ISARY(p->in.type) && dimtab[p->fn.cdim]==0 ) 1403 uerror("null storage definition"); 1404 1405 #ifndef LCOMM 1406 if( class==EXTDEF || class==STATIC ) 1407 #else 1408 if (class==STATIC) { 1409 register struct symtab *s = &stab[p->tn.rval]; 1410 extern int stabLCSYM; 1411 int sz = tsize(s->stype, s->dimoff, s->sizoff)/SZCHAR; 1412 1413 stabLCSYM = 0; 1414 if (sz % sizeof (int)) 1415 sz += sizeof (int) - (sz % sizeof (int)); 1416 if (s->slevel > 1) 1417 printf(" .lcomm L%d,%d\n", s->offset, sz); 1418 else 1419 printf(" .lcomm %s,%d\n", exname(s->sname), sz); 1420 }else if (class == EXTDEF) 1421 #endif 1422 { 1423 /* simulate initialization by 0 */ 1424 beginit(p->tn.rval); 1425 endinit(); 1426 } 1427 if( commflag ) commdec( p->tn.rval ); 1428 } 1429 1430 TWORD 1431 types( t1, t2, t3 ) TWORD t1, t2, t3; { 1432 /* return a basic type from basic types t1, t2, and t3 */ 1433 1434 TWORD t[3], noun, adj, unsg; 1435 register i; 1436 1437 t[0] = t1; 1438 t[1] = t2; 1439 t[2] = t3; 1440 1441 unsg = INT; /* INT or UNSIGNED */ 1442 noun = UNDEF; /* INT, CHAR, or FLOAT */ 1443 adj = INT; /* INT, LONG, or SHORT */ 1444 1445 for( i=0; i<3; ++i ){ 1446 switch( t[i] ){ 1447 1448 default: 1449 bad: 1450 uerror( "illegal type combination" ); 1451 return( INT ); 1452 1453 case UNDEF: 1454 continue; 1455 1456 case UNSIGNED: 1457 if( unsg != INT ) goto bad; 1458 unsg = UNSIGNED; 1459 continue; 1460 1461 case LONG: 1462 case SHORT: 1463 if( adj != INT ) goto bad; 1464 adj = t[i]; 1465 continue; 1466 1467 case INT: 1468 case CHAR: 1469 case FLOAT: 1470 if( noun != UNDEF ) goto bad; 1471 noun = t[i]; 1472 continue; 1473 } 1474 } 1475 1476 /* now, construct final type */ 1477 if( noun == UNDEF ) noun = INT; 1478 else if( noun == FLOAT ){ 1479 if( unsg != INT || adj == SHORT ) goto bad; 1480 return( adj==LONG ? DOUBLE : FLOAT ); 1481 } 1482 else if( noun == CHAR && adj != INT ) goto bad; 1483 1484 /* now, noun is INT or CHAR */ 1485 if( adj != INT ) noun = adj; 1486 if( unsg == UNSIGNED ) return( noun + (UNSIGNED-INT) ); 1487 else return( noun ); 1488 } 1489 1490 NODE * 1491 tymerge( typ, idp ) NODE *typ, *idp; { 1492 /* merge type typ with identifier idp */ 1493 1494 register unsigned t; 1495 register i; 1496 extern int eprint(); 1497 1498 if( typ->in.op != TYPE ) cerror( "tymerge: arg 1" ); 1499 if(idp == NIL ) return( NIL ); 1500 1501 # ifndef BUG1 1502 if( ddebug > 2 ) fwalk( idp, eprint, 0 ); 1503 # endif 1504 1505 idp->in.type = typ->in.type; 1506 idp->fn.cdim = curdim; 1507 tyreduce( idp ); 1508 idp->fn.csiz = typ->fn.csiz; 1509 1510 for( t=typ->in.type, i=typ->fn.cdim; t&TMASK; t = DECREF(t) ){ 1511 if( ISARY(t) ) dstash( dimtab[i++] ); 1512 } 1513 1514 /* now idp is a single node: fix up type */ 1515 1516 idp->in.type = ctype( idp->in.type ); 1517 1518 if( (t = BTYPE(idp->in.type)) != STRTY && t != UNIONTY && t != ENUMTY ){ 1519 idp->fn.csiz = t; /* in case ctype has rewritten things */ 1520 } 1521 1522 return( idp ); 1523 } 1524 1525 tyreduce( p ) register NODE *p; { 1526 1527 /* build a type, and stash away dimensions, from a parse tree of the declaration */ 1528 /* the type is build top down, the dimensions bottom up */ 1529 register o, temp; 1530 register unsigned t; 1531 1532 o = p->in.op; 1533 p->in.op = FREE; 1534 1535 if( o == NAME ) return; 1536 1537 t = INCREF( p->in.type ); 1538 if( o == UNARY CALL ) t += (FTN-PTR); 1539 else if( o == LB ){ 1540 t += (ARY-PTR); 1541 temp = p->in.right->tn.lval; 1542 p->in.right->in.op = FREE; 1543 if( temp == 0 && p->in.left->tn.op == LB ) 1544 uerror( "null dimension" ); 1545 } 1546 1547 p->in.left->in.type = t; 1548 tyreduce( p->in.left ); 1549 1550 if( o == LB ) dstash( temp ); 1551 1552 p->tn.rval = p->in.left->tn.rval; 1553 p->in.type = p->in.left->in.type; 1554 1555 } 1556 1557 fixtype( p, class ) register NODE *p; { 1558 register unsigned t, type; 1559 register mod1, mod2; 1560 /* fix up the types, and check for legality */ 1561 1562 if( (type = p->in.type) == UNDEF ) return; 1563 if( mod2 = (type&TMASK) ){ 1564 t = DECREF(type); 1565 while( mod1=mod2, mod2 = (t&TMASK) ){ 1566 if( mod1 == ARY && mod2 == FTN ){ 1567 uerror( "array of functions is illegal" ); 1568 type = 0; 1569 } 1570 else if( mod1 == FTN && ( mod2 == ARY || mod2 == FTN ) ){ 1571 uerror( "function returns illegal type" ); 1572 type = 0; 1573 } 1574 t = DECREF(t); 1575 } 1576 } 1577 1578 /* detect function arguments, watching out for structure declarations */ 1579 /* for example, beware of f(x) struct { int a[10]; } *x; { ... } */ 1580 /* the danger is that "a" will be converted to a pointer */ 1581 1582 if( class==SNULL && blevel==1 && !(instruct&(INSTRUCT|INUNION)) ) 1583 class = PARAM; 1584 if( class == PARAM || ( class==REGISTER && blevel==1 ) ){ 1585 if( type == FLOAT ) type = DOUBLE; 1586 else if( ISARY(type) ){ 1587 #ifdef LINT 1588 if( hflag && dimtab[p->fn.cdim]!=0 ) 1589 werror("array[%d] type changed to pointer", 1590 dimtab[p->fn.cdim]); 1591 #endif 1592 ++p->fn.cdim; 1593 type += (PTR-ARY); 1594 } 1595 else if( ISFTN(type) ){ 1596 werror( "a function is declared as an argument" ); 1597 type = INCREF(type); 1598 } 1599 1600 } 1601 1602 if( instruct && ISFTN(type) ){ 1603 uerror( "function illegal in structure or union" ); 1604 type = INCREF(type); 1605 } 1606 p->in.type = type; 1607 } 1608 1609 uclass( class ) register class; { 1610 /* give undefined version of class */ 1611 if( class == SNULL ) return( EXTERN ); 1612 else if( class == STATIC ) return( USTATIC ); 1613 else if( class == FORTRAN ) return( UFORTRAN ); 1614 else return( class ); 1615 } 1616 1617 fixclass( class, type ) TWORD type; { 1618 1619 /* first, fix null class */ 1620 1621 if( class == SNULL ){ 1622 if( instruct&INSTRUCT ) class = MOS; 1623 else if( instruct&INUNION ) class = MOU; 1624 else if( blevel == 0 ) class = EXTDEF; 1625 else if( blevel == 1 ) class = PARAM; 1626 else class = AUTO; 1627 1628 } 1629 1630 /* now, do general checking */ 1631 1632 if( ISFTN( type ) ){ 1633 switch( class ) { 1634 default: 1635 uerror( "function has illegal storage class" ); 1636 case AUTO: 1637 class = EXTERN; 1638 case EXTERN: 1639 case EXTDEF: 1640 case FORTRAN: 1641 case TYPEDEF: 1642 case STATIC: 1643 case UFORTRAN: 1644 case USTATIC: 1645 ; 1646 } 1647 } 1648 1649 if( class&FIELD ){ 1650 if( !(instruct&INSTRUCT) ) uerror( "illegal use of field" ); 1651 return( class ); 1652 } 1653 1654 switch( class ){ 1655 1656 case MOU: 1657 if( !(instruct&INUNION) ) uerror( "illegal class" ); 1658 return( class ); 1659 1660 case MOS: 1661 if( !(instruct&INSTRUCT) ) uerror( "illegal class" ); 1662 return( class ); 1663 1664 case MOE: 1665 if( instruct & (INSTRUCT|INUNION) ) uerror( "illegal class" ); 1666 return( class ); 1667 1668 case REGISTER: 1669 if( blevel == 0 ) uerror( "illegal register declaration" ); 1670 else if( regvar >= MINRVAR && cisreg( type ) ) return( class ); 1671 if( blevel == 1 ) return( PARAM ); 1672 else return( AUTO ); 1673 1674 case AUTO: 1675 case LABEL: 1676 case ULABEL: 1677 if( blevel < 2 ) uerror( "illegal class" ); 1678 return( class ); 1679 1680 case PARAM: 1681 if( blevel != 1 ) uerror( "illegal class" ); 1682 return( class ); 1683 1684 case UFORTRAN: 1685 case FORTRAN: 1686 # ifdef NOFORTRAN 1687 NOFORTRAN; /* a condition which can regulate the FORTRAN usage */ 1688 # endif 1689 if( !ISFTN(type) ) uerror( "fortran declaration must apply to function" ); 1690 else { 1691 type = DECREF(type); 1692 if( ISFTN(type) || ISARY(type) || ISPTR(type) ) { 1693 uerror( "fortran function has wrong type" ); 1694 } 1695 } 1696 case EXTERN: 1697 case STATIC: 1698 case EXTDEF: 1699 case TYPEDEF: 1700 case USTATIC: 1701 if( blevel == 1 ){ 1702 uerror( "illegal class" ); 1703 return( PARAM ); 1704 } 1705 case STNAME: 1706 case UNAME: 1707 case ENAME: 1708 return( class ); 1709 1710 default: 1711 cerror( "illegal class: %d", class ); 1712 /* NOTREACHED */ 1713 1714 } 1715 } 1716 1717 struct symtab * 1718 mknonuniq(idindex) int *idindex; {/* locate a symbol table entry for */ 1719 /* an occurrence of a nonunique structure member name */ 1720 /* or field */ 1721 register i; 1722 register struct symtab * sp; 1723 char *q; 1724 1725 sp = & stab[ i= *idindex ]; /* position search at old entry */ 1726 while( sp->stype != TNULL ){ /* locate unused entry */ 1727 if( ++i >= SYMTSZ ){/* wrap around symbol table */ 1728 i = 0; 1729 sp = stab; 1730 } 1731 else ++sp; 1732 if( i == *idindex ) cerror("Symbol table full"); 1733 } 1734 sp->sflags = SNONUNIQ | SMOS; 1735 q = stab[*idindex].sname; /* old entry name */ 1736 #ifdef FLEXNAMES 1737 sp->sname = stab[*idindex].sname; 1738 #endif 1739 # ifndef BUG1 1740 if( ddebug ){ 1741 printf("\tnonunique entry for %s from %d to %d\n", 1742 q, *idindex, i ); 1743 } 1744 # endif 1745 *idindex = i; 1746 #ifndef FLEXNAMES 1747 { 1748 char *p = sp->sname; 1749 for( i=1; i<=NCHNAM; ++i ) /* copy name */ 1750 if( *p++ = *q /* assign */ ) ++q; 1751 } 1752 #endif 1753 return ( sp ); 1754 } 1755 1756 lookup( name, s) char *name; { 1757 /* look up name: must agree with s w.r.t. STAG, SMOS and SHIDDEN */ 1758 1759 register char *p, *q; 1760 int i, ii; 1761 #ifndef FLEXNAMES 1762 int j; 1763 #endif 1764 register struct symtab *sp; 1765 1766 /* compute initial hash index */ 1767 # ifndef BUG1 1768 if( ddebug > 2 ){ 1769 printf( "lookup( %s, %d ), stwart=%d, instruct=%d\n", name, s, stwart, instruct ); 1770 } 1771 # endif 1772 1773 i = 0; 1774 #ifndef FLEXNAMES 1775 for( p=name, j=0; *p != '\0'; ++p ){ 1776 i += *p; 1777 if( ++j >= NCHNAM ) break; 1778 } 1779 #else 1780 i = (int)name; 1781 #endif 1782 i = i%SYMTSZ; 1783 sp = &stab[ii=i]; 1784 1785 for(;;){ /* look for name */ 1786 1787 if( sp->stype == TNULL ){ /* empty slot */ 1788 sp->sflags = s; /* set STAG, SMOS if needed, turn off all others */ 1789 #ifndef FLEXNAMES 1790 p = sp->sname; 1791 for( j=0; j<NCHNAM; ++j ) if( *p++ = *name ) ++name; 1792 #else 1793 sp->sname = name; 1794 #endif 1795 sp->stype = UNDEF; 1796 sp->sclass = SNULL; 1797 return( i ); 1798 } 1799 if( (sp->sflags & (STAG|SMOS|SHIDDEN)) != s ) goto next; 1800 p = sp->sname; 1801 q = name; 1802 #ifndef FLEXNAMES 1803 for( j=0; j<NCHNAM;++j ){ 1804 if( *p++ != *q ) goto next; 1805 if( !*q++ ) break; 1806 } 1807 return( i ); 1808 #else 1809 if (p == q) 1810 return ( i ); 1811 #endif 1812 next: 1813 if( ++i >= SYMTSZ ){ 1814 i = 0; 1815 sp = stab; 1816 } 1817 else ++sp; 1818 if( i == ii ) cerror( "symbol table full" ); 1819 } 1820 } 1821 1822 #ifndef checkst 1823 /* if not debugging, make checkst a macro */ 1824 checkst(lev){ 1825 register int s, i, j; 1826 register struct symtab *p, *q; 1827 1828 for( i=0, p=stab; i<SYMTSZ; ++i, ++p ){ 1829 if( p->stype == TNULL ) continue; 1830 j = lookup( p->sname, p->sflags&(SMOS|STAG) ); 1831 if( j != i ){ 1832 q = &stab[j]; 1833 if( q->stype == UNDEF || 1834 q->slevel <= p->slevel ){ 1835 #ifndef FLEXNAMES 1836 cerror( "check error: %.8s", q->sname ); 1837 #else 1838 cerror( "check error: %s", q->sname ); 1839 #endif 1840 } 1841 } 1842 #ifndef FLEXNAMES 1843 else if( p->slevel > lev ) cerror( "%.8s check at level %d", p->sname, lev ); 1844 #else 1845 else if( p->slevel > lev ) cerror( "%s check at level %d", p->sname, lev ); 1846 #endif 1847 } 1848 } 1849 #endif 1850 1851 struct symtab * 1852 relook(p) register struct symtab *p; { /* look up p again, and see where it lies */ 1853 1854 register struct symtab *q; 1855 1856 /* I'm not sure that this handles towers of several hidden definitions in all cases */ 1857 q = &stab[lookup( p->sname, p->sflags&(STAG|SMOS|SHIDDEN) )]; 1858 /* make relook always point to either p or an empty cell */ 1859 if( q->stype == UNDEF ){ 1860 q->stype = TNULL; 1861 return(q); 1862 } 1863 while( q != p ){ 1864 if( q->stype == TNULL ) break; 1865 if( ++q >= &stab[SYMTSZ] ) q=stab; 1866 } 1867 return(q); 1868 } 1869 1870 clearst( lev ) register int lev; { 1871 register struct symtab *p, *q; 1872 register int temp; 1873 struct symtab *clist = 0; 1874 1875 temp = lineno; 1876 aobeg(); 1877 1878 /* step 1: remove entries */ 1879 while( chaintop-1 > lev ){ 1880 p = schain[--chaintop]; 1881 schain[chaintop] = 0; 1882 for( ; p; p = q ){ 1883 q = p->snext; 1884 if( p->stype == TNULL || p->slevel <= lev ) 1885 cerror( "schain botch" ); 1886 lineno = p->suse < 0 ? -p->suse : p->suse; 1887 if( p->stype==UNDEF || ( p->sclass==ULABEL && lev<2 ) ){ 1888 lineno = temp; 1889 #ifndef FLEXNAMES 1890 uerror( "%.8s undefined", p->sname ); 1891 #else 1892 uerror( "%s undefined", p->sname ); 1893 #endif 1894 } 1895 else aocode(p); 1896 # ifndef BUG1 1897 if( ddebug ){ 1898 #ifndef FLEXNAMES 1899 printf( "removing %.8s", p->sname ); 1900 #else 1901 printf( "removing %s", p->sname ); 1902 #endif 1903 printf( " from stab[%d], flags %o level %d\n", 1904 p-stab, p->sflags, p->slevel); 1905 } 1906 # endif 1907 if( p->sflags & SHIDES )unhide( p ); 1908 p->stype = TNULL; 1909 p->snext = clist; 1910 clist = p; 1911 } 1912 } 1913 1914 /* step 2: fix any mishashed entries */ 1915 p = clist; 1916 while( p ){ 1917 register struct symtab *r, *next; 1918 1919 q = p; 1920 next = p->snext; 1921 for(;;){ 1922 if( ++q >= &stab[SYMTSZ] )q = stab; 1923 if( q == p || q->stype == TNULL )break; 1924 if( (r = relook(q)) != q ) { 1925 *r = *q; 1926 q->stype = TNULL; 1927 } 1928 } 1929 p = next; 1930 } 1931 1932 lineno = temp; 1933 aoend(); 1934 } 1935 1936 hide( p ) register struct symtab *p; { 1937 register struct symtab *q; 1938 for( q=p+1; ; ++q ){ 1939 if( q >= &stab[SYMTSZ] ) q = stab; 1940 if( q == p ) cerror( "symbol table full" ); 1941 if( q->stype == TNULL ) break; 1942 } 1943 *q = *p; 1944 p->sflags |= SHIDDEN; 1945 q->sflags = (p->sflags&(SMOS|STAG)) | SHIDES; 1946 #ifndef FLEXNAMES 1947 if( hflag ) werror( "%.8s redefinition hides earlier one", p->sname ); 1948 #else 1949 if( hflag ) werror( "%s redefinition hides earlier one", p->sname ); 1950 #endif 1951 # ifndef BUG1 1952 if( ddebug ) printf( " %d hidden in %d\n", p-stab, q-stab ); 1953 # endif 1954 return( idname = q-stab ); 1955 } 1956 1957 unhide( p ) register struct symtab *p; { 1958 register struct symtab *q; 1959 register s; 1960 1961 s = p->sflags & (SMOS|STAG); 1962 q = p; 1963 1964 for(;;){ 1965 1966 if( q == stab ) q = &stab[SYMTSZ-1]; 1967 else --q; 1968 1969 if( q == p ) break; 1970 1971 if( (q->sflags&(SMOS|STAG)) == s ){ 1972 #ifndef FLEXNAMES 1973 register j; 1974 for( j =0; j<NCHNAM; ++j ) if( p->sname[j] != q->sname[j] ) break; 1975 if( j == NCHNAM ){ /* found the name */ 1976 #else 1977 if (p->sname == q->sname) { 1978 #endif 1979 q->sflags &= ~SHIDDEN; 1980 # ifndef BUG1 1981 if( ddebug ) printf( "unhide uncovered %d from %d\n", q-stab,p-stab); 1982 # endif 1983 return; 1984 } 1985 } 1986 1987 } 1988 cerror( "unhide fails" ); 1989 } 1990