1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993 Free Software Foundation, Inc.
4 Author: 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, 675 Mass Ave, Cambridge, MA 02139, 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;
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);
52 static id __objc_word_forward(id, SEL, ...);
53 typedef struct { id many[8]; } __big;
54 #if INVISIBLE_STRUCT_RETURN
59 __objc_block_forward(id, SEL, ...);
60 static Method_t search_for_method_in_hierarchy (Class* class, SEL sel);
61 static Method_t search_for_method_in_list(MethodList_t list, SEL op);
62 id nil_method(id, SEL, ...);
65 nil_method(id receiver, SEL op, ...)
70 /* Given a class and selector, return the selector's implementation. */
72 get_imp (Class* class, SEL sel)
75 void* res = sarray_get (class->dtable, (size_t) sel->sel_id);
76 if(res == __objc_init_install_dtable)
78 __objc_install_dispatch_table_for_class (class);
79 res = sarray_get (class->dtable, (size_t) sel->sel_id);
83 const char *t = sel->sel_types;
84 if (t && (*t == '[' || *t == '(' || *t == '{'))
85 res = (IMP)__objc_block_forward;
87 res = (IMP)__objc_word_forward;
93 __objc_responds_to (id object, SEL sel)
95 void* res = sarray_get (object->class_pointer->dtable, (size_t) sel->sel_id);
96 if(res == __objc_init_install_dtable)
98 __objc_install_dispatch_table_for_class (object->class_pointer);
99 res = sarray_get (object->class_pointer->dtable, (size_t) sel->sel_id);
104 /* This is the lookup function. All entries in the table are either a
105 valid method *or* one of `__objc_missing_method' which calls
106 forward:: etc, or `__objc_init_install_dtable' which installs the
109 objc_msg_lookup(id receiver, SEL op)
114 result = sarray_get(receiver->class_pointer->dtable, (sidx)op->sel_id);
117 const char *t = op->sel_types;
118 if (t && (*t == '[' || *t == '(' || *t == '{'))
119 result = (IMP)__objc_block_forward;
121 result = (IMP)__objc_word_forward;
130 objc_msg_lookup_super (Super_t super, SEL sel)
133 return get_imp (super->class, sel);
138 int method_get_sizeof_arguments (Method*);
141 objc_msg_sendv(id object, SEL op, arglist_t arg_frame)
143 Method* m = class_get_instance_method(object->class_pointer, op);
145 *((id*)method_get_first_argument (m, arg_frame, &type)) = object;
146 *((SEL*)method_get_next_argument (arg_frame, &type)) = op;
147 return __builtin_apply((apply_t)m->method_imp,
149 method_get_sizeof_arguments (m));
152 void __objc_init_dispatch_tables()
154 __objc_uninstalled_dtable
155 = sarray_new(200, __objc_init_install_dtable);
158 /* This one is a bit hairy. This function is installed in the
159 premature dispatch table, and thus called once for each class,
160 namely when the very first message is send to it. */
162 static void __objc_init_install_dtable(id receiver, SEL op)
164 __label__ allready_initialized;
169 /* This may happen, if the programmer has taken the address of a
170 method before the dtable was initialized... too bad for him! */
171 if(receiver->class_pointer->dtable != __objc_uninstalled_dtable)
172 goto allready_initialized;
174 if(CLS_ISCLASS(receiver->class_pointer))
176 /* receiver is an ordinary object */
177 assert(CLS_ISCLASS(receiver->class_pointer));
179 /* install instance methods table */
180 __objc_install_dispatch_table_for_class (receiver->class_pointer);
182 /* call +initialize -- this will in turn install the factory
183 dispatch table if not already done :-) */
184 __objc_send_initialize(receiver->class_pointer);
188 /* receiver is a class object */
189 assert(CLS_ISCLASS((Class*)receiver));
190 assert(CLS_ISMETA(receiver->class_pointer));
192 /* Install real dtable for factory methods */
193 __objc_install_dispatch_table_for_class (receiver->class_pointer);
195 if (strcmp (sel_get_name (op), "initialize"))
196 __objc_send_initialize((Class*)receiver);
198 CLS_SETINITIALIZED((Class*)receiver);
201 allready_initialized:
203 /* Get real method for this in newly installed dtable */
204 imp = get_imp(receiver->class_pointer, op);
206 args = __builtin_apply_args();
207 result = __builtin_apply((apply_t)imp, args, 96);
209 __builtin_return (result);
215 /* Install dummy table for class which causes the first message to
216 that class (or instances hereof) to be initialized properly */
217 void __objc_install_premature_dtable(Class* class)
219 assert(__objc_uninstalled_dtable);
220 class->dtable = __objc_uninstalled_dtable;
223 /* Send +initialize to class if not already done */
224 static void __objc_send_initialize(Class* class)
226 /* This *must* be a class object */
227 assert(CLS_ISCLASS(class));
228 assert(!CLS_ISMETA(class));
230 if (!CLS_ISINITIALIZED(class))
232 CLS_SETINITIALIZED(class);
233 CLS_SETINITIALIZED(class->class_pointer);
235 if(class->super_class)
236 __objc_send_initialize(class->super_class);
239 MethodList_t method_list = class->class_pointer->methods;
240 SEL op = sel_register_name ("initialize");
242 /* If not found then we'll search the list. */
247 /* Search the method list. */
248 for (i = 0; i < method_list->method_count; ++i)
250 Method_t method = &method_list->method_list[i];
253 if (method->method_name->sel_id == op->sel_id)
254 (*method->method_imp)((id) class, op);
257 /* The method wasn't found. Follow the link to the next list of
259 method_list = method_list->method_next;
266 __objc_install_dispatch_table_for_class (Class* class)
272 /* If the class has not yet had it's class links resolved, we must
273 re-compute all class links */
274 if(!CLS_ISRESOLV(class))
275 __objc_resolve_class_links();
277 super = class->super_class;
279 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
280 __objc_install_dispatch_table_for_class (super);
282 /* Allocate dtable if nessecary */
285 class->dtable = sarray_new (__objc_selector_max_index, 0);
288 class->dtable = sarray_lazy_copy (super->dtable);
290 for (mlist = class->methods; mlist; mlist = mlist->method_next)
292 counter = mlist->method_count - 1;
295 Method_t method = &(mlist->method_list[counter]);
296 sarray_at_put_safe (class->dtable,
297 (sidx) method->method_name->sel_id,
304 void __objc_update_dispatch_table_for_class (Class* class)
308 /* not yet installed -- skip it */
309 if (class->dtable == __objc_uninstalled_dtable)
312 sarray_free (class->dtable); /* release memory */
313 __objc_install_premature_dtable (class); /* someone might require it... */
314 __objc_install_dispatch_table_for_class (class); /* could have been lazy... */
316 if (class->subclass_list) /* Traverse subclasses */
317 for (next = class->subclass_list; next; next = next->sibling_class)
318 __objc_update_dispatch_table_for_class (next);
323 /* This function adds a method list to a class. This function is
324 typically called by another function specific to the run-time. As
325 such this function does not worry about thread safe issued.
327 This one is only called for categories. Class objects have their
328 methods installed rightaway, and their selectors are made into
329 SEL's by the function __objc_register_selectors_from_class. */
331 class_add_method_list (Class* class, MethodList_t list)
334 static SEL initialize_sel = 0;
336 initialize_sel = sel_register_name ("initialize");
338 /* Passing of a linked list is not allowed. Do multiple calls. */
339 assert (!list->method_next);
341 /* Check for duplicates. */
342 for (i = 0; i < list->method_count; ++i)
344 Method_t method = &list->method_list[i];
346 if (method->method_name) /* Sometimes these are NULL */
348 /* This is where selector names are transmogriffed to SEL's */
349 method->method_name =
350 sel_register_typed_name ((const char*)method->method_name,
351 method->method_types);
353 if (search_for_method_in_list (class->methods, method->method_name)
354 && method->method_name->sel_id != initialize_sel->sel_id)
356 /* Duplication. Print a error message an change the method name
358 fprintf (stderr, "attempt to add a existing method: %s\n",
359 sel_get_name(method->method_name));
360 method->method_name = 0;
365 /* Add the methods to the class's method list. */
366 list->method_next = class->methods;
367 class->methods = list;
372 class_get_instance_method(Class* class, SEL op)
374 return search_for_method_in_hierarchy(class, op);
378 class_get_class_method(MetaClass* class, SEL op)
380 return search_for_method_in_hierarchy(class, op);
384 /* Search for a method starting from the current class up its hierarchy.
385 Return a pointer to the method's method structure if found. NULL
389 search_for_method_in_hierarchy (Class* cls, SEL sel)
391 Method_t method = NULL;
394 if (! sel_is_mapped (sel))
397 /* Scan the method list of the class. If the method isn't found in the
398 list then step to its super class. */
399 for (class = cls; ((! method) && class); class = class->super_class)
400 method = search_for_method_in_list (class->methods, sel);
407 /* Given a linked list of method and a method's name. Search for the named
408 method's method structure. Return a pointer to the method's method
409 structure if found. NULL otherwise. */
411 search_for_method_in_list (MethodList_t list, SEL op)
413 MethodList_t method_list = list;
415 if (! sel_is_mapped (op))
418 /* If not found then we'll search the list. */
423 /* Search the method list. */
424 for (i = 0; i < method_list->method_count; ++i)
426 Method_t method = &method_list->method_list[i];
428 if (method->method_name)
429 if (method->method_name->sel_id == op->sel_id)
433 /* The method wasn't found. Follow the link to the next list of
435 method_list = method_list->method_next;
441 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
444 __objc_word_forward (id rcv, SEL op, ...)
448 args = __builtin_apply_args ();
449 res = __objc_forward (rcv, op, args);
451 __builtin_return (res);
456 #if INVISIBLE_STRUCT_RETURN
461 __objc_block_forward (id rcv, SEL op, ...)
465 args = __builtin_apply_args ();
466 res = __objc_forward (rcv, op, args);
468 __builtin_return (res);
472 /* This fuction is installed in the dispatch table for all methods which are
473 not implemented. Thus, it is called when a selector is not recognized. */
475 __objc_forward (id object, SEL sel, arglist_t args)
478 static SEL frwd_sel = 0;
481 /* first try if the object understands forward:: */
483 frwd_sel = sel_get_any_uid("forward::");
485 if (__objc_responds_to (object, frwd_sel))
487 imp = get_imp(object->class_pointer, frwd_sel);
488 return (*imp)(object, frwd_sel, sel, args);
491 /* If the object recognizes the doesNotRecognize: method then we're going
493 err_sel = sel_get_any_uid ("doesNotRecognize:");
494 if (__objc_responds_to (object, err_sel))
496 imp = get_imp (object->class_pointer, err_sel);
497 return (*imp) (object, err_sel, sel);
500 /* The object doesn't recognize the method. Check for responding to
501 error:. If it does then sent it. */
503 size_t strlen (const char*);
504 char msg[256 + strlen ((const char*)sel_get_name (sel))
505 + strlen ((const char*)object->class_pointer->name)];
507 sprintf (msg, "(%s) %s does not recognize %s",
508 (CLS_ISMETA(object->class_pointer)
511 object->class_pointer->name, sel_get_name (sel));
513 err_sel = sel_get_any_uid ("error:");
514 if (__objc_responds_to (object, err_sel))
516 imp = get_imp (object->class_pointer, err_sel);
517 return (*imp) (object, sel_get_any_uid ("error:"), msg);
520 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
521 a default action is taken. */
522 fprintf (stderr, "fatal: %s\n", msg);
527 void __objc_print_dtable_stats()
530 printf("memory usage: (%s)\n",
532 "2-level sparse arrays"
534 "3-level sparse arrays"
538 printf("arrays: %d = %ld bytes\n", narrays, (int)narrays*sizeof(struct sarray));
539 total += narrays*sizeof(struct sarray);
540 printf("buckets: %d = %ld bytes\n", nbuckets, (int)nbuckets*sizeof(struct sbucket));
541 total += nbuckets*sizeof(struct sbucket);
543 printf("idxtables: %d = %ld bytes\n", idxsize, (int)idxsize*sizeof(void*));
544 total += idxsize*sizeof(void*);
545 printf("-----------------------------------\n");
546 printf("total: %d bytes\n", total);
547 printf("===================================\n");