1 /* Encoding of types for Objective C.
2 Copyright (C) 1993, 1995, 1996, 1997, 1998, 2000 Free Software Foundation, Inc.
3 Contributed by Kresten Krab Thorup
4 Bitfield support by Ovidiu Predescu
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* As a special exception, if you link this library with files
24 compiled with GCC to produce an executable, this does not cause
25 the resulting executable to be covered by the GNU General Public License.
26 This exception does not however invalidate any other reasons why
27 the executable file might be covered by the GNU General Public License. */
35 ({ typeof(X) __x = (X), __y = (Y); \
36 (__x > __y ? __x : __y); })
40 ({ typeof(X) __x = (X), __y = (Y); \
41 (__x < __y ? __x : __y); })
45 ({ typeof(V) __v=(V); typeof(A) __a=(A); \
46 __a*((__v+__a-1)/__a); })
49 /* Various hacks for objc_layout_record. These are used by the target
52 #define TREE_CODE(TYPE) *(TYPE)
53 #define TREE_TYPE(TREE) (TREE)
55 #define RECORD_TYPE _C_STRUCT_B
56 #define UNION_TYPE _C_UNION_B
57 #define QUAL_UNION_TYPE _C_UNION_B
58 #define ARRAY_TYPE _C_ARY_B
60 #define REAL_TYPE _C_DBL
62 #define VECTOR_TYPE _C_VECTOR
64 #define TYPE_FIELDS(TYPE) objc_skip_typespec (TYPE)
66 #define DECL_MODE(TYPE) *(TYPE)
67 #define TYPE_MODE(TYPE) *(TYPE)
71 #define get_inner_array_type(TYPE) ((TYPE) + 1)
73 /* Some ports (eg ARM) allow the structure size boundary to be
74 selected at compile-time. We override the normal definition with
75 one that has a constant value for this compilation. */
76 #undef STRUCTURE_SIZE_BOUNDARY
77 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
79 /* Some ROUND_TYPE_ALIGN macros use TARGET_foo, and consequently
80 target_flags. Define a dummy entry here to so we don't die. */
82 static int target_flags
= 0;
85 atoi (const char* str
)
89 while (isdigit (*str
))
90 res
*= 10, res
+= (*str
++ - '0');
96 return the size of an object specified by type
100 objc_sizeof_type (const char* type
)
102 /* Skip the variable name if any */
105 for (type
++; *type
++ != '"';)
115 return sizeof(Class
);
127 return sizeof(unsigned char);
131 return sizeof(short);
135 return sizeof(unsigned short);
143 return sizeof(unsigned int);
151 return sizeof(unsigned long);
155 return sizeof(long long);
159 return sizeof(unsigned long long);
163 return sizeof(float);
167 return sizeof(double);
176 return sizeof(char*);
181 int len
= atoi(type
+1);
182 while (isdigit(*++type
));
183 return len
*objc_aligned_size (type
);
189 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
191 int startByte
, endByte
;
193 position
= atoi (type
+ 1);
194 while (isdigit (*++type
));
195 size
= atoi (type
+ 1);
197 startByte
= position
/ BITS_PER_UNIT
;
198 endByte
= (position
+ size
) / BITS_PER_UNIT
;
199 return endByte
- startByte
;
204 struct objc_struct_layout layout
;
207 objc_layout_structure (type
, &layout
);
208 while (objc_layout_structure_next_member (&layout
))
210 objc_layout_finish_structure (&layout
, &size
, NULL
);
218 while (*type
!= _C_UNION_E
&& *type
++ != '=') /* do nothing */;
219 while (*type
!= _C_UNION_E
)
221 /* Skip the variable name if any */
224 for (type
++; *type
++ != '"';)
227 max_size
= MAX (max_size
, objc_sizeof_type (type
));
228 type
= objc_skip_typespec (type
);
235 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
243 Return the alignment of an object specified by type
247 objc_alignof_type(const char* type
)
249 /* Skip the variable name if any */
252 for (type
++; *type
++ != '"';)
257 return __alignof__(id
);
261 return __alignof__(Class
);
265 return __alignof__(SEL
);
269 return __alignof__(char);
273 return __alignof__(unsigned char);
277 return __alignof__(short);
281 return __alignof__(unsigned short);
285 return __alignof__(int);
289 return __alignof__(unsigned int);
293 return __alignof__(long);
297 return __alignof__(unsigned long);
301 return __alignof__(long long);
305 return __alignof__(unsigned long long);
309 return __alignof__(float);
313 return __alignof__(double);
319 return __alignof__(char*);
323 while (isdigit(*++type
)) /* do nothing */;
324 return objc_alignof_type (type
);
328 struct objc_struct_layout layout
;
331 objc_layout_structure (type
, &layout
);
332 while (objc_layout_structure_next_member (&layout
))
334 objc_layout_finish_structure (&layout
, NULL
, &align
);
342 while (*type
!= _C_UNION_E
&& *type
++ != '=') /* do nothing */;
343 while (*type
!= _C_UNION_E
)
345 /* Skip the variable name if any */
348 for (type
++; *type
++ != '"';)
351 maxalign
= MAX (maxalign
, objc_alignof_type (type
));
352 type
= objc_skip_typespec (type
);
359 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
366 The aligned size if the size rounded up to the nearest alignment.
370 objc_aligned_size (const char* type
)
374 /* Skip the variable name */
377 for (type
++; *type
++ != '"';)
381 size
= objc_sizeof_type (type
);
382 align
= objc_alignof_type (type
);
384 return ROUND (size
, align
);
388 The size rounded up to the nearest integral of the wordsize, taken
389 to be the size of a void*.
393 objc_promoted_size (const char* type
)
397 /* Skip the variable name */
400 for (type
++; *type
++ != '"';)
404 size
= objc_sizeof_type (type
);
405 wordsize
= sizeof (void*);
407 return ROUND (size
, wordsize
);
411 Skip type qualifiers. These may eventually precede typespecs
412 occurring in method prototype encodings.
416 objc_skip_type_qualifiers (const char* type
)
418 while (*type
== _C_CONST
422 || *type
== _C_BYCOPY
424 || *type
== _C_ONEWAY
425 || *type
== _C_GCINVISIBLE
)
434 Skip one typespec element. If the typespec is prepended by type
435 qualifiers, these are skipped as well.
439 objc_skip_typespec (const char* type
)
441 /* Skip the variable name if any */
444 for (type
++; *type
++ != '"';)
448 type
= objc_skip_type_qualifiers (type
);
453 /* An id may be annotated by the actual type if it is known
454 with the @"ClassName" syntax */
460 while (*++type
!= '"') /* do nothing */;
464 /* The following are one character type codes */
487 /* skip digits, typespec and closing ']' */
489 while(isdigit(*++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 (*++type
)); /* skip position */
502 while (isdigit (*++type
)); /* skip type and size */
506 /* skip name, and elements until closing '}' */
508 while (*type
!= _C_STRUCT_E
&& *type
++ != '=');
509 while (*type
!= _C_STRUCT_E
) { type
= objc_skip_typespec (type
); }
513 /* skip name, and elements until closing ')' */
515 while (*type
!= _C_UNION_E
&& *type
++ != '=');
516 while (*type
!= _C_UNION_E
) { type
= objc_skip_typespec (type
); }
520 /* Just skip the following typespec */
522 return objc_skip_typespec (++type
);
526 objc_error(nil
, OBJC_ERR_BAD_TYPE
, "unknown type %s\n", type
);
533 Skip an offset as part of a method encoding. This is prepended by a
534 '+' if the argument is passed in registers.
537 objc_skip_offset (const char* type
)
539 if (*type
== '+') type
++;
540 while(isdigit(*++type
));
545 Skip an argument specification of a method encoding.
548 objc_skip_argspec (const char* type
)
550 type
= objc_skip_typespec (type
);
551 type
= objc_skip_offset (type
);
556 Return the number of arguments that the method MTH expects.
557 Note that all methods need two implicit arguments `self' and
561 method_get_number_of_arguments (struct objc_method
* mth
)
564 const char* type
= mth
->method_types
;
567 type
= objc_skip_argspec (type
);
574 Return the size of the argument block needed on the stack to invoke
575 the method MTH. This may be zero, if all arguments are passed in
580 method_get_sizeof_arguments (struct objc_method
* mth
)
582 const char* type
= objc_skip_typespec (mth
->method_types
);
587 Return a pointer to the next argument of ARGFRAME. type points to
588 the last argument. Typical use of this look like:
592 for (datum = method_get_first_argument (method, argframe, &type);
593 datum; datum = method_get_next_argument (argframe, &type))
595 unsigned flags = objc_get_type_qualifiers (type);
596 type = objc_skip_type_qualifiers (type);
598 [portal encodeData: datum ofType: type];
601 if ((flags & _F_IN) == _F_IN)
602 [portal encodeData: *(char**)datum ofType: ++type];
609 method_get_next_argument (arglist_t argframe
,
612 const char *t
= objc_skip_argspec (*type
);
618 t
= objc_skip_typespec (t
);
621 return argframe
->arg_regs
+ atoi (++t
);
623 return argframe
->arg_ptr
+ atoi (t
);
627 Return a pointer to the value of the first argument of the method
628 described in M with the given argumentframe ARGFRAME. The type
629 is returned in TYPE. type must be passed to successive calls of
630 method_get_next_argument.
633 method_get_first_argument (struct objc_method
* m
,
637 *type
= m
->method_types
;
638 return method_get_next_argument (argframe
, type
);
642 Return a pointer to the ARGth argument of the method
643 M from the frame ARGFRAME. The type of the argument
644 is returned in the value-result argument TYPE
648 method_get_nth_argument (struct objc_method
* m
,
649 arglist_t argframe
, int arg
,
652 const char* t
= objc_skip_argspec (m
->method_types
);
654 if (arg
> method_get_number_of_arguments (m
))
658 t
= objc_skip_argspec (t
);
661 t
= objc_skip_typespec (t
);
664 return argframe
->arg_regs
+ atoi (++t
);
666 return argframe
->arg_ptr
+ atoi (t
);
670 objc_get_type_qualifiers (const char* type
)
678 case _C_CONST
: res
|= _F_CONST
; break;
679 case _C_IN
: res
|= _F_IN
; break;
680 case _C_INOUT
: res
|= _F_INOUT
; break;
681 case _C_OUT
: res
|= _F_OUT
; break;
682 case _C_BYCOPY
: res
|= _F_BYCOPY
; break;
683 case _C_BYREF
: res
|= _F_BYREF
; break;
684 case _C_ONEWAY
: res
|= _F_ONEWAY
; break;
685 case _C_GCINVISIBLE
: res
|= _F_GCINVISIBLE
; break;
693 /* The following three functions can be used to determine how a
694 structure is laid out by the compiler. For example:
696 struct objc_struct_layout layout;
699 objc_layout_structure (type, &layout);
700 while (objc_layout_structure_next_member (&layout))
705 objc_layout_structure_get_info (&layout, &position, &align, &type);
706 printf ("element %d has offset %d, alignment %d\n",
707 i++, position, align);
710 These functions are used by objc_sizeof_type and objc_alignof_type
711 functions to compute the size and alignment of structures. The
712 previous method of computing the size and alignment of a structure
713 was not working on some architectures, particulary on AIX, and in
714 the presence of bitfields inside the structure. */
716 objc_layout_structure (const char *type
,
717 struct objc_struct_layout
*layout
)
721 if (*type
++ != _C_STRUCT_B
)
723 objc_error(nil
, OBJC_ERR_BAD_TYPE
,
724 "record type expected in objc_layout_structure, got %s\n",
728 layout
->original_type
= type
;
730 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
732 while (*ntype
!= _C_STRUCT_E
&& *ntype
!= _C_STRUCT_B
&& *ntype
!= _C_UNION_B
736 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
737 if (*(ntype
- 1) == '=')
741 layout
->prev_type
= NULL
;
742 layout
->record_size
= 0;
743 layout
->record_align
= BITS_PER_UNIT
;
745 layout
->record_align
= MAX (layout
->record_align
, STRUCTURE_SIZE_BOUNDARY
);
750 objc_layout_structure_next_member (struct objc_struct_layout
*layout
)
752 register int known_align
= layout
->record_size
;
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
;