vm

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 14, 2021 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const GlobalsSize = 65536 // upper limit on the number of global bindings since operands are 16 bits wide.
View Source
const MaxFrames = 1024 // arbitrary number
View Source
const StackSize = 2048 // arbitrary number

Variables

View Source
var False = &object.Boolean{Value: false}
View Source
var Null = &object.Null{}
View Source
var True = &object.Boolean{Value: true}

Functions

This section is empty.

Types

type Frame

type Frame struct {
	// contains filtered or unexported fields
}

Frame is the struct that holds the execution-relevant information for a function. It is effectively like the inner-environment of a function, allowing the VM to execute its instructions and update the ip (instruction-pointer) without entangling them with the outer scopes. cl points to the closure referenced by the frame. ip is the instruction pointer in this frame for this function. basePointer is a stack pointer value to indicate where in the stack to start allocating memory when executing the function, it is used to create a "hole" on the stack to store all the local bindings of the function. Below the "hole" is the lower boundary which contains all the values on the stack before calling the function. If sp is 3 before calling the function, the lower boundary contains indices [0, 1, 2]. The "hole" will be n-length deep where n is the number of local-bindings. Above the "hole" is the function's workspace, where it will push and pop values, if sp is 3 it should use the indices that are greater than 3+n. When the function exits, we can restore the stack, removing all values after the initial basePointer, thus giving us the stack before the function was called.

func NewFrame

func NewFrame(cl *object.Closure, basePointer int) *Frame

NewFrame creates a new frame for the given compiled function

func (*Frame) Instructions

func (f *Frame) Instructions() code.Instructions

Instructions simply returns the instructions of the function enclosed by the Closure

type VM

type VM struct {
	// contains filtered or unexported fields
}

VM is the struct for our virtual-machine. It holds the bytecode instructions and constants-pool generated by the compiler. A VM implements a stack, as it executes the bytecode, it organizes (push, pop, etc) the evaluated constants on the stack. The field sp helps keep track of the position of the next item in the stack (top to bottom).

func New

func New(bytecode *compiler.Bytecode) *VM

New initializes a new VM using the bytecode generated by the compiler. VMs are initialized with an sp of 0 (the initial top). The stack will have a preallocated number of elements (StackSize).

func NewWithGlobalStore

func NewWithGlobalStore(bytecode *compiler.Bytecode, s []object.Object) *VM

NewWithGlobalStore keeps global state in the REPL so the VM can execute with the byteode and global store from a previous compilation.

func (*VM) LastPoppedStackElem

func (vm *VM) LastPoppedStackElem() object.Object

LastPoppedStackElem helps identify the last element that was popped from the stack as the VM executes through it. If a stack had two elements [a, b], sp would be at index 2. If the vm pops an element, it would pop the element at [sp-1], so index 1, and then sp is moved to index 1. Leaving b to be the last popped stack element.

func (*VM) Run

func (vm *VM) Run() error

Run will start the VM. The VM will execute the bytecode and handle the specific instructions (opcode + operands) that it was provided from the compiler. It executes the fetch-decode-execute cycle.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL