Skip to content

Commit

Permalink
Added Bz instruction to usm64
Browse files Browse the repository at this point in the history
  • Loading branch information
RealA10N committed Dec 14, 2024
1 parent 574c8a7 commit 2a803fc
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 21 deletions.
20 changes: 17 additions & 3 deletions usm64/core/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ type ValuedArgument interface {
Value(ctx *EmulationContext) uint64
}

func ArgumentToValuedArgument(
arg Argument,
) (ValuedArgument, core.ResultList) {
func ArgumentToValuedArgument(arg Argument) (ValuedArgument, core.ResultList) {
valued, ok := arg.(ValuedArgument)
if !ok {
v := arg.Declaration()
Expand All @@ -34,6 +32,22 @@ func ArgumentToValuedArgument(
return valued, core.ResultList{}
}

func ArgumentToLabel(argument Argument) (Label, core.ResultList) {
label, ok := argument.(Label)
if !ok {
v := argument.Declaration()
return Label{}, list.FromSingle(core.Result{
{
Type: core.ErrorResult,
Message: "Expected label argument",
Location: &v,
},
})
}

return label, core.ResultList{}
}

func NewArgument(argument gen.ArgumentInfo) (Argument, core.ResultList) {
switch typedArgument := argument.(type) {
case *gen.ImmediateInfo:
Expand Down
50 changes: 50 additions & 0 deletions usm64/isa/bz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package usm64isa

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

type BzInstruction struct {
Argument usm64core.ValuedArgument
Label usm64core.Label
}

func (i *BzInstruction) Emulate(
ctx *usm64core.EmulationContext,
) usm64core.EmulationError {
if i.Argument.Value(ctx) == uint64(0) {
ctx.JumpToLabel(i.Label)
} else {
ctx.IncrementInstructionPointer()
}
return nil
}

func NewBzInstruction(
targets []usm64core.Register,
arguments []usm64core.Argument,
) (usm64core.Instruction, core.ResultList) {
results := core.ResultList{}

argument, argumentResults := usm64core.ArgumentToValuedArgument(arguments[0])
results.Extend(&argumentResults)

label, labelResults := usm64core.ArgumentToLabel(arguments[1])
results.Extend(&labelResults)

if !results.IsEmpty() {
return nil, results
}

return &BzInstruction{Argument: argument, Label: label}, core.ResultList{}
}

func NewBzInstructionDefinition() gen.InstructionDefinition[usm64core.Instruction] {
return &FixedInstructionDefinition{
Targets: 0,
Arguments: 2,
Creator: NewBzInstruction,
}
}
17 changes: 4 additions & 13 deletions usm64/isa/jump.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package usm64isa

import (
"alon.kr/x/list"
"alon.kr/x/usm/core"
"alon.kr/x/usm/gen"
usm64core "alon.kr/x/usm/usm64/core"
Expand All @@ -20,19 +19,11 @@ func (i *JumpInstruction) Emulate(

func NewJumpInstruction(
targets []usm64core.Register,
argument []usm64core.Argument,
arguments []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,
},
})
label, results := usm64core.ArgumentToLabel(arguments[0])
if !results.IsEmpty() {
return nil, results
}

return &JumpInstruction{Label: label}, core.ResultList{}
Expand Down
4 changes: 2 additions & 2 deletions usm64/isa/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ func (i *PutInstruction) Emulate(

func NewPutInstruction(
targets []usm64core.Register,
argument []usm64core.Argument,
arguments []usm64core.Argument,
) (usm64core.Instruction, core.ResultList) {
valued, results := usm64core.ArgumentToValuedArgument(argument[0])
valued, results := usm64core.ArgumentToValuedArgument(arguments[0])
if !results.IsEmpty() {
return nil, results
}
Expand Down
17 changes: 14 additions & 3 deletions usm64/managers/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (m *InstructionMap) GetInstructionDefinition(
return nil, list.FromSingle(core.Result{{
Type: core.ErrorResult,
Message: "Undefined instruction",
// TODO: add location
}})
}
return instDef, core.ResultList{}
Expand All @@ -30,10 +31,20 @@ func (m *InstructionMap) GetInstructionDefinition(
func NewInstructionManager() gen.InstructionManager[usm64core.Instruction] {
return gen.InstructionManager[usm64core.Instruction](
&InstructionMap{
"": usm64isa.NewMovInstructionDefinition(),
"add": usm64isa.NewAddInstructionDefinition(),
"put": usm64isa.NewPutInstructionDefinition(),

// mov
"": usm64isa.NewMovInstructionDefinition(),
"mov": usm64isa.NewMovInstructionDefinition(),

// arithmetic
"add": usm64isa.NewAddInstructionDefinition(),

// control flow
"jump": usm64isa.NewJumpInstructionDefinition(),
"bz": usm64isa.NewBzInstructionDefinition(),

// debug
"put": usm64isa.NewPutInstructionDefinition(),
},
)
}

0 comments on commit 2a803fc

Please sign in to comment.