Skip to content

Commit

Permalink
Added jump instruction
Browse files Browse the repository at this point in the history
  • Loading branch information
RealA10N committed Dec 13, 2024
1 parent a673efa commit d42d1be
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
49 changes: 49 additions & 0 deletions usm64/isa/jump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package usm64isa

import (
"alon.kr/x/list"
"alon.kr/x/usm/core"
"alon.kr/x/usm/gen"
usm64core "alon.kr/x/usm/usm64/core"
)

type JumpInstruction struct {
JumpToInstructionIndex uint64
}

func (i *JumpInstruction) Emulate(
ctx *usm64core.EmulationContext,
) usm64core.EmulationError {
ctx.NextInstructionIndex = i.JumpToInstructionIndex
return nil
}

func NewJumpInstruction(
targets []usm64core.Register,
argument []usm64core.Argument,
) (usm64core.Instruction, core.ResultList) {

label, ok := argument[0].(usm64core.Label)
if !ok {
v := argument[0].Declaration()
return nil, list.FromSingle(core.Result{
{
Type: core.ErrorResult,
Message: "Expected label argument",
Location: &v,
},
})
}

return &JumpInstruction{
JumpToInstructionIndex: label.InstructionIndex,
}, core.ResultList{}
}

func NewJumpInstructionDefinition() gen.InstructionDefinition[usm64core.Instruction] {
return &FixedInstructionDefinition{
Targets: 0,
Arguments: 1,
Creator: NewJumpInstruction,
}
}
5 changes: 3 additions & 2 deletions usm64/managers/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ func (m *InstructionMap) GetInstructionDefinition(
func NewInstructionManager() gen.InstructionManager[usm64core.Instruction] {
return gen.InstructionManager[usm64core.Instruction](
&InstructionMap{
"add": usm64isa.NewAddInstructionDefinition(),
"put": usm64isa.NewPutInstructionDefinition(),
"add": usm64isa.NewAddInstructionDefinition(),
"put": usm64isa.NewPutInstructionDefinition(),
"jump": usm64isa.NewJumpInstructionDefinition(),
},
)
}

0 comments on commit d42d1be

Please sign in to comment.