How Blyx learned to compile itself
A small executable and a file called compiler.blyx are enough to build another copy of Blyx Direct. Here is what happens between those two files, and how to check the result yourself.
One language, two compiler downloads
Until this release, building a native Blyx program meant asking the Blyx driver to translate it into Rust. The driver invoked rustc and the host linker, and you could give the finished executable to someone who had none of those tools. Rust was a dependency on the machine doing the build. For a developer working on Blyx itself, that also meant that part of the language’s behavior lived in the code that generated Rust.
Blyx Direct 0.1.0 brings a second route. Its lexer, parser, type checks, instruction emitter and executable writer live in a Blyx source file. On Windows x64 or Linux x64, the downloaded compiler can turn a supported program into an executable on its own. One program that fits within its supported language is the compiler itself. That is what self-hosted means in this release. Read the compiler source.
The two downloads both contain a file named blyxc, so keep them in separate directories. Direct reports blyxc direct 0.1.0 when you run its version command. The 0.2.1 toolchain remains the choice for floating-point arithmetic, vectors, imported packages and Apple Silicon macOS. The browser playground and the editor’s Run File command use the Blyx interpreter compiled to WebAssembly. Choose a download.
A first program with an awkward last batch
Suppose a sensor logger sends up to 64 readings per batch. Exactly 64 readings take one batch. A 65th reading needs a second batch, even though that batch has 63 empty slots. This little calculation is a useful introduction to Blyx because the answer is easy to check by hand and the obvious formula has an edge case.
// Count batches while keeping every intermediate value within i64.
fn batches(readings: i64, capacity: i64) -> i64 {
if readings < 0 || capacity <= 0 { return -1; }
let full = readings / capacity;
if readings % capacity == 0 { return full; }
full + 1
}
fn main() {
println!(batches(0, 64));
println!(batches(64, 64));
println!(batches(65, 64));
println!(batches(9223372036854775807, 2));
println!(batches(65, 0));
}fn introduces a function. Both parameters are signed 64-bit integers, and -> i64 tells the compiler what the caller expects back. let full binds an immutable value. The early returns handle invalid inputs and exact multiples. The final expression, full + 1, supplies the remaining return value. The function uses −1 as its own convention for invalid input. A caller can distinguish that result from the zero batches needed for an empty log.
A common formula adds capacity minus one to the number of readings before dividing. With the largest signed 64-bit integer and a capacity of two, that intermediate addition overflows. Here we divide first. When there is a remainder, the quotient is small enough for one more batch. That is why the fourth call can print the correct answer even at the upper limit of the type.
Download batches.blyx into the directory where you extracted Direct. On Windows, run these commands in PowerShell. Choose a new output filename if you have already built the example.
.\blyxc.exe --version
.\blyxc.exe build batches.blyx -o batches.exe
.\batches.exeOn Linux x64, run the compiler and executable from the extracted directory.
./blyxc --version
./blyxc build batches.blyx -o batches
./batchesThe five lines correspond to an empty log, an exact batch, a partial final batch, the integer boundary and an invalid capacity. Change 65 to 128 in the third call and predict which output will change before rebuilding.
0
1
2
4611686018427387904
-1Follow a comparison into the executable
Start with the test readings % capacity == 0. The lexer distinguishes identifiers, numbers and operators. In particular, it reads the two adjacent equals signs as a single comparison token. The parser gives the remainder operation higher precedence than equality, so the comparison uses the remainder of the division. The result is a Boolean, the type required by the enclosing if.
The compiler makes two passes over the functions. The first records names, parameter types and result types. That lets a function call another function defined later in the same file. On the second pass the compiler can check the argument count and types at a call site while emitting the function body. A call to an unknown function is a compilation error. Diagnostics report a byte offset into the source, which matters when a string contains multibyte UTF-8 characters.
While emitting an if, the compiler knows which condition will skip the body. It cannot yet know how many instruction bytes the body will occupy. It reserves space for that distance and fills it in once it has emitted the destination. Calls to functions defined later need a similar fixup. You can follow those steps in the source by looking for branch, resolve and call_fixup.
Instructions alone are insufficient for an operating system to launch a program. Windows needs a PE32+ image with an entry point and imports for system services. Linux needs an ELF64 image with loadable segments and an entry point. Direct writes those headers, emits the runtime routines and resolves the addresses itself. Both targets use x86-64 instructions, but each executable belongs to its own operating system. Inspect the PE and ELF writers.
Where the first compiler came from
If a compiler is written in the language it compiles, something still has to build the first executable. For this release, the Rust-based Blyx driver builds a one-time seed. The seed reads compiler.blyx and writes generation one. Generation one reads the same source and writes generation two. The release workflow packages the verified second generation with the source and instructions.

You can repeat the interesting part with the files already in the download. In a fresh directory on Linux, build the compiler twice, then ask the operating system to compare the executable files.
./blyxc build compiler.blyx -o generation2
./generation2 build compiler.blyx -o generation3
cmp generation2 generation3A successful cmp exits with status zero and prints nothing. On Windows, the two SHA-256 values printed by these commands should agree.
.\blyxc.exe build compiler.blyx -o generation2.exe
.\generation2.exe build compiler.blyx -o generation3.exe
Get-FileHash .\generation2.exe, .\generation3.exeIdentical bytes are evidence that the compiler reaches a fixed point for this source and target. A compiler could also reproduce a bug perfectly. The release tests therefore run the programs it emits. They check recursion, block scope, short-circuit logic and signed arithmetic, along with errors such as an unknown function or an assignment to an immutable value. They also exercise paths containing spaces and Unicode and check that an existing output file survives a failed attempt to reuse its name. The test process clears PATH for these builds and runs. Read the tests or inspect the release run.
What the release covers today
We ran the 195 programs in the website’s example catalog through the Windows Direct compiler at the release revision. Eight compiled, ran and produced exactly the catalog’s expected output. The other 187 stopped at compilation. Fibonacci, greatest common divisor, short-circuit evaluation and a value returned from a loop were among the eight that passed. This count describes one public collection, which includes numerical and package examples. It measures neither execution speed nor the percentage of the language specification implemented.
| What you want to do | What to use |
|---|---|
| Learn syntax, edit examples or inspect output immediately | The browser playground, or Run File in the Blyx Language editor extension. |
| Build an integer-based utility or study a working compiler | Blyx Direct on Windows x64 or Linux x64. It supports checked integer arithmetic, functions, recursion and loops. |
| Fit a regression, compute a dot product or import an existing numerical package | The 0.2.1 toolchain. Its native build path needs Rust and a host linker. |
A concrete dividing line is a batch of floating-point sensor readings. Direct can calculate how many batches to transmit, as in our example. Calculating the mean of a vector of readings uses the wider numerical toolchain. The distinction applies to packages too. LinearFit trains a regression, Kinglet works with checkpoint ranges, and BlyxTorch provides training tools. A package listing in Blyx Nest tells you where to find its source and examples. Read its version and execution requirements before choosing a compiler. Browse frameworks, explore libraries or open Blyx Nest.
For a first contribution, choose one short program that already works in the wider toolchain and write down its expected result. Try compiling it with Direct. If it stops, reduce it to the smallest example that still exhibits the behavior. Include the compiler version, operating system, source and exact output in a report. That gives a compiler contributor a test case and gives other developers a precise answer about what works. Share a result with the Blyx community.
Release archives and checksums · Handbook build guide · Learn Blyx from the beginning