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