Docs · Toolchain

Targets

A target is the kind of machine a program is compiled for. Vyi has three, and every command takes --target to pick one before the program (CLI):

--targetProducesvyi run runs it
wasm (default)a WebAssembly modulein a runtime built into vyi
x86-64-linuxa Linux executable for x86-64directly
arm64-linuxa Linux executable for arm64 (AArch64)directly on an arm64 machine, through qemu-aarch64 elsewhere

The same program prints the same output on every target, apart from the differences listed below. The compiler's own test programs run on all three and must agree, so you can develop on wasm and ship a native build. Guide 17, Tooling shows the flags in use.

What each target needs

TargetTo buildTo run with vyi run
wasmnothingnothing; clang only when the entry file has a c_include with a matching .c file beside it
x86-64-linuxa C compiler to link: the first of cc, gcc, clang on your PATHthe same
arm64-linuxaarch64-linux-gnu-gcc to link; on an arm64 machine, cc, gcc, or clang also worksthe same, and qemu-aarch64 on a machine that isn't arm64

A native build without its C compiler stops with no C compiler found (need cc, gcc, or clang to link) or no aarch64 C compiler found (need aarch64-linux-gnu-gcc) and exit status 1.

wasm

vyi build writes a WebAssembly module to out/wasm/<name>. vyi run runs it in the runtime built into vyi: the program reads and writes the terminal's standard input, output, and error, gets its arguments from args() in @io, and can open files by paths relative to the current folder.

The module does its input and output through WASI (wasi_snapshot_preview1). It exports main and has no _start, and vyi run calls main and turns its result into the exit status (CLI).

C code on wasm (guide 15): when the entry file has c_include "mylib" and mylib.c sits beside it, vyi run compiles mylib.c with clang into a module of its own and runs it next to the program. The two modules don't share memory, so only scalar values cross: a pointer, a C string, or a c_struct in such a function's signature is an error on wasm. Linking the C code into the program's own module is (planned). vyi build writes only the program's own module.

x86-64-linux

vyi build --target x86-64-linux writes an x86-64 ELF executable to out/x86-64-linux/<name>, linked by the system C compiler against the system's C library, which it loads when it starts. The program does its input, output, and allocation through the C library.

arm64-linux

vyi build --target arm64-linux writes an AArch64 ELF executable to out/arm64-linux/<name>. It is linked statically, C library included, so it runs on an arm64 Linux machine, or under qemu-aarch64, without the C library installed. vyi run uses qemu-aarch64 from your PATH when the machine isn't arm64.

On both Linux targets the C standard library is always linked, so a c_fn without a body can name any C library function. Linking your own .c files is (planned) (guide 15).

Build modes

--mode picks the build mode, debug (the default) or release. It changes what happens when an integer operation goes wrong, the same way on every target:

Operationdebugrelease
+, -, *, /, or negation whose result doesn't fit the type, including the smallest integer divided by -1panics with integer overflowwraps around at the type's width
a shift (<<, >>) by a count that is negative or not less than the operand's widthpanics with shift count out of rangeuses the count modulo the width: shifting an i32 by 33 shifts it by 1
/ or % by zeropanics with division by zeropanics with division by zero
+%, -%, *%wrapwrap
a conversion between integer types, such as u8(300)keeps the low bits: 44keeps the low bits: 44

On x86-64-linux, release also turns on the optimization passes that --passes adjusts (CLI flags).

A program reads the mode as targetBuildMode from @target, a known value that is "debug" or "release". An if on it is decided while compiling, and the other branch isn't in the program:

Vyi
import { println } from "@fmt"
import { targetBuildMode } from "@target"

fn main() !u8 {
    if targetBuildMode == "debug" {
        println("checks on")   // prints checks on
    }
    return Ok(0)
}

Differences between targets

wasmx86-64-linuxarm64-linux
pointer size4 bytes8 bytes8 bytes
iw and uw32 bits64 bits64 bits
sizeof an array pointer such as [*con]u881616
i8 to i64, u8 to u64, f32, f64the samethe samethe same
a float converted to an integer type it doesn't fit, or NaNstops the program: vyi run: main: wasm error: integer overflow (or invalid conversion to integer for NaN), exit status 1gives a value Vyi doesn't definegives a value Vyi doesn't define, which can differ from x86-64's
stack1 MiBthe operating system's, commonly 8 MiBthe operating system's
running out of stackvyi run: stack overflow, exit status 1stopped by signal 11, exit status 139stopped by signal 11, exit status 139
calls a panic's trace can record4,0962,7302,730
a construct the target can't compile yetan error starting wasm:an error starting x86-64-linux:an error starting arm64-linux:

Out-of-range float conversions differ between the two native targets too: u8(-5.0), computed while the program runs, gives 251 on x86-64 and 0 on arm64. Check a float's range before converting it.

sizeof(T) and alignof(T) answer for the target being compiled, so code that needs a size asks for it instead of assuming one. Running out of stack stops the program before it writes past its stack on every target (guide 08). Past the trace's limit, calls still run but aren't recorded, and a panic's trace ends with a line counting the calls it couldn't show.

Code for one target

@target describes the target being compiled as known values:

Namewasmx86-64-linuxarm64-linux
targetOS"wasm""linux""linux"
targetArch"wasm32""x86-64""arm64"
targetBuildMode"debug" or "release"the samethe same

A file-level if on them chooses declarations while compiling (known values):

Vyi
import { println } from "@fmt"
import { targetOS } from "@target"

if targetOS == "wasm" {
    con platform = "WebAssembly"
} else {
    con platform = "Linux"
}

fn main() !u8 {
    println(platform)
    return Ok(0)
}

This prints WebAssembly on wasm and Linux on the two Linux targets. --os changes what targetOS reports (CLI flags).