Bytecode

A look under the hood of the Ry VM.

What is Bytecode?

When you run a Ry script, the code isn't executed directly. Instead, it's first compiled by the Ry compiler into a series of compact, platform-agnostic instructions called bytecode. This bytecode is then run by the Ry Virtual Machine (VM).

Ry uses a register-based architecture, meaning instructions operate on virtual registers rather than a stack. This reduces instruction overhead and maps efficiently to modern CPU architectures.

From Source to Instructions

Consider this simple Ry script that defines a variable and calls a function.

data message = "Hello, VM!"
out(message)

Disassembly View

The compiler turns that script into instructions that look like this:

# == main chunk ==
# Load "Hello, VM!" (const #0) into Register 0.
0000  OP_LOAD_CONST     R0, #0      ; "Hello, VM!"

# Define global 'message' (const #1) with value from R0.
0001  OP_DEFINE_GLOBAL  R0, #1      ; "message"

# Prepare call: Load 'out' (const #2) into R0.
0002  OP_GET_GLOBAL     R0, #2      ; "out"

# Load argument 'message' (const #1) into R1.
0003  OP_GET_GLOBAL     R1, #1      ; "message"

# Call function in R0 with 1 argument.
0004  OP_CALL           R0, 1

# Return result (in R0).
0005  OP_RETURN         R0

Documentation Complete!

Congratulations! You've reached the end of the Ry documentation. You now have a complete overview of the language, from its basic syntax to its advanced features and underlying execution model.