Skip to content

Commit

Permalink
Refactored VM constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Oct 26, 2024
1 parent a627598 commit c2c11c4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
4 changes: 2 additions & 2 deletions pkg/compiler/compiler_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ type UseCase struct {
}

func Run(p *runtime.Program, opts ...runtime.EnvironmentOption) ([]byte, error) {
vm := runtime.NewVM(opts...)
vm := runtime.NewVM(p)

return vm.Run(context.Background(), p)
return vm.Run(context.Background(), opts...)
}

func Exec(p *runtime.Program, raw bool, opts ...runtime.EnvironmentOption) (any, error) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ func TestUnaryOperators(t *testing.T) {
func TestEqualityOperators(t *testing.T) {
Convey("Equality operators", t, func() {
run := func(p *runtime.Program) (string, error) {
vm := runtime.NewVM()
vm := runtime.NewVM(p)

out, err := vm.Run(context.Background(), p)
out, err := vm.Run(context.Background())

return string(out), err
}
Expand Down
42 changes: 22 additions & 20 deletions pkg/runtime/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@ import (

type VM struct {
env *Environment
program *Program
globals map[string]core.Value
frames []*Frame
currentFrame *Frame
pc int
}

func NewVM(opts ...EnvironmentOption) *VM {
func NewVM(program *Program) *VM {
vm := new(VM)
vm.env = newEnvironment(opts)
vm.program = program

return vm
}

// TODO: Move program to the constructor. No need to pass it as an argument since the VM is stateful.
// But the environment can be passed as an argument.
func (vm *VM) Run(ctx context.Context, program *Program) ([]byte, error) {
func (vm *VM) Run(ctx context.Context, opts ...EnvironmentOption) ([]byte, error) {
//tryCatch := func(pos int) bool {
// for _, pair := range program.CatchTable {
// if pos >= pair[0] && pos <= pair[1] {
Expand All @@ -34,18 +33,12 @@ func (vm *VM) Run(ctx context.Context, program *Program) ([]byte, error) {
// return false
//}

loadData := func(op Operand) core.Value {
if op.IsRegister() {
return vm.currentFrame.registers[op.Register()]
}

return program.Constants[op.Constant()]
}

vm.env = newEnvironment(opts)
vm.currentFrame = newFrame(64, 0, nil)
vm.frames = make([]*Frame, 4)
vm.globals = make(map[string]core.Value)
vm.pc = 0
program := vm.program

loop:
for vm.pc < len(program.Bytecode) {
Expand All @@ -56,23 +49,23 @@ loop:

switch inst.Opcode {
case OpMove:
reg[dst] = loadData(src1)
reg[dst] = vm.load(src1)
case OpLoadConst:
reg[dst] = program.Constants[src1.Constant()]
case OpStoreGlobal:
vm.globals[program.Constants[dst.Constant()].String()] = loadData(src1)
vm.globals[program.Constants[dst.Constant()].String()] = vm.load(src1)
case OpLoadGlobal:
reg[dst] = vm.globals[program.Constants[src1].String()]
case OpAdd:
reg[dst] = operators.Add(loadData(src1), loadData(src2))
reg[dst] = operators.Add(vm.load(src1), vm.load(src2))
case OpSub:
reg[dst] = operators.Subtract(loadData(src1), loadData(src2))
reg[dst] = operators.Subtract(vm.load(src1), vm.load(src2))
case OpMulti:
reg[dst] = operators.Multiply(loadData(src1), loadData(src2))
reg[dst] = operators.Multiply(vm.load(src1), vm.load(src2))
case OpDiv:
reg[dst] = operators.Divide(loadData(src1), loadData(src2))
reg[dst] = operators.Divide(vm.load(src1), vm.load(src2))
case OpMod:
reg[dst] = operators.Modulus(loadData(src1), loadData(src2))
reg[dst] = operators.Modulus(vm.load(src1), vm.load(src2))
case OpIncr:
reg[dst] = operators.Increment(reg[dst])
case OpDecr:
Expand Down Expand Up @@ -451,3 +444,12 @@ loop:

return vm.currentFrame.registers[ResultOperand].MarshalJSON()
}

//go:inline
func (vm *VM) load(op Operand) core.Value {
if op.IsRegister() {
return vm.currentFrame.registers[op.Register()]
}

return vm.program.Constants[op.Constant()]
}

0 comments on commit c2c11c4

Please sign in to comment.