1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995, 1996, 1997, 1998,
3 2001, 2002, 2004 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 2, 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 You should have received a copy of the GNU General Public License along with
18 GCC; see the file COPYING. If not, write to the Free Software
19 Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 /* As a special exception, if you link this library with files compiled with
23 GCC to produce an executable, this does not cause the resulting executable
24 to be covered by the GNU General Public License. This exception does not
25 however invalidate any other reasons why the executable file might be
26 covered by the GNU General Public License. */
28 /* FIXME: This file has no business including tm.h. */
29 /* FIXME: This should be using libffi instead of __builtin_apply
33 #include "coretypes.h"
35 #include "objc/runtime.h"
36 #include "objc/sarray.h"
37 #include "objc/encoding.h"
38 #include "runtime-info.h"
40 /* This is how we hack STRUCT_VALUE to be 1 or 0. */
41 #define gen_rtx(args...) 1
42 #define gen_rtx_MEM(args...) 1
43 #define gen_rtx_REG(args...) 1
47 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
48 #define INVISIBLE_STRUCT_RETURN 1
50 #define INVISIBLE_STRUCT_RETURN 0
53 /* The uninstalled dispatch table */
54 struct sarray
*__objc_uninstalled_dtable
= 0; /* !T:MUTEX */
56 /* Hook for method forwarding. If it is set, is invoked to return a
57 function that performs the real forwarding. Otherwise the libgcc
58 based functions (__builtin_apply and friends) are used. */
59 IMP (*__objc_msg_forward
) (SEL
) = NULL
;
61 /* Send +initialize to class */
62 static void __objc_send_initialize (Class
);
64 static void __objc_install_dispatch_table_for_class (Class
);
66 /* Forward declare some functions */
67 static void __objc_init_install_dtable (id
, SEL
);
69 /* Various forwarding functions that are used based upon the
70 return type for the selector.
71 __objc_block_forward for structures.
72 __objc_double_forward for floats/doubles.
73 __objc_word_forward for pointers or types that fit in registers.
75 static double __objc_double_forward (id
, SEL
, ...);
76 static id
__objc_word_forward (id
, SEL
, ...);
77 typedef struct { id many
[8]; } __big
;
78 #if INVISIBLE_STRUCT_RETURN
83 __objc_block_forward (id
, SEL
, ...);
84 static Method_t
search_for_method_in_hierarchy (Class
class, SEL sel
);
85 Method_t
search_for_method_in_list (MethodList_t list
, SEL op
);
86 id
nil_method (id
, SEL
);
88 /* Given a selector, return the proper forwarding implementation. */
91 __objc_get_forward_imp (SEL sel
)
93 /* If a custom forwarding hook was registered, try getting a forwarding
94 * function from it. */
95 if (__objc_msg_forward
)
98 if ((result
= __objc_msg_forward (sel
)) != NULL
)
102 /* In all other cases, use the default forwarding functions built using
103 * __builtin_apply and friends. */
105 const char *t
= sel
->sel_types
;
107 if (t
&& (*t
== '[' || *t
== '(' || *t
== '{')
108 #ifdef OBJC_MAX_STRUCT_BY_VALUE
109 && objc_sizeof_type (t
) > OBJC_MAX_STRUCT_BY_VALUE
112 return (IMP
)__objc_block_forward
;
113 else if (t
&& (*t
== 'f' || *t
== 'd'))
114 return (IMP
)__objc_double_forward
;
116 return (IMP
)__objc_word_forward
;
120 /* Given a class and selector, return the selector's implementation. */
123 get_imp (Class
class, SEL sel
)
125 /* In a vanilla implementation we would first check if the dispatch
126 table is installed. Here instead, to get more speed in the
127 standard case (that the dispatch table is installed) we first try
128 to get the imp using brute force. Only if that fails, we do what
129 we should have been doing from the very beginning, that is, check
130 if the dispatch table needs to be installed, install it if it's
131 not installed, and retrieve the imp from the table if it's
133 void *res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
136 /* Not a valid method */
137 if (class->dtable
== __objc_uninstalled_dtable
)
139 /* The dispatch table needs to be installed. */
140 objc_mutex_lock (__objc_runtime_mutex
);
142 /* Double-checked locking pattern: Check
143 __objc_uninstalled_dtable again in case another thread
144 installed the dtable while we were waiting for the lock
146 if (class->dtable
== __objc_uninstalled_dtable
)
148 __objc_install_dispatch_table_for_class (class);
151 objc_mutex_unlock (__objc_runtime_mutex
);
152 /* Call ourselves with the installed dispatch table
153 and get the real method */
154 res
= get_imp (class, sel
);
158 /* The dispatch table has been installed. */
160 /* Get the method from the dispatch table (we try to get it
161 again in case another thread has installed the dtable just
162 after we invoked sarray_get_safe, but before we checked
163 class->dtable == __objc_uninstalled_dtable).
165 res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
168 /* The dispatch table has been installed, and the method
169 is not in the dispatch table. So the method just
170 doesn't exist for the class. Return the forwarding
172 res
= __objc_get_forward_imp (sel
);
179 /* Query if an object can respond to a selector, returns YES if the
180 object implements the selector otherwise NO. Does not check if the
181 method can be forwarded. */
184 __objc_responds_to (id object
, SEL sel
)
188 /* Install dispatch table if need be */
189 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
191 objc_mutex_lock (__objc_runtime_mutex
);
192 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
194 __objc_install_dispatch_table_for_class (object
->class_pointer
);
196 objc_mutex_unlock (__objc_runtime_mutex
);
199 /* Get the method from the dispatch table */
200 res
= sarray_get_safe (object
->class_pointer
->dtable
, (size_t) sel
->sel_id
);
204 /* This is the lookup function. All entries in the table are either a
205 valid method *or* zero. If zero then either the dispatch table
206 needs to be installed or it doesn't exist and forwarding is attempted. */
209 objc_msg_lookup (id receiver
, SEL op
)
214 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
218 /* Not a valid method */
219 if (receiver
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
221 /* The dispatch table needs to be installed.
222 This happens on the very first method call to the class. */
223 __objc_init_install_dtable (receiver
, op
);
225 /* Get real method for this in newly installed dtable */
226 result
= get_imp (receiver
->class_pointer
, op
);
230 /* The dispatch table has been installed. Check again
231 if the method exists (just in case the dispatch table
232 has been installed by another thread after we did the
233 previous check that the method exists).
235 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
239 /* If the method still just doesn't exist for the
240 class, attempt to forward the method. */
241 result
= __objc_get_forward_imp (op
);
248 return (IMP
)nil_method
;
252 objc_msg_lookup_super (Super_t super
, SEL sel
)
255 return get_imp (super
->class, sel
);
257 return (IMP
)nil_method
;
260 int method_get_sizeof_arguments (Method
*);
263 objc_msg_sendv (id object
, SEL op
, arglist_t arg_frame
)
265 Method
*m
= class_get_instance_method (object
->class_pointer
, op
);
267 *((id
*) method_get_first_argument (m
, arg_frame
, &type
)) = object
;
268 *((SEL
*) method_get_next_argument (arg_frame
, &type
)) = op
;
269 return __builtin_apply ((apply_t
) m
->method_imp
,
271 method_get_sizeof_arguments (m
));
275 __objc_init_dispatch_tables ()
277 __objc_uninstalled_dtable
= sarray_new (200, 0);
280 /* This function is called by objc_msg_lookup when the
281 dispatch table needs to be installed; thus it is called once
282 for each class, namely when the very first message is sent to it. */
284 __objc_init_install_dtable (id receiver
, SEL op
__attribute__ ((__unused__
)))
286 objc_mutex_lock (__objc_runtime_mutex
);
288 /* This may happen, if the programmer has taken the address of a
289 method before the dtable was initialized... too bad for him! */
290 if (receiver
->class_pointer
->dtable
!= __objc_uninstalled_dtable
)
292 objc_mutex_unlock (__objc_runtime_mutex
);
296 if (CLS_ISCLASS (receiver
->class_pointer
))
298 /* receiver is an ordinary object */
299 assert (CLS_ISCLASS (receiver
->class_pointer
));
301 /* install instance methods table */
302 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
304 /* call +initialize -- this will in turn install the factory
305 dispatch table if not already done :-) */
306 __objc_send_initialize (receiver
->class_pointer
);
310 /* receiver is a class object */
311 assert (CLS_ISCLASS ((Class
)receiver
));
312 assert (CLS_ISMETA (receiver
->class_pointer
));
314 /* Install real dtable for factory methods */
315 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
317 __objc_send_initialize ((Class
)receiver
);
319 objc_mutex_unlock (__objc_runtime_mutex
);
322 /* Install dummy table for class which causes the first message to
323 that class (or instances hereof) to be initialized properly */
325 __objc_install_premature_dtable (Class
class)
327 assert (__objc_uninstalled_dtable
);
328 class->dtable
= __objc_uninstalled_dtable
;
331 /* Send +initialize to class if not already done */
333 __objc_send_initialize (Class
class)
335 /* This *must* be a class object */
336 assert (CLS_ISCLASS (class));
337 assert (! CLS_ISMETA (class));
339 if (! CLS_ISINITIALIZED (class))
341 CLS_SETINITIALIZED (class);
342 CLS_SETINITIALIZED (class->class_pointer
);
344 /* Create the garbage collector type memory description */
345 __objc_generate_gc_type_description (class);
347 if (class->super_class
)
348 __objc_send_initialize (class->super_class
);
351 SEL op
= sel_register_name ("initialize");
353 MethodList_t method_list
= class->class_pointer
->methods
;
355 while (method_list
) {
359 for (i
= 0; i
< method_list
->method_count
; i
++) {
360 method
= &(method_list
->method_list
[i
]);
361 if (method
->method_name
362 && method
->method_name
->sel_id
== op
->sel_id
) {
363 imp
= method
->method_imp
;
371 method_list
= method_list
->method_next
;
375 (*imp
) ((id
) class, op
);
381 /* Walk on the methods list of class and install the methods in the reverse
382 order of the lists. Since methods added by categories are before the methods
383 of class in the methods list, this allows categories to substitute methods
384 declared in class. However if more than one category replaces the same
385 method nothing is guaranteed about what method will be used.
386 Assumes that __objc_runtime_mutex is locked down. */
388 __objc_install_methods_in_dtable (Class
class, MethodList_t method_list
)
395 if (method_list
->method_next
)
396 __objc_install_methods_in_dtable (class, method_list
->method_next
);
398 for (i
= 0; i
< method_list
->method_count
; i
++)
400 Method_t method
= &(method_list
->method_list
[i
]);
401 sarray_at_put_safe (class->dtable
,
402 (sidx
) method
->method_name
->sel_id
,
407 /* Assumes that __objc_runtime_mutex is locked down. */
409 __objc_install_dispatch_table_for_class (Class
class)
413 /* If the class has not yet had its class links resolved, we must
414 re-compute all class links */
415 if (! CLS_ISRESOLV (class))
416 __objc_resolve_class_links ();
418 super
= class->super_class
;
420 if (super
!= 0 && (super
->dtable
== __objc_uninstalled_dtable
))
421 __objc_install_dispatch_table_for_class (super
);
423 /* Allocate dtable if necessary */
426 objc_mutex_lock (__objc_runtime_mutex
);
427 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
428 objc_mutex_unlock (__objc_runtime_mutex
);
431 class->dtable
= sarray_lazy_copy (super
->dtable
);
433 __objc_install_methods_in_dtable (class, class->methods
);
437 __objc_update_dispatch_table_for_class (Class
class)
442 /* not yet installed -- skip it */
443 if (class->dtable
== __objc_uninstalled_dtable
)
446 objc_mutex_lock (__objc_runtime_mutex
);
449 __objc_install_premature_dtable (class); /* someone might require it... */
450 sarray_free (arr
); /* release memory */
452 /* could have been lazy... */
453 __objc_install_dispatch_table_for_class (class);
455 if (class->subclass_list
) /* Traverse subclasses */
456 for (next
= class->subclass_list
; next
; next
= next
->sibling_class
)
457 __objc_update_dispatch_table_for_class (next
);
459 objc_mutex_unlock (__objc_runtime_mutex
);
463 /* This function adds a method list to a class. This function is
464 typically called by another function specific to the run-time. As
465 such this function does not worry about thread safe issues.
467 This one is only called for categories. Class objects have their
468 methods installed right away, and their selectors are made into
469 SEL's by the function __objc_register_selectors_from_class. */
471 class_add_method_list (Class
class, MethodList_t list
)
473 /* Passing of a linked list is not allowed. Do multiple calls. */
474 assert (! list
->method_next
);
476 __objc_register_selectors_from_list(list
);
478 /* Add the methods to the class's method list. */
479 list
->method_next
= class->methods
;
480 class->methods
= list
;
482 /* Update the dispatch table of class */
483 __objc_update_dispatch_table_for_class (class);
487 class_get_instance_method (Class
class, SEL op
)
489 return search_for_method_in_hierarchy (class, op
);
493 class_get_class_method (MetaClass
class, SEL op
)
495 return search_for_method_in_hierarchy (class, op
);
499 /* Search for a method starting from the current class up its hierarchy.
500 Return a pointer to the method's method structure if found. NULL
504 search_for_method_in_hierarchy (Class cls
, SEL sel
)
506 Method_t method
= NULL
;
509 if (! sel_is_mapped (sel
))
512 /* Scan the method list of the class. If the method isn't found in the
513 list then step to its super class. */
514 for (class = cls
; ((! method
) && class); class = class->super_class
)
515 method
= search_for_method_in_list (class->methods
, sel
);
522 /* Given a linked list of method and a method's name. Search for the named
523 method's method structure. Return a pointer to the method's method
524 structure if found. NULL otherwise. */
526 search_for_method_in_list (MethodList_t list
, SEL op
)
528 MethodList_t method_list
= list
;
530 if (! sel_is_mapped (op
))
533 /* If not found then we'll search the list. */
538 /* Search the method list. */
539 for (i
= 0; i
< method_list
->method_count
; ++i
)
541 Method_t method
= &method_list
->method_list
[i
];
543 if (method
->method_name
)
544 if (method
->method_name
->sel_id
== op
->sel_id
)
548 /* The method wasn't found. Follow the link to the next list of
550 method_list
= method_list
->method_next
;
556 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
558 /* Forwarding pointers/integers through the normal registers */
560 __objc_word_forward (id rcv
, SEL op
, ...)
564 args
= __builtin_apply_args ();
565 res
= __objc_forward (rcv
, op
, args
);
567 __builtin_return (res
);
572 /* Specific routine for forwarding floats/double because of
573 architectural differences on some processors. i386s for
574 example which uses a floating point stack versus general
575 registers for floating point numbers. This forward routine
576 makes sure that GCC restores the proper return values */
578 __objc_double_forward (id rcv
, SEL op
, ...)
582 args
= __builtin_apply_args ();
583 res
= __objc_forward (rcv
, op
, args
);
584 __builtin_return (res
);
587 #if INVISIBLE_STRUCT_RETURN
592 __objc_block_forward (id rcv
, SEL op
, ...)
596 args
= __builtin_apply_args ();
597 res
= __objc_forward (rcv
, op
, args
);
599 __builtin_return (res
);
601 #if INVISIBLE_STRUCT_RETURN
602 return (__big
) {{0, 0, 0, 0, 0, 0, 0, 0}};
609 /* This function is installed in the dispatch table for all methods which are
610 not implemented. Thus, it is called when a selector is not recognized. */
612 __objc_forward (id object
, SEL sel
, arglist_t args
)
615 static SEL frwd_sel
= 0; /* !T:SAFE2 */
618 /* first try if the object understands forward:: */
620 frwd_sel
= sel_get_any_uid ("forward::");
622 if (__objc_responds_to (object
, frwd_sel
))
624 imp
= get_imp (object
->class_pointer
, frwd_sel
);
625 return (*imp
) (object
, frwd_sel
, sel
, args
);
628 /* If the object recognizes the doesNotRecognize: method then we're going
630 err_sel
= sel_get_any_uid ("doesNotRecognize:");
631 if (__objc_responds_to (object
, err_sel
))
633 imp
= get_imp (object
->class_pointer
, err_sel
);
634 return (*imp
) (object
, err_sel
, sel
);
637 /* The object doesn't recognize the method. Check for responding to
638 error:. If it does then sent it. */
640 char msg
[256 + strlen ((const char *) sel_get_name (sel
))
641 + strlen ((const char *) object
->class_pointer
->name
)];
643 sprintf (msg
, "(%s) %s does not recognize %s",
644 (CLS_ISMETA (object
->class_pointer
)
647 object
->class_pointer
->name
, sel_get_name (sel
));
649 err_sel
= sel_get_any_uid ("error:");
650 if (__objc_responds_to (object
, err_sel
))
652 imp
= get_imp (object
->class_pointer
, err_sel
);
653 return (*imp
) (object
, sel_get_any_uid ("error:"), msg
);
656 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
657 a default action is taken. */
658 objc_error (object
, OBJC_ERR_UNIMPLEMENTED
, "%s\n", msg
);
665 __objc_print_dtable_stats ()
669 objc_mutex_lock (__objc_runtime_mutex
);
672 printf ("memory usage: (%s)\n", "2-level sparse arrays");
674 printf ("memory usage: (%s)\n", "3-level sparse arrays");
677 printf ("arrays: %d = %ld bytes\n", narrays
,
678 (long) narrays
* sizeof (struct sarray
));
679 total
+= narrays
* sizeof (struct sarray
);
680 printf ("buckets: %d = %ld bytes\n", nbuckets
,
681 (long) nbuckets
* sizeof (struct sbucket
));
682 total
+= nbuckets
* sizeof (struct sbucket
);
684 printf ("idxtables: %d = %ld bytes\n",
685 idxsize
, (long) idxsize
* sizeof (void *));
686 total
+= idxsize
* sizeof (void *);
687 printf ("-----------------------------------\n");
688 printf ("total: %d bytes\n", total
);
689 printf ("===================================\n");
691 objc_mutex_unlock (__objc_runtime_mutex
);
694 /* Returns the uninstalled dispatch table indicator.
695 If a class' dispatch table points to __objc_uninstalled_dtable
696 then that means it needs its dispatch table to be installed. */
699 objc_get_uninstalled_dtable ()
701 return __objc_uninstalled_dtable
;