forked from gnolang/gno
-
Notifications
You must be signed in to change notification settings - Fork 1
/
interpret.go
1365 lines (1283 loc) · 32.1 KB
/
interpret.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
package gno
import (
"fmt"
"io"
"os"
"reflect"
"strings"
)
//----------------------------------------
// Machine
type Machine struct {
// State
Ops []Op // main operations
NumOps int
Values []TypedValue // buffer of values to be operated on
NumValues int // number of values
Exprs []Expr // pending expressions
Stmts []Stmt // pending statements
Blocks []*Block // block (scope) stack
Frames []Frame // func call stack
Package *PackageValue // active package
Realm *Realm // active realm
Exception *TypedValue // if panic'd unless recovered
// Volatile State
NumResults int // number of results returned
// Configuration
CheckTypes bool
Output io.Writer
Store Store
}
// Machine with new package of given path.
// Creates a new MemRealmer for any new realms.
func NewMachine(pkgPath string, store Store) *Machine {
pkgName := defaultPkgName(pkgPath)
rlm := (*Realm)(nil)
if IsRealmPath(pkgPath) {
rlm = NewRealm(pkgPath)
}
pn := NewPackageNode(pkgName, pkgPath, &FileSet{})
pv := pn.NewPackage(rlm)
return NewMachineWithOptions(
MachineOptions{
Package: pv,
Store: store,
})
}
type MachineOptions struct {
Package *PackageValue
CheckTypes bool
Output io.Writer
Store Store
}
func NewMachineWithOptions(opts MachineOptions) *Machine {
pkg := opts.Package
if pkg == nil {
pn := NewPackageNode("main", ".main", &FileSet{})
pkg = pn.NewPackage(nil) // no realm by default.
}
rlm := pkg.GetRealm()
checkTypes := opts.CheckTypes
output := opts.Output
if output == nil {
output = os.Stdout
}
store := opts.Store
if store == nil {
// bare machine, no stdlibs.
}
blocks := []*Block{
&pkg.Block,
}
return &Machine{
Ops: make([]Op, 1024),
NumOps: 0,
Values: make([]TypedValue, 1024),
NumValues: 0,
Blocks: blocks,
Package: pkg,
Realm: rlm,
CheckTypes: checkTypes,
Output: output,
Store: store,
}
}
//----------------------------------------
// top level Run* methods.
// Add files to the package's *FileSet and run them.
// This will also run each init function encountered.
func (m *Machine) RunFiles(fns ...*FileNode) {
// Files' package names must match the machine's active one.
// if there is one.
for _, fn := range fns {
if fn.PkgName != "" && fn.PkgName != m.Package.PkgName {
panic(fmt.Sprintf("expected package name [%s] but got [%s]",
m.Package.PkgName, fn.PkgName))
}
}
// Add files to *PackageNode.FileSet.
pv := m.Package
pn := pv.Source.(*PackageNode)
if pn.FileSet == nil {
pn.FileSet = &FileSet{}
}
pn.FileSet.AddFiles(fns...)
fileupdates := make([][]TypedValue, len(fns))
// Preprocess each new file.
for i, fn := range fns {
// Preprocess file.
// NOTE: Most of the declaration is handled by
// Preprocess and any constant values set on
// pn.StaticBlock, and those values are copied to the
// runtime package value via PrepareNewValues. Then,
// non-constant var declarations and file-level imports
// are re-set in runDeclaration(,true).
fn = Preprocess(m.Store, pn, fn).(*FileNode)
if debug {
debug.Println("PREPROCESSED FILE: ", fn.String())
}
// Make block for fn.
// Each file for each *PackageValue gets its own file *Block,
// with values copied over from each file's
// *FileNode.StaticBlock.
fb := NewBlock(fn, &pv.Block)
fb.Values = make([]TypedValue, len(fn.StaticBlock.Values))
copy(fb.Values, fn.StaticBlock.Values)
pv.AddFileBlock(fn.Name, fb)
updates := pn.PrepareNewValues(pv) // with fb
fileupdates[i] = updates
}
// exists if declaration run.
var fdeclared = map[Name]struct{}{}
// to detect loops in var declarations.
var loopfindr = []Name{}
// recursive function for var declarations.
var runDeclarationFor func(fn *FileNode, decl Decl)
runDeclarationFor = func(fn *FileNode, decl Decl) {
// get dependencies of decl.
deps := make(map[Name]struct{})
findDependentNames(decl, deps)
for dep, _ := range deps {
// if dep already in fdeclared, skip.
if _, ok := fdeclared[dep]; ok {
continue
}
fn, depdecl := pn.FileSet.GetDeclFor(dep)
// if dep already in loopfindr, abort.
if hasName(dep, loopfindr) {
if _, ok := (*depdecl).(*FuncDecl); ok {
// recursive function dependencies
// are OK with func decls.
continue
} else {
panic(fmt.Sprintf(
"loop in variable initialization: dependency trail %v circularly depends on %s", loopfindr, dep))
}
}
// run dependecy declaration
loopfindr = append(loopfindr, dep)
runDeclarationFor(fn, *depdecl)
loopfindr = loopfindr[:len(loopfindr)-1]
}
// run declaration
fb := pv.GetFileBlock(m.Store, fn.Name)
m.PushBlock(fb)
m.runDeclaration(decl)
m.PopBlock()
for _, n := range decl.GetDeclNames() {
fdeclared[n] = struct{}{}
}
}
// Variable initialization. This must happen after all
// files are preprocessed, because value decl may be
// out of order and depend on other files.
for _, fn := range fns {
// Run declarations.
for _, decl := range fn.Decls {
runDeclarationFor(fn, decl)
}
}
// Run new init functions.
// Go spec: "To ensure reproducible initialization
// behavior, build systems are encouraged to present
// multiple files belonging to the same package in
// lexical file name order to a compiler."
for i, fn := range fns {
updates := fileupdates[i]
fb := pv.GetFileBlock(m.Store, fn.Name)
m.PushBlock(fb)
for i := 0; i < len(updates); i++ {
tv := &updates[i]
if tv.IsDefined() && tv.T.Kind() == FuncKind && tv.V != nil {
if fv, ok := tv.V.(*FuncValue); ok {
fn := fv.Name
if strings.HasPrefix(string(fn), "init.") {
m.RunFunc(fn)
}
}
}
}
m.PopBlock()
}
}
func (m *Machine) RunFunc(fn Name) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Machine.RunFunc(%q) panic: %v\n%s\n",
fn, r, m.String())
panic(r)
}
}()
m.RunStatement(S(Call(Nx(fn))))
}
func (m *Machine) RunMain() {
defer func() {
if r := recover(); r != nil {
fmt.Printf("Machine.RunMain() panic: %v\n%s\n",
r, m.String())
panic(r)
}
}()
m.RunStatement(S(Call(X("main"))))
}
// Evaluate throwaway expression in new block scope.
// If x is a function call, it must return 1 result.
// This function is mainly for debugging and testing,
// but it could also be useful for a repl.
// Input must not have been preprocessed, that is,
// it should not be the child of any parent.
func (m *Machine) Eval(x Expr) TypedValue {
if debug {
m.Printf("Machine.Eval(%v)\n", x)
}
// X must not have been preprocessed.
if x.GetAttribute(ATTR_PREPROCESSED) != nil {
panic(fmt.Sprintf(
"Machine.Eval(x) expression already preprocessed: %s",
x.String()))
}
// Preprocess input using package block.
// There should only be one block, a *PackageNode.
// Other usage styles not yet supported.
pn := m.LastBlock().Source.(*PackageNode)
// Transform expression to ensure isolation.
// This is to ensure that the existing machine
// context (ie **PackageNode) doesn't get modified.
if _, ok := x.(*CallExpr); !ok {
x = Call(Fn(nil, Flds("x", InterfaceT(nil)),
Ss(
Return(x),
)))
} else {
// x already creates its own scope.
}
// Preprocess x.
x = Preprocess(m.Store, pn, x).(Expr)
// Evaluate x.
start := m.NumValues
m.PushOp(OpHalt)
m.PushExpr(x)
m.PushOp(OpEval)
m.Run()
res := m.ReapValues(start)
if len(res) != 1 {
panic("should not happen")
}
return res[0]
}
// Evaluate any preprocessed expression statically.
// This is primiarily used by the preprocessor to evaluate
// static types and values.
func (m *Machine) EvalStatic(last BlockNode, x Expr) TypedValue {
if debug {
m.Printf("Machine.EvalStatic(%v, %v)\n", last, x)
}
// X must have been preprocessed.
if x.GetAttribute(ATTR_PREPROCESSED) == nil {
panic(fmt.Sprintf(
"Machine.EvalStatic(x) expression not yet preprocessed: %s",
x.String()))
}
// Temporarily push last to m.Blocks.
m.PushBlock(last.GetStaticBlock().GetBlock())
// Evaluate x.
start := m.NumValues
m.PushOp(OpHalt)
m.PushOp(OpPopBlock)
m.PushExpr(x)
m.PushOp(OpEval)
m.Run()
res := m.ReapValues(start)
if len(res) != 1 {
panic("should not happen")
}
return res[0]
}
// Evaluate the type of any preprocessed expression statically.
// This is primiarily used by the preprocessor to evaluate
// static types of nodes.
func (m *Machine) EvalStaticTypeOf(last BlockNode, x Expr) Type {
if debug {
m.Printf("Machine.EvalStaticTypeOf(%v, %v)\n", last, x)
}
// X must have been preprocessed.
if x.GetAttribute(ATTR_PREPROCESSED) == nil {
panic(fmt.Sprintf(
"Machine.EvalStaticTypeOf(x) expression not yet preprocessed: %s",
x.String()))
}
// Temporarily push last to m.Blocks.
m.PushBlock(last.GetStaticBlock().GetBlock())
// Evaluate x.
start := m.NumValues
m.PushOp(OpHalt)
m.PushOp(OpPopBlock)
m.PushExpr(x)
m.PushOp(OpStaticTypeOf)
m.Run()
res := m.ReapValues(start)
if len(res) != 1 {
panic("should not happen")
}
tv := res[0].V.(TypeValue)
return tv.Type
}
func (m *Machine) RunStatement(s Stmt) {
sn := m.LastBlock().Source
s = Preprocess(m.Store, sn, s).(Stmt)
m.PushOp(OpHalt)
m.PushStmt(s)
m.PushOp(OpExec)
m.Run()
}
// Runs a declaration after preprocessing d. If d was already
// preprocessed, call runDeclaration() instead.
func (m *Machine) RunDeclaration(d Decl) {
// Preprocess input using package block. There should only
// be one block right now, and it's a *PackageNode.
pn := m.LastBlock().Source.(*PackageNode)
d = Preprocess(m.Store, pn, d).(Decl)
pn.PrepareNewValues(m.Package)
m.runDeclaration(d)
if debug {
if pn != m.Package.Source {
panic("package mismatch")
}
}
}
// Declarations to be run within a body (not at the file or
// package level, for which evaluations happen during
// preprocessing).
func (m *Machine) runDeclaration(d Decl) {
switch d := d.(type) {
case *FuncDecl:
// nothing to do.
// closure and package already set
// during PackageNode.NewPackage().
case *ValueDecl:
m.PushOp(OpHalt)
m.PushStmt(d)
m.PushOp(OpExec)
m.Run()
case *TypeDecl:
m.PushOp(OpHalt)
m.PushStmt(d)
m.PushOp(OpExec)
m.Run()
default:
// Do nothing for package constants.
}
}
//----------------------------------------
// Op
type Op uint8
const (
/* Control operators */
OpInvalid Op = 0x00 // invalid
OpHalt Op = 0x01 // halt (e.g. last statement)
OpNoop Op = 0x02 // no-op
OpExec Op = 0x03 // exec next statement
OpPrecall Op = 0x04 // sets X (func) to frame
OpCall Op = 0x05 // call(Frame.Func, [...])
OpCallNativeBody Op = 0x06 // call body is native
OpReturn Op = 0x07 // return ...
OpReturnFromBlock Op = 0x08 // return results (after defers)
OpReturnToBlock Op = 0x09 // copy results to block (before defer)
OpDefer Op = 0x0A // defer call(X, [...])
OpCallDeferNativeBody Op = 0x0B // call body is native
OpGo Op = 0x0C // go call(X, [...])
OpSelect Op = 0x0D // exec next select case
OpSwitchClause Op = 0x0E // exec next switch clause
OpSwitchClauseCase Op = 0x0F // exec next switch clause case
OpTypeSwitch Op = 0x10 // exec type switch clauses (all)
OpIfCond Op = 0x11 // eval cond
OpPopValue Op = 0x12 // pop X
OpPopResults Op = 0x13 // pop n call results
OpPopBlock Op = 0x14 // pop block NOTE breaks certain invariants.
OpPanic1 Op = 0x15 // pop exception and pop call frames.
OpPanic2 Op = 0x16 // pop call frames.
/* Unary & binary operators */
OpUpos Op = 0x20 // + (unary)
OpUneg Op = 0x21 // - (unary)
OpUnot Op = 0x22 // ! (unary)
OpUxor Op = 0x23 // ^ (unary)
OpUrecv Op = 0x25 // <- (unary) // TODO make expr
OpLor Op = 0x26 // ||
OpLand Op = 0x27 // &&
OpEql Op = 0x28 // ==
OpNeq Op = 0x29 // !=
OpLss Op = 0x2A // <
OpLeq Op = 0x2B // <=
OpGtr Op = 0x2C // >
OpGeq Op = 0x2D // >=
OpAdd Op = 0x2E // +
OpSub Op = 0x2F // -
OpBor Op = 0x30 // |
OpXor Op = 0x31 // ^
OpMul Op = 0x32 // *
OpQuo Op = 0x33 // /
OpRem Op = 0x34 // %
OpShl Op = 0x35 // <<
OpShr Op = 0x36 // >>
OpBand Op = 0x37 // &
OpBandn Op = 0x38 // &^
/* Other expression operators */
OpEval Op = 0x40 // eval next expression
OpBinary1 Op = 0x41 // X op ?
OpIndex1 Op = 0x42 // X[Y]
OpIndex2 Op = 0x43 // (_, ok :=) X[Y]
OpSelector Op = 0x44 // X.Y
OpSlice Op = 0x45 // X[Low:High:Max]
OpStar Op = 0x46 // *X (deref or pointer-to)
OpRef Op = 0x47 // &X
OpTypeAssert1 Op = 0x48 // X.(Type)
OpTypeAssert2 Op = 0x49 // (_, ok :=) X.(Type)
OpStaticTypeOf Op = 0x4A // static type of X
OpCompositeLit Op = 0x4B // X{???}
OpArrayLit Op = 0x4C // [Len]{...}
OpSliceLit Op = 0x4D // []{...}
OpMapLit Op = 0x4E // X{...}
OpStructLit Op = 0x4F // X{...}
OpFuncLit Op = 0x50 // func(T){Body}
OpConvert Op = 0x51 // Y(X)
/* Native operators */
OpStructLitGoNative Op = 0x60
OpCallGoNative Op = 0x61
/* Type operators */
OpFieldType Op = 0x70 // Name: X `tag`
OpArrayType Op = 0x71 // [X]Y{}
OpSliceType Op = 0x72 // []X{}
OpPointerType Op = 0x73 // *X
OpInterfaceType Op = 0x74 // interface{...}
OpChanType Op = 0x75 // [<-]chan[<-]X
OpFuncType Op = 0x76 // func(params...)results...
OpMapType Op = 0x77 // map[X]Y
OpStructType Op = 0x78 // struct{...}
/* Statement operators */
OpAssign Op = 0x80 // Lhs = Rhs
OpAddAssign Op = 0x81 // Lhs += Rhs
OpSubAssign Op = 0x82 // Lhs -= Rhs
OpMulAssign Op = 0x83 // Lhs *= Rhs
OpQuoAssign Op = 0x84 // Lhs /= Rhs
OpRemAssign Op = 0x85 // Lhs %= Rhs
OpBandAssign Op = 0x86 // Lhs &= Rhs
OpBandnAssign Op = 0x87 // Lhs &^= Rhs
OpBorAssign Op = 0x88 // Lhs |= Rhs
OpXorAssign Op = 0x89 // Lhs ^= Rhs
OpShlAssign Op = 0x8A // Lhs <<= Rhs
OpShrAssign Op = 0x8B // Lhs >>= Rhs
OpDefine Op = 0x8C // X... := Y...
OpInc Op = 0x8D // X++
OpDec Op = 0x8E // X--
/* Decl operators */
OpValueDecl Op = 0x90 // var/const ...
OpTypeDecl Op = 0x91 // type ...
/* Loop (sticky) operators (>= 0xD0) */
OpSticky Op = 0xD0 // not a real op.
OpBody Op = 0xD1 // if/block/switch/select.
OpForLoop Op = 0xD2
OpRangeIter Op = 0xD3
OpRangeIterString Op = 0xD4
OpRangeIterMap Op = 0xD5
OpRangeIterArrayPtr Op = 0xD6
OpReturnCallDefers Op = 0xD7 // TODO rename?
)
//----------------------------------------
// main run loop.
func (m *Machine) Run() {
for {
op := m.PopOp()
// TODO: this can be optimized manually, even into tiers.
switch op {
/* Control operators */
case OpHalt:
return
case OpNoop:
continue
case OpExec:
m.doOpExec(op)
case OpPrecall:
m.doOpPrecall()
case OpCall:
m.doOpCall()
case OpCallNativeBody:
m.doOpCallNativeBody()
case OpReturn:
m.doOpReturn()
case OpReturnFromBlock:
m.doOpReturnFromBlock()
case OpReturnToBlock:
m.doOpReturnToBlock()
case OpDefer:
m.doOpDefer()
case OpPanic1:
m.doOpPanic1()
case OpPanic2:
m.doOpPanic2()
case OpCallDeferNativeBody:
m.doOpCallDeferNativeBody()
case OpGo:
panic("not yet implemented")
case OpSelect:
panic("not yet implemented")
case OpSwitchClause:
m.doOpSwitchClause()
case OpSwitchClauseCase:
m.doOpSwitchClauseCase()
case OpTypeSwitch:
m.doOpTypeSwitch()
case OpIfCond:
m.doOpIfCond()
case OpPopValue:
m.PopValue()
case OpPopResults:
m.PopResults()
case OpPopBlock:
m.PopBlock()
/* Unary operators */
case OpUpos:
m.doOpUpos()
case OpUneg:
m.doOpUneg()
case OpUnot:
m.doOpUnot()
case OpUxor:
m.doOpUxor()
case OpUrecv:
m.doOpUrecv()
/* Binary operators */
case OpLor:
m.doOpLor()
case OpLand:
m.doOpLand()
case OpEql:
m.doOpEql()
case OpNeq:
m.doOpNeq()
case OpLss:
m.doOpLss()
case OpLeq:
m.doOpLeq()
case OpGtr:
m.doOpGtr()
case OpGeq:
m.doOpGeq()
case OpAdd:
m.doOpAdd()
case OpSub:
m.doOpSub()
case OpBor:
m.doOpBor()
case OpXor:
m.doOpXor()
case OpMul:
m.doOpMul()
case OpQuo:
m.doOpQuo()
case OpRem:
m.doOpRem()
case OpShl:
m.doOpShl()
case OpShr:
m.doOpShr()
case OpBand:
m.doOpBand()
case OpBandn:
m.doOpBandn()
/* Expression operators */
case OpEval:
m.doOpEval()
case OpBinary1:
m.doOpBinary1()
case OpIndex1:
m.doOpIndex1()
case OpIndex2:
m.doOpIndex2()
case OpSelector:
m.doOpSelector()
case OpSlice:
m.doOpSlice()
case OpStar:
m.doOpStar()
case OpRef:
m.doOpRef()
case OpTypeAssert1:
m.doOpTypeAssert1()
case OpTypeAssert2:
m.doOpTypeAssert2()
case OpStaticTypeOf:
m.doOpStaticTypeOf()
case OpCompositeLit:
m.doOpCompositeLit()
case OpArrayLit:
m.doOpArrayLit()
case OpSliceLit:
m.doOpSliceLit()
case OpFuncLit:
m.doOpFuncLit()
case OpMapLit:
m.doOpMapLit()
case OpStructLit:
m.doOpStructLit()
case OpConvert:
m.doOpConvert()
/* GoNative Operators */
case OpStructLitGoNative:
m.doOpStructLitGoNative()
case OpCallGoNative:
m.doOpCallGoNative()
/* Type operators */
case OpFieldType:
m.doOpFieldType()
case OpArrayType:
m.doOpArrayType()
case OpSliceType:
m.doOpSliceType()
case OpChanType:
m.doOpChanType()
case OpFuncType:
m.doOpFuncType()
case OpMapType:
m.doOpMapType()
case OpStructType:
m.doOpStructType()
case OpInterfaceType:
m.doOpInterfaceType()
/* Statement operators */
case OpAssign:
m.doOpAssign()
case OpAddAssign:
m.doOpAddAssign()
case OpSubAssign:
m.doOpSubAssign()
case OpMulAssign:
m.doOpMulAssign()
case OpQuoAssign:
m.doOpQuoAssign()
case OpRemAssign:
m.doOpRemAssign()
case OpBandAssign:
m.doOpBandAssign()
case OpBandnAssign:
m.doOpBandnAssign()
case OpBorAssign:
m.doOpBorAssign()
case OpXorAssign:
m.doOpXorAssign()
case OpShlAssign:
m.doOpShlAssign()
case OpShrAssign:
m.doOpShrAssign()
case OpDefine:
m.doOpDefine()
case OpInc:
m.doOpInc()
case OpDec:
m.doOpDec()
/* Decl operators */
case OpValueDecl:
m.doOpValueDecl()
case OpTypeDecl:
m.doOpTypeDecl()
/* Loop (sticky) operators */
case OpBody:
m.doOpExec(op)
case OpForLoop:
m.doOpExec(op)
case OpRangeIter, OpRangeIterArrayPtr:
m.doOpExec(op)
case OpRangeIterString:
m.doOpExec(op)
case OpRangeIterMap:
m.doOpExec(op)
case OpReturnCallDefers:
m.doOpReturnCallDefers()
default:
panic(fmt.Sprintf("unexpected opcode %s", op.String()))
}
}
}
//----------------------------------------
// push pop methods.
func (m *Machine) PushOp(op Op) {
if debug {
m.Printf("+o %v\n", op)
}
m.Ops[m.NumOps] = op
m.NumOps++
}
func (m *Machine) PopOp() Op {
numOps := m.NumOps
op := m.Ops[numOps-1]
if debug {
m.Printf("-o %v\n", op)
}
if OpSticky <= op {
// do not pop persistent op types.
} else {
m.NumOps--
}
return op
}
func (m *Machine) ForcePopOp() {
if debug {
m.Printf("-o! %v\n", m.Ops[m.NumOps-1])
}
m.NumOps--
}
// Offset starts at 1.
// DEPRECATED use PeekStmt1() instead.
func (m *Machine) PeekStmt(offset int) Stmt {
if debug {
if offset != 1 {
panic("should not happen")
}
}
return m.Stmts[len(m.Stmts)-offset]
}
func (m *Machine) PeekStmt1() Stmt {
numStmts := len(m.Stmts)
s := m.Stmts[numStmts-1]
if bs, ok := s.(*bodyStmt); ok {
return bs.Active
} else {
return m.Stmts[numStmts-1]
}
}
func (m *Machine) PushStmt(s Stmt) {
if debug {
m.Printf("+s %v\n", s)
}
m.Stmts = append(m.Stmts, s)
}
func (m *Machine) PushStmts(ss ...Stmt) {
if debug {
for _, s := range ss {
m.Printf("+s %v\n", s)
}
}
m.Stmts = append(m.Stmts, ss...)
}
func (m *Machine) PopStmt() Stmt {
numStmts := len(m.Stmts)
s := m.Stmts[numStmts-1]
if debug {
m.Printf("-s %v\n", s)
}
if bs, ok := s.(*bodyStmt); ok {
return bs.PopActiveStmt()
} else {
// general case.
m.Stmts = m.Stmts[:numStmts-1]
return s
}
}
func (m *Machine) ForcePopStmt() (s Stmt) {
numStmts := len(m.Stmts)
s = m.Stmts[numStmts-1]
if debug {
m.Printf("-s %v\n", s)
}
// TODO debug lines and assertions.
m.Stmts = m.Stmts[:len(m.Stmts)-1]
return
}
// Offset starts at 1.
func (m *Machine) PeekExpr(offset int) Expr {
return m.Exprs[len(m.Exprs)-offset]
}
func (m *Machine) PushExpr(x Expr) {
if debug {
m.Printf("+x %v\n", x)
}
m.Exprs = append(m.Exprs, x)
}
func (m *Machine) PopExpr() Expr {
numExprs := len(m.Exprs)
x := m.Exprs[numExprs-1]
if debug {
m.Printf("-x %v\n", x)
}
m.Exprs = m.Exprs[:numExprs-1]
return x
}
// Returns reference to value in Values stack. Offset starts at 1.
func (m *Machine) PeekValue(offset int) *TypedValue {
return &m.Values[m.NumValues-offset]
}
// XXX delete?
func (m *Machine) PeekType(offset int) Type {
return m.Values[m.NumValues-offset].T
}
func (m *Machine) PushValue(tv TypedValue) {
if debug {
m.Printf("+v %s\n", tv.String())
}
if len(m.Values) == m.NumValues {
// TODO tune.
newValues := make([]TypedValue, len(m.Values)*2)
copy(newValues, m.Values)
m.Values = newValues
}
m.Values[m.NumValues] = tv
m.NumValues++
return
}
// Resulting reference is volatile.
func (m *Machine) PopValue() (tv *TypedValue) {
tv = &m.Values[m.NumValues-1]
if debug {
m.Printf("-v %s\n", tv.String())
}
m.NumValues--
return tv
}
// Returns a slice of n values in the stack and decrements NumValues.
// NOTE: The results are on the values stack, so they must be copied or used
// immediately. If you need to use the machine before or during usage,
// consider using PopCopyValues().
// NOTE: the values are in stack order, oldest first, the opposite order of
// multiple pop calls. This is used for params assignment, for example.
func (m *Machine) PopValues(n int) []TypedValue {
if debug {
for i := 0; i < n; i++ {
tv := m.Values[m.NumValues-n+i]
m.Printf("-vs[%d/%d] %s\n", i, n, tv.String())
}
}
m.NumValues -= n
return m.Values[m.NumValues : m.NumValues+n]
}
// Like PopValues(), but copies the values onto a new slice.
func (m *Machine) PopCopyValues(n int) []TypedValue {
res := make([]TypedValue, n)
ptvs := m.PopValues(n)
copy(res, ptvs)
return res
}
// Decrements NumValues by number of last results.
func (m *Machine) PopResults() {
if debug {
for i := 0; i < m.NumResults; i++ {
m.PopValue()
}
} else {
m.NumValues -= m.NumResults
}
m.NumResults = 0
}
// Pops values with index start or greater.
func (m *Machine) ReapValues(start int) []TypedValue {
end := m.NumValues
rs := make([]TypedValue, end-start)
copy(rs, m.Values[start:end])
m.NumValues = start
return rs
}
func (m *Machine) PushBlock(b *Block) {
if debug {
m.Println("+B")
}
m.Blocks = append(m.Blocks, b)
}
func (m *Machine) PopBlock() (b *Block) {
if debug {
m.Println("-B")
}
numBlocks := len(m.Blocks)
b = m.Blocks[numBlocks-1]
m.Blocks = m.Blocks[:numBlocks-1]
return b
}
// The result is a volatile reference in the machine's type stack.
// Mutate and forget.
func (m *Machine) LastBlock() *Block {
return m.Blocks[len(m.Blocks)-1]
}
// Pushes a frame with one less statement.
func (m *Machine) PushFrameBasic(s Stmt) {
label := s.GetAttribute(ATTR_LABEL)
lname := Name("")
if label != nil {
lname = label.(Name)
}
fr := Frame{
Label: lname,
Source: s,
NumOps: m.NumOps,
NumValues: m.NumValues,
NumExprs: len(m.Exprs),
NumStmts: len(m.Stmts),
NumBlocks: len(m.Blocks),
}
if debug {
m.Printf("+F %#v\n", fr)
}
m.Frames = append(m.Frames, fr)
}
// TODO: track breaks/panics/returns on frame and
// ensure the counts are consistent, otherwise we mask
// bugs with frame pops.
func (m *Machine) PushFrameCall(cx *CallExpr, fv *FuncValue, recv TypedValue) {
fr := Frame{
Source: cx,
NumOps: m.NumOps,
NumValues: m.NumValues,
NumExprs: len(m.Exprs),
NumStmts: len(m.Stmts),
NumBlocks: len(m.Blocks),
Func: fv,
GoFunc: nil,
Receiver: recv,
NumArgs: cx.NumArgs,
IsVarg: cx.Varg,
Defers: nil,
LastPackage: m.Package,
LastRealm: m.Realm,
}
if debug {