Debian Codegraph @

go/ssa/interp/ops.go

raw symbols SCIP
1 // Copyright 2013 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package interp
6
7 import (
8 "bytes"
9 "fmt"
10 "go/constant"
11 "go/token"
12 "go/types"
13 "os"
14 "reflect"
15 "strings"
16 "unsafe"
17
18 "golang.org/x/tools/go/ssa"
19 "golang.org/x/tools/internal/typeparams"
20 )
21
22 // If the target program panics, the interpreter panics with this type.
23 type targetPanic struct {
24 v value
25 }
26
27 func (p targetPanic) String() string {
28 return toString(p.v)
29 }
30
31 // If the target program calls exit, the interpreter panics with this type.
32 type exitPanic int
33
34 // constValue returns the value of the constant with the
35 // dynamic type tag appropriate for c.Type().
36 func constValue(c *ssa.Const) value {
37 if c.Value == nil {
38 return zero(c.Type()) // typed zero
39 }
40 // c is not a type parameter so it's underlying type is basic.
41
42 if t, ok := c.Type().Underlying().(*types.Basic); ok {
43 // TODO(adonovan): eliminate untyped constants from SSA form.
44 switch t.Kind() {
45 case types.Bool, types.UntypedBool:
46 return constant.BoolVal(c.Value)
47 case types.Int, types.UntypedInt:
48 // Assume sizeof(int) is same on host and target.
49 return int(c.Int64())
50 case types.Int8:
51 return int8(c.Int64())
52 case types.Int16:
53 return int16(c.Int64())
54 case types.Int32, types.UntypedRune:
55 return int32(c.Int64())
56 case types.Int64:
57 return c.Int64()
58 case types.Uint:
59 // Assume sizeof(uint) is same on host and target.
60 return uint(c.Uint64())
61 case types.Uint8:
62 return uint8(c.Uint64())
63 case types.Uint16:
64 return uint16(c.Uint64())
65 case types.Uint32:
66 return uint32(c.Uint64())
67 case types.Uint64:
68 return c.Uint64()
69 case types.Uintptr:
70 // Assume sizeof(uintptr) is same on host and target.
71 return uintptr(c.Uint64())
72 case types.Float32:
73 return float32(c.Float64())
74 case types.Float64, types.UntypedFloat:
75 return c.Float64()
76 case types.Complex64:
77 return complex64(c.Complex128())
78 case types.Complex128, types.UntypedComplex:
79 return c.Complex128()
80 case types.String, types.UntypedString:
81 if c.Value.Kind() == constant.String {
82 return constant.StringVal(c.Value)
83 }
84 return string(rune(c.Int64()))
85 }
86 }
87
88 panic(fmt.Sprintf("constValue: %s", c))
89 }
90
91 // fitsInt returns true if x fits in type int according to sizes.
92 func fitsInt(x int64, sizes types.Sizes) bool {
93 intSize := sizes.Sizeof(types.Typ[types.Int])
94 if intSize < sizes.Sizeof(types.Typ[types.Int64]) {
95 maxInt := int64(1)<<((intSize*8)-1) - 1
96 minInt := -int64(1) << ((intSize * 8) - 1)
97 return minInt <= x && x <= maxInt
98 }
99 return true
100 }
101
102 // asInt64 converts x, which must be an integer, to an int64.
103 //
104 // Callers that need a value directly usable as an int should combine this with fitsInt().
105 func asInt64(x value) int64 {
106 switch x := x.(type) {
107 case int:
108 return int64(x)
109 case int8:
110 return int64(x)
111 case int16:
112 return int64(x)
113 case int32:
114 return int64(x)
115 case int64:
116 return x
117 case uint:
118 return int64(x)
119 case uint8:
120 return int64(x)
121 case uint16:
122 return int64(x)
123 case uint32:
124 return int64(x)
125 case uint64:
126 return int64(x)
127 case uintptr:
128 return int64(x)
129 }
130 panic(fmt.Sprintf("cannot convert %T to int64", x))
131 }
132
133 // asUint64 converts x, which must be an unsigned integer, to a uint64
134 // suitable for use as a bitwise shift count.
135 func asUint64(x value) uint64 {
136 switch x := x.(type) {
137 case uint:
138 return uint64(x)
139 case uint8:
140 return uint64(x)
141 case uint16:
142 return uint64(x)
143 case uint32:
144 return uint64(x)
145 case uint64:
146 return x
147 case uintptr:
148 return uint64(x)
149 }
150 panic(fmt.Sprintf("cannot convert %T to uint64", x))
151 }
152
153 // asUnsigned returns the value of x, which must be an integer type, as its equivalent unsigned type,
154 // and returns true if x is non-negative.
155 func asUnsigned(x value) (value, bool) {
156 switch x := x.(type) {
157 case int:
158 return uint(x), x >= 0
159 case int8:
160 return uint8(x), x >= 0
161 case int16:
162 return uint16(x), x >= 0
163 case int32:
164 return uint32(x), x >= 0
165 case int64:
166 return uint64(x), x >= 0
167 case uint, uint8, uint32, uint64, uintptr:
168 return x, true
169 }
170 panic(fmt.Sprintf("cannot convert %T to unsigned", x))
171 }
172
173 // zero returns a new "zero" value of the specified type.
174 func zero(t types.Type) value {
175 switch t := t.(type) {
176 case *types.Basic:
177 if t.Kind() == types.UntypedNil {
178 panic("untyped nil has no zero value")
179 }
180 if t.Info()&types.IsUntyped != 0 {
181 // TODO(adonovan): make it an invariant that
182 // this is unreachable. Currently some
183 // constants have 'untyped' types when they
184 // should be defaulted by the typechecker.
185 t = types.Default(t).(*types.Basic)
186 }
187 switch t.Kind() {
188 case types.Bool:
189 return false
190 case types.Int:
191 return int(0)
192 case types.Int8:
193 return int8(0)
194 case types.Int16:
195 return int16(0)
196 case types.Int32:
197 return int32(0)
198 case types.Int64:
199 return int64(0)
200 case types.Uint:
201 return uint(0)
202 case types.Uint8:
203 return uint8(0)
204 case types.Uint16:
205 return uint16(0)
206 case types.Uint32:
207 return uint32(0)
208 case types.Uint64:
209 return uint64(0)
210 case types.Uintptr:
211 return uintptr(0)
212 case types.Float32:
213 return float32(0)
214 case types.Float64:
215 return float64(0)
216 case types.Complex64:
217 return complex64(0)
218 case types.Complex128:
219 return complex128(0)
220 case types.String:
221 return ""
222 case types.UnsafePointer:
223 return unsafe.Pointer(nil)
224 default:
225 panic(fmt.Sprint("zero for unexpected type:", t))
226 }
227 case *types.Pointer:
228 return (*value)(nil)
229 case *types.Array:
230 a := make(array, t.Len())
231 for i := range a {
232 a[i] = zero(t.Elem())
233 }
234 return a
235 case *types.Named:
236 return zero(t.Underlying())
237 case *types.Alias:
238 return zero(types.Unalias(t))
239 case *types.Interface:
240 return iface{} // nil type, methodset and value
241 case *types.Slice:
242 return []value(nil)
243 case *types.Struct:
244 s := make(structure, t.NumFields())
245 for i := range s {
246 s[i] = zero(t.Field(i).Type())
247 }
248 return s
249 case *types.Tuple:
250 if t.Len() == 1 {
251 return zero(t.At(0).Type())
252 }
253 s := make(tuple, t.Len())
254 for i := range s {
255 s[i] = zero(t.At(i).Type())
256 }
257 return s
258 case *types.Chan:
259 return chan value(nil)
260 case *types.Map:
261 if usesBuiltinMap(t.Key()) {
262 return map[value]value(nil)
263 }
264 return (*hashmap)(nil)
265 case *types.Signature:
266 return (*ssa.Function)(nil)
267 }
268 panic(fmt.Sprint("zero: unexpected ", t))
269 }
270
271 // slice returns x[lo:hi:max]. Any of lo, hi and max may be nil.
272 func slice(x, lo, hi, max value) value {
273 var Len, Cap int
274 switch x := x.(type) {
275 case string:
276 Len = len(x)
277 case []value:
278 Len = len(x)
279 Cap = cap(x)
280 case *value: // *array
281 a := (*x).(array)
282 Len = len(a)
283 Cap = cap(a)
284 }
285
286 l := int64(0)
287 if lo != nil {
288 l = asInt64(lo)
289 }
290
291 h := int64(Len)
292 if hi != nil {
293 h = asInt64(hi)
294 }
295
296 m := int64(Cap)
297 if max != nil {
298 m = asInt64(max)
299 }
300
301 switch x := x.(type) {
302 case string:
303 return x[l:h]
304 case []value:
305 return x[l:h:m]
306 case *value: // *array
307 a := (*x).(array)
308 return []value(a)[l:h:m]
309 }
310 panic(fmt.Sprintf("slice: unexpected X type: %T", x))
311 }
312
313 // lookup returns x[idx] where x is a map.
314 func lookup(instr *ssa.Lookup, x, idx value) value {
315 switch x := x.(type) { // map or string
316 case map[value]value, *hashmap:
317 var v value
318 var ok bool
319 switch x := x.(type) {
320 case map[value]value:
321 v, ok = x[idx]
322 case *hashmap:
323 v = x.lookup(idx.(hashable))
324 ok = v != nil
325 }
326 if !ok {
327 v = zero(instr.X.Type().Underlying().(*types.Map).Elem())
328 }
329 if instr.CommaOk {
330 v = tuple{v, ok}
331 }
332 return v
333 }
334 panic(fmt.Sprintf("unexpected x type in Lookup: %T", x))
335 }
336
337 // binop implements all arithmetic and logical binary operators for
338 // numeric datatypes and strings. Both operands must have identical
339 // dynamic type.
340 func binop(op token.Token, t types.Type, x, y value) value {
341 switch op {
342 case token.ADD:
343 switch x.(type) {
344 case int:
345 return x.(int) + y.(int)
346 case int8:
347 return x.(int8) + y.(int8)
348 case int16:
349 return x.(int16) + y.(int16)
350 case int32:
351 return x.(int32) + y.(int32)
352 case int64:
353 return x.(int64) + y.(int64)
354 case uint:
355 return x.(uint) + y.(uint)
356 case uint8:
357 return x.(uint8) + y.(uint8)
358 case uint16:
359 return x.(uint16) + y.(uint16)
360 case uint32:
361 return x.(uint32) + y.(uint32)
362 case uint64:
363 return x.(uint64) + y.(uint64)
364 case uintptr:
365 return x.(uintptr) + y.(uintptr)
366 case float32:
367 return x.(float32) + y.(float32)
368 case float64:
369 return x.(float64) + y.(float64)
370 case complex64:
371 return x.(complex64) + y.(complex64)
372 case complex128:
373 return x.(complex128) + y.(complex128)
374 case string:
375 return x.(string) + y.(string)
376 }
377
378 case token.SUB:
379 switch x.(type) {
380 case int:
381 return x.(int) - y.(int)
382 case int8:
383 return x.(int8) - y.(int8)
384 case int16:
385 return x.(int16) - y.(int16)
386 case int32:
387 return x.(int32) - y.(int32)
388 case int64:
389 return x.(int64) - y.(int64)
390 case uint:
391 return x.(uint) - y.(uint)
392 case uint8:
393 return x.(uint8) - y.(uint8)
394 case uint16:
395 return x.(uint16) - y.(uint16)
396 case uint32:
397 return x.(uint32) - y.(uint32)
398 case uint64:
399 return x.(uint64) - y.(uint64)
400 case uintptr:
401 return x.(uintptr) - y.(uintptr)
402 case float32:
403 return x.(float32) - y.(float32)
404 case float64:
405 return x.(float64) - y.(float64)
406 case complex64:
407 return x.(complex64) - y.(complex64)
408 case complex128:
409 return x.(complex128) - y.(complex128)
410 }
411
412 case token.MUL:
413 switch x.(type) {
414 case int:
415 return x.(int) * y.(int)
416 case int8:
417 return x.(int8) * y.(int8)
418 case int16:
419 return x.(int16) * y.(int16)
420 case int32:
421 return x.(int32) * y.(int32)
422 case int64:
423 return x.(int64) * y.(int64)
424 case uint:
425 return x.(uint) * y.(uint)
426 case uint8:
427 return x.(uint8) * y.(uint8)
428 case uint16:
429 return x.(uint16) * y.(uint16)
430 case uint32:
431 return x.(uint32) * y.(uint32)
432 case uint64:
433 return x.(uint64) * y.(uint64)
434 case uintptr:
435 return x.(uintptr) * y.(uintptr)
436 case float32:
437 return x.(float32) * y.(float32)
438 case float64:
439 return x.(float64) * y.(float64)
440 case complex64:
441 return x.(complex64) * y.(complex64)
442 case complex128:
443 return x.(complex128) * y.(complex128)
444 }
445
446 case token.QUO:
447 switch x.(type) {
448 case int:
449 return x.(int) / y.(int)
450 case int8:
451 return x.(int8) / y.(int8)
452 case int16:
453 return x.(int16) / y.(int16)
454 case int32:
455 return x.(int32) / y.(int32)
456 case int64:
457 return x.(int64) / y.(int64)
458 case uint:
459 return x.(uint) / y.(uint)
460 case uint8:
461 return x.(uint8) / y.(uint8)
462 case uint16:
463 return x.(uint16) / y.(uint16)
464 case uint32:
465 return x.(uint32) / y.(uint32)
466 case uint64:
467 return x.(uint64) / y.(uint64)
468 case uintptr:
469 return x.(uintptr) / y.(uintptr)
470 case float32:
471 return x.(float32) / y.(float32)
472 case float64:
473 return x.(float64) / y.(float64)
474 case complex64:
475 return x.(complex64) / y.(complex64)
476 case complex128:
477 return x.(complex128) / y.(complex128)
478 }
479
480 case token.REM:
481 switch x.(type) {
482 case int:
483 return x.(int) % y.(int)
484 case int8:
485 return x.(int8) % y.(int8)
486 case int16:
487 return x.(int16) % y.(int16)
488 case int32:
489 return x.(int32) % y.(int32)
490 case int64:
491 return x.(int64) % y.(int64)
492 case uint:
493 return x.(uint) % y.(uint)
494 case uint8:
495 return x.(uint8) % y.(uint8)
496 case uint16:
497 return x.(uint16) % y.(uint16)
498 case uint32:
499 return x.(uint32) % y.(uint32)
500 case uint64:
501 return x.(uint64) % y.(uint64)
502 case uintptr:
503 return x.(uintptr) % y.(uintptr)
504 }
505
506 case token.AND:
507 switch x.(type) {
508 case int:
509 return x.(int) & y.(int)
510 case int8:
511 return x.(int8) & y.(int8)
512 case int16:
513 return x.(int16) & y.(int16)
514 case int32:
515 return x.(int32) & y.(int32)
516 case int64:
517 return x.(int64) & y.(int64)
518 case uint:
519 return x.(uint) & y.(uint)
520 case uint8:
521 return x.(uint8) & y.(uint8)
522 case uint16:
523 return x.(uint16) & y.(uint16)
524 case uint32:
525 return x.(uint32) & y.(uint32)
526 case uint64:
527 return x.(uint64) & y.(uint64)
528 case uintptr:
529 return x.(uintptr) & y.(uintptr)
530 }
531
532 case token.OR:
533 switch x.(type) {
534 case int:
535 return x.(int) | y.(int)
536 case int8:
537 return x.(int8) | y.(int8)
538 case int16:
539 return x.(int16) | y.(int16)
540 case int32:
541 return x.(int32) | y.(int32)
542 case int64:
543 return x.(int64) | y.(int64)
544 case uint:
545 return x.(uint) | y.(uint)
546 case uint8:
547 return x.(uint8) | y.(uint8)
548 case uint16:
549 return x.(uint16) | y.(uint16)
550 case uint32:
551 return x.(uint32) | y.(uint32)
552 case uint64:
553 return x.(uint64) | y.(uint64)
554 case uintptr:
555 return x.(uintptr) | y.(uintptr)
556 }
557
558 case token.XOR:
559 switch x.(type) {
560 case int:
561 return x.(int) ^ y.(int)
562 case int8:
563 return x.(int8) ^ y.(int8)
564 case int16:
565 return x.(int16) ^ y.(int16)
566 case int32:
567 return x.(int32) ^ y.(int32)
568 case int64:
569 return x.(int64) ^ y.(int64)
570 case uint:
571 return x.(uint) ^ y.(uint)
572 case uint8:
573 return x.(uint8) ^ y.(uint8)
574 case uint16:
575 return x.(uint16) ^ y.(uint16)
576 case uint32:
577 return x.(uint32) ^ y.(uint32)
578 case uint64:
579 return x.(uint64) ^ y.(uint64)
580 case uintptr:
581 return x.(uintptr) ^ y.(uintptr)
582 }
583
584 case token.AND_NOT:
585 switch x.(type) {
586 case int:
587 return x.(int) &^ y.(int)
588 case int8:
589 return x.(int8) &^ y.(int8)
590 case int16:
591 return x.(int16) &^ y.(int16)
592 case int32:
593 return x.(int32) &^ y.(int32)
594 case int64:
595 return x.(int64) &^ y.(int64)
596 case uint:
597 return x.(uint) &^ y.(uint)
598 case uint8:
599 return x.(uint8) &^ y.(uint8)
600 case uint16:
601 return x.(uint16) &^ y.(uint16)
602 case uint32:
603 return x.(uint32) &^ y.(uint32)
604 case uint64:
605 return x.(uint64) &^ y.(uint64)
606 case uintptr:
607 return x.(uintptr) &^ y.(uintptr)
608 }
609
610 case token.SHL:
611 u, ok := asUnsigned(y)
612 if !ok {
613 panic("negative shift amount")
614 }
615 y := asUint64(u)
616 switch x.(type) {
617 case int:
618 return x.(int) << y
619 case int8:
620 return x.(int8) << y
621 case int16:
622 return x.(int16) << y
623 case int32:
624 return x.(int32) << y
625 case int64:
626 return x.(int64) << y
627 case uint:
628 return x.(uint) << y
629 case uint8:
630 return x.(uint8) << y
631 case uint16:
632 return x.(uint16) << y
633 case uint32:
634 return x.(uint32) << y
635 case uint64:
636 return x.(uint64) << y
637 case uintptr:
638 return x.(uintptr) << y
639 }
640
641 case token.SHR:
642 u, ok := asUnsigned(y)
643 if !ok {
644 panic("negative shift amount")
645 }
646 y := asUint64(u)
647 switch x.(type) {
648 case int:
649 return x.(int) >> y
650 case int8:
651 return x.(int8) >> y
652 case int16:
653 return x.(int16) >> y
654 case int32:
655 return x.(int32) >> y
656 case int64:
657 return x.(int64) >> y
658 case uint:
659 return x.(uint) >> y
660 case uint8:
661 return x.(uint8) >> y
662 case uint16:
663 return x.(uint16) >> y
664 case uint32:
665 return x.(uint32) >> y
666 case uint64:
667 return x.(uint64) >> y
668 case uintptr:
669 return x.(uintptr) >> y
670 }
671
672 case token.LSS:
673 switch x.(type) {
674 case int:
675 return x.(int) < y.(int)
676 case int8:
677 return x.(int8) < y.(int8)
678 case int16:
679 return x.(int16) < y.(int16)
680 case int32:
681 return x.(int32) < y.(int32)
682 case int64:
683 return x.(int64) < y.(int64)
684 case uint:
685 return x.(uint) < y.(uint)
686 case uint8:
687 return x.(uint8) < y.(uint8)
688 case uint16:
689 return x.(uint16) < y.(uint16)
690 case uint32:
691 return x.(uint32) < y.(uint32)
692 case uint64:
693 return x.(uint64) < y.(uint64)
694 case uintptr:
695 return x.(uintptr) < y.(uintptr)
696 case float32:
697 return x.(float32) < y.(float32)
698 case float64:
699 return x.(float64) < y.(float64)
700 case string:
701 return x.(string) < y.(string)
702 }
703
704 case token.LEQ:
705 switch x.(type) {
706 case int:
707 return x.(int) <= y.(int)
708 case int8:
709 return x.(int8) <= y.(int8)
710 case int16:
711 return x.(int16) <= y.(int16)
712 case int32:
713 return x.(int32) <= y.(int32)
714 case int64:
715 return x.(int64) <= y.(int64)
716 case uint:
717 return x.(uint) <= y.(uint)
718 case uint8:
719 return x.(uint8) <= y.(uint8)
720 case uint16:
721 return x.(uint16) <= y.(uint16)
722 case uint32:
723 return x.(uint32) <= y.(uint32)
724 case uint64:
725 return x.(uint64) <= y.(uint64)
726 case uintptr:
727 return x.(uintptr) <= y.(uintptr)
728 case float32:
729 return x.(float32) <= y.(float32)
730 case float64:
731 return x.(float64) <= y.(float64)
732 case string:
733 return x.(string) <= y.(string)
734 }
735
736 case token.EQL:
737 return eqnil(t, x, y)
738
739 case token.NEQ:
740 return !eqnil(t, x, y)
741
742 case token.GTR:
743 switch x.(type) {
744 case int:
745 return x.(int) > y.(int)
746 case int8:
747 return x.(int8) > y.(int8)
748 case int16:
749 return x.(int16) > y.(int16)
750 case int32:
751 return x.(int32) > y.(int32)
752 case int64:
753 return x.(int64) > y.(int64)
754 case uint:
755 return x.(uint) > y.(uint)
756 case uint8:
757 return x.(uint8) > y.(uint8)
758 case uint16:
759 return x.(uint16) > y.(uint16)
760 case uint32:
761 return x.(uint32) > y.(uint32)
762 case uint64:
763 return x.(uint64) > y.(uint64)
764 case uintptr:
765 return x.(uintptr) > y.(uintptr)
766 case float32:
767 return x.(float32) > y.(float32)
768 case float64:
769 return x.(float64) > y.(float64)
770 case string:
771 return x.(string) > y.(string)
772 }
773
774 case token.GEQ:
775 switch x.(type) {
776 case int:
777 return x.(int) >= y.(int)
778 case int8:
779 return x.(int8) >= y.(int8)
780 case int16:
781 return x.(int16) >= y.(int16)
782 case int32:
783 return x.(int32) >= y.(int32)
784 case int64:
785 return x.(int64) >= y.(int64)
786 case uint:
787 return x.(uint) >= y.(uint)
788 case uint8:
789 return x.(uint8) >= y.(uint8)
790 case uint16:
791 return x.(uint16) >= y.(uint16)
792 case uint32:
793 return x.(uint32) >= y.(uint32)
794 case uint64:
795 return x.(uint64) >= y.(uint64)
796 case uintptr:
797 return x.(uintptr) >= y.(uintptr)
798 case float32:
799 return x.(float32) >= y.(float32)
800 case float64:
801 return x.(float64) >= y.(float64)
802 case string:
803 return x.(string) >= y.(string)
804 }
805 }
806 panic(fmt.Sprintf("invalid binary op: %T %s %T", x, op, y))
807 }
808
809 // eqnil returns the comparison x == y using the equivalence relation
810 // appropriate for type t.
811 // If t is a reference type, at most one of x or y may be a nil value
812 // of that type.
813 func eqnil(t types.Type, x, y value) bool {
814 switch t.Underlying().(type) {
815 case *types.Map, *types.Signature, *types.Slice:
816 // Since these types don't support comparison,
817 // one of the operands must be a literal nil.
818 switch x := x.(type) {
819 case *hashmap:
820 return (x != nil) == (y.(*hashmap) != nil)
821 case map[value]value:
822 return (x != nil) == (y.(map[value]value) != nil)
823 case *ssa.Function:
824 switch y := y.(type) {
825 case *ssa.Function:
826 return (x != nil) == (y != nil)
827 case *closure:
828 return true
829 }
830 case *closure:
831 return (x != nil) == (y.(*ssa.Function) != nil)
832 case []value:
833 return (x != nil) == (y.([]value) != nil)
834 }
835 panic(fmt.Sprintf("eqnil(%s): illegal dynamic type: %T", t, x))
836 }
837
838 return equals(t, x, y)
839 }
840
841 func unop(instr *ssa.UnOp, x value) value {
842 switch instr.Op {
843 case token.ARROW: // receive
844 v, ok := <-x.(chan value)
845 if !ok {
846 v = zero(instr.X.Type().Underlying().(*types.Chan).Elem())
847 }
848 if instr.CommaOk {
849 v = tuple{v, ok}
850 }
851 return v
852 case token.SUB:
853 switch x := x.(type) {
854 case int:
855 return -x
856 case int8:
857 return -x
858 case int16:
859 return -x
860 case int32:
861 return -x
862 case int64:
863 return -x
864 case uint:
865 return -x
866 case uint8:
867 return -x
868 case uint16:
869 return -x
870 case uint32:
871 return -x
872 case uint64:
873 return -x
874 case uintptr:
875 return -x
876 case float32:
877 return -x
878 case float64:
879 return -x
880 case complex64:
881 return -x
882 case complex128:
883 return -x
884 }
885 case token.MUL:
886 return load(typeparams.MustDeref(instr.X.Type()), x.(*value))
887 case token.NOT:
888 return !x.(bool)
889 case token.XOR:
890 switch x := x.(type) {
891 case int:
892 return ^x
893 case int8:
894 return ^x
895 case int16:
896 return ^x
897 case int32:
898 return ^x
899 case int64:
900 return ^x
901 case uint:
902 return ^x
903 case uint8:
904 return ^x
905 case uint16:
906 return ^x
907 case uint32:
908 return ^x
909 case uint64:
910 return ^x
911 case uintptr:
912 return ^x
913 }
914 }
915 panic(fmt.Sprintf("invalid unary op %s %T", instr.Op, x))
916 }
917
918 // typeAssert checks whether dynamic type of itf is instr.AssertedType.
919 // It returns the extracted value on success, and panics on failure,
920 // unless instr.CommaOk, in which case it always returns a "value,ok" tuple.
921 func typeAssert(instr *ssa.TypeAssert, itf iface) value {
922 var v value
923 err := ""
924 if itf.t == nil {
925 err = fmt.Sprintf("interface conversion: interface is nil, not %s", instr.AssertedType)
926
927 } else if idst, ok := instr.AssertedType.Underlying().(*types.Interface); ok {
928 v = itf
929 err = checkInterface(idst, itf)
930
931 } else if types.Identical(itf.t, instr.AssertedType) {
932 v = itf.v // extract value
933
934 } else {
935 err = fmt.Sprintf("interface conversion: interface is %s, not %s", itf.t, instr.AssertedType)
936 }
937 // Note: if instr.Underlying==true ever becomes reachable from interp check that
938 // types.Identical(itf.t.Underlying(), instr.AssertedType)
939
940 if err != "" {
941 if !instr.CommaOk {
942 panic(err)
943 }
944 return tuple{zero(instr.AssertedType), false}
945 }
946 if instr.CommaOk {
947 return tuple{v, true}
948 }
949 return v
950 }
951
952 // This variable is no longer used but remains to prevent build breakage.
953 var CapturedOutput *bytes.Buffer
954
955 // callBuiltin interprets a call to builtin fn with arguments args,
956 // returning its result.
957 func callBuiltin(caller *frame, fn *ssa.Builtin, args []value) value {
958 switch fn.Name() {
959 case "append":
960 if len(args) == 1 {
961 return args[0]
962 }
963 if s, ok := args[1].(string); ok {
964 // append([]byte, ...string) []byte
965 arg0 := args[0].([]value)
966 for i := 0; i < len(s); i++ {
967 arg0 = append(arg0, s[i])
968 }
969 return arg0
970 }
971 // append([]T, ...[]T) []T
972 return append(args[0].([]value), args[1].([]value)...)
973
974 case "copy": // copy([]T, []T) int or copy([]byte, string) int
975 src := args[1]
976 if _, ok := src.(string); ok {
977 params := fn.Type().(*types.Signature).Params()
978 src = conv(params.At(0).Type(), params.At(1).Type(), src)
979 }
980 return copy(args[0].([]value), src.([]value))
981
982 case "close": // close(chan T)
983 close(args[0].(chan value))
984 return nil
985
986 case "delete": // delete(map[K]value, K)
987 switch m := args[0].(type) {
988 case map[value]value:
989 delete(m, args[1])
990 case *hashmap:
991 m.delete(args[1].(hashable))
992 default:
993 panic(fmt.Sprintf("illegal map type: %T", m))
994 }
995 return nil
996
997 case "print", "println": // print(any, ...)
998 ln := fn.Name() == "println"
999 var buf bytes.Buffer
1000 for i, arg := range args {
1001 if i > 0 && ln {
1002 buf.WriteRune(' ')
1003 }
1004 buf.WriteString(toString(arg))
1005 }
1006 if ln {
1007 buf.WriteRune('\n')
1008 }
1009 os.Stderr.Write(buf.Bytes())
1010 return nil
1011
1012 case "len":
1013 switch x := args[0].(type) {
1014 case string:
1015 return len(x)
1016 case array:
1017 return len(x)
1018 case *value:
1019 return len((*x).(array))
1020 case []value:
1021 return len(x)
1022 case map[value]value:
1023 return len(x)
1024 case *hashmap:
1025 return x.len()
1026 case chan value:
1027 return len(x)
1028 default:
1029 panic(fmt.Sprintf("len: illegal operand: %T", x))
1030 }
1031
1032 case "cap":
1033 switch x := args[0].(type) {
1034 case array:
1035 return cap(x)
1036 case *value:
1037 return cap((*x).(array))
1038 case []value:
1039 return cap(x)
1040 case chan value:
1041 return cap(x)
1042 default:
1043 panic(fmt.Sprintf("cap: illegal operand: %T", x))
1044 }
1045
1046 case "min":
1047 return foldLeft(min, args)
1048 case "max":
1049 return foldLeft(max, args)
1050
1051 case "real":
1052 switch c := args[0].(type) {
1053 case complex64:
1054 return real(c)
1055 case complex128:
1056 return real(c)
1057 default:
1058 panic(fmt.Sprintf("real: illegal operand: %T", c))
1059 }
1060
1061 case "imag":
1062 switch c := args[0].(type) {
1063 case complex64:
1064 return imag(c)
1065 case complex128:
1066 return imag(c)
1067 default:
1068 panic(fmt.Sprintf("imag: illegal operand: %T", c))
1069 }
1070
1071 case "complex":
1072 switch f := args[0].(type) {
1073 case float32:
1074 return complex(f, args[1].(float32))
1075 case float64:
1076 return complex(f, args[1].(float64))
1077 default:
1078 panic(fmt.Sprintf("complex: illegal operand: %T", f))
1079 }
1080
1081 case "panic":
1082 // ssa.Panic handles most cases; this is only for "go
1083 // panic" or "defer panic".
1084 panic(targetPanic{args[0]})
1085
1086 case "recover":
1087 return doRecover(caller)
1088
1089 case "ssa:wrapnilchk":
1090 recv := args[0]
1091 if recv.(*value) == nil {
1092 recvType := args[1]
1093 methodName := args[2]
1094 panic(fmt.Sprintf("value method (%s).%s called using nil *%s pointer",
1095 recvType, methodName, recvType))
1096 }
1097 return recv
1098
1099 case "ssa:deferstack":
1100 return &caller.defers
1101 }
1102
1103 panic("unknown built-in: " + fn.Name())
1104 }
1105
1106 func rangeIter(x value) iter {
1107 switch x := x.(type) {
1108 case map[value]value:
1109 return &mapIter{iter: reflect.ValueOf(x).MapRange()}
1110 case *hashmap:
1111 return &hashmapIter{iter: reflect.ValueOf(x.entries()).MapRange()}
1112 case string:
1113 return &stringIter{Reader: strings.NewReader(x)}
1114 }
1115 panic(fmt.Sprintf("cannot range over %T", x))
1116 }
1117
1118 // widen widens a basic typed value x to the widest type of its
1119 // category, one of:
1120 //
1121 // bool, int64, uint64, float64, complex128, string.
1122 //
1123 // This is inefficient but reduces the size of the cross-product of
1124 // cases we have to consider.
1125 func widen(x value) value {
1126 switch y := x.(type) {
1127 case bool, int64, uint64, float64, complex128, string, unsafe.Pointer:
1128 return x
1129 case int:
1130 return int64(y)
1131 case int8:
1132 return int64(y)
1133 case int16:
1134 return int64(y)
1135 case int32:
1136 return int64(y)
1137 case uint:
1138 return uint64(y)
1139 case uint8:
1140 return uint64(y)
1141 case uint16:
1142 return uint64(y)
1143 case uint32:
1144 return uint64(y)
1145 case uintptr:
1146 return uint64(y)
1147 case float32:
1148 return float64(y)
1149 case complex64:
1150 return complex128(y)
1151 }
1152 panic(fmt.Sprintf("cannot widen %T", x))
1153 }
1154
1155 // conv converts the value x of type t_src to type t_dst and returns
1156 // the result.
1157 // Possible cases are described with the ssa.Convert operator.
1158 func conv(t_dst, t_src types.Type, x value) value {
1159 ut_src := t_src.Underlying()
1160 ut_dst := t_dst.Underlying()
1161
1162 // Destination type is not an "untyped" type.
1163 if b, ok := ut_dst.(*types.Basic); ok && b.Info()&types.IsUntyped != 0 {
1164 panic("oops: conversion to 'untyped' type: " + b.String())
1165 }
1166
1167 // Nor is it an interface type.
1168 if _, ok := ut_dst.(*types.Interface); ok {
1169 if _, ok := ut_src.(*types.Interface); ok {
1170 panic("oops: Convert should be ChangeInterface")
1171 } else {
1172 panic("oops: Convert should be MakeInterface")
1173 }
1174 }
1175
1176 // Remaining conversions:
1177 // + untyped string/number/bool constant to a specific
1178 // representation.
1179 // + conversions between non-complex numeric types.
1180 // + conversions between complex numeric types.
1181 // + integer/[]byte/[]rune -> string.
1182 // + string -> []byte/[]rune.
1183 //
1184 // All are treated the same: first we extract the value to the
1185 // widest representation (int64, uint64, float64, complex128,
1186 // or string), then we convert it to the desired type.
1187
1188 switch ut_src := ut_src.(type) {
1189 case *types.Pointer:
1190 switch ut_dst := ut_dst.(type) {
1191 case *types.Basic:
1192 // *value to unsafe.Pointer?
1193 if ut_dst.Kind() == types.UnsafePointer {
1194 return unsafe.Pointer(x.(*value))
1195 }
1196 }
1197
1198 case *types.Slice:
1199 // []byte or []rune -> string
1200 switch ut_src.Elem().Underlying().(*types.Basic).Kind() {
1201 case types.Byte:
1202 x := x.([]value)
1203 b := make([]byte, 0, len(x))
1204 for i := range x {
1205 b = append(b, x[i].(byte))
1206 }
1207 return string(b)
1208
1209 case types.Rune:
1210 x := x.([]value)
1211 r := make([]rune, 0, len(x))
1212 for i := range x {
1213 r = append(r, x[i].(rune))
1214 }
1215 return string(r)
1216 }
1217
1218 case *types.Basic:
1219 x = widen(x)
1220
1221 // integer -> string?
1222 if ut_src.Info()&types.IsInteger != 0 {
1223 if ut_dst, ok := ut_dst.(*types.Basic); ok && ut_dst.Kind() == types.String {
1224 return fmt.Sprintf("%c", x)
1225 }
1226 }
1227
1228 // string -> []rune, []byte or string?
1229 if s, ok := x.(string); ok {
1230 switch ut_dst := ut_dst.(type) {
1231 case *types.Slice:
1232 var res []value
1233 switch ut_dst.Elem().Underlying().(*types.Basic).Kind() {
1234 case types.Rune:
1235 for _, r := range []rune(s) {
1236 res = append(res, r)
1237 }
1238 return res
1239 case types.Byte:
1240 for _, b := range []byte(s) {
1241 res = append(res, b)
1242 }
1243 return res
1244 }
1245 case *types.Basic:
1246 if ut_dst.Kind() == types.String {
1247 return x.(string)
1248 }
1249 }
1250 break // fail: no other conversions for string
1251 }
1252
1253 // unsafe.Pointer -> *value
1254 if ut_src.Kind() == types.UnsafePointer {
1255 // TODO(adonovan): this is wrong and cannot
1256 // really be fixed with the current design.
1257 //
1258 // return (*value)(x.(unsafe.Pointer))
1259 // creates a new pointer of a different
1260 // type but the underlying interface value
1261 // knows its "true" type and so cannot be
1262 // meaningfully used through the new pointer.
1263 //
1264 // To make this work, the interpreter needs to
1265 // simulate the memory layout of a real
1266 // compiled implementation.
1267 //
1268 // To at least preserve type-safety, we'll
1269 // just return the zero value of the
1270 // destination type.
1271 return zero(t_dst)
1272 }
1273
1274 // Conversions between complex numeric types?
1275 if ut_src.Info()&types.IsComplex != 0 {
1276 switch ut_dst.(*types.Basic).Kind() {
1277 case types.Complex64:
1278 return complex64(x.(complex128))
1279 case types.Complex128:
1280 return x.(complex128)
1281 }
1282 break // fail: no other conversions for complex
1283 }
1284
1285 // Conversions between non-complex numeric types?
1286 if ut_src.Info()&types.IsNumeric != 0 {
1287 kind := ut_dst.(*types.Basic).Kind()
1288 switch x := x.(type) {
1289 case int64: // signed integer -> numeric?
1290 switch kind {
1291 case types.Int:
1292 return int(x)
1293 case types.Int8:
1294 return int8(x)
1295 case types.Int16:
1296 return int16(x)
1297 case types.Int32:
1298 return int32(x)
1299 case types.Int64:
1300 return int64(x)
1301 case types.Uint:
1302 return uint(x)
1303 case types.Uint8:
1304 return uint8(x)
1305 case types.Uint16:
1306 return uint16(x)
1307 case types.Uint32:
1308 return uint32(x)
1309 case types.Uint64:
1310 return uint64(x)
1311 case types.Uintptr:
1312 return uintptr(x)
1313 case types.Float32:
1314 return float32(x)
1315 case types.Float64:
1316 return float64(x)
1317 }
1318
1319 case uint64: // unsigned integer -> numeric?
1320 switch kind {
1321 case types.Int:
1322 return int(x)
1323 case types.Int8:
1324 return int8(x)
1325 case types.Int16:
1326 return int16(x)
1327 case types.Int32:
1328 return int32(x)
1329 case types.Int64:
1330 return int64(x)
1331 case types.Uint:
1332 return uint(x)
1333 case types.Uint8:
1334 return uint8(x)
1335 case types.Uint16:
1336 return uint16(x)
1337 case types.Uint32:
1338 return uint32(x)
1339 case types.Uint64:
1340 return uint64(x)
1341 case types.Uintptr:
1342 return uintptr(x)
1343 case types.Float32:
1344 return float32(x)
1345 case types.Float64:
1346 return float64(x)
1347 }
1348
1349 case float64: // floating point -> numeric?
1350 switch kind {
1351 case types.Int:
1352 return int(x)
1353 case types.Int8:
1354 return int8(x)
1355 case types.Int16:
1356 return int16(x)
1357 case types.Int32:
1358 return int32(x)
1359 case types.Int64:
1360 return int64(x)
1361 case types.Uint:
1362 return uint(x)
1363 case types.Uint8:
1364 return uint8(x)
1365 case types.Uint16:
1366 return uint16(x)
1367 case types.Uint32:
1368 return uint32(x)
1369 case types.Uint64:
1370 return uint64(x)
1371 case types.Uintptr:
1372 return uintptr(x)
1373 case types.Float32:
1374 return float32(x)
1375 case types.Float64:
1376 return float64(x)
1377 }
1378 }
1379 }
1380 }
1381
1382 panic(fmt.Sprintf("unsupported conversion: %s -> %s, dynamic type %T", t_src, t_dst, x))
1383 }
1384
1385 // sliceToArrayPointer converts the value x of type slice to type t_dst
1386 // a pointer to array and returns the result.
1387 func sliceToArrayPointer(t_dst, t_src types.Type, x value) value {
1388 if _, ok := t_src.Underlying().(*types.Slice); ok {
1389 if ptr, ok := t_dst.Underlying().(*types.Pointer); ok {
1390 if arr, ok := ptr.Elem().Underlying().(*types.Array); ok {
1391 x := x.([]value)
1392 if arr.Len() > int64(len(x)) {
1393 panic("array length is greater than slice length")
1394 }
1395 if x == nil {
1396 return zero(t_dst)
1397 }
1398 v := value(array(x[:arr.Len()]))
1399 return &v
1400 }
1401 }
1402 }
1403
1404 panic(fmt.Sprintf("unsupported conversion: %s -> %s, dynamic type %T", t_src, t_dst, x))
1405 }
1406
1407 // checkInterface checks that the method set of x implements the
1408 // interface itype.
1409 // On success it returns "", on failure, an error message.
1410 func checkInterface(itype *types.Interface, x iface) string {
1411 if meth, _ := types.MissingMethod(x.t, itype, true); meth != nil {
1412 return fmt.Sprintf("interface conversion: %v is not %v: missing method %s",
1413 x.t, itype, meth.Name())
1414 }
1415 return "" // ok
1416 }
1417
1418 func foldLeft(op func(value, value) value, args []value) value {
1419 x := args[0]
1420 for _, arg := range args[1:] {
1421 x = op(x, arg)
1422 }
1423 return x
1424 }
1425
1426 func min(x, y value) value {
1427 switch x := x.(type) {
1428 case float32:
1429 return fmin(x, y.(float32))
1430 case float64:
1431 return fmin(x, y.(float64))
1432 }
1433
1434 // return (y < x) ? y : x
1435 if binop(token.LSS, nil, y, x).(bool) {
1436 return y
1437 }
1438 return x
1439 }
1440
1441 func max(x, y value) value {
1442 switch x := x.(type) {
1443 case float32:
1444 return fmax(x, y.(float32))
1445 case float64:
1446 return fmax(x, y.(float64))
1447 }
1448
1449 // return (y > x) ? y : x
1450 if binop(token.GTR, nil, y, x).(bool) {
1451 return y
1452 }
1453 return x
1454 }
1455
1456 // copied from $GOROOT/src/runtime/minmax.go
1457
1458 type floaty interface{ ~float32 | ~float64 }
1459
1460 func fmin[F floaty](x, y F) F {
1461 if y != y || y < x {
1462 return y
1463 }
1464 if x != x || x < y || x != 0 {
1465 return x
1466 }
1467 // x and y are both ±0
1468 // if either is -0, return -0; else return +0
1469 return forbits(x, y)
1470 }
1471
1472 func fmax[F floaty](x, y F) F {
1473 if y != y || y > x {
1474 return y
1475 }
1476 if x != x || x > y || x != 0 {
1477 return x
1478 }
1479 // x and y are both ±0
1480 // if both are -0, return -0; else return +0
1481 return fandbits(x, y)
1482 }
1483
1484 func forbits[F floaty](x, y F) F {
1485 switch unsafe.Sizeof(x) {
1486 case 4:
1487 *(*uint32)(unsafe.Pointer(&x)) |= *(*uint32)(unsafe.Pointer(&y))
1488 case 8:
1489 *(*uint64)(unsafe.Pointer(&x)) |= *(*uint64)(unsafe.Pointer(&y))
1490 }
1491 return x
1492 }
1493
1494 func fandbits[F floaty](x, y F) F {
1495 switch unsafe.Sizeof(x) {
1496 case 4:
1497 *(*uint32)(unsafe.Pointer(&x)) &= *(*uint32)(unsafe.Pointer(&y))
1498 case 8:
1499 *(*uint64)(unsafe.Pointer(&x)) &= *(*uint64)(unsafe.Pointer(&y))
1500 }
1501 return x
1502 }