re PR c++/6716 (loop and fill ram during compiling)
[gcc.git] / libobjc / sendmsg.c
1 /* GNU Objective C Runtime message lookup
2 Copyright (C) 1993, 1995, 1996, 1997, 1998,
3 2001 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
5
6 This file is part of GNU CC.
7
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.
11
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
15 details.
16
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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 /* As a special exception, if you link this library with files compiled with
23 GCC to produce an executable, this does not cause the resulting executable
24 to be covered by the GNU General Public License. This exception does not
25 however invalidate any other reasons why the executable file might be
26 covered by the GNU General Public License. */
27
28 #include "tconfig.h"
29 #include "runtime.h"
30 #include "sarray.h"
31 #include "encoding.h"
32 #include "runtime-info.h"
33
34 /* this is how we hack STRUCT_VALUE to be 1 or 0 */
35 #define gen_rtx(args...) 1
36 #define gen_rtx_MEM(args...) 1
37 #define gen_rtx_REG(args...) 1
38 #define rtx int
39
40 #if !defined(STRUCT_VALUE) || STRUCT_VALUE == 0
41 #define INVISIBLE_STRUCT_RETURN 1
42 #else
43 #define INVISIBLE_STRUCT_RETURN 0
44 #endif
45
46 /* The uninstalled dispatch table */
47 struct sarray* __objc_uninstalled_dtable = 0; /* !T:MUTEX */
48
49 /* Hook for method forwarding. If it is set, is invoked to return a
50 function that performs the real forwarding. Otherwise the libgcc
51 based functions (__builtin_apply and friends) are used. */
52 IMP (*__objc_msg_forward)(SEL) = NULL;
53
54 /* Send +initialize to class */
55 static void __objc_send_initialize(Class);
56
57 static void __objc_install_dispatch_table_for_class (Class);
58
59 /* Forward declare some functions */
60 static void __objc_init_install_dtable(id, SEL);
61
62 /* Various forwarding functions that are used based upon the
63 return type for the selector.
64 __objc_block_forward for structures.
65 __objc_double_forward for floats/doubles.
66 __objc_word_forward for pointers or types that fit in registers.
67 */
68 static double __objc_double_forward(id, SEL, ...);
69 static id __objc_word_forward(id, SEL, ...);
70 typedef struct { id many[8]; } __big;
71 #if INVISIBLE_STRUCT_RETURN
72 static __big
73 #else
74 static id
75 #endif
76 __objc_block_forward(id, SEL, ...);
77 static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
78 Method_t search_for_method_in_list(MethodList_t list, SEL op);
79 id nil_method(id, SEL, ...);
80
81 /* Given a selector, return the proper forwarding implementation. */
82 __inline__
83 IMP
84 __objc_get_forward_imp (SEL sel)
85 {
86 /* If a custom forwarding hook was registered, try getting a forwarding
87 * function from it. */
88 if (__objc_msg_forward)
89 {
90 IMP result;
91 if ((result = __objc_msg_forward (sel)) != NULL)
92 return result;
93 }
94
95 /* In all other cases, use the default forwarding functions built using
96 * __builtin_apply and friends. */
97 {
98 const char *t = sel->sel_types;
99
100 if (t && (*t == '[' || *t == '(' || *t == '{')
101 #ifdef OBJC_MAX_STRUCT_BY_VALUE
102 && objc_sizeof_type(t) > OBJC_MAX_STRUCT_BY_VALUE
103 #endif
104 )
105 return (IMP)__objc_block_forward;
106 else if (t && (*t == 'f' || *t == 'd'))
107 return (IMP)__objc_double_forward;
108 else
109 return (IMP)__objc_word_forward;
110 }
111 }
112
113 /* Given a class and selector, return the selector's implementation. */
114 __inline__
115 IMP
116 get_imp (Class class, SEL sel)
117 {
118 void* res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
119 if (res == 0)
120 {
121 /* Not a valid method */
122 if(class->dtable == __objc_uninstalled_dtable)
123 {
124 /* The dispatch table needs to be installed. */
125 objc_mutex_lock(__objc_runtime_mutex);
126 __objc_install_dispatch_table_for_class (class);
127 objc_mutex_unlock(__objc_runtime_mutex);
128 /* Call ourselves with the installed dispatch table
129 and get the real method */
130 res = get_imp(class, sel);
131 }
132 else
133 {
134 /* The dispatch table has been installed so the
135 method just doesn't exist for the class.
136 Return the forwarding implementation. */
137 res = __objc_get_forward_imp(sel);
138 }
139 }
140 return res;
141 }
142
143 /* Query if an object can respond to a selector, returns YES if the
144 object implements the selector otherwise NO. Does not check if the
145 method can be forwarded. */
146 __inline__
147 BOOL
148 __objc_responds_to (id object, SEL sel)
149 {
150 void* res;
151
152 /* Install dispatch table if need be */
153 if (object->class_pointer->dtable == __objc_uninstalled_dtable)
154 {
155 objc_mutex_lock(__objc_runtime_mutex);
156 __objc_install_dispatch_table_for_class (object->class_pointer);
157 objc_mutex_unlock(__objc_runtime_mutex);
158 }
159
160 /* Get the method from the dispatch table */
161 res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
162 return (res != 0);
163 }
164
165 /* This is the lookup function. All entries in the table are either a
166 valid method *or* zero. If zero then either the dispatch table
167 needs to be installed or it doesn't exist and forwarding is attempted. */
168 __inline__
169 IMP
170 objc_msg_lookup(id receiver, SEL op)
171 {
172 IMP result;
173 if(receiver)
174 {
175 result = sarray_get_safe (receiver->class_pointer->dtable,
176 (sidx)op->sel_id);
177 if (result == 0)
178 {
179 /* Not a valid method */
180 if(receiver->class_pointer->dtable == __objc_uninstalled_dtable)
181 {
182 /* The dispatch table needs to be installed.
183 This happens on the very first method call to the class. */
184 __objc_init_install_dtable(receiver, op);
185
186 /* Get real method for this in newly installed dtable */
187 result = get_imp(receiver->class_pointer, op);
188 }
189 else
190 {
191 /* The dispatch table has been installed so the
192 method just doesn't exist for the class.
193 Attempt to forward the method. */
194 result = __objc_get_forward_imp(op);
195 }
196 }
197 return result;
198 }
199 else
200 return nil_method;
201 }
202
203 IMP
204 objc_msg_lookup_super (Super_t super, SEL sel)
205 {
206 if (super->self)
207 return get_imp (super->class, sel);
208 else
209 return nil_method;
210 }
211
212 int method_get_sizeof_arguments (Method*);
213
214 retval_t
215 objc_msg_sendv(id object, SEL op, arglist_t arg_frame)
216 {
217 Method* m = class_get_instance_method(object->class_pointer, op);
218 const char *type;
219 *((id*)method_get_first_argument (m, arg_frame, &type)) = object;
220 *((SEL*)method_get_next_argument (arg_frame, &type)) = op;
221 return __builtin_apply((apply_t)m->method_imp,
222 arg_frame,
223 method_get_sizeof_arguments (m));
224 }
225
226 void
227 __objc_init_dispatch_tables()
228 {
229 __objc_uninstalled_dtable
230 = sarray_new(200, 0);
231 }
232
233 /* This function is called by objc_msg_lookup when the
234 dispatch table needs to be installed; thus it is called once
235 for each class, namely when the very first message is sent to it. */
236 static void
237 __objc_init_install_dtable(id receiver, SEL op __attribute__ ((__unused__)))
238 {
239 /* This may happen, if the programmer has taken the address of a
240 method before the dtable was initialized... too bad for him! */
241 if(receiver->class_pointer->dtable != __objc_uninstalled_dtable)
242 return;
243
244 objc_mutex_lock(__objc_runtime_mutex);
245
246 if(CLS_ISCLASS(receiver->class_pointer))
247 {
248 /* receiver is an ordinary object */
249 assert(CLS_ISCLASS(receiver->class_pointer));
250
251 /* install instance methods table */
252 __objc_install_dispatch_table_for_class (receiver->class_pointer);
253
254 /* call +initialize -- this will in turn install the factory
255 dispatch table if not already done :-) */
256 __objc_send_initialize(receiver->class_pointer);
257 }
258 else
259 {
260 /* receiver is a class object */
261 assert(CLS_ISCLASS((Class)receiver));
262 assert(CLS_ISMETA(receiver->class_pointer));
263
264 /* Install real dtable for factory methods */
265 __objc_install_dispatch_table_for_class (receiver->class_pointer);
266
267 __objc_send_initialize((Class)receiver);
268 }
269 objc_mutex_unlock(__objc_runtime_mutex);
270 }
271
272 /* Install dummy table for class which causes the first message to
273 that class (or instances hereof) to be initialized properly */
274 void
275 __objc_install_premature_dtable(Class class)
276 {
277 assert(__objc_uninstalled_dtable);
278 class->dtable = __objc_uninstalled_dtable;
279 }
280
281 /* Send +initialize to class if not already done */
282 static void
283 __objc_send_initialize(Class class)
284 {
285 /* This *must* be a class object */
286 assert(CLS_ISCLASS(class));
287 assert(!CLS_ISMETA(class));
288
289 if (!CLS_ISINITIALIZED(class))
290 {
291 CLS_SETINITIALIZED(class);
292 CLS_SETINITIALIZED(class->class_pointer);
293
294 /* Create the garbage collector type memory description */
295 __objc_generate_gc_type_description (class);
296
297 if(class->super_class)
298 __objc_send_initialize(class->super_class);
299
300 {
301 SEL op = sel_register_name ("initialize");
302 IMP imp = 0;
303 MethodList_t method_list = class->class_pointer->methods;
304
305 while (method_list) {
306 int i;
307 Method_t method;
308
309 for (i = 0; i< method_list->method_count; i++) {
310 method = &(method_list->method_list[i]);
311 if (method->method_name
312 && method->method_name->sel_id == op->sel_id) {
313 imp = method->method_imp;
314 break;
315 }
316 }
317
318 if (imp)
319 break;
320
321 method_list = method_list->method_next;
322
323 }
324 if (imp)
325 (*imp)((id)class, op);
326
327 }
328 }
329 }
330
331 /* Walk on the methods list of class and install the methods in the reverse
332 order of the lists. Since methods added by categories are before the methods
333 of class in the methods list, this allows categories to substitute methods
334 declared in class. However if more than one category replaces the same
335 method nothing is guaranteed about what method will be used.
336 Assumes that __objc_runtime_mutex is locked down. */
337 static void
338 __objc_install_methods_in_dtable (Class class, MethodList_t method_list)
339 {
340 int i;
341
342 if (!method_list)
343 return;
344
345 if (method_list->method_next)
346 __objc_install_methods_in_dtable (class, method_list->method_next);
347
348 for (i = 0; i < method_list->method_count; i++)
349 {
350 Method_t method = &(method_list->method_list[i]);
351 sarray_at_put_safe (class->dtable,
352 (sidx) method->method_name->sel_id,
353 method->method_imp);
354 }
355 }
356
357 /* Assumes that __objc_runtime_mutex is locked down. */
358 static void
359 __objc_install_dispatch_table_for_class (Class class)
360 {
361 Class super;
362
363 /* If the class has not yet had its class links resolved, we must
364 re-compute all class links */
365 if(!CLS_ISRESOLV(class))
366 __objc_resolve_class_links();
367
368 super = class->super_class;
369
370 if (super != 0 && (super->dtable == __objc_uninstalled_dtable))
371 __objc_install_dispatch_table_for_class (super);
372
373 /* Allocate dtable if necessary */
374 if (super == 0)
375 {
376 objc_mutex_lock(__objc_runtime_mutex);
377 class->dtable = sarray_new (__objc_selector_max_index, 0);
378 objc_mutex_unlock(__objc_runtime_mutex);
379 }
380 else
381 class->dtable = sarray_lazy_copy (super->dtable);
382
383 __objc_install_methods_in_dtable (class, class->methods);
384 }
385
386 void
387 __objc_update_dispatch_table_for_class (Class class)
388 {
389 Class next;
390 struct sarray *arr;
391
392 /* not yet installed -- skip it */
393 if (class->dtable == __objc_uninstalled_dtable)
394 return;
395
396 objc_mutex_lock(__objc_runtime_mutex);
397
398 arr = class->dtable;
399 __objc_install_premature_dtable (class); /* someone might require it... */
400 sarray_free (arr); /* release memory */
401
402 /* could have been lazy... */
403 __objc_install_dispatch_table_for_class (class);
404
405 if (class->subclass_list) /* Traverse subclasses */
406 for (next = class->subclass_list; next; next = next->sibling_class)
407 __objc_update_dispatch_table_for_class (next);
408
409 objc_mutex_unlock(__objc_runtime_mutex);
410 }
411
412
413 /* This function adds a method list to a class. This function is
414 typically called by another function specific to the run-time. As
415 such this function does not worry about thread safe issues.
416
417 This one is only called for categories. Class objects have their
418 methods installed right away, and their selectors are made into
419 SEL's by the function __objc_register_selectors_from_class. */
420 void
421 class_add_method_list (Class class, MethodList_t list)
422 {
423 int i;
424
425 /* Passing of a linked list is not allowed. Do multiple calls. */
426 assert (!list->method_next);
427
428 /* Check for duplicates. */
429 for (i = 0; i < list->method_count; ++i)
430 {
431 Method_t method = &list->method_list[i];
432
433 if (method->method_name) /* Sometimes these are NULL */
434 {
435 /* This is where selector names are transmogrified to SEL's */
436 method->method_name =
437 sel_register_typed_name ((const char*)method->method_name,
438 method->method_types);
439 }
440 }
441
442 /* Add the methods to the class's method list. */
443 list->method_next = class->methods;
444 class->methods = list;
445
446 /* Update the dispatch table of class */
447 __objc_update_dispatch_table_for_class (class);
448 }
449
450 Method_t
451 class_get_instance_method(Class class, SEL op)
452 {
453 return search_for_method_in_hierarchy(class, op);
454 }
455
456 Method_t
457 class_get_class_method(MetaClass class, SEL op)
458 {
459 return search_for_method_in_hierarchy(class, op);
460 }
461
462
463 /* Search for a method starting from the current class up its hierarchy.
464 Return a pointer to the method's method structure if found. NULL
465 otherwise. */
466
467 static Method_t
468 search_for_method_in_hierarchy (Class cls, SEL sel)
469 {
470 Method_t method = NULL;
471 Class class;
472
473 if (! sel_is_mapped (sel))
474 return NULL;
475
476 /* Scan the method list of the class. If the method isn't found in the
477 list then step to its super class. */
478 for (class = cls; ((! method) && class); class = class->super_class)
479 method = search_for_method_in_list (class->methods, sel);
480
481 return method;
482 }
483
484
485
486 /* Given a linked list of method and a method's name. Search for the named
487 method's method structure. Return a pointer to the method's method
488 structure if found. NULL otherwise. */
489 Method_t
490 search_for_method_in_list (MethodList_t list, SEL op)
491 {
492 MethodList_t method_list = list;
493
494 if (! sel_is_mapped (op))
495 return NULL;
496
497 /* If not found then we'll search the list. */
498 while (method_list)
499 {
500 int i;
501
502 /* Search the method list. */
503 for (i = 0; i < method_list->method_count; ++i)
504 {
505 Method_t method = &method_list->method_list[i];
506
507 if (method->method_name)
508 if (method->method_name->sel_id == op->sel_id)
509 return method;
510 }
511
512 /* The method wasn't found. Follow the link to the next list of
513 methods. */
514 method_list = method_list->method_next;
515 }
516
517 return NULL;
518 }
519
520 static retval_t __objc_forward (id object, SEL sel, arglist_t args);
521
522 /* Forwarding pointers/integers through the normal registers */
523 static id
524 __objc_word_forward (id rcv, SEL op, ...)
525 {
526 void *args, *res;
527
528 args = __builtin_apply_args ();
529 res = __objc_forward (rcv, op, args);
530 if (res)
531 __builtin_return (res);
532 else
533 return res;
534 }
535
536 /* Specific routine for forwarding floats/double because of
537 architectural differences on some processors. i386s for
538 example which uses a floating point stack versus general
539 registers for floating point numbers. This forward routine
540 makes sure that GCC restores the proper return values */
541 static double
542 __objc_double_forward (id rcv, SEL op, ...)
543 {
544 void *args, *res;
545
546 args = __builtin_apply_args ();
547 res = __objc_forward (rcv, op, args);
548 __builtin_return (res);
549 }
550
551 #if INVISIBLE_STRUCT_RETURN
552 static __big
553 #else
554 static id
555 #endif
556 __objc_block_forward (id rcv, SEL op, ...)
557 {
558 void *args, *res;
559
560 args = __builtin_apply_args ();
561 res = __objc_forward (rcv, op, args);
562 if (res)
563 __builtin_return (res);
564 else
565 #if INVISIBLE_STRUCT_RETURN
566 return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
567 #else
568 return nil;
569 #endif
570 }
571
572
573 /* This function is installed in the dispatch table for all methods which are
574 not implemented. Thus, it is called when a selector is not recognized. */
575 static retval_t
576 __objc_forward (id object, SEL sel, arglist_t args)
577 {
578 IMP imp;
579 static SEL frwd_sel = 0; /* !T:SAFE2 */
580 SEL err_sel;
581
582 /* first try if the object understands forward:: */
583 if (!frwd_sel)
584 frwd_sel = sel_get_any_uid("forward::");
585
586 if (__objc_responds_to (object, frwd_sel))
587 {
588 imp = get_imp(object->class_pointer, frwd_sel);
589 return (*imp)(object, frwd_sel, sel, args);
590 }
591
592 /* If the object recognizes the doesNotRecognize: method then we're going
593 to send it. */
594 err_sel = sel_get_any_uid ("doesNotRecognize:");
595 if (__objc_responds_to (object, err_sel))
596 {
597 imp = get_imp (object->class_pointer, err_sel);
598 return (*imp) (object, err_sel, sel);
599 }
600
601 /* The object doesn't recognize the method. Check for responding to
602 error:. If it does then sent it. */
603 {
604 char msg[256 + strlen ((const char*)sel_get_name (sel))
605 + strlen ((const char*)object->class_pointer->name)];
606
607 sprintf (msg, "(%s) %s does not recognize %s",
608 (CLS_ISMETA(object->class_pointer)
609 ? "class"
610 : "instance" ),
611 object->class_pointer->name, sel_get_name (sel));
612
613 err_sel = sel_get_any_uid ("error:");
614 if (__objc_responds_to (object, err_sel))
615 {
616 imp = get_imp (object->class_pointer, err_sel);
617 return (*imp) (object, sel_get_any_uid ("error:"), msg);
618 }
619
620 /* The object doesn't respond to doesNotRecognize: or error:; Therefore,
621 a default action is taken. */
622 objc_error (object, OBJC_ERR_UNIMPLEMENTED, "%s\n", msg);
623
624 return 0;
625 }
626 }
627
628 void
629 __objc_print_dtable_stats()
630 {
631 int total = 0;
632
633 objc_mutex_lock(__objc_runtime_mutex);
634
635 #ifdef OBJC_SPARSE2
636 printf("memory usage: (%s)\n", "2-level sparse arrays");
637 #else
638 printf("memory usage: (%s)\n", "3-level sparse arrays");
639 #endif
640
641 printf("arrays: %d = %ld bytes\n", narrays,
642 (long)narrays*sizeof(struct sarray));
643 total += narrays*sizeof(struct sarray);
644 printf("buckets: %d = %ld bytes\n", nbuckets,
645 (long)nbuckets*sizeof(struct sbucket));
646 total += nbuckets*sizeof(struct sbucket);
647
648 printf("idxtables: %d = %ld bytes\n", idxsize, (long)idxsize*sizeof(void*));
649 total += idxsize*sizeof(void*);
650 printf("-----------------------------------\n");
651 printf("total: %d bytes\n", total);
652 printf("===================================\n");
653
654 objc_mutex_unlock(__objc_runtime_mutex);
655 }
656
657 /* Returns the uninstalled dispatch table indicator.
658 If a class' dispatch table points to __objc_uninstalled_dtable
659 then that means it needs its dispatch table to be installed. */
660 __inline__
661 struct sarray*
662 objc_get_uninstalled_dtable()
663 {
664 return __objc_uninstalled_dtable;
665 }