rt/abi.rs
1// abi -- the calling convention of plugins and loadable modules (docs/ARM64.md, decision 4)
2//
3// rt::plugin_abi!(fn(args) -> ret) the type of such a function pointer
4// rt::plugin_abi! { #[attrs] pub fn name(args) -> ret { body } } such a function
5//
6// x86-64 keeps the Win64 convention on every system (extern "win64": the plugins and modules
7// built so far stay loadable, and a record holds the same pointers on Windows and Linux); every
8// other processor takes the system's C convention (extern "C"). On ARM64 that is AAPCS64 on
9// Linux, Android and Apple and Microsoft's ARM64 convention on Windows: for the integer and
10// pointer arguments and results of these ABIs the two pass the same registers (x0-x7, the
11// result in x0 / x1), Windows only keeps x18 for itself, which code from rustc never touches.
12// A plugin or module is built per processor and system anyway (docs/ARM64.md, decision 4: an
13// x86-64 file is refused on ARM64 with LIBE_ARCH and the other way round).
14//
15// Used by plat (plib.rs, modhost.rs, modabi.rs), the module runtime modules/modrt (a copy of
16// this file in its shared/), the modules and the plugins' hosts (JFPainter). A function the
17// host runs under plat::guard_call must have this convention.
18
19/// The calling convention of plugins and modules: `extern "win64"` on x86-64, `extern "C"`
20/// elsewhere (docs/ARM64.md, decision 4). `plugin_abi!(fn(u32) -> u32)` is the type of such a
21/// function pointer; `plugin_abi! { pub fn f(a: u32) -> u32 { a } }` defines such a function
22/// (attributes, visibility, `unsafe`, generics and the body pass through unchanged).
23#[cfg(target_arch = "x86_64")]
24#[macro_export]
25macro_rules! plugin_abi {
26 (fn ($($a:tt)*) $($r:tt)*) => { extern "win64" fn($($a)*) $($r)* };
27 (unsafe fn ($($a:tt)*) $($r:tt)*) => { unsafe extern "win64" fn($($a)*) $($r)* };
28 ($(#[$($m:tt)*])* $v:vis unsafe fn $($i:tt)*) => { $(#[$($m)*])* $v unsafe extern "win64" fn $($i)* };
29 ($(#[$($m:tt)*])* $v:vis fn $($i:tt)*) => { $(#[$($m)*])* $v extern "win64" fn $($i)* };
30}
31
32/// The calling convention of plugins and modules: `extern "win64"` on x86-64, `extern "C"`
33/// elsewhere (docs/ARM64.md, decision 4). `plugin_abi!(fn(u32) -> u32)` is the type of such a
34/// function pointer; `plugin_abi! { pub fn f(a: u32) -> u32 { a } }` defines such a function
35/// (attributes, visibility, `unsafe`, generics and the body pass through unchanged).
36#[cfg(not(target_arch = "x86_64"))]
37#[macro_export]
38macro_rules! plugin_abi {
39 (fn ($($a:tt)*) $($r:tt)*) => { extern "C" fn($($a)*) $($r)* };
40 (unsafe fn ($($a:tt)*) $($r:tt)*) => { unsafe extern "C" fn($($a)*) $($r)* };
41 ($(#[$($m:tt)*])* $v:vis unsafe fn $($i:tt)*) => { $(#[$($m)*])* $v unsafe extern "C" fn $($i)* };
42 ($(#[$($m:tt)*])* $v:vis fn $($i:tt)*) => { $(#[$($m)*])* $v extern "C" fn $($i)* };
43}