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 strip_array_types(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 (strip_array_types (TREE_TYPE (_fields))) == DFmode) \
119 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
120 : MAX (COMPUTED, SPECIFIED));})
121 /* FIXME: The word 'fixme' is insufficient to explain the wrong-ness
122 of this next macro definition. */
123 #define darwin_rs6000_special_round_type_align(S,C,S2) \
124 rs6000_special_round_type_align(S,C,S2)
127 return the size of an object specified by type
131 objc_sizeof_type (const char *type
)
133 /* Skip the variable name if any */
136 for (type
++; *type
++ != '"';)
142 return sizeof (_Bool
);
150 return sizeof (Class
);
158 return sizeof (char);
162 return sizeof (unsigned char);
166 return sizeof (short);
170 return sizeof (unsigned short);
178 return sizeof (unsigned int);
182 return sizeof (long);
186 return sizeof (unsigned long);
190 return sizeof (long long);
194 return sizeof (unsigned long long);
198 return sizeof (float);
202 return sizeof (double);
206 return sizeof (void);
212 return sizeof (char *);
217 int len
= atoi (type
+ 1);
218 while (isdigit ((unsigned char)*++type
))
220 return len
* objc_aligned_size (type
);
226 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
228 int startByte
, endByte
;
230 position
= atoi (type
+ 1);
231 while (isdigit ((unsigned char)*++type
))
233 size
= atoi (type
+ 1);
235 startByte
= position
/ BITS_PER_UNIT
;
236 endByte
= (position
+ size
) / BITS_PER_UNIT
;
237 return endByte
- startByte
;
243 struct objc_struct_layout layout
;
246 objc_layout_structure (type
, &layout
);
247 while (objc_layout_structure_next_member (&layout
))
249 objc_layout_finish_structure (&layout
, &size
, NULL
);
256 type
++; /* Skip after the 'j'. */
260 return sizeof (_Complex
char);
264 return sizeof (_Complex
unsigned char);
268 return sizeof (_Complex
short);
272 return sizeof (_Complex
unsigned short);
276 return sizeof (_Complex
int);
280 return sizeof (_Complex
unsigned int);
284 return sizeof (_Complex
long);
288 return sizeof (_Complex
unsigned long);
292 return sizeof (_Complex
long long);
296 return sizeof (_Complex
unsigned long long);
300 return sizeof (_Complex
float);
304 return sizeof (_Complex
double);
309 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown complex type %s\n",
318 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
326 Return the alignment of an object specified by type
330 objc_alignof_type (const char *type
)
332 /* Skip the variable name if any */
335 for (type
++; *type
++ != '"';)
340 return __alignof__ (_Bool
);
344 return __alignof__ (id
);
348 return __alignof__ (Class
);
352 return __alignof__ (SEL
);
356 return __alignof__ (char);
360 return __alignof__ (unsigned char);
364 return __alignof__ (short);
368 return __alignof__ (unsigned short);
372 return __alignof__ (int);
376 return __alignof__ (unsigned int);
380 return __alignof__ (long);
384 return __alignof__ (unsigned long);
388 return __alignof__ (long long);
392 return __alignof__ (unsigned long long);
396 return __alignof__ (float);
400 return __alignof__ (double);
406 return __alignof__ (char *);
410 while (isdigit ((unsigned char)*++type
))
412 return objc_alignof_type (type
);
417 struct objc_struct_layout layout
;
420 objc_layout_structure (type
, &layout
);
421 while (objc_layout_structure_next_member (&layout
))
423 objc_layout_finish_structure (&layout
, NULL
, &align
);
431 type
++; /* Skip after the 'j'. */
435 return __alignof__ (_Complex
char);
439 return __alignof__ (_Complex
unsigned char);
443 return __alignof__ (_Complex
short);
447 return __alignof__ (_Complex
unsigned short);
451 return __alignof__ (_Complex
int);
455 return __alignof__ (_Complex
unsigned int);
459 return __alignof__ (_Complex
long);
463 return __alignof__ (_Complex
unsigned long);
467 return __alignof__ (_Complex
long long);
471 return __alignof__ (_Complex
unsigned long long);
475 return __alignof__ (_Complex
float);
479 return __alignof__ (_Complex
double);
484 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown complex type %s\n",
493 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
500 The aligned size if the size rounded up to the nearest alignment.
504 objc_aligned_size (const char *type
)
508 /* Skip the variable name */
511 for (type
++; *type
++ != '"';)
515 size
= objc_sizeof_type (type
);
516 align
= objc_alignof_type (type
);
518 return ROUND (size
, align
);
522 The size rounded up to the nearest integral of the wordsize, taken
523 to be the size of a void *.
527 objc_promoted_size (const char *type
)
531 /* Skip the variable name */
534 for (type
++; *type
++ != '"';)
538 size
= objc_sizeof_type (type
);
539 wordsize
= sizeof (void *);
541 return ROUND (size
, wordsize
);
545 Skip type qualifiers. These may eventually precede typespecs
546 occurring in method prototype encodings.
550 objc_skip_type_qualifiers (const char *type
)
552 while (*type
== _C_CONST
556 || *type
== _C_BYCOPY
558 || *type
== _C_ONEWAY
559 || *type
== _C_GCINVISIBLE
)
568 Skip one typespec element. If the typespec is prepended by type
569 qualifiers, these are skipped as well.
573 objc_skip_typespec (const char *type
)
575 /* Skip the variable name if any */
578 for (type
++; *type
++ != '"';)
582 type
= objc_skip_type_qualifiers (type
);
587 /* An id may be annotated by the actual type if it is known
588 with the @"ClassName" syntax */
594 while (*++type
!= '"')
599 /* The following are one character type codes */
627 /* skip digits, typespec and closing ']' */
629 while (isdigit ((unsigned char)*++type
))
631 type
= objc_skip_typespec (type
);
632 if (*type
== _C_ARY_E
)
636 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "bad array type %s\n", type
);
641 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
642 while (isdigit ((unsigned char)*++type
))
643 ; /* skip position */
644 while (isdigit ((unsigned char)*++type
))
645 ; /* skip type and size */
649 /* skip name, and elements until closing '}' */
651 while (*type
!= _C_STRUCT_E
&& *type
++ != '=')
653 while (*type
!= _C_STRUCT_E
)
655 type
= objc_skip_typespec (type
);
660 /* skip name, and elements until closing ')' */
662 while (*type
!= _C_UNION_E
&& *type
++ != '=')
664 while (*type
!= _C_UNION_E
)
666 type
= objc_skip_typespec (type
);
671 /* Just skip the following typespec */
673 return objc_skip_typespec (++type
);
677 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
684 Skip an offset as part of a method encoding. This is prepended by a
685 '+' if the argument is passed in registers.
688 objc_skip_offset (const char *type
)
692 while (isdigit ((unsigned char) *++type
))
698 Skip an argument specification of a method encoding.
701 objc_skip_argspec (const char *type
)
703 type
= objc_skip_typespec (type
);
704 type
= objc_skip_offset (type
);
709 Return the number of arguments that the method MTH expects.
710 Note that all methods need two implicit arguments `self' and
714 method_get_number_of_arguments (struct objc_method
*mth
)
717 const char *type
= mth
->method_types
;
720 type
= objc_skip_argspec (type
);
727 Return the size of the argument block needed on the stack to invoke
728 the method MTH. This may be zero, if all arguments are passed in
733 method_get_sizeof_arguments (struct objc_method
*mth
)
735 const char *type
= objc_skip_typespec (mth
->method_types
);
740 Return a pointer to the next argument of ARGFRAME. type points to
741 the last argument. Typical use of this look like:
745 for (datum = method_get_first_argument (method, argframe, &type);
746 datum; datum = method_get_next_argument (argframe, &type))
748 unsigned flags = objc_get_type_qualifiers (type);
749 type = objc_skip_type_qualifiers (type);
751 [portal encodeData: datum ofType: type];
754 if ((flags & _F_IN) == _F_IN)
755 [portal encodeData: *(char **) datum ofType: ++type];
762 method_get_next_argument (arglist_t argframe
, const char **type
)
764 const char *t
= objc_skip_argspec (*type
);
770 t
= objc_skip_typespec (t
);
773 return argframe
->arg_regs
+ atoi (++t
);
775 return argframe
->arg_ptr
+ atoi (t
);
779 Return a pointer to the value of the first argument of the method
780 described in M with the given argumentframe ARGFRAME. The type
781 is returned in TYPE. type must be passed to successive calls of
782 method_get_next_argument.
785 method_get_first_argument (struct objc_method
*m
,
789 *type
= m
->method_types
;
790 return method_get_next_argument (argframe
, type
);
794 Return a pointer to the ARGth argument of the method
795 M from the frame ARGFRAME. The type of the argument
796 is returned in the value-result argument TYPE
800 method_get_nth_argument (struct objc_method
*m
,
801 arglist_t argframe
, int arg
,
804 const char *t
= objc_skip_argspec (m
->method_types
);
806 if (arg
> method_get_number_of_arguments (m
))
810 t
= objc_skip_argspec (t
);
813 t
= objc_skip_typespec (t
);
816 return argframe
->arg_regs
+ atoi (++t
);
818 return argframe
->arg_ptr
+ atoi (t
);
822 objc_get_type_qualifiers (const char *type
)
830 case _C_CONST
: res
|= _F_CONST
; break;
831 case _C_IN
: res
|= _F_IN
; break;
832 case _C_INOUT
: res
|= _F_INOUT
; break;
833 case _C_OUT
: res
|= _F_OUT
; break;
834 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
835 case _C_BYREF
: res
|= _F_BYREF
; break;
836 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
837 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
845 /* The following three functions can be used to determine how a
846 structure is laid out by the compiler. For example:
848 struct objc_struct_layout layout;
851 objc_layout_structure (type, &layout);
852 while (objc_layout_structure_next_member (&layout))
857 objc_layout_structure_get_info (&layout, &position, &align, &type);
858 printf ("element %d has offset %d, alignment %d\n",
859 i++, position, align);
862 These functions are used by objc_sizeof_type and objc_alignof_type
863 functions to compute the size and alignment of structures. The
864 previous method of computing the size and alignment of a structure
865 was not working on some architectures, particulary on AIX, and in
866 the presence of bitfields inside the structure. */
868 objc_layout_structure (const char *type
,
869 struct objc_struct_layout
*layout
)
873 if (*type
!= _C_UNION_B
&& *type
!= _C_STRUCT_B
)
875 objc_error (nil
, OBJC_ERR_BAD_TYPE
,
876 "record (or union) type expected in objc_layout_structure, got %s\n",
881 layout
->original_type
= type
;
883 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
885 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
889 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
890 if (*(ntype
- 1) == '=')
894 layout
->prev_type
= NULL
;
895 layout
->record_size
= 0;
896 layout
->record_align
= BITS_PER_UNIT
;
898 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
903 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
905 register int desired_align
= 0;
907 /* The following are used only if the field is a bitfield */
908 register const char *bfld_type
= 0;
909 register int bfld_type_size
, bfld_type_align
= 0, bfld_field_size
= 0;
911 /* The current type without the type qualifiers */
913 BOOL unionp
= layout
->original_type
[-1] == _C_UNION_B
;
915 /* Add the size of the previous field to the size of the record. */
916 if (layout
->prev_type
)
918 type
= objc_skip_type_qualifiers (layout
->prev_type
);
920 layout
->record_size
= MAX (layout
->record_size
,
921 objc_sizeof_type (type
) * BITS_PER_UNIT
);
923 else if (*type
!= _C_BFLD
)
924 layout
->record_size
+= objc_sizeof_type (type
) * BITS_PER_UNIT
;
926 /* Get the bitfield's type */
927 for (bfld_type
= type
+ 1;
928 isdigit ((unsigned char)*bfld_type
);
932 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
933 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
934 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
935 layout
->record_size
+= bfld_field_size
;
939 if ((unionp
&& *layout
->type
== _C_UNION_E
)
940 || (!unionp
&& *layout
->type
== _C_STRUCT_E
))
943 /* Skip the variable name if any */
944 if (*layout
->type
== '"')
946 for (layout
->type
++; *layout
->type
++ != '"';)
950 type
= objc_skip_type_qualifiers (layout
->type
);
952 if (*type
!= _C_BFLD
)
953 desired_align
= objc_alignof_type (type
) * BITS_PER_UNIT
;
957 /* Skip the bitfield's offset */
958 for (bfld_type
= type
+ 1;
959 isdigit ((unsigned char) *bfld_type
);
963 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
964 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
965 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
968 #ifdef BIGGEST_FIELD_ALIGNMENT
969 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
971 #ifdef ADJUST_FIELD_ALIGN
972 desired_align
= ADJUST_FIELD_ALIGN (type
, desired_align
);
975 /* Record must have at least as much alignment as any field.
976 Otherwise, the alignment of the field within the record
978 #ifndef PCC_BITFIELD_TYPE_MATTERS
979 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
980 #else /* PCC_BITFIELD_TYPE_MATTERS */
981 if (*type
== _C_BFLD
)
983 /* For these machines, a zero-length field does not
984 affect the alignment of the structure as a whole.
985 It does, however, affect the alignment of the next field
986 within the structure. */
988 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
990 desired_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
992 /* A named bit field of declared type `int'
993 forces the entire structure to have `int' alignment.
994 Q1: How is encoded this thing and how to check for it?
995 Q2: How to determine maximum_field_alignment at runtime? */
997 /* if (DECL_NAME (field) != 0) */
999 int type_align
= bfld_type_align
;
1001 if (maximum_field_alignment
!= 0)
1002 type_align
= MIN (type_align
, maximum_field_alignment
);
1003 else if (DECL_PACKED (field
))
1004 type_align
= MIN (type_align
, BITS_PER_UNIT
);
1007 layout
->record_align
= MAX (layout
->record_align
, type_align
);
1011 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
1012 #endif /* PCC_BITFIELD_TYPE_MATTERS */
1014 /* Does this field automatically have alignment it needs
1015 by virtue of the fields that precede it and the record's
1018 if (*type
== _C_BFLD
)
1019 layout
->record_size
= atoi (type
+ 1);
1020 else if (layout
->record_size
% desired_align
!= 0)
1022 /* No, we need to skip space before this field.
1023 Bump the cumulative size to multiple of field alignment. */
1024 layout
->record_size
= ROUND (layout
->record_size
, desired_align
);
1027 /* Jump to the next field in record. */
1029 layout
->prev_type
= layout
->type
;
1030 layout
->type
= objc_skip_typespec (layout
->type
); /* skip component */
1036 void objc_layout_finish_structure (struct objc_struct_layout
*layout
,
1038 unsigned int *align
)
1040 BOOL unionp
= layout
->original_type
[-1] == _C_UNION_B
;
1042 && ((!unionp
&& *layout
->type
== _C_STRUCT_E
)
1043 || (unionp
&& *layout
->type
== _C_UNION_E
)))
1045 /* Work out the alignment of the record as one expression and store
1046 in the record type. Round it up to a multiple of the record's
1048 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
1049 layout
->record_align
= ROUND_TYPE_ALIGN (layout
->original_type
-1,
1051 layout
->record_align
);
1053 layout
->record_align
= MAX (1, layout
->record_align
);
1056 #ifdef ROUND_TYPE_SIZE
1057 layout
->record_size
= ROUND_TYPE_SIZE (layout
->original_type
,
1058 layout
->record_size
,
1059 layout
->record_align
);
1061 /* Round the size up to be a multiple of the required alignment */
1062 layout
->record_size
= ROUND (layout
->record_size
, layout
->record_align
);
1065 layout
->type
= NULL
;
1068 *size
= layout
->record_size
/ BITS_PER_UNIT
;
1070 *align
= layout
->record_align
/ BITS_PER_UNIT
;
1074 void objc_layout_structure_get_info (struct objc_struct_layout
*layout
,
1075 unsigned int *offset
,
1076 unsigned int *align
,
1080 *offset
= layout
->record_size
/ BITS_PER_UNIT
;
1082 *align
= layout
->record_align
/ BITS_PER_UNIT
;
1084 *type
= layout
->prev_type
;