The Zeno Book Zeno 0.1 zeno-lang.org

28 Compiling

zeno run reads a program and does what it says, a piece at a time. zeno build turns the same program into C, and the machine’s C compiler turns that into a program of its own: no Zeno needed to run it, and many times faster. The C can also be compiled for a microcontroller, which is where Zeno is going.

28.1 zeno build

primes.zeno

function is_prime(n: int) -> bool {
    if n < 2 {
        return false
    }
    var d = 2
    while d * d <= n {
        if n % d == 0 {
            return false
        }
        d += 1
    }
    return true
}

var count = 0
for n in range(200000) {
    if is_prime(n) {
        count += 1
    }
}
print(count, "primes below 200000")
$ zeno build primes.zeno
Wrote primes.c
Built primes. Run it with ./primes
$ ./primes
17984 primes below 200000

zeno build wrote primes.c, called the C compiler on the machine (cc, which is usually GCC or Clang), and left the program primes beside it. On the computer this book was built on, zeno run takes about two seconds over it, zeno run --vm about one, and ./primes a fiftieth of a second.

28.2 Reading the C

The C file is kept, and it is meant to be read beside the Zeno. Here is is_prime:

$ zeno build primes.zeno > /dev/null
$ grep -A12 "^static bool z_is_prime(int64_t z2_n) {" primes.c
static bool z_is_prime(int64_t z2_n) {
    if (z2_n < 2) {
        return false;
    }
    int64_t z_d = 2;
    while (zeno_mul(z_d, z_d, 6, 11) <= z2_n) {
        if (zeno_mod(z2_n, z_d, 7, 12) == 0) {
            return false;
        }
        z_d = zeno_add(z_d, 1, 10, 9);
    }
    return true;
}

It is the Zeno, line for line. An int is an int64_t. d * d is zeno_mul, which checks for overflow with the compiler’s own built-in and raises an OverflowError with the line and column (the two numbers after the operands) when the answer does not fit. In front of the program stands Zeno’s runtime, a small C library for text, lists, errors and print, pasted into every file so that the C needs nothing else to compile.

C runs on every processor there is, and every microcontroller vendor ships a C compiler for its chips. Writing C first gave Zeno all of them at once, and C that a person can read is a C that a person can check. (Decisions 5.5 and 5.5b.)

28.3 Build agrees with run

A program must print the same whichever way it is run: zeno run, zeno run --vm, a C build, an LLVM build, a firmware build. Errors included: an uncaught error gives the same message, at the same line and column, in every one. Zeno’s own tests run every test program every way and compare the output byte for byte.

A program that behaves one way under test and another on the board is the worst kind to debug. If zeno run is to be the place where firmware is tried, it must mean exactly what the compiled program means. (Decision 5.5d.)

28.4 A build is strict

zeno build always checks in strict mode: every type must be known, because a compiled program has no tags to carry them (Types, Written and Worked Out). A program that runs with dynamic values under zeno run is told which value is the trouble:

loose.zeno

function same(x) {
    return x
}
print(same(1), same("one"))
$ zeno build loose.zeno
error: strict mode: the parameter `x` of `same` needs a type. It could not be worked out from how it is used
 --> loose.zeno:1:1
  |
1 | function same(x) {
  | ^

1 error found. Nothing was built.

28.5 The options

Option Means
--memory the program reports, as it ends, how much memory it used
--llvm compile to LLVM’s language in place of C, and hand it to clang
--firmware errors travel as values, with no setjmp, as coding standards for firmware ask (Firmware)
--no-collector no garbage collector; every list and object has one owner, and memory comes from a pool of a fixed size (Building with No Collector)

lists.zeno

var squares: list[int] = []
for n in range(1000) {
    squares.append(n * n)
}
print(len(squares), squares[999])
$ zeno build --memory lists.zeno
Wrote lists.c
Built lists. Run it with ./lists
$ ./lists
1000 998001
memory: 12 blocks made (16388 bytes), at most 12312 bytes in use at once, 0 collections, 8 blocks freed without the collector

Memory explains the report.

28.6 Through LLVM

zeno build --llvm writes the program in LLVM’s intermediate language, the one Clang, Rust and Swift compile through, and hands it to clang. The program is the same, and prints the same:

$ zeno build --llvm primes.zeno
Wrote primes.ll and primes_runtime.c
Built primes. Run it with ./primes
$ ./primes
17984 primes below 200000

The IR is written as text by Zeno itself, with no LLVM library, and primes.ll can be read like primes.c. LLVM brings its own optimiser and every processor it knows. Inside Zeno shows a piece of it.

28.7 For another processor

zeno build compiles for the computer it runs on. For a microcontroller, take the C file it wrote and give it to the microcontroller’s own C compiler, with the board’s header (Firmware):

$ zeno build --firmware --no-collector blink.zeno
$ arm-none-eabi-gcc -O2 -DZENO_BOARD='"nucleo.h"' -c blink.c

Building C for a target is a job the target’s own tools already do well, with the flags, linker scripts and start-up code that only they know. Zeno’s part ends at a C file that compiles anywhere. (Decision 5.12c.)

28.8 What does not compile yet

Nearly the whole language compiles. A few corners do not, and zeno build names each one and points at the line, while zeno run still runs the program:

inner.zeno

function outer() -> int {
    function inner() -> int {
        return 2
    }
    return inner()
}
print(outer())
$ zeno build inner.zeno
error: `zeno build` cannot compile a function inside a function or block yet. `zeno run` can still run this program
 --> inner.zeno:2:5
  |
2 |     function inner() -> int {
  |     ^

A function written with no name, let inner = function() { ... }, does compile. Appendix G has the full list.

zeno build writes C, compiles it with the machine’s compiler, and keeps both. The compiled program prints what zeno run prints, errors included, and is many times faster. A build is always strict. --llvm goes through LLVM; --memory, --firmware and --no-collector are for the board. For another processor, the C goes to that processor’s compiler.

  1. Build a program from an earlier chapter, open its C file, and find each of your functions in it.

  2. Time zeno run, zeno run --vm and the built program on the primes example, with a larger limit.

  3. Make a program overflow an int and build it. Is the message the same as zeno run’s?

  4. Build with --llvm, open the .ll file, and find is_prime.