Sync usage with man page.
[netbsd-mini2440.git] / gnu / dist / gcc4 / libobjc / sendmsg.c
blob63930c8d36403f00a42e032449c65dd6a00c4e82
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 #undef rtx
45 #define rtx int
47 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
48 #define INVISIBLE_STRUCT_RETURN 1
49 #else
50 #define INVISIBLE_STRUCT_RETURN 0
51 #endif
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
79 static __big
80 #else
81 static id
82 #endif
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. */
89 inline
90 IMP
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)
97 IMP result;
98 if ((result = __objc_msg_forward (sel)) != NULL)
99 return result;
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
110 #endif
112 return (IMP)__objc_block_forward;
113 else if (t && (*t == 'f' || *t == 'd'))
114 return (IMP)__objc_double_forward;
115 else
116 return (IMP)__objc_word_forward;
120 /* Given a class and selector, return the selector's implementation. */
121 inline
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
132 installed. */
133 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
134 if (res == 0)
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
145 to be released. */
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);
156 else
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);
166 if (res == 0)
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
171 implementation. */
172 res = __objc_get_forward_imp (sel);
176 return res;
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. */
182 inline
183 BOOL
184 __objc_responds_to (id object, SEL sel)
186 void *res;
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);
201 return (res != 0);
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. */
207 inline
209 objc_msg_lookup (id receiver, SEL op)
211 IMP result;
212 if (receiver)
214 result = sarray_get_safe (receiver->class_pointer->dtable,
215 (sidx)op->sel_id);
216 if (result == 0)
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);
228 else
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,
236 (sidx)op->sel_id);
237 if (result == 0)
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);
245 return result;
247 else
248 return (IMP)nil_method;
252 objc_msg_lookup_super (Super_t super, SEL sel)
254 if (super->self)
255 return get_imp (super->class, sel);
256 else
257 return (IMP)nil_method;
260 int method_get_sizeof_arguments (Method *);
262 retval_t
263 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
265 Method *m = class_get_instance_method (object->class_pointer, op);
266 const char *type;
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,
270 arg_frame,
271 method_get_sizeof_arguments (m));
274 void
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. */
283 static void
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);
293 return;
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);
308 else
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 */
324 void
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 */
332 static void
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");
352 IMP imp = 0;
353 MethodList_t method_list = class->class_pointer->methods;
355 while (method_list) {
356 int i;
357 Method_t method;
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;
364 break;
368 if (imp)
369 break;
371 method_list = method_list->method_next;
374 if (imp)
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. */
387 static void
388 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
390 int i;
392 if (! method_list)
393 return;
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,
403 method->method_imp);
407 /* Assumes that __objc_runtime_mutex is locked down. */
408 static void
409 __objc_install_dispatch_table_for_class (Class class)
411 Class super;
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 */
424 if (super == 0)
426 objc_mutex_lock (__objc_runtime_mutex);
427 class->dtable = sarray_new (__objc_selector_max_index, 0);
428 objc_mutex_unlock (__objc_runtime_mutex);
430 else
431 class->dtable = sarray_lazy_copy (super->dtable);
433 __objc_install_methods_in_dtable (class, class->methods);
436 void
437 __objc_update_dispatch_table_for_class (Class class)
439 Class next;
440 struct sarray *arr;
442 /* not yet installed -- skip it */
443 if (class->dtable == __objc_uninstalled_dtable)
444 return;
446 objc_mutex_lock (__objc_runtime_mutex);
448 arr = class->dtable;
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. */
470 void
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);
486 Method_t
487 class_get_instance_method (Class class, SEL op)
489 return search_for_method_in_hierarchy (class, op);
492 Method_t
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
501 otherwise. */
503 static Method_t
504 search_for_method_in_hierarchy (Class cls, SEL sel)
506 Method_t method = NULL;
507 Class class;
509 if (! sel_is_mapped (sel))
510 return NULL;
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);
517 return method;
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. */
525 Method_t
526 search_for_method_in_list (MethodList_t list, SEL op)
528 MethodList_t method_list = list;
530 if (! sel_is_mapped (op))
531 return NULL;
533 /* If not found then we'll search the list. */
534 while (method_list)
536 int i;
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)
545 return method;
548 /* The method wasn't found. Follow the link to the next list of
549 methods. */
550 method_list = method_list->method_next;
553 return NULL;
556 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
558 /* Forwarding pointers/integers through the normal registers */
559 static id
560 __objc_word_forward (id rcv, SEL op, ...)
562 void *args, *res;
564 args = __builtin_apply_args ();
565 res = __objc_forward (rcv, op, args);
566 if (res)
567 __builtin_return (res);
568 else
569 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 */
577 static double
578 __objc_double_forward (id rcv, SEL op, ...)
580 void *args, *res;
582 args = __builtin_apply_args ();
583 res = __objc_forward (rcv, op, args);
584 __builtin_return (res);
587 #if INVISIBLE_STRUCT_RETURN
588 static __big
589 #else
590 static id
591 #endif
592 __objc_block_forward (id rcv, SEL op, ...)
594 void *args, *res;
596 args = __builtin_apply_args ();
597 res = __objc_forward (rcv, op, args);
598 if (res)
599 __builtin_return (res);
600 else
601 #if INVISIBLE_STRUCT_RETURN
602 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
603 #else
604 return nil;
605 #endif
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. */
611 static retval_t
612 __objc_forward (id object, SEL sel, arglist_t args)
614 IMP imp;
615 static SEL frwd_sel = 0; /* !T:SAFE2 */
616 SEL err_sel;
618 /* first try if the object understands forward:: */
619 if (! frwd_sel)
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
629 to send it. */
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)
645 ? "class"
646 : "instance" ),
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);
660 return 0;
664 void
665 __objc_print_dtable_stats ()
667 int total = 0;
669 objc_mutex_lock (__objc_runtime_mutex);
671 #ifdef OBJC_SPARSE2
672 printf ("memory usage: (%s)\n", "2-level sparse arrays");
673 #else
674 printf ("memory usage: (%s)\n", "3-level sparse arrays");
675 #endif
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. */
697 inline
698 struct sarray *
699 objc_get_uninstalled_dtable ()
701 return __objc_uninstalled_dtable;