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
);
119 return sizeof(unsigned char);
123 return sizeof(short);
127 return sizeof(unsigned short);
135 return sizeof(unsigned int);
143 return sizeof(unsigned long);
147 return sizeof(long long);
151 return sizeof(unsigned long long);
155 return sizeof(float);
159 return sizeof(double);
168 return sizeof(char*);
173 int len
= atoi(type
+1);
174 while (isdigit((unsigned char)*++type
));
175 return len
*objc_aligned_size (type
);
181 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
183 int startByte
, endByte
;
185 position
= atoi (type
+ 1);
186 while (isdigit ((unsigned char)*++type
));
187 size
= atoi (type
+ 1);
189 startByte
= position
/ BITS_PER_UNIT
;
190 endByte
= (position
+ size
) / BITS_PER_UNIT
;
191 return endByte
- startByte
;
196 struct objc_struct_layout layout
;
199 objc_layout_structure (type
, &layout
);
200 while (objc_layout_structure_next_member (&layout
))
202 objc_layout_finish_structure (&layout
, &size
, NULL
);
210 while (*type
!= _C_UNION_E
&& *type
++ != '=') /* do nothing */;
211 while (*type
!= _C_UNION_E
)
213 /* Skip the variable name if any */
216 for (type
++; *type
++ != '"';)
219 max_size
= MAX (max_size
, objc_sizeof_type (type
));
220 type
= objc_skip_typespec (type
);
227 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
235 Return the alignment of an object specified by type
239 objc_alignof_type(const char* type
)
241 /* Skip the variable name if any */
244 for (type
++; *type
++ != '"';)
249 return __alignof__(id
);
253 return __alignof__(Class
);
257 return __alignof__(SEL
);
261 return __alignof__(char);
265 return __alignof__(unsigned char);
269 return __alignof__(short);
273 return __alignof__(unsigned short);
277 return __alignof__(int);
281 return __alignof__(unsigned int);
285 return __alignof__(long);
289 return __alignof__(unsigned long);
293 return __alignof__(long long);
297 return __alignof__(unsigned long long);
301 return __alignof__(float);
305 return __alignof__(double);
311 return __alignof__(char*);
315 while (isdigit((unsigned char)*++type
)) /* do nothing */;
316 return objc_alignof_type (type
);
320 struct objc_struct_layout layout
;
323 objc_layout_structure (type
, &layout
);
324 while (objc_layout_structure_next_member (&layout
))
326 objc_layout_finish_structure (&layout
, NULL
, &align
);
334 while (*type
!= _C_UNION_E
&& *type
++ != '=') /* do nothing */;
335 while (*type
!= _C_UNION_E
)
337 /* Skip the variable name if any */
340 for (type
++; *type
++ != '"';)
343 maxalign
= MAX (maxalign
, objc_alignof_type (type
));
344 type
= objc_skip_typespec (type
);
351 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
358 The aligned size if the size rounded up to the nearest alignment.
362 objc_aligned_size (const char* type
)
366 /* Skip the variable name */
369 for (type
++; *type
++ != '"';)
373 size
= objc_sizeof_type (type
);
374 align
= objc_alignof_type (type
);
376 return ROUND (size
, align
);
380 The size rounded up to the nearest integral of the wordsize, taken
381 to be the size of a void*.
385 objc_promoted_size (const char* type
)
389 /* Skip the variable name */
392 for (type
++; *type
++ != '"';)
396 size
= objc_sizeof_type (type
);
397 wordsize
= sizeof (void*);
399 return ROUND (size
, wordsize
);
403 Skip type qualifiers. These may eventually precede typespecs
404 occurring in method prototype encodings.
408 objc_skip_type_qualifiers (const char* type
)
410 while (*type
== _C_CONST
414 || *type
== _C_BYCOPY
416 || *type
== _C_ONEWAY
417 || *type
== _C_GCINVISIBLE
)
426 Skip one typespec element. If the typespec is prepended by type
427 qualifiers, these are skipped as well.
431 objc_skip_typespec (const char* type
)
433 /* Skip the variable name if any */
436 for (type
++; *type
++ != '"';)
440 type
= objc_skip_type_qualifiers (type
);
445 /* An id may be annotated by the actual type if it is known
446 with the @"ClassName" syntax */
452 while (*++type
!= '"') /* do nothing */;
456 /* The following are one character type codes */
479 /* skip digits, typespec and closing ']' */
481 while(isdigit((unsigned char)*++type
));
482 type
= objc_skip_typespec(type
);
483 if (*type
== _C_ARY_E
)
487 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "bad array type %s\n", type
);
492 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
493 while (isdigit ((unsigned char)*++type
)); /* skip position */
494 while (isdigit ((unsigned char)*++type
)); /* skip type and size */
498 /* skip name, and elements until closing '}' */
500 while (*type
!= _C_STRUCT_E
&& *type
++ != '=');
501 while (*type
!= _C_STRUCT_E
) { type
= objc_skip_typespec (type
); }
505 /* skip name, and elements until closing ')' */
507 while (*type
!= _C_UNION_E
&& *type
++ != '=');
508 while (*type
!= _C_UNION_E
) { type
= objc_skip_typespec (type
); }
512 /* Just skip the following typespec */
514 return objc_skip_typespec (++type
);
518 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
525 Skip an offset as part of a method encoding. This is prepended by a
526 '+' if the argument is passed in registers.
529 objc_skip_offset (const char* type
)
531 if (*type
== '+') type
++;
532 while(isdigit((unsigned char)*++type
));
537 Skip an argument specification of a method encoding.
540 objc_skip_argspec (const char* type
)
542 type
= objc_skip_typespec (type
);
543 type
= objc_skip_offset (type
);
548 Return the number of arguments that the method MTH expects.
549 Note that all methods need two implicit arguments `self' and
553 method_get_number_of_arguments (struct objc_method
* mth
)
556 const char* type
= mth
->method_types
;
559 type
= objc_skip_argspec (type
);
566 Return the size of the argument block needed on the stack to invoke
567 the method MTH. This may be zero, if all arguments are passed in
572 method_get_sizeof_arguments (struct objc_method
* mth
)
574 const char* type
= objc_skip_typespec (mth
->method_types
);
579 Return a pointer to the next argument of ARGFRAME. type points to
580 the last argument. Typical use of this look like:
584 for (datum = method_get_first_argument (method, argframe, &type);
585 datum; datum = method_get_next_argument (argframe, &type))
587 unsigned flags = objc_get_type_qualifiers (type);
588 type = objc_skip_type_qualifiers (type);
590 [portal encodeData: datum ofType: type];
593 if ((flags & _F_IN) == _F_IN)
594 [portal encodeData: *(char**)datum ofType: ++type];
601 method_get_next_argument (arglist_t argframe
,
604 const char *t
= objc_skip_argspec (*type
);
610 t
= objc_skip_typespec (t
);
613 return argframe
->arg_regs
+ atoi (++t
);
615 return argframe
->arg_ptr
+ atoi (t
);
619 Return a pointer to the value of the first argument of the method
620 described in M with the given argumentframe ARGFRAME. The type
621 is returned in TYPE. type must be passed to successive calls of
622 method_get_next_argument.
625 method_get_first_argument (struct objc_method
* m
,
629 *type
= m
->method_types
;
630 return method_get_next_argument (argframe
, type
);
634 Return a pointer to the ARGth argument of the method
635 M from the frame ARGFRAME. The type of the argument
636 is returned in the value-result argument TYPE
640 method_get_nth_argument (struct objc_method
* m
,
641 arglist_t argframe
, int arg
,
644 const char* t
= objc_skip_argspec (m
->method_types
);
646 if (arg
> method_get_number_of_arguments (m
))
650 t
= objc_skip_argspec (t
);
653 t
= objc_skip_typespec (t
);
656 return argframe
->arg_regs
+ atoi (++t
);
658 return argframe
->arg_ptr
+ atoi (t
);
662 objc_get_type_qualifiers (const char* type
)
670 case _C_CONST
: res
|= _F_CONST
; break;
671 case _C_IN
: res
|= _F_IN
; break;
672 case _C_INOUT
: res
|= _F_INOUT
; break;
673 case _C_OUT
: res
|= _F_OUT
; break;
674 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
675 case _C_BYREF
: res
|= _F_BYREF
; break;
676 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
677 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
685 /* The following three functions can be used to determine how a
686 structure is laid out by the compiler. For example:
688 struct objc_struct_layout layout;
691 objc_layout_structure (type, &layout);
692 while (objc_layout_structure_next_member (&layout))
697 objc_layout_structure_get_info (&layout, &position, &align, &type);
698 printf ("element %d has offset %d, alignment %d\n",
699 i++, position, align);
702 These functions are used by objc_sizeof_type and objc_alignof_type
703 functions to compute the size and alignment of structures. The
704 previous method of computing the size and alignment of a structure
705 was not working on some architectures, particulary on AIX, and in
706 the presence of bitfields inside the structure. */
708 objc_layout_structure (const char *type
,
709 struct objc_struct_layout
*layout
)
713 if (*type
++ != _C_STRUCT_B
)
715 objc_error(nil
, OBJC_ERR_BAD_TYPE
,
716 "record type expected in objc_layout_structure, got %s\n",
720 layout
->original_type
= type
;
722 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
724 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
728 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
729 if (*(ntype
- 1) == '=')
733 layout
->prev_type
= NULL
;
734 layout
->record_size
= 0;
735 layout
->record_align
= BITS_PER_UNIT
;
737 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
742 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
744 register int desired_align
= 0;
746 /* The following are used only if the field is a bitfield */
747 register const char *bfld_type
= 0;
748 register int bfld_type_size
, bfld_type_align
= 0, bfld_field_size
= 0;
750 /* The current type without the type qualifiers */
753 /* Add the size of the previous field to the size of the record. */
754 if (layout
->prev_type
)
756 type
= objc_skip_type_qualifiers (layout
->prev_type
);
758 if (*type
!= _C_BFLD
)
759 layout
->record_size
+= objc_sizeof_type (type
) * BITS_PER_UNIT
;
761 /* Get the bitfield's type */
762 for (bfld_type
= type
+ 1;
763 isdigit((unsigned char)*bfld_type
);
767 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
768 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
769 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
770 layout
->record_size
+= bfld_field_size
;
774 if (*layout
->type
== _C_STRUCT_E
)
777 /* Skip the variable name if any */
778 if (*layout
->type
== '"')
780 for (layout
->type
++; *layout
->type
++ != '"';)
784 type
= objc_skip_type_qualifiers (layout
->type
);
786 if (*type
!= _C_BFLD
)
787 desired_align
= objc_alignof_type(type
) * BITS_PER_UNIT
;
791 /* Skip the bitfield's offset */
792 for (bfld_type
= type
+ 1; isdigit((unsigned char)*bfld_type
); bfld_type
++)
795 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
796 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
797 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
800 #ifdef BIGGEST_FIELD_ALIGNMENT
801 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
803 #ifdef ADJUST_FIELD_ALIGN
804 desired_align
= ADJUST_FIELD_ALIGN (type
, desired_align
);
807 /* Record must have at least as much alignment as any field.
808 Otherwise, the alignment of the field within the record
810 #ifndef PCC_BITFIELD_TYPE_MATTERS
811 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
813 if (*type
== _C_BFLD
)
815 /* For these machines, a zero-length field does not
816 affect the alignment of the structure as a whole.
817 It does, however, affect the alignment of the next field
818 within the structure. */
820 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
822 desired_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
824 /* A named bit field of declared type `int'
825 forces the entire structure to have `int' alignment.
826 Q1: How is encoded this thing and how to check for it?
827 Q2: How to determine maximum_field_alignment at runtime? */
829 /* if (DECL_NAME (field) != 0) */
831 int type_align
= bfld_type_align
;
833 if (maximum_field_alignment
!= 0)
834 type_align
= MIN (type_align
, maximum_field_alignment
);
835 else if (DECL_PACKED (field
))
836 type_align
= MIN (type_align
, BITS_PER_UNIT
);
839 layout
->record_align
= MAX (layout
->record_align
, type_align
);
843 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
846 /* Does this field automatically have alignment it needs
847 by virtue of the fields that precede it and the record's
850 if (*type
== _C_BFLD
)
851 layout
->record_size
= atoi (type
+ 1);
852 else if (layout
->record_size
% desired_align
!= 0)
854 /* No, we need to skip space before this field.
855 Bump the cumulative size to multiple of field alignment. */
856 layout
->record_size
= ROUND (layout
->record_size
, desired_align
);
859 /* Jump to the next field in record. */
861 layout
->prev_type
= layout
->type
;
862 layout
->type
= objc_skip_typespec (layout
->type
); /* skip component */
868 void objc_layout_finish_structure (struct objc_struct_layout
*layout
,
872 if (layout
->type
&& *layout
->type
== _C_STRUCT_E
)
874 /* Work out the alignment of the record as one expression and store
875 in the record type. Round it up to a multiple of the record's
878 #if defined(ROUND_TYPE_ALIGN) && !defined(__sparc__)
879 layout
->record_align
= ROUND_TYPE_ALIGN (layout
->original_type
,
881 layout
->record_align
);
883 layout
->record_align
= MAX (1, layout
->record_align
);
886 #ifdef ROUND_TYPE_SIZE
887 layout
->record_size
= ROUND_TYPE_SIZE (layout
->original_type
,
889 layout
->record_align
);
891 /* Round the size up to be a multiple of the required alignment */
892 layout
->record_size
= ROUND (layout
->record_size
, layout
->record_align
);
898 *size
= layout
->record_size
/ BITS_PER_UNIT
;
900 *align
= layout
->record_align
/ BITS_PER_UNIT
;
904 void objc_layout_structure_get_info (struct objc_struct_layout
*layout
,
905 unsigned int *offset
,
910 *offset
= layout
->record_size
/ BITS_PER_UNIT
;
912 *align
= layout
->record_align
/ BITS_PER_UNIT
;
914 *type
= layout
->prev_type
;