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 method_getNumberOfArguments (struct objc_method
*method
)
808 const char *type
= method
->method_types
;
811 type
= objc_skip_argspec (type
);
817 /* This could only happen if method_types is invalid; in
818 that case, return 0. */
823 /* Remove the return type. */
830 method_get_number_of_arguments (struct objc_method
*mth
)
832 return method_getNumberOfArguments (mth
);
836 Return the size of the argument block needed on the stack to invoke
837 the method MTH. This may be zero, if all arguments are passed in
842 method_get_sizeof_arguments (struct objc_method
*mth
)
844 const char *type
= objc_skip_typespec (mth
->method_types
);
849 Return a pointer to the next argument of ARGFRAME. type points to
850 the last argument. Typical use of this look like:
854 for (datum = method_get_first_argument (method, argframe, &type);
855 datum; datum = method_get_next_argument (argframe, &type))
857 unsigned flags = objc_get_type_qualifiers (type);
858 type = objc_skip_type_qualifiers (type);
860 [portal encodeData: datum ofType: type];
863 if ((flags & _F_IN) == _F_IN)
864 [portal encodeData: *(char **) datum ofType: ++type];
871 method_get_next_argument (arglist_t argframe
, const char **type
)
873 const char *t
= objc_skip_argspec (*type
);
879 t
= objc_skip_typespec (t
);
882 return argframe
->arg_regs
+ atoi (++t
);
884 return argframe
->arg_ptr
+ atoi (t
);
888 Return a pointer to the value of the first argument of the method
889 described in M with the given argumentframe ARGFRAME. The type
890 is returned in TYPE. type must be passed to successive calls of
891 method_get_next_argument.
894 method_get_first_argument (struct objc_method
*m
,
898 *type
= m
->method_types
;
899 return method_get_next_argument (argframe
, type
);
903 Return a pointer to the ARGth argument of the method
904 M from the frame ARGFRAME. The type of the argument
905 is returned in the value-result argument TYPE
909 method_get_nth_argument (struct objc_method
*m
,
910 arglist_t argframe
, int arg
,
913 const char *t
= objc_skip_argspec (m
->method_types
);
915 if (arg
> method_get_number_of_arguments (m
))
919 t
= objc_skip_argspec (t
);
922 t
= objc_skip_typespec (t
);
925 return argframe
->arg_regs
+ atoi (++t
);
927 return argframe
->arg_ptr
+ atoi (t
);
931 objc_get_type_qualifiers (const char *type
)
939 case _C_CONST
: res
|= _F_CONST
; break;
940 case _C_IN
: res
|= _F_IN
; break;
941 case _C_INOUT
: res
|= _F_INOUT
; break;
942 case _C_OUT
: res
|= _F_OUT
; break;
943 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
944 case _C_BYREF
: res
|= _F_BYREF
; break;
945 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
946 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
954 /* The following three functions can be used to determine how a
955 structure is laid out by the compiler. For example:
957 struct objc_struct_layout layout;
960 objc_layout_structure (type, &layout);
961 while (objc_layout_structure_next_member (&layout))
966 objc_layout_structure_get_info (&layout, &position, &align, &type);
967 printf ("element %d has offset %d, alignment %d\n",
968 i++, position, align);
971 These functions are used by objc_sizeof_type and objc_alignof_type
972 functions to compute the size and alignment of structures. The
973 previous method of computing the size and alignment of a structure
974 was not working on some architectures, particulary on AIX, and in
975 the presence of bitfields inside the structure. */
977 objc_layout_structure (const char *type
,
978 struct objc_struct_layout
*layout
)
982 if (*type
!= _C_UNION_B
&& *type
!= _C_STRUCT_B
)
984 _objc_abort ("record (or union) type expected in objc_layout_structure, got %s\n",
989 layout
->original_type
= type
;
991 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
993 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
997 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
998 if (*(ntype
- 1) == '=')
1001 layout
->type
= type
;
1002 layout
->prev_type
= NULL
;
1003 layout
->record_size
= 0;
1004 layout
->record_align
= BITS_PER_UNIT
;
1006 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
1011 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
1013 register int desired_align
= 0;
1015 /* The following are used only if the field is a bitfield */
1016 register const char *bfld_type
= 0;
1017 register int bfld_type_align
= 0, bfld_field_size
= 0;
1019 /* The current type without the type qualifiers */
1021 BOOL unionp
= layout
->original_type
[-1] == _C_UNION_B
;
1023 /* Add the size of the previous field to the size of the record. */
1024 if (layout
->prev_type
)
1026 type
= objc_skip_type_qualifiers (layout
->prev_type
);
1028 layout
->record_size
= MAX (layout
->record_size
,
1029 objc_sizeof_type (type
) * BITS_PER_UNIT
);
1031 else if (*type
!= _C_BFLD
)
1032 layout
->record_size
+= objc_sizeof_type (type
) * BITS_PER_UNIT
;
1034 /* Get the bitfield's type */
1035 for (bfld_type
= type
+ 1;
1036 isdigit ((unsigned char)*bfld_type
);
1040 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
1041 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
1042 layout
->record_size
+= bfld_field_size
;
1046 if ((unionp
&& *layout
->type
== _C_UNION_E
)
1047 || (!unionp
&& *layout
->type
== _C_STRUCT_E
))
1050 /* Skip the variable name if any */
1051 if (*layout
->type
== '"')
1053 for (layout
->type
++; *layout
->type
++ != '"';)
1057 type
= objc_skip_type_qualifiers (layout
->type
);
1059 if (*type
!= _C_BFLD
)
1060 desired_align
= objc_alignof_type (type
) * BITS_PER_UNIT
;
1064 /* Skip the bitfield's offset */
1065 for (bfld_type
= type
+ 1;
1066 isdigit ((unsigned char) *bfld_type
);
1070 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
1071 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
1074 /* The following won't work for vectors. */
1075 #ifdef BIGGEST_FIELD_ALIGNMENT
1076 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
1078 #ifdef ADJUST_FIELD_ALIGN
1079 desired_align
= ADJUST_FIELD_ALIGN (type
, desired_align
);
1082 /* Record must have at least as much alignment as any field.
1083 Otherwise, the alignment of the field within the record
1085 #ifndef PCC_BITFIELD_TYPE_MATTERS
1086 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
1087 #else /* PCC_BITFIELD_TYPE_MATTERS */
1088 if (*type
== _C_BFLD
)
1090 /* For these machines, a zero-length field does not
1091 affect the alignment of the structure as a whole.
1092 It does, however, affect the alignment of the next field
1093 within the structure. */
1094 if (bfld_field_size
)
1095 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
1097 desired_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
1099 /* A named bit field of declared type `int'
1100 forces the entire structure to have `int' alignment.
1101 Q1: How is encoded this thing and how to check for it?
1102 Q2: How to determine maximum_field_alignment at runtime? */
1104 /* if (DECL_NAME (field) != 0) */
1106 int type_align
= bfld_type_align
;
1108 if (maximum_field_alignment
!= 0)
1109 type_align
= MIN (type_align
, maximum_field_alignment
);
1110 else if (DECL_PACKED (field
))
1111 type_align
= MIN (type_align
, BITS_PER_UNIT
);
1114 layout
->record_align
= MAX (layout
->record_align
, type_align
);
1118 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
1119 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1121 /* Does this field automatically have alignment it needs
1122 by virtue of the fields that precede it and the record's
1125 if (*type
== _C_BFLD
)
1126 layout
->record_size
= atoi (type
+ 1);
1127 else if (layout
->record_size
% desired_align
!= 0)
1129 /* No, we need to skip space before this field.
1130 Bump the cumulative size to multiple of field alignment. */
1131 layout
->record_size
= ROUND (layout
->record_size
, desired_align
);
1134 /* Jump to the next field in record. */
1136 layout
->prev_type
= layout
->type
;
1137 layout
->type
= objc_skip_typespec (layout
->type
); /* skip component */
1143 void objc_layout_finish_structure (struct objc_struct_layout
*layout
,
1145 unsigned int *align
)
1147 BOOL unionp
= layout
->original_type
[-1] == _C_UNION_B
;
1149 && ((!unionp
&& *layout
->type
== _C_STRUCT_E
)
1150 || (unionp
&& *layout
->type
== _C_UNION_E
)))
1152 /* Work out the alignment of the record as one expression and store
1153 in the record type. Round it up to a multiple of the record's
1155 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1156 layout
->record_align
= ROUND_TYPE_ALIGN (layout
->original_type
-1,
1158 layout
->record_align
);
1160 layout
->record_align
= MAX (1, layout
->record_align
);
1163 #ifdef ROUND_TYPE_SIZE
1164 layout
->record_size
= ROUND_TYPE_SIZE (layout
->original_type
,
1165 layout
->record_size
,
1166 layout
->record_align
);
1168 /* Round the size up to be a multiple of the required alignment */
1169 layout
->record_size
= ROUND (layout
->record_size
, layout
->record_align
);
1172 layout
->type
= NULL
;
1175 *size
= layout
->record_size
/ BITS_PER_UNIT
;
1177 *align
= layout
->record_align
/ BITS_PER_UNIT
;
1181 void objc_layout_structure_get_info (struct objc_struct_layout
*layout
,
1182 unsigned int *offset
,
1183 unsigned int *align
,
1187 *offset
= layout
->record_size
/ BITS_PER_UNIT
;
1189 *align
= layout
->record_align
/ BITS_PER_UNIT
;
1191 *type
= layout
->prev_type
;