Fix conflicts. This has a small part of rev 1.2 in it.
This commit is contained in:
parent
b93c24d42b
commit
0ecfea2ee8
@ -1,5 +1,5 @@
|
||||
/* GNU Objective C Runtime message lookup
|
||||
Copyright (C) 1993, 1995 Free Software Foundation, Inc.
|
||||
Copyright (C) 1993, 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
|
||||
Contributed by Kresten Krab Thorup
|
||||
|
||||
This file is part of GNU CC.
|
||||
@ -24,23 +24,27 @@ Boston, MA 02111-1307, USA. */
|
||||
however invalidate any other reasons why the executable file might be
|
||||
covered by the GNU General Public License. */
|
||||
|
||||
/* $FreeBSD$ */
|
||||
|
||||
#include "../tconfig.h"
|
||||
#include "runtime.h"
|
||||
#include "sarray.h"
|
||||
#include "encoding.h"
|
||||
#include "runtime-info.h"
|
||||
|
||||
/* this is how we hack STRUCT_VALUE to be 1 or 0 */
|
||||
#define gen_rtx(args...) 1
|
||||
#define gen_rtx_MEM(args...) 1
|
||||
#define rtx int
|
||||
|
||||
#if STRUCT_VALUE == 0
|
||||
#if !defined(STRUCT_VALUE) || STRUCT_VALUE == 0
|
||||
#define INVISIBLE_STRUCT_RETURN 1
|
||||
#else
|
||||
#define INVISIBLE_STRUCT_RETURN 0
|
||||
#endif
|
||||
|
||||
/* The uninstalled dispatch table */
|
||||
struct sarray* __objc_uninstalled_dtable = 0;
|
||||
struct sarray* __objc_uninstalled_dtable = 0; /* !T:MUTEX */
|
||||
|
||||
/* Send +initialize to class */
|
||||
static void __objc_send_initialize(Class);
|
||||
@ -49,6 +53,14 @@ static void __objc_install_dispatch_table_for_class (Class);
|
||||
|
||||
/* Forward declare some functions */
|
||||
static void __objc_init_install_dtable(id, SEL);
|
||||
|
||||
/* Various forwarding functions that are used based upon the
|
||||
return type for the selector.
|
||||
__objc_block_forward for structures.
|
||||
__objc_double_forward for floats/doubles.
|
||||
__objc_word_forward for pointers or types that fit in registers.
|
||||
*/
|
||||
static double __objc_double_forward(id, SEL, ...);
|
||||
static id __objc_word_forward(id, SEL, ...);
|
||||
typedef struct { id many[8]; } __big;
|
||||
#if INVISIBLE_STRUCT_RETURN
|
||||
@ -58,13 +70,26 @@ static id
|
||||
#endif
|
||||
__objc_block_forward(id, SEL, ...);
|
||||
static Method_t search_for_method_in_hierarchy (Class class, SEL sel);
|
||||
static Method_t search_for_method_in_list(MethodList_t list, SEL op);
|
||||
Method_t search_for_method_in_list(MethodList_t list, SEL op);
|
||||
id nil_method(id, SEL, ...);
|
||||
|
||||
id
|
||||
nil_method(id receiver, SEL op, ...)
|
||||
/* Given a selector, return the proper forwarding implementation. */
|
||||
__inline__
|
||||
IMP
|
||||
__objc_get_forward_imp (SEL sel)
|
||||
{
|
||||
return receiver;
|
||||
const char *t = sel->sel_types;
|
||||
|
||||
if (t && (*t == '[' || *t == '(' || *t == '{')
|
||||
#ifdef OBJC_MAX_STRUCT_BY_VALUE
|
||||
&& objc_sizeof_type(t) > OBJC_MAX_STRUCT_BY_VALUE
|
||||
#endif
|
||||
)
|
||||
return (IMP)__objc_block_forward;
|
||||
else if (t && (*t == 'f' || *t == 'd'))
|
||||
return (IMP)__objc_double_forward;
|
||||
else
|
||||
return (IMP)__objc_word_forward;
|
||||
}
|
||||
|
||||
/* Given a class and selector, return the selector's implementation. */
|
||||
@ -72,54 +97,84 @@ __inline__
|
||||
IMP
|
||||
get_imp (Class class, SEL sel)
|
||||
{
|
||||
IMP impl;
|
||||
void* res = sarray_get (class->dtable, (size_t) sel->sel_id);
|
||||
if(res == __objc_init_install_dtable)
|
||||
{
|
||||
__objc_install_dispatch_table_for_class (class);
|
||||
res = sarray_get (class->dtable, (size_t) sel->sel_id);
|
||||
}
|
||||
void* res = sarray_get_safe (class->dtable, (size_t) sel->sel_id);
|
||||
if (res == 0)
|
||||
{
|
||||
const char *t = sel->sel_types;
|
||||
if (t && (*t == '[' || *t == '(' || *t == '{'))
|
||||
res = (IMP)__objc_block_forward;
|
||||
/* Not a valid method */
|
||||
if(class->dtable == __objc_uninstalled_dtable)
|
||||
{
|
||||
/* The dispatch table needs to be installed. */
|
||||
objc_mutex_lock(__objc_runtime_mutex);
|
||||
__objc_install_dispatch_table_for_class (class);
|
||||
objc_mutex_unlock(__objc_runtime_mutex);
|
||||
/* Call ourselves with the installed dispatch table
|
||||
and get the real method */
|
||||
res = get_imp(class, sel);
|
||||
}
|
||||
else
|
||||
res = (IMP)__objc_word_forward;
|
||||
{
|
||||
/* The dispatch table has been installed so the
|
||||
method just doesn't exist for the class.
|
||||
Return the forwarding implementation. */
|
||||
res = __objc_get_forward_imp(sel);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
__inline__ BOOL
|
||||
/* Query if an object can respond to a selector, returns YES if the
|
||||
object implements the selector otherwise NO. Does not check if the
|
||||
method can be forwarded. */
|
||||
__inline__
|
||||
BOOL
|
||||
__objc_responds_to (id object, SEL sel)
|
||||
{
|
||||
void* res = sarray_get (object->class_pointer->dtable, (size_t) sel->sel_id);
|
||||
if(res == __objc_init_install_dtable)
|
||||
void* res;
|
||||
|
||||
/* Install dispatch table if need be */
|
||||
if (object->class_pointer->dtable == __objc_uninstalled_dtable)
|
||||
{
|
||||
objc_mutex_lock(__objc_runtime_mutex);
|
||||
__objc_install_dispatch_table_for_class (object->class_pointer);
|
||||
res = sarray_get (object->class_pointer->dtable, (size_t) sel->sel_id);
|
||||
objc_mutex_unlock(__objc_runtime_mutex);
|
||||
}
|
||||
|
||||
/* Get the method from the dispatch table */
|
||||
res = sarray_get_safe (object->class_pointer->dtable, (size_t) sel->sel_id);
|
||||
return (res != 0);
|
||||
}
|
||||
|
||||
/* This is the lookup function. All entries in the table are either a
|
||||
valid method *or* one of `__objc_missing_method' which calls
|
||||
forward:: etc, or `__objc_init_install_dtable' which installs the
|
||||
real dtable */
|
||||
__inline__ IMP
|
||||
valid method *or* zero. If zero then either the dispatch table
|
||||
needs to be installed or it doesn't exist and forwarding is attempted. */
|
||||
__inline__
|
||||
IMP
|
||||
objc_msg_lookup(id receiver, SEL op)
|
||||
{
|
||||
IMP result;
|
||||
if(receiver)
|
||||
{
|
||||
result = sarray_get(receiver->class_pointer->dtable, (sidx)op->sel_id);
|
||||
result = sarray_get_safe (receiver->class_pointer->dtable,
|
||||
(sidx)op->sel_id);
|
||||
if (result == 0)
|
||||
{
|
||||
const char *t = op->sel_types;
|
||||
if (t && (*t == '[' || *t == '(' || *t == '{'))
|
||||
result = (IMP)__objc_block_forward;
|
||||
/* Not a valid method */
|
||||
if(receiver->class_pointer->dtable == __objc_uninstalled_dtable)
|
||||
{
|
||||
/* The dispatch table needs to be installed.
|
||||
This happens on the very first method call to the class. */
|
||||
__objc_init_install_dtable(receiver, op);
|
||||
|
||||
/* Get real method for this in newly installed dtable */
|
||||
result = get_imp(receiver->class_pointer, op);
|
||||
}
|
||||
else
|
||||
result = (IMP)__objc_word_forward;
|
||||
{
|
||||
/* The dispatch table has been installed so the
|
||||
method just doesn't exist for the class.
|
||||
Attempt to forward the method. */
|
||||
result = __objc_get_forward_imp(op);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@ -150,27 +205,25 @@ objc_msg_sendv(id object, SEL op, arglist_t arg_frame)
|
||||
method_get_sizeof_arguments (m));
|
||||
}
|
||||
|
||||
void __objc_init_dispatch_tables()
|
||||
void
|
||||
__objc_init_dispatch_tables()
|
||||
{
|
||||
__objc_uninstalled_dtable
|
||||
= sarray_new(200, __objc_init_install_dtable);
|
||||
= sarray_new(200, 0);
|
||||
}
|
||||
|
||||
/* This one is a bit hairy. This function is installed in the
|
||||
premature dispatch table, and thus called once for each class,
|
||||
namely when the very first message is send to it. */
|
||||
|
||||
static void __objc_init_install_dtable(id receiver, SEL op)
|
||||
/* This function is called by objc_msg_lookup when the
|
||||
dispatch table needs to be installed; thus it is called once
|
||||
for each class, namely when the very first message is sent to it. */
|
||||
static void
|
||||
__objc_init_install_dtable(id receiver, SEL op)
|
||||
{
|
||||
__label__ already_initialized;
|
||||
IMP imp;
|
||||
void* args;
|
||||
void* result;
|
||||
|
||||
/* This may happen, if the programmer has taken the address of a
|
||||
method before the dtable was initialized... too bad for him! */
|
||||
if(receiver->class_pointer->dtable != __objc_uninstalled_dtable)
|
||||
goto already_initialized;
|
||||
return;
|
||||
|
||||
objc_mutex_lock(__objc_runtime_mutex);
|
||||
|
||||
if(CLS_ISCLASS(receiver->class_pointer))
|
||||
{
|
||||
@ -198,31 +251,21 @@ static void __objc_init_install_dtable(id receiver, SEL op)
|
||||
else
|
||||
CLS_SETINITIALIZED((Class)receiver);
|
||||
}
|
||||
|
||||
already_initialized:
|
||||
|
||||
/* Get real method for this in newly installed dtable */
|
||||
imp = get_imp(receiver->class_pointer, op);
|
||||
|
||||
args = __builtin_apply_args();
|
||||
result = __builtin_apply((apply_t)imp, args, 96);
|
||||
if (result)
|
||||
__builtin_return (result);
|
||||
else
|
||||
return;
|
||||
|
||||
objc_mutex_unlock(__objc_runtime_mutex);
|
||||
}
|
||||
|
||||
/* Install dummy table for class which causes the first message to
|
||||
that class (or instances hereof) to be initialized properly */
|
||||
void __objc_install_premature_dtable(Class class)
|
||||
void
|
||||
__objc_install_premature_dtable(Class class)
|
||||
{
|
||||
assert(__objc_uninstalled_dtable);
|
||||
class->dtable = __objc_uninstalled_dtable;
|
||||
}
|
||||
|
||||
/* Send +initialize to class if not already done */
|
||||
static void __objc_send_initialize(Class class)
|
||||
static void
|
||||
__objc_send_initialize(Class class)
|
||||
{
|
||||
/* This *must* be a class object */
|
||||
assert(CLS_ISCLASS(class));
|
||||
@ -250,7 +293,8 @@ static void __objc_send_initialize(Class class)
|
||||
|
||||
for (i=0;i<method_list->method_count;i++) {
|
||||
method = &(method_list->method_list[i]);
|
||||
if (method->method_name->sel_id == op->sel_id) {
|
||||
if (method->method_name
|
||||
&& method->method_name->sel_id == op->sel_id) {
|
||||
imp = method->method_imp;
|
||||
break;
|
||||
}
|
||||
@ -267,16 +311,41 @@ static void __objc_send_initialize(Class class)
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Walk on the methods list of class and install the methods in the reverse
|
||||
order of the lists. Since methods added by categories are before the methods
|
||||
of class in the methods list, this allows categories to substitute methods
|
||||
declared in class. However if more than one category replaces the same
|
||||
method nothing is guaranteed about what method will be used.
|
||||
Assumes that __objc_runtime_mutex is locked down. */
|
||||
static void
|
||||
__objc_install_methods_in_dtable (Class class, MethodList_t method_list)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!method_list)
|
||||
return;
|
||||
|
||||
if (method_list->method_next)
|
||||
__objc_install_methods_in_dtable (class, method_list->method_next);
|
||||
|
||||
for (i = 0; i < method_list->method_count; i++)
|
||||
{
|
||||
Method_t method = &(method_list->method_list[i]);
|
||||
sarray_at_put_safe (class->dtable,
|
||||
(sidx) method->method_name->sel_id,
|
||||
method->method_imp);
|
||||
}
|
||||
}
|
||||
|
||||
/* Assumes that __objc_runtime_mutex is locked down. */
|
||||
static void
|
||||
__objc_install_dispatch_table_for_class (Class class)
|
||||
{
|
||||
Class super;
|
||||
MethodList_t mlist;
|
||||
int counter;
|
||||
|
||||
/* If the class has not yet had it's class links resolved, we must
|
||||
/* If the class has not yet had its class links resolved, we must
|
||||
re-compute all class links */
|
||||
if(!CLS_ISRESOLV(class))
|
||||
__objc_resolve_class_links();
|
||||
@ -289,47 +358,46 @@ __objc_install_dispatch_table_for_class (Class class)
|
||||
/* Allocate dtable if necessary */
|
||||
if (super == 0)
|
||||
{
|
||||
objc_mutex_lock(__objc_runtime_mutex);
|
||||
class->dtable = sarray_new (__objc_selector_max_index, 0);
|
||||
objc_mutex_unlock(__objc_runtime_mutex);
|
||||
}
|
||||
else
|
||||
class->dtable = sarray_lazy_copy (super->dtable);
|
||||
|
||||
for (mlist = class->methods; mlist; mlist = mlist->method_next)
|
||||
{
|
||||
counter = mlist->method_count - 1;
|
||||
while (counter >= 0)
|
||||
{
|
||||
Method_t method = &(mlist->method_list[counter]);
|
||||
sarray_at_put_safe (class->dtable,
|
||||
(sidx) method->method_name->sel_id,
|
||||
method->method_imp);
|
||||
counter -= 1;
|
||||
}
|
||||
}
|
||||
__objc_install_methods_in_dtable (class, class->methods);
|
||||
}
|
||||
|
||||
void __objc_update_dispatch_table_for_class (Class class)
|
||||
void
|
||||
__objc_update_dispatch_table_for_class (Class class)
|
||||
{
|
||||
Class next;
|
||||
struct sarray *arr;
|
||||
|
||||
/* not yet installed -- skip it */
|
||||
if (class->dtable == __objc_uninstalled_dtable)
|
||||
return;
|
||||
|
||||
sarray_free (class->dtable); /* release memory */
|
||||
objc_mutex_lock(__objc_runtime_mutex);
|
||||
|
||||
arr = class->dtable;
|
||||
__objc_install_premature_dtable (class); /* someone might require it... */
|
||||
__objc_install_dispatch_table_for_class (class); /* could have been lazy... */
|
||||
sarray_free (arr); /* release memory */
|
||||
|
||||
/* could have been lazy... */
|
||||
__objc_install_dispatch_table_for_class (class);
|
||||
|
||||
if (class->subclass_list) /* Traverse subclasses */
|
||||
for (next = class->subclass_list; next; next = next->sibling_class)
|
||||
__objc_update_dispatch_table_for_class (next);
|
||||
|
||||
objc_mutex_unlock(__objc_runtime_mutex);
|
||||
}
|
||||
|
||||
|
||||
/* This function adds a method list to a class. This function is
|
||||
typically called by another function specific to the run-time. As
|
||||
such this function does not worry about thread safe issued.
|
||||
such this function does not worry about thread safe issues.
|
||||
|
||||
This one is only called for categories. Class objects have their
|
||||
methods installed right away, and their selectors are made into
|
||||
@ -338,9 +406,6 @@ void
|
||||
class_add_method_list (Class class, MethodList_t list)
|
||||
{
|
||||
int i;
|
||||
static SEL initialize_sel = 0;
|
||||
if (!initialize_sel)
|
||||
initialize_sel = sel_register_name ("initialize");
|
||||
|
||||
/* Passing of a linked list is not allowed. Do multiple calls. */
|
||||
assert (!list->method_next);
|
||||
@ -356,24 +421,16 @@ class_add_method_list (Class class, MethodList_t list)
|
||||
method->method_name =
|
||||
sel_register_typed_name ((const char*)method->method_name,
|
||||
method->method_types);
|
||||
|
||||
if (search_for_method_in_list (class->methods, method->method_name)
|
||||
&& method->method_name->sel_id != initialize_sel->sel_id)
|
||||
{
|
||||
/* Duplication. Print a error message an change the method name
|
||||
to NULL. */
|
||||
fprintf (stderr, "attempt to add a existing method: %s\n",
|
||||
sel_get_name(method->method_name));
|
||||
method->method_name = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Add the methods to the class's method list. */
|
||||
list->method_next = class->methods;
|
||||
class->methods = list;
|
||||
}
|
||||
|
||||
/* Update the dispatch table of class */
|
||||
__objc_update_dispatch_table_for_class (class);
|
||||
}
|
||||
|
||||
Method_t
|
||||
class_get_instance_method(Class class, SEL op)
|
||||
@ -414,7 +471,7 @@ search_for_method_in_hierarchy (Class cls, SEL sel)
|
||||
/* Given a linked list of method and a method's name. Search for the named
|
||||
method's method structure. Return a pointer to the method's method
|
||||
structure if found. NULL otherwise. */
|
||||
static Method_t
|
||||
Method_t
|
||||
search_for_method_in_list (MethodList_t list, SEL op)
|
||||
{
|
||||
MethodList_t method_list = list;
|
||||
@ -447,6 +504,7 @@ search_for_method_in_list (MethodList_t list, SEL op)
|
||||
|
||||
static retval_t __objc_forward (id object, SEL sel, arglist_t args);
|
||||
|
||||
/* Forwarding pointers/integers through the normal registers */
|
||||
static id
|
||||
__objc_word_forward (id rcv, SEL op, ...)
|
||||
{
|
||||
@ -460,6 +518,21 @@ __objc_word_forward (id rcv, SEL op, ...)
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Specific routine for forwarding floats/double because of
|
||||
architectural differences on some processors. i386s for
|
||||
example which uses a floating point stack versus general
|
||||
registers for floating point numbers. This forward routine
|
||||
makes sure that GCC restores the proper return values */
|
||||
static double
|
||||
__objc_double_forward (id rcv, SEL op, ...)
|
||||
{
|
||||
void *args, *res;
|
||||
|
||||
args = __builtin_apply_args ();
|
||||
res = __objc_forward (rcv, op, args);
|
||||
__builtin_return (res);
|
||||
}
|
||||
|
||||
#if INVISIBLE_STRUCT_RETURN
|
||||
static __big
|
||||
#else
|
||||
@ -473,6 +546,12 @@ __objc_block_forward (id rcv, SEL op, ...)
|
||||
res = __objc_forward (rcv, op, args);
|
||||
if (res)
|
||||
__builtin_return (res);
|
||||
else
|
||||
#if INVISIBLE_STRUCT_RETURN
|
||||
return (__big) {{0, 0, 0, 0, 0, 0, 0, 0}};
|
||||
#else
|
||||
return nil;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@ -482,7 +561,7 @@ static retval_t
|
||||
__objc_forward (id object, SEL sel, arglist_t args)
|
||||
{
|
||||
IMP imp;
|
||||
static SEL frwd_sel = 0;
|
||||
static SEL frwd_sel = 0; /* !T:SAFE2 */
|
||||
SEL err_sel;
|
||||
|
||||
/* first try if the object understands forward:: */
|
||||
@ -526,14 +605,19 @@ __objc_forward (id object, SEL sel, arglist_t args)
|
||||
|
||||
/* The object doesn't respond to doesNotRecognize: or error:; Therefore,
|
||||
a default action is taken. */
|
||||
fprintf (stderr, "fatal: %s\n", msg);
|
||||
abort ();
|
||||
objc_error (object, OBJC_ERR_UNIMPLEMENTED, "%s\n", msg);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
void __objc_print_dtable_stats()
|
||||
void
|
||||
__objc_print_dtable_stats()
|
||||
{
|
||||
int total = 0;
|
||||
|
||||
objc_mutex_lock(__objc_runtime_mutex);
|
||||
|
||||
printf("memory usage: (%s)\n",
|
||||
#ifdef OBJC_SPARSE2
|
||||
"2-level sparse arrays"
|
||||
@ -542,20 +626,28 @@ void __objc_print_dtable_stats()
|
||||
#endif
|
||||
);
|
||||
|
||||
printf("arrays: %d = %lu bytes\n",
|
||||
narrays, (unsigned long)narrays*sizeof(struct sarray));
|
||||
printf("arrays: %d = %ld bytes\n", narrays,
|
||||
(long)narrays*sizeof(struct sarray));
|
||||
total += narrays*sizeof(struct sarray);
|
||||
printf("buckets: %d = %lu bytes\n",
|
||||
nbuckets, (unsigned long)nbuckets*sizeof(struct sbucket));
|
||||
printf("buckets: %d = %ld bytes\n", nbuckets,
|
||||
(long)nbuckets*sizeof(struct sbucket));
|
||||
total += nbuckets*sizeof(struct sbucket);
|
||||
|
||||
printf("idxtables: %d = %lu bytes\n",
|
||||
idxsize, (unsigned long)idxsize*sizeof(void*));
|
||||
printf("idxtables: %d = %ld bytes\n", idxsize, (long)idxsize*sizeof(void*));
|
||||
total += idxsize*sizeof(void*);
|
||||
printf("-----------------------------------\n");
|
||||
printf("total: %d bytes\n", total);
|
||||
printf("===================================\n");
|
||||
|
||||
objc_mutex_unlock(__objc_runtime_mutex);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Returns the uninstalled dispatch table indicator.
|
||||
If a class' dispatch table points to __objc_uninstalled_dtable
|
||||
then that means it needs its dispatch table to be installed. */
|
||||
__inline__
|
||||
struct sarray*
|
||||
objc_get_uninstalled_dtable()
|
||||
{
|
||||
return __objc_uninstalled_dtable;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user