Skip to content

Commit c499a56

Browse files
authored
Merge pull request #196 from madsmtm/deduplicate-common-selectors
Deduplicate common selectors
2 parents 83cc8ac + 94fb406 commit c499a56

File tree

16 files changed

+202
-493
lines changed

16 files changed

+202
-493
lines changed

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,8 @@ members = [
1313
"test-ui",
1414
]
1515
resolver = "2"
16+
17+
[profile.assembly-tests]
18+
inherits = "release"
19+
# Enable LTO to allow testing the `unstable-static-sel-inlined` feature
20+
lto = true

objc2/src/__macro_helpers.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::__sel_inner;
12
use crate::rc::{Allocated, Id, Ownership};
23
use crate::runtime::{Class, Sel};
34
use crate::{Message, MessageArguments, MessageReceiver};
@@ -12,6 +13,31 @@ pub use core::{compile_error, concat, panic, stringify};
1213
#[cfg(feature = "objc2-proc-macros")]
1314
pub use objc2_proc_macros::__hash_idents;
1415

16+
// Common selectors.
17+
//
18+
// These are put here to deduplicate the cached selector, and when using
19+
// `unstable-static-sel`, the statics.
20+
//
21+
// Note that our assembly tests of `unstable-static-sel-inlined` output a GOT
22+
// entry for such accesses, but that is just a limitation of our tests - the
23+
// actual assembly is as one would expect.
24+
25+
#[inline]
26+
pub fn alloc() -> Sel {
27+
// SAFETY: Must have NUL byte
28+
__sel_inner!("alloc\0", uniqueIdent)
29+
}
30+
#[inline]
31+
pub fn init() -> Sel {
32+
// SAFETY: Must have NUL byte
33+
__sel_inner!("init\0", uniqueIdent)
34+
}
35+
#[inline]
36+
pub fn new() -> Sel {
37+
// SAFETY: Must have NUL byte
38+
__sel_inner!("new\0", uniqueIdent)
39+
}
40+
1541
/// Helper for specifying the retain semantics for a given selector family.
1642
///
1743
/// Note that we can't actually check if a method is in a method family; only

objc2/src/macros.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ macro_rules! class {
4545
/// # Specification
4646
///
4747
/// This has similar syntax and functionality as the `@selector` directive in
48-
/// Objective-C. This calls [`Sel::register`] internally. The result is cached
49-
/// for efficiency.
48+
/// Objective-C.
49+
///
50+
/// This calls [`Sel::register`] internally. The result is cached for
51+
/// efficiency. The cache for certain common selectors (`alloc`, `init` and
52+
/// `new`) is deduplicated to reduce code-size.
5053
///
5154
/// Non-ascii identifiers are ill-tested, if supported at all.
5255
///
@@ -142,6 +145,15 @@ macro_rules! class {
142145
/// ```
143146
#[macro_export]
144147
macro_rules! sel {
148+
(alloc) => ({
149+
$crate::__macro_helpers::alloc()
150+
});
151+
(init) => ({
152+
$crate::__macro_helpers::init()
153+
});
154+
(new) => ({
155+
$crate::__macro_helpers::new()
156+
});
145157
($first:ident $(: $($rest:ident :)*)?) => ({
146158
use $crate::__macro_helpers::{concat, stringify, str};
147159
const SELECTOR_DATA: &str = concat!(stringify!($first), $(':', $(stringify!($rest), ':',)*)? '\0');
@@ -153,7 +165,7 @@ macro_rules! sel {
153165
#[macro_export]
154166
#[cfg(not(feature = "unstable-static-sel"))]
155167
macro_rules! __sel_inner {
156-
($data:ident, $($idents:ident)+) => {{
168+
($data:expr, $($idents:ident)+) => {{
157169
use $crate::__macro_helpers::CachedSel;
158170
static CACHED_SEL: CachedSel = CachedSel::new();
159171
#[allow(unused_unsafe)]
@@ -170,10 +182,10 @@ macro_rules! __sel_inner_statics_apple_generic {
170182
$image_info_section:literal;
171183
$var_name_section:literal;
172184
$selector_ref_section:literal;
173-
$data:ident,
185+
$data:expr,
174186
$($idents:ident)+
175187
} => {
176-
use $crate::__macro_helpers::{__hash_idents, u8, UnsafeCell};
188+
use $crate::__macro_helpers::{u8, UnsafeCell};
177189
use $crate::ffi::__ImageInfo;
178190
use $crate::runtime::Sel;
179191

@@ -185,7 +197,10 @@ macro_rules! __sel_inner_statics_apple_generic {
185197
/// reports `__DATA/__objc_imageinfo has unexpectedly large size XXX`,
186198
/// but things still seems to work.
187199
#[link_section = $image_info_section]
188-
#[export_name = concat!("\x01L_OBJC_IMAGE_INFO_", __hash_idents!($($idents)+))]
200+
#[export_name = $crate::__macro_helpers::concat!(
201+
"\x01L_OBJC_IMAGE_INFO_",
202+
$crate::__macro_helpers::__hash_idents!($($idents)+),
203+
)]
189204
#[used] // Make sure this reaches the linker
190205
static _IMAGE_INFO: __ImageInfo = __ImageInfo::system();
191206

@@ -195,7 +210,10 @@ macro_rules! __sel_inner_statics_apple_generic {
195210
/// See rust-lang/rust#18297
196211
/// Should only be an optimization (?)
197212
#[link_section = $var_name_section]
198-
#[export_name = concat!("\x01L_OBJC_METH_VAR_NAME_", __hash_idents!($($idents)+))]
213+
#[export_name = $crate::__macro_helpers::concat!(
214+
"\x01L_OBJC_METH_VAR_NAME_",
215+
$crate::__macro_helpers::__hash_idents!($($idents)+),
216+
)]
199217
static NAME_DATA: [u8; X.len()] = {
200218
// Convert the `&[u8]` slice to an array with known length, so
201219
// that we can place that directly in a static.
@@ -236,7 +254,10 @@ macro_rules! __sel_inner_statics_apple_generic {
236254
/// See the [`ctor`](https://crates.io/crates/ctor) crate for more
237255
/// info on "life before main".
238256
#[link_section = $selector_ref_section]
239-
#[export_name = concat!("\x01L_OBJC_SELECTOR_REFERENCES_", __hash_idents!($($idents)+))]
257+
#[export_name = $crate::__macro_helpers::concat!(
258+
"\x01L_OBJC_SELECTOR_REFERENCES_",
259+
$crate::__macro_helpers::__hash_idents!($($idents)+),
260+
)]
240261
static mut REF: UnsafeCell<Sel> = unsafe {
241262
UnsafeCell::new(Sel::__internal_from_ptr(NAME_DATA.as_ptr().cast()))
242263
};

test-assembly/crates/test_msg_send_static_sel/expected/apple-aarch64.s

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@ _handle_alloc_init:
1616
stp x29, x30, [sp, #16]
1717
add x29, sp, #16
1818
Lloh2:
19-
adrp x8, L_OBJC_SELECTOR_REFERENCES_cb49b9ab1b00e328@PAGE
19+
adrp x8, L_OBJC_SELECTOR_REFERENCES_0ea0a15a3d108c32@GOTPAGE
2020
Lloh3:
21-
ldr x19, [x8, L_OBJC_SELECTOR_REFERENCES_cb49b9ab1b00e328@PAGEOFF]
21+
ldr x8, [x8, L_OBJC_SELECTOR_REFERENCES_0ea0a15a3d108c32@GOTPAGEOFF]
2222
Lloh4:
23-
adrp x8, L_OBJC_SELECTOR_REFERENCES_dcb825748735621d@PAGE
23+
ldr x19, [x8]
2424
Lloh5:
25-
ldr x1, [x8, L_OBJC_SELECTOR_REFERENCES_dcb825748735621d@PAGEOFF]
25+
adrp x8, L_OBJC_SELECTOR_REFERENCES_1678d2f7468155d2@GOTPAGE
26+
Lloh6:
27+
ldr x8, [x8, L_OBJC_SELECTOR_REFERENCES_1678d2f7468155d2@GOTPAGEOFF]
28+
Lloh7:
29+
ldr x1, [x8]
2630
bl _objc_msgSend
2731
mov x1, x19
2832
ldp x29, x30, [sp, #16]
2933
ldp x20, x19, [sp], #32
3034
b _objc_msgSend
31-
.loh AdrpLdr Lloh4, Lloh5
32-
.loh AdrpAdrp Lloh2, Lloh4
33-
.loh AdrpLdr Lloh2, Lloh3
35+
.loh AdrpLdrGotLdr Lloh5, Lloh6, Lloh7
36+
.loh AdrpLdrGotLdr Lloh2, Lloh3, Lloh4
3437

3538
.globl _use_generic
3639
.p2align 2
@@ -39,32 +42,32 @@ _use_generic:
3942
stp x29, x30, [sp, #16]
4043
add x29, sp, #16
4144
mov x19, x0
42-
Lloh6:
45+
Lloh8:
4346
adrp x8, L_OBJC_SELECTOR_REFERENCES_f16064a6f68ca673@PAGE
44-
Lloh7:
47+
Lloh9:
4548
ldr x1, [x8, L_OBJC_SELECTOR_REFERENCES_f16064a6f68ca673@PAGEOFF]
4649
adrp x20, L_OBJC_SELECTOR_REFERENCES_457d234345d46cbe@PAGE
4750
ldr x2, [x20, L_OBJC_SELECTOR_REFERENCES_457d234345d46cbe@PAGEOFF]
4851
bl _objc_msgSend
49-
Lloh8:
52+
Lloh10:
5053
adrp x8, L_OBJC_SELECTOR_REFERENCES_9f134b97cb598446@PAGE
51-
Lloh9:
54+
Lloh11:
5255
ldr x1, [x8, L_OBJC_SELECTOR_REFERENCES_9f134b97cb598446@PAGEOFF]
5356
ldr x2, [x20, L_OBJC_SELECTOR_REFERENCES_457d234345d46cbe@PAGEOFF]
5457
mov x0, x19
5558
bl _objc_msgSend
56-
Lloh10:
59+
Lloh12:
5760
adrp x8, L_OBJC_SELECTOR_REFERENCES_e76e01e8b2327e5d@PAGE
58-
Lloh11:
61+
Lloh13:
5962
ldr x1, [x8, L_OBJC_SELECTOR_REFERENCES_e76e01e8b2327e5d@PAGEOFF]
6063
ldr x2, [x20, L_OBJC_SELECTOR_REFERENCES_457d234345d46cbe@PAGEOFF]
6164
mov x0, x19
6265
ldp x29, x30, [sp, #16]
6366
ldp x20, x19, [sp], #32
6467
b _objc_msgSend
68+
.loh AdrpLdr Lloh12, Lloh13
6569
.loh AdrpLdr Lloh10, Lloh11
6670
.loh AdrpLdr Lloh8, Lloh9
67-
.loh AdrpLdr Lloh6, Lloh7
6871

6972
.section __DATA,__objc_imageinfo,regular,no_dead_strip
7073
.globl L_OBJC_IMAGE_INFO_044375a4329d08dc
@@ -83,40 +86,6 @@ L_OBJC_METH_VAR_NAME_044375a4329d08dc:
8386
L_OBJC_SELECTOR_REFERENCES_044375a4329d08dc:
8487
.quad L_OBJC_METH_VAR_NAME_044375a4329d08dc
8588

86-
.section __DATA,__objc_imageinfo,regular,no_dead_strip
87-
.globl L_OBJC_IMAGE_INFO_cb49b9ab1b00e328
88-
.p2align 2
89-
L_OBJC_IMAGE_INFO_cb49b9ab1b00e328:
90-
.asciz "\000\000\000\000@\000\000"
91-
92-
.section __TEXT,__objc_methname,cstring_literals
93-
.globl L_OBJC_METH_VAR_NAME_cb49b9ab1b00e328
94-
L_OBJC_METH_VAR_NAME_cb49b9ab1b00e328:
95-
.asciz "init"
96-
97-
.section __DATA,__objc_selrefs,literal_pointers,no_dead_strip
98-
.globl L_OBJC_SELECTOR_REFERENCES_cb49b9ab1b00e328
99-
.p2align 3
100-
L_OBJC_SELECTOR_REFERENCES_cb49b9ab1b00e328:
101-
.quad L_OBJC_METH_VAR_NAME_cb49b9ab1b00e328
102-
103-
.section __DATA,__objc_imageinfo,regular,no_dead_strip
104-
.globl L_OBJC_IMAGE_INFO_dcb825748735621d
105-
.p2align 2
106-
L_OBJC_IMAGE_INFO_dcb825748735621d:
107-
.asciz "\000\000\000\000@\000\000"
108-
109-
.section __TEXT,__objc_methname,cstring_literals
110-
.globl L_OBJC_METH_VAR_NAME_dcb825748735621d
111-
L_OBJC_METH_VAR_NAME_dcb825748735621d:
112-
.asciz "alloc"
113-
114-
.section __DATA,__objc_selrefs,literal_pointers,no_dead_strip
115-
.globl L_OBJC_SELECTOR_REFERENCES_dcb825748735621d
116-
.p2align 3
117-
L_OBJC_SELECTOR_REFERENCES_dcb825748735621d:
118-
.quad L_OBJC_METH_VAR_NAME_dcb825748735621d
119-
12089
.section __DATA,__objc_imageinfo,regular,no_dead_strip
12190
.globl L_OBJC_IMAGE_INFO_457d234345d46cbe
12291
.p2align 2

test-assembly/crates/test_msg_send_static_sel/expected/apple-armv7.s

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ LPC0_0:
1616
_handle_alloc_init:
1717
push {r4, r7, lr}
1818
add r7, sp, #4
19-
movw r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_dcb825748735621d-(LPC1_0+8))
20-
movt r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_dcb825748735621d-(LPC1_0+8))
19+
movw r1, :lower16:(LL_OBJC_SELECTOR_REFERENCES_0ea0a15a3d108c32$non_lazy_ptr-(LPC1_0+8))
20+
movt r1, :upper16:(LL_OBJC_SELECTOR_REFERENCES_0ea0a15a3d108c32$non_lazy_ptr-(LPC1_0+8))
21+
movw r2, :lower16:(LL_OBJC_SELECTOR_REFERENCES_1678d2f7468155d2$non_lazy_ptr-(LPC1_1+8))
22+
movt r2, :upper16:(LL_OBJC_SELECTOR_REFERENCES_1678d2f7468155d2$non_lazy_ptr-(LPC1_1+8))
2123
LPC1_0:
2224
ldr r1, [pc, r1]
23-
movw r4, :lower16:(L_OBJC_SELECTOR_REFERENCES_cb49b9ab1b00e328-(LPC1_1+8))
24-
movt r4, :upper16:(L_OBJC_SELECTOR_REFERENCES_cb49b9ab1b00e328-(LPC1_1+8))
2525
LPC1_1:
26-
ldr r4, [pc, r4]
26+
ldr r2, [pc, r2]
27+
ldr r4, [r1]
28+
ldr r1, [r2]
2729
bl _objc_msgSend
2830
mov r1, r4
2931
pop {r4, r7, lr}
@@ -84,40 +86,6 @@ L_OBJC_METH_VAR_NAME_044375a4329d08dc:
8486
L_OBJC_SELECTOR_REFERENCES_044375a4329d08dc:
8587
.long L_OBJC_METH_VAR_NAME_044375a4329d08dc
8688

87-
.section __DATA,__objc_imageinfo,regular,no_dead_strip
88-
.globl L_OBJC_IMAGE_INFO_cb49b9ab1b00e328
89-
.p2align 2
90-
L_OBJC_IMAGE_INFO_cb49b9ab1b00e328:
91-
.asciz "\000\000\000\000@\000\000"
92-
93-
.section __TEXT,__objc_methname,cstring_literals
94-
.globl L_OBJC_METH_VAR_NAME_cb49b9ab1b00e328
95-
L_OBJC_METH_VAR_NAME_cb49b9ab1b00e328:
96-
.asciz "init"
97-
98-
.section __DATA,__objc_selrefs,literal_pointers,no_dead_strip
99-
.globl L_OBJC_SELECTOR_REFERENCES_cb49b9ab1b00e328
100-
.p2align 2
101-
L_OBJC_SELECTOR_REFERENCES_cb49b9ab1b00e328:
102-
.long L_OBJC_METH_VAR_NAME_cb49b9ab1b00e328
103-
104-
.section __DATA,__objc_imageinfo,regular,no_dead_strip
105-
.globl L_OBJC_IMAGE_INFO_dcb825748735621d
106-
.p2align 2
107-
L_OBJC_IMAGE_INFO_dcb825748735621d:
108-
.asciz "\000\000\000\000@\000\000"
109-
110-
.section __TEXT,__objc_methname,cstring_literals
111-
.globl L_OBJC_METH_VAR_NAME_dcb825748735621d
112-
L_OBJC_METH_VAR_NAME_dcb825748735621d:
113-
.asciz "alloc"
114-
115-
.section __DATA,__objc_selrefs,literal_pointers,no_dead_strip
116-
.globl L_OBJC_SELECTOR_REFERENCES_dcb825748735621d
117-
.p2align 2
118-
L_OBJC_SELECTOR_REFERENCES_dcb825748735621d:
119-
.long L_OBJC_METH_VAR_NAME_dcb825748735621d
120-
12189
.section __DATA,__objc_imageinfo,regular,no_dead_strip
12290
.globl L_OBJC_IMAGE_INFO_457d234345d46cbe
12391
.p2align 2
@@ -186,4 +154,13 @@ L_OBJC_METH_VAR_NAME_e76e01e8b2327e5d:
186154
L_OBJC_SELECTOR_REFERENCES_e76e01e8b2327e5d:
187155
.long L_OBJC_METH_VAR_NAME_e76e01e8b2327e5d
188156

157+
.section __DATA,__nl_symbol_ptr,non_lazy_symbol_pointers
158+
.p2align 2
159+
LL_OBJC_SELECTOR_REFERENCES_0ea0a15a3d108c32$non_lazy_ptr:
160+
.indirect_symbol L_OBJC_SELECTOR_REFERENCES_0ea0a15a3d108c32
161+
.long 0
162+
LL_OBJC_SELECTOR_REFERENCES_1678d2f7468155d2$non_lazy_ptr:
163+
.indirect_symbol L_OBJC_SELECTOR_REFERENCES_1678d2f7468155d2
164+
.long 0
165+
189166
.subsections_via_symbols

0 commit comments

Comments
 (0)