Skip to main content

byteflow/jit/
exit.rs

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