cranelift_codegen/isa/x64/inst/
emit_state.rs

1use super::*;
2use crate::ir;
3use cranelift_control::ControlPlane;
4
5/// State carried between emissions of a sequence of instructions.
6#[derive(Default, Clone, Debug)]
7pub struct EmitState {
8    /// Safepoint stack map for upcoming instruction, as provided to
9    /// `pre_safepoint()`.
10    stack_map: Option<StackMap>,
11
12    /// The user stack map for the upcoming instruction, as provided to
13    /// `pre_safepoint()`.
14    user_stack_map: Option<ir::UserStackMap>,
15
16    /// Only used during fuzz-testing. Otherwise, it is a zero-sized struct and
17    /// optimized away at compiletime. See [cranelift_control].
18    ctrl_plane: ControlPlane,
19
20    /// A copy of the frame layout, used during the emission of `Inst::ReturnCallKnown` and
21    /// `Inst::ReturnCallUnknown` instructions.
22    frame_layout: FrameLayout,
23}
24
25impl MachInstEmitState<Inst> for EmitState {
26    fn new(abi: &Callee<X64ABIMachineSpec>, ctrl_plane: ControlPlane) -> Self {
27        EmitState {
28            stack_map: None,
29            user_stack_map: None,
30            ctrl_plane,
31            frame_layout: abi.frame_layout().clone(),
32        }
33    }
34
35    fn pre_safepoint(
36        &mut self,
37        stack_map: Option<StackMap>,
38        user_stack_map: Option<ir::UserStackMap>,
39    ) {
40        self.stack_map = stack_map;
41        self.user_stack_map = user_stack_map;
42    }
43
44    fn ctrl_plane_mut(&mut self) -> &mut ControlPlane {
45        &mut self.ctrl_plane
46    }
47
48    fn take_ctrl_plane(self) -> ControlPlane {
49        self.ctrl_plane
50    }
51
52    fn frame_layout(&self) -> &FrameLayout {
53        &self.frame_layout
54    }
55}
56
57impl EmitState {
58    pub(crate) fn take_stack_map(&mut self) -> (Option<StackMap>, Option<ir::UserStackMap>) {
59        (self.stack_map.take(), self.user_stack_map.take())
60    }
61
62    pub(crate) fn clear_post_insn(&mut self) {
63        self.stack_map = None;
64    }
65}