|
| 1 | +//! Optimized versions of `objc_retain` and `objc_release`. |
| 2 | +//! |
| 3 | +//! On macOS 13.0 / iOS 16.0 / tvOS 16.0 / watchOS 9.0, on ARM64, optimized |
| 4 | +//! versions of these two functions that use a different calling convention |
| 5 | +//! than the usual C calling convention, are available. |
| 6 | +//! |
| 7 | +//! Specifically, the expected input register is changed. The output register |
| 8 | +//! is unchanged. |
| 9 | +//! |
| 10 | +//! As an example, if the object is stored in the `x19` register and we need |
| 11 | +//! to release it, we usually end up emitting an extra `mov` to get the object |
| 12 | +//! into the `x0` register first, as expected by the C calling convention: |
| 13 | +//! |
| 14 | +//! ```asm |
| 15 | +//! mov x0, x19 |
| 16 | +//! bl _objc_release |
| 17 | +//! ``` |
| 18 | +//! |
| 19 | +//! With this optimization though, since the expected register is encoded in |
| 20 | +//! the name of the function instead, we can avoid the move altogether. |
| 21 | +//! |
| 22 | +//! ```asm |
| 23 | +//! bl _objc_release_x19 |
| 24 | +//! ``` |
| 25 | +//! |
| 26 | +//! |
| 27 | +//! |
| 28 | +//! Safety of our two uses of the `asm!` macro: |
| 29 | +//! |
| 30 | +//! 1. We use the register class `reg`, with the modifier `x`, which on |
| 31 | +//! Aarch64 is defined as `x[0-30]`, see [this][asm-reg-cls]. |
| 32 | +//! |
| 33 | +//! The functions are only available in the variants `x[0-15]` and |
| 34 | +//! `x[19-28]` though, see [this][objc4-source], so if the register |
| 35 | +//! allocator ends up using `x16`, `x17`, `x18`, `x29` or `x30`, we will |
| 36 | +//! emit a call to e.g. `objc_retain_x29`, which will fail at link time. |
| 37 | +//! |
| 38 | +//! TODO: Before this option can be stable, we need a way to prevent that! |
| 39 | +//! |
| 40 | +//! 2. We use the `clobber_abi("C")` since we're effectively calling a C |
| 41 | +//! C function. |
| 42 | +//! |
| 43 | +//! [asm-reg-cls]: https://doc.rust-lang.org/nightly/reference/inline-assembly.html#register-operands |
| 44 | +//! [objc4-source]: https://github.com/apple-oss-distributions/objc4/blob/objc4-866.9/runtime/objc-abi.h#L442-L498 |
| 45 | +use crate::ffi; |
| 46 | + |
| 47 | +/// A potentially faster version of `ffi::objc_retain`. |
| 48 | +/// |
| 49 | +/// |
| 50 | +/// # Safety |
| 51 | +/// |
| 52 | +/// Same as `ffi::objc_retain`. |
| 53 | +#[inline] |
| 54 | +pub(crate) unsafe fn objc_retain_fast(obj: *mut ffi::objc_object) -> *mut ffi::objc_object { |
| 55 | + #[cfg(all(feature = "unstable-apple-new", target_arch = "aarch64"))] |
| 56 | + // SAFETY: See the file header. |
| 57 | + // |
| 58 | + // As per the ARM64 calling convention, the return value is put in `x0`. |
| 59 | + // |
| 60 | + // That the function itself is safe to call is upheld by the caller. |
| 61 | + unsafe { |
| 62 | + let result; |
| 63 | + core::arch::asm!( |
| 64 | + "bl _objc_retain_{obj:x}", |
| 65 | + obj = in(reg) obj, |
| 66 | + lateout("x0") result, |
| 67 | + clobber_abi("C"), |
| 68 | + ); |
| 69 | + result |
| 70 | + } |
| 71 | + |
| 72 | + #[cfg(not(all(feature = "unstable-apple-new", target_arch = "aarch64")))] |
| 73 | + // SAFETY: Upheld by caller. |
| 74 | + unsafe { |
| 75 | + ffi::objc_retain(obj) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// A potentially faster version of `ffi::objc_release`. |
| 80 | +/// |
| 81 | +/// |
| 82 | +/// # Safety |
| 83 | +/// |
| 84 | +/// Same as `ffi::objc_release`. |
| 85 | +#[inline] |
| 86 | +pub(crate) unsafe fn objc_release_fast(obj: *mut ffi::objc_object) { |
| 87 | + #[cfg(all(feature = "unstable-apple-new", target_arch = "aarch64"))] |
| 88 | + // SAFETY: See the file header. |
| 89 | + // |
| 90 | + // That the function itself is safe to call is upheld by the caller. |
| 91 | + unsafe { |
| 92 | + core::arch::asm!( |
| 93 | + "bl _objc_release_{obj:x}", |
| 94 | + obj = in(reg) obj, |
| 95 | + clobber_abi("C"), |
| 96 | + ) |
| 97 | + } |
| 98 | + |
| 99 | + #[cfg(not(all(feature = "unstable-apple-new", target_arch = "aarch64")))] |
| 100 | + // SAFETY: Upheld by caller. |
| 101 | + unsafe { |
| 102 | + ffi::objc_release(obj) |
| 103 | + } |
| 104 | +} |
0 commit comments