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-private/sarray.h"
41 #include "objc/encoding.h"
42 #include "runtime-info.h"
43 #include <assert.h> /* For assert */
44 #include <string.h> /* For strlen */
46 /* This is how we hack STRUCT_VALUE to be 1 or 0. */
47 #define gen_rtx(args...) 1
48 #define gen_rtx_MEM(args...) 1
49 #define gen_rtx_REG(args...) 1
50 /* Alread defined in gcc/coretypes.h. So prevent double definition warning. */
54 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
55 #define INVISIBLE_STRUCT_RETURN 1
57 #define INVISIBLE_STRUCT_RETURN 0
60 /* The uninstalled dispatch table */
61 struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
63 /* Two hooks for method forwarding. If either is set, it is invoked
64 * to return a function that performs the real forwarding. If both
65 * are set, the result of __objc_msg_forward2 will be preferred over
66 * that of __objc_msg_forward. If both return NULL or are unset,
67 * the libgcc based functions (__builtin_apply and friends) are
70 IMP (*__objc_msg_forward) (SEL) = NULL;
71 IMP (*__objc_msg_forward2) (id, SEL) = NULL;
73 /* Send +initialize to class */
74 static void __objc_send_initialize (Class);
76 static void __objc_install_dispatch_table_for_class (Class);
78 /* Forward declare some functions */
79 static void __objc_init_install_dtable (id, SEL);
81 /* Various forwarding functions that are used based upon the
82 return type for the selector.
83 __objc_block_forward for structures.
84 __objc_double_forward for floats/doubles.
85 __objc_word_forward for pointers or types that fit in registers. */
86 static double __objc_double_forward (id, SEL, ...);
87 static id __objc_word_forward (id, SEL, ...);
88 typedef struct { id many[8]; } __big;
89 #if INVISIBLE_STRUCT_RETURN
94 __objc_block_forward (id, SEL, ...);
95 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
96 Method_t search_for_method_in_list (MethodList_t list, SEL op);
97 id nil_method (id, SEL);
99 /* Given a selector, return the proper forwarding implementation. */
102 __objc_get_forward_imp (id rcv, SEL sel)
104 /* If a custom forwarding hook was registered, try getting a forwarding
105 function from it. There are two forward routine hooks, one that
106 takes the receiver as an argument and one that does not. */
107 if (__objc_msg_forward2)
110 if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
113 if (__objc_msg_forward)
116 if ((result = __objc_msg_forward (sel)) != NULL)
120 /* In all other cases, use the default forwarding functions built using
121 __builtin_apply and friends. */
123 const char *t = sel->sel_types;
125 if (t && (*t == '[' || *t == '(' || *t == '{')
126 #ifdef OBJC_MAX_STRUCT_BY_VALUE
127 && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
130 return (IMP)__objc_block_forward;
131 else if (t && (*t == 'f' || *t == 'd'))
132 return (IMP)__objc_double_forward;
134 return (IMP)__objc_word_forward;
138 /* Given a class and selector, return the selector's implementation. */
141 get_imp (Class class, SEL sel)
143 /* In a vanilla implementation we would first check if the dispatch
144 table is installed. Here instead, to get more speed in the
145 standard case (that the dispatch table is installed) we first try
146 to get the imp using brute force. Only if that fails, we do what
147 we should have been doing from the very beginning, that is, check
148 if the dispatch table needs to be installed, install it if it's
149 not installed, and retrieve the imp from the table if it's
151 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
154 /* Not a valid method */
155 if (class->dtable == __objc_uninstalled_dtable)
157 /* The dispatch table needs to be installed. */
158 objc_mutex_lock (__objc_runtime_mutex);
160 /* Double-checked locking pattern: Check
161 __objc_uninstalled_dtable again in case another thread
162 installed the dtable while we were waiting for the lock
164 if (class->dtable == __objc_uninstalled_dtable)
166 __objc_install_dispatch_table_for_class (class);
169 objc_mutex_unlock (__objc_runtime_mutex);
170 /* Call ourselves with the installed dispatch table
171 and get the real method */
172 res = get_imp (class, sel);
176 /* The dispatch table has been installed. */
178 /* Get the method from the dispatch table (we try to get it
179 again in case another thread has installed the dtable just
180 after we invoked sarray_get_safe, but before we checked
181 class->dtable == __objc_uninstalled_dtable).
183 res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
186 /* The dispatch table has been installed, and the method
187 is not in the dispatch table. So the method just
188 doesn't exist for the class. Return the forwarding
190 res = __objc_get_forward_imp ((id)class, sel);
197 /* Query if an object can respond to a selector, returns YES if the
198 object implements the selector otherwise NO. Does not check if the
199 method can be forwarded. */
202 __objc_responds_to (id object, SEL sel)
206 /* Install dispatch table if need be */
207 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
209 objc_mutex_lock (__objc_runtime_mutex);
210 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
212 __objc_install_dispatch_table_for_class (object->class_pointer);
214 objc_mutex_unlock (__objc_runtime_mutex);
217 /* Get the method from the dispatch table */
218 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
222 /* This is the lookup function. All entries in the table are either a
223 valid method *or* zero. If zero then either the dispatch table
224 needs to be installed or it doesn't exist and forwarding is attempted. */
227 objc_msg_lookup (id receiver, SEL op)
232 result = sarray_get_safe (receiver->class_pointer->dtable,
236 /* Not a valid method */
237 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
239 /* The dispatch table needs to be installed.
240 This happens on the very first method call to the class. */
241 __objc_init_install_dtable (receiver, op);
243 /* Get real method for this in newly installed dtable */
244 result = get_imp (receiver->class_pointer, op);
248 /* The dispatch table has been installed. Check again
249 if the method exists (just in case the dispatch table
250 has been installed by another thread after we did the
251 previous check that the method exists).
253 result = sarray_get_safe (receiver->class_pointer->dtable,
257 /* If the method still just doesn't exist for the
258 class, attempt to forward the method. */
259 result = __objc_get_forward_imp (receiver, op);
266 return (IMP)nil_method;
270 objc_msg_lookup_super (Super_t super, SEL sel)
273 return get_imp (super->class, sel);
275 return (IMP)nil_method;
278 int method_get_sizeof_arguments (Method *);
281 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
283 Method *m = class_get_instance_method (object->class_pointer, op);
285 *((id *) method_get_first_argument (m, arg_frame, &type)) = object;
286 *((SEL *) method_get_next_argument (arg_frame, &type)) = op;
287 return __builtin_apply ((apply_t) m->method_imp,
289 method_get_sizeof_arguments (m));
293 __objc_init_dispatch_tables ()
295 __objc_uninstalled_dtable = sarray_new (200, 0);
298 /* This function is called by objc_msg_lookup when the
299 dispatch table needs to be installed; thus it is called once
300 for each class, namely when the very first message is sent to it. */
302 __objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
304 objc_mutex_lock (__objc_runtime_mutex);
306 /* This may happen, if the programmer has taken the address of a
307 method before the dtable was initialized... too bad for him! */
308 if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
310 objc_mutex_unlock (__objc_runtime_mutex);
314 if (CLS_ISCLASS (receiver->class_pointer))
316 /* receiver is an ordinary object */
317 assert (CLS_ISCLASS (receiver->class_pointer));
319 /* install instance methods table */
320 __objc_install_dispatch_table_for_class (receiver->class_pointer);
322 /* call +initialize -- this will in turn install the factory
323 dispatch table if not already done :-) */
324 __objc_send_initialize (receiver->class_pointer);
328 /* receiver is a class object */
329 assert (CLS_ISCLASS ((Class)receiver));
330 assert (CLS_ISMETA (receiver->class_pointer));
332 /* Install real dtable for factory methods */
333 __objc_install_dispatch_table_for_class (receiver->class_pointer);
335 __objc_send_initialize ((Class)receiver);
337 objc_mutex_unlock (__objc_runtime_mutex);
340 /* Install dummy table for class which causes the first message to
341 that class (or instances hereof) to be initialized properly */
343 __objc_install_premature_dtable (Class class)
345 assert (__objc_uninstalled_dtable);
346 class->dtable = __objc_uninstalled_dtable;
349 /* Send +initialize to class if not already done */
351 __objc_send_initialize (Class class)
353 /* This *must* be a class object */
354 assert (CLS_ISCLASS (class));
355 assert (! CLS_ISMETA (class));
357 if (! CLS_ISINITIALIZED (class))
359 CLS_SETINITIALIZED (class);
360 CLS_SETINITIALIZED (class->class_pointer);
362 /* Create the garbage collector type memory description */
363 __objc_generate_gc_type_description (class);
365 if (class->super_class)
366 __objc_send_initialize (class->super_class);
369 SEL op = sel_register_name ("initialize");
371 MethodList_t method_list = class->class_pointer->methods;
373 while (method_list) {
377 for (i = 0; i < method_list->method_count; i++) {
378 method = &(method_list->method_list[i]);
379 if (method->method_name
380 && method->method_name->sel_id == op->sel_id) {
381 imp = method->method_imp;
389 method_list = method_list->method_next;
393 (*imp) ((id) class, op);
399 /* Walk on the methods list of class and install the methods in the reverse
400 order of the lists. Since methods added by categories are before the methods
401 of class in the methods list, this allows categories to substitute methods
402 declared in class. However if more than one category replaces the same
403 method nothing is guaranteed about what method will be used.
404 Assumes that __objc_runtime_mutex is locked down. */
406 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
413 if (method_list->method_next)
414 __objc_install_methods_in_dtable (class, method_list->method_next);
416 for (i = 0; i < method_list->method_count; i++)
418 Method_t method = &(method_list->method_list[i]);
419 sarray_at_put_safe (class->dtable,
420 (sidx) method->method_name->sel_id,
425 /* Assumes that __objc_runtime_mutex is locked down. */
427 __objc_install_dispatch_table_for_class (Class class)
431 /* If the class has not yet had its class links resolved, we must
432 re-compute all class links */
433 if (! CLS_ISRESOLV (class))
434 __objc_resolve_class_links ();
436 super = class->super_class;
438 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
439 __objc_install_dispatch_table_for_class (super);
441 /* Allocate dtable if necessary */
444 objc_mutex_lock (__objc_runtime_mutex);
445 class->dtable = sarray_new (__objc_selector_max_index, 0);
446 objc_mutex_unlock (__objc_runtime_mutex);
449 class->dtable = sarray_lazy_copy (super->dtable);
451 __objc_install_methods_in_dtable (class, class->methods);
455 __objc_update_dispatch_table_for_class (Class class)
460 /* not yet installed -- skip it */
461 if (class->dtable == __objc_uninstalled_dtable)
464 objc_mutex_lock (__objc_runtime_mutex);
467 __objc_install_premature_dtable (class); /* someone might require it... */
468 sarray_free (arr); /* release memory */
470 /* could have been lazy... */
471 __objc_install_dispatch_table_for_class (class);
473 if (class->subclass_list) /* Traverse subclasses */
474 for (next = class->subclass_list; next; next = next->sibling_class)
475 __objc_update_dispatch_table_for_class (next);
477 objc_mutex_unlock (__objc_runtime_mutex);
481 /* This function adds a method list to a class. This function is
482 typically called by another function specific to the run-time. As
483 such this function does not worry about thread safe issues.
485 This one is only called for categories. Class objects have their
486 methods installed right away, and their selectors are made into
487 SEL's by the function __objc_register_selectors_from_class. */
489 class_add_method_list (Class class, MethodList_t list)
491 /* Passing of a linked list is not allowed. Do multiple calls. */
492 assert (! list->method_next);
494 __objc_register_selectors_from_list(list);
496 /* Add the methods to the class's method list. */
497 list->method_next = class->methods;
498 class->methods = list;
500 /* Update the dispatch table of class */
501 __objc_update_dispatch_table_for_class (class);
505 class_get_instance_method (Class class, SEL op)
507 return search_for_method_in_hierarchy (class, op);
511 class_get_class_method (MetaClass class, SEL op)
513 return search_for_method_in_hierarchy (class, op);
517 /* Search for a method starting from the current class up its hierarchy.
518 Return a pointer to the method's method structure if found. NULL
522 search_for_method_in_hierarchy (Class cls, SEL sel)
524 Method_t method = NULL;
527 if (! sel_is_mapped (sel))
530 /* Scan the method list of the class. If the method isn't found in the
531 list then step to its super class. */
532 for (class = cls; ((! method) && class); class = class->super_class)
533 method = search_for_method_in_list (class->methods, sel);
540 /* Given a linked list of method and a method's name. Search for the named
541 method's method structure. Return a pointer to the method's method
542 structure if found. NULL otherwise. */
544 search_for_method_in_list (MethodList_t list, SEL op)
546 MethodList_t method_list = list;
548 if (! sel_is_mapped (op))
551 /* If not found then we'll search the list. */
556 /* Search the method list. */
557 for (i = 0; i < method_list->method_count; ++i)
559 Method_t method = &method_list->method_list[i];
561 if (method->method_name)
562 if (method->method_name->sel_id == op->sel_id)
566 /* The method wasn't found. Follow the link to the next list of
568 method_list = method_list->method_next;
574 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
576 /* Forwarding pointers/integers through the normal registers */
578 __objc_word_forward (id rcv, SEL op, ...)
582 args = __builtin_apply_args ();
583 res = __objc_forward (rcv, op, args);
585 __builtin_return (res);
590 /* Specific routine for forwarding floats/double because of
591 architectural differences on some processors. i386s for
592 example which uses a floating point stack versus general
593 registers for floating point numbers. This forward routine
594 makes sure that GCC restores the proper return values */
596 __objc_double_forward (id rcv, SEL op, ...)
600 args = __builtin_apply_args ();
601 res = __objc_forward (rcv, op, args);
602 __builtin_return (res);
605 #if INVISIBLE_STRUCT_RETURN
610 __objc_block_forward (id rcv, SEL op, ...)
614 args = __builtin_apply_args ();
615 res = __objc_forward (rcv, op, args);
617 __builtin_return (res);
619 #if INVISIBLE_STRUCT_RETURN
620 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
627 /* This function is installed in the dispatch table for all methods which are
628 not implemented. Thus, it is called when a selector is not recognized. */
630 __objc_forward (id object, SEL sel, arglist_t args)
633 static SEL frwd_sel = 0; /* !T:SAFE2 */
636 /* first try if the object understands forward:: */
638 frwd_sel = sel_get_any_uid ("forward::");
640 if (__objc_responds_to (object, frwd_sel))
642 imp = get_imp (object->class_pointer, frwd_sel);
643 return (*imp) (object, frwd_sel, sel, args);
646 /* If the object recognizes the doesNotRecognize: method then we're going
648 err_sel = sel_get_any_uid ("doesNotRecognize:");
649 if (__objc_responds_to (object, err_sel))
651 imp = get_imp (object->class_pointer, err_sel);
652 return (*imp) (object, err_sel, sel);
655 /* The object doesn't recognize the method. Check for responding to
656 error:. If it does then sent it. */
658 char msg[256 + strlen ((const char *) sel_get_name (sel))
659 + strlen ((const char *) object->class_pointer->name)];
661 sprintf (msg, "(%s) %s does not recognize %s",
662 (CLS_ISMETA (object->class_pointer)
665 object->class_pointer->name, sel_get_name (sel));
667 /* TODO: support for error: is surely deprecated ? */
668 err_sel = sel_get_any_uid ("error:");
669 if (__objc_responds_to (object, err_sel))
671 imp = get_imp (object->class_pointer, err_sel);
672 return (*imp) (object, sel_get_any_uid ("error:"), msg);
675 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
676 a default action is taken. */
677 _objc_abort ("%s\n", msg);
684 __objc_print_dtable_stats ()
688 objc_mutex_lock (__objc_runtime_mutex);
691 printf ("memory usage: (%s)\n", "2-level sparse arrays");
693 printf ("memory usage: (%s)\n", "3-level sparse arrays");
696 printf ("arrays: %d = %ld bytes\n", narrays,
697 (long) ((size_t) narrays * sizeof (struct sarray)));
698 total += narrays * sizeof (struct sarray);
699 printf ("buckets: %d = %ld bytes\n", nbuckets,
700 (long) ((size_t) nbuckets * sizeof (struct sbucket)));
701 total += nbuckets * sizeof (struct sbucket);
703 printf ("idxtables: %d = %ld bytes\n",
704 idxsize, (long) ((size_t) idxsize * sizeof (void *)));
705 total += idxsize * sizeof (void *);
706 printf ("-----------------------------------\n");
707 printf ("total: %d bytes\n", total);
708 printf ("===================================\n");
710 objc_mutex_unlock (__objc_runtime_mutex);
713 /* Returns the uninstalled dispatch table indicator.
714 If a class' dispatch table points to __objc_uninstalled_dtable
715 then that means it needs its dispatch table to be installed. */
718 objc_get_uninstalled_dtable ()
720 return __objc_uninstalled_dtable;