-
Notifications
You must be signed in to change notification settings - Fork 14
/
interp.go
1475 lines (1367 loc) · 36.4 KB
/
interp.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 2013 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 igop defines an interpreter for the SSA
// representation of Go programs.
//
// This interpreter is provided as an adjunct for testing the SSA
// construction algorithm. Its purpose is to provide a minimal
// metacircular implementation of the dynamic semantics of each SSA
// instruction. It is not, and will never be, a production-quality Go
// interpreter.
//
// The following is a partial list of Go features that are currently
// unsupported or incomplete in the interpreter.
//
// * Unsafe operations, including all uses of unsafe.Pointer, are
// impossible to support given the "boxed" value representation we
// have chosen.
//
// * The reflect package is only partially implemented.
//
// * The "testing" package is no longer supported because it
// depends on low-level details that change too often.
//
// * "sync/atomic" operations are not atomic due to the "boxed" value
// representation: it is not possible to read, modify and write an
// interface value atomically. As a consequence, Mutexes are currently
// broken.
//
// * recover is only partially implemented. Also, the interpreter
// makes no attempt to distinguish target panics from interpreter
// crashes.
//
// * the sizes of the int, uint and uintptr types in the target
// program are assumed to be the same as those of the interpreter
// itself.
//
// * all values occupy space, even those of types defined by the spec
// to have zero size, e.g. struct{}. This can cause asymptotic
// performance degradation.
//
// * os.Exit is implemented using panic, causing deferred functions to
// run.
package igop
import (
"fmt"
"go/ast"
"go/constant"
"go/token"
"go/types"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"
"unsafe"
"github.com/goplus/igop/load"
"github.com/goplus/reflectx"
"github.com/visualfc/gid"
"github.com/visualfc/xtype"
"golang.org/x/tools/go/ssa"
)
var (
maxMemLen int
)
const intSize = 32 << (^uint(0) >> 63)
func init() {
if intSize == 32 {
maxMemLen = 1<<31 - 1
} else {
v := int64(1) << 59
maxMemLen = int(v)
}
}
type Interp struct {
ctx *Context
mainpkg *ssa.Package // the SSA main package
record *TypesRecord // lookup type and ToType
globals map[string]value // addresses of global variables (immutable)
chkinit map[string]bool // init vars
preloadTypes map[types.Type]reflect.Type // preload types.Type -> reflect.Type
funcs map[*ssa.Function]*function // ssa.Function -> *function
msets map[reflect.Type](map[string]*ssa.Function) // user defined type method sets
chexit chan int // call os.Exit code by chan for runtime.Goexit
cherror chan PanicError // call by go func error for context
deferMap sync.Map // defer goroutine id -> call frame
rfuncMap sync.Map // reflect.Value(fn).Pointer -> *function
typesMutex sync.RWMutex // findType/toType mutex
mainid int64 // main goroutine id
exitCode int // call os.Exit code
goroutines int32 // atomically updated
deferCount int32 // fast has defer check
goexited int32 // is call runtime.Goexit
exited int32 // is call os.Exit
}
func (i *Interp) MainPkg() *ssa.Package {
return i.mainpkg
}
func (i *Interp) installed(path string) (pkg *Package, ok bool) {
pkg, ok = i.ctx.Loader.Installed(path)
return
}
func (i *Interp) loadFunction(fn *ssa.Function) *function {
if pfn, ok := i.funcs[fn]; ok {
return pfn
}
pfn := &function{
Interp: i,
Fn: fn,
index: make(map[ssa.Value]uint32),
instrIndex: make(map[ssa.Instruction][]uint32),
narg: len(fn.Params),
nenv: len(fn.FreeVars),
}
if len(fn.Blocks) > 0 {
pfn.Main = fn.Blocks[0]
}
if res := fn.Signature.Results(); res != nil {
pfn.nres = res.Len()
pfn.stack = make([]value, pfn.nres)
}
i.funcs[fn] = pfn
return pfn
}
func (i *Interp) findType(rt reflect.Type, local bool) (types.Type, bool) {
i.typesMutex.Lock()
defer i.typesMutex.Unlock()
if local {
return i.record.LookupLocalTypes(rt)
} else {
return i.record.LookupTypes(rt)
}
}
func (i *Interp) tryDeferFrame() *frame {
if i != nil && atomic.LoadInt32(&i.deferCount) != 0 {
if f, ok := i.deferMap.Load(goroutineID()); ok {
return f.(*frame)
}
}
return &frame{}
}
func (pfn *function) callFunctionByReflect(mtyp reflect.Type, args []reflect.Value, env []interface{}) []reflect.Value {
return pfn.Interp.callFunctionByReflect(pfn.Interp.tryDeferFrame(), mtyp, pfn, args, env)
}
func (i *Interp) FindMethod(mtyp reflect.Type, fn *types.Func) func([]reflect.Value) []reflect.Value {
typ := fn.Type().(*types.Signature).Recv().Type()
if f := i.mainpkg.Prog.LookupMethod(typ, fn.Pkg(), fn.Name()); f != nil {
pfn := i.loadFunction(f)
return func(args []reflect.Value) []reflect.Value {
return pfn.callFunctionByReflect(mtyp, args, nil)
}
}
name := fn.FullName()
if v, ok := externValues[name]; ok && v.Kind() == reflect.Func {
if v.Type().IsVariadic() {
return func(args []reflect.Value) []reflect.Value {
return v.CallSlice(args)
}
}
return func(args []reflect.Value) []reflect.Value {
return v.Call(args)
}
}
panic(fmt.Sprintf("Not found method %v", fn))
}
func (pfn *function) makeFunction(typ reflect.Type, env []value) reflect.Value {
return reflect.MakeFunc(typ, func(args []reflect.Value) []reflect.Value {
return pfn.Interp.callFunctionByReflect(pfn.Interp.tryDeferFrame(), typ, pfn, args, env)
})
}
type _defer struct {
fn value
tail *_defer
args []value
ssaArgs []ssa.Value
}
type frame struct {
interp *Interp
caller *frame
callee *frame
pfn *function
_defer *_defer
_panic *_panic
block *ssa.BasicBlock
stack []value // result args env datas
ipc int
pred int
deferid int64
}
func dumpBlock(block *ssa.BasicBlock, level int, pc ssa.Instruction) {
if level == 0 {
fmt.Printf("--- %v ---\n", block.Parent())
}
fmt.Printf("%v.%v %v Jump:%v Idom:%v\n", strings.Repeat(" ", level), block.Index, block.Comment, block.Succs, block.Idom())
for _, instr := range block.Instrs {
var head string
if instr == pc {
head = " " + strings.Repeat(" ", level) + "=>"
} else {
head = " " + strings.Repeat(" ", level)
}
if value, ok := instr.(ssa.Value); ok {
fmt.Printf("%v %-20T %-4v = %v %v\n", head, instr, value.Name(), instr, value.Type())
} else {
fmt.Printf("%v %-20T %v\n", head, instr, instr)
}
}
for _, v := range block.Dominees() {
dumpBlock(v, level+1, pc)
}
}
type tasks struct {
jumps map[*ssa.BasicBlock]bool
}
func checkJumps(block *ssa.BasicBlock, jumps map[*ssa.BasicBlock]bool, succs map[*ssa.BasicBlock]*ssa.BasicBlock) {
if s, ok := succs[block]; ok {
if jumps[s] {
return
}
jumps[s] = true
checkJumps(s, jumps, succs)
return
}
for _, s := range block.Succs {
if jumps[s] {
continue
}
jumps[s] = true
checkJumps(s, jumps, succs)
}
}
func checkRuns(block *ssa.BasicBlock, jumps map[*ssa.BasicBlock]bool, succs map[*ssa.BasicBlock]*ssa.BasicBlock) {
if jumps[block] {
return
}
jumps[block] = true
if s, ok := succs[block]; ok {
checkRuns(s, jumps, succs)
return
}
for _, s := range block.Dominees() {
checkRuns(s, jumps, succs)
}
}
func (fr *frame) gc() {
alloc := make(map[int]bool)
checkAlloc := func(instr ssa.Instruction) {
for _, v := range fr.pfn.instrIndex[instr] {
vk := kind(v >> 30)
if vk.isStatic() {
continue
}
rk := reflect.Kind(v >> 24 & 0x3f)
switch rk {
case reflect.String, reflect.Func, reflect.Ptr, reflect.Array, reflect.Slice, reflect.Map, reflect.Struct, reflect.Interface:
default:
continue
}
alloc[int(v&0xffffff)] = true
}
}
// check params and freevar
checkAlloc(nil)
// check alloc
cur := fr.pfn.ssaInstrs[fr.ipc-1]
var remain int
for i, instr := range fr.block.Instrs {
checkAlloc(instr)
if cur == instr {
remain = i
break
}
}
// check
seen := make(map[*ssa.BasicBlock]bool)
var checkChild func(block *ssa.BasicBlock)
checkChild = func(block *ssa.BasicBlock) {
for _, child := range block.Dominees() {
if seen[child] {
continue
}
seen[child] = true
checkChild(child)
}
}
// check block child
checkChild(fr.block)
block := fr.block
// check seen
for block != nil {
idom := block.Idom()
if idom == nil {
break
}
var find bool
switch idom.Comment {
case "for.loop":
find = true
case "rangeindex.loop":
find = true
case "rangechan.loop":
case "rangeiter.loop":
}
for _, v := range idom.Succs {
if find {
seen[v] = true
checkChild(v)
}
if block == v {
find = true
}
}
for _, instr := range idom.Instrs {
checkAlloc(instr)
}
block = idom
}
if fr.block.Comment == "for.done" {
delete(seen, fr.block)
}
// check used in block
var ops []*ssa.Value
for _, instr := range fr.block.Instrs[remain+1:] {
for _, op := range instr.Operands(ops[:0]) {
if *op == nil {
continue
}
reg := fr.pfn.regIndex(*op)
alloc[int(reg)] = false
}
}
// check unused in seen
for block := range seen {
var ops []*ssa.Value
for _, instr := range block.Instrs {
for _, op := range instr.Operands(ops[:0]) {
if *op == nil {
continue
}
reg := fr.pfn.regIndex(*op)
alloc[int(reg)] = false
}
}
}
// remove unused
for i, b := range alloc {
if !b {
continue
}
fr.stack[i] = nil
}
}
func (fr *frame) valid() bool {
return fr != nil && fr.pfn != nil && fr.block != nil
}
func (fr *frame) pc() uintptr {
return uintptr(fr.pfn.base + fr.ipc)
}
func (fr *frame) aborted() bool {
return fr != nil && fr.ipc != -1
}
func (fr *frame) setReg(ir register, v value) {
fr.stack[ir] = v
}
func (fr *frame) reg(ir register) value {
return fr.stack[ir]
}
func (fr *frame) bytes(ir register) []byte {
return xtype.Bytes(fr.stack[ir])
}
func (fr *frame) runes(ir register) []rune {
return xtype.Runes(fr.stack[ir])
}
func (fr *frame) bool(ir register) bool {
return xtype.Bool(fr.stack[ir])
}
func (fr *frame) int(ir register) int {
return xtype.Int(fr.stack[ir])
}
func (fr *frame) int8(ir register) int8 {
return xtype.Int8(fr.stack[ir])
}
func (fr *frame) int16(ir register) int16 {
return xtype.Int16(fr.stack[ir])
}
func (fr *frame) int32(ir register) int32 {
return xtype.Int32(fr.stack[ir])
}
func (fr *frame) int64(ir register) int64 {
return xtype.Int64(fr.stack[ir])
}
func (fr *frame) uint(ir register) uint {
return xtype.Uint(fr.stack[ir])
}
func (fr *frame) uint8(ir register) uint8 {
return xtype.Uint8(fr.stack[ir])
}
func (fr *frame) uint16(ir register) uint16 {
return xtype.Uint16(fr.stack[ir])
}
func (fr *frame) uint32(ir register) uint32 {
return xtype.Uint32(fr.stack[ir])
}
func (fr *frame) uint64(ir register) uint64 {
return xtype.Uint64(fr.stack[ir])
}
func (fr *frame) uintptr(ir register) uintptr {
return xtype.Uintptr(fr.stack[ir])
}
func (fr *frame) float32(ir register) float32 {
return xtype.Float32(fr.stack[ir])
}
func (fr *frame) float64(ir register) float64 {
return xtype.Float64(fr.stack[ir])
}
func (fr *frame) complex64(ir register) complex64 {
return xtype.Complex64(fr.stack[ir])
}
func (fr *frame) complex128(ir register) complex128 {
return xtype.Complex128(fr.stack[ir])
}
func (fr *frame) string(ir register) string {
return xtype.String(fr.stack[ir])
}
func (fr *frame) pointer(ir register) unsafe.Pointer {
return xtype.Pointer(fr.stack[ir])
}
func (fr *frame) copyReg(dst register, src register) {
fr.stack[dst] = fr.stack[src]
}
type _panic struct {
arg interface{}
link *_panic
pcs []uintptr
aborted bool
recovered bool
}
func (p *_panic) isNil() bool {
return p == nil || p.recovered
}
// runDefer runs a deferred call d.
// It always returns normally, but may set or clear fr.panic.
func (fr *frame) runDefer(d *_defer) (ok bool) {
defer func() {
if !ok {
// Deferred call created a new state of panic.
if fr._panic != nil {
fr._panic.aborted = true
}
fr._panic = &_panic{arg: recover(), link: fr._panic}
// no tail add callee.pc
if d.tail != nil {
callee := fr.callee
for callee.aborted() {
fr._panic.pcs = append([]uintptr{callee.pc()}, fr._panic.pcs...)
callee = callee.callee
}
}
}
}()
fr.interp.callDiscardsResult(fr, d.fn, d.args, d.ssaArgs)
ok = true
return
}
// runDefers executes fr's deferred function calls in LIFO order.
//
// On entry, fr.panicking indicates a state of panic; if
// true, fr.panic contains the panic value.
//
// On completion, if a deferred call started a panic, or if no
// deferred call recovered from a previous state of panic, then
// runDefers itself panics after the last deferred call has run.
//
// If there was no initial state of panic, or it was recovered from,
// runDefers returns normally.
func (fr *frame) runDefers() {
interp := fr.interp
atomic.AddInt32(&interp.deferCount, 1)
fr.deferid = goroutineID()
interp.deferMap.Store(fr.deferid, fr)
for d := fr._defer; d != nil; d = d.tail {
fr.runDefer(d)
}
interp.deferMap.Delete(fr.deferid)
atomic.AddInt32(&interp.deferCount, -1)
fr.deferid = 0
fr._defer = nil
// runtime.Goexit() fr.panic == nil
if !fr._panic.isNil() {
panic(fr._panic.arg) // new panic, or still panicking
}
}
// lookupMethod returns the method set for type typ, which may be one
// of the interpreter's fake types.
func lookupMethod(i *Interp, typ types.Type, meth *types.Func) *ssa.Function {
return i.mainpkg.Prog.LookupMethod(typ, meth.Pkg(), meth.Name())
}
func SetValue(v reflect.Value, x reflect.Value) {
switch v.Kind() {
case reflect.Bool:
v.SetBool(x.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v.SetInt(x.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v.SetUint(x.Uint())
case reflect.Uintptr:
v.SetUint(x.Uint())
case reflect.Float32, reflect.Float64:
v.SetFloat(x.Float())
case reflect.Complex64, reflect.Complex128:
v.SetComplex(x.Complex())
case reflect.String:
v.SetString(x.String())
case reflect.UnsafePointer:
v.SetPointer(unsafe.Pointer(x.Pointer()))
default:
v.Set(x)
}
}
type DebugInfo struct {
*ssa.DebugRef
fset *token.FileSet
toValue func() (*types.Var, interface{}, bool) // var object value
}
func (i *DebugInfo) Position() token.Position {
return i.fset.Position(i.Pos())
}
func (i *DebugInfo) AsVar() (*types.Var, interface{}, bool) {
return i.toValue()
}
func (i *DebugInfo) AsFunc() (*types.Func, bool) {
v, ok := i.Object().(*types.Func)
return v, ok
}
// prepareCall determines the function value and argument values for a
// function call in a Call, Go or Defer instruction, performing
// interface method lookup if needed.
func (i *Interp) prepareCall(fr *frame, call *ssa.CallCommon, iv register, ia []register, ib []register) (fv value, args []value) {
if call.Method == nil {
switch f := call.Value.(type) {
case *ssa.Builtin:
fv = f
case *ssa.Function:
if f.Blocks == nil {
ext, ok := findExternFunc(i, f)
if !ok {
// skip pkg.init
if f.Pkg != nil && f.Name() == "init" {
fv = func() {}
} else {
panic(fmt.Errorf("no code for function: %v", f))
}
} else {
fv = ext
}
} else {
fv = f
}
case *ssa.MakeClosure:
var bindings []value
for i := range f.Bindings {
bindings = append(bindings, fr.reg(ib[i]))
}
fv = &closure{i.funcs[f.Fn.(*ssa.Function)], bindings}
default:
fv = fr.reg(iv)
}
} else {
v := fr.reg(iv)
rtype := reflect.TypeOf(v)
mname := call.Method.Name()
if mset, ok := i.msets[rtype]; ok {
if f, ok := mset[mname]; ok {
fv = f
} else {
ext, ok := findUserMethod(rtype, mname)
if !ok {
panic(fmt.Errorf("no code for method: %v.%v", rtype, mname))
}
fv = ext
}
} else {
ext, ok := findExternMethod(rtype, mname)
if !ok {
panic(fmt.Errorf("no code for method: %v.%v", rtype, mname))
}
fv = ext
}
args = append(args, v)
}
for i := range call.Args {
v := fr.reg(ia[i])
args = append(args, v)
}
return
}
// call interprets a call to a function (function, builtin or closure)
// fn with arguments args, returning its result.
// callpos is the position of the callsite.
func (i *Interp) call(caller *frame, fn value, args []value, ssaArgs []ssa.Value) value {
switch fn := fn.(type) {
case *ssa.Function:
return i.callFunction(caller, i.funcs[fn], args, nil)
case *closure:
return i.callFunction(caller, fn.pfn, args, fn.env)
case *ssa.Builtin:
return i.callBuiltin(caller, fn, args, ssaArgs)
case reflect.Value:
return i.callExternal(caller, fn, args, nil)
default:
return i.callExternal(caller, reflect.ValueOf(fn), args, nil)
}
}
// call interprets a call to a function (function, builtin or closure)
// fn with arguments args, returning its result.
// callpos is the position of the callsite.
func (i *Interp) callDiscardsResult(caller *frame, fn value, args []value, ssaArgs []ssa.Value) {
switch fn := fn.(type) {
case *ssa.Function:
i.callFunctionDiscardsResult(caller, i.funcs[fn], args, nil)
case *closure:
i.callFunctionDiscardsResult(caller, fn.pfn, args, fn.env)
case *ssa.Builtin:
i.callBuiltinDiscardsResult(caller, fn, args, ssaArgs)
case reflect.Value:
i.callExternalDiscardsResult(caller, fn, args, nil)
default:
i.callExternalDiscardsResult(caller, reflect.ValueOf(fn), args, nil)
}
}
func (i *Interp) callFunction(caller *frame, pfn *function, args []value, env []value) (result value) {
fr := pfn.allocFrame(caller)
for i := 0; i < pfn.narg; i++ {
fr.stack[i+pfn.nres] = args[i]
}
for i := 0; i < pfn.nenv; i++ {
fr.stack[pfn.narg+i+pfn.nres] = env[i]
}
fr.run()
if pfn.nres == 1 {
result = fr.stack[0]
} else if pfn.nres > 1 {
result = tuple(fr.stack[0:pfn.nres])
}
pfn.deleteFrame(caller, fr)
return
}
func (i *Interp) callFunctionByReflect(caller *frame, typ reflect.Type, pfn *function, args []reflect.Value, env []value) (results []reflect.Value) {
fr := pfn.allocFrame(caller)
for i := 0; i < pfn.narg; i++ {
fr.stack[i+pfn.nres] = args[i].Interface()
}
for i := 0; i < pfn.nenv; i++ {
fr.stack[pfn.narg+i+pfn.nres] = env[i]
}
fr.run()
if pfn.nres > 0 {
results = make([]reflect.Value, pfn.nres)
for i := 0; i < pfn.nres; i++ {
v := fr.stack[i]
if v == nil {
results[i] = reflect.New(typ.Out(i)).Elem()
} else {
results[i] = reflect.ValueOf(v)
}
}
}
pfn.deleteFrame(caller, fr)
return
}
func (i *Interp) callFunctionDiscardsResult(caller *frame, pfn *function, args []value, env []value) {
fr := pfn.allocFrame(caller)
for i := 0; i < pfn.narg; i++ {
fr.stack[i+pfn.nres] = args[i]
}
for i := 0; i < pfn.nenv; i++ {
fr.stack[pfn.narg+i+pfn.nres] = env[i]
}
fr.run()
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callFunctionByStack0(caller *frame, pfn *function, ir register, ia []register) {
fr := pfn.allocFrame(caller)
for i := 0; i < len(ia); i++ {
fr.stack[i] = caller.reg(ia[i])
}
fr.run()
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callFunctionByStack1(caller *frame, pfn *function, ir register, ia []register) {
fr := pfn.allocFrame(caller)
for i := 0; i < len(ia); i++ {
fr.stack[i+1] = caller.reg(ia[i])
}
fr.run()
caller.setReg(ir, fr.stack[0])
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callFunctionByStackN(caller *frame, pfn *function, ir register, ia []register) {
fr := pfn.allocFrame(caller)
for i := 0; i < len(ia); i++ {
fr.stack[i+pfn.nres] = caller.reg(ia[i])
}
fr.run()
caller.setReg(ir, tuple(fr.stack[0:pfn.nres]))
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callFunctionByStack(caller *frame, pfn *function, ir register, ia []register) {
fr := pfn.allocFrame(caller)
for i := 0; i < len(ia); i++ {
fr.stack[i+pfn.nres] = caller.reg(ia[i])
}
fr.run()
if pfn.nres == 1 {
caller.setReg(ir, fr.stack[0])
} else if pfn.nres > 1 {
caller.setReg(ir, tuple(fr.stack[0:pfn.nres]))
}
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callFunctionByStackNoRecover0(caller *frame, pfn *function, ir register, ia []register) {
fr := pfn.allocFrame(caller)
for i := 0; i < len(ia); i++ {
fr.stack[i] = caller.reg(ia[i])
}
for fr.ipc != -1 {
fn := fr.pfn.Instrs[fr.ipc]
fr.ipc++
fn(fr)
}
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callFunctionByStackNoRecover1(caller *frame, pfn *function, ir register, ia []register) {
fr := pfn.allocFrame(caller)
for i := 0; i < len(ia); i++ {
fr.stack[i+1] = caller.reg(ia[i])
}
for fr.ipc != -1 {
fn := fr.pfn.Instrs[fr.ipc]
fr.ipc++
fn(fr)
}
caller.setReg(ir, fr.stack[0])
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callFunctionByStackNoRecoverN(caller *frame, pfn *function, ir register, ia []register) {
fr := pfn.allocFrame(caller)
for i := 0; i < len(ia); i++ {
fr.stack[i+pfn.nres] = caller.reg(ia[i])
}
for fr.ipc != -1 {
fn := fr.pfn.Instrs[fr.ipc]
fr.ipc++
fn(fr)
}
caller.setReg(ir, tuple(fr.stack[0:pfn.nres]))
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callFunctionByStackWithEnv(caller *frame, pfn *function, ir register, ia []register, env []value) {
fr := pfn.allocFrame(caller)
for i := 0; i < pfn.narg; i++ {
fr.stack[i+pfn.nres] = caller.reg(ia[i])
}
for i := 0; i < pfn.nenv; i++ {
fr.stack[pfn.narg+i+pfn.nres] = env[i]
}
fr.run()
if pfn.nres == 1 {
caller.setReg(ir, fr.stack[0])
} else if pfn.nres > 1 {
caller.setReg(ir, tuple(fr.stack[0:pfn.nres]))
}
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callFunctionByStackNoRecoverWithEnv(caller *frame, pfn *function, ir register, ia []register, env []value) {
fr := pfn.allocFrame(caller)
for i := 0; i < pfn.narg; i++ {
fr.stack[i+pfn.nres] = caller.reg(ia[i])
}
for i := 0; i < pfn.nenv; i++ {
fr.stack[pfn.narg+i+pfn.nres] = env[i]
}
for fr.ipc != -1 {
fn := fr.pfn.Instrs[fr.ipc]
fr.ipc++
fn(fr)
}
if pfn.nres == 1 {
caller.setReg(ir, fr.stack[0])
} else if pfn.nres > 1 {
caller.setReg(ir, tuple(fr.stack[0:pfn.nres]))
}
pfn.deleteFrame(caller, fr)
}
func (i *Interp) callExternal(caller *frame, fn reflect.Value, args []value, env []value) value {
if caller != nil && caller.deferid != 0 {
i.deferMap.Store(caller.deferid, caller)
}
var ins []reflect.Value
typ := fn.Type()
isVariadic := fn.Type().IsVariadic()
if isVariadic {
for i := 0; i < len(args)-1; i++ {
if args[i] == nil {
ins = append(ins, reflect.New(typ.In(i)).Elem())
} else {
ins = append(ins, reflect.ValueOf(args[i]))
}
}
ins = append(ins, reflect.ValueOf(args[len(args)-1]))
} else {
ins = make([]reflect.Value, len(args))
for i := 0; i < len(args); i++ {
if args[i] == nil {
ins[i] = reflect.New(typ.In(i)).Elem()
} else {
ins[i] = reflect.ValueOf(args[i])
}
}
}
var results []reflect.Value
if isVariadic {
results = fn.CallSlice(ins)
} else {
results = fn.Call(ins)
}
switch len(results) {
case 0:
return nil
case 1:
return results[0].Interface()
default:
var res []value
for _, r := range results {
res = append(res, r.Interface())
}
return tuple(res)
}
}
func (i *Interp) callExternalDiscardsResult(caller *frame, fn reflect.Value, args []value, env []value) {
if caller != nil && caller.deferid != 0 {
i.deferMap.Store(caller.deferid, caller)
}
var ins []reflect.Value
typ := fn.Type()
isVariadic := fn.Type().IsVariadic()
if isVariadic {
for i := 0; i < len(args)-1; i++ {
if args[i] == nil {
ins = append(ins, reflect.New(typ.In(i)).Elem())
} else {
ins = append(ins, reflect.ValueOf(args[i]))
}
}
ins = append(ins, reflect.ValueOf(args[len(args)-1]))
fn.CallSlice(ins)
} else {
ins = make([]reflect.Value, len(args))
for i := 0; i < len(args); i++ {
if args[i] == nil {
ins[i] = reflect.New(typ.In(i)).Elem()
} else {
ins[i] = reflect.ValueOf(args[i])
}
}
fn.Call(ins)
}
}
func (i *Interp) callExternalByStack(caller *frame, fn reflect.Value, ir register, ia []register) {
if caller.deferid != 0 {
i.deferMap.Store(caller.deferid, caller)
}
var ins []reflect.Value
typ := fn.Type()
isVariadic := fn.Type().IsVariadic()
if isVariadic {
var i int
for n := len(ia) - 1; i < n; i++ {
arg := caller.reg(ia[i])
if arg == nil {
ins = append(ins, reflect.New(typ.In(i)).Elem())
} else {
ins = append(ins, reflect.ValueOf(arg))
}
}
ins = append(ins, reflect.ValueOf(caller.reg(ia[i])))
} else {
n := len(ia)
ins = make([]reflect.Value, n)
for i := 0; i < n; i++ {
arg := caller.reg(ia[i])
if arg == nil {
ins[i] = reflect.New(typ.In(i)).Elem()
} else {
ins[i] = reflect.ValueOf(arg)
}
}
}
var results []reflect.Value
if isVariadic {
results = fn.CallSlice(ins)
} else {
results = fn.Call(ins)
}
switch len(results) {
case 0:
case 1:
caller.setReg(ir, results[0].Interface())
default:
var res []value
for _, r := range results {
res = append(res, r.Interface())
}
caller.setReg(ir, tuple(res))
}
}
func (i *Interp) callExternalWithFrameByStack(caller *frame, fn reflect.Value, ir register, ia []register) {
if caller.deferid != 0 {
i.deferMap.Store(caller.deferid, caller)
}
var ins []reflect.Value