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