1 /* Encoding of types for Objective C.
2 Copyright (C) 1993, 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2009
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 3, 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 Under Section 7 of GPL version 3, you are granted additional
20 permissions described in the GCC Runtime Library Exception, version
21 3.1, as published by the Free Software Foundation.
23 You should have received a copy of the GNU General Public License and
24 a copy of the GCC Runtime Library Exception along with this program;
25 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
26 <http://www.gnu.org/licenses/>. */
28 /* FIXME: This file has no business including tm.h. */
30 /* FIXME: This file contains functions that will abort the entire
31 program if they fail. Is that really needed ?
34 #include "objc-private/common.h"
35 #include "objc-private/error.h"
37 #include "coretypes.h"
39 #include "objc/runtime.h"
40 #include "objc-private/module-abi-8.h" /* For struct objc_method */
46 ({ typeof (X) __x = (X), __y = (Y); \
47 (__x > __y ? __x : __y); })
51 ({ typeof (X) __x = (X), __y = (Y); \
52 (__x < __y ? __x : __y); })
56 ({ typeof (V) __v = (V); typeof (A) __a = (A); \
57 __a * ((__v+__a - 1)/__a); })
60 /* Various hacks for objc_layout_record. These are used by the target
63 #define TREE_CODE(TYPE) *(TYPE)
64 #define TREE_TYPE(TREE) (TREE)
66 #define RECORD_TYPE _C_STRUCT_B
67 #define UNION_TYPE _C_UNION_B
68 #define QUAL_UNION_TYPE _C_UNION_B
69 #define ARRAY_TYPE _C_ARY_B
71 #define REAL_TYPE _C_DBL
73 #define VECTOR_TYPE _C_VECTOR
75 #define TYPE_FIELDS(TYPE) ({const char *_field = (TYPE)+1; \
76 while (*_field != _C_STRUCT_E && *_field != _C_STRUCT_B \
77 && *_field != _C_UNION_B && *_field++ != '=') \
81 #define DECL_MODE(TYPE) *(TYPE)
82 #define TYPE_MODE(TYPE) *(TYPE)
86 #define strip_array_types(TYPE) ({const char *_field = (TYPE); \
87 while (*_field == _C_ARY_B)\
89 while (isdigit ((unsigned char)*++_field))\
94 /* Some ports (eg ARM) allow the structure size boundary to be
95 selected at compile-time. We override the normal definition with
96 one that has a constant value for this compilation. */
98 #define BITS_PER_UNIT 8
100 #undef STRUCTURE_SIZE_BOUNDARY
101 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
103 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
104 target_flags. Define a dummy entry here to so we don't die.
105 We have to rename it because target_flags may already have been
107 #define target_flags not_target_flags
108 static int __attribute__ ((__unused__)) not_target_flags = 0;
110 /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
111 Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */
112 #undef ALTIVEC_VECTOR_MODE
113 #define ALTIVEC_VECTOR_MODE(MODE) (0)
115 /* Furthermore, some (powerpc) targets also use TARGET_ALIGN_NATURAL
116 in their alignment macros. Currently[4.5/6], rs6000.h points this
117 to a static variable, initialized by target overrides. This is reset
118 in linux64.h but not in darwin64.h. The macro is not used by *86*. */
120 #if __MACH__ && __LP64__
121 # undef TARGET_ALIGN_NATURAL
122 # define TARGET_ALIGN_NATURAL 1
125 /* FIXME: while this file has no business including tm.h, this
126 definitely has no business defining this macro but it
127 is only way around without really rewritting this file,
128 should look after the branch of 3.4 to fix this.
129 FIXME1: It's also out of date, darwin no longer has the same alignment
130 'special' as aix - this is probably the origin of the m32 breakage. */
131 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
132 ({ const char *_fields = TYPE_FIELDS (STRUCT); \
134 && TYPE_MODE (strip_array_types (TREE_TYPE (_fields))) == DFmode) \
135 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
136 : MAX (COMPUTED, SPECIFIED));})
137 /* FIXME: The word 'fixme' is insufficient to explain the wrong-ness
138 of this next macro definition. */
139 #define darwin_rs6000_special_round_type_align(S,C,S2) \
140 rs6000_special_round_type_align(S,C,S2)
143 return the size of an object specified by type
147 objc_sizeof_type (const char *type)
149 /* Skip the variable name if any */
152 /* FIXME: How do we know we won't read beyond the end of the
153 string. Here and in the rest of the file! */
154 for (type++; *type++ != '"';)
160 return sizeof (_Bool);
168 return sizeof (Class);
176 return sizeof (char);
180 return sizeof (unsigned char);
184 return sizeof (short);
188 return sizeof (unsigned short);
196 return sizeof (unsigned int);
200 return sizeof (long);
204 return sizeof (unsigned long);
208 return sizeof (long long);
212 return sizeof (unsigned long long);
216 return sizeof (float);
220 return sizeof (double);
224 return sizeof (long double);
228 return sizeof (void);
234 return sizeof (char *);
239 int len = atoi (type + 1);
240 while (isdigit ((unsigned char)*++type))
242 return len * objc_aligned_size (type);
253 /* The size in bytes is the following number. */
254 int size = atoi (type);
261 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
263 int startByte, endByte;
265 position = atoi (type + 1);
266 while (isdigit ((unsigned char)*++type))
268 size = atoi (type + 1);
270 startByte = position / BITS_PER_UNIT;
271 endByte = (position + size) / BITS_PER_UNIT;
272 return endByte - startByte;
278 struct objc_struct_layout layout;
281 objc_layout_structure (type, &layout);
282 while (objc_layout_structure_next_member (&layout))
284 objc_layout_finish_structure (&layout, &size, NULL);
291 type++; /* Skip after the 'j'. */
295 return sizeof (_Complex char);
299 return sizeof (_Complex unsigned char);
303 return sizeof (_Complex short);
307 return sizeof (_Complex unsigned short);
311 return sizeof (_Complex int);
315 return sizeof (_Complex unsigned int);
319 return sizeof (_Complex long);
323 return sizeof (_Complex unsigned long);
327 return sizeof (_Complex long long);
331 return sizeof (_Complex unsigned long long);
335 return sizeof (_Complex float);
339 return sizeof (_Complex double);
343 return sizeof (_Complex long double);
348 /* FIXME: Is this so bad that we have to abort the
349 entire program ? (it applies to all the other
350 _objc_abort calls in this file).
352 _objc_abort ("unknown complex type %s\n", type);
360 _objc_abort ("unknown type %s\n", type);
368 Return the alignment of an object specified by type
372 objc_alignof_type (const char *type)
374 /* Skip the variable name if any */
377 for (type++; *type++ != '"';)
382 return __alignof__ (_Bool);
386 return __alignof__ (id);
390 return __alignof__ (Class);
394 return __alignof__ (SEL);
398 return __alignof__ (char);
402 return __alignof__ (unsigned char);
406 return __alignof__ (short);
410 return __alignof__ (unsigned short);
414 return __alignof__ (int);
418 return __alignof__ (unsigned int);
422 return __alignof__ (long);
426 return __alignof__ (unsigned long);
430 return __alignof__ (long long);
434 return __alignof__ (unsigned long long);
438 return __alignof__ (float);
442 return __alignof__ (double);
446 return __alignof__ (long double);
452 return __alignof__ (char *);
456 while (isdigit ((unsigned char)*++type))
458 return objc_alignof_type (type);
468 while (isdigit ((unsigned char)*type))
474 /* The alignment in bytes is the following number. */
480 struct objc_struct_layout layout;
483 objc_layout_structure (type, &layout);
484 while (objc_layout_structure_next_member (&layout))
486 objc_layout_finish_structure (&layout, NULL, &align);
494 type++; /* Skip after the 'j'. */
498 return __alignof__ (_Complex char);
502 return __alignof__ (_Complex unsigned char);
506 return __alignof__ (_Complex short);
510 return __alignof__ (_Complex unsigned short);
514 return __alignof__ (_Complex int);
518 return __alignof__ (_Complex unsigned int);
522 return __alignof__ (_Complex long);
526 return __alignof__ (_Complex unsigned long);
530 return __alignof__ (_Complex long long);
534 return __alignof__ (_Complex unsigned long long);
538 return __alignof__ (_Complex float);
542 return __alignof__ (_Complex double);
546 return __alignof__ (_Complex long double);
551 _objc_abort ("unknown complex type %s\n", type);
559 _objc_abort ("unknown type %s\n", type);
566 The aligned size if the size rounded up to the nearest alignment.
570 objc_aligned_size (const char *type)
574 /* Skip the variable name */
577 for (type++; *type++ != '"';)
581 size = objc_sizeof_type (type);
582 align = objc_alignof_type (type);
584 return ROUND (size, align);
588 The size rounded up to the nearest integral of the wordsize, taken
589 to be the size of a void *.
593 objc_promoted_size (const char *type)
597 /* Skip the variable name */
600 for (type++; *type++ != '"';)
604 size = objc_sizeof_type (type);
605 wordsize = sizeof (void *);
607 return ROUND (size, wordsize);
611 Skip type qualifiers. These may eventually precede typespecs
612 occurring in method prototype encodings.
616 objc_skip_type_qualifiers (const char *type)
618 while (*type == _C_CONST
622 || *type == _C_BYCOPY
624 || *type == _C_ONEWAY
625 || *type == _C_GCINVISIBLE)
634 Skip one typespec element. If the typespec is prepended by type
635 qualifiers, these are skipped as well.
639 objc_skip_typespec (const char *type)
641 /* Skip the variable name if any */
644 for (type++; *type++ != '"';)
648 type = objc_skip_type_qualifiers (type);
653 /* An id may be annotated by the actual type if it is known
654 with the @"ClassName" syntax */
660 while (*++type != '"')
665 /* The following are one character type codes */
694 /* skip digits, typespec and closing ']' */
695 while (isdigit ((unsigned char)*++type))
697 type = objc_skip_typespec (type);
698 if (*type == _C_ARY_E)
702 _objc_abort ("bad array type %s\n", type);
711 /* Skip digits (size) */
712 while (isdigit ((unsigned char)*type))
716 /* Skip digits (alignment) */
717 while (isdigit ((unsigned char)*type))
720 type = objc_skip_typespec (type);
721 /* Skip closing ']'. */
722 if (*type == _C_ARY_E)
726 _objc_abort ("bad vector type %s\n", type);
731 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
732 while (isdigit ((unsigned char)*++type))
733 ; /* skip position */
734 while (isdigit ((unsigned char)*++type))
735 ; /* skip type and size */
739 /* skip name, and elements until closing '}' */
741 while (*type != _C_STRUCT_E && *type++ != '=')
743 while (*type != _C_STRUCT_E)
745 type = objc_skip_typespec (type);
750 /* skip name, and elements until closing ')' */
752 while (*type != _C_UNION_E && *type++ != '=')
754 while (*type != _C_UNION_E)
756 type = objc_skip_typespec (type);
761 /* Just skip the following typespec */
763 return objc_skip_typespec (++type);
767 _objc_abort ("unknown type %s\n", type);
774 Skip an offset as part of a method encoding. This is prepended by a
775 '+' if the argument is passed in registers.
777 FIXME: The compiler never generates '+'.
780 objc_skip_offset (const char *type)
784 while (isdigit ((unsigned char) *++type))
790 Skip an argument specification of a method encoding.
793 objc_skip_argspec (const char *type)
795 type = objc_skip_typespec (type);
796 type = objc_skip_offset (type);
801 Return the number of arguments that the method MTH expects.
802 Note that all methods need two implicit arguments `self' and
806 method_get_number_of_arguments (struct objc_method *mth)
809 const char *type = mth->method_types;
812 type = objc_skip_argspec (type);
819 Return the size of the argument block needed on the stack to invoke
820 the method MTH. This may be zero, if all arguments are passed in
825 method_get_sizeof_arguments (struct objc_method *mth)
827 const char *type = objc_skip_typespec (mth->method_types);
832 Return a pointer to the next argument of ARGFRAME. type points to
833 the last argument. Typical use of this look like:
837 for (datum = method_get_first_argument (method, argframe, &type);
838 datum; datum = method_get_next_argument (argframe, &type))
840 unsigned flags = objc_get_type_qualifiers (type);
841 type = objc_skip_type_qualifiers (type);
843 [portal encodeData: datum ofType: type];
846 if ((flags & _F_IN) == _F_IN)
847 [portal encodeData: *(char **) datum ofType: ++type];
854 method_get_next_argument (arglist_t argframe, const char **type)
856 const char *t = objc_skip_argspec (*type);
862 t = objc_skip_typespec (t);
865 return argframe->arg_regs + atoi (++t);
867 return argframe->arg_ptr + atoi (t);
871 Return a pointer to the value of the first argument of the method
872 described in M with the given argumentframe ARGFRAME. The type
873 is returned in TYPE. type must be passed to successive calls of
874 method_get_next_argument.
877 method_get_first_argument (struct objc_method *m,
881 *type = m->method_types;
882 return method_get_next_argument (argframe, type);
886 Return a pointer to the ARGth argument of the method
887 M from the frame ARGFRAME. The type of the argument
888 is returned in the value-result argument TYPE
892 method_get_nth_argument (struct objc_method *m,
893 arglist_t argframe, int arg,
896 const char *t = objc_skip_argspec (m->method_types);
898 if (arg > method_get_number_of_arguments (m))
902 t = objc_skip_argspec (t);
905 t = objc_skip_typespec (t);
908 return argframe->arg_regs + atoi (++t);
910 return argframe->arg_ptr + atoi (t);
914 objc_get_type_qualifiers (const char *type)
922 case _C_CONST: res |= _F_CONST; break;
923 case _C_IN: res |= _F_IN; break;
924 case _C_INOUT: res |= _F_INOUT; break;
925 case _C_OUT: res |= _F_OUT; break;
926 case _C_BYCOPY: res |= _F_BYCOPY; break;
927 case _C_BYREF: res |= _F_BYREF; break;
928 case _C_ONEWAY: res |= _F_ONEWAY; break;
929 case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
937 /* The following three functions can be used to determine how a
938 structure is laid out by the compiler. For example:
940 struct objc_struct_layout layout;
943 objc_layout_structure (type, &layout);
944 while (objc_layout_structure_next_member (&layout))
949 objc_layout_structure_get_info (&layout, &position, &align, &type);
950 printf ("element %d has offset %d, alignment %d\n",
951 i++, position, align);
954 These functions are used by objc_sizeof_type and objc_alignof_type
955 functions to compute the size and alignment of structures. The
956 previous method of computing the size and alignment of a structure
957 was not working on some architectures, particulary on AIX, and in
958 the presence of bitfields inside the structure. */
960 objc_layout_structure (const char *type,
961 struct objc_struct_layout *layout)
965 if (*type != _C_UNION_B && *type != _C_STRUCT_B)
967 _objc_abort ("record (or union) type expected in objc_layout_structure, got %s\n",
972 layout->original_type = type;
974 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
976 while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
980 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
981 if (*(ntype - 1) == '=')
985 layout->prev_type = NULL;
986 layout->record_size = 0;
987 layout->record_align = BITS_PER_UNIT;
989 layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
994 objc_layout_structure_next_member (struct objc_struct_layout *layout)
996 register int desired_align = 0;
998 /* The following are used only if the field is a bitfield */
999 register const char *bfld_type = 0;
1000 register int bfld_type_align = 0, bfld_field_size = 0;
1002 /* The current type without the type qualifiers */
1004 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1006 /* Add the size of the previous field to the size of the record. */
1007 if (layout->prev_type)
1009 type = objc_skip_type_qualifiers (layout->prev_type);
1011 layout->record_size = MAX (layout->record_size,
1012 objc_sizeof_type (type) * BITS_PER_UNIT);
1014 else if (*type != _C_BFLD)
1015 layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
1017 /* Get the bitfield's type */
1018 for (bfld_type = type + 1;
1019 isdigit ((unsigned char)*bfld_type);
1023 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1024 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1025 layout->record_size += bfld_field_size;
1029 if ((unionp && *layout->type == _C_UNION_E)
1030 || (!unionp && *layout->type == _C_STRUCT_E))
1033 /* Skip the variable name if any */
1034 if (*layout->type == '"')
1036 for (layout->type++; *layout->type++ != '"';)
1040 type = objc_skip_type_qualifiers (layout->type);
1042 if (*type != _C_BFLD)
1043 desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
1047 /* Skip the bitfield's offset */
1048 for (bfld_type = type + 1;
1049 isdigit ((unsigned char) *bfld_type);
1053 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1054 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
1057 /* The following won't work for vectors. */
1058 #ifdef BIGGEST_FIELD_ALIGNMENT
1059 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
1061 #ifdef ADJUST_FIELD_ALIGN
1062 desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
1065 /* Record must have at least as much alignment as any field.
1066 Otherwise, the alignment of the field within the record
1068 #ifndef PCC_BITFIELD_TYPE_MATTERS
1069 layout->record_align = MAX (layout->record_align, desired_align);
1070 #else /* PCC_BITFIELD_TYPE_MATTERS */
1071 if (*type == _C_BFLD)
1073 /* For these machines, a zero-length field does not
1074 affect the alignment of the structure as a whole.
1075 It does, however, affect the alignment of the next field
1076 within the structure. */
1077 if (bfld_field_size)
1078 layout->record_align = MAX (layout->record_align, desired_align);
1080 desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
1082 /* A named bit field of declared type `int'
1083 forces the entire structure to have `int' alignment.
1084 Q1: How is encoded this thing and how to check for it?
1085 Q2: How to determine maximum_field_alignment at runtime? */
1087 /* if (DECL_NAME (field) != 0) */
1089 int type_align = bfld_type_align;
1091 if (maximum_field_alignment != 0)
1092 type_align = MIN (type_align, maximum_field_alignment);
1093 else if (DECL_PACKED (field))
1094 type_align = MIN (type_align, BITS_PER_UNIT);
1097 layout->record_align = MAX (layout->record_align, type_align);
1101 layout->record_align = MAX (layout->record_align, desired_align);
1102 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1104 /* Does this field automatically have alignment it needs
1105 by virtue of the fields that precede it and the record's
1108 if (*type == _C_BFLD)
1109 layout->record_size = atoi (type + 1);
1110 else if (layout->record_size % desired_align != 0)
1112 /* No, we need to skip space before this field.
1113 Bump the cumulative size to multiple of field alignment. */
1114 layout->record_size = ROUND (layout->record_size, desired_align);
1117 /* Jump to the next field in record. */
1119 layout->prev_type = layout->type;
1120 layout->type = objc_skip_typespec (layout->type); /* skip component */
1126 void objc_layout_finish_structure (struct objc_struct_layout *layout,
1128 unsigned int *align)
1130 BOOL unionp = layout->original_type[-1] == _C_UNION_B;
1132 && ((!unionp && *layout->type == _C_STRUCT_E)
1133 || (unionp && *layout->type == _C_UNION_E)))
1135 /* Work out the alignment of the record as one expression and store
1136 in the record type. Round it up to a multiple of the record's
1138 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1139 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type-1,
1141 layout->record_align);
1143 layout->record_align = MAX (1, layout->record_align);
1146 #ifdef ROUND_TYPE_SIZE
1147 layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
1148 layout->record_size,
1149 layout->record_align);
1151 /* Round the size up to be a multiple of the required alignment */
1152 layout->record_size = ROUND (layout->record_size, layout->record_align);
1155 layout->type = NULL;
1158 *size = layout->record_size / BITS_PER_UNIT;
1160 *align = layout->record_align / BITS_PER_UNIT;
1164 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
1165 unsigned int *offset,
1166 unsigned int *align,
1170 *offset = layout->record_size / BITS_PER_UNIT;
1172 *align = layout->record_align / BITS_PER_UNIT;
1174 *type = layout->prev_type;