1
1
// Evaluate opcodes
2
2
package vm
3
3
4
- // FIXME vm.result -> retval
5
- // vm.exit -> vm.why
6
- // why codes
7
-
8
4
// FIXME make opcode its own type so can stringer
9
5
10
6
// FIXME use LocalVars instead of storing everything in the Locals dict
@@ -119,13 +115,13 @@ func (vm *Vm) AddTraceback(exc *py.ExceptionInfo) {
119
115
// The exception must be a valid exception instance (eg as returned by
120
116
// py.MakeException)
121
117
//
122
- // It sets vm.curexc.* and sets vm.exit to exitException
118
+ // It sets vm.curexc.* and sets vm.why to whyException
123
119
func (vm * Vm ) SetException (exception py.Object ) {
124
120
vm .curexc .Value = exception
125
121
vm .curexc .Type = exception .Type ()
126
122
vm .curexc .Traceback = nil
127
123
vm .AddTraceback (& vm .curexc )
128
- vm .exit = exitException
124
+ vm .why = whyException
129
125
}
130
126
131
127
// Check for an exception (panic)
@@ -136,7 +132,7 @@ func (vm *Vm) CheckExceptionRecover(r interface{}) {
136
132
if exc , ok := r .(py.ExceptionInfo ); ok {
137
133
vm .curexc = exc
138
134
vm .AddTraceback (& vm .curexc )
139
- vm .exit = exitException
135
+ vm .why = whyException
140
136
debugf ("*** Propagating exception: %s\n " , exc .Error ())
141
137
} else {
142
138
// Coerce whatever was raised into a *Exception
@@ -489,15 +485,15 @@ func do_PRINT_EXPR(vm *Vm, arg int32) {
489
485
// Terminates a loop due to a break statement.
490
486
func do_BREAK_LOOP (vm * Vm , arg int32 ) {
491
487
defer vm .CheckException ()
492
- vm .exit = exitBreak
488
+ vm .why = whyBreak
493
489
}
494
490
495
491
// Continues a loop due to a continue statement. target is the address
496
492
// to jump to (which should be a FOR_ITER instruction).
497
493
func do_CONTINUE_LOOP (vm * Vm , target int32 ) {
498
494
defer vm .CheckException ()
499
- vm .result = py .Int (target )
500
- vm .exit = exitContinue
495
+ vm .retval = py .Int (target )
496
+ vm .why = whyContinue
501
497
}
502
498
503
499
// Iterate v argcnt times and store the results on the stack (via decreasing
@@ -599,13 +595,13 @@ func do_MAP_ADD(vm *Vm, i int32) {
599
595
// Returns with TOS to the caller of the function.
600
596
func do_RETURN_VALUE (vm * Vm , arg int32 ) {
601
597
defer vm .CheckException ()
602
- vm .result = vm .POP ()
598
+ vm .retval = vm .POP ()
603
599
if len (vm .frame .Stack ) != 0 {
604
600
debugf ("vmstack = %#v\n " , vm .frame .Stack )
605
601
panic ("vm stack should be empty at this point" )
606
602
}
607
603
vm .frame .Yielded = false
608
- vm .exit = exitReturn
604
+ vm .why = whyReturn
609
605
}
610
606
611
607
// Pops TOS and delegates to it as a subiterator from a generator.
@@ -627,21 +623,21 @@ func do_YIELD_FROM(vm *Vm, arg int32) {
627
623
}
628
624
// x remains on stack, retval is value to be yielded
629
625
// FIXME vm.frame.Stacktop = stack_pointer
630
- //why = exitYield
626
+ //why = whyYield
631
627
// and repeat...
632
628
vm .frame .Lasti --
633
629
634
- vm .result = retval
630
+ vm .retval = retval
635
631
vm .frame .Yielded = true
636
- vm .exit = exitYield
632
+ vm .why = whyYield
637
633
}
638
634
639
635
// Pops TOS and yields it from a generator.
640
636
func do_YIELD_VALUE (vm * Vm , arg int32 ) {
641
637
defer vm .CheckException ()
642
- vm .result = vm .POP ()
638
+ vm .retval = vm .POP ()
643
639
vm .frame .Yielded = true
644
- vm .exit = exitYield
640
+ vm .why = whyYield
645
641
}
646
642
647
643
// Loads all symbols not starting with '_' directly from the module
@@ -702,16 +698,16 @@ func do_END_FINALLY(vm *Vm, arg int32) {
702
698
// None exception
703
699
debugf (" END_FINALLY: None\n " )
704
700
} else if vInt , ok := v .(py.Int ); ok {
705
- vm .exit = vmExit (vInt )
706
- debugf (" END_FINALLY: Int %v\n " , vm .exit )
707
- switch vm .exit {
708
- case exitYield :
709
- panic ("Unexpected exitYield in END_FINALLY" )
710
- case exitException :
711
- panic ("Unexpected exitException in END_FINALLY" )
712
- case exitReturn , exitContinue :
713
- vm .result = vm .POP ()
714
- case exitSilenced :
701
+ vm .why = vmStatus (vInt )
702
+ debugf (" END_FINALLY: Int %v\n " , vm .why )
703
+ switch vm .why {
704
+ case whyYield :
705
+ panic ("Unexpected whyYield in END_FINALLY" )
706
+ case whyException :
707
+ panic ("Unexpected whyException in END_FINALLY" )
708
+ case whyReturn , whyContinue :
709
+ vm .retval = vm .POP ()
710
+ case whySilenced :
715
711
// An exception was silenced by 'with', we must
716
712
// manually unwind the EXCEPT_HANDLER block which was
717
713
// created when the exception was caught, otherwise
@@ -723,7 +719,7 @@ func do_END_FINALLY(vm *Vm, arg int32) {
723
719
panic ("Expecting EXCEPT_HANDLER in END_FINALLY" )
724
720
}
725
721
vm .UnwindExceptHandler (frame , b )
726
- vm .exit = exitNot
722
+ vm .why = whyNot
727
723
}
728
724
} else if py .ExceptionClassCheck (v ) {
729
725
w := vm .POP ()
@@ -733,11 +729,11 @@ func do_END_FINALLY(vm *Vm, arg int32) {
733
729
vm .curexc .Type , _ = v .(* py.Type )
734
730
vm .curexc .Value = w
735
731
vm .curexc .Traceback , _ = u .(* py.Traceback )
736
- vm .exit = exitException
732
+ vm .why = whyException
737
733
} else {
738
734
vm .SetException (py .ExceptionNewf (py .SystemError , "'finally' pops bad exception %#v" , v ))
739
735
}
740
- debugf ("END_FINALLY: vm.exit = %v\n " , vm .exit )
736
+ debugf ("END_FINALLY: vm.why = %v\n " , vm .why )
741
737
}
742
738
743
739
// Loads the __build_class__ helper function to the stack which
@@ -1250,7 +1246,7 @@ func (vm *Vm) raise(exc, cause py.Object) {
1250
1246
// Resignal the exception
1251
1247
vm .curexc = vm .exc
1252
1248
// Signal the existing exception again
1253
- vm .exit = exitException
1249
+ vm .why = whyException
1254
1250
1255
1251
}
1256
1252
} else {
@@ -1518,7 +1514,7 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1518
1514
1519
1515
var opcode byte
1520
1516
var arg int32
1521
- for vm .exit == exitNot {
1517
+ for vm .why == whyNot {
1522
1518
frame := vm .frame
1523
1519
debugf ("* %4d:" , frame .Lasti )
1524
1520
opcodes := frame .Code .Code
@@ -1546,21 +1542,21 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1546
1542
// }
1547
1543
// }
1548
1544
}
1549
- if vm .exit == exitYield {
1545
+ if vm .why == whyYield {
1550
1546
goto fast_yield
1551
1547
}
1552
1548
1553
1549
// Something exceptional has happened - unwind the block stack
1554
1550
// and find out what
1555
- for vm .exit != exitNot && vm .frame .Block != nil {
1551
+ for vm .why != whyNot && vm .frame .Block != nil {
1556
1552
// Peek at the current block.
1557
1553
frame := vm .frame
1558
1554
b := frame .Block
1559
1555
debugf ("*** Unwinding %#v vm %#v\n " , b , vm )
1560
1556
1561
- if b .Type == SETUP_LOOP && vm .exit == exitContinue {
1562
- vm .exit = exitNot
1563
- dest := vm .result .(py.Int )
1557
+ if b .Type == SETUP_LOOP && vm .why == whyContinue {
1558
+ vm .why = whyNot
1559
+ dest := vm .retval .(py.Int )
1564
1560
frame .Lasti = int32 (dest )
1565
1561
break
1566
1562
}
@@ -1574,13 +1570,13 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1574
1570
continue
1575
1571
}
1576
1572
vm .UnwindBlock (frame , b )
1577
- if b .Type == SETUP_LOOP && vm .exit == exitBreak {
1573
+ if b .Type == SETUP_LOOP && vm .why == whyBreak {
1578
1574
debugf ("*** Loop\n " )
1579
- vm .exit = exitNot
1575
+ vm .why = whyNot
1580
1576
frame .Lasti = b .Handler
1581
1577
break
1582
1578
}
1583
- if vm .exit == exitException && (b .Type == SETUP_EXCEPT || b .Type == SETUP_FINALLY ) {
1579
+ if vm .why == whyException && (b .Type == SETUP_EXCEPT || b .Type == SETUP_FINALLY ) {
1584
1580
debugf ("*** Exception\n " )
1585
1581
handler := b .Handler
1586
1582
// This invalidates b
@@ -1615,29 +1611,29 @@ func RunFrame(frame *py.Frame) (res py.Object, err error) {
1615
1611
} else {
1616
1612
vm .PUSH (exc )
1617
1613
}
1618
- vm .exit = exitNot
1614
+ vm .why = whyNot
1619
1615
frame .Lasti = handler
1620
1616
break
1621
1617
}
1622
1618
if b .Type == SETUP_FINALLY {
1623
- if vm .exit == exitReturn || vm .exit == exitContinue {
1624
- vm .PUSH (vm .result )
1619
+ if vm .why == whyReturn || vm .why == whyContinue {
1620
+ vm .PUSH (vm .retval )
1625
1621
}
1626
- vm .PUSH (py .Int (vm .exit ))
1627
- vm .exit = exitNot
1622
+ vm .PUSH (py .Int (vm .why ))
1623
+ vm .why = whyNot
1628
1624
frame .Lasti = b .Handler
1629
1625
break
1630
1626
}
1631
1627
}
1632
1628
}
1633
- debugf ("EXIT with %v\n " , vm .exit )
1634
- if vm .exit != exitReturn {
1635
- vm .result = nil
1629
+ debugf ("EXIT with %v\n " , vm .why )
1630
+ if vm .why != whyReturn {
1631
+ vm .retval = nil
1636
1632
}
1637
- if vm .result == nil && ! vm .curexc .IsSet () {
1633
+ if vm .retval == nil && ! vm .curexc .IsSet () {
1638
1634
panic ("vm: no result or exception" )
1639
1635
}
1640
- if vm .result != nil && vm .curexc .IsSet () {
1636
+ if vm .retval != nil && vm .curexc .IsSet () {
1641
1637
panic ("vm: result and exception" )
1642
1638
}
1643
1639
@@ -1664,9 +1660,9 @@ fast_yield:
1664
1660
// }
1665
1661
1666
1662
if vm .curexc .IsSet () {
1667
- return vm .result , vm .curexc
1663
+ return vm .retval , vm .curexc
1668
1664
}
1669
- return vm .result , nil
1665
+ return vm .retval , nil
1670
1666
}
1671
1667
1672
1668
// Run the virtual machine on a Code object
0 commit comments