* doc/gcc.texi, doc/install.texi, doc/invoke.texi: Remove trailing
[official-gcc.git] / libobjc / sendmsg.c
blob61fa28833b3099f7c2ea397c0fb2b637fcb9f018
1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995, 1996, 1997, 1998,
3 2001 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
6 This file is part of GNU CC.
8 GNU CC 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 GNU CC 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 GNU CC; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, 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 #include "tconfig.h"
29 #include "runtime.h"
30 #include "sarray.h"
31 #include "encoding.h"
32 #include "runtime-info.h"
34 /* this is how we hack STRUCT_VALUE to be 1 or 0 */
35 #define gen_rtx(args...) 1
36 #define gen_rtx_MEM(args...) 1
37 #define gen_rtx_REG(args...) 1
38 #define rtx int
40 #if !defined(STRUCT_VALUE) || STRUCT_VALUE == 0
41 #define INVISIBLE_STRUCT_RETURN 1
42 #else
43 #define INVISIBLE_STRUCT_RETURN 0
44 #endif
46 /* The uninstalled dispatch table */
47 struct sarray* __objc_uninstalled_dtable = 0; /* !T:MUTEX */
49 /* Hook for method forwarding. If it is set, is invoked to return a
50 function that performs the real forwarding. Otherwise the libgcc
51 based functions (__builtin_apply and friends) are used. */
52 IMP (*__objc_msg_forward)(SEL) = NULL;
54 /* Send +initialize to class */
55 static void __objc_send_initialize(Class);
57 static void __objc_install_dispatch_table_for_class (Class);
59 /* Forward declare some functions */
60 static void __objc_init_install_dtable(id, SEL);
62 /* Various forwarding functions that are used based upon the
63 return type for the selector.
64 __objc_block_forward for structures.
65 __objc_double_forward for floats/doubles.
66 __objc_word_forward for pointers or types that fit in registers.
68 static double __objc_double_forward(id, SEL, ...);
69 static id __objc_word_forward(id, SEL, ...);
70 typedef struct { id many[8]; } __big;
71 #if INVISIBLE_STRUCT_RETURN
72 static __big
73 #else
74 static id
75 #endif
76 __objc_block_forward(id, SEL, ...);
77 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
78 Method_t search_for_method_in_list(MethodList_t list, SEL op);
79 id nil_method(id, SEL, ...);
81 /* Given a selector, return the proper forwarding implementation. */
82 __inline__
83 IMP
84 __objc_get_forward_imp (SEL sel)
86 if (__objc_msg_forward)
88 IMP result;
89 if ((result = __objc_msg_forward (sel)))
90 return result;
92 else
94 const char *t = sel->sel_types;
96 if (t && (*t == '[' || *t == '(' || *t == '{')
97 #ifdef OBJC_MAX_STRUCT_BY_VALUE
98 && objc_sizeof_type(t) > OBJC_MAX_STRUCT_BY_VALUE
99 #endif
101 return (IMP)__objc_block_forward;
102 else if (t && (*t == 'f' || *t == 'd'))
103 return (IMP)__objc_double_forward;
104 else
105 return (IMP)__objc_word_forward;
109 /* Given a class and selector, return the selector's implementation. */
110 __inline__
112 get_imp (Class class, SEL sel)
114 void* res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
115 if (res == 0)
117 /* Not a valid method */
118 if(class->dtable == __objc_uninstalled_dtable)
120 /* The dispatch table needs to be installed. */
121 objc_mutex_lock(__objc_runtime_mutex);
122 __objc_install_dispatch_table_for_class (class);
123 objc_mutex_unlock(__objc_runtime_mutex);
124 /* Call ourselves with the installed dispatch table
125 and get the real method */
126 res = get_imp(class, sel);
128 else
130 /* The dispatch table has been installed so the
131 method just doesn't exist for the class.
132 Return the forwarding implementation. */
133 res = __objc_get_forward_imp(sel);
136 return res;
139 /* Query if an object can respond to a selector, returns YES if the
140 object implements the selector otherwise NO. Does not check if the
141 method can be forwarded. */
142 __inline__
143 BOOL
144 __objc_responds_to (id object, SEL sel)
146 void* res;
148 /* Install dispatch table if need be */
149 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
151 objc_mutex_lock(__objc_runtime_mutex);
152 __objc_install_dispatch_table_for_class (object->class_pointer);
153 objc_mutex_unlock(__objc_runtime_mutex);
156 /* Get the method from the dispatch table */
157 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
158 return (res != 0);
161 /* This is the lookup function. All entries in the table are either a
162 valid method *or* zero. If zero then either the dispatch table
163 needs to be installed or it doesn't exist and forwarding is attempted. */
164 __inline__
166 objc_msg_lookup(id receiver, SEL op)
168 IMP result;
169 if(receiver)
171 result = sarray_get_safe (receiver->class_pointer->dtable,
172 (sidx)op->sel_id);
173 if (result == 0)
175 /* Not a valid method */
176 if(receiver->class_pointer->dtable == __objc_uninstalled_dtable)
178 /* The dispatch table needs to be installed.
179 This happens on the very first method call to the class. */
180 __objc_init_install_dtable(receiver, op);
182 /* Get real method for this in newly installed dtable */
183 result = get_imp(receiver->class_pointer, op);
185 else
187 /* The dispatch table has been installed so the
188 method just doesn't exist for the class.
189 Attempt to forward the method. */
190 result = __objc_get_forward_imp(op);
193 return result;
195 else
196 return nil_method;
200 objc_msg_lookup_super (Super_t super, SEL sel)
202 if (super->self)
203 return get_imp (super->class, sel);
204 else
205 return nil_method;
208 int method_get_sizeof_arguments (Method*);
210 retval_t
211 objc_msg_sendv(id object, SEL op, arglist_t arg_frame)
213 Method* m = class_get_instance_method(object->class_pointer, op);
214 const char *type;
215 *((id*)method_get_first_argument (m, arg_frame, &type)) = object;
216 *((SEL*)method_get_next_argument (arg_frame, &type)) = op;
217 return __builtin_apply((apply_t)m->method_imp,
218 arg_frame,
219 method_get_sizeof_arguments (m));
222 void
223 __objc_init_dispatch_tables()
225 __objc_uninstalled_dtable
226 = sarray_new(200, 0);
229 /* This function is called by objc_msg_lookup when the
230 dispatch table needs to be installed; thus it is called once
231 for each class, namely when the very first message is sent to it. */
232 static void
233 __objc_init_install_dtable(id receiver, SEL op)
235 /* This may happen, if the programmer has taken the address of a
236 method before the dtable was initialized... too bad for him! */
237 if(receiver->class_pointer->dtable != __objc_uninstalled_dtable)
238 return;
240 objc_mutex_lock(__objc_runtime_mutex);
242 if(CLS_ISCLASS(receiver->class_pointer))
244 /* receiver is an ordinary object */
245 assert(CLS_ISCLASS(receiver->class_pointer));
247 /* install instance methods table */
248 __objc_install_dispatch_table_for_class (receiver->class_pointer);
250 /* call +initialize -- this will in turn install the factory
251 dispatch table if not already done :-) */
252 __objc_send_initialize(receiver->class_pointer);
254 else
256 /* receiver is a class object */
257 assert(CLS_ISCLASS((Class)receiver));
258 assert(CLS_ISMETA(receiver->class_pointer));
260 /* Install real dtable for factory methods */
261 __objc_install_dispatch_table_for_class (receiver->class_pointer);
263 __objc_send_initialize((Class)receiver);
265 objc_mutex_unlock(__objc_runtime_mutex);
268 /* Install dummy table for class which causes the first message to
269 that class (or instances hereof) to be initialized properly */
270 void
271 __objc_install_premature_dtable(Class class)
273 assert(__objc_uninstalled_dtable);
274 class->dtable = __objc_uninstalled_dtable;
277 /* Send +initialize to class if not already done */
278 static void
279 __objc_send_initialize(Class class)
281 /* This *must* be a class object */
282 assert(CLS_ISCLASS(class));
283 assert(!CLS_ISMETA(class));
285 if (!CLS_ISINITIALIZED(class))
287 CLS_SETINITIALIZED(class);
288 CLS_SETINITIALIZED(class->class_pointer);
290 /* Create the garbage collector type memory description */
291 __objc_generate_gc_type_description (class);
293 if(class->super_class)
294 __objc_send_initialize(class->super_class);
297 SEL op = sel_register_name ("initialize");
298 IMP imp = 0;
299 MethodList_t method_list = class->class_pointer->methods;
301 while (method_list) {
302 int i;
303 Method_t method;
305 for (i = 0; i< method_list->method_count; i++) {
306 method = &(method_list->method_list[i]);
307 if (method->method_name
308 && method->method_name->sel_id == op->sel_id) {
309 imp = method->method_imp;
310 break;
314 if (imp)
315 break;
317 method_list = method_list->method_next;
320 if (imp)
321 (*imp)((id)class, op);
327 /* Walk on the methods list of class and install the methods in the reverse
328 order of the lists. Since methods added by categories are before the methods
329 of class in the methods list, this allows categories to substitute methods
330 declared in class. However if more than one category replaces the same
331 method nothing is guaranteed about what method will be used.
332 Assumes that __objc_runtime_mutex is locked down. */
333 static void
334 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
336 int i;
338 if (!method_list)
339 return;
341 if (method_list->method_next)
342 __objc_install_methods_in_dtable (class, method_list->method_next);
344 for (i = 0; i < method_list->method_count; i++)
346 Method_t method = &(method_list->method_list[i]);
347 sarray_at_put_safe (class->dtable,
348 (sidx) method->method_name->sel_id,
349 method->method_imp);
353 /* Assumes that __objc_runtime_mutex is locked down. */
354 static void
355 __objc_install_dispatch_table_for_class (Class class)
357 Class super;
359 /* If the class has not yet had its class links resolved, we must
360 re-compute all class links */
361 if(!CLS_ISRESOLV(class))
362 __objc_resolve_class_links();
364 super = class->super_class;
366 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
367 __objc_install_dispatch_table_for_class (super);
369 /* Allocate dtable if necessary */
370 if (super == 0)
372 objc_mutex_lock(__objc_runtime_mutex);
373 class->dtable = sarray_new (__objc_selector_max_index, 0);
374 objc_mutex_unlock(__objc_runtime_mutex);
376 else
377 class->dtable = sarray_lazy_copy (super->dtable);
379 __objc_install_methods_in_dtable (class, class->methods);
382 void
383 __objc_update_dispatch_table_for_class (Class class)
385 Class next;
386 struct sarray *arr;
388 /* not yet installed -- skip it */
389 if (class->dtable == __objc_uninstalled_dtable)
390 return;
392 objc_mutex_lock(__objc_runtime_mutex);
394 arr = class->dtable;
395 __objc_install_premature_dtable (class); /* someone might require it... */
396 sarray_free (arr); /* release memory */
398 /* could have been lazy... */
399 __objc_install_dispatch_table_for_class (class);
401 if (class->subclass_list) /* Traverse subclasses */
402 for (next = class->subclass_list; next; next = next->sibling_class)
403 __objc_update_dispatch_table_for_class (next);
405 objc_mutex_unlock(__objc_runtime_mutex);
409 /* This function adds a method list to a class. This function is
410 typically called by another function specific to the run-time. As
411 such this function does not worry about thread safe issues.
413 This one is only called for categories. Class objects have their
414 methods installed right away, and their selectors are made into
415 SEL's by the function __objc_register_selectors_from_class. */
416 void
417 class_add_method_list (Class class, MethodList_t list)
419 int i;
421 /* Passing of a linked list is not allowed. Do multiple calls. */
422 assert (!list->method_next);
424 /* Check for duplicates. */
425 for (i = 0; i < list->method_count; ++i)
427 Method_t method = &list->method_list[i];
429 if (method->method_name) /* Sometimes these are NULL */
431 /* This is where selector names are transmogrified to SEL's */
432 method->method_name =
433 sel_register_typed_name ((const char*)method->method_name,
434 method->method_types);
438 /* Add the methods to the class's method list. */
439 list->method_next = class->methods;
440 class->methods = list;
442 /* Update the dispatch table of class */
443 __objc_update_dispatch_table_for_class (class);
446 Method_t
447 class_get_instance_method(Class class, SEL op)
449 return search_for_method_in_hierarchy(class, op);
452 Method_t
453 class_get_class_method(MetaClass class, SEL op)
455 return search_for_method_in_hierarchy(class, op);
459 /* Search for a method starting from the current class up its hierarchy.
460 Return a pointer to the method's method structure if found. NULL
461 otherwise. */
463 static Method_t
464 search_for_method_in_hierarchy (Class cls, SEL sel)
466 Method_t method = NULL;
467 Class class;
469 if (! sel_is_mapped (sel))
470 return NULL;
472 /* Scan the method list of the class. If the method isn't found in the
473 list then step to its super class. */
474 for (class = cls; ((! method) && class); class = class->super_class)
475 method = search_for_method_in_list (class->methods, sel);
477 return method;
482 /* Given a linked list of method and a method's name. Search for the named
483 method's method structure. Return a pointer to the method's method
484 structure if found. NULL otherwise. */
485 Method_t
486 search_for_method_in_list (MethodList_t list, SEL op)
488 MethodList_t method_list = list;
490 if (! sel_is_mapped (op))
491 return NULL;
493 /* If not found then we'll search the list. */
494 while (method_list)
496 int i;
498 /* Search the method list. */
499 for (i = 0; i < method_list->method_count; ++i)
501 Method_t method = &method_list->method_list[i];
503 if (method->method_name)
504 if (method->method_name->sel_id == op->sel_id)
505 return method;
508 /* The method wasn't found. Follow the link to the next list of
509 methods. */
510 method_list = method_list->method_next;
513 return NULL;
516 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
518 /* Forwarding pointers/integers through the normal registers */
519 static id
520 __objc_word_forward (id rcv, SEL op, ...)
522 void *args, *res;
524 args = __builtin_apply_args ();
525 res = __objc_forward (rcv, op, args);
526 if (res)
527 __builtin_return (res);
528 else
529 return res;
532 /* Specific routine for forwarding floats/double because of
533 architectural differences on some processors. i386s for
534 example which uses a floating point stack versus general
535 registers for floating point numbers. This forward routine
536 makes sure that GCC restores the proper return values */
537 static double
538 __objc_double_forward (id rcv, SEL op, ...)
540 void *args, *res;
542 args = __builtin_apply_args ();
543 res = __objc_forward (rcv, op, args);
544 __builtin_return (res);
547 #if INVISIBLE_STRUCT_RETURN
548 static __big
549 #else
550 static id
551 #endif
552 __objc_block_forward (id rcv, SEL op, ...)
554 void *args, *res;
556 args = __builtin_apply_args ();
557 res = __objc_forward (rcv, op, args);
558 if (res)
559 __builtin_return (res);
560 else
561 #if INVISIBLE_STRUCT_RETURN
562 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
563 #else
564 return nil;
565 #endif
569 /* This function is installed in the dispatch table for all methods which are
570 not implemented. Thus, it is called when a selector is not recognized. */
571 static retval_t
572 __objc_forward (id object, SEL sel, arglist_t args)
574 IMP imp;
575 static SEL frwd_sel = 0; /* !T:SAFE2 */
576 SEL err_sel;
578 /* first try if the object understands forward:: */
579 if (!frwd_sel)
580 frwd_sel = sel_get_any_uid("forward::");
582 if (__objc_responds_to (object, frwd_sel))
584 imp = get_imp(object->class_pointer, frwd_sel);
585 return (*imp)(object, frwd_sel, sel, args);
588 /* If the object recognizes the doesNotRecognize: method then we're going
589 to send it. */
590 err_sel = sel_get_any_uid ("doesNotRecognize:");
591 if (__objc_responds_to (object, err_sel))
593 imp = get_imp (object->class_pointer, err_sel);
594 return (*imp) (object, err_sel, sel);
597 /* The object doesn't recognize the method. Check for responding to
598 error:. If it does then sent it. */
600 char msg[256 + strlen ((const char*)sel_get_name (sel))
601 + strlen ((const char*)object->class_pointer->name)];
603 sprintf (msg, "(%s) %s does not recognize %s",
604 (CLS_ISMETA(object->class_pointer)
605 ? "class"
606 : "instance" ),
607 object->class_pointer->name, sel_get_name (sel));
609 err_sel = sel_get_any_uid ("error:");
610 if (__objc_responds_to (object, err_sel))
612 imp = get_imp (object->class_pointer, err_sel);
613 return (*imp) (object, sel_get_any_uid ("error:"), msg);
616 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
617 a default action is taken. */
618 objc_error (object, OBJC_ERR_UNIMPLEMENTED, "%s\n", msg);
620 return 0;
624 void
625 __objc_print_dtable_stats()
627 int total = 0;
629 objc_mutex_lock(__objc_runtime_mutex);
631 #ifdef OBJC_SPARSE2
632 printf("memory usage: (%s)\n", "2-level sparse arrays");
633 #else
634 printf("memory usage: (%s)\n", "3-level sparse arrays");
635 #endif
637 printf("arrays: %d = %ld bytes\n", narrays,
638 (long)narrays*sizeof(struct sarray));
639 total += narrays*sizeof(struct sarray);
640 printf("buckets: %d = %ld bytes\n", nbuckets,
641 (long)nbuckets*sizeof(struct sbucket));
642 total += nbuckets*sizeof(struct sbucket);
644 printf("idxtables: %d = %ld bytes\n", idxsize, (long)idxsize*sizeof(void*));
645 total += idxsize*sizeof(void*);
646 printf("-----------------------------------\n");
647 printf("total: %d bytes\n", total);
648 printf("===================================\n");
650 objc_mutex_unlock(__objc_runtime_mutex);
653 /* Returns the uninstalled dispatch table indicator.
654 If a class' dispatch table points to __objc_uninstalled_dtable
655 then that means it needs its dispatch table to be installed. */
656 __inline__
657 struct sarray*
658 objc_get_uninstalled_dtable()
660 return __objc_uninstalled_dtable;