2 * @APPLE_LICENSE_HEADER_START@
4 * Copyright (c) 1999-2007 Apple Computer, Inc. All Rights Reserved.
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 /********************************************************************
25 * objc-msg-arm.s - ARM code to support objc messaging
27 ********************************************************************/
32 #include "objc-config.h"
36 # error requires armv7
39 // Set FP=1 on architectures that pass parameters in floating-point registers
52 # define FP_RETURN_ZERO \
66 # define FP_RETURN_ZERO
74 #define MI_EXTERN(var) \
75 .non_lazy_symbol_pointer ;\
76 L##var##$$non_lazy_ptr: ;\
77 .indirect_symbol var ;\
80 #define MI_GET_EXTERN(reg,var) \
81 movw reg, :lower16:(L##var##$$non_lazy_ptr-7f-4) ;\
82 movt reg, :upper16:(L##var##$$non_lazy_ptr-7f-4) ;\
86 #define MI_GET_ADDRESS(reg,var) \
87 movw reg, :lower16:(var-7f-4) ;\
88 movt reg, :upper16:(var-7f-4) ;\
94 #if SUPPORT_INDEXED_ISA
97 .globl _objc_indexed_classes
98 _objc_indexed_classes:
99 .fill ISA_INDEX_COUNT, 4, 0
105 // _objc_restartableRanges is used by method dispatch
106 // caching code to figure out whether any threads are actively
107 // in the cache for dispatching. The labels surround the asm code
108 // that do cache lookups. The tables are zero-terminated.
110 .macro RestartableEntry
113 .short LLookupEnd$0 - LLookupStart$0
114 .short 0xffff // poor ol' armv7 doesn't support kernel based recovery
119 .private_extern _objc_restartableRanges
120 _objc_restartableRanges:
121 RestartableEntry _cache_getImp
122 RestartableEntry _objc_msgSend
123 RestartableEntry _objc_msgSend_stret
124 RestartableEntry _objc_msgSendSuper
125 RestartableEntry _objc_msgSendSuper_stret
126 RestartableEntry _objc_msgSendSuper2
127 RestartableEntry _objc_msgSendSuper2_stret
128 RestartableEntry _objc_msgLookup
129 RestartableEntry _objc_msgLookup_stret
130 RestartableEntry _objc_msgLookupSuper2
131 RestartableEntry _objc_msgLookupSuper2_stret
135 /********************************************************************
136 * Names for relative labels
137 * DO NOT USE THESE LABELS ELSEWHERE
138 * Reserved labels: 6: 7: 8: 9:
139 ********************************************************************/
140 // 6: used by CacheLookup
141 // 7: used by MI_GET_ADDRESS etc
142 // 8: used by CacheLookup
143 #define LNilReceiver 9
144 #define LNilReceiver_f 9f
145 #define LNilReceiver_b 9b
148 /********************************************************************
150 ********************************************************************/
156 /********************************************************************
158 * Structure definitions.
160 ********************************************************************/
162 /* objc_super parameter to sendSuper */
166 /* Selected field offsets in class structure */
170 #define CACHE_MASK 12
172 /* Field offsets in method cache bucket */
176 /* Selected field offsets in method structure */
177 #define METHOD_NAME 0
178 #define METHOD_TYPES 4
182 //////////////////////////////////////////////////////////////////////
184 // ENTRY functionName
186 // Assembly directives to begin an exported function.
188 // Takes: functionName - name of the exported function
189 //////////////////////////////////////////////////////////////////////
191 .macro ENTRY /* name */
200 .macro STATIC_ENTRY /*name*/
210 //////////////////////////////////////////////////////////////////////
212 // END_ENTRY functionName
214 // Assembly directives to end an exported function. Just a placeholder,
215 // a close-parenthesis for ENTRY, until it is needed for something.
217 // Takes: functionName - name of the exported function
218 //////////////////////////////////////////////////////////////////////
220 .macro END_ENTRY /* name */
225 /////////////////////////////////////////////////////////////////////
227 // CacheLookup NORMAL|STRET <function>
228 // CacheLookup2 NORMAL|STRET <function>
230 // Locate the implementation for a selector in a class's method cache.
233 // $0 = NORMAL, STRET
234 // r0 or r1 (STRET) = receiver
235 // r1 or r2 (STRET) = selector
236 // r9 = class to search in
238 // On exit: r9 clobbered
239 // (found) continues after CacheLookup, IMP in r12, eq set
240 // (not found) continues after CacheLookup2
242 /////////////////////////////////////////////////////////////////////
248 // As soon as we're past the LLookupStart$1 label we may have loaded
249 // an invalid cache pointer or mask.
251 // When task_restartable_ranges_synchronize() is called,
252 // (or when a signal hits us) before we're past LLookupEnd$1,
253 // then our PC will be reset to LCacheMiss$1 which forcefully
254 // jumps to the cache-miss codepath.
256 // It is assumed that the CacheMiss codepath starts right at the end
257 // of CacheLookup2 and will re-setup the registers to meet the cache-miss
261 // The cache-miss is just returning NULL (setting r9 to 0)
264 // - r0 or r1 (STRET) contains the receiver
265 // - r1 or r2 (STRET) contains the selector
266 // - r9 contains the isa (reloaded from r0/r1)
267 // - other registers are set as per calling conventions
271 ldrh r12, [r9, #CACHE_MASK] // r12 = mask
272 ldr r9, [r9, #CACHE] // r9 = buckets
274 and r12, r12, r2 // r12 = index = SEL & mask
276 and r12, r12, r1 // r12 = index = SEL & mask
278 add r9, r9, r12, LSL #3 // r9 = bucket = buckets+index*8
279 ldr r12, [r9, #CACHED_SEL] // r12 = bucket->sel
287 ldr r12, [r9, #CACHED_IMP] // r12 = bucket->imp
290 tst r12, r12 // set ne for stret forwarding
292 // eq already set for nonstret forwarding by `teq` above
299 # error this code requires that SEL be at offset 0
303 blo LCacheMiss$1 // if (bucket->sel == 0) cache miss
304 it eq // if (bucket->sel == 1) cache wrap
305 ldreq r9, [r9, #CACHED_IMP] // bucket->imp is before first bucket
306 ldr r12, [r9, #8]! // r12 = (++bucket)->sel
314 /////////////////////////////////////////////////////////////////////
316 // GetClassFromIsa return-type
318 // Given an Isa, return the class for the Isa.
323 // On exit: r12 clobbered
324 // r9 contains the class for this Isa.
326 /////////////////////////////////////////////////////////////////////
327 .macro GetClassFromIsa
329 #if SUPPORT_INDEXED_ISA
330 // Note: We are doing a little wasted work here to load values we might not
331 // need. Branching turns out to be even worse when performance was measured.
332 MI_GET_ADDRESS(r12, _objc_indexed_classes)
333 tst.w r9, #ISA_INDEX_IS_NPI_MASK
335 ubfxne r9, r9, #ISA_INDEX_SHIFT, #ISA_INDEX_BITS
336 ldrne.w r9, [r12, r9, lsl #2]
342 /********************************************************************
343 * IMP cache_getImp(Class cls, SEL sel)
345 * On entry: r0 = class whose cache is to be searched
346 * r1 = selector to search for
348 * If found, returns method implementation.
349 * If not found, returns NULL.
350 ********************************************************************/
352 STATIC_ENTRY _cache_getImp
355 CacheLookup NORMAL, _cache_getImp
356 // cache hit, IMP in r12
360 CacheLookup2 GETIMP, _cache_getImp
361 // cache miss, return nil
365 END_ENTRY _cache_getImp
368 /********************************************************************
370 * id objc_msgSend(id self, SEL _cmd, ...);
371 * IMP objc_msgLookup(id self, SEL _cmd, ...);
373 * objc_msgLookup ABI:
374 * IMP returned in r12
375 * Forwarding returned in Z flag
376 * r9 reserved for our use but not used
378 ********************************************************************/
382 cbz r0, LNilReceiver_f
384 ldr r9, [r0] // r9 = self->isa
385 GetClassFromIsa // r9 = class
386 CacheLookup NORMAL, _objc_msgSend
387 // cache hit, IMP in r12, eq already set for nonstret forwarding
390 CacheLookup2 NORMAL, _objc_msgSend
392 ldr r9, [r0] // r9 = self->isa
393 GetClassFromIsa // r9 = class
394 b __objc_msgSend_uncached
397 // r0 is already zero
404 END_ENTRY _objc_msgSend
407 ENTRY _objc_msgLookup
409 cbz r0, LNilReceiver_f
411 ldr r9, [r0] // r9 = self->isa
412 GetClassFromIsa // r9 = class
413 CacheLookup NORMAL, _objc_msgLookup
414 // cache hit, IMP in r12, eq already set for nonstret forwarding
417 CacheLookup2 NORMAL, _objc_msgLookup
419 ldr r9, [r0] // r9 = self->isa
420 GetClassFromIsa // r9 = class
421 b __objc_msgLookup_uncached
424 MI_GET_ADDRESS(r12, __objc_msgNil)
427 END_ENTRY _objc_msgLookup
430 STATIC_ENTRY __objc_msgNil
432 // r0 is already zero
439 END_ENTRY __objc_msgNil
442 /********************************************************************
443 * void objc_msgSend_stret(void *st_addr, id self, SEL op, ...);
444 * IMP objc_msgLookup_stret(void *st_addr, id self, SEL op, ...);
446 * objc_msgSend_stret is the struct-return form of msgSend.
447 * The ABI calls for r0 to be used as the address of the structure
448 * being returned, with the parameters in the succeeding registers.
450 * On entry: r0 is the address where the structure is returned,
451 * r1 is the message receiver,
453 ********************************************************************/
455 ENTRY _objc_msgSend_stret
457 cbz r1, LNilReceiver_f
459 ldr r9, [r1] // r9 = self->isa
460 GetClassFromIsa // r9 = class
461 CacheLookup STRET, _objc_msgSend_stret
462 // cache hit, IMP in r12, ne already set for stret forwarding
465 CacheLookup2 STRET, _objc_msgSend_stret
467 ldr r9, [r1] // r9 = self->isa
468 GetClassFromIsa // r9 = class
469 b __objc_msgSend_stret_uncached
474 END_ENTRY _objc_msgSend_stret
477 ENTRY _objc_msgLookup_stret
479 cbz r1, LNilReceiver_f
481 ldr r9, [r1] // r9 = self->isa
482 GetClassFromIsa // r9 = class
483 CacheLookup STRET, _objc_msgLookup_stret
484 // cache hit, IMP in r12, ne already set for stret forwarding
487 CacheLookup2 STRET, _objc_msgLookup_stret
489 ldr r9, [r1] // r9 = self->isa
490 GetClassFromIsa // r9 = class
491 b __objc_msgLookup_stret_uncached
494 MI_GET_ADDRESS(r12, __objc_msgNil_stret)
497 END_ENTRY _objc_msgLookup_stret
500 STATIC_ENTRY __objc_msgNil_stret
504 END_ENTRY __objc_msgNil_stret
507 /********************************************************************
508 * id objc_msgSendSuper(struct objc_super *super, SEL op, ...)
510 * struct objc_super {
512 * Class cls; // the class to search
514 ********************************************************************/
516 ENTRY _objc_msgSendSuper
518 ldr r9, [r0, #CLASS] // r9 = struct super->class
519 CacheLookup NORMAL, _objc_msgSendSuper
520 // cache hit, IMP in r12, eq already set for nonstret forwarding
521 ldr r0, [r0, #RECEIVER] // load real receiver
524 CacheLookup2 NORMAL, _objc_msgSendSuper
526 ldr r9, [r0, #CLASS] // r9 = struct super->class
527 ldr r0, [r0, #RECEIVER] // load real receiver
528 b __objc_msgSend_uncached
530 END_ENTRY _objc_msgSendSuper
533 /********************************************************************
534 * id objc_msgSendSuper2(struct objc_super *super, SEL op, ...)
536 * struct objc_super {
538 * Class cls; // SUBCLASS of the class to search
540 ********************************************************************/
542 ENTRY _objc_msgSendSuper2
544 ldr r9, [r0, #CLASS] // class = struct super->class
545 ldr r9, [r9, #SUPERCLASS] // class = class->superclass
546 CacheLookup NORMAL, _objc_msgSendSuper2
547 // cache hit, IMP in r12, eq already set for nonstret forwarding
548 ldr r0, [r0, #RECEIVER] // load real receiver
551 CacheLookup2 NORMAL, _objc_msgSendSuper2
553 ldr r9, [r0, #CLASS] // class = struct super->class
554 ldr r9, [r9, #SUPERCLASS] // class = class->superclass
555 ldr r0, [r0, #RECEIVER] // load real receiver
556 b __objc_msgSend_uncached
558 END_ENTRY _objc_msgSendSuper2
561 ENTRY _objc_msgLookupSuper2
563 ldr r9, [r0, #CLASS] // class = struct super->class
564 ldr r9, [r9, #SUPERCLASS] // class = class->superclass
565 CacheLookup NORMAL, _objc_msgLookupSuper2
566 // cache hit, IMP in r12, eq already set for nonstret forwarding
567 ldr r0, [r0, #RECEIVER] // load real receiver
570 CacheLookup2 NORMAL, _objc_msgLookupSuper2
573 ldr r9, [r9, #SUPERCLASS] // r9 = class to search
574 ldr r0, [r0, #RECEIVER] // load real receiver
575 b __objc_msgLookup_uncached
577 END_ENTRY _objc_msgLookupSuper2
580 /********************************************************************
581 * void objc_msgSendSuper_stret(void *st_addr, objc_super *self, SEL op, ...);
583 * objc_msgSendSuper_stret is the struct-return form of msgSendSuper.
584 * The ABI calls for r0 to be used as the address of the structure
585 * being returned, with the parameters in the succeeding registers.
587 * On entry: r0 is the address where the structure is returned,
588 * r1 is the address of the objc_super structure,
590 ********************************************************************/
592 ENTRY _objc_msgSendSuper_stret
594 ldr r9, [r1, #CLASS] // r9 = struct super->class
595 CacheLookup STRET, _objc_msgSendSuper_stret
596 // cache hit, IMP in r12, ne already set for stret forwarding
597 ldr r1, [r1, #RECEIVER] // load real receiver
600 CacheLookup2 STRET, _objc_msgSendSuper_stret
602 ldr r9, [r1, #CLASS] // r9 = struct super->class
603 ldr r1, [r1, #RECEIVER] // load real receiver
604 b __objc_msgSend_stret_uncached
606 END_ENTRY _objc_msgSendSuper_stret
609 /********************************************************************
610 * id objc_msgSendSuper2_stret
611 ********************************************************************/
613 ENTRY _objc_msgSendSuper2_stret
615 ldr r9, [r1, #CLASS] // class = struct super->class
616 ldr r9, [r9, #SUPERCLASS] // class = class->superclass
617 CacheLookup STRET, _objc_msgSendSuper2_stret
618 // cache hit, IMP in r12, ne already set for stret forwarding
619 ldr r1, [r1, #RECEIVER] // load real receiver
622 CacheLookup2 STRET, _objc_msgSendSuper2_stret
624 ldr r9, [r1, #CLASS] // class = struct super->class
625 ldr r9, [r9, #SUPERCLASS] // class = class->superclass
626 ldr r1, [r1, #RECEIVER] // load real receiver
627 b __objc_msgSend_stret_uncached
629 END_ENTRY _objc_msgSendSuper2_stret
632 ENTRY _objc_msgLookupSuper2_stret
634 ldr r9, [r1, #CLASS] // class = struct super->class
635 ldr r9, [r9, #SUPERCLASS] // class = class->superclass
636 CacheLookup STRET, _objc_msgLookupSuper2_stret
637 // cache hit, IMP in r12, ne already set for stret forwarding
638 ldr r1, [r1, #RECEIVER] // load real receiver
641 CacheLookup2 STRET, _objc_msgLookupSuper2_stret
644 ldr r9, [r9, #SUPERCLASS] // r9 = class to search
645 ldr r1, [r1, #RECEIVER] // load real receiver
646 b __objc_msgLookup_stret_uncached
648 END_ENTRY _objc_msgLookupSuper2_stret
651 /////////////////////////////////////////////////////////////////////
653 // MethodTableLookup NORMAL|STRET
655 // Locate the implementation for a selector in a class's method lists.
658 // $0 = NORMAL, STRET
659 // r0 or r1 (STRET) = receiver
660 // r1 or r2 (STRET) = selector
661 // r9 = class to search in
663 // On exit: IMP in r12, eq/ne set for forwarding
665 /////////////////////////////////////////////////////////////////////
667 .macro MethodTableLookup
669 stmfd sp!, {r0-r3,r7,lr}
671 sub sp, #8 // align stack
674 // lookUpImpOrForward(obj, sel, cls, LOOKUP_INITIALIZE | LOOKUP_RESOLVER)
676 // receiver already in r0
677 // selector already in r1
679 mov r0, r1 // receiver
680 mov r1, r2 // selector
682 mov r2, r9 // class to search
683 mov r3, #3 // LOOKUP_INITIALIZE | LOOKUP_INITIALIZE
684 blx _lookUpImpOrForward
685 mov r12, r0 // r12 = IMP
688 cmp r12, r12 // set eq for nonstret forwarding
690 tst r12, r12 // set ne for stret forwarding
694 add sp, #8 // align stack
695 ldmfd sp!, {r0-r3,r7,lr}
700 /********************************************************************
702 * _objc_msgSend_uncached
703 * _objc_msgSend_stret_uncached
704 * _objc_msgLookup_uncached
705 * _objc_msgLookup_stret_uncached
706 * The uncached method lookup.
708 ********************************************************************/
710 STATIC_ENTRY __objc_msgSend_uncached
712 // THIS IS NOT A CALLABLE C FUNCTION
713 // Out-of-band r9 is the class to search
715 MethodTableLookup NORMAL // returns IMP in r12
718 END_ENTRY __objc_msgSend_uncached
721 STATIC_ENTRY __objc_msgSend_stret_uncached
723 // THIS IS NOT A CALLABLE C FUNCTION
724 // Out-of-band r9 is the class to search
726 MethodTableLookup STRET // returns IMP in r12
729 END_ENTRY __objc_msgSend_stret_uncached
732 STATIC_ENTRY __objc_msgLookup_uncached
734 // THIS IS NOT A CALLABLE C FUNCTION
735 // Out-of-band r9 is the class to search
737 MethodTableLookup NORMAL // returns IMP in r12
740 END_ENTRY __objc_msgLookup_uncached
743 STATIC_ENTRY __objc_msgLookup_stret_uncached
745 // THIS IS NOT A CALLABLE C FUNCTION
746 // Out-of-band r9 is the class to search
748 MethodTableLookup STRET // returns IMP in r12
751 END_ENTRY __objc_msgLookup_stret_uncached
754 /********************************************************************
756 * id _objc_msgForward(id self, SEL _cmd,...);
758 * _objc_msgForward and _objc_msgForward_stret are the externally-callable
759 * functions returned by things like method_getImplementation().
760 * _objc_msgForward_impcache is the function pointer actually stored in
763 ********************************************************************/
765 MI_EXTERN(__objc_forward_handler)
766 MI_EXTERN(__objc_forward_stret_handler)
768 STATIC_ENTRY __objc_msgForward_impcache
769 // Method cache version
771 // THIS IS NOT A CALLABLE C FUNCTION
772 // Out-of-band Z is 0 (EQ) for normal, 1 (NE) for stret
774 beq __objc_msgForward
775 b __objc_msgForward_stret
777 END_ENTRY __objc_msgForward_impcache
780 ENTRY __objc_msgForward
783 MI_GET_EXTERN(r12, __objc_forward_handler)
787 END_ENTRY __objc_msgForward
790 ENTRY __objc_msgForward_stret
791 // Struct-return version
793 MI_GET_EXTERN(r12, __objc_forward_stret_handler)
797 END_ENTRY __objc_msgForward_stret
800 ENTRY _objc_msgSend_noarg
802 END_ENTRY _objc_msgSend_noarg
804 ENTRY _objc_msgSend_debug
806 END_ENTRY _objc_msgSend_debug
808 ENTRY _objc_msgSendSuper2_debug
809 b _objc_msgSendSuper2
810 END_ENTRY _objc_msgSendSuper2_debug
812 ENTRY _objc_msgSend_stret_debug
813 b _objc_msgSend_stret
814 END_ENTRY _objc_msgSend_stret_debug
816 ENTRY _objc_msgSendSuper2_stret_debug
817 b _objc_msgSendSuper2_stret
818 END_ENTRY _objc_msgSendSuper2_stret_debug
822 // r1 is method triplet instead of SEL
823 ldr r12, [r1, #METHOD_IMP]
824 ldr r1, [r1, #METHOD_NAME]
826 END_ENTRY _method_invoke
829 ENTRY _method_invoke_stret
830 // r2 is method triplet instead of SEL
831 ldr r12, [r2, #METHOD_IMP]
832 ldr r2, [r2, #METHOD_NAME]
834 END_ENTRY _method_invoke_stret
837 .section __DATA,__objc_msg_break