Skip to main content

byteflow/jit/
frame.rs

1//! Execution frame passed into compiled traces.
2
3/// Byte offsets for [`JitFrame`] fields (must match `repr(C)` layout).
4pub const OFF_REGISTER_COUNT: i32 = 8;
5pub const OFF_PC: i32 = 12;
6pub const OFF_BUDGET: i32 = 16;
7pub const OFF_FUNCTION: i32 = 20;
8pub const OFF_EXIT_KIND: i32 = 24;
9pub const OFF_RETURN_REG: i32 = 28;
10pub const OFF_CALL_DEPTH: i32 = 32;
11pub const OFF_CALL_STACK: i32 = 40;
12/// Four `u64` words: bit `reg` is set when the trace stored that register.
13/// Must match [`JitFrame::written`].
14pub const OFF_WRITTEN: i32 = 48;
15/// Four `u64` words: bit `reg` means the stored value is a `Bool`, not an `Int`.
16/// Must match [`JitFrame::as_bool`].
17pub const OFF_AS_BOOL: i32 = 80;
18/// Four `u64` words: bit `reg` means the stored value is `Unit`.
19/// Must match [`JitFrame::as_unit`].
20pub const OFF_AS_UNIT: i32 = 112;
21/// Tags of the registers as they were when the trace was entered.
22/// A `Move` of a slot the trace has not assigned copies these bits unless
23/// that register was already written earlier in the same execution.
24/// Must match [`JitFrame::entry_as_bool`] and [`JitFrame::entry_as_unit`].
25pub const OFF_ENTRY_BOOL: i32 = 144;
26pub const OFF_ENTRY_UNIT: i32 = 176;
27
28/// Maximum nested intra-chunk calls handled inside one compiled trace.
29pub const MAX_JIT_CALL_DEPTH: usize = 32;
30
31/// Saved caller state for one `Opcode::Call`.
32#[repr(C)]
33#[derive(Clone, Copy, Debug)]
34pub struct JitCallRecord {
35    pub return_pc: u32,
36    pub return_function: u32,
37    pub dest_reg: u32,
38    pub caller_register_count: u32,
39}
40
41/// Mutable state for one JIT invocation on the current bytecode frame.
42///
43/// v1 keeps scalar work in a parallel `i64` slot table (Int specialization).
44/// The dispatcher copies [`crate::Value::Int`] in before the call and
45/// writes results back after `Return`.
46#[repr(C)]
47pub struct JitFrame {
48    pub slots: *mut i64,
49    pub register_count: u32,
50    pub pc: u32,
51    pub budget: u32,
52    pub function: u32,
53    pub exit_kind: u32,
54    pub return_reg: u32,
55    pub call_depth: u32,
56    pub call_stack: *mut JitCallRecord,
57    /// Registers this trace assigned. Unset bits keep the VM's previous value
58    /// on the way back, so a `Unit` the trace never wrote does not become `Int(0)`.
59    pub written: [u64; 4],
60    /// Among [`Self::written`] bits, which results are bytecode `Bool`s
61    /// (`Eq` / `Lt` / `Le` / `LoadConst` of a bool, or a `Move` of one).
62    pub as_bool: [u64; 4],
63    /// Among [`Self::written`] bits, which results are `Unit`.
64    pub as_unit: [u64; 4],
65    /// `Bool` bits observed on entry. Read-only for the compiled trace.
66    pub entry_as_bool: [u64; 4],
67    /// `Unit` bits observed on entry. Read-only for the compiled trace.
68    pub entry_as_unit: [u64; 4],
69}
70
71pub type JitEntry = unsafe extern "C" fn(*mut JitFrame);
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn tag_words_sit_where_the_compiler_stores_them() {
79        assert_eq!(
80            std::mem::offset_of!(JitFrame, written),
81            OFF_WRITTEN as usize
82        );
83        assert_eq!(
84            std::mem::offset_of!(JitFrame, as_bool),
85            OFF_AS_BOOL as usize
86        );
87        assert_eq!(
88            std::mem::offset_of!(JitFrame, as_unit),
89            OFF_AS_UNIT as usize
90        );
91        assert_eq!(
92            std::mem::offset_of!(JitFrame, entry_as_bool),
93            OFF_ENTRY_BOOL as usize
94        );
95        assert_eq!(
96            std::mem::offset_of!(JitFrame, entry_as_unit),
97            OFF_ENTRY_UNIT as usize
98        );
99    }
100}