Skip to content

Commit 7f59978

Browse files
committed
Move panic message print routine into basm-std
1 parent 182ad3e commit 7f59978

File tree

2 files changed

+26
-26
lines changed

2 files changed

+26
-26
lines changed

basm-std/src/platform/codegen.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,4 +276,27 @@ pub unsafe extern "win64" fn __chkstk() -> ! {
276276
"ret",
277277
options(noreturn)
278278
);
279+
}
280+
281+
pub unsafe fn print_panicinfo_and_exit(_pi: &core::panic::PanicInfo) -> ! {
282+
use alloc::string::ToString;
283+
use crate::platform::services::write_stdio;
284+
write_stdio(2, _pi.to_string().as_bytes());
285+
write_stdio(2, b"\n");
286+
287+
// Rust sets an exit code of 101 when the process panicked.
288+
// Hence, we follow that practice for maximum compatibility.
289+
// Reference: https://rust-cli.github.io/book/in-depth/exit-code.html
290+
#[cfg(all(windows, target_arch = "x86_64"))]
291+
{
292+
extern "win64" {
293+
fn ExitProcess(uExitCode: u32) -> !;
294+
}
295+
ExitProcess(101)
296+
}
297+
#[cfg(target_os = "linux")] {
298+
crate::platform::os::linux::syscall::exit_group(101)
299+
}
300+
#[cfg(not(any(all(windows, target_arch = "x86_64"), target_os = "linux")))]
301+
core::hint::unreachable_unchecked()
279302
}

basm/src/bin/lang_items.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,34 +34,11 @@ mod runtime {
3434
unsafe { core::hint::unreachable_unchecked() }
3535
}
3636

37-
#[cfg(all(not(test), feature = "submit"))]
3837
#[panic_handler]
3938
fn panic(_pi: &core::panic::PanicInfo) -> ! {
40-
unsafe { core::hint::unreachable_unchecked() }
41-
}
42-
43-
#[cfg(all(not(test), not(feature = "submit")))]
44-
#[panic_handler]
45-
fn panic(_pi: &core::panic::PanicInfo) -> ! {
46-
use alloc::string::ToString;
47-
use basm::platform::services::write_stdio;
48-
write_stdio(2, _pi.to_string().as_bytes());
49-
write_stdio(2, b"\n");
50-
51-
// Rust sets an exit code of 101 when the process panicked.
52-
// Hence, we follow that practice for maximum compatibility.
53-
// Reference: https://rust-cli.github.io/book/in-depth/exit-code.html
54-
#[cfg(all(windows, target_arch = "x86_64"))]
55-
{
56-
extern "win64" {
57-
fn ExitProcess(uExitCode: u32) -> !;
58-
}
59-
unsafe { ExitProcess(101); }
60-
}
61-
#[cfg(target_os = "linux")] {
62-
unsafe { basm::platform::os::linux::syscall::exit_group(101); }
63-
}
64-
#[cfg(not(any(all(windows, target_arch = "x86_64"), target_os = "linux")))]
39+
#[cfg(not(feature = "submit"))]
40+
unsafe { basm::platform::codegen::print_panicinfo_and_exit(_pi) }
41+
#[cfg(feature = "submit")]
6542
unsafe { core::hint::unreachable_unchecked() }
6643
}
6744
}

0 commit comments

Comments
 (0)