sysl

Attributes, annotations, and compile time

:: attributes a type answers, the nineteen annotations a function takes, the three that lay out or place what they mark, the six a file’s header takes, @assert which stands on its own, and the #if directive that gates lines before the lexer sees them.

Three kinds of form in sysl reach into the compilation rather than into the running program. Each has a name and a spelling of its own:

writtenisread by
T::Attran attribute — a question a type’s own name answersthe analyzer, at the use
@test, @tailrec, @pure, @ghost, @export, @reads, @writes, @crossing, @domain, @noinline, @inline, @cold, @disjoint, @no_alloc, @no_block, @no_recursion, @blocks, @stack, @abandons, @track_calleran annotation — a fact about the free function under itthe grammar
@setup, @teardown, @setup_all, @teardown_allan annotation — a hook sysl test runs around a module’s teststhe grammar
@keeps(...)an annotation on an extern — the parameters the C function holds on to past the call; see @keeps. On a trait’s own method declaration, with @answers(...), what a call through an object is told; see belowthe grammar
@abandons *extern(A) -> Ra mark on a type — a C function pointer a call through which may abandon every frame below it; see @abandons *externthe grammar
@returns_twicean annotation on an extern — the C function can return a second time, as setjmp does; see @returns_twicethe grammar
@needs(...)an annotation — the capabilities reaching the declaration under it requires; with @abandons and @keeps, one of the three an extern takesthe grammar
@packed, @align(n), @section("...")an annotation — where the declaration under it is laid out, or where it landsthe grammar
@export("...", weak), @export(weak) on a functionan annotation — the export is a default a strong definition of the symbol replaces at the link; see a weak exportthe grammar
@out_of_memory(value) beside an @exportan annotation — what the export answers its C caller where an allocation inside the call is refused, in place of a trap; see an export that answers out of memorythe grammar, then the analyzer
@export("...") on a structan annotation — the name the type carries in a generated C headerthe grammar
@renamed("...", "..."), @removed("...", "...")an annotation — the declaration under it is a tombstone, a name that is gone; see @renamed and @removedthe grammar
@export("...") on a module var or valan annotation — the C symbol the storage is published underthe grammar
@export("...") on a const or a simple enuman annotation — the macro a generated C header defines for the value, or the prefix of one per variant; see constants in the headerthe grammar
@no_alloc, @no_block, @no_recursion, @requires, @link, @include, @tests, @no_fusean annotation — a fact about the whole file, in its headerthe grammar
@assertan annotation that describes nothing but itself — a condition settled while compilingthe analyzer, once
#ifa directive — a gate on linesa pass before the lexer

The last two are told apart by the sigil, and that is the whole rule. An annotation is @ and belongs to what it is written above — a declaration, or the file itself; a directive is # and gates lines. Nothing about indentation is involved, which matters because a declaration at the margin — a module header — has its annotation at the margin too, so a rule about columns would have had the two forms competing for one position.

A directive is still written at column 1, and for its own reason: it is gone before anything counts a column, so an indented one would look like it takes part in a block structure it has nothing to do with. That is a rule about directives, not the thing that distinguishes them.

Annotations come in groups, by what they attach to — and the last of them is the empty one, which is as much a rule as the others.

On a function there are seventeen, each written on its own line above the declaration. More than one may be stacked, and writing the same one twice is refused. @test and @tailrec are below, and so are the four hooks a module’s tests may declare — @setup, @teardown, @setup_all and @teardown_all — the three that tell the optimizer what a definition is for, @noinline, @inline and @cold, and @disjoint(...), which tells it which parameters never overlap; @pure, @ghost, @reads and @writes belong to the specification vocabulary and are on the verification page; @export makes the definition C-callable and is on the FFI page, beside the extern it is the mirror image of; @crossing(...) says a parameter hands a value to another concurrency domain where no body shows it — a by-value one is handed a copy of every view in it, and one a body shows is inferred — and is on the memory page, beside the crossing rule it asks; @no_alloc, the one header word a function may also carry, holds that one definition to making no heap storage and is below; and @abandons says a call to the function never comes back while the program goes on, so the caller’s frame is given back first — it marks an extern too, and is on the declarations page beside become. On an extern that answers something other than never it says a call may abandon — Lua’s lua_call comes back, or passes a lua_error up past its caller — so the caller keeps its frame and may hold nothing counted across the call; see an extern that may abandon.

On a type, or on storage there are four, and three of them are about where rather than about what the declaration does. @packed and @align(n) lay out a struct — no interior padding, and the boundary the aggregate begins on — and @align(n) marks one binding’s storage as well. @section("...") marks a binding or a function, and says which linker section it lands in. All three are below. The fourth is @export, which on a struct names the type in a generated C header rather than placing it, and on a module var or val publishes the storage under a C symbol; both are on the FFI page with the other half of themselves.

On the file there are six, in its header directly below module and before everything else: @no_alloc and its siblings, @requires(...), @link("..."), @include("..."), and @tests. The first two say what the whole module may do; @link says what its externs need at the linker and @include what its c const and c type blocks need at the C compiler; the last says it is scaffolding for the module’s tests; and @no_fuse says no multiply in the module is fused with the add it feeds. All six attach to the file rather than to any declaration in it — and writing one further down is refused with a message saying where it belongs, except @no_alloc and @no_fuse above a function or a method, which hold that one definition instead (below). The first four are covered under modules and FFI, where what they mean is; @tests and @no_fuse are below.

On nothing at all there is one: @assert, which stands where a declaration stands and describes only itself. It attaches to nothing, declares no name, and nothing can refer to one — two saying the same thing are two checks rather than a duplicate. It is below.

On a member, four: the ones that are about a PARAMETER. @crossing, @scoped, @reads and @writes each name parameters, and a method, a property or an associated function has parameters exactly as a free function does — so there was never anything for a blanket refusal to be about in their case:

struct Chan
    open: bool

    @crossing(v)
    send(*self, v: *int) -> int = v[0]

var c = Chan(true)
var n = 7

print(c.send(&n))
7

Beside them a member takes the seven about the function it lowers to — @noinline, @inline, @cold, @no_alloc, @no_block, @no_recursion and @blocks — and @relocates, which says a call may move the storage its receiver’s elements live in, so a live ref to one of them is refused the call (memory), and @views(self), which says the view a call answers is borrowed from its receiver, so the view may not outlive it (memory), and @consumes, which says a call takes everything its receiver holds, so a local it was called on may not be read again until it is assigned (below). Everything else is refused, and the line is what the annotation is about rather than where it is written. “A function” in the list above means a free function: what sysl test calls, what recurses, what a symbol names, how a type is laid out — none of which a member supplies.

struct Counter
    n: int

    @test
    is_zero(self) -> bool = self.n == 0
the only annotations a member may carry are the ones about a parameter

So what sysl test runs is a free function that calls the member. @packed and @align(n) are not the exception they look like: they mark the struct, written above struct and not above a field. @assert inside a type’s body is refused too, and gets its own sentence, because it is not a claim about the member under it — it goes beside the type, where sizeof and offsetof still name what it is about.

They are refused above a field and above a variant, by a sentence of their own: each names parameters, and neither a field nor a variant has any.

struct Point
    @crossing(x)
    x: int
are about a parameter

#test above a member is answered by the same sentence, with the sigil named at the end of it. A directive is gone before the lexer counts a column, so an indented # never reaches the directive pass at all and arrives at the member grammar instead — and being told only that an annotation is written @ would send a reader to write @test, which a member is refused just the same.

An annotation’s name is an ordinary identifier, which is the point of writing these as annotations at all: nothing here is reserved, so a program may still call something test, link, alloc, no or requires. The slab package’s allocator calls its central function alloc and threads its free list through a function called link.

The @ is also read inside a pattern, where it binds a name to what a sub-pattern matched (patterns). The two never compete: an annotation’s @ is a prefix at the start of a line above a declaration, and a pattern’s is infix between a name and a pattern, in a position no declaration may stand.

@scoped(f) — a member that finishes what it starts before its scope closes

@scoped(f) above a method promises that f runs in another concurrency domain and finishes before the call that lent the method’s *self returns. It is how a scheduler’s spawn says its children are joined by the scope that handed out the receiver, and it implies @crossing(f): what is passed at f is held to the crossing rule, exactly as if both were written. The compiler cannot see the join — that is the library’s promise to keep, as @crossing‘s boundary is — so what it changes is what a closure that was lent the scope may do with it: a call of the member keeps neither its receiver nor what it runs, though the job it queues captures the scope (memory.md § A borrowed pointer cannot escape).

import sysl.buf.{Buf, buf}

struct Scope
    jobs: Buf[&sync Fn() -> unit]

    @scoped(f)
    spawn(*self, f: &sync Fn(*Scope) -> unit)
        val s = self

        self.jobs.push(() -> f(s))

struct Runner
    n: int

    scope[R](self, body: *Scope -> R) -> R
        var s = Scope(buf())
        val r = body(&s)

        for j in s.jobs do j()
        r

print(Runner(0).scope((s) ->
    s.spawn((t) -> print(usize(s) == usize(t)))
    17))
true
17

Without @scoped the same closure is refused: spawn keeps the receiver in the job it queues, and a closure borrows the scope it is handed for the call. What spawn answers is still followed as made of the scope, so a handle built around self may not be stored outside the body.

A closure written straight at f may capture a view. Everything a &sync Fn may capture it may capture too — scalars, arrays, &sync T, raw pointers, the lent scope — and also a view, []T or []const T, whose elements may themselves be shared. The view is lent to the child rather than shared with it: it is stored with a null owner, the form a view of a frame’s own array has, so neither building the child nor letting it go touches the count of the storage it was taken from, and the caller is held inside the scope call until the child is done.

import sysl.buf.{Buf, buf}

struct Scope
    jobs: Buf[&sync Fn() -> unit]

    @scoped(f)
    spawn(*self, f: &sync Fn() -> unit) = self.jobs.push(f)

struct Runner
    n: int

    scope[R](self, body: *Scope -> R) -> R
        var s = Scope(buf())
        val r = body(&s)

        for j in s.jobs do j()
        r

total(v: []const int) -> int
    Runner(0).scope((s) ->
        s.spawn(() -> print(v[0] + v[1] + v[2]))
        int(v.len))

var b: Buf[int] = buf()

b.push(3)
b.push(4)
b.push(5)
print(total(b.view()))
12
3

A struct or tuple holding a view is lent the same way, whether the child captures it whole or reads a field of it: every view in its own storage, at any depth, is stored with a null owner, and every other field is asked as anything a &sync Fn captures is. So a matrix — a view of its elements and the scalars that say how to read them — goes to a child as the bare view does, and the child’s writes through it are the caller’s once the scope closes. A string or a &T beside the view is lent with it (memory).

import sysl.buf.{Buf, buf}

struct Scope
    jobs: Buf[&sync Fn() -> unit]

    @scoped(f)
    spawn(*self, f: &sync Fn() -> unit) = self.jobs.push(f)

struct Runner
    n: int

    scope[R](self, body: *Scope -> R) -> R
        var s = Scope(buf())
        val r = body(&s)

        for j in s.jobs do j()
        r

struct Matrix
    data: []int
    rows: usize
    cols: usize

scale(m: Matrix, k: int) -> int
    Runner(0).scope((s) ->
        s.spawn(() ->
            for i in 0..<m.rows * m.cols do m.data[i] *= k)
        int(m.rows))

var b: Buf[int] = buf()

for i in 1..4 do b.push(i)
print(scale(Matrix(b.view_mut(), 2, 2), 10))
print(b.view()[0] + b.view()[3])
2
50

A &T, a Buf, a Map, a view of references or of strings, and a struct of them are lent to a closure written at the argument: every count on what they reach changes atomically until the closure is let go of (memory). They are lent where the closure may run in another domain, which is where such a capture was refused before — a @scoped parameter, and a @bounded one whose type is a &sync callable, written so or through an alias (type Job = &sync Fn(usize) -> int, then job: Job). A @bounded parameter that is a bare arrow or a plain Fn runs where the call runs, takes its captures’ shares as any closure does, and may write them. A lend at a @bounded parameter lasts that call: the caller writes what it lent before the call and after it. One at a @scoped parameter lasts until the scope that lent the receiver returns.

Only a closure written at the argument borrows or is lent; the same capture anywhere else — a &sync Fn parameter, a member that is only @crossing, a closure made earlier and handed in — is refused as it always was:

import sysl.buf.{Buf, buf}

struct Scope
    jobs: Buf[&sync Fn() -> unit]

    @scoped(f)
    spawn(*self, f: &sync Fn() -> unit) = self.jobs.push(f)

struct Runner
    n: int

    scope[R](self, body: *Scope -> R) -> R
        var s = Scope(buf())
        val r = body(&s)

        for j in s.jobs do j()
        r

first(ws: []string) -> int
    Runner(0).scope((s) ->
        val f: &sync Fn() -> unit = () -> print(ws[0])

        s.spawn(f)
        0)
but the 'ws' it captures reaches a '[]string', which owns its elements through a count that is not atomic

What it names is a call, once, and the receiver is the *self a call lent:

struct Chan
    open: bool

    @scoped(v)
    run(*self, v: int) -> int = v
'@scoped' names 'v', which is neither a call nor a pointer
struct Chan
    open: bool

    @scoped(g)
    run(*self, f: &sync Fn() -> int) -> int = f()
'@scoped' names 'g', which is not a parameter of 'Chan.run'
struct Chan
    open: bool

    @scoped(f, f)
    run(*self, f: &sync Fn() -> int) -> int = f()
'@scoped' names 'f' twice
struct Chan
    open: bool

    @scoped(f)
    run(self, f: &sync Fn() -> int) -> int = f()
so the receiver is the pointer that call lent, written '*self' — this one takes 'self' by value
struct Chan
    open: bool

    @scoped()
    run(*self, f: &sync Fn() -> int) -> int = f()
There is no empty form

Naming a call, it stands above a method and nowhere else. A free function has no receiver for a call to have lent, and a field is never called:

@scoped(f)
run(f: &sync Fn() -> int) -> int = f()
a free function has no receiver for a call to have lent
struct Chan
    @scoped(f)
    f: int
a field and a variant are never called, and have no parameter to run

@scoped(p) on a pointer — held only while the caller waits

@scoped(p) naming a *T parameter promises that the function may hold the pointer, but only while its caller is suspended or inside the caller’s scope, and lets go of it before the caller’s frame ends. It is how a blocking primitive says so: a kernel’s block parks the address of the caller’s wait record until wake, an awaitable read keeps the caller’s buffer until the device delivers, a scope’s spawn holds the scope until the join. The release happens in a context the compiler cannot see, so it trusts the promise, written once on the primitive — the code that calls it stays annotation-free and may hand it the address of a local. It may stand above any function or method, generic or not, nested or not, and above an impl‘s method or a trait’s default body.

static var idle = 0
static var waiter: *int = &idle

@scoped(slot)
block(slot: *int)
    waiter = slot

wake(v: int)
    *waiter = v
    waiter = &idle

read_sensor() -> int
    var got = 0

    block(&got)
    wake(42)
    got

print(read_sensor())
42

The promise is the primitive’s own, so it does not spread. Its body may store the pointer, but handing it to a callee that keeps it — or capturing it in a closure that is stored — is refused, because that callee promised nothing. A callee whose parameter says @scoped too, or one that only uses the pointer, may be handed it:

static var idle = 0
static var waiter: *int = &idle

park(slot: *int)
    waiter = slot

@scoped(slot)
block(slot: *int)
    park(slot)
'block' says '@scoped(slot)', a promise to let go of 'slot' before its caller's scope ends that only its own body keeps, and it is passed to 'park', which keeps it

It takes a *T or a call, and nothing else; it does not stand above an extern, whose C body holds what @keeps(...) says, nor above a trait’s requirement, which has no body to keep it — each impl writes it above its own method.

@bounded(f) — every run of a callable finishes before the call returns

@bounded(f) above a function or a method promises that every run of the callable f, on whatever domain it runs, finishes before the call returns. @scoped(f) binds what a member runs to the scope that lent its receiver, which outlives the caller’s frame when the caller was handed that scope; the bound @bounded names is the call itself, so it needs no receiver and stands above a free function too. A worker set that runs a job on threads of its own and joins them before returning says it:

import sysl.sync.Atomic
import sysl.threads.scope

struct Spawned
    n: usize

    @bounded(job)
    each(*self, job: &sync Fn(usize) -> unit)
        scope((s) ->
            for w in 1..<self.n
                s.spawn(() -> job(w))
            job(0))

run(on: *Spawned)
    var hits: [4]Atomic[int] = [Atomic(0); 4]
    val v = hits[..]
    on.each((i) -> v[i].add(int(i) + 1))
    print(hits[0].load() + hits[3].load())

var sp = Spawned(4)
run(&sp)
5

A closure written straight at f may capture views and addresses of the caller’s frame, lent to it exactly as a closure written at a @scoped parameter is lent them: its captures need only outlive the call. Only a closure written at the argument borrows; a closure made earlier and handed in, or a parameter that is not @bounded, is held to the crossing rule as it always was. A @bounded parameter may be handed on to another @bounded callee, spawned on a scope the body opens, or held in the receiver’s own field for the call.

It is trusted, like @scoped: the compiler cannot see that every run has finished, and does not check the body for it. What it does refuse is the two ways of breaking the promise in plain sight — answering the callable, and storing it in module storage:

var saved: Option[&sync Fn(usize) -> unit] = None

@bounded(job)
keep(job: &sync Fn(usize) -> unit)
    saved = Some(job)
    job(0)

main()
    keep((i) -> print(i))
but it stores 'job' in module storage

A top-level var of the file a program starts in lasts as long as module storage does — it is a local of the program’s outermost block (Memory) — so storing the callable there is refused too. So is pushing it into a buffer either one holds, since the copy of a Buf that push is handed names the same slots:

import sysl.buf.{Buf, buf}

var jobs: Buf[&sync Fn(usize) -> unit] = buf()

@bounded(job)
keep(job: &sync Fn(usize) -> unit)
    jobs.push(job)
    job(0)

keep((i) -> print(i))
but it stores 'job' in a top-level 'var' of the file the program starts in (it is passed to 'push', which keeps it)

It names a callable — a &sync Fn, a &Fn, a bare arrow or a *extern function pointer — once, and never a parameter @scoped names too. Like @scoped it stands above neither an extern nor a trait’s requirement: each impl writes it above its own method.

There is no @joined

A *T a written @crossing names is kept for good: the thread it reaches may read it after the call returns, and no join the caller writes later takes it back. A thread that borrows the caller’s frame is started on sysl.threads.scope, whose spawn is @scoped(f) above (memory § A thread borrows a frame only inside a scope), so there is no annotation for a pointer held until a handle is joined, and the word is refused:

@crossing(p)
@joined(p)
begin(p: *int) = ()
'@joined' is not an annotation — a thread that borrows its caller's frame is started on 'sysl.threads.scope'

@keeps and @answers on a trait method

A call through a trait object sees no body: which implementation it reaches is decided when the program runs. So the trait’s own declaration of the method is the whole answer to what the call keeps, and it says what an extern‘s does — every parameter is borrowed for the call, and nothing read through one is held past it, unless the declaration says @keeps(p); the answer is storage of the implementation’s own unless it says @answers(p). Every implementation is held to the declaration where it is written, so an implementation that keeps more than its trait says is refused, and told the mark that would let it:

trait Registry
    add(*self, f: &Fn() -> int)

struct Board
    f: Option[&Fn() -> int]

impl Registry for Board
    add(*self, f: &Fn() -> int)
        self.f = Some(f)

var b = Board(None)
val r: *Registry = &b

r.add(() -> 1)
'add' keeps its parameter 'f' (it is stored in 'self.f'), but a method of 'Registry' borrows its parameters for the call

With the mark, every implementation may keep the parameter, and every call through an object hands it only what may be kept — a closure that captures a number, here:

trait Registry
    @keeps(f)
    add(*self, f: &Fn() -> int)

struct Board
    f: Option[&Fn() -> int]

impl Registry for Board
    add(*self, f: &Fn() -> int)
        self.f = Some(f)

main()
    var b = Board(None)
    val r: *Registry = &b
    val k = 41

    r.add(() -> k + 1)

    val g = b.f.unwrap()

    print(g())
42

and one capturing the address of a local is refused at the call:

trait Registry
    @keeps(f)
    add(*self, f: &Fn() -> int)

struct Board
    f: Option[&Fn() -> int]

impl Registry for Board
    add(*self, f: &Fn() -> int)
        self.f = Some(f)

main()
    var b = Board(None)
    val r: *Registry = &b
    var n = 7
    val p = &n

    r.add(() -> *p)
its trait's 'add' declares '@keeps(f)'

@answers(p) lets the answer be made of what p was handed, and a caller then holds the answer to how long p lives, as it does a sysl function’s:

trait Picker
    @answers(p)
    pick(self, p: *int) -> *int

struct First
    n: int

impl Picker for First
    pick(self, p: *int) -> *int = p

leak(o: &Picker) -> *int
    var n = 4
    o.pick(&n)
the address of a local of 'leak'

An unmarked answer may be made of the receiver only, as Rust elides a method’s lifetime: a builder that hands back an object holding what it was handed says so once, in the trait, and every implementation and every call is held to it.

trait View
    @answers(f)
    on_tap(&self, f: &Fn() -> unit) -> &View

Without the mark, an implementation answering f is refused where it is written:

trait Picker
    pick(self, p: *int) -> *int

struct First
    n: int

impl Picker for First
    pick(self, p: *int) -> *int = p

main()
    val o: &Picker = First(0)
    var n = 1

    print(*o.pick(&n))
'pick' answers with what it is handed as 'p', but a method of 'Picker' answers storage of its own unless its declaration says '@answers(p)'

@keeps(self) lets an implementation hold the object the call went through past the call — a view registering itself, say. The object may have been made in the caller’s frame, so without the mark an implementation that keeps self is refused where it is written, and with it every call through an object hands over an object that may be kept. The refusal names the mark, because the trait’s borrowed receiver is its whole cause — it says the same for what the method reads through self and keeps, and for self handed on to a function that keeps it:

trait Widget
    register(&self) -> int

struct Label
    f: &Fn() -> int

impl Widget for Label
    register(&self) -> int
        shown = Some(self)
        self.f()

var shown: Option[&Label] = None

main()
    val w: &Widget = Label(() -> 1)

    w.register()
or let every implementation keep it by writing '@keeps(self)' above the declaration of 'register' in 'Widget'

With the mark on the trait’s method the same implementation is accepted:

trait Widget
    @keeps(self)
    register(&self) -> int

struct Label
    f: &Fn() -> int

impl Widget for Label
    register(&self) -> int
        shown = Some(self)
        self.f()

var shown: Option[&Label] = None

setup(k: int) -> int
    val w: &Widget = Label(() -> k + 1)

    w.register()

main()
    print(setup(1))
    print(shown.unwrap().f())

    shown = None
2
2

Both name the method’s own parameters, each once — @keeps the receiver too, as self — and only a method with a receiver takes them, since nothing else is called through an object. An implementation’s method never carries them: its trait says them for every implementation at once.

trait Sink
    @keeps(q)
    put(*self, p: *int)
'@keeps' names 'q', which is not a parameter of 'put' — its parameters are 'p'

:: — what a type’s own name answers

A type’s name is a type and not a value, so nothing is read from it. What it answers are attributes, written with :: rather than . so they stay out of the member namespace: Color::First cannot be confused with a variant, an associated function, or a member an impl added, and no impl can shadow one by declaring a member of that name.

The name may be written in full, through its module: geo.Point::Kind, a.b.T::Fields and geo.Pair[int]::Name answer exactly what Point::Kind does after import geo.Point, since an import only shortens a reference (modules). A path whose head is a local is a member read, not a type, and is refused as any value on the left of :: is.

Four kinds of type answer them, and each set is fixed and closed.

A simple enum

enum Color: u8
    Red = 2
    Green = 5
    Blue = 9

print(Color::Image(Color::First), Color::Image(Color::Last))
print(Color::Pos(Green), Color::Image(Color::Val(2)))
print(Color::Image(Color::Succ(Red)), Color::Image(Color::Pred(Blue)))
print(Color::Image(Color::Value("Blue")), int(Color::Value("Blue")))
Red Blue
1 Blue
Green Green
Blue 9
writtenistraps
T::First / T::Lastthe first and last variantno
T::Pos(v)a value’s 0-based position in the declarationonly on a value no variant has
T::Val(i)the value at position ion a position past the last, or below zero
T::Succ(v) / T::Pred(v)the neighbouring valueat the last / at the first, and on a value no variant has
T::Image(v)the variant’s name, as a stringonly on a value no variant has
T::Value(s)the value a name stands foron a name no variant has

A value no variant has cannot be built — E(n) checks, and so does reading a bitfield’s enum field — so it arrives only through storage a ptr_cast or C filled. Pos, Succ, Pred and Image stop on one rather than answer for a variant it is not.

Position is not the discriminant, and that distinction is why Pos and Val exist. Discriminants may be explicit, non-contiguous and not zero-based — Green above is position 1 and discriminant 5 — so an ordinal has to be looked up rather than computed. Pos and Val are the two directions of that lookup, and Succ/Pred walk the declaration order rather than adding one. Going the other way, to a value’s discriminant, is the ordinary conversion int(c).

Image and Value are the printed-name pair, and they are what makes a simple enum observable. The type carries no == and no Display of its own, which is why every line above turns a value into a position or a name to look at it. A comparison is written on the discriminants, int(a) == int(b).

A constrained subtype

The other set — First, Last, Valid, Succ, Pred, Range — belongs to a within-ranged integer subtype, and is on the errors and contracts page beside the checking it is the question form of.

The two sets are spelled the same way for the same reason and overlap where the questions overlap, but they are not one set: an enum has Pos, Val, Image and Value because its values are named and unevenly spaced, and a subtype has Valid and Range because its values are a contiguous run of integers.

An integer type

The third set is two attributes wide — Min and Max, the extremes the type can hold. They belong to every member of the iN/uN family, and to the named types that are members of it.

print(u8::Min, u8::Max)
print(i8::Min, i8::Max)
print(int::Min, int::Max)
print(byte::Min, byte::Max)
0 255
-128 127
-2147483648 2147483647
0 255

The open family is why these exist rather than being written out. A program can spell 4294967295 for a u32 and lose nothing; the largest u10000 is 3,011 digits and cannot practically be written at all, so for a wide member the attribute is the only way to name a value the type obviously has. It is also why they cannot be a library table the way C’s UINT8_MAX is — there is no finite set of integer types to tabulate.

print(u3::Min, u3::Max)
print(i5::Min, i5::Max)
print(u64::Max)
0 7
-16 15
18446744073709551615

They are constants taking no argument, so they fold. That is what puts them where no call is admitted — a const initializer, an @assert condition, an array bound:

const LIMIT: u16 = u16::Max
const HALF: u32 = u32::Max / 2

print(LIMIT, HALF)
65535 2147483647

usize answers only a floor across targets. Its width is the pointer’s, so usize::Min is 0 everywhere and usize::Max is whatever the target makes it — a program that prints the latter is reading a fact about the machine rather than about the language.

Min/Max are not First/Last renamed, and asking for the wrong pair is answered by name rather than by a general refusal:

print(u8::First)
'u8' is an integer, not a declared sequence — its extremes are 'u8::Min' and 'u8::Max'. 'First' and 'Last' name the ends of an enum's variants or of a 'within' range, which is a different question

First and Last name the ends of a declared sequence — an enum’s variants, a written range — and an enum’s discriminants may be explicit and non-contiguous, so its first-declared variant need not carry the smallest value. The two questions coincide on an integer and only there. A within-ranged subtype answers both, since there they genuinely agree, and a reader who learned Min on u32 should not find it renamed on a subtype of u32.

And a subtype that narrows nothing has its base’s, because that is what it can hold. First and Last still need a range, which is the same distinction one paragraph up — a declared sequence against what the type can hold:

type Handle = new u16

print(u16(Handle::Max))
65535

That is the case a c type is always in: a measured typedef carries no range, so asking a size_t for its maximum is asking about the integer C said it is. A where predicate is the exception and is refused — it narrows the type without saying to what, so there is no extreme to read off the declaration.

Only integers have them: f64::Max and bool::Max are refused, the float because its extremes are sysl.math‘s business and the boolean because it is not that kind of type.

A type parameter answers them too, from whatever the instantiation bound it to — which is what makes the bounds usable by a library rather than only by a program. Bounded narrowing, integer parsing, saturating arithmetic and a min/max reduction’s identity all want T‘s extreme, and none of them can name it any other way:

widest[T]() -> T = T::Max

val a: u8 = widest()
val b: u16 = widest()

print(a, b)
print(widest[i8]())
255 65535
127

One body serves every width it is instantiated at, and the argument may be written at the call or inferred from what the result is used as.

The parameter carries the type that was written, so a within-ranged subtype answers its own bound rather than its base’s:

type Age = int within 0..150

widest[T]() -> T = T::Max

val a: Age = widest()

print(int(a))
150

This is the one place a transparent subtype and its base give different answers, which is why generics states the rule: everywhere a value flows the two are interchangeable, and a bound is the one thing either can produce that the other cannot hold.

Min and Max are the only two a parameter answers. The rest stay on a written type name:

first[T]() -> T = T::First

val a: u8 = first()

print(a)
'T' is a type parameter, and the only attributes one answers are 'T::Min' and 'T::Max'

The reason is the walk that checks a generic body once, with T standing for itself rather than for any particular type. That walk has to hand back a typed value, and Min and Max both answer in T — so one stand-in is right for both. Valid answers a bool, Pos a usize and Image a string, so admitting those would mean restating each attribute’s result type a second time, in a second place, where the two copies could drift apart.

A derive body’s T is not such a parameter. Its body is read once for each type that names the trait in its derives, with T that type and never standing for itself, so it answers every attribute the type does — T::Image(self) in an arm under if const T::Kind == Kind.SimpleEnum (reflection).

Nothing is asked of T in the signature. A parameter given a type with no extremes is reported at the instantiation that gave it one, and the message names both:

struct P
    x: int

widest[T]() -> T = T::Max

val a: P = widest()

print(1)
'T::Max' needs an integer type, and 'T' is P here

That is the same deferral sizeof(T) takes, and it is what keeps a bound from being something a generic has to declare in order to measure.

A fixed-point type

A fixed-point type answers Min, Max, Small — one unit, 2^-F — and Bits(n), which makes a value from the integer it is stored as, and nothing else:

print(q31::Min, q31::Max, q15::Bits(16384))
-1 0.9999999995 0.5

What has no attributes

A data enum, because its value is a variant plus a payload, so a position, a name and a neighbour are each questions about only half of it:

enum Shape
    Circle(r: int)
    Rect(w: int, h: int)

print(Shape::First)
'Shape::First' needs a simple enum, and 'Shape' carries data

A generic enum, because its name stands for no one value set until it is applied:

enum Box[T]
    Full(v: T)
    Empty

print(Box::First)
'Box' is generic, so 'Box::First' has no single enum to read

And an attribute outside the set is answered by name, since the set is closed:

enum Color: u8
    Red
    Green

print(Color::Nonesuch)
'Color' has no attribute 'Nonesuch'

Reflection queries answer on every type

The sets above belong to one kind of type each. A second family answers on any type, because it asks what the type is made of rather than what a value of it may do, and it is answered while compiling:

writtenanswers
T::Kinda Kind variant — Struct, DataEnum, Int, Opaque…
T::Name, T::BaseNamethe type as written, with its arguments / the declared name alone
T::Targetthe type a reference, a pointer or a slice points at (a type, not a value)
T::Fields, T::Membersthe named fields / every stored member of a struct, tuple or union
T::Variantsthe variants of an enum, simple or data
T::Make(c, p)the variant c of a data enum, built from a payload
T::Annotation[A], f::Annotation[A]an Option of the program’s own @annotation on a type or field

A data enum, which has no First or Pos, is walked through T::Variants instead. The descriptors, for const f in T::Fields, v.(f), f::Type and the rest are on reflection.

@test — a function with a caller nothing else has

A word after @ that is neither test nor tailrec is answered by name rather than as grammar. The set is closed, which is what makes a misspelling an error instead of a marker that quietly does nothing — the built-in annotations are a closed list. A program’s own are declared with @annotation and read by reflection.

The annotation goes on its own line above an ordinary function declaration, which may still be private:

add(a: int, b: int) -> int = a + b

@test
adds_two()
    assert(add(1, 1) == 2, "one and one")

@test("an empty slice has no first element")
private first_of_empty()
    assert(true, "nothing to see")

print(add(2, 3))
5

That program prints 5 and runs neither test, which is the whole of the arrangement: sysl run builds the program, and the tests are for sysl test.

Six forms of the annotation:

writtenmeans
@testan ordinary test, named after the function
@test("a sentence")named by the sentence instead
@test(should_trap)the test passes by stopping the program
@test(should_trap: "past the end")…and the run must have printed that text
@test(timeout: 30)the test may run for 30 seconds, whatever the run’s own limit
@test(ignore: "why")the test is compiled and checked, and not run

They compose, in the order the table gives them — @test("a sentence", should_trap: "past the end", timeout: 30, ignore: "why") is one annotation.

Six, and @test() is not a seventh. An empty argument list would mean what bare @test means, and it is refused rather than accepted as a synonym: it is not a shorter way of saying nothing, it reads as a description that was going to be there and got lost, and a reader who saw it accepted could not tell which.

@test()
subtracts()
    assert(3 - 1 == 2, "two")
'@test' takes a description or nothing at all, and '()' is neither — drop the parentheses, and the function's own name becomes its description

Everything after the ( is answered by the annotation, so a description that is not a string and an argument list left open are told what @test wanted rather than being blamed on the function below.

What a test may be

A test is an ordinary function with a caller nothing else has: no parameters, no result other than a Result[unit, E], not generic. All three are the same requirement from different sides, since the runner calls it with nothing and reads the answer off whether it returned — or, for a test that answers with a Result, whether it answered Ok. They are checked at the annotation, because the function is a perfectly good function and it is @test that made a promise about it:

@test
takes_one(n: int)
    assert(n > 0, "positive")
a '@test' function takes no parameters, and 'takes_one' takes one — 'sysl test' calls it with nothing, so there is nowhere for an argument to come from
@test
generic_one[T]()
    assert(true, "yes")
a '@test' function has no type parameters, and 'generic_one' declares 'T' — a generic is compiled for the arguments a caller fixes, and the runner supplies none
@test
gives_back() -> int = 1
a '@test' function returns nothing or a 'Result[unit, E]', and 'gives_back' returns 'int' — a test's result is whether it came back or failed, so there is nothing to read a value with

A test passes by returning

That is the whole protocol, and it is what lets a test assert in the language it is testing rather than in a framework. A broken require, a bounds violation, an unwrap of a None — each ends the process, and none of them had to know it was running under a test.

A test may answer with a Result[unit, E], so the fallible code it exercises is called with ? just as the program calls it. An Ok passes. An Err fails the test: the run says the error — through its Display, or its Debug where it has only that — and the error return trace under it, and exits with status 1:

parse(s: string) -> Result[usize, string] = if s == "" then Err("empty input") else Ok(s.len)

check(s: string) -> Result[unit, string]
    val n = parse(s)?
    assert(n > 0)
    Ok(())

@test
reads_a_word() -> Result[unit, string]
    check("port")?
    Ok(())

Given check("") instead, the report under the failed test reads

error: empty input
error return trace (origin first):
  main.sysl:1:61 in parse
  main.sysl:4:21 in check
  main.sysl:10:14 in reads_a_word

should_trap inverts the reading, for a test whose subject is the check. With a string it additionally requires that the run printed it, which is what tells a trap from the right trap. A bounds check says what failed before it traps — @test(should_trap: "out of bounds for length 3") asks for exactly that — and so does every other compiler-inserted check, a contract clause with its message: @test(should_trap: "x must be non-negative") matches require x >= 0, "x must be non-negative". Only trap() itself says nothing, so it satisfies should_trap and can satisfy no string — see what stopping looks like.

A test may be written and not run

A test for a defect that is not fixed yet is still worth writing — with the assertion that should hold, rather than one bent to match what the code does today. ignore is how it lands: the function is compiled and checked exactly like every other test, and sysl test reports it without starting it.

parse_arms(n: int) -> int = n - 1

@test(ignore: "the parser drops the second arm")
keeps_both_arms()
    assert(parse_arms(2) == 2, "both arms")

print(parse_arms(2))
1

Checked, not run, is the whole point of the form. An ignored test is held to everything a test is — no parameters, no result, a body that compiles — so it cannot quietly stop being valid code while it waits, which is what a test that was commented out or deleted does. It composes with the rest of the annotation: @test("both arms are kept", should_trap: "past the end", ignore: "…") is a trap test that is waiting, and it is reported as waiting rather than as a run that returned.

The reason is required. An ignored test that does not say what it is waiting for is the one nobody comes back to, so bare ignore — or a reason that is empty — is refused:

@test(ignore)
keeps_both_arms()
    assert(true, "both arms")
'ignore' needs the reason the test is not run, written as a string — '@test(ignore: "the parser drops the second arm")'

Ignoring is something a test is, so there is no @ignore of its own; written above a function it is answered with the spelling above.

A test may be given a limit

Every test sysl test runs has a time limit — the run’s, 300 seconds unless --timeout says otherwise — and a test that outlasts it fails, reading timed out after Ns, rather than holding up every test after it. timeout gives one test a limit of its own, in whole seconds, in place of the run’s: longer for a test that is slow on purpose, shorter for one whose whole point is that it finishes quickly, and 0 for none at all.

import sysl.time.{sleep, millis}

@test(timeout: 2)
answers_promptly()
    sleep(millis(10))

@test("a soak that is meant to take a while", timeout: 0)
soaks()
    sleep(millis(10))

print("built")
built

The test’s own limit beats the run’s in both directions, --timeout 0 included: a run that asked for no limit still stops a test that said timeout: 5, because the test is the one that knows. The limit is a whole number of seconds from 0 to 2147483, the most milliseconds an int counts, and anything else is refused where it is written:

@test(timeout: 1.5)
answers_promptly()
    assert(true, "prompt")
'timeout' needs the seconds the test may run, written as a whole number from 0 to 2147483 — '@test(timeout: 30)'; 0 is no limit at all

A test may find out that it cannot run

ignore is decided before the test starts. Some tests can only find out once they are running: a test that drives a tool which is not installed, or talks to a device that is not plugged in. Such a test calls skip, from sysl.testing, and the runner reports it as skip with the reason it gave:

import sysl.env.get
import sysl.testing.skip

needs_tool(name: string)
    if get(name).is_none() then skip(s"${name} is not set, so there is nothing to talk to")

@test
talks_to_the_board()
    needs_tool("BOARD_PORT")
    assert(false, "not reached on a machine without a board")
running 1 test

board.sysl
  skip  talks_to_the_board  skipped: BOARD_PORT is not set, so there is nothing to talk to

0 passed, 0 failed, 1 ignored — 2ms

A test that printed a note and returned would have been reported ok: a pass that checked nothing. skip makes the run say so on the line a reader looks at last.

A test may call the compiler that is running it

sysl test and sysl run start the program with SYSL in its environment: the absolute path of the compiler doing the running. It is always set, replacing a SYSL the environment already held, because one left by an outer run names the outer compiler. sysl.testing.compiler() reads it and answers "sysl" where it is absent (a built program run by hand), so a test that builds a fixture or asks for --version runs the compiler under test and not whichever sysl the search path finds:

import sysl.process.capture
import sysl.testing.compiler

@test
asks_the_running_compiler_its_version()
    val answer = capture(compiler(), ["--version"]).unwrap()
    assert(answer.status.ok(), "the compiler answers --version")
  • skip is imported, not ambient. It lives in sysl.testing rather than the root module, so a test file that also imports sysl.harness.* — which has a skip of its own, for the on-target runner — is not told the two collide.
  • skip ends the test at once. It returns never, so nothing after it runs, and a helper the test calls may make the call on the test’s behalf, as needs_tool does above.
  • It is not a pass and not a failure. The summary counts it with the ignored tests, it does not change the exit status, and --fail-fast does not stop at it. The row’s word says which kind it was: ignored is the annotation’s, skipped is the test’s own.
  • A failure before it is still a failure. An assert that failed earlier ended the process before skip was reached, so the run is reported as that failure.
  • The test’s @teardown still runs, in a process of its own, as it does after a test that trapped. A teardown that fails turns the row into that failure. A @setup that calls skip skips the test it was setting up.
  • A should_trap test that skips is reported as skipped, since it never reached the thing it was meant to trap on.

How it works: skip prints one control byte, the reason up to the end of its line, and then ends the process with status 0. The runner looks for that byte in what the run printed. Because the exit is clean, a sysl test older than skip reads the test as one that returned and reports ok, which is what such a test was before skip existed, rather than reporting a failure.

A test has one caller, and the program is not it

run_it()
    adds_two()

@test
adds_two()
    assert(true, "yes")
'adds_two' is a '@test' function, which 'sysl test' calls and nothing else does — every other build leaves it out, so this call would have no definition to reach. Work two tests share belongs in an ordinary function they both call

What is dropped, and when

Every build but sysl test drops the tests after the parse and before analysis — sysl run, sysl build, sysl build-c, sysl emit-llvm and sysl build-lib alike, for a program’s own files exactly as for a library’s and a dependency’s. What goes is every @tests file whole, and every @test function and hook wherever it was written. A syntax error in a test still stops the build, since every file is parsed first; everything after the parse — names, imports, types, capabilities — is asked of a test by sysl test alone, which is the one command that runs it.

So a test’s imports are never read by the program that ships. A @tests file may import a dev dependency, which only sysl test fetches, and the program still builds:

main()
    print("built")

@test
checks_nothing_yet()
    val unused: NoSuchType = 0
built

The @test above names a type that does not exist, and sysl build says nothing about it: a broken test is reported by sysl test, the build that would run it. The module’s capability rules reach its tests there like they reach any other member — with the one difference that a @tests file states its own, since that file is what every other build drops.

This is what lets a test sit beside what it tests: a program’s tests do not run when it runs, and nothing they need is asked of it.

An ordinary helper only a test calls leaves too, because with the test gone it is unreachable and pruning notices; a helper the program also calls stays, because the program still calls it.

What a test build keeps regardless is the three definitions nothing in the program names: an interrupt handler, an @export, and a @section definition (below). Each is entered from somewhere no walk over the program can see — the processor, a caller outside this compilation, and a linker script gathering a named section — so each is a root wherever the walk begins. A test build swaps the roots, putting the tests where the entry point was, and these three are roots there too. They have to be: leaving them out would not make them reachable from the tests instead, it would make them reachable from nothing.

A freestanding image is the one place an @export is not a root. The compiler still emits it, but the image is linked from its entry, a function at a time, and the linker drops every function nothing the entry reaches calls — an export nobody calls included, with the thread-local storage and the rest of what only it touched. An archive keeps every export, since what links it decides. An export the image must keep though nothing in it calls it — a routine a debugger or a boot ROM jumps to by name — is given a @section (below): a placed definition is marked for the linker as well as the optimizer, and stays.

A destructor is entered from somewhere no walk sees as well — the release hook generated from a payload type — but it is kept by its type rather than as a root: wherever anything kept holds a value of the type, in a test build exactly as in a program, the destructor its release calls is kept with it.

That matters most to a package, whose only build is its own suite. A test build dropping an export would link, pass, and quietly go without the surface the package exists to provide.

Why the drop comes before analysis. Analysis is not a passive reading: a test naming Buf[int] creates the whole of Buf at int, and that instantiation is an ordinary library function afterwards, with nothing in it recording which declaration asked for it. Dropping the test from an analyzed program would drop the test and keep everything it caused — shipping, in an artifact, instantiations no caller of the library ever asked for. And a test’s imports would be resolved against a build that never fetched what they name.

The line falls between parsing and analysis. Every source is parsed before the drop, so a syntax error in a test still stops a build or a build-lib. What a test no longer gets there is everything after the parse — name resolution, types, visibility, capabilities, the @test well-formedness rules above, generic instantiation, and the duplicate-@export check. So a test that is well-formed text and wrong in every other way builds clean, and its real errors are reported by sysl test (sysl test --std for the standard library’s), which is where tests are run. The refusals this page shows for tests are sysl test‘s. That includes an ordinary function calling a @test or naming what a @tests file declares: sysl test says why, and any other build reports a name that is not there — since, in that build, it is not.

That last one is worth naming because it is the case where “everything after the parse” is easy to apply to only half of a build. An @export in a @tests file names a symbol this build is about to discard, so it can collide with nothing — and a check that read the tree before the drop would refuse a program over a name that was never going to be emitted.

Read the other way, sysl test is the build where such an export is a definition, and it is held to every rule on the FFI page accordingly — private, variadic, generic, a parameter C has no declaration for, and two exports claiming one symbol. Each of those asks about the symbol table the build in hand emits, so which tree is read is the whole of the difference between the two commands.

The one rule on that page a test build is not held to is module storage, and for a reason that is about the build rather than about the tree: a test binary has an entry point of its own, so it fills its own storage before the first test runs. That refusal belongs to the artifact a C project links on a freestanding target, which is the one build with nowhere to fill it.

@tests — a file of scaffolding

Pruning answers for a program. It does not answer for a library, which has no main to lower outwards from, so every public declaration is a potential entry and all of them are emitted. A helper only a test called would ride into the artifact and be advertised out of it, nameable by everything that links the library. Nothing about the declaration says it is scaffolding, and nothing could — it is an ordinary function, which is the point of it.

So the file says it, in its header, beside the capability clauses:

@tests

fixture() -> int = 6 * 7

@test("the fixture is the answer")
the_fixture_holds() =
    assert_eq(fixture(), 42)

It is @tests and not @test because the two say different things: @test names something the runner calls, and this names something no build but the runner’s keeps. One word for both would read as though the file were itself a test.

Two rules, and either alone would be unsound. Every build but sysl test drops everything such a file declares — and nothing outside a test may name any of it. Without the second, a program that called a helper would compile here and fail at the link, with a message about a missing symbol rather than about the line that named it.

The restriction is stated over the referring declaration, not over the file it sits in. So a @test function may name scaffolding wherever it was written — which is what keeps a test able to sit beside what it tests — and another @tests file may, and nothing else may:

@tests

fixture() -> int = 6 * 7

print(fixture())
error: 'fixture' is declared in a file that said '@tests', so it is there for the module's tests and no build but 'sysl test' keeps it — only another such file, or a '@test' function, may name it

A closure counts as the body it was written in. It is lowered to a function of its own under a name nobody wrote, so on its own terms it belongs to no file — but what decides is where it sits, not what it ends up called. A lambda inside a test may name the test file’s own scaffolding, and so may a bare function name, which is the same thing written shorter:

@tests

fixture() -> int = 6 * 7

apply(f: &Fn(int) -> int, n: int) -> int = f(n)

@test("the fixture is the answer, reached through a callback")
the_fixture_holds() =
    assert_eq(apply(v -> fixture() + v, 0), 42)

It goes when the file goes, too, which is the half that makes the naming safe: the program that ships carries no body for it.

An impl block sits in one only for a type that same file declares. An impl declares no name; it fills a slot in a method table, which the rest of the program reads without naming anything. Kept in a test build and dropped everywhere else, an impl for a type the program has would mean a trait answering one way while the tests ran and another way in the program that ships, so it belongs beside the type. A type the @tests file declares itself has no such second answer — no other build has the type at all — so its impl may sit beside it, and that is how a test gets a stand-in to hand to code written against a trait:

@tests

trait Area
    area(self) -> int

    described(self) -> string = s"area ${self.area()}"

measured(s: &Area) -> int = s.area()

struct Square
    side: int

impl Area for Square
    area(self) -> int = self.side * self.side

@test("a stand-in answers through the trait")
a_square_measures()
    val s: &Area = Square(3)

    assert_eq(measured(s), 9)
    assert_eq(s.described(), "area 9")

Where the trait is declared does not enter into the first half of the rule: a trait of the module and a trait of the test file are equally fine for a type the file declares. The other half is a trait the file declares: its impl may be for any type — a builtin, a generic type, another file’s struct, a blanket impl[T] … for T — because no other build has the trait, so there is no second answer for the block to disagree with. A test file can give the types it tests a helper trait of its own:

@tests

trait Approx
    near(self, other: Self) -> bool

impl Approx for f64
    near(self, other: f64) -> bool = self - other < 0.5 && other - self < 0.5

agree[T: Approx](a: T, b: T) -> bool = a.near(b)

@test("a trait of the file's own, implemented for a builtin")
floats_are_near()
    assert(1.0.near(1.2))
    assert(agree(1.0, 1.1))

The trait and its impl go with the file: a library built from the module exports neither, and no code outside the test build can name them. What is still refused is an impl of any other trait — the module’s, another module’s, or one another @tests file declares — for a type the file did not declare: one of the module’s, a builtin, a blanket impl[T] … for T, and a type another @tests file declares, since which files a build keeps is decided file by file:

@tests

impl Hash for int
    hash(self) -> u64 = 1
error: an 'impl' block in a file that said '@tests' has to be for a type that file declares, or of a trait that file declares, and 'int' is not declared in it — a test build would keep this block and every other build would drop it, so a trait would answer one way while the tests ran and another way in the program that ships. Write the block beside the type's own declaration, or declare a type for the tests in this file

A closure is the one exception the compiler makes for itself, and it is not a hole: the table it writes is dropped with the closure.

Whatever the file writes goes with it, so whatever it writes may name its scaffolding. A method of a type the file declares, written inline or in that type’s impl, is as much the file’s as a free function is, and may call the file’s helpers:

@tests

helper() -> int = 41

struct Probe
    n: int

    bumped(self) -> int = helper() + self.n

impl Probe
    twice(self) -> int = helper() + helper() + self.n

@test("a stand-in's methods call the file's helper")
probe_bumps() =
    assert_eq(Probe(1).bumped(), 42)
    assert_eq(Probe(0).twice(), 82)

A @tests file of declarations and bindings is never where a program starts, even a root file with no module header: no build but sysl test keeps it, so it cannot be the body a script runs. Its vars are the module’s storage, and a test reading one is a test like any other:

@tests

var shared: int = 11

@test("a test reads the file's storage")
reads_it() =
    shared += 1
    assert_eq(shared, 12)

Every test runs or is refused by name. A @test or a hook written in the file a script starts in, reading one of that script’s bindings, would be nested into the script’s body where the runner cannot reach it — so it is refused, and marking the binding static makes it the module’s:

var n = 1
print(n)

@test("reads")
reads() =
    assert_eq(n, 1)
error: 'reads' is a '@test' that reads a binding of the body of the file this program starts in, so it would be nested inside that body where the test runner cannot reach it — mark what it reads 'static', which makes it the module's

It stops at the package boundary. A package is compiled from source and a library arrives as an artifact whose test files were never encoded, so a rule that let the reference cross would compile against one and fail against the other. A test-support library meant to be imported is therefore ordinary code that ships, and is a different thing from a file of scaffolding inside a package.

A build that depends on a package does not read that package’s @tests files at all — not analyzed, not even parsed, the header being enough to know one. One written for a newer compiler, or against a dev dependency the consumer never fetches, stops nothing but that package’s own sysl test.

An import of a name, or of a whole module, that only such a file declares is refused for what it is: the message names the @tests file and says a dependent never sees a package’s @tests files, rather than that the package declares no such name. What is meant to be imported goes in ordinary code that ships.

The runner

sysl test <path>
sysl test <path> --filter <text>
sysl test <path> --fail-fast
sysl test <path> --jobs <n>
sysl test <path> --timeout <seconds>

One build, one process per test. The tree is compiled once, into a binary whose entry point takes a test’s name and runs that test alone — the program’s own statements and its main are not run, though its module-level vals are still filled, since a test reads a module’s storage like any other function. The runner then starts that binary once per test.

The tests are the named tree’s own. A dependency’s modules are compiled in, since the tree calls them, and its @test functions are not — a package’s tests are run in the package’s repository, by sysl test there.

The process per test is not a cost being tolerated; it is the mechanism. A test that fails does so by ending its process, so a run that shared one would report the first failure and nothing after it. It is also what lets the tests run several at once — as many as the machine has processors, or --jobs <n> — with nothing shared between them that a run of one at a time did not share; the report waits for each row’s turn, so it reads the same either way (the CLI).

Exit status is 0 if and only if every test that ran passed. A tree with no tests, and a filter that matched none of the tests there are, both exit 0 and say which happened.

A process that does not come back in time is stopped, and the run goes on. Each test’s process is started in a process group of its own and given its limit (timeout, or the run’s --timeout, 300 seconds by default); past it, the process and everything it started are ended, the row reads timed out after Ns with what the test had printed underneath — a test’s output is written a line at a time, so a stopped test still has it — and the next test starts. In the other direction, a test process watches the runner that started it and ends itself and its group once the runner is gone, so a run stopped by hand, or killed outright, leaves nothing running behind it.

An ignored test is reported, never left out. It starts no process — not its own, and not its module’s @setup or @teardown — and its row says why in place of a time. The summary counts it, so a run that ignored something says so on the line a reader looks at last:

running 2 tests

parser.sysl
  ok    holds            1ms
  skip  keeps_both_arms  ignored: the parser drops the second arm

1 passed, 0 failed, 1 ignored — 1ms

A test that skipped itself gets the same skip row, reading skipped: where an ignored test’s reads ignored:, and the summary counts it in the same figure.

Where nothing was ignored the summary is N passed, M failed exactly as it always was. A filter selects an ignored test like any other and reports it as ignored, and an ignored test fails nothing: it does not change the exit status, and --fail-fast does not stop at it.

The hooks a module may write

Four more annotations, and none of them names a test — each names a function sysl test calls around the ones it does. @setup runs before every @test in the module and @teardown after it; @setup_all runs once, before the module’s first test, and @teardown_all once, after its last. A module may declare at most one of each. There is no way to nest them and no way for one test to opt out of a hook the rest of the module has.

@setup
opens_the_log()
    print("setup")

@setup
also_opens_it()
    print("setup, again")
a module writes at most one '@setup', and this one already has one — 'opens_the_log'

A second @setup in one module is refused, and the message names both the function already declared and the one that repeats it — the same shape as two @tests sharing a name, except that here there is only ever room for one.

The process per test, which “The runner” above already made the mechanism rather than a cost, is what shapes everything about these four. sysl test starts one process per test, so a hook that runs inside that process sees what the process saw, and a hook that runs in a process of its own sees nothing the test process held:

  • @setup runs in the test’s own process, immediately before the test. A module-level val it sets is filled by the time the test reads it, the same as any other statement that ran first would leave it.
  • @teardown runs wherever the test left off: in the test’s own process if the test returned, or in a process of its own if the test trapped — a trapped process is gone, so nothing after it can run inside it. What a @teardown can rely on releasing is therefore only what outlives a process — a file, a pid, a socket path — never a variable, because half the time there is no process left holding one.
  • @setup_all and @teardown_all are each their own process invocation, run once, and share nothing with the tests they bracket except what outlives a process — the same kind of file, pid or path a @teardown is left with.

A @setup fault fails the tests it guards without running them, and names the setup function rather than an assertion a test never reached; a @setup_all fault reaches every test in the module, so none of them run. A @teardown or @teardown_all fault is a counted failure of its own, alongside whatever verdict the test it followed already reported.

Worked example: a module whose tests share a fixture across the process boundary. @setup_all writes the fixture once, to a file; each test reads it back; @teardown_all removes it. The file is the whole of what crosses a process boundary — which is the reason a @setup_all exists at all, rather than a @setup repeating the write once per test:

import sysl.fs.*

writes_the_fixture()
    write_text("fixture.txt", "ready").unwrap()

removes_the_fixture()
    remove_file("fixture.txt").unwrap()

@setup_all
starts_the_fixture()
    writes_the_fixture()

@teardown_all
stops_the_fixture()
    removes_the_fixture()

@test
reads_the_fixture()
    assert(read_text("fixture.txt").unwrap() == "ready", "fixture ready")

writes_the_fixture()
print(read_text("fixture.txt").unwrap())
removes_the_fixture()
ready

assert and panic

Both are ordinary functions in the standard module:

panic(msg: string) -> never
    eprints("panic: " + msg + "\n")
    abort()

assert(cond: bool, msg: string)
    if !cond then panic(msg)

They exist because require is a promise about a call, checked on entry, and a test’s fifth statement has no contract to hang a claim on — the contract was about the arguments, four statements ago.

They stop the program the way unwrap does — a line on standard error naming what happened and where, then abort (what stopping looks like) — rather than through the trap instruction, because a check a program makes is one the compiler cannot see, and the message is the whole point of it. (The library’s own version streams the message rather than building a string, so it is callable from a @no_alloc module.) The message is required rather than defaulted, because the condition’s source is not available to print and a failure saying only “assertion failed” sends its reader looking for which one.

trap — the instruction without the message

trap() is the other half: it stops the program at once with the target’s trap instruction and says nothing. It answers never, so a function may end in it with no value to return; it needs no capability, allocates nothing and is legal under @no_alloc and on every freestanding target. Reach for it where a message cannot be written — a bare-metal abort, a fault handler — and for assert or panic everywhere a reader would want to be told what broke.

@no_alloc
halt() -> never = trap()

pick(n: int) -> int
    if n > 0 then return n
    halt()

print(pick(4))
4

Like a compiler-inserted check it runs no defer and flushes nothing, and a should_trap test passes by it.

@assert — a condition settled while compiling

const capacity: usize = 512

@assert(capacity == 512, "the protocol fixes this")

print(capacity)
512

The condition is a constant expression, folded by the machinery a const initializer already goes through — so it may name constants, sizeof, alignof, offsetof, the arithmetic and comparisons over them, and an if or match that chooses between them, including a constant declared below it, and call any function whose body can run while compiling (declarations.md § Calls where a type is read):

sq(n: int) -> int = n * n

@assert(sq(3) == 9, "the square of three")

print(sq(3))
9

A true one emits nothing at all. A false one stops the compilation, quoting the message:

@assert(1 == 2, "arithmetic broke")

print(1)
assertion failed: arithmetic broke

The message is optional and is the reader’s own, because they know what the number means — that a struct matches its C counterpart, that a table is the size a protocol fixes — where the expression alone says only that two numbers differ.

The message is any string the compiler can work out while compiling, not only a literal: an interpolation over constants, a call whose body runs while compiling, and — inside a generic or a derive body — what a type answers, T::Name or a field’s f.name:

const WIDTH: int = 3

@assert(WIDTH % 2 == 0, s"a width of $WIDTH is not even")

print(WIDTH)
assertion failed: a width of 3 is not even

It is worked out only once the condition has come out false, so a message that could not be computed costs nothing while the condition holds:

const tenth(n: int) -> string = s"${10 / n}"

@assert(1 == 1, tenth(0))

print("the message was never worked out")
the message was never worked out

Where the condition is false and the message cannot be worked out — it traps, reads storage filled at run time, or is not a string — that is what is reported, in words that say the assertion failed:

const tenth(n: int) -> string = s"${10 / n}"

@assert(1 == 2, tenth(0))

print(1)
the message of this failed '@assert' traps while it is being compiled: integer division by zero
@assert(1 == 2, 42)

print(1)
the message of this failed '@assert' is of type 'int', and a message is a 'string'

A failed comparison also says what each side came out as. The compiler folded both in order to decide the condition, so it has the numbers at the moment it reports that one of them is wrong, and withholding them would leave you editing the literal and rebuilding to find out. A side you wrote as a literal is not repeated back — it is on the line above the message — so sizeof(FRect) == 16 reports only the left; where both sides are computed, both are named. A condition that is not a comparison has nothing to add, since the thing that came out false is its only operand.

It is not require. A require is a runtime precondition: it is compiled, it branches, and it traps when the program reaches it. This is settled while compiling and reaches the binary as nothing. A condition it cannot settle is refused rather than deferred — and a call that reaches run-time storage, a var for one, is the line, since only a function holding what exists while compiling can run then:

var seed: int = 3

f() -> int = seed

@assert(f() == 3)

print(1)
has to be a constant expression

In a body, per instance

@assert may stand in a body as well, and there it is checked for each instance of a generic and in each unrolled copy of a for const — against the types and values that instance was given, which is the only moment sizeof(T) is a number. Inside an if const arm it is checked only where that arm is taken, which makes it a compile-time error that a generic raises on purpose:

ring[T, const N: usize]() -> usize
    if const N > 64
        @assert(sizeof(T) * N <= 4096, "a ring this large belongs on the heap")
    N

print(ring[u8, 100](), ring[u64, 10]())
print(ring[u64, 1000]())
assertion failed: a ring this large belongs on the heap — the left side is 8000 — where N = 1000

The first line compiles: u8 times 100 is 100 bytes, and ring[u64, 10] never takes the arm.

The message is worked out in the instance’s terms too, so it can name the type that was given:

struct Big
    a: i64
    b: i64

fits[T]() -> int
    @assert(sizeof(T) <= 8, s"'${T::Name}' is wider than a register")
    1

print(fits[i32]() + fits[Big]())
assertion failed: 'Big' is wider than a register — the left side is 16 — where T = Big

Checking a C struct’s layout

This is what it was built for. sysl lays a struct out in declaration order and is C-compatible by construction, but from inside sysl that claim cannot be checked: sizeof reports what sysl laid out, not what the header says, so comparing the two is a tautology.

Where the C side is __attribute__((packed)) or was declared at a boundary, @packed and @align(n) further down this page are how the sysl side says the same thing — and the check below is what proves the two agree rather than merely look similar.

It stops being one when both sides name the same number. The C half goes in a .c beside the sysl, which is compiled with it and for the same target, so it reads the headers of the machine being built for:

#include <stddef.h>
_Static_assert(sizeof(struct pair) == 8, "struct pair size moved");
_Static_assert(offsetof(struct pair, b) == 4, "struct pair.b moved");

and the sysl half pins what sysl laid out:

struct Pair
    a: i32
    b: i32

@assert(sizeof(Pair) == 8, "Pair must match struct pair")
@assert(offsetof(Pair, b) == 4, "struct pair.b moved")

print(sizeof(Pair), offsetof(Pair, b))
8 4

Neither half finds the other’s mistake, which is why both are written: the .c catches a header that moved, and the @assert catches a struct that was transcribed wrong.

Pin every field you read, not only the total. A size catches a field that changed width and one that was added. It says nothing about order — so two same-width fields transposed in the mirror leave the total unchanged, and the size assertion on both sides passes while every read is off by the distance between them:

struct Pair
    b: i32
    a: i32

@assert(sizeof(Pair) == 8, "Pair must match struct pair")
@assert(offsetof(Pair, b) == 4, "struct pair.b moved")

print(1)
assertion failed: struct pair.b moved — the left side is 0

That is the failure the whole pairing exists to prevent, arriving through the half that was not checked. offsetof(T, field) closes it: it takes a type and a field name, and answers where the field starts in bytes. A field the struct does not have is refused by name rather than reported as a condition that would not fold.

Inside a generic, where it is settled once per instantiation

A generic’s interesting facts are not properties of its declaration — they are properties of each set of arguments it is compiled for. sizeof(T) has no width until something chooses a T, so an assertion about one is settled at every instantiation rather than once:

slab[T](x: T) -> usize
    @assert(sizeof(T) >= sizeof(*u8), "a free block has to hold the link through it")
    sizeof(T)

print(slab(1u64))
8

An argument the claim does not hold for stops the compilation, and the report names which one asked — the mistake is at the call that chose the type, while the sentence explaining why is at the declaration, so a message carrying only one of the two sends its reader to the wrong file:

slab[T](x: T) -> usize
    @assert(sizeof(T) >= sizeof(*u8), "a free block has to hold the link through it")
    sizeof(T)

print(slab(1u8))
assertion failed: a free block has to hold the link through it — the left side is 1 and the right side is 8 — where T = byte

This is the check require cannot make. A precondition over sizeof(T) written as a require is a runtime branch for a fact that was settled at the call: a container instantiated at a type too narrow compiles, ships, and traps the first time anybody uses it. The slab package was written that way before this landed and is the worked example of the difference.

A value parameter is bound the same way and so is checked the same way, which is what lets a generic hold its caller to a bound on a length:

scratch[const N: usize](xs: [N]int) -> usize
    @assert(N <= 4, "the scratch buffer is a stack array")
    N

var small: [2]int = [1, 2]

print(scratch(small))
2

A generic nothing instantiates is not checked, and that is the same deferral an array bound already lives under: [sizeof(T)]u8 is a well-formed length nobody can name until a T is chosen, and the width is not wrong there — it is not being measured yet. The claim is settled at the first call, which is the first moment there is anything to settle. A condition that could never fold is still refused inside a generic, exactly as it is outside one.

@no_alloc on a function — one entry point that makes no heap storage

In a file’s header @no_alloc gives the allocator up for the whole module (modules). Above one function or method it gives it up for that definition alone, checked by the same two rules through every call the body makes: what the body itself makes is refused at the outermost allocation, and a call that reaches a function that allocates is refused at the smallest piece of the body that still arrives there. It is the form for a module that has one path that must not allocate beside others that must — an interrupt entry beside the system calls that share its file:

boxed(n: int) -> &int = n

@no_alloc
on_irq(n: int) -> int = n + 1

print(on_irq(1), *boxed(2))
2 2

boxed allocates and is not refused: the promise is on_irq‘s, not the module’s. When the promised function reaches an allocation the refusal names the function that made the promise, what the far end makes, and the calls between the two — the far end is somebody else’s code, and the line to change is as often in the middle of the chain as at either end:

boxed(n: int) -> &int = n

middle(n: int) -> int = *boxed(n)

@no_alloc
on_irq(n: int) -> int = middle(n) + 1

print(on_irq(3))
which makes a reference, by the call path 'on_irq' → 'middle' → 'boxed', and 'on_irq' declared '@no_alloc'

What the body makes on its own line is refused the same way the module’s clause refuses it, naming the function instead of the module:

boxed(n: int) -> &int = n

@no_alloc
shown(n: int) -> usize = n.to_string().len

print(shown(42))
the string a value renders as needs an allocator, and 'shown' declared '@no_alloc'

A closure handed on as a &Fn makes nothing when nothing keeps it past the frame. Its box is built in the frame of the function that writes the closure — the header a heap box has, in a slot of the frame — so a callee that only calls it, or takes a share and gives it back, allocates nothing (closures):

call(f: &Fn() -> unit) = f()

@no_alloc
counted() -> int
    var n = 0

    call(() -> n += 1)
    call(() -> n += 1)
    n

print(counted())
2

One a callee keeps, one returned, one kept past a turn of the loop that builds it, and a &sync Fn still go on the heap, and are refused here at the closure:

var later: Option[&Fn() -> int] = None

install(f: &Fn() -> int)
    later = Some(f)

@no_alloc
setup()
    install(() -> 1)

setup()
a reference needs an allocator, and 'setup' declared '@no_alloc'

Directly under the header the word is still the module’s clause. The header is read first, and blank lines do not end it, so a file whose first declaration is the promised function has the module’s clause instead; an import or another declaration above the function is what makes it the function’s. A function in a module that already declared @no_alloc is held to the module’s clause, which says everything the annotation would. The module’s clause binds its own files and never its @tests files (a @tests file states its own capabilities): a test may use to_string, f-strings and Buf, while the module’s functions stay allocator-free when a test calls them.

A method may carry it — it lowers to a function like any other and is held the same way. A function declared inside another may not: it is a closure by another spelling, and what it captures is boxed by the function around it, outside the body the promise would be written on.

outer(n: int) -> int
    @no_alloc
    inner(k: int) -> int = k + n

    inner(1)

print(outer(3))
'@no_alloc' holds a top-level function or a method, and 'inner' is declared inside another function

Above anything that is not a function — a struct, a binding, an extern — it is the module’s word written in the wrong place, and the refusal says so before naming the other reading:

f() -> int = 1

@no_alloc
struct P
    x: int

print(f())
Above a function or a method, '@no_alloc' holds that one definition instead, and this declares neither

A generic keeps the promise twice: as it was written, and at every instance. The body it was written as is held first, and what it makes whatever the type — the n.to_string() above — is refused once, naming the declaration, however many instances the program makes. A method of a generic struct and a method of a generic impl are held the same way:

f() -> int = 1

@no_alloc
shown[T](x: T) -> usize = 42.to_string().len

print(shown(1), shown(true))
the string a value renders as needs an allocator, and 'shown' declared '@no_alloc'

What depends on the type is only there to see once a type is chosen: a call through a bound reaches a different implementation at each instance. Each instance is held to the promise as well, and the refusal names the instance that breaks it; the instances that keep it are not mentioned:

trait Size
    size(self) -> usize

impl Size for int
    size(self) -> usize = 8

impl Size for bool
    size(self) -> usize = self.to_string().len

@no_alloc
measured[T: Size](x: T) -> usize = x.size()

print(measured(1), measured(true))
this reaches 'bool.size', which makes the string a value renders as, and 'measured' declared '@no_alloc' (instance 'measured.bool')

This is the function’s own promise, not the module’s: under a module’s @no_alloc clause a generic still answers only for the body it was written as (modules), since the type an instance is made at is chosen by somebody else’s code.

@no_block and @blocks — a path that must never wait

An interrupt handler may take a completion and wake a thread, and must never sleep: sleeping there switches away from an exception frame with the interrupt still unacknowledged. A real-time audio callback is held to the same rule by a deadline rather than by a machine. @no_block is that promise, and @blocks marks what breaks it. Above a function or a method, @no_block says the definition never waits; in a file’s header it says it of every body in the module. Either way it is checked as @no_alloc is, through every call the body makes: a call that reaches a declaration marked @blocks is refused at the smallest piece of the body that still arrives there.

struct WaitQueue
    n: int

    @blocks
    sleep(*self) = self.n += 1

read_sector(q: *WaitQueue) -> int
    q.sleep()
    1

@no_block
on_irq(q: *WaitQueue) -> int = read_sector(q) + 1

var q = WaitQueue(0)
print(on_irq(&q))
this reaches 'read_sector', which blocks through 'WaitQueue.sleep', and 'on_irq' declared '@no_block'

The refusal names the call the reader wrote and, after through, the declaration it arrives at — the line to change is the first, and the reason is the second. A body that keeps the promise compiles, and a sibling beside it may wait as much as it likes:

struct WaitQueue
    n: int

    @blocks
    sleep(*self) = self.n += 1

@no_block
take(n: int) -> int = n + 1

drain(q: *WaitQueue) -> int
    q.sleep()
    q.n

var q = WaitQueue(0)
print(take(1), drain(&q))
2 1

Whether something waits is declared, not inferred. A C function’s waiting is invisible to the compiler, and a sysl loop that spins until another thread moves cannot be told from a search, so @blocks is written where the waiting is: above a function, a method, or an extern whose C call waits.

@blocks
extern "usleep" c_usleep(us: u32) -> int

@no_block
nap() -> int = c_usleep(10)

print(nap())
this reaches 'c_usleep', which blocks, and 'nap' declared '@no_block'

The standard library marks its own waiting operations: sysl.posix.time‘s sleep and nanosleep, Mutex.with and Mutex.lock_raw, Thread.join, Channel.send and Channel.receive, sysl.net‘s Socket.connect, accept, send, send_all and recv and UdpSocket.send, send_to, recv and recv_from, resolve and resolve_passive (a name waits on DNS), the opening of a file (a FIFO waits for its other end), Child.wait, and the hooks a blocking read or write of a descriptor, a file or a socket goes through — so a promised body reaches none of them, however many calls away. block_on, which drives a task to its end on the calling thread, has no declaration to mark and counts as blocking all the same. A generic is named as it was declared, since the type argument says nothing about whether it waits:

import sysl.threads.Mutex

f() -> int = 1

@no_block
guarded(m: *Mutex[int]) -> int = *m.lock_raw()

print(f())
this reaches 'sysl.threads.Mutex.lock_raw', which blocks, and 'guarded' declared '@no_block'

A call through a trait object reaches the implementation the body erased into it, as for @no_alloc. try_with, try_lock_raw, try_send and try_receive never wait and are not marked. A spin lock is not marked either: it waits only as long as another core holds it, which is what an interrupt handler’s own locks are made of.

A generic that promises is held as @no_alloc holds one: the body it was written as, and then each instance, so a call through a bound that one type answers by waiting is refused at that instance:

trait Poll
    poll(self) -> int

impl Poll for int
    poll(self) -> int = 0

impl Poll for bool
    @blocks
    poll(self) -> int = 1

@no_block
ready[T: Poll](x: T) -> int = x.poll()

print(ready(1), ready(true))
this reaches 'bool.poll', which blocks, and 'ready' declared '@no_block' (instance 'ready.bool')

The rules for where it stands are @no_alloc‘s. Directly under the header the word is the module’s clause, and a function there answers to it; a method may promise it; a function declared inside another may not, being a closure. @blocks takes no arguments and marks a function, a method or an extern function and nothing else — an extern variable is read rather than called:

f() -> int = 1

@blocks
struct P
    x: int

print(f())
'@blocks' says a call may wait, so it marks a function, a method or an 'extern' — and this declares none of them

The two above one definition contradict each other:

f() -> int = 1

@blocks
@no_block
g() -> int = 2

print(g())
'@blocks' says this definition may wait and '@no_block' that it never does

Waiting is conduct, not a facility. No machine takes it away, so @no_block has no requires beside it the way @no_alloc has requires heap, and @requires(block) and @needs(block) are refused:

@needs(block)
f() -> int = 1

print(f())
'block' is what a module does, not something a machine has, and no machine takes it away

@no_recursion — a path that never calls itself again

Flight software, a kernel’s interrupt path and every safety-critical rule set forbid recursion for one reason: a function that can reach itself again before it returns has a stack depth nobody can bound by reading it. @no_recursion is that promise. Above a function or a method it says no call the definition makes, however many calls away and in whoever’s code, ever arrives back at a function already on the stack; in a file’s header it says it of every body in the module. A cycle is refused at the call that begins the way to it, and the refusal names the cycle:

f() -> int = 1

@no_recursion
fact(n: int) -> int = if n <= 1 then 1 else n * fact(n - 1)

print(fact(5))
this call recurses by the cycle 'fact' → 'fact', and 'fact' declared '@no_recursion'

Any cycle counts, not only a function calling itself. Where the promised body reaches a cycle it is not on, the refusal names the call the body wrote, the way from it to the cycle, and the cycle:

f() -> int = 1

even(n: int) -> bool = if n == 0 then true else odd(n - 1)
odd(n: int) -> bool = if n == 0 then false else even(n - 1)
middle(n: int) -> bool = even(n)

@no_recursion
top(n: int) -> bool = middle(n)

print(top(4))
this reaches 'middle', which recurses by the call path 'middle' → 'even' and the cycle 'even' → 'odd' → 'even', and 'top' declared '@no_recursion'

A body that keeps the promise builds, and a sibling beside it may recurse as it likes:

fact(n: int) -> int = if n <= 1 then 1 else n * fact(n - 1)

@no_recursion
square(n: int) -> int = n * n

print(square(3), fact(4))
9 24

A call whose target is decided at run time is followed to everything it could reach, since a cycle missed would be a promise broken in silence:

  • a call through a trait object, a &Fn closure among them, reaches every implementation of that method in the program — the value behind it is chosen wherever it was erased, often in a body the promised one never reaches;
  • a call through a function pointer reaches every function whose address the program takes at that pointer’s type;
  • taking a function’s address is a call of it, since whoever is handed the address — a C function among them — may call it while the body that took it is still on the stack.
trait Shape
    area(self) -> int

struct Sq
    n: int

impl Shape for Sq
    area(self) -> int = if self.n == 0 then 0 else measure(Sq(self.n - 1))

measure(s: &Shape) -> int = s.area()

@no_recursion
top(n: int) -> int = measure(Sq(n))

print(top(3))
this reaches 'measure', which recurses by the cycle 'measure' → 'Sq.area' → 'measure', and 'top' declared '@no_recursion'

A closure handed to a generic or a bare-arrow parameter is an ordinary call in the instance, and a generic is held at each instance, so a cycle that only one type argument closes is refused there, naming the instance. The library is held too: sysl.slices.sort recurses, and a promised body that sorts is refused naming it. What a C function does is not read, but a C function can only call back into sysl through an address the program took, and that address is already a call.

One cycle is one refusal. Where several promised functions are on one cycle — a module’s clause holds every function in it — the first by name is told; a promised function that only reaches a cycle some promised function is on says nothing more. A @tailrec self-call is a cycle like any other: it is lowered as a jump, but the promise is about what the program says, and a loop says it plainly. The rules for where it stands are @no_alloc‘s: directly under the header it is the module’s clause, a method may promise it, and a function declared inside another may not — the function around it, whose promise covers every call the inner one makes, carries it. Recursing is conduct, not a facility, so @requires(recursion) and @needs(recursion) are refused:

@needs(recursion)
f() -> int = 1

print(f())
'recursion' is what a module does, not something a machine has, and no machine takes it away

@stack — a bound on the stack a call needs

A kernel sizes every thread’s stack, and an interrupt pushes its frame on top of whichever thread it lands on, so a handler that needs more than the room left writes into the guard below. @stack(n) above a function or a method fails the build when a call of it can need more than n bytes of stack, counted the whole way down: the call graph is the program’s, the library’s bodies included, and each frame is the one the back end lays out at the level and for the machine being built — the frame @frame_limit(n) limits one function to, and --stack-usage lists (the frame of every function). A bound too small is refused at the annotation, with the depth and the way down that needs it:

deep(n: usize) -> u8
    var block: [4096]u8 = [0; 4096]
    block[n % 4096] = u8(n)
    block[(n * 7) % 4096]

@stack(1024)
handler(x: usize) -> u8 = deep(x)

print(handler(3))
'handler' declared '@stack(1024)', and it can need

A bound the depth fits under builds as if it were not there:

deep(n: usize) -> u8
    var block: [4096]u8 = [0; 4096]
    block[n % 4096] = u8(n)
    block[(n * 7) % 4096]

@stack(65536)
handler(x: usize) -> u8 = deep(x)

print(handler(3))
0

A depth with no bound is refused, with the reason: a cycle of calls reachable from the function, a frame the back end sizes at run time, or a call through a pointer — a trait object, a &Fn closure, a function pointer — whose target the text does not name. with_lock is kept out of line here; where the optimizer can see which closure a call runs, the call is a direct one and needs nothing said:

struct Lock
    n: int

@noinline
with_lock(l: *Lock, op: &Fn() -> int) -> int
    l.n += 1
    val r = op()
    l.n -= 1
    r

@stack(16384)
handler(x: int) -> int
    var l = Lock(0)
    with_lock(&l, () -> x + 1)

print(handler(1))
'handler' declared '@stack(16384)', and how deep its stack goes has no bound — 'with_lock' calls through a pointer

@stack(calls: m) is what the program says a call through a pointer needs: above the function whose body makes the call, it makes each such call count m bytes. It is the program’s word for code the text cannot name — the closure behind the object is chosen elsewhere — so it is an assumption, and every report that leans on it says so. The two parts combine, @stack(8192, calls: 1024), and a byte count may be written in hexadecimal:

struct Lock
    n: int

@noinline
@stack(calls: 1024)
with_lock(l: *Lock, op: &Fn() -> int) -> int
    l.n += 1
    val r = op()
    l.n -= 1
    r

@stack(16384)
handler(x: int) -> int
    var l = Lock(0)
    with_lock(&l, () -> x + 1)

print(handler(1))
2

The assumption follows the call wherever the optimizer moves it: a body inlined into its caller takes its calls through a pointer with it, and they still count m. A function the optimizer inlined is counted once, in the frame it was inlined into, and a recursion it turned into a loop is a frame rather than a cycle — so a bound checked at -O2 may hold where the same bound at -O0 does not, and the build checks the level it builds at. A destructor, run through the header of the object it tears down, is answered by every destructor in the program rather than refused; teardown never recurses (memory). A C function’s frame is not in the text and counts as nothing; sysl stack names every C function each entry reaches. Every build checks the bound — build, run, build-c and emit-llvm — and @stack marks a function or a method and nothing else:

f() -> int = 1

@stack(64)
struct P
    x: int

print(f())
'@stack(...)' bounds the stack a call needs, so it marks a function or a method whose body is sysl

sysl stack prints the same depth for every way into a program — main, a bare image’s entry and each export — and why where there is none.

@track_caller — a function that reports its caller’s line

A function marked @track_caller is handed the location of each call — file:line:column — and caller_location() inside it answers that location rather than a line of its own:

@track_caller
where() -> Location = caller_location()

print(where())
print(where())
main.sysl:4:7
main.sysl:5:7

Outside a tracked function, caller_location() names its own call. A Location prints as file:line:column, and has file(), line() and column() for its parts.

A chain of tracked functions names the outermost call. A tracked function’s call of another tracked function hands on the location it was handed, so a helper written over a helper still reports the line in the program that started it. An untracked function in the chain is where it stops: its calls name its own lines.

@track_caller
inner() -> Location = caller_location()

@track_caller
outer() -> Location = inner()

plain() -> Location = inner()

print(outer())
print(plain())
main.sysl:9:7
main.sysl:7:23

What a tracked function reports names the caller too. A check the compiler laid down in its body — an array’s index, a slice’s range — reports the caller’s location, and so does every tracked function it calls. This is what the standard library is built on: Buf, Frozen, Deque, Ring and InlineRing‘s insert, remove and their Index members, Option and Result‘s unwrap and expect, and assert, assert_eq, assert_slice_eq and panic are all tracked, so b[5] on a three-element Buf reports index 5 out of bounds for length 3 with the subscript’s line and column, exactly as an array would, and a checking function of a program’s own reports its caller’s line by carrying the mark too:

@track_caller
positive(n: int) = assert(n > 0, "not positive")

positive(1)
print("held")
held

A failed positive(-1) prints panic: not positive (main.sysl:<the line of the call>).

A call that cannot see the location gets the declaration’s

The location is a parameter nobody wrote, after the declared ones, so a direct call — of a function, a method, a generic function’s instance, a member reached through a type parameter’s bound — hands it over. A function pointer and a trait object name no function where they are called, so what they hold is a stand-in that passes the location of the tracked function’s own declaration, as Rust does:

@track_caller
line() -> u32 = caller_location().line()

val f: *extern() -> u32 = &line
print(line())
print(f())
5
2

A closure calling a tracked function is an ordinary call, and names the line in the closure.

Where there is no location

A build that leaves the bounds-check locations out (bounds_locations = false, or --no-bounds-locations) lays down none for a tracked call either: caller_location() is then unknown — known() is false, it prints <unknown>, its line and column are zero — and the reports that would have named it end at their values. Each distinct call site of a tracked function otherwise costs one file:line:column string in the image, as a bounds check does. __CALLER__ is the raw pointer caller_location() is made of (reserved identifiers).

What it may not mark

A tracked function is called by sysl calls, which hand it the location. The callers that cannot are refused:

@export("count")
@track_caller
count() -> int = 1

print(count())
'@track_caller' hands 'count' the location of each call as a parameter past its declared ones, and an '@export'ed function is called from C, which hands it none

main, a @test or a hook, an interrupt handler, an async function (whose body runs when its task is resumed) and a variadic function are refused the same way, and so is an extern, whose body is compiled elsewhere. It takes no arguments.

@frame_limit(n) — the most stack one call of a function may take

A function marked @frame_limit(n) may not have a stack frame larger than n bytes, and a build in which it does fails, naming the function, its frame and the limit. The frame is laid out by LLVM, so the check is made where clang compiles the program — here an archive’s (sysl build-c):

@frame_limit(2048)
@export("deep")
deep(n: usize) -> u8
    var block: [65536]u8 = [0; 65536]
    block[n % 65536] = u8(n)
    block[(n * 7) % 65536]
'deep' needs a stack frame of

The whole message reads 'deep' needs a stack frame of 65584 bytes, over its limit of 2048 bytes, set by '@frame_limit(2048)' above 'deep' on an AArch64 Mac at the default level, the size being whatever the machine and the optimization level lay the frame out at. The frame is what the function’s prologue reserves — its locals, the registers it saves, what it passes on the stack — and not what the functions it calls take: a bound over a whole call chain is @stack(n), which adds these frames up the whole way down.

The limit is the emitted function’s, as clang’s is. A function the optimizer inlines takes its frame into its caller and is held to the caller’s limit there; a function called once with a constant may be folded away entirely, array and all. An @exported function’s C entry point is held to the function’s own limit, since its body is inlined into it.

It is the one function’s answer to a limit set for many. A package’s frame_limit key and the --frame-limit flag hold every function a build compiles (a function’s stack frame), and the attribute is nearer than either: a kernel holding every function to 2 KiB gives its entry point what that one needs, and @frame_limit(none) takes the function out of every limit at all:

@frame_limit(none)
setup() -> int
    var scratch: [8192]u8 = [0; 8192]
    scratch[100] = 7
    int(scratch[100])

print(setup())
7

A closure or nested function written inside a marked function takes its mark, being part of the body the mark was written about; one written anywhere else takes the limit of its own package. Every instance of a generic function carries the mark its declaration was written with, and so does every lowered copy of a method.

The number is a literal from 1 to 4294967295, which is the most LLVM’s attribute holds; none is the one word it takes. A limit is a fact about the code LLVM lays out, so it marks a function or a method, and an extern — whose frame is laid out by whoever compiled it — is refused it.

@renamed and @removed — a name that is gone, and what became of it

A module that renames or removes a declaration leaves a tombstone where it was, so a program still using the old name is told what to write instead rather than that the name stands for nothing:

struct Point
    x: int

@renamed("Point", "2.0") type Pt

val p = Pt(1)
'Pt' was renamed to 'Point' in 2.0

@renamed takes the name that replaced the declaration and the release that renamed it; @removed takes what to write instead and the release that removed it:

struct Point
    x: int

@removed("read the field: write 'p.x'", "2.0") point_x()

print(point_x())
'point_x' was removed in 2.0: read the field: write 'p.x'

A tombstone is the head of the declaration and nothing after it: type Name for a type of any kind — a struct, an enum, an alias — and name() for a function. A use reaches every type through one table of type names whatever it was, so one spelling covers them all; and a function’s empty () says what it was without a signature, since no call is checked against it. A body, a field or a parameter list would be code nothing runs, and is refused. Both arguments are non-empty strings, a tombstone carries no other annotation and no private, and it stands at a module’s top level — not in a body, and not above a member.

The refusal is the same wherever the name is written: as a type annotation, a constructor, a generic argument, an impl subject, a call, a function named as a value, a qualified module.Name, and in an import list, where the line of the import is the one told. It points at the use, never at the tombstone. A function’s tombstone does not answer for a type: f(p: point_x) is still an unknown type.

A tombstone declares nothing. It is not a name in its module: completion, sysl doc and the API pages leave it out, no trait or impl lookup reaches it, and it collides with nothing. It is asked only once a name has been found to stand for nothing else, so a program’s own declaration of the old name — or another module’s, through a wildcard — is the one meant, with no clash:

struct Point
    x: int

@renamed("Point", "2.0") type Pt

struct Pt
    y: int

print(Pt(3).y)
3

The standard library’s own renames are tombstones: sysl.time‘s LocalDate, LocalTime, LocalDateTime, Civil, date_at, time_at and datetime_at, and its free resolve. str(x), removed in 0.7.0, keeps its own refusal — 'str(x)' was removed in 0.7.0: write 'x.to_string()' — because str is still a form the compiler supplies to an interpolation, so no declaration can stand in its place.

@tailrec — an assertion that the frame is reused

A function whose last act is a call to itself compiles to a branch back to its own entry rather than a second frame, so the recursion is bounded by the arithmetic and not by the stack. That happens whether or not anything is written — see tail calls for what counts as the last act, and for the three things that end a tail position.

@tailrec asserts the jump is there:

@tailrec
count(n: int, acc: int) -> int =
    if n == 0 then acc else count(n - 1, acc + 1)

print(count(100000, 0))
100000

What it buys is the refusal. The optimization is silent, so an edit that costs it is silent too — until the day the recursion is deep enough to matter:

@tailrec
count(n: int, acc: int) -> int =
    if n == 0 then acc else 1 + count(n - 1, acc)

print(count(5, 0))
calls itself nowhere the jump can replace

It changes nothing about what is emitted. Write it where losing the jump silently would be a bug, and leave it off everywhere else.

@consumes — a call that takes everything its receiver holds

Some members hand back what their receiver held and leave the receiver empty: Buf.freeze() moves a buffer’s elements into the frozen buffer it answers (memory). A read of that receiver afterwards can only find nothing, so @consumes above such a member makes the read a refusal: once a local has been called on, it may not be read again on any path until it is assigned a new value.

import sysl.buf.{Buf, buf}

run()
    var b: Buf[int] = buf()

    b.push(1)
    b.push(2)

    val f = b.freeze()

    print(f.len(), b.len())

run()
'b' is read here after 'b.freeze()' took everything it held — 'freeze' is '@consumes', so 'b' has nothing in it on this path until it is assigned again

Assigning it ends it, and so does binding the name afresh — a var inside a loop body is a new local every turn:

import sysl.buf.{Buf, buf}

run()
    var b: Buf[int] = buf()

    b.push(1)
    b.push(2)

    val f = b.freeze()

    b = buf()
    b.push(7)
    print(f.len(), b.len(), b[0])

    for i in 0..<2
        var c: Buf[int] = buf()

        c.push(i)
        print(c.freeze()[0])

run()
2 1 7
0
1

The check follows every path the body can take. A branch that consumed on one side leaves the local consumed after the two sides meet, and a loop is followed round, so a read on a later turn counts — here the second turn’s push:

import sysl.buf.{Buf, buf}

run()
    var b: Buf[int] = buf()

    for i in 0..<3
        b.push(i)

        if i == 1 then print(b.freeze().len())

run()
'b' is read here after 'b.freeze()' took everything it held

A branch that consumed and then left — a return, a break past the read — never reaches it, and is not refused.

Only a local is followed. A field, an element, or what a pointer or a second name reaches may be reached another way the body cannot see, so a call on one is accepted, and the member’s own emptying at run time is what a later read finds:

import sysl.buf.{Buf, buf}

struct Holder
    b: Buf[int]

val h: &Holder = Holder(buf())

h.b.push(1)

val f = h.b.freeze()

print(f.len(), h.b.len())
1 0

A call through a trait object is not followed either: which body a slot dispatches to is settled at run time, so a trait member’s @consumes says nothing about the object’s own local. A method has no second spelling the check could miss — Buf.freeze(b) is refused as every instance method named through its type is.

It marks a member that takes a receiver, in any mode, and takes no arguments. Above a free function, or a member that takes no self, it is refused:

struct S
    v: int

    @consumes
    make() -> S = S(1)
'@consumes' says a call to 'make' takes everything its receiver holds, and it has none — it takes no 'self'

@noinline, @inline and @cold — what the optimizer is told about a definition

A private function is visible only inside the file that wrote it, so the optimizer knows every call it has. With one call site it folds the body back into the caller and deletes it — which is usually what you want, and is exactly wrong for a slow path. A buffer’s grow is called from push‘s cold arm and from extend, and is almost never reached; inlined into either caller it would make that caller large enough that it stops being inlined into its own callers. Splitting the rare path out bought nothing.

@noinline says the call stays a call:

@noinline
grow(n: int) -> int = n * 2

print(grow(21))
42

@inline says the opposite, and it is the one a library reaches for more often. A member written to be absorbed by its caller — a sequence’s append, an accessor that checks one thing — sits a few units either side of the budget the optimizer is willing to spend, and which side is decided by things the member has no say in: how wide the element type is, how many counted fields it carries. @inline raises that budget for this definition, so the decision stops moving when a struct one field along grows:

@inline
push(n: int) -> int = n + 21

print(push(21))
42

Bare, it is a hint, not an instruction. The inliner is told to spend more and then decides as it decides, and a definition that is enormous is still a definition. That is the right default — an instruction would make a library’s opinion about one member override a caller’s whole code size.

@inline(always) is the instruction. Every call is replaced by the body, at every optimization level — -O0 included, where nothing else is inlined at all — so it is for the definition whose call is known to cost more than its body: a decoder’s per-symbol step, a fixed-point multiply, a register accessor on a board built for debugging:

@inline(always)
symbol(bits: int) -> int = bits & 0xff

print(symbol(298))
42

It lowers to LLVM’s alwaysinline in the place @inline‘s inlinehint takes, and a member, a generic — every instantiation — and an @exported function may carry it like the bare word; the exported symbol is still defined for C to call. A function that calls itself is not inlined, whatever it is marked — LLVM leaves such a body out of line, as clang and rustc do with theirs, and the program is still right. @tailrec turns the self-call into a jump, and a body that no longer calls itself is absorbed like any other:

@tailrec
@inline(always)
fact(n: int, acc: int) -> int = if n <= 1 then acc else fact(n - 1, acc * n)

print(fact(5, 1))
120

@cold says the definition is reached rarely. The optimizer spends nothing on it and keeps its blocks away from the hot ones — but it is a frequency, not a prohibition, and a cold function may still be inlined:

@cold
fail(n: int) -> int = n - 1

print(fail(43))
42

@noinline and @inline are one axis; @cold is another. The first two contradict, and the compiler refuses them together. @cold composes with either: a rare slow path wants @noinline and @cold — kept out of line, and known to be rare — and a rarely reached helper written to be absorbed wants @inline and @cold.

@noinline
@cold
grow(n: int) -> int = n * 2

print(grow(21))
42
@inline
@cold
report(n: int) -> int = n + 21

print(report(21))
42

What @inline guarantees across packages and codegen units

A program is not always one module. A package — a dependency, or a --lib root — is compiled once into an archive of its own that every program using it links, and a large program is cut into codegen units compiled side by side. A call from one module into another is ordinarily a wall the optimizer cannot see through. At every optimization level but -O0, an optimized build carries the body across that wall for:

  • every function marked @inline or @inline(always), whatever its length, and
  • every small function — a few statements’ worth: an accessor, a comparison, a fixed-point multiply — whether marked or not.

Small is measured by what the optimizer keeps of the body, not by its unoptimized length: the report and trap of a failed bounds or overflow check is a cold exit and does not count, and nor does the guard on a shift by a constant amount, which folds away. A getter that indexes an array and shifts the element is small.

The calling module is given a copy of the body to inline (LLVM’s available_externally) and never emits it, so the program still links the package’s one definition, and a call the optimizer leaves out of line is the same call it always was. The copy is made from the package’s source on every build, so an edit to the package is what the next build inlines; nothing is kept that could be stale.

A package’s own archive is a calling module too. Its calls into the standard module, or into another package, carry the same copies — including a call a generic reaches through a bound. A package whose is_nonzero[T: Zero + Eq](x: T) = x != T.zero() is instantiated at real by the package’s own code compiles real.zero() to the constant 0.0 in its archive, as it would in the program, rather than calling the standard module’s real.zero out of line.

So the guarantee is the same as within one file: a call into a package, or into another unit, inlines exactly where the same call would inline if both functions sat side by side — the hint is still a hint, and the inliner still decides. What is never carried across:

  • a function marked @noinline, or @cold without @inline — the call stays a call;
  • an async function, an interrupt handler, one placed in a @section, or a variadic one;
  • a function that reaches module storage filled at run time, or @thread_local, @per_cpu or @main_thread storage, or another package’s storage, which is compiled by the program anyway (the package keeps no copy of it), and so needs nothing carried. Storage that is a constant image is not that: the package’s own, and the standard module’s, is laid down once in its own archive, and every other archive and the program name that one object — one address, one value — so a function reaching it, one that indexes a Buf or reads a lookup table say, is compiled into the package’s archive like any other;
  • a generic, which needs none of this: it is instantiated in the module that uses it, beside its callers.

sysl emit-llvm prints the program as one module and carries no copies.

They mark a member too

A method lowers to an ordinary function, and these three are about that function — so a generic container writes the word once and every element type it is used at gets it:

struct Buf[T]
    cap: int

    @noinline
    @cold
    grow(self) -> int = self.cap * 2

    @inline
    room(self) -> int = self.cap + 21

var a: Buf[int] = Buf(21)
print(a.grow())
print(a.room())
42
42

That is what sysl.buf does with its own push: the member is written so a caller can absorb the whole of it, and @inline is what keeps the element type from deciding whether it does.

They are the only three annotations a member may carry that are not about a parameter.

None of the three takes an argument but always

Whether a call stays a call is the whole of what the first says, @inline raises a budget rather than naming one, and how rare a rare path is is not a number a program has. always is the one argument there is:

@noinline(2)
grow(n: int) -> int = n * 2

print(grow(21))
'@noinline' takes no arguments
@inline(200)
grow(n: int) -> int = n * 2

print(grow(21))
'@inline' takes no argument but 'always'

The opposite request is a word of its own, so never is pointed at it:

@inline(never)
grow(n: int) -> int = n * 2

print(grow(21))
'@inline(never)' is written '@noinline'

What they may not mark

They mark a function. A struct has no definition to keep out of line, a const is folded into every use, and an extern names something this program does not define — a declare line carries no function attributes at all.

@noinline
struct S
    a: int

print(1)
an annotation marks a function

@inline(always) above an extern is the case most often tried, and it says why — the body is in the object the extern is linked from, and only that object’s own compiler can put it anywhere:

@inline(always)
extern abs(n: i32) -> i32

print(1)
An 'extern' has no body in this program to put anywhere

@ghost is the one other annotation they contradict rather than merely have nothing to do with. A ghost function is erased before anything is emitted, so there is no definition left for any of them to be about:

@ghost
@noinline
sorted(n: int) -> bool = n > 0

print(1)
'@ghost' means there is none

@noinline beside @inline

One forbids inlining and the other asks for it, so a definition carrying both is held to neither. This is the one refusal in the group that is not about a missing definition:

@noinline
@inline
grow(n: int) -> int = n * 2

print(grow(21))
they contradict above one declaration

@inline(always) beside @noinline is refused the same way, and beside a bare @inline it says one thing twice — the instruction already asks for everything the hint does:

@inline
@inline(always)
grow(n: int) -> int = n * 2

print(grow(21))
the two together say one thing twice

If what you meant was keep it out of the hot path, that is @cold, and it stands beside either of them.

They stand beside everything else. @tailrec is about this function’s call to itself becoming a jump and @noinline about the call into it from elsewhere staying a call — two different calls, so there is nothing for them to disagree about; and @test only says who calls the function.

@strict_fp — floating point that follows the environment it runs in

An ordinary float operation is IEEE 754’s answer for round-to-nearest, and nothing promises when it runs or which exceptions it raises: the compiler may work it out while compiling, merge two identical ones, or move one past a call. That is what lets x * k leave a loop. It is also what makes a rounding mode impossible to use, because set_rounding is a call like any other and the addition written after it may not be.

@strict_fp on a function says its floating point follows the environment in force when each operation runs — the rounding mode, and on Arm the flush-to-zero and default-NaN controls — and raises its exceptions there. sysl.fenv is how a program sets the environment and reads the flags:

import sysl.fenv.*

@strict_fp
thirds(x: f64) -> (u64, u64)
    set_rounding(Upward)
    val up = x / 3.0
    set_rounding(Downward)
    val down = x / 3.0
    set_rounding(NearestEven)
    (up.bits(), down.bits())

val (up, down) = thirds(1.0)
print(f"$up%016x")
print(f"$down%016x")
3fd5555555555556
3fd5555555555555

The two divisions are the same expression with the same operands, and they are two operations, each rounded in the mode written above it. Without the annotation the second is free to be the first one’s answer.

What it covers

Inside a strict body, every floating-point operation at f32 and f64 follows the environment:

  • +, -, *, /, and a compound assignment of one, on a float or lane by lane on a vector of them;
  • a multiply feeding an add, which is one fused operation rounded once, as it is everywhere, and x.mul_add(a, b), which asks for that operation by name;
  • the comparisons. <, <=, > and >= raise invalid when an operand is a NaN, as IEEE 754 says the relational predicates do; == and != are quiet;
  • the conversions: an integer read as a float and a float narrowed round in the mode in force, a float read as an integer raises inexact when it drops a fraction, and one the integer cannot hold — or a NaN — raises invalid while it saturates as it does everywhere;
  • a conversion between f16 and f32 or f64, below;
  • sqrt, floor, ceil, round and trunc called on a float, and the intrinsics a program declares with extern "llvm.…".

Negation, abs and copysign touch the sign bit alone, round nothing and raise nothing, so they are what they always are. A negation is the sign bit flipped where it is written: x + -y is never made x - y, nor (-a).mul_add(b, c) a fused multiply-subtract, so where the answer is a NaN its sign is the one the expression as written gives it — Arm, which hands a NaN operand back with the sign it has, shows the difference. A literal is a value, not an operation: 0.1f32 is the binary32 nearest a tenth whatever the mode, and so is a const, which is worked out while compiling. An expression of literals is an operation like any other:

import sysl.fenv.*

@strict_fp
shown() -> string
    set_rounding(Downward)
    val third = 1.0f32 / 3.0f32
    val tenth = 0.1f32
    set_rounding(NearestEven)
    f"${third.bits()}%08x ${tenth.bits()}%08x"

print(shown())
3eaaaaaa 3dcccccd

The flags an operation raises are raised where it runs, so a read of them after it sees them:

import sysl.fenv.*

@strict_fp
divided(x: f64, y: f64) -> string
    clear_flags()
    val q = x / y
    val f = flags()
    f"${q > 1.0} divide-by-zero ${f.divide_by_zero}, inexact ${f.inexact}"

print(divided(1.0, 0.0))
print(divided(1.0, 3.0))
true divide-by-zero true, inexact false
false divide-by-zero false, inexact true

Closures and nested functions are strict too

A closure or a nested function written inside a strict body is part of its text, so it is strict too. That is what makes with_rounding useful — the closure it runs is the strict caller’s:

import sysl.fenv.*

@strict_fp
third_up(x: f32) -> u32 = with_rounding(Upward, () -> x / 3.0f32).bits()

print(f"${third_up(1.0f32)}%08x")
3eaaaaab

A closure takes no annotation of its own, so the place to say it is the function it is written in:

val f = @strict_fp (x: f32) -> x + x
print(f(1.0f32))
a closure takes no annotations, and '@strict_fp' reaches one through the function it is written in — every closure written inside a '@strict_fp' function is strict too, so mark that function, or move the closure into one

What a strict body calls is compiled as it was written

A call from a strict body to an ordinary function runs that function’s operations as ordinary ones: they are not strict, whether the optimizer inlines the function or not, and the strict operations around the call stay strict either way. A generic function is the same — a strict body that calls plain[T] calls an ordinary instance — and a generic marked @strict_fp is strict at every instance. A method takes the annotation as a function does.

The other way round, a strict function called from an ordinary one is strict, and is never inlined into it: its operations would have to stay strict among ones that are not, so the call stays a call. That is why @inline(always) cannot stand beside it:

@strict_fp
@inline(always)
twice(x: f32) -> f32 = x + x

print(twice(1.0f32))
'@inline(always)' puts the body in place of every call, and a '@strict_fp' body is only ever put inside a caller that is '@strict_fp' too — anywhere else the call stays a call, so the promise could not be kept. Write '@inline' for the hint, which a strict caller takes

@strict_fp takes no argument — the mode is whatever the environment holds — and it marks a function or a method; above an extern, whose operations someone else compiled, it is refused.

Where there is no environment to follow

A strict body computes at f32 and f64. An operation at f16, bf16 or f128 is widened, emulated or called on most machines, and is refused where it is written:

@strict_fp
half(x: f16) -> f16 = x + x

print(half(1.0f16))
a '@strict_fp' body computes at f32 and f64, the widths a floating-point unit performs under its environment, and this is f16

f16 conversions, and nothing else at f16

The one operation at f16 a strict body computes is a conversion between it and f32 or f64, because AArch64’s fcvt and an Arm core’s vcvtb convert binary16 in one instruction, rounding in the mode in force and raising inexact, overflow, underflow and — for a signalling NaN — invalid. A narrowing from f64 is rounded once, never twice through f32:

import sysl.fenv.*

@strict_fp
narrowed(x: f32, d: f64) -> string
    set_rounding(Upward)
    val up = f16(x).bits()
    set_rounding(NearestEven)
    clear_flags()
    val near = f16(x).bits()
    val once = f16(d).bits()
    f"$up%04x $near%04x inexact ${flags().inexact}, $once%04x"

print(narrowed(f32.from_bits(0x3f801000), real.from_bits(0x3ff0020000001000)))
3c01 3c00 inexact true, 3c01

1 + 2^-11 is halfway between binary16’s 1.0 and the value above it: upward it goes up, to nearest to the even 1.0. The f64 is a little above that halfway, which rounding it to f32 first would lose. An f16 literal is a value like any other literal, IEEE’s binary16 nearest it whatever the mode; Arm’s alternative half-precision format changes what a conversion produces.

Arithmetic and comparison at f16, and a conversion between f16 and an integer, stay refused. So is the conversion where it is a library call — on x86-64 and RISC-V, whose units have no binary16 instruction in the targets sysl builds for — and one between f16 and f64 on a single-precision unit, a Cortex-M4F or M33, where it would be a call too, or two roundings through f32:

@strict_fp
@export("narrowed")
narrowed(d: f64) -> u16 = f16(d).bits()
'@strict_fp' asks the floating-point unit to follow its environment, and 'thumbv7em-freestanding''s unit is single precision, so a conversion between real and f16 is a library call, which has no environment to follow and raises nothing; narrowing through f32 instead would round twice, so convert an f32, or build for a target whose unit has double precision
@strict_fp
@export("narrowed")
narrowed(x: f32) -> u16 = f16(x).bits()
'@strict_fp' asks the floating-point unit to follow its environment, and on 'x86_64-freestanding' a conversion between f32 and f16 is a library call, since the unit has no instruction converting f16

A Cortex-M7 with a double-precision unit (thumbv7em-freestanding-dp) converts between f16 and f64 in one vcvtb, and a strict body may.

A float a machine computes in software — every float on a core with no floating-point unit, an f64 on a Cortex-M4F, whose unit is single precision — has no environment to follow either, so it is refused for that machine. Fixed point is integer arithmetic and follows no floating-point environment, so a strict body neither changes it nor refuses it.

@strict_fp
@export("twice")
twice(x: f32) -> f32 = x + x
'@strict_fp' asks the floating-point unit to follow its environment, and 'thumbv6m-freestanding' computes f32 in software, which has no environment to follow and raises nothing

So is a conversion whose integer is wider than the machine’s word. A unit converts the integers its registers hold, so a 32-bit Arm core reads an i64 as a float with a library call, and every machine does the same with an i128; the library rounds to nearest and raises nothing. An integer of the word’s width or narrower is the unit’s own instruction:

@strict_fp
@export("widened")
widened(n: i64) -> f32 = f32(n)
'@strict_fp' asks the floating-point unit to follow its environment, and on 'thumbv7em-freestanding' a conversion between long and f32 is a library call, since the unit converts integers of at most 32 bits

WebAssembly’s floating point always rounds to nearest and raises nothing, so a strict body’s float operations are refused there. On a vector, lane-wise operations are strict, but a reduction — v.sum(), v.min() — combines the lanes in an order the target chooses and has no strict form, so inside a strict body it is refused, and the lanes are read and combined one by one instead.

@reassociate — float sums the back end may regroup

A float +, - or * is computed in the order it is written, and floating-point addition is not associative, so a loop adding into one accumulator is a chain of dependent additions that the optimizer may not reorder. It stays scalar, one addition waiting for the one before, however many lanes the machine’s vector registers have.

@reassociate on a function or a method gives that order up for every float +, - and * written in its body: the back end may regroup them, so a sum is split over several accumulators — lanes of a vector register — that are added together at the end. The result may differ from the written order’s in the last bits, and that is the whole of what changes:

@reassociate
total(xs: []const real) -> real
    var t = 0.0

    for x in xs do t = t + x

    t

var xs = [0.5, 1.25, 2.0, 4.25]

print(total(xs))
8

Every partial sum there is exact, so any grouping gives 8. Nothing else is relaxed: NaNs, infinities and signed zeros mean what they always do, / is untouched, integers are associative already, and a product meets a sum as one fused multiply-add exactly where it would anyway — though one the back end may also keep as two operations, which is how a reduction of products vectorizes. sysl.slices‘ sum and dot are @reassociate functions.

Written on a line of its own above an indented block, it marks the block instead, and only what is written inside it is regrouped:

var xs = [0.5, 1.25, 2.0, 4.25]
var total = 0.0

@reassociate
    for x in xs do total = total + x

print(total * 2.0)
16

The multiply after the block keeps its written order, as everything outside does.

What it covers

The region is lexical: the operations written in the function or the block, and in any closure or nested function written inside it. A function called from inside but written elsewhere keeps its own order, inlined or not.

A value worked out while compiling is the written order’s. A const, a module val laid down as data and a const function’s result are each computed left to right, as everywhere, so a @reassociate const function folds to the same value it would unmarked, and the same call made while the program runs may differ from it in the last bits.

It takes no argument, and it marks a function, a method or a block — nothing else:

@reassociate
struct P
    x: real
'@reassociate' lets the back end regroup the float sums and products written in a body, so it marks a function, a method, or — on a line of its own — the block indented under it

A closure takes no annotations; it is regrouped when it is written inside a marked function or block.

It contradicts @strict_fp, which keeps every operation where and how it is written, so the two above one function are refused, and so is a @reassociate block inside a strict body. A strict function written inside a @reassociate one keeps its own promise:

@reassociate
@strict_fp
twice(x: f32) -> f32 = x + x
'@strict_fp' keeps every floating-point operation exactly where and how it is written, and '@reassociate' lets the back end regroup them

@extensions — a body compiled for instructions the build does not promise

A build uses what its machine guarantees: a row’s architecture, plus whatever codegen.extensions and --cpu add for the whole image. A CRC, an AES round or a dot product often has an instruction only some processors of that architecture have, and a library that wants it cannot make the whole program require it. @extensions compiles one function for them, as Rust’s #[target_feature(enable = "…")] and clang’s __attribute__((target("…"))) do:

#if aarch64
extern "llvm.aarch64.crc32cx" crc_word(crc: u32, word: u64) -> u32

@extensions("crc")
crc_words(words: []u64) -> u32
    var crc: u32 = 0xffffffff
    for w in words
        crc = crc_word(crc, w)
    crc ^ 0xffffffff
#endif

print("compiled")
compiled

Each name is the extension as LLVM names the feature, the names codegen.extensions takes — crc, aes, sha3, dotprod, lse on AArch64, sse4.2, avx2, bmi2 on x86-64 — and each is asked of the clang that will build the program, which would only warn about a name it does not know:

@extensions("crcc")
step(x: u32) -> u32 = x

@export("main")
start() -> i32 = i32(step(1))
'@extensions' names 'crcc', which the aarch64 back end does not know — an extension is spelled as LLVM spells the feature

The body’s define line carries LLVM’s "target-features": the processor’s own list, the build’s, then the function’s. So the back end may select the extension’s instructions there, an intrinsic it calls is one the back end can select, and an asm block in it may name them — the assembler refuses aese in a body that does not say aes. A closure or a nested function written inside is compiled the same way: it can only be reached by running the body that made it.

A call is the promise, so a call is what is checked

On a processor without the extension, the body’s first such instruction faults. So a call is accepted only where the code making it is known to run on a processor that has every extension the callee names:

  • code compiled for them itself — a function marked with them, or written inside one;
  • code the build promises them to everywhere — codegen.extensions, --cpu, or the row’s own processor (aarch64-macos is built for the M1, which has crc, aes, sha3 and dotprod);
  • code inside an @unchecked("…") block naming them, which is the program’s word that it has asked the processor first.

Anywhere else the call is refused, naming both ways to make it:

@extensions("crc")
step(x: u32) -> u32 = x

@export("main")
start() -> i32 = i32(step(1))
'step' is called, and it is compiled for the processor's 'crc' extension, which 'aarch64-freestanding' does not promise and the code here is not compiled for — on a processor without it its first such instruction faults. Mark the caller '@extensions("crc")', or make the call inside '@unchecked("crc")' once the program has asked the processor

That is Rust’s rule — a #[target_feature] function is an unsafe call from a function without the features — said in the word sysl’s unchecked tier already uses. The block checks nothing while the program runs: the program asks its processor with sysl.cpu.has, and the answer goes in an ordinary if around the block. It relaxes nothing else either — a subscript inside it is bounds-checked as anywhere:

import sysl.cpu

#if aarch64
@extensions("crc")
twice(x: u32) -> u32 = x * 2

if cpu.has("crc")
    @unchecked("crc")
        print(twice(21))
else
    print(21 * 2)
#else
print(42)
#endif
42

The block is lexical, as @unchecked‘s is: a closure written inside is inside it, and a call after its last line is not. A block naming aes does not cover a callee that needs sha3 as well, and the refusal names only what is missing. Taking the function’s address is held to the same rule, since a call through the address names no callee; and a marked method may not fill a trait’s table, for the same reason.

The CRC-32C externs are such functions. llvm.aarch64.crc32cx and SSE 4.2’s crc32 are compiled for crc and sse4.2 (ffi), so a declaration is accepted on its own processor and each call is held to this rule.

So are the AES, SHA-3 and dot-product instructions, which take and answer vectors (ffi): AArch64’s aese, aesd, aesmc and aesimc are aes, its eor3, bcax and rax1 are sha3, and sdot and udot are dotprod; x86’s AES-NI rounds are aes and the VNNI dot products avxvnni. A function that calls one is marked with its extension, or the call is inside an @unchecked naming it (the row aarch64-macos promises the first three), and a mark naming another extension does not cover it — the refusal names the one that is missing:

extern "llvm.aarch64.crypto.aese" aese(state: <16>u8, key: <16>u8) -> <16>u8

@extensions("sha3")
round(state: <16>u8, key: <16>u8) -> <16>u8 = aese(state, key)

@export("main")
start() -> i32 = 0
'aese' is called, and it is compiled for the processor's 'aes' extension, which 'aarch64-freestanding' does not promise and the code here is not compiled for — on a processor without it its first such instruction faults. Mark the caller '@extensions("aes")', or make the call inside '@unchecked("aes")' once the program has asked the processor

Asking the processor — sysl.cpu

sysl.cpu is the asking. cpu.has("sha3") answers whether the processor the program is running on has the extension, by the system’s own means — sysctlbyname on macOS, getauxval(AT_HWCAP) on Linux and Android, cpuid and xgetbv on x86-64 — asked once and kept. Its names are this attribute’s, so the name in the if is the name in the block under it, and a literal name the target’s processor cannot be asked about is refused as @extensions refuses it:

import sysl.cpu

print(cpu.has("sha33"))
'sysl.cpu.has' is asked about 'sha33', which is not an extension an aarch64 processor can be asked about

A bare-metal target has no operating system to ask, so there a call of has is refused, and the build’s own codegen.extensions or --cpu says what the processor has.

Inlining follows the features

LLVM puts a body inside a caller only where the caller’s features hold the body’s, as clang’s and rustc’s do. A marked body is absorbed by a caller marked the same and stays a call from any other, and an ordinary helper is absorbed by a marked body as by any other — its list starts with the processor’s, so it holds everything an ordinary function’s does. @inline is the hint it always was; @inline(always) is refused beside @extensions, being a promise the back end would break at every call from code without them:

@inline(always)
@extensions("crc")
step(x: u32) -> u32 = x
'@inline(always)' puts the body in place of every call, and a body compiled for a processor's extensions is only ever put inside a caller that has them too

Where it applies

@extensions is accepted on aarch64 and x86_64, where a function’s list is added to its processor’s. On Thumb the build’s list carries the unit -mfpu named, and on RISC-V the extensions its triple names, and LLVM replaces a build’s list with a function’s — so there the manifest says it, for the whole image:

@extensions("dsp")
step(x: u32) -> u32 = x

@export("main")
start() -> i32 = i32(step(1))
'@extensions' is accepted on aarch64 and x86_64, where a function's extensions add to its processor's, and this build is for 'thumb-freestanding'

It marks a function or a method, whose body is sysl; an extern‘s instructions are its compiler’s. A name is written alone, as codegen.extensions writes it: "+crc", "crc,aes" and an empty list are refused.

@no_fuse — multiplies and adds rounded apart

A float multiply feeding an add in the same expression is one fused multiply-add wherever the machine fuses at that width (expressions), rounded once. That is the more accurate answer, but not the one a program written for C’s -ffp-contract=off checks its results against. @no_fuse on a function or a method turns fusing off for its body — C’s -ffp-contract=off: every a * b + c there is a multiply, rounded, and then an add, rounded, at every optimization level:

id(x: real) -> real = x

@no_fuse
apart(x: real, c: real) -> real = x * x + c

fused(x: real, c: real) -> real = x * x + c

var x = 1.0 + 1.0 / 1073741824.0
var c = -(1.0 + 1.0 / 536870912.0)

#if fma_dp
val wanted = 1.0
#else
val wanted = 0.0
#endif

print(apart(x, c) * 1152921504606846976.0, fused(x, c) * 1152921504606846976.0 == wanted)
0 true

x * x is 1 + 2^-29 + 2^-60, and c cancels all but the 2^-60: fused, that is the answer; rounded first, it is gone. The function at the top is there for the header’s reason below.

In a file’s header it marks the whole module — every function, method, closure and constant of it, and the initializers of its storage — and nothing outside it: a program using the module fuses its own arithmetic as before. Like a capability, it is a property of the module, so every file of the module states it, and a file that does not is refused.

module kernels
@no_fuse

advance(x: real, v: real, dt: real) -> real = x + v * dt

The header is read first, as it is for @no_alloc: @no_fuse on the first lines of a file is the module’s clause even where a function follows it. A file whose first declaration is the one function it means writes something else above it — an import, another declaration — as the example at the top of this section does.

What it covers

The region is the body, and everything the compiler makes of it:

  • A closure or a nested function written inside a marked body is unfused too; a function called from inside but written elsewhere keeps its own rule.
  • Every instantiation of a marked generic, and every copy of a marked member, is unfused, and a generic written in a @no_fuse module is unfused wherever it is instantiated.
  • Inlined, the body stays unfused. No operation in it carries anything that lets the optimizer fuse it, so it stays two operations inside a caller that fuses; the caller’s own sums around it are fused as before.
  • An arithmetic operator marked @no_fuse is never read as its body at a call: it stays a call, and its body is unfused. A @no_fuse body reads no operator as its body either.
  • A value worked out while compiling agrees. A const, a module val laid down as data and a const function’s result are each computed with two roundings in a marked body or module, so the value is the same whether the compiler or the program computed it.
  • x.mul_add(a, b) is still one rounding. It asks for the fused instruction by name, which is not contraction; @no_fuse turns off only the fusing the compiler would choose on its own.
id(x: real) -> real = x

@no_fuse
const apart(x: real, c: real) -> real = x * x + c

const X = 1.0 + 1.0 / 1073741824.0
const C = -(1.0 + 1.0 / 536870912.0)
const A = apart(X, C)

var x = X
var c = C

print(A == apart(x, c), A)
true 0

It composes with the other two float annotations. Beside @reassociate — on one function, or a @reassociate block inside a @no_fuse body, or a @no_fuse function written inside a @reassociate one — sums are regrouped and every product is still rounded on its own: @reassociate licenses no fusing of its own. Beside @strict_fp the body is its strict multiplies and strict adds, each following the environment, never a strict fused one.

It takes no argument, and it marks a function, a method or a module — nothing else:

id(x: real) -> real = x

@no_fuse
struct P
    x: real
'@no_fuse' says how a body's multiplies and adds are compiled, so it marks a function or a method, whose body is sysl, or — in the file's header — the whole module

A closure takes no annotations; it is unfused when it is written inside a marked function.

There is no per-build setting. Whether a body fuses is a fact about the arithmetic written in it — the results it is checked against — so it is said where that arithmetic is, as @strict_fp and @reassociate are, and a module that means it everywhere says it once in its header.

@unchecked — a block whose subscripts are not bounds-checked

@unchecked is the one annotation that marks a block rather than a declaration: written on a line of its own, it covers the statements indented under it, and every subscript written there — of an array, a slice, or a container such as Buf that says how — is read with no bounds check, and so is a vector load or store of a run of elements.

var squares = [0; 5]

@unchecked
    for i in 0..<5
        squares[i] = i * i

print(squares)
[0, 1, 4, 9, 16]

The block is lexical: a closure or a nested function written inside it is inside it, and one called from inside but written elsewhere is not. Bare, it takes no argument — with the names of processor extensions in parentheses, @unchecked("crc"), it is the other block, which lets a call of a function compiled for them be made and leaves every subscript checked. It does not mark a function, a member or a single statement on the same line:

@unchecked
first(xs: []int) -> int = xs[0]
'@unchecked' marks a block

What it does to a subscript, get_unchecked and set_unchecked for one subscript at a time, and what stays checked are in arrays.

@checked — integer overflow that traps in every build

What a plain integer +, - or * does when the answer does not fit is the build mode’s: a debug build stops the program, and a release build wraps. Code whose overflow is a fault however it was built — flight software, a counter whose wrap would read as a small number — says so with @checked above a function or a method. Every plain +, -, *, unary -, compound assignment, ++ and -- and every written integer conversion in its body is then checked in a release build exactly as a debug build checks it, with the same report:

@checked
total(a: u8, b: u8) -> u8 = a + b

print(total(200, 55))
print(total(200, 56))
255

The second call stops the program with integer overflow: u8 + u8 (main.sysl:2:29) whether it was built by sysl run or by sysl build.

Written on a line of its own above an indented block, it marks the block instead, and only what is written inside it is checked — what comes before and after keeps the mode’s meaning:

var level: u8 = 250

@checked
    level = level + 5

print(level)
255

It never makes anything wrap, and it changes nothing that already says what it does: +% and +| wrap and saturate, u8.wrap(n) keeps the bit pattern, checked_add answers None, a shift is defined at every count, and a vector’s lanes wrap as the machine’s lane instructions do. Division by zero traps everywhere already.

What it covers

The region is lexical, as @reassociate‘s is: the operations written in the function or the block, and in any closure or nested function written inside it. A function called from inside but written elsewhere keeps the mode’s meaning, inlined or not. A generic @checked function is checked at every instantiation, and a method marked @checked is checked while its siblings are not.

It is independent of @unchecked, which is about subscripts: an @unchecked block inside a @checked function reads its subscripts with no bounds check and still traps on overflow, and a @checked block inside an @unchecked one is checked the same way.

A value worked out while compiling agrees. A const function marked @checked, or one written inside a @checked block, is held to the same promise when a constant runs it: an overflow that would stop the running program refuses the constant instead of folding a wrapped value into it. Unmarked, a const function folds the value a release build would compute.

@checked
const add(a: u8, b: u8) -> u8 = a + b

const K = add(200, 100)
'K' traps while it is being compiled: 200 + 100 overflows byte

A whole package is checked with checked_overflow = true in its manifest, which does for every file of the package what @checked does for one function.

It takes no argument, and it marks a function, a method or a block — nothing else:

@checked
struct P
    x: int
'@checked' makes the integer arithmetic written in a body trap on overflow in every build, so it marks a function, a method, or — on a line of its own — the block indented under it

A closure takes no annotations; it is checked when it is written inside a marked function or block.

@disjoint — parameters whose memory nothing else reaches

A kernel that writes through one view and reads through another is the loop a vectorizer exists for, and the one it is least sure of. axpy below adds a * x[i] into y[i]; if y and x could be two views of one array, offset by an element, then a store to y[i] changes what a later read of x sees, and doing four elements at once would get the wrong answer. The optimizer cannot tell from the function, so it vectorizes the loop and keeps a scalar copy, choosing between them at run time by comparing the two address ranges on every call.

@disjoint(y, x) is the function saying the comparison is never needed:

@disjoint(y, x)
axpy(y: []f64, x: []const f64, a: f64)
    for i in 0..<y.len
        y[i] = y[i] + a * x[i]

var ys: [4]f64 = [1.0, 1.0, 1.0, 1.0]
val xs: [4]f64 = [1.0, 2.0, 3.0, 4.0]

axpy(ys[0..<4], xs[0..<4], 2.0)
print(ys[3])
9

What it promises is C’s restrict. While a call runs, no memory reached through one named parameter is reached through another named parameter, or through any other pointer the body uses. The parameters are named on the function, as @scoped(...) and @crossing(...) name theirs — a parameter carries no annotation of its own.

It names pointers and views: *T, &T, []T and []const T. A parameter of any other type arrives as a copy, which nothing else can reach, so naming one promises nothing and is refused:

@disjoint(n, p)
store(n: int, p: *int) = *p = n

var v = 0
store(42, &v)
print(v)
'@disjoint' names 'n', a parameter of 'store' whose type 'int' is a value

A generic is held to that at each instantiation, where its parameters have types — the views and pointers of every instance carry the promise. A name that is not a parameter, or one written twice, is refused where it is written, as @crossing‘s are; so is the annotation above an async function, whose body runs after the call that made the task has returned, so that there is no call for the parameters to be kept apart during:

@disjoint(p)
async fill(p: *int) -> int = 1

print(1)
'@disjoint' marks 'fill', which is 'async'

A method names its other parameters, never self. What a named parameter reaches, nothing else the body uses reaches — the receiver included — so naming the slice a method copies from already keeps it apart from the storage the method writes into:

struct Ring
    bytes: [8]u8

impl Ring
    @disjoint(data)
    fill(*self, data: []const u8)
        for i in 0..<data.len
            self.bytes[i] = data[i]

var r = Ring([0; 8])
val src: [3]u8 = [7, 8, 9]

r.fill(src[..])
print(r.bytes[2])
9

Writing the receiver into the list is refused, and says so:

struct Ring
    bytes: [8]u8

impl Ring
    @disjoint(self, data)
    fill(*self, data: []const u8) = ()

print(1)
'@disjoint' names parameters, and 'self' is the receiver — leave it out

It is not @no_overlap: the @no_ words are the capabilities a module or a definition gives up — @no_alloc, @no_block — and every @no_<word> is read as one, so a promise about two parameters takes a word of its own.

Checked where a call shows it broken

A call is refused where the arguments it hands two named parameters are provably the same storage: the same local twice, two slices of one array or view whose constant bounds share an element, or a view and the address of one of its own elements.

@disjoint(y, x)
axpy(y: []f64, x: []const f64, a: f64)
    for i in 0..<y.len
        y[i] = y[i] + a * x[i]

var ys: [8]f64 = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]

axpy(ys[0..<4], ys[2..<6], 2.0)
'axpy' is '@disjoint' over 'y' and 'x', and this call hands them storage that overlaps — 'ys[0..<4]' and 'ys[2..<6]' overlap

Slices that only meet at a boundary are separate storage, and a call handing them over compiles:

@disjoint(y, x)
axpy(y: []f64, x: []const f64, a: f64)
    for i in 0..<y.len
        y[i] = y[i] + a * x[i]

var ys: [8]f64 = [1.0, 1.0, 1.0, 1.0, 1.0, 2.0, 3.0, 4.0]

axpy(ys[0..<4], ys[4..<8], 2.0)
print(ys[3])
9

Where the call cannot settle it, the promise is trusted, as C trusts restrict. Two views a caller was itself handed, a bound or an index computed at run time, a pointer read out of a field — none of these is refused, and none is checked when the program runs. A call that breaks the promise anyway is undefined behaviour: the optimizer has been told the loads and stores through the two parameters never touch the same memory, and the code it makes is free to read a value before the store that should have changed it, or after.

Over a function’s result

A function that cuts storage into pieces can promise the pieces never share an element. The parts of its result are named by position, as a tuple’s part is selected and by the name an ensure gives the result — result.0, result.1:

@disjoint(result.0, result.1)
halves(xs: []int) -> ([]int, []int) = (xs[..<xs.len / 2], xs[xs.len / 2..])

var v: [6]int = [1, 2, 3, 4, 5, 6]
val (l, r) = halves(v[..])
l[0] = 10
r[0] = 40
print(v[0], v[3])
10 40

The standard library’s sysl.slices.split_at(xs, n) is the general form, (xs[..<n], xs[n..]), and makes the same promise. A result list (-> []int, []int) names its parts the same way.

What the caller may rely on: two parts of one call’s result that the function names share no element, after they are destructured, and so does every slice of either. Handed on to a function @disjoint over two parameters, they are storage the call knows is apart rather than storage it takes on trust. A part is not promised apart from the function’s parameters — split_at‘s pieces are its parameter, cut in two.

That is what lets a recursive halving hand each half to a different writer. Every level cuts its own piece, and the two it makes are apart, so every element is written exactly once:

import sysl.slices.split_at

number(xs: []usize, from: usize)
    if xs.len == 1
        xs[0] = from
    else if xs.len > 1
        val (l, r) = split_at(xs, xs.len / 2)

        number(l, from)
        number(r, from + l.len)

@disjoint(y, x)
add_into(y: []usize, x: []const usize)
    for i in 0..<y.len
        y[i] = y[i] + x[i]

var v: [8]usize = [0; 8]

number(v[..], 10)

val (lo, hi) = split_at(v[..], 4)

add_into(lo, hi)
print(v[0], v[3], v[4], v[7])
24 30 14 17

What the function must show — it is checked, never trusted. Every value the body answers, the trailing one and each return‘s, shows the named parts apart in one of two ways: slices of one storage whose bounds meet, where one ends at the very bound the other begins at — the same literal, or the same names and arithmetic, as xs.len / 2 written twice — or two runs of literal bounds that share no element; or parts of one call of a function that makes the same promise, which is how a function answers pieces of pieces. Slicing traps past the end, so two slices that meet at a bound can never both reach one element. A name the check traces a part through holds one value for the whole body: declared once, never assigned, its address never taken. Anything else is refused where it is answered:

@disjoint(result.0, result.1)
cut(xs: []int, n: usize, m: usize) -> ([]int, []int) = (xs[..<n], xs[m..])

var v: [4]int = [1, 2, 3, 4]
val (a, b) = cut(v[..], 2, 2)
print(a.len + b.len)
'cut' is '@disjoint' over 'result.0' and 'result.1', and this answer does not show them apart — 'xs[..<n]' and 'xs[m..]' may share an element; a body shows it with slices of one view that meet at a bound, as '(xs[..<n], xs[n..])', or with parts of one call of a function '@disjoint' over its result

A generic’s body is checked at each instantiation, as its parameters’ types are. The promise changes nothing the back end is told: it is a fact the compiler has checked, for the checks that need to tell two pieces of one storage apart.

A part is held to what a parameter is: it reaches memory, it is a part the result has, and the promise is about two of them, so one alone is refused:

@disjoint(result.0)
cut(xs: []int, n: usize) -> ([]int, []int) = (xs[..<n], xs[n..])

print(1)
'@disjoint' names 'result.0' and no other part of 'cut''s result, and a part is kept apart from the other parts named — name two, as '@disjoint(result.0, result.1)'
@disjoint(result.0, result.1)
count(xs: []int) -> ([]int, usize) = (xs, xs.len)

print(1)
'@disjoint' names 'result.1', a part of 'count''s result whose type 'usize' is a value

A list may name parameters and parts together, and each keeps its own promise: the parameters are kept apart while a call runs, the parts from each other once it has returned.

What the back end is told

A pointer @disjoint names carries LLVM’s noalias on the function’s definition. A view is two pointers and a count in one value, and noalias attaches only to a pointer parameter, so a function naming a view is emitted as two: the function with the signature every caller already uses, taking each named view apart, and an internal, always-inlined body taking the parts, its element address noalias. Its callers, function pointers and exports are unchanged.

The body is inlined into the function at every optimization level, and LLVM’s inliner turns each noalias parameter into the scoped alias metadata it uses for an inlined restrict — the form that survives the function being inlined further, into a caller and a loop unrolled around it. axpy above, built at -O2, is a vector loop with no run-time comparison of the two ranges; the same function without the annotation keeps both copies of its loop and the comparison between them.

An @export of one is the same function behind a C entry point. An extern may not carry it — its body is C, which never reads the sysl declaration, so the promise is made on the C side, as restrict:

@disjoint(d, s)
extern "memcpy" c_memcpy(d: *u8, s: *u8, n: usize) -> *u8
an 'extern''s body is C, which never reads the sysl declaration, so the promise belongs on the C declaration as 'restrict'

@packed and @align(n) — where a struct’s fields sit

By default a struct pads: each field begins on its own alignment, and the aggregate takes the widest of them. That is the layout a program wants unless something outside the program has an opinion.

struct Head
    tag: u8
    len: u32

print(sizeof(Head), alignof(Head))
8 4

Three bytes of that eight are a gap in front of len. Two attributes move a struct off the default, and they are separate axes — one removes the padding between fields, the other raises where the aggregate begins.

@packed — no interior padding

@packed
struct Head
    tag: u8
    len: u32

print(sizeof(Head), alignof(Head), sizeof([4]Head))
5 1 20

The fields are laid end to end, the aggregate needs no alignment of its own, and an array of them has no gap between elements either. A register block, and a C struct that has to match one, are what it is for.

The fields still read and write their own values — packing changes where they sit, not what they are. Each is read and written as storage on no boundary, as C’s packed fields are, so a core that cannot reach a word at an odd address (an ARMv6-M Cortex-M0+) moves the u32 below a byte at a time rather than faulting:

@packed
struct Head
    tag: u8
    len: u32

var h = Head(7, 1000)

h.len += 1

print(h.tag, h.len)
7 1001

A packed field has no address. &s.f is refused, and so is any *T into one:

@packed
struct Head
    tag: u8
    len: u32

var h = Head(1, 2)
val p = &h.len

print(1)
'&' here makes a '*uint' into Head, which is '@packed' — its fields sit at their declared offsets, so this one need not be on a 'uint' boundary, and every use of a '*uint' is entitled to assume that it is. Read or write the field through the struct, which is where the offset is known, or take the address of the Head itself

The field sits at its declared offset, which is very often not a multiple of its own alignment — that is the point of the attribute — while a *u32 is a *u32 wherever it came from, and every use of one is entitled to assume the address is aligned. Only the escaped address loses that, and it would lose it arbitrarily far from the & that made it. The struct’s own address is untouched, being an ordinary pointer to an ordinary aggregate.

A volatile field off its boundary is refused. A volatile access is exactly one access of its width (volatile) — on ARM one ldr or str — and that one instruction at an address off the type’s boundary faults on an ARMv6-M core always, and on any core’s device memory, which is what volatile is for. Byte-at-a-time is not an option: it would be four accesses to one register. So selecting such a field is refused where it is written:

@packed
struct Reg
    tag: u8
    word: volatile u32

@export("get")
get(r: *Reg) -> u32 = r.word
'Reg.word' is a 'volatile' uint at byte 1 of Reg, which is misaligned: 1 is not a multiple of 4, a uint's alignment on this target. A volatile access is exactly one access of its width, and one off that boundary faults on device memory — place it at a multiple of 4 bytes from the start of Reg

Misaligned is exact: a volatile value sits at a byte offset, counted from the start of the struct whose field is selected, that is not a multiple of its type’s natural alignment on the target — what alignof says of the type outside any packed struct, so a volatile usize at byte 4 is refused on a 64-bit target and accepted on a 32-bit one. The offset counts through every struct and array that holds the value by value: a packed struct held one byte into another, an ordinary struct held in a packed one, a packed one held in an ordinary one (it needs no alignment of its own, so it may begin at any byte), and an array’s element, each a stride on from the last — so an array is on its boundary when its first two elements are. A bitfield struct is one volatile container of its width, and a C bitfield’s unit is the access it names. A generic struct is checked at each instantiation, and a field of a type whose alignment is one byte (u8, bool) is never off its boundary. An ordinary field is unaffected: Head.len above still moves a byte at a time. Pad the register to its boundary instead:

@packed
struct Reg
    tag: u8
    pad: [3]u8
    word: volatile u32

print(sizeof(Reg))
8

Bitfields — an iN field in exactly N bits

Inside @packed, an integer field occupies exactly its declared width. A packed struct whose fields all lower to an integer, at least one of them narrower than a byte, is one unsigned integer — and its fields are ranges of it:

@packed
struct Ctrl
    enable: u1
    mode: u3
    prescale: u4

val c = Ctrl(1, 5, 9)

print(sizeof(Ctrl), c.enable, c.mode, c.prescale)
1 1 5 9

No syntax was needed for this, which is why there is none: the open integer family already does the work C needs int x : 3 for, so a five-bit field is written u5 in a struct exactly as it is anywhere else.

The rule is the same for a whole number of bytes that is not a power of two: a u24 inside @packed is three bytes, a u40 five, a u48 six, where on its own each is rounded up to the next power of two. So a three-byte length overlays a wire format’s three bytes:

@packed
@byte_order(big)
struct FrameHeader
    len: u24
    kind: u8
    flags: u8
    stream: u32

print(sizeof(FrameHeader), offsetof(FrameHeader, kind), offsetof(FrameHeader, stream), sizeof(u24))
9 3 5 4

Two things follow that C leaves to the implementation, and pinning them is the point of the feature rather than a detail of it — it is why portable embedded C avoids bitfields and writes the shifts out by hand. The bits fill from the least significant upward in declaration order, and a field straddles a byte boundary rather than moving off it. Both are visible in the storage:

import sysl.raw.ptr_cast

@packed
struct Ctrl
    enable: u1
    mode: u3
    prescale: u4

var arena: [4]u8 = [0; 4]
var c: *Ctrl = ptr_cast(&arena[0])

c.enable = 1
c.mode = 5
c.prescale = 9

print(arena[0])
155

155 is 1 | 5 << 1 | 9 << 4: the first field declared is the low bits.

The rule is stated over the integer’s value and never over memory bytes. Written the other way — “the low bits of byte 0” — it would be a claim about endianness, which nothing in sysl is allowed to make. Written this way it costs nothing: the struct is an integer, so how it reaches memory is the target’s ordinary byte order for an integer of that width. A wire format’s byte order belongs to the protocol rather than to the CPU, and a struct of whole-byte fields states it with @byte_order below.

Being one integer, it goes to and from that integer with nothing in between. T::Bits(n) makes the struct from the integer it is stored as and x.bits reads that integer back — the same pair a fixed-point type has, and for the same reason: neither converts anything, both read one set of bits at another type. A system register read whole is decoded in one step:

@packed
struct Esr
    iss: u25
    il: u1
    ec: u6
    iss2: u5
    res0: u27

val e = Esr::Bits(0xF2000000)

print(e.ec, e.il, e.iss, e.bits)
print((e with { iss = 7 }).bits)
60 1 0 4060086272
4060086279

The integer is the struct’s own: unsigned, and as wide as its container — the fields’ widths added and rounded up to whole bytes, so Esr is a u64 and a struct of 3 and 21 bits a u24. A literal takes that type from where it sits, and a narrower unsigned value widens to it as any argument does, so a 32-bit register read decodes into Esr directly:

@packed
struct Esr
    iss: u25
    il: u1
    ec: u6
    iss2: u5
    res0: u27

val low: u32 = 0xF2000000

print(Esr::Bits(low).ec)
60

A value that would have to be narrowed, or change its sign, is refused, and the conversion is written:

@packed
struct Esr
    iss: u25
    il: u1
    ec: u6
    iss2: u5
    res0: u27

val word: int = 0x3c

print(Esr::Bits(word).ec)
'Esr::Bits' takes the ulong 'Esr' is stored as, not int

Esr(raw) is not the spelling because it is already the constructor, which takes one value per field — and a bitfield struct of a single field would make it mean two things. A field or member the struct declares named bits is what .bits reaches, ahead of the container.

A @packed struct whose fields are all whole bytes converts too, though it is not stored as one integer. Its fields are read as ranges exactly as a bitfield struct’s are — the first declared is the low bits — each as wide as its type, so the integer is the fields’ widths added: a u8 and a u16 make a u24, a u32 and a u8 a u40. Its storage does not change: it is still its fields end to end, with the size, alignment and C layout a __attribute__((packed)) struct has, so a protocol header or a C mirror is exactly what it was. On a little-endian machine — every machine sysl targets — the integer is the struct’s bytes read in order, wherever each field is a power of two bytes wide. A register modelled from its description converts whether or not any of its fields is narrower than a byte:

@packed
struct Status
    code: u8
    _: u8
    count: u16

val raw: u32 = 0x00070011
val s = Status::Bits(raw)

print(s.code, s.count, s.bits == raw)
print(Status(0x22, 0, 9).bits, sizeof(Status))
17 7 true
589858 4

A @byte_order struct’s integer holds its fields’ values, not the bytes of the wire: each field is reversed on the way in and out exactly as a field read and write reverses it.

A generic struct converts at each instance, as wide as that instance’s fields. The instance may be written out in place on the left of ::, as an alias of it would be, and a generic body reads .bits at the width its instantiation has:

@packed
struct Pair[T]
    lo: T
    hi: T

show[T](p: Pair[T]) = print(p.bits)

val a = Pair[u8]::Bits(0x0201)

print(a.lo, a.hi, Pair[u16]::Bits(0x00020001).hi)
show(a)
show(Pair[u16](1, 2))
1 2 2
513
131073

An ordinary struct, a union and a struct with C bitfields have no ::Bits, and P::Bits(n) on one is refused saying which it is.

Nothing is checked on the way in. The bits are taken as they are, so an enum-typed field may hold a value none of its variants has; the field is held to its variants where it is read, as one reached through a pointer the hardware filled is.

Every field has to be an integer — or a simple enum, another bitfield struct, or a @packed struct that converts itself. A bool is not one here — its storage is a byte and its representation a bit — and neither is a pointer, a float or an array:

@packed
struct Mixed
    p: *u8
    a: u3

val m = Mixed(null, 1)

print(m.a)
is one integer, and every field of it has to be one too

A @packed struct of whole bytes may hold any of those — it is laid out as fields — but then it does not convert, and ::Bits names the field that stops it:

@packed
struct Sample
    id: u8
    value: f32

print(Sample::Bits(1).id)
'Sample::Bits' has no integer to make it from: its field 'value' is f32 — only a '@packed' struct whose every field is an integer, a simple enum, a bitfield struct or a '@packed' struct that converts itself converts to one

A bitfield struct may be a field of another, and there it occupies its own fields’ widths — the bits it uses, not the whole bytes its own container rounds up to. That is how a group of flags shared by several registers is named once: a page descriptor’s ten lower attribute bits sit at bit 2 of the block, the page and the stage 2 descriptor alike. A field of the inner struct is read and written through both containers, and .bits and ::Bits of the outer one cover all of it:

@packed
struct Lower
    attr: u3
    ns: u1
    ap: u2
    sh: u2
    af: u1
    ng: u1

@packed
struct Desc
    valid: u1
    page: u1
    lower: Lower
    oa: u36
    rest: u16

var d = Desc(1, 1, Lower(5, 0, 1, 3, 1, 0), 0x12345, 0)

d.lower.ap = 2

print(sizeof(Desc), d.lower.attr, d.lower.ap, d.lower.sh, d.oa, d.lower.bits)
8 5 2 3 74565 485

A struct that is not one integer is refused there — an ordinary struct, or a @packed one whose fields are all whole bytes:

struct Pt
    x: u8
    y: u8

@packed
struct Flags
    on: u1
    at: Pt

val f = Flags(1, Pt(2, 3))

print(f.on)
Pt is laid out as fields rather than as one integer — only a '@packed' struct with a field narrower than a byte nests in another, in its own fields' width

What decides whether a @packed struct is one integer is its own integer and enum fields, so an ordinary @packed struct whose fields are whole bytes lays a bitfield struct out as a field of its size, as it always has.

@packed
struct Ctrl
    enable: u1
    mode: u3
    prescale: u4

@packed
struct Block
    ctrl: Ctrl
    count: u32

var b = Block(Ctrl(1, 5, 9), 3)

b.ctrl.mode = 2

print(sizeof(Block), b.ctrl.enable, b.ctrl.mode, b.count)
5 1 2 3

A whole-byte @packed struct that converts may itself be a field of another that does, at every depth, and there it is as wide as its own integer. Outer below is a u32, Header in it a u24:

@packed
struct Header
    a: u8
    b: u16

@packed
struct Outer
    h: Header
    c: u8

val o = Outer::Bits(0x04030201)

print(o.h.a, o.h.b, o.c, o.bits == 0x04030201)
1 770 4 true

A bitfield may be volatile, and it means a volatile access of the container. Reading a field is one volatile load of the whole container; writing one is a volatile load and a volatile store of it — what C does with volatile unsigned x : 3, and what lets a @packed struct describe a hardware register rather than only a data layout:

import sysl.raw.ptr_cast

@packed
struct Reg
    enable: volatile u1
    mode: volatile u3
    prescale: volatile u4

var block: [4]u8 = [0; 4]
var r: *Reg = ptr_cast(&block[0])

r.enable = 1
r.prescale = 9
r.mode = 5

print(block[0], r.enable, r.mode, r.prescale)
155 1 5 9

The qualifier belongs to the container rather than to one range of it, so writing it on any field qualifies every access to the struct. Every field of a bitfield struct is bits of one word, so there is no shadow field here to stay ordinary — which is the thing per-field qualification buys in an ordinary register block. The struct itself still may not carry the qualifier, and does not need to:

import sysl.raw.ptr_cast

@packed
struct Ctrl
    a: u3
    b: u5

var p: *volatile Ctrl = ptr_cast(4096)

print(p.a)
'volatile Ctrl' is not a type

A write is a read-modify-write, and nothing diagnoses what that costs. A device is entitled to one bus cycle, so writing a field of a register with clear-on-read or write-1-to-clear semantics corrupts the ranges beside it — the read that begins the sequence has already had its effect. Nothing in the language describes a register’s read semantics, so this is stated rather than refused, exactly as it is in C. A register of that kind keeps its volatile u32 and its shifts, where the single access is written out.

A field that is a set of named values is a simple enum, which is one integer and may be volatile like any other. A data enum may not — a tag beside a payload is more than the one access the qualifier promises.

Reading an enum field is the checked conversion E(n), made where the field is read. The bits were put there by hardware or by a cast, and a width with values no variant covers — six bits of an exception class with two dozen defined — can hold one of them. A value no variant has stops the program at the read, reporting it the way a failed bounds check does, rather than reaching Image as a wrong name or a match as no arm at all:

value 42 is not one of the enum's variants (main.sysl:12:11)

An enum whose variants cover every value of its width — two variants in u1, four in u2 — has nothing to check, and its read is the shift and nothing more.

The fallible read is E.try(s.field) — the conversion that answers instead of trapping, handed the field itself. The field’s bits are read without the check and answered for: Some of the variant they are, or None. It is how a kernel asks for “the exception class, if it is one I know” without declaring the field a plain u6:

import sysl.raw.ptr_cast

enum Ec: u6
    Unknown = 0x00
    Brk = 0x3c

@packed
struct Esr
    low: u26
    ec: Ec
    high: u32

describe(class: Option[Ec]) -> string = class match
    Some(c) -> Ec::Image(c)
    None -> "a class this kernel does not know"

var raw: u64 = 0x2a << 26
val e: *Esr = ptr_cast(&raw)

print(describe(Ec.try(e.ec)))

raw = 0x3c << 26
print(describe(Ec.try(e.ec)))
a class this kernel does not know
Brk

A signed enum’s field answers by its two’s-complement bits, as its read does, and where the variants cover the width try is Some with no test at all. Only a bitfield’s enum field is taken as it is — a value of the enum read anywhere else has been held to its variants already:

enum Ec: u6
    Unknown = 0x00
    Brk = 0x3c

val c = Ec.Brk

print(Ec.try(c).is_some())
'Ec' converts an integer, but the value has type Ec — a value of the enum is already one of its variants

A bitfield has no byte offset, so offsetof says so rather than rounding down to the byte the field begins in:

@packed
struct Ctrl
    enable: u1
    mode: u3
    prescale: u4

print(offsetof(Ctrl, mode))
is a bitfield — it starts at bit 1 of the struct and is 3 bits wide, so it has no byte offset

It has no address either, which is the packed rule above and needs no separate ruling.

Across a C boundary it travels as what it is — a packed struct of one integer — and not as a C bitfield struct. Those are different things on some machines, and it is C’s own doing: MSVC allocates unsigned a:1 into a four-byte unit where the Itanium ABI packs it into a byte, so a C bitfield struct of the same fields need not even be the same size everywhere. A sysl bitfield struct is one integer on every target. Where the point is to match a C header’s bitfields, the fields are written as C writes them — the next section.

C bitfields — name: uW in T, laid out as C lays it out

A field written name: uW in T is C’s T name : W;: W bits allocated in a unit of T, the type C declares the field with, by the target’s own C rule. A struct with any such field is an ordinary struct whose other fields sit where C puts them after a bitfield, so its size, its boundary and every offset are what the C compiler for that target gives the same declaration. There are three rules: Itanium (Linux, macOS, the BSDs, RISC-V, WebAssembly, i386 and CRAFT), AAPCS (Arm outside Apple’s platforms: an unnamed or zero-width field raises the struct’s boundary too) and MSVC (Windows: a run of bitfields shares a unit only while their declared types have one size, and no field ever straddles a unit).

struct Status
    ready: bool in u32
    mode: u3 in u32
    count: u12 in u32
    tag: u8

var s = Status(true, 5, 4000, 7)

s.mode = 6
s.count += 1
print(sizeof(Status), alignof(Status), offsetof(Status, tag), s.ready, s.mode, s.count, s.tag)
4 4 2 true 6 4001 7

The field’s value type is an integer of W bits, a simple enum whose underlying type is W bits wide, or bool (bool in bool is C’s _Bool b : 1). T is an integer type of 8, 16, 32 or 64 bits or bool, W fits in it, and the value’s signedness is T‘s — i3 in u32 is refused, because C’s read of the same field would disagree with sysl’s.

A read loads the unit, shifts the field down and cuts it to its width, so a signed field comes back with its sign; a write is a read-modify-write of the unit, so the fields beside it — in the unit, in the next unit, and the ordinary fields around them — keep what they held. Compound assignment reads the field, computes at its type and writes it back. An enum field is checked where it is read, as any enum read from storage is, and E.try(s.field) answers instead of trapping:

struct Range
    lo: i3 in i32
    hi: i3 in i32
    rest: u8

var r = Range(-4, 3, 255)

r.lo += 1
r.hi = -4
print(r.lo, r.hi, r.rest)
-3 -4 255

An unnamed field, _: uW in T, and a zero-width one, _: in T, only move the fields after them — C’s T : W; and T : 0;. As in C, nothing initializes them: a construction and a positional pattern name every field but these, and a value built field by field holds zero in their bits. A derived Eq, Ord, Hash or Display reads every named field:

struct Reg derives Eq, Display
    lo: i8 in i32
    _: u4 in u32
    hi: u8 in u32
    _: in u32
    next: u16 in u32

val r = Reg(-3, 200, 9)
val Reg(lo, hi, next) = r
val s = r with { hi = 7 }

print(sizeof(Reg), lo, hi, next, s, r == s)
8 -3 200 9 Reg(-3, 7, 9) false

A module val of one is laid down as data, its bits where C puts them, as any constant struct is.

volatile makes every access to the field’s unit exactly the declared type’s width — one load of the whole unit, and for a write one load and one store of it, never merged with a neighbour and never narrowed. That is the AAPCS rule for volatile bitfields, and what a peripheral register needs: a device may ignore, or fault on, an access narrower than its register. As with today’s bitfields, the qualifier on any field of a unit qualifies every access to that unit, and nothing diagnoses what a read-modify-write costs a register whose read clears it. Under @packed a volatile field that lies across two units of its declared type has no single access to make, and is refused. On AArch64 and on 32-bit ARM each of those unit accesses is one plain ldr/str of the unit’s width from a bare register address (on 32-bit ARM a u64 unit is two, one per word), as every volatile access is there (memory). On 32-bit ARM a @packed struct of narrow fields is reached as its one container, so with a volatile field the container must be 8, 16 or 32 bits or whole 32-bit words: two u12 fields (24 bits) or two u20 fields (40) are refused there, and a u12 with a u20 is one ldr.

@packed on such a struct is GNU’s packed (MSVC’s pack(1)): the fields abut bit for bit, and @align(n) raises it as it raises any struct. The two kinds of bitfield do not mix in one struct — a @packed struct with an in field may not also hold today’s narrow fields — and @byte_order is refused on one, there being no portable C spelling of a bitfield stored the other way round.

A C bitfield has no address: it is bits inside a unit and begins on no byte, so &, ref and offsetof refuse it, and the struct is reached instead:

struct Status
    ready: bool in u32
    mode: u3 in u32

var s = Status(true, 5)
val p = &s.mode

print(p[0])
'&' here would reach 'Status.mode', a C bitfield — it is bits inside a unit of its declared type and has no address of its own

What carries a count stays out: a struct with C bitfields is laid out as C lays its counterpart out, so a string, a slice or a reference among its fields is refused.

It crosses a C boundary by value as C’s own struct does — as an extern‘s argument or result, an @export‘s parameter or result, and through a *extern pointer either way. A C compiler classifies such a struct by its fields, each named bitfield an integer of its own, and that is not always how an ordinary struct of the same size travels: on x86-64 { double d; unsigned a:3; } comes back in a whole second register, WebAssembly passes one bitfield directly but two through memory, and RISC-V’s lp64d gives a float beside one bitfield a register of each but a float beside two bitfields a single integer register. sysl follows clang on every machine it builds for:

struct Status
    ready: bool in u32
    mode: u3 in u32

@export("next")
next(s: Status) -> Status = Status(!s.ready, s.mode + 1)

val step: *extern(Status) -> Status = &next
val s = step(Status(true, 5))

print(s.ready, s.mode)
false 6

The same holds for anything holding one by value, at any depth: a struct whose field is one, an array of them, a union over one, a struct holding any of those, and a struct with an anonymous member that declares bitfields — which are the struct’s own, as in C:

struct Status
    ready: bool in u32
    mode: u3 in u32

struct Device
    id: u16
    states: [2]Status

@export("modes")
modes(d: Device) -> u32 = u32(d.id) * 100 + u32(d.states[0].mode) * 10 + u32(d.states[1].mode)

val report: *extern(Device) -> u32 = &modes

print(report(Device(1, [Status(true, 5), Status(false, 2)])))
152

Not built in this release: its declaration in a generated header, which writes it as an incomplete type C reaches through a pointer.

@align(n) — where the aggregate begins

@align(64)
struct Head
    tag: u8
    len: u32

print(sizeof(Head), alignof(Head))
64 64

The size rounds up as well as the start, so an array keeps every element on the boundary. It may only raise: asking for less than the fields already need changes nothing, since lowering is what @packed is for and a type that under-promised would be unsound to pass around.

The boundary travels with every value of the type the compiler lays down: a module var or val, a static, a local, an element of an array, a field of another struct — which begins on the boundary and raises its holder’s to match — and the payload of an enum variant carrying one. A value on the heap keeps it too. A box (&T), a buffer or a task’s frame takes its storage on the type’s boundary, and the payload is laid after the box’s header on it. A heap the manifest names is handed the boundary with every block; under libc’s pair, whose malloc promises only two words — sixteen bytes on a 64-bit machine and eight on a 32-bit one — storage of a type aligned beyond that comes from the C library’s aligned allocation:

@align(4096)
struct Page
    e: [512]u64

var p: Page
p.e[3] = 9

val b: &Page = p

print(usize(&*b) % 4096, b.e[3])
0 9

Every access through an address of the type claims its whole boundary. With libc’s malloc and free on a POSIX system, WASI or Windows this needs nothing from the program; on a machine with no C library the program names a heap of its own, as packages.md § A type aligned beyond what malloc promises says.

The bound is folded rather than lexed, so a program writes the name it already has for the number, and arithmetic over one works:

const CACHE_LINE: int = 64

@align(CACHE_LINE)
struct Head
    tag: u8
    len: u32

print(alignof(Head))
64

It must be a power of two — an address is aligned by having low bits clear, so a boundary of six is unsatisfiable rather than merely weak:

@align(6)
struct S
    a: int
'@align(6)' on 'S' is not an alignment — a boundary is a power of two, since an address is aligned by having low bits clear

They compose

The gaps between fields and the boundary the whole thing starts on are different questions, so a wire header that has to live in a DMA-capable buffer is both at once. The order they are written in does not matter.

@packed
@align(16)
struct Head
    tag: u8
    len: u32

print(sizeof(Head), alignof(Head))
16 16

Sub-byte fields are a separate axis and @align(n) does not touch them. Inside @packed an iN field occupies exactly N bits (bitfields above); the boundary the aggregate begins on is this attribute, and a bitfield struct takes one like any other.

@align(n) on a binding

The boundary may also be written on the storage itself — a var or a val, at the top of a module or inside a function, and on the static spelling of either.

@align(64)
var region: [128]u8

region[0] = 7u8
print(region[0], region.len)
7 128

It says nothing the struct form could not. A boundary put on a type travels with every value of it, and a named aligned type is reusable where a repeated attribute is not — so reach for the type when more than one thing needs the boundary. What this spelling saves is at the use site: a buffer wrapped in a struct is read as region.bytes[i] rather than region[i].

@packed has no meaning here, and says so, because it describes the arrangement of fields inside an aggregate and a binding has none:

@packed
var n: int = 1

print(n)
'@packed' describes how a struct's fields are laid out, so it can only mark a struct — a 'var' or a 'val' has no fields to pack, and '@align(n)' is the one of the two that may stand above one

A binding that names several things is refused for a related reason: there is no one object for a boundary to be about.

@align(16)
var a, b = 1, 2

print(a, b)
'@align(n)' is the boundary one object's storage begins on, and a binding that names several has no one object for it to be about — declare them on lines of their own

The bound is held to the same rule a struct’s is — a power of two, folded rather than lexed, so a const or arithmetic over one is what a program writes.

@byte_order(big) and @byte_order(little) — the order a struct’s integers keep their bytes in

A protocol header or an on-disk record has a byte order of its own, and it is the format’s rather than the machine’s: IPv4, UDP and TCP keep the most significant byte first, on every machine that reads them. @byte_order says so once, above the struct, and every integer field of it is then stored in that order and read and written as an ordinary integer.

import sysl.raw.ptr_cast

@packed
@byte_order(big)
struct Udp
    src: u16
    dst: u16
    len: u16
    sum: u16

var h = Udp(5353, 53, 8, 0)

h.len += 4

val raw: *u8 = ptr_cast(&h)

print(h.src, h.dst, h.len)
print(raw[0], raw[1], raw[4], raw[5])
5353 53 12
20 233 0 12

5353 is 0x14E9, and the first byte in memory is 0x14 — the order the wire wants, though the machine running this is little-endian. The struct’s storage is the bytes of the format, so a whole value is copied, cast over a frame, compared or handed to C exactly as those bytes, and the reversal happens only where a field turns into a value and back: a read, a write, a construction, a destructuring. On a machine whose own order is the one stated, nothing is reversed at all.

@byte_order(little) is the other word, for a format that keeps the least significant byte first — most file formats, and every one written by a little-endian machine. On today’s targets it reverses nothing, since every machine sysl targets is little-endian; it is written so the struct still means what it says on the first one that is not.

It is a separate axis from @packed and @align(n), and composes with both. A byte order says nothing about gaps between fields, so a header whose fields are not already on their own boundaries is @packed as well — Udp above is four u16s and would lay out the same either way.

What a field may be, inside one:

  • an integer of a whole number of bytes — u16, i32, u24 — or a fixed-point number, or a simple enum (whose discriminant is the integer that is stored), or a name for any of these: these are the fields whose bytes are reversed;
  • a byte, a bool, or an array of either — one byte has no order to keep;
  • a struct that states its own @byte_order, or that holds only bytes, which is how a format whose fields disagree is written: two structs, one a field of the other.

A field is read through the struct and nowhere else, so its address is refused — a *u16 is read in the machine’s order, and would hand every later read the bytes backwards:

@byte_order(big)
struct Hdr
    len: u16

var h = Hdr(1)
val p = &h.len

print(1)
'&' here would reach a 'ushort' inside Hdr, which is '@byte_order(big)' — the field holds its bytes most significant first whatever the machine does, and a '*ushort' is read in the machine's order. Read or write the field through the struct, which is where the order is applied, or take the address of the Hdr itself

A ref is refused in the same words, for the same reason. The address of the struct itself is untouched — it is an ordinary pointer to the bytes of the format, which is what a frame is.

Every other kind of field is refused, and the refusal says what to hold instead. A float’s bytes are put in an order through its bits:

@byte_order(big)
struct Sample
    level: f32

print(sizeof(Sample))
'Sample.level' is f32, and 'Sample' is '@byte_order(big)' — a float is put in an order through its bits, which the language does not do behind a field read. Hold it as the unsigned integer of its width and convert with '.bits()' and 'from_bits'

An array of wider integers has elements, which are read by index rather than as fields:

@byte_order(big)
struct Table
    slots: [4]u16

print(sizeof(Table))
'Table.slots' is [4]ushort, and 'Table' is '@byte_order(big)' — an element is reached by index rather than read as a field, and the order is applied where a field is read. Hold it as bytes, '[N]u8', or make the element a '@byte_order' struct of its own

A struct held as a field keeps its own order, so one that states none would be read in the machine’s:

struct Pair
    a: u16
    b: u16

@byte_order(big)
struct Outer
    p: Pair

print(sizeof(Outer))
'Outer.p' is Pair, and 'Outer' is '@byte_order(big)' — 'Pair' keeps its integers in the machine's order, so a field of it would be read in the wrong one. Mark it '@byte_order' too

An address means nothing in the memory of whatever reads the bytes:

@byte_order(big)
struct Link
    next: *u8

print(sizeof(Link))
'Link.next' is *byte, and 'Link' is '@byte_order(big)' — an address means nothing outside the memory it points into, and a struct with a byte order is laid out to be written somewhere else. Hold an offset or an index instead

A bitfield struct takes one too, and the order is its container’s. Its fields are ranges of one integer, so the integer is what is stored in the stated order — IPv4’s sixteen bits of flags and fragment offset are exactly this:

import sysl.raw.ptr_cast

@packed
@byte_order(big)
struct Frag
    offset: u13
    more: u1
    dont: u1
    reserved: u1

@packed
@byte_order(big)
struct Ipv4Tail
    ident: u16
    frag: Frag
    ttl: u8

var t = Ipv4Tail(0x1c46, Frag(0, 0, 1, 0), 64)
val raw: *u8 = ptr_cast(&t)

print(raw[2], raw[3], t.frag.dont, t.frag.bits)
64 0 1 16384

The ranges are taken from the integer, as they are for any bitfield struct, so dont is bit 14 of 0x4000 and the wire holds 0x40 0x00. A bitfield struct with no order of its own is refused as a field of one that has an order, its integer being in the machine’s.

A union takes one too, and it orders every integer of every field — the bytes are shared, so a word written through one field reads back in network order through another, and building the union from that field stores it in the stated order. An anonymous member’s fields are the enclosing type’s, so the order above the type reaches them as well. Each field is held to the rule above:

@byte_order(big)
union Wire
    raw: [4]u8
    word: u32

var w = Wire(word = 0x01020304)

print(w.raw[0], w.raw[3])
w.raw = [0, 0, 1, 2]
print(w.word)
1 4
258

The order is one of two words, and the attribute marks a struct or a union and nothing else:

@byte_order(middle)
struct Odd
    a: u16

print(1)
'@byte_order' names the order in parentheses — '@byte_order(big)' for a network header, which keeps the most significant byte first, or '@byte_order(little)' for a format that keeps the least significant first
@byte_order(big)
var n: u16 = 1

print(n)
'@byte_order(...)' is the order a struct's integer fields keep their bytes in, so it can only mark a struct — and this declares none. A lone value is put in an order with 'to_be' and 'from_be', the members 'sysl.math.ByteOrder' gives every whole-byte integer

A value that is not in a struct is put in an order with sysl.math.ByteOrder‘s to_be and from_be, and a byte-ordered struct is read out of bytes, every bool and enum in it checked, with sysl.encoding.Plain‘s decode.

@pinned on a struct — a value C holds pointers into

Some C state points into itself. littlefs’s lfs_t keeps a pointer to its configuration and a list of open files threaded through its own storage; a USB stack’s device struct is handed to interrupt handlers by address. Copy one, and C goes on writing through pointers into the copy that is gone. @pinned above a struct says a value of it is never copied or moved once it is built.

A new value has no address yet, so it may land once — in a binding, in module storage, in a &T box, in a field of another pinned value, or as what a function hands back, where it is built in the caller’s slot. After that it is reached only through it: a field, &, or a method taking *self.

struct Config
    block: int

@pinned
struct Fs
    cfg: *Config
    open: int

    mount(*self, cfg: *Config)
        self.cfg = cfg
        self.open = 1

    size(*self) -> int = self.open * self.cfg.block

// A constructor hands back a value it has just built, and the caller's storage is where it lands.
filesystem(open: int) -> Fs = Fs(null, open)

var cfg: Config = Config(512)
var fs: Fs = filesystem(0)

main()
    fs.mount(&cfg)

    val boxed: &Fs = filesystem(2)

    boxed.mount(&cfg)
    print(fs.size(), boxed.open, fs.cfg.block)
512 1 512

Everything that would read the value whole is refused, each with what it does to the value:

@pinned
struct Fs
    open: int

var fs: Fs = Fs(0)

main()
    var b = fs
    print(b.open)
'Fs' is '@pinned' — C may hold pointers into a value of it, so a value is never copied or moved once it is built, and this binds a copy of it. Take its address ('&') or name it with a 'ref' instead, or build the value in place

The same sentence ends differently for each form:

writtenrefused because this…
var b = fs, val p: &Fs = fsbinds a copy of it / copies it into a new box
c = fs, fs = Fs(2)assigns a copy of it / assigns over a value already built, which C may still hold pointers into
take(f: Fs), take(fs)parameter ‘f’ of ‘take’ takes it by value / passes it by value
peek(self) in the structreceiver of ‘Fs.peek’ is taken as ‘(self)’, which is a copy
give() -> Fs = fsreturns it by value — only a value built right there may be handed back
() -> here.open returned or storedthis closure captures ‘here’ by value — a closure that does not outlive the local holds it by reference, and is accepted
val Fs(n) = fs, fs matchtakes it apart / matches on it, which reads it whole into a copy first
(Fs(3), 1), Option[Fs]puts it in a tuple / carries it by value in variant ‘Some’
for item in all over [2]Fscopies each element into the loop variable
Buf[Fs]makes it an element of a buffer whose storage moves when it grows

Assignment is refused even of a new value: the one already there may be what C points into. A pinned value is built once, where it lives, and changed afterwards through its own members.

A type holding one is pinned too, and says so

A struct holding a pinned value by value moves it whenever the struct moves, so it is pinned as well — and it is marked, not inferred, so the reader of the outer declaration sees that its address matters:

@pinned
struct Fs
    open: int

struct Holder
    fs: Fs

var h: Holder = Holder(Fs(1))

main()
    print(h.fs.open)
and this holds it by value in field 'fs' of 'Holder', which moves it whenever it moves. Mark 'Holder' '@pinned' too, or hold a '&Fs' or a '*Fs'

An array of pinned values is pinned the same way, and a []Fs view, a *Fs and a &Fs are all fine: none of them is the value.

A generic is held to it at the instance

A generic instantiated at a pinned type is checked as the instance, the way an instance is held to its declaration’s @no_alloc: addr[T](x: *T) -> *T is fine at Fs, and first[T](x: *T) -> T = *x is refused, the refusal naming first.Fs. A container is refused at the type when any of its members is @relocates, since that member moves its elements — which is why Buf[Fs] is.

A drop is the one member taking self that a pinned struct may have: it is handed the value as the value is destroyed. And @pinned marks a struct beside @packed, @align(n) and @export(...); above a function it is still the per-core promise of @per_cpu.

@section("...") — where a symbol lands

@align(n) says what boundary storage begins on. @section says where the storage is, and it is what a program reaches for when the address of a thing is part of what it is: a vector table at the address the processor fetches from, storage in .noinit that survives a warm reset, a DMA buffer in the RAM bank the engine can reach, a function copied into RAM so it can run while flash is being erased.

It marks whatever occupies an address — a module var, a module val, a function, and an asm block written at the top of a file, which is how code that runs before there is a stack is placed (see inline assembly; such a block also takes @align(n)).

@section(".noinit")
var crash_reason: u32

@section(".ramfunc")
erase_page(n: usize) -> bool = n < 64

Nothing on this page runs one of these. A section name belongs to the target’s object format — .noinit is ELF’s spelling, __DATA,__mine is Mach-O’s — so a program that places something is a program about one machine, and the machine this page’s examples run on is not the interesting one.

That is also why the compiler does not check what is in the string. A set of characters chosen by sysl would refuse a section some target requires, and the assembler that will not take one says so better than a rule here could. It is extern‘s link name and @export‘s symbol read a third time: a spelling belongs to whoever consumes it.

It composes with @align(n)

The two are different axes — one is the boundary an object begins on, the other is where it lives — and a statically placed stack is written with both. The order does not matter.

@align(4096)
@section(".noinit")
var page_table: [512]u64

A placed symbol is kept

Nothing inside the program reads a table that a linker script gathers — that is the whole point of writing one — so a placed definition is a root, exactly as an interrupt handler and an @exported function are, and a placed symbol is marked so that the optimizer does not delete an object with no reader.

Without that second half the attribute would compile, link, and place nothing. The failure would be the absence of a section, which is not a thing anybody looks for.

A placed definition a dependency supplied is kept only where your program reaches its module, which is the rule the FFI page states for every kind of root at once. It bites hardest here, because being marked is exactly what stops anything downstream undoing it: a placed definition kept for want of the rule is bytes in your image that no optimizer will remove, in the region the attribute exists to manage. A package whose placed definition you want is one you name — an import is enough.

Where nothing runs an initializer, placed storage holds what the image carries

Whatever reads a placed object reads the bytes the image carries: a boot ROM walking a table, a processor fetching a vector. Storage whose initializer has to compute something is filled when the program starts, by a hosted machine’s loader or by a bare image’s main. Two builds never run that code: a C archive for a freestanding target, and a bare image whose entry never calls main. There a placed val or var has to be constant data, laid straight into the object file, as C’s static const initializer is.

An address is constant data, because the linker fills it in. That holds for the address of a function or of module storage, and for that address read as an integer of exactly the machine’s word: usize(&f), and u32(usize(&f)) on a 32-bit target, are C’s (uint32_t)(uintptr_t)&f. A constant added to or subtracted from that word is constant data too. All of these are relocations, an R_ARM_ABS32 here, and the + 1 is its addend:

@export("rom_popcount32")
popcount(x: u32) -> u32 = x.count_ones()

idle() = ()

struct Entry
    tag: u32
    at: u32

@section(".rom.table")
val TABLE: [3]u32 = [7, u32(usize(&popcount)), u32(usize(&idle)) + 1]

@section(".rom.entry")
val ENTRY: Entry = Entry(2, u32(usize(&popcount)))

In those two builds, storage that something has to compute is refused rather than placed, because nothing would ever fill it and the section would hold zeros:

extern "rom_next" next() -> u32

@section(".rom.g")
val G: u32 = next()
'G' is placed in section ".rom.g", so what reads it is outside the program and reads the bytes the image carries

An address cut to a narrower integer, or widened past the machine’s word, is not something any relocation writes. It stays code, so in those builds it is refused when placed, and the refusal says why:

@export("rom_popcount32")
popcount(x: u32) -> u32 = x.count_ones()

@section(".rom.n")
val N: u16 = u16(usize(&popcount))
'N' converts an address to ushort, and an address is a word the linker fills in at the machine's 32 bits — no relocation writes one at 16

A function placed in a section is a way in from outside the program, as an @export is. In a freestanding archive it may not reach storage that an initializer fills, for the same reason:

extern "rom_next" next() -> u32

val COUNT: u32 = next()

@section(".ramfunc")
tick() -> u32 = COUNT + 1
'tick' is placed for code outside the program to enter and reaches 'COUNT'

A bare image whose main runs fills computed placed storage there, before the program’s own statements, as a hosted machine’s loader does. A buffer in a second RAM bank may therefore start at a value worked out when the program starts:

extern "rom_next" next() -> u32

@section(".ram2")
var buf: u32 = next()

@export("main")
main_entry() -> int = int(buf)

Placed in flash, the same storage would be written by main, so a table meant for ROM is one that folds.

What it may not mark

A const is folded into every use and has no storage; a type is not an object. Neither takes a section:

@section(".rodata")
const N: int = 4

print(N)
'@section("...")' places one object in a linker section, so it marks a 'var', a 'val' or a function

A local is refused for a different reason, and the message says which — its storage is the frame of whichever call is running, while a section is a region of the image, decided once at the link:

f() -> int
    @section(".noinit")
    var n: int = 1
    n

print(f())
'n' is a local, so it cannot be placed in section ".noinit"

A binding that names several things has no one object for a section to be about, which is the rule @align(n) above already follows:

@section(".data")
static var a, b = 1, 2

print(a, b)
'@section("...")' places one object, and a binding that names several has no one object for it to be about

@thread_local

@thread_local gives a module var one copy per thread rather than one for the whole program. Every thread reading the name reads its own storage, and &name answers the address of the copy belonging to the thread that took it.

The initializer has to be a value the compiler can write down — a literal, null, a const, the address of other module storage, or an array or struct built from those — or left off, which starts every thread at the type’s zero. That is the one rule the attribute adds, and it follows from what a per-thread copy is: the copies are made from a single image in the object file, because a thread started halfway through a run never passes through the prologue an ordinary module var is filled by. A call that has to run when the program does — one that prints, or reads a var — would fill exactly one copy and leave every other thread reading zeroes, so it is refused where it is written.

It marks a var and nothing else. A val and a const never change, so the one copy each already has is every thread’s.

import sysl.threads.*

@thread_local static var n: int = 0

count_to_five(state: *int)
    for i in 0..<5
        n = n + 1
    print(n)

var a = spawn(&count_to_five, null).unwrap()
a.join()

var b = spawn(&count_to_five, null).unwrap()
b.join()

print(n)
5
5
0

Each worker’s n starts at zero and counts up in storage no other thread can see; joining one before spawning the next is what makes the two 5s print in a fixed order rather than racing. Main’s own copy is never touched, so its print(n) reads the zero it started at — the third line is the proof that @thread_local gave three threads three different objects under one name.

It composes with @align(n) and @section("...")

All three are facts about where one object’s storage lives, so a thread-local can be aligned or placed exactly as an ordinary module var can:

@align(16)
@section(".tdata")
@thread_local var scratch: [4]u64

A &T into a thread-local needs no &sync

A reference into a @thread_local belongs to one thread by construction, so there is no second thread for its count to be torn by, and reaching it is not crossing a concurrency domain.

What it may not take as an initializer

next() -> int
    print("next")
    42

@thread_local static var n: int = next()

print(n)
'n' is '@thread_local', so its value has to be one the compiler can write down: every thread gets a copy of the initial value, and the copies are made from a single image in the object file rather than by code that runs per thread. A literal, 'null', a 'const', the address of other module storage and the arrays and structs built from them are all values; a call is not. Leave the value off to start every thread at the type's zero, and do the work in each thread instead

It may start at the address of other module storage

The address of an ordinary module var or val — or of a field of one, or of an element at a constant index — is a relocation the linker fills in once, so it is a value the image can carry, as C’s static _Thread_local Vm *vm = &main_vm; is. Every thread starts pointing at the same storage, and each can then point its own copy somewhere else without the others seeing it:

import sysl.threads.*

struct Vm
    id: int

static var main_vm: Vm = Vm(1)

@thread_local static var current: *Vm = &main_vm

worker(seen: *int)
    var mine = Vm(2)

    *seen = current.id
    current = &mine
    *seen = *seen * 10 + current.id
    current = &main_vm

var seen = 0

spawn(&worker, &seen).unwrap().join()

print(seen)
print(current.id)
12
1

The worker starts at main’s Vm, repoints its own copy at a Vm of its own, and reads 2 through it; main’s copy was never touched, so it still reads 1.

The address of another @thread_local is not a value, being a different address on every thread:

@thread_local static var count: int = 0

@thread_local static var counter: *int = &count

print(*counter)
'counter' is '@thread_local', so its value has to be one the compiler can write down, and the address of 'count' is not one: 'count' is '@thread_local' too, so its address is a different one on every thread, and no single image in the object file can hold it. The address of ordinary module storage is a value. Leave the value off to start every thread at the type's zero, and do the work in each thread instead

A target with no thread-local storage refuses it by name

A freestanding target has no loader and no libc, so nothing lays a thread’s storage down and nothing writes the thread pointer an offset would be read from:

@thread_local static var n: int = 0

print(n)
'n' cannot be '@thread_local' on 'thumbv7em-freestanding': there is no loader and no libc on this target, so nothing lays a thread's storage down and nothing writes the thread pointer the offset would be read from. Declare it as a plain module 'var' and let the port's scheduler answer for which task is running, which is what the ownership runtime's reaper slot does

What a bare target uses instead is exactly this: a plain module var the port’s scheduler answers for, the same shape the ownership runtime’s reaper slot already takes.

On a bare AArch64 machine the program may own the thread pointer

A kernel is the thing that writes the thread pointer, so on aarch64-freestanding the refusal above becomes a question: which register? The manifest answers it, and from then on @thread_local compiles to the local-exec model against that register:

codegen {
  thread_pointer = "tpidr_el1"
}

The register is any of tpidr_el0, tpidrro_el0, tpidr_el1, tpidr_el2 and tpidr_el3. Every function is compiled to read it (clang’s -mtp), and so is a package’s carried C, so a _Thread_local there and a @thread_local here find one block.

What the program then owes is what a loader would have done, and the library does most of it. The linker gathers every thread-local into a .tdata image (the initial values) and a .tbss (the zeros), and on AArch64 a variable lives at the thread pointer plus 16 plus its offset in that image, the 16 bytes being reserved for the thread’s own use. Every bare image’s link names that template — __sysl_tls_data, __sysl_tls_data_end, __sysl_tls_end and __sysl_tls_align — so sysl.threads.spawn lays each thread it starts out of it and hands the block’s address to the spawn hook (library/threads.md § On a target with no C library), and the program’s part is the register: a kernel writes it on every switch. The first thread is the one the library did not start, so the program lays its block itself, sysl.threads.tls_room() bytes set aside and tls_place answering what the register should hold:

var block: [256]u8 = [0; 256]            // at least tls_room()
val tp = tls_place(block[..]).unwrap()   // copies .tdata, zeroes .tbss
write_sysreg("tpidr_el0", u64(usize(tp)))

A thread that never gets a block reads whatever the register points at, as it would through any pointer the program forgot to set. A register the machine has no use for is refused before anything is built:

@thread_local static var n: int = 0

print(n)
cannot be '@thread_local' on 'aarch64-freestanding' until the program says which register holds the thread pointer

One file for every machine: #if thread_local

A library that runs both where there are threads and on a board — a C library’s errno is the usual case — wants one variable that is per-thread wherever the build has thread-local storage and a plain module var everywhere else. The #if symbol thread_local (§ #if) is true exactly where the attribute is accepted, so the attribute line is gated by it and the declaration is written once:

import sysl.threads.*

#if thread_local
@thread_local
#endif
static var errno: int = 0

fail(state: *int)
    errno = 22
    print(errno)

var t = spawn(&fail, null).unwrap()
t.join()

print(errno)
22
0

On this hosted machine errno is one per thread, so the worker’s 22 never reaches main. On a board, and on aarch64-freestanding with no codegen.thread_pointer, the same file builds with a plain module var the scheduler answers for; on aarch64-freestanding with the register named it is a thread-local again. thread_local is the one #if symbol the target alone does not settle: it also reads the root manifest’s codegen.thread_pointer, which is why the register is the root’s to name and why it is read before any condition is.

@main_thread — storage only the main thread reaches

A body that may run on another thread — a closure held as a &sync Fn, a named function read into one — may not reach module storage whose counts are not atomic, since the rest of the program could be touching the same counts at the same moment:

struct Counter
    n: int

static var counter: &Counter = Counter(0)

val bump: &sync Fn() -> unit = () -> counter.n += 1

bump()
a closure shared between two domains may be called from either, so every count it reaches has to be atomic — but the module storage 'counter' it reaches is a '&Counter'

Some programs know something the compiler cannot see: that a callable typed &sync because it may cross is in fact only ever called on the main thread. An interpreter’s builtins are the usual case — the table is shared with worker threads, but the builtins that touch the main machine run on the main thread alone. @main_thread above a module var or val writes that promise down, and the refusal above stops:

struct Counter
    n: int

@main_thread
static var counter: &Counter = Counter(0)

val bump: &sync Fn() -> unit = () -> counter.n += 1

bump()
bump()
print(counter.n)
2

The promise is checked where it can be, at run time. Each access to @main_thread storage from a body that may run in another domain first asks whether the thread making it is the one the process began on; where it is not, the program writes 'counter' is @main_thread storage and was reached from another thread to standard error and traps, as a failed bounds check does. The question is answered without anything recorded at start-up, so an archive a C program links answers it too. The program’s own code — the entry file, a function no &sync body reaches — is not checked, and reads the storage exactly as it reads any module var. The check is kept in every build, and is there only on a target with POSIX threads.

It marks one object every thread could otherwise reach, so it is refused above a local (already one call’s), above a const, an extern or a function (no storage), and beside @thread_local or @per_cpu (two answers to who reaches the binding):

@main_thread
@thread_local
static var n: int = 0
'@main_thread' and '@thread_local' are two answers to who reaches one binding — the main thread alone, or each thread its own copy — so they cannot stand above the same one. '@thread_local' already keeps every copy to one thread; '@main_thread' is for the one object the whole program shares

A freestanding target has no threads for one to be the main one:

@main_thread
static var n: int = 0

print(n)
'n' cannot be '@main_thread' on 'thumbv7em-freestanding': the promise is that only the program's main thread reaches it, and a freestanding target has no threads for one to be the main one

@per_cpu — one copy per processor core

@per_cpu gives a module var one copy per core rather than per thread: the record a scheduler keeps for each processor, the core’s run queue, its tick count. It exists only on aarch64-freestanding, where the program owns a register to find the copy through, and the manifest names that register:

codegen {
  per_cpu_offset = "tpidr_el1"
}

Every @per_cpu binding is laid down in one section, .percpu, and that section is the image each core’s copy is made from. The register holds this core’s copy’s address minus the image’s, so an access is the binding’s address plus the register — read afresh at every access — and a core whose register is zero is using the image itself. A boot core can therefore use its per-core data before it has made any copies, provided it zeroes the register first. For the others the program copies the image once per core and writes each core’s offset:

.percpu : { __percpu_start = .; *(.percpu) __percpu_end = .; }
@per_cpu
var ticks: u64 = 0

@pinned
tick() -> u64
    ticks += 1
    ticks

Its initializer has to be one the compiler can write down, as a @thread_local‘s does, and it stands beside @align(n) and nothing else: the section is the storage class’s own.

A per-core read is right only where the core cannot change

Which copy ticks names depends on the core running, so the read is right only while the thread cannot move: a thread preempted between finding its core’s copy and using it resumes on another core holding the first core’s. Nothing in a function’s text says whether interrupts are masked, so the function says it, in one of two words:

  • @pinned — every caller already keeps this function on one core: it runs in an interrupt handler, or under a lock that masks interrupts. That is a precondition, so a call of a @pinned function is held to the same rule as a per-core read, and so is taking its address.
  • @pins — the body keeps itself on one core before it gets there (it masks interrupts first, and unmasks them after). Its callers are asked nothing.

A @per_cpu binding may be named, and a @pinned function called, only inside a body marked one or the other. The words are the program’s claim, as an @export‘s symbol is — the compiler cannot see a register being written — but they put the claim at the function, where a reader can check it against the code, and they carry it up the call tree to the place that keeps the core still.

@pinned(thread) is the same claim one level up, and asks nothing of callers: the fiber the body runs on is only ever resumed on the thread it began on, so it may hold a count that is not atomic across a call that can reach sysl.fiber.switch (fiber § A count held across a switch is refused). @pinned takes no other argument:

@pinned(core)
depth() -> int = 3
'@pinned' takes no argument but 'thread' — '@pinned' alone says every caller keeps the function on one core, and '@pinned(thread)' that the fiber it runs on is only ever resumed on the thread it began on

The rule is not about storage, so it runs anywhere a @pinned function is written:

@pinned
depth() -> int = 3

@pins
probe() -> int
    // The real thing masks interrupts here and restores them after.
    depth()

print(probe())
3

A closure is a body of its own and carries neither word, since it can be called after the function that wrote it has let the core go; nor do the entry file’s statements.

@pinned
depth() -> int = 3

print(depth())
'depth' is '@pinned', so every call of it has to be made where the core cannot change, and the program's statements are neither '@pinned' nor '@pins'

@pinned says the callers hold the core still and @pins that the body does, so one function is one or the other:

@pinned
@pins
depth() -> int = 3
'@pinned' says every caller already keeps this function on one core, and '@pins' says the function does that itself before it reaches per-core storage — one body is one or the other

What it refuses

Anywhere but a bare AArch64 machine, the operating system moves a thread between cores whenever it likes, so no answer to “which core” stays true long enough to read through:

@per_cpu
static var ticks: u64 = 0

print(ticks)
cannot be '@per_cpu' on

On aarch64-freestanding, until the manifest names the register:

@per_cpu
static var ticks: u64 = 0

@pins
tick() -> u64 = ticks

print(tick())
cannot be '@per_cpu' until the program says which register holds this core's offset

A copy per thread and a copy per core are two answers to one question:

@per_cpu
@thread_local
static var ticks: u64 = 0
'@thread_local' and '@per_cpu' are two answers to how many copies one 'var' has

A section would move the binding out of the image the copies are taken from:

@per_cpu
@section(".data")
static var ticks: u64 = 0
'@per_cpu' places the binding in '.percpu', the image every core's copy is made from

A val never changes, so the one copy it has is already every core’s:

@per_cpu
static val ticks: u64 = 0
a per-core constant is a constant

A local is in one call’s frame, which no other core can see:

count() -> u64
    @per_cpu
    var n: u64 = 0
    n

print(count())
'n' is a local, and a local is already one call's

@domain(interrupt) — a function entered on top of whatever was running

An interrupt handler is a function the processor enters directly, in the frame it pushed. Most kernels do not write one: a vector table in assembly saves the interrupted registers itself and then calls an ordinary function, exactly as C would. That function is @exported, and an export is how C enters sysl at all — almost always on the program’s one thread — so nothing about it says a second domain exists.

@domain(interrupt) says so. It changes nothing about how the function is called; it makes the function a root of the interrupt domain, held to what an interrupt handler is held to: every count it reaches, however many calls down, has to be atomic (memory § Module storage reached from another domain).

import sysl.sync.Atomic

var done: Atomic[u32] = Atomic(0)

@domain(interrupt)
@export("disk_irq")
disk_irq()
    done.store(1)

@export("disk_wait")
disk_wait()
    while done.load() == 0 do ()

The handler and the program share one word, and every touch of it is one atomic instruction. A handler that reaches a counted reference instead is refused at the handler, naming what it reached:

struct Request
    sector: u64

var pending: Option[&Request] = None

@domain(interrupt)
@export("disk_irq")
disk_irq()
    pending = None
'disk_irq' is entered in the 'interrupt' domain, on top of whatever was running, so every count it reaches has to be atomic — but the module storage 'pending' it reaches holds a '&Request' in its 'Some(value)', whose count is not. Hold it as a '&sync Request' ('06')

A waker is the case a kernel meets first, and it has no atomic spelling at all — see async § A task that parks stays in its domain.

It names one domain, and interrupt is the one there is

@domain(thread)
worker() = ()
'thread' is not a domain a function is entered in — 'interrupt' is the one there is

A function the program calls itself is in the program’s own domain and says so by not writing the annotation, so there is no empty form, and a function the processor may enter in either of two is entered in the stricter, so there is no list.

It is not async

Whatever enters the function calls it directly, and calling an async function only makes its task:

@domain(interrupt)
async on_irq() = ()
a function entered in the 'interrupt' domain cannot be 'async': whatever enters it calls it directly, and calling an async function only makes its task, which nothing there can drive

#if — gating lines before the lexer

Everything a target decides is a fact the compiler reads about the machine. #if is the one place a program reads one, and it exists because machines genuinely differ in ways a library cannot paper over: a syscall number, a struct a header lays out two ways, a symbol one libc exports and the other does not.

#if posix
line_ending() -> string = "\n"
#else
line_ending() -> string = "\r\n"
#endif

print("a posix machine", line_ending().len)
a posix machine 1

What differs between the branches is the implementation, not the answer. That is the shape most uses of #if have: a syscall number, a struct a header lays out two ways, a symbol one libc exports and the other does not — chosen per machine so that everything above the choice can stop caring.

#elif chains, and #else catches what nothing named:

#if aarch64
machine() -> string = "aarch64"
#elif x86_64
machine() -> string = "x86_64"
#elif riscv64
machine() -> string = "riscv64"
#else
machine() -> string = "something else"
#endif

That one is quoted rather than run, because what it prints is supposed to depend on the machine — which is the whole point of the construct and exactly what a page cannot pin to one answer.

#if / #elif / #else / #endif, nesting freely, and the branches are exclusive — the first whose condition holds is the one that contributes, and a group inside a branch that was not taken contributes nothing however its own condition reads.

It gates lines, and it gates them before anything is parsed

A line in a branch this build is not for is replaced by an empty line, not removed, and so is every directive line. After the pass the file is an ordinary sysl file that happens to have some blank lines in it, and nothing downstream knows any of this happened.

Replaced rather than removed, because every line below a gate has to keep the number it was written at. Deleting them would leave the messages right and the carets somewhere else, with nothing to say so:

#if linux
say() -> string = "one"
extra() -> int = 1
another() -> int = 2
#endif

print(missing_name)
:7:7

The caret is on line 7, which is where print(missing_name) is written — not line 2, where it would be if the four gated lines had been dropped.

A directive sits at the margin, column 1. That is a rule and not a convention. sysl is indentation-sensitive, and indentation is how the language reads block structure — so a gate written in that channel would look like it takes part in a nesting it has nothing to do with, when in fact the line is gone before anything counts a column. At the margin it is visibly not part of the code’s shape, which is what it is. It is also how C is written. What keeps a declaration’s @test from ever being mistaken for one of these is the sigil, not the margin — an annotation on a module header sits at column 1 too.

Why lines and not a construct wrapping declarations. Rust spells this #[cfg], an attribute on an item, and can because Rust is brace-delimited: the attribute attaches without moving anything. Here the equivalent would have to take an indented block, so adding or removing a platform gate would reindent everything inside it — a one-line intent showing up as a whole-body diff. A flat marker disturbs nothing.

The symbols are derived from the target, and the set is closed

kindsymbols
operating systemmacos, linux, windows, freestanding, android, wasi
processoraarch64, x86_64, riscv64, riscv32, thumb, x86, wasm32, craft
derivedhosted (not freestanding), posix (macos, linux or android), bsd (macos)
floating pointfpu (the machine has floating-point registers), fpu_dp (they hold a double), hardfloat / softfp (the float ABI), fma (a fused multiply-add is one instruction at f32), fma_dp (and at f64)
instructionscrc32c (the CRC-32C instructions are there: AArch64’s crc32c*, SSE 4.2’s crc32)
storagethread_local (a @thread_local is accepted on this build)
address widthptr16 (an address is 16 bits), ptr32 (it is 32 bits), ptr64 (it is 64 bits)
instrumentationsanitize_address (the build is instrumented for AddressSanitizer), sanitize_thread (for ThreadSanitizer)
build modedebug (a debug build: plain integer overflow is checked)

That is the whole vocabulary. There is no #define and nothing a project can add, and only two symbols depend on a project config: thread_local, which also reads the root manifest’s codegen.thread_pointer, and crc32c, which also reads codegen.extensions and --cpu (below). The two instrumentation symbols are facts of the build rather than of the machine, and read SYSL_EXTRA_CFLAGS (below). A condition is a symbol, !, &&, ||, and parentheses; && binds tighter than ||.

posix is a name for the commonest disjunction rather than a replacement for writing it — #if linux || macos still says the same thing, and since android joined it is the shorter way to say all three. android is its own symbol and is not linux, though there is a Linux kernel under it: what a source file gating on a system is really asking about is the libc and the libraries, and Bionic is neither glibc’s set of -l names nor its headers. Code that wants both writes #if linux || android. Note that this posix is not the capability of the same name: this one asks is this a POSIX system, a fact about the machine settled by the target, where the capability asks may this module use POSIX, a permission a project grants and a no posix clause takes away. They agree today only because nothing denies anything yet.

bsd is narrower than posix and is about the libc rather than the machine. A BSD and a glibc system are both POSIX, and yet they disagree about a handful of things a source file has to get right: errno is reached through __error on one and __errno_location on the other, ENOTEMPTY is 66 against 39, ENAMETOOLONG is 63 against 36, and mode_t is sixteen bits against thirty-two. Every one of those is a libc fact with nothing about any particular system in it, which is why the standard library’s own gates for them say bsd and not macos.

fma and fma_dp are C’s FP_FAST_FMAF and FP_FAST_FMA: whether llvm.fma (reference/ffi.md) is a single instruction at that width, so that a fused multiply-add is no slower than a multiply and an add. They are facts about the unit, not the calling convention: AArch64 and a hosted RISC-V machine have both, every Arm unit sysl targets has fma, and only the Cortex-M7’s double-precision one (thumbv7em-freestanding-dp) has fma_dp as well. The x86 rows ask for no processor past the baseline, which has no FMA, and WebAssembly has none. Where a symbol is false the operation is still exact — it is a call rather than an instruction. The same two symbols say where a written a * b + c is contracted into one rounding (expressions).

thread_local is true exactly where a @thread_local would be accepted (§ @thread_local): on every hosted machine, and on bare AArch64 when the root manifest says which register holds the thread pointer (codegen { thread_pointer = "tpidr_el0" }). It is false on a board and on bare AArch64 with no such setting. It is the one symbol the target alone does not settle, because the register is the program’s to name; the compiler reads it before the first condition, a dependency’s if included.

crc32c is true exactly where the build promises the CRC-32C instructions everywhere, so a call of an extern naming one needs neither an @extensions caller nor an @unchecked("crc") block (ffi). The instructions are optional on both machines that have them: AArch64 makes them mandatory only from Armv8.1, and SSE 4.2 is past the x86-64 baseline the x86 rows ask for. So the symbol is true on aarch64-macos, whose every processor has them, and elsewhere only where the build promises them: codegen { extensions = ["crc"] } on an AArch64 row, ["sse4.2"] on an x86-64 one, or --cpu native on a machine that has them. A processor named with --cpu does not count by itself, since the compiler does not know its features — name the extension beside it. Under general_regs_only an x86-64 build has no SSE, and so no crc32c. sysl.checksum‘s crc32c reads this symbol to choose the instruction over its table.

That distinction is worth making before it is needed rather than after. macOS is the only BSD sysl targets today, so #if macos and #if bsd select identically — and a second BSD would route into the glibc branch at every one of those sites, where exactly one of them says anything: the errno accessor fails at the link, while a wrong ENOTEMPTY merely stops matching and a mode_t at the wrong width appears to work. A symbol that means what it says costs nothing today and is the whole of the fix later.

The floating-point four are facts of the whole target, not of the processor. Seven Arm targets are all thumb, and they differ in exactly this: a Cortex-M0+ has no floating-point registers, a Cortex-M4F has single-precision ones, a Cortex-M7 double-precision ones, and a core with a unit may still be built to pass a float in an integer register. Code that saves those registers — a setjmp, a context switch — has to know, because an instruction naming s16 faults on a core with none:

targetfpufpu_dphardfloatsoftfp
every hosted target, aarch64-freestanding, x86_64-freestanding, both wasm32yesyesyesno
thumb-freestanding, thumbv7em-freestandingyesnoyesno
thumb-freestanding-softfpyesnonoyes
thumbv7em-freestanding-dpyesyesyesno
thumbv6m-, thumbv7m-, thumb-freestanding-soft, thumbv7em-freestanding-soft, both RISC-V freestanding, craft-freestandingnononono

fpu asks about the registers, whichever ABI passes a value, so a soft-float-ABI build of a core that has them still answers yes — the registers are there and a context switch must save them. A machine with registers is exactly one of hardfloat and softfp; one without is neither, and !fpu is how a condition says soft float throughout. Every hosted machine answers yes, which is what this host says:

#if fpu && fpu_dp && hardfloat
print("floating-point registers, doubles in them, and floats passed in them")
#else
print("something else")
#endif
floating-point registers, doubles in them, and floats passed in them

Inside an asm statement the arm is still chosen by processor, and what differs between the cores of one processor is gated by #if inside its arm — a setjmp saving the callee-saved single registers only where there are any:

save_fp(at: usize)
    asm
        [thumb]
#if fpu
            "vstmia {at}, {{s16-s31}}"
            in at : reg
#endif
        [x86_64, aarch64, riscv64, riscv32, craft, wasm32] unavailable "not this example"

ptr16, ptr32 and ptr64 say how wide an address is — the width of usize and isize — and every target answers exactly one. ptr16 is true on craft, ptr32 on thumb, riscv32, wasm32 and x86, and ptr64 on aarch64, x86_64 and riscv64. They are for a type defined by its relation to the machine’s word rather than by one processor: a Forth double cell is twice the width of a cell, so it is an i64 where a cell is 32 bits and an i128 where it is 64, and one condition says so for every machine at once. This host’s addresses are 64 bits:

#if ptr32
type DCell = i64
#else
type DCell = i128
#endif

@assert(sizeof(DCell) == 2 * sizeof(isize), "a double cell is two cells")

print(sizeof(DCell))
16

On a 32-bit board the same lines choose the narrower type, and an @assert the build checks says so:

#if ptr32
type DCell = i64
#else
type DCell = i128
#endif

@assert(sizeof(DCell) == 8, "a double cell on a 32-bit board is eight bytes")

@export("dcell_mul")
dcell_mul(a: isize, b: isize) -> DCell = DCell(a) * DCell(b)

and on a 64-bit one the wider:

#if ptr32
type DCell = i64
#else
type DCell = i128
#endif

@assert(sizeof(DCell) == 16, "a double cell on a 64-bit machine is sixteen bytes")

@export("dcell_mul")
dcell_mul(a: isize, b: isize) -> DCell = DCell(a) * DCell(b)

Like fma, the three are facts about the machine, so a directory cannot select on them.

sanitize_address and sanitize_thread say the build is instrumented for AddressSanitizer or ThreadSanitizer. They are read from the same list the compiler marks every function with when SYSL_EXTRA_CFLAGS carries -fsanitize=address or -fsanitize=thread (one flag may name several, -fsanitize=address,undefined; -fno-sanitize= asks for nothing), so a condition and the instrumentation cannot disagree: on any target, the symbol is true exactly when the code under it is instrumented. They are for the code that has to behave differently under a sanitizer — a test that measures the allocator, which ASan replaces, or a lock-free path TSan cannot see the ordering of:

#if sanitize_address
say() -> string = "instrumented"
#else
say() -> string = "plain"
#endif

print(say())
plain

#if !sanitize_address and #elif sanitize_thread read as every other symbol does. Only those two are answered: asan, tsan and sanitize_memory are refused, naming them:

#if asan
say() -> string = "instrumented"
#endif
'asan' is not something a build says about itself — sysl answers sanitize_address (AddressSanitizer) and sanitize_thread (ThreadSanitizer)

A dependency’s if cannot name either, because which packages a resolution takes may not depend on how one build of them is compiled. Every cache the build keeps is keyed on SYSL_EXTRA_CFLAGS, so an instrumented build never reuses what a plain one gated.

debug says the build is a debug build — the build mode that checks plain integer overflow (--debug), and false in a release build (--release). It is read from that mode itself, so it is true under sysl run and sysl test with no flag, false under sysl build, build-c and build-lib with none, and --debug or --release settles it for any of them. A library can compile its own checks out of a release build:

#if debug
mode() -> string = "debug"
#else
mode() -> string = "release"
#endif

print(mode())
debug

The page’s blocks are built in debug, which is what is printed; under --release the same program prints release. #if !debug reads as every other symbol does. Code is gated in the mode of the build that compiles it: the standard library’s artifact is built once per mode, so its #if debug follows the program’s, while a .syslib made by build-lib keeps the mode it was built in, as its integer arithmetic does (the mode is recorded in its metadata). A dependency’s if cannot name debug, for the reason it cannot name the sanitizers, and a misspelling such as debgu is refused like any unknown symbol.

A symbol nobody knows is an error, not false:

#if darwin
say() -> string = "one"
#else
say() -> string = "two"
#endif

print(say())
'darwin' is not something a target says about itself — sysl knows aarch64, android, bsd, craft, crc32c, debug, fma, fma_dp, fpu, fpu_dp, freestanding, hardfloat, hosted, linux, macos, posix, ptr16, ptr32, ptr64, riscv32, riscv64, sanitize_address, sanitize_thread, softfp, thread_local, thumb, wasi, wasm32, windows, x86, x86_64

The set is closed, so a name outside it is a mistake rather than a fact this build happens not to have — and a misspelling that read as false would gate code out of the build with nothing said. Silently missing code is the one failure this feature cannot be allowed to have, and it is the one C has.

A target’s name is not a symbol — it has a - in it, which no identifier carries — and writing one is told what to write instead, because otherwise the reader is told that - is not an operator, which is true and no help:

#if aarch64-macos
say() -> string = "one"
#else
say() -> string = "two"
#endif

print(say())
'aarch64-macos' is a target's name rather than something a condition asks about — a condition asks about one fact of the machine at a time, so this is written 'aarch64 && macos'

Every condition is checked, in the branch being taken and the ones being skipped alike. So a misspelling in the Linux half is caught by a macOS build, which is where it would otherwise sit until somebody built for Linux:

#if linux
say() -> string = "one"
#elif nosuchmachine
say() -> string = "two"
#else
say() -> string = "three"
#endif

print(say())
'nosuchmachine' is not something a target says about itself

That refusal comes from a build whose #if linux was already false and whose #else is the branch being taken. The condition on the way past was still read.

What is given up, and what is not

The inactive branch is never syntax-checked. That is the price of gating text rather than trees, it is C’s price too, and a Linux branch can therefore rot while the macOS build stays green. What finds that is a build for each target — a thing to run, not a thing to design around. The conditions themselves are the part that is checked everywhere, and they are the part where a mistake would otherwise be silent.

Inside a text block, a condition sysl can read gates the block’s lines, and every other margin line is the block’s text. A text block carrying C — a header to compile, a template — has its #define, #if and #include at column 1, and they belong to the string; a text block a program assembles may also want some of its lines only on some machines, or only with a feature on. The two are told apart by the condition: an #if or #elif whose condition reads as sysl’s, over the symbols a condition may name here — the target’s and the package’s feature_ ones — gates exactly as it does in live code, down to its own #else and #endif. Anything else is text: #define, #include, #ifdef, #pragma and every other word, an #if whose condition is not sysl’s (#if LIMIT > 2, #if defined(X), #if NDEBUG), and that #if‘s own #elif, #else and #endif, so a C conditional stays whole with a sysl group around it or inside it.

import sysl.text.Search

val header = raw"""
    #include <stddef.h>
#if posix
    #include <unistd.h>
#else
    #include <windows.h>
#endif
    """

print(header.contains("<unistd.h>"), header.contains("<windows.h>"), header.starts_with("#include"))
true false true

The directive lines go as they do everywhere, leaving blank lines, and the block’s incidental indentation is reckoned from what is left — the gated text, not the text as written. A condition over a C macro is the block’s text whatever else is around it:

import sysl.text.Search

val header = raw"""
#define LIMIT 4
#if LIMIT > 2
#include <stdio.h>
#endif
"""

#if posix
val kind = "posix"
#else
val kind = "other"
#endif

print(kind, header.contains("#if LIMIT > 2\n#include <stdio.h>\n#endif"))
posix true

#if posix in a block of C gates, since posix is a symbol sysl knows; a C header that means its own macro of that name writes it as #ifdef posix, which is text. A feature_ name the manifest does not declare is false, as it is in live code, so a misspelled one gates its lines out rather than passing into the text. The same holds for option_<name>, the symbol of a bool option of the package that is true (a feature’s option is option_<feature>_<name>): in a package’s files #if option_trace gates exactly as #if feature_trace does. A group opened inside a text block closes inside it: one whose #endif is below the block’s closing delimiter is refused at its #if, naming the line the block ends on.

val s = raw"""
#if posix
    on a posix machine
    """
#endif
this '#if' opens a group inside the text block that ends on line 4, and the group has to close inside the block — add its '#endif' before line 4

A block comment is all text. Nothing reads a comment’s lines, so there is nothing for a condition in one to gate, and a C fragment quoted in a comment stays as written. The gate asks the lexer itself where a literal opened in live code ends, and which kind it is, with the same scans a token is read with, so there is one set of rules about strings and comments rather than two that could drift. A branch not taken is not read this way, since it need not lex at all — a text block that opens inside one does not hide the #endif that closes it.

/*
#if windows
The quoted condition is the comment's, and so is the '#endif' below it.
#endif
*/
print("a comment's lines are its own")
a comment's lines are its own

The library is subject to it too

library/sysl is sysl source, so it may gate on the machine like any other — which makes “the standard module” a question with a target in it, and the library’s source is parsed once per target accordingly.

The one thing held fixed is that a name the compiler spells for itself is declared on every target. A library that gated Option away for Windows would be a library nothing compiles against there, so that is refused in a registry-wide check rather than at the first ? somebody writes.

And an artifact records the target it was built for and is refused by a build for another, because the trees a library ships are now a per-target answer.

What is deliberately absent

absentwhy
a general annotation mechanismthe set is closed: @test, @tailrec, @pure, @ghost, @export, @reads(...), @writes(...), @crossing(...), @noinline, @inline, @cold, @disjoint(...), @setup, @teardown, @setup_all and @teardown_all on a free function, @packed, @align(n) and @export("...") on a struct, @export("...") on a module binding, @section("...") on a binding or a function, @section("...") and @align(n) on an asm block at the top of a file, @no_<capability>, @requires, @link, @include and @tests on a file, and @assert on nothing at all. Each was designed and added on its own evidence; there is no way to write one the compiler does not already know
@naked, a function with no framecode that must run before there is a stack — a reset entry, a vector table — is an asm block at the top of a file, which is not a function and so has no frame to leave out
bitfield syntaxthere is nothing to write: inside @packed an iN field already occupies exactly N bits, so a five-bit register field is u5 and needs no : 5 beside it. The open integer family does the work C’s declarator syntax was invented for
#define, or any project-supplied symbolthe #if vocabulary is derived from the target and closed, which is what makes an unknown symbol an error rather than a false
a #if that asks about a capabilitya condition asks what the target says; what a project permits is a different question, left with the config that would define it
a test framework in the librarya test asserts in the language it is testing, and passes by returning
a release mode that drops a checksee errors and contracts — every check is in every build

That is the language. What ships beside it is the standard library, which is a section of its own — nothing in it is a language feature, and every type in it is one a program could have written.

Search

Esc
↑↓ to navigate ↵ to open Esc to close