byteflow/bytecode/chunk.rs
1// Docs ratchet: public items here predate `missing_docs`. Delete this
2// allow once they are documented; new files get the lint by default.
3#![allow(missing_docs)]
4
5use super::instruction::Instruction;
6use super::value::Value;
7
8/// On-disk magic for the `.bf` module format (see design notes §32).
9/// Chosen so a corrupted/truncated file is rejected in the first 4 bytes
10/// rather than partway through decoding.
11pub const MAGIC: [u8; 4] = *b"BFV0";
12
13/// Scalar module header (v5). A list, map, or record raises the header to
14/// [`ABI_VERSION_LISTS`], [`ABI_VERSION_MAPS`], or [`ABI_VERSION_RECORDS`].
15/// Bump on any breaking change to instruction encoding, constant
16/// representation, or function-table layout.
17///
18/// v2: [`Value::Message`] wire tag `5`.
19/// v3: `Message.reply_cap`; [`Value::Cap`] wire tag `6` (FlowCap).
20/// v4: [`Value::Str`] tag `7`, [`Value::Bytes`] tag `8`.
21/// v5: CapId is 128-bit; `Message.payload` is a nested [`Value`].
22/// v6: [`Value::List`] wire tag `9`. Modules without a list still encode
23/// as v5 so an older reader keeps accepting them. A list constant is
24/// written only as v6; v5 decoders reject tag `9`.
25/// v7: [`Value::Map`] wire tag `10`. A module without a map stays on v5 or
26/// v6. v5 and v6 decoders reject tag `10`.
27/// v8: [`Value::Record`] wire tag `11`. A module without a record stays on
28/// v5, v6, or v7. Older headers reject tag `11`.
29pub const ABI_VERSION: u32 = 5;
30
31/// ABI that introduces [`Value::List`]. [`encode`](super::encode) emits this
32/// when the pool contains a list and no map or record. [`decode`](super::decode)
33/// accepts v5 through v8. A v5 header rejects tag `9`.
34pub const ABI_VERSION_LISTS: u32 = 6;
35
36/// ABI that introduces [`Value::Map`]. Emitted when a constant contains a
37/// map and no record. A map nested in a record is written as
38/// [`ABI_VERSION_RECORDS`]. Decode accepts v5 through v8. A header below v7
39/// rejects tag `10`.
40pub const ABI_VERSION_MAPS: u32 = 7;
41
42/// ABI that introduces [`Value::Record`]. Emitted when a constant contains
43/// a record, including one nested in a list, map, or message. Decode accepts
44/// v5 through v8. A header below v8 rejects tag `11`.
45pub const ABI_VERSION_RECORDS: u32 = 8;
46
47/// A callable entry point inside a [`Chunk`]: either bytecode-defined or a
48/// slot reserved for a native (Rust) function registered with the runtime
49/// via the FFI table.
50#[derive(Clone, Debug, PartialEq, Eq)]
51pub struct FunctionDef {
52 pub name: String,
53 /// Index into `Chunk::code` where execution starts.
54 pub entry: u32,
55 /// Number of parameters. The VM copies them into `r0..arity`
56 /// (exclusive end): arity 2 fills `r0` and `r1`.
57 pub arity: u8,
58 /// Upper bound on registers this function uses; the VM allocates
59 /// exactly this many per call frame instead of a fixed worst-case size.
60 pub num_registers: u8,
61}
62
63/// A compiled unit of Byteflow bytecode: code, constants and the function
64/// table. One `Chunk` can back many concurrently-running flows — it is
65/// immutable after construction, so it is shared behind an `Arc` rather than
66/// copied per flow (see [`crate::Vm`]).
67#[derive(Clone, Debug, Default, PartialEq)]
68pub struct Chunk {
69 pub name: String,
70 pub constants: Vec<Value>,
71 pub code: Vec<Instruction>,
72 pub functions: Vec<FunctionDef>,
73}
74
75impl Chunk {
76 pub fn function(&self, index: u32) -> Option<&FunctionDef> {
77 self.functions.get(index as usize)
78 }
79
80 pub fn constant(&self, index: u32) -> Option<&Value> {
81 self.constants.get(index as usize)
82 }
83
84 /// Number of instructions, used by the verifier to bound-check jump
85 /// targets ahead of time instead of on every branch at runtime.
86 pub fn len(&self) -> usize {
87 self.code.len()
88 }
89
90 pub fn is_empty(&self) -> bool {
91 self.code.is_empty()
92 }
93}