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, 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, 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"
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) objc_skip_typespec (TYPE)
72 #define DECL_MODE(TYPE) *(TYPE)
73 #define TYPE_MODE(TYPE) *(TYPE)
77 #define get_inner_array_type(TYPE) ((TYPE) + 1)
79 /* Some ports (eg ARM) allow the structure size boundary to be
80 selected at compile-time. We override the normal definition with
81 one that has a constant value for this compilation. */
83 #define BITS_PER_UNIT 8
85 #undef STRUCTURE_SIZE_BOUNDARY
86 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
88 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
89 target_flags. Define a dummy entry here to so we don't die.
90 We have to rename it because target_flags may already have been
92 #define target_flags not_target_flags
93 static int __attribute__ ((__unused__
)) not_target_flags
= 0;
96 /* FIXME: while this file has no business including tm.h, this
97 definitely has no business defining this macro but it
98 is only way around without really rewritting this file,
99 should look after the branch of 3.4 to fix this. */
100 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
101 ((TYPE_FIELDS (STRUCT) != 0 \
102 && DECL_MODE (TYPE_FIELDS (STRUCT)) == DFmode) \
103 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
104 : MAX (COMPUTED, SPECIFIED))
107 return the size of an object specified by type
111 objc_sizeof_type (const char *type
)
113 /* Skip the variable name if any */
116 for (type
++; *type
++ != '"';)
126 return sizeof (Class
);
134 return sizeof (char);
138 return sizeof (unsigned char);
142 return sizeof (short);
146 return sizeof (unsigned short);
154 return sizeof (unsigned int);
158 return sizeof (long);
162 return sizeof (unsigned long);
166 return sizeof (long long);
170 return sizeof (unsigned long long);
174 return sizeof (float);
178 return sizeof (double);
182 return sizeof (void);
188 return sizeof (char *);
193 int len
= atoi (type
+ 1);
194 while (isdigit ((unsigned char)*++type
))
196 return len
* objc_aligned_size (type
);
202 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
204 int startByte
, endByte
;
206 position
= atoi (type
+ 1);
207 while (isdigit ((unsigned char)*++type
))
209 size
= atoi (type
+ 1);
211 startByte
= position
/ BITS_PER_UNIT
;
212 endByte
= (position
+ size
) / BITS_PER_UNIT
;
213 return endByte
- startByte
;
218 struct objc_struct_layout layout
;
221 objc_layout_structure (type
, &layout
);
222 while (objc_layout_structure_next_member (&layout
))
224 objc_layout_finish_structure (&layout
, &size
, NULL
);
232 while (*type
!= _C_UNION_E
&& *type
++ != '=')
234 while (*type
!= _C_UNION_E
)
236 /* Skip the variable name if any */
239 for (type
++; *type
++ != '"';)
242 max_size
= MAX (max_size
, objc_sizeof_type (type
));
243 type
= objc_skip_typespec (type
);
250 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
258 Return the alignment of an object specified by type
262 objc_alignof_type (const char *type
)
264 /* Skip the variable name if any */
267 for (type
++; *type
++ != '"';)
272 return __alignof__ (id
);
276 return __alignof__ (Class
);
280 return __alignof__ (SEL
);
284 return __alignof__ (char);
288 return __alignof__ (unsigned char);
292 return __alignof__ (short);
296 return __alignof__ (unsigned short);
300 return __alignof__ (int);
304 return __alignof__ (unsigned int);
308 return __alignof__ (long);
312 return __alignof__ (unsigned long);
316 return __alignof__ (long long);
320 return __alignof__ (unsigned long long);
324 return __alignof__ (float);
328 return __alignof__ (double);
334 return __alignof__ (char *);
338 while (isdigit ((unsigned char)*++type
))
340 return objc_alignof_type (type
);
344 struct objc_struct_layout layout
;
347 objc_layout_structure (type
, &layout
);
348 while (objc_layout_structure_next_member (&layout
))
350 objc_layout_finish_structure (&layout
, NULL
, &align
);
358 while (*type
!= _C_UNION_E
&& *type
++ != '=')
360 while (*type
!= _C_UNION_E
)
362 /* Skip the variable name if any */
365 for (type
++; *type
++ != '"';)
368 maxalign
= MAX (maxalign
, objc_alignof_type (type
));
369 type
= objc_skip_typespec (type
);
376 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
383 The aligned size if the size rounded up to the nearest alignment.
387 objc_aligned_size (const char *type
)
391 /* Skip the variable name */
394 for (type
++; *type
++ != '"';)
398 size
= objc_sizeof_type (type
);
399 align
= objc_alignof_type (type
);
401 return ROUND (size
, align
);
405 The size rounded up to the nearest integral of the wordsize, taken
406 to be the size of a void *.
410 objc_promoted_size (const char *type
)
414 /* Skip the variable name */
417 for (type
++; *type
++ != '"';)
421 size
= objc_sizeof_type (type
);
422 wordsize
= sizeof (void *);
424 return ROUND (size
, wordsize
);
428 Skip type qualifiers. These may eventually precede typespecs
429 occurring in method prototype encodings.
433 objc_skip_type_qualifiers (const char *type
)
435 while (*type
== _C_CONST
439 || *type
== _C_BYCOPY
441 || *type
== _C_ONEWAY
442 || *type
== _C_GCINVISIBLE
)
451 Skip one typespec element. If the typespec is prepended by type
452 qualifiers, these are skipped as well.
456 objc_skip_typespec (const char *type
)
458 /* Skip the variable name if any */
461 for (type
++; *type
++ != '"';)
465 type
= objc_skip_type_qualifiers (type
);
470 /* An id may be annotated by the actual type if it is known
471 with the @"ClassName" syntax */
477 while (*++type
!= '"')
482 /* The following are one character type codes */
505 /* skip digits, typespec and closing ']' */
507 while (isdigit ((unsigned char)*++type
))
509 type
= objc_skip_typespec (type
);
510 if (*type
== _C_ARY_E
)
514 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "bad array type %s\n", type
);
519 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
520 while (isdigit ((unsigned char)*++type
))
521 ; /* skip position */
522 while (isdigit ((unsigned char)*++type
))
523 ; /* skip type and size */
527 /* skip name, and elements until closing '}' */
529 while (*type
!= _C_STRUCT_E
&& *type
++ != '=')
531 while (*type
!= _C_STRUCT_E
)
533 type
= objc_skip_typespec (type
);
538 /* skip name, and elements until closing ')' */
540 while (*type
!= _C_UNION_E
&& *type
++ != '=')
542 while (*type
!= _C_UNION_E
)
544 type
= objc_skip_typespec (type
);
549 /* Just skip the following typespec */
551 return objc_skip_typespec (++type
);
555 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
562 Skip an offset as part of a method encoding. This is prepended by a
563 '+' if the argument is passed in registers.
566 objc_skip_offset (const char *type
)
570 while (isdigit ((unsigned char) *++type
))
576 Skip an argument specification of a method encoding.
579 objc_skip_argspec (const char *type
)
581 type
= objc_skip_typespec (type
);
582 type
= objc_skip_offset (type
);
587 Return the number of arguments that the method MTH expects.
588 Note that all methods need two implicit arguments `self' and
592 method_get_number_of_arguments (struct objc_method
*mth
)
595 const char *type
= mth
->method_types
;
598 type
= objc_skip_argspec (type
);
605 Return the size of the argument block needed on the stack to invoke
606 the method MTH. This may be zero, if all arguments are passed in
611 method_get_sizeof_arguments (struct objc_method
*mth
)
613 const char *type
= objc_skip_typespec (mth
->method_types
);
618 Return a pointer to the next argument of ARGFRAME. type points to
619 the last argument. Typical use of this look like:
623 for (datum = method_get_first_argument (method, argframe, &type);
624 datum; datum = method_get_next_argument (argframe, &type))
626 unsigned flags = objc_get_type_qualifiers (type);
627 type = objc_skip_type_qualifiers (type);
629 [portal encodeData: datum ofType: type];
632 if ((flags & _F_IN) == _F_IN)
633 [portal encodeData: *(char **) datum ofType: ++type];
640 method_get_next_argument (arglist_t argframe
, const char **type
)
642 const char *t
= objc_skip_argspec (*type
);
648 t
= objc_skip_typespec (t
);
651 return argframe
->arg_regs
+ atoi (++t
);
653 return argframe
->arg_ptr
+ atoi (t
);
657 Return a pointer to the value of the first argument of the method
658 described in M with the given argumentframe ARGFRAME. The type
659 is returned in TYPE. type must be passed to successive calls of
660 method_get_next_argument.
663 method_get_first_argument (struct objc_method
*m
,
667 *type
= m
->method_types
;
668 return method_get_next_argument (argframe
, type
);
672 Return a pointer to the ARGth argument of the method
673 M from the frame ARGFRAME. The type of the argument
674 is returned in the value-result argument TYPE
678 method_get_nth_argument (struct objc_method
*m
,
679 arglist_t argframe
, int arg
,
682 const char *t
= objc_skip_argspec (m
->method_types
);
684 if (arg
> method_get_number_of_arguments (m
))
688 t
= objc_skip_argspec (t
);
691 t
= objc_skip_typespec (t
);
694 return argframe
->arg_regs
+ atoi (++t
);
696 return argframe
->arg_ptr
+ atoi (t
);
700 objc_get_type_qualifiers (const char *type
)
708 case _C_CONST
: res
|= _F_CONST
; break;
709 case _C_IN
: res
|= _F_IN
; break;
710 case _C_INOUT
: res
|= _F_INOUT
; break;
711 case _C_OUT
: res
|= _F_OUT
; break;
712 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
713 case _C_BYREF
: res
|= _F_BYREF
; break;
714 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
715 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
723 /* The following three functions can be used to determine how a
724 structure is laid out by the compiler. For example:
726 struct objc_struct_layout layout;
729 objc_layout_structure (type, &layout);
730 while (objc_layout_structure_next_member (&layout))
735 objc_layout_structure_get_info (&layout, &position, &align, &type);
736 printf ("element %d has offset %d, alignment %d\n",
737 i++, position, align);
740 These functions are used by objc_sizeof_type and objc_alignof_type
741 functions to compute the size and alignment of structures. The
742 previous method of computing the size and alignment of a structure
743 was not working on some architectures, particulary on AIX, and in
744 the presence of bitfields inside the structure. */
746 objc_layout_structure (const char *type
,
747 struct objc_struct_layout
*layout
)
751 if (*type
++ != _C_STRUCT_B
)
753 objc_error (nil
, OBJC_ERR_BAD_TYPE
,
754 "record type expected in objc_layout_structure, got %s\n",
758 layout
->original_type
= type
;
760 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
762 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
766 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
767 if (*(ntype
- 1) == '=')
771 layout
->prev_type
= NULL
;
772 layout
->record_size
= 0;
773 layout
->record_align
= BITS_PER_UNIT
;
775 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
780 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
782 register int desired_align
= 0;
784 /* The following are used only if the field is a bitfield */
785 register const char *bfld_type
= 0;
786 register int bfld_type_size
, bfld_type_align
= 0, bfld_field_size
= 0;
788 /* The current type without the type qualifiers */
791 /* Add the size of the previous field to the size of the record. */
792 if (layout
->prev_type
)
794 type
= objc_skip_type_qualifiers (layout
->prev_type
);
796 if (*type
!= _C_BFLD
)
797 layout
->record_size
+= objc_sizeof_type (type
) * BITS_PER_UNIT
;
799 /* Get the bitfield's type */
800 for (bfld_type
= type
+ 1;
801 isdigit ((unsigned char)*bfld_type
);
805 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
806 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
807 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
808 layout
->record_size
+= bfld_field_size
;
812 if (*layout
->type
== _C_STRUCT_E
)
815 /* Skip the variable name if any */
816 if (*layout
->type
== '"')
818 for (layout
->type
++; *layout
->type
++ != '"';)
822 type
= objc_skip_type_qualifiers (layout
->type
);
824 if (*type
!= _C_BFLD
)
825 desired_align
= objc_alignof_type (type
) * BITS_PER_UNIT
;
829 /* Skip the bitfield's offset */
830 for (bfld_type
= type
+ 1;
831 isdigit ((unsigned char) *bfld_type
);
835 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
836 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
837 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
840 #ifdef BIGGEST_FIELD_ALIGNMENT
841 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
843 #ifdef ADJUST_FIELD_ALIGN
844 desired_align
= ADJUST_FIELD_ALIGN (type
, desired_align
);
847 /* Record must have at least as much alignment as any field.
848 Otherwise, the alignment of the field within the record
850 #ifndef PCC_BITFIELD_TYPE_MATTERS
851 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
852 #else /* PCC_BITFIELD_TYPE_MATTERS */
853 if (*type
== _C_BFLD
)
855 /* For these machines, a zero-length field does not
856 affect the alignment of the structure as a whole.
857 It does, however, affect the alignment of the next field
858 within the structure. */
860 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
862 desired_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
864 /* A named bit field of declared type `int'
865 forces the entire structure to have `int' alignment.
866 Q1: How is encoded this thing and how to check for it?
867 Q2: How to determine maximum_field_alignment at runtime? */
869 /* if (DECL_NAME (field) != 0) */
871 int type_align
= bfld_type_align
;
873 if (maximum_field_alignment
!= 0)
874 type_align
= MIN (type_align
, maximum_field_alignment
);
875 else if (DECL_PACKED (field
))
876 type_align
= MIN (type_align
, BITS_PER_UNIT
);
879 layout
->record_align
= MAX (layout
->record_align
, type_align
);
883 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
884 #endif /* PCC_BITFIELD_TYPE_MATTERS */
886 /* Does this field automatically have alignment it needs
887 by virtue of the fields that precede it and the record's
890 if (*type
== _C_BFLD
)
891 layout
->record_size
= atoi (type
+ 1);
892 else if (layout
->record_size
% desired_align
!= 0)
894 /* No, we need to skip space before this field.
895 Bump the cumulative size to multiple of field alignment. */
896 layout
->record_size
= ROUND (layout
->record_size
, desired_align
);
899 /* Jump to the next field in record. */
901 layout
->prev_type
= layout
->type
;
902 layout
->type
= objc_skip_typespec (layout
->type
); /* skip component */
908 void objc_layout_finish_structure (struct objc_struct_layout
*layout
,
912 if (layout
->type
&& *layout
->type
== _C_STRUCT_E
)
914 /* Work out the alignment of the record as one expression and store
915 in the record type. Round it up to a multiple of the record's
918 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
919 layout
->record_align
= ROUND_TYPE_ALIGN (layout
->original_type
,
921 layout
->record_align
);
923 layout
->record_align
= MAX (1, layout
->record_align
);
926 #ifdef ROUND_TYPE_SIZE
927 layout
->record_size
= ROUND_TYPE_SIZE (layout
->original_type
,
929 layout
->record_align
);
931 /* Round the size up to be a multiple of the required alignment */
932 layout
->record_size
= ROUND (layout
->record_size
, layout
->record_align
);
938 *size
= layout
->record_size
/ BITS_PER_UNIT
;
940 *align
= layout
->record_align
/ BITS_PER_UNIT
;
944 void objc_layout_structure_get_info (struct objc_struct_layout
*layout
,
945 unsigned int *offset
,
950 *offset
= layout
->record_size
/ BITS_PER_UNIT
;
952 *align
= layout
->record_align
/ BITS_PER_UNIT
;
954 *type
= layout
->prev_type
;