github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/cmd/gc/const.c (about) 1 // Copyright 2009 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 #include <u.h> 6 #include <libc.h> 7 #include "go.h" 8 #define TUP(x,y) (((x)<<16)|(y)) 9 10 static Val tocplx(Val); 11 static Val toflt(Val); 12 static Val tostr(Val); 13 static Val copyval(Val); 14 static void cmplxmpy(Mpcplx*, Mpcplx*); 15 static void cmplxdiv(Mpcplx*, Mpcplx*); 16 17 /* 18 * truncate float literal fv to 32-bit or 64-bit precision 19 * according to type; return truncated value. 20 */ 21 Mpflt* 22 truncfltlit(Mpflt *oldv, Type *t) 23 { 24 double d; 25 float f; 26 Mpflt *fv; 27 28 if(t == T) 29 return oldv; 30 31 fv = mal(sizeof *fv); 32 *fv = *oldv; 33 34 // convert large precision literal floating 35 // into limited precision (float64 or float32) 36 // botch -- this assumes that compiler fp 37 // has same precision as runtime fp 38 switch(t->etype) { 39 case TFLOAT64: 40 d = mpgetflt(fv); 41 mpmovecflt(fv, d); 42 break; 43 44 case TFLOAT32: 45 d = mpgetflt(fv); 46 f = d; 47 d = f; 48 mpmovecflt(fv, d); 49 break; 50 } 51 return fv; 52 } 53 54 /* 55 * convert n, if literal, to type t. 56 * implicit conversion. 57 */ 58 void 59 convlit(Node **np, Type *t) 60 { 61 convlit1(np, t, 0); 62 } 63 64 /* 65 * convert n, if literal, to type t. 66 * return a new node if necessary 67 * (if n is a named constant, can't edit n->type directly). 68 */ 69 void 70 convlit1(Node **np, Type *t, int explicit) 71 { 72 int ct, et; 73 Node *n, *nn; 74 75 n = *np; 76 if(n == N || t == T || n->type == T || isideal(t) || n->type == t) 77 return; 78 if(!explicit && !isideal(n->type)) 79 return; 80 81 if(n->op == OLITERAL) { 82 nn = nod(OXXX, N, N); 83 *nn = *n; 84 n = nn; 85 *np = n; 86 } 87 88 switch(n->op) { 89 default: 90 if(n->type == idealbool) { 91 if(t->etype == TBOOL) 92 n->type = t; 93 else 94 n->type = types[TBOOL]; 95 } 96 if(n->type->etype == TIDEAL) { 97 convlit(&n->left, t); 98 convlit(&n->right, t); 99 n->type = t; 100 } 101 return; 102 case OLITERAL: 103 // target is invalid type for a constant? leave alone. 104 if(!okforconst[t->etype] && n->type->etype != TNIL) { 105 defaultlit(&n, T); 106 *np = n; 107 return; 108 } 109 break; 110 case OLSH: 111 case ORSH: 112 convlit1(&n->left, t, explicit && isideal(n->left->type)); 113 t = n->left->type; 114 if(t != T && t->etype == TIDEAL && n->val.ctype != CTINT) 115 n->val = toint(n->val); 116 if(t != T && !isint[t->etype]) { 117 yyerror("invalid operation: %N (shift of type %T)", n, t); 118 t = T; 119 } 120 n->type = t; 121 return; 122 case OCOMPLEX: 123 if(n->type->etype == TIDEAL) { 124 switch(t->etype) { 125 default: 126 // If trying to convert to non-complex type, 127 // leave as complex128 and let typechecker complain. 128 t = types[TCOMPLEX128]; 129 //fallthrough 130 case TCOMPLEX128: 131 n->type = t; 132 convlit(&n->left, types[TFLOAT64]); 133 convlit(&n->right, types[TFLOAT64]); 134 break; 135 case TCOMPLEX64: 136 n->type = t; 137 convlit(&n->left, types[TFLOAT32]); 138 convlit(&n->right, types[TFLOAT32]); 139 break; 140 } 141 } 142 return; 143 } 144 145 // avoided repeated calculations, errors 146 if(eqtype(n->type, t)) 147 return; 148 149 ct = consttype(n); 150 if(ct < 0) 151 goto bad; 152 153 et = t->etype; 154 if(et == TINTER) { 155 if(ct == CTNIL && n->type == types[TNIL]) { 156 n->type = t; 157 return; 158 } 159 defaultlit(np, T); 160 return; 161 } 162 163 switch(ct) { 164 default: 165 goto bad; 166 167 case CTNIL: 168 switch(et) { 169 default: 170 n->type = T; 171 goto bad; 172 173 case TSTRING: 174 // let normal conversion code handle it 175 return; 176 177 case TARRAY: 178 if(!isslice(t)) 179 goto bad; 180 break; 181 182 case TPTR32: 183 case TPTR64: 184 case TINTER: 185 case TMAP: 186 case TCHAN: 187 case TFUNC: 188 case TUNSAFEPTR: 189 break; 190 191 case TUINTPTR: 192 // A nil literal may be converted to uintptr 193 // if it is an unsafe.Pointer 194 if(n->type->etype == TUNSAFEPTR) { 195 n->val.u.xval = mal(sizeof(*n->val.u.xval)); 196 mpmovecfix(n->val.u.xval, 0); 197 n->val.ctype = CTINT; 198 } else 199 goto bad; 200 } 201 break; 202 203 case CTSTR: 204 case CTBOOL: 205 if(et != n->type->etype) 206 goto bad; 207 break; 208 209 case CTINT: 210 case CTRUNE: 211 case CTFLT: 212 case CTCPLX: 213 ct = n->val.ctype; 214 if(isint[et]) { 215 switch(ct) { 216 default: 217 goto bad; 218 case CTCPLX: 219 case CTFLT: 220 case CTRUNE: 221 n->val = toint(n->val); 222 // flowthrough 223 case CTINT: 224 overflow(n->val, t); 225 break; 226 } 227 } else 228 if(isfloat[et]) { 229 switch(ct) { 230 default: 231 goto bad; 232 case CTCPLX: 233 case CTINT: 234 case CTRUNE: 235 n->val = toflt(n->val); 236 // flowthrough 237 case CTFLT: 238 overflow(n->val, t); 239 n->val.u.fval = truncfltlit(n->val.u.fval, t); 240 break; 241 } 242 } else 243 if(iscomplex[et]) { 244 switch(ct) { 245 default: 246 goto bad; 247 case CTFLT: 248 case CTINT: 249 case CTRUNE: 250 n->val = tocplx(n->val); 251 break; 252 case CTCPLX: 253 overflow(n->val, t); 254 break; 255 } 256 } else 257 if(et == TSTRING && (ct == CTINT || ct == CTRUNE) && explicit) 258 n->val = tostr(n->val); 259 else 260 goto bad; 261 break; 262 } 263 n->type = t; 264 return; 265 266 bad: 267 if(!n->diag) { 268 if(!t->broke) 269 yyerror("cannot convert %N to type %T", n, t); 270 n->diag = 1; 271 } 272 if(isideal(n->type)) { 273 defaultlit(&n, T); 274 *np = n; 275 } 276 return; 277 } 278 279 static Val 280 copyval(Val v) 281 { 282 Mpint *i; 283 Mpflt *f; 284 Mpcplx *c; 285 286 switch(v.ctype) { 287 case CTINT: 288 case CTRUNE: 289 i = mal(sizeof(*i)); 290 mpmovefixfix(i, v.u.xval); 291 v.u.xval = i; 292 break; 293 case CTFLT: 294 f = mal(sizeof(*f)); 295 mpmovefltflt(f, v.u.fval); 296 v.u.fval = f; 297 break; 298 case CTCPLX: 299 c = mal(sizeof(*c)); 300 mpmovefltflt(&c->real, &v.u.cval->real); 301 mpmovefltflt(&c->imag, &v.u.cval->imag); 302 v.u.cval = c; 303 break; 304 } 305 return v; 306 } 307 308 static Val 309 tocplx(Val v) 310 { 311 Mpcplx *c; 312 313 switch(v.ctype) { 314 case CTINT: 315 case CTRUNE: 316 c = mal(sizeof(*c)); 317 mpmovefixflt(&c->real, v.u.xval); 318 mpmovecflt(&c->imag, 0.0); 319 v.ctype = CTCPLX; 320 v.u.cval = c; 321 break; 322 case CTFLT: 323 c = mal(sizeof(*c)); 324 mpmovefltflt(&c->real, v.u.fval); 325 mpmovecflt(&c->imag, 0.0); 326 v.ctype = CTCPLX; 327 v.u.cval = c; 328 break; 329 } 330 return v; 331 } 332 333 static Val 334 toflt(Val v) 335 { 336 Mpflt *f; 337 338 switch(v.ctype) { 339 case CTINT: 340 case CTRUNE: 341 f = mal(sizeof(*f)); 342 mpmovefixflt(f, v.u.xval); 343 v.ctype = CTFLT; 344 v.u.fval = f; 345 break; 346 case CTCPLX: 347 f = mal(sizeof(*f)); 348 mpmovefltflt(f, &v.u.cval->real); 349 if(mpcmpfltc(&v.u.cval->imag, 0) != 0) 350 yyerror("constant %#F%+#Fi truncated to real", &v.u.cval->real, &v.u.cval->imag); 351 v.ctype = CTFLT; 352 v.u.fval = f; 353 break; 354 } 355 return v; 356 } 357 358 Val 359 toint(Val v) 360 { 361 Mpint *i; 362 363 switch(v.ctype) { 364 case CTRUNE: 365 v.ctype = CTINT; 366 break; 367 case CTFLT: 368 i = mal(sizeof(*i)); 369 if(mpmovefltfix(i, v.u.fval) < 0) 370 yyerror("constant %#F truncated to integer", v.u.fval); 371 v.ctype = CTINT; 372 v.u.xval = i; 373 break; 374 case CTCPLX: 375 i = mal(sizeof(*i)); 376 if(mpmovefltfix(i, &v.u.cval->real) < 0) 377 yyerror("constant %#F%+#Fi truncated to integer", &v.u.cval->real, &v.u.cval->imag); 378 if(mpcmpfltc(&v.u.cval->imag, 0) != 0) 379 yyerror("constant %#F%+#Fi truncated to real", &v.u.cval->real, &v.u.cval->imag); 380 v.ctype = CTINT; 381 v.u.xval = i; 382 break; 383 } 384 return v; 385 } 386 387 int 388 doesoverflow(Val v, Type *t) 389 { 390 switch(v.ctype) { 391 case CTINT: 392 case CTRUNE: 393 if(!isint[t->etype]) 394 fatal("overflow: %T integer constant", t); 395 if(mpcmpfixfix(v.u.xval, minintval[t->etype]) < 0 || 396 mpcmpfixfix(v.u.xval, maxintval[t->etype]) > 0) 397 return 1; 398 break; 399 case CTFLT: 400 if(!isfloat[t->etype]) 401 fatal("overflow: %T floating-point constant", t); 402 if(mpcmpfltflt(v.u.fval, minfltval[t->etype]) <= 0 || 403 mpcmpfltflt(v.u.fval, maxfltval[t->etype]) >= 0) 404 return 1; 405 break; 406 case CTCPLX: 407 if(!iscomplex[t->etype]) 408 fatal("overflow: %T complex constant", t); 409 if(mpcmpfltflt(&v.u.cval->real, minfltval[t->etype]) <= 0 || 410 mpcmpfltflt(&v.u.cval->real, maxfltval[t->etype]) >= 0 || 411 mpcmpfltflt(&v.u.cval->imag, minfltval[t->etype]) <= 0 || 412 mpcmpfltflt(&v.u.cval->imag, maxfltval[t->etype]) >= 0) 413 return 1; 414 break; 415 } 416 return 0; 417 } 418 419 void 420 overflow(Val v, Type *t) 421 { 422 // v has already been converted 423 // to appropriate form for t. 424 if(t == T || t->etype == TIDEAL) 425 return; 426 427 if(!doesoverflow(v, t)) 428 return; 429 430 switch(v.ctype) { 431 case CTINT: 432 case CTRUNE: 433 yyerror("constant %B overflows %T", v.u.xval, t); 434 break; 435 case CTFLT: 436 yyerror("constant %#F overflows %T", v.u.fval, t); 437 break; 438 case CTCPLX: 439 yyerror("constant %#F overflows %T", v.u.fval, t); 440 break; 441 } 442 } 443 444 static Val 445 tostr(Val v) 446 { 447 Rune rune; 448 int l; 449 Strlit *s; 450 451 switch(v.ctype) { 452 case CTINT: 453 case CTRUNE: 454 if(mpcmpfixfix(v.u.xval, minintval[TINT]) < 0 || 455 mpcmpfixfix(v.u.xval, maxintval[TINT]) > 0) 456 yyerror("overflow in int -> string"); 457 rune = mpgetfix(v.u.xval); 458 l = runelen(rune); 459 s = mal(sizeof(*s)+l); 460 s->len = l; 461 runetochar((char*)s->s, &rune); 462 memset(&v, 0, sizeof v); 463 v.ctype = CTSTR; 464 v.u.sval = s; 465 break; 466 467 case CTFLT: 468 yyerror("no float -> string"); 469 470 case CTNIL: 471 memset(&v, 0, sizeof v); 472 v.ctype = CTSTR; 473 v.u.sval = mal(sizeof *s); 474 break; 475 } 476 return v; 477 } 478 479 int 480 consttype(Node *n) 481 { 482 if(n == N || n->op != OLITERAL) 483 return -1; 484 return n->val.ctype; 485 } 486 487 int 488 isconst(Node *n, int ct) 489 { 490 int t; 491 492 t = consttype(n); 493 // If the caller is asking for CTINT, allow CTRUNE too. 494 // Makes life easier for back ends. 495 return t == ct || (ct == CTINT && t == CTRUNE); 496 } 497 498 static Node* 499 saveorig(Node *n) 500 { 501 Node *n1; 502 503 if(n == n->orig) { 504 // duplicate node for n->orig. 505 n1 = nod(OLITERAL, N, N); 506 n->orig = n1; 507 *n1 = *n; 508 } 509 return n->orig; 510 } 511 512 /* 513 * if n is constant, rewrite as OLITERAL node. 514 */ 515 void 516 evconst(Node *n) 517 { 518 Node *nl, *nr, *norig; 519 int32 len; 520 Strlit *str; 521 int wl, wr, lno, et; 522 Val v, rv; 523 Mpint b; 524 525 // pick off just the opcodes that can be 526 // constant evaluated. 527 switch(n->op) { 528 default: 529 return; 530 case OADD: 531 case OADDSTR: 532 case OAND: 533 case OANDAND: 534 case OANDNOT: 535 case OARRAYBYTESTR: 536 case OCOM: 537 case ODIV: 538 case OEQ: 539 case OGE: 540 case OGT: 541 case OLE: 542 case OLSH: 543 case OLT: 544 case OMINUS: 545 case OMOD: 546 case OMUL: 547 case ONE: 548 case ONOT: 549 case OOR: 550 case OOROR: 551 case OPLUS: 552 case ORSH: 553 case OSUB: 554 case OXOR: 555 break; 556 case OCONV: 557 if(n->type == T) 558 return; 559 if(!okforconst[n->type->etype] && n->type->etype != TNIL) 560 return; 561 break; 562 } 563 564 nl = n->left; 565 if(nl == N || nl->type == T) 566 return; 567 if(consttype(nl) < 0) 568 return; 569 wl = nl->type->etype; 570 if(isint[wl] || isfloat[wl] || iscomplex[wl]) 571 wl = TIDEAL; 572 573 nr = n->right; 574 if(nr == N) 575 goto unary; 576 if(nr->type == T) 577 return; 578 if(consttype(nr) < 0) 579 return; 580 wr = nr->type->etype; 581 if(isint[wr] || isfloat[wr] || iscomplex[wr]) 582 wr = TIDEAL; 583 584 // check for compatible general types (numeric, string, etc) 585 if(wl != wr) 586 goto illegal; 587 588 // check for compatible types. 589 switch(n->op) { 590 default: 591 // ideal const mixes with anything but otherwise must match. 592 if(nl->type->etype != TIDEAL) { 593 defaultlit(&nr, nl->type); 594 n->right = nr; 595 } 596 if(nr->type->etype != TIDEAL) { 597 defaultlit(&nl, nr->type); 598 n->left = nl; 599 } 600 if(nl->type->etype != nr->type->etype) 601 goto illegal; 602 break; 603 604 case OLSH: 605 case ORSH: 606 // right must be unsigned. 607 // left can be ideal. 608 defaultlit(&nr, types[TUINT]); 609 n->right = nr; 610 if(nr->type && (issigned[nr->type->etype] || !isint[nr->type->etype])) 611 goto illegal; 612 if(nl->val.ctype != CTRUNE) 613 nl->val = toint(nl->val); 614 nr->val = toint(nr->val); 615 break; 616 } 617 618 // copy numeric value to avoid modifying 619 // n->left, in case someone still refers to it (e.g. iota). 620 v = nl->val; 621 if(wl == TIDEAL) 622 v = copyval(v); 623 624 rv = nr->val; 625 626 // convert to common ideal 627 if(v.ctype == CTCPLX || rv.ctype == CTCPLX) { 628 v = tocplx(v); 629 rv = tocplx(rv); 630 } 631 if(v.ctype == CTFLT || rv.ctype == CTFLT) { 632 v = toflt(v); 633 rv = toflt(rv); 634 } 635 636 // Rune and int turns into rune. 637 if(v.ctype == CTRUNE && rv.ctype == CTINT) 638 rv.ctype = CTRUNE; 639 if(v.ctype == CTINT && rv.ctype == CTRUNE) { 640 if(n->op == OLSH || n->op == ORSH) 641 rv.ctype = CTINT; 642 else 643 v.ctype = CTRUNE; 644 } 645 646 if(v.ctype != rv.ctype) { 647 // Use of undefined name as constant? 648 if((v.ctype == 0 || rv.ctype == 0) && nerrors > 0) 649 return; 650 fatal("constant type mismatch %T(%d) %T(%d)", nl->type, v.ctype, nr->type, rv.ctype); 651 } 652 653 // run op 654 switch(TUP(n->op, v.ctype)) { 655 default: 656 illegal: 657 if(!n->diag) { 658 yyerror("illegal constant expression: %T %O %T", 659 nl->type, n->op, nr->type); 660 n->diag = 1; 661 } 662 return; 663 664 case TUP(OADD, CTINT): 665 case TUP(OADD, CTRUNE): 666 mpaddfixfix(v.u.xval, rv.u.xval, 0); 667 break; 668 case TUP(OSUB, CTINT): 669 case TUP(OSUB, CTRUNE): 670 mpsubfixfix(v.u.xval, rv.u.xval); 671 break; 672 case TUP(OMUL, CTINT): 673 case TUP(OMUL, CTRUNE): 674 mpmulfixfix(v.u.xval, rv.u.xval); 675 break; 676 case TUP(ODIV, CTINT): 677 case TUP(ODIV, CTRUNE): 678 if(mpcmpfixc(rv.u.xval, 0) == 0) { 679 yyerror("division by zero"); 680 mpmovecfix(v.u.xval, 1); 681 break; 682 } 683 mpdivfixfix(v.u.xval, rv.u.xval); 684 break; 685 case TUP(OMOD, CTINT): 686 case TUP(OMOD, CTRUNE): 687 if(mpcmpfixc(rv.u.xval, 0) == 0) { 688 yyerror("division by zero"); 689 mpmovecfix(v.u.xval, 1); 690 break; 691 } 692 mpmodfixfix(v.u.xval, rv.u.xval); 693 break; 694 695 case TUP(OLSH, CTINT): 696 case TUP(OLSH, CTRUNE): 697 mplshfixfix(v.u.xval, rv.u.xval); 698 break; 699 case TUP(ORSH, CTINT): 700 case TUP(ORSH, CTRUNE): 701 mprshfixfix(v.u.xval, rv.u.xval); 702 break; 703 case TUP(OOR, CTINT): 704 case TUP(OOR, CTRUNE): 705 mporfixfix(v.u.xval, rv.u.xval); 706 break; 707 case TUP(OAND, CTINT): 708 case TUP(OAND, CTRUNE): 709 mpandfixfix(v.u.xval, rv.u.xval); 710 break; 711 case TUP(OANDNOT, CTINT): 712 case TUP(OANDNOT, CTRUNE): 713 mpandnotfixfix(v.u.xval, rv.u.xval); 714 break; 715 case TUP(OXOR, CTINT): 716 case TUP(OXOR, CTRUNE): 717 mpxorfixfix(v.u.xval, rv.u.xval); 718 break; 719 720 case TUP(OADD, CTFLT): 721 mpaddfltflt(v.u.fval, rv.u.fval); 722 break; 723 case TUP(OSUB, CTFLT): 724 mpsubfltflt(v.u.fval, rv.u.fval); 725 break; 726 case TUP(OMUL, CTFLT): 727 mpmulfltflt(v.u.fval, rv.u.fval); 728 break; 729 case TUP(ODIV, CTFLT): 730 if(mpcmpfltc(rv.u.fval, 0) == 0) { 731 yyerror("division by zero"); 732 mpmovecflt(v.u.fval, 1.0); 733 break; 734 } 735 mpdivfltflt(v.u.fval, rv.u.fval); 736 break; 737 case TUP(OMOD, CTFLT): 738 // The default case above would print 'ideal % ideal', 739 // which is not quite an ideal error. 740 if(!n->diag) { 741 yyerror("illegal constant expression: floating-point %% operation"); 742 n->diag = 1; 743 } 744 return; 745 746 case TUP(OADD, CTCPLX): 747 mpaddfltflt(&v.u.cval->real, &rv.u.cval->real); 748 mpaddfltflt(&v.u.cval->imag, &rv.u.cval->imag); 749 break; 750 case TUP(OSUB, CTCPLX): 751 mpsubfltflt(&v.u.cval->real, &rv.u.cval->real); 752 mpsubfltflt(&v.u.cval->imag, &rv.u.cval->imag); 753 break; 754 case TUP(OMUL, CTCPLX): 755 cmplxmpy(v.u.cval, rv.u.cval); 756 break; 757 case TUP(ODIV, CTCPLX): 758 if(mpcmpfltc(&rv.u.cval->real, 0) == 0 && 759 mpcmpfltc(&rv.u.cval->imag, 0) == 0) { 760 yyerror("complex division by zero"); 761 mpmovecflt(&rv.u.cval->real, 1.0); 762 mpmovecflt(&rv.u.cval->imag, 0.0); 763 break; 764 } 765 cmplxdiv(v.u.cval, rv.u.cval); 766 break; 767 768 case TUP(OEQ, CTNIL): 769 goto settrue; 770 case TUP(ONE, CTNIL): 771 goto setfalse; 772 773 case TUP(OEQ, CTINT): 774 case TUP(OEQ, CTRUNE): 775 if(mpcmpfixfix(v.u.xval, rv.u.xval) == 0) 776 goto settrue; 777 goto setfalse; 778 case TUP(ONE, CTINT): 779 case TUP(ONE, CTRUNE): 780 if(mpcmpfixfix(v.u.xval, rv.u.xval) != 0) 781 goto settrue; 782 goto setfalse; 783 case TUP(OLT, CTINT): 784 case TUP(OLT, CTRUNE): 785 if(mpcmpfixfix(v.u.xval, rv.u.xval) < 0) 786 goto settrue; 787 goto setfalse; 788 case TUP(OLE, CTINT): 789 case TUP(OLE, CTRUNE): 790 if(mpcmpfixfix(v.u.xval, rv.u.xval) <= 0) 791 goto settrue; 792 goto setfalse; 793 case TUP(OGE, CTINT): 794 case TUP(OGE, CTRUNE): 795 if(mpcmpfixfix(v.u.xval, rv.u.xval) >= 0) 796 goto settrue; 797 goto setfalse; 798 case TUP(OGT, CTINT): 799 case TUP(OGT, CTRUNE): 800 if(mpcmpfixfix(v.u.xval, rv.u.xval) > 0) 801 goto settrue; 802 goto setfalse; 803 804 case TUP(OEQ, CTFLT): 805 if(mpcmpfltflt(v.u.fval, rv.u.fval) == 0) 806 goto settrue; 807 goto setfalse; 808 case TUP(ONE, CTFLT): 809 if(mpcmpfltflt(v.u.fval, rv.u.fval) != 0) 810 goto settrue; 811 goto setfalse; 812 case TUP(OLT, CTFLT): 813 if(mpcmpfltflt(v.u.fval, rv.u.fval) < 0) 814 goto settrue; 815 goto setfalse; 816 case TUP(OLE, CTFLT): 817 if(mpcmpfltflt(v.u.fval, rv.u.fval) <= 0) 818 goto settrue; 819 goto setfalse; 820 case TUP(OGE, CTFLT): 821 if(mpcmpfltflt(v.u.fval, rv.u.fval) >= 0) 822 goto settrue; 823 goto setfalse; 824 case TUP(OGT, CTFLT): 825 if(mpcmpfltflt(v.u.fval, rv.u.fval) > 0) 826 goto settrue; 827 goto setfalse; 828 829 case TUP(OEQ, CTCPLX): 830 if(mpcmpfltflt(&v.u.cval->real, &rv.u.cval->real) == 0 && 831 mpcmpfltflt(&v.u.cval->imag, &rv.u.cval->imag) == 0) 832 goto settrue; 833 goto setfalse; 834 case TUP(ONE, CTCPLX): 835 if(mpcmpfltflt(&v.u.cval->real, &rv.u.cval->real) != 0 || 836 mpcmpfltflt(&v.u.cval->imag, &rv.u.cval->imag) != 0) 837 goto settrue; 838 goto setfalse; 839 840 case TUP(OEQ, CTSTR): 841 if(cmpslit(nl, nr) == 0) 842 goto settrue; 843 goto setfalse; 844 case TUP(ONE, CTSTR): 845 if(cmpslit(nl, nr) != 0) 846 goto settrue; 847 goto setfalse; 848 case TUP(OLT, CTSTR): 849 if(cmpslit(nl, nr) < 0) 850 goto settrue; 851 goto setfalse; 852 case TUP(OLE, CTSTR): 853 if(cmpslit(nl, nr) <= 0) 854 goto settrue; 855 goto setfalse; 856 case TUP(OGE, CTSTR): 857 if(cmpslit(nl, nr) >= 0l) 858 goto settrue; 859 goto setfalse; 860 case TUP(OGT, CTSTR): 861 if(cmpslit(nl, nr) > 0) 862 goto settrue; 863 goto setfalse; 864 case TUP(OADDSTR, CTSTR): 865 len = v.u.sval->len + rv.u.sval->len; 866 str = mal(sizeof(*str) + len); 867 str->len = len; 868 memcpy(str->s, v.u.sval->s, v.u.sval->len); 869 memcpy(str->s+v.u.sval->len, rv.u.sval->s, rv.u.sval->len); 870 str->len = len; 871 v.u.sval = str; 872 break; 873 874 case TUP(OOROR, CTBOOL): 875 if(v.u.bval || rv.u.bval) 876 goto settrue; 877 goto setfalse; 878 case TUP(OANDAND, CTBOOL): 879 if(v.u.bval && rv.u.bval) 880 goto settrue; 881 goto setfalse; 882 case TUP(OEQ, CTBOOL): 883 if(v.u.bval == rv.u.bval) 884 goto settrue; 885 goto setfalse; 886 case TUP(ONE, CTBOOL): 887 if(v.u.bval != rv.u.bval) 888 goto settrue; 889 goto setfalse; 890 } 891 goto ret; 892 893 unary: 894 // copy numeric value to avoid modifying 895 // nl, in case someone still refers to it (e.g. iota). 896 v = nl->val; 897 if(wl == TIDEAL) 898 v = copyval(v); 899 900 switch(TUP(n->op, v.ctype)) { 901 default: 902 if(!n->diag) { 903 yyerror("illegal constant expression %O %T", n->op, nl->type); 904 n->diag = 1; 905 } 906 return; 907 908 case TUP(OCONV, CTNIL): 909 case TUP(OARRAYBYTESTR, CTNIL): 910 if(n->type->etype == TSTRING) { 911 v = tostr(v); 912 nl->type = n->type; 913 break; 914 } 915 // fall through 916 case TUP(OCONV, CTINT): 917 case TUP(OCONV, CTRUNE): 918 case TUP(OCONV, CTFLT): 919 case TUP(OCONV, CTSTR): 920 convlit1(&nl, n->type, 1); 921 break; 922 923 case TUP(OPLUS, CTINT): 924 case TUP(OPLUS, CTRUNE): 925 break; 926 case TUP(OMINUS, CTINT): 927 case TUP(OMINUS, CTRUNE): 928 mpnegfix(v.u.xval); 929 break; 930 case TUP(OCOM, CTINT): 931 case TUP(OCOM, CTRUNE): 932 et = Txxx; 933 if(nl->type != T) 934 et = nl->type->etype; 935 936 // calculate the mask in b 937 // result will be (a ^ mask) 938 switch(et) { 939 default: 940 // signed guys change sign 941 mpmovecfix(&b, -1); 942 break; 943 944 case TUINT8: 945 case TUINT16: 946 case TUINT32: 947 case TUINT64: 948 case TUINT: 949 case TUINTPTR: 950 // unsigned guys invert their bits 951 mpmovefixfix(&b, maxintval[et]); 952 break; 953 } 954 mpxorfixfix(v.u.xval, &b); 955 break; 956 957 case TUP(OPLUS, CTFLT): 958 break; 959 case TUP(OMINUS, CTFLT): 960 mpnegflt(v.u.fval); 961 break; 962 963 case TUP(OPLUS, CTCPLX): 964 break; 965 case TUP(OMINUS, CTCPLX): 966 mpnegflt(&v.u.cval->real); 967 mpnegflt(&v.u.cval->imag); 968 break; 969 970 case TUP(ONOT, CTBOOL): 971 if(!v.u.bval) 972 goto settrue; 973 goto setfalse; 974 } 975 976 ret: 977 norig = saveorig(n); 978 *n = *nl; 979 // restore value of n->orig. 980 n->orig = norig; 981 n->val = v; 982 983 // check range. 984 lno = setlineno(n); 985 overflow(v, n->type); 986 lineno = lno; 987 988 // truncate precision for non-ideal float. 989 if(v.ctype == CTFLT && n->type->etype != TIDEAL) 990 n->val.u.fval = truncfltlit(v.u.fval, n->type); 991 return; 992 993 settrue: 994 norig = saveorig(n); 995 *n = *nodbool(1); 996 n->orig = norig; 997 return; 998 999 setfalse: 1000 norig = saveorig(n); 1001 *n = *nodbool(0); 1002 n->orig = norig; 1003 return; 1004 } 1005 1006 Node* 1007 nodlit(Val v) 1008 { 1009 Node *n; 1010 1011 n = nod(OLITERAL, N, N); 1012 n->val = v; 1013 switch(v.ctype) { 1014 default: 1015 fatal("nodlit ctype %d", v.ctype); 1016 case CTSTR: 1017 n->type = idealstring; 1018 break; 1019 case CTBOOL: 1020 n->type = idealbool; 1021 break; 1022 case CTINT: 1023 case CTRUNE: 1024 case CTFLT: 1025 case CTCPLX: 1026 n->type = types[TIDEAL]; 1027 break; 1028 case CTNIL: 1029 n->type = types[TNIL]; 1030 break; 1031 } 1032 return n; 1033 } 1034 1035 Node* 1036 nodcplxlit(Val r, Val i) 1037 { 1038 Node *n; 1039 Mpcplx *c; 1040 1041 r = toflt(r); 1042 i = toflt(i); 1043 1044 c = mal(sizeof(*c)); 1045 n = nod(OLITERAL, N, N); 1046 n->type = types[TIDEAL]; 1047 n->val.u.cval = c; 1048 n->val.ctype = CTCPLX; 1049 1050 if(r.ctype != CTFLT || i.ctype != CTFLT) 1051 fatal("nodcplxlit ctype %d/%d", r.ctype, i.ctype); 1052 1053 mpmovefltflt(&c->real, r.u.fval); 1054 mpmovefltflt(&c->imag, i.u.fval); 1055 return n; 1056 } 1057 1058 // idealkind returns a constant kind like consttype 1059 // but for an arbitrary "ideal" expression. 1060 static int 1061 idealkind(Node *n) 1062 { 1063 int k1, k2; 1064 1065 if(n == N || !isideal(n->type)) 1066 return CTxxx; 1067 1068 switch(n->op) { 1069 default: 1070 return CTxxx; 1071 case OLITERAL: 1072 return n->val.ctype; 1073 case OADD: 1074 case OAND: 1075 case OANDNOT: 1076 case OCOM: 1077 case ODIV: 1078 case OMINUS: 1079 case OMOD: 1080 case OMUL: 1081 case OSUB: 1082 case OXOR: 1083 case OOR: 1084 case OPLUS: 1085 // numeric kinds. 1086 k1 = idealkind(n->left); 1087 k2 = idealkind(n->right); 1088 if(k1 > k2) 1089 return k1; 1090 else 1091 return k2; 1092 case OREAL: 1093 case OIMAG: 1094 return CTFLT; 1095 case OCOMPLEX: 1096 return CTCPLX; 1097 case OADDSTR: 1098 return CTSTR; 1099 case OANDAND: 1100 case OEQ: 1101 case OGE: 1102 case OGT: 1103 case OLE: 1104 case OLT: 1105 case ONE: 1106 case ONOT: 1107 case OOROR: 1108 case OCMPSTR: 1109 case OCMPIFACE: 1110 return CTBOOL; 1111 case OLSH: 1112 case ORSH: 1113 // shifts (beware!). 1114 return idealkind(n->left); 1115 } 1116 } 1117 1118 void 1119 defaultlit(Node **np, Type *t) 1120 { 1121 int lno; 1122 int ctype; 1123 Node *n, *nn; 1124 Type *t1; 1125 1126 n = *np; 1127 if(n == N || !isideal(n->type)) 1128 return; 1129 1130 if(n->op == OLITERAL) { 1131 nn = nod(OXXX, N, N); 1132 *nn = *n; 1133 n = nn; 1134 *np = n; 1135 } 1136 1137 lno = setlineno(n); 1138 ctype = idealkind(n); 1139 switch(ctype) { 1140 default: 1141 if(t != T) { 1142 convlit(np, t); 1143 return; 1144 } 1145 if(n->val.ctype == CTNIL) { 1146 lineno = lno; 1147 yyerror("use of untyped nil"); 1148 n->type = T; 1149 break; 1150 } 1151 if(n->val.ctype == CTSTR) { 1152 t1 = types[TSTRING]; 1153 convlit(np, t1); 1154 break; 1155 } 1156 yyerror("defaultlit: unknown literal: %N", n); 1157 break; 1158 case CTxxx: 1159 fatal("defaultlit: idealkind is CTxxx: %+N", n); 1160 break; 1161 case CTBOOL: 1162 t1 = types[TBOOL]; 1163 if(t != T && t->etype == TBOOL) 1164 t1 = t; 1165 convlit(np, t1); 1166 break; 1167 case CTINT: 1168 t1 = types[TINT]; 1169 goto num; 1170 case CTRUNE: 1171 t1 = runetype; 1172 goto num; 1173 case CTFLT: 1174 t1 = types[TFLOAT64]; 1175 goto num; 1176 case CTCPLX: 1177 t1 = types[TCOMPLEX128]; 1178 goto num; 1179 num: 1180 if(t != T) { 1181 if(isint[t->etype]) { 1182 t1 = t; 1183 n->val = toint(n->val); 1184 } 1185 else 1186 if(isfloat[t->etype]) { 1187 t1 = t; 1188 n->val = toflt(n->val); 1189 } 1190 else 1191 if(iscomplex[t->etype]) { 1192 t1 = t; 1193 n->val = tocplx(n->val); 1194 } 1195 } 1196 overflow(n->val, t1); 1197 convlit(np, t1); 1198 break; 1199 } 1200 lineno = lno; 1201 } 1202 1203 /* 1204 * defaultlit on both nodes simultaneously; 1205 * if they're both ideal going in they better 1206 * get the same type going out. 1207 * force means must assign concrete (non-ideal) type. 1208 */ 1209 void 1210 defaultlit2(Node **lp, Node **rp, int force) 1211 { 1212 Node *l, *r; 1213 int lkind, rkind; 1214 1215 l = *lp; 1216 r = *rp; 1217 if(l->type == T || r->type == T) 1218 return; 1219 if(!isideal(l->type)) { 1220 convlit(rp, l->type); 1221 return; 1222 } 1223 if(!isideal(r->type)) { 1224 convlit(lp, r->type); 1225 return; 1226 } 1227 if(!force) 1228 return; 1229 if(l->type->etype == TBOOL) { 1230 convlit(lp, types[TBOOL]); 1231 convlit(rp, types[TBOOL]); 1232 } 1233 lkind = idealkind(l); 1234 rkind = idealkind(r); 1235 if(lkind == CTCPLX || rkind == CTCPLX) { 1236 convlit(lp, types[TCOMPLEX128]); 1237 convlit(rp, types[TCOMPLEX128]); 1238 return; 1239 } 1240 if(lkind == CTFLT || rkind == CTFLT) { 1241 convlit(lp, types[TFLOAT64]); 1242 convlit(rp, types[TFLOAT64]); 1243 return; 1244 } 1245 1246 if(lkind == CTRUNE || rkind == CTRUNE) { 1247 convlit(lp, runetype); 1248 convlit(rp, runetype); 1249 return; 1250 } 1251 1252 convlit(lp, types[TINT]); 1253 convlit(rp, types[TINT]); 1254 } 1255 1256 int 1257 cmpslit(Node *l, Node *r) 1258 { 1259 int32 l1, l2, i, m; 1260 uchar *s1, *s2; 1261 1262 l1 = l->val.u.sval->len; 1263 l2 = r->val.u.sval->len; 1264 s1 = (uchar*)l->val.u.sval->s; 1265 s2 = (uchar*)r->val.u.sval->s; 1266 1267 m = l1; 1268 if(l2 < m) 1269 m = l2; 1270 1271 for(i=0; i<m; i++) { 1272 if(s1[i] == s2[i]) 1273 continue; 1274 if(s1[i] > s2[i]) 1275 return +1; 1276 return -1; 1277 } 1278 if(l1 == l2) 1279 return 0; 1280 if(l1 > l2) 1281 return +1; 1282 return -1; 1283 } 1284 1285 int 1286 smallintconst(Node *n) 1287 { 1288 if(n->op == OLITERAL && isconst(n, CTINT) && n->type != T) 1289 switch(simtype[n->type->etype]) { 1290 case TINT8: 1291 case TUINT8: 1292 case TINT16: 1293 case TUINT16: 1294 case TINT32: 1295 case TUINT32: 1296 case TBOOL: 1297 case TPTR32: 1298 return 1; 1299 case TIDEAL: 1300 case TINT64: 1301 case TUINT64: 1302 case TPTR64: 1303 if(mpcmpfixfix(n->val.u.xval, minintval[TINT32]) < 0 1304 || mpcmpfixfix(n->val.u.xval, maxintval[TINT32]) > 0) 1305 break; 1306 return 1; 1307 } 1308 return 0; 1309 } 1310 1311 long 1312 nonnegconst(Node *n) 1313 { 1314 if(n->op == OLITERAL && n->type != T) 1315 switch(simtype[n->type->etype]) { 1316 case TINT8: 1317 case TUINT8: 1318 case TINT16: 1319 case TUINT16: 1320 case TINT32: 1321 case TUINT32: 1322 case TINT64: 1323 case TUINT64: 1324 case TIDEAL: 1325 // check negative and 2^31 1326 if(mpcmpfixfix(n->val.u.xval, minintval[TUINT32]) < 0 1327 || mpcmpfixfix(n->val.u.xval, maxintval[TINT32]) > 0) 1328 break; 1329 return mpgetfix(n->val.u.xval); 1330 } 1331 return -1; 1332 } 1333 1334 /* 1335 * convert x to type et and back to int64 1336 * for sign extension and truncation. 1337 */ 1338 static int64 1339 iconv(int64 x, int et) 1340 { 1341 switch(et) { 1342 case TINT8: 1343 x = (int8)x; 1344 break; 1345 case TUINT8: 1346 x = (uint8)x; 1347 break; 1348 case TINT16: 1349 x = (int16)x; 1350 break; 1351 case TUINT16: 1352 x = (uint64)x; 1353 break; 1354 case TINT32: 1355 x = (int32)x; 1356 break; 1357 case TUINT32: 1358 x = (uint32)x; 1359 break; 1360 case TINT64: 1361 case TUINT64: 1362 break; 1363 } 1364 return x; 1365 } 1366 1367 /* 1368 * convert constant val to type t; leave in con. 1369 * for back end. 1370 */ 1371 void 1372 convconst(Node *con, Type *t, Val *val) 1373 { 1374 int64 i; 1375 int tt; 1376 1377 tt = simsimtype(t); 1378 1379 // copy the constant for conversion 1380 nodconst(con, types[TINT8], 0); 1381 con->type = t; 1382 con->val = *val; 1383 1384 if(isint[tt]) { 1385 con->val.ctype = CTINT; 1386 con->val.u.xval = mal(sizeof *con->val.u.xval); 1387 switch(val->ctype) { 1388 default: 1389 fatal("convconst ctype=%d %lT", val->ctype, t); 1390 case CTINT: 1391 case CTRUNE: 1392 i = mpgetfix(val->u.xval); 1393 break; 1394 case CTBOOL: 1395 i = val->u.bval; 1396 break; 1397 case CTNIL: 1398 i = 0; 1399 break; 1400 } 1401 i = iconv(i, tt); 1402 mpmovecfix(con->val.u.xval, i); 1403 return; 1404 } 1405 1406 if(isfloat[tt]) { 1407 con->val = toflt(con->val); 1408 if(con->val.ctype != CTFLT) 1409 fatal("convconst ctype=%d %T", con->val.ctype, t); 1410 if(tt == TFLOAT32) 1411 con->val.u.fval = truncfltlit(con->val.u.fval, t); 1412 return; 1413 } 1414 1415 if(iscomplex[tt]) { 1416 con->val = tocplx(con->val); 1417 if(tt == TCOMPLEX64) { 1418 con->val.u.cval->real = *truncfltlit(&con->val.u.cval->real, types[TFLOAT32]); 1419 con->val.u.cval->imag = *truncfltlit(&con->val.u.cval->imag, types[TFLOAT32]); 1420 } 1421 return; 1422 } 1423 1424 fatal("convconst %lT constant", t); 1425 1426 } 1427 1428 // complex multiply v *= rv 1429 // (a, b) * (c, d) = (a*c - b*d, b*c + a*d) 1430 static void 1431 cmplxmpy(Mpcplx *v, Mpcplx *rv) 1432 { 1433 Mpflt ac, bd, bc, ad; 1434 1435 mpmovefltflt(&ac, &v->real); 1436 mpmulfltflt(&ac, &rv->real); // ac 1437 1438 mpmovefltflt(&bd, &v->imag); 1439 mpmulfltflt(&bd, &rv->imag); // bd 1440 1441 mpmovefltflt(&bc, &v->imag); 1442 mpmulfltflt(&bc, &rv->real); // bc 1443 1444 mpmovefltflt(&ad, &v->real); 1445 mpmulfltflt(&ad, &rv->imag); // ad 1446 1447 mpmovefltflt(&v->real, &ac); 1448 mpsubfltflt(&v->real, &bd); // ac-bd 1449 1450 mpmovefltflt(&v->imag, &bc); 1451 mpaddfltflt(&v->imag, &ad); // bc+ad 1452 } 1453 1454 // complex divide v /= rv 1455 // (a, b) / (c, d) = ((a*c + b*d), (b*c - a*d))/(c*c + d*d) 1456 static void 1457 cmplxdiv(Mpcplx *v, Mpcplx *rv) 1458 { 1459 Mpflt ac, bd, bc, ad, cc_plus_dd; 1460 1461 mpmovefltflt(&cc_plus_dd, &rv->real); 1462 mpmulfltflt(&cc_plus_dd, &rv->real); // cc 1463 1464 mpmovefltflt(&ac, &rv->imag); 1465 mpmulfltflt(&ac, &rv->imag); // dd 1466 1467 mpaddfltflt(&cc_plus_dd, &ac); // cc+dd 1468 1469 mpmovefltflt(&ac, &v->real); 1470 mpmulfltflt(&ac, &rv->real); // ac 1471 1472 mpmovefltflt(&bd, &v->imag); 1473 mpmulfltflt(&bd, &rv->imag); // bd 1474 1475 mpmovefltflt(&bc, &v->imag); 1476 mpmulfltflt(&bc, &rv->real); // bc 1477 1478 mpmovefltflt(&ad, &v->real); 1479 mpmulfltflt(&ad, &rv->imag); // ad 1480 1481 mpmovefltflt(&v->real, &ac); 1482 mpaddfltflt(&v->real, &bd); // ac+bd 1483 mpdivfltflt(&v->real, &cc_plus_dd); // (ac+bd)/(cc+dd) 1484 1485 mpmovefltflt(&v->imag, &bc); 1486 mpsubfltflt(&v->imag, &ad); // bc-ad 1487 mpdivfltflt(&v->imag, &cc_plus_dd); // (bc+ad)/(cc+dd) 1488 } 1489 1490 static int hascallchan(Node*); 1491 1492 // Is n a Go language constant (as opposed to a compile-time constant)? 1493 // Expressions derived from nil, like string([]byte(nil)), while they 1494 // may be known at compile time, are not Go language constants. 1495 // Only called for expressions known to evaluated to compile-time 1496 // constants. 1497 int 1498 isgoconst(Node *n) 1499 { 1500 Node *l; 1501 Type *t; 1502 1503 if(n->orig != N) 1504 n = n->orig; 1505 1506 switch(n->op) { 1507 case OADD: 1508 case OADDSTR: 1509 case OAND: 1510 case OANDAND: 1511 case OANDNOT: 1512 case OCOM: 1513 case ODIV: 1514 case OEQ: 1515 case OGE: 1516 case OGT: 1517 case OLE: 1518 case OLSH: 1519 case OLT: 1520 case OMINUS: 1521 case OMOD: 1522 case OMUL: 1523 case ONE: 1524 case ONOT: 1525 case OOR: 1526 case OOROR: 1527 case OPLUS: 1528 case ORSH: 1529 case OSUB: 1530 case OXOR: 1531 case OCONV: 1532 case OIOTA: 1533 case OCOMPLEX: 1534 case OREAL: 1535 case OIMAG: 1536 if(isgoconst(n->left) && (n->right == N || isgoconst(n->right))) 1537 return 1; 1538 break; 1539 1540 case OLEN: 1541 case OCAP: 1542 l = n->left; 1543 if(isgoconst(l)) 1544 return 1; 1545 // Special case: len/cap is constant when applied to array or 1546 // pointer to array when the expression does not contain 1547 // function calls or channel receive operations. 1548 t = l->type; 1549 if(t != T && isptr[t->etype]) 1550 t = t->type; 1551 if(isfixedarray(t) && !hascallchan(l)) 1552 return 1; 1553 break; 1554 1555 case OLITERAL: 1556 if(n->val.ctype != CTNIL) 1557 return 1; 1558 break; 1559 1560 case ONAME: 1561 l = n->sym->def; 1562 if(l->op == OLITERAL && n->val.ctype != CTNIL) 1563 return 1; 1564 break; 1565 1566 case ONONAME: 1567 if(n->sym->def != N && n->sym->def->op == OIOTA) 1568 return 1; 1569 break; 1570 1571 case OCALL: 1572 // Only constant calls are unsafe.Alignof, Offsetof, and Sizeof. 1573 l = n->left; 1574 while(l->op == OPAREN) 1575 l = l->left; 1576 if(l->op != ONAME || l->sym->pkg != unsafepkg) 1577 break; 1578 if(strcmp(l->sym->name, "Alignof") == 0 || 1579 strcmp(l->sym->name, "Offsetof") == 0 || 1580 strcmp(l->sym->name, "Sizeof") == 0) 1581 return 1; 1582 break; 1583 } 1584 1585 //dump("nonconst", n); 1586 return 0; 1587 } 1588 1589 static int 1590 hascallchan(Node *n) 1591 { 1592 NodeList *l; 1593 1594 if(n == N) 1595 return 0; 1596 switch(n->op) { 1597 case OCALL: 1598 case OCALLFUNC: 1599 case OCALLMETH: 1600 case OCALLINTER: 1601 case ORECV: 1602 return 1; 1603 } 1604 1605 if(hascallchan(n->left) || 1606 hascallchan(n->right)) 1607 return 1; 1608 1609 for(l=n->list; l; l=l->next) 1610 if(hascallchan(l->n)) 1611 return 1; 1612 for(l=n->rlist; l; l=l->next) 1613 if(hascallchan(l->n)) 1614 return 1; 1615 1616 return 0; 1617 }