Skip to main content

byteflow/jit/
exit.rs

1//! Native exit codes returned by compiled traces.
2
3/// Why a compiled trace stopped.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum ExitReason {
6    /// Keep executing natively at `pc`.
7    Continue { pc: u32 },
8    /// Delegate to the interpreter / scheduler at `pc` (Send, Ask, …).
9    Effect { pc: u32 },
10    /// Outermost `Return` — value lives in `return_reg`.
11    Return { return_reg: u8 },
12    /// Instruction quantum exhausted mid-trace.
13    Budget { pc: u32 },
14    /// Semantic fault (divide-by-zero, explicit trap, …).
15    Trap { pc: u32 },
16    /// Type guard failed — resume in the interpreter.
17    Deopt { pc: u32 },
18}
19
20/// Wire format returned across the native boundary.
21#[repr(C)]
22#[derive(Debug, Clone, Copy, PartialEq, Eq)]
23pub struct JitReturn {
24    pub kind: u32,
25    pub pc: u32,
26    pub return_reg: u32,
27    /// Copied out of [`super::frame::JitFrame::written`] after the trace returns.
28    pub written: [u64; 4],
29    /// Copied out of [`super::frame::JitFrame::as_bool`].
30    pub as_bool: [u64; 4],
31    /// Copied out of [`super::frame::JitFrame::as_unit`].
32    pub as_unit: [u64; 4],
33    /// Copied out of [`super::frame::JitFrame::as_float`].
34    pub as_float: [u64; 4],
35}
36
37pub const JIT_CONTINUE: u32 = 0;
38pub const JIT_EFFECT: u32 = 1;
39pub const JIT_RETURN: u32 = 2;
40pub const JIT_BUDGET: u32 = 3;
41pub const JIT_TRAP: u32 = 4;
42pub const JIT_DEOPT: u32 = 5;
43
44impl JitReturn {
45    pub fn from_reason(reason: ExitReason) -> Self {
46        let mut ret = Self {
47            kind: 0,
48            pc: 0,
49            return_reg: 0,
50            written: [0; 4],
51            as_bool: [0; 4],
52            as_unit: [0; 4],
53            as_float: [0; 4],
54        };
55        match reason {
56            ExitReason::Continue { pc } => {
57                ret.kind = JIT_CONTINUE;
58                ret.pc = pc;
59            }
60            ExitReason::Effect { pc } => {
61                ret.kind = JIT_EFFECT;
62                ret.pc = pc;
63            }
64            ExitReason::Return { return_reg } => {
65                ret.kind = JIT_RETURN;
66                ret.return_reg = u32::from(return_reg);
67            }
68            ExitReason::Budget { pc } => {
69                ret.kind = JIT_BUDGET;
70                ret.pc = pc;
71            }
72            ExitReason::Trap { pc } => {
73                ret.kind = JIT_TRAP;
74                ret.pc = pc;
75            }
76            ExitReason::Deopt { pc } => {
77                ret.kind = JIT_DEOPT;
78                ret.pc = pc;
79            }
80        }
81        ret
82    }
83
84    pub fn into_reason(self) -> ExitReason {
85        match self.kind {
86            JIT_CONTINUE => ExitReason::Continue { pc: self.pc },
87            JIT_EFFECT => ExitReason::Effect { pc: self.pc },
88            JIT_RETURN => ExitReason::Return {
89                return_reg: self.return_reg as u8,
90            },
91            JIT_BUDGET => ExitReason::Budget { pc: self.pc },
92            JIT_TRAP => ExitReason::Trap { pc: self.pc },
93            JIT_DEOPT => ExitReason::Deopt { pc: self.pc },
94            _ => ExitReason::Deopt { pc: self.pc },
95        }
96    }
97}