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):
--target | Produces | vyi run runs it |
|---|---|---|
wasm (default) | a WebAssembly module | in a runtime built into vyi |
x86-64-linux | a Linux executable for x86-64 | directly |
arm64-linux | a 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
| Target | To build | To run with vyi run |
|---|---|---|
wasm | nothing | nothing; clang only when the entry file has a c_include with a matching .c file beside it |
x86-64-linux | a C compiler to link: the first of cc, gcc, clang on your PATH | the same |
arm64-linux | aarch64-linux-gnu-gcc to link; on an arm64 machine, cc, gcc, or clang also works | the 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:
| Operation | debug | release |
|---|---|---|
+, -, *, /, or negation whose result doesn't fit the type, including the smallest integer divided by -1 | panics with integer overflow | wraps around at the type's width |
a shift (<<, >>) by a count that is negative or not less than the operand's width | panics with shift count out of range | uses the count modulo the width: shifting an i32 by 33 shifts it by 1 |
/ or % by zero | panics with division by zero | panics with division by zero |
+%, -%, *% | wrap | wrap |
a conversion between integer types, such as u8(300) | keeps the low bits: 44 | keeps 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:
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
wasm | x86-64-linux | arm64-linux | |
|---|---|---|---|
| pointer size | 4 bytes | 8 bytes | 8 bytes |
iw and uw | 32 bits | 64 bits | 64 bits |
sizeof an array pointer such as [*con]u8 | 8 | 16 | 16 |
i8 to i64, u8 to u64, f32, f64 | the same | the same | the same |
| a float converted to an integer type it doesn't fit, or NaN | stops the program: vyi run: main: wasm error: integer overflow (or invalid conversion to integer for NaN), exit status 1 | gives a value Vyi doesn't define | gives a value Vyi doesn't define, which can differ from x86-64's |
| stack | 1 MiB | the operating system's, commonly 8 MiB | the operating system's |
| running out of stack | vyi run: stack overflow, exit status 1 | stopped by signal 11, exit status 139 | stopped by signal 11, exit status 139 |
| calls a panic's trace can record | 4,096 | 2,730 | 2,730 |
| a construct the target can't compile yet | an 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:
| Name | wasm | x86-64-linux | arm64-linux |
|---|---|---|---|
targetOS | "wasm" | "linux" | "linux" |
targetArch | "wasm32" | "x86-64" | "arm64" |
targetBuildMode | "debug" or "release" | the same | the same |
A file-level if on them chooses declarations while compiling
(known values):
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).