glados-0.1.0.0
Safe HaskellNone
LanguageHaskell2010

VM.VMState

Synopsis

Documentation

data VMState Source #

The core state of the Virtual Machine.

@details This structure holds the entire context of the executing program at any given microsecond. It includes memory (stack, heap/globals), the processor registers (IP, FP), and the code itself.

Constructors

VMState 

Fields

  • bytecode :: ByteString

    The read-only byte array containing the compiled machine code.

  • bytecodeIndex :: Int

    The current index in bytecode. When a function is called, this value (plus offset) becomes the callbackIndex in the pushed CallSnapshot.

  • vStack :: Vector VMValue

    The main data stack. Contains inputs, outputs, and local variables.

  • baseVStackIndex :: Int

    The index in vStack where the current function's local variables begin. When a function is called, this value is saved as vStackIndex in the CallSnapshot.

  • snapshotStack :: [CallSnapshot]

    The stack of saved contexts (formerly callStack). Used to restore the state upon returning from a function.

  • env :: Vector VMValue

    The Current Environment (Closures). Contains variables captured by the currently executing function. If the current function is not a closure, this is typically empty.

  • globalEnv :: Vector VMValue

    The persistent global environment. Stores global variables accessible throughout the program life.

  • isRunning :: Bool

    Execution flag. If False, the VM loop terminates.

  • isDebug :: Bool

    Enable tracing

  • vmFds :: Map Int Handle

    Table of file descriptors (Kernel)

  • nextFd :: Int

    Next free fd

type VirtualMachine a = StateT VMState IO a Source #

The Monad Transformer stack used for the VM.

@details We use StateT to carry the VMState and IO to allow for side effects like printing to the console (PRINT instruction).

createVMState :: ByteString -> Bool -> VMState Source #

Creates the initial state of the VM for a given program.

@args - code: The compiled binary code to execute.

@return A pristine VMState ready for execution.

createSnapshot :: VMState -> CallSnapshot Source #

Creates a snapshot of the current VM context (IP, FP, Env).

@args - vm: The current VM state.

@details Captures the current Instruction Pointer, Frame Pointer, and Closure Environment. Used by CALL instructions to save the state before jumping.

@return A CallSnapshot containing the saved context.

doSnapshot :: Int -> Vector VMValue -> VirtualMachine () Source #

Sets up a new stack frame for a function call (Shared Logic).

@args - idx: The absolute address (index) to jump to. - newEnv: The closure environment to load (empty for static calls).

@details 1. Creates and pushes a snapshot of the current state. 2. Updates FP (baseVStackIndex) and IP (bytecodeIndex). 3. Sets the new environment.

@return Unit. State is modified.