1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995, 1996, 1997, 1998,
3 2001, 2002, 2004, 2009 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.h"
37 #include "objc/objc-api.h"
39 #include "objc-private/runtime.h"
40 #include "objc/sarray.h"
41 #include "objc/encoding.h"
42 #include "runtime-info.h"
44 /* This is how we hack STRUCT_VALUE to be 1 or 0. */
45 #define gen_rtx(args...) 1
46 #define gen_rtx_MEM(args...) 1
47 #define gen_rtx_REG(args...) 1
48 /* Alread defined in gcc/coretypes.h. So prevent double definition warning. */
52 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
53 #define INVISIBLE_STRUCT_RETURN 1
55 #define INVISIBLE_STRUCT_RETURN 0
58 /* The uninstalled dispatch table */
59 struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
61 /* Two hooks for method forwarding. If either is set, it is invoked
62 * to return a function that performs the real forwarding. If both
63 * are set, the result of __objc_msg_forward2 will be preferred over
64 * that of __objc_msg_forward. If both return NULL or are unset,
65 * the libgcc based functions (__builtin_apply and friends) are
68 IMP (*__objc_msg_forward) (SEL) = NULL;
69 IMP (*__objc_msg_forward2) (id, SEL) = NULL;
71 /* Send +initialize to class */
72 static void __objc_send_initialize (Class);
74 static void __objc_install_dispatch_table_for_class (Class);
76 /* Forward declare some functions */
77 static void __objc_init_install_dtable (id, SEL);
79 /* Various forwarding functions that are used based upon the
80 return type for the selector.
81 __objc_block_forward for structures.
82 __objc_double_forward for floats/doubles.
83 __objc_word_forward for pointers or types that fit in registers. */
84 static double __objc_double_forward (id, SEL, ...);
85 static id __objc_word_forward (id, SEL, ...);
86 typedef struct { id many[8]; } __big;
87 #if INVISIBLE_STRUCT_RETURN
92 __objc_block_forward (id, SEL, ...);
93 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
94 Method_t search_for_method_in_list (MethodList_t list, SEL op);
95 id nil_method (id, SEL);
97 /* Given a selector, return the proper forwarding implementation. */
100 __objc_get_forward_imp (id rcv, SEL sel)
102 /* If a custom forwarding hook was registered, try getting a forwarding
103 function from it. There are two forward routine hooks, one that
104 takes the receiver as an argument and one that does not. */
105 if (__objc_msg_forward2)
108 if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
111 if (__objc_msg_forward)
114 if ((result = __objc_msg_forward (sel)) != NULL)
118 /* In all other cases, use the default forwarding functions built using
119 __builtin_apply and friends. */
121 const char *t = sel->sel_types;
123 if (t && (*t == '[' || *t == '(' || *t == '{')
124 #ifdef OBJC_MAX_STRUCT_BY_VALUE
125 && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
128 return (IMP)__objc_block_forward;
129 else if (t && (*t == 'f' || *t == 'd'))
130 return (IMP)__objc_double_forward;
132 return (IMP)__objc_word_forward;
136 /* Given a class and selector, return the selector's implementation. */
139 get_imp (Class class, SEL sel)
141 /* In a vanilla implementation we would first check if the dispatch
142 table is installed. Here instead, to get more speed in the
143 standard case (that the dispatch table is installed) we first try
144 to get the imp using brute force. Only if that fails, we do what
145 we should have been doing from the very beginning, that is, check
146 if the dispatch table needs to be installed, install it if it's
147 not installed, and retrieve the imp from the table if it's
149 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
152 /* Not a valid method */
153 if (class->dtable == __objc_uninstalled_dtable)
155 /* The dispatch table needs to be installed. */
156 objc_mutex_lock (__objc_runtime_mutex);
158 /* Double-checked locking pattern: Check
159 __objc_uninstalled_dtable again in case another thread
160 installed the dtable while we were waiting for the lock
162 if (class->dtable == __objc_uninstalled_dtable)
164 __objc_install_dispatch_table_for_class (class);
167 objc_mutex_unlock (__objc_runtime_mutex);
168 /* Call ourselves with the installed dispatch table
169 and get the real method */
170 res = get_imp (class, sel);
174 /* The dispatch table has been installed. */
176 /* Get the method from the dispatch table (we try to get it
177 again in case another thread has installed the dtable just
178 after we invoked sarray_get_safe, but before we checked
179 class->dtable == __objc_uninstalled_dtable).
181 res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
184 /* The dispatch table has been installed, and the method
185 is not in the dispatch table. So the method just
186 doesn't exist for the class. Return the forwarding
188 res = __objc_get_forward_imp ((id)class, sel);
195 /* Query if an object can respond to a selector, returns YES if the
196 object implements the selector otherwise NO. Does not check if the
197 method can be forwarded. */
200 __objc_responds_to (id object, SEL sel)
204 /* Install dispatch table if need be */
205 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
207 objc_mutex_lock (__objc_runtime_mutex);
208 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
210 __objc_install_dispatch_table_for_class (object->class_pointer);
212 objc_mutex_unlock (__objc_runtime_mutex);
215 /* Get the method from the dispatch table */
216 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
220 /* This is the lookup function. All entries in the table are either a
221 valid method *or* zero. If zero then either the dispatch table
222 needs to be installed or it doesn't exist and forwarding is attempted. */
225 objc_msg_lookup (id receiver, SEL op)
230 result = sarray_get_safe (receiver->class_pointer->dtable,
234 /* Not a valid method */
235 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
237 /* The dispatch table needs to be installed.
238 This happens on the very first method call to the class. */
239 __objc_init_install_dtable (receiver, op);
241 /* Get real method for this in newly installed dtable */
242 result = get_imp (receiver->class_pointer, op);
246 /* The dispatch table has been installed. Check again
247 if the method exists (just in case the dispatch table
248 has been installed by another thread after we did the
249 previous check that the method exists).
251 result = sarray_get_safe (receiver->class_pointer->dtable,
255 /* If the method still just doesn't exist for the
256 class, attempt to forward the method. */
257 result = __objc_get_forward_imp (receiver, op);
264 return (IMP)nil_method;
268 objc_msg_lookup_super (Super_t super, SEL sel)
271 return get_imp (super->class, sel);
273 return (IMP)nil_method;
276 int method_get_sizeof_arguments (Method *);
279 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
281 Method *m = class_get_instance_method (object->class_pointer, op);
283 *((id *) method_get_first_argument (m, arg_frame, &type)) = object;
284 *((SEL *) method_get_next_argument (arg_frame, &type)) = op;
285 return __builtin_apply ((apply_t) m->method_imp,
287 method_get_sizeof_arguments (m));
291 __objc_init_dispatch_tables ()
293 __objc_uninstalled_dtable = sarray_new (200, 0);
296 /* This function is called by objc_msg_lookup when the
297 dispatch table needs to be installed; thus it is called once
298 for each class, namely when the very first message is sent to it. */
300 __objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
302 objc_mutex_lock (__objc_runtime_mutex);
304 /* This may happen, if the programmer has taken the address of a
305 method before the dtable was initialized... too bad for him! */
306 if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
308 objc_mutex_unlock (__objc_runtime_mutex);
312 if (CLS_ISCLASS (receiver->class_pointer))
314 /* receiver is an ordinary object */
315 assert (CLS_ISCLASS (receiver->class_pointer));
317 /* install instance methods table */
318 __objc_install_dispatch_table_for_class (receiver->class_pointer);
320 /* call +initialize -- this will in turn install the factory
321 dispatch table if not already done :-) */
322 __objc_send_initialize (receiver->class_pointer);
326 /* receiver is a class object */
327 assert (CLS_ISCLASS ((Class)receiver));
328 assert (CLS_ISMETA (receiver->class_pointer));
330 /* Install real dtable for factory methods */
331 __objc_install_dispatch_table_for_class (receiver->class_pointer);
333 __objc_send_initialize ((Class)receiver);
335 objc_mutex_unlock (__objc_runtime_mutex);
338 /* Install dummy table for class which causes the first message to
339 that class (or instances hereof) to be initialized properly */
341 __objc_install_premature_dtable (Class class)
343 assert (__objc_uninstalled_dtable);
344 class->dtable = __objc_uninstalled_dtable;
347 /* Send +initialize to class if not already done */
349 __objc_send_initialize (Class class)
351 /* This *must* be a class object */
352 assert (CLS_ISCLASS (class));
353 assert (! CLS_ISMETA (class));
355 if (! CLS_ISINITIALIZED (class))
357 CLS_SETINITIALIZED (class);
358 CLS_SETINITIALIZED (class->class_pointer);
360 /* Create the garbage collector type memory description */
361 __objc_generate_gc_type_description (class);
363 if (class->super_class)
364 __objc_send_initialize (class->super_class);
367 SEL op = sel_register_name ("initialize");
369 MethodList_t method_list = class->class_pointer->methods;
371 while (method_list) {
375 for (i = 0; i < method_list->method_count; i++) {
376 method = &(method_list->method_list[i]);
377 if (method->method_name
378 && method->method_name->sel_id == op->sel_id) {
379 imp = method->method_imp;
387 method_list = method_list->method_next;
391 (*imp) ((id) class, op);
397 /* Walk on the methods list of class and install the methods in the reverse
398 order of the lists. Since methods added by categories are before the methods
399 of class in the methods list, this allows categories to substitute methods
400 declared in class. However if more than one category replaces the same
401 method nothing is guaranteed about what method will be used.
402 Assumes that __objc_runtime_mutex is locked down. */
404 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
411 if (method_list->method_next)
412 __objc_install_methods_in_dtable (class, method_list->method_next);
414 for (i = 0; i < method_list->method_count; i++)
416 Method_t method = &(method_list->method_list[i]);
417 sarray_at_put_safe (class->dtable,
418 (sidx) method->method_name->sel_id,
423 /* Assumes that __objc_runtime_mutex is locked down. */
425 __objc_install_dispatch_table_for_class (Class class)
429 /* If the class has not yet had its class links resolved, we must
430 re-compute all class links */
431 if (! CLS_ISRESOLV (class))
432 __objc_resolve_class_links ();
434 super = class->super_class;
436 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
437 __objc_install_dispatch_table_for_class (super);
439 /* Allocate dtable if necessary */
442 objc_mutex_lock (__objc_runtime_mutex);
443 class->dtable = sarray_new (__objc_selector_max_index, 0);
444 objc_mutex_unlock (__objc_runtime_mutex);
447 class->dtable = sarray_lazy_copy (super->dtable);
449 __objc_install_methods_in_dtable (class, class->methods);
453 __objc_update_dispatch_table_for_class (Class class)
458 /* not yet installed -- skip it */
459 if (class->dtable == __objc_uninstalled_dtable)
462 objc_mutex_lock (__objc_runtime_mutex);
465 __objc_install_premature_dtable (class); /* someone might require it... */
466 sarray_free (arr); /* release memory */
468 /* could have been lazy... */
469 __objc_install_dispatch_table_for_class (class);
471 if (class->subclass_list) /* Traverse subclasses */
472 for (next = class->subclass_list; next; next = next->sibling_class)
473 __objc_update_dispatch_table_for_class (next);
475 objc_mutex_unlock (__objc_runtime_mutex);
479 /* This function adds a method list to a class. This function is
480 typically called by another function specific to the run-time. As
481 such this function does not worry about thread safe issues.
483 This one is only called for categories. Class objects have their
484 methods installed right away, and their selectors are made into
485 SEL's by the function __objc_register_selectors_from_class. */
487 class_add_method_list (Class class, MethodList_t list)
489 /* Passing of a linked list is not allowed. Do multiple calls. */
490 assert (! list->method_next);
492 __objc_register_selectors_from_list(list);
494 /* Add the methods to the class's method list. */
495 list->method_next = class->methods;
496 class->methods = list;
498 /* Update the dispatch table of class */
499 __objc_update_dispatch_table_for_class (class);
503 class_get_instance_method (Class class, SEL op)
505 return search_for_method_in_hierarchy (class, op);
509 class_get_class_method (MetaClass class, SEL op)
511 return search_for_method_in_hierarchy (class, op);
515 /* Search for a method starting from the current class up its hierarchy.
516 Return a pointer to the method's method structure if found. NULL
520 search_for_method_in_hierarchy (Class cls, SEL sel)
522 Method_t method = NULL;
525 if (! sel_is_mapped (sel))
528 /* Scan the method list of the class. If the method isn't found in the
529 list then step to its super class. */
530 for (class = cls; ((! method) && class); class = class->super_class)
531 method = search_for_method_in_list (class->methods, sel);
538 /* Given a linked list of method and a method's name. Search for the named
539 method's method structure. Return a pointer to the method's method
540 structure if found. NULL otherwise. */
542 search_for_method_in_list (MethodList_t list, SEL op)
544 MethodList_t method_list = list;
546 if (! sel_is_mapped (op))
549 /* If not found then we'll search the list. */
554 /* Search the method list. */
555 for (i = 0; i < method_list->method_count; ++i)
557 Method_t method = &method_list->method_list[i];
559 if (method->method_name)
560 if (method->method_name->sel_id == op->sel_id)
564 /* The method wasn't found. Follow the link to the next list of
566 method_list = method_list->method_next;
572 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
574 /* Forwarding pointers/integers through the normal registers */
576 __objc_word_forward (id rcv, SEL op, ...)
580 args = __builtin_apply_args ();
581 res = __objc_forward (rcv, op, args);
583 __builtin_return (res);
588 /* Specific routine for forwarding floats/double because of
589 architectural differences on some processors. i386s for
590 example which uses a floating point stack versus general
591 registers for floating point numbers. This forward routine
592 makes sure that GCC restores the proper return values */
594 __objc_double_forward (id rcv, SEL op, ...)
598 args = __builtin_apply_args ();
599 res = __objc_forward (rcv, op, args);
600 __builtin_return (res);
603 #if INVISIBLE_STRUCT_RETURN
608 __objc_block_forward (id rcv, SEL op, ...)
612 args = __builtin_apply_args ();
613 res = __objc_forward (rcv, op, args);
615 __builtin_return (res);
617 #if INVISIBLE_STRUCT_RETURN
618 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
625 /* This function is installed in the dispatch table for all methods which are
626 not implemented. Thus, it is called when a selector is not recognized. */
628 __objc_forward (id object, SEL sel, arglist_t args)
631 static SEL frwd_sel = 0; /* !T:SAFE2 */
634 /* first try if the object understands forward:: */
636 frwd_sel = sel_get_any_uid ("forward::");
638 if (__objc_responds_to (object, frwd_sel))
640 imp = get_imp (object->class_pointer, frwd_sel);
641 return (*imp) (object, frwd_sel, sel, args);
644 /* If the object recognizes the doesNotRecognize: method then we're going
646 err_sel = sel_get_any_uid ("doesNotRecognize:");
647 if (__objc_responds_to (object, err_sel))
649 imp = get_imp (object->class_pointer, err_sel);
650 return (*imp) (object, err_sel, sel);
653 /* The object doesn't recognize the method. Check for responding to
654 error:. If it does then sent it. */
656 char msg[256 + strlen ((const char *) sel_get_name (sel))
657 + strlen ((const char *) object->class_pointer->name)];
659 sprintf (msg, "(%s) %s does not recognize %s",
660 (CLS_ISMETA (object->class_pointer)
663 object->class_pointer->name, sel_get_name (sel));
665 /* TODO: support for error: is surely deprecated ? */
666 err_sel = sel_get_any_uid ("error:");
667 if (__objc_responds_to (object, err_sel))
669 imp = get_imp (object->class_pointer, err_sel);
670 return (*imp) (object, sel_get_any_uid ("error:"), msg);
673 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
674 a default action is taken. */
675 _objc_abort ("%s\n", msg);
682 __objc_print_dtable_stats ()
686 objc_mutex_lock (__objc_runtime_mutex);
689 printf ("memory usage: (%s)\n", "2-level sparse arrays");
691 printf ("memory usage: (%s)\n", "3-level sparse arrays");
694 printf ("arrays: %d = %ld bytes\n", narrays,
695 (long) ((size_t) narrays * sizeof (struct sarray)));
696 total += narrays * sizeof (struct sarray);
697 printf ("buckets: %d = %ld bytes\n", nbuckets,
698 (long) ((size_t) nbuckets * sizeof (struct sbucket)));
699 total += nbuckets * sizeof (struct sbucket);
701 printf ("idxtables: %d = %ld bytes\n",
702 idxsize, (long) ((size_t) idxsize * sizeof (void *)));
703 total += idxsize * sizeof (void *);
704 printf ("-----------------------------------\n");
705 printf ("total: %d bytes\n", total);
706 printf ("===================================\n");
708 objc_mutex_unlock (__objc_runtime_mutex);
711 /* Returns the uninstalled dispatch table indicator.
712 If a class' dispatch table points to __objc_uninstalled_dtable
713 then that means it needs its dispatch table to be installed. */
716 objc_get_uninstalled_dtable ()
718 return __objc_uninstalled_dtable;