Add hppa-openbsd target
[official-gcc.git] / libobjc / encoding.c
blobbe4c08b423e24be54ae449ea7346eb9c052ae3a6
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)
12 any later version.
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. */
30 #include "tconfig.h"
31 #include "objc-api.h"
32 #include "encoding.h"
33 #include <stdlib.h>
35 #undef MAX
36 #define MAX(X, Y) \
37 ({ typeof (X) __x = (X), __y = (Y); \
38 (__x > __y ? __x : __y); })
40 #undef MIN
41 #define MIN(X, Y) \
42 ({ typeof (X) __x = (X), __y = (Y); \
43 (__x < __y ? __x : __y); })
45 #undef ROUND
46 #define ROUND(V, A) \
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
52 macros. */
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)
71 #define DFmode _C_DBL
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
91 int
92 objc_sizeof_type (const char *type)
94 /* Skip the variable name if any */
95 if (*type == '"')
97 for (type++; *type++ != '"';)
98 /* do nothing */;
101 switch (*type) {
102 case _C_ID:
103 return sizeof (id);
104 break;
106 case _C_CLASS:
107 return sizeof (Class);
108 break;
110 case _C_SEL:
111 return sizeof (SEL);
112 break;
114 case _C_CHR:
115 return sizeof (char);
116 break;
118 case _C_UCHR:
119 return sizeof (unsigned char);
120 break;
122 case _C_SHT:
123 return sizeof (short);
124 break;
126 case _C_USHT:
127 return sizeof (unsigned short);
128 break;
130 case _C_INT:
131 return sizeof (int);
132 break;
134 case _C_UINT:
135 return sizeof (unsigned int);
136 break;
138 case _C_LNG:
139 return sizeof (long);
140 break;
142 case _C_ULNG:
143 return sizeof (unsigned long);
144 break;
146 case _C_LNG_LNG:
147 return sizeof (long long);
148 break;
150 case _C_ULNG_LNG:
151 return sizeof (unsigned long long);
152 break;
154 case _C_FLT:
155 return sizeof (float);
156 break;
158 case _C_DBL:
159 return sizeof (double);
160 break;
162 case _C_VOID:
163 return sizeof (void);
164 break;
166 case _C_PTR:
167 case _C_ATOM:
168 case _C_CHARPTR:
169 return sizeof (char *);
170 break;
172 case _C_ARY_B:
174 int len = atoi (type + 1);
175 while (isdigit ((unsigned char)*++type))
177 return len * objc_aligned_size (type);
179 break;
181 case _C_BFLD:
183 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
184 int position, 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;
197 case _C_STRUCT_B:
199 struct objc_struct_layout layout;
200 unsigned int size;
202 objc_layout_structure (type, &layout);
203 while (objc_layout_structure_next_member (&layout))
204 /* do nothing */ ;
205 objc_layout_finish_structure (&layout, &size, NULL);
207 return size;
210 case _C_UNION_B:
212 int max_size = 0;
213 while (*type != _C_UNION_E && *type++ != '=')
214 /* do nothing */;
215 while (*type != _C_UNION_E)
217 /* Skip the variable name if any */
218 if (*type == '"')
220 for (type++; *type++ != '"';)
221 /* do nothing */;
223 max_size = MAX (max_size, objc_sizeof_type (type));
224 type = objc_skip_typespec (type);
226 return max_size;
229 default:
231 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
232 return 0;
239 Return the alignment of an object specified by type
243 objc_alignof_type (const char *type)
245 /* Skip the variable name if any */
246 if (*type == '"')
248 for (type++; *type++ != '"';)
249 /* do nothing */;
251 switch (*type) {
252 case _C_ID:
253 return __alignof__ (id);
254 break;
256 case _C_CLASS:
257 return __alignof__ (Class);
258 break;
260 case _C_SEL:
261 return __alignof__ (SEL);
262 break;
264 case _C_CHR:
265 return __alignof__ (char);
266 break;
268 case _C_UCHR:
269 return __alignof__ (unsigned char);
270 break;
272 case _C_SHT:
273 return __alignof__ (short);
274 break;
276 case _C_USHT:
277 return __alignof__ (unsigned short);
278 break;
280 case _C_INT:
281 return __alignof__ (int);
282 break;
284 case _C_UINT:
285 return __alignof__ (unsigned int);
286 break;
288 case _C_LNG:
289 return __alignof__ (long);
290 break;
292 case _C_ULNG:
293 return __alignof__ (unsigned long);
294 break;
296 case _C_LNG_LNG:
297 return __alignof__ (long long);
298 break;
300 case _C_ULNG_LNG:
301 return __alignof__ (unsigned long long);
302 break;
304 case _C_FLT:
305 return __alignof__ (float);
306 break;
308 case _C_DBL:
309 return __alignof__ (double);
310 break;
312 case _C_PTR:
313 case _C_ATOM:
314 case _C_CHARPTR:
315 return __alignof__ (char *);
316 break;
318 case _C_ARY_B:
319 while (isdigit ((unsigned char)*++type))
320 /* do nothing */;
321 return objc_alignof_type (type);
323 case _C_STRUCT_B:
325 struct objc_struct_layout layout;
326 unsigned int align;
328 objc_layout_structure (type, &layout);
329 while (objc_layout_structure_next_member (&layout))
330 /* do nothing */;
331 objc_layout_finish_structure (&layout, NULL, &align);
333 return align;
336 case _C_UNION_B:
338 int maxalign = 0;
339 while (*type != _C_UNION_E && *type++ != '=')
340 /* do nothing */;
341 while (*type != _C_UNION_E)
343 /* Skip the variable name if any */
344 if (*type == '"')
346 for (type++; *type++ != '"';)
347 /* do nothing */;
349 maxalign = MAX (maxalign, objc_alignof_type (type));
350 type = objc_skip_typespec (type);
352 return maxalign;
355 default:
357 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
358 return 0;
364 The aligned size if the size rounded up to the nearest alignment.
368 objc_aligned_size (const char *type)
370 int size, align;
372 /* Skip the variable name */
373 if (*type == '"')
375 for (type++; *type++ != '"';)
376 /* do nothing */;
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)
393 int size, wordsize;
395 /* Skip the variable name */
396 if (*type == '"')
398 for (type++; *type++ != '"';)
399 /* do nothing */;
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.
413 inline const char *
414 objc_skip_type_qualifiers (const char *type)
416 while (*type == _C_CONST
417 || *type == _C_IN
418 || *type == _C_INOUT
419 || *type == _C_OUT
420 || *type == _C_BYCOPY
421 || *type == _C_BYREF
422 || *type == _C_ONEWAY
423 || *type == _C_GCINVISIBLE)
425 type += 1;
427 return type;
432 Skip one typespec element. If the typespec is prepended by type
433 qualifiers, these are skipped as well.
436 const char *
437 objc_skip_typespec (const char *type)
439 /* Skip the variable name if any */
440 if (*type == '"')
442 for (type++; *type++ != '"';)
443 /* do nothing */;
446 type = objc_skip_type_qualifiers (type);
448 switch (*type) {
450 case _C_ID:
451 /* An id may be annotated by the actual type if it is known
452 with the @"ClassName" syntax */
454 if (*++type != '"')
455 return type;
456 else
458 while (*++type != '"')
459 /* do nothing */;
460 return type + 1;
463 /* The following are one character type codes */
464 case _C_CLASS:
465 case _C_SEL:
466 case _C_CHR:
467 case _C_UCHR:
468 case _C_CHARPTR:
469 case _C_ATOM:
470 case _C_SHT:
471 case _C_USHT:
472 case _C_INT:
473 case _C_UINT:
474 case _C_LNG:
475 case _C_ULNG:
476 case _C_LNG_LNG:
477 case _C_ULNG_LNG:
478 case _C_FLT:
479 case _C_DBL:
480 case _C_VOID:
481 case _C_UNDEF:
482 return ++type;
483 break;
485 case _C_ARY_B:
486 /* skip digits, typespec and closing ']' */
488 while (isdigit ((unsigned char)*++type))
490 type = objc_skip_typespec (type);
491 if (*type == _C_ARY_E)
492 return ++type;
493 else
495 objc_error (nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
496 return 0;
499 case _C_BFLD:
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 */
505 return type;
507 case _C_STRUCT_B:
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);
516 return ++type;
518 case _C_UNION_B:
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);
527 return ++type;
529 case _C_PTR:
530 /* Just skip the following typespec */
532 return objc_skip_typespec (++type);
534 default:
536 objc_error (nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
537 return 0;
543 Skip an offset as part of a method encoding. This is prepended by a
544 '+' if the argument is passed in registers.
546 inline const char *
547 objc_skip_offset (const char *type)
549 if (*type == '+')
550 type++;
551 while (isdigit ((unsigned char) *++type))
553 return type;
557 Skip an argument specification of a method encoding.
559 const char *
560 objc_skip_argspec (const char *type)
562 type = objc_skip_typespec (type);
563 type = objc_skip_offset (type);
564 return type;
568 Return the number of arguments that the method MTH expects.
569 Note that all methods need two implicit arguments `self' and
570 `_cmd'.
573 method_get_number_of_arguments (struct objc_method *mth)
575 int i = 0;
576 const char *type = mth->method_types;
577 while (*type)
579 type = objc_skip_argspec (type);
580 i += 1;
582 return i - 1;
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
588 registers.
592 method_get_sizeof_arguments (struct objc_method *mth)
594 const char *type = objc_skip_typespec (mth->method_types);
595 return atoi (type);
599 Return a pointer to the next argument of ARGFRAME. type points to
600 the last argument. Typical use of this look like:
603 char *datum, *type;
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);
609 if (*type != _C_PTR)
610 [portal encodeData: datum ofType: type];
611 else
613 if ((flags & _F_IN) == _F_IN)
614 [portal encodeData: *(char **) datum ofType: ++type];
620 char *
621 method_get_next_argument (arglist_t argframe, const char **type)
623 const char *t = objc_skip_argspec (*type);
625 if (*t == '\0')
626 return 0;
628 *type = t;
629 t = objc_skip_typespec (t);
631 if (*t == '+')
632 return argframe->arg_regs + atoi (++t);
633 else
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.
643 char *
644 method_get_first_argument (struct objc_method *m,
645 arglist_t argframe,
646 const char **type)
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
658 char *
659 method_get_nth_argument (struct objc_method *m,
660 arglist_t argframe, int arg,
661 const char **type)
663 const char *t = objc_skip_argspec (m->method_types);
665 if (arg > method_get_number_of_arguments (m))
666 return 0;
668 while (arg--)
669 t = objc_skip_argspec (t);
671 *type = t;
672 t = objc_skip_typespec (t);
674 if (*t == '+')
675 return argframe->arg_regs + atoi (++t);
676 else
677 return argframe->arg_ptr + atoi (t);
680 unsigned
681 objc_get_type_qualifiers (const char *type)
683 unsigned res = 0;
684 BOOL flag = YES;
686 while (flag)
687 switch (*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;
697 default: flag = NO;
700 return res;
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;
708 int i;
710 objc_layout_structure (type, &layout);
711 while (objc_layout_structure_next_member (&layout))
713 int position, align;
714 const char *type;
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. */
726 void
727 objc_layout_structure (const char *type,
728 struct objc_struct_layout *layout)
730 const char *ntype;
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",
736 type);
739 layout->original_type = type;
741 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
742 ntype = type;
743 while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
744 && *ntype++ != '=')
745 /* do nothing */;
747 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
748 if (*(ntype - 1) == '=')
749 type = ntype;
751 layout->type = type;
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);
760 BOOL
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 */
770 const char *type;
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;
779 else {
780 /* Get the bitfield's type */
781 for (bfld_type = type + 1;
782 isdigit ((unsigned char)*bfld_type);
783 bfld_type++)
784 /* do nothing */;
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)
794 return NO;
796 /* Skip the variable name if any */
797 if (*layout->type == '"')
799 for (layout->type++; *layout->type++ != '"';)
800 /* do nothing */;
803 type = objc_skip_type_qualifiers (layout->type);
805 if (*type != _C_BFLD)
806 desired_align = objc_alignof_type (type) * BITS_PER_UNIT;
807 else
809 desired_align = 1;
810 /* Skip the bitfield's offset */
811 for (bfld_type = type + 1;
812 isdigit ((unsigned char) *bfld_type);
813 bfld_type++)
814 /* do nothing */;
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);
823 #endif
824 #ifdef ADJUST_FIELD_ALIGN
825 desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
826 #endif
828 /* Record must have at least as much alignment as any field.
829 Otherwise, the alignment of the field within the record
830 is meaningless. */
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. */
840 if (bfld_field_size)
841 layout->record_align = MAX (layout->record_align, desired_align);
842 else
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;
853 #if 0
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);
858 #endif
860 layout->record_align = MAX (layout->record_align, type_align);
863 else
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
869 own alignment? */
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 */
885 return YES;
889 void objc_layout_finish_structure (struct objc_struct_layout *layout,
890 unsigned int *size,
891 unsigned int *align)
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
897 alignment. */
899 #if defined (ROUND_TYPE_ALIGN) && ! defined (__sparc__)
900 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type,
902 layout->record_align);
903 #else
904 layout->record_align = MAX (1, layout->record_align);
905 #endif
907 #ifdef ROUND_TYPE_SIZE
908 layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
909 layout->record_size,
910 layout->record_align);
911 #else
912 /* Round the size up to be a multiple of the required alignment */
913 layout->record_size = ROUND (layout->record_size, layout->record_align);
914 #endif
916 layout->type = NULL;
918 if (size)
919 *size = layout->record_size / BITS_PER_UNIT;
920 if (align)
921 *align = layout->record_align / BITS_PER_UNIT;
925 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
926 unsigned int *offset,
927 unsigned int *align,
928 const char **type)
930 if (offset)
931 *offset = layout->record_size / BITS_PER_UNIT;
932 if (align)
933 *align = layout->record_align / BITS_PER_UNIT;
934 if (type)
935 *type = layout->prev_type;