github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/src/cmd/gc/const.c (about)

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