From 7c353009d5099789282c2b46360b4ba5eb6bc332 Mon Sep 17 00:00:00 2001 From: Michael Forney Date: Sat, 26 Oct 2019 14:51:06 -0700 Subject: [PATCH] core/asm: Fix PC calculations when a label is pushed Incrementing PC by 5 is only correct if the label appears after a jump, in which case there is an implicit push. When it appears after an explicit push, PC should only be incremented by 4. --- core/asm/compiler.go | 7 ++++++- core/asm/compiler_test.go | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/asm/compiler.go b/core/asm/compiler.go index c951725213..00da650913 100644 --- a/core/asm/compiler.go +++ b/core/asm/compiler.go @@ -57,6 +57,7 @@ func NewCompiler(debug bool) *Compiler { // second stage to push labels and determine the right // position. func (c *Compiler) Feed(ch <-chan token) { + var prev token for i := range ch { switch i.typ { case number: @@ -73,10 +74,14 @@ func (c *Compiler) Feed(ch <-chan token) { c.labels[i.text] = c.pc c.pc++ case label: - c.pc += 5 + c.pc += 4 + if prev.typ == element && isJump(prev.text) { + c.pc++ + } } c.tokens = append(c.tokens, i) + prev = i } if c.debug { fmt.Fprintln(os.Stderr, "found", len(c.labels), "labels") diff --git a/core/asm/compiler_test.go b/core/asm/compiler_test.go index 065e1961df..684ca574b0 100644 --- a/core/asm/compiler_test.go +++ b/core/asm/compiler_test.go @@ -32,6 +32,13 @@ func TestCompiler(t *testing.T) { `, output: "5a5b6300000001", }, + { + input: ` + PUSH @label + label: +`, + output: "63000000055b", + }, } for _, test := range tests { ch := Lex([]byte(test.input), false)