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