Merge remote-tracking branch 'origin/mohr/rust' into nehlich/ffs

This commit is contained in:
paul nehlich
2024-07-26 12:53:39 +02:00
26 changed files with 513 additions and 110 deletions

View File

@ -10,8 +10,4 @@ crate-type = ["staticlib"]
panic = 'abort'
[profile.release]
panic = 'abort'
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
panic = 'abort'

View File

@ -1,37 +1,76 @@
// TODO this is platform specific
pub struct Outbytes {}
pub struct Stdout {}
pub struct Stderr {}
use core::fmt::{Error, Write};
impl Write for Outbytes {
extern "C" {
pub fn write(fd: core::ffi::c_int, buffer: *const core::ffi::c_void, count: usize) -> core::ffi::c_int;
}
impl Write for Stdout {
fn write_str(&mut self, s: &str) -> Result<(), Error> {
for c in s.as_bytes() {
unsafe {
crate::fsrc::osal::outbyte(*c);
}
// Safe because write will only read the pointer an then return
let result = unsafe {
write(
1,
s.as_bytes().as_ptr() as *const core::ffi::c_void,
s.as_bytes().len(),
)
};
// We do not retry incomplete writes, this is stdout after all...
let safe_count: i32 = match s.as_bytes().len().try_into() {
Ok(count)=> count,
Err(_) => return Err(Error)
};
if result < safe_count {
Err(Error)
} else {
Ok(())
}
}
}
impl Write for Stderr {
fn write_str(&mut self, s: &str) -> Result<(), Error> {
// Safe because write will only read the pointer an then return
let result = unsafe {
write(
2,
s.as_bytes().as_ptr() as *const core::ffi::c_void,
s.as_bytes().len(),
)
};
// We do not retry incomplete writes, this is stdout after all...
let safe_count: i32 = match s.as_bytes().len().try_into() {
Ok(count)=> count,
Err(_) => return Err(Error)
};
if result < safe_count {
Err(Error)
} else {
Ok(())
}
Ok(())
}
}
#[macro_export]
macro_rules! sifln {
($(,)?) => (
let mut stdout = Outbytes {};
writeln!(stdout);
//let mut stdout = Outbytes {};
writeln!(Stdout {});
);
($($arg:tt)*) => (
let mut stdout = crate::fsrc::sif::Outbytes {};
let _alwaysok = writeln!(stdout, $($arg)*);
let _alwaysok = writeln!(crate::fsrc::sif::Stdout {}, $($arg)*);
);
}
#[macro_export]
macro_rules! sif {
($($arg:tt)*) => (
let mut stdout = crate::fsrc::sif::Outbytes {};
let _alwaysok = write!(stdout, $($arg)*);
let _alwaysok = write!(crate::fsrc::sif::Stdout {}, $($arg)*);
);
}

View File

@ -20,9 +20,9 @@ fn panic(panic: &PanicInfo<'_>) -> ! {
unsafe {
osal::stop_it();
}
// TODO: Make this unicode-safe
sifln!("");
sif!("in task \"");
// TODO: Make this unicode-safe
_ = writeln!(crate::fsrc::sif::Stderr {},"");
_ = write!(crate::fsrc::sif::Stderr {},"in task \"");
unsafe {
//TODO is from_ptr safe enough?
let task_name = core::ffi::CStr::from_ptr(osal::get_task_name());
@ -32,12 +32,12 @@ fn panic(panic: &PanicInfo<'_>) -> ! {
sif!("{}", string);
}
Err(_) => {
sif!("Schei<EFBFBD> Encoding");
_ = writeln!(crate::fsrc::sif::Stderr {},"Schei<EFBFBD> Encoding");
}
}
}
sifln!("\":");
sifln!("{}", panic);
_ = writeln!(crate::fsrc::sif::Stderr {},"\":");
_ = writeln!(crate::fsrc::sif::Stderr {},"{}", panic);
//TODO: stop RTOS, exit if hosted
unsafe { done() };
loop {}
@ -59,7 +59,7 @@ extern "C" fn rust_assert_called(ptr: *const core::ffi::c_char, line: core::ffi:
}
};
panic!("assertion failed at {file_name}:{}", line);
panic!("assertion failed at {file_name}:{line}");
}
#[no_mangle]