contrib.texi: Update contributions.
[official-gcc.git] / libobjc / sendmsg.c
blob42d3e02af9fc62c6c4060c00488893ee05b495c1
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 /* Alread defined in gcc/coretypes.h. So prevent double definition warning. */
45 #undef rtx
46 #define rtx int
48 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
49 #define INVISIBLE_STRUCT_RETURN 1
50 #else
51 #define INVISIBLE_STRUCT_RETURN 0
52 #endif
54 /* The uninstalled dispatch table */
55 struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
57 /* Two hooks for method forwarding. If either is set, it is invoked
58 * to return a function that performs the real forwarding. If both
59 * are set, the result of __objc_msg_forward2 will be preferred over
60 * that of __objc_msg_forward. If both return NULL or are unset,
61 * the libgcc based functions (__builtin_apply and friends) are
62 * used.
64 IMP (*__objc_msg_forward) (SEL) = NULL;
65 IMP (*__objc_msg_forward2) (id, SEL) = NULL;
67 /* Send +initialize to class */
68 static void __objc_send_initialize (Class);
70 static void __objc_install_dispatch_table_for_class (Class);
72 /* Forward declare some functions */
73 static void __objc_init_install_dtable (id, SEL);
75 /* Various forwarding functions that are used based upon the
76 return type for the selector.
77 __objc_block_forward for structures.
78 __objc_double_forward for floats/doubles.
79 __objc_word_forward for pointers or types that fit in registers. */
80 static double __objc_double_forward (id, SEL, ...);
81 static id __objc_word_forward (id, SEL, ...);
82 typedef struct { id many[8]; } __big;
83 #if INVISIBLE_STRUCT_RETURN
84 static __big
85 #else
86 static id
87 #endif
88 __objc_block_forward (id, SEL, ...);
89 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
90 Method_t search_for_method_in_list (MethodList_t list, SEL op);
91 id nil_method (id, SEL);
93 /* Given a selector, return the proper forwarding implementation. */
94 inline
95 IMP
96 __objc_get_forward_imp (id rcv, SEL sel)
98 /* If a custom forwarding hook was registered, try getting a forwarding
99 function from it. There are two forward routine hooks, one that
100 takes the receiver as an argument and one that does not. */
101 if (__objc_msg_forward2)
103 IMP result;
104 if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
105 return result;
107 if (__objc_msg_forward)
109 IMP result;
110 if ((result = __objc_msg_forward (sel)) != NULL)
111 return result;
114 /* In all other cases, use the default forwarding functions built using
115 __builtin_apply and friends. */
117 const char *t = sel->sel_types;
119 if (t && (*t == '[' || *t == '(' || *t == '{')
120 #ifdef OBJC_MAX_STRUCT_BY_VALUE
121 && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
122 #endif
124 return (IMP)__objc_block_forward;
125 else if (t && (*t == 'f' || *t == 'd'))
126 return (IMP)__objc_double_forward;
127 else
128 return (IMP)__objc_word_forward;
132 /* Given a class and selector, return the selector's implementation. */
133 inline
135 get_imp (Class class, SEL sel)
137 /* In a vanilla implementation we would first check if the dispatch
138 table is installed. Here instead, to get more speed in the
139 standard case (that the dispatch table is installed) we first try
140 to get the imp using brute force. Only if that fails, we do what
141 we should have been doing from the very beginning, that is, check
142 if the dispatch table needs to be installed, install it if it's
143 not installed, and retrieve the imp from the table if it's
144 installed. */
145 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
146 if (res == 0)
148 /* Not a valid method */
149 if (class->dtable == __objc_uninstalled_dtable)
151 /* The dispatch table needs to be installed. */
152 objc_mutex_lock (__objc_runtime_mutex);
154 /* Double-checked locking pattern: Check
155 __objc_uninstalled_dtable again in case another thread
156 installed the dtable while we were waiting for the lock
157 to be released. */
158 if (class->dtable == __objc_uninstalled_dtable)
160 __objc_install_dispatch_table_for_class (class);
163 objc_mutex_unlock (__objc_runtime_mutex);
164 /* Call ourselves with the installed dispatch table
165 and get the real method */
166 res = get_imp (class, sel);
168 else
170 /* The dispatch table has been installed. */
172 /* Get the method from the dispatch table (we try to get it
173 again in case another thread has installed the dtable just
174 after we invoked sarray_get_safe, but before we checked
175 class->dtable == __objc_uninstalled_dtable).
177 res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
178 if (res == 0)
180 /* The dispatch table has been installed, and the method
181 is not in the dispatch table. So the method just
182 doesn't exist for the class. Return the forwarding
183 implementation. */
184 res = __objc_get_forward_imp ((id)class, sel);
188 return res;
191 /* Query if an object can respond to a selector, returns YES if the
192 object implements the selector otherwise NO. Does not check if the
193 method can be forwarded. */
194 inline
195 BOOL
196 __objc_responds_to (id object, SEL sel)
198 void *res;
200 /* Install dispatch table if need be */
201 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
203 objc_mutex_lock (__objc_runtime_mutex);
204 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
206 __objc_install_dispatch_table_for_class (object->class_pointer);
208 objc_mutex_unlock (__objc_runtime_mutex);
211 /* Get the method from the dispatch table */
212 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
213 return (res != 0);
216 /* This is the lookup function. All entries in the table are either a
217 valid method *or* zero. If zero then either the dispatch table
218 needs to be installed or it doesn't exist and forwarding is attempted. */
219 inline
221 objc_msg_lookup (id receiver, SEL op)
223 IMP result;
224 if (receiver)
226 result = sarray_get_safe (receiver->class_pointer->dtable,
227 (sidx)op->sel_id);
228 if (result == 0)
230 /* Not a valid method */
231 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
233 /* The dispatch table needs to be installed.
234 This happens on the very first method call to the class. */
235 __objc_init_install_dtable (receiver, op);
237 /* Get real method for this in newly installed dtable */
238 result = get_imp (receiver->class_pointer, op);
240 else
242 /* The dispatch table has been installed. Check again
243 if the method exists (just in case the dispatch table
244 has been installed by another thread after we did the
245 previous check that the method exists).
247 result = sarray_get_safe (receiver->class_pointer->dtable,
248 (sidx)op->sel_id);
249 if (result == 0)
251 /* If the method still just doesn't exist for the
252 class, attempt to forward the method. */
253 result = __objc_get_forward_imp (receiver, op);
257 return result;
259 else
260 return (IMP)nil_method;
264 objc_msg_lookup_super (Super_t super, SEL sel)
266 if (super->self)
267 return get_imp (super->class, sel);
268 else
269 return (IMP)nil_method;
272 int method_get_sizeof_arguments (Method *);
274 retval_t
275 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
277 Method *m = class_get_instance_method (object->class_pointer, op);
278 const char *type;
279 *((id *) method_get_first_argument (m, arg_frame, &type)) = object;
280 *((SEL *) method_get_next_argument (arg_frame, &type)) = op;
281 return __builtin_apply ((apply_t) m->method_imp,
282 arg_frame,
283 method_get_sizeof_arguments (m));
286 void
287 __objc_init_dispatch_tables ()
289 __objc_uninstalled_dtable = sarray_new (200, 0);
292 /* This function is called by objc_msg_lookup when the
293 dispatch table needs to be installed; thus it is called once
294 for each class, namely when the very first message is sent to it. */
295 static void
296 __objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
298 objc_mutex_lock (__objc_runtime_mutex);
300 /* This may happen, if the programmer has taken the address of a
301 method before the dtable was initialized... too bad for him! */
302 if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
304 objc_mutex_unlock (__objc_runtime_mutex);
305 return;
308 if (CLS_ISCLASS (receiver->class_pointer))
310 /* receiver is an ordinary object */
311 assert (CLS_ISCLASS (receiver->class_pointer));
313 /* install instance methods table */
314 __objc_install_dispatch_table_for_class (receiver->class_pointer);
316 /* call +initialize -- this will in turn install the factory
317 dispatch table if not already done :-) */
318 __objc_send_initialize (receiver->class_pointer);
320 else
322 /* receiver is a class object */
323 assert (CLS_ISCLASS ((Class)receiver));
324 assert (CLS_ISMETA (receiver->class_pointer));
326 /* Install real dtable for factory methods */
327 __objc_install_dispatch_table_for_class (receiver->class_pointer);
329 __objc_send_initialize ((Class)receiver);
331 objc_mutex_unlock (__objc_runtime_mutex);
334 /* Install dummy table for class which causes the first message to
335 that class (or instances hereof) to be initialized properly */
336 void
337 __objc_install_premature_dtable (Class class)
339 assert (__objc_uninstalled_dtable);
340 class->dtable = __objc_uninstalled_dtable;
343 /* Send +initialize to class if not already done */
344 static void
345 __objc_send_initialize (Class class)
347 /* This *must* be a class object */
348 assert (CLS_ISCLASS (class));
349 assert (! CLS_ISMETA (class));
351 if (! CLS_ISINITIALIZED (class))
353 CLS_SETINITIALIZED (class);
354 CLS_SETINITIALIZED (class->class_pointer);
356 /* Create the garbage collector type memory description */
357 __objc_generate_gc_type_description (class);
359 if (class->super_class)
360 __objc_send_initialize (class->super_class);
363 SEL op = sel_register_name ("initialize");
364 IMP imp = 0;
365 MethodList_t method_list = class->class_pointer->methods;
367 while (method_list) {
368 int i;
369 Method_t method;
371 for (i = 0; i < method_list->method_count; i++) {
372 method = &(method_list->method_list[i]);
373 if (method->method_name
374 && method->method_name->sel_id == op->sel_id) {
375 imp = method->method_imp;
376 break;
380 if (imp)
381 break;
383 method_list = method_list->method_next;
386 if (imp)
387 (*imp) ((id) class, op);
393 /* Walk on the methods list of class and install the methods in the reverse
394 order of the lists. Since methods added by categories are before the methods
395 of class in the methods list, this allows categories to substitute methods
396 declared in class. However if more than one category replaces the same
397 method nothing is guaranteed about what method will be used.
398 Assumes that __objc_runtime_mutex is locked down. */
399 static void
400 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
402 int i;
404 if (! method_list)
405 return;
407 if (method_list->method_next)
408 __objc_install_methods_in_dtable (class, method_list->method_next);
410 for (i = 0; i < method_list->method_count; i++)
412 Method_t method = &(method_list->method_list[i]);
413 sarray_at_put_safe (class->dtable,
414 (sidx) method->method_name->sel_id,
415 method->method_imp);
419 /* Assumes that __objc_runtime_mutex is locked down. */
420 static void
421 __objc_install_dispatch_table_for_class (Class class)
423 Class super;
425 /* If the class has not yet had its class links resolved, we must
426 re-compute all class links */
427 if (! CLS_ISRESOLV (class))
428 __objc_resolve_class_links ();
430 super = class->super_class;
432 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
433 __objc_install_dispatch_table_for_class (super);
435 /* Allocate dtable if necessary */
436 if (super == 0)
438 objc_mutex_lock (__objc_runtime_mutex);
439 class->dtable = sarray_new (__objc_selector_max_index, 0);
440 objc_mutex_unlock (__objc_runtime_mutex);
442 else
443 class->dtable = sarray_lazy_copy (super->dtable);
445 __objc_install_methods_in_dtable (class, class->methods);
448 void
449 __objc_update_dispatch_table_for_class (Class class)
451 Class next;
452 struct sarray *arr;
454 /* not yet installed -- skip it */
455 if (class->dtable == __objc_uninstalled_dtable)
456 return;
458 objc_mutex_lock (__objc_runtime_mutex);
460 arr = class->dtable;
461 __objc_install_premature_dtable (class); /* someone might require it... */
462 sarray_free (arr); /* release memory */
464 /* could have been lazy... */
465 __objc_install_dispatch_table_for_class (class);
467 if (class->subclass_list) /* Traverse subclasses */
468 for (next = class->subclass_list; next; next = next->sibling_class)
469 __objc_update_dispatch_table_for_class (next);
471 objc_mutex_unlock (__objc_runtime_mutex);
475 /* This function adds a method list to a class. This function is
476 typically called by another function specific to the run-time. As
477 such this function does not worry about thread safe issues.
479 This one is only called for categories. Class objects have their
480 methods installed right away, and their selectors are made into
481 SEL's by the function __objc_register_selectors_from_class. */
482 void
483 class_add_method_list (Class class, MethodList_t list)
485 /* Passing of a linked list is not allowed. Do multiple calls. */
486 assert (! list->method_next);
488 __objc_register_selectors_from_list(list);
490 /* Add the methods to the class's method list. */
491 list->method_next = class->methods;
492 class->methods = list;
494 /* Update the dispatch table of class */
495 __objc_update_dispatch_table_for_class (class);
498 Method_t
499 class_get_instance_method (Class class, SEL op)
501 return search_for_method_in_hierarchy (class, op);
504 Method_t
505 class_get_class_method (MetaClass class, SEL op)
507 return search_for_method_in_hierarchy (class, op);
511 /* Search for a method starting from the current class up its hierarchy.
512 Return a pointer to the method's method structure if found. NULL
513 otherwise. */
515 static Method_t
516 search_for_method_in_hierarchy (Class cls, SEL sel)
518 Method_t method = NULL;
519 Class class;
521 if (! sel_is_mapped (sel))
522 return NULL;
524 /* Scan the method list of the class. If the method isn't found in the
525 list then step to its super class. */
526 for (class = cls; ((! method) && class); class = class->super_class)
527 method = search_for_method_in_list (class->methods, sel);
529 return method;
534 /* Given a linked list of method and a method's name. Search for the named
535 method's method structure. Return a pointer to the method's method
536 structure if found. NULL otherwise. */
537 Method_t
538 search_for_method_in_list (MethodList_t list, SEL op)
540 MethodList_t method_list = list;
542 if (! sel_is_mapped (op))
543 return NULL;
545 /* If not found then we'll search the list. */
546 while (method_list)
548 int i;
550 /* Search the method list. */
551 for (i = 0; i < method_list->method_count; ++i)
553 Method_t method = &method_list->method_list[i];
555 if (method->method_name)
556 if (method->method_name->sel_id == op->sel_id)
557 return method;
560 /* The method wasn't found. Follow the link to the next list of
561 methods. */
562 method_list = method_list->method_next;
565 return NULL;
568 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
570 /* Forwarding pointers/integers through the normal registers */
571 static id
572 __objc_word_forward (id rcv, SEL op, ...)
574 void *args, *res;
576 args = __builtin_apply_args ();
577 res = __objc_forward (rcv, op, args);
578 if (res)
579 __builtin_return (res);
580 else
581 return res;
584 /* Specific routine for forwarding floats/double because of
585 architectural differences on some processors. i386s for
586 example which uses a floating point stack versus general
587 registers for floating point numbers. This forward routine
588 makes sure that GCC restores the proper return values */
589 static double
590 __objc_double_forward (id rcv, SEL op, ...)
592 void *args, *res;
594 args = __builtin_apply_args ();
595 res = __objc_forward (rcv, op, args);
596 __builtin_return (res);
599 #if INVISIBLE_STRUCT_RETURN
600 static __big
601 #else
602 static id
603 #endif
604 __objc_block_forward (id rcv, SEL op, ...)
606 void *args, *res;
608 args = __builtin_apply_args ();
609 res = __objc_forward (rcv, op, args);
610 if (res)
611 __builtin_return (res);
612 else
613 #if INVISIBLE_STRUCT_RETURN
614 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
615 #else
616 return nil;
617 #endif
621 /* This function is installed in the dispatch table for all methods which are
622 not implemented. Thus, it is called when a selector is not recognized. */
623 static retval_t
624 __objc_forward (id object, SEL sel, arglist_t args)
626 IMP imp;
627 static SEL frwd_sel = 0; /* !T:SAFE2 */
628 SEL err_sel;
630 /* first try if the object understands forward:: */
631 if (! frwd_sel)
632 frwd_sel = sel_get_any_uid ("forward::");
634 if (__objc_responds_to (object, frwd_sel))
636 imp = get_imp (object->class_pointer, frwd_sel);
637 return (*imp) (object, frwd_sel, sel, args);
640 /* If the object recognizes the doesNotRecognize: method then we're going
641 to send it. */
642 err_sel = sel_get_any_uid ("doesNotRecognize:");
643 if (__objc_responds_to (object, err_sel))
645 imp = get_imp (object->class_pointer, err_sel);
646 return (*imp) (object, err_sel, sel);
649 /* The object doesn't recognize the method. Check for responding to
650 error:. If it does then sent it. */
652 char msg[256 + strlen ((const char *) sel_get_name (sel))
653 + strlen ((const char *) object->class_pointer->name)];
655 sprintf (msg, "(%s) %s does not recognize %s",
656 (CLS_ISMETA (object->class_pointer)
657 ? "class"
658 : "instance" ),
659 object->class_pointer->name, sel_get_name (sel));
661 err_sel = sel_get_any_uid ("error:");
662 if (__objc_responds_to (object, err_sel))
664 imp = get_imp (object->class_pointer, err_sel);
665 return (*imp) (object, sel_get_any_uid ("error:"), msg);
668 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
669 a default action is taken. */
670 objc_error (object, OBJC_ERR_UNIMPLEMENTED, "%s\n", msg);
672 return 0;
676 void
677 __objc_print_dtable_stats ()
679 int total = 0;
681 objc_mutex_lock (__objc_runtime_mutex);
683 #ifdef OBJC_SPARSE2
684 printf ("memory usage: (%s)\n", "2-level sparse arrays");
685 #else
686 printf ("memory usage: (%s)\n", "3-level sparse arrays");
687 #endif
689 printf ("arrays: %d = %ld bytes\n", narrays,
690 (long) ((size_t) narrays * sizeof (struct sarray)));
691 total += narrays * sizeof (struct sarray);
692 printf ("buckets: %d = %ld bytes\n", nbuckets,
693 (long) ((size_t) nbuckets * sizeof (struct sbucket)));
694 total += nbuckets * sizeof (struct sbucket);
696 printf ("idxtables: %d = %ld bytes\n",
697 idxsize, (long) ((size_t) idxsize * sizeof (void *)));
698 total += idxsize * sizeof (void *);
699 printf ("-----------------------------------\n");
700 printf ("total: %d bytes\n", total);
701 printf ("===================================\n");
703 objc_mutex_unlock (__objc_runtime_mutex);
706 /* Returns the uninstalled dispatch table indicator.
707 If a class' dispatch table points to __objc_uninstalled_dtable
708 then that means it needs its dispatch table to be installed. */
709 inline
710 struct sarray *
711 objc_get_uninstalled_dtable ()
713 return __objc_uninstalled_dtable;