-
Notifications
You must be signed in to change notification settings - Fork 1
/
const.go
1810 lines (1513 loc) · 32.2 KB
/
const.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gc
import (
"github.com/glycerine/golang-repl/cmd/avail/obj"
"github.com/glycerine/golang-repl/cmd/compile/avail/big"
"strings"
)
// Ctype describes the constant kind of an "ideal" (untyped) constant.
type Ctype int8
const (
CTxxx Ctype = iota
CTINT
CTRUNE
CTFLT
CTCPLX
CTSTR
CTBOOL
CTNIL
)
type Val struct {
// U contains one of:
// bool bool when n.ValCtype() == CTBOOL
// *Mpint int when n.ValCtype() == CTINT, rune when n.ValCtype() == CTRUNE
// *Mpflt float when n.ValCtype() == CTFLT
// *Mpcplx pair of floats when n.ValCtype() == CTCPLX
// string string when n.ValCtype() == CTSTR
// *Nilval when n.ValCtype() == CTNIL
U interface{}
}
func (v Val) Ctype() Ctype {
switch x := v.U.(type) {
default:
Fatalf("unexpected Ctype for %T", v.U)
panic("not reached")
case nil:
return 0
case *NilVal:
return CTNIL
case bool:
return CTBOOL
case *Mpint:
if x.Rune {
return CTRUNE
}
return CTINT
case *Mpflt:
return CTFLT
case *Mpcplx:
return CTCPLX
case string:
return CTSTR
}
}
type NilVal struct{}
// IntLiteral returns the Node's literal value as an integer.
func (n *Node) IntLiteral() (x int64, ok bool) {
switch {
case n == nil:
return
case Isconst(n, CTINT):
return n.Int64(), true
case Isconst(n, CTBOOL):
return int64(obj.Bool2int(n.Bool())), true
}
return
}
// Int64 returns n as an int64.
// n must be an integer or rune constant.
func (n *Node) Int64() int64 {
if !Isconst(n, CTINT) {
Fatalf("Int(%v)", n)
}
return n.Val().U.(*Mpint).Int64()
}
// SetInt sets n's value to i.
// n must be an integer constant.
func (n *Node) SetInt(i int64) {
if !Isconst(n, CTINT) {
Fatalf("SetInt(%v)", n)
}
n.Val().U.(*Mpint).SetInt64(i)
}
// SetBigInt sets n's value to x.
// n must be an integer constant.
func (n *Node) SetBigInt(x *big.Int) {
if !Isconst(n, CTINT) {
Fatalf("SetBigInt(%v)", n)
}
n.Val().U.(*Mpint).Val.Set(x)
}
// Bool returns n as an bool.
// n must be an boolean constant.
func (n *Node) Bool() bool {
if !Isconst(n, CTBOOL) {
Fatalf("Int(%v)", n)
}
return n.Val().U.(bool)
}
// truncate float literal fv to 32-bit or 64-bit precision
// according to type; return truncated value.
func truncfltlit(oldv *Mpflt, t *Type) *Mpflt {
if t == nil {
return oldv
}
var v Val
v.U = oldv
overflow(v, t)
fv := newMpflt()
fv.Set(oldv)
// convert large precision literal floating
// into limited precision (float64 or float32)
switch t.Etype {
case TFLOAT64:
d := fv.Float64()
fv.SetFloat64(d)
case TFLOAT32:
d := fv.Float32()
fv.SetFloat64(d)
}
return fv
}
// NegOne returns a Node of type t with value -1.
func NegOne(t *Type) *Node {
n := Nodintconst(-1)
n = convlit(n, t)
return n
}
// canReuseNode indicates whether it is known to be safe
// to reuse a Node.
type canReuseNode bool
const (
noReuse canReuseNode = false // not necessarily safe to reuse
//reuseOK canReuseNode = true // safe to reuse
)
// convert n, if literal, to type t.
// implicit conversion.
// The result of convlit MUST be assigned back to n, e.g.
// n.Left = convlit(n.Left, t)
func convlit(n *Node, t *Type) *Node {
return convlit1(n, t, false, noReuse)
}
// convlit1 converts n, if literal, to type t.
// It returns a new node if necessary.
// The result of convlit1 MUST be assigned back to n, e.g.
// n.Left = convlit1(n.Left, t, explicit, reuse)
func convlit1(n *Node, t *Type, explicit bool, reuse canReuseNode) *Node {
if n == nil || t == nil || n.Type == nil || t.IsUntyped() || n.Type == t {
return n
}
if !explicit && !n.Type.IsUntyped() {
return n
}
if n.Op == OLITERAL && !reuse {
// Can't always set n.Type directly on OLITERAL nodes.
// See discussion on CL 20813.
nn := *n
n = &nn
reuse = true
}
switch n.Op {
default:
if n.Type == idealbool {
if t.IsBoolean() {
n.Type = t
} else {
n.Type = Types[TBOOL]
}
}
if n.Type.Etype == TIDEAL {
n.Left = convlit(n.Left, t)
n.Right = convlit(n.Right, t)
n.Type = t
}
return n
// target is invalid type for a constant? leave alone.
case OLITERAL:
if !okforconst[t.Etype] && n.Type.Etype != TNIL {
return defaultlitreuse(n, nil, reuse)
}
case OLSH, ORSH:
n.Left = convlit1(n.Left, t, explicit && n.Left.Type.IsUntyped(), noReuse)
t = n.Left.Type
if t != nil && t.Etype == TIDEAL && n.Val().Ctype() != CTINT {
n.SetVal(toint(n.Val()))
}
if t != nil && !t.IsInteger() {
Yyerror("invalid operation: %v (shift of type %v)", n, t)
t = nil
}
n.Type = t
return n
case OCOMPLEX:
if n.Type.Etype == TIDEAL {
switch t.Etype {
default:
// If trying to convert to non-complex type,
// leave as complex128 and let typechecker complain.
t = Types[TCOMPLEX128]
fallthrough
case TCOMPLEX128:
n.Type = t
n.Left = convlit(n.Left, Types[TFLOAT64])
n.Right = convlit(n.Right, Types[TFLOAT64])
case TCOMPLEX64:
n.Type = t
n.Left = convlit(n.Left, Types[TFLOAT32])
n.Right = convlit(n.Right, Types[TFLOAT32])
}
}
return n
}
// avoided repeated calculations, errors
if Eqtype(n.Type, t) {
return n
}
ct := consttype(n)
var et EType
if ct < 0 {
goto bad
}
et = t.Etype
if et == TINTER {
if ct == CTNIL && n.Type == Types[TNIL] {
n.Type = t
return n
}
return defaultlitreuse(n, nil, reuse)
}
switch ct {
default:
goto bad
case CTNIL:
switch et {
default:
n.Type = nil
goto bad
// let normal conversion code handle it
case TSTRING:
return n
case TARRAY:
goto bad
case TPTR32,
TPTR64,
TINTER,
TMAP,
TCHAN,
TFUNC,
TSLICE,
TUNSAFEPTR:
break
// A nil literal may be converted to uintptr
// if it is an unsafe.Pointer
case TUINTPTR:
if n.Type.Etype == TUNSAFEPTR {
n.SetVal(Val{new(Mpint)})
n.Val().U.(*Mpint).SetInt64(0)
} else {
goto bad
}
}
case CTSTR, CTBOOL:
if et != n.Type.Etype {
goto bad
}
case CTINT, CTRUNE, CTFLT, CTCPLX:
if n.Type.Etype == TUNSAFEPTR && t.Etype != TUINTPTR {
goto bad
}
ct := n.Val().Ctype()
if Isint[et] {
switch ct {
default:
goto bad
case CTCPLX, CTFLT, CTRUNE:
n.SetVal(toint(n.Val()))
fallthrough
case CTINT:
overflow(n.Val(), t)
}
} else if Isfloat[et] {
switch ct {
default:
goto bad
case CTCPLX, CTINT, CTRUNE:
n.SetVal(toflt(n.Val()))
fallthrough
case CTFLT:
n.SetVal(Val{truncfltlit(n.Val().U.(*Mpflt), t)})
}
} else if Iscomplex[et] {
switch ct {
default:
goto bad
case CTFLT, CTINT, CTRUNE:
n.SetVal(tocplx(n.Val()))
fallthrough
case CTCPLX:
overflow(n.Val(), t)
}
} else if et == TSTRING && (ct == CTINT || ct == CTRUNE) && explicit {
n.SetVal(tostr(n.Val()))
} else {
goto bad
}
}
n.Type = t
return n
bad:
if n.Diag == 0 {
if !t.Broke {
Yyerror("cannot convert %v to type %v", n, t)
}
n.Diag = 1
}
if n.Type.IsUntyped() {
n = defaultlitreuse(n, nil, reuse)
}
return n
}
func copyval(v Val) Val {
switch u := v.U.(type) {
case *Mpint:
i := new(Mpint)
i.Set(u)
i.Rune = u.Rune
v.U = i
case *Mpflt:
f := newMpflt()
f.Set(u)
v.U = f
case *Mpcplx:
c := new(Mpcplx)
c.Real.Set(&u.Real)
c.Imag.Set(&u.Imag)
v.U = c
}
return v
}
func tocplx(v Val) Val {
switch u := v.U.(type) {
case *Mpint:
c := new(Mpcplx)
c.Real.SetInt(u)
c.Imag.SetFloat64(0.0)
v.U = c
case *Mpflt:
c := new(Mpcplx)
c.Real.Set(u)
c.Imag.SetFloat64(0.0)
v.U = c
}
return v
}
func toflt(v Val) Val {
switch u := v.U.(type) {
case *Mpint:
f := newMpflt()
f.SetInt(u)
v.U = f
case *Mpcplx:
f := newMpflt()
f.Set(&u.Real)
if u.Imag.CmpFloat64(0) != 0 {
Yyerror("constant %v%vi truncated to real", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp|FmtSign))
}
v.U = f
}
return v
}
func toint(v Val) Val {
switch u := v.U.(type) {
case *Mpint:
if u.Rune {
i := new(Mpint)
i.Set(u)
v.U = i
}
case *Mpflt:
i := new(Mpint)
if i.SetFloat(u) < 0 {
msg := "constant %v truncated to integer"
// provide better error message if SetFloat failed because f was too large
if u.Val.IsInt() {
msg = "constant %v overflows integer"
}
Yyerror(msg, fconv(u, FmtSharp))
}
v.U = i
case *Mpcplx:
i := new(Mpint)
if i.SetFloat(&u.Real) < 0 {
Yyerror("constant %v%vi truncated to integer", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp|FmtSign))
}
if u.Imag.CmpFloat64(0) != 0 {
Yyerror("constant %v%vi truncated to real", fconv(&u.Real, FmtSharp), fconv(&u.Imag, FmtSharp|FmtSign))
}
v.U = i
}
return v
}
func doesoverflow(v Val, t *Type) bool {
switch u := v.U.(type) {
case *Mpint:
if !t.IsInteger() {
Fatalf("overflow: %v integer constant", t)
}
return u.Cmp(Minintval[t.Etype]) < 0 || u.Cmp(Maxintval[t.Etype]) > 0
case *Mpflt:
if !t.IsFloat() {
Fatalf("overflow: %v floating-point constant", t)
}
return u.Cmp(minfltval[t.Etype]) <= 0 || u.Cmp(maxfltval[t.Etype]) >= 0
case *Mpcplx:
if !t.IsComplex() {
Fatalf("overflow: %v complex constant", t)
}
return u.Real.Cmp(minfltval[t.Etype]) <= 0 || u.Real.Cmp(maxfltval[t.Etype]) >= 0 ||
u.Imag.Cmp(minfltval[t.Etype]) <= 0 || u.Imag.Cmp(maxfltval[t.Etype]) >= 0
}
return false
}
func overflow(v Val, t *Type) {
// v has already been converted
// to appropriate form for t.
if t == nil || t.Etype == TIDEAL {
return
}
// Only uintptrs may be converted to unsafe.Pointer, which cannot overflow.
if t.Etype == TUNSAFEPTR {
return
}
if doesoverflow(v, t) {
Yyerror("constant %s overflows %v", vconv(v, 0), t)
}
}
func tostr(v Val) Val {
switch u := v.U.(type) {
case *Mpint:
var i int64 = 0xFFFD
if u.Cmp(Minintval[TUINT32]) >= 0 && u.Cmp(Maxintval[TUINT32]) <= 0 {
i = u.Int64()
}
v.U = string(i)
case *NilVal:
// Can happen because of string([]byte(nil)).
v.U = ""
}
return v
}
func consttype(n *Node) Ctype {
if n == nil || n.Op != OLITERAL {
return -1
}
return n.Val().Ctype()
}
func Isconst(n *Node, ct Ctype) bool {
t := consttype(n)
// If the caller is asking for CTINT, allow CTRUNE too.
// Makes life easier for back ends.
return t == ct || (ct == CTINT && t == CTRUNE)
}
func saveorig(n *Node) *Node {
if n == n.Orig {
// duplicate node for n->orig.
n1 := Nod(OLITERAL, nil, nil)
n.Orig = n1
*n1 = *n
}
return n.Orig
}
// if n is constant, rewrite as OLITERAL node.
func evconst(n *Node) {
// pick off just the opcodes that can be
// constant evaluated.
switch n.Op {
default:
return
case OADD,
OAND,
OANDAND,
OANDNOT,
OARRAYBYTESTR,
OCOM,
ODIV,
OEQ,
OGE,
OGT,
OLE,
OLSH,
OLT,
OMINUS,
OMOD,
OMUL,
ONE,
ONOT,
OOR,
OOROR,
OPLUS,
ORSH,
OSUB,
OXOR:
break
case OCONV:
if n.Type == nil {
return
}
if !okforconst[n.Type.Etype] && n.Type.Etype != TNIL {
return
}
// merge adjacent constants in the argument list.
case OADDSTR:
s := n.List.Slice()
for i1 := 0; i1 < len(s); i1++ {
if Isconst(s[i1], CTSTR) && i1+1 < len(s) && Isconst(s[i1+1], CTSTR) {
// merge from i1 up to but not including i2
var strs []string
i2 := i1
for i2 < len(s) && Isconst(s[i2], CTSTR) {
strs = append(strs, s[i2].Val().U.(string))
i2++
}
nl := *s[i1]
nl.Orig = &nl
nl.SetVal(Val{strings.Join(strs, "")})
s[i1] = &nl
s = append(s[:i1+1], s[i2:]...)
}
}
if len(s) == 1 && Isconst(s[0], CTSTR) {
n.Op = OLITERAL
n.SetVal(s[0].Val())
} else {
n.List.Set(s)
}
return
}
nl := n.Left
if nl == nil || nl.Type == nil {
return
}
if consttype(nl) < 0 {
return
}
wl := nl.Type.Etype
if Isint[wl] || Isfloat[wl] || Iscomplex[wl] {
wl = TIDEAL
}
// avoid constant conversions in switches below
const (
CTINT_ = uint32(CTINT)
CTRUNE_ = uint32(CTRUNE)
CTFLT_ = uint32(CTFLT)
CTCPLX_ = uint32(CTCPLX)
CTSTR_ = uint32(CTSTR)
CTBOOL_ = uint32(CTBOOL)
CTNIL_ = uint32(CTNIL)
OCONV_ = uint32(OCONV) << 16
OARRAYBYTESTR_ = uint32(OARRAYBYTESTR) << 16
OPLUS_ = uint32(OPLUS) << 16
OMINUS_ = uint32(OMINUS) << 16
OCOM_ = uint32(OCOM) << 16
ONOT_ = uint32(ONOT) << 16
OLSH_ = uint32(OLSH) << 16
ORSH_ = uint32(ORSH) << 16
OADD_ = uint32(OADD) << 16
OSUB_ = uint32(OSUB) << 16
OMUL_ = uint32(OMUL) << 16
ODIV_ = uint32(ODIV) << 16
OMOD_ = uint32(OMOD) << 16
OOR_ = uint32(OOR) << 16
OAND_ = uint32(OAND) << 16
OANDNOT_ = uint32(OANDNOT) << 16
OXOR_ = uint32(OXOR) << 16
OEQ_ = uint32(OEQ) << 16
ONE_ = uint32(ONE) << 16
OLT_ = uint32(OLT) << 16
OLE_ = uint32(OLE) << 16
OGE_ = uint32(OGE) << 16
OGT_ = uint32(OGT) << 16
OOROR_ = uint32(OOROR) << 16
OANDAND_ = uint32(OANDAND) << 16
)
nr := n.Right
var rv Val
var lno int32
var wr EType
var v Val
var norig *Node
var nn *Node
if nr == nil {
// copy numeric value to avoid modifying
// nl, in case someone still refers to it (e.g. iota).
v = nl.Val()
if wl == TIDEAL {
v = copyval(v)
}
switch uint32(n.Op)<<16 | uint32(v.Ctype()) {
default:
if n.Diag == 0 {
Yyerror("illegal constant expression %v %v", n.Op, nl.Type)
n.Diag = 1
}
return
case OCONV_ | CTNIL_,
OARRAYBYTESTR_ | CTNIL_:
if n.Type.IsString() {
v = tostr(v)
nl.Type = n.Type
break
}
fallthrough
case OCONV_ | CTINT_,
OCONV_ | CTRUNE_,
OCONV_ | CTFLT_,
OCONV_ | CTSTR_,
OCONV_ | CTBOOL_:
nl = convlit1(nl, n.Type, true, false)
v = nl.Val()
case OPLUS_ | CTINT_,
OPLUS_ | CTRUNE_:
break
case OMINUS_ | CTINT_,
OMINUS_ | CTRUNE_:
v.U.(*Mpint).Neg()
case OCOM_ | CTINT_,
OCOM_ | CTRUNE_:
var et EType = Txxx
if nl.Type != nil {
et = nl.Type.Etype
}
// calculate the mask in b
// result will be (a ^ mask)
var b Mpint
switch et {
// signed guys change sign
default:
b.SetInt64(-1)
// unsigned guys invert their bits
case TUINT8,
TUINT16,
TUINT32,
TUINT64,
TUINT,
TUINTPTR:
b.Set(Maxintval[et])
}
v.U.(*Mpint).Xor(&b)
case OPLUS_ | CTFLT_:
break
case OMINUS_ | CTFLT_:
v.U.(*Mpflt).Neg()
case OPLUS_ | CTCPLX_:
break
case OMINUS_ | CTCPLX_:
v.U.(*Mpcplx).Real.Neg()
v.U.(*Mpcplx).Imag.Neg()
case ONOT_ | CTBOOL_:
if !v.U.(bool) {
goto settrue
}
goto setfalse
}
goto ret
}
if nr.Type == nil {
return
}
if consttype(nr) < 0 {
return
}
wr = nr.Type.Etype
if Isint[wr] || Isfloat[wr] || Iscomplex[wr] {
wr = TIDEAL
}
// check for compatible general types (numeric, string, etc)
if wl != wr {
goto illegal
}
// check for compatible types.
switch n.Op {
// ideal const mixes with anything but otherwise must match.
default:
if nl.Type.Etype != TIDEAL {
nr = defaultlit(nr, nl.Type)
n.Right = nr
}
if nr.Type.Etype != TIDEAL {
nl = defaultlit(nl, nr.Type)
n.Left = nl
}
if nl.Type.Etype != nr.Type.Etype {
goto illegal
}
// right must be unsigned.
// left can be ideal.
case OLSH, ORSH:
nr = defaultlit(nr, Types[TUINT])
n.Right = nr
if nr.Type != nil && (nr.Type.IsSigned() || !nr.Type.IsInteger()) {
goto illegal
}
if nl.Val().Ctype() != CTRUNE {
nl.SetVal(toint(nl.Val()))
}
nr.SetVal(toint(nr.Val()))
}
// copy numeric value to avoid modifying
// n->left, in case someone still refers to it (e.g. iota).
v = nl.Val()
if wl == TIDEAL {
v = copyval(v)
}
rv = nr.Val()
// convert to common ideal
if v.Ctype() == CTCPLX || rv.Ctype() == CTCPLX {
v = tocplx(v)
rv = tocplx(rv)
}
if v.Ctype() == CTFLT || rv.Ctype() == CTFLT {
v = toflt(v)
rv = toflt(rv)
}
// Rune and int turns into rune.
if v.Ctype() == CTRUNE && rv.Ctype() == CTINT {
i := new(Mpint)
i.Set(rv.U.(*Mpint))
i.Rune = true
rv.U = i
}
if v.Ctype() == CTINT && rv.Ctype() == CTRUNE {
if n.Op == OLSH || n.Op == ORSH {
i := new(Mpint)
i.Set(rv.U.(*Mpint))
rv.U = i
} else {
i := new(Mpint)
i.Set(v.U.(*Mpint))
i.Rune = true
v.U = i
}
}
if v.Ctype() != rv.Ctype() {
// Use of undefined name as constant?
if (v.Ctype() == 0 || rv.Ctype() == 0) && nerrors > 0 {
return
}
Fatalf("constant type mismatch %v(%d) %v(%d)", nl.Type, v.Ctype(), nr.Type, rv.Ctype())
}
// run op
switch uint32(n.Op)<<16 | uint32(v.Ctype()) {
default:
goto illegal
case OADD_ | CTINT_,
OADD_ | CTRUNE_:
v.U.(*Mpint).Add(rv.U.(*Mpint))
case OSUB_ | CTINT_,
OSUB_ | CTRUNE_:
v.U.(*Mpint).Sub(rv.U.(*Mpint))
case OMUL_ | CTINT_,
OMUL_ | CTRUNE_:
v.U.(*Mpint).Mul(rv.U.(*Mpint))
case ODIV_ | CTINT_,
ODIV_ | CTRUNE_:
if rv.U.(*Mpint).CmpInt64(0) == 0 {
Yyerror("division by zero")
v.U.(*Mpint).SetOverflow()
break
}
v.U.(*Mpint).Quo(rv.U.(*Mpint))
case OMOD_ | CTINT_,
OMOD_ | CTRUNE_:
if rv.U.(*Mpint).CmpInt64(0) == 0 {
Yyerror("division by zero")
v.U.(*Mpint).SetOverflow()
break
}
v.U.(*Mpint).Rem(rv.U.(*Mpint))
case OLSH_ | CTINT_,
OLSH_ | CTRUNE_:
v.U.(*Mpint).Lsh(rv.U.(*Mpint))
case ORSH_ | CTINT_,
ORSH_ | CTRUNE_:
v.U.(*Mpint).Rsh(rv.U.(*Mpint))
case OOR_ | CTINT_,
OOR_ | CTRUNE_:
v.U.(*Mpint).Or(rv.U.(*Mpint))
case OAND_ | CTINT_,
OAND_ | CTRUNE_:
v.U.(*Mpint).And(rv.U.(*Mpint))
case OANDNOT_ | CTINT_,
OANDNOT_ | CTRUNE_:
v.U.(*Mpint).AndNot(rv.U.(*Mpint))
case OXOR_ | CTINT_,
OXOR_ | CTRUNE_:
v.U.(*Mpint).Xor(rv.U.(*Mpint))
case OADD_ | CTFLT_:
v.U.(*Mpflt).Add(rv.U.(*Mpflt))
case OSUB_ | CTFLT_:
v.U.(*Mpflt).Sub(rv.U.(*Mpflt))
case OMUL_ | CTFLT_:
v.U.(*Mpflt).Mul(rv.U.(*Mpflt))
case ODIV_ | CTFLT_:
if rv.U.(*Mpflt).CmpFloat64(0) == 0 {
Yyerror("division by zero")
v.U.(*Mpflt).SetFloat64(1.0)
break
}
v.U.(*Mpflt).Quo(rv.U.(*Mpflt))
// The default case above would print 'ideal % ideal',
// which is not quite an ideal error.
case OMOD_ | CTFLT_:
if n.Diag == 0 {
Yyerror("illegal constant expression: floating-point %% operation")
n.Diag = 1
}
return
case OADD_ | CTCPLX_:
v.U.(*Mpcplx).Real.Add(&rv.U.(*Mpcplx).Real)
v.U.(*Mpcplx).Imag.Add(&rv.U.(*Mpcplx).Imag)
case OSUB_ | CTCPLX_:
v.U.(*Mpcplx).Real.Sub(&rv.U.(*Mpcplx).Real)
v.U.(*Mpcplx).Imag.Sub(&rv.U.(*Mpcplx).Imag)
case OMUL_ | CTCPLX_:
cmplxmpy(v.U.(*Mpcplx), rv.U.(*Mpcplx))
case ODIV_ | CTCPLX_:
if rv.U.(*Mpcplx).Real.CmpFloat64(0) == 0 && rv.U.(*Mpcplx).Imag.CmpFloat64(0) == 0 {
Yyerror("complex division by zero")
rv.U.(*Mpcplx).Real.SetFloat64(1.0)
rv.U.(*Mpcplx).Imag.SetFloat64(0.0)
break
}
cmplxdiv(v.U.(*Mpcplx), rv.U.(*Mpcplx))
case OEQ_ | CTNIL_:
goto settrue
case ONE_ | CTNIL_:
goto setfalse
case OEQ_ | CTINT_,
OEQ_ | CTRUNE_:
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) == 0 {
goto settrue
}
goto setfalse
case ONE_ | CTINT_,
ONE_ | CTRUNE_:
if v.U.(*Mpint).Cmp(rv.U.(*Mpint)) != 0 {
goto settrue
}