summaryrefslogtreecommitdiff
path: root/vm
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2025-11-26 15:42:21 +0100
committerMarc Vertes <mvertes@free.fr>2025-11-26 15:42:21 +0100
commitaed20c1c453e50f716c454c0bd7e4995a0f5d898 (patch)
tree91af798989300451113e5b865a20fb2eae777938 /vm
parent31054164870b413db797572b8e3d5a00c41d328e (diff)
Chore: improve tracing of code emits
Diffstat (limited to 'vm')
-rw-r--r--vm/op_string.go5
-rw-r--r--vm/vm.go5
2 files changed, 7 insertions, 3 deletions
diff --git a/vm/op_string.go b/vm/op_string.go
index ef3f3ee..ac5b557 100644
--- a/vm/op_string.go
+++ b/vm/op_string.go
@@ -42,11 +42,12 @@ func _() {
_ = x[Return-31]
_ = x[Sub-32]
_ = x[Subi-33]
+ _ = x[Swap-34]
}
-const _Op_name = "NopAddAddrAssignFassignVassignCallCalliCallXDerefDupFdupEqualEqualSetExitFieldGreaterGrowIndexJumpJumpTrueJumpFalseJumpSetTrueJumpSetFalseLowerLoweriMulNewNotPopPushReturnSubSubi"
+const _Op_name = "NopAddAddrAssignFassignVassignCallCalliCallXDerefDupFdupEqualEqualSetExitFieldGreaterGrowIndexJumpJumpTrueJumpFalseJumpSetTrueJumpSetFalseLowerLoweriMulNewNotPopPushReturnSubSubiSwap"
-var _Op_index = [...]uint8{0, 3, 6, 10, 16, 23, 30, 34, 39, 44, 49, 52, 56, 61, 69, 73, 78, 85, 89, 94, 98, 106, 115, 126, 138, 143, 149, 152, 155, 158, 161, 165, 171, 174, 178}
+var _Op_index = [...]uint8{0, 3, 6, 10, 16, 23, 30, 34, 39, 44, 49, 52, 56, 61, 69, 73, 78, 85, 89, 94, 98, 106, 115, 126, 138, 143, 149, 152, 155, 158, 161, 165, 171, 174, 178, 182}
func (i Op) String() string {
idx := int(i) - 0
diff --git a/vm/vm.go b/vm/vm.go
index 6c9f3e1..f981415 100644
--- a/vm/vm.go
+++ b/vm/vm.go
@@ -55,6 +55,7 @@ const (
Return // [r1 .. ri] -- ; exit frame: sp = fp, fp = pop
Sub // n1 n2 -- diff ; diff = n1 - n2
Subi // n1 -- diff ; diff = n1 - $1
+ Swap // --
)
// Instruction represents a virtual machine bytecode instruction.
@@ -65,7 +66,7 @@ type Instruction struct {
}
func (i Instruction) String() (s string) {
- s = i.Op.String()
+ s = fmt.Sprintf("%4d: %v", i.Pos, i.Op)
var sb strings.Builder
for _, a := range i.Arg {
sb.WriteString(fmt.Sprintf(" %v", a))
@@ -226,6 +227,8 @@ func (m *Machine) Run() (err error) {
mem = mem[:sp-1]
case Subi:
mem[sp-1] = ValueOf(int(mem[sp-1].Data.Int()) - c.Arg[0])
+ case Swap:
+ mem[sp-2], mem[sp-1] = mem[sp-1], mem[sp-2]
case Index:
mem[sp-2].Data = mem[sp-1].Data.Index(int(mem[sp-2].Data.Int()))
mem = mem[:sp-1]