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/// Spill arena for in-trace calls. One window of [`JitFrame::spill_stride`]
28/// slots per [`MAX_JIT_CALL_DEPTH`] level.
29pub const OFF_SPILL: i32 = 208;
30pub const OFF_SPILL_STRIDE: i32 = 216;
31/// Bit `reg` means the stored bits are an `f64`, not an `Int`.
32pub const OFF_AS_FLOAT: i32 = 224;
33/// Float tags observed on entry. Must match [`JitFrame::entry_as_float`].
34pub const OFF_ENTRY_FLOAT: i32 = 256;
35
36/// Byte offsets inside [`JitCallRecord`].
37pub const REC_RETURN_PC: i32 = 0;
38pub const REC_RETURN_FN: i32 = 4;
39pub const REC_DEST: i32 = 8;
40pub const REC_CALLER_REGS: i32 = 12;
41pub const REC_WRITTEN: i32 = 16;
42pub const REC_AS_BOOL: i32 = 48;
43pub const REC_AS_UNIT: i32 = 80;
44
45/// Maximum nested intra-chunk calls handled inside one compiled trace.
46pub const MAX_JIT_CALL_DEPTH: usize = 32;
47
48/// Saved caller state for one `Opcode::Call`.
49#[repr(C)]
50#[derive(Clone, Copy, Debug)]
51pub struct JitCallRecord {
52    pub return_pc: u32,
53    pub return_function: u32,
54    pub dest_reg: u32,
55    pub caller_register_count: u32,
56    /// Caller [`JitFrame::written`] saved across the leaf.
57    pub written: [u64; 4],
58    pub as_bool: [u64; 4],
59    pub as_unit: [u64; 4],
60}
61
62/// Mutable state for one JIT invocation on the current bytecode frame.
63///
64/// v1 keeps scalar work in a parallel `i64` slot table (Int specialization).
65/// The dispatcher copies [`crate::Value::Int`] in before the call and
66/// writes results back after `Return`.
67#[repr(C)]
68pub struct JitFrame {
69    pub slots: *mut i64,
70    pub register_count: u32,
71    pub pc: u32,
72    pub budget: u32,
73    pub function: u32,
74    pub exit_kind: u32,
75    pub return_reg: u32,
76    pub call_depth: u32,
77    pub call_stack: *mut JitCallRecord,
78    /// Registers this trace assigned. Unset bits keep the VM's previous value
79    /// on the way back, so a `Unit` the trace never wrote does not become `Int(0)`.
80    pub written: [u64; 4],
81    /// Among [`Self::written`] bits, which results are bytecode `Bool`s
82    /// (`Eq` / `Lt` / `Le` / `LoadConst` of a bool, or a `Move` of one).
83    pub as_bool: [u64; 4],
84    /// Among [`Self::written`] bits, which results are `Unit`.
85    pub as_unit: [u64; 4],
86    /// `Bool` bits observed on entry. Read-only for the compiled trace.
87    pub entry_as_bool: [u64; 4],
88    /// `Unit` bits observed on entry. Read-only for the compiled trace.
89    pub entry_as_unit: [u64; 4],
90    /// `spill_stride * MAX_JIT_CALL_DEPTH` i64 slots, owned by the caller
91    /// of the trace for the duration of the call.
92    pub spill: *mut i64,
93    pub spill_stride: u32,
94    /// Among [`Self::written`] bits, which results are `Float`s stored as
95    /// their IEEE bits in the `i64` slot.
96    pub as_float: [u64; 4],
97    /// `Float` bits observed on entry.
98    pub entry_as_float: [u64; 4],
99}
100
101pub type JitEntry = unsafe extern "C" fn(*mut JitFrame);
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn tag_words_sit_where_the_compiler_stores_them() {
109        assert_eq!(
110            std::mem::offset_of!(JitFrame, written),
111            OFF_WRITTEN as usize
112        );
113        assert_eq!(
114            std::mem::offset_of!(JitFrame, as_bool),
115            OFF_AS_BOOL as usize
116        );
117        assert_eq!(
118            std::mem::offset_of!(JitFrame, as_unit),
119            OFF_AS_UNIT as usize
120        );
121        assert_eq!(
122            std::mem::offset_of!(JitFrame, entry_as_bool),
123            OFF_ENTRY_BOOL as usize
124        );
125        assert_eq!(
126            std::mem::offset_of!(JitFrame, entry_as_unit),
127            OFF_ENTRY_UNIT as usize
128        );
129        assert_eq!(std::mem::offset_of!(JitFrame, spill), OFF_SPILL as usize);
130        assert_eq!(
131            std::mem::offset_of!(JitFrame, spill_stride),
132            OFF_SPILL_STRIDE as usize
133        );
134        assert_eq!(
135            std::mem::offset_of!(JitFrame, as_float),
136            OFF_AS_FLOAT as usize
137        );
138        assert_eq!(
139            std::mem::offset_of!(JitFrame, entry_as_float),
140            OFF_ENTRY_FLOAT as usize
141        );
142        assert_eq!(
143            std::mem::offset_of!(JitCallRecord, written),
144            REC_WRITTEN as usize
145        );
146        assert_eq!(
147            std::mem::offset_of!(JitCallRecord, as_bool),
148            REC_AS_BOOL as usize
149        );
150        assert_eq!(
151            std::mem::offset_of!(JitCallRecord, as_unit),
152            REC_AS_UNIT as usize
153        );
154    }
155}