/* On Darwin32, we need to recurse until we find the starting stuct type. */ static int _darwin_rs6000_special_round_type_align (const char *struc, int comp, int spec) { const char *_stp , *_fields = TYPE_FIELDS (struc); if (!_fields) return MAX (comp, spec); _stp = strip_array_types (_fields); if (TYPE_MODE(_stp) == _C_COMPLEX) _stp++; switch (TYPE_MODE(_stp)) { case RECORD_TYPE: case UNION_TYPE: return MAX (MAX (comp, spec), objc_alignof_type (_stp) * BITS_PER_UNIT); break; case DFmode: case _C_LNG_LNG: case _C_ULNG_LNG: return MAX (MAX (comp, spec), 64); break; default: return MAX (comp, spec); break; } }
static void __objc_gc_setup_union (GC_bitmap mask, const char *type, int offset) { /* Sub-optimal, quick implementation: assume the union is made of pointers, set up the mask accordingly. */ int i, size, align; /* Skip the variable name */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } size = objc_sizeof_type (type); align = objc_alignof_type (type); offset = ROUND (offset, align); for (i = 0; i < size; i += sizeof (void *)) { SET_BIT_FOR_OFFSET (mask, offset); offset += sizeof (void *); } }
int objc_aligned_size (const char *type) { int size, align; type = objc_skip_variable_name (type); size = objc_sizeof_type (type); align = objc_alignof_type (type); return ROUND (size, align); }
int objc_aligned_size (const char* type) { int size, align; /* Skip the variable name */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } size = objc_sizeof_type (type); align = objc_alignof_type (type); return ROUND (size, align); }
int objc_alignof_type (const char *type) { type = objc_skip_variable_name (type); switch (*type) { case _C_BOOL: return __alignof__ (_Bool); break; case _C_ID: return __alignof__ (id); break; case _C_CLASS: return __alignof__ (Class); break; case _C_SEL: return __alignof__ (SEL); break; case _C_CHR: return __alignof__ (char); break; case _C_UCHR: return __alignof__ (unsigned char); break; case _C_SHT: return __alignof__ (short); break; case _C_USHT: return __alignof__ (unsigned short); break; case _C_INT: return __alignof__ (int); break; case _C_UINT: return __alignof__ (unsigned int); break; case _C_LNG: return __alignof__ (long); break; case _C_ULNG: return __alignof__ (unsigned long); break; case _C_LNG_LNG: return __alignof__ (long long); break; case _C_ULNG_LNG: return __alignof__ (unsigned long long); break; case _C_FLT: return __alignof__ (float); break; case _C_DBL: return __alignof__ (double); break; case _C_LNG_DBL: return __alignof__ (long double); break; case _C_PTR: case _C_ATOM: case _C_CHARPTR: return __alignof__ (char *); break; case _C_ARY_B: while (isdigit ((unsigned char)*++type)) /* do nothing */; return objc_alignof_type (type); case _C_VECTOR: { /* Skip the '!'. */ type++; /* Skip the '['. */ type++; /* Skip the size. */ while (isdigit ((unsigned char)*type)) type++; /* Skip the ','. */ type++; /* The alignment in bytes is the following number. */ return atoi (type); } case _C_STRUCT_B: case _C_UNION_B: { struct objc_struct_layout layout; unsigned int align; objc_layout_structure (type, &layout); while (objc_layout_structure_next_member (&layout)) /* do nothing */; objc_layout_finish_structure (&layout, NULL, &align); return align; } case _C_COMPLEX: { type++; /* Skip after the 'j'. */ switch (*type) { case _C_CHR: return __alignof__ (_Complex char); break; case _C_UCHR: return __alignof__ (_Complex unsigned char); break; case _C_SHT: return __alignof__ (_Complex short); break; case _C_USHT: return __alignof__ (_Complex unsigned short); break; case _C_INT: return __alignof__ (_Complex int); break; case _C_UINT: return __alignof__ (_Complex unsigned int); break; case _C_LNG: return __alignof__ (_Complex long); break; case _C_ULNG: return __alignof__ (_Complex unsigned long); break; case _C_LNG_LNG: return __alignof__ (_Complex long long); break; case _C_ULNG_LNG: return __alignof__ (_Complex unsigned long long); break; case _C_FLT: return __alignof__ (_Complex float); break; case _C_DBL: return __alignof__ (_Complex double); break; case _C_LNG_DBL: return __alignof__ (_Complex long double); break; default: { _objc_abort ("unknown complex type %s\n", type); return 0; } } } default: { _objc_abort ("unknown type %s\n", type); return 0; } } }
BOOL objc_layout_structure_next_member (struct objc_struct_layout *layout) { register int desired_align = 0; /* The following are used only if the field is a bitfield */ register const char *bfld_type = 0; register int bfld_type_align = 0, bfld_field_size = 0; /* The current type without the type qualifiers */ const char *type; BOOL unionp = layout->original_type[-1] == _C_UNION_B; /* Add the size of the previous field to the size of the record. */ if (layout->prev_type) { type = objc_skip_type_qualifiers (layout->prev_type); if (unionp) layout->record_size = MAX (layout->record_size, objc_sizeof_type (type) * BITS_PER_UNIT); else if (*type != _C_BFLD) layout->record_size += objc_sizeof_type (type) * BITS_PER_UNIT; else { /* Get the bitfield's type */ for (bfld_type = type + 1; isdigit ((unsigned char)*bfld_type); bfld_type++) /* do nothing */; bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT; bfld_field_size = atoi (objc_skip_typespec (bfld_type)); layout->record_size += bfld_field_size; } } if ((unionp && *layout->type == _C_UNION_E) || (!unionp && *layout->type == _C_STRUCT_E)) return NO; /* Skip the variable name if any */ layout->type = objc_skip_variable_name (layout->type); type = objc_skip_type_qualifiers (layout->type); if (*type != _C_BFLD) desired_align = objc_alignof_type (type) * BITS_PER_UNIT; else { desired_align = 1; /* Skip the bitfield's offset */ for (bfld_type = type + 1; isdigit ((unsigned char) *bfld_type); bfld_type++) /* do nothing */; bfld_type_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT; bfld_field_size = atoi (objc_skip_typespec (bfld_type)); } /* The following won't work for vectors. */ #ifdef BIGGEST_FIELD_ALIGNMENT desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT); #endif #ifdef ADJUST_FIELD_ALIGN desired_align = ADJUST_FIELD_ALIGN (type, desired_align); #endif /* Record must have at least as much alignment as any field. Otherwise, the alignment of the field within the record is meaningless. */ #ifndef HAVE_BITFIELD_TYPE_MATTERS layout->record_align = MAX (layout->record_align, desired_align); #else /* PCC_BITFIELD_TYPE_MATTERS */ if (*type == _C_BFLD) { /* For these machines, a zero-length field does not affect the alignment of the structure as a whole. It does, however, affect the alignment of the next field within the structure. */ if (bfld_field_size) layout->record_align = MAX (layout->record_align, desired_align); else desired_align = objc_alignof_type (bfld_type) * BITS_PER_UNIT; /* A named bit field of declared type `int' forces the entire structure to have `int' alignment. Q1: How is encoded this thing and how to check for it? Q2: How to determine maximum_field_alignment at runtime? */ /* if (DECL_NAME (field) != 0) */ { int type_align = bfld_type_align; #if 0 if (maximum_field_alignment != 0) type_align = MIN (type_align, maximum_field_alignment); else if (DECL_PACKED (field)) type_align = MIN (type_align, BITS_PER_UNIT); #endif layout->record_align = MAX (layout->record_align, type_align); } } else layout->record_align = MAX (layout->record_align, desired_align); #endif /* PCC_BITFIELD_TYPE_MATTERS */ /* Does this field automatically have alignment it needs by virtue of the fields that precede it and the record's own alignment? */ if (*type == _C_BFLD) layout->record_size = atoi (type + 1); else if (layout->record_size % desired_align != 0) { /* No, we need to skip space before this field. Bump the cumulative size to multiple of field alignment. */ layout->record_size = ROUND (layout->record_size, desired_align); } /* Jump to the next field in record. */ layout->prev_type = layout->type; layout->type = objc_skip_typespec (layout->type); /* skip component */ return YES; }
int objc_alignof_type(const char* type) { /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } switch(*type) { case _C_ID: return __alignof__(id); break; case _C_CLASS: return __alignof__(Class); break; case _C_SEL: return __alignof__(SEL); break; case _C_CHR: return __alignof__(char); break; case _C_UCHR: return __alignof__(unsigned char); break; case _C_SHT: return __alignof__(short); break; case _C_USHT: return __alignof__(unsigned short); break; case _C_INT: return __alignof__(int); break; case _C_UINT: return __alignof__(unsigned int); break; case _C_LNG: return __alignof__(long); break; case _C_ULNG: return __alignof__(unsigned long); break; case _C_LNG_LNG: return __alignof__(long long); break; case _C_ULNG_LNG: return __alignof__(unsigned long long); break; case _C_FLT: return __alignof__(float); break; case _C_DBL: return __alignof__(double); break; case _C_PTR: case _C_ATOM: case _C_CHARPTR: return __alignof__(char*); break; case _C_ARY_B: while (isdigit(*++type)) /* do nothing */; return objc_alignof_type (type); case _C_STRUCT_B: { struct objc_struct_layout layout; unsigned int align; objc_layout_structure (type, &layout); while (objc_layout_structure_next_member (&layout)) /* do nothing */; objc_layout_finish_structure (&layout, NULL, &align); return align; } case _C_UNION_B: { int maxalign = 0; while (*type != _C_UNION_E && *type++ != '=') /* do nothing */; while (*type != _C_UNION_E) { /* Skip the variable name if any */ if (*type == '"') { for (type++; *type++ != '"';) /* do nothing */; } maxalign = MAX (maxalign, objc_alignof_type (type)); type = objc_skip_typespec (type); } return maxalign; } default: { objc_error(nil, OBJC_ERR_BAD_TYPE, "unknown type %s\n", type); return 0; } } }