#include "tconfig.h"
#include "objc.h"
#include "encoding.h"
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#if OBJC_WITH_GC
#include <gc.h>
#include <limits.h>
typedef GC_word word;
typedef GC_signed_word signed_word;
#define BITS_PER_WORD (CHAR_BIT * sizeof (word))
#include <gc_typed.h>
#define ROUND(V, A) \
({ typeof (V) __v = (V); typeof (A) __a = (A); \
__a * ((__v+__a - 1)/__a); })
#define SET_BIT_FOR_OFFSET(mask, offset) \
GC_set_bit (mask, offset / sizeof (void *))
static void
__objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset);
static void
__objc_gc_setup_union (GC_bitmap mask, const char *type, int offset);
static void
__objc_gc_setup_array (GC_bitmap mask, const char *type, int offset)
{
int i, len = atoi (type + 1);
while (isdigit (*++type))
;
switch (*type) {
case _C_ARY_B:
for (i = 0; i < len; i++)
__objc_gc_setup_array (mask, type, offset);
break;
case _C_STRUCT_B:
for (i = 0; i < len; i++)
__objc_gc_setup_struct (mask, type, offset);
break;
case _C_UNION_B:
for (i = 0; i < len; i++)
__objc_gc_setup_union (mask, type, offset);
break;
default:
break;
}
}
static void
__objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset)
{
struct objc_struct_layout layout;
unsigned int position;
const char *mtype;
objc_layout_structure (type, &layout);
while (objc_layout_structure_next_member (&layout))
{
BOOL gc_invisible = NO;
objc_layout_structure_get_info (&layout, &position, NULL, &mtype);
if (*mtype == '"')
{
for (mtype++; *mtype++ != '"';)
;
}
if (*mtype == _C_GCINVISIBLE)
{
gc_invisible = YES;
mtype++;
}
position += offset;
switch (*mtype) {
case _C_ID:
case _C_CLASS:
case _C_SEL:
case _C_PTR:
case _C_CHARPTR:
case _C_ATOM:
if (! gc_invisible)
SET_BIT_FOR_OFFSET (mask, position);
break;
case _C_ARY_B:
__objc_gc_setup_array (mask, mtype, position);
break;
case _C_STRUCT_B:
__objc_gc_setup_struct (mask, mtype, position);
break;
case _C_UNION_B:
__objc_gc_setup_union (mask, mtype, position);
break;
default:
break;
}
}
}
static void
__objc_gc_setup_union (GC_bitmap mask, const char *type, int offset)
{
int i, size, align;
if (*type == '"')
{
for (type++; *type++ != '"';)
;
}
size = objc_sizeof_type (type);
align = objc_alignof_type (type);
offset = ROUND (offset, align);
for (i = 0; i < size; i += sizeof (void *))
{
SET_BIT_FOR_OFFSET (mask, offset);
offset += sizeof (void *);
}
}
static void
__objc_gc_type_description_from_type (GC_bitmap mask, const char *type)
{
struct objc_struct_layout layout;
unsigned int offset, align;
const char *ivar_type;
objc_layout_structure (type, &layout);
while (objc_layout_structure_next_member (&layout))
{
BOOL gc_invisible = NO;
objc_layout_structure_get_info (&layout, &offset, &align, &ivar_type);
if (*ivar_type == '"')
{
for (ivar_type++; *ivar_type++ != '"';)
;
}
if (*ivar_type == _C_GCINVISIBLE)
{
gc_invisible = YES;
ivar_type++;
}
switch (*ivar_type) {
case _C_ID:
case _C_CLASS:
case _C_SEL:
case _C_PTR:
case _C_CHARPTR:
if (! gc_invisible)
SET_BIT_FOR_OFFSET (mask, offset);
break;
case _C_ARY_B:
__objc_gc_setup_array (mask, ivar_type, offset);
break;
case _C_STRUCT_B:
__objc_gc_setup_struct (mask, ivar_type, offset);
break;
case _C_UNION_B:
__objc_gc_setup_union (mask, ivar_type, offset);
break;
default:
break;
}
}
}
static void
__objc_class_structure_encoding (Class class, char **type, int *size,
int *current)
{
int i, ivar_count;
struct objc_ivar_list *ivars;
if (! class)
{
strcat (*type, "{");
*current++;
return;
}
__objc_class_structure_encoding (class->super_class, type, size, current);
ivars = class->ivars;
if (! ivars)
return;
ivar_count = ivars->ivar_count;
for (i = 0; i < ivar_count; i++)
{
struct objc_ivar *ivar = &(ivars->ivar_list[i]);
const char *ivar_type = ivar->ivar_type;
int len = strlen (ivar_type);
if (*current + len + 1 >= *size)
{
*size = ROUND (*current + len + 1, 10);
*type = objc_realloc (*type, *size);
}
strcat (*type + *current, ivar_type);
*current += len;
}
}
void
__objc_generate_gc_type_description (Class class)
{
GC_bitmap mask;
int bits_no, size;
int type_size = 10, current;
char *class_structure_type;
if (! CLS_ISCLASS (class))
return;
bits_no = (ROUND (class_get_instance_size (class), sizeof (void *))
/ sizeof (void *));
size = ROUND (bits_no, BITS_PER_WORD) / BITS_PER_WORD;
mask = objc_atomic_malloc (size * sizeof (int));
memset (mask, 0, size * sizeof (int));
class_structure_type = objc_atomic_malloc (type_size);
*class_structure_type = current = 0;
__objc_class_structure_encoding (class, &class_structure_type,
&type_size, ¤t);
if (current + 1 == type_size)
class_structure_type = objc_realloc (class_structure_type, ++type_size);
strcat (class_structure_type + current, "}");
#ifdef DEBUG
printf ("type description for '%s' is %s\n", class->name, class_structure_type);
#endif
__objc_gc_type_description_from_type (mask, class_structure_type);
objc_free (class_structure_type);
#ifdef DEBUG
printf (" mask for '%s', type '%s' (bits %d, mask size %d) is:",
class_structure_type, class->name, bits_no, size);
{
int i;
for (i = 0; i < size; i++)
printf (" %lx", mask[i]);
}
puts ("");
#endif
class->gc_object_type = (void *) GC_make_descriptor (mask, bits_no);
}
static inline BOOL
__objc_ivar_pointer (const char *type)
{
type = objc_skip_type_qualifiers (type);
return (*type == _C_ID
|| *type == _C_CLASS
|| *type == _C_SEL
|| *type == _C_PTR
|| *type == _C_CHARPTR
|| *type == _C_ATOM);
}
void
class_ivar_set_gcinvisible (Class class, const char *ivarname,
BOOL gc_invisible)
{
int i, ivar_count;
struct objc_ivar_list *ivars;
if (! class || ! ivarname)
return;
ivars = class->ivars;
if (! ivars)
return;
ivar_count = ivars->ivar_count;
for (i = 0; i < ivar_count; i++)
{
struct objc_ivar *ivar = &(ivars->ivar_list[i]);
const char *type;
if (! ivar->ivar_name || strcmp (ivar->ivar_name, ivarname))
continue;
assert (ivar->ivar_type);
type = ivar->ivar_type;
if (*type == '"')
{
for (type++; *type++ != '"';)
;
}
if (*type == _C_GCINVISIBLE)
{
char *new_type;
if (gc_invisible || ! __objc_ivar_pointer (type))
return;
new_type = objc_atomic_malloc (strlen (ivar->ivar_type));
strncpy (new_type, ivar->ivar_type,
(size_t)(type - ivar->ivar_type));
strcat (new_type, type + 1);
ivar->ivar_type = new_type;
}
else
{
char *new_type;
if (! gc_invisible || ! __objc_ivar_pointer (type))
return;
new_type = objc_malloc (strlen (ivar->ivar_type) + 2);
strncpy (new_type, ivar->ivar_type,
(size_t)(type - ivar->ivar_type));
strcat (new_type, "!");
strcat (new_type, type);
ivar->ivar_type = new_type;
}
__objc_generate_gc_type_description (class);
return;
}
class_ivar_set_gcinvisible (class->super_class, ivarname, gc_invisible);
}
#else
void
__objc_generate_gc_type_description (Class class __attribute__ ((__unused__)))
{
}
void class_ivar_set_gcinvisible (Class class __attribute__ ((__unused__)),
const char *ivarname __attribute__ ((__unused__)),
BOOL gc_invisible __attribute__ ((__unused__)))
{
}
#endif