1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995, 1996, 1997, 1998,
3 2001, 2002, 2004, 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 3, or (at your option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
27 /* FIXME: This file has no business including tm.h. */
28 /* FIXME: This should be using libffi instead of __builtin_apply
31 #include "objc-private/common.h"
32 #include "objc-private/error.h"
34 #include "coretypes.h"
36 #include "objc/objc-api.h"
38 #include "objc-private/runtime.h"
39 #include "objc-private/sarray.h"
40 #include "objc/encoding.h"
41 #include "runtime-info.h"
42 #include <assert.h> /* For assert */
43 #include <string.h> /* For strlen */
45 /* Temporarily while we include objc/objc-api.h instead of objc-private/module-abi-8.h. */
46 #define _CLS_IN_CONSTRUCTION 0x10L
47 #define CLS_IS_IN_CONSTRUCTION(cls) __CLS_ISINFO(cls, _CLS_IN_CONSTRUCTION)
49 /* This is how we hack STRUCT_VALUE to be 1 or 0. */
50 #define gen_rtx(args...) 1
51 #define gen_rtx_MEM(args...) 1
52 #define gen_rtx_REG(args...) 1
53 /* Alread defined in gcc/coretypes.h. So prevent double definition warning. */
57 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
58 #define INVISIBLE_STRUCT_RETURN 1
60 #define INVISIBLE_STRUCT_RETURN 0
63 /* The uninstalled dispatch table */
64 struct sarray
*__objc_uninstalled_dtable
= 0; /* !T:MUTEX */
66 /* Two hooks for method forwarding. If either is set, it is invoked
67 * to return a function that performs the real forwarding. If both
68 * are set, the result of __objc_msg_forward2 will be preferred over
69 * that of __objc_msg_forward. If both return NULL or are unset,
70 * the libgcc based functions (__builtin_apply and friends) are
73 IMP (*__objc_msg_forward
) (SEL
) = NULL
;
74 IMP (*__objc_msg_forward2
) (id
, SEL
) = NULL
;
76 /* Send +initialize to class */
77 static void __objc_send_initialize (Class
);
79 static void __objc_install_dispatch_table_for_class (Class
);
81 /* Forward declare some functions */
82 static void __objc_init_install_dtable (id
, SEL
);
84 /* Various forwarding functions that are used based upon the
85 return type for the selector.
86 __objc_block_forward for structures.
87 __objc_double_forward for floats/doubles.
88 __objc_word_forward for pointers or types that fit in registers. */
89 static double __objc_double_forward (id
, SEL
, ...);
90 static id
__objc_word_forward (id
, SEL
, ...);
91 typedef struct { id many
[8]; } __big
;
92 #if INVISIBLE_STRUCT_RETURN
97 __objc_block_forward (id
, SEL
, ...);
98 static struct objc_method
* search_for_method_in_hierarchy (Class
class, SEL sel
);
99 struct objc_method
* search_for_method_in_list (struct objc_method_list
* list
, SEL op
);
100 id
nil_method (id
, SEL
);
102 /* Given a selector, return the proper forwarding implementation. */
105 __objc_get_forward_imp (id rcv
, SEL sel
)
107 /* If a custom forwarding hook was registered, try getting a forwarding
108 function from it. There are two forward routine hooks, one that
109 takes the receiver as an argument and one that does not. */
110 if (__objc_msg_forward2
)
113 if ((result
= __objc_msg_forward2 (rcv
, sel
)) != NULL
)
116 if (__objc_msg_forward
)
119 if ((result
= __objc_msg_forward (sel
)) != NULL
)
123 /* In all other cases, use the default forwarding functions built using
124 __builtin_apply and friends. */
126 const char *t
= sel
->sel_types
;
128 if (t
&& (*t
== '[' || *t
== '(' || *t
== '{')
129 #ifdef OBJC_MAX_STRUCT_BY_VALUE
130 && objc_sizeof_type (t
) > OBJC_MAX_STRUCT_BY_VALUE
133 return (IMP
)__objc_block_forward
;
134 else if (t
&& (*t
== 'f' || *t
== 'd'))
135 return (IMP
)__objc_double_forward
;
137 return (IMP
)__objc_word_forward
;
141 /* Given a class and selector, return the selector's implementation. */
144 get_imp (Class
class, SEL sel
)
146 /* In a vanilla implementation we would first check if the dispatch
147 table is installed. Here instead, to get more speed in the
148 standard case (that the dispatch table is installed) we first try
149 to get the imp using brute force. Only if that fails, we do what
150 we should have been doing from the very beginning, that is, check
151 if the dispatch table needs to be installed, install it if it's
152 not installed, and retrieve the imp from the table if it's
154 void *res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
157 /* Not a valid method */
158 if (class->dtable
== __objc_uninstalled_dtable
)
160 /* The dispatch table needs to be installed. */
161 objc_mutex_lock (__objc_runtime_mutex
);
163 /* Double-checked locking pattern: Check
164 __objc_uninstalled_dtable again in case another thread
165 installed the dtable while we were waiting for the lock
167 if (class->dtable
== __objc_uninstalled_dtable
)
169 __objc_install_dispatch_table_for_class (class);
172 objc_mutex_unlock (__objc_runtime_mutex
);
173 /* Call ourselves with the installed dispatch table
174 and get the real method */
175 res
= get_imp (class, sel
);
179 /* The dispatch table has been installed. */
181 /* Get the method from the dispatch table (we try to get it
182 again in case another thread has installed the dtable just
183 after we invoked sarray_get_safe, but before we checked
184 class->dtable == __objc_uninstalled_dtable).
186 res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
189 /* The dispatch table has been installed, and the method
190 is not in the dispatch table. So the method just
191 doesn't exist for the class. Return the forwarding
193 res
= __objc_get_forward_imp ((id
)class, sel
);
200 /* The new name of get_imp(). */
202 class_getMethodImplementation (Class class_
, SEL selector
)
204 if (class_
== Nil
|| selector
== NULL
)
207 /* get_imp is inlined, so we're good. */
208 return get_imp (class_
, selector
);
211 /* Given a method, return its implementation. */
213 method_get_imp (struct objc_method
* method
)
215 return (method
!= (struct objc_method
*)0) ? method
->method_imp
: (IMP
)0;
218 /* Query if an object can respond to a selector, returns YES if the
219 object implements the selector otherwise NO. Does not check if the
220 method can be forwarded. */
223 __objc_responds_to (id object
, SEL sel
)
227 /* Install dispatch table if need be */
228 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
230 objc_mutex_lock (__objc_runtime_mutex
);
231 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
233 __objc_install_dispatch_table_for_class (object
->class_pointer
);
235 objc_mutex_unlock (__objc_runtime_mutex
);
238 /* Get the method from the dispatch table */
239 res
= sarray_get_safe (object
->class_pointer
->dtable
, (size_t) sel
->sel_id
);
244 class_respondsToSelector (Class class_
, SEL selector
)
248 if (class_
== Nil
|| selector
== NULL
)
251 /* Install dispatch table if need be */
252 if (class_
->dtable
== __objc_uninstalled_dtable
)
254 objc_mutex_lock (__objc_runtime_mutex
);
255 if (class_
->dtable
== __objc_uninstalled_dtable
)
257 __objc_install_dispatch_table_for_class (class_
);
259 objc_mutex_unlock (__objc_runtime_mutex
);
262 /* Get the method from the dispatch table */
263 res
= sarray_get_safe (class_
->dtable
, (size_t) selector
->sel_id
);
267 /* This is the lookup function. All entries in the table are either a
268 valid method *or* zero. If zero then either the dispatch table
269 needs to be installed or it doesn't exist and forwarding is attempted. */
272 objc_msg_lookup (id receiver
, SEL op
)
277 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
281 /* Not a valid method */
282 if (receiver
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
284 /* The dispatch table needs to be installed.
285 This happens on the very first method call to the class. */
286 __objc_init_install_dtable (receiver
, op
);
288 /* Get real method for this in newly installed dtable */
289 result
= get_imp (receiver
->class_pointer
, op
);
293 /* The dispatch table has been installed. Check again
294 if the method exists (just in case the dispatch table
295 has been installed by another thread after we did the
296 previous check that the method exists).
298 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
302 /* If the method still just doesn't exist for the
303 class, attempt to forward the method. */
304 result
= __objc_get_forward_imp (receiver
, op
);
311 return (IMP
)nil_method
;
315 objc_msg_lookup_super (Super_t super
, SEL sel
)
318 return get_imp (super
->class, sel
);
320 return (IMP
)nil_method
;
323 int method_get_sizeof_arguments (Method
*);
326 objc_msg_sendv (id object
, SEL op
, arglist_t arg_frame
)
328 Method
*m
= class_get_instance_method (object
->class_pointer
, op
);
330 *((id
*) method_get_first_argument (m
, arg_frame
, &type
)) = object
;
331 *((SEL
*) method_get_next_argument (arg_frame
, &type
)) = op
;
332 return __builtin_apply ((apply_t
) m
->method_imp
,
334 method_get_sizeof_arguments (m
));
338 __objc_init_dispatch_tables ()
340 __objc_uninstalled_dtable
= sarray_new (200, 0);
343 /* This function is called by objc_msg_lookup when the
344 dispatch table needs to be installed; thus it is called once
345 for each class, namely when the very first message is sent to it. */
347 __objc_init_install_dtable (id receiver
, SEL op
__attribute__ ((__unused__
)))
349 objc_mutex_lock (__objc_runtime_mutex
);
351 /* This may happen, if the programmer has taken the address of a
352 method before the dtable was initialized... too bad for him! */
353 if (receiver
->class_pointer
->dtable
!= __objc_uninstalled_dtable
)
355 objc_mutex_unlock (__objc_runtime_mutex
);
359 if (CLS_ISCLASS (receiver
->class_pointer
))
361 /* receiver is an ordinary object */
362 assert (CLS_ISCLASS (receiver
->class_pointer
));
364 /* install instance methods table */
365 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
367 /* call +initialize -- this will in turn install the factory
368 dispatch table if not already done :-) */
369 __objc_send_initialize (receiver
->class_pointer
);
373 /* receiver is a class object */
374 assert (CLS_ISCLASS ((Class
)receiver
));
375 assert (CLS_ISMETA (receiver
->class_pointer
));
377 /* Install real dtable for factory methods */
378 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
380 __objc_send_initialize ((Class
)receiver
);
382 objc_mutex_unlock (__objc_runtime_mutex
);
385 /* Install dummy table for class which causes the first message to
386 that class (or instances hereof) to be initialized properly */
388 __objc_install_premature_dtable (Class
class)
390 assert (__objc_uninstalled_dtable
);
391 class->dtable
= __objc_uninstalled_dtable
;
394 /* Send +initialize to class if not already done */
396 __objc_send_initialize (Class
class)
398 /* This *must* be a class object */
399 assert (CLS_ISCLASS (class));
400 assert (! CLS_ISMETA (class));
402 if (! CLS_ISINITIALIZED (class))
404 CLS_SETINITIALIZED (class);
405 CLS_SETINITIALIZED (class->class_pointer
);
407 /* Create the garbage collector type memory description */
408 __objc_generate_gc_type_description (class);
410 if (class->super_class
)
411 __objc_send_initialize (class->super_class
);
414 SEL op
= sel_register_name ("initialize");
416 struct objc_method_list
* method_list
= class->class_pointer
->methods
;
418 while (method_list
) {
420 struct objc_method
* method
;
422 for (i
= 0; i
< method_list
->method_count
; i
++) {
423 method
= &(method_list
->method_list
[i
]);
424 if (method
->method_name
425 && method
->method_name
->sel_id
== op
->sel_id
) {
426 imp
= method
->method_imp
;
434 method_list
= method_list
->method_next
;
438 (*imp
) ((id
) class, op
);
444 /* Walk on the methods list of class and install the methods in the reverse
445 order of the lists. Since methods added by categories are before the methods
446 of class in the methods list, this allows categories to substitute methods
447 declared in class. However if more than one category replaces the same
448 method nothing is guaranteed about what method will be used.
449 Assumes that __objc_runtime_mutex is locked down. */
451 __objc_install_methods_in_dtable (Class
class, struct objc_method_list
* method_list
)
458 if (method_list
->method_next
)
459 __objc_install_methods_in_dtable (class, method_list
->method_next
);
461 for (i
= 0; i
< method_list
->method_count
; i
++)
463 struct objc_method
* method
= &(method_list
->method_list
[i
]);
464 sarray_at_put_safe (class->dtable
,
465 (sidx
) method
->method_name
->sel_id
,
470 /* Assumes that __objc_runtime_mutex is locked down. */
472 __objc_install_dispatch_table_for_class (Class
class)
476 /* If the class has not yet had its class links resolved, we must
477 re-compute all class links */
478 if (! CLS_ISRESOLV (class))
479 __objc_resolve_class_links ();
481 super
= class->super_class
;
483 if (super
!= 0 && (super
->dtable
== __objc_uninstalled_dtable
))
484 __objc_install_dispatch_table_for_class (super
);
486 /* Allocate dtable if necessary */
489 objc_mutex_lock (__objc_runtime_mutex
);
490 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
491 objc_mutex_unlock (__objc_runtime_mutex
);
494 class->dtable
= sarray_lazy_copy (super
->dtable
);
496 __objc_install_methods_in_dtable (class, class->methods
);
500 __objc_update_dispatch_table_for_class (Class
class)
505 /* not yet installed -- skip it */
506 if (class->dtable
== __objc_uninstalled_dtable
)
509 objc_mutex_lock (__objc_runtime_mutex
);
512 __objc_install_premature_dtable (class); /* someone might require it... */
513 sarray_free (arr
); /* release memory */
515 /* could have been lazy... */
516 __objc_install_dispatch_table_for_class (class);
518 if (class->subclass_list
) /* Traverse subclasses */
519 for (next
= class->subclass_list
; next
; next
= next
->sibling_class
)
520 __objc_update_dispatch_table_for_class (next
);
522 objc_mutex_unlock (__objc_runtime_mutex
);
526 /* This function adds a method list to a class. This function is
527 typically called by another function specific to the run-time. As
528 such this function does not worry about thread safe issues.
530 This one is only called for categories. Class objects have their
531 methods installed right away, and their selectors are made into
532 SEL's by the function __objc_register_selectors_from_class. */
534 class_add_method_list (Class
class, struct objc_method_list
* list
)
536 /* Passing of a linked list is not allowed. Do multiple calls. */
537 assert (! list
->method_next
);
539 __objc_register_selectors_from_list(list
);
541 /* Add the methods to the class's method list. */
542 list
->method_next
= class->methods
;
543 class->methods
= list
;
545 /* Update the dispatch table of class */
546 __objc_update_dispatch_table_for_class (class);
550 class_get_instance_method (Class
class, SEL op
)
552 return search_for_method_in_hierarchy (class, op
);
556 class_get_class_method (MetaClass
class, SEL op
)
558 return search_for_method_in_hierarchy (class, op
);
562 class_getInstanceMethod (Class class_
, SEL selector
)
564 if (class_
== Nil
|| selector
== NULL
)
567 return search_for_method_in_hierarchy (class_
, selector
);
571 class_getClassMethod (Class class_
, SEL selector
)
573 if (class_
== Nil
|| selector
== NULL
)
576 return search_for_method_in_hierarchy (class_
->class_pointer
,
581 class_addMethod (Class class_
, SEL selector
, IMP implementation
,
582 const char *method_types
)
584 struct objc_method_list
*method_list
;
585 struct objc_method
*method
;
586 const char *method_name
;
588 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
589 || method_types
== NULL
|| (strcmp (method_types
, "") == 0))
592 method_name
= sel_get_name (selector
);
593 if (method_name
== NULL
)
596 method_list
= (struct objc_method_list
*)objc_calloc (1, sizeof (struct objc_method_list
));
597 method_list
->method_count
= 1;
599 method
= &(method_list
->method_list
[0]);
600 method
->method_name
= objc_malloc (strlen (method_name
) + 1);
601 strcpy ((char *)method
->method_name
, method_name
);
603 method
->method_types
= objc_malloc (strlen (method_types
) + 1);
604 strcpy ((char *)method
->method_types
, method_types
);
606 method
->method_imp
= implementation
;
608 if (CLS_IS_IN_CONSTRUCTION (class_
))
610 /* We only need to add the method to the list. It will be
611 registered with the runtime when the class pair is registered
613 method_list
->method_next
= class_
->methods
;
614 class_
->methods
= method_list
;
618 /* Add the method to a live class. */
619 objc_mutex_lock (__objc_runtime_mutex
);
620 class_add_method_list (class_
, method_list
);
621 objc_mutex_unlock (__objc_runtime_mutex
);
627 /* Temporarily, until we include objc/runtime.h. */
629 method_setImplementation (struct objc_method
* method
, IMP implementation
);
632 class_replaceMethod (Class class_
, SEL selector
, IMP implementation
,
633 const char *method_types
)
635 struct objc_method
* method
;
637 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
638 || method_types
== NULL
)
641 method
= search_for_method_in_hierarchy (class_
, selector
);
645 return method_setImplementation (method
, implementation
);
649 class_addMethod (class_
, selector
, implementation
, method_types
);
654 /* Search for a method starting from the current class up its hierarchy.
655 Return a pointer to the method's method structure if found. NULL
657 static struct objc_method
*
658 search_for_method_in_hierarchy (Class cls
, SEL sel
)
660 struct objc_method
* method
= NULL
;
663 if (! sel_is_mapped (sel
))
666 /* Scan the method list of the class. If the method isn't found in the
667 list then step to its super class. */
668 for (class = cls
; ((! method
) && class); class = class->super_class
)
669 method
= search_for_method_in_list (class->methods
, sel
);
676 /* Given a linked list of method and a method's name. Search for the named
677 method's method structure. Return a pointer to the method's method
678 structure if found. NULL otherwise. */
680 search_for_method_in_list (struct objc_method_list
* list
, SEL op
)
682 struct objc_method_list
* method_list
= list
;
684 if (! sel_is_mapped (op
))
687 /* If not found then we'll search the list. */
692 /* Search the method list. */
693 for (i
= 0; i
< method_list
->method_count
; ++i
)
695 struct objc_method
* method
= &method_list
->method_list
[i
];
697 if (method
->method_name
)
698 if (method
->method_name
->sel_id
== op
->sel_id
)
702 /* The method wasn't found. Follow the link to the next list of
704 method_list
= method_list
->method_next
;
710 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
712 /* Forwarding pointers/integers through the normal registers */
714 __objc_word_forward (id rcv
, SEL op
, ...)
718 args
= __builtin_apply_args ();
719 res
= __objc_forward (rcv
, op
, args
);
721 __builtin_return (res
);
726 /* Specific routine for forwarding floats/double because of
727 architectural differences on some processors. i386s for
728 example which uses a floating point stack versus general
729 registers for floating point numbers. This forward routine
730 makes sure that GCC restores the proper return values */
732 __objc_double_forward (id rcv
, SEL op
, ...)
736 args
= __builtin_apply_args ();
737 res
= __objc_forward (rcv
, op
, args
);
738 __builtin_return (res
);
741 #if INVISIBLE_STRUCT_RETURN
746 __objc_block_forward (id rcv
, SEL op
, ...)
750 args
= __builtin_apply_args ();
751 res
= __objc_forward (rcv
, op
, args
);
753 __builtin_return (res
);
755 #if INVISIBLE_STRUCT_RETURN
756 return (__big
) {{0, 0, 0, 0, 0, 0, 0, 0}};
763 /* This function is installed in the dispatch table for all methods which are
764 not implemented. Thus, it is called when a selector is not recognized. */
766 __objc_forward (id object
, SEL sel
, arglist_t args
)
769 static SEL frwd_sel
= 0; /* !T:SAFE2 */
772 /* first try if the object understands forward:: */
774 frwd_sel
= sel_get_any_uid ("forward::");
776 if (__objc_responds_to (object
, frwd_sel
))
778 imp
= get_imp (object
->class_pointer
, frwd_sel
);
779 return (*imp
) (object
, frwd_sel
, sel
, args
);
782 /* If the object recognizes the doesNotRecognize: method then we're going
784 err_sel
= sel_get_any_uid ("doesNotRecognize:");
785 if (__objc_responds_to (object
, err_sel
))
787 imp
= get_imp (object
->class_pointer
, err_sel
);
788 return (*imp
) (object
, err_sel
, sel
);
791 /* The object doesn't recognize the method. Check for responding to
792 error:. If it does then sent it. */
794 char msg
[256 + strlen ((const char *) sel_get_name (sel
))
795 + strlen ((const char *) object
->class_pointer
->name
)];
797 sprintf (msg
, "(%s) %s does not recognize %s",
798 (CLS_ISMETA (object
->class_pointer
)
801 object
->class_pointer
->name
, sel_get_name (sel
));
803 /* TODO: support for error: is surely deprecated ? */
804 err_sel
= sel_get_any_uid ("error:");
805 if (__objc_responds_to (object
, err_sel
))
807 imp
= get_imp (object
->class_pointer
, err_sel
);
808 return (*imp
) (object
, sel_get_any_uid ("error:"), msg
);
811 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
812 a default action is taken. */
813 _objc_abort ("%s\n", msg
);
820 __objc_print_dtable_stats ()
824 objc_mutex_lock (__objc_runtime_mutex
);
827 printf ("memory usage: (%s)\n", "2-level sparse arrays");
829 printf ("memory usage: (%s)\n", "3-level sparse arrays");
832 printf ("arrays: %d = %ld bytes\n", narrays
,
833 (long) ((size_t) narrays
* sizeof (struct sarray
)));
834 total
+= narrays
* sizeof (struct sarray
);
835 printf ("buckets: %d = %ld bytes\n", nbuckets
,
836 (long) ((size_t) nbuckets
* sizeof (struct sbucket
)));
837 total
+= nbuckets
* sizeof (struct sbucket
);
839 printf ("idxtables: %d = %ld bytes\n",
840 idxsize
, (long) ((size_t) idxsize
* sizeof (void *)));
841 total
+= idxsize
* sizeof (void *);
842 printf ("-----------------------------------\n");
843 printf ("total: %d bytes\n", total
);
844 printf ("===================================\n");
846 objc_mutex_unlock (__objc_runtime_mutex
);
849 /* Returns the uninstalled dispatch table indicator.
850 If a class' dispatch table points to __objc_uninstalled_dtable
851 then that means it needs its dispatch table to be installed. */
854 objc_get_uninstalled_dtable ()
856 return __objc_uninstalled_dtable
;