2005-12-13 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / varasm.c
blobfe5231476f0f499addeff18be60a3cc63dc9401b
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 /* This file handles generation of all the assembler code
25 *except* the instructions of a function.
26 This includes declarations of variables and their initial values.
28 We also output the assembler code for constants stored in memory
29 and are responsible for combining constants with the same value. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "rtl.h"
36 #include "tree.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "hard-reg-set.h"
41 #include "regs.h"
42 #include "real.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "hashtab.h"
46 #include "c-pragma.h"
47 #include "ggc.h"
48 #include "langhooks.h"
49 #include "tm_p.h"
50 #include "debug.h"
51 #include "target.h"
52 #include "tree-mudflap.h"
53 #include "cgraph.h"
54 #include "cfglayout.h"
55 #include "basic-block.h"
57 #ifdef XCOFF_DEBUGGING_INFO
58 #include "xcoffout.h" /* Needed for external data
59 declarations for e.g. AIX 4.x. */
60 #endif
62 /* The (assembler) name of the first globally-visible object output. */
63 extern GTY(()) const char *first_global_object_name;
64 extern GTY(()) const char *weak_global_object_name;
66 const char *first_global_object_name;
67 const char *weak_global_object_name;
69 struct addr_const;
70 struct constant_descriptor_rtx;
71 struct rtx_constant_pool;
73 struct varasm_status GTY(())
75 /* If we're using a per-function constant pool, this is it. */
76 struct rtx_constant_pool *pool;
78 /* Number of tree-constants deferred during the expansion of this
79 function. */
80 unsigned int deferred_constants;
83 #define n_deferred_constants (cfun->varasm->deferred_constants)
85 /* Number for making the label on the next
86 constant that is stored in memory. */
88 static GTY(()) int const_labelno;
90 /* Carry information from ASM_DECLARE_OBJECT_NAME
91 to ASM_FINISH_DECLARE_OBJECT. */
93 int size_directive_output;
95 /* The last decl for which assemble_variable was called,
96 if it did ASM_DECLARE_OBJECT_NAME.
97 If the last call to assemble_variable didn't do that,
98 this holds 0. */
100 tree last_assemble_variable_decl;
102 /* The following global variable indicates if the first basic block
103 in a function belongs to the cold partition or not. */
105 bool first_function_block_is_cold;
107 /* We give all constants their own alias set. Perhaps redundant with
108 MEM_READONLY_P, but pre-dates it. */
110 static HOST_WIDE_INT const_alias_set;
112 static const char *strip_reg_name (const char *);
113 static int contains_pointers_p (tree);
114 #ifdef ASM_OUTPUT_EXTERNAL
115 static bool incorporeal_function_p (tree);
116 #endif
117 static void decode_addr_const (tree, struct addr_const *);
118 static hashval_t const_desc_hash (const void *);
119 static int const_desc_eq (const void *, const void *);
120 static hashval_t const_hash_1 (const tree);
121 static int compare_constant (const tree, const tree);
122 static tree copy_constant (tree);
123 static void output_constant_def_contents (rtx);
124 static void output_addressed_constants (tree);
125 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
126 static unsigned min_align (unsigned, unsigned);
127 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
128 static void globalize_decl (tree);
129 static void maybe_assemble_visibility (tree);
130 #ifdef BSS_SECTION_ASM_OP
131 #ifdef ASM_OUTPUT_BSS
132 static void asm_output_bss (FILE *, tree, const char *,
133 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
134 #endif
135 #ifdef ASM_OUTPUT_ALIGNED_BSS
136 static void asm_output_aligned_bss (FILE *, tree, const char *,
137 unsigned HOST_WIDE_INT, int)
138 ATTRIBUTE_UNUSED;
139 #endif
140 #endif /* BSS_SECTION_ASM_OP */
141 static bool asm_emit_uninitialised (tree, const char*,
142 unsigned HOST_WIDE_INT,
143 unsigned HOST_WIDE_INT);
144 static void mark_weak (tree);
146 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
147 section *text_section;
148 section *data_section;
149 section *readonly_data_section;
150 section *sdata_section;
151 section *ctors_section;
152 section *dtors_section;
153 section *bss_section;
154 section *sbss_section;
156 /* The section that holds the main exception table, when known. The section
157 is set either by the target's init_sections hook or by the first call to
158 switch_to_exception_section. */
159 section *exception_section;
161 /* The section that holds the DWARF2 frame unwind information, when known.
162 The section is set either by the target's init_sections hook or by the
163 first call to switch_to_eh_frame_section. */
164 section *eh_frame_section;
166 /* asm_out_file's current section. This is NULL if no section has yet
167 been selected or if we lose track of what the current section is. */
168 section *in_section;
170 /* The last text section used by asm_out_file. */
171 section *last_text_section;
173 /* A linked list of all the unnamed sections. */
174 static GTY(()) section *unnamed_sections;
176 /* Return a nonzero value if DECL has a section attribute. */
177 #ifndef IN_NAMED_SECTION
178 #define IN_NAMED_SECTION(DECL) \
179 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
180 && DECL_SECTION_NAME (DECL) != NULL_TREE)
181 #endif
183 /* Hash table of named sections. */
184 static GTY((param_is (section))) htab_t section_htab;
186 /* Helper routines for maintaining section_htab. */
188 static int
189 section_entry_eq (const void *p1, const void *p2)
191 const section *old = p1;
192 const char *new = p2;
194 return strcmp (old->named.name, new) == 0;
197 static hashval_t
198 section_entry_hash (const void *p)
200 const section *old = p;
201 return htab_hash_string (old->named.name);
204 /* Return a new unnamed section with the given fields. */
206 section *
207 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
208 const void *data)
210 section *sect;
212 sect = ggc_alloc (sizeof (struct unnamed_section));
213 sect->unnamed.common.flags = flags;
214 sect->unnamed.callback = callback;
215 sect->unnamed.data = data;
216 sect->unnamed.next = unnamed_sections;
218 unnamed_sections = sect;
219 return sect;
222 /* Return the named section structure associated with NAME. Create
223 a new section with the given fields if no such structure exists. */
225 section *
226 get_section (const char *name, unsigned int flags, tree decl)
228 section *sect, **slot;
230 slot = (section **)
231 htab_find_slot_with_hash (section_htab, name,
232 htab_hash_string (name), INSERT);
233 flags |= SECTION_NAMED;
234 if (*slot == NULL)
236 sect = ggc_alloc (sizeof (struct named_section));
237 sect->named.common.flags = flags;
238 sect->named.name = ggc_strdup (name);
239 sect->named.decl = decl;
240 *slot = sect;
242 else
244 sect = *slot;
245 if ((sect->common.flags & ~SECTION_DECLARED) != flags
246 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
248 /* Sanity check user variables for flag changes. */
249 if (decl == 0)
250 decl = sect->named.decl;
251 if (decl)
252 error ("%+D causes a section type conflict", decl);
253 else
254 gcc_unreachable ();
257 return sect;
260 static void
261 initialize_cold_section_name (void)
263 const char *stripped_name;
264 char *name, *buffer;
265 tree dsn;
267 gcc_assert (cfun && current_function_decl);
268 if (cfun->unlikely_text_section_name)
269 return;
271 dsn = DECL_SECTION_NAME (current_function_decl);
272 if (flag_function_sections && dsn)
274 name = alloca (TREE_STRING_LENGTH (dsn) + 1);
275 memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
277 stripped_name = targetm.strip_name_encoding (name);
279 buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
280 cfun->unlikely_text_section_name = ggc_strdup (buffer);
282 else
283 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
286 /* Tell assembler to switch to unlikely-to-be-executed text section. */
288 section *
289 unlikely_text_section (void)
291 if (cfun)
293 if (!cfun->unlikely_text_section_name)
294 initialize_cold_section_name ();
296 return get_named_section (NULL, cfun->unlikely_text_section_name, 0);
298 else
299 return get_named_section (NULL, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
302 /* When called within a function context, return true if the function
303 has been assigned a cold text section and if SECT is that section.
304 When called outside a function context, return true if SECT is the
305 default cold section. */
307 bool
308 unlikely_text_section_p (section *sect)
310 const char *name;
312 if (cfun)
313 name = cfun->unlikely_text_section_name;
314 else
315 name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
317 return (name
318 && sect
319 && (sect->common.flags & SECTION_NAMED) != 0
320 && strcmp (name, sect->named.name) == 0);
323 /* Return a section with a particular name and with whatever SECTION_*
324 flags section_type_flags deems appropriate. The name of the section
325 is taken from NAME if nonnull, otherwise it is taken from DECL's
326 DECL_SECTION_NAME. DECL is the decl associated with the section
327 (see the section comment for details) and RELOC is as for
328 section_type_flags. */
330 section *
331 get_named_section (tree decl, const char *name, int reloc)
333 unsigned int flags;
335 gcc_assert (!decl || DECL_P (decl));
336 if (name == NULL)
337 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
339 flags = targetm.section_type_flags (decl, name, reloc);
341 return get_section (name, flags, decl);
344 /* If required, set DECL_SECTION_NAME to a unique name. */
346 void
347 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
348 int flag_function_or_data_sections)
350 if (DECL_SECTION_NAME (decl) == NULL_TREE
351 && targetm.have_named_sections
352 && (flag_function_or_data_sections
353 || DECL_ONE_ONLY (decl)))
354 targetm.asm_out.unique_section (decl, reloc);
357 #ifdef BSS_SECTION_ASM_OP
359 #ifdef ASM_OUTPUT_BSS
361 /* Utility function for ASM_OUTPUT_BSS for targets to use if
362 they don't support alignments in .bss.
363 ??? It is believed that this function will work in most cases so such
364 support is localized here. */
366 static void
367 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
368 const char *name,
369 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
370 unsigned HOST_WIDE_INT rounded)
372 targetm.asm_out.globalize_label (file, name);
373 switch_to_section (bss_section);
374 #ifdef ASM_DECLARE_OBJECT_NAME
375 last_assemble_variable_decl = decl;
376 ASM_DECLARE_OBJECT_NAME (file, name, decl);
377 #else
378 /* Standard thing is just output label for the object. */
379 ASM_OUTPUT_LABEL (file, name);
380 #endif /* ASM_DECLARE_OBJECT_NAME */
381 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
384 #endif
386 #ifdef ASM_OUTPUT_ALIGNED_BSS
388 /* Utility function for targets to use in implementing
389 ASM_OUTPUT_ALIGNED_BSS.
390 ??? It is believed that this function will work in most cases so such
391 support is localized here. */
393 static void
394 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
395 const char *name, unsigned HOST_WIDE_INT size,
396 int align)
398 switch_to_section (bss_section);
399 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
400 #ifdef ASM_DECLARE_OBJECT_NAME
401 last_assemble_variable_decl = decl;
402 ASM_DECLARE_OBJECT_NAME (file, name, decl);
403 #else
404 /* Standard thing is just output label for the object. */
405 ASM_OUTPUT_LABEL (file, name);
406 #endif /* ASM_DECLARE_OBJECT_NAME */
407 ASM_OUTPUT_SKIP (file, size ? size : 1);
410 #endif
412 #endif /* BSS_SECTION_ASM_OP */
414 /* Return the section for function DECL.
416 If DECL is NULL_TREE, return the text section. We can be passed
417 NULL_TREE under some circumstances by dbxout.c at least. */
419 section *
420 function_section (tree decl)
422 int reloc = 0;
424 if (first_function_block_is_cold)
425 reloc = 1;
427 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
428 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
429 #else
430 if (decl != NULL_TREE
431 && DECL_SECTION_NAME (decl) != NULL_TREE
432 && targetm.have_named_sections)
433 return get_named_section (decl, NULL, 0);
434 else
435 return text_section;
436 #endif
439 section *
440 current_function_section (void)
442 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
443 int reloc = 0;
445 if (unlikely_text_section_p (last_text_section))
446 reloc = 1;
448 return targetm.asm_out.select_section (current_function_decl, reloc,
449 DECL_ALIGN (current_function_decl));
450 #else
451 if (last_text_section)
452 return last_text_section;
453 return function_section (current_function_decl);
454 #endif
457 /* Return the read-only data section associated with function DECL. */
459 section *
460 default_function_rodata_section (tree decl)
462 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
464 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
466 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
468 size_t len = strlen (name) + 3;
469 char* rname = alloca (len);
471 strcpy (rname, ".rodata");
472 strcat (rname, name + 5);
473 return get_section (rname, SECTION_LINKONCE, decl);
475 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
476 else if (DECL_ONE_ONLY (decl)
477 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
479 size_t len = strlen (name) + 1;
480 char *rname = alloca (len);
482 memcpy (rname, name, len);
483 rname[14] = 'r';
484 return get_section (rname, SECTION_LINKONCE, decl);
486 /* For .text.foo we want to use .rodata.foo. */
487 else if (flag_function_sections && flag_data_sections
488 && strncmp (name, ".text.", 6) == 0)
490 size_t len = strlen (name) + 1;
491 char *rname = alloca (len + 2);
493 memcpy (rname, ".rodata", 7);
494 memcpy (rname + 7, name + 5, len - 5);
495 return get_section (rname, 0, decl);
499 return readonly_data_section;
502 /* Return the read-only data section associated with function DECL
503 for targets where that section should be always the single
504 readonly data section. */
506 section *
507 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
509 return readonly_data_section;
512 /* Switch to section for variable DECL. RELOC is the same as the
513 argument to SELECT_SECTION. */
515 void
516 variable_section (tree decl, int reloc)
518 if (IN_NAMED_SECTION (decl))
519 switch_to_section (get_named_section (decl, NULL, reloc));
520 else
521 switch_to_section (targetm.asm_out.select_section (decl, reloc,
522 DECL_ALIGN (decl)));
525 /* Return the section to use for string merging. */
527 static section *
528 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
529 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
530 unsigned int flags ATTRIBUTE_UNUSED)
532 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
533 && TREE_CODE (decl) == STRING_CST
534 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
535 && align <= 256
536 && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
538 enum machine_mode mode;
539 unsigned int modesize;
540 const char *str;
541 int i, j, len, unit;
542 char name[30];
544 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
545 modesize = GET_MODE_BITSIZE (mode);
546 if (modesize >= 8 && modesize <= 256
547 && (modesize & (modesize - 1)) == 0)
549 if (align < modesize)
550 align = modesize;
552 str = TREE_STRING_POINTER (decl);
553 len = TREE_STRING_LENGTH (decl);
554 unit = GET_MODE_SIZE (mode);
556 /* Check for embedded NUL characters. */
557 for (i = 0; i < len; i += unit)
559 for (j = 0; j < unit; j++)
560 if (str[i + j] != '\0')
561 break;
562 if (j == unit)
563 break;
565 if (i == len - unit)
567 sprintf (name, ".rodata.str%d.%d", modesize / 8,
568 (int) (align / 8));
569 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
570 return get_section (name, flags, NULL);
575 return readonly_data_section;
578 /* Return the section to use for constant merging. */
580 section *
581 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
582 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
583 unsigned int flags ATTRIBUTE_UNUSED)
585 unsigned int modesize = GET_MODE_BITSIZE (mode);
587 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
588 && mode != VOIDmode
589 && mode != BLKmode
590 && modesize <= align
591 && align >= 8
592 && align <= 256
593 && (align & (align - 1)) == 0)
595 char name[24];
597 sprintf (name, ".rodata.cst%d", (int) (align / 8));
598 flags |= (align / 8) | SECTION_MERGE;
599 return get_section (name, flags, NULL);
601 return readonly_data_section;
604 /* Given NAME, a putative register name, discard any customary prefixes. */
606 static const char *
607 strip_reg_name (const char *name)
609 #ifdef REGISTER_PREFIX
610 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
611 name += strlen (REGISTER_PREFIX);
612 #endif
613 if (name[0] == '%' || name[0] == '#')
614 name++;
615 return name;
618 /* The user has asked for a DECL to have a particular name. Set (or
619 change) it in such a way that we don't prefix an underscore to
620 it. */
621 void
622 set_user_assembler_name (tree decl, const char *name)
624 char *starred = alloca (strlen (name) + 2);
625 starred[0] = '*';
626 strcpy (starred + 1, name);
627 change_decl_assembler_name (decl, get_identifier (starred));
628 SET_DECL_RTL (decl, NULL_RTX);
631 /* Decode an `asm' spec for a declaration as a register name.
632 Return the register number, or -1 if nothing specified,
633 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
634 or -3 if ASMSPEC is `cc' and is not recognized,
635 or -4 if ASMSPEC is `memory' and is not recognized.
636 Accept an exact spelling or a decimal number.
637 Prefixes such as % are optional. */
640 decode_reg_name (const char *asmspec)
642 if (asmspec != 0)
644 int i;
646 /* Get rid of confusing prefixes. */
647 asmspec = strip_reg_name (asmspec);
649 /* Allow a decimal number as a "register name". */
650 for (i = strlen (asmspec) - 1; i >= 0; i--)
651 if (! ISDIGIT (asmspec[i]))
652 break;
653 if (asmspec[0] != 0 && i < 0)
655 i = atoi (asmspec);
656 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
657 return i;
658 else
659 return -2;
662 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
663 if (reg_names[i][0]
664 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
665 return i;
667 #ifdef ADDITIONAL_REGISTER_NAMES
669 static const struct { const char *const name; const int number; } table[]
670 = ADDITIONAL_REGISTER_NAMES;
672 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
673 if (table[i].name[0]
674 && ! strcmp (asmspec, table[i].name))
675 return table[i].number;
677 #endif /* ADDITIONAL_REGISTER_NAMES */
679 if (!strcmp (asmspec, "memory"))
680 return -4;
682 if (!strcmp (asmspec, "cc"))
683 return -3;
685 return -2;
688 return -1;
691 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
692 have static storage duration. In other words, it should not be an
693 automatic variable, including PARM_DECLs.
695 There is, however, one exception: this function handles variables
696 explicitly placed in a particular register by the user.
698 This is never called for PARM_DECL nodes. */
700 void
701 make_decl_rtl (tree decl)
703 const char *name = 0;
704 int reg_number;
705 rtx x;
707 /* Check that we are not being given an automatic variable. */
708 gcc_assert (TREE_CODE (decl) != PARM_DECL
709 && TREE_CODE (decl) != RESULT_DECL);
711 /* A weak alias has TREE_PUBLIC set but not the other bits. */
712 gcc_assert (TREE_CODE (decl) != VAR_DECL
713 || TREE_STATIC (decl)
714 || TREE_PUBLIC (decl)
715 || DECL_EXTERNAL (decl)
716 || DECL_REGISTER (decl));
718 /* And that we were not given a type or a label. */
719 gcc_assert (TREE_CODE (decl) != TYPE_DECL
720 && TREE_CODE (decl) != LABEL_DECL);
722 /* For a duplicate declaration, we can be called twice on the
723 same DECL node. Don't discard the RTL already made. */
724 if (DECL_RTL_SET_P (decl))
726 /* If the old RTL had the wrong mode, fix the mode. */
727 if (GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
728 SET_DECL_RTL (decl, adjust_address_nv (DECL_RTL (decl),
729 DECL_MODE (decl), 0));
731 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
732 return;
734 /* ??? Another way to do this would be to maintain a hashed
735 table of such critters. Instead of adding stuff to a DECL
736 to give certain attributes to it, we could use an external
737 hash map from DECL to set of attributes. */
739 /* Let the target reassign the RTL if it wants.
740 This is necessary, for example, when one machine specific
741 decl attribute overrides another. */
742 targetm.encode_section_info (decl, DECL_RTL (decl), false);
744 /* Make this function static known to the mudflap runtime. */
745 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
746 mudflap_enqueue_decl (decl);
748 return;
751 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
753 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
754 && DECL_REGISTER (decl))
756 error ("register name not specified for %q+D", decl);
758 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
760 const char *asmspec = name+1;
761 reg_number = decode_reg_name (asmspec);
762 /* First detect errors in declaring global registers. */
763 if (reg_number == -1)
764 error ("register name not specified for %q+D", decl);
765 else if (reg_number < 0)
766 error ("invalid register name for %q+D", decl);
767 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
768 error ("data type of %q+D isn%'t suitable for a register",
769 decl);
770 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
771 error ("register specified for %q+D isn%'t suitable for data type",
772 decl);
773 /* Now handle properly declared static register variables. */
774 else
776 int nregs;
778 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
780 DECL_INITIAL (decl) = 0;
781 error ("global register variable has initial value");
783 if (TREE_THIS_VOLATILE (decl))
784 warning (OPT_Wvolatile_register_var,
785 "optimization may eliminate reads and/or "
786 "writes to register variables");
788 /* If the user specified one of the eliminables registers here,
789 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
790 confused with that register and be eliminated. This usage is
791 somewhat suspect... */
793 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
794 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
795 REG_USERVAR_P (DECL_RTL (decl)) = 1;
797 if (TREE_STATIC (decl))
799 /* Make this register global, so not usable for anything
800 else. */
801 #ifdef ASM_DECLARE_REGISTER_GLOBAL
802 name = IDENTIFIER_POINTER (DECL_NAME (decl));
803 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
804 #endif
805 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
806 while (nregs > 0)
807 globalize_reg (reg_number + --nregs);
810 /* As a register variable, it has no section. */
811 return;
814 /* Now handle ordinary static variables and functions (in memory).
815 Also handle vars declared register invalidly. */
816 else if (name[0] == '*')
818 #ifdef REGISTER_PREFIX
819 if (strlen (REGISTER_PREFIX) != 0)
821 reg_number = decode_reg_name (name);
822 if (reg_number >= 0 || reg_number == -3)
823 error ("register name given for non-register variable %q+D", decl);
825 #endif
828 /* Specifying a section attribute on a variable forces it into a
829 non-.bss section, and thus it cannot be common. */
830 if (TREE_CODE (decl) == VAR_DECL
831 && DECL_SECTION_NAME (decl) != NULL_TREE
832 && DECL_INITIAL (decl) == NULL_TREE
833 && DECL_COMMON (decl))
834 DECL_COMMON (decl) = 0;
836 /* Variables can't be both common and weak. */
837 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
838 DECL_COMMON (decl) = 0;
840 x = gen_rtx_SYMBOL_REF (Pmode, name);
841 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
842 SYMBOL_REF_DECL (x) = decl;
844 x = gen_rtx_MEM (DECL_MODE (decl), x);
845 if (TREE_CODE (decl) != FUNCTION_DECL)
846 set_mem_attributes (x, decl, 1);
847 SET_DECL_RTL (decl, x);
849 /* Optionally set flags or add text to the name to record information
850 such as that it is a function name.
851 If the name is changed, the macro ASM_OUTPUT_LABELREF
852 will have to know how to strip this information. */
853 targetm.encode_section_info (decl, DECL_RTL (decl), true);
855 /* Make this function static known to the mudflap runtime. */
856 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
857 mudflap_enqueue_decl (decl);
860 /* Make the rtl for variable VAR be volatile.
861 Use this only for static variables. */
863 void
864 make_var_volatile (tree var)
866 gcc_assert (MEM_P (DECL_RTL (var)));
868 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
871 /* Output a string of literal assembler code
872 for an `asm' keyword used between functions. */
874 void
875 assemble_asm (tree string)
877 app_enable ();
879 if (TREE_CODE (string) == ADDR_EXPR)
880 string = TREE_OPERAND (string, 0);
882 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
885 /* Record an element in the table of global destructors. SYMBOL is
886 a SYMBOL_REF of the function to be called; PRIORITY is a number
887 between 0 and MAX_INIT_PRIORITY. */
889 void
890 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
891 int priority ATTRIBUTE_UNUSED)
893 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
894 /* Tell GNU LD that this is part of the static destructor set.
895 This will work for any system that uses stabs, most usefully
896 aout systems. */
897 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
898 dbxout_stab_value_label (XSTR (symbol, 0));
899 #else
900 sorry ("global destructors not supported on this target");
901 #endif
904 void
905 default_named_section_asm_out_destructor (rtx symbol, int priority)
907 const char *section = ".dtors";
908 char buf[16];
910 /* ??? This only works reliably with the GNU linker. */
911 if (priority != DEFAULT_INIT_PRIORITY)
913 sprintf (buf, ".dtors.%.5u",
914 /* Invert the numbering so the linker puts us in the proper
915 order; constructors are run from right to left, and the
916 linker sorts in increasing order. */
917 MAX_INIT_PRIORITY - priority);
918 section = buf;
921 switch_to_section (get_section (section, SECTION_WRITE, NULL));
922 assemble_align (POINTER_SIZE);
923 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
926 #ifdef DTORS_SECTION_ASM_OP
927 void
928 default_dtor_section_asm_out_destructor (rtx symbol,
929 int priority ATTRIBUTE_UNUSED)
931 switch_to_section (dtors_section);
932 assemble_align (POINTER_SIZE);
933 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
935 #endif
937 /* Likewise for global constructors. */
939 void
940 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
941 int priority ATTRIBUTE_UNUSED)
943 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
944 /* Tell GNU LD that this is part of the static destructor set.
945 This will work for any system that uses stabs, most usefully
946 aout systems. */
947 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
948 dbxout_stab_value_label (XSTR (symbol, 0));
949 #else
950 sorry ("global constructors not supported on this target");
951 #endif
954 void
955 default_named_section_asm_out_constructor (rtx symbol, int priority)
957 const char *section = ".ctors";
958 char buf[16];
960 /* ??? This only works reliably with the GNU linker. */
961 if (priority != DEFAULT_INIT_PRIORITY)
963 sprintf (buf, ".ctors.%.5u",
964 /* Invert the numbering so the linker puts us in the proper
965 order; constructors are run from right to left, and the
966 linker sorts in increasing order. */
967 MAX_INIT_PRIORITY - priority);
968 section = buf;
971 switch_to_section (get_section (section, SECTION_WRITE, NULL));
972 assemble_align (POINTER_SIZE);
973 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
976 #ifdef CTORS_SECTION_ASM_OP
977 void
978 default_ctor_section_asm_out_constructor (rtx symbol,
979 int priority ATTRIBUTE_UNUSED)
981 switch_to_section (ctors_section);
982 assemble_align (POINTER_SIZE);
983 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
985 #endif
987 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
988 a nonzero value if the constant pool should be output before the
989 start of the function, or a zero value if the pool should output
990 after the end of the function. The default is to put it before the
991 start. */
993 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
994 #define CONSTANT_POOL_BEFORE_FUNCTION 1
995 #endif
997 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
998 to be output to assembler.
999 Set first_global_object_name and weak_global_object_name as appropriate. */
1001 void
1002 notice_global_symbol (tree decl)
1004 const char **type = &first_global_object_name;
1006 if (first_global_object_name
1007 || !TREE_PUBLIC (decl) || DECL_EXTERNAL (decl)
1008 || !DECL_NAME (decl)
1009 || (TREE_CODE (decl) != FUNCTION_DECL
1010 && (TREE_CODE (decl) != VAR_DECL
1011 || (DECL_COMMON (decl)
1012 && (DECL_INITIAL (decl) == 0
1013 || DECL_INITIAL (decl) == error_mark_node))))
1014 || !MEM_P (DECL_RTL (decl)))
1015 return;
1017 /* We win when global object is found, but it is useful to know about weak
1018 symbol as well so we can produce nicer unique names. */
1019 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1020 type = &weak_global_object_name;
1022 if (!*type)
1024 const char *p;
1025 const char *name;
1026 rtx decl_rtl = DECL_RTL (decl);
1028 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1029 name = ggc_strdup (p);
1031 *type = name;
1035 /* Output assembler code for the constant pool of a function and associated
1036 with defining the name of the function. DECL describes the function.
1037 NAME is the function's name. For the constant pool, we use the current
1038 constant pool data. */
1040 void
1041 assemble_start_function (tree decl, const char *fnname)
1043 int align;
1044 char tmp_label[100];
1045 bool hot_label_written = false;
1047 cfun->unlikely_text_section_name = NULL;
1049 first_function_block_is_cold = false;
1050 if (flag_reorder_blocks_and_partition)
1052 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1053 cfun->hot_section_label = ggc_strdup (tmp_label);
1054 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1055 cfun->cold_section_label = ggc_strdup (tmp_label);
1056 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1057 cfun->hot_section_end_label = ggc_strdup (tmp_label);
1058 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1059 cfun->cold_section_end_label = ggc_strdup (tmp_label);
1060 const_labelno++;
1062 else
1064 cfun->hot_section_label = NULL;
1065 cfun->cold_section_label = NULL;
1066 cfun->hot_section_end_label = NULL;
1067 cfun->cold_section_end_label = NULL;
1070 /* The following code does not need preprocessing in the assembler. */
1072 app_disable ();
1074 if (CONSTANT_POOL_BEFORE_FUNCTION)
1075 output_constant_pool (fnname, decl);
1077 resolve_unique_section (decl, 0, flag_function_sections);
1079 /* Make sure the not and cold text (code) sections are properly
1080 aligned. This is necessary here in the case where the function
1081 has both hot and cold sections, because we don't want to re-set
1082 the alignment when the section switch happens mid-function. */
1084 if (flag_reorder_blocks_and_partition)
1086 unlikely_text_section ();
1087 assemble_align (FUNCTION_BOUNDARY);
1088 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_label);
1090 /* When the function starts with a cold section, we need to explicitly
1091 align the hot section and write out the hot section label.
1092 But if the current function is a thunk, we do not have a CFG. */
1093 if (!current_function_is_thunk
1094 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1096 switch_to_section (text_section);
1097 assemble_align (FUNCTION_BOUNDARY);
1098 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1099 hot_label_written = true;
1100 first_function_block_is_cold = true;
1103 else if (DECL_SECTION_NAME (decl))
1105 /* Calls to function_section rely on first_function_block_is_cold
1106 being accurate. The first block may be cold even if we aren't
1107 doing partitioning, if the entire function was decided by
1108 choose_function_section (predict.c) to be cold. */
1110 initialize_cold_section_name ();
1112 if (cfun->unlikely_text_section_name
1113 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1114 cfun->unlikely_text_section_name) == 0)
1115 first_function_block_is_cold = true;
1118 last_text_section = NULL;
1120 /* Switch to the correct text section for the start of the function. */
1122 switch_to_section (function_section (decl));
1123 if (flag_reorder_blocks_and_partition
1124 && !hot_label_written)
1125 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1127 /* Tell assembler to move to target machine's alignment for functions. */
1128 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1129 if (align < force_align_functions_log)
1130 align = force_align_functions_log;
1131 if (align > 0)
1133 ASM_OUTPUT_ALIGN (asm_out_file, align);
1136 /* Handle a user-specified function alignment.
1137 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1138 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1139 if (align_functions_log > align
1140 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1142 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1143 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1144 align_functions_log, align_functions - 1);
1145 #else
1146 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1147 #endif
1150 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1151 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1152 #endif
1154 (*debug_hooks->begin_function) (decl);
1156 /* Make function name accessible from other files, if appropriate. */
1158 if (TREE_PUBLIC (decl))
1160 notice_global_symbol (decl);
1162 globalize_decl (decl);
1164 maybe_assemble_visibility (decl);
1167 if (DECL_PRESERVE_P (decl))
1168 targetm.asm_out.mark_decl_preserved (fnname);
1170 /* Do any machine/system dependent processing of the function name. */
1171 #ifdef ASM_DECLARE_FUNCTION_NAME
1172 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1173 #else
1174 /* Standard thing is just output label for the function. */
1175 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1176 #endif /* ASM_DECLARE_FUNCTION_NAME */
1179 /* Output assembler code associated with defining the size of the
1180 function. DECL describes the function. NAME is the function's name. */
1182 void
1183 assemble_end_function (tree decl, const char *fnname)
1185 #ifdef ASM_DECLARE_FUNCTION_SIZE
1186 /* We could have switched section in the middle of the function. */
1187 if (flag_reorder_blocks_and_partition)
1188 switch_to_section (function_section (decl));
1189 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1190 #endif
1191 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1193 output_constant_pool (fnname, decl);
1194 switch_to_section (function_section (decl)); /* need to switch back */
1196 /* Output labels for end of hot/cold text sections (to be used by
1197 debug info.) */
1198 if (flag_reorder_blocks_and_partition)
1200 section *save_text_section;
1202 save_text_section = in_section;
1203 unlikely_text_section ();
1204 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_end_label);
1205 if (first_function_block_is_cold)
1206 switch_to_section (text_section);
1207 else
1208 switch_to_section (function_section (decl));
1209 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_end_label);
1210 switch_to_section (save_text_section);
1214 /* Assemble code to leave SIZE bytes of zeros. */
1216 void
1217 assemble_zeros (unsigned HOST_WIDE_INT size)
1219 /* Do no output if -fsyntax-only. */
1220 if (flag_syntax_only)
1221 return;
1223 #ifdef ASM_NO_SKIP_IN_TEXT
1224 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1225 so we must output 0s explicitly in the text section. */
1226 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1228 unsigned HOST_WIDE_INT i;
1229 for (i = 0; i < size; i++)
1230 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1232 else
1233 #endif
1234 if (size > 0)
1235 ASM_OUTPUT_SKIP (asm_out_file, size);
1238 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1240 void
1241 assemble_align (int align)
1243 if (align > BITS_PER_UNIT)
1245 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1249 /* Assemble a string constant with the specified C string as contents. */
1251 void
1252 assemble_string (const char *p, int size)
1254 int pos = 0;
1255 int maximum = 2000;
1257 /* If the string is very long, split it up. */
1259 while (pos < size)
1261 int thissize = size - pos;
1262 if (thissize > maximum)
1263 thissize = maximum;
1265 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1267 pos += thissize;
1268 p += thissize;
1273 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1274 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1275 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1276 #else
1277 #if defined ASM_OUTPUT_ALIGNED_LOCAL
1278 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1279 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl))
1280 #else
1281 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1282 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded)
1283 #endif
1284 #endif
1286 #if defined ASM_OUTPUT_ALIGNED_BSS
1287 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1288 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1289 #else
1290 #if defined ASM_OUTPUT_BSS
1291 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1292 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded)
1293 #else
1294 #undef ASM_EMIT_BSS
1295 #endif
1296 #endif
1298 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1299 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1300 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1301 #else
1302 #if defined ASM_OUTPUT_ALIGNED_COMMON
1303 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1304 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl))
1305 #else
1306 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1307 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded)
1308 #endif
1309 #endif
1311 static bool
1312 asm_emit_uninitialised (tree decl, const char *name,
1313 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1314 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1316 enum
1318 asm_dest_common,
1319 asm_dest_bss,
1320 asm_dest_local
1322 destination = asm_dest_local;
1324 /* ??? We should handle .bss via select_section mechanisms rather than
1325 via special target hooks. That would eliminate this special case. */
1326 if (TREE_PUBLIC (decl))
1328 if (!DECL_COMMON (decl))
1329 #ifdef ASM_EMIT_BSS
1330 destination = asm_dest_bss;
1331 #else
1332 return false;
1333 #endif
1334 else
1335 destination = asm_dest_common;
1338 if (destination != asm_dest_common)
1340 resolve_unique_section (decl, 0, flag_data_sections);
1341 /* Custom sections don't belong here. */
1342 if (DECL_SECTION_NAME (decl))
1343 return false;
1346 if (destination == asm_dest_bss)
1347 globalize_decl (decl);
1349 if (flag_shared_data)
1351 switch (destination)
1353 #ifdef ASM_OUTPUT_SHARED_BSS
1354 case asm_dest_bss:
1355 ASM_OUTPUT_SHARED_BSS (asm_out_file, decl, name, size, rounded);
1356 return;
1357 #endif
1358 #ifdef ASM_OUTPUT_SHARED_COMMON
1359 case asm_dest_common:
1360 ASM_OUTPUT_SHARED_COMMON (asm_out_file, name, size, rounded);
1361 return;
1362 #endif
1363 #ifdef ASM_OUTPUT_SHARED_LOCAL
1364 case asm_dest_local:
1365 ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded);
1366 return;
1367 #endif
1368 default:
1369 break;
1373 switch (destination)
1375 #ifdef ASM_EMIT_BSS
1376 case asm_dest_bss:
1377 ASM_EMIT_BSS (decl, name, size, rounded);
1378 break;
1379 #endif
1380 case asm_dest_common:
1381 ASM_EMIT_COMMON (decl, name, size, rounded);
1382 break;
1383 case asm_dest_local:
1384 ASM_EMIT_LOCAL (decl, name, size, rounded);
1385 break;
1386 default:
1387 gcc_unreachable ();
1390 return true;
1393 /* Assemble everything that is needed for a variable or function declaration.
1394 Not used for automatic variables, and not used for function definitions.
1395 Should not be called for variables of incomplete structure type.
1397 TOP_LEVEL is nonzero if this variable has file scope.
1398 AT_END is nonzero if this is the special handling, at end of compilation,
1399 to define things that have had only tentative definitions.
1400 DONT_OUTPUT_DATA if nonzero means don't actually output the
1401 initial value (that will be done by the caller). */
1403 void
1404 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1405 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1407 const char *name;
1408 unsigned int align;
1409 int reloc = 0;
1410 rtx decl_rtl;
1412 if (lang_hooks.decls.prepare_assemble_variable)
1413 lang_hooks.decls.prepare_assemble_variable (decl);
1415 last_assemble_variable_decl = 0;
1417 /* Normally no need to say anything here for external references,
1418 since assemble_external is called by the language-specific code
1419 when a declaration is first seen. */
1421 if (DECL_EXTERNAL (decl))
1422 return;
1424 /* Output no assembler code for a function declaration.
1425 Only definitions of functions output anything. */
1427 if (TREE_CODE (decl) == FUNCTION_DECL)
1428 return;
1430 /* Do nothing for global register variables. */
1431 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1433 TREE_ASM_WRITTEN (decl) = 1;
1434 return;
1437 /* If type was incomplete when the variable was declared,
1438 see if it is complete now. */
1440 if (DECL_SIZE (decl) == 0)
1441 layout_decl (decl, 0);
1443 /* Still incomplete => don't allocate it; treat the tentative defn
1444 (which is what it must have been) as an `extern' reference. */
1446 if (!dont_output_data && DECL_SIZE (decl) == 0)
1448 error ("storage size of %q+D isn%'t known", decl);
1449 TREE_ASM_WRITTEN (decl) = 1;
1450 return;
1453 /* The first declaration of a variable that comes through this function
1454 decides whether it is global (in C, has external linkage)
1455 or local (in C, has internal linkage). So do nothing more
1456 if this function has already run. */
1458 if (TREE_ASM_WRITTEN (decl))
1459 return;
1461 /* Make sure targetm.encode_section_info is invoked before we set
1462 ASM_WRITTEN. */
1463 decl_rtl = DECL_RTL (decl);
1465 TREE_ASM_WRITTEN (decl) = 1;
1467 /* Do no output if -fsyntax-only. */
1468 if (flag_syntax_only)
1469 return;
1471 app_disable ();
1473 if (! dont_output_data
1474 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1476 error ("size of variable %q+D is too large", decl);
1477 return;
1480 name = XSTR (XEXP (decl_rtl, 0), 0);
1481 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1482 notice_global_symbol (decl);
1484 /* Compute the alignment of this data. */
1486 align = DECL_ALIGN (decl);
1488 /* In the case for initialing an array whose length isn't specified,
1489 where we have not yet been able to do the layout,
1490 figure out the proper alignment now. */
1491 if (dont_output_data && DECL_SIZE (decl) == 0
1492 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1493 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1495 /* Some object file formats have a maximum alignment which they support.
1496 In particular, a.out format supports a maximum alignment of 4. */
1497 if (align > MAX_OFILE_ALIGNMENT)
1499 warning (0, "alignment of %q+D is greater than maximum object "
1500 "file alignment. Using %d", decl,
1501 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1502 align = MAX_OFILE_ALIGNMENT;
1505 /* On some machines, it is good to increase alignment sometimes. */
1506 if (! DECL_USER_ALIGN (decl))
1508 #ifdef DATA_ALIGNMENT
1509 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1510 #endif
1511 #ifdef CONSTANT_ALIGNMENT
1512 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1513 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1514 #endif
1517 /* Reset the alignment in case we have made it tighter, so we can benefit
1518 from it in get_pointer_alignment. */
1519 DECL_ALIGN (decl) = align;
1520 set_mem_align (decl_rtl, align);
1522 if (TREE_PUBLIC (decl))
1523 maybe_assemble_visibility (decl);
1525 if (DECL_PRESERVE_P (decl))
1526 targetm.asm_out.mark_decl_preserved (name);
1528 /* Handle uninitialized definitions. */
1530 /* If the decl has been given an explicit section name, then it
1531 isn't common, and shouldn't be handled as such. */
1532 if (DECL_SECTION_NAME (decl) || dont_output_data)
1534 /* We don't implement common thread-local data at present. */
1535 else if (DECL_THREAD_LOCAL_P (decl))
1537 if (DECL_COMMON (decl))
1538 sorry ("thread-local COMMON data not implemented");
1540 else if (DECL_INITIAL (decl) == 0
1541 || DECL_INITIAL (decl) == error_mark_node
1542 || (flag_zero_initialized_in_bss
1543 /* Leave constant zeroes in .rodata so they can be shared. */
1544 && !TREE_READONLY (decl)
1545 && initializer_zerop (DECL_INITIAL (decl))))
1547 unsigned HOST_WIDE_INT size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1548 unsigned HOST_WIDE_INT rounded = size;
1550 /* Don't allocate zero bytes of common,
1551 since that means "undefined external" in the linker. */
1552 if (size == 0)
1553 rounded = 1;
1555 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1556 so that each uninitialized object starts on such a boundary. */
1557 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1558 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1559 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1561 #if !defined(ASM_OUTPUT_ALIGNED_COMMON) && !defined(ASM_OUTPUT_ALIGNED_DECL_COMMON) && !defined(ASM_OUTPUT_ALIGNED_BSS)
1562 if ((unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1563 warning (0, "requested alignment for %q+D is greater than "
1564 "implemented alignment of %wu", decl, rounded);
1565 #endif
1567 /* If the target cannot output uninitialized but not common global data
1568 in .bss, then we have to use .data, so fall through. */
1569 if (asm_emit_uninitialised (decl, name, size, rounded))
1570 return;
1573 /* Handle initialized definitions.
1574 Also handle uninitialized global definitions if -fno-common and the
1575 target doesn't support ASM_OUTPUT_BSS. */
1577 /* First make the assembler name(s) global if appropriate. */
1578 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1579 globalize_decl (decl);
1581 /* Output any data that we will need to use the address of. */
1582 if (DECL_INITIAL (decl) == error_mark_node)
1583 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1584 else if (DECL_INITIAL (decl))
1586 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1587 output_addressed_constants (DECL_INITIAL (decl));
1590 /* Switch to the appropriate section. */
1591 resolve_unique_section (decl, reloc, flag_data_sections);
1592 variable_section (decl, reloc);
1594 /* dbxout.c needs to know this. */
1595 if (in_section && (in_section->common.flags & SECTION_CODE) != 0)
1596 DECL_IN_TEXT_SECTION (decl) = 1;
1598 /* Output the alignment of this data. */
1599 if (align > BITS_PER_UNIT)
1600 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1602 /* Do any machine/system dependent processing of the object. */
1603 #ifdef ASM_DECLARE_OBJECT_NAME
1604 last_assemble_variable_decl = decl;
1605 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1606 #else
1607 /* Standard thing is just output label for the object. */
1608 ASM_OUTPUT_LABEL (asm_out_file, name);
1609 #endif /* ASM_DECLARE_OBJECT_NAME */
1611 if (!dont_output_data)
1613 if (DECL_INITIAL (decl)
1614 && DECL_INITIAL (decl) != error_mark_node
1615 && !initializer_zerop (DECL_INITIAL (decl)))
1616 /* Output the actual data. */
1617 output_constant (DECL_INITIAL (decl),
1618 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1619 align);
1620 else
1621 /* Leave space for it. */
1622 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1626 /* Return 1 if type TYPE contains any pointers. */
1628 static int
1629 contains_pointers_p (tree type)
1631 switch (TREE_CODE (type))
1633 case POINTER_TYPE:
1634 case REFERENCE_TYPE:
1635 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1636 so I'll play safe and return 1. */
1637 case OFFSET_TYPE:
1638 return 1;
1640 case RECORD_TYPE:
1641 case UNION_TYPE:
1642 case QUAL_UNION_TYPE:
1644 tree fields;
1645 /* For a type that has fields, see if the fields have pointers. */
1646 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1647 if (TREE_CODE (fields) == FIELD_DECL
1648 && contains_pointers_p (TREE_TYPE (fields)))
1649 return 1;
1650 return 0;
1653 case ARRAY_TYPE:
1654 /* An array type contains pointers if its element type does. */
1655 return contains_pointers_p (TREE_TYPE (type));
1657 default:
1658 return 0;
1662 /* In unit-at-a-time mode, we delay assemble_external processing until
1663 the compilation unit is finalized. This is the best we can do for
1664 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1665 it all the way to final. See PR 17982 for further discussion. */
1666 static GTY(()) tree pending_assemble_externals;
1668 #ifdef ASM_OUTPUT_EXTERNAL
1669 /* True if DECL is a function decl for which no out-of-line copy exists.
1670 It is assumed that DECL's assembler name has been set. */
1672 static bool
1673 incorporeal_function_p (tree decl)
1675 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1677 const char *name;
1679 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1680 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1681 return true;
1683 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1684 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1685 return true;
1687 return false;
1690 /* Actually do the tests to determine if this is necessary, and invoke
1691 ASM_OUTPUT_EXTERNAL. */
1692 static void
1693 assemble_external_real (tree decl)
1695 rtx rtl = DECL_RTL (decl);
1697 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1698 && !SYMBOL_REF_USED (XEXP (rtl, 0))
1699 && !incorporeal_function_p (decl))
1701 /* Some systems do require some output. */
1702 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1703 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1706 #endif
1708 void
1709 process_pending_assemble_externals (void)
1711 #ifdef ASM_OUTPUT_EXTERNAL
1712 tree list;
1713 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1714 assemble_external_real (TREE_VALUE (list));
1716 pending_assemble_externals = 0;
1717 #endif
1720 /* Output something to declare an external symbol to the assembler.
1721 (Most assemblers don't need this, so we normally output nothing.)
1722 Do nothing if DECL is not external. */
1724 void
1725 assemble_external (tree decl ATTRIBUTE_UNUSED)
1727 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1728 main body of this code is only rarely exercised. To provide some
1729 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1730 open. If it's not, we should not be calling this function. */
1731 gcc_assert (asm_out_file);
1733 #ifdef ASM_OUTPUT_EXTERNAL
1734 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1735 return;
1737 if (flag_unit_at_a_time)
1738 pending_assemble_externals = tree_cons (0, decl,
1739 pending_assemble_externals);
1740 else
1741 assemble_external_real (decl);
1742 #endif
1745 /* Similar, for calling a library function FUN. */
1747 void
1748 assemble_external_libcall (rtx fun)
1750 /* Declare library function name external when first used, if nec. */
1751 if (! SYMBOL_REF_USED (fun))
1753 SYMBOL_REF_USED (fun) = 1;
1754 targetm.asm_out.external_libcall (fun);
1758 /* Assemble a label named NAME. */
1760 void
1761 assemble_label (const char *name)
1763 ASM_OUTPUT_LABEL (asm_out_file, name);
1766 /* Set the symbol_referenced flag for ID. */
1767 void
1768 mark_referenced (tree id)
1770 TREE_SYMBOL_REFERENCED (id) = 1;
1773 /* Set the symbol_referenced flag for DECL and notify callgraph. */
1774 void
1775 mark_decl_referenced (tree decl)
1777 if (TREE_CODE (decl) == FUNCTION_DECL)
1779 /* Extern inline functions don't become needed when referenced.
1780 If we know a method will be emitted in other TU and no new
1781 functions can be marked reachable, just use the external
1782 definition. */
1783 struct cgraph_node *node = cgraph_node (decl);
1784 if (!DECL_EXTERNAL (decl)
1785 && (!node->local.vtable_method || !cgraph_global_info_ready
1786 || !node->local.finalized))
1787 cgraph_mark_needed_node (node);
1789 else if (TREE_CODE (decl) == VAR_DECL)
1791 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
1792 cgraph_varpool_mark_needed_node (node);
1793 /* C++ frontend use mark_decl_references to force COMDAT variables
1794 to be output that might appear dead otherwise. */
1795 node->force_output = true;
1797 /* else do nothing - we can get various sorts of CST nodes here,
1798 which do not need to be marked. */
1802 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1803 until we find an identifier that is not itself a transparent alias.
1804 Modify the alias passed to it by reference (and all aliases on the
1805 way to the ultimate target), such that they do not have to be
1806 followed again, and return the ultimate target of the alias
1807 chain. */
1809 static inline tree
1810 ultimate_transparent_alias_target (tree *alias)
1812 tree target = *alias;
1814 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1816 gcc_assert (TREE_CHAIN (target));
1817 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1818 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1819 && ! TREE_CHAIN (target));
1820 *alias = target;
1823 return target;
1826 /* Output to FILE (an assembly file) a reference to NAME. If NAME
1827 starts with a *, the rest of NAME is output verbatim. Otherwise
1828 NAME is transformed in a target-specific way (usually by the
1829 addition of an underscore). */
1831 void
1832 assemble_name_raw (FILE *file, const char *name)
1834 if (name[0] == '*')
1835 fputs (&name[1], file);
1836 else
1837 ASM_OUTPUT_LABELREF (file, name);
1840 /* Like assemble_name_raw, but should be used when NAME might refer to
1841 an entity that is also represented as a tree (like a function or
1842 variable). If NAME does refer to such an entity, that entity will
1843 be marked as referenced. */
1845 void
1846 assemble_name (FILE *file, const char *name)
1848 const char *real_name;
1849 tree id;
1851 real_name = targetm.strip_name_encoding (name);
1853 id = maybe_get_identifier (real_name);
1854 if (id)
1856 tree id_orig = id;
1858 mark_referenced (id);
1859 ultimate_transparent_alias_target (&id);
1860 if (id != id_orig)
1861 name = IDENTIFIER_POINTER (id);
1862 gcc_assert (! TREE_CHAIN (id));
1865 assemble_name_raw (file, name);
1868 /* Allocate SIZE bytes writable static space with a gensym name
1869 and return an RTX to refer to its address. */
1872 assemble_static_space (unsigned HOST_WIDE_INT size)
1874 char name[12];
1875 const char *namestring;
1876 rtx x;
1878 #if 0
1879 if (flag_shared_data)
1880 switch_to_section (data_section);
1881 #endif
1883 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
1884 ++const_labelno;
1885 namestring = ggc_strdup (name);
1887 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
1888 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
1890 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
1891 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
1892 BIGGEST_ALIGNMENT);
1893 #else
1894 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
1895 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
1896 #else
1898 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1899 so that each uninitialized object starts on such a boundary. */
1900 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
1901 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
1902 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
1903 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1904 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1905 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1907 #endif
1908 #endif
1909 return x;
1912 /* Assemble the static constant template for function entry trampolines.
1913 This is done at most once per compilation.
1914 Returns an RTX for the address of the template. */
1916 static GTY(()) rtx initial_trampoline;
1918 #ifdef TRAMPOLINE_TEMPLATE
1920 assemble_trampoline_template (void)
1922 char label[256];
1923 const char *name;
1924 int align;
1925 rtx symbol;
1927 if (initial_trampoline)
1928 return initial_trampoline;
1930 /* By default, put trampoline templates in read-only data section. */
1932 #ifdef TRAMPOLINE_SECTION
1933 switch_to_section (TRAMPOLINE_SECTION);
1934 #else
1935 switch_to_section (readonly_data_section);
1936 #endif
1938 /* Write the assembler code to define one. */
1939 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
1940 if (align > 0)
1942 ASM_OUTPUT_ALIGN (asm_out_file, align);
1945 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
1946 TRAMPOLINE_TEMPLATE (asm_out_file);
1948 /* Record the rtl to refer to it. */
1949 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
1950 name = ggc_strdup (label);
1951 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
1952 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
1954 initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
1955 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
1957 return initial_trampoline;
1959 #endif
1961 /* A and B are either alignments or offsets. Return the minimum alignment
1962 that may be assumed after adding the two together. */
1964 static inline unsigned
1965 min_align (unsigned int a, unsigned int b)
1967 return (a | b) & -(a | b);
1970 /* Return the assembler directive for creating a given kind of integer
1971 object. SIZE is the number of bytes in the object and ALIGNED_P
1972 indicates whether it is known to be aligned. Return NULL if the
1973 assembly dialect has no such directive.
1975 The returned string should be printed at the start of a new line and
1976 be followed immediately by the object's initial value. */
1978 const char *
1979 integer_asm_op (int size, int aligned_p)
1981 struct asm_int_op *ops;
1983 if (aligned_p)
1984 ops = &targetm.asm_out.aligned_op;
1985 else
1986 ops = &targetm.asm_out.unaligned_op;
1988 switch (size)
1990 case 1:
1991 return targetm.asm_out.byte_op;
1992 case 2:
1993 return ops->hi;
1994 case 4:
1995 return ops->si;
1996 case 8:
1997 return ops->di;
1998 case 16:
1999 return ops->ti;
2000 default:
2001 return NULL;
2005 /* Use directive OP to assemble an integer object X. Print OP at the
2006 start of the line, followed immediately by the value of X. */
2008 void
2009 assemble_integer_with_op (const char *op, rtx x)
2011 fputs (op, asm_out_file);
2012 output_addr_const (asm_out_file, x);
2013 fputc ('\n', asm_out_file);
2016 /* The default implementation of the asm_out.integer target hook. */
2018 bool
2019 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2020 unsigned int size ATTRIBUTE_UNUSED,
2021 int aligned_p ATTRIBUTE_UNUSED)
2023 const char *op = integer_asm_op (size, aligned_p);
2024 /* Avoid GAS bugs for large values. Specifically negative values whose
2025 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2026 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2027 return false;
2028 return op && (assemble_integer_with_op (op, x), true);
2031 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2032 the alignment of the integer in bits. Return 1 if we were able to output
2033 the constant, otherwise 0. We must be able to output the constant,
2034 if FORCE is nonzero. */
2036 bool
2037 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2039 int aligned_p;
2041 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2043 /* See if the target hook can handle this kind of object. */
2044 if (targetm.asm_out.integer (x, size, aligned_p))
2045 return true;
2047 /* If the object is a multi-byte one, try splitting it up. Split
2048 it into words it if is multi-word, otherwise split it into bytes. */
2049 if (size > 1)
2051 enum machine_mode omode, imode;
2052 unsigned int subalign;
2053 unsigned int subsize, i;
2055 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2056 subalign = MIN (align, subsize * BITS_PER_UNIT);
2057 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
2058 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2060 for (i = 0; i < size; i += subsize)
2062 rtx partial = simplify_subreg (omode, x, imode, i);
2063 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2064 break;
2066 if (i == size)
2067 return true;
2069 /* If we've printed some of it, but not all of it, there's no going
2070 back now. */
2071 gcc_assert (!i);
2074 gcc_assert (!force);
2076 return false;
2079 void
2080 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2082 long data[4];
2083 int i;
2084 int bitsize, nelts, nunits, units_per;
2086 /* This is hairy. We have a quantity of known size. real_to_target
2087 will put it into an array of *host* longs, 32 bits per element
2088 (even if long is more than 32 bits). We need to determine the
2089 number of array elements that are occupied (nelts) and the number
2090 of *target* min-addressable units that will be occupied in the
2091 object file (nunits). We cannot assume that 32 divides the
2092 mode's bitsize (size * BITS_PER_UNIT) evenly.
2094 size * BITS_PER_UNIT is used here to make sure that padding bits
2095 (which might appear at either end of the value; real_to_target
2096 will include the padding bits in its output array) are included. */
2098 nunits = GET_MODE_SIZE (mode);
2099 bitsize = nunits * BITS_PER_UNIT;
2100 nelts = CEIL (bitsize, 32);
2101 units_per = 32 / BITS_PER_UNIT;
2103 real_to_target (data, &d, mode);
2105 /* Put out the first word with the specified alignment. */
2106 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2107 nunits -= units_per;
2109 /* Subsequent words need only 32-bit alignment. */
2110 align = min_align (align, 32);
2112 for (i = 1; i < nelts; i++)
2114 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2115 nunits -= units_per;
2119 /* Given an expression EXP with a constant value,
2120 reduce it to the sum of an assembler symbol and an integer.
2121 Store them both in the structure *VALUE.
2122 EXP must be reducible. */
2124 struct addr_const GTY(())
2126 rtx base;
2127 HOST_WIDE_INT offset;
2130 static void
2131 decode_addr_const (tree exp, struct addr_const *value)
2133 tree target = TREE_OPERAND (exp, 0);
2134 int offset = 0;
2135 rtx x;
2137 while (1)
2139 if (TREE_CODE (target) == COMPONENT_REF
2140 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2143 offset += int_byte_position (TREE_OPERAND (target, 1));
2144 target = TREE_OPERAND (target, 0);
2146 else if (TREE_CODE (target) == ARRAY_REF
2147 || TREE_CODE (target) == ARRAY_RANGE_REF)
2149 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2150 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2151 target = TREE_OPERAND (target, 0);
2153 else
2154 break;
2157 switch (TREE_CODE (target))
2159 case VAR_DECL:
2160 case FUNCTION_DECL:
2161 x = DECL_RTL (target);
2162 break;
2164 case LABEL_DECL:
2165 x = gen_rtx_MEM (FUNCTION_MODE,
2166 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2167 break;
2169 case REAL_CST:
2170 case STRING_CST:
2171 case COMPLEX_CST:
2172 case CONSTRUCTOR:
2173 case INTEGER_CST:
2174 x = output_constant_def (target, 1);
2175 break;
2177 default:
2178 gcc_unreachable ();
2181 gcc_assert (MEM_P (x));
2182 x = XEXP (x, 0);
2184 value->base = x;
2185 value->offset = offset;
2188 /* Uniquize all constants that appear in memory.
2189 Each constant in memory thus far output is recorded
2190 in `const_desc_table'. */
2192 struct constant_descriptor_tree GTY(())
2194 /* A MEM for the constant. */
2195 rtx rtl;
2197 /* The value of the constant. */
2198 tree value;
2200 /* Hash of value. Computing the hash from value each time
2201 hashfn is called can't work properly, as that means recursive
2202 use of the hash table during hash table expansion. */
2203 hashval_t hash;
2206 static GTY((param_is (struct constant_descriptor_tree)))
2207 htab_t const_desc_htab;
2209 static struct constant_descriptor_tree * build_constant_desc (tree);
2210 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2212 /* Compute a hash code for a constant expression. */
2214 static hashval_t
2215 const_desc_hash (const void *ptr)
2217 return ((struct constant_descriptor_tree *)ptr)->hash;
2220 static hashval_t
2221 const_hash_1 (const tree exp)
2223 const char *p;
2224 hashval_t hi;
2225 int len, i;
2226 enum tree_code code = TREE_CODE (exp);
2228 /* Either set P and LEN to the address and len of something to hash and
2229 exit the switch or return a value. */
2231 switch (code)
2233 case INTEGER_CST:
2234 p = (char *) &TREE_INT_CST (exp);
2235 len = sizeof TREE_INT_CST (exp);
2236 break;
2238 case REAL_CST:
2239 return real_hash (TREE_REAL_CST_PTR (exp));
2241 case STRING_CST:
2242 p = TREE_STRING_POINTER (exp);
2243 len = TREE_STRING_LENGTH (exp);
2244 break;
2246 case COMPLEX_CST:
2247 return (const_hash_1 (TREE_REALPART (exp)) * 5
2248 + const_hash_1 (TREE_IMAGPART (exp)));
2250 case CONSTRUCTOR:
2252 unsigned HOST_WIDE_INT idx;
2253 tree value;
2255 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2257 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2258 if (value)
2259 hi = hi * 603 + const_hash_1 (value);
2261 return hi;
2264 case ADDR_EXPR:
2265 case FDESC_EXPR:
2267 struct addr_const value;
2269 decode_addr_const (exp, &value);
2270 switch (GET_CODE (value.base))
2272 case SYMBOL_REF:
2273 /* Don't hash the address of the SYMBOL_REF;
2274 only use the offset and the symbol name. */
2275 hi = value.offset;
2276 p = XSTR (value.base, 0);
2277 for (i = 0; p[i] != 0; i++)
2278 hi = ((hi * 613) + (unsigned) (p[i]));
2279 break;
2281 case LABEL_REF:
2282 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2283 break;
2285 default:
2286 gcc_unreachable ();
2289 return hi;
2291 case PLUS_EXPR:
2292 case MINUS_EXPR:
2293 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2294 + const_hash_1 (TREE_OPERAND (exp, 1)));
2296 case NOP_EXPR:
2297 case CONVERT_EXPR:
2298 case NON_LVALUE_EXPR:
2299 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2301 default:
2302 /* A language specific constant. Just hash the code. */
2303 return code;
2306 /* Compute hashing function. */
2307 hi = len;
2308 for (i = 0; i < len; i++)
2309 hi = ((hi * 613) + (unsigned) (p[i]));
2311 return hi;
2314 /* Wrapper of compare_constant, for the htab interface. */
2315 static int
2316 const_desc_eq (const void *p1, const void *p2)
2318 const struct constant_descriptor_tree *c1 = p1;
2319 const struct constant_descriptor_tree *c2 = p2;
2320 if (c1->hash != c2->hash)
2321 return 0;
2322 return compare_constant (c1->value, c2->value);
2325 /* Compare t1 and t2, and return 1 only if they are known to result in
2326 the same bit pattern on output. */
2328 static int
2329 compare_constant (const tree t1, const tree t2)
2331 enum tree_code typecode;
2333 if (t1 == NULL_TREE)
2334 return t2 == NULL_TREE;
2335 if (t2 == NULL_TREE)
2336 return 0;
2338 if (TREE_CODE (t1) != TREE_CODE (t2))
2339 return 0;
2341 switch (TREE_CODE (t1))
2343 case INTEGER_CST:
2344 /* Integer constants are the same only if the same width of type. */
2345 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2346 return 0;
2347 return tree_int_cst_equal (t1, t2);
2349 case REAL_CST:
2350 /* Real constants are the same only if the same width of type. */
2351 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2352 return 0;
2354 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2356 case STRING_CST:
2357 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2358 return 0;
2360 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2361 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2362 TREE_STRING_LENGTH (t1)));
2364 case COMPLEX_CST:
2365 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2366 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2368 case CONSTRUCTOR:
2370 VEC(constructor_elt, gc) *v1, *v2;
2371 unsigned HOST_WIDE_INT idx;
2373 typecode = TREE_CODE (TREE_TYPE (t1));
2374 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2375 return 0;
2377 if (typecode == ARRAY_TYPE)
2379 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2380 /* For arrays, check that the sizes all match. */
2381 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2382 || size_1 == -1
2383 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2384 return 0;
2386 else
2388 /* For record and union constructors, require exact type
2389 equality. */
2390 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2391 return 0;
2394 v1 = CONSTRUCTOR_ELTS (t1);
2395 v2 = CONSTRUCTOR_ELTS (t2);
2396 if (VEC_length (constructor_elt, v1)
2397 != VEC_length (constructor_elt, v2))
2398 return 0;
2400 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2402 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2403 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2405 /* Check that each value is the same... */
2406 if (!compare_constant (c1->value, c2->value))
2407 return 0;
2408 /* ... and that they apply to the same fields! */
2409 if (typecode == ARRAY_TYPE)
2411 if (!compare_constant (c1->index, c2->index))
2412 return 0;
2414 else
2416 if (c1->index != c2->index)
2417 return 0;
2421 return 1;
2424 case ADDR_EXPR:
2425 case FDESC_EXPR:
2427 struct addr_const value1, value2;
2429 decode_addr_const (t1, &value1);
2430 decode_addr_const (t2, &value2);
2431 return (value1.offset == value2.offset
2432 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2435 case PLUS_EXPR:
2436 case MINUS_EXPR:
2437 case RANGE_EXPR:
2438 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2439 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2441 case NOP_EXPR:
2442 case CONVERT_EXPR:
2443 case NON_LVALUE_EXPR:
2444 case VIEW_CONVERT_EXPR:
2445 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2447 default:
2449 tree nt1, nt2;
2450 nt1 = lang_hooks.expand_constant (t1);
2451 nt2 = lang_hooks.expand_constant (t2);
2452 if (nt1 != t1 || nt2 != t2)
2453 return compare_constant (nt1, nt2);
2454 else
2455 return 0;
2459 gcc_unreachable ();
2462 /* Make a copy of the whole tree structure for a constant. This
2463 handles the same types of nodes that compare_constant handles. */
2465 static tree
2466 copy_constant (tree exp)
2468 switch (TREE_CODE (exp))
2470 case ADDR_EXPR:
2471 /* For ADDR_EXPR, we do not want to copy the decl whose address
2472 is requested. We do want to copy constants though. */
2473 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2474 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2475 copy_constant (TREE_OPERAND (exp, 0)));
2476 else
2477 return copy_node (exp);
2479 case INTEGER_CST:
2480 case REAL_CST:
2481 case STRING_CST:
2482 return copy_node (exp);
2484 case COMPLEX_CST:
2485 return build_complex (TREE_TYPE (exp),
2486 copy_constant (TREE_REALPART (exp)),
2487 copy_constant (TREE_IMAGPART (exp)));
2489 case PLUS_EXPR:
2490 case MINUS_EXPR:
2491 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2492 copy_constant (TREE_OPERAND (exp, 0)),
2493 copy_constant (TREE_OPERAND (exp, 1)));
2495 case NOP_EXPR:
2496 case CONVERT_EXPR:
2497 case NON_LVALUE_EXPR:
2498 case VIEW_CONVERT_EXPR:
2499 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2500 copy_constant (TREE_OPERAND (exp, 0)));
2502 case CONSTRUCTOR:
2504 tree copy = copy_node (exp);
2505 VEC(constructor_elt, gc) *v;
2506 unsigned HOST_WIDE_INT idx;
2507 tree purpose, value;
2509 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2510 CONSTRUCTOR_ELTS (exp)));
2511 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2513 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2514 ce->index = purpose;
2515 ce->value = copy_constant (value);
2517 CONSTRUCTOR_ELTS (copy) = v;
2518 return copy;
2521 default:
2523 tree t = lang_hooks.expand_constant (exp);
2525 gcc_assert (t == exp);
2526 return copy_constant (t);
2531 /* Subroutine of output_constant_def:
2532 No constant equal to EXP is known to have been output.
2533 Make a constant descriptor to enter EXP in the hash table.
2534 Assign the label number and construct RTL to refer to the
2535 constant's location in memory.
2536 Caller is responsible for updating the hash table. */
2538 static struct constant_descriptor_tree *
2539 build_constant_desc (tree exp)
2541 rtx symbol;
2542 rtx rtl;
2543 char label[256];
2544 int labelno;
2545 struct constant_descriptor_tree *desc;
2547 desc = ggc_alloc (sizeof (*desc));
2548 desc->value = copy_constant (exp);
2550 /* Propagate marked-ness to copied constant. */
2551 if (flag_mudflap && mf_marked_p (exp))
2552 mf_mark (desc->value);
2554 /* Create a string containing the label name, in LABEL. */
2555 labelno = const_labelno++;
2556 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2558 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
2559 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2560 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2561 SYMBOL_REF_DECL (symbol) = desc->value;
2562 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2564 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2565 set_mem_attributes (rtl, exp, 1);
2566 set_mem_alias_set (rtl, 0);
2567 set_mem_alias_set (rtl, const_alias_set);
2569 /* Set flags or add text to the name to record information, such as
2570 that it is a local symbol. If the name is changed, the macro
2571 ASM_OUTPUT_LABELREF will have to know how to strip this
2572 information. This call might invalidate our local variable
2573 SYMBOL; we can't use it afterward. */
2575 targetm.encode_section_info (exp, rtl, true);
2577 desc->rtl = rtl;
2579 return desc;
2582 /* Return an rtx representing a reference to constant data in memory
2583 for the constant expression EXP.
2585 If assembler code for such a constant has already been output,
2586 return an rtx to refer to it.
2587 Otherwise, output such a constant in memory
2588 and generate an rtx for it.
2590 If DEFER is nonzero, this constant can be deferred and output only
2591 if referenced in the function after all optimizations.
2593 `const_desc_table' records which constants already have label strings. */
2596 output_constant_def (tree exp, int defer)
2598 struct constant_descriptor_tree *desc;
2599 struct constant_descriptor_tree key;
2600 void **loc;
2602 /* Look up EXP in the table of constant descriptors. If we didn't find
2603 it, create a new one. */
2604 key.value = exp;
2605 key.hash = const_hash_1 (exp);
2606 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
2608 desc = *loc;
2609 if (desc == 0)
2611 desc = build_constant_desc (exp);
2612 desc->hash = key.hash;
2613 *loc = desc;
2616 maybe_output_constant_def_contents (desc, defer);
2617 return desc->rtl;
2620 /* Subroutine of output_constant_def: Decide whether or not we need to
2621 output the constant DESC now, and if so, do it. */
2622 static void
2623 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2624 int defer)
2626 rtx symbol = XEXP (desc->rtl, 0);
2627 tree exp = desc->value;
2629 if (flag_syntax_only)
2630 return;
2632 if (TREE_ASM_WRITTEN (exp))
2633 /* Already output; don't do it again. */
2634 return;
2636 /* We can always defer constants as long as the context allows
2637 doing so. */
2638 if (defer)
2640 /* Increment n_deferred_constants if it exists. It needs to be at
2641 least as large as the number of constants actually referred to
2642 by the function. If it's too small we'll stop looking too early
2643 and fail to emit constants; if it's too large we'll only look
2644 through the entire function when we could have stopped earlier. */
2645 if (cfun)
2646 n_deferred_constants++;
2647 return;
2650 output_constant_def_contents (symbol);
2653 /* We must output the constant data referred to by SYMBOL; do so. */
2655 static void
2656 output_constant_def_contents (rtx symbol)
2658 tree exp = SYMBOL_REF_DECL (symbol);
2659 const char *label = XSTR (symbol, 0);
2660 HOST_WIDE_INT size;
2662 /* Make sure any other constants whose addresses appear in EXP
2663 are assigned label numbers. */
2664 int reloc = compute_reloc_for_constant (exp);
2666 /* Align the location counter as required by EXP's data type. */
2667 unsigned int align = TYPE_ALIGN (TREE_TYPE (exp));
2668 #ifdef CONSTANT_ALIGNMENT
2669 align = CONSTANT_ALIGNMENT (exp, align);
2670 #endif
2672 output_addressed_constants (exp);
2674 /* We are no longer deferring this constant. */
2675 TREE_ASM_WRITTEN (exp) = 1;
2677 if (IN_NAMED_SECTION (exp))
2678 switch_to_section (get_named_section (exp, NULL, reloc));
2679 else
2680 switch_to_section (targetm.asm_out.select_section (exp, reloc, align));
2682 if (align > BITS_PER_UNIT)
2684 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2687 size = int_size_in_bytes (TREE_TYPE (exp));
2688 if (TREE_CODE (exp) == STRING_CST)
2689 size = MAX (TREE_STRING_LENGTH (exp), size);
2691 /* Do any machine/system dependent processing of the constant. */
2692 #ifdef ASM_DECLARE_CONSTANT_NAME
2693 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2694 #else
2695 /* Standard thing is just output label for the constant. */
2696 ASM_OUTPUT_LABEL (asm_out_file, label);
2697 #endif /* ASM_DECLARE_CONSTANT_NAME */
2699 /* Output the value of EXP. */
2700 output_constant (exp, size, align);
2701 if (flag_mudflap)
2702 mudflap_enqueue_constant (exp);
2705 /* Look up EXP in the table of constant descriptors. Return the rtl
2706 if it has been emitted, else null. */
2709 lookup_constant_def (tree exp)
2711 struct constant_descriptor_tree *desc;
2712 struct constant_descriptor_tree key;
2714 key.value = exp;
2715 key.hash = const_hash_1 (exp);
2716 desc = htab_find_with_hash (const_desc_htab, &key, key.hash);
2718 return (desc ? desc->rtl : NULL_RTX);
2721 /* Used in the hash tables to avoid outputting the same constant
2722 twice. Unlike 'struct constant_descriptor_tree', RTX constants
2723 are output once per function, not once per file. */
2724 /* ??? Only a few targets need per-function constant pools. Most
2725 can use one per-file pool. Should add a targetm bit to tell the
2726 difference. */
2728 struct rtx_constant_pool GTY(())
2730 /* Pointers to first and last constant in pool, as ordered by offset. */
2731 struct constant_descriptor_rtx *first;
2732 struct constant_descriptor_rtx *last;
2734 /* Hash facility for making memory-constants from constant rtl-expressions.
2735 It is used on RISC machines where immediate integer arguments and
2736 constant addresses are restricted so that such constants must be stored
2737 in memory. */
2738 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
2739 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_sym_htab;
2741 /* Current offset in constant pool (does not include any
2742 machine-specific header). */
2743 HOST_WIDE_INT offset;
2746 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
2748 struct constant_descriptor_rtx *next;
2749 rtx mem;
2750 rtx sym;
2751 rtx constant;
2752 HOST_WIDE_INT offset;
2753 hashval_t hash;
2754 enum machine_mode mode;
2755 unsigned int align;
2756 int labelno;
2757 int mark;
2760 /* Hash and compare functions for const_rtx_htab. */
2762 static hashval_t
2763 const_desc_rtx_hash (const void *ptr)
2765 const struct constant_descriptor_rtx *desc = ptr;
2766 return desc->hash;
2769 static int
2770 const_desc_rtx_eq (const void *a, const void *b)
2772 const struct constant_descriptor_rtx *x = a;
2773 const struct constant_descriptor_rtx *y = b;
2775 if (x->mode != y->mode)
2776 return 0;
2777 return rtx_equal_p (x->constant, y->constant);
2780 /* Hash and compare functions for const_rtx_sym_htab. */
2782 static hashval_t
2783 const_desc_rtx_sym_hash (const void *ptr)
2785 const struct constant_descriptor_rtx *desc = ptr;
2786 return htab_hash_string (XSTR (desc->sym, 0));
2789 static int
2790 const_desc_rtx_sym_eq (const void *a, const void *b)
2792 const struct constant_descriptor_rtx *x = a;
2793 const struct constant_descriptor_rtx *y = b;
2794 return XSTR (x->sym, 0) == XSTR (y->sym, 0);
2797 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
2799 static int
2800 const_rtx_hash_1 (rtx *xp, void *data)
2802 unsigned HOST_WIDE_INT hwi;
2803 enum machine_mode mode;
2804 enum rtx_code code;
2805 hashval_t h, *hp;
2806 rtx x;
2808 x = *xp;
2809 code = GET_CODE (x);
2810 mode = GET_MODE (x);
2811 h = (hashval_t) code * 1048573 + mode;
2813 switch (code)
2815 case CONST_INT:
2816 hwi = INTVAL (x);
2817 fold_hwi:
2819 const int shift = sizeof (hashval_t) * CHAR_BIT;
2820 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
2821 int i;
2823 h ^= (hashval_t) hwi;
2824 for (i = 1; i < n; ++i)
2826 hwi >>= shift;
2827 h ^= (hashval_t) hwi;
2830 break;
2832 case CONST_DOUBLE:
2833 if (mode == VOIDmode)
2835 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
2836 goto fold_hwi;
2838 else
2839 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
2840 break;
2842 case CONST_VECTOR:
2844 int i;
2845 for (i = XVECLEN (x, 0); i-- > 0; )
2846 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
2848 break;
2850 case SYMBOL_REF:
2851 h ^= htab_hash_string (XSTR (x, 0));
2852 break;
2854 case LABEL_REF:
2855 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
2856 break;
2858 case UNSPEC:
2859 case UNSPEC_VOLATILE:
2860 h = h * 251 + XINT (x, 1);
2861 break;
2863 default:
2864 break;
2867 hp = data;
2868 *hp = *hp * 509 + h;
2869 return 0;
2872 /* Compute a hash value for X, which should be a constant. */
2874 static hashval_t
2875 const_rtx_hash (rtx x)
2877 hashval_t h = 0;
2878 for_each_rtx (&x, const_rtx_hash_1, &h);
2879 return h;
2883 /* Initialize constant pool hashing for a new function. */
2885 void
2886 init_varasm_status (struct function *f)
2888 struct varasm_status *p;
2889 struct rtx_constant_pool *pool;
2891 p = ggc_alloc (sizeof (struct varasm_status));
2892 f->varasm = p;
2894 pool = ggc_alloc (sizeof (struct rtx_constant_pool));
2895 p->pool = pool;
2896 p->deferred_constants = 0;
2898 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
2899 const_desc_rtx_eq, NULL);
2900 pool->const_rtx_sym_htab = htab_create_ggc (31, const_desc_rtx_sym_hash,
2901 const_desc_rtx_sym_eq, NULL);
2902 pool->first = pool->last = NULL;
2903 pool->offset = 0;
2906 /* Given a MINUS expression, simplify it if both sides
2907 include the same symbol. */
2910 simplify_subtraction (rtx x)
2912 rtx r = simplify_rtx (x);
2913 return r ? r : x;
2916 /* Given a constant rtx X, make (or find) a memory constant for its value
2917 and return a MEM rtx to refer to it in memory. */
2920 force_const_mem (enum machine_mode mode, rtx x)
2922 struct constant_descriptor_rtx *desc, tmp;
2923 struct rtx_constant_pool *pool = cfun->varasm->pool;
2924 char label[256];
2925 rtx def, symbol;
2926 hashval_t hash;
2927 unsigned int align;
2928 void **slot;
2930 /* If we're not allowed to drop X into the constant pool, don't. */
2931 if (targetm.cannot_force_const_mem (x))
2932 return NULL_RTX;
2934 /* Lookup the value in the hashtable. */
2935 tmp.constant = x;
2936 tmp.mode = mode;
2937 hash = const_rtx_hash (x);
2938 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
2939 desc = *slot;
2941 /* If the constant was already present, return its memory. */
2942 if (desc)
2943 return copy_rtx (desc->mem);
2945 /* Otherwise, create a new descriptor. */
2946 desc = ggc_alloc (sizeof (*desc));
2947 *slot = desc;
2949 /* Align the location counter as required by EXP's data type. */
2950 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
2951 #ifdef CONSTANT_ALIGNMENT
2953 tree type = lang_hooks.types.type_for_mode (mode, 0);
2954 if (type != NULL_TREE)
2955 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
2957 #endif
2959 pool->offset += (align / BITS_PER_UNIT) - 1;
2960 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
2962 desc->next = NULL;
2963 desc->constant = tmp.constant;
2964 desc->offset = pool->offset;
2965 desc->hash = hash;
2966 desc->mode = mode;
2967 desc->align = align;
2968 desc->labelno = const_labelno;
2969 desc->mark = 0;
2971 pool->offset += GET_MODE_SIZE (mode);
2972 if (pool->last)
2973 pool->last->next = desc;
2974 else
2975 pool->first = pool->last = desc;
2976 pool->last = desc;
2978 /* Create a string containing the label name, in LABEL. */
2979 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
2980 ++const_labelno;
2982 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
2983 the constants pool. */
2984 desc->sym = symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2985 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2986 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2987 current_function_uses_const_pool = 1;
2989 /* Insert the descriptor into the symbol cross-reference table too. */
2990 slot = htab_find_slot (pool->const_rtx_sym_htab, desc, INSERT);
2991 gcc_assert (!*slot);
2992 *slot = desc;
2994 /* Construct the MEM. */
2995 desc->mem = def = gen_const_mem (mode, symbol);
2996 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
2997 set_mem_align (def, align);
2999 /* If we're dropping a label to the constant pool, make sure we
3000 don't delete it. */
3001 if (GET_CODE (x) == LABEL_REF)
3002 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3004 return copy_rtx (def);
3007 /* Given a SYMBOL_REF with CONSTANT_POOL_ADDRESS_P true, return a pointer to
3008 the corresponding constant_descriptor_rtx structure. */
3010 static struct constant_descriptor_rtx *
3011 find_pool_constant (struct rtx_constant_pool *pool, rtx sym)
3013 struct constant_descriptor_rtx tmp;
3014 tmp.sym = sym;
3015 return htab_find (pool->const_rtx_sym_htab, &tmp);
3018 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3021 get_pool_constant (rtx addr)
3023 return find_pool_constant (cfun->varasm->pool, addr)->constant;
3026 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3027 and whether it has been output or not. */
3030 get_pool_constant_mark (rtx addr, bool *pmarked)
3032 struct constant_descriptor_rtx *desc;
3034 desc = find_pool_constant (cfun->varasm->pool, addr);
3035 *pmarked = (desc->mark != 0);
3036 return desc->constant;
3039 /* Likewise, but for the constant pool of a specific function. */
3042 get_pool_constant_for_function (struct function *f, rtx addr)
3044 return find_pool_constant (f->varasm->pool, addr)->constant;
3047 /* Similar, return the mode. */
3049 enum machine_mode
3050 get_pool_mode (rtx addr)
3052 return find_pool_constant (cfun->varasm->pool, addr)->mode;
3055 /* Return the size of the constant pool. */
3058 get_pool_size (void)
3060 return cfun->varasm->pool->offset;
3063 /* Worker function for output_constant_pool_1. Emit assembly for X
3064 in MODE with known alignment ALIGN. */
3066 static void
3067 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3069 switch (GET_MODE_CLASS (mode))
3071 case MODE_FLOAT:
3073 REAL_VALUE_TYPE r;
3075 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3076 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3077 assemble_real (r, mode, align);
3078 break;
3081 case MODE_INT:
3082 case MODE_PARTIAL_INT:
3083 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3084 break;
3086 case MODE_VECTOR_FLOAT:
3087 case MODE_VECTOR_INT:
3089 int i, units;
3090 enum machine_mode submode = GET_MODE_INNER (mode);
3091 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3093 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3094 units = CONST_VECTOR_NUNITS (x);
3096 for (i = 0; i < units; i++)
3098 rtx elt = CONST_VECTOR_ELT (x, i);
3099 output_constant_pool_2 (submode, elt, i ? subalign : align);
3102 break;
3104 default:
3105 gcc_unreachable ();
3109 /* Worker function for output_constant_pool. Emit POOL. */
3111 static void
3112 output_constant_pool_1 (struct constant_descriptor_rtx *desc)
3114 rtx x, tmp;
3116 if (!desc->mark)
3117 return;
3118 x = desc->constant;
3120 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3121 whose CODE_LABEL has been deleted. This can occur if a jump table
3122 is eliminated by optimization. If so, write a constant of zero
3123 instead. Note that this can also happen by turning the
3124 CODE_LABEL into a NOTE. */
3125 /* ??? This seems completely and utterly wrong. Certainly it's
3126 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3127 functioning even with INSN_DELETED_P and friends. */
3129 tmp = x;
3130 switch (GET_CODE (x))
3132 case CONST:
3133 if (GET_CODE (XEXP (x, 0)) != PLUS
3134 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3135 break;
3136 tmp = XEXP (XEXP (x, 0), 0);
3137 /* FALLTHRU */
3139 case LABEL_REF:
3140 tmp = XEXP (x, 0);
3141 gcc_assert (!INSN_DELETED_P (tmp));
3142 gcc_assert (!NOTE_P (tmp)
3143 || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3144 break;
3146 default:
3147 break;
3150 /* First switch to correct section. */
3151 switch_to_section (targetm.asm_out.select_rtx_section (desc->mode, x,
3152 desc->align));
3154 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3155 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3156 desc->align, desc->labelno, done);
3157 #endif
3159 assemble_align (desc->align);
3161 /* Output the label. */
3162 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3164 /* Output the data. */
3165 output_constant_pool_2 (desc->mode, x, desc->align);
3167 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3168 sections have proper size. */
3169 if (desc->align > GET_MODE_BITSIZE (desc->mode)
3170 && in_section
3171 && (in_section->common.flags & SECTION_MERGE))
3172 assemble_align (desc->align);
3174 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3175 done:
3176 #endif
3177 return;
3180 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3181 to as used. Emit referenced deferred strings. This function can
3182 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3184 static int
3185 mark_constant (rtx *current_rtx, void *data)
3187 struct rtx_constant_pool *pool = data;
3188 rtx x = *current_rtx;
3190 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3191 return 0;
3193 if (CONSTANT_POOL_ADDRESS_P (x))
3195 struct constant_descriptor_rtx *desc = find_pool_constant (pool, x);
3196 if (desc->mark == 0)
3198 desc->mark = 1;
3199 for_each_rtx (&desc->constant, mark_constant, pool);
3202 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3204 tree exp = SYMBOL_REF_DECL (x);
3205 if (!TREE_ASM_WRITTEN (exp))
3207 n_deferred_constants--;
3208 output_constant_def_contents (x);
3212 return -1;
3215 /* Look through appropriate parts of INSN, marking all entries in the
3216 constant pool which are actually being used. Entries that are only
3217 referenced by other constants are also marked as used. Emit
3218 deferred strings that are used. */
3220 static void
3221 mark_constants (struct rtx_constant_pool *pool, rtx insn)
3223 if (!INSN_P (insn))
3224 return;
3226 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3227 insns, not any notes that may be attached. We don't want to mark
3228 a constant just because it happens to appear in a REG_EQUIV note. */
3229 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3231 rtx seq = PATTERN (insn);
3232 int i, n = XVECLEN (seq, 0);
3233 for (i = 0; i < n; ++i)
3235 rtx subinsn = XVECEXP (seq, 0, i);
3236 if (INSN_P (subinsn))
3237 for_each_rtx (&PATTERN (subinsn), mark_constant, pool);
3240 else
3241 for_each_rtx (&PATTERN (insn), mark_constant, pool);
3244 /* Look through the instructions for this function, and mark all the
3245 entries in POOL which are actually being used. Emit deferred constants
3246 which have indeed been used. */
3248 static void
3249 mark_constant_pool (struct rtx_constant_pool *pool)
3251 rtx insn, link;
3253 if (pool->first == 0 && n_deferred_constants == 0)
3254 return;
3256 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3257 mark_constants (pool, insn);
3259 for (link = current_function_epilogue_delay_list;
3260 link;
3261 link = XEXP (link, 1))
3262 mark_constants (pool, XEXP (link, 0));
3265 /* Write all the constants in the constant pool. */
3267 void
3268 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3269 tree fndecl ATTRIBUTE_UNUSED)
3271 struct rtx_constant_pool *pool = cfun->varasm->pool;
3272 struct constant_descriptor_rtx *desc;
3274 /* It is possible for gcc to call force_const_mem and then to later
3275 discard the instructions which refer to the constant. In such a
3276 case we do not need to output the constant. */
3277 mark_constant_pool (pool);
3279 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3280 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3281 #endif
3283 for (desc = pool->first; desc ; desc = desc->next)
3284 output_constant_pool_1 (desc);
3286 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3287 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3288 #endif
3291 /* Determine what kind of relocations EXP may need. */
3294 compute_reloc_for_constant (tree exp)
3296 int reloc = 0, reloc2;
3297 tree tem;
3299 /* Give the front-end a chance to convert VALUE to something that
3300 looks more like a constant to the back-end. */
3301 exp = lang_hooks.expand_constant (exp);
3303 switch (TREE_CODE (exp))
3305 case ADDR_EXPR:
3306 case FDESC_EXPR:
3307 /* Go inside any operations that get_inner_reference can handle and see
3308 if what's inside is a constant: no need to do anything here for
3309 addresses of variables or functions. */
3310 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3311 tem = TREE_OPERAND (tem, 0))
3314 if (TREE_PUBLIC (tem))
3315 reloc |= 2;
3316 else
3317 reloc |= 1;
3318 break;
3320 case PLUS_EXPR:
3321 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3322 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3323 break;
3325 case MINUS_EXPR:
3326 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3327 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3328 /* The difference of two local labels is computable at link time. */
3329 if (reloc == 1 && reloc2 == 1)
3330 reloc = 0;
3331 else
3332 reloc |= reloc2;
3333 break;
3335 case NOP_EXPR:
3336 case CONVERT_EXPR:
3337 case NON_LVALUE_EXPR:
3338 case VIEW_CONVERT_EXPR:
3339 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3340 break;
3342 case CONSTRUCTOR:
3344 unsigned HOST_WIDE_INT idx;
3345 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3346 if (tem != 0)
3347 reloc |= compute_reloc_for_constant (tem);
3349 break;
3351 default:
3352 break;
3354 return reloc;
3357 /* Find all the constants whose addresses are referenced inside of EXP,
3358 and make sure assembler code with a label has been output for each one.
3359 Indicate whether an ADDR_EXPR has been encountered. */
3361 static void
3362 output_addressed_constants (tree exp)
3364 tree tem;
3366 /* Give the front-end a chance to convert VALUE to something that
3367 looks more like a constant to the back-end. */
3368 exp = lang_hooks.expand_constant (exp);
3370 switch (TREE_CODE (exp))
3372 case ADDR_EXPR:
3373 case FDESC_EXPR:
3374 /* Go inside any operations that get_inner_reference can handle and see
3375 if what's inside is a constant: no need to do anything here for
3376 addresses of variables or functions. */
3377 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3378 tem = TREE_OPERAND (tem, 0))
3381 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3382 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3383 tem = DECL_INITIAL (tem);
3385 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3386 output_constant_def (tem, 0);
3387 break;
3389 case PLUS_EXPR:
3390 case MINUS_EXPR:
3391 output_addressed_constants (TREE_OPERAND (exp, 1));
3392 /* Fall through. */
3394 case NOP_EXPR:
3395 case CONVERT_EXPR:
3396 case NON_LVALUE_EXPR:
3397 case VIEW_CONVERT_EXPR:
3398 output_addressed_constants (TREE_OPERAND (exp, 0));
3399 break;
3401 case CONSTRUCTOR:
3403 unsigned HOST_WIDE_INT idx;
3404 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3405 if (tem != 0)
3406 output_addressed_constants (tem);
3408 break;
3410 default:
3411 break;
3415 /* Return nonzero if VALUE is a valid constant-valued expression
3416 for use in initializing a static variable; one that can be an
3417 element of a "constant" initializer.
3419 Return null_pointer_node if the value is absolute;
3420 if it is relocatable, return the variable that determines the relocation.
3421 We assume that VALUE has been folded as much as possible;
3422 therefore, we do not need to check for such things as
3423 arithmetic-combinations of integers. */
3425 tree
3426 initializer_constant_valid_p (tree value, tree endtype)
3428 /* Give the front-end a chance to convert VALUE to something that
3429 looks more like a constant to the back-end. */
3430 value = lang_hooks.expand_constant (value);
3432 switch (TREE_CODE (value))
3434 case CONSTRUCTOR:
3435 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3436 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3437 && TREE_CONSTANT (value)
3438 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (value)))
3440 unsigned HOST_WIDE_INT idx;
3441 tree elt;
3442 bool absolute = true;
3444 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
3446 tree reloc;
3447 reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
3448 if (!reloc)
3449 return NULL_TREE;
3450 if (reloc != null_pointer_node)
3451 absolute = false;
3453 /* For a non-absolute relocation, there is no single
3454 variable that can be "the variable that determines the
3455 relocation." */
3456 return absolute ? null_pointer_node : error_mark_node;
3459 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3461 case INTEGER_CST:
3462 case VECTOR_CST:
3463 case REAL_CST:
3464 case STRING_CST:
3465 case COMPLEX_CST:
3466 return null_pointer_node;
3468 case ADDR_EXPR:
3469 case FDESC_EXPR:
3470 value = staticp (TREE_OPERAND (value, 0));
3471 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out to
3472 be a constant, this is old-skool offsetof-like nonsense. */
3473 if (value
3474 && TREE_CODE (value) == INDIRECT_REF
3475 && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3476 return null_pointer_node;
3477 /* Taking the address of a nested function involves a trampoline. */
3478 if (value
3479 && TREE_CODE (value) == FUNCTION_DECL
3480 && ((decl_function_context (value) && !DECL_NO_STATIC_CHAIN (value))
3481 || DECL_DLLIMPORT_P (value)))
3482 return NULL_TREE;
3483 return value;
3485 case VIEW_CONVERT_EXPR:
3486 case NON_LVALUE_EXPR:
3487 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3489 case CONVERT_EXPR:
3490 case NOP_EXPR:
3492 tree src;
3493 tree src_type;
3494 tree dest_type;
3496 src = TREE_OPERAND (value, 0);
3497 src_type = TREE_TYPE (src);
3498 dest_type = TREE_TYPE (value);
3500 /* Allow conversions between pointer types, floating-point
3501 types, and offset types. */
3502 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3503 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3504 || (TREE_CODE (dest_type) == OFFSET_TYPE
3505 && TREE_CODE (src_type) == OFFSET_TYPE))
3506 return initializer_constant_valid_p (src, endtype);
3508 /* Allow length-preserving conversions between integer types. */
3509 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3510 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3511 return initializer_constant_valid_p (src, endtype);
3513 /* Allow conversions between other integer types only if
3514 explicit value. */
3515 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3517 tree inner = initializer_constant_valid_p (src, endtype);
3518 if (inner == null_pointer_node)
3519 return null_pointer_node;
3520 break;
3523 /* Allow (int) &foo provided int is as wide as a pointer. */
3524 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3525 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3526 return initializer_constant_valid_p (src, endtype);
3528 /* Likewise conversions from int to pointers, but also allow
3529 conversions from 0. */
3530 if ((POINTER_TYPE_P (dest_type)
3531 || TREE_CODE (dest_type) == OFFSET_TYPE)
3532 && INTEGRAL_TYPE_P (src_type))
3534 if (integer_zerop (src))
3535 return null_pointer_node;
3536 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3537 return initializer_constant_valid_p (src, endtype);
3540 /* Allow conversions to struct or union types if the value
3541 inside is okay. */
3542 if (TREE_CODE (dest_type) == RECORD_TYPE
3543 || TREE_CODE (dest_type) == UNION_TYPE)
3544 return initializer_constant_valid_p (src, endtype);
3546 break;
3548 case PLUS_EXPR:
3549 if (! INTEGRAL_TYPE_P (endtype)
3550 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3552 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3553 endtype);
3554 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3555 endtype);
3556 /* If either term is absolute, use the other terms relocation. */
3557 if (valid0 == null_pointer_node)
3558 return valid1;
3559 if (valid1 == null_pointer_node)
3560 return valid0;
3562 break;
3564 case MINUS_EXPR:
3565 if (! INTEGRAL_TYPE_P (endtype)
3566 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3568 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3569 endtype);
3570 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3571 endtype);
3572 /* Win if second argument is absolute. */
3573 if (valid1 == null_pointer_node)
3574 return valid0;
3575 /* Win if both arguments have the same relocation.
3576 Then the value is absolute. */
3577 if (valid0 == valid1 && valid0 != 0)
3578 return null_pointer_node;
3580 /* Since GCC guarantees that string constants are unique in the
3581 generated code, a subtraction between two copies of the same
3582 constant string is absolute. */
3583 if (valid0 && TREE_CODE (valid0) == STRING_CST
3584 && valid1 && TREE_CODE (valid1) == STRING_CST
3585 && operand_equal_p (valid0, valid1, 1))
3586 return null_pointer_node;
3589 /* Support narrowing differences. */
3590 if (INTEGRAL_TYPE_P (endtype))
3592 tree op0, op1;
3594 op0 = TREE_OPERAND (value, 0);
3595 op1 = TREE_OPERAND (value, 1);
3597 /* Like STRIP_NOPS except allow the operand mode to widen.
3598 This works around a feature of fold that simplifies
3599 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3600 that the narrower operation is cheaper. */
3602 while (TREE_CODE (op0) == NOP_EXPR
3603 || TREE_CODE (op0) == CONVERT_EXPR
3604 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3606 tree inner = TREE_OPERAND (op0, 0);
3607 if (inner == error_mark_node
3608 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3609 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3610 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3611 break;
3612 op0 = inner;
3615 while (TREE_CODE (op1) == NOP_EXPR
3616 || TREE_CODE (op1) == CONVERT_EXPR
3617 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3619 tree inner = TREE_OPERAND (op1, 0);
3620 if (inner == error_mark_node
3621 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3622 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3623 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3624 break;
3625 op1 = inner;
3628 op0 = initializer_constant_valid_p (op0, endtype);
3629 op1 = initializer_constant_valid_p (op1, endtype);
3631 /* Both initializers must be known. */
3632 if (op0 && op1)
3634 if (op0 == op1)
3635 return null_pointer_node;
3637 /* Support differences between labels. */
3638 if (TREE_CODE (op0) == LABEL_DECL
3639 && TREE_CODE (op1) == LABEL_DECL)
3640 return null_pointer_node;
3642 if (TREE_CODE (op0) == STRING_CST
3643 && TREE_CODE (op1) == STRING_CST
3644 && operand_equal_p (op0, op1, 1))
3645 return null_pointer_node;
3648 break;
3650 default:
3651 break;
3654 return 0;
3657 /* Output assembler code for constant EXP to FILE, with no label.
3658 This includes the pseudo-op such as ".int" or ".byte", and a newline.
3659 Assumes output_addressed_constants has been done on EXP already.
3661 Generate exactly SIZE bytes of assembler data, padding at the end
3662 with zeros if necessary. SIZE must always be specified.
3664 SIZE is important for structure constructors,
3665 since trailing members may have been omitted from the constructor.
3666 It is also important for initialization of arrays from string constants
3667 since the full length of the string constant might not be wanted.
3668 It is also needed for initialization of unions, where the initializer's
3669 type is just one member, and that may not be as long as the union.
3671 There a case in which we would fail to output exactly SIZE bytes:
3672 for a structure constructor that wants to produce more than SIZE bytes.
3673 But such constructors will never be generated for any possible input.
3675 ALIGN is the alignment of the data in bits. */
3677 void
3678 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3680 enum tree_code code;
3681 unsigned HOST_WIDE_INT thissize;
3683 /* Some front-ends use constants other than the standard language-independent
3684 varieties, but which may still be output directly. Give the front-end a
3685 chance to convert EXP to a language-independent representation. */
3686 exp = lang_hooks.expand_constant (exp);
3688 if (size == 0 || flag_syntax_only)
3689 return;
3691 /* See if we're trying to initialize a pointer in a non-default mode
3692 to the address of some declaration somewhere. If the target says
3693 the mode is valid for pointers, assume the target has a way of
3694 resolving it. */
3695 if (TREE_CODE (exp) == NOP_EXPR
3696 && POINTER_TYPE_P (TREE_TYPE (exp))
3697 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3699 tree saved_type = TREE_TYPE (exp);
3701 /* Peel off any intermediate conversions-to-pointer for valid
3702 pointer modes. */
3703 while (TREE_CODE (exp) == NOP_EXPR
3704 && POINTER_TYPE_P (TREE_TYPE (exp))
3705 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3706 exp = TREE_OPERAND (exp, 0);
3708 /* If what we're left with is the address of something, we can
3709 convert the address to the final type and output it that
3710 way. */
3711 if (TREE_CODE (exp) == ADDR_EXPR)
3712 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
3715 /* Eliminate any conversions since we'll be outputting the underlying
3716 constant. */
3717 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
3718 || TREE_CODE (exp) == NON_LVALUE_EXPR
3719 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
3721 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
3722 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
3724 /* Make sure eliminating the conversion is really a no-op, except with
3725 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
3726 union types to allow for Ada unchecked unions. */
3727 if (type_size > op_size
3728 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
3729 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
3730 internal_error ("no-op convert from %wd to %wd bytes in initializer",
3731 op_size, type_size);
3733 exp = TREE_OPERAND (exp, 0);
3736 code = TREE_CODE (TREE_TYPE (exp));
3737 thissize = int_size_in_bytes (TREE_TYPE (exp));
3739 /* Allow a constructor with no elements for any data type.
3740 This means to fill the space with zeros. */
3741 if (TREE_CODE (exp) == CONSTRUCTOR
3742 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
3744 assemble_zeros (size);
3745 return;
3748 if (TREE_CODE (exp) == FDESC_EXPR)
3750 #ifdef ASM_OUTPUT_FDESC
3751 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
3752 tree decl = TREE_OPERAND (exp, 0);
3753 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
3754 #else
3755 gcc_unreachable ();
3756 #endif
3757 return;
3760 /* Now output the underlying data. If we've handling the padding, return.
3761 Otherwise, break and ensure SIZE is the size written. */
3762 switch (code)
3764 case CHAR_TYPE:
3765 case BOOLEAN_TYPE:
3766 case INTEGER_TYPE:
3767 case ENUMERAL_TYPE:
3768 case POINTER_TYPE:
3769 case REFERENCE_TYPE:
3770 case OFFSET_TYPE:
3771 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3772 EXPAND_INITIALIZER),
3773 MIN (size, thissize), align, 0))
3774 error ("initializer for integer value is too complicated");
3775 break;
3777 case REAL_TYPE:
3778 if (TREE_CODE (exp) != REAL_CST)
3779 error ("initializer for floating value is not a floating constant");
3781 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3782 break;
3784 case COMPLEX_TYPE:
3785 output_constant (TREE_REALPART (exp), thissize / 2, align);
3786 output_constant (TREE_IMAGPART (exp), thissize / 2,
3787 min_align (align, BITS_PER_UNIT * (thissize / 2)));
3788 break;
3790 case ARRAY_TYPE:
3791 case VECTOR_TYPE:
3792 switch (TREE_CODE (exp))
3794 case CONSTRUCTOR:
3795 output_constructor (exp, size, align);
3796 return;
3797 case STRING_CST:
3798 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3799 size);
3800 assemble_string (TREE_STRING_POINTER (exp), thissize);
3801 break;
3803 case VECTOR_CST:
3805 int elt_size;
3806 tree link;
3807 unsigned int nalign;
3808 enum machine_mode inner;
3810 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
3811 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3813 elt_size = GET_MODE_SIZE (inner);
3815 link = TREE_VECTOR_CST_ELTS (exp);
3816 output_constant (TREE_VALUE (link), elt_size, align);
3817 while ((link = TREE_CHAIN (link)) != NULL)
3818 output_constant (TREE_VALUE (link), elt_size, nalign);
3819 break;
3821 default:
3822 gcc_unreachable ();
3824 break;
3826 case RECORD_TYPE:
3827 case UNION_TYPE:
3828 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
3829 output_constructor (exp, size, align);
3830 return;
3832 case ERROR_MARK:
3833 return;
3835 default:
3836 gcc_unreachable ();
3839 if (size > thissize)
3840 assemble_zeros (size - thissize);
3844 /* Subroutine of output_constructor, used for computing the size of
3845 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
3846 type with an unspecified upper bound. */
3848 static unsigned HOST_WIDE_INT
3849 array_size_for_constructor (tree val)
3851 tree max_index, i;
3852 unsigned HOST_WIDE_INT cnt;
3853 tree index, value;
3855 /* This code used to attempt to handle string constants that are not
3856 arrays of single-bytes, but nothing else does, so there's no point in
3857 doing it here. */
3858 if (TREE_CODE (val) == STRING_CST)
3859 return TREE_STRING_LENGTH (val);
3861 max_index = NULL_TREE;
3862 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
3864 if (TREE_CODE (index) == RANGE_EXPR)
3865 index = TREE_OPERAND (index, 1);
3866 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
3867 max_index = index;
3870 if (max_index == NULL_TREE)
3871 return 0;
3873 /* Compute the total number of array elements. */
3874 i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
3875 convert (sizetype,
3876 TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
3877 i = size_binop (PLUS_EXPR, i, convert (sizetype, integer_one_node));
3879 /* Multiply by the array element unit size to find number of bytes. */
3880 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
3882 return tree_low_cst (i, 1);
3885 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
3886 Generate at least SIZE bytes, padding if necessary. */
3888 static void
3889 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
3890 unsigned int align)
3892 tree type = TREE_TYPE (exp);
3893 tree field = 0;
3894 tree min_index = 0;
3895 /* Number of bytes output or skipped so far.
3896 In other words, current position within the constructor. */
3897 HOST_WIDE_INT total_bytes = 0;
3898 /* Nonzero means BYTE contains part of a byte, to be output. */
3899 int byte_buffer_in_use = 0;
3900 int byte = 0;
3901 unsigned HOST_WIDE_INT cnt;
3902 constructor_elt *ce;
3904 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
3906 if (TREE_CODE (type) == RECORD_TYPE)
3907 field = TYPE_FIELDS (type);
3909 if (TREE_CODE (type) == ARRAY_TYPE
3910 && TYPE_DOMAIN (type) != 0)
3911 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3913 /* As LINK goes through the elements of the constant,
3914 FIELD goes through the structure fields, if the constant is a structure.
3915 if the constant is a union, then we override this,
3916 by getting the field from the TREE_LIST element.
3917 But the constant could also be an array. Then FIELD is zero.
3919 There is always a maximum of one element in the chain LINK for unions
3920 (even if the initializer in a source program incorrectly contains
3921 more one). */
3922 for (cnt = 0;
3923 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
3924 cnt++, field = field ? TREE_CHAIN (field) : 0)
3926 tree val = ce->value;
3927 tree index = 0;
3929 /* The element in a union constructor specifies the proper field
3930 or index. */
3931 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
3932 || TREE_CODE (type) == QUAL_UNION_TYPE)
3933 && ce->index != 0)
3934 field = ce->index;
3936 else if (TREE_CODE (type) == ARRAY_TYPE)
3937 index = ce->index;
3939 #ifdef ASM_COMMENT_START
3940 if (field && flag_verbose_asm)
3941 fprintf (asm_out_file, "%s %s:\n",
3942 ASM_COMMENT_START,
3943 DECL_NAME (field)
3944 ? IDENTIFIER_POINTER (DECL_NAME (field))
3945 : "<anonymous>");
3946 #endif
3948 /* Eliminate the marker that makes a cast not be an lvalue. */
3949 if (val != 0)
3950 STRIP_NOPS (val);
3952 if (index && TREE_CODE (index) == RANGE_EXPR)
3954 unsigned HOST_WIDE_INT fieldsize
3955 = int_size_in_bytes (TREE_TYPE (type));
3956 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
3957 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
3958 HOST_WIDE_INT index;
3959 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
3961 for (index = lo_index; index <= hi_index; index++)
3963 /* Output the element's initial value. */
3964 if (val == 0)
3965 assemble_zeros (fieldsize);
3966 else
3967 output_constant (val, fieldsize, align2);
3969 /* Count its size. */
3970 total_bytes += fieldsize;
3973 else if (field == 0 || !DECL_BIT_FIELD (field))
3975 /* An element that is not a bit-field. */
3977 unsigned HOST_WIDE_INT fieldsize;
3978 /* Since this structure is static,
3979 we know the positions are constant. */
3980 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
3981 unsigned int align2;
3983 if (index != 0)
3984 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
3985 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
3987 /* Output any buffered-up bit-fields preceding this element. */
3988 if (byte_buffer_in_use)
3990 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3991 total_bytes++;
3992 byte_buffer_in_use = 0;
3995 /* Advance to offset of this element.
3996 Note no alignment needed in an array, since that is guaranteed
3997 if each element has the proper size. */
3998 if ((field != 0 || index != 0) && pos != total_bytes)
4000 gcc_assert (pos >= total_bytes);
4001 assemble_zeros (pos - total_bytes);
4002 total_bytes = pos;
4005 /* Find the alignment of this element. */
4006 align2 = min_align (align, BITS_PER_UNIT * pos);
4008 /* Determine size this element should occupy. */
4009 if (field)
4011 fieldsize = 0;
4013 /* If this is an array with an unspecified upper bound,
4014 the initializer determines the size. */
4015 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4016 but we cannot do this until the deprecated support for
4017 initializing zero-length array members is removed. */
4018 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4019 && TYPE_DOMAIN (TREE_TYPE (field))
4020 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4022 fieldsize = array_size_for_constructor (val);
4023 /* Given a non-empty initialization, this field had
4024 better be last. */
4025 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4027 else if (DECL_SIZE_UNIT (field))
4029 /* ??? This can't be right. If the decl size overflows
4030 a host integer we will silently emit no data. */
4031 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4032 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4035 else
4036 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4038 /* Output the element's initial value. */
4039 if (val == 0)
4040 assemble_zeros (fieldsize);
4041 else
4042 output_constant (val, fieldsize, align2);
4044 /* Count its size. */
4045 total_bytes += fieldsize;
4047 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4048 error ("invalid initial value for member %qs",
4049 IDENTIFIER_POINTER (DECL_NAME (field)));
4050 else
4052 /* Element that is a bit-field. */
4054 HOST_WIDE_INT next_offset = int_bit_position (field);
4055 HOST_WIDE_INT end_offset
4056 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4058 if (val == 0)
4059 val = integer_zero_node;
4061 /* If this field does not start in this (or, next) byte,
4062 skip some bytes. */
4063 if (next_offset / BITS_PER_UNIT != total_bytes)
4065 /* Output remnant of any bit field in previous bytes. */
4066 if (byte_buffer_in_use)
4068 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4069 total_bytes++;
4070 byte_buffer_in_use = 0;
4073 /* If still not at proper byte, advance to there. */
4074 if (next_offset / BITS_PER_UNIT != total_bytes)
4076 gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
4077 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4078 total_bytes = next_offset / BITS_PER_UNIT;
4082 if (! byte_buffer_in_use)
4083 byte = 0;
4085 /* We must split the element into pieces that fall within
4086 separate bytes, and combine each byte with previous or
4087 following bit-fields. */
4089 /* next_offset is the offset n fbits from the beginning of
4090 the structure to the next bit of this element to be processed.
4091 end_offset is the offset of the first bit past the end of
4092 this element. */
4093 while (next_offset < end_offset)
4095 int this_time;
4096 int shift;
4097 HOST_WIDE_INT value;
4098 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4099 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4101 /* Advance from byte to byte
4102 within this element when necessary. */
4103 while (next_byte != total_bytes)
4105 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4106 total_bytes++;
4107 byte = 0;
4110 /* Number of bits we can process at once
4111 (all part of the same byte). */
4112 this_time = MIN (end_offset - next_offset,
4113 BITS_PER_UNIT - next_bit);
4114 if (BYTES_BIG_ENDIAN)
4116 /* On big-endian machine, take the most significant bits
4117 first (of the bits that are significant)
4118 and put them into bytes from the most significant end. */
4119 shift = end_offset - next_offset - this_time;
4121 /* Don't try to take a bunch of bits that cross
4122 the word boundary in the INTEGER_CST. We can
4123 only select bits from the LOW or HIGH part
4124 not from both. */
4125 if (shift < HOST_BITS_PER_WIDE_INT
4126 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4128 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4129 shift = HOST_BITS_PER_WIDE_INT;
4132 /* Now get the bits from the appropriate constant word. */
4133 if (shift < HOST_BITS_PER_WIDE_INT)
4134 value = TREE_INT_CST_LOW (val);
4135 else
4137 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4138 value = TREE_INT_CST_HIGH (val);
4139 shift -= HOST_BITS_PER_WIDE_INT;
4142 /* Get the result. This works only when:
4143 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4144 byte |= (((value >> shift)
4145 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4146 << (BITS_PER_UNIT - this_time - next_bit));
4148 else
4150 /* On little-endian machines,
4151 take first the least significant bits of the value
4152 and pack them starting at the least significant
4153 bits of the bytes. */
4154 shift = next_offset - int_bit_position (field);
4156 /* Don't try to take a bunch of bits that cross
4157 the word boundary in the INTEGER_CST. We can
4158 only select bits from the LOW or HIGH part
4159 not from both. */
4160 if (shift < HOST_BITS_PER_WIDE_INT
4161 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4162 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4164 /* Now get the bits from the appropriate constant word. */
4165 if (shift < HOST_BITS_PER_WIDE_INT)
4166 value = TREE_INT_CST_LOW (val);
4167 else
4169 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4170 value = TREE_INT_CST_HIGH (val);
4171 shift -= HOST_BITS_PER_WIDE_INT;
4174 /* Get the result. This works only when:
4175 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4176 byte |= (((value >> shift)
4177 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4178 << next_bit);
4181 next_offset += this_time;
4182 byte_buffer_in_use = 1;
4187 if (byte_buffer_in_use)
4189 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4190 total_bytes++;
4193 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4194 assemble_zeros (size - total_bytes);
4197 /* This TREE_LIST contains any weak symbol declarations waiting
4198 to be emitted. */
4199 static GTY(()) tree weak_decls;
4201 /* Mark DECL as weak. */
4203 static void
4204 mark_weak (tree decl)
4206 DECL_WEAK (decl) = 1;
4208 if (DECL_RTL_SET_P (decl)
4209 && MEM_P (DECL_RTL (decl))
4210 && XEXP (DECL_RTL (decl), 0)
4211 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4212 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4215 /* Merge weak status between NEWDECL and OLDDECL. */
4217 void
4218 merge_weak (tree newdecl, tree olddecl)
4220 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4222 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4224 tree *pwd;
4225 /* We put the NEWDECL on the weak_decls list at some point
4226 and OLDDECL as well. Keep just OLDDECL on the list. */
4227 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4228 if (TREE_VALUE (*pwd) == newdecl)
4230 *pwd = TREE_CHAIN (*pwd);
4231 break;
4234 return;
4237 if (DECL_WEAK (newdecl))
4239 tree wd;
4241 /* NEWDECL is weak, but OLDDECL is not. */
4243 /* If we already output the OLDDECL, we're in trouble; we can't
4244 go back and make it weak. This error cannot caught in
4245 declare_weak because the NEWDECL and OLDDECL was not yet
4246 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4247 if (TREE_ASM_WRITTEN (olddecl))
4248 error ("weak declaration of %q+D must precede definition",
4249 newdecl);
4251 /* If we've already generated rtl referencing OLDDECL, we may
4252 have done so in a way that will not function properly with
4253 a weak symbol. */
4254 else if (TREE_USED (olddecl)
4255 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4256 warning (0, "weak declaration of %q+D after first use results "
4257 "in unspecified behavior", newdecl);
4259 if (SUPPORTS_WEAK)
4261 /* We put the NEWDECL on the weak_decls list at some point.
4262 Replace it with the OLDDECL. */
4263 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4264 if (TREE_VALUE (wd) == newdecl)
4266 TREE_VALUE (wd) = olddecl;
4267 break;
4269 /* We may not find the entry on the list. If NEWDECL is a
4270 weak alias, then we will have already called
4271 globalize_decl to remove the entry; in that case, we do
4272 not need to do anything. */
4275 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4276 mark_weak (olddecl);
4278 else
4279 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4280 weak. Just update NEWDECL to indicate that it's weak too. */
4281 mark_weak (newdecl);
4284 /* Declare DECL to be a weak symbol. */
4286 void
4287 declare_weak (tree decl)
4289 if (! TREE_PUBLIC (decl))
4290 error ("weak declaration of %q+D must be public", decl);
4291 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4292 error ("weak declaration of %q+D must precede definition", decl);
4293 else if (SUPPORTS_WEAK)
4295 if (! DECL_WEAK (decl))
4296 weak_decls = tree_cons (NULL, decl, weak_decls);
4298 else
4299 warning (0, "weak declaration of %q+D not supported", decl);
4301 mark_weak (decl);
4304 static void
4305 weak_finish_1 (tree decl)
4307 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4308 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4309 #endif
4311 if (! TREE_USED (decl))
4312 return;
4314 #ifdef ASM_WEAKEN_DECL
4315 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4316 #else
4317 #ifdef ASM_WEAKEN_LABEL
4318 ASM_WEAKEN_LABEL (asm_out_file, name);
4319 #else
4320 #ifdef ASM_OUTPUT_WEAK_ALIAS
4322 static bool warn_once = 0;
4323 if (! warn_once)
4325 warning (0, "only weak aliases are supported in this configuration");
4326 warn_once = 1;
4328 return;
4330 #endif
4331 #endif
4332 #endif
4335 /* This TREE_LIST contains weakref targets. */
4337 static GTY(()) tree weakref_targets;
4339 /* Forward declaration. */
4340 static tree find_decl_and_mark_needed (tree decl, tree target);
4342 /* Emit any pending weak declarations. */
4344 void
4345 weak_finish (void)
4347 tree t;
4349 for (t = weakref_targets; t; t = TREE_CHAIN (t))
4351 tree alias_decl = TREE_PURPOSE (t);
4352 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
4354 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
4355 /* Remove alias_decl from the weak list, but leave entries for
4356 the target alone. */
4357 target = NULL_TREE;
4358 #ifndef ASM_OUTPUT_WEAKREF
4359 else if (! TREE_SYMBOL_REFERENCED (target))
4361 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
4362 defined, otherwise we and weak_finish_1 would use a
4363 different macros. */
4364 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
4365 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
4366 # else
4367 tree decl = find_decl_and_mark_needed (alias_decl, target);
4369 if (! decl)
4371 decl = build_decl (TREE_CODE (alias_decl), target,
4372 TREE_TYPE (alias_decl));
4374 DECL_EXTERNAL (decl) = 1;
4375 TREE_PUBLIC (decl) = 1;
4376 DECL_ARTIFICIAL (decl) = 1;
4377 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
4378 TREE_USED (decl) = 1;
4381 weak_finish_1 (decl);
4382 # endif
4384 #endif
4387 tree *p;
4388 tree t2;
4390 /* Remove the alias and the target from the pending weak list
4391 so that we do not emit any .weak directives for the former,
4392 nor multiple .weak directives for the latter. */
4393 for (p = &weak_decls; (t2 = *p) ; )
4395 if (TREE_VALUE (t2) == alias_decl
4396 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
4397 *p = TREE_CHAIN (t2);
4398 else
4399 p = &TREE_CHAIN (t2);
4402 /* Remove other weakrefs to the same target, to speed things up. */
4403 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
4405 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
4406 *p = TREE_CHAIN (t2);
4407 else
4408 p = &TREE_CHAIN (t2);
4413 for (t = weak_decls; t; t = TREE_CHAIN (t))
4415 tree decl = TREE_VALUE (t);
4417 weak_finish_1 (decl);
4421 /* Emit the assembly bits to indicate that DECL is globally visible. */
4423 static void
4424 globalize_decl (tree decl)
4426 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4428 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4429 if (DECL_WEAK (decl))
4431 tree *p, t;
4433 #ifdef ASM_WEAKEN_DECL
4434 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4435 #else
4436 ASM_WEAKEN_LABEL (asm_out_file, name);
4437 #endif
4439 /* Remove this function from the pending weak list so that
4440 we do not emit multiple .weak directives for it. */
4441 for (p = &weak_decls; (t = *p) ; )
4443 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4444 *p = TREE_CHAIN (t);
4445 else
4446 p = &TREE_CHAIN (t);
4449 /* Remove weakrefs to the same target from the pending weakref
4450 list, for the same reason. */
4451 for (p = &weakref_targets; (t = *p) ; )
4453 if (DECL_ASSEMBLER_NAME (decl)
4454 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4455 *p = TREE_CHAIN (t);
4456 else
4457 p = &TREE_CHAIN (t);
4460 return;
4462 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4463 if (DECL_ONE_ONLY (decl))
4464 ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4465 #endif
4467 targetm.asm_out.globalize_label (asm_out_file, name);
4470 /* We have to be able to tell cgraph about the needed-ness of the target
4471 of an alias. This requires that the decl have been defined. Aliases
4472 that precede their definition have to be queued for later processing. */
4474 typedef struct alias_pair GTY(())
4476 tree decl;
4477 tree target;
4478 } alias_pair;
4480 /* Define gc'd vector type. */
4481 DEF_VEC_O(alias_pair);
4482 DEF_VEC_ALLOC_O(alias_pair,gc);
4484 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
4486 /* Given an assembly name, find the decl it is associated with. At the
4487 same time, mark it needed for cgraph. */
4489 static tree
4490 find_decl_and_mark_needed (tree decl, tree target)
4492 struct cgraph_node *fnode = NULL;
4493 struct cgraph_varpool_node *vnode = NULL;
4495 if (TREE_CODE (decl) == FUNCTION_DECL)
4497 fnode = cgraph_node_for_asm (target);
4498 if (fnode == NULL)
4499 vnode = cgraph_varpool_node_for_asm (target);
4501 else
4503 vnode = cgraph_varpool_node_for_asm (target);
4504 if (vnode == NULL)
4505 fnode = cgraph_node_for_asm (target);
4508 if (fnode)
4510 /* We can't mark function nodes as used after cgraph global info
4511 is finished. This wouldn't generally be necessary, but C++
4512 virtual table thunks are introduced late in the game and
4513 might seem like they need marking, although in fact they
4514 don't. */
4515 if (! cgraph_global_info_ready)
4516 cgraph_mark_needed_node (fnode);
4517 return fnode->decl;
4519 else if (vnode)
4521 cgraph_varpool_mark_needed_node (vnode);
4522 return vnode->decl;
4524 else
4525 return NULL_TREE;
4528 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4529 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
4530 tree node is DECL to have the value of the tree node TARGET. */
4532 static void
4533 do_assemble_alias (tree decl, tree target)
4535 if (TREE_ASM_WRITTEN (decl))
4536 return;
4538 TREE_ASM_WRITTEN (decl) = 1;
4539 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4541 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4543 ultimate_transparent_alias_target (&target);
4545 if (!TREE_SYMBOL_REFERENCED (target))
4546 weakref_targets = tree_cons (decl, target, weakref_targets);
4548 #ifdef ASM_OUTPUT_WEAKREF
4549 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4550 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4551 IDENTIFIER_POINTER (target));
4552 #else
4553 if (!SUPPORTS_WEAK)
4555 error ("%Jweakref is not supported in this configuration", decl);
4556 return;
4558 #endif
4559 return;
4562 #ifdef ASM_OUTPUT_DEF
4563 /* Make name accessible from other files, if appropriate. */
4565 if (TREE_PUBLIC (decl))
4567 globalize_decl (decl);
4568 maybe_assemble_visibility (decl);
4571 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4572 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4573 # else
4574 ASM_OUTPUT_DEF (asm_out_file,
4575 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4576 IDENTIFIER_POINTER (target));
4577 # endif
4578 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4580 const char *name;
4581 tree *p, t;
4583 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4584 # ifdef ASM_WEAKEN_DECL
4585 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4586 # else
4587 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4588 # endif
4589 /* Remove this function from the pending weak list so that
4590 we do not emit multiple .weak directives for it. */
4591 for (p = &weak_decls; (t = *p) ; )
4592 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4593 *p = TREE_CHAIN (t);
4594 else
4595 p = &TREE_CHAIN (t);
4597 /* Remove weakrefs to the same target from the pending weakref
4598 list, for the same reason. */
4599 for (p = &weakref_targets; (t = *p) ; )
4601 if (DECL_ASSEMBLER_NAME (decl)
4602 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4603 *p = TREE_CHAIN (t);
4604 else
4605 p = &TREE_CHAIN (t);
4608 #endif
4611 /* First pass of completing pending aliases. Make sure that cgraph knows
4612 which symbols will be required. */
4614 void
4615 finish_aliases_1 (void)
4617 unsigned i;
4618 alias_pair *p;
4620 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4622 tree target_decl;
4624 target_decl = find_decl_and_mark_needed (p->decl, p->target);
4625 if (target_decl == NULL)
4627 if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4628 error ("%q+D aliased to undefined symbol %qs",
4629 p->decl, IDENTIFIER_POINTER (p->target));
4631 else if (DECL_EXTERNAL (target_decl)
4632 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4633 error ("%q+D aliased to external symbol %qs",
4634 p->decl, IDENTIFIER_POINTER (p->target));
4638 /* Second pass of completing pending aliases. Emit the actual assembly.
4639 This happens at the end of compilation and thus it is assured that the
4640 target symbol has been emitted. */
4642 void
4643 finish_aliases_2 (void)
4645 unsigned i;
4646 alias_pair *p;
4648 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4649 do_assemble_alias (p->decl, p->target);
4651 VEC_truncate (alias_pair, alias_pairs, 0);
4654 /* Emit an assembler directive to make the symbol for DECL an alias to
4655 the symbol for TARGET. */
4657 void
4658 assemble_alias (tree decl, tree target)
4660 tree target_decl;
4661 bool is_weakref = false;
4663 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4665 tree alias = DECL_ASSEMBLER_NAME (decl);
4667 is_weakref = true;
4669 ultimate_transparent_alias_target (&target);
4671 if (alias == target)
4672 error ("weakref %q+D ultimately targets itself", decl);
4673 else
4675 #ifndef ASM_OUTPUT_WEAKREF
4676 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
4677 TREE_CHAIN (alias) = target;
4678 #endif
4680 if (TREE_PUBLIC (decl))
4681 error ("weakref %q+D must have static linkage", decl);
4683 else
4685 #if !defined (ASM_OUTPUT_DEF)
4686 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4687 error ("%Jalias definitions not supported in this configuration", decl);
4688 return;
4689 # else
4690 if (!DECL_WEAK (decl))
4692 error ("%Jonly weak aliases are supported in this configuration", decl);
4693 return;
4695 # endif
4696 #endif
4699 /* We must force creation of DECL_RTL for debug info generation, even though
4700 we don't use it here. */
4701 make_decl_rtl (decl);
4702 TREE_USED (decl) = 1;
4704 /* A quirk of the initial implementation of aliases required that the user
4705 add "extern" to all of them. Which is silly, but now historical. Do
4706 note that the symbol is in fact locally defined. */
4707 if (! is_weakref)
4708 DECL_EXTERNAL (decl) = 0;
4710 /* Allow aliases to aliases. */
4711 if (TREE_CODE (decl) == FUNCTION_DECL)
4712 cgraph_node (decl)->alias = true;
4713 else
4714 cgraph_varpool_node (decl)->alias = true;
4716 /* If the target has already been emitted, we don't have to queue the
4717 alias. This saves a tad o memory. */
4718 target_decl = find_decl_and_mark_needed (decl, target);
4719 if (target_decl && TREE_ASM_WRITTEN (target_decl))
4720 do_assemble_alias (decl, target);
4721 else
4723 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
4724 p->decl = decl;
4725 p->target = target;
4729 /* Emit an assembler directive to set symbol for DECL visibility to
4730 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
4732 void
4733 default_assemble_visibility (tree decl, int vis)
4735 static const char * const visibility_types[] = {
4736 NULL, "internal", "hidden", "protected"
4739 const char *name, *type;
4741 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4742 type = visibility_types[vis];
4744 #ifdef HAVE_GAS_HIDDEN
4745 fprintf (asm_out_file, "\t.%s\t", type);
4746 assemble_name (asm_out_file, name);
4747 fprintf (asm_out_file, "\n");
4748 #else
4749 warning (OPT_Wattributes, "visibility attribute not supported "
4750 "in this configuration; ignored");
4751 #endif
4754 /* A helper function to call assemble_visibility when needed for a decl. */
4756 static void
4757 maybe_assemble_visibility (tree decl)
4759 enum symbol_visibility vis = DECL_VISIBILITY (decl);
4761 if (vis != VISIBILITY_DEFAULT)
4762 targetm.asm_out.visibility (decl, vis);
4765 /* Returns 1 if the target configuration supports defining public symbols
4766 so that one of them will be chosen at link time instead of generating a
4767 multiply-defined symbol error, whether through the use of weak symbols or
4768 a target-specific mechanism for having duplicates discarded. */
4771 supports_one_only (void)
4773 if (SUPPORTS_ONE_ONLY)
4774 return 1;
4775 return SUPPORTS_WEAK;
4778 /* Set up DECL as a public symbol that can be defined in multiple
4779 translation units without generating a linker error. */
4781 void
4782 make_decl_one_only (tree decl)
4784 gcc_assert (TREE_CODE (decl) == VAR_DECL
4785 || TREE_CODE (decl) == FUNCTION_DECL);
4787 TREE_PUBLIC (decl) = 1;
4789 if (SUPPORTS_ONE_ONLY)
4791 #ifdef MAKE_DECL_ONE_ONLY
4792 MAKE_DECL_ONE_ONLY (decl);
4793 #endif
4794 DECL_ONE_ONLY (decl) = 1;
4796 else if (TREE_CODE (decl) == VAR_DECL
4797 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4798 DECL_COMMON (decl) = 1;
4799 else
4801 gcc_assert (SUPPORTS_WEAK);
4802 DECL_WEAK (decl) = 1;
4806 void
4807 init_varasm_once (void)
4809 section_htab = htab_create_ggc (31, section_entry_hash,
4810 section_entry_eq, NULL);
4811 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4812 const_desc_eq, NULL);
4814 const_alias_set = new_alias_set ();
4816 #ifdef TEXT_SECTION_ASM_OP
4817 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
4818 TEXT_SECTION_ASM_OP);
4819 #endif
4821 #ifdef DATA_SECTION_ASM_OP
4822 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
4823 DATA_SECTION_ASM_OP);
4824 #endif
4826 #ifdef SDATA_SECTION_ASM_OP
4827 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
4828 SDATA_SECTION_ASM_OP);
4829 #endif
4831 #ifdef READONLY_DATA_SECTION_ASM_OP
4832 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
4833 READONLY_DATA_SECTION_ASM_OP);
4834 #endif
4836 #ifdef CTORS_SECTION_ASM_OP
4837 ctors_section = get_unnamed_section (0, output_section_asm_op,
4838 CTORS_SECTION_ASM_OP);
4839 #endif
4841 #ifdef DTORS_SECTION_ASM_OP
4842 dtors_section = get_unnamed_section (0, output_section_asm_op,
4843 DTORS_SECTION_ASM_OP);
4844 #endif
4846 #ifdef BSS_SECTION_ASM_OP
4847 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
4848 output_section_asm_op,
4849 BSS_SECTION_ASM_OP);
4850 #endif
4852 #ifdef SBSS_SECTION_ASM_OP
4853 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
4854 output_section_asm_op,
4855 SBSS_SECTION_ASM_OP);
4856 #endif
4858 targetm.asm_out.init_sections ();
4860 if (readonly_data_section == NULL)
4861 readonly_data_section = text_section;
4864 enum tls_model
4865 decl_default_tls_model (tree decl)
4867 enum tls_model kind;
4868 bool is_local;
4870 is_local = targetm.binds_local_p (decl);
4871 if (!flag_shlib)
4873 if (is_local)
4874 kind = TLS_MODEL_LOCAL_EXEC;
4875 else
4876 kind = TLS_MODEL_INITIAL_EXEC;
4879 /* Local dynamic is inefficient when we're not combining the
4880 parts of the address. */
4881 else if (optimize && is_local)
4882 kind = TLS_MODEL_LOCAL_DYNAMIC;
4883 else
4884 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4885 if (kind < flag_tls_default)
4886 kind = flag_tls_default;
4888 return kind;
4891 /* Select a set of attributes for section NAME based on the properties
4892 of DECL and whether or not RELOC indicates that DECL's initializer
4893 might contain runtime relocations.
4895 We make the section read-only and executable for a function decl,
4896 read-only for a const data decl, and writable for a non-const data decl. */
4898 unsigned int
4899 default_section_type_flags (tree decl, const char *name, int reloc)
4901 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4904 unsigned int
4905 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4906 int shlib)
4908 unsigned int flags;
4910 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4911 flags = SECTION_CODE;
4912 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4913 flags = 0;
4914 else if (current_function_decl
4915 && cfun
4916 && cfun->unlikely_text_section_name
4917 && strcmp (name, cfun->unlikely_text_section_name) == 0)
4918 flags = SECTION_CODE;
4919 else if (!decl
4920 && (!current_function_decl || !cfun)
4921 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
4922 flags = SECTION_CODE;
4923 else
4924 flags = SECTION_WRITE;
4926 if (decl && DECL_ONE_ONLY (decl))
4927 flags |= SECTION_LINKONCE;
4929 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
4930 flags |= SECTION_TLS | SECTION_WRITE;
4932 if (strcmp (name, ".bss") == 0
4933 || strncmp (name, ".bss.", 5) == 0
4934 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4935 || strcmp (name, ".sbss") == 0
4936 || strncmp (name, ".sbss.", 6) == 0
4937 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
4938 flags |= SECTION_BSS;
4940 if (strcmp (name, ".tdata") == 0
4941 || strncmp (name, ".tdata.", 7) == 0
4942 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
4943 flags |= SECTION_TLS;
4945 if (strcmp (name, ".tbss") == 0
4946 || strncmp (name, ".tbss.", 6) == 0
4947 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4948 flags |= SECTION_TLS | SECTION_BSS;
4950 /* These three sections have special ELF types. They are neither
4951 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4952 want to print a section type (@progbits or @nobits). If someone
4953 is silly enough to emit code or TLS variables to one of these
4954 sections, then don't handle them specially. */
4955 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4956 && (strcmp (name, ".init_array") == 0
4957 || strcmp (name, ".fini_array") == 0
4958 || strcmp (name, ".preinit_array") == 0))
4959 flags |= SECTION_NOTYPE;
4961 return flags;
4964 /* Output assembly to switch to section NAME with attribute FLAGS.
4965 Four variants for common object file formats. */
4967 void
4968 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4969 unsigned int flags ATTRIBUTE_UNUSED,
4970 tree decl ATTRIBUTE_UNUSED)
4972 /* Some object formats don't support named sections at all. The
4973 front-end should already have flagged this as an error. */
4974 gcc_unreachable ();
4977 void
4978 default_elf_asm_named_section (const char *name, unsigned int flags,
4979 tree decl ATTRIBUTE_UNUSED)
4981 char flagchars[10], *f = flagchars;
4983 /* If we have already declared this section, we can use an
4984 abbreviated form to switch back to it -- unless this section is
4985 part of a COMDAT groups, in which case GAS requires the full
4986 declaration every time. */
4987 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4988 && (flags & SECTION_DECLARED))
4990 fprintf (asm_out_file, "\t.section\t%s\n", name);
4991 return;
4994 if (!(flags & SECTION_DEBUG))
4995 *f++ = 'a';
4996 if (flags & SECTION_WRITE)
4997 *f++ = 'w';
4998 if (flags & SECTION_CODE)
4999 *f++ = 'x';
5000 if (flags & SECTION_SMALL)
5001 *f++ = 's';
5002 if (flags & SECTION_MERGE)
5003 *f++ = 'M';
5004 if (flags & SECTION_STRINGS)
5005 *f++ = 'S';
5006 if (flags & SECTION_TLS)
5007 *f++ = 'T';
5008 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5009 *f++ = 'G';
5010 *f = '\0';
5012 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5014 if (!(flags & SECTION_NOTYPE))
5016 const char *type;
5017 const char *format;
5019 if (flags & SECTION_BSS)
5020 type = "nobits";
5021 else
5022 type = "progbits";
5024 format = ",@%s";
5025 #ifdef ASM_COMMENT_START
5026 /* On platforms that use "@" as the assembly comment character,
5027 use "%" instead. */
5028 if (strcmp (ASM_COMMENT_START, "@") == 0)
5029 format = ",%%%s";
5030 #endif
5031 fprintf (asm_out_file, format, type);
5033 if (flags & SECTION_ENTSIZE)
5034 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5035 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5036 fprintf (asm_out_file, ",%s,comdat",
5037 lang_hooks.decls.comdat_group (decl));
5040 putc ('\n', asm_out_file);
5043 void
5044 default_coff_asm_named_section (const char *name, unsigned int flags,
5045 tree decl ATTRIBUTE_UNUSED)
5047 char flagchars[8], *f = flagchars;
5049 if (flags & SECTION_WRITE)
5050 *f++ = 'w';
5051 if (flags & SECTION_CODE)
5052 *f++ = 'x';
5053 *f = '\0';
5055 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5058 void
5059 default_pe_asm_named_section (const char *name, unsigned int flags,
5060 tree decl)
5062 default_coff_asm_named_section (name, flags, decl);
5064 if (flags & SECTION_LINKONCE)
5066 /* Functions may have been compiled at various levels of
5067 optimization so we can't use `same_size' here.
5068 Instead, have the linker pick one. */
5069 fprintf (asm_out_file, "\t.linkonce %s\n",
5070 (flags & SECTION_CODE ? "discard" : "same_size"));
5074 /* The lame default section selector. */
5076 section *
5077 default_select_section (tree decl, int reloc,
5078 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5080 if (DECL_P (decl))
5082 if (decl_readonly_section (decl, reloc))
5083 return readonly_data_section;
5085 else if (TREE_CODE (decl) == CONSTRUCTOR)
5087 if (! ((flag_pic && reloc)
5088 || !TREE_READONLY (decl)
5089 || TREE_SIDE_EFFECTS (decl)
5090 || !TREE_CONSTANT (decl)))
5091 return readonly_data_section;
5093 else if (TREE_CODE (decl) == STRING_CST)
5094 return readonly_data_section;
5095 else if (! (flag_pic && reloc))
5096 return readonly_data_section;
5098 return data_section;
5101 enum section_category
5102 categorize_decl_for_section (tree decl, int reloc, int shlib)
5104 enum section_category ret;
5106 if (TREE_CODE (decl) == FUNCTION_DECL)
5107 return SECCAT_TEXT;
5108 else if (TREE_CODE (decl) == STRING_CST)
5110 if (flag_mudflap) /* or !flag_merge_constants */
5111 return SECCAT_RODATA;
5112 else
5113 return SECCAT_RODATA_MERGE_STR;
5115 else if (TREE_CODE (decl) == VAR_DECL)
5117 if (DECL_INITIAL (decl) == NULL
5118 || DECL_INITIAL (decl) == error_mark_node
5119 || (flag_zero_initialized_in_bss
5120 /* Leave constant zeroes in .rodata so they can be shared. */
5121 && !TREE_READONLY (decl)
5122 && initializer_zerop (DECL_INITIAL (decl))))
5123 ret = SECCAT_BSS;
5124 else if (! TREE_READONLY (decl)
5125 || TREE_SIDE_EFFECTS (decl)
5126 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5128 if (shlib && (reloc & 2))
5129 ret = SECCAT_DATA_REL;
5130 else if (shlib && reloc)
5131 ret = SECCAT_DATA_REL_LOCAL;
5132 else
5133 ret = SECCAT_DATA;
5135 else if (shlib && (reloc & 2))
5136 ret = SECCAT_DATA_REL_RO;
5137 else if (shlib && reloc)
5138 ret = SECCAT_DATA_REL_RO_LOCAL;
5139 else if (reloc || flag_merge_constants < 2)
5140 /* C and C++ don't allow different variables to share the same
5141 location. -fmerge-all-constants allows even that (at the
5142 expense of not conforming). */
5143 ret = SECCAT_RODATA;
5144 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5145 ret = SECCAT_RODATA_MERGE_STR_INIT;
5146 else
5147 ret = SECCAT_RODATA_MERGE_CONST;
5149 else if (TREE_CODE (decl) == CONSTRUCTOR)
5151 if ((shlib && reloc)
5152 || TREE_SIDE_EFFECTS (decl)
5153 || ! TREE_CONSTANT (decl))
5154 ret = SECCAT_DATA;
5155 else
5156 ret = SECCAT_RODATA;
5158 else
5159 ret = SECCAT_RODATA;
5161 /* There are no read-only thread-local sections. */
5162 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5164 /* Note that this would be *just* SECCAT_BSS, except that there's
5165 no concept of a read-only thread-local-data section. */
5166 if (ret == SECCAT_BSS
5167 || (flag_zero_initialized_in_bss
5168 && initializer_zerop (DECL_INITIAL (decl))))
5169 ret = SECCAT_TBSS;
5170 else
5171 ret = SECCAT_TDATA;
5174 /* If the target uses small data sections, select it. */
5175 else if (targetm.in_small_data_p (decl))
5177 if (ret == SECCAT_BSS)
5178 ret = SECCAT_SBSS;
5179 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5180 ret = SECCAT_SRODATA;
5181 else
5182 ret = SECCAT_SDATA;
5185 return ret;
5188 bool
5189 decl_readonly_section (tree decl, int reloc)
5191 return decl_readonly_section_1 (decl, reloc, flag_pic);
5194 bool
5195 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5197 switch (categorize_decl_for_section (decl, reloc, shlib))
5199 case SECCAT_RODATA:
5200 case SECCAT_RODATA_MERGE_STR:
5201 case SECCAT_RODATA_MERGE_STR_INIT:
5202 case SECCAT_RODATA_MERGE_CONST:
5203 case SECCAT_SRODATA:
5204 return true;
5205 break;
5206 default:
5207 return false;
5208 break;
5212 /* Select a section based on the above categorization. */
5214 section *
5215 default_elf_select_section (tree decl, int reloc,
5216 unsigned HOST_WIDE_INT align)
5218 return default_elf_select_section_1 (decl, reloc, align, flag_pic);
5221 section *
5222 default_elf_select_section_1 (tree decl, int reloc,
5223 unsigned HOST_WIDE_INT align, int shlib)
5225 const char *sname;
5226 switch (categorize_decl_for_section (decl, reloc, shlib))
5228 case SECCAT_TEXT:
5229 /* We're not supposed to be called on FUNCTION_DECLs. */
5230 gcc_unreachable ();
5231 case SECCAT_RODATA:
5232 return readonly_data_section;
5233 case SECCAT_RODATA_MERGE_STR:
5234 return mergeable_string_section (decl, align, 0);
5235 case SECCAT_RODATA_MERGE_STR_INIT:
5236 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5237 case SECCAT_RODATA_MERGE_CONST:
5238 return mergeable_constant_section (DECL_MODE (decl), align, 0);
5239 case SECCAT_SRODATA:
5240 sname = ".sdata2";
5241 break;
5242 case SECCAT_DATA:
5243 return data_section;
5244 case SECCAT_DATA_REL:
5245 sname = ".data.rel";
5246 break;
5247 case SECCAT_DATA_REL_LOCAL:
5248 sname = ".data.rel.local";
5249 break;
5250 case SECCAT_DATA_REL_RO:
5251 sname = ".data.rel.ro";
5252 break;
5253 case SECCAT_DATA_REL_RO_LOCAL:
5254 sname = ".data.rel.ro.local";
5255 break;
5256 case SECCAT_SDATA:
5257 sname = ".sdata";
5258 break;
5259 case SECCAT_TDATA:
5260 sname = ".tdata";
5261 break;
5262 case SECCAT_BSS:
5263 if (bss_section)
5264 return bss_section;
5265 sname = ".bss";
5266 break;
5267 case SECCAT_SBSS:
5268 sname = ".sbss";
5269 break;
5270 case SECCAT_TBSS:
5271 sname = ".tbss";
5272 break;
5273 default:
5274 gcc_unreachable ();
5277 if (!DECL_P (decl))
5278 decl = NULL_TREE;
5279 return get_named_section (decl, sname, reloc);
5282 /* Construct a unique section name based on the decl name and the
5283 categorization performed above. */
5285 void
5286 default_unique_section (tree decl, int reloc)
5288 default_unique_section_1 (decl, reloc, flag_pic);
5291 void
5292 default_unique_section_1 (tree decl, int reloc, int shlib)
5294 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
5295 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5296 const char *prefix, *name;
5297 size_t nlen, plen;
5298 char *string;
5300 switch (categorize_decl_for_section (decl, reloc, shlib))
5302 case SECCAT_TEXT:
5303 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5304 break;
5305 case SECCAT_RODATA:
5306 case SECCAT_RODATA_MERGE_STR:
5307 case SECCAT_RODATA_MERGE_STR_INIT:
5308 case SECCAT_RODATA_MERGE_CONST:
5309 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5310 break;
5311 case SECCAT_SRODATA:
5312 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5313 break;
5314 case SECCAT_DATA:
5315 case SECCAT_DATA_REL:
5316 case SECCAT_DATA_REL_LOCAL:
5317 case SECCAT_DATA_REL_RO:
5318 case SECCAT_DATA_REL_RO_LOCAL:
5319 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5320 break;
5321 case SECCAT_SDATA:
5322 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5323 break;
5324 case SECCAT_BSS:
5325 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5326 break;
5327 case SECCAT_SBSS:
5328 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5329 break;
5330 case SECCAT_TDATA:
5331 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5332 break;
5333 case SECCAT_TBSS:
5334 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5335 break;
5336 default:
5337 gcc_unreachable ();
5339 plen = strlen (prefix);
5341 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5342 name = targetm.strip_name_encoding (name);
5343 nlen = strlen (name);
5345 string = alloca (nlen + plen + 1);
5346 memcpy (string, prefix, plen);
5347 memcpy (string + plen, name, nlen + 1);
5349 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5352 section *
5353 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5354 rtx x,
5355 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5357 if (flag_pic)
5358 switch (GET_CODE (x))
5360 case CONST:
5361 case SYMBOL_REF:
5362 case LABEL_REF:
5363 return data_section;
5365 default:
5366 break;
5369 return readonly_data_section;
5372 section *
5373 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5374 unsigned HOST_WIDE_INT align)
5376 /* ??? Handle small data here somehow. */
5378 if (flag_pic)
5379 switch (GET_CODE (x))
5381 case CONST:
5382 case SYMBOL_REF:
5383 return get_named_section (NULL, ".data.rel.ro", 3);
5385 case LABEL_REF:
5386 return get_named_section (NULL, ".data.rel.ro.local", 1);
5388 default:
5389 break;
5392 return mergeable_constant_section (mode, align, 0);
5395 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
5397 void
5398 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5400 rtx symbol;
5401 int flags;
5403 /* Careful not to prod global register variables. */
5404 if (!MEM_P (rtl))
5405 return;
5406 symbol = XEXP (rtl, 0);
5407 if (GET_CODE (symbol) != SYMBOL_REF)
5408 return;
5410 flags = 0;
5411 if (TREE_CODE (decl) == FUNCTION_DECL)
5412 flags |= SYMBOL_FLAG_FUNCTION;
5413 if (targetm.binds_local_p (decl))
5414 flags |= SYMBOL_FLAG_LOCAL;
5415 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5416 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5417 else if (targetm.in_small_data_p (decl))
5418 flags |= SYMBOL_FLAG_SMALL;
5419 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
5420 being PUBLIC, the thing *must* be defined in this translation unit.
5421 Prevent this buglet from being propagated into rtl code as well. */
5422 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5423 flags |= SYMBOL_FLAG_EXTERNAL;
5425 SYMBOL_REF_FLAGS (symbol) = flags;
5428 /* By default, we do nothing for encode_section_info, so we need not
5429 do anything but discard the '*' marker. */
5431 const char *
5432 default_strip_name_encoding (const char *str)
5434 return str + (*str == '*');
5437 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5438 wrt cross-module name binding. */
5440 bool
5441 default_binds_local_p (tree exp)
5443 return default_binds_local_p_1 (exp, flag_shlib);
5446 bool
5447 default_binds_local_p_1 (tree exp, int shlib)
5449 bool local_p;
5451 /* A non-decl is an entry in the constant pool. */
5452 if (!DECL_P (exp))
5453 local_p = true;
5454 /* Weakrefs may not bind locally, even though the weakref itself is
5455 always static and therefore local. */
5456 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
5457 local_p = false;
5458 /* Static variables are always local. */
5459 else if (! TREE_PUBLIC (exp))
5460 local_p = true;
5461 /* A variable is local if the user has said explicitly that it will
5462 be. */
5463 else if (DECL_VISIBILITY_SPECIFIED (exp)
5464 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5465 local_p = true;
5466 /* Variables defined outside this object might not be local. */
5467 else if (DECL_EXTERNAL (exp))
5468 local_p = false;
5469 /* If defined in this object and visibility is not default, must be
5470 local. */
5471 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5472 local_p = true;
5473 /* Default visibility weak data can be overridden by a strong symbol
5474 in another module and so are not local. */
5475 else if (DECL_WEAK (exp))
5476 local_p = false;
5477 /* If PIC, then assume that any global name can be overridden by
5478 symbols resolved from other modules. */
5479 else if (shlib)
5480 local_p = false;
5481 /* Uninitialized COMMON variable may be unified with symbols
5482 resolved from other modules. */
5483 else if (DECL_COMMON (exp)
5484 && (DECL_INITIAL (exp) == NULL
5485 || DECL_INITIAL (exp) == error_mark_node))
5486 local_p = false;
5487 /* Otherwise we're left with initialized (or non-common) global data
5488 which is of necessity defined locally. */
5489 else
5490 local_p = true;
5492 return local_p;
5495 /* Determine whether or not a pointer mode is valid. Assume defaults
5496 of ptr_mode or Pmode - can be overridden. */
5497 bool
5498 default_valid_pointer_mode (enum machine_mode mode)
5500 return (mode == ptr_mode || mode == Pmode);
5503 /* Default function to output code that will globalize a label. A
5504 target must define GLOBAL_ASM_OP or provide its own function to
5505 globalize a label. */
5506 #ifdef GLOBAL_ASM_OP
5507 void
5508 default_globalize_label (FILE * stream, const char *name)
5510 fputs (GLOBAL_ASM_OP, stream);
5511 assemble_name (stream, name);
5512 putc ('\n', stream);
5514 #endif /* GLOBAL_ASM_OP */
5516 /* Default function to output a label for unwind information. The
5517 default is to do nothing. A target that needs nonlocal labels for
5518 unwind information must provide its own function to do this. */
5519 void
5520 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5521 tree decl ATTRIBUTE_UNUSED,
5522 int for_eh ATTRIBUTE_UNUSED,
5523 int empty ATTRIBUTE_UNUSED)
5527 /* This is how to output an internal numbered label where PREFIX is
5528 the class of label and LABELNO is the number within the class. */
5530 void
5531 default_internal_label (FILE *stream, const char *prefix,
5532 unsigned long labelno)
5534 char *const buf = alloca (40 + strlen (prefix));
5535 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5536 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5539 /* This is the default behavior at the beginning of a file. It's
5540 controlled by two other target-hook toggles. */
5541 void
5542 default_file_start (void)
5544 if (targetm.file_start_app_off && !flag_verbose_asm)
5545 fputs (ASM_APP_OFF, asm_out_file);
5547 if (targetm.file_start_file_directive)
5548 output_file_directive (asm_out_file, main_input_filename);
5551 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5552 which emits a special section directive used to indicate whether or
5553 not this object file needs an executable stack. This is primarily
5554 a GNU extension to ELF but could be used on other targets. */
5556 int trampolines_created;
5558 void
5559 file_end_indicate_exec_stack (void)
5561 unsigned int flags = SECTION_DEBUG;
5562 if (trampolines_created)
5563 flags |= SECTION_CODE;
5565 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
5568 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
5569 a get_unnamed_section callback. */
5571 void
5572 output_section_asm_op (const void *directive)
5574 fprintf (asm_out_file, "%s\n", (const char *) directive);
5577 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
5578 the current section is NEW_SECTION. */
5580 void
5581 switch_to_section (section *new_section)
5583 if (in_section == new_section)
5584 return;
5586 if (new_section->common.flags & SECTION_FORGET)
5587 in_section = NULL;
5588 else
5590 in_section = new_section;
5591 if (new_section->common.flags & SECTION_CODE)
5592 last_text_section = in_section;
5595 if (new_section->common.flags & SECTION_NAMED)
5597 if (cfun
5598 && !cfun->unlikely_text_section_name
5599 && strcmp (new_section->named.name,
5600 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5601 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
5603 targetm.asm_out.named_section (new_section->named.name,
5604 new_section->named.common.flags,
5605 new_section->named.decl);
5607 else
5608 new_section->unnamed.callback (new_section->unnamed.data);
5610 new_section->common.flags |= SECTION_DECLARED;
5613 #include "gt-varasm.h"