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