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. */
36 ({ typeof(X) __x = (X), __y = (Y); \
37 (__x > __y ? __x : __y); })
41 ({ typeof(X) __x = (X), __y = (Y); \
42 (__x < __y ? __x : __y); })
46 ({ typeof(V) __v=(V); typeof(A) __a=(A); \
47 __a*((__v+__a-1)/__a); })
50 /* Various hacks for objc_layout_record. These are used by the target
53 #define TREE_CODE(TYPE) *(TYPE)
54 #define TREE_TYPE(TREE) (TREE)
56 #define RECORD_TYPE _C_STRUCT_B
57 #define UNION_TYPE _C_UNION_B
58 #define QUAL_UNION_TYPE _C_UNION_B
59 #define ARRAY_TYPE _C_ARY_B
61 #define REAL_TYPE _C_DBL
63 #define VECTOR_TYPE _C_VECTOR
65 #define TYPE_FIELDS(TYPE) objc_skip_typespec (TYPE)
67 #define DECL_MODE(TYPE) *(TYPE)
68 #define TYPE_MODE(TYPE) *(TYPE)
72 #define get_inner_array_type(TYPE) ((TYPE) + 1)
74 /* Some ports (eg ARM) allow the structure size boundary to be
75 selected at compile-time. We override the normal definition with
76 one that has a constant value for this compilation. */
77 #undef STRUCTURE_SIZE_BOUNDARY
78 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
80 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
81 target_flags. Define a dummy entry here to so we don't die. */
83 static int target_flags
= 0;
86 atoi (const char* str
)
90 while (isdigit (*str
))
91 res
*= 10, res
+= (*str
++ - '0');
97 return the size of an object specified by type
101 objc_sizeof_type (const char* type
)
103 /* Skip the variable name if any */
106 for (type
++; *type
++ != '"';)
116 return sizeof(Class
);
128 return sizeof(unsigned char);
132 return sizeof(short);
136 return sizeof(unsigned short);
144 return sizeof(unsigned int);
152 return sizeof(unsigned long);
156 return sizeof(long long);
160 return sizeof(unsigned long long);
164 return sizeof(float);
168 return sizeof(double);
177 return sizeof(char*);
182 int len
= atoi(type
+1);
183 while (isdigit(*++type
));
184 return len
*objc_aligned_size (type
);
190 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
192 int startByte
, endByte
;
194 position
= atoi (type
+ 1);
195 while (isdigit (*++type
));
196 size
= atoi (type
+ 1);
198 startByte
= position
/ BITS_PER_UNIT
;
199 endByte
= (position
+ size
) / BITS_PER_UNIT
;
200 return endByte
- startByte
;
205 struct objc_struct_layout layout
;
208 objc_layout_structure (type
, &layout
);
209 while (objc_layout_structure_next_member (&layout
))
211 objc_layout_finish_structure (&layout
, &size
, NULL
);
219 while (*type
!= _C_UNION_E
&& *type
++ != '=') /* do nothing */;
220 while (*type
!= _C_UNION_E
)
222 /* Skip the variable name if any */
225 for (type
++; *type
++ != '"';)
228 max_size
= MAX (max_size
, objc_sizeof_type (type
));
229 type
= objc_skip_typespec (type
);
236 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
244 Return the alignment of an object specified by type
248 objc_alignof_type(const char* type
)
250 /* Skip the variable name if any */
253 for (type
++; *type
++ != '"';)
258 return __alignof__(id
);
262 return __alignof__(Class
);
266 return __alignof__(SEL
);
270 return __alignof__(char);
274 return __alignof__(unsigned char);
278 return __alignof__(short);
282 return __alignof__(unsigned short);
286 return __alignof__(int);
290 return __alignof__(unsigned int);
294 return __alignof__(long);
298 return __alignof__(unsigned long);
302 return __alignof__(long long);
306 return __alignof__(unsigned long long);
310 return __alignof__(float);
314 return __alignof__(double);
320 return __alignof__(char*);
324 while (isdigit(*++type
)) /* do nothing */;
325 return objc_alignof_type (type
);
329 struct objc_struct_layout layout
;
332 objc_layout_structure (type
, &layout
);
333 while (objc_layout_structure_next_member (&layout
))
335 objc_layout_finish_structure (&layout
, NULL
, &align
);
343 while (*type
!= _C_UNION_E
&& *type
++ != '=') /* do nothing */;
344 while (*type
!= _C_UNION_E
)
346 /* Skip the variable name if any */
349 for (type
++; *type
++ != '"';)
352 maxalign
= MAX (maxalign
, objc_alignof_type (type
));
353 type
= objc_skip_typespec (type
);
360 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
367 The aligned size if the size rounded up to the nearest alignment.
371 objc_aligned_size (const char* type
)
375 /* Skip the variable name */
378 for (type
++; *type
++ != '"';)
382 size
= objc_sizeof_type (type
);
383 align
= objc_alignof_type (type
);
385 return ROUND (size
, align
);
389 The size rounded up to the nearest integral of the wordsize, taken
390 to be the size of a void*.
394 objc_promoted_size (const char* type
)
398 /* Skip the variable name */
401 for (type
++; *type
++ != '"';)
405 size
= objc_sizeof_type (type
);
406 wordsize
= sizeof (void*);
408 return ROUND (size
, wordsize
);
412 Skip type qualifiers. These may eventually precede typespecs
413 occurring in method prototype encodings.
417 objc_skip_type_qualifiers (const char* type
)
419 while (*type
== _C_CONST
423 || *type
== _C_BYCOPY
425 || *type
== _C_ONEWAY
426 || *type
== _C_GCINVISIBLE
)
435 Skip one typespec element. If the typespec is prepended by type
436 qualifiers, these are skipped as well.
440 objc_skip_typespec (const char* type
)
442 /* Skip the variable name if any */
445 for (type
++; *type
++ != '"';)
449 type
= objc_skip_type_qualifiers (type
);
454 /* An id may be annotated by the actual type if it is known
455 with the @"ClassName" syntax */
461 while (*++type
!= '"') /* do nothing */;
465 /* The following are one character type codes */
488 /* skip digits, typespec and closing ']' */
490 while(isdigit(*++type
));
491 type
= objc_skip_typespec(type
);
492 if (*type
== _C_ARY_E
)
496 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "bad array type %s\n", type
);
501 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
502 while (isdigit (*++type
)); /* skip position */
503 while (isdigit (*++type
)); /* skip type and size */
507 /* skip name, and elements until closing '}' */
509 while (*type
!= _C_STRUCT_E
&& *type
++ != '=');
510 while (*type
!= _C_STRUCT_E
) { type
= objc_skip_typespec (type
); }
514 /* skip name, and elements until closing ')' */
516 while (*type
!= _C_UNION_E
&& *type
++ != '=');
517 while (*type
!= _C_UNION_E
) { type
= objc_skip_typespec (type
); }
521 /* Just skip the following typespec */
523 return objc_skip_typespec (++type
);
527 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
534 Skip an offset as part of a method encoding. This is prepended by a
535 '+' if the argument is passed in registers.
538 objc_skip_offset (const char* type
)
540 if (*type
== '+') type
++;
541 while(isdigit(*++type
));
546 Skip an argument specification of a method encoding.
549 objc_skip_argspec (const char* type
)
551 type
= objc_skip_typespec (type
);
552 type
= objc_skip_offset (type
);
557 Return the number of arguments that the method MTH expects.
558 Note that all methods need two implicit arguments `self' and
562 method_get_number_of_arguments (struct objc_method
* mth
)
565 const char* type
= mth
->method_types
;
568 type
= objc_skip_argspec (type
);
575 Return the size of the argument block needed on the stack to invoke
576 the method MTH. This may be zero, if all arguments are passed in
581 method_get_sizeof_arguments (struct objc_method
* mth
)
583 const char* type
= objc_skip_typespec (mth
->method_types
);
588 Return a pointer to the next argument of ARGFRAME. type points to
589 the last argument. Typical use of this look like:
593 for (datum = method_get_first_argument (method, argframe, &type);
594 datum; datum = method_get_next_argument (argframe, &type))
596 unsigned flags = objc_get_type_qualifiers (type);
597 type = objc_skip_type_qualifiers (type);
599 [portal encodeData: datum ofType: type];
602 if ((flags & _F_IN) == _F_IN)
603 [portal encodeData: *(char**)datum ofType: ++type];
610 method_get_next_argument (arglist_t argframe
,
613 const char *t
= objc_skip_argspec (*type
);
619 t
= objc_skip_typespec (t
);
622 return argframe
->arg_regs
+ atoi (++t
);
624 return argframe
->arg_ptr
+ atoi (t
);
628 Return a pointer to the value of the first argument of the method
629 described in M with the given argumentframe ARGFRAME. The type
630 is returned in TYPE. type must be passed to successive calls of
631 method_get_next_argument.
634 method_get_first_argument (struct objc_method
* m
,
638 *type
= m
->method_types
;
639 return method_get_next_argument (argframe
, type
);
643 Return a pointer to the ARGth argument of the method
644 M from the frame ARGFRAME. The type of the argument
645 is returned in the value-result argument TYPE
649 method_get_nth_argument (struct objc_method
* m
,
650 arglist_t argframe
, int arg
,
653 const char* t
= objc_skip_argspec (m
->method_types
);
655 if (arg
> method_get_number_of_arguments (m
))
659 t
= objc_skip_argspec (t
);
662 t
= objc_skip_typespec (t
);
665 return argframe
->arg_regs
+ atoi (++t
);
667 return argframe
->arg_ptr
+ atoi (t
);
671 objc_get_type_qualifiers (const char* type
)
679 case _C_CONST
: res
|= _F_CONST
; break;
680 case _C_IN
: res
|= _F_IN
; break;
681 case _C_INOUT
: res
|= _F_INOUT
; break;
682 case _C_OUT
: res
|= _F_OUT
; break;
683 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
684 case _C_BYREF
: res
|= _F_BYREF
; break;
685 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
686 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
694 /* The following three functions can be used to determine how a
695 structure is laid out by the compiler. For example:
697 struct objc_struct_layout layout;
700 objc_layout_structure (type, &layout);
701 while (objc_layout_structure_next_member (&layout))
706 objc_layout_structure_get_info (&layout, &position, &align, &type);
707 printf ("element %d has offset %d, alignment %d\n",
708 i++, position, align);
711 These functions are used by objc_sizeof_type and objc_alignof_type
712 functions to compute the size and alignment of structures. The
713 previous method of computing the size and alignment of a structure
714 was not working on some architectures, particulary on AIX, and in
715 the presence of bitfields inside the structure. */
717 objc_layout_structure (const char *type
,
718 struct objc_struct_layout
*layout
)
722 if (*type
++ != _C_STRUCT_B
)
724 objc_error(nil
, OBJC_ERR_BAD_TYPE
,
725 "record type expected in objc_layout_structure, got %s\n",
729 layout
->original_type
= type
;
731 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
733 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
737 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
738 if (*(ntype
- 1) == '=')
742 layout
->prev_type
= NULL
;
743 layout
->record_size
= 0;
744 layout
->record_align
= BITS_PER_UNIT
;
746 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
751 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
753 register int desired_align
= 0;
755 /* The following are used only if the field is a bitfield */
756 register const char *bfld_type
;
757 register int bfld_type_size
, bfld_type_align
, bfld_field_size
;
759 /* The current type without the type qualifiers */
762 /* Add the size of the previous field to the size of the record. */
763 if (layout
->prev_type
)
765 type
= objc_skip_type_qualifiers (layout
->prev_type
);
767 if (*type
!= _C_BFLD
)
768 layout
->record_size
+= objc_sizeof_type (type
) * BITS_PER_UNIT
;
770 /* Get the bitfield's type */
771 for (bfld_type
= type
+ 1;
776 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
777 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
778 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
779 layout
->record_size
+= bfld_field_size
;
783 if (*layout
->type
== _C_STRUCT_E
)
786 /* Skip the variable name if any */
787 if (*layout
->type
== '"')
789 for (layout
->type
++; *layout
->type
++ != '"';)
793 type
= objc_skip_type_qualifiers (layout
->type
);
795 if (*type
!= _C_BFLD
)
796 desired_align
= objc_alignof_type(type
) * BITS_PER_UNIT
;
800 /* Skip the bitfield's offset */
801 for (bfld_type
= type
+ 1; isdigit(*bfld_type
); bfld_type
++)
804 bfld_type_size
= objc_sizeof_type (bfld_type
) * BITS_PER_UNIT
;
805 bfld_type_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
806 bfld_field_size
= atoi (objc_skip_typespec (bfld_type
));
809 #ifdef BIGGEST_FIELD_ALIGNMENT
810 desired_align
= MIN (desired_align
, BIGGEST_FIELD_ALIGNMENT
);
812 #ifdef ADJUST_FIELD_ALIGN
813 desired_align
= ADJUST_FIELD_ALIGN (type
, desired_align
);
816 /* Record must have at least as much alignment as any field.
817 Otherwise, the alignment of the field within the record
819 #ifndef PCC_BITFIELD_TYPE_MATTERS
820 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
822 if (*type
== _C_BFLD
)
824 /* For these machines, a zero-length field does not
825 affect the alignment of the structure as a whole.
826 It does, however, affect the alignment of the next field
827 within the structure. */
829 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
831 desired_align
= objc_alignof_type (bfld_type
) * BITS_PER_UNIT
;
833 /* A named bit field of declared type `int'
834 forces the entire structure to have `int' alignment.
835 Q1: How is encoded this thing and how to check for it?
836 Q2: How to determine maximum_field_alignment at runtime? */
838 /* if (DECL_NAME (field) != 0) */
840 int type_align
= bfld_type_align
;
842 if (maximum_field_alignment
!= 0)
843 type_align
= MIN (type_align
, maximum_field_alignment
);
844 else if (DECL_PACKED (field
))
845 type_align
= MIN (type_align
, BITS_PER_UNIT
);
848 layout
->record_align
= MAX (layout
->record_align
, type_align
);
852 layout
->record_align
= MAX (layout
->record_align
, desired_align
);
855 /* Does this field automatically have alignment it needs
856 by virtue of the fields that precede it and the record's
859 if (*type
== _C_BFLD
)
860 layout
->record_size
= atoi (type
+ 1);
861 else if (layout
->record_size
% desired_align
!= 0)
863 /* No, we need to skip space before this field.
864 Bump the cumulative size to multiple of field alignment. */
865 layout
->record_size
= ROUND (layout
->record_size
, desired_align
);
868 /* Jump to the next field in record. */
870 layout
->prev_type
= layout
->type
;
871 layout
->type
= objc_skip_typespec (layout
->type
); /* skip component */
877 void objc_layout_finish_structure (struct objc_struct_layout
*layout
,
881 if (layout
->type
&& *layout
->type
== _C_STRUCT_E
)
883 /* Work out the alignment of the record as one expression and store
884 in the record type. Round it up to a multiple of the record's
887 #if defined(ROUND_TYPE_ALIGN) && !defined(__sparc__)
888 layout
->record_align
= ROUND_TYPE_ALIGN (layout
->original_type
,
890 layout
->record_align
);
892 layout
->record_align
= MAX (1, layout
->record_align
);
895 #ifdef ROUND_TYPE_SIZE
896 layout
->record_size
= ROUND_TYPE_SIZE (layout
->original_type
,
898 layout
->record_align
);
900 /* Round the size up to be a multiple of the required alignment */
901 layout
->record_size
= ROUND (layout
->record_size
, layout
->record_align
);
907 *size
= layout
->record_size
/ BITS_PER_UNIT
;
909 *align
= layout
->record_align
/ BITS_PER_UNIT
;
913 void objc_layout_structure_get_info (struct objc_struct_layout
*layout
,
914 unsigned int *offset
,
919 *offset
= layout
->record_size
/ BITS_PER_UNIT
;
921 *align
= layout
->record_align
/ BITS_PER_UNIT
;
923 *type
= layout
->prev_type
;