github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/cc/sub.c (about) 1 // Inferno utils/cc/sub.c 2 // http://code.google.com/p/inferno-os/source/browse/utils/cc/sub.c 3 // 4 // Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved. 5 // Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net) 6 // Portions Copyright © 1997-1999 Vita Nuova Limited 7 // Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com) 8 // Portions Copyright © 2004,2006 Bruce Ellis 9 // Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net) 10 // Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others 11 // Portions Copyright © 2009 The Go Authors. All rights reserved. 12 // 13 // Permission is hereby granted, free of charge, to any person obtaining a copy 14 // of this software and associated documentation files (the "Software"), to deal 15 // in the Software without restriction, including without limitation the rights 16 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 17 // copies of the Software, and to permit persons to whom the Software is 18 // furnished to do so, subject to the following conditions: 19 // 20 // The above copyright notice and this permission notice shall be included in 21 // all copies or substantial portions of the Software. 22 // 23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 24 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 25 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 26 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 27 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 28 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 29 // THE SOFTWARE. 30 31 #include <u.h> 32 #include "cc.h" 33 34 Node* 35 new(int t, Node *l, Node *r) 36 { 37 Node *n; 38 39 n = alloc(sizeof(*n)); 40 n->op = t; 41 n->left = l; 42 n->right = r; 43 if(l && t != OGOTO) 44 n->lineno = l->lineno; 45 else if(r) 46 n->lineno = r->lineno; 47 else 48 n->lineno = lineno; 49 newflag = 1; 50 return n; 51 } 52 53 Node* 54 new1(int o, Node *l, Node *r) 55 { 56 Node *n; 57 58 n = new(o, l, r); 59 n->lineno = nearln; 60 return n; 61 } 62 63 void 64 prtree(Node *n, char *s) 65 { 66 67 print(" == %s ==\n", s); 68 prtree1(n, 0, 0); 69 print("\n"); 70 } 71 72 void 73 prtree1(Node *n, int d, int f) 74 { 75 int i; 76 77 if(f) 78 for(i=0; i<d; i++) 79 print(" "); 80 if(n == Z) { 81 print("Z\n"); 82 return; 83 } 84 if(n->op == OLIST) { 85 prtree1(n->left, d, 0); 86 prtree1(n->right, d, 1); 87 return; 88 } 89 d++; 90 print("%O", n->op); 91 i = 3; 92 switch(n->op) 93 { 94 case ONAME: 95 print(" \"%F\"", n); 96 print(" %d", n->xoffset); 97 i = 0; 98 break; 99 100 case OINDREG: 101 print(" %d(R%d)", n->xoffset, n->reg); 102 i = 0; 103 break; 104 105 case OREGISTER: 106 if(n->xoffset) 107 print(" %d+R%d", n->xoffset, n->reg); 108 else 109 print(" R%d", n->reg); 110 i = 0; 111 break; 112 113 case OSTRING: 114 print(" \"%s\"", n->cstring); 115 i = 0; 116 break; 117 118 case OLSTRING: 119 print(" \"%S\"", n->rstring); 120 i = 0; 121 break; 122 123 case ODOT: 124 case OELEM: 125 print(" \"%F\"", n); 126 break; 127 128 case OCONST: 129 if(typefd[n->type->etype]) 130 print(" \"%.8e\"", n->fconst); 131 else 132 print(" \"%lld\"", n->vconst); 133 i = 0; 134 break; 135 } 136 if(n->addable != 0) 137 print(" <%d>", n->addable); 138 if(n->type != T) 139 print(" %T", n->type); 140 if(n->complex != 0) 141 print(" (%d)", n->complex); 142 print(" %L\n", n->lineno); 143 if(i & 2) 144 prtree1(n->left, d, 1); 145 if(i & 1) 146 prtree1(n->right, d, 1); 147 } 148 149 Type* 150 typ(int et, Type *d) 151 { 152 Type *t; 153 154 t = alloc(sizeof(*t)); 155 t->etype = et; 156 t->link = d; 157 t->down = T; 158 t->sym = S; 159 if(et < NTYPE) 160 t->width = ewidth[et]; 161 else 162 t->width = -1; // for TDOT or TOLD in prototype 163 t->offset = 0; 164 t->shift = 0; 165 t->nbits = 0; 166 t->garb = 0; 167 return t; 168 } 169 170 Type* 171 copytyp(Type *t) 172 { 173 Type *nt; 174 175 nt = typ(TXXX, T); 176 *nt = *t; 177 return nt; 178 } 179 180 Type* 181 garbt(Type *t, int32 b) 182 { 183 Type *t1; 184 185 if(b & BGARB) { 186 t1 = copytyp(t); 187 t1->garb = simpleg(b); 188 return t1; 189 } 190 return t; 191 } 192 193 int 194 simpleg(int32 b) 195 { 196 197 b &= BGARB; 198 switch(b) { 199 case BCONSTNT: 200 return GCONSTNT; 201 case BVOLATILE: 202 return GVOLATILE; 203 case BVOLATILE|BCONSTNT: 204 return GCONSTNT|GVOLATILE; 205 } 206 return GXXX; 207 } 208 209 int 210 simplec(int32 b) 211 { 212 213 b &= BCLASS; 214 switch(b) { 215 case 0: 216 case BREGISTER: 217 return CXXX; 218 case BAUTO: 219 case BAUTO|BREGISTER: 220 return CAUTO; 221 case BEXTERN: 222 return CEXTERN; 223 case BEXTERN|BREGISTER: 224 return CEXREG; 225 case BSTATIC: 226 return CSTATIC; 227 case BTYPEDEF: 228 return CTYPEDEF; 229 case BTYPESTR: 230 return CTYPESTR; 231 } 232 diag(Z, "illegal combination of classes %Q", b); 233 return CXXX; 234 } 235 236 Type* 237 simplet(int32 b) 238 { 239 240 b &= ~BCLASS & ~BGARB; 241 switch(b) { 242 case BCHAR: 243 case BCHAR|BSIGNED: 244 return types[TCHAR]; 245 246 case BCHAR|BUNSIGNED: 247 return types[TUCHAR]; 248 249 case BSHORT: 250 case BSHORT|BINT: 251 case BSHORT|BSIGNED: 252 case BSHORT|BINT|BSIGNED: 253 return types[TSHORT]; 254 255 case BUNSIGNED|BSHORT: 256 case BUNSIGNED|BSHORT|BINT: 257 return types[TUSHORT]; 258 259 case 0: 260 case BINT: 261 case BINT|BSIGNED: 262 case BSIGNED: 263 return types[TINT]; 264 265 case BUNSIGNED: 266 case BUNSIGNED|BINT: 267 return types[TUINT]; 268 269 case BLONG: 270 case BLONG|BINT: 271 case BLONG|BSIGNED: 272 case BLONG|BINT|BSIGNED: 273 return types[TLONG]; 274 275 case BUNSIGNED|BLONG: 276 case BUNSIGNED|BLONG|BINT: 277 return types[TULONG]; 278 279 case BVLONG|BLONG: 280 case BVLONG|BLONG|BINT: 281 case BVLONG|BLONG|BSIGNED: 282 case BVLONG|BLONG|BINT|BSIGNED: 283 return types[TVLONG]; 284 285 case BVLONG|BLONG|BUNSIGNED: 286 case BVLONG|BLONG|BINT|BUNSIGNED: 287 return types[TUVLONG]; 288 289 case BFLOAT: 290 return types[TFLOAT]; 291 292 case BDOUBLE: 293 case BDOUBLE|BLONG: 294 case BFLOAT|BLONG: 295 return types[TDOUBLE]; 296 297 case BVOID: 298 return types[TVOID]; 299 } 300 301 diag(Z, "illegal combination of types %Q", b); 302 return types[TINT]; 303 } 304 305 int 306 stcompat(Node *n, Type *t1, Type *t2, int32 ttab[]) 307 { 308 int i; 309 uint32 b; 310 311 i = 0; 312 if(t2 != T) 313 i = t2->etype; 314 b = 1L << i; 315 i = 0; 316 if(t1 != T) 317 i = t1->etype; 318 if(b & ttab[i]) { 319 if(ttab == tasign) 320 if(b == BSTRUCT || b == BUNION) 321 if(!sametype(t1, t2)) 322 return 1; 323 if(n->op != OCAST) 324 if(b == BIND && i == TIND) 325 if(!sametype(t1, t2)) 326 return 1; 327 return 0; 328 } 329 return 1; 330 } 331 332 int 333 tcompat(Node *n, Type *t1, Type *t2, int32 ttab[]) 334 { 335 336 if(stcompat(n, t1, t2, ttab)) { 337 if(t1 == T) 338 diag(n, "incompatible type: \"%T\" for op \"%O\"", 339 t2, n->op); 340 else 341 diag(n, "incompatible types: \"%T\" and \"%T\" for op \"%O\"", 342 t1, t2, n->op); 343 return 1; 344 } 345 return 0; 346 } 347 348 void 349 makedot(Node *n, Type *t, int32 o) 350 { 351 Node *n1, *n2; 352 353 if(t->nbits) { 354 n1 = new(OXXX, Z, Z); 355 *n1 = *n; 356 n->op = OBIT; 357 n->left = n1; 358 n->right = Z; 359 n->type = t; 360 n->addable = n1->left->addable; 361 n = n1; 362 } 363 n->addable = n->left->addable; 364 if(n->addable == 0) { 365 n1 = new1(OCONST, Z, Z); 366 n1->vconst = o; 367 n1->type = types[TLONG]; 368 n->right = n1; 369 n->type = t; 370 return; 371 } 372 n->left->type = t; 373 if(o == 0) { 374 *n = *n->left; 375 return; 376 } 377 n->type = t; 378 n1 = new1(OCONST, Z, Z); 379 n1->vconst = o; 380 t = typ(TIND, t); 381 t->width = types[TIND]->width; 382 n1->type = t; 383 384 n2 = new1(OADDR, n->left, Z); 385 n2->type = t; 386 387 n1 = new1(OADD, n1, n2); 388 n1->type = t; 389 390 n->op = OIND; 391 n->left = n1; 392 n->right = Z; 393 } 394 395 Type* 396 dotsearch(Sym *s, Type *t, Node *n, int32 *off) 397 { 398 Type *t1, *xt, *rt; 399 400 xt = T; 401 402 /* 403 * look it up by name 404 */ 405 for(t1 = t; t1 != T; t1 = t1->down) 406 if(t1->sym == s) { 407 if(xt != T) 408 goto ambig; 409 xt = t1; 410 } 411 412 /* 413 * look it up by type 414 */ 415 if(s->class == CTYPEDEF || s->class == CTYPESTR) 416 for(t1 = t; t1 != T; t1 = t1->down) 417 if(t1->sym == S && typesu[t1->etype]) 418 if(sametype(s->type, t1)) { 419 if(xt != T) 420 goto ambig; 421 xt = t1; 422 } 423 if(xt != T) { 424 *off = xt->offset; 425 return xt; 426 } 427 428 /* 429 * look it up in unnamed substructures 430 */ 431 for(t1 = t; t1 != T; t1 = t1->down) 432 if(t1->sym == S && typesu[t1->etype]){ 433 rt = dotsearch(s, t1->link, n, off); 434 if(rt != T) { 435 if(xt != T) 436 goto ambig; 437 xt = rt; 438 *off += t1->offset; 439 } 440 } 441 return xt; 442 443 ambig: 444 diag(n, "ambiguous structure element: %s", s->name); 445 return xt; 446 } 447 448 int32 449 dotoffset(Type *st, Type *lt, Node *n) 450 { 451 Type *t; 452 Sym *g; 453 int32 o, o1; 454 455 o = -1; 456 /* 457 * first try matching at the top level 458 * for matching tag names 459 */ 460 g = st->tag; 461 if(g != S) 462 for(t=lt->link; t!=T; t=t->down) 463 if(t->sym == S) 464 if(g == t->tag) { 465 if(o >= 0) 466 goto ambig; 467 o = t->offset; 468 } 469 if(o >= 0) 470 return o; 471 472 /* 473 * second try matching at the top level 474 * for similar types 475 */ 476 for(t=lt->link; t!=T; t=t->down) 477 if(t->sym == S) 478 if(sametype(st, t)) { 479 if(o >= 0) 480 goto ambig; 481 o = t->offset; 482 } 483 if(o >= 0) 484 return o; 485 486 /* 487 * last try matching sub-levels 488 */ 489 for(t=lt->link; t!=T; t=t->down) 490 if(t->sym == S) 491 if(typesu[t->etype]) { 492 o1 = dotoffset(st, t, n); 493 if(o1 >= 0) { 494 if(o >= 0) 495 goto ambig; 496 o = o1 + t->offset; 497 } 498 } 499 return o; 500 501 ambig: 502 diag(n, "ambiguous unnamed structure element"); 503 return o; 504 } 505 506 /* 507 * look into tree for floating point constant expressions 508 */ 509 int 510 allfloat(Node *n, int flag) 511 { 512 513 if(n != Z) { 514 if(n->type->etype != TDOUBLE) 515 return 1; 516 switch(n->op) { 517 case OCONST: 518 if(flag) 519 n->type = types[TFLOAT]; 520 return 1; 521 case OADD: /* no need to get more exotic than this */ 522 case OSUB: 523 case OMUL: 524 case ODIV: 525 if(!allfloat(n->right, flag)) 526 break; 527 case OCAST: 528 if(!allfloat(n->left, flag)) 529 break; 530 if(flag) 531 n->type = types[TFLOAT]; 532 return 1; 533 } 534 } 535 return 0; 536 } 537 538 void 539 constas(Node *n, Type *il, Type *ir) 540 { 541 Type *l, *r; 542 543 l = il; 544 r = ir; 545 546 if(l == T) 547 return; 548 if(l->garb & GCONSTNT) { 549 warn(n, "assignment to a constant type (%T)", il); 550 return; 551 } 552 if(r == T) 553 return; 554 for(;;) { 555 if(l->etype != TIND || r->etype != TIND) 556 break; 557 l = l->link; 558 r = r->link; 559 if(l == T || r == T) 560 break; 561 if(r->garb & GCONSTNT) 562 if(!(l->garb & GCONSTNT)) { 563 warn(n, "assignment of a constant pointer type (%T)", ir); 564 break; 565 } 566 } 567 } 568 569 void 570 typeext1(Type *st, Node *l) 571 { 572 if(st->etype == TFLOAT && allfloat(l, 0)) 573 allfloat(l, 1); 574 } 575 576 void 577 typeext(Type *st, Node *l) 578 { 579 Type *lt; 580 Node *n1, *n2; 581 int32 o; 582 583 lt = l->type; 584 if(lt == T) 585 return; 586 if(st->etype == TIND && vconst(l) == 0) { 587 l->type = st; 588 l->vconst = 0; 589 return; 590 } 591 typeext1(st, l); 592 593 /* 594 * extension of C 595 * if assign of struct containing unnamed sub-struct 596 * to type of sub-struct, insert the DOT. 597 * if assign of *struct containing unnamed substruct 598 * to type of *sub-struct, insert the add-offset 599 */ 600 if(typesu[st->etype] && typesu[lt->etype]) { 601 o = dotoffset(st, lt, l); 602 if(o >= 0) { 603 n1 = new1(OXXX, Z, Z); 604 *n1 = *l; 605 l->op = ODOT; 606 l->left = n1; 607 l->right = Z; 608 makedot(l, st, o); 609 } 610 return; 611 } 612 if(st->etype == TIND && typesu[st->link->etype]) 613 if(lt->etype == TIND && typesu[lt->link->etype]) { 614 o = dotoffset(st->link, lt->link, l); 615 if(o >= 0) { 616 l->type = st; 617 if(o == 0) 618 return; 619 n1 = new1(OXXX, Z, Z); 620 *n1 = *l; 621 n2 = new1(OCONST, Z, Z); 622 n2->vconst = o; 623 n2->type = st; 624 l->op = OADD; 625 l->left = n1; 626 l->right = n2; 627 } 628 return; 629 } 630 } 631 632 /* 633 * a cast that generates no code 634 * (same size move) 635 */ 636 int 637 nocast(Type *t1, Type *t2) 638 { 639 int i, b; 640 641 if(t1->nbits) 642 return 0; 643 i = 0; 644 if(t2 != T) 645 i = t2->etype; 646 b = 1<<i; 647 i = 0; 648 if(t1 != T) 649 i = t1->etype; 650 if(b & ncast[i]) 651 return 1; 652 return 0; 653 } 654 655 /* 656 * a cast that has a noop semantic 657 * (small to large, convert) 658 */ 659 int 660 nilcast(Type *t1, Type *t2) 661 { 662 int et1, et2; 663 664 if(t1 == T) 665 return 0; 666 if(t1->nbits) 667 return 0; 668 if(t2 == T) 669 return 0; 670 et1 = t1->etype; 671 et2 = t2->etype; 672 if(et1 == et2) 673 return 1; 674 if(typefd[et1] && typefd[et2]) { 675 if(ewidth[et1] < ewidth[et2]) 676 return 1; 677 return 0; 678 } 679 if(typechlp[et1] && typechlp[et2]) { 680 if(ewidth[et1] < ewidth[et2]) 681 return 1; 682 return 0; 683 } 684 return 0; 685 } 686 687 /* 688 * "the usual arithmetic conversions are performed" 689 */ 690 void 691 arith(Node *n, int f) 692 { 693 Type *t1, *t2; 694 int i, j, k; 695 Node *n1; 696 int32 w; 697 698 t1 = n->left->type; 699 if(n->right == Z) 700 t2 = t1; 701 else 702 t2 = n->right->type; 703 i = TXXX; 704 if(t1 != T) 705 i = t1->etype; 706 j = TXXX; 707 if(t2 != T) 708 j = t2->etype; 709 k = tab[i][j]; 710 if(k == TIND) { 711 if(i == TIND) 712 n->type = t1; 713 else 714 if(j == TIND) 715 n->type = t2; 716 } else { 717 /* convert up to at least int */ 718 if(f == 1) 719 while(k < TINT) 720 k += 2; 721 n->type = types[k]; 722 } 723 if(n->op == OSUB) 724 if(i == TIND && j == TIND) { 725 w = n->right->type->link->width; 726 if(w < 1 || n->left->type->link == T || n->left->type->link->width < 1) 727 goto bad; 728 n->type = types[ewidth[TIND] <= ewidth[TLONG]? TLONG: TVLONG]; 729 if(0 && ewidth[TIND] > ewidth[TLONG]){ 730 n1 = new1(OXXX, Z, Z); 731 *n1 = *n; 732 n->op = OCAST; 733 n->left = n1; 734 n->right = Z; 735 n->type = types[TLONG]; 736 } 737 if(w > 1) { 738 n1 = new1(OXXX, Z, Z); 739 *n1 = *n; 740 n->op = ODIV; 741 n->left = n1; 742 n1 = new1(OCONST, Z, Z); 743 n1->vconst = w; 744 n1->type = n->type; 745 n->right = n1; 746 w = vlog(n1); 747 if(w >= 0) { 748 n->op = OASHR; 749 n1->vconst = w; 750 } 751 } 752 return; 753 } 754 if(!sametype(n->type, n->left->type)) { 755 n->left = new1(OCAST, n->left, Z); 756 n->left->type = n->type; 757 if(n->type->etype == TIND) { 758 w = n->type->link->width; 759 if(w < 1) { 760 snap(n->type->link); 761 w = n->type->link->width; 762 if(w < 1) 763 goto bad; 764 } 765 if(w > 1) { 766 n1 = new1(OCONST, Z, Z); 767 n1->vconst = w; 768 n1->type = n->type; 769 n->left = new1(OMUL, n->left, n1); 770 n->left->type = n->type; 771 } 772 } 773 } 774 if(n->right != Z) 775 if(!sametype(n->type, n->right->type)) { 776 n->right = new1(OCAST, n->right, Z); 777 n->right->type = n->type; 778 if(n->type->etype == TIND) { 779 w = n->type->link->width; 780 if(w < 1) { 781 snap(n->type->link); 782 w = n->type->link->width; 783 if(w < 1) 784 goto bad; 785 } 786 if(w != 1) { 787 n1 = new1(OCONST, Z, Z); 788 n1->vconst = w; 789 n1->type = n->type; 790 n->right = new1(OMUL, n->right, n1); 791 n->right->type = n->type; 792 } 793 } 794 } 795 return; 796 bad: 797 diag(n, "pointer addition not fully declared: %T", n->type->link); 798 } 799 800 /* 801 * try to rewrite shift & mask 802 */ 803 void 804 simplifyshift(Node *n) 805 { 806 uint32 c3; 807 int o, s1, s2, c1, c2; 808 809 if(!typechlp[n->type->etype]) 810 return; 811 switch(n->op) { 812 default: 813 return; 814 case OASHL: 815 s1 = 0; 816 break; 817 case OLSHR: 818 s1 = 1; 819 break; 820 case OASHR: 821 s1 = 2; 822 break; 823 } 824 if(n->right->op != OCONST) 825 return; 826 if(n->left->op != OAND) 827 return; 828 if(n->left->right->op != OCONST) 829 return; 830 switch(n->left->left->op) { 831 default: 832 return; 833 case OASHL: 834 s2 = 0; 835 break; 836 case OLSHR: 837 s2 = 1; 838 break; 839 case OASHR: 840 s2 = 2; 841 break; 842 } 843 if(n->left->left->right->op != OCONST) 844 return; 845 846 c1 = n->right->vconst; 847 c2 = n->left->left->right->vconst; 848 c3 = n->left->right->vconst; 849 850 o = n->op; 851 switch((s1<<3)|s2) { 852 case 000: /* (((e <<u c2) & c3) <<u c1) */ 853 c3 >>= c2; 854 c1 += c2; 855 if(c1 >= 32) 856 break; 857 goto rewrite1; 858 859 case 002: /* (((e >>s c2) & c3) <<u c1) */ 860 if(topbit(c3) >= (32-c2)) 861 break; 862 case 001: /* (((e >>u c2) & c3) <<u c1) */ 863 if(c1 > c2) { 864 c3 <<= c2; 865 c1 -= c2; 866 o = OASHL; 867 goto rewrite1; 868 } 869 c3 <<= c1; 870 if(c1 == c2) 871 goto rewrite0; 872 c1 = c2-c1; 873 o = OLSHR; 874 goto rewrite2; 875 876 case 022: /* (((e >>s c2) & c3) >>s c1) */ 877 if(c2 <= 0) 878 break; 879 case 012: /* (((e >>s c2) & c3) >>u c1) */ 880 if(topbit(c3) >= (32-c2)) 881 break; 882 goto s11; 883 case 021: /* (((e >>u c2) & c3) >>s c1) */ 884 if(topbit(c3) >= 31 && c2 <= 0) 885 break; 886 goto s11; 887 case 011: /* (((e >>u c2) & c3) >>u c1) */ 888 s11: 889 c3 <<= c2; 890 c1 += c2; 891 if(c1 >= 32) 892 break; 893 o = OLSHR; 894 goto rewrite1; 895 896 case 020: /* (((e <<u c2) & c3) >>s c1) */ 897 if(topbit(c3) >= 31) 898 break; 899 case 010: /* (((e <<u c2) & c3) >>u c1) */ 900 c3 >>= c1; 901 if(c1 == c2) 902 goto rewrite0; 903 if(c1 > c2) { 904 c1 -= c2; 905 goto rewrite2; 906 } 907 c1 = c2 - c1; 908 o = OASHL; 909 goto rewrite2; 910 } 911 return; 912 913 rewrite0: /* get rid of both shifts */ 914 if(debug['<'])prtree(n, "rewrite0"); 915 *n = *n->left; 916 n->left = n->left->left; 917 n->right->vconst = c3; 918 return; 919 rewrite1: /* get rid of lower shift */ 920 if(debug['<'])prtree(n, "rewrite1"); 921 n->left->left = n->left->left->left; 922 n->left->right->vconst = c3; 923 n->right->vconst = c1; 924 n->op = o; 925 return; 926 rewrite2: /* get rid of upper shift */ 927 if(debug['<'])prtree(n, "rewrite2"); 928 *n = *n->left; 929 n->right->vconst = c3; 930 n->left->right->vconst = c1; 931 n->left->op = o; 932 } 933 934 int 935 side(Node *n) 936 { 937 938 loop: 939 if(n != Z) 940 switch(n->op) { 941 case OCAST: 942 case ONOT: 943 case OADDR: 944 case OIND: 945 n = n->left; 946 goto loop; 947 948 case OCOND: 949 if(side(n->left)) 950 break; 951 n = n->right; 952 953 case OEQ: 954 case ONE: 955 case OLT: 956 case OGE: 957 case OGT: 958 case OLE: 959 case OADD: 960 case OSUB: 961 case OMUL: 962 case OLMUL: 963 case ODIV: 964 case OLDIV: 965 case OLSHR: 966 case OASHL: 967 case OASHR: 968 case OAND: 969 case OOR: 970 case OXOR: 971 case OMOD: 972 case OLMOD: 973 case OANDAND: 974 case OOROR: 975 case OCOMMA: 976 case ODOT: 977 if(side(n->left)) 978 break; 979 n = n->right; 980 goto loop; 981 982 case OSIGN: 983 case OSIZE: 984 case OCONST: 985 case OSTRING: 986 case OLSTRING: 987 case ONAME: 988 return 0; 989 } 990 return 1; 991 } 992 993 int 994 vconst(Node *n) 995 { 996 int i; 997 998 if(n == Z) 999 goto no; 1000 if(n->op != OCONST) 1001 goto no; 1002 if(n->type == T) 1003 goto no; 1004 switch(n->type->etype) 1005 { 1006 case TFLOAT: 1007 case TDOUBLE: 1008 i = 100; 1009 if(n->fconst > i || n->fconst < -i) 1010 goto no; 1011 i = n->fconst; 1012 if(i != n->fconst) 1013 goto no; 1014 return i; 1015 1016 case TVLONG: 1017 case TUVLONG: 1018 i = n->vconst; 1019 if(i != n->vconst) 1020 goto no; 1021 return i; 1022 1023 case TCHAR: 1024 case TUCHAR: 1025 case TSHORT: 1026 case TUSHORT: 1027 case TINT: 1028 case TUINT: 1029 case TLONG: 1030 case TULONG: 1031 case TIND: 1032 i = n->vconst; 1033 if(i != n->vconst) 1034 goto no; 1035 return i; 1036 } 1037 no: 1038 return -159; /* first uninteresting constant */ 1039 } 1040 1041 /* 1042 * return log(n) if n is a power of 2 constant 1043 */ 1044 int 1045 xlog2(uvlong v) 1046 { 1047 int s, i; 1048 uvlong m; 1049 1050 s = 0; 1051 m = MASK(8*sizeof(uvlong)); 1052 for(i=32; i; i>>=1) { 1053 m >>= i; 1054 if(!(v & m)) { 1055 v >>= i; 1056 s += i; 1057 } 1058 } 1059 if(v == 1) 1060 return s; 1061 return -1; 1062 } 1063 1064 int 1065 vlog(Node *n) 1066 { 1067 if(n->op != OCONST) 1068 goto bad; 1069 if(typefd[n->type->etype]) 1070 goto bad; 1071 1072 return xlog2(n->vconst); 1073 1074 bad: 1075 return -1; 1076 } 1077 1078 int 1079 topbit(uint32 v) 1080 { 1081 int i; 1082 1083 for(i = -1; v; i++) 1084 v >>= 1; 1085 return i; 1086 } 1087 1088 /* 1089 * try to cast a constant down 1090 * rather than cast a variable up 1091 * example: 1092 * if(c == 'a') 1093 */ 1094 void 1095 relcon(Node *l, Node *r) 1096 { 1097 vlong v; 1098 1099 if(l->op != OCONST) 1100 return; 1101 if(r->op != OCAST) 1102 return; 1103 if(!nilcast(r->left->type, r->type)) 1104 return; 1105 switch(r->type->etype) { 1106 default: 1107 return; 1108 case TCHAR: 1109 case TUCHAR: 1110 case TSHORT: 1111 case TUSHORT: 1112 v = convvtox(l->vconst, r->type->etype); 1113 if(v != l->vconst) 1114 return; 1115 break; 1116 } 1117 l->type = r->left->type; 1118 *r = *r->left; 1119 } 1120 1121 int 1122 relindex(int o) 1123 { 1124 1125 switch(o) { 1126 default: 1127 diag(Z, "bad in relindex: %O", o); 1128 case OEQ: return 0; 1129 case ONE: return 1; 1130 case OLE: return 2; 1131 case OLS: return 3; 1132 case OLT: return 4; 1133 case OLO: return 5; 1134 case OGE: return 6; 1135 case OHS: return 7; 1136 case OGT: return 8; 1137 case OHI: return 9; 1138 } 1139 } 1140 1141 Node* 1142 invert(Node *n) 1143 { 1144 Node *i; 1145 1146 if(n == Z || n->op != OLIST) 1147 return n; 1148 i = n; 1149 for(n = n->left; n != Z; n = n->left) { 1150 if(n->op != OLIST) 1151 break; 1152 i->left = n->right; 1153 n->right = i; 1154 i = n; 1155 } 1156 i->left = n; 1157 return i; 1158 } 1159 1160 int 1161 bitno(int32 b) 1162 { 1163 int i; 1164 1165 for(i=0; i<32; i++) 1166 if(b & (1L<<i)) 1167 return i; 1168 diag(Z, "bad in bitno"); 1169 return 0; 1170 } 1171 1172 int32 1173 typebitor(int32 a, int32 b) 1174 { 1175 int32 c; 1176 1177 c = a | b; 1178 if(a & b) 1179 if((a & b) == BLONG) 1180 c |= BVLONG; /* long long => vlong */ 1181 else 1182 warn(Z, "once is enough: %Q", a & b); 1183 return c; 1184 } 1185 1186 void 1187 diag(Node *n, char *fmt, ...) 1188 { 1189 char buf[STRINGSZ]; 1190 va_list arg; 1191 1192 va_start(arg, fmt); 1193 vseprint(buf, buf+sizeof(buf), fmt, arg); 1194 va_end(arg); 1195 Bprint(&diagbuf, "%L %s\n", (n==Z)? nearln: n->lineno, buf); 1196 1197 if(debug['X']){ 1198 Bflush(&diagbuf); 1199 abort(); 1200 } 1201 if(n != Z) 1202 if(debug['v']) 1203 prtree(n, "diagnostic"); 1204 1205 nerrors++; 1206 if(nerrors > 10) { 1207 Bprint(&diagbuf, "too many errors\n"); 1208 errorexit(); 1209 } 1210 } 1211 1212 void 1213 warn(Node *n, char *fmt, ...) 1214 { 1215 char buf[STRINGSZ]; 1216 va_list arg; 1217 1218 if(debug['w']) { 1219 Bprint(&diagbuf, "warning: "); 1220 va_start(arg, fmt); 1221 vseprint(buf, buf+sizeof(buf), fmt, arg); 1222 va_end(arg); 1223 Bprint(&diagbuf, "%L %s\n", (n==Z)? nearln: n->lineno, buf); 1224 1225 if(n != Z) 1226 if(debug['v']) 1227 prtree(n, "warning"); 1228 } 1229 } 1230 1231 void 1232 yyerror(char *fmt, ...) 1233 { 1234 char buf[STRINGSZ]; 1235 va_list arg; 1236 1237 /* 1238 * hack to intercept message from yaccpar 1239 */ 1240 if(strcmp(fmt, "syntax error") == 0) { 1241 yyerror("syntax error, last name: %s", symb); 1242 return; 1243 } 1244 va_start(arg, fmt); 1245 vseprint(buf, buf+sizeof(buf), fmt, arg); 1246 va_end(arg); 1247 Bprint(&diagbuf, "%L %s\n", lineno, buf); 1248 nerrors++; 1249 if(nerrors > 10) { 1250 Bprint(&diagbuf, "too many errors\n"); 1251 errorexit(); 1252 } 1253 } 1254 1255 void 1256 fatal(Node *n, char *fmt, ...) 1257 { 1258 char buf[STRINGSZ]; 1259 va_list arg; 1260 1261 va_start(arg, fmt); 1262 vseprint(buf, buf+sizeof(buf), fmt, arg); 1263 va_end(arg); 1264 Bprint(&diagbuf, "%L %s\n", (n==Z)? nearln: n->lineno, buf); 1265 1266 if(debug['X']){ 1267 Bflush(&diagbuf); 1268 abort(); 1269 } 1270 if(n != Z) 1271 if(debug['v']) 1272 prtree(n, "diagnostic"); 1273 1274 nerrors++; 1275 errorexit(); 1276 } 1277 1278 uint32 thash1 = 0x2edab8c9; 1279 uint32 thash2 = 0x1dc74fb8; 1280 uint32 thash3 = 0x1f241331; 1281 uint32 thash[NALLTYPES]; 1282 Init thashinit[] = 1283 { 1284 TXXX, 0x17527bbd, 0, 1285 TCHAR, 0x5cedd32b, 0, 1286 TUCHAR, 0x552c4454, 0, 1287 TSHORT, 0x63040b4b, 0, 1288 TUSHORT, 0x32a45878, 0, 1289 TINT, 0x4151d5bd, 0, 1290 TUINT, 0x5ae707d6, 0, 1291 TLONG, 0x5ef20f47, 0, 1292 TULONG, 0x36d8eb8f, 0, 1293 TVLONG, 0x6e5e9590, 0, 1294 TUVLONG, 0x75910105, 0, 1295 TFLOAT, 0x25fd7af1, 0, 1296 TDOUBLE, 0x7c40a1b2, 0, 1297 TIND, 0x1b832357, 0, 1298 TFUNC, 0x6babc9cb, 0, 1299 TARRAY, 0x7c50986d, 0, 1300 TVOID, 0x44112eff, 0, 1301 TSTRUCT, 0x7c2da3bf, 0, 1302 TUNION, 0x3eb25e98, 0, 1303 TENUM, 0x44b54f61, 0, 1304 TFILE, 0x19242ac3, 0, 1305 TOLD, 0x22b15988, 0, 1306 TDOT, 0x0204f6b3, 0, 1307 -1, 0, 0, 1308 }; 1309 1310 char* bnames[NALIGN]; 1311 Init bnamesinit[] = 1312 { 1313 Axxx, 0, "Axxx", 1314 Ael1, 0, "el1", 1315 Ael2, 0, "el2", 1316 Asu2, 0, "su2", 1317 Aarg0, 0, "arg0", 1318 Aarg1, 0, "arg1", 1319 Aarg2, 0, "arg2", 1320 Aaut3, 0, "aut3", 1321 -1, 0, 0, 1322 }; 1323 1324 char* tnames[NALLTYPES]; 1325 Init tnamesinit[] = 1326 { 1327 TXXX, 0, "TXXX", 1328 TCHAR, 0, "CHAR", 1329 TUCHAR, 0, "UCHAR", 1330 TSHORT, 0, "SHORT", 1331 TUSHORT, 0, "USHORT", 1332 TINT, 0, "INT", 1333 TUINT, 0, "UINT", 1334 TLONG, 0, "LONG", 1335 TULONG, 0, "ULONG", 1336 TVLONG, 0, "VLONG", 1337 TUVLONG, 0, "UVLONG", 1338 TFLOAT, 0, "FLOAT", 1339 TDOUBLE, 0, "DOUBLE", 1340 TIND, 0, "IND", 1341 TFUNC, 0, "FUNC", 1342 TARRAY, 0, "ARRAY", 1343 TVOID, 0, "VOID", 1344 TSTRUCT, 0, "STRUCT", 1345 TUNION, 0, "UNION", 1346 TENUM, 0, "ENUM", 1347 TFILE, 0, "FILE", 1348 TOLD, 0, "OLD", 1349 TDOT, 0, "DOT", 1350 -1, 0, 0, 1351 }; 1352 1353 char* gnames[NGTYPES]; 1354 Init gnamesinit[] = 1355 { 1356 GXXX, 0, "GXXX", 1357 GCONSTNT, 0, "CONST", 1358 GVOLATILE, 0, "VOLATILE", 1359 GVOLATILE|GCONSTNT, 0, "CONST-VOLATILE", 1360 -1, 0, 0, 1361 }; 1362 1363 char* qnames[NALLTYPES]; 1364 Init qnamesinit[] = 1365 { 1366 TXXX, 0, "TXXX", 1367 TCHAR, 0, "CHAR", 1368 TUCHAR, 0, "UCHAR", 1369 TSHORT, 0, "SHORT", 1370 TUSHORT, 0, "USHORT", 1371 TINT, 0, "INT", 1372 TUINT, 0, "UINT", 1373 TLONG, 0, "LONG", 1374 TULONG, 0, "ULONG", 1375 TVLONG, 0, "VLONG", 1376 TUVLONG, 0, "UVLONG", 1377 TFLOAT, 0, "FLOAT", 1378 TDOUBLE, 0, "DOUBLE", 1379 TIND, 0, "IND", 1380 TFUNC, 0, "FUNC", 1381 TARRAY, 0, "ARRAY", 1382 TVOID, 0, "VOID", 1383 TSTRUCT, 0, "STRUCT", 1384 TUNION, 0, "UNION", 1385 TENUM, 0, "ENUM", 1386 1387 TAUTO, 0, "AUTO", 1388 TEXTERN, 0, "EXTERN", 1389 TSTATIC, 0, "STATIC", 1390 TTYPEDEF, 0, "TYPEDEF", 1391 TTYPESTR, 0, "TYPESTR", 1392 TREGISTER, 0, "REGISTER", 1393 TCONSTNT, 0, "CONSTNT", 1394 TVOLATILE, 0, "VOLATILE", 1395 TUNSIGNED, 0, "UNSIGNED", 1396 TSIGNED, 0, "SIGNED", 1397 TDOT, 0, "DOT", 1398 TFILE, 0, "FILE", 1399 TOLD, 0, "OLD", 1400 -1, 0, 0, 1401 }; 1402 char* cnames[NCTYPES]; 1403 Init cnamesinit[] = 1404 { 1405 CXXX, 0, "CXXX", 1406 CAUTO, 0, "AUTO", 1407 CEXTERN, 0, "EXTERN", 1408 CGLOBL, 0, "GLOBL", 1409 CSTATIC, 0, "STATIC", 1410 CLOCAL, 0, "LOCAL", 1411 CTYPEDEF, 0, "TYPEDEF", 1412 CTYPESTR, 0, "TYPESTR", 1413 CPARAM, 0, "PARAM", 1414 CSELEM, 0, "SELEM", 1415 CLABEL, 0, "LABEL", 1416 CEXREG, 0, "EXREG", 1417 -1, 0, 0, 1418 }; 1419 1420 char* onames[OEND+1]; 1421 Init onamesinit[] = 1422 { 1423 OXXX, 0, "OXXX", 1424 OADD, 0, "ADD", 1425 OADDR, 0, "ADDR", 1426 OAND, 0, "AND", 1427 OANDAND, 0, "ANDAND", 1428 OARRAY, 0, "ARRAY", 1429 OAS, 0, "AS", 1430 OASI, 0, "ASI", 1431 OASADD, 0, "ASADD", 1432 OASAND, 0, "ASAND", 1433 OASASHL, 0, "ASASHL", 1434 OASASHR, 0, "ASASHR", 1435 OASDIV, 0, "ASDIV", 1436 OASHL, 0, "ASHL", 1437 OASHR, 0, "ASHR", 1438 OASLDIV, 0, "ASLDIV", 1439 OASLMOD, 0, "ASLMOD", 1440 OASLMUL, 0, "ASLMUL", 1441 OASLSHR, 0, "ASLSHR", 1442 OASMOD, 0, "ASMOD", 1443 OASMUL, 0, "ASMUL", 1444 OASOR, 0, "ASOR", 1445 OASSUB, 0, "ASSUB", 1446 OASXOR, 0, "ASXOR", 1447 OBIT, 0, "BIT", 1448 OBREAK, 0, "BREAK", 1449 OCASE, 0, "CASE", 1450 OCAST, 0, "CAST", 1451 OCOMMA, 0, "COMMA", 1452 OCOND, 0, "COND", 1453 OCONST, 0, "CONST", 1454 OCONTINUE, 0, "CONTINUE", 1455 ODIV, 0, "DIV", 1456 ODOT, 0, "DOT", 1457 ODOTDOT, 0, "DOTDOT", 1458 ODWHILE, 0, "DWHILE", 1459 OENUM, 0, "ENUM", 1460 OEQ, 0, "EQ", 1461 OEXREG, 0, "EXREG", 1462 OFOR, 0, "FOR", 1463 OFUNC, 0, "FUNC", 1464 OGE, 0, "GE", 1465 OGOTO, 0, "GOTO", 1466 OGT, 0, "GT", 1467 OHI, 0, "HI", 1468 OHS, 0, "HS", 1469 OIF, 0, "IF", 1470 OIND, 0, "IND", 1471 OINDREG, 0, "INDREG", 1472 OINIT, 0, "INIT", 1473 OLABEL, 0, "LABEL", 1474 OLDIV, 0, "LDIV", 1475 OLE, 0, "LE", 1476 OLIST, 0, "LIST", 1477 OLMOD, 0, "LMOD", 1478 OLMUL, 0, "LMUL", 1479 OLO, 0, "LO", 1480 OLS, 0, "LS", 1481 OLSHR, 0, "LSHR", 1482 OLT, 0, "LT", 1483 OMOD, 0, "MOD", 1484 OMUL, 0, "MUL", 1485 ONAME, 0, "NAME", 1486 ONE, 0, "NE", 1487 ONOT, 0, "NOT", 1488 OOR, 0, "OR", 1489 OOROR, 0, "OROR", 1490 OPOSTDEC, 0, "POSTDEC", 1491 OPOSTINC, 0, "POSTINC", 1492 OPREDEC, 0, "PREDEC", 1493 OPREINC, 0, "PREINC", 1494 OPREFETCH, 0, "PREFETCH", 1495 OPROTO, 0, "PROTO", 1496 OREGISTER, 0, "REGISTER", 1497 ORETURN, 0, "RETURN", 1498 OSET, 0, "SET", 1499 OSIGN, 0, "SIGN", 1500 OSIZE, 0, "SIZE", 1501 OSTRING, 0, "STRING", 1502 OLSTRING, 0, "LSTRING", 1503 OSTRUCT, 0, "STRUCT", 1504 OSUB, 0, "SUB", 1505 OSWITCH, 0, "SWITCH", 1506 OUNION, 0, "UNION", 1507 OUSED, 0, "USED", 1508 OWHILE, 0, "WHILE", 1509 OXOR, 0, "XOR", 1510 OPOS, 0, "POS", 1511 ONEG, 0, "NEG", 1512 OCOM, 0, "COM", 1513 OELEM, 0, "ELEM", 1514 OTST, 0, "TST", 1515 OINDEX, 0, "INDEX", 1516 OFAS, 0, "FAS", 1517 OREGPAIR, 0, "REGPAIR", 1518 OROTL, 0, "ROTL", 1519 OEND, 0, "END", 1520 -1, 0, 0, 1521 }; 1522 1523 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */ 1524 uchar comrel[12] = 1525 { 1526 ONE, OEQ, OGT, OHI, OGE, OHS, OLT, OLO, OLE, OLS, 1527 }; 1528 uchar invrel[12] = 1529 { 1530 OEQ, ONE, OGE, OHS, OGT, OHI, OLE, OLS, OLT, OLO, 1531 }; 1532 uchar logrel[12] = 1533 { 1534 OEQ, ONE, OLS, OLS, OLO, OLO, OHS, OHS, OHI, OHI, 1535 }; 1536 1537 uchar typei[NALLTYPES]; 1538 int typeiinit[] = 1539 { 1540 TCHAR, TUCHAR, TSHORT, TUSHORT, TINT, TUINT, TLONG, TULONG, TVLONG, TUVLONG, -1, 1541 }; 1542 uchar typeu[NALLTYPES]; 1543 int typeuinit[] = 1544 { 1545 TUCHAR, TUSHORT, TUINT, TULONG, TUVLONG, TIND, -1, 1546 }; 1547 1548 uchar typesuv[NALLTYPES]; 1549 int typesuvinit[] = 1550 { 1551 TVLONG, TUVLONG, TSTRUCT, TUNION, -1, 1552 }; 1553 1554 uchar typeilp[NALLTYPES]; 1555 int typeilpinit[] = 1556 { 1557 TINT, TUINT, TLONG, TULONG, TIND, -1 1558 }; 1559 1560 uchar typechl[NALLTYPES]; 1561 uchar typechlv[NALLTYPES]; 1562 uchar typechlvp[NALLTYPES]; 1563 int typechlinit[] = 1564 { 1565 TCHAR, TUCHAR, TSHORT, TUSHORT, TINT, TUINT, TLONG, TULONG, -1, 1566 }; 1567 1568 uchar typechlp[NALLTYPES]; 1569 int typechlpinit[] = 1570 { 1571 TCHAR, TUCHAR, TSHORT, TUSHORT, TINT, TUINT, TLONG, TULONG, TIND, -1, 1572 }; 1573 1574 uchar typechlpfd[NALLTYPES]; 1575 int typechlpfdinit[] = 1576 { 1577 TCHAR, TUCHAR, TSHORT, TUSHORT, TINT, TUINT, TLONG, TULONG, TFLOAT, TDOUBLE, TIND, -1, 1578 }; 1579 1580 uchar typec[NALLTYPES]; 1581 int typecinit[] = 1582 { 1583 TCHAR, TUCHAR, -1 1584 }; 1585 1586 uchar typeh[NALLTYPES]; 1587 int typehinit[] = 1588 { 1589 TSHORT, TUSHORT, -1, 1590 }; 1591 1592 uchar typeil[NALLTYPES]; 1593 int typeilinit[] = 1594 { 1595 TINT, TUINT, TLONG, TULONG, -1, 1596 }; 1597 1598 uchar typev[NALLTYPES]; 1599 int typevinit[] = 1600 { 1601 TVLONG, TUVLONG, -1, 1602 }; 1603 1604 uchar typefd[NALLTYPES]; 1605 int typefdinit[] = 1606 { 1607 TFLOAT, TDOUBLE, -1, 1608 }; 1609 1610 uchar typeaf[NALLTYPES]; 1611 int typeafinit[] = 1612 { 1613 TFUNC, TARRAY, -1, 1614 }; 1615 1616 uchar typesu[NALLTYPES]; 1617 int typesuinit[] = 1618 { 1619 TSTRUCT, TUNION, -1, 1620 }; 1621 1622 int32 tasign[NALLTYPES]; 1623 Init tasigninit[] = 1624 { 1625 TCHAR, BNUMBER, 0, 1626 TUCHAR, BNUMBER, 0, 1627 TSHORT, BNUMBER, 0, 1628 TUSHORT, BNUMBER, 0, 1629 TINT, BNUMBER, 0, 1630 TUINT, BNUMBER, 0, 1631 TLONG, BNUMBER, 0, 1632 TULONG, BNUMBER, 0, 1633 TVLONG, BNUMBER, 0, 1634 TUVLONG, BNUMBER, 0, 1635 TFLOAT, BNUMBER, 0, 1636 TDOUBLE, BNUMBER, 0, 1637 TIND, BIND, 0, 1638 TSTRUCT, BSTRUCT, 0, 1639 TUNION, BUNION, 0, 1640 -1, 0, 0, 1641 }; 1642 1643 int32 tasadd[NALLTYPES]; 1644 Init tasaddinit[] = 1645 { 1646 TCHAR, BNUMBER, 0, 1647 TUCHAR, BNUMBER, 0, 1648 TSHORT, BNUMBER, 0, 1649 TUSHORT, BNUMBER, 0, 1650 TINT, BNUMBER, 0, 1651 TUINT, BNUMBER, 0, 1652 TLONG, BNUMBER, 0, 1653 TULONG, BNUMBER, 0, 1654 TVLONG, BNUMBER, 0, 1655 TUVLONG, BNUMBER, 0, 1656 TFLOAT, BNUMBER, 0, 1657 TDOUBLE, BNUMBER, 0, 1658 TIND, BINTEGER, 0, 1659 -1, 0, 0, 1660 }; 1661 1662 int32 tcast[NALLTYPES]; 1663 Init tcastinit[] = 1664 { 1665 TCHAR, BNUMBER|BIND|BVOID, 0, 1666 TUCHAR, BNUMBER|BIND|BVOID, 0, 1667 TSHORT, BNUMBER|BIND|BVOID, 0, 1668 TUSHORT, BNUMBER|BIND|BVOID, 0, 1669 TINT, BNUMBER|BIND|BVOID, 0, 1670 TUINT, BNUMBER|BIND|BVOID, 0, 1671 TLONG, BNUMBER|BIND|BVOID, 0, 1672 TULONG, BNUMBER|BIND|BVOID, 0, 1673 TVLONG, BNUMBER|BIND|BVOID, 0, 1674 TUVLONG, BNUMBER|BIND|BVOID, 0, 1675 TFLOAT, BNUMBER|BVOID, 0, 1676 TDOUBLE, BNUMBER|BVOID, 0, 1677 TIND, BINTEGER|BIND|BVOID, 0, 1678 TVOID, BVOID, 0, 1679 TSTRUCT, BSTRUCT|BVOID, 0, 1680 TUNION, BUNION|BVOID, 0, 1681 -1, 0, 0, 1682 }; 1683 1684 int32 tadd[NALLTYPES]; 1685 Init taddinit[] = 1686 { 1687 TCHAR, BNUMBER|BIND, 0, 1688 TUCHAR, BNUMBER|BIND, 0, 1689 TSHORT, BNUMBER|BIND, 0, 1690 TUSHORT, BNUMBER|BIND, 0, 1691 TINT, BNUMBER|BIND, 0, 1692 TUINT, BNUMBER|BIND, 0, 1693 TLONG, BNUMBER|BIND, 0, 1694 TULONG, BNUMBER|BIND, 0, 1695 TVLONG, BNUMBER|BIND, 0, 1696 TUVLONG, BNUMBER|BIND, 0, 1697 TFLOAT, BNUMBER, 0, 1698 TDOUBLE, BNUMBER, 0, 1699 TIND, BINTEGER, 0, 1700 -1, 0, 0, 1701 }; 1702 1703 int32 tsub[NALLTYPES]; 1704 Init tsubinit[] = 1705 { 1706 TCHAR, BNUMBER, 0, 1707 TUCHAR, BNUMBER, 0, 1708 TSHORT, BNUMBER, 0, 1709 TUSHORT, BNUMBER, 0, 1710 TINT, BNUMBER, 0, 1711 TUINT, BNUMBER, 0, 1712 TLONG, BNUMBER, 0, 1713 TULONG, BNUMBER, 0, 1714 TVLONG, BNUMBER, 0, 1715 TUVLONG, BNUMBER, 0, 1716 TFLOAT, BNUMBER, 0, 1717 TDOUBLE, BNUMBER, 0, 1718 TIND, BINTEGER|BIND, 0, 1719 -1, 0, 0, 1720 }; 1721 1722 int32 tmul[NALLTYPES]; 1723 Init tmulinit[] = 1724 { 1725 TCHAR, BNUMBER, 0, 1726 TUCHAR, BNUMBER, 0, 1727 TSHORT, BNUMBER, 0, 1728 TUSHORT, BNUMBER, 0, 1729 TINT, BNUMBER, 0, 1730 TUINT, BNUMBER, 0, 1731 TLONG, BNUMBER, 0, 1732 TULONG, BNUMBER, 0, 1733 TVLONG, BNUMBER, 0, 1734 TUVLONG, BNUMBER, 0, 1735 TFLOAT, BNUMBER, 0, 1736 TDOUBLE, BNUMBER, 0, 1737 -1, 0, 0, 1738 }; 1739 1740 int32 tand[NALLTYPES]; 1741 Init tandinit[] = 1742 { 1743 TCHAR, BINTEGER, 0, 1744 TUCHAR, BINTEGER, 0, 1745 TSHORT, BINTEGER, 0, 1746 TUSHORT, BINTEGER, 0, 1747 TINT, BNUMBER, 0, 1748 TUINT, BNUMBER, 0, 1749 TLONG, BINTEGER, 0, 1750 TULONG, BINTEGER, 0, 1751 TVLONG, BINTEGER, 0, 1752 TUVLONG, BINTEGER, 0, 1753 -1, 0, 0, 1754 }; 1755 1756 int32 trel[NALLTYPES]; 1757 Init trelinit[] = 1758 { 1759 TCHAR, BNUMBER, 0, 1760 TUCHAR, BNUMBER, 0, 1761 TSHORT, BNUMBER, 0, 1762 TUSHORT, BNUMBER, 0, 1763 TINT, BNUMBER, 0, 1764 TUINT, BNUMBER, 0, 1765 TLONG, BNUMBER, 0, 1766 TULONG, BNUMBER, 0, 1767 TVLONG, BNUMBER, 0, 1768 TUVLONG, BNUMBER, 0, 1769 TFLOAT, BNUMBER, 0, 1770 TDOUBLE, BNUMBER, 0, 1771 TIND, BIND, 0, 1772 -1, 0, 0, 1773 }; 1774 1775 int32 tfunct[1] = 1776 { 1777 BFUNC, 1778 }; 1779 1780 int32 tindir[1] = 1781 { 1782 BIND, 1783 }; 1784 1785 int32 tdot[1] = 1786 { 1787 BSTRUCT|BUNION, 1788 }; 1789 1790 int32 tnot[1] = 1791 { 1792 BNUMBER|BIND, 1793 }; 1794 1795 int32 targ[1] = 1796 { 1797 BNUMBER|BIND|BSTRUCT|BUNION, 1798 }; 1799 1800 uchar tab[NTYPE][NTYPE] = 1801 { 1802 /*TXXX*/ { 0, 1803 }, 1804 1805 /*TCHAR*/ { 0, TCHAR, TUCHAR, TSHORT, TUSHORT, TINT, TUINT, TLONG, 1806 TULONG, TVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1807 }, 1808 /*TUCHAR*/ { 0, TUCHAR, TUCHAR, TUSHORT, TUSHORT, TUINT, TUINT, TULONG, 1809 TULONG, TUVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1810 }, 1811 /*TSHORT*/ { 0, TSHORT, TUSHORT, TSHORT, TUSHORT, TINT, TUINT, TLONG, 1812 TULONG, TVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1813 }, 1814 /*TUSHORT*/ { 0, TUSHORT, TUSHORT, TUSHORT, TUSHORT, TUINT, TUINT, TULONG, 1815 TULONG, TUVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1816 }, 1817 /*TINT*/ { 0, TINT, TUINT, TINT, TUINT, TINT, TUINT, TLONG, 1818 TULONG, TVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1819 }, 1820 /*TUINT*/ { 0, TUINT, TUINT, TUINT, TUINT, TUINT, TUINT, TULONG, 1821 TULONG, TUVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1822 }, 1823 /*TLONG*/ { 0, TLONG, TULONG, TLONG, TULONG, TLONG, TULONG, TLONG, 1824 TULONG, TVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1825 }, 1826 /*TULONG*/ { 0, TULONG, TULONG, TULONG, TULONG, TULONG, TULONG, TULONG, 1827 TULONG, TUVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1828 }, 1829 /*TVLONG*/ { 0, TVLONG, TUVLONG, TVLONG, TUVLONG, TVLONG, TUVLONG, TVLONG, 1830 TUVLONG, TVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1831 }, 1832 /*TUVLONG*/ { 0, TUVLONG, TUVLONG, TUVLONG, TUVLONG, TUVLONG, TUVLONG, TUVLONG, 1833 TUVLONG, TUVLONG, TUVLONG, TFLOAT, TDOUBLE, TIND, 1834 }, 1835 /*TFLOAT*/ { 0, TFLOAT, TFLOAT, TFLOAT, TFLOAT, TFLOAT, TFLOAT, TFLOAT, 1836 TFLOAT, TFLOAT, TFLOAT, TFLOAT, TDOUBLE, TIND, 1837 }, 1838 /*TDOUBLE*/ { 0, TDOUBLE, TDOUBLE, TDOUBLE, TDOUBLE, TDOUBLE, TDOUBLE, TDOUBLE, 1839 TDOUBLE, TDOUBLE, TDOUBLE, TFLOAT, TDOUBLE, TIND, 1840 }, 1841 /*TIND*/ { 0, TIND, TIND, TIND, TIND, TIND, TIND, TIND, 1842 TIND, TIND, TIND, TIND, TIND, TIND, 1843 }, 1844 }; 1845 1846 void 1847 urk(char *name, int max, int i) 1848 { 1849 if(i >= max) { 1850 fprint(2, "bad tinit: %s %d>=%d\n", name, i, max); 1851 exits("init"); 1852 } 1853 } 1854 1855 void 1856 tinit(void) 1857 { 1858 int *ip; 1859 Init *p; 1860 1861 for(p=thashinit; p->code >= 0; p++) { 1862 urk("thash", nelem(thash), p->code); 1863 thash[p->code] = p->value; 1864 } 1865 for(p=bnamesinit; p->code >= 0; p++) { 1866 urk("bnames", nelem(bnames), p->code); 1867 bnames[p->code] = p->s; 1868 } 1869 for(p=tnamesinit; p->code >= 0; p++) { 1870 urk("tnames", nelem(tnames), p->code); 1871 tnames[p->code] = p->s; 1872 } 1873 for(p=gnamesinit; p->code >= 0; p++) { 1874 urk("gnames", nelem(gnames), p->code); 1875 gnames[p->code] = p->s; 1876 } 1877 for(p=qnamesinit; p->code >= 0; p++) { 1878 urk("qnames", nelem(qnames), p->code); 1879 qnames[p->code] = p->s; 1880 } 1881 for(p=cnamesinit; p->code >= 0; p++) { 1882 urk("cnames", nelem(cnames), p->code); 1883 cnames[p->code] = p->s; 1884 } 1885 for(p=onamesinit; p->code >= 0; p++) { 1886 urk("onames", nelem(onames), p->code); 1887 onames[p->code] = p->s; 1888 } 1889 for(ip=typeiinit; *ip>=0; ip++) { 1890 urk("typei", nelem(typei), *ip); 1891 typei[*ip] = 1; 1892 } 1893 for(ip=typeuinit; *ip>=0; ip++) { 1894 urk("typeu", nelem(typeu), *ip); 1895 typeu[*ip] = 1; 1896 } 1897 for(ip=typesuvinit; *ip>=0; ip++) { 1898 urk("typesuv", nelem(typesuv), *ip); 1899 typesuv[*ip] = 1; 1900 } 1901 for(ip=typeilpinit; *ip>=0; ip++) { 1902 urk("typeilp", nelem(typeilp), *ip); 1903 typeilp[*ip] = 1; 1904 } 1905 for(ip=typechlinit; *ip>=0; ip++) { 1906 urk("typechl", nelem(typechl), *ip); 1907 typechl[*ip] = 1; 1908 typechlv[*ip] = 1; 1909 typechlvp[*ip] = 1; 1910 } 1911 for(ip=typechlpinit; *ip>=0; ip++) { 1912 urk("typechlp", nelem(typechlp), *ip); 1913 typechlp[*ip] = 1; 1914 typechlvp[*ip] = 1; 1915 } 1916 for(ip=typechlpfdinit; *ip>=0; ip++) { 1917 urk("typechlpfd", nelem(typechlpfd), *ip); 1918 typechlpfd[*ip] = 1; 1919 } 1920 for(ip=typecinit; *ip>=0; ip++) { 1921 urk("typec", nelem(typec), *ip); 1922 typec[*ip] = 1; 1923 } 1924 for(ip=typehinit; *ip>=0; ip++) { 1925 urk("typeh", nelem(typeh), *ip); 1926 typeh[*ip] = 1; 1927 } 1928 for(ip=typeilinit; *ip>=0; ip++) { 1929 urk("typeil", nelem(typeil), *ip); 1930 typeil[*ip] = 1; 1931 } 1932 for(ip=typevinit; *ip>=0; ip++) { 1933 urk("typev", nelem(typev), *ip); 1934 typev[*ip] = 1; 1935 typechlv[*ip] = 1; 1936 typechlvp[*ip] = 1; 1937 } 1938 for(ip=typefdinit; *ip>=0; ip++) { 1939 urk("typefd", nelem(typefd), *ip); 1940 typefd[*ip] = 1; 1941 } 1942 for(ip=typeafinit; *ip>=0; ip++) { 1943 urk("typeaf", nelem(typeaf), *ip); 1944 typeaf[*ip] = 1; 1945 } 1946 for(ip=typesuinit; *ip >= 0; ip++) { 1947 urk("typesu", nelem(typesu), *ip); 1948 typesu[*ip] = 1; 1949 } 1950 for(p=tasigninit; p->code >= 0; p++) { 1951 urk("tasign", nelem(tasign), p->code); 1952 tasign[p->code] = p->value; 1953 } 1954 for(p=tasaddinit; p->code >= 0; p++) { 1955 urk("tasadd", nelem(tasadd), p->code); 1956 tasadd[p->code] = p->value; 1957 } 1958 for(p=tcastinit; p->code >= 0; p++) { 1959 urk("tcast", nelem(tcast), p->code); 1960 tcast[p->code] = p->value; 1961 } 1962 for(p=taddinit; p->code >= 0; p++) { 1963 urk("tadd", nelem(tadd), p->code); 1964 tadd[p->code] = p->value; 1965 } 1966 for(p=tsubinit; p->code >= 0; p++) { 1967 urk("tsub", nelem(tsub), p->code); 1968 tsub[p->code] = p->value; 1969 } 1970 for(p=tmulinit; p->code >= 0; p++) { 1971 urk("tmul", nelem(tmul), p->code); 1972 tmul[p->code] = p->value; 1973 } 1974 for(p=tandinit; p->code >= 0; p++) { 1975 urk("tand", nelem(tand), p->code); 1976 tand[p->code] = p->value; 1977 } 1978 for(p=trelinit; p->code >= 0; p++) { 1979 urk("trel", nelem(trel), p->code); 1980 trel[p->code] = p->value; 1981 } 1982 1983 /* 32-bit defaults */ 1984 typeword = typechlp; 1985 typecmplx = typesuv; 1986 } 1987 1988 /* 1989 * return 1 if it is impossible to jump into the middle of n. 1990 */ 1991 static int 1992 deadhead(Node *n, int caseok) 1993 { 1994 loop: 1995 if(n == Z) 1996 return 1; 1997 switch(n->op) { 1998 case OLIST: 1999 if(!deadhead(n->left, caseok)) 2000 return 0; 2001 rloop: 2002 n = n->right; 2003 goto loop; 2004 2005 case ORETURN: 2006 break; 2007 2008 case OLABEL: 2009 return 0; 2010 2011 case OGOTO: 2012 break; 2013 2014 case OCASE: 2015 if(!caseok) 2016 return 0; 2017 goto rloop; 2018 2019 case OSWITCH: 2020 return deadhead(n->right, 1); 2021 2022 case OWHILE: 2023 case ODWHILE: 2024 goto rloop; 2025 2026 case OFOR: 2027 goto rloop; 2028 2029 case OCONTINUE: 2030 break; 2031 2032 case OBREAK: 2033 break; 2034 2035 case OIF: 2036 return deadhead(n->right->left, caseok) && deadhead(n->right->right, caseok); 2037 2038 case OSET: 2039 case OUSED: 2040 break; 2041 } 2042 return 1; 2043 } 2044 2045 int 2046 deadheads(Node *c) 2047 { 2048 return deadhead(c->left, 0) && deadhead(c->right, 0); 2049 } 2050 2051 int 2052 mixedasop(Type *l, Type *r) 2053 { 2054 return !typefd[l->etype] && typefd[r->etype]; 2055 }