diff options
author | Ovidiu Predescu <ovidiu@aracnet.com> | 1998-09-02 23:48:01 +0000 |
---|---|---|
committer | Jeff Law <law@gcc.gnu.org> | 1998-09-02 17:48:01 -0600 |
commit | d8bb17c8e33975277dd5231e190bf99451b7025d (patch) | |
tree | 1c3b607110323b831606c2ec27d39a0f292a1a15 /gcc/objc/encoding.c | |
parent | d636ae0b801758df397e3e1ee790ed041a48b9f9 (diff) | |
download | gcc-d8bb17c8e33975277dd5231e190bf99451b7025d.zip gcc-d8bb17c8e33975277dd5231e190bf99451b7025d.tar.gz gcc-d8bb17c8e33975277dd5231e190bf99451b7025d.tar.bz2 |
Boehm GC support for ObjC from Ovidiu Predescu.
From-SVN: r22199
Diffstat (limited to 'gcc/objc/encoding.c')
-rw-r--r-- | gcc/objc/encoding.c | 445 |
1 files changed, 409 insertions, 36 deletions
diff --git a/gcc/objc/encoding.c b/gcc/objc/encoding.c index c90e914..ab47e9c 100644 --- a/gcc/objc/encoding.c +++ b/gcc/objc/encoding.c @@ -1,6 +1,7 @@ /* Encoding of types for Objective C. - Copyright (C) 1993, 1995, 1996, 1997 Free Software Foundation, Inc. + Copyright (C) 1993, 1995, 1996, 1997, 1998 Free Software Foundation, Inc. Contributed by Kresten Krab Thorup + Bitfield support by Ovidiu Predescu This file is part of GNU CC. @@ -25,6 +26,8 @@ Boston, MA 02111-1307, USA. */ This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License. */ +#include "tconfig.h" +#include "objc-api.h" #include "encoding.h" #define MAX(X, Y) \ @@ -40,6 +43,22 @@ Boston, MA 02111-1307, USA. */ __a*((__v+__a-1)/__a); }) +/* Various hacks for objc_layout_record. These are used by the target + macros. */ + +#define TREE_CODE(TYPE) *TYPE +#define RECORD_TYPE _C_STRUCT_B +#define UNION_TYPE _C_UNION_B +#define QUAL_UNION_TYPE _C_UNION_B + +#define TYPE_FIELDS(TYPE) objc_skip_typespec (TYPE) + +#define DECL_MODE(TYPE) *(TYPE) + +#define DFmode _C_DBL + + + static inline int atoi (const char* str) { @@ -56,8 +75,15 @@ atoi (const char* str) */ int -objc_sizeof_type(const char* type) +objc_sizeof_type (const char* type) { + /* Skip the variable name if any */ + if (*type == '"') + { + for (type++; *type++ != '"';) + /* do nothing */; + } + switch(*type) { case _C_ID: return sizeof(id); @@ -103,6 +129,14 @@ objc_sizeof_type(const char* type) return sizeof(unsigned long); break; + case _C_LNG_LNG: + return sizeof(long long); + break; + + case _C_ULNG_LNG: + return sizeof(unsigned long long); + break; + case _C_FLT: return sizeof(float); break; @@ -111,6 +145,9 @@ objc_sizeof_type(const char* type) return sizeof(double); break; + case _C_VOID: + return sizeof(void); + break; case _C_PTR: case _C_ATOM: case _C_CHARPTR: @@ -125,19 +162,32 @@ objc_sizeof_type(const char* type) } break; + case _C_BFLD: + { + /* The new encoding of bitfields is: b 'position' 'type' 'size' */ + int position, size; + int startByte, endByte; + + position = atoi (type + 1); + while (isdigit (*++type)); + size = atoi (type + 1); + + startByte = position / BITS_PER_UNIT; + endByte = (position + size) / BITS_PER_UNIT; + return endByte - startByte; + } + case _C_STRUCT_B: { - int acc_size = 0; - int align; - while (*type != _C_STRUCT_E && *type++ != '='); /* skip "<name>=" */ - while (*type != _C_STRUCT_E) - { - align = objc_alignof_type (type); /* padd to alignment */ - acc_size = ROUND (acc_size, align); - acc_size += objc_sizeof_type (type); /* add component size */ - type = objc_skip_typespec (type); /* skip component */ - } - return acc_size; + struct objc_struct_layout layout; + unsigned int size; + + objc_layout_structure (type, &layout); + while (objc_layout_structure_next_member (&layout)) + /* do nothing */ ; + objc_layout_finish_structure (&layout, &size, NULL); + + return size; } case _C_UNION_B: @@ -146,6 +196,12 @@ objc_sizeof_type(const char* type) while (*type != _C_UNION_E && *type++ != '=') /* do nothing */; while (*type != _C_UNION_E) { + /* Skip the variable name if any */ + if (*type == '"') + { + for (type++; *type++ != '"';) + /* do nothing */; + } max_size = MAX (max_size, objc_sizeof_type (type)); type = objc_skip_typespec (type); } @@ -153,7 +209,10 @@ objc_sizeof_type(const char* type) } default: - objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); + { + objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); + return 0; + } } } @@ -165,6 +224,12 @@ objc_sizeof_type(const char* type) int objc_alignof_type(const char* type) { + /* Skip the variable name if any */ + if (*type == '"') + { + for (type++; *type++ != '"';) + /* do nothing */; + } switch(*type) { case _C_ID: return __alignof__(id); @@ -210,6 +275,14 @@ objc_alignof_type(const char* type) return __alignof__(unsigned long); break; + case _C_LNG_LNG: + return __alignof__(long long); + break; + + case _C_ULNG_LNG: + return __alignof__(unsigned long long); + break; + case _C_FLT: return __alignof__(float); break; @@ -227,15 +300,18 @@ objc_alignof_type(const char* type) case _C_ARY_B: while (isdigit(*++type)) /* do nothing */; return objc_alignof_type (type); - + case _C_STRUCT_B: { - struct { int x; double y; } fooalign; - while(*type != _C_STRUCT_E && *type++ != '=') /* do nothing */; - if (*type != _C_STRUCT_E) - return MAX (objc_alignof_type (type), __alignof__ (fooalign)); - else - return __alignof__ (fooalign); + struct objc_struct_layout layout; + unsigned int align; + + objc_layout_structure (type, &layout); + while (objc_layout_structure_next_member (&layout)) + /* do nothing */; + objc_layout_finish_structure (&layout, NULL, &align); + + return align; } case _C_UNION_B: @@ -244,14 +320,23 @@ objc_alignof_type(const char* type) while (*type != _C_UNION_E && *type++ != '=') /* do nothing */; while (*type != _C_UNION_E) { + /* Skip the variable name if any */ + if (*type == '"') + { + for (type++; *type++ != '"';) + /* do nothing */; + } maxalign = MAX (maxalign, objc_alignof_type (type)); type = objc_skip_typespec (type); } return maxalign; } - + default: - objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); + { + objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); + return 0; + } } } @@ -262,8 +347,18 @@ objc_alignof_type(const char* type) int objc_aligned_size (const char* type) { - int size = objc_sizeof_type (type); - int align = objc_alignof_type (type); + int size, align; + + /* Skip the variable name */ + if (*type == '"') + { + for (type++; *type++ != '"';) + /* do nothing */; + } + + size = objc_sizeof_type (type); + align = objc_alignof_type (type); + return ROUND (size, align); } @@ -275,8 +370,17 @@ objc_aligned_size (const char* type) int objc_promoted_size (const char* type) { - int size = objc_sizeof_type (type); - int wordsize = sizeof (void*); + int size, wordsize; + + /* Skip the variable name */ + if (*type == '"') + { + for (type++; *type++ != '"';) + /* do nothing */; + } + + size = objc_sizeof_type (type); + wordsize = sizeof (void*); return ROUND (size, wordsize); } @@ -294,7 +398,8 @@ objc_skip_type_qualifiers (const char* type) || *type == _C_INOUT || *type == _C_OUT || *type == _C_BYCOPY - || *type == _C_ONEWAY) + || *type == _C_ONEWAY + || *type == _C_GCINVISIBLE) { type += 1; } @@ -310,6 +415,13 @@ objc_skip_type_qualifiers (const char* type) const char* objc_skip_typespec (const char* type) { + /* Skip the variable name if any */ + if (*type == '"') + { + for (type++; *type++ != '"';) + /* do nothing */; + } + type = objc_skip_type_qualifiers (type); switch (*type) { @@ -339,6 +451,8 @@ objc_skip_typespec (const char* type) case _C_UINT: case _C_LNG: case _C_ULNG: + case _C_LNG_LNG: + case _C_ULNG_LNG: case _C_FLT: case _C_DBL: case _C_VOID: @@ -354,7 +468,16 @@ objc_skip_typespec (const char* type) if (*type == _C_ARY_E) return ++type; else - objc_error(nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type); + { + objc_error(nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type); + return 0; + } + + case _C_BFLD: + /* The new encoding of bitfields is: b 'position' 'type' 'size' */ + while (isdigit (*++type)); /* skip position */ + while (isdigit (*++type)); /* skip type and size */ + return type; case _C_STRUCT_B: /* skip name, and elements until closing '}' */ @@ -376,7 +499,10 @@ objc_skip_typespec (const char* type) return objc_skip_typespec (++type); default: - objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); + { + objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); + return 0; + } } } @@ -526,14 +652,261 @@ objc_get_type_qualifiers (const char* type) while (flag) switch (*type++) { - case _C_CONST: res |= _F_CONST; break; - case _C_IN: res |= _F_IN; break; - case _C_INOUT: res |= _F_INOUT; break; - case _C_OUT: res |= _F_OUT; break; - case _C_BYCOPY: res |= _F_BYCOPY; break; - case _C_ONEWAY: res |= _F_ONEWAY; break; + case _C_CONST: res |= _F_CONST; break; + case _C_IN: res |= _F_IN; break; + case _C_INOUT: res |= _F_INOUT; break; + case _C_OUT: res |= _F_OUT; break; + case _C_BYCOPY: res |= _F_BYCOPY; break; + case _C_ONEWAY: res |= _F_ONEWAY; break; + case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break; default: flag = NO; } return res; } + + +/* The following three functions can be used to determine how a + structure is laid out by the compiler. For example: + + struct objc_struct_layout layout; + int i; + + objc_layout_structure (type, &layout); + while (objc_layout_structure_next_member (&layout)) + { + int position, align; + const char *type; + + objc_layout_structure_get_info (&layout, &position, &align, &type); + printf ("element %d has offset %d, alignment %d\n", + i++, position, align); + } + + These functions are used by objc_sizeof_type and objc_alignof_type + functions to compute the size and alignment of structures. The + previous method of computing the size and alignment of a structure + was not working on some architectures, particulary on AIX, and in + the presence of bitfields inside the structure. */ +void +objc_layout_structure (const char *type, + struct objc_struct_layout *layout) +{ + const char *ntype; + + if (*type++ != _C_STRUCT_B) + { + objc_error(nil, OBJC_ERR_BAD_TYPE, + "record type expected in objc_layout_structure, got %s\n", + type); + } + + layout->original_type = type; + + /* Skip "<name>=" if any. Avoid embedded structures and unions. */ + ntype = type; + while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B + && *ntype++ != '=') + /* do nothing */; + + /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */ + if (*(ntype - 1) == '=') + type = ntype; + + layout->type = type; + layout->prev_type = NULL; + layout->record_size = 0; + layout->record_align = BITS_PER_UNIT; + +#ifdef STRUCTURE_SIZE_BOUNDARY + layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY); +#endif +} + + +BOOL +objc_layout_structure_next_member (struct objc_struct_layout *layout) +{ + register int known_align = layout->record_size; + register int desired_align = 0; + + /* The following are used only if the field is a bitfield */ + register const char *bfld_type; + register int bfld_type_size, bfld_type_align, bfld_field_size; + + /* The current type without the type qualifiers */ + const char *type; + +#if 1 + if (layout->prev_type == NULL) + { + layout->prev_type = layout->type; + layout->type = objc_skip_typespec (layout->prev_type); + return YES; + } +#endif + + /* Add the size of the previous field to the size of the record. */ + if (layout->prev_type) + { + type = objc_skip_type_qualifiers (layout->prev_type); + + if (*type != _C_BFLD) + layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT; + else { + desired_align = 1; + /* Get the bitfield's type */ + for (bfld_type = type + 1; + isdigit(*bfld_type); + bfld_type++) + /* do nothing */; + + bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT; + bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT; + bfld_field_size = atoi (objc_skip_typespec (bfld_type)); + layout->record_size += bfld_field_size; + } + } + + if (*layout->type == _C_STRUCT_E) + return NO; + + /* Skip the variable name if any */ + if (*layout->type == '"') + { + for (layout->type++; *layout->type++ != '"';) + /* do nothing */; + } + + type = objc_skip_type_qualifiers (layout->type); + + if (*type != _C_BFLD) + desired_align = objc_alignof_type(type) * BITS_PER_UNIT; + else + { + desired_align = 1; + /* Skip the bitfield's offset */ + for (bfld_type = type + 1; isdigit(*bfld_type); bfld_type++) + /* do nothing */; + + bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT; + bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT; + bfld_field_size = atoi (objc_skip_typespec (bfld_type)); + } + +#ifdef BIGGEST_FIELD_ALIGNMENT + desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT); +#endif +#ifdef ADJUST_FIELD_ALIGN + desired_align = ADJUST_FIELD_ALIGN (type, desired_align); +#endif + + /* Record must have at least as much alignment as any field. + Otherwise, the alignment of the field within the record + is meaningless. */ +#ifndef PCC_BITFIELD_TYPE_MATTERS + layout->record_align = MAX (layout->record_align, desired_align); +#else + if (*type == _C_BFLD) + { + /* For these machines, a zero-length field does not + affect the alignment of the structure as a whole. + It does, however, affect the alignment of the next field + within the structure. */ + if (bfld_field_size) + layout->record_align = MAX (layout->record_align, desired_align); + else + desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT; + + /* A named bit field of declared type `int' + forces the entire structure to have `int' alignment. + Q1: How is encoded this thing and how to check for it? + Q2: How to determine maximum_field_alignment at runtime? */ + +/* if (DECL_NAME (field) != 0) */ + { + int type_align = bfld_type_align; +#if 0 + if (maximum_field_alignment != 0) + type_align = MIN (type_align, maximum_field_alignment); + else if (DECL_PACKED (field)) + type_align = MIN (type_align, BITS_PER_UNIT); +#endif + + layout->record_align = MAX (layout->record_align, type_align); + } + } + else + layout->record_align = MAX (layout->record_align, desired_align); +#endif + + /* Does this field automatically have alignment it needs + by virtue of the fields that precede it and the record's + own alignment? */ + + if (*type == _C_BFLD) + layout->record_size = atoi (type + 1); + else if (layout->record_size % desired_align != 0) + { + /* No, we need to skip space before this field. + Bump the cumulative size to multiple of field alignment. */ + layout->record_size = ROUND (layout->record_size, desired_align); + } + + /* Jump to the next field in record. */ + + layout->prev_type = layout->type; + layout->type = objc_skip_typespec (layout->type); /* skip component */ + + return YES; +} + + +void objc_layout_finish_structure (struct objc_struct_layout *layout, + unsigned int *size, + unsigned int *align) +{ + if (layout->type && *layout->type == _C_STRUCT_E) + { + /* Work out the alignment of the record as one expression and store + in the record type. Round it up to a multiple of the record's + alignment. */ + +#ifdef ROUND_TYPE_ALIGN + layout->record_align = ROUND_TYPE_ALIGN (layout->original_type, + 1, + layout->record_align); +#else + layout->record_align = MAX (1, layout->record_align); +#endif + +#ifdef ROUND_TYPE_SIZE + layout->record_size = ROUND_TYPE_SIZE (layout->original_type, + layout->record_size, + layout->record_align); +#else + /* Round the size up to be a multiple of the required alignment */ + layout->record_size = ROUND (layout->record_size, layout->record_align); +#endif + + layout->type = NULL; + } + if (size) + *size = layout->record_size / BITS_PER_UNIT; + if (align) + *align = layout->record_align / BITS_PER_UNIT; +} + + +void objc_layout_structure_get_info (struct objc_struct_layout *layout, + unsigned int *offset, + unsigned int *align, + const char **type) +{ + if (offset) + *offset = layout->record_size / BITS_PER_UNIT; + if (align) + *align = layout->record_align / BITS_PER_UNIT; + if (type) + *type = layout->prev_type; +} |