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