RunMat
  • Pricing
RunMat
GitHub
GitHub
DownloadSign InTry in Browser
DesktopRuntimeServer
RunMat

Run math blazing fast

GitHubX (Twitter)LinkedIn

Company

  • About
  • Pricing
  • Contact
  • License
  • Privacy

Learn

  • Docs
  • Blog
  • Benchmarks
  • RunMat vs MATLAB Online

Get product updates and release notes from the RunMat team.

© 2026 Dystr · Made withfor the scientific community.

RunMat™ is a registered trademark of Dystr, Inc. MATLAB® is a registered trademark of The MathWorks, Inc. RunMat is not affiliated with, endorsed by, or sponsored by The MathWorks, Inc.

/
    • Virtual Machine (VM)
    • Bytecode Compilation
    • Interpreter Dispatch & Execution Loop
    • Indexing Subsystem
    • Callable Resolution & Function Dispatch
    • JIT Compiler
    • JIT Pipeline
    • Memory Management
Docs>Runtime>Execution Engines

Interpreter Dispatch & Execution Loop

The RunMat interpreter executes the bytecode emitted by the VM compiler. It is an async instruction loop over Instr values, with runtime state held in InterpreterState and passed into the dispatch layer as a mutable DispatchState. The loop owns the program counter, value stack, variable slots, try/catch stack, semantic function resolver hooks, and optional native-acceleration fusion plan.

Runtime State

InterpreterState is created from a Bytecode program and the caller's initial variable slots. Before execution begins, the interpreter installs semantic function hooks, prepares workspace state, initializes GC roots, and activates a fusion plan when the native-accel feature is enabled.

Key Runtime Fields

FieldPurpose
bytecodeThe instruction stream, variable metadata, function registry, and fusion metadata.
stackTemporary operand stack used by bytecode instructions.
varsWorkspace or frame-local variable slots addressed by LoadVar, StoreVar, and local fallbacks.
contextCall stack, local frame storage, instruction pointer metadata, and async task tracking.
pcProgram counter into bytecode.instructions.
try_stackStack of scoped catch targets used by try/catch bytecode.
last_exceptionLast caught exception value, used by exception-sensitive built-ins such as rethrow.

Exact native continuation

The compiler retains a portable MIR-program-point to bytecode-PC map for boundaries where the VM operand stack is empty. When native execution exits at one of those verified boundaries, Core can resume this same interpreter loop with materialized variable assignment state, global and persistent aliases, omitted inputs, nargin and nargout, imports, source context, and the exact next pc. The bytecode before that boundary is not executed again. If the native frame contains state the interpreter cannot represent exactly, execution stays in the generic-native continuation instead.

Execution Loop

The main loop in run_interpreter_inner repeatedly fetches the instruction at pc, offers eligible spans to the fusion executor, dispatches the instruction, and then applies the resulting control-flow decision.

Loading diagram...

Instruction Dispatch

dispatch_instruction receives three grouped inputs:

  • DispatchMeta: the current instruction, function registry, source metadata, call spans, and call-count context.
  • DispatchState: mutable stack, variables, context, try stack, imports, aliases, persistent state, missing-input slots, and pc.
  • DispatchHooks: callbacks used by the runner for residency cleanup and workspace or persistent-variable synchronization.

The dispatcher first gives broad instruction families to specialized modules. Indexing, object, and arithmetic handlers return true when they consume the instruction. The remaining bytecode variants are handled by the central match in dispatch/mod.rs.

Loading diagram...

Dispatch Categories

CategoryRepresentative InstructionsPrimary Handler
IndexingIndex, StoreIndex, IndexSlice, StoreSliceExprinterpreter/dispatch/indexing.rs
Arithmetic and logical opsAdd, Sub, Mul, comparisons, boolean opsinterpreter/dispatch/arithmetic.rs
Object supportObject literals and object operationsinterpreter/dispatch/object.rs
Variables and stackLoadVar, StoreVar, LoadLocal, Pop, Swapinterpreter/dispatch/mod.rs
Control flowJump, JumpIfFalse, EnterTry, Returnops/control_flow.rs
CallsBuilt-ins, semantic functions, feval, multi-output callsinterpreter/dispatch/calls.rs and call/*
AsyncSemantic futures, Spawn, Awaitinterpreter/dispatch/mod.rs

Indexed assignment instructions push the updated base value back onto the stack. Follow-up bytecode is responsible for storing that value into the target variable or local slot with StoreVar or StoreLocal.

Exception Routing

try/catch is represented directly in bytecode. EnterTry pushes a compiler-assigned scope and catch target onto try_stack. MIR control-flow analysis identifies the complete try-only region, including nested branches, loops, and await continuations. LeaveTry removes that exact scope on each normal edge out of the protected region; it does not accidentally remove an enclosing handler or require the try body to fit in one basic block. If a handler returns RuntimeError, the runner calls redirect_exception_to_catch.

When a catch target exists, the VM:

  • Pops the innermost active catch entry while retaining enclosing handlers.
  • Converts the runtime error into Runtime's canonical MException.
  • Stores that exception into the optional catch variable slot.
  • Updates last_exception.
  • Sets pc to the catch handler.

If no catch entry exists, the error leaves the interpreter.

Semantic Function Hooks

Before entering the loop, the interpreter installs thread-local semantic invoker and resolver hooks backed by the bytecode FunctionRegistry. This lets the runtime call back into bytecode-defined functions from several paths: direct semantic calls, closures, feval, object dispatch, and end-expression calls inside indexing.

Those hooks are scoped by guards. Bound semantic calls depend on them being active; without the interpreter-installed invoker, a semantic function ID cannot be executed by the runtime layer.

From here, call-specific behavior is covered in Callable Resolution & Function Dispatch, and indexing-specific behavior is covered in Indexing Subsystem.

On this page
  • Runtime State
  • Key Runtime Fields
  • Exact native continuation
  • Execution Loop
  • Instruction Dispatch
  • Dispatch Categories
  • Exception Routing
  • Semantic Function Hooks