import of gcc-2.8
[official-gcc.git] / gcc / objc / sendmsg.c
blob6f3487c260ee3daa59345a5a57874423557cb701
1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc.
3 Contributed by Kresten Krab Thorup
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2, or (at your option) any later version.
11 GNU CC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 details.
16 You should have received a copy of the GNU General Public License along with
17 GNU CC; see the file COPYING. If not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* As a special exception, if you link this library with files compiled with
22 GCC to produce an executable, this does not cause the resulting executable
23 to be covered by the GNU General Public License. This exception does not
24 however invalidate any other reasons why the executable file might be
25 covered by the GNU General Public License. */
27 #include "../tconfig.h"
28 #include "runtime.h"
29 #include "sarray.h"
30 #include "encoding.h"
31 #include "runtime-info.h"
33 /* this is how we hack STRUCT_VALUE to be 1 or 0 */
34 #define gen_rtx(args...) 1
35 #define rtx int
37 #if !defined(STRUCT_VALUE) || STRUCT_VALUE == 0
38 #define INVISIBLE_STRUCT_RETURN 1
39 #else
40 #define INVISIBLE_STRUCT_RETURN 0
41 #endif
43 /* The uninstalled dispatch table */
44 struct sarray* __objc_uninstalled_dtable = 0; /* !T:MUTEX */
46 /* Send +initialize to class */
47 static void __objc_send_initialize(Class);
49 static void __objc_install_dispatch_table_for_class (Class);
51 /* Forward declare some functions */
52 static void __objc_init_install_dtable(id, SEL);
54 /* Various forwarding functions that are used based upon the
55 return type for the selector.
56 __objc_block_forward for structures.
57 __objc_double_forward for floats/doubles.
58 __objc_word_forward for pointers or types that fit in registers.
60 static double __objc_double_forward(id, SEL, ...);
61 static id __objc_word_forward(id, SEL, ...);
62 typedef struct { id many[8]; } __big;
63 #if INVISIBLE_STRUCT_RETURN
64 static __big
65 #else
66 static id
67 #endif
68 __objc_block_forward(id, SEL, ...);
69 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
70 Method_t search_for_method_in_list(MethodList_t list, SEL op);
71 id nil_method(id, SEL, ...);
73 /* Given a selector, return the proper forwarding implementation. */
74 __inline__
75 IMP
76 __objc_get_forward_imp (SEL sel)
78 const char *t = sel->sel_types;
80 if (t && (*t == '[' || *t == '(' || *t == '{')
81 #ifdef OBJC_MAX_STRUCT_BY_VALUE
82 && objc_sizeof_type(t) > OBJC_MAX_STRUCT_BY_VALUE
83 #endif
85 return (IMP)__objc_block_forward;
86 else if (t && (*t == 'f' || *t == 'd'))
87 return (IMP)__objc_double_forward;
88 else
89 return (IMP)__objc_word_forward;
92 /* Given a class and selector, return the selector's implementation. */
93 __inline__
94 IMP
95 get_imp (Class class, SEL sel)
97 void* res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
98 if (res == 0)
100 /* Not a valid method */
101 if(class->dtable == __objc_uninstalled_dtable)
103 /* The dispatch table needs to be installed. */
104 objc_mutex_lock(__objc_runtime_mutex);
105 __objc_install_dispatch_table_for_class (class);
106 objc_mutex_unlock(__objc_runtime_mutex);
107 /* Call ourselves with the installed dispatch table
108 and get the real method */
109 res = get_imp(class, sel);
111 else
113 /* The dispatch table has been installed so the
114 method just doesn't exist for the class.
115 Return the forwarding implementation. */
116 res = __objc_get_forward_imp(sel);
119 return res;
122 /* Query if an object can respond to a selector, returns YES if the
123 object implements the selector otherwise NO. Does not check if the
124 method can be forwarded. */
125 __inline__
126 BOOL
127 __objc_responds_to (id object, SEL sel)
129 void* res;
131 /* Install dispatch table if need be */
132 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
134 objc_mutex_lock(__objc_runtime_mutex);
135 __objc_install_dispatch_table_for_class (object->class_pointer);
136 objc_mutex_unlock(__objc_runtime_mutex);
139 /* Get the method from the dispatch table */
140 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
141 return (res != 0);
144 /* This is the lookup function. All entries in the table are either a
145 valid method *or* zero. If zero then either the dispatch table
146 needs to be installed or it doesn't exist and forwarding is attempted. */
147 __inline__
149 objc_msg_lookup(id receiver, SEL op)
151 IMP result;
152 if(receiver)
154 result = sarray_get_safe (receiver->class_pointer->dtable,
155 (sidx)op->sel_id);
156 if (result == 0)
158 /* Not a valid method */
159 if(receiver->class_pointer->dtable == __objc_uninstalled_dtable)
161 /* The dispatch table needs to be installed.
162 This happens on the very first method call to the class. */
163 __objc_init_install_dtable(receiver, op);
165 /* Get real method for this in newly installed dtable */
166 result = get_imp(receiver->class_pointer, op);
168 else
170 /* The dispatch table has been installed so the
171 method just doesn't exist for the class.
172 Attempt to forward the method. */
173 result = __objc_get_forward_imp(op);
176 return result;
178 else
179 return nil_method;
183 objc_msg_lookup_super (Super_t super, SEL sel)
185 if (super->self)
186 return get_imp (super->class, sel);
187 else
188 return nil_method;
191 int method_get_sizeof_arguments (Method*);
193 retval_t
194 objc_msg_sendv(id object, SEL op, arglist_t arg_frame)
196 Method* m = class_get_instance_method(object->class_pointer, op);
197 const char *type;
198 *((id*)method_get_first_argument (m, arg_frame, &type)) = object;
199 *((SEL*)method_get_next_argument (arg_frame, &type)) = op;
200 return __builtin_apply((apply_t)m->method_imp,
201 arg_frame,
202 method_get_sizeof_arguments (m));
205 void
206 __objc_init_dispatch_tables()
208 __objc_uninstalled_dtable
209 = sarray_new(200, 0);
212 /* This function is called by objc_msg_lookup when the
213 dispatch table needs to be installed; thus it is called once
214 for each class, namely when the very first message is sent to it. */
215 static void
216 __objc_init_install_dtable(id receiver, SEL op)
218 /* This may happen, if the programmer has taken the address of a
219 method before the dtable was initialized... too bad for him! */
220 if(receiver->class_pointer->dtable != __objc_uninstalled_dtable)
221 return;
223 objc_mutex_lock(__objc_runtime_mutex);
225 if(CLS_ISCLASS(receiver->class_pointer))
227 /* receiver is an ordinary object */
228 assert(CLS_ISCLASS(receiver->class_pointer));
230 /* install instance methods table */
231 __objc_install_dispatch_table_for_class (receiver->class_pointer);
233 /* call +initialize -- this will in turn install the factory
234 dispatch table if not already done :-) */
235 __objc_send_initialize(receiver->class_pointer);
237 else
239 /* receiver is a class object */
240 assert(CLS_ISCLASS((Class)receiver));
241 assert(CLS_ISMETA(receiver->class_pointer));
243 /* Install real dtable for factory methods */
244 __objc_install_dispatch_table_for_class (receiver->class_pointer);
246 if (strcmp (sel_get_name (op), "initialize"))
247 __objc_send_initialize((Class)receiver);
248 else
249 CLS_SETINITIALIZED((Class)receiver);
251 objc_mutex_unlock(__objc_runtime_mutex);
254 /* Install dummy table for class which causes the first message to
255 that class (or instances hereof) to be initialized properly */
256 void
257 __objc_install_premature_dtable(Class class)
259 assert(__objc_uninstalled_dtable);
260 class->dtable = __objc_uninstalled_dtable;
263 /* Send +initialize to class if not already done */
264 static void
265 __objc_send_initialize(Class class)
267 /* This *must* be a class object */
268 assert(CLS_ISCLASS(class));
269 assert(!CLS_ISMETA(class));
271 if (!CLS_ISINITIALIZED(class))
273 CLS_SETINITIALIZED(class);
274 CLS_SETINITIALIZED(class->class_pointer);
276 if(class->super_class)
277 __objc_send_initialize(class->super_class);
280 SEL op = sel_register_name ("initialize");
281 Class tmpclass = class;
282 IMP imp = 0;
284 while (!imp && tmpclass) {
285 MethodList_t method_list = tmpclass->class_pointer->methods;
287 while(!imp && method_list) {
288 int i;
289 Method_t method;
291 for (i=0;i<method_list->method_count;i++) {
292 method = &(method_list->method_list[i]);
293 if (method->method_name
294 && method->method_name->sel_id == op->sel_id) {
295 imp = method->method_imp;
296 break;
300 method_list = method_list->method_next;
304 tmpclass = tmpclass->super_class;
306 if (imp)
307 (*imp)((id)class, op);
313 /* Walk on the methods list of class and install the methods in the reverse
314 order of the lists. Since methods added by categories are before the methods
315 of class in the methods list, this allows categories to substitute methods
316 declared in class. However if more than one category replaces the same
317 method nothing is guaranteed about what method will be used.
318 Assumes that __objc_runtime_mutex is locked down. */
319 static void
320 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
322 int i;
324 if (!method_list)
325 return;
327 if (method_list->method_next)
328 __objc_install_methods_in_dtable (class, method_list->method_next);
330 for (i = 0; i < method_list->method_count; i++)
332 Method_t method = &(method_list->method_list[i]);
333 sarray_at_put_safe (class->dtable,
334 (sidx) method->method_name->sel_id,
335 method->method_imp);
339 /* Assumes that __objc_runtime_mutex is locked down. */
340 static void
341 __objc_install_dispatch_table_for_class (Class class)
343 Class super;
345 /* If the class has not yet had it's class links resolved, we must
346 re-compute all class links */
347 if(!CLS_ISRESOLV(class))
348 __objc_resolve_class_links();
350 super = class->super_class;
352 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
353 __objc_install_dispatch_table_for_class (super);
355 /* Allocate dtable if necessary */
356 if (super == 0)
358 objc_mutex_lock(__objc_runtime_mutex);
359 class->dtable = sarray_new (__objc_selector_max_index, 0);
360 objc_mutex_unlock(__objc_runtime_mutex);
362 else
363 class->dtable = sarray_lazy_copy (super->dtable);
365 __objc_install_methods_in_dtable (class, class->methods);
368 void
369 __objc_update_dispatch_table_for_class (Class class)
371 Class next;
372 struct sarray *arr;
374 /* not yet installed -- skip it */
375 if (class->dtable == __objc_uninstalled_dtable)
376 return;
378 objc_mutex_lock(__objc_runtime_mutex);
380 arr = class->dtable;
381 __objc_install_premature_dtable (class); /* someone might require it... */
382 sarray_free (arr); /* release memory */
384 /* could have been lazy... */
385 __objc_install_dispatch_table_for_class (class);
387 if (class->subclass_list) /* Traverse subclasses */
388 for (next = class->subclass_list; next; next = next->sibling_class)
389 __objc_update_dispatch_table_for_class (next);
391 objc_mutex_unlock(__objc_runtime_mutex);
395 /* This function adds a method list to a class. This function is
396 typically called by another function specific to the run-time. As
397 such this function does not worry about thread safe issues.
399 This one is only called for categories. Class objects have their
400 methods installed right away, and their selectors are made into
401 SEL's by the function __objc_register_selectors_from_class. */
402 void
403 class_add_method_list (Class class, MethodList_t list)
405 int i;
407 /* Passing of a linked list is not allowed. Do multiple calls. */
408 assert (!list->method_next);
410 /* Check for duplicates. */
411 for (i = 0; i < list->method_count; ++i)
413 Method_t method = &list->method_list[i];
415 if (method->method_name) /* Sometimes these are NULL */
417 /* This is where selector names are transmogrified to SEL's */
418 method->method_name =
419 sel_register_typed_name ((const char*)method->method_name,
420 method->method_types);
424 /* Add the methods to the class's method list. */
425 list->method_next = class->methods;
426 class->methods = list;
428 /* Update the dispatch table of class */
429 __objc_update_dispatch_table_for_class (class);
432 Method_t
433 class_get_instance_method(Class class, SEL op)
435 return search_for_method_in_hierarchy(class, op);
438 Method_t
439 class_get_class_method(MetaClass class, SEL op)
441 return search_for_method_in_hierarchy(class, op);
445 /* Search for a method starting from the current class up its hierarchy.
446 Return a pointer to the method's method structure if found. NULL
447 otherwise. */
449 static Method_t
450 search_for_method_in_hierarchy (Class cls, SEL sel)
452 Method_t method = NULL;
453 Class class;
455 if (! sel_is_mapped (sel))
456 return NULL;
458 /* Scan the method list of the class. If the method isn't found in the
459 list then step to its super class. */
460 for (class = cls; ((! method) && class); class = class->super_class)
461 method = search_for_method_in_list (class->methods, sel);
463 return method;
468 /* Given a linked list of method and a method's name. Search for the named
469 method's method structure. Return a pointer to the method's method
470 structure if found. NULL otherwise. */
471 Method_t
472 search_for_method_in_list (MethodList_t list, SEL op)
474 MethodList_t method_list = list;
476 if (! sel_is_mapped (op))
477 return NULL;
479 /* If not found then we'll search the list. */
480 while (method_list)
482 int i;
484 /* Search the method list. */
485 for (i = 0; i < method_list->method_count; ++i)
487 Method_t method = &method_list->method_list[i];
489 if (method->method_name)
490 if (method->method_name->sel_id == op->sel_id)
491 return method;
494 /* The method wasn't found. Follow the link to the next list of
495 methods. */
496 method_list = method_list->method_next;
499 return NULL;
502 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
504 /* Forwarding pointers/integers through the normal registers */
505 static id
506 __objc_word_forward (id rcv, SEL op, ...)
508 void *args, *res;
510 args = __builtin_apply_args ();
511 res = __objc_forward (rcv, op, args);
512 if (res)
513 __builtin_return (res);
514 else
515 return res;
518 /* Specific routine for forwarding floats/double because of
519 architectural differences on some processors. i386s for
520 example which uses a floating point stack versus general
521 registers for floating point numbers. This forward routine
522 makes sure that GCC restores the proper return values */
523 static double
524 __objc_double_forward (id rcv, SEL op, ...)
526 void *args, *res;
528 args = __builtin_apply_args ();
529 res = __objc_forward (rcv, op, args);
530 __builtin_return (res);
533 #if INVISIBLE_STRUCT_RETURN
534 static __big
535 #else
536 static id
537 #endif
538 __objc_block_forward (id rcv, SEL op, ...)
540 void *args, *res;
542 args = __builtin_apply_args ();
543 res = __objc_forward (rcv, op, args);
544 if (res)
545 __builtin_return (res);
546 else
547 #if INVISIBLE_STRUCT_RETURN
548 return (__big) {0};
549 #else
550 return nil;
551 #endif
555 /* This function is installed in the dispatch table for all methods which are
556 not implemented. Thus, it is called when a selector is not recognized. */
557 static retval_t
558 __objc_forward (id object, SEL sel, arglist_t args)
560 IMP imp;
561 static SEL frwd_sel = 0; /* !T:SAFE2 */
562 SEL err_sel;
564 /* first try if the object understands forward:: */
565 if (!frwd_sel)
566 frwd_sel = sel_get_any_uid("forward::");
568 if (__objc_responds_to (object, frwd_sel))
570 imp = get_imp(object->class_pointer, frwd_sel);
571 return (*imp)(object, frwd_sel, sel, args);
574 /* If the object recognizes the doesNotRecognize: method then we're going
575 to send it. */
576 err_sel = sel_get_any_uid ("doesNotRecognize:");
577 if (__objc_responds_to (object, err_sel))
579 imp = get_imp (object->class_pointer, err_sel);
580 return (*imp) (object, err_sel, sel);
583 /* The object doesn't recognize the method. Check for responding to
584 error:. If it does then sent it. */
586 size_t strlen (const char*);
587 char msg[256 + strlen ((const char*)sel_get_name (sel))
588 + strlen ((const char*)object->class_pointer->name)];
590 sprintf (msg, "(%s) %s does not recognize %s",
591 (CLS_ISMETA(object->class_pointer)
592 ? "class"
593 : "instance" ),
594 object->class_pointer->name, sel_get_name (sel));
596 err_sel = sel_get_any_uid ("error:");
597 if (__objc_responds_to (object, err_sel))
599 imp = get_imp (object->class_pointer, err_sel);
600 return (*imp) (object, sel_get_any_uid ("error:"), msg);
603 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
604 a default action is taken. */
605 objc_error (object, OBJC_ERR_UNIMPLEMENTED, "%s\n", msg);
607 return 0;
611 void
612 __objc_print_dtable_stats()
614 int total = 0;
616 objc_mutex_lock(__objc_runtime_mutex);
618 printf("memory usage: (%s)\n",
619 #ifdef OBJC_SPARSE2
620 "2-level sparse arrays"
621 #else
622 "3-level sparse arrays"
623 #endif
626 printf("arrays: %d = %ld bytes\n", narrays,
627 (long)narrays*sizeof(struct sarray));
628 total += narrays*sizeof(struct sarray);
629 printf("buckets: %d = %ld bytes\n", nbuckets,
630 (long)nbuckets*sizeof(struct sbucket));
631 total += nbuckets*sizeof(struct sbucket);
633 printf("idxtables: %d = %ld bytes\n", idxsize, (long)idxsize*sizeof(void*));
634 total += idxsize*sizeof(void*);
635 printf("-----------------------------------\n");
636 printf("total: %d bytes\n", total);
637 printf("===================================\n");
639 objc_mutex_unlock(__objc_runtime_mutex);
642 /* Returns the uninstalled dispatch table indicator.
643 If a class' dispatch table points to __objc_uninstalled_dtable
644 then that means it needs its dispatch table to be installed. */
645 __inline__
646 struct sarray*
647 objc_get_uninstalled_dtable()
649 return __objc_uninstalled_dtable;