2005-06-30 J. D. Johnston <jjohnst@us.ibm.com>
[official-gcc.git] / gcc / varasm.c
blob9e3183dd9d6447c23cfec1c2d55de99c98ca2ab2
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 const char *first_global_object_name;
64 const char *weak_global_object_name;
66 struct addr_const;
67 struct constant_descriptor_rtx;
68 struct rtx_constant_pool;
70 struct varasm_status GTY(())
72 /* If we're using a per-function constant pool, this is it. */
73 struct rtx_constant_pool *pool;
75 /* Number of tree-constants deferred during the expansion of this
76 function. */
77 unsigned int deferred_constants;
80 #define n_deferred_constants (cfun->varasm->deferred_constants)
82 /* Number for making the label on the next
83 constant that is stored in memory. */
85 static GTY(()) int const_labelno;
87 /* Carry information from ASM_DECLARE_OBJECT_NAME
88 to ASM_FINISH_DECLARE_OBJECT. */
90 int size_directive_output;
92 /* The last decl for which assemble_variable was called,
93 if it did ASM_DECLARE_OBJECT_NAME.
94 If the last call to assemble_variable didn't do that,
95 this holds 0. */
97 tree last_assemble_variable_decl;
99 /* The following global variable indicates if the first basic block
100 in a function belongs to the cold partition or not. */
102 bool first_function_block_is_cold;
104 /* We give all constants their own alias set. Perhaps redundant with
105 MEM_READONLY_P, but pre-dates it. */
107 static HOST_WIDE_INT const_alias_set;
109 static const char *strip_reg_name (const char *);
110 static int contains_pointers_p (tree);
111 #ifdef ASM_OUTPUT_EXTERNAL
112 static bool incorporeal_function_p (tree);
113 #endif
114 static void decode_addr_const (tree, struct addr_const *);
115 static hashval_t const_desc_hash (const void *);
116 static int const_desc_eq (const void *, const void *);
117 static hashval_t const_hash_1 (const tree);
118 static int compare_constant (const tree, const tree);
119 static tree copy_constant (tree);
120 static void output_constant_def_contents (rtx);
121 static void output_addressed_constants (tree);
122 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
123 static unsigned min_align (unsigned, unsigned);
124 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
125 static void globalize_decl (tree);
126 static void maybe_assemble_visibility (tree);
127 static int in_named_entry_eq (const void *, const void *);
128 static hashval_t in_named_entry_hash (const void *);
129 static void initialize_cold_section_name (void);
130 #ifdef BSS_SECTION_ASM_OP
131 #ifdef ASM_OUTPUT_BSS
132 static void asm_output_bss (FILE *, tree, const char *,
133 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
134 #endif
135 #ifdef ASM_OUTPUT_ALIGNED_BSS
136 static void asm_output_aligned_bss (FILE *, tree, const char *,
137 unsigned HOST_WIDE_INT, int)
138 ATTRIBUTE_UNUSED;
139 #endif
140 #endif /* BSS_SECTION_ASM_OP */
141 static bool asm_emit_uninitialised (tree, const char*,
142 unsigned HOST_WIDE_INT,
143 unsigned HOST_WIDE_INT);
144 static void mark_weak (tree);
146 static GTY(()) enum in_section in_section = no_section;
147 enum in_section last_text_section;
149 /* Return a nonzero value if DECL has a section attribute. */
150 #ifndef IN_NAMED_SECTION
151 #define IN_NAMED_SECTION(DECL) \
152 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
153 && DECL_SECTION_NAME (DECL) != NULL_TREE)
154 #endif
156 /* Text of section name when in_section == in_named. */
157 static GTY(()) const char *in_named_name;
158 const char *last_text_section_name;
160 /* Hash table of flags that have been used for a particular named section. */
162 struct in_named_entry GTY(())
164 const char *name;
165 unsigned int flags;
166 bool declared;
169 static GTY((param_is (struct in_named_entry))) htab_t in_named_htab;
171 /* Define functions like text_section for any extra sections. */
172 #ifdef EXTRA_SECTION_FUNCTIONS
173 EXTRA_SECTION_FUNCTIONS
174 #endif
176 static void
177 initialize_cold_section_name (void)
179 const char *stripped_name;
180 char *name, *buffer;
181 tree dsn;
183 gcc_assert (cfun && current_function_decl);
184 if (cfun->unlikely_text_section_name)
185 return;
187 dsn = DECL_SECTION_NAME (current_function_decl);
188 if (flag_function_sections && dsn)
190 name = alloca (TREE_STRING_LENGTH (dsn) + 1);
191 memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
193 stripped_name = targetm.strip_name_encoding (name);
195 buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
196 cfun->unlikely_text_section_name = ggc_strdup (buffer);
198 else
199 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
202 /* Tell assembler to switch to text section. */
204 void
205 text_section (void)
207 if (in_section != in_text)
209 in_section = in_text;
210 last_text_section = in_text;
211 fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
215 /* Tell assembler to switch to unlikely-to-be-executed text section. */
217 void
218 unlikely_text_section (void)
220 if (cfun)
222 if (!cfun->unlikely_text_section_name)
223 initialize_cold_section_name ();
225 if (flag_function_sections
226 || ((in_section != in_unlikely_executed_text)
227 && (in_section != in_named
228 || (strcmp (in_named_name, cfun->unlikely_text_section_name)
229 != 0))))
231 named_section (NULL_TREE, cfun->unlikely_text_section_name, 0);
232 in_section = in_unlikely_executed_text;
233 last_text_section = in_unlikely_executed_text;
236 else
238 named_section (NULL_TREE, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
239 in_section = in_unlikely_executed_text;
240 last_text_section = in_unlikely_executed_text;
244 /* Tell assembler to switch to data section. */
246 void
247 data_section (void)
249 if (in_section != in_data)
251 in_section = in_data;
252 fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
256 /* Tell assembler to switch to read-only data section. This is normally
257 the text section. */
259 void
260 readonly_data_section (void)
262 #ifdef READONLY_DATA_SECTION
263 READONLY_DATA_SECTION (); /* Note this can call data_section. */
264 #else
265 #ifdef READONLY_DATA_SECTION_ASM_OP
266 if (in_section != in_readonly_data)
268 in_section = in_readonly_data;
269 fputs (READONLY_DATA_SECTION_ASM_OP, asm_out_file);
270 fputc ('\n', asm_out_file);
272 #else
273 text_section ();
274 #endif
275 #endif
278 /* Determine if we're in the text section. */
281 in_text_section (void)
283 return in_section == in_text;
286 /* Determine if we're in the unlikely-to-be-executed text section. */
289 in_unlikely_text_section (void)
291 bool ret_val;
293 if (cfun)
295 ret_val = ((in_section == in_unlikely_executed_text)
296 || (in_section == in_named
297 && cfun->unlikely_text_section_name
298 && strcmp (in_named_name,
299 cfun->unlikely_text_section_name) == 0));
301 else
303 ret_val = ((in_section == in_unlikely_executed_text)
304 || (in_section == in_named
305 && strcmp (in_named_name,
306 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0));
309 return ret_val;
312 /* Determine if we're in the data section. */
315 in_data_section (void)
317 return in_section == in_data;
320 /* Helper routines for maintaining in_named_htab. */
322 static int
323 in_named_entry_eq (const void *p1, const void *p2)
325 const struct in_named_entry *old = p1;
326 const char *new = p2;
328 return strcmp (old->name, new) == 0;
331 static hashval_t
332 in_named_entry_hash (const void *p)
334 const struct in_named_entry *old = p;
335 return htab_hash_string (old->name);
338 /* If SECTION has been seen before as a named section, return the flags
339 that were used. Otherwise, return 0. Note, that 0 is a perfectly valid
340 set of flags for a section to have, so 0 does not mean that the section
341 has not been seen. */
343 static unsigned int
344 get_named_section_flags (const char *section)
346 struct in_named_entry **slot;
348 slot = (struct in_named_entry **)
349 htab_find_slot_with_hash (in_named_htab, section,
350 htab_hash_string (section), NO_INSERT);
352 return slot ? (*slot)->flags : 0;
355 /* Returns true if the section has been declared before. Sets internal
356 flag on this section in in_named_hash so subsequent calls on this
357 section will return false. */
359 bool
360 named_section_first_declaration (const char *name)
362 struct in_named_entry **slot;
364 slot = (struct in_named_entry **)
365 htab_find_slot_with_hash (in_named_htab, name,
366 htab_hash_string (name), NO_INSERT);
367 if (! (*slot)->declared)
369 (*slot)->declared = true;
370 return true;
372 else
374 return false;
379 /* Record FLAGS for SECTION. If SECTION was previously recorded with a
380 different set of flags, return false. */
382 bool
383 set_named_section_flags (const char *section, unsigned int flags)
385 struct in_named_entry **slot, *entry;
387 slot = (struct in_named_entry **)
388 htab_find_slot_with_hash (in_named_htab, section,
389 htab_hash_string (section), INSERT);
390 entry = *slot;
392 if (!entry)
394 entry = ggc_alloc (sizeof (*entry));
395 *slot = entry;
396 entry->name = ggc_strdup (section);
397 entry->flags = flags;
398 entry->declared = false;
400 else if (entry->flags != flags)
401 return false;
403 return true;
406 /* Tell assembler to change to section NAME with attributes FLAGS. If
407 DECL is non-NULL, it is the VAR_DECL or FUNCTION_DECL with which
408 this section is associated. */
410 void
411 named_section_real (const char *name, unsigned int flags, tree decl)
413 if (in_section != in_named || strcmp (name, in_named_name) != 0)
415 bool unchanged = set_named_section_flags (name, flags);
417 gcc_assert (unchanged);
419 targetm.asm_out.named_section (name, flags, decl);
421 if (flags & SECTION_FORGET)
422 in_section = no_section;
423 else
425 in_named_name = ggc_strdup (name);
426 in_section = in_named;
430 if (in_text_section () || in_unlikely_text_section ())
432 last_text_section = in_section;
433 last_text_section_name = name;
437 /* Tell assembler to change to section NAME for DECL.
438 If DECL is NULL, just switch to section NAME.
439 If NAME is NULL, get the name from DECL.
440 If RELOC is 1, the initializer for DECL contains relocs. */
442 void
443 named_section (tree decl, const char *name, int reloc)
445 unsigned int flags;
447 gcc_assert (!decl || DECL_P (decl));
448 if (name == NULL)
449 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
451 if (strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0
452 && cfun
453 && ! cfun->unlikely_text_section_name)
454 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
456 flags = targetm.section_type_flags (decl, name, reloc);
458 /* Sanity check user variables for flag changes. Non-user
459 section flag changes will die in named_section_flags.
460 However, don't complain if SECTION_OVERRIDE is set.
461 We trust that the setter knows that it is safe to ignore
462 the default flags for this decl. */
463 if (decl && ! set_named_section_flags (name, flags))
465 flags = get_named_section_flags (name);
466 if ((flags & SECTION_OVERRIDE) == 0)
467 error ("%J%D causes a section type conflict", decl, decl);
470 named_section_real (name, flags, decl);
473 /* If required, set DECL_SECTION_NAME to a unique name. */
475 void
476 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
477 int flag_function_or_data_sections)
479 if (DECL_SECTION_NAME (decl) == NULL_TREE
480 && targetm.have_named_sections
481 && (flag_function_or_data_sections
482 || DECL_ONE_ONLY (decl)))
483 targetm.asm_out.unique_section (decl, reloc);
486 #ifdef BSS_SECTION_ASM_OP
488 /* Tell the assembler to switch to the bss section. */
490 void
491 bss_section (void)
493 if (in_section != in_bss)
495 fprintf (asm_out_file, "%s\n", BSS_SECTION_ASM_OP);
496 in_section = in_bss;
500 #ifdef ASM_OUTPUT_BSS
502 /* Utility function for ASM_OUTPUT_BSS for targets to use if
503 they don't support alignments in .bss.
504 ??? It is believed that this function will work in most cases so such
505 support is localized here. */
507 static void
508 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
509 const char *name,
510 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
511 unsigned HOST_WIDE_INT rounded)
513 targetm.asm_out.globalize_label (file, name);
514 bss_section ();
515 #ifdef ASM_DECLARE_OBJECT_NAME
516 last_assemble_variable_decl = decl;
517 ASM_DECLARE_OBJECT_NAME (file, name, decl);
518 #else
519 /* Standard thing is just output label for the object. */
520 ASM_OUTPUT_LABEL (file, name);
521 #endif /* ASM_DECLARE_OBJECT_NAME */
522 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
525 #endif
527 #ifdef ASM_OUTPUT_ALIGNED_BSS
529 /* Utility function for targets to use in implementing
530 ASM_OUTPUT_ALIGNED_BSS.
531 ??? It is believed that this function will work in most cases so such
532 support is localized here. */
534 static void
535 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
536 const char *name, unsigned HOST_WIDE_INT size,
537 int align)
539 bss_section ();
540 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
541 #ifdef ASM_DECLARE_OBJECT_NAME
542 last_assemble_variable_decl = decl;
543 ASM_DECLARE_OBJECT_NAME (file, name, decl);
544 #else
545 /* Standard thing is just output label for the object. */
546 ASM_OUTPUT_LABEL (file, name);
547 #endif /* ASM_DECLARE_OBJECT_NAME */
548 ASM_OUTPUT_SKIP (file, size ? size : 1);
551 #endif
553 #endif /* BSS_SECTION_ASM_OP */
555 /* Switch to the section for function DECL.
557 If DECL is NULL_TREE, switch to the text section. We can be passed
558 NULL_TREE under some circumstances by dbxout.c at least. */
560 void
561 function_section (tree decl)
563 int reloc = 0;
565 if (first_function_block_is_cold)
566 reloc = 1;
568 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
569 targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
570 #else
571 if (decl != NULL_TREE
572 && DECL_SECTION_NAME (decl) != NULL_TREE
573 && targetm.have_named_sections)
574 named_section (decl, (char *) 0, 0);
575 else
576 text_section ();
577 #endif
580 void
581 current_function_section (tree decl)
583 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
584 int reloc = 0;
586 if (in_unlikely_text_section ()
587 || last_text_section == in_unlikely_executed_text)
588 reloc = 1;
590 targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
591 #else
592 if (last_text_section == in_unlikely_executed_text)
593 unlikely_text_section ();
594 else if (last_text_section == in_text)
595 text_section ();
596 else if (last_text_section == in_named
597 && targetm.have_named_sections)
598 named_section (NULL_TREE, last_text_section_name, 0);
599 else
600 function_section (decl);
601 #endif
604 /* Switch to read-only data section associated with function DECL. */
606 void
607 default_function_rodata_section (tree decl)
609 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
611 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
613 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
615 size_t len = strlen (name) + 3;
616 char* rname = alloca (len);
618 strcpy (rname, ".rodata");
619 strcat (rname, name + 5);
620 named_section_real (rname, SECTION_LINKONCE, decl);
621 return;
623 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
624 else if (DECL_ONE_ONLY (decl)
625 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
627 size_t len = strlen (name) + 1;
628 char *rname = alloca (len);
630 memcpy (rname, name, len);
631 rname[14] = 'r';
632 named_section_real (rname, SECTION_LINKONCE, decl);
633 return;
635 /* For .text.foo we want to use .rodata.foo. */
636 else if (flag_function_sections && flag_data_sections
637 && strncmp (name, ".text.", 6) == 0)
639 size_t len = strlen (name) + 1;
640 char *rname = alloca (len + 2);
642 memcpy (rname, ".rodata", 7);
643 memcpy (rname + 7, name + 5, len - 5);
644 named_section_flags (rname, 0);
645 return;
649 readonly_data_section ();
652 /* Switch to read-only data section associated with function DECL
653 for targets where that section should be always the single
654 readonly data section. */
656 void
657 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
659 readonly_data_section ();
662 /* Switch to section for variable DECL. RELOC is the same as the
663 argument to SELECT_SECTION. */
665 void
666 variable_section (tree decl, int reloc)
668 if (IN_NAMED_SECTION (decl))
669 named_section (decl, NULL, reloc);
670 else
671 targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
674 /* Tell assembler to switch to the section for string merging. */
676 void
677 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
678 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
679 unsigned int flags ATTRIBUTE_UNUSED)
681 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
682 && TREE_CODE (decl) == STRING_CST
683 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
684 && align <= 256
685 && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
687 enum machine_mode mode;
688 unsigned int modesize;
689 const char *str;
690 int i, j, len, unit;
691 char name[30];
693 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
694 modesize = GET_MODE_BITSIZE (mode);
695 if (modesize >= 8 && modesize <= 256
696 && (modesize & (modesize - 1)) == 0)
698 if (align < modesize)
699 align = modesize;
701 str = TREE_STRING_POINTER (decl);
702 len = TREE_STRING_LENGTH (decl);
703 unit = GET_MODE_SIZE (mode);
705 /* Check for embedded NUL characters. */
706 for (i = 0; i < len; i += unit)
708 for (j = 0; j < unit; j++)
709 if (str[i + j] != '\0')
710 break;
711 if (j == unit)
712 break;
714 if (i == len - unit)
716 sprintf (name, ".rodata.str%d.%d", modesize / 8,
717 (int) (align / 8));
718 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
719 if (!i && modesize < align)
721 /* A "" string with requested alignment greater than
722 character size might cause a problem:
723 if some other string required even bigger
724 alignment than "", then linker might think the
725 "" is just part of padding after some other string
726 and not put it into the hash table initially.
727 But this means "" could have smaller alignment
728 than requested. */
729 #ifdef ASM_OUTPUT_SECTION_START
730 named_section_flags (name, flags);
731 ASM_OUTPUT_SECTION_START (asm_out_file);
732 #else
733 readonly_data_section ();
734 #endif
735 return;
738 named_section_flags (name, flags);
739 return;
744 readonly_data_section ();
747 /* Tell assembler to switch to the section for constant merging. */
749 void
750 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
751 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
752 unsigned int flags ATTRIBUTE_UNUSED)
754 unsigned int modesize = GET_MODE_BITSIZE (mode);
756 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
757 && mode != VOIDmode
758 && mode != BLKmode
759 && modesize <= align
760 && align >= 8
761 && align <= 256
762 && (align & (align - 1)) == 0)
764 char name[24];
766 sprintf (name, ".rodata.cst%d", (int) (align / 8));
767 flags |= (align / 8) | SECTION_MERGE;
768 named_section_flags (name, flags);
769 return;
772 readonly_data_section ();
775 /* Given NAME, a putative register name, discard any customary prefixes. */
777 static const char *
778 strip_reg_name (const char *name)
780 #ifdef REGISTER_PREFIX
781 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
782 name += strlen (REGISTER_PREFIX);
783 #endif
784 if (name[0] == '%' || name[0] == '#')
785 name++;
786 return name;
789 /* The user has asked for a DECL to have a particular name. Set (or
790 change) it in such a way that we don't prefix an underscore to
791 it. */
792 void
793 set_user_assembler_name (tree decl, const char *name)
795 char *starred = alloca (strlen (name) + 2);
796 starred[0] = '*';
797 strcpy (starred + 1, name);
798 change_decl_assembler_name (decl, get_identifier (starred));
799 SET_DECL_RTL (decl, NULL_RTX);
802 /* Decode an `asm' spec for a declaration as a register name.
803 Return the register number, or -1 if nothing specified,
804 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
805 or -3 if ASMSPEC is `cc' and is not recognized,
806 or -4 if ASMSPEC is `memory' and is not recognized.
807 Accept an exact spelling or a decimal number.
808 Prefixes such as % are optional. */
811 decode_reg_name (const char *asmspec)
813 if (asmspec != 0)
815 int i;
817 /* Get rid of confusing prefixes. */
818 asmspec = strip_reg_name (asmspec);
820 /* Allow a decimal number as a "register name". */
821 for (i = strlen (asmspec) - 1; i >= 0; i--)
822 if (! ISDIGIT (asmspec[i]))
823 break;
824 if (asmspec[0] != 0 && i < 0)
826 i = atoi (asmspec);
827 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
828 return i;
829 else
830 return -2;
833 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
834 if (reg_names[i][0]
835 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
836 return i;
838 #ifdef ADDITIONAL_REGISTER_NAMES
840 static const struct { const char *const name; const int number; } table[]
841 = ADDITIONAL_REGISTER_NAMES;
843 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
844 if (! strcmp (asmspec, table[i].name))
845 return table[i].number;
847 #endif /* ADDITIONAL_REGISTER_NAMES */
849 if (!strcmp (asmspec, "memory"))
850 return -4;
852 if (!strcmp (asmspec, "cc"))
853 return -3;
855 return -2;
858 return -1;
861 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
862 have static storage duration. In other words, it should not be an
863 automatic variable, including PARM_DECLs.
865 There is, however, one exception: this function handles variables
866 explicitly placed in a particular register by the user.
868 This is never called for PARM_DECL nodes. */
870 void
871 make_decl_rtl (tree decl)
873 const char *name = 0;
874 int reg_number;
875 rtx x;
877 /* Check that we are not being given an automatic variable. */
878 gcc_assert (TREE_CODE (decl) != PARM_DECL
879 && TREE_CODE (decl) != RESULT_DECL);
881 /* A weak alias has TREE_PUBLIC set but not the other bits. */
882 gcc_assert (TREE_CODE (decl) != VAR_DECL
883 || TREE_STATIC (decl)
884 || TREE_PUBLIC (decl)
885 || DECL_EXTERNAL (decl)
886 || DECL_REGISTER (decl));
888 /* And that we were not given a type or a label. */
889 gcc_assert (TREE_CODE (decl) != TYPE_DECL
890 && TREE_CODE (decl) != LABEL_DECL);
892 /* For a duplicate declaration, we can be called twice on the
893 same DECL node. Don't discard the RTL already made. */
894 if (DECL_RTL_SET_P (decl))
896 /* If the old RTL had the wrong mode, fix the mode. */
897 if (GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
898 SET_DECL_RTL (decl, adjust_address_nv (DECL_RTL (decl),
899 DECL_MODE (decl), 0));
901 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
902 return;
904 /* ??? Another way to do this would be to maintain a hashed
905 table of such critters. Instead of adding stuff to a DECL
906 to give certain attributes to it, we could use an external
907 hash map from DECL to set of attributes. */
909 /* Let the target reassign the RTL if it wants.
910 This is necessary, for example, when one machine specific
911 decl attribute overrides another. */
912 targetm.encode_section_info (decl, DECL_RTL (decl), false);
914 /* Make this function static known to the mudflap runtime. */
915 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
916 mudflap_enqueue_decl (decl);
918 return;
921 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
923 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
925 reg_number = decode_reg_name (name);
926 /* First detect errors in declaring global registers. */
927 if (reg_number == -1)
928 error ("%Jregister name not specified for %qD", decl, decl);
929 else if (reg_number < 0)
930 error ("%Jinvalid register name for %qD", decl, decl);
931 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
932 error ("%Jdata type of %qD isn%'t suitable for a register",
933 decl, decl);
934 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
935 error ("%Jregister specified for %qD isn%'t suitable for data type",
936 decl, decl);
937 /* Now handle properly declared static register variables. */
938 else
940 int nregs;
942 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
944 DECL_INITIAL (decl) = 0;
945 error ("global register variable has initial value");
947 if (TREE_THIS_VOLATILE (decl))
948 warning (0, "volatile register variables don%'t "
949 "work as you might wish");
951 /* If the user specified one of the eliminables registers here,
952 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
953 confused with that register and be eliminated. This usage is
954 somewhat suspect... */
956 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
957 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
958 REG_USERVAR_P (DECL_RTL (decl)) = 1;
960 if (TREE_STATIC (decl))
962 /* Make this register global, so not usable for anything
963 else. */
964 #ifdef ASM_DECLARE_REGISTER_GLOBAL
965 name = IDENTIFIER_POINTER (DECL_NAME (decl));
966 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
967 #endif
968 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
969 while (nregs > 0)
970 globalize_reg (reg_number + --nregs);
973 /* As a register variable, it has no section. */
974 return;
977 /* Now handle ordinary static variables and functions (in memory).
978 Also handle vars declared register invalidly. */
979 else if (name[0] == '*')
981 #ifdef REGISTER_PREFIX
982 if (strlen (REGISTER_PREFIX) != 0)
984 reg_number = decode_reg_name (name);
985 if (reg_number >= 0 || reg_number == -3)
986 error ("%Jregister name given for non-register variable %qD", decl, decl);
988 #endif
991 /* Specifying a section attribute on a variable forces it into a
992 non-.bss section, and thus it cannot be common. */
993 if (TREE_CODE (decl) == VAR_DECL
994 && DECL_SECTION_NAME (decl) != NULL_TREE
995 && DECL_INITIAL (decl) == NULL_TREE
996 && DECL_COMMON (decl))
997 DECL_COMMON (decl) = 0;
999 /* Variables can't be both common and weak. */
1000 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1001 DECL_COMMON (decl) = 0;
1003 x = gen_rtx_SYMBOL_REF (Pmode, name);
1004 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1005 SYMBOL_REF_DECL (x) = decl;
1007 x = gen_rtx_MEM (DECL_MODE (decl), x);
1008 if (TREE_CODE (decl) != FUNCTION_DECL)
1009 set_mem_attributes (x, decl, 1);
1010 SET_DECL_RTL (decl, x);
1012 /* Optionally set flags or add text to the name to record information
1013 such as that it is a function name.
1014 If the name is changed, the macro ASM_OUTPUT_LABELREF
1015 will have to know how to strip this information. */
1016 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1018 /* Make this function static known to the mudflap runtime. */
1019 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1020 mudflap_enqueue_decl (decl);
1023 /* Make the rtl for variable VAR be volatile.
1024 Use this only for static variables. */
1026 void
1027 make_var_volatile (tree var)
1029 gcc_assert (MEM_P (DECL_RTL (var)));
1031 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
1034 /* Output a string of literal assembler code
1035 for an `asm' keyword used between functions. */
1037 void
1038 assemble_asm (tree string)
1040 app_enable ();
1042 if (TREE_CODE (string) == ADDR_EXPR)
1043 string = TREE_OPERAND (string, 0);
1045 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1048 /* Record an element in the table of global destructors. SYMBOL is
1049 a SYMBOL_REF of the function to be called; PRIORITY is a number
1050 between 0 and MAX_INIT_PRIORITY. */
1052 void
1053 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1054 int priority ATTRIBUTE_UNUSED)
1056 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1057 /* Tell GNU LD that this is part of the static destructor set.
1058 This will work for any system that uses stabs, most usefully
1059 aout systems. */
1060 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1061 dbxout_stab_value_label (XSTR (symbol, 0));
1062 #else
1063 sorry ("global destructors not supported on this target");
1064 #endif
1067 void
1068 default_named_section_asm_out_destructor (rtx symbol, int priority)
1070 const char *section = ".dtors";
1071 char buf[16];
1073 /* ??? This only works reliably with the GNU linker. */
1074 if (priority != DEFAULT_INIT_PRIORITY)
1076 sprintf (buf, ".dtors.%.5u",
1077 /* Invert the numbering so the linker puts us in the proper
1078 order; constructors are run from right to left, and the
1079 linker sorts in increasing order. */
1080 MAX_INIT_PRIORITY - priority);
1081 section = buf;
1084 named_section_flags (section, SECTION_WRITE);
1085 assemble_align (POINTER_SIZE);
1086 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1089 #ifdef DTORS_SECTION_ASM_OP
1090 void
1091 dtors_section (void)
1093 if (in_section != in_dtors)
1095 in_section = in_dtors;
1096 fputs (DTORS_SECTION_ASM_OP, asm_out_file);
1097 fputc ('\n', asm_out_file);
1101 void
1102 default_dtor_section_asm_out_destructor (rtx symbol,
1103 int priority ATTRIBUTE_UNUSED)
1105 dtors_section ();
1106 assemble_align (POINTER_SIZE);
1107 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1109 #endif
1111 /* Likewise for global constructors. */
1113 void
1114 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1115 int priority ATTRIBUTE_UNUSED)
1117 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1118 /* Tell GNU LD that this is part of the static destructor set.
1119 This will work for any system that uses stabs, most usefully
1120 aout systems. */
1121 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1122 dbxout_stab_value_label (XSTR (symbol, 0));
1123 #else
1124 sorry ("global constructors not supported on this target");
1125 #endif
1128 void
1129 default_named_section_asm_out_constructor (rtx symbol, int priority)
1131 const char *section = ".ctors";
1132 char buf[16];
1134 /* ??? This only works reliably with the GNU linker. */
1135 if (priority != DEFAULT_INIT_PRIORITY)
1137 sprintf (buf, ".ctors.%.5u",
1138 /* Invert the numbering so the linker puts us in the proper
1139 order; constructors are run from right to left, and the
1140 linker sorts in increasing order. */
1141 MAX_INIT_PRIORITY - priority);
1142 section = buf;
1145 named_section_flags (section, SECTION_WRITE);
1146 assemble_align (POINTER_SIZE);
1147 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1150 #ifdef CTORS_SECTION_ASM_OP
1151 void
1152 ctors_section (void)
1154 if (in_section != in_ctors)
1156 in_section = in_ctors;
1157 fputs (CTORS_SECTION_ASM_OP, asm_out_file);
1158 fputc ('\n', asm_out_file);
1162 void
1163 default_ctor_section_asm_out_constructor (rtx symbol,
1164 int priority ATTRIBUTE_UNUSED)
1166 ctors_section ();
1167 assemble_align (POINTER_SIZE);
1168 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1170 #endif
1172 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1173 a nonzero value if the constant pool should be output before the
1174 start of the function, or a zero value if the pool should output
1175 after the end of the function. The default is to put it before the
1176 start. */
1178 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1179 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1180 #endif
1182 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1183 to be output to assembler.
1184 Set first_global_object_name and weak_global_object_name as appropriate. */
1186 void
1187 notice_global_symbol (tree decl)
1189 const char **type = &first_global_object_name;
1191 if (first_global_object_name
1192 || !TREE_PUBLIC (decl) || DECL_EXTERNAL (decl)
1193 || !DECL_NAME (decl)
1194 || (TREE_CODE (decl) != FUNCTION_DECL
1195 && (TREE_CODE (decl) != VAR_DECL
1196 || (DECL_COMMON (decl)
1197 && (DECL_INITIAL (decl) == 0
1198 || DECL_INITIAL (decl) == error_mark_node))))
1199 || !MEM_P (DECL_RTL (decl)))
1200 return;
1202 /* We win when global object is found, but it is useful to know about weak
1203 symbol as well so we can produce nicer unique names. */
1204 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1205 type = &weak_global_object_name;
1207 if (!*type)
1209 const char *p;
1210 char *name;
1211 rtx decl_rtl = DECL_RTL (decl);
1213 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1214 name = xstrdup (p);
1216 *type = name;
1220 /* Output assembler code for the constant pool of a function and associated
1221 with defining the name of the function. DECL describes the function.
1222 NAME is the function's name. For the constant pool, we use the current
1223 constant pool data. */
1225 void
1226 assemble_start_function (tree decl, const char *fnname)
1228 int align;
1229 char tmp_label[100];
1230 bool hot_label_written = false;
1232 cfun->unlikely_text_section_name = NULL;
1234 first_function_block_is_cold = false;
1235 if (flag_reorder_blocks_and_partition)
1237 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1238 cfun->hot_section_label = ggc_strdup (tmp_label);
1239 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1240 cfun->cold_section_label = ggc_strdup (tmp_label);
1241 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1242 cfun->hot_section_end_label = ggc_strdup (tmp_label);
1243 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1244 cfun->cold_section_end_label = ggc_strdup (tmp_label);
1245 const_labelno++;
1247 else
1249 cfun->hot_section_label = NULL;
1250 cfun->cold_section_label = NULL;
1251 cfun->hot_section_end_label = NULL;
1252 cfun->cold_section_end_label = NULL;
1255 /* The following code does not need preprocessing in the assembler. */
1257 app_disable ();
1259 if (CONSTANT_POOL_BEFORE_FUNCTION)
1260 output_constant_pool (fnname, decl);
1262 resolve_unique_section (decl, 0, flag_function_sections);
1264 /* Make sure the not and cold text (code) sections are properly
1265 aligned. This is necessary here in the case where the function
1266 has both hot and cold sections, because we don't want to re-set
1267 the alignment when the section switch happens mid-function. */
1269 if (flag_reorder_blocks_and_partition)
1271 unlikely_text_section ();
1272 assemble_align (FUNCTION_BOUNDARY);
1273 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_label);
1274 if (BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1276 /* Since the function starts with a cold section, we need to
1277 explicitly align the hot section and write out the hot
1278 section label. */
1279 text_section ();
1280 assemble_align (FUNCTION_BOUNDARY);
1281 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1282 hot_label_written = true;
1283 first_function_block_is_cold = true;
1286 else if (DECL_SECTION_NAME (decl))
1288 /* Calls to function_section rely on first_function_block_is_cold
1289 being accurate. The first block may be cold even if we aren't
1290 doing partitioning, if the entire function was decided by
1291 choose_function_section (predict.c) to be cold. */
1293 initialize_cold_section_name ();
1295 if (cfun->unlikely_text_section_name
1296 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1297 cfun->unlikely_text_section_name) == 0)
1298 first_function_block_is_cold = true;
1301 last_text_section = no_section;
1303 /* Switch to the correct text section for the start of the function. */
1305 function_section (decl);
1306 if (flag_reorder_blocks_and_partition
1307 && !hot_label_written)
1308 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1310 /* Tell assembler to move to target machine's alignment for functions. */
1311 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1312 if (align < force_align_functions_log)
1313 align = force_align_functions_log;
1314 if (align > 0)
1316 ASM_OUTPUT_ALIGN (asm_out_file, align);
1319 /* Handle a user-specified function alignment.
1320 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1321 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1322 if (align_functions_log > align
1323 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1325 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1326 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1327 align_functions_log, align_functions - 1);
1328 #else
1329 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1330 #endif
1333 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1334 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1335 #endif
1337 (*debug_hooks->begin_function) (decl);
1339 /* Make function name accessible from other files, if appropriate. */
1341 if (TREE_PUBLIC (decl))
1343 notice_global_symbol (decl);
1345 globalize_decl (decl);
1347 maybe_assemble_visibility (decl);
1350 if (DECL_PRESERVE_P (decl))
1351 targetm.asm_out.mark_decl_preserved (fnname);
1353 /* Do any machine/system dependent processing of the function name. */
1354 #ifdef ASM_DECLARE_FUNCTION_NAME
1355 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1356 #else
1357 /* Standard thing is just output label for the function. */
1358 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1359 #endif /* ASM_DECLARE_FUNCTION_NAME */
1361 insert_section_boundary_note ();
1364 /* Output assembler code associated with defining the size of the
1365 function. DECL describes the function. NAME is the function's name. */
1367 void
1368 assemble_end_function (tree decl, const char *fnname)
1370 #ifdef ASM_DECLARE_FUNCTION_SIZE
1371 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1372 #endif
1373 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1375 output_constant_pool (fnname, decl);
1376 function_section (decl); /* need to switch back */
1378 /* Output labels for end of hot/cold text sections (to be used by
1379 debug info.) */
1380 if (flag_reorder_blocks_and_partition)
1382 enum in_section save_text_section;
1384 save_text_section = in_section;
1385 unlikely_text_section ();
1386 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_end_label);
1387 if (first_function_block_is_cold)
1388 text_section ();
1389 else
1390 function_section (decl);
1391 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_end_label);
1392 if (save_text_section == in_unlikely_executed_text)
1393 unlikely_text_section ();
1397 /* Assemble code to leave SIZE bytes of zeros. */
1399 void
1400 assemble_zeros (unsigned HOST_WIDE_INT size)
1402 /* Do no output if -fsyntax-only. */
1403 if (flag_syntax_only)
1404 return;
1406 #ifdef ASM_NO_SKIP_IN_TEXT
1407 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1408 so we must output 0s explicitly in the text section. */
1409 if ((ASM_NO_SKIP_IN_TEXT && in_text_section ())
1410 || (ASM_NO_SKIP_IN_TEXT && in_unlikely_text_section ()))
1412 unsigned HOST_WIDE_INT i;
1413 for (i = 0; i < size; i++)
1414 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1416 else
1417 #endif
1418 if (size > 0)
1419 ASM_OUTPUT_SKIP (asm_out_file, size);
1422 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1424 void
1425 assemble_align (int align)
1427 if (align > BITS_PER_UNIT)
1429 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1433 /* Assemble a string constant with the specified C string as contents. */
1435 void
1436 assemble_string (const char *p, int size)
1438 int pos = 0;
1439 int maximum = 2000;
1441 /* If the string is very long, split it up. */
1443 while (pos < size)
1445 int thissize = size - pos;
1446 if (thissize > maximum)
1447 thissize = maximum;
1449 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1451 pos += thissize;
1452 p += thissize;
1457 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1458 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1459 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1460 #else
1461 #if defined ASM_OUTPUT_ALIGNED_LOCAL
1462 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1463 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl))
1464 #else
1465 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1466 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded)
1467 #endif
1468 #endif
1470 #if defined ASM_OUTPUT_ALIGNED_BSS
1471 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1472 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1473 #else
1474 #if defined ASM_OUTPUT_BSS
1475 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1476 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded)
1477 #else
1478 #undef ASM_EMIT_BSS
1479 #endif
1480 #endif
1482 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1483 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1484 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1485 #else
1486 #if defined ASM_OUTPUT_ALIGNED_COMMON
1487 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1488 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl))
1489 #else
1490 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1491 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded)
1492 #endif
1493 #endif
1495 static bool
1496 asm_emit_uninitialised (tree decl, const char *name,
1497 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1498 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1500 enum
1502 asm_dest_common,
1503 asm_dest_bss,
1504 asm_dest_local
1506 destination = asm_dest_local;
1508 /* ??? We should handle .bss via select_section mechanisms rather than
1509 via special target hooks. That would eliminate this special case. */
1510 if (TREE_PUBLIC (decl))
1512 if (!DECL_COMMON (decl))
1513 #ifdef ASM_EMIT_BSS
1514 destination = asm_dest_bss;
1515 #else
1516 return false;
1517 #endif
1518 else
1519 destination = asm_dest_common;
1522 if (destination != asm_dest_common)
1524 resolve_unique_section (decl, 0, flag_data_sections);
1525 /* Custom sections don't belong here. */
1526 if (DECL_SECTION_NAME (decl))
1527 return false;
1530 if (destination == asm_dest_bss)
1531 globalize_decl (decl);
1533 if (flag_shared_data)
1535 switch (destination)
1537 #ifdef ASM_OUTPUT_SHARED_BSS
1538 case asm_dest_bss:
1539 ASM_OUTPUT_SHARED_BSS (asm_out_file, decl, name, size, rounded);
1540 return;
1541 #endif
1542 #ifdef ASM_OUTPUT_SHARED_COMMON
1543 case asm_dest_common:
1544 ASM_OUTPUT_SHARED_COMMON (asm_out_file, name, size, rounded);
1545 return;
1546 #endif
1547 #ifdef ASM_OUTPUT_SHARED_LOCAL
1548 case asm_dest_local:
1549 ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded);
1550 return;
1551 #endif
1552 default:
1553 break;
1557 switch (destination)
1559 #ifdef ASM_EMIT_BSS
1560 case asm_dest_bss:
1561 ASM_EMIT_BSS (decl, name, size, rounded);
1562 break;
1563 #endif
1564 case asm_dest_common:
1565 ASM_EMIT_COMMON (decl, name, size, rounded);
1566 break;
1567 case asm_dest_local:
1568 ASM_EMIT_LOCAL (decl, name, size, rounded);
1569 break;
1570 default:
1571 gcc_unreachable ();
1574 return true;
1577 /* Assemble everything that is needed for a variable or function declaration.
1578 Not used for automatic variables, and not used for function definitions.
1579 Should not be called for variables of incomplete structure type.
1581 TOP_LEVEL is nonzero if this variable has file scope.
1582 AT_END is nonzero if this is the special handling, at end of compilation,
1583 to define things that have had only tentative definitions.
1584 DONT_OUTPUT_DATA if nonzero means don't actually output the
1585 initial value (that will be done by the caller). */
1587 void
1588 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1589 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1591 const char *name;
1592 unsigned int align;
1593 int reloc = 0;
1594 rtx decl_rtl;
1596 if (lang_hooks.decls.prepare_assemble_variable)
1597 lang_hooks.decls.prepare_assemble_variable (decl);
1599 last_assemble_variable_decl = 0;
1601 /* Normally no need to say anything here for external references,
1602 since assemble_external is called by the language-specific code
1603 when a declaration is first seen. */
1605 if (DECL_EXTERNAL (decl))
1606 return;
1608 /* Output no assembler code for a function declaration.
1609 Only definitions of functions output anything. */
1611 if (TREE_CODE (decl) == FUNCTION_DECL)
1612 return;
1614 /* Do nothing for global register variables. */
1615 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1617 TREE_ASM_WRITTEN (decl) = 1;
1618 return;
1621 /* If type was incomplete when the variable was declared,
1622 see if it is complete now. */
1624 if (DECL_SIZE (decl) == 0)
1625 layout_decl (decl, 0);
1627 /* Still incomplete => don't allocate it; treat the tentative defn
1628 (which is what it must have been) as an `extern' reference. */
1630 if (!dont_output_data && DECL_SIZE (decl) == 0)
1632 error ("%Jstorage size of %qD isn%'t known", decl, decl);
1633 TREE_ASM_WRITTEN (decl) = 1;
1634 return;
1637 /* The first declaration of a variable that comes through this function
1638 decides whether it is global (in C, has external linkage)
1639 or local (in C, has internal linkage). So do nothing more
1640 if this function has already run. */
1642 if (TREE_ASM_WRITTEN (decl))
1643 return;
1645 /* Make sure targetm.encode_section_info is invoked before we set
1646 ASM_WRITTEN. */
1647 decl_rtl = DECL_RTL (decl);
1649 TREE_ASM_WRITTEN (decl) = 1;
1651 /* Do no output if -fsyntax-only. */
1652 if (flag_syntax_only)
1653 return;
1655 app_disable ();
1657 if (! dont_output_data
1658 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1660 error ("%Jsize of variable %qD is too large", decl, decl);
1661 return;
1664 name = XSTR (XEXP (decl_rtl, 0), 0);
1665 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1666 notice_global_symbol (decl);
1668 /* Compute the alignment of this data. */
1670 align = DECL_ALIGN (decl);
1672 /* In the case for initialing an array whose length isn't specified,
1673 where we have not yet been able to do the layout,
1674 figure out the proper alignment now. */
1675 if (dont_output_data && DECL_SIZE (decl) == 0
1676 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1677 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1679 /* Some object file formats have a maximum alignment which they support.
1680 In particular, a.out format supports a maximum alignment of 4. */
1681 if (align > MAX_OFILE_ALIGNMENT)
1683 warning (0, "%Jalignment of %qD is greater than maximum object "
1684 "file alignment. Using %d", decl, decl,
1685 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1686 align = MAX_OFILE_ALIGNMENT;
1689 /* On some machines, it is good to increase alignment sometimes. */
1690 if (! DECL_USER_ALIGN (decl))
1692 #ifdef DATA_ALIGNMENT
1693 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1694 #endif
1695 #ifdef CONSTANT_ALIGNMENT
1696 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1697 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1698 #endif
1701 /* Reset the alignment in case we have made it tighter, so we can benefit
1702 from it in get_pointer_alignment. */
1703 DECL_ALIGN (decl) = align;
1704 set_mem_align (decl_rtl, align);
1706 if (TREE_PUBLIC (decl))
1707 maybe_assemble_visibility (decl);
1709 if (DECL_PRESERVE_P (decl))
1710 targetm.asm_out.mark_decl_preserved (name);
1712 /* Handle uninitialized definitions. */
1714 /* If the decl has been given an explicit section name, then it
1715 isn't common, and shouldn't be handled as such. */
1716 if (DECL_SECTION_NAME (decl) || dont_output_data)
1718 /* We don't implement common thread-local data at present. */
1719 else if (DECL_THREAD_LOCAL_P (decl))
1721 if (DECL_COMMON (decl))
1722 sorry ("thread-local COMMON data not implemented");
1724 else if (DECL_INITIAL (decl) == 0
1725 || DECL_INITIAL (decl) == error_mark_node
1726 || (flag_zero_initialized_in_bss
1727 /* Leave constant zeroes in .rodata so they can be shared. */
1728 && !TREE_READONLY (decl)
1729 && initializer_zerop (DECL_INITIAL (decl))))
1731 unsigned HOST_WIDE_INT size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1732 unsigned HOST_WIDE_INT rounded = size;
1734 /* Don't allocate zero bytes of common,
1735 since that means "undefined external" in the linker. */
1736 if (size == 0)
1737 rounded = 1;
1739 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1740 so that each uninitialized object starts on such a boundary. */
1741 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1742 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1743 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1745 #if !defined(ASM_OUTPUT_ALIGNED_COMMON) && !defined(ASM_OUTPUT_ALIGNED_DECL_COMMON) && !defined(ASM_OUTPUT_ALIGNED_BSS)
1746 if ((unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1747 warning (0, "%Jrequested alignment for %qD is greater than "
1748 "implemented alignment of %d", decl, decl, rounded);
1749 #endif
1751 /* If the target cannot output uninitialized but not common global data
1752 in .bss, then we have to use .data, so fall through. */
1753 if (asm_emit_uninitialised (decl, name, size, rounded))
1754 return;
1757 /* Handle initialized definitions.
1758 Also handle uninitialized global definitions if -fno-common and the
1759 target doesn't support ASM_OUTPUT_BSS. */
1761 /* First make the assembler name(s) global if appropriate. */
1762 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1763 globalize_decl (decl);
1765 /* Output any data that we will need to use the address of. */
1766 if (DECL_INITIAL (decl) == error_mark_node)
1767 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1768 else if (DECL_INITIAL (decl))
1770 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1771 output_addressed_constants (DECL_INITIAL (decl));
1774 /* Switch to the appropriate section. */
1775 resolve_unique_section (decl, reloc, flag_data_sections);
1776 variable_section (decl, reloc);
1778 /* dbxout.c needs to know this. */
1779 if (in_text_section () || in_unlikely_text_section ())
1780 DECL_IN_TEXT_SECTION (decl) = 1;
1782 /* Output the alignment of this data. */
1783 if (align > BITS_PER_UNIT)
1784 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1786 /* Do any machine/system dependent processing of the object. */
1787 #ifdef ASM_DECLARE_OBJECT_NAME
1788 last_assemble_variable_decl = decl;
1789 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1790 #else
1791 /* Standard thing is just output label for the object. */
1792 ASM_OUTPUT_LABEL (asm_out_file, name);
1793 #endif /* ASM_DECLARE_OBJECT_NAME */
1795 if (!dont_output_data)
1797 if (DECL_INITIAL (decl)
1798 && DECL_INITIAL (decl) != error_mark_node
1799 && !initializer_zerop (DECL_INITIAL (decl)))
1800 /* Output the actual data. */
1801 output_constant (DECL_INITIAL (decl),
1802 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1803 align);
1804 else
1805 /* Leave space for it. */
1806 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1810 /* Return 1 if type TYPE contains any pointers. */
1812 static int
1813 contains_pointers_p (tree type)
1815 switch (TREE_CODE (type))
1817 case POINTER_TYPE:
1818 case REFERENCE_TYPE:
1819 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1820 so I'll play safe and return 1. */
1821 case OFFSET_TYPE:
1822 return 1;
1824 case RECORD_TYPE:
1825 case UNION_TYPE:
1826 case QUAL_UNION_TYPE:
1828 tree fields;
1829 /* For a type that has fields, see if the fields have pointers. */
1830 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1831 if (TREE_CODE (fields) == FIELD_DECL
1832 && contains_pointers_p (TREE_TYPE (fields)))
1833 return 1;
1834 return 0;
1837 case ARRAY_TYPE:
1838 /* An array type contains pointers if its element type does. */
1839 return contains_pointers_p (TREE_TYPE (type));
1841 default:
1842 return 0;
1846 /* In unit-at-a-time mode, we delay assemble_external processing until
1847 the compilation unit is finalized. This is the best we can do for
1848 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1849 it all the way to final. See PR 17982 for further discussion. */
1850 static GTY(()) tree pending_assemble_externals;
1852 #ifdef ASM_OUTPUT_EXTERNAL
1853 /* True if DECL is a function decl for which no out-of-line copy exists.
1854 It is assumed that DECL's assembler name has been set. */
1856 static bool
1857 incorporeal_function_p (tree decl)
1859 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1861 const char *name;
1863 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1864 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1865 return true;
1867 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1868 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1869 return true;
1871 return false;
1874 /* Actually do the tests to determine if this is necessary, and invoke
1875 ASM_OUTPUT_EXTERNAL. */
1876 static void
1877 assemble_external_real (tree decl)
1879 rtx rtl = DECL_RTL (decl);
1881 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1882 && !SYMBOL_REF_USED (XEXP (rtl, 0))
1883 && !incorporeal_function_p (decl))
1885 /* Some systems do require some output. */
1886 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1887 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1890 #endif
1892 void
1893 process_pending_assemble_externals (void)
1895 #ifdef ASM_OUTPUT_EXTERNAL
1896 tree list;
1897 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1898 assemble_external_real (TREE_VALUE (list));
1900 pending_assemble_externals = 0;
1901 #endif
1904 /* Output something to declare an external symbol to the assembler.
1905 (Most assemblers don't need this, so we normally output nothing.)
1906 Do nothing if DECL is not external. */
1908 void
1909 assemble_external (tree decl ATTRIBUTE_UNUSED)
1911 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1912 main body of this code is only rarely exercised. To provide some
1913 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1914 open. If it's not, we should not be calling this function. */
1915 gcc_assert (asm_out_file);
1917 #ifdef ASM_OUTPUT_EXTERNAL
1918 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1919 return;
1921 if (flag_unit_at_a_time)
1922 pending_assemble_externals = tree_cons (0, decl,
1923 pending_assemble_externals);
1924 else
1925 assemble_external_real (decl);
1926 #endif
1929 /* Similar, for calling a library function FUN. */
1931 void
1932 assemble_external_libcall (rtx fun)
1934 /* Declare library function name external when first used, if nec. */
1935 if (! SYMBOL_REF_USED (fun))
1937 SYMBOL_REF_USED (fun) = 1;
1938 targetm.asm_out.external_libcall (fun);
1942 /* Assemble a label named NAME. */
1944 void
1945 assemble_label (const char *name)
1947 ASM_OUTPUT_LABEL (asm_out_file, name);
1950 /* Set the symbol_referenced flag for ID. */
1951 void
1952 mark_referenced (tree id)
1954 TREE_SYMBOL_REFERENCED (id) = 1;
1957 /* Set the symbol_referenced flag for DECL and notify callgraph. */
1958 void
1959 mark_decl_referenced (tree decl)
1961 if (TREE_CODE (decl) == FUNCTION_DECL)
1963 /* Extern inline functions don't become needed when referenced.
1964 If we know a method will be emitted in other TU and no new
1965 functions can be marked reachable, just use the external
1966 definition. */
1967 struct cgraph_node *node = cgraph_node (decl);
1968 if (!DECL_EXTERNAL (decl)
1969 && (!node->local.vtable_method || !cgraph_global_info_ready
1970 || !node->local.finalized))
1971 cgraph_mark_needed_node (node);
1973 else if (TREE_CODE (decl) == VAR_DECL)
1975 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
1976 cgraph_varpool_mark_needed_node (node);
1977 /* C++ frontend use mark_decl_references to force COMDAT variables
1978 to be output that might appear dead otherwise. */
1979 node->force_output = true;
1981 /* else do nothing - we can get various sorts of CST nodes here,
1982 which do not need to be marked. */
1985 /* Output to FILE (an assembly file) a reference to NAME. If NAME
1986 starts with a *, the rest of NAME is output verbatim. Otherwise
1987 NAME is transformed in a target-specific way (usually by the
1988 addition of an underscore). */
1990 void
1991 assemble_name_raw (FILE *file, const char *name)
1993 if (name[0] == '*')
1994 fputs (&name[1], file);
1995 else
1996 ASM_OUTPUT_LABELREF (file, name);
1999 /* Like assemble_name_raw, but should be used when NAME might refer to
2000 an entity that is also represented as a tree (like a function or
2001 variable). If NAME does refer to such an entity, that entity will
2002 be marked as referenced. */
2004 void
2005 assemble_name (FILE *file, const char *name)
2007 const char *real_name;
2008 tree id;
2010 real_name = targetm.strip_name_encoding (name);
2012 id = maybe_get_identifier (real_name);
2013 if (id)
2014 mark_referenced (id);
2016 assemble_name_raw (file, name);
2019 /* Allocate SIZE bytes writable static space with a gensym name
2020 and return an RTX to refer to its address. */
2023 assemble_static_space (unsigned HOST_WIDE_INT size)
2025 char name[12];
2026 const char *namestring;
2027 rtx x;
2029 #if 0
2030 if (flag_shared_data)
2031 data_section ();
2032 #endif
2034 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2035 ++const_labelno;
2036 namestring = ggc_strdup (name);
2038 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2039 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2041 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2042 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2043 BIGGEST_ALIGNMENT);
2044 #else
2045 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2046 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2047 #else
2049 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2050 so that each uninitialized object starts on such a boundary. */
2051 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2052 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2053 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2054 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2055 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2056 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2058 #endif
2059 #endif
2060 return x;
2063 /* Assemble the static constant template for function entry trampolines.
2064 This is done at most once per compilation.
2065 Returns an RTX for the address of the template. */
2067 static GTY(()) rtx initial_trampoline;
2069 #ifdef TRAMPOLINE_TEMPLATE
2071 assemble_trampoline_template (void)
2073 char label[256];
2074 const char *name;
2075 int align;
2076 rtx symbol;
2078 if (initial_trampoline)
2079 return initial_trampoline;
2081 /* By default, put trampoline templates in read-only data section. */
2083 #ifdef TRAMPOLINE_SECTION
2084 TRAMPOLINE_SECTION ();
2085 #else
2086 readonly_data_section ();
2087 #endif
2089 /* Write the assembler code to define one. */
2090 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2091 if (align > 0)
2093 ASM_OUTPUT_ALIGN (asm_out_file, align);
2096 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2097 TRAMPOLINE_TEMPLATE (asm_out_file);
2099 /* Record the rtl to refer to it. */
2100 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2101 name = ggc_strdup (label);
2102 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2103 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2105 initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2106 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2108 return initial_trampoline;
2110 #endif
2112 /* A and B are either alignments or offsets. Return the minimum alignment
2113 that may be assumed after adding the two together. */
2115 static inline unsigned
2116 min_align (unsigned int a, unsigned int b)
2118 return (a | b) & -(a | b);
2121 /* Return the assembler directive for creating a given kind of integer
2122 object. SIZE is the number of bytes in the object and ALIGNED_P
2123 indicates whether it is known to be aligned. Return NULL if the
2124 assembly dialect has no such directive.
2126 The returned string should be printed at the start of a new line and
2127 be followed immediately by the object's initial value. */
2129 const char *
2130 integer_asm_op (int size, int aligned_p)
2132 struct asm_int_op *ops;
2134 if (aligned_p)
2135 ops = &targetm.asm_out.aligned_op;
2136 else
2137 ops = &targetm.asm_out.unaligned_op;
2139 switch (size)
2141 case 1:
2142 return targetm.asm_out.byte_op;
2143 case 2:
2144 return ops->hi;
2145 case 4:
2146 return ops->si;
2147 case 8:
2148 return ops->di;
2149 case 16:
2150 return ops->ti;
2151 default:
2152 return NULL;
2156 /* Use directive OP to assemble an integer object X. Print OP at the
2157 start of the line, followed immediately by the value of X. */
2159 void
2160 assemble_integer_with_op (const char *op, rtx x)
2162 fputs (op, asm_out_file);
2163 output_addr_const (asm_out_file, x);
2164 fputc ('\n', asm_out_file);
2167 /* The default implementation of the asm_out.integer target hook. */
2169 bool
2170 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2171 unsigned int size ATTRIBUTE_UNUSED,
2172 int aligned_p ATTRIBUTE_UNUSED)
2174 const char *op = integer_asm_op (size, aligned_p);
2175 /* Avoid GAS bugs for large values. Specifically negative values whose
2176 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2177 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2178 return false;
2179 return op && (assemble_integer_with_op (op, x), true);
2182 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2183 the alignment of the integer in bits. Return 1 if we were able to output
2184 the constant, otherwise 0. We must be able to output the constant,
2185 if FORCE is nonzero. */
2187 bool
2188 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2190 int aligned_p;
2192 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2194 /* See if the target hook can handle this kind of object. */
2195 if (targetm.asm_out.integer (x, size, aligned_p))
2196 return true;
2198 /* If the object is a multi-byte one, try splitting it up. Split
2199 it into words it if is multi-word, otherwise split it into bytes. */
2200 if (size > 1)
2202 enum machine_mode omode, imode;
2203 unsigned int subalign;
2204 unsigned int subsize, i;
2206 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2207 subalign = MIN (align, subsize * BITS_PER_UNIT);
2208 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
2209 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2211 for (i = 0; i < size; i += subsize)
2213 rtx partial = simplify_subreg (omode, x, imode, i);
2214 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2215 break;
2217 if (i == size)
2218 return true;
2220 /* If we've printed some of it, but not all of it, there's no going
2221 back now. */
2222 gcc_assert (!i);
2225 gcc_assert (!force);
2227 return false;
2230 void
2231 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2233 long data[4];
2234 int i;
2235 int bitsize, nelts, nunits, units_per;
2237 /* This is hairy. We have a quantity of known size. real_to_target
2238 will put it into an array of *host* longs, 32 bits per element
2239 (even if long is more than 32 bits). We need to determine the
2240 number of array elements that are occupied (nelts) and the number
2241 of *target* min-addressable units that will be occupied in the
2242 object file (nunits). We cannot assume that 32 divides the
2243 mode's bitsize (size * BITS_PER_UNIT) evenly.
2245 size * BITS_PER_UNIT is used here to make sure that padding bits
2246 (which might appear at either end of the value; real_to_target
2247 will include the padding bits in its output array) are included. */
2249 nunits = GET_MODE_SIZE (mode);
2250 bitsize = nunits * BITS_PER_UNIT;
2251 nelts = CEIL (bitsize, 32);
2252 units_per = 32 / BITS_PER_UNIT;
2254 real_to_target (data, &d, mode);
2256 /* Put out the first word with the specified alignment. */
2257 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2258 nunits -= units_per;
2260 /* Subsequent words need only 32-bit alignment. */
2261 align = min_align (align, 32);
2263 for (i = 1; i < nelts; i++)
2265 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2266 nunits -= units_per;
2270 /* Given an expression EXP with a constant value,
2271 reduce it to the sum of an assembler symbol and an integer.
2272 Store them both in the structure *VALUE.
2273 EXP must be reducible. */
2275 struct addr_const GTY(())
2277 rtx base;
2278 HOST_WIDE_INT offset;
2281 static void
2282 decode_addr_const (tree exp, struct addr_const *value)
2284 tree target = TREE_OPERAND (exp, 0);
2285 int offset = 0;
2286 rtx x;
2288 while (1)
2290 if (TREE_CODE (target) == COMPONENT_REF
2291 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2294 offset += int_byte_position (TREE_OPERAND (target, 1));
2295 target = TREE_OPERAND (target, 0);
2297 else if (TREE_CODE (target) == ARRAY_REF
2298 || TREE_CODE (target) == ARRAY_RANGE_REF)
2300 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2301 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2302 target = TREE_OPERAND (target, 0);
2304 else
2305 break;
2308 switch (TREE_CODE (target))
2310 case VAR_DECL:
2311 case FUNCTION_DECL:
2312 x = DECL_RTL (target);
2313 break;
2315 case LABEL_DECL:
2316 x = gen_rtx_MEM (FUNCTION_MODE,
2317 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2318 break;
2320 case REAL_CST:
2321 case STRING_CST:
2322 case COMPLEX_CST:
2323 case CONSTRUCTOR:
2324 case INTEGER_CST:
2325 x = output_constant_def (target, 1);
2326 break;
2328 default:
2329 gcc_unreachable ();
2332 gcc_assert (MEM_P (x));
2333 x = XEXP (x, 0);
2335 value->base = x;
2336 value->offset = offset;
2339 /* Uniquize all constants that appear in memory.
2340 Each constant in memory thus far output is recorded
2341 in `const_desc_table'. */
2343 struct constant_descriptor_tree GTY(())
2345 /* A MEM for the constant. */
2346 rtx rtl;
2348 /* The value of the constant. */
2349 tree value;
2351 /* Hash of value. Computing the hash from value each time
2352 hashfn is called can't work properly, as that means recursive
2353 use of the hash table during hash table expansion. */
2354 hashval_t hash;
2357 static GTY((param_is (struct constant_descriptor_tree)))
2358 htab_t const_desc_htab;
2360 static struct constant_descriptor_tree * build_constant_desc (tree);
2361 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2363 /* Compute a hash code for a constant expression. */
2365 static hashval_t
2366 const_desc_hash (const void *ptr)
2368 return ((struct constant_descriptor_tree *)ptr)->hash;
2371 static hashval_t
2372 const_hash_1 (const tree exp)
2374 const char *p;
2375 hashval_t hi;
2376 int len, i;
2377 enum tree_code code = TREE_CODE (exp);
2379 /* Either set P and LEN to the address and len of something to hash and
2380 exit the switch or return a value. */
2382 switch (code)
2384 case INTEGER_CST:
2385 p = (char *) &TREE_INT_CST (exp);
2386 len = sizeof TREE_INT_CST (exp);
2387 break;
2389 case REAL_CST:
2390 return real_hash (TREE_REAL_CST_PTR (exp));
2392 case STRING_CST:
2393 p = TREE_STRING_POINTER (exp);
2394 len = TREE_STRING_LENGTH (exp);
2395 break;
2397 case COMPLEX_CST:
2398 return (const_hash_1 (TREE_REALPART (exp)) * 5
2399 + const_hash_1 (TREE_IMAGPART (exp)));
2401 case CONSTRUCTOR:
2403 tree link;
2405 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2407 for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2408 if (TREE_VALUE (link))
2409 hi = hi * 603 + const_hash_1 (TREE_VALUE (link));
2411 return hi;
2414 case ADDR_EXPR:
2415 case FDESC_EXPR:
2417 struct addr_const value;
2419 decode_addr_const (exp, &value);
2420 switch (GET_CODE (value.base))
2422 case SYMBOL_REF:
2423 /* Don't hash the address of the SYMBOL_REF;
2424 only use the offset and the symbol name. */
2425 hi = value.offset;
2426 p = XSTR (value.base, 0);
2427 for (i = 0; p[i] != 0; i++)
2428 hi = ((hi * 613) + (unsigned) (p[i]));
2429 break;
2431 case LABEL_REF:
2432 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2433 break;
2435 default:
2436 gcc_unreachable ();
2439 return hi;
2441 case PLUS_EXPR:
2442 case MINUS_EXPR:
2443 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2444 + const_hash_1 (TREE_OPERAND (exp, 1)));
2446 case NOP_EXPR:
2447 case CONVERT_EXPR:
2448 case NON_LVALUE_EXPR:
2449 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2451 default:
2452 /* A language specific constant. Just hash the code. */
2453 return code;
2456 /* Compute hashing function. */
2457 hi = len;
2458 for (i = 0; i < len; i++)
2459 hi = ((hi * 613) + (unsigned) (p[i]));
2461 return hi;
2464 /* Wrapper of compare_constant, for the htab interface. */
2465 static int
2466 const_desc_eq (const void *p1, const void *p2)
2468 const struct constant_descriptor_tree *c1 = p1;
2469 const struct constant_descriptor_tree *c2 = p2;
2470 if (c1->hash != c2->hash)
2471 return 0;
2472 return compare_constant (c1->value, c2->value);
2475 /* Compare t1 and t2, and return 1 only if they are known to result in
2476 the same bit pattern on output. */
2478 static int
2479 compare_constant (const tree t1, const tree t2)
2481 enum tree_code typecode;
2483 if (t1 == NULL_TREE)
2484 return t2 == NULL_TREE;
2485 if (t2 == NULL_TREE)
2486 return 0;
2488 if (TREE_CODE (t1) != TREE_CODE (t2))
2489 return 0;
2491 switch (TREE_CODE (t1))
2493 case INTEGER_CST:
2494 /* Integer constants are the same only if the same width of type. */
2495 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2496 return 0;
2497 return tree_int_cst_equal (t1, t2);
2499 case REAL_CST:
2500 /* Real constants are the same only if the same width of type. */
2501 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2502 return 0;
2504 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2506 case STRING_CST:
2507 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2508 return 0;
2510 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2511 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2512 TREE_STRING_LENGTH (t1)));
2514 case COMPLEX_CST:
2515 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2516 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2518 case CONSTRUCTOR:
2520 tree l1, l2;
2522 typecode = TREE_CODE (TREE_TYPE (t1));
2523 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2524 return 0;
2526 if (typecode == ARRAY_TYPE)
2528 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2529 /* For arrays, check that the sizes all match. */
2530 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2531 || size_1 == -1
2532 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2533 return 0;
2535 else
2537 /* For record and union constructors, require exact type
2538 equality. */
2539 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2540 return 0;
2543 for (l1 = CONSTRUCTOR_ELTS (t1), l2 = CONSTRUCTOR_ELTS (t2);
2544 l1 && l2;
2545 l1 = TREE_CHAIN (l1), l2 = TREE_CHAIN (l2))
2547 /* Check that each value is the same... */
2548 if (! compare_constant (TREE_VALUE (l1), TREE_VALUE (l2)))
2549 return 0;
2550 /* ... and that they apply to the same fields! */
2551 if (typecode == ARRAY_TYPE)
2553 if (! compare_constant (TREE_PURPOSE (l1),
2554 TREE_PURPOSE (l2)))
2555 return 0;
2557 else
2559 if (TREE_PURPOSE (l1) != TREE_PURPOSE (l2))
2560 return 0;
2564 return l1 == NULL_TREE && l2 == NULL_TREE;
2567 case ADDR_EXPR:
2568 case FDESC_EXPR:
2570 struct addr_const value1, value2;
2572 decode_addr_const (t1, &value1);
2573 decode_addr_const (t2, &value2);
2574 return (value1.offset == value2.offset
2575 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2578 case PLUS_EXPR:
2579 case MINUS_EXPR:
2580 case RANGE_EXPR:
2581 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2582 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2584 case NOP_EXPR:
2585 case CONVERT_EXPR:
2586 case NON_LVALUE_EXPR:
2587 case VIEW_CONVERT_EXPR:
2588 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2590 default:
2592 tree nt1, nt2;
2593 nt1 = lang_hooks.expand_constant (t1);
2594 nt2 = lang_hooks.expand_constant (t2);
2595 if (nt1 != t1 || nt2 != t2)
2596 return compare_constant (nt1, nt2);
2597 else
2598 return 0;
2602 gcc_unreachable ();
2605 /* Make a copy of the whole tree structure for a constant. This
2606 handles the same types of nodes that compare_constant handles. */
2608 static tree
2609 copy_constant (tree exp)
2611 switch (TREE_CODE (exp))
2613 case ADDR_EXPR:
2614 /* For ADDR_EXPR, we do not want to copy the decl whose address
2615 is requested. We do want to copy constants though. */
2616 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2617 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2618 copy_constant (TREE_OPERAND (exp, 0)));
2619 else
2620 return copy_node (exp);
2622 case INTEGER_CST:
2623 case REAL_CST:
2624 case STRING_CST:
2625 return copy_node (exp);
2627 case COMPLEX_CST:
2628 return build_complex (TREE_TYPE (exp),
2629 copy_constant (TREE_REALPART (exp)),
2630 copy_constant (TREE_IMAGPART (exp)));
2632 case PLUS_EXPR:
2633 case MINUS_EXPR:
2634 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2635 copy_constant (TREE_OPERAND (exp, 0)),
2636 copy_constant (TREE_OPERAND (exp, 1)));
2638 case NOP_EXPR:
2639 case CONVERT_EXPR:
2640 case NON_LVALUE_EXPR:
2641 case VIEW_CONVERT_EXPR:
2642 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2643 copy_constant (TREE_OPERAND (exp, 0)));
2645 case CONSTRUCTOR:
2647 tree copy = copy_node (exp);
2648 tree list = copy_list (CONSTRUCTOR_ELTS (exp));
2649 tree tail;
2651 CONSTRUCTOR_ELTS (copy) = list;
2652 for (tail = list; tail; tail = TREE_CHAIN (tail))
2653 TREE_VALUE (tail) = copy_constant (TREE_VALUE (tail));
2655 return copy;
2658 default:
2660 tree t = lang_hooks.expand_constant (exp);
2662 gcc_assert (t == exp);
2663 return copy_constant (t);
2668 /* Subroutine of output_constant_def:
2669 No constant equal to EXP is known to have been output.
2670 Make a constant descriptor to enter EXP in the hash table.
2671 Assign the label number and construct RTL to refer to the
2672 constant's location in memory.
2673 Caller is responsible for updating the hash table. */
2675 static struct constant_descriptor_tree *
2676 build_constant_desc (tree exp)
2678 rtx symbol;
2679 rtx rtl;
2680 char label[256];
2681 int labelno;
2682 struct constant_descriptor_tree *desc;
2684 desc = ggc_alloc (sizeof (*desc));
2685 desc->value = copy_constant (exp);
2687 /* Propagate marked-ness to copied constant. */
2688 if (flag_mudflap && mf_marked_p (exp))
2689 mf_mark (desc->value);
2691 /* Create a string containing the label name, in LABEL. */
2692 labelno = const_labelno++;
2693 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2695 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
2696 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2697 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2698 SYMBOL_REF_DECL (symbol) = desc->value;
2699 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2701 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2702 set_mem_attributes (rtl, exp, 1);
2703 set_mem_alias_set (rtl, 0);
2704 set_mem_alias_set (rtl, const_alias_set);
2706 /* Set flags or add text to the name to record information, such as
2707 that it is a local symbol. If the name is changed, the macro
2708 ASM_OUTPUT_LABELREF will have to know how to strip this
2709 information. This call might invalidate our local variable
2710 SYMBOL; we can't use it afterward. */
2712 targetm.encode_section_info (exp, rtl, true);
2714 desc->rtl = rtl;
2716 return desc;
2719 /* Return an rtx representing a reference to constant data in memory
2720 for the constant expression EXP.
2722 If assembler code for such a constant has already been output,
2723 return an rtx to refer to it.
2724 Otherwise, output such a constant in memory
2725 and generate an rtx for it.
2727 If DEFER is nonzero, this constant can be deferred and output only
2728 if referenced in the function after all optimizations.
2730 `const_desc_table' records which constants already have label strings. */
2733 output_constant_def (tree exp, int defer)
2735 struct constant_descriptor_tree *desc;
2736 struct constant_descriptor_tree key;
2737 void **loc;
2739 /* Look up EXP in the table of constant descriptors. If we didn't find
2740 it, create a new one. */
2741 key.value = exp;
2742 key.hash = const_hash_1 (exp);
2743 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
2745 desc = *loc;
2746 if (desc == 0)
2748 desc = build_constant_desc (exp);
2749 desc->hash = key.hash;
2750 *loc = desc;
2753 maybe_output_constant_def_contents (desc, defer);
2754 return desc->rtl;
2757 /* Subroutine of output_constant_def: Decide whether or not we need to
2758 output the constant DESC now, and if so, do it. */
2759 static void
2760 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2761 int defer)
2763 rtx symbol = XEXP (desc->rtl, 0);
2764 tree exp = desc->value;
2766 if (flag_syntax_only)
2767 return;
2769 if (TREE_ASM_WRITTEN (exp))
2770 /* Already output; don't do it again. */
2771 return;
2773 /* We can always defer constants as long as the context allows
2774 doing so. */
2775 if (defer)
2777 /* Increment n_deferred_constants if it exists. It needs to be at
2778 least as large as the number of constants actually referred to
2779 by the function. If it's too small we'll stop looking too early
2780 and fail to emit constants; if it's too large we'll only look
2781 through the entire function when we could have stopped earlier. */
2782 if (cfun)
2783 n_deferred_constants++;
2784 return;
2787 output_constant_def_contents (symbol);
2790 /* We must output the constant data referred to by SYMBOL; do so. */
2792 static void
2793 output_constant_def_contents (rtx symbol)
2795 tree exp = SYMBOL_REF_DECL (symbol);
2796 const char *label = XSTR (symbol, 0);
2797 HOST_WIDE_INT size;
2799 /* Make sure any other constants whose addresses appear in EXP
2800 are assigned label numbers. */
2801 int reloc = compute_reloc_for_constant (exp);
2803 /* Align the location counter as required by EXP's data type. */
2804 unsigned int align = TYPE_ALIGN (TREE_TYPE (exp));
2805 #ifdef CONSTANT_ALIGNMENT
2806 align = CONSTANT_ALIGNMENT (exp, align);
2807 #endif
2809 output_addressed_constants (exp);
2811 /* We are no longer deferring this constant. */
2812 TREE_ASM_WRITTEN (exp) = 1;
2814 if (IN_NAMED_SECTION (exp))
2815 named_section (exp, NULL, reloc);
2816 else
2817 targetm.asm_out.select_section (exp, reloc, align);
2819 if (align > BITS_PER_UNIT)
2821 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2824 size = int_size_in_bytes (TREE_TYPE (exp));
2825 if (TREE_CODE (exp) == STRING_CST)
2826 size = MAX (TREE_STRING_LENGTH (exp), size);
2828 /* Do any machine/system dependent processing of the constant. */
2829 #ifdef ASM_DECLARE_CONSTANT_NAME
2830 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2831 #else
2832 /* Standard thing is just output label for the constant. */
2833 ASM_OUTPUT_LABEL (asm_out_file, label);
2834 #endif /* ASM_DECLARE_CONSTANT_NAME */
2836 /* Output the value of EXP. */
2837 output_constant (exp, size, align);
2838 if (flag_mudflap)
2839 mudflap_enqueue_constant (exp);
2842 /* Look up EXP in the table of constant descriptors. Return the rtl
2843 if it has been emitted, else null. */
2846 lookup_constant_def (tree exp)
2848 struct constant_descriptor_tree *desc;
2849 struct constant_descriptor_tree key;
2851 key.value = exp;
2852 key.hash = const_hash_1 (exp);
2853 desc = htab_find_with_hash (const_desc_htab, &key, key.hash);
2855 return (desc ? desc->rtl : NULL_RTX);
2858 /* Used in the hash tables to avoid outputting the same constant
2859 twice. Unlike 'struct constant_descriptor_tree', RTX constants
2860 are output once per function, not once per file. */
2861 /* ??? Only a few targets need per-function constant pools. Most
2862 can use one per-file pool. Should add a targetm bit to tell the
2863 difference. */
2865 struct rtx_constant_pool GTY(())
2867 /* Pointers to first and last constant in pool, as ordered by offset. */
2868 struct constant_descriptor_rtx *first;
2869 struct constant_descriptor_rtx *last;
2871 /* Hash facility for making memory-constants from constant rtl-expressions.
2872 It is used on RISC machines where immediate integer arguments and
2873 constant addresses are restricted so that such constants must be stored
2874 in memory. */
2875 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
2876 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_sym_htab;
2878 /* Current offset in constant pool (does not include any
2879 machine-specific header). */
2880 HOST_WIDE_INT offset;
2883 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
2885 struct constant_descriptor_rtx *next;
2886 rtx mem;
2887 rtx sym;
2888 rtx constant;
2889 HOST_WIDE_INT offset;
2890 hashval_t hash;
2891 enum machine_mode mode;
2892 unsigned int align;
2893 int labelno;
2894 int mark;
2897 /* Hash and compare functions for const_rtx_htab. */
2899 static hashval_t
2900 const_desc_rtx_hash (const void *ptr)
2902 const struct constant_descriptor_rtx *desc = ptr;
2903 return desc->hash;
2906 static int
2907 const_desc_rtx_eq (const void *a, const void *b)
2909 const struct constant_descriptor_rtx *x = a;
2910 const struct constant_descriptor_rtx *y = b;
2912 if (x->mode != y->mode)
2913 return 0;
2914 return rtx_equal_p (x->constant, y->constant);
2917 /* Hash and compare functions for const_rtx_sym_htab. */
2919 static hashval_t
2920 const_desc_rtx_sym_hash (const void *ptr)
2922 const struct constant_descriptor_rtx *desc = ptr;
2923 return htab_hash_string (XSTR (desc->sym, 0));
2926 static int
2927 const_desc_rtx_sym_eq (const void *a, const void *b)
2929 const struct constant_descriptor_rtx *x = a;
2930 const struct constant_descriptor_rtx *y = b;
2931 return XSTR (x->sym, 0) == XSTR (y->sym, 0);
2934 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
2936 static int
2937 const_rtx_hash_1 (rtx *xp, void *data)
2939 unsigned HOST_WIDE_INT hwi;
2940 enum machine_mode mode;
2941 enum rtx_code code;
2942 hashval_t h, *hp;
2943 rtx x;
2945 x = *xp;
2946 code = GET_CODE (x);
2947 mode = GET_MODE (x);
2948 h = (hashval_t) code * 1048573 + mode;
2950 switch (code)
2952 case CONST_INT:
2953 hwi = INTVAL (x);
2954 fold_hwi:
2956 const int shift = sizeof (hashval_t) * CHAR_BIT;
2957 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
2958 int i;
2960 h ^= (hashval_t) hwi;
2961 for (i = 1; i < n; ++i)
2963 hwi >>= shift;
2964 h ^= (hashval_t) hwi;
2967 break;
2969 case CONST_DOUBLE:
2970 if (mode == VOIDmode)
2972 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
2973 goto fold_hwi;
2975 else
2976 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
2977 break;
2979 case CONST_VECTOR:
2981 int i;
2982 for (i = XVECLEN (x, 0); i-- > 0; )
2983 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
2985 break;
2987 case SYMBOL_REF:
2988 h ^= htab_hash_string (XSTR (x, 0));
2989 break;
2991 case LABEL_REF:
2992 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
2993 break;
2995 case UNSPEC:
2996 case UNSPEC_VOLATILE:
2997 h = h * 251 + XINT (x, 1);
2998 break;
3000 default:
3001 break;
3004 hp = data;
3005 *hp = *hp * 509 + h;
3006 return 0;
3009 /* Compute a hash value for X, which should be a constant. */
3011 static hashval_t
3012 const_rtx_hash (rtx x)
3014 hashval_t h = 0;
3015 for_each_rtx (&x, const_rtx_hash_1, &h);
3016 return h;
3020 /* Initialize constant pool hashing for a new function. */
3022 void
3023 init_varasm_status (struct function *f)
3025 struct varasm_status *p;
3026 struct rtx_constant_pool *pool;
3028 p = ggc_alloc (sizeof (struct varasm_status));
3029 f->varasm = p;
3031 pool = ggc_alloc (sizeof (struct rtx_constant_pool));
3032 p->pool = pool;
3033 p->deferred_constants = 0;
3035 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3036 const_desc_rtx_eq, NULL);
3037 pool->const_rtx_sym_htab = htab_create_ggc (31, const_desc_rtx_sym_hash,
3038 const_desc_rtx_sym_eq, NULL);
3039 pool->first = pool->last = NULL;
3040 pool->offset = 0;
3043 /* Given a MINUS expression, simplify it if both sides
3044 include the same symbol. */
3047 simplify_subtraction (rtx x)
3049 rtx r = simplify_rtx (x);
3050 return r ? r : x;
3053 /* Given a constant rtx X, make (or find) a memory constant for its value
3054 and return a MEM rtx to refer to it in memory. */
3057 force_const_mem (enum machine_mode mode, rtx x)
3059 struct constant_descriptor_rtx *desc, tmp;
3060 struct rtx_constant_pool *pool = cfun->varasm->pool;
3061 char label[256];
3062 rtx def, symbol;
3063 hashval_t hash;
3064 unsigned int align;
3065 void **slot;
3067 /* If we're not allowed to drop X into the constant pool, don't. */
3068 if (targetm.cannot_force_const_mem (x))
3069 return NULL_RTX;
3071 /* Lookup the value in the hashtable. */
3072 tmp.constant = x;
3073 tmp.mode = mode;
3074 hash = const_rtx_hash (x);
3075 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3076 desc = *slot;
3078 /* If the constant was already present, return its memory. */
3079 if (desc)
3080 return copy_rtx (desc->mem);
3082 /* Otherwise, create a new descriptor. */
3083 desc = ggc_alloc (sizeof (*desc));
3084 *slot = desc;
3086 /* Align the location counter as required by EXP's data type. */
3087 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3088 #ifdef CONSTANT_ALIGNMENT
3090 tree type = lang_hooks.types.type_for_mode (mode, 0);
3091 if (type != NULL_TREE)
3092 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3094 #endif
3096 pool->offset += (align / BITS_PER_UNIT) - 1;
3097 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3099 desc->next = NULL;
3100 desc->constant = tmp.constant;
3101 desc->offset = pool->offset;
3102 desc->hash = hash;
3103 desc->mode = mode;
3104 desc->align = align;
3105 desc->labelno = const_labelno;
3106 desc->mark = 0;
3108 pool->offset += GET_MODE_SIZE (mode);
3109 if (pool->last)
3110 pool->last->next = desc;
3111 else
3112 pool->first = pool->last = desc;
3113 pool->last = desc;
3115 /* Create a string containing the label name, in LABEL. */
3116 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3117 ++const_labelno;
3119 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3120 the constants pool. */
3121 desc->sym = symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3122 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
3123 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3124 current_function_uses_const_pool = 1;
3126 /* Insert the descriptor into the symbol cross-reference table too. */
3127 slot = htab_find_slot (pool->const_rtx_sym_htab, desc, INSERT);
3128 gcc_assert (!*slot);
3129 *slot = desc;
3131 /* Construct the MEM. */
3132 desc->mem = def = gen_const_mem (mode, symbol);
3133 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3134 set_mem_align (def, align);
3136 /* If we're dropping a label to the constant pool, make sure we
3137 don't delete it. */
3138 if (GET_CODE (x) == LABEL_REF)
3139 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3141 return copy_rtx (def);
3144 /* Given a SYMBOL_REF with CONSTANT_POOL_ADDRESS_P true, return a pointer to
3145 the corresponding constant_descriptor_rtx structure. */
3147 static struct constant_descriptor_rtx *
3148 find_pool_constant (struct rtx_constant_pool *pool, rtx sym)
3150 struct constant_descriptor_rtx tmp;
3151 tmp.sym = sym;
3152 return htab_find (pool->const_rtx_sym_htab, &tmp);
3155 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3158 get_pool_constant (rtx addr)
3160 return find_pool_constant (cfun->varasm->pool, addr)->constant;
3163 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3164 and whether it has been output or not. */
3167 get_pool_constant_mark (rtx addr, bool *pmarked)
3169 struct constant_descriptor_rtx *desc;
3171 desc = find_pool_constant (cfun->varasm->pool, addr);
3172 *pmarked = (desc->mark != 0);
3173 return desc->constant;
3176 /* Likewise, but for the constant pool of a specific function. */
3179 get_pool_constant_for_function (struct function *f, rtx addr)
3181 return find_pool_constant (f->varasm->pool, addr)->constant;
3184 /* Similar, return the mode. */
3186 enum machine_mode
3187 get_pool_mode (rtx addr)
3189 return find_pool_constant (cfun->varasm->pool, addr)->mode;
3192 /* Return the size of the constant pool. */
3195 get_pool_size (void)
3197 return cfun->varasm->pool->offset;
3200 /* Worker function for output_constant_pool_1. Emit assembly for X
3201 in MODE with known alignment ALIGN. */
3203 static void
3204 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3206 switch (GET_MODE_CLASS (mode))
3208 case MODE_FLOAT:
3210 REAL_VALUE_TYPE r;
3212 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3213 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3214 assemble_real (r, mode, align);
3215 break;
3218 case MODE_INT:
3219 case MODE_PARTIAL_INT:
3220 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3221 break;
3223 case MODE_VECTOR_FLOAT:
3224 case MODE_VECTOR_INT:
3226 int i, units;
3227 enum machine_mode submode = GET_MODE_INNER (mode);
3228 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3230 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3231 units = CONST_VECTOR_NUNITS (x);
3233 for (i = 0; i < units; i++)
3235 rtx elt = CONST_VECTOR_ELT (x, i);
3236 output_constant_pool_2 (submode, elt, i ? subalign : align);
3239 break;
3241 default:
3242 gcc_unreachable ();
3246 /* Worker function for output_constant_pool. Emit POOL. */
3248 static void
3249 output_constant_pool_1 (struct constant_descriptor_rtx *desc)
3251 rtx x, tmp;
3253 if (!desc->mark)
3254 return;
3255 x = desc->constant;
3257 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3258 whose CODE_LABEL has been deleted. This can occur if a jump table
3259 is eliminated by optimization. If so, write a constant of zero
3260 instead. Note that this can also happen by turning the
3261 CODE_LABEL into a NOTE. */
3262 /* ??? This seems completely and utterly wrong. Certainly it's
3263 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3264 functioning even with INSN_DELETED_P and friends. */
3266 tmp = x;
3267 switch (GET_CODE (x))
3269 case CONST:
3270 if (GET_CODE (XEXP (x, 0)) != PLUS
3271 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3272 break;
3273 tmp = XEXP (XEXP (x, 0), 0);
3274 /* FALLTHRU */
3276 case LABEL_REF:
3277 tmp = XEXP (x, 0);
3278 gcc_assert (!INSN_DELETED_P (tmp));
3279 gcc_assert (!NOTE_P (tmp)
3280 || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3281 break;
3283 default:
3284 break;
3287 /* First switch to correct section. */
3288 targetm.asm_out.select_rtx_section (desc->mode, x, desc->align);
3290 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3291 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3292 desc->align, desc->labelno, done);
3293 #endif
3295 assemble_align (desc->align);
3297 /* Output the label. */
3298 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3300 /* Output the data. */
3301 output_constant_pool_2 (desc->mode, x, desc->align);
3303 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3304 sections have proper size. */
3305 if (desc->align > GET_MODE_BITSIZE (desc->mode)
3306 && in_section == in_named
3307 && get_named_section_flags (in_named_name) & SECTION_MERGE)
3308 assemble_align (desc->align);
3310 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3311 done:
3312 #endif
3313 return;
3316 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3317 to as used. Emit referenced deferred strings. This function can
3318 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3320 static int
3321 mark_constant (rtx *current_rtx, void *data)
3323 struct rtx_constant_pool *pool = data;
3324 rtx x = *current_rtx;
3326 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3327 return 0;
3329 if (CONSTANT_POOL_ADDRESS_P (x))
3331 struct constant_descriptor_rtx *desc = find_pool_constant (pool, x);
3332 if (desc->mark == 0)
3334 desc->mark = 1;
3335 for_each_rtx (&desc->constant, mark_constant, pool);
3338 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3340 tree exp = SYMBOL_REF_DECL (x);
3341 if (!TREE_ASM_WRITTEN (exp))
3343 n_deferred_constants--;
3344 output_constant_def_contents (x);
3348 return -1;
3351 /* Look through appropriate parts of INSN, marking all entries in the
3352 constant pool which are actually being used. Entries that are only
3353 referenced by other constants are also marked as used. Emit
3354 deferred strings that are used. */
3356 static void
3357 mark_constants (struct rtx_constant_pool *pool, rtx insn)
3359 if (!INSN_P (insn))
3360 return;
3362 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3363 insns, not any notes that may be attached. We don't want to mark
3364 a constant just because it happens to appear in a REG_EQUIV note. */
3365 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3367 rtx seq = PATTERN (insn);
3368 int i, n = XVECLEN (seq, 0);
3369 for (i = 0; i < n; ++i)
3371 rtx subinsn = XVECEXP (seq, 0, i);
3372 if (INSN_P (subinsn))
3373 for_each_rtx (&PATTERN (subinsn), mark_constant, pool);
3376 else
3377 for_each_rtx (&PATTERN (insn), mark_constant, pool);
3380 /* Look through the instructions for this function, and mark all the
3381 entries in POOL which are actually being used. Emit deferred constants
3382 which have indeed been used. */
3384 static void
3385 mark_constant_pool (struct rtx_constant_pool *pool)
3387 rtx insn, link;
3389 if (pool->first == 0 && n_deferred_constants == 0)
3390 return;
3392 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3393 mark_constants (pool, insn);
3395 for (link = current_function_epilogue_delay_list;
3396 link;
3397 link = XEXP (link, 1))
3398 mark_constants (pool, XEXP (link, 0));
3401 /* Write all the constants in the constant pool. */
3403 void
3404 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3405 tree fndecl ATTRIBUTE_UNUSED)
3407 struct rtx_constant_pool *pool = cfun->varasm->pool;
3408 struct constant_descriptor_rtx *desc;
3410 /* It is possible for gcc to call force_const_mem and then to later
3411 discard the instructions which refer to the constant. In such a
3412 case we do not need to output the constant. */
3413 mark_constant_pool (pool);
3415 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3416 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3417 #endif
3419 for (desc = pool->first; desc ; desc = desc->next)
3420 output_constant_pool_1 (desc);
3422 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3423 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3424 #endif
3427 /* Determine what kind of relocations EXP may need. */
3430 compute_reloc_for_constant (tree exp)
3432 int reloc = 0, reloc2;
3433 tree tem;
3435 /* Give the front-end a chance to convert VALUE to something that
3436 looks more like a constant to the back-end. */
3437 exp = lang_hooks.expand_constant (exp);
3439 switch (TREE_CODE (exp))
3441 case ADDR_EXPR:
3442 case FDESC_EXPR:
3443 /* Go inside any operations that get_inner_reference can handle and see
3444 if what's inside is a constant: no need to do anything here for
3445 addresses of variables or functions. */
3446 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3447 tem = TREE_OPERAND (tem, 0))
3450 if (TREE_PUBLIC (tem))
3451 reloc |= 2;
3452 else
3453 reloc |= 1;
3454 break;
3456 case PLUS_EXPR:
3457 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3458 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3459 break;
3461 case MINUS_EXPR:
3462 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3463 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3464 /* The difference of two local labels is computable at link time. */
3465 if (reloc == 1 && reloc2 == 1)
3466 reloc = 0;
3467 else
3468 reloc |= reloc2;
3469 break;
3471 case NOP_EXPR:
3472 case CONVERT_EXPR:
3473 case NON_LVALUE_EXPR:
3474 case VIEW_CONVERT_EXPR:
3475 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3476 break;
3478 case CONSTRUCTOR:
3479 for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3480 if (TREE_VALUE (tem) != 0)
3481 reloc |= compute_reloc_for_constant (TREE_VALUE (tem));
3483 break;
3485 default:
3486 break;
3488 return reloc;
3491 /* Find all the constants whose addresses are referenced inside of EXP,
3492 and make sure assembler code with a label has been output for each one.
3493 Indicate whether an ADDR_EXPR has been encountered. */
3495 static void
3496 output_addressed_constants (tree exp)
3498 tree tem;
3500 /* Give the front-end a chance to convert VALUE to something that
3501 looks more like a constant to the back-end. */
3502 exp = lang_hooks.expand_constant (exp);
3504 switch (TREE_CODE (exp))
3506 case ADDR_EXPR:
3507 case FDESC_EXPR:
3508 /* Go inside any operations that get_inner_reference can handle and see
3509 if what's inside is a constant: no need to do anything here for
3510 addresses of variables or functions. */
3511 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3512 tem = TREE_OPERAND (tem, 0))
3515 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3516 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3517 tem = DECL_INITIAL (tem);
3519 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3520 output_constant_def (tem, 0);
3521 break;
3523 case PLUS_EXPR:
3524 case MINUS_EXPR:
3525 output_addressed_constants (TREE_OPERAND (exp, 1));
3526 /* Fall through. */
3528 case NOP_EXPR:
3529 case CONVERT_EXPR:
3530 case NON_LVALUE_EXPR:
3531 case VIEW_CONVERT_EXPR:
3532 output_addressed_constants (TREE_OPERAND (exp, 0));
3533 break;
3535 case CONSTRUCTOR:
3536 for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3537 if (TREE_VALUE (tem) != 0)
3538 output_addressed_constants (TREE_VALUE (tem));
3540 break;
3542 default:
3543 break;
3547 /* Return nonzero if VALUE is a valid constant-valued expression
3548 for use in initializing a static variable; one that can be an
3549 element of a "constant" initializer.
3551 Return null_pointer_node if the value is absolute;
3552 if it is relocatable, return the variable that determines the relocation.
3553 We assume that VALUE has been folded as much as possible;
3554 therefore, we do not need to check for such things as
3555 arithmetic-combinations of integers. */
3557 tree
3558 initializer_constant_valid_p (tree value, tree endtype)
3560 /* Give the front-end a chance to convert VALUE to something that
3561 looks more like a constant to the back-end. */
3562 value = lang_hooks.expand_constant (value);
3564 switch (TREE_CODE (value))
3566 case CONSTRUCTOR:
3567 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3568 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3569 && TREE_CONSTANT (value)
3570 && CONSTRUCTOR_ELTS (value))
3572 tree elt;
3573 bool absolute = true;
3575 for (elt = CONSTRUCTOR_ELTS (value); elt; elt = TREE_CHAIN (elt))
3577 tree reloc;
3578 value = TREE_VALUE (elt);
3579 reloc = initializer_constant_valid_p (value, TREE_TYPE (value));
3580 if (!reloc)
3581 return NULL_TREE;
3582 if (reloc != null_pointer_node)
3583 absolute = false;
3585 /* For a non-absolute relocation, there is no single
3586 variable that can be "the variable that determines the
3587 relocation." */
3588 return absolute ? null_pointer_node : error_mark_node;
3591 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3593 case INTEGER_CST:
3594 case VECTOR_CST:
3595 case REAL_CST:
3596 case STRING_CST:
3597 case COMPLEX_CST:
3598 return null_pointer_node;
3600 case ADDR_EXPR:
3601 case FDESC_EXPR:
3602 value = staticp (TREE_OPERAND (value, 0));
3603 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out to
3604 be a constant, this is old-skool offsetof-like nonsense. */
3605 if (value
3606 && TREE_CODE (value) == INDIRECT_REF
3607 && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3608 return null_pointer_node;
3609 /* Taking the address of a nested function involves a trampoline. */
3610 if (value
3611 && TREE_CODE (value) == FUNCTION_DECL
3612 && ((decl_function_context (value) && !DECL_NO_STATIC_CHAIN (value))
3613 || DECL_NON_ADDR_CONST_P (value)))
3614 return NULL_TREE;
3615 return value;
3617 case VIEW_CONVERT_EXPR:
3618 case NON_LVALUE_EXPR:
3619 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3621 case CONVERT_EXPR:
3622 case NOP_EXPR:
3624 tree src;
3625 tree src_type;
3626 tree dest_type;
3628 src = TREE_OPERAND (value, 0);
3629 src_type = TREE_TYPE (src);
3630 dest_type = TREE_TYPE (value);
3632 /* Allow conversions between pointer types, floating-point
3633 types, and offset types. */
3634 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3635 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3636 || (TREE_CODE (dest_type) == OFFSET_TYPE
3637 && TREE_CODE (src_type) == OFFSET_TYPE))
3638 return initializer_constant_valid_p (src, endtype);
3640 /* Allow length-preserving conversions between integer types. */
3641 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3642 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3643 return initializer_constant_valid_p (src, endtype);
3645 /* Allow conversions between other integer types only if
3646 explicit value. */
3647 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3649 tree inner = initializer_constant_valid_p (src, endtype);
3650 if (inner == null_pointer_node)
3651 return null_pointer_node;
3652 break;
3655 /* Allow (int) &foo provided int is as wide as a pointer. */
3656 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3657 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3658 return initializer_constant_valid_p (src, endtype);
3660 /* Likewise conversions from int to pointers, but also allow
3661 conversions from 0. */
3662 if ((POINTER_TYPE_P (dest_type)
3663 || TREE_CODE (dest_type) == OFFSET_TYPE)
3664 && INTEGRAL_TYPE_P (src_type))
3666 if (integer_zerop (src))
3667 return null_pointer_node;
3668 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3669 return initializer_constant_valid_p (src, endtype);
3672 /* Allow conversions to struct or union types if the value
3673 inside is okay. */
3674 if (TREE_CODE (dest_type) == RECORD_TYPE
3675 || TREE_CODE (dest_type) == UNION_TYPE)
3676 return initializer_constant_valid_p (src, endtype);
3678 break;
3680 case PLUS_EXPR:
3681 if (! INTEGRAL_TYPE_P (endtype)
3682 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3684 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3685 endtype);
3686 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3687 endtype);
3688 /* If either term is absolute, use the other terms relocation. */
3689 if (valid0 == null_pointer_node)
3690 return valid1;
3691 if (valid1 == null_pointer_node)
3692 return valid0;
3694 break;
3696 case MINUS_EXPR:
3697 if (! INTEGRAL_TYPE_P (endtype)
3698 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3700 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3701 endtype);
3702 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3703 endtype);
3704 /* Win if second argument is absolute. */
3705 if (valid1 == null_pointer_node)
3706 return valid0;
3707 /* Win if both arguments have the same relocation.
3708 Then the value is absolute. */
3709 if (valid0 == valid1 && valid0 != 0)
3710 return null_pointer_node;
3712 /* Since GCC guarantees that string constants are unique in the
3713 generated code, a subtraction between two copies of the same
3714 constant string is absolute. */
3715 if (valid0 && TREE_CODE (valid0) == STRING_CST
3716 && valid1 && TREE_CODE (valid1) == STRING_CST
3717 && operand_equal_p (valid0, valid1, 1))
3718 return null_pointer_node;
3721 /* Support narrowing differences. */
3722 if (INTEGRAL_TYPE_P (endtype))
3724 tree op0, op1;
3726 op0 = TREE_OPERAND (value, 0);
3727 op1 = TREE_OPERAND (value, 1);
3729 /* Like STRIP_NOPS except allow the operand mode to widen.
3730 This works around a feature of fold that simplifies
3731 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3732 that the narrower operation is cheaper. */
3734 while (TREE_CODE (op0) == NOP_EXPR
3735 || TREE_CODE (op0) == CONVERT_EXPR
3736 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3738 tree inner = TREE_OPERAND (op0, 0);
3739 if (inner == error_mark_node
3740 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3741 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3742 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3743 break;
3744 op0 = inner;
3747 while (TREE_CODE (op1) == NOP_EXPR
3748 || TREE_CODE (op1) == CONVERT_EXPR
3749 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3751 tree inner = TREE_OPERAND (op1, 0);
3752 if (inner == error_mark_node
3753 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3754 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3755 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3756 break;
3757 op1 = inner;
3760 op0 = initializer_constant_valid_p (op0, endtype);
3761 op1 = initializer_constant_valid_p (op1, endtype);
3763 /* Both initializers must be known. */
3764 if (op0 && op1)
3766 if (op0 == op1)
3767 return null_pointer_node;
3769 /* Support differences between labels. */
3770 if (TREE_CODE (op0) == LABEL_DECL
3771 && TREE_CODE (op1) == LABEL_DECL)
3772 return null_pointer_node;
3774 if (TREE_CODE (op0) == STRING_CST
3775 && TREE_CODE (op1) == STRING_CST
3776 && operand_equal_p (op0, op1, 1))
3777 return null_pointer_node;
3780 break;
3782 default:
3783 break;
3786 return 0;
3789 /* Output assembler code for constant EXP to FILE, with no label.
3790 This includes the pseudo-op such as ".int" or ".byte", and a newline.
3791 Assumes output_addressed_constants has been done on EXP already.
3793 Generate exactly SIZE bytes of assembler data, padding at the end
3794 with zeros if necessary. SIZE must always be specified.
3796 SIZE is important for structure constructors,
3797 since trailing members may have been omitted from the constructor.
3798 It is also important for initialization of arrays from string constants
3799 since the full length of the string constant might not be wanted.
3800 It is also needed for initialization of unions, where the initializer's
3801 type is just one member, and that may not be as long as the union.
3803 There a case in which we would fail to output exactly SIZE bytes:
3804 for a structure constructor that wants to produce more than SIZE bytes.
3805 But such constructors will never be generated for any possible input.
3807 ALIGN is the alignment of the data in bits. */
3809 void
3810 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3812 enum tree_code code;
3813 unsigned HOST_WIDE_INT thissize;
3815 /* Some front-ends use constants other than the standard language-independent
3816 varieties, but which may still be output directly. Give the front-end a
3817 chance to convert EXP to a language-independent representation. */
3818 exp = lang_hooks.expand_constant (exp);
3820 if (size == 0 || flag_syntax_only)
3821 return;
3823 /* Eliminate any conversions since we'll be outputting the underlying
3824 constant. */
3825 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
3826 || TREE_CODE (exp) == NON_LVALUE_EXPR
3827 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
3828 exp = TREE_OPERAND (exp, 0);
3830 code = TREE_CODE (TREE_TYPE (exp));
3831 thissize = int_size_in_bytes (TREE_TYPE (exp));
3833 /* Allow a constructor with no elements for any data type.
3834 This means to fill the space with zeros. */
3835 if (TREE_CODE (exp) == CONSTRUCTOR && CONSTRUCTOR_ELTS (exp) == 0)
3837 assemble_zeros (size);
3838 return;
3841 if (TREE_CODE (exp) == FDESC_EXPR)
3843 #ifdef ASM_OUTPUT_FDESC
3844 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
3845 tree decl = TREE_OPERAND (exp, 0);
3846 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
3847 #else
3848 gcc_unreachable ();
3849 #endif
3850 return;
3853 /* Now output the underlying data. If we've handling the padding, return.
3854 Otherwise, break and ensure SIZE is the size written. */
3855 switch (code)
3857 case CHAR_TYPE:
3858 case BOOLEAN_TYPE:
3859 case INTEGER_TYPE:
3860 case ENUMERAL_TYPE:
3861 case POINTER_TYPE:
3862 case REFERENCE_TYPE:
3863 case OFFSET_TYPE:
3864 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3865 EXPAND_INITIALIZER),
3866 MIN (size, thissize), align, 0))
3867 error ("initializer for integer value is too complicated");
3868 break;
3870 case REAL_TYPE:
3871 if (TREE_CODE (exp) != REAL_CST)
3872 error ("initializer for floating value is not a floating constant");
3874 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3875 break;
3877 case COMPLEX_TYPE:
3878 output_constant (TREE_REALPART (exp), thissize / 2, align);
3879 output_constant (TREE_IMAGPART (exp), thissize / 2,
3880 min_align (align, BITS_PER_UNIT * (thissize / 2)));
3881 break;
3883 case ARRAY_TYPE:
3884 case VECTOR_TYPE:
3885 switch (TREE_CODE (exp))
3887 case CONSTRUCTOR:
3888 output_constructor (exp, size, align);
3889 return;
3890 case STRING_CST:
3891 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3892 size);
3893 assemble_string (TREE_STRING_POINTER (exp), thissize);
3894 break;
3896 case VECTOR_CST:
3898 int elt_size;
3899 tree link;
3900 unsigned int nalign;
3901 enum machine_mode inner;
3903 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
3904 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3906 elt_size = GET_MODE_SIZE (inner);
3908 link = TREE_VECTOR_CST_ELTS (exp);
3909 output_constant (TREE_VALUE (link), elt_size, align);
3910 while ((link = TREE_CHAIN (link)) != NULL)
3911 output_constant (TREE_VALUE (link), elt_size, nalign);
3912 break;
3914 default:
3915 gcc_unreachable ();
3917 break;
3919 case RECORD_TYPE:
3920 case UNION_TYPE:
3921 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
3922 output_constructor (exp, size, align);
3923 return;
3925 case ERROR_MARK:
3926 return;
3928 default:
3929 gcc_unreachable ();
3932 if (size > thissize)
3933 assemble_zeros (size - thissize);
3937 /* Subroutine of output_constructor, used for computing the size of
3938 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
3939 type with an unspecified upper bound. */
3941 static unsigned HOST_WIDE_INT
3942 array_size_for_constructor (tree val)
3944 tree max_index, i;
3946 /* This code used to attempt to handle string constants that are not
3947 arrays of single-bytes, but nothing else does, so there's no point in
3948 doing it here. */
3949 if (TREE_CODE (val) == STRING_CST)
3950 return TREE_STRING_LENGTH (val);
3952 max_index = NULL_TREE;
3953 for (i = CONSTRUCTOR_ELTS (val); i; i = TREE_CHAIN (i))
3955 tree index = TREE_PURPOSE (i);
3957 if (TREE_CODE (index) == RANGE_EXPR)
3958 index = TREE_OPERAND (index, 1);
3959 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
3960 max_index = index;
3963 if (max_index == NULL_TREE)
3964 return 0;
3966 /* Compute the total number of array elements. */
3967 i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
3968 convert (sizetype,
3969 TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
3970 i = size_binop (PLUS_EXPR, i, convert (sizetype, integer_one_node));
3972 /* Multiply by the array element unit size to find number of bytes. */
3973 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
3975 return tree_low_cst (i, 1);
3978 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
3979 Generate at least SIZE bytes, padding if necessary. */
3981 static void
3982 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
3983 unsigned int align)
3985 tree type = TREE_TYPE (exp);
3986 tree link, field = 0;
3987 tree min_index = 0;
3988 /* Number of bytes output or skipped so far.
3989 In other words, current position within the constructor. */
3990 HOST_WIDE_INT total_bytes = 0;
3991 /* Nonzero means BYTE contains part of a byte, to be output. */
3992 int byte_buffer_in_use = 0;
3993 int byte = 0;
3995 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
3997 if (TREE_CODE (type) == RECORD_TYPE)
3998 field = TYPE_FIELDS (type);
4000 if (TREE_CODE (type) == ARRAY_TYPE
4001 && TYPE_DOMAIN (type) != 0)
4002 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
4004 /* As LINK goes through the elements of the constant,
4005 FIELD goes through the structure fields, if the constant is a structure.
4006 if the constant is a union, then we override this,
4007 by getting the field from the TREE_LIST element.
4008 But the constant could also be an array. Then FIELD is zero.
4010 There is always a maximum of one element in the chain LINK for unions
4011 (even if the initializer in a source program incorrectly contains
4012 more one). */
4013 for (link = CONSTRUCTOR_ELTS (exp);
4014 link;
4015 link = TREE_CHAIN (link),
4016 field = field ? TREE_CHAIN (field) : 0)
4018 tree val = TREE_VALUE (link);
4019 tree index = 0;
4021 /* The element in a union constructor specifies the proper field
4022 or index. */
4023 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
4024 || TREE_CODE (type) == QUAL_UNION_TYPE)
4025 && TREE_PURPOSE (link) != 0)
4026 field = TREE_PURPOSE (link);
4028 else if (TREE_CODE (type) == ARRAY_TYPE)
4029 index = TREE_PURPOSE (link);
4031 #ifdef ASM_COMMENT_START
4032 if (field && flag_verbose_asm)
4033 fprintf (asm_out_file, "%s %s:\n",
4034 ASM_COMMENT_START,
4035 DECL_NAME (field)
4036 ? IDENTIFIER_POINTER (DECL_NAME (field))
4037 : "<anonymous>");
4038 #endif
4040 /* Eliminate the marker that makes a cast not be an lvalue. */
4041 if (val != 0)
4042 STRIP_NOPS (val);
4044 if (index && TREE_CODE (index) == RANGE_EXPR)
4046 unsigned HOST_WIDE_INT fieldsize
4047 = int_size_in_bytes (TREE_TYPE (type));
4048 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
4049 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
4050 HOST_WIDE_INT index;
4051 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
4053 for (index = lo_index; index <= hi_index; index++)
4055 /* Output the element's initial value. */
4056 if (val == 0)
4057 assemble_zeros (fieldsize);
4058 else
4059 output_constant (val, fieldsize, align2);
4061 /* Count its size. */
4062 total_bytes += fieldsize;
4065 else if (field == 0 || !DECL_BIT_FIELD (field))
4067 /* An element that is not a bit-field. */
4069 unsigned HOST_WIDE_INT fieldsize;
4070 /* Since this structure is static,
4071 we know the positions are constant. */
4072 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
4073 unsigned int align2;
4075 if (index != 0)
4076 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
4077 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
4079 /* Output any buffered-up bit-fields preceding this element. */
4080 if (byte_buffer_in_use)
4082 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4083 total_bytes++;
4084 byte_buffer_in_use = 0;
4087 /* Advance to offset of this element.
4088 Note no alignment needed in an array, since that is guaranteed
4089 if each element has the proper size. */
4090 if ((field != 0 || index != 0) && pos != total_bytes)
4092 assemble_zeros (pos - total_bytes);
4093 total_bytes = pos;
4096 /* Find the alignment of this element. */
4097 align2 = min_align (align, BITS_PER_UNIT * pos);
4099 /* Determine size this element should occupy. */
4100 if (field)
4102 fieldsize = 0;
4104 /* If this is an array with an unspecified upper bound,
4105 the initializer determines the size. */
4106 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4107 but we cannot do this until the deprecated support for
4108 initializing zero-length array members is removed. */
4109 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4110 && TYPE_DOMAIN (TREE_TYPE (field))
4111 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4113 fieldsize = array_size_for_constructor (val);
4114 /* Given a non-empty initialization, this field had
4115 better be last. */
4116 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4118 else if (DECL_SIZE_UNIT (field))
4120 /* ??? This can't be right. If the decl size overflows
4121 a host integer we will silently emit no data. */
4122 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4123 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4126 else
4127 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4129 /* Output the element's initial value. */
4130 if (val == 0)
4131 assemble_zeros (fieldsize);
4132 else
4133 output_constant (val, fieldsize, align2);
4135 /* Count its size. */
4136 total_bytes += fieldsize;
4138 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4139 error ("invalid initial value for member %qs",
4140 IDENTIFIER_POINTER (DECL_NAME (field)));
4141 else
4143 /* Element that is a bit-field. */
4145 HOST_WIDE_INT next_offset = int_bit_position (field);
4146 HOST_WIDE_INT end_offset
4147 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4149 if (val == 0)
4150 val = integer_zero_node;
4152 /* If this field does not start in this (or, next) byte,
4153 skip some bytes. */
4154 if (next_offset / BITS_PER_UNIT != total_bytes)
4156 /* Output remnant of any bit field in previous bytes. */
4157 if (byte_buffer_in_use)
4159 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4160 total_bytes++;
4161 byte_buffer_in_use = 0;
4164 /* If still not at proper byte, advance to there. */
4165 if (next_offset / BITS_PER_UNIT != total_bytes)
4167 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4168 total_bytes = next_offset / BITS_PER_UNIT;
4172 if (! byte_buffer_in_use)
4173 byte = 0;
4175 /* We must split the element into pieces that fall within
4176 separate bytes, and combine each byte with previous or
4177 following bit-fields. */
4179 /* next_offset is the offset n fbits from the beginning of
4180 the structure to the next bit of this element to be processed.
4181 end_offset is the offset of the first bit past the end of
4182 this element. */
4183 while (next_offset < end_offset)
4185 int this_time;
4186 int shift;
4187 HOST_WIDE_INT value;
4188 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4189 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4191 /* Advance from byte to byte
4192 within this element when necessary. */
4193 while (next_byte != total_bytes)
4195 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4196 total_bytes++;
4197 byte = 0;
4200 /* Number of bits we can process at once
4201 (all part of the same byte). */
4202 this_time = MIN (end_offset - next_offset,
4203 BITS_PER_UNIT - next_bit);
4204 if (BYTES_BIG_ENDIAN)
4206 /* On big-endian machine, take the most significant bits
4207 first (of the bits that are significant)
4208 and put them into bytes from the most significant end. */
4209 shift = end_offset - next_offset - this_time;
4211 /* Don't try to take a bunch of bits that cross
4212 the word boundary in the INTEGER_CST. We can
4213 only select bits from the LOW or HIGH part
4214 not from both. */
4215 if (shift < HOST_BITS_PER_WIDE_INT
4216 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4218 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4219 shift = HOST_BITS_PER_WIDE_INT;
4222 /* Now get the bits from the appropriate constant word. */
4223 if (shift < HOST_BITS_PER_WIDE_INT)
4224 value = TREE_INT_CST_LOW (val);
4225 else
4227 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4228 value = TREE_INT_CST_HIGH (val);
4229 shift -= HOST_BITS_PER_WIDE_INT;
4232 /* Get the result. This works only when:
4233 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4234 byte |= (((value >> shift)
4235 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4236 << (BITS_PER_UNIT - this_time - next_bit));
4238 else
4240 /* On little-endian machines,
4241 take first the least significant bits of the value
4242 and pack them starting at the least significant
4243 bits of the bytes. */
4244 shift = next_offset - int_bit_position (field);
4246 /* Don't try to take a bunch of bits that cross
4247 the word boundary in the INTEGER_CST. We can
4248 only select bits from the LOW or HIGH part
4249 not from both. */
4250 if (shift < HOST_BITS_PER_WIDE_INT
4251 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4252 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4254 /* Now get the bits from the appropriate constant word. */
4255 if (shift < HOST_BITS_PER_WIDE_INT)
4256 value = TREE_INT_CST_LOW (val);
4257 else
4259 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4260 value = TREE_INT_CST_HIGH (val);
4261 shift -= HOST_BITS_PER_WIDE_INT;
4264 /* Get the result. This works only when:
4265 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4266 byte |= (((value >> shift)
4267 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4268 << next_bit);
4271 next_offset += this_time;
4272 byte_buffer_in_use = 1;
4277 if (byte_buffer_in_use)
4279 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4280 total_bytes++;
4283 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4284 assemble_zeros (size - total_bytes);
4287 /* This TREE_LIST contains any weak symbol declarations waiting
4288 to be emitted. */
4289 static GTY(()) tree weak_decls;
4291 /* Mark DECL as weak. */
4293 static void
4294 mark_weak (tree decl)
4296 DECL_WEAK (decl) = 1;
4298 if (DECL_RTL_SET_P (decl)
4299 && MEM_P (DECL_RTL (decl))
4300 && XEXP (DECL_RTL (decl), 0)
4301 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4302 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4305 /* Merge weak status between NEWDECL and OLDDECL. */
4307 void
4308 merge_weak (tree newdecl, tree olddecl)
4310 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4312 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4314 tree *pwd;
4315 /* We put the NEWDECL on the weak_decls list at some point
4316 and OLDDECL as well. Keep just OLDDECL on the list. */
4317 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4318 if (TREE_VALUE (*pwd) == newdecl)
4320 *pwd = TREE_CHAIN (*pwd);
4321 break;
4324 return;
4327 if (DECL_WEAK (newdecl))
4329 tree wd;
4331 /* NEWDECL is weak, but OLDDECL is not. */
4333 /* If we already output the OLDDECL, we're in trouble; we can't
4334 go back and make it weak. This error cannot caught in
4335 declare_weak because the NEWDECL and OLDDECL was not yet
4336 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4337 if (TREE_ASM_WRITTEN (olddecl))
4338 error ("%Jweak declaration of %qD must precede definition",
4339 newdecl, newdecl);
4341 /* If we've already generated rtl referencing OLDDECL, we may
4342 have done so in a way that will not function properly with
4343 a weak symbol. */
4344 else if (TREE_USED (olddecl)
4345 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4346 warning (0, "%Jweak declaration of %qD after first use results "
4347 "in unspecified behavior", newdecl, newdecl);
4349 if (SUPPORTS_WEAK)
4351 /* We put the NEWDECL on the weak_decls list at some point.
4352 Replace it with the OLDDECL. */
4353 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4354 if (TREE_VALUE (wd) == newdecl)
4356 TREE_VALUE (wd) = olddecl;
4357 break;
4359 /* We may not find the entry on the list. If NEWDECL is a
4360 weak alias, then we will have already called
4361 globalize_decl to remove the entry; in that case, we do
4362 not need to do anything. */
4365 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4366 mark_weak (olddecl);
4368 else
4369 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4370 weak. Just update NEWDECL to indicate that it's weak too. */
4371 mark_weak (newdecl);
4374 /* Declare DECL to be a weak symbol. */
4376 void
4377 declare_weak (tree decl)
4379 if (! TREE_PUBLIC (decl))
4380 error ("%Jweak declaration of %qD must be public", decl, decl);
4381 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4382 error ("%Jweak declaration of %qD must precede definition", decl, decl);
4383 else if (SUPPORTS_WEAK)
4385 if (! DECL_WEAK (decl))
4386 weak_decls = tree_cons (NULL, decl, weak_decls);
4388 else
4389 warning (0, "%Jweak declaration of %qD not supported", decl, decl);
4391 mark_weak (decl);
4394 /* Emit any pending weak declarations. */
4396 void
4397 weak_finish (void)
4399 tree t;
4401 for (t = weak_decls; t; t = TREE_CHAIN (t))
4403 tree decl = TREE_VALUE (t);
4404 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4405 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4406 #endif
4408 if (! TREE_USED (decl))
4409 continue;
4411 #ifdef ASM_WEAKEN_DECL
4412 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4413 #else
4414 #ifdef ASM_WEAKEN_LABEL
4415 ASM_WEAKEN_LABEL (asm_out_file, name);
4416 #else
4417 #ifdef ASM_OUTPUT_WEAK_ALIAS
4418 warning (0, "only weak aliases are supported in this configuration");
4419 return;
4420 #endif
4421 #endif
4422 #endif
4426 /* Emit the assembly bits to indicate that DECL is globally visible. */
4428 static void
4429 globalize_decl (tree decl)
4431 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4433 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4434 if (DECL_WEAK (decl))
4436 tree *p, t;
4438 #ifdef ASM_WEAKEN_DECL
4439 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4440 #else
4441 ASM_WEAKEN_LABEL (asm_out_file, name);
4442 #endif
4444 /* Remove this function from the pending weak list so that
4445 we do not emit multiple .weak directives for it. */
4446 for (p = &weak_decls; (t = *p) ; )
4448 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4449 *p = TREE_CHAIN (t);
4450 else
4451 p = &TREE_CHAIN (t);
4453 return;
4455 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4456 if (DECL_ONE_ONLY (decl))
4457 ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4458 #endif
4460 targetm.asm_out.globalize_label (asm_out_file, name);
4463 /* We have to be able to tell cgraph about the needed-ness of the target
4464 of an alias. This requires that the decl have been defined. Aliases
4465 that precede their definition have to be queued for later processing. */
4467 typedef struct alias_pair GTY(())
4469 tree decl;
4470 tree target;
4471 } alias_pair;
4473 /* Define gc'd vector type. */
4474 DEF_VEC_O(alias_pair);
4475 DEF_VEC_ALLOC_O(alias_pair,gc);
4477 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
4479 /* Given an assembly name, find the decl it is associated with. At the
4480 same time, mark it needed for cgraph. */
4482 static tree
4483 find_decl_and_mark_needed (tree decl, tree target)
4485 struct cgraph_node *fnode = NULL;
4486 struct cgraph_varpool_node *vnode = NULL;
4488 /* C++ thunk emitting code produces aliases late in the game.
4489 Avoid confusing cgraph code in that case. */
4490 if (!cgraph_global_info_ready)
4492 if (TREE_CODE (decl) == FUNCTION_DECL)
4494 fnode = cgraph_node_for_asm (target);
4495 if (fnode == NULL)
4496 vnode = cgraph_varpool_node_for_asm (target);
4498 else
4500 vnode = cgraph_varpool_node_for_asm (target);
4501 if (vnode == NULL)
4502 fnode = cgraph_node_for_asm (target);
4506 if (fnode)
4508 cgraph_mark_needed_node (fnode);
4509 return fnode->decl;
4511 else if (vnode)
4513 cgraph_varpool_mark_needed_node (vnode);
4514 return vnode->decl;
4516 else
4517 return NULL_TREE;
4520 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4521 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
4522 tree node is DECL to have the value of the tree node TARGET. */
4524 static void
4525 do_assemble_alias (tree decl, tree target ATTRIBUTE_UNUSED)
4527 if (TREE_ASM_WRITTEN (decl))
4528 return;
4530 TREE_ASM_WRITTEN (decl) = 1;
4531 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4533 #ifdef ASM_OUTPUT_DEF
4534 /* Make name accessible from other files, if appropriate. */
4536 if (TREE_PUBLIC (decl))
4538 globalize_decl (decl);
4539 maybe_assemble_visibility (decl);
4542 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4543 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4544 # else
4545 ASM_OUTPUT_DEF (asm_out_file,
4546 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4547 IDENTIFIER_POINTER (target));
4548 # endif
4549 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4551 const char *name;
4552 tree *p, t;
4554 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4555 # ifdef ASM_WEAKEN_DECL
4556 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4557 # else
4558 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4559 # endif
4560 /* Remove this function from the pending weak list so that
4561 we do not emit multiple .weak directives for it. */
4562 for (p = &weak_decls; (t = *p) ; )
4563 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4564 *p = TREE_CHAIN (t);
4565 else
4566 p = &TREE_CHAIN (t);
4568 #endif
4571 /* First pass of completing pending aliases. Make sure that cgraph knows
4572 which symbols will be required. */
4574 void
4575 finish_aliases_1 (void)
4577 unsigned i;
4578 alias_pair *p;
4580 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4582 tree target_decl;
4584 target_decl = find_decl_and_mark_needed (p->decl, p->target);
4585 if (target_decl == NULL)
4586 error ("%J%qD aliased to undefined symbol %qE",
4587 p->decl, p->decl, p->target);
4588 else if (DECL_EXTERNAL (target_decl))
4589 error ("%J%qD aliased to external symbol %qE",
4590 p->decl, p->decl, p->target);
4594 /* Second pass of completing pending aliases. Emit the actual assembly.
4595 This happens at the end of compilation and thus it is assured that the
4596 target symbol has been emitted. */
4598 void
4599 finish_aliases_2 (void)
4601 unsigned i;
4602 alias_pair *p;
4604 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4605 do_assemble_alias (p->decl, p->target);
4607 VEC_truncate (alias_pair, alias_pairs, 0);
4610 /* Emit an assembler directive to make the symbol for DECL an alias to
4611 the symbol for TARGET. */
4613 void
4614 assemble_alias (tree decl, tree target)
4616 tree target_decl;
4618 #if !defined (ASM_OUTPUT_DEF)
4619 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4620 error ("%Jalias definitions not supported in this configuration", decl);
4621 return;
4622 # else
4623 if (!DECL_WEAK (decl))
4625 error ("%Jonly weak aliases are supported in this configuration", decl);
4626 return;
4628 # endif
4629 #endif
4631 /* We must force creation of DECL_RTL for debug info generation, even though
4632 we don't use it here. */
4633 make_decl_rtl (decl);
4634 TREE_USED (decl) = 1;
4636 /* A quirk of the initial implementation of aliases required that the user
4637 add "extern" to all of them. Which is silly, but now historical. Do
4638 note that the symbol is in fact locally defined. */
4639 DECL_EXTERNAL (decl) = 0;
4641 /* Allow aliases to aliases. */
4642 if (TREE_CODE (decl) == FUNCTION_DECL)
4643 cgraph_node (decl)->alias = true;
4644 else
4645 cgraph_varpool_node (decl)->alias = true;
4647 /* If the target has already been emitted, we don't have to queue the
4648 alias. This saves a tad o memory. */
4649 target_decl = find_decl_and_mark_needed (decl, target);
4650 if (target_decl && TREE_ASM_WRITTEN (target_decl))
4651 do_assemble_alias (decl, target);
4652 else
4654 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
4655 p->decl = decl;
4656 p->target = target;
4660 /* Emit an assembler directive to set symbol for DECL visibility to
4661 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
4663 void
4664 default_assemble_visibility (tree decl, int vis)
4666 static const char * const visibility_types[] = {
4667 NULL, "internal", "hidden", "protected"
4670 const char *name, *type;
4672 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4673 type = visibility_types[vis];
4675 #ifdef HAVE_GAS_HIDDEN
4676 fprintf (asm_out_file, "\t.%s\t", type);
4677 assemble_name (asm_out_file, name);
4678 fprintf (asm_out_file, "\n");
4679 #else
4680 warning (OPT_Wattributes, "visibility attribute not supported "
4681 "in this configuration; ignored");
4682 #endif
4685 /* A helper function to call assemble_visibility when needed for a decl. */
4687 static void
4688 maybe_assemble_visibility (tree decl)
4690 enum symbol_visibility vis = DECL_VISIBILITY (decl);
4692 if (vis != VISIBILITY_DEFAULT)
4693 targetm.asm_out.visibility (decl, vis);
4696 /* Returns 1 if the target configuration supports defining public symbols
4697 so that one of them will be chosen at link time instead of generating a
4698 multiply-defined symbol error, whether through the use of weak symbols or
4699 a target-specific mechanism for having duplicates discarded. */
4702 supports_one_only (void)
4704 if (SUPPORTS_ONE_ONLY)
4705 return 1;
4706 return SUPPORTS_WEAK;
4709 /* Set up DECL as a public symbol that can be defined in multiple
4710 translation units without generating a linker error. */
4712 void
4713 make_decl_one_only (tree decl)
4715 gcc_assert (TREE_CODE (decl) == VAR_DECL
4716 || TREE_CODE (decl) == FUNCTION_DECL);
4718 TREE_PUBLIC (decl) = 1;
4720 if (SUPPORTS_ONE_ONLY)
4722 #ifdef MAKE_DECL_ONE_ONLY
4723 MAKE_DECL_ONE_ONLY (decl);
4724 #endif
4725 DECL_ONE_ONLY (decl) = 1;
4727 else if (TREE_CODE (decl) == VAR_DECL
4728 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4729 DECL_COMMON (decl) = 1;
4730 else
4732 gcc_assert (SUPPORTS_WEAK);
4733 DECL_WEAK (decl) = 1;
4737 void
4738 init_varasm_once (void)
4740 in_named_htab = htab_create_ggc (31, in_named_entry_hash,
4741 in_named_entry_eq, NULL);
4742 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4743 const_desc_eq, NULL);
4745 const_alias_set = new_alias_set ();
4748 enum tls_model
4749 decl_default_tls_model (tree decl)
4751 enum tls_model kind;
4752 bool is_local;
4754 is_local = targetm.binds_local_p (decl);
4755 if (!flag_shlib)
4757 if (is_local)
4758 kind = TLS_MODEL_LOCAL_EXEC;
4759 else
4760 kind = TLS_MODEL_INITIAL_EXEC;
4763 /* Local dynamic is inefficient when we're not combining the
4764 parts of the address. */
4765 else if (optimize && is_local)
4766 kind = TLS_MODEL_LOCAL_DYNAMIC;
4767 else
4768 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4769 if (kind < flag_tls_default)
4770 kind = flag_tls_default;
4772 return kind;
4775 /* Select a set of attributes for section NAME based on the properties
4776 of DECL and whether or not RELOC indicates that DECL's initializer
4777 might contain runtime relocations.
4779 We make the section read-only and executable for a function decl,
4780 read-only for a const data decl, and writable for a non-const data decl. */
4782 unsigned int
4783 default_section_type_flags (tree decl, const char *name, int reloc)
4785 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4788 unsigned int
4789 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4790 int shlib)
4792 unsigned int flags;
4794 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4795 flags = SECTION_CODE;
4796 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4797 flags = 0;
4798 else if (current_function_decl
4799 && cfun
4800 && cfun->unlikely_text_section_name
4801 && strcmp (name, cfun->unlikely_text_section_name) == 0)
4802 flags = SECTION_CODE;
4803 else if (!decl
4804 && (!current_function_decl || !cfun)
4805 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
4806 flags = SECTION_CODE;
4807 else
4808 flags = SECTION_WRITE;
4810 if (decl && DECL_ONE_ONLY (decl))
4811 flags |= SECTION_LINKONCE;
4813 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
4814 flags |= SECTION_TLS | SECTION_WRITE;
4816 if (strcmp (name, ".bss") == 0
4817 || strncmp (name, ".bss.", 5) == 0
4818 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4819 || strcmp (name, ".sbss") == 0
4820 || strncmp (name, ".sbss.", 6) == 0
4821 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
4822 flags |= SECTION_BSS;
4824 if (strcmp (name, ".tdata") == 0
4825 || strncmp (name, ".tdata.", 7) == 0
4826 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
4827 flags |= SECTION_TLS;
4829 if (strcmp (name, ".tbss") == 0
4830 || strncmp (name, ".tbss.", 6) == 0
4831 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4832 flags |= SECTION_TLS | SECTION_BSS;
4834 /* These three sections have special ELF types. They are neither
4835 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4836 want to print a section type (@progbits or @nobits). If someone
4837 is silly enough to emit code or TLS variables to one of these
4838 sections, then don't handle them specially. */
4839 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4840 && (strcmp (name, ".init_array") == 0
4841 || strcmp (name, ".fini_array") == 0
4842 || strcmp (name, ".preinit_array") == 0))
4843 flags |= SECTION_NOTYPE;
4845 return flags;
4848 /* Output assembly to switch to section NAME with attribute FLAGS.
4849 Four variants for common object file formats. */
4851 void
4852 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4853 unsigned int flags ATTRIBUTE_UNUSED,
4854 tree decl ATTRIBUTE_UNUSED)
4856 /* Some object formats don't support named sections at all. The
4857 front-end should already have flagged this as an error. */
4858 gcc_unreachable ();
4861 void
4862 default_elf_asm_named_section (const char *name, unsigned int flags,
4863 tree decl ATTRIBUTE_UNUSED)
4865 char flagchars[10], *f = flagchars;
4867 /* If we have already declared this section, we can use an
4868 abbreviated form to switch back to it -- unless this section is
4869 part of a COMDAT groups, in which case GAS requires the full
4870 declaration every time. */
4871 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4872 && ! named_section_first_declaration (name))
4874 fprintf (asm_out_file, "\t.section\t%s\n", name);
4875 return;
4878 if (!(flags & SECTION_DEBUG))
4879 *f++ = 'a';
4880 if (flags & SECTION_WRITE)
4881 *f++ = 'w';
4882 if (flags & SECTION_CODE)
4883 *f++ = 'x';
4884 if (flags & SECTION_SMALL)
4885 *f++ = 's';
4886 if (flags & SECTION_MERGE)
4887 *f++ = 'M';
4888 if (flags & SECTION_STRINGS)
4889 *f++ = 'S';
4890 if (flags & SECTION_TLS)
4891 *f++ = 'T';
4892 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4893 *f++ = 'G';
4894 *f = '\0';
4896 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
4898 if (!(flags & SECTION_NOTYPE))
4900 const char *type;
4901 const char *format;
4903 if (flags & SECTION_BSS)
4904 type = "nobits";
4905 else
4906 type = "progbits";
4908 format = ",@%s";
4909 #ifdef ASM_COMMENT_START
4910 /* On platforms that use "@" as the assembly comment character,
4911 use "%" instead. */
4912 if (strcmp (ASM_COMMENT_START, "@") == 0)
4913 format = ",%%%s";
4914 #endif
4915 fprintf (asm_out_file, format, type);
4917 if (flags & SECTION_ENTSIZE)
4918 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
4919 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4920 fprintf (asm_out_file, ",%s,comdat",
4921 lang_hooks.decls.comdat_group (decl));
4924 putc ('\n', asm_out_file);
4927 void
4928 default_coff_asm_named_section (const char *name, unsigned int flags,
4929 tree decl ATTRIBUTE_UNUSED)
4931 char flagchars[8], *f = flagchars;
4933 if (flags & SECTION_WRITE)
4934 *f++ = 'w';
4935 if (flags & SECTION_CODE)
4936 *f++ = 'x';
4937 *f = '\0';
4939 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
4942 void
4943 default_pe_asm_named_section (const char *name, unsigned int flags,
4944 tree decl)
4946 default_coff_asm_named_section (name, flags, decl);
4948 if (flags & SECTION_LINKONCE)
4950 /* Functions may have been compiled at various levels of
4951 optimization so we can't use `same_size' here.
4952 Instead, have the linker pick one. */
4953 fprintf (asm_out_file, "\t.linkonce %s\n",
4954 (flags & SECTION_CODE ? "discard" : "same_size"));
4958 /* The lame default section selector. */
4960 void
4961 default_select_section (tree decl, int reloc,
4962 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
4964 bool readonly = false;
4966 if (DECL_P (decl))
4968 if (decl_readonly_section (decl, reloc))
4969 readonly = true;
4971 else if (TREE_CODE (decl) == CONSTRUCTOR)
4973 if (! ((flag_pic && reloc)
4974 || !TREE_READONLY (decl)
4975 || TREE_SIDE_EFFECTS (decl)
4976 || !TREE_CONSTANT (decl)))
4977 readonly = true;
4979 else if (TREE_CODE (decl) == STRING_CST)
4980 readonly = true;
4981 else if (! (flag_pic && reloc))
4982 readonly = true;
4984 if (readonly)
4985 readonly_data_section ();
4986 else
4987 data_section ();
4990 /* A helper function for default_elf_select_section and
4991 default_elf_unique_section. Categorizes the DECL. */
4993 enum section_category
4995 SECCAT_TEXT,
4997 SECCAT_RODATA,
4998 SECCAT_RODATA_MERGE_STR,
4999 SECCAT_RODATA_MERGE_STR_INIT,
5000 SECCAT_RODATA_MERGE_CONST,
5001 SECCAT_SRODATA,
5003 SECCAT_DATA,
5005 /* To optimize loading of shared programs, define following subsections
5006 of data section:
5007 _REL Contains data that has relocations, so they get grouped
5008 together and dynamic linker will visit fewer pages in memory.
5009 _RO Contains data that is otherwise read-only. This is useful
5010 with prelinking as most relocations won't be dynamically
5011 linked and thus stay read only.
5012 _LOCAL Marks data containing relocations only to local objects.
5013 These relocations will get fully resolved by prelinking. */
5014 SECCAT_DATA_REL,
5015 SECCAT_DATA_REL_LOCAL,
5016 SECCAT_DATA_REL_RO,
5017 SECCAT_DATA_REL_RO_LOCAL,
5019 SECCAT_SDATA,
5020 SECCAT_TDATA,
5022 SECCAT_BSS,
5023 SECCAT_SBSS,
5024 SECCAT_TBSS
5027 static enum section_category
5028 categorize_decl_for_section (tree, int, int);
5030 static enum section_category
5031 categorize_decl_for_section (tree decl, int reloc, int shlib)
5033 enum section_category ret;
5035 if (TREE_CODE (decl) == FUNCTION_DECL)
5036 return SECCAT_TEXT;
5037 else if (TREE_CODE (decl) == STRING_CST)
5039 if (flag_mudflap) /* or !flag_merge_constants */
5040 return SECCAT_RODATA;
5041 else
5042 return SECCAT_RODATA_MERGE_STR;
5044 else if (TREE_CODE (decl) == VAR_DECL)
5046 if (DECL_INITIAL (decl) == NULL
5047 || DECL_INITIAL (decl) == error_mark_node
5048 || (flag_zero_initialized_in_bss
5049 /* Leave constant zeroes in .rodata so they can be shared. */
5050 && !TREE_READONLY (decl)
5051 && initializer_zerop (DECL_INITIAL (decl))))
5052 ret = SECCAT_BSS;
5053 else if (! TREE_READONLY (decl)
5054 || TREE_SIDE_EFFECTS (decl)
5055 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5057 if (shlib && (reloc & 2))
5058 ret = SECCAT_DATA_REL;
5059 else if (shlib && reloc)
5060 ret = SECCAT_DATA_REL_LOCAL;
5061 else
5062 ret = SECCAT_DATA;
5064 else if (shlib && (reloc & 2))
5065 ret = SECCAT_DATA_REL_RO;
5066 else if (shlib && reloc)
5067 ret = SECCAT_DATA_REL_RO_LOCAL;
5068 else if (reloc || flag_merge_constants < 2)
5069 /* C and C++ don't allow different variables to share the same
5070 location. -fmerge-all-constants allows even that (at the
5071 expense of not conforming). */
5072 ret = SECCAT_RODATA;
5073 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5074 ret = SECCAT_RODATA_MERGE_STR_INIT;
5075 else
5076 ret = SECCAT_RODATA_MERGE_CONST;
5078 else if (TREE_CODE (decl) == CONSTRUCTOR)
5080 if ((shlib && reloc)
5081 || TREE_SIDE_EFFECTS (decl)
5082 || ! TREE_CONSTANT (decl))
5083 ret = SECCAT_DATA;
5084 else
5085 ret = SECCAT_RODATA;
5087 else
5088 ret = SECCAT_RODATA;
5090 /* There are no read-only thread-local sections. */
5091 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5093 /* Note that this would be *just* SECCAT_BSS, except that there's
5094 no concept of a read-only thread-local-data section. */
5095 if (ret == SECCAT_BSS
5096 || (flag_zero_initialized_in_bss
5097 && initializer_zerop (DECL_INITIAL (decl))))
5098 ret = SECCAT_TBSS;
5099 else
5100 ret = SECCAT_TDATA;
5103 /* If the target uses small data sections, select it. */
5104 else if (targetm.in_small_data_p (decl))
5106 if (ret == SECCAT_BSS)
5107 ret = SECCAT_SBSS;
5108 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5109 ret = SECCAT_SRODATA;
5110 else
5111 ret = SECCAT_SDATA;
5114 return ret;
5117 bool
5118 decl_readonly_section (tree decl, int reloc)
5120 return decl_readonly_section_1 (decl, reloc, flag_pic);
5123 bool
5124 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5126 switch (categorize_decl_for_section (decl, reloc, shlib))
5128 case SECCAT_RODATA:
5129 case SECCAT_RODATA_MERGE_STR:
5130 case SECCAT_RODATA_MERGE_STR_INIT:
5131 case SECCAT_RODATA_MERGE_CONST:
5132 case SECCAT_SRODATA:
5133 return true;
5134 break;
5135 default:
5136 return false;
5137 break;
5141 /* Select a section based on the above categorization. */
5143 void
5144 default_elf_select_section (tree decl, int reloc,
5145 unsigned HOST_WIDE_INT align)
5147 default_elf_select_section_1 (decl, reloc, align, flag_pic);
5150 void
5151 default_elf_select_section_1 (tree decl, int reloc,
5152 unsigned HOST_WIDE_INT align, int shlib)
5154 const char *sname;
5155 switch (categorize_decl_for_section (decl, reloc, shlib))
5157 case SECCAT_TEXT:
5158 /* We're not supposed to be called on FUNCTION_DECLs. */
5159 gcc_unreachable ();
5160 case SECCAT_RODATA:
5161 readonly_data_section ();
5162 return;
5163 case SECCAT_RODATA_MERGE_STR:
5164 mergeable_string_section (decl, align, 0);
5165 return;
5166 case SECCAT_RODATA_MERGE_STR_INIT:
5167 mergeable_string_section (DECL_INITIAL (decl), align, 0);
5168 return;
5169 case SECCAT_RODATA_MERGE_CONST:
5170 mergeable_constant_section (DECL_MODE (decl), align, 0);
5171 return;
5172 case SECCAT_SRODATA:
5173 sname = ".sdata2";
5174 break;
5175 case SECCAT_DATA:
5176 data_section ();
5177 return;
5178 case SECCAT_DATA_REL:
5179 sname = ".data.rel";
5180 break;
5181 case SECCAT_DATA_REL_LOCAL:
5182 sname = ".data.rel.local";
5183 break;
5184 case SECCAT_DATA_REL_RO:
5185 sname = ".data.rel.ro";
5186 break;
5187 case SECCAT_DATA_REL_RO_LOCAL:
5188 sname = ".data.rel.ro.local";
5189 break;
5190 case SECCAT_SDATA:
5191 sname = ".sdata";
5192 break;
5193 case SECCAT_TDATA:
5194 sname = ".tdata";
5195 break;
5196 case SECCAT_BSS:
5197 #ifdef BSS_SECTION_ASM_OP
5198 bss_section ();
5199 return;
5200 #else
5201 sname = ".bss";
5202 break;
5203 #endif
5204 case SECCAT_SBSS:
5205 sname = ".sbss";
5206 break;
5207 case SECCAT_TBSS:
5208 sname = ".tbss";
5209 break;
5210 default:
5211 gcc_unreachable ();
5214 if (!DECL_P (decl))
5215 decl = NULL_TREE;
5216 named_section (decl, sname, reloc);
5219 /* Construct a unique section name based on the decl name and the
5220 categorization performed above. */
5222 void
5223 default_unique_section (tree decl, int reloc)
5225 default_unique_section_1 (decl, reloc, flag_pic);
5228 void
5229 default_unique_section_1 (tree decl, int reloc, int shlib)
5231 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
5232 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5233 const char *prefix, *name;
5234 size_t nlen, plen;
5235 char *string;
5237 switch (categorize_decl_for_section (decl, reloc, shlib))
5239 case SECCAT_TEXT:
5240 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5241 break;
5242 case SECCAT_RODATA:
5243 case SECCAT_RODATA_MERGE_STR:
5244 case SECCAT_RODATA_MERGE_STR_INIT:
5245 case SECCAT_RODATA_MERGE_CONST:
5246 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5247 break;
5248 case SECCAT_SRODATA:
5249 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5250 break;
5251 case SECCAT_DATA:
5252 case SECCAT_DATA_REL:
5253 case SECCAT_DATA_REL_LOCAL:
5254 case SECCAT_DATA_REL_RO:
5255 case SECCAT_DATA_REL_RO_LOCAL:
5256 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5257 break;
5258 case SECCAT_SDATA:
5259 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5260 break;
5261 case SECCAT_BSS:
5262 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5263 break;
5264 case SECCAT_SBSS:
5265 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5266 break;
5267 case SECCAT_TDATA:
5268 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5269 break;
5270 case SECCAT_TBSS:
5271 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5272 break;
5273 default:
5274 gcc_unreachable ();
5276 plen = strlen (prefix);
5278 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5279 name = targetm.strip_name_encoding (name);
5280 nlen = strlen (name);
5282 string = alloca (nlen + plen + 1);
5283 memcpy (string, prefix, plen);
5284 memcpy (string + plen, name, nlen + 1);
5286 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5289 void
5290 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5291 rtx x,
5292 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5294 if (flag_pic)
5295 switch (GET_CODE (x))
5297 case CONST:
5298 case SYMBOL_REF:
5299 case LABEL_REF:
5300 data_section ();
5301 return;
5303 default:
5304 break;
5307 readonly_data_section ();
5310 void
5311 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5312 unsigned HOST_WIDE_INT align)
5314 /* ??? Handle small data here somehow. */
5316 if (flag_pic)
5317 switch (GET_CODE (x))
5319 case CONST:
5320 case SYMBOL_REF:
5321 named_section (NULL_TREE, ".data.rel.ro", 3);
5322 return;
5324 case LABEL_REF:
5325 named_section (NULL_TREE, ".data.rel.ro.local", 1);
5326 return;
5328 default:
5329 break;
5332 mergeable_constant_section (mode, align, 0);
5335 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
5337 void
5338 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5340 rtx symbol;
5341 int flags;
5343 /* Careful not to prod global register variables. */
5344 if (!MEM_P (rtl))
5345 return;
5346 symbol = XEXP (rtl, 0);
5347 if (GET_CODE (symbol) != SYMBOL_REF)
5348 return;
5350 flags = 0;
5351 if (TREE_CODE (decl) == FUNCTION_DECL)
5352 flags |= SYMBOL_FLAG_FUNCTION;
5353 if (targetm.binds_local_p (decl))
5354 flags |= SYMBOL_FLAG_LOCAL;
5355 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5356 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5357 else if (targetm.in_small_data_p (decl))
5358 flags |= SYMBOL_FLAG_SMALL;
5359 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
5360 being PUBLIC, the thing *must* be defined in this translation unit.
5361 Prevent this buglet from being propagated into rtl code as well. */
5362 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5363 flags |= SYMBOL_FLAG_EXTERNAL;
5365 SYMBOL_REF_FLAGS (symbol) = flags;
5368 /* By default, we do nothing for encode_section_info, so we need not
5369 do anything but discard the '*' marker. */
5371 const char *
5372 default_strip_name_encoding (const char *str)
5374 return str + (*str == '*');
5377 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5378 wrt cross-module name binding. */
5380 bool
5381 default_binds_local_p (tree exp)
5383 return default_binds_local_p_1 (exp, flag_shlib);
5386 bool
5387 default_binds_local_p_1 (tree exp, int shlib)
5389 bool local_p;
5391 /* A non-decl is an entry in the constant pool. */
5392 if (!DECL_P (exp))
5393 local_p = true;
5394 /* Static variables are always local. */
5395 else if (! TREE_PUBLIC (exp))
5396 local_p = true;
5397 /* A variable is local if the user explicitly tells us so. */
5398 else if (DECL_VISIBILITY_SPECIFIED (exp) && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5399 local_p = true;
5400 /* Otherwise, variables defined outside this object may not be local. */
5401 else if (DECL_EXTERNAL (exp))
5402 local_p = false;
5403 /* Linkonce and weak data are never local. */
5404 else if (DECL_ONE_ONLY (exp) || DECL_WEAK (exp))
5405 local_p = false;
5406 /* If none of the above and visibility is not default, make local. */
5407 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5408 local_p = true;
5409 /* If PIC, then assume that any global name can be overridden by
5410 symbols resolved from other modules. */
5411 else if (shlib)
5412 local_p = false;
5413 /* Uninitialized COMMON variable may be unified with symbols
5414 resolved from other modules. */
5415 else if (DECL_COMMON (exp)
5416 && (DECL_INITIAL (exp) == NULL
5417 || DECL_INITIAL (exp) == error_mark_node))
5418 local_p = false;
5419 /* Otherwise we're left with initialized (or non-common) global data
5420 which is of necessity defined locally. */
5421 else
5422 local_p = true;
5424 return local_p;
5427 /* Determine whether or not a pointer mode is valid. Assume defaults
5428 of ptr_mode or Pmode - can be overridden. */
5429 bool
5430 default_valid_pointer_mode (enum machine_mode mode)
5432 return (mode == ptr_mode || mode == Pmode);
5435 /* Default function to output code that will globalize a label. A
5436 target must define GLOBAL_ASM_OP or provide its own function to
5437 globalize a label. */
5438 #ifdef GLOBAL_ASM_OP
5439 void
5440 default_globalize_label (FILE * stream, const char *name)
5442 fputs (GLOBAL_ASM_OP, stream);
5443 assemble_name (stream, name);
5444 putc ('\n', stream);
5446 #endif /* GLOBAL_ASM_OP */
5448 /* Default function to output a label for unwind information. The
5449 default is to do nothing. A target that needs nonlocal labels for
5450 unwind information must provide its own function to do this. */
5451 void
5452 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5453 tree decl ATTRIBUTE_UNUSED,
5454 int for_eh ATTRIBUTE_UNUSED,
5455 int empty ATTRIBUTE_UNUSED)
5459 /* This is how to output an internal numbered label where PREFIX is
5460 the class of label and LABELNO is the number within the class. */
5462 void
5463 default_internal_label (FILE *stream, const char *prefix,
5464 unsigned long labelno)
5466 char *const buf = alloca (40 + strlen (prefix));
5467 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5468 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5471 /* This is the default behavior at the beginning of a file. It's
5472 controlled by two other target-hook toggles. */
5473 void
5474 default_file_start (void)
5476 if (targetm.file_start_app_off && !flag_verbose_asm)
5477 fputs (ASM_APP_OFF, asm_out_file);
5479 if (targetm.file_start_file_directive)
5480 output_file_directive (asm_out_file, main_input_filename);
5483 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5484 which emits a special section directive used to indicate whether or
5485 not this object file needs an executable stack. This is primarily
5486 a GNU extension to ELF but could be used on other targets. */
5488 int trampolines_created;
5490 void
5491 file_end_indicate_exec_stack (void)
5493 unsigned int flags = SECTION_DEBUG;
5494 if (trampolines_created)
5495 flags |= SECTION_CODE;
5497 named_section_flags (".note.GNU-stack", flags);
5500 #include "gt-varasm.h"