aboutsummaryrefslogtreecommitdiff
path: root/cortex-m-semihosting/src/macros.rs
diff options
context:
space:
mode:
authorGravatar Jonas Schievink <jonasschievink@gmail.com> 2020-08-31 23:16:21 +0200
committerGravatar Jonas Schievink <jonasschievink@gmail.com> 2020-10-13 21:30:44 +0200
commit10b29d3a4fc43503c25fb2c7ffbeaad44c2fb103 (patch)
treeceb6fc008cfbcc430a4254464d0e3341c34796e1 /cortex-m-semihosting/src/macros.rs
parentf77d64a2d1505335e4a170d03a40993bb066fd02 (diff)
downloadcortex-m-10b29d3a4fc43503c25fb2c7ffbeaad44c2fb103.tar.gz
cortex-m-10b29d3a4fc43503c25fb2c7ffbeaad44c2fb103.tar.zst
cortex-m-10b29d3a4fc43503c25fb2c7ffbeaad44c2fb103.zip
Import semihosting crates as-is
Diffstat (limited to 'cortex-m-semihosting/src/macros.rs')
-rw-r--r--cortex-m-semihosting/src/macros.rs119
1 files changed, 119 insertions, 0 deletions
diff --git a/cortex-m-semihosting/src/macros.rs b/cortex-m-semihosting/src/macros.rs
new file mode 100644
index 0000000..d10cd3f
--- /dev/null
+++ b/cortex-m-semihosting/src/macros.rs
@@ -0,0 +1,119 @@
+/// Variable argument version of `syscall`
+#[macro_export]
+macro_rules! syscall {
+ ($nr:ident) => {
+ $crate::syscall1($crate::nr::$nr, 0)
+ };
+ ($nr:ident, $a1:expr) => {
+ $crate::syscall($crate::nr::$nr, &[$a1 as usize])
+ };
+ ($nr:ident, $a1:expr, $a2:expr) => {
+ $crate::syscall($crate::nr::$nr, &[$a1 as usize, $a2 as usize])
+ };
+ ($nr:ident, $a1:expr, $a2:expr, $a3:expr) => {
+ $crate::syscall($crate::nr::$nr, &[$a1 as usize, $a2 as usize,
+ $a3 as usize])
+ };
+ ($nr:ident, $a1:expr, $a2:expr, $a3:expr, $a4:expr) => {
+ $crate::syscall($crate::nr::$nr, &[$a1 as usize, $a2 as usize,
+ $a3 as usize, $a4 as usize])
+ };
+}
+
+/// Macro version of `syscall1`.
+#[macro_export]
+macro_rules! syscall1 {
+ ($nr:ident, $a1:expr) => {
+ $crate::syscall1($crate::nr::$nr, $a1 as usize)
+ };
+}
+
+/// Macro for printing to the HOST standard output.
+///
+/// This is similar to the `print!` macro in the standard library. Both will panic on any failure to
+/// print.
+#[macro_export]
+macro_rules! hprint {
+ ($s:expr) => {
+ $crate::export::hstdout_str($s)
+ };
+ ($($tt:tt)*) => {
+ $crate::export::hstdout_fmt(format_args!($($tt)*))
+ };
+}
+
+/// Macro for printing to the HOST standard output, with a newline.
+///
+/// This is similar to the `println!` macro in the standard library. Both will panic on any failure to
+/// print.
+#[macro_export]
+macro_rules! hprintln {
+ () => {
+ $crate::export::hstdout_str("\n")
+ };
+ ($s:expr) => {
+ $crate::export::hstdout_str(concat!($s, "\n"))
+ };
+ ($s:expr, $($tt:tt)*) => {
+ $crate::export::hstdout_fmt(format_args!(concat!($s, "\n"), $($tt)*))
+ };
+}
+
+/// Macro for printing to the HOST standard error.
+///
+/// This is similar to the `eprint!` macro in the standard library. Both will panic on any failure
+/// to print.
+#[macro_export]
+macro_rules! heprint {
+ ($s:expr) => {
+ $crate::export::hstderr_str($s)
+ };
+ ($($tt:tt)*) => {
+ $crate::export::hstderr_fmt(format_args!($($tt)*))
+ };
+}
+
+/// Macro for printing to the HOST standard error, with a newline.
+///
+/// This is similar to the `eprintln!` macro in the standard library. Both will panic on any failure
+/// to print.
+#[macro_export]
+macro_rules! heprintln {
+ () => {
+ $crate::export::hstderr_str("\n")
+ };
+ ($s:expr) => {
+ $crate::export::hstderr_str(concat!($s, "\n"))
+ };
+ ($s:expr, $($tt:tt)*) => {
+ $crate::export::hstderr_fmt(format_args!(concat!($s, "\n"), $($tt)*))
+ };
+}
+
+/// Macro that prints and returns the value of a given expression for quick and
+/// dirty debugging.
+///
+/// Works exactly like `dbg!` in the standard library, replacing `eprintln!`
+/// with `heprintln!`.
+#[macro_export]
+macro_rules! dbg {
+ () => {
+ $crate::heprintln!("[{}:{}]", file!(), line!());
+ };
+ ($val:expr) => {
+ // Use of `match` here is intentional because it affects the lifetimes
+ // of temporaries - https://stackoverflow.com/a/48732525/1063961
+ match $val {
+ tmp => {
+ $crate::heprintln!("[{}:{}] {} = {:#?}",
+ file!(), line!(), stringify!($val), &tmp);
+ tmp
+ }
+ }
+ };
+ // Trailing comma with single argument is ignored
+ ($val:expr,) => { $crate::dbg!($val) };
+ ($($val:expr),+ $(,)?) => {
+ ($($crate::dbg!($val)),+,)
+ };
+}