Types cap<K>, resources, own/region, scope/task/shield, endpoints, mutex/guard, island/frozen/iref/arc and the built-in enums; productions for capability, heap, region, task, cancellation, channel, sync and graph instructions, with the named-edge terminator forms. Every opcode in the conformance corpus and in the spec contract tables has a production. AIR bundle +3.6 % (cl100k), recorded.
228 lines
12 KiB
EBNF
228 lines
12 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). *)
|
|
|
|
operation = "const." int_type int_lit
|
|
| "const.bool" ( "true" | "false" )
|
|
| "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 ] ")"
|
|
| ownership_op
|
|
| arrays_op
|
|
| capability_op
|
|
| heap_op
|
|
| region_op
|
|
| task_op
|
|
| cancellation_op
|
|
| channel_op
|
|
| sync_op
|
|
| graph_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> *)
|
|
| "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 *)
|
|
|
|
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 *)
|
|
| ( "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 *)
|
|
| 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` *)
|