Daily bump.
[official-gcc.git] / gcc / varasm.c
blobff1ee6e05776dc82794536709b01e76f89825db1
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 /* This file handles generation of all the assembler code
25 *except* the instructions of a function.
26 This includes declarations of variables and their initial values.
28 We also output the assembler code for constants stored in memory
29 and are responsible for combining constants with the same value. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "rtl.h"
36 #include "tree.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "hard-reg-set.h"
41 #include "regs.h"
42 #include "real.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "hashtab.h"
46 #include "c-pragma.h"
47 #include "ggc.h"
48 #include "langhooks.h"
49 #include "tm_p.h"
50 #include "debug.h"
51 #include "target.h"
52 #include "tree-mudflap.h"
53 #include "cgraph.h"
54 #include "cfglayout.h"
55 #include "basic-block.h"
57 #ifdef XCOFF_DEBUGGING_INFO
58 #include "xcoffout.h" /* Needed for external data
59 declarations for e.g. AIX 4.x. */
60 #endif
62 /* The (assembler) name of the first globally-visible object output. */
63 extern GTY(()) const char *first_global_object_name;
64 extern GTY(()) const char *weak_global_object_name;
66 const char *first_global_object_name;
67 const char *weak_global_object_name;
69 struct addr_const;
70 struct constant_descriptor_rtx;
71 struct rtx_constant_pool;
73 struct varasm_status GTY(())
75 /* If we're using a per-function constant pool, this is it. */
76 struct rtx_constant_pool *pool;
78 /* Number of tree-constants deferred during the expansion of this
79 function. */
80 unsigned int deferred_constants;
83 #define n_deferred_constants (cfun->varasm->deferred_constants)
85 /* Number for making the label on the next
86 constant that is stored in memory. */
88 static GTY(()) int const_labelno;
90 /* Carry information from ASM_DECLARE_OBJECT_NAME
91 to ASM_FINISH_DECLARE_OBJECT. */
93 int size_directive_output;
95 /* The last decl for which assemble_variable was called,
96 if it did ASM_DECLARE_OBJECT_NAME.
97 If the last call to assemble_variable didn't do that,
98 this holds 0. */
100 tree last_assemble_variable_decl;
102 /* The following global variable indicates if the first basic block
103 in a function belongs to the cold partition or not. */
105 bool first_function_block_is_cold;
107 /* We give all constants their own alias set. Perhaps redundant with
108 MEM_READONLY_P, but pre-dates it. */
110 static HOST_WIDE_INT const_alias_set;
112 static const char *strip_reg_name (const char *);
113 static int contains_pointers_p (tree);
114 #ifdef ASM_OUTPUT_EXTERNAL
115 static bool incorporeal_function_p (tree);
116 #endif
117 static void decode_addr_const (tree, struct addr_const *);
118 static hashval_t const_desc_hash (const void *);
119 static int const_desc_eq (const void *, const void *);
120 static hashval_t const_hash_1 (const tree);
121 static int compare_constant (const tree, const tree);
122 static tree copy_constant (tree);
123 static void output_constant_def_contents (rtx);
124 static void output_addressed_constants (tree);
125 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
126 static unsigned min_align (unsigned, unsigned);
127 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
128 static void globalize_decl (tree);
129 #ifdef BSS_SECTION_ASM_OP
130 #ifdef ASM_OUTPUT_BSS
131 static void asm_output_bss (FILE *, tree, const char *,
132 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
133 #endif
134 #ifdef ASM_OUTPUT_ALIGNED_BSS
135 static void asm_output_aligned_bss (FILE *, tree, const char *,
136 unsigned HOST_WIDE_INT, int)
137 ATTRIBUTE_UNUSED;
138 #endif
139 #endif /* BSS_SECTION_ASM_OP */
140 static void mark_weak (tree);
141 static void output_constant_pool (const char *, tree);
143 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
144 section *text_section;
145 section *data_section;
146 section *readonly_data_section;
147 section *sdata_section;
148 section *ctors_section;
149 section *dtors_section;
150 section *bss_section;
151 section *sbss_section;
153 /* Various forms of common section. All are guaranteed to be nonnull. */
154 section *tls_comm_section;
155 section *comm_section;
156 section *lcomm_section;
158 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
159 May be null. */
160 section *bss_noswitch_section;
162 /* The section that holds the main exception table, when known. The section
163 is set either by the target's init_sections hook or by the first call to
164 switch_to_exception_section. */
165 section *exception_section;
167 /* The section that holds the DWARF2 frame unwind information, when known.
168 The section is set either by the target's init_sections hook or by the
169 first call to switch_to_eh_frame_section. */
170 section *eh_frame_section;
172 /* asm_out_file's current section. This is NULL if no section has yet
173 been selected or if we lose track of what the current section is. */
174 section *in_section;
176 /* True if code for the current function is currently being directed
177 at the cold section. */
178 bool in_cold_section_p;
180 /* A linked list of all the unnamed sections. */
181 static GTY(()) section *unnamed_sections;
183 /* Return a nonzero value if DECL has a section attribute. */
184 #ifndef IN_NAMED_SECTION
185 #define IN_NAMED_SECTION(DECL) \
186 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
187 && DECL_SECTION_NAME (DECL) != NULL_TREE)
188 #endif
190 /* Hash table of named sections. */
191 static GTY((param_is (section))) htab_t section_htab;
193 /* A table of object_blocks, indexed by section. */
194 static GTY((param_is (struct object_block))) htab_t object_block_htab;
196 /* The next number to use for internal anchor labels. */
197 static GTY(()) int anchor_labelno;
199 /* A pool of constants that can be shared between functions. */
200 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
202 /* Helper routines for maintaining section_htab. */
204 static int
205 section_entry_eq (const void *p1, const void *p2)
207 const section *old = p1;
208 const char *new = p2;
210 return strcmp (old->named.name, new) == 0;
213 static hashval_t
214 section_entry_hash (const void *p)
216 const section *old = p;
217 return htab_hash_string (old->named.name);
220 /* Return a hash value for section SECT. */
222 static hashval_t
223 hash_section (section *sect)
225 if (sect->common.flags & SECTION_NAMED)
226 return htab_hash_string (sect->named.name);
227 return sect->common.flags;
230 /* Helper routines for maintaining object_block_htab. */
232 static int
233 object_block_entry_eq (const void *p1, const void *p2)
235 const struct object_block *old = p1;
236 const section *new = p2;
238 return old->sect == new;
241 static hashval_t
242 object_block_entry_hash (const void *p)
244 const struct object_block *old = p;
245 return hash_section (old->sect);
248 /* Return a new unnamed section with the given fields. */
250 section *
251 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
252 const void *data)
254 section *sect;
256 sect = ggc_alloc (sizeof (struct unnamed_section));
257 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
258 sect->unnamed.callback = callback;
259 sect->unnamed.data = data;
260 sect->unnamed.next = unnamed_sections;
262 unnamed_sections = sect;
263 return sect;
266 /* Return a SECTION_NOSWITCH section with the given fields. */
268 static section *
269 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
271 section *sect;
273 sect = ggc_alloc (sizeof (struct unnamed_section));
274 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
275 sect->noswitch.callback = callback;
277 return sect;
280 /* Return the named section structure associated with NAME. Create
281 a new section with the given fields if no such structure exists. */
283 section *
284 get_section (const char *name, unsigned int flags, tree decl)
286 section *sect, **slot;
288 slot = (section **)
289 htab_find_slot_with_hash (section_htab, name,
290 htab_hash_string (name), INSERT);
291 flags |= SECTION_NAMED;
292 if (*slot == NULL)
294 sect = ggc_alloc (sizeof (struct named_section));
295 sect->named.common.flags = flags;
296 sect->named.name = ggc_strdup (name);
297 sect->named.decl = decl;
298 *slot = sect;
300 else
302 sect = *slot;
303 if ((sect->common.flags & ~SECTION_DECLARED) != flags
304 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
306 /* Sanity check user variables for flag changes. */
307 if (decl == 0)
308 decl = sect->named.decl;
309 gcc_assert (decl);
310 error ("%+D causes a section type conflict", decl);
313 return sect;
316 /* Return true if the current compilation mode benefits from having
317 objects grouped into blocks. */
319 static bool
320 use_object_blocks_p (void)
322 return flag_section_anchors;
325 /* Return the object_block structure for section SECT. Create a new
326 structure if we haven't created one already. Return null if SECT
327 itself is null. */
329 static struct object_block *
330 get_block_for_section (section *sect)
332 struct object_block *block;
333 void **slot;
335 if (sect == NULL)
336 return NULL;
338 slot = htab_find_slot_with_hash (object_block_htab, sect,
339 hash_section (sect), INSERT);
340 block = (struct object_block *) *slot;
341 if (block == NULL)
343 block = (struct object_block *)
344 ggc_alloc_cleared (sizeof (struct object_block));
345 block->sect = sect;
346 *slot = block;
348 return block;
351 /* Create a symbol with label LABEL and place it at byte offset
352 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
353 is not yet known. LABEL must be a garbage-collected string. */
355 static rtx
356 create_block_symbol (const char *label, struct object_block *block,
357 HOST_WIDE_INT offset)
359 rtx symbol;
360 unsigned int size;
362 /* Create the extended SYMBOL_REF. */
363 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
364 symbol = ggc_alloc_zone (size, &rtl_zone);
366 /* Initialize the normal SYMBOL_REF fields. */
367 memset (symbol, 0, size);
368 PUT_CODE (symbol, SYMBOL_REF);
369 PUT_MODE (symbol, Pmode);
370 XSTR (symbol, 0) = label;
371 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
373 /* Initialize the block_symbol stuff. */
374 SYMBOL_REF_BLOCK (symbol) = block;
375 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
377 return symbol;
380 static void
381 initialize_cold_section_name (void)
383 const char *stripped_name;
384 char *name, *buffer;
385 tree dsn;
387 gcc_assert (cfun && current_function_decl);
388 if (cfun->unlikely_text_section_name)
389 return;
391 dsn = DECL_SECTION_NAME (current_function_decl);
392 if (flag_function_sections && dsn)
394 name = alloca (TREE_STRING_LENGTH (dsn) + 1);
395 memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
397 stripped_name = targetm.strip_name_encoding (name);
399 buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
400 cfun->unlikely_text_section_name = ggc_strdup (buffer);
402 else
403 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
406 /* Tell assembler to switch to unlikely-to-be-executed text section. */
408 section *
409 unlikely_text_section (void)
411 if (cfun)
413 if (!cfun->unlikely_text_section_name)
414 initialize_cold_section_name ();
416 return get_named_section (NULL, cfun->unlikely_text_section_name, 0);
418 else
419 return get_named_section (NULL, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
422 /* When called within a function context, return true if the function
423 has been assigned a cold text section and if SECT is that section.
424 When called outside a function context, return true if SECT is the
425 default cold section. */
427 bool
428 unlikely_text_section_p (section *sect)
430 const char *name;
432 if (cfun)
433 name = cfun->unlikely_text_section_name;
434 else
435 name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
437 return (name
438 && sect
439 && SECTION_STYLE (sect) == SECTION_NAMED
440 && strcmp (name, sect->named.name) == 0);
443 /* Return a section with a particular name and with whatever SECTION_*
444 flags section_type_flags deems appropriate. The name of the section
445 is taken from NAME if nonnull, otherwise it is taken from DECL's
446 DECL_SECTION_NAME. DECL is the decl associated with the section
447 (see the section comment for details) and RELOC is as for
448 section_type_flags. */
450 section *
451 get_named_section (tree decl, const char *name, int reloc)
453 unsigned int flags;
455 gcc_assert (!decl || DECL_P (decl));
456 if (name == NULL)
457 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
459 flags = targetm.section_type_flags (decl, name, reloc);
461 return get_section (name, flags, decl);
464 /* If required, set DECL_SECTION_NAME to a unique name. */
466 void
467 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
468 int flag_function_or_data_sections)
470 if (DECL_SECTION_NAME (decl) == NULL_TREE
471 && targetm.have_named_sections
472 && (flag_function_or_data_sections
473 || DECL_ONE_ONLY (decl)))
474 targetm.asm_out.unique_section (decl, reloc);
477 #ifdef BSS_SECTION_ASM_OP
479 #ifdef ASM_OUTPUT_BSS
481 /* Utility function for ASM_OUTPUT_BSS for targets to use if
482 they don't support alignments in .bss.
483 ??? It is believed that this function will work in most cases so such
484 support is localized here. */
486 static void
487 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
488 const char *name,
489 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
490 unsigned HOST_WIDE_INT rounded)
492 targetm.asm_out.globalize_label (file, name);
493 switch_to_section (bss_section);
494 #ifdef ASM_DECLARE_OBJECT_NAME
495 last_assemble_variable_decl = decl;
496 ASM_DECLARE_OBJECT_NAME (file, name, decl);
497 #else
498 /* Standard thing is just output label for the object. */
499 ASM_OUTPUT_LABEL (file, name);
500 #endif /* ASM_DECLARE_OBJECT_NAME */
501 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
504 #endif
506 #ifdef ASM_OUTPUT_ALIGNED_BSS
508 /* Utility function for targets to use in implementing
509 ASM_OUTPUT_ALIGNED_BSS.
510 ??? It is believed that this function will work in most cases so such
511 support is localized here. */
513 static void
514 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
515 const char *name, unsigned HOST_WIDE_INT size,
516 int align)
518 switch_to_section (bss_section);
519 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
520 #ifdef ASM_DECLARE_OBJECT_NAME
521 last_assemble_variable_decl = decl;
522 ASM_DECLARE_OBJECT_NAME (file, name, decl);
523 #else
524 /* Standard thing is just output label for the object. */
525 ASM_OUTPUT_LABEL (file, name);
526 #endif /* ASM_DECLARE_OBJECT_NAME */
527 ASM_OUTPUT_SKIP (file, size ? size : 1);
530 #endif
532 #endif /* BSS_SECTION_ASM_OP */
534 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
535 /* Return the hot section for function DECL. Return text_section for
536 null DECLs. */
538 static section *
539 hot_function_section (tree decl)
541 if (decl != NULL_TREE
542 && DECL_SECTION_NAME (decl) != NULL_TREE
543 && targetm.have_named_sections)
544 return get_named_section (decl, NULL, 0);
545 else
546 return text_section;
548 #endif
550 /* Return the section for function DECL.
552 If DECL is NULL_TREE, return the text section. We can be passed
553 NULL_TREE under some circumstances by dbxout.c at least. */
555 section *
556 function_section (tree decl)
558 int reloc = 0;
560 if (first_function_block_is_cold)
561 reloc = 1;
563 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
564 if (decl != NULL_TREE
565 && DECL_SECTION_NAME (decl) != NULL_TREE)
566 return reloc ? unlikely_text_section ()
567 : get_named_section (decl, NULL, 0);
568 else
569 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
570 #else
571 return reloc ? unlikely_text_section () : hot_function_section (decl);
572 #endif
575 section *
576 current_function_section (void)
578 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
579 if (current_function_decl != NULL_TREE
580 && DECL_SECTION_NAME (current_function_decl) != NULL_TREE)
581 return in_cold_section_p ? unlikely_text_section ()
582 : get_named_section (current_function_decl,
583 NULL, 0);
584 else
585 return targetm.asm_out.select_section (current_function_decl,
586 in_cold_section_p,
587 DECL_ALIGN (current_function_decl));
588 #else
589 return (in_cold_section_p
590 ? unlikely_text_section ()
591 : hot_function_section (current_function_decl));
592 #endif
595 /* Return the read-only data section associated with function DECL. */
597 section *
598 default_function_rodata_section (tree decl)
600 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
602 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
604 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
606 size_t len = strlen (name) + 3;
607 char* rname = alloca (len);
609 strcpy (rname, ".rodata");
610 strcat (rname, name + 5);
611 return get_section (rname, SECTION_LINKONCE, decl);
613 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
614 else if (DECL_ONE_ONLY (decl)
615 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
617 size_t len = strlen (name) + 1;
618 char *rname = alloca (len);
620 memcpy (rname, name, len);
621 rname[14] = 'r';
622 return get_section (rname, SECTION_LINKONCE, decl);
624 /* For .text.foo we want to use .rodata.foo. */
625 else if (flag_function_sections && flag_data_sections
626 && strncmp (name, ".text.", 6) == 0)
628 size_t len = strlen (name) + 1;
629 char *rname = alloca (len + 2);
631 memcpy (rname, ".rodata", 7);
632 memcpy (rname + 7, name + 5, len - 5);
633 return get_section (rname, 0, decl);
637 return readonly_data_section;
640 /* Return the read-only data section associated with function DECL
641 for targets where that section should be always the single
642 readonly data section. */
644 section *
645 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
647 return readonly_data_section;
650 /* Return the section to use for string merging. */
652 static section *
653 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
654 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
655 unsigned int flags ATTRIBUTE_UNUSED)
657 HOST_WIDE_INT len;
659 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
660 && TREE_CODE (decl) == STRING_CST
661 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
662 && align <= 256
663 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
664 && TREE_STRING_LENGTH (decl) >= len)
666 enum machine_mode mode;
667 unsigned int modesize;
668 const char *str;
669 HOST_WIDE_INT i;
670 int j, unit;
671 char name[30];
673 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
674 modesize = GET_MODE_BITSIZE (mode);
675 if (modesize >= 8 && modesize <= 256
676 && (modesize & (modesize - 1)) == 0)
678 if (align < modesize)
679 align = modesize;
681 str = TREE_STRING_POINTER (decl);
682 unit = GET_MODE_SIZE (mode);
684 /* Check for embedded NUL characters. */
685 for (i = 0; i < len; i += unit)
687 for (j = 0; j < unit; j++)
688 if (str[i + j] != '\0')
689 break;
690 if (j == unit)
691 break;
693 if (i == len - unit)
695 sprintf (name, ".rodata.str%d.%d", modesize / 8,
696 (int) (align / 8));
697 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
698 return get_section (name, flags, NULL);
703 return readonly_data_section;
706 /* Return the section to use for constant merging. */
708 section *
709 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
710 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
711 unsigned int flags ATTRIBUTE_UNUSED)
713 unsigned int modesize = GET_MODE_BITSIZE (mode);
715 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
716 && mode != VOIDmode
717 && mode != BLKmode
718 && modesize <= align
719 && align >= 8
720 && align <= 256
721 && (align & (align - 1)) == 0)
723 char name[24];
725 sprintf (name, ".rodata.cst%d", (int) (align / 8));
726 flags |= (align / 8) | SECTION_MERGE;
727 return get_section (name, flags, NULL);
729 return readonly_data_section;
732 /* Given NAME, a putative register name, discard any customary prefixes. */
734 static const char *
735 strip_reg_name (const char *name)
737 #ifdef REGISTER_PREFIX
738 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
739 name += strlen (REGISTER_PREFIX);
740 #endif
741 if (name[0] == '%' || name[0] == '#')
742 name++;
743 return name;
746 /* The user has asked for a DECL to have a particular name. Set (or
747 change) it in such a way that we don't prefix an underscore to
748 it. */
749 void
750 set_user_assembler_name (tree decl, const char *name)
752 char *starred = alloca (strlen (name) + 2);
753 starred[0] = '*';
754 strcpy (starred + 1, name);
755 change_decl_assembler_name (decl, get_identifier (starred));
756 SET_DECL_RTL (decl, NULL_RTX);
759 /* Decode an `asm' spec for a declaration as a register name.
760 Return the register number, or -1 if nothing specified,
761 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
762 or -3 if ASMSPEC is `cc' and is not recognized,
763 or -4 if ASMSPEC is `memory' and is not recognized.
764 Accept an exact spelling or a decimal number.
765 Prefixes such as % are optional. */
768 decode_reg_name (const char *asmspec)
770 if (asmspec != 0)
772 int i;
774 /* Get rid of confusing prefixes. */
775 asmspec = strip_reg_name (asmspec);
777 /* Allow a decimal number as a "register name". */
778 for (i = strlen (asmspec) - 1; i >= 0; i--)
779 if (! ISDIGIT (asmspec[i]))
780 break;
781 if (asmspec[0] != 0 && i < 0)
783 i = atoi (asmspec);
784 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
785 return i;
786 else
787 return -2;
790 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
791 if (reg_names[i][0]
792 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
793 return i;
795 #ifdef ADDITIONAL_REGISTER_NAMES
797 static const struct { const char *const name; const int number; } table[]
798 = ADDITIONAL_REGISTER_NAMES;
800 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
801 if (table[i].name[0]
802 && ! strcmp (asmspec, table[i].name))
803 return table[i].number;
805 #endif /* ADDITIONAL_REGISTER_NAMES */
807 if (!strcmp (asmspec, "memory"))
808 return -4;
810 if (!strcmp (asmspec, "cc"))
811 return -3;
813 return -2;
816 return -1;
819 /* Return true if DECL's initializer is suitable for a BSS section. */
821 static bool
822 bss_initializer_p (tree decl)
824 return (DECL_INITIAL (decl) == NULL
825 || DECL_INITIAL (decl) == error_mark_node
826 || (flag_zero_initialized_in_bss
827 /* Leave constant zeroes in .rodata so they
828 can be shared. */
829 && !TREE_READONLY (decl)
830 && initializer_zerop (DECL_INITIAL (decl))));
833 /* Compute the alignment of variable specified by DECL.
834 DONT_OUTPUT_DATA is from assemble_variable. */
836 void
837 align_variable (tree decl, bool dont_output_data)
839 unsigned int align = DECL_ALIGN (decl);
841 /* In the case for initialing an array whose length isn't specified,
842 where we have not yet been able to do the layout,
843 figure out the proper alignment now. */
844 if (dont_output_data && DECL_SIZE (decl) == 0
845 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
846 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
848 /* Some object file formats have a maximum alignment which they support.
849 In particular, a.out format supports a maximum alignment of 4. */
850 if (align > MAX_OFILE_ALIGNMENT)
852 warning (0, "alignment of %q+D is greater than maximum object "
853 "file alignment. Using %d", decl,
854 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
855 align = MAX_OFILE_ALIGNMENT;
858 /* On some machines, it is good to increase alignment sometimes. */
859 if (! DECL_USER_ALIGN (decl))
861 #ifdef DATA_ALIGNMENT
862 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
863 #endif
864 #ifdef CONSTANT_ALIGNMENT
865 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
866 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
867 #endif
870 /* Reset the alignment in case we have made it tighter, so we can benefit
871 from it in get_pointer_alignment. */
872 DECL_ALIGN (decl) = align;
875 /* Return the section into which the given VAR_DECL or CONST_DECL
876 should be placed. PREFER_NOSWITCH_P is true if a noswitch
877 section should be used wherever possible. */
879 static section *
880 get_variable_section (tree decl, bool prefer_noswitch_p)
882 int reloc;
884 /* If the decl has been given an explicit section name, then it
885 isn't common, and shouldn't be handled as such. */
886 if (DECL_COMMON (decl) && DECL_SECTION_NAME (decl) == NULL)
888 if (DECL_THREAD_LOCAL_P (decl))
889 return tls_comm_section;
890 if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
891 return comm_section;
894 if (DECL_INITIAL (decl) == error_mark_node)
895 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
896 else if (DECL_INITIAL (decl))
897 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
898 else
899 reloc = 0;
901 resolve_unique_section (decl, reloc, flag_data_sections);
902 if (IN_NAMED_SECTION (decl))
903 return get_named_section (decl, NULL, reloc);
905 if (!DECL_THREAD_LOCAL_P (decl)
906 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
907 && bss_initializer_p (decl))
909 if (!TREE_PUBLIC (decl))
910 return lcomm_section;
911 if (bss_noswitch_section)
912 return bss_noswitch_section;
915 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
918 /* Return the block into which object_block DECL should be placed. */
920 static struct object_block *
921 get_block_for_decl (tree decl)
923 section *sect;
925 if (TREE_CODE (decl) == VAR_DECL)
927 /* The object must be defined in this translation unit. */
928 if (DECL_EXTERNAL (decl))
929 return NULL;
931 /* There's no point using object blocks for something that is
932 isolated by definition. */
933 if (DECL_ONE_ONLY (decl))
934 return NULL;
937 /* We can only calculate block offsets if the decl has a known
938 constant size. */
939 if (DECL_SIZE_UNIT (decl) == NULL)
940 return NULL;
941 if (!host_integerp (DECL_SIZE_UNIT (decl), 1))
942 return NULL;
944 /* Find out which section should contain DECL. We cannot put it into
945 an object block if it requires a standalone definition. */
946 if (TREE_CODE (decl) == VAR_DECL)
947 align_variable (decl, 0);
948 sect = get_variable_section (decl, true);
949 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
950 return NULL;
952 return get_block_for_section (sect);
955 /* Make sure block symbol SYMBOL is in block BLOCK. */
957 static void
958 change_symbol_block (rtx symbol, struct object_block *block)
960 if (block != SYMBOL_REF_BLOCK (symbol))
962 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
963 SYMBOL_REF_BLOCK (symbol) = block;
967 /* Return true if it is possible to put DECL in an object_block. */
969 static bool
970 use_blocks_for_decl_p (tree decl)
972 /* Only data DECLs can be placed into object blocks. */
973 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
974 return false;
976 /* Detect decls created by dw2_force_const_mem. Such decls are
977 special because DECL_INITIAL doesn't specify the decl's true value.
978 dw2_output_indirect_constants will instead call assemble_variable
979 with dont_output_data set to 1 and then print the contents itself. */
980 if (DECL_INITIAL (decl) == decl)
981 return false;
983 return true;
986 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
987 have static storage duration. In other words, it should not be an
988 automatic variable, including PARM_DECLs.
990 There is, however, one exception: this function handles variables
991 explicitly placed in a particular register by the user.
993 This is never called for PARM_DECL nodes. */
995 void
996 make_decl_rtl (tree decl)
998 const char *name = 0;
999 int reg_number;
1000 rtx x;
1002 /* Check that we are not being given an automatic variable. */
1003 gcc_assert (TREE_CODE (decl) != PARM_DECL
1004 && TREE_CODE (decl) != RESULT_DECL);
1006 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1007 gcc_assert (TREE_CODE (decl) != VAR_DECL
1008 || TREE_STATIC (decl)
1009 || TREE_PUBLIC (decl)
1010 || DECL_EXTERNAL (decl)
1011 || DECL_REGISTER (decl));
1013 /* And that we were not given a type or a label. */
1014 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1015 && TREE_CODE (decl) != LABEL_DECL);
1017 /* For a duplicate declaration, we can be called twice on the
1018 same DECL node. Don't discard the RTL already made. */
1019 if (DECL_RTL_SET_P (decl))
1021 /* If the old RTL had the wrong mode, fix the mode. */
1022 x = DECL_RTL (decl);
1023 if (GET_MODE (x) != DECL_MODE (decl))
1024 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1026 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1027 return;
1029 /* ??? Another way to do this would be to maintain a hashed
1030 table of such critters. Instead of adding stuff to a DECL
1031 to give certain attributes to it, we could use an external
1032 hash map from DECL to set of attributes. */
1034 /* Let the target reassign the RTL if it wants.
1035 This is necessary, for example, when one machine specific
1036 decl attribute overrides another. */
1037 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1039 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1040 on the new decl information. */
1041 if (MEM_P (x)
1042 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1043 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1044 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1046 /* Make this function static known to the mudflap runtime. */
1047 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1048 mudflap_enqueue_decl (decl);
1050 return;
1053 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1055 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1056 && DECL_REGISTER (decl))
1058 error ("register name not specified for %q+D", decl);
1060 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1062 const char *asmspec = name+1;
1063 reg_number = decode_reg_name (asmspec);
1064 /* First detect errors in declaring global registers. */
1065 if (reg_number == -1)
1066 error ("register name not specified for %q+D", decl);
1067 else if (reg_number < 0)
1068 error ("invalid register name for %q+D", decl);
1069 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
1070 error ("data type of %q+D isn%'t suitable for a register",
1071 decl);
1072 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
1073 error ("register specified for %q+D isn%'t suitable for data type",
1074 decl);
1075 /* Now handle properly declared static register variables. */
1076 else
1078 int nregs;
1080 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1082 DECL_INITIAL (decl) = 0;
1083 error ("global register variable has initial value");
1085 if (TREE_THIS_VOLATILE (decl))
1086 warning (OPT_Wvolatile_register_var,
1087 "optimization may eliminate reads and/or "
1088 "writes to register variables");
1090 /* If the user specified one of the eliminables registers here,
1091 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1092 confused with that register and be eliminated. This usage is
1093 somewhat suspect... */
1095 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
1096 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1097 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1099 if (TREE_STATIC (decl))
1101 /* Make this register global, so not usable for anything
1102 else. */
1103 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1104 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1105 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1106 #endif
1107 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
1108 while (nregs > 0)
1109 globalize_reg (reg_number + --nregs);
1112 /* As a register variable, it has no section. */
1113 return;
1116 /* Now handle ordinary static variables and functions (in memory).
1117 Also handle vars declared register invalidly. */
1118 else if (name[0] == '*')
1120 #ifdef REGISTER_PREFIX
1121 if (strlen (REGISTER_PREFIX) != 0)
1123 reg_number = decode_reg_name (name);
1124 if (reg_number >= 0 || reg_number == -3)
1125 error ("register name given for non-register variable %q+D", decl);
1127 #endif
1130 /* Specifying a section attribute on a variable forces it into a
1131 non-.bss section, and thus it cannot be common. */
1132 if (TREE_CODE (decl) == VAR_DECL
1133 && DECL_SECTION_NAME (decl) != NULL_TREE
1134 && DECL_INITIAL (decl) == NULL_TREE
1135 && DECL_COMMON (decl))
1136 DECL_COMMON (decl) = 0;
1138 /* Variables can't be both common and weak. */
1139 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1140 DECL_COMMON (decl) = 0;
1142 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1143 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1144 else
1145 x = gen_rtx_SYMBOL_REF (Pmode, name);
1146 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1147 SET_SYMBOL_REF_DECL (x, decl);
1149 x = gen_rtx_MEM (DECL_MODE (decl), x);
1150 if (TREE_CODE (decl) != FUNCTION_DECL)
1151 set_mem_attributes (x, decl, 1);
1152 SET_DECL_RTL (decl, x);
1154 /* Optionally set flags or add text to the name to record information
1155 such as that it is a function name.
1156 If the name is changed, the macro ASM_OUTPUT_LABELREF
1157 will have to know how to strip this information. */
1158 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1160 /* Make this function static known to the mudflap runtime. */
1161 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1162 mudflap_enqueue_decl (decl);
1165 /* Make the rtl for variable VAR be volatile.
1166 Use this only for static variables. */
1168 void
1169 make_var_volatile (tree var)
1171 gcc_assert (MEM_P (DECL_RTL (var)));
1173 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
1176 /* Output a string of literal assembler code
1177 for an `asm' keyword used between functions. */
1179 void
1180 assemble_asm (tree string)
1182 app_enable ();
1184 if (TREE_CODE (string) == ADDR_EXPR)
1185 string = TREE_OPERAND (string, 0);
1187 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1190 /* Record an element in the table of global destructors. SYMBOL is
1191 a SYMBOL_REF of the function to be called; PRIORITY is a number
1192 between 0 and MAX_INIT_PRIORITY. */
1194 void
1195 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1196 int priority ATTRIBUTE_UNUSED)
1198 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1199 /* Tell GNU LD that this is part of the static destructor set.
1200 This will work for any system that uses stabs, most usefully
1201 aout systems. */
1202 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1203 dbxout_stab_value_label (XSTR (symbol, 0));
1204 #else
1205 sorry ("global destructors not supported on this target");
1206 #endif
1209 void
1210 default_named_section_asm_out_destructor (rtx symbol, int priority)
1212 const char *section = ".dtors";
1213 char buf[16];
1215 /* ??? This only works reliably with the GNU linker. */
1216 if (priority != DEFAULT_INIT_PRIORITY)
1218 sprintf (buf, ".dtors.%.5u",
1219 /* Invert the numbering so the linker puts us in the proper
1220 order; constructors are run from right to left, and the
1221 linker sorts in increasing order. */
1222 MAX_INIT_PRIORITY - priority);
1223 section = buf;
1226 switch_to_section (get_section (section, SECTION_WRITE, NULL));
1227 assemble_align (POINTER_SIZE);
1228 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1231 #ifdef DTORS_SECTION_ASM_OP
1232 void
1233 default_dtor_section_asm_out_destructor (rtx symbol,
1234 int priority ATTRIBUTE_UNUSED)
1236 switch_to_section (dtors_section);
1237 assemble_align (POINTER_SIZE);
1238 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1240 #endif
1242 /* Likewise for global constructors. */
1244 void
1245 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1246 int priority ATTRIBUTE_UNUSED)
1248 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1249 /* Tell GNU LD that this is part of the static destructor set.
1250 This will work for any system that uses stabs, most usefully
1251 aout systems. */
1252 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1253 dbxout_stab_value_label (XSTR (symbol, 0));
1254 #else
1255 sorry ("global constructors not supported on this target");
1256 #endif
1259 void
1260 default_named_section_asm_out_constructor (rtx symbol, int priority)
1262 const char *section = ".ctors";
1263 char buf[16];
1265 /* ??? This only works reliably with the GNU linker. */
1266 if (priority != DEFAULT_INIT_PRIORITY)
1268 sprintf (buf, ".ctors.%.5u",
1269 /* Invert the numbering so the linker puts us in the proper
1270 order; constructors are run from right to left, and the
1271 linker sorts in increasing order. */
1272 MAX_INIT_PRIORITY - priority);
1273 section = buf;
1276 switch_to_section (get_section (section, SECTION_WRITE, NULL));
1277 assemble_align (POINTER_SIZE);
1278 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1281 #ifdef CTORS_SECTION_ASM_OP
1282 void
1283 default_ctor_section_asm_out_constructor (rtx symbol,
1284 int priority ATTRIBUTE_UNUSED)
1286 switch_to_section (ctors_section);
1287 assemble_align (POINTER_SIZE);
1288 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1290 #endif
1292 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1293 a nonzero value if the constant pool should be output before the
1294 start of the function, or a zero value if the pool should output
1295 after the end of the function. The default is to put it before the
1296 start. */
1298 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1299 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1300 #endif
1302 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1303 to be output to assembler.
1304 Set first_global_object_name and weak_global_object_name as appropriate. */
1306 void
1307 notice_global_symbol (tree decl)
1309 const char **type = &first_global_object_name;
1311 if (first_global_object_name
1312 || !TREE_PUBLIC (decl)
1313 || DECL_EXTERNAL (decl)
1314 || !DECL_NAME (decl)
1315 || (TREE_CODE (decl) != FUNCTION_DECL
1316 && (TREE_CODE (decl) != VAR_DECL
1317 || (DECL_COMMON (decl)
1318 && (DECL_INITIAL (decl) == 0
1319 || DECL_INITIAL (decl) == error_mark_node))))
1320 || !MEM_P (DECL_RTL (decl)))
1321 return;
1323 /* We win when global object is found, but it is useful to know about weak
1324 symbol as well so we can produce nicer unique names. */
1325 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1326 type = &weak_global_object_name;
1328 if (!*type)
1330 const char *p;
1331 const char *name;
1332 rtx decl_rtl = DECL_RTL (decl);
1334 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1335 name = ggc_strdup (p);
1337 *type = name;
1341 /* Output assembler code for the constant pool of a function and associated
1342 with defining the name of the function. DECL describes the function.
1343 NAME is the function's name. For the constant pool, we use the current
1344 constant pool data. */
1346 void
1347 assemble_start_function (tree decl, const char *fnname)
1349 int align;
1350 char tmp_label[100];
1351 bool hot_label_written = false;
1353 cfun->unlikely_text_section_name = NULL;
1355 first_function_block_is_cold = false;
1356 if (flag_reorder_blocks_and_partition)
1358 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1359 cfun->hot_section_label = ggc_strdup (tmp_label);
1360 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1361 cfun->cold_section_label = ggc_strdup (tmp_label);
1362 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1363 cfun->hot_section_end_label = ggc_strdup (tmp_label);
1364 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1365 cfun->cold_section_end_label = ggc_strdup (tmp_label);
1366 const_labelno++;
1368 else
1370 cfun->hot_section_label = NULL;
1371 cfun->cold_section_label = NULL;
1372 cfun->hot_section_end_label = NULL;
1373 cfun->cold_section_end_label = NULL;
1376 /* The following code does not need preprocessing in the assembler. */
1378 app_disable ();
1380 if (CONSTANT_POOL_BEFORE_FUNCTION)
1381 output_constant_pool (fnname, decl);
1383 resolve_unique_section (decl, 0, flag_function_sections);
1385 /* Make sure the not and cold text (code) sections are properly
1386 aligned. This is necessary here in the case where the function
1387 has both hot and cold sections, because we don't want to re-set
1388 the alignment when the section switch happens mid-function. */
1390 if (flag_reorder_blocks_and_partition)
1392 switch_to_section (unlikely_text_section ());
1393 assemble_align (FUNCTION_BOUNDARY);
1394 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_label);
1396 /* When the function starts with a cold section, we need to explicitly
1397 align the hot section and write out the hot section label.
1398 But if the current function is a thunk, we do not have a CFG. */
1399 if (!current_function_is_thunk
1400 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1402 switch_to_section (text_section);
1403 assemble_align (FUNCTION_BOUNDARY);
1404 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1405 hot_label_written = true;
1406 first_function_block_is_cold = true;
1409 else if (DECL_SECTION_NAME (decl))
1411 /* Calls to function_section rely on first_function_block_is_cold
1412 being accurate. The first block may be cold even if we aren't
1413 doing partitioning, if the entire function was decided by
1414 choose_function_section (predict.c) to be cold. */
1416 initialize_cold_section_name ();
1418 if (cfun->unlikely_text_section_name
1419 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1420 cfun->unlikely_text_section_name) == 0)
1421 first_function_block_is_cold = true;
1424 in_cold_section_p = first_function_block_is_cold;
1426 /* Switch to the correct text section for the start of the function. */
1428 switch_to_section (function_section (decl));
1429 if (flag_reorder_blocks_and_partition
1430 && !hot_label_written)
1431 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1433 /* Tell assembler to move to target machine's alignment for functions. */
1434 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1435 if (align < force_align_functions_log)
1436 align = force_align_functions_log;
1437 if (align > 0)
1439 ASM_OUTPUT_ALIGN (asm_out_file, align);
1442 /* Handle a user-specified function alignment.
1443 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1444 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1445 if (align_functions_log > align
1446 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1448 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1449 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1450 align_functions_log, align_functions - 1);
1451 #else
1452 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1453 #endif
1456 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1457 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1458 #endif
1460 (*debug_hooks->begin_function) (decl);
1462 /* Make function name accessible from other files, if appropriate. */
1464 if (TREE_PUBLIC (decl))
1466 notice_global_symbol (decl);
1468 globalize_decl (decl);
1470 maybe_assemble_visibility (decl);
1473 if (DECL_PRESERVE_P (decl))
1474 targetm.asm_out.mark_decl_preserved (fnname);
1476 /* Do any machine/system dependent processing of the function name. */
1477 #ifdef ASM_DECLARE_FUNCTION_NAME
1478 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1479 #else
1480 /* Standard thing is just output label for the function. */
1481 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1482 #endif /* ASM_DECLARE_FUNCTION_NAME */
1485 /* Output assembler code associated with defining the size of the
1486 function. DECL describes the function. NAME is the function's name. */
1488 void
1489 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1491 #ifdef ASM_DECLARE_FUNCTION_SIZE
1492 /* We could have switched section in the middle of the function. */
1493 if (flag_reorder_blocks_and_partition)
1494 switch_to_section (function_section (decl));
1495 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1496 #endif
1497 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1499 output_constant_pool (fnname, decl);
1500 switch_to_section (function_section (decl)); /* need to switch back */
1502 /* Output labels for end of hot/cold text sections (to be used by
1503 debug info.) */
1504 if (flag_reorder_blocks_and_partition)
1506 section *save_text_section;
1508 save_text_section = in_section;
1509 switch_to_section (unlikely_text_section ());
1510 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_end_label);
1511 if (first_function_block_is_cold)
1512 switch_to_section (text_section);
1513 else
1514 switch_to_section (function_section (decl));
1515 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_end_label);
1516 switch_to_section (save_text_section);
1520 /* Assemble code to leave SIZE bytes of zeros. */
1522 void
1523 assemble_zeros (unsigned HOST_WIDE_INT size)
1525 /* Do no output if -fsyntax-only. */
1526 if (flag_syntax_only)
1527 return;
1529 #ifdef ASM_NO_SKIP_IN_TEXT
1530 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1531 so we must output 0s explicitly in the text section. */
1532 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1534 unsigned HOST_WIDE_INT i;
1535 for (i = 0; i < size; i++)
1536 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1538 else
1539 #endif
1540 if (size > 0)
1541 ASM_OUTPUT_SKIP (asm_out_file, size);
1544 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1546 void
1547 assemble_align (int align)
1549 if (align > BITS_PER_UNIT)
1551 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1555 /* Assemble a string constant with the specified C string as contents. */
1557 void
1558 assemble_string (const char *p, int size)
1560 int pos = 0;
1561 int maximum = 2000;
1563 /* If the string is very long, split it up. */
1565 while (pos < size)
1567 int thissize = size - pos;
1568 if (thissize > maximum)
1569 thissize = maximum;
1571 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1573 pos += thissize;
1574 p += thissize;
1579 /* A noswitch_section_callback for lcomm_section. */
1581 static bool
1582 emit_local (tree decl ATTRIBUTE_UNUSED,
1583 const char *name ATTRIBUTE_UNUSED,
1584 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1585 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1587 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1588 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1589 size, DECL_ALIGN (decl));
1590 return true;
1591 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1592 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl));
1593 return true;
1594 #else
1595 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1596 return false;
1597 #endif
1600 /* A noswitch_section_callback for bss_noswitch_section. */
1602 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
1603 static bool
1604 emit_bss (tree decl ATTRIBUTE_UNUSED,
1605 const char *name ATTRIBUTE_UNUSED,
1606 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1607 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1609 #if defined ASM_OUTPUT_ALIGNED_BSS
1610 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl));
1611 return true;
1612 #else
1613 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded);
1614 return false;
1615 #endif
1617 #endif
1619 /* A noswitch_section_callback for comm_section. */
1621 static bool
1622 emit_common (tree decl ATTRIBUTE_UNUSED,
1623 const char *name ATTRIBUTE_UNUSED,
1624 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1625 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1627 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1628 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1629 size, DECL_ALIGN (decl));
1630 return true;
1631 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1632 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl));
1633 return true;
1634 #else
1635 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1636 return false;
1637 #endif
1640 /* A noswitch_section_callback for tls_comm_section. */
1642 static bool
1643 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1644 const char *name ATTRIBUTE_UNUSED,
1645 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1646 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1648 #ifdef ASM_OUTPUT_TLS_COMMON
1649 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1650 return true;
1651 #else
1652 sorry ("thread-local COMMON data not implemented");
1653 return true;
1654 #endif
1657 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
1658 NAME is the name of DECL's SYMBOL_REF. */
1660 static void
1661 assemble_noswitch_variable (tree decl, const char *name, section *sect)
1663 unsigned HOST_WIDE_INT size, rounded;
1665 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1666 rounded = size;
1668 /* Don't allocate zero bytes of common,
1669 since that means "undefined external" in the linker. */
1670 if (size == 0)
1671 rounded = 1;
1673 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1674 so that each uninitialized object starts on such a boundary. */
1675 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1676 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1677 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1679 if (!sect->noswitch.callback (decl, name, size, rounded)
1680 && (unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1681 warning (0, "requested alignment for %q+D is greater than "
1682 "implemented alignment of %wu", decl, rounded);
1685 /* A subroutine of assemble_variable. Output the label and contents of
1686 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
1687 is as for assemble_variable. */
1689 static void
1690 assemble_variable_contents (tree decl, const char *name,
1691 bool dont_output_data)
1693 /* Do any machine/system dependent processing of the object. */
1694 #ifdef ASM_DECLARE_OBJECT_NAME
1695 last_assemble_variable_decl = decl;
1696 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1697 #else
1698 /* Standard thing is just output label for the object. */
1699 ASM_OUTPUT_LABEL (asm_out_file, name);
1700 #endif /* ASM_DECLARE_OBJECT_NAME */
1702 if (!dont_output_data)
1704 if (DECL_INITIAL (decl)
1705 && DECL_INITIAL (decl) != error_mark_node
1706 && !initializer_zerop (DECL_INITIAL (decl)))
1707 /* Output the actual data. */
1708 output_constant (DECL_INITIAL (decl),
1709 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1710 DECL_ALIGN (decl));
1711 else
1712 /* Leave space for it. */
1713 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1717 /* Assemble everything that is needed for a variable or function declaration.
1718 Not used for automatic variables, and not used for function definitions.
1719 Should not be called for variables of incomplete structure type.
1721 TOP_LEVEL is nonzero if this variable has file scope.
1722 AT_END is nonzero if this is the special handling, at end of compilation,
1723 to define things that have had only tentative definitions.
1724 DONT_OUTPUT_DATA if nonzero means don't actually output the
1725 initial value (that will be done by the caller). */
1727 void
1728 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1729 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1731 const char *name;
1732 rtx decl_rtl, symbol;
1733 section *sect;
1735 if (lang_hooks.decls.prepare_assemble_variable)
1736 lang_hooks.decls.prepare_assemble_variable (decl);
1738 last_assemble_variable_decl = 0;
1740 /* Normally no need to say anything here for external references,
1741 since assemble_external is called by the language-specific code
1742 when a declaration is first seen. */
1744 if (DECL_EXTERNAL (decl))
1745 return;
1747 /* Output no assembler code for a function declaration.
1748 Only definitions of functions output anything. */
1750 if (TREE_CODE (decl) == FUNCTION_DECL)
1751 return;
1753 /* Do nothing for global register variables. */
1754 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1756 TREE_ASM_WRITTEN (decl) = 1;
1757 return;
1760 /* If type was incomplete when the variable was declared,
1761 see if it is complete now. */
1763 if (DECL_SIZE (decl) == 0)
1764 layout_decl (decl, 0);
1766 /* Still incomplete => don't allocate it; treat the tentative defn
1767 (which is what it must have been) as an `extern' reference. */
1769 if (!dont_output_data && DECL_SIZE (decl) == 0)
1771 error ("storage size of %q+D isn%'t known", decl);
1772 TREE_ASM_WRITTEN (decl) = 1;
1773 return;
1776 /* The first declaration of a variable that comes through this function
1777 decides whether it is global (in C, has external linkage)
1778 or local (in C, has internal linkage). So do nothing more
1779 if this function has already run. */
1781 if (TREE_ASM_WRITTEN (decl))
1782 return;
1784 /* Make sure targetm.encode_section_info is invoked before we set
1785 ASM_WRITTEN. */
1786 decl_rtl = DECL_RTL (decl);
1788 TREE_ASM_WRITTEN (decl) = 1;
1790 /* Do no output if -fsyntax-only. */
1791 if (flag_syntax_only)
1792 return;
1794 app_disable ();
1796 if (! dont_output_data
1797 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1799 error ("size of variable %q+D is too large", decl);
1800 return;
1803 gcc_assert (MEM_P (decl_rtl));
1804 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
1805 symbol = XEXP (decl_rtl, 0);
1806 name = XSTR (symbol, 0);
1807 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1808 notice_global_symbol (decl);
1810 /* Compute the alignment of this data. */
1812 align_variable (decl, dont_output_data);
1813 set_mem_align (decl_rtl, DECL_ALIGN (decl));
1815 if (TREE_PUBLIC (decl))
1816 maybe_assemble_visibility (decl);
1818 if (DECL_PRESERVE_P (decl))
1819 targetm.asm_out.mark_decl_preserved (name);
1821 /* First make the assembler name(s) global if appropriate. */
1822 sect = get_variable_section (decl, false);
1823 if (TREE_PUBLIC (decl)
1824 && DECL_NAME (decl)
1825 && (sect->common.flags & SECTION_COMMON) == 0)
1826 globalize_decl (decl);
1828 /* Output any data that we will need to use the address of. */
1829 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
1830 output_addressed_constants (DECL_INITIAL (decl));
1832 /* dbxout.c needs to know this. */
1833 if (sect && (sect->common.flags & SECTION_CODE) != 0)
1834 DECL_IN_TEXT_SECTION (decl) = 1;
1836 /* If the decl is part of an object_block, make sure that the decl
1837 has been positioned within its block, but do not write out its
1838 definition yet. output_object_blocks will do that later. */
1839 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
1841 gcc_assert (!dont_output_data);
1842 place_block_symbol (symbol);
1844 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1845 assemble_noswitch_variable (decl, name, sect);
1846 else
1848 switch_to_section (sect);
1849 if (DECL_ALIGN (decl) > BITS_PER_UNIT)
1850 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1851 assemble_variable_contents (decl, name, dont_output_data);
1855 /* Return 1 if type TYPE contains any pointers. */
1857 static int
1858 contains_pointers_p (tree type)
1860 switch (TREE_CODE (type))
1862 case POINTER_TYPE:
1863 case REFERENCE_TYPE:
1864 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1865 so I'll play safe and return 1. */
1866 case OFFSET_TYPE:
1867 return 1;
1869 case RECORD_TYPE:
1870 case UNION_TYPE:
1871 case QUAL_UNION_TYPE:
1873 tree fields;
1874 /* For a type that has fields, see if the fields have pointers. */
1875 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1876 if (TREE_CODE (fields) == FIELD_DECL
1877 && contains_pointers_p (TREE_TYPE (fields)))
1878 return 1;
1879 return 0;
1882 case ARRAY_TYPE:
1883 /* An array type contains pointers if its element type does. */
1884 return contains_pointers_p (TREE_TYPE (type));
1886 default:
1887 return 0;
1891 /* In unit-at-a-time mode, we delay assemble_external processing until
1892 the compilation unit is finalized. This is the best we can do for
1893 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1894 it all the way to final. See PR 17982 for further discussion. */
1895 static GTY(()) tree pending_assemble_externals;
1897 #ifdef ASM_OUTPUT_EXTERNAL
1898 /* True if DECL is a function decl for which no out-of-line copy exists.
1899 It is assumed that DECL's assembler name has been set. */
1901 static bool
1902 incorporeal_function_p (tree decl)
1904 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1906 const char *name;
1908 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1909 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1910 return true;
1912 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1913 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1914 return true;
1916 return false;
1919 /* Actually do the tests to determine if this is necessary, and invoke
1920 ASM_OUTPUT_EXTERNAL. */
1921 static void
1922 assemble_external_real (tree decl)
1924 rtx rtl = DECL_RTL (decl);
1926 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1927 && !SYMBOL_REF_USED (XEXP (rtl, 0))
1928 && !incorporeal_function_p (decl))
1930 /* Some systems do require some output. */
1931 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1932 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1935 #endif
1937 void
1938 process_pending_assemble_externals (void)
1940 #ifdef ASM_OUTPUT_EXTERNAL
1941 tree list;
1942 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1943 assemble_external_real (TREE_VALUE (list));
1945 pending_assemble_externals = 0;
1946 #endif
1949 /* Output something to declare an external symbol to the assembler.
1950 (Most assemblers don't need this, so we normally output nothing.)
1951 Do nothing if DECL is not external. */
1953 void
1954 assemble_external (tree decl ATTRIBUTE_UNUSED)
1956 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1957 main body of this code is only rarely exercised. To provide some
1958 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1959 open. If it's not, we should not be calling this function. */
1960 gcc_assert (asm_out_file);
1962 #ifdef ASM_OUTPUT_EXTERNAL
1963 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1964 return;
1966 /* We want to output external symbols at very last to check if they
1967 are references or not. */
1968 pending_assemble_externals = tree_cons (0, decl,
1969 pending_assemble_externals);
1970 #endif
1973 /* Similar, for calling a library function FUN. */
1975 void
1976 assemble_external_libcall (rtx fun)
1978 /* Declare library function name external when first used, if nec. */
1979 if (! SYMBOL_REF_USED (fun))
1981 SYMBOL_REF_USED (fun) = 1;
1982 targetm.asm_out.external_libcall (fun);
1986 /* Assemble a label named NAME. */
1988 void
1989 assemble_label (const char *name)
1991 ASM_OUTPUT_LABEL (asm_out_file, name);
1994 /* Set the symbol_referenced flag for ID. */
1995 void
1996 mark_referenced (tree id)
1998 TREE_SYMBOL_REFERENCED (id) = 1;
2001 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2002 void
2003 mark_decl_referenced (tree decl)
2005 if (TREE_CODE (decl) == FUNCTION_DECL)
2007 /* Extern inline functions don't become needed when referenced.
2008 If we know a method will be emitted in other TU and no new
2009 functions can be marked reachable, just use the external
2010 definition. */
2011 struct cgraph_node *node = cgraph_node (decl);
2012 if (!DECL_EXTERNAL (decl)
2013 && (!node->local.vtable_method || !cgraph_global_info_ready
2014 || !node->local.finalized))
2015 cgraph_mark_needed_node (node);
2017 else if (TREE_CODE (decl) == VAR_DECL)
2019 struct varpool_node *node = varpool_node (decl);
2020 varpool_mark_needed_node (node);
2021 /* C++ frontend use mark_decl_references to force COMDAT variables
2022 to be output that might appear dead otherwise. */
2023 node->force_output = true;
2025 /* else do nothing - we can get various sorts of CST nodes here,
2026 which do not need to be marked. */
2030 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
2031 until we find an identifier that is not itself a transparent alias.
2032 Modify the alias passed to it by reference (and all aliases on the
2033 way to the ultimate target), such that they do not have to be
2034 followed again, and return the ultimate target of the alias
2035 chain. */
2037 static inline tree
2038 ultimate_transparent_alias_target (tree *alias)
2040 tree target = *alias;
2042 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
2044 gcc_assert (TREE_CHAIN (target));
2045 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
2046 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
2047 && ! TREE_CHAIN (target));
2048 *alias = target;
2051 return target;
2054 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2055 starts with a *, the rest of NAME is output verbatim. Otherwise
2056 NAME is transformed in a target-specific way (usually by the
2057 addition of an underscore). */
2059 void
2060 assemble_name_raw (FILE *file, const char *name)
2062 if (name[0] == '*')
2063 fputs (&name[1], file);
2064 else
2065 ASM_OUTPUT_LABELREF (file, name);
2068 /* Like assemble_name_raw, but should be used when NAME might refer to
2069 an entity that is also represented as a tree (like a function or
2070 variable). If NAME does refer to such an entity, that entity will
2071 be marked as referenced. */
2073 void
2074 assemble_name (FILE *file, const char *name)
2076 const char *real_name;
2077 tree id;
2079 real_name = targetm.strip_name_encoding (name);
2081 id = maybe_get_identifier (real_name);
2082 if (id)
2084 tree id_orig = id;
2086 mark_referenced (id);
2087 ultimate_transparent_alias_target (&id);
2088 if (id != id_orig)
2089 name = IDENTIFIER_POINTER (id);
2090 gcc_assert (! TREE_CHAIN (id));
2093 assemble_name_raw (file, name);
2096 /* Allocate SIZE bytes writable static space with a gensym name
2097 and return an RTX to refer to its address. */
2100 assemble_static_space (unsigned HOST_WIDE_INT size)
2102 char name[12];
2103 const char *namestring;
2104 rtx x;
2106 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2107 ++const_labelno;
2108 namestring = ggc_strdup (name);
2110 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2111 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2113 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2114 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2115 BIGGEST_ALIGNMENT);
2116 #else
2117 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2118 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2119 #else
2121 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2122 so that each uninitialized object starts on such a boundary. */
2123 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2124 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2125 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2126 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2127 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2128 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2130 #endif
2131 #endif
2132 return x;
2135 /* Assemble the static constant template for function entry trampolines.
2136 This is done at most once per compilation.
2137 Returns an RTX for the address of the template. */
2139 static GTY(()) rtx initial_trampoline;
2141 #ifdef TRAMPOLINE_TEMPLATE
2143 assemble_trampoline_template (void)
2145 char label[256];
2146 const char *name;
2147 int align;
2148 rtx symbol;
2150 if (initial_trampoline)
2151 return initial_trampoline;
2153 /* By default, put trampoline templates in read-only data section. */
2155 #ifdef TRAMPOLINE_SECTION
2156 switch_to_section (TRAMPOLINE_SECTION);
2157 #else
2158 switch_to_section (readonly_data_section);
2159 #endif
2161 /* Write the assembler code to define one. */
2162 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2163 if (align > 0)
2165 ASM_OUTPUT_ALIGN (asm_out_file, align);
2168 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2169 TRAMPOLINE_TEMPLATE (asm_out_file);
2171 /* Record the rtl to refer to it. */
2172 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2173 name = ggc_strdup (label);
2174 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2175 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2177 initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2178 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2180 return initial_trampoline;
2182 #endif
2184 /* A and B are either alignments or offsets. Return the minimum alignment
2185 that may be assumed after adding the two together. */
2187 static inline unsigned
2188 min_align (unsigned int a, unsigned int b)
2190 return (a | b) & -(a | b);
2193 /* Return the assembler directive for creating a given kind of integer
2194 object. SIZE is the number of bytes in the object and ALIGNED_P
2195 indicates whether it is known to be aligned. Return NULL if the
2196 assembly dialect has no such directive.
2198 The returned string should be printed at the start of a new line and
2199 be followed immediately by the object's initial value. */
2201 const char *
2202 integer_asm_op (int size, int aligned_p)
2204 struct asm_int_op *ops;
2206 if (aligned_p)
2207 ops = &targetm.asm_out.aligned_op;
2208 else
2209 ops = &targetm.asm_out.unaligned_op;
2211 switch (size)
2213 case 1:
2214 return targetm.asm_out.byte_op;
2215 case 2:
2216 return ops->hi;
2217 case 4:
2218 return ops->si;
2219 case 8:
2220 return ops->di;
2221 case 16:
2222 return ops->ti;
2223 default:
2224 return NULL;
2228 /* Use directive OP to assemble an integer object X. Print OP at the
2229 start of the line, followed immediately by the value of X. */
2231 void
2232 assemble_integer_with_op (const char *op, rtx x)
2234 fputs (op, asm_out_file);
2235 output_addr_const (asm_out_file, x);
2236 fputc ('\n', asm_out_file);
2239 /* The default implementation of the asm_out.integer target hook. */
2241 bool
2242 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2243 unsigned int size ATTRIBUTE_UNUSED,
2244 int aligned_p ATTRIBUTE_UNUSED)
2246 const char *op = integer_asm_op (size, aligned_p);
2247 /* Avoid GAS bugs for large values. Specifically negative values whose
2248 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2249 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2250 return false;
2251 return op && (assemble_integer_with_op (op, x), true);
2254 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2255 the alignment of the integer in bits. Return 1 if we were able to output
2256 the constant, otherwise 0. We must be able to output the constant,
2257 if FORCE is nonzero. */
2259 bool
2260 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2262 int aligned_p;
2264 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2266 /* See if the target hook can handle this kind of object. */
2267 if (targetm.asm_out.integer (x, size, aligned_p))
2268 return true;
2270 /* If the object is a multi-byte one, try splitting it up. Split
2271 it into words it if is multi-word, otherwise split it into bytes. */
2272 if (size > 1)
2274 enum machine_mode omode, imode;
2275 unsigned int subalign;
2276 unsigned int subsize, i;
2278 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2279 subalign = MIN (align, subsize * BITS_PER_UNIT);
2280 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
2281 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2283 for (i = 0; i < size; i += subsize)
2285 rtx partial = simplify_subreg (omode, x, imode, i);
2286 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2287 break;
2289 if (i == size)
2290 return true;
2292 /* If we've printed some of it, but not all of it, there's no going
2293 back now. */
2294 gcc_assert (!i);
2297 gcc_assert (!force);
2299 return false;
2302 void
2303 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2305 long data[4] = {0, 0, 0, 0};
2306 int i;
2307 int bitsize, nelts, nunits, units_per;
2309 /* This is hairy. We have a quantity of known size. real_to_target
2310 will put it into an array of *host* longs, 32 bits per element
2311 (even if long is more than 32 bits). We need to determine the
2312 number of array elements that are occupied (nelts) and the number
2313 of *target* min-addressable units that will be occupied in the
2314 object file (nunits). We cannot assume that 32 divides the
2315 mode's bitsize (size * BITS_PER_UNIT) evenly.
2317 size * BITS_PER_UNIT is used here to make sure that padding bits
2318 (which might appear at either end of the value; real_to_target
2319 will include the padding bits in its output array) are included. */
2321 nunits = GET_MODE_SIZE (mode);
2322 bitsize = nunits * BITS_PER_UNIT;
2323 nelts = CEIL (bitsize, 32);
2324 units_per = 32 / BITS_PER_UNIT;
2326 real_to_target (data, &d, mode);
2328 /* Put out the first word with the specified alignment. */
2329 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2330 nunits -= units_per;
2332 /* Subsequent words need only 32-bit alignment. */
2333 align = min_align (align, 32);
2335 for (i = 1; i < nelts; i++)
2337 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2338 nunits -= units_per;
2342 /* Given an expression EXP with a constant value,
2343 reduce it to the sum of an assembler symbol and an integer.
2344 Store them both in the structure *VALUE.
2345 EXP must be reducible. */
2347 struct addr_const GTY(())
2349 rtx base;
2350 HOST_WIDE_INT offset;
2353 static void
2354 decode_addr_const (tree exp, struct addr_const *value)
2356 tree target = TREE_OPERAND (exp, 0);
2357 int offset = 0;
2358 rtx x;
2360 while (1)
2362 if (TREE_CODE (target) == COMPONENT_REF
2363 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2366 offset += int_byte_position (TREE_OPERAND (target, 1));
2367 target = TREE_OPERAND (target, 0);
2369 else if (TREE_CODE (target) == ARRAY_REF
2370 || TREE_CODE (target) == ARRAY_RANGE_REF)
2372 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2373 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2374 target = TREE_OPERAND (target, 0);
2376 else
2377 break;
2380 switch (TREE_CODE (target))
2382 case VAR_DECL:
2383 case FUNCTION_DECL:
2384 x = DECL_RTL (target);
2385 break;
2387 case LABEL_DECL:
2388 x = gen_rtx_MEM (FUNCTION_MODE,
2389 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2390 break;
2392 case REAL_CST:
2393 case STRING_CST:
2394 case COMPLEX_CST:
2395 case CONSTRUCTOR:
2396 case INTEGER_CST:
2397 x = output_constant_def (target, 1);
2398 break;
2400 default:
2401 gcc_unreachable ();
2404 gcc_assert (MEM_P (x));
2405 x = XEXP (x, 0);
2407 value->base = x;
2408 value->offset = offset;
2411 /* Uniquize all constants that appear in memory.
2412 Each constant in memory thus far output is recorded
2413 in `const_desc_table'. */
2415 struct constant_descriptor_tree GTY(())
2417 /* A MEM for the constant. */
2418 rtx rtl;
2420 /* The value of the constant. */
2421 tree value;
2423 /* Hash of value. Computing the hash from value each time
2424 hashfn is called can't work properly, as that means recursive
2425 use of the hash table during hash table expansion. */
2426 hashval_t hash;
2429 static GTY((param_is (struct constant_descriptor_tree)))
2430 htab_t const_desc_htab;
2432 static struct constant_descriptor_tree * build_constant_desc (tree);
2433 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2435 /* Compute a hash code for a constant expression. */
2437 static hashval_t
2438 const_desc_hash (const void *ptr)
2440 return ((struct constant_descriptor_tree *)ptr)->hash;
2443 static hashval_t
2444 const_hash_1 (const tree exp)
2446 const char *p;
2447 hashval_t hi;
2448 int len, i;
2449 enum tree_code code = TREE_CODE (exp);
2451 /* Either set P and LEN to the address and len of something to hash and
2452 exit the switch or return a value. */
2454 switch (code)
2456 case INTEGER_CST:
2457 p = (char *) &TREE_INT_CST (exp);
2458 len = sizeof TREE_INT_CST (exp);
2459 break;
2461 case REAL_CST:
2462 return real_hash (TREE_REAL_CST_PTR (exp));
2464 case STRING_CST:
2465 p = TREE_STRING_POINTER (exp);
2466 len = TREE_STRING_LENGTH (exp);
2467 break;
2469 case COMPLEX_CST:
2470 return (const_hash_1 (TREE_REALPART (exp)) * 5
2471 + const_hash_1 (TREE_IMAGPART (exp)));
2473 case CONSTRUCTOR:
2475 unsigned HOST_WIDE_INT idx;
2476 tree value;
2478 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2480 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2481 if (value)
2482 hi = hi * 603 + const_hash_1 (value);
2484 return hi;
2487 case ADDR_EXPR:
2488 case FDESC_EXPR:
2490 struct addr_const value;
2492 decode_addr_const (exp, &value);
2493 switch (GET_CODE (value.base))
2495 case SYMBOL_REF:
2496 /* Don't hash the address of the SYMBOL_REF;
2497 only use the offset and the symbol name. */
2498 hi = value.offset;
2499 p = XSTR (value.base, 0);
2500 for (i = 0; p[i] != 0; i++)
2501 hi = ((hi * 613) + (unsigned) (p[i]));
2502 break;
2504 case LABEL_REF:
2505 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2506 break;
2508 default:
2509 gcc_unreachable ();
2512 return hi;
2514 case PLUS_EXPR:
2515 case MINUS_EXPR:
2516 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2517 + const_hash_1 (TREE_OPERAND (exp, 1)));
2519 case NOP_EXPR:
2520 case CONVERT_EXPR:
2521 case NON_LVALUE_EXPR:
2522 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2524 default:
2525 /* A language specific constant. Just hash the code. */
2526 return code;
2529 /* Compute hashing function. */
2530 hi = len;
2531 for (i = 0; i < len; i++)
2532 hi = ((hi * 613) + (unsigned) (p[i]));
2534 return hi;
2537 /* Wrapper of compare_constant, for the htab interface. */
2538 static int
2539 const_desc_eq (const void *p1, const void *p2)
2541 const struct constant_descriptor_tree *c1 = p1;
2542 const struct constant_descriptor_tree *c2 = p2;
2543 if (c1->hash != c2->hash)
2544 return 0;
2545 return compare_constant (c1->value, c2->value);
2548 /* Compare t1 and t2, and return 1 only if they are known to result in
2549 the same bit pattern on output. */
2551 static int
2552 compare_constant (const tree t1, const tree t2)
2554 enum tree_code typecode;
2556 if (t1 == NULL_TREE)
2557 return t2 == NULL_TREE;
2558 if (t2 == NULL_TREE)
2559 return 0;
2561 if (TREE_CODE (t1) != TREE_CODE (t2))
2562 return 0;
2564 switch (TREE_CODE (t1))
2566 case INTEGER_CST:
2567 /* Integer constants are the same only if the same width of type. */
2568 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2569 return 0;
2570 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2571 return 0;
2572 return tree_int_cst_equal (t1, t2);
2574 case REAL_CST:
2575 /* Real constants are the same only if the same width of type. */
2576 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2577 return 0;
2579 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2581 case STRING_CST:
2582 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2583 return 0;
2585 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2586 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2587 TREE_STRING_LENGTH (t1)));
2589 case COMPLEX_CST:
2590 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2591 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2593 case CONSTRUCTOR:
2595 VEC(constructor_elt, gc) *v1, *v2;
2596 unsigned HOST_WIDE_INT idx;
2598 typecode = TREE_CODE (TREE_TYPE (t1));
2599 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2600 return 0;
2602 if (typecode == ARRAY_TYPE)
2604 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2605 /* For arrays, check that the sizes all match. */
2606 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2607 || size_1 == -1
2608 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2609 return 0;
2611 else
2613 /* For record and union constructors, require exact type
2614 equality. */
2615 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2616 return 0;
2619 v1 = CONSTRUCTOR_ELTS (t1);
2620 v2 = CONSTRUCTOR_ELTS (t2);
2621 if (VEC_length (constructor_elt, v1)
2622 != VEC_length (constructor_elt, v2))
2623 return 0;
2625 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2627 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2628 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2630 /* Check that each value is the same... */
2631 if (!compare_constant (c1->value, c2->value))
2632 return 0;
2633 /* ... and that they apply to the same fields! */
2634 if (typecode == ARRAY_TYPE)
2636 if (!compare_constant (c1->index, c2->index))
2637 return 0;
2639 else
2641 if (c1->index != c2->index)
2642 return 0;
2646 return 1;
2649 case ADDR_EXPR:
2650 case FDESC_EXPR:
2652 struct addr_const value1, value2;
2654 decode_addr_const (t1, &value1);
2655 decode_addr_const (t2, &value2);
2656 return (value1.offset == value2.offset
2657 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2660 case PLUS_EXPR:
2661 case MINUS_EXPR:
2662 case RANGE_EXPR:
2663 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2664 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2666 case NOP_EXPR:
2667 case CONVERT_EXPR:
2668 case NON_LVALUE_EXPR:
2669 case VIEW_CONVERT_EXPR:
2670 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2672 default:
2674 tree nt1, nt2;
2675 nt1 = lang_hooks.expand_constant (t1);
2676 nt2 = lang_hooks.expand_constant (t2);
2677 if (nt1 != t1 || nt2 != t2)
2678 return compare_constant (nt1, nt2);
2679 else
2680 return 0;
2684 gcc_unreachable ();
2687 /* Make a copy of the whole tree structure for a constant. This
2688 handles the same types of nodes that compare_constant handles. */
2690 static tree
2691 copy_constant (tree exp)
2693 switch (TREE_CODE (exp))
2695 case ADDR_EXPR:
2696 /* For ADDR_EXPR, we do not want to copy the decl whose address
2697 is requested. We do want to copy constants though. */
2698 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2699 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2700 copy_constant (TREE_OPERAND (exp, 0)));
2701 else
2702 return copy_node (exp);
2704 case INTEGER_CST:
2705 case REAL_CST:
2706 case STRING_CST:
2707 return copy_node (exp);
2709 case COMPLEX_CST:
2710 return build_complex (TREE_TYPE (exp),
2711 copy_constant (TREE_REALPART (exp)),
2712 copy_constant (TREE_IMAGPART (exp)));
2714 case PLUS_EXPR:
2715 case MINUS_EXPR:
2716 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2717 copy_constant (TREE_OPERAND (exp, 0)),
2718 copy_constant (TREE_OPERAND (exp, 1)));
2720 case NOP_EXPR:
2721 case CONVERT_EXPR:
2722 case NON_LVALUE_EXPR:
2723 case VIEW_CONVERT_EXPR:
2724 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2725 copy_constant (TREE_OPERAND (exp, 0)));
2727 case CONSTRUCTOR:
2729 tree copy = copy_node (exp);
2730 VEC(constructor_elt, gc) *v;
2731 unsigned HOST_WIDE_INT idx;
2732 tree purpose, value;
2734 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2735 CONSTRUCTOR_ELTS (exp)));
2736 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2738 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2739 ce->index = purpose;
2740 ce->value = copy_constant (value);
2742 CONSTRUCTOR_ELTS (copy) = v;
2743 return copy;
2746 default:
2748 tree t = lang_hooks.expand_constant (exp);
2750 gcc_assert (t != exp);
2751 return copy_constant (t);
2756 /* Return the alignment of constant EXP in bits. */
2758 static unsigned int
2759 get_constant_alignment (tree exp)
2761 unsigned int align;
2763 align = TYPE_ALIGN (TREE_TYPE (exp));
2764 #ifdef CONSTANT_ALIGNMENT
2765 align = CONSTANT_ALIGNMENT (exp, align);
2766 #endif
2767 return align;
2770 /* Return the section into which constant EXP should be placed. */
2772 static section *
2773 get_constant_section (tree exp)
2775 if (IN_NAMED_SECTION (exp))
2776 return get_named_section (exp, NULL, compute_reloc_for_constant (exp));
2777 else
2778 return targetm.asm_out.select_section (exp,
2779 compute_reloc_for_constant (exp),
2780 get_constant_alignment (exp));
2783 /* Return the size of constant EXP in bytes. */
2785 static HOST_WIDE_INT
2786 get_constant_size (tree exp)
2788 HOST_WIDE_INT size;
2790 size = int_size_in_bytes (TREE_TYPE (exp));
2791 if (TREE_CODE (exp) == STRING_CST)
2792 size = MAX (TREE_STRING_LENGTH (exp), size);
2793 return size;
2796 /* Subroutine of output_constant_def:
2797 No constant equal to EXP is known to have been output.
2798 Make a constant descriptor to enter EXP in the hash table.
2799 Assign the label number and construct RTL to refer to the
2800 constant's location in memory.
2801 Caller is responsible for updating the hash table. */
2803 static struct constant_descriptor_tree *
2804 build_constant_desc (tree exp)
2806 rtx symbol;
2807 rtx rtl;
2808 char label[256];
2809 int labelno;
2810 struct constant_descriptor_tree *desc;
2812 desc = ggc_alloc (sizeof (*desc));
2813 desc->value = copy_constant (exp);
2815 /* Propagate marked-ness to copied constant. */
2816 if (flag_mudflap && mf_marked_p (exp))
2817 mf_mark (desc->value);
2819 /* Create a string containing the label name, in LABEL. */
2820 labelno = const_labelno++;
2821 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2823 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
2824 if (use_object_blocks_p ())
2826 section *sect = get_constant_section (exp);
2827 symbol = create_block_symbol (ggc_strdup (label),
2828 get_block_for_section (sect), -1);
2830 else
2831 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2832 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
2833 SET_SYMBOL_REF_DECL (symbol, desc->value);
2834 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2836 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2837 set_mem_attributes (rtl, exp, 1);
2838 set_mem_alias_set (rtl, 0);
2839 set_mem_alias_set (rtl, const_alias_set);
2841 /* Set flags or add text to the name to record information, such as
2842 that it is a local symbol. If the name is changed, the macro
2843 ASM_OUTPUT_LABELREF will have to know how to strip this
2844 information. This call might invalidate our local variable
2845 SYMBOL; we can't use it afterward. */
2847 targetm.encode_section_info (exp, rtl, true);
2849 desc->rtl = rtl;
2851 return desc;
2854 /* Return an rtx representing a reference to constant data in memory
2855 for the constant expression EXP.
2857 If assembler code for such a constant has already been output,
2858 return an rtx to refer to it.
2859 Otherwise, output such a constant in memory
2860 and generate an rtx for it.
2862 If DEFER is nonzero, this constant can be deferred and output only
2863 if referenced in the function after all optimizations.
2865 `const_desc_table' records which constants already have label strings. */
2868 output_constant_def (tree exp, int defer)
2870 struct constant_descriptor_tree *desc;
2871 struct constant_descriptor_tree key;
2872 void **loc;
2874 /* Look up EXP in the table of constant descriptors. If we didn't find
2875 it, create a new one. */
2876 key.value = exp;
2877 key.hash = const_hash_1 (exp);
2878 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
2880 desc = *loc;
2881 if (desc == 0)
2883 desc = build_constant_desc (exp);
2884 desc->hash = key.hash;
2885 *loc = desc;
2888 maybe_output_constant_def_contents (desc, defer);
2889 return desc->rtl;
2892 /* Subroutine of output_constant_def: Decide whether or not we need to
2893 output the constant DESC now, and if so, do it. */
2894 static void
2895 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2896 int defer)
2898 rtx symbol = XEXP (desc->rtl, 0);
2899 tree exp = desc->value;
2901 if (flag_syntax_only)
2902 return;
2904 if (TREE_ASM_WRITTEN (exp))
2905 /* Already output; don't do it again. */
2906 return;
2908 /* We can always defer constants as long as the context allows
2909 doing so. */
2910 if (defer)
2912 /* Increment n_deferred_constants if it exists. It needs to be at
2913 least as large as the number of constants actually referred to
2914 by the function. If it's too small we'll stop looking too early
2915 and fail to emit constants; if it's too large we'll only look
2916 through the entire function when we could have stopped earlier. */
2917 if (cfun)
2918 n_deferred_constants++;
2919 return;
2922 output_constant_def_contents (symbol);
2925 /* Subroutine of output_constant_def_contents. Output the definition
2926 of constant EXP, which is pointed to by label LABEL. ALIGN is the
2927 constant's alignment in bits. */
2929 static void
2930 assemble_constant_contents (tree exp, const char *label, unsigned int align)
2932 HOST_WIDE_INT size;
2934 size = get_constant_size (exp);
2936 /* Do any machine/system dependent processing of the constant. */
2937 #ifdef ASM_DECLARE_CONSTANT_NAME
2938 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2939 #else
2940 /* Standard thing is just output label for the constant. */
2941 ASM_OUTPUT_LABEL (asm_out_file, label);
2942 #endif /* ASM_DECLARE_CONSTANT_NAME */
2944 /* Output the value of EXP. */
2945 output_constant (exp, size, align);
2948 /* We must output the constant data referred to by SYMBOL; do so. */
2950 static void
2951 output_constant_def_contents (rtx symbol)
2953 tree exp = SYMBOL_REF_DECL (symbol);
2954 unsigned int align;
2956 /* Make sure any other constants whose addresses appear in EXP
2957 are assigned label numbers. */
2958 output_addressed_constants (exp);
2960 /* We are no longer deferring this constant. */
2961 TREE_ASM_WRITTEN (exp) = 1;
2963 /* If the constant is part of an object block, make sure that the
2964 decl has been positioned within its block, but do not write out
2965 its definition yet. output_object_blocks will do that later. */
2966 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2967 place_block_symbol (symbol);
2968 else
2970 switch_to_section (get_constant_section (exp));
2971 align = get_constant_alignment (exp);
2972 if (align > BITS_PER_UNIT)
2973 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2974 assemble_constant_contents (exp, XSTR (symbol, 0), align);
2976 if (flag_mudflap)
2977 mudflap_enqueue_constant (exp);
2980 /* Look up EXP in the table of constant descriptors. Return the rtl
2981 if it has been emitted, else null. */
2984 lookup_constant_def (tree exp)
2986 struct constant_descriptor_tree *desc;
2987 struct constant_descriptor_tree key;
2989 key.value = exp;
2990 key.hash = const_hash_1 (exp);
2991 desc = htab_find_with_hash (const_desc_htab, &key, key.hash);
2993 return (desc ? desc->rtl : NULL_RTX);
2996 /* Used in the hash tables to avoid outputting the same constant
2997 twice. Unlike 'struct constant_descriptor_tree', RTX constants
2998 are output once per function, not once per file. */
2999 /* ??? Only a few targets need per-function constant pools. Most
3000 can use one per-file pool. Should add a targetm bit to tell the
3001 difference. */
3003 struct rtx_constant_pool GTY(())
3005 /* Pointers to first and last constant in pool, as ordered by offset. */
3006 struct constant_descriptor_rtx *first;
3007 struct constant_descriptor_rtx *last;
3009 /* Hash facility for making memory-constants from constant rtl-expressions.
3010 It is used on RISC machines where immediate integer arguments and
3011 constant addresses are restricted so that such constants must be stored
3012 in memory. */
3013 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
3015 /* Current offset in constant pool (does not include any
3016 machine-specific header). */
3017 HOST_WIDE_INT offset;
3020 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
3022 struct constant_descriptor_rtx *next;
3023 rtx mem;
3024 rtx sym;
3025 rtx constant;
3026 HOST_WIDE_INT offset;
3027 hashval_t hash;
3028 enum machine_mode mode;
3029 unsigned int align;
3030 int labelno;
3031 int mark;
3034 /* Hash and compare functions for const_rtx_htab. */
3036 static hashval_t
3037 const_desc_rtx_hash (const void *ptr)
3039 const struct constant_descriptor_rtx *desc = ptr;
3040 return desc->hash;
3043 static int
3044 const_desc_rtx_eq (const void *a, const void *b)
3046 const struct constant_descriptor_rtx *x = a;
3047 const struct constant_descriptor_rtx *y = b;
3049 if (x->mode != y->mode)
3050 return 0;
3051 return rtx_equal_p (x->constant, y->constant);
3054 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
3056 static int
3057 const_rtx_hash_1 (rtx *xp, void *data)
3059 unsigned HOST_WIDE_INT hwi;
3060 enum machine_mode mode;
3061 enum rtx_code code;
3062 hashval_t h, *hp;
3063 rtx x;
3065 x = *xp;
3066 code = GET_CODE (x);
3067 mode = GET_MODE (x);
3068 h = (hashval_t) code * 1048573 + mode;
3070 switch (code)
3072 case CONST_INT:
3073 hwi = INTVAL (x);
3074 fold_hwi:
3076 const int shift = sizeof (hashval_t) * CHAR_BIT;
3077 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3078 int i;
3080 h ^= (hashval_t) hwi;
3081 for (i = 1; i < n; ++i)
3083 hwi >>= shift;
3084 h ^= (hashval_t) hwi;
3087 break;
3089 case CONST_DOUBLE:
3090 if (mode == VOIDmode)
3092 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3093 goto fold_hwi;
3095 else
3096 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3097 break;
3099 case CONST_VECTOR:
3101 int i;
3102 for (i = XVECLEN (x, 0); i-- > 0; )
3103 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3105 break;
3107 case SYMBOL_REF:
3108 h ^= htab_hash_string (XSTR (x, 0));
3109 break;
3111 case LABEL_REF:
3112 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3113 break;
3115 case UNSPEC:
3116 case UNSPEC_VOLATILE:
3117 h = h * 251 + XINT (x, 1);
3118 break;
3120 default:
3121 break;
3124 hp = data;
3125 *hp = *hp * 509 + h;
3126 return 0;
3129 /* Compute a hash value for X, which should be a constant. */
3131 static hashval_t
3132 const_rtx_hash (rtx x)
3134 hashval_t h = 0;
3135 for_each_rtx (&x, const_rtx_hash_1, &h);
3136 return h;
3140 /* Create and return a new rtx constant pool. */
3142 static struct rtx_constant_pool *
3143 create_constant_pool (void)
3145 struct rtx_constant_pool *pool;
3147 pool = ggc_alloc (sizeof (struct rtx_constant_pool));
3148 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3149 const_desc_rtx_eq, NULL);
3150 pool->first = NULL;
3151 pool->last = NULL;
3152 pool->offset = 0;
3153 return pool;
3156 /* Initialize constant pool hashing for a new function. */
3158 void
3159 init_varasm_status (struct function *f)
3161 struct varasm_status *p;
3163 p = ggc_alloc (sizeof (struct varasm_status));
3164 f->varasm = p;
3166 p->pool = create_constant_pool ();
3167 p->deferred_constants = 0;
3170 /* Given a MINUS expression, simplify it if both sides
3171 include the same symbol. */
3174 simplify_subtraction (rtx x)
3176 rtx r = simplify_rtx (x);
3177 return r ? r : x;
3180 /* Given a constant rtx X, make (or find) a memory constant for its value
3181 and return a MEM rtx to refer to it in memory. */
3184 force_const_mem (enum machine_mode mode, rtx x)
3186 struct constant_descriptor_rtx *desc, tmp;
3187 struct rtx_constant_pool *pool;
3188 char label[256];
3189 rtx def, symbol;
3190 hashval_t hash;
3191 unsigned int align;
3192 void **slot;
3194 /* If we're not allowed to drop X into the constant pool, don't. */
3195 if (targetm.cannot_force_const_mem (x))
3196 return NULL_RTX;
3198 /* Record that this function has used a constant pool entry. */
3199 current_function_uses_const_pool = 1;
3201 /* Decide which pool to use. */
3202 pool = (targetm.use_blocks_for_constant_p (mode, x)
3203 ? shared_constant_pool
3204 : cfun->varasm->pool);
3206 /* Lookup the value in the hashtable. */
3207 tmp.constant = x;
3208 tmp.mode = mode;
3209 hash = const_rtx_hash (x);
3210 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3211 desc = *slot;
3213 /* If the constant was already present, return its memory. */
3214 if (desc)
3215 return copy_rtx (desc->mem);
3217 /* Otherwise, create a new descriptor. */
3218 desc = ggc_alloc (sizeof (*desc));
3219 *slot = desc;
3221 /* Align the location counter as required by EXP's data type. */
3222 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3223 #ifdef CONSTANT_ALIGNMENT
3225 tree type = lang_hooks.types.type_for_mode (mode, 0);
3226 if (type != NULL_TREE)
3227 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3229 #endif
3231 pool->offset += (align / BITS_PER_UNIT) - 1;
3232 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3234 desc->next = NULL;
3235 desc->constant = tmp.constant;
3236 desc->offset = pool->offset;
3237 desc->hash = hash;
3238 desc->mode = mode;
3239 desc->align = align;
3240 desc->labelno = const_labelno;
3241 desc->mark = 0;
3243 pool->offset += GET_MODE_SIZE (mode);
3244 if (pool->last)
3245 pool->last->next = desc;
3246 else
3247 pool->first = pool->last = desc;
3248 pool->last = desc;
3250 /* Create a string containing the label name, in LABEL. */
3251 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3252 ++const_labelno;
3254 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3255 the constants pool. */
3256 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3258 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3259 symbol = create_block_symbol (ggc_strdup (label),
3260 get_block_for_section (sect), -1);
3262 else
3263 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3264 desc->sym = symbol;
3265 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3266 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3267 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3269 /* Construct the MEM. */
3270 desc->mem = def = gen_const_mem (mode, symbol);
3271 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3272 set_mem_align (def, align);
3274 /* If we're dropping a label to the constant pool, make sure we
3275 don't delete it. */
3276 if (GET_CODE (x) == LABEL_REF)
3277 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3279 return copy_rtx (def);
3282 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3285 get_pool_constant (rtx addr)
3287 return SYMBOL_REF_CONSTANT (addr)->constant;
3290 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3291 and whether it has been output or not. */
3294 get_pool_constant_mark (rtx addr, bool *pmarked)
3296 struct constant_descriptor_rtx *desc;
3298 desc = SYMBOL_REF_CONSTANT (addr);
3299 *pmarked = (desc->mark != 0);
3300 return desc->constant;
3303 /* Similar, return the mode. */
3305 enum machine_mode
3306 get_pool_mode (rtx addr)
3308 return SYMBOL_REF_CONSTANT (addr)->mode;
3311 /* Return the size of the constant pool. */
3314 get_pool_size (void)
3316 return cfun->varasm->pool->offset;
3319 /* Worker function for output_constant_pool_1. Emit assembly for X
3320 in MODE with known alignment ALIGN. */
3322 static void
3323 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3325 switch (GET_MODE_CLASS (mode))
3327 case MODE_FLOAT:
3328 case MODE_DECIMAL_FLOAT:
3330 REAL_VALUE_TYPE r;
3332 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3333 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3334 assemble_real (r, mode, align);
3335 break;
3338 case MODE_INT:
3339 case MODE_PARTIAL_INT:
3340 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3341 break;
3343 case MODE_VECTOR_FLOAT:
3344 case MODE_VECTOR_INT:
3346 int i, units;
3347 enum machine_mode submode = GET_MODE_INNER (mode);
3348 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3350 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3351 units = CONST_VECTOR_NUNITS (x);
3353 for (i = 0; i < units; i++)
3355 rtx elt = CONST_VECTOR_ELT (x, i);
3356 output_constant_pool_2 (submode, elt, i ? subalign : align);
3359 break;
3361 default:
3362 gcc_unreachable ();
3366 /* Worker function for output_constant_pool. Emit constant DESC,
3367 giving it ALIGN bits of alignment. */
3369 static void
3370 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3371 unsigned int align)
3373 rtx x, tmp;
3375 x = desc->constant;
3377 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3378 whose CODE_LABEL has been deleted. This can occur if a jump table
3379 is eliminated by optimization. If so, write a constant of zero
3380 instead. Note that this can also happen by turning the
3381 CODE_LABEL into a NOTE. */
3382 /* ??? This seems completely and utterly wrong. Certainly it's
3383 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3384 functioning even with INSN_DELETED_P and friends. */
3386 tmp = x;
3387 switch (GET_CODE (x))
3389 case CONST:
3390 if (GET_CODE (XEXP (x, 0)) != PLUS
3391 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3392 break;
3393 tmp = XEXP (XEXP (x, 0), 0);
3394 /* FALLTHRU */
3396 case LABEL_REF:
3397 tmp = XEXP (x, 0);
3398 gcc_assert (!INSN_DELETED_P (tmp));
3399 gcc_assert (!NOTE_P (tmp)
3400 || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3401 break;
3403 default:
3404 break;
3407 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3408 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3409 align, desc->labelno, done);
3410 #endif
3412 assemble_align (align);
3414 /* Output the label. */
3415 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3417 /* Output the data. */
3418 output_constant_pool_2 (desc->mode, x, align);
3420 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3421 sections have proper size. */
3422 if (align > GET_MODE_BITSIZE (desc->mode)
3423 && in_section
3424 && (in_section->common.flags & SECTION_MERGE))
3425 assemble_align (align);
3427 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3428 done:
3429 #endif
3430 return;
3433 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3434 to as used. Emit referenced deferred strings. This function can
3435 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3437 static int
3438 mark_constant (rtx *current_rtx, void *data ATTRIBUTE_UNUSED)
3440 rtx x = *current_rtx;
3442 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3443 return 0;
3445 if (CONSTANT_POOL_ADDRESS_P (x))
3447 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3448 if (desc->mark == 0)
3450 desc->mark = 1;
3451 for_each_rtx (&desc->constant, mark_constant, NULL);
3454 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3456 tree exp = SYMBOL_REF_DECL (x);
3457 if (!TREE_ASM_WRITTEN (exp))
3459 n_deferred_constants--;
3460 output_constant_def_contents (x);
3464 return -1;
3467 /* Look through appropriate parts of INSN, marking all entries in the
3468 constant pool which are actually being used. Entries that are only
3469 referenced by other constants are also marked as used. Emit
3470 deferred strings that are used. */
3472 static void
3473 mark_constants (rtx insn)
3475 if (!INSN_P (insn))
3476 return;
3478 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3479 insns, not any notes that may be attached. We don't want to mark
3480 a constant just because it happens to appear in a REG_EQUIV note. */
3481 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3483 rtx seq = PATTERN (insn);
3484 int i, n = XVECLEN (seq, 0);
3485 for (i = 0; i < n; ++i)
3487 rtx subinsn = XVECEXP (seq, 0, i);
3488 if (INSN_P (subinsn))
3489 for_each_rtx (&PATTERN (subinsn), mark_constant, NULL);
3492 else
3493 for_each_rtx (&PATTERN (insn), mark_constant, NULL);
3496 /* Look through the instructions for this function, and mark all the
3497 entries in POOL which are actually being used. Emit deferred constants
3498 which have indeed been used. */
3500 static void
3501 mark_constant_pool (void)
3503 rtx insn, link;
3505 if (!current_function_uses_const_pool && n_deferred_constants == 0)
3506 return;
3508 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3509 mark_constants (insn);
3511 for (link = current_function_epilogue_delay_list;
3512 link;
3513 link = XEXP (link, 1))
3514 mark_constants (XEXP (link, 0));
3517 /* Write all the constants in POOL. */
3519 static void
3520 output_constant_pool_contents (struct rtx_constant_pool *pool)
3522 struct constant_descriptor_rtx *desc;
3524 for (desc = pool->first; desc ; desc = desc->next)
3525 if (desc->mark)
3527 /* If the constant is part of an object_block, make sure that
3528 the constant has been positioned within its block, but do not
3529 write out its definition yet. output_object_blocks will do
3530 that later. */
3531 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
3532 && SYMBOL_REF_BLOCK (desc->sym))
3533 place_block_symbol (desc->sym);
3534 else
3536 switch_to_section (targetm.asm_out.select_rtx_section
3537 (desc->mode, desc->constant, desc->align));
3538 output_constant_pool_1 (desc, desc->align);
3543 /* Mark all constants that are used in the current function, then write
3544 out the function's private constant pool. */
3546 static void
3547 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3548 tree fndecl ATTRIBUTE_UNUSED)
3550 struct rtx_constant_pool *pool = cfun->varasm->pool;
3552 /* It is possible for gcc to call force_const_mem and then to later
3553 discard the instructions which refer to the constant. In such a
3554 case we do not need to output the constant. */
3555 mark_constant_pool ();
3557 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3558 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3559 #endif
3561 output_constant_pool_contents (pool);
3563 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3564 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3565 #endif
3568 /* Write the contents of the shared constant pool. */
3570 void
3571 output_shared_constant_pool (void)
3573 output_constant_pool_contents (shared_constant_pool);
3576 /* Determine what kind of relocations EXP may need. */
3579 compute_reloc_for_constant (tree exp)
3581 int reloc = 0, reloc2;
3582 tree tem;
3584 /* Give the front-end a chance to convert VALUE to something that
3585 looks more like a constant to the back-end. */
3586 exp = lang_hooks.expand_constant (exp);
3588 switch (TREE_CODE (exp))
3590 case ADDR_EXPR:
3591 case FDESC_EXPR:
3592 /* Go inside any operations that get_inner_reference can handle and see
3593 if what's inside is a constant: no need to do anything here for
3594 addresses of variables or functions. */
3595 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3596 tem = TREE_OPERAND (tem, 0))
3599 if (TREE_PUBLIC (tem))
3600 reloc |= 2;
3601 else
3602 reloc |= 1;
3603 break;
3605 case PLUS_EXPR:
3606 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3607 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3608 break;
3610 case MINUS_EXPR:
3611 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3612 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3613 /* The difference of two local labels is computable at link time. */
3614 if (reloc == 1 && reloc2 == 1)
3615 reloc = 0;
3616 else
3617 reloc |= reloc2;
3618 break;
3620 case NOP_EXPR:
3621 case CONVERT_EXPR:
3622 case NON_LVALUE_EXPR:
3623 case VIEW_CONVERT_EXPR:
3624 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3625 break;
3627 case CONSTRUCTOR:
3629 unsigned HOST_WIDE_INT idx;
3630 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3631 if (tem != 0)
3632 reloc |= compute_reloc_for_constant (tem);
3634 break;
3636 default:
3637 break;
3639 return reloc;
3642 /* Find all the constants whose addresses are referenced inside of EXP,
3643 and make sure assembler code with a label has been output for each one.
3644 Indicate whether an ADDR_EXPR has been encountered. */
3646 static void
3647 output_addressed_constants (tree exp)
3649 tree tem;
3651 /* Give the front-end a chance to convert VALUE to something that
3652 looks more like a constant to the back-end. */
3653 exp = lang_hooks.expand_constant (exp);
3655 switch (TREE_CODE (exp))
3657 case ADDR_EXPR:
3658 case FDESC_EXPR:
3659 /* Go inside any operations that get_inner_reference can handle and see
3660 if what's inside is a constant: no need to do anything here for
3661 addresses of variables or functions. */
3662 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3663 tem = TREE_OPERAND (tem, 0))
3666 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3667 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3668 tem = DECL_INITIAL (tem);
3670 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3671 output_constant_def (tem, 0);
3672 break;
3674 case PLUS_EXPR:
3675 case MINUS_EXPR:
3676 output_addressed_constants (TREE_OPERAND (exp, 1));
3677 /* Fall through. */
3679 case NOP_EXPR:
3680 case CONVERT_EXPR:
3681 case NON_LVALUE_EXPR:
3682 case VIEW_CONVERT_EXPR:
3683 output_addressed_constants (TREE_OPERAND (exp, 0));
3684 break;
3686 case CONSTRUCTOR:
3688 unsigned HOST_WIDE_INT idx;
3689 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3690 if (tem != 0)
3691 output_addressed_constants (tem);
3693 break;
3695 default:
3696 break;
3700 /* Whether a constructor CTOR is a valid static constant initializer if all
3701 its elements are. This used to be internal to initializer_constant_valid_p
3702 and has been exposed to let other functions like categorize_ctor_elements
3703 evaluate the property while walking a constructor for other purposes. */
3705 bool
3706 constructor_static_from_elts_p (tree ctor)
3708 return (TREE_CONSTANT (ctor)
3709 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
3710 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE)
3711 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (ctor)));
3714 /* Return nonzero if VALUE is a valid constant-valued expression
3715 for use in initializing a static variable; one that can be an
3716 element of a "constant" initializer.
3718 Return null_pointer_node if the value is absolute;
3719 if it is relocatable, return the variable that determines the relocation.
3720 We assume that VALUE has been folded as much as possible;
3721 therefore, we do not need to check for such things as
3722 arithmetic-combinations of integers. */
3724 tree
3725 initializer_constant_valid_p (tree value, tree endtype)
3727 /* Give the front-end a chance to convert VALUE to something that
3728 looks more like a constant to the back-end. */
3729 value = lang_hooks.expand_constant (value);
3731 switch (TREE_CODE (value))
3733 case CONSTRUCTOR:
3734 if (constructor_static_from_elts_p (value))
3736 unsigned HOST_WIDE_INT idx;
3737 tree elt;
3738 bool absolute = true;
3740 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
3742 tree reloc;
3743 reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
3744 if (!reloc)
3745 return NULL_TREE;
3746 if (reloc != null_pointer_node)
3747 absolute = false;
3749 /* For a non-absolute relocation, there is no single
3750 variable that can be "the variable that determines the
3751 relocation." */
3752 return absolute ? null_pointer_node : error_mark_node;
3755 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3757 case INTEGER_CST:
3758 case VECTOR_CST:
3759 case REAL_CST:
3760 case STRING_CST:
3761 case COMPLEX_CST:
3762 return null_pointer_node;
3764 case ADDR_EXPR:
3765 case FDESC_EXPR:
3766 value = staticp (TREE_OPERAND (value, 0));
3767 if (value)
3769 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out to
3770 be a constant, this is old-skool offsetof-like nonsense. */
3771 if (TREE_CODE (value) == INDIRECT_REF
3772 && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3773 return null_pointer_node;
3774 /* Taking the address of a nested function involves a trampoline. */
3775 if (TREE_CODE (value) == FUNCTION_DECL
3776 && ((decl_function_context (value)
3777 && !DECL_NO_STATIC_CHAIN (value))
3778 || DECL_DLLIMPORT_P (value)))
3779 return NULL_TREE;
3780 /* "&{...}" requires a temporary to hold the constructed
3781 object. */
3782 if (TREE_CODE (value) == CONSTRUCTOR)
3783 return NULL_TREE;
3785 return value;
3787 case VIEW_CONVERT_EXPR:
3788 case NON_LVALUE_EXPR:
3789 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3791 case CONVERT_EXPR:
3792 case NOP_EXPR:
3794 tree src;
3795 tree src_type;
3796 tree dest_type;
3798 src = TREE_OPERAND (value, 0);
3799 src_type = TREE_TYPE (src);
3800 dest_type = TREE_TYPE (value);
3802 /* Allow conversions between pointer types, floating-point
3803 types, and offset types. */
3804 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3805 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3806 || (TREE_CODE (dest_type) == OFFSET_TYPE
3807 && TREE_CODE (src_type) == OFFSET_TYPE))
3808 return initializer_constant_valid_p (src, endtype);
3810 /* Allow length-preserving conversions between integer types. */
3811 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3812 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3813 return initializer_constant_valid_p (src, endtype);
3815 /* Allow conversions between other integer types only if
3816 explicit value. */
3817 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3819 tree inner = initializer_constant_valid_p (src, endtype);
3820 if (inner == null_pointer_node)
3821 return null_pointer_node;
3822 break;
3825 /* Allow (int) &foo provided int is as wide as a pointer. */
3826 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3827 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3828 return initializer_constant_valid_p (src, endtype);
3830 /* Likewise conversions from int to pointers, but also allow
3831 conversions from 0. */
3832 if ((POINTER_TYPE_P (dest_type)
3833 || TREE_CODE (dest_type) == OFFSET_TYPE)
3834 && INTEGRAL_TYPE_P (src_type))
3836 if (TREE_CODE (src) == INTEGER_CST
3837 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
3838 return null_pointer_node;
3839 if (integer_zerop (src))
3840 return null_pointer_node;
3841 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3842 return initializer_constant_valid_p (src, endtype);
3845 /* Allow conversions to struct or union types if the value
3846 inside is okay. */
3847 if (TREE_CODE (dest_type) == RECORD_TYPE
3848 || TREE_CODE (dest_type) == UNION_TYPE)
3849 return initializer_constant_valid_p (src, endtype);
3851 break;
3853 case PLUS_EXPR:
3854 if (! INTEGRAL_TYPE_P (endtype)
3855 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3857 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3858 endtype);
3859 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3860 endtype);
3861 /* If either term is absolute, use the other terms relocation. */
3862 if (valid0 == null_pointer_node)
3863 return valid1;
3864 if (valid1 == null_pointer_node)
3865 return valid0;
3867 break;
3869 case MINUS_EXPR:
3870 if (! INTEGRAL_TYPE_P (endtype)
3871 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3873 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3874 endtype);
3875 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3876 endtype);
3877 /* Win if second argument is absolute. */
3878 if (valid1 == null_pointer_node)
3879 return valid0;
3880 /* Win if both arguments have the same relocation.
3881 Then the value is absolute. */
3882 if (valid0 == valid1 && valid0 != 0)
3883 return null_pointer_node;
3885 /* Since GCC guarantees that string constants are unique in the
3886 generated code, a subtraction between two copies of the same
3887 constant string is absolute. */
3888 if (valid0 && TREE_CODE (valid0) == STRING_CST
3889 && valid1 && TREE_CODE (valid1) == STRING_CST
3890 && operand_equal_p (valid0, valid1, 1))
3891 return null_pointer_node;
3894 /* Support narrowing differences. */
3895 if (INTEGRAL_TYPE_P (endtype))
3897 tree op0, op1;
3899 op0 = TREE_OPERAND (value, 0);
3900 op1 = TREE_OPERAND (value, 1);
3902 /* Like STRIP_NOPS except allow the operand mode to widen.
3903 This works around a feature of fold that simplifies
3904 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3905 that the narrower operation is cheaper. */
3907 while (TREE_CODE (op0) == NOP_EXPR
3908 || TREE_CODE (op0) == CONVERT_EXPR
3909 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3911 tree inner = TREE_OPERAND (op0, 0);
3912 if (inner == error_mark_node
3913 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3914 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3915 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3916 break;
3917 op0 = inner;
3920 while (TREE_CODE (op1) == NOP_EXPR
3921 || TREE_CODE (op1) == CONVERT_EXPR
3922 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3924 tree inner = TREE_OPERAND (op1, 0);
3925 if (inner == error_mark_node
3926 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3927 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3928 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3929 break;
3930 op1 = inner;
3933 op0 = initializer_constant_valid_p (op0, endtype);
3934 op1 = initializer_constant_valid_p (op1, endtype);
3936 /* Both initializers must be known. */
3937 if (op0 && op1)
3939 if (op0 == op1)
3940 return null_pointer_node;
3942 /* Support differences between labels. */
3943 if (TREE_CODE (op0) == LABEL_DECL
3944 && TREE_CODE (op1) == LABEL_DECL)
3945 return null_pointer_node;
3947 if (TREE_CODE (op0) == STRING_CST
3948 && TREE_CODE (op1) == STRING_CST
3949 && operand_equal_p (op0, op1, 1))
3950 return null_pointer_node;
3953 break;
3955 default:
3956 break;
3959 return 0;
3962 /* Output assembler code for constant EXP to FILE, with no label.
3963 This includes the pseudo-op such as ".int" or ".byte", and a newline.
3964 Assumes output_addressed_constants has been done on EXP already.
3966 Generate exactly SIZE bytes of assembler data, padding at the end
3967 with zeros if necessary. SIZE must always be specified.
3969 SIZE is important for structure constructors,
3970 since trailing members may have been omitted from the constructor.
3971 It is also important for initialization of arrays from string constants
3972 since the full length of the string constant might not be wanted.
3973 It is also needed for initialization of unions, where the initializer's
3974 type is just one member, and that may not be as long as the union.
3976 There a case in which we would fail to output exactly SIZE bytes:
3977 for a structure constructor that wants to produce more than SIZE bytes.
3978 But such constructors will never be generated for any possible input.
3980 ALIGN is the alignment of the data in bits. */
3982 void
3983 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3985 enum tree_code code;
3986 unsigned HOST_WIDE_INT thissize;
3988 /* Some front-ends use constants other than the standard language-independent
3989 varieties, but which may still be output directly. Give the front-end a
3990 chance to convert EXP to a language-independent representation. */
3991 exp = lang_hooks.expand_constant (exp);
3993 if (size == 0 || flag_syntax_only)
3994 return;
3996 /* See if we're trying to initialize a pointer in a non-default mode
3997 to the address of some declaration somewhere. If the target says
3998 the mode is valid for pointers, assume the target has a way of
3999 resolving it. */
4000 if (TREE_CODE (exp) == NOP_EXPR
4001 && POINTER_TYPE_P (TREE_TYPE (exp))
4002 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
4004 tree saved_type = TREE_TYPE (exp);
4006 /* Peel off any intermediate conversions-to-pointer for valid
4007 pointer modes. */
4008 while (TREE_CODE (exp) == NOP_EXPR
4009 && POINTER_TYPE_P (TREE_TYPE (exp))
4010 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
4011 exp = TREE_OPERAND (exp, 0);
4013 /* If what we're left with is the address of something, we can
4014 convert the address to the final type and output it that
4015 way. */
4016 if (TREE_CODE (exp) == ADDR_EXPR)
4017 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4018 /* Likewise for constant ints. */
4019 else if (TREE_CODE (exp) == INTEGER_CST)
4020 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
4021 TREE_INT_CST_HIGH (exp));
4025 /* Eliminate any conversions since we'll be outputting the underlying
4026 constant. */
4027 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
4028 || TREE_CODE (exp) == NON_LVALUE_EXPR
4029 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4031 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4032 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4034 /* Make sure eliminating the conversion is really a no-op, except with
4035 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4036 union types to allow for Ada unchecked unions. */
4037 if (type_size > op_size
4038 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4039 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4040 /* Keep the conversion. */
4041 break;
4042 else
4043 exp = TREE_OPERAND (exp, 0);
4046 code = TREE_CODE (TREE_TYPE (exp));
4047 thissize = int_size_in_bytes (TREE_TYPE (exp));
4049 /* Give the front end another chance to expand constants. */
4050 exp = lang_hooks.expand_constant (exp);
4052 /* Allow a constructor with no elements for any data type.
4053 This means to fill the space with zeros. */
4054 if (TREE_CODE (exp) == CONSTRUCTOR
4055 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
4057 assemble_zeros (size);
4058 return;
4061 if (TREE_CODE (exp) == FDESC_EXPR)
4063 #ifdef ASM_OUTPUT_FDESC
4064 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
4065 tree decl = TREE_OPERAND (exp, 0);
4066 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4067 #else
4068 gcc_unreachable ();
4069 #endif
4070 return;
4073 /* Now output the underlying data. If we've handling the padding, return.
4074 Otherwise, break and ensure SIZE is the size written. */
4075 switch (code)
4077 case BOOLEAN_TYPE:
4078 case INTEGER_TYPE:
4079 case ENUMERAL_TYPE:
4080 case POINTER_TYPE:
4081 case REFERENCE_TYPE:
4082 case OFFSET_TYPE:
4083 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4084 EXPAND_INITIALIZER),
4085 MIN (size, thissize), align, 0))
4086 error ("initializer for integer value is too complicated");
4087 break;
4089 case REAL_TYPE:
4090 if (TREE_CODE (exp) != REAL_CST)
4091 error ("initializer for floating value is not a floating constant");
4093 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4094 break;
4096 case COMPLEX_TYPE:
4097 output_constant (TREE_REALPART (exp), thissize / 2, align);
4098 output_constant (TREE_IMAGPART (exp), thissize / 2,
4099 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4100 break;
4102 case ARRAY_TYPE:
4103 case VECTOR_TYPE:
4104 switch (TREE_CODE (exp))
4106 case CONSTRUCTOR:
4107 output_constructor (exp, size, align);
4108 return;
4109 case STRING_CST:
4110 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
4111 size);
4112 assemble_string (TREE_STRING_POINTER (exp), thissize);
4113 break;
4115 case VECTOR_CST:
4117 int elt_size;
4118 tree link;
4119 unsigned int nalign;
4120 enum machine_mode inner;
4122 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4123 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4125 elt_size = GET_MODE_SIZE (inner);
4127 link = TREE_VECTOR_CST_ELTS (exp);
4128 output_constant (TREE_VALUE (link), elt_size, align);
4129 thissize = elt_size;
4130 while ((link = TREE_CHAIN (link)) != NULL)
4132 output_constant (TREE_VALUE (link), elt_size, nalign);
4133 thissize += elt_size;
4135 break;
4137 default:
4138 gcc_unreachable ();
4140 break;
4142 case RECORD_TYPE:
4143 case UNION_TYPE:
4144 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4145 output_constructor (exp, size, align);
4146 return;
4148 case ERROR_MARK:
4149 return;
4151 default:
4152 gcc_unreachable ();
4155 if (size > thissize)
4156 assemble_zeros (size - thissize);
4160 /* Subroutine of output_constructor, used for computing the size of
4161 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4162 type with an unspecified upper bound. */
4164 static unsigned HOST_WIDE_INT
4165 array_size_for_constructor (tree val)
4167 tree max_index, i;
4168 unsigned HOST_WIDE_INT cnt;
4169 tree index, value, tmp;
4171 /* This code used to attempt to handle string constants that are not
4172 arrays of single-bytes, but nothing else does, so there's no point in
4173 doing it here. */
4174 if (TREE_CODE (val) == STRING_CST)
4175 return TREE_STRING_LENGTH (val);
4177 max_index = NULL_TREE;
4178 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4180 if (TREE_CODE (index) == RANGE_EXPR)
4181 index = TREE_OPERAND (index, 1);
4182 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4183 max_index = index;
4186 if (max_index == NULL_TREE)
4187 return 0;
4189 /* Compute the total number of array elements. */
4190 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4191 i = size_binop (MINUS_EXPR, fold_convert (sizetype, max_index),
4192 fold_convert (sizetype, tmp));
4193 i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
4195 /* Multiply by the array element unit size to find number of bytes. */
4196 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4198 return tree_low_cst (i, 1);
4201 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4202 Generate at least SIZE bytes, padding if necessary. */
4204 static void
4205 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
4206 unsigned int align)
4208 tree type = TREE_TYPE (exp);
4209 tree field = 0;
4210 tree min_index = 0;
4211 /* Number of bytes output or skipped so far.
4212 In other words, current position within the constructor. */
4213 HOST_WIDE_INT total_bytes = 0;
4214 /* Nonzero means BYTE contains part of a byte, to be output. */
4215 int byte_buffer_in_use = 0;
4216 int byte = 0;
4217 unsigned HOST_WIDE_INT cnt;
4218 constructor_elt *ce;
4220 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
4222 if (TREE_CODE (type) == RECORD_TYPE)
4223 field = TYPE_FIELDS (type);
4225 if (TREE_CODE (type) == ARRAY_TYPE
4226 && TYPE_DOMAIN (type) != 0)
4227 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
4229 /* As LINK goes through the elements of the constant,
4230 FIELD goes through the structure fields, if the constant is a structure.
4231 if the constant is a union, then we override this,
4232 by getting the field from the TREE_LIST element.
4233 But the constant could also be an array. Then FIELD is zero.
4235 There is always a maximum of one element in the chain LINK for unions
4236 (even if the initializer in a source program incorrectly contains
4237 more one). */
4238 for (cnt = 0;
4239 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
4240 cnt++, field = field ? TREE_CHAIN (field) : 0)
4242 tree val = ce->value;
4243 tree index = 0;
4245 /* The element in a union constructor specifies the proper field
4246 or index. */
4247 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
4248 || TREE_CODE (type) == QUAL_UNION_TYPE)
4249 && ce->index != 0)
4250 field = ce->index;
4252 else if (TREE_CODE (type) == ARRAY_TYPE)
4253 index = ce->index;
4255 #ifdef ASM_COMMENT_START
4256 if (field && flag_verbose_asm)
4257 fprintf (asm_out_file, "%s %s:\n",
4258 ASM_COMMENT_START,
4259 DECL_NAME (field)
4260 ? IDENTIFIER_POINTER (DECL_NAME (field))
4261 : "<anonymous>");
4262 #endif
4264 /* Eliminate the marker that makes a cast not be an lvalue. */
4265 if (val != 0)
4266 STRIP_NOPS (val);
4268 if (index && TREE_CODE (index) == RANGE_EXPR)
4270 unsigned HOST_WIDE_INT fieldsize
4271 = int_size_in_bytes (TREE_TYPE (type));
4272 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
4273 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
4274 HOST_WIDE_INT index;
4275 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
4277 for (index = lo_index; index <= hi_index; index++)
4279 /* Output the element's initial value. */
4280 if (val == 0)
4281 assemble_zeros (fieldsize);
4282 else
4283 output_constant (val, fieldsize, align2);
4285 /* Count its size. */
4286 total_bytes += fieldsize;
4289 else if (field == 0 || !DECL_BIT_FIELD (field))
4291 /* An element that is not a bit-field. */
4293 unsigned HOST_WIDE_INT fieldsize;
4294 /* Since this structure is static,
4295 we know the positions are constant. */
4296 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
4297 unsigned int align2;
4299 if (index != 0)
4300 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
4301 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
4303 /* Output any buffered-up bit-fields preceding this element. */
4304 if (byte_buffer_in_use)
4306 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4307 total_bytes++;
4308 byte_buffer_in_use = 0;
4311 /* Advance to offset of this element.
4312 Note no alignment needed in an array, since that is guaranteed
4313 if each element has the proper size. */
4314 if ((field != 0 || index != 0) && pos != total_bytes)
4316 gcc_assert (pos >= total_bytes);
4317 assemble_zeros (pos - total_bytes);
4318 total_bytes = pos;
4321 /* Find the alignment of this element. */
4322 align2 = min_align (align, BITS_PER_UNIT * pos);
4324 /* Determine size this element should occupy. */
4325 if (field)
4327 fieldsize = 0;
4329 /* If this is an array with an unspecified upper bound,
4330 the initializer determines the size. */
4331 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4332 but we cannot do this until the deprecated support for
4333 initializing zero-length array members is removed. */
4334 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4335 && TYPE_DOMAIN (TREE_TYPE (field))
4336 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4338 fieldsize = array_size_for_constructor (val);
4339 /* Given a non-empty initialization, this field had
4340 better be last. */
4341 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4343 else if (DECL_SIZE_UNIT (field))
4345 /* ??? This can't be right. If the decl size overflows
4346 a host integer we will silently emit no data. */
4347 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4348 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4351 else
4352 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4354 /* Output the element's initial value. */
4355 if (val == 0)
4356 assemble_zeros (fieldsize);
4357 else
4358 output_constant (val, fieldsize, align2);
4360 /* Count its size. */
4361 total_bytes += fieldsize;
4363 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4364 error ("invalid initial value for member %qs",
4365 IDENTIFIER_POINTER (DECL_NAME (field)));
4366 else
4368 /* Element that is a bit-field. */
4370 HOST_WIDE_INT next_offset = int_bit_position (field);
4371 HOST_WIDE_INT end_offset
4372 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4374 if (val == 0)
4375 val = integer_zero_node;
4377 /* If this field does not start in this (or, next) byte,
4378 skip some bytes. */
4379 if (next_offset / BITS_PER_UNIT != total_bytes)
4381 /* Output remnant of any bit field in previous bytes. */
4382 if (byte_buffer_in_use)
4384 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4385 total_bytes++;
4386 byte_buffer_in_use = 0;
4389 /* If still not at proper byte, advance to there. */
4390 if (next_offset / BITS_PER_UNIT != total_bytes)
4392 gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
4393 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4394 total_bytes = next_offset / BITS_PER_UNIT;
4398 if (! byte_buffer_in_use)
4399 byte = 0;
4401 /* We must split the element into pieces that fall within
4402 separate bytes, and combine each byte with previous or
4403 following bit-fields. */
4405 /* next_offset is the offset n fbits from the beginning of
4406 the structure to the next bit of this element to be processed.
4407 end_offset is the offset of the first bit past the end of
4408 this element. */
4409 while (next_offset < end_offset)
4411 int this_time;
4412 int shift;
4413 HOST_WIDE_INT value;
4414 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4415 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4417 /* Advance from byte to byte
4418 within this element when necessary. */
4419 while (next_byte != total_bytes)
4421 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4422 total_bytes++;
4423 byte = 0;
4426 /* Number of bits we can process at once
4427 (all part of the same byte). */
4428 this_time = MIN (end_offset - next_offset,
4429 BITS_PER_UNIT - next_bit);
4430 if (BYTES_BIG_ENDIAN)
4432 /* On big-endian machine, take the most significant bits
4433 first (of the bits that are significant)
4434 and put them into bytes from the most significant end. */
4435 shift = end_offset - next_offset - this_time;
4437 /* Don't try to take a bunch of bits that cross
4438 the word boundary in the INTEGER_CST. We can
4439 only select bits from the LOW or HIGH part
4440 not from both. */
4441 if (shift < HOST_BITS_PER_WIDE_INT
4442 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4444 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4445 shift = HOST_BITS_PER_WIDE_INT;
4448 /* Now get the bits from the appropriate constant word. */
4449 if (shift < HOST_BITS_PER_WIDE_INT)
4450 value = TREE_INT_CST_LOW (val);
4451 else
4453 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4454 value = TREE_INT_CST_HIGH (val);
4455 shift -= HOST_BITS_PER_WIDE_INT;
4458 /* Get the result. This works only when:
4459 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4460 byte |= (((value >> shift)
4461 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4462 << (BITS_PER_UNIT - this_time - next_bit));
4464 else
4466 /* On little-endian machines,
4467 take first the least significant bits of the value
4468 and pack them starting at the least significant
4469 bits of the bytes. */
4470 shift = next_offset - int_bit_position (field);
4472 /* Don't try to take a bunch of bits that cross
4473 the word boundary in the INTEGER_CST. We can
4474 only select bits from the LOW or HIGH part
4475 not from both. */
4476 if (shift < HOST_BITS_PER_WIDE_INT
4477 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4478 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4480 /* Now get the bits from the appropriate constant word. */
4481 if (shift < HOST_BITS_PER_WIDE_INT)
4482 value = TREE_INT_CST_LOW (val);
4483 else
4485 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4486 value = TREE_INT_CST_HIGH (val);
4487 shift -= HOST_BITS_PER_WIDE_INT;
4490 /* Get the result. This works only when:
4491 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4492 byte |= (((value >> shift)
4493 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4494 << next_bit);
4497 next_offset += this_time;
4498 byte_buffer_in_use = 1;
4503 if (byte_buffer_in_use)
4505 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4506 total_bytes++;
4509 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4510 assemble_zeros (size - total_bytes);
4513 /* This TREE_LIST contains any weak symbol declarations waiting
4514 to be emitted. */
4515 static GTY(()) tree weak_decls;
4517 /* Mark DECL as weak. */
4519 static void
4520 mark_weak (tree decl)
4522 DECL_WEAK (decl) = 1;
4524 if (DECL_RTL_SET_P (decl)
4525 && MEM_P (DECL_RTL (decl))
4526 && XEXP (DECL_RTL (decl), 0)
4527 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4528 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4531 /* Merge weak status between NEWDECL and OLDDECL. */
4533 void
4534 merge_weak (tree newdecl, tree olddecl)
4536 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4538 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4540 tree *pwd;
4541 /* We put the NEWDECL on the weak_decls list at some point
4542 and OLDDECL as well. Keep just OLDDECL on the list. */
4543 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4544 if (TREE_VALUE (*pwd) == newdecl)
4546 *pwd = TREE_CHAIN (*pwd);
4547 break;
4550 return;
4553 if (DECL_WEAK (newdecl))
4555 tree wd;
4557 /* NEWDECL is weak, but OLDDECL is not. */
4559 /* If we already output the OLDDECL, we're in trouble; we can't
4560 go back and make it weak. This error cannot caught in
4561 declare_weak because the NEWDECL and OLDDECL was not yet
4562 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4563 if (TREE_ASM_WRITTEN (olddecl))
4564 error ("weak declaration of %q+D must precede definition",
4565 newdecl);
4567 /* If we've already generated rtl referencing OLDDECL, we may
4568 have done so in a way that will not function properly with
4569 a weak symbol. */
4570 else if (TREE_USED (olddecl)
4571 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4572 warning (0, "weak declaration of %q+D after first use results "
4573 "in unspecified behavior", newdecl);
4575 if (SUPPORTS_WEAK)
4577 /* We put the NEWDECL on the weak_decls list at some point.
4578 Replace it with the OLDDECL. */
4579 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4580 if (TREE_VALUE (wd) == newdecl)
4582 TREE_VALUE (wd) = olddecl;
4583 break;
4585 /* We may not find the entry on the list. If NEWDECL is a
4586 weak alias, then we will have already called
4587 globalize_decl to remove the entry; in that case, we do
4588 not need to do anything. */
4591 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4592 mark_weak (olddecl);
4594 else
4595 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4596 weak. Just update NEWDECL to indicate that it's weak too. */
4597 mark_weak (newdecl);
4600 /* Declare DECL to be a weak symbol. */
4602 void
4603 declare_weak (tree decl)
4605 if (! TREE_PUBLIC (decl))
4606 error ("weak declaration of %q+D must be public", decl);
4607 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4608 error ("weak declaration of %q+D must precede definition", decl);
4609 else if (SUPPORTS_WEAK)
4611 if (! DECL_WEAK (decl))
4612 weak_decls = tree_cons (NULL, decl, weak_decls);
4614 else
4615 warning (0, "weak declaration of %q+D not supported", decl);
4617 mark_weak (decl);
4620 static void
4621 weak_finish_1 (tree decl)
4623 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4624 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4625 #endif
4627 if (! TREE_USED (decl))
4628 return;
4630 #ifdef ASM_WEAKEN_DECL
4631 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4632 #else
4633 #ifdef ASM_WEAKEN_LABEL
4634 ASM_WEAKEN_LABEL (asm_out_file, name);
4635 #else
4636 #ifdef ASM_OUTPUT_WEAK_ALIAS
4638 static bool warn_once = 0;
4639 if (! warn_once)
4641 warning (0, "only weak aliases are supported in this configuration");
4642 warn_once = 1;
4644 return;
4646 #endif
4647 #endif
4648 #endif
4651 /* This TREE_LIST contains weakref targets. */
4653 static GTY(()) tree weakref_targets;
4655 /* Forward declaration. */
4656 static tree find_decl_and_mark_needed (tree decl, tree target);
4658 /* Emit any pending weak declarations. */
4660 void
4661 weak_finish (void)
4663 tree t;
4665 for (t = weakref_targets; t; t = TREE_CHAIN (t))
4667 tree alias_decl = TREE_PURPOSE (t);
4668 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
4670 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
4671 /* Remove alias_decl from the weak list, but leave entries for
4672 the target alone. */
4673 target = NULL_TREE;
4674 #ifndef ASM_OUTPUT_WEAKREF
4675 else if (! TREE_SYMBOL_REFERENCED (target))
4677 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
4678 defined, otherwise we and weak_finish_1 would use a
4679 different macros. */
4680 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
4681 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
4682 # else
4683 tree decl = find_decl_and_mark_needed (alias_decl, target);
4685 if (! decl)
4687 decl = build_decl (TREE_CODE (alias_decl), target,
4688 TREE_TYPE (alias_decl));
4690 DECL_EXTERNAL (decl) = 1;
4691 TREE_PUBLIC (decl) = 1;
4692 DECL_ARTIFICIAL (decl) = 1;
4693 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
4694 TREE_USED (decl) = 1;
4697 weak_finish_1 (decl);
4698 # endif
4700 #endif
4703 tree *p;
4704 tree t2;
4706 /* Remove the alias and the target from the pending weak list
4707 so that we do not emit any .weak directives for the former,
4708 nor multiple .weak directives for the latter. */
4709 for (p = &weak_decls; (t2 = *p) ; )
4711 if (TREE_VALUE (t2) == alias_decl
4712 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
4713 *p = TREE_CHAIN (t2);
4714 else
4715 p = &TREE_CHAIN (t2);
4718 /* Remove other weakrefs to the same target, to speed things up. */
4719 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
4721 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
4722 *p = TREE_CHAIN (t2);
4723 else
4724 p = &TREE_CHAIN (t2);
4729 for (t = weak_decls; t; t = TREE_CHAIN (t))
4731 tree decl = TREE_VALUE (t);
4733 weak_finish_1 (decl);
4737 /* Emit the assembly bits to indicate that DECL is globally visible. */
4739 static void
4740 globalize_decl (tree decl)
4742 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4744 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4745 if (DECL_WEAK (decl))
4747 tree *p, t;
4749 #ifdef ASM_WEAKEN_DECL
4750 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4751 #else
4752 ASM_WEAKEN_LABEL (asm_out_file, name);
4753 #endif
4755 /* Remove this function from the pending weak list so that
4756 we do not emit multiple .weak directives for it. */
4757 for (p = &weak_decls; (t = *p) ; )
4759 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4760 *p = TREE_CHAIN (t);
4761 else
4762 p = &TREE_CHAIN (t);
4765 /* Remove weakrefs to the same target from the pending weakref
4766 list, for the same reason. */
4767 for (p = &weakref_targets; (t = *p) ; )
4769 if (DECL_ASSEMBLER_NAME (decl)
4770 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4771 *p = TREE_CHAIN (t);
4772 else
4773 p = &TREE_CHAIN (t);
4776 return;
4778 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4779 if (DECL_ONE_ONLY (decl))
4780 ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4781 #endif
4783 targetm.asm_out.globalize_label (asm_out_file, name);
4786 /* We have to be able to tell cgraph about the needed-ness of the target
4787 of an alias. This requires that the decl have been defined. Aliases
4788 that precede their definition have to be queued for later processing. */
4790 typedef struct alias_pair GTY(())
4792 tree decl;
4793 tree target;
4794 } alias_pair;
4796 /* Define gc'd vector type. */
4797 DEF_VEC_O(alias_pair);
4798 DEF_VEC_ALLOC_O(alias_pair,gc);
4800 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
4802 /* Given an assembly name, find the decl it is associated with. At the
4803 same time, mark it needed for cgraph. */
4805 static tree
4806 find_decl_and_mark_needed (tree decl, tree target)
4808 struct cgraph_node *fnode = NULL;
4809 struct varpool_node *vnode = NULL;
4811 if (TREE_CODE (decl) == FUNCTION_DECL)
4813 fnode = cgraph_node_for_asm (target);
4814 if (fnode == NULL)
4815 vnode = varpool_node_for_asm (target);
4817 else
4819 vnode = varpool_node_for_asm (target);
4820 if (vnode == NULL)
4821 fnode = cgraph_node_for_asm (target);
4824 if (fnode)
4826 /* We can't mark function nodes as used after cgraph global info
4827 is finished. This wouldn't generally be necessary, but C++
4828 virtual table thunks are introduced late in the game and
4829 might seem like they need marking, although in fact they
4830 don't. */
4831 if (! cgraph_global_info_ready)
4832 cgraph_mark_needed_node (fnode);
4833 return fnode->decl;
4835 else if (vnode)
4837 varpool_mark_needed_node (vnode);
4838 return vnode->decl;
4840 else
4841 return NULL_TREE;
4844 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4845 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
4846 tree node is DECL to have the value of the tree node TARGET. */
4848 static void
4849 do_assemble_alias (tree decl, tree target)
4851 if (TREE_ASM_WRITTEN (decl))
4852 return;
4854 TREE_ASM_WRITTEN (decl) = 1;
4855 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4857 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4859 ultimate_transparent_alias_target (&target);
4861 if (!TREE_SYMBOL_REFERENCED (target))
4862 weakref_targets = tree_cons (decl, target, weakref_targets);
4864 #ifdef ASM_OUTPUT_WEAKREF
4865 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4866 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4867 IDENTIFIER_POINTER (target));
4868 #else
4869 if (!SUPPORTS_WEAK)
4871 error ("%Jweakref is not supported in this configuration", decl);
4872 return;
4874 #endif
4875 return;
4878 #ifdef ASM_OUTPUT_DEF
4879 /* Make name accessible from other files, if appropriate. */
4881 if (TREE_PUBLIC (decl))
4883 globalize_decl (decl);
4884 maybe_assemble_visibility (decl);
4887 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4888 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4889 # else
4890 ASM_OUTPUT_DEF (asm_out_file,
4891 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4892 IDENTIFIER_POINTER (target));
4893 # endif
4894 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4896 const char *name;
4897 tree *p, t;
4899 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4900 # ifdef ASM_WEAKEN_DECL
4901 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4902 # else
4903 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4904 # endif
4905 /* Remove this function from the pending weak list so that
4906 we do not emit multiple .weak directives for it. */
4907 for (p = &weak_decls; (t = *p) ; )
4908 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4909 *p = TREE_CHAIN (t);
4910 else
4911 p = &TREE_CHAIN (t);
4913 /* Remove weakrefs to the same target from the pending weakref
4914 list, for the same reason. */
4915 for (p = &weakref_targets; (t = *p) ; )
4917 if (DECL_ASSEMBLER_NAME (decl)
4918 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4919 *p = TREE_CHAIN (t);
4920 else
4921 p = &TREE_CHAIN (t);
4924 #endif
4927 /* First pass of completing pending aliases. Make sure that cgraph knows
4928 which symbols will be required. */
4930 void
4931 finish_aliases_1 (void)
4933 unsigned i;
4934 alias_pair *p;
4936 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4938 tree target_decl;
4940 target_decl = find_decl_and_mark_needed (p->decl, p->target);
4941 if (target_decl == NULL)
4943 if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4944 error ("%q+D aliased to undefined symbol %qs",
4945 p->decl, IDENTIFIER_POINTER (p->target));
4947 else if (DECL_EXTERNAL (target_decl)
4948 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4949 error ("%q+D aliased to external symbol %qs",
4950 p->decl, IDENTIFIER_POINTER (p->target));
4954 /* Second pass of completing pending aliases. Emit the actual assembly.
4955 This happens at the end of compilation and thus it is assured that the
4956 target symbol has been emitted. */
4958 void
4959 finish_aliases_2 (void)
4961 unsigned i;
4962 alias_pair *p;
4964 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4965 do_assemble_alias (p->decl, p->target);
4967 VEC_truncate (alias_pair, alias_pairs, 0);
4970 /* Emit an assembler directive to make the symbol for DECL an alias to
4971 the symbol for TARGET. */
4973 void
4974 assemble_alias (tree decl, tree target)
4976 tree target_decl;
4977 bool is_weakref = false;
4979 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4981 tree alias = DECL_ASSEMBLER_NAME (decl);
4983 is_weakref = true;
4985 ultimate_transparent_alias_target (&target);
4987 if (alias == target)
4988 error ("weakref %q+D ultimately targets itself", decl);
4989 else
4991 #ifndef ASM_OUTPUT_WEAKREF
4992 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
4993 TREE_CHAIN (alias) = target;
4994 #endif
4996 if (TREE_PUBLIC (decl))
4997 error ("weakref %q+D must have static linkage", decl);
4999 else
5001 #if !defined (ASM_OUTPUT_DEF)
5002 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5003 error ("%Jalias definitions not supported in this configuration", decl);
5004 return;
5005 # else
5006 if (!DECL_WEAK (decl))
5008 error ("%Jonly weak aliases are supported in this configuration", decl);
5009 return;
5011 # endif
5012 #endif
5015 /* We must force creation of DECL_RTL for debug info generation, even though
5016 we don't use it here. */
5017 make_decl_rtl (decl);
5018 TREE_USED (decl) = 1;
5020 /* A quirk of the initial implementation of aliases required that the user
5021 add "extern" to all of them. Which is silly, but now historical. Do
5022 note that the symbol is in fact locally defined. */
5023 if (! is_weakref)
5024 DECL_EXTERNAL (decl) = 0;
5026 /* Allow aliases to aliases. */
5027 if (TREE_CODE (decl) == FUNCTION_DECL)
5028 cgraph_node (decl)->alias = true;
5029 else
5030 varpool_node (decl)->alias = true;
5032 /* If the target has already been emitted, we don't have to queue the
5033 alias. This saves a tad o memory. */
5034 target_decl = find_decl_and_mark_needed (decl, target);
5035 if (target_decl && TREE_ASM_WRITTEN (target_decl))
5036 do_assemble_alias (decl, target);
5037 else
5039 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
5040 p->decl = decl;
5041 p->target = target;
5045 /* Emit an assembler directive to set symbol for DECL visibility to
5046 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5048 void
5049 default_assemble_visibility (tree decl, int vis)
5051 static const char * const visibility_types[] = {
5052 NULL, "protected", "hidden", "internal"
5055 const char *name, *type;
5057 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5058 type = visibility_types[vis];
5060 #ifdef HAVE_GAS_HIDDEN
5061 fprintf (asm_out_file, "\t.%s\t", type);
5062 assemble_name (asm_out_file, name);
5063 fprintf (asm_out_file, "\n");
5064 #else
5065 warning (OPT_Wattributes, "visibility attribute not supported "
5066 "in this configuration; ignored");
5067 #endif
5070 /* A helper function to call assemble_visibility when needed for a decl. */
5073 maybe_assemble_visibility (tree decl)
5075 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5077 if (vis != VISIBILITY_DEFAULT)
5079 targetm.asm_out.visibility (decl, vis);
5080 return 1;
5082 else
5083 return 0;
5086 /* Returns 1 if the target configuration supports defining public symbols
5087 so that one of them will be chosen at link time instead of generating a
5088 multiply-defined symbol error, whether through the use of weak symbols or
5089 a target-specific mechanism for having duplicates discarded. */
5092 supports_one_only (void)
5094 if (SUPPORTS_ONE_ONLY)
5095 return 1;
5096 return SUPPORTS_WEAK;
5099 /* Set up DECL as a public symbol that can be defined in multiple
5100 translation units without generating a linker error. */
5102 void
5103 make_decl_one_only (tree decl)
5105 gcc_assert (TREE_CODE (decl) == VAR_DECL
5106 || TREE_CODE (decl) == FUNCTION_DECL);
5108 TREE_PUBLIC (decl) = 1;
5110 if (SUPPORTS_ONE_ONLY)
5112 #ifdef MAKE_DECL_ONE_ONLY
5113 MAKE_DECL_ONE_ONLY (decl);
5114 #endif
5115 DECL_ONE_ONLY (decl) = 1;
5117 else if (TREE_CODE (decl) == VAR_DECL
5118 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
5119 DECL_COMMON (decl) = 1;
5120 else
5122 gcc_assert (SUPPORTS_WEAK);
5123 DECL_WEAK (decl) = 1;
5127 void
5128 init_varasm_once (void)
5130 section_htab = htab_create_ggc (31, section_entry_hash,
5131 section_entry_eq, NULL);
5132 object_block_htab = htab_create_ggc (31, object_block_entry_hash,
5133 object_block_entry_eq, NULL);
5134 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
5135 const_desc_eq, NULL);
5137 const_alias_set = new_alias_set ();
5138 shared_constant_pool = create_constant_pool ();
5140 #ifdef TEXT_SECTION_ASM_OP
5141 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5142 TEXT_SECTION_ASM_OP);
5143 #endif
5145 #ifdef DATA_SECTION_ASM_OP
5146 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5147 DATA_SECTION_ASM_OP);
5148 #endif
5150 #ifdef SDATA_SECTION_ASM_OP
5151 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
5152 SDATA_SECTION_ASM_OP);
5153 #endif
5155 #ifdef READONLY_DATA_SECTION_ASM_OP
5156 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
5157 READONLY_DATA_SECTION_ASM_OP);
5158 #endif
5160 #ifdef CTORS_SECTION_ASM_OP
5161 ctors_section = get_unnamed_section (0, output_section_asm_op,
5162 CTORS_SECTION_ASM_OP);
5163 #endif
5165 #ifdef DTORS_SECTION_ASM_OP
5166 dtors_section = get_unnamed_section (0, output_section_asm_op,
5167 DTORS_SECTION_ASM_OP);
5168 #endif
5170 #ifdef BSS_SECTION_ASM_OP
5171 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5172 output_section_asm_op,
5173 BSS_SECTION_ASM_OP);
5174 #endif
5176 #ifdef SBSS_SECTION_ASM_OP
5177 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
5178 output_section_asm_op,
5179 SBSS_SECTION_ASM_OP);
5180 #endif
5182 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5183 | SECTION_COMMON, emit_tls_common);
5184 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5185 | SECTION_COMMON, emit_local);
5186 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
5187 | SECTION_COMMON, emit_common);
5189 #if defined ASM_OUTPUT_ALIGNED_BSS || defined ASM_OUTPUT_BSS
5190 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
5191 emit_bss);
5192 #endif
5194 targetm.asm_out.init_sections ();
5196 if (readonly_data_section == NULL)
5197 readonly_data_section = text_section;
5200 enum tls_model
5201 decl_default_tls_model (tree decl)
5203 enum tls_model kind;
5204 bool is_local;
5206 is_local = targetm.binds_local_p (decl);
5207 if (!flag_shlib)
5209 if (is_local)
5210 kind = TLS_MODEL_LOCAL_EXEC;
5211 else
5212 kind = TLS_MODEL_INITIAL_EXEC;
5215 /* Local dynamic is inefficient when we're not combining the
5216 parts of the address. */
5217 else if (optimize && is_local)
5218 kind = TLS_MODEL_LOCAL_DYNAMIC;
5219 else
5220 kind = TLS_MODEL_GLOBAL_DYNAMIC;
5221 if (kind < flag_tls_default)
5222 kind = flag_tls_default;
5224 return kind;
5227 /* Select a set of attributes for section NAME based on the properties
5228 of DECL and whether or not RELOC indicates that DECL's initializer
5229 might contain runtime relocations.
5231 We make the section read-only and executable for a function decl,
5232 read-only for a const data decl, and writable for a non-const data decl. */
5234 unsigned int
5235 default_section_type_flags (tree decl, const char *name, int reloc)
5237 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
5240 unsigned int
5241 default_section_type_flags_1 (tree decl, const char *name, int reloc,
5242 int shlib)
5244 unsigned int flags;
5246 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
5247 flags = SECTION_CODE;
5248 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
5249 flags = 0;
5250 else if (current_function_decl
5251 && cfun
5252 && cfun->unlikely_text_section_name
5253 && strcmp (name, cfun->unlikely_text_section_name) == 0)
5254 flags = SECTION_CODE;
5255 else if (!decl
5256 && (!current_function_decl || !cfun)
5257 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5258 flags = SECTION_CODE;
5259 else
5260 flags = SECTION_WRITE;
5262 if (decl && DECL_ONE_ONLY (decl))
5263 flags |= SECTION_LINKONCE;
5265 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5266 flags |= SECTION_TLS | SECTION_WRITE;
5268 if (strcmp (name, ".bss") == 0
5269 || strncmp (name, ".bss.", 5) == 0
5270 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
5271 || strcmp (name, ".sbss") == 0
5272 || strncmp (name, ".sbss.", 6) == 0
5273 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
5274 flags |= SECTION_BSS;
5276 if (strcmp (name, ".tdata") == 0
5277 || strncmp (name, ".tdata.", 7) == 0
5278 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
5279 flags |= SECTION_TLS;
5281 if (strcmp (name, ".tbss") == 0
5282 || strncmp (name, ".tbss.", 6) == 0
5283 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
5284 flags |= SECTION_TLS | SECTION_BSS;
5286 /* These three sections have special ELF types. They are neither
5287 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
5288 want to print a section type (@progbits or @nobits). If someone
5289 is silly enough to emit code or TLS variables to one of these
5290 sections, then don't handle them specially. */
5291 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
5292 && (strcmp (name, ".init_array") == 0
5293 || strcmp (name, ".fini_array") == 0
5294 || strcmp (name, ".preinit_array") == 0))
5295 flags |= SECTION_NOTYPE;
5297 return flags;
5300 /* Return true if the target supports some form of global BSS,
5301 either through bss_noswitch_section, or by selecting a BSS
5302 section in TARGET_ASM_SELECT_SECTION. */
5304 bool
5305 have_global_bss_p (void)
5307 return bss_noswitch_section || targetm.have_switchable_bss_sections;
5310 /* Output assembly to switch to section NAME with attribute FLAGS.
5311 Four variants for common object file formats. */
5313 void
5314 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
5315 unsigned int flags ATTRIBUTE_UNUSED,
5316 tree decl ATTRIBUTE_UNUSED)
5318 /* Some object formats don't support named sections at all. The
5319 front-end should already have flagged this as an error. */
5320 gcc_unreachable ();
5323 void
5324 default_elf_asm_named_section (const char *name, unsigned int flags,
5325 tree decl ATTRIBUTE_UNUSED)
5327 char flagchars[10], *f = flagchars;
5329 /* If we have already declared this section, we can use an
5330 abbreviated form to switch back to it -- unless this section is
5331 part of a COMDAT groups, in which case GAS requires the full
5332 declaration every time. */
5333 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5334 && (flags & SECTION_DECLARED))
5336 fprintf (asm_out_file, "\t.section\t%s\n", name);
5337 return;
5340 if (!(flags & SECTION_DEBUG))
5341 *f++ = 'a';
5342 if (flags & SECTION_WRITE)
5343 *f++ = 'w';
5344 if (flags & SECTION_CODE)
5345 *f++ = 'x';
5346 if (flags & SECTION_SMALL)
5347 *f++ = 's';
5348 if (flags & SECTION_MERGE)
5349 *f++ = 'M';
5350 if (flags & SECTION_STRINGS)
5351 *f++ = 'S';
5352 if (flags & SECTION_TLS)
5353 *f++ = 'T';
5354 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5355 *f++ = 'G';
5356 *f = '\0';
5358 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5360 if (!(flags & SECTION_NOTYPE))
5362 const char *type;
5363 const char *format;
5365 if (flags & SECTION_BSS)
5366 type = "nobits";
5367 else
5368 type = "progbits";
5370 format = ",@%s";
5371 #ifdef ASM_COMMENT_START
5372 /* On platforms that use "@" as the assembly comment character,
5373 use "%" instead. */
5374 if (strcmp (ASM_COMMENT_START, "@") == 0)
5375 format = ",%%%s";
5376 #endif
5377 fprintf (asm_out_file, format, type);
5379 if (flags & SECTION_ENTSIZE)
5380 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5381 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5382 fprintf (asm_out_file, ",%s,comdat",
5383 lang_hooks.decls.comdat_group (decl));
5386 putc ('\n', asm_out_file);
5389 void
5390 default_coff_asm_named_section (const char *name, unsigned int flags,
5391 tree decl ATTRIBUTE_UNUSED)
5393 char flagchars[8], *f = flagchars;
5395 if (flags & SECTION_WRITE)
5396 *f++ = 'w';
5397 if (flags & SECTION_CODE)
5398 *f++ = 'x';
5399 *f = '\0';
5401 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5404 void
5405 default_pe_asm_named_section (const char *name, unsigned int flags,
5406 tree decl)
5408 default_coff_asm_named_section (name, flags, decl);
5410 if (flags & SECTION_LINKONCE)
5412 /* Functions may have been compiled at various levels of
5413 optimization so we can't use `same_size' here.
5414 Instead, have the linker pick one. */
5415 fprintf (asm_out_file, "\t.linkonce %s\n",
5416 (flags & SECTION_CODE ? "discard" : "same_size"));
5420 /* The lame default section selector. */
5422 section *
5423 default_select_section (tree decl, int reloc,
5424 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5426 if (DECL_P (decl))
5428 if (decl_readonly_section (decl, reloc))
5429 return readonly_data_section;
5431 else if (TREE_CODE (decl) == CONSTRUCTOR)
5433 if (! ((flag_pic && reloc)
5434 || !TREE_READONLY (decl)
5435 || TREE_SIDE_EFFECTS (decl)
5436 || !TREE_CONSTANT (decl)))
5437 return readonly_data_section;
5439 else if (TREE_CODE (decl) == STRING_CST)
5440 return readonly_data_section;
5441 else if (! (flag_pic && reloc))
5442 return readonly_data_section;
5444 return data_section;
5447 enum section_category
5448 categorize_decl_for_section (tree decl, int reloc, int shlib)
5450 enum section_category ret;
5452 if (TREE_CODE (decl) == FUNCTION_DECL)
5453 return SECCAT_TEXT;
5454 else if (TREE_CODE (decl) == STRING_CST)
5456 if (flag_mudflap) /* or !flag_merge_constants */
5457 return SECCAT_RODATA;
5458 else
5459 return SECCAT_RODATA_MERGE_STR;
5461 else if (TREE_CODE (decl) == VAR_DECL)
5463 if (bss_initializer_p (decl))
5464 ret = SECCAT_BSS;
5465 else if (! TREE_READONLY (decl)
5466 || TREE_SIDE_EFFECTS (decl)
5467 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5469 if (shlib && (reloc & 2))
5470 ret = SECCAT_DATA_REL;
5471 else if (shlib && reloc)
5472 ret = SECCAT_DATA_REL_LOCAL;
5473 else
5474 ret = SECCAT_DATA;
5476 else if (shlib && (reloc & 2))
5477 ret = SECCAT_DATA_REL_RO;
5478 else if (shlib && reloc)
5479 ret = SECCAT_DATA_REL_RO_LOCAL;
5480 else if (reloc || flag_merge_constants < 2)
5481 /* C and C++ don't allow different variables to share the same
5482 location. -fmerge-all-constants allows even that (at the
5483 expense of not conforming). */
5484 ret = SECCAT_RODATA;
5485 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5486 ret = SECCAT_RODATA_MERGE_STR_INIT;
5487 else
5488 ret = SECCAT_RODATA_MERGE_CONST;
5490 else if (TREE_CODE (decl) == CONSTRUCTOR)
5492 if ((shlib && reloc)
5493 || TREE_SIDE_EFFECTS (decl)
5494 || ! TREE_CONSTANT (decl))
5495 ret = SECCAT_DATA;
5496 else
5497 ret = SECCAT_RODATA;
5499 else
5500 ret = SECCAT_RODATA;
5502 /* There are no read-only thread-local sections. */
5503 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5505 /* Note that this would be *just* SECCAT_BSS, except that there's
5506 no concept of a read-only thread-local-data section. */
5507 if (ret == SECCAT_BSS
5508 || (flag_zero_initialized_in_bss
5509 && initializer_zerop (DECL_INITIAL (decl))))
5510 ret = SECCAT_TBSS;
5511 else
5512 ret = SECCAT_TDATA;
5515 /* If the target uses small data sections, select it. */
5516 else if (targetm.in_small_data_p (decl))
5518 if (ret == SECCAT_BSS)
5519 ret = SECCAT_SBSS;
5520 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5521 ret = SECCAT_SRODATA;
5522 else
5523 ret = SECCAT_SDATA;
5526 return ret;
5529 bool
5530 decl_readonly_section (tree decl, int reloc)
5532 return decl_readonly_section_1 (decl, reloc, flag_pic);
5535 bool
5536 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5538 switch (categorize_decl_for_section (decl, reloc, shlib))
5540 case SECCAT_RODATA:
5541 case SECCAT_RODATA_MERGE_STR:
5542 case SECCAT_RODATA_MERGE_STR_INIT:
5543 case SECCAT_RODATA_MERGE_CONST:
5544 case SECCAT_SRODATA:
5545 return true;
5546 break;
5547 default:
5548 return false;
5549 break;
5553 /* Select a section based on the above categorization. */
5555 section *
5556 default_elf_select_section (tree decl, int reloc,
5557 unsigned HOST_WIDE_INT align)
5559 return default_elf_select_section_1 (decl, reloc, align, flag_pic);
5562 section *
5563 default_elf_select_section_1 (tree decl, int reloc,
5564 unsigned HOST_WIDE_INT align, int shlib)
5566 const char *sname;
5567 switch (categorize_decl_for_section (decl, reloc, shlib))
5569 case SECCAT_TEXT:
5570 /* We're not supposed to be called on FUNCTION_DECLs. */
5571 gcc_unreachable ();
5572 case SECCAT_RODATA:
5573 return readonly_data_section;
5574 case SECCAT_RODATA_MERGE_STR:
5575 return mergeable_string_section (decl, align, 0);
5576 case SECCAT_RODATA_MERGE_STR_INIT:
5577 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5578 case SECCAT_RODATA_MERGE_CONST:
5579 return mergeable_constant_section (DECL_MODE (decl), align, 0);
5580 case SECCAT_SRODATA:
5581 sname = ".sdata2";
5582 break;
5583 case SECCAT_DATA:
5584 return data_section;
5585 case SECCAT_DATA_REL:
5586 sname = ".data.rel";
5587 break;
5588 case SECCAT_DATA_REL_LOCAL:
5589 sname = ".data.rel.local";
5590 break;
5591 case SECCAT_DATA_REL_RO:
5592 sname = ".data.rel.ro";
5593 break;
5594 case SECCAT_DATA_REL_RO_LOCAL:
5595 sname = ".data.rel.ro.local";
5596 break;
5597 case SECCAT_SDATA:
5598 sname = ".sdata";
5599 break;
5600 case SECCAT_TDATA:
5601 sname = ".tdata";
5602 break;
5603 case SECCAT_BSS:
5604 if (bss_section)
5605 return bss_section;
5606 sname = ".bss";
5607 break;
5608 case SECCAT_SBSS:
5609 sname = ".sbss";
5610 break;
5611 case SECCAT_TBSS:
5612 sname = ".tbss";
5613 break;
5614 default:
5615 gcc_unreachable ();
5618 if (!DECL_P (decl))
5619 decl = NULL_TREE;
5620 return get_named_section (decl, sname, reloc);
5623 /* Construct a unique section name based on the decl name and the
5624 categorization performed above. */
5626 void
5627 default_unique_section (tree decl, int reloc)
5629 default_unique_section_1 (decl, reloc, flag_pic);
5632 void
5633 default_unique_section_1 (tree decl, int reloc, int shlib)
5635 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
5636 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5637 const char *prefix, *name;
5638 size_t nlen, plen;
5639 char *string;
5641 switch (categorize_decl_for_section (decl, reloc, shlib))
5643 case SECCAT_TEXT:
5644 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5645 break;
5646 case SECCAT_RODATA:
5647 case SECCAT_RODATA_MERGE_STR:
5648 case SECCAT_RODATA_MERGE_STR_INIT:
5649 case SECCAT_RODATA_MERGE_CONST:
5650 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5651 break;
5652 case SECCAT_SRODATA:
5653 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5654 break;
5655 case SECCAT_DATA:
5656 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5657 break;
5658 case SECCAT_DATA_REL:
5659 prefix = one_only ? ".gnu.linkonce.d.rel." : ".data.rel.";
5660 break;
5661 case SECCAT_DATA_REL_LOCAL:
5662 prefix = one_only ? ".gnu.linkonce.d.rel.local." : ".data.rel.local.";
5663 break;
5664 case SECCAT_DATA_REL_RO:
5665 prefix = one_only ? ".gnu.linkonce.d.rel.ro." : ".data.rel.ro.";
5666 break;
5667 case SECCAT_DATA_REL_RO_LOCAL:
5668 prefix = one_only ? ".gnu.linkonce.d.rel.ro.local."
5669 : ".data.rel.ro.local.";
5670 break;
5671 case SECCAT_SDATA:
5672 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5673 break;
5674 case SECCAT_BSS:
5675 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5676 break;
5677 case SECCAT_SBSS:
5678 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5679 break;
5680 case SECCAT_TDATA:
5681 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5682 break;
5683 case SECCAT_TBSS:
5684 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5685 break;
5686 default:
5687 gcc_unreachable ();
5689 plen = strlen (prefix);
5691 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5692 name = targetm.strip_name_encoding (name);
5693 nlen = strlen (name);
5695 string = alloca (nlen + plen + 1);
5696 memcpy (string, prefix, plen);
5697 memcpy (string + plen, name, nlen + 1);
5699 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5702 section *
5703 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5704 rtx x,
5705 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5707 if (flag_pic)
5708 switch (GET_CODE (x))
5710 case CONST:
5711 case SYMBOL_REF:
5712 case LABEL_REF:
5713 return data_section;
5715 default:
5716 break;
5719 return readonly_data_section;
5722 section *
5723 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5724 unsigned HOST_WIDE_INT align)
5726 /* ??? Handle small data here somehow. */
5728 if (flag_pic)
5729 switch (GET_CODE (x))
5731 case CONST:
5732 case SYMBOL_REF:
5733 return get_named_section (NULL, ".data.rel.ro", 3);
5735 case LABEL_REF:
5736 return get_named_section (NULL, ".data.rel.ro.local", 1);
5738 default:
5739 break;
5742 return mergeable_constant_section (mode, align, 0);
5745 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
5747 void
5748 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5750 rtx symbol;
5751 int flags;
5753 /* Careful not to prod global register variables. */
5754 if (!MEM_P (rtl))
5755 return;
5756 symbol = XEXP (rtl, 0);
5757 if (GET_CODE (symbol) != SYMBOL_REF)
5758 return;
5760 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
5761 if (TREE_CODE (decl) == FUNCTION_DECL)
5762 flags |= SYMBOL_FLAG_FUNCTION;
5763 if (targetm.binds_local_p (decl))
5764 flags |= SYMBOL_FLAG_LOCAL;
5765 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5766 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5767 else if (targetm.in_small_data_p (decl))
5768 flags |= SYMBOL_FLAG_SMALL;
5769 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
5770 being PUBLIC, the thing *must* be defined in this translation unit.
5771 Prevent this buglet from being propagated into rtl code as well. */
5772 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5773 flags |= SYMBOL_FLAG_EXTERNAL;
5775 SYMBOL_REF_FLAGS (symbol) = flags;
5778 /* By default, we do nothing for encode_section_info, so we need not
5779 do anything but discard the '*' marker. */
5781 const char *
5782 default_strip_name_encoding (const char *str)
5784 return str + (*str == '*');
5787 #ifdef ASM_OUTPUT_DEF
5788 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
5789 anchor relative to ".", the current section position. */
5791 void
5792 default_asm_output_anchor (rtx symbol)
5794 char buffer[100];
5796 sprintf (buffer, ". + " HOST_WIDE_INT_PRINT_DEC,
5797 SYMBOL_REF_BLOCK_OFFSET (symbol));
5798 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
5800 #endif
5802 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
5804 bool
5805 default_use_anchors_for_symbol_p (rtx symbol)
5807 section *sect;
5808 tree decl;
5810 /* Don't use anchors for mergeable sections. The linker might move
5811 the objects around. */
5812 sect = SYMBOL_REF_BLOCK (symbol)->sect;
5813 if (sect->common.flags & SECTION_MERGE)
5814 return false;
5816 /* Don't use anchors for small data sections. The small data register
5817 acts as an anchor for such sections. */
5818 if (sect->common.flags & SECTION_SMALL)
5819 return false;
5821 decl = SYMBOL_REF_DECL (symbol);
5822 if (decl && DECL_P (decl))
5824 /* Don't use section anchors for decls that might be defined by
5825 other modules. */
5826 if (!targetm.binds_local_p (decl))
5827 return false;
5829 /* Don't use section anchors for decls that will be placed in a
5830 small data section. */
5831 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
5832 one above. The problem is that we only use SECTION_SMALL for
5833 sections that should be marked as small in the section directive. */
5834 if (targetm.in_small_data_p (decl))
5835 return false;
5837 return true;
5840 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5841 wrt cross-module name binding. */
5843 bool
5844 default_binds_local_p (tree exp)
5846 return default_binds_local_p_1 (exp, flag_shlib);
5849 bool
5850 default_binds_local_p_1 (tree exp, int shlib)
5852 bool local_p;
5854 /* A non-decl is an entry in the constant pool. */
5855 if (!DECL_P (exp))
5856 local_p = true;
5857 /* Weakrefs may not bind locally, even though the weakref itself is
5858 always static and therefore local. */
5859 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
5860 local_p = false;
5861 /* Static variables are always local. */
5862 else if (! TREE_PUBLIC (exp))
5863 local_p = true;
5864 /* A variable is local if the user has said explicitly that it will
5865 be. */
5866 else if (DECL_VISIBILITY_SPECIFIED (exp)
5867 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5868 local_p = true;
5869 /* Variables defined outside this object might not be local. */
5870 else if (DECL_EXTERNAL (exp))
5871 local_p = false;
5872 /* If defined in this object and visibility is not default, must be
5873 local. */
5874 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5875 local_p = true;
5876 /* Default visibility weak data can be overridden by a strong symbol
5877 in another module and so are not local. */
5878 else if (DECL_WEAK (exp))
5879 local_p = false;
5880 /* If PIC, then assume that any global name can be overridden by
5881 symbols resolved from other modules. */
5882 else if (shlib)
5883 local_p = false;
5884 /* Uninitialized COMMON variable may be unified with symbols
5885 resolved from other modules. */
5886 else if (DECL_COMMON (exp)
5887 && (DECL_INITIAL (exp) == NULL
5888 || DECL_INITIAL (exp) == error_mark_node))
5889 local_p = false;
5890 /* Otherwise we're left with initialized (or non-common) global data
5891 which is of necessity defined locally. */
5892 else
5893 local_p = true;
5895 return local_p;
5898 /* Determine whether or not a pointer mode is valid. Assume defaults
5899 of ptr_mode or Pmode - can be overridden. */
5900 bool
5901 default_valid_pointer_mode (enum machine_mode mode)
5903 return (mode == ptr_mode || mode == Pmode);
5906 /* Default function to output code that will globalize a label. A
5907 target must define GLOBAL_ASM_OP or provide its own function to
5908 globalize a label. */
5909 #ifdef GLOBAL_ASM_OP
5910 void
5911 default_globalize_label (FILE * stream, const char *name)
5913 fputs (GLOBAL_ASM_OP, stream);
5914 assemble_name (stream, name);
5915 putc ('\n', stream);
5917 #endif /* GLOBAL_ASM_OP */
5919 /* Default function to output a label for unwind information. The
5920 default is to do nothing. A target that needs nonlocal labels for
5921 unwind information must provide its own function to do this. */
5922 void
5923 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5924 tree decl ATTRIBUTE_UNUSED,
5925 int for_eh ATTRIBUTE_UNUSED,
5926 int empty ATTRIBUTE_UNUSED)
5930 /* Default function to output a label to divide up the exception table.
5931 The default is to do nothing. A target that needs/wants to divide
5932 up the table must provide it's own function to do this. */
5933 void
5934 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
5938 /* This is how to output an internal numbered label where PREFIX is
5939 the class of label and LABELNO is the number within the class. */
5941 void
5942 default_internal_label (FILE *stream, const char *prefix,
5943 unsigned long labelno)
5945 char *const buf = alloca (40 + strlen (prefix));
5946 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5947 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5950 /* This is the default behavior at the beginning of a file. It's
5951 controlled by two other target-hook toggles. */
5952 void
5953 default_file_start (void)
5955 if (targetm.file_start_app_off && !flag_verbose_asm)
5956 fputs (ASM_APP_OFF, asm_out_file);
5958 if (targetm.file_start_file_directive)
5959 output_file_directive (asm_out_file, main_input_filename);
5962 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5963 which emits a special section directive used to indicate whether or
5964 not this object file needs an executable stack. This is primarily
5965 a GNU extension to ELF but could be used on other targets. */
5967 int trampolines_created;
5969 void
5970 file_end_indicate_exec_stack (void)
5972 unsigned int flags = SECTION_DEBUG;
5973 if (trampolines_created)
5974 flags |= SECTION_CODE;
5976 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
5979 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
5980 a get_unnamed_section callback. */
5982 void
5983 output_section_asm_op (const void *directive)
5985 fprintf (asm_out_file, "%s\n", (const char *) directive);
5988 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
5989 the current section is NEW_SECTION. */
5991 void
5992 switch_to_section (section *new_section)
5994 if (in_section == new_section)
5995 return;
5997 if (new_section->common.flags & SECTION_FORGET)
5998 in_section = NULL;
5999 else
6000 in_section = new_section;
6002 switch (SECTION_STYLE (new_section))
6004 case SECTION_NAMED:
6005 if (cfun
6006 && !cfun->unlikely_text_section_name
6007 && strcmp (new_section->named.name,
6008 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
6009 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
6011 targetm.asm_out.named_section (new_section->named.name,
6012 new_section->named.common.flags,
6013 new_section->named.decl);
6014 break;
6016 case SECTION_UNNAMED:
6017 new_section->unnamed.callback (new_section->unnamed.data);
6018 break;
6020 case SECTION_NOSWITCH:
6021 gcc_unreachable ();
6022 break;
6025 new_section->common.flags |= SECTION_DECLARED;
6028 /* If block symbol SYMBOL has not yet been assigned an offset, place
6029 it at the end of its block. */
6031 void
6032 place_block_symbol (rtx symbol)
6034 unsigned HOST_WIDE_INT size, mask, offset;
6035 struct constant_descriptor_rtx *desc;
6036 unsigned int alignment;
6037 struct object_block *block;
6038 tree decl;
6040 gcc_assert (SYMBOL_REF_BLOCK (symbol));
6041 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
6042 return;
6044 /* Work out the symbol's size and alignment. */
6045 if (CONSTANT_POOL_ADDRESS_P (symbol))
6047 desc = SYMBOL_REF_CONSTANT (symbol);
6048 alignment = desc->align;
6049 size = GET_MODE_SIZE (desc->mode);
6051 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6053 decl = SYMBOL_REF_DECL (symbol);
6054 alignment = get_constant_alignment (decl);
6055 size = get_constant_size (decl);
6057 else
6059 decl = SYMBOL_REF_DECL (symbol);
6060 alignment = DECL_ALIGN (decl);
6061 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6064 /* Calculate the object's offset from the start of the block. */
6065 block = SYMBOL_REF_BLOCK (symbol);
6066 mask = alignment / BITS_PER_UNIT - 1;
6067 offset = (block->size + mask) & ~mask;
6068 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
6070 /* Record the block's new alignment and size. */
6071 block->alignment = MAX (block->alignment, alignment);
6072 block->size = offset + size;
6074 VEC_safe_push (rtx, gc, block->objects, symbol);
6077 /* Return the anchor that should be used to address byte offset OFFSET
6078 from the first object in BLOCK. MODEL is the TLS model used
6079 to access it. */
6082 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
6083 enum tls_model model)
6085 char label[100];
6086 unsigned int begin, middle, end;
6087 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
6088 rtx anchor;
6090 /* Work out the anchor's offset. Use an offset of 0 for the first
6091 anchor so that we don't pessimize the case where we take the address
6092 of a variable at the beginning of the block. This is particularly
6093 useful when a block has only one variable assigned to it.
6095 We try to place anchors RANGE bytes apart, so there can then be
6096 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
6097 a ptr_mode offset. With some target settings, the lowest such
6098 anchor might be out of range for the lowest ptr_mode offset;
6099 likewise the highest anchor for the highest offset. Use anchors
6100 at the extreme ends of the ptr_mode range in such cases.
6102 All arithmetic uses unsigned integers in order to avoid
6103 signed overflow. */
6104 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
6105 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
6106 range = max_offset - min_offset + 1;
6107 if (range == 0)
6108 offset = 0;
6109 else
6111 bias = 1 << (GET_MODE_BITSIZE (ptr_mode) - 1);
6112 if (offset < 0)
6114 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
6115 delta -= delta % range;
6116 if (delta > bias)
6117 delta = bias;
6118 offset = (HOST_WIDE_INT) (-delta);
6120 else
6122 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
6123 delta -= delta % range;
6124 if (delta > bias - 1)
6125 delta = bias - 1;
6126 offset = (HOST_WIDE_INT) delta;
6130 /* Do a binary search to see if there's already an anchor we can use.
6131 Set BEGIN to the new anchor's index if not. */
6132 begin = 0;
6133 end = VEC_length (rtx, block->anchors);
6134 while (begin != end)
6136 middle = (end + begin) / 2;
6137 anchor = VEC_index (rtx, block->anchors, middle);
6138 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
6139 end = middle;
6140 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
6141 begin = middle + 1;
6142 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
6143 end = middle;
6144 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
6145 begin = middle + 1;
6146 else
6147 return anchor;
6150 /* Create a new anchor with a unique label. */
6151 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
6152 anchor = create_block_symbol (ggc_strdup (label), block, offset);
6153 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
6154 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
6156 /* Insert it at index BEGIN. */
6157 VEC_safe_insert (rtx, gc, block->anchors, begin, anchor);
6158 return anchor;
6161 /* Output the objects in BLOCK. */
6163 static void
6164 output_object_block (struct object_block *block)
6166 struct constant_descriptor_rtx *desc;
6167 unsigned int i;
6168 HOST_WIDE_INT offset;
6169 tree decl;
6170 rtx symbol;
6172 if (block->objects == NULL)
6173 return;
6175 /* Switch to the section and make sure that the first byte is
6176 suitably aligned. */
6177 switch_to_section (block->sect);
6178 assemble_align (block->alignment);
6180 /* Define the values of all anchors relative to the current section
6181 position. */
6182 for (i = 0; VEC_iterate (rtx, block->anchors, i, symbol); i++)
6183 targetm.asm_out.output_anchor (symbol);
6185 /* Output the objects themselves. */
6186 offset = 0;
6187 for (i = 0; VEC_iterate (rtx, block->objects, i, symbol); i++)
6189 /* Move to the object's offset, padding with zeros if necessary. */
6190 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
6191 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
6192 if (CONSTANT_POOL_ADDRESS_P (symbol))
6194 desc = SYMBOL_REF_CONSTANT (symbol);
6195 output_constant_pool_1 (desc, 1);
6196 offset += GET_MODE_SIZE (desc->mode);
6198 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
6200 decl = SYMBOL_REF_DECL (symbol);
6201 assemble_constant_contents (decl, XSTR (symbol, 0),
6202 get_constant_alignment (decl));
6203 offset += get_constant_size (decl);
6205 else
6207 decl = SYMBOL_REF_DECL (symbol);
6208 assemble_variable_contents (decl, XSTR (symbol, 0), false);
6209 offset += tree_low_cst (DECL_SIZE_UNIT (decl), 1);
6214 /* A htab_traverse callback used to call output_object_block for
6215 each member of object_block_htab. */
6217 static int
6218 output_object_block_htab (void **slot, void *data ATTRIBUTE_UNUSED)
6220 output_object_block ((struct object_block *) (*slot));
6221 return 1;
6224 /* Output the definitions of all object_blocks. */
6226 void
6227 output_object_blocks (void)
6229 htab_traverse (object_block_htab, output_object_block_htab, NULL);
6232 /* This function provides a possible implementation of the
6233 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
6234 by -frecord-gcc-switches it creates a new mergeable, string section in the
6235 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
6236 contains the switches in ASCII format.
6238 FIXME: This code does not correctly handle double quote characters
6239 that appear inside strings, (it strips them rather than preserving them).
6240 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
6241 characters - instead it treats them as sub-string separators. Since
6242 we want to emit NUL strings terminators into the object file we have to use
6243 ASM_OUTPUT_SKIP. */
6246 elf_record_gcc_switches (print_switch_type type, const char * name)
6248 static char buffer[1024];
6250 /* This variable is used as part of a simplistic heuristic to detect
6251 command line switches which take an argument:
6253 "If a command line option does not start with a dash then
6254 it is an argument for the previous command line option."
6256 This fails in the case of the command line option which is the name
6257 of the file to compile, but otherwise it is pretty reasonable. */
6258 static bool previous_name_held_back = FALSE;
6260 switch (type)
6262 case SWITCH_TYPE_PASSED:
6263 if (* name != '-')
6265 if (previous_name_held_back)
6267 unsigned int len = strlen (buffer);
6269 snprintf (buffer + len, sizeof buffer - len, " %s", name);
6270 ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6271 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6272 previous_name_held_back = FALSE;
6274 else
6276 strncpy (buffer, name, sizeof buffer);
6277 ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6278 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6281 else
6283 if (previous_name_held_back)
6285 ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6286 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6289 strncpy (buffer, name, sizeof buffer);
6290 previous_name_held_back = TRUE;
6292 break;
6294 case SWITCH_TYPE_DESCRIPTIVE:
6295 if (name == NULL)
6297 /* Distinguish between invocations where name is NULL. */
6298 static bool started = false;
6300 if (started)
6302 if (previous_name_held_back)
6304 ASM_OUTPUT_ASCII (asm_out_file, buffer, strlen (buffer));
6305 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
6308 else
6310 section * sec;
6312 sec = get_section (targetm.asm_out.record_gcc_switches_section,
6313 SECTION_DEBUG
6314 | SECTION_MERGE
6315 | SECTION_STRINGS
6316 | (SECTION_ENTSIZE & 1),
6317 NULL);
6318 switch_to_section (sec);
6319 started = true;
6323 default:
6324 break;
6327 /* The return value is currently ignored by the caller, but must be 0.
6328 For -fverbose-asm the return value would be the number of characters
6329 emitted into the assembler file. */
6330 return 0;
6333 /* Emit text to declare externally defined symbols. It is needed to
6334 properly support non-default visibility. */
6335 void
6336 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
6337 tree decl,
6338 const char *name ATTRIBUTE_UNUSED)
6340 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
6341 set in order to avoid putting out names that are never really
6342 used. */
6343 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
6344 && targetm.binds_local_p (decl))
6345 maybe_assemble_visibility (decl);
6348 #include "gt-varasm.h"