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 "go.h"
6 #define TUP(x,y) (((x)<<16)|(y))
7
8 static Val tocplx(Val);
9 static Val toflt(Val);
10 static Val tostr(Val);
11 static Val copyval(Val);
12 static void cmplxmpy(Mpcplx*, Mpcplx*);
13 static void cmplxdiv(Mpcplx*, Mpcplx*);
14
15 /*
16 * truncate float literal fv to 32-bit or 64-bit precision
17 * according to type; return truncated value.
18 */
19 Mpflt*
20 truncfltlit(Mpflt *oldv, Type *t)
21 {
22 double d;
23 float f;
24 Mpflt *fv;
25
26 if(t == T)
27 return oldv;
28
29 fv = mal(sizeof *fv);
30 *fv = *oldv;
31
32 // convert large precision literal floating
33 // into limited precision (float64 or float32)
34 // botch -- this assumes that compiler fp
35 // has same precision as runtime fp
36 switch(t->etype) {
37 case TFLOAT64:
38 d = mpgetflt(fv);
39 mpmovecflt(fv, d);
40 break;
41
42 case TFLOAT32:
43 d = mpgetflt(fv);
44 f = d;
45 d = f;
46 mpmovecflt(fv, d);
47 break;
48 }
49 return fv;
50 }
51
52 /*
53 * convert n, if literal, to type t.
54 * implicit conversion.
55 */
56 void
57 convlit(Node **np, Type *t)
58 {
59 convlit1(np, t, 0);
60 }
61
62 /*
63 * convert n, if literal, to type t.
64 * return a new node if necessary
65 * (if n is a named constant, can't edit n->type directly).
66 */
67 void
68 convlit1(Node **np, Type *t, int explicit)
69 {
70 int ct, et;
71 Node *n, *nn;
72
73 n = *np;
74 if(n == N || t == T || n->type == T || isideal(t) || n->type == t)
75 return;
76 if(!explicit && !isideal(n->type))
77 return;
78
79 if(n->op == OLITERAL) {
80 nn = nod(OXXX, N, N);
81 *nn = *n;
82 n = nn;
83 *np = n;
84 }
85
86 switch(n->op) {
87 default:
88 if(n->type->etype == TIDEAL) {
89 convlit(&n->left, t);
90 convlit(&n->right, t);
91 n->type = t;
92 }
93 return;
94 case OLITERAL:
95 // target is invalid type for a constant? leave alone.
96 if(!okforconst[t->etype] && n->type->etype != TNIL) {
97 defaultlit(&n, T);
98 *np = n;
99 return;
100 }
101 break;
102 case OLSH:
103 case ORSH:
104 convlit1(&n->left, t, explicit && isideal(n->left->type));
105 t = n->left->type;
106 if(t != T && t->etype == TIDEAL && n->val.ctype != CTINT)
107 n->val = toint(n->val);
108 if(t != T && !isint[t->etype]) {
109 yyerror("invalid operation: %#N (shift of type %T)", n, t);
110 t = T;
111 }
112 n->type = t;
113 return;
114 }
115
116 // avoided repeated calculations, errors
117 if(eqtype(n->type, t))
118 return;
119
120 ct = consttype(n);
121 if(ct < 0)
122 goto bad;
123
124 et = t->etype;
125 if(et == TINTER) {
126 if(ct == CTNIL && n->type == types[TNIL]) {
127 n->type = t;
128 return;
129 }
130 defaultlit(np, T);
131 return;
132 }
133
134 switch(ct) {
135 default:
136 goto bad;
137
138 case CTNIL:
139 switch(et) {
140 default:
141 n->type = T;
142 goto bad;
143
144 case TSTRING:
145 // let normal conversion code handle it
146 return;
147
148 case TARRAY:
149 if(!isslice(t))
150 goto bad;
151 break;
152
153 case TPTR32:
154 case TPTR64:
155 case TINTER:
156 case TMAP:
157 case TCHAN:
158 case TFUNC:
159 case TUNSAFEPTR:
160 break;
161 }
162 break;
163
164 case CTSTR:
165 case CTBOOL:
166 if(et != n->type->etype)
167 goto bad;
168 break;
169
170 case CTINT:
171 case CTFLT:
172 case CTCPLX:
173 ct = n->val.ctype;
174 if(isint[et]) {
175 switch(ct) {
176 default:
177 goto bad;
178 case CTCPLX:
179 case CTFLT:
180 n->val = toint(n->val);
181 // flowthrough
182 case CTINT:
183 overflow(n->val, t);
184 break;
185 }
186 } else
187 if(isfloat[et]) {
188 switch(ct) {
189 default:
190 goto bad;
191 case CTCPLX:
192 case CTINT:
193 n->val = toflt(n->val);
194 // flowthrough
195 case CTFLT:
196 overflow(n->val, t);
197 n->val.u.fval = truncfltlit(n->val.u.fval, t);
198 break;
199 }
200 } else
201 if(iscomplex[et]) {
202 switch(ct) {
203 default:
204 goto bad;
205 case CTFLT:
206 case CTINT:
207 n->val = tocplx(n->val);
208 break;
209 case CTCPLX:
210 overflow(n->val, t);
211 break;
212 }
213 } else
214 if(et == TSTRING && ct == CTINT && explicit)
215 n->val = tostr(n->val);
216 else
217 goto bad;
218 break;
219 }
220 n->type = t;
221 return;
222
223 bad:
224 if(!n->diag) {
225 yyerror("cannot convert %#N to type %T", n, t);
226 n->diag = 1;
227 }
228 if(isideal(n->type)) {
229 defaultlit(&n, T);
230 *np = n;
231 }
232 return;
233 }
234
235 static Val
236 copyval(Val v)
237 {
238 Mpint *i;
239 Mpflt *f;
240 Mpcplx *c;
241
242 switch(v.ctype) {
243 case CTINT:
244 i = mal(sizeof(*i));
245 mpmovefixfix(i, v.u.xval);
246 v.u.xval = i;
247 break;
248 case CTFLT:
249 f = mal(sizeof(*f));
250 mpmovefltflt(f, v.u.fval);
251 v.u.fval = f;
252 break;
253 case CTCPLX:
254 c = mal(sizeof(*c));
255 mpmovefltflt(&c->real, &v.u.cval->real);
256 mpmovefltflt(&c->imag, &v.u.cval->imag);
257 v.u.cval = c;
258 break;
259 }
260 return v;
261 }
262
263 static Val
264 tocplx(Val v)
265 {
266 Mpcplx *c;
267
268 switch(v.ctype) {
269 case CTINT:
270 c = mal(sizeof(*c));
271 mpmovefixflt(&c->real, v.u.xval);
272 mpmovecflt(&c->imag, 0.0);
273 v.ctype = CTCPLX;
274 v.u.cval = c;
275 break;
276 case CTFLT:
277 c = mal(sizeof(*c));
278 mpmovefltflt(&c->real, v.u.fval);
279 mpmovecflt(&c->imag, 0.0);
280 v.ctype = CTCPLX;
281 v.u.cval = c;
282 break;
283 }
284 return v;
285 }
286
287 static Val
288 toflt(Val v)
289 {
290 Mpflt *f;
291
292 switch(v.ctype) {
293 case CTINT:
294 f = mal(sizeof(*f));
295 mpmovefixflt(f, v.u.xval);
296 v.ctype = CTFLT;
297 v.u.fval = f;
298 break;
299 case CTCPLX:
300 f = mal(sizeof(*f));
301 mpmovefltflt(f, &v.u.cval->real);
302 if(mpcmpfltc(&v.u.cval->imag, 0) != 0)
303 yyerror("constant %#F%+#Fi truncated to real", &v.u.cval->real, &v.u.cval->imag);
304 v.ctype = CTFLT;
305 v.u.fval = f;
306 break;
307 }
308 return v;
309 }
310
311 Val
312 toint(Val v)
313 {
314 Mpint *i;
315
316 switch(v.ctype) {
317 case CTFLT:
318 i = mal(sizeof(*i));
319 if(mpmovefltfix(i, v.u.fval) < 0)
320 yyerror("constant %#F truncated to integer", v.u.fval);
321 v.ctype = CTINT;
322 v.u.xval = i;
323 break;
324 case CTCPLX:
325 i = mal(sizeof(*i));
326 if(mpmovefltfix(i, &v.u.cval->real) < 0)
327 yyerror("constant %#F%+#Fi truncated to integer", &v.u.cval->real, &v.u.cval->imag);
328 if(mpcmpfltc(&v.u.cval->imag, 0) != 0)
329 yyerror("constant %#F%+#Fi truncated to real", &v.u.cval->real, &v.u.cval->imag);
330 v.ctype = CTINT;
331 v.u.xval = i;
332 break;
333 }
334 return v;
335 }
336
337 void
338 overflow(Val v, Type *t)
339 {
340 // v has already been converted
341 // to appropriate form for t.
342 if(t == T || t->etype == TIDEAL)
343 return;
344 switch(v.ctype) {
345 case CTINT:
346 if(!isint[t->etype])
347 fatal("overflow: %T integer constant", t);
348 if(mpcmpfixfix(v.u.xval, minintval[t->etype]) < 0 ||
349 mpcmpfixfix(v.u.xval, maxintval[t->etype]) > 0)
350 yyerror("constant %B overflows %T", v.u.xval, t);
351 break;
352 case CTFLT:
353 if(!isfloat[t->etype])
354 fatal("overflow: %T floating-point constant", t);
355 if(mpcmpfltflt(v.u.fval, minfltval[t->etype]) <= 0 ||
356 mpcmpfltflt(v.u.fval, maxfltval[t->etype]) >= 0)
357 yyerror("constant %#F overflows %T", v.u.fval, t);
358 break;
359 case CTCPLX:
360 if(!iscomplex[t->etype])
361 fatal("overflow: %T complex constant", t);
362 if(mpcmpfltflt(&v.u.cval->real, minfltval[t->etype]) <= 0 ||
363 mpcmpfltflt(&v.u.cval->real, maxfltval[t->etype]) >= 0 ||
364 mpcmpfltflt(&v.u.cval->imag, minfltval[t->etype]) <= 0 ||
365 mpcmpfltflt(&v.u.cval->imag, maxfltval[t->etype]) >= 0)
366 yyerror("constant %#F overflows %T", v.u.fval, t);
367 break;
368 }
369 }
370
371 static Val
372 tostr(Val v)
373 {
374 Rune rune;
375 int l;
376 Strlit *s;
377
378 switch(v.ctype) {
379 case CTINT:
380 if(mpcmpfixfix(v.u.xval, minintval[TINT]) < 0 ||
381 mpcmpfixfix(v.u.xval, maxintval[TINT]) > 0)
382 yyerror("overflow in int -> string");
383 rune = mpgetfix(v.u.xval);
384 l = runelen(rune);
385 s = mal(sizeof(*s)+l);
386 s->len = l;
387 runetochar((char*)s->s, &rune);
388 memset(&v, 0, sizeof v);
389 v.ctype = CTSTR;
390 v.u.sval = s;
391 break;
392
393 case CTFLT:
394 yyerror("no float -> string");
395
396 case CTNIL:
397 memset(&v, 0, sizeof v);
398 v.ctype = CTSTR;
399 v.u.sval = mal(sizeof *s);
400 break;
401 }
402 return v;
403 }
404
405 int
406 consttype(Node *n)
407 {
408 if(n == N || n->op != OLITERAL)
409 return -1;
410 return n->val.ctype;
411 }
412
413 int
414 isconst(Node *n, int ct)
415 {
416 return consttype(n) == ct;
417 }
418
419 /*
420 * if n is constant, rewrite as OLITERAL node.
421 */
422 void
423 evconst(Node *n)
424 {
425 Node *nl, *nr;
426 int32 len;
427 Strlit *str;
428 int wl, wr, lno, et;
429 Val v, rv;
430 Mpint b;
431
432 // pick off just the opcodes that can be
433 // constant evaluated.
434 switch(n->op) {
435 default:
436 return;
437 case OADD:
438 case OADDSTR:
439 case OAND:
440 case OANDAND:
441 case OANDNOT:
442 case OARRAYBYTESTR:
443 case OCOM:
444 case ODIV:
445 case OEQ:
446 case OGE:
447 case OGT:
448 case OLE:
449 case OLSH:
450 case OLT:
451 case OMINUS:
452 case OMOD:
453 case OMUL:
454 case ONE:
455 case ONOT:
456 case OOR:
457 case OOROR:
458 case OPLUS:
459 case ORSH:
460 case OSUB:
461 case OXOR:
462 break;
463 case OCONV:
464 if(n->type == T)
465 return;
466 if(!okforconst[n->type->etype] && n->type->etype != TNIL)
467 return;
468 break;
469 }
470
471 nl = n->left;
472 if(nl == N || nl->type == T)
473 return;
474 if(consttype(nl) < 0)
475 return;
476 wl = nl->type->etype;
477 if(isint[wl] || isfloat[wl] || iscomplex[wl])
478 wl = TIDEAL;
479
480 nr = n->right;
481 if(nr == N)
482 goto unary;
483 if(nr->type == T)
484 return;
485 if(consttype(nr) < 0)
486 return;
487 wr = nr->type->etype;
488 if(isint[wr] || isfloat[wr] || iscomplex[wr])
489 wr = TIDEAL;
490
491 // check for compatible general types (numeric, string, etc)
492 if(wl != wr)
493 goto illegal;
494
495 // check for compatible types.
496 switch(n->op) {
497 default:
498 // ideal const mixes with anything but otherwise must match.
499 if(nl->type->etype != TIDEAL) {
500 defaultlit(&nr, nl->type);
501 n->right = nr;
502 }
503 if(nr->type->etype != TIDEAL) {
504 defaultlit(&nl, nr->type);
505 n->left = nl;
506 }
507 if(nl->type->etype != nr->type->etype)
508 goto illegal;
509 break;
510
511 case OLSH:
512 case ORSH:
513 // right must be unsigned.
514 // left can be ideal.
515 defaultlit(&nr, types[TUINT]);
516 n->right = nr;
517 if(nr->type && (issigned[nr->type->etype] || !isint[nr->type->etype]))
518 goto illegal;
519 nl->val = toint(nl->val);
520 nr->val = toint(nr->val);
521 break;
522 }
523
524 // copy numeric value to avoid modifying
525 // n->left, in case someone still refers to it (e.g. iota).
526 v = nl->val;
527 if(wl == TIDEAL)
528 v = copyval(v);
529
530 rv = nr->val;
531
532 // convert to common ideal
533 if(v.ctype == CTCPLX || rv.ctype == CTCPLX) {
534 v = tocplx(v);
535 rv = tocplx(rv);
536 }
537 if(v.ctype == CTFLT || rv.ctype == CTFLT) {
538 v = toflt(v);
539 rv = toflt(rv);
540 }
541 if(v.ctype != rv.ctype) {
542 // Use of undefined name as constant?
543 if((v.ctype == 0 || rv.ctype == 0) && nerrors > 0)
544 return;
545 fatal("constant type mismatch %T(%d) %T(%d)", nl->type, v.ctype, nr->type, rv.ctype);
546 }
547
548 // run op
549 switch(TUP(n->op, v.ctype)) {
550 default:
551 illegal:
552 if(!n->diag) {
553 yyerror("illegal constant expression: %T %O %T",
554 nl->type, n->op, nr->type);
555 n->diag = 1;
556 }
557 return;
558
559 case TUP(OADD, CTINT):
560 mpaddfixfix(v.u.xval, rv.u.xval);
561 break;
562 case TUP(OSUB, CTINT):
563 mpsubfixfix(v.u.xval, rv.u.xval);
564 break;
565 case TUP(OMUL, CTINT):
566 mpmulfixfix(v.u.xval, rv.u.xval);
567 break;
568 case TUP(ODIV, CTINT):
569 if(mpcmpfixc(rv.u.xval, 0) == 0) {
570 yyerror("division by zero");
571 mpmovecfix(v.u.xval, 1);
572 break;
573 }
574 mpdivfixfix(v.u.xval, rv.u.xval);
575 break;
576 case TUP(OMOD, CTINT):
577 if(mpcmpfixc(rv.u.xval, 0) == 0) {
578 yyerror("division by zero");
579 mpmovecfix(v.u.xval, 1);
580 break;
581 }
582 mpmodfixfix(v.u.xval, rv.u.xval);
583 break;
584
585 case TUP(OLSH, CTINT):
586 mplshfixfix(v.u.xval, rv.u.xval);
587 break;
588 case TUP(ORSH, CTINT):
589 mprshfixfix(v.u.xval, rv.u.xval);
590 break;
591 case TUP(OOR, CTINT):
592 mporfixfix(v.u.xval, rv.u.xval);
593 break;
594 case TUP(OAND, CTINT):
595 mpandfixfix(v.u.xval, rv.u.xval);
596 break;
597 case TUP(OANDNOT, CTINT):
598 mpandnotfixfix(v.u.xval, rv.u.xval);
599 break;
600 case TUP(OXOR, CTINT):
601 mpxorfixfix(v.u.xval, rv.u.xval);
602 break;
603
604 case TUP(OADD, CTFLT):
605 mpaddfltflt(v.u.fval, rv.u.fval);
606 break;
607 case TUP(OSUB, CTFLT):
608 mpsubfltflt(v.u.fval, rv.u.fval);
609 break;
610 case TUP(OMUL, CTFLT):
611 mpmulfltflt(v.u.fval, rv.u.fval);
612 break;
613 case TUP(ODIV, CTFLT):
614 if(mpcmpfltc(rv.u.fval, 0) == 0) {
615 yyerror("division by zero");
616 mpmovecflt(v.u.fval, 1.0);
617 break;
618 }
619 mpdivfltflt(v.u.fval, rv.u.fval);
620 break;
621
622 case TUP(OADD, CTCPLX):
623 mpaddfltflt(&v.u.cval->real, &rv.u.cval->real);
624 mpaddfltflt(&v.u.cval->imag, &rv.u.cval->imag);
625 break;
626 case TUP(OSUB, CTCPLX):
627 mpsubfltflt(&v.u.cval->real, &rv.u.cval->real);
628 mpsubfltflt(&v.u.cval->imag, &rv.u.cval->imag);
629 break;
630 case TUP(OMUL, CTCPLX):
631 cmplxmpy(v.u.cval, rv.u.cval);
632 break;
633 case TUP(ODIV, CTCPLX):
634 if(mpcmpfltc(&rv.u.cval->real, 0) == 0 &&
635 mpcmpfltc(&rv.u.cval->imag, 0) == 0) {
636 yyerror("complex division by zero");
637 mpmovecflt(&rv.u.cval->real, 1.0);
638 mpmovecflt(&rv.u.cval->imag, 0.0);
639 break;
640 }
641 cmplxdiv(v.u.cval, rv.u.cval);
642 break;
643
644 case TUP(OEQ, CTNIL):
645 goto settrue;
646 case TUP(ONE, CTNIL):
647 goto setfalse;
648
649 case TUP(OEQ, CTINT):
650 if(mpcmpfixfix(v.u.xval, rv.u.xval) == 0)
651 goto settrue;
652 goto setfalse;
653 case TUP(ONE, CTINT):
654 if(mpcmpfixfix(v.u.xval, rv.u.xval) != 0)
655 goto settrue;
656 goto setfalse;
657 case TUP(OLT, CTINT):
658 if(mpcmpfixfix(v.u.xval, rv.u.xval) < 0)
659 goto settrue;
660 goto setfalse;
661 case TUP(OLE, CTINT):
662 if(mpcmpfixfix(v.u.xval, rv.u.xval) <= 0)
663 goto settrue;
664 goto setfalse;
665 case TUP(OGE, CTINT):
666 if(mpcmpfixfix(v.u.xval, rv.u.xval) >= 0)
667 goto settrue;
668 goto setfalse;
669 case TUP(OGT, CTINT):
670 if(mpcmpfixfix(v.u.xval, rv.u.xval) > 0)
671 goto settrue;
672 goto setfalse;
673
674 case TUP(OEQ, CTFLT):
675 if(mpcmpfltflt(v.u.fval, rv.u.fval) == 0)
676 goto settrue;
677 goto setfalse;
678 case TUP(ONE, CTFLT):
679 if(mpcmpfltflt(v.u.fval, rv.u.fval) != 0)
680 goto settrue;
681 goto setfalse;
682 case TUP(OLT, CTFLT):
683 if(mpcmpfltflt(v.u.fval, rv.u.fval) < 0)
684 goto settrue;
685 goto setfalse;
686 case TUP(OLE, CTFLT):
687 if(mpcmpfltflt(v.u.fval, rv.u.fval) <= 0)
688 goto settrue;
689 goto setfalse;
690 case TUP(OGE, CTFLT):
691 if(mpcmpfltflt(v.u.fval, rv.u.fval) >= 0)
692 goto settrue;
693 goto setfalse;
694 case TUP(OGT, CTFLT):
695 if(mpcmpfltflt(v.u.fval, rv.u.fval) > 0)
696 goto settrue;
697 goto setfalse;
698
699 case TUP(OEQ, CTCPLX):
700 if(mpcmpfltflt(&v.u.cval->real, &rv.u.cval->real) == 0 &&
701 mpcmpfltflt(&v.u.cval->imag, &rv.u.cval->imag) == 0)
702 goto settrue;
703 goto setfalse;
704 case TUP(ONE, CTCPLX):
705 if(mpcmpfltflt(&v.u.cval->real, &rv.u.cval->real) != 0 ||
706 mpcmpfltflt(&v.u.cval->imag, &rv.u.cval->imag) != 0)
707 goto settrue;
708 goto setfalse;
709
710 case TUP(OEQ, CTSTR):
711 if(cmpslit(nl, nr) == 0)
712 goto settrue;
713 goto setfalse;
714 case TUP(ONE, CTSTR):
715 if(cmpslit(nl, nr) != 0)
716 goto settrue;
717 goto setfalse;
718 case TUP(OLT, CTSTR):
719 if(cmpslit(nl, nr) < 0)
720 goto settrue;
721 goto setfalse;
722 case TUP(OLE, CTSTR):
723 if(cmpslit(nl, nr) <= 0)
724 goto settrue;
725 goto setfalse;
726 case TUP(OGE, CTSTR):
727 if(cmpslit(nl, nr) >= 0l)
728 goto settrue;
729 goto setfalse;
730 case TUP(OGT, CTSTR):
731 if(cmpslit(nl, nr) > 0)
732 goto settrue;
733 goto setfalse;
734 case TUP(OADDSTR, CTSTR):
735 len = v.u.sval->len + rv.u.sval->len;
736 str = mal(sizeof(*str) + len);
737 str->len = len;
738 memcpy(str->s, v.u.sval->s, v.u.sval->len);
739 memcpy(str->s+v.u.sval->len, rv.u.sval->s, rv.u.sval->len);
740 str->len = len;
741 v.u.sval = str;
742 break;
743
744 case TUP(OOROR, CTBOOL):
745 if(v.u.bval || rv.u.bval)
746 goto settrue;
747 goto setfalse;
748 case TUP(OANDAND, CTBOOL):
749 if(v.u.bval && rv.u.bval)
750 goto settrue;
751 goto setfalse;
752 case TUP(OEQ, CTBOOL):
753 if(v.u.bval == rv.u.bval)
754 goto settrue;
755 goto setfalse;
756 case TUP(ONE, CTBOOL):
757 if(v.u.bval != rv.u.bval)
758 goto settrue;
759 goto setfalse;
760 }
761 goto ret;
762
763 unary:
764 // copy numeric value to avoid modifying
765 // nl, in case someone still refers to it (e.g. iota).
766 v = nl->val;
767 if(wl == TIDEAL)
768 v = copyval(v);
769
770 switch(TUP(n->op, v.ctype)) {
771 default:
772 if(!n->diag) {
773 yyerror("illegal constant expression %O %T", n->op, nl->type);
774 n->diag = 1;
775 }
776 return;
777
778 case TUP(OCONV, CTNIL):
779 case TUP(OARRAYBYTESTR, CTNIL):
780 if(n->type->etype == TSTRING) {
781 v = tostr(v);
782 nl->type = n->type;
783 break;
784 }
785 // fall through
786 case TUP(OCONV, CTINT):
787 case TUP(OCONV, CTFLT):
788 case TUP(OCONV, CTSTR):
789 convlit1(&nl, n->type, 1);
790 break;
791
792 case TUP(OPLUS, CTINT):
793 break;
794 case TUP(OMINUS, CTINT):
795 mpnegfix(v.u.xval);
796 break;
797 case TUP(OCOM, CTINT):
798 et = Txxx;
799 if(nl->type != T)
800 et = nl->type->etype;
801
802 // calculate the mask in b
803 // result will be (a ^ mask)
804 switch(et) {
805 default:
806 // signed guys change sign
807 mpmovecfix(&b, -1);
808 break;
809
810 case TUINT8:
811 case TUINT16:
812 case TUINT32:
813 case TUINT64:
814 case TUINT:
815 case TUINTPTR:
816 // unsigned guys invert their bits
817 mpmovefixfix(&b, maxintval[et]);
818 break;
819 }
820 mpxorfixfix(v.u.xval, &b);
821 break;
822
823 case TUP(OPLUS, CTFLT):
824 break;
825 case TUP(OMINUS, CTFLT):
826 mpnegflt(v.u.fval);
827 break;
828
829 case TUP(OPLUS, CTCPLX):
830 break;
831 case TUP(OMINUS, CTCPLX):
832 mpnegflt(&v.u.cval->real);
833 mpnegflt(&v.u.cval->imag);
834 break;
835
836 case TUP(ONOT, CTBOOL):
837 if(!v.u.bval)
838 goto settrue;
839 goto setfalse;
840 }
841
842 ret:
843 // rewrite n in place.
844 *n = *nl;
845 n->val = v;
846
847 // check range.
848 lno = setlineno(n);
849 overflow(v, n->type);
850 lineno = lno;
851
852 // truncate precision for non-ideal float.
853 if(v.ctype == CTFLT && n->type->etype != TIDEAL)
854 n->val.u.fval = truncfltlit(v.u.fval, n->type);
855 return;
856
857 settrue:
858 *n = *nodbool(1);
859 return;
860
861 setfalse:
862 *n = *nodbool(0);
863 return;
864 }
865
866 Node*
867 nodlit(Val v)
868 {
869 Node *n;
870
871 n = nod(OLITERAL, N, N);
872 n->val = v;
873 switch(v.ctype) {
874 default:
875 fatal("nodlit ctype %d", v.ctype);
876 case CTSTR:
877 n->type = idealstring;
878 break;
879 case CTBOOL:
880 n->type = idealbool;
881 break;
882 case CTINT:
883 case CTFLT:
884 case CTCPLX:
885 n->type = types[TIDEAL];
886 break;
887 case CTNIL:
888 n->type = types[TNIL];
889 break;
890 }
891 return n;
892 }
893
894 Node*
895 nodcplxlit(Val r, Val i)
896 {
897 Node *n;
898 Mpcplx *c;
899
900 r = toflt(r);
901 i = toflt(i);
902
903 c = mal(sizeof(*c));
904 n = nod(OLITERAL, N, N);
905 n->type = types[TIDEAL];
906 n->val.u.cval = c;
907 n->val.ctype = CTCPLX;
908
909 if(r.ctype != CTFLT || i.ctype != CTFLT)
910 fatal("nodcplxlit ctype %d/%d", r.ctype, i.ctype);
911
912 mpmovefltflt(&c->real, r.u.fval);
913 mpmovefltflt(&c->imag, i.u.fval);
914 return n;
915 }
916
917 // TODO(rsc): combine with convlit
918 void
919 defaultlit(Node **np, Type *t)
920 {
921 int lno;
922 Node *n, *nn;
923
924 n = *np;
925 if(n == N || !isideal(n->type))
926 return;
927
928 switch(n->op) {
929 case OLITERAL:
930 nn = nod(OXXX, N, N);
931 *nn = *n;
932 n = nn;
933 *np = n;
934 break;
935 case OLSH:
936 case ORSH:
937 defaultlit(&n->left, t);
938 t = n->left->type;
939 if(t != T && !isint[t->etype]) {
940 yyerror("invalid operation: %#N (shift of type %T)", n, t);
941 t = T;
942 }
943 n->type = t;
944 return;
945 default:
946 if(n->left == N) {
947 dump("defaultlit", n);
948 fatal("defaultlit");
949 }
950 // n is ideal, so left and right must both be ideal.
951 // n has not been computed as a constant value,
952 // so either left or right must not be constant.
953 // The only 'ideal' non-constant expressions are shifts. Ugh.
954 // If one of these is a shift and the other is not, use that type.
955 // When compiling x := 1<<i + 3.14, this means we try to push
956 // the float64 down into the 1<<i, producing the correct error
957 // (cannot shift float64).
958 if(t == T && (n->right->op == OLSH || n->right->op == ORSH)) {
959 defaultlit(&n->left, T);
960 defaultlit(&n->right, n->left->type);
961 } else if(t == T && (n->left->op == OLSH || n->left->op == ORSH)) {
962 defaultlit(&n->right, T);
963 defaultlit(&n->left, n->right->type);
964 } else {
965 defaultlit(&n->left, t);
966 defaultlit(&n->right, t);
967 }
968 if(n->type == idealbool || n->type == idealstring)
969 n->type = types[n->type->etype];
970 else
971 n->type = n->left->type;
972 return;
973 }
974
975 lno = setlineno(n);
976 switch(n->val.ctype) {
977 default:
978 if(t != T) {
979 convlit(np, t);
980 break;
981 }
982 if(n->val.ctype == CTNIL) {
983 lineno = lno;
984 yyerror("use of untyped nil");
985 n->type = T;
986 break;
987 }
988 if(n->val.ctype == CTSTR) {
989 n->type = types[TSTRING];
990 break;
991 }
992 yyerror("defaultlit: unknown literal: %#N", n);
993 break;
994 case CTBOOL:
995 n->type = types[TBOOL];
996 if(t != T && t->etype == TBOOL)
997 n->type = t;
998 break;
999 case CTINT:
1000 n->type = types[TINT];
1001 goto num;
1002 case CTFLT:
1003 n->type = types[TFLOAT64];
1004 goto num;
1005 case CTCPLX:
1006 n->type = types[TCOMPLEX128];
1007 goto num;
1008 num:
1009 if(t != T) {
1010 if(isint[t->etype]) {
1011 n->type = t;
1012 n->val = toint(n->val);
1013 }
1014 else
1015 if(isfloat[t->etype]) {
1016 n->type = t;
1017 n->val = toflt(n->val);
1018 }
1019 else
1020 if(iscomplex[t->etype]) {
1021 n->type = t;
1022 n->val = tocplx(n->val);
1023 }
1024 }
1025 overflow(n->val, n->type);
1026 break;
1027 }
1028 lineno = lno;
1029 }
1030
1031 /*
1032 * defaultlit on both nodes simultaneously;
1033 * if they're both ideal going in they better
1034 * get the same type going out.
1035 * force means must assign concrete (non-ideal) type.
1036 */
1037 void
1038 defaultlit2(Node **lp, Node **rp, int force)
1039 {
1040 Node *l, *r;
1041
1042 l = *lp;
1043 r = *rp;
1044 if(l->type == T || r->type == T)
1045 return;
1046 if(!isideal(l->type)) {
1047 convlit(rp, l->type);
1048 return;
1049 }
1050 if(!isideal(r->type)) {
1051 convlit(lp, r->type);
1052 return;
1053 }
1054 if(!force)
1055 return;
1056 if(isconst(l, CTCPLX) || isconst(r, CTCPLX)) {
1057 convlit(lp, types[TCOMPLEX128]);
1058 convlit(rp, types[TCOMPLEX128]);
1059 return;
1060 }
1061 if(isconst(l, CTFLT) || isconst(r, CTFLT)) {
1062 convlit(lp, types[TFLOAT64]);
1063 convlit(rp, types[TFLOAT64]);
1064 return;
1065 }
1066 convlit(lp, types[TINT]);
1067 convlit(rp, types[TINT]);
1068 }
1069
1070 int
1071 cmpslit(Node *l, Node *r)
1072 {
1073 int32 l1, l2, i, m;
1074 uchar *s1, *s2;
1075
1076 l1 = l->val.u.sval->len;
1077 l2 = r->val.u.sval->len;
1078 s1 = (uchar*)l->val.u.sval->s;
1079 s2 = (uchar*)r->val.u.sval->s;
1080
1081 m = l1;
1082 if(l2 < m)
1083 m = l2;
1084
1085 for(i=0; i<m; i++) {
1086 if(s1[i] == s2[i])
1087 continue;
1088 if(s1[i] > s2[i])
1089 return +1;
1090 return -1;
1091 }
1092 if(l1 == l2)
1093 return 0;
1094 if(l1 > l2)
1095 return +1;
1096 return -1;
1097 }
1098
1099 int
1100 smallintconst(Node *n)
1101 {
1102 if(n->op == OLITERAL && n->type != T)
1103 switch(simtype[n->type->etype]) {
1104 case TINT8:
1105 case TUINT8:
1106 case TINT16:
1107 case TUINT16:
1108 case TINT32:
1109 case TUINT32:
1110 case TBOOL:
1111 case TPTR32:
1112 return 1;
1113 case TINT64:
1114 case TUINT64:
1115 if(mpcmpfixfix(n->val.u.xval, minintval[TINT32]) < 0
1116 || mpcmpfixfix(n->val.u.xval, maxintval[TINT32]) > 0)
1117 break;
1118 return 1;
1119 }
1120 return 0;
1121 }
1122
1123 long
1124 nonnegconst(Node *n)
1125 {
1126 if(n->op == OLITERAL && n->type != T)
1127 switch(simtype[n->type->etype]) {
1128 case TINT8:
1129 case TUINT8:
1130 case TINT16:
1131 case TUINT16:
1132 case TINT32:
1133 case TUINT32:
1134 case TINT64:
1135 case TUINT64:
1136 case TIDEAL:
1137 // check negative and 2^31
1138 if(mpcmpfixfix(n->val.u.xval, minintval[TUINT32]) < 0
1139 || mpcmpfixfix(n->val.u.xval, maxintval[TINT32]) > 0)
1140 break;
1141 return mpgetfix(n->val.u.xval);
1142 }
1143 return -1;
1144 }
1145
1146 /*
1147 * convert x to type et and back to int64
1148 * for sign extension and truncation.
1149 */
1150 static int64
1151 iconv(int64 x, int et)
1152 {
1153 switch(et) {
1154 case TINT8:
1155 x = (int8)x;
1156 break;
1157 case TUINT8:
1158 x = (uint8)x;
1159 break;
1160 case TINT16:
1161 x = (int16)x;
1162 break;
1163 case TUINT16:
1164 x = (uint64)x;
1165 break;
1166 case TINT32:
1167 x = (int32)x;
1168 break;
1169 case TUINT32:
1170 x = (uint32)x;
1171 break;
1172 case TINT64:
1173 case TUINT64:
1174 break;
1175 }
1176 return x;
1177 }
1178
1179 /*
1180 * convert constant val to type t; leave in con.
1181 * for back end.
1182 */
1183 void
1184 convconst(Node *con, Type *t, Val *val)
1185 {
1186 int64 i;
1187 int tt;
1188
1189 tt = simsimtype(t);
1190
1191 // copy the constant for conversion
1192 nodconst(con, types[TINT8], 0);
1193 con->type = t;
1194 con->val = *val;
1195
1196 if(isint[tt]) {
1197 con->val.ctype = CTINT;
1198 con->val.u.xval = mal(sizeof *con->val.u.xval);
1199 switch(val->ctype) {
1200 default:
1201 fatal("convconst ctype=%d %lT", val->ctype, t);
1202 case CTINT:
1203 i = mpgetfix(val->u.xval);
1204 break;
1205 case CTBOOL:
1206 i = val->u.bval;
1207 break;
1208 case CTNIL:
1209 i = 0;
1210 break;
1211 }
1212 i = iconv(i, tt);
1213 mpmovecfix(con->val.u.xval, i);
1214 return;
1215 }
1216
1217 if(isfloat[tt]) {
1218 con->val = toflt(con->val);
1219 if(con->val.ctype != CTFLT)
1220 fatal("convconst ctype=%d %T", con->val.ctype, t);
1221 if(tt == TFLOAT32)
1222 con->val.u.fval = truncfltlit(con->val.u.fval, t);
1223 return;
1224 }
1225
1226 if(iscomplex[tt]) {
1227 con->val = tocplx(con->val);
1228 if(tt == TCOMPLEX64) {
1229 con->val.u.cval->real = *truncfltlit(&con->val.u.cval->real, types[TFLOAT32]);
1230 con->val.u.cval->imag = *truncfltlit(&con->val.u.cval->imag, types[TFLOAT32]);
1231 }
1232 return;
1233 }
1234
1235 fatal("convconst %lT constant", t);
1236
1237 }
1238
1239 // complex multiply v *= rv
1240 // (a, b) * (c, d) = (a*c - b*d, b*c + a*d)
1241 static void
1242 cmplxmpy(Mpcplx *v, Mpcplx *rv)
1243 {
1244 Mpflt ac, bd, bc, ad;
1245
1246 mpmovefltflt(&ac, &v->real);
1247 mpmulfltflt(&ac, &rv->real); // ac
1248
1249 mpmovefltflt(&bd, &v->imag);
1250 mpmulfltflt(&bd, &rv->imag); // bd
1251
1252 mpmovefltflt(&bc, &v->imag);
1253 mpmulfltflt(&bc, &rv->real); // bc
1254
1255 mpmovefltflt(&ad, &v->real);
1256 mpmulfltflt(&ad, &rv->imag); // ad
1257
1258 mpmovefltflt(&v->real, &ac);
1259 mpsubfltflt(&v->real, &bd); // ac-bd
1260
1261 mpmovefltflt(&v->imag, &bc);
1262 mpaddfltflt(&v->imag, &ad); // bc+ad
1263 }
1264
1265 // complex divide v /= rv
1266 // (a, b) / (c, d) = ((a*c + b*d), (b*c - a*d))/(c*c + d*d)
1267 static void
1268 cmplxdiv(Mpcplx *v, Mpcplx *rv)
1269 {
1270 Mpflt ac, bd, bc, ad, cc_plus_dd;
1271
1272 mpmovefltflt(&cc_plus_dd, &rv->real);
1273 mpmulfltflt(&cc_plus_dd, &rv->real); // cc
1274
1275 mpmovefltflt(&ac, &rv->imag);
1276 mpmulfltflt(&ac, &rv->imag); // dd
1277
1278 mpaddfltflt(&cc_plus_dd, &ac); // cc+dd
1279
1280 mpmovefltflt(&ac, &v->real);
1281 mpmulfltflt(&ac, &rv->real); // ac
1282
1283 mpmovefltflt(&bd, &v->imag);
1284 mpmulfltflt(&bd, &rv->imag); // bd
1285
1286 mpmovefltflt(&bc, &v->imag);
1287 mpmulfltflt(&bc, &rv->real); // bc
1288
1289 mpmovefltflt(&ad, &v->real);
1290 mpmulfltflt(&ad, &rv->imag); // ad
1291
1292 mpmovefltflt(&v->real, &ac);
1293 mpaddfltflt(&v->real, &bd); // ac+bd
1294 mpdivfltflt(&v->real, &cc_plus_dd); // (ac+bd)/(cc+dd)
1295
1296 mpmovefltflt(&v->imag, &bc);
1297 mpsubfltflt(&v->imag, &ad); // bc-ad
1298 mpdivfltflt(&v->imag, &cc_plus_dd); // (bc+ad)/(cc+dd)
1299 }