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
192 implementation. We don't know the receiver (only its
193 class), so we have to pass 'nil' as the first
194 argument. Passing the class as first argument is
195 wrong because the class is not the receiver; it can
196 result in us calling a class method when we want an
197 instance method of the same name. */
198 res
= __objc_get_forward_imp (nil
, sel
);
205 /* The new name of get_imp(). */
207 class_getMethodImplementation (Class class_
, SEL selector
)
209 if (class_
== Nil
|| selector
== NULL
)
212 /* get_imp is inlined, so we're good. */
213 return get_imp (class_
, selector
);
216 /* Given a method, return its implementation. */
218 method_get_imp (struct objc_method
* method
)
220 return (method
!= (struct objc_method
*)0) ? method
->method_imp
: (IMP
)0;
223 /* Query if an object can respond to a selector, returns YES if the
224 object implements the selector otherwise NO. Does not check if the
225 method can be forwarded. */
228 __objc_responds_to (id object
, SEL sel
)
232 /* Install dispatch table if need be */
233 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
235 objc_mutex_lock (__objc_runtime_mutex
);
236 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
238 __objc_install_dispatch_table_for_class (object
->class_pointer
);
240 objc_mutex_unlock (__objc_runtime_mutex
);
243 /* Get the method from the dispatch table */
244 res
= sarray_get_safe (object
->class_pointer
->dtable
, (size_t) sel
->sel_id
);
249 class_respondsToSelector (Class class_
, SEL selector
)
253 if (class_
== Nil
|| selector
== NULL
)
256 /* Install dispatch table if need be */
257 if (class_
->dtable
== __objc_uninstalled_dtable
)
259 objc_mutex_lock (__objc_runtime_mutex
);
260 if (class_
->dtable
== __objc_uninstalled_dtable
)
262 __objc_install_dispatch_table_for_class (class_
);
264 objc_mutex_unlock (__objc_runtime_mutex
);
267 /* Get the method from the dispatch table */
268 res
= sarray_get_safe (class_
->dtable
, (size_t) selector
->sel_id
);
272 /* This is the lookup function. All entries in the table are either a
273 valid method *or* zero. If zero then either the dispatch table
274 needs to be installed or it doesn't exist and forwarding is attempted. */
277 objc_msg_lookup (id receiver
, SEL op
)
282 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
286 /* Not a valid method */
287 if (receiver
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
289 /* The dispatch table needs to be installed.
290 This happens on the very first method call to the class. */
291 __objc_init_install_dtable (receiver
, op
);
293 /* Get real method for this in newly installed dtable */
294 result
= get_imp (receiver
->class_pointer
, op
);
298 /* The dispatch table has been installed. Check again
299 if the method exists (just in case the dispatch table
300 has been installed by another thread after we did the
301 previous check that the method exists).
303 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
307 /* If the method still just doesn't exist for the
308 class, attempt to forward the method. */
309 result
= __objc_get_forward_imp (receiver
, op
);
316 return (IMP
)nil_method
;
320 objc_msg_lookup_super (Super_t super
, SEL sel
)
323 return get_imp (super
->class, sel
);
325 return (IMP
)nil_method
;
328 int method_get_sizeof_arguments (Method
*);
331 objc_msg_sendv (id object
, SEL op
, arglist_t arg_frame
)
333 Method
*m
= class_get_instance_method (object
->class_pointer
, op
);
335 *((id
*) method_get_first_argument (m
, arg_frame
, &type
)) = object
;
336 *((SEL
*) method_get_next_argument (arg_frame
, &type
)) = op
;
337 return __builtin_apply ((apply_t
) m
->method_imp
,
339 method_get_sizeof_arguments (m
));
343 __objc_init_dispatch_tables ()
345 __objc_uninstalled_dtable
= sarray_new (200, 0);
348 /* This function is called by objc_msg_lookup when the
349 dispatch table needs to be installed; thus it is called once
350 for each class, namely when the very first message is sent to it. */
352 __objc_init_install_dtable (id receiver
, SEL op
__attribute__ ((__unused__
)))
354 objc_mutex_lock (__objc_runtime_mutex
);
356 /* This may happen, if the programmer has taken the address of a
357 method before the dtable was initialized... too bad for him! */
358 if (receiver
->class_pointer
->dtable
!= __objc_uninstalled_dtable
)
360 objc_mutex_unlock (__objc_runtime_mutex
);
364 if (CLS_ISCLASS (receiver
->class_pointer
))
366 /* receiver is an ordinary object */
367 assert (CLS_ISCLASS (receiver
->class_pointer
));
369 /* install instance methods table */
370 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
372 /* call +initialize -- this will in turn install the factory
373 dispatch table if not already done :-) */
374 __objc_send_initialize (receiver
->class_pointer
);
378 /* receiver is a class object */
379 assert (CLS_ISCLASS ((Class
)receiver
));
380 assert (CLS_ISMETA (receiver
->class_pointer
));
382 /* Install real dtable for factory methods */
383 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
385 __objc_send_initialize ((Class
)receiver
);
387 objc_mutex_unlock (__objc_runtime_mutex
);
390 /* Install dummy table for class which causes the first message to
391 that class (or instances hereof) to be initialized properly */
393 __objc_install_premature_dtable (Class
class)
395 assert (__objc_uninstalled_dtable
);
396 class->dtable
= __objc_uninstalled_dtable
;
399 /* Send +initialize to class if not already done */
401 __objc_send_initialize (Class
class)
403 /* This *must* be a class object */
404 assert (CLS_ISCLASS (class));
405 assert (! CLS_ISMETA (class));
407 if (! CLS_ISINITIALIZED (class))
409 CLS_SETINITIALIZED (class);
410 CLS_SETINITIALIZED (class->class_pointer
);
412 /* Create the garbage collector type memory description */
413 __objc_generate_gc_type_description (class);
415 if (class->super_class
)
416 __objc_send_initialize (class->super_class
);
419 SEL op
= sel_register_name ("initialize");
421 struct objc_method_list
* method_list
= class->class_pointer
->methods
;
423 while (method_list
) {
425 struct objc_method
* method
;
427 for (i
= 0; i
< method_list
->method_count
; i
++) {
428 method
= &(method_list
->method_list
[i
]);
429 if (method
->method_name
430 && method
->method_name
->sel_id
== op
->sel_id
) {
431 imp
= method
->method_imp
;
439 method_list
= method_list
->method_next
;
443 (*imp
) ((id
) class, op
);
449 /* Walk on the methods list of class and install the methods in the reverse
450 order of the lists. Since methods added by categories are before the methods
451 of class in the methods list, this allows categories to substitute methods
452 declared in class. However if more than one category replaces the same
453 method nothing is guaranteed about what method will be used.
454 Assumes that __objc_runtime_mutex is locked down. */
456 __objc_install_methods_in_dtable (Class
class, struct objc_method_list
* method_list
)
463 if (method_list
->method_next
)
464 __objc_install_methods_in_dtable (class, method_list
->method_next
);
466 for (i
= 0; i
< method_list
->method_count
; i
++)
468 struct objc_method
* method
= &(method_list
->method_list
[i
]);
469 sarray_at_put_safe (class->dtable
,
470 (sidx
) method
->method_name
->sel_id
,
475 /* Assumes that __objc_runtime_mutex is locked down. */
477 __objc_install_dispatch_table_for_class (Class
class)
481 /* If the class has not yet had its class links resolved, we must
482 re-compute all class links */
483 if (! CLS_ISRESOLV (class))
484 __objc_resolve_class_links ();
486 super
= class->super_class
;
488 if (super
!= 0 && (super
->dtable
== __objc_uninstalled_dtable
))
489 __objc_install_dispatch_table_for_class (super
);
491 /* Allocate dtable if necessary */
494 objc_mutex_lock (__objc_runtime_mutex
);
495 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
496 objc_mutex_unlock (__objc_runtime_mutex
);
499 class->dtable
= sarray_lazy_copy (super
->dtable
);
501 __objc_install_methods_in_dtable (class, class->methods
);
505 __objc_update_dispatch_table_for_class (Class
class)
510 /* not yet installed -- skip it */
511 if (class->dtable
== __objc_uninstalled_dtable
)
514 objc_mutex_lock (__objc_runtime_mutex
);
517 __objc_install_premature_dtable (class); /* someone might require it... */
518 sarray_free (arr
); /* release memory */
520 /* could have been lazy... */
521 __objc_install_dispatch_table_for_class (class);
523 if (class->subclass_list
) /* Traverse subclasses */
524 for (next
= class->subclass_list
; next
; next
= next
->sibling_class
)
525 __objc_update_dispatch_table_for_class (next
);
527 objc_mutex_unlock (__objc_runtime_mutex
);
531 /* This function adds a method list to a class. This function is
532 typically called by another function specific to the run-time. As
533 such this function does not worry about thread safe issues.
535 This one is only called for categories. Class objects have their
536 methods installed right away, and their selectors are made into
537 SEL's by the function __objc_register_selectors_from_class. */
539 class_add_method_list (Class
class, struct objc_method_list
* list
)
541 /* Passing of a linked list is not allowed. Do multiple calls. */
542 assert (! list
->method_next
);
544 __objc_register_selectors_from_list(list
);
546 /* Add the methods to the class's method list. */
547 list
->method_next
= class->methods
;
548 class->methods
= list
;
550 /* Update the dispatch table of class */
551 __objc_update_dispatch_table_for_class (class);
555 class_get_instance_method (Class
class, SEL op
)
557 return search_for_method_in_hierarchy (class, op
);
561 class_get_class_method (MetaClass
class, SEL op
)
563 return search_for_method_in_hierarchy (class, op
);
567 class_getInstanceMethod (Class class_
, SEL selector
)
569 if (class_
== Nil
|| selector
== NULL
)
572 return search_for_method_in_hierarchy (class_
, selector
);
576 class_getClassMethod (Class class_
, SEL selector
)
578 if (class_
== Nil
|| selector
== NULL
)
581 return search_for_method_in_hierarchy (class_
->class_pointer
,
586 class_addMethod (Class class_
, SEL selector
, IMP implementation
,
587 const char *method_types
)
589 struct objc_method_list
*method_list
;
590 struct objc_method
*method
;
591 const char *method_name
;
593 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
594 || method_types
== NULL
|| (strcmp (method_types
, "") == 0))
597 method_name
= sel_get_name (selector
);
598 if (method_name
== NULL
)
601 method_list
= (struct objc_method_list
*)objc_calloc (1, sizeof (struct objc_method_list
));
602 method_list
->method_count
= 1;
604 method
= &(method_list
->method_list
[0]);
605 method
->method_name
= objc_malloc (strlen (method_name
) + 1);
606 strcpy ((char *)method
->method_name
, method_name
);
608 method
->method_types
= objc_malloc (strlen (method_types
) + 1);
609 strcpy ((char *)method
->method_types
, method_types
);
611 method
->method_imp
= implementation
;
613 if (CLS_IS_IN_CONSTRUCTION (class_
))
615 /* We only need to add the method to the list. It will be
616 registered with the runtime when the class pair is registered
618 method_list
->method_next
= class_
->methods
;
619 class_
->methods
= method_list
;
623 /* Add the method to a live class. */
624 objc_mutex_lock (__objc_runtime_mutex
);
625 class_add_method_list (class_
, method_list
);
626 objc_mutex_unlock (__objc_runtime_mutex
);
632 /* Temporarily, until we include objc/runtime.h. */
634 method_setImplementation (struct objc_method
* method
, IMP implementation
);
637 class_replaceMethod (Class class_
, SEL selector
, IMP implementation
,
638 const char *method_types
)
640 struct objc_method
* method
;
642 if (class_
== Nil
|| selector
== NULL
|| implementation
== NULL
643 || method_types
== NULL
)
646 method
= search_for_method_in_hierarchy (class_
, selector
);
650 return method_setImplementation (method
, implementation
);
654 class_addMethod (class_
, selector
, implementation
, method_types
);
659 /* Search for a method starting from the current class up its hierarchy.
660 Return a pointer to the method's method structure if found. NULL
662 static struct objc_method
*
663 search_for_method_in_hierarchy (Class cls
, SEL sel
)
665 struct objc_method
* method
= NULL
;
668 if (! sel_is_mapped (sel
))
671 /* Scan the method list of the class. If the method isn't found in the
672 list then step to its super class. */
673 for (class = cls
; ((! method
) && class); class = class->super_class
)
674 method
= search_for_method_in_list (class->methods
, sel
);
681 /* Given a linked list of method and a method's name. Search for the named
682 method's method structure. Return a pointer to the method's method
683 structure if found. NULL otherwise. */
685 search_for_method_in_list (struct objc_method_list
* list
, SEL op
)
687 struct objc_method_list
* method_list
= list
;
689 if (! sel_is_mapped (op
))
692 /* If not found then we'll search the list. */
697 /* Search the method list. */
698 for (i
= 0; i
< method_list
->method_count
; ++i
)
700 struct objc_method
* method
= &method_list
->method_list
[i
];
702 if (method
->method_name
)
703 if (method
->method_name
->sel_id
== op
->sel_id
)
707 /* The method wasn't found. Follow the link to the next list of
709 method_list
= method_list
->method_next
;
715 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
717 /* Forwarding pointers/integers through the normal registers */
719 __objc_word_forward (id rcv
, SEL op
, ...)
723 args
= __builtin_apply_args ();
724 res
= __objc_forward (rcv
, op
, args
);
726 __builtin_return (res
);
731 /* Specific routine for forwarding floats/double because of
732 architectural differences on some processors. i386s for
733 example which uses a floating point stack versus general
734 registers for floating point numbers. This forward routine
735 makes sure that GCC restores the proper return values */
737 __objc_double_forward (id rcv
, SEL op
, ...)
741 args
= __builtin_apply_args ();
742 res
= __objc_forward (rcv
, op
, args
);
743 __builtin_return (res
);
746 #if INVISIBLE_STRUCT_RETURN
751 __objc_block_forward (id rcv
, SEL op
, ...)
755 args
= __builtin_apply_args ();
756 res
= __objc_forward (rcv
, op
, args
);
758 __builtin_return (res
);
760 #if INVISIBLE_STRUCT_RETURN
761 return (__big
) {{0, 0, 0, 0, 0, 0, 0, 0}};
768 /* This function is installed in the dispatch table for all methods which are
769 not implemented. Thus, it is called when a selector is not recognized. */
771 __objc_forward (id object
, SEL sel
, arglist_t args
)
774 static SEL frwd_sel
= 0; /* !T:SAFE2 */
777 /* first try if the object understands forward:: */
779 frwd_sel
= sel_get_any_uid ("forward::");
781 if (__objc_responds_to (object
, frwd_sel
))
783 imp
= get_imp (object
->class_pointer
, frwd_sel
);
784 return (*imp
) (object
, frwd_sel
, sel
, args
);
787 /* If the object recognizes the doesNotRecognize: method then we're going
789 err_sel
= sel_get_any_uid ("doesNotRecognize:");
790 if (__objc_responds_to (object
, err_sel
))
792 imp
= get_imp (object
->class_pointer
, err_sel
);
793 return (*imp
) (object
, err_sel
, sel
);
796 /* The object doesn't recognize the method. Check for responding to
797 error:. If it does then sent it. */
799 char msg
[256 + strlen ((const char *) sel_get_name (sel
))
800 + strlen ((const char *) object
->class_pointer
->name
)];
802 sprintf (msg
, "(%s) %s does not recognize %s",
803 (CLS_ISMETA (object
->class_pointer
)
806 object
->class_pointer
->name
, sel_get_name (sel
));
808 /* TODO: support for error: is surely deprecated ? */
809 err_sel
= sel_get_any_uid ("error:");
810 if (__objc_responds_to (object
, err_sel
))
812 imp
= get_imp (object
->class_pointer
, err_sel
);
813 return (*imp
) (object
, sel_get_any_uid ("error:"), msg
);
816 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
817 a default action is taken. */
818 _objc_abort ("%s\n", msg
);
825 __objc_print_dtable_stats ()
829 objc_mutex_lock (__objc_runtime_mutex
);
832 printf ("memory usage: (%s)\n", "2-level sparse arrays");
834 printf ("memory usage: (%s)\n", "3-level sparse arrays");
837 printf ("arrays: %d = %ld bytes\n", narrays
,
838 (long) ((size_t) narrays
* sizeof (struct sarray
)));
839 total
+= narrays
* sizeof (struct sarray
);
840 printf ("buckets: %d = %ld bytes\n", nbuckets
,
841 (long) ((size_t) nbuckets
* sizeof (struct sbucket
)));
842 total
+= nbuckets
* sizeof (struct sbucket
);
844 printf ("idxtables: %d = %ld bytes\n",
845 idxsize
, (long) ((size_t) idxsize
* sizeof (void *)));
846 total
+= idxsize
* sizeof (void *);
847 printf ("-----------------------------------\n");
848 printf ("total: %d bytes\n", total
);
849 printf ("===================================\n");
851 objc_mutex_unlock (__objc_runtime_mutex
);
854 /* Returns the uninstalled dispatch table indicator.
855 If a class' dispatch table points to __objc_uninstalled_dtable
856 then that means it needs its dispatch table to be installed. */
859 objc_get_uninstalled_dtable ()
861 return __objc_uninstalled_dtable
;