sendmsg.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:19k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

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