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