archive.c: Do not include objc/objc.h.
[official-gcc.git] / libobjc / sendmsg.c
blob02be39c5b0528d3e70d8e3ac64253d6cdd7dacaa
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
15 details.
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
29 and friends. */
31 #include "objc-private/common.h"
32 #include "objc-private/error.h"
33 #include "tconfig.h"
34 #include "coretypes.h"
35 #include "tm.h"
36 #include "objc/objc-api.h"
37 #include "objc/thr.h"
38 #include "objc-private/runtime.h"
39 #include "objc-private/sarray.h"
40 #include "objc/encoding.h"
41 #include "runtime-info.h"
42 #include <assert.h> /* For assert */
43 #include <string.h> /* For strlen */
45 /* This is how we hack STRUCT_VALUE to be 1 or 0. */
46 #define gen_rtx(args...) 1
47 #define gen_rtx_MEM(args...) 1
48 #define gen_rtx_REG(args...) 1
49 /* Alread defined in gcc/coretypes.h. So prevent double definition warning. */
50 #undef rtx
51 #define rtx int
53 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
54 #define INVISIBLE_STRUCT_RETURN 1
55 #else
56 #define INVISIBLE_STRUCT_RETURN 0
57 #endif
59 /* The uninstalled dispatch table */
60 struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
62 /* Two hooks for method forwarding. If either is set, it is invoked
63 * to return a function that performs the real forwarding. If both
64 * are set, the result of __objc_msg_forward2 will be preferred over
65 * that of __objc_msg_forward. If both return NULL or are unset,
66 * the libgcc based functions (__builtin_apply and friends) are
67 * used.
69 IMP (*__objc_msg_forward) (SEL) = NULL;
70 IMP (*__objc_msg_forward2) (id, SEL) = NULL;
72 /* Send +initialize to class */
73 static void __objc_send_initialize (Class);
75 static void __objc_install_dispatch_table_for_class (Class);
77 /* Forward declare some functions */
78 static void __objc_init_install_dtable (id, SEL);
80 /* Various forwarding functions that are used based upon the
81 return type for the selector.
82 __objc_block_forward for structures.
83 __objc_double_forward for floats/doubles.
84 __objc_word_forward for pointers or types that fit in registers. */
85 static double __objc_double_forward (id, SEL, ...);
86 static id __objc_word_forward (id, SEL, ...);
87 typedef struct { id many[8]; } __big;
88 #if INVISIBLE_STRUCT_RETURN
89 static __big
90 #else
91 static id
92 #endif
93 __objc_block_forward (id, SEL, ...);
94 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
95 Method_t search_for_method_in_list (MethodList_t list, SEL op);
96 id nil_method (id, SEL);
98 /* Given a selector, return the proper forwarding implementation. */
99 inline
101 __objc_get_forward_imp (id rcv, SEL sel)
103 /* If a custom forwarding hook was registered, try getting a forwarding
104 function from it. There are two forward routine hooks, one that
105 takes the receiver as an argument and one that does not. */
106 if (__objc_msg_forward2)
108 IMP result;
109 if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
110 return result;
112 if (__objc_msg_forward)
114 IMP result;
115 if ((result = __objc_msg_forward (sel)) != NULL)
116 return result;
119 /* In all other cases, use the default forwarding functions built using
120 __builtin_apply and friends. */
122 const char *t = sel->sel_types;
124 if (t && (*t == '[' || *t == '(' || *t == '{')
125 #ifdef OBJC_MAX_STRUCT_BY_VALUE
126 && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
127 #endif
129 return (IMP)__objc_block_forward;
130 else if (t && (*t == 'f' || *t == 'd'))
131 return (IMP)__objc_double_forward;
132 else
133 return (IMP)__objc_word_forward;
137 /* Given a class and selector, return the selector's implementation. */
138 inline
140 get_imp (Class class, SEL sel)
142 /* In a vanilla implementation we would first check if the dispatch
143 table is installed. Here instead, to get more speed in the
144 standard case (that the dispatch table is installed) we first try
145 to get the imp using brute force. Only if that fails, we do what
146 we should have been doing from the very beginning, that is, check
147 if the dispatch table needs to be installed, install it if it's
148 not installed, and retrieve the imp from the table if it's
149 installed. */
150 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
151 if (res == 0)
153 /* Not a valid method */
154 if (class->dtable == __objc_uninstalled_dtable)
156 /* The dispatch table needs to be installed. */
157 objc_mutex_lock (__objc_runtime_mutex);
159 /* Double-checked locking pattern: Check
160 __objc_uninstalled_dtable again in case another thread
161 installed the dtable while we were waiting for the lock
162 to be released. */
163 if (class->dtable == __objc_uninstalled_dtable)
165 __objc_install_dispatch_table_for_class (class);
168 objc_mutex_unlock (__objc_runtime_mutex);
169 /* Call ourselves with the installed dispatch table
170 and get the real method */
171 res = get_imp (class, sel);
173 else
175 /* The dispatch table has been installed. */
177 /* Get the method from the dispatch table (we try to get it
178 again in case another thread has installed the dtable just
179 after we invoked sarray_get_safe, but before we checked
180 class->dtable == __objc_uninstalled_dtable).
182 res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
183 if (res == 0)
185 /* The dispatch table has been installed, and the method
186 is not in the dispatch table. So the method just
187 doesn't exist for the class. Return the forwarding
188 implementation. */
189 res = __objc_get_forward_imp ((id)class, sel);
193 return res;
196 /* Given a method, return its implementation. */
198 method_get_imp (Method_t method)
200 return (method != (Method_t)0) ? method->method_imp : (IMP)0;
203 /* Query if an object can respond to a selector, returns YES if the
204 object implements the selector otherwise NO. Does not check if the
205 method can be forwarded. */
206 inline
207 BOOL
208 __objc_responds_to (id object, SEL sel)
210 void *res;
212 /* Install dispatch table if need be */
213 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
215 objc_mutex_lock (__objc_runtime_mutex);
216 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
218 __objc_install_dispatch_table_for_class (object->class_pointer);
220 objc_mutex_unlock (__objc_runtime_mutex);
223 /* Get the method from the dispatch table */
224 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
225 return (res != 0);
228 /* This is the lookup function. All entries in the table are either a
229 valid method *or* zero. If zero then either the dispatch table
230 needs to be installed or it doesn't exist and forwarding is attempted. */
233 objc_msg_lookup (id receiver, SEL op)
235 IMP result;
236 if (receiver)
238 result = sarray_get_safe (receiver->class_pointer->dtable,
239 (sidx)op->sel_id);
240 if (result == 0)
242 /* Not a valid method */
243 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
245 /* The dispatch table needs to be installed.
246 This happens on the very first method call to the class. */
247 __objc_init_install_dtable (receiver, op);
249 /* Get real method for this in newly installed dtable */
250 result = get_imp (receiver->class_pointer, op);
252 else
254 /* The dispatch table has been installed. Check again
255 if the method exists (just in case the dispatch table
256 has been installed by another thread after we did the
257 previous check that the method exists).
259 result = sarray_get_safe (receiver->class_pointer->dtable,
260 (sidx)op->sel_id);
261 if (result == 0)
263 /* If the method still just doesn't exist for the
264 class, attempt to forward the method. */
265 result = __objc_get_forward_imp (receiver, op);
269 return result;
271 else
272 return (IMP)nil_method;
276 objc_msg_lookup_super (Super_t super, SEL sel)
278 if (super->self)
279 return get_imp (super->class, sel);
280 else
281 return (IMP)nil_method;
284 int method_get_sizeof_arguments (Method *);
286 retval_t
287 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
289 Method *m = class_get_instance_method (object->class_pointer, op);
290 const char *type;
291 *((id *) method_get_first_argument (m, arg_frame, &type)) = object;
292 *((SEL *) method_get_next_argument (arg_frame, &type)) = op;
293 return __builtin_apply ((apply_t) m->method_imp,
294 arg_frame,
295 method_get_sizeof_arguments (m));
298 void
299 __objc_init_dispatch_tables ()
301 __objc_uninstalled_dtable = sarray_new (200, 0);
304 /* This function is called by objc_msg_lookup when the
305 dispatch table needs to be installed; thus it is called once
306 for each class, namely when the very first message is sent to it. */
307 static void
308 __objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
310 objc_mutex_lock (__objc_runtime_mutex);
312 /* This may happen, if the programmer has taken the address of a
313 method before the dtable was initialized... too bad for him! */
314 if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
316 objc_mutex_unlock (__objc_runtime_mutex);
317 return;
320 if (CLS_ISCLASS (receiver->class_pointer))
322 /* receiver is an ordinary object */
323 assert (CLS_ISCLASS (receiver->class_pointer));
325 /* install instance methods table */
326 __objc_install_dispatch_table_for_class (receiver->class_pointer);
328 /* call +initialize -- this will in turn install the factory
329 dispatch table if not already done :-) */
330 __objc_send_initialize (receiver->class_pointer);
332 else
334 /* receiver is a class object */
335 assert (CLS_ISCLASS ((Class)receiver));
336 assert (CLS_ISMETA (receiver->class_pointer));
338 /* Install real dtable for factory methods */
339 __objc_install_dispatch_table_for_class (receiver->class_pointer);
341 __objc_send_initialize ((Class)receiver);
343 objc_mutex_unlock (__objc_runtime_mutex);
346 /* Install dummy table for class which causes the first message to
347 that class (or instances hereof) to be initialized properly */
348 void
349 __objc_install_premature_dtable (Class class)
351 assert (__objc_uninstalled_dtable);
352 class->dtable = __objc_uninstalled_dtable;
355 /* Send +initialize to class if not already done */
356 static void
357 __objc_send_initialize (Class class)
359 /* This *must* be a class object */
360 assert (CLS_ISCLASS (class));
361 assert (! CLS_ISMETA (class));
363 if (! CLS_ISINITIALIZED (class))
365 CLS_SETINITIALIZED (class);
366 CLS_SETINITIALIZED (class->class_pointer);
368 /* Create the garbage collector type memory description */
369 __objc_generate_gc_type_description (class);
371 if (class->super_class)
372 __objc_send_initialize (class->super_class);
375 SEL op = sel_register_name ("initialize");
376 IMP imp = 0;
377 MethodList_t method_list = class->class_pointer->methods;
379 while (method_list) {
380 int i;
381 Method_t method;
383 for (i = 0; i < method_list->method_count; i++) {
384 method = &(method_list->method_list[i]);
385 if (method->method_name
386 && method->method_name->sel_id == op->sel_id) {
387 imp = method->method_imp;
388 break;
392 if (imp)
393 break;
395 method_list = method_list->method_next;
398 if (imp)
399 (*imp) ((id) class, op);
405 /* Walk on the methods list of class and install the methods in the reverse
406 order of the lists. Since methods added by categories are before the methods
407 of class in the methods list, this allows categories to substitute methods
408 declared in class. However if more than one category replaces the same
409 method nothing is guaranteed about what method will be used.
410 Assumes that __objc_runtime_mutex is locked down. */
411 static void
412 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
414 int i;
416 if (! method_list)
417 return;
419 if (method_list->method_next)
420 __objc_install_methods_in_dtable (class, method_list->method_next);
422 for (i = 0; i < method_list->method_count; i++)
424 Method_t method = &(method_list->method_list[i]);
425 sarray_at_put_safe (class->dtable,
426 (sidx) method->method_name->sel_id,
427 method->method_imp);
431 /* Assumes that __objc_runtime_mutex is locked down. */
432 static void
433 __objc_install_dispatch_table_for_class (Class class)
435 Class super;
437 /* If the class has not yet had its class links resolved, we must
438 re-compute all class links */
439 if (! CLS_ISRESOLV (class))
440 __objc_resolve_class_links ();
442 super = class->super_class;
444 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
445 __objc_install_dispatch_table_for_class (super);
447 /* Allocate dtable if necessary */
448 if (super == 0)
450 objc_mutex_lock (__objc_runtime_mutex);
451 class->dtable = sarray_new (__objc_selector_max_index, 0);
452 objc_mutex_unlock (__objc_runtime_mutex);
454 else
455 class->dtable = sarray_lazy_copy (super->dtable);
457 __objc_install_methods_in_dtable (class, class->methods);
460 void
461 __objc_update_dispatch_table_for_class (Class class)
463 Class next;
464 struct sarray *arr;
466 /* not yet installed -- skip it */
467 if (class->dtable == __objc_uninstalled_dtable)
468 return;
470 objc_mutex_lock (__objc_runtime_mutex);
472 arr = class->dtable;
473 __objc_install_premature_dtable (class); /* someone might require it... */
474 sarray_free (arr); /* release memory */
476 /* could have been lazy... */
477 __objc_install_dispatch_table_for_class (class);
479 if (class->subclass_list) /* Traverse subclasses */
480 for (next = class->subclass_list; next; next = next->sibling_class)
481 __objc_update_dispatch_table_for_class (next);
483 objc_mutex_unlock (__objc_runtime_mutex);
487 /* This function adds a method list to a class. This function is
488 typically called by another function specific to the run-time. As
489 such this function does not worry about thread safe issues.
491 This one is only called for categories. Class objects have their
492 methods installed right away, and their selectors are made into
493 SEL's by the function __objc_register_selectors_from_class. */
494 void
495 class_add_method_list (Class class, MethodList_t list)
497 /* Passing of a linked list is not allowed. Do multiple calls. */
498 assert (! list->method_next);
500 __objc_register_selectors_from_list(list);
502 /* Add the methods to the class's method list. */
503 list->method_next = class->methods;
504 class->methods = list;
506 /* Update the dispatch table of class */
507 __objc_update_dispatch_table_for_class (class);
510 Method_t
511 class_get_instance_method (Class class, SEL op)
513 return search_for_method_in_hierarchy (class, op);
516 Method_t
517 class_get_class_method (MetaClass class, SEL op)
519 return search_for_method_in_hierarchy (class, op);
523 /* Search for a method starting from the current class up its hierarchy.
524 Return a pointer to the method's method structure if found. NULL
525 otherwise. */
527 static Method_t
528 search_for_method_in_hierarchy (Class cls, SEL sel)
530 Method_t method = NULL;
531 Class class;
533 if (! sel_is_mapped (sel))
534 return NULL;
536 /* Scan the method list of the class. If the method isn't found in the
537 list then step to its super class. */
538 for (class = cls; ((! method) && class); class = class->super_class)
539 method = search_for_method_in_list (class->methods, sel);
541 return method;
546 /* Given a linked list of method and a method's name. Search for the named
547 method's method structure. Return a pointer to the method's method
548 structure if found. NULL otherwise. */
549 Method_t
550 search_for_method_in_list (MethodList_t list, SEL op)
552 MethodList_t method_list = list;
554 if (! sel_is_mapped (op))
555 return NULL;
557 /* If not found then we'll search the list. */
558 while (method_list)
560 int i;
562 /* Search the method list. */
563 for (i = 0; i < method_list->method_count; ++i)
565 Method_t method = &method_list->method_list[i];
567 if (method->method_name)
568 if (method->method_name->sel_id == op->sel_id)
569 return method;
572 /* The method wasn't found. Follow the link to the next list of
573 methods. */
574 method_list = method_list->method_next;
577 return NULL;
580 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
582 /* Forwarding pointers/integers through the normal registers */
583 static id
584 __objc_word_forward (id rcv, SEL op, ...)
586 void *args, *res;
588 args = __builtin_apply_args ();
589 res = __objc_forward (rcv, op, args);
590 if (res)
591 __builtin_return (res);
592 else
593 return res;
596 /* Specific routine for forwarding floats/double because of
597 architectural differences on some processors. i386s for
598 example which uses a floating point stack versus general
599 registers for floating point numbers. This forward routine
600 makes sure that GCC restores the proper return values */
601 static double
602 __objc_double_forward (id rcv, SEL op, ...)
604 void *args, *res;
606 args = __builtin_apply_args ();
607 res = __objc_forward (rcv, op, args);
608 __builtin_return (res);
611 #if INVISIBLE_STRUCT_RETURN
612 static __big
613 #else
614 static id
615 #endif
616 __objc_block_forward (id rcv, SEL op, ...)
618 void *args, *res;
620 args = __builtin_apply_args ();
621 res = __objc_forward (rcv, op, args);
622 if (res)
623 __builtin_return (res);
624 else
625 #if INVISIBLE_STRUCT_RETURN
626 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
627 #else
628 return nil;
629 #endif
633 /* This function is installed in the dispatch table for all methods which are
634 not implemented. Thus, it is called when a selector is not recognized. */
635 static retval_t
636 __objc_forward (id object, SEL sel, arglist_t args)
638 IMP imp;
639 static SEL frwd_sel = 0; /* !T:SAFE2 */
640 SEL err_sel;
642 /* first try if the object understands forward:: */
643 if (! frwd_sel)
644 frwd_sel = sel_get_any_uid ("forward::");
646 if (__objc_responds_to (object, frwd_sel))
648 imp = get_imp (object->class_pointer, frwd_sel);
649 return (*imp) (object, frwd_sel, sel, args);
652 /* If the object recognizes the doesNotRecognize: method then we're going
653 to send it. */
654 err_sel = sel_get_any_uid ("doesNotRecognize:");
655 if (__objc_responds_to (object, err_sel))
657 imp = get_imp (object->class_pointer, err_sel);
658 return (*imp) (object, err_sel, sel);
661 /* The object doesn't recognize the method. Check for responding to
662 error:. If it does then sent it. */
664 char msg[256 + strlen ((const char *) sel_get_name (sel))
665 + strlen ((const char *) object->class_pointer->name)];
667 sprintf (msg, "(%s) %s does not recognize %s",
668 (CLS_ISMETA (object->class_pointer)
669 ? "class"
670 : "instance" ),
671 object->class_pointer->name, sel_get_name (sel));
673 /* TODO: support for error: is surely deprecated ? */
674 err_sel = sel_get_any_uid ("error:");
675 if (__objc_responds_to (object, err_sel))
677 imp = get_imp (object->class_pointer, err_sel);
678 return (*imp) (object, sel_get_any_uid ("error:"), msg);
681 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
682 a default action is taken. */
683 _objc_abort ("%s\n", msg);
685 return 0;
689 void
690 __objc_print_dtable_stats ()
692 int total = 0;
694 objc_mutex_lock (__objc_runtime_mutex);
696 #ifdef OBJC_SPARSE2
697 printf ("memory usage: (%s)\n", "2-level sparse arrays");
698 #else
699 printf ("memory usage: (%s)\n", "3-level sparse arrays");
700 #endif
702 printf ("arrays: %d = %ld bytes\n", narrays,
703 (long) ((size_t) narrays * sizeof (struct sarray)));
704 total += narrays * sizeof (struct sarray);
705 printf ("buckets: %d = %ld bytes\n", nbuckets,
706 (long) ((size_t) nbuckets * sizeof (struct sbucket)));
707 total += nbuckets * sizeof (struct sbucket);
709 printf ("idxtables: %d = %ld bytes\n",
710 idxsize, (long) ((size_t) idxsize * sizeof (void *)));
711 total += idxsize * sizeof (void *);
712 printf ("-----------------------------------\n");
713 printf ("total: %d bytes\n", total);
714 printf ("===================================\n");
716 objc_mutex_unlock (__objc_runtime_mutex);
719 /* Returns the uninstalled dispatch table indicator.
720 If a class' dispatch table points to __objc_uninstalled_dtable
721 then that means it needs its dispatch table to be installed. */
723 struct sarray *
724 objc_get_uninstalled_dtable ()
726 return __objc_uninstalled_dtable;