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
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. */
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
40 #if !defined(STRUCT_VALUE) || STRUCT_VALUE == 0
41 #define INVISIBLE_STRUCT_RETURN 1
43 #define INVISIBLE_STRUCT_RETURN 0
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
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. */
84 __objc_get_forward_imp (SEL sel
)
86 if (__objc_msg_forward
)
89 if ((result
= __objc_msg_forward (sel
)))
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
101 return (IMP
)__objc_block_forward
;
102 else if (t
&& (*t
== 'f' || *t
== 'd'))
103 return (IMP
)__objc_double_forward
;
105 return (IMP
)__objc_word_forward
;
109 /* Given a class and selector, return the selector's implementation. */
112 get_imp (Class
class, SEL sel
)
114 void* res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
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
);
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
);
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. */
144 __objc_responds_to (id object
, SEL sel
)
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
);
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. */
166 objc_msg_lookup(id receiver
, SEL op
)
171 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
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
);
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
);
200 objc_msg_lookup_super (Super_t super
, SEL sel
)
203 return get_imp (super
->class, sel
);
208 int method_get_sizeof_arguments (Method
*);
211 objc_msg_sendv(id object
, SEL op
, arglist_t arg_frame
)
213 Method
* m
= class_get_instance_method(object
->class_pointer
, op
);
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
,
219 method_get_sizeof_arguments (m
));
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. */
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
)
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
);
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 */
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 */
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");
299 MethodList_t method_list
= class->class_pointer
->methods
;
301 while (method_list
) {
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
;
317 method_list
= method_list
->method_next
;
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. */
334 __objc_install_methods_in_dtable (Class
class, MethodList_t method_list
)
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
,
353 /* Assumes that __objc_runtime_mutex is locked down. */
355 __objc_install_dispatch_table_for_class (Class
class)
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 */
372 objc_mutex_lock(__objc_runtime_mutex
);
373 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
374 objc_mutex_unlock(__objc_runtime_mutex
);
377 class->dtable
= sarray_lazy_copy (super
->dtable
);
379 __objc_install_methods_in_dtable (class, class->methods
);
383 __objc_update_dispatch_table_for_class (Class
class)
388 /* not yet installed -- skip it */
389 if (class->dtable
== __objc_uninstalled_dtable
)
392 objc_mutex_lock(__objc_runtime_mutex
);
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. */
417 class_add_method_list (Class
class, MethodList_t list
)
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);
447 class_get_instance_method(Class
class, SEL op
)
449 return search_for_method_in_hierarchy(class, op
);
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
464 search_for_method_in_hierarchy (Class cls
, SEL sel
)
466 Method_t method
= NULL
;
469 if (! sel_is_mapped (sel
))
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
);
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. */
486 search_for_method_in_list (MethodList_t list
, SEL op
)
488 MethodList_t method_list
= list
;
490 if (! sel_is_mapped (op
))
493 /* If not found then we'll search the list. */
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
)
508 /* The method wasn't found. Follow the link to the next list of
510 method_list
= method_list
->method_next
;
516 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
518 /* Forwarding pointers/integers through the normal registers */
520 __objc_word_forward (id rcv
, SEL op
, ...)
524 args
= __builtin_apply_args ();
525 res
= __objc_forward (rcv
, op
, args
);
527 __builtin_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 */
538 __objc_double_forward (id rcv
, SEL op
, ...)
542 args
= __builtin_apply_args ();
543 res
= __objc_forward (rcv
, op
, args
);
544 __builtin_return (res
);
547 #if INVISIBLE_STRUCT_RETURN
552 __objc_block_forward (id rcv
, SEL op
, ...)
556 args
= __builtin_apply_args ();
557 res
= __objc_forward (rcv
, op
, args
);
559 __builtin_return (res
);
561 #if INVISIBLE_STRUCT_RETURN
562 return (__big
) {{0, 0, 0, 0, 0, 0, 0, 0}};
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. */
572 __objc_forward (id object
, SEL sel
, arglist_t args
)
575 static SEL frwd_sel
= 0; /* !T:SAFE2 */
578 /* first try if the object understands forward:: */
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
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
)
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
);
625 __objc_print_dtable_stats()
629 objc_mutex_lock(__objc_runtime_mutex
);
632 printf("memory usage: (%s)\n", "2-level sparse arrays");
634 printf("memory usage: (%s)\n", "3-level sparse arrays");
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. */
658 objc_get_uninstalled_dtable()
660 return __objc_uninstalled_dtable
;