PR c++/31074
[official-gcc.git] / libobjc / sendmsg.c
blobc1aed1c0f9a3f722dde349ccf4cec6c05bb8f7a1
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
15 details.
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
30 and friends. */
32 #include "tconfig.h"
33 #include "coretypes.h"
34 #include "tm.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
44 #define rtx int
46 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
47 #define INVISIBLE_STRUCT_RETURN 1
48 #else
49 #define INVISIBLE_STRUCT_RETURN 0
50 #endif
52 /* The uninstalled dispatch table */
53 struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
55 /* Two hooks for method forwarding. If either is set, it is invoked
56 * to return a function that performs the real forwarding. If both
57 * are set, the result of __objc_msg_forward2 will be preferred over
58 * that of __objc_msg_forward. If both return NULL or are unset,
59 * the libgcc based functions (__builtin_apply and friends) are
60 * used.
62 IMP (*__objc_msg_forward) (SEL) = NULL;
63 IMP (*__objc_msg_forward2) (id, SEL) = NULL;
65 /* Send +initialize to class */
66 static void __objc_send_initialize (Class);
68 static void __objc_install_dispatch_table_for_class (Class);
70 /* Forward declare some functions */
71 static void __objc_init_install_dtable (id, SEL);
73 /* Various forwarding functions that are used based upon the
74 return type for the selector.
75 __objc_block_forward for structures.
76 __objc_double_forward for floats/doubles.
77 __objc_word_forward for pointers or types that fit in registers. */
78 static double __objc_double_forward (id, SEL, ...);
79 static id __objc_word_forward (id, SEL, ...);
80 typedef struct { id many[8]; } __big;
81 #if INVISIBLE_STRUCT_RETURN
82 static __big
83 #else
84 static id
85 #endif
86 __objc_block_forward (id, SEL, ...);
87 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
88 Method_t search_for_method_in_list (MethodList_t list, SEL op);
89 id nil_method (id, SEL);
91 /* Given a selector, return the proper forwarding implementation. */
92 inline
93 IMP
94 __objc_get_forward_imp (id rcv, SEL sel)
96 /* If a custom forwarding hook was registered, try getting a forwarding
97 function from it. There are two forward routine hooks, one that
98 takes the receiver as an argument and one that does not. */
99 if (__objc_msg_forward)
101 IMP result;
102 if ((result = __objc_msg_forward (sel)) != NULL)
103 return result;
106 /* In all other cases, use the default forwarding functions built using
107 __builtin_apply and friends. */
109 const char *t = sel->sel_types;
111 if (t && (*t == '[' || *t == '(' || *t == '{')
112 #ifdef OBJC_MAX_STRUCT_BY_VALUE
113 && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
114 #endif
116 return (IMP)__objc_block_forward;
117 else if (t && (*t == 'f' || *t == 'd'))
118 return (IMP)__objc_double_forward;
119 else
120 return (IMP)__objc_word_forward;
124 /* Given a class and selector, return the selector's implementation. */
125 inline
127 get_imp (Class class, SEL sel)
129 /* In a vanilla implementation we would first check if the dispatch
130 table is installed. Here instead, to get more speed in the
131 standard case (that the dispatch table is installed) we first try
132 to get the imp using brute force. Only if that fails, we do what
133 we should have been doing from the very beginning, that is, check
134 if the dispatch table needs to be installed, install it if it's
135 not installed, and retrieve the imp from the table if it's
136 installed. */
137 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
138 if (res == 0)
140 /* Not a valid method */
141 if (class->dtable == __objc_uninstalled_dtable)
143 /* The dispatch table needs to be installed. */
144 objc_mutex_lock (__objc_runtime_mutex);
146 /* Double-checked locking pattern: Check
147 __objc_uninstalled_dtable again in case another thread
148 installed the dtable while we were waiting for the lock
149 to be released. */
150 if (class->dtable == __objc_uninstalled_dtable)
152 __objc_install_dispatch_table_for_class (class);
155 objc_mutex_unlock (__objc_runtime_mutex);
156 /* Call ourselves with the installed dispatch table
157 and get the real method */
158 res = get_imp (class, sel);
160 else
162 /* The dispatch table has been installed. */
164 /* Get the method from the dispatch table (we try to get it
165 again in case another thread has installed the dtable just
166 after we invoked sarray_get_safe, but before we checked
167 class->dtable == __objc_uninstalled_dtable).
169 res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
170 if (res == 0)
172 /* The dispatch table has been installed, and the method
173 is not in the dispatch table. So the method just
174 doesn't exist for the class. Return the forwarding
175 implementation. */
176 res = __objc_get_forward_imp ((id)class, sel);
180 return res;
183 /* Query if an object can respond to a selector, returns YES if the
184 object implements the selector otherwise NO. Does not check if the
185 method can be forwarded. */
186 inline
187 BOOL
188 __objc_responds_to (id object, SEL sel)
190 void *res;
192 /* Install dispatch table if need be */
193 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
195 objc_mutex_lock (__objc_runtime_mutex);
196 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
198 __objc_install_dispatch_table_for_class (object->class_pointer);
200 objc_mutex_unlock (__objc_runtime_mutex);
203 /* Get the method from the dispatch table */
204 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
205 return (res != 0);
208 /* This is the lookup function. All entries in the table are either a
209 valid method *or* zero. If zero then either the dispatch table
210 needs to be installed or it doesn't exist and forwarding is attempted. */
211 inline
213 objc_msg_lookup (id receiver, SEL op)
215 IMP result;
216 if (receiver)
218 result = sarray_get_safe (receiver->class_pointer->dtable,
219 (sidx)op->sel_id);
220 if (result == 0)
222 /* Not a valid method */
223 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
225 /* The dispatch table needs to be installed.
226 This happens on the very first method call to the class. */
227 __objc_init_install_dtable (receiver, op);
229 /* Get real method for this in newly installed dtable */
230 result = get_imp (receiver->class_pointer, op);
232 else
234 /* The dispatch table has been installed. Check again
235 if the method exists (just in case the dispatch table
236 has been installed by another thread after we did the
237 previous check that the method exists).
239 result = sarray_get_safe (receiver->class_pointer->dtable,
240 (sidx)op->sel_id);
241 if (result == 0)
243 /* If the method still just doesn't exist for the
244 class, attempt to forward the method. */
245 result = __objc_get_forward_imp (receiver, op);
249 return result;
251 else
252 return (IMP)nil_method;
256 objc_msg_lookup_super (Super_t super, SEL sel)
258 if (super->self)
259 return get_imp (super->class, sel);
260 else
261 return (IMP)nil_method;
264 int method_get_sizeof_arguments (Method *);
266 retval_t
267 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
269 Method *m = class_get_instance_method (object->class_pointer, op);
270 const char *type;
271 *((id *) method_get_first_argument (m, arg_frame, &type)) = object;
272 *((SEL *) method_get_next_argument (arg_frame, &type)) = op;
273 return __builtin_apply ((apply_t) m->method_imp,
274 arg_frame,
275 method_get_sizeof_arguments (m));
278 void
279 __objc_init_dispatch_tables ()
281 __objc_uninstalled_dtable = sarray_new (200, 0);
284 /* This function is called by objc_msg_lookup when the
285 dispatch table needs to be installed; thus it is called once
286 for each class, namely when the very first message is sent to it. */
287 static void
288 __objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
290 objc_mutex_lock (__objc_runtime_mutex);
292 /* This may happen, if the programmer has taken the address of a
293 method before the dtable was initialized... too bad for him! */
294 if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
296 objc_mutex_unlock (__objc_runtime_mutex);
297 return;
300 if (CLS_ISCLASS (receiver->class_pointer))
302 /* receiver is an ordinary object */
303 assert (CLS_ISCLASS (receiver->class_pointer));
305 /* install instance methods table */
306 __objc_install_dispatch_table_for_class (receiver->class_pointer);
308 /* call +initialize -- this will in turn install the factory
309 dispatch table if not already done :-) */
310 __objc_send_initialize (receiver->class_pointer);
312 else
314 /* receiver is a class object */
315 assert (CLS_ISCLASS ((Class)receiver));
316 assert (CLS_ISMETA (receiver->class_pointer));
318 /* Install real dtable for factory methods */
319 __objc_install_dispatch_table_for_class (receiver->class_pointer);
321 __objc_send_initialize ((Class)receiver);
323 objc_mutex_unlock (__objc_runtime_mutex);
326 /* Install dummy table for class which causes the first message to
327 that class (or instances hereof) to be initialized properly */
328 void
329 __objc_install_premature_dtable (Class class)
331 assert (__objc_uninstalled_dtable);
332 class->dtable = __objc_uninstalled_dtable;
335 /* Send +initialize to class if not already done */
336 static void
337 __objc_send_initialize (Class class)
339 /* This *must* be a class object */
340 assert (CLS_ISCLASS (class));
341 assert (! CLS_ISMETA (class));
343 if (! CLS_ISINITIALIZED (class))
345 CLS_SETINITIALIZED (class);
346 CLS_SETINITIALIZED (class->class_pointer);
348 /* Create the garbage collector type memory description */
349 __objc_generate_gc_type_description (class);
351 if (class->super_class)
352 __objc_send_initialize (class->super_class);
355 SEL op = sel_register_name ("initialize");
356 IMP imp = 0;
357 MethodList_t method_list = class->class_pointer->methods;
359 while (method_list) {
360 int i;
361 Method_t method;
363 for (i = 0; i < method_list->method_count; i++) {
364 method = &(method_list->method_list[i]);
365 if (method->method_name
366 && method->method_name->sel_id == op->sel_id) {
367 imp = method->method_imp;
368 break;
372 if (imp)
373 break;
375 method_list = method_list->method_next;
378 if (imp)
379 (*imp) ((id) class, op);
385 /* Walk on the methods list of class and install the methods in the reverse
386 order of the lists. Since methods added by categories are before the methods
387 of class in the methods list, this allows categories to substitute methods
388 declared in class. However if more than one category replaces the same
389 method nothing is guaranteed about what method will be used.
390 Assumes that __objc_runtime_mutex is locked down. */
391 static void
392 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
394 int i;
396 if (! method_list)
397 return;
399 if (method_list->method_next)
400 __objc_install_methods_in_dtable (class, method_list->method_next);
402 for (i = 0; i < method_list->method_count; i++)
404 Method_t method = &(method_list->method_list[i]);
405 sarray_at_put_safe (class->dtable,
406 (sidx) method->method_name->sel_id,
407 method->method_imp);
411 /* Assumes that __objc_runtime_mutex is locked down. */
412 static void
413 __objc_install_dispatch_table_for_class (Class class)
415 Class super;
417 /* If the class has not yet had its class links resolved, we must
418 re-compute all class links */
419 if (! CLS_ISRESOLV (class))
420 __objc_resolve_class_links ();
422 super = class->super_class;
424 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
425 __objc_install_dispatch_table_for_class (super);
427 /* Allocate dtable if necessary */
428 if (super == 0)
430 objc_mutex_lock (__objc_runtime_mutex);
431 class->dtable = sarray_new (__objc_selector_max_index, 0);
432 objc_mutex_unlock (__objc_runtime_mutex);
434 else
435 class->dtable = sarray_lazy_copy (super->dtable);
437 __objc_install_methods_in_dtable (class, class->methods);
440 void
441 __objc_update_dispatch_table_for_class (Class class)
443 Class next;
444 struct sarray *arr;
446 /* not yet installed -- skip it */
447 if (class->dtable == __objc_uninstalled_dtable)
448 return;
450 objc_mutex_lock (__objc_runtime_mutex);
452 arr = class->dtable;
453 __objc_install_premature_dtable (class); /* someone might require it... */
454 sarray_free (arr); /* release memory */
456 /* could have been lazy... */
457 __objc_install_dispatch_table_for_class (class);
459 if (class->subclass_list) /* Traverse subclasses */
460 for (next = class->subclass_list; next; next = next->sibling_class)
461 __objc_update_dispatch_table_for_class (next);
463 objc_mutex_unlock (__objc_runtime_mutex);
467 /* This function adds a method list to a class. This function is
468 typically called by another function specific to the run-time. As
469 such this function does not worry about thread safe issues.
471 This one is only called for categories. Class objects have their
472 methods installed right away, and their selectors are made into
473 SEL's by the function __objc_register_selectors_from_class. */
474 void
475 class_add_method_list (Class class, MethodList_t list)
477 /* Passing of a linked list is not allowed. Do multiple calls. */
478 assert (! list->method_next);
480 __objc_register_selectors_from_list(list);
482 /* Add the methods to the class's method list. */
483 list->method_next = class->methods;
484 class->methods = list;
486 /* Update the dispatch table of class */
487 __objc_update_dispatch_table_for_class (class);
490 Method_t
491 class_get_instance_method (Class class, SEL op)
493 return search_for_method_in_hierarchy (class, op);
496 Method_t
497 class_get_class_method (MetaClass class, SEL op)
499 return search_for_method_in_hierarchy (class, op);
503 /* Search for a method starting from the current class up its hierarchy.
504 Return a pointer to the method's method structure if found. NULL
505 otherwise. */
507 static Method_t
508 search_for_method_in_hierarchy (Class cls, SEL sel)
510 Method_t method = NULL;
511 Class class;
513 if (! sel_is_mapped (sel))
514 return NULL;
516 /* Scan the method list of the class. If the method isn't found in the
517 list then step to its super class. */
518 for (class = cls; ((! method) && class); class = class->super_class)
519 method = search_for_method_in_list (class->methods, sel);
521 return method;
526 /* Given a linked list of method and a method's name. Search for the named
527 method's method structure. Return a pointer to the method's method
528 structure if found. NULL otherwise. */
529 Method_t
530 search_for_method_in_list (MethodList_t list, SEL op)
532 MethodList_t method_list = list;
534 if (! sel_is_mapped (op))
535 return NULL;
537 /* If not found then we'll search the list. */
538 while (method_list)
540 int i;
542 /* Search the method list. */
543 for (i = 0; i < method_list->method_count; ++i)
545 Method_t method = &method_list->method_list[i];
547 if (method->method_name)
548 if (method->method_name->sel_id == op->sel_id)
549 return method;
552 /* The method wasn't found. Follow the link to the next list of
553 methods. */
554 method_list = method_list->method_next;
557 return NULL;
560 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
562 /* Forwarding pointers/integers through the normal registers */
563 static id
564 __objc_word_forward (id rcv, SEL op, ...)
566 void *args, *res;
568 args = __builtin_apply_args ();
569 res = __objc_forward (rcv, op, args);
570 if (res)
571 __builtin_return (res);
572 else
573 return res;
576 /* Specific routine for forwarding floats/double because of
577 architectural differences on some processors. i386s for
578 example which uses a floating point stack versus general
579 registers for floating point numbers. This forward routine
580 makes sure that GCC restores the proper return values */
581 static double
582 __objc_double_forward (id rcv, SEL op, ...)
584 void *args, *res;
586 args = __builtin_apply_args ();
587 res = __objc_forward (rcv, op, args);
588 __builtin_return (res);
591 #if INVISIBLE_STRUCT_RETURN
592 static __big
593 #else
594 static id
595 #endif
596 __objc_block_forward (id rcv, SEL op, ...)
598 void *args, *res;
600 args = __builtin_apply_args ();
601 res = __objc_forward (rcv, op, args);
602 if (res)
603 __builtin_return (res);
604 else
605 #if INVISIBLE_STRUCT_RETURN
606 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
607 #else
608 return nil;
609 #endif
613 /* This function is installed in the dispatch table for all methods which are
614 not implemented. Thus, it is called when a selector is not recognized. */
615 static retval_t
616 __objc_forward (id object, SEL sel, arglist_t args)
618 IMP imp;
619 static SEL frwd_sel = 0; /* !T:SAFE2 */
620 SEL err_sel;
622 /* first try if the object understands forward:: */
623 if (! frwd_sel)
624 frwd_sel = sel_get_any_uid ("forward::");
626 if (__objc_responds_to (object, frwd_sel))
628 imp = get_imp (object->class_pointer, frwd_sel);
629 return (*imp) (object, frwd_sel, sel, args);
632 /* If the object recognizes the doesNotRecognize: method then we're going
633 to send it. */
634 err_sel = sel_get_any_uid ("doesNotRecognize:");
635 if (__objc_responds_to (object, err_sel))
637 imp = get_imp (object->class_pointer, err_sel);
638 return (*imp) (object, err_sel, sel);
641 /* The object doesn't recognize the method. Check for responding to
642 error:. If it does then sent it. */
644 char msg[256 + strlen ((const char *) sel_get_name (sel))
645 + strlen ((const char *) object->class_pointer->name)];
647 sprintf (msg, "(%s) %s does not recognize %s",
648 (CLS_ISMETA (object->class_pointer)
649 ? "class"
650 : "instance" ),
651 object->class_pointer->name, sel_get_name (sel));
653 err_sel = sel_get_any_uid ("error:");
654 if (__objc_responds_to (object, err_sel))
656 imp = get_imp (object->class_pointer, err_sel);
657 return (*imp) (object, sel_get_any_uid ("error:"), msg);
660 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
661 a default action is taken. */
662 objc_error (object, OBJC_ERR_UNIMPLEMENTED, "%s\n", msg);
664 return 0;
668 void
669 __objc_print_dtable_stats ()
671 int total = 0;
673 objc_mutex_lock (__objc_runtime_mutex);
675 #ifdef OBJC_SPARSE2
676 printf ("memory usage: (%s)\n", "2-level sparse arrays");
677 #else
678 printf ("memory usage: (%s)\n", "3-level sparse arrays");
679 #endif
681 printf ("arrays: %d = %ld bytes\n", narrays,
682 (long) narrays * sizeof (struct sarray));
683 total += narrays * sizeof (struct sarray);
684 printf ("buckets: %d = %ld bytes\n", nbuckets,
685 (long) nbuckets * sizeof (struct sbucket));
686 total += nbuckets * sizeof (struct sbucket);
688 printf ("idxtables: %d = %ld bytes\n",
689 idxsize, (long) idxsize * sizeof (void *));
690 total += idxsize * sizeof (void *);
691 printf ("-----------------------------------\n");
692 printf ("total: %d bytes\n", total);
693 printf ("===================================\n");
695 objc_mutex_unlock (__objc_runtime_mutex);
698 /* Returns the uninstalled dispatch table indicator.
699 If a class' dispatch table points to __objc_uninstalled_dtable
700 then that means it needs its dispatch table to be installed. */
701 inline
702 struct sarray *
703 objc_get_uninstalled_dtable ()
705 return __objc_uninstalled_dtable;