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 ********************************************************************/
49 #if defined(__DYNAMIC__)
50 #define MI_EXTERN(var) \
51 .non_lazy_symbol_pointer ;\
52 L ## var ## __non_lazy_ptr: ;\
53 .indirect_symbol var ;\
56 #define MI_EXTERN(var) \
61 #if defined(__DYNAMIC__) && defined(THUMB)
62 #define MI_GET_ADDRESS(reg,var) \
68 4: .long L ## var ## __non_lazy_ptr - (3b + 4) ;\
70 #elif defined(__DYNAMIC__)
71 #define MI_GET_ADDRESS(reg,var) \
73 3: ldr reg, [pc, reg] ;\
76 4: .long L ## var ## __non_lazy_ptr - (3b + 8) ;\
79 #define MI_GET_ADDRESS(reg,var) \
88 #if defined(__DYNAMIC__)
89 #define MI_BRANCH_EXTERNAL(var) \
90 MI_GET_ADDRESS(ip, var) ;\
93 #define MI_BRANCH_EXTERNAL(var) \
97 #if defined(__DYNAMIC__) && defined(THUMB)
98 #define MI_CALL_EXTERNAL(var) \
99 MI_GET_ADDRESS(ip,var) ;\
101 #elif defined(__DYNAMIC__)
102 #define MI_CALL_EXTERNAL(var) \
103 MI_GET_ADDRESS(ip,var) ;\
107 #define MI_CALL_EXTERNAL(var) \
112 MI_EXTERN(__class_lookupMethodAndLoadCache3)
114 MI_EXTERN(___objc_error)
115 MI_EXTERN(__objc_forward_handler)
116 MI_EXTERN(__objc_forward_stret_handler)
119 // Special section containing a function pointer that dyld will call
120 // when it loads new images.
121 MI_EXTERN(__objc_notify_images)
124 L__objc_notify_images:
125 MI_BRANCH_EXTERNAL(__objc_notify_images)
127 .section __DATA,__image_notify
128 .long L__objc_notify_images
132 # _objc_entryPoints and _objc_exitPoints are used by method dispatch
133 # caching code to figure out whether any threads are actively
134 # in the cache for dispatching. The labels surround the asm code
135 # that do cache lookups. The tables are zero-terminated.
137 .private_extern _objc_entryPoints
140 .long __cache_getMethod
142 .long _objc_msgSend_noarg
143 .long _objc_msgSend_stret
144 .long _objc_msgSendSuper
145 .long _objc_msgSendSuper_stret
146 .long _objc_msgSendSuper2
147 .long _objc_msgSendSuper2_stret
151 .private_extern _objc_exitPoints
156 .long LMsgSendNoArgExit
157 .long LMsgSendStretExit
158 .long LMsgSendSuperExit
159 .long LMsgSendSuperStretExit
160 .long LMsgSendSuper2Exit
161 .long LMsgSendSuper2StretExit
165 /* objc_super parameter to sendSuper */
169 /* Selected field offsets in class structure */
174 /* Method descriptor */
182 .set BUCKETS, 8 /* variable length array */
185 #####################################################################
189 # Assembly directives to begin an exported function.
190 # We align on cache boundaries for these few functions.
192 # Takes: functionName - name of the exported function
193 #####################################################################
195 .macro ENTRY /* name */
208 .macro STATIC_ENTRY /*name*/
222 #####################################################################
224 # END_ENTRY functionName
226 # Assembly directives to end an exported function. Just a placeholder,
227 # a close-parenthesis for ENTRY, until it is needed for something.
229 # Takes: functionName - name of the exported function
230 #####################################################################
232 .macro END_ENTRY /* name */
236 #####################################################################
238 # CacheLookup selectorRegister, classReg, cacheMissLabel
240 # Locate the implementation for a selector in a class method cache.
243 # $0 = register containing selector (a2 or a3 ONLY)
244 # $1 = class whose cache is to be searched
245 # cacheMissLabel = label to branch to iff method is not cached
250 # On exit: (found) method triplet in $1, imp in ip
251 # (not found) jumps to cacheMissLabel
253 #####################################################################
255 .macro CacheLookup /* selReg, classReg, missLabel */
257 MOVE r9, $0, LSR #2 /* index = (sel >> 2) */
258 ldr a4, [$1, #CACHE] /* cache = class->cache */
259 add a4, a4, #BUCKETS /* buckets = &cache->buckets */
261 /* search the cache */
262 /* a1=receiver, a2 or a3=sel, r9=index, a4=buckets, $1=method */
264 ldr ip, [a4, #NEGMASK] /* mask = cache->mask */
265 and r9, r9, ip /* index &= mask */
266 ldr $1, [a4, r9, LSL #2] /* method = buckets[index] */
267 teq $1, #0 /* if (method == NULL) */
268 add r9, r9, #1 /* index++ */
269 beq $2 /* goto cacheMissLabel */
271 ldr ip, [$1, #METHOD_NAME] /* load method->method_name */
272 teq $0, ip /* if (method->method_name != sel) */
275 /* cache hit, $1 == method triplet address */
276 /* Return triplet in $1 and imp in ip */
277 ldr ip, [$1, #METHOD_IMP] /* imp = method->method_imp */
282 /********************************************************************
283 * Method _cache_getMethod(Class cls, SEL sel, IMP msgForward_internal_imp)
285 * On entry: a1 = class whose cache is to be searched
286 * a2 = selector to search for
287 * a3 = _objc_msgForward_internal IMP
289 * If found, returns method triplet pointer.
290 * If not found, returns NULL.
292 * NOTE: _cache_getMethod never returns any cache entry whose implementation
293 * is _objc_msgForward_internal. It returns NULL instead. This prevents thread-
294 * safety and memory management bugs in _class_lookupMethodAndLoadCache.
295 * See _class_lookupMethodAndLoadCache for details.
297 * _objc_msgForward_internal is passed as a parameter because it's more
298 * efficient to do the (PIC) lookup once in the caller than repeatedly here.
299 ********************************************************************/
301 STATIC_ENTRY _cache_getMethod
304 CacheLookup a2, a1, LGetMethodMiss
306 # cache hit, method triplet in a1 and imp in ip
307 teq ip, a3 /* check for _objc_msgForward_internal */
309 MOVEEQ a1, #1 /* return (Method)1 if forward */
310 /* else return triplet (already in a1) */
314 MOVE a1, #0 /* return nil if cache miss */
318 END_ENTRY _cache_getMethod
321 /********************************************************************
322 * IMP _cache_getImp(Class cls, SEL sel)
324 * On entry: a1 = class whose cache is to be searched
325 * a2 = selector to search for
327 * If found, returns method implementation.
328 * If not found, returns NULL.
329 ********************************************************************/
331 STATIC_ENTRY _cache_getImp
333 # save registers and load class for CacheLookup
336 CacheLookup a2, a1, LGetImpMiss
338 # cache hit, method triplet in a1 and imp in ip
339 MOVE a1, ip @ return imp
343 MOVE a1, #0 @ return nil if cache miss
347 END_ENTRY _cache_getImp
350 /********************************************************************
351 * id objc_msgSend(id self,
355 * On entry: a1 is the message receiver,
357 ********************************************************************/
360 # check whether receiver is nil
362 beq LMsgSendNilReceiver
364 # save registers and load receiver's class for CacheLookup
368 # receiver is non-nil: search the cache
369 CacheLookup a2, v1, LMsgSendCacheMiss
371 # cache hit (imp in ip) and CacheLookup returns with nonstret (eq) set, restore registers and call
375 # cache miss: go search the method lists
378 b _objc_msgSend_uncached
385 END_ENTRY objc_msgSend
388 STATIC_ENTRY objc_msgSend_uncached
391 stmfd sp!, {a1-a4,r7,lr}
394 # Load class and selector
395 ldr a3, [a1, #ISA] /* class = receiver->isa */
396 /* selector already in a2 */
397 /* receiver already in a1 */
400 MI_CALL_EXTERNAL(__class_lookupMethodAndLoadCache3)
403 # Prep for forwarding, Pop stack frame and call imp
404 teq v1, v1 /* set nonstret (eq) */
405 ldmfd sp!, {a1-a4,r7,lr}
408 /********************************************************************
409 * id objc_msgSend_noarg(id self, SEL op)
411 * On entry: a1 is the message receiver,
413 ********************************************************************/
415 ENTRY objc_msgSend_noarg
416 # check whether receiver is nil
418 beq LMsgSendNilReceiver
420 # load receiver's class for CacheLookup
423 # receiver is non-nil: search the cache
424 CacheLookup a2, a3, LMsgSendNoArgCacheMiss
426 # cache hit (imp in ip) and CacheLookup returns with nonstret (eq) set
429 # cache miss: go search the method lists
430 LMsgSendNoArgCacheMiss:
431 b _objc_msgSend_uncached
434 END_ENTRY objc_msgSend_noarg
437 /********************************************************************
438 * struct_type objc_msgSend_stret(id self,
442 * objc_msgSend_stret is the struct-return form of msgSend.
443 * The ABI calls for a1 to be used as the address of the structure
444 * being returned, with the parameters in the succeeding registers.
446 * On entry: a1 is the address where the structure is returned,
447 * a2 is the message receiver,
449 ********************************************************************/
451 ENTRY objc_msgSend_stret
452 # check whether receiver is nil
457 # save registers and load receiver's class for CacheLookup
461 # receiver is non-nil: search the cache
462 CacheLookup a3, v1, LMsgSendStretCacheMiss
464 # cache hit (imp in ip) - prep for forwarding, restore registers and call
465 tst v1, v1 /* set stret (ne); v1 is nonzero (triplet) */
469 # cache miss: go search the method lists
470 LMsgSendStretCacheMiss:
472 b _objc_msgSend_stret_uncached
475 END_ENTRY objc_msgSend_stret
478 STATIC_ENTRY objc_msgSend_stret_uncached
481 stmfd sp!, {a1-a4,r7,lr}
484 # Load class and selector
485 MOVE a1, a2 /* receiver */
486 MOVE a2, a3 /* selector */
487 ldr a3, [a1, #ISA] /* class = receiver->isa */
490 MI_CALL_EXTERNAL(__class_lookupMethodAndLoadCache3)
493 # Prep for forwarding, pop stack frame and call imp
494 tst a1, a1 /* set stret (ne); a1 is nonzero (imp) */
496 ldmfd sp!, {a1-a4,r7,lr}
500 /********************************************************************
501 * id objc_msgSendSuper(struct objc_super *super,
505 * struct objc_super {
509 ********************************************************************/
511 ENTRY objc_msgSendSuper
513 # save registers and load super class for CacheLookup
518 CacheLookup a2, v1, LMsgSendSuperCacheMiss
520 # cache hit (imp in ip) and CacheLookup returns with nonstret (eq) set, restore registers and call
522 ldr a1, [a1, #RECEIVER] @ fetch real receiver
525 # cache miss: go search the method lists
526 LMsgSendSuperCacheMiss:
528 b _objc_msgSendSuper_uncached
531 END_ENTRY objc_msgSendSuper
534 STATIC_ENTRY objc_msgSendSuper_uncached
537 stmfd sp!, {a1-a4,r7,lr}
540 # Load class and selector
541 ldr a3, [a1, #CLASS] /* class = super->class */
542 /* selector already in a2 */
543 ldr a1, [a1, #RECEIVER] /* receiver = super->receiver */
546 MI_CALL_EXTERNAL(__class_lookupMethodAndLoadCache3)
549 # Prep for forwarding, pop stack frame and call imp
550 teq v1, v1 /* set nonstret (eq) */
551 ldmfd sp!, {a1-a4,r7,lr}
552 ldr a1, [a1, #RECEIVER] @ fetch real receiver
556 /********************************************************************
558 ********************************************************************/
560 ENTRY objc_msgSendSuper2
562 # save registers and load super class for CacheLookup
565 ldr v1, [v1, #SUPERCLASS]
568 CacheLookup a2, v1, LMsgSendSuper2CacheMiss
570 # cache hit (imp in ip) and CacheLookup returns with nonstret (eq) set, restore registers and call
572 ldr a1, [a1, #RECEIVER] @ fetch real receiver
575 # cache miss: go search the method lists
576 LMsgSendSuper2CacheMiss:
578 b _objc_msgSendSuper2_uncached
581 END_ENTRY objc_msgSendSuper2
584 STATIC_ENTRY objc_msgSendSuper2_uncached
587 stmfd sp!, {a1-a4,r7,lr}
590 # Load class and selector
591 ldr a3, [a1, #CLASS] /* class = super->class */
592 ldr a3, [a3, #SUPERCLASS] /* class = class->superclass */
593 /* selector already in a2 */
594 ldr a1, [a1, #RECEIVER] /* receiver = super->receiver */
597 MI_CALL_EXTERNAL(__class_lookupMethodAndLoadCache3)
600 # Prep for forwarding, pop stack frame and call imp
601 teq v1, v1 /* set nonstret (eq) */
602 ldmfd sp!, {a1-a4,r7,lr}
603 ldr a1, [a1, #RECEIVER] @ fetch real receiver
607 /********************************************************************
608 * struct_type objc_msgSendSuper_stret(objc_super *super,
612 * struct objc_super {
618 * objc_msgSendSuper_stret is the struct-return form of msgSendSuper.
619 * The ABI calls for a1 to be used as the address of the structure
620 * being returned, with the parameters in the succeeding registers.
622 * On entry: a1 is the address to which to copy the returned structure,
623 * a2 is the address of the objc_super structure,
625 ********************************************************************/
627 ENTRY objc_msgSendSuper_stret
629 # save registers and load super class for CacheLookup
634 CacheLookup a3, v1, LMsgSendSuperStretCacheMiss
636 # cache hit (imp in ip) - prep for forwarding, restore registers and call
637 tst v1, v1 /* set stret (ne); v1 is nonzero (triplet) */
639 ldr a2, [a2, #RECEIVER] @ fetch real receiver
642 # cache miss: go search the method lists
643 LMsgSendSuperStretCacheMiss:
645 b _objc_msgSendSuper_stret_uncached
647 LMsgSendSuperStretExit:
648 END_ENTRY objc_msgSendSuper_stret
651 STATIC_ENTRY objc_msgSendSuper_stret_uncached
654 stmfd sp!, {a1-a4,r7,lr}
657 # Load class and selector
658 MOVE a1, a2 /* struct super */
659 MOVE a2, a3 /* selector */
660 ldr a3, [a1, #CLASS] /* class = super->class */
661 ldr a1, [a1, #RECEIVER] /* receiver = super->receiver */
664 MI_CALL_EXTERNAL(__class_lookupMethodAndLoadCache3)
667 # Prep for forwarding, pop stack frame and call imp
668 tst v1, v1 /* set stret (ne); v1 is nonzero (triplet) */
670 ldmfd sp!, {a1-a4,r7,lr}
671 ldr a2, [a2, #RECEIVER] @ fetch real receiver
675 /********************************************************************
676 * id objc_msgSendSuper2_stret
677 ********************************************************************/
679 ENTRY objc_msgSendSuper2_stret
681 # save registers and load super class for CacheLookup
684 ldr v1, [v1, #SUPERCLASS]
687 CacheLookup a3, v1, LMsgSendSuper2StretCacheMiss
689 # cache hit (imp in ip) - prep for forwarding, restore registers and call
690 tst v1, v1 /* set stret (ne); v1 is nonzero (triplet) */
692 ldr a2, [a2, #RECEIVER] @ fetch real receiver
695 # cache miss: go search the method lists
696 LMsgSendSuper2StretCacheMiss:
698 b _objc_msgSendSuper2_stret_uncached
700 LMsgSendSuper2StretExit:
701 END_ENTRY objc_msgSendSuper2_stret
704 STATIC_ENTRY objc_msgSendSuper2_stret_uncached
707 stmfd sp!, {a1-a4,r7,lr}
710 # Load class and selector
711 MOVE a1, a2 /* struct super */
712 MOVE a2, a3 /* selector */
713 ldr a3, [a1, #CLASS] /* class = super->class */
714 ldr a3, [a3, #SUPERCLASS] /* class = class->superclass */
715 ldr a1, [a1, #RECEIVER] /* receiver = super->receiver */
718 MI_CALL_EXTERNAL(__class_lookupMethodAndLoadCache3)
721 # Prep for forwarding, pop stack frame and call imp
722 tst v1, v1 /* set stret (ne); v1 is nonzero (triplet) */
724 ldmfd sp!, {a1-a4,r7,lr}
725 ldr a2, [a2, #RECEIVER] @ fetch real receiver
729 /********************************************************************
731 * id _objc_msgForward(id self,
734 * struct_type _objc_msgForward_stret (id self,
738 * Both _objc_msgForward and _objc_msgForward_stret
739 * send the message to a method having the signature:
741 * - forward:(SEL)sel :(marg_list)args;
743 * The marg_list's layout is:
746 * d2 | increasing address
758 * typedef struct objc_sendv_margs {
760 * int stackArgs[...];
763 ********************************************************************/
766 .private_extern _FwdSel
770 .private_extern __objc_forward_handler
771 __objc_forward_handler:
774 .private_extern __objc_forward_stret_handler
775 __objc_forward_stret_handler:
779 STATIC_ENTRY _objc_msgForward_internal
780 // Method cache version
782 // THIS IS NOT A CALLABLE C FUNCTION
783 // Out-of-band condition register is NE for stret, EQ otherwise.
785 bne __objc_msgForward_stret
788 END_ENTRY _objc_msgForward_internal
791 ENTRY _objc_msgForward
794 # check for user-installed forwarding handler
795 MI_GET_ADDRESS(ip, __objc_forward_handler)
802 stmfd sp!, {a1-a4} @ push args to marg_list
804 # build forward::'s parameter list (self, forward::, original sel, marg_list)
806 MOVE a3, a2 @ original sel
807 MI_GET_ADDRESS(a2, _FwdSel) @ "forward::"
809 MOVE a4, sp @ marg_list
811 # check for forwarding of forward:: itself
813 beq LMsgForwardError @ original sel == forward:: - give up
816 str lr, [sp, #-(2*4)]! @ save lr and align stack
821 # pop stack frame and return
823 add sp, sp, #(4 + 4 + 4*4) @ skip lr, pad, a1..a4
826 END_ENTRY _objc_msgForward
829 ENTRY _objc_msgForward_stret
830 // Struct-return version
832 # check for user-installed forwarding handler
833 MI_GET_ADDRESS(ip, __objc_forward_stret_handler)
840 stmfd sp!, {a1-a4} @ push args to marg_list
842 # build forward::'s parameter list (self, forward::, original sel, marg_list)
844 MI_GET_ADDRESS(a2, _FwdSel) @ "forward::"
846 # a3 is already original sel
847 MOVE a4, sp @ marg_list
849 # check for forwarding of forward:: itself
851 beq LMsgForwardError @ original sel == forward:: - give up
854 str lr, [sp, #-(2*4)]! @ save lr and align stack
859 # pop stack frame and return
861 add sp, sp, #(4 + 4 + 4*4) @ skip lr, pad, a1..a4
864 END_ENTRY _objc_msgForward_stret
867 # currently a1=self, a2=forward::, a3 = original sel, a4 = marg_list
868 # call __objc_error(self, format, original sel)
869 add a2, pc, #4 @ pc bias is 8 bytes
870 MI_CALL_EXTERNAL(___objc_error)
871 .ascii "Does not recognize selector %s\0"
874 ENTRY objc_msgSend_debug
876 END_ENTRY objc_msgSend_debug
878 ENTRY objc_msgSendSuper2_debug
879 b _objc_msgSendSuper2
880 END_ENTRY objc_msgSendSuper2_debug
882 ENTRY objc_msgSend_stret_debug
883 b _objc_msgSend_stret
884 END_ENTRY objc_msgSend_stret_debug
886 ENTRY objc_msgSendSuper2_stret_debug
887 b _objc_msgSendSuper2_stret
888 END_ENTRY objc_msgSendSuper2_stret_debug
892 # a2 is method triplet instead of SEL
893 ldr ip, [a2, #METHOD_IMP]
894 ldr a2, [a2, #METHOD_NAME]
896 END_ENTRY method_invoke
899 ENTRY method_invoke_stret
900 # a3 is method triplet instead of SEL
901 ldr ip, [a3, #METHOD_IMP]
902 ldr a3, [a3, #METHOD_NAME]
904 END_ENTRY method_invoke_stret
907 STATIC_ENTRY _objc_ignored_method
909 # self is already in a0
912 END_ENTRY _objc_ignored_method