New test.
[official-gcc.git] / libobjc / encoding.c
bloba87f981e8eb81ff924d85a973b6676d3798690d1
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)
11 any later version.
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. */
29 #include "tconfig.h"
30 #include "objc-api.h"
31 #include "encoding.h"
33 #define MAX(X, Y) \
34 ({ typeof(X) __x = (X), __y = (Y); \
35 (__x > __y ? __x : __y); })
37 #define MIN(X, Y) \
38 ({ typeof(X) __x = (X), __y = (Y); \
39 (__x < __y ? __x : __y); })
41 #define ROUND(V, A) \
42 ({ typeof(V) __v=(V); typeof(A) __a=(A); \
43 __a*((__v+__a-1)/__a); })
46 /* Various hacks for objc_layout_record. These are used by the target
47 macros. */
49 #define TREE_CODE(TYPE) *(TYPE)
50 #define TREE_TYPE(TREE) (TREE)
52 #define RECORD_TYPE _C_STRUCT_B
53 #define UNION_TYPE _C_UNION_B
54 #define QUAL_UNION_TYPE _C_UNION_B
55 #define ARRAY_TYPE _C_ARY_B
57 #define TYPE_FIELDS(TYPE) objc_skip_typespec (TYPE)
59 #define DECL_MODE(TYPE) *(TYPE)
60 #define TYPE_MODE(TYPE) *(TYPE)
62 #define DFmode _C_DBL
64 #define get_inner_array_type(TYPE) ((TYPE) + 1)
66 /* Some ports (eg ARM) allow the structure size boundary to be
67 selected at compile-time. We override the normal definition with
68 one that has a constant value for this compilation. */
69 #undef STRUCTURE_SIZE_BOUNDARY
70 #define STRUCTURE_SIZE_BOUNDARY (BITS_PER_UNIT * sizeof (struct{char a;}))
72 static inline int
73 atoi (const char* str)
75 int res = 0;
77 while (isdigit (*str))
78 res *= 10, res += (*str++ - '0');
80 return res;
84 return the size of an object specified by type
87 int
88 objc_sizeof_type (const char* type)
90 /* Skip the variable name if any */
91 if (*type == '"')
93 for (type++; *type++ != '"';)
94 /* do nothing */;
97 switch(*type) {
98 case _C_ID:
99 return sizeof(id);
100 break;
102 case _C_CLASS:
103 return sizeof(Class);
104 break;
106 case _C_SEL:
107 return sizeof(SEL);
108 break;
110 case _C_CHR:
111 return sizeof(char);
112 break;
114 case _C_UCHR:
115 return sizeof(unsigned char);
116 break;
118 case _C_SHT:
119 return sizeof(short);
120 break;
122 case _C_USHT:
123 return sizeof(unsigned short);
124 break;
126 case _C_INT:
127 return sizeof(int);
128 break;
130 case _C_UINT:
131 return sizeof(unsigned int);
132 break;
134 case _C_LNG:
135 return sizeof(long);
136 break;
138 case _C_ULNG:
139 return sizeof(unsigned long);
140 break;
142 case _C_LNG_LNG:
143 return sizeof(long long);
144 break;
146 case _C_ULNG_LNG:
147 return sizeof(unsigned long long);
148 break;
150 case _C_FLT:
151 return sizeof(float);
152 break;
154 case _C_DBL:
155 return sizeof(double);
156 break;
158 case _C_VOID:
159 return sizeof(void);
160 break;
161 case _C_PTR:
162 case _C_ATOM:
163 case _C_CHARPTR:
164 return sizeof(char*);
165 break;
167 case _C_ARY_B:
169 int len = atoi(type+1);
170 while (isdigit(*++type));
171 return len*objc_aligned_size (type);
173 break;
175 case _C_BFLD:
177 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
178 int position, size;
179 int startByte, endByte;
181 position = atoi (type + 1);
182 while (isdigit (*++type));
183 size = atoi (type + 1);
185 startByte = position / BITS_PER_UNIT;
186 endByte = (position + size) / BITS_PER_UNIT;
187 return endByte - startByte;
190 case _C_STRUCT_B:
192 struct objc_struct_layout layout;
193 unsigned int size;
195 objc_layout_structure (type, &layout);
196 while (objc_layout_structure_next_member (&layout))
197 /* do nothing */ ;
198 objc_layout_finish_structure (&layout, &size, NULL);
200 return size;
203 case _C_UNION_B:
205 int max_size = 0;
206 while (*type != _C_UNION_E && *type++ != '=') /* do nothing */;
207 while (*type != _C_UNION_E)
209 /* Skip the variable name if any */
210 if (*type == '"')
212 for (type++; *type++ != '"';)
213 /* do nothing */;
215 max_size = MAX (max_size, objc_sizeof_type (type));
216 type = objc_skip_typespec (type);
218 return max_size;
221 default:
223 objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
224 return 0;
231 Return the alignment of an object specified by type
235 objc_alignof_type(const char* type)
237 /* Skip the variable name if any */
238 if (*type == '"')
240 for (type++; *type++ != '"';)
241 /* do nothing */;
243 switch(*type) {
244 case _C_ID:
245 return __alignof__(id);
246 break;
248 case _C_CLASS:
249 return __alignof__(Class);
250 break;
252 case _C_SEL:
253 return __alignof__(SEL);
254 break;
256 case _C_CHR:
257 return __alignof__(char);
258 break;
260 case _C_UCHR:
261 return __alignof__(unsigned char);
262 break;
264 case _C_SHT:
265 return __alignof__(short);
266 break;
268 case _C_USHT:
269 return __alignof__(unsigned short);
270 break;
272 case _C_INT:
273 return __alignof__(int);
274 break;
276 case _C_UINT:
277 return __alignof__(unsigned int);
278 break;
280 case _C_LNG:
281 return __alignof__(long);
282 break;
284 case _C_ULNG:
285 return __alignof__(unsigned long);
286 break;
288 case _C_LNG_LNG:
289 return __alignof__(long long);
290 break;
292 case _C_ULNG_LNG:
293 return __alignof__(unsigned long long);
294 break;
296 case _C_FLT:
297 return __alignof__(float);
298 break;
300 case _C_DBL:
301 return __alignof__(double);
302 break;
304 case _C_PTR:
305 case _C_ATOM:
306 case _C_CHARPTR:
307 return __alignof__(char*);
308 break;
310 case _C_ARY_B:
311 while (isdigit(*++type)) /* do nothing */;
312 return objc_alignof_type (type);
314 case _C_STRUCT_B:
316 struct objc_struct_layout layout;
317 unsigned int align;
319 objc_layout_structure (type, &layout);
320 while (objc_layout_structure_next_member (&layout))
321 /* do nothing */;
322 objc_layout_finish_structure (&layout, NULL, &align);
324 return align;
327 case _C_UNION_B:
329 int maxalign = 0;
330 while (*type != _C_UNION_E && *type++ != '=') /* do nothing */;
331 while (*type != _C_UNION_E)
333 /* Skip the variable name if any */
334 if (*type == '"')
336 for (type++; *type++ != '"';)
337 /* do nothing */;
339 maxalign = MAX (maxalign, objc_alignof_type (type));
340 type = objc_skip_typespec (type);
342 return maxalign;
345 default:
347 objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
348 return 0;
354 The aligned size if the size rounded up to the nearest alignment.
358 objc_aligned_size (const char* type)
360 int size, align;
362 /* Skip the variable name */
363 if (*type == '"')
365 for (type++; *type++ != '"';)
366 /* do nothing */;
369 size = objc_sizeof_type (type);
370 align = objc_alignof_type (type);
372 return ROUND (size, align);
376 The size rounded up to the nearest integral of the wordsize, taken
377 to be the size of a void*.
380 int
381 objc_promoted_size (const char* type)
383 int size, wordsize;
385 /* Skip the variable name */
386 if (*type == '"')
388 for (type++; *type++ != '"';)
389 /* do nothing */;
392 size = objc_sizeof_type (type);
393 wordsize = sizeof (void*);
395 return ROUND (size, wordsize);
399 Skip type qualifiers. These may eventually precede typespecs
400 occurring in method prototype encodings.
403 inline const char*
404 objc_skip_type_qualifiers (const char* type)
406 while (*type == _C_CONST
407 || *type == _C_IN
408 || *type == _C_INOUT
409 || *type == _C_OUT
410 || *type == _C_BYCOPY
411 || *type == _C_BYREF
412 || *type == _C_ONEWAY
413 || *type == _C_GCINVISIBLE)
415 type += 1;
417 return type;
422 Skip one typespec element. If the typespec is prepended by type
423 qualifiers, these are skipped as well.
426 const char*
427 objc_skip_typespec (const char* type)
429 /* Skip the variable name if any */
430 if (*type == '"')
432 for (type++; *type++ != '"';)
433 /* do nothing */;
436 type = objc_skip_type_qualifiers (type);
438 switch (*type) {
440 case _C_ID:
441 /* An id may be annotated by the actual type if it is known
442 with the @"ClassName" syntax */
444 if (*++type != '"')
445 return type;
446 else
448 while (*++type != '"') /* do nothing */;
449 return type + 1;
452 /* The following are one character type codes */
453 case _C_CLASS:
454 case _C_SEL:
455 case _C_CHR:
456 case _C_UCHR:
457 case _C_CHARPTR:
458 case _C_ATOM:
459 case _C_SHT:
460 case _C_USHT:
461 case _C_INT:
462 case _C_UINT:
463 case _C_LNG:
464 case _C_ULNG:
465 case _C_LNG_LNG:
466 case _C_ULNG_LNG:
467 case _C_FLT:
468 case _C_DBL:
469 case _C_VOID:
470 case _C_UNDEF:
471 return ++type;
472 break;
474 case _C_ARY_B:
475 /* skip digits, typespec and closing ']' */
477 while(isdigit(*++type));
478 type = objc_skip_typespec(type);
479 if (*type == _C_ARY_E)
480 return ++type;
481 else
483 objc_error(nil, OBJC_ERR_BAD_TYPE, "bad array type %s\n", type);
484 return 0;
487 case _C_BFLD:
488 /* The new encoding of bitfields is: b 'position' 'type' 'size' */
489 while (isdigit (*++type)); /* skip position */
490 while (isdigit (*++type)); /* skip type and size */
491 return type;
493 case _C_STRUCT_B:
494 /* skip name, and elements until closing '}' */
496 while (*type != _C_STRUCT_E && *type++ != '=');
497 while (*type != _C_STRUCT_E) { type = objc_skip_typespec (type); }
498 return ++type;
500 case _C_UNION_B:
501 /* skip name, and elements until closing ')' */
503 while (*type != _C_UNION_E && *type++ != '=');
504 while (*type != _C_UNION_E) { type = objc_skip_typespec (type); }
505 return ++type;
507 case _C_PTR:
508 /* Just skip the following typespec */
510 return objc_skip_typespec (++type);
512 default:
514 objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type);
515 return 0;
521 Skip an offset as part of a method encoding. This is prepended by a
522 '+' if the argument is passed in registers.
524 inline const char*
525 objc_skip_offset (const char* type)
527 if (*type == '+') type++;
528 while(isdigit(*++type));
529 return type;
533 Skip an argument specification of a method encoding.
535 const char*
536 objc_skip_argspec (const char* type)
538 type = objc_skip_typespec (type);
539 type = objc_skip_offset (type);
540 return type;
544 Return the number of arguments that the method MTH expects.
545 Note that all methods need two implicit arguments `self' and
546 `_cmd'.
549 method_get_number_of_arguments (struct objc_method* mth)
551 int i = 0;
552 const char* type = mth->method_types;
553 while (*type)
555 type = objc_skip_argspec (type);
556 i += 1;
558 return i - 1;
562 Return the size of the argument block needed on the stack to invoke
563 the method MTH. This may be zero, if all arguments are passed in
564 registers.
568 method_get_sizeof_arguments (struct objc_method* mth)
570 const char* type = objc_skip_typespec (mth->method_types);
571 return atoi (type);
575 Return a pointer to the next argument of ARGFRAME. type points to
576 the last argument. Typical use of this look like:
579 char *datum, *type;
580 for (datum = method_get_first_argument (method, argframe, &type);
581 datum; datum = method_get_next_argument (argframe, &type))
583 unsigned flags = objc_get_type_qualifiers (type);
584 type = objc_skip_type_qualifiers (type);
585 if (*type != _C_PTR)
586 [portal encodeData: datum ofType: type];
587 else
589 if ((flags & _F_IN) == _F_IN)
590 [portal encodeData: *(char**)datum ofType: ++type];
596 char*
597 method_get_next_argument (arglist_t argframe,
598 const char **type)
600 const char *t = objc_skip_argspec (*type);
602 if (*t == '\0')
603 return 0;
605 *type = t;
606 t = objc_skip_typespec (t);
608 if (*t == '+')
609 return argframe->arg_regs + atoi (++t);
610 else
611 return argframe->arg_ptr + atoi (t);
615 Return a pointer to the value of the first argument of the method
616 described in M with the given argumentframe ARGFRAME. The type
617 is returned in TYPE. type must be passed to successive calls of
618 method_get_next_argument.
620 char*
621 method_get_first_argument (struct objc_method* m,
622 arglist_t argframe,
623 const char** type)
625 *type = m->method_types;
626 return method_get_next_argument (argframe, type);
630 Return a pointer to the ARGth argument of the method
631 M from the frame ARGFRAME. The type of the argument
632 is returned in the value-result argument TYPE
635 char*
636 method_get_nth_argument (struct objc_method* m,
637 arglist_t argframe, int arg,
638 const char **type)
640 const char* t = objc_skip_argspec (m->method_types);
642 if (arg > method_get_number_of_arguments (m))
643 return 0;
645 while (arg--)
646 t = objc_skip_argspec (t);
648 *type = t;
649 t = objc_skip_typespec (t);
651 if (*t == '+')
652 return argframe->arg_regs + atoi (++t);
653 else
654 return argframe->arg_ptr + atoi (t);
657 unsigned
658 objc_get_type_qualifiers (const char* type)
660 unsigned res = 0;
661 BOOL flag = YES;
663 while (flag)
664 switch (*type++)
666 case _C_CONST: res |= _F_CONST; break;
667 case _C_IN: res |= _F_IN; break;
668 case _C_INOUT: res |= _F_INOUT; break;
669 case _C_OUT: res |= _F_OUT; break;
670 case _C_BYCOPY: res |= _F_BYCOPY; break;
671 case _C_BYREF: res |= _F_BYREF; break;
672 case _C_ONEWAY: res |= _F_ONEWAY; break;
673 case _C_GCINVISIBLE: res |= _F_GCINVISIBLE; break;
674 default: flag = NO;
677 return res;
681 /* The following three functions can be used to determine how a
682 structure is laid out by the compiler. For example:
684 struct objc_struct_layout layout;
685 int i;
687 objc_layout_structure (type, &layout);
688 while (objc_layout_structure_next_member (&layout))
690 int position, align;
691 const char *type;
693 objc_layout_structure_get_info (&layout, &position, &align, &type);
694 printf ("element %d has offset %d, alignment %d\n",
695 i++, position, align);
698 These functions are used by objc_sizeof_type and objc_alignof_type
699 functions to compute the size and alignment of structures. The
700 previous method of computing the size and alignment of a structure
701 was not working on some architectures, particulary on AIX, and in
702 the presence of bitfields inside the structure. */
703 void
704 objc_layout_structure (const char *type,
705 struct objc_struct_layout *layout)
707 const char *ntype;
709 if (*type++ != _C_STRUCT_B)
711 objc_error(nil, OBJC_ERR_BAD_TYPE,
712 "record type expected in objc_layout_structure, got %s\n",
713 type);
716 layout->original_type = type;
718 /* Skip "<name>=" if any. Avoid embedded structures and unions. */
719 ntype = type;
720 while (*ntype != _C_STRUCT_E && *ntype != _C_STRUCT_B && *ntype != _C_UNION_B
721 && *ntype++ != '=')
722 /* do nothing */;
724 /* If there's a "<name>=", ntype - 1 points to '='; skip the the name */
725 if (*(ntype - 1) == '=')
726 type = ntype;
728 layout->type = type;
729 layout->prev_type = NULL;
730 layout->record_size = 0;
731 layout->record_align = BITS_PER_UNIT;
733 layout->record_align = MAX (layout->record_align, STRUCTURE_SIZE_BOUNDARY);
737 BOOL
738 objc_layout_structure_next_member (struct objc_struct_layout *layout)
740 register int known_align = layout->record_size;
741 register int desired_align = 0;
743 /* The following are used only if the field is a bitfield */
744 register const char *bfld_type;
745 register int bfld_type_size, bfld_type_align, bfld_field_size;
747 /* The current type without the type qualifiers */
748 const char *type;
750 /* Add the size of the previous field to the size of the record. */
751 if (layout->prev_type)
753 type = objc_skip_type_qualifiers (layout->prev_type);
755 if (*type != _C_BFLD)
756 layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT;
757 else {
758 /* Get the bitfield's type */
759 for (bfld_type = type + 1;
760 isdigit(*bfld_type);
761 bfld_type++)
762 /* do nothing */;
764 bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
765 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
766 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
767 layout->record_size += bfld_field_size;
771 if (*layout->type == _C_STRUCT_E)
772 return NO;
774 /* Skip the variable name if any */
775 if (*layout->type == '"')
777 for (layout->type++; *layout->type++ != '"';)
778 /* do nothing */;
781 type = objc_skip_type_qualifiers (layout->type);
783 if (*type != _C_BFLD)
784 desired_align = objc_alignof_type(type) * BITS_PER_UNIT;
785 else
787 desired_align = 1;
788 /* Skip the bitfield's offset */
789 for (bfld_type = type + 1; isdigit(*bfld_type); bfld_type++)
790 /* do nothing */;
792 bfld_type_size = objc_sizeof_type (bfld_type) * BITS_PER_UNIT;
793 bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
794 bfld_field_size = atoi (objc_skip_typespec (bfld_type));
797 #ifdef BIGGEST_FIELD_ALIGNMENT
798 desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT);
799 #endif
800 #ifdef ADJUST_FIELD_ALIGN
801 desired_align = ADJUST_FIELD_ALIGN (type, desired_align);
802 #endif
804 /* Record must have at least as much alignment as any field.
805 Otherwise, the alignment of the field within the record
806 is meaningless. */
807 #ifndef PCC_BITFIELD_TYPE_MATTERS
808 layout->record_align = MAX (layout->record_align, desired_align);
809 #else
810 if (*type == _C_BFLD)
812 /* For these machines, a zero-length field does not
813 affect the alignment of the structure as a whole.
814 It does, however, affect the alignment of the next field
815 within the structure. */
816 if (bfld_field_size)
817 layout->record_align = MAX (layout->record_align, desired_align);
818 else
819 desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT;
821 /* A named bit field of declared type `int'
822 forces the entire structure to have `int' alignment.
823 Q1: How is encoded this thing and how to check for it?
824 Q2: How to determine maximum_field_alignment at runtime? */
826 /* if (DECL_NAME (field) != 0) */
828 int type_align = bfld_type_align;
829 #if 0
830 if (maximum_field_alignment != 0)
831 type_align = MIN (type_align, maximum_field_alignment);
832 else if (DECL_PACKED (field))
833 type_align = MIN (type_align, BITS_PER_UNIT);
834 #endif
836 layout->record_align = MAX (layout->record_align, type_align);
839 else
840 layout->record_align = MAX (layout->record_align, desired_align);
841 #endif
843 /* Does this field automatically have alignment it needs
844 by virtue of the fields that precede it and the record's
845 own alignment? */
847 if (*type == _C_BFLD)
848 layout->record_size = atoi (type + 1);
849 else if (layout->record_size % desired_align != 0)
851 /* No, we need to skip space before this field.
852 Bump the cumulative size to multiple of field alignment. */
853 layout->record_size = ROUND (layout->record_size, desired_align);
856 /* Jump to the next field in record. */
858 layout->prev_type = layout->type;
859 layout->type = objc_skip_typespec (layout->type); /* skip component */
861 return YES;
865 void objc_layout_finish_structure (struct objc_struct_layout *layout,
866 unsigned int *size,
867 unsigned int *align)
869 if (layout->type && *layout->type == _C_STRUCT_E)
871 /* Work out the alignment of the record as one expression and store
872 in the record type. Round it up to a multiple of the record's
873 alignment. */
875 #if defined(ROUND_TYPE_ALIGN) && !defined(__sparc__)
876 layout->record_align = ROUND_TYPE_ALIGN (layout->original_type,
878 layout->record_align);
879 #else
880 layout->record_align = MAX (1, layout->record_align);
881 #endif
883 #ifdef ROUND_TYPE_SIZE
884 layout->record_size = ROUND_TYPE_SIZE (layout->original_type,
885 layout->record_size,
886 layout->record_align);
887 #else
888 /* Round the size up to be a multiple of the required alignment */
889 layout->record_size = ROUND (layout->record_size, layout->record_align);
890 #endif
892 layout->type = NULL;
894 if (size)
895 *size = layout->record_size / BITS_PER_UNIT;
896 if (align)
897 *align = layout->record_align / BITS_PER_UNIT;
901 void objc_layout_structure_get_info (struct objc_struct_layout *layout,
902 unsigned int *offset,
903 unsigned int *align,
904 const char **type)
906 if (offset)
907 *offset = layout->record_size / BITS_PER_UNIT;
908 if (align)
909 *align = layout->record_align / BITS_PER_UNIT;
910 if (type)
911 *type = layout->prev_type;