PR tree-opt/22237
[official-gcc.git] / gcc / varasm.c
blobf21c4b981ddf5bb9fa677737a8f48ed05f9f5c5f
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
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 static void maybe_assemble_visibility (tree);
130 static int in_named_entry_eq (const void *, const void *);
131 static hashval_t in_named_entry_hash (const void *);
132 static void initialize_cold_section_name (void);
133 #ifdef BSS_SECTION_ASM_OP
134 #ifdef ASM_OUTPUT_BSS
135 static void asm_output_bss (FILE *, tree, const char *,
136 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
137 #endif
138 #ifdef ASM_OUTPUT_ALIGNED_BSS
139 static void asm_output_aligned_bss (FILE *, tree, const char *,
140 unsigned HOST_WIDE_INT, int)
141 ATTRIBUTE_UNUSED;
142 #endif
143 #endif /* BSS_SECTION_ASM_OP */
144 static bool asm_emit_uninitialised (tree, const char*,
145 unsigned HOST_WIDE_INT,
146 unsigned HOST_WIDE_INT);
147 static void mark_weak (tree);
149 static GTY(()) enum in_section in_section = no_section;
150 enum in_section last_text_section;
152 /* Return a nonzero value if DECL has a section attribute. */
153 #ifndef IN_NAMED_SECTION
154 #define IN_NAMED_SECTION(DECL) \
155 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
156 && DECL_SECTION_NAME (DECL) != NULL_TREE)
157 #endif
159 /* Text of section name when in_section == in_named. */
160 static GTY(()) const char *in_named_name;
161 const char *last_text_section_name;
163 /* Hash table of flags that have been used for a particular named section. */
165 struct in_named_entry GTY(())
167 const char *name;
168 unsigned int flags;
169 bool declared;
172 static GTY((param_is (struct in_named_entry))) htab_t in_named_htab;
174 /* Define functions like text_section for any extra sections. */
175 #ifdef EXTRA_SECTION_FUNCTIONS
176 EXTRA_SECTION_FUNCTIONS
177 #endif
179 static void
180 initialize_cold_section_name (void)
182 const char *stripped_name;
183 char *name, *buffer;
184 tree dsn;
186 gcc_assert (cfun && current_function_decl);
187 if (cfun->unlikely_text_section_name)
188 return;
190 dsn = DECL_SECTION_NAME (current_function_decl);
191 if (flag_function_sections && dsn)
193 name = alloca (TREE_STRING_LENGTH (dsn) + 1);
194 memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
196 stripped_name = targetm.strip_name_encoding (name);
198 buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
199 cfun->unlikely_text_section_name = ggc_strdup (buffer);
201 else
202 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
205 /* Tell assembler to switch to text section. */
207 void
208 text_section (void)
210 if (in_section != in_text)
212 in_section = in_text;
213 last_text_section = in_text;
214 fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
218 /* Tell assembler to switch to unlikely-to-be-executed text section. */
220 void
221 unlikely_text_section (void)
223 if (cfun)
225 if (!cfun->unlikely_text_section_name)
226 initialize_cold_section_name ();
228 if (flag_function_sections
229 || ((in_section != in_unlikely_executed_text)
230 && (in_section != in_named
231 || (strcmp (in_named_name, cfun->unlikely_text_section_name)
232 != 0))))
234 named_section (NULL_TREE, cfun->unlikely_text_section_name, 0);
235 in_section = in_unlikely_executed_text;
236 last_text_section = in_unlikely_executed_text;
239 else
241 named_section (NULL_TREE, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
242 in_section = in_unlikely_executed_text;
243 last_text_section = in_unlikely_executed_text;
247 /* Tell assembler to switch to data section. */
249 void
250 data_section (void)
252 if (in_section != in_data)
254 in_section = in_data;
255 fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
259 /* Tell assembler to switch to read-only data section. This is normally
260 the text section. */
262 void
263 readonly_data_section (void)
265 #ifdef READONLY_DATA_SECTION
266 READONLY_DATA_SECTION (); /* Note this can call data_section. */
267 #else
268 #ifdef READONLY_DATA_SECTION_ASM_OP
269 if (in_section != in_readonly_data)
271 in_section = in_readonly_data;
272 fputs (READONLY_DATA_SECTION_ASM_OP, asm_out_file);
273 fputc ('\n', asm_out_file);
275 #else
276 text_section ();
277 #endif
278 #endif
281 /* Determine if we're in the text section. */
284 in_text_section (void)
286 return in_section == in_text;
289 /* Determine if we're in the unlikely-to-be-executed text section. */
292 in_unlikely_text_section (void)
294 bool ret_val;
296 if (cfun)
298 ret_val = ((in_section == in_unlikely_executed_text)
299 || (in_section == in_named
300 && cfun->unlikely_text_section_name
301 && strcmp (in_named_name,
302 cfun->unlikely_text_section_name) == 0));
304 else
306 ret_val = ((in_section == in_unlikely_executed_text)
307 || (in_section == in_named
308 && strcmp (in_named_name,
309 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0));
312 return ret_val;
315 /* Determine if we're in the data section. */
318 in_data_section (void)
320 return in_section == in_data;
323 /* Helper routines for maintaining in_named_htab. */
325 static int
326 in_named_entry_eq (const void *p1, const void *p2)
328 const struct in_named_entry *old = p1;
329 const char *new = p2;
331 return strcmp (old->name, new) == 0;
334 static hashval_t
335 in_named_entry_hash (const void *p)
337 const struct in_named_entry *old = p;
338 return htab_hash_string (old->name);
341 /* If SECTION has been seen before as a named section, return the flags
342 that were used. Otherwise, return 0. Note, that 0 is a perfectly valid
343 set of flags for a section to have, so 0 does not mean that the section
344 has not been seen. */
346 static unsigned int
347 get_named_section_flags (const char *section)
349 struct in_named_entry **slot;
351 slot = (struct in_named_entry **)
352 htab_find_slot_with_hash (in_named_htab, section,
353 htab_hash_string (section), NO_INSERT);
355 return slot ? (*slot)->flags : 0;
358 /* Returns true if the section has been declared before. Sets internal
359 flag on this section in in_named_hash so subsequent calls on this
360 section will return false. */
362 bool
363 named_section_first_declaration (const char *name)
365 struct in_named_entry **slot;
367 slot = (struct in_named_entry **)
368 htab_find_slot_with_hash (in_named_htab, name,
369 htab_hash_string (name), NO_INSERT);
370 if (! (*slot)->declared)
372 (*slot)->declared = true;
373 return true;
375 else
377 return false;
382 /* Record FLAGS for SECTION. If SECTION was previously recorded with a
383 different set of flags, return false. */
385 bool
386 set_named_section_flags (const char *section, unsigned int flags)
388 struct in_named_entry **slot, *entry;
390 slot = (struct in_named_entry **)
391 htab_find_slot_with_hash (in_named_htab, section,
392 htab_hash_string (section), INSERT);
393 entry = *slot;
395 if (!entry)
397 entry = ggc_alloc (sizeof (*entry));
398 *slot = entry;
399 entry->name = ggc_strdup (section);
400 entry->flags = flags;
401 entry->declared = false;
403 else if (entry->flags != flags)
404 return false;
406 return true;
409 /* Tell assembler to change to section NAME with attributes FLAGS. If
410 DECL is non-NULL, it is the VAR_DECL or FUNCTION_DECL with which
411 this section is associated. */
413 void
414 named_section_real (const char *name, unsigned int flags, tree decl)
416 if (in_section != in_named || strcmp (name, in_named_name) != 0)
418 bool unchanged = set_named_section_flags (name, flags);
420 gcc_assert (unchanged);
422 targetm.asm_out.named_section (name, flags, decl);
424 if (flags & SECTION_FORGET)
425 in_section = no_section;
426 else
428 in_named_name = ggc_strdup (name);
429 in_section = in_named;
433 if (in_text_section () || in_unlikely_text_section ())
435 last_text_section = in_section;
436 last_text_section_name = name;
440 /* Tell assembler to change to section NAME for DECL.
441 If DECL is NULL, just switch to section NAME.
442 If NAME is NULL, get the name from DECL.
443 If RELOC is 1, the initializer for DECL contains relocs. */
445 void
446 named_section (tree decl, const char *name, int reloc)
448 unsigned int flags;
450 gcc_assert (!decl || DECL_P (decl));
451 if (name == NULL)
452 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
454 if (strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0
455 && cfun
456 && ! cfun->unlikely_text_section_name)
457 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
459 flags = targetm.section_type_flags (decl, name, reloc);
461 /* Sanity check user variables for flag changes. Non-user
462 section flag changes will die in named_section_flags.
463 However, don't complain if SECTION_OVERRIDE is set.
464 We trust that the setter knows that it is safe to ignore
465 the default flags for this decl. */
466 if (decl && ! set_named_section_flags (name, flags))
468 flags = get_named_section_flags (name);
469 if ((flags & SECTION_OVERRIDE) == 0)
470 error ("%+D causes a section type conflict", decl);
473 named_section_real (name, flags, decl);
476 /* If required, set DECL_SECTION_NAME to a unique name. */
478 void
479 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
480 int flag_function_or_data_sections)
482 if (DECL_SECTION_NAME (decl) == NULL_TREE
483 && targetm.have_named_sections
484 && (flag_function_or_data_sections
485 || DECL_ONE_ONLY (decl)))
486 targetm.asm_out.unique_section (decl, reloc);
489 #ifdef BSS_SECTION_ASM_OP
491 /* Tell the assembler to switch to the bss section. */
493 void
494 bss_section (void)
496 if (in_section != in_bss)
498 fprintf (asm_out_file, "%s\n", BSS_SECTION_ASM_OP);
499 in_section = in_bss;
503 #ifdef ASM_OUTPUT_BSS
505 /* Utility function for ASM_OUTPUT_BSS for targets to use if
506 they don't support alignments in .bss.
507 ??? It is believed that this function will work in most cases so such
508 support is localized here. */
510 static void
511 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
512 const char *name,
513 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
514 unsigned HOST_WIDE_INT rounded)
516 targetm.asm_out.globalize_label (file, name);
517 bss_section ();
518 #ifdef ASM_DECLARE_OBJECT_NAME
519 last_assemble_variable_decl = decl;
520 ASM_DECLARE_OBJECT_NAME (file, name, decl);
521 #else
522 /* Standard thing is just output label for the object. */
523 ASM_OUTPUT_LABEL (file, name);
524 #endif /* ASM_DECLARE_OBJECT_NAME */
525 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
528 #endif
530 #ifdef ASM_OUTPUT_ALIGNED_BSS
532 /* Utility function for targets to use in implementing
533 ASM_OUTPUT_ALIGNED_BSS.
534 ??? It is believed that this function will work in most cases so such
535 support is localized here. */
537 static void
538 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
539 const char *name, unsigned HOST_WIDE_INT size,
540 int align)
542 bss_section ();
543 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
544 #ifdef ASM_DECLARE_OBJECT_NAME
545 last_assemble_variable_decl = decl;
546 ASM_DECLARE_OBJECT_NAME (file, name, decl);
547 #else
548 /* Standard thing is just output label for the object. */
549 ASM_OUTPUT_LABEL (file, name);
550 #endif /* ASM_DECLARE_OBJECT_NAME */
551 ASM_OUTPUT_SKIP (file, size ? size : 1);
554 #endif
556 #endif /* BSS_SECTION_ASM_OP */
558 /* Switch to the section for function DECL.
560 If DECL is NULL_TREE, switch to the text section. We can be passed
561 NULL_TREE under some circumstances by dbxout.c at least. */
563 void
564 function_section (tree decl)
566 int reloc = 0;
568 if (first_function_block_is_cold)
569 reloc = 1;
571 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
572 targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
573 #else
574 if (decl != NULL_TREE
575 && DECL_SECTION_NAME (decl) != NULL_TREE
576 && targetm.have_named_sections)
577 named_section (decl, (char *) 0, 0);
578 else
579 text_section ();
580 #endif
583 void
584 current_function_section (tree decl)
586 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
587 int reloc = 0;
589 if (in_unlikely_text_section ()
590 || last_text_section == in_unlikely_executed_text)
591 reloc = 1;
593 targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
594 #else
595 if (last_text_section == in_unlikely_executed_text)
596 unlikely_text_section ();
597 else if (last_text_section == in_text)
598 text_section ();
599 else if (last_text_section == in_named
600 && targetm.have_named_sections)
601 named_section (NULL_TREE, last_text_section_name, 0);
602 else
603 function_section (decl);
604 #endif
607 /* Switch to read-only data section associated with function DECL. */
609 void
610 default_function_rodata_section (tree decl)
612 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
614 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
616 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
618 size_t len = strlen (name) + 3;
619 char* rname = alloca (len);
621 strcpy (rname, ".rodata");
622 strcat (rname, name + 5);
623 named_section_real (rname, SECTION_LINKONCE, decl);
624 return;
626 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
627 else if (DECL_ONE_ONLY (decl)
628 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
630 size_t len = strlen (name) + 1;
631 char *rname = alloca (len);
633 memcpy (rname, name, len);
634 rname[14] = 'r';
635 named_section_real (rname, SECTION_LINKONCE, decl);
636 return;
638 /* For .text.foo we want to use .rodata.foo. */
639 else if (flag_function_sections && flag_data_sections
640 && strncmp (name, ".text.", 6) == 0)
642 size_t len = strlen (name) + 1;
643 char *rname = alloca (len + 2);
645 memcpy (rname, ".rodata", 7);
646 memcpy (rname + 7, name + 5, len - 5);
647 named_section_flags (rname, 0);
648 return;
652 readonly_data_section ();
655 /* Switch to read-only data section associated with function DECL
656 for targets where that section should be always the single
657 readonly data section. */
659 void
660 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
662 readonly_data_section ();
665 /* Switch to section for variable DECL. RELOC is the same as the
666 argument to SELECT_SECTION. */
668 void
669 variable_section (tree decl, int reloc)
671 if (IN_NAMED_SECTION (decl))
672 named_section (decl, NULL, reloc);
673 else
674 targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
677 /* Tell assembler to switch to the section for string merging. */
679 void
680 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
681 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
682 unsigned int flags ATTRIBUTE_UNUSED)
684 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
685 && TREE_CODE (decl) == STRING_CST
686 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
687 && align <= 256
688 && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
690 enum machine_mode mode;
691 unsigned int modesize;
692 const char *str;
693 int i, j, len, unit;
694 char name[30];
696 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
697 modesize = GET_MODE_BITSIZE (mode);
698 if (modesize >= 8 && modesize <= 256
699 && (modesize & (modesize - 1)) == 0)
701 if (align < modesize)
702 align = modesize;
704 str = TREE_STRING_POINTER (decl);
705 len = TREE_STRING_LENGTH (decl);
706 unit = GET_MODE_SIZE (mode);
708 /* Check for embedded NUL characters. */
709 for (i = 0; i < len; i += unit)
711 for (j = 0; j < unit; j++)
712 if (str[i + j] != '\0')
713 break;
714 if (j == unit)
715 break;
717 if (i == len - unit)
719 sprintf (name, ".rodata.str%d.%d", modesize / 8,
720 (int) (align / 8));
721 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
722 if (!i && modesize < align)
724 /* A "" string with requested alignment greater than
725 character size might cause a problem:
726 if some other string required even bigger
727 alignment than "", then linker might think the
728 "" is just part of padding after some other string
729 and not put it into the hash table initially.
730 But this means "" could have smaller alignment
731 than requested. */
732 #ifdef ASM_OUTPUT_SECTION_START
733 named_section_flags (name, flags);
734 ASM_OUTPUT_SECTION_START (asm_out_file);
735 #else
736 readonly_data_section ();
737 #endif
738 return;
741 named_section_flags (name, flags);
742 return;
747 readonly_data_section ();
750 /* Tell assembler to switch to the section for constant merging. */
752 void
753 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
754 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
755 unsigned int flags ATTRIBUTE_UNUSED)
757 unsigned int modesize = GET_MODE_BITSIZE (mode);
759 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
760 && mode != VOIDmode
761 && mode != BLKmode
762 && modesize <= align
763 && align >= 8
764 && align <= 256
765 && (align & (align - 1)) == 0)
767 char name[24];
769 sprintf (name, ".rodata.cst%d", (int) (align / 8));
770 flags |= (align / 8) | SECTION_MERGE;
771 named_section_flags (name, flags);
772 return;
775 readonly_data_section ();
778 /* Given NAME, a putative register name, discard any customary prefixes. */
780 static const char *
781 strip_reg_name (const char *name)
783 #ifdef REGISTER_PREFIX
784 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
785 name += strlen (REGISTER_PREFIX);
786 #endif
787 if (name[0] == '%' || name[0] == '#')
788 name++;
789 return name;
792 /* The user has asked for a DECL to have a particular name. Set (or
793 change) it in such a way that we don't prefix an underscore to
794 it. */
795 void
796 set_user_assembler_name (tree decl, const char *name)
798 char *starred = alloca (strlen (name) + 2);
799 starred[0] = '*';
800 strcpy (starred + 1, name);
801 change_decl_assembler_name (decl, get_identifier (starred));
802 SET_DECL_RTL (decl, NULL_RTX);
805 /* Decode an `asm' spec for a declaration as a register name.
806 Return the register number, or -1 if nothing specified,
807 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
808 or -3 if ASMSPEC is `cc' and is not recognized,
809 or -4 if ASMSPEC is `memory' and is not recognized.
810 Accept an exact spelling or a decimal number.
811 Prefixes such as % are optional. */
814 decode_reg_name (const char *asmspec)
816 if (asmspec != 0)
818 int i;
820 /* Get rid of confusing prefixes. */
821 asmspec = strip_reg_name (asmspec);
823 /* Allow a decimal number as a "register name". */
824 for (i = strlen (asmspec) - 1; i >= 0; i--)
825 if (! ISDIGIT (asmspec[i]))
826 break;
827 if (asmspec[0] != 0 && i < 0)
829 i = atoi (asmspec);
830 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
831 return i;
832 else
833 return -2;
836 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
837 if (reg_names[i][0]
838 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
839 return i;
841 #ifdef ADDITIONAL_REGISTER_NAMES
843 static const struct { const char *const name; const int number; } table[]
844 = ADDITIONAL_REGISTER_NAMES;
846 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
847 if (table[i].name[0]
848 && ! strcmp (asmspec, table[i].name))
849 return table[i].number;
851 #endif /* ADDITIONAL_REGISTER_NAMES */
853 if (!strcmp (asmspec, "memory"))
854 return -4;
856 if (!strcmp (asmspec, "cc"))
857 return -3;
859 return -2;
862 return -1;
865 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
866 have static storage duration. In other words, it should not be an
867 automatic variable, including PARM_DECLs.
869 There is, however, one exception: this function handles variables
870 explicitly placed in a particular register by the user.
872 This is never called for PARM_DECL nodes. */
874 void
875 make_decl_rtl (tree decl)
877 const char *name = 0;
878 int reg_number;
879 rtx x;
881 /* Check that we are not being given an automatic variable. */
882 gcc_assert (TREE_CODE (decl) != PARM_DECL
883 && TREE_CODE (decl) != RESULT_DECL);
885 /* A weak alias has TREE_PUBLIC set but not the other bits. */
886 gcc_assert (TREE_CODE (decl) != VAR_DECL
887 || TREE_STATIC (decl)
888 || TREE_PUBLIC (decl)
889 || DECL_EXTERNAL (decl)
890 || DECL_REGISTER (decl));
892 /* And that we were not given a type or a label. */
893 gcc_assert (TREE_CODE (decl) != TYPE_DECL
894 && TREE_CODE (decl) != LABEL_DECL);
896 /* For a duplicate declaration, we can be called twice on the
897 same DECL node. Don't discard the RTL already made. */
898 if (DECL_RTL_SET_P (decl))
900 /* If the old RTL had the wrong mode, fix the mode. */
901 if (GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
902 SET_DECL_RTL (decl, adjust_address_nv (DECL_RTL (decl),
903 DECL_MODE (decl), 0));
905 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
906 return;
908 /* ??? Another way to do this would be to maintain a hashed
909 table of such critters. Instead of adding stuff to a DECL
910 to give certain attributes to it, we could use an external
911 hash map from DECL to set of attributes. */
913 /* Let the target reassign the RTL if it wants.
914 This is necessary, for example, when one machine specific
915 decl attribute overrides another. */
916 targetm.encode_section_info (decl, DECL_RTL (decl), false);
918 /* Make this function static known to the mudflap runtime. */
919 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
920 mudflap_enqueue_decl (decl);
922 return;
925 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
927 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
928 && DECL_REGISTER (decl))
930 error ("register name not specified for %q+D", decl);
932 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
934 const char *asmspec = name+1;
935 reg_number = decode_reg_name (asmspec);
936 /* First detect errors in declaring global registers. */
937 if (reg_number == -1)
938 error ("register name not specified for %q+D", decl);
939 else if (reg_number < 0)
940 error ("invalid register name for %q+D", decl);
941 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
942 error ("data type of %q+D isn%'t suitable for a register",
943 decl);
944 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
945 error ("register specified for %q+D isn%'t suitable for data type",
946 decl);
947 /* Now handle properly declared static register variables. */
948 else
950 int nregs;
952 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
954 DECL_INITIAL (decl) = 0;
955 error ("global register variable has initial value");
957 if (TREE_THIS_VOLATILE (decl))
958 warning (0, "volatile register variables don%'t "
959 "work as you might wish");
961 /* If the user specified one of the eliminables registers here,
962 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
963 confused with that register and be eliminated. This usage is
964 somewhat suspect... */
966 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
967 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
968 REG_USERVAR_P (DECL_RTL (decl)) = 1;
970 if (TREE_STATIC (decl))
972 /* Make this register global, so not usable for anything
973 else. */
974 #ifdef ASM_DECLARE_REGISTER_GLOBAL
975 name = IDENTIFIER_POINTER (DECL_NAME (decl));
976 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
977 #endif
978 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
979 while (nregs > 0)
980 globalize_reg (reg_number + --nregs);
983 /* As a register variable, it has no section. */
984 return;
987 /* Now handle ordinary static variables and functions (in memory).
988 Also handle vars declared register invalidly. */
989 else if (name[0] == '*')
991 #ifdef REGISTER_PREFIX
992 if (strlen (REGISTER_PREFIX) != 0)
994 reg_number = decode_reg_name (name);
995 if (reg_number >= 0 || reg_number == -3)
996 error ("register name given for non-register variable %q+D", decl);
998 #endif
1001 /* Specifying a section attribute on a variable forces it into a
1002 non-.bss section, and thus it cannot be common. */
1003 if (TREE_CODE (decl) == VAR_DECL
1004 && DECL_SECTION_NAME (decl) != NULL_TREE
1005 && DECL_INITIAL (decl) == NULL_TREE
1006 && DECL_COMMON (decl))
1007 DECL_COMMON (decl) = 0;
1009 /* Variables can't be both common and weak. */
1010 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1011 DECL_COMMON (decl) = 0;
1013 x = gen_rtx_SYMBOL_REF (Pmode, name);
1014 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1015 SYMBOL_REF_DECL (x) = decl;
1017 x = gen_rtx_MEM (DECL_MODE (decl), x);
1018 if (TREE_CODE (decl) != FUNCTION_DECL)
1019 set_mem_attributes (x, decl, 1);
1020 SET_DECL_RTL (decl, x);
1022 /* Optionally set flags or add text to the name to record information
1023 such as that it is a function name.
1024 If the name is changed, the macro ASM_OUTPUT_LABELREF
1025 will have to know how to strip this information. */
1026 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1028 /* Make this function static known to the mudflap runtime. */
1029 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1030 mudflap_enqueue_decl (decl);
1033 /* Make the rtl for variable VAR be volatile.
1034 Use this only for static variables. */
1036 void
1037 make_var_volatile (tree var)
1039 gcc_assert (MEM_P (DECL_RTL (var)));
1041 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
1044 /* Output a string of literal assembler code
1045 for an `asm' keyword used between functions. */
1047 void
1048 assemble_asm (tree string)
1050 app_enable ();
1052 if (TREE_CODE (string) == ADDR_EXPR)
1053 string = TREE_OPERAND (string, 0);
1055 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1058 /* Record an element in the table of global destructors. SYMBOL is
1059 a SYMBOL_REF of the function to be called; PRIORITY is a number
1060 between 0 and MAX_INIT_PRIORITY. */
1062 void
1063 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1064 int priority ATTRIBUTE_UNUSED)
1066 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1067 /* Tell GNU LD that this is part of the static destructor set.
1068 This will work for any system that uses stabs, most usefully
1069 aout systems. */
1070 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1071 dbxout_stab_value_label (XSTR (symbol, 0));
1072 #else
1073 sorry ("global destructors not supported on this target");
1074 #endif
1077 void
1078 default_named_section_asm_out_destructor (rtx symbol, int priority)
1080 const char *section = ".dtors";
1081 char buf[16];
1083 /* ??? This only works reliably with the GNU linker. */
1084 if (priority != DEFAULT_INIT_PRIORITY)
1086 sprintf (buf, ".dtors.%.5u",
1087 /* Invert the numbering so the linker puts us in the proper
1088 order; constructors are run from right to left, and the
1089 linker sorts in increasing order. */
1090 MAX_INIT_PRIORITY - priority);
1091 section = buf;
1094 named_section_flags (section, SECTION_WRITE);
1095 assemble_align (POINTER_SIZE);
1096 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1099 #ifdef DTORS_SECTION_ASM_OP
1100 void
1101 dtors_section (void)
1103 if (in_section != in_dtors)
1105 in_section = in_dtors;
1106 fputs (DTORS_SECTION_ASM_OP, asm_out_file);
1107 fputc ('\n', asm_out_file);
1111 void
1112 default_dtor_section_asm_out_destructor (rtx symbol,
1113 int priority ATTRIBUTE_UNUSED)
1115 dtors_section ();
1116 assemble_align (POINTER_SIZE);
1117 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1119 #endif
1121 /* Likewise for global constructors. */
1123 void
1124 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1125 int priority ATTRIBUTE_UNUSED)
1127 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1128 /* Tell GNU LD that this is part of the static destructor set.
1129 This will work for any system that uses stabs, most usefully
1130 aout systems. */
1131 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1132 dbxout_stab_value_label (XSTR (symbol, 0));
1133 #else
1134 sorry ("global constructors not supported on this target");
1135 #endif
1138 void
1139 default_named_section_asm_out_constructor (rtx symbol, int priority)
1141 const char *section = ".ctors";
1142 char buf[16];
1144 /* ??? This only works reliably with the GNU linker. */
1145 if (priority != DEFAULT_INIT_PRIORITY)
1147 sprintf (buf, ".ctors.%.5u",
1148 /* Invert the numbering so the linker puts us in the proper
1149 order; constructors are run from right to left, and the
1150 linker sorts in increasing order. */
1151 MAX_INIT_PRIORITY - priority);
1152 section = buf;
1155 named_section_flags (section, SECTION_WRITE);
1156 assemble_align (POINTER_SIZE);
1157 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1160 #ifdef CTORS_SECTION_ASM_OP
1161 void
1162 ctors_section (void)
1164 if (in_section != in_ctors)
1166 in_section = in_ctors;
1167 fputs (CTORS_SECTION_ASM_OP, asm_out_file);
1168 fputc ('\n', asm_out_file);
1172 void
1173 default_ctor_section_asm_out_constructor (rtx symbol,
1174 int priority ATTRIBUTE_UNUSED)
1176 ctors_section ();
1177 assemble_align (POINTER_SIZE);
1178 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1180 #endif
1182 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1183 a nonzero value if the constant pool should be output before the
1184 start of the function, or a zero value if the pool should output
1185 after the end of the function. The default is to put it before the
1186 start. */
1188 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1189 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1190 #endif
1192 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1193 to be output to assembler.
1194 Set first_global_object_name and weak_global_object_name as appropriate. */
1196 void
1197 notice_global_symbol (tree decl)
1199 const char **type = &first_global_object_name;
1201 if (first_global_object_name
1202 || !TREE_PUBLIC (decl) || DECL_EXTERNAL (decl)
1203 || !DECL_NAME (decl)
1204 || (TREE_CODE (decl) != FUNCTION_DECL
1205 && (TREE_CODE (decl) != VAR_DECL
1206 || (DECL_COMMON (decl)
1207 && (DECL_INITIAL (decl) == 0
1208 || DECL_INITIAL (decl) == error_mark_node))))
1209 || !MEM_P (DECL_RTL (decl)))
1210 return;
1212 /* We win when global object is found, but it is useful to know about weak
1213 symbol as well so we can produce nicer unique names. */
1214 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1215 type = &weak_global_object_name;
1217 if (!*type)
1219 const char *p;
1220 const char *name;
1221 rtx decl_rtl = DECL_RTL (decl);
1223 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1224 name = ggc_strdup (p);
1226 *type = name;
1230 /* Output assembler code for the constant pool of a function and associated
1231 with defining the name of the function. DECL describes the function.
1232 NAME is the function's name. For the constant pool, we use the current
1233 constant pool data. */
1235 void
1236 assemble_start_function (tree decl, const char *fnname)
1238 int align;
1239 char tmp_label[100];
1240 bool hot_label_written = false;
1242 cfun->unlikely_text_section_name = NULL;
1244 first_function_block_is_cold = false;
1245 if (flag_reorder_blocks_and_partition)
1247 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1248 cfun->hot_section_label = ggc_strdup (tmp_label);
1249 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1250 cfun->cold_section_label = ggc_strdup (tmp_label);
1251 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1252 cfun->hot_section_end_label = ggc_strdup (tmp_label);
1253 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1254 cfun->cold_section_end_label = ggc_strdup (tmp_label);
1255 const_labelno++;
1257 else
1259 cfun->hot_section_label = NULL;
1260 cfun->cold_section_label = NULL;
1261 cfun->hot_section_end_label = NULL;
1262 cfun->cold_section_end_label = NULL;
1265 /* The following code does not need preprocessing in the assembler. */
1267 app_disable ();
1269 if (CONSTANT_POOL_BEFORE_FUNCTION)
1270 output_constant_pool (fnname, decl);
1272 resolve_unique_section (decl, 0, flag_function_sections);
1274 /* Make sure the not and cold text (code) sections are properly
1275 aligned. This is necessary here in the case where the function
1276 has both hot and cold sections, because we don't want to re-set
1277 the alignment when the section switch happens mid-function. */
1279 if (flag_reorder_blocks_and_partition)
1281 unlikely_text_section ();
1282 assemble_align (FUNCTION_BOUNDARY);
1283 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_label);
1285 /* When the function starts with a cold section, we need to explicitly
1286 align the hot section and write out the hot section label.
1287 But if the current function is a thunk, we do not have a CFG. */
1288 if (!current_function_is_thunk
1289 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1291 text_section ();
1292 assemble_align (FUNCTION_BOUNDARY);
1293 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1294 hot_label_written = true;
1295 first_function_block_is_cold = true;
1298 else if (DECL_SECTION_NAME (decl))
1300 /* Calls to function_section rely on first_function_block_is_cold
1301 being accurate. The first block may be cold even if we aren't
1302 doing partitioning, if the entire function was decided by
1303 choose_function_section (predict.c) to be cold. */
1305 initialize_cold_section_name ();
1307 if (cfun->unlikely_text_section_name
1308 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1309 cfun->unlikely_text_section_name) == 0)
1310 first_function_block_is_cold = true;
1313 last_text_section = no_section;
1315 /* Switch to the correct text section for the start of the function. */
1317 function_section (decl);
1318 if (flag_reorder_blocks_and_partition
1319 && !hot_label_written)
1320 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1322 /* Tell assembler to move to target machine's alignment for functions. */
1323 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1324 if (align < force_align_functions_log)
1325 align = force_align_functions_log;
1326 if (align > 0)
1328 ASM_OUTPUT_ALIGN (asm_out_file, align);
1331 /* Handle a user-specified function alignment.
1332 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1333 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1334 if (align_functions_log > align
1335 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1337 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1338 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1339 align_functions_log, align_functions - 1);
1340 #else
1341 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1342 #endif
1345 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1346 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1347 #endif
1349 (*debug_hooks->begin_function) (decl);
1351 /* Make function name accessible from other files, if appropriate. */
1353 if (TREE_PUBLIC (decl))
1355 notice_global_symbol (decl);
1357 globalize_decl (decl);
1359 maybe_assemble_visibility (decl);
1362 if (DECL_PRESERVE_P (decl))
1363 targetm.asm_out.mark_decl_preserved (fnname);
1365 /* Do any machine/system dependent processing of the function name. */
1366 #ifdef ASM_DECLARE_FUNCTION_NAME
1367 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1368 #else
1369 /* Standard thing is just output label for the function. */
1370 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1371 #endif /* ASM_DECLARE_FUNCTION_NAME */
1373 /* Add NOTE_INSN_SWITCH_TEXT_SECTIONS notes. Don't do this if the current
1374 function is a thunk, because we don't have a CFG in that case. */
1375 if (!current_function_is_thunk)
1376 insert_section_boundary_note ();
1379 /* Output assembler code associated with defining the size of the
1380 function. DECL describes the function. NAME is the function's name. */
1382 void
1383 assemble_end_function (tree decl, const char *fnname)
1385 #ifdef ASM_DECLARE_FUNCTION_SIZE
1386 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1387 #endif
1388 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1390 output_constant_pool (fnname, decl);
1391 function_section (decl); /* need to switch back */
1393 /* Output labels for end of hot/cold text sections (to be used by
1394 debug info.) */
1395 if (flag_reorder_blocks_and_partition)
1397 enum in_section save_text_section;
1399 save_text_section = in_section;
1400 unlikely_text_section ();
1401 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_end_label);
1402 if (first_function_block_is_cold)
1403 text_section ();
1404 else
1405 function_section (decl);
1406 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_end_label);
1407 if (save_text_section == in_unlikely_executed_text)
1408 unlikely_text_section ();
1412 /* Assemble code to leave SIZE bytes of zeros. */
1414 void
1415 assemble_zeros (unsigned HOST_WIDE_INT size)
1417 /* Do no output if -fsyntax-only. */
1418 if (flag_syntax_only)
1419 return;
1421 #ifdef ASM_NO_SKIP_IN_TEXT
1422 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1423 so we must output 0s explicitly in the text section. */
1424 if ((ASM_NO_SKIP_IN_TEXT && in_text_section ())
1425 || (ASM_NO_SKIP_IN_TEXT && in_unlikely_text_section ()))
1427 unsigned HOST_WIDE_INT i;
1428 for (i = 0; i < size; i++)
1429 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1431 else
1432 #endif
1433 if (size > 0)
1434 ASM_OUTPUT_SKIP (asm_out_file, size);
1437 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1439 void
1440 assemble_align (int align)
1442 if (align > BITS_PER_UNIT)
1444 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1448 /* Assemble a string constant with the specified C string as contents. */
1450 void
1451 assemble_string (const char *p, int size)
1453 int pos = 0;
1454 int maximum = 2000;
1456 /* If the string is very long, split it up. */
1458 while (pos < size)
1460 int thissize = size - pos;
1461 if (thissize > maximum)
1462 thissize = maximum;
1464 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1466 pos += thissize;
1467 p += thissize;
1472 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1473 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1474 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1475 #else
1476 #if defined ASM_OUTPUT_ALIGNED_LOCAL
1477 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1478 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl))
1479 #else
1480 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1481 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded)
1482 #endif
1483 #endif
1485 #if defined ASM_OUTPUT_ALIGNED_BSS
1486 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1487 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1488 #else
1489 #if defined ASM_OUTPUT_BSS
1490 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1491 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded)
1492 #else
1493 #undef ASM_EMIT_BSS
1494 #endif
1495 #endif
1497 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1498 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1499 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1500 #else
1501 #if defined ASM_OUTPUT_ALIGNED_COMMON
1502 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1503 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl))
1504 #else
1505 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1506 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded)
1507 #endif
1508 #endif
1510 static bool
1511 asm_emit_uninitialised (tree decl, const char *name,
1512 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1513 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1515 enum
1517 asm_dest_common,
1518 asm_dest_bss,
1519 asm_dest_local
1521 destination = asm_dest_local;
1523 /* ??? We should handle .bss via select_section mechanisms rather than
1524 via special target hooks. That would eliminate this special case. */
1525 if (TREE_PUBLIC (decl))
1527 if (!DECL_COMMON (decl))
1528 #ifdef ASM_EMIT_BSS
1529 destination = asm_dest_bss;
1530 #else
1531 return false;
1532 #endif
1533 else
1534 destination = asm_dest_common;
1537 if (destination != asm_dest_common)
1539 resolve_unique_section (decl, 0, flag_data_sections);
1540 /* Custom sections don't belong here. */
1541 if (DECL_SECTION_NAME (decl))
1542 return false;
1545 if (destination == asm_dest_bss)
1546 globalize_decl (decl);
1548 if (flag_shared_data)
1550 switch (destination)
1552 #ifdef ASM_OUTPUT_SHARED_BSS
1553 case asm_dest_bss:
1554 ASM_OUTPUT_SHARED_BSS (asm_out_file, decl, name, size, rounded);
1555 return;
1556 #endif
1557 #ifdef ASM_OUTPUT_SHARED_COMMON
1558 case asm_dest_common:
1559 ASM_OUTPUT_SHARED_COMMON (asm_out_file, name, size, rounded);
1560 return;
1561 #endif
1562 #ifdef ASM_OUTPUT_SHARED_LOCAL
1563 case asm_dest_local:
1564 ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded);
1565 return;
1566 #endif
1567 default:
1568 break;
1572 switch (destination)
1574 #ifdef ASM_EMIT_BSS
1575 case asm_dest_bss:
1576 ASM_EMIT_BSS (decl, name, size, rounded);
1577 break;
1578 #endif
1579 case asm_dest_common:
1580 ASM_EMIT_COMMON (decl, name, size, rounded);
1581 break;
1582 case asm_dest_local:
1583 ASM_EMIT_LOCAL (decl, name, size, rounded);
1584 break;
1585 default:
1586 gcc_unreachable ();
1589 return true;
1592 /* Assemble everything that is needed for a variable or function declaration.
1593 Not used for automatic variables, and not used for function definitions.
1594 Should not be called for variables of incomplete structure type.
1596 TOP_LEVEL is nonzero if this variable has file scope.
1597 AT_END is nonzero if this is the special handling, at end of compilation,
1598 to define things that have had only tentative definitions.
1599 DONT_OUTPUT_DATA if nonzero means don't actually output the
1600 initial value (that will be done by the caller). */
1602 void
1603 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1604 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1606 const char *name;
1607 unsigned int align;
1608 int reloc = 0;
1609 rtx decl_rtl;
1611 if (lang_hooks.decls.prepare_assemble_variable)
1612 lang_hooks.decls.prepare_assemble_variable (decl);
1614 last_assemble_variable_decl = 0;
1616 /* Normally no need to say anything here for external references,
1617 since assemble_external is called by the language-specific code
1618 when a declaration is first seen. */
1620 if (DECL_EXTERNAL (decl))
1621 return;
1623 /* Output no assembler code for a function declaration.
1624 Only definitions of functions output anything. */
1626 if (TREE_CODE (decl) == FUNCTION_DECL)
1627 return;
1629 /* Do nothing for global register variables. */
1630 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1632 TREE_ASM_WRITTEN (decl) = 1;
1633 return;
1636 /* If type was incomplete when the variable was declared,
1637 see if it is complete now. */
1639 if (DECL_SIZE (decl) == 0)
1640 layout_decl (decl, 0);
1642 /* Still incomplete => don't allocate it; treat the tentative defn
1643 (which is what it must have been) as an `extern' reference. */
1645 if (!dont_output_data && DECL_SIZE (decl) == 0)
1647 error ("storage size of %q+D isn%'t known", decl);
1648 TREE_ASM_WRITTEN (decl) = 1;
1649 return;
1652 /* The first declaration of a variable that comes through this function
1653 decides whether it is global (in C, has external linkage)
1654 or local (in C, has internal linkage). So do nothing more
1655 if this function has already run. */
1657 if (TREE_ASM_WRITTEN (decl))
1658 return;
1660 /* Make sure targetm.encode_section_info is invoked before we set
1661 ASM_WRITTEN. */
1662 decl_rtl = DECL_RTL (decl);
1664 TREE_ASM_WRITTEN (decl) = 1;
1666 /* Do no output if -fsyntax-only. */
1667 if (flag_syntax_only)
1668 return;
1670 app_disable ();
1672 if (! dont_output_data
1673 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1675 error ("size of variable %q+D is too large", decl);
1676 return;
1679 name = XSTR (XEXP (decl_rtl, 0), 0);
1680 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1681 notice_global_symbol (decl);
1683 /* Compute the alignment of this data. */
1685 align = DECL_ALIGN (decl);
1687 /* In the case for initialing an array whose length isn't specified,
1688 where we have not yet been able to do the layout,
1689 figure out the proper alignment now. */
1690 if (dont_output_data && DECL_SIZE (decl) == 0
1691 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1692 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1694 /* Some object file formats have a maximum alignment which they support.
1695 In particular, a.out format supports a maximum alignment of 4. */
1696 if (align > MAX_OFILE_ALIGNMENT)
1698 warning (0, "alignment of %q+D is greater than maximum object "
1699 "file alignment. Using %d", decl,
1700 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1701 align = MAX_OFILE_ALIGNMENT;
1704 /* On some machines, it is good to increase alignment sometimes. */
1705 if (! DECL_USER_ALIGN (decl))
1707 #ifdef DATA_ALIGNMENT
1708 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1709 #endif
1710 #ifdef CONSTANT_ALIGNMENT
1711 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1712 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1713 #endif
1716 /* Reset the alignment in case we have made it tighter, so we can benefit
1717 from it in get_pointer_alignment. */
1718 DECL_ALIGN (decl) = align;
1719 set_mem_align (decl_rtl, align);
1721 if (TREE_PUBLIC (decl))
1722 maybe_assemble_visibility (decl);
1724 if (DECL_PRESERVE_P (decl))
1725 targetm.asm_out.mark_decl_preserved (name);
1727 /* Handle uninitialized definitions. */
1729 /* If the decl has been given an explicit section name, then it
1730 isn't common, and shouldn't be handled as such. */
1731 if (DECL_SECTION_NAME (decl) || dont_output_data)
1733 /* We don't implement common thread-local data at present. */
1734 else if (DECL_THREAD_LOCAL_P (decl))
1736 if (DECL_COMMON (decl))
1737 sorry ("thread-local COMMON data not implemented");
1739 else if (DECL_INITIAL (decl) == 0
1740 || DECL_INITIAL (decl) == error_mark_node
1741 || (flag_zero_initialized_in_bss
1742 /* Leave constant zeroes in .rodata so they can be shared. */
1743 && !TREE_READONLY (decl)
1744 && initializer_zerop (DECL_INITIAL (decl))))
1746 unsigned HOST_WIDE_INT size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1747 unsigned HOST_WIDE_INT rounded = size;
1749 /* Don't allocate zero bytes of common,
1750 since that means "undefined external" in the linker. */
1751 if (size == 0)
1752 rounded = 1;
1754 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1755 so that each uninitialized object starts on such a boundary. */
1756 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1757 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1758 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1760 #if !defined(ASM_OUTPUT_ALIGNED_COMMON) && !defined(ASM_OUTPUT_ALIGNED_DECL_COMMON) && !defined(ASM_OUTPUT_ALIGNED_BSS)
1761 if ((unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1762 warning (0, "requested alignment for %q+D is greater than "
1763 "implemented alignment of %wu", decl, rounded);
1764 #endif
1766 /* If the target cannot output uninitialized but not common global data
1767 in .bss, then we have to use .data, so fall through. */
1768 if (asm_emit_uninitialised (decl, name, size, rounded))
1769 return;
1772 /* Handle initialized definitions.
1773 Also handle uninitialized global definitions if -fno-common and the
1774 target doesn't support ASM_OUTPUT_BSS. */
1776 /* First make the assembler name(s) global if appropriate. */
1777 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1778 globalize_decl (decl);
1780 /* Output any data that we will need to use the address of. */
1781 if (DECL_INITIAL (decl) == error_mark_node)
1782 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1783 else if (DECL_INITIAL (decl))
1785 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1786 output_addressed_constants (DECL_INITIAL (decl));
1789 /* Switch to the appropriate section. */
1790 resolve_unique_section (decl, reloc, flag_data_sections);
1791 variable_section (decl, reloc);
1793 /* dbxout.c needs to know this. */
1794 if (in_text_section () || in_unlikely_text_section ())
1795 DECL_IN_TEXT_SECTION (decl) = 1;
1797 /* Output the alignment of this data. */
1798 if (align > BITS_PER_UNIT)
1799 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1801 /* Do any machine/system dependent processing of the object. */
1802 #ifdef ASM_DECLARE_OBJECT_NAME
1803 last_assemble_variable_decl = decl;
1804 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1805 #else
1806 /* Standard thing is just output label for the object. */
1807 ASM_OUTPUT_LABEL (asm_out_file, name);
1808 #endif /* ASM_DECLARE_OBJECT_NAME */
1810 if (!dont_output_data)
1812 if (DECL_INITIAL (decl)
1813 && DECL_INITIAL (decl) != error_mark_node
1814 && !initializer_zerop (DECL_INITIAL (decl)))
1815 /* Output the actual data. */
1816 output_constant (DECL_INITIAL (decl),
1817 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1818 align);
1819 else
1820 /* Leave space for it. */
1821 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1825 /* Return 1 if type TYPE contains any pointers. */
1827 static int
1828 contains_pointers_p (tree type)
1830 switch (TREE_CODE (type))
1832 case POINTER_TYPE:
1833 case REFERENCE_TYPE:
1834 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1835 so I'll play safe and return 1. */
1836 case OFFSET_TYPE:
1837 return 1;
1839 case RECORD_TYPE:
1840 case UNION_TYPE:
1841 case QUAL_UNION_TYPE:
1843 tree fields;
1844 /* For a type that has fields, see if the fields have pointers. */
1845 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1846 if (TREE_CODE (fields) == FIELD_DECL
1847 && contains_pointers_p (TREE_TYPE (fields)))
1848 return 1;
1849 return 0;
1852 case ARRAY_TYPE:
1853 /* An array type contains pointers if its element type does. */
1854 return contains_pointers_p (TREE_TYPE (type));
1856 default:
1857 return 0;
1861 /* In unit-at-a-time mode, we delay assemble_external processing until
1862 the compilation unit is finalized. This is the best we can do for
1863 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1864 it all the way to final. See PR 17982 for further discussion. */
1865 static GTY(()) tree pending_assemble_externals;
1867 #ifdef ASM_OUTPUT_EXTERNAL
1868 /* True if DECL is a function decl for which no out-of-line copy exists.
1869 It is assumed that DECL's assembler name has been set. */
1871 static bool
1872 incorporeal_function_p (tree decl)
1874 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1876 const char *name;
1878 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1879 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1880 return true;
1882 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1883 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1884 return true;
1886 return false;
1889 /* Actually do the tests to determine if this is necessary, and invoke
1890 ASM_OUTPUT_EXTERNAL. */
1891 static void
1892 assemble_external_real (tree decl)
1894 rtx rtl = DECL_RTL (decl);
1896 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1897 && !SYMBOL_REF_USED (XEXP (rtl, 0))
1898 && !incorporeal_function_p (decl))
1900 /* Some systems do require some output. */
1901 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1902 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1905 #endif
1907 void
1908 process_pending_assemble_externals (void)
1910 #ifdef ASM_OUTPUT_EXTERNAL
1911 tree list;
1912 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1913 assemble_external_real (TREE_VALUE (list));
1915 pending_assemble_externals = 0;
1916 #endif
1919 /* Output something to declare an external symbol to the assembler.
1920 (Most assemblers don't need this, so we normally output nothing.)
1921 Do nothing if DECL is not external. */
1923 void
1924 assemble_external (tree decl ATTRIBUTE_UNUSED)
1926 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1927 main body of this code is only rarely exercised. To provide some
1928 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1929 open. If it's not, we should not be calling this function. */
1930 gcc_assert (asm_out_file);
1932 #ifdef ASM_OUTPUT_EXTERNAL
1933 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1934 return;
1936 if (flag_unit_at_a_time)
1937 pending_assemble_externals = tree_cons (0, decl,
1938 pending_assemble_externals);
1939 else
1940 assemble_external_real (decl);
1941 #endif
1944 /* Similar, for calling a library function FUN. */
1946 void
1947 assemble_external_libcall (rtx fun)
1949 /* Declare library function name external when first used, if nec. */
1950 if (! SYMBOL_REF_USED (fun))
1952 SYMBOL_REF_USED (fun) = 1;
1953 targetm.asm_out.external_libcall (fun);
1957 /* Assemble a label named NAME. */
1959 void
1960 assemble_label (const char *name)
1962 ASM_OUTPUT_LABEL (asm_out_file, name);
1965 /* Set the symbol_referenced flag for ID. */
1966 void
1967 mark_referenced (tree id)
1969 TREE_SYMBOL_REFERENCED (id) = 1;
1972 /* Set the symbol_referenced flag for DECL and notify callgraph. */
1973 void
1974 mark_decl_referenced (tree decl)
1976 if (TREE_CODE (decl) == FUNCTION_DECL)
1978 /* Extern inline functions don't become needed when referenced.
1979 If we know a method will be emitted in other TU and no new
1980 functions can be marked reachable, just use the external
1981 definition. */
1982 struct cgraph_node *node = cgraph_node (decl);
1983 if (!DECL_EXTERNAL (decl)
1984 && (!node->local.vtable_method || !cgraph_global_info_ready
1985 || !node->local.finalized))
1986 cgraph_mark_needed_node (node);
1988 else if (TREE_CODE (decl) == VAR_DECL)
1990 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
1991 cgraph_varpool_mark_needed_node (node);
1992 /* C++ frontend use mark_decl_references to force COMDAT variables
1993 to be output that might appear dead otherwise. */
1994 node->force_output = true;
1996 /* else do nothing - we can get various sorts of CST nodes here,
1997 which do not need to be marked. */
2000 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2001 starts with a *, the rest of NAME is output verbatim. Otherwise
2002 NAME is transformed in a target-specific way (usually by the
2003 addition of an underscore). */
2005 void
2006 assemble_name_raw (FILE *file, const char *name)
2008 if (name[0] == '*')
2009 fputs (&name[1], file);
2010 else
2011 ASM_OUTPUT_LABELREF (file, name);
2014 /* Like assemble_name_raw, but should be used when NAME might refer to
2015 an entity that is also represented as a tree (like a function or
2016 variable). If NAME does refer to such an entity, that entity will
2017 be marked as referenced. */
2019 void
2020 assemble_name (FILE *file, const char *name)
2022 const char *real_name;
2023 tree id;
2025 real_name = targetm.strip_name_encoding (name);
2027 id = maybe_get_identifier (real_name);
2028 if (id)
2029 mark_referenced (id);
2031 assemble_name_raw (file, name);
2034 /* Allocate SIZE bytes writable static space with a gensym name
2035 and return an RTX to refer to its address. */
2038 assemble_static_space (unsigned HOST_WIDE_INT size)
2040 char name[12];
2041 const char *namestring;
2042 rtx x;
2044 #if 0
2045 if (flag_shared_data)
2046 data_section ();
2047 #endif
2049 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2050 ++const_labelno;
2051 namestring = ggc_strdup (name);
2053 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2054 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2056 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2057 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2058 BIGGEST_ALIGNMENT);
2059 #else
2060 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2061 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2062 #else
2064 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2065 so that each uninitialized object starts on such a boundary. */
2066 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2067 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2068 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2069 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2070 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2071 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2073 #endif
2074 #endif
2075 return x;
2078 /* Assemble the static constant template for function entry trampolines.
2079 This is done at most once per compilation.
2080 Returns an RTX for the address of the template. */
2082 static GTY(()) rtx initial_trampoline;
2084 #ifdef TRAMPOLINE_TEMPLATE
2086 assemble_trampoline_template (void)
2088 char label[256];
2089 const char *name;
2090 int align;
2091 rtx symbol;
2093 if (initial_trampoline)
2094 return initial_trampoline;
2096 /* By default, put trampoline templates in read-only data section. */
2098 #ifdef TRAMPOLINE_SECTION
2099 TRAMPOLINE_SECTION ();
2100 #else
2101 readonly_data_section ();
2102 #endif
2104 /* Write the assembler code to define one. */
2105 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2106 if (align > 0)
2108 ASM_OUTPUT_ALIGN (asm_out_file, align);
2111 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2112 TRAMPOLINE_TEMPLATE (asm_out_file);
2114 /* Record the rtl to refer to it. */
2115 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2116 name = ggc_strdup (label);
2117 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2118 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2120 initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2121 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2123 return initial_trampoline;
2125 #endif
2127 /* A and B are either alignments or offsets. Return the minimum alignment
2128 that may be assumed after adding the two together. */
2130 static inline unsigned
2131 min_align (unsigned int a, unsigned int b)
2133 return (a | b) & -(a | b);
2136 /* Return the assembler directive for creating a given kind of integer
2137 object. SIZE is the number of bytes in the object and ALIGNED_P
2138 indicates whether it is known to be aligned. Return NULL if the
2139 assembly dialect has no such directive.
2141 The returned string should be printed at the start of a new line and
2142 be followed immediately by the object's initial value. */
2144 const char *
2145 integer_asm_op (int size, int aligned_p)
2147 struct asm_int_op *ops;
2149 if (aligned_p)
2150 ops = &targetm.asm_out.aligned_op;
2151 else
2152 ops = &targetm.asm_out.unaligned_op;
2154 switch (size)
2156 case 1:
2157 return targetm.asm_out.byte_op;
2158 case 2:
2159 return ops->hi;
2160 case 4:
2161 return ops->si;
2162 case 8:
2163 return ops->di;
2164 case 16:
2165 return ops->ti;
2166 default:
2167 return NULL;
2171 /* Use directive OP to assemble an integer object X. Print OP at the
2172 start of the line, followed immediately by the value of X. */
2174 void
2175 assemble_integer_with_op (const char *op, rtx x)
2177 fputs (op, asm_out_file);
2178 output_addr_const (asm_out_file, x);
2179 fputc ('\n', asm_out_file);
2182 /* The default implementation of the asm_out.integer target hook. */
2184 bool
2185 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2186 unsigned int size ATTRIBUTE_UNUSED,
2187 int aligned_p ATTRIBUTE_UNUSED)
2189 const char *op = integer_asm_op (size, aligned_p);
2190 /* Avoid GAS bugs for large values. Specifically negative values whose
2191 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2192 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2193 return false;
2194 return op && (assemble_integer_with_op (op, x), true);
2197 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2198 the alignment of the integer in bits. Return 1 if we were able to output
2199 the constant, otherwise 0. We must be able to output the constant,
2200 if FORCE is nonzero. */
2202 bool
2203 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2205 int aligned_p;
2207 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2209 /* See if the target hook can handle this kind of object. */
2210 if (targetm.asm_out.integer (x, size, aligned_p))
2211 return true;
2213 /* If the object is a multi-byte one, try splitting it up. Split
2214 it into words it if is multi-word, otherwise split it into bytes. */
2215 if (size > 1)
2217 enum machine_mode omode, imode;
2218 unsigned int subalign;
2219 unsigned int subsize, i;
2221 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2222 subalign = MIN (align, subsize * BITS_PER_UNIT);
2223 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
2224 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2226 for (i = 0; i < size; i += subsize)
2228 rtx partial = simplify_subreg (omode, x, imode, i);
2229 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2230 break;
2232 if (i == size)
2233 return true;
2235 /* If we've printed some of it, but not all of it, there's no going
2236 back now. */
2237 gcc_assert (!i);
2240 gcc_assert (!force);
2242 return false;
2245 void
2246 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2248 long data[4];
2249 int i;
2250 int bitsize, nelts, nunits, units_per;
2252 /* This is hairy. We have a quantity of known size. real_to_target
2253 will put it into an array of *host* longs, 32 bits per element
2254 (even if long is more than 32 bits). We need to determine the
2255 number of array elements that are occupied (nelts) and the number
2256 of *target* min-addressable units that will be occupied in the
2257 object file (nunits). We cannot assume that 32 divides the
2258 mode's bitsize (size * BITS_PER_UNIT) evenly.
2260 size * BITS_PER_UNIT is used here to make sure that padding bits
2261 (which might appear at either end of the value; real_to_target
2262 will include the padding bits in its output array) are included. */
2264 nunits = GET_MODE_SIZE (mode);
2265 bitsize = nunits * BITS_PER_UNIT;
2266 nelts = CEIL (bitsize, 32);
2267 units_per = 32 / BITS_PER_UNIT;
2269 real_to_target (data, &d, mode);
2271 /* Put out the first word with the specified alignment. */
2272 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2273 nunits -= units_per;
2275 /* Subsequent words need only 32-bit alignment. */
2276 align = min_align (align, 32);
2278 for (i = 1; i < nelts; i++)
2280 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2281 nunits -= units_per;
2285 /* Given an expression EXP with a constant value,
2286 reduce it to the sum of an assembler symbol and an integer.
2287 Store them both in the structure *VALUE.
2288 EXP must be reducible. */
2290 struct addr_const GTY(())
2292 rtx base;
2293 HOST_WIDE_INT offset;
2296 static void
2297 decode_addr_const (tree exp, struct addr_const *value)
2299 tree target = TREE_OPERAND (exp, 0);
2300 int offset = 0;
2301 rtx x;
2303 while (1)
2305 if (TREE_CODE (target) == COMPONENT_REF
2306 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2309 offset += int_byte_position (TREE_OPERAND (target, 1));
2310 target = TREE_OPERAND (target, 0);
2312 else if (TREE_CODE (target) == ARRAY_REF
2313 || TREE_CODE (target) == ARRAY_RANGE_REF)
2315 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2316 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2317 target = TREE_OPERAND (target, 0);
2319 else
2320 break;
2323 switch (TREE_CODE (target))
2325 case VAR_DECL:
2326 case FUNCTION_DECL:
2327 x = DECL_RTL (target);
2328 break;
2330 case LABEL_DECL:
2331 x = gen_rtx_MEM (FUNCTION_MODE,
2332 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2333 break;
2335 case REAL_CST:
2336 case STRING_CST:
2337 case COMPLEX_CST:
2338 case CONSTRUCTOR:
2339 case INTEGER_CST:
2340 x = output_constant_def (target, 1);
2341 break;
2343 default:
2344 gcc_unreachable ();
2347 gcc_assert (MEM_P (x));
2348 x = XEXP (x, 0);
2350 value->base = x;
2351 value->offset = offset;
2354 /* Uniquize all constants that appear in memory.
2355 Each constant in memory thus far output is recorded
2356 in `const_desc_table'. */
2358 struct constant_descriptor_tree GTY(())
2360 /* A MEM for the constant. */
2361 rtx rtl;
2363 /* The value of the constant. */
2364 tree value;
2366 /* Hash of value. Computing the hash from value each time
2367 hashfn is called can't work properly, as that means recursive
2368 use of the hash table during hash table expansion. */
2369 hashval_t hash;
2372 static GTY((param_is (struct constant_descriptor_tree)))
2373 htab_t const_desc_htab;
2375 static struct constant_descriptor_tree * build_constant_desc (tree);
2376 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2378 /* Compute a hash code for a constant expression. */
2380 static hashval_t
2381 const_desc_hash (const void *ptr)
2383 return ((struct constant_descriptor_tree *)ptr)->hash;
2386 static hashval_t
2387 const_hash_1 (const tree exp)
2389 const char *p;
2390 hashval_t hi;
2391 int len, i;
2392 enum tree_code code = TREE_CODE (exp);
2394 /* Either set P and LEN to the address and len of something to hash and
2395 exit the switch or return a value. */
2397 switch (code)
2399 case INTEGER_CST:
2400 p = (char *) &TREE_INT_CST (exp);
2401 len = sizeof TREE_INT_CST (exp);
2402 break;
2404 case REAL_CST:
2405 return real_hash (TREE_REAL_CST_PTR (exp));
2407 case STRING_CST:
2408 p = TREE_STRING_POINTER (exp);
2409 len = TREE_STRING_LENGTH (exp);
2410 break;
2412 case COMPLEX_CST:
2413 return (const_hash_1 (TREE_REALPART (exp)) * 5
2414 + const_hash_1 (TREE_IMAGPART (exp)));
2416 case CONSTRUCTOR:
2418 unsigned HOST_WIDE_INT idx;
2419 tree value;
2421 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2423 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2424 if (value)
2425 hi = hi * 603 + const_hash_1 (value);
2427 return hi;
2430 case ADDR_EXPR:
2431 case FDESC_EXPR:
2433 struct addr_const value;
2435 decode_addr_const (exp, &value);
2436 switch (GET_CODE (value.base))
2438 case SYMBOL_REF:
2439 /* Don't hash the address of the SYMBOL_REF;
2440 only use the offset and the symbol name. */
2441 hi = value.offset;
2442 p = XSTR (value.base, 0);
2443 for (i = 0; p[i] != 0; i++)
2444 hi = ((hi * 613) + (unsigned) (p[i]));
2445 break;
2447 case LABEL_REF:
2448 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2449 break;
2451 default:
2452 gcc_unreachable ();
2455 return hi;
2457 case PLUS_EXPR:
2458 case MINUS_EXPR:
2459 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2460 + const_hash_1 (TREE_OPERAND (exp, 1)));
2462 case NOP_EXPR:
2463 case CONVERT_EXPR:
2464 case NON_LVALUE_EXPR:
2465 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2467 default:
2468 /* A language specific constant. Just hash the code. */
2469 return code;
2472 /* Compute hashing function. */
2473 hi = len;
2474 for (i = 0; i < len; i++)
2475 hi = ((hi * 613) + (unsigned) (p[i]));
2477 return hi;
2480 /* Wrapper of compare_constant, for the htab interface. */
2481 static int
2482 const_desc_eq (const void *p1, const void *p2)
2484 const struct constant_descriptor_tree *c1 = p1;
2485 const struct constant_descriptor_tree *c2 = p2;
2486 if (c1->hash != c2->hash)
2487 return 0;
2488 return compare_constant (c1->value, c2->value);
2491 /* Compare t1 and t2, and return 1 only if they are known to result in
2492 the same bit pattern on output. */
2494 static int
2495 compare_constant (const tree t1, const tree t2)
2497 enum tree_code typecode;
2499 if (t1 == NULL_TREE)
2500 return t2 == NULL_TREE;
2501 if (t2 == NULL_TREE)
2502 return 0;
2504 if (TREE_CODE (t1) != TREE_CODE (t2))
2505 return 0;
2507 switch (TREE_CODE (t1))
2509 case INTEGER_CST:
2510 /* Integer constants are the same only if the same width of type. */
2511 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2512 return 0;
2513 return tree_int_cst_equal (t1, t2);
2515 case REAL_CST:
2516 /* Real constants are the same only if the same width of type. */
2517 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2518 return 0;
2520 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2522 case STRING_CST:
2523 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2524 return 0;
2526 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2527 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2528 TREE_STRING_LENGTH (t1)));
2530 case COMPLEX_CST:
2531 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2532 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2534 case CONSTRUCTOR:
2536 VEC(constructor_elt, gc) *v1, *v2;
2537 unsigned HOST_WIDE_INT idx;
2539 typecode = TREE_CODE (TREE_TYPE (t1));
2540 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2541 return 0;
2543 if (typecode == ARRAY_TYPE)
2545 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2546 /* For arrays, check that the sizes all match. */
2547 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2548 || size_1 == -1
2549 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2550 return 0;
2552 else
2554 /* For record and union constructors, require exact type
2555 equality. */
2556 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2557 return 0;
2560 v1 = CONSTRUCTOR_ELTS (t1);
2561 v2 = CONSTRUCTOR_ELTS (t2);
2562 if (VEC_length (constructor_elt, v1)
2563 != VEC_length (constructor_elt, v2))
2564 return 0;
2566 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2568 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2569 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2571 /* Check that each value is the same... */
2572 if (!compare_constant (c1->value, c2->value))
2573 return 0;
2574 /* ... and that they apply to the same fields! */
2575 if (typecode == ARRAY_TYPE)
2577 if (!compare_constant (c1->index, c2->index))
2578 return 0;
2580 else
2582 if (c1->index != c2->index)
2583 return 0;
2587 return 1;
2590 case ADDR_EXPR:
2591 case FDESC_EXPR:
2593 struct addr_const value1, value2;
2595 decode_addr_const (t1, &value1);
2596 decode_addr_const (t2, &value2);
2597 return (value1.offset == value2.offset
2598 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2601 case PLUS_EXPR:
2602 case MINUS_EXPR:
2603 case RANGE_EXPR:
2604 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2605 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2607 case NOP_EXPR:
2608 case CONVERT_EXPR:
2609 case NON_LVALUE_EXPR:
2610 case VIEW_CONVERT_EXPR:
2611 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2613 default:
2615 tree nt1, nt2;
2616 nt1 = lang_hooks.expand_constant (t1);
2617 nt2 = lang_hooks.expand_constant (t2);
2618 if (nt1 != t1 || nt2 != t2)
2619 return compare_constant (nt1, nt2);
2620 else
2621 return 0;
2625 gcc_unreachable ();
2628 /* Make a copy of the whole tree structure for a constant. This
2629 handles the same types of nodes that compare_constant handles. */
2631 static tree
2632 copy_constant (tree exp)
2634 switch (TREE_CODE (exp))
2636 case ADDR_EXPR:
2637 /* For ADDR_EXPR, we do not want to copy the decl whose address
2638 is requested. We do want to copy constants though. */
2639 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2640 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2641 copy_constant (TREE_OPERAND (exp, 0)));
2642 else
2643 return copy_node (exp);
2645 case INTEGER_CST:
2646 case REAL_CST:
2647 case STRING_CST:
2648 return copy_node (exp);
2650 case COMPLEX_CST:
2651 return build_complex (TREE_TYPE (exp),
2652 copy_constant (TREE_REALPART (exp)),
2653 copy_constant (TREE_IMAGPART (exp)));
2655 case PLUS_EXPR:
2656 case MINUS_EXPR:
2657 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2658 copy_constant (TREE_OPERAND (exp, 0)),
2659 copy_constant (TREE_OPERAND (exp, 1)));
2661 case NOP_EXPR:
2662 case CONVERT_EXPR:
2663 case NON_LVALUE_EXPR:
2664 case VIEW_CONVERT_EXPR:
2665 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2666 copy_constant (TREE_OPERAND (exp, 0)));
2668 case CONSTRUCTOR:
2670 tree copy = copy_node (exp);
2671 VEC(constructor_elt, gc) *v;
2672 unsigned HOST_WIDE_INT idx;
2673 tree purpose, value;
2675 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2676 CONSTRUCTOR_ELTS (exp)));
2677 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2679 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2680 ce->index = purpose;
2681 ce->value = copy_constant (value);
2683 CONSTRUCTOR_ELTS (copy) = v;
2684 return copy;
2687 default:
2689 tree t = lang_hooks.expand_constant (exp);
2691 gcc_assert (t == exp);
2692 return copy_constant (t);
2697 /* Subroutine of output_constant_def:
2698 No constant equal to EXP is known to have been output.
2699 Make a constant descriptor to enter EXP in the hash table.
2700 Assign the label number and construct RTL to refer to the
2701 constant's location in memory.
2702 Caller is responsible for updating the hash table. */
2704 static struct constant_descriptor_tree *
2705 build_constant_desc (tree exp)
2707 rtx symbol;
2708 rtx rtl;
2709 char label[256];
2710 int labelno;
2711 struct constant_descriptor_tree *desc;
2713 desc = ggc_alloc (sizeof (*desc));
2714 desc->value = copy_constant (exp);
2716 /* Propagate marked-ness to copied constant. */
2717 if (flag_mudflap && mf_marked_p (exp))
2718 mf_mark (desc->value);
2720 /* Create a string containing the label name, in LABEL. */
2721 labelno = const_labelno++;
2722 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2724 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
2725 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2726 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2727 SYMBOL_REF_DECL (symbol) = desc->value;
2728 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2730 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2731 set_mem_attributes (rtl, exp, 1);
2732 set_mem_alias_set (rtl, 0);
2733 set_mem_alias_set (rtl, const_alias_set);
2735 /* Set flags or add text to the name to record information, such as
2736 that it is a local symbol. If the name is changed, the macro
2737 ASM_OUTPUT_LABELREF will have to know how to strip this
2738 information. This call might invalidate our local variable
2739 SYMBOL; we can't use it afterward. */
2741 targetm.encode_section_info (exp, rtl, true);
2743 desc->rtl = rtl;
2745 return desc;
2748 /* Return an rtx representing a reference to constant data in memory
2749 for the constant expression EXP.
2751 If assembler code for such a constant has already been output,
2752 return an rtx to refer to it.
2753 Otherwise, output such a constant in memory
2754 and generate an rtx for it.
2756 If DEFER is nonzero, this constant can be deferred and output only
2757 if referenced in the function after all optimizations.
2759 `const_desc_table' records which constants already have label strings. */
2762 output_constant_def (tree exp, int defer)
2764 struct constant_descriptor_tree *desc;
2765 struct constant_descriptor_tree key;
2766 void **loc;
2768 /* Look up EXP in the table of constant descriptors. If we didn't find
2769 it, create a new one. */
2770 key.value = exp;
2771 key.hash = const_hash_1 (exp);
2772 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
2774 desc = *loc;
2775 if (desc == 0)
2777 desc = build_constant_desc (exp);
2778 desc->hash = key.hash;
2779 *loc = desc;
2782 maybe_output_constant_def_contents (desc, defer);
2783 return desc->rtl;
2786 /* Subroutine of output_constant_def: Decide whether or not we need to
2787 output the constant DESC now, and if so, do it. */
2788 static void
2789 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2790 int defer)
2792 rtx symbol = XEXP (desc->rtl, 0);
2793 tree exp = desc->value;
2795 if (flag_syntax_only)
2796 return;
2798 if (TREE_ASM_WRITTEN (exp))
2799 /* Already output; don't do it again. */
2800 return;
2802 /* We can always defer constants as long as the context allows
2803 doing so. */
2804 if (defer)
2806 /* Increment n_deferred_constants if it exists. It needs to be at
2807 least as large as the number of constants actually referred to
2808 by the function. If it's too small we'll stop looking too early
2809 and fail to emit constants; if it's too large we'll only look
2810 through the entire function when we could have stopped earlier. */
2811 if (cfun)
2812 n_deferred_constants++;
2813 return;
2816 output_constant_def_contents (symbol);
2819 /* We must output the constant data referred to by SYMBOL; do so. */
2821 static void
2822 output_constant_def_contents (rtx symbol)
2824 tree exp = SYMBOL_REF_DECL (symbol);
2825 const char *label = XSTR (symbol, 0);
2826 HOST_WIDE_INT size;
2828 /* Make sure any other constants whose addresses appear in EXP
2829 are assigned label numbers. */
2830 int reloc = compute_reloc_for_constant (exp);
2832 /* Align the location counter as required by EXP's data type. */
2833 unsigned int align = TYPE_ALIGN (TREE_TYPE (exp));
2834 #ifdef CONSTANT_ALIGNMENT
2835 align = CONSTANT_ALIGNMENT (exp, align);
2836 #endif
2838 output_addressed_constants (exp);
2840 /* We are no longer deferring this constant. */
2841 TREE_ASM_WRITTEN (exp) = 1;
2843 if (IN_NAMED_SECTION (exp))
2844 named_section (exp, NULL, reloc);
2845 else
2846 targetm.asm_out.select_section (exp, reloc, align);
2848 if (align > BITS_PER_UNIT)
2850 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2853 size = int_size_in_bytes (TREE_TYPE (exp));
2854 if (TREE_CODE (exp) == STRING_CST)
2855 size = MAX (TREE_STRING_LENGTH (exp), size);
2857 /* Do any machine/system dependent processing of the constant. */
2858 #ifdef ASM_DECLARE_CONSTANT_NAME
2859 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2860 #else
2861 /* Standard thing is just output label for the constant. */
2862 ASM_OUTPUT_LABEL (asm_out_file, label);
2863 #endif /* ASM_DECLARE_CONSTANT_NAME */
2865 /* Output the value of EXP. */
2866 output_constant (exp, size, align);
2867 if (flag_mudflap)
2868 mudflap_enqueue_constant (exp);
2871 /* Look up EXP in the table of constant descriptors. Return the rtl
2872 if it has been emitted, else null. */
2875 lookup_constant_def (tree exp)
2877 struct constant_descriptor_tree *desc;
2878 struct constant_descriptor_tree key;
2880 key.value = exp;
2881 key.hash = const_hash_1 (exp);
2882 desc = htab_find_with_hash (const_desc_htab, &key, key.hash);
2884 return (desc ? desc->rtl : NULL_RTX);
2887 /* Used in the hash tables to avoid outputting the same constant
2888 twice. Unlike 'struct constant_descriptor_tree', RTX constants
2889 are output once per function, not once per file. */
2890 /* ??? Only a few targets need per-function constant pools. Most
2891 can use one per-file pool. Should add a targetm bit to tell the
2892 difference. */
2894 struct rtx_constant_pool GTY(())
2896 /* Pointers to first and last constant in pool, as ordered by offset. */
2897 struct constant_descriptor_rtx *first;
2898 struct constant_descriptor_rtx *last;
2900 /* Hash facility for making memory-constants from constant rtl-expressions.
2901 It is used on RISC machines where immediate integer arguments and
2902 constant addresses are restricted so that such constants must be stored
2903 in memory. */
2904 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
2905 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_sym_htab;
2907 /* Current offset in constant pool (does not include any
2908 machine-specific header). */
2909 HOST_WIDE_INT offset;
2912 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
2914 struct constant_descriptor_rtx *next;
2915 rtx mem;
2916 rtx sym;
2917 rtx constant;
2918 HOST_WIDE_INT offset;
2919 hashval_t hash;
2920 enum machine_mode mode;
2921 unsigned int align;
2922 int labelno;
2923 int mark;
2926 /* Hash and compare functions for const_rtx_htab. */
2928 static hashval_t
2929 const_desc_rtx_hash (const void *ptr)
2931 const struct constant_descriptor_rtx *desc = ptr;
2932 return desc->hash;
2935 static int
2936 const_desc_rtx_eq (const void *a, const void *b)
2938 const struct constant_descriptor_rtx *x = a;
2939 const struct constant_descriptor_rtx *y = b;
2941 if (x->mode != y->mode)
2942 return 0;
2943 return rtx_equal_p (x->constant, y->constant);
2946 /* Hash and compare functions for const_rtx_sym_htab. */
2948 static hashval_t
2949 const_desc_rtx_sym_hash (const void *ptr)
2951 const struct constant_descriptor_rtx *desc = ptr;
2952 return htab_hash_string (XSTR (desc->sym, 0));
2955 static int
2956 const_desc_rtx_sym_eq (const void *a, const void *b)
2958 const struct constant_descriptor_rtx *x = a;
2959 const struct constant_descriptor_rtx *y = b;
2960 return XSTR (x->sym, 0) == XSTR (y->sym, 0);
2963 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
2965 static int
2966 const_rtx_hash_1 (rtx *xp, void *data)
2968 unsigned HOST_WIDE_INT hwi;
2969 enum machine_mode mode;
2970 enum rtx_code code;
2971 hashval_t h, *hp;
2972 rtx x;
2974 x = *xp;
2975 code = GET_CODE (x);
2976 mode = GET_MODE (x);
2977 h = (hashval_t) code * 1048573 + mode;
2979 switch (code)
2981 case CONST_INT:
2982 hwi = INTVAL (x);
2983 fold_hwi:
2985 const int shift = sizeof (hashval_t) * CHAR_BIT;
2986 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
2987 int i;
2989 h ^= (hashval_t) hwi;
2990 for (i = 1; i < n; ++i)
2992 hwi >>= shift;
2993 h ^= (hashval_t) hwi;
2996 break;
2998 case CONST_DOUBLE:
2999 if (mode == VOIDmode)
3001 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3002 goto fold_hwi;
3004 else
3005 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3006 break;
3008 case CONST_VECTOR:
3010 int i;
3011 for (i = XVECLEN (x, 0); i-- > 0; )
3012 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
3014 break;
3016 case SYMBOL_REF:
3017 h ^= htab_hash_string (XSTR (x, 0));
3018 break;
3020 case LABEL_REF:
3021 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
3022 break;
3024 case UNSPEC:
3025 case UNSPEC_VOLATILE:
3026 h = h * 251 + XINT (x, 1);
3027 break;
3029 default:
3030 break;
3033 hp = data;
3034 *hp = *hp * 509 + h;
3035 return 0;
3038 /* Compute a hash value for X, which should be a constant. */
3040 static hashval_t
3041 const_rtx_hash (rtx x)
3043 hashval_t h = 0;
3044 for_each_rtx (&x, const_rtx_hash_1, &h);
3045 return h;
3049 /* Initialize constant pool hashing for a new function. */
3051 void
3052 init_varasm_status (struct function *f)
3054 struct varasm_status *p;
3055 struct rtx_constant_pool *pool;
3057 p = ggc_alloc (sizeof (struct varasm_status));
3058 f->varasm = p;
3060 pool = ggc_alloc (sizeof (struct rtx_constant_pool));
3061 p->pool = pool;
3062 p->deferred_constants = 0;
3064 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3065 const_desc_rtx_eq, NULL);
3066 pool->const_rtx_sym_htab = htab_create_ggc (31, const_desc_rtx_sym_hash,
3067 const_desc_rtx_sym_eq, NULL);
3068 pool->first = pool->last = NULL;
3069 pool->offset = 0;
3072 /* Given a MINUS expression, simplify it if both sides
3073 include the same symbol. */
3076 simplify_subtraction (rtx x)
3078 rtx r = simplify_rtx (x);
3079 return r ? r : x;
3082 /* Given a constant rtx X, make (or find) a memory constant for its value
3083 and return a MEM rtx to refer to it in memory. */
3086 force_const_mem (enum machine_mode mode, rtx x)
3088 struct constant_descriptor_rtx *desc, tmp;
3089 struct rtx_constant_pool *pool = cfun->varasm->pool;
3090 char label[256];
3091 rtx def, symbol;
3092 hashval_t hash;
3093 unsigned int align;
3094 void **slot;
3096 /* If we're not allowed to drop X into the constant pool, don't. */
3097 if (targetm.cannot_force_const_mem (x))
3098 return NULL_RTX;
3100 /* Lookup the value in the hashtable. */
3101 tmp.constant = x;
3102 tmp.mode = mode;
3103 hash = const_rtx_hash (x);
3104 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3105 desc = *slot;
3107 /* If the constant was already present, return its memory. */
3108 if (desc)
3109 return copy_rtx (desc->mem);
3111 /* Otherwise, create a new descriptor. */
3112 desc = ggc_alloc (sizeof (*desc));
3113 *slot = desc;
3115 /* Align the location counter as required by EXP's data type. */
3116 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3117 #ifdef CONSTANT_ALIGNMENT
3119 tree type = lang_hooks.types.type_for_mode (mode, 0);
3120 if (type != NULL_TREE)
3121 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3123 #endif
3125 pool->offset += (align / BITS_PER_UNIT) - 1;
3126 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3128 desc->next = NULL;
3129 desc->constant = tmp.constant;
3130 desc->offset = pool->offset;
3131 desc->hash = hash;
3132 desc->mode = mode;
3133 desc->align = align;
3134 desc->labelno = const_labelno;
3135 desc->mark = 0;
3137 pool->offset += GET_MODE_SIZE (mode);
3138 if (pool->last)
3139 pool->last->next = desc;
3140 else
3141 pool->first = pool->last = desc;
3142 pool->last = desc;
3144 /* Create a string containing the label name, in LABEL. */
3145 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3146 ++const_labelno;
3148 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3149 the constants pool. */
3150 desc->sym = symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3151 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
3152 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3153 current_function_uses_const_pool = 1;
3155 /* Insert the descriptor into the symbol cross-reference table too. */
3156 slot = htab_find_slot (pool->const_rtx_sym_htab, desc, INSERT);
3157 gcc_assert (!*slot);
3158 *slot = desc;
3160 /* Construct the MEM. */
3161 desc->mem = def = gen_const_mem (mode, symbol);
3162 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3163 set_mem_align (def, align);
3165 /* If we're dropping a label to the constant pool, make sure we
3166 don't delete it. */
3167 if (GET_CODE (x) == LABEL_REF)
3168 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3170 return copy_rtx (def);
3173 /* Given a SYMBOL_REF with CONSTANT_POOL_ADDRESS_P true, return a pointer to
3174 the corresponding constant_descriptor_rtx structure. */
3176 static struct constant_descriptor_rtx *
3177 find_pool_constant (struct rtx_constant_pool *pool, rtx sym)
3179 struct constant_descriptor_rtx tmp;
3180 tmp.sym = sym;
3181 return htab_find (pool->const_rtx_sym_htab, &tmp);
3184 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3187 get_pool_constant (rtx addr)
3189 return find_pool_constant (cfun->varasm->pool, addr)->constant;
3192 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3193 and whether it has been output or not. */
3196 get_pool_constant_mark (rtx addr, bool *pmarked)
3198 struct constant_descriptor_rtx *desc;
3200 desc = find_pool_constant (cfun->varasm->pool, addr);
3201 *pmarked = (desc->mark != 0);
3202 return desc->constant;
3205 /* Likewise, but for the constant pool of a specific function. */
3208 get_pool_constant_for_function (struct function *f, rtx addr)
3210 return find_pool_constant (f->varasm->pool, addr)->constant;
3213 /* Similar, return the mode. */
3215 enum machine_mode
3216 get_pool_mode (rtx addr)
3218 return find_pool_constant (cfun->varasm->pool, addr)->mode;
3221 /* Return the size of the constant pool. */
3224 get_pool_size (void)
3226 return cfun->varasm->pool->offset;
3229 /* Worker function for output_constant_pool_1. Emit assembly for X
3230 in MODE with known alignment ALIGN. */
3232 static void
3233 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3235 switch (GET_MODE_CLASS (mode))
3237 case MODE_FLOAT:
3239 REAL_VALUE_TYPE r;
3241 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3242 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3243 assemble_real (r, mode, align);
3244 break;
3247 case MODE_INT:
3248 case MODE_PARTIAL_INT:
3249 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3250 break;
3252 case MODE_VECTOR_FLOAT:
3253 case MODE_VECTOR_INT:
3255 int i, units;
3256 enum machine_mode submode = GET_MODE_INNER (mode);
3257 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3259 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3260 units = CONST_VECTOR_NUNITS (x);
3262 for (i = 0; i < units; i++)
3264 rtx elt = CONST_VECTOR_ELT (x, i);
3265 output_constant_pool_2 (submode, elt, i ? subalign : align);
3268 break;
3270 default:
3271 gcc_unreachable ();
3275 /* Worker function for output_constant_pool. Emit POOL. */
3277 static void
3278 output_constant_pool_1 (struct constant_descriptor_rtx *desc)
3280 rtx x, tmp;
3282 if (!desc->mark)
3283 return;
3284 x = desc->constant;
3286 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3287 whose CODE_LABEL has been deleted. This can occur if a jump table
3288 is eliminated by optimization. If so, write a constant of zero
3289 instead. Note that this can also happen by turning the
3290 CODE_LABEL into a NOTE. */
3291 /* ??? This seems completely and utterly wrong. Certainly it's
3292 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3293 functioning even with INSN_DELETED_P and friends. */
3295 tmp = x;
3296 switch (GET_CODE (x))
3298 case CONST:
3299 if (GET_CODE (XEXP (x, 0)) != PLUS
3300 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3301 break;
3302 tmp = XEXP (XEXP (x, 0), 0);
3303 /* FALLTHRU */
3305 case LABEL_REF:
3306 tmp = XEXP (x, 0);
3307 gcc_assert (!INSN_DELETED_P (tmp));
3308 gcc_assert (!NOTE_P (tmp)
3309 || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3310 break;
3312 default:
3313 break;
3316 /* First switch to correct section. */
3317 targetm.asm_out.select_rtx_section (desc->mode, x, desc->align);
3319 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3320 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3321 desc->align, desc->labelno, done);
3322 #endif
3324 assemble_align (desc->align);
3326 /* Output the label. */
3327 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3329 /* Output the data. */
3330 output_constant_pool_2 (desc->mode, x, desc->align);
3332 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3333 sections have proper size. */
3334 if (desc->align > GET_MODE_BITSIZE (desc->mode)
3335 && in_section == in_named
3336 && get_named_section_flags (in_named_name) & SECTION_MERGE)
3337 assemble_align (desc->align);
3339 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3340 done:
3341 #endif
3342 return;
3345 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3346 to as used. Emit referenced deferred strings. This function can
3347 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3349 static int
3350 mark_constant (rtx *current_rtx, void *data)
3352 struct rtx_constant_pool *pool = data;
3353 rtx x = *current_rtx;
3355 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3356 return 0;
3358 if (CONSTANT_POOL_ADDRESS_P (x))
3360 struct constant_descriptor_rtx *desc = find_pool_constant (pool, x);
3361 if (desc->mark == 0)
3363 desc->mark = 1;
3364 for_each_rtx (&desc->constant, mark_constant, pool);
3367 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3369 tree exp = SYMBOL_REF_DECL (x);
3370 if (!TREE_ASM_WRITTEN (exp))
3372 n_deferred_constants--;
3373 output_constant_def_contents (x);
3377 return -1;
3380 /* Look through appropriate parts of INSN, marking all entries in the
3381 constant pool which are actually being used. Entries that are only
3382 referenced by other constants are also marked as used. Emit
3383 deferred strings that are used. */
3385 static void
3386 mark_constants (struct rtx_constant_pool *pool, rtx insn)
3388 if (!INSN_P (insn))
3389 return;
3391 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3392 insns, not any notes that may be attached. We don't want to mark
3393 a constant just because it happens to appear in a REG_EQUIV note. */
3394 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3396 rtx seq = PATTERN (insn);
3397 int i, n = XVECLEN (seq, 0);
3398 for (i = 0; i < n; ++i)
3400 rtx subinsn = XVECEXP (seq, 0, i);
3401 if (INSN_P (subinsn))
3402 for_each_rtx (&PATTERN (subinsn), mark_constant, pool);
3405 else
3406 for_each_rtx (&PATTERN (insn), mark_constant, pool);
3409 /* Look through the instructions for this function, and mark all the
3410 entries in POOL which are actually being used. Emit deferred constants
3411 which have indeed been used. */
3413 static void
3414 mark_constant_pool (struct rtx_constant_pool *pool)
3416 rtx insn, link;
3418 if (pool->first == 0 && n_deferred_constants == 0)
3419 return;
3421 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3422 mark_constants (pool, insn);
3424 for (link = current_function_epilogue_delay_list;
3425 link;
3426 link = XEXP (link, 1))
3427 mark_constants (pool, XEXP (link, 0));
3430 /* Write all the constants in the constant pool. */
3432 void
3433 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3434 tree fndecl ATTRIBUTE_UNUSED)
3436 struct rtx_constant_pool *pool = cfun->varasm->pool;
3437 struct constant_descriptor_rtx *desc;
3439 /* It is possible for gcc to call force_const_mem and then to later
3440 discard the instructions which refer to the constant. In such a
3441 case we do not need to output the constant. */
3442 mark_constant_pool (pool);
3444 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3445 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3446 #endif
3448 for (desc = pool->first; desc ; desc = desc->next)
3449 output_constant_pool_1 (desc);
3451 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3452 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3453 #endif
3456 /* Determine what kind of relocations EXP may need. */
3459 compute_reloc_for_constant (tree exp)
3461 int reloc = 0, reloc2;
3462 tree tem;
3464 /* Give the front-end a chance to convert VALUE to something that
3465 looks more like a constant to the back-end. */
3466 exp = lang_hooks.expand_constant (exp);
3468 switch (TREE_CODE (exp))
3470 case ADDR_EXPR:
3471 case FDESC_EXPR:
3472 /* Go inside any operations that get_inner_reference can handle and see
3473 if what's inside is a constant: no need to do anything here for
3474 addresses of variables or functions. */
3475 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3476 tem = TREE_OPERAND (tem, 0))
3479 if (TREE_PUBLIC (tem))
3480 reloc |= 2;
3481 else
3482 reloc |= 1;
3483 break;
3485 case PLUS_EXPR:
3486 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3487 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3488 break;
3490 case MINUS_EXPR:
3491 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3492 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3493 /* The difference of two local labels is computable at link time. */
3494 if (reloc == 1 && reloc2 == 1)
3495 reloc = 0;
3496 else
3497 reloc |= reloc2;
3498 break;
3500 case NOP_EXPR:
3501 case CONVERT_EXPR:
3502 case NON_LVALUE_EXPR:
3503 case VIEW_CONVERT_EXPR:
3504 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3505 break;
3507 case CONSTRUCTOR:
3509 unsigned HOST_WIDE_INT idx;
3510 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3511 if (tem != 0)
3512 reloc |= compute_reloc_for_constant (tem);
3514 break;
3516 default:
3517 break;
3519 return reloc;
3522 /* Find all the constants whose addresses are referenced inside of EXP,
3523 and make sure assembler code with a label has been output for each one.
3524 Indicate whether an ADDR_EXPR has been encountered. */
3526 static void
3527 output_addressed_constants (tree exp)
3529 tree tem;
3531 /* Give the front-end a chance to convert VALUE to something that
3532 looks more like a constant to the back-end. */
3533 exp = lang_hooks.expand_constant (exp);
3535 switch (TREE_CODE (exp))
3537 case ADDR_EXPR:
3538 case FDESC_EXPR:
3539 /* Go inside any operations that get_inner_reference can handle and see
3540 if what's inside is a constant: no need to do anything here for
3541 addresses of variables or functions. */
3542 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3543 tem = TREE_OPERAND (tem, 0))
3546 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3547 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3548 tem = DECL_INITIAL (tem);
3550 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3551 output_constant_def (tem, 0);
3552 break;
3554 case PLUS_EXPR:
3555 case MINUS_EXPR:
3556 output_addressed_constants (TREE_OPERAND (exp, 1));
3557 /* Fall through. */
3559 case NOP_EXPR:
3560 case CONVERT_EXPR:
3561 case NON_LVALUE_EXPR:
3562 case VIEW_CONVERT_EXPR:
3563 output_addressed_constants (TREE_OPERAND (exp, 0));
3564 break;
3566 case CONSTRUCTOR:
3568 unsigned HOST_WIDE_INT idx;
3569 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3570 if (tem != 0)
3571 output_addressed_constants (tem);
3573 break;
3575 default:
3576 break;
3580 /* Return nonzero if VALUE is a valid constant-valued expression
3581 for use in initializing a static variable; one that can be an
3582 element of a "constant" initializer.
3584 Return null_pointer_node if the value is absolute;
3585 if it is relocatable, return the variable that determines the relocation.
3586 We assume that VALUE has been folded as much as possible;
3587 therefore, we do not need to check for such things as
3588 arithmetic-combinations of integers. */
3590 tree
3591 initializer_constant_valid_p (tree value, tree endtype)
3593 /* Give the front-end a chance to convert VALUE to something that
3594 looks more like a constant to the back-end. */
3595 value = lang_hooks.expand_constant (value);
3597 switch (TREE_CODE (value))
3599 case CONSTRUCTOR:
3600 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3601 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3602 && TREE_CONSTANT (value)
3603 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (value)))
3605 unsigned HOST_WIDE_INT idx;
3606 tree elt;
3607 bool absolute = true;
3609 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
3611 tree reloc;
3612 reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
3613 if (!reloc)
3614 return NULL_TREE;
3615 if (reloc != null_pointer_node)
3616 absolute = false;
3618 /* For a non-absolute relocation, there is no single
3619 variable that can be "the variable that determines the
3620 relocation." */
3621 return absolute ? null_pointer_node : error_mark_node;
3624 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3626 case INTEGER_CST:
3627 case VECTOR_CST:
3628 case REAL_CST:
3629 case STRING_CST:
3630 case COMPLEX_CST:
3631 return null_pointer_node;
3633 case ADDR_EXPR:
3634 case FDESC_EXPR:
3635 value = staticp (TREE_OPERAND (value, 0));
3636 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out to
3637 be a constant, this is old-skool offsetof-like nonsense. */
3638 if (value
3639 && TREE_CODE (value) == INDIRECT_REF
3640 && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3641 return null_pointer_node;
3642 /* Taking the address of a nested function involves a trampoline. */
3643 if (value
3644 && TREE_CODE (value) == FUNCTION_DECL
3645 && ((decl_function_context (value) && !DECL_NO_STATIC_CHAIN (value))
3646 || DECL_NON_ADDR_CONST_P (value)))
3647 return NULL_TREE;
3648 return value;
3650 case VIEW_CONVERT_EXPR:
3651 case NON_LVALUE_EXPR:
3652 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3654 case CONVERT_EXPR:
3655 case NOP_EXPR:
3657 tree src;
3658 tree src_type;
3659 tree dest_type;
3661 src = TREE_OPERAND (value, 0);
3662 src_type = TREE_TYPE (src);
3663 dest_type = TREE_TYPE (value);
3665 /* Allow conversions between pointer types, floating-point
3666 types, and offset types. */
3667 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3668 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3669 || (TREE_CODE (dest_type) == OFFSET_TYPE
3670 && TREE_CODE (src_type) == OFFSET_TYPE))
3671 return initializer_constant_valid_p (src, endtype);
3673 /* Allow length-preserving conversions between integer types. */
3674 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3675 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3676 return initializer_constant_valid_p (src, endtype);
3678 /* Allow conversions between other integer types only if
3679 explicit value. */
3680 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3682 tree inner = initializer_constant_valid_p (src, endtype);
3683 if (inner == null_pointer_node)
3684 return null_pointer_node;
3685 break;
3688 /* Allow (int) &foo provided int is as wide as a pointer. */
3689 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3690 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3691 return initializer_constant_valid_p (src, endtype);
3693 /* Likewise conversions from int to pointers, but also allow
3694 conversions from 0. */
3695 if ((POINTER_TYPE_P (dest_type)
3696 || TREE_CODE (dest_type) == OFFSET_TYPE)
3697 && INTEGRAL_TYPE_P (src_type))
3699 if (integer_zerop (src))
3700 return null_pointer_node;
3701 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3702 return initializer_constant_valid_p (src, endtype);
3705 /* Allow conversions to struct or union types if the value
3706 inside is okay. */
3707 if (TREE_CODE (dest_type) == RECORD_TYPE
3708 || TREE_CODE (dest_type) == UNION_TYPE)
3709 return initializer_constant_valid_p (src, endtype);
3711 break;
3713 case PLUS_EXPR:
3714 if (! INTEGRAL_TYPE_P (endtype)
3715 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3717 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3718 endtype);
3719 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3720 endtype);
3721 /* If either term is absolute, use the other terms relocation. */
3722 if (valid0 == null_pointer_node)
3723 return valid1;
3724 if (valid1 == null_pointer_node)
3725 return valid0;
3727 break;
3729 case MINUS_EXPR:
3730 if (! INTEGRAL_TYPE_P (endtype)
3731 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3733 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3734 endtype);
3735 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3736 endtype);
3737 /* Win if second argument is absolute. */
3738 if (valid1 == null_pointer_node)
3739 return valid0;
3740 /* Win if both arguments have the same relocation.
3741 Then the value is absolute. */
3742 if (valid0 == valid1 && valid0 != 0)
3743 return null_pointer_node;
3745 /* Since GCC guarantees that string constants are unique in the
3746 generated code, a subtraction between two copies of the same
3747 constant string is absolute. */
3748 if (valid0 && TREE_CODE (valid0) == STRING_CST
3749 && valid1 && TREE_CODE (valid1) == STRING_CST
3750 && operand_equal_p (valid0, valid1, 1))
3751 return null_pointer_node;
3754 /* Support narrowing differences. */
3755 if (INTEGRAL_TYPE_P (endtype))
3757 tree op0, op1;
3759 op0 = TREE_OPERAND (value, 0);
3760 op1 = TREE_OPERAND (value, 1);
3762 /* Like STRIP_NOPS except allow the operand mode to widen.
3763 This works around a feature of fold that simplifies
3764 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3765 that the narrower operation is cheaper. */
3767 while (TREE_CODE (op0) == NOP_EXPR
3768 || TREE_CODE (op0) == CONVERT_EXPR
3769 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3771 tree inner = TREE_OPERAND (op0, 0);
3772 if (inner == error_mark_node
3773 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3774 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3775 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3776 break;
3777 op0 = inner;
3780 while (TREE_CODE (op1) == NOP_EXPR
3781 || TREE_CODE (op1) == CONVERT_EXPR
3782 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3784 tree inner = TREE_OPERAND (op1, 0);
3785 if (inner == error_mark_node
3786 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3787 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3788 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3789 break;
3790 op1 = inner;
3793 op0 = initializer_constant_valid_p (op0, endtype);
3794 op1 = initializer_constant_valid_p (op1, endtype);
3796 /* Both initializers must be known. */
3797 if (op0 && op1)
3799 if (op0 == op1)
3800 return null_pointer_node;
3802 /* Support differences between labels. */
3803 if (TREE_CODE (op0) == LABEL_DECL
3804 && TREE_CODE (op1) == LABEL_DECL)
3805 return null_pointer_node;
3807 if (TREE_CODE (op0) == STRING_CST
3808 && TREE_CODE (op1) == STRING_CST
3809 && operand_equal_p (op0, op1, 1))
3810 return null_pointer_node;
3813 break;
3815 default:
3816 break;
3819 return 0;
3822 /* Output assembler code for constant EXP to FILE, with no label.
3823 This includes the pseudo-op such as ".int" or ".byte", and a newline.
3824 Assumes output_addressed_constants has been done on EXP already.
3826 Generate exactly SIZE bytes of assembler data, padding at the end
3827 with zeros if necessary. SIZE must always be specified.
3829 SIZE is important for structure constructors,
3830 since trailing members may have been omitted from the constructor.
3831 It is also important for initialization of arrays from string constants
3832 since the full length of the string constant might not be wanted.
3833 It is also needed for initialization of unions, where the initializer's
3834 type is just one member, and that may not be as long as the union.
3836 There a case in which we would fail to output exactly SIZE bytes:
3837 for a structure constructor that wants to produce more than SIZE bytes.
3838 But such constructors will never be generated for any possible input.
3840 ALIGN is the alignment of the data in bits. */
3842 void
3843 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3845 enum tree_code code;
3846 unsigned HOST_WIDE_INT thissize;
3848 /* Some front-ends use constants other than the standard language-independent
3849 varieties, but which may still be output directly. Give the front-end a
3850 chance to convert EXP to a language-independent representation. */
3851 exp = lang_hooks.expand_constant (exp);
3853 if (size == 0 || flag_syntax_only)
3854 return;
3856 /* See if we're trying to initialize a pointer in a non-default mode
3857 to the address of some declaration somewhere. If the target says
3858 the mode is valid for pointers, assume the target has a way of
3859 resolving it. */
3860 if (TREE_CODE (exp) == NOP_EXPR
3861 && POINTER_TYPE_P (TREE_TYPE (exp))
3862 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3864 tree saved_type = TREE_TYPE (exp);
3866 /* Peel off any intermediate conversions-to-pointer for valid
3867 pointer modes. */
3868 while (TREE_CODE (exp) == NOP_EXPR
3869 && POINTER_TYPE_P (TREE_TYPE (exp))
3870 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3871 exp = TREE_OPERAND (exp, 0);
3873 /* If what we're left with is the address of something, we can
3874 convert the address to the final type and output it that
3875 way. */
3876 if (TREE_CODE (exp) == ADDR_EXPR)
3877 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
3880 /* Eliminate any conversions since we'll be outputting the underlying
3881 constant. */
3882 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
3883 || TREE_CODE (exp) == NON_LVALUE_EXPR
3884 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
3886 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
3887 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
3889 /* Make sure eliminating the conversion is really a no-op, except with
3890 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
3891 union types to allow for Ada unchecked unions. */
3892 if (type_size != op_size
3893 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
3894 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
3895 internal_error ("no-op convert from %wd to %wd bytes in initializer",
3896 op_size, type_size);
3898 exp = TREE_OPERAND (exp, 0);
3901 code = TREE_CODE (TREE_TYPE (exp));
3902 thissize = int_size_in_bytes (TREE_TYPE (exp));
3904 /* Allow a constructor with no elements for any data type.
3905 This means to fill the space with zeros. */
3906 if (TREE_CODE (exp) == CONSTRUCTOR
3907 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
3909 assemble_zeros (size);
3910 return;
3913 if (TREE_CODE (exp) == FDESC_EXPR)
3915 #ifdef ASM_OUTPUT_FDESC
3916 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
3917 tree decl = TREE_OPERAND (exp, 0);
3918 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
3919 #else
3920 gcc_unreachable ();
3921 #endif
3922 return;
3925 /* Now output the underlying data. If we've handling the padding, return.
3926 Otherwise, break and ensure SIZE is the size written. */
3927 switch (code)
3929 case CHAR_TYPE:
3930 case BOOLEAN_TYPE:
3931 case INTEGER_TYPE:
3932 case ENUMERAL_TYPE:
3933 case POINTER_TYPE:
3934 case REFERENCE_TYPE:
3935 case OFFSET_TYPE:
3936 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3937 EXPAND_INITIALIZER),
3938 MIN (size, thissize), align, 0))
3939 error ("initializer for integer value is too complicated");
3940 break;
3942 case REAL_TYPE:
3943 if (TREE_CODE (exp) != REAL_CST)
3944 error ("initializer for floating value is not a floating constant");
3946 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3947 break;
3949 case COMPLEX_TYPE:
3950 output_constant (TREE_REALPART (exp), thissize / 2, align);
3951 output_constant (TREE_IMAGPART (exp), thissize / 2,
3952 min_align (align, BITS_PER_UNIT * (thissize / 2)));
3953 break;
3955 case ARRAY_TYPE:
3956 case VECTOR_TYPE:
3957 switch (TREE_CODE (exp))
3959 case CONSTRUCTOR:
3960 output_constructor (exp, size, align);
3961 return;
3962 case STRING_CST:
3963 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3964 size);
3965 assemble_string (TREE_STRING_POINTER (exp), thissize);
3966 break;
3968 case VECTOR_CST:
3970 int elt_size;
3971 tree link;
3972 unsigned int nalign;
3973 enum machine_mode inner;
3975 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
3976 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3978 elt_size = GET_MODE_SIZE (inner);
3980 link = TREE_VECTOR_CST_ELTS (exp);
3981 output_constant (TREE_VALUE (link), elt_size, align);
3982 while ((link = TREE_CHAIN (link)) != NULL)
3983 output_constant (TREE_VALUE (link), elt_size, nalign);
3984 break;
3986 default:
3987 gcc_unreachable ();
3989 break;
3991 case RECORD_TYPE:
3992 case UNION_TYPE:
3993 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
3994 output_constructor (exp, size, align);
3995 return;
3997 case ERROR_MARK:
3998 return;
4000 default:
4001 gcc_unreachable ();
4004 if (size > thissize)
4005 assemble_zeros (size - thissize);
4009 /* Subroutine of output_constructor, used for computing the size of
4010 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4011 type with an unspecified upper bound. */
4013 static unsigned HOST_WIDE_INT
4014 array_size_for_constructor (tree val)
4016 tree max_index, i;
4017 unsigned HOST_WIDE_INT cnt;
4018 tree index, value;
4020 /* This code used to attempt to handle string constants that are not
4021 arrays of single-bytes, but nothing else does, so there's no point in
4022 doing it here. */
4023 if (TREE_CODE (val) == STRING_CST)
4024 return TREE_STRING_LENGTH (val);
4026 max_index = NULL_TREE;
4027 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4029 if (TREE_CODE (index) == RANGE_EXPR)
4030 index = TREE_OPERAND (index, 1);
4031 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4032 max_index = index;
4035 if (max_index == NULL_TREE)
4036 return 0;
4038 /* Compute the total number of array elements. */
4039 i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
4040 convert (sizetype,
4041 TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
4042 i = size_binop (PLUS_EXPR, i, convert (sizetype, integer_one_node));
4044 /* Multiply by the array element unit size to find number of bytes. */
4045 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4047 return tree_low_cst (i, 1);
4050 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
4051 Generate at least SIZE bytes, padding if necessary. */
4053 static void
4054 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
4055 unsigned int align)
4057 tree type = TREE_TYPE (exp);
4058 tree field = 0;
4059 tree min_index = 0;
4060 /* Number of bytes output or skipped so far.
4061 In other words, current position within the constructor. */
4062 HOST_WIDE_INT total_bytes = 0;
4063 /* Nonzero means BYTE contains part of a byte, to be output. */
4064 int byte_buffer_in_use = 0;
4065 int byte = 0;
4066 unsigned HOST_WIDE_INT cnt;
4067 constructor_elt *ce;
4069 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
4071 if (TREE_CODE (type) == RECORD_TYPE)
4072 field = TYPE_FIELDS (type);
4074 if (TREE_CODE (type) == ARRAY_TYPE
4075 && TYPE_DOMAIN (type) != 0)
4076 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
4078 /* As LINK goes through the elements of the constant,
4079 FIELD goes through the structure fields, if the constant is a structure.
4080 if the constant is a union, then we override this,
4081 by getting the field from the TREE_LIST element.
4082 But the constant could also be an array. Then FIELD is zero.
4084 There is always a maximum of one element in the chain LINK for unions
4085 (even if the initializer in a source program incorrectly contains
4086 more one). */
4087 for (cnt = 0;
4088 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
4089 cnt++, field = field ? TREE_CHAIN (field) : 0)
4091 tree val = ce->value;
4092 tree index = 0;
4094 /* The element in a union constructor specifies the proper field
4095 or index. */
4096 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
4097 || TREE_CODE (type) == QUAL_UNION_TYPE)
4098 && ce->index != 0)
4099 field = ce->index;
4101 else if (TREE_CODE (type) == ARRAY_TYPE)
4102 index = ce->index;
4104 #ifdef ASM_COMMENT_START
4105 if (field && flag_verbose_asm)
4106 fprintf (asm_out_file, "%s %s:\n",
4107 ASM_COMMENT_START,
4108 DECL_NAME (field)
4109 ? IDENTIFIER_POINTER (DECL_NAME (field))
4110 : "<anonymous>");
4111 #endif
4113 /* Eliminate the marker that makes a cast not be an lvalue. */
4114 if (val != 0)
4115 STRIP_NOPS (val);
4117 if (index && TREE_CODE (index) == RANGE_EXPR)
4119 unsigned HOST_WIDE_INT fieldsize
4120 = int_size_in_bytes (TREE_TYPE (type));
4121 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
4122 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
4123 HOST_WIDE_INT index;
4124 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
4126 for (index = lo_index; index <= hi_index; index++)
4128 /* Output the element's initial value. */
4129 if (val == 0)
4130 assemble_zeros (fieldsize);
4131 else
4132 output_constant (val, fieldsize, align2);
4134 /* Count its size. */
4135 total_bytes += fieldsize;
4138 else if (field == 0 || !DECL_BIT_FIELD (field))
4140 /* An element that is not a bit-field. */
4142 unsigned HOST_WIDE_INT fieldsize;
4143 /* Since this structure is static,
4144 we know the positions are constant. */
4145 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
4146 unsigned int align2;
4148 if (index != 0)
4149 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
4150 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
4152 /* Output any buffered-up bit-fields preceding this element. */
4153 if (byte_buffer_in_use)
4155 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4156 total_bytes++;
4157 byte_buffer_in_use = 0;
4160 /* Advance to offset of this element.
4161 Note no alignment needed in an array, since that is guaranteed
4162 if each element has the proper size. */
4163 if ((field != 0 || index != 0) && pos != total_bytes)
4165 gcc_assert (pos >= total_bytes);
4166 assemble_zeros (pos - total_bytes);
4167 total_bytes = pos;
4170 /* Find the alignment of this element. */
4171 align2 = min_align (align, BITS_PER_UNIT * pos);
4173 /* Determine size this element should occupy. */
4174 if (field)
4176 fieldsize = 0;
4178 /* If this is an array with an unspecified upper bound,
4179 the initializer determines the size. */
4180 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4181 but we cannot do this until the deprecated support for
4182 initializing zero-length array members is removed. */
4183 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4184 && TYPE_DOMAIN (TREE_TYPE (field))
4185 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4187 fieldsize = array_size_for_constructor (val);
4188 /* Given a non-empty initialization, this field had
4189 better be last. */
4190 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4192 else if (DECL_SIZE_UNIT (field))
4194 /* ??? This can't be right. If the decl size overflows
4195 a host integer we will silently emit no data. */
4196 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4197 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4200 else
4201 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4203 /* Output the element's initial value. */
4204 if (val == 0)
4205 assemble_zeros (fieldsize);
4206 else
4207 output_constant (val, fieldsize, align2);
4209 /* Count its size. */
4210 total_bytes += fieldsize;
4212 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4213 error ("invalid initial value for member %qs",
4214 IDENTIFIER_POINTER (DECL_NAME (field)));
4215 else
4217 /* Element that is a bit-field. */
4219 HOST_WIDE_INT next_offset = int_bit_position (field);
4220 HOST_WIDE_INT end_offset
4221 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4223 if (val == 0)
4224 val = integer_zero_node;
4226 /* If this field does not start in this (or, next) byte,
4227 skip some bytes. */
4228 if (next_offset / BITS_PER_UNIT != total_bytes)
4230 /* Output remnant of any bit field in previous bytes. */
4231 if (byte_buffer_in_use)
4233 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4234 total_bytes++;
4235 byte_buffer_in_use = 0;
4238 /* If still not at proper byte, advance to there. */
4239 if (next_offset / BITS_PER_UNIT != total_bytes)
4241 gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
4242 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4243 total_bytes = next_offset / BITS_PER_UNIT;
4247 if (! byte_buffer_in_use)
4248 byte = 0;
4250 /* We must split the element into pieces that fall within
4251 separate bytes, and combine each byte with previous or
4252 following bit-fields. */
4254 /* next_offset is the offset n fbits from the beginning of
4255 the structure to the next bit of this element to be processed.
4256 end_offset is the offset of the first bit past the end of
4257 this element. */
4258 while (next_offset < end_offset)
4260 int this_time;
4261 int shift;
4262 HOST_WIDE_INT value;
4263 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4264 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4266 /* Advance from byte to byte
4267 within this element when necessary. */
4268 while (next_byte != total_bytes)
4270 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4271 total_bytes++;
4272 byte = 0;
4275 /* Number of bits we can process at once
4276 (all part of the same byte). */
4277 this_time = MIN (end_offset - next_offset,
4278 BITS_PER_UNIT - next_bit);
4279 if (BYTES_BIG_ENDIAN)
4281 /* On big-endian machine, take the most significant bits
4282 first (of the bits that are significant)
4283 and put them into bytes from the most significant end. */
4284 shift = end_offset - next_offset - this_time;
4286 /* Don't try to take a bunch of bits that cross
4287 the word boundary in the INTEGER_CST. We can
4288 only select bits from the LOW or HIGH part
4289 not from both. */
4290 if (shift < HOST_BITS_PER_WIDE_INT
4291 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4293 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4294 shift = HOST_BITS_PER_WIDE_INT;
4297 /* Now get the bits from the appropriate constant word. */
4298 if (shift < HOST_BITS_PER_WIDE_INT)
4299 value = TREE_INT_CST_LOW (val);
4300 else
4302 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4303 value = TREE_INT_CST_HIGH (val);
4304 shift -= HOST_BITS_PER_WIDE_INT;
4307 /* Get the result. This works only when:
4308 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4309 byte |= (((value >> shift)
4310 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4311 << (BITS_PER_UNIT - this_time - next_bit));
4313 else
4315 /* On little-endian machines,
4316 take first the least significant bits of the value
4317 and pack them starting at the least significant
4318 bits of the bytes. */
4319 shift = next_offset - int_bit_position (field);
4321 /* Don't try to take a bunch of bits that cross
4322 the word boundary in the INTEGER_CST. We can
4323 only select bits from the LOW or HIGH part
4324 not from both. */
4325 if (shift < HOST_BITS_PER_WIDE_INT
4326 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4327 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4329 /* Now get the bits from the appropriate constant word. */
4330 if (shift < HOST_BITS_PER_WIDE_INT)
4331 value = TREE_INT_CST_LOW (val);
4332 else
4334 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4335 value = TREE_INT_CST_HIGH (val);
4336 shift -= HOST_BITS_PER_WIDE_INT;
4339 /* Get the result. This works only when:
4340 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4341 byte |= (((value >> shift)
4342 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4343 << next_bit);
4346 next_offset += this_time;
4347 byte_buffer_in_use = 1;
4352 if (byte_buffer_in_use)
4354 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4355 total_bytes++;
4358 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4359 assemble_zeros (size - total_bytes);
4362 /* This TREE_LIST contains any weak symbol declarations waiting
4363 to be emitted. */
4364 static GTY(()) tree weak_decls;
4366 /* Mark DECL as weak. */
4368 static void
4369 mark_weak (tree decl)
4371 DECL_WEAK (decl) = 1;
4373 if (DECL_RTL_SET_P (decl)
4374 && MEM_P (DECL_RTL (decl))
4375 && XEXP (DECL_RTL (decl), 0)
4376 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4377 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4380 /* Merge weak status between NEWDECL and OLDDECL. */
4382 void
4383 merge_weak (tree newdecl, tree olddecl)
4385 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4387 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4389 tree *pwd;
4390 /* We put the NEWDECL on the weak_decls list at some point
4391 and OLDDECL as well. Keep just OLDDECL on the list. */
4392 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4393 if (TREE_VALUE (*pwd) == newdecl)
4395 *pwd = TREE_CHAIN (*pwd);
4396 break;
4399 return;
4402 if (DECL_WEAK (newdecl))
4404 tree wd;
4406 /* NEWDECL is weak, but OLDDECL is not. */
4408 /* If we already output the OLDDECL, we're in trouble; we can't
4409 go back and make it weak. This error cannot caught in
4410 declare_weak because the NEWDECL and OLDDECL was not yet
4411 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4412 if (TREE_ASM_WRITTEN (olddecl))
4413 error ("weak declaration of %q+D must precede definition",
4414 newdecl);
4416 /* If we've already generated rtl referencing OLDDECL, we may
4417 have done so in a way that will not function properly with
4418 a weak symbol. */
4419 else if (TREE_USED (olddecl)
4420 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4421 warning (0, "weak declaration of %q+D after first use results "
4422 "in unspecified behavior", newdecl);
4424 if (SUPPORTS_WEAK)
4426 /* We put the NEWDECL on the weak_decls list at some point.
4427 Replace it with the OLDDECL. */
4428 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4429 if (TREE_VALUE (wd) == newdecl)
4431 TREE_VALUE (wd) = olddecl;
4432 break;
4434 /* We may not find the entry on the list. If NEWDECL is a
4435 weak alias, then we will have already called
4436 globalize_decl to remove the entry; in that case, we do
4437 not need to do anything. */
4440 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4441 mark_weak (olddecl);
4443 else
4444 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4445 weak. Just update NEWDECL to indicate that it's weak too. */
4446 mark_weak (newdecl);
4449 /* Declare DECL to be a weak symbol. */
4451 void
4452 declare_weak (tree decl)
4454 if (! TREE_PUBLIC (decl))
4455 error ("weak declaration of %q+D must be public", decl);
4456 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4457 error ("weak declaration of %q+D must precede definition", decl);
4458 else if (SUPPORTS_WEAK)
4460 if (! DECL_WEAK (decl))
4461 weak_decls = tree_cons (NULL, decl, weak_decls);
4463 else
4464 warning (0, "weak declaration of %q+D not supported", decl);
4466 mark_weak (decl);
4469 /* Emit any pending weak declarations. */
4471 void
4472 weak_finish (void)
4474 tree t;
4476 for (t = weak_decls; t; t = TREE_CHAIN (t))
4478 tree decl = TREE_VALUE (t);
4479 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4480 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4481 #endif
4483 if (! TREE_USED (decl))
4484 continue;
4486 #ifdef ASM_WEAKEN_DECL
4487 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4488 #else
4489 #ifdef ASM_WEAKEN_LABEL
4490 ASM_WEAKEN_LABEL (asm_out_file, name);
4491 #else
4492 #ifdef ASM_OUTPUT_WEAK_ALIAS
4493 warning (0, "only weak aliases are supported in this configuration");
4494 return;
4495 #endif
4496 #endif
4497 #endif
4501 /* Emit the assembly bits to indicate that DECL is globally visible. */
4503 static void
4504 globalize_decl (tree decl)
4506 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4508 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4509 if (DECL_WEAK (decl))
4511 tree *p, t;
4513 #ifdef ASM_WEAKEN_DECL
4514 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4515 #else
4516 ASM_WEAKEN_LABEL (asm_out_file, name);
4517 #endif
4519 /* Remove this function from the pending weak list so that
4520 we do not emit multiple .weak directives for it. */
4521 for (p = &weak_decls; (t = *p) ; )
4523 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4524 *p = TREE_CHAIN (t);
4525 else
4526 p = &TREE_CHAIN (t);
4528 return;
4530 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4531 if (DECL_ONE_ONLY (decl))
4532 ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4533 #endif
4535 targetm.asm_out.globalize_label (asm_out_file, name);
4538 /* We have to be able to tell cgraph about the needed-ness of the target
4539 of an alias. This requires that the decl have been defined. Aliases
4540 that precede their definition have to be queued for later processing. */
4542 typedef struct alias_pair GTY(())
4544 tree decl;
4545 tree target;
4546 } alias_pair;
4548 /* Define gc'd vector type. */
4549 DEF_VEC_O(alias_pair);
4550 DEF_VEC_ALLOC_O(alias_pair,gc);
4552 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
4554 /* Given an assembly name, find the decl it is associated with. At the
4555 same time, mark it needed for cgraph. */
4557 static tree
4558 find_decl_and_mark_needed (tree decl, tree target)
4560 struct cgraph_node *fnode = NULL;
4561 struct cgraph_varpool_node *vnode = NULL;
4563 /* C++ thunk emitting code produces aliases late in the game.
4564 Avoid confusing cgraph code in that case. */
4565 if (!cgraph_global_info_ready)
4567 if (TREE_CODE (decl) == FUNCTION_DECL)
4569 fnode = cgraph_node_for_asm (target);
4570 if (fnode == NULL)
4571 vnode = cgraph_varpool_node_for_asm (target);
4573 else
4575 vnode = cgraph_varpool_node_for_asm (target);
4576 if (vnode == NULL)
4577 fnode = cgraph_node_for_asm (target);
4581 if (fnode)
4583 cgraph_mark_needed_node (fnode);
4584 return fnode->decl;
4586 else if (vnode)
4588 cgraph_varpool_mark_needed_node (vnode);
4589 return vnode->decl;
4591 else
4592 return NULL_TREE;
4595 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4596 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
4597 tree node is DECL to have the value of the tree node TARGET. */
4599 static void
4600 do_assemble_alias (tree decl, tree target ATTRIBUTE_UNUSED)
4602 if (TREE_ASM_WRITTEN (decl))
4603 return;
4605 TREE_ASM_WRITTEN (decl) = 1;
4606 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4608 #ifdef ASM_OUTPUT_DEF
4609 /* Make name accessible from other files, if appropriate. */
4611 if (TREE_PUBLIC (decl))
4613 globalize_decl (decl);
4614 maybe_assemble_visibility (decl);
4617 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4618 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4619 # else
4620 ASM_OUTPUT_DEF (asm_out_file,
4621 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4622 IDENTIFIER_POINTER (target));
4623 # endif
4624 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4626 const char *name;
4627 tree *p, t;
4629 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4630 # ifdef ASM_WEAKEN_DECL
4631 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4632 # else
4633 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4634 # endif
4635 /* Remove this function from the pending weak list so that
4636 we do not emit multiple .weak directives for it. */
4637 for (p = &weak_decls; (t = *p) ; )
4638 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4639 *p = TREE_CHAIN (t);
4640 else
4641 p = &TREE_CHAIN (t);
4643 #endif
4646 /* First pass of completing pending aliases. Make sure that cgraph knows
4647 which symbols will be required. */
4649 void
4650 finish_aliases_1 (void)
4652 unsigned i;
4653 alias_pair *p;
4655 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4657 tree target_decl;
4659 target_decl = find_decl_and_mark_needed (p->decl, p->target);
4660 if (target_decl == NULL)
4661 error ("%q+D aliased to undefined symbol %qs",
4662 p->decl, IDENTIFIER_POINTER (p->target));
4663 else if (DECL_EXTERNAL (target_decl))
4664 error ("%q+D aliased to external symbol %qs",
4665 p->decl, IDENTIFIER_POINTER (p->target));
4669 /* Second pass of completing pending aliases. Emit the actual assembly.
4670 This happens at the end of compilation and thus it is assured that the
4671 target symbol has been emitted. */
4673 void
4674 finish_aliases_2 (void)
4676 unsigned i;
4677 alias_pair *p;
4679 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4680 do_assemble_alias (p->decl, p->target);
4682 VEC_truncate (alias_pair, alias_pairs, 0);
4685 /* Emit an assembler directive to make the symbol for DECL an alias to
4686 the symbol for TARGET. */
4688 void
4689 assemble_alias (tree decl, tree target)
4691 tree target_decl;
4693 #if !defined (ASM_OUTPUT_DEF)
4694 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4695 error ("%Jalias definitions not supported in this configuration", decl);
4696 return;
4697 # else
4698 if (!DECL_WEAK (decl))
4700 error ("%Jonly weak aliases are supported in this configuration", decl);
4701 return;
4703 # endif
4704 #endif
4706 /* We must force creation of DECL_RTL for debug info generation, even though
4707 we don't use it here. */
4708 make_decl_rtl (decl);
4709 TREE_USED (decl) = 1;
4711 /* A quirk of the initial implementation of aliases required that the user
4712 add "extern" to all of them. Which is silly, but now historical. Do
4713 note that the symbol is in fact locally defined. */
4714 DECL_EXTERNAL (decl) = 0;
4716 /* Allow aliases to aliases. */
4717 if (TREE_CODE (decl) == FUNCTION_DECL)
4718 cgraph_node (decl)->alias = true;
4719 else
4720 cgraph_varpool_node (decl)->alias = true;
4722 /* If the target has already been emitted, we don't have to queue the
4723 alias. This saves a tad o memory. */
4724 target_decl = find_decl_and_mark_needed (decl, target);
4725 if (target_decl && TREE_ASM_WRITTEN (target_decl))
4726 do_assemble_alias (decl, target);
4727 else
4729 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
4730 p->decl = decl;
4731 p->target = target;
4735 /* Emit an assembler directive to set symbol for DECL visibility to
4736 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
4738 void
4739 default_assemble_visibility (tree decl, int vis)
4741 static const char * const visibility_types[] = {
4742 NULL, "internal", "hidden", "protected"
4745 const char *name, *type;
4747 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4748 type = visibility_types[vis];
4750 #ifdef HAVE_GAS_HIDDEN
4751 fprintf (asm_out_file, "\t.%s\t", type);
4752 assemble_name (asm_out_file, name);
4753 fprintf (asm_out_file, "\n");
4754 #else
4755 warning (OPT_Wattributes, "visibility attribute not supported "
4756 "in this configuration; ignored");
4757 #endif
4760 /* A helper function to call assemble_visibility when needed for a decl. */
4762 static void
4763 maybe_assemble_visibility (tree decl)
4765 enum symbol_visibility vis = DECL_VISIBILITY (decl);
4767 if (vis != VISIBILITY_DEFAULT)
4768 targetm.asm_out.visibility (decl, vis);
4771 /* Returns 1 if the target configuration supports defining public symbols
4772 so that one of them will be chosen at link time instead of generating a
4773 multiply-defined symbol error, whether through the use of weak symbols or
4774 a target-specific mechanism for having duplicates discarded. */
4777 supports_one_only (void)
4779 if (SUPPORTS_ONE_ONLY)
4780 return 1;
4781 return SUPPORTS_WEAK;
4784 /* Set up DECL as a public symbol that can be defined in multiple
4785 translation units without generating a linker error. */
4787 void
4788 make_decl_one_only (tree decl)
4790 gcc_assert (TREE_CODE (decl) == VAR_DECL
4791 || TREE_CODE (decl) == FUNCTION_DECL);
4793 TREE_PUBLIC (decl) = 1;
4795 if (SUPPORTS_ONE_ONLY)
4797 #ifdef MAKE_DECL_ONE_ONLY
4798 MAKE_DECL_ONE_ONLY (decl);
4799 #endif
4800 DECL_ONE_ONLY (decl) = 1;
4802 else if (TREE_CODE (decl) == VAR_DECL
4803 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4804 DECL_COMMON (decl) = 1;
4805 else
4807 gcc_assert (SUPPORTS_WEAK);
4808 DECL_WEAK (decl) = 1;
4812 void
4813 init_varasm_once (void)
4815 in_named_htab = htab_create_ggc (31, in_named_entry_hash,
4816 in_named_entry_eq, NULL);
4817 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4818 const_desc_eq, NULL);
4820 const_alias_set = new_alias_set ();
4823 enum tls_model
4824 decl_default_tls_model (tree decl)
4826 enum tls_model kind;
4827 bool is_local;
4829 is_local = targetm.binds_local_p (decl);
4830 if (!flag_shlib)
4832 if (is_local)
4833 kind = TLS_MODEL_LOCAL_EXEC;
4834 else
4835 kind = TLS_MODEL_INITIAL_EXEC;
4838 /* Local dynamic is inefficient when we're not combining the
4839 parts of the address. */
4840 else if (optimize && is_local)
4841 kind = TLS_MODEL_LOCAL_DYNAMIC;
4842 else
4843 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4844 if (kind < flag_tls_default)
4845 kind = flag_tls_default;
4847 return kind;
4850 /* Select a set of attributes for section NAME based on the properties
4851 of DECL and whether or not RELOC indicates that DECL's initializer
4852 might contain runtime relocations.
4854 We make the section read-only and executable for a function decl,
4855 read-only for a const data decl, and writable for a non-const data decl. */
4857 unsigned int
4858 default_section_type_flags (tree decl, const char *name, int reloc)
4860 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4863 unsigned int
4864 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4865 int shlib)
4867 unsigned int flags;
4869 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4870 flags = SECTION_CODE;
4871 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4872 flags = 0;
4873 else if (current_function_decl
4874 && cfun
4875 && cfun->unlikely_text_section_name
4876 && strcmp (name, cfun->unlikely_text_section_name) == 0)
4877 flags = SECTION_CODE;
4878 else if (!decl
4879 && (!current_function_decl || !cfun)
4880 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
4881 flags = SECTION_CODE;
4882 else
4883 flags = SECTION_WRITE;
4885 if (decl && DECL_ONE_ONLY (decl))
4886 flags |= SECTION_LINKONCE;
4888 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
4889 flags |= SECTION_TLS | SECTION_WRITE;
4891 if (strcmp (name, ".bss") == 0
4892 || strncmp (name, ".bss.", 5) == 0
4893 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4894 || strcmp (name, ".sbss") == 0
4895 || strncmp (name, ".sbss.", 6) == 0
4896 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
4897 flags |= SECTION_BSS;
4899 if (strcmp (name, ".tdata") == 0
4900 || strncmp (name, ".tdata.", 7) == 0
4901 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
4902 flags |= SECTION_TLS;
4904 if (strcmp (name, ".tbss") == 0
4905 || strncmp (name, ".tbss.", 6) == 0
4906 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4907 flags |= SECTION_TLS | SECTION_BSS;
4909 /* These three sections have special ELF types. They are neither
4910 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4911 want to print a section type (@progbits or @nobits). If someone
4912 is silly enough to emit code or TLS variables to one of these
4913 sections, then don't handle them specially. */
4914 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4915 && (strcmp (name, ".init_array") == 0
4916 || strcmp (name, ".fini_array") == 0
4917 || strcmp (name, ".preinit_array") == 0))
4918 flags |= SECTION_NOTYPE;
4920 return flags;
4923 /* Output assembly to switch to section NAME with attribute FLAGS.
4924 Four variants for common object file formats. */
4926 void
4927 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4928 unsigned int flags ATTRIBUTE_UNUSED,
4929 tree decl ATTRIBUTE_UNUSED)
4931 /* Some object formats don't support named sections at all. The
4932 front-end should already have flagged this as an error. */
4933 gcc_unreachable ();
4936 void
4937 default_elf_asm_named_section (const char *name, unsigned int flags,
4938 tree decl ATTRIBUTE_UNUSED)
4940 char flagchars[10], *f = flagchars;
4942 /* If we have already declared this section, we can use an
4943 abbreviated form to switch back to it -- unless this section is
4944 part of a COMDAT groups, in which case GAS requires the full
4945 declaration every time. */
4946 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4947 && ! named_section_first_declaration (name))
4949 fprintf (asm_out_file, "\t.section\t%s\n", name);
4950 return;
4953 if (!(flags & SECTION_DEBUG))
4954 *f++ = 'a';
4955 if (flags & SECTION_WRITE)
4956 *f++ = 'w';
4957 if (flags & SECTION_CODE)
4958 *f++ = 'x';
4959 if (flags & SECTION_SMALL)
4960 *f++ = 's';
4961 if (flags & SECTION_MERGE)
4962 *f++ = 'M';
4963 if (flags & SECTION_STRINGS)
4964 *f++ = 'S';
4965 if (flags & SECTION_TLS)
4966 *f++ = 'T';
4967 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4968 *f++ = 'G';
4969 *f = '\0';
4971 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
4973 if (!(flags & SECTION_NOTYPE))
4975 const char *type;
4976 const char *format;
4978 if (flags & SECTION_BSS)
4979 type = "nobits";
4980 else
4981 type = "progbits";
4983 format = ",@%s";
4984 #ifdef ASM_COMMENT_START
4985 /* On platforms that use "@" as the assembly comment character,
4986 use "%" instead. */
4987 if (strcmp (ASM_COMMENT_START, "@") == 0)
4988 format = ",%%%s";
4989 #endif
4990 fprintf (asm_out_file, format, type);
4992 if (flags & SECTION_ENTSIZE)
4993 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
4994 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4995 fprintf (asm_out_file, ",%s,comdat",
4996 lang_hooks.decls.comdat_group (decl));
4999 putc ('\n', asm_out_file);
5002 void
5003 default_coff_asm_named_section (const char *name, unsigned int flags,
5004 tree decl ATTRIBUTE_UNUSED)
5006 char flagchars[8], *f = flagchars;
5008 if (flags & SECTION_WRITE)
5009 *f++ = 'w';
5010 if (flags & SECTION_CODE)
5011 *f++ = 'x';
5012 *f = '\0';
5014 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5017 void
5018 default_pe_asm_named_section (const char *name, unsigned int flags,
5019 tree decl)
5021 default_coff_asm_named_section (name, flags, decl);
5023 if (flags & SECTION_LINKONCE)
5025 /* Functions may have been compiled at various levels of
5026 optimization so we can't use `same_size' here.
5027 Instead, have the linker pick one. */
5028 fprintf (asm_out_file, "\t.linkonce %s\n",
5029 (flags & SECTION_CODE ? "discard" : "same_size"));
5033 /* The lame default section selector. */
5035 void
5036 default_select_section (tree decl, int reloc,
5037 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5039 bool readonly = false;
5041 if (DECL_P (decl))
5043 if (decl_readonly_section (decl, reloc))
5044 readonly = true;
5046 else if (TREE_CODE (decl) == CONSTRUCTOR)
5048 if (! ((flag_pic && reloc)
5049 || !TREE_READONLY (decl)
5050 || TREE_SIDE_EFFECTS (decl)
5051 || !TREE_CONSTANT (decl)))
5052 readonly = true;
5054 else if (TREE_CODE (decl) == STRING_CST)
5055 readonly = true;
5056 else if (! (flag_pic && reloc))
5057 readonly = true;
5059 if (readonly)
5060 readonly_data_section ();
5061 else
5062 data_section ();
5065 enum section_category
5066 categorize_decl_for_section (tree decl, int reloc, int shlib)
5068 enum section_category ret;
5070 if (TREE_CODE (decl) == FUNCTION_DECL)
5071 return SECCAT_TEXT;
5072 else if (TREE_CODE (decl) == STRING_CST)
5074 if (flag_mudflap) /* or !flag_merge_constants */
5075 return SECCAT_RODATA;
5076 else
5077 return SECCAT_RODATA_MERGE_STR;
5079 else if (TREE_CODE (decl) == VAR_DECL)
5081 if (DECL_INITIAL (decl) == NULL
5082 || DECL_INITIAL (decl) == error_mark_node
5083 || (flag_zero_initialized_in_bss
5084 /* Leave constant zeroes in .rodata so they can be shared. */
5085 && !TREE_READONLY (decl)
5086 && initializer_zerop (DECL_INITIAL (decl))))
5087 ret = SECCAT_BSS;
5088 else if (! TREE_READONLY (decl)
5089 || TREE_SIDE_EFFECTS (decl)
5090 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5092 if (shlib && (reloc & 2))
5093 ret = SECCAT_DATA_REL;
5094 else if (shlib && reloc)
5095 ret = SECCAT_DATA_REL_LOCAL;
5096 else
5097 ret = SECCAT_DATA;
5099 else if (shlib && (reloc & 2))
5100 ret = SECCAT_DATA_REL_RO;
5101 else if (shlib && reloc)
5102 ret = SECCAT_DATA_REL_RO_LOCAL;
5103 else if (reloc || flag_merge_constants < 2)
5104 /* C and C++ don't allow different variables to share the same
5105 location. -fmerge-all-constants allows even that (at the
5106 expense of not conforming). */
5107 ret = SECCAT_RODATA;
5108 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5109 ret = SECCAT_RODATA_MERGE_STR_INIT;
5110 else
5111 ret = SECCAT_RODATA_MERGE_CONST;
5113 else if (TREE_CODE (decl) == CONSTRUCTOR)
5115 if ((shlib && reloc)
5116 || TREE_SIDE_EFFECTS (decl)
5117 || ! TREE_CONSTANT (decl))
5118 ret = SECCAT_DATA;
5119 else
5120 ret = SECCAT_RODATA;
5122 else
5123 ret = SECCAT_RODATA;
5125 /* There are no read-only thread-local sections. */
5126 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5128 /* Note that this would be *just* SECCAT_BSS, except that there's
5129 no concept of a read-only thread-local-data section. */
5130 if (ret == SECCAT_BSS
5131 || (flag_zero_initialized_in_bss
5132 && initializer_zerop (DECL_INITIAL (decl))))
5133 ret = SECCAT_TBSS;
5134 else
5135 ret = SECCAT_TDATA;
5138 /* If the target uses small data sections, select it. */
5139 else if (targetm.in_small_data_p (decl))
5141 if (ret == SECCAT_BSS)
5142 ret = SECCAT_SBSS;
5143 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5144 ret = SECCAT_SRODATA;
5145 else
5146 ret = SECCAT_SDATA;
5149 return ret;
5152 bool
5153 decl_readonly_section (tree decl, int reloc)
5155 return decl_readonly_section_1 (decl, reloc, flag_pic);
5158 bool
5159 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5161 switch (categorize_decl_for_section (decl, reloc, shlib))
5163 case SECCAT_RODATA:
5164 case SECCAT_RODATA_MERGE_STR:
5165 case SECCAT_RODATA_MERGE_STR_INIT:
5166 case SECCAT_RODATA_MERGE_CONST:
5167 case SECCAT_SRODATA:
5168 return true;
5169 break;
5170 default:
5171 return false;
5172 break;
5176 /* Select a section based on the above categorization. */
5178 void
5179 default_elf_select_section (tree decl, int reloc,
5180 unsigned HOST_WIDE_INT align)
5182 default_elf_select_section_1 (decl, reloc, align, flag_pic);
5185 void
5186 default_elf_select_section_1 (tree decl, int reloc,
5187 unsigned HOST_WIDE_INT align, int shlib)
5189 const char *sname;
5190 switch (categorize_decl_for_section (decl, reloc, shlib))
5192 case SECCAT_TEXT:
5193 /* We're not supposed to be called on FUNCTION_DECLs. */
5194 gcc_unreachable ();
5195 case SECCAT_RODATA:
5196 readonly_data_section ();
5197 return;
5198 case SECCAT_RODATA_MERGE_STR:
5199 mergeable_string_section (decl, align, 0);
5200 return;
5201 case SECCAT_RODATA_MERGE_STR_INIT:
5202 mergeable_string_section (DECL_INITIAL (decl), align, 0);
5203 return;
5204 case SECCAT_RODATA_MERGE_CONST:
5205 mergeable_constant_section (DECL_MODE (decl), align, 0);
5206 return;
5207 case SECCAT_SRODATA:
5208 sname = ".sdata2";
5209 break;
5210 case SECCAT_DATA:
5211 data_section ();
5212 return;
5213 case SECCAT_DATA_REL:
5214 sname = ".data.rel";
5215 break;
5216 case SECCAT_DATA_REL_LOCAL:
5217 sname = ".data.rel.local";
5218 break;
5219 case SECCAT_DATA_REL_RO:
5220 sname = ".data.rel.ro";
5221 break;
5222 case SECCAT_DATA_REL_RO_LOCAL:
5223 sname = ".data.rel.ro.local";
5224 break;
5225 case SECCAT_SDATA:
5226 sname = ".sdata";
5227 break;
5228 case SECCAT_TDATA:
5229 sname = ".tdata";
5230 break;
5231 case SECCAT_BSS:
5232 #ifdef BSS_SECTION_ASM_OP
5233 bss_section ();
5234 return;
5235 #else
5236 sname = ".bss";
5237 break;
5238 #endif
5239 case SECCAT_SBSS:
5240 sname = ".sbss";
5241 break;
5242 case SECCAT_TBSS:
5243 sname = ".tbss";
5244 break;
5245 default:
5246 gcc_unreachable ();
5249 if (!DECL_P (decl))
5250 decl = NULL_TREE;
5251 named_section (decl, sname, reloc);
5254 /* Construct a unique section name based on the decl name and the
5255 categorization performed above. */
5257 void
5258 default_unique_section (tree decl, int reloc)
5260 default_unique_section_1 (decl, reloc, flag_pic);
5263 void
5264 default_unique_section_1 (tree decl, int reloc, int shlib)
5266 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
5267 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5268 const char *prefix, *name;
5269 size_t nlen, plen;
5270 char *string;
5272 switch (categorize_decl_for_section (decl, reloc, shlib))
5274 case SECCAT_TEXT:
5275 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5276 break;
5277 case SECCAT_RODATA:
5278 case SECCAT_RODATA_MERGE_STR:
5279 case SECCAT_RODATA_MERGE_STR_INIT:
5280 case SECCAT_RODATA_MERGE_CONST:
5281 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5282 break;
5283 case SECCAT_SRODATA:
5284 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5285 break;
5286 case SECCAT_DATA:
5287 case SECCAT_DATA_REL:
5288 case SECCAT_DATA_REL_LOCAL:
5289 case SECCAT_DATA_REL_RO:
5290 case SECCAT_DATA_REL_RO_LOCAL:
5291 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5292 break;
5293 case SECCAT_SDATA:
5294 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5295 break;
5296 case SECCAT_BSS:
5297 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5298 break;
5299 case SECCAT_SBSS:
5300 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5301 break;
5302 case SECCAT_TDATA:
5303 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5304 break;
5305 case SECCAT_TBSS:
5306 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5307 break;
5308 default:
5309 gcc_unreachable ();
5311 plen = strlen (prefix);
5313 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5314 name = targetm.strip_name_encoding (name);
5315 nlen = strlen (name);
5317 string = alloca (nlen + plen + 1);
5318 memcpy (string, prefix, plen);
5319 memcpy (string + plen, name, nlen + 1);
5321 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5324 void
5325 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5326 rtx x,
5327 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5329 if (flag_pic)
5330 switch (GET_CODE (x))
5332 case CONST:
5333 case SYMBOL_REF:
5334 case LABEL_REF:
5335 data_section ();
5336 return;
5338 default:
5339 break;
5342 readonly_data_section ();
5345 void
5346 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5347 unsigned HOST_WIDE_INT align)
5349 /* ??? Handle small data here somehow. */
5351 if (flag_pic)
5352 switch (GET_CODE (x))
5354 case CONST:
5355 case SYMBOL_REF:
5356 named_section (NULL_TREE, ".data.rel.ro", 3);
5357 return;
5359 case LABEL_REF:
5360 named_section (NULL_TREE, ".data.rel.ro.local", 1);
5361 return;
5363 default:
5364 break;
5367 mergeable_constant_section (mode, align, 0);
5370 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
5372 void
5373 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5375 rtx symbol;
5376 int flags;
5378 /* Careful not to prod global register variables. */
5379 if (!MEM_P (rtl))
5380 return;
5381 symbol = XEXP (rtl, 0);
5382 if (GET_CODE (symbol) != SYMBOL_REF)
5383 return;
5385 flags = 0;
5386 if (TREE_CODE (decl) == FUNCTION_DECL)
5387 flags |= SYMBOL_FLAG_FUNCTION;
5388 if (targetm.binds_local_p (decl))
5389 flags |= SYMBOL_FLAG_LOCAL;
5390 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5391 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5392 else if (targetm.in_small_data_p (decl))
5393 flags |= SYMBOL_FLAG_SMALL;
5394 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
5395 being PUBLIC, the thing *must* be defined in this translation unit.
5396 Prevent this buglet from being propagated into rtl code as well. */
5397 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5398 flags |= SYMBOL_FLAG_EXTERNAL;
5400 SYMBOL_REF_FLAGS (symbol) = flags;
5403 /* By default, we do nothing for encode_section_info, so we need not
5404 do anything but discard the '*' marker. */
5406 const char *
5407 default_strip_name_encoding (const char *str)
5409 return str + (*str == '*');
5412 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5413 wrt cross-module name binding. */
5415 bool
5416 default_binds_local_p (tree exp)
5418 return default_binds_local_p_1 (exp, flag_shlib);
5421 bool
5422 default_binds_local_p_1 (tree exp, int shlib)
5424 bool local_p;
5426 /* A non-decl is an entry in the constant pool. */
5427 if (!DECL_P (exp))
5428 local_p = true;
5429 /* Static variables are always local. */
5430 else if (! TREE_PUBLIC (exp))
5431 local_p = true;
5432 /* A variable is local if the user explicitly tells us so. */
5433 else if (DECL_VISIBILITY_SPECIFIED (exp) && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5434 local_p = true;
5435 /* Otherwise, variables defined outside this object may not be local. */
5436 else if (DECL_EXTERNAL (exp))
5437 local_p = false;
5438 /* Linkonce and weak data are never local. */
5439 else if (DECL_ONE_ONLY (exp) || DECL_WEAK (exp))
5440 local_p = false;
5441 /* If none of the above and visibility is not default, make local. */
5442 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5443 local_p = true;
5444 /* If PIC, then assume that any global name can be overridden by
5445 symbols resolved from other modules. */
5446 else if (shlib)
5447 local_p = false;
5448 /* Uninitialized COMMON variable may be unified with symbols
5449 resolved from other modules. */
5450 else if (DECL_COMMON (exp)
5451 && (DECL_INITIAL (exp) == NULL
5452 || DECL_INITIAL (exp) == error_mark_node))
5453 local_p = false;
5454 /* Otherwise we're left with initialized (or non-common) global data
5455 which is of necessity defined locally. */
5456 else
5457 local_p = true;
5459 return local_p;
5462 /* Determine whether or not a pointer mode is valid. Assume defaults
5463 of ptr_mode or Pmode - can be overridden. */
5464 bool
5465 default_valid_pointer_mode (enum machine_mode mode)
5467 return (mode == ptr_mode || mode == Pmode);
5470 /* Default function to output code that will globalize a label. A
5471 target must define GLOBAL_ASM_OP or provide its own function to
5472 globalize a label. */
5473 #ifdef GLOBAL_ASM_OP
5474 void
5475 default_globalize_label (FILE * stream, const char *name)
5477 fputs (GLOBAL_ASM_OP, stream);
5478 assemble_name (stream, name);
5479 putc ('\n', stream);
5481 #endif /* GLOBAL_ASM_OP */
5483 /* Default function to output a label for unwind information. The
5484 default is to do nothing. A target that needs nonlocal labels for
5485 unwind information must provide its own function to do this. */
5486 void
5487 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5488 tree decl ATTRIBUTE_UNUSED,
5489 int for_eh ATTRIBUTE_UNUSED,
5490 int empty ATTRIBUTE_UNUSED)
5494 /* This is how to output an internal numbered label where PREFIX is
5495 the class of label and LABELNO is the number within the class. */
5497 void
5498 default_internal_label (FILE *stream, const char *prefix,
5499 unsigned long labelno)
5501 char *const buf = alloca (40 + strlen (prefix));
5502 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5503 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5506 /* This is the default behavior at the beginning of a file. It's
5507 controlled by two other target-hook toggles. */
5508 void
5509 default_file_start (void)
5511 if (targetm.file_start_app_off && !flag_verbose_asm)
5512 fputs (ASM_APP_OFF, asm_out_file);
5514 if (targetm.file_start_file_directive)
5515 output_file_directive (asm_out_file, main_input_filename);
5518 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5519 which emits a special section directive used to indicate whether or
5520 not this object file needs an executable stack. This is primarily
5521 a GNU extension to ELF but could be used on other targets. */
5523 int trampolines_created;
5525 void
5526 file_end_indicate_exec_stack (void)
5528 unsigned int flags = SECTION_DEBUG;
5529 if (trampolines_created)
5530 flags |= SECTION_CODE;
5532 named_section_flags (".note.GNU-stack", flags);
5535 #include "gt-varasm.h"