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) ({const char *_field = (TYPE)+1; \
71 while (*_field != _C_STRUCT_E && *_field != _C_STRUCT_B \
72 && *_field != _C_UNION_B && *_field++ != '=') \
76 #define DECL_MODE(TYPE) *(TYPE)
77 #define TYPE_MODE(TYPE) *(TYPE)
81 #define get_inner_array_type(TYPE) ({const char *_field = (TYPE); \
82 while (*_field == _C_ARY_B)\
84 while (isdigit ((unsigned char)*++_field))\
89 /* Some ports (eg ARM) allow the structure size boundary to be
90 selected at compile-time. We override the normal definition with
91 one that has a constant value for this compilation. */
93 #define BITS_PER_UNIT 8
95 #undef STRUCTURE_SIZE_BOUNDARY
96 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
98 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
99 target_flags. Define a dummy entry here to so we don't die.
100 We have to rename it because target_flags may already have been
102 #define target_flags not_target_flags
103 static int __attribute__ ((__unused__
)) not_target_flags
= 0;
105 /* Some ROUND_TYPE_ALIGN use ALTIVEC_VECTOR_MODE (rs6000 darwin).
106 Define a dummy ALTIVEC_VECTOR_MODE so it will not die. */
107 #undef ALTIVEC_VECTOR_MODE
108 #define ALTIVEC_VECTOR_MODE(MODE) (0)
111 /* FIXME: while this file has no business including tm.h, this
112 definitely has no business defining this macro but it
113 is only way around without really rewritting this file,
114 should look after the branch of 3.4 to fix this. */
115 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
116 ({ const char *_fields = TYPE_FIELDS (STRUCT); \
118 && TYPE_MODE (TREE_CODE (TREE_TYPE (_fields)) == ARRAY_TYPE \
119 ? get_inner_array_type (_fields) \
120 : TREE_TYPE (_fields)) == DFmode) \
121 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
122 : MAX (COMPUTED, SPECIFIED));})
123 /* FIXME: The word 'fixme' is insufficient to explain the wrong-ness
124 of this next macro definition. */
125 #define darwin_rs6000_special_round_type_align(S,C,S2) \
126 rs6000_special_round_type_align(S,C,S2)
129 return the size of an object specified by type
133 objc_sizeof_type (const char *type
)
135 /* Skip the variable name if any */
138 for (type
++; *type
++ != '"';)
144 return sizeof (_Bool
);
152 return sizeof (Class
);
160 return sizeof (char);
164 return sizeof (unsigned char);
168 return sizeof (short);
172 return sizeof (unsigned short);
180 return sizeof (unsigned int);
184 return sizeof (long);
188 return sizeof (unsigned long);
192 return sizeof (long long);
196 return sizeof (unsigned long long);
200 return sizeof (float);
204 return sizeof (double);
208 return sizeof (void);
214 return sizeof (char *);
219 int len
= atoi (type
+ 1);
220 while (isdigit ((unsigned char)*++type
))
222 return len
* objc_aligned_size (type
);
228 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
230 int startByte
, endByte
;
232 position
= atoi (type
+ 1);
233 while (isdigit ((unsigned char)*++type
))
235 size
= atoi (type
+ 1);
237 startByte
= position
/ BITS_PER_UNIT
;
238 endByte
= (position
+ size
) / BITS_PER_UNIT
;
239 return endByte
- startByte
;
245 struct objc_struct_layout layout
;
248 objc_layout_structure (type
, &layout
);
249 while (objc_layout_structure_next_member (&layout
))
251 objc_layout_finish_structure (&layout
, &size
, NULL
);
258 type
++; /* Skip after the 'j'. */
262 return sizeof (_Complex
char);
266 return sizeof (_Complex
unsigned char);
270 return sizeof (_Complex
short);
274 return sizeof (_Complex
unsigned short);
278 return sizeof (_Complex
int);
282 return sizeof (_Complex
unsigned int);
286 return sizeof (_Complex
long);
290 return sizeof (_Complex
unsigned long);
294 return sizeof (_Complex
long long);
298 return sizeof (_Complex
unsigned long long);
302 return sizeof (_Complex
float);
306 return sizeof (_Complex
double);
311 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown complex type %s\n",
320 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
328 Return the alignment of an object specified by type
332 objc_alignof_type (const char *type
)
334 /* Skip the variable name if any */
337 for (type
++; *type
++ != '"';)
342 return __alignof__ (_Bool
);
346 return __alignof__ (id
);
350 return __alignof__ (Class
);
354 return __alignof__ (SEL
);
358 return __alignof__ (char);
362 return __alignof__ (unsigned char);
366 return __alignof__ (short);
370 return __alignof__ (unsigned short);
374 return __alignof__ (int);
378 return __alignof__ (unsigned int);
382 return __alignof__ (long);
386 return __alignof__ (unsigned long);
390 return __alignof__ (long long);
394 return __alignof__ (unsigned long long);
398 return __alignof__ (float);
402 return __alignof__ (double);
408 return __alignof__ (char *);
412 while (isdigit ((unsigned char)*++type
))
414 return objc_alignof_type (type
);
419 struct objc_struct_layout layout
;
422 objc_layout_structure (type
, &layout
);
423 while (objc_layout_structure_next_member (&layout
))
425 objc_layout_finish_structure (&layout
, NULL
, &align
);
433 type
++; /* Skip after the 'j'. */
437 return __alignof__ (_Complex
char);
441 return __alignof__ (_Complex
unsigned char);
445 return __alignof__ (_Complex
short);
449 return __alignof__ (_Complex
unsigned short);
453 return __alignof__ (_Complex
int);
457 return __alignof__ (_Complex
unsigned int);
461 return __alignof__ (_Complex
long);
465 return __alignof__ (_Complex
unsigned long);
469 return __alignof__ (_Complex
long long);
473 return __alignof__ (_Complex
unsigned long long);
477 return __alignof__ (_Complex
float);
481 return __alignof__ (_Complex
double);
486 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown complex type %s\n",
495 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
502 The aligned size if the size rounded up to the nearest alignment.
506 objc_aligned_size (const char *type
)
510 /* Skip the variable name */
513 for (type
++; *type
++ != '"';)
517 size
= objc_sizeof_type (type
);
518 align
= objc_alignof_type (type
);
520 return ROUND (size
, align
);
524 The size rounded up to the nearest integral of the wordsize, taken
525 to be the size of a void *.
529 objc_promoted_size (const char *type
)
533 /* Skip the variable name */
536 for (type
++; *type
++ != '"';)
540 size
= objc_sizeof_type (type
);
541 wordsize
= sizeof (void *);
543 return ROUND (size
, wordsize
);
547 Skip type qualifiers. These may eventually precede typespecs
548 occurring in method prototype encodings.
552 objc_skip_type_qualifiers (const char *type
)
554 while (*type
== _C_CONST
558 || *type
== _C_BYCOPY
560 || *type
== _C_ONEWAY
561 || *type
== _C_GCINVISIBLE
)
570 Skip one typespec element. If the typespec is prepended by type
571 qualifiers, these are skipped as well.
575 objc_skip_typespec (const char *type
)
577 /* Skip the variable name if any */
580 for (type
++; *type
++ != '"';)
584 type
= objc_skip_type_qualifiers (type
);
589 /* An id may be annotated by the actual type if it is known
590 with the @"ClassName" syntax */
596 while (*++type
!= '"')
601 /* The following are one character type codes */
629 /* skip digits, typespec and closing ']' */
631 while (isdigit ((unsigned char)*++type
))
633 type
= objc_skip_typespec (type
);
634 if (*type
== _C_ARY_E
)
638 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "bad array type %s\n", type
);
643 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
644 while (isdigit ((unsigned char)*++type
))
645 ; /* skip position */
646 while (isdigit ((unsigned char)*++type
))
647 ; /* skip type and size */
651 /* skip name, and elements until closing '}' */
653 while (*type
!= _C_STRUCT_E
&& *type
++ != '=')
655 while (*type
!= _C_STRUCT_E
)
657 type
= objc_skip_typespec (type
);
662 /* skip name, and elements until closing ')' */
664 while (*type
!= _C_UNION_E
&& *type
++ != '=')
666 while (*type
!= _C_UNION_E
)
668 type
= objc_skip_typespec (type
);
673 /* Just skip the following typespec */
675 return objc_skip_typespec (++type
);
679 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
686 Skip an offset as part of a method encoding. This is prepended by a
687 '+' if the argument is passed in registers.
690 objc_skip_offset (const char *type
)
694 while (isdigit ((unsigned char) *++type
))
700 Skip an argument specification of a method encoding.
703 objc_skip_argspec (const char *type
)
705 type
= objc_skip_typespec (type
);
706 type
= objc_skip_offset (type
);
711 Return the number of arguments that the method MTH expects.
712 Note that all methods need two implicit arguments `self' and
716 method_get_number_of_arguments (struct objc_method
*mth
)
719 const char *type
= mth
->method_types
;
722 type
= objc_skip_argspec (type
);
729 Return the size of the argument block needed on the stack to invoke
730 the method MTH. This may be zero, if all arguments are passed in
735 method_get_sizeof_arguments (struct objc_method
*mth
)
737 const char *type
= objc_skip_typespec (mth
->method_types
);
742 Return a pointer to the next argument of ARGFRAME. type points to
743 the last argument. Typical use of this look like:
747 for (datum = method_get_first_argument (method, argframe, &type);
748 datum; datum = method_get_next_argument (argframe, &type))
750 unsigned flags = objc_get_type_qualifiers (type);
751 type = objc_skip_type_qualifiers (type);
753 [portal encodeData: datum ofType: type];
756 if ((flags & _F_IN) == _F_IN)
757 [portal encodeData: *(char **) datum ofType: ++type];
764 method_get_next_argument (arglist_t argframe
, const char **type
)
766 const char *t
= objc_skip_argspec (*type
);
772 t
= objc_skip_typespec (t
);
775 return argframe
->arg_regs
+ atoi (++t
);
777 return argframe
->arg_ptr
+ atoi (t
);
781 Return a pointer to the value of the first argument of the method
782 described in M with the given argumentframe ARGFRAME. The type
783 is returned in TYPE. type must be passed to successive calls of
784 method_get_next_argument.
787 method_get_first_argument (struct objc_method
*m
,
791 *type
= m
->method_types
;
792 return method_get_next_argument (argframe
, type
);
796 Return a pointer to the ARGth argument of the method
797 M from the frame ARGFRAME. The type of the argument
798 is returned in the value-result argument TYPE
802 method_get_nth_argument (struct objc_method
*m
,
803 arglist_t argframe
, int arg
,
806 const char *t
= objc_skip_argspec (m
->method_types
);
808 if (arg
> method_get_number_of_arguments (m
))
812 t
= objc_skip_argspec (t
);
815 t
= objc_skip_typespec (t
);
818 return argframe
->arg_regs
+ atoi (++t
);
820 return argframe
->arg_ptr
+ atoi (t
);
824 objc_get_type_qualifiers (const char *type
)
832 case _C_CONST
: res
|= _F_CONST
; break;
833 case _C_IN
: res
|= _F_IN
; break;
834 case _C_INOUT
: res
|= _F_INOUT
; break;
835 case _C_OUT
: res
|= _F_OUT
; break;
836 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
837 case _C_BYREF
: res
|= _F_BYREF
; break;
838 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
839 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
847 /* The following three functions can be used to determine how a
848 structure is laid out by the compiler. For example:
850 struct objc_struct_layout layout;
853 objc_layout_structure (type, &layout);
854 while (objc_layout_structure_next_member (&layout))
859 objc_layout_structure_get_info (&layout, &position, &align, &type);
860 printf ("element %d has offset %d, alignment %d\n",
861 i++, position, align);
864 These functions are used by objc_sizeof_type and objc_alignof_type
865 functions to compute the size and alignment of structures. The
866 previous method of computing the size and alignment of a structure
867 was not working on some architectures, particulary on AIX, and in
868 the presence of bitfields inside the structure. */
870 objc_layout_structure (const char *type
,
871 struct objc_struct_layout
*layout
)
875 if (*type
!= _C_UNION_B
&& *type
!= _C_STRUCT_B
)
877 objc_error (nil
, OBJC_ERR_BAD_TYPE
,
878 "record (or union) type expected in objc_layout_structure, got %s\n",
883 layout
->original_type
= type
;
885 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
887 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
891 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
892 if (*(ntype
- 1) == '=')
896 layout
->prev_type
= NULL
;
897 layout
->record_size
= 0;
898 layout
->record_align
= BITS_PER_UNIT
;
900 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
905 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
907 register int desired_align
= 0;
909 /* The following are used only if the field is a bitfield */
910 register const char *bfld_type
= 0;
911 register int bfld_type_size
, bfld_type_align
= 0, bfld_field_size
= 0;
913 /* The current type without the type qualifiers */
915 BOOL unionp
= layout
->original_type
[-1] == _C_UNION_B
;
917 /* Add the size of the previous field to the size of the record. */
918 if (layout
->prev_type
)
920 type
= objc_skip_type_qualifiers (layout
->prev_type
);
922 layout
->record_size
= MAX (layout
->record_size
,
923 objc_sizeof_type (type
) * BITS_PER_UNIT
);
925 else if (*type
!= _C_BFLD
)
926 layout
->record_size
+= objc_sizeof_type (type
) * BITS_PER_UNIT
;
928 /* Get the bitfield's type */
929 for (bfld_type
= type
+ 1;
930 isdigit ((unsigned char)*bfld_type
);
934 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
935 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
936 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
937 layout
->record_size
+= bfld_field_size
;
941 if ((unionp
&& *layout
->type
== _C_UNION_E
)
942 || (!unionp
&& *layout
->type
== _C_STRUCT_E
))
945 /* Skip the variable name if any */
946 if (*layout
->type
== '"')
948 for (layout
->type
++; *layout
->type
++ != '"';)
952 type
= objc_skip_type_qualifiers (layout
->type
);
954 if (*type
!= _C_BFLD
)
955 desired_align
= objc_alignof_type (type
) * BITS_PER_UNIT
;
959 /* Skip the bitfield's offset */
960 for (bfld_type
= type
+ 1;
961 isdigit ((unsigned char) *bfld_type
);
965 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
966 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
967 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
970 #ifdef BIGGEST_FIELD_ALIGNMENT
971 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
973 #ifdef ADJUST_FIELD_ALIGN
974 desired_align
= ADJUST_FIELD_ALIGN (type
, desired_align
);
977 /* Record must have at least as much alignment as any field.
978 Otherwise, the alignment of the field within the record
980 #ifndef PCC_BITFIELD_TYPE_MATTERS
981 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
982 #else /* PCC_BITFIELD_TYPE_MATTERS */
983 if (*type
== _C_BFLD
)
985 /* For these machines, a zero-length field does not
986 affect the alignment of the structure as a whole.
987 It does, however, affect the alignment of the next field
988 within the structure. */
990 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
992 desired_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
994 /* A named bit field of declared type `int'
995 forces the entire structure to have `int' alignment.
996 Q1: How is encoded this thing and how to check for it?
997 Q2: How to determine maximum_field_alignment at runtime? */
999 /* if (DECL_NAME (field) != 0) */
1001 int type_align
= bfld_type_align
;
1003 if (maximum_field_alignment
!= 0)
1004 type_align
= MIN (type_align
, maximum_field_alignment
);
1005 else if (DECL_PACKED (field
))
1006 type_align
= MIN (type_align
, BITS_PER_UNIT
);
1009 layout
->record_align
= MAX (layout
->record_align
, type_align
);
1013 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
1014 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1016 /* Does this field automatically have alignment it needs
1017 by virtue of the fields that precede it and the record's
1020 if (*type
== _C_BFLD
)
1021 layout
->record_size
= atoi (type
+ 1);
1022 else if (layout
->record_size
% desired_align
!= 0)
1024 /* No, we need to skip space before this field.
1025 Bump the cumulative size to multiple of field alignment. */
1026 layout
->record_size
= ROUND (layout
->record_size
, desired_align
);
1029 /* Jump to the next field in record. */
1031 layout
->prev_type
= layout
->type
;
1032 layout
->type
= objc_skip_typespec (layout
->type
); /* skip component */
1038 void objc_layout_finish_structure (struct objc_struct_layout
*layout
,
1040 unsigned int *align
)
1042 BOOL unionp
= layout
->original_type
[-1] == _C_UNION_B
;
1044 && ((!unionp
&& *layout
->type
== _C_STRUCT_E
)
1045 || (unionp
&& *layout
->type
== _C_UNION_E
)))
1047 /* Work out the alignment of the record as one expression and store
1048 in the record type. Round it up to a multiple of the record's
1050 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1051 layout
->record_align
= ROUND_TYPE_ALIGN (layout
->original_type
-1,
1053 layout
->record_align
);
1055 layout
->record_align
= MAX (1, layout
->record_align
);
1058 #ifdef ROUND_TYPE_SIZE
1059 layout
->record_size
= ROUND_TYPE_SIZE (layout
->original_type
,
1060 layout
->record_size
,
1061 layout
->record_align
);
1063 /* Round the size up to be a multiple of the required alignment */
1064 layout
->record_size
= ROUND (layout
->record_size
, layout
->record_align
);
1067 layout
->type
= NULL
;
1070 *size
= layout
->record_size
/ BITS_PER_UNIT
;
1072 *align
= layout
->record_align
/ BITS_PER_UNIT
;
1076 void objc_layout_structure_get_info (struct objc_struct_layout
*layout
,
1077 unsigned int *offset
,
1078 unsigned int *align
,
1082 *offset
= layout
->record_size
/ BITS_PER_UNIT
;
1084 *align
= layout
->record_align
/ BITS_PER_UNIT
;
1086 *type
= layout
->prev_type
;