1 /* Encoding of types for Objective C.
2 Copyright (C) 1993, 1995, 1996, 1997, 1998, 2000, 2002, 2004
3 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
5 Bitfield support by Ovidiu Predescu
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 /* As a special exception, if you link this library with files
25 compiled with GCC to produce an executable, this does not cause
26 the resulting executable to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
30 /* FIXME: This file has no business including tm.h. */
33 #include "coretypes.h"
35 #include "objc/objc-api.h"
36 #include "objc/encoding.h"
41 ({ typeof (X) __x = (X), __y = (Y); \
42 (__x > __y ? __x : __y); })
46 ({ typeof (X) __x = (X), __y = (Y); \
47 (__x < __y ? __x : __y); })
51 ({ typeof (V) __v = (V); typeof (A) __a = (A); \
52 __a * ((__v+__a - 1)/__a); })
55 /* Various hacks for objc_layout_record. These are used by the target
58 #define TREE_CODE(TYPE) *(TYPE)
59 #define TREE_TYPE(TREE) (TREE)
61 #define RECORD_TYPE _C_STRUCT_B
62 #define UNION_TYPE _C_UNION_B
63 #define QUAL_UNION_TYPE _C_UNION_B
64 #define ARRAY_TYPE _C_ARY_B
66 #define REAL_TYPE _C_DBL
68 #define VECTOR_TYPE _C_VECTOR
70 #define TYPE_FIELDS(TYPE) objc_skip_typespec (TYPE)
72 #define DECL_MODE(TYPE) *(TYPE)
73 #define TYPE_MODE(TYPE) *(TYPE)
77 #define get_inner_array_type(TYPE) ((TYPE) + 1)
79 /* Some ports (eg ARM) allow the structure size boundary to be
80 selected at compile-time. We override the normal definition with
81 one that has a constant value for this compilation. */
83 #define BITS_PER_UNIT 8
85 #undef STRUCTURE_SIZE_BOUNDARY
86 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
88 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
89 target_flags. Define a dummy entry here to so we don't die.
90 We have to rename it because target_flags may already have been
92 #define target_flags not_target_flags
93 static int __attribute__ ((__unused__)) not_target_flags = 0;
95 /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
96 Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */
97 #undef ALTIVEC_VECTOR_MODE
98 #define ALTIVEC_VECTOR_MODE(MODE) (0)
101 /* FIXME: while this file has no business including tm.h, this
102 definitely has no business defining this macro but it
103 is only way around without really rewritting this file,
104 should look after the branch of 3.4 to fix this. */
105 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
106 ((TYPE_FIELDS (STRUCT) != 0 \
107 && DECL_MODE (TYPE_FIELDS (STRUCT)) == DFmode) \
108 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
109 : MAX (COMPUTED, SPECIFIED))
112 return the size of an object specified by type
116 objc_sizeof_type (const char *type)
118 /* Skip the variable name if any */
121 for (type++; *type++ != '"';)
127 return sizeof (_Bool);
135 return sizeof (Class);
143 return sizeof (char);
147 return sizeof (unsigned char);
151 return sizeof (short);
155 return sizeof (unsigned short);
163 return sizeof (unsigned int);
167 return sizeof (long);
171 return sizeof (unsigned long);
175 return sizeof (long long);
179 return sizeof (unsigned long long);
183 return sizeof (float);
187 return sizeof (double);
191 return sizeof (void);
197 return sizeof (char *);
202 int len = atoi (type + 1);
203 while (isdigit ((unsigned char)*++type))
205 return len * objc_aligned_size (type);
211 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
213 int startByte, endByte;
215 position = atoi (type + 1);
216 while (isdigit ((unsigned char)*++type))
218 size = atoi (type + 1);
220 startByte = position / BITS_PER_UNIT;
221 endByte = (position + size) / BITS_PER_UNIT;
222 return endByte - startByte;
228 struct objc_struct_layout layout;
231 objc_layout_structure (type, &layout);
232 while (objc_layout_structure_next_member (&layout))
234 objc_layout_finish_structure (&layout, &size, NULL);
241 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
249 Return the alignment of an object specified by type
253 objc_alignof_type (const char *type)
255 /* Skip the variable name if any */
258 for (type++; *type++ != '"';)
263 return __alignof__ (_Bool);
267 return __alignof__ (id);
271 return __alignof__ (Class);
275 return __alignof__ (SEL);
279 return __alignof__ (char);
283 return __alignof__ (unsigned char);
287 return __alignof__ (short);
291 return __alignof__ (unsigned short);
295 return __alignof__ (int);
299 return __alignof__ (unsigned int);
303 return __alignof__ (long);
307 return __alignof__ (unsigned long);
311 return __alignof__ (long long);
315 return __alignof__ (unsigned long long);
319 return __alignof__ (float);
323 return __alignof__ (double);
329 return __alignof__ (char *);
333 while (isdigit ((unsigned char)*++type))
335 return objc_alignof_type (type);
340 struct objc_struct_layout layout;
343 objc_layout_structure (type, &layout);
344 while (objc_layout_structure_next_member (&layout))
346 objc_layout_finish_structure (&layout, NULL, &align);
353 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
360 The aligned size if the size rounded up to the nearest alignment.
364 objc_aligned_size (const char *type)
368 /* Skip the variable name */
371 for (type++; *type++ != '"';)
375 size = objc_sizeof_type (type);
376 align = objc_alignof_type (type);
378 return ROUND (size, align);
382 The size rounded up to the nearest integral of the wordsize, taken
383 to be the size of a void *.
387 objc_promoted_size (const char *type)
391 /* Skip the variable name */
394 for (type++; *type++ != '"';)
398 size = objc_sizeof_type (type);
399 wordsize = sizeof (void *);
401 return ROUND (size, wordsize);
405 Skip type qualifiers. These may eventually precede typespecs
406 occurring in method prototype encodings.
410 objc_skip_type_qualifiers (const char *type)
412 while (*type == _C_CONST
416 || *type == _C_BYCOPY
418 || *type == _C_ONEWAY
419 || *type == _C_GCINVISIBLE)
428 Skip one typespec element. If the typespec is prepended by type
429 qualifiers, these are skipped as well.
433 objc_skip_typespec (const char *type)
435 /* Skip the variable name if any */
438 for (type++; *type++ != '"';)
442 type = objc_skip_type_qualifiers (type);
447 /* An id may be annotated by the actual type if it is known
448 with the @"ClassName" syntax */
454 while (*++type != '"')
459 /* The following are one character type codes */
483 /* skip digits, typespec and closing ']' */
485 while (isdigit ((unsigned char)*++type))
487 type = objc_skip_typespec (type);
488 if (*type == _C_ARY_E)
492 objc_error (nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
497 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
498 while (isdigit ((unsigned char)*++type))
499 ; /* skip position */
500 while (isdigit ((unsigned char)*++type))
501 ; /* skip type and size */
505 /* skip name, and elements until closing '}' */
507 while (*type != _C_STRUCT_E && *type++ != '=')
509 while (*type != _C_STRUCT_E)
511 type = objc_skip_typespec (type);
516 /* skip name, and elements until closing ')' */
518 while (*type != _C_UNION_E && *type++ != '=')
520 while (*type != _C_UNION_E)
522 type = objc_skip_typespec (type);
527 /* Just skip the following typespec */
529 return objc_skip_typespec (++type);
533 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
540 Skip an offset as part of a method encoding. This is prepended by a
541 '+' if the argument is passed in registers.
544 objc_skip_offset (const char *type)
548 while (isdigit ((unsigned char) *++type))
554 Skip an argument specification of a method encoding.
557 objc_skip_argspec (const char *type)
559 type = objc_skip_typespec (type);
560 type = objc_skip_offset (type);
565 Return the number of arguments that the method MTH expects.
566 Note that all methods need two implicit arguments `self' and
570 method_get_number_of_arguments (struct objc_method *mth)
573 const char *type = mth->method_types;
576 type = objc_skip_argspec (type);
583 Return the size of the argument block needed on the stack to invoke
584 the method MTH. This may be zero, if all arguments are passed in
589 method_get_sizeof_arguments (struct objc_method *mth)
591 const char *type = objc_skip_typespec (mth->method_types);
596 Return a pointer to the next argument of ARGFRAME. type points to
597 the last argument. Typical use of this look like:
601 for (datum = method_get_first_argument (method, argframe, &type);
602 datum; datum = method_get_next_argument (argframe, &type))
604 unsigned flags = objc_get_type_qualifiers (type);
605 type = objc_skip_type_qualifiers (type);
607 [portal encodeData: datum ofType: type];
610 if ((flags & _F_IN) == _F_IN)
611 [portal encodeData: *(char **) datum ofType: ++type];
618 method_get_next_argument (arglist_t argframe, const char **type)
620 const char *t = objc_skip_argspec (*type);
626 t = objc_skip_typespec (t);
629 return argframe->arg_regs + atoi (++t);
631 return argframe->arg_ptr + atoi (t);
635 Return a pointer to the value of the first argument of the method
636 described in M with the given argumentframe ARGFRAME. The type
637 is returned in TYPE. type must be passed to successive calls of
638 method_get_next_argument.
641 method_get_first_argument (struct objc_method *m,
645 *type = m->method_types;
646 return method_get_next_argument (argframe, type);
650 Return a pointer to the ARGth argument of the method
651 M from the frame ARGFRAME. The type of the argument
652 is returned in the value-result argument TYPE
656 method_get_nth_argument (struct objc_method *m,
657 arglist_t argframe, int arg,
660 const char *t = objc_skip_argspec (m->method_types);
662 if (arg > method_get_number_of_arguments (m))
666 t = objc_skip_argspec (t);
669 t = objc_skip_typespec (t);
672 return argframe->arg_regs + atoi (++t);
674 return argframe->arg_ptr + atoi (t);
678 objc_get_type_qualifiers (const char *type)
686 case _C_CONST: res |= _F_CONST; break;
687 case _C_IN: res |= _F_IN; break;
688 case _C_INOUT: res |= _F_INOUT; break;
689 case _C_OUT: res |= _F_OUT; break;
690 case _C_BYCOPY: res |= _F_BYCOPY; break;
691 case _C_BYREF: res |= _F_BYREF; break;
692 case _C_ONEWAY: res |= _F_ONEWAY; break;
693 case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
701 /* The following three functions can be used to determine how a
702 structure is laid out by the compiler. For example:
704 struct objc_struct_layout layout;
707 objc_layout_structure (type, &layout);
708 while (objc_layout_structure_next_member (&layout))
713 objc_layout_structure_get_info (&layout, &position, &align, &type);
714 printf ("element %d has offset %d, alignment %d\n",
715 i++, position, align);
718 These functions are used by objc_sizeof_type and objc_alignof_type
719 functions to compute the size and alignment of structures. The
720 previous method of computing the size and alignment of a structure
721 was not working on some architectures, particulary on AIX, and in
722 the presence of bitfields inside the structure. */
724 objc_layout_structure (const char *type,
725 struct objc_struct_layout *layout)
729 if (*type != _C_UNION_B && *type != _C_STRUCT_B)
731 objc_error (nil, OBJC_ERR_BAD_TYPE,
732 "record (or union) type expected in objc_layout_structure, got %s\n",
737 layout->original_type = type;
739 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
741 while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
745 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
746 if (*(ntype - 1) == '=')
750 layout->prev_type = NULL;
751 layout->record_size = 0;
752 layout->record_align = BITS_PER_UNIT;
754 layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
759 objc_layout_structure_next_member (struct objc_struct_layout *layout)
761 register int desired_align = 0;
763 /* The following are used only if the field is a bitfield */
764 register const char *bfld_type = 0;
765 register int bfld_type_size, bfld_type_align = 0, bfld_field_size = 0;
767 /* The current type without the type qualifiers */
769 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
771 /* Add the size of the previous field to the size of the record. */
772 if (layout->prev_type)
774 type = objc_skip_type_qualifiers (layout->prev_type);
776 layout->record_size = MAX (layout->record_size,
777 objc_sizeof_type (type) * BITS_PER_UNIT);
779 else if (*type != _C_BFLD)
780 layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
782 /* Get the bitfield's type */
783 for (bfld_type = type + 1;
784 isdigit ((unsigned char)*bfld_type);
788 bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
789 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
790 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
791 layout->record_size += bfld_field_size;
795 if ((unionp && *layout->type == _C_UNION_E)
796 || (!unionp && *layout->type == _C_STRUCT_E))
799 /* Skip the variable name if any */
800 if (*layout->type == '"')
802 for (layout->type++; *layout->type++ != '"';)
806 type = objc_skip_type_qualifiers (layout->type);
808 if (*type != _C_BFLD)
809 desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
813 /* Skip the bitfield's offset */
814 for (bfld_type = type + 1;
815 isdigit ((unsigned char) *bfld_type);
819 bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
820 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
821 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
824 #ifdef BIGGEST_FIELD_ALIGNMENT
825 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
827 #ifdef ADJUST_FIELD_ALIGN
828 desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
831 /* Record must have at least as much alignment as any field.
832 Otherwise, the alignment of the field within the record
834 #ifndef PCC_BITFIELD_TYPE_MATTERS
835 layout->record_align = MAX (layout->record_align, desired_align);
836 #else /* PCC_BITFIELD_TYPE_MATTERS */
837 if (*type == _C_BFLD)
839 /* For these machines, a zero-length field does not
840 affect the alignment of the structure as a whole.
841 It does, however, affect the alignment of the next field
842 within the structure. */
844 layout->record_align = MAX (layout->record_align, desired_align);
846 desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
848 /* A named bit field of declared type `int'
849 forces the entire structure to have `int' alignment.
850 Q1: How is encoded this thing and how to check for it?
851 Q2: How to determine maximum_field_alignment at runtime? */
853 /* if (DECL_NAME (field) != 0) */
855 int type_align = bfld_type_align;
857 if (maximum_field_alignment != 0)
858 type_align = MIN (type_align, maximum_field_alignment);
859 else if (DECL_PACKED (field))
860 type_align = MIN (type_align, BITS_PER_UNIT);
863 layout->record_align = MAX (layout->record_align, type_align);
867 layout->record_align = MAX (layout->record_align, desired_align);
868 #endif /* PCC_BITFIELD_TYPE_MATTERS */
870 /* Does this field automatically have alignment it needs
871 by virtue of the fields that precede it and the record's
874 if (*type == _C_BFLD)
875 layout->record_size = atoi (type + 1);
876 else if (layout->record_size % desired_align != 0)
878 /* No, we need to skip space before this field.
879 Bump the cumulative size to multiple of field alignment. */
880 layout->record_size = ROUND (layout->record_size, desired_align);
883 /* Jump to the next field in record. */
885 layout->prev_type = layout->type;
886 layout->type = objc_skip_typespec (layout->type); /* skip component */
892 void objc_layout_finish_structure (struct objc_struct_layout *layout,
896 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
898 && ((!unionp && *layout->type == _C_STRUCT_E)
899 || (unionp && *layout->type == _C_UNION_E)))
901 /* Work out the alignment of the record as one expression and store
902 in the record type. Round it up to a multiple of the record's
905 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
906 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type,
908 layout->record_align);
910 layout->record_align = MAX (1, layout->record_align);
913 #ifdef ROUND_TYPE_SIZE
914 layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
916 layout->record_align);
918 /* Round the size up to be a multiple of the required alignment */
919 layout->record_size = ROUND (layout->record_size, layout->record_align);
925 *size = layout->record_size / BITS_PER_UNIT;
927 *align = layout->record_align / BITS_PER_UNIT;
931 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
932 unsigned int *offset,
937 *offset = layout->record_size / BITS_PER_UNIT;
939 *align = layout->record_align / BITS_PER_UNIT;
941 *type = layout->prev_type;