1 /* Encoding of types for Objective C.
2 Copyright (C) 1993, 1995, 1996, 1997, 1998, 2000, 2002
3 Free Software Foundation, Inc.
4 Contributed by Kresten Krab Thorup
5 Bitfield support by Ovidiu Predescu
7 This file is part of GNU CC.
9 GNU CC 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 GNU CC 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 GNU CC; 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. */
37 ({ typeof (X) __x = (X), __y = (Y); \
38 (__x > __y ? __x : __y); })
42 ({ typeof (X) __x = (X), __y = (Y); \
43 (__x < __y ? __x : __y); })
47 ({ typeof (V) __v = (V); typeof (A) __a = (A); \
48 __a * ((__v+__a - 1)/__a); })
51 /* Various hacks for objc_layout_record. These are used by the target
54 #define TREE_CODE(TYPE) *(TYPE)
55 #define TREE_TYPE(TREE) (TREE)
57 #define RECORD_TYPE _C_STRUCT_B
58 #define UNION_TYPE _C_UNION_B
59 #define QUAL_UNION_TYPE _C_UNION_B
60 #define ARRAY_TYPE _C_ARY_B
62 #define REAL_TYPE _C_DBL
64 #define VECTOR_TYPE _C_VECTOR
66 #define TYPE_FIELDS(TYPE) objc_skip_typespec (TYPE)
68 #define DECL_MODE(TYPE) *(TYPE)
69 #define TYPE_MODE(TYPE) *(TYPE)
73 #define get_inner_array_type(TYPE) ((TYPE) + 1)
75 /* Some ports (eg ARM) allow the structure size boundary to be
76 selected at compile-time. We override the normal definition with
77 one that has a constant value for this compilation. */
78 #undef STRUCTURE_SIZE_BOUNDARY
79 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
81 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
82 target_flags. Define a dummy entry here to so we don't die. */
83 /* ??? FIXME: As of 2002-06-21, the attribute `unused' doesn't seem to
84 eliminate the warning. */
85 static int __attribute__ ((__unused__
)) target_flags
= 0;
88 return the size of an object specified by type
92 objc_sizeof_type (const char *type
)
94 /* Skip the variable name if any */
97 for (type
++; *type
++ != '"';)
107 return sizeof (Class
);
115 return sizeof (char);
119 return sizeof (unsigned char);
123 return sizeof (short);
127 return sizeof (unsigned short);
135 return sizeof (unsigned int);
139 return sizeof (long);
143 return sizeof (unsigned long);
147 return sizeof (long long);
151 return sizeof (unsigned long long);
155 return sizeof (float);
159 return sizeof (double);
163 return sizeof (void);
169 return sizeof (char *);
174 int len
= atoi (type
+ 1);
175 while (isdigit ((unsigned char)*++type
))
177 return len
* objc_aligned_size (type
);
183 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
185 int startByte
, endByte
;
187 position
= atoi (type
+ 1);
188 while (isdigit ((unsigned char)*++type
))
190 size
= atoi (type
+ 1);
192 startByte
= position
/ BITS_PER_UNIT
;
193 endByte
= (position
+ size
) / BITS_PER_UNIT
;
194 return endByte
- startByte
;
199 struct objc_struct_layout layout
;
202 objc_layout_structure (type
, &layout
);
203 while (objc_layout_structure_next_member (&layout
))
205 objc_layout_finish_structure (&layout
, &size
, NULL
);
213 while (*type
!= _C_UNION_E
&& *type
++ != '=')
215 while (*type
!= _C_UNION_E
)
217 /* Skip the variable name if any */
220 for (type
++; *type
++ != '"';)
223 max_size
= MAX (max_size
, objc_sizeof_type (type
));
224 type
= objc_skip_typespec (type
);
231 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
239 Return the alignment of an object specified by type
243 objc_alignof_type (const char *type
)
245 /* Skip the variable name if any */
248 for (type
++; *type
++ != '"';)
253 return __alignof__ (id
);
257 return __alignof__ (Class
);
261 return __alignof__ (SEL
);
265 return __alignof__ (char);
269 return __alignof__ (unsigned char);
273 return __alignof__ (short);
277 return __alignof__ (unsigned short);
281 return __alignof__ (int);
285 return __alignof__ (unsigned int);
289 return __alignof__ (long);
293 return __alignof__ (unsigned long);
297 return __alignof__ (long long);
301 return __alignof__ (unsigned long long);
305 return __alignof__ (float);
309 return __alignof__ (double);
315 return __alignof__ (char *);
319 while (isdigit ((unsigned char)*++type
))
321 return objc_alignof_type (type
);
325 struct objc_struct_layout layout
;
328 objc_layout_structure (type
, &layout
);
329 while (objc_layout_structure_next_member (&layout
))
331 objc_layout_finish_structure (&layout
, NULL
, &align
);
339 while (*type
!= _C_UNION_E
&& *type
++ != '=')
341 while (*type
!= _C_UNION_E
)
343 /* Skip the variable name if any */
346 for (type
++; *type
++ != '"';)
349 maxalign
= MAX (maxalign
, objc_alignof_type (type
));
350 type
= objc_skip_typespec (type
);
357 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
364 The aligned size if the size rounded up to the nearest alignment.
368 objc_aligned_size (const char *type
)
372 /* Skip the variable name */
375 for (type
++; *type
++ != '"';)
379 size
= objc_sizeof_type (type
);
380 align
= objc_alignof_type (type
);
382 return ROUND (size
, align
);
386 The size rounded up to the nearest integral of the wordsize, taken
387 to be the size of a void *.
391 objc_promoted_size (const char *type
)
395 /* Skip the variable name */
398 for (type
++; *type
++ != '"';)
402 size
= objc_sizeof_type (type
);
403 wordsize
= sizeof (void *);
405 return ROUND (size
, wordsize
);
409 Skip type qualifiers. These may eventually precede typespecs
410 occurring in method prototype encodings.
414 objc_skip_type_qualifiers (const char *type
)
416 while (*type
== _C_CONST
420 || *type
== _C_BYCOPY
422 || *type
== _C_ONEWAY
423 || *type
== _C_GCINVISIBLE
)
432 Skip one typespec element. If the typespec is prepended by type
433 qualifiers, these are skipped as well.
437 objc_skip_typespec (const char *type
)
439 /* Skip the variable name if any */
442 for (type
++; *type
++ != '"';)
446 type
= objc_skip_type_qualifiers (type
);
451 /* An id may be annotated by the actual type if it is known
452 with the @"ClassName" syntax */
458 while (*++type
!= '"')
463 /* The following are one character type codes */
486 /* skip digits, typespec and closing ']' */
488 while (isdigit ((unsigned char)*++type
))
490 type
= objc_skip_typespec (type
);
491 if (*type
== _C_ARY_E
)
495 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "bad array type %s\n", type
);
500 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
501 while (isdigit ((unsigned char)*++type
))
502 ; /* skip position */
503 while (isdigit ((unsigned char)*++type
))
504 ; /* skip type and size */
508 /* skip name, and elements until closing '}' */
510 while (*type
!= _C_STRUCT_E
&& *type
++ != '=')
512 while (*type
!= _C_STRUCT_E
)
514 type
= objc_skip_typespec (type
);
519 /* skip name, and elements until closing ')' */
521 while (*type
!= _C_UNION_E
&& *type
++ != '=')
523 while (*type
!= _C_UNION_E
)
525 type
= objc_skip_typespec (type
);
530 /* Just skip the following typespec */
532 return objc_skip_typespec (++type
);
536 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
543 Skip an offset as part of a method encoding. This is prepended by a
544 '+' if the argument is passed in registers.
547 objc_skip_offset (const char *type
)
551 while (isdigit ((unsigned char) *++type
))
557 Skip an argument specification of a method encoding.
560 objc_skip_argspec (const char *type
)
562 type
= objc_skip_typespec (type
);
563 type
= objc_skip_offset (type
);
568 Return the number of arguments that the method MTH expects.
569 Note that all methods need two implicit arguments `self' and
573 method_get_number_of_arguments (struct objc_method
*mth
)
576 const char *type
= mth
->method_types
;
579 type
= objc_skip_argspec (type
);
586 Return the size of the argument block needed on the stack to invoke
587 the method MTH. This may be zero, if all arguments are passed in
592 method_get_sizeof_arguments (struct objc_method
*mth
)
594 const char *type
= objc_skip_typespec (mth
->method_types
);
599 Return a pointer to the next argument of ARGFRAME. type points to
600 the last argument. Typical use of this look like:
604 for (datum = method_get_first_argument (method, argframe, &type);
605 datum; datum = method_get_next_argument (argframe, &type))
607 unsigned flags = objc_get_type_qualifiers (type);
608 type = objc_skip_type_qualifiers (type);
610 [portal encodeData: datum ofType: type];
613 if ((flags & _F_IN) == _F_IN)
614 [portal encodeData: *(char **) datum ofType: ++type];
621 method_get_next_argument (arglist_t argframe
, const char **type
)
623 const char *t
= objc_skip_argspec (*type
);
629 t
= objc_skip_typespec (t
);
632 return argframe
->arg_regs
+ atoi (++t
);
634 return argframe
->arg_ptr
+ atoi (t
);
638 Return a pointer to the value of the first argument of the method
639 described in M with the given argumentframe ARGFRAME. The type
640 is returned in TYPE. type must be passed to successive calls of
641 method_get_next_argument.
644 method_get_first_argument (struct objc_method
*m
,
648 *type
= m
->method_types
;
649 return method_get_next_argument (argframe
, type
);
653 Return a pointer to the ARGth argument of the method
654 M from the frame ARGFRAME. The type of the argument
655 is returned in the value-result argument TYPE
659 method_get_nth_argument (struct objc_method
*m
,
660 arglist_t argframe
, int arg
,
663 const char *t
= objc_skip_argspec (m
->method_types
);
665 if (arg
> method_get_number_of_arguments (m
))
669 t
= objc_skip_argspec (t
);
672 t
= objc_skip_typespec (t
);
675 return argframe
->arg_regs
+ atoi (++t
);
677 return argframe
->arg_ptr
+ atoi (t
);
681 objc_get_type_qualifiers (const char *type
)
689 case _C_CONST
: res
|= _F_CONST
; break;
690 case _C_IN
: res
|= _F_IN
; break;
691 case _C_INOUT
: res
|= _F_INOUT
; break;
692 case _C_OUT
: res
|= _F_OUT
; break;
693 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
694 case _C_BYREF
: res
|= _F_BYREF
; break;
695 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
696 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
704 /* The following three functions can be used to determine how a
705 structure is laid out by the compiler. For example:
707 struct objc_struct_layout layout;
710 objc_layout_structure (type, &layout);
711 while (objc_layout_structure_next_member (&layout))
716 objc_layout_structure_get_info (&layout, &position, &align, &type);
717 printf ("element %d has offset %d, alignment %d\n",
718 i++, position, align);
721 These functions are used by objc_sizeof_type and objc_alignof_type
722 functions to compute the size and alignment of structures. The
723 previous method of computing the size and alignment of a structure
724 was not working on some architectures, particulary on AIX, and in
725 the presence of bitfields inside the structure. */
727 objc_layout_structure (const char *type
,
728 struct objc_struct_layout
*layout
)
732 if (*type
++ != _C_STRUCT_B
)
734 objc_error (nil
, OBJC_ERR_BAD_TYPE
,
735 "record type expected in objc_layout_structure, got %s\n",
739 layout
->original_type
= type
;
741 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
743 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
747 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
748 if (*(ntype
- 1) == '=')
752 layout
->prev_type
= NULL
;
753 layout
->record_size
= 0;
754 layout
->record_align
= BITS_PER_UNIT
;
756 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
761 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
763 register int desired_align
= 0;
765 /* The following are used only if the field is a bitfield */
766 register const char *bfld_type
= 0;
767 register int bfld_type_size
, bfld_type_align
= 0, bfld_field_size
= 0;
769 /* The current type without the type qualifiers */
772 /* Add the size of the previous field to the size of the record. */
773 if (layout
->prev_type
)
775 type
= objc_skip_type_qualifiers (layout
->prev_type
);
777 if (*type
!= _C_BFLD
)
778 layout
->record_size
+= objc_sizeof_type (type
) * BITS_PER_UNIT
;
780 /* Get the bitfield's type */
781 for (bfld_type
= type
+ 1;
782 isdigit ((unsigned char)*bfld_type
);
786 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
787 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
788 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
789 layout
->record_size
+= bfld_field_size
;
793 if (*layout
->type
== _C_STRUCT_E
)
796 /* Skip the variable name if any */
797 if (*layout
->type
== '"')
799 for (layout
->type
++; *layout
->type
++ != '"';)
803 type
= objc_skip_type_qualifiers (layout
->type
);
805 if (*type
!= _C_BFLD
)
806 desired_align
= objc_alignof_type (type
) * BITS_PER_UNIT
;
810 /* Skip the bitfield's offset */
811 for (bfld_type
= type
+ 1;
812 isdigit ((unsigned char) *bfld_type
);
816 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
817 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
818 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
821 #ifdef BIGGEST_FIELD_ALIGNMENT
822 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
824 #ifdef ADJUST_FIELD_ALIGN
825 desired_align
= ADJUST_FIELD_ALIGN (type
, desired_align
);
828 /* Record must have at least as much alignment as any field.
829 Otherwise, the alignment of the field within the record
831 #ifndef PCC_BITFIELD_TYPE_MATTERS
832 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
833 #else /* PCC_BITFIELD_TYPE_MATTERS */
834 if (*type
== _C_BFLD
)
836 /* For these machines, a zero-length field does not
837 affect the alignment of the structure as a whole.
838 It does, however, affect the alignment of the next field
839 within the structure. */
841 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
843 desired_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
845 /* A named bit field of declared type `int'
846 forces the entire structure to have `int' alignment.
847 Q1: How is encoded this thing and how to check for it?
848 Q2: How to determine maximum_field_alignment at runtime? */
850 /* if (DECL_NAME (field) != 0) */
852 int type_align
= bfld_type_align
;
854 if (maximum_field_alignment
!= 0)
855 type_align
= MIN (type_align
, maximum_field_alignment
);
856 else if (DECL_PACKED (field
))
857 type_align
= MIN (type_align
, BITS_PER_UNIT
);
860 layout
->record_align
= MAX (layout
->record_align
, type_align
);
864 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
865 #endif /* PCC_BITFIELD_TYPE_MATTERS */
867 /* Does this field automatically have alignment it needs
868 by virtue of the fields that precede it and the record's
871 if (*type
== _C_BFLD
)
872 layout
->record_size
= atoi (type
+ 1);
873 else if (layout
->record_size
% desired_align
!= 0)
875 /* No, we need to skip space before this field.
876 Bump the cumulative size to multiple of field alignment. */
877 layout
->record_size
= ROUND (layout
->record_size
, desired_align
);
880 /* Jump to the next field in record. */
882 layout
->prev_type
= layout
->type
;
883 layout
->type
= objc_skip_typespec (layout
->type
); /* skip component */
889 void objc_layout_finish_structure (struct objc_struct_layout
*layout
,
893 if (layout
->type
&& *layout
->type
== _C_STRUCT_E
)
895 /* Work out the alignment of the record as one expression and store
896 in the record type. Round it up to a multiple of the record's
899 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
900 layout
->record_align
= ROUND_TYPE_ALIGN (layout
->original_type
,
902 layout
->record_align
);
904 layout
->record_align
= MAX (1, layout
->record_align
);
907 #ifdef ROUND_TYPE_SIZE
908 layout
->record_size
= ROUND_TYPE_SIZE (layout
->original_type
,
910 layout
->record_align
);
912 /* Round the size up to be a multiple of the required alignment */
913 layout
->record_size
= ROUND (layout
->record_size
, layout
->record_align
);
919 *size
= layout
->record_size
/ BITS_PER_UNIT
;
921 *align
= layout
->record_align
/ BITS_PER_UNIT
;
925 void objc_layout_structure_get_info (struct objc_struct_layout
*layout
,
926 unsigned int *offset
,
931 *offset
= layout
->record_size
/ BITS_PER_UNIT
;
933 *align
= layout
->record_align
/ BITS_PER_UNIT
;
935 *type
= layout
->prev_type
;