Architecture

Metorex reads a program in four stages: a lexer turns the text into tokens, a parser turns the tokens into a syntax tree, a compiler turns each method, block, class body, and file of the tree into bytecode the first time it runs, and an interpreter runs those instructions. A unit that runs often is compiled again, to machine code. Each path below is in the src/ directory of the metorex repository.

How Metorex runs a program Source text goes from the command line to the lexer, the parser, the compiler, and the interpreter. The parser asks Prism whether MRI would refuse the program, and code as objects hands the syntax tree to the program. The interpreter runs hot units through the JIT and takes them back when an assumption breaks. It calls on method calls, core methods, values, and loading, and loading sends required files back to the lexer. source text tokens syntax tree units, compiled on first run hot units deoptimize required files Starting a runmain.rs · bin_support · repl.rs Lexersrc/lexer Parsersrc/parser → src/ast Prism syntax checkvendor/prism · prism_parsing.rs Code as objectsast_methods.rsMethod#body, parse Compilersrc/bytecode · src/vm/units.rs InterpreterVirtualMachine, src/vm/core.rsbytecode_runner.rs JITsrc/vm/jitarm64 and x86-64 code Loadingsrc/vm/loadingstdlib · capi Method callsmethod_lookup.rsmethod_invocation.rs Core methodsnative_methods, Rustprelude, Ruby Valuessrc/objectsrc/class How Metorex runs a program source text tokens syntax tree units, on first run hot units deoptimize required files Starting a runmain.rs · bin_support · repl.rs Lexersrc/lexer Parsersrc/parser → src/ast Prism checkvendor/prismprism_parsing.rs Code as objectsast_methods.rsMethod#body, parse Compilersrc/bytecode · src/vm/units.rs InterpreterVirtualMachine, src/vm/core.rsbytecode_runner.rs JITsrc/vm/jit · arm64 and x86-64 Loadingsrc/vm/loadingstdlib · capi Method callsmethod_lookup.rsmethod_invocation.rs Core methodsnative_methodsprelude Valuessrc/objectsrc/class

Starting a Run#

src/main.rs reads the command line with the Cli struct in src/bin_support/options.rs, folds in RUBYOPT, and picks what runs: metorex spec, the subcommands in src/bin_support/subcommands.rs, a file, -e code, standard input, or the REPL in src/repl.rs. src/bin_support/startup.rs applies the flags to the interpreter before the program runs.

Lexing#

src/lexer turns source text into tokens, split into files by the kind of token they read: identifiers.rs for names and keywords, numbers.rs, strings.rs for quoted strings and interpolation, heredoc.rs, regex_lit.rs, and percent.rs for %w, %r, and the other percent literals. dispatch.rs picks among them by the next character. The token types are in token.rs.

Parsing#

src/parser reads the tokens into the syntax tree defined in src/ast, with statements in parser/statements and expressions in parser/expressions. The tree's node types are in src/ast/node: statements.rs, expressions.rs, parameters.rs, and patterns.rs for pattern matching.

Prism, MRI's parser, is compiled into the binary from vendor/prism by build.rs. Before a program runs, src/vm/native_functions/prism_parsing.rs asks Prism whether MRI would refuse it, and when it would, the program does not run and the SyntaxError report is laid out the way MRI lays it out. The same functions back require "prism".

Compiling#

src/bytecode/compiler.rs compiles a body of the syntax tree to a unit, defined in src/bytecode/unit.rs: its instructions, the source position of each, and the tables of constants, names, locals, and call sites they name. The instructions, 4 bytes each, are the Instruction enum in src/bytecode/instruction.rs, and most of them name registers. src/vm/units.rs compiles a method, block, class or module body, or file the first time it runs and keeps the unit with it. Code handed to eval as a String is compiled each time it runs.

METOREX_DISASM=1 writes each unit to standard error as it is compiled, and RubyVM::InstructionSequence#disasm lists the instructions of a program or method.

Running#

The interpreter is the VirtualMachine struct in src/vm/core.rs. A new one registers the core classes and their methods through src/vm/init, and loads the core methods written in Ruby from src/vm/prelude.

src/vm/jit compiles a unit that has run often enough to machine code for arm64 or x86-64. The machine code calls the interpreter's step for each instruction, except these, which it runs itself: jumps, integer arithmetic and comparisons while their operands have only been Integers, instance variable reads and writes, and calls to the method a call site found before. It checks its assumptions as it goes, and when one breaks (a method defined or removed, a constant set, a TracePoint enabled, a binding taken) it hands the run back to bytecode_runner.rs at the current instruction. METOREX_JIT=0 turns it off.

Values are the 8-byte Object struct in src/object/types.rs, laid out as MRI's C VALUE is. Integers that fit in 63 bits, most Floats, true, false, nil, and Symbols are held in the word itself, and any other value is a pointer to a reference-counted body. An object is freed when the last reference to it goes, and src/object/collector.rs frees groups of objects that refer only to each other. Classes and their method tables are in src/class.

Loading Code#

src/vm/loading handles require, load, and autoload. The libraries Metorex ships are held in the binary under src/vm/stdlib, so require finds them without a directory on the load path. src/vm/capi exports MRI's C API from the binary and loads C extensions built against it, as C extensions shows.

Code as Objects#

The syntax tree the compiler reads is the one a program reads with code as objects. src/vm/native_methods/ast_methods.rs turns tree nodes into hashes, and Method#body and parse return them. The nodes a program reads are the ones its instructions were compiled from.