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
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"
32 /* this is how we hack STRUCT_VALUE to be 1 or 0 */
33 #define gen_rtx(args...) 1
37 #define INVISIBLE_STRUCT_RETURN 1
39 #define INVISIBLE_STRUCT_RETURN 0
42 /* The uninstalled dispatch table */
43 struct sarray* __objc_uninstalled_dtable = 0; /* !T:MUTEX */
45 /* Send +initialize to class */
46 static void __objc_send_initialize(Class);
48 static void __objc_install_dispatch_table_for_class (Class);
50 /* Forward declare some functions */
51 static void __objc_init_install_dtable(id, SEL);
53 /* Various forwarding functions that are used based upon the
54 return type for the selector.
55 __objc_block_forward for structures.
56 __objc_double_forward for floats/doubles.
57 __objc_word_forward for pointers or types that fit in registers.
59 static double __objc_double_forward(id, SEL, ...);
60 static id __objc_word_forward(id, SEL, ...);
61 typedef struct { id many[8]; } __big;
62 #if INVISIBLE_STRUCT_RETURN
67 __objc_block_forward(id, SEL, ...);
68 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
69 static Method_t search_for_method_in_list(MethodList_t list, SEL op);
70 id nil_method(id, SEL, ...);
72 /* Given a class and selector, return the selector's implementation. */
75 get_imp (Class class, SEL sel)
78 void* res = sarray_get (class->dtable, (size_t) sel->sel_id);
79 if(res == __objc_init_install_dtable)
81 objc_mutex_lock(__objc_runtime_mutex);
82 __objc_install_dispatch_table_for_class (class);
83 objc_mutex_unlock(__objc_runtime_mutex);
84 res = sarray_get (class->dtable, (size_t) sel->sel_id);
88 const char *t = sel->sel_types;
89 if (t && (*t == '[' || *t == '(' || *t == '{'))
90 res = (IMP)__objc_block_forward;
91 else if (t && (*t == 'f' || *t == 'd'))
92 res = (IMP)__objc_double_forward;
94 res = (IMP)__objc_word_forward;
100 __objc_responds_to (id object, SEL sel)
102 void* res = sarray_get (object->class_pointer->dtable, (size_t) sel->sel_id);
103 if(res == __objc_init_install_dtable)
105 objc_mutex_lock(__objc_runtime_mutex);
106 __objc_install_dispatch_table_for_class (object->class_pointer);
107 objc_mutex_unlock(__objc_runtime_mutex);
108 res = sarray_get (object->class_pointer->dtable, (size_t) sel->sel_id);
113 /* This is the lookup function. All entries in the table are either a
114 valid method *or* one of `__objc_missing_method' which calls
115 forward:: etc, or `__objc_init_install_dtable' which installs the
118 objc_msg_lookup(id receiver, SEL op)
123 result = sarray_get(receiver->class_pointer->dtable, (sidx)op->sel_id);
126 const char *t = op->sel_types;
127 if (t && (*t == '[' || *t == '(' || *t == '{'))
128 result = (IMP)__objc_block_forward;
129 else if (t && (*t == 'f' || *t == 'd'))
130 result = (IMP)__objc_double_forward;
132 result = (IMP)__objc_word_forward;
141 objc_msg_lookup_super (Super_t super, SEL sel)
144 return get_imp (super->class, sel);
149 int method_get_sizeof_arguments (Method*);
152 objc_msg_sendv(id object, SEL op, arglist_t arg_frame)
154 Method* m = class_get_instance_method(object->class_pointer, op);
156 *((id*)method_get_first_argument (m, arg_frame, &type)) = object;
157 *((SEL*)method_get_next_argument (arg_frame, &type)) = op;
158 return __builtin_apply((apply_t)m->method_imp,
160 method_get_sizeof_arguments (m));
163 void __objc_init_dispatch_tables()
165 __objc_uninstalled_dtable
166 = sarray_new(200, __objc_init_install_dtable);
169 /* Various return functions that are used based upon the
170 return type for the selector.
171 __objc_block_return for structures.
172 __objc_double_return for floats/doubles.
173 __objc_word_return for pointers or types that fit in registers.
175 #if INVISIBLE_STRUCT_RETURN
180 __objc_block_return(IMP imp, void * args)
182 void * result = __builtin_apply((apply_t)imp, args, 96);
184 __builtin_return (result);
190 __objc_word_return(IMP imp, void * args)
192 void * result = __builtin_apply((apply_t)imp, args, 96);
194 __builtin_return (result);
200 __objc_double_return(IMP imp, void * args)
202 void * result = __builtin_apply((apply_t)imp, args, 96);
204 __builtin_return (result);
209 /* This one is a bit hairy. This function is installed in the
210 premature dispatch table, and thus called once for each class,
211 namely when the very first message is send to it. */
213 static void __objc_init_install_dtable(id receiver, SEL op)
215 __label__ already_initialized;
221 /* This may happen, if the programmer has taken the address of a
222 method before the dtable was initialized... too bad for him! */
223 if(receiver->class_pointer->dtable != __objc_uninstalled_dtable)
224 goto already_initialized;
226 objc_mutex_lock(__objc_runtime_mutex);
228 if(CLS_ISCLASS(receiver->class_pointer))
230 /* receiver is an ordinary object */
231 assert(CLS_ISCLASS(receiver->class_pointer));
233 /* install instance methods table */
234 __objc_install_dispatch_table_for_class (receiver->class_pointer);
236 /* call +initialize -- this will in turn install the factory
237 dispatch table if not already done :-) */
238 __objc_send_initialize(receiver->class_pointer);
242 /* receiver is a class object */
243 assert(CLS_ISCLASS((Class)receiver));
244 assert(CLS_ISMETA(receiver->class_pointer));
246 /* Install real dtable for factory methods */
247 __objc_install_dispatch_table_for_class (receiver->class_pointer);
249 if (strcmp (sel_get_name (op), "initialize"))
250 __objc_send_initialize((Class)receiver);
252 CLS_SETINITIALIZED((Class)receiver);
254 objc_mutex_unlock(__objc_runtime_mutex);
258 /* Get real method for this in newly installed dtable */
259 imp = get_imp(receiver->class_pointer, op);
261 /* Perform the appropriate return based upon the method return type */
262 args = __builtin_apply_args();
264 if (t && (*t == '[' || *t == '(' || *t == '{'))
265 ((id(*)())__objc_block_return)(imp, args);
266 else if (t && (*t == 'f' || *t == 'd'))
267 ((id(*)())__objc_double_return)(imp, args);
269 __objc_word_return(imp, args);
272 /* Install dummy table for class which causes the first message to
273 that class (or instances hereof) to be initialized properly */
274 void __objc_install_premature_dtable(Class class)
276 assert(__objc_uninstalled_dtable);
277 class->dtable = __objc_uninstalled_dtable;
280 /* Send +initialize to class if not already done */
281 static void __objc_send_initialize(Class class)
283 /* This *must* be a class object */
284 assert(CLS_ISCLASS(class));
285 assert(!CLS_ISMETA(class));
287 if (!CLS_ISINITIALIZED(class))
289 CLS_SETINITIALIZED(class);
290 CLS_SETINITIALIZED(class->class_pointer);
292 if(class->super_class)
293 __objc_send_initialize(class->super_class);
296 SEL op = sel_register_name ("initialize");
297 Class tmpclass = class;
300 while (!imp && tmpclass) {
301 MethodList_t method_list = tmpclass->class_pointer->methods;
303 while(!imp && method_list) {
307 for (i=0;i<method_list->method_count;i++) {
308 method = &(method_list->method_list[i]);
309 if (method->method_name
310 && method->method_name->sel_id == op->sel_id) {
311 imp = method->method_imp;
316 method_list = method_list->method_next;
320 tmpclass = tmpclass->super_class;
323 (*imp)((id)class, op);
329 /* Walk on the methods list of class and install the methods in the reverse
330 order of the lists. Since methods added by categories are before the methods
331 of class in the methods list, this allows categories to substitute methods
332 declared in class. However if more than one category replace the same method
333 nothing is guarranteed about what method will be used.
334 Assumes that __objc_runtime_mutex is locked down. */
336 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
343 if (method_list->method_next)
344 __objc_install_methods_in_dtable (class, method_list->method_next);
346 for (i = 0; i < method_list->method_count; i++)
348 Method_t method = &(method_list->method_list[i]);
349 sarray_at_put_safe (class->dtable,
350 (sidx) method->method_name->sel_id,
355 /* Assumes that __objc_runtime_mutex is locked down. */
357 __objc_install_dispatch_table_for_class (Class class)
362 /* If the class has not yet had it's class links resolved, we must
363 re-compute all class links */
364 if(!CLS_ISRESOLV(class))
365 __objc_resolve_class_links();
367 super = class->super_class;
369 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
370 __objc_install_dispatch_table_for_class (super);
372 /* Allocate dtable if necessary */
375 objc_mutex_lock(__objc_runtime_mutex);
376 class->dtable = sarray_new (__objc_selector_max_index, 0);
377 objc_mutex_unlock(__objc_runtime_mutex);
380 class->dtable = sarray_lazy_copy (super->dtable);
382 __objc_install_methods_in_dtable (class, class->methods);
385 void __objc_update_dispatch_table_for_class (Class class)
390 /* not yet installed -- skip it */
391 if (class->dtable == __objc_uninstalled_dtable)
394 objc_mutex_lock(__objc_runtime_mutex);
397 __objc_install_premature_dtable (class); /* someone might require it... */
398 sarray_free (arr); /* release memory */
400 /* could have been lazy... */
401 __objc_install_dispatch_table_for_class (class);
403 if (class->subclass_list) /* Traverse subclasses */
404 for (next = class->subclass_list; next; next = next->sibling_class)
405 __objc_update_dispatch_table_for_class (next);
407 objc_mutex_unlock(__objc_runtime_mutex);
411 /* This function adds a method list to a class. This function is
412 typically called by another function specific to the run-time. As
413 such this function does not worry about thread safe issues.
415 This one is only called for categories. Class objects have their
416 methods installed right away, and their selectors are made into
417 SEL's by the function __objc_register_selectors_from_class. */
419 class_add_method_list (Class class, MethodList_t list)
423 /* Passing of a linked list is not allowed. Do multiple calls. */
424 assert (!list->method_next);
426 /* Check for duplicates. */
427 for (i = 0; i < list->method_count; ++i)
429 Method_t method = &list->method_list[i];
431 if (method->method_name) /* Sometimes these are NULL */
433 /* This is where selector names are transmogrified to SEL's */
434 method->method_name =
435 sel_register_typed_name ((const char*)method->method_name,
436 method->method_types);
440 /* Add the methods to the class's method list. */
441 list->method_next = class->methods;
442 class->methods = list;
444 /* Update the dispatch table of class */
445 __objc_update_dispatch_table_for_class (class);
449 class_get_instance_method(Class class, SEL op)
451 return search_for_method_in_hierarchy(class, op);
455 class_get_class_method(MetaClass class, SEL op)
457 return search_for_method_in_hierarchy(class, op);
461 /* Search for a method starting from the current class up its hierarchy.
462 Return a pointer to the method's method structure if found. NULL
466 search_for_method_in_hierarchy (Class cls, SEL sel)
468 Method_t method = NULL;
471 if (! sel_is_mapped (sel))
474 /* Scan the method list of the class. If the method isn't found in the
475 list then step to its super class. */
476 for (class = cls; ((! method) && class); class = class->super_class)
477 method = search_for_method_in_list (class->methods, sel);
484 /* Given a linked list of method and a method's name. Search for the named
485 method's method structure. Return a pointer to the method's method
486 structure if found. NULL otherwise. */
488 search_for_method_in_list (MethodList_t list, SEL op)
490 MethodList_t method_list = list;
492 if (! sel_is_mapped (op))
495 /* If not found then we'll search the list. */
500 /* Search the method list. */
501 for (i = 0; i < method_list->method_count; ++i)
503 Method_t method = &method_list->method_list[i];
505 if (method->method_name)
506 if (method->method_name->sel_id == op->sel_id)
510 /* The method wasn't found. Follow the link to the next list of
512 method_list = method_list->method_next;
518 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
520 /* Forwarding pointers/integers through the normal registers */
522 __objc_word_forward (id rcv, SEL op, ...)
526 args = __builtin_apply_args ();
527 res = __objc_forward (rcv, op, args);
529 __builtin_return (res);
534 /* Specific routine for forwarding floats/double because of
535 architectural differences on some processors. i386s for
536 example which uses a floating point stack versus general
537 registers for floating point numbers. This forward routine
538 makes sure that GCC restores the proper return values */
540 __objc_double_forward (id rcv, SEL op, ...)
544 args = __builtin_apply_args ();
545 res = __objc_forward (rcv, op, args);
546 __builtin_return (res);
549 #if INVISIBLE_STRUCT_RETURN
554 __objc_block_forward (id rcv, SEL op, ...)
558 args = __builtin_apply_args ();
559 res = __objc_forward (rcv, op, args);
561 __builtin_return (res);
565 /* This function is installed in the dispatch table for all methods which are
566 not implemented. Thus, it is called when a selector is not recognized. */
568 __objc_forward (id object, SEL sel, arglist_t args)
571 static SEL frwd_sel = 0; /* !T:SAFE2 */
574 /* first try if the object understands forward:: */
576 frwd_sel = sel_get_any_uid("forward::");
578 if (__objc_responds_to (object, frwd_sel))
580 imp = get_imp(object->class_pointer, frwd_sel);
581 return (*imp)(object, frwd_sel, sel, args);
584 /* If the object recognizes the doesNotRecognize: method then we're going
586 err_sel = sel_get_any_uid ("doesNotRecognize:");
587 if (__objc_responds_to (object, err_sel))
589 imp = get_imp (object->class_pointer, err_sel);
590 return (*imp) (object, err_sel, sel);
593 /* The object doesn't recognize the method. Check for responding to
594 error:. If it does then sent it. */
596 size_t strlen (const char*);
597 char msg[256 + strlen ((const char*)sel_get_name (sel))
598 + strlen ((const char*)object->class_pointer->name)];
600 sprintf (msg, "(%s) %s does not recognize %s",
601 (CLS_ISMETA(object->class_pointer)
604 object->class_pointer->name, sel_get_name (sel));
606 err_sel = sel_get_any_uid ("error:");
607 if (__objc_responds_to (object, err_sel))
609 imp = get_imp (object->class_pointer, err_sel);
610 return (*imp) (object, sel_get_any_uid ("error:"), msg);
613 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
614 a default action is taken. */
615 objc_error (object, OBJC_ERR_UNIMPLEMENTED, "%s\n", msg);
619 void __objc_print_dtable_stats()
623 objc_mutex_lock(__objc_runtime_mutex);
625 printf("memory usage: (%s)\n",
627 "2-level sparse arrays"
629 "3-level sparse arrays"
633 printf("arrays: %d = %ld bytes\n", narrays,
634 (int)narrays*sizeof(struct sarray));
635 total += narrays*sizeof(struct sarray);
636 printf("buckets: %d = %ld bytes\n", nbuckets,
637 (int)nbuckets*sizeof(struct sbucket));
638 total += nbuckets*sizeof(struct sbucket);
640 printf("idxtables: %d = %ld bytes\n", idxsize, (int)idxsize*sizeof(void*));
641 total += idxsize*sizeof(void*);
642 printf("-----------------------------------\n");
643 printf("total: %d bytes\n", total);
644 printf("===================================\n");
646 objc_mutex_unlock(__objc_runtime_mutex);
649 /* Returns the dispatch table */
652 objc_get_uninstalled_dtable()
654 return __objc_uninstalled_dtable;