Skip to main content

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/// This event is emitted via `trace`, and indicates that a line should be printed.
15///
16/// The bytes representing the string are expected in memory. The executor reads the start address
17/// and length from the operand stack.
18///
19/// The mnemonic here is ASCII `PRNT`.
20pub const TRACE_PRINT_LN: u32 = 0x50_52_4e_54;
21
22/// A typed wrapper around the raw trace events known to the compiler
23#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
24#[repr(u32)]
25pub enum TraceEvent {
26    FrameStart,
27    FrameEnd,
28    PrintLn,
29    AssertionFailed(Option<NonZeroU32>),
30    Unknown(u32),
31}
32impl TraceEvent {
33    #[inline(always)]
34    pub fn is_frame_start(&self) -> bool {
35        matches!(self, Self::FrameStart)
36    }
37
38    #[inline(always)]
39    pub fn is_frame_end(&self) -> bool {
40        matches!(self, Self::FrameEnd)
41    }
42
43    pub fn as_u32(self) -> u32 {
44        match self {
45            Self::FrameStart => TRACE_FRAME_START,
46            Self::FrameEnd => TRACE_FRAME_END,
47            Self::PrintLn => TRACE_PRINT_LN,
48            Self::AssertionFailed(None) => 0,
49            Self::AssertionFailed(Some(code)) => code.get(),
50            Self::Unknown(event) => event,
51        }
52    }
53}
54impl From<u32> for TraceEvent {
55    fn from(raw: u32) -> Self {
56        match raw {
57            TRACE_FRAME_START => Self::FrameStart,
58            TRACE_FRAME_END => Self::FrameEnd,
59            TRACE_PRINT_LN => Self::PrintLn,
60            _ => Self::Unknown(raw),
61        }
62    }
63}
64impl From<TraceEvent> for u32 {
65    fn from(event: TraceEvent) -> Self {
66        match event {
67            TraceEvent::FrameStart => TRACE_FRAME_START,
68            TraceEvent::FrameEnd => TRACE_FRAME_END,
69            TraceEvent::PrintLn => TRACE_PRINT_LN,
70            TraceEvent::AssertionFailed(None) => 0,
71            TraceEvent::AssertionFailed(Some(code)) => code.get(),
72            TraceEvent::Unknown(code) => code,
73        }
74    }
75}