Skip to content

Commit 2eff74f

Browse files
committed
Slightly improve assembly for init and new message calls
Previously, we were calling `sel!` before the inner expression, which increases stack pressure because the compiler doesn't know it's allowed to defer loading the selector until just before it needs it.
1 parent 8c23de1 commit 2eff74f

File tree

11 files changed

+61
-66
lines changed

11 files changed

+61
-66
lines changed

crates/objc2/src/macros/mod.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,9 +1188,12 @@ macro_rules! msg_send_bool {
11881188
#[macro_export]
11891189
macro_rules! msg_send_id {
11901190
[$obj:expr, new $(,)?] => ({
1191-
let sel = $crate::sel!(new);
11921191
let result;
1193-
result = <$crate::__macro_helpers::New as $crate::__macro_helpers::MsgSendId<_, _>>::send_message_id($obj, sel, ());
1192+
result = <$crate::__macro_helpers::New as $crate::__macro_helpers::MsgSendId<_, _>>::send_message_id(
1193+
$obj,
1194+
$crate::sel!(new),
1195+
(),
1196+
);
11941197
result
11951198
});
11961199
[$obj:expr, alloc $(,)?] => ({
@@ -1199,9 +1202,12 @@ macro_rules! msg_send_id {
11991202
result
12001203
});
12011204
[$obj:expr, init $(,)?] => ({
1202-
let sel = $crate::sel!(init);
12031205
let result;
1204-
result = <$crate::__macro_helpers::Init as $crate::__macro_helpers::MsgSendId<_, _>>::send_message_id($obj, sel, ());
1206+
result = <$crate::__macro_helpers::Init as $crate::__macro_helpers::MsgSendId<_, _>>::send_message_id(
1207+
$obj,
1208+
$crate::sel!(init),
1209+
(),
1210+
);
12051211
result
12061212
});
12071213
[$obj:expr, $($selector_and_arguments:tt)+] => {

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,16 @@ Lloh82:
341341
.globl _get_obj
342342
.p2align 2
343343
_get_obj:
344-
stp x20, x19, [sp, #-32]!
345-
stp x29, x30, [sp, #16]
346-
add x29, sp, #16
344+
stp x29, x30, [sp, #-16]!
345+
mov x29, sp
346+
bl _get_class
347347
Lloh83:
348348
adrp x8, L_OBJC_SELECTOR_REFERENCES_new@GOTPAGE
349349
Lloh84:
350350
ldr x8, [x8, L_OBJC_SELECTOR_REFERENCES_new@GOTPAGEOFF]
351351
Lloh85:
352-
ldr x19, [x8]
353-
bl _get_class
354-
mov x1, x19
355-
ldp x29, x30, [sp, #16]
356-
ldp x20, x19, [sp], #32
352+
ldr x1, [x8]
353+
ldp x29, x30, [sp], #16
357354
b _objc_msgSend
358355
.loh AdrpLdrGotLdr Lloh83, Lloh84, Lloh85
359356

crates/test-assembly/crates/test_declare_class/expected/apple-armv7s.s

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -302,16 +302,15 @@ LPC3_3:
302302
.p2align 2
303303
.code 32
304304
_get_obj:
305-
push {r4, r7, lr}
306-
add r7, sp, #4
307-
movw r0, :lower16:(LL_OBJC_SELECTOR_REFERENCES_new$non_lazy_ptr-(LPC4_0+8))
308-
movt r0, :upper16:(LL_OBJC_SELECTOR_REFERENCES_new$non_lazy_ptr-(LPC4_0+8))
309-
LPC4_0:
310-
ldr r0, [pc, r0]
311-
ldr r4, [r0]
305+
push {r7, lr}
306+
mov r7, sp
312307
bl _get_class
313-
mov r1, r4
314-
pop {r4, r7, lr}
308+
movw r1, :lower16:(LL_OBJC_SELECTOR_REFERENCES_new$non_lazy_ptr-(LPC4_0+8))
309+
movt r1, :upper16:(LL_OBJC_SELECTOR_REFERENCES_new$non_lazy_ptr-(LPC4_0+8))
310+
LPC4_0:
311+
ldr r1, [pc, r1]
312+
ldr r1, [r1]
313+
pop {r7, lr}
315314
b _objc_msgSend
316315

317316
.globl _access_ivars

crates/test-assembly/crates/test_declare_class/expected/apple-old-x86.s

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,11 @@ _get_obj:
260260
push eax
261261
call L4$pb
262262
L4$pb:
263-
pop eax
264-
mov eax, dword ptr [eax + LL_OBJC_SELECTOR_REFERENCES_new$non_lazy_ptr-L4$pb]
265-
mov esi, dword ptr [eax]
263+
pop esi
266264
call _get_class
265+
mov ecx, dword ptr [esi + LL_OBJC_SELECTOR_REFERENCES_new$non_lazy_ptr-L4$pb]
267266
sub esp, 8
268-
push esi
267+
push dword ptr [ecx]
269268
push eax
270269
call _objc_msgSend
271270
add esp, 20

crates/test-assembly/crates/test_declare_class/expected/apple-x86.s

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,11 @@ _get_obj:
260260
push eax
261261
call L4$pb
262262
L4$pb:
263-
pop eax
264-
mov eax, dword ptr [eax + LL_OBJC_SELECTOR_REFERENCES_new$non_lazy_ptr-L4$pb]
265-
mov esi, dword ptr [eax]
263+
pop esi
266264
call _get_class
265+
mov ecx, dword ptr [esi + LL_OBJC_SELECTOR_REFERENCES_new$non_lazy_ptr-L4$pb]
267266
sub esp, 8
268-
push esi
267+
push dword ptr [ecx]
269268
push eax
270269
call _objc_msgSend
271270
add esp, 20

crates/test-assembly/crates/test_declare_class/expected/apple-x86_64.s

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,10 @@ LBB3_4:
187187
_get_obj:
188188
push rbp
189189
mov rbp, rsp
190-
push rbx
191-
push rax
192-
mov rax, qword ptr [rip + L_OBJC_SELECTOR_REFERENCES_new@GOTPCREL]
193-
mov rbx, qword ptr [rax]
194190
call _get_class
191+
mov rcx, qword ptr [rip + L_OBJC_SELECTOR_REFERENCES_new@GOTPCREL]
192+
mov rsi, qword ptr [rcx]
195193
mov rdi, rax
196-
mov rsi, rbx
197-
add rsp, 8
198-
pop rbx
199194
pop rbp
200195
jmp _objc_msgSend
201196

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ _handle_alloc_init:
1515
stp x20, x19, [sp, #-32]!
1616
stp x29, x30, [sp, #16]
1717
add x29, sp, #16
18+
bl _objc_alloc
19+
mov x19, x0
1820
Lloh2:
1921
adrp x8, L_OBJC_SELECTOR_REFERENCES_init@GOTPAGE
2022
Lloh3:
2123
ldr x8, [x8, L_OBJC_SELECTOR_REFERENCES_init@GOTPAGEOFF]
2224
Lloh4:
23-
ldr x19, [x8]
24-
bl _objc_alloc
25-
mov x20, x0
26-
mov x1, x19
25+
ldr x20, [x8]
26+
mov x1, x20
2727
bl _objc_msgSend
2828
cbz x0, LBB1_2
2929
ldp x29, x30, [sp, #16]
@@ -34,8 +34,8 @@ Lloh5:
3434
adrp x2, l_anon.[ID].1@PAGE
3535
Lloh6:
3636
add x2, x2, l_anon.[ID].1@PAGEOFF
37-
mov x0, x20
38-
mov x1, x19
37+
mov x0, x19
38+
mov x1, x20
3939
bl SYM(<objc2::__macro_helpers::method_family::RetainSemantics<3_u8> as objc2::__macro_helpers::msg_send_id::MsgSendIdFailed>::failed::GENERATED_ID, 0)
4040
.loh AdrpLdrGotLdr Lloh2, Lloh3, Lloh4
4141
.loh AdrpAdd Lloh5, Lloh6

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ LPC0_0:
1616
_handle_alloc_init:
1717
push {r4, r5, r7, lr}
1818
add r7, sp, #8
19+
bl _objc_alloc
1920
movw r1, :lower16:(LL_OBJC_SELECTOR_REFERENCES_init$non_lazy_ptr-(LPC1_0+8))
2021
movt r1, :upper16:(LL_OBJC_SELECTOR_REFERENCES_init$non_lazy_ptr-(LPC1_0+8))
2122
LPC1_0:
2223
ldr r1, [pc, r1]
23-
ldr r4, [r1]
24-
bl _objc_alloc
2524
mov r5, r0
25+
ldr r4, [r1]
2626
mov r1, r4
2727
bl _objc_msgSend
2828
cmp r0, #0

crates/test-assembly/crates/test_msg_send_static_sel/expected/apple-old-x86.s

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@ _handle_alloc_init:
2828
sub esp, 12
2929
call L1$pb
3030
L1$pb:
31-
pop ebx
32-
mov eax, dword ptr [ebx + LL_OBJC_SELECTOR_REFERENCES_init$non_lazy_ptr-L1$pb]
33-
mov edi, dword ptr [eax]
34-
mov eax, dword ptr [ebx + LL_OBJC_SELECTOR_REFERENCES_alloc$non_lazy_ptr-L1$pb]
31+
pop edi
32+
mov eax, dword ptr [edi + LL_OBJC_SELECTOR_REFERENCES_alloc$non_lazy_ptr-L1$pb]
3533
sub esp, 8
3634
push dword ptr [eax]
3735
push dword ptr [ebp + 8]
3836
call _objc_msgSend
3937
add esp, 16
4038
mov esi, eax
39+
mov eax, dword ptr [edi + LL_OBJC_SELECTOR_REFERENCES_init$non_lazy_ptr-L1$pb]
40+
mov ebx, dword ptr [eax]
4141
sub esp, 8
42-
push edi
43-
push eax
42+
push ebx
43+
push esi
4444
call _objc_msgSend
4545
add esp, 16
4646
test eax, eax
@@ -53,9 +53,9 @@ L1$pb:
5353
ret
5454
LBB1_2:
5555
sub esp, 4
56-
lea eax, [ebx + l_anon.[ID].1-L1$pb]
56+
lea eax, [edi + l_anon.[ID].1-L1$pb]
5757
push eax
58-
push edi
58+
push ebx
5959
push esi
6060
call SYM(<objc2::__macro_helpers::method_family::RetainSemantics<3_u8> as objc2::__macro_helpers::msg_send_id::MsgSendIdFailed>::failed::GENERATED_ID, 0)
6161

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,17 @@ _handle_alloc_init:
2828
sub esp, 12
2929
call L1$pb
3030
L1$pb:
31-
pop ebx
32-
mov eax, dword ptr [ebx + LL_OBJC_SELECTOR_REFERENCES_init$non_lazy_ptr-L1$pb]
33-
mov edi, dword ptr [eax]
31+
pop edi
3432
sub esp, 12
3533
push dword ptr [ebp + 8]
3634
call _objc_alloc
3735
add esp, 16
3836
mov esi, eax
37+
mov eax, dword ptr [edi + LL_OBJC_SELECTOR_REFERENCES_init$non_lazy_ptr-L1$pb]
38+
mov ebx, dword ptr [eax]
3939
sub esp, 8
40-
push edi
41-
push eax
40+
push ebx
41+
push esi
4242
call _objc_msgSend
4343
add esp, 16
4444
test eax, eax
@@ -51,9 +51,9 @@ L1$pb:
5151
ret
5252
LBB1_2:
5353
sub esp, 4
54-
lea eax, [ebx + l_anon.[ID].1-L1$pb]
54+
lea eax, [edi + l_anon.[ID].1-L1$pb]
5555
push eax
56-
push edi
56+
push ebx
5757
push esi
5858
call SYM(<objc2::__macro_helpers::method_family::RetainSemantics<3_u8> as objc2::__macro_helpers::msg_send_id::MsgSendIdFailed>::failed::GENERATED_ID, 0)
5959

0 commit comments

Comments
 (0)