From b6fa4bc82398b09307f2e6b75e27422d1d1ecb33 Mon Sep 17 00:00:00 2001 From: Mel Date: Tue, 19 Jul 2022 02:27:19 +0200 Subject: Implement stack hygiene --- pkg/lang/vm/exec.go | 10 ++++++++++ pkg/lang/vm/text/decompiler.go | 2 +- pkg/lang/vm/vm.go | 5 ++++- pkg/lang/vm/vm_test.go | 5 ++--- 4 files changed, 17 insertions(+), 5 deletions(-) (limited to 'pkg/lang/vm') diff --git a/pkg/lang/vm/exec.go b/pkg/lang/vm/exec.go index f92e486..3a1ce36 100644 --- a/pkg/lang/vm/exec.go +++ b/pkg/lang/vm/exec.go @@ -97,6 +97,16 @@ func (vm *VM) execPushType(name string) error { return nil } +func (vm *VM) execDrop(dropAmount uint) error { + for i := 0; i < int(dropAmount); i++ { + if _, err := vm.popAndDrop(); err != nil { + return err + } + } + + return nil +} + func (vm *VM) execGetMember(name string) error { parent, err := vm.stack.Pop() if err != nil { diff --git a/pkg/lang/vm/text/decompiler.go b/pkg/lang/vm/text/decompiler.go index b3c8bb4..2b4e704 100644 --- a/pkg/lang/vm/text/decompiler.go +++ b/pkg/lang/vm/text/decompiler.go @@ -55,7 +55,6 @@ func (d *Decompiler) decompileInstruction(bc code.Raw) (string, code.Raw) { code.OpPushNull, code.OpPushArray, code.OpPushObject, - code.OpDrop, code.OpAnchorType, code.OpAdd, code.OpSub, @@ -75,6 +74,7 @@ func (d *Decompiler) decompileInstruction(bc code.Raw) (string, code.Raw) { // Operations that take an int. case code.OpPushInt, + code.OpDrop, code.OpGetLocal, code.OpSetLocal, code.OpGetEnv, diff --git a/pkg/lang/vm/vm.go b/pkg/lang/vm/vm.go index ff9c28e..8b47915 100644 --- a/pkg/lang/vm/vm.go +++ b/pkg/lang/vm/vm.go @@ -116,7 +116,10 @@ func (vm *VM) step(op code.Op) (stepDecision, error) { err = vm.execPushType(name) case code.OpDrop: - _, err = vm.stack.Pop() + dropAmount, advance := vm.code.GetUint(vm.pc) + vm.pc += advance + + err = vm.execDrop(uint(dropAmount)) case code.OpGetGlobal: panic("not implemented") diff --git a/pkg/lang/vm/vm_test.go b/pkg/lang/vm/vm_test.go index 7fe07dd..7e749a4 100644 --- a/pkg/lang/vm/vm_test.go +++ b/pkg/lang/vm/vm_test.go @@ -75,7 +75,7 @@ func TestFibonacci(t *testing.T) { temp_arr_push # Drop local 1, which was the length of the array, which we no longer need - drop + drop 1 get_local 0 temp_arr_len @@ -391,8 +391,7 @@ func TestPrimes(t *testing.T) { add set_local 0 - drop - drop + drop 2 jmp @main_loop @end: -- cgit 1.4.1