|
1 | 1 | //! IMPLEMENTATION DETAILS USED BY MACROS
|
2 | 2 |
|
| 3 | +use crate::hio::{self, HostStream}; |
3 | 4 | use core::fmt::{self, Write};
|
4 | 5 |
|
5 |
| -#[cfg(feature = "machine-mode")] |
6 |
| -use riscv::interrupt; |
| 6 | +static mut HSTDOUT: Option<HostStream> = None; |
7 | 7 |
|
8 |
| -use crate::hio::{self, HostStream}; |
| 8 | +static mut HSTDERR: Option<HostStream> = None; |
9 | 9 |
|
10 |
| -static mut HSTDOUT: Option<HostStream> = None; |
| 10 | +#[cfg(not(feature = "u-mode"))] |
| 11 | +mod machine { |
| 12 | + use super::*; |
| 13 | + |
| 14 | + pub fn hstdout_str(s: &str) { |
| 15 | + let _result = critical_section::with(|_| unsafe { |
| 16 | + if HSTDOUT.is_none() { |
| 17 | + HSTDOUT = Some(hio::hstdout()?); |
| 18 | + } |
| 19 | + |
| 20 | + HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop) |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + pub fn hstdout_fmt(args: fmt::Arguments) { |
| 25 | + let _result = critical_section::with(|_| unsafe { |
| 26 | + if HSTDOUT.is_none() { |
| 27 | + HSTDOUT = Some(hio::hstdout()?); |
| 28 | + } |
| 29 | + |
| 30 | + HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop) |
| 31 | + }); |
| 32 | + } |
| 33 | + |
| 34 | + pub fn hstderr_str(s: &str) { |
| 35 | + let _result = critical_section::with(|_| unsafe { |
| 36 | + if HSTDERR.is_none() { |
| 37 | + HSTDERR = Some(hio::hstderr()?); |
| 38 | + } |
| 39 | + |
| 40 | + HSTDERR.as_mut().unwrap().write_str(s).map_err(drop) |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + pub fn hstderr_fmt(args: fmt::Arguments) { |
| 45 | + let _result = critical_section::with(|_| unsafe { |
| 46 | + if HSTDERR.is_none() { |
| 47 | + HSTDERR = Some(hio::hstderr()?); |
| 48 | + } |
| 49 | + |
| 50 | + HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop) |
| 51 | + }); |
| 52 | + } |
| 53 | +} |
| 54 | +#[cfg(not(feature = "u-mode"))] |
| 55 | +pub use machine::*; |
| 56 | + |
| 57 | +#[cfg(feature = "u-mode")] |
| 58 | +mod user { |
| 59 | + use super::*; |
| 60 | + pub fn hstdout_str(s: &str) { |
| 61 | + let _result = unsafe { |
| 62 | + if HSTDOUT.is_none() { |
| 63 | + HSTDOUT = Some(hio::hstdout().unwrap()); |
| 64 | + } |
| 65 | + |
| 66 | + HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop) |
| 67 | + }; |
| 68 | + } |
11 | 69 |
|
12 |
| -#[cfg(not(feature = "no-semihosting"))] |
13 |
| -cfg_if::cfg_if! { |
14 |
| - if #[cfg(feature="machine-mode")] { |
15 |
| - pub fn hstdout_str(s: &str) { |
16 |
| - let _result = interrupt::free(|_| unsafe { |
17 |
| - if HSTDOUT.is_none() { |
18 |
| - HSTDOUT = Some(hio::hstdout()?); |
19 |
| - } |
20 |
| - |
21 |
| - HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop) |
22 |
| - }); |
23 |
| - } |
24 |
| - |
25 |
| - pub fn hstdout_fmt(args: fmt::Arguments) { |
26 |
| - let _result = interrupt::free(|_| unsafe { |
27 |
| - if HSTDOUT.is_none() { |
28 |
| - HSTDOUT = Some(hio::hstdout()?); |
29 |
| - } |
30 |
| - |
31 |
| - HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop) |
32 |
| - }); |
33 |
| - } |
34 |
| - |
35 |
| - static mut HSTDERR: Option<HostStream> = None; |
36 |
| - |
37 |
| - pub fn hstderr_str(s: &str) { |
38 |
| - let _result = interrupt::free(|_| unsafe { |
39 |
| - if HSTDERR.is_none() { |
40 |
| - HSTDERR = Some(hio::hstderr()?); |
41 |
| - } |
42 |
| - |
43 |
| - HSTDERR.as_mut().unwrap().write_str(s).map_err(drop) |
44 |
| - }); |
45 |
| - } |
46 |
| - |
47 |
| - pub fn hstderr_fmt(args: fmt::Arguments) { |
48 |
| - let _result = interrupt::free(|_| unsafe { |
49 |
| - if HSTDERR.is_none() { |
50 |
| - HSTDERR = Some(hio::hstderr()?); |
51 |
| - } |
52 |
| - |
53 |
| - HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop) |
54 |
| - }); |
55 |
| - } |
| 70 | + pub fn hstdout_fmt(args: fmt::Arguments) { |
| 71 | + let _result = unsafe { |
| 72 | + if HSTDOUT.is_none() { |
| 73 | + HSTDOUT = Some(hio::hstdout().unwrap()); |
| 74 | + } |
| 75 | + |
| 76 | + HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop) |
| 77 | + }; |
56 | 78 | }
|
57 |
| - else if #[cfg(feature = "user-mode")] { |
58 |
| - pub fn hstdout_str(s: &str) { |
59 |
| - let _result = unsafe { |
60 |
| - if HSTDOUT.is_none() { |
61 |
| - HSTDOUT = Some(hio::hstdout().unwrap()); |
62 |
| - } |
63 |
| - |
64 |
| - HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop) |
65 |
| - }; |
66 |
| - } |
67 |
| - |
68 |
| - pub fn hstdout_fmt(args: fmt::Arguments) { |
69 |
| - let _result = unsafe { |
70 |
| - if HSTDOUT.is_none() { |
71 |
| - HSTDOUT = Some(hio::hstdout().unwrap()); |
72 |
| - } |
73 |
| - |
74 |
| - HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop) |
75 |
| - }; |
76 |
| - } |
77 |
| - |
78 |
| - static mut HSTDERR: Option<HostStream> = None; |
79 |
| - |
80 |
| - pub fn hstderr_str(s: &str) { |
81 |
| - let _result = unsafe { |
82 |
| - if HSTDERR.is_none() { |
83 |
| - HSTDERR = Some(hio::hstderr().unwrap()); |
84 |
| - } |
85 |
| - |
86 |
| - HSTDERR.as_mut().unwrap().write_str(s).map_err(drop) |
87 |
| - }; |
88 |
| - } |
89 |
| - |
90 |
| - pub fn hstderr_fmt(args: fmt::Arguments) { |
91 |
| - let _result = unsafe { |
92 |
| - if HSTDERR.is_none() { |
93 |
| - HSTDERR = Some(hio::hstderr().unwrap()); |
94 |
| - } |
95 |
| - |
96 |
| - HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop) |
97 |
| - }; |
98 |
| - } |
| 79 | + |
| 80 | + static mut HSTDERR: Option<HostStream> = None; |
| 81 | + |
| 82 | + pub fn hstderr_str(s: &str) { |
| 83 | + let _result = unsafe { |
| 84 | + if HSTDERR.is_none() { |
| 85 | + HSTDERR = Some(hio::hstderr().unwrap()); |
| 86 | + } |
| 87 | + |
| 88 | + HSTDERR.as_mut().unwrap().write_str(s).map_err(drop) |
| 89 | + }; |
99 | 90 | }
|
100 |
| - else { |
101 |
| - compile_error!("A privilege level has not been selected. Enable either \ |
102 |
| - the machine-mode or user-mode features as appropriate \ |
103 |
| - for your use case."); |
| 91 | + |
| 92 | + pub fn hstderr_fmt(args: fmt::Arguments) { |
| 93 | + let _result = unsafe { |
| 94 | + if HSTDERR.is_none() { |
| 95 | + HSTDERR = Some(hio::hstderr().unwrap()); |
| 96 | + } |
| 97 | + |
| 98 | + HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop) |
| 99 | + }; |
104 | 100 | }
|
105 | 101 | }
|
| 102 | +#[cfg(feature = "u-mode")] |
| 103 | +pub use user::*; |
0 commit comments