]> git.ipfire.org Git - thirdparty/gcc.git/blob - libobjc/sendmsg.c
In libobjc/:
[thirdparty/gcc.git] / libobjc / sendmsg.c
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
5
6 This file is part of GCC.
7
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.
11
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.
16
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.
20
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/>. */
25
26
27 /* FIXME: This file has no business including tm.h. */
28 /* FIXME: This should be using libffi instead of __builtin_apply
29 and friends. */
30
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.h"
37 #include "objc/objc-api.h"
38 #include "objc/thr.h"
39 #include "objc-private/runtime.h"
40 #include "objc/sarray.h"
41 #include "objc/encoding.h"
42 #include "runtime-info.h"
43
44 /* This is how we hack STRUCT_VALUE to be 1 or 0. */
45 #define gen_rtx(args...) 1
46 #define gen_rtx_MEM(args...) 1
47 #define gen_rtx_REG(args...) 1
48 /* Alread defined in gcc/coretypes.h. So prevent double definition warning. */
49 #undef rtx
50 #define rtx int
51
52 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
53 #define INVISIBLE_STRUCT_RETURN 1
54 #else
55 #define INVISIBLE_STRUCT_RETURN 0
56 #endif
57
58 /* The uninstalled dispatch table */
59 struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
60
61 /* Two hooks for method forwarding. If either is set, it is invoked
62 * to return a function that performs the real forwarding. If both
63 * are set, the result of __objc_msg_forward2 will be preferred over
64 * that of __objc_msg_forward. If both return NULL or are unset,
65 * the libgcc based functions (__builtin_apply and friends) are
66 * used.
67 */
68 IMP (*__objc_msg_forward) (SEL) = NULL;
69 IMP (*__objc_msg_forward2) (id, SEL) = NULL;
70
71 /* Send +initialize to class */
72 static void __objc_send_initialize (Class);
73
74 static void __objc_install_dispatch_table_for_class (Class);
75
76 /* Forward declare some functions */
77 static void __objc_init_install_dtable (id, SEL);
78
79 /* Various forwarding functions that are used based upon the
80 return type for the selector.
81 __objc_block_forward for structures.
82 __objc_double_forward for floats/doubles.
83 __objc_word_forward for pointers or types that fit in registers. */
84 static double __objc_double_forward (id, SEL, ...);
85 static id __objc_word_forward (id, SEL, ...);
86 typedef struct { id many[8]; } __big;
87 #if INVISIBLE_STRUCT_RETURN
88 static __big
89 #else
90 static id
91 #endif
92 __objc_block_forward (id, SEL, ...);
93 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
94 Method_t search_for_method_in_list (MethodList_t list, SEL op);
95 id nil_method (id, SEL);
96
97 /* Given a selector, return the proper forwarding implementation. */
98 inline
99 IMP
100 __objc_get_forward_imp (id rcv, SEL sel)
101 {
102 /* If a custom forwarding hook was registered, try getting a forwarding
103 function from it. There are two forward routine hooks, one that
104 takes the receiver as an argument and one that does not. */
105 if (__objc_msg_forward2)
106 {
107 IMP result;
108 if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
109 return result;
110 }
111 if (__objc_msg_forward)
112 {
113 IMP result;
114 if ((result = __objc_msg_forward (sel)) != NULL)
115 return result;
116 }
117
118 /* In all other cases, use the default forwarding functions built using
119 __builtin_apply and friends. */
120 {
121 const char *t = sel->sel_types;
122
123 if (t && (*t == '[' || *t == '(' || *t == '{')
124 #ifdef OBJC_MAX_STRUCT_BY_VALUE
125 && objc_sizeof_type (t) > OBJC_MAX_STRUCT_BY_VALUE
126 #endif
127 )
128 return (IMP)__objc_block_forward;
129 else if (t && (*t == 'f' || *t == 'd'))
130 return (IMP)__objc_double_forward;
131 else
132 return (IMP)__objc_word_forward;
133 }
134 }
135
136 /* Given a class and selector, return the selector's implementation. */
137 inline
138 IMP
139 get_imp (Class class, SEL sel)
140 {
141 /* In a vanilla implementation we would first check if the dispatch
142 table is installed. Here instead, to get more speed in the
143 standard case (that the dispatch table is installed) we first try
144 to get the imp using brute force. Only if that fails, we do what
145 we should have been doing from the very beginning, that is, check
146 if the dispatch table needs to be installed, install it if it's
147 not installed, and retrieve the imp from the table if it's
148 installed. */
149 void *res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
150 if (res == 0)
151 {
152 /* Not a valid method */
153 if (class->dtable == __objc_uninstalled_dtable)
154 {
155 /* The dispatch table needs to be installed. */
156 objc_mutex_lock (__objc_runtime_mutex);
157
158 /* Double-checked locking pattern: Check
159 __objc_uninstalled_dtable again in case another thread
160 installed the dtable while we were waiting for the lock
161 to be released. */
162 if (class->dtable == __objc_uninstalled_dtable)
163 {
164 __objc_install_dispatch_table_for_class (class);
165 }
166
167 objc_mutex_unlock (__objc_runtime_mutex);
168 /* Call ourselves with the installed dispatch table
169 and get the real method */
170 res = get_imp (class, sel);
171 }
172 else
173 {
174 /* The dispatch table has been installed. */
175
176 /* Get the method from the dispatch table (we try to get it
177 again in case another thread has installed the dtable just
178 after we invoked sarray_get_safe, but before we checked
179 class->dtable == __objc_uninstalled_dtable).
180 */
181 res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
182 if (res == 0)
183 {
184 /* The dispatch table has been installed, and the method
185 is not in the dispatch table. So the method just
186 doesn't exist for the class. Return the forwarding
187 implementation. */
188 res = __objc_get_forward_imp ((id)class, sel);
189 }
190 }
191 }
192 return res;
193 }
194
195 /* Query if an object can respond to a selector, returns YES if the
196 object implements the selector otherwise NO. Does not check if the
197 method can be forwarded. */
198 inline
199 BOOL
200 __objc_responds_to (id object, SEL sel)
201 {
202 void *res;
203
204 /* Install dispatch table if need be */
205 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
206 {
207 objc_mutex_lock (__objc_runtime_mutex);
208 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
209 {
210 __objc_install_dispatch_table_for_class (object->class_pointer);
211 }
212 objc_mutex_unlock (__objc_runtime_mutex);
213 }
214
215 /* Get the method from the dispatch table */
216 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
217 return (res != 0);
218 }
219
220 /* This is the lookup function. All entries in the table are either a
221 valid method *or* zero. If zero then either the dispatch table
222 needs to be installed or it doesn't exist and forwarding is attempted. */
223 inline
224 IMP
225 objc_msg_lookup (id receiver, SEL op)
226 {
227 IMP result;
228 if (receiver)
229 {
230 result = sarray_get_safe (receiver->class_pointer->dtable,
231 (sidx)op->sel_id);
232 if (result == 0)
233 {
234 /* Not a valid method */
235 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
236 {
237 /* The dispatch table needs to be installed.
238 This happens on the very first method call to the class. */
239 __objc_init_install_dtable (receiver, op);
240
241 /* Get real method for this in newly installed dtable */
242 result = get_imp (receiver->class_pointer, op);
243 }
244 else
245 {
246 /* The dispatch table has been installed. Check again
247 if the method exists (just in case the dispatch table
248 has been installed by another thread after we did the
249 previous check that the method exists).
250 */
251 result = sarray_get_safe (receiver->class_pointer->dtable,
252 (sidx)op->sel_id);
253 if (result == 0)
254 {
255 /* If the method still just doesn't exist for the
256 class, attempt to forward the method. */
257 result = __objc_get_forward_imp (receiver, op);
258 }
259 }
260 }
261 return result;
262 }
263 else
264 return (IMP)nil_method;
265 }
266
267 IMP
268 objc_msg_lookup_super (Super_t super, SEL sel)
269 {
270 if (super->self)
271 return get_imp (super->class, sel);
272 else
273 return (IMP)nil_method;
274 }
275
276 int method_get_sizeof_arguments (Method *);
277
278 retval_t
279 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
280 {
281 Method *m = class_get_instance_method (object->class_pointer, op);
282 const char *type;
283 *((id *) method_get_first_argument (m, arg_frame, &type)) = object;
284 *((SEL *) method_get_next_argument (arg_frame, &type)) = op;
285 return __builtin_apply ((apply_t) m->method_imp,
286 arg_frame,
287 method_get_sizeof_arguments (m));
288 }
289
290 void
291 __objc_init_dispatch_tables ()
292 {
293 __objc_uninstalled_dtable = sarray_new (200, 0);
294 }
295
296 /* This function is called by objc_msg_lookup when the
297 dispatch table needs to be installed; thus it is called once
298 for each class, namely when the very first message is sent to it. */
299 static void
300 __objc_init_install_dtable (id receiver, SEL op __attribute__ ((__unused__)))
301 {
302 objc_mutex_lock (__objc_runtime_mutex);
303
304 /* This may happen, if the programmer has taken the address of a
305 method before the dtable was initialized... too bad for him! */
306 if (receiver->class_pointer->dtable != __objc_uninstalled_dtable)
307 {
308 objc_mutex_unlock (__objc_runtime_mutex);
309 return;
310 }
311
312 if (CLS_ISCLASS (receiver->class_pointer))
313 {
314 /* receiver is an ordinary object */
315 assert (CLS_ISCLASS (receiver->class_pointer));
316
317 /* install instance methods table */
318 __objc_install_dispatch_table_for_class (receiver->class_pointer);
319
320 /* call +initialize -- this will in turn install the factory
321 dispatch table if not already done :-) */
322 __objc_send_initialize (receiver->class_pointer);
323 }
324 else
325 {
326 /* receiver is a class object */
327 assert (CLS_ISCLASS ((Class)receiver));
328 assert (CLS_ISMETA (receiver->class_pointer));
329
330 /* Install real dtable for factory methods */
331 __objc_install_dispatch_table_for_class (receiver->class_pointer);
332
333 __objc_send_initialize ((Class)receiver);
334 }
335 objc_mutex_unlock (__objc_runtime_mutex);
336 }
337
338 /* Install dummy table for class which causes the first message to
339 that class (or instances hereof) to be initialized properly */
340 void
341 __objc_install_premature_dtable (Class class)
342 {
343 assert (__objc_uninstalled_dtable);
344 class->dtable = __objc_uninstalled_dtable;
345 }
346
347 /* Send +initialize to class if not already done */
348 static void
349 __objc_send_initialize (Class class)
350 {
351 /* This *must* be a class object */
352 assert (CLS_ISCLASS (class));
353 assert (! CLS_ISMETA (class));
354
355 if (! CLS_ISINITIALIZED (class))
356 {
357 CLS_SETINITIALIZED (class);
358 CLS_SETINITIALIZED (class->class_pointer);
359
360 /* Create the garbage collector type memory description */
361 __objc_generate_gc_type_description (class);
362
363 if (class->super_class)
364 __objc_send_initialize (class->super_class);
365
366 {
367 SEL op = sel_register_name ("initialize");
368 IMP imp = 0;
369 MethodList_t method_list = class->class_pointer->methods;
370
371 while (method_list) {
372 int i;
373 Method_t method;
374
375 for (i = 0; i < method_list->method_count; i++) {
376 method = &(method_list->method_list[i]);
377 if (method->method_name
378 && method->method_name->sel_id == op->sel_id) {
379 imp = method->method_imp;
380 break;
381 }
382 }
383
384 if (imp)
385 break;
386
387 method_list = method_list->method_next;
388
389 }
390 if (imp)
391 (*imp) ((id) class, op);
392
393 }
394 }
395 }
396
397 /* Walk on the methods list of class and install the methods in the reverse
398 order of the lists. Since methods added by categories are before the methods
399 of class in the methods list, this allows categories to substitute methods
400 declared in class. However if more than one category replaces the same
401 method nothing is guaranteed about what method will be used.
402 Assumes that __objc_runtime_mutex is locked down. */
403 static void
404 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
405 {
406 int i;
407
408 if (! method_list)
409 return;
410
411 if (method_list->method_next)
412 __objc_install_methods_in_dtable (class, method_list->method_next);
413
414 for (i = 0; i < method_list->method_count; i++)
415 {
416 Method_t method = &(method_list->method_list[i]);
417 sarray_at_put_safe (class->dtable,
418 (sidx) method->method_name->sel_id,
419 method->method_imp);
420 }
421 }
422
423 /* Assumes that __objc_runtime_mutex is locked down. */
424 static void
425 __objc_install_dispatch_table_for_class (Class class)
426 {
427 Class super;
428
429 /* If the class has not yet had its class links resolved, we must
430 re-compute all class links */
431 if (! CLS_ISRESOLV (class))
432 __objc_resolve_class_links ();
433
434 super = class->super_class;
435
436 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
437 __objc_install_dispatch_table_for_class (super);
438
439 /* Allocate dtable if necessary */
440 if (super == 0)
441 {
442 objc_mutex_lock (__objc_runtime_mutex);
443 class->dtable = sarray_new (__objc_selector_max_index, 0);
444 objc_mutex_unlock (__objc_runtime_mutex);
445 }
446 else
447 class->dtable = sarray_lazy_copy (super->dtable);
448
449 __objc_install_methods_in_dtable (class, class->methods);
450 }
451
452 void
453 __objc_update_dispatch_table_for_class (Class class)
454 {
455 Class next;
456 struct sarray *arr;
457
458 /* not yet installed -- skip it */
459 if (class->dtable == __objc_uninstalled_dtable)
460 return;
461
462 objc_mutex_lock (__objc_runtime_mutex);
463
464 arr = class->dtable;
465 __objc_install_premature_dtable (class); /* someone might require it... */
466 sarray_free (arr); /* release memory */
467
468 /* could have been lazy... */
469 __objc_install_dispatch_table_for_class (class);
470
471 if (class->subclass_list) /* Traverse subclasses */
472 for (next = class->subclass_list; next; next = next->sibling_class)
473 __objc_update_dispatch_table_for_class (next);
474
475 objc_mutex_unlock (__objc_runtime_mutex);
476 }
477
478
479 /* This function adds a method list to a class. This function is
480 typically called by another function specific to the run-time. As
481 such this function does not worry about thread safe issues.
482
483 This one is only called for categories. Class objects have their
484 methods installed right away, and their selectors are made into
485 SEL's by the function __objc_register_selectors_from_class. */
486 void
487 class_add_method_list (Class class, MethodList_t list)
488 {
489 /* Passing of a linked list is not allowed. Do multiple calls. */
490 assert (! list->method_next);
491
492 __objc_register_selectors_from_list(list);
493
494 /* Add the methods to the class's method list. */
495 list->method_next = class->methods;
496 class->methods = list;
497
498 /* Update the dispatch table of class */
499 __objc_update_dispatch_table_for_class (class);
500 }
501
502 Method_t
503 class_get_instance_method (Class class, SEL op)
504 {
505 return search_for_method_in_hierarchy (class, op);
506 }
507
508 Method_t
509 class_get_class_method (MetaClass class, SEL op)
510 {
511 return search_for_method_in_hierarchy (class, op);
512 }
513
514
515 /* Search for a method starting from the current class up its hierarchy.
516 Return a pointer to the method's method structure if found. NULL
517 otherwise. */
518
519 static Method_t
520 search_for_method_in_hierarchy (Class cls, SEL sel)
521 {
522 Method_t method = NULL;
523 Class class;
524
525 if (! sel_is_mapped (sel))
526 return NULL;
527
528 /* Scan the method list of the class. If the method isn't found in the
529 list then step to its super class. */
530 for (class = cls; ((! method) && class); class = class->super_class)
531 method = search_for_method_in_list (class->methods, sel);
532
533 return method;
534 }
535
536
537
538 /* Given a linked list of method and a method's name. Search for the named
539 method's method structure. Return a pointer to the method's method
540 structure if found. NULL otherwise. */
541 Method_t
542 search_for_method_in_list (MethodList_t list, SEL op)
543 {
544 MethodList_t method_list = list;
545
546 if (! sel_is_mapped (op))
547 return NULL;
548
549 /* If not found then we'll search the list. */
550 while (method_list)
551 {
552 int i;
553
554 /* Search the method list. */
555 for (i = 0; i < method_list->method_count; ++i)
556 {
557 Method_t method = &method_list->method_list[i];
558
559 if (method->method_name)
560 if (method->method_name->sel_id == op->sel_id)
561 return method;
562 }
563
564 /* The method wasn't found. Follow the link to the next list of
565 methods. */
566 method_list = method_list->method_next;
567 }
568
569 return NULL;
570 }
571
572 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
573
574 /* Forwarding pointers/integers through the normal registers */
575 static id
576 __objc_word_forward (id rcv, SEL op, ...)
577 {
578 void *args, *res;
579
580 args = __builtin_apply_args ();
581 res = __objc_forward (rcv, op, args);
582 if (res)
583 __builtin_return (res);
584 else
585 return res;
586 }
587
588 /* Specific routine for forwarding floats/double because of
589 architectural differences on some processors. i386s for
590 example which uses a floating point stack versus general
591 registers for floating point numbers. This forward routine
592 makes sure that GCC restores the proper return values */
593 static double
594 __objc_double_forward (id rcv, SEL op, ...)
595 {
596 void *args, *res;
597
598 args = __builtin_apply_args ();
599 res = __objc_forward (rcv, op, args);
600 __builtin_return (res);
601 }
602
603 #if INVISIBLE_STRUCT_RETURN
604 static __big
605 #else
606 static id
607 #endif
608 __objc_block_forward (id rcv, SEL op, ...)
609 {
610 void *args, *res;
611
612 args = __builtin_apply_args ();
613 res = __objc_forward (rcv, op, args);
614 if (res)
615 __builtin_return (res);
616 else
617 #if INVISIBLE_STRUCT_RETURN
618 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
619 #else
620 return nil;
621 #endif
622 }
623
624
625 /* This function is installed in the dispatch table for all methods which are
626 not implemented. Thus, it is called when a selector is not recognized. */
627 static retval_t
628 __objc_forward (id object, SEL sel, arglist_t args)
629 {
630 IMP imp;
631 static SEL frwd_sel = 0; /* !T:SAFE2 */
632 SEL err_sel;
633
634 /* first try if the object understands forward:: */
635 if (! frwd_sel)
636 frwd_sel = sel_get_any_uid ("forward::");
637
638 if (__objc_responds_to (object, frwd_sel))
639 {
640 imp = get_imp (object->class_pointer, frwd_sel);
641 return (*imp) (object, frwd_sel, sel, args);
642 }
643
644 /* If the object recognizes the doesNotRecognize: method then we're going
645 to send it. */
646 err_sel = sel_get_any_uid ("doesNotRecognize:");
647 if (__objc_responds_to (object, err_sel))
648 {
649 imp = get_imp (object->class_pointer, err_sel);
650 return (*imp) (object, err_sel, sel);
651 }
652
653 /* The object doesn't recognize the method. Check for responding to
654 error:. If it does then sent it. */
655 {
656 char msg[256 + strlen ((const char *) sel_get_name (sel))
657 + strlen ((const char *) object->class_pointer->name)];
658
659 sprintf (msg, "(%s) %s does not recognize %s",
660 (CLS_ISMETA (object->class_pointer)
661 ? "class"
662 : "instance" ),
663 object->class_pointer->name, sel_get_name (sel));
664
665 /* TODO: support for error: is surely deprecated ? */
666 err_sel = sel_get_any_uid ("error:");
667 if (__objc_responds_to (object, err_sel))
668 {
669 imp = get_imp (object->class_pointer, err_sel);
670 return (*imp) (object, sel_get_any_uid ("error:"), msg);
671 }
672
673 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
674 a default action is taken. */
675 _objc_abort ("%s\n", msg);
676
677 return 0;
678 }
679 }
680
681 void
682 __objc_print_dtable_stats ()
683 {
684 int total = 0;
685
686 objc_mutex_lock (__objc_runtime_mutex);
687
688 #ifdef OBJC_SPARSE2
689 printf ("memory usage: (%s)\n", "2-level sparse arrays");
690 #else
691 printf ("memory usage: (%s)\n", "3-level sparse arrays");
692 #endif
693
694 printf ("arrays: %d = %ld bytes\n", narrays,
695 (long) ((size_t) narrays * sizeof (struct sarray)));
696 total += narrays * sizeof (struct sarray);
697 printf ("buckets: %d = %ld bytes\n", nbuckets,
698 (long) ((size_t) nbuckets * sizeof (struct sbucket)));
699 total += nbuckets * sizeof (struct sbucket);
700
701 printf ("idxtables: %d = %ld bytes\n",
702 idxsize, (long) ((size_t) idxsize * sizeof (void *)));
703 total += idxsize * sizeof (void *);
704 printf ("-----------------------------------\n");
705 printf ("total: %d bytes\n", total);
706 printf ("===================================\n");
707
708 objc_mutex_unlock (__objc_runtime_mutex);
709 }
710
711 /* Returns the uninstalled dispatch table indicator.
712 If a class' dispatch table points to __objc_uninstalled_dtable
713 then that means it needs its dispatch table to be installed. */
714 inline
715 struct sarray *
716 objc_get_uninstalled_dtable ()
717 {
718 return __objc_uninstalled_dtable;
719 }