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. */
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
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 if (strcmp (sel_get_name (op
), "initialize"))
248 __objc_send_initialize((Class
)receiver
);
250 CLS_SETINITIALIZED((Class
)receiver
);
252 objc_mutex_unlock(__objc_runtime_mutex
);
255 /* Install dummy table for class which causes the first message to
256 that class (or instances hereof) to be initialized properly */
258 __objc_install_premature_dtable(Class
class)
260 assert(__objc_uninstalled_dtable
);
261 class->dtable
= __objc_uninstalled_dtable
;
264 /* Send +initialize to class if not already done */
266 __objc_send_initialize(Class
class)
268 /* This *must* be a class object */
269 assert(CLS_ISCLASS(class));
270 assert(!CLS_ISMETA(class));
272 if (!CLS_ISINITIALIZED(class))
274 CLS_SETINITIALIZED(class);
275 CLS_SETINITIALIZED(class->class_pointer
);
277 if(class->super_class
)
278 __objc_send_initialize(class->super_class
);
281 SEL op
= sel_register_name ("initialize");
282 Class tmpclass
= class;
285 while (!imp
&& tmpclass
) {
286 MethodList_t method_list
= tmpclass
->class_pointer
->methods
;
288 while(!imp
&& method_list
) {
292 for (i
=0;i
<method_list
->method_count
;i
++) {
293 method
= &(method_list
->method_list
[i
]);
294 if (method
->method_name
295 && method
->method_name
->sel_id
== op
->sel_id
) {
296 imp
= method
->method_imp
;
301 method_list
= method_list
->method_next
;
305 tmpclass
= tmpclass
->super_class
;
308 (*imp
)((id
)class, op
);
314 /* Walk on the methods list of class and install the methods in the reverse
315 order of the lists. Since methods added by categories are before the methods
316 of class in the methods list, this allows categories to substitute methods
317 declared in class. However if more than one category replaces the same
318 method nothing is guaranteed about what method will be used.
319 Assumes that __objc_runtime_mutex is locked down. */
321 __objc_install_methods_in_dtable (Class
class, MethodList_t method_list
)
328 if (method_list
->method_next
)
329 __objc_install_methods_in_dtable (class, method_list
->method_next
);
331 for (i
= 0; i
< method_list
->method_count
; i
++)
333 Method_t method
= &(method_list
->method_list
[i
]);
334 sarray_at_put_safe (class->dtable
,
335 (sidx
) method
->method_name
->sel_id
,
340 /* Assumes that __objc_runtime_mutex is locked down. */
342 __objc_install_dispatch_table_for_class (Class
class)
346 /* If the class has not yet had its class links resolved, we must
347 re-compute all class links */
348 if(!CLS_ISRESOLV(class))
349 __objc_resolve_class_links();
351 super
= class->super_class
;
353 if (super
!= 0 && (super
->dtable
== __objc_uninstalled_dtable
))
354 __objc_install_dispatch_table_for_class (super
);
356 /* Allocate dtable if necessary */
359 objc_mutex_lock(__objc_runtime_mutex
);
360 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
361 objc_mutex_unlock(__objc_runtime_mutex
);
364 class->dtable
= sarray_lazy_copy (super
->dtable
);
366 __objc_install_methods_in_dtable (class, class->methods
);
370 __objc_update_dispatch_table_for_class (Class
class)
375 /* not yet installed -- skip it */
376 if (class->dtable
== __objc_uninstalled_dtable
)
379 objc_mutex_lock(__objc_runtime_mutex
);
382 __objc_install_premature_dtable (class); /* someone might require it... */
383 sarray_free (arr
); /* release memory */
385 /* could have been lazy... */
386 __objc_install_dispatch_table_for_class (class);
388 if (class->subclass_list
) /* Traverse subclasses */
389 for (next
= class->subclass_list
; next
; next
= next
->sibling_class
)
390 __objc_update_dispatch_table_for_class (next
);
392 objc_mutex_unlock(__objc_runtime_mutex
);
396 /* This function adds a method list to a class. This function is
397 typically called by another function specific to the run-time. As
398 such this function does not worry about thread safe issues.
400 This one is only called for categories. Class objects have their
401 methods installed right away, and their selectors are made into
402 SEL's by the function __objc_register_selectors_from_class. */
404 class_add_method_list (Class
class, MethodList_t list
)
408 /* Passing of a linked list is not allowed. Do multiple calls. */
409 assert (!list
->method_next
);
411 /* Check for duplicates. */
412 for (i
= 0; i
< list
->method_count
; ++i
)
414 Method_t method
= &list
->method_list
[i
];
416 if (method
->method_name
) /* Sometimes these are NULL */
418 /* This is where selector names are transmogrified to SEL's */
419 method
->method_name
=
420 sel_register_typed_name ((const char*)method
->method_name
,
421 method
->method_types
);
425 /* Add the methods to the class's method list. */
426 list
->method_next
= class->methods
;
427 class->methods
= list
;
429 /* Update the dispatch table of class */
430 __objc_update_dispatch_table_for_class (class);
434 class_get_instance_method(Class
class, SEL op
)
436 return search_for_method_in_hierarchy(class, op
);
440 class_get_class_method(MetaClass
class, SEL op
)
442 return search_for_method_in_hierarchy(class, op
);
446 /* Search for a method starting from the current class up its hierarchy.
447 Return a pointer to the method's method structure if found. NULL
451 search_for_method_in_hierarchy (Class cls
, SEL sel
)
453 Method_t method
= NULL
;
456 if (! sel_is_mapped (sel
))
459 /* Scan the method list of the class. If the method isn't found in the
460 list then step to its super class. */
461 for (class = cls
; ((! method
) && class); class = class->super_class
)
462 method
= search_for_method_in_list (class->methods
, sel
);
469 /* Given a linked list of method and a method's name. Search for the named
470 method's method structure. Return a pointer to the method's method
471 structure if found. NULL otherwise. */
473 search_for_method_in_list (MethodList_t list
, SEL op
)
475 MethodList_t method_list
= list
;
477 if (! sel_is_mapped (op
))
480 /* If not found then we'll search the list. */
485 /* Search the method list. */
486 for (i
= 0; i
< method_list
->method_count
; ++i
)
488 Method_t method
= &method_list
->method_list
[i
];
490 if (method
->method_name
)
491 if (method
->method_name
->sel_id
== op
->sel_id
)
495 /* The method wasn't found. Follow the link to the next list of
497 method_list
= method_list
->method_next
;
503 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
505 /* Forwarding pointers/integers through the normal registers */
507 __objc_word_forward (id rcv
, SEL op
, ...)
511 args
= __builtin_apply_args ();
512 res
= __objc_forward (rcv
, op
, args
);
514 __builtin_return (res
);
519 /* Specific routine for forwarding floats/double because of
520 architectural differences on some processors. i386s for
521 example which uses a floating point stack versus general
522 registers for floating point numbers. This forward routine
523 makes sure that GCC restores the proper return values */
525 __objc_double_forward (id rcv
, SEL op
, ...)
529 args
= __builtin_apply_args ();
530 res
= __objc_forward (rcv
, op
, args
);
531 __builtin_return (res
);
534 #if INVISIBLE_STRUCT_RETURN
539 __objc_block_forward (id rcv
, SEL op
, ...)
543 args
= __builtin_apply_args ();
544 res
= __objc_forward (rcv
, op
, args
);
546 __builtin_return (res
);
548 #if INVISIBLE_STRUCT_RETURN
549 return (__big
) {{0, 0, 0, 0, 0, 0, 0, 0}};
556 /* This function is installed in the dispatch table for all methods which are
557 not implemented. Thus, it is called when a selector is not recognized. */
559 __objc_forward (id object
, SEL sel
, arglist_t args
)
562 static SEL frwd_sel
= 0; /* !T:SAFE2 */
565 /* first try if the object understands forward:: */
567 frwd_sel
= sel_get_any_uid("forward::");
569 if (__objc_responds_to (object
, frwd_sel
))
571 imp
= get_imp(object
->class_pointer
, frwd_sel
);
572 return (*imp
)(object
, frwd_sel
, sel
, args
);
575 /* If the object recognizes the doesNotRecognize: method then we're going
577 err_sel
= sel_get_any_uid ("doesNotRecognize:");
578 if (__objc_responds_to (object
, err_sel
))
580 imp
= get_imp (object
->class_pointer
, err_sel
);
581 return (*imp
) (object
, err_sel
, sel
);
584 /* The object doesn't recognize the method. Check for responding to
585 error:. If it does then sent it. */
587 size_t strlen (const char*);
588 char msg
[256 + strlen ((const char*)sel_get_name (sel
))
589 + strlen ((const char*)object
->class_pointer
->name
)];
591 sprintf (msg
, "(%s) %s does not recognize %s",
592 (CLS_ISMETA(object
->class_pointer
)
595 object
->class_pointer
->name
, sel_get_name (sel
));
597 err_sel
= sel_get_any_uid ("error:");
598 if (__objc_responds_to (object
, err_sel
))
600 imp
= get_imp (object
->class_pointer
, err_sel
);
601 return (*imp
) (object
, sel_get_any_uid ("error:"), msg
);
604 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
605 a default action is taken. */
606 objc_error (object
, OBJC_ERR_UNIMPLEMENTED
, "%s\n", msg
);
613 __objc_print_dtable_stats()
617 objc_mutex_lock(__objc_runtime_mutex
);
619 printf("memory usage: (%s)\n",
621 "2-level sparse arrays"
623 "3-level sparse arrays"
627 printf("arrays: %d = %ld bytes\n", narrays
,
628 (long)narrays
*sizeof(struct sarray
));
629 total
+= narrays
*sizeof(struct sarray
);
630 printf("buckets: %d = %ld bytes\n", nbuckets
,
631 (long)nbuckets
*sizeof(struct sbucket
));
632 total
+= nbuckets
*sizeof(struct sbucket
);
634 printf("idxtables: %d = %ld bytes\n", idxsize
, (long)idxsize
*sizeof(void*));
635 total
+= idxsize
*sizeof(void*);
636 printf("-----------------------------------\n");
637 printf("total: %d bytes\n", total
);
638 printf("===================================\n");
640 objc_mutex_unlock(__objc_runtime_mutex
);
643 /* Returns the uninstalled dispatch table indicator.
644 If a class' dispatch table points to __objc_uninstalled_dtable
645 then that means it needs its dispatch table to be installed. */
648 objc_get_uninstalled_dtable()
650 return __objc_uninstalled_dtable
;