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