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 static int __attribute__ ((__unused__
)) target_flags
= 0;
93 /* FIXME: while this file has no business including tm.h, this
94 definitely has no business defining this macro but it
95 is only way around without really rewritting this file,
96 should look after the branch of 3.4 to fix this. */
97 #define rs6000_special_round_type_align(STRUCT, COMPUTED, SPECIFIED) \
98 ((TYPE_FIELDS (STRUCT) != 0 \
99 && DECL_MODE (TYPE_FIELDS (STRUCT)) == DFmode) \
100 ? MAX (MAX (COMPUTED, SPECIFIED), 64) \
101 : MAX (COMPUTED, SPECIFIED))
104 return the size of an object specified by type
108 objc_sizeof_type (const char *type
)
110 /* Skip the variable name if any */
113 for (type
++; *type
++ != '"';)
123 return sizeof (Class
);
131 return sizeof (char);
135 return sizeof (unsigned char);
139 return sizeof (short);
143 return sizeof (unsigned short);
151 return sizeof (unsigned int);
155 return sizeof (long);
159 return sizeof (unsigned long);
163 return sizeof (long long);
167 return sizeof (unsigned long long);
171 return sizeof (float);
175 return sizeof (double);
179 return sizeof (void);
185 return sizeof (char *);
190 int len
= atoi (type
+ 1);
191 while (isdigit ((unsigned char)*++type
))
193 return len
* objc_aligned_size (type
);
199 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
201 int startByte
, endByte
;
203 position
= atoi (type
+ 1);
204 while (isdigit ((unsigned char)*++type
))
206 size
= atoi (type
+ 1);
208 startByte
= position
/ BITS_PER_UNIT
;
209 endByte
= (position
+ size
) / BITS_PER_UNIT
;
210 return endByte
- startByte
;
215 struct objc_struct_layout layout
;
218 objc_layout_structure (type
, &layout
);
219 while (objc_layout_structure_next_member (&layout
))
221 objc_layout_finish_structure (&layout
, &size
, NULL
);
229 while (*type
!= _C_UNION_E
&& *type
++ != '=')
231 while (*type
!= _C_UNION_E
)
233 /* Skip the variable name if any */
236 for (type
++; *type
++ != '"';)
239 max_size
= MAX (max_size
, objc_sizeof_type (type
));
240 type
= objc_skip_typespec (type
);
247 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
255 Return the alignment of an object specified by type
259 objc_alignof_type (const char *type
)
261 /* Skip the variable name if any */
264 for (type
++; *type
++ != '"';)
269 return __alignof__ (id
);
273 return __alignof__ (Class
);
277 return __alignof__ (SEL
);
281 return __alignof__ (char);
285 return __alignof__ (unsigned char);
289 return __alignof__ (short);
293 return __alignof__ (unsigned short);
297 return __alignof__ (int);
301 return __alignof__ (unsigned int);
305 return __alignof__ (long);
309 return __alignof__ (unsigned long);
313 return __alignof__ (long long);
317 return __alignof__ (unsigned long long);
321 return __alignof__ (float);
325 return __alignof__ (double);
331 return __alignof__ (char *);
335 while (isdigit ((unsigned char)*++type
))
337 return objc_alignof_type (type
);
341 struct objc_struct_layout layout
;
344 objc_layout_structure (type
, &layout
);
345 while (objc_layout_structure_next_member (&layout
))
347 objc_layout_finish_structure (&layout
, NULL
, &align
);
355 while (*type
!= _C_UNION_E
&& *type
++ != '=')
357 while (*type
!= _C_UNION_E
)
359 /* Skip the variable name if any */
362 for (type
++; *type
++ != '"';)
365 maxalign
= MAX (maxalign
, objc_alignof_type (type
));
366 type
= objc_skip_typespec (type
);
373 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
380 The aligned size if the size rounded up to the nearest alignment.
384 objc_aligned_size (const char *type
)
388 /* Skip the variable name */
391 for (type
++; *type
++ != '"';)
395 size
= objc_sizeof_type (type
);
396 align
= objc_alignof_type (type
);
398 return ROUND (size
, align
);
402 The size rounded up to the nearest integral of the wordsize, taken
403 to be the size of a void *.
407 objc_promoted_size (const char *type
)
411 /* Skip the variable name */
414 for (type
++; *type
++ != '"';)
418 size
= objc_sizeof_type (type
);
419 wordsize
= sizeof (void *);
421 return ROUND (size
, wordsize
);
425 Skip type qualifiers. These may eventually precede typespecs
426 occurring in method prototype encodings.
430 objc_skip_type_qualifiers (const char *type
)
432 while (*type
== _C_CONST
436 || *type
== _C_BYCOPY
438 || *type
== _C_ONEWAY
439 || *type
== _C_GCINVISIBLE
)
448 Skip one typespec element. If the typespec is prepended by type
449 qualifiers, these are skipped as well.
453 objc_skip_typespec (const char *type
)
455 /* Skip the variable name if any */
458 for (type
++; *type
++ != '"';)
462 type
= objc_skip_type_qualifiers (type
);
467 /* An id may be annotated by the actual type if it is known
468 with the @"ClassName" syntax */
474 while (*++type
!= '"')
479 /* The following are one character type codes */
502 /* skip digits, typespec and closing ']' */
504 while (isdigit ((unsigned char)*++type
))
506 type
= objc_skip_typespec (type
);
507 if (*type
== _C_ARY_E
)
511 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "bad array type %s\n", type
);
516 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
517 while (isdigit ((unsigned char)*++type
))
518 ; /* skip position */
519 while (isdigit ((unsigned char)*++type
))
520 ; /* skip type and size */
524 /* skip name, and elements until closing '}' */
526 while (*type
!= _C_STRUCT_E
&& *type
++ != '=')
528 while (*type
!= _C_STRUCT_E
)
530 type
= objc_skip_typespec (type
);
535 /* skip name, and elements until closing ')' */
537 while (*type
!= _C_UNION_E
&& *type
++ != '=')
539 while (*type
!= _C_UNION_E
)
541 type
= objc_skip_typespec (type
);
546 /* Just skip the following typespec */
548 return objc_skip_typespec (++type
);
552 objc_error (nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
559 Skip an offset as part of a method encoding. This is prepended by a
560 '+' if the argument is passed in registers.
563 objc_skip_offset (const char *type
)
567 while (isdigit ((unsigned char) *++type
))
573 Skip an argument specification of a method encoding.
576 objc_skip_argspec (const char *type
)
578 type
= objc_skip_typespec (type
);
579 type
= objc_skip_offset (type
);
584 Return the number of arguments that the method MTH expects.
585 Note that all methods need two implicit arguments `self' and
589 method_get_number_of_arguments (struct objc_method
*mth
)
592 const char *type
= mth
->method_types
;
595 type
= objc_skip_argspec (type
);
602 Return the size of the argument block needed on the stack to invoke
603 the method MTH. This may be zero, if all arguments are passed in
608 method_get_sizeof_arguments (struct objc_method
*mth
)
610 const char *type
= objc_skip_typespec (mth
->method_types
);
615 Return a pointer to the next argument of ARGFRAME. type points to
616 the last argument. Typical use of this look like:
620 for (datum = method_get_first_argument (method, argframe, &type);
621 datum; datum = method_get_next_argument (argframe, &type))
623 unsigned flags = objc_get_type_qualifiers (type);
624 type = objc_skip_type_qualifiers (type);
626 [portal encodeData: datum ofType: type];
629 if ((flags & _F_IN) == _F_IN)
630 [portal encodeData: *(char **) datum ofType: ++type];
637 method_get_next_argument (arglist_t argframe
, const char **type
)
639 const char *t
= objc_skip_argspec (*type
);
645 t
= objc_skip_typespec (t
);
648 return argframe
->arg_regs
+ atoi (++t
);
650 return argframe
->arg_ptr
+ atoi (t
);
654 Return a pointer to the value of the first argument of the method
655 described in M with the given argumentframe ARGFRAME. The type
656 is returned in TYPE. type must be passed to successive calls of
657 method_get_next_argument.
660 method_get_first_argument (struct objc_method
*m
,
664 *type
= m
->method_types
;
665 return method_get_next_argument (argframe
, type
);
669 Return a pointer to the ARGth argument of the method
670 M from the frame ARGFRAME. The type of the argument
671 is returned in the value-result argument TYPE
675 method_get_nth_argument (struct objc_method
*m
,
676 arglist_t argframe
, int arg
,
679 const char *t
= objc_skip_argspec (m
->method_types
);
681 if (arg
> method_get_number_of_arguments (m
))
685 t
= objc_skip_argspec (t
);
688 t
= objc_skip_typespec (t
);
691 return argframe
->arg_regs
+ atoi (++t
);
693 return argframe
->arg_ptr
+ atoi (t
);
697 objc_get_type_qualifiers (const char *type
)
705 case _C_CONST
: res
|= _F_CONST
; break;
706 case _C_IN
: res
|= _F_IN
; break;
707 case _C_INOUT
: res
|= _F_INOUT
; break;
708 case _C_OUT
: res
|= _F_OUT
; break;
709 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
710 case _C_BYREF
: res
|= _F_BYREF
; break;
711 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
712 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
720 /* The following three functions can be used to determine how a
721 structure is laid out by the compiler. For example:
723 struct objc_struct_layout layout;
726 objc_layout_structure (type, &layout);
727 while (objc_layout_structure_next_member (&layout))
732 objc_layout_structure_get_info (&layout, &position, &align, &type);
733 printf ("element %d has offset %d, alignment %d\n",
734 i++, position, align);
737 These functions are used by objc_sizeof_type and objc_alignof_type
738 functions to compute the size and alignment of structures. The
739 previous method of computing the size and alignment of a structure
740 was not working on some architectures, particulary on AIX, and in
741 the presence of bitfields inside the structure. */
743 objc_layout_structure (const char *type
,
744 struct objc_struct_layout
*layout
)
748 if (*type
++ != _C_STRUCT_B
)
750 objc_error (nil
, OBJC_ERR_BAD_TYPE
,
751 "record type expected in objc_layout_structure, got %s\n",
755 layout
->original_type
= type
;
757 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
759 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
763 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
764 if (*(ntype
- 1) == '=')
768 layout
->prev_type
= NULL
;
769 layout
->record_size
= 0;
770 layout
->record_align
= BITS_PER_UNIT
;
772 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
777 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
779 register int desired_align
= 0;
781 /* The following are used only if the field is a bitfield */
782 register const char *bfld_type
= 0;
783 register int bfld_type_size
, bfld_type_align
= 0, bfld_field_size
= 0;
785 /* The current type without the type qualifiers */
788 /* Add the size of the previous field to the size of the record. */
789 if (layout
->prev_type
)
791 type
= objc_skip_type_qualifiers (layout
->prev_type
);
793 if (*type
!= _C_BFLD
)
794 layout
->record_size
+= objc_sizeof_type (type
) * BITS_PER_UNIT
;
796 /* Get the bitfield's type */
797 for (bfld_type
= type
+ 1;
798 isdigit ((unsigned char)*bfld_type
);
802 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
803 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
804 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
805 layout
->record_size
+= bfld_field_size
;
809 if (*layout
->type
== _C_STRUCT_E
)
812 /* Skip the variable name if any */
813 if (*layout
->type
== '"')
815 for (layout
->type
++; *layout
->type
++ != '"';)
819 type
= objc_skip_type_qualifiers (layout
->type
);
821 if (*type
!= _C_BFLD
)
822 desired_align
= objc_alignof_type (type
) * BITS_PER_UNIT
;
826 /* Skip the bitfield's offset */
827 for (bfld_type
= type
+ 1;
828 isdigit ((unsigned char) *bfld_type
);
832 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
833 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
834 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
837 #ifdef BIGGEST_FIELD_ALIGNMENT
838 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
840 #ifdef ADJUST_FIELD_ALIGN
841 desired_align
= ADJUST_FIELD_ALIGN (type
, desired_align
);
844 /* Record must have at least as much alignment as any field.
845 Otherwise, the alignment of the field within the record
847 #ifndef PCC_BITFIELD_TYPE_MATTERS
848 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
849 #else /* PCC_BITFIELD_TYPE_MATTERS */
850 if (*type
== _C_BFLD
)
852 /* For these machines, a zero-length field does not
853 affect the alignment of the structure as a whole.
854 It does, however, affect the alignment of the next field
855 within the structure. */
857 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
859 desired_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
861 /* A named bit field of declared type `int'
862 forces the entire structure to have `int' alignment.
863 Q1: How is encoded this thing and how to check for it?
864 Q2: How to determine maximum_field_alignment at runtime? */
866 /* if (DECL_NAME (field) != 0) */
868 int type_align
= bfld_type_align
;
870 if (maximum_field_alignment
!= 0)
871 type_align
= MIN (type_align
, maximum_field_alignment
);
872 else if (DECL_PACKED (field
))
873 type_align
= MIN (type_align
, BITS_PER_UNIT
);
876 layout
->record_align
= MAX (layout
->record_align
, type_align
);
880 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
881 #endif /* PCC_BITFIELD_TYPE_MATTERS */
883 /* Does this field automatically have alignment it needs
884 by virtue of the fields that precede it and the record's
887 if (*type
== _C_BFLD
)
888 layout
->record_size
= atoi (type
+ 1);
889 else if (layout
->record_size
% desired_align
!= 0)
891 /* No, we need to skip space before this field.
892 Bump the cumulative size to multiple of field alignment. */
893 layout
->record_size
= ROUND (layout
->record_size
, desired_align
);
896 /* Jump to the next field in record. */
898 layout
->prev_type
= layout
->type
;
899 layout
->type
= objc_skip_typespec (layout
->type
); /* skip component */
905 void objc_layout_finish_structure (struct objc_struct_layout
*layout
,
909 if (layout
->type
&& *layout
->type
== _C_STRUCT_E
)
911 /* Work out the alignment of the record as one expression and store
912 in the record type. Round it up to a multiple of the record's
915 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
916 layout
->record_align
= ROUND_TYPE_ALIGN (layout
->original_type
,
918 layout
->record_align
);
920 layout
->record_align
= MAX (1, layout
->record_align
);
923 #ifdef ROUND_TYPE_SIZE
924 layout
->record_size
= ROUND_TYPE_SIZE (layout
->original_type
,
926 layout
->record_align
);
928 /* Round the size up to be a multiple of the required alignment */
929 layout
->record_size
= ROUND (layout
->record_size
, layout
->record_align
);
935 *size
= layout
->record_size
/ BITS_PER_UNIT
;
937 *align
= layout
->record_align
/ BITS_PER_UNIT
;
941 void objc_layout_structure_get_info (struct objc_struct_layout
*layout
,
942 unsigned int *offset
,
947 *offset
= layout
->record_size
/ BITS_PER_UNIT
;
949 *align
= layout
->record_align
/ BITS_PER_UNIT
;
951 *type
= layout
->prev_type
;