1 #include "cc.h" 2 3 typedef struct Com Com; 4 struct Com 5 { 6 int n; 7 Node *t[500]; 8 }; 9 10 int compar(Node*, int); 11 static void comma(Node*); 12 static Node* commas(Com*, Node*); 13 14 void 15 complex(Node *n) 16 { 17 18 if(n == Z) 19 return; 20 21 nearln = n->lineno; 22 if(debug['t']) 23 if(n->op != OCONST) 24 prtree(n, "pre complex"); 25 if(tcom(n)) 26 return; 27 if(debug['y'] || 1) 28 comma(n); 29 if(debug['t']) 30 if(n->op != OCONST) 31 prtree(n, "t complex"); 32 ccom(n); 33 if(debug['t']) 34 if(n->op != OCONST) 35 prtree(n, "c complex"); 36 acom(n); 37 if(debug['t']) 38 if(n->op != OCONST) 39 prtree(n, "a complex"); 40 xcom(n); 41 if(debug['t']) 42 if(n->op != OCONST) 43 prtree(n, "x complex"); 44 } 45 46 /* 47 * evaluate types 48 * evaluate lvalues (addable == 1) 49 */ 50 enum 51 { 52 ADDROF = 1<<0, 53 CASTOF = 1<<1, 54 ADDROP = 1<<2, 55 }; 56 57 int 58 tcom(Node *n) 59 { 60 61 return tcomo(n, ADDROF); 62 } 63 64 int 65 tcomo(Node *n, int f) 66 { 67 Node *l, *r; 68 Type *t; 69 int o; 70 71 if(n == Z) { 72 diag(Z, "Z in tcom"); 73 errorexit(); 74 } 75 n->addable = 0; 76 l = n->left; 77 r = n->right; 78 79 switch(n->op) { 80 default: 81 diag(n, "unknown op in type complex: %O", n->op); 82 goto bad; 83 84 case ODOTDOT: 85 /* 86 * tcom has already been called on this subtree 87 */ 88 *n = *n->left; 89 if(n->type == T) 90 goto bad; 91 break; 92 93 case OCAST: 94 if(n->type == T) 95 break; 96 if(n->type->width == types[TLONG]->width) { 97 if(tcomo(l, ADDROF|CASTOF)) 98 goto bad; 99 } else 100 if(tcom(l)) 101 goto bad; 102 if(isfunct(n)) 103 break; 104 if(tcompat(n, l->type, n->type, tcast)) 105 goto bad; 106 break; 107 108 case ORETURN: 109 if(l == Z) { 110 if(n->type->etype != TVOID) 111 warn(n, "null return of a typed function"); 112 break; 113 } 114 if(tcom(l)) 115 goto bad; 116 typeext(n->type, l); 117 if(tcompat(n, n->type, l->type, tasign)) 118 break; 119 constas(n, n->type, l->type); 120 if(!sametype(n->type, l->type)) { 121 l = new1(OCAST, l, Z); 122 l->type = n->type; 123 n->left = l; 124 } 125 break; 126 127 case OASI: /* same as as, but no test for const */ 128 n->op = OAS; 129 o = tcom(l); 130 if(o | tcom(r)) 131 goto bad; 132 133 typeext(l->type, r); 134 if(tlvalue(l) || tcompat(n, l->type, r->type, tasign)) 135 goto bad; 136 if(!sametype(l->type, r->type)) { 137 r = new1(OCAST, r, Z); 138 r->type = l->type; 139 n->right = r; 140 } 141 n->type = l->type; 142 break; 143 144 case OAS: 145 o = tcom(l); 146 if(o | tcom(r)) 147 goto bad; 148 if(tlvalue(l)) 149 goto bad; 150 if(isfunct(n)) 151 break; 152 typeext(l->type, r); 153 if(tcompat(n, l->type, r->type, tasign)) 154 goto bad; 155 constas(n, l->type, r->type); 156 if(!sametype(l->type, r->type)) { 157 r = new1(OCAST, r, Z); 158 r->type = l->type; 159 n->right = r; 160 } 161 n->type = l->type; 162 break; 163 164 case OASADD: 165 case OASSUB: 166 o = tcom(l); 167 if(o | tcom(r)) 168 goto bad; 169 if(tlvalue(l)) 170 goto bad; 171 if(isfunct(n)) 172 break; 173 typeext1(l->type, r); 174 if(tcompat(n, l->type, r->type, tasadd)) 175 goto bad; 176 constas(n, l->type, r->type); 177 t = l->type; 178 arith(n, 0); 179 while(n->left->op == OCAST) 180 n->left = n->left->left; 181 if(!sametype(t, n->type) && !mixedasop(t, n->type)) { 182 r = new1(OCAST, n->right, Z); 183 r->type = t; 184 n->right = r; 185 n->type = t; 186 } 187 break; 188 189 case OASMUL: 190 case OASLMUL: 191 case OASDIV: 192 case OASLDIV: 193 o = tcom(l); 194 if(o | tcom(r)) 195 goto bad; 196 if(tlvalue(l)) 197 goto bad; 198 if(isfunct(n)) 199 break; 200 typeext1(l->type, r); 201 if(tcompat(n, l->type, r->type, tmul)) 202 goto bad; 203 constas(n, l->type, r->type); 204 t = l->type; 205 arith(n, 0); 206 while(n->left->op == OCAST) 207 n->left = n->left->left; 208 if(!sametype(t, n->type) && !mixedasop(t, n->type)) { 209 r = new1(OCAST, n->right, Z); 210 r->type = t; 211 n->right = r; 212 n->type = t; 213 } 214 if(typeu[n->type->etype]) { 215 if(n->op == OASDIV) 216 n->op = OASLDIV; 217 if(n->op == OASMUL) 218 n->op = OASLMUL; 219 } 220 break; 221 222 case OASLSHR: 223 case OASASHR: 224 case OASASHL: 225 o = tcom(l); 226 if(o | tcom(r)) 227 goto bad; 228 if(tlvalue(l)) 229 goto bad; 230 if(isfunct(n)) 231 break; 232 if(tcompat(n, l->type, r->type, tand)) 233 goto bad; 234 n->type = l->type; 235 n->right = new1(OCAST, r, Z); 236 n->right->type = types[TINT]; 237 if(typeu[n->type->etype]) { 238 if(n->op == OASASHR) 239 n->op = OASLSHR; 240 } 241 break; 242 243 case OASMOD: 244 case OASLMOD: 245 case OASOR: 246 case OASAND: 247 case OASXOR: 248 o = tcom(l); 249 if(o | tcom(r)) 250 goto bad; 251 if(tlvalue(l)) 252 goto bad; 253 if(isfunct(n)) 254 break; 255 if(tcompat(n, l->type, r->type, tand)) 256 goto bad; 257 t = l->type; 258 arith(n, 0); 259 while(n->left->op == OCAST) 260 n->left = n->left->left; 261 if(!sametype(t, n->type) && !mixedasop(t, n->type)) { 262 r = new1(OCAST, n->right, Z); 263 r->type = t; 264 n->right = r; 265 n->type = t; 266 } 267 if(typeu[n->type->etype]) { 268 if(n->op == OASMOD) 269 n->op = OASLMOD; 270 } 271 break; 272 273 case OPREINC: 274 case OPREDEC: 275 case OPOSTINC: 276 case OPOSTDEC: 277 if(tcom(l)) 278 goto bad; 279 if(tlvalue(l)) 280 goto bad; 281 if(isfunct(n)) 282 break; 283 if(tcompat(n, l->type, types[TINT], tadd)) 284 goto bad; 285 n->type = l->type; 286 if(n->type->etype == TIND) 287 if(n->type->link->width < 1) { 288 snap(n->type->link); 289 if(n->type->link->width < 1) 290 diag(n, "inc/dec of a void pointer"); 291 } 292 break; 293 294 case OEQ: 295 case ONE: 296 o = tcom(l); 297 if(o | tcom(r)) 298 goto bad; 299 if(isfunct(n)) 300 break; 301 typeext(l->type, r); 302 typeext(r->type, l); 303 if(tcompat(n, l->type, r->type, trel)) 304 goto bad; 305 arith(n, 0); 306 n->type = types[TINT]; 307 break; 308 309 case OLT: 310 case OGE: 311 case OGT: 312 case OLE: 313 o = tcom(l); 314 if(o | tcom(r)) 315 goto bad; 316 if(isfunct(n)) 317 break; 318 typeext1(l->type, r); 319 typeext1(r->type, l); 320 if(tcompat(n, l->type, r->type, trel)) 321 goto bad; 322 arith(n, 0); 323 if(typeu[n->type->etype]) 324 n->op = logrel[relindex(n->op)]; 325 n->type = types[TINT]; 326 break; 327 328 case OCOND: 329 o = tcom(l); 330 o |= tcom(r->left); 331 if(o | tcom(r->right)) 332 goto bad; 333 if(r->right->type->etype == TIND && vconst(r->left) == 0) { 334 r->left->type = r->right->type; 335 r->left->vconst = 0; 336 } 337 if(r->left->type->etype == TIND && vconst(r->right) == 0) { 338 r->right->type = r->left->type; 339 r->right->vconst = 0; 340 } 341 if(sametype(r->right->type, r->left->type)) { 342 r->type = r->right->type; 343 n->type = r->type; 344 break; 345 } 346 if(tcompat(r, r->left->type, r->right->type, trel)) 347 goto bad; 348 arith(r, 0); 349 n->type = r->type; 350 break; 351 352 case OADD: 353 o = tcom(l); 354 if(o | tcom(r)) 355 goto bad; 356 if(isfunct(n)) 357 break; 358 if(tcompat(n, l->type, r->type, tadd)) 359 goto bad; 360 arith(n, 1); 361 break; 362 363 case OSUB: 364 o = tcom(l); 365 if(o | tcom(r)) 366 goto bad; 367 if(isfunct(n)) 368 break; 369 if(tcompat(n, l->type, r->type, tsub)) 370 goto bad; 371 arith(n, 1); 372 break; 373 374 case OMUL: 375 case OLMUL: 376 case ODIV: 377 case OLDIV: 378 o = tcom(l); 379 if(o | tcom(r)) 380 goto bad; 381 if(isfunct(n)) 382 break; 383 if(tcompat(n, l->type, r->type, tmul)) 384 goto bad; 385 arith(n, 1); 386 if(typeu[n->type->etype]) { 387 if(n->op == ODIV) 388 n->op = OLDIV; 389 if(n->op == OMUL) 390 n->op = OLMUL; 391 } 392 break; 393 394 case OLSHR: 395 case OASHL: 396 case OASHR: 397 o = tcom(l); 398 if(o | tcom(r)) 399 goto bad; 400 if(isfunct(n)) 401 break; 402 if(tcompat(n, l->type, r->type, tand)) 403 goto bad; 404 n->right = Z; 405 arith(n, 1); 406 n->right = new1(OCAST, r, Z); 407 n->right->type = types[TINT]; 408 if(typeu[n->type->etype]) 409 if(n->op == OASHR) 410 n->op = OLSHR; 411 break; 412 413 case OAND: 414 case OOR: 415 case OXOR: 416 o = tcom(l); 417 if(o | tcom(r)) 418 goto bad; 419 if(isfunct(n)) 420 break; 421 if(tcompat(n, l->type, r->type, tand)) 422 goto bad; 423 arith(n, 1); 424 break; 425 426 case OMOD: 427 case OLMOD: 428 o = tcom(l); 429 if(o | tcom(r)) 430 goto bad; 431 if(isfunct(n)) 432 break; 433 if(tcompat(n, l->type, r->type, tand)) 434 goto bad; 435 arith(n, 1); 436 if(typeu[n->type->etype]) 437 n->op = OLMOD; 438 break; 439 440 case OPOS: 441 if(tcom(l)) 442 goto bad; 443 if(isfunct(n)) 444 break; 445 446 r = l; 447 l = new(OCONST, Z, Z); 448 l->vconst = 0; 449 l->type = types[TINT]; 450 n->op = OADD; 451 n->right = r; 452 n->left = l; 453 454 if(tcom(l)) 455 goto bad; 456 if(tcompat(n, l->type, r->type, tsub)) 457 goto bad; 458 arith(n, 1); 459 break; 460 461 case ONEG: 462 if(tcom(l)) 463 goto bad; 464 if(isfunct(n)) 465 break; 466 467 if(!machcap(n)) { 468 r = l; 469 l = new(OCONST, Z, Z); 470 l->vconst = 0; 471 l->type = types[TINT]; 472 n->op = OSUB; 473 n->right = r; 474 n->left = l; 475 476 if(tcom(l)) 477 goto bad; 478 if(tcompat(n, l->type, r->type, tsub)) 479 goto bad; 480 } 481 arith(n, 1); 482 break; 483 484 case OCOM: 485 if(tcom(l)) 486 goto bad; 487 if(isfunct(n)) 488 break; 489 490 if(!machcap(n)) { 491 r = l; 492 l = new(OCONST, Z, Z); 493 l->vconst = -1; 494 l->type = types[TINT]; 495 n->op = OXOR; 496 n->right = r; 497 n->left = l; 498 499 if(tcom(l)) 500 goto bad; 501 if(tcompat(n, l->type, r->type, tand)) 502 goto bad; 503 } 504 arith(n, 1); 505 break; 506 507 case ONOT: 508 if(tcom(l)) 509 goto bad; 510 if(isfunct(n)) 511 break; 512 if(tcompat(n, T, l->type, tnot)) 513 goto bad; 514 n->type = types[TINT]; 515 break; 516 517 case OANDAND: 518 case OOROR: 519 o = tcom(l); 520 if(o | tcom(r)) 521 goto bad; 522 if(tcompat(n, T, l->type, tnot) | 523 tcompat(n, T, r->type, tnot)) 524 goto bad; 525 n->type = types[TINT]; 526 break; 527 528 case OCOMMA: 529 o = tcom(l); 530 if(o | tcom(r)) 531 goto bad; 532 n->type = r->type; 533 break; 534 535 536 case OSIGN: /* extension signof(type) returns a hash */ 537 if(l != Z) { 538 if(l->op != OSTRING && l->op != OLSTRING) 539 if(tcomo(l, 0)) 540 goto bad; 541 if(l->op == OBIT) { 542 diag(n, "signof bitfield"); 543 goto bad; 544 } 545 n->type = l->type; 546 } 547 if(n->type == T) 548 goto bad; 549 if(n->type->width < 0) { 550 diag(n, "signof undefined type"); 551 goto bad; 552 } 553 n->op = OCONST; 554 n->left = Z; 555 n->right = Z; 556 n->vconst = convvtox(signature(n->type), TULONG); 557 n->type = types[TULONG]; 558 break; 559 560 case OSIZE: 561 if(l != Z) { 562 if(l->op != OSTRING && l->op != OLSTRING) 563 if(tcomo(l, 0)) 564 goto bad; 565 if(l->op == OBIT) { 566 diag(n, "sizeof bitfield"); 567 goto bad; 568 } 569 n->type = l->type; 570 } 571 if(n->type == T) 572 goto bad; 573 if(n->type->width <= 0) { 574 diag(n, "sizeof undefined type"); 575 goto bad; 576 } 577 if(n->type->etype == TFUNC) { 578 diag(n, "sizeof function"); 579 goto bad; 580 } 581 n->op = OCONST; 582 n->left = Z; 583 n->right = Z; 584 n->vconst = convvtox(n->type->width, TINT); 585 n->type = types[TINT]; 586 break; 587 588 case OFUNC: 589 o = tcomo(l, 0); 590 if(o) 591 goto bad; 592 if(l->type->etype == TIND && l->type->link->etype == TFUNC) { 593 l = new1(OIND, l, Z); 594 l->type = l->left->type->link; 595 n->left = l; 596 } 597 if(tcompat(n, T, l->type, tfunct)) 598 goto bad; 599 if(o | tcoma(l, r, l->type->down, 1)) 600 goto bad; 601 n->type = l->type->link; 602 if(!debug['B']) 603 if(l->type->down == T || l->type->down->etype == TOLD) { 604 nerrors--; 605 diag(n, "function args not checked: %F", l); 606 } 607 dpcheck(n); 608 break; 609 610 case ONAME: 611 if(n->type == T) { 612 diag(n, "name not declared: %F", n); 613 goto bad; 614 } 615 if(n->type->etype == TENUM) { 616 n->op = OCONST; 617 n->type = n->sym->tenum; 618 if(!typefd[n->type->etype]) 619 n->vconst = n->sym->vconst; 620 else 621 n->fconst = n->sym->fconst; 622 break; 623 } 624 n->addable = 1; 625 if(n->class == CEXREG) { 626 n->op = OREGISTER; 627 if(thechar == '8') 628 n->op = OEXREG; 629 n->reg = n->sym->offset; 630 n->xoffset = 0; 631 break; 632 } 633 break; 634 635 case OLSTRING: 636 if(n->type->link != types[TUSHORT]) { 637 o = outstring(0, 0); 638 while(o & 3) { 639 outlstring(L"", sizeof(ushort)); 640 o = outlstring(0, 0); 641 } 642 } 643 n->op = ONAME; 644 n->xoffset = outlstring(n->rstring, n->type->width); 645 n->addable = 1; 646 break; 647 648 case OSTRING: 649 if(n->type->link != types[TCHAR]) { 650 o = outstring(0, 0); 651 while(o & 3) { 652 outstring("", 1); 653 o = outstring(0, 0); 654 } 655 } 656 n->op = ONAME; 657 n->xoffset = outstring(n->cstring, n->type->width); 658 n->addable = 1; 659 break; 660 661 case OCONST: 662 break; 663 664 case ODOT: 665 if(tcom(l)) 666 goto bad; 667 if(tcompat(n, T, l->type, tdot)) 668 goto bad; 669 if(tcomd(n)) 670 goto bad; 671 break; 672 673 case OADDR: 674 if(tcomo(l, ADDROP)) 675 goto bad; 676 if(tlvalue(l)) 677 goto bad; 678 if(l->type->nbits) { 679 diag(n, "address of a bit field"); 680 goto bad; 681 } 682 if(l->op == OREGISTER) { 683 diag(n, "address of a register"); 684 goto bad; 685 } 686 n->type = typ(TIND, l->type); 687 n->type->width = types[TIND]->width; 688 break; 689 690 case OIND: 691 if(tcom(l)) 692 goto bad; 693 if(tcompat(n, T, l->type, tindir)) 694 goto bad; 695 n->type = l->type->link; 696 n->addable = 1; 697 break; 698 699 case OSTRUCT: 700 if(tcomx(n)) 701 goto bad; 702 break; 703 } 704 t = n->type; 705 if(t == T) 706 goto bad; 707 if(t->width < 0) { 708 snap(t); 709 if(t->width < 0) { 710 if(typesu[t->etype] && t->tag) 711 diag(n, "structure not fully declared %s", t->tag->name); 712 else 713 diag(n, "structure not fully declared"); 714 goto bad; 715 } 716 } 717 if(typeaf[t->etype]) { 718 if(f & ADDROF) 719 goto addaddr; 720 if(f & ADDROP) 721 warn(n, "address of array/func ignored"); 722 } 723 return 0; 724 725 addaddr: 726 if(tlvalue(n)) 727 goto bad; 728 l = new1(OXXX, Z, Z); 729 *l = *n; 730 n->op = OADDR; 731 if(l->type->etype == TARRAY) 732 l->type = l->type->link; 733 n->left = l; 734 n->right = Z; 735 n->addable = 0; 736 n->type = typ(TIND, l->type); 737 n->type->width = types[TIND]->width; 738 return 0; 739 740 bad: 741 n->type = T; 742 return 1; 743 } 744 745 int 746 tcoma(Node *l, Node *n, Type *t, int f) 747 { 748 Node *n1; 749 int o; 750 751 if(t != T) 752 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */ 753 t = T; 754 if(n == Z) { 755 if(t != T && !sametype(t, types[TVOID])) { 756 diag(n, "not enough function arguments: %F", l); 757 return 1; 758 } 759 return 0; 760 } 761 if(n->op == OLIST) { 762 o = tcoma(l, n->left, t, 0); 763 if(t != T) { 764 t = t->down; 765 if(t == T) 766 t = types[TVOID]; 767 } 768 return o | tcoma(l, n->right, t, 1); 769 } 770 if(f && t != T) 771 tcoma(l, Z, t->down, 0); 772 if(tcom(n) || tcompat(n, T, n->type, targ)) 773 return 1; 774 if(sametype(t, types[TVOID])) { 775 diag(n, "too many function arguments: %F", l); 776 return 1; 777 } 778 if(t != T) { 779 typeext(t, n); 780 if(stcompat(nodproto, t, n->type, tasign)) { 781 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F", 782 n->type, t, l); 783 return 1; 784 } 785 switch(t->etype) { 786 case TCHAR: 787 case TSHORT: 788 t = types[TINT]; 789 break; 790 791 case TUCHAR: 792 case TUSHORT: 793 t = types[TUINT]; 794 break; 795 } 796 } else 797 switch(n->type->etype) 798 { 799 case TCHAR: 800 case TSHORT: 801 t = types[TINT]; 802 break; 803 804 case TUCHAR: 805 case TUSHORT: 806 t = types[TUINT]; 807 break; 808 809 case TFLOAT: 810 t = types[TDOUBLE]; 811 } 812 if(t != T && !sametype(t, n->type)) { 813 n1 = new1(OXXX, Z, Z); 814 *n1 = *n; 815 n->op = OCAST; 816 n->left = n1; 817 n->right = Z; 818 n->type = t; 819 n->addable = 0; 820 } 821 return 0; 822 } 823 824 int 825 tcomd(Node *n) 826 { 827 Type *t; 828 long o; 829 830 o = 0; 831 t = dotsearch(n->sym, n->left->type->link, n, &o); 832 if(t == T) { 833 diag(n, "not a member of struct/union: %F", n); 834 return 1; 835 } 836 makedot(n, t, o); 837 return 0; 838 } 839 840 int 841 tcomx(Node *n) 842 { 843 Type *t; 844 Node *l, *r, **ar, **al; 845 int e; 846 847 e = 0; 848 if(n->type->etype != TSTRUCT) { 849 diag(n, "constructor must be a structure"); 850 return 1; 851 } 852 l = invert(n->left); 853 n->left = l; 854 al = &n->left; 855 for(t = n->type->link; t != T; t = t->down) { 856 if(l == Z) { 857 diag(n, "constructor list too short"); 858 return 1; 859 } 860 if(l->op == OLIST) { 861 r = l->left; 862 ar = &l->left; 863 al = &l->right; 864 l = l->right; 865 } else { 866 r = l; 867 ar = al; 868 l = Z; 869 } 870 if(tcom(r)) 871 e++; 872 typeext(t, r); 873 if(tcompat(n, t, r->type, tasign)) 874 e++; 875 constas(n, t, r->type); 876 if(!e && !sametype(t, r->type)) { 877 r = new1(OCAST, r, Z); 878 r->type = t; 879 *ar = r; 880 } 881 } 882 if(l != Z) { 883 diag(n, "constructor list too long"); 884 return 1; 885 } 886 return e; 887 } 888 889 int 890 tlvalue(Node *n) 891 { 892 893 if(!n->addable) { 894 diag(n, "not an l-value"); 895 return 1; 896 } 897 return 0; 898 } 899 900 /* 901 * hoist comma operators out of expressions 902 * (a,b) OP c => (a, b OP c) 903 * OP(a,b) => (a, OP b) 904 * a OP (b,c) => (b, a OP c) 905 */ 906 907 static Node* 908 comargs(Com *com, Node *n) 909 { 910 if(n != Z && n->op == OLIST){ 911 n->left = comargs(com, n->left); 912 n->right = comargs(com, n->right); 913 } 914 return commas(com, n); 915 } 916 917 static Node* 918 commas(Com *com, Node *n) 919 { 920 Node *t; 921 922 if(n == Z) 923 return n; 924 switch(n->op){ 925 case OREGISTER: 926 case OINDREG: 927 case OCONST: 928 case ONAME: 929 case OSTRING: 930 /* leaf */ 931 return n; 932 933 case OCOMMA: 934 t = commas(com, n->left); 935 if(com->n >= nelem(com->t)) 936 fatal(n, "comma list overflow"); 937 com->t[com->n++] = t; 938 return commas(com, n->right); 939 940 case OFUNC: 941 n->left = commas(com, n->left); 942 n->right = comargs(com, n->right); 943 return n; 944 945 case OCOND: 946 n->left = commas(com, n->left); 947 comma(n->right->left); 948 comma(n->right->right); 949 return n; 950 951 case OANDAND: 952 case OOROR: 953 n->left = commas(com, n->left); 954 comma(n->right); 955 return n; 956 957 case ORETURN: 958 comma(n->left); 959 return n; 960 } 961 n->left = commas(com, n->left); 962 if(n->right != Z) 963 n->right = commas(com, n->right); 964 return n; 965 } 966 967 static void 968 comma(Node *n) 969 { 970 Com com; 971 Node *nn; 972 973 com.n = 0; 974 nn = commas(&com, n); 975 if(com.n > 0){ 976 if(debug['y'])print("n=%d\n", com.n); 977 if(debug['y']) prtree(nn, "res"); 978 if(nn != n) 979 *n = *nn; 980 while(com.n > 0){ 981 if(debug['y']) prtree(com.t[com.n-1], "tree"); 982 nn = new1(OXXX, Z, Z); 983 *nn = *n; 984 n->op = OCOMMA; 985 n->type = nn->type; 986 n->left = com.t[--com.n]; 987 n->right = nn; 988 n->lineno = n->left->lineno; 989 } 990 if(debug['y']) prtree(n, "final"); 991 }else if(n != nn) 992 fatal(n, "odd tree"); 993 } 994 995 /* 996 * general rewrite 997 * (IND(ADDR x)) ==> x 998 * (ADDR(IND x)) ==> x 999 * remove some zero operands 1000 * remove no op casts 1001 * evaluate constants 1002 */ 1003 void 1004 ccom(Node *n) 1005 { 1006 Node *l, *r; 1007 int t; 1008 1009 loop: 1010 if(n == Z) 1011 return; 1012 l = n->left; 1013 r = n->right; 1014 switch(n->op) { 1015 1016 case OAS: 1017 case OASXOR: 1018 case OASAND: 1019 case OASOR: 1020 case OASMOD: 1021 case OASLMOD: 1022 case OASLSHR: 1023 case OASASHR: 1024 case OASASHL: 1025 case OASDIV: 1026 case OASLDIV: 1027 case OASMUL: 1028 case OASLMUL: 1029 case OASSUB: 1030 case OASADD: 1031 ccom(l); 1032 ccom(r); 1033 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL) 1034 if(r->op == OCONST) { 1035 t = n->type->width * 8; /* bits per byte */ 1036 if(r->vconst >= t || r->vconst < 0) 1037 warn(n, "stupid shift: %lld", r->vconst); 1038 } 1039 break; 1040 1041 case OCAST: 1042 ccom(l); 1043 if(l->op == OCONST) { 1044 evconst(n); 1045 if(n->op == OCONST) 1046 break; 1047 } 1048 if(nocast(l->type, n->type) && 1049 (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) { 1050 l->type = n->type; 1051 *n = *l; 1052 } 1053 break; 1054 1055 case OCOND: 1056 ccom(l); 1057 ccom(r); 1058 if(l->op == OCONST) 1059 if(vconst(l) == 0) 1060 *n = *r->right; 1061 else 1062 *n = *r->left; 1063 break; 1064 1065 case OREGISTER: 1066 case OINDREG: 1067 case OCONST: 1068 case ONAME: 1069 break; 1070 1071 case OADDR: 1072 ccom(l); 1073 l->etype = TVOID; 1074 if(l->op == OIND) { 1075 l->left->type = n->type; 1076 *n = *l->left; 1077 break; 1078 } 1079 goto common; 1080 1081 case OIND: 1082 ccom(l); 1083 if(l->op == OADDR) { 1084 l->left->type = n->type; 1085 *n = *l->left; 1086 break; 1087 } 1088 goto common; 1089 1090 case OEQ: 1091 case ONE: 1092 1093 case OLE: 1094 case OGE: 1095 case OLT: 1096 case OGT: 1097 1098 case OLS: 1099 case OHS: 1100 case OLO: 1101 case OHI: 1102 ccom(l); 1103 ccom(r); 1104 if(compar(n, 0) || compar(n, 1)) 1105 break; 1106 relcon(l, r); 1107 relcon(r, l); 1108 goto common; 1109 1110 case OASHR: 1111 case OASHL: 1112 case OLSHR: 1113 ccom(l); 1114 if(vconst(l) == 0 && !side(r)) { 1115 *n = *l; 1116 break; 1117 } 1118 ccom(r); 1119 if(vconst(r) == 0) { 1120 *n = *l; 1121 break; 1122 } 1123 if(r->op == OCONST) { 1124 t = n->type->width * 8; /* bits per byte */ 1125 if(r->vconst >= t || r->vconst <= -t) 1126 warn(n, "stupid shift: %lld", r->vconst); 1127 } 1128 goto common; 1129 1130 case OMUL: 1131 case OLMUL: 1132 ccom(l); 1133 t = vconst(l); 1134 if(t == 0 && !side(r)) { 1135 *n = *l; 1136 break; 1137 } 1138 if(t == 1) { 1139 *n = *r; 1140 goto loop; 1141 } 1142 ccom(r); 1143 t = vconst(r); 1144 if(t == 0 && !side(l)) { 1145 *n = *r; 1146 break; 1147 } 1148 if(t == 1) { 1149 *n = *l; 1150 break; 1151 } 1152 goto common; 1153 1154 case ODIV: 1155 case OLDIV: 1156 ccom(l); 1157 if(vconst(l) == 0 && !side(r)) { 1158 *n = *l; 1159 break; 1160 } 1161 ccom(r); 1162 t = vconst(r); 1163 if(t == 0) { 1164 diag(n, "divide check"); 1165 *n = *r; 1166 break; 1167 } 1168 if(t == 1) { 1169 *n = *l; 1170 break; 1171 } 1172 goto common; 1173 1174 case OSUB: 1175 ccom(r); 1176 if(r->op == OCONST) { 1177 if(typefd[r->type->etype]) { 1178 n->op = OADD; 1179 r->fconst = -r->fconst; 1180 goto loop; 1181 } else { 1182 n->op = OADD; 1183 r->vconst = -r->vconst; 1184 goto loop; 1185 } 1186 } 1187 ccom(l); 1188 goto common; 1189 1190 case OXOR: 1191 case OOR: 1192 case OADD: 1193 ccom(l); 1194 if(vconst(l) == 0) { 1195 *n = *r; 1196 goto loop; 1197 } 1198 ccom(r); 1199 if(vconst(r) == 0) { 1200 *n = *l; 1201 break; 1202 } 1203 goto commute; 1204 1205 case OAND: 1206 ccom(l); 1207 ccom(r); 1208 if(vconst(l) == 0 && !side(r)) { 1209 *n = *l; 1210 break; 1211 } 1212 if(vconst(r) == 0 && !side(l)) { 1213 *n = *r; 1214 break; 1215 } 1216 1217 commute: 1218 /* look for commutative constant */ 1219 if(r->op == OCONST) { 1220 if(l->op == n->op) { 1221 if(l->left->op == OCONST) { 1222 n->right = l->right; 1223 l->right = r; 1224 goto loop; 1225 } 1226 if(l->right->op == OCONST) { 1227 n->right = l->left; 1228 l->left = r; 1229 goto loop; 1230 } 1231 } 1232 } 1233 if(l->op == OCONST) { 1234 if(r->op == n->op) { 1235 if(r->left->op == OCONST) { 1236 n->left = r->right; 1237 r->right = l; 1238 goto loop; 1239 } 1240 if(r->right->op == OCONST) { 1241 n->left = r->left; 1242 r->left = l; 1243 goto loop; 1244 } 1245 } 1246 } 1247 goto common; 1248 1249 case OANDAND: 1250 ccom(l); 1251 if(vconst(l) == 0) { 1252 *n = *l; 1253 break; 1254 } 1255 ccom(r); 1256 goto common; 1257 1258 case OOROR: 1259 ccom(l); 1260 if(l->op == OCONST && l->vconst != 0) { 1261 *n = *l; 1262 n->vconst = 1; 1263 break; 1264 } 1265 ccom(r); 1266 goto common; 1267 1268 default: 1269 if(l != Z) 1270 ccom(l); 1271 if(r != Z) 1272 ccom(r); 1273 common: 1274 if(l != Z) 1275 if(l->op != OCONST) 1276 break; 1277 if(r != Z) 1278 if(r->op != OCONST) 1279 break; 1280 evconst(n); 1281 } 1282 } 1283 1284 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */ 1285 static char *cmps[12] = 1286 { 1287 "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">", 1288 }; 1289 1290 /* 128-bit numbers */ 1291 typedef struct Big Big; 1292 struct Big 1293 { 1294 vlong a; 1295 uvlong b; 1296 }; 1297 static int 1298 cmp(Big x, Big y) 1299 { 1300 if(x.a != y.a){ 1301 if(x.a < y.a) 1302 return -1; 1303 return 1; 1304 } 1305 if(x.b != y.b){ 1306 if(x.b < y.b) 1307 return -1; 1308 return 1; 1309 } 1310 return 0; 1311 } 1312 static Big 1313 add(Big x, int y) 1314 { 1315 uvlong ob; 1316 1317 ob = x.b; 1318 x.b += y; 1319 if(y > 0 && x.b < ob) 1320 x.a++; 1321 if(y < 0 && x.b > ob) 1322 x.a--; 1323 return x; 1324 } 1325 1326 Big 1327 big(vlong a, uvlong b) 1328 { 1329 Big x; 1330 1331 x.a = a; 1332 x.b = b; 1333 return x; 1334 } 1335 1336 int 1337 compar(Node *n, int reverse) 1338 { 1339 Big lo, hi, x; 1340 int op; 1341 char xbuf[40], cmpbuf[50]; 1342 Node *l, *r; 1343 Type *lt, *rt; 1344 1345 /* 1346 * The point of this function is to diagnose comparisons 1347 * that can never be true or that look misleading because 1348 * of the `usual arithmetic conversions'. As an example 1349 * of the latter, if x is a ulong, then if(x <= -1) really means 1350 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means 1351 * what it says (but 8c compiles it wrong anyway). 1352 */ 1353 1354 if(reverse){ 1355 r = n->left; 1356 l = n->right; 1357 op = comrel[relindex(n->op)]; 1358 }else{ 1359 l = n->left; 1360 r = n->right; 1361 op = n->op; 1362 } 1363 1364 /* 1365 * Skip over left casts to find out the original expression range. 1366 */ 1367 while(l->op == OCAST) 1368 l = l->left; 1369 if(l->op == OCONST) 1370 return 0; 1371 lt = l->type; 1372 if(l->op == ONAME && l->sym->type){ 1373 lt = l->sym->type; 1374 if(lt->etype == TARRAY) 1375 lt = lt->link; 1376 } 1377 if(lt == T) 1378 return 0; 1379 if(lt->etype == TXXX || lt->etype > TUVLONG) 1380 return 0; 1381 1382 /* 1383 * Skip over the right casts to find the on-screen value. 1384 */ 1385 if(r->op != OCONST) 1386 return 0; 1387 while(r->oldop == OCAST && !r->xcast) 1388 r = r->left; 1389 rt = r->type; 1390 if(rt == T) 1391 return 0; 1392 1393 x.b = r->vconst; 1394 x.a = 0; 1395 if((rt->etype&1) && r->vconst < 0) /* signed negative */ 1396 x.a = ~0ULL; 1397 1398 if((lt->etype&1)==0){ 1399 /* unsigned */ 1400 lo = big(0, 0); 1401 if(lt->width == 8) 1402 hi = big(0, ~0ULL); 1403 else 1404 hi = big(0, (1LL<<(l->type->width*8))-1); 1405 }else{ 1406 lo = big(~0ULL, -(1LL<<(l->type->width*8-1))); 1407 hi = big(0, (1LL<<(l->type->width*8-1))-1); 1408 } 1409 1410 switch(op){ 1411 case OLT: 1412 case OLO: 1413 case OGE: 1414 case OHS: 1415 if(cmp(x, lo) <= 0) 1416 goto useless; 1417 if(cmp(x, add(hi, 1)) >= 0) 1418 goto useless; 1419 break; 1420 case OLE: 1421 case OLS: 1422 case OGT: 1423 case OHI: 1424 if(cmp(x, add(lo, -1)) <= 0) 1425 goto useless; 1426 if(cmp(x, hi) >= 0) 1427 goto useless; 1428 break; 1429 case OEQ: 1430 case ONE: 1431 /* 1432 * Don't warn about comparisons if the expression 1433 * is as wide as the value: the compiler-supplied casts 1434 * will make both outcomes possible. 1435 */ 1436 if(lt->width >= rt->width && debug['w'] < 2) 1437 return 0; 1438 if(cmp(x, lo) < 0 || cmp(x, hi) > 0) 1439 goto useless; 1440 break; 1441 } 1442 return 0; 1443 1444 useless: 1445 if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL)) 1446 snprint(xbuf, sizeof xbuf, "%lld", x.b); 1447 else if(x.a == 0) 1448 snprint(xbuf, sizeof xbuf, "%#llux", x.b); 1449 else 1450 snprint(xbuf, sizeof xbuf, "%#llx", x.b); 1451 if(reverse) 1452 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T", 1453 xbuf, cmps[relindex(n->op)], lt); 1454 else 1455 snprint(cmpbuf, sizeof cmpbuf, "%T %s %s", 1456 lt, cmps[relindex(n->op)], xbuf); 1457 if(debug['y']) prtree(n, "strange"); 1458 warn(n, "useless or misleading comparison: %s", cmpbuf); 1459 return 0; 1460 } 1461 1462