Skip to content

Commit 9546fa1

Browse files
MaxVerevkinoverdrivenpotato
authored andcommitted
std main() support
1 parent ac224b6 commit 9546fa1

File tree

1 file changed

+56
-31
lines changed

1 file changed

+56
-31
lines changed

psp/src/lib.rs

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// For unwinding support
1414
#![feature(std_internals, panic_info_message, panic_internals, c_unwind)]
1515
#![cfg_attr(not(feature = "stub-only"), feature(panic_unwind))]
16+
#![cfg_attr(feature = "std", feature(psp_std))]
1617
// For the `const_generics` feature.
1718
#![allow(incomplete_features)]
1819
#![cfg_attr(not(feature = "std"), no_std)]
@@ -115,6 +116,58 @@ core::arch::global_asm!(
115116
"#
116117
);
117118

119+
#[cfg(feature = "std")]
120+
extern "C" {
121+
#[link_name = "main"]
122+
#[doc(hidden)]
123+
pub fn c_main(argc: isize, argv: *const *const u8) -> isize;
124+
}
125+
126+
// Because code generated by `module!()` lives in user's crate, cfg attribute cannot be used there.
127+
// Hence this macro.
128+
#[cfg(feature = "std")]
129+
#[doc(hidden)]
130+
#[macro_export]
131+
macro_rules! _start {
132+
($_:expr, $argc:expr, $argv:expr) => {
133+
unsafe { $crate::c_main($argc as _, $argv as _) as _ }
134+
};
135+
}
136+
#[cfg(not(feature = "std"))]
137+
#[doc(hidden)]
138+
#[macro_export]
139+
macro_rules! _start {
140+
($psp_main:expr, $argc:expr, $argv:expr) => {{
141+
unsafe fn init_cwd(arg0: *mut u8) {
142+
let mut len = 0;
143+
while *arg0.add(len) != 0 {
144+
len += 1;
145+
}
146+
147+
// Truncate until last '/'
148+
while len > 0 && *arg0.add(len - 1) != b'/' {
149+
len -= 1;
150+
}
151+
152+
if len > 0 {
153+
let tmp = *arg0.add(len);
154+
*arg0.add(len) = 0;
155+
$crate::sys::sceIoChdir(arg0 as *const u8);
156+
*arg0.add(len) = tmp;
157+
}
158+
}
159+
160+
if $argc > 0 {
161+
unsafe { init_cwd($argv as *mut u8) };
162+
}
163+
164+
// TODO: Maybe print any error to debug screen?
165+
let _ = $crate::catch_unwind($psp_main);
166+
167+
0
168+
}};
169+
}
170+
118171
/// Declare a PSP module.
119172
///
120173
/// You must also define a `fn psp_main() { ... }` function in conjunction with
@@ -177,36 +230,8 @@ macro_rules! module {
177230

178231
#[no_mangle]
179232
extern "C" fn module_start(argc_bytes: usize, argv: *mut c_void) -> isize {
180-
use $crate::sys::ThreadAttributes;
181-
182-
unsafe fn init_cwd(arg0: *mut u8) {
183-
let mut len = 0;
184-
while *arg0.add(len) != 0 {
185-
len += 1;
186-
}
187-
188-
// Truncate until last '/'
189-
while len > 0 && *arg0.add(len - 1) != b'/' {
190-
len -= 1;
191-
}
192-
193-
if len > 0 {
194-
let tmp = *arg0.add(len);
195-
*arg0.add(len) = 0;
196-
$crate::sys::sceIoChdir(arg0 as *const u8);
197-
*arg0.add(len) = tmp;
198-
}
199-
}
200-
201-
extern "C" fn main_thread(argc_bytes: usize, argv: *mut c_void) -> i32 {
202-
if argc_bytes > 0 {
203-
unsafe { init_cwd(argv as *mut u8) };
204-
}
205-
206-
// TODO: Maybe print any error to debug screen?
207-
let _ = $crate::catch_unwind(super::psp_main);
208-
209-
0
233+
extern "C" fn main_thread(argc: usize, argv: *mut c_void) -> i32 {
234+
$crate::_start!(super::psp_main, argc, argv)
210235
}
211236

212237
unsafe {
@@ -217,7 +242,7 @@ macro_rules! module {
217242
32,
218243
// 256kb stack
219244
256 * 1024,
220-
ThreadAttributes::USER | ThreadAttributes::VFPU,
245+
$crate::sys::ThreadAttributes::USER | $crate::sys::ThreadAttributes::VFPU,
221246
core::ptr::null_mut(),
222247
);
223248

0 commit comments

Comments
 (0)