Skip to main content

runmat_runtime/native/
control.rs

1use super::{NativeResumeState, NativeRootSet, NativeSourceLocation};
2
3#[repr(transparent)]
4#[derive(Clone, Copy, Debug, Eq, PartialEq)]
5pub struct NativeExitKind(pub u32);
6
7impl NativeExitKind {
8    pub const COMPLETED: Self = Self(0);
9    pub const EXCEPTION: Self = Self(1);
10    pub const CANCELLED: Self = Self(2);
11    pub const SUSPENDED: Self = Self(3);
12    pub const DEOPTIMIZED: Self = Self(4);
13
14    pub const fn is_known(self) -> bool {
15        self.0 <= Self::DEOPTIMIZED.0
16    }
17}
18
19#[repr(transparent)]
20#[derive(Clone, Copy, Debug, Eq, PartialEq)]
21pub struct NativeCancellationReason(pub u32);
22
23impl NativeCancellationReason {
24    pub const REQUESTED: Self = Self(0);
25    pub const DEADLINE: Self = Self(1);
26    pub const PARENT_SCOPE: Self = Self(2);
27    pub const SHUTDOWN: Self = Self(3);
28}
29
30#[repr(transparent)]
31#[derive(Clone, Copy, Debug, Eq, PartialEq)]
32pub struct NativeResumeKind(pub u32);
33
34impl NativeResumeKind {
35    pub const INTERPRETER: Self = Self(0);
36    pub const GENERIC_NATIVE: Self = Self(1);
37    pub const OPTIMIZED_NATIVE: Self = Self(2);
38}
39
40#[repr(transparent)]
41#[derive(Clone, Copy, Debug, Eq, PartialEq)]
42pub struct NativeDeoptReason(pub u32);
43
44impl NativeDeoptReason {
45    pub const GUARD: Self = Self(0);
46    pub const REPRESENTATION: Self = Self(1);
47    pub const DEPENDENCY_INVALIDATED: Self = Self(2);
48    pub const RUNTIME_STATE: Self = Self(3);
49    pub const EXPLICIT_SLOW_PATH: Self = Self(4);
50}
51
52/// Opaque exception identity plus precise throw location.
53#[repr(C)]
54#[derive(Clone, Copy, Debug, Default)]
55pub struct NativeException {
56    pub handle: u64,
57    pub generation: u64,
58    pub source: NativeSourceLocation,
59}
60
61#[repr(C)]
62#[derive(Clone, Copy, Debug)]
63pub struct NativeCancellation {
64    pub reason: NativeCancellationReason,
65    pub reserved: u32,
66    pub generation: u64,
67}
68
69#[repr(C)]
70#[derive(Clone, Copy, Debug)]
71pub struct NativeSuspension {
72    pub continuation: u64,
73    pub generation: u64,
74    pub resume: *const NativeResumeState,
75    pub roots: NativeRootSet,
76}
77
78#[repr(C)]
79#[derive(Clone, Copy, Debug)]
80pub struct NativeDeoptimization {
81    pub reason: NativeDeoptReason,
82    pub target: NativeResumeKind,
83    pub guard: u64,
84    pub resume: *const NativeResumeState,
85}
86
87/// Result of a native call. Only the payload selected by `kind` is meaningful.
88#[repr(C)]
89#[derive(Clone, Copy, Debug)]
90pub struct NativeExit {
91    pub abi_version: u32,
92    pub kind: NativeExitKind,
93    pub produced_outputs: u32,
94    pub flags: u32,
95    pub exception: NativeException,
96    pub cancellation: NativeCancellation,
97    pub suspension: NativeSuspension,
98    pub deoptimization: NativeDeoptimization,
99}
100
101/// State exposed at every allocation, cancellation, transfer, or deopt poll.
102#[repr(C)]
103#[derive(Clone, Copy, Debug)]
104pub struct NativeSafepoint {
105    pub id: u64,
106    pub resume: *const NativeResumeState,
107    pub roots: NativeRootSet,
108}
109
110impl NativeExit {
111    pub fn completed(produced_outputs: u32) -> Self {
112        Self::base(NativeExitKind::COMPLETED, produced_outputs)
113    }
114
115    pub fn exception(exception: NativeException) -> Self {
116        Self {
117            exception,
118            ..Self::base(NativeExitKind::EXCEPTION, 0)
119        }
120    }
121
122    pub fn cancelled(cancellation: NativeCancellation) -> Self {
123        Self {
124            cancellation,
125            ..Self::base(NativeExitKind::CANCELLED, 0)
126        }
127    }
128
129    pub fn suspended(suspension: NativeSuspension) -> Self {
130        Self {
131            suspension,
132            ..Self::base(NativeExitKind::SUSPENDED, 0)
133        }
134    }
135
136    pub fn deoptimized(deoptimization: NativeDeoptimization) -> Self {
137        Self {
138            deoptimization,
139            ..Self::base(NativeExitKind::DEOPTIMIZED, 0)
140        }
141    }
142
143    fn base(kind: NativeExitKind, produced_outputs: u32) -> Self {
144        Self {
145            abi_version: super::NATIVE_ABI_VERSION.encoded(),
146            kind,
147            produced_outputs,
148            flags: 0,
149            exception: NativeException::default(),
150            cancellation: NativeCancellation {
151                reason: NativeCancellationReason::REQUESTED,
152                reserved: 0,
153                generation: 0,
154            },
155            suspension: NativeSuspension {
156                continuation: 0,
157                generation: 0,
158                resume: std::ptr::null(),
159                roots: NativeRootSet::default(),
160            },
161            deoptimization: NativeDeoptimization {
162                reason: NativeDeoptReason::GUARD,
163                target: NativeResumeKind::INTERPRETER,
164                guard: 0,
165                resume: std::ptr::null(),
166            },
167        }
168    }
169}