midenc_codegen_masm/
events.rs

1//! This module contains the set of compiler-emitted event codes, and their explanations
2use core::num::NonZeroU32;
3
4/// This event is emitted via `trace`, and indicates that a procedure call frame is entered
5///
6/// The mnemonic here is F = frame, 0 = open
7pub const TRACE_FRAME_START: u32 = 0xf0;
8
9/// This event is emitted via `trace`, and indicates that a procedure call frame is exited
10///
11/// The mnemonic here is F = frame, C = close
12pub const TRACE_FRAME_END: u32 = 0xfc;
13
14/// A typed wrapper around the raw trace events known to the compiler
15#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
16#[repr(u32)]
17pub enum TraceEvent {
18    FrameStart,
19    FrameEnd,
20    AssertionFailed(Option<NonZeroU32>),
21    Unknown(u32),
22}
23impl TraceEvent {
24    #[inline(always)]
25    pub fn is_frame_start(&self) -> bool {
26        matches!(self, Self::FrameStart)
27    }
28
29    #[inline(always)]
30    pub fn is_frame_end(&self) -> bool {
31        matches!(self, Self::FrameEnd)
32    }
33
34    pub fn as_u32(self) -> u32 {
35        match self {
36            Self::FrameStart => TRACE_FRAME_START,
37            Self::FrameEnd => TRACE_FRAME_END,
38            Self::AssertionFailed(None) => 0,
39            Self::AssertionFailed(Some(code)) => code.get(),
40            Self::Unknown(event) => event,
41        }
42    }
43}
44impl From<u32> for TraceEvent {
45    fn from(raw: u32) -> Self {
46        match raw {
47            TRACE_FRAME_START => Self::FrameStart,
48            TRACE_FRAME_END => Self::FrameEnd,
49            _ => Self::Unknown(raw),
50        }
51    }
52}
53impl From<TraceEvent> for u32 {
54    fn from(event: TraceEvent) -> Self {
55        match event {
56            TraceEvent::FrameStart => TRACE_FRAME_START,
57            TraceEvent::FrameEnd => TRACE_FRAME_END,
58            TraceEvent::AssertionFailed(None) => 0,
59            TraceEvent::AssertionFailed(Some(code)) => code.get(),
60            TraceEvent::Unknown(code) => code,
61        }
62    }
63}