u/Fresh-Spread3374

▲ 1 r/rust

W16 — runtime.

Hi !

For the past 3 months, I’ve been working on W16. What is this project about? Here is a brief overview (for more details, feel free to visit the repository).

In Short

>W16 is a multi-stage pipeline language runtime written in Rust.

What does "Multi-stage Pipeline" mean here?

It means that W16 doesn't just use a simple Bytecode -> VM approach. Instead, it utilizes HIR, MIR, Bytecode, and multiple bytecode execution engines.

https://preview.redd.it/vaieine9oa5h1.png?width=1246&format=png&auto=webp&s=089d8658ae45bf3561ebbf221ba7b7d04fe6813a

The pipeline looks like this:

Pipeline Breakdown

  • HIR:
  • What it is: A high-level, structured, and typed intermediate representation.
  • Why: To allow any language frontend to easily translate its source code into a representation that is convenient for the runtime.
  • Why not jump straight to bytecode or MIR?: Because it is much more convenient for a developer to target a high-level HIR rather than low-level bytecode right away.
  • MIR:
  • What it is: A mid-level SSA IR.
  • Why: It is the perfect place for optimizations. The SSA form makes writing an optimizer clean and straightforward, and since the representation isn't as low-level as bytecode, it makes development even more convenient.
  • Bytecode:
  • What it is: A register-based, 3-address (two source operands and one destination register), 32-bit bytecode.
  • Why: This is the most efficient format for interpretation, as well as for JIT/AOT compilation.
  • Bytecode Layout:
8-bit 8-bit 8-bit 8-bit Total
Opcode Destination Register First Operand Second Operand 32-bit
  • Standard VM:
  • What it is: A standard virtual machine for executing bytecode. It's called "standard" because the runtime actually features two different VMs.
  • Why: Because a standard VM is more stable than the alternative executor.
  • Orca VM:
  • What it is: An experimental VM. It is experimental because it skips runtime checks and the codebase is very unsafe.
  • Why: Fewer checks = faster execution speed.
  • JIT Compiler:
  • What it is: A Just-In-Time compiler that compiles bytecode into native machine code on the fly.
  • Why: For maximum bytecode execution performance.
  • AOT Compiler:
  • What it is: An experimental Ahead-Of-Time compiler. In W16, it generates an object file and then invokes an external host linker (link.exe from MSVC on Windows, cc on macOS & Linux). It is considered experimental precisely because it relies on this external linker invocation.
  • Why: To make it possible to compile bytecode directly into a standalone executable file.

Have any languages been built using W16 as a backend?

>So far, no stable compilers have been created using W16 for code generation.

HOWEVER! As part of the project, I built an experimental C compiler (C11 standard) that has most core features implemented (though some are still missing). You can find its codebase in the exact same repository where W16 lives.

Libraries I use

  • cranelift (and its sub-crates) — used for both JIT and AOT code generation.
  • target-lexicon — required for handling target architectures during AOT compilation.
  • cc — also used for AOT compilation to locate the system linker.

Links

Project Status

>Currently paused

Recently, I decided that I want to completely rewrite w16-ir (a workspace crate within the project). Because of this, I am currently actively focusing on implementing this new crate.

Feedback

I would be incredibly happy if you find the project interesting! If you have any questions, thoughts, or advice — I'm right here.

reddit.com
u/Fresh-Spread3374 — 6 days ago

W16 runtime.

Hello.

For the past few months, I've been working on my custom runtime and architecture called W16, written entirely in Rust. I just published the first crates (w16-core, w16-ir, and w16-lib) to crates.io and pushed the project to GitHub. I'd love to share its architecture with the community and get some feedback!

The Pipeline & Architecture

In short, the compilation and execution pipeline looks like this: HIR -> MIR -> Bytecode -> Execution (via VM or JIT).

Here is a detailed breakdown of what happens under the hood:

  • HIR (High-Level Intermediate Representation): This representation preserves most of the source language's semantics. It is designed for type checking, static analysis, and providing a clean integration layer for any custom programming languages that want to use W16 as their backend.
  • MIR (Mid-Level Intermediate Representation): This is where all the optimization passes happen. The MIR strictly uses the SSA (Static Single Assignment) format. It is almost entirely stripped of high-level semantics, making it the perfect place for optimizations like dce, constant folding, etc.
  • Bytecode: W16 uses a register-based bytecode format. Every instruction has a fixed size of 32 bits, structured as a 3-address code layout:
    • Bits 0–8: Opcode (Operation Code)
    • Bits 8–16: First operand (Register or immediate value)
    • Bits 16–24: Second operand
    • Bits 24–32: Third operand
  • Execution Paths: Once the bytecode is generated, W16 offers two execution strategies:
    • VM (Virtual Machine): A register-based virtual machine. I implemented a dispatch table approach in the interpreter loop to achieve better execution speed.
    • JIT Compiler: For maximum performance, the JIT backend uses Cranelift. The runtime translates the W16 bytecode into Cranelift IR, and Cranelift handles the actual machine code generation and execution.

Links

I would highly appreciate your thoughts on this architecture! What do you think about the architecture? If you have any suggestions, questions, or want to discuss the implementation details — please let me know!

(Note: English is not my native language, so I am using a translator to read and reply to your comments.)

https://preview.redd.it/zaay39k8v93h1.png?width=400&format=png&auto=webp&s=1bd2eea86286e384267d91f29cddad6a52124a6c

reddit.com
u/Fresh-Spread3374 — 16 days ago