Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assembler: parse negative values in Offset properly #188

Merged
merged 2 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pkg/assembler/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ var parser *participle.Parser[CasmProgram] = participle.MustBuild[CasmProgram](
// also required for:
// instr -> jmp rel <expr> and
// instr -> jmp rel <expr> if <val> != 0
participle.UseLookahead(7),
//
// an extra +1 (7->8) step is required for an optionally negative offset (see #186):
// jmp rel [fp + -111]; without lookahead=8, it won't be parsed sucessfully
participle.UseLookahead(8),
)

// Given a CASM program it returns its encoded bytecode
Expand Down
38 changes: 38 additions & 0 deletions pkg/assembler/assembler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,44 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCondJumpNegativeImmediate(t *testing.T) {
// See #186
instrList := parseSingleInstructionToInstrList("jmp rel [ap+5] if [fp + -3] != 0;")

expected := Instruction{
OffDest: -3,
OffOp0: -1,
OffOp1: 5,
DstRegister: Fp,
Op0Register: Fp,
Op1Source: ApPlusOffOp1,
Res: Op1,
PcUpdate: PcUpdateJnz,
ApUpdate: SameAp,
Opcode: OpCodeNop,
}
assert.Equal(t, expected, instrList[0])
}

func TestJumpNegativeImmediate(t *testing.T) {
// See #186
instrList := parseSingleInstructionToInstrList("jmp rel [fp + -111];")

expected := Instruction{
OffDest: -1,
OffOp0: -1,
OffOp1: -111,
DstRegister: Fp,
Op0Register: Fp,
Op1Source: FpPlusOffOp1,
Res: Op1,
PcUpdate: PcUpdateJumpRel,
ApUpdate: SameAp,
Opcode: OpCodeNop,
}
assert.Equal(t, expected, instrList[0])
}

func TestAssertEqRegisterToInstrList(t *testing.T) {
instrList := parseSingleInstructionToInstrList("[ap] = [fp], ap++;")

Expand Down
2 changes: 1 addition & 1 deletion pkg/assembler/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ type DoubleDeref struct {

type Offset struct {
Sign string `@("+" | "-")`
Value *int `@Int`
Value *int `@("-"? Int)`
}

type MathOperation struct {
Expand Down
Loading