1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995, 1996 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 /* This one is a bit hairy. This function is installed in the
170 premature dispatch table, and thus called once for each class,
171 namely when the very first message is send to it. */
173 static void __objc_init_install_dtable(id receiver
, SEL op
)
175 __label__ already_initialized
;
180 /* This may happen, if the programmer has taken the address of a
181 method before the dtable was initialized... too bad for him! */
182 if(receiver
->class_pointer
->dtable
!= __objc_uninstalled_dtable
)
183 goto already_initialized
;
185 objc_mutex_lock(__objc_runtime_mutex
);
187 if(CLS_ISCLASS(receiver
->class_pointer
))
189 /* receiver is an ordinary object */
190 assert(CLS_ISCLASS(receiver
->class_pointer
));
192 /* install instance methods table */
193 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
195 /* call +initialize -- this will in turn install the factory
196 dispatch table if not already done :-) */
197 __objc_send_initialize(receiver
->class_pointer
);
201 /* receiver is a class object */
202 assert(CLS_ISCLASS((Class
)receiver
));
203 assert(CLS_ISMETA(receiver
->class_pointer
));
205 /* Install real dtable for factory methods */
206 __objc_install_dispatch_table_for_class (receiver
->class_pointer
);
208 if (strcmp (sel_get_name (op
), "initialize"))
209 __objc_send_initialize((Class
)receiver
);
211 CLS_SETINITIALIZED((Class
)receiver
);
213 objc_mutex_unlock(__objc_runtime_mutex
);
217 /* Get real method for this in newly installed dtable */
218 imp
= get_imp(receiver
->class_pointer
, op
);
220 args
= __builtin_apply_args();
221 result
= __builtin_apply((apply_t
)imp
, args
, 96);
223 __builtin_return (result
);
229 /* Install dummy table for class which causes the first message to
230 that class (or instances hereof) to be initialized properly */
231 void __objc_install_premature_dtable(Class
class)
233 assert(__objc_uninstalled_dtable
);
234 class->dtable
= __objc_uninstalled_dtable
;
237 /* Send +initialize to class if not already done */
238 static void __objc_send_initialize(Class
class)
240 /* This *must* be a class object */
241 assert(CLS_ISCLASS(class));
242 assert(!CLS_ISMETA(class));
244 if (!CLS_ISINITIALIZED(class))
246 CLS_SETINITIALIZED(class);
247 CLS_SETINITIALIZED(class->class_pointer
);
249 if(class->super_class
)
250 __objc_send_initialize(class->super_class
);
253 SEL op
= sel_register_name ("initialize");
254 Class tmpclass
= class;
257 while (!imp
&& tmpclass
) {
258 MethodList_t method_list
= tmpclass
->class_pointer
->methods
;
260 while(!imp
&& method_list
) {
264 for (i
=0;i
<method_list
->method_count
;i
++) {
265 method
= &(method_list
->method_list
[i
]);
266 if (method
->method_name
->sel_id
== op
->sel_id
) {
267 imp
= method
->method_imp
;
272 method_list
= method_list
->method_next
;
276 tmpclass
= tmpclass
->super_class
;
279 (*imp
)((id
)class, op
);
285 /* Assumes that __objc_runtime_mutex is locked down. */
287 __objc_install_dispatch_table_for_class (Class
class)
293 /* If the class has not yet had it's class links resolved, we must
294 re-compute all class links */
295 if(!CLS_ISRESOLV(class))
296 __objc_resolve_class_links();
298 super
= class->super_class
;
300 if (super
!= 0 && (super
->dtable
== __objc_uninstalled_dtable
))
301 __objc_install_dispatch_table_for_class (super
);
303 /* Allocate dtable if necessary */
306 objc_mutex_lock(__objc_runtime_mutex
);
307 class->dtable
= sarray_new (__objc_selector_max_index
, 0);
308 objc_mutex_unlock(__objc_runtime_mutex
);
311 class->dtable
= sarray_lazy_copy (super
->dtable
);
313 for (mlist
= class->methods
; mlist
; mlist
= mlist
->method_next
)
315 counter
= mlist
->method_count
- 1;
318 Method_t method
= &(mlist
->method_list
[counter
]);
319 sarray_at_put_safe (class->dtable
,
320 (sidx
) method
->method_name
->sel_id
,
327 void __objc_update_dispatch_table_for_class (Class
class)
332 /* not yet installed -- skip it */
333 if (class->dtable
== __objc_uninstalled_dtable
)
336 objc_mutex_lock(__objc_runtime_mutex
);
339 __objc_install_premature_dtable (class); /* someone might require it... */
340 sarray_free (arr
); /* release memory */
342 /* could have been lazy... */
343 __objc_install_dispatch_table_for_class (class);
345 if (class->subclass_list
) /* Traverse subclasses */
346 for (next
= class->subclass_list
; next
; next
= next
->sibling_class
)
347 __objc_update_dispatch_table_for_class (next
);
349 objc_mutex_unlock(__objc_runtime_mutex
);
353 /* This function adds a method list to a class. This function is
354 typically called by another function specific to the run-time. As
355 such this function does not worry about thread safe issues.
357 This one is only called for categories. Class objects have their
358 methods installed right away, and their selectors are made into
359 SEL's by the function __objc_register_selectors_from_class. */
361 class_add_method_list (Class
class, MethodList_t list
)
364 static SEL initialize_sel
= 0; /* !T:SAFE2 */
367 initialize_sel
= sel_register_name ("initialize");
369 /* Passing of a linked list is not allowed. Do multiple calls. */
370 assert (!list
->method_next
);
372 /* Check for duplicates. */
373 for (i
= 0; i
< list
->method_count
; ++i
)
375 Method_t method
= &list
->method_list
[i
];
377 if (method
->method_name
) /* Sometimes these are NULL */
379 /* This is where selector names are transmogrified to SEL's */
380 method
->method_name
=
381 sel_register_typed_name ((const char*)method
->method_name
,
382 method
->method_types
);
384 if (search_for_method_in_list (class->methods
, method
->method_name
)
385 && method
->method_name
->sel_id
!= initialize_sel
->sel_id
)
387 /* Duplication. Print a error message an change the method name
389 fprintf (stderr
, "attempt to add a existing method: %s\n",
390 sel_get_name(method
->method_name
));
391 method
->method_name
= 0;
396 /* Add the methods to the class's method list. */
397 list
->method_next
= class->methods
;
398 class->methods
= list
;
403 class_get_instance_method(Class
class, SEL op
)
405 return search_for_method_in_hierarchy(class, op
);
409 class_get_class_method(MetaClass
class, SEL op
)
411 return search_for_method_in_hierarchy(class, op
);
415 /* Search for a method starting from the current class up its hierarchy.
416 Return a pointer to the method's method structure if found. NULL
420 search_for_method_in_hierarchy (Class cls
, SEL sel
)
422 Method_t method
= NULL
;
425 if (! sel_is_mapped (sel
))
428 /* Scan the method list of the class. If the method isn't found in the
429 list then step to its super class. */
430 for (class = cls
; ((! method
) && class); class = class->super_class
)
431 method
= search_for_method_in_list (class->methods
, sel
);
438 /* Given a linked list of method and a method's name. Search for the named
439 method's method structure. Return a pointer to the method's method
440 structure if found. NULL otherwise. */
442 search_for_method_in_list (MethodList_t list
, SEL op
)
444 MethodList_t method_list
= list
;
446 if (! sel_is_mapped (op
))
449 /* If not found then we'll search the list. */
454 /* Search the method list. */
455 for (i
= 0; i
< method_list
->method_count
; ++i
)
457 Method_t method
= &method_list
->method_list
[i
];
459 if (method
->method_name
)
460 if (method
->method_name
->sel_id
== op
->sel_id
)
464 /* The method wasn't found. Follow the link to the next list of
466 method_list
= method_list
->method_next
;
472 static retval_t
__objc_forward (id object
, SEL sel
, arglist_t args
);
474 /* Forwarding pointers/integers through the normal registers */
476 __objc_word_forward (id rcv
, SEL op
, ...)
480 args
= __builtin_apply_args ();
481 res
= __objc_forward (rcv
, op
, args
);
483 __builtin_return (res
);
488 /* Specific routine for forwarding floats/double because of
489 architectural differences on some processors. i386s for
490 example which uses a floating point stack versus general
491 registers for floating point numbers. This forward routine
492 makes sure that GCC restores the proper return values */
494 __objc_double_forward (id rcv
, SEL op
, ...)
498 args
= __builtin_apply_args ();
499 res
= __objc_forward (rcv
, op
, args
);
500 __builtin_return (res
);
503 #if INVISIBLE_STRUCT_RETURN
508 __objc_block_forward (id rcv
, SEL op
, ...)
512 args
= __builtin_apply_args ();
513 res
= __objc_forward (rcv
, op
, args
);
515 __builtin_return (res
);
519 /* This function is installed in the dispatch table for all methods which are
520 not implemented. Thus, it is called when a selector is not recognized. */
522 __objc_forward (id object
, SEL sel
, arglist_t args
)
525 static SEL frwd_sel
= 0; /* !T:SAFE2 */
528 /* first try if the object understands forward:: */
530 frwd_sel
= sel_get_any_uid("forward::");
532 if (__objc_responds_to (object
, frwd_sel
))
534 imp
= get_imp(object
->class_pointer
, frwd_sel
);
535 return (*imp
)(object
, frwd_sel
, sel
, args
);
538 /* If the object recognizes the doesNotRecognize: method then we're going
540 err_sel
= sel_get_any_uid ("doesNotRecognize:");
541 if (__objc_responds_to (object
, err_sel
))
543 imp
= get_imp (object
->class_pointer
, err_sel
);
544 return (*imp
) (object
, err_sel
, sel
);
547 /* The object doesn't recognize the method. Check for responding to
548 error:. If it does then sent it. */
550 size_t strlen (const char*);
551 char msg
[256 + strlen ((const char*)sel_get_name (sel
))
552 + strlen ((const char*)object
->class_pointer
->name
)];
554 sprintf (msg
, "(%s) %s does not recognize %s",
555 (CLS_ISMETA(object
->class_pointer
)
558 object
->class_pointer
->name
, sel_get_name (sel
));
560 err_sel
= sel_get_any_uid ("error:");
561 if (__objc_responds_to (object
, err_sel
))
563 imp
= get_imp (object
->class_pointer
, err_sel
);
564 return (*imp
) (object
, sel_get_any_uid ("error:"), msg
);
567 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
568 a default action is taken. */
569 fprintf (stderr
, "fatal: %s\n", msg
);
574 void __objc_print_dtable_stats()
578 objc_mutex_lock(__objc_runtime_mutex
);
580 printf("memory usage: (%s)\n",
582 "2-level sparse arrays"
584 "3-level sparse arrays"
588 printf("arrays: %d = %ld bytes\n", narrays
,
589 (int)narrays
*sizeof(struct sarray
));
590 total
+= narrays
*sizeof(struct sarray
);
591 printf("buckets: %d = %ld bytes\n", nbuckets
,
592 (int)nbuckets
*sizeof(struct sbucket
));
593 total
+= nbuckets
*sizeof(struct sbucket
);
595 printf("idxtables: %d = %ld bytes\n", idxsize
, (int)idxsize
*sizeof(void*));
596 total
+= idxsize
*sizeof(void*);
597 printf("-----------------------------------\n");
598 printf("total: %d bytes\n", total
);
599 printf("===================================\n");
601 objc_mutex_unlock(__objc_runtime_mutex
);
604 /* Returns the dispatch table */
607 objc_get_uninstalled_dtable()
609 return __objc_uninstalled_dtable
;