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