1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995, 1996, 1997, 1998 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. */
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 gen_rtx_MEM(args...) 1
38 #if !defined(STRUCT_VALUE) || STRUCT_VALUE == 0
39 #define INVISIBLE_STRUCT_RETURN 1
41 #define INVISIBLE_STRUCT_RETURN 0
44 /* The uninstalled dispatch table */
45 struct sarray
* __objc_uninstalled_dtable
= 0; /* !T:MUTEX */
47 /* Send +initialize to class */
48 static void __objc_send_initialize(Class
);
50 static void __objc_install_dispatch_table_for_class (Class
);
52 /* Forward declare some functions */
53 static void __objc_init_install_dtable(id
, SEL
);
55 /* Various forwarding functions that are used based upon the
56 return type for the selector.
57 __objc_block_forward for structures.
58 __objc_double_forward for floats/doubles.
59 __objc_word_forward for pointers or types that fit in registers.
61 static double __objc_double_forward(id
, SEL
, ...);
62 static id
__objc_word_forward(id
, SEL
, ...);
63 typedef struct { id many
[8]; } __big
;
64 #if INVISIBLE_STRUCT_RETURN
69 __objc_block_forward(id
, SEL
, ...);
70 static Method_t
search_for_method_in_hierarchy (Class
class, SEL sel
);
71 Method_t
search_for_method_in_list(MethodList_t list
, SEL op
);
72 id
nil_method(id
, SEL
, ...);
74 /* Given a selector, return the proper forwarding implementation. */
77 __objc_get_forward_imp (SEL sel
)
79 const char *t
= sel
->sel_types
;
81 if (t
&& (*t
== '[' || *t
== '(' || *t
== '{')
82 #ifdef OBJC_MAX_STRUCT_BY_VALUE
83 && objc_sizeof_type(t
) > OBJC_MAX_STRUCT_BY_VALUE
86 return (IMP
)__objc_block_forward
;
87 else if (t
&& (*t
== 'f' || *t
== 'd'))
88 return (IMP
)__objc_double_forward
;
90 return (IMP
)__objc_word_forward
;
93 /* Given a class and selector, return the selector's implementation. */
96 get_imp (Class
class, SEL sel
)
98 void* res
= sarray_get_safe (class->dtable
, (size_t) sel
->sel_id
);
101 /* Not a valid method */
102 if(class->dtable
== __objc_uninstalled_dtable
)
104 /* The dispatch table needs to be installed. */
105 objc_mutex_lock(__objc_runtime_mutex
);
106 __objc_install_dispatch_table_for_class (class);
107 objc_mutex_unlock(__objc_runtime_mutex
);
108 /* Call ourselves with the installed dispatch table
109 and get the real method */
110 res
= get_imp(class, sel
);
114 /* The dispatch table has been installed so the
115 method just doesn't exist for the class.
116 Return the forwarding implementation. */
117 res
= __objc_get_forward_imp(sel
);
123 /* Query if an object can respond to a selector, returns YES if the
124 object implements the selector otherwise NO. Does not check if the
125 method can be forwarded. */
128 __objc_responds_to (id object
, SEL sel
)
132 /* Install dispatch table if need be */
133 if (object
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
135 objc_mutex_lock(__objc_runtime_mutex
);
136 __objc_install_dispatch_table_for_class (object
->class_pointer
);
137 objc_mutex_unlock(__objc_runtime_mutex
);
140 /* Get the method from the dispatch table */
141 res
= sarray_get_safe (object
->class_pointer
->dtable
, (size_t) sel
->sel_id
);
145 /* This is the lookup function. All entries in the table are either a
146 valid method *or* zero. If zero then either the dispatch table
147 needs to be installed or it doesn't exist and forwarding is attempted. */
150 objc_msg_lookup(id receiver
, SEL op
)
155 result
= sarray_get_safe (receiver
->class_pointer
->dtable
,
159 /* Not a valid method */
160 if(receiver
->class_pointer
->dtable
== __objc_uninstalled_dtable
)
162 /* The dispatch table needs to be installed.
163 This happens on the very first method call to the class. */
164 __objc_init_install_dtable(receiver
, op
);
166 /* Get real method for this in newly installed dtable */
167 result
= get_imp(receiver
->class_pointer
, op
);
171 /* The dispatch table has been installed so the
172 method just doesn't exist for the class.
173 Attempt to forward the method. */
174 result
= __objc_get_forward_imp(op
);
184 objc_msg_lookup_super (Super_t super
, SEL sel
)
187 return get_imp (super
->class, sel
);
192 int method_get_sizeof_arguments (Method
*);
195 objc_msg_sendv(id object
, SEL op
, arglist_t arg_frame
)
197 Method
* m
= class_get_instance_method(object
->class_pointer
, op
);
199 *((id
*)method_get_first_argument (m
, arg_frame
, &type
)) = object
;
200 *((SEL
*)method_get_next_argument (arg_frame
, &type
)) = op
;
201 return __builtin_apply((apply_t
)m
->method_imp
,
203 method_get_sizeof_arguments (m
));
207 __objc_init_dispatch_tables()
209 __objc_uninstalled_dtable
210 = sarray_new(200, 0);
213 /* This function is called by objc_msg_lookup when the
214 dispatch table needs to be installed; thus it is called once
215 for each class, namely when the very first message is sent to it. */
217 __objc_init_install_dtable(id receiver
, SEL op
)
219 /* This may happen, if the programmer has taken the address of a
220 method before the dtable was initialized... too bad for him! */
221 if(receiver
->class_pointer
->dtable
!= __objc_uninstalled_dtable
)
224 objc_mutex_lock(__objc_runtime_mutex
);
226 if(CLS_ISCLASS(receiver
->class_pointer
))
228 /* receiver is an ordinary object */
229 assert(CLS_ISCLASS(receiver
->class_pointer
));
231 /* install instance methods table */
232 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
234 /* call +initialize -- this will in turn install the factory
235 dispatch table if not already done :-) */
236 __objc_send_initialize(receiver
->class_pointer
);
240 /* receiver is a class object */
241 assert(CLS_ISCLASS((Class
)receiver
));
242 assert(CLS_ISMETA(receiver
->class_pointer
));
244 /* Install real dtable for factory methods */
245 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
247 __objc_send_initialize((Class
)receiver
);
249 objc_mutex_unlock(__objc_runtime_mutex
);
252 /* Install dummy table for class which causes the first message to
253 that class (or instances hereof) to be initialized properly */
255 __objc_install_premature_dtable(Class
class)
257 assert(__objc_uninstalled_dtable
);
258 class->dtable
= __objc_uninstalled_dtable
;
261 /* Send +initialize to class if not already done */
263 __objc_send_initialize(Class
class)
265 /* This *must* be a class object */
266 assert(CLS_ISCLASS(class));
267 assert(!CLS_ISMETA(class));
269 if (!CLS_ISINITIALIZED(class))
271 CLS_SETINITIALIZED(class);
272 CLS_SETINITIALIZED(class->class_pointer
);
274 /* Create the garbage collector type memory description */
275 __objc_generate_gc_type_description (class);
277 if(class->super_class
)
278 __objc_send_initialize(class->super_class
);
281 SEL op
= sel_register_name ("initialize");
283 MethodList_t method_list
= class->class_pointer
->methods
;
285 while (method_list
) {
289 for (i
= 0; i
< method_list
->method_count
; i
++) {
290 method
= &(method_list
->method_list
[i
]);
291 if (method
->method_name
292 && method
->method_name
->sel_id
== op
->sel_id
) {
293 imp
= method
->method_imp
;
301 method_list
= method_list
->method_next
;
305 (*imp
)((id
)class, op
);
311 /* Walk on the methods list of class and install the methods in the reverse
312 order of the lists. Since methods added by categories are before the methods
313 of class in the methods list, this allows categories to substitute methods
314 declared in class. However if more than one category replaces the same
315 method nothing is guaranteed about what method will be used.
316 Assumes that __objc_runtime_mutex is locked down. */
318 __objc_install_methods_in_dtable (Class
class, MethodList_t method_list
)
325 if (method_list
->method_next
)
326 __objc_install_methods_in_dtable (class, method_list
->method_next
);
328 for (i
= 0; i
< method_list
->method_count
; i
++)
330 Method_t method
= &(method_list
->method_list
[i
]);
331 sarray_at_put_safe (class->dtable
,
332 (sidx
) method
->method_name
->sel_id
,
337 /* Assumes that __objc_runtime_mutex is locked down. */
339 __objc_install_dispatch_table_for_class (Class
class)
343 /* If the class has not yet had its class links resolved, we must
344 re-compute all class links */
345 if(!CLS_ISRESOLV(class))
346 __objc_resolve_class_links();
348 super
= class->super_class
;
350 if (super
!= 0 && (super
->dtable
== __objc_uninstalled_dtable
))
351 __objc_install_dispatch_table_for_class (super
);
353 /* Allocate dtable if necessary */
356 objc_mutex_lock(__objc_runtime_mutex
);
357 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
358 objc_mutex_unlock(__objc_runtime_mutex
);
361 class->dtable
= sarray_lazy_copy (super
->dtable
);
363 __objc_install_methods_in_dtable (class, class->methods
);
367 __objc_update_dispatch_table_for_class (Class
class)
372 /* not yet installed -- skip it */
373 if (class->dtable
== __objc_uninstalled_dtable
)
376 objc_mutex_lock(__objc_runtime_mutex
);
379 __objc_install_premature_dtable (class); /* someone might require it... */
380 sarray_free (arr
); /* release memory */
382 /* could have been lazy... */
383 __objc_install_dispatch_table_for_class (class);
385 if (class->subclass_list
) /* Traverse subclasses */
386 for (next
= class->subclass_list
; next
; next
= next
->sibling_class
)
387 __objc_update_dispatch_table_for_class (next
);
389 objc_mutex_unlock(__objc_runtime_mutex
);
393 /* This function adds a method list to a class. This function is
394 typically called by another function specific to the run-time. As
395 such this function does not worry about thread safe issues.
397 This one is only called for categories. Class objects have their
398 methods installed right away, and their selectors are made into
399 SEL's by the function __objc_register_selectors_from_class. */
401 class_add_method_list (Class
class, MethodList_t list
)
405 /* Passing of a linked list is not allowed. Do multiple calls. */
406 assert (!list
->method_next
);
408 /* Check for duplicates. */
409 for (i
= 0; i
< list
->method_count
; ++i
)
411 Method_t method
= &list
->method_list
[i
];
413 if (method
->method_name
) /* Sometimes these are NULL */
415 /* This is where selector names are transmogrified to SEL's */
416 method
->method_name
=
417 sel_register_typed_name ((const char*)method
->method_name
,
418 method
->method_types
);
422 /* Add the methods to the class's method list. */
423 list
->method_next
= class->methods
;
424 class->methods
= list
;
426 /* Update the dispatch table of class */
427 __objc_update_dispatch_table_for_class (class);
431 class_get_instance_method(Class
class, SEL op
)
433 return search_for_method_in_hierarchy(class, op
);
437 class_get_class_method(MetaClass
class, SEL op
)
439 return search_for_method_in_hierarchy(class, op
);
443 /* Search for a method starting from the current class up its hierarchy.
444 Return a pointer to the method's method structure if found. NULL
448 search_for_method_in_hierarchy (Class cls
, SEL sel
)
450 Method_t method
= NULL
;
453 if (! sel_is_mapped (sel
))
456 /* Scan the method list of the class. If the method isn't found in the
457 list then step to its super class. */
458 for (class = cls
; ((! method
) && class); class = class->super_class
)
459 method
= search_for_method_in_list (class->methods
, sel
);
466 /* Given a linked list of method and a method's name. Search for the named
467 method's method structure. Return a pointer to the method's method
468 structure if found. NULL otherwise. */
470 search_for_method_in_list (MethodList_t list
, SEL op
)
472 MethodList_t method_list
= list
;
474 if (! sel_is_mapped (op
))
477 /* If not found then we'll search the list. */
482 /* Search the method list. */
483 for (i
= 0; i
< method_list
->method_count
; ++i
)
485 Method_t method
= &method_list
->method_list
[i
];
487 if (method
->method_name
)
488 if (method
->method_name
->sel_id
== op
->sel_id
)
492 /* The method wasn't found. Follow the link to the next list of
494 method_list
= method_list
->method_next
;
500 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
502 /* Forwarding pointers/integers through the normal registers */
504 __objc_word_forward (id rcv
, SEL op
, ...)
508 args
= __builtin_apply_args ();
509 res
= __objc_forward (rcv
, op
, args
);
511 __builtin_return (res
);
516 /* Specific routine for forwarding floats/double because of
517 architectural differences on some processors. i386s for
518 example which uses a floating point stack versus general
519 registers for floating point numbers. This forward routine
520 makes sure that GCC restores the proper return values */
522 __objc_double_forward (id rcv
, SEL op
, ...)
526 args
= __builtin_apply_args ();
527 res
= __objc_forward (rcv
, op
, args
);
528 __builtin_return (res
);
531 #if INVISIBLE_STRUCT_RETURN
536 __objc_block_forward (id rcv
, SEL op
, ...)
540 args
= __builtin_apply_args ();
541 res
= __objc_forward (rcv
, op
, args
);
543 __builtin_return (res
);
545 #if INVISIBLE_STRUCT_RETURN
546 return (__big
) {{0, 0, 0, 0, 0, 0, 0, 0}};
553 /* This function is installed in the dispatch table for all methods which are
554 not implemented. Thus, it is called when a selector is not recognized. */
556 __objc_forward (id object
, SEL sel
, arglist_t args
)
559 static SEL frwd_sel
= 0; /* !T:SAFE2 */
562 /* first try if the object understands forward:: */
564 frwd_sel
= sel_get_any_uid("forward::");
566 if (__objc_responds_to (object
, frwd_sel
))
568 imp
= get_imp(object
->class_pointer
, frwd_sel
);
569 return (*imp
)(object
, frwd_sel
, sel
, args
);
572 /* If the object recognizes the doesNotRecognize: method then we're going
574 err_sel
= sel_get_any_uid ("doesNotRecognize:");
575 if (__objc_responds_to (object
, err_sel
))
577 imp
= get_imp (object
->class_pointer
, err_sel
);
578 return (*imp
) (object
, err_sel
, sel
);
581 /* The object doesn't recognize the method. Check for responding to
582 error:. If it does then sent it. */
584 size_t strlen (const char*);
585 char msg
[256 + strlen ((const char*)sel_get_name (sel
))
586 + strlen ((const char*)object
->class_pointer
->name
)];
588 sprintf (msg
, "(%s) %s does not recognize %s",
589 (CLS_ISMETA(object
->class_pointer
)
592 object
->class_pointer
->name
, sel_get_name (sel
));
594 err_sel
= sel_get_any_uid ("error:");
595 if (__objc_responds_to (object
, err_sel
))
597 imp
= get_imp (object
->class_pointer
, err_sel
);
598 return (*imp
) (object
, sel_get_any_uid ("error:"), msg
);
601 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
602 a default action is taken. */
603 objc_error (object
, OBJC_ERR_UNIMPLEMENTED
, "%s\n", msg
);
610 __objc_print_dtable_stats()
614 objc_mutex_lock(__objc_runtime_mutex
);
616 printf("memory usage: (%s)\n",
618 "2-level sparse arrays"
620 "3-level sparse arrays"
624 printf("arrays: %d = %ld bytes\n", narrays
,
625 (long)narrays
*sizeof(struct sarray
));
626 total
+= narrays
*sizeof(struct sarray
);
627 printf("buckets: %d = %ld bytes\n", nbuckets
,
628 (long)nbuckets
*sizeof(struct sbucket
));
629 total
+= nbuckets
*sizeof(struct sbucket
);
631 printf("idxtables: %d = %ld bytes\n", idxsize
, (long)idxsize
*sizeof(void*));
632 total
+= idxsize
*sizeof(void*);
633 printf("-----------------------------------\n");
634 printf("total: %d bytes\n", total
);
635 printf("===================================\n");
637 objc_mutex_unlock(__objc_runtime_mutex
);
640 /* Returns the uninstalled dispatch table indicator.
641 If a class' dispatch table points to __objc_uninstalled_dtable
642 then that means it needs its dispatch table to be installed. */
645 objc_get_uninstalled_dtable()
647 return __objc_uninstalled_dtable
;