stor-layout.c revision 52284
1161748Scperciva/* C-compiler utilities for types and variables storage layout 2161748Scperciva Copyright (C) 1987, 88, 92-97, 1998 Free Software Foundation, Inc. 3161748Scperciva 4173441ScpercivaThis file is part of GNU CC. 5161748Scperciva 6161748ScpercivaGNU CC is free software; you can redistribute it and/or modify 7161748Scpercivait under the terms of the GNU General Public License as published by 8161748Scpercivathe Free Software Foundation; either version 2, or (at your option) 9161748Scpercivaany later version. 10161748Scperciva 11161748ScpercivaGNU CC is distributed in the hope that it will be useful, 12161748Scpercivabut WITHOUT ANY WARRANTY; without even the implied warranty of 13161748ScpercivaMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14161748ScpercivaGNU General Public License for more details. 15161748Scperciva 16161748ScpercivaYou should have received a copy of the GNU General Public License 17161748Scpercivaalong with GNU CC; see the file COPYING. If not, write to 18161748Scpercivathe Free Software Foundation, 59 Temple Place - Suite 330, 19161748ScpercivaBoston, MA 02111-1307, USA. */ 20161748Scperciva 21161748Scperciva 22161748Scperciva#include "config.h" 23161748Scperciva#include "system.h" 24161748Scperciva 25161748Scperciva#include "tree.h" 26161748Scperciva#include "rtl.h" 27161748Scperciva#include "flags.h" 28161748Scperciva#include "except.h" 29161748Scperciva#include "function.h" 30161748Scperciva#include "expr.h" 31161748Scperciva#include "toplev.h" 32161748Scperciva 33161748Scperciva#define CEIL(x,y) (((x) + (y) - 1) / (y)) 34161748Scperciva 35161748Scperciva/* Data type for the expressions representing sizes of data types. 36161748Scperciva It is the first integer type laid out. */ 37161748Scperciva 38161748Scpercivastruct sizetype_tab sizetype_tab; 39161748Scperciva 40161748Scperciva/* An integer constant with value 0 whose type is sizetype. */ 41161748Scperciva 42161748Scpercivatree size_zero_node; 43161748Scperciva 44161748Scperciva/* An integer constant with value 1 whose type is sizetype. */ 45161748Scperciva 46161748Scpercivatree size_one_node; 47173564Scperciva 48161748Scperciva/* If nonzero, this is an upper limit on alignment of structure fields. 49161748Scperciva The value is measured in bits. */ 50161748Scpercivaint maximum_field_alignment; 51161748Scperciva 52161748Scperciva/* If non-zero, the alignment of a bitstring or (power-)set value, in bits. 53161748Scperciva May be overridden by front-ends. */ 54161748Scpercivaint set_alignment = 0; 55161748Scperciva 56173564Scpercivastatic tree layout_record PROTO((tree)); 57173564Scpercivastatic void layout_union PROTO((tree)); 58161748Scperciva 59161748Scperciva/* SAVE_EXPRs for sizes of types and decls, waiting to be expanded. */ 60161748Scperciva 61161748Scpercivastatic tree pending_sizes; 62161748Scperciva 63161748Scperciva/* Nonzero means cannot safely call expand_expr now, 64161748Scperciva so put variable sizes onto `pending_sizes' instead. */ 65161748Scperciva 66161748Scpercivaint immediate_size_expand; 67161748Scperciva 68161748Scpercivatree 69161748Scpercivaget_pending_sizes () 70161748Scperciva{ 71161748Scperciva tree chain = pending_sizes; 72161748Scperciva tree t; 73161748Scperciva 74161748Scperciva /* Put each SAVE_EXPR into the current function. */ 75161748Scperciva for (t = chain; t; t = TREE_CHAIN (t)) 76161748Scperciva SAVE_EXPR_CONTEXT (TREE_VALUE (t)) = current_function_decl; 77161748Scperciva pending_sizes = 0; 78161748Scperciva return chain; 79161748Scperciva} 80161748Scperciva 81161748Scpercivavoid 82161748Scpercivaput_pending_sizes (chain) 83161748Scperciva tree chain; 84161748Scperciva{ 85161748Scperciva if (pending_sizes) 86161748Scperciva abort (); 87161748Scperciva 88161748Scperciva pending_sizes = chain; 89173564Scperciva} 90161748Scperciva 91161748Scperciva/* Given a size SIZE that may not be a constant, return a SAVE_EXPR 92161748Scperciva to serve as the actual size-expression for a type or decl. */ 93161748Scperciva 94161748Scpercivatree 95161748Scpercivavariable_size (size) 96161748Scperciva tree size; 97161748Scperciva{ 98161748Scperciva /* If the language-processor is to take responsibility for variable-sized 99161748Scperciva items (e.g., languages which have elaboration procedures like Ada), 100161748Scperciva just return SIZE unchanged. Likewise for self-referential sizes. */ 101161748Scperciva if (TREE_CONSTANT (size) 102161748Scperciva || global_bindings_p () < 0 || contains_placeholder_p (size)) 103161748Scperciva return size; 104161748Scperciva 105161748Scperciva size = save_expr (size); 106161748Scperciva 107161748Scperciva if (global_bindings_p ()) 108161748Scperciva { 109161748Scperciva if (TREE_CONSTANT (size)) 110161748Scperciva error ("type size can't be explicitly evaluated"); 111161748Scperciva else 112161748Scperciva error ("variable-size type declared outside of any function"); 113161748Scperciva 114161748Scperciva return size_int (1); 115161748Scperciva } 116161748Scperciva 117161748Scperciva if (immediate_size_expand) 118161748Scperciva /* NULL_RTX is not defined; neither is the rtx type. 119161748Scperciva Also, we would like to pass const0_rtx here, but don't have it. */ 120161748Scperciva expand_expr (size, expand_expr (integer_zero_node, NULL_PTR, VOIDmode, 0), 121161748Scperciva VOIDmode, 0); 122161748Scperciva else 123161748Scperciva pending_sizes = tree_cons (NULL_TREE, size, pending_sizes); 124161748Scperciva 125161748Scperciva return size; 126161748Scperciva} 127161748Scperciva 128161748Scperciva#ifndef MAX_FIXED_MODE_SIZE 129161748Scperciva#define MAX_FIXED_MODE_SIZE GET_MODE_BITSIZE (DImode) 130161748Scperciva#endif 131161748Scperciva 132161748Scperciva/* Return the machine mode to use for a nonscalar of SIZE bits. 133161748Scperciva The mode must be in class CLASS, and have exactly that many bits. 134161748Scperciva If LIMIT is nonzero, modes of wider than MAX_FIXED_MODE_SIZE will not 135161748Scperciva be used. */ 136161748Scperciva 137161748Scpercivaenum machine_mode 138161748Scpercivamode_for_size (size, class, limit) 139161748Scperciva unsigned int size; 140161748Scperciva enum mode_class class; 141161748Scperciva int limit; 142161748Scperciva{ 143161748Scperciva register enum machine_mode mode; 144161748Scperciva 145161748Scperciva if (limit && size > (unsigned int)(MAX_FIXED_MODE_SIZE)) 146161748Scperciva return BLKmode; 147161748Scperciva 148161748Scperciva /* Get the first mode which has this size, in the specified class. */ 149161748Scperciva for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode; 150161748Scperciva mode = GET_MODE_WIDER_MODE (mode)) 151161748Scperciva if ((unsigned int)GET_MODE_BITSIZE (mode) == size) 152161748Scperciva return mode; 153161748Scperciva 154161748Scperciva return BLKmode; 155161748Scperciva} 156161748Scperciva 157161748Scperciva/* Similar, but never return BLKmode; return the narrowest mode that 158161748Scperciva contains at least the requested number of bits. */ 159161748Scperciva 160161748Scpercivaenum machine_mode 161161748Scpercivasmallest_mode_for_size (size, class) 162161748Scperciva unsigned int size; 163161748Scperciva enum mode_class class; 164161748Scperciva{ 165161748Scperciva register enum machine_mode mode; 166161748Scperciva 167161748Scperciva /* Get the first mode which has at least this size, in the 168161748Scperciva specified class. */ 169161748Scperciva for (mode = GET_CLASS_NARROWEST_MODE (class); mode != VOIDmode; 170161748Scperciva mode = GET_MODE_WIDER_MODE (mode)) 171161748Scperciva if ((unsigned int)GET_MODE_BITSIZE (mode) >= size) 172161748Scperciva return mode; 173161748Scperciva 174161748Scperciva abort (); 175161748Scperciva} 176161748Scperciva 177161748Scperciva/* Find an integer mode of the exact same size, or BLKmode on failure. */ 178161748Scperciva 179161748Scpercivaenum machine_mode 180161748Scpercivaint_mode_for_mode (mode) 181161748Scperciva enum machine_mode mode; 182161748Scperciva{ 183161748Scperciva switch (GET_MODE_CLASS (mode)) 184161748Scperciva { 185161748Scperciva case MODE_INT: 186161748Scperciva case MODE_PARTIAL_INT: 187161748Scperciva break; 188161748Scperciva 189161748Scperciva case MODE_COMPLEX_INT: 190161748Scperciva case MODE_COMPLEX_FLOAT: 191161748Scperciva case MODE_FLOAT: 192161748Scperciva mode = mode_for_size (GET_MODE_BITSIZE (mode), MODE_INT, 0); 193161748Scperciva break; 194161748Scperciva 195161748Scperciva case MODE_RANDOM: 196161748Scperciva if (mode == BLKmode) 197161748Scperciva break; 198161748Scperciva /* FALLTHRU */ 199161748Scperciva 200161748Scperciva case MODE_CC: 201161748Scperciva default: 202161748Scperciva abort(); 203161748Scperciva } 204161748Scperciva 205161748Scperciva return mode; 206161748Scperciva} 207161748Scperciva 208161748Scperciva/* Return the value of VALUE, rounded up to a multiple of DIVISOR. */ 209161748Scperciva 210161748Scpercivatree 211161748Scpercivaround_up (value, divisor) 212161748Scperciva tree value; 213161748Scperciva int divisor; 214161748Scperciva{ 215161748Scperciva return size_binop (MULT_EXPR, 216161748Scperciva size_binop (CEIL_DIV_EXPR, value, size_int (divisor)), 217161748Scperciva size_int (divisor)); 218161748Scperciva} 219161748Scperciva 220161748Scperciva/* Set the size, mode and alignment of a ..._DECL node. 221161748Scperciva TYPE_DECL does need this for C++. 222161748Scperciva Note that LABEL_DECL and CONST_DECL nodes do not need this, 223161748Scperciva and FUNCTION_DECL nodes have them set up in a special (and simple) way. 224161748Scperciva Don't call layout_decl for them. 225161748Scperciva 226161748Scperciva KNOWN_ALIGN is the amount of alignment we can assume this 227161748Scperciva decl has with no special effort. It is relevant only for FIELD_DECLs 228161748Scperciva and depends on the previous fields. 229161748Scperciva All that matters about KNOWN_ALIGN is which powers of 2 divide it. 230161748Scperciva If KNOWN_ALIGN is 0, it means, "as much alignment as you like": 231161748Scperciva the record will be aligned to suit. */ 232161748Scperciva 233173564Scpercivavoid 234173564Scpercivalayout_decl (decl, known_align) 235173564Scperciva tree decl; 236173564Scperciva unsigned known_align; 237173564Scperciva{ 238173564Scperciva register tree type = TREE_TYPE (decl); 239173564Scperciva register enum tree_code code = TREE_CODE (decl); 240173564Scperciva int spec_size = DECL_FIELD_SIZE (decl); 241161748Scperciva 242161748Scperciva if (code == CONST_DECL) 243161748Scperciva return; 244161748Scperciva 245161748Scperciva if (code != VAR_DECL && code != PARM_DECL && code != RESULT_DECL 246161748Scperciva && code != FIELD_DECL && code != TYPE_DECL) 247161748Scperciva abort (); 248161748Scperciva 249161748Scperciva if (type == error_mark_node) 250173564Scperciva { 251173564Scperciva type = void_type_node; 252173564Scperciva spec_size = 0; 253173564Scperciva } 254173564Scperciva 255173564Scperciva /* Usually the size and mode come from the data type without change. */ 256173564Scperciva 257173564Scperciva DECL_MODE (decl) = TYPE_MODE (type); 258173564Scperciva TREE_UNSIGNED (decl) = TREE_UNSIGNED (type); 259173564Scperciva if (DECL_SIZE (decl) == 0) 260173564Scperciva DECL_SIZE (decl) = TYPE_SIZE (type); 261173564Scperciva 262173564Scperciva if (code == FIELD_DECL && DECL_BIT_FIELD (decl)) 263173564Scperciva { 264173564Scperciva if (spec_size == 0 && DECL_NAME (decl) != 0) 265173564Scperciva abort (); 266173564Scperciva 267173564Scperciva /* Size is specified number of bits. */ 268173564Scperciva DECL_SIZE (decl) = size_int (spec_size); 269173564Scperciva } 270173564Scperciva /* Force alignment required for the data type. 271173564Scperciva But if the decl itself wants greater alignment, don't override that. 272173564Scperciva Likewise, if the decl is packed, don't override it. */ 273173564Scperciva else if (DECL_ALIGN (decl) == 0 274173564Scperciva || (! DECL_PACKED (decl) && TYPE_ALIGN (type) > DECL_ALIGN (decl))) 275173564Scperciva DECL_ALIGN (decl) = TYPE_ALIGN (type); 276173564Scperciva 277173564Scperciva /* See if we can use an ordinary integer mode for a bit-field. */ 278173564Scperciva /* Conditions are: a fixed size that is correct for another mode 279173564Scperciva and occupying a complete byte or bytes on proper boundary. */ 280161748Scperciva if (code == FIELD_DECL) 281161748Scperciva { 282161748Scperciva DECL_BIT_FIELD_TYPE (decl) = DECL_BIT_FIELD (decl) ? type : 0; 283161748Scperciva if (maximum_field_alignment != 0) 284161748Scperciva DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), 285161748Scperciva (unsigned)maximum_field_alignment); 286161748Scperciva else if (DECL_PACKED (decl)) 287161748Scperciva DECL_ALIGN (decl) = MIN (DECL_ALIGN (decl), BITS_PER_UNIT); 288161748Scperciva } 289161748Scperciva 290161748Scperciva if (DECL_BIT_FIELD (decl) 291161748Scperciva && TYPE_SIZE (type) != 0 292161748Scperciva && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST 293161748Scperciva && GET_MODE_CLASS (TYPE_MODE (type)) == MODE_INT) 294161748Scperciva { 295161748Scperciva register enum machine_mode xmode 296161748Scperciva = mode_for_size (TREE_INT_CST_LOW (DECL_SIZE (decl)), MODE_INT, 1); 297161748Scperciva 298161748Scperciva if (xmode != BLKmode 299161748Scperciva && known_align % GET_MODE_ALIGNMENT (xmode) == 0) 300161748Scperciva { 301161748Scperciva DECL_ALIGN (decl) = MAX ((unsigned) GET_MODE_ALIGNMENT (xmode), 302161748Scperciva DECL_ALIGN (decl)); 303161748Scperciva DECL_MODE (decl) = xmode; 304161748Scperciva DECL_SIZE (decl) = size_int (GET_MODE_BITSIZE (xmode)); 305161748Scperciva /* This no longer needs to be accessed as a bit field. */ 306161748Scperciva DECL_BIT_FIELD (decl) = 0; 307161748Scperciva } 308161748Scperciva } 309161748Scperciva 310161748Scperciva /* Turn off DECL_BIT_FIELD if we won't need it set. */ 311161748Scperciva if (DECL_BIT_FIELD (decl) && TYPE_MODE (type) == BLKmode 312161748Scperciva && known_align % TYPE_ALIGN (type) == 0 313161748Scperciva && DECL_SIZE (decl) != 0 314161748Scperciva && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST 315161748Scperciva || (TREE_INT_CST_LOW (DECL_SIZE (decl)) % BITS_PER_UNIT) == 0) 316161748Scperciva && DECL_ALIGN (decl) >= TYPE_ALIGN (type)) 317161748Scperciva DECL_BIT_FIELD (decl) = 0; 318161748Scperciva 319161748Scperciva /* Evaluate nonconstant size only once, either now or as soon as safe. */ 320161748Scperciva if (DECL_SIZE (decl) != 0 && TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST) 321161748Scperciva DECL_SIZE (decl) = variable_size (DECL_SIZE (decl)); 322161748Scperciva} 323161748Scperciva 324161748Scperciva/* Lay out a RECORD_TYPE type (a C struct). 325161748Scperciva This means laying out the fields, determining their positions, 326161748Scperciva and computing the overall size and required alignment of the record. 327161748Scperciva Note that if you set the TYPE_ALIGN before calling this 328161748Scperciva then the struct is aligned to at least that boundary. 329161748Scperciva 330161748Scperciva If the type has basetypes, you must call layout_basetypes 331161748Scperciva before calling this function. 332161748Scperciva 333161748Scperciva The return value is a list of static members of the record. 334161748Scperciva They still need to be laid out. */ 335161748Scperciva 336161748Scpercivastatic tree 337161748Scpercivalayout_record (rec) 338161748Scperciva tree rec; 339161748Scperciva{ 340161748Scperciva register tree field; 341161748Scperciva unsigned record_align = MAX (BITS_PER_UNIT, TYPE_ALIGN (rec)); 342161748Scperciva /* These must be laid out *after* the record is. */ 343161748Scperciva tree pending_statics = NULL_TREE; 344161748Scperciva /* Record size so far is CONST_SIZE + VAR_SIZE bits, 345161748Scperciva where CONST_SIZE is an integer 346161748Scperciva and VAR_SIZE is a tree expression. 347161748Scperciva If VAR_SIZE is null, the size is just CONST_SIZE. 348161748Scperciva Naturally we try to avoid using VAR_SIZE. */ 349161748Scperciva register HOST_WIDE_INT const_size = 0; 350161748Scperciva register tree var_size = 0; 351161748Scperciva /* Once we start using VAR_SIZE, this is the maximum alignment 352161748Scperciva that we know VAR_SIZE has. */ 353161748Scperciva register int var_align = BITS_PER_UNIT; 354161748Scperciva 355161748Scperciva#ifdef STRUCTURE_SIZE_BOUNDARY 356173564Scperciva /* Packed structures don't need to have minimum size. */ 357173564Scperciva if (! TYPE_PACKED (rec)) 358173564Scperciva record_align = MAX (record_align, STRUCTURE_SIZE_BOUNDARY); 359173564Scperciva#endif 360161748Scperciva 361161748Scperciva for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field)) 362161748Scperciva { 363161748Scperciva register int known_align = var_size ? var_align : const_size; 364161748Scperciva register int desired_align = 0; 365161748Scperciva 366161748Scperciva /* If FIELD is static, then treat it like a separate variable, 367161748Scperciva not really like a structure field. 368161748Scperciva If it is a FUNCTION_DECL, it's a method. 369161748Scperciva In both cases, all we do is lay out the decl, 370161748Scperciva and we do it *after* the record is laid out. */ 371161748Scperciva 372161748Scperciva if (TREE_CODE (field) == VAR_DECL) 373161748Scperciva { 374161748Scperciva pending_statics = tree_cons (NULL_TREE, field, pending_statics); 375161748Scperciva continue; 376161748Scperciva } 377161748Scperciva /* Enumerators and enum types which are local to this class need not 378173564Scperciva be laid out. Likewise for initialized constant fields. */ 379161748Scperciva if (TREE_CODE (field) != FIELD_DECL) 380161748Scperciva continue; 381161748Scperciva 382161748Scperciva /* Lay out the field so we know what alignment it needs. 383161748Scperciva For a packed field, use the alignment as specified, 384161748Scperciva disregarding what the type would want. */ 385161748Scperciva if (DECL_PACKED (field)) 386161748Scperciva desired_align = DECL_ALIGN (field); 387161748Scperciva layout_decl (field, known_align); 388161748Scperciva if (! DECL_PACKED (field)) 389161748Scperciva desired_align = DECL_ALIGN (field); 390161748Scperciva /* Some targets (i.e. VMS) limit struct field alignment 391161748Scperciva to a lower boundary than alignment of variables. */ 392161748Scperciva#ifdef BIGGEST_FIELD_ALIGNMENT 393161748Scperciva desired_align = MIN (desired_align, BIGGEST_FIELD_ALIGNMENT); 394161748Scperciva#endif 395161748Scperciva#ifdef ADJUST_FIELD_ALIGN 396161748Scperciva desired_align = ADJUST_FIELD_ALIGN (field, desired_align); 397161748Scperciva#endif 398161748Scperciva 399161748Scperciva /* Record must have at least as much alignment as any field. 400161748Scperciva Otherwise, the alignment of the field within the record 401161748Scperciva is meaningless. */ 402161748Scperciva 403161748Scperciva#ifndef PCC_BITFIELD_TYPE_MATTERS 404161748Scperciva record_align = MAX (record_align, desired_align); 405161748Scperciva#else 406161748Scperciva if (PCC_BITFIELD_TYPE_MATTERS && TREE_TYPE (field) != error_mark_node 407161748Scperciva && DECL_BIT_FIELD_TYPE (field) 408161748Scperciva && ! integer_zerop (TYPE_SIZE (TREE_TYPE (field)))) 409161748Scperciva { 410161748Scperciva /* For these machines, a zero-length field does not 411161748Scperciva affect the alignment of the structure as a whole. 412161748Scperciva It does, however, affect the alignment of the next field 413161748Scperciva within the structure. */ 414161748Scperciva if (! integer_zerop (DECL_SIZE (field))) 415161748Scperciva record_align = MAX ((int)record_align, desired_align); 416161748Scperciva else if (! DECL_PACKED (field)) 417161748Scperciva desired_align = TYPE_ALIGN (TREE_TYPE (field)); 418161748Scperciva /* A named bit field of declared type `int' 419161748Scperciva forces the entire structure to have `int' alignment. */ 420161748Scperciva if (DECL_NAME (field) != 0) 421161748Scperciva { 422161748Scperciva int type_align = TYPE_ALIGN (TREE_TYPE (field)); 423161748Scperciva if (maximum_field_alignment != 0) 424161748Scperciva type_align = MIN (type_align, maximum_field_alignment); 425161748Scperciva else if (DECL_PACKED (field)) 426161748Scperciva type_align = MIN (type_align, BITS_PER_UNIT); 427161748Scperciva 428161748Scperciva record_align = MAX ((int)record_align, type_align); 429161748Scperciva } 430161748Scperciva } 431161748Scperciva else 432161748Scperciva record_align = MAX ((int)record_align, desired_align); 433161748Scperciva#endif 434161748Scperciva 435161748Scperciva /* Does this field automatically have alignment it needs 436161748Scperciva by virtue of the fields that precede it and the record's 437161748Scperciva own alignment? */ 438161748Scperciva 439161748Scperciva if (const_size % desired_align != 0 440161748Scperciva || (var_align % desired_align != 0 441161748Scperciva && var_size != 0)) 442161748Scperciva { 443161748Scperciva /* No, we need to skip space before this field. 444161748Scperciva Bump the cumulative size to multiple of field alignment. */ 445161748Scperciva 446161748Scperciva if (var_size == 0 447161748Scperciva || var_align % desired_align == 0) 448161748Scperciva const_size 449161748Scperciva = CEIL (const_size, desired_align) * desired_align; 450161748Scperciva else 451161748Scperciva { 452161748Scperciva if (const_size > 0) 453161748Scperciva var_size = size_binop (PLUS_EXPR, var_size, 454173564Scperciva bitsize_int (const_size, 0L)); 455161748Scperciva const_size = 0; 456161748Scperciva var_size = round_up (var_size, desired_align); 457161748Scperciva var_align = MIN (var_align, desired_align); 458161748Scperciva } 459161748Scperciva } 460161748Scperciva 461161748Scperciva#ifdef PCC_BITFIELD_TYPE_MATTERS 462161748Scperciva if (PCC_BITFIELD_TYPE_MATTERS 463161748Scperciva && TREE_CODE (field) == FIELD_DECL 464161748Scperciva && TREE_TYPE (field) != error_mark_node 465161748Scperciva && DECL_BIT_FIELD_TYPE (field) 466161748Scperciva && !DECL_PACKED (field) 467161748Scperciva && maximum_field_alignment == 0 468161748Scperciva && !integer_zerop (DECL_SIZE (field))) 469161748Scperciva { 470161748Scperciva int type_align = TYPE_ALIGN (TREE_TYPE (field)); 471161748Scperciva register tree dsize = DECL_SIZE (field); 472161748Scperciva int field_size = TREE_INT_CST_LOW (dsize); 473161748Scperciva 474161748Scperciva /* A bit field may not span more units of alignment of its type 475161748Scperciva than its type itself. Advance to next boundary if necessary. */ 476161748Scperciva if (((const_size + field_size + type_align - 1) / type_align 477161748Scperciva - const_size / type_align) 478161748Scperciva > TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (field))) / type_align) 479161748Scperciva const_size = CEIL (const_size, type_align) * type_align; 480161748Scperciva } 481161748Scperciva#endif 482161748Scperciva 483161748Scperciva/* No existing machine description uses this parameter. 484161748Scperciva So I have made it in this aspect identical to PCC_BITFIELD_TYPE_MATTERS. */ 485161748Scperciva#ifdef BITFIELD_NBYTES_LIMITED 486161748Scperciva if (BITFIELD_NBYTES_LIMITED 487161748Scperciva && TREE_CODE (field) == FIELD_DECL 488161748Scperciva && TREE_TYPE (field) != error_mark_node 489161748Scperciva && DECL_BIT_FIELD_TYPE (field) 490161748Scperciva && !DECL_PACKED (field) 491161748Scperciva && !integer_zerop (DECL_SIZE (field))) 492161748Scperciva { 493161748Scperciva int type_align = TYPE_ALIGN (TREE_TYPE (field)); 494161748Scperciva register tree dsize = DECL_SIZE (field); 495161748Scperciva int field_size = TREE_INT_CST_LOW (dsize); 496161748Scperciva 497161748Scperciva if (maximum_field_alignment != 0) 498161748Scperciva type_align = MIN (type_align, maximum_field_alignment); 499161748Scperciva /* ??? This test is opposite the test in the containing if 500161748Scperciva statement, so this code is unreachable currently. */ 501161748Scperciva else if (DECL_PACKED (field)) 502161748Scperciva type_align = MIN (type_align, BITS_PER_UNIT); 503161748Scperciva 504161748Scperciva /* A bit field may not span the unit of alignment of its type. 505161748Scperciva Advance to next boundary if necessary. */ 506161748Scperciva /* ??? This code should match the code above for the 507161748Scperciva PCC_BITFIELD_TYPE_MATTERS case. */ 508161748Scperciva if (const_size / type_align 509161748Scperciva != (const_size + field_size - 1) / type_align) 510161748Scperciva const_size = CEIL (const_size, type_align) * type_align; 511161748Scperciva } 512161748Scperciva#endif 513161748Scperciva 514161748Scperciva /* Size so far becomes the position of this field. */ 515161748Scperciva 516161748Scperciva if (var_size && const_size) 517161748Scperciva DECL_FIELD_BITPOS (field) 518161748Scperciva = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L)); 519161748Scperciva else if (var_size) 520161748Scperciva DECL_FIELD_BITPOS (field) = var_size; 521161748Scperciva else 522161748Scperciva { 523161748Scperciva DECL_FIELD_BITPOS (field) = size_int (const_size); 524161748Scperciva 525161748Scperciva /* If this field ended up more aligned than we thought it 526161748Scperciva would be (we approximate this by seeing if its position 527161748Scperciva changed), lay out the field again; perhaps we can use an 528161748Scperciva integral mode for it now. */ 529161748Scperciva if (known_align != const_size) 530161748Scperciva layout_decl (field, const_size); 531161748Scperciva } 532161748Scperciva 533161748Scperciva /* Now add size of this field to the size of the record. */ 534161748Scperciva 535161748Scperciva { 536161748Scperciva register tree dsize = DECL_SIZE (field); 537161748Scperciva 538173564Scperciva /* This can happen when we have an invalid nested struct definition, 539161748Scperciva such as struct j { struct j { int i; } }. The error message is 540161748Scperciva printed in finish_struct. */ 541161748Scperciva if (dsize == 0) 542161748Scperciva /* Do nothing. */; 543161748Scperciva else if (TREE_CODE (dsize) == INTEGER_CST 544161748Scperciva && ! TREE_CONSTANT_OVERFLOW (dsize) 545161748Scperciva && TREE_INT_CST_HIGH (dsize) == 0 546161748Scperciva && TREE_INT_CST_LOW (dsize) + const_size >= const_size) 547161748Scperciva /* Use const_size if there's no overflow. */ 548167189Scperciva const_size += TREE_INT_CST_LOW (dsize); 549167189Scperciva else 550167189Scperciva { 551167189Scperciva if (var_size == 0) 552167189Scperciva var_size = dsize; 553167189Scperciva else 554167189Scperciva var_size = size_binop (PLUS_EXPR, var_size, dsize); 555167189Scperciva } 556167189Scperciva } 557167189Scperciva } 558167189Scperciva 559167189Scperciva /* Work out the total size and alignment of the record 560167189Scperciva as one expression and store in the record type. 561167189Scperciva Round it up to a multiple of the record's alignment. */ 562167189Scperciva 563167189Scperciva if (var_size == 0) 564167189Scperciva { 565167189Scperciva TYPE_SIZE (rec) = size_int (const_size); 566161748Scperciva } 567161748Scperciva else 568161748Scperciva { 569161748Scperciva if (const_size) 570161748Scperciva var_size 571161748Scperciva = size_binop (PLUS_EXPR, var_size, bitsize_int (const_size, 0L)); 572161748Scperciva TYPE_SIZE (rec) = var_size; 573161748Scperciva } 574161748Scperciva 575161748Scperciva /* Determine the desired alignment. */ 576161748Scperciva#ifdef ROUND_TYPE_ALIGN 577161748Scperciva TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), record_align); 578173564Scperciva#else 579173564Scperciva TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), record_align); 580173564Scperciva#endif 581173564Scperciva 582173564Scperciva /* Record the un-rounded size in the binfo node. But first we check 583173564Scperciva the size of TYPE_BINFO to make sure that BINFO_SIZE is available. */ 584173564Scperciva if (TYPE_BINFO (rec) && TREE_VEC_LENGTH (TYPE_BINFO (rec)) > 6) 585173564Scperciva TYPE_BINFO_SIZE (rec) = TYPE_SIZE (rec); 586173564Scperciva 587173564Scperciva#ifdef ROUND_TYPE_SIZE 588173564Scperciva TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec)); 589173564Scperciva#else 590173564Scperciva /* Round the size up to be a multiple of the required alignment */ 591173564Scperciva TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec)); 592173564Scperciva#endif 593173564Scperciva 594173564Scperciva return pending_statics; 595173564Scperciva} 596173564Scperciva 597173564Scperciva/* Lay out a UNION_TYPE or QUAL_UNION_TYPE type. 598173564Scperciva Lay out all the fields, set their positions to zero, 599173564Scperciva and compute the size and alignment of the union (maximum of any field). 600173564Scperciva Note that if you set the TYPE_ALIGN before calling this 601173564Scperciva then the union align is aligned to at least that boundary. */ 602173564Scperciva 603173564Scpercivastatic void 604173564Scpercivalayout_union (rec) 605173564Scperciva tree rec; 606173564Scperciva{ 607173564Scperciva register tree field; 608173564Scperciva unsigned union_align = BITS_PER_UNIT; 609173564Scperciva 610173564Scperciva /* The size of the union, based on the fields scanned so far, 611173564Scperciva is max (CONST_SIZE, VAR_SIZE). 612173564Scperciva VAR_SIZE may be null; then CONST_SIZE by itself is the size. */ 613173564Scperciva register int const_size = 0; 614173564Scperciva register tree var_size = 0; 615173564Scperciva 616173564Scperciva#ifdef STRUCTURE_SIZE_BOUNDARY 617173564Scperciva /* Packed structures don't need to have minimum size. */ 618161748Scperciva if (! TYPE_PACKED (rec)) 619161748Scperciva union_align = STRUCTURE_SIZE_BOUNDARY; 620161748Scperciva#endif 621161748Scperciva 622161748Scperciva /* If this is a QUAL_UNION_TYPE, we want to process the fields in 623161748Scperciva the reverse order in building the COND_EXPR that denotes its 624161748Scperciva size. We reverse them again later. */ 625161748Scperciva if (TREE_CODE (rec) == QUAL_UNION_TYPE) 626161748Scperciva TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec)); 627173441Scperciva 628173441Scperciva for (field = TYPE_FIELDS (rec); field; field = TREE_CHAIN (field)) 629173441Scperciva { 630173441Scperciva /* Enums which are local to this class need not be laid out. */ 631173441Scperciva if (TREE_CODE (field) == CONST_DECL || TREE_CODE (field) == TYPE_DECL) 632173441Scperciva continue; 633173441Scperciva 634161748Scperciva layout_decl (field, 0); 635161748Scperciva DECL_FIELD_BITPOS (field) = bitsize_int (0L, 0L); 636161748Scperciva 637161748Scperciva /* Union must be at least as aligned as any field requires. */ 638161748Scperciva 639161748Scperciva union_align = MAX (union_align, DECL_ALIGN (field)); 640161748Scperciva 641161748Scperciva#ifdef PCC_BITFIELD_TYPE_MATTERS 642161748Scperciva /* On the m88000, a bit field of declare type `int' 643161748Scperciva forces the entire union to have `int' alignment. */ 644161748Scperciva if (PCC_BITFIELD_TYPE_MATTERS && DECL_BIT_FIELD_TYPE (field)) 645161748Scperciva union_align = MAX (union_align, TYPE_ALIGN (TREE_TYPE (field))); 646161748Scperciva#endif 647161748Scperciva 648161748Scperciva if (TREE_CODE (rec) == UNION_TYPE) 649161748Scperciva { 650161748Scperciva /* Set union_size to max (decl_size, union_size). 651161748Scperciva There are more and less general ways to do this. 652161748Scperciva Use only CONST_SIZE unless forced to use VAR_SIZE. */ 653161748Scperciva 654161748Scperciva if (TREE_CODE (DECL_SIZE (field)) == INTEGER_CST) 655161748Scperciva const_size 656161748Scperciva = MAX (const_size, TREE_INT_CST_LOW (DECL_SIZE (field))); 657161748Scperciva else if (var_size == 0) 658161748Scperciva var_size = DECL_SIZE (field); 659161748Scperciva else 660161748Scperciva var_size = size_binop (MAX_EXPR, var_size, DECL_SIZE (field)); 661161748Scperciva } 662161748Scperciva else if (TREE_CODE (rec) == QUAL_UNION_TYPE) 663161748Scperciva var_size = fold (build (COND_EXPR, sizetype, DECL_QUALIFIER (field), 664161748Scperciva DECL_SIZE (field), 665161748Scperciva var_size ? var_size : bitsize_int (0L, 0L))); 666161748Scperciva } 667161748Scperciva 668161748Scperciva if (TREE_CODE (rec) == QUAL_UNION_TYPE) 669161748Scperciva TYPE_FIELDS (rec) = nreverse (TYPE_FIELDS (rec)); 670161748Scperciva 671161748Scperciva /* Determine the ultimate size of the union (in bytes). */ 672161748Scperciva if (NULL == var_size) 673161748Scperciva TYPE_SIZE (rec) = bitsize_int (CEIL (const_size, BITS_PER_UNIT) 674161748Scperciva * BITS_PER_UNIT, 0L); 675161748Scperciva else if (const_size == 0) 676161748Scperciva TYPE_SIZE (rec) = var_size; 677161748Scperciva else 678161748Scperciva TYPE_SIZE (rec) = size_binop (MAX_EXPR, var_size, 679161748Scperciva round_up (bitsize_int (const_size, 0L), 680161748Scperciva BITS_PER_UNIT)); 681161748Scperciva 682161748Scperciva /* Determine the desired alignment. */ 683161748Scperciva#ifdef ROUND_TYPE_ALIGN 684161748Scperciva TYPE_ALIGN (rec) = ROUND_TYPE_ALIGN (rec, TYPE_ALIGN (rec), union_align); 685161748Scperciva#else 686161748Scperciva TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), union_align); 687161748Scperciva#endif 688161748Scperciva 689161748Scperciva#ifdef ROUND_TYPE_SIZE 690161748Scperciva TYPE_SIZE (rec) = ROUND_TYPE_SIZE (rec, TYPE_SIZE (rec), TYPE_ALIGN (rec)); 691161748Scperciva#else 692161748Scperciva /* Round the size up to be a multiple of the required alignment */ 693161748Scperciva TYPE_SIZE (rec) = round_up (TYPE_SIZE (rec), TYPE_ALIGN (rec)); 694161748Scperciva#endif 695161748Scperciva} 696161748Scperciva 697161748Scperciva/* Calculate the mode, size, and alignment for TYPE. 698161748Scperciva For an array type, calculate the element separation as well. 699161748Scperciva Record TYPE on the chain of permanent or temporary types 700161748Scperciva so that dbxout will find out about it. 701161748Scperciva 702161748Scperciva TYPE_SIZE of a type is nonzero if the type has been laid out already. 703161748Scperciva layout_type does nothing on such a type. 704161748Scperciva 705161748Scperciva If the type is incomplete, its TYPE_SIZE remains zero. */ 706161748Scperciva 707161748Scpercivavoid 708161748Scpercivalayout_type (type) 709161748Scperciva tree type; 710161748Scperciva{ 711161748Scperciva int old; 712161748Scperciva tree pending_statics; 713161748Scperciva 714161748Scperciva if (type == 0) 715161748Scperciva abort (); 716161748Scperciva 717161748Scperciva /* Do nothing if type has been laid out before. */ 718161748Scperciva if (TYPE_SIZE (type)) 719161748Scperciva return; 720161748Scperciva 721161748Scperciva /* Make sure all nodes we allocate are not momentary; 722161748Scperciva they must last past the current statement. */ 723161748Scperciva old = suspend_momentary (); 724161748Scperciva 725161748Scperciva /* Put all our nodes into the same obstack as the type. Also, 726161748Scperciva make expressions saveable (this is a no-op for permanent types). */ 727161748Scperciva 728161748Scperciva push_obstacks (TYPE_OBSTACK (type), TYPE_OBSTACK (type)); 729161748Scperciva saveable_allocation (); 730161748Scperciva 731161748Scperciva switch (TREE_CODE (type)) 732161748Scperciva { 733161748Scperciva case LANG_TYPE: 734161748Scperciva /* This kind of type is the responsibility 735161748Scperciva of the language-specific code. */ 736161748Scperciva abort (); 737161748Scperciva 738161748Scperciva case BOOLEAN_TYPE: /* Used for Java, Pascal, and Chill. */ 739161748Scperciva if (TYPE_PRECISION (type) == 0) 740161748Scperciva TYPE_PRECISION (type) = 1; /* default to one byte/boolean. */ 741161748Scperciva /* ... fall through ... */ 742161748Scperciva 743161748Scperciva case INTEGER_TYPE: 744161748Scperciva case ENUMERAL_TYPE: 745161748Scperciva case CHAR_TYPE: 746161748Scperciva if (TREE_CODE (TYPE_MIN_VALUE (type)) == INTEGER_CST 747161748Scperciva && tree_int_cst_sgn (TYPE_MIN_VALUE (type)) >= 0) 748161748Scperciva TREE_UNSIGNED (type) = 1; 749161748Scperciva 750161748Scperciva TYPE_MODE (type) = smallest_mode_for_size (TYPE_PRECISION (type), 751161748Scperciva MODE_INT); 752161748Scperciva TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L); 753161748Scperciva TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type))); 754161748Scperciva break; 755161748Scperciva 756161748Scperciva case REAL_TYPE: 757161748Scperciva TYPE_MODE (type) = mode_for_size (TYPE_PRECISION (type), MODE_FLOAT, 0); 758161748Scperciva TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L); 759161748Scperciva TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type))); 760161748Scperciva break; 761161748Scperciva 762161748Scperciva case COMPLEX_TYPE: 763161748Scperciva TREE_UNSIGNED (type) = TREE_UNSIGNED (TREE_TYPE (type)); 764161748Scperciva TYPE_MODE (type) 765161748Scperciva = mode_for_size (2 * TYPE_PRECISION (TREE_TYPE (type)), 766161748Scperciva (TREE_CODE (TREE_TYPE (type)) == INTEGER_TYPE 767161748Scperciva ? MODE_COMPLEX_INT : MODE_COMPLEX_FLOAT), 768161748Scperciva 0); 769161748Scperciva TYPE_SIZE (type) = bitsize_int (GET_MODE_BITSIZE (TYPE_MODE (type)), 0L); 770161748Scperciva TYPE_SIZE_UNIT (type) = size_int (GET_MODE_SIZE (TYPE_MODE (type))); 771161748Scperciva break; 772161748Scperciva 773161748Scperciva case VOID_TYPE: 774161748Scperciva TYPE_SIZE (type) = size_zero_node; 775161748Scperciva TYPE_SIZE_UNIT (type) = size_zero_node; 776161748Scperciva TYPE_ALIGN (type) = 1; 777161748Scperciva TYPE_MODE (type) = VOIDmode; 778161748Scperciva break; 779161748Scperciva 780161748Scperciva case OFFSET_TYPE: 781161748Scperciva TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L); 782161748Scperciva TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT); 783161748Scperciva TYPE_MODE (type) = ptr_mode; 784161748Scperciva break; 785161748Scperciva 786161748Scperciva case FUNCTION_TYPE: 787161748Scperciva case METHOD_TYPE: 788161748Scperciva TYPE_MODE (type) = mode_for_size (2 * POINTER_SIZE, MODE_INT, 0); 789161748Scperciva TYPE_SIZE (type) = bitsize_int (2 * POINTER_SIZE, 0); 790161748Scperciva TYPE_SIZE_UNIT (type) = size_int ((2 * POINTER_SIZE) / BITS_PER_UNIT); 791161748Scperciva break; 792161748Scperciva 793161748Scperciva case POINTER_TYPE: 794161748Scperciva case REFERENCE_TYPE: 795161748Scperciva TYPE_MODE (type) = ptr_mode; 796161748Scperciva TYPE_SIZE (type) = bitsize_int (POINTER_SIZE, 0L); 797161748Scperciva TYPE_SIZE_UNIT (type) = size_int (POINTER_SIZE / BITS_PER_UNIT); 798161748Scperciva TREE_UNSIGNED (type) = 1; 799161748Scperciva TYPE_PRECISION (type) = POINTER_SIZE; 800161748Scperciva break; 801161748Scperciva 802161748Scperciva case ARRAY_TYPE: 803161748Scperciva { 804161748Scperciva register tree index = TYPE_DOMAIN (type); 805161748Scperciva register tree element = TREE_TYPE (type); 806161748Scperciva 807161748Scperciva build_pointer_type (element); 808161748Scperciva 809161748Scperciva /* We need to know both bounds in order to compute the size. */ 810161748Scperciva if (index && TYPE_MAX_VALUE (index) && TYPE_MIN_VALUE (index) 811161748Scperciva && TYPE_SIZE (element)) 812161748Scperciva { 813161748Scperciva tree ub = TYPE_MAX_VALUE (index); 814161748Scperciva tree lb = TYPE_MIN_VALUE (index); 815161748Scperciva tree length; 816161748Scperciva tree element_size; 817161748Scperciva 818161748Scperciva /* If UB is max (lb - 1, x), remove the MAX_EXPR since the 819161748Scperciva test for negative below covers it. */ 820161748Scperciva if (TREE_CODE (ub) == MAX_EXPR 821161748Scperciva && TREE_CODE (TREE_OPERAND (ub, 0)) == MINUS_EXPR 822161748Scperciva && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 0), 1)) 823161748Scperciva && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 0), 0), 824161748Scperciva lb, 0)) 825161748Scperciva ub = TREE_OPERAND (ub, 1); 826161748Scperciva else if (TREE_CODE (ub) == MAX_EXPR 827161748Scperciva && TREE_CODE (TREE_OPERAND (ub, 1)) == MINUS_EXPR 828161748Scperciva && integer_onep (TREE_OPERAND (TREE_OPERAND (ub, 1), 1)) 829161748Scperciva && operand_equal_p (TREE_OPERAND (TREE_OPERAND (ub, 1), 830161748Scperciva 0), 831161748Scperciva lb, 0)) 832161748Scperciva ub = TREE_OPERAND (ub, 0); 833161748Scperciva 834161748Scperciva /* The initial subtraction should happen in the original type so 835161748Scperciva that (possible) negative values are handled appropriately. */ 836161748Scperciva length = size_binop (PLUS_EXPR, size_one_node, 837161748Scperciva fold (build (MINUS_EXPR, TREE_TYPE (lb), 838173564Scperciva ub, lb))); 839173564Scperciva 840173564Scperciva /* If neither bound is a constant and sizetype is signed, make 841173564Scperciva sure the size is never negative. We should really do this 842173564Scperciva if *either* bound is non-constant, but this is the best 843173564Scperciva compromise between C and Ada. */ 844173564Scperciva if (! TREE_UNSIGNED (sizetype) 845173564Scperciva && TREE_CODE (TYPE_MIN_VALUE (index)) != INTEGER_CST 846173564Scperciva && TREE_CODE (TYPE_MAX_VALUE (index)) != INTEGER_CST) 847173564Scperciva length = size_binop (MAX_EXPR, length, size_zero_node); 848173564Scperciva 849173564Scperciva /* Special handling for arrays of bits (for Chill). */ 850173564Scperciva element_size = TYPE_SIZE (element); 851173564Scperciva if (TYPE_PACKED (type) && INTEGRAL_TYPE_P (element)) 852161748Scperciva { 853161748Scperciva HOST_WIDE_INT maxvalue, minvalue; 854161748Scperciva maxvalue = TREE_INT_CST_LOW (TYPE_MAX_VALUE (element)); 855161748Scperciva minvalue = TREE_INT_CST_LOW (TYPE_MIN_VALUE (element)); 856161748Scperciva if (maxvalue - minvalue == 1 857161748Scperciva && (maxvalue == 1 || maxvalue == 0)) 858161748Scperciva element_size = integer_one_node; 859161748Scperciva } 860161748Scperciva 861161748Scperciva TYPE_SIZE (type) = size_binop (MULT_EXPR, element_size, length); 862161748Scperciva 863161748Scperciva /* If we know the size of the element, calculate the total 864161748Scperciva size directly, rather than do some division thing below. 865161748Scperciva This optimization helps Fortran assumed-size arrays 866161748Scperciva (where the size of the array is determined at runtime) 867161748Scperciva substantially. 868161748Scperciva Note that we can't do this in the case where the size of 869161748Scperciva the elements is one bit since TYPE_SIZE_UNIT cannot be 870161748Scperciva set correctly in that case. */ 871161748Scperciva if (TYPE_SIZE_UNIT (element) != 0 872161748Scperciva && element_size != integer_one_node) 873161748Scperciva { 874161748Scperciva TYPE_SIZE_UNIT (type) 875161748Scperciva = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (element), length); 876161748Scperciva } 877161748Scperciva } 878161748Scperciva 879161748Scperciva /* Now round the alignment and size, 880161748Scperciva using machine-dependent criteria if any. */ 881161748Scperciva 882161748Scperciva#ifdef ROUND_TYPE_ALIGN 883161748Scperciva TYPE_ALIGN (type) 884173564Scperciva = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (element), BITS_PER_UNIT); 885173564Scperciva#else 886161748Scperciva TYPE_ALIGN (type) = MAX (TYPE_ALIGN (element), BITS_PER_UNIT); 887161748Scperciva#endif 888161748Scperciva 889161748Scperciva#ifdef ROUND_TYPE_SIZE 890161748Scperciva if (TYPE_SIZE (type) != 0) 891161748Scperciva { 892161748Scperciva tree tmp; 893161748Scperciva tmp = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type)); 894161748Scperciva /* If the rounding changed the size of the type, remove any 895161748Scperciva pre-calculated TYPE_SIZE_UNIT. */ 896161748Scperciva if (simple_cst_equal (TYPE_SIZE (type), tmp) != 1) 897161748Scperciva TYPE_SIZE_UNIT (type) = NULL; 898161748Scperciva TYPE_SIZE (type) = tmp; 899161748Scperciva } 900161748Scperciva#endif 901161748Scperciva 902161748Scperciva TYPE_MODE (type) = BLKmode; 903161748Scperciva if (TYPE_SIZE (type) != 0 904161748Scperciva && TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST 905161748Scperciva /* BLKmode elements force BLKmode aggregate; 906161748Scperciva else extract/store fields may lose. */ 907161748Scperciva && (TYPE_MODE (TREE_TYPE (type)) != BLKmode 908161748Scperciva || TYPE_NO_FORCE_BLK (TREE_TYPE (type)))) 909161748Scperciva { 910161748Scperciva TYPE_MODE (type) 911161748Scperciva = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)), 912161748Scperciva MODE_INT, 1); 913161748Scperciva 914161748Scperciva if (STRICT_ALIGNMENT && TYPE_ALIGN (type) < BIGGEST_ALIGNMENT 915161748Scperciva && (int)TYPE_ALIGN (type) < TREE_INT_CST_LOW (TYPE_SIZE (type)) 916161748Scperciva && TYPE_MODE (type) != BLKmode) 917161748Scperciva { 918161748Scperciva TYPE_NO_FORCE_BLK (type) = 1; 919161748Scperciva TYPE_MODE (type) = BLKmode; 920161748Scperciva } 921161748Scperciva } 922161748Scperciva break; 923161748Scperciva } 924161748Scperciva 925161748Scperciva case RECORD_TYPE: 926161748Scperciva pending_statics = layout_record (type); 927161748Scperciva TYPE_MODE (type) = BLKmode; 928161748Scperciva if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST) 929161748Scperciva { 930161748Scperciva tree field; 931161748Scperciva enum machine_mode mode = VOIDmode; 932161748Scperciva 933161748Scperciva /* A record which has any BLKmode members must itself be BLKmode; 934161748Scperciva it can't go in a register. 935161748Scperciva Unless the member is BLKmode only because it isn't aligned. */ 936161748Scperciva for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 937161748Scperciva { 938161748Scperciva int bitpos; 939161748Scperciva 940161748Scperciva if (TREE_CODE (field) != FIELD_DECL) 941161748Scperciva continue; 942161748Scperciva 943161748Scperciva if (TYPE_MODE (TREE_TYPE (field)) == BLKmode 944161748Scperciva && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field))) 945161748Scperciva goto record_lose; 946161748Scperciva 947161748Scperciva if (TREE_CODE (DECL_FIELD_BITPOS (field)) != INTEGER_CST) 948161748Scperciva goto record_lose; 949161748Scperciva 950161748Scperciva bitpos = TREE_INT_CST_LOW (DECL_FIELD_BITPOS (field)); 951161748Scperciva 952161748Scperciva /* Must be BLKmode if any field crosses a word boundary, 953161748Scperciva since extract_bit_field can't handle that in registers. */ 954161748Scperciva if (bitpos / BITS_PER_WORD 955161748Scperciva != ((TREE_INT_CST_LOW (DECL_SIZE (field)) + bitpos - 1) 956161748Scperciva / BITS_PER_WORD) 957161748Scperciva /* But there is no problem if the field is entire words. */ 958161748Scperciva && TREE_INT_CST_LOW (DECL_SIZE (field)) % BITS_PER_WORD != 0) 959161748Scperciva goto record_lose; 960161748Scperciva 961161748Scperciva /* If this field is the whole struct, remember its mode so 962161748Scperciva that, say, we can put a double in a class into a DF 963161748Scperciva register instead of forcing it to live in the stack. */ 964161748Scperciva if (simple_cst_equal (TYPE_SIZE (type), DECL_SIZE (field))) 965161748Scperciva mode = DECL_MODE (field); 966161748Scperciva } 967161748Scperciva 968161748Scperciva if (mode != VOIDmode) 969161748Scperciva /* We only have one real field; use its mode. */ 970161748Scperciva TYPE_MODE (type) = mode; 971161748Scperciva else 972161748Scperciva TYPE_MODE (type) 973161748Scperciva = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)), 974161748Scperciva MODE_INT, 1); 975161748Scperciva 976161748Scperciva /* If structure's known alignment is less than 977161748Scperciva what the scalar mode would need, and it matters, 978161748Scperciva then stick with BLKmode. */ 979161748Scperciva if (STRICT_ALIGNMENT 980161748Scperciva && ! (TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT 981161748Scperciva || ((int)TYPE_ALIGN (type) 982161748Scperciva >= TREE_INT_CST_LOW (TYPE_SIZE (type))))) 983161748Scperciva { 984161748Scperciva if (TYPE_MODE (type) != BLKmode) 985161748Scperciva /* If this is the only reason this type is BLKmode, 986161748Scperciva then don't force containing types to be BLKmode. */ 987161748Scperciva TYPE_NO_FORCE_BLK (type) = 1; 988161748Scperciva TYPE_MODE (type) = BLKmode; 989161748Scperciva } 990161748Scperciva 991161748Scperciva record_lose: ; 992161748Scperciva } 993161748Scperciva 994161748Scperciva /* Lay out any static members. This is done now 995161748Scperciva because their type may use the record's type. */ 996161748Scperciva while (pending_statics) 997161748Scperciva { 998161748Scperciva layout_decl (TREE_VALUE (pending_statics), 0); 999161748Scperciva pending_statics = TREE_CHAIN (pending_statics); 1000161748Scperciva } 1001161748Scperciva break; 1002161748Scperciva 1003161748Scperciva case UNION_TYPE: 1004161748Scperciva case QUAL_UNION_TYPE: 1005161748Scperciva layout_union (type); 1006161748Scperciva TYPE_MODE (type) = BLKmode; 1007161748Scperciva if (TREE_CODE (TYPE_SIZE (type)) == INTEGER_CST 1008161748Scperciva /* If structure's known alignment is less than 1009161748Scperciva what the scalar mode would need, and it matters, 1010161748Scperciva then stick with BLKmode. */ 1011161748Scperciva && (! STRICT_ALIGNMENT 1012161748Scperciva || TYPE_ALIGN (type) >= BIGGEST_ALIGNMENT 1013161748Scperciva || (int)TYPE_ALIGN (type) >= TREE_INT_CST_LOW (TYPE_SIZE (type)))) 1014161748Scperciva { 1015161748Scperciva tree field; 1016161748Scperciva /* A union which has any BLKmode members must itself be BLKmode; 1017161748Scperciva it can't go in a register. 1018161748Scperciva Unless the member is BLKmode only because it isn't aligned. */ 1019161748Scperciva for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field)) 1020161748Scperciva { 1021161748Scperciva if (TREE_CODE (field) != FIELD_DECL) 1022161748Scperciva continue; 1023161748Scperciva 1024161748Scperciva if (TYPE_MODE (TREE_TYPE (field)) == BLKmode 1025161748Scperciva && ! TYPE_NO_FORCE_BLK (TREE_TYPE (field))) 1026161748Scperciva goto union_lose; 1027161748Scperciva } 1028161748Scperciva 1029161748Scperciva TYPE_MODE (type) 1030161748Scperciva = mode_for_size (TREE_INT_CST_LOW (TYPE_SIZE (type)), 1031161748Scperciva MODE_INT, 1); 1032161748Scperciva 1033161748Scperciva union_lose: ; 1034161748Scperciva } 1035161748Scperciva break; 1036161748Scperciva 1037161748Scperciva case SET_TYPE: /* Used by Chill and Pascal. */ 1038161748Scperciva if (TREE_CODE (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST 1039161748Scperciva || TREE_CODE (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) != INTEGER_CST) 1040161748Scperciva abort(); 1041161748Scperciva else 1042161748Scperciva { 1043161748Scperciva#ifndef SET_WORD_SIZE 1044161748Scperciva#define SET_WORD_SIZE BITS_PER_WORD 1045161748Scperciva#endif 1046161748Scperciva int alignment = set_alignment ? set_alignment : SET_WORD_SIZE; 1047161748Scperciva int size_in_bits 1048161748Scperciva = (TREE_INT_CST_LOW (TYPE_MAX_VALUE (TYPE_DOMAIN (type))) 1049161748Scperciva - TREE_INT_CST_LOW (TYPE_MIN_VALUE (TYPE_DOMAIN (type))) + 1); 1050161748Scperciva int rounded_size 1051161748Scperciva = ((size_in_bits + alignment - 1) / alignment) * alignment; 1052161748Scperciva if (rounded_size > alignment) 1053161748Scperciva TYPE_MODE (type) = BLKmode; 1054161748Scperciva else 1055161748Scperciva TYPE_MODE (type) = mode_for_size (alignment, MODE_INT, 1); 1056161748Scperciva TYPE_SIZE (type) = bitsize_int (rounded_size, 0L); 1057161748Scperciva TYPE_SIZE_UNIT (type) = size_int (rounded_size / BITS_PER_UNIT); 1058161748Scperciva TYPE_ALIGN (type) = alignment; 1059161748Scperciva TYPE_PRECISION (type) = size_in_bits; 1060161748Scperciva } 1061161748Scperciva break; 1062161748Scperciva 1063161748Scperciva case FILE_TYPE: 1064161748Scperciva /* The size may vary in different languages, so the language front end 1065161748Scperciva should fill in the size. */ 1066161748Scperciva TYPE_ALIGN (type) = BIGGEST_ALIGNMENT; 1067161748Scperciva TYPE_MODE (type) = BLKmode; 1068161748Scperciva break; 1069161748Scperciva 1070161748Scperciva default: 1071161748Scperciva abort (); 1072161748Scperciva } /* end switch */ 1073161748Scperciva 1074161748Scperciva /* Normally, use the alignment corresponding to the mode chosen. 1075161748Scperciva However, where strict alignment is not required, avoid 1076161748Scperciva over-aligning structures, since most compilers do not do this 1077161748Scperciva alignment. */ 1078161748Scperciva 1079161748Scperciva if (TYPE_MODE (type) != BLKmode && TYPE_MODE (type) != VOIDmode 1080161748Scperciva && (STRICT_ALIGNMENT 1081161748Scperciva || (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE 1082161748Scperciva && TREE_CODE (type) != QUAL_UNION_TYPE 1083161748Scperciva && TREE_CODE (type) != ARRAY_TYPE))) 1084161748Scperciva TYPE_ALIGN (type) = GET_MODE_ALIGNMENT (TYPE_MODE (type)); 1085161748Scperciva 1086161748Scperciva /* Do machine-dependent extra alignment. */ 1087161748Scperciva#ifdef ROUND_TYPE_ALIGN 1088161748Scperciva TYPE_ALIGN (type) 1089161748Scperciva = ROUND_TYPE_ALIGN (type, TYPE_ALIGN (type), BITS_PER_UNIT); 1090161748Scperciva#endif 1091161748Scperciva 1092161748Scperciva#ifdef ROUND_TYPE_SIZE 1093161748Scperciva if (TYPE_SIZE (type) != 0) 1094161748Scperciva TYPE_SIZE (type) 1095161748Scperciva = ROUND_TYPE_SIZE (type, TYPE_SIZE (type), TYPE_ALIGN (type)); 1096161748Scperciva#endif 1097161748Scperciva 1098161748Scperciva /* Evaluate nonconstant size only once, either now or as soon as safe. */ 1099161748Scperciva if (TYPE_SIZE (type) != 0 && TREE_CODE (TYPE_SIZE (type)) != INTEGER_CST) 1100161748Scperciva TYPE_SIZE (type) = variable_size (TYPE_SIZE (type)); 1101161748Scperciva 1102161748Scperciva /* If we failed to find a simple way to calculate the unit size 1103161748Scperciva of the type above, find it by division. */ 1104161748Scperciva if (TYPE_SIZE_UNIT (type) == 0 && TYPE_SIZE (type) != 0) 1105161748Scperciva { 1106161748Scperciva TYPE_SIZE_UNIT (type) = size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (type), 1107161748Scperciva size_int (BITS_PER_UNIT)); 1108161748Scperciva } 1109161748Scperciva 1110161748Scperciva /* Once again evaluate only once, either now or as soon as safe. */ 1111161748Scperciva if (TYPE_SIZE_UNIT (type) != 0 1112161748Scperciva && TREE_CODE (TYPE_SIZE_UNIT (type)) != INTEGER_CST) 1113161748Scperciva TYPE_SIZE_UNIT (type) = variable_size (TYPE_SIZE_UNIT (type)); 1114161748Scperciva 1115161748Scperciva /* Also layout any other variants of the type. */ 1116161748Scperciva if (TYPE_NEXT_VARIANT (type) 1117161748Scperciva || type != TYPE_MAIN_VARIANT (type)) 1118161748Scperciva { 1119161748Scperciva tree variant; 1120161748Scperciva /* Record layout info of this variant. */ 1121161748Scperciva tree size = TYPE_SIZE (type); 1122161748Scperciva tree size_unit = TYPE_SIZE_UNIT (type); 1123161748Scperciva int align = TYPE_ALIGN (type); 1124161748Scperciva enum machine_mode mode = TYPE_MODE (type); 1125161748Scperciva 1126164600Scperciva /* Copy it into all variants. */ 1127164600Scperciva for (variant = TYPE_MAIN_VARIANT (type); 1128161748Scperciva variant; 1129161748Scperciva variant = TYPE_NEXT_VARIANT (variant)) 1130161748Scperciva { 1131161748Scperciva TYPE_SIZE (variant) = size; 1132161748Scperciva TYPE_SIZE_UNIT (variant) = size_unit; 1133161748Scperciva TYPE_ALIGN (variant) = align; 1134161748Scperciva TYPE_MODE (variant) = mode; 1135161748Scperciva } 1136161748Scperciva } 1137161748Scperciva 1138161748Scperciva pop_obstacks (); 1139161748Scperciva resume_momentary (old); 1140161748Scperciva} 1141161748Scperciva 1142161748Scperciva/* Create and return a type for signed integers of PRECISION bits. */ 1143161748Scperciva 1144161748Scpercivatree 1145161748Scpercivamake_signed_type (precision) 1146161748Scperciva int precision; 1147161748Scperciva{ 1148161748Scperciva register tree type = make_node (INTEGER_TYPE); 1149161748Scperciva 1150161748Scperciva TYPE_PRECISION (type) = precision; 1151161748Scperciva 1152161748Scperciva /* Create the extreme values based on the number of bits. */ 1153161748Scperciva 1154161748Scperciva TYPE_MIN_VALUE (type) 1155161748Scperciva = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0 1156161748Scperciva ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)), 1157161748Scperciva (((HOST_WIDE_INT) (-1) 1158161748Scperciva << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0 1159161748Scperciva ? precision - HOST_BITS_PER_WIDE_INT - 1 1160161748Scperciva : 0)))); 1161161748Scperciva TYPE_MAX_VALUE (type) 1162161748Scperciva = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0 1163161748Scperciva ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1), 1164161748Scperciva (precision - HOST_BITS_PER_WIDE_INT - 1 > 0 1165161748Scperciva ? (((HOST_WIDE_INT) 1 1166161748Scperciva << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1 1167161748Scperciva : 0)); 1168161748Scperciva 1169161748Scperciva /* Give this type's extreme values this type as their type. */ 1170161748Scperciva 1171161748Scperciva TREE_TYPE (TYPE_MIN_VALUE (type)) = type; 1172161748Scperciva TREE_TYPE (TYPE_MAX_VALUE (type)) = type; 1173161748Scperciva 1174161748Scperciva /* The first type made with this or `make_unsigned_type' 1175161748Scperciva is the type for size values. */ 1176173564Scperciva 1177173564Scperciva if (sizetype == 0) 1178173564Scperciva set_sizetype (type); 1179173564Scperciva 1180173564Scperciva /* Lay out the type: set its alignment, size, etc. */ 1181173564Scperciva 1182173564Scperciva layout_type (type); 1183173564Scperciva 1184173564Scperciva return type; 1185173564Scperciva} 1186173564Scperciva 1187173564Scperciva/* Create and return a type for unsigned integers of PRECISION bits. */ 1188173564Scperciva 1189173564Scpercivatree 1190173564Scpercivamake_unsigned_type (precision) 1191161869Scperciva int precision; 1192161748Scperciva{ 1193161748Scperciva register tree type = make_node (INTEGER_TYPE); 1194161748Scperciva 1195161748Scperciva TYPE_PRECISION (type) = precision; 1196173564Scperciva 1197173564Scperciva /* The first type made with this or `make_signed_type' 1198161748Scperciva is the type for size values. */ 1199161748Scperciva 1200161748Scperciva if (sizetype == 0) 1201161748Scperciva { 1202161748Scperciva TREE_UNSIGNED (type) = 1; 1203173564Scperciva set_sizetype (type); 1204161748Scperciva } 1205161748Scperciva 1206161748Scperciva fixup_unsigned_type (type); 1207161748Scperciva return type; 1208161748Scperciva} 1209161748Scperciva 1210161748Scperciva/* Set sizetype to TYPE, and initialize *sizetype accordingly. 1211161748Scperciva Also update the type of any standard type's sizes made so far. */ 1212161748Scperciva 1213161748Scpercivavoid 1214161748Scpercivaset_sizetype (type) 1215173564Scperciva tree type; 1216161748Scperciva{ 1217161748Scperciva int oprecision = TYPE_PRECISION (type), precision; 1218173564Scperciva 1219164600Scperciva sizetype = type; 1220173564Scperciva 1221164600Scperciva /* The *bitsizetype types use a precision that avoids overflows when 1222164600Scperciva calculating signed sizes / offsets in bits. 1223164600Scperciva 1224161748Scperciva We are allocating bitsizetype once and change it in place when 1225173564Scperciva we decide later that we want to change it. This way, we avoid the 1226173564Scperciva hassle of changing all the TYPE_SIZE (TREE_TYPE (sometype)) 1227161748Scperciva individually in each front end. */ 1228161748Scperciva if (! bitsizetype) 1229164600Scperciva bitsizetype = make_node (INTEGER_TYPE); 1230173564Scperciva if (TYPE_NAME (sizetype) && ! TYPE_NAME (bitsizetype)) 1231173564Scperciva TYPE_NAME (bitsizetype) = TYPE_NAME (sizetype); 1232164600Scperciva 1233164600Scperciva precision = oprecision + BITS_PER_UNIT_LOG + 1; 1234161748Scperciva /* However, when cross-compiling from a 32 bit to a 64 bit host, 1235161748Scperciva we are limited to 64 bit precision. */ 1236161748Scperciva if (precision > 2 * HOST_BITS_PER_WIDE_INT) 1237161748Scperciva precision = 2 * HOST_BITS_PER_WIDE_INT; 1238161748Scperciva TYPE_PRECISION (bitsizetype) = precision; 1239161748Scperciva if (TREE_UNSIGNED (type)) 1240161748Scperciva fixup_unsigned_type (bitsizetype); 1241161748Scperciva else 1242161748Scperciva fixup_signed_type (bitsizetype); 1243161748Scperciva layout_type (bitsizetype); 1244161748Scperciva 1245161748Scperciva if (TREE_UNSIGNED (type)) 1246161748Scperciva { 1247161748Scperciva usizetype = sizetype; 1248161748Scperciva ubitsizetype = bitsizetype; 1249161748Scperciva ssizetype = make_signed_type (oprecision); 1250161748Scperciva sbitsizetype = make_signed_type (precision); 1251161748Scperciva } 1252161748Scperciva else 1253161748Scperciva { 1254161748Scperciva ssizetype = sizetype; 1255161748Scperciva sbitsizetype = bitsizetype; 1256161748Scperciva usizetype = make_unsigned_type (oprecision); 1257161748Scperciva ubitsizetype = make_unsigned_type (precision); 1258161748Scperciva } 1259161748Scperciva} 1260161748Scperciva 1261161748Scperciva/* Set the extreme values of TYPE based on its precision in bits, 1262161748Scperciva then lay it out. Used when make_signed_type won't do 1263161748Scperciva because the tree code is not INTEGER_TYPE. 1264161748Scperciva E.g. for Pascal, when the -fsigned-char option is given. */ 1265161748Scperciva 1266161748Scpercivavoid 1267161748Scpercivafixup_signed_type (type) 1268161748Scperciva tree type; 1269161748Scperciva{ 1270161748Scperciva register int precision = TYPE_PRECISION (type); 1271161748Scperciva 1272161748Scperciva TYPE_MIN_VALUE (type) 1273161748Scperciva = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0 1274161748Scperciva ? 0 : (HOST_WIDE_INT) (-1) << (precision - 1)), 1275161748Scperciva (((HOST_WIDE_INT) (-1) 1276161748Scperciva << (precision - HOST_BITS_PER_WIDE_INT - 1 > 0 1277161748Scperciva ? precision - HOST_BITS_PER_WIDE_INT - 1 1278161748Scperciva : 0)))); 1279161748Scperciva TYPE_MAX_VALUE (type) 1280161748Scperciva = build_int_2 ((precision - HOST_BITS_PER_WIDE_INT > 0 1281161748Scperciva ? -1 : ((HOST_WIDE_INT) 1 << (precision - 1)) - 1), 1282161748Scperciva (precision - HOST_BITS_PER_WIDE_INT - 1 > 0 1283161748Scperciva ? (((HOST_WIDE_INT) 1 1284161748Scperciva << (precision - HOST_BITS_PER_WIDE_INT - 1))) - 1 1285161748Scperciva : 0)); 1286161748Scperciva 1287161748Scperciva TREE_TYPE (TYPE_MIN_VALUE (type)) = type; 1288161748Scperciva TREE_TYPE (TYPE_MAX_VALUE (type)) = type; 1289161748Scperciva 1290161748Scperciva /* Lay out the type: set its alignment, size, etc. */ 1291161748Scperciva 1292161748Scperciva layout_type (type); 1293161748Scperciva} 1294161748Scperciva 1295161748Scperciva/* Set the extreme values of TYPE based on its precision in bits, 1296161748Scperciva then lay it out. This is used both in `make_unsigned_type' 1297161748Scperciva and for enumeral types. */ 1298161748Scperciva 1299161748Scpercivavoid 1300161748Scpercivafixup_unsigned_type (type) 1301161748Scperciva tree type; 1302161748Scperciva{ 1303161748Scperciva register int precision = TYPE_PRECISION (type); 1304161748Scperciva 1305161748Scperciva TYPE_MIN_VALUE (type) = build_int_2 (0, 0); 1306161748Scperciva TYPE_MAX_VALUE (type) 1307161748Scperciva = build_int_2 (precision - HOST_BITS_PER_WIDE_INT >= 0 1308161748Scperciva ? -1 : ((HOST_WIDE_INT) 1 << precision) - 1, 1309161748Scperciva precision - HOST_BITS_PER_WIDE_INT > 0 1310161748Scperciva ? ((unsigned HOST_WIDE_INT) ~0 1311161748Scperciva >> (HOST_BITS_PER_WIDE_INT 1312161748Scperciva - (precision - HOST_BITS_PER_WIDE_INT))) 1313161748Scperciva : 0); 1314173564Scperciva TREE_TYPE (TYPE_MIN_VALUE (type)) = type; 1315173564Scperciva TREE_TYPE (TYPE_MAX_VALUE (type)) = type; 1316173564Scperciva 1317173564Scperciva /* Lay out the type: set its alignment, size, etc. */ 1318173564Scperciva 1319173564Scperciva layout_type (type); 1320173564Scperciva} 1321173564Scperciva 1322173564Scperciva/* Find the best machine mode to use when referencing a bit field of length 1323173564Scperciva BITSIZE bits starting at BITPOS. 1324173564Scperciva 1325173564Scperciva The underlying object is known to be aligned to a boundary of ALIGN bits. 1326173564Scperciva If LARGEST_MODE is not VOIDmode, it means that we should not use a mode 1327173564Scperciva larger than LARGEST_MODE (usually SImode). 1328173564Scperciva 1329173564Scperciva If no mode meets all these conditions, we return VOIDmode. Otherwise, if 1330173564Scperciva VOLATILEP is true or SLOW_BYTE_ACCESS is false, we return the smallest 1331173564Scperciva mode meeting these conditions. 1332173564Scperciva 1333173564Scperciva Otherwise (VOLATILEP is false and SLOW_BYTE_ACCESS is true), we return 1334173564Scperciva the largest mode (but a mode no wider than UNITS_PER_WORD) that meets 1335173564Scperciva all the conditions. */ 1336173564Scperciva 1337173564Scpercivaenum machine_mode 1338173564Scpercivaget_best_mode (bitsize, bitpos, align, largest_mode, volatilep) 1339173564Scperciva int bitsize, bitpos; 1340173564Scperciva int align; 1341173564Scperciva enum machine_mode largest_mode; 1342173564Scperciva int volatilep; 1343173564Scperciva{ 1344173564Scperciva enum machine_mode mode; 1345173564Scperciva int unit = 0; 1346161748Scperciva 1347173564Scperciva /* Find the narrowest integer mode that contains the bit field. */ 1348173564Scperciva for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode; 1349173564Scperciva mode = GET_MODE_WIDER_MODE (mode)) 1350173564Scperciva { 1351161748Scperciva unit = GET_MODE_BITSIZE (mode); 1352161748Scperciva if ((bitpos % unit) + bitsize <= unit) 1353161748Scperciva break; 1354161748Scperciva } 1355161748Scperciva 1356161748Scperciva if (mode == MAX_MACHINE_MODE 1357161748Scperciva /* It is tempting to omit the following line 1358161748Scperciva if STRICT_ALIGNMENT is true. 1359161748Scperciva But that is incorrect, since if the bitfield uses part of 3 bytes 1360161748Scperciva and we use a 4-byte mode, we could get a spurious segv 1361161748Scperciva if the extra 4th byte is past the end of memory. 1362161748Scperciva (Though at least one Unix compiler ignores this problem: 1363161748Scperciva that on the Sequent 386 machine. */ 1364161748Scperciva || MIN (unit, BIGGEST_ALIGNMENT) > align 1365161748Scperciva || (largest_mode != VOIDmode && unit > GET_MODE_BITSIZE (largest_mode))) 1366161748Scperciva return VOIDmode; 1367161748Scperciva 1368161748Scperciva if (SLOW_BYTE_ACCESS && ! volatilep) 1369161748Scperciva { 1370161748Scperciva enum machine_mode wide_mode = VOIDmode, tmode; 1371161748Scperciva 1372173564Scperciva for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT); tmode != VOIDmode; 1373173564Scperciva tmode = GET_MODE_WIDER_MODE (tmode)) 1374173564Scperciva { 1375173564Scperciva unit = GET_MODE_BITSIZE (tmode); 1376173564Scperciva if (bitpos / unit == (bitpos + bitsize - 1) / unit 1377173564Scperciva && unit <= BITS_PER_WORD 1378173564Scperciva && unit <= MIN (align, BIGGEST_ALIGNMENT) 1379173564Scperciva && (largest_mode == VOIDmode 1380173564Scperciva || unit <= GET_MODE_BITSIZE (largest_mode))) 1381161748Scperciva wide_mode = tmode; 1382161748Scperciva } 1383161748Scperciva 1384161748Scperciva if (wide_mode != VOIDmode) 1385161748Scperciva return wide_mode; 1386161748Scperciva } 1387161748Scperciva 1388161748Scperciva return mode; 1389161748Scperciva} 1390161748Scperciva 1391161748Scperciva/* Save all variables describing the current status into the structure *P. 1392161748Scperciva This is used before starting a nested function. */ 1393161748Scperciva 1394161748Scpercivavoid 1395161748Scpercivasave_storage_status (p) 1396161748Scperciva struct function *p ATTRIBUTE_UNUSED; 1397161748Scperciva{ 1398161748Scperciva#if 0 /* Need not save, since always 0 and non0 (resp.) within a function. */ 1399161748Scperciva p->pending_sizes = pending_sizes; 1400161748Scperciva p->immediate_size_expand = immediate_size_expand; 1401161748Scperciva#endif /* 0 */ 1402161748Scperciva} 1403161748Scperciva 1404161748Scperciva/* Restore all variables describing the current status from the structure *P. 1405161748Scperciva This is used after a nested function. */ 1406161748Scperciva 1407161748Scpercivavoid 1408161748Scpercivarestore_storage_status (p) 1409161748Scperciva struct function *p ATTRIBUTE_UNUSED; 1410161748Scperciva{ 1411161748Scperciva#if 0 1412161748Scperciva pending_sizes = p->pending_sizes; 1413161748Scperciva immediate_size_expand = p->immediate_size_expand; 1414161748Scperciva#endif /* 0 */ 1415161748Scperciva} 1416161748Scperciva