cortex_m_semihosting/
macros.rs

1/// Variable argument version of `syscall`
2#[macro_export]
3macro_rules! syscall {
4    ($nr:ident) => {
5        $crate::syscall1($crate::nr::$nr, 0)
6    };
7    ($nr:ident, $a1:expr) => {
8        $crate::syscall($crate::nr::$nr, &[$a1 as usize])
9    };
10    ($nr:ident, $a1:expr, $a2:expr) => {
11        $crate::syscall($crate::nr::$nr, &[$a1 as usize, $a2 as usize])
12    };
13    ($nr:ident, $a1:expr, $a2:expr, $a3:expr) => {
14        $crate::syscall($crate::nr::$nr, &[$a1 as usize, $a2 as usize, $a3 as usize])
15    };
16    ($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr) => {
17        $crate::syscall(
18            $crate::nr::$nr,
19            &[$a1 as usize, $a2 as usize, $a3 as usize, $a4 as usize],
20        )
21    };
22}
23
24/// Macro version of `syscall1`.
25#[macro_export]
26macro_rules! syscall1 {
27    ($nr:ident, $a1:expr) => {
28        $crate::syscall1($crate::nr::$nr, $a1 as usize)
29    };
30}
31
32/// Macro for printing to the HOST standard output.
33///
34/// This is similar to the `print!` macro in the standard library. Both will panic on any failure to
35/// print.
36#[macro_export]
37macro_rules! hprint {
38    ($s:expr) => {
39        $crate::export::hstdout_str($s)
40    };
41    ($($tt:tt)*) => {
42        $crate::export::hstdout_fmt(format_args!($($tt)*))
43    };
44}
45
46/// Macro for printing to the HOST standard output, with a newline.
47///
48/// This is similar to the `println!` macro in the standard library. Both will panic on any failure to
49/// print.
50#[macro_export]
51macro_rules! hprintln {
52    () => {
53        $crate::export::hstdout_str("\n")
54    };
55    ($s:expr) => {
56        $crate::export::hstdout_str(concat!($s, "\n"))
57    };
58    ($s:expr, $($tt:tt)*) => {
59        $crate::export::hstdout_fmt(format_args!(concat!($s, "\n"), $($tt)*))
60    };
61}
62
63/// Macro for printing to the HOST standard error.
64///
65/// This is similar to the `eprint!` macro in the standard library. Both will panic on any failure
66/// to print.
67#[macro_export]
68macro_rules! heprint {
69    ($s:expr) => {
70        $crate::export::hstderr_str($s)
71    };
72    ($($tt:tt)*) => {
73        $crate::export::hstderr_fmt(format_args!($($tt)*))
74    };
75}
76
77/// Macro for printing to the HOST standard error, with a newline.
78///
79/// This is similar to the `eprintln!` macro in the standard library. Both will panic on any failure
80/// to print.
81#[macro_export]
82macro_rules! heprintln {
83    () => {
84        $crate::export::hstderr_str("\n")
85    };
86    ($s:expr) => {
87        $crate::export::hstderr_str(concat!($s, "\n"))
88    };
89    ($s:expr, $($tt:tt)*) => {
90        $crate::export::hstderr_fmt(format_args!(concat!($s, "\n"), $($tt)*))
91    };
92}
93
94/// Macro that prints and returns the value of a given expression for quick and
95/// dirty debugging.
96///
97/// Works exactly like `dbg!` in the standard library, replacing `eprintln!`
98/// with `heprintln!`.
99#[macro_export]
100macro_rules! dbg {
101    () => {
102        $crate::heprintln!("[{}:{}]", file!(), line!());
103    };
104    ($val:expr) => {
105        // Use of `match` here is intentional because it affects the lifetimes
106        // of temporaries - https://stackoverflow.com/a/48732525/1063961
107        match $val {
108            tmp => {
109                $crate::heprintln!("[{}:{}] {} = {:#?}",
110                    file!(), line!(), stringify!($val), &tmp);
111                tmp
112            }
113        }
114    };
115    // Trailing comma with single argument is ignored
116    ($val:expr,) => { $crate::dbg!($val) };
117    ($($val:expr),+ $(,)?) => {
118        ($($crate::dbg!($val)),+,)
119    };
120}