]> gcc.gnu.org Git - gcc.git/blob - libobjc/sendmsg.c
archive.c: Do not include objc/objc.h.
[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-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 */
44
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
52
53 #if ! defined (STRUCT_VALUE) || STRUCT_VALUE == 0
54 #define INVISIBLE_STRUCT_RETURN 1
55 #else
56 #define INVISIBLE_STRUCT_RETURN 0
57 #endif
58
59 /* The uninstalled dispatch table */
60 struct sarray *__objc_uninstalled_dtable = 0; /* !T:MUTEX */
61
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.
68 */
69 IMP (*__objc_msg_forward) (SEL) = NULL;
70 IMP (*__objc_msg_forward2) (id, SEL) = NULL;
71
72 /* Send +initialize to class */
73 static void __objc_send_initialize (Class);
74
75 static void __objc_install_dispatch_table_for_class (Class);
76
77 /* Forward declare some functions */
78 static void __objc_init_install_dtable (id, SEL);
79
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);
97
98 /* Given a selector, return the proper forwarding implementation. */
99 inline
100 IMP
101 __objc_get_forward_imp (id rcv, SEL sel)
102 {
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)
107 {
108 IMP result;
109 if ((result = __objc_msg_forward2 (rcv, sel)) != NULL)
110 return result;
111 }
112 if (__objc_msg_forward)
113 {
114 IMP result;
115 if ((result = __objc_msg_forward (sel)) != NULL)
116 return result;
117 }
118
119 /* In all other cases, use the default forwarding functions built using
120 __builtin_apply and friends. */
121 {
122 const char *t = sel->sel_types;
123
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
128 )
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;
134 }
135 }
136
137 /* Given a class and selector, return the selector's implementation. */
138 inline
139 IMP
140 get_imp (Class class, SEL sel)
141 {
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)
152 {
153 /* Not a valid method */
154 if (class->dtable == __objc_uninstalled_dtable)
155 {
156 /* The dispatch table needs to be installed. */
157 objc_mutex_lock (__objc_runtime_mutex);
158
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)
164 {
165 __objc_install_dispatch_table_for_class (class);
166 }
167
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);
172 }
173 else
174 {
175 /* The dispatch table has been installed. */
176
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).
181 */
182 res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
183 if (res == 0)
184 {
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);
190 }
191 }
192 }
193 return res;
194 }
195
196 /* Given a method, return its implementation. */
197 IMP
198 method_get_imp (Method_t method)
199 {
200 return (method != (Method_t)0) ? method->method_imp : (IMP)0;
201 }
202
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)
209 {
210 void *res;
211
212 /* Install dispatch table if need be */
213 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
214 {
215 objc_mutex_lock (__objc_runtime_mutex);
216 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
217 {
218 __objc_install_dispatch_table_for_class (object->class_pointer);
219 }
220 objc_mutex_unlock (__objc_runtime_mutex);
221 }
222
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);
226 }
227
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. */
231
232 IMP
233 objc_msg_lookup (id receiver, SEL op)
234 {
235 IMP result;
236 if (receiver)
237 {
238 result = sarray_get_safe (receiver->class_pointer->dtable,
239 (sidx)op->sel_id);
240 if (result == 0)
241 {
242 /* Not a valid method */
243 if (receiver->class_pointer->dtable == __objc_uninstalled_dtable)
244 {
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);
248
249 /* Get real method for this in newly installed dtable */
250 result = get_imp (receiver->class_pointer, op);
251 }
252 else
253 {
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).
258 */
259 result = sarray_get_safe (receiver->class_pointer->dtable,
260 (sidx)op->sel_id);
261 if (result == 0)
262 {
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);
266 }
267 }
268 }
269 return result;
270 }
271 else
272 return (IMP)nil_method;
273 }
274
275 IMP
276 objc_msg_lookup_super (Super_t super, SEL sel)
277 {
278 if (super->self)
279 return get_imp (super->class, sel);
280 else
281 return (IMP)nil_method;
282 }
283
284 int method_get_sizeof_arguments (Method *);
285
286 retval_t
287 objc_msg_sendv (id object, SEL op, arglist_t arg_frame)
288 {
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));
296 }
297
298 void
299 __objc_init_dispatch_tables ()
300 {
301 __objc_uninstalled_dtable = sarray_new (200, 0);
302 }
303
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__)))
309 {
310 objc_mutex_lock (__objc_runtime_mutex);
311
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)
315 {
316 objc_mutex_unlock (__objc_runtime_mutex);
317 return;
318 }
319
320 if (CLS_ISCLASS (receiver->class_pointer))
321 {
322 /* receiver is an ordinary object */
323 assert (CLS_ISCLASS (receiver->class_pointer));
324
325 /* install instance methods table */
326 __objc_install_dispatch_table_for_class (receiver->class_pointer);
327
328 /* call +initialize -- this will in turn install the factory
329 dispatch table if not already done :-) */
330 __objc_send_initialize (receiver->class_pointer);
331 }
332 else
333 {
334 /* receiver is a class object */
335 assert (CLS_ISCLASS ((Class)receiver));
336 assert (CLS_ISMETA (receiver->class_pointer));
337
338 /* Install real dtable for factory methods */
339 __objc_install_dispatch_table_for_class (receiver->class_pointer);
340
341 __objc_send_initialize ((Class)receiver);
342 }
343 objc_mutex_unlock (__objc_runtime_mutex);
344 }
345
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)
350 {
351 assert (__objc_uninstalled_dtable);
352 class->dtable = __objc_uninstalled_dtable;
353 }
354
355 /* Send +initialize to class if not already done */
356 static void
357 __objc_send_initialize (Class class)
358 {
359 /* This *must* be a class object */
360 assert (CLS_ISCLASS (class));
361 assert (! CLS_ISMETA (class));
362
363 if (! CLS_ISINITIALIZED (class))
364 {
365 CLS_SETINITIALIZED (class);
366 CLS_SETINITIALIZED (class->class_pointer);
367
368 /* Create the garbage collector type memory description */
369 __objc_generate_gc_type_description (class);
370
371 if (class->super_class)
372 __objc_send_initialize (class->super_class);
373
374 {
375 SEL op = sel_register_name ("initialize");
376 IMP imp = 0;
377 MethodList_t method_list = class->class_pointer->methods;
378
379 while (method_list) {
380 int i;
381 Method_t method;
382
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;
389 }
390 }
391
392 if (imp)
393 break;
394
395 method_list = method_list->method_next;
396
397 }
398 if (imp)
399 (*imp) ((id) class, op);
400
401 }
402 }
403 }
404
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)
413 {
414 int i;
415
416 if (! method_list)
417 return;
418
419 if (method_list->method_next)
420 __objc_install_methods_in_dtable (class, method_list->method_next);
421
422 for (i = 0; i < method_list->method_count; i++)
423 {
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);
428 }
429 }
430
431 /* Assumes that __objc_runtime_mutex is locked down. */
432 static void
433 __objc_install_dispatch_table_for_class (Class class)
434 {
435 Class super;
436
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 ();
441
442 super = class->super_class;
443
444 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
445 __objc_install_dispatch_table_for_class (super);
446
447 /* Allocate dtable if necessary */
448 if (super == 0)
449 {
450 objc_mutex_lock (__objc_runtime_mutex);
451 class->dtable = sarray_new (__objc_selector_max_index, 0);
452 objc_mutex_unlock (__objc_runtime_mutex);
453 }
454 else
455 class->dtable = sarray_lazy_copy (super->dtable);
456
457 __objc_install_methods_in_dtable (class, class->methods);
458 }
459
460 void
461 __objc_update_dispatch_table_for_class (Class class)
462 {
463 Class next;
464 struct sarray *arr;
465
466 /* not yet installed -- skip it */
467 if (class->dtable == __objc_uninstalled_dtable)
468 return;
469
470 objc_mutex_lock (__objc_runtime_mutex);
471
472 arr = class->dtable;
473 __objc_install_premature_dtable (class); /* someone might require it... */
474 sarray_free (arr); /* release memory */
475
476 /* could have been lazy... */
477 __objc_install_dispatch_table_for_class (class);
478
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);
482
483 objc_mutex_unlock (__objc_runtime_mutex);
484 }
485
486
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.
490
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)
496 {
497 /* Passing of a linked list is not allowed. Do multiple calls. */
498 assert (! list->method_next);
499
500 __objc_register_selectors_from_list(list);
501
502 /* Add the methods to the class's method list. */
503 list->method_next = class->methods;
504 class->methods = list;
505
506 /* Update the dispatch table of class */
507 __objc_update_dispatch_table_for_class (class);
508 }
509
510 Method_t
511 class_get_instance_method (Class class, SEL op)
512 {
513 return search_for_method_in_hierarchy (class, op);
514 }
515
516 Method_t
517 class_get_class_method (MetaClass class, SEL op)
518 {
519 return search_for_method_in_hierarchy (class, op);
520 }
521
522
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. */
526
527 static Method_t
528 search_for_method_in_hierarchy (Class cls, SEL sel)
529 {
530 Method_t method = NULL;
531 Class class;
532
533 if (! sel_is_mapped (sel))
534 return NULL;
535
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);
540
541 return method;
542 }
543
544
545
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)
551 {
552 MethodList_t method_list = list;
553
554 if (! sel_is_mapped (op))
555 return NULL;
556
557 /* If not found then we'll search the list. */
558 while (method_list)
559 {
560 int i;
561
562 /* Search the method list. */
563 for (i = 0; i < method_list->method_count; ++i)
564 {
565 Method_t method = &method_list->method_list[i];
566
567 if (method->method_name)
568 if (method->method_name->sel_id == op->sel_id)
569 return method;
570 }
571
572 /* The method wasn't found. Follow the link to the next list of
573 methods. */
574 method_list = method_list->method_next;
575 }
576
577 return NULL;
578 }
579
580 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
581
582 /* Forwarding pointers/integers through the normal registers */
583 static id
584 __objc_word_forward (id rcv, SEL op, ...)
585 {
586 void *args, *res;
587
588 args = __builtin_apply_args ();
589 res = __objc_forward (rcv, op, args);
590 if (res)
591 __builtin_return (res);
592 else
593 return res;
594 }
595
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, ...)
603 {
604 void *args, *res;
605
606 args = __builtin_apply_args ();
607 res = __objc_forward (rcv, op, args);
608 __builtin_return (res);
609 }
610
611 #if INVISIBLE_STRUCT_RETURN
612 static __big
613 #else
614 static id
615 #endif
616 __objc_block_forward (id rcv, SEL op, ...)
617 {
618 void *args, *res;
619
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
630 }
631
632
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)
637 {
638 IMP imp;
639 static SEL frwd_sel = 0; /* !T:SAFE2 */
640 SEL err_sel;
641
642 /* first try if the object understands forward:: */
643 if (! frwd_sel)
644 frwd_sel = sel_get_any_uid ("forward::");
645
646 if (__objc_responds_to (object, frwd_sel))
647 {
648 imp = get_imp (object->class_pointer, frwd_sel);
649 return (*imp) (object, frwd_sel, sel, args);
650 }
651
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))
656 {
657 imp = get_imp (object->class_pointer, err_sel);
658 return (*imp) (object, err_sel, sel);
659 }
660
661 /* The object doesn't recognize the method. Check for responding to
662 error:. If it does then sent it. */
663 {
664 char msg[256 + strlen ((const char *) sel_get_name (sel))
665 + strlen ((const char *) object->class_pointer->name)];
666
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));
672
673 /* TODO: support for error: is surely deprecated ? */
674 err_sel = sel_get_any_uid ("error:");
675 if (__objc_responds_to (object, err_sel))
676 {
677 imp = get_imp (object->class_pointer, err_sel);
678 return (*imp) (object, sel_get_any_uid ("error:"), msg);
679 }
680
681 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
682 a default action is taken. */
683 _objc_abort ("%s\n", msg);
684
685 return 0;
686 }
687 }
688
689 void
690 __objc_print_dtable_stats ()
691 {
692 int total = 0;
693
694 objc_mutex_lock (__objc_runtime_mutex);
695
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
701
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);
708
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");
715
716 objc_mutex_unlock (__objc_runtime_mutex);
717 }
718
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. */
722
723 struct sarray *
724 objc_get_uninstalled_dtable ()
725 {
726 return __objc_uninstalled_dtable;
727 }
This page took 0.064664 seconds and 5 git commands to generate.