RunMat
  • Pricing
RunMat
GitHub
GitHub
Sign inTry in browser
DesktopRuntimePhysicsCADAdministrationAgent

Get RunMat updates

New releases, guides and examples.

Product+
  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download
Math+
  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting
Physics+
  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA
Industries+
  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance
Learn+
  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students
Compare+
  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons
Company+
  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms

Product

  • Runtime
  • GPU
  • Parallel
  • Compile
  • Interop and MEX
  • Simulation
  • MCAD
  • ECAD
  • Enterprise
  • AI integrations
  • Pricing
  • Download

Math

  • Signal processing
  • Linear algebra
  • Statistics
  • Curve fitting
  • Differential equations
  • Control systems
  • Optimization
  • Finance
  • Data and tables
  • Plotting

Physics

  • Simulation overview
  • Structural
  • Thermal
  • Electromagnetic
  • Joule heating
  • Thermal stress
  • Acoustics
  • Fluid flow
  • Conjugate heat transfer
  • Fluid–structure
  • What is FEA

Industries

  • Aerospace
  • Automotive and transportation
  • Defense
  • Electronics and semiconductors
  • Energy
  • Industrial machinery
  • Life sciences
  • Quantitative finance

Learn

  • Guides
  • Docs
  • Function reference
  • Language reference
  • Migration guide
  • Changelog
  • MATLAB Online
  • MATLAB AI agent
  • For students

Compare

  • RunMat vs MATLAB
  • RunMat vs Ansys
  • RunMat vs SOLIDWORKS
  • RunMat vs Altium
  • RunMat vs Cadence
  • RunMat vs COMSOL
  • RunMat vs Python
  • RunMat vs Julia
  • RunMat vs Physics AI
  • RunMat vs Octave
  • Benchmarks
  • MATLAB alternatives
  • All comparisons

Company

  • About
  • Blog
  • Contact
  • Source code↗
  • License
  • Privacy
  • Terms
GitHubXLinkedIn

© Dystr, Inc. · RunMat v0.7.2, Nov 26, 2026 ·

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

Bytecode Compilation (MIR → Bytecode)

The bytecode compilation stage is the final lowering step before VM execution. It transforms the Mid-Level IR (MIR) into a linear sequence of Instr values, plus the metadata the interpreter needs for variable layout, source spans, semantic function dispatch, async execution, pure-region boundaries, and acceleration planning.

The lowering is managed by the Compiler in runmat-vm. It consumes a MirAssembly and the corresponding HirAssembly, derives a VmAssemblyLayout, emits bytecode for the entry body and semantic functions, patches jumps, and attaches fusion metadata used by the acceleration tiers.

Compiler Architecture

The compiler is a stateful instruction emitter. It tracks the instruction stream, layout, spans, call-argument spans, and deferred jump targets while walking MIR basic blocks.

Key Data Structures

  • Compiler: Orchestrates MIR-to-bytecode lowering.
  • Instr: The VM instruction enum executed by the interpreter.
  • Bytecode: The top-level program container for instructions, layout, functions, spans, async metadata, exact region-to-PC boundaries, and fusion metadata.
  • FunctionRegistry: Maps FunctionId and display names to compiled semantic function bytecode.
  • VmAssemblyLayout: Maps MIR locals and HIR bindings to VM slots.

Data Flow: MIR to Bytecode

Loading diagram...

Lowering Responsibilities

MIR Statements and Terminators

The compiler walks each MirBody by basic block. MirStmt values emit stack operations, variable stores, indexing instructions, aggregate constructors, or call instructions. MirTerminator values emit jumps, conditional branches, returns, and exception-region boundaries.

Calls

Call lowering depends on the resolved callee shape:

  • CallBuiltinMulti calls a known runtime built-in.
  • CallSemanticFunctionMulti calls a known function in the compiled semantic assembly.
  • CallFunctionMulti carries a CallableIdentity and CallableFallbackPolicy for runtime resolution.
  • CallFevalMulti keeps the callable value on the stack and resolves it at runtime.
  • *ExpandMultiOutput variants evaluate comma-separated-list expansion before dispatch.

Call arguments retain their source order. An argument marked as a member or brace expansion is evaluated once and contributes its values at that exact argument position; the sequence does not become an ordinary stack value or workspace binding.

Indexing

The compiler preserves the indexing plan established by MIR. Scalar indexing emits Index or StoreIndex; plain slices emit IndexSlice or StoreSlice; selectors involving runtime end arithmetic emit IndexSliceExpr or StoreSliceExpr.

Control Flow

MIR basic-block targets are not known as final instruction offsets until all relevant blocks are emitted. The compiler therefore records block-to-instruction mappings and patches Jump, JumpIfFalse, AndAnd, and OrOr targets after emission.

Region Boundaries

The compiler records a resume PC at every empty-stack MIR statement boundary. After MIR analysis discovers pure regions, ExecutableUnit installs each stable region contract into bytecode by resolving its exact entry and exit program points through those resume maps. The top-level bytecode table contains the complete region inventory, while each semantic function retains the regions it owns. Missing functions or boundaries are compile-product errors; the VM does not estimate a nearby PC. The portable manifest retains the same contracts for Native IR and other execution consumers.

Complete Instruction Set

The table below lists every current Instr variant, grouped by runtime behavior.

Constants, Variables, and Locals

InstructionsPurpose
LoadConst, LoadComplex, LoadBool, LoadString, LoadCharRowPush literal values onto the stack.
LoadVar, LoadVarForIndexAssignment, StoreVarRead and write workspace variable slots. LoadVarForIndexAssignment can initialize an undefined indexed-assignment base.
EnterScope, ExitScope, LoadLocal, StoreLocalManage local frame storage and local slot access.

Arithmetic, Comparison, and Logical Operations

InstructionsPurpose
Add, Sub, Mul, RightDiv, LeftDiv, PowMatrix/scalar arithmetic operations.
Neg, UPlusUnary arithmetic operations.
Transpose, ConjugateTransposeMATLAB transpose operators.
ElemMul, ElemDiv, ElemPow, ElemLeftDivElement-wise arithmetic operations.
LessEqual, Less, Greater, GreaterEqual, Equal, NotEqualComparison operations.
LogicalNot, LogicalAnd, LogicalOrEager logical operations.
AndAnd, OrOrShort-circuit logical control-flow operations.

Stack and Control Flow

InstructionsPurpose
Pop, Swap, UnpackStack manipulation and output-list expansion.
JumpIfFalse, JumpBranch to patched instruction offsets.
EnterTry, PopTryMaintain the try/catch handler stack.
Return, ReturnValueExit the current interpreter frame.
StochasticEvolutionSpecialized fast-path marker for stochastic evolution kernels.

Array and Aggregate Construction

InstructionsPurpose
CreateMatrix, CreateMatrixDynamicBuild matrix values from stack operands.
CreateRangeBuild colon/range values.
CreateCell2DBuild cell arrays.
CreateStructLiteralBuild struct literals from named field values.
CreateObjectLiteralBuild object literals with class names and field values.
PackToRow, PackToColPack stack values into row or column tensor form.

Indexing and Assignment

InstructionsPurpose
Index, StoreIndex, StoreIndexDeleteScalar or linear paren indexing and assignment.
IndexSlice, StoreSlice, StoreSliceDeleteSlice indexing with compiler-encoded colon and plain end masks.
IndexSliceExpr, StoreSliceExpr, StoreSliceExprDeleteGeneral slice indexing with dynamic ranges and EndExpr arithmetic.
IndexCell, IndexCellExpand, IndexCellListBrace/cell indexing and context-bound comma-separated-list expansion.
StoreIndexCell, StoreIndexCellDeleteBrace/cell indexed assignment and deletion form.

Indexed assignment instructions push the updated base value. A later StoreVar or StoreLocal commits that updated base to its destination slot.

Comma-Separated Sequence Assignment

MIR distinguishes a structure-member sequence destination from an ordinary single-place assignment. Lowering evaluates its base, selectors, and dynamic member name from left to right and exactly once, prepares the complete writeback path, and obtains the destination cardinality before invoking the right-hand side. That cardinality is the right-hand side's requested output count.

The resulting values do not travel as a user Value. Bytecode uses a private sequence register between a producer such as LoadMemberSequenceUsingOutputSlot, CaptureCallOutputSequence, or CaptureScalarSequence and the immediately following StoreMemberSequence or StoreMemberDynamicSequence. The prepared destination remains executor state until the consumer distributes the values and commits the updated root.

The bytecode verifier treats this as a linear lifetime. It rejects a consumer without a producer, a second producer before consumption, any unrelated instruction between the pair, a dynamic call-output capture not attached to its output-slot call, and a sequence left live at a function boundary. Exception handling clears the transient register rather than exposing it to a catch block.

Native IR carries the same contract. Its producer owns a rooted prepared destination plus the produced value references; the adjacent consumer must name the same MIR target and statement site. Native verification rejects missing, overwritten, mismatched, non-adjacent, or block-live sequence state. This state belongs to execution and continuation bookkeeping only, never to the MATLAB value model.

Struct, Object, Class, and Member Operations

InstructionsPurpose
LoadMember, LoadMemberOrInit, LoadMemberDynamic, LoadMemberDynamicOrInitRead static or dynamic members, with optional initialization semantics.
LoadMemberSequence, LoadMemberDynamicSequenceRead a member value sequence and apply its explicit scalar, prefix, dynamic-output, expansion, or discard context.
MemberSequenceCardinality, LoadMemberSequenceUsingOutputSlot, LoadMemberDynamicSequenceUsingOutputSlotDetermine a prepared sequence destination's cardinality and produce the matching number of right-hand-side values.
CaptureCallOutputSequence, CaptureScalarSequence, StoreMemberSequence, StoreMemberDynamicSequenceMove right-hand-side results through the transient sequence-assignment channel and consume them in a static or dynamic member store.
StoreMember, StoreMemberOrInit, StoreMemberDynamic, StoreMemberDynamicOrInitWrite static or dynamic members, with optional initialization semantics.
LoadMethodLoad a method reference from an object-like value.
CallMethodOrMemberIndexMulti, CallMethodOrMemberIndexExpandMultiOutputResolve ambiguous method/member indexing calls at runtime.
LoadStaticPropertyLoad a class static property.
RegisterClassRegister a lowered classdef definition at runtime.

Function Handles, Calls, and Async

InstructionsPurpose
CreateFunctionHandle, CreateExternalFunctionHandle, CreateMethodFunctionHandle, CreateBoundFunctionHandleBuild function-handle values.
CreateClosure, CreateSemanticClosureBuild closure values with captured stack operands.
CallBuiltinMulti, CallBuiltinExpandMultiOutputDispatch runtime built-ins.
CallFunctionMulti, CallFunctionExpandMultiOutputDispatch a CallableIdentity through fallback-aware callable resolution.
CallSemanticFunctionMulti, CallSemanticFunctionExpandMultiOutputDispatch directly to compiled semantic functions.
CallFevalMulti, CallFevalExpandMultiOutputDispatch a runtime callable value through feval.
CreateSemanticFuture, CreateSemanticFutureExpandMultiOutputBuild lazy semantic-future descriptors.
Spawn, AwaitExplicit async spawn and await boundaries.

Imports, Globals, Persistents, and Visible Output

InstructionsPurpose
RegisterImportRegister an import path for later unqualified callable resolution.
DeclareGlobal, DeclareGlobalNamedDeclare global slots, including name-stable forms across units.
DeclarePersistent, DeclarePersistentNamedDeclare persistent slots, including name-stable forms across units.
EmitStackTop, EmitVarEmit visible workspace output labels such as ans or a variable name.

Supporting Operand Types

EndExpr

EndExpr represents selector expressions that depend on the runtime size of the indexed value. It supports:

  • Base values: End, Const, Var.
  • Callable expressions: ResolvedCall.
  • Arithmetic: Add, Sub, Mul, Div, LeftDiv, Pow.
  • Unary and rounding operations: Neg, Pos, Floor, Ceil, Round, Fix.

ArgSpec

ArgSpec describes multi-output argument expansion for call instructions. It records whether an argument is expanded, how many indices are consumed for a brace/object expansion, and whether the expansion consumes all contents.

Stack Effects

Most instructions have a static StackEffect reported by Instr::stack_effect(). The effect is used by metadata and acceleration planning:

  • Literal loads, variable loads, function-handle creation, and local loads push one value.
  • Binary arithmetic and comparisons pop two values and push one.
  • Calls pop their arguments and push the requested result shape.
  • Indexed writes pop the base, selectors, and RHS, then push the updated base.
  • StochasticEvolution has no ordinary static stack effect because it is handled as a specialized fast path.

IndexSliceExpr and the expanded call variants also carry structured operand metadata. Their exact runtime pops depend on the encoded range or expansion specs, while the bytecode still records enough static information for planning and validation.

System Entity Mapping

Loading diagram...

Acceleration and Fusion Metadata

The compiler generates FusionMetadata to assist the GPU offload engine and runtime planner.

Metadata EntityPurpose
FusionCandidateGroupsIdentifies sequences of MIR operations that are valid candidates for fusion.
InstructionWindowsMaps emitted instruction ranges back to fusion candidates.
AccelGraphDescribes bytecode data flow for residency planning and CPU/GPU transfer minimization.

Error Handling and Validation

The compiler validates MIR and HIR contracts before emitting bytecode. Stable error identifiers include:

  • RunMat:MirScalarIndexPlanInvalid: A scalar indexing plan cannot be lowered safely.
  • RunMat:MirSliceIndexPlanInvalid: A slice or end-relative plan is malformed.
  • RunMat:MirCellIndexPlanInvalid: A cell indexing plan is malformed.
  • RunMat:MirFunctionHandleNameMissing: A function handle target lacks a usable name.
  • RunMat:ImportAmbiguous: Import resolution produced conflicting candidates.

Bytecode, MIR, analysis, compiler, and Native IR schemas are versioned with these contracts. Artifacts produced before the typed sequence instructions and verifier rules are not reinterpreted under the new semantics: execution admission rejects the stale form or component revisions and asks the caller to rebuild with the current RunMat version. This guarantee currently covers compiler, VM, and native executable admission; it does not by itself claim closure of browser, foreign-runtime, or general value-transport versioning.

From here, bytecode execution continues in Interpreter Dispatch & Execution Loop.

On this page
  • Compiler Architecture
  • Key Data Structures
  • Data Flow: MIR to Bytecode
  • Lowering Responsibilities
  • MIR Statements and Terminators
  • Calls
  • Indexing
  • Control Flow
  • Region Boundaries
  • Complete Instruction Set
  • Constants, Variables, and Locals
  • Arithmetic, Comparison, and Logical Operations
  • Stack and Control Flow
  • Array and Aggregate Construction
  • Indexing and Assignment
  • Comma-Separated Sequence Assignment
  • Struct, Object, Class, and Member Operations
  • Function Handles, Calls, and Async
  • Imports, Globals, Persistents, and Visible Output
  • Supporting Operand Types
  • EndExpr
  • ArgSpec
  • Stack Effects
  • System Entity Mapping
  • Acceleration and Fusion Metadata
  • Error Handling and Validation