* modulo-sched.c (CFG_HOOKS): Remove.
[official-gcc.git] / gcc / varasm.c
blobf2a361dff20d3e7d28d0c4eb7e3a122c270c8702
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"
55 #include "basic-block.h"
57 #ifdef XCOFF_DEBUGGING_INFO
58 #include "xcoffout.h" /* Needed for external data
59 declarations for e.g. AIX 4.x. */
60 #endif
62 /* The (assembler) name of the first globally-visible object output. */
63 const char *first_global_object_name;
64 const char *weak_global_object_name;
66 struct addr_const;
67 struct constant_descriptor_rtx;
68 struct rtx_constant_pool;
70 struct varasm_status GTY(())
72 /* If we're using a per-function constant pool, this is it. */
73 struct rtx_constant_pool *pool;
75 /* Number of tree-constants deferred during the expansion of this
76 function. */
77 unsigned int deferred_constants;
80 #define n_deferred_constants (cfun->varasm->deferred_constants)
82 /* Number for making the label on the next
83 constant that is stored in memory. */
85 static GTY(()) int const_labelno;
87 /* Carry information from ASM_DECLARE_OBJECT_NAME
88 to ASM_FINISH_DECLARE_OBJECT. */
90 int size_directive_output;
92 /* The last decl for which assemble_variable was called,
93 if it did ASM_DECLARE_OBJECT_NAME.
94 If the last call to assemble_variable didn't do that,
95 this holds 0. */
97 tree last_assemble_variable_decl;
99 /* The following global variable indicates if the first basic block
100 in a function belongs to the cold partition or not. */
102 bool first_function_block_is_cold;
104 /* The following global variable indicates the label name to be put at
105 the start of the first cold section within each function, when
106 partitioning basic blocks into hot and cold sections. Used for
107 debug info. */
109 char *unlikely_section_label;
111 /* The following global variable indicates the label name to be put at
112 the start of the first hot section within each function, when
113 partitioning basic blocks into hot and cold sections. Used for
114 debug info. */
116 char *hot_section_label;
118 /* The following global variable indicates the label name to be put at
119 the end of the last hot section within each function, when
120 partitioning basic blocks into hot and cold sections. Used for
121 debug info. */
123 char *hot_section_end_label;
125 /* The following global variable indicates the label name to be put at
126 the end of the last cold section within each function, when
127 partitioning basic blocks into hot and cold sections. Used for
128 debug info.*/
130 char *cold_section_end_label;
132 /* The following global variable indicates the section name to be used
133 for the current cold section, when partitioning hot and cold basic
134 blocks into separate sections. */
136 char *unlikely_text_section_name;
138 /* We give all constants their own alias set. Perhaps redundant with
139 MEM_READONLY_P, but pre-dates it. */
141 static HOST_WIDE_INT const_alias_set;
143 static const char *strip_reg_name (const char *);
144 static int contains_pointers_p (tree);
145 #ifdef ASM_OUTPUT_EXTERNAL
146 static bool incorporeal_function_p (tree);
147 #endif
148 static void decode_addr_const (tree, struct addr_const *);
149 static hashval_t const_desc_hash (const void *);
150 static int const_desc_eq (const void *, const void *);
151 static hashval_t const_hash_1 (const tree);
152 static int compare_constant (const tree, const tree);
153 static tree copy_constant (tree);
154 static void output_constant_def_contents (rtx);
155 static void output_addressed_constants (tree);
156 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
157 static unsigned min_align (unsigned, unsigned);
158 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
159 static void globalize_decl (tree);
160 static void maybe_assemble_visibility (tree);
161 static int in_named_entry_eq (const void *, const void *);
162 static hashval_t in_named_entry_hash (const void *);
163 static void initialize_cold_section_name (void);
164 #ifdef BSS_SECTION_ASM_OP
165 #ifdef ASM_OUTPUT_BSS
166 static void asm_output_bss (FILE *, tree, const char *,
167 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
168 #endif
169 #ifdef ASM_OUTPUT_ALIGNED_BSS
170 static void asm_output_aligned_bss (FILE *, tree, const char *,
171 unsigned HOST_WIDE_INT, int)
172 ATTRIBUTE_UNUSED;
173 #endif
174 #endif /* BSS_SECTION_ASM_OP */
175 static bool asm_emit_uninitialised (tree, const char*,
176 unsigned HOST_WIDE_INT,
177 unsigned HOST_WIDE_INT);
178 static void mark_weak (tree);
180 static GTY(()) enum in_section in_section = no_section;
181 enum in_section last_text_section;
183 /* Return a nonzero value if DECL has a section attribute. */
184 #ifndef IN_NAMED_SECTION
185 #define IN_NAMED_SECTION(DECL) \
186 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
187 && DECL_SECTION_NAME (DECL) != NULL_TREE)
188 #endif
190 /* Text of section name when in_section == in_named. */
191 static GTY(()) const char *in_named_name;
192 const char *last_text_section_name;
194 /* Hash table of flags that have been used for a particular named section. */
196 struct in_named_entry GTY(())
198 const char *name;
199 unsigned int flags;
200 bool declared;
203 static GTY((param_is (struct in_named_entry))) htab_t in_named_htab;
205 /* Define functions like text_section for any extra sections. */
206 #ifdef EXTRA_SECTION_FUNCTIONS
207 EXTRA_SECTION_FUNCTIONS
208 #endif
210 static void
211 initialize_cold_section_name (void)
213 const char *name;
214 int len;
216 if (! unlikely_text_section_name)
218 if (DECL_SECTION_NAME (current_function_decl)
219 && (strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME
220 (current_function_decl)),
221 HOT_TEXT_SECTION_NAME) != 0)
222 && (strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME
223 (current_function_decl)),
224 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) != 0))
226 name = TREE_STRING_POINTER (DECL_SECTION_NAME
227 (current_function_decl));
228 len = strlen (name);
229 unlikely_text_section_name = xmalloc (len + 10);
230 sprintf (unlikely_text_section_name, "%s%s", name, "_unlikely");
232 else
233 unlikely_text_section_name =
234 xstrdup (UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
238 /* Tell assembler to switch to text section. */
240 void
241 text_section (void)
243 if (in_section != in_text)
245 in_section = in_text;
246 last_text_section = in_text;
247 fprintf (asm_out_file, "%s\n", TEXT_SECTION_ASM_OP);
251 /* Tell assembler to switch to unlikely-to-be-executed text section. */
253 void
254 unlikely_text_section (void)
256 if (! unlikely_text_section_name)
257 initialize_cold_section_name ();
259 if ((in_section != in_unlikely_executed_text)
260 && (in_section != in_named
261 || strcmp (in_named_name, unlikely_text_section_name) != 0))
263 named_section (NULL_TREE, unlikely_text_section_name, 0);
264 in_section = in_unlikely_executed_text;
265 last_text_section = in_unlikely_executed_text;
269 /* Tell assembler to switch to data section. */
271 void
272 data_section (void)
274 if (in_section != in_data)
276 in_section = in_data;
277 fprintf (asm_out_file, "%s\n", DATA_SECTION_ASM_OP);
281 /* Tell assembler to switch to read-only data section. This is normally
282 the text section. */
284 void
285 readonly_data_section (void)
287 #ifdef READONLY_DATA_SECTION
288 READONLY_DATA_SECTION (); /* Note this can call data_section. */
289 #else
290 #ifdef READONLY_DATA_SECTION_ASM_OP
291 if (in_section != in_readonly_data)
293 in_section = in_readonly_data;
294 fputs (READONLY_DATA_SECTION_ASM_OP, asm_out_file);
295 fputc ('\n', asm_out_file);
297 #else
298 text_section ();
299 #endif
300 #endif
303 /* Determine if we're in the text section. */
306 in_text_section (void)
308 return in_section == in_text;
311 /* Determine if we're in the unlikely-to-be-executed text section. */
314 in_unlikely_text_section (void)
316 bool ret_val;
318 ret_val = ((in_section == in_unlikely_executed_text)
319 || (in_section == in_named
320 && unlikely_text_section_name
321 && strcmp (in_named_name, unlikely_text_section_name) == 0));
323 return ret_val;
326 /* Determine if we're in the data section. */
329 in_data_section (void)
331 return in_section == in_data;
334 /* Helper routines for maintaining in_named_htab. */
336 static int
337 in_named_entry_eq (const void *p1, const void *p2)
339 const struct in_named_entry *old = p1;
340 const char *new = p2;
342 return strcmp (old->name, new) == 0;
345 static hashval_t
346 in_named_entry_hash (const void *p)
348 const struct in_named_entry *old = p;
349 return htab_hash_string (old->name);
352 /* If SECTION has been seen before as a named section, return the flags
353 that were used. Otherwise, return 0. Note, that 0 is a perfectly valid
354 set of flags for a section to have, so 0 does not mean that the section
355 has not been seen. */
357 static unsigned int
358 get_named_section_flags (const char *section)
360 struct in_named_entry **slot;
362 slot = (struct in_named_entry **)
363 htab_find_slot_with_hash (in_named_htab, section,
364 htab_hash_string (section), NO_INSERT);
366 return slot ? (*slot)->flags : 0;
369 /* Returns true if the section has been declared before. Sets internal
370 flag on this section in in_named_hash so subsequent calls on this
371 section will return false. */
373 bool
374 named_section_first_declaration (const char *name)
376 struct in_named_entry **slot;
378 slot = (struct in_named_entry **)
379 htab_find_slot_with_hash (in_named_htab, name,
380 htab_hash_string (name), NO_INSERT);
381 if (! (*slot)->declared)
383 (*slot)->declared = true;
384 return true;
386 else
388 return false;
393 /* Record FLAGS for SECTION. If SECTION was previously recorded with a
394 different set of flags, return false. */
396 bool
397 set_named_section_flags (const char *section, unsigned int flags)
399 struct in_named_entry **slot, *entry;
401 slot = (struct in_named_entry **)
402 htab_find_slot_with_hash (in_named_htab, section,
403 htab_hash_string (section), INSERT);
404 entry = *slot;
406 if (!entry)
408 entry = ggc_alloc (sizeof (*entry));
409 *slot = entry;
410 entry->name = ggc_strdup (section);
411 entry->flags = flags;
412 entry->declared = false;
414 else if (entry->flags != flags)
415 return false;
417 return true;
420 /* Tell assembler to change to section NAME with attributes FLAGS. If
421 DECL is non-NULL, it is the VAR_DECL or FUNCTION_DECL with which
422 this section is associated. */
424 void
425 named_section_real (const char *name, unsigned int flags, tree decl)
427 if (in_section != in_named || strcmp (name, in_named_name) != 0)
429 bool unchanged = set_named_section_flags (name, flags);
431 gcc_assert (unchanged);
433 targetm.asm_out.named_section (name, flags, decl);
435 if (flags & SECTION_FORGET)
436 in_section = no_section;
437 else
439 in_named_name = ggc_strdup (name);
440 in_section = in_named;
444 if (in_text_section () || in_unlikely_text_section ())
446 last_text_section = in_section;
447 last_text_section_name = name;
451 /* Tell assembler to change to section NAME for DECL.
452 If DECL is NULL, just switch to section NAME.
453 If NAME is NULL, get the name from DECL.
454 If RELOC is 1, the initializer for DECL contains relocs. */
456 void
457 named_section (tree decl, const char *name, int reloc)
459 unsigned int flags;
461 gcc_assert (!decl || DECL_P (decl));
462 if (name == NULL)
463 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
465 if (strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0
466 && !unlikely_text_section_name)
467 unlikely_text_section_name =
468 xstrdup (UNLIKELY_EXECUTED_TEXT_SECTION_NAME);
470 flags = targetm.section_type_flags (decl, name, reloc);
472 /* Sanity check user variables for flag changes. Non-user
473 section flag changes will abort in named_section_flags.
474 However, don't complain if SECTION_OVERRIDE is set.
475 We trust that the setter knows that it is safe to ignore
476 the default flags for this decl. */
477 if (decl && ! set_named_section_flags (name, flags))
479 flags = get_named_section_flags (name);
480 if ((flags & SECTION_OVERRIDE) == 0)
481 error ("%J%D causes a section type conflict", decl, decl);
484 named_section_real (name, flags, decl);
487 /* If required, set DECL_SECTION_NAME to a unique name. */
489 void
490 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
491 int flag_function_or_data_sections)
493 if (DECL_SECTION_NAME (decl) == NULL_TREE
494 && targetm.have_named_sections
495 && (flag_function_or_data_sections
496 || DECL_ONE_ONLY (decl)))
497 targetm.asm_out.unique_section (decl, reloc);
500 #ifdef BSS_SECTION_ASM_OP
502 /* Tell the assembler to switch to the bss section. */
504 void
505 bss_section (void)
507 if (in_section != in_bss)
509 fprintf (asm_out_file, "%s\n", BSS_SECTION_ASM_OP);
510 in_section = in_bss;
514 #ifdef ASM_OUTPUT_BSS
516 /* Utility function for ASM_OUTPUT_BSS for targets to use if
517 they don't support alignments in .bss.
518 ??? It is believed that this function will work in most cases so such
519 support is localized here. */
521 static void
522 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
523 const char *name,
524 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
525 unsigned HOST_WIDE_INT rounded)
527 targetm.asm_out.globalize_label (file, name);
528 bss_section ();
529 #ifdef ASM_DECLARE_OBJECT_NAME
530 last_assemble_variable_decl = decl;
531 ASM_DECLARE_OBJECT_NAME (file, name, decl);
532 #else
533 /* Standard thing is just output label for the object. */
534 ASM_OUTPUT_LABEL (file, name);
535 #endif /* ASM_DECLARE_OBJECT_NAME */
536 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
539 #endif
541 #ifdef ASM_OUTPUT_ALIGNED_BSS
543 /* Utility function for targets to use in implementing
544 ASM_OUTPUT_ALIGNED_BSS.
545 ??? It is believed that this function will work in most cases so such
546 support is localized here. */
548 static void
549 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
550 const char *name, unsigned HOST_WIDE_INT size,
551 int align)
553 bss_section ();
554 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
555 #ifdef ASM_DECLARE_OBJECT_NAME
556 last_assemble_variable_decl = decl;
557 ASM_DECLARE_OBJECT_NAME (file, name, decl);
558 #else
559 /* Standard thing is just output label for the object. */
560 ASM_OUTPUT_LABEL (file, name);
561 #endif /* ASM_DECLARE_OBJECT_NAME */
562 ASM_OUTPUT_SKIP (file, size ? size : 1);
565 #endif
567 #endif /* BSS_SECTION_ASM_OP */
569 /* Switch to the section for function DECL.
571 If DECL is NULL_TREE, switch to the text section. We can be passed
572 NULL_TREE under some circumstances by dbxout.c at least. */
574 void
575 function_section (tree decl)
577 bool unlikely = false;
579 if (first_function_block_is_cold)
580 unlikely = true;
582 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
583 targetm.asm_out.select_section (decl, unlikely, DECL_ALIGN (decl));
584 #else
585 if (decl != NULL_TREE
586 && DECL_SECTION_NAME (decl) != NULL_TREE)
587 named_section (decl, (char *) 0, 0);
588 else
589 text_section ();
590 #endif
593 void
594 current_function_section (tree decl)
596 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
597 bool unlikely = (in_unlikely_text_section ()
598 || (last_text_section == in_unlikely_executed_text));
600 targetm.asm_out.select_section (decl, unlikely, DECL_ALIGN (decl));
601 #else
602 if (last_text_section == in_unlikely_executed_text)
603 unlikely_text_section ();
604 else if (last_text_section == in_text)
605 text_section ();
606 else if (last_text_section == in_named)
607 named_section (NULL_TREE, last_text_section_name, 0);
608 else
609 function_section (decl);
610 #endif
613 /* Switch to read-only data section associated with function DECL. */
615 void
616 default_function_rodata_section (tree decl)
618 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
620 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
622 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
623 if (DECL_ONE_ONLY (decl) && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
625 size_t len = strlen (name) + 1;
626 char *rname = alloca (len);
628 memcpy (rname, name, len);
629 rname[14] = 'r';
630 named_section_real (rname, SECTION_LINKONCE, decl);
631 return;
633 /* For .text.foo we want to use .rodata.foo. */
634 else if (flag_function_sections && flag_data_sections
635 && strncmp (name, ".text.", 6) == 0)
637 size_t len = strlen (name) + 1;
638 char *rname = alloca (len + 2);
640 memcpy (rname, ".rodata", 7);
641 memcpy (rname + 7, name + 5, len - 5);
642 named_section_flags (rname, 0);
643 return;
647 readonly_data_section ();
650 /* Switch to read-only data section associated with function DECL
651 for targets where that section should be always the single
652 readonly data section. */
654 void
655 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
657 readonly_data_section ();
660 /* Switch to section for variable DECL. RELOC is the same as the
661 argument to SELECT_SECTION. */
663 void
664 variable_section (tree decl, int reloc)
666 if (IN_NAMED_SECTION (decl))
667 named_section (decl, NULL, reloc);
668 else
669 targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
672 /* Tell assembler to switch to the section for string merging. */
674 void
675 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
676 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
677 unsigned int flags ATTRIBUTE_UNUSED)
679 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
680 && TREE_CODE (decl) == STRING_CST
681 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
682 && align <= 256
683 && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
685 enum machine_mode mode;
686 unsigned int modesize;
687 const char *str;
688 int i, j, len, unit;
689 char name[30];
691 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
692 modesize = GET_MODE_BITSIZE (mode);
693 if (modesize >= 8 && modesize <= 256
694 && (modesize & (modesize - 1)) == 0)
696 if (align < modesize)
697 align = modesize;
699 str = TREE_STRING_POINTER (decl);
700 len = TREE_STRING_LENGTH (decl);
701 unit = GET_MODE_SIZE (mode);
703 /* Check for embedded NUL characters. */
704 for (i = 0; i < len; i += unit)
706 for (j = 0; j < unit; j++)
707 if (str[i + j] != '\0')
708 break;
709 if (j == unit)
710 break;
712 if (i == len - unit)
714 sprintf (name, ".rodata.str%d.%d", modesize / 8,
715 (int) (align / 8));
716 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
717 if (!i && modesize < align)
719 /* A "" string with requested alignment greater than
720 character size might cause a problem:
721 if some other string required even bigger
722 alignment than "", then linker might think the
723 "" is just part of padding after some other string
724 and not put it into the hash table initially.
725 But this means "" could have smaller alignment
726 than requested. */
727 #ifdef ASM_OUTPUT_SECTION_START
728 named_section_flags (name, flags);
729 ASM_OUTPUT_SECTION_START (asm_out_file);
730 #else
731 readonly_data_section ();
732 #endif
733 return;
736 named_section_flags (name, flags);
737 return;
742 readonly_data_section ();
745 /* Tell assembler to switch to the section for constant merging. */
747 void
748 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
749 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
750 unsigned int flags ATTRIBUTE_UNUSED)
752 unsigned int modesize = GET_MODE_BITSIZE (mode);
754 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
755 && mode != VOIDmode
756 && mode != BLKmode
757 && modesize <= align
758 && align >= 8
759 && align <= 256
760 && (align & (align - 1)) == 0)
762 char name[24];
764 sprintf (name, ".rodata.cst%d", (int) (align / 8));
765 flags |= (align / 8) | SECTION_MERGE;
766 named_section_flags (name, flags);
767 return;
770 readonly_data_section ();
773 /* Given NAME, a putative register name, discard any customary prefixes. */
775 static const char *
776 strip_reg_name (const char *name)
778 #ifdef REGISTER_PREFIX
779 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
780 name += strlen (REGISTER_PREFIX);
781 #endif
782 if (name[0] == '%' || name[0] == '#')
783 name++;
784 return name;
787 /* The user has asked for a DECL to have a particular name. Set (or
788 change) it in such a way that we don't prefix an underscore to
789 it. */
790 void
791 set_user_assembler_name (tree decl, const char *name)
793 char *starred = alloca (strlen (name) + 2);
794 starred[0] = '*';
795 strcpy (starred + 1, name);
796 change_decl_assembler_name (decl, get_identifier (starred));
797 SET_DECL_RTL (decl, NULL_RTX);
800 /* Decode an `asm' spec for a declaration as a register name.
801 Return the register number, or -1 if nothing specified,
802 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
803 or -3 if ASMSPEC is `cc' and is not recognized,
804 or -4 if ASMSPEC is `memory' and is not recognized.
805 Accept an exact spelling or a decimal number.
806 Prefixes such as % are optional. */
809 decode_reg_name (const char *asmspec)
811 if (asmspec != 0)
813 int i;
815 /* Get rid of confusing prefixes. */
816 asmspec = strip_reg_name (asmspec);
818 /* Allow a decimal number as a "register name". */
819 for (i = strlen (asmspec) - 1; i >= 0; i--)
820 if (! ISDIGIT (asmspec[i]))
821 break;
822 if (asmspec[0] != 0 && i < 0)
824 i = atoi (asmspec);
825 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
826 return i;
827 else
828 return -2;
831 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
832 if (reg_names[i][0]
833 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
834 return i;
836 #ifdef ADDITIONAL_REGISTER_NAMES
838 static const struct { const char *const name; const int number; } table[]
839 = ADDITIONAL_REGISTER_NAMES;
841 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
842 if (! strcmp (asmspec, table[i].name))
843 return table[i].number;
845 #endif /* ADDITIONAL_REGISTER_NAMES */
847 if (!strcmp (asmspec, "memory"))
848 return -4;
850 if (!strcmp (asmspec, "cc"))
851 return -3;
853 return -2;
856 return -1;
859 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
860 have static storage duration. In other words, it should not be an
861 automatic variable, including PARM_DECLs.
863 There is, however, one exception: this function handles variables
864 explicitly placed in a particular register by the user.
866 This is never called for PARM_DECL nodes. */
868 void
869 make_decl_rtl (tree decl)
871 const char *name = 0;
872 int reg_number;
873 rtx x;
875 /* Check that we are not being given an automatic variable. */
876 gcc_assert (TREE_CODE (decl) != PARM_DECL
877 && TREE_CODE (decl) != RESULT_DECL);
879 /* A weak alias has TREE_PUBLIC set but not the other bits. */
880 gcc_assert (TREE_CODE (decl) != VAR_DECL
881 || TREE_STATIC (decl)
882 || TREE_PUBLIC (decl)
883 || DECL_EXTERNAL (decl)
884 || DECL_REGISTER (decl));
886 /* And that we were not given a type or a label. */
887 gcc_assert (TREE_CODE (decl) != TYPE_DECL
888 && TREE_CODE (decl) != LABEL_DECL);
890 /* For a duplicate declaration, we can be called twice on the
891 same DECL node. Don't discard the RTL already made. */
892 if (DECL_RTL_SET_P (decl))
894 /* If the old RTL had the wrong mode, fix the mode. */
895 if (GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
896 SET_DECL_RTL (decl, adjust_address_nv (DECL_RTL (decl),
897 DECL_MODE (decl), 0));
899 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
900 return;
902 /* ??? Another way to do this would be to maintain a hashed
903 table of such critters. Instead of adding stuff to a DECL
904 to give certain attributes to it, we could use an external
905 hash map from DECL to set of attributes. */
907 /* Let the target reassign the RTL if it wants.
908 This is necessary, for example, when one machine specific
909 decl attribute overrides another. */
910 targetm.encode_section_info (decl, DECL_RTL (decl), false);
912 /* Make this function static known to the mudflap runtime. */
913 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
914 mudflap_enqueue_decl (decl);
916 return;
919 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
921 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
923 reg_number = decode_reg_name (name);
924 /* First detect errors in declaring global registers. */
925 if (reg_number == -1)
926 error ("%Jregister name not specified for %qD", decl, decl);
927 else if (reg_number < 0)
928 error ("%Jinvalid register name for %qD", decl, decl);
929 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
930 error ("%Jdata type of %qD isn%'t suitable for a register",
931 decl, decl);
932 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
933 error ("%Jregister specified for %qD isn%'t suitable for data type",
934 decl, decl);
935 /* Now handle properly declared static register variables. */
936 else
938 int nregs;
940 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
942 DECL_INITIAL (decl) = 0;
943 error ("global register variable has initial value");
945 if (TREE_THIS_VOLATILE (decl))
946 warning ("volatile register variables don%'t "
947 "work as you might wish");
949 /* If the user specified one of the eliminables registers here,
950 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
951 confused with that register and be eliminated. This usage is
952 somewhat suspect... */
954 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
955 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
956 REG_USERVAR_P (DECL_RTL (decl)) = 1;
958 if (TREE_STATIC (decl))
960 /* Make this register global, so not usable for anything
961 else. */
962 #ifdef ASM_DECLARE_REGISTER_GLOBAL
963 name = IDENTIFIER_POINTER (DECL_NAME (decl));
964 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
965 #endif
966 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
967 while (nregs > 0)
968 globalize_reg (reg_number + --nregs);
971 /* As a register variable, it has no section. */
972 return;
975 /* Now handle ordinary static variables and functions (in memory).
976 Also handle vars declared register invalidly. */
977 else if (name[0] == '*')
979 #ifdef REGISTER_PREFIX
980 if (strlen (REGISTER_PREFIX) != 0)
982 reg_number = decode_reg_name (name);
983 if (reg_number >= 0 || reg_number == -3)
984 error ("%Jregister name given for non-register variable %qD", decl, decl);
986 #endif
989 /* Specifying a section attribute on a variable forces it into a
990 non-.bss section, and thus it cannot be common. */
991 if (TREE_CODE (decl) == VAR_DECL
992 && DECL_SECTION_NAME (decl) != NULL_TREE
993 && DECL_INITIAL (decl) == NULL_TREE
994 && DECL_COMMON (decl))
995 DECL_COMMON (decl) = 0;
997 /* Variables can't be both common and weak. */
998 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
999 DECL_COMMON (decl) = 0;
1001 x = gen_rtx_SYMBOL_REF (Pmode, name);
1002 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1003 SYMBOL_REF_DECL (x) = decl;
1005 x = gen_rtx_MEM (DECL_MODE (decl), x);
1006 if (TREE_CODE (decl) != FUNCTION_DECL)
1007 set_mem_attributes (x, decl, 1);
1008 SET_DECL_RTL (decl, x);
1010 /* Optionally set flags or add text to the name to record information
1011 such as that it is a function name.
1012 If the name is changed, the macro ASM_OUTPUT_LABELREF
1013 will have to know how to strip this information. */
1014 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1016 /* Make this function static known to the mudflap runtime. */
1017 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
1018 mudflap_enqueue_decl (decl);
1021 /* Make the rtl for variable VAR be volatile.
1022 Use this only for static variables. */
1024 void
1025 make_var_volatile (tree var)
1027 gcc_assert (MEM_P (DECL_RTL (var)));
1029 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
1032 /* Output a string of literal assembler code
1033 for an `asm' keyword used between functions. */
1035 void
1036 assemble_asm (tree string)
1038 app_enable ();
1040 if (TREE_CODE (string) == ADDR_EXPR)
1041 string = TREE_OPERAND (string, 0);
1043 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
1046 /* Record an element in the table of global destructors. SYMBOL is
1047 a SYMBOL_REF of the function to be called; PRIORITY is a number
1048 between 0 and MAX_INIT_PRIORITY. */
1050 void
1051 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
1052 int priority ATTRIBUTE_UNUSED)
1054 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1055 /* Tell GNU LD that this is part of the static destructor set.
1056 This will work for any system that uses stabs, most usefully
1057 aout systems. */
1058 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
1059 dbxout_stab_value_label (XSTR (symbol, 0));
1060 #else
1061 sorry ("global destructors not supported on this target");
1062 #endif
1065 void
1066 default_named_section_asm_out_destructor (rtx symbol, int priority)
1068 const char *section = ".dtors";
1069 char buf[16];
1071 /* ??? This only works reliably with the GNU linker. */
1072 if (priority != DEFAULT_INIT_PRIORITY)
1074 sprintf (buf, ".dtors.%.5u",
1075 /* Invert the numbering so the linker puts us in the proper
1076 order; constructors are run from right to left, and the
1077 linker sorts in increasing order. */
1078 MAX_INIT_PRIORITY - priority);
1079 section = buf;
1082 named_section_flags (section, SECTION_WRITE);
1083 assemble_align (POINTER_SIZE);
1084 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1087 #ifdef DTORS_SECTION_ASM_OP
1088 void
1089 dtors_section (void)
1091 if (in_section != in_dtors)
1093 in_section = in_dtors;
1094 fputs (DTORS_SECTION_ASM_OP, asm_out_file);
1095 fputc ('\n', asm_out_file);
1099 void
1100 default_dtor_section_asm_out_destructor (rtx symbol,
1101 int priority ATTRIBUTE_UNUSED)
1103 dtors_section ();
1104 assemble_align (POINTER_SIZE);
1105 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1107 #endif
1109 /* Likewise for global constructors. */
1111 void
1112 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
1113 int priority ATTRIBUTE_UNUSED)
1115 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
1116 /* Tell GNU LD that this is part of the static destructor set.
1117 This will work for any system that uses stabs, most usefully
1118 aout systems. */
1119 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
1120 dbxout_stab_value_label (XSTR (symbol, 0));
1121 #else
1122 sorry ("global constructors not supported on this target");
1123 #endif
1126 void
1127 default_named_section_asm_out_constructor (rtx symbol, int priority)
1129 const char *section = ".ctors";
1130 char buf[16];
1132 /* ??? This only works reliably with the GNU linker. */
1133 if (priority != DEFAULT_INIT_PRIORITY)
1135 sprintf (buf, ".ctors.%.5u",
1136 /* Invert the numbering so the linker puts us in the proper
1137 order; constructors are run from right to left, and the
1138 linker sorts in increasing order. */
1139 MAX_INIT_PRIORITY - priority);
1140 section = buf;
1143 named_section_flags (section, SECTION_WRITE);
1144 assemble_align (POINTER_SIZE);
1145 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1148 #ifdef CTORS_SECTION_ASM_OP
1149 void
1150 ctors_section (void)
1152 if (in_section != in_ctors)
1154 in_section = in_ctors;
1155 fputs (CTORS_SECTION_ASM_OP, asm_out_file);
1156 fputc ('\n', asm_out_file);
1160 void
1161 default_ctor_section_asm_out_constructor (rtx symbol,
1162 int priority ATTRIBUTE_UNUSED)
1164 ctors_section ();
1165 assemble_align (POINTER_SIZE);
1166 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1168 #endif
1170 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1171 a nonzero value if the constant pool should be output before the
1172 start of the function, or a zero value if the pool should output
1173 after the end of the function. The default is to put it before the
1174 start. */
1176 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1177 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1178 #endif
1180 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1181 to be output to assembler.
1182 Set first_global_object_name and weak_global_object_name as appropriate. */
1184 void
1185 notice_global_symbol (tree decl)
1187 const char **type = &first_global_object_name;
1189 if (first_global_object_name
1190 || !TREE_PUBLIC (decl) || DECL_EXTERNAL (decl)
1191 || !DECL_NAME (decl)
1192 || (TREE_CODE (decl) != FUNCTION_DECL
1193 && (TREE_CODE (decl) != VAR_DECL
1194 || (DECL_COMMON (decl)
1195 && (DECL_INITIAL (decl) == 0
1196 || DECL_INITIAL (decl) == error_mark_node))))
1197 || !MEM_P (DECL_RTL (decl)))
1198 return;
1200 /* We win when global object is found, but it is useful to know about weak
1201 symbol as well so we can produce nicer unique names. */
1202 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1203 type = &weak_global_object_name;
1205 if (!*type)
1207 const char *p;
1208 char *name;
1209 rtx decl_rtl = DECL_RTL (decl);
1211 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1212 name = xstrdup (p);
1214 *type = name;
1218 /* Output assembler code for the constant pool of a function and associated
1219 with defining the name of the function. DECL describes the function.
1220 NAME is the function's name. For the constant pool, we use the current
1221 constant pool data. */
1223 void
1224 assemble_start_function (tree decl, const char *fnname)
1226 int align;
1227 bool hot_label_written = false;
1229 unlikely_text_section_name = NULL;
1231 first_function_block_is_cold = false;
1232 hot_section_label = reconcat (hot_section_label, fnname, ".hot_section", NULL);
1233 unlikely_section_label = reconcat (unlikely_section_label,
1234 fnname, ".unlikely_section", NULL);
1235 hot_section_end_label = reconcat (hot_section_end_label,
1236 fnname, ".end", NULL);
1237 cold_section_end_label = reconcat (cold_section_end_label,
1238 fnname, ".end.cold", NULL);
1240 /* The following code does not need preprocessing in the assembler. */
1242 app_disable ();
1244 if (CONSTANT_POOL_BEFORE_FUNCTION)
1245 output_constant_pool (fnname, decl);
1247 /* Make sure the not and cold text (code) sections are properly
1248 aligned. This is necessary here in the case where the function
1249 has both hot and cold sections, because we don't want to re-set
1250 the alignment when the section switch happens mid-function. */
1252 if (flag_reorder_blocks_and_partition)
1254 unlikely_text_section ();
1255 assemble_align (FUNCTION_BOUNDARY);
1256 ASM_OUTPUT_LABEL (asm_out_file, unlikely_section_label);
1257 if (BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1259 /* Since the function starts with a cold section, we need to
1260 explicitly align the hot section and write out the hot
1261 section label. */
1262 text_section ();
1263 assemble_align (FUNCTION_BOUNDARY);
1264 ASM_OUTPUT_LABEL (asm_out_file, hot_section_label);
1265 hot_label_written = true;
1266 first_function_block_is_cold = true;
1269 else if (DECL_SECTION_NAME (decl))
1271 /* Calls to function_section rely on first_function_block_is_cold
1272 being accurate. The first block may be cold even if we aren't
1273 doing partitioning, if the entire function was decided by
1274 choose_function_section (predict.c) to be cold. */
1276 int i;
1277 int len;
1278 char *s;
1280 initialize_cold_section_name ();
1282 /* The following is necessary, because 'strcmp
1283 (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)), blah)' always
1284 fails, presumably because TREE_STRING_POINTER is declared to
1285 be an array of size 1 of char. */
1287 len = TREE_STRING_LENGTH (DECL_SECTION_NAME (decl));
1288 s = (char *) xmalloc (len + 1);
1290 for (i = 0; i < len; i ++)
1291 s[i] = (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)))[i];
1292 s[len] = '\0';
1294 if (unlikely_text_section_name
1295 && (strcmp (s, unlikely_text_section_name) == 0))
1296 first_function_block_is_cold = true;
1299 last_text_section = no_section;
1300 in_section = no_section;
1301 resolve_unique_section (decl, 0, flag_function_sections);
1303 /* Switch to the correct text section for the start of the function. */
1305 function_section (decl);
1306 if (flag_reorder_blocks_and_partition
1307 && !hot_label_written)
1308 ASM_OUTPUT_LABEL (asm_out_file, hot_section_label);
1310 /* Tell assembler to move to target machine's alignment for functions. */
1311 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1312 if (align < force_align_functions_log)
1313 align = force_align_functions_log;
1314 if (align > 0)
1316 ASM_OUTPUT_ALIGN (asm_out_file, align);
1319 /* Handle a user-specified function alignment.
1320 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1321 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1322 if (align_functions_log > align
1323 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1325 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1326 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1327 align_functions_log, align_functions - 1);
1328 #else
1329 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1330 #endif
1333 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1334 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1335 #endif
1337 (*debug_hooks->begin_function) (decl);
1339 /* Make function name accessible from other files, if appropriate. */
1341 if (TREE_PUBLIC (decl))
1343 notice_global_symbol (decl);
1345 globalize_decl (decl);
1347 maybe_assemble_visibility (decl);
1350 if (DECL_PRESERVE_P (decl))
1351 targetm.asm_out.mark_decl_preserved (fnname);
1353 /* Do any machine/system dependent processing of the function name. */
1354 #ifdef ASM_DECLARE_FUNCTION_NAME
1355 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1356 #else
1357 /* Standard thing is just output label for the function. */
1358 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1359 #endif /* ASM_DECLARE_FUNCTION_NAME */
1361 insert_section_boundary_note ();
1364 /* Output assembler code associated with defining the size of the
1365 function. DECL describes the function. NAME is the function's name. */
1367 void
1368 assemble_end_function (tree decl, const char *fnname)
1370 enum in_section save_text_section;
1371 #ifdef ASM_DECLARE_FUNCTION_SIZE
1372 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1373 #endif
1374 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1376 output_constant_pool (fnname, decl);
1377 function_section (decl); /* need to switch back */
1379 /* Output labels for end of hot/cold text sections (to be used by
1380 debug info.) */
1381 if (flag_reorder_blocks_and_partition)
1383 save_text_section = in_section;
1384 unlikely_text_section ();
1385 ASM_OUTPUT_LABEL (asm_out_file, cold_section_end_label);
1386 text_section ();
1387 ASM_OUTPUT_LABEL (asm_out_file, hot_section_end_label);
1388 if (save_text_section == in_unlikely_executed_text)
1389 unlikely_text_section ();
1393 /* Assemble code to leave SIZE bytes of zeros. */
1395 void
1396 assemble_zeros (unsigned HOST_WIDE_INT size)
1398 /* Do no output if -fsyntax-only. */
1399 if (flag_syntax_only)
1400 return;
1402 #ifdef ASM_NO_SKIP_IN_TEXT
1403 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1404 so we must output 0s explicitly in the text section. */
1405 if ((ASM_NO_SKIP_IN_TEXT && in_text_section ())
1406 || (ASM_NO_SKIP_IN_TEXT && in_unlikely_text_section ()))
1408 unsigned HOST_WIDE_INT i;
1409 for (i = 0; i < size; i++)
1410 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1412 else
1413 #endif
1414 if (size > 0)
1415 ASM_OUTPUT_SKIP (asm_out_file, size);
1418 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1420 void
1421 assemble_align (int align)
1423 if (align > BITS_PER_UNIT)
1425 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1429 /* Assemble a string constant with the specified C string as contents. */
1431 void
1432 assemble_string (const char *p, int size)
1434 int pos = 0;
1435 int maximum = 2000;
1437 /* If the string is very long, split it up. */
1439 while (pos < size)
1441 int thissize = size - pos;
1442 if (thissize > maximum)
1443 thissize = maximum;
1445 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1447 pos += thissize;
1448 p += thissize;
1453 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1454 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1455 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1456 #else
1457 #if defined ASM_OUTPUT_ALIGNED_LOCAL
1458 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1459 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl))
1460 #else
1461 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1462 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded)
1463 #endif
1464 #endif
1466 #if defined ASM_OUTPUT_ALIGNED_BSS
1467 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1468 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1469 #else
1470 #if defined ASM_OUTPUT_BSS
1471 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1472 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded)
1473 #else
1474 #undef ASM_EMIT_BSS
1475 #endif
1476 #endif
1478 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1479 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1480 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1481 #else
1482 #if defined ASM_OUTPUT_ALIGNED_COMMON
1483 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1484 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl))
1485 #else
1486 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1487 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded)
1488 #endif
1489 #endif
1491 static bool
1492 asm_emit_uninitialised (tree decl, const char *name,
1493 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1494 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1496 enum
1498 asm_dest_common,
1499 asm_dest_bss,
1500 asm_dest_local
1502 destination = asm_dest_local;
1504 /* ??? We should handle .bss via select_section mechanisms rather than
1505 via special target hooks. That would eliminate this special case. */
1506 if (TREE_PUBLIC (decl))
1508 if (!DECL_COMMON (decl))
1509 #ifdef ASM_EMIT_BSS
1510 destination = asm_dest_bss;
1511 #else
1512 return false;
1513 #endif
1514 else
1515 destination = asm_dest_common;
1518 if (destination != asm_dest_common)
1520 resolve_unique_section (decl, 0, flag_data_sections);
1521 /* Custom sections don't belong here. */
1522 if (DECL_SECTION_NAME (decl))
1523 return false;
1526 if (destination == asm_dest_bss)
1527 globalize_decl (decl);
1529 if (flag_shared_data)
1531 switch (destination)
1533 #ifdef ASM_OUTPUT_SHARED_BSS
1534 case asm_dest_bss:
1535 ASM_OUTPUT_SHARED_BSS (asm_out_file, decl, name, size, rounded);
1536 return;
1537 #endif
1538 #ifdef ASM_OUTPUT_SHARED_COMMON
1539 case asm_dest_common:
1540 ASM_OUTPUT_SHARED_COMMON (asm_out_file, name, size, rounded);
1541 return;
1542 #endif
1543 #ifdef ASM_OUTPUT_SHARED_LOCAL
1544 case asm_dest_local:
1545 ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded);
1546 return;
1547 #endif
1548 default:
1549 break;
1553 switch (destination)
1555 #ifdef ASM_EMIT_BSS
1556 case asm_dest_bss:
1557 ASM_EMIT_BSS (decl, name, size, rounded);
1558 break;
1559 #endif
1560 case asm_dest_common:
1561 ASM_EMIT_COMMON (decl, name, size, rounded);
1562 break;
1563 case asm_dest_local:
1564 ASM_EMIT_LOCAL (decl, name, size, rounded);
1565 break;
1566 default:
1567 gcc_unreachable ();
1570 return true;
1573 /* Assemble everything that is needed for a variable or function declaration.
1574 Not used for automatic variables, and not used for function definitions.
1575 Should not be called for variables of incomplete structure type.
1577 TOP_LEVEL is nonzero if this variable has file scope.
1578 AT_END is nonzero if this is the special handling, at end of compilation,
1579 to define things that have had only tentative definitions.
1580 DONT_OUTPUT_DATA if nonzero means don't actually output the
1581 initial value (that will be done by the caller). */
1583 void
1584 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1585 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1587 const char *name;
1588 unsigned int align;
1589 int reloc = 0;
1590 rtx decl_rtl;
1592 if (lang_hooks.decls.prepare_assemble_variable)
1593 lang_hooks.decls.prepare_assemble_variable (decl);
1595 last_assemble_variable_decl = 0;
1597 /* Normally no need to say anything here for external references,
1598 since assemble_external is called by the language-specific code
1599 when a declaration is first seen. */
1601 if (DECL_EXTERNAL (decl))
1602 return;
1604 /* Output no assembler code for a function declaration.
1605 Only definitions of functions output anything. */
1607 if (TREE_CODE (decl) == FUNCTION_DECL)
1608 return;
1610 /* Do nothing for global register variables. */
1611 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1613 TREE_ASM_WRITTEN (decl) = 1;
1614 return;
1617 /* If type was incomplete when the variable was declared,
1618 see if it is complete now. */
1620 if (DECL_SIZE (decl) == 0)
1621 layout_decl (decl, 0);
1623 /* Still incomplete => don't allocate it; treat the tentative defn
1624 (which is what it must have been) as an `extern' reference. */
1626 if (!dont_output_data && DECL_SIZE (decl) == 0)
1628 error ("%Jstorage size of %qD isn%'t known", decl, decl);
1629 TREE_ASM_WRITTEN (decl) = 1;
1630 return;
1633 /* The first declaration of a variable that comes through this function
1634 decides whether it is global (in C, has external linkage)
1635 or local (in C, has internal linkage). So do nothing more
1636 if this function has already run. */
1638 if (TREE_ASM_WRITTEN (decl))
1639 return;
1641 /* Make sure targetm.encode_section_info is invoked before we set
1642 ASM_WRITTEN. */
1643 decl_rtl = DECL_RTL (decl);
1645 TREE_ASM_WRITTEN (decl) = 1;
1647 /* Do no output if -fsyntax-only. */
1648 if (flag_syntax_only)
1649 return;
1651 app_disable ();
1653 if (! dont_output_data
1654 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1656 error ("%Jsize of variable %qD is too large", decl, decl);
1657 return;
1660 name = XSTR (XEXP (decl_rtl, 0), 0);
1661 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1662 notice_global_symbol (decl);
1664 /* Compute the alignment of this data. */
1666 align = DECL_ALIGN (decl);
1668 /* In the case for initialing an array whose length isn't specified,
1669 where we have not yet been able to do the layout,
1670 figure out the proper alignment now. */
1671 if (dont_output_data && DECL_SIZE (decl) == 0
1672 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1673 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1675 /* Some object file formats have a maximum alignment which they support.
1676 In particular, a.out format supports a maximum alignment of 4. */
1677 if (align > MAX_OFILE_ALIGNMENT)
1679 warning ("%Jalignment of %qD is greater than maximum object "
1680 "file alignment. Using %d", decl, decl,
1681 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1682 align = MAX_OFILE_ALIGNMENT;
1685 /* On some machines, it is good to increase alignment sometimes. */
1686 if (! DECL_USER_ALIGN (decl))
1688 #ifdef DATA_ALIGNMENT
1689 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1690 #endif
1691 #ifdef CONSTANT_ALIGNMENT
1692 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1693 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1694 #endif
1697 /* Reset the alignment in case we have made it tighter, so we can benefit
1698 from it in get_pointer_alignment. */
1699 DECL_ALIGN (decl) = align;
1700 set_mem_align (decl_rtl, align);
1702 if (TREE_PUBLIC (decl))
1703 maybe_assemble_visibility (decl);
1705 if (DECL_PRESERVE_P (decl))
1706 targetm.asm_out.mark_decl_preserved (name);
1708 /* Handle uninitialized definitions. */
1710 /* If the decl has been given an explicit section name, then it
1711 isn't common, and shouldn't be handled as such. */
1712 if (DECL_SECTION_NAME (decl) || dont_output_data)
1714 /* We don't implement common thread-local data at present. */
1715 else if (DECL_THREAD_LOCAL (decl))
1717 if (DECL_COMMON (decl))
1718 sorry ("thread-local COMMON data not implemented");
1720 else if (DECL_INITIAL (decl) == 0
1721 || DECL_INITIAL (decl) == error_mark_node
1722 || (flag_zero_initialized_in_bss
1723 /* Leave constant zeroes in .rodata so they can be shared. */
1724 && !TREE_READONLY (decl)
1725 && initializer_zerop (DECL_INITIAL (decl))))
1727 unsigned HOST_WIDE_INT size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1728 unsigned HOST_WIDE_INT rounded = size;
1730 /* Don't allocate zero bytes of common,
1731 since that means "undefined external" in the linker. */
1732 if (size == 0)
1733 rounded = 1;
1735 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1736 so that each uninitialized object starts on such a boundary. */
1737 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1738 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1739 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1741 #if !defined(ASM_OUTPUT_ALIGNED_COMMON) && !defined(ASM_OUTPUT_ALIGNED_DECL_COMMON) && !defined(ASM_OUTPUT_ALIGNED_BSS)
1742 if ((unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1743 warning ("%Jrequested alignment for %qD is greater than "
1744 "implemented alignment of %d", decl, decl, rounded);
1745 #endif
1747 /* If the target cannot output uninitialized but not common global data
1748 in .bss, then we have to use .data, so fall through. */
1749 if (asm_emit_uninitialised (decl, name, size, rounded))
1750 return;
1753 /* Handle initialized definitions.
1754 Also handle uninitialized global definitions if -fno-common and the
1755 target doesn't support ASM_OUTPUT_BSS. */
1757 /* First make the assembler name(s) global if appropriate. */
1758 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1759 globalize_decl (decl);
1761 /* Output any data that we will need to use the address of. */
1762 if (DECL_INITIAL (decl) == error_mark_node)
1763 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1764 else if (DECL_INITIAL (decl))
1766 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1767 output_addressed_constants (DECL_INITIAL (decl));
1770 /* Switch to the appropriate section. */
1771 resolve_unique_section (decl, reloc, flag_data_sections);
1772 variable_section (decl, reloc);
1774 /* dbxout.c needs to know this. */
1775 if (in_text_section () || in_unlikely_text_section ())
1776 DECL_IN_TEXT_SECTION (decl) = 1;
1778 /* Output the alignment of this data. */
1779 if (align > BITS_PER_UNIT)
1780 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1782 /* Do any machine/system dependent processing of the object. */
1783 #ifdef ASM_DECLARE_OBJECT_NAME
1784 last_assemble_variable_decl = decl;
1785 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1786 #else
1787 /* Standard thing is just output label for the object. */
1788 ASM_OUTPUT_LABEL (asm_out_file, name);
1789 #endif /* ASM_DECLARE_OBJECT_NAME */
1791 if (!dont_output_data)
1793 if (DECL_INITIAL (decl)
1794 && DECL_INITIAL (decl) != error_mark_node
1795 && !initializer_zerop (DECL_INITIAL (decl)))
1796 /* Output the actual data. */
1797 output_constant (DECL_INITIAL (decl),
1798 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1799 align);
1800 else
1801 /* Leave space for it. */
1802 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1806 /* Return 1 if type TYPE contains any pointers. */
1808 static int
1809 contains_pointers_p (tree type)
1811 switch (TREE_CODE (type))
1813 case POINTER_TYPE:
1814 case REFERENCE_TYPE:
1815 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1816 so I'll play safe and return 1. */
1817 case OFFSET_TYPE:
1818 return 1;
1820 case RECORD_TYPE:
1821 case UNION_TYPE:
1822 case QUAL_UNION_TYPE:
1824 tree fields;
1825 /* For a type that has fields, see if the fields have pointers. */
1826 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1827 if (TREE_CODE (fields) == FIELD_DECL
1828 && contains_pointers_p (TREE_TYPE (fields)))
1829 return 1;
1830 return 0;
1833 case ARRAY_TYPE:
1834 /* An array type contains pointers if its element type does. */
1835 return contains_pointers_p (TREE_TYPE (type));
1837 default:
1838 return 0;
1842 /* In unit-at-a-time mode, we delay assemble_external processing until
1843 the compilation unit is finalized. This is the best we can do for
1844 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1845 it all the way to final. See PR 17982 for further discussion. */
1846 static GTY(()) tree pending_assemble_externals;
1848 #ifdef ASM_OUTPUT_EXTERNAL
1849 /* True if DECL is a function decl for which no out-of-line copy exists.
1850 It is assumed that DECL's assembler name has been set. */
1852 static bool
1853 incorporeal_function_p (tree decl)
1855 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1857 const char *name;
1859 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1860 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1861 return true;
1863 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1864 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1865 return true;
1867 return false;
1870 /* Actually do the tests to determine if this is necessary, and invoke
1871 ASM_OUTPUT_EXTERNAL. */
1872 static void
1873 assemble_external_real (tree decl)
1875 rtx rtl = DECL_RTL (decl);
1877 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1878 && !SYMBOL_REF_USED (XEXP (rtl, 0))
1879 && !incorporeal_function_p (decl))
1881 /* Some systems do require some output. */
1882 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1883 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1886 #endif
1888 void
1889 process_pending_assemble_externals (void)
1891 #ifdef ASM_OUTPUT_EXTERNAL
1892 tree list;
1893 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1894 assemble_external_real (TREE_VALUE (list));
1896 pending_assemble_externals = 0;
1897 #endif
1900 /* Output something to declare an external symbol to the assembler.
1901 (Most assemblers don't need this, so we normally output nothing.)
1902 Do nothing if DECL is not external. */
1904 void
1905 assemble_external (tree decl ATTRIBUTE_UNUSED)
1907 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1908 main body of this code is only rarely exercised. To provide some
1909 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1910 open. If it's not, we should not be calling this function. */
1911 gcc_assert (asm_out_file);
1913 #ifdef ASM_OUTPUT_EXTERNAL
1914 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1915 return;
1917 if (flag_unit_at_a_time)
1918 pending_assemble_externals = tree_cons (0, decl,
1919 pending_assemble_externals);
1920 else
1921 assemble_external_real (decl);
1922 #endif
1925 /* Similar, for calling a library function FUN. */
1927 void
1928 assemble_external_libcall (rtx fun)
1930 /* Declare library function name external when first used, if nec. */
1931 if (! SYMBOL_REF_USED (fun))
1933 SYMBOL_REF_USED (fun) = 1;
1934 targetm.asm_out.external_libcall (fun);
1938 /* Assemble a label named NAME. */
1940 void
1941 assemble_label (const char *name)
1943 ASM_OUTPUT_LABEL (asm_out_file, name);
1946 /* Set the symbol_referenced flag for ID. */
1947 void
1948 mark_referenced (tree id)
1950 TREE_SYMBOL_REFERENCED (id) = 1;
1953 /* Set the symbol_referenced flag for DECL and notify callgraph. */
1954 void
1955 mark_decl_referenced (tree decl)
1957 if (TREE_CODE (decl) == FUNCTION_DECL)
1959 /* Extern inline functions don't become needed when referenced. */
1960 if (!DECL_EXTERNAL (decl))
1961 cgraph_mark_needed_node (cgraph_node (decl));
1963 else if (TREE_CODE (decl) == VAR_DECL)
1965 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
1966 cgraph_varpool_mark_needed_node (node);
1967 /* C++ frontend use mark_decl_references to force COMDAT variables
1968 to be output that might appear dead otherwise. */
1969 node->force_output = true;
1971 /* else do nothing - we can get various sorts of CST nodes here,
1972 which do not need to be marked. */
1975 /* Output to FILE (an assembly file) a reference to NAME. If NAME
1976 starts with a *, the rest of NAME is output verbatim. Otherwise
1977 NAME is transformed in a target-specific way (usually by the
1978 addition of an underscore). */
1980 void
1981 assemble_name_raw (FILE *file, const char *name)
1983 if (name[0] == '*')
1984 fputs (&name[1], file);
1985 else
1986 ASM_OUTPUT_LABELREF (file, name);
1989 /* Like assemble_name_raw, but should be used when NAME might refer to
1990 an entity that is also represented as a tree (like a function or
1991 variable). If NAME does refer to such an entity, that entity will
1992 be marked as referenced. */
1994 void
1995 assemble_name (FILE *file, const char *name)
1997 const char *real_name;
1998 tree id;
2000 real_name = targetm.strip_name_encoding (name);
2002 id = maybe_get_identifier (real_name);
2003 if (id)
2004 mark_referenced (id);
2006 assemble_name_raw (file, name);
2009 /* Allocate SIZE bytes writable static space with a gensym name
2010 and return an RTX to refer to its address. */
2013 assemble_static_space (unsigned HOST_WIDE_INT size)
2015 char name[12];
2016 const char *namestring;
2017 rtx x;
2019 #if 0
2020 if (flag_shared_data)
2021 data_section ();
2022 #endif
2024 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2025 ++const_labelno;
2026 namestring = ggc_strdup (name);
2028 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2029 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2031 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2032 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2033 BIGGEST_ALIGNMENT);
2034 #else
2035 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2036 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2037 #else
2039 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2040 so that each uninitialized object starts on such a boundary. */
2041 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2042 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2043 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2044 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2045 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2046 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2048 #endif
2049 #endif
2050 return x;
2053 /* Assemble the static constant template for function entry trampolines.
2054 This is done at most once per compilation.
2055 Returns an RTX for the address of the template. */
2057 static GTY(()) rtx initial_trampoline;
2059 #ifdef TRAMPOLINE_TEMPLATE
2061 assemble_trampoline_template (void)
2063 char label[256];
2064 const char *name;
2065 int align;
2066 rtx symbol;
2068 if (initial_trampoline)
2069 return initial_trampoline;
2071 /* By default, put trampoline templates in read-only data section. */
2073 #ifdef TRAMPOLINE_SECTION
2074 TRAMPOLINE_SECTION ();
2075 #else
2076 readonly_data_section ();
2077 #endif
2079 /* Write the assembler code to define one. */
2080 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2081 if (align > 0)
2083 ASM_OUTPUT_ALIGN (asm_out_file, align);
2086 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2087 TRAMPOLINE_TEMPLATE (asm_out_file);
2089 /* Record the rtl to refer to it. */
2090 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2091 name = ggc_strdup (label);
2092 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2093 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2095 initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
2096 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2098 return initial_trampoline;
2100 #endif
2102 /* A and B are either alignments or offsets. Return the minimum alignment
2103 that may be assumed after adding the two together. */
2105 static inline unsigned
2106 min_align (unsigned int a, unsigned int b)
2108 return (a | b) & -(a | b);
2111 /* Return the assembler directive for creating a given kind of integer
2112 object. SIZE is the number of bytes in the object and ALIGNED_P
2113 indicates whether it is known to be aligned. Return NULL if the
2114 assembly dialect has no such directive.
2116 The returned string should be printed at the start of a new line and
2117 be followed immediately by the object's initial value. */
2119 const char *
2120 integer_asm_op (int size, int aligned_p)
2122 struct asm_int_op *ops;
2124 if (aligned_p)
2125 ops = &targetm.asm_out.aligned_op;
2126 else
2127 ops = &targetm.asm_out.unaligned_op;
2129 switch (size)
2131 case 1:
2132 return targetm.asm_out.byte_op;
2133 case 2:
2134 return ops->hi;
2135 case 4:
2136 return ops->si;
2137 case 8:
2138 return ops->di;
2139 case 16:
2140 return ops->ti;
2141 default:
2142 return NULL;
2146 /* Use directive OP to assemble an integer object X. Print OP at the
2147 start of the line, followed immediately by the value of X. */
2149 void
2150 assemble_integer_with_op (const char *op, rtx x)
2152 fputs (op, asm_out_file);
2153 output_addr_const (asm_out_file, x);
2154 fputc ('\n', asm_out_file);
2157 /* The default implementation of the asm_out.integer target hook. */
2159 bool
2160 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2161 unsigned int size ATTRIBUTE_UNUSED,
2162 int aligned_p ATTRIBUTE_UNUSED)
2164 const char *op = integer_asm_op (size, aligned_p);
2165 /* Avoid GAS bugs for large values. Specifically negative values whose
2166 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2167 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2168 return false;
2169 return op && (assemble_integer_with_op (op, x), true);
2172 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2173 the alignment of the integer in bits. Return 1 if we were able to output
2174 the constant, otherwise 0. If FORCE is nonzero, abort if we can't output
2175 the constant. */
2177 bool
2178 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2180 int aligned_p;
2182 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2184 /* See if the target hook can handle this kind of object. */
2185 if (targetm.asm_out.integer (x, size, aligned_p))
2186 return true;
2188 /* If the object is a multi-byte one, try splitting it up. Split
2189 it into words it if is multi-word, otherwise split it into bytes. */
2190 if (size > 1)
2192 enum machine_mode omode, imode;
2193 unsigned int subalign;
2194 unsigned int subsize, i;
2196 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2197 subalign = MIN (align, subsize * BITS_PER_UNIT);
2198 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
2199 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2201 for (i = 0; i < size; i += subsize)
2203 rtx partial = simplify_subreg (omode, x, imode, i);
2204 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2205 break;
2207 if (i == size)
2208 return true;
2210 /* If we've printed some of it, but not all of it, there's no going
2211 back now. */
2212 gcc_assert (!i);
2215 gcc_assert (!force);
2217 return false;
2220 void
2221 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2223 long data[4];
2224 int i;
2225 int bitsize, nelts, nunits, units_per;
2227 /* This is hairy. We have a quantity of known size. real_to_target
2228 will put it into an array of *host* longs, 32 bits per element
2229 (even if long is more than 32 bits). We need to determine the
2230 number of array elements that are occupied (nelts) and the number
2231 of *target* min-addressable units that will be occupied in the
2232 object file (nunits). We cannot assume that 32 divides the
2233 mode's bitsize (size * BITS_PER_UNIT) evenly.
2235 size * BITS_PER_UNIT is used here to make sure that padding bits
2236 (which might appear at either end of the value; real_to_target
2237 will include the padding bits in its output array) are included. */
2239 nunits = GET_MODE_SIZE (mode);
2240 bitsize = nunits * BITS_PER_UNIT;
2241 nelts = CEIL (bitsize, 32);
2242 units_per = 32 / BITS_PER_UNIT;
2244 real_to_target (data, &d, mode);
2246 /* Put out the first word with the specified alignment. */
2247 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2248 nunits -= units_per;
2250 /* Subsequent words need only 32-bit alignment. */
2251 align = min_align (align, 32);
2253 for (i = 1; i < nelts; i++)
2255 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2256 nunits -= units_per;
2260 /* Given an expression EXP with a constant value,
2261 reduce it to the sum of an assembler symbol and an integer.
2262 Store them both in the structure *VALUE.
2263 Abort if EXP does not reduce. */
2265 struct addr_const GTY(())
2267 rtx base;
2268 HOST_WIDE_INT offset;
2271 static void
2272 decode_addr_const (tree exp, struct addr_const *value)
2274 tree target = TREE_OPERAND (exp, 0);
2275 int offset = 0;
2276 rtx x;
2278 while (1)
2280 if (TREE_CODE (target) == COMPONENT_REF
2281 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2284 offset += int_byte_position (TREE_OPERAND (target, 1));
2285 target = TREE_OPERAND (target, 0);
2287 else if (TREE_CODE (target) == ARRAY_REF
2288 || TREE_CODE (target) == ARRAY_RANGE_REF)
2290 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2291 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2292 target = TREE_OPERAND (target, 0);
2294 else
2295 break;
2298 switch (TREE_CODE (target))
2300 case VAR_DECL:
2301 case FUNCTION_DECL:
2302 x = DECL_RTL (target);
2303 break;
2305 case LABEL_DECL:
2306 x = gen_rtx_MEM (FUNCTION_MODE,
2307 gen_rtx_LABEL_REF (VOIDmode, force_label_rtx (target)));
2308 break;
2310 case REAL_CST:
2311 case STRING_CST:
2312 case COMPLEX_CST:
2313 case CONSTRUCTOR:
2314 case INTEGER_CST:
2315 x = output_constant_def (target, 1);
2316 break;
2318 default:
2319 gcc_unreachable ();
2322 gcc_assert (MEM_P (x));
2323 x = XEXP (x, 0);
2325 value->base = x;
2326 value->offset = offset;
2329 /* Uniquize all constants that appear in memory.
2330 Each constant in memory thus far output is recorded
2331 in `const_desc_table'. */
2333 struct constant_descriptor_tree GTY(())
2335 /* A MEM for the constant. */
2336 rtx rtl;
2338 /* The value of the constant. */
2339 tree value;
2342 static GTY((param_is (struct constant_descriptor_tree)))
2343 htab_t const_desc_htab;
2345 static struct constant_descriptor_tree * build_constant_desc (tree);
2346 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2348 /* Compute a hash code for a constant expression. */
2350 static hashval_t
2351 const_desc_hash (const void *ptr)
2353 return const_hash_1 (((struct constant_descriptor_tree *)ptr)->value);
2356 static hashval_t
2357 const_hash_1 (const tree exp)
2359 const char *p;
2360 hashval_t hi;
2361 int len, i;
2362 enum tree_code code = TREE_CODE (exp);
2364 /* Either set P and LEN to the address and len of something to hash and
2365 exit the switch or return a value. */
2367 switch (code)
2369 case INTEGER_CST:
2370 p = (char *) &TREE_INT_CST (exp);
2371 len = sizeof TREE_INT_CST (exp);
2372 break;
2374 case REAL_CST:
2375 return real_hash (TREE_REAL_CST_PTR (exp));
2377 case STRING_CST:
2378 p = TREE_STRING_POINTER (exp);
2379 len = TREE_STRING_LENGTH (exp);
2380 break;
2382 case COMPLEX_CST:
2383 return (const_hash_1 (TREE_REALPART (exp)) * 5
2384 + const_hash_1 (TREE_IMAGPART (exp)));
2386 case CONSTRUCTOR:
2388 tree link;
2390 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2392 for (link = CONSTRUCTOR_ELTS (exp); link; link = TREE_CHAIN (link))
2393 if (TREE_VALUE (link))
2394 hi = hi * 603 + const_hash_1 (TREE_VALUE (link));
2396 return hi;
2399 case ADDR_EXPR:
2400 case FDESC_EXPR:
2402 struct addr_const value;
2404 decode_addr_const (exp, &value);
2405 switch (GET_CODE (value.base))
2407 case SYMBOL_REF:
2408 /* Don't hash the address of the SYMBOL_REF;
2409 only use the offset and the symbol name. */
2410 hi = value.offset;
2411 p = XSTR (value.base, 0);
2412 for (i = 0; p[i] != 0; i++)
2413 hi = ((hi * 613) + (unsigned) (p[i]));
2414 break;
2416 case LABEL_REF:
2417 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2418 break;
2420 default:
2421 gcc_unreachable ();
2424 return hi;
2426 case PLUS_EXPR:
2427 case MINUS_EXPR:
2428 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2429 + const_hash_1 (TREE_OPERAND (exp, 1)));
2431 case NOP_EXPR:
2432 case CONVERT_EXPR:
2433 case NON_LVALUE_EXPR:
2434 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2436 default:
2437 /* A language specific constant. Just hash the code. */
2438 return code;
2441 /* Compute hashing function. */
2442 hi = len;
2443 for (i = 0; i < len; i++)
2444 hi = ((hi * 613) + (unsigned) (p[i]));
2446 return hi;
2449 /* Wrapper of compare_constant, for the htab interface. */
2450 static int
2451 const_desc_eq (const void *p1, const void *p2)
2453 return compare_constant (((struct constant_descriptor_tree *)p1)->value,
2454 ((struct constant_descriptor_tree *)p2)->value);
2457 /* Compare t1 and t2, and return 1 only if they are known to result in
2458 the same bit pattern on output. */
2460 static int
2461 compare_constant (const tree t1, const tree t2)
2463 enum tree_code typecode;
2465 if (t1 == NULL_TREE)
2466 return t2 == NULL_TREE;
2467 if (t2 == NULL_TREE)
2468 return 0;
2470 if (TREE_CODE (t1) != TREE_CODE (t2))
2471 return 0;
2473 switch (TREE_CODE (t1))
2475 case INTEGER_CST:
2476 /* Integer constants are the same only if the same width of type. */
2477 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2478 return 0;
2479 return tree_int_cst_equal (t1, t2);
2481 case REAL_CST:
2482 /* Real constants are the same only if the same width of type. */
2483 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2484 return 0;
2486 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2488 case STRING_CST:
2489 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2490 return 0;
2492 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2493 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2494 TREE_STRING_LENGTH (t1)));
2496 case COMPLEX_CST:
2497 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2498 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2500 case CONSTRUCTOR:
2502 tree l1, l2;
2504 typecode = TREE_CODE (TREE_TYPE (t1));
2505 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2506 return 0;
2508 if (typecode == ARRAY_TYPE)
2510 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2511 /* For arrays, check that the sizes all match. */
2512 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2513 || size_1 == -1
2514 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2515 return 0;
2517 else
2519 /* For record and union constructors, require exact type
2520 equality. */
2521 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2522 return 0;
2525 for (l1 = CONSTRUCTOR_ELTS (t1), l2 = CONSTRUCTOR_ELTS (t2);
2526 l1 && l2;
2527 l1 = TREE_CHAIN (l1), l2 = TREE_CHAIN (l2))
2529 /* Check that each value is the same... */
2530 if (! compare_constant (TREE_VALUE (l1), TREE_VALUE (l2)))
2531 return 0;
2532 /* ... and that they apply to the same fields! */
2533 if (typecode == ARRAY_TYPE)
2535 if (! compare_constant (TREE_PURPOSE (l1),
2536 TREE_PURPOSE (l2)))
2537 return 0;
2539 else
2541 if (TREE_PURPOSE (l1) != TREE_PURPOSE (l2))
2542 return 0;
2546 return l1 == NULL_TREE && l2 == NULL_TREE;
2549 case ADDR_EXPR:
2550 case FDESC_EXPR:
2552 struct addr_const value1, value2;
2554 decode_addr_const (t1, &value1);
2555 decode_addr_const (t2, &value2);
2556 return (value1.offset == value2.offset
2557 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2560 case PLUS_EXPR:
2561 case MINUS_EXPR:
2562 case RANGE_EXPR:
2563 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2564 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2566 case NOP_EXPR:
2567 case CONVERT_EXPR:
2568 case NON_LVALUE_EXPR:
2569 case VIEW_CONVERT_EXPR:
2570 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2572 default:
2574 tree nt1, nt2;
2575 nt1 = lang_hooks.expand_constant (t1);
2576 nt2 = lang_hooks.expand_constant (t2);
2577 if (nt1 != t1 || nt2 != t2)
2578 return compare_constant (nt1, nt2);
2579 else
2580 return 0;
2584 gcc_unreachable ();
2587 /* Make a copy of the whole tree structure for a constant. This
2588 handles the same types of nodes that compare_constant handles. */
2590 static tree
2591 copy_constant (tree exp)
2593 switch (TREE_CODE (exp))
2595 case ADDR_EXPR:
2596 /* For ADDR_EXPR, we do not want to copy the decl whose address
2597 is requested. We do want to copy constants though. */
2598 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2599 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2600 copy_constant (TREE_OPERAND (exp, 0)));
2601 else
2602 return copy_node (exp);
2604 case INTEGER_CST:
2605 case REAL_CST:
2606 case STRING_CST:
2607 return copy_node (exp);
2609 case COMPLEX_CST:
2610 return build_complex (TREE_TYPE (exp),
2611 copy_constant (TREE_REALPART (exp)),
2612 copy_constant (TREE_IMAGPART (exp)));
2614 case PLUS_EXPR:
2615 case MINUS_EXPR:
2616 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2617 copy_constant (TREE_OPERAND (exp, 0)),
2618 copy_constant (TREE_OPERAND (exp, 1)));
2620 case NOP_EXPR:
2621 case CONVERT_EXPR:
2622 case NON_LVALUE_EXPR:
2623 case VIEW_CONVERT_EXPR:
2624 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2625 copy_constant (TREE_OPERAND (exp, 0)));
2627 case CONSTRUCTOR:
2629 tree copy = copy_node (exp);
2630 tree list = copy_list (CONSTRUCTOR_ELTS (exp));
2631 tree tail;
2633 CONSTRUCTOR_ELTS (copy) = list;
2634 for (tail = list; tail; tail = TREE_CHAIN (tail))
2635 TREE_VALUE (tail) = copy_constant (TREE_VALUE (tail));
2637 return copy;
2640 default:
2642 tree t = lang_hooks.expand_constant (exp);
2644 gcc_assert (t == exp);
2645 return copy_constant (t);
2650 /* Subroutine of output_constant_def:
2651 No constant equal to EXP is known to have been output.
2652 Make a constant descriptor to enter EXP in the hash table.
2653 Assign the label number and construct RTL to refer to the
2654 constant's location in memory.
2655 Caller is responsible for updating the hash table. */
2657 static struct constant_descriptor_tree *
2658 build_constant_desc (tree exp)
2660 rtx symbol;
2661 rtx rtl;
2662 char label[256];
2663 int labelno;
2664 struct constant_descriptor_tree *desc;
2666 desc = ggc_alloc (sizeof (*desc));
2667 desc->value = copy_constant (exp);
2669 /* Propagate marked-ness to copied constant. */
2670 if (flag_mudflap && mf_marked_p (exp))
2671 mf_mark (desc->value);
2673 /* Create a string containing the label name, in LABEL. */
2674 labelno = const_labelno++;
2675 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2677 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
2678 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2679 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2680 SYMBOL_REF_DECL (symbol) = desc->value;
2681 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2683 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2684 set_mem_attributes (rtl, exp, 1);
2685 set_mem_alias_set (rtl, 0);
2686 set_mem_alias_set (rtl, const_alias_set);
2688 /* Set flags or add text to the name to record information, such as
2689 that it is a local symbol. If the name is changed, the macro
2690 ASM_OUTPUT_LABELREF will have to know how to strip this
2691 information. This call might invalidate our local variable
2692 SYMBOL; we can't use it afterward. */
2694 targetm.encode_section_info (exp, rtl, true);
2696 desc->rtl = rtl;
2698 return desc;
2701 /* Return an rtx representing a reference to constant data in memory
2702 for the constant expression EXP.
2704 If assembler code for such a constant has already been output,
2705 return an rtx to refer to it.
2706 Otherwise, output such a constant in memory
2707 and generate an rtx for it.
2709 If DEFER is nonzero, this constant can be deferred and output only
2710 if referenced in the function after all optimizations.
2712 `const_desc_table' records which constants already have label strings. */
2715 output_constant_def (tree exp, int defer)
2717 struct constant_descriptor_tree *desc;
2718 struct constant_descriptor_tree key;
2719 void **loc;
2721 /* Look up EXP in the table of constant descriptors. If we didn't find
2722 it, create a new one. */
2723 key.value = exp;
2724 loc = htab_find_slot (const_desc_htab, &key, INSERT);
2726 desc = *loc;
2727 if (desc == 0)
2729 desc = build_constant_desc (exp);
2730 *loc = desc;
2733 maybe_output_constant_def_contents (desc, defer);
2734 return desc->rtl;
2737 /* Subroutine of output_constant_def: Decide whether or not we need to
2738 output the constant DESC now, and if so, do it. */
2739 static void
2740 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2741 int defer)
2743 rtx symbol = XEXP (desc->rtl, 0);
2744 tree exp = desc->value;
2746 if (flag_syntax_only)
2747 return;
2749 if (TREE_ASM_WRITTEN (exp))
2750 /* Already output; don't do it again. */
2751 return;
2753 /* We can always defer constants as long as the context allows
2754 doing so. */
2755 if (defer)
2757 /* Increment n_deferred_constants if it exists. It needs to be at
2758 least as large as the number of constants actually referred to
2759 by the function. If it's too small we'll stop looking too early
2760 and fail to emit constants; if it's too large we'll only look
2761 through the entire function when we could have stopped earlier. */
2762 if (cfun)
2763 n_deferred_constants++;
2764 return;
2767 output_constant_def_contents (symbol);
2770 /* We must output the constant data referred to by SYMBOL; do so. */
2772 static void
2773 output_constant_def_contents (rtx symbol)
2775 tree exp = SYMBOL_REF_DECL (symbol);
2776 const char *label = XSTR (symbol, 0);
2777 HOST_WIDE_INT size;
2779 /* Make sure any other constants whose addresses appear in EXP
2780 are assigned label numbers. */
2781 int reloc = compute_reloc_for_constant (exp);
2783 /* Align the location counter as required by EXP's data type. */
2784 unsigned int align = TYPE_ALIGN (TREE_TYPE (exp));
2785 #ifdef CONSTANT_ALIGNMENT
2786 align = CONSTANT_ALIGNMENT (exp, align);
2787 #endif
2789 output_addressed_constants (exp);
2791 /* We are no longer deferring this constant. */
2792 TREE_ASM_WRITTEN (exp) = 1;
2794 if (IN_NAMED_SECTION (exp))
2795 named_section (exp, NULL, reloc);
2796 else
2797 targetm.asm_out.select_section (exp, reloc, align);
2799 if (align > BITS_PER_UNIT)
2801 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2804 size = int_size_in_bytes (TREE_TYPE (exp));
2805 if (TREE_CODE (exp) == STRING_CST)
2806 size = MAX (TREE_STRING_LENGTH (exp), size);
2808 /* Do any machine/system dependent processing of the constant. */
2809 #ifdef ASM_DECLARE_CONSTANT_NAME
2810 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2811 #else
2812 /* Standard thing is just output label for the constant. */
2813 ASM_OUTPUT_LABEL (asm_out_file, label);
2814 #endif /* ASM_DECLARE_CONSTANT_NAME */
2816 /* Output the value of EXP. */
2817 output_constant (exp, size, align);
2818 if (flag_mudflap)
2819 mudflap_enqueue_constant (exp);
2822 /* Look up EXP in the table of constant descriptors. Return the rtl
2823 if it has been emitted, else null. */
2826 lookup_constant_def (tree exp)
2828 struct constant_descriptor_tree *desc;
2829 struct constant_descriptor_tree key;
2831 key.value = exp;
2832 desc = htab_find (const_desc_htab, &key);
2834 return (desc ? desc->rtl : NULL_RTX);
2837 /* Used in the hash tables to avoid outputting the same constant
2838 twice. Unlike 'struct constant_descriptor_tree', RTX constants
2839 are output once per function, not once per file. */
2840 /* ??? Only a few targets need per-function constant pools. Most
2841 can use one per-file pool. Should add a targetm bit to tell the
2842 difference. */
2844 struct rtx_constant_pool GTY(())
2846 /* Pointers to first and last constant in pool, as ordered by offset. */
2847 struct constant_descriptor_rtx *first;
2848 struct constant_descriptor_rtx *last;
2850 /* Hash facility for making memory-constants from constant rtl-expressions.
2851 It is used on RISC machines where immediate integer arguments and
2852 constant addresses are restricted so that such constants must be stored
2853 in memory. */
2854 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
2855 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_sym_htab;
2857 /* Current offset in constant pool (does not include any
2858 machine-specific header). */
2859 HOST_WIDE_INT offset;
2862 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
2864 struct constant_descriptor_rtx *next;
2865 rtx mem;
2866 rtx sym;
2867 rtx constant;
2868 HOST_WIDE_INT offset;
2869 hashval_t hash;
2870 enum machine_mode mode;
2871 unsigned int align;
2872 int labelno;
2873 int mark;
2876 /* Hash and compare functions for const_rtx_htab. */
2878 static hashval_t
2879 const_desc_rtx_hash (const void *ptr)
2881 const struct constant_descriptor_rtx *desc = ptr;
2882 return desc->hash;
2885 static int
2886 const_desc_rtx_eq (const void *a, const void *b)
2888 const struct constant_descriptor_rtx *x = a;
2889 const struct constant_descriptor_rtx *y = b;
2891 if (x->mode != y->mode)
2892 return 0;
2893 return rtx_equal_p (x->constant, y->constant);
2896 /* Hash and compare functions for const_rtx_sym_htab. */
2898 static hashval_t
2899 const_desc_rtx_sym_hash (const void *ptr)
2901 const struct constant_descriptor_rtx *desc = ptr;
2902 return htab_hash_string (XSTR (desc->sym, 0));
2905 static int
2906 const_desc_rtx_sym_eq (const void *a, const void *b)
2908 const struct constant_descriptor_rtx *x = a;
2909 const struct constant_descriptor_rtx *y = b;
2910 return XSTR (x->sym, 0) == XSTR (y->sym, 0);
2913 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
2915 static int
2916 const_rtx_hash_1 (rtx *xp, void *data)
2918 unsigned HOST_WIDE_INT hwi;
2919 enum machine_mode mode;
2920 enum rtx_code code;
2921 hashval_t h, *hp;
2922 rtx x;
2924 x = *xp;
2925 code = GET_CODE (x);
2926 mode = GET_MODE (x);
2927 h = (hashval_t) code * 1048573 + mode;
2929 switch (code)
2931 case CONST_INT:
2932 hwi = INTVAL (x);
2933 fold_hwi:
2935 const int shift = sizeof (hashval_t) * CHAR_BIT;
2936 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
2937 int i;
2939 h ^= (hashval_t) hwi;
2940 for (i = 1; i < n; ++i)
2942 hwi >>= shift;
2943 h ^= (hashval_t) hwi;
2946 break;
2948 case CONST_DOUBLE:
2949 if (mode == VOIDmode)
2951 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
2952 goto fold_hwi;
2954 else
2955 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
2956 break;
2958 case CONST_VECTOR:
2960 int i;
2961 for (i = XVECLEN (x, 0); i-- > 0; )
2962 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
2964 break;
2966 case SYMBOL_REF:
2967 h ^= htab_hash_string (XSTR (x, 0));
2968 break;
2970 case LABEL_REF:
2971 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
2972 break;
2974 case UNSPEC:
2975 case UNSPEC_VOLATILE:
2976 h = h * 251 + XINT (x, 1);
2977 break;
2979 default:
2980 break;
2983 hp = data;
2984 *hp = *hp * 509 + h;
2985 return 0;
2988 /* Compute a hash value for X, which should be a constant. */
2990 static hashval_t
2991 const_rtx_hash (rtx x)
2993 hashval_t h = 0;
2994 for_each_rtx (&x, const_rtx_hash_1, &h);
2995 return h;
2999 /* Initialize constant pool hashing for a new function. */
3001 void
3002 init_varasm_status (struct function *f)
3004 struct varasm_status *p;
3005 struct rtx_constant_pool *pool;
3007 p = ggc_alloc (sizeof (struct varasm_status));
3008 f->varasm = p;
3010 pool = ggc_alloc (sizeof (struct rtx_constant_pool));
3011 p->pool = pool;
3012 p->deferred_constants = 0;
3014 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
3015 const_desc_rtx_eq, NULL);
3016 pool->const_rtx_sym_htab = htab_create_ggc (31, const_desc_rtx_sym_hash,
3017 const_desc_rtx_sym_eq, NULL);
3018 pool->first = pool->last = NULL;
3019 pool->offset = 0;
3022 /* Given a MINUS expression, simplify it if both sides
3023 include the same symbol. */
3026 simplify_subtraction (rtx x)
3028 rtx r = simplify_rtx (x);
3029 return r ? r : x;
3032 /* Given a constant rtx X, make (or find) a memory constant for its value
3033 and return a MEM rtx to refer to it in memory. */
3036 force_const_mem (enum machine_mode mode, rtx x)
3038 struct constant_descriptor_rtx *desc, tmp;
3039 struct rtx_constant_pool *pool = cfun->varasm->pool;
3040 char label[256];
3041 rtx def, symbol;
3042 hashval_t hash;
3043 unsigned int align;
3044 void **slot;
3046 /* If we're not allowed to drop X into the constant pool, don't. */
3047 if (targetm.cannot_force_const_mem (x))
3048 return NULL_RTX;
3050 /* Lookup the value in the hashtable. */
3051 tmp.constant = x;
3052 tmp.mode = mode;
3053 hash = const_rtx_hash (x);
3054 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
3055 desc = *slot;
3057 /* If the constant was already present, return its memory. */
3058 if (desc)
3059 return copy_rtx (desc->mem);
3061 /* Otherwise, create a new descriptor. */
3062 desc = ggc_alloc (sizeof (*desc));
3063 *slot = desc;
3065 /* Align the location counter as required by EXP's data type. */
3066 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3067 #ifdef CONSTANT_ALIGNMENT
3069 tree type = lang_hooks.types.type_for_mode (mode, 0);
3070 if (type != NULL_TREE)
3071 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3073 #endif
3075 pool->offset += (align / BITS_PER_UNIT) - 1;
3076 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3078 desc->next = NULL;
3079 desc->constant = tmp.constant;
3080 desc->offset = pool->offset;
3081 desc->hash = hash;
3082 desc->mode = mode;
3083 desc->align = align;
3084 desc->labelno = const_labelno;
3085 desc->mark = 0;
3087 pool->offset += GET_MODE_SIZE (mode);
3088 if (pool->last)
3089 pool->last->next = desc;
3090 else
3091 pool->first = pool->last = desc;
3092 pool->last = desc;
3094 /* Create a string containing the label name, in LABEL. */
3095 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3096 ++const_labelno;
3098 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3099 the constants pool. */
3100 desc->sym = symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3101 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
3102 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3103 current_function_uses_const_pool = 1;
3105 /* Insert the descriptor into the symbol cross-reference table too. */
3106 slot = htab_find_slot (pool->const_rtx_sym_htab, desc, INSERT);
3107 gcc_assert (!*slot);
3108 *slot = desc;
3110 /* Construct the MEM. */
3111 desc->mem = def = gen_const_mem (mode, symbol);
3112 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3113 set_mem_align (def, align);
3115 /* If we're dropping a label to the constant pool, make sure we
3116 don't delete it. */
3117 if (GET_CODE (x) == LABEL_REF)
3118 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3120 return copy_rtx (def);
3123 /* Given a SYMBOL_REF with CONSTANT_POOL_ADDRESS_P true, return a pointer to
3124 the corresponding constant_descriptor_rtx structure. */
3126 static struct constant_descriptor_rtx *
3127 find_pool_constant (struct rtx_constant_pool *pool, rtx sym)
3129 struct constant_descriptor_rtx tmp;
3130 tmp.sym = sym;
3131 return htab_find (pool->const_rtx_sym_htab, &tmp);
3134 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3137 get_pool_constant (rtx addr)
3139 return find_pool_constant (cfun->varasm->pool, addr)->constant;
3142 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3143 and whether it has been output or not. */
3146 get_pool_constant_mark (rtx addr, bool *pmarked)
3148 struct constant_descriptor_rtx *desc;
3150 desc = find_pool_constant (cfun->varasm->pool, addr);
3151 *pmarked = (desc->mark != 0);
3152 return desc->constant;
3155 /* Likewise, but for the constant pool of a specific function. */
3158 get_pool_constant_for_function (struct function *f, rtx addr)
3160 return find_pool_constant (f->varasm->pool, addr)->constant;
3163 /* Similar, return the mode. */
3165 enum machine_mode
3166 get_pool_mode (rtx addr)
3168 return find_pool_constant (cfun->varasm->pool, addr)->mode;
3171 /* Return the size of the constant pool. */
3174 get_pool_size (void)
3176 return cfun->varasm->pool->offset;
3179 /* Worker function for output_constant_pool_1. Emit assembly for X
3180 in MODE with known alignment ALIGN. */
3182 static void
3183 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3185 switch (GET_MODE_CLASS (mode))
3187 case MODE_FLOAT:
3189 REAL_VALUE_TYPE r;
3191 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3192 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3193 assemble_real (r, mode, align);
3194 break;
3197 case MODE_INT:
3198 case MODE_PARTIAL_INT:
3199 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3200 break;
3202 case MODE_VECTOR_FLOAT:
3203 case MODE_VECTOR_INT:
3205 int i, units;
3206 enum machine_mode submode = GET_MODE_INNER (mode);
3207 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3209 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3210 units = CONST_VECTOR_NUNITS (x);
3212 for (i = 0; i < units; i++)
3214 rtx elt = CONST_VECTOR_ELT (x, i);
3215 output_constant_pool_2 (submode, elt, i ? subalign : align);
3218 break;
3220 default:
3221 gcc_unreachable ();
3225 /* Worker function for output_constant_pool. Emit POOL. */
3227 static void
3228 output_constant_pool_1 (struct constant_descriptor_rtx *desc)
3230 rtx x, tmp;
3232 if (!desc->mark)
3233 return;
3234 x = desc->constant;
3236 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3237 whose CODE_LABEL has been deleted. This can occur if a jump table
3238 is eliminated by optimization. If so, write a constant of zero
3239 instead. Note that this can also happen by turning the
3240 CODE_LABEL into a NOTE. */
3241 /* ??? This seems completely and utterly wrong. Certainly it's
3242 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3243 functioning even with INSN_DELETED_P and friends. */
3245 tmp = x;
3246 switch (GET_CODE (x))
3248 case CONST:
3249 if (GET_CODE (XEXP (x, 0)) != PLUS
3250 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3251 break;
3252 tmp = XEXP (XEXP (x, 0), 0);
3253 /* FALLTHRU */
3255 case LABEL_REF:
3256 tmp = XEXP (x, 0);
3257 gcc_assert (!INSN_DELETED_P (tmp));
3258 gcc_assert (!NOTE_P (tmp)
3259 || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3260 break;
3262 default:
3263 break;
3266 /* First switch to correct section. */
3267 targetm.asm_out.select_rtx_section (desc->mode, x, desc->align);
3269 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3270 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3271 desc->align, desc->labelno, done);
3272 #endif
3274 assemble_align (desc->align);
3276 /* Output the label. */
3277 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3279 /* Output the data. */
3280 output_constant_pool_2 (desc->mode, x, desc->align);
3282 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3283 sections have proper size. */
3284 if (desc->align > GET_MODE_BITSIZE (desc->mode)
3285 && in_section == in_named
3286 && get_named_section_flags (in_named_name) & SECTION_MERGE)
3287 assemble_align (desc->align);
3289 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3290 done:
3291 #endif
3292 return;
3295 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3296 to as used. Emit referenced deferred strings. This function can
3297 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3299 static int
3300 mark_constant (rtx *current_rtx, void *data)
3302 struct rtx_constant_pool *pool = data;
3303 rtx x = *current_rtx;
3305 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3306 return 0;
3308 if (CONSTANT_POOL_ADDRESS_P (x))
3310 struct constant_descriptor_rtx *desc = find_pool_constant (pool, x);
3311 if (desc->mark == 0)
3313 desc->mark = 1;
3314 for_each_rtx (&desc->constant, mark_constant, pool);
3317 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3319 tree exp = SYMBOL_REF_DECL (x);
3320 if (!TREE_ASM_WRITTEN (exp))
3322 n_deferred_constants--;
3323 output_constant_def_contents (x);
3327 return -1;
3330 /* Look through appropriate parts of INSN, marking all entries in the
3331 constant pool which are actually being used. Entries that are only
3332 referenced by other constants are also marked as used. Emit
3333 deferred strings that are used. */
3335 static void
3336 mark_constants (struct rtx_constant_pool *pool, rtx insn)
3338 if (!INSN_P (insn))
3339 return;
3341 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3342 insns, not any notes that may be attached. We don't want to mark
3343 a constant just because it happens to appear in a REG_EQUIV note. */
3344 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3346 rtx seq = PATTERN (insn);
3347 int i, n = XVECLEN (seq, 0);
3348 for (i = 0; i < n; ++i)
3350 rtx subinsn = XVECEXP (seq, 0, i);
3351 if (INSN_P (subinsn))
3352 for_each_rtx (&PATTERN (subinsn), mark_constant, pool);
3355 else
3356 for_each_rtx (&PATTERN (insn), mark_constant, pool);
3359 /* Look through the instructions for this function, and mark all the
3360 entries in POOL which are actually being used. Emit deferred constants
3361 which have indeed been used. */
3363 static void
3364 mark_constant_pool (struct rtx_constant_pool *pool)
3366 rtx insn, link;
3368 if (pool->first == 0 && n_deferred_constants == 0)
3369 return;
3371 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3372 mark_constants (pool, insn);
3374 for (link = current_function_epilogue_delay_list;
3375 link;
3376 link = XEXP (link, 1))
3377 mark_constants (pool, XEXP (link, 0));
3380 /* Write all the constants in the constant pool. */
3382 void
3383 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3384 tree fndecl ATTRIBUTE_UNUSED)
3386 struct rtx_constant_pool *pool = cfun->varasm->pool;
3387 struct constant_descriptor_rtx *desc;
3389 /* It is possible for gcc to call force_const_mem and then to later
3390 discard the instructions which refer to the constant. In such a
3391 case we do not need to output the constant. */
3392 mark_constant_pool (pool);
3394 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3395 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3396 #endif
3398 for (desc = pool->first; desc ; desc = desc->next)
3399 output_constant_pool_1 (desc);
3401 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3402 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3403 #endif
3406 /* Determine what kind of relocations EXP may need. */
3409 compute_reloc_for_constant (tree exp)
3411 int reloc = 0, reloc2;
3412 tree tem;
3414 /* Give the front-end a chance to convert VALUE to something that
3415 looks more like a constant to the back-end. */
3416 exp = lang_hooks.expand_constant (exp);
3418 switch (TREE_CODE (exp))
3420 case ADDR_EXPR:
3421 case FDESC_EXPR:
3422 /* Go inside any operations that get_inner_reference can handle and see
3423 if what's inside is a constant: no need to do anything here for
3424 addresses of variables or functions. */
3425 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3426 tem = TREE_OPERAND (tem, 0))
3429 if (TREE_PUBLIC (tem))
3430 reloc |= 2;
3431 else
3432 reloc |= 1;
3433 break;
3435 case PLUS_EXPR:
3436 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3437 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3438 break;
3440 case MINUS_EXPR:
3441 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3442 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3443 /* The difference of two local labels is computable at link time. */
3444 if (reloc == 1 && reloc2 == 1)
3445 reloc = 0;
3446 else
3447 reloc |= reloc2;
3448 break;
3450 case NOP_EXPR:
3451 case CONVERT_EXPR:
3452 case NON_LVALUE_EXPR:
3453 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3454 break;
3456 case CONSTRUCTOR:
3457 for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3458 if (TREE_VALUE (tem) != 0)
3459 reloc |= compute_reloc_for_constant (TREE_VALUE (tem));
3461 break;
3463 default:
3464 break;
3466 return reloc;
3469 /* Find all the constants whose addresses are referenced inside of EXP,
3470 and make sure assembler code with a label has been output for each one.
3471 Indicate whether an ADDR_EXPR has been encountered. */
3473 static void
3474 output_addressed_constants (tree exp)
3476 tree tem;
3478 /* Give the front-end a chance to convert VALUE to something that
3479 looks more like a constant to the back-end. */
3480 exp = lang_hooks.expand_constant (exp);
3482 switch (TREE_CODE (exp))
3484 case ADDR_EXPR:
3485 case FDESC_EXPR:
3486 /* Go inside any operations that get_inner_reference can handle and see
3487 if what's inside is a constant: no need to do anything here for
3488 addresses of variables or functions. */
3489 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3490 tem = TREE_OPERAND (tem, 0))
3493 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3494 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3495 tem = DECL_INITIAL (tem);
3497 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3498 output_constant_def (tem, 0);
3499 break;
3501 case PLUS_EXPR:
3502 case MINUS_EXPR:
3503 output_addressed_constants (TREE_OPERAND (exp, 1));
3504 /* Fall through. */
3506 case NOP_EXPR:
3507 case CONVERT_EXPR:
3508 case NON_LVALUE_EXPR:
3509 output_addressed_constants (TREE_OPERAND (exp, 0));
3510 break;
3512 case CONSTRUCTOR:
3513 for (tem = CONSTRUCTOR_ELTS (exp); tem; tem = TREE_CHAIN (tem))
3514 if (TREE_VALUE (tem) != 0)
3515 output_addressed_constants (TREE_VALUE (tem));
3517 break;
3519 default:
3520 break;
3524 /* Return nonzero if VALUE is a valid constant-valued expression
3525 for use in initializing a static variable; one that can be an
3526 element of a "constant" initializer.
3528 Return null_pointer_node if the value is absolute;
3529 if it is relocatable, return the variable that determines the relocation.
3530 We assume that VALUE has been folded as much as possible;
3531 therefore, we do not need to check for such things as
3532 arithmetic-combinations of integers. */
3534 tree
3535 initializer_constant_valid_p (tree value, tree endtype)
3537 /* Give the front-end a chance to convert VALUE to something that
3538 looks more like a constant to the back-end. */
3539 value = lang_hooks.expand_constant (value);
3541 switch (TREE_CODE (value))
3543 case CONSTRUCTOR:
3544 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3545 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3546 && TREE_CONSTANT (value)
3547 && CONSTRUCTOR_ELTS (value))
3549 tree elt;
3550 bool absolute = true;
3552 for (elt = CONSTRUCTOR_ELTS (value); elt; elt = TREE_CHAIN (elt))
3554 tree reloc;
3555 value = TREE_VALUE (elt);
3556 reloc = initializer_constant_valid_p (value, TREE_TYPE (value));
3557 if (!reloc)
3558 return NULL_TREE;
3559 if (reloc != null_pointer_node)
3560 absolute = false;
3562 /* For a non-absolute relocation, there is no single
3563 variable that can be "the variable that determines the
3564 relocation." */
3565 return absolute ? null_pointer_node : error_mark_node;
3568 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3570 case INTEGER_CST:
3571 case VECTOR_CST:
3572 case REAL_CST:
3573 case STRING_CST:
3574 case COMPLEX_CST:
3575 return null_pointer_node;
3577 case ADDR_EXPR:
3578 case FDESC_EXPR:
3579 value = staticp (TREE_OPERAND (value, 0));
3580 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out to
3581 be a constant, this is old-skool offsetof-like nonsense. */
3582 if (value
3583 && TREE_CODE (value) == INDIRECT_REF
3584 && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3585 return null_pointer_node;
3586 /* Taking the address of a nested function involves a trampoline. */
3587 if (value
3588 && TREE_CODE (value) == FUNCTION_DECL
3589 && ((decl_function_context (value) && !DECL_NO_STATIC_CHAIN (value))
3590 || DECL_NON_ADDR_CONST_P (value)))
3591 return NULL_TREE;
3592 return value;
3594 case VIEW_CONVERT_EXPR:
3595 case NON_LVALUE_EXPR:
3596 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3598 case CONVERT_EXPR:
3599 case NOP_EXPR:
3601 tree src;
3602 tree src_type;
3603 tree dest_type;
3605 src = TREE_OPERAND (value, 0);
3606 src_type = TREE_TYPE (src);
3607 dest_type = TREE_TYPE (value);
3609 /* Allow conversions between pointer types, floating-point
3610 types, and offset types. */
3611 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3612 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3613 || (TREE_CODE (dest_type) == OFFSET_TYPE
3614 && TREE_CODE (src_type) == OFFSET_TYPE))
3615 return initializer_constant_valid_p (src, endtype);
3617 /* Allow length-preserving conversions between integer types. */
3618 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3619 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3620 return initializer_constant_valid_p (src, endtype);
3622 /* Allow conversions between other integer types only if
3623 explicit value. */
3624 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3626 tree inner = initializer_constant_valid_p (src, endtype);
3627 if (inner == null_pointer_node)
3628 return null_pointer_node;
3629 break;
3632 /* Allow (int) &foo provided int is as wide as a pointer. */
3633 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3634 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3635 return initializer_constant_valid_p (src, endtype);
3637 /* Likewise conversions from int to pointers, but also allow
3638 conversions from 0. */
3639 if ((POINTER_TYPE_P (dest_type)
3640 || TREE_CODE (dest_type) == OFFSET_TYPE)
3641 && INTEGRAL_TYPE_P (src_type))
3643 if (integer_zerop (src))
3644 return null_pointer_node;
3645 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3646 return initializer_constant_valid_p (src, endtype);
3649 /* Allow conversions to struct or union types if the value
3650 inside is okay. */
3651 if (TREE_CODE (dest_type) == RECORD_TYPE
3652 || TREE_CODE (dest_type) == UNION_TYPE)
3653 return initializer_constant_valid_p (src, endtype);
3655 break;
3657 case PLUS_EXPR:
3658 if (! INTEGRAL_TYPE_P (endtype)
3659 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3661 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3662 endtype);
3663 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3664 endtype);
3665 /* If either term is absolute, use the other terms relocation. */
3666 if (valid0 == null_pointer_node)
3667 return valid1;
3668 if (valid1 == null_pointer_node)
3669 return valid0;
3671 break;
3673 case MINUS_EXPR:
3674 if (! INTEGRAL_TYPE_P (endtype)
3675 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3677 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3678 endtype);
3679 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3680 endtype);
3681 /* Win if second argument is absolute. */
3682 if (valid1 == null_pointer_node)
3683 return valid0;
3684 /* Win if both arguments have the same relocation.
3685 Then the value is absolute. */
3686 if (valid0 == valid1 && valid0 != 0)
3687 return null_pointer_node;
3689 /* Since GCC guarantees that string constants are unique in the
3690 generated code, a subtraction between two copies of the same
3691 constant string is absolute. */
3692 if (valid0 && TREE_CODE (valid0) == STRING_CST
3693 && valid1 && TREE_CODE (valid1) == STRING_CST
3694 && operand_equal_p (valid0, valid1, 1))
3695 return null_pointer_node;
3698 /* Support narrowing differences. */
3699 if (INTEGRAL_TYPE_P (endtype))
3701 tree op0, op1;
3703 op0 = TREE_OPERAND (value, 0);
3704 op1 = TREE_OPERAND (value, 1);
3706 /* Like STRIP_NOPS except allow the operand mode to widen.
3707 This works around a feature of fold that simplifies
3708 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3709 that the narrower operation is cheaper. */
3711 while (TREE_CODE (op0) == NOP_EXPR
3712 || TREE_CODE (op0) == CONVERT_EXPR
3713 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3715 tree inner = TREE_OPERAND (op0, 0);
3716 if (inner == error_mark_node
3717 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3718 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3719 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3720 break;
3721 op0 = inner;
3724 while (TREE_CODE (op1) == NOP_EXPR
3725 || TREE_CODE (op1) == CONVERT_EXPR
3726 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3728 tree inner = TREE_OPERAND (op1, 0);
3729 if (inner == error_mark_node
3730 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3731 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3732 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3733 break;
3734 op1 = inner;
3737 op0 = initializer_constant_valid_p (op0, endtype);
3738 op1 = initializer_constant_valid_p (op1, endtype);
3740 /* Both initializers must be known. */
3741 if (op0 && op1)
3743 if (op0 == op1)
3744 return null_pointer_node;
3746 /* Support differences between labels. */
3747 if (TREE_CODE (op0) == LABEL_DECL
3748 && TREE_CODE (op1) == LABEL_DECL)
3749 return null_pointer_node;
3751 if (TREE_CODE (op0) == STRING_CST
3752 && TREE_CODE (op1) == STRING_CST
3753 && operand_equal_p (op0, op1, 1))
3754 return null_pointer_node;
3757 break;
3759 default:
3760 break;
3763 return 0;
3766 /* Output assembler code for constant EXP to FILE, with no label.
3767 This includes the pseudo-op such as ".int" or ".byte", and a newline.
3768 Assumes output_addressed_constants has been done on EXP already.
3770 Generate exactly SIZE bytes of assembler data, padding at the end
3771 with zeros if necessary. SIZE must always be specified.
3773 SIZE is important for structure constructors,
3774 since trailing members may have been omitted from the constructor.
3775 It is also important for initialization of arrays from string constants
3776 since the full length of the string constant might not be wanted.
3777 It is also needed for initialization of unions, where the initializer's
3778 type is just one member, and that may not be as long as the union.
3780 There a case in which we would fail to output exactly SIZE bytes:
3781 for a structure constructor that wants to produce more than SIZE bytes.
3782 But such constructors will never be generated for any possible input.
3784 ALIGN is the alignment of the data in bits. */
3786 void
3787 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3789 enum tree_code code;
3790 unsigned HOST_WIDE_INT thissize;
3792 /* Some front-ends use constants other than the standard language-independent
3793 varieties, but which may still be output directly. Give the front-end a
3794 chance to convert EXP to a language-independent representation. */
3795 exp = lang_hooks.expand_constant (exp);
3797 if (size == 0 || flag_syntax_only)
3798 return;
3800 /* Eliminate any conversions since we'll be outputting the underlying
3801 constant. */
3802 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
3803 || TREE_CODE (exp) == NON_LVALUE_EXPR
3804 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
3805 exp = TREE_OPERAND (exp, 0);
3807 code = TREE_CODE (TREE_TYPE (exp));
3808 thissize = int_size_in_bytes (TREE_TYPE (exp));
3810 /* Allow a constructor with no elements for any data type.
3811 This means to fill the space with zeros. */
3812 if (TREE_CODE (exp) == CONSTRUCTOR && CONSTRUCTOR_ELTS (exp) == 0)
3814 assemble_zeros (size);
3815 return;
3818 if (TREE_CODE (exp) == FDESC_EXPR)
3820 #ifdef ASM_OUTPUT_FDESC
3821 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
3822 tree decl = TREE_OPERAND (exp, 0);
3823 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
3824 #else
3825 gcc_unreachable ();
3826 #endif
3827 return;
3830 /* Now output the underlying data. If we've handling the padding, return.
3831 Otherwise, break and ensure SIZE is the size written. */
3832 switch (code)
3834 case CHAR_TYPE:
3835 case BOOLEAN_TYPE:
3836 case INTEGER_TYPE:
3837 case ENUMERAL_TYPE:
3838 case POINTER_TYPE:
3839 case REFERENCE_TYPE:
3840 case OFFSET_TYPE:
3841 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3842 EXPAND_INITIALIZER),
3843 MIN (size, thissize), align, 0))
3844 error ("initializer for integer value is too complicated");
3845 break;
3847 case REAL_TYPE:
3848 if (TREE_CODE (exp) != REAL_CST)
3849 error ("initializer for floating value is not a floating constant");
3851 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3852 break;
3854 case COMPLEX_TYPE:
3855 output_constant (TREE_REALPART (exp), thissize / 2, align);
3856 output_constant (TREE_IMAGPART (exp), thissize / 2,
3857 min_align (align, BITS_PER_UNIT * (thissize / 2)));
3858 break;
3860 case ARRAY_TYPE:
3861 case VECTOR_TYPE:
3862 switch (TREE_CODE (exp))
3864 case CONSTRUCTOR:
3865 output_constructor (exp, size, align);
3866 return;
3867 case STRING_CST:
3868 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3869 size);
3870 assemble_string (TREE_STRING_POINTER (exp), thissize);
3871 break;
3873 case VECTOR_CST:
3875 int elt_size;
3876 tree link;
3877 unsigned int nalign;
3878 enum machine_mode inner;
3880 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
3881 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3883 elt_size = GET_MODE_SIZE (inner);
3885 link = TREE_VECTOR_CST_ELTS (exp);
3886 output_constant (TREE_VALUE (link), elt_size, align);
3887 while ((link = TREE_CHAIN (link)) != NULL)
3888 output_constant (TREE_VALUE (link), elt_size, nalign);
3889 break;
3891 default:
3892 gcc_unreachable ();
3894 break;
3896 case RECORD_TYPE:
3897 case UNION_TYPE:
3898 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
3899 output_constructor (exp, size, align);
3900 return;
3902 case ERROR_MARK:
3903 return;
3905 default:
3906 gcc_unreachable ();
3909 if (size > thissize)
3910 assemble_zeros (size - thissize);
3914 /* Subroutine of output_constructor, used for computing the size of
3915 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
3916 type with an unspecified upper bound. */
3918 static unsigned HOST_WIDE_INT
3919 array_size_for_constructor (tree val)
3921 tree max_index, i;
3923 /* This code used to attempt to handle string constants that are not
3924 arrays of single-bytes, but nothing else does, so there's no point in
3925 doing it here. */
3926 if (TREE_CODE (val) == STRING_CST)
3927 return TREE_STRING_LENGTH (val);
3929 max_index = NULL_TREE;
3930 for (i = CONSTRUCTOR_ELTS (val); i; i = TREE_CHAIN (i))
3932 tree index = TREE_PURPOSE (i);
3934 if (TREE_CODE (index) == RANGE_EXPR)
3935 index = TREE_OPERAND (index, 1);
3936 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
3937 max_index = index;
3940 if (max_index == NULL_TREE)
3941 return 0;
3943 /* Compute the total number of array elements. */
3944 i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
3945 convert (sizetype,
3946 TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
3947 i = size_binop (PLUS_EXPR, i, convert (sizetype, integer_one_node));
3949 /* Multiply by the array element unit size to find number of bytes. */
3950 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
3952 return tree_low_cst (i, 1);
3955 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
3956 Generate at least SIZE bytes, padding if necessary. */
3958 static void
3959 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
3960 unsigned int align)
3962 tree type = TREE_TYPE (exp);
3963 tree link, field = 0;
3964 tree min_index = 0;
3965 /* Number of bytes output or skipped so far.
3966 In other words, current position within the constructor. */
3967 HOST_WIDE_INT total_bytes = 0;
3968 /* Nonzero means BYTE contains part of a byte, to be output. */
3969 int byte_buffer_in_use = 0;
3970 int byte = 0;
3972 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
3974 if (TREE_CODE (type) == RECORD_TYPE)
3975 field = TYPE_FIELDS (type);
3977 if (TREE_CODE (type) == ARRAY_TYPE
3978 && TYPE_DOMAIN (type) != 0)
3979 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3981 /* As LINK goes through the elements of the constant,
3982 FIELD goes through the structure fields, if the constant is a structure.
3983 if the constant is a union, then we override this,
3984 by getting the field from the TREE_LIST element.
3985 But the constant could also be an array. Then FIELD is zero.
3987 There is always a maximum of one element in the chain LINK for unions
3988 (even if the initializer in a source program incorrectly contains
3989 more one). */
3990 for (link = CONSTRUCTOR_ELTS (exp);
3991 link;
3992 link = TREE_CHAIN (link),
3993 field = field ? TREE_CHAIN (field) : 0)
3995 tree val = TREE_VALUE (link);
3996 tree index = 0;
3998 /* The element in a union constructor specifies the proper field
3999 or index. */
4000 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
4001 || TREE_CODE (type) == QUAL_UNION_TYPE)
4002 && TREE_PURPOSE (link) != 0)
4003 field = TREE_PURPOSE (link);
4005 else if (TREE_CODE (type) == ARRAY_TYPE)
4006 index = TREE_PURPOSE (link);
4008 #ifdef ASM_COMMENT_START
4009 if (field && flag_verbose_asm)
4010 fprintf (asm_out_file, "%s %s:\n",
4011 ASM_COMMENT_START,
4012 DECL_NAME (field)
4013 ? IDENTIFIER_POINTER (DECL_NAME (field))
4014 : "<anonymous>");
4015 #endif
4017 /* Eliminate the marker that makes a cast not be an lvalue. */
4018 if (val != 0)
4019 STRIP_NOPS (val);
4021 if (index && TREE_CODE (index) == RANGE_EXPR)
4023 unsigned HOST_WIDE_INT fieldsize
4024 = int_size_in_bytes (TREE_TYPE (type));
4025 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
4026 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
4027 HOST_WIDE_INT index;
4028 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
4030 for (index = lo_index; index <= hi_index; index++)
4032 /* Output the element's initial value. */
4033 if (val == 0)
4034 assemble_zeros (fieldsize);
4035 else
4036 output_constant (val, fieldsize, align2);
4038 /* Count its size. */
4039 total_bytes += fieldsize;
4042 else if (field == 0 || !DECL_BIT_FIELD (field))
4044 /* An element that is not a bit-field. */
4046 unsigned HOST_WIDE_INT fieldsize;
4047 /* Since this structure is static,
4048 we know the positions are constant. */
4049 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
4050 unsigned int align2;
4052 if (index != 0)
4053 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
4054 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
4056 /* Output any buffered-up bit-fields preceding this element. */
4057 if (byte_buffer_in_use)
4059 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4060 total_bytes++;
4061 byte_buffer_in_use = 0;
4064 /* Advance to offset of this element.
4065 Note no alignment needed in an array, since that is guaranteed
4066 if each element has the proper size. */
4067 if ((field != 0 || index != 0) && pos != total_bytes)
4069 assemble_zeros (pos - total_bytes);
4070 total_bytes = pos;
4073 /* Find the alignment of this element. */
4074 align2 = min_align (align, BITS_PER_UNIT * pos);
4076 /* Determine size this element should occupy. */
4077 if (field)
4079 fieldsize = 0;
4081 /* If this is an array with an unspecified upper bound,
4082 the initializer determines the size. */
4083 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4084 but we cannot do this until the deprecated support for
4085 initializing zero-length array members is removed. */
4086 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4087 && TYPE_DOMAIN (TREE_TYPE (field))
4088 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4090 fieldsize = array_size_for_constructor (val);
4091 /* Given a non-empty initialization, this field had
4092 better be last. */
4093 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4095 else if (DECL_SIZE_UNIT (field))
4097 /* ??? This can't be right. If the decl size overflows
4098 a host integer we will silently emit no data. */
4099 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4100 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4103 else
4104 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4106 /* Output the element's initial value. */
4107 if (val == 0)
4108 assemble_zeros (fieldsize);
4109 else
4110 output_constant (val, fieldsize, align2);
4112 /* Count its size. */
4113 total_bytes += fieldsize;
4115 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4116 error ("invalid initial value for member %qs",
4117 IDENTIFIER_POINTER (DECL_NAME (field)));
4118 else
4120 /* Element that is a bit-field. */
4122 HOST_WIDE_INT next_offset = int_bit_position (field);
4123 HOST_WIDE_INT end_offset
4124 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4126 if (val == 0)
4127 val = integer_zero_node;
4129 /* If this field does not start in this (or, next) byte,
4130 skip some bytes. */
4131 if (next_offset / BITS_PER_UNIT != total_bytes)
4133 /* Output remnant of any bit field in previous bytes. */
4134 if (byte_buffer_in_use)
4136 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4137 total_bytes++;
4138 byte_buffer_in_use = 0;
4141 /* If still not at proper byte, advance to there. */
4142 if (next_offset / BITS_PER_UNIT != total_bytes)
4144 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4145 total_bytes = next_offset / BITS_PER_UNIT;
4149 if (! byte_buffer_in_use)
4150 byte = 0;
4152 /* We must split the element into pieces that fall within
4153 separate bytes, and combine each byte with previous or
4154 following bit-fields. */
4156 /* next_offset is the offset n fbits from the beginning of
4157 the structure to the next bit of this element to be processed.
4158 end_offset is the offset of the first bit past the end of
4159 this element. */
4160 while (next_offset < end_offset)
4162 int this_time;
4163 int shift;
4164 HOST_WIDE_INT value;
4165 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4166 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4168 /* Advance from byte to byte
4169 within this element when necessary. */
4170 while (next_byte != total_bytes)
4172 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4173 total_bytes++;
4174 byte = 0;
4177 /* Number of bits we can process at once
4178 (all part of the same byte). */
4179 this_time = MIN (end_offset - next_offset,
4180 BITS_PER_UNIT - next_bit);
4181 if (BYTES_BIG_ENDIAN)
4183 /* On big-endian machine, take the most significant bits
4184 first (of the bits that are significant)
4185 and put them into bytes from the most significant end. */
4186 shift = end_offset - next_offset - this_time;
4188 /* Don't try to take a bunch of bits that cross
4189 the word boundary in the INTEGER_CST. We can
4190 only select bits from the LOW or HIGH part
4191 not from both. */
4192 if (shift < HOST_BITS_PER_WIDE_INT
4193 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4195 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4196 shift = HOST_BITS_PER_WIDE_INT;
4199 /* Now get the bits from the appropriate constant word. */
4200 if (shift < HOST_BITS_PER_WIDE_INT)
4201 value = TREE_INT_CST_LOW (val);
4202 else
4204 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4205 value = TREE_INT_CST_HIGH (val);
4206 shift -= HOST_BITS_PER_WIDE_INT;
4209 /* Get the result. This works only when:
4210 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4211 byte |= (((value >> shift)
4212 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4213 << (BITS_PER_UNIT - this_time - next_bit));
4215 else
4217 /* On little-endian machines,
4218 take first the least significant bits of the value
4219 and pack them starting at the least significant
4220 bits of the bytes. */
4221 shift = next_offset - int_bit_position (field);
4223 /* Don't try to take a bunch of bits that cross
4224 the word boundary in the INTEGER_CST. We can
4225 only select bits from the LOW or HIGH part
4226 not from both. */
4227 if (shift < HOST_BITS_PER_WIDE_INT
4228 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4229 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4231 /* Now get the bits from the appropriate constant word. */
4232 if (shift < HOST_BITS_PER_WIDE_INT)
4233 value = TREE_INT_CST_LOW (val);
4234 else
4236 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4237 value = TREE_INT_CST_HIGH (val);
4238 shift -= HOST_BITS_PER_WIDE_INT;
4241 /* Get the result. This works only when:
4242 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4243 byte |= (((value >> shift)
4244 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4245 << next_bit);
4248 next_offset += this_time;
4249 byte_buffer_in_use = 1;
4254 if (byte_buffer_in_use)
4256 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4257 total_bytes++;
4260 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4261 assemble_zeros (size - total_bytes);
4264 /* This TREE_LIST contains any weak symbol declarations waiting
4265 to be emitted. */
4266 static GTY(()) tree weak_decls;
4268 /* Mark DECL as weak. */
4270 static void
4271 mark_weak (tree decl)
4273 DECL_WEAK (decl) = 1;
4275 if (DECL_RTL_SET_P (decl)
4276 && MEM_P (DECL_RTL (decl))
4277 && XEXP (DECL_RTL (decl), 0)
4278 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4279 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4282 /* Merge weak status between NEWDECL and OLDDECL. */
4284 void
4285 merge_weak (tree newdecl, tree olddecl)
4287 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4288 return;
4290 if (DECL_WEAK (newdecl))
4292 tree wd;
4294 /* NEWDECL is weak, but OLDDECL is not. */
4296 /* If we already output the OLDDECL, we're in trouble; we can't
4297 go back and make it weak. This error cannot caught in
4298 declare_weak because the NEWDECL and OLDDECL was not yet
4299 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4300 if (TREE_ASM_WRITTEN (olddecl))
4301 error ("%Jweak declaration of %qD must precede definition",
4302 newdecl, newdecl);
4304 /* If we've already generated rtl referencing OLDDECL, we may
4305 have done so in a way that will not function properly with
4306 a weak symbol. */
4307 else if (TREE_USED (olddecl)
4308 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4309 warning ("%Jweak declaration of %qD after first use results "
4310 "in unspecified behavior", newdecl, newdecl);
4312 if (SUPPORTS_WEAK)
4314 /* We put the NEWDECL on the weak_decls list at some point.
4315 Replace it with the OLDDECL. */
4316 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4317 if (TREE_VALUE (wd) == newdecl)
4319 TREE_VALUE (wd) = olddecl;
4320 break;
4322 /* We may not find the entry on the list. If NEWDECL is a
4323 weak alias, then we will have already called
4324 globalize_decl to remove the entry; in that case, we do
4325 not need to do anything. */
4328 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4329 mark_weak (olddecl);
4331 else
4332 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4333 weak. Just update NEWDECL to indicate that it's weak too. */
4334 mark_weak (newdecl);
4337 /* Declare DECL to be a weak symbol. */
4339 void
4340 declare_weak (tree decl)
4342 if (! TREE_PUBLIC (decl))
4343 error ("%Jweak declaration of %qD must be public", decl, decl);
4344 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4345 error ("%Jweak declaration of %qD must precede definition", decl, decl);
4346 else if (SUPPORTS_WEAK)
4348 if (! DECL_WEAK (decl))
4349 weak_decls = tree_cons (NULL, decl, weak_decls);
4351 else
4352 warning ("%Jweak declaration of %qD not supported", decl, decl);
4354 mark_weak (decl);
4357 /* Emit any pending weak declarations. */
4359 void
4360 weak_finish (void)
4362 tree t;
4364 for (t = weak_decls; t; t = TREE_CHAIN (t))
4366 tree decl = TREE_VALUE (t);
4367 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4368 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4369 #endif
4371 if (! TREE_USED (decl))
4372 continue;
4374 #ifdef ASM_WEAKEN_DECL
4375 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4376 #else
4377 #ifdef ASM_WEAKEN_LABEL
4378 ASM_WEAKEN_LABEL (asm_out_file, name);
4379 #else
4380 #ifdef ASM_OUTPUT_WEAK_ALIAS
4381 warning ("only weak aliases are supported in this configuration");
4382 return;
4383 #endif
4384 #endif
4385 #endif
4389 /* Emit the assembly bits to indicate that DECL is globally visible. */
4391 static void
4392 globalize_decl (tree decl)
4394 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4396 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4397 if (DECL_WEAK (decl))
4399 tree *p, t;
4401 #ifdef ASM_WEAKEN_DECL
4402 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4403 #else
4404 ASM_WEAKEN_LABEL (asm_out_file, name);
4405 #endif
4407 /* Remove this function from the pending weak list so that
4408 we do not emit multiple .weak directives for it. */
4409 for (p = &weak_decls; (t = *p) ; )
4411 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4412 *p = TREE_CHAIN (t);
4413 else
4414 p = &TREE_CHAIN (t);
4416 return;
4418 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4419 if (DECL_ONE_ONLY (decl))
4420 ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4421 #endif
4423 targetm.asm_out.globalize_label (asm_out_file, name);
4426 /* We have to be able to tell cgraph about the needed-ness of the target
4427 of an alias. This requires that the decl have been defined. Aliases
4428 that precede their definition have to be queued for later processing. */
4430 struct alias_pair GTY(())
4432 tree decl;
4433 tree target;
4435 typedef struct alias_pair *alias_pair;
4437 /* Define gc'd vector type. */
4438 DEF_VEC_GC_P(alias_pair);
4440 static GTY(()) VEC(alias_pair) *alias_pairs;
4442 /* Given an assembly name, find the decl it is associated with. At the
4443 same time, mark it needed for cgraph. */
4445 static tree
4446 find_decl_and_mark_needed (tree decl, tree target)
4448 struct cgraph_node *fnode = NULL;
4449 struct cgraph_varpool_node *vnode = NULL;
4451 /* C++ thunk emitting code produces aliases late in the game.
4452 Avoid confusing cgraph code in that case. */
4453 if (!cgraph_global_info_ready)
4455 if (TREE_CODE (decl) == FUNCTION_DECL)
4457 fnode = cgraph_node_for_asm (target);
4458 if (fnode == NULL)
4459 vnode = cgraph_varpool_node_for_asm (target);
4461 else
4463 vnode = cgraph_varpool_node_for_asm (target);
4464 if (vnode == NULL)
4465 fnode = cgraph_node_for_asm (target);
4469 if (fnode)
4471 cgraph_mark_needed_node (fnode);
4472 return fnode->decl;
4474 else if (vnode)
4476 cgraph_varpool_mark_needed_node (vnode);
4477 return vnode->decl;
4479 else
4480 return NULL_TREE;
4483 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4484 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
4485 tree node is DECL to have the value of the tree node TARGET. */
4487 static void
4488 do_assemble_alias (tree decl, tree target ATTRIBUTE_UNUSED)
4490 TREE_ASM_WRITTEN (decl) = 1;
4491 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4493 #ifdef ASM_OUTPUT_DEF
4494 /* Make name accessible from other files, if appropriate. */
4496 if (TREE_PUBLIC (decl))
4498 globalize_decl (decl);
4499 maybe_assemble_visibility (decl);
4502 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4503 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4504 # else
4505 ASM_OUTPUT_DEF (asm_out_file,
4506 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4507 IDENTIFIER_POINTER (target));
4508 # endif
4509 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4511 const char *name;
4512 tree *p, t;
4514 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4515 # ifdef ASM_WEAKEN_DECL
4516 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4517 # else
4518 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4519 # endif
4520 /* Remove this function from the pending weak list so that
4521 we do not emit multiple .weak directives for it. */
4522 for (p = &weak_decls; (t = *p) ; )
4523 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4524 *p = TREE_CHAIN (t);
4525 else
4526 p = &TREE_CHAIN (t);
4528 #endif
4531 /* First pass of completing pending aliases. Make sure that cgraph knows
4532 which symbols will be required. */
4534 void
4535 finish_aliases_1 (void)
4537 unsigned i;
4538 alias_pair p;
4540 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4542 tree target_decl;
4544 target_decl = find_decl_and_mark_needed (p->decl, p->target);
4545 if (target_decl == NULL)
4546 error ("%J%qD aliased to undefined symbol %qE",
4547 p->decl, p->decl, p->target);
4548 else if (DECL_EXTERNAL (target_decl))
4549 error ("%J%qD aliased to external symbol %qE",
4550 p->decl, p->decl, p->target);
4554 /* Second pass of completing pending aliases. Emit the actual assembly.
4555 This happens at the end of compilation and thus it is assured that the
4556 target symbol has been emitted. */
4558 void
4559 finish_aliases_2 (void)
4561 unsigned i;
4562 alias_pair p;
4564 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4565 do_assemble_alias (p->decl, p->target);
4567 alias_pairs = NULL;
4570 /* Emit an assembler directive to make the symbol for DECL an alias to
4571 the symbol for TARGET. */
4573 void
4574 assemble_alias (tree decl, tree target)
4576 tree target_decl;
4578 #if !defined (ASM_OUTPUT_DEF)
4579 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4580 error ("%Jalias definitions not supported in this configuration", decl);
4581 return;
4582 # else
4583 if (!DECL_WEAK (decl))
4585 error ("%Jonly weak aliases are supported in this configuration", decl);
4586 return;
4588 # endif
4589 #endif
4591 /* We must force creation of DECL_RTL for debug info generation, even though
4592 we don't use it here. */
4593 make_decl_rtl (decl);
4594 TREE_USED (decl) = 1;
4596 /* A quirk of the initial implementation of aliases required that the user
4597 add "extern" to all of them. Which is silly, but now historical. Do
4598 note that the symbol is in fact locally defined. */
4599 DECL_EXTERNAL (decl) = 0;
4601 /* Allow aliases to aliases. */
4602 if (TREE_CODE (decl) == FUNCTION_DECL)
4603 cgraph_node (decl)->alias = true;
4604 else
4605 cgraph_varpool_node (decl)->alias = true;
4607 /* If the target has already been emitted, we don't have to queue the
4608 alias. This saves a tad o memory. */
4609 target_decl = find_decl_and_mark_needed (decl, target);
4610 if (target_decl && TREE_ASM_WRITTEN (target_decl))
4611 do_assemble_alias (decl, target);
4612 else
4614 alias_pair p;
4616 p = ggc_alloc (sizeof (struct alias_pair));
4617 p->decl = decl;
4618 p->target = target;
4619 VEC_safe_push (alias_pair, alias_pairs, p);
4623 /* Emit an assembler directive to set symbol for DECL visibility to
4624 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
4626 void
4627 default_assemble_visibility (tree decl, int vis)
4629 static const char * const visibility_types[] = {
4630 NULL, "internal", "hidden", "protected"
4633 const char *name, *type;
4635 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4636 type = visibility_types[vis];
4638 #ifdef HAVE_GAS_HIDDEN
4639 fprintf (asm_out_file, "\t.%s\t", type);
4640 assemble_name (asm_out_file, name);
4641 fprintf (asm_out_file, "\n");
4642 #else
4643 warning ("visibility attribute not supported in this configuration; ignored");
4644 #endif
4647 /* A helper function to call assemble_visibility when needed for a decl. */
4649 static void
4650 maybe_assemble_visibility (tree decl)
4652 enum symbol_visibility vis = DECL_VISIBILITY (decl);
4654 if (vis != VISIBILITY_DEFAULT)
4655 targetm.asm_out.visibility (decl, vis);
4658 /* Returns 1 if the target configuration supports defining public symbols
4659 so that one of them will be chosen at link time instead of generating a
4660 multiply-defined symbol error, whether through the use of weak symbols or
4661 a target-specific mechanism for having duplicates discarded. */
4664 supports_one_only (void)
4666 if (SUPPORTS_ONE_ONLY)
4667 return 1;
4668 return SUPPORTS_WEAK;
4671 /* Set up DECL as a public symbol that can be defined in multiple
4672 translation units without generating a linker error. */
4674 void
4675 make_decl_one_only (tree decl)
4677 gcc_assert (TREE_CODE (decl) == VAR_DECL
4678 || TREE_CODE (decl) == FUNCTION_DECL);
4680 TREE_PUBLIC (decl) = 1;
4682 if (SUPPORTS_ONE_ONLY)
4684 #ifdef MAKE_DECL_ONE_ONLY
4685 MAKE_DECL_ONE_ONLY (decl);
4686 #endif
4687 DECL_ONE_ONLY (decl) = 1;
4689 else if (TREE_CODE (decl) == VAR_DECL
4690 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4691 DECL_COMMON (decl) = 1;
4692 else
4694 gcc_assert (SUPPORTS_WEAK);
4695 DECL_WEAK (decl) = 1;
4699 void
4700 init_varasm_once (void)
4702 in_named_htab = htab_create_ggc (31, in_named_entry_hash,
4703 in_named_entry_eq, NULL);
4704 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4705 const_desc_eq, NULL);
4707 const_alias_set = new_alias_set ();
4710 static enum tls_model
4711 decl_tls_model (tree decl)
4713 enum tls_model kind;
4714 tree attr = lookup_attribute ("tls_model", DECL_ATTRIBUTES (decl));
4715 bool is_local;
4717 if (attr)
4719 attr = TREE_VALUE (TREE_VALUE (attr));
4720 gcc_assert (TREE_CODE (attr) == STRING_CST);
4722 if (!strcmp (TREE_STRING_POINTER (attr), "local-exec"))
4723 kind = TLS_MODEL_LOCAL_EXEC;
4724 else if (!strcmp (TREE_STRING_POINTER (attr), "initial-exec"))
4725 kind = TLS_MODEL_INITIAL_EXEC;
4726 else if (!strcmp (TREE_STRING_POINTER (attr), "local-dynamic"))
4727 kind = optimize ? TLS_MODEL_LOCAL_DYNAMIC : TLS_MODEL_GLOBAL_DYNAMIC;
4728 else if (!strcmp (TREE_STRING_POINTER (attr), "global-dynamic"))
4729 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4730 else
4731 gcc_unreachable ();
4732 return kind;
4735 is_local = targetm.binds_local_p (decl);
4736 if (!flag_shlib)
4738 if (is_local)
4739 kind = TLS_MODEL_LOCAL_EXEC;
4740 else
4741 kind = TLS_MODEL_INITIAL_EXEC;
4743 /* Local dynamic is inefficient when we're not combining the
4744 parts of the address. */
4745 else if (optimize && is_local)
4746 kind = TLS_MODEL_LOCAL_DYNAMIC;
4747 else
4748 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4749 if (kind < flag_tls_default)
4750 kind = flag_tls_default;
4752 return kind;
4755 /* Select a set of attributes for section NAME based on the properties
4756 of DECL and whether or not RELOC indicates that DECL's initializer
4757 might contain runtime relocations.
4759 We make the section read-only and executable for a function decl,
4760 read-only for a const data decl, and writable for a non-const data decl. */
4762 unsigned int
4763 default_section_type_flags (tree decl, const char *name, int reloc)
4765 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4768 unsigned int
4769 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4770 int shlib)
4772 unsigned int flags;
4774 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4775 flags = SECTION_CODE;
4776 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4777 flags = 0;
4778 else if (unlikely_text_section_name
4779 && strcmp (name, unlikely_text_section_name) == 0)
4780 flags = SECTION_CODE;
4781 else
4782 flags = SECTION_WRITE;
4784 if (decl && DECL_ONE_ONLY (decl))
4785 flags |= SECTION_LINKONCE;
4787 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
4788 flags |= SECTION_TLS | SECTION_WRITE;
4790 if (strcmp (name, ".bss") == 0
4791 || strncmp (name, ".bss.", 5) == 0
4792 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4793 || strcmp (name, ".sbss") == 0
4794 || strncmp (name, ".sbss.", 6) == 0
4795 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
4796 flags |= SECTION_BSS;
4798 if (strcmp (name, ".tdata") == 0
4799 || strncmp (name, ".tdata.", 7) == 0
4800 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
4801 flags |= SECTION_TLS;
4803 if (strcmp (name, ".tbss") == 0
4804 || strncmp (name, ".tbss.", 6) == 0
4805 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4806 flags |= SECTION_TLS | SECTION_BSS;
4808 /* These three sections have special ELF types. They are neither
4809 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4810 want to print a section type (@progbits or @nobits). If someone
4811 is silly enough to emit code or TLS variables to one of these
4812 sections, then don't handle them specially. */
4813 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4814 && (strcmp (name, ".init_array") == 0
4815 || strcmp (name, ".fini_array") == 0
4816 || strcmp (name, ".preinit_array") == 0))
4817 flags |= SECTION_NOTYPE;
4819 return flags;
4822 /* Output assembly to switch to section NAME with attribute FLAGS.
4823 Four variants for common object file formats. */
4825 void
4826 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4827 unsigned int flags ATTRIBUTE_UNUSED,
4828 tree decl ATTRIBUTE_UNUSED)
4830 /* Some object formats don't support named sections at all. The
4831 front-end should already have flagged this as an error. */
4832 gcc_unreachable ();
4835 void
4836 default_elf_asm_named_section (const char *name, unsigned int flags,
4837 tree decl ATTRIBUTE_UNUSED)
4839 char flagchars[10], *f = flagchars;
4841 /* If we have already declared this section, we can use an
4842 abbreviated form to switch back to it -- unless this section is
4843 part of a COMDAT groups, in which case GAS requires the full
4844 declaration every time. */
4845 if (!(HAVE_GAS_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4846 && ! named_section_first_declaration (name))
4848 fprintf (asm_out_file, "\t.section\t%s\n", name);
4849 return;
4852 if (!(flags & SECTION_DEBUG))
4853 *f++ = 'a';
4854 if (flags & SECTION_WRITE)
4855 *f++ = 'w';
4856 if (flags & SECTION_CODE)
4857 *f++ = 'x';
4858 if (flags & SECTION_SMALL)
4859 *f++ = 's';
4860 if (flags & SECTION_MERGE)
4861 *f++ = 'M';
4862 if (flags & SECTION_STRINGS)
4863 *f++ = 'S';
4864 if (flags & SECTION_TLS)
4865 *f++ = 'T';
4866 if (HAVE_GAS_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4867 *f++ = 'G';
4868 *f = '\0';
4870 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
4872 if (!(flags & SECTION_NOTYPE))
4874 const char *type;
4875 const char *format;
4877 if (flags & SECTION_BSS)
4878 type = "nobits";
4879 else
4880 type = "progbits";
4882 format = ",@%s";
4883 #ifdef ASM_COMMENT_START
4884 /* On platforms that use "@" as the assembly comment character,
4885 use "%" instead. */
4886 if (strcmp (ASM_COMMENT_START, "@") == 0)
4887 format = ",%%%s";
4888 #endif
4889 fprintf (asm_out_file, format, type);
4891 if (flags & SECTION_ENTSIZE)
4892 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
4893 if (HAVE_GAS_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4894 fprintf (asm_out_file, ",%s,comdat",
4895 lang_hooks.decls.comdat_group (decl));
4898 putc ('\n', asm_out_file);
4901 void
4902 default_coff_asm_named_section (const char *name, unsigned int flags,
4903 tree decl ATTRIBUTE_UNUSED)
4905 char flagchars[8], *f = flagchars;
4907 if (flags & SECTION_WRITE)
4908 *f++ = 'w';
4909 if (flags & SECTION_CODE)
4910 *f++ = 'x';
4911 *f = '\0';
4913 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
4916 void
4917 default_pe_asm_named_section (const char *name, unsigned int flags,
4918 tree decl)
4920 default_coff_asm_named_section (name, flags, decl);
4922 if (flags & SECTION_LINKONCE)
4924 /* Functions may have been compiled at various levels of
4925 optimization so we can't use `same_size' here.
4926 Instead, have the linker pick one. */
4927 fprintf (asm_out_file, "\t.linkonce %s\n",
4928 (flags & SECTION_CODE ? "discard" : "same_size"));
4932 /* The lame default section selector. */
4934 void
4935 default_select_section (tree decl, int reloc,
4936 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
4938 bool readonly = false;
4940 if (DECL_P (decl))
4942 if (decl_readonly_section (decl, reloc))
4943 readonly = true;
4945 else if (TREE_CODE (decl) == CONSTRUCTOR)
4947 if (! ((flag_pic && reloc)
4948 || !TREE_READONLY (decl)
4949 || TREE_SIDE_EFFECTS (decl)
4950 || !TREE_CONSTANT (decl)))
4951 readonly = true;
4953 else if (TREE_CODE (decl) == STRING_CST)
4954 readonly = true;
4955 else if (! (flag_pic && reloc))
4956 readonly = true;
4958 if (readonly)
4959 readonly_data_section ();
4960 else
4961 data_section ();
4964 /* A helper function for default_elf_select_section and
4965 default_elf_unique_section. Categorizes the DECL. */
4967 enum section_category
4969 SECCAT_TEXT,
4971 SECCAT_RODATA,
4972 SECCAT_RODATA_MERGE_STR,
4973 SECCAT_RODATA_MERGE_STR_INIT,
4974 SECCAT_RODATA_MERGE_CONST,
4975 SECCAT_SRODATA,
4977 SECCAT_DATA,
4979 /* To optimize loading of shared programs, define following subsections
4980 of data section:
4981 _REL Contains data that has relocations, so they get grouped
4982 together and dynamic linker will visit fewer pages in memory.
4983 _RO Contains data that is otherwise read-only. This is useful
4984 with prelinking as most relocations won't be dynamically
4985 linked and thus stay read only.
4986 _LOCAL Marks data containing relocations only to local objects.
4987 These relocations will get fully resolved by prelinking. */
4988 SECCAT_DATA_REL,
4989 SECCAT_DATA_REL_LOCAL,
4990 SECCAT_DATA_REL_RO,
4991 SECCAT_DATA_REL_RO_LOCAL,
4993 SECCAT_SDATA,
4994 SECCAT_TDATA,
4996 SECCAT_BSS,
4997 SECCAT_SBSS,
4998 SECCAT_TBSS
5001 static enum section_category
5002 categorize_decl_for_section (tree, int, int);
5004 static enum section_category
5005 categorize_decl_for_section (tree decl, int reloc, int shlib)
5007 enum section_category ret;
5009 if (TREE_CODE (decl) == FUNCTION_DECL)
5010 return SECCAT_TEXT;
5011 else if (TREE_CODE (decl) == STRING_CST)
5013 if (flag_mudflap) /* or !flag_merge_constants */
5014 return SECCAT_RODATA;
5015 else
5016 return SECCAT_RODATA_MERGE_STR;
5018 else if (TREE_CODE (decl) == VAR_DECL)
5020 if (DECL_INITIAL (decl) == NULL
5021 || DECL_INITIAL (decl) == error_mark_node
5022 || (flag_zero_initialized_in_bss
5023 /* Leave constant zeroes in .rodata so they can be shared. */
5024 && !TREE_READONLY (decl)
5025 && initializer_zerop (DECL_INITIAL (decl))))
5026 ret = SECCAT_BSS;
5027 else if (! TREE_READONLY (decl)
5028 || TREE_SIDE_EFFECTS (decl)
5029 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5031 if (shlib && (reloc & 2))
5032 ret = SECCAT_DATA_REL;
5033 else if (shlib && reloc)
5034 ret = SECCAT_DATA_REL_LOCAL;
5035 else
5036 ret = SECCAT_DATA;
5038 else if (shlib && (reloc & 2))
5039 ret = SECCAT_DATA_REL_RO;
5040 else if (shlib && reloc)
5041 ret = SECCAT_DATA_REL_RO_LOCAL;
5042 else if (reloc || flag_merge_constants < 2)
5043 /* C and C++ don't allow different variables to share the same
5044 location. -fmerge-all-constants allows even that (at the
5045 expense of not conforming). */
5046 ret = SECCAT_RODATA;
5047 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5048 ret = SECCAT_RODATA_MERGE_STR_INIT;
5049 else
5050 ret = SECCAT_RODATA_MERGE_CONST;
5052 else if (TREE_CODE (decl) == CONSTRUCTOR)
5054 if ((shlib && reloc)
5055 || TREE_SIDE_EFFECTS (decl)
5056 || ! TREE_CONSTANT (decl))
5057 ret = SECCAT_DATA;
5058 else
5059 ret = SECCAT_RODATA;
5061 else
5062 ret = SECCAT_RODATA;
5064 /* There are no read-only thread-local sections. */
5065 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
5067 /* Note that this would be *just* SECCAT_BSS, except that there's
5068 no concept of a read-only thread-local-data section. */
5069 if (ret == SECCAT_BSS
5070 || (flag_zero_initialized_in_bss
5071 && initializer_zerop (DECL_INITIAL (decl))))
5072 ret = SECCAT_TBSS;
5073 else
5074 ret = SECCAT_TDATA;
5077 /* If the target uses small data sections, select it. */
5078 else if (targetm.in_small_data_p (decl))
5080 if (ret == SECCAT_BSS)
5081 ret = SECCAT_SBSS;
5082 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5083 ret = SECCAT_SRODATA;
5084 else
5085 ret = SECCAT_SDATA;
5088 return ret;
5091 bool
5092 decl_readonly_section (tree decl, int reloc)
5094 return decl_readonly_section_1 (decl, reloc, flag_pic);
5097 bool
5098 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5100 switch (categorize_decl_for_section (decl, reloc, shlib))
5102 case SECCAT_RODATA:
5103 case SECCAT_RODATA_MERGE_STR:
5104 case SECCAT_RODATA_MERGE_STR_INIT:
5105 case SECCAT_RODATA_MERGE_CONST:
5106 case SECCAT_SRODATA:
5107 return true;
5108 break;
5109 default:
5110 return false;
5111 break;
5115 /* Select a section based on the above categorization. */
5117 void
5118 default_elf_select_section (tree decl, int reloc,
5119 unsigned HOST_WIDE_INT align)
5121 default_elf_select_section_1 (decl, reloc, align, flag_pic);
5124 void
5125 default_elf_select_section_1 (tree decl, int reloc,
5126 unsigned HOST_WIDE_INT align, int shlib)
5128 const char *sname;
5129 switch (categorize_decl_for_section (decl, reloc, shlib))
5131 case SECCAT_TEXT:
5132 /* We're not supposed to be called on FUNCTION_DECLs. */
5133 gcc_unreachable ();
5134 case SECCAT_RODATA:
5135 readonly_data_section ();
5136 return;
5137 case SECCAT_RODATA_MERGE_STR:
5138 mergeable_string_section (decl, align, 0);
5139 return;
5140 case SECCAT_RODATA_MERGE_STR_INIT:
5141 mergeable_string_section (DECL_INITIAL (decl), align, 0);
5142 return;
5143 case SECCAT_RODATA_MERGE_CONST:
5144 mergeable_constant_section (DECL_MODE (decl), align, 0);
5145 return;
5146 case SECCAT_SRODATA:
5147 sname = ".sdata2";
5148 break;
5149 case SECCAT_DATA:
5150 data_section ();
5151 return;
5152 case SECCAT_DATA_REL:
5153 sname = ".data.rel";
5154 break;
5155 case SECCAT_DATA_REL_LOCAL:
5156 sname = ".data.rel.local";
5157 break;
5158 case SECCAT_DATA_REL_RO:
5159 sname = ".data.rel.ro";
5160 break;
5161 case SECCAT_DATA_REL_RO_LOCAL:
5162 sname = ".data.rel.ro.local";
5163 break;
5164 case SECCAT_SDATA:
5165 sname = ".sdata";
5166 break;
5167 case SECCAT_TDATA:
5168 sname = ".tdata";
5169 break;
5170 case SECCAT_BSS:
5171 #ifdef BSS_SECTION_ASM_OP
5172 bss_section ();
5173 return;
5174 #else
5175 sname = ".bss";
5176 break;
5177 #endif
5178 case SECCAT_SBSS:
5179 sname = ".sbss";
5180 break;
5181 case SECCAT_TBSS:
5182 sname = ".tbss";
5183 break;
5184 default:
5185 gcc_unreachable ();
5188 if (!DECL_P (decl))
5189 decl = NULL_TREE;
5190 named_section (decl, sname, reloc);
5193 /* Construct a unique section name based on the decl name and the
5194 categorization performed above. */
5196 void
5197 default_unique_section (tree decl, int reloc)
5199 default_unique_section_1 (decl, reloc, flag_pic);
5202 void
5203 default_unique_section_1 (tree decl, int reloc, int shlib)
5205 bool one_only = DECL_ONE_ONLY (decl);
5206 const char *prefix, *name;
5207 size_t nlen, plen;
5208 char *string;
5210 switch (categorize_decl_for_section (decl, reloc, shlib))
5212 case SECCAT_TEXT:
5213 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5214 break;
5215 case SECCAT_RODATA:
5216 case SECCAT_RODATA_MERGE_STR:
5217 case SECCAT_RODATA_MERGE_STR_INIT:
5218 case SECCAT_RODATA_MERGE_CONST:
5219 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5220 break;
5221 case SECCAT_SRODATA:
5222 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5223 break;
5224 case SECCAT_DATA:
5225 case SECCAT_DATA_REL:
5226 case SECCAT_DATA_REL_LOCAL:
5227 case SECCAT_DATA_REL_RO:
5228 case SECCAT_DATA_REL_RO_LOCAL:
5229 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5230 break;
5231 case SECCAT_SDATA:
5232 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5233 break;
5234 case SECCAT_BSS:
5235 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5236 break;
5237 case SECCAT_SBSS:
5238 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5239 break;
5240 case SECCAT_TDATA:
5241 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5242 break;
5243 case SECCAT_TBSS:
5244 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5245 break;
5246 default:
5247 gcc_unreachable ();
5249 plen = strlen (prefix);
5251 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5252 name = targetm.strip_name_encoding (name);
5253 nlen = strlen (name);
5255 string = alloca (nlen + plen + 1);
5256 memcpy (string, prefix, plen);
5257 memcpy (string + plen, name, nlen + 1);
5259 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5262 void
5263 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5264 rtx x,
5265 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5267 if (flag_pic)
5268 switch (GET_CODE (x))
5270 case CONST:
5271 case SYMBOL_REF:
5272 case LABEL_REF:
5273 data_section ();
5274 return;
5276 default:
5277 break;
5280 readonly_data_section ();
5283 void
5284 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5285 unsigned HOST_WIDE_INT align)
5287 /* ??? Handle small data here somehow. */
5289 if (flag_pic)
5290 switch (GET_CODE (x))
5292 case CONST:
5293 case SYMBOL_REF:
5294 named_section (NULL_TREE, ".data.rel.ro", 3);
5295 return;
5297 case LABEL_REF:
5298 named_section (NULL_TREE, ".data.rel.ro.local", 1);
5299 return;
5301 default:
5302 break;
5305 mergeable_constant_section (mode, align, 0);
5308 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
5310 void
5311 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5313 rtx symbol;
5314 int flags;
5316 /* Careful not to prod global register variables. */
5317 if (!MEM_P (rtl))
5318 return;
5319 symbol = XEXP (rtl, 0);
5320 if (GET_CODE (symbol) != SYMBOL_REF)
5321 return;
5323 flags = 0;
5324 if (TREE_CODE (decl) == FUNCTION_DECL)
5325 flags |= SYMBOL_FLAG_FUNCTION;
5326 if (targetm.binds_local_p (decl))
5327 flags |= SYMBOL_FLAG_LOCAL;
5328 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL (decl))
5329 flags |= decl_tls_model (decl) << SYMBOL_FLAG_TLS_SHIFT;
5330 else if (targetm.in_small_data_p (decl))
5331 flags |= SYMBOL_FLAG_SMALL;
5332 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
5333 being PUBLIC, the thing *must* be defined in this translation unit.
5334 Prevent this buglet from being propagated into rtl code as well. */
5335 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5336 flags |= SYMBOL_FLAG_EXTERNAL;
5338 SYMBOL_REF_FLAGS (symbol) = flags;
5341 /* By default, we do nothing for encode_section_info, so we need not
5342 do anything but discard the '*' marker. */
5344 const char *
5345 default_strip_name_encoding (const char *str)
5347 return str + (*str == '*');
5350 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5351 wrt cross-module name binding. */
5353 bool
5354 default_binds_local_p (tree exp)
5356 return default_binds_local_p_1 (exp, flag_shlib);
5359 bool
5360 default_binds_local_p_1 (tree exp, int shlib)
5362 bool local_p;
5364 /* A non-decl is an entry in the constant pool. */
5365 if (!DECL_P (exp))
5366 local_p = true;
5367 /* Static variables are always local. */
5368 else if (! TREE_PUBLIC (exp))
5369 local_p = true;
5370 /* A variable is local if the user explicitly tells us so. */
5371 else if (DECL_VISIBILITY_SPECIFIED (exp) && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5372 local_p = true;
5373 /* Otherwise, variables defined outside this object may not be local. */
5374 else if (DECL_EXTERNAL (exp))
5375 local_p = false;
5376 /* Linkonce and weak data are never local. */
5377 else if (DECL_ONE_ONLY (exp) || DECL_WEAK (exp))
5378 local_p = false;
5379 /* If none of the above and visibility is not default, make local. */
5380 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5381 local_p = true;
5382 /* If PIC, then assume that any global name can be overridden by
5383 symbols resolved from other modules. */
5384 else if (shlib)
5385 local_p = false;
5386 /* Uninitialized COMMON variable may be unified with symbols
5387 resolved from other modules. */
5388 else if (DECL_COMMON (exp)
5389 && (DECL_INITIAL (exp) == NULL
5390 || DECL_INITIAL (exp) == error_mark_node))
5391 local_p = false;
5392 /* Otherwise we're left with initialized (or non-common) global data
5393 which is of necessity defined locally. */
5394 else
5395 local_p = true;
5397 return local_p;
5400 /* Determine whether or not a pointer mode is valid. Assume defaults
5401 of ptr_mode or Pmode - can be overridden. */
5402 bool
5403 default_valid_pointer_mode (enum machine_mode mode)
5405 return (mode == ptr_mode || mode == Pmode);
5408 /* Default function to output code that will globalize a label. A
5409 target must define GLOBAL_ASM_OP or provide its own function to
5410 globalize a label. */
5411 #ifdef GLOBAL_ASM_OP
5412 void
5413 default_globalize_label (FILE * stream, const char *name)
5415 fputs (GLOBAL_ASM_OP, stream);
5416 assemble_name (stream, name);
5417 putc ('\n', stream);
5419 #endif /* GLOBAL_ASM_OP */
5421 /* Default function to output a label for unwind information. The
5422 default is to do nothing. A target that needs nonlocal labels for
5423 unwind information must provide its own function to do this. */
5424 void
5425 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5426 tree decl ATTRIBUTE_UNUSED,
5427 int for_eh ATTRIBUTE_UNUSED,
5428 int empty ATTRIBUTE_UNUSED)
5432 /* This is how to output an internal numbered label where PREFIX is
5433 the class of label and LABELNO is the number within the class. */
5435 void
5436 default_internal_label (FILE *stream, const char *prefix,
5437 unsigned long labelno)
5439 char *const buf = alloca (40 + strlen (prefix));
5440 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5441 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5444 /* This is the default behavior at the beginning of a file. It's
5445 controlled by two other target-hook toggles. */
5446 void
5447 default_file_start (void)
5449 if (targetm.file_start_app_off && !flag_verbose_asm)
5450 fputs (ASM_APP_OFF, asm_out_file);
5452 if (targetm.file_start_file_directive)
5453 output_file_directive (asm_out_file, main_input_filename);
5456 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5457 which emits a special section directive used to indicate whether or
5458 not this object file needs an executable stack. This is primarily
5459 a GNU extension to ELF but could be used on other targets. */
5461 int trampolines_created;
5463 void
5464 file_end_indicate_exec_stack (void)
5466 unsigned int flags = SECTION_DEBUG;
5467 if (trampolines_created)
5468 flags |= SECTION_CODE;
5470 named_section_flags (".note.GNU-stack", flags);
5473 #include "gt-varasm.h"