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"
31 #include "runtime-info.h"
33 /* this is how we hack STRUCT_VALUE to be 1 or 0 */
34 #define gen_rtx(args...) 1
37 #if !defined(STRUCT_VALUE) || STRUCT_VALUE == 0
38 #define INVISIBLE_STRUCT_RETURN 1
40 #define INVISIBLE_STRUCT_RETURN 0
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
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. */
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
85 return (IMP
)__objc_block_forward
;
86 else if (t
&& (*t
== 'f' || *t
== 'd'))
87 return (IMP
)__objc_double_forward
;
89 return (IMP
)__objc_word_forward
;
92 /* Given a class and selector, return the selector's implementation. */
95 get_imp (Class
class, SEL sel
)
97 void* res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
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
);
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
);
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. */
127 __objc_responds_to (id object
, SEL sel
)
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
);
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. */
149 objc_msg_lookup(id receiver
, SEL op
)
154 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
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
);
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
);
183 objc_msg_lookup_super (Super_t super
, SEL sel
)
186 return get_imp (super
->class, sel
);
191 int method_get_sizeof_arguments (Method
*);
194 objc_msg_sendv(id object
, SEL op
, arglist_t arg_frame
)
196 Method
* m
= class_get_instance_method(object
->class_pointer
, op
);
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
,
202 method_get_sizeof_arguments (m
));
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. */
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
)
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
);
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
);
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 */
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 */
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;
284 while (!imp
&& tmpclass
) {
285 MethodList_t method_list
= tmpclass
->class_pointer
->methods
;
287 while(!imp
&& method_list
) {
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
;
300 method_list
= method_list
->method_next
;
304 tmpclass
= tmpclass
->super_class
;
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. */
320 __objc_install_methods_in_dtable (Class
class, MethodList_t method_list
)
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
,
339 /* Assumes that __objc_runtime_mutex is locked down. */
341 __objc_install_dispatch_table_for_class (Class
class)
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 */
358 objc_mutex_lock(__objc_runtime_mutex
);
359 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
360 objc_mutex_unlock(__objc_runtime_mutex
);
363 class->dtable
= sarray_lazy_copy (super
->dtable
);
365 __objc_install_methods_in_dtable (class, class->methods
);
369 __objc_update_dispatch_table_for_class (Class
class)
374 /* not yet installed -- skip it */
375 if (class->dtable
== __objc_uninstalled_dtable
)
378 objc_mutex_lock(__objc_runtime_mutex
);
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. */
403 class_add_method_list (Class
class, MethodList_t list
)
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);
433 class_get_instance_method(Class
class, SEL op
)
435 return search_for_method_in_hierarchy(class, op
);
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
450 search_for_method_in_hierarchy (Class cls
, SEL sel
)
452 Method_t method
= NULL
;
455 if (! sel_is_mapped (sel
))
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
);
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. */
472 search_for_method_in_list (MethodList_t list
, SEL op
)
474 MethodList_t method_list
= list
;
476 if (! sel_is_mapped (op
))
479 /* If not found then we'll search the list. */
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
)
494 /* The method wasn't found. Follow the link to the next list of
496 method_list
= method_list
->method_next
;
502 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
504 /* Forwarding pointers/integers through the normal registers */
506 __objc_word_forward (id rcv
, SEL op
, ...)
510 args
= __builtin_apply_args ();
511 res
= __objc_forward (rcv
, op
, args
);
513 __builtin_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 */
524 __objc_double_forward (id rcv
, SEL op
, ...)
528 args
= __builtin_apply_args ();
529 res
= __objc_forward (rcv
, op
, args
);
530 __builtin_return (res
);
533 #if INVISIBLE_STRUCT_RETURN
538 __objc_block_forward (id rcv
, SEL op
, ...)
542 args
= __builtin_apply_args ();
543 res
= __objc_forward (rcv
, op
, args
);
545 __builtin_return (res
);
547 #if INVISIBLE_STRUCT_RETURN
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. */
558 __objc_forward (id object
, SEL sel
, arglist_t args
)
561 static SEL frwd_sel
= 0; /* !T:SAFE2 */
564 /* first try if the object understands forward:: */
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
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
)
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
);
612 __objc_print_dtable_stats()
616 objc_mutex_lock(__objc_runtime_mutex
);
618 printf("memory usage: (%s)\n",
620 "2-level sparse arrays"
622 "3-level sparse arrays"
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. */
647 objc_get_uninstalled_dtable()
649 return __objc_uninstalled_dtable
;