4.1 Interpreter vs Runtime vs VM vs Compiler

INTERPRETERSTATUS: STABLELEVEL: INTROREVIEWED: 2026-11-18
Before this page How R Executes Code Today

Modifying a language runtime, or building a new one, means deciding which part owns each job: the compiler, the interpreter, the virtual machine, or the runtime around them. The four terms overlap in everyday use, but each names a different part of how code executes.

4.1.1 Mental model

Think of a program’s journey from text to behavior. A compiler transforms it once, before any run. An interpreter reads and executes it during the run. A runtime provides services that the running program needs. A virtual machine is an execution engine for an instruction set no hardware implements. All four concepts can appear in one system, and usually do.

flowchart TD
    src["source text"] -->|parser| ast["AST"]
    ast --> aot["AOT compiler"]
    ast --> interp["AST interpreter<br/>(R, Ruby 1.8)"]
    ast --> bcc["bytecode compiler"]
    aot --> mach["machine code<br/>(C, Rust)"]
    bcc --> vm["bytecode VM<br/>(CPython, R, JVM)"]
    vm --> jit["JIT compiler<br/>(JVM, V8)"]

    mach --> hw["hardware execution"]
    interp --> hw
    vm --> hw
    jit --> hw

Compilation and interpretation strategies. After parsing, a program can be compiled ahead of time to machine code, interpreted as an AST, or compiled to bytecode for a VM, which may hand hot code to a JIT compiler.

4.1.2 Compiler

A compiler translates a program from one form to another before execution starts. The input is typically source text. The output can be machine code, bytecode, AST, another language, or any intermediate form.

The C compiler reads .c files and writes machine code directly runnable on hardware. The TypeScript compiler reads .ts files and writes JavaScript. The Rust compiler reads .rs files and writes machine code. Compilers work ahead of time (AOT). You run them once, get a binary, and run that binary many times.

Compilation takes time. In return, the program runs fast and needs no translation during execution. The compiled form can be optimized heavily because the compiler has time to analyze the code before it ever runs, which is a large part of why C and Rust programs are fast.

4.1.3 Interpreter

An interpreter executes a program directly from a textual or structural representation. The input is source code, an AST, or bytecode. The interpreter reads this representation one step at a time and runs the corresponding action.

An interpreter does little translation before it starts running, and how little varies. CPython compiles a whole module to bytecode first, then interprets the bytecode. Rscript reads a script one top-level expression at a time, parsing and evaluating each before reading the next, so a syntax error near the end stops the script only after the earlier lines have run. source() parses the whole file first, so the same error stops it before anything runs.

Interpretation is slower than running compiled code because the interpreter must decode what to do on every execution. A loop that runs one thousand times decodes the same instructions one thousand times. Compilers decode once and optimize the result.

Interpreters are flexible. You can modify code, reload it, and run it without recompiling. You can introspect a running program. You can execute strings as code. These properties make interpreters useful for interactive work and scripting.

4.1.4 Virtual machine

A virtual machine (VM) is an execution engine for an instruction set that no actual hardware implements. The instruction set is designed for the machine, not for humans to write directly. A compiler or interpreter translates human code to VM instructions. The VM then executes these instructions.

Stack-based VMs are simpler to build and understand. The machine maintains a stack of values. Instructions push values onto the stack, pop them off, and perform operations. Almost all arithmetic and logic work takes values from the top of the stack. A simple instruction like ADD pops two values, adds them, and pushes the result. The JVM, .NET, and Python bytecode VM use stack-based designs.

Stack-based machines need many instructions to describe a computation. The instruction stream grows long. Instruction dispatch (fetching and decoding each instruction) takes time. But the stack acts as implicit memory. You do not need explicit register names. Code generation is straightforward.

Register-based VMs maintain named storage slots called registers, like hardware CPUs do. An instruction ADD r1, r2, r3 adds registers r2 and r3, storing the result in r1. Register machines need fewer instructions for the same work. Instruction dispatch happens less often. Memory access patterns can be better. But code generation is harder. You must allocate values to specific registers and avoid register conflicts. The Lua VM and the BEAM VM (for Erlang and Elixir) use register-based designs.

4.1.5 Runtime system

A runtime system is everything a program needs to run beyond its own code. It is not the interpreter or compiler. It is the services the program depends on.

In a garbage-collected language, the runtime provides memory allocation and garbage collection. (C’s runtime library provides allocation only.) A running program allocates objects on the heap. When objects are no longer needed, the GC frees their memory. The allocator decides where new objects land. The GC decides what to reclaim.

A runtime also provides type machinery. When a program calls a function, the runtime checks that arguments have the right types (or coerces them). When a program accesses a field of an object, the runtime finds the field’s location in memory. When a program adds two values, the runtime knows whether to call integer addition or floating-point addition.

A runtime handles errors. When a division by zero occurs, the runtime decides whether to raise an exception, return infinity, or crash. When a file operation fails, the runtime packages the error into an object the program can catch.

A runtime manages concurrency if the language supports it. This means creating and scheduling threads and providing locks. A runtime can also provide a scheduler for coroutines or async-await.

A runtime enables foreign function interfaces (FFI). When R code calls C code, the runtime finds the C symbol in a loaded library, passes the arguments as SEXP pointers or converted arrays, makes the call through the platform’s calling convention, and turns the result back into an R object. FFI is complex because different languages use different data layouts and calling conventions.

flowchart TD
    prog["running program"]
    prog --> mem["allocator and<br/>garbage collector"]
    prog --> types["type<br/>machinery"]
    prog --> ffi["FFI"]
    prog --> err["error<br/>handling"]
    prog --> sched["scheduler<br/>(if concurrent)"]

    mem --> heap[("heap")]

Runtime system structure. A runtime wraps the program and provides essential services independent of the interpreter or compiler.

4.1.6 Where terms overlap

Most modern language implementations use all four concepts together. The boundaries between them blur.

CPython compiles Python source to bytecode when it imports a module, and caches the result in .pyc files. The bytecode is a low-level form (the dis module prints it). CPython then interprets this bytecode inside a runtime. The runtime allocates heap memory, runs the garbage collector, handles exceptions, and manages type checks. CPython is an interpreter because it executes bytecode step-by-step. It is also compiled because it translates to bytecode first. It lives inside a runtime that provides GC, memory, and type services.

The Java Virtual Machine (JVM) runs bytecode. Java code compiles to bytecode, which HotSpot, the standard JVM, starts by interpreting. HotSpot also contains just-in-time (JIT) compilers. When it sees a method execute many times, it compiles that bytecode to machine code on the fly. The JVM is interpreter plus JIT plus runtime. The runtime provides heap memory, GC, thread management, and type safety.

V8, the JavaScript engine in Chrome, has four tiers. Ignition interprets bytecode and records type feedback. Sparkplug compiles bytecode straight to machine code without optimizing it. Maglev and TurboFan are optimizing compilers: Maglev compiles quickly, and TurboFan spends longer for peak speed. Both make guesses about types from the feedback. When a guess fails, the optimized code deoptimizes: V8 rebuilds the interpreter’s frame and the call continues in Ignition. V8 is interpreter plus JIT tiers plus feedback-directed optimization plus runtime.

Julia compiles each method to machine code through LLVM the first time it is called with a new combination of argument types. It does not wait for code to become hot, so the cost shows up as a delay on first calls, and the payoff is fast numerical code. Julia also has an interpreter, and a heuristic in its top-level evaluator decides whether to compile a top-level expression or interpret it directly.

R parses source to an AST, and the R interpreter can walk that tree directly. R has also shipped a byte-code compiler since R 2.13.0, and since R 3.4.0 its JIT has been on by default: most closures are compiled to bytecode on their first or second call, and top-level loops are compiled before they run. Packages have been byte-compiled at install time since R 3.5.0. In a default session, then, most function bodies run as bytecode. Builtin functions are C code and need no compiling. The tree-walker still runs top-level code outside loops, and anything the compiler declines.

R’s byte-code compiler, the compiler package, translates closures to bytecode, and bcEval in eval.c interprets it on a stack-based VM. compiler::enableJIT(-1) reports the current JIT level, which is 3 by default. R’s documentation calls this JIT compilation, but its output is bytecode. GNU R has no compiler to machine code.

4.1.7 Just-in-time compilation

Just-in-time (JIT) compilation happens during program execution. The system interprets code normally. When a function or loop executes often, the JIT compiler translates it to machine code. The next execution runs the compiled code instead of interpreting.

JIT has a cycle time. Interpretation is slow but fast to start. Compilation takes time, so a tiered JIT waits until a function has run often enough that compiling it is likely to pay back. Each system sets its own thresholds, counting calls and loop iterations. The delay keeps the JIT from compiling code that runs once.

JIT can specialize based on runtime facts. The interpreter sees that a variable always holds integers. The JIT compiles code assuming integers, skipping type checks. If the assumption fails, the code deoptimizes: the running call moves back to the interpreter, and the compiled code is usually thrown away. When guards fail often, deoptimization becomes expensive. The JIT must balance speculation with caution.

Dynamic languages gain the most from a JIT, because their interpreters spend so much time checking types that a specializing compiler can remove. The major JavaScript engines all ship one. Python mostly runs without one: PyPy has a tracing JIT, and CPython added an experimental JIT in 3.13 that is off by default. The JIT lets the runtime adapt to the data it actually sees, instead of guessing at compile time.

4.1.8 Relevance to my work

threaded-r modifies GNU R’s existing interpreter and runtime. It does not change the byte-code compiler’s algorithm or the tree-walker’s design, but it holds the JIT state per interpreter and serializes byte-code construction behind one process-wide lock. It moves interpreter state into a per-interpreter structure, so each interpreter has its own heap, symbol table, string cache, and global state, and each is owned by one thread. The bytecode VM and tree-walker remain sequential within each interpreter. The result is that R code can run in parallel in several interpreters inside one process. The threaded-r page records what has been verified and what is still open.

threaded-r patches GNU R instead of rewriting it. Native packages then run against R’s own implementation of the C API, unchanged at source level; they do need rebuilding against threaded-r’s headers.

r-in-rust (rrs) is an R interpreter written from scratch in Rust. It parses R source to an AST and interprets it. It does not use R’s bytecode VM currently. Each interpreter instance owns its own heap, garbage collector, symbol table, and string cache. The interpreter crates have no global mutable state, and a gate check enforces that. Only the C API crates keep a short, reviewed allow list. Rust itself has no garbage collector, so rrs implements its own: a precise mark-sweep collector per interpreter.

rrs can add a bytecode VM and JIT as performance optimization layers without breaking the interpreter foundation. The VM is a separate execution path, not a replacement for the tree-walker.

4.1.9 Comparison across systems

Aspect R CPython JVM V8 Julia
Unit of execution Bytecode instruction, or AST node Bytecode instruction Bytecode instruction Bytecode instruction Machine code
Compilation strategy Parse to AST, compile closures to bytecode Parse to AST, compile to bytecode Compile to bytecode ahead of time Parse to bytecode Compile each method on first call
Interpreter Bytecode VM plus tree-walker Bytecode interpreter Bytecode interpreter Bytecode interpreter (Ignition) Used for some top-level code
JIT To bytecode only (enableJIT), no machine code PyPy has one; CPython’s is experimental, off by default Tiered (C1, C2) Tiered (Sparkplug, Maglev, TurboFan) Every method, through LLVM
Garbage collection Generational mark and sweep, non-moving Reference counting plus cycle detection Generational, compacting (G1 by default) Generational: copying young generation, mark-compact old Generational mark and sweep, non-moving
Runtime services Allocator, GC, type dispatch, FFI, environments Allocator, GC, type checks, FFI, module system Allocator, GC, class loading, thread scheduling Allocator, GC, microtask queue for promises Allocator, GC, type inference, FFI

Further reading

Where this appears

  • threaded-r Uses GNU R’s interpreter and runtime, with their state split per interpreter.
  • r-in-rust (rrs) Implements a new AST interpreter and runtime. A bytecode VM is a possible later layer.

Where to go next

Back to top