]> git.saurik.com Git - apple/objc4.git/blob - runtime/Messengers.subproj/objc-msg-arm64.s
objc4-756.2.tar.gz
[apple/objc4.git] / runtime / Messengers.subproj / objc-msg-arm64.s
1 /*
2 * @APPLE_LICENSE_HEADER_START@
3 *
4 * Copyright (c) 2011 Apple Inc. All Rights Reserved.
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /********************************************************************
24 *
25 * objc-msg-arm64.s - ARM64 code to support objc messaging
26 *
27 ********************************************************************/
28
29 #ifdef __arm64__
30
31 #include <arm/arch.h>
32 #include "isa.h"
33 #include "arm64-asm.h"
34
35 .data
36
37 // _objc_entryPoints and _objc_exitPoints are used by method dispatch
38 // caching code to figure out whether any threads are actively
39 // in the cache for dispatching. The labels surround the asm code
40 // that do cache lookups. The tables are zero-terminated.
41
42 .align 4
43 .private_extern _objc_entryPoints
44 _objc_entryPoints:
45 PTR _cache_getImp
46 PTR _objc_msgSend
47 PTR _objc_msgSendSuper
48 PTR _objc_msgSendSuper2
49 PTR _objc_msgLookup
50 PTR _objc_msgLookupSuper2
51 PTR 0
52
53 .private_extern _objc_exitPoints
54 _objc_exitPoints:
55 PTR LExit_cache_getImp
56 PTR LExit_objc_msgSend
57 PTR LExit_objc_msgSendSuper
58 PTR LExit_objc_msgSendSuper2
59 PTR LExit_objc_msgLookup
60 PTR LExit_objc_msgLookupSuper2
61 PTR 0
62
63
64 /* objc_super parameter to sendSuper */
65 #define RECEIVER 0
66 #define CLASS __SIZEOF_POINTER__
67
68 /* Selected field offsets in class structure */
69 #define SUPERCLASS __SIZEOF_POINTER__
70 #define CACHE (2 * __SIZEOF_POINTER__)
71
72 /* Selected field offsets in method structure */
73 #define METHOD_NAME 0
74 #define METHOD_TYPES __SIZEOF_POINTER__
75 #define METHOD_IMP (2 * __SIZEOF_POINTER__)
76
77 #define BUCKET_SIZE (2 * __SIZEOF_POINTER__)
78
79
80 /********************************************************************
81 * GetClassFromIsa_p16 src
82 * src is a raw isa field. Sets p16 to the corresponding class pointer.
83 * The raw isa might be an indexed isa to be decoded, or a
84 * packed isa that needs to be masked.
85 *
86 * On exit:
87 * $0 is unchanged
88 * p16 is a class pointer
89 * x10 is clobbered
90 ********************************************************************/
91
92 #if SUPPORT_INDEXED_ISA
93 .align 3
94 .globl _objc_indexed_classes
95 _objc_indexed_classes:
96 .fill ISA_INDEX_COUNT, PTRSIZE, 0
97 #endif
98
99 .macro GetClassFromIsa_p16 /* src */
100
101 #if SUPPORT_INDEXED_ISA
102 // Indexed isa
103 mov p16, $0 // optimistically set dst = src
104 tbz p16, #ISA_INDEX_IS_NPI_BIT, 1f // done if not non-pointer isa
105 // isa in p16 is indexed
106 adrp x10, _objc_indexed_classes@PAGE
107 add x10, x10, _objc_indexed_classes@PAGEOFF
108 ubfx p16, p16, #ISA_INDEX_SHIFT, #ISA_INDEX_BITS // extract index
109 ldr p16, [x10, p16, UXTP #PTRSHIFT] // load class from array
110 1:
111
112 #elif __LP64__
113 // 64-bit packed isa
114 and p16, $0, #ISA_MASK
115
116 #else
117 // 32-bit raw isa
118 mov p16, $0
119
120 #endif
121
122 .endmacro
123
124
125 /********************************************************************
126 * ENTRY functionName
127 * STATIC_ENTRY functionName
128 * END_ENTRY functionName
129 ********************************************************************/
130
131 .macro ENTRY /* name */
132 .text
133 .align 5
134 .globl $0
135 $0:
136 .endmacro
137
138 .macro STATIC_ENTRY /*name*/
139 .text
140 .align 5
141 .private_extern $0
142 $0:
143 .endmacro
144
145 .macro END_ENTRY /* name */
146 LExit$0:
147 .endmacro
148
149
150 /********************************************************************
151 * UNWIND name, flags
152 * Unwind info generation
153 ********************************************************************/
154 .macro UNWIND
155 .section __LD,__compact_unwind,regular,debug
156 PTR $0
157 .set LUnwind$0, LExit$0 - $0
158 .long LUnwind$0
159 .long $1
160 PTR 0 /* no personality */
161 PTR 0 /* no LSDA */
162 .text
163 .endmacro
164
165 #define NoFrame 0x02000000 // no frame, no SP adjustment
166 #define FrameWithNoSaves 0x04000000 // frame, no non-volatile saves
167
168
169 /********************************************************************
170 *
171 * CacheLookup NORMAL|GETIMP|LOOKUP
172 *
173 * Locate the implementation for a selector in a class method cache.
174 *
175 * Takes:
176 * x1 = selector
177 * x16 = class to be searched
178 *
179 * Kills:
180 * x9,x10,x11,x12, x17
181 *
182 * On exit: (found) calls or returns IMP
183 * with x16 = class, x17 = IMP
184 * (not found) jumps to LCacheMiss
185 *
186 ********************************************************************/
187
188 #define NORMAL 0
189 #define GETIMP 1
190 #define LOOKUP 2
191
192 // CacheHit: x17 = cached IMP, x12 = address of cached IMP, x1 = SEL
193 .macro CacheHit
194 .if $0 == NORMAL
195 TailCallCachedImp x17, x12, x1 // authenticate and call imp
196 .elseif $0 == GETIMP
197 mov p0, p17
198 cbz p0, 9f // don't ptrauth a nil imp
199 AuthAndResignAsIMP x0, x12, x1 // authenticate imp and re-sign as IMP
200 9: ret // return IMP
201 .elseif $0 == LOOKUP
202 // No nil check for ptrauth: the caller would crash anyway when they
203 // jump to a nil IMP. We don't care if that jump also fails ptrauth.
204 AuthAndResignAsIMP x17, x12, x1 // authenticate imp and re-sign as IMP
205 ret // return imp via x17
206 .else
207 .abort oops
208 .endif
209 .endmacro
210
211 .macro CheckMiss
212 // miss if bucket->sel == 0
213 .if $0 == GETIMP
214 cbz p9, LGetImpMiss
215 .elseif $0 == NORMAL
216 cbz p9, __objc_msgSend_uncached
217 .elseif $0 == LOOKUP
218 cbz p9, __objc_msgLookup_uncached
219 .else
220 .abort oops
221 .endif
222 .endmacro
223
224 .macro JumpMiss
225 .if $0 == GETIMP
226 b LGetImpMiss
227 .elseif $0 == NORMAL
228 b __objc_msgSend_uncached
229 .elseif $0 == LOOKUP
230 b __objc_msgLookup_uncached
231 .else
232 .abort oops
233 .endif
234 .endmacro
235
236 .macro CacheLookup
237 // p1 = SEL, p16 = isa
238 ldp p10, p11, [x16, #CACHE] // p10 = buckets, p11 = occupied|mask
239 #if !__LP64__
240 and w11, w11, 0xffff // p11 = mask
241 #endif
242 and w12, w1, w11 // x12 = _cmd & mask
243 add p12, p10, p12, LSL #(1+PTRSHIFT)
244 // p12 = buckets + ((_cmd & mask) << (1+PTRSHIFT))
245
246 ldp p17, p9, [x12] // {imp, sel} = *bucket
247 1: cmp p9, p1 // if (bucket->sel != _cmd)
248 b.ne 2f // scan more
249 CacheHit $0 // call or return imp
250
251 2: // not hit: p12 = not-hit bucket
252 CheckMiss $0 // miss if bucket->sel == 0
253 cmp p12, p10 // wrap if bucket == buckets
254 b.eq 3f
255 ldp p17, p9, [x12, #-BUCKET_SIZE]! // {imp, sel} = *--bucket
256 b 1b // loop
257
258 3: // wrap: p12 = first bucket, w11 = mask
259 add p12, p12, w11, UXTW #(1+PTRSHIFT)
260 // p12 = buckets + (mask << 1+PTRSHIFT)
261
262 // Clone scanning loop to miss instead of hang when cache is corrupt.
263 // The slow path may detect any corruption and halt later.
264
265 ldp p17, p9, [x12] // {imp, sel} = *bucket
266 1: cmp p9, p1 // if (bucket->sel != _cmd)
267 b.ne 2f // scan more
268 CacheHit $0 // call or return imp
269
270 2: // not hit: p12 = not-hit bucket
271 CheckMiss $0 // miss if bucket->sel == 0
272 cmp p12, p10 // wrap if bucket == buckets
273 b.eq 3f
274 ldp p17, p9, [x12, #-BUCKET_SIZE]! // {imp, sel} = *--bucket
275 b 1b // loop
276
277 3: // double wrap
278 JumpMiss $0
279
280 .endmacro
281
282
283 /********************************************************************
284 *
285 * id objc_msgSend(id self, SEL _cmd, ...);
286 * IMP objc_msgLookup(id self, SEL _cmd, ...);
287 *
288 * objc_msgLookup ABI:
289 * IMP returned in x17
290 * x16 reserved for our use but not used
291 *
292 ********************************************************************/
293
294 #if SUPPORT_TAGGED_POINTERS
295 .data
296 .align 3
297 .globl _objc_debug_taggedpointer_classes
298 _objc_debug_taggedpointer_classes:
299 .fill 16, 8, 0
300 .globl _objc_debug_taggedpointer_ext_classes
301 _objc_debug_taggedpointer_ext_classes:
302 .fill 256, 8, 0
303 #endif
304
305 ENTRY _objc_msgSend
306 UNWIND _objc_msgSend, NoFrame
307
308 cmp p0, #0 // nil check and tagged pointer check
309 #if SUPPORT_TAGGED_POINTERS
310 b.le LNilOrTagged // (MSB tagged pointer looks negative)
311 #else
312 b.eq LReturnZero
313 #endif
314 ldr p13, [x0] // p13 = isa
315 GetClassFromIsa_p16 p13 // p16 = class
316 LGetIsaDone:
317 CacheLookup NORMAL // calls imp or objc_msgSend_uncached
318
319 #if SUPPORT_TAGGED_POINTERS
320 LNilOrTagged:
321 b.eq LReturnZero // nil check
322
323 // tagged
324 adrp x10, _objc_debug_taggedpointer_classes@PAGE
325 add x10, x10, _objc_debug_taggedpointer_classes@PAGEOFF
326 ubfx x11, x0, #60, #4
327 ldr x16, [x10, x11, LSL #3]
328 adrp x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGE
329 add x10, x10, _OBJC_CLASS_$___NSUnrecognizedTaggedPointer@PAGEOFF
330 cmp x10, x16
331 b.ne LGetIsaDone
332
333 // ext tagged
334 adrp x10, _objc_debug_taggedpointer_ext_classes@PAGE
335 add x10, x10, _objc_debug_taggedpointer_ext_classes@PAGEOFF
336 ubfx x11, x0, #52, #8
337 ldr x16, [x10, x11, LSL #3]
338 b LGetIsaDone
339 // SUPPORT_TAGGED_POINTERS
340 #endif
341
342 LReturnZero:
343 // x0 is already zero
344 mov x1, #0
345 movi d0, #0
346 movi d1, #0
347 movi d2, #0
348 movi d3, #0
349 ret
350
351 END_ENTRY _objc_msgSend
352
353
354 ENTRY _objc_msgLookup
355 UNWIND _objc_msgLookup, NoFrame
356 cmp p0, #0 // nil check and tagged pointer check
357 #if SUPPORT_TAGGED_POINTERS
358 b.le LLookup_NilOrTagged // (MSB tagged pointer looks negative)
359 #else
360 b.eq LLookup_Nil
361 #endif
362 ldr p13, [x0] // p13 = isa
363 GetClassFromIsa_p16 p13 // p16 = class
364 LLookup_GetIsaDone:
365 CacheLookup LOOKUP // returns imp
366
367 #if SUPPORT_TAGGED_POINTERS
368 LLookup_NilOrTagged:
369 b.eq LLookup_Nil // nil check
370
371 // tagged
372 mov x10, #0xf000000000000000
373 cmp x0, x10
374 b.hs LLookup_ExtTag
375 adrp x10, _objc_debug_taggedpointer_classes@PAGE
376 add x10, x10, _objc_debug_taggedpointer_classes@PAGEOFF
377 ubfx x11, x0, #60, #4
378 ldr x16, [x10, x11, LSL #3]
379 b LLookup_GetIsaDone
380
381 LLookup_ExtTag:
382 adrp x10, _objc_debug_taggedpointer_ext_classes@PAGE
383 add x10, x10, _objc_debug_taggedpointer_ext_classes@PAGEOFF
384 ubfx x11, x0, #52, #8
385 ldr x16, [x10, x11, LSL #3]
386 b LLookup_GetIsaDone
387 // SUPPORT_TAGGED_POINTERS
388 #endif
389
390 LLookup_Nil:
391 adrp x17, __objc_msgNil@PAGE
392 add x17, x17, __objc_msgNil@PAGEOFF
393 ret
394
395 END_ENTRY _objc_msgLookup
396
397
398 STATIC_ENTRY __objc_msgNil
399
400 // x0 is already zero
401 mov x1, #0
402 movi d0, #0
403 movi d1, #0
404 movi d2, #0
405 movi d3, #0
406 ret
407
408 END_ENTRY __objc_msgNil
409
410
411 ENTRY _objc_msgSendSuper
412 UNWIND _objc_msgSendSuper, NoFrame
413
414 ldp p0, p16, [x0] // p0 = real receiver, p16 = class
415 CacheLookup NORMAL // calls imp or objc_msgSend_uncached
416
417 END_ENTRY _objc_msgSendSuper
418
419 // no _objc_msgLookupSuper
420
421 ENTRY _objc_msgSendSuper2
422 UNWIND _objc_msgSendSuper2, NoFrame
423
424 ldp p0, p16, [x0] // p0 = real receiver, p16 = class
425 ldr p16, [x16, #SUPERCLASS] // p16 = class->superclass
426 CacheLookup NORMAL
427
428 END_ENTRY _objc_msgSendSuper2
429
430
431 ENTRY _objc_msgLookupSuper2
432 UNWIND _objc_msgLookupSuper2, NoFrame
433
434 ldp p0, p16, [x0] // p0 = real receiver, p16 = class
435 ldr p16, [x16, #SUPERCLASS] // p16 = class->superclass
436 CacheLookup LOOKUP
437
438 END_ENTRY _objc_msgLookupSuper2
439
440
441 .macro MethodTableLookup
442
443 // push frame
444 SignLR
445 stp fp, lr, [sp, #-16]!
446 mov fp, sp
447
448 // save parameter registers: x0..x8, q0..q7
449 sub sp, sp, #(10*8 + 8*16)
450 stp q0, q1, [sp, #(0*16)]
451 stp q2, q3, [sp, #(2*16)]
452 stp q4, q5, [sp, #(4*16)]
453 stp q6, q7, [sp, #(6*16)]
454 stp x0, x1, [sp, #(8*16+0*8)]
455 stp x2, x3, [sp, #(8*16+2*8)]
456 stp x4, x5, [sp, #(8*16+4*8)]
457 stp x6, x7, [sp, #(8*16+6*8)]
458 str x8, [sp, #(8*16+8*8)]
459
460 // receiver and selector already in x0 and x1
461 mov x2, x16
462 bl __class_lookupMethodAndLoadCache3
463
464 // IMP in x0
465 mov x17, x0
466
467 // restore registers and return
468 ldp q0, q1, [sp, #(0*16)]
469 ldp q2, q3, [sp, #(2*16)]
470 ldp q4, q5, [sp, #(4*16)]
471 ldp q6, q7, [sp, #(6*16)]
472 ldp x0, x1, [sp, #(8*16+0*8)]
473 ldp x2, x3, [sp, #(8*16+2*8)]
474 ldp x4, x5, [sp, #(8*16+4*8)]
475 ldp x6, x7, [sp, #(8*16+6*8)]
476 ldr x8, [sp, #(8*16+8*8)]
477
478 mov sp, fp
479 ldp fp, lr, [sp], #16
480 AuthenticateLR
481
482 .endmacro
483
484 STATIC_ENTRY __objc_msgSend_uncached
485 UNWIND __objc_msgSend_uncached, FrameWithNoSaves
486
487 // THIS IS NOT A CALLABLE C FUNCTION
488 // Out-of-band p16 is the class to search
489
490 MethodTableLookup
491 TailCallFunctionPointer x17
492
493 END_ENTRY __objc_msgSend_uncached
494
495
496 STATIC_ENTRY __objc_msgLookup_uncached
497 UNWIND __objc_msgLookup_uncached, FrameWithNoSaves
498
499 // THIS IS NOT A CALLABLE C FUNCTION
500 // Out-of-band p16 is the class to search
501
502 MethodTableLookup
503 ret
504
505 END_ENTRY __objc_msgLookup_uncached
506
507
508 STATIC_ENTRY _cache_getImp
509
510 GetClassFromIsa_p16 p0
511 CacheLookup GETIMP
512
513 LGetImpMiss:
514 mov p0, #0
515 ret
516
517 END_ENTRY _cache_getImp
518
519
520 /********************************************************************
521 *
522 * id _objc_msgForward(id self, SEL _cmd,...);
523 *
524 * _objc_msgForward is the externally-callable
525 * function returned by things like method_getImplementation().
526 * _objc_msgForward_impcache is the function pointer actually stored in
527 * method caches.
528 *
529 ********************************************************************/
530
531 STATIC_ENTRY __objc_msgForward_impcache
532
533 // No stret specialization.
534 b __objc_msgForward
535
536 END_ENTRY __objc_msgForward_impcache
537
538
539 ENTRY __objc_msgForward
540
541 adrp x17, __objc_forward_handler@PAGE
542 ldr p17, [x17, __objc_forward_handler@PAGEOFF]
543 TailCallFunctionPointer x17
544
545 END_ENTRY __objc_msgForward
546
547
548 ENTRY _objc_msgSend_noarg
549 b _objc_msgSend
550 END_ENTRY _objc_msgSend_noarg
551
552 ENTRY _objc_msgSend_debug
553 b _objc_msgSend
554 END_ENTRY _objc_msgSend_debug
555
556 ENTRY _objc_msgSendSuper2_debug
557 b _objc_msgSendSuper2
558 END_ENTRY _objc_msgSendSuper2_debug
559
560
561 ENTRY _method_invoke
562 // x1 is method triplet instead of SEL
563 add p16, p1, #METHOD_IMP
564 ldr p17, [x16]
565 ldr p1, [x1, #METHOD_NAME]
566 TailCallMethodListImp x17, x16
567 END_ENTRY _method_invoke
568
569 #endif