Files
aji/docs/reference/air-text.ebnf
T
dev 1eaf968f4f cli: add explicit built-in error values
Add the error_values feature and error.const through specification, AIR,
verification, interpretation and source lowering. Error values are Copy data
with no allocation, effects or authority. Keep enum.make semantics frozen.

Cover all registered tags, feature negotiation, payload/type errors and
authority rejection in AIR/source; verify canonical and hosted CLI behavior.
Record reference-bundle costs and the map preflight-validation prerequisite.
2026-10-07 08:09:49 +03:00

259 lines
14 KiB
EBNF

(* Aji text AIR, format version 0.
Normative together with docs/spec/07-air.md §8 and docs/spec/07a-air-instructions.md.
Lexical conventions
-------------------
- A comment starts with ";" and runs to the end of the line.
- Line breaks are significant: each header line, declaration header,
block label and instruction ends with NL. Line breaks inside "(" ")",
"[" "]" and "{" "}" are ignored.
- Spaces and tabs separate tokens and are otherwise insignificant.
- Opcodes are written as one dotted word, e.g. "add.checked.u64".
- Every construct marked "(* feature f *)" requires f in the module's
"features" header (E0006 otherwise); the feature table and dependency
closure are in docs/reference/air-features.md.
Name spaces
-----------
- "%name" SSA value, scoped to its function.
- "@name" function, type or block label. Functions and types share the
module scope; block labels are scoped to their function. A label
reference appears only in branch targets, so it never clashes
with a function name.
- bare field names, variant names, module path segments, effect names,
capability kinds, intrinsic names, edge roles.
*)
module = { NL } header { NL } { declaration { NL } } ;
header = "module" path NL
"air" int_lit NL (* AIR text format version; this file defines 0 *)
"lang" version_lit NL (* Aji semantic version, e.g. 0.1 *)
"features" "[" [ path_list ] "]" NL ; (* required features and their full dependency closure *)
declaration = type_decl | function ;
type_decl = "type" global "=" ( struct_type | enum_type ) NL ;
struct_type = "struct" "{" [ field { "," field } [ "," ] ] "}" ;
field = ident ":" type ;
enum_type = "enum" "{" variant { "," variant } [ "," ] "}" ;
variant = ident [ "(" [ type { "," type } ] ")" ] ;
(* Linear types as fields, payloads or elements need features
that are reserved in format 0; see air-features.md. *)
function = "fn" global "(" [ param_list ] ")" "->" type [ "from" local ] NL (* from: feature ownership *)
"effects" "[" [ path_list ] "]" NL
[ "trusted" string NL ] (* feature unsafe *)
block { block } ;
param_list = param { "," param } ;
param = local ":" type ;
block = ident [ "(" [ param_list ] ")" ] ":" NL { instruction NL } ;
(* The first block is the entry block and takes no parameters. *)
instruction = [ local { "," local } "=" ] operation ;
(* More than one result only for instructions specified with several results
(slice.split; atomic.cmpxchg with feature atomics). *)
operation = "const." int_type int_lit
| "const.bool" ( "true" | "false" )
| "error.const" ( "alloc_error" | "cap_error" | "io_error" ) "." ident (* feature error_values; no payload *)
| "copy" local
| arith_op local "," local
| "assert" local
| "cast." cast_mode "." int_type local
| "struct.make" global "(" [ local_list ] ")"
| "struct.get" global "." ident "," local
| "enum.make" global "." ident "(" [ local_list ] ")"
| "call" global "(" [ local_list ] ")"
| linear_aggregate_op
| ownership_op
| arrays_op
| capability_op
| heap_op
| region_op
| task_op
| cancellation_op
| channel_op
| sync_op
| graph_op
| atomic_op
| terminator ;
(* Several instructions are terminators with named outgoing edges (spec 07a).
Each edge names a block of the same function; the instruction's results
arrive as that block's parameters, so an edge carries no arguments. *)
edge = role "->" global ;
role = "ok" | "err" | "closed" | "cancelled" | "pending" ;
ok_err = "," "ok" "->" global "," "err" "->" global ;
ok_cancelled = "," "ok" "->" global "," "cancelled" "->" global ;
(* Feature ownership (spec 07a). *)
ownership_op = "local.alloc" type
| "local.store" local "," local
| "local.zero" local
| "local.load" local
| "borrow.ref" local
| "borrow.mut" local
| "borrow.field" global "." ident "," local
| "borrow.end" local
| "load" local
| "store" local "," local ;
(* Feature arrays (spec 07a). *)
arrays_op = "array.splat" type local
| ( "borrow.index" | "borrow.index.proven" ) local "," local
| "slice.make" local
| "slice.len" local
| "slice.sub" local "," local "," local
| "slice.split" local "," local
| "slice.copy" local "," local
| "guard" local
| "assume" local (* feature unsafe *)
| "borrow.index.unchecked" local "," local ; (* feature unsafe *)
(* Feature capabilities (spec 05 §10–§12, reference/intrinsics.md). *)
capability_op = "cap.narrow." ident local { "," local } ok_err (* terminator; form registered for the kind *)
| "intrinsic" path "(" [ local_list ] ")" [ ok_err ] ; (* fallible intrinsics are terminators;
resource intrinsics need feature resources *)
(* Feature heap (spec 03 §18–§19). *)
heap_op = "own.new" local "," local ok_err (* terminator: value, cap<mem.alloc> *)
| "buffer.new" type local "," local ok_err
| ( "buffer.len" | "buffer.capacity" | "buffer.view" ) local
| "buffer.reserve" local "," local "," local ok_err
| "buffer.push" local "," local ok_err
| ( "buffer.pop" | "buffer.free" ) local ok_err
| "own.array" local "," local "," local ok_err (* terminator: count, element, cap<mem.alloc> *)
| "own.take" local
| "own.free" local ;
(* Feature regions (spec 03 §20). *)
region_op = "region.create" local ok_err (* terminator: cap<mem.alloc> *)
| "region.new" local "," local ok_err (* terminator: region borrow, value *)
| "region.array" local "," local "," local ok_err (* terminator: region borrow, count, element *)
| "region.reset" local
| "region.destroy" local ;
(* Feature tasks (spec 04 §33–§36). *)
task_op = "task.scope.begin"
| "task.spawn" local "," global "(" [ local_list ] ")"
| "task.join" local
| "task.cancel" local
| "task.cancelled"
| "task.scope.end" local ;
(* Feature cancellation (spec 04 §37). *)
cancellation_op = "task.check_cancel" ok_cancelled (* terminator *)
| "task.join.cancel" local ok_cancelled (* terminator *)
| "task.shield.begin"
| "task.shield.end" local ;
(* Feature channels (spec 04 §38–§39). *)
channel_op = "channel.make" type "," local "," local ok_err (* terminator: element type, capacity, cap<mem.alloc> *)
| "channel.sender.dup" local
| "channel.send" local "," local "," "ok" "->" global "," "closed" "->" global "," "cancelled" "->" global
| "channel.recv" local "," "ok" "->" global "," "closed" "->" global "," "cancelled" "->" global
| "channel.close_sender" local
| "channel.close_receiver" local "," "ok" "->" global "," "pending" "->" global
| "select" select_arm { select_arm } "cancelled" "->" global ; (* terminator *)
select_arm = "[" ( "recv" local | "send" local "," local )
"," "ok" "->" global "," "closed" "->" global "]" "," ;
(* send, recv and close_receiver are terminators *)
(* Feature sync (spec 04 §40–§41). *)
sync_op = "mutex.make" local
| "mutex.take" local
| "mutex.lock" local ok_cancelled (* terminator *)
| "mutex.borrow" local
| "mutex.unlock" local ;
(* Features islands and arc (spec 03 §22–§24). *)
graph_op = "island.create" local "," local ok_err (* terminator: root value, cap<mem.alloc> *)
| "island.alloc" local "," local ok_err (* terminator: mut<island<T>>, value *)
| ( "island.root" | "frozen.root" | "arc.root" ) local
| ( "island.borrow" | "frozen.borrow" | "arc.borrow" ) local "," local (* owner borrow, iref *)
| "island.freeze" local
| "island.destroy" local
| "frozen.release" local
| "iref.eq" local "," local
| "arc.new" local "," local ok_err (* feature arc; terminator: root value, cap<mem.alloc> *)
| ( "arc.from_frozen" | "arc.clone" | "arc.release" ) local ; (* feature arc *)
(* Feature atomics (spec 04 §20–§30, §42). Each opcode is one dotted word; the
allowed orderings per operation are spec 04 §24 (E4201 otherwise). *)
atomic_op = "atomic.new" local
| "atomic.take" local
| "atomic.load." ordering local
| "atomic.store." ordering local "," local
| "atomic." rmw_op "." ordering local "," local
| "atomic.cmpxchg." ordering "." ordering local "," local "," local (* two results: old, exchanged *)
| "atomic.fence." ordering ;
rmw_op = "xchg" | "add.wrap" | "sub.wrap" | "and" | "or" | "xor" ;
ordering = "relaxed" | "acquire" | "release" | "acq_rel" | "seq_cst" ;
arith_op = ( "add" | "sub" | "mul" ) "." ( "checked" | "wrap" ) "." int_type
| ( "div" | "rem" ) ".checked." int_type
| ( "and" | "or" | "xor" ) "." ( int_type | "bool" )
| ( "shl" | "shr" ) "." int_type
| ( "eq" | "ne" ) "." ( int_type | "bool" )
| ( "lt" | "le" | "gt" | "ge" ) "." int_type ;
cast_mode = "checked" | "wrap" | "widen" ;
terminator = "br" target
| "cbr" local "," target "," target
| "switch." int_type local "," { int_lit "->" target "," } "default" "->" target
| "enum.switch" ( global | builtin_type ) local "," enum_arm { "," enum_arm } [ "," "default" "->" target ]
| "ret" [ local ]
| "trap" ;
enum_arm = ident "->" global ; (* target block receives the variant payload as parameters *)
target = global "(" [ local_list ] ")" ;
type = int_type | "bool" | "void" | "never" | global
| ( "local" | "ref" | "mut" ) "<" type ">" (* feature ownership *)
| "array" "<" type "," int_lit ">" (* feature arrays; ";" starts a comment, so source syntax [T; N] is spelled array<T, N> *)
| ( "slice" | "slice_mut" ) "<" type ">" (* feature arrays *)
| "cap" "<" path ">" (* feature capabilities; a capability kind, e.g. cap<fs.read> *)
| "file" "<" ( "read" | "write" | "append" ) ">" (* feature resources *)
| "listener" | "stream" (* feature resources *)
| "buffer" "<" type ">" (* feature linear_buffers *)
| ( "own" | "own_array" ) "<" type ">" (* feature heap *)
| "region" (* feature regions *)
| "scope" | "task" "<" type ">" (* feature tasks *)
| "shield" (* feature cancellation *)
| ( "sender" | "receiver" ) "<" type ">" (* feature channels *)
| ( "mutex" | "guard" ) "<" type ">" (* feature sync *)
| ( "island" | "frozen" | "iref" ) "<" type ">" (* feature islands *)
| "arc" "<" type ">" (* feature arc *)
| "atomic" "<" type ">" (* feature atomics; bool or an integer type up to 64 bits *)
| builtin_type ;
builtin_type = "cap_error" (* feature capabilities *)
| "alloc_error" (* feature heap *)
| "io_error" ; (* feature resources *)
int_type = "u8" | "u16" | "u32" | "u64" | "u128" | "usize"
| "i8" | "i16" | "i32" | "i64" | "i128" | "isize" ;
(* "f32" and "f64" are reserved; see spec 07 §8.4. "byte" is not an AIR type: frontends lower it to u8. *)
local_list = local { "," local } ;
path_list = path { "," path } ;
path = ident { "." ident } ;
local = "%" name_char { name_char } ;
global = "@" ident ; (* no dots inside a global name *)
ident = ( letter | "_" ) { name_char } ;
name_char = letter | digit | "_" ;
int_lit = [ "-" ] ( dec_digits | "0x" hex_digits | "0b" bin_digits ) ;
(* "_" may separate digits; it carries no meaning. *)
version_lit = digit { digit } "." digit { digit } ;
string = '"' { string_char | '\\"' | '\\\\' | '\\n' } '"' ; (* not empty; only in `trusted` *)
(* Feature linear_aggregates, spec 03 §25 and spec 07a. *)
linear_aggregate_op = "array.make" type "(" [ local_list ] ")"
| "struct.unpack" global local "," "next" "->" global
| "array.unpack" type local "," "next" "->" global
| "enum.take" global local "," enum_arm { "," enum_arm }
| "value.swap" local "," local
| "own.pack" type local "," local ok_err ;