(*zeroextract[qs]i_compare0_scratch): Use const_int_operand
[official-gcc.git] / gcc / objc / sendmsg.c
blob79d4a6b5d5b57453b74cf3c7d335f900d29de862
1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995 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
14 details.
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"
28 #include "runtime.h"
29 #include "sarray.h"
30 #include "encoding.h"
32 /* this is how we hack STRUCT_VALUE to be 1 or 0 */
33 #define gen_rtx(args...) 1
34 #define rtx int
36 #if STRUCT_VALUE == 0
37 #define INVISIBLE_STRUCT_RETURN 1
38 #else
39 #define INVISIBLE_STRUCT_RETURN 0
40 #endif
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
55 static __big
56 #else
57 static id
58 #endif
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, ...)
67 return receiver;
70 /* Given a class and selector, return the selector's implementation. */
71 __inline__
72 IMP
73 get_imp (Class class, SEL sel)
75 IMP impl;
76 void* res = sarray_get (class->dtable, (size_t) sel->sel_id);
77 if(res == __objc_init_install_dtable)
79 __objc_install_dispatch_table_for_class (class);
80 res = sarray_get (class->dtable, (size_t) sel->sel_id);
82 if (res == 0)
84 const char *t = sel->sel_types;
85 if (t && (*t == '[' || *t == '(' || *t == '{'))
86 res = (IMP)__objc_block_forward;
87 else
88 res = (IMP)__objc_word_forward;
90 return res;
93 __inline__ BOOL
94 __objc_responds_to (id object, SEL sel)
96 void* res = sarray_get (object->class_pointer->dtable, (size_t) sel->sel_id);
97 if(res == __objc_init_install_dtable)
99 __objc_install_dispatch_table_for_class (object->class_pointer);
100 res = sarray_get (object->class_pointer->dtable, (size_t) sel->sel_id);
102 return (res != 0);
105 /* This is the lookup function. All entries in the table are either a
106 valid method *or* one of `__objc_missing_method' which calls
107 forward:: etc, or `__objc_init_install_dtable' which installs the
108 real dtable */
109 __inline__ IMP
110 objc_msg_lookup(id receiver, SEL op)
112 IMP result;
113 if(receiver)
115 result = sarray_get(receiver->class_pointer->dtable, (sidx)op->sel_id);
116 if (result == 0)
118 const char *t = op->sel_types;
119 if (t && (*t == '[' || *t == '(' || *t == '{'))
120 result = (IMP)__objc_block_forward;
121 else
122 result = (IMP)__objc_word_forward;
124 return result;
126 else
127 return nil_method;
131 objc_msg_lookup_super (Super_t super, SEL sel)
133 if (super->self)
134 return get_imp (super->class, sel);
135 else
136 return nil_method;
139 int method_get_sizeof_arguments (Method*);
141 retval_t
142 objc_msg_sendv(id object, SEL op, arglist_t arg_frame)
144 Method* m = class_get_instance_method(object->class_pointer, op);
145 const char *type;
146 *((id*)method_get_first_argument (m, arg_frame, &type)) = object;
147 *((SEL*)method_get_next_argument (arg_frame, &type)) = op;
148 return __builtin_apply((apply_t)m->method_imp,
149 arg_frame,
150 method_get_sizeof_arguments (m));
153 void __objc_init_dispatch_tables()
155 __objc_uninstalled_dtable
156 = sarray_new(200, __objc_init_install_dtable);
159 /* This one is a bit hairy. This function is installed in the
160 premature dispatch table, and thus called once for each class,
161 namely when the very first message is send to it. */
163 static void __objc_init_install_dtable(id receiver, SEL op)
165 __label__ already_initialized;
166 IMP imp;
167 void* args;
168 void* result;
170 /* This may happen, if the programmer has taken the address of a
171 method before the dtable was initialized... too bad for him! */
172 if(receiver->class_pointer->dtable != __objc_uninstalled_dtable)
173 goto already_initialized;
175 if(CLS_ISCLASS(receiver->class_pointer))
177 /* receiver is an ordinary object */
178 assert(CLS_ISCLASS(receiver->class_pointer));
180 /* install instance methods table */
181 __objc_install_dispatch_table_for_class (receiver->class_pointer);
183 /* call +initialize -- this will in turn install the factory
184 dispatch table if not already done :-) */
185 __objc_send_initialize(receiver->class_pointer);
187 else
189 /* receiver is a class object */
190 assert(CLS_ISCLASS((Class)receiver));
191 assert(CLS_ISMETA(receiver->class_pointer));
193 /* Install real dtable for factory methods */
194 __objc_install_dispatch_table_for_class (receiver->class_pointer);
196 if (strcmp (sel_get_name (op), "initialize"))
197 __objc_send_initialize((Class)receiver);
198 else
199 CLS_SETINITIALIZED((Class)receiver);
202 already_initialized:
204 /* Get real method for this in newly installed dtable */
205 imp = get_imp(receiver->class_pointer, op);
207 args = __builtin_apply_args();
208 result = __builtin_apply((apply_t)imp, args, 96);
209 if (result)
210 __builtin_return (result);
211 else
212 return;
216 /* Install dummy table for class which causes the first message to
217 that class (or instances hereof) to be initialized properly */
218 void __objc_install_premature_dtable(Class class)
220 assert(__objc_uninstalled_dtable);
221 class->dtable = __objc_uninstalled_dtable;
224 /* Send +initialize to class if not already done */
225 static void __objc_send_initialize(Class class)
227 /* This *must* be a class object */
228 assert(CLS_ISCLASS(class));
229 assert(!CLS_ISMETA(class));
231 if (!CLS_ISINITIALIZED(class))
233 CLS_SETINITIALIZED(class);
234 CLS_SETINITIALIZED(class->class_pointer);
236 if(class->super_class)
237 __objc_send_initialize(class->super_class);
240 SEL op = sel_register_name ("initialize");
241 Class tmpclass = class;
242 IMP imp = 0;
244 while (!imp && tmpclass) {
245 MethodList_t method_list = tmpclass->class_pointer->methods;
247 while(!imp && method_list) {
248 int i;
249 Method_t method;
251 for (i=0;i<method_list->method_count;i++) {
252 method = &(method_list->method_list[i]);
253 if (method->method_name->sel_id == op->sel_id) {
254 imp = method->method_imp;
255 break;
259 method_list = method_list->method_next;
263 tmpclass = tmpclass->super_class;
265 if (imp)
266 (*imp)((id)class, op);
272 static void
273 __objc_install_dispatch_table_for_class (Class class)
275 Class super;
276 MethodList_t mlist;
277 int counter;
279 /* If the class has not yet had it's class links resolved, we must
280 re-compute all class links */
281 if(!CLS_ISRESOLV(class))
282 __objc_resolve_class_links();
284 super = class->super_class;
286 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
287 __objc_install_dispatch_table_for_class (super);
289 /* Allocate dtable if necessary */
290 if (super == 0)
292 class->dtable = sarray_new (__objc_selector_max_index, 0);
294 else
295 class->dtable = sarray_lazy_copy (super->dtable);
297 for (mlist = class->methods; mlist; mlist = mlist->method_next)
299 counter = mlist->method_count - 1;
300 while (counter >= 0)
302 Method_t method = &(mlist->method_list[counter]);
303 sarray_at_put_safe (class->dtable,
304 (sidx) method->method_name->sel_id,
305 method->method_imp);
306 counter -= 1;
311 void __objc_update_dispatch_table_for_class (Class class)
313 Class next;
315 /* not yet installed -- skip it */
316 if (class->dtable == __objc_uninstalled_dtable)
317 return;
319 sarray_free (class->dtable); /* release memory */
320 __objc_install_premature_dtable (class); /* someone might require it... */
321 __objc_install_dispatch_table_for_class (class); /* could have been lazy... */
323 if (class->subclass_list) /* Traverse subclasses */
324 for (next = class->subclass_list; next; next = next->sibling_class)
325 __objc_update_dispatch_table_for_class (next);
330 /* This function adds a method list to a class. This function is
331 typically called by another function specific to the run-time. As
332 such this function does not worry about thread safe issued.
334 This one is only called for categories. Class objects have their
335 methods installed right away, and their selectors are made into
336 SEL's by the function __objc_register_selectors_from_class. */
337 void
338 class_add_method_list (Class class, MethodList_t list)
340 int i;
341 static SEL initialize_sel = 0;
342 if (!initialize_sel)
343 initialize_sel = sel_register_name ("initialize");
345 /* Passing of a linked list is not allowed. Do multiple calls. */
346 assert (!list->method_next);
348 /* Check for duplicates. */
349 for (i = 0; i < list->method_count; ++i)
351 Method_t method = &list->method_list[i];
353 if (method->method_name) /* Sometimes these are NULL */
355 /* This is where selector names are transmogrified to SEL's */
356 method->method_name =
357 sel_register_typed_name ((const char*)method->method_name,
358 method->method_types);
360 if (search_for_method_in_list (class->methods, method->method_name)
361 && method->method_name->sel_id != initialize_sel->sel_id)
363 /* Duplication. Print a error message an change the method name
364 to NULL. */
365 fprintf (stderr, "attempt to add a existing method: %s\n",
366 sel_get_name(method->method_name));
367 method->method_name = 0;
372 /* Add the methods to the class's method list. */
373 list->method_next = class->methods;
374 class->methods = list;
378 Method_t
379 class_get_instance_method(Class class, SEL op)
381 return search_for_method_in_hierarchy(class, op);
384 Method_t
385 class_get_class_method(MetaClass class, SEL op)
387 return search_for_method_in_hierarchy(class, op);
391 /* Search for a method starting from the current class up its hierarchy.
392 Return a pointer to the method's method structure if found. NULL
393 otherwise. */
395 static Method_t
396 search_for_method_in_hierarchy (Class cls, SEL sel)
398 Method_t method = NULL;
399 Class class;
401 if (! sel_is_mapped (sel))
402 return NULL;
404 /* Scan the method list of the class. If the method isn't found in the
405 list then step to its super class. */
406 for (class = cls; ((! method) && class); class = class->super_class)
407 method = search_for_method_in_list (class->methods, sel);
409 return method;
414 /* Given a linked list of method and a method's name. Search for the named
415 method's method structure. Return a pointer to the method's method
416 structure if found. NULL otherwise. */
417 static Method_t
418 search_for_method_in_list (MethodList_t list, SEL op)
420 MethodList_t method_list = list;
422 if (! sel_is_mapped (op))
423 return NULL;
425 /* If not found then we'll search the list. */
426 while (method_list)
428 int i;
430 /* Search the method list. */
431 for (i = 0; i < method_list->method_count; ++i)
433 Method_t method = &method_list->method_list[i];
435 if (method->method_name)
436 if (method->method_name->sel_id == op->sel_id)
437 return method;
440 /* The method wasn't found. Follow the link to the next list of
441 methods. */
442 method_list = method_list->method_next;
445 return NULL;
448 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
450 static id
451 __objc_word_forward (id rcv, SEL op, ...)
453 void *args, *res;
455 args = __builtin_apply_args ();
456 res = __objc_forward (rcv, op, args);
457 if (res)
458 __builtin_return (res);
459 else
460 return res;
463 #if INVISIBLE_STRUCT_RETURN
464 static __big
465 #else
466 static id
467 #endif
468 __objc_block_forward (id rcv, SEL op, ...)
470 void *args, *res;
472 args = __builtin_apply_args ();
473 res = __objc_forward (rcv, op, args);
474 if (res)
475 __builtin_return (res);
479 /* This function is installed in the dispatch table for all methods which are
480 not implemented. Thus, it is called when a selector is not recognized. */
481 static retval_t
482 __objc_forward (id object, SEL sel, arglist_t args)
484 IMP imp;
485 static SEL frwd_sel = 0;
486 SEL err_sel;
488 /* first try if the object understands forward:: */
489 if (!frwd_sel)
490 frwd_sel = sel_get_any_uid("forward::");
492 if (__objc_responds_to (object, frwd_sel))
494 imp = get_imp(object->class_pointer, frwd_sel);
495 return (*imp)(object, frwd_sel, sel, args);
498 /* If the object recognizes the doesNotRecognize: method then we're going
499 to send it. */
500 err_sel = sel_get_any_uid ("doesNotRecognize:");
501 if (__objc_responds_to (object, err_sel))
503 imp = get_imp (object->class_pointer, err_sel);
504 return (*imp) (object, err_sel, sel);
507 /* The object doesn't recognize the method. Check for responding to
508 error:. If it does then sent it. */
510 size_t strlen (const char*);
511 char msg[256 + strlen ((const char*)sel_get_name (sel))
512 + strlen ((const char*)object->class_pointer->name)];
514 sprintf (msg, "(%s) %s does not recognize %s",
515 (CLS_ISMETA(object->class_pointer)
516 ? "class"
517 : "instance" ),
518 object->class_pointer->name, sel_get_name (sel));
520 err_sel = sel_get_any_uid ("error:");
521 if (__objc_responds_to (object, err_sel))
523 imp = get_imp (object->class_pointer, err_sel);
524 return (*imp) (object, sel_get_any_uid ("error:"), msg);
527 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
528 a default action is taken. */
529 fprintf (stderr, "fatal: %s\n", msg);
530 abort ();
534 void __objc_print_dtable_stats()
536 int total = 0;
537 printf("memory usage: (%s)\n",
538 #ifdef OBJC_SPARSE2
539 "2-level sparse arrays"
540 #else
541 "3-level sparse arrays"
542 #endif
545 printf("arrays: %d = %ld bytes\n", narrays, (int)narrays*sizeof(struct sarray));
546 total += narrays*sizeof(struct sarray);
547 printf("buckets: %d = %ld bytes\n", nbuckets, (int)nbuckets*sizeof(struct sbucket));
548 total += nbuckets*sizeof(struct sbucket);
550 printf("idxtables: %d = %ld bytes\n", idxsize, (int)idxsize*sizeof(void*));
551 total += idxsize*sizeof(void*);
552 printf("-----------------------------------\n");
553 printf("total: %d bytes\n", total);
554 printf("===================================\n");