Fix entry.
[official-gcc.git] / gcc / varasm.c
blob2f16f7e6057c66f651618c5a5521b86a968edced
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, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, 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"
56 #ifdef XCOFF_DEBUGGING_INFO
57 #include "xcoffout.h" /* Needed for external data
58 declarations for e.g. AIX 4.x. */
59 #endif
61 /* The (assembler) name of the first globally-visible object output. */
62 const char *first_global_object_name;
63 const char *weak_global_object_name;
65 struct addr_const;
66 struct constant_descriptor_rtx;
67 struct rtx_constant_pool;
69 struct varasm_status GTY(())
71 /* If we're using a per-function constant pool, this is it. */
72 struct rtx_constant_pool *pool;
74 /* Number of tree-constants deferred during the expansion of this
75 function. */
76 unsigned int deferred_constants;
79 #define n_deferred_constants (cfun->varasm->deferred_constants)
81 /* Number for making the label on the next
82 constant that is stored in memory. */
84 static GTY(()) int const_labelno;
86 /* Carry information from ASM_DECLARE_OBJECT_NAME
87 to ASM_FINISH_DECLARE_OBJECT. */
89 int size_directive_output;
91 /* The last decl for which assemble_variable was called,
92 if it did ASM_DECLARE_OBJECT_NAME.
93 If the last call to assemble_variable didn't do that,
94 this holds 0. */
96 tree last_assemble_variable_decl;
98 /* The following global variable indicates if the section label for the
99 "cold" section of code has been output yet to the assembler. The
100 label is useful when running gdb. This is part of the optimization that
101 partitions hot and cold basic blocks into separate sections of the .o
102 file. */
104 static bool unlikely_section_label_printed = false;
106 /* The following global variable indicates the label name to be put at
107 the start of the first cold section within each function, when
108 partitioning basic blocks into hot and cold sections. */
110 static char *unlikely_section_label = NULL;
112 /* The following global variable indicates the section name to be used
113 for the current cold section, when partitioning hot and cold basic
114 blocks into separate sections. */
116 static char *unlikely_text_section_name = NULL;
118 /* We give all constants their own alias set. Perhaps redundant with
119 MEM_READONLY_P, but pre-dates it. */
121 static HOST_WIDE_INT const_alias_set;
123 static const char *strip_reg_name (const char *);
124 static int contains_pointers_p (tree);
125 #ifdef ASM_OUTPUT_EXTERNAL
126 static bool incorporeal_function_p (tree);
127 #endif
128 static void decode_addr_const (tree, struct addr_const *);
129 static hashval_t const_desc_hash (const void *);
130 static int const_desc_eq (const void *, const void *);
131 static hashval_t const_hash_1 (const tree);
132 static int compare_constant (const tree, const tree);
133 static tree copy_constant (tree);
134 static void output_constant_def_contents (rtx);
135 static void output_addressed_constants (tree);
136 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
137 static unsigned min_align (unsigned, unsigned);
138 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
139 static void globalize_decl (tree);
140 static void maybe_assemble_visibility (tree);
141 static int in_named_entry_eq (const void *, const void *);
142 static hashval_t in_named_entry_hash (const void *);
143 #ifdef BSS_SECTION_ASM_OP
144 #ifdef ASM_OUTPUT_BSS
145 static void asm_output_bss (FILE *, tree, const char *,
146 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
147 #endif
148 #ifdef ASM_OUTPUT_ALIGNED_BSS
149 static void asm_output_aligned_bss (FILE *, tree, const char *,
150 unsigned HOST_WIDE_INT, int)
151 ATTRIBUTE_UNUSED;
152 #endif
153 #endif /* BSS_SECTION_ASM_OP */
154 static bool asm_emit_uninitialised (tree, const char*,
155 unsigned HOST_WIDE_INT,
156 unsigned HOST_WIDE_INT);
157 static void mark_weak (tree);
159 enum in_section { no_section, in_text, in_unlikely_executed_text, in_data,
160 in_named
161 #ifdef BSS_SECTION_ASM_OP
162 , in_bss
163 #endif
164 #ifdef CTORS_SECTION_ASM_OP
165 , in_ctors
166 #endif
167 #ifdef DTORS_SECTION_ASM_OP
168 , in_dtors
169 #endif
170 #ifdef READONLY_DATA_SECTION_ASM_OP
171 , in_readonly_data
172 #endif
173 #ifdef EXTRA_SECTIONS
174 , EXTRA_SECTIONS
175 #endif
177 static GTY(()) enum in_section in_section = no_section;
179 /* Return a nonzero value if DECL has a section attribute. */
180 #ifndef IN_NAMED_SECTION
181 #define IN_NAMED_SECTION(DECL) \
182 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
183 && DECL_SECTION_NAME (DECL) != NULL_TREE)
184 #endif
186 /* Text of section name when in_section == in_named. */
187 static GTY(()) const char *in_named_name;
189 /* Hash table of flags that have been used for a particular named section. */
191 struct in_named_entry GTY(())
193 const char *name;
194 unsigned int flags;
195 bool declared;
198 static GTY((param_is (struct in_named_entry))) htab_t in_named_htab;
200 /* Define functions like text_section for any extra sections. */
201 #ifdef EXTRA_SECTION_FUNCTIONS
202 EXTRA_SECTION_FUNCTIONS
203 #endif
205 /* Tell assembler to switch to text section. */
207 void
208 text_section (void)
210 if (in_section != in_text)
212 in_section = in_text;
213 fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
217 /* Tell assembler to switch to unlikely-to-be-executed text section. */
219 void
220 unlikely_text_section (void)
222 const char *name;
223 int len;
225 if (! unlikely_text_section_name)
227 if (DECL_SECTION_NAME (current_function_decl)
228 && (strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME
229 (current_function_decl)),
230 HOT_TEXT_SECTION_NAME) != 0)
231 && (strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME
232 (current_function_decl)),
233 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) != 0))
235 name = TREE_STRING_POINTER (DECL_SECTION_NAME
236 (current_function_decl));
237 len = strlen (name);
238 unlikely_text_section_name = xmalloc ((len + 10) * sizeof (char));
239 strcpy (unlikely_text_section_name, name);
240 strcat (unlikely_text_section_name, "_unlikely");
242 else
244 len = strlen (UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
245 unlikely_text_section_name = xmalloc (len+1 * sizeof (char));
246 strcpy (unlikely_text_section_name,
247 UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
251 if ((in_section != in_unlikely_executed_text)
252 && (in_section != in_named
253 || strcmp (in_named_name, unlikely_text_section_name) != 0))
255 named_section (NULL_TREE, unlikely_text_section_name, 0);
256 in_section = in_unlikely_executed_text;
258 if (!unlikely_section_label_printed)
260 ASM_OUTPUT_LABEL (asm_out_file, unlikely_section_label);
261 unlikely_section_label_printed = true;
266 /* Tell assembler to switch to data section. */
268 void
269 data_section (void)
271 if (in_section != in_data)
273 in_section = in_data;
274 fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
278 /* Tell assembler to switch to read-only data section. This is normally
279 the text section. */
281 void
282 readonly_data_section (void)
284 #ifdef READONLY_DATA_SECTION
285 READONLY_DATA_SECTION (); /* Note this can call data_section. */
286 #else
287 #ifdef READONLY_DATA_SECTION_ASM_OP
288 if (in_section != in_readonly_data)
290 in_section = in_readonly_data;
291 fputs (READONLY_DATA_SECTION_ASM_OP, asm_out_file);
292 fputc ('\n', asm_out_file);
294 #else
295 text_section ();
296 #endif
297 #endif
300 /* Determine if we're in the text section. */
303 in_text_section (void)
305 return in_section == in_text;
308 /* Determine if we're in the unlikely-to-be-executed text section. */
311 in_unlikely_text_section (void)
313 bool ret_val;
315 ret_val = ((in_section == in_unlikely_executed_text)
316 || (in_section == in_named
317 && unlikely_text_section_name
318 && strcmp (in_named_name, unlikely_text_section_name) == 0));
320 return ret_val;
323 /* Determine if we're in the data section. */
326 in_data_section (void)
328 return in_section == in_data;
331 /* Helper routines for maintaining in_named_htab. */
333 static int
334 in_named_entry_eq (const void *p1, const void *p2)
336 const struct in_named_entry *old = p1;
337 const char *new = p2;
339 return strcmp (old->name, new) == 0;
342 static hashval_t
343 in_named_entry_hash (const void *p)
345 const struct in_named_entry *old = p;
346 return htab_hash_string (old->name);
349 /* If SECTION has been seen before as a named section, return the flags
350 that were used. Otherwise, return 0. Note, that 0 is a perfectly valid
351 set of flags for a section to have, so 0 does not mean that the section
352 has not been seen. */
354 static unsigned int
355 get_named_section_flags (const char *section)
357 struct in_named_entry **slot;
359 slot = (struct in_named_entry **)
360 htab_find_slot_with_hash (in_named_htab, section,
361 htab_hash_string (section), NO_INSERT);
363 return slot ? (*slot)->flags : 0;
366 /* Returns true if the section has been declared before. Sets internal
367 flag on this section in in_named_hash so subsequent calls on this
368 section will return false. */
370 bool
371 named_section_first_declaration (const char *name)
373 struct in_named_entry **slot;
375 slot = (struct in_named_entry **)
376 htab_find_slot_with_hash (in_named_htab, name,
377 htab_hash_string (name), NO_INSERT);
378 if (! (*slot)->declared)
380 (*slot)->declared = true;
381 return true;
383 else
385 return false;
390 /* Record FLAGS for SECTION. If SECTION was previously recorded with a
391 different set of flags, return false. */
393 bool
394 set_named_section_flags (const char *section, unsigned int flags)
396 struct in_named_entry **slot, *entry;
398 slot = (struct in_named_entry **)
399 htab_find_slot_with_hash (in_named_htab, section,
400 htab_hash_string (section), INSERT);
401 entry = *slot;
403 if (!entry)
405 entry = ggc_alloc (sizeof (*entry));
406 *slot = entry;
407 entry->name = ggc_strdup (section);
408 entry->flags = flags;
409 entry->declared = false;
411 else if (entry->flags != flags)
412 return false;
414 return true;
417 /* Tell assembler to change to section NAME with attributes FLAGS. If
418 DECL is non-NULL, it is the VAR_DECL or FUNCTION_DECL with which
419 this section is associated. */
421 void
422 named_section_real (const char *name, unsigned int flags, tree decl)
424 if (in_section != in_named || strcmp (name, in_named_name) != 0)
426 bool unchanged = set_named_section_flags (name, flags);
428 gcc_assert (unchanged);
430 targetm.asm_out.named_section (name, flags, decl);
432 if (flags & SECTION_FORGET)
433 in_section = no_section;
434 else
436 in_named_name = ggc_strdup (name);
437 in_section = in_named;
442 /* Tell assembler to change to section NAME for DECL.
443 If DECL is NULL, just switch to section NAME.
444 If NAME is NULL, get the name from DECL.
445 If RELOC is 1, the initializer for DECL contains relocs. */
447 void
448 named_section (tree decl, const char *name, int reloc)
450 unsigned int flags;
452 gcc_assert (!decl || DECL_P (decl));
453 if (name == NULL)
454 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
456 if (strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0
457 && !unlikely_text_section_name)
458 unlikely_text_section_name =
459 xstrdup (UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
461 flags = targetm.section_type_flags (decl, name, reloc);
463 /* Sanity check user variables for flag changes. Non-user
464 section flag changes will abort in named_section_flags.
465 However, don't complain if SECTION_OVERRIDE is set.
466 We trust that the setter knows that it is safe to ignore
467 the default flags for this decl. */
468 if (decl && ! set_named_section_flags (name, flags))
470 flags = get_named_section_flags (name);
471 if ((flags & SECTION_OVERRIDE) == 0)
472 error ("%J%D causes a section type conflict", decl, decl);
475 named_section_real (name, flags, decl);
478 /* If required, set DECL_SECTION_NAME to a unique name. */
480 void
481 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
482 int flag_function_or_data_sections)
484 if (DECL_SECTION_NAME (decl) == NULL_TREE
485 && targetm.have_named_sections
486 && (flag_function_or_data_sections
487 || DECL_ONE_ONLY (decl)))
488 targetm.asm_out.unique_section (decl, reloc);
491 #ifdef BSS_SECTION_ASM_OP
493 /* Tell the assembler to switch to the bss section. */
495 void
496 bss_section (void)
498 if (in_section != in_bss)
500 fprintf (asm_out_file, "%s\n", BSS_SECTION_ASM_OP);
501 in_section = in_bss;
505 #ifdef ASM_OUTPUT_BSS
507 /* Utility function for ASM_OUTPUT_BSS for targets to use if
508 they don't support alignments in .bss.
509 ??? It is believed that this function will work in most cases so such
510 support is localized here. */
512 static void
513 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
514 const char *name,
515 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
516 unsigned HOST_WIDE_INT rounded)
518 targetm.asm_out.globalize_label (file, name);
519 bss_section ();
520 #ifdef ASM_DECLARE_OBJECT_NAME
521 last_assemble_variable_decl = decl;
522 ASM_DECLARE_OBJECT_NAME (file, name, decl);
523 #else
524 /* Standard thing is just output label for the object. */
525 ASM_OUTPUT_LABEL (file, name);
526 #endif /* ASM_DECLARE_OBJECT_NAME */
527 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
530 #endif
532 #ifdef ASM_OUTPUT_ALIGNED_BSS
534 /* Utility function for targets to use in implementing
535 ASM_OUTPUT_ALIGNED_BSS.
536 ??? It is believed that this function will work in most cases so such
537 support is localized here. */
539 static void
540 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
541 const char *name, unsigned HOST_WIDE_INT size,
542 int align)
544 bss_section ();
545 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
546 #ifdef ASM_DECLARE_OBJECT_NAME
547 last_assemble_variable_decl = decl;
548 ASM_DECLARE_OBJECT_NAME (file, name, decl);
549 #else
550 /* Standard thing is just output label for the object. */
551 ASM_OUTPUT_LABEL (file, name);
552 #endif /* ASM_DECLARE_OBJECT_NAME */
553 ASM_OUTPUT_SKIP (file, size ? size : 1);
556 #endif
558 #endif /* BSS_SECTION_ASM_OP */
560 /* Switch to the section for function DECL.
562 If DECL is NULL_TREE, switch to the text section. We can be passed
563 NULL_TREE under some circumstances by dbxout.c at least. */
565 void
566 function_section (tree decl)
568 if (decl == NULL_TREE)
569 text_section ();
570 else
572 /* ??? Typical use of this function maybe shouldn't be looking
573 for unlikely blocks at all - in the event that an entire
574 function is going into the unlikely-execute section, that
575 should be reflected in its DECL_SECTION_NAME. */
576 rtx insns = cfun && cfun->emit ? get_insns () : 0;
577 bool unlikely = insns && scan_ahead_for_unlikely_executed_note (insns);
579 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
580 targetm.asm_out.select_section (decl, unlikely, DECL_ALIGN (decl));
581 #else
582 if (unlikely)
583 unlikely_text_section ();
584 else if (DECL_SECTION_NAME (decl))
585 named_section (decl, 0, 0);
586 else
587 text_section ();
588 #endif
592 /* Switch to read-only data section associated with function DECL. */
594 void
595 default_function_rodata_section (tree decl)
597 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
599 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
601 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
602 if (DECL_ONE_ONLY (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
604 size_t len = strlen (name) + 1;
605 char *rname = alloca (len);
607 memcpy (rname, name, len);
608 rname[14] = 'r';
609 named_section_real (rname, SECTION_LINKONCE, decl);
610 return;
612 /* For .text.foo we want to use .rodata.foo. */
613 else if (flag_function_sections && flag_data_sections
614 && strncmp (name, ".text.", 6) == 0)
616 size_t len = strlen (name) + 1;
617 char *rname = alloca (len + 2);
619 memcpy (rname, ".rodata", 7);
620 memcpy (rname + 7, name + 5, len - 5);
621 named_section_flags (rname, 0);
622 return;
626 readonly_data_section ();
629 /* Switch to read-only data section associated with function DECL
630 for targets where that section should be always the single
631 readonly data section. */
633 void
634 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
636 readonly_data_section ();
639 /* Switch to section for variable DECL. RELOC is the same as the
640 argument to SELECT_SECTION. */
642 void
643 variable_section (tree decl, int reloc)
645 if (IN_NAMED_SECTION (decl))
646 named_section (decl, NULL, reloc);
647 else
648 targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
651 /* Tell assembler to switch to the section for string merging. */
653 void
654 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
655 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
656 unsigned int flags ATTRIBUTE_UNUSED)
658 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
659 && TREE_CODE (decl) == STRING_CST
660 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
661 && align <= 256
662 && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
664 enum machine_mode mode;
665 unsigned int modesize;
666 const char *str;
667 int i, j, len, unit;
668 char name[30];
670 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
671 modesize = GET_MODE_BITSIZE (mode);
672 if (modesize >= 8 && modesize <= 256
673 && (modesize & (modesize - 1)) == 0)
675 if (align < modesize)
676 align = modesize;
678 str = TREE_STRING_POINTER (decl);
679 len = TREE_STRING_LENGTH (decl);
680 unit = GET_MODE_SIZE (mode);
682 /* Check for embedded NUL characters. */
683 for (i = 0; i < len; i += unit)
685 for (j = 0; j < unit; j++)
686 if (str[i + j] != '\0')
687 break;
688 if (j == unit)
689 break;
691 if (i == len - unit)
693 sprintf (name, ".rodata.str%d.%d", modesize / 8,
694 (int) (align / 8));
695 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
696 if (!i && modesize < align)
698 /* A "" string with requested alignment greater than
699 character size might cause a problem:
700 if some other string required even bigger
701 alignment than "", then linker might think the
702 "" is just part of padding after some other string
703 and not put it into the hash table initially.
704 But this means "" could have smaller alignment
705 than requested. */
706 #ifdef ASM_OUTPUT_SECTION_START
707 named_section_flags (name, flags);
708 ASM_OUTPUT_SECTION_START (asm_out_file);
709 #else
710 readonly_data_section ();
711 #endif
712 return;
715 named_section_flags (name, flags);
716 return;
721 readonly_data_section ();
724 /* Tell assembler to switch to the section for constant merging. */
726 void
727 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
728 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
729 unsigned int flags ATTRIBUTE_UNUSED)
731 unsigned int modesize = GET_MODE_BITSIZE (mode);
733 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
734 && mode != VOIDmode
735 && mode != BLKmode
736 && modesize <= align
737 && align >= 8
738 && align <= 256
739 && (align & (align - 1)) == 0)
741 char name[24];
743 sprintf (name, ".rodata.cst%d", (int) (align / 8));
744 flags |= (align / 8) | SECTION_MERGE;
745 named_section_flags (name, flags);
746 return;
749 readonly_data_section ();
752 /* Given NAME, a putative register name, discard any customary prefixes. */
754 static const char *
755 strip_reg_name (const char *name)
757 #ifdef REGISTER_PREFIX
758 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
759 name += strlen (REGISTER_PREFIX);
760 #endif
761 if (name[0] == '%' || name[0] == '#')
762 name++;
763 return name;
766 /* The user has asked for a DECL to have a particular name. Set (or
767 change) it in such a way that we don't prefix an underscore to
768 it. */
769 void
770 set_user_assembler_name (tree decl, const char *name)
772 char *starred = alloca (strlen (name) + 2);
773 starred[0] = '*';
774 strcpy (starred + 1, name);
775 change_decl_assembler_name (decl, get_identifier (starred));
776 SET_DECL_RTL (decl, NULL_RTX);
779 /* Decode an `asm' spec for a declaration as a register name.
780 Return the register number, or -1 if nothing specified,
781 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
782 or -3 if ASMSPEC is `cc' and is not recognized,
783 or -4 if ASMSPEC is `memory' and is not recognized.
784 Accept an exact spelling or a decimal number.
785 Prefixes such as % are optional. */
788 decode_reg_name (const char *asmspec)
790 if (asmspec != 0)
792 int i;
794 /* Get rid of confusing prefixes. */
795 asmspec = strip_reg_name (asmspec);
797 /* Allow a decimal number as a "register name". */
798 for (i = strlen (asmspec) - 1; i >= 0; i--)
799 if (! ISDIGIT (asmspec[i]))
800 break;
801 if (asmspec[0] != 0 && i < 0)
803 i = atoi (asmspec);
804 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
805 return i;
806 else
807 return -2;
810 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
811 if (reg_names[i][0]
812 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
813 return i;
815 #ifdef ADDITIONAL_REGISTER_NAMES
817 static const struct { const char *const name; const int number; } table[]
818 = ADDITIONAL_REGISTER_NAMES;
820 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
821 if (! strcmp (asmspec, table[i].name))
822 return table[i].number;
824 #endif /* ADDITIONAL_REGISTER_NAMES */
826 if (!strcmp (asmspec, "memory"))
827 return -4;
829 if (!strcmp (asmspec, "cc"))
830 return -3;
832 return -2;
835 return -1;
838 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
839 have static storage duration. In other words, it should not be an
840 automatic variable, including PARM_DECLs.
842 There is, however, one exception: this function handles variables
843 explicitly placed in a particular register by the user.
845 This is never called for PARM_DECL nodes. */
847 void
848 make_decl_rtl (tree decl)
850 const char *name = 0;
851 int reg_number;
852 rtx x;
854 /* Check that we are not being given an automatic variable. */
855 gcc_assert (TREE_CODE (decl) != PARM_DECL
856 && TREE_CODE (decl) != RESULT_DECL);
858 /* A weak alias has TREE_PUBLIC set but not the other bits. */
859 gcc_assert (TREE_CODE (decl) != VAR_DECL
860 || TREE_STATIC (decl)
861 || TREE_PUBLIC (decl)
862 || DECL_EXTERNAL (decl)
863 || DECL_REGISTER (decl));
865 /* And that we were not given a type or a label. */
866 gcc_assert (TREE_CODE (decl) != TYPE_DECL
867 && TREE_CODE (decl) != LABEL_DECL);
869 /* For a duplicate declaration, we can be called twice on the
870 same DECL node. Don't discard the RTL already made. */
871 if (DECL_RTL_SET_P (decl))
873 /* If the old RTL had the wrong mode, fix the mode. */
874 if (GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
875 SET_DECL_RTL (decl, adjust_address_nv (DECL_RTL (decl),
876 DECL_MODE (decl), 0));
878 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
879 return;
881 /* ??? Another way to do this would be to maintain a hashed
882 table of such critters. Instead of adding stuff to a DECL
883 to give certain attributes to it, we could use an external
884 hash map from DECL to set of attributes. */
886 /* Let the target reassign the RTL if it wants.
887 This is necessary, for example, when one machine specific
888 decl attribute overrides another. */
889 targetm.encode_section_info (decl, DECL_RTL (decl), false);
891 /* Make this function static known to the mudflap runtime. */
892 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
893 mudflap_enqueue_decl (decl);
895 return;
898 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
901 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
903 reg_number = decode_reg_name (name);
904 /* First detect errors in declaring global registers. */
905 if (reg_number == -1)
906 error ("%Jregister name not specified for %qD", decl, decl);
907 else if (reg_number < 0)
908 error ("%Jinvalid register name for %qD", decl, decl);
909 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
910 error ("%Jdata type of %qD isn%'t suitable for a register",
911 decl, decl);
912 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
913 error ("%Jregister specified for %qD isn%'t suitable for data type",
914 decl, decl);
915 /* Now handle properly declared static register variables. */
916 else
918 int nregs;
920 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
922 DECL_INITIAL (decl) = 0;
923 error ("global register variable has initial value");
925 if (TREE_THIS_VOLATILE (decl))
926 warning ("volatile register variables don%'t "
927 "work as you might wish");
929 /* If the user specified one of the eliminables registers here,
930 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
931 confused with that register and be eliminated. This usage is
932 somewhat suspect... */
934 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
935 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
936 REG_USERVAR_P (DECL_RTL (decl)) = 1;
938 if (TREE_STATIC (decl))
940 /* Make this register global, so not usable for anything
941 else. */
942 #ifdef ASM_DECLARE_REGISTER_GLOBAL
943 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
944 #endif
945 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
946 while (nregs > 0)
947 globalize_reg (reg_number + --nregs);
950 /* As a register variable, it has no section. */
951 return;
954 /* Now handle ordinary static variables and functions (in memory).
955 Also handle vars declared register invalidly. */
956 else if (name[0] == '*')
958 #ifdef REGISTER_PREFIX
959 if (strlen (REGISTER_PREFIX) != 0)
961 reg_number = decode_reg_name (name);
962 if (reg_number >= 0 || reg_number == -3)
963 error ("%Jregister name given for non-register variable %qD", decl, decl);
965 #endif
968 /* Specifying a section attribute on a variable forces it into a
969 non-.bss section, and thus it cannot be common. */
970 if (TREE_CODE (decl) == VAR_DECL
971 && DECL_SECTION_NAME (decl) != NULL_TREE
972 && DECL_INITIAL (decl) == NULL_TREE
973 && DECL_COMMON (decl))
974 DECL_COMMON (decl) = 0;
976 /* Variables can't be both common and weak. */
977 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
978 DECL_COMMON (decl) = 0;
980 x = gen_rtx_SYMBOL_REF (Pmode, name);
981 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
982 SYMBOL_REF_DECL (x) = decl;
984 x = gen_rtx_MEM (DECL_MODE (decl), x);
985 if (TREE_CODE (decl) != FUNCTION_DECL)
986 set_mem_attributes (x, decl, 1);
987 SET_DECL_RTL (decl, x);
989 /* Optionally set flags or add text to the name to record information
990 such as that it is a function name.
991 If the name is changed, the macro ASM_OUTPUT_LABELREF
992 will have to know how to strip this information. */
993 targetm.encode_section_info (decl, DECL_RTL (decl), true);
995 /* Make this function static known to the mudflap runtime. */
996 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
997 mudflap_enqueue_decl (decl);
1000 /* Make the rtl for variable VAR be volatile.
1001 Use this only for static variables. */
1003 void
1004 make_var_volatile (tree var)
1006 gcc_assert (MEM_P (DECL_RTL (var)));
1008 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
1011 /* Output a string of literal assembler code
1012 for an `asm' keyword used between functions. */
1014 void
1015 assemble_asm (tree string)
1017 app_enable ();
1019 if (TREE_CODE (string) == ADDR_EXPR)
1020 string = TREE_OPERAND (string, 0);
1022 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1025 /* Record an element in the table of global destructors. SYMBOL is
1026 a SYMBOL_REF of the function to be called; PRIORITY is a number
1027 between 0 and MAX_INIT_PRIORITY. */
1029 void
1030 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1031 int priority ATTRIBUTE_UNUSED)
1033 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1034 /* Tell GNU LD that this is part of the static destructor set.
1035 This will work for any system that uses stabs, most usefully
1036 aout systems. */
1037 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1038 dbxout_stab_value_label (XSTR (symbol, 0));
1039 #else
1040 sorry ("global destructors not supported on this target");
1041 #endif
1044 void
1045 default_named_section_asm_out_destructor (rtx symbol, int priority)
1047 const char *section = ".dtors";
1048 char buf[16];
1050 /* ??? This only works reliably with the GNU linker. */
1051 if (priority != DEFAULT_INIT_PRIORITY)
1053 sprintf (buf, ".dtors.%.5u",
1054 /* Invert the numbering so the linker puts us in the proper
1055 order; constructors are run from right to left, and the
1056 linker sorts in increasing order. */
1057 MAX_INIT_PRIORITY - priority);
1058 section = buf;
1061 named_section_flags (section, SECTION_WRITE);
1062 assemble_align (POINTER_SIZE);
1063 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1066 #ifdef DTORS_SECTION_ASM_OP
1067 void
1068 dtors_section (void)
1070 if (in_section != in_dtors)
1072 in_section = in_dtors;
1073 fputs (DTORS_SECTION_ASM_OP, asm_out_file);
1074 fputc ('\n', asm_out_file);
1078 void
1079 default_dtor_section_asm_out_destructor (rtx symbol,
1080 int priority ATTRIBUTE_UNUSED)
1082 dtors_section ();
1083 assemble_align (POINTER_SIZE);
1084 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1086 #endif
1088 /* Likewise for global constructors. */
1090 void
1091 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1092 int priority ATTRIBUTE_UNUSED)
1094 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1095 /* Tell GNU LD that this is part of the static destructor set.
1096 This will work for any system that uses stabs, most usefully
1097 aout systems. */
1098 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1099 dbxout_stab_value_label (XSTR (symbol, 0));
1100 #else
1101 sorry ("global constructors not supported on this target");
1102 #endif
1105 void
1106 default_named_section_asm_out_constructor (rtx symbol, int priority)
1108 const char *section = ".ctors";
1109 char buf[16];
1111 /* ??? This only works reliably with the GNU linker. */
1112 if (priority != DEFAULT_INIT_PRIORITY)
1114 sprintf (buf, ".ctors.%.5u",
1115 /* Invert the numbering so the linker puts us in the proper
1116 order; constructors are run from right to left, and the
1117 linker sorts in increasing order. */
1118 MAX_INIT_PRIORITY - priority);
1119 section = buf;
1122 named_section_flags (section, SECTION_WRITE);
1123 assemble_align (POINTER_SIZE);
1124 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1127 #ifdef CTORS_SECTION_ASM_OP
1128 void
1129 ctors_section (void)
1131 if (in_section != in_ctors)
1133 in_section = in_ctors;
1134 fputs (CTORS_SECTION_ASM_OP, asm_out_file);
1135 fputc ('\n', asm_out_file);
1139 void
1140 default_ctor_section_asm_out_constructor (rtx symbol,
1141 int priority ATTRIBUTE_UNUSED)
1143 ctors_section ();
1144 assemble_align (POINTER_SIZE);
1145 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1147 #endif
1149 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1150 a nonzero value if the constant pool should be output before the
1151 start of the function, or a zero value if the pool should output
1152 after the end of the function. The default is to put it before the
1153 start. */
1155 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1156 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1157 #endif
1159 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1160 to be output to assembler.
1161 Set first_global_object_name and weak_global_object_name as appropriate. */
1163 void
1164 notice_global_symbol (tree decl)
1166 const char **type = &first_global_object_name;
1168 if (first_global_object_name
1169 || !TREE_PUBLIC (decl) || DECL_EXTERNAL (decl)
1170 || !DECL_NAME (decl)
1171 || (TREE_CODE (decl) != FUNCTION_DECL
1172 && (TREE_CODE (decl) != VAR_DECL
1173 || (DECL_COMMON (decl)
1174 && (DECL_INITIAL (decl) == 0
1175 || DECL_INITIAL (decl) == error_mark_node))))
1176 || !MEM_P (DECL_RTL (decl)))
1177 return;
1179 /* We win when global object is found, but it is useful to know about weak
1180 symbol as well so we can produce nicer unique names. */
1181 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1182 type = &weak_global_object_name;
1184 if (!*type)
1186 const char *p;
1187 char *name;
1188 rtx decl_rtl = DECL_RTL (decl);
1190 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1191 name = xstrdup (p);
1193 *type = name;
1197 /* Output assembler code for the constant pool of a function and associated
1198 with defining the name of the function. DECL describes the function.
1199 NAME is the function's name. For the constant pool, we use the current
1200 constant pool data. */
1202 void
1203 assemble_start_function (tree decl, const char *fnname)
1205 int align;
1207 if (unlikely_text_section_name)
1208 free (unlikely_text_section_name);
1210 unlikely_section_label_printed = false;
1211 unlikely_text_section_name = NULL;
1213 unlikely_section_label = reconcat (unlikely_section_label,
1214 fnname, ".unlikely_section", NULL);
1216 /* The following code does not need preprocessing in the assembler. */
1218 app_disable ();
1220 if (CONSTANT_POOL_BEFORE_FUNCTION)
1221 output_constant_pool (fnname, decl);
1223 /* Make sure the cold text (code) section is properly aligned. This
1224 is necessary here in the case where the function has both hot and
1225 cold sections, because we don't want to re-set the alignment when the
1226 section switch happens mid-function. We don't need to set the hot
1227 section alignment here, because code further down in this function
1228 sets the alignment for whichever section comes first, and if there
1229 is a hot section it is guaranteed to be first. */
1231 if (flag_reorder_blocks_and_partition)
1233 unlikely_text_section ();
1234 assemble_align (FUNCTION_BOUNDARY);
1237 resolve_unique_section (decl, 0, flag_function_sections);
1238 function_section (decl);
1240 /* Tell assembler to move to target machine's alignment for functions. */
1241 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1242 if (align < force_align_functions_log)
1243 align = force_align_functions_log;
1244 if (align > 0)
1246 ASM_OUTPUT_ALIGN (asm_out_file, align);
1249 /* Handle a user-specified function alignment.
1250 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1251 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1252 if (align_functions_log > align
1253 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1255 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1256 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1257 align_functions_log, align_functions - 1);
1258 #else
1259 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1260 #endif
1263 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1264 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1265 #endif
1267 (*debug_hooks->begin_function) (decl);
1269 /* Make function name accessible from other files, if appropriate. */
1271 if (TREE_PUBLIC (decl))
1273 notice_global_symbol (decl);
1275 globalize_decl (decl);
1277 maybe_assemble_visibility (decl);
1280 if (DECL_PRESERVE_P (decl))
1281 targetm.asm_out.mark_decl_preserved (fnname);
1283 /* Do any machine/system dependent processing of the function name. */
1284 #ifdef ASM_DECLARE_FUNCTION_NAME
1285 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1286 #else
1287 /* Standard thing is just output label for the function. */
1288 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1289 #endif /* ASM_DECLARE_FUNCTION_NAME */
1291 if (in_unlikely_text_section ()
1292 && !unlikely_section_label_printed)
1294 ASM_OUTPUT_LABEL (asm_out_file, unlikely_section_label);
1295 unlikely_section_label_printed = true;
1299 /* Output assembler code associated with defining the size of the
1300 function. DECL describes the function. NAME is the function's name. */
1302 void
1303 assemble_end_function (tree decl, const char *fnname)
1305 #ifdef ASM_DECLARE_FUNCTION_SIZE
1306 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1307 #endif
1308 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1310 output_constant_pool (fnname, decl);
1311 function_section (decl); /* need to switch back */
1315 /* Assemble code to leave SIZE bytes of zeros. */
1317 void
1318 assemble_zeros (unsigned HOST_WIDE_INT size)
1320 /* Do no output if -fsyntax-only. */
1321 if (flag_syntax_only)
1322 return;
1324 #ifdef ASM_NO_SKIP_IN_TEXT
1325 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1326 so we must output 0s explicitly in the text section. */
1327 if ((ASM_NO_SKIP_IN_TEXT && in_text_section ())
1328 || (ASM_NO_SKIP_IN_TEXT && in_unlikely_text_section ()))
1330 unsigned HOST_WIDE_INT i;
1331 for (i = 0; i < size; i++)
1332 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1334 else
1335 #endif
1336 if (size > 0)
1337 ASM_OUTPUT_SKIP (asm_out_file, size);
1340 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1342 void
1343 assemble_align (int align)
1345 if (align > BITS_PER_UNIT)
1347 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1351 /* Assemble a string constant with the specified C string as contents. */
1353 void
1354 assemble_string (const char *p, int size)
1356 int pos = 0;
1357 int maximum = 2000;
1359 /* If the string is very long, split it up. */
1361 while (pos < size)
1363 int thissize = size - pos;
1364 if (thissize > maximum)
1365 thissize = maximum;
1367 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1369 pos += thissize;
1370 p += thissize;
1375 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1376 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1377 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1378 #else
1379 #if defined ASM_OUTPUT_ALIGNED_LOCAL
1380 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1381 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl))
1382 #else
1383 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1384 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded)
1385 #endif
1386 #endif
1388 #if defined ASM_OUTPUT_ALIGNED_BSS
1389 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1390 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1391 #else
1392 #if defined ASM_OUTPUT_BSS
1393 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1394 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded)
1395 #else
1396 #undef ASM_EMIT_BSS
1397 #endif
1398 #endif
1400 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1401 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1402 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1403 #else
1404 #if defined ASM_OUTPUT_ALIGNED_COMMON
1405 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1406 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl))
1407 #else
1408 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1409 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded)
1410 #endif
1411 #endif
1413 static bool
1414 asm_emit_uninitialised (tree decl, const char *name,
1415 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1416 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1418 enum
1420 asm_dest_common,
1421 asm_dest_bss,
1422 asm_dest_local
1424 destination = asm_dest_local;
1426 /* ??? We should handle .bss via select_section mechanisms rather than
1427 via special target hooks. That would eliminate this special case. */
1428 if (TREE_PUBLIC (decl))
1430 if (!DECL_COMMON (decl))
1431 #ifdef ASM_EMIT_BSS
1432 destination = asm_dest_bss;
1433 #else
1434 return false;
1435 #endif
1436 else
1437 destination = asm_dest_common;
1440 if (destination != asm_dest_common)
1442 resolve_unique_section (decl, 0, flag_data_sections);
1443 /* Custom sections don't belong here. */
1444 if (DECL_SECTION_NAME (decl))
1445 return false;
1448 if (destination == asm_dest_bss)
1449 globalize_decl (decl);
1451 if (flag_shared_data)
1453 switch (destination)
1455 #ifdef ASM_OUTPUT_SHARED_BSS
1456 case asm_dest_bss:
1457 ASM_OUTPUT_SHARED_BSS (asm_out_file, decl, name, size, rounded);
1458 return;
1459 #endif
1460 #ifdef ASM_OUTPUT_SHARED_COMMON
1461 case asm_dest_common:
1462 ASM_OUTPUT_SHARED_COMMON (asm_out_file, name, size, rounded);
1463 return;
1464 #endif
1465 #ifdef ASM_OUTPUT_SHARED_LOCAL
1466 case asm_dest_local:
1467 ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded);
1468 return;
1469 #endif
1470 default:
1471 break;
1475 switch (destination)
1477 #ifdef ASM_EMIT_BSS
1478 case asm_dest_bss:
1479 ASM_EMIT_BSS (decl, name, size, rounded);
1480 break;
1481 #endif
1482 case asm_dest_common:
1483 ASM_EMIT_COMMON (decl, name, size, rounded);
1484 break;
1485 case asm_dest_local:
1486 ASM_EMIT_LOCAL (decl, name, size, rounded);
1487 break;
1488 default:
1489 gcc_unreachable ();
1492 return true;
1495 /* Assemble everything that is needed for a variable or function declaration.
1496 Not used for automatic variables, and not used for function definitions.
1497 Should not be called for variables of incomplete structure type.
1499 TOP_LEVEL is nonzero if this variable has file scope.
1500 AT_END is nonzero if this is the special handling, at end of compilation,
1501 to define things that have had only tentative definitions.
1502 DONT_OUTPUT_DATA if nonzero means don't actually output the
1503 initial value (that will be done by the caller). */
1505 void
1506 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1507 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1509 const char *name;
1510 unsigned int align;
1511 int reloc = 0;
1512 rtx decl_rtl;
1514 if (lang_hooks.decls.prepare_assemble_variable)
1515 lang_hooks.decls.prepare_assemble_variable (decl);
1517 last_assemble_variable_decl = 0;
1519 /* Normally no need to say anything here for external references,
1520 since assemble_external is called by the language-specific code
1521 when a declaration is first seen. */
1523 if (DECL_EXTERNAL (decl))
1524 return;
1526 /* Output no assembler code for a function declaration.
1527 Only definitions of functions output anything. */
1529 if (TREE_CODE (decl) == FUNCTION_DECL)
1530 return;
1532 /* Do nothing for global register variables. */
1533 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1535 TREE_ASM_WRITTEN (decl) = 1;
1536 return;
1539 /* If type was incomplete when the variable was declared,
1540 see if it is complete now. */
1542 if (DECL_SIZE (decl) == 0)
1543 layout_decl (decl, 0);
1545 /* Still incomplete => don't allocate it; treat the tentative defn
1546 (which is what it must have been) as an `extern' reference. */
1548 if (!dont_output_data && DECL_SIZE (decl) == 0)
1550 error ("%Jstorage size of %qD isn%'t known", decl, decl);
1551 TREE_ASM_WRITTEN (decl) = 1;
1552 return;
1555 /* The first declaration of a variable that comes through this function
1556 decides whether it is global (in C, has external linkage)
1557 or local (in C, has internal linkage). So do nothing more
1558 if this function has already run. */
1560 if (TREE_ASM_WRITTEN (decl))
1561 return;
1563 /* Make sure targetm.encode_section_info is invoked before we set
1564 ASM_WRITTEN. */
1565 decl_rtl = DECL_RTL (decl);
1567 TREE_ASM_WRITTEN (decl) = 1;
1569 /* Do no output if -fsyntax-only. */
1570 if (flag_syntax_only)
1571 return;
1573 app_disable ();
1575 if (! dont_output_data
1576 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1578 error ("%Jsize of variable %qD is too large", decl, decl);
1579 return;
1582 name = XSTR (XEXP (decl_rtl, 0), 0);
1583 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1584 notice_global_symbol (decl);
1586 /* Compute the alignment of this data. */
1588 align = DECL_ALIGN (decl);
1590 /* In the case for initialing an array whose length isn't specified,
1591 where we have not yet been able to do the layout,
1592 figure out the proper alignment now. */
1593 if (dont_output_data && DECL_SIZE (decl) == 0
1594 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1595 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1597 /* Some object file formats have a maximum alignment which they support.
1598 In particular, a.out format supports a maximum alignment of 4. */
1599 if (align > MAX_OFILE_ALIGNMENT)
1601 warning ("%Jalignment of %qD is greater than maximum object "
1602 "file alignment. Using %d", decl, decl,
1603 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1604 align = MAX_OFILE_ALIGNMENT;
1607 /* On some machines, it is good to increase alignment sometimes. */
1608 if (! DECL_USER_ALIGN (decl))
1610 #ifdef DATA_ALIGNMENT
1611 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1612 #endif
1613 #ifdef CONSTANT_ALIGNMENT
1614 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1615 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1616 #endif
1619 /* Reset the alignment in case we have made it tighter, so we can benefit
1620 from it in get_pointer_alignment. */
1621 DECL_ALIGN (decl) = align;
1622 set_mem_align (decl_rtl, align);
1624 if (TREE_PUBLIC (decl))
1625 maybe_assemble_visibility (decl);
1627 if (DECL_PRESERVE_P (decl))
1628 targetm.asm_out.mark_decl_preserved (name);
1630 /* Handle uninitialized definitions. */
1632 /* If the decl has been given an explicit section name, then it
1633 isn't common, and shouldn't be handled as such. */
1634 if (DECL_SECTION_NAME (decl) || dont_output_data)
1636 /* We don't implement common thread-local data at present. */
1637 else if (DECL_THREAD_LOCAL (decl))
1639 if (DECL_COMMON (decl))
1640 sorry ("thread-local COMMON data not implemented");
1642 else if (DECL_INITIAL (decl) == 0
1643 || DECL_INITIAL (decl) == error_mark_node
1644 || (flag_zero_initialized_in_bss
1645 /* Leave constant zeroes in .rodata so they can be shared. */
1646 && !TREE_READONLY (decl)
1647 && initializer_zerop (DECL_INITIAL (decl))))
1649 unsigned HOST_WIDE_INT size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1650 unsigned HOST_WIDE_INT rounded = size;
1652 /* Don't allocate zero bytes of common,
1653 since that means "undefined external" in the linker. */
1654 if (size == 0)
1655 rounded = 1;
1657 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1658 so that each uninitialized object starts on such a boundary. */
1659 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1660 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1661 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1663 #if !defined(ASM_OUTPUT_ALIGNED_COMMON) && !defined(ASM_OUTPUT_ALIGNED_DECL_COMMON) && !defined(ASM_OUTPUT_ALIGNED_BSS)
1664 if ((unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1665 warning ("%Jrequested alignment for %qD is greater than "
1666 "implemented alignment of %d", decl, decl, rounded);
1667 #endif
1669 /* If the target cannot output uninitialized but not common global data
1670 in .bss, then we have to use .data, so fall through. */
1671 if (asm_emit_uninitialised (decl, name, size, rounded))
1672 return;
1675 /* Handle initialized definitions.
1676 Also handle uninitialized global definitions if -fno-common and the
1677 target doesn't support ASM_OUTPUT_BSS. */
1679 /* First make the assembler name(s) global if appropriate. */
1680 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1681 globalize_decl (decl);
1683 /* Output any data that we will need to use the address of. */
1684 if (DECL_INITIAL (decl) == error_mark_node)
1685 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1686 else if (DECL_INITIAL (decl))
1688 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1689 output_addressed_constants (DECL_INITIAL (decl));
1692 /* Switch to the appropriate section. */
1693 resolve_unique_section (decl, reloc, flag_data_sections);
1694 variable_section (decl, reloc);
1696 /* dbxout.c needs to know this. */
1697 if (in_text_section () || in_unlikely_text_section ())
1698 DECL_IN_TEXT_SECTION (decl) = 1;
1700 /* Output the alignment of this data. */
1701 if (align > BITS_PER_UNIT)
1702 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1704 /* Do any machine/system dependent processing of the object. */
1705 #ifdef ASM_DECLARE_OBJECT_NAME
1706 last_assemble_variable_decl = decl;
1707 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1708 #else
1709 /* Standard thing is just output label for the object. */
1710 ASM_OUTPUT_LABEL (asm_out_file, name);
1711 #endif /* ASM_DECLARE_OBJECT_NAME */
1713 if (!dont_output_data)
1715 if (DECL_INITIAL (decl)
1716 && DECL_INITIAL (decl) != error_mark_node
1717 && !initializer_zerop (DECL_INITIAL (decl)))
1718 /* Output the actual data. */
1719 output_constant (DECL_INITIAL (decl),
1720 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1721 align);
1722 else
1723 /* Leave space for it. */
1724 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1728 /* Return 1 if type TYPE contains any pointers. */
1730 static int
1731 contains_pointers_p (tree type)
1733 switch (TREE_CODE (type))
1735 case POINTER_TYPE:
1736 case REFERENCE_TYPE:
1737 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1738 so I'll play safe and return 1. */
1739 case OFFSET_TYPE:
1740 return 1;
1742 case RECORD_TYPE:
1743 case UNION_TYPE:
1744 case QUAL_UNION_TYPE:
1746 tree fields;
1747 /* For a type that has fields, see if the fields have pointers. */
1748 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1749 if (TREE_CODE (fields) == FIELD_DECL
1750 && contains_pointers_p (TREE_TYPE (fields)))
1751 return 1;
1752 return 0;
1755 case ARRAY_TYPE:
1756 /* An array type contains pointers if its element type does. */
1757 return contains_pointers_p (TREE_TYPE (type));
1759 default:
1760 return 0;
1764 /* In unit-at-a-time mode, we delay assemble_external processing until
1765 the compilation unit is finalized. This is the best we can do for
1766 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1767 it all the way to final. See PR 17982 for further discussion. */
1768 static GTY(()) tree pending_assemble_externals;
1770 #ifdef ASM_OUTPUT_EXTERNAL
1771 /* True if DECL is a function decl for which no out-of-line copy exists.
1772 It is assumed that DECL's assembler name has been set. */
1774 static bool
1775 incorporeal_function_p (tree decl)
1777 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1779 const char *name;
1781 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1782 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1783 return true;
1785 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1786 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1787 return true;
1789 return false;
1792 /* Actually do the tests to determine if this is necessary, and invoke
1793 ASM_OUTPUT_EXTERNAL. */
1794 static void
1795 assemble_external_real (tree decl)
1797 rtx rtl = DECL_RTL (decl);
1799 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1800 && !SYMBOL_REF_USED (XEXP (rtl, 0))
1801 && !incorporeal_function_p (decl))
1803 /* Some systems do require some output. */
1804 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1805 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1808 #endif
1810 void
1811 process_pending_assemble_externals (void)
1813 #ifdef ASM_OUTPUT_EXTERNAL
1814 tree list;
1815 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1816 assemble_external_real (TREE_VALUE (list));
1818 pending_assemble_externals = 0;
1819 #endif
1822 /* Output something to declare an external symbol to the assembler.
1823 (Most assemblers don't need this, so we normally output nothing.)
1824 Do nothing if DECL is not external. */
1826 void
1827 assemble_external (tree decl ATTRIBUTE_UNUSED)
1829 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1830 main body of this code is only rarely exercised. To provide some
1831 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1832 open. If it's not, we should not be calling this function. */
1833 gcc_assert (asm_out_file);
1835 #ifdef ASM_OUTPUT_EXTERNAL
1836 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1837 return;
1839 if (flag_unit_at_a_time)
1840 pending_assemble_externals = tree_cons (0, decl,
1841 pending_assemble_externals);
1842 else
1843 assemble_external_real (decl);
1844 #endif
1847 /* Similar, for calling a library function FUN. */
1849 void
1850 assemble_external_libcall (rtx fun)
1852 /* Declare library function name external when first used, if nec. */
1853 if (! SYMBOL_REF_USED (fun))
1855 SYMBOL_REF_USED (fun) = 1;
1856 targetm.asm_out.external_libcall (fun);
1860 /* Assemble a label named NAME. */
1862 void
1863 assemble_label (const char *name)
1865 ASM_OUTPUT_LABEL (asm_out_file, name);
1868 /* Set the symbol_referenced flag for ID. */
1869 void
1870 mark_referenced (tree id)
1872 TREE_SYMBOL_REFERENCED (id) = 1;
1875 /* Set the symbol_referenced flag for DECL and notify callgraph. */
1876 void
1877 mark_decl_referenced (tree decl)
1879 if (TREE_CODE (decl) == FUNCTION_DECL)
1880 cgraph_mark_needed_node (cgraph_node (decl));
1881 else if (TREE_CODE (decl) == VAR_DECL)
1882 cgraph_varpool_mark_needed_node (cgraph_varpool_node (decl));
1883 /* else do nothing - we can get various sorts of CST nodes here,
1884 which do not need to be marked. */
1887 /* Output to FILE (an assembly file) a reference to NAME. If NAME
1888 starts with a *, the rest of NAME is output verbatim. Otherwise
1889 NAME is transformed in a target-specific way (usually by the
1890 addition of an underscore). */
1892 void
1893 assemble_name_raw (FILE *file, const char *name)
1895 if (name[0] == '*')
1896 fputs (&name[1], file);
1897 else
1898 ASM_OUTPUT_LABELREF (file, name);
1901 /* Like assemble_name_raw, but should be used when NAME might refer to
1902 an entity that is also represented as a tree (like a function or
1903 variable). If NAME does refer to such an entity, that entity will
1904 be marked as referenced. */
1906 void
1907 assemble_name (FILE *file, const char *name)
1909 const char *real_name;
1910 tree id;
1912 real_name = targetm.strip_name_encoding (name);
1914 id = maybe_get_identifier (real_name);
1915 if (id)
1916 mark_referenced (id);
1918 assemble_name_raw (file, name);
1921 /* Allocate SIZE bytes writable static space with a gensym name
1922 and return an RTX to refer to its address. */
1925 assemble_static_space (unsigned HOST_WIDE_INT size)
1927 char name[12];
1928 const char *namestring;
1929 rtx x;
1931 #if 0
1932 if (flag_shared_data)
1933 data_section ();
1934 #endif
1936 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
1937 ++const_labelno;
1938 namestring = ggc_strdup (name);
1940 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
1941 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
1943 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
1944 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
1945 BIGGEST_ALIGNMENT);
1946 #else
1947 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
1948 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
1949 #else
1951 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1952 so that each uninitialized object starts on such a boundary. */
1953 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
1954 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
1955 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
1956 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1957 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1958 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1960 #endif
1961 #endif
1962 return x;
1965 /* Assemble the static constant template for function entry trampolines.
1966 This is done at most once per compilation.
1967 Returns an RTX for the address of the template. */
1969 static GTY(()) rtx initial_trampoline;
1971 #ifdef TRAMPOLINE_TEMPLATE
1973 assemble_trampoline_template (void)
1975 char label[256];
1976 const char *name;
1977 int align;
1978 rtx symbol;
1980 if (initial_trampoline)
1981 return initial_trampoline;
1983 /* By default, put trampoline templates in read-only data section. */
1985 #ifdef TRAMPOLINE_SECTION
1986 TRAMPOLINE_SECTION ();
1987 #else
1988 readonly_data_section ();
1989 #endif
1991 /* Write the assembler code to define one. */
1992 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
1993 if (align > 0)
1995 ASM_OUTPUT_ALIGN (asm_out_file, align);
1998 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
1999 TRAMPOLINE_TEMPLATE (asm_out_file);
2001 /* Record the rtl to refer to it. */
2002 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2003 name = ggc_strdup (label);
2004 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2005 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2007 initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2008 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2010 return initial_trampoline;
2012 #endif
2014 /* A and B are either alignments or offsets. Return the minimum alignment
2015 that may be assumed after adding the two together. */
2017 static inline unsigned
2018 min_align (unsigned int a, unsigned int b)
2020 return (a | b) & -(a | b);
2023 /* Return the assembler directive for creating a given kind of integer
2024 object. SIZE is the number of bytes in the object and ALIGNED_P
2025 indicates whether it is known to be aligned. Return NULL if the
2026 assembly dialect has no such directive.
2028 The returned string should be printed at the start of a new line and
2029 be followed immediately by the object's initial value. */
2031 const char *
2032 integer_asm_op (int size, int aligned_p)
2034 struct asm_int_op *ops;
2036 if (aligned_p)
2037 ops = &targetm.asm_out.aligned_op;
2038 else
2039 ops = &targetm.asm_out.unaligned_op;
2041 switch (size)
2043 case 1:
2044 return targetm.asm_out.byte_op;
2045 case 2:
2046 return ops->hi;
2047 case 4:
2048 return ops->si;
2049 case 8:
2050 return ops->di;
2051 case 16:
2052 return ops->ti;
2053 default:
2054 return NULL;
2058 /* Use directive OP to assemble an integer object X. Print OP at the
2059 start of the line, followed immediately by the value of X. */
2061 void
2062 assemble_integer_with_op (const char *op, rtx x)
2064 fputs (op, asm_out_file);
2065 output_addr_const (asm_out_file, x);
2066 fputc ('\n', asm_out_file);
2069 /* The default implementation of the asm_out.integer target hook. */
2071 bool
2072 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2073 unsigned int size ATTRIBUTE_UNUSED,
2074 int aligned_p ATTRIBUTE_UNUSED)
2076 const char *op = integer_asm_op (size, aligned_p);
2077 /* Avoid GAS bugs for large values. Specifically negative values whose
2078 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2079 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2080 return false;
2081 return op && (assemble_integer_with_op (op, x), true);
2084 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2085 the alignment of the integer in bits. Return 1 if we were able to output
2086 the constant, otherwise 0. If FORCE is nonzero, abort if we can't output
2087 the constant. */
2089 bool
2090 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2092 int aligned_p;
2094 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2096 /* See if the target hook can handle this kind of object. */
2097 if (targetm.asm_out.integer (x, size, aligned_p))
2098 return true;
2100 /* If the object is a multi-byte one, try splitting it up. Split
2101 it into words it if is multi-word, otherwise split it into bytes. */
2102 if (size > 1)
2104 enum machine_mode omode, imode;
2105 unsigned int subalign;
2106 unsigned int subsize, i;
2108 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2109 subalign = MIN (align, subsize * BITS_PER_UNIT);
2110 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
2111 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2113 for (i = 0; i < size; i += subsize)
2115 rtx partial = simplify_subreg (omode, x, imode, i);
2116 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2117 break;
2119 if (i == size)
2120 return true;
2122 /* If we've printed some of it, but not all of it, there's no going
2123 back now. */
2124 gcc_assert (!i);
2127 gcc_assert (!force);
2129 return false;
2132 void
2133 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2135 long data[4];
2136 int i;
2137 int bitsize, nelts, nunits, units_per;
2139 /* This is hairy. We have a quantity of known size. real_to_target
2140 will put it into an array of *host* longs, 32 bits per element
2141 (even if long is more than 32 bits). We need to determine the
2142 number of array elements that are occupied (nelts) and the number
2143 of *target* min-addressable units that will be occupied in the
2144 object file (nunits). We cannot assume that 32 divides the
2145 mode's bitsize (size * BITS_PER_UNIT) evenly.
2147 size * BITS_PER_UNIT is used here to make sure that padding bits
2148 (which might appear at either end of the value; real_to_target
2149 will include the padding bits in its output array) are included. */
2151 nunits = GET_MODE_SIZE (mode);
2152 bitsize = nunits * BITS_PER_UNIT;
2153 nelts = CEIL (bitsize, 32);
2154 units_per = 32 / BITS_PER_UNIT;
2156 real_to_target (data, &d, mode);
2158 /* Put out the first word with the specified alignment. */
2159 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2160 nunits -= units_per;
2162 /* Subsequent words need only 32-bit alignment. */
2163 align = min_align (align, 32);
2165 for (i = 1; i < nelts; i++)
2167 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2168 nunits -= units_per;
2172 /* Given an expression EXP with a constant value,
2173 reduce it to the sum of an assembler symbol and an integer.
2174 Store them both in the structure *VALUE.
2175 Abort if EXP does not reduce. */
2177 struct addr_const GTY(())
2179 rtx base;
2180 HOST_WIDE_INT offset;
2183 static void
2184 decode_addr_const (tree exp, struct addr_const *value)
2186 tree target = TREE_OPERAND (exp, 0);
2187 int offset = 0;
2188 rtx x;
2190 while (1)
2192 if (TREE_CODE (target) == COMPONENT_REF
2193 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2196 offset += int_byte_position (TREE_OPERAND (target, 1));
2197 target = TREE_OPERAND (target, 0);
2199 else if (TREE_CODE (target) == ARRAY_REF
2200 || TREE_CODE (target) == ARRAY_RANGE_REF)
2202 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2203 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2204 target = TREE_OPERAND (target, 0);
2206 else
2207 break;
2210 switch (TREE_CODE (target))
2212 case VAR_DECL:
2213 case FUNCTION_DECL:
2214 x = DECL_RTL (target);
2215 break;
2217 case LABEL_DECL:
2218 x = gen_rtx_MEM (FUNCTION_MODE,
2219 gen_rtx_LABEL_REF (VOIDmode, force_label_rtx (target)));
2220 break;
2222 case REAL_CST:
2223 case STRING_CST:
2224 case COMPLEX_CST:
2225 case CONSTRUCTOR:
2226 case INTEGER_CST:
2227 x = output_constant_def (target, 1);
2228 break;
2230 default:
2231 gcc_unreachable ();
2234 gcc_assert (MEM_P (x));
2235 x = XEXP (x, 0);
2237 value->base = x;
2238 value->offset = offset;
2241 /* Uniquize all constants that appear in memory.
2242 Each constant in memory thus far output is recorded
2243 in `const_desc_table'. */
2245 struct constant_descriptor_tree GTY(())
2247 /* A MEM for the constant. */
2248 rtx rtl;
2250 /* The value of the constant. */
2251 tree value;
2254 static GTY((param_is (struct constant_descriptor_tree)))
2255 htab_t const_desc_htab;
2257 static struct constant_descriptor_tree * build_constant_desc (tree);
2258 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2260 /* Compute a hash code for a constant expression. */
2262 static hashval_t
2263 const_desc_hash (const void *ptr)
2265 return const_hash_1 (((struct constant_descriptor_tree *)ptr)->value);
2268 static hashval_t
2269 const_hash_1 (const tree exp)
2271 const char *p;
2272 hashval_t hi;
2273 int len, i;
2274 enum tree_code code = TREE_CODE (exp);
2276 /* Either set P and LEN to the address and len of something to hash and
2277 exit the switch or return a value. */
2279 switch (code)
2281 case INTEGER_CST:
2282 p = (char *) &TREE_INT_CST (exp);
2283 len = sizeof TREE_INT_CST (exp);
2284 break;
2286 case REAL_CST:
2287 return real_hash (TREE_REAL_CST_PTR (exp));
2289 case STRING_CST:
2290 p = TREE_STRING_POINTER (exp);
2291 len = TREE_STRING_LENGTH (exp);
2292 break;
2294 case COMPLEX_CST:
2295 return (const_hash_1 (TREE_REALPART (exp)) * 5
2296 + const_hash_1 (TREE_IMAGPART (exp)));
2298 case CONSTRUCTOR:
2300 tree link;
2302 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2304 for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2305 if (TREE_VALUE (link))
2306 hi = hi * 603 + const_hash_1 (TREE_VALUE (link));
2308 return hi;
2311 case ADDR_EXPR:
2312 case FDESC_EXPR:
2314 struct addr_const value;
2316 decode_addr_const (exp, &value);
2317 switch (GET_CODE (value.base))
2319 case SYMBOL_REF:
2320 /* Don't hash the address of the SYMBOL_REF;
2321 only use the offset and the symbol name. */
2322 hi = value.offset;
2323 p = XSTR (value.base, 0);
2324 for (i = 0; p[i] != 0; i++)
2325 hi = ((hi * 613) + (unsigned) (p[i]));
2326 break;
2328 case LABEL_REF:
2329 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2330 break;
2332 default:
2333 gcc_unreachable ();
2336 return hi;
2338 case PLUS_EXPR:
2339 case MINUS_EXPR:
2340 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2341 + const_hash_1 (TREE_OPERAND (exp, 1)));
2343 case NOP_EXPR:
2344 case CONVERT_EXPR:
2345 case NON_LVALUE_EXPR:
2346 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2348 default:
2349 /* A language specific constant. Just hash the code. */
2350 return code;
2353 /* Compute hashing function. */
2354 hi = len;
2355 for (i = 0; i < len; i++)
2356 hi = ((hi * 613) + (unsigned) (p[i]));
2358 return hi;
2361 /* Wrapper of compare_constant, for the htab interface. */
2362 static int
2363 const_desc_eq (const void *p1, const void *p2)
2365 return compare_constant (((struct constant_descriptor_tree *)p1)->value,
2366 ((struct constant_descriptor_tree *)p2)->value);
2369 /* Compare t1 and t2, and return 1 only if they are known to result in
2370 the same bit pattern on output. */
2372 static int
2373 compare_constant (const tree t1, const tree t2)
2375 enum tree_code typecode;
2377 if (t1 == NULL_TREE)
2378 return t2 == NULL_TREE;
2379 if (t2 == NULL_TREE)
2380 return 0;
2382 if (TREE_CODE (t1) != TREE_CODE (t2))
2383 return 0;
2385 switch (TREE_CODE (t1))
2387 case INTEGER_CST:
2388 /* Integer constants are the same only if the same width of type. */
2389 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2390 return 0;
2391 return tree_int_cst_equal (t1, t2);
2393 case REAL_CST:
2394 /* Real constants are the same only if the same width of type. */
2395 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2396 return 0;
2398 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2400 case STRING_CST:
2401 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2402 return 0;
2404 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2405 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2406 TREE_STRING_LENGTH (t1)));
2408 case COMPLEX_CST:
2409 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2410 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2412 case CONSTRUCTOR:
2414 tree l1, l2;
2416 typecode = TREE_CODE (TREE_TYPE (t1));
2417 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2418 return 0;
2420 if (typecode == ARRAY_TYPE)
2422 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2423 /* For arrays, check that the sizes all match. */
2424 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2425 || size_1 == -1
2426 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2427 return 0;
2429 else
2431 /* For record and union constructors, require exact type
2432 equality. */
2433 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2434 return 0;
2437 for (l1 = CONSTRUCTOR_ELTS (t1), l2 = CONSTRUCTOR_ELTS (t2);
2438 l1 && l2;
2439 l1 = TREE_CHAIN (l1), l2 = TREE_CHAIN (l2))
2441 /* Check that each value is the same... */
2442 if (! compare_constant (TREE_VALUE (l1), TREE_VALUE (l2)))
2443 return 0;
2444 /* ... and that they apply to the same fields! */
2445 if (typecode == ARRAY_TYPE)
2447 if (! compare_constant (TREE_PURPOSE (l1),
2448 TREE_PURPOSE (l2)))
2449 return 0;
2451 else
2453 if (TREE_PURPOSE (l1) != TREE_PURPOSE (l2))
2454 return 0;
2458 return l1 == NULL_TREE && l2 == NULL_TREE;
2461 case ADDR_EXPR:
2462 case FDESC_EXPR:
2464 struct addr_const value1, value2;
2466 decode_addr_const (t1, &value1);
2467 decode_addr_const (t2, &value2);
2468 return (value1.offset == value2.offset
2469 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2472 case PLUS_EXPR:
2473 case MINUS_EXPR:
2474 case RANGE_EXPR:
2475 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2476 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2478 case NOP_EXPR:
2479 case CONVERT_EXPR:
2480 case NON_LVALUE_EXPR:
2481 case VIEW_CONVERT_EXPR:
2482 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2484 default:
2486 tree nt1, nt2;
2487 nt1 = lang_hooks.expand_constant (t1);
2488 nt2 = lang_hooks.expand_constant (t2);
2489 if (nt1 != t1 || nt2 != t2)
2490 return compare_constant (nt1, nt2);
2491 else
2492 return 0;
2496 gcc_unreachable ();
2499 /* Make a copy of the whole tree structure for a constant. This
2500 handles the same types of nodes that compare_constant handles. */
2502 static tree
2503 copy_constant (tree exp)
2505 switch (TREE_CODE (exp))
2507 case ADDR_EXPR:
2508 /* For ADDR_EXPR, we do not want to copy the decl whose address
2509 is requested. We do want to copy constants though. */
2510 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2511 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2512 copy_constant (TREE_OPERAND (exp, 0)));
2513 else
2514 return copy_node (exp);
2516 case INTEGER_CST:
2517 case REAL_CST:
2518 case STRING_CST:
2519 return copy_node (exp);
2521 case COMPLEX_CST:
2522 return build_complex (TREE_TYPE (exp),
2523 copy_constant (TREE_REALPART (exp)),
2524 copy_constant (TREE_IMAGPART (exp)));
2526 case PLUS_EXPR:
2527 case MINUS_EXPR:
2528 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2529 copy_constant (TREE_OPERAND (exp, 0)),
2530 copy_constant (TREE_OPERAND (exp, 1)));
2532 case NOP_EXPR:
2533 case CONVERT_EXPR:
2534 case NON_LVALUE_EXPR:
2535 case VIEW_CONVERT_EXPR:
2536 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2537 copy_constant (TREE_OPERAND (exp, 0)));
2539 case CONSTRUCTOR:
2541 tree copy = copy_node (exp);
2542 tree list = copy_list (CONSTRUCTOR_ELTS (exp));
2543 tree tail;
2545 CONSTRUCTOR_ELTS (copy) = list;
2546 for (tail = list; tail; tail = TREE_CHAIN (tail))
2547 TREE_VALUE (tail) = copy_constant (TREE_VALUE (tail));
2549 return copy;
2552 default:
2554 tree t = lang_hooks.expand_constant (exp);
2556 gcc_assert (t == exp);
2557 return copy_constant (t);
2562 /* Subroutine of output_constant_def:
2563 No constant equal to EXP is known to have been output.
2564 Make a constant descriptor to enter EXP in the hash table.
2565 Assign the label number and construct RTL to refer to the
2566 constant's location in memory.
2567 Caller is responsible for updating the hash table. */
2569 static struct constant_descriptor_tree *
2570 build_constant_desc (tree exp)
2572 rtx symbol;
2573 rtx rtl;
2574 char label[256];
2575 int labelno;
2576 struct constant_descriptor_tree *desc;
2578 desc = ggc_alloc (sizeof (*desc));
2579 desc->value = copy_constant (exp);
2581 /* Propagate marked-ness to copied constant. */
2582 if (flag_mudflap && mf_marked_p (exp))
2583 mf_mark (desc->value);
2585 /* Create a string containing the label name, in LABEL. */
2586 labelno = const_labelno++;
2587 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2589 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
2590 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2591 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2592 SYMBOL_REF_DECL (symbol) = desc->value;
2593 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2595 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2596 set_mem_attributes (rtl, exp, 1);
2597 set_mem_alias_set (rtl, 0);
2598 set_mem_alias_set (rtl, const_alias_set);
2600 /* Set flags or add text to the name to record information, such as
2601 that it is a local symbol. If the name is changed, the macro
2602 ASM_OUTPUT_LABELREF will have to know how to strip this
2603 information. This call might invalidate our local variable
2604 SYMBOL; we can't use it afterward. */
2606 targetm.encode_section_info (exp, rtl, true);
2608 desc->rtl = rtl;
2610 return desc;
2613 /* Return an rtx representing a reference to constant data in memory
2614 for the constant expression EXP.
2616 If assembler code for such a constant has already been output,
2617 return an rtx to refer to it.
2618 Otherwise, output such a constant in memory
2619 and generate an rtx for it.
2621 If DEFER is nonzero, this constant can be deferred and output only
2622 if referenced in the function after all optimizations.
2624 `const_desc_table' records which constants already have label strings. */
2627 output_constant_def (tree exp, int defer)
2629 struct constant_descriptor_tree *desc;
2630 struct constant_descriptor_tree key;
2631 void **loc;
2633 /* Look up EXP in the table of constant descriptors. If we didn't find
2634 it, create a new one. */
2635 key.value = exp;
2636 loc = htab_find_slot (const_desc_htab, &key, INSERT);
2638 desc = *loc;
2639 if (desc == 0)
2641 desc = build_constant_desc (exp);
2642 *loc = desc;
2645 maybe_output_constant_def_contents (desc, defer);
2646 return desc->rtl;
2649 /* Subroutine of output_constant_def: Decide whether or not we need to
2650 output the constant DESC now, and if so, do it. */
2651 static void
2652 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2653 int defer)
2655 rtx symbol = XEXP (desc->rtl, 0);
2656 tree exp = desc->value;
2658 if (flag_syntax_only)
2659 return;
2661 if (TREE_ASM_WRITTEN (exp))
2662 /* Already output; don't do it again. */
2663 return;
2665 /* We can always defer constants as long as the context allows
2666 doing so. */
2667 if (defer)
2669 /* Increment n_deferred_constants if it exists. It needs to be at
2670 least as large as the number of constants actually referred to
2671 by the function. If it's too small we'll stop looking too early
2672 and fail to emit constants; if it's too large we'll only look
2673 through the entire function when we could have stopped earlier. */
2674 if (cfun)
2675 n_deferred_constants++;
2676 return;
2679 output_constant_def_contents (symbol);
2682 /* We must output the constant data referred to by SYMBOL; do so. */
2684 static void
2685 output_constant_def_contents (rtx symbol)
2687 tree exp = SYMBOL_REF_DECL (symbol);
2688 const char *label = XSTR (symbol, 0);
2689 HOST_WIDE_INT size;
2691 /* Make sure any other constants whose addresses appear in EXP
2692 are assigned label numbers. */
2693 int reloc = compute_reloc_for_constant (exp);
2695 /* Align the location counter as required by EXP's data type. */
2696 unsigned int align = TYPE_ALIGN (TREE_TYPE (exp));
2697 #ifdef CONSTANT_ALIGNMENT
2698 align = CONSTANT_ALIGNMENT (exp, align);
2699 #endif
2701 output_addressed_constants (exp);
2703 /* We are no longer deferring this constant. */
2704 TREE_ASM_WRITTEN (exp) = 1;
2706 if (IN_NAMED_SECTION (exp))
2707 named_section (exp, NULL, reloc);
2708 else
2709 targetm.asm_out.select_section (exp, reloc, align);
2711 if (align > BITS_PER_UNIT)
2713 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2716 size = int_size_in_bytes (TREE_TYPE (exp));
2717 if (TREE_CODE (exp) == STRING_CST)
2718 size = MAX (TREE_STRING_LENGTH (exp), size);
2720 /* Do any machine/system dependent processing of the constant. */
2721 #ifdef ASM_DECLARE_CONSTANT_NAME
2722 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2723 #else
2724 /* Standard thing is just output label for the constant. */
2725 ASM_OUTPUT_LABEL (asm_out_file, label);
2726 #endif /* ASM_DECLARE_CONSTANT_NAME */
2728 /* Output the value of EXP. */
2729 output_constant (exp, size, align);
2730 if (flag_mudflap)
2731 mudflap_enqueue_constant (exp);
2734 /* Look up EXP in the table of constant descriptors. Return the rtl
2735 if it has been emitted, else null. */
2738 lookup_constant_def (tree exp)
2740 struct constant_descriptor_tree *desc;
2741 struct constant_descriptor_tree key;
2743 key.value = exp;
2744 desc = htab_find (const_desc_htab, &key);
2746 return (desc ? desc->rtl : NULL_RTX);
2749 /* Used in the hash tables to avoid outputting the same constant
2750 twice. Unlike 'struct constant_descriptor_tree', RTX constants
2751 are output once per function, not once per file. */
2752 /* ??? Only a few targets need per-function constant pools. Most
2753 can use one per-file pool. Should add a targetm bit to tell the
2754 difference. */
2756 struct rtx_constant_pool GTY(())
2758 /* Pointers to first and last constant in pool, as ordered by offset. */
2759 struct constant_descriptor_rtx *first;
2760 struct constant_descriptor_rtx *last;
2762 /* Hash facility for making memory-constants from constant rtl-expressions.
2763 It is used on RISC machines where immediate integer arguments and
2764 constant addresses are restricted so that such constants must be stored
2765 in memory. */
2766 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
2767 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_sym_htab;
2769 /* Current offset in constant pool (does not include any
2770 machine-specific header). */
2771 HOST_WIDE_INT offset;
2774 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
2776 struct constant_descriptor_rtx *next;
2777 rtx mem;
2778 rtx sym;
2779 rtx constant;
2780 HOST_WIDE_INT offset;
2781 hashval_t hash;
2782 enum machine_mode mode;
2783 unsigned int align;
2784 int labelno;
2785 int mark;
2788 /* Hash and compare functions for const_rtx_htab. */
2790 static hashval_t
2791 const_desc_rtx_hash (const void *ptr)
2793 const struct constant_descriptor_rtx *desc = ptr;
2794 return desc->hash;
2797 static int
2798 const_desc_rtx_eq (const void *a, const void *b)
2800 const struct constant_descriptor_rtx *x = a;
2801 const struct constant_descriptor_rtx *y = b;
2803 if (x->mode != y->mode)
2804 return 0;
2805 return rtx_equal_p (x->constant, y->constant);
2808 /* Hash and compare functions for const_rtx_sym_htab. */
2810 static hashval_t
2811 const_desc_rtx_sym_hash (const void *ptr)
2813 const struct constant_descriptor_rtx *desc = ptr;
2814 return htab_hash_string (XSTR (desc->sym, 0));
2817 static int
2818 const_desc_rtx_sym_eq (const void *a, const void *b)
2820 const struct constant_descriptor_rtx *x = a;
2821 const struct constant_descriptor_rtx *y = b;
2822 return XSTR (x->sym, 0) == XSTR (y->sym, 0);
2825 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
2827 static int
2828 const_rtx_hash_1 (rtx *xp, void *data)
2830 unsigned HOST_WIDE_INT hwi;
2831 enum machine_mode mode;
2832 enum rtx_code code;
2833 hashval_t h, *hp;
2834 rtx x;
2836 x = *xp;
2837 code = GET_CODE (x);
2838 mode = GET_MODE (x);
2839 h = (hashval_t) code * 1048573 + mode;
2841 switch (code)
2843 case CONST_INT:
2844 hwi = INTVAL (x);
2845 fold_hwi:
2847 const int shift = sizeof (hashval_t) * CHAR_BIT;
2848 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
2849 int i;
2851 h ^= (hashval_t) hwi;
2852 for (i = 1; i < n; ++i)
2854 hwi >>= shift;
2855 h ^= (hashval_t) hwi;
2858 break;
2860 case CONST_DOUBLE:
2861 if (mode == VOIDmode)
2863 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
2864 goto fold_hwi;
2866 else
2867 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
2868 break;
2870 case CONST_VECTOR:
2872 int i;
2873 for (i = XVECLEN (x, 0); i-- > 0; )
2874 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
2876 break;
2878 case SYMBOL_REF:
2879 h ^= htab_hash_string (XSTR (x, 0));
2880 break;
2882 case LABEL_REF:
2883 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
2884 break;
2886 case UNSPEC:
2887 case UNSPEC_VOLATILE:
2888 h = h * 251 + XINT (x, 1);
2889 break;
2891 default:
2892 break;
2895 hp = data;
2896 *hp = *hp * 509 + h;
2897 return 0;
2900 /* Compute a hash value for X, which should be a constant. */
2902 static hashval_t
2903 const_rtx_hash (rtx x)
2905 hashval_t h = 0;
2906 for_each_rtx (&x, const_rtx_hash_1, &h);
2907 return h;
2911 /* Initialize constant pool hashing for a new function. */
2913 void
2914 init_varasm_status (struct function *f)
2916 struct varasm_status *p;
2917 struct rtx_constant_pool *pool;
2919 p = ggc_alloc (sizeof (struct varasm_status));
2920 f->varasm = p;
2922 pool = ggc_alloc (sizeof (struct rtx_constant_pool));
2923 p->pool = pool;
2924 p->deferred_constants = 0;
2926 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
2927 const_desc_rtx_eq, NULL);
2928 pool->const_rtx_sym_htab = htab_create_ggc (31, const_desc_rtx_sym_hash,
2929 const_desc_rtx_sym_eq, NULL);
2930 pool->first = pool->last = NULL;
2931 pool->offset = 0;
2934 /* Given a MINUS expression, simplify it if both sides
2935 include the same symbol. */
2938 simplify_subtraction (rtx x)
2940 rtx r = simplify_rtx (x);
2941 return r ? r : x;
2944 /* Given a constant rtx X, make (or find) a memory constant for its value
2945 and return a MEM rtx to refer to it in memory. */
2948 force_const_mem (enum machine_mode mode, rtx x)
2950 struct constant_descriptor_rtx *desc, tmp;
2951 struct rtx_constant_pool *pool = cfun->varasm->pool;
2952 char label[256];
2953 rtx def, symbol;
2954 hashval_t hash;
2955 unsigned int align;
2956 void **slot;
2958 /* If we're not allowed to drop X into the constant pool, don't. */
2959 if (targetm.cannot_force_const_mem (x))
2960 return NULL_RTX;
2962 /* Lookup the value in the hashtable. */
2963 tmp.constant = x;
2964 tmp.mode = mode;
2965 hash = const_rtx_hash (x);
2966 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
2967 desc = *slot;
2969 /* If the constant was already present, return its memory. */
2970 if (desc)
2971 return copy_rtx (desc->mem);
2973 /* Otherwise, create a new descriptor. */
2974 desc = ggc_alloc (sizeof (*desc));
2975 *slot = desc;
2977 /* Align the location counter as required by EXP's data type. */
2978 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
2979 #ifdef CONSTANT_ALIGNMENT
2981 tree type = lang_hooks.types.type_for_mode (mode, 0);
2982 if (type != NULL_TREE)
2983 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
2985 #endif
2987 pool->offset += (align / BITS_PER_UNIT) - 1;
2988 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
2990 desc->next = NULL;
2991 desc->constant = tmp.constant;
2992 desc->offset = pool->offset;
2993 desc->hash = hash;
2994 desc->mode = mode;
2995 desc->align = align;
2996 desc->labelno = const_labelno;
2997 desc->mark = 0;
2999 pool->offset += GET_MODE_SIZE (mode);
3000 if (pool->last)
3001 pool->last->next = desc;
3002 else
3003 pool->first = pool->last = desc;
3004 pool->last = desc;
3006 /* Create a string containing the label name, in LABEL. */
3007 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3008 ++const_labelno;
3010 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3011 the constants pool. */
3012 desc->sym = symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3013 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
3014 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3015 current_function_uses_const_pool = 1;
3017 /* Insert the descriptor into the symbol cross-reference table too. */
3018 slot = htab_find_slot (pool->const_rtx_sym_htab, desc, INSERT);
3019 gcc_assert (!*slot);
3020 *slot = desc;
3022 /* Construct the MEM. */
3023 desc->mem = def = gen_const_mem (mode, symbol);
3024 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3025 set_mem_align (def, align);
3027 /* If we're dropping a label to the constant pool, make sure we
3028 don't delete it. */
3029 if (GET_CODE (x) == LABEL_REF)
3030 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3032 return copy_rtx (def);
3035 /* Given a SYMBOL_REF with CONSTANT_POOL_ADDRESS_P true, return a pointer to
3036 the corresponding constant_descriptor_rtx structure. */
3038 static struct constant_descriptor_rtx *
3039 find_pool_constant (struct rtx_constant_pool *pool, rtx sym)
3041 struct constant_descriptor_rtx tmp;
3042 tmp.sym = sym;
3043 return htab_find (pool->const_rtx_sym_htab, &tmp);
3046 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3049 get_pool_constant (rtx addr)
3051 return find_pool_constant (cfun->varasm->pool, addr)->constant;
3054 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3055 and whether it has been output or not. */
3058 get_pool_constant_mark (rtx addr, bool *pmarked)
3060 struct constant_descriptor_rtx *desc;
3062 desc = find_pool_constant (cfun->varasm->pool, addr);
3063 *pmarked = (desc->mark != 0);
3064 return desc->constant;
3067 /* Likewise, but for the constant pool of a specific function. */
3070 get_pool_constant_for_function (struct function *f, rtx addr)
3072 return find_pool_constant (f->varasm->pool, addr)->constant;
3075 /* Similar, return the mode. */
3077 enum machine_mode
3078 get_pool_mode (rtx addr)
3080 return find_pool_constant (cfun->varasm->pool, addr)->mode;
3083 /* Return the size of the constant pool. */
3086 get_pool_size (void)
3088 return cfun->varasm->pool->offset;
3091 /* Worker function for output_constant_pool_1. Emit assembly for X
3092 in MODE with known alignment ALIGN. */
3094 static void
3095 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3097 switch (GET_MODE_CLASS (mode))
3099 case MODE_FLOAT:
3101 REAL_VALUE_TYPE r;
3103 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3104 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3105 assemble_real (r, mode, align);
3106 break;
3109 case MODE_INT:
3110 case MODE_PARTIAL_INT:
3111 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3112 break;
3114 case MODE_VECTOR_FLOAT:
3115 case MODE_VECTOR_INT:
3117 int i, units;
3118 enum machine_mode submode = GET_MODE_INNER (mode);
3119 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3121 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3122 units = CONST_VECTOR_NUNITS (x);
3124 for (i = 0; i < units; i++)
3126 rtx elt = CONST_VECTOR_ELT (x, i);
3127 output_constant_pool_2 (submode, elt, i ? subalign : align);
3130 break;
3132 default:
3133 gcc_unreachable ();
3137 /* Worker function for output_constant_pool. Emit POOL. */
3139 static void
3140 output_constant_pool_1 (struct constant_descriptor_rtx *desc)
3142 rtx x, tmp;
3144 if (!desc->mark)
3145 return;
3146 x = desc->constant;
3148 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3149 whose CODE_LABEL has been deleted. This can occur if a jump table
3150 is eliminated by optimization. If so, write a constant of zero
3151 instead. Note that this can also happen by turning the
3152 CODE_LABEL into a NOTE. */
3153 /* ??? This seems completely and utterly wrong. Certainly it's
3154 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3155 functioning even with INSN_DELETED_P and friends. */
3157 tmp = x;
3158 switch (GET_CODE (x))
3160 case CONST:
3161 if (GET_CODE (XEXP (x, 0)) != PLUS
3162 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3163 break;
3164 tmp = XEXP (XEXP (x, 0), 0);
3165 /* FALLTHRU */
3167 case LABEL_REF:
3168 tmp = XEXP (x, 0);
3169 gcc_assert (!INSN_DELETED_P (tmp));
3170 gcc_assert (!NOTE_P (tmp)
3171 || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3172 break;
3174 default:
3175 break;
3178 /* First switch to correct section. */
3179 targetm.asm_out.select_rtx_section (desc->mode, x, desc->align);
3181 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3182 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3183 desc->align, desc->labelno, done);
3184 #endif
3186 assemble_align (desc->align);
3188 /* Output the label. */
3189 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3191 /* Output the data. */
3192 output_constant_pool_2 (desc->mode, x, desc->align);
3194 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3195 sections have proper size. */
3196 if (desc->align > GET_MODE_BITSIZE (desc->mode)
3197 && in_section == in_named
3198 && get_named_section_flags (in_named_name) & SECTION_MERGE)
3199 assemble_align (desc->align);
3201 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3202 done:
3203 #endif
3204 return;
3207 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3208 to as used. Emit referenced deferred strings. This function can
3209 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3211 static int
3212 mark_constant (rtx *current_rtx, void *data)
3214 struct rtx_constant_pool *pool = data;
3215 rtx x = *current_rtx;
3217 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3218 return 0;
3220 if (CONSTANT_POOL_ADDRESS_P (x))
3222 struct constant_descriptor_rtx *desc = find_pool_constant (pool, x);
3223 if (desc->mark == 0)
3225 desc->mark = 1;
3226 for_each_rtx (&desc->constant, mark_constant, pool);
3229 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3231 tree exp = SYMBOL_REF_DECL (x);
3232 if (!TREE_ASM_WRITTEN (exp))
3234 n_deferred_constants--;
3235 output_constant_def_contents (x);
3239 return -1;
3242 /* Look through appropriate parts of INSN, marking all entries in the
3243 constant pool which are actually being used. Entries that are only
3244 referenced by other constants are also marked as used. Emit
3245 deferred strings that are used. */
3247 static void
3248 mark_constants (struct rtx_constant_pool *pool, rtx insn)
3250 if (!INSN_P (insn))
3251 return;
3253 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3254 insns, not any notes that may be attached. We don't want to mark
3255 a constant just because it happens to appear in a REG_EQUIV note. */
3256 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3258 rtx seq = PATTERN (insn);
3259 int i, n = XVECLEN (seq, 0);
3260 for (i = 0; i < n; ++i)
3262 rtx subinsn = XVECEXP (seq, 0, i);
3263 if (INSN_P (subinsn))
3264 for_each_rtx (&PATTERN (subinsn), mark_constant, pool);
3267 else
3268 for_each_rtx (&PATTERN (insn), mark_constant, pool);
3271 /* Look through the instructions for this function, and mark all the
3272 entries in POOL which are actually being used. Emit deferred constants
3273 which have indeed been used. */
3275 static void
3276 mark_constant_pool (struct rtx_constant_pool *pool)
3278 rtx insn, link;
3280 if (pool->first == 0 && n_deferred_constants == 0)
3281 return;
3283 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3284 mark_constants (pool, insn);
3286 for (link = current_function_epilogue_delay_list;
3287 link;
3288 link = XEXP (link, 1))
3289 mark_constants (pool, XEXP (link, 0));
3292 /* Write all the constants in the constant pool. */
3294 void
3295 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3296 tree fndecl ATTRIBUTE_UNUSED)
3298 struct rtx_constant_pool *pool = cfun->varasm->pool;
3299 struct constant_descriptor_rtx *desc;
3301 /* It is possible for gcc to call force_const_mem and then to later
3302 discard the instructions which refer to the constant. In such a
3303 case we do not need to output the constant. */
3304 mark_constant_pool (pool);
3306 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3307 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3308 #endif
3310 for (desc = pool->first; desc ; desc = desc->next)
3311 output_constant_pool_1 (desc);
3313 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3314 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3315 #endif
3318 /* Determine what kind of relocations EXP may need. */
3321 compute_reloc_for_constant (tree exp)
3323 int reloc = 0, reloc2;
3324 tree tem;
3326 /* Give the front-end a chance to convert VALUE to something that
3327 looks more like a constant to the back-end. */
3328 exp = lang_hooks.expand_constant (exp);
3330 switch (TREE_CODE (exp))
3332 case ADDR_EXPR:
3333 case FDESC_EXPR:
3334 /* Go inside any operations that get_inner_reference can handle and see
3335 if what's inside is a constant: no need to do anything here for
3336 addresses of variables or functions. */
3337 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3338 tem = TREE_OPERAND (tem, 0))
3341 if (TREE_PUBLIC (tem))
3342 reloc |= 2;
3343 else
3344 reloc |= 1;
3345 break;
3347 case PLUS_EXPR:
3348 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3349 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3350 break;
3352 case MINUS_EXPR:
3353 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3354 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3355 /* The difference of two local labels is computable at link time. */
3356 if (reloc == 1 && reloc2 == 1)
3357 reloc = 0;
3358 else
3359 reloc |= reloc2;
3360 break;
3362 case NOP_EXPR:
3363 case CONVERT_EXPR:
3364 case NON_LVALUE_EXPR:
3365 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3366 break;
3368 case CONSTRUCTOR:
3369 for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3370 if (TREE_VALUE (tem) != 0)
3371 reloc |= compute_reloc_for_constant (TREE_VALUE (tem));
3373 break;
3375 default:
3376 break;
3378 return reloc;
3381 /* Find all the constants whose addresses are referenced inside of EXP,
3382 and make sure assembler code with a label has been output for each one.
3383 Indicate whether an ADDR_EXPR has been encountered. */
3385 static void
3386 output_addressed_constants (tree exp)
3388 tree tem;
3390 /* Give the front-end a chance to convert VALUE to something that
3391 looks more like a constant to the back-end. */
3392 exp = lang_hooks.expand_constant (exp);
3394 switch (TREE_CODE (exp))
3396 case ADDR_EXPR:
3397 case FDESC_EXPR:
3398 /* Go inside any operations that get_inner_reference can handle and see
3399 if what's inside is a constant: no need to do anything here for
3400 addresses of variables or functions. */
3401 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3402 tem = TREE_OPERAND (tem, 0))
3405 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3406 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3407 tem = DECL_INITIAL (tem);
3409 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3410 output_constant_def (tem, 0);
3411 break;
3413 case PLUS_EXPR:
3414 case MINUS_EXPR:
3415 output_addressed_constants (TREE_OPERAND (exp, 1));
3416 /* Fall through. */
3418 case NOP_EXPR:
3419 case CONVERT_EXPR:
3420 case NON_LVALUE_EXPR:
3421 output_addressed_constants (TREE_OPERAND (exp, 0));
3422 break;
3424 case CONSTRUCTOR:
3425 for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3426 if (TREE_VALUE (tem) != 0)
3427 output_addressed_constants (TREE_VALUE (tem));
3429 break;
3431 default:
3432 break;
3436 /* Return nonzero if VALUE is a valid constant-valued expression
3437 for use in initializing a static variable; one that can be an
3438 element of a "constant" initializer.
3440 Return null_pointer_node if the value is absolute;
3441 if it is relocatable, return the variable that determines the relocation.
3442 We assume that VALUE has been folded as much as possible;
3443 therefore, we do not need to check for such things as
3444 arithmetic-combinations of integers. */
3446 tree
3447 initializer_constant_valid_p (tree value, tree endtype)
3449 /* Give the front-end a chance to convert VALUE to something that
3450 looks more like a constant to the back-end. */
3451 value = lang_hooks.expand_constant (value);
3453 switch (TREE_CODE (value))
3455 case CONSTRUCTOR:
3456 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3457 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3458 && TREE_CONSTANT (value)
3459 && CONSTRUCTOR_ELTS (value))
3461 tree elt;
3462 bool absolute = true;
3464 for (elt = CONSTRUCTOR_ELTS (value); elt; elt = TREE_CHAIN (elt))
3466 tree reloc;
3467 value = TREE_VALUE (elt);
3468 reloc = initializer_constant_valid_p (value, TREE_TYPE (value));
3469 if (!reloc)
3470 return NULL_TREE;
3471 if (reloc != null_pointer_node)
3472 absolute = false;
3474 /* For a non-absolute relocation, there is no single
3475 variable that can be "the variable that determines the
3476 relocation." */
3477 return absolute ? null_pointer_node : error_mark_node;
3480 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3482 case INTEGER_CST:
3483 case VECTOR_CST:
3484 case REAL_CST:
3485 case STRING_CST:
3486 case COMPLEX_CST:
3487 return null_pointer_node;
3489 case ADDR_EXPR:
3490 case FDESC_EXPR:
3491 value = staticp (TREE_OPERAND (value, 0));
3492 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out to
3493 be a constant, this is old-skool offsetof-like nonsense. */
3494 if (value
3495 && TREE_CODE (value) == INDIRECT_REF
3496 && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3497 return null_pointer_node;
3498 /* Taking the address of a nested function involves a trampoline. */
3499 if (value
3500 && TREE_CODE (value) == FUNCTION_DECL
3501 && ((decl_function_context (value) && !DECL_NO_STATIC_CHAIN (value))
3502 || DECL_NON_ADDR_CONST_P (value)))
3503 return NULL_TREE;
3504 return value;
3506 case VIEW_CONVERT_EXPR:
3507 case NON_LVALUE_EXPR:
3508 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3510 case CONVERT_EXPR:
3511 case NOP_EXPR:
3513 tree src;
3514 tree src_type;
3515 tree dest_type;
3517 src = TREE_OPERAND (value, 0);
3518 src_type = TREE_TYPE (src);
3519 dest_type = TREE_TYPE (value);
3521 /* Allow conversions between pointer types, floating-point
3522 types, and offset types. */
3523 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3524 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3525 || (TREE_CODE (dest_type) == OFFSET_TYPE
3526 && TREE_CODE (src_type) == OFFSET_TYPE))
3527 return initializer_constant_valid_p (src, endtype);
3529 /* Allow length-preserving conversions between integer types. */
3530 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3531 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3532 return initializer_constant_valid_p (src, endtype);
3534 /* Allow conversions between other integer types only if
3535 explicit value. */
3536 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3538 tree inner = initializer_constant_valid_p (src, endtype);
3539 if (inner == null_pointer_node)
3540 return null_pointer_node;
3541 break;
3544 /* Allow (int) &foo provided int is as wide as a pointer. */
3545 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3546 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3547 return initializer_constant_valid_p (src, endtype);
3549 /* Likewise conversions from int to pointers, but also allow
3550 conversions from 0. */
3551 if ((POINTER_TYPE_P (dest_type)
3552 || TREE_CODE (dest_type) == OFFSET_TYPE)
3553 && INTEGRAL_TYPE_P (src_type))
3555 if (integer_zerop (src))
3556 return null_pointer_node;
3557 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3558 return initializer_constant_valid_p (src, endtype);
3561 /* Allow conversions to struct or union types if the value
3562 inside is okay. */
3563 if (TREE_CODE (dest_type) == RECORD_TYPE
3564 || TREE_CODE (dest_type) == UNION_TYPE)
3565 return initializer_constant_valid_p (src, endtype);
3567 break;
3569 case PLUS_EXPR:
3570 if (! INTEGRAL_TYPE_P (endtype)
3571 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3573 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3574 endtype);
3575 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3576 endtype);
3577 /* If either term is absolute, use the other terms relocation. */
3578 if (valid0 == null_pointer_node)
3579 return valid1;
3580 if (valid1 == null_pointer_node)
3581 return valid0;
3583 break;
3585 case MINUS_EXPR:
3586 if (! INTEGRAL_TYPE_P (endtype)
3587 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3589 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3590 endtype);
3591 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3592 endtype);
3593 /* Win if second argument is absolute. */
3594 if (valid1 == null_pointer_node)
3595 return valid0;
3596 /* Win if both arguments have the same relocation.
3597 Then the value is absolute. */
3598 if (valid0 == valid1 && valid0 != 0)
3599 return null_pointer_node;
3601 /* Since GCC guarantees that string constants are unique in the
3602 generated code, a subtraction between two copies of the same
3603 constant string is absolute. */
3604 if (valid0 && TREE_CODE (valid0) == STRING_CST
3605 && valid1 && TREE_CODE (valid1) == STRING_CST
3606 && operand_equal_p (valid0, valid1, 1))
3607 return null_pointer_node;
3610 /* Support narrowing differences. */
3611 if (INTEGRAL_TYPE_P (endtype))
3613 tree op0, op1;
3615 op0 = TREE_OPERAND (value, 0);
3616 op1 = TREE_OPERAND (value, 1);
3618 /* Like STRIP_NOPS except allow the operand mode to widen.
3619 This works around a feature of fold that simplifies
3620 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3621 that the narrower operation is cheaper. */
3623 while (TREE_CODE (op0) == NOP_EXPR
3624 || TREE_CODE (op0) == CONVERT_EXPR
3625 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3627 tree inner = TREE_OPERAND (op0, 0);
3628 if (inner == error_mark_node
3629 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3630 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3631 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3632 break;
3633 op0 = inner;
3636 while (TREE_CODE (op1) == NOP_EXPR
3637 || TREE_CODE (op1) == CONVERT_EXPR
3638 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3640 tree inner = TREE_OPERAND (op1, 0);
3641 if (inner == error_mark_node
3642 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3643 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3644 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3645 break;
3646 op1 = inner;
3649 op0 = initializer_constant_valid_p (op0, endtype);
3650 op1 = initializer_constant_valid_p (op1, endtype);
3652 /* Both initializers must be known. */
3653 if (op0 && op1)
3655 if (op0 == op1)
3656 return null_pointer_node;
3658 /* Support differences between labels. */
3659 if (TREE_CODE (op0) == LABEL_DECL
3660 && TREE_CODE (op1) == LABEL_DECL)
3661 return null_pointer_node;
3663 if (TREE_CODE (op0) == STRING_CST
3664 && TREE_CODE (op1) == STRING_CST
3665 && operand_equal_p (op0, op1, 1))
3666 return null_pointer_node;
3669 break;
3671 default:
3672 break;
3675 return 0;
3678 /* Output assembler code for constant EXP to FILE, with no label.
3679 This includes the pseudo-op such as ".int" or ".byte", and a newline.
3680 Assumes output_addressed_constants has been done on EXP already.
3682 Generate exactly SIZE bytes of assembler data, padding at the end
3683 with zeros if necessary. SIZE must always be specified.
3685 SIZE is important for structure constructors,
3686 since trailing members may have been omitted from the constructor.
3687 It is also important for initialization of arrays from string constants
3688 since the full length of the string constant might not be wanted.
3689 It is also needed for initialization of unions, where the initializer's
3690 type is just one member, and that may not be as long as the union.
3692 There a case in which we would fail to output exactly SIZE bytes:
3693 for a structure constructor that wants to produce more than SIZE bytes.
3694 But such constructors will never be generated for any possible input.
3696 ALIGN is the alignment of the data in bits. */
3698 void
3699 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3701 enum tree_code code;
3702 unsigned HOST_WIDE_INT thissize;
3704 /* Some front-ends use constants other than the standard language-independent
3705 varieties, but which may still be output directly. Give the front-end a
3706 chance to convert EXP to a language-independent representation. */
3707 exp = lang_hooks.expand_constant (exp);
3709 if (size == 0 || flag_syntax_only)
3710 return;
3712 /* Eliminate any conversions since we'll be outputting the underlying
3713 constant. */
3714 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
3715 || TREE_CODE (exp) == NON_LVALUE_EXPR
3716 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
3717 exp = TREE_OPERAND (exp, 0);
3719 code = TREE_CODE (TREE_TYPE (exp));
3720 thissize = int_size_in_bytes (TREE_TYPE (exp));
3722 /* Allow a constructor with no elements for any data type.
3723 This means to fill the space with zeros. */
3724 if (TREE_CODE (exp) == CONSTRUCTOR && CONSTRUCTOR_ELTS (exp) == 0)
3726 assemble_zeros (size);
3727 return;
3730 if (TREE_CODE (exp) == FDESC_EXPR)
3732 #ifdef ASM_OUTPUT_FDESC
3733 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
3734 tree decl = TREE_OPERAND (exp, 0);
3735 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
3736 #else
3737 gcc_unreachable ();
3738 #endif
3739 return;
3742 /* Now output the underlying data. If we've handling the padding, return.
3743 Otherwise, break and ensure SIZE is the size written. */
3744 switch (code)
3746 case CHAR_TYPE:
3747 case BOOLEAN_TYPE:
3748 case INTEGER_TYPE:
3749 case ENUMERAL_TYPE:
3750 case POINTER_TYPE:
3751 case REFERENCE_TYPE:
3752 case OFFSET_TYPE:
3753 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3754 EXPAND_INITIALIZER),
3755 MIN (size, thissize), align, 0))
3756 error ("initializer for integer value is too complicated");
3757 break;
3759 case REAL_TYPE:
3760 if (TREE_CODE (exp) != REAL_CST)
3761 error ("initializer for floating value is not a floating constant");
3763 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3764 break;
3766 case COMPLEX_TYPE:
3767 output_constant (TREE_REALPART (exp), thissize / 2, align);
3768 output_constant (TREE_IMAGPART (exp), thissize / 2,
3769 min_align (align, BITS_PER_UNIT * (thissize / 2)));
3770 break;
3772 case ARRAY_TYPE:
3773 case VECTOR_TYPE:
3774 switch (TREE_CODE (exp))
3776 case CONSTRUCTOR:
3777 output_constructor (exp, size, align);
3778 return;
3779 case STRING_CST:
3780 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3781 size);
3782 assemble_string (TREE_STRING_POINTER (exp), thissize);
3783 break;
3785 case VECTOR_CST:
3787 int elt_size;
3788 tree link;
3789 unsigned int nalign;
3790 enum machine_mode inner;
3792 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
3793 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3795 elt_size = GET_MODE_SIZE (inner);
3797 link = TREE_VECTOR_CST_ELTS (exp);
3798 output_constant (TREE_VALUE (link), elt_size, align);
3799 while ((link = TREE_CHAIN (link)) != NULL)
3800 output_constant (TREE_VALUE (link), elt_size, nalign);
3801 break;
3803 default:
3804 gcc_unreachable ();
3806 break;
3808 case RECORD_TYPE:
3809 case UNION_TYPE:
3810 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
3811 output_constructor (exp, size, align);
3812 return;
3814 case ERROR_MARK:
3815 return;
3817 default:
3818 gcc_unreachable ();
3821 if (size > thissize)
3822 assemble_zeros (size - thissize);
3826 /* Subroutine of output_constructor, used for computing the size of
3827 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
3828 type with an unspecified upper bound. */
3830 static unsigned HOST_WIDE_INT
3831 array_size_for_constructor (tree val)
3833 tree max_index, i;
3835 /* This code used to attempt to handle string constants that are not
3836 arrays of single-bytes, but nothing else does, so there's no point in
3837 doing it here. */
3838 if (TREE_CODE (val) == STRING_CST)
3839 return TREE_STRING_LENGTH (val);
3841 max_index = NULL_TREE;
3842 for (i = CONSTRUCTOR_ELTS (val); i; i = TREE_CHAIN (i))
3844 tree index = TREE_PURPOSE (i);
3846 if (TREE_CODE (index) == RANGE_EXPR)
3847 index = TREE_OPERAND (index, 1);
3848 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
3849 max_index = index;
3852 if (max_index == NULL_TREE)
3853 return 0;
3855 /* Compute the total number of array elements. */
3856 i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
3857 convert (sizetype,
3858 TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
3859 i = size_binop (PLUS_EXPR, i, convert (sizetype, integer_one_node));
3861 /* Multiply by the array element unit size to find number of bytes. */
3862 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
3864 return tree_low_cst (i, 1);
3867 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
3868 Generate at least SIZE bytes, padding if necessary. */
3870 static void
3871 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
3872 unsigned int align)
3874 tree type = TREE_TYPE (exp);
3875 tree link, field = 0;
3876 tree min_index = 0;
3877 /* Number of bytes output or skipped so far.
3878 In other words, current position within the constructor. */
3879 HOST_WIDE_INT total_bytes = 0;
3880 /* Nonzero means BYTE contains part of a byte, to be output. */
3881 int byte_buffer_in_use = 0;
3882 int byte = 0;
3884 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
3886 if (TREE_CODE (type) == RECORD_TYPE)
3887 field = TYPE_FIELDS (type);
3889 if (TREE_CODE (type) == ARRAY_TYPE
3890 && TYPE_DOMAIN (type) != 0)
3891 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3893 /* As LINK goes through the elements of the constant,
3894 FIELD goes through the structure fields, if the constant is a structure.
3895 if the constant is a union, then we override this,
3896 by getting the field from the TREE_LIST element.
3897 But the constant could also be an array. Then FIELD is zero.
3899 There is always a maximum of one element in the chain LINK for unions
3900 (even if the initializer in a source program incorrectly contains
3901 more one). */
3902 for (link = CONSTRUCTOR_ELTS (exp);
3903 link;
3904 link = TREE_CHAIN (link),
3905 field = field ? TREE_CHAIN (field) : 0)
3907 tree val = TREE_VALUE (link);
3908 tree index = 0;
3910 /* The element in a union constructor specifies the proper field
3911 or index. */
3912 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
3913 || TREE_CODE (type) == QUAL_UNION_TYPE)
3914 && TREE_PURPOSE (link) != 0)
3915 field = TREE_PURPOSE (link);
3917 else if (TREE_CODE (type) == ARRAY_TYPE)
3918 index = TREE_PURPOSE (link);
3920 #ifdef ASM_COMMENT_START
3921 if (field && flag_verbose_asm)
3922 fprintf (asm_out_file, "%s %s:\n",
3923 ASM_COMMENT_START,
3924 DECL_NAME (field)
3925 ? IDENTIFIER_POINTER (DECL_NAME (field))
3926 : "<anonymous>");
3927 #endif
3929 /* Eliminate the marker that makes a cast not be an lvalue. */
3930 if (val != 0)
3931 STRIP_NOPS (val);
3933 if (index && TREE_CODE (index) == RANGE_EXPR)
3935 unsigned HOST_WIDE_INT fieldsize
3936 = int_size_in_bytes (TREE_TYPE (type));
3937 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
3938 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
3939 HOST_WIDE_INT index;
3940 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
3942 for (index = lo_index; index <= hi_index; index++)
3944 /* Output the element's initial value. */
3945 if (val == 0)
3946 assemble_zeros (fieldsize);
3947 else
3948 output_constant (val, fieldsize, align2);
3950 /* Count its size. */
3951 total_bytes += fieldsize;
3954 else if (field == 0 || !DECL_BIT_FIELD (field))
3956 /* An element that is not a bit-field. */
3958 unsigned HOST_WIDE_INT fieldsize;
3959 /* Since this structure is static,
3960 we know the positions are constant. */
3961 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
3962 unsigned int align2;
3964 if (index != 0)
3965 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
3966 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
3968 /* Output any buffered-up bit-fields preceding this element. */
3969 if (byte_buffer_in_use)
3971 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3972 total_bytes++;
3973 byte_buffer_in_use = 0;
3976 /* Advance to offset of this element.
3977 Note no alignment needed in an array, since that is guaranteed
3978 if each element has the proper size. */
3979 if ((field != 0 || index != 0) && pos != total_bytes)
3981 assemble_zeros (pos - total_bytes);
3982 total_bytes = pos;
3985 /* Find the alignment of this element. */
3986 align2 = min_align (align, BITS_PER_UNIT * pos);
3988 /* Determine size this element should occupy. */
3989 if (field)
3991 fieldsize = 0;
3993 /* If this is an array with an unspecified upper bound,
3994 the initializer determines the size. */
3995 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
3996 but we cannot do this until the deprecated support for
3997 initializing zero-length array members is removed. */
3998 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
3999 && TYPE_DOMAIN (TREE_TYPE (field))
4000 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4002 fieldsize = array_size_for_constructor (val);
4003 /* Given a non-empty initialization, this field had
4004 better be last. */
4005 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4007 else if (DECL_SIZE_UNIT (field))
4009 /* ??? This can't be right. If the decl size overflows
4010 a host integer we will silently emit no data. */
4011 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4012 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4015 else
4016 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4018 /* Output the element's initial value. */
4019 if (val == 0)
4020 assemble_zeros (fieldsize);
4021 else
4022 output_constant (val, fieldsize, align2);
4024 /* Count its size. */
4025 total_bytes += fieldsize;
4027 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4028 error ("invalid initial value for member %qs",
4029 IDENTIFIER_POINTER (DECL_NAME (field)));
4030 else
4032 /* Element that is a bit-field. */
4034 HOST_WIDE_INT next_offset = int_bit_position (field);
4035 HOST_WIDE_INT end_offset
4036 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4038 if (val == 0)
4039 val = integer_zero_node;
4041 /* If this field does not start in this (or, next) byte,
4042 skip some bytes. */
4043 if (next_offset / BITS_PER_UNIT != total_bytes)
4045 /* Output remnant of any bit field in previous bytes. */
4046 if (byte_buffer_in_use)
4048 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4049 total_bytes++;
4050 byte_buffer_in_use = 0;
4053 /* If still not at proper byte, advance to there. */
4054 if (next_offset / BITS_PER_UNIT != total_bytes)
4056 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4057 total_bytes = next_offset / BITS_PER_UNIT;
4061 if (! byte_buffer_in_use)
4062 byte = 0;
4064 /* We must split the element into pieces that fall within
4065 separate bytes, and combine each byte with previous or
4066 following bit-fields. */
4068 /* next_offset is the offset n fbits from the beginning of
4069 the structure to the next bit of this element to be processed.
4070 end_offset is the offset of the first bit past the end of
4071 this element. */
4072 while (next_offset < end_offset)
4074 int this_time;
4075 int shift;
4076 HOST_WIDE_INT value;
4077 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4078 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4080 /* Advance from byte to byte
4081 within this element when necessary. */
4082 while (next_byte != total_bytes)
4084 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4085 total_bytes++;
4086 byte = 0;
4089 /* Number of bits we can process at once
4090 (all part of the same byte). */
4091 this_time = MIN (end_offset - next_offset,
4092 BITS_PER_UNIT - next_bit);
4093 if (BYTES_BIG_ENDIAN)
4095 /* On big-endian machine, take the most significant bits
4096 first (of the bits that are significant)
4097 and put them into bytes from the most significant end. */
4098 shift = end_offset - next_offset - this_time;
4100 /* Don't try to take a bunch of bits that cross
4101 the word boundary in the INTEGER_CST. We can
4102 only select bits from the LOW or HIGH part
4103 not from both. */
4104 if (shift < HOST_BITS_PER_WIDE_INT
4105 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4107 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4108 shift = HOST_BITS_PER_WIDE_INT;
4111 /* Now get the bits from the appropriate constant word. */
4112 if (shift < HOST_BITS_PER_WIDE_INT)
4113 value = TREE_INT_CST_LOW (val);
4114 else
4116 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4117 value = TREE_INT_CST_HIGH (val);
4118 shift -= HOST_BITS_PER_WIDE_INT;
4121 /* Get the result. This works only when:
4122 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4123 byte |= (((value >> shift)
4124 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4125 << (BITS_PER_UNIT - this_time - next_bit));
4127 else
4129 /* On little-endian machines,
4130 take first the least significant bits of the value
4131 and pack them starting at the least significant
4132 bits of the bytes. */
4133 shift = next_offset - int_bit_position (field);
4135 /* Don't try to take a bunch of bits that cross
4136 the word boundary in the INTEGER_CST. We can
4137 only select bits from the LOW or HIGH part
4138 not from both. */
4139 if (shift < HOST_BITS_PER_WIDE_INT
4140 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4141 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4143 /* Now get the bits from the appropriate constant word. */
4144 if (shift < HOST_BITS_PER_WIDE_INT)
4145 value = TREE_INT_CST_LOW (val);
4146 else
4148 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4149 value = TREE_INT_CST_HIGH (val);
4150 shift -= HOST_BITS_PER_WIDE_INT;
4153 /* Get the result. This works only when:
4154 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4155 byte |= (((value >> shift)
4156 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4157 << next_bit);
4160 next_offset += this_time;
4161 byte_buffer_in_use = 1;
4166 if (byte_buffer_in_use)
4168 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4169 total_bytes++;
4172 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4173 assemble_zeros (size - total_bytes);
4176 /* This TREE_LIST contains any weak symbol declarations waiting
4177 to be emitted. */
4178 static GTY(()) tree weak_decls;
4180 /* Mark DECL as weak. */
4182 static void
4183 mark_weak (tree decl)
4185 DECL_WEAK (decl) = 1;
4187 if (DECL_RTL_SET_P (decl)
4188 && MEM_P (DECL_RTL (decl))
4189 && XEXP (DECL_RTL (decl), 0)
4190 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4191 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4194 /* Merge weak status between NEWDECL and OLDDECL. */
4196 void
4197 merge_weak (tree newdecl, tree olddecl)
4199 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4200 return;
4202 if (DECL_WEAK (newdecl))
4204 tree wd;
4206 /* NEWDECL is weak, but OLDDECL is not. */
4208 /* If we already output the OLDDECL, we're in trouble; we can't
4209 go back and make it weak. This error cannot caught in
4210 declare_weak because the NEWDECL and OLDDECL was not yet
4211 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4212 if (TREE_ASM_WRITTEN (olddecl))
4213 error ("%Jweak declaration of %qD must precede definition",
4214 newdecl, newdecl);
4216 /* If we've already generated rtl referencing OLDDECL, we may
4217 have done so in a way that will not function properly with
4218 a weak symbol. */
4219 else if (TREE_USED (olddecl)
4220 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4221 warning ("%Jweak declaration of %qD after first use results "
4222 "in unspecified behavior", newdecl, newdecl);
4224 if (SUPPORTS_WEAK)
4226 /* We put the NEWDECL on the weak_decls list at some point.
4227 Replace it with the OLDDECL. */
4228 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4229 if (TREE_VALUE (wd) == newdecl)
4231 TREE_VALUE (wd) = olddecl;
4232 break;
4234 /* We may not find the entry on the list. If NEWDECL is a
4235 weak alias, then we will have already called
4236 globalize_decl to remove the entry; in that case, we do
4237 not need to do anything. */
4240 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4241 mark_weak (olddecl);
4243 else
4244 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4245 weak. Just update NEWDECL to indicate that it's weak too. */
4246 mark_weak (newdecl);
4249 /* Declare DECL to be a weak symbol. */
4251 void
4252 declare_weak (tree decl)
4254 if (! TREE_PUBLIC (decl))
4255 error ("%Jweak declaration of %qD must be public", decl, decl);
4256 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4257 error ("%Jweak declaration of %qD must precede definition", decl, decl);
4258 else if (SUPPORTS_WEAK)
4260 if (! DECL_WEAK (decl))
4261 weak_decls = tree_cons (NULL, decl, weak_decls);
4263 else
4264 warning ("%Jweak declaration of %qD not supported", decl, decl);
4266 mark_weak (decl);
4269 /* Emit any pending weak declarations. */
4271 void
4272 weak_finish (void)
4274 tree t;
4276 for (t = weak_decls; t; t = TREE_CHAIN (t))
4278 tree decl = TREE_VALUE (t);
4279 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4280 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4281 #endif
4283 if (! TREE_USED (decl))
4284 continue;
4286 #ifdef ASM_WEAKEN_DECL
4287 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4288 #else
4289 #ifdef ASM_WEAKEN_LABEL
4290 ASM_WEAKEN_LABEL (asm_out_file, name);
4291 #else
4292 #ifdef ASM_OUTPUT_WEAK_ALIAS
4293 warning ("only weak aliases are supported in this configuration");
4294 return;
4295 #endif
4296 #endif
4297 #endif
4301 /* Emit the assembly bits to indicate that DECL is globally visible. */
4303 static void
4304 globalize_decl (tree decl)
4306 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4308 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4309 if (DECL_WEAK (decl))
4311 tree *p, t;
4313 #ifdef ASM_WEAKEN_DECL
4314 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4315 #else
4316 ASM_WEAKEN_LABEL (asm_out_file, name);
4317 #endif
4319 /* Remove this function from the pending weak list so that
4320 we do not emit multiple .weak directives for it. */
4321 for (p = &weak_decls; (t = *p) ; )
4323 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4324 *p = TREE_CHAIN (t);
4325 else
4326 p = &TREE_CHAIN (t);
4328 return;
4330 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4331 if (DECL_ONE_ONLY (decl))
4332 ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4333 #endif
4335 targetm.asm_out.globalize_label (asm_out_file, name);
4338 /* We have to be able to tell cgraph about the needed-ness of the target
4339 of an alias. This requires that the decl have been defined. Aliases
4340 that preceed their definition have to be queued for later processing. */
4342 struct alias_pair GTY(())
4344 tree decl;
4345 tree target;
4347 typedef struct alias_pair *alias_pair;
4349 /* Define gc'd vector type. */
4350 DEF_VEC_GC_P(alias_pair);
4352 static GTY(()) VEC(alias_pair) *alias_pairs;
4354 /* Given an assembly name, find the decl it is associated with. At the
4355 same time, mark it needed for cgraph. */
4357 static tree
4358 find_decl_and_mark_needed (tree decl, tree target)
4360 struct cgraph_node *fnode = NULL;
4361 struct cgraph_varpool_node *vnode = NULL;
4363 if (TREE_CODE (decl) == FUNCTION_DECL)
4365 fnode = cgraph_node_for_asm (target);
4366 if (fnode == NULL)
4367 vnode = cgraph_varpool_node_for_asm (target);
4369 else
4371 vnode = cgraph_varpool_node_for_asm (target);
4372 if (vnode == NULL)
4373 fnode = cgraph_node_for_asm (target);
4376 if (fnode)
4378 cgraph_mark_needed_node (fnode);
4379 return fnode->decl;
4381 else if (vnode)
4383 cgraph_varpool_mark_needed_node (vnode);
4384 return vnode->decl;
4386 else
4387 return NULL_TREE;
4390 static void
4391 do_assemble_alias (tree decl, tree target)
4393 TREE_ASM_WRITTEN (decl) = 1;
4394 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4396 #ifdef ASM_OUTPUT_DEF
4397 /* Make name accessible from other files, if appropriate. */
4399 if (TREE_PUBLIC (decl))
4401 globalize_decl (decl);
4402 maybe_assemble_visibility (decl);
4405 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4406 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4407 # else
4408 ASM_OUTPUT_DEF (asm_out_file,
4409 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4410 IDENTIFIER_POINTER (target));
4411 # endif
4412 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4414 const char *name;
4415 tree *p, t;
4417 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4418 # ifdef ASM_WEAKEN_DECL
4419 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4420 # else
4421 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4422 # endif
4423 /* Remove this function from the pending weak list so that
4424 we do not emit multiple .weak directives for it. */
4425 for (p = &weak_decls; (t = *p) ; )
4426 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4427 *p = TREE_CHAIN (t);
4428 else
4429 p = &TREE_CHAIN (t);
4431 #endif
4434 /* First pass of completing pending aliases. Make sure that cgraph knows
4435 which symbols will be required. */
4437 void
4438 finish_aliases_1 (void)
4440 unsigned i;
4441 alias_pair p;
4443 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4445 tree target_decl;
4447 target_decl = find_decl_and_mark_needed (p->decl, p->target);
4448 if (target_decl == NULL)
4449 error ("%J%qD aliased to undefined symbol %qE",
4450 p->decl, p->decl, p->target);
4451 else if (DECL_EXTERNAL (target_decl))
4452 error ("%J%qD aliased to external symbol %qE",
4453 p->decl, p->decl, p->target);
4457 /* Second pass of completing pending aliases. Emit the actual assembly.
4458 This happens at the end of compilation and thus it is assured that the
4459 target symbol has been emitted. */
4461 void
4462 finish_aliases_2 (void)
4464 unsigned i;
4465 alias_pair p;
4467 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4468 do_assemble_alias (p->decl, p->target);
4470 alias_pairs = NULL;
4473 /* Emit an assembler directive to make the symbol for DECL an alias to
4474 the symbol for TARGET. */
4476 void
4477 assemble_alias (tree decl, tree target)
4479 tree target_decl;
4481 #if !defined (ASM_OUTPUT_DEF)
4482 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4483 error ("%Jalias definitions not supported in this configuration", decl);
4484 return;
4485 # else
4486 if (!DECL_WEAK (decl))
4488 error ("%Jonly weak aliases are supported in this configuration", decl);
4489 return;
4491 # endif
4492 #endif
4494 /* We must force creation of DECL_RTL for debug info generation, even though
4495 we don't use it here. */
4496 make_decl_rtl (decl);
4497 TREE_USED (decl) = 1;
4499 /* A quirk of the initial implementation of aliases required that the user
4500 add "extern" to all of them. Which is silly, but now historical. Do
4501 note that the symbol is in fact locally defined. */
4502 DECL_EXTERNAL (decl) = 0;
4504 /* Allow aliases to aliases. */
4505 if (TREE_CODE (decl) == FUNCTION_DECL)
4506 cgraph_node (decl);
4507 else
4508 cgraph_varpool_node (decl);
4510 /* If the target has already been emitted, we don't have to queue the
4511 alias. This saves a tad o memory. */
4512 target_decl = find_decl_and_mark_needed (decl, target);
4513 if (target_decl && TREE_ASM_WRITTEN (target_decl))
4514 do_assemble_alias (decl, target);
4515 else
4517 alias_pair p;
4519 p = ggc_alloc (sizeof (struct alias_pair));
4520 p->decl = decl;
4521 p->target = target;
4522 VEC_safe_push (alias_pair, alias_pairs, p);
4526 /* Emit an assembler directive to set symbol for DECL visibility to
4527 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
4529 void
4530 default_assemble_visibility (tree decl, int vis)
4532 static const char * const visibility_types[] = {
4533 NULL, "internal", "hidden", "protected"
4536 const char *name, *type;
4538 name = (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
4539 type = visibility_types[vis];
4541 #ifdef HAVE_GAS_HIDDEN
4542 fprintf (asm_out_file, "\t.%s\t", type);
4543 assemble_name (asm_out_file, name);
4544 fprintf (asm_out_file, "\n");
4545 #else
4546 warning ("visibility attribute not supported in this configuration; ignored");
4547 #endif
4550 /* A helper function to call assemble_visibility when needed for a decl. */
4552 static void
4553 maybe_assemble_visibility (tree decl)
4555 enum symbol_visibility vis = DECL_VISIBILITY (decl);
4557 if (vis != VISIBILITY_DEFAULT)
4558 targetm.asm_out.visibility (decl, vis);
4561 /* Returns 1 if the target configuration supports defining public symbols
4562 so that one of them will be chosen at link time instead of generating a
4563 multiply-defined symbol error, whether through the use of weak symbols or
4564 a target-specific mechanism for having duplicates discarded. */
4567 supports_one_only (void)
4569 if (SUPPORTS_ONE_ONLY)
4570 return 1;
4571 return SUPPORTS_WEAK;
4574 /* Set up DECL as a public symbol that can be defined in multiple
4575 translation units without generating a linker error. */
4577 void
4578 make_decl_one_only (tree decl)
4580 gcc_assert (TREE_CODE (decl) == VAR_DECL
4581 || TREE_CODE (decl) == FUNCTION_DECL);
4583 TREE_PUBLIC (decl) = 1;
4585 if (SUPPORTS_ONE_ONLY)
4587 #ifdef MAKE_DECL_ONE_ONLY
4588 MAKE_DECL_ONE_ONLY (decl);
4589 #endif
4590 DECL_ONE_ONLY (decl) = 1;
4592 else if (TREE_CODE (decl) == VAR_DECL
4593 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4594 DECL_COMMON (decl) = 1;
4595 else
4597 gcc_assert (SUPPORTS_WEAK);
4598 DECL_WEAK (decl) = 1;
4602 void
4603 init_varasm_once (void)
4605 in_named_htab = htab_create_ggc (31, in_named_entry_hash,
4606 in_named_entry_eq, NULL);
4607 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4608 const_desc_eq, NULL);
4610 const_alias_set = new_alias_set ();
4613 static enum tls_model
4614 decl_tls_model (tree decl)
4616 enum tls_model kind;
4617 tree attr = lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl));
4618 bool is_local;
4620 if (attr)
4622 attr = TREE_VALUE (TREE_VALUE (attr));
4623 gcc_assert (TREE_CODE (attr) == STRING_CST);
4625 if (!strcmp (TREE_STRING_POINTER (attr), "local-exec"))
4626 kind = TLS_MODEL_LOCAL_EXEC;
4627 else if (!strcmp (TREE_STRING_POINTER (attr), "initial-exec"))
4628 kind = TLS_MODEL_INITIAL_EXEC;
4629 else if (!strcmp (TREE_STRING_POINTER (attr), "local-dynamic"))
4630 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
4631 else if (!strcmp (TREE_STRING_POINTER (attr), "global-dynamic"))
4632 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4633 else
4634 gcc_unreachable ();
4635 return kind;
4638 is_local = targetm.binds_local_p (decl);
4639 if (!flag_shlib)
4641 if (is_local)
4642 kind = TLS_MODEL_LOCAL_EXEC;
4643 else
4644 kind = TLS_MODEL_INITIAL_EXEC;
4646 /* Local dynamic is inefficient when we're not combining the
4647 parts of the address. */
4648 else if (optimize && is_local)
4649 kind = TLS_MODEL_LOCAL_DYNAMIC;
4650 else
4651 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4652 if (kind < flag_tls_default)
4653 kind = flag_tls_default;
4655 return kind;
4658 /* Select a set of attributes for section NAME based on the properties
4659 of DECL and whether or not RELOC indicates that DECL's initializer
4660 might contain runtime relocations.
4662 We make the section read-only and executable for a function decl,
4663 read-only for a const data decl, and writable for a non-const data decl. */
4665 unsigned int
4666 default_section_type_flags (tree decl, const char *name, int reloc)
4668 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4671 unsigned int
4672 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4673 int shlib)
4675 unsigned int flags;
4677 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4678 flags = SECTION_CODE;
4679 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4680 flags = 0;
4681 else if (unlikely_text_section_name
4682 && strcmp (name, unlikely_text_section_name) == 0)
4683 flags = SECTION_CODE;
4684 else
4685 flags = SECTION_WRITE;
4687 if (decl && DECL_ONE_ONLY (decl))
4688 flags |= SECTION_LINKONCE;
4690 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
4691 flags |= SECTION_TLS | SECTION_WRITE;
4693 if (strcmp (name, ".bss") == 0
4694 || strncmp (name, ".bss.", 5) == 0
4695 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4696 || strcmp (name, ".sbss") == 0
4697 || strncmp (name, ".sbss.", 6) == 0
4698 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
4699 flags |= SECTION_BSS;
4701 if (strcmp (name, ".tdata") == 0
4702 || strncmp (name, ".tdata.", 7) == 0
4703 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
4704 flags |= SECTION_TLS;
4706 if (strcmp (name, ".tbss") == 0
4707 || strncmp (name, ".tbss.", 6) == 0
4708 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4709 flags |= SECTION_TLS | SECTION_BSS;
4711 /* These three sections have special ELF types. They are neither
4712 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4713 want to print a section type (@progbits or @nobits). If someone
4714 is silly enough to emit code or TLS variables to one of these
4715 sections, then don't handle them specially. */
4716 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4717 && (strcmp (name, ".init_array") == 0
4718 || strcmp (name, ".fini_array") == 0
4719 || strcmp (name, ".preinit_array") == 0))
4720 flags |= SECTION_NOTYPE;
4722 return flags;
4725 /* Output assembly to switch to section NAME with attribute FLAGS.
4726 Four variants for common object file formats. */
4728 void
4729 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4730 unsigned int flags ATTRIBUTE_UNUSED,
4731 tree decl ATTRIBUTE_UNUSED)
4733 /* Some object formats don't support named sections at all. The
4734 front-end should already have flagged this as an error. */
4735 gcc_unreachable ();
4738 void
4739 default_elf_asm_named_section (const char *name, unsigned int flags,
4740 tree decl ATTRIBUTE_UNUSED)
4742 char flagchars[10], *f = flagchars;
4744 /* If we have already declared this section, we can use an
4745 abbreviated form to switch back to it -- unless this section is
4746 part of a COMDAT groups, in which case GAS requires the full
4747 declaration every time. */
4748 if (!(HAVE_GAS_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4749 && ! named_section_first_declaration (name))
4751 fprintf (asm_out_file, "\t.section\t%s\n", name);
4752 return;
4755 if (!(flags & SECTION_DEBUG))
4756 *f++ = 'a';
4757 if (flags & SECTION_WRITE)
4758 *f++ = 'w';
4759 if (flags & SECTION_CODE)
4760 *f++ = 'x';
4761 if (flags & SECTION_SMALL)
4762 *f++ = 's';
4763 if (flags & SECTION_MERGE)
4764 *f++ = 'M';
4765 if (flags & SECTION_STRINGS)
4766 *f++ = 'S';
4767 if (flags & SECTION_TLS)
4768 *f++ = 'T';
4769 if (HAVE_GAS_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4770 *f++ = 'G';
4771 *f = '\0';
4773 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
4775 if (!(flags & SECTION_NOTYPE))
4777 const char *type;
4778 const char *format;
4780 if (flags & SECTION_BSS)
4781 type = "nobits";
4782 else
4783 type = "progbits";
4785 format = ",@%s";
4786 #ifdef ASM_COMMENT_START
4787 /* On platforms that use "@" as the assembly comment character,
4788 use "%" instead. */
4789 if (strcmp (ASM_COMMENT_START, "@") == 0)
4790 format = ",%%%s";
4791 #endif
4792 fprintf (asm_out_file, format, type);
4794 if (flags & SECTION_ENTSIZE)
4795 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
4796 if (HAVE_GAS_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4797 fprintf (asm_out_file, ",%s,comdat",
4798 lang_hooks.decls.comdat_group (decl));
4801 putc ('\n', asm_out_file);
4804 void
4805 default_coff_asm_named_section (const char *name, unsigned int flags,
4806 tree decl ATTRIBUTE_UNUSED)
4808 char flagchars[8], *f = flagchars;
4810 if (flags & SECTION_WRITE)
4811 *f++ = 'w';
4812 if (flags & SECTION_CODE)
4813 *f++ = 'x';
4814 *f = '\0';
4816 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
4819 void
4820 default_pe_asm_named_section (const char *name, unsigned int flags,
4821 tree decl)
4823 default_coff_asm_named_section (name, flags, decl);
4825 if (flags & SECTION_LINKONCE)
4827 /* Functions may have been compiled at various levels of
4828 optimization so we can't use `same_size' here.
4829 Instead, have the linker pick one. */
4830 fprintf (asm_out_file, "\t.linkonce %s\n",
4831 (flags & SECTION_CODE ? "discard" : "same_size"));
4835 /* The lame default section selector. */
4837 void
4838 default_select_section (tree decl, int reloc,
4839 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
4841 bool readonly = false;
4843 if (DECL_P (decl))
4845 if (decl_readonly_section (decl, reloc))
4846 readonly = true;
4848 else if (TREE_CODE (decl) == CONSTRUCTOR)
4850 if (! ((flag_pic && reloc)
4851 || !TREE_READONLY (decl)
4852 || TREE_SIDE_EFFECTS (decl)
4853 || !TREE_CONSTANT (decl)))
4854 readonly = true;
4856 else if (TREE_CODE (decl) == STRING_CST)
4857 readonly = true;
4858 else if (! (flag_pic && reloc))
4859 readonly = true;
4861 if (readonly)
4862 readonly_data_section ();
4863 else
4864 data_section ();
4867 /* A helper function for default_elf_select_section and
4868 default_elf_unique_section. Categorizes the DECL. */
4870 enum section_category
4872 SECCAT_TEXT,
4874 SECCAT_RODATA,
4875 SECCAT_RODATA_MERGE_STR,
4876 SECCAT_RODATA_MERGE_STR_INIT,
4877 SECCAT_RODATA_MERGE_CONST,
4878 SECCAT_SRODATA,
4880 SECCAT_DATA,
4882 /* To optimize loading of shared programs, define following subsections
4883 of data section:
4884 _REL Contains data that has relocations, so they get grouped
4885 together and dynamic linker will visit fewer pages in memory.
4886 _RO Contains data that is otherwise read-only. This is useful
4887 with prelinking as most relocations won't be dynamically
4888 linked and thus stay read only.
4889 _LOCAL Marks data containing relocations only to local objects.
4890 These relocations will get fully resolved by prelinking. */
4891 SECCAT_DATA_REL,
4892 SECCAT_DATA_REL_LOCAL,
4893 SECCAT_DATA_REL_RO,
4894 SECCAT_DATA_REL_RO_LOCAL,
4896 SECCAT_SDATA,
4897 SECCAT_TDATA,
4899 SECCAT_BSS,
4900 SECCAT_SBSS,
4901 SECCAT_TBSS
4904 static enum section_category
4905 categorize_decl_for_section (tree, int, int);
4907 static enum section_category
4908 categorize_decl_for_section (tree decl, int reloc, int shlib)
4910 enum section_category ret;
4912 if (TREE_CODE (decl) == FUNCTION_DECL)
4913 return SECCAT_TEXT;
4914 else if (TREE_CODE (decl) == STRING_CST)
4916 if (flag_mudflap) /* or !flag_merge_constants */
4917 return SECCAT_RODATA;
4918 else
4919 return SECCAT_RODATA_MERGE_STR;
4921 else if (TREE_CODE (decl) == VAR_DECL)
4923 if (DECL_INITIAL (decl) == NULL
4924 || DECL_INITIAL (decl) == error_mark_node
4925 || (flag_zero_initialized_in_bss
4926 /* Leave constant zeroes in .rodata so they can be shared. */
4927 && !TREE_READONLY (decl)
4928 && initializer_zerop (DECL_INITIAL (decl))))
4929 ret = SECCAT_BSS;
4930 else if (! TREE_READONLY (decl)
4931 || TREE_SIDE_EFFECTS (decl)
4932 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
4934 if (shlib && (reloc & 2))
4935 ret = SECCAT_DATA_REL;
4936 else if (shlib && reloc)
4937 ret = SECCAT_DATA_REL_LOCAL;
4938 else
4939 ret = SECCAT_DATA;
4941 else if (shlib && (reloc & 2))
4942 ret = SECCAT_DATA_REL_RO;
4943 else if (shlib && reloc)
4944 ret = SECCAT_DATA_REL_RO_LOCAL;
4945 else if (reloc || flag_merge_constants < 2)
4946 /* C and C++ don't allow different variables to share the same
4947 location. -fmerge-all-constants allows even that (at the
4948 expense of not conforming). */
4949 ret = SECCAT_RODATA;
4950 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
4951 ret = SECCAT_RODATA_MERGE_STR_INIT;
4952 else
4953 ret = SECCAT_RODATA_MERGE_CONST;
4955 else if (TREE_CODE (decl) == CONSTRUCTOR)
4957 if ((shlib && reloc)
4958 || TREE_SIDE_EFFECTS (decl)
4959 || ! TREE_CONSTANT (decl))
4960 ret = SECCAT_DATA;
4961 else
4962 ret = SECCAT_RODATA;
4964 else
4965 ret = SECCAT_RODATA;
4967 /* There are no read-only thread-local sections. */
4968 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
4970 /* Note that this would be *just* SECCAT_BSS, except that there's
4971 no concept of a read-only thread-local-data section. */
4972 if (ret == SECCAT_BSS
4973 || (flag_zero_initialized_in_bss
4974 && initializer_zerop (DECL_INITIAL (decl))))
4975 ret = SECCAT_TBSS;
4976 else
4977 ret = SECCAT_TDATA;
4980 /* If the target uses small data sections, select it. */
4981 else if (targetm.in_small_data_p (decl))
4983 if (ret == SECCAT_BSS)
4984 ret = SECCAT_SBSS;
4985 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
4986 ret = SECCAT_SRODATA;
4987 else
4988 ret = SECCAT_SDATA;
4991 return ret;
4994 bool
4995 decl_readonly_section (tree decl, int reloc)
4997 return decl_readonly_section_1 (decl, reloc, flag_pic);
5000 bool
5001 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5003 switch (categorize_decl_for_section (decl, reloc, shlib))
5005 case SECCAT_RODATA:
5006 case SECCAT_RODATA_MERGE_STR:
5007 case SECCAT_RODATA_MERGE_STR_INIT:
5008 case SECCAT_RODATA_MERGE_CONST:
5009 case SECCAT_SRODATA:
5010 return true;
5011 break;
5012 default:
5013 return false;
5014 break;
5018 /* Select a section based on the above categorization. */
5020 void
5021 default_elf_select_section (tree decl, int reloc,
5022 unsigned HOST_WIDE_INT align)
5024 default_elf_select_section_1 (decl, reloc, align, flag_pic);
5027 void
5028 default_elf_select_section_1 (tree decl, int reloc,
5029 unsigned HOST_WIDE_INT align, int shlib)
5031 const char *sname;
5032 switch (categorize_decl_for_section (decl, reloc, shlib))
5034 case SECCAT_TEXT:
5035 /* We're not supposed to be called on FUNCTION_DECLs. */
5036 gcc_unreachable ();
5037 case SECCAT_RODATA:
5038 readonly_data_section ();
5039 return;
5040 case SECCAT_RODATA_MERGE_STR:
5041 mergeable_string_section (decl, align, 0);
5042 return;
5043 case SECCAT_RODATA_MERGE_STR_INIT:
5044 mergeable_string_section (DECL_INITIAL (decl), align, 0);
5045 return;
5046 case SECCAT_RODATA_MERGE_CONST:
5047 mergeable_constant_section (DECL_MODE (decl), align, 0);
5048 return;
5049 case SECCAT_SRODATA:
5050 sname = ".sdata2";
5051 break;
5052 case SECCAT_DATA:
5053 data_section ();
5054 return;
5055 case SECCAT_DATA_REL:
5056 sname = ".data.rel";
5057 break;
5058 case SECCAT_DATA_REL_LOCAL:
5059 sname = ".data.rel.local";
5060 break;
5061 case SECCAT_DATA_REL_RO:
5062 sname = ".data.rel.ro";
5063 break;
5064 case SECCAT_DATA_REL_RO_LOCAL:
5065 sname = ".data.rel.ro.local";
5066 break;
5067 case SECCAT_SDATA:
5068 sname = ".sdata";
5069 break;
5070 case SECCAT_TDATA:
5071 sname = ".tdata";
5072 break;
5073 case SECCAT_BSS:
5074 #ifdef BSS_SECTION_ASM_OP
5075 bss_section ();
5076 return;
5077 #else
5078 sname = ".bss";
5079 break;
5080 #endif
5081 case SECCAT_SBSS:
5082 sname = ".sbss";
5083 break;
5084 case SECCAT_TBSS:
5085 sname = ".tbss";
5086 break;
5087 default:
5088 gcc_unreachable ();
5091 if (!DECL_P (decl))
5092 decl = NULL_TREE;
5093 named_section (decl, sname, reloc);
5096 /* Construct a unique section name based on the decl name and the
5097 categorization performed above. */
5099 void
5100 default_unique_section (tree decl, int reloc)
5102 default_unique_section_1 (decl, reloc, flag_pic);
5105 void
5106 default_unique_section_1 (tree decl, int reloc, int shlib)
5108 bool one_only = DECL_ONE_ONLY (decl);
5109 const char *prefix, *name;
5110 size_t nlen, plen;
5111 char *string;
5113 switch (categorize_decl_for_section (decl, reloc, shlib))
5115 case SECCAT_TEXT:
5116 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5117 break;
5118 case SECCAT_RODATA:
5119 case SECCAT_RODATA_MERGE_STR:
5120 case SECCAT_RODATA_MERGE_STR_INIT:
5121 case SECCAT_RODATA_MERGE_CONST:
5122 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5123 break;
5124 case SECCAT_SRODATA:
5125 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5126 break;
5127 case SECCAT_DATA:
5128 case SECCAT_DATA_REL:
5129 case SECCAT_DATA_REL_LOCAL:
5130 case SECCAT_DATA_REL_RO:
5131 case SECCAT_DATA_REL_RO_LOCAL:
5132 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5133 break;
5134 case SECCAT_SDATA:
5135 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5136 break;
5137 case SECCAT_BSS:
5138 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5139 break;
5140 case SECCAT_SBSS:
5141 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5142 break;
5143 case SECCAT_TDATA:
5144 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5145 break;
5146 case SECCAT_TBSS:
5147 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5148 break;
5149 default:
5150 gcc_unreachable ();
5152 plen = strlen (prefix);
5154 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5155 name = targetm.strip_name_encoding (name);
5156 nlen = strlen (name);
5158 string = alloca (nlen + plen + 1);
5159 memcpy (string, prefix, plen);
5160 memcpy (string + plen, name, nlen + 1);
5162 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5165 void
5166 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5167 rtx x,
5168 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5170 if (flag_pic)
5171 switch (GET_CODE (x))
5173 case CONST:
5174 case SYMBOL_REF:
5175 case LABEL_REF:
5176 data_section ();
5177 return;
5179 default:
5180 break;
5183 readonly_data_section ();
5186 void
5187 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5188 unsigned HOST_WIDE_INT align)
5190 /* ??? Handle small data here somehow. */
5192 if (flag_pic)
5193 switch (GET_CODE (x))
5195 case CONST:
5196 case SYMBOL_REF:
5197 named_section (NULL_TREE, ".data.rel.ro", 3);
5198 return;
5200 case LABEL_REF:
5201 named_section (NULL_TREE, ".data.rel.ro.local", 1);
5202 return;
5204 default:
5205 break;
5208 mergeable_constant_section (mode, align, 0);
5211 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
5213 void
5214 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5216 rtx symbol;
5217 int flags;
5219 /* Careful not to prod global register variables. */
5220 if (!MEM_P (rtl))
5221 return;
5222 symbol = XEXP (rtl, 0);
5223 if (GET_CODE (symbol) != SYMBOL_REF)
5224 return;
5226 flags = 0;
5227 if (TREE_CODE (decl) == FUNCTION_DECL)
5228 flags |= SYMBOL_FLAG_FUNCTION;
5229 if (targetm.binds_local_p (decl))
5230 flags |= SYMBOL_FLAG_LOCAL;
5231 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
5232 flags |= decl_tls_model (decl) << SYMBOL_FLAG_TLS_SHIFT;
5233 else if (targetm.in_small_data_p (decl))
5234 flags |= SYMBOL_FLAG_SMALL;
5235 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
5236 being PUBLIC, the thing *must* be defined in this translation unit.
5237 Prevent this buglet from being propagated into rtl code as well. */
5238 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5239 flags |= SYMBOL_FLAG_EXTERNAL;
5241 SYMBOL_REF_FLAGS (symbol) = flags;
5244 /* By default, we do nothing for encode_section_info, so we need not
5245 do anything but discard the '*' marker. */
5247 const char *
5248 default_strip_name_encoding (const char *str)
5250 return str + (*str == '*');
5253 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5254 wrt cross-module name binding. */
5256 bool
5257 default_binds_local_p (tree exp)
5259 return default_binds_local_p_1 (exp, flag_shlib);
5262 bool
5263 default_binds_local_p_1 (tree exp, int shlib)
5265 bool local_p;
5267 /* A non-decl is an entry in the constant pool. */
5268 if (!DECL_P (exp))
5269 local_p = true;
5270 /* Static variables are always local. */
5271 else if (! TREE_PUBLIC (exp))
5272 local_p = true;
5273 /* A variable is local if the user explicitly tells us so. */
5274 else if (DECL_VISIBILITY_SPECIFIED (exp) && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5275 local_p = true;
5276 /* Otherwise, variables defined outside this object may not be local. */
5277 else if (DECL_EXTERNAL (exp))
5278 local_p = false;
5279 /* Linkonce and weak data are never local. */
5280 else if (DECL_ONE_ONLY (exp) || DECL_WEAK (exp))
5281 local_p = false;
5282 /* If none of the above and visibility is not default, make local. */
5283 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5284 local_p = true;
5285 /* If PIC, then assume that any global name can be overridden by
5286 symbols resolved from other modules. */
5287 else if (shlib)
5288 local_p = false;
5289 /* Uninitialized COMMON variable may be unified with symbols
5290 resolved from other modules. */
5291 else if (DECL_COMMON (exp)
5292 && (DECL_INITIAL (exp) == NULL
5293 || DECL_INITIAL (exp) == error_mark_node))
5294 local_p = false;
5295 /* Otherwise we're left with initialized (or non-common) global data
5296 which is of necessity defined locally. */
5297 else
5298 local_p = true;
5300 return local_p;
5303 /* Determine whether or not a pointer mode is valid. Assume defaults
5304 of ptr_mode or Pmode - can be overridden. */
5305 bool
5306 default_valid_pointer_mode (enum machine_mode mode)
5308 return (mode == ptr_mode || mode == Pmode);
5311 /* Default function to output code that will globalize a label. A
5312 target must define GLOBAL_ASM_OP or provide it's own function to
5313 globalize a label. */
5314 #ifdef GLOBAL_ASM_OP
5315 void
5316 default_globalize_label (FILE * stream, const char *name)
5318 fputs (GLOBAL_ASM_OP, stream);
5319 assemble_name (stream, name);
5320 putc ('\n', stream);
5322 #endif /* GLOBAL_ASM_OP */
5324 /* Default function to output a label for unwind information. The
5325 default is to do nothing. A target that needs nonlocal labels for
5326 unwind information must provide its own function to do this. */
5327 void
5328 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5329 tree decl ATTRIBUTE_UNUSED,
5330 int for_eh ATTRIBUTE_UNUSED,
5331 int empty ATTRIBUTE_UNUSED)
5335 /* This is how to output an internal numbered label where PREFIX is
5336 the class of label and LABELNO is the number within the class. */
5338 void
5339 default_internal_label (FILE *stream, const char *prefix,
5340 unsigned long labelno)
5342 char *const buf = alloca (40 + strlen (prefix));
5343 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5344 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5347 /* This is the default behavior at the beginning of a file. It's
5348 controlled by two other target-hook toggles. */
5349 void
5350 default_file_start (void)
5352 if (targetm.file_start_app_off && !flag_verbose_asm)
5353 fputs (ASM_APP_OFF, asm_out_file);
5355 if (targetm.file_start_file_directive)
5356 output_file_directive (asm_out_file, main_input_filename);
5359 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5360 which emits a special section directive used to indicate whether or
5361 not this object file needs an executable stack. This is primarily
5362 a GNU extension to ELF but could be used on other targets. */
5364 int trampolines_created;
5366 void
5367 file_end_indicate_exec_stack (void)
5369 unsigned int flags = SECTION_DEBUG;
5370 if (trampolines_created)
5371 flags |= SECTION_CODE;
5373 named_section_flags (".note.GNU-stack", flags);
5376 #include "gt-varasm.h"