Skip to main content

byteflow/bytecode/
chunk.rs

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