machdocs
Home Ecosystem Changelog

The comptime channel

The $ prefix opens the comptime channel, the compiler-owned namespace a program reads at compile time. It is read-only: $ selects and expands, it never executes or mutates. Conditional compilation, intrinsics, and target queries all ride one of its shapes.

The shapes

The parser tells the shapes apart by structure, so a name never has to be looked up to know what kind of read it is.

ShapeMeaning
$mach.* / $project.* / $bin.*rooted compiler-owned read (compiler to developer)
$sym(args)comptime function call (an intrinsic)
$if, $orcomptime control flow
Note

Per-declaration codegen attributes (symbol rename, library pin, inline, align, section) are written as #[...] decorators, not $-comptime shapes. See Decorators.

Compiler-owned roots

Three rooted trees carry the read-only facts a build exposes. $mach.* (the active build, the compiler identity, the project that owns the module, and the source position) has its own section below. $project.* and $bin.* read the root project and the current artifact, one member at a time:

ReadValue
$project.idthe root project's id, from [project]
$project.versionwhole version string, e.g. "2.0.0"
$project.version.majormajor version component (folded integer)
$project.version.minorminor version component
$project.version.patchpatch version component
$project.target.sysselected target system string, e.g. "linux"
$project.target.archselected target arch string, e.g. "x86_64"
$project.target.abiselected target abi string, e.g. "sysv64"
$bin.namethe table key of the artifact being built
$bin.kindthat artifact's kind as the manifest spells it: "bin", "shared", "static", "object" or "module"

The $project.target.* reads carry the manifest's declared string spellings, distinct from the numeric tags used for $mach.build.* comparison. The flat $project.version string and its structured major, minor, and patch components are all available.

val ver: str = $project.version;                 # "2.0.0", from [project].version
$if ($project.target.sys == "windows") { ... }   # the declared sys string
$if ($bin.kind == "shared") { ... }             # only in a shared library

$project.* names the root: the project being built, the same in every module of the build, a dependency's modules included. $mach.project.* names the owner: the project whose source tree holds the module being compiled. In a root module the two agree. In a dependency's module $project.version is the consumer's version and $mach.project.version the dependency's own, so a library that reports its version reads $mach.project.version.

A single file built outside a project is an executable named after the file: mach build tool.mach reads "tool" and "bin". A project that declares no artifact has neither, and reading either is a compile error.

Note

A path the root does not carry, such as $project.name or $project.description, is rejected as an unknown $project.* path. The v6 spelling $project.target.os is refused naming $project.target.sys, which mach migrate writes.

The $mach.* namespace

The $mach.* subtree is the compiler view of the world: the resolved build context, the compiler identity, the project that owns the module, and source position. Every read is a comptime constant, and every read is deterministic: a function of the source, the manifests and the selected build, never of the clock, the host machine or the checkout. There is no build timestamp, host name or version-control state. A build that wants one generates a source file from a build step.

Renamed in 7

$mach.os.* is $mach.sys.*, $mach.build.os is $mach.build.sys, and $mach.build.extensions.* is $mach.build.ext.*. Each old spelling is refused naming its new one, which mach migrate writes.

$mach.build.* - what we build for

The resolved active build facts. sys, arch, abi, and mode share the numeric tag space of $mach.{sys,arch,abi,mode}.*, so a comparison is a plain integer compare.

$mach.build.sys                 # compared against $mach.sys.* tags
$mach.build.arch                # compared against $mach.arch.* tags
$mach.build.abi                 # compared against $mach.abi.* tags
$mach.build.pointer_width       # integer count of bytes
$mach.build.mode                # compared against $mach.mode.* tags
$mach.build.pie                 # 1 when building position-independent, else 0
$mach.build.platform            # the target's platform tag as a string, "" when unset
$mach.build.ct_mul(op, width)   # 1 when that multiply runs in constant time on the target, else 0
$mach.build.ext.<name>          # 1 when the target selects that instruction-set extension, else 0

$mach.build.mode answers which pipeline the build runs, whatever the profile is named: $mach.mode.release when the selected profile has optimize = true (or -O is given), $mach.mode.debug when it does not.

$mach.build.ct_mul takes a bare word for the multiply (low, high_u, high_s, high_su, wide_u, wide_s) and the operand width in bits (8, 16, 32, 64 or 128). It lets a constant-time library choose a hardware or a bit-serial path without keeping a per-target list. See Secrecy.

$mach.build.ext.<name> has one member per extension name of the selected architecture, closed over what each one implies. A name the architecture does not declare is a compile error, never a silent 0, so a source that serves several architectures asks the extension question only inside an architecture guard. A comptime && reads both sides, so the guard is a nested $if, which also keeps one fallback per architecture:

$if ($mach.build.arch == $mach.arch.x86_64) {
    $if ($mach.build.ext.sha == 1) {
        val BACKEND: u8 = 1;
    }
    $or {
        val BACKEND: u8 = 0;
    }
}
$or ($mach.build.arch == $mach.arch.aarch64) {
    $if ($mach.build.ext.sha2 == 1) {
        val BACKEND: u8 = 2;
    }
    $or {
        val BACKEND: u8 = 0;
    }
}
$or {
    val BACKEND: u8 = 0;
}

The members above are the whole subtree, and no manifest key adds one. An unknown $mach.build.<name> is a compile error at the use site. Project configuration constants are ordinary vals selected with $if over these build facts.

Version and compiler identity

$mach.version                   # the version string, e.g. "7.0.0"
$mach.version.major             # integer component (minor, patch too)
$mach.compiler.name             # compiler identity
$mach.compiler.version          # same value as $mach.version

The owning project and the source position

$mach.project.id                # the owning project's [project].id
$mach.project.version           # its version string (major, minor, patch too)
$mach.source.file               # the module's file, relative to its project: "src/lib/hedge.mach"
$mach.source.line               # the 1-based line the read is written on
$mach.source.module             # the module's qualified name: "hedge.lib.hedge"

$mach.source.file is never an absolute host path and is /-separated on every host, so a checkout at another path reads the same value.

Tag tables

The $mach.{sys,arch,abi,mode}.* tables exist for path-value comparison against the resolved build. The names are the target registries' own spellings, and an unrecognized tag name is a compile error, never a silent fold.

$mach.sys.linux
$mach.sys.darwin
$mach.sys.windows
$mach.sys.freestanding           # no OS, bare metal
$mach.sys.uefi                   # a PE image a UEFI firmware loads
$mach.sys.vulkan                 # a SPIR-V module's system
$mach.sys.wasi

$mach.arch.x86_64
$mach.arch.aarch64
$mach.arch.riscv64
$mach.arch.riscv32
$mach.arch.spirv
$mach.arch.wasm32

$mach.abi.sysv64
$mach.abi.win64
$mach.abi.aapcs64
$mach.abi.darwinpcs
$mach.abi.aapcs64win
$mach.abi.lp64
$mach.abi.lp64f
$mach.abi.lp64d
$mach.abi.ilp32
$mach.abi.ilp32f
$mach.abi.ilp32d
$mach.abi.shader                 # a SPIR-V module's convention
$mach.abi.basic

$mach.mode.debug
$mach.mode.release

Comparing tags

Tag comparisons are path-value, with no .id suffix or unwrapping. Both sides share one numeric space, so the comparison is an ordinary integer compare.

$if ($mach.build.sys == $mach.sys.linux) { ... }
$if ($mach.build.arch == $mach.arch.x86_64) { ... }

Folding into runtime values

A comptime read can initialize a runtime binding, and the compiler folds the right-hand side at compile time. A comparison is a u1.

use std.types.string.str;

pub val IS_LINUX: u1  = $mach.build.sys == $mach.sys.linux;
pub val COMPILER: *u8 = $mach.compiler.name;
pub val VERSION:  str = $mach.project.version;
pub val WIDTH:    u64 = $mach.build.pointer_width;

The bare $ident is rejected

A standalone bare $ident such as $mode or $foo is none of the shapes above. A comptime parameter is referenced by its bare name (no $), and every comptime path is rooted. The rule applies identically in a $if gate and in value position.

Diagnostic

comptime parameters are referenced without $. a comptime path starts at $mach, $project or $bin

Which binding a name denotes

A name in a comptime position denotes the declaration it resolves to under the ordinary scoping rules. A runtime local that shadows a module constant has no comptime value, so it cannot size an array:

val N: i64 = 9;

fun f(k: i64) i64 {
    val N:  i64 = k;
    var xs: [N]i64;   # error: the array length must be a comptime constant
    ret xs[0];
}

A comptime parameter or an $each loop variable is a comptime binding, so it can size a type in each instance or iteration that binds it.

Not in the channel

See also

  • Intrinsics - $size_of, $length_of, and the comptime call set
  • Control flow - $if / $or gates over these reads
  • Decorators - #[...] codegen attributes
  • Manifest - the [project] and target stanzas feeding $project.*