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. */
31 #include "coretypes.h"
33 #include "objc/objc-api.h"
34 #include "objc/encoding.h"
39 ({ typeof (X) __x = (X), __y = (Y); \
40 (__x > __y ? __x : __y); })
44 ({ typeof (X) __x = (X), __y = (Y); \
45 (__x < __y ? __x : __y); })
49 ({ typeof (V) __v = (V); typeof (A) __a = (A); \
50 __a * ((__v+__a - 1)/__a); })
53 /* Various hacks for objc_layout_record. These are used by the target
56 #define TREE_CODE(TYPE) *(TYPE)
57 #define TREE_TYPE(TREE) (TREE)
59 #define RECORD_TYPE _C_STRUCT_B
60 #define UNION_TYPE _C_UNION_B
61 #define QUAL_UNION_TYPE _C_UNION_B
62 #define ARRAY_TYPE _C_ARY_B
64 #define REAL_TYPE _C_DBL
66 #define VECTOR_TYPE _C_VECTOR
68 #define TYPE_FIELDS(TYPE) ({const char *_field = (TYPE)+1; \
69 while (*_field != _C_STRUCT_E && *_field != _C_STRUCT_B \
70 && *_field != _C_UNION_B && *_field++ != '=') \
74 #define DECL_MODE(TYPE) *(TYPE)
75 #define TYPE_MODE(TYPE) *(TYPE)
79 #define strip_array_types(TYPE) ({const char *_field = (TYPE); \
80 while (*_field == _C_ARY_B)\
82 while (isdigit ((unsigned char)*++_field))\
87 /* Some ports (eg ARM) allow the structure size boundary to be
88 selected at compile-time. We override the normal definition with
89 one that has a constant value for this compilation. */
91 #define BITS_PER_UNIT 8
93 #undef STRUCTURE_SIZE_BOUNDARY
94 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
96 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
97 target_flags. Define a dummy entry here to so we don't die.
98 We have to rename it because target_flags may already have been
100 #define target_flags not_target_flags
101 static int __attribute__ ((__unused__
)) not_target_flags
= 0;
103 /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
104 Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */
105 #undef ALTIVEC_VECTOR_MODE
106 #define ALTIVEC_VECTOR_MODE(MODE) (0)
109 /* FIXME: while this file has no business including tm.h, this
110 definitely has no business defining this macro but it
111 is only way around without really rewritting this file,
112 should look after the branch of 3.4 to fix this. */
113 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
114 ({ const char *_fields = TYPE_FIELDS (STRUCT); \
116 && TYPE_MODE (strip_array_types (TREE_TYPE (_fields))) == DFmode) \
117 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
118 : MAX (COMPUTED, SPECIFIED));})
119 /* FIXME: The word 'fixme' is insufficient to explain the wrong-ness
120 of this next macro definition. */
121 #define darwin_rs6000_special_round_type_align(S,C,S2) \
122 rs6000_special_round_type_align(S,C,S2)
125 return the size of an object specified by type
129 objc_sizeof_type (const char *type
)
131 /* Skip the variable name if any */
134 for (type
++; *type
++ != '"';)
140 return sizeof (_Bool
);
148 return sizeof (Class
);
156 return sizeof (char);
160 return sizeof (unsigned char);
164 return sizeof (short);
168 return sizeof (unsigned short);
176 return sizeof (unsigned int);
180 return sizeof (long);
184 return sizeof (unsigned long);
188 return sizeof (long long);
192 return sizeof (unsigned long long);
196 return sizeof (float);
200 return sizeof (double);
204 return sizeof (void);
210 return sizeof (char *);
215 int len
= atoi (type
+ 1);
216 while (isdigit ((unsigned char)*++type
))
218 return len
* objc_aligned_size (type
);
224 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
226 int startByte
, endByte
;
228 position
= atoi (type
+ 1);
229 while (isdigit ((unsigned char)*++type
))
231 size
= atoi (type
+ 1);
233 startByte
= position
/ BITS_PER_UNIT
;
234 endByte
= (position
+ size
) / BITS_PER_UNIT
;
235 return endByte
- startByte
;
241 struct objc_struct_layout layout
;
244 objc_layout_structure (type
, &layout
);
245 while (objc_layout_structure_next_member (&layout
))
247 objc_layout_finish_structure (&layout
, &size
, NULL
);
254 type
++; /* Skip after the 'j'. */
258 return sizeof (_Complex
char);
262 return sizeof (_Complex
unsigned char);
266 return sizeof (_Complex
short);
270 return sizeof (_Complex
unsigned short);
274 return sizeof (_Complex
int);
278 return sizeof (_Complex
unsigned int);
282 return sizeof (_Complex
long);
286 return sizeof (_Complex
unsigned long);
290 return sizeof (_Complex
long long);
294 return sizeof (_Complex
unsigned long long);
298 return sizeof (_Complex
float);
302 return sizeof (_Complex
double);
307 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown complex type %s\n",
316 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
324 Return the alignment of an object specified by type
328 objc_alignof_type (const char *type
)
330 /* Skip the variable name if any */
333 for (type
++; *type
++ != '"';)
338 return __alignof__ (_Bool
);
342 return __alignof__ (id
);
346 return __alignof__ (Class
);
350 return __alignof__ (SEL
);
354 return __alignof__ (char);
358 return __alignof__ (unsigned char);
362 return __alignof__ (short);
366 return __alignof__ (unsigned short);
370 return __alignof__ (int);
374 return __alignof__ (unsigned int);
378 return __alignof__ (long);
382 return __alignof__ (unsigned long);
386 return __alignof__ (long long);
390 return __alignof__ (unsigned long long);
394 return __alignof__ (float);
398 return __alignof__ (double);
404 return __alignof__ (char *);
408 while (isdigit ((unsigned char)*++type
))
410 return objc_alignof_type (type
);
415 struct objc_struct_layout layout
;
418 objc_layout_structure (type
, &layout
);
419 while (objc_layout_structure_next_member (&layout
))
421 objc_layout_finish_structure (&layout
, NULL
, &align
);
429 type
++; /* Skip after the 'j'. */
433 return __alignof__ (_Complex
char);
437 return __alignof__ (_Complex
unsigned char);
441 return __alignof__ (_Complex
short);
445 return __alignof__ (_Complex
unsigned short);
449 return __alignof__ (_Complex
int);
453 return __alignof__ (_Complex
unsigned int);
457 return __alignof__ (_Complex
long);
461 return __alignof__ (_Complex
unsigned long);
465 return __alignof__ (_Complex
long long);
469 return __alignof__ (_Complex
unsigned long long);
473 return __alignof__ (_Complex
float);
477 return __alignof__ (_Complex
double);
482 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown complex type %s\n",
491 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
498 The aligned size if the size rounded up to the nearest alignment.
502 objc_aligned_size (const char *type
)
506 /* Skip the variable name */
509 for (type
++; *type
++ != '"';)
513 size
= objc_sizeof_type (type
);
514 align
= objc_alignof_type (type
);
516 return ROUND (size
, align
);
520 The size rounded up to the nearest integral of the wordsize, taken
521 to be the size of a void *.
525 objc_promoted_size (const char *type
)
529 /* Skip the variable name */
532 for (type
++; *type
++ != '"';)
536 size
= objc_sizeof_type (type
);
537 wordsize
= sizeof (void *);
539 return ROUND (size
, wordsize
);
543 Skip type qualifiers. These may eventually precede typespecs
544 occurring in method prototype encodings.
548 objc_skip_type_qualifiers (const char *type
)
550 while (*type
== _C_CONST
554 || *type
== _C_BYCOPY
556 || *type
== _C_ONEWAY
557 || *type
== _C_GCINVISIBLE
)
566 Skip one typespec element. If the typespec is prepended by type
567 qualifiers, these are skipped as well.
571 objc_skip_typespec (const char *type
)
573 /* Skip the variable name if any */
576 for (type
++; *type
++ != '"';)
580 type
= objc_skip_type_qualifiers (type
);
585 /* An id may be annotated by the actual type if it is known
586 with the @"ClassName" syntax */
592 while (*++type
!= '"')
597 /* The following are one character type codes */
625 /* skip digits, typespec and closing ']' */
627 while (isdigit ((unsigned char)*++type
))
629 type
= objc_skip_typespec (type
);
630 if (*type
== _C_ARY_E
)
634 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "bad array type %s\n", type
);
639 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
640 while (isdigit ((unsigned char)*++type
))
641 ; /* skip position */
642 while (isdigit ((unsigned char)*++type
))
643 ; /* skip type and size */
647 /* skip name, and elements until closing '}' */
649 while (*type
!= _C_STRUCT_E
&& *type
++ != '=')
651 while (*type
!= _C_STRUCT_E
)
653 type
= objc_skip_typespec (type
);
658 /* skip name, and elements until closing ')' */
660 while (*type
!= _C_UNION_E
&& *type
++ != '=')
662 while (*type
!= _C_UNION_E
)
664 type
= objc_skip_typespec (type
);
669 /* Just skip the following typespec */
671 return objc_skip_typespec (++type
);
675 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
682 Skip an offset as part of a method encoding. This is prepended by a
683 '+' if the argument is passed in registers.
686 objc_skip_offset (const char *type
)
690 while (isdigit ((unsigned char) *++type
))
696 Skip an argument specification of a method encoding.
699 objc_skip_argspec (const char *type
)
701 type
= objc_skip_typespec (type
);
702 type
= objc_skip_offset (type
);
707 Return the number of arguments that the method MTH expects.
708 Note that all methods need two implicit arguments `self' and
712 method_get_number_of_arguments (struct objc_method
*mth
)
715 const char *type
= mth
->method_types
;
718 type
= objc_skip_argspec (type
);
725 Return the size of the argument block needed on the stack to invoke
726 the method MTH. This may be zero, if all arguments are passed in
731 method_get_sizeof_arguments (struct objc_method
*mth
)
733 const char *type
= objc_skip_typespec (mth
->method_types
);
738 Return a pointer to the next argument of ARGFRAME. type points to
739 the last argument. Typical use of this look like:
743 for (datum = method_get_first_argument (method, argframe, &type);
744 datum; datum = method_get_next_argument (argframe, &type))
746 unsigned flags = objc_get_type_qualifiers (type);
747 type = objc_skip_type_qualifiers (type);
749 [portal encodeData: datum ofType: type];
752 if ((flags & _F_IN) == _F_IN)
753 [portal encodeData: *(char **) datum ofType: ++type];
760 method_get_next_argument (arglist_t argframe
, const char **type
)
762 const char *t
= objc_skip_argspec (*type
);
768 t
= objc_skip_typespec (t
);
771 return argframe
->arg_regs
+ atoi (++t
);
773 return argframe
->arg_ptr
+ atoi (t
);
777 Return a pointer to the value of the first argument of the method
778 described in M with the given argumentframe ARGFRAME. The type
779 is returned in TYPE. type must be passed to successive calls of
780 method_get_next_argument.
783 method_get_first_argument (struct objc_method
*m
,
787 *type
= m
->method_types
;
788 return method_get_next_argument (argframe
, type
);
792 Return a pointer to the ARGth argument of the method
793 M from the frame ARGFRAME. The type of the argument
794 is returned in the value-result argument TYPE
798 method_get_nth_argument (struct objc_method
*m
,
799 arglist_t argframe
, int arg
,
802 const char *t
= objc_skip_argspec (m
->method_types
);
804 if (arg
> method_get_number_of_arguments (m
))
808 t
= objc_skip_argspec (t
);
811 t
= objc_skip_typespec (t
);
814 return argframe
->arg_regs
+ atoi (++t
);
816 return argframe
->arg_ptr
+ atoi (t
);
820 objc_get_type_qualifiers (const char *type
)
828 case _C_CONST
: res
|= _F_CONST
; break;
829 case _C_IN
: res
|= _F_IN
; break;
830 case _C_INOUT
: res
|= _F_INOUT
; break;
831 case _C_OUT
: res
|= _F_OUT
; break;
832 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
833 case _C_BYREF
: res
|= _F_BYREF
; break;
834 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
835 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
843 /* The following three functions can be used to determine how a
844 structure is laid out by the compiler. For example:
846 struct objc_struct_layout layout;
849 objc_layout_structure (type, &layout);
850 while (objc_layout_structure_next_member (&layout))
855 objc_layout_structure_get_info (&layout, &position, &align, &type);
856 printf ("element %d has offset %d, alignment %d\n",
857 i++, position, align);
860 These functions are used by objc_sizeof_type and objc_alignof_type
861 functions to compute the size and alignment of structures. The
862 previous method of computing the size and alignment of a structure
863 was not working on some architectures, particulary on AIX, and in
864 the presence of bitfields inside the structure. */
866 objc_layout_structure (const char *type
,
867 struct objc_struct_layout
*layout
)
871 if (*type
!= _C_UNION_B
&& *type
!= _C_STRUCT_B
)
873 objc_error (nil
, OBJC_ERR_BAD_TYPE
,
874 "record (or union) type expected in objc_layout_structure, got %s\n",
879 layout
->original_type
= type
;
881 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
883 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
887 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
888 if (*(ntype
- 1) == '=')
892 layout
->prev_type
= NULL
;
893 layout
->record_size
= 0;
894 layout
->record_align
= BITS_PER_UNIT
;
896 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
901 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
903 register int desired_align
= 0;
905 /* The following are used only if the field is a bitfield */
906 register const char *bfld_type
= 0;
907 register int bfld_type_align
= 0, bfld_field_size
= 0;
909 /* The current type without the type qualifiers */
911 BOOL unionp
= layout
->original_type
[-1] == _C_UNION_B
;
913 /* Add the size of the previous field to the size of the record. */
914 if (layout
->prev_type
)
916 type
= objc_skip_type_qualifiers (layout
->prev_type
);
918 layout
->record_size
= MAX (layout
->record_size
,
919 objc_sizeof_type (type
) * BITS_PER_UNIT
);
921 else if (*type
!= _C_BFLD
)
922 layout
->record_size
+= objc_sizeof_type (type
) * BITS_PER_UNIT
;
924 /* Get the bitfield's type */
925 for (bfld_type
= type
+ 1;
926 isdigit ((unsigned char)*bfld_type
);
930 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
931 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
932 layout
->record_size
+= bfld_field_size
;
936 if ((unionp
&& *layout
->type
== _C_UNION_E
)
937 || (!unionp
&& *layout
->type
== _C_STRUCT_E
))
940 /* Skip the variable name if any */
941 if (*layout
->type
== '"')
943 for (layout
->type
++; *layout
->type
++ != '"';)
947 type
= objc_skip_type_qualifiers (layout
->type
);
949 if (*type
!= _C_BFLD
)
950 desired_align
= objc_alignof_type (type
) * BITS_PER_UNIT
;
954 /* Skip the bitfield's offset */
955 for (bfld_type
= type
+ 1;
956 isdigit ((unsigned char) *bfld_type
);
960 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
961 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
964 #ifdef BIGGEST_FIELD_ALIGNMENT
965 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
967 #ifdef ADJUST_FIELD_ALIGN
968 desired_align
= ADJUST_FIELD_ALIGN (type
, desired_align
);
971 /* Record must have at least as much alignment as any field.
972 Otherwise, the alignment of the field within the record
974 #ifndef PCC_BITFIELD_TYPE_MATTERS
975 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
976 #else /* PCC_BITFIELD_TYPE_MATTERS */
977 if (*type
== _C_BFLD
)
979 /* For these machines, a zero-length field does not
980 affect the alignment of the structure as a whole.
981 It does, however, affect the alignment of the next field
982 within the structure. */
984 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
986 desired_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
988 /* A named bit field of declared type `int'
989 forces the entire structure to have `int' alignment.
990 Q1: How is encoded this thing and how to check for it?
991 Q2: How to determine maximum_field_alignment at runtime? */
993 /* if (DECL_NAME (field) != 0) */
995 int type_align
= bfld_type_align
;
997 if (maximum_field_alignment
!= 0)
998 type_align
= MIN (type_align
, maximum_field_alignment
);
999 else if (DECL_PACKED (field
))
1000 type_align
= MIN (type_align
, BITS_PER_UNIT
);
1003 layout
->record_align
= MAX (layout
->record_align
, type_align
);
1007 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
1008 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1010 /* Does this field automatically have alignment it needs
1011 by virtue of the fields that precede it and the record's
1014 if (*type
== _C_BFLD
)
1015 layout
->record_size
= atoi (type
+ 1);
1016 else if (layout
->record_size
% desired_align
!= 0)
1018 /* No, we need to skip space before this field.
1019 Bump the cumulative size to multiple of field alignment. */
1020 layout
->record_size
= ROUND (layout
->record_size
, desired_align
);
1023 /* Jump to the next field in record. */
1025 layout
->prev_type
= layout
->type
;
1026 layout
->type
= objc_skip_typespec (layout
->type
); /* skip component */
1032 void objc_layout_finish_structure (struct objc_struct_layout
*layout
,
1034 unsigned int *align
)
1036 BOOL unionp
= layout
->original_type
[-1] == _C_UNION_B
;
1038 && ((!unionp
&& *layout
->type
== _C_STRUCT_E
)
1039 || (unionp
&& *layout
->type
== _C_UNION_E
)))
1041 /* Work out the alignment of the record as one expression and store
1042 in the record type. Round it up to a multiple of the record's
1044 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1045 layout
->record_align
= ROUND_TYPE_ALIGN (layout
->original_type
-1,
1047 layout
->record_align
);
1049 layout
->record_align
= MAX (1, layout
->record_align
);
1052 #ifdef ROUND_TYPE_SIZE
1053 layout
->record_size
= ROUND_TYPE_SIZE (layout
->original_type
,
1054 layout
->record_size
,
1055 layout
->record_align
);
1057 /* Round the size up to be a multiple of the required alignment */
1058 layout
->record_size
= ROUND (layout
->record_size
, layout
->record_align
);
1061 layout
->type
= NULL
;
1064 *size
= layout
->record_size
/ BITS_PER_UNIT
;
1066 *align
= layout
->record_align
/ BITS_PER_UNIT
;
1070 void objc_layout_structure_get_info (struct objc_struct_layout
*layout
,
1071 unsigned int *offset
,
1072 unsigned int *align
,
1076 *offset
= layout
->record_size
/ BITS_PER_UNIT
;
1078 *align
= layout
->record_align
/ BITS_PER_UNIT
;
1080 *type
= layout
->prev_type
;