c-common.h (flag_const_strings): Don't declare.
[official-gcc.git] / gcc / varasm.c
blob68bd026a2791f926b6d6dff6d8c608c1bd689b59
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, 2006
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 /* True if code for the current function is currently being directed
171 at the cold section. */
172 bool in_cold_section_p;
174 /* A linked list of all the unnamed sections. */
175 static GTY(()) section *unnamed_sections;
177 /* Return a nonzero value if DECL has a section attribute. */
178 #ifndef IN_NAMED_SECTION
179 #define IN_NAMED_SECTION(DECL) \
180 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
181 && DECL_SECTION_NAME (DECL) != NULL_TREE)
182 #endif
184 /* Hash table of named sections. */
185 static GTY((param_is (section))) htab_t section_htab;
187 /* Helper routines for maintaining section_htab. */
189 static int
190 section_entry_eq (const void *p1, const void *p2)
192 const section *old = p1;
193 const char *new = p2;
195 return strcmp (old->named.name, new) == 0;
198 static hashval_t
199 section_entry_hash (const void *p)
201 const section *old = p;
202 return htab_hash_string (old->named.name);
205 /* Return a new unnamed section with the given fields. */
207 section *
208 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
209 const void *data)
211 section *sect;
213 sect = ggc_alloc (sizeof (struct unnamed_section));
214 sect->unnamed.common.flags = flags;
215 sect->unnamed.callback = callback;
216 sect->unnamed.data = data;
217 sect->unnamed.next = unnamed_sections;
219 unnamed_sections = sect;
220 return sect;
223 /* Return the named section structure associated with NAME. Create
224 a new section with the given fields if no such structure exists. */
226 section *
227 get_section (const char *name, unsigned int flags, tree decl)
229 section *sect, **slot;
231 slot = (section **)
232 htab_find_slot_with_hash (section_htab, name,
233 htab_hash_string (name), INSERT);
234 flags |= SECTION_NAMED;
235 if (*slot == NULL)
237 sect = ggc_alloc (sizeof (struct named_section));
238 sect->named.common.flags = flags;
239 sect->named.name = ggc_strdup (name);
240 sect->named.decl = decl;
241 *slot = sect;
243 else
245 sect = *slot;
246 if ((sect->common.flags & ~SECTION_DECLARED) != flags
247 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
249 /* Sanity check user variables for flag changes. */
250 if (decl == 0)
251 decl = sect->named.decl;
252 gcc_assert (decl);
253 error ("%+D causes a section type conflict", decl);
256 return sect;
259 static void
260 initialize_cold_section_name (void)
262 const char *stripped_name;
263 char *name, *buffer;
264 tree dsn;
266 gcc_assert (cfun && current_function_decl);
267 if (cfun->unlikely_text_section_name)
268 return;
270 dsn = DECL_SECTION_NAME (current_function_decl);
271 if (flag_function_sections && dsn)
273 name = alloca (TREE_STRING_LENGTH (dsn) + 1);
274 memcpy (name, TREE_STRING_POINTER (dsn), TREE_STRING_LENGTH (dsn) + 1);
276 stripped_name = targetm.strip_name_encoding (name);
278 buffer = ACONCAT ((stripped_name, "_unlikely", NULL));
279 cfun->unlikely_text_section_name = ggc_strdup (buffer);
281 else
282 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
285 /* Tell assembler to switch to unlikely-to-be-executed text section. */
287 section *
288 unlikely_text_section (void)
290 if (cfun)
292 if (!cfun->unlikely_text_section_name)
293 initialize_cold_section_name ();
295 return get_named_section (NULL, cfun->unlikely_text_section_name, 0);
297 else
298 return get_named_section (NULL, UNLIKELY_EXECUTED_TEXT_SECTION_NAME, 0);
301 /* When called within a function context, return true if the function
302 has been assigned a cold text section and if SECT is that section.
303 When called outside a function context, return true if SECT is the
304 default cold section. */
306 bool
307 unlikely_text_section_p (section *sect)
309 const char *name;
311 if (cfun)
312 name = cfun->unlikely_text_section_name;
313 else
314 name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
316 return (name
317 && sect
318 && (sect->common.flags & SECTION_NAMED) != 0
319 && strcmp (name, sect->named.name) == 0);
322 /* Return a section with a particular name and with whatever SECTION_*
323 flags section_type_flags deems appropriate. The name of the section
324 is taken from NAME if nonnull, otherwise it is taken from DECL's
325 DECL_SECTION_NAME. DECL is the decl associated with the section
326 (see the section comment for details) and RELOC is as for
327 section_type_flags. */
329 section *
330 get_named_section (tree decl, const char *name, int reloc)
332 unsigned int flags;
334 gcc_assert (!decl || DECL_P (decl));
335 if (name == NULL)
336 name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
338 flags = targetm.section_type_flags (decl, name, reloc);
340 return get_section (name, flags, decl);
343 /* If required, set DECL_SECTION_NAME to a unique name. */
345 void
346 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
347 int flag_function_or_data_sections)
349 if (DECL_SECTION_NAME (decl) == NULL_TREE
350 && targetm.have_named_sections
351 && (flag_function_or_data_sections
352 || DECL_ONE_ONLY (decl)))
353 targetm.asm_out.unique_section (decl, reloc);
356 #ifdef BSS_SECTION_ASM_OP
358 #ifdef ASM_OUTPUT_BSS
360 /* Utility function for ASM_OUTPUT_BSS for targets to use if
361 they don't support alignments in .bss.
362 ??? It is believed that this function will work in most cases so such
363 support is localized here. */
365 static void
366 asm_output_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
367 const char *name,
368 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
369 unsigned HOST_WIDE_INT rounded)
371 targetm.asm_out.globalize_label (file, name);
372 switch_to_section (bss_section);
373 #ifdef ASM_DECLARE_OBJECT_NAME
374 last_assemble_variable_decl = decl;
375 ASM_DECLARE_OBJECT_NAME (file, name, decl);
376 #else
377 /* Standard thing is just output label for the object. */
378 ASM_OUTPUT_LABEL (file, name);
379 #endif /* ASM_DECLARE_OBJECT_NAME */
380 ASM_OUTPUT_SKIP (file, rounded ? rounded : 1);
383 #endif
385 #ifdef ASM_OUTPUT_ALIGNED_BSS
387 /* Utility function for targets to use in implementing
388 ASM_OUTPUT_ALIGNED_BSS.
389 ??? It is believed that this function will work in most cases so such
390 support is localized here. */
392 static void
393 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
394 const char *name, unsigned HOST_WIDE_INT size,
395 int align)
397 switch_to_section (bss_section);
398 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
399 #ifdef ASM_DECLARE_OBJECT_NAME
400 last_assemble_variable_decl = decl;
401 ASM_DECLARE_OBJECT_NAME (file, name, decl);
402 #else
403 /* Standard thing is just output label for the object. */
404 ASM_OUTPUT_LABEL (file, name);
405 #endif /* ASM_DECLARE_OBJECT_NAME */
406 ASM_OUTPUT_SKIP (file, size ? size : 1);
409 #endif
411 #endif /* BSS_SECTION_ASM_OP */
413 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
414 /* Return the hot section for function DECL. Return text_section for
415 null DECLs. */
417 static section *
418 hot_function_section (tree decl)
420 if (decl != NULL_TREE
421 && DECL_SECTION_NAME (decl) != NULL_TREE
422 && targetm.have_named_sections)
423 return get_named_section (decl, NULL, 0);
424 else
425 return text_section;
427 #endif
429 /* Return the section for function DECL.
431 If DECL is NULL_TREE, return the text section. We can be passed
432 NULL_TREE under some circumstances by dbxout.c at least. */
434 section *
435 function_section (tree decl)
437 int reloc = 0;
439 if (first_function_block_is_cold)
440 reloc = 1;
442 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
443 return targetm.asm_out.select_section (decl, reloc, DECL_ALIGN (decl));
444 #else
445 return hot_function_section (decl);
446 #endif
449 section *
450 current_function_section (void)
452 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
453 return targetm.asm_out.select_section (current_function_decl,
454 in_cold_section_p,
455 DECL_ALIGN (current_function_decl));
456 #else
457 return (in_cold_section_p
458 ? unlikely_text_section ()
459 : hot_function_section (current_function_decl));
460 #endif
463 /* Return the read-only data section associated with function DECL. */
465 section *
466 default_function_rodata_section (tree decl)
468 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
470 const char *name = TREE_STRING_POINTER (DECL_SECTION_NAME (decl));
472 if (DECL_ONE_ONLY (decl) && HAVE_COMDAT_GROUP)
474 size_t len = strlen (name) + 3;
475 char* rname = alloca (len);
477 strcpy (rname, ".rodata");
478 strcat (rname, name + 5);
479 return get_section (rname, SECTION_LINKONCE, decl);
481 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
482 else if (DECL_ONE_ONLY (decl)
483 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
485 size_t len = strlen (name) + 1;
486 char *rname = alloca (len);
488 memcpy (rname, name, len);
489 rname[14] = 'r';
490 return get_section (rname, SECTION_LINKONCE, decl);
492 /* For .text.foo we want to use .rodata.foo. */
493 else if (flag_function_sections && flag_data_sections
494 && strncmp (name, ".text.", 6) == 0)
496 size_t len = strlen (name) + 1;
497 char *rname = alloca (len + 2);
499 memcpy (rname, ".rodata", 7);
500 memcpy (rname + 7, name + 5, len - 5);
501 return get_section (rname, 0, decl);
505 return readonly_data_section;
508 /* Return the read-only data section associated with function DECL
509 for targets where that section should be always the single
510 readonly data section. */
512 section *
513 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
515 return readonly_data_section;
518 /* Switch to section for variable DECL. RELOC is the same as the
519 argument to SELECT_SECTION. */
521 void
522 variable_section (tree decl, int reloc)
524 if (IN_NAMED_SECTION (decl))
525 switch_to_section (get_named_section (decl, NULL, reloc));
526 else
527 switch_to_section (targetm.asm_out.select_section (decl, reloc,
528 DECL_ALIGN (decl)));
531 /* Return the section to use for string merging. */
533 static section *
534 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
535 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
536 unsigned int flags ATTRIBUTE_UNUSED)
538 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
539 && TREE_CODE (decl) == STRING_CST
540 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
541 && align <= 256
542 && TREE_STRING_LENGTH (decl) >= int_size_in_bytes (TREE_TYPE (decl)))
544 enum machine_mode mode;
545 unsigned int modesize;
546 const char *str;
547 int i, j, len, unit;
548 char name[30];
550 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
551 modesize = GET_MODE_BITSIZE (mode);
552 if (modesize >= 8 && modesize <= 256
553 && (modesize & (modesize - 1)) == 0)
555 if (align < modesize)
556 align = modesize;
558 str = TREE_STRING_POINTER (decl);
559 len = TREE_STRING_LENGTH (decl);
560 unit = GET_MODE_SIZE (mode);
562 /* Check for embedded NUL characters. */
563 for (i = 0; i < len; i += unit)
565 for (j = 0; j < unit; j++)
566 if (str[i + j] != '\0')
567 break;
568 if (j == unit)
569 break;
571 if (i == len - unit)
573 sprintf (name, ".rodata.str%d.%d", modesize / 8,
574 (int) (align / 8));
575 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
576 return get_section (name, flags, NULL);
581 return readonly_data_section;
584 /* Return the section to use for constant merging. */
586 section *
587 mergeable_constant_section (enum machine_mode mode ATTRIBUTE_UNUSED,
588 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
589 unsigned int flags ATTRIBUTE_UNUSED)
591 unsigned int modesize = GET_MODE_BITSIZE (mode);
593 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
594 && mode != VOIDmode
595 && mode != BLKmode
596 && modesize <= align
597 && align >= 8
598 && align <= 256
599 && (align & (align - 1)) == 0)
601 char name[24];
603 sprintf (name, ".rodata.cst%d", (int) (align / 8));
604 flags |= (align / 8) | SECTION_MERGE;
605 return get_section (name, flags, NULL);
607 return readonly_data_section;
610 /* Given NAME, a putative register name, discard any customary prefixes. */
612 static const char *
613 strip_reg_name (const char *name)
615 #ifdef REGISTER_PREFIX
616 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
617 name += strlen (REGISTER_PREFIX);
618 #endif
619 if (name[0] == '%' || name[0] == '#')
620 name++;
621 return name;
624 /* The user has asked for a DECL to have a particular name. Set (or
625 change) it in such a way that we don't prefix an underscore to
626 it. */
627 void
628 set_user_assembler_name (tree decl, const char *name)
630 char *starred = alloca (strlen (name) + 2);
631 starred[0] = '*';
632 strcpy (starred + 1, name);
633 change_decl_assembler_name (decl, get_identifier (starred));
634 SET_DECL_RTL (decl, NULL_RTX);
637 /* Decode an `asm' spec for a declaration as a register name.
638 Return the register number, or -1 if nothing specified,
639 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
640 or -3 if ASMSPEC is `cc' and is not recognized,
641 or -4 if ASMSPEC is `memory' and is not recognized.
642 Accept an exact spelling or a decimal number.
643 Prefixes such as % are optional. */
646 decode_reg_name (const char *asmspec)
648 if (asmspec != 0)
650 int i;
652 /* Get rid of confusing prefixes. */
653 asmspec = strip_reg_name (asmspec);
655 /* Allow a decimal number as a "register name". */
656 for (i = strlen (asmspec) - 1; i >= 0; i--)
657 if (! ISDIGIT (asmspec[i]))
658 break;
659 if (asmspec[0] != 0 && i < 0)
661 i = atoi (asmspec);
662 if (i < FIRST_PSEUDO_REGISTER && i >= 0)
663 return i;
664 else
665 return -2;
668 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
669 if (reg_names[i][0]
670 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
671 return i;
673 #ifdef ADDITIONAL_REGISTER_NAMES
675 static const struct { const char *const name; const int number; } table[]
676 = ADDITIONAL_REGISTER_NAMES;
678 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
679 if (table[i].name[0]
680 && ! strcmp (asmspec, table[i].name))
681 return table[i].number;
683 #endif /* ADDITIONAL_REGISTER_NAMES */
685 if (!strcmp (asmspec, "memory"))
686 return -4;
688 if (!strcmp (asmspec, "cc"))
689 return -3;
691 return -2;
694 return -1;
697 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
698 have static storage duration. In other words, it should not be an
699 automatic variable, including PARM_DECLs.
701 There is, however, one exception: this function handles variables
702 explicitly placed in a particular register by the user.
704 This is never called for PARM_DECL nodes. */
706 void
707 make_decl_rtl (tree decl)
709 const char *name = 0;
710 int reg_number;
711 rtx x;
713 /* Check that we are not being given an automatic variable. */
714 gcc_assert (TREE_CODE (decl) != PARM_DECL
715 && TREE_CODE (decl) != RESULT_DECL);
717 /* A weak alias has TREE_PUBLIC set but not the other bits. */
718 gcc_assert (TREE_CODE (decl) != VAR_DECL
719 || TREE_STATIC (decl)
720 || TREE_PUBLIC (decl)
721 || DECL_EXTERNAL (decl)
722 || DECL_REGISTER (decl));
724 /* And that we were not given a type or a label. */
725 gcc_assert (TREE_CODE (decl) != TYPE_DECL
726 && TREE_CODE (decl) != LABEL_DECL);
728 /* For a duplicate declaration, we can be called twice on the
729 same DECL node. Don't discard the RTL already made. */
730 if (DECL_RTL_SET_P (decl))
732 /* If the old RTL had the wrong mode, fix the mode. */
733 if (GET_MODE (DECL_RTL (decl)) != DECL_MODE (decl))
734 SET_DECL_RTL (decl, adjust_address_nv (DECL_RTL (decl),
735 DECL_MODE (decl), 0));
737 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
738 return;
740 /* ??? Another way to do this would be to maintain a hashed
741 table of such critters. Instead of adding stuff to a DECL
742 to give certain attributes to it, we could use an external
743 hash map from DECL to set of attributes. */
745 /* Let the target reassign the RTL if it wants.
746 This is necessary, for example, when one machine specific
747 decl attribute overrides another. */
748 targetm.encode_section_info (decl, DECL_RTL (decl), false);
750 /* Make this function static known to the mudflap runtime. */
751 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
752 mudflap_enqueue_decl (decl);
754 return;
757 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
759 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
760 && DECL_REGISTER (decl))
762 error ("register name not specified for %q+D", decl);
764 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
766 const char *asmspec = name+1;
767 reg_number = decode_reg_name (asmspec);
768 /* First detect errors in declaring global registers. */
769 if (reg_number == -1)
770 error ("register name not specified for %q+D", decl);
771 else if (reg_number < 0)
772 error ("invalid register name for %q+D", decl);
773 else if (TYPE_MODE (TREE_TYPE (decl)) == BLKmode)
774 error ("data type of %q+D isn%'t suitable for a register",
775 decl);
776 else if (! HARD_REGNO_MODE_OK (reg_number, TYPE_MODE (TREE_TYPE (decl))))
777 error ("register specified for %q+D isn%'t suitable for data type",
778 decl);
779 /* Now handle properly declared static register variables. */
780 else
782 int nregs;
784 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
786 DECL_INITIAL (decl) = 0;
787 error ("global register variable has initial value");
789 if (TREE_THIS_VOLATILE (decl))
790 warning (OPT_Wvolatile_register_var,
791 "optimization may eliminate reads and/or "
792 "writes to register variables");
794 /* If the user specified one of the eliminables registers here,
795 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
796 confused with that register and be eliminated. This usage is
797 somewhat suspect... */
799 SET_DECL_RTL (decl, gen_rtx_raw_REG (DECL_MODE (decl), reg_number));
800 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
801 REG_USERVAR_P (DECL_RTL (decl)) = 1;
803 if (TREE_STATIC (decl))
805 /* Make this register global, so not usable for anything
806 else. */
807 #ifdef ASM_DECLARE_REGISTER_GLOBAL
808 name = IDENTIFIER_POINTER (DECL_NAME (decl));
809 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
810 #endif
811 nregs = hard_regno_nregs[reg_number][DECL_MODE (decl)];
812 while (nregs > 0)
813 globalize_reg (reg_number + --nregs);
816 /* As a register variable, it has no section. */
817 return;
820 /* Now handle ordinary static variables and functions (in memory).
821 Also handle vars declared register invalidly. */
822 else if (name[0] == '*')
824 #ifdef REGISTER_PREFIX
825 if (strlen (REGISTER_PREFIX) != 0)
827 reg_number = decode_reg_name (name);
828 if (reg_number >= 0 || reg_number == -3)
829 error ("register name given for non-register variable %q+D", decl);
831 #endif
834 /* Specifying a section attribute on a variable forces it into a
835 non-.bss section, and thus it cannot be common. */
836 if (TREE_CODE (decl) == VAR_DECL
837 && DECL_SECTION_NAME (decl) != NULL_TREE
838 && DECL_INITIAL (decl) == NULL_TREE
839 && DECL_COMMON (decl))
840 DECL_COMMON (decl) = 0;
842 /* Variables can't be both common and weak. */
843 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
844 DECL_COMMON (decl) = 0;
846 x = gen_rtx_SYMBOL_REF (Pmode, name);
847 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
848 SET_SYMBOL_REF_DECL (x, decl);
850 x = gen_rtx_MEM (DECL_MODE (decl), x);
851 if (TREE_CODE (decl) != FUNCTION_DECL)
852 set_mem_attributes (x, decl, 1);
853 SET_DECL_RTL (decl, x);
855 /* Optionally set flags or add text to the name to record information
856 such as that it is a function name.
857 If the name is changed, the macro ASM_OUTPUT_LABELREF
858 will have to know how to strip this information. */
859 targetm.encode_section_info (decl, DECL_RTL (decl), true);
861 /* Make this function static known to the mudflap runtime. */
862 if (flag_mudflap && TREE_CODE (decl) == VAR_DECL)
863 mudflap_enqueue_decl (decl);
866 /* Make the rtl for variable VAR be volatile.
867 Use this only for static variables. */
869 void
870 make_var_volatile (tree var)
872 gcc_assert (MEM_P (DECL_RTL (var)));
874 MEM_VOLATILE_P (DECL_RTL (var)) = 1;
877 /* Output a string of literal assembler code
878 for an `asm' keyword used between functions. */
880 void
881 assemble_asm (tree string)
883 app_enable ();
885 if (TREE_CODE (string) == ADDR_EXPR)
886 string = TREE_OPERAND (string, 0);
888 fprintf (asm_out_file, "\t%s\n", TREE_STRING_POINTER (string));
891 /* Record an element in the table of global destructors. SYMBOL is
892 a SYMBOL_REF of the function to be called; PRIORITY is a number
893 between 0 and MAX_INIT_PRIORITY. */
895 void
896 default_stabs_asm_out_destructor (rtx symbol ATTRIBUTE_UNUSED,
897 int priority ATTRIBUTE_UNUSED)
899 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
900 /* Tell GNU LD that this is part of the static destructor set.
901 This will work for any system that uses stabs, most usefully
902 aout systems. */
903 dbxout_begin_simple_stabs ("___DTOR_LIST__", 22 /* N_SETT */);
904 dbxout_stab_value_label (XSTR (symbol, 0));
905 #else
906 sorry ("global destructors not supported on this target");
907 #endif
910 void
911 default_named_section_asm_out_destructor (rtx symbol, int priority)
913 const char *section = ".dtors";
914 char buf[16];
916 /* ??? This only works reliably with the GNU linker. */
917 if (priority != DEFAULT_INIT_PRIORITY)
919 sprintf (buf, ".dtors.%.5u",
920 /* Invert the numbering so the linker puts us in the proper
921 order; constructors are run from right to left, and the
922 linker sorts in increasing order. */
923 MAX_INIT_PRIORITY - priority);
924 section = buf;
927 switch_to_section (get_section (section, SECTION_WRITE, NULL));
928 assemble_align (POINTER_SIZE);
929 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
932 #ifdef DTORS_SECTION_ASM_OP
933 void
934 default_dtor_section_asm_out_destructor (rtx symbol,
935 int priority ATTRIBUTE_UNUSED)
937 switch_to_section (dtors_section);
938 assemble_align (POINTER_SIZE);
939 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
941 #endif
943 /* Likewise for global constructors. */
945 void
946 default_stabs_asm_out_constructor (rtx symbol ATTRIBUTE_UNUSED,
947 int priority ATTRIBUTE_UNUSED)
949 #if defined DBX_DEBUGGING_INFO || defined XCOFF_DEBUGGING_INFO
950 /* Tell GNU LD that this is part of the static destructor set.
951 This will work for any system that uses stabs, most usefully
952 aout systems. */
953 dbxout_begin_simple_stabs ("___CTOR_LIST__", 22 /* N_SETT */);
954 dbxout_stab_value_label (XSTR (symbol, 0));
955 #else
956 sorry ("global constructors not supported on this target");
957 #endif
960 void
961 default_named_section_asm_out_constructor (rtx symbol, int priority)
963 const char *section = ".ctors";
964 char buf[16];
966 /* ??? This only works reliably with the GNU linker. */
967 if (priority != DEFAULT_INIT_PRIORITY)
969 sprintf (buf, ".ctors.%.5u",
970 /* Invert the numbering so the linker puts us in the proper
971 order; constructors are run from right to left, and the
972 linker sorts in increasing order. */
973 MAX_INIT_PRIORITY - priority);
974 section = buf;
977 switch_to_section (get_section (section, SECTION_WRITE, NULL));
978 assemble_align (POINTER_SIZE);
979 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
982 #ifdef CTORS_SECTION_ASM_OP
983 void
984 default_ctor_section_asm_out_constructor (rtx symbol,
985 int priority ATTRIBUTE_UNUSED)
987 switch_to_section (ctors_section);
988 assemble_align (POINTER_SIZE);
989 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
991 #endif
993 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
994 a nonzero value if the constant pool should be output before the
995 start of the function, or a zero value if the pool should output
996 after the end of the function. The default is to put it before the
997 start. */
999 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1000 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1001 #endif
1003 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1004 to be output to assembler.
1005 Set first_global_object_name and weak_global_object_name as appropriate. */
1007 void
1008 notice_global_symbol (tree decl)
1010 const char **type = &first_global_object_name;
1012 if (first_global_object_name
1013 || !TREE_PUBLIC (decl) || DECL_EXTERNAL (decl)
1014 || !DECL_NAME (decl)
1015 || (TREE_CODE (decl) != FUNCTION_DECL
1016 && (TREE_CODE (decl) != VAR_DECL
1017 || (DECL_COMMON (decl)
1018 && (DECL_INITIAL (decl) == 0
1019 || DECL_INITIAL (decl) == error_mark_node))))
1020 || !MEM_P (DECL_RTL (decl)))
1021 return;
1023 /* We win when global object is found, but it is useful to know about weak
1024 symbol as well so we can produce nicer unique names. */
1025 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl))
1026 type = &weak_global_object_name;
1028 if (!*type)
1030 const char *p;
1031 const char *name;
1032 rtx decl_rtl = DECL_RTL (decl);
1034 p = targetm.strip_name_encoding (XSTR (XEXP (decl_rtl, 0), 0));
1035 name = ggc_strdup (p);
1037 *type = name;
1041 /* Output assembler code for the constant pool of a function and associated
1042 with defining the name of the function. DECL describes the function.
1043 NAME is the function's name. For the constant pool, we use the current
1044 constant pool data. */
1046 void
1047 assemble_start_function (tree decl, const char *fnname)
1049 int align;
1050 char tmp_label[100];
1051 bool hot_label_written = false;
1053 cfun->unlikely_text_section_name = NULL;
1055 first_function_block_is_cold = false;
1056 if (flag_reorder_blocks_and_partition)
1058 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1059 cfun->hot_section_label = ggc_strdup (tmp_label);
1060 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1061 cfun->cold_section_label = ggc_strdup (tmp_label);
1062 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1063 cfun->hot_section_end_label = ggc_strdup (tmp_label);
1064 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1065 cfun->cold_section_end_label = ggc_strdup (tmp_label);
1066 const_labelno++;
1068 else
1070 cfun->hot_section_label = NULL;
1071 cfun->cold_section_label = NULL;
1072 cfun->hot_section_end_label = NULL;
1073 cfun->cold_section_end_label = NULL;
1076 /* The following code does not need preprocessing in the assembler. */
1078 app_disable ();
1080 if (CONSTANT_POOL_BEFORE_FUNCTION)
1081 output_constant_pool (fnname, decl);
1083 resolve_unique_section (decl, 0, flag_function_sections);
1085 /* Make sure the not and cold text (code) sections are properly
1086 aligned. This is necessary here in the case where the function
1087 has both hot and cold sections, because we don't want to re-set
1088 the alignment when the section switch happens mid-function. */
1090 if (flag_reorder_blocks_and_partition)
1092 switch_to_section (unlikely_text_section ());
1093 assemble_align (FUNCTION_BOUNDARY);
1094 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_label);
1096 /* When the function starts with a cold section, we need to explicitly
1097 align the hot section and write out the hot section label.
1098 But if the current function is a thunk, we do not have a CFG. */
1099 if (!current_function_is_thunk
1100 && BB_PARTITION (ENTRY_BLOCK_PTR->next_bb) == BB_COLD_PARTITION)
1102 switch_to_section (text_section);
1103 assemble_align (FUNCTION_BOUNDARY);
1104 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1105 hot_label_written = true;
1106 first_function_block_is_cold = true;
1109 else if (DECL_SECTION_NAME (decl))
1111 /* Calls to function_section rely on first_function_block_is_cold
1112 being accurate. The first block may be cold even if we aren't
1113 doing partitioning, if the entire function was decided by
1114 choose_function_section (predict.c) to be cold. */
1116 initialize_cold_section_name ();
1118 if (cfun->unlikely_text_section_name
1119 && strcmp (TREE_STRING_POINTER (DECL_SECTION_NAME (decl)),
1120 cfun->unlikely_text_section_name) == 0)
1121 first_function_block_is_cold = true;
1124 in_cold_section_p = first_function_block_is_cold;
1126 /* Switch to the correct text section for the start of the function. */
1128 switch_to_section (function_section (decl));
1129 if (flag_reorder_blocks_and_partition
1130 && !hot_label_written)
1131 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_label);
1133 /* Tell assembler to move to target machine's alignment for functions. */
1134 align = floor_log2 (FUNCTION_BOUNDARY / BITS_PER_UNIT);
1135 if (align < force_align_functions_log)
1136 align = force_align_functions_log;
1137 if (align > 0)
1139 ASM_OUTPUT_ALIGN (asm_out_file, align);
1142 /* Handle a user-specified function alignment.
1143 Note that we still need to align to FUNCTION_BOUNDARY, as above,
1144 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1145 if (align_functions_log > align
1146 && cfun->function_frequency != FUNCTION_FREQUENCY_UNLIKELY_EXECUTED)
1148 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1149 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1150 align_functions_log, align_functions - 1);
1151 #else
1152 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1153 #endif
1156 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1157 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1158 #endif
1160 (*debug_hooks->begin_function) (decl);
1162 /* Make function name accessible from other files, if appropriate. */
1164 if (TREE_PUBLIC (decl))
1166 notice_global_symbol (decl);
1168 globalize_decl (decl);
1170 maybe_assemble_visibility (decl);
1173 if (DECL_PRESERVE_P (decl))
1174 targetm.asm_out.mark_decl_preserved (fnname);
1176 /* Do any machine/system dependent processing of the function name. */
1177 #ifdef ASM_DECLARE_FUNCTION_NAME
1178 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1179 #else
1180 /* Standard thing is just output label for the function. */
1181 ASM_OUTPUT_LABEL (asm_out_file, fnname);
1182 #endif /* ASM_DECLARE_FUNCTION_NAME */
1185 /* Output assembler code associated with defining the size of the
1186 function. DECL describes the function. NAME is the function's name. */
1188 void
1189 assemble_end_function (tree decl, const char *fnname)
1191 #ifdef ASM_DECLARE_FUNCTION_SIZE
1192 /* We could have switched section in the middle of the function. */
1193 if (flag_reorder_blocks_and_partition)
1194 switch_to_section (function_section (decl));
1195 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1196 #endif
1197 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1199 output_constant_pool (fnname, decl);
1200 switch_to_section (function_section (decl)); /* need to switch back */
1202 /* Output labels for end of hot/cold text sections (to be used by
1203 debug info.) */
1204 if (flag_reorder_blocks_and_partition)
1206 section *save_text_section;
1208 save_text_section = in_section;
1209 switch_to_section (unlikely_text_section ());
1210 ASM_OUTPUT_LABEL (asm_out_file, cfun->cold_section_end_label);
1211 if (first_function_block_is_cold)
1212 switch_to_section (text_section);
1213 else
1214 switch_to_section (function_section (decl));
1215 ASM_OUTPUT_LABEL (asm_out_file, cfun->hot_section_end_label);
1216 switch_to_section (save_text_section);
1220 /* Assemble code to leave SIZE bytes of zeros. */
1222 void
1223 assemble_zeros (unsigned HOST_WIDE_INT size)
1225 /* Do no output if -fsyntax-only. */
1226 if (flag_syntax_only)
1227 return;
1229 #ifdef ASM_NO_SKIP_IN_TEXT
1230 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1231 so we must output 0s explicitly in the text section. */
1232 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1234 unsigned HOST_WIDE_INT i;
1235 for (i = 0; i < size; i++)
1236 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1238 else
1239 #endif
1240 if (size > 0)
1241 ASM_OUTPUT_SKIP (asm_out_file, size);
1244 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1246 void
1247 assemble_align (int align)
1249 if (align > BITS_PER_UNIT)
1251 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1255 /* Assemble a string constant with the specified C string as contents. */
1257 void
1258 assemble_string (const char *p, int size)
1260 int pos = 0;
1261 int maximum = 2000;
1263 /* If the string is very long, split it up. */
1265 while (pos < size)
1267 int thissize = size - pos;
1268 if (thissize > maximum)
1269 thissize = maximum;
1271 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1273 pos += thissize;
1274 p += thissize;
1279 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1280 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1281 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1282 #else
1283 #if defined ASM_OUTPUT_ALIGNED_LOCAL
1284 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1285 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, DECL_ALIGN (decl))
1286 #else
1287 #define ASM_EMIT_LOCAL(decl, name, size, rounded) \
1288 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded)
1289 #endif
1290 #endif
1292 #if defined ASM_OUTPUT_ALIGNED_BSS
1293 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1294 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1295 #else
1296 #if defined ASM_OUTPUT_BSS
1297 #define ASM_EMIT_BSS(decl, name, size, rounded) \
1298 ASM_OUTPUT_BSS (asm_out_file, decl, name, size, rounded)
1299 #else
1300 #undef ASM_EMIT_BSS
1301 #endif
1302 #endif
1304 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1305 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1306 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name, size, DECL_ALIGN (decl))
1307 #else
1308 #if defined ASM_OUTPUT_ALIGNED_COMMON
1309 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1310 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size, DECL_ALIGN (decl))
1311 #else
1312 #define ASM_EMIT_COMMON(decl, name, size, rounded) \
1313 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded)
1314 #endif
1315 #endif
1317 static bool
1318 asm_emit_uninitialised (tree decl, const char *name,
1319 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1320 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1322 enum
1324 asm_dest_common,
1325 asm_dest_bss,
1326 asm_dest_local
1328 destination = asm_dest_local;
1330 /* ??? We should handle .bss via select_section mechanisms rather than
1331 via special target hooks. That would eliminate this special case. */
1332 if (TREE_PUBLIC (decl))
1334 if (!DECL_COMMON (decl))
1335 #ifdef ASM_EMIT_BSS
1336 destination = asm_dest_bss;
1337 #else
1338 return false;
1339 #endif
1340 else
1341 destination = asm_dest_common;
1344 if (destination != asm_dest_common)
1346 resolve_unique_section (decl, 0, flag_data_sections);
1347 /* Custom sections don't belong here. */
1348 if (DECL_SECTION_NAME (decl))
1349 return false;
1352 if (destination == asm_dest_bss)
1353 globalize_decl (decl);
1355 if (flag_shared_data)
1357 switch (destination)
1359 #ifdef ASM_OUTPUT_SHARED_BSS
1360 case asm_dest_bss:
1361 ASM_OUTPUT_SHARED_BSS (asm_out_file, decl, name, size, rounded);
1362 return;
1363 #endif
1364 #ifdef ASM_OUTPUT_SHARED_COMMON
1365 case asm_dest_common:
1366 ASM_OUTPUT_SHARED_COMMON (asm_out_file, name, size, rounded);
1367 return;
1368 #endif
1369 #ifdef ASM_OUTPUT_SHARED_LOCAL
1370 case asm_dest_local:
1371 ASM_OUTPUT_SHARED_LOCAL (asm_out_file, name, size, rounded);
1372 return;
1373 #endif
1374 default:
1375 break;
1379 switch (destination)
1381 #ifdef ASM_EMIT_BSS
1382 case asm_dest_bss:
1383 ASM_EMIT_BSS (decl, name, size, rounded);
1384 break;
1385 #endif
1386 case asm_dest_common:
1387 ASM_EMIT_COMMON (decl, name, size, rounded);
1388 break;
1389 case asm_dest_local:
1390 ASM_EMIT_LOCAL (decl, name, size, rounded);
1391 break;
1392 default:
1393 gcc_unreachable ();
1396 return true;
1399 /* Assemble everything that is needed for a variable or function declaration.
1400 Not used for automatic variables, and not used for function definitions.
1401 Should not be called for variables of incomplete structure type.
1403 TOP_LEVEL is nonzero if this variable has file scope.
1404 AT_END is nonzero if this is the special handling, at end of compilation,
1405 to define things that have had only tentative definitions.
1406 DONT_OUTPUT_DATA if nonzero means don't actually output the
1407 initial value (that will be done by the caller). */
1409 void
1410 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
1411 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
1413 const char *name;
1414 unsigned int align;
1415 int reloc = 0;
1416 rtx decl_rtl;
1418 if (lang_hooks.decls.prepare_assemble_variable)
1419 lang_hooks.decls.prepare_assemble_variable (decl);
1421 last_assemble_variable_decl = 0;
1423 /* Normally no need to say anything here for external references,
1424 since assemble_external is called by the language-specific code
1425 when a declaration is first seen. */
1427 if (DECL_EXTERNAL (decl))
1428 return;
1430 /* Output no assembler code for a function declaration.
1431 Only definitions of functions output anything. */
1433 if (TREE_CODE (decl) == FUNCTION_DECL)
1434 return;
1436 /* Do nothing for global register variables. */
1437 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
1439 TREE_ASM_WRITTEN (decl) = 1;
1440 return;
1443 /* If type was incomplete when the variable was declared,
1444 see if it is complete now. */
1446 if (DECL_SIZE (decl) == 0)
1447 layout_decl (decl, 0);
1449 /* Still incomplete => don't allocate it; treat the tentative defn
1450 (which is what it must have been) as an `extern' reference. */
1452 if (!dont_output_data && DECL_SIZE (decl) == 0)
1454 error ("storage size of %q+D isn%'t known", decl);
1455 TREE_ASM_WRITTEN (decl) = 1;
1456 return;
1459 /* The first declaration of a variable that comes through this function
1460 decides whether it is global (in C, has external linkage)
1461 or local (in C, has internal linkage). So do nothing more
1462 if this function has already run. */
1464 if (TREE_ASM_WRITTEN (decl))
1465 return;
1467 /* Make sure targetm.encode_section_info is invoked before we set
1468 ASM_WRITTEN. */
1469 decl_rtl = DECL_RTL (decl);
1471 TREE_ASM_WRITTEN (decl) = 1;
1473 /* Do no output if -fsyntax-only. */
1474 if (flag_syntax_only)
1475 return;
1477 app_disable ();
1479 if (! dont_output_data
1480 && ! host_integerp (DECL_SIZE_UNIT (decl), 1))
1482 error ("size of variable %q+D is too large", decl);
1483 return;
1486 name = XSTR (XEXP (decl_rtl, 0), 0);
1487 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1488 notice_global_symbol (decl);
1490 /* Compute the alignment of this data. */
1492 align = DECL_ALIGN (decl);
1494 /* In the case for initialing an array whose length isn't specified,
1495 where we have not yet been able to do the layout,
1496 figure out the proper alignment now. */
1497 if (dont_output_data && DECL_SIZE (decl) == 0
1498 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1499 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1501 /* Some object file formats have a maximum alignment which they support.
1502 In particular, a.out format supports a maximum alignment of 4. */
1503 if (align > MAX_OFILE_ALIGNMENT)
1505 warning (0, "alignment of %q+D is greater than maximum object "
1506 "file alignment. Using %d", decl,
1507 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1508 align = MAX_OFILE_ALIGNMENT;
1511 /* On some machines, it is good to increase alignment sometimes. */
1512 if (! DECL_USER_ALIGN (decl))
1514 #ifdef DATA_ALIGNMENT
1515 align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1516 #endif
1517 #ifdef CONSTANT_ALIGNMENT
1518 if (DECL_INITIAL (decl) != 0 && DECL_INITIAL (decl) != error_mark_node)
1519 align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1520 #endif
1523 /* Reset the alignment in case we have made it tighter, so we can benefit
1524 from it in get_pointer_alignment. */
1525 DECL_ALIGN (decl) = align;
1526 set_mem_align (decl_rtl, align);
1528 if (TREE_PUBLIC (decl))
1529 maybe_assemble_visibility (decl);
1531 if (DECL_PRESERVE_P (decl))
1532 targetm.asm_out.mark_decl_preserved (name);
1534 /* Handle uninitialized definitions. */
1536 /* If the decl has been given an explicit section name, then it
1537 isn't common, and shouldn't be handled as such. */
1538 if (DECL_SECTION_NAME (decl) || dont_output_data)
1540 else if (DECL_THREAD_LOCAL_P (decl))
1542 if (DECL_COMMON (decl))
1544 #ifdef ASM_OUTPUT_TLS_COMMON
1545 unsigned HOST_WIDE_INT size;
1547 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1548 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1549 return;
1550 #else
1551 sorry ("thread-local COMMON data not implemented");
1552 #endif
1555 else if (DECL_INITIAL (decl) == 0
1556 || DECL_INITIAL (decl) == error_mark_node
1557 || (flag_zero_initialized_in_bss
1558 /* Leave constant zeroes in .rodata so they can be shared. */
1559 && !TREE_READONLY (decl)
1560 && initializer_zerop (DECL_INITIAL (decl))))
1562 unsigned HOST_WIDE_INT size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
1563 unsigned HOST_WIDE_INT rounded = size;
1565 /* Don't allocate zero bytes of common,
1566 since that means "undefined external" in the linker. */
1567 if (size == 0)
1568 rounded = 1;
1570 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1571 so that each uninitialized object starts on such a boundary. */
1572 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
1573 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1574 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1576 #if !defined(ASM_OUTPUT_ALIGNED_COMMON) && !defined(ASM_OUTPUT_ALIGNED_DECL_COMMON) && !defined(ASM_OUTPUT_ALIGNED_BSS)
1577 if ((unsigned HOST_WIDE_INT) DECL_ALIGN_UNIT (decl) > rounded)
1578 warning (0, "requested alignment for %q+D is greater than "
1579 "implemented alignment of %wu", decl, rounded);
1580 #endif
1582 /* If the target cannot output uninitialized but not common global data
1583 in .bss, then we have to use .data, so fall through. */
1584 if (asm_emit_uninitialised (decl, name, size, rounded))
1585 return;
1588 /* Handle initialized definitions.
1589 Also handle uninitialized global definitions if -fno-common and the
1590 target doesn't support ASM_OUTPUT_BSS. */
1592 /* First make the assembler name(s) global if appropriate. */
1593 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
1594 globalize_decl (decl);
1596 /* Output any data that we will need to use the address of. */
1597 if (DECL_INITIAL (decl) == error_mark_node)
1598 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1599 else if (DECL_INITIAL (decl))
1601 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1602 output_addressed_constants (DECL_INITIAL (decl));
1605 /* Switch to the appropriate section. */
1606 resolve_unique_section (decl, reloc, flag_data_sections);
1607 variable_section (decl, reloc);
1609 /* dbxout.c needs to know this. */
1610 if (in_section && (in_section->common.flags & SECTION_CODE) != 0)
1611 DECL_IN_TEXT_SECTION (decl) = 1;
1613 /* Output the alignment of this data. */
1614 if (align > BITS_PER_UNIT)
1615 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (DECL_ALIGN_UNIT (decl)));
1617 /* Do any machine/system dependent processing of the object. */
1618 #ifdef ASM_DECLARE_OBJECT_NAME
1619 last_assemble_variable_decl = decl;
1620 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
1621 #else
1622 /* Standard thing is just output label for the object. */
1623 ASM_OUTPUT_LABEL (asm_out_file, name);
1624 #endif /* ASM_DECLARE_OBJECT_NAME */
1626 if (!dont_output_data)
1628 if (DECL_INITIAL (decl)
1629 && DECL_INITIAL (decl) != error_mark_node
1630 && !initializer_zerop (DECL_INITIAL (decl)))
1631 /* Output the actual data. */
1632 output_constant (DECL_INITIAL (decl),
1633 tree_low_cst (DECL_SIZE_UNIT (decl), 1),
1634 align);
1635 else
1636 /* Leave space for it. */
1637 assemble_zeros (tree_low_cst (DECL_SIZE_UNIT (decl), 1));
1641 /* Return 1 if type TYPE contains any pointers. */
1643 static int
1644 contains_pointers_p (tree type)
1646 switch (TREE_CODE (type))
1648 case POINTER_TYPE:
1649 case REFERENCE_TYPE:
1650 /* I'm not sure whether OFFSET_TYPE needs this treatment,
1651 so I'll play safe and return 1. */
1652 case OFFSET_TYPE:
1653 return 1;
1655 case RECORD_TYPE:
1656 case UNION_TYPE:
1657 case QUAL_UNION_TYPE:
1659 tree fields;
1660 /* For a type that has fields, see if the fields have pointers. */
1661 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
1662 if (TREE_CODE (fields) == FIELD_DECL
1663 && contains_pointers_p (TREE_TYPE (fields)))
1664 return 1;
1665 return 0;
1668 case ARRAY_TYPE:
1669 /* An array type contains pointers if its element type does. */
1670 return contains_pointers_p (TREE_TYPE (type));
1672 default:
1673 return 0;
1677 /* In unit-at-a-time mode, we delay assemble_external processing until
1678 the compilation unit is finalized. This is the best we can do for
1679 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
1680 it all the way to final. See PR 17982 for further discussion. */
1681 static GTY(()) tree pending_assemble_externals;
1683 #ifdef ASM_OUTPUT_EXTERNAL
1684 /* True if DECL is a function decl for which no out-of-line copy exists.
1685 It is assumed that DECL's assembler name has been set. */
1687 static bool
1688 incorporeal_function_p (tree decl)
1690 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
1692 const char *name;
1694 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
1695 && DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA)
1696 return true;
1698 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
1699 if (strncmp (name, "__builtin_", strlen ("__builtin_")) == 0)
1700 return true;
1702 return false;
1705 /* Actually do the tests to determine if this is necessary, and invoke
1706 ASM_OUTPUT_EXTERNAL. */
1707 static void
1708 assemble_external_real (tree decl)
1710 rtx rtl = DECL_RTL (decl);
1712 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
1713 && !SYMBOL_REF_USED (XEXP (rtl, 0))
1714 && !incorporeal_function_p (decl))
1716 /* Some systems do require some output. */
1717 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
1718 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
1721 #endif
1723 void
1724 process_pending_assemble_externals (void)
1726 #ifdef ASM_OUTPUT_EXTERNAL
1727 tree list;
1728 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
1729 assemble_external_real (TREE_VALUE (list));
1731 pending_assemble_externals = 0;
1732 #endif
1735 /* Output something to declare an external symbol to the assembler.
1736 (Most assemblers don't need this, so we normally output nothing.)
1737 Do nothing if DECL is not external. */
1739 void
1740 assemble_external (tree decl ATTRIBUTE_UNUSED)
1742 /* Because most platforms do not define ASM_OUTPUT_EXTERNAL, the
1743 main body of this code is only rarely exercised. To provide some
1744 testing, on all platforms, we make sure that the ASM_OUT_FILE is
1745 open. If it's not, we should not be calling this function. */
1746 gcc_assert (asm_out_file);
1748 #ifdef ASM_OUTPUT_EXTERNAL
1749 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
1750 return;
1752 if (flag_unit_at_a_time)
1753 pending_assemble_externals = tree_cons (0, decl,
1754 pending_assemble_externals);
1755 else
1756 assemble_external_real (decl);
1757 #endif
1760 /* Similar, for calling a library function FUN. */
1762 void
1763 assemble_external_libcall (rtx fun)
1765 /* Declare library function name external when first used, if nec. */
1766 if (! SYMBOL_REF_USED (fun))
1768 SYMBOL_REF_USED (fun) = 1;
1769 targetm.asm_out.external_libcall (fun);
1773 /* Assemble a label named NAME. */
1775 void
1776 assemble_label (const char *name)
1778 ASM_OUTPUT_LABEL (asm_out_file, name);
1781 /* Set the symbol_referenced flag for ID. */
1782 void
1783 mark_referenced (tree id)
1785 TREE_SYMBOL_REFERENCED (id) = 1;
1788 /* Set the symbol_referenced flag for DECL and notify callgraph. */
1789 void
1790 mark_decl_referenced (tree decl)
1792 if (TREE_CODE (decl) == FUNCTION_DECL)
1794 /* Extern inline functions don't become needed when referenced.
1795 If we know a method will be emitted in other TU and no new
1796 functions can be marked reachable, just use the external
1797 definition. */
1798 struct cgraph_node *node = cgraph_node (decl);
1799 if (!DECL_EXTERNAL (decl)
1800 && (!node->local.vtable_method || !cgraph_global_info_ready
1801 || !node->local.finalized))
1802 cgraph_mark_needed_node (node);
1804 else if (TREE_CODE (decl) == VAR_DECL)
1806 struct cgraph_varpool_node *node = cgraph_varpool_node (decl);
1807 cgraph_varpool_mark_needed_node (node);
1808 /* C++ frontend use mark_decl_references to force COMDAT variables
1809 to be output that might appear dead otherwise. */
1810 node->force_output = true;
1812 /* else do nothing - we can get various sorts of CST nodes here,
1813 which do not need to be marked. */
1817 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1818 until we find an identifier that is not itself a transparent alias.
1819 Modify the alias passed to it by reference (and all aliases on the
1820 way to the ultimate target), such that they do not have to be
1821 followed again, and return the ultimate target of the alias
1822 chain. */
1824 static inline tree
1825 ultimate_transparent_alias_target (tree *alias)
1827 tree target = *alias;
1829 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1831 gcc_assert (TREE_CHAIN (target));
1832 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1833 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1834 && ! TREE_CHAIN (target));
1835 *alias = target;
1838 return target;
1841 /* Output to FILE (an assembly file) a reference to NAME. If NAME
1842 starts with a *, the rest of NAME is output verbatim. Otherwise
1843 NAME is transformed in a target-specific way (usually by the
1844 addition of an underscore). */
1846 void
1847 assemble_name_raw (FILE *file, const char *name)
1849 if (name[0] == '*')
1850 fputs (&name[1], file);
1851 else
1852 ASM_OUTPUT_LABELREF (file, name);
1855 /* Like assemble_name_raw, but should be used when NAME might refer to
1856 an entity that is also represented as a tree (like a function or
1857 variable). If NAME does refer to such an entity, that entity will
1858 be marked as referenced. */
1860 void
1861 assemble_name (FILE *file, const char *name)
1863 const char *real_name;
1864 tree id;
1866 real_name = targetm.strip_name_encoding (name);
1868 id = maybe_get_identifier (real_name);
1869 if (id)
1871 tree id_orig = id;
1873 mark_referenced (id);
1874 ultimate_transparent_alias_target (&id);
1875 if (id != id_orig)
1876 name = IDENTIFIER_POINTER (id);
1877 gcc_assert (! TREE_CHAIN (id));
1880 assemble_name_raw (file, name);
1883 /* Allocate SIZE bytes writable static space with a gensym name
1884 and return an RTX to refer to its address. */
1887 assemble_static_space (unsigned HOST_WIDE_INT size)
1889 char name[12];
1890 const char *namestring;
1891 rtx x;
1893 #if 0
1894 if (flag_shared_data)
1895 switch_to_section (data_section);
1896 #endif
1898 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
1899 ++const_labelno;
1900 namestring = ggc_strdup (name);
1902 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
1903 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
1905 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
1906 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
1907 BIGGEST_ALIGNMENT);
1908 #else
1909 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
1910 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
1911 #else
1913 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
1914 so that each uninitialized object starts on such a boundary. */
1915 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
1916 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
1917 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
1918 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
1919 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
1920 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1922 #endif
1923 #endif
1924 return x;
1927 /* Assemble the static constant template for function entry trampolines.
1928 This is done at most once per compilation.
1929 Returns an RTX for the address of the template. */
1931 static GTY(()) rtx initial_trampoline;
1933 #ifdef TRAMPOLINE_TEMPLATE
1935 assemble_trampoline_template (void)
1937 char label[256];
1938 const char *name;
1939 int align;
1940 rtx symbol;
1942 if (initial_trampoline)
1943 return initial_trampoline;
1945 /* By default, put trampoline templates in read-only data section. */
1947 #ifdef TRAMPOLINE_SECTION
1948 switch_to_section (TRAMPOLINE_SECTION);
1949 #else
1950 switch_to_section (readonly_data_section);
1951 #endif
1953 /* Write the assembler code to define one. */
1954 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
1955 if (align > 0)
1957 ASM_OUTPUT_ALIGN (asm_out_file, align);
1960 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
1961 TRAMPOLINE_TEMPLATE (asm_out_file);
1963 /* Record the rtl to refer to it. */
1964 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
1965 name = ggc_strdup (label);
1966 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
1967 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
1969 initial_trampoline = gen_rtx_MEM (BLKmode, symbol);
1970 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
1972 return initial_trampoline;
1974 #endif
1976 /* A and B are either alignments or offsets. Return the minimum alignment
1977 that may be assumed after adding the two together. */
1979 static inline unsigned
1980 min_align (unsigned int a, unsigned int b)
1982 return (a | b) & -(a | b);
1985 /* Return the assembler directive for creating a given kind of integer
1986 object. SIZE is the number of bytes in the object and ALIGNED_P
1987 indicates whether it is known to be aligned. Return NULL if the
1988 assembly dialect has no such directive.
1990 The returned string should be printed at the start of a new line and
1991 be followed immediately by the object's initial value. */
1993 const char *
1994 integer_asm_op (int size, int aligned_p)
1996 struct asm_int_op *ops;
1998 if (aligned_p)
1999 ops = &targetm.asm_out.aligned_op;
2000 else
2001 ops = &targetm.asm_out.unaligned_op;
2003 switch (size)
2005 case 1:
2006 return targetm.asm_out.byte_op;
2007 case 2:
2008 return ops->hi;
2009 case 4:
2010 return ops->si;
2011 case 8:
2012 return ops->di;
2013 case 16:
2014 return ops->ti;
2015 default:
2016 return NULL;
2020 /* Use directive OP to assemble an integer object X. Print OP at the
2021 start of the line, followed immediately by the value of X. */
2023 void
2024 assemble_integer_with_op (const char *op, rtx x)
2026 fputs (op, asm_out_file);
2027 output_addr_const (asm_out_file, x);
2028 fputc ('\n', asm_out_file);
2031 /* The default implementation of the asm_out.integer target hook. */
2033 bool
2034 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2035 unsigned int size ATTRIBUTE_UNUSED,
2036 int aligned_p ATTRIBUTE_UNUSED)
2038 const char *op = integer_asm_op (size, aligned_p);
2039 /* Avoid GAS bugs for large values. Specifically negative values whose
2040 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2041 if (size > UNITS_PER_WORD && size > POINTER_SIZE / BITS_PER_UNIT)
2042 return false;
2043 return op && (assemble_integer_with_op (op, x), true);
2046 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2047 the alignment of the integer in bits. Return 1 if we were able to output
2048 the constant, otherwise 0. We must be able to output the constant,
2049 if FORCE is nonzero. */
2051 bool
2052 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2054 int aligned_p;
2056 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2058 /* See if the target hook can handle this kind of object. */
2059 if (targetm.asm_out.integer (x, size, aligned_p))
2060 return true;
2062 /* If the object is a multi-byte one, try splitting it up. Split
2063 it into words it if is multi-word, otherwise split it into bytes. */
2064 if (size > 1)
2066 enum machine_mode omode, imode;
2067 unsigned int subalign;
2068 unsigned int subsize, i;
2070 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2071 subalign = MIN (align, subsize * BITS_PER_UNIT);
2072 omode = mode_for_size (subsize * BITS_PER_UNIT, MODE_INT, 0);
2073 imode = mode_for_size (size * BITS_PER_UNIT, MODE_INT, 0);
2075 for (i = 0; i < size; i += subsize)
2077 rtx partial = simplify_subreg (omode, x, imode, i);
2078 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2079 break;
2081 if (i == size)
2082 return true;
2084 /* If we've printed some of it, but not all of it, there's no going
2085 back now. */
2086 gcc_assert (!i);
2089 gcc_assert (!force);
2091 return false;
2094 void
2095 assemble_real (REAL_VALUE_TYPE d, enum machine_mode mode, unsigned int align)
2097 long data[4] = {0, 0, 0, 0};
2098 int i;
2099 int bitsize, nelts, nunits, units_per;
2101 /* This is hairy. We have a quantity of known size. real_to_target
2102 will put it into an array of *host* longs, 32 bits per element
2103 (even if long is more than 32 bits). We need to determine the
2104 number of array elements that are occupied (nelts) and the number
2105 of *target* min-addressable units that will be occupied in the
2106 object file (nunits). We cannot assume that 32 divides the
2107 mode's bitsize (size * BITS_PER_UNIT) evenly.
2109 size * BITS_PER_UNIT is used here to make sure that padding bits
2110 (which might appear at either end of the value; real_to_target
2111 will include the padding bits in its output array) are included. */
2113 nunits = GET_MODE_SIZE (mode);
2114 bitsize = nunits * BITS_PER_UNIT;
2115 nelts = CEIL (bitsize, 32);
2116 units_per = 32 / BITS_PER_UNIT;
2118 real_to_target (data, &d, mode);
2120 /* Put out the first word with the specified alignment. */
2121 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2122 nunits -= units_per;
2124 /* Subsequent words need only 32-bit alignment. */
2125 align = min_align (align, 32);
2127 for (i = 1; i < nelts; i++)
2129 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2130 nunits -= units_per;
2134 /* Given an expression EXP with a constant value,
2135 reduce it to the sum of an assembler symbol and an integer.
2136 Store them both in the structure *VALUE.
2137 EXP must be reducible. */
2139 struct addr_const GTY(())
2141 rtx base;
2142 HOST_WIDE_INT offset;
2145 static void
2146 decode_addr_const (tree exp, struct addr_const *value)
2148 tree target = TREE_OPERAND (exp, 0);
2149 int offset = 0;
2150 rtx x;
2152 while (1)
2154 if (TREE_CODE (target) == COMPONENT_REF
2155 && host_integerp (byte_position (TREE_OPERAND (target, 1)), 0))
2158 offset += int_byte_position (TREE_OPERAND (target, 1));
2159 target = TREE_OPERAND (target, 0);
2161 else if (TREE_CODE (target) == ARRAY_REF
2162 || TREE_CODE (target) == ARRAY_RANGE_REF)
2164 offset += (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (target)), 1)
2165 * tree_low_cst (TREE_OPERAND (target, 1), 0));
2166 target = TREE_OPERAND (target, 0);
2168 else
2169 break;
2172 switch (TREE_CODE (target))
2174 case VAR_DECL:
2175 case FUNCTION_DECL:
2176 x = DECL_RTL (target);
2177 break;
2179 case LABEL_DECL:
2180 x = gen_rtx_MEM (FUNCTION_MODE,
2181 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2182 break;
2184 case REAL_CST:
2185 case STRING_CST:
2186 case COMPLEX_CST:
2187 case CONSTRUCTOR:
2188 case INTEGER_CST:
2189 x = output_constant_def (target, 1);
2190 break;
2192 default:
2193 gcc_unreachable ();
2196 gcc_assert (MEM_P (x));
2197 x = XEXP (x, 0);
2199 value->base = x;
2200 value->offset = offset;
2203 /* Uniquize all constants that appear in memory.
2204 Each constant in memory thus far output is recorded
2205 in `const_desc_table'. */
2207 struct constant_descriptor_tree GTY(())
2209 /* A MEM for the constant. */
2210 rtx rtl;
2212 /* The value of the constant. */
2213 tree value;
2215 /* Hash of value. Computing the hash from value each time
2216 hashfn is called can't work properly, as that means recursive
2217 use of the hash table during hash table expansion. */
2218 hashval_t hash;
2221 static GTY((param_is (struct constant_descriptor_tree)))
2222 htab_t const_desc_htab;
2224 static struct constant_descriptor_tree * build_constant_desc (tree);
2225 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2227 /* Compute a hash code for a constant expression. */
2229 static hashval_t
2230 const_desc_hash (const void *ptr)
2232 return ((struct constant_descriptor_tree *)ptr)->hash;
2235 static hashval_t
2236 const_hash_1 (const tree exp)
2238 const char *p;
2239 hashval_t hi;
2240 int len, i;
2241 enum tree_code code = TREE_CODE (exp);
2243 /* Either set P and LEN to the address and len of something to hash and
2244 exit the switch or return a value. */
2246 switch (code)
2248 case INTEGER_CST:
2249 p = (char *) &TREE_INT_CST (exp);
2250 len = sizeof TREE_INT_CST (exp);
2251 break;
2253 case REAL_CST:
2254 return real_hash (TREE_REAL_CST_PTR (exp));
2256 case STRING_CST:
2257 p = TREE_STRING_POINTER (exp);
2258 len = TREE_STRING_LENGTH (exp);
2259 break;
2261 case COMPLEX_CST:
2262 return (const_hash_1 (TREE_REALPART (exp)) * 5
2263 + const_hash_1 (TREE_IMAGPART (exp)));
2265 case CONSTRUCTOR:
2267 unsigned HOST_WIDE_INT idx;
2268 tree value;
2270 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2272 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2273 if (value)
2274 hi = hi * 603 + const_hash_1 (value);
2276 return hi;
2279 case ADDR_EXPR:
2280 case FDESC_EXPR:
2282 struct addr_const value;
2284 decode_addr_const (exp, &value);
2285 switch (GET_CODE (value.base))
2287 case SYMBOL_REF:
2288 /* Don't hash the address of the SYMBOL_REF;
2289 only use the offset and the symbol name. */
2290 hi = value.offset;
2291 p = XSTR (value.base, 0);
2292 for (i = 0; p[i] != 0; i++)
2293 hi = ((hi * 613) + (unsigned) (p[i]));
2294 break;
2296 case LABEL_REF:
2297 hi = value.offset + CODE_LABEL_NUMBER (XEXP (value.base, 0)) * 13;
2298 break;
2300 default:
2301 gcc_unreachable ();
2304 return hi;
2306 case PLUS_EXPR:
2307 case MINUS_EXPR:
2308 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2309 + const_hash_1 (TREE_OPERAND (exp, 1)));
2311 case NOP_EXPR:
2312 case CONVERT_EXPR:
2313 case NON_LVALUE_EXPR:
2314 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2316 default:
2317 /* A language specific constant. Just hash the code. */
2318 return code;
2321 /* Compute hashing function. */
2322 hi = len;
2323 for (i = 0; i < len; i++)
2324 hi = ((hi * 613) + (unsigned) (p[i]));
2326 return hi;
2329 /* Wrapper of compare_constant, for the htab interface. */
2330 static int
2331 const_desc_eq (const void *p1, const void *p2)
2333 const struct constant_descriptor_tree *c1 = p1;
2334 const struct constant_descriptor_tree *c2 = p2;
2335 if (c1->hash != c2->hash)
2336 return 0;
2337 return compare_constant (c1->value, c2->value);
2340 /* Compare t1 and t2, and return 1 only if they are known to result in
2341 the same bit pattern on output. */
2343 static int
2344 compare_constant (const tree t1, const tree t2)
2346 enum tree_code typecode;
2348 if (t1 == NULL_TREE)
2349 return t2 == NULL_TREE;
2350 if (t2 == NULL_TREE)
2351 return 0;
2353 if (TREE_CODE (t1) != TREE_CODE (t2))
2354 return 0;
2356 switch (TREE_CODE (t1))
2358 case INTEGER_CST:
2359 /* Integer constants are the same only if the same width of type. */
2360 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2361 return 0;
2362 return tree_int_cst_equal (t1, t2);
2364 case REAL_CST:
2365 /* Real constants are the same only if the same width of type. */
2366 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
2367 return 0;
2369 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
2371 case STRING_CST:
2372 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
2373 return 0;
2375 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
2376 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
2377 TREE_STRING_LENGTH (t1)));
2379 case COMPLEX_CST:
2380 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
2381 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
2383 case CONSTRUCTOR:
2385 VEC(constructor_elt, gc) *v1, *v2;
2386 unsigned HOST_WIDE_INT idx;
2388 typecode = TREE_CODE (TREE_TYPE (t1));
2389 if (typecode != TREE_CODE (TREE_TYPE (t2)))
2390 return 0;
2392 if (typecode == ARRAY_TYPE)
2394 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
2395 /* For arrays, check that the sizes all match. */
2396 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
2397 || size_1 == -1
2398 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
2399 return 0;
2401 else
2403 /* For record and union constructors, require exact type
2404 equality. */
2405 if (TREE_TYPE (t1) != TREE_TYPE (t2))
2406 return 0;
2409 v1 = CONSTRUCTOR_ELTS (t1);
2410 v2 = CONSTRUCTOR_ELTS (t2);
2411 if (VEC_length (constructor_elt, v1)
2412 != VEC_length (constructor_elt, v2))
2413 return 0;
2415 for (idx = 0; idx < VEC_length (constructor_elt, v1); ++idx)
2417 constructor_elt *c1 = VEC_index (constructor_elt, v1, idx);
2418 constructor_elt *c2 = VEC_index (constructor_elt, v2, idx);
2420 /* Check that each value is the same... */
2421 if (!compare_constant (c1->value, c2->value))
2422 return 0;
2423 /* ... and that they apply to the same fields! */
2424 if (typecode == ARRAY_TYPE)
2426 if (!compare_constant (c1->index, c2->index))
2427 return 0;
2429 else
2431 if (c1->index != c2->index)
2432 return 0;
2436 return 1;
2439 case ADDR_EXPR:
2440 case FDESC_EXPR:
2442 struct addr_const value1, value2;
2444 decode_addr_const (t1, &value1);
2445 decode_addr_const (t2, &value2);
2446 return (value1.offset == value2.offset
2447 && strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
2450 case PLUS_EXPR:
2451 case MINUS_EXPR:
2452 case RANGE_EXPR:
2453 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
2454 && compare_constant(TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
2456 case NOP_EXPR:
2457 case CONVERT_EXPR:
2458 case NON_LVALUE_EXPR:
2459 case VIEW_CONVERT_EXPR:
2460 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
2462 default:
2464 tree nt1, nt2;
2465 nt1 = lang_hooks.expand_constant (t1);
2466 nt2 = lang_hooks.expand_constant (t2);
2467 if (nt1 != t1 || nt2 != t2)
2468 return compare_constant (nt1, nt2);
2469 else
2470 return 0;
2474 gcc_unreachable ();
2477 /* Make a copy of the whole tree structure for a constant. This
2478 handles the same types of nodes that compare_constant handles. */
2480 static tree
2481 copy_constant (tree exp)
2483 switch (TREE_CODE (exp))
2485 case ADDR_EXPR:
2486 /* For ADDR_EXPR, we do not want to copy the decl whose address
2487 is requested. We do want to copy constants though. */
2488 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
2489 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2490 copy_constant (TREE_OPERAND (exp, 0)));
2491 else
2492 return copy_node (exp);
2494 case INTEGER_CST:
2495 case REAL_CST:
2496 case STRING_CST:
2497 return copy_node (exp);
2499 case COMPLEX_CST:
2500 return build_complex (TREE_TYPE (exp),
2501 copy_constant (TREE_REALPART (exp)),
2502 copy_constant (TREE_IMAGPART (exp)));
2504 case PLUS_EXPR:
2505 case MINUS_EXPR:
2506 return build2 (TREE_CODE (exp), TREE_TYPE (exp),
2507 copy_constant (TREE_OPERAND (exp, 0)),
2508 copy_constant (TREE_OPERAND (exp, 1)));
2510 case NOP_EXPR:
2511 case CONVERT_EXPR:
2512 case NON_LVALUE_EXPR:
2513 case VIEW_CONVERT_EXPR:
2514 return build1 (TREE_CODE (exp), TREE_TYPE (exp),
2515 copy_constant (TREE_OPERAND (exp, 0)));
2517 case CONSTRUCTOR:
2519 tree copy = copy_node (exp);
2520 VEC(constructor_elt, gc) *v;
2521 unsigned HOST_WIDE_INT idx;
2522 tree purpose, value;
2524 v = VEC_alloc(constructor_elt, gc, VEC_length(constructor_elt,
2525 CONSTRUCTOR_ELTS (exp)));
2526 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, purpose, value)
2528 constructor_elt *ce = VEC_quick_push (constructor_elt, v, NULL);
2529 ce->index = purpose;
2530 ce->value = copy_constant (value);
2532 CONSTRUCTOR_ELTS (copy) = v;
2533 return copy;
2536 default:
2538 tree t = lang_hooks.expand_constant (exp);
2540 gcc_assert (t == exp);
2541 return copy_constant (t);
2546 /* Subroutine of output_constant_def:
2547 No constant equal to EXP is known to have been output.
2548 Make a constant descriptor to enter EXP in the hash table.
2549 Assign the label number and construct RTL to refer to the
2550 constant's location in memory.
2551 Caller is responsible for updating the hash table. */
2553 static struct constant_descriptor_tree *
2554 build_constant_desc (tree exp)
2556 rtx symbol;
2557 rtx rtl;
2558 char label[256];
2559 int labelno;
2560 struct constant_descriptor_tree *desc;
2562 desc = ggc_alloc (sizeof (*desc));
2563 desc->value = copy_constant (exp);
2565 /* Propagate marked-ness to copied constant. */
2566 if (flag_mudflap && mf_marked_p (exp))
2567 mf_mark (desc->value);
2569 /* Create a string containing the label name, in LABEL. */
2570 labelno = const_labelno++;
2571 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
2573 /* We have a symbol name; construct the SYMBOL_REF and the MEM. */
2574 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2575 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2576 SET_SYMBOL_REF_DECL (symbol, desc->value);
2577 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2579 rtl = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (exp)), symbol);
2580 set_mem_attributes (rtl, exp, 1);
2581 set_mem_alias_set (rtl, 0);
2582 set_mem_alias_set (rtl, const_alias_set);
2584 /* Set flags or add text to the name to record information, such as
2585 that it is a local symbol. If the name is changed, the macro
2586 ASM_OUTPUT_LABELREF will have to know how to strip this
2587 information. This call might invalidate our local variable
2588 SYMBOL; we can't use it afterward. */
2590 targetm.encode_section_info (exp, rtl, true);
2592 desc->rtl = rtl;
2594 return desc;
2597 /* Return an rtx representing a reference to constant data in memory
2598 for the constant expression EXP.
2600 If assembler code for such a constant has already been output,
2601 return an rtx to refer to it.
2602 Otherwise, output such a constant in memory
2603 and generate an rtx for it.
2605 If DEFER is nonzero, this constant can be deferred and output only
2606 if referenced in the function after all optimizations.
2608 `const_desc_table' records which constants already have label strings. */
2611 output_constant_def (tree exp, int defer)
2613 struct constant_descriptor_tree *desc;
2614 struct constant_descriptor_tree key;
2615 void **loc;
2617 /* Look up EXP in the table of constant descriptors. If we didn't find
2618 it, create a new one. */
2619 key.value = exp;
2620 key.hash = const_hash_1 (exp);
2621 loc = htab_find_slot_with_hash (const_desc_htab, &key, key.hash, INSERT);
2623 desc = *loc;
2624 if (desc == 0)
2626 desc = build_constant_desc (exp);
2627 desc->hash = key.hash;
2628 *loc = desc;
2631 maybe_output_constant_def_contents (desc, defer);
2632 return desc->rtl;
2635 /* Subroutine of output_constant_def: Decide whether or not we need to
2636 output the constant DESC now, and if so, do it. */
2637 static void
2638 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
2639 int defer)
2641 rtx symbol = XEXP (desc->rtl, 0);
2642 tree exp = desc->value;
2644 if (flag_syntax_only)
2645 return;
2647 if (TREE_ASM_WRITTEN (exp))
2648 /* Already output; don't do it again. */
2649 return;
2651 /* We can always defer constants as long as the context allows
2652 doing so. */
2653 if (defer)
2655 /* Increment n_deferred_constants if it exists. It needs to be at
2656 least as large as the number of constants actually referred to
2657 by the function. If it's too small we'll stop looking too early
2658 and fail to emit constants; if it's too large we'll only look
2659 through the entire function when we could have stopped earlier. */
2660 if (cfun)
2661 n_deferred_constants++;
2662 return;
2665 output_constant_def_contents (symbol);
2668 /* We must output the constant data referred to by SYMBOL; do so. */
2670 static void
2671 output_constant_def_contents (rtx symbol)
2673 tree exp = SYMBOL_REF_DECL (symbol);
2674 const char *label = XSTR (symbol, 0);
2675 HOST_WIDE_INT size;
2677 /* Make sure any other constants whose addresses appear in EXP
2678 are assigned label numbers. */
2679 int reloc = compute_reloc_for_constant (exp);
2681 /* Align the location counter as required by EXP's data type. */
2682 unsigned int align = TYPE_ALIGN (TREE_TYPE (exp));
2683 #ifdef CONSTANT_ALIGNMENT
2684 align = CONSTANT_ALIGNMENT (exp, align);
2685 #endif
2687 output_addressed_constants (exp);
2689 /* We are no longer deferring this constant. */
2690 TREE_ASM_WRITTEN (exp) = 1;
2692 if (IN_NAMED_SECTION (exp))
2693 switch_to_section (get_named_section (exp, NULL, reloc));
2694 else
2695 switch_to_section (targetm.asm_out.select_section (exp, reloc, align));
2697 if (align > BITS_PER_UNIT)
2699 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2702 size = int_size_in_bytes (TREE_TYPE (exp));
2703 if (TREE_CODE (exp) == STRING_CST)
2704 size = MAX (TREE_STRING_LENGTH (exp), size);
2706 /* Do any machine/system dependent processing of the constant. */
2707 #ifdef ASM_DECLARE_CONSTANT_NAME
2708 ASM_DECLARE_CONSTANT_NAME (asm_out_file, label, exp, size);
2709 #else
2710 /* Standard thing is just output label for the constant. */
2711 ASM_OUTPUT_LABEL (asm_out_file, label);
2712 #endif /* ASM_DECLARE_CONSTANT_NAME */
2714 /* Output the value of EXP. */
2715 output_constant (exp, size, align);
2716 if (flag_mudflap)
2717 mudflap_enqueue_constant (exp);
2720 /* Look up EXP in the table of constant descriptors. Return the rtl
2721 if it has been emitted, else null. */
2724 lookup_constant_def (tree exp)
2726 struct constant_descriptor_tree *desc;
2727 struct constant_descriptor_tree key;
2729 key.value = exp;
2730 key.hash = const_hash_1 (exp);
2731 desc = htab_find_with_hash (const_desc_htab, &key, key.hash);
2733 return (desc ? desc->rtl : NULL_RTX);
2736 /* Used in the hash tables to avoid outputting the same constant
2737 twice. Unlike 'struct constant_descriptor_tree', RTX constants
2738 are output once per function, not once per file. */
2739 /* ??? Only a few targets need per-function constant pools. Most
2740 can use one per-file pool. Should add a targetm bit to tell the
2741 difference. */
2743 struct rtx_constant_pool GTY(())
2745 /* Pointers to first and last constant in pool, as ordered by offset. */
2746 struct constant_descriptor_rtx *first;
2747 struct constant_descriptor_rtx *last;
2749 /* Hash facility for making memory-constants from constant rtl-expressions.
2750 It is used on RISC machines where immediate integer arguments and
2751 constant addresses are restricted so that such constants must be stored
2752 in memory. */
2753 htab_t GTY((param_is (struct constant_descriptor_rtx))) const_rtx_htab;
2755 /* Current offset in constant pool (does not include any
2756 machine-specific header). */
2757 HOST_WIDE_INT offset;
2760 struct constant_descriptor_rtx GTY((chain_next ("%h.next")))
2762 struct constant_descriptor_rtx *next;
2763 rtx mem;
2764 rtx sym;
2765 rtx constant;
2766 HOST_WIDE_INT offset;
2767 hashval_t hash;
2768 enum machine_mode mode;
2769 unsigned int align;
2770 int labelno;
2771 int mark;
2774 /* Hash and compare functions for const_rtx_htab. */
2776 static hashval_t
2777 const_desc_rtx_hash (const void *ptr)
2779 const struct constant_descriptor_rtx *desc = ptr;
2780 return desc->hash;
2783 static int
2784 const_desc_rtx_eq (const void *a, const void *b)
2786 const struct constant_descriptor_rtx *x = a;
2787 const struct constant_descriptor_rtx *y = b;
2789 if (x->mode != y->mode)
2790 return 0;
2791 return rtx_equal_p (x->constant, y->constant);
2794 /* This is the worker function for const_rtx_hash, called via for_each_rtx. */
2796 static int
2797 const_rtx_hash_1 (rtx *xp, void *data)
2799 unsigned HOST_WIDE_INT hwi;
2800 enum machine_mode mode;
2801 enum rtx_code code;
2802 hashval_t h, *hp;
2803 rtx x;
2805 x = *xp;
2806 code = GET_CODE (x);
2807 mode = GET_MODE (x);
2808 h = (hashval_t) code * 1048573 + mode;
2810 switch (code)
2812 case CONST_INT:
2813 hwi = INTVAL (x);
2814 fold_hwi:
2816 const int shift = sizeof (hashval_t) * CHAR_BIT;
2817 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
2818 int i;
2820 h ^= (hashval_t) hwi;
2821 for (i = 1; i < n; ++i)
2823 hwi >>= shift;
2824 h ^= (hashval_t) hwi;
2827 break;
2829 case CONST_DOUBLE:
2830 if (mode == VOIDmode)
2832 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
2833 goto fold_hwi;
2835 else
2836 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
2837 break;
2839 case CONST_VECTOR:
2841 int i;
2842 for (i = XVECLEN (x, 0); i-- > 0; )
2843 h = h * 251 + const_rtx_hash_1 (&XVECEXP (x, 0, i), data);
2845 break;
2847 case SYMBOL_REF:
2848 h ^= htab_hash_string (XSTR (x, 0));
2849 break;
2851 case LABEL_REF:
2852 h = h * 251 + CODE_LABEL_NUMBER (XEXP (x, 0));
2853 break;
2855 case UNSPEC:
2856 case UNSPEC_VOLATILE:
2857 h = h * 251 + XINT (x, 1);
2858 break;
2860 default:
2861 break;
2864 hp = data;
2865 *hp = *hp * 509 + h;
2866 return 0;
2869 /* Compute a hash value for X, which should be a constant. */
2871 static hashval_t
2872 const_rtx_hash (rtx x)
2874 hashval_t h = 0;
2875 for_each_rtx (&x, const_rtx_hash_1, &h);
2876 return h;
2880 /* Initialize constant pool hashing for a new function. */
2882 void
2883 init_varasm_status (struct function *f)
2885 struct varasm_status *p;
2886 struct rtx_constant_pool *pool;
2888 p = ggc_alloc (sizeof (struct varasm_status));
2889 f->varasm = p;
2891 pool = ggc_alloc (sizeof (struct rtx_constant_pool));
2892 p->pool = pool;
2893 p->deferred_constants = 0;
2895 pool->const_rtx_htab = htab_create_ggc (31, const_desc_rtx_hash,
2896 const_desc_rtx_eq, NULL);
2897 pool->first = pool->last = NULL;
2898 pool->offset = 0;
2901 /* Given a MINUS expression, simplify it if both sides
2902 include the same symbol. */
2905 simplify_subtraction (rtx x)
2907 rtx r = simplify_rtx (x);
2908 return r ? r : x;
2911 /* Given a constant rtx X, make (or find) a memory constant for its value
2912 and return a MEM rtx to refer to it in memory. */
2915 force_const_mem (enum machine_mode mode, rtx x)
2917 struct constant_descriptor_rtx *desc, tmp;
2918 struct rtx_constant_pool *pool = cfun->varasm->pool;
2919 char label[256];
2920 rtx def, symbol;
2921 hashval_t hash;
2922 unsigned int align;
2923 void **slot;
2925 /* If we're not allowed to drop X into the constant pool, don't. */
2926 if (targetm.cannot_force_const_mem (x))
2927 return NULL_RTX;
2929 /* Lookup the value in the hashtable. */
2930 tmp.constant = x;
2931 tmp.mode = mode;
2932 hash = const_rtx_hash (x);
2933 slot = htab_find_slot_with_hash (pool->const_rtx_htab, &tmp, hash, INSERT);
2934 desc = *slot;
2936 /* If the constant was already present, return its memory. */
2937 if (desc)
2938 return copy_rtx (desc->mem);
2940 /* Otherwise, create a new descriptor. */
2941 desc = ggc_alloc (sizeof (*desc));
2942 *slot = desc;
2944 /* Align the location counter as required by EXP's data type. */
2945 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
2946 #ifdef CONSTANT_ALIGNMENT
2948 tree type = lang_hooks.types.type_for_mode (mode, 0);
2949 if (type != NULL_TREE)
2950 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
2952 #endif
2954 pool->offset += (align / BITS_PER_UNIT) - 1;
2955 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
2957 desc->next = NULL;
2958 desc->constant = tmp.constant;
2959 desc->offset = pool->offset;
2960 desc->hash = hash;
2961 desc->mode = mode;
2962 desc->align = align;
2963 desc->labelno = const_labelno;
2964 desc->mark = 0;
2966 pool->offset += GET_MODE_SIZE (mode);
2967 if (pool->last)
2968 pool->last->next = desc;
2969 else
2970 pool->first = pool->last = desc;
2971 pool->last = desc;
2973 /* Create a string containing the label name, in LABEL. */
2974 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
2975 ++const_labelno;
2977 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
2978 the constants pool. */
2979 desc->sym = symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
2980 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2981 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
2982 SET_SYMBOL_REF_CONSTANT (symbol, desc);
2983 current_function_uses_const_pool = 1;
2985 /* Construct the MEM. */
2986 desc->mem = def = gen_const_mem (mode, symbol);
2987 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
2988 set_mem_align (def, align);
2990 /* If we're dropping a label to the constant pool, make sure we
2991 don't delete it. */
2992 if (GET_CODE (x) == LABEL_REF)
2993 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
2995 return copy_rtx (def);
2998 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3001 get_pool_constant (rtx addr)
3003 return SYMBOL_REF_CONSTANT (addr)->constant;
3006 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3007 and whether it has been output or not. */
3010 get_pool_constant_mark (rtx addr, bool *pmarked)
3012 struct constant_descriptor_rtx *desc;
3014 desc = SYMBOL_REF_CONSTANT (addr);
3015 *pmarked = (desc->mark != 0);
3016 return desc->constant;
3019 /* Similar, return the mode. */
3021 enum machine_mode
3022 get_pool_mode (rtx addr)
3024 return SYMBOL_REF_CONSTANT (addr)->mode;
3027 /* Return the size of the constant pool. */
3030 get_pool_size (void)
3032 return cfun->varasm->pool->offset;
3035 /* Worker function for output_constant_pool_1. Emit assembly for X
3036 in MODE with known alignment ALIGN. */
3038 static void
3039 output_constant_pool_2 (enum machine_mode mode, rtx x, unsigned int align)
3041 switch (GET_MODE_CLASS (mode))
3043 case MODE_FLOAT:
3044 case MODE_DECIMAL_FLOAT:
3046 REAL_VALUE_TYPE r;
3048 gcc_assert (GET_CODE (x) == CONST_DOUBLE);
3049 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3050 assemble_real (r, mode, align);
3051 break;
3054 case MODE_INT:
3055 case MODE_PARTIAL_INT:
3056 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3057 break;
3059 case MODE_VECTOR_FLOAT:
3060 case MODE_VECTOR_INT:
3062 int i, units;
3063 enum machine_mode submode = GET_MODE_INNER (mode);
3064 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3066 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3067 units = CONST_VECTOR_NUNITS (x);
3069 for (i = 0; i < units; i++)
3071 rtx elt = CONST_VECTOR_ELT (x, i);
3072 output_constant_pool_2 (submode, elt, i ? subalign : align);
3075 break;
3077 default:
3078 gcc_unreachable ();
3082 /* Worker function for output_constant_pool. Emit POOL. */
3084 static void
3085 output_constant_pool_1 (struct constant_descriptor_rtx *desc)
3087 rtx x, tmp;
3089 if (!desc->mark)
3090 return;
3091 x = desc->constant;
3093 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3094 whose CODE_LABEL has been deleted. This can occur if a jump table
3095 is eliminated by optimization. If so, write a constant of zero
3096 instead. Note that this can also happen by turning the
3097 CODE_LABEL into a NOTE. */
3098 /* ??? This seems completely and utterly wrong. Certainly it's
3099 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3100 functioning even with INSN_DELETED_P and friends. */
3102 tmp = x;
3103 switch (GET_CODE (x))
3105 case CONST:
3106 if (GET_CODE (XEXP (x, 0)) != PLUS
3107 || GET_CODE (XEXP (XEXP (x, 0), 0)) != LABEL_REF)
3108 break;
3109 tmp = XEXP (XEXP (x, 0), 0);
3110 /* FALLTHRU */
3112 case LABEL_REF:
3113 tmp = XEXP (x, 0);
3114 gcc_assert (!INSN_DELETED_P (tmp));
3115 gcc_assert (!NOTE_P (tmp)
3116 || NOTE_LINE_NUMBER (tmp) != NOTE_INSN_DELETED);
3117 break;
3119 default:
3120 break;
3123 /* First switch to correct section. */
3124 switch_to_section (targetm.asm_out.select_rtx_section (desc->mode, x,
3125 desc->align));
3127 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3128 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3129 desc->align, desc->labelno, done);
3130 #endif
3132 assemble_align (desc->align);
3134 /* Output the label. */
3135 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3137 /* Output the data. */
3138 output_constant_pool_2 (desc->mode, x, desc->align);
3140 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3141 sections have proper size. */
3142 if (desc->align > GET_MODE_BITSIZE (desc->mode)
3143 && in_section
3144 && (in_section->common.flags & SECTION_MERGE))
3145 assemble_align (desc->align);
3147 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3148 done:
3149 #endif
3150 return;
3153 /* Given a SYMBOL_REF CURRENT_RTX, mark it and all constants it refers
3154 to as used. Emit referenced deferred strings. This function can
3155 be used with for_each_rtx to mark all SYMBOL_REFs in an rtx. */
3157 static int
3158 mark_constant (rtx *current_rtx, void *data)
3160 struct rtx_constant_pool *pool = data;
3161 rtx x = *current_rtx;
3163 if (x == NULL_RTX || GET_CODE (x) != SYMBOL_REF)
3164 return 0;
3166 if (CONSTANT_POOL_ADDRESS_P (x))
3168 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3169 if (desc->mark == 0)
3171 desc->mark = 1;
3172 for_each_rtx (&desc->constant, mark_constant, pool);
3175 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3177 tree exp = SYMBOL_REF_DECL (x);
3178 if (!TREE_ASM_WRITTEN (exp))
3180 n_deferred_constants--;
3181 output_constant_def_contents (x);
3185 return -1;
3188 /* Look through appropriate parts of INSN, marking all entries in the
3189 constant pool which are actually being used. Entries that are only
3190 referenced by other constants are also marked as used. Emit
3191 deferred strings that are used. */
3193 static void
3194 mark_constants (struct rtx_constant_pool *pool, rtx insn)
3196 if (!INSN_P (insn))
3197 return;
3199 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3200 insns, not any notes that may be attached. We don't want to mark
3201 a constant just because it happens to appear in a REG_EQUIV note. */
3202 if (GET_CODE (PATTERN (insn)) == SEQUENCE)
3204 rtx seq = PATTERN (insn);
3205 int i, n = XVECLEN (seq, 0);
3206 for (i = 0; i < n; ++i)
3208 rtx subinsn = XVECEXP (seq, 0, i);
3209 if (INSN_P (subinsn))
3210 for_each_rtx (&PATTERN (subinsn), mark_constant, pool);
3213 else
3214 for_each_rtx (&PATTERN (insn), mark_constant, pool);
3217 /* Look through the instructions for this function, and mark all the
3218 entries in POOL which are actually being used. Emit deferred constants
3219 which have indeed been used. */
3221 static void
3222 mark_constant_pool (struct rtx_constant_pool *pool)
3224 rtx insn, link;
3226 if (pool->first == 0 && n_deferred_constants == 0)
3227 return;
3229 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3230 mark_constants (pool, insn);
3232 for (link = current_function_epilogue_delay_list;
3233 link;
3234 link = XEXP (link, 1))
3235 mark_constants (pool, XEXP (link, 0));
3238 /* Write all the constants in the constant pool. */
3240 void
3241 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
3242 tree fndecl ATTRIBUTE_UNUSED)
3244 struct rtx_constant_pool *pool = cfun->varasm->pool;
3245 struct constant_descriptor_rtx *desc;
3247 /* It is possible for gcc to call force_const_mem and then to later
3248 discard the instructions which refer to the constant. In such a
3249 case we do not need to output the constant. */
3250 mark_constant_pool (pool);
3252 #ifdef ASM_OUTPUT_POOL_PROLOGUE
3253 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
3254 #endif
3256 for (desc = pool->first; desc ; desc = desc->next)
3257 output_constant_pool_1 (desc);
3259 #ifdef ASM_OUTPUT_POOL_EPILOGUE
3260 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
3261 #endif
3264 /* Determine what kind of relocations EXP may need. */
3267 compute_reloc_for_constant (tree exp)
3269 int reloc = 0, reloc2;
3270 tree tem;
3272 /* Give the front-end a chance to convert VALUE to something that
3273 looks more like a constant to the back-end. */
3274 exp = lang_hooks.expand_constant (exp);
3276 switch (TREE_CODE (exp))
3278 case ADDR_EXPR:
3279 case FDESC_EXPR:
3280 /* Go inside any operations that get_inner_reference can handle and see
3281 if what's inside is a constant: no need to do anything here for
3282 addresses of variables or functions. */
3283 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3284 tem = TREE_OPERAND (tem, 0))
3287 if (TREE_PUBLIC (tem))
3288 reloc |= 2;
3289 else
3290 reloc |= 1;
3291 break;
3293 case PLUS_EXPR:
3294 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3295 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3296 break;
3298 case MINUS_EXPR:
3299 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3300 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
3301 /* The difference of two local labels is computable at link time. */
3302 if (reloc == 1 && reloc2 == 1)
3303 reloc = 0;
3304 else
3305 reloc |= reloc2;
3306 break;
3308 case NOP_EXPR:
3309 case CONVERT_EXPR:
3310 case NON_LVALUE_EXPR:
3311 case VIEW_CONVERT_EXPR:
3312 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
3313 break;
3315 case CONSTRUCTOR:
3317 unsigned HOST_WIDE_INT idx;
3318 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3319 if (tem != 0)
3320 reloc |= compute_reloc_for_constant (tem);
3322 break;
3324 default:
3325 break;
3327 return reloc;
3330 /* Find all the constants whose addresses are referenced inside of EXP,
3331 and make sure assembler code with a label has been output for each one.
3332 Indicate whether an ADDR_EXPR has been encountered. */
3334 static void
3335 output_addressed_constants (tree exp)
3337 tree tem;
3339 /* Give the front-end a chance to convert VALUE to something that
3340 looks more like a constant to the back-end. */
3341 exp = lang_hooks.expand_constant (exp);
3343 switch (TREE_CODE (exp))
3345 case ADDR_EXPR:
3346 case FDESC_EXPR:
3347 /* Go inside any operations that get_inner_reference can handle and see
3348 if what's inside is a constant: no need to do anything here for
3349 addresses of variables or functions. */
3350 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
3351 tem = TREE_OPERAND (tem, 0))
3354 /* If we have an initialized CONST_DECL, retrieve the initializer. */
3355 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
3356 tem = DECL_INITIAL (tem);
3358 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
3359 output_constant_def (tem, 0);
3360 break;
3362 case PLUS_EXPR:
3363 case MINUS_EXPR:
3364 output_addressed_constants (TREE_OPERAND (exp, 1));
3365 /* Fall through. */
3367 case NOP_EXPR:
3368 case CONVERT_EXPR:
3369 case NON_LVALUE_EXPR:
3370 case VIEW_CONVERT_EXPR:
3371 output_addressed_constants (TREE_OPERAND (exp, 0));
3372 break;
3374 case CONSTRUCTOR:
3376 unsigned HOST_WIDE_INT idx;
3377 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
3378 if (tem != 0)
3379 output_addressed_constants (tem);
3381 break;
3383 default:
3384 break;
3388 /* Return nonzero if VALUE is a valid constant-valued expression
3389 for use in initializing a static variable; one that can be an
3390 element of a "constant" initializer.
3392 Return null_pointer_node if the value is absolute;
3393 if it is relocatable, return the variable that determines the relocation.
3394 We assume that VALUE has been folded as much as possible;
3395 therefore, we do not need to check for such things as
3396 arithmetic-combinations of integers. */
3398 tree
3399 initializer_constant_valid_p (tree value, tree endtype)
3401 /* Give the front-end a chance to convert VALUE to something that
3402 looks more like a constant to the back-end. */
3403 value = lang_hooks.expand_constant (value);
3405 switch (TREE_CODE (value))
3407 case CONSTRUCTOR:
3408 if ((TREE_CODE (TREE_TYPE (value)) == UNION_TYPE
3409 || TREE_CODE (TREE_TYPE (value)) == RECORD_TYPE)
3410 && TREE_CONSTANT (value)
3411 && !VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (value)))
3413 unsigned HOST_WIDE_INT idx;
3414 tree elt;
3415 bool absolute = true;
3417 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
3419 tree reloc;
3420 reloc = initializer_constant_valid_p (elt, TREE_TYPE (elt));
3421 if (!reloc)
3422 return NULL_TREE;
3423 if (reloc != null_pointer_node)
3424 absolute = false;
3426 /* For a non-absolute relocation, there is no single
3427 variable that can be "the variable that determines the
3428 relocation." */
3429 return absolute ? null_pointer_node : error_mark_node;
3432 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
3434 case INTEGER_CST:
3435 case VECTOR_CST:
3436 case REAL_CST:
3437 case STRING_CST:
3438 case COMPLEX_CST:
3439 return null_pointer_node;
3441 case ADDR_EXPR:
3442 case FDESC_EXPR:
3443 value = staticp (TREE_OPERAND (value, 0));
3444 if (value)
3446 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out to
3447 be a constant, this is old-skool offsetof-like nonsense. */
3448 if (TREE_CODE (value) == INDIRECT_REF
3449 && TREE_CONSTANT (TREE_OPERAND (value, 0)))
3450 return null_pointer_node;
3451 /* Taking the address of a nested function involves a trampoline. */
3452 if (TREE_CODE (value) == FUNCTION_DECL
3453 && ((decl_function_context (value)
3454 && !DECL_NO_STATIC_CHAIN (value))
3455 || DECL_DLLIMPORT_P (value)))
3456 return NULL_TREE;
3457 /* "&{...}" requires a temporary to hold the constructed
3458 object. */
3459 if (TREE_CODE (value) == CONSTRUCTOR)
3460 return NULL_TREE;
3462 return value;
3464 case VIEW_CONVERT_EXPR:
3465 case NON_LVALUE_EXPR:
3466 return initializer_constant_valid_p (TREE_OPERAND (value, 0), endtype);
3468 case CONVERT_EXPR:
3469 case NOP_EXPR:
3471 tree src;
3472 tree src_type;
3473 tree dest_type;
3475 src = TREE_OPERAND (value, 0);
3476 src_type = TREE_TYPE (src);
3477 dest_type = TREE_TYPE (value);
3479 /* Allow conversions between pointer types, floating-point
3480 types, and offset types. */
3481 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
3482 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
3483 || (TREE_CODE (dest_type) == OFFSET_TYPE
3484 && TREE_CODE (src_type) == OFFSET_TYPE))
3485 return initializer_constant_valid_p (src, endtype);
3487 /* Allow length-preserving conversions between integer types. */
3488 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
3489 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
3490 return initializer_constant_valid_p (src, endtype);
3492 /* Allow conversions between other integer types only if
3493 explicit value. */
3494 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
3496 tree inner = initializer_constant_valid_p (src, endtype);
3497 if (inner == null_pointer_node)
3498 return null_pointer_node;
3499 break;
3502 /* Allow (int) &foo provided int is as wide as a pointer. */
3503 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
3504 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
3505 return initializer_constant_valid_p (src, endtype);
3507 /* Likewise conversions from int to pointers, but also allow
3508 conversions from 0. */
3509 if ((POINTER_TYPE_P (dest_type)
3510 || TREE_CODE (dest_type) == OFFSET_TYPE)
3511 && INTEGRAL_TYPE_P (src_type))
3513 if (TREE_CODE (src) == INTEGER_CST
3514 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
3515 return null_pointer_node;
3516 if (integer_zerop (src))
3517 return null_pointer_node;
3518 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
3519 return initializer_constant_valid_p (src, endtype);
3522 /* Allow conversions to struct or union types if the value
3523 inside is okay. */
3524 if (TREE_CODE (dest_type) == RECORD_TYPE
3525 || TREE_CODE (dest_type) == UNION_TYPE)
3526 return initializer_constant_valid_p (src, endtype);
3528 break;
3530 case PLUS_EXPR:
3531 if (! INTEGRAL_TYPE_P (endtype)
3532 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3534 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3535 endtype);
3536 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3537 endtype);
3538 /* If either term is absolute, use the other terms relocation. */
3539 if (valid0 == null_pointer_node)
3540 return valid1;
3541 if (valid1 == null_pointer_node)
3542 return valid0;
3544 break;
3546 case MINUS_EXPR:
3547 if (! INTEGRAL_TYPE_P (endtype)
3548 || TYPE_PRECISION (endtype) >= POINTER_SIZE)
3550 tree valid0 = initializer_constant_valid_p (TREE_OPERAND (value, 0),
3551 endtype);
3552 tree valid1 = initializer_constant_valid_p (TREE_OPERAND (value, 1),
3553 endtype);
3554 /* Win if second argument is absolute. */
3555 if (valid1 == null_pointer_node)
3556 return valid0;
3557 /* Win if both arguments have the same relocation.
3558 Then the value is absolute. */
3559 if (valid0 == valid1 && valid0 != 0)
3560 return null_pointer_node;
3562 /* Since GCC guarantees that string constants are unique in the
3563 generated code, a subtraction between two copies of the same
3564 constant string is absolute. */
3565 if (valid0 && TREE_CODE (valid0) == STRING_CST
3566 && valid1 && TREE_CODE (valid1) == STRING_CST
3567 && operand_equal_p (valid0, valid1, 1))
3568 return null_pointer_node;
3571 /* Support narrowing differences. */
3572 if (INTEGRAL_TYPE_P (endtype))
3574 tree op0, op1;
3576 op0 = TREE_OPERAND (value, 0);
3577 op1 = TREE_OPERAND (value, 1);
3579 /* Like STRIP_NOPS except allow the operand mode to widen.
3580 This works around a feature of fold that simplifies
3581 (int)(p1 - p2) to ((int)p1 - (int)p2) under the theory
3582 that the narrower operation is cheaper. */
3584 while (TREE_CODE (op0) == NOP_EXPR
3585 || TREE_CODE (op0) == CONVERT_EXPR
3586 || TREE_CODE (op0) == NON_LVALUE_EXPR)
3588 tree inner = TREE_OPERAND (op0, 0);
3589 if (inner == error_mark_node
3590 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3591 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
3592 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3593 break;
3594 op0 = inner;
3597 while (TREE_CODE (op1) == NOP_EXPR
3598 || TREE_CODE (op1) == CONVERT_EXPR
3599 || TREE_CODE (op1) == NON_LVALUE_EXPR)
3601 tree inner = TREE_OPERAND (op1, 0);
3602 if (inner == error_mark_node
3603 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
3604 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
3605 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
3606 break;
3607 op1 = inner;
3610 op0 = initializer_constant_valid_p (op0, endtype);
3611 op1 = initializer_constant_valid_p (op1, endtype);
3613 /* Both initializers must be known. */
3614 if (op0 && op1)
3616 if (op0 == op1)
3617 return null_pointer_node;
3619 /* Support differences between labels. */
3620 if (TREE_CODE (op0) == LABEL_DECL
3621 && TREE_CODE (op1) == LABEL_DECL)
3622 return null_pointer_node;
3624 if (TREE_CODE (op0) == STRING_CST
3625 && TREE_CODE (op1) == STRING_CST
3626 && operand_equal_p (op0, op1, 1))
3627 return null_pointer_node;
3630 break;
3632 default:
3633 break;
3636 return 0;
3639 /* Output assembler code for constant EXP to FILE, with no label.
3640 This includes the pseudo-op such as ".int" or ".byte", and a newline.
3641 Assumes output_addressed_constants has been done on EXP already.
3643 Generate exactly SIZE bytes of assembler data, padding at the end
3644 with zeros if necessary. SIZE must always be specified.
3646 SIZE is important for structure constructors,
3647 since trailing members may have been omitted from the constructor.
3648 It is also important for initialization of arrays from string constants
3649 since the full length of the string constant might not be wanted.
3650 It is also needed for initialization of unions, where the initializer's
3651 type is just one member, and that may not be as long as the union.
3653 There a case in which we would fail to output exactly SIZE bytes:
3654 for a structure constructor that wants to produce more than SIZE bytes.
3655 But such constructors will never be generated for any possible input.
3657 ALIGN is the alignment of the data in bits. */
3659 void
3660 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
3662 enum tree_code code;
3663 unsigned HOST_WIDE_INT thissize;
3665 /* Some front-ends use constants other than the standard language-independent
3666 varieties, but which may still be output directly. Give the front-end a
3667 chance to convert EXP to a language-independent representation. */
3668 exp = lang_hooks.expand_constant (exp);
3670 if (size == 0 || flag_syntax_only)
3671 return;
3673 /* See if we're trying to initialize a pointer in a non-default mode
3674 to the address of some declaration somewhere. If the target says
3675 the mode is valid for pointers, assume the target has a way of
3676 resolving it. */
3677 if (TREE_CODE (exp) == NOP_EXPR
3678 && POINTER_TYPE_P (TREE_TYPE (exp))
3679 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3681 tree saved_type = TREE_TYPE (exp);
3683 /* Peel off any intermediate conversions-to-pointer for valid
3684 pointer modes. */
3685 while (TREE_CODE (exp) == NOP_EXPR
3686 && POINTER_TYPE_P (TREE_TYPE (exp))
3687 && targetm.valid_pointer_mode (TYPE_MODE (TREE_TYPE (exp))))
3688 exp = TREE_OPERAND (exp, 0);
3690 /* If what we're left with is the address of something, we can
3691 convert the address to the final type and output it that
3692 way. */
3693 if (TREE_CODE (exp) == ADDR_EXPR)
3694 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
3695 /* Likewise for constant ints. */
3696 else if (TREE_CODE (exp) == INTEGER_CST)
3697 exp = build_int_cst_wide (saved_type, TREE_INT_CST_LOW (exp),
3698 TREE_INT_CST_HIGH (exp));
3702 /* Eliminate any conversions since we'll be outputting the underlying
3703 constant. */
3704 while (TREE_CODE (exp) == NOP_EXPR || TREE_CODE (exp) == CONVERT_EXPR
3705 || TREE_CODE (exp) == NON_LVALUE_EXPR
3706 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
3708 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
3709 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
3711 /* Make sure eliminating the conversion is really a no-op, except with
3712 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
3713 union types to allow for Ada unchecked unions. */
3714 if (type_size > op_size
3715 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
3716 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
3717 internal_error ("no-op convert from %wd to %wd bytes in initializer",
3718 op_size, type_size);
3720 exp = TREE_OPERAND (exp, 0);
3723 code = TREE_CODE (TREE_TYPE (exp));
3724 thissize = int_size_in_bytes (TREE_TYPE (exp));
3726 /* Allow a constructor with no elements for any data type.
3727 This means to fill the space with zeros. */
3728 if (TREE_CODE (exp) == CONSTRUCTOR
3729 && VEC_empty (constructor_elt, CONSTRUCTOR_ELTS (exp)))
3731 assemble_zeros (size);
3732 return;
3735 if (TREE_CODE (exp) == FDESC_EXPR)
3737 #ifdef ASM_OUTPUT_FDESC
3738 HOST_WIDE_INT part = tree_low_cst (TREE_OPERAND (exp, 1), 0);
3739 tree decl = TREE_OPERAND (exp, 0);
3740 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
3741 #else
3742 gcc_unreachable ();
3743 #endif
3744 return;
3747 /* Now output the underlying data. If we've handling the padding, return.
3748 Otherwise, break and ensure SIZE is the size written. */
3749 switch (code)
3751 case BOOLEAN_TYPE:
3752 case INTEGER_TYPE:
3753 case ENUMERAL_TYPE:
3754 case POINTER_TYPE:
3755 case REFERENCE_TYPE:
3756 case OFFSET_TYPE:
3757 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3758 EXPAND_INITIALIZER),
3759 MIN (size, thissize), align, 0))
3760 error ("initializer for integer value is too complicated");
3761 break;
3763 case REAL_TYPE:
3764 if (TREE_CODE (exp) != REAL_CST)
3765 error ("initializer for floating value is not a floating constant");
3767 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3768 break;
3770 case COMPLEX_TYPE:
3771 output_constant (TREE_REALPART (exp), thissize / 2, align);
3772 output_constant (TREE_IMAGPART (exp), thissize / 2,
3773 min_align (align, BITS_PER_UNIT * (thissize / 2)));
3774 break;
3776 case ARRAY_TYPE:
3777 case VECTOR_TYPE:
3778 switch (TREE_CODE (exp))
3780 case CONSTRUCTOR:
3781 output_constructor (exp, size, align);
3782 return;
3783 case STRING_CST:
3784 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3785 size);
3786 assemble_string (TREE_STRING_POINTER (exp), thissize);
3787 break;
3789 case VECTOR_CST:
3791 int elt_size;
3792 tree link;
3793 unsigned int nalign;
3794 enum machine_mode inner;
3796 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
3797 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3799 elt_size = GET_MODE_SIZE (inner);
3801 link = TREE_VECTOR_CST_ELTS (exp);
3802 output_constant (TREE_VALUE (link), elt_size, align);
3803 while ((link = TREE_CHAIN (link)) != NULL)
3804 output_constant (TREE_VALUE (link), elt_size, nalign);
3805 break;
3807 default:
3808 gcc_unreachable ();
3810 break;
3812 case RECORD_TYPE:
3813 case UNION_TYPE:
3814 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
3815 output_constructor (exp, size, align);
3816 return;
3818 case ERROR_MARK:
3819 return;
3821 default:
3822 gcc_unreachable ();
3825 if (size > thissize)
3826 assemble_zeros (size - thissize);
3830 /* Subroutine of output_constructor, used for computing the size of
3831 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
3832 type with an unspecified upper bound. */
3834 static unsigned HOST_WIDE_INT
3835 array_size_for_constructor (tree val)
3837 tree max_index, i;
3838 unsigned HOST_WIDE_INT cnt;
3839 tree index, value;
3841 /* This code used to attempt to handle string constants that are not
3842 arrays of single-bytes, but nothing else does, so there's no point in
3843 doing it here. */
3844 if (TREE_CODE (val) == STRING_CST)
3845 return TREE_STRING_LENGTH (val);
3847 max_index = NULL_TREE;
3848 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
3850 if (TREE_CODE (index) == RANGE_EXPR)
3851 index = TREE_OPERAND (index, 1);
3852 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
3853 max_index = index;
3856 if (max_index == NULL_TREE)
3857 return 0;
3859 /* Compute the total number of array elements. */
3860 i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
3861 convert (sizetype,
3862 TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
3863 i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
3865 /* Multiply by the array element unit size to find number of bytes. */
3866 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
3868 return tree_low_cst (i, 1);
3871 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
3872 Generate at least SIZE bytes, padding if necessary. */
3874 static void
3875 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
3876 unsigned int align)
3878 tree type = TREE_TYPE (exp);
3879 tree field = 0;
3880 tree min_index = 0;
3881 /* Number of bytes output or skipped so far.
3882 In other words, current position within the constructor. */
3883 HOST_WIDE_INT total_bytes = 0;
3884 /* Nonzero means BYTE contains part of a byte, to be output. */
3885 int byte_buffer_in_use = 0;
3886 int byte = 0;
3887 unsigned HOST_WIDE_INT cnt;
3888 constructor_elt *ce;
3890 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
3892 if (TREE_CODE (type) == RECORD_TYPE)
3893 field = TYPE_FIELDS (type);
3895 if (TREE_CODE (type) == ARRAY_TYPE
3896 && TYPE_DOMAIN (type) != 0)
3897 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3899 /* As LINK goes through the elements of the constant,
3900 FIELD goes through the structure fields, if the constant is a structure.
3901 if the constant is a union, then we override this,
3902 by getting the field from the TREE_LIST element.
3903 But the constant could also be an array. Then FIELD is zero.
3905 There is always a maximum of one element in the chain LINK for unions
3906 (even if the initializer in a source program incorrectly contains
3907 more one). */
3908 for (cnt = 0;
3909 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
3910 cnt++, field = field ? TREE_CHAIN (field) : 0)
3912 tree val = ce->value;
3913 tree index = 0;
3915 /* The element in a union constructor specifies the proper field
3916 or index. */
3917 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
3918 || TREE_CODE (type) == QUAL_UNION_TYPE)
3919 && ce->index != 0)
3920 field = ce->index;
3922 else if (TREE_CODE (type) == ARRAY_TYPE)
3923 index = ce->index;
3925 #ifdef ASM_COMMENT_START
3926 if (field && flag_verbose_asm)
3927 fprintf (asm_out_file, "%s %s:\n",
3928 ASM_COMMENT_START,
3929 DECL_NAME (field)
3930 ? IDENTIFIER_POINTER (DECL_NAME (field))
3931 : "<anonymous>");
3932 #endif
3934 /* Eliminate the marker that makes a cast not be an lvalue. */
3935 if (val != 0)
3936 STRIP_NOPS (val);
3938 if (index && TREE_CODE (index) == RANGE_EXPR)
3940 unsigned HOST_WIDE_INT fieldsize
3941 = int_size_in_bytes (TREE_TYPE (type));
3942 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
3943 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
3944 HOST_WIDE_INT index;
3945 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
3947 for (index = lo_index; index <= hi_index; index++)
3949 /* Output the element's initial value. */
3950 if (val == 0)
3951 assemble_zeros (fieldsize);
3952 else
3953 output_constant (val, fieldsize, align2);
3955 /* Count its size. */
3956 total_bytes += fieldsize;
3959 else if (field == 0 || !DECL_BIT_FIELD (field))
3961 /* An element that is not a bit-field. */
3963 unsigned HOST_WIDE_INT fieldsize;
3964 /* Since this structure is static,
3965 we know the positions are constant. */
3966 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
3967 unsigned int align2;
3969 if (index != 0)
3970 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
3971 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
3973 /* Output any buffered-up bit-fields preceding this element. */
3974 if (byte_buffer_in_use)
3976 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3977 total_bytes++;
3978 byte_buffer_in_use = 0;
3981 /* Advance to offset of this element.
3982 Note no alignment needed in an array, since that is guaranteed
3983 if each element has the proper size. */
3984 if ((field != 0 || index != 0) && pos != total_bytes)
3986 gcc_assert (pos >= total_bytes);
3987 assemble_zeros (pos - total_bytes);
3988 total_bytes = pos;
3991 /* Find the alignment of this element. */
3992 align2 = min_align (align, BITS_PER_UNIT * pos);
3994 /* Determine size this element should occupy. */
3995 if (field)
3997 fieldsize = 0;
3999 /* If this is an array with an unspecified upper bound,
4000 the initializer determines the size. */
4001 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4002 but we cannot do this until the deprecated support for
4003 initializing zero-length array members is removed. */
4004 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4005 && TYPE_DOMAIN (TREE_TYPE (field))
4006 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4008 fieldsize = array_size_for_constructor (val);
4009 /* Given a non-empty initialization, this field had
4010 better be last. */
4011 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4013 else if (DECL_SIZE_UNIT (field))
4015 /* ??? This can't be right. If the decl size overflows
4016 a host integer we will silently emit no data. */
4017 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4018 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4021 else
4022 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4024 /* Output the element's initial value. */
4025 if (val == 0)
4026 assemble_zeros (fieldsize);
4027 else
4028 output_constant (val, fieldsize, align2);
4030 /* Count its size. */
4031 total_bytes += fieldsize;
4033 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4034 error ("invalid initial value for member %qs",
4035 IDENTIFIER_POINTER (DECL_NAME (field)));
4036 else
4038 /* Element that is a bit-field. */
4040 HOST_WIDE_INT next_offset = int_bit_position (field);
4041 HOST_WIDE_INT end_offset
4042 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4044 if (val == 0)
4045 val = integer_zero_node;
4047 /* If this field does not start in this (or, next) byte,
4048 skip some bytes. */
4049 if (next_offset / BITS_PER_UNIT != total_bytes)
4051 /* Output remnant of any bit field in previous bytes. */
4052 if (byte_buffer_in_use)
4054 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4055 total_bytes++;
4056 byte_buffer_in_use = 0;
4059 /* If still not at proper byte, advance to there. */
4060 if (next_offset / BITS_PER_UNIT != total_bytes)
4062 gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
4063 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4064 total_bytes = next_offset / BITS_PER_UNIT;
4068 if (! byte_buffer_in_use)
4069 byte = 0;
4071 /* We must split the element into pieces that fall within
4072 separate bytes, and combine each byte with previous or
4073 following bit-fields. */
4075 /* next_offset is the offset n fbits from the beginning of
4076 the structure to the next bit of this element to be processed.
4077 end_offset is the offset of the first bit past the end of
4078 this element. */
4079 while (next_offset < end_offset)
4081 int this_time;
4082 int shift;
4083 HOST_WIDE_INT value;
4084 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4085 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4087 /* Advance from byte to byte
4088 within this element when necessary. */
4089 while (next_byte != total_bytes)
4091 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4092 total_bytes++;
4093 byte = 0;
4096 /* Number of bits we can process at once
4097 (all part of the same byte). */
4098 this_time = MIN (end_offset - next_offset,
4099 BITS_PER_UNIT - next_bit);
4100 if (BYTES_BIG_ENDIAN)
4102 /* On big-endian machine, take the most significant bits
4103 first (of the bits that are significant)
4104 and put them into bytes from the most significant end. */
4105 shift = end_offset - next_offset - this_time;
4107 /* Don't try to take a bunch of bits that cross
4108 the word boundary in the INTEGER_CST. We can
4109 only select bits from the LOW or HIGH part
4110 not from both. */
4111 if (shift < HOST_BITS_PER_WIDE_INT
4112 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4114 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4115 shift = HOST_BITS_PER_WIDE_INT;
4118 /* Now get the bits from the appropriate constant word. */
4119 if (shift < HOST_BITS_PER_WIDE_INT)
4120 value = TREE_INT_CST_LOW (val);
4121 else
4123 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4124 value = TREE_INT_CST_HIGH (val);
4125 shift -= HOST_BITS_PER_WIDE_INT;
4128 /* Get the result. This works only when:
4129 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4130 byte |= (((value >> shift)
4131 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4132 << (BITS_PER_UNIT - this_time - next_bit));
4134 else
4136 /* On little-endian machines,
4137 take first the least significant bits of the value
4138 and pack them starting at the least significant
4139 bits of the bytes. */
4140 shift = next_offset - int_bit_position (field);
4142 /* Don't try to take a bunch of bits that cross
4143 the word boundary in the INTEGER_CST. We can
4144 only select bits from the LOW or HIGH part
4145 not from both. */
4146 if (shift < HOST_BITS_PER_WIDE_INT
4147 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4148 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4150 /* Now get the bits from the appropriate constant word. */
4151 if (shift < HOST_BITS_PER_WIDE_INT)
4152 value = TREE_INT_CST_LOW (val);
4153 else
4155 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4156 value = TREE_INT_CST_HIGH (val);
4157 shift -= HOST_BITS_PER_WIDE_INT;
4160 /* Get the result. This works only when:
4161 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4162 byte |= (((value >> shift)
4163 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4164 << next_bit);
4167 next_offset += this_time;
4168 byte_buffer_in_use = 1;
4173 if (byte_buffer_in_use)
4175 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4176 total_bytes++;
4179 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4180 assemble_zeros (size - total_bytes);
4183 /* This TREE_LIST contains any weak symbol declarations waiting
4184 to be emitted. */
4185 static GTY(()) tree weak_decls;
4187 /* Mark DECL as weak. */
4189 static void
4190 mark_weak (tree decl)
4192 DECL_WEAK (decl) = 1;
4194 if (DECL_RTL_SET_P (decl)
4195 && MEM_P (DECL_RTL (decl))
4196 && XEXP (DECL_RTL (decl), 0)
4197 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4198 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4201 /* Merge weak status between NEWDECL and OLDDECL. */
4203 void
4204 merge_weak (tree newdecl, tree olddecl)
4206 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4208 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4210 tree *pwd;
4211 /* We put the NEWDECL on the weak_decls list at some point
4212 and OLDDECL as well. Keep just OLDDECL on the list. */
4213 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4214 if (TREE_VALUE (*pwd) == newdecl)
4216 *pwd = TREE_CHAIN (*pwd);
4217 break;
4220 return;
4223 if (DECL_WEAK (newdecl))
4225 tree wd;
4227 /* NEWDECL is weak, but OLDDECL is not. */
4229 /* If we already output the OLDDECL, we're in trouble; we can't
4230 go back and make it weak. This error cannot caught in
4231 declare_weak because the NEWDECL and OLDDECL was not yet
4232 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4233 if (TREE_ASM_WRITTEN (olddecl))
4234 error ("weak declaration of %q+D must precede definition",
4235 newdecl);
4237 /* If we've already generated rtl referencing OLDDECL, we may
4238 have done so in a way that will not function properly with
4239 a weak symbol. */
4240 else if (TREE_USED (olddecl)
4241 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4242 warning (0, "weak declaration of %q+D after first use results "
4243 "in unspecified behavior", newdecl);
4245 if (SUPPORTS_WEAK)
4247 /* We put the NEWDECL on the weak_decls list at some point.
4248 Replace it with the OLDDECL. */
4249 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4250 if (TREE_VALUE (wd) == newdecl)
4252 TREE_VALUE (wd) = olddecl;
4253 break;
4255 /* We may not find the entry on the list. If NEWDECL is a
4256 weak alias, then we will have already called
4257 globalize_decl to remove the entry; in that case, we do
4258 not need to do anything. */
4261 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4262 mark_weak (olddecl);
4264 else
4265 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4266 weak. Just update NEWDECL to indicate that it's weak too. */
4267 mark_weak (newdecl);
4270 /* Declare DECL to be a weak symbol. */
4272 void
4273 declare_weak (tree decl)
4275 if (! TREE_PUBLIC (decl))
4276 error ("weak declaration of %q+D must be public", decl);
4277 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4278 error ("weak declaration of %q+D must precede definition", decl);
4279 else if (SUPPORTS_WEAK)
4281 if (! DECL_WEAK (decl))
4282 weak_decls = tree_cons (NULL, decl, weak_decls);
4284 else
4285 warning (0, "weak declaration of %q+D not supported", decl);
4287 mark_weak (decl);
4290 static void
4291 weak_finish_1 (tree decl)
4293 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4294 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4295 #endif
4297 if (! TREE_USED (decl))
4298 return;
4300 #ifdef ASM_WEAKEN_DECL
4301 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4302 #else
4303 #ifdef ASM_WEAKEN_LABEL
4304 ASM_WEAKEN_LABEL (asm_out_file, name);
4305 #else
4306 #ifdef ASM_OUTPUT_WEAK_ALIAS
4308 static bool warn_once = 0;
4309 if (! warn_once)
4311 warning (0, "only weak aliases are supported in this configuration");
4312 warn_once = 1;
4314 return;
4316 #endif
4317 #endif
4318 #endif
4321 /* This TREE_LIST contains weakref targets. */
4323 static GTY(()) tree weakref_targets;
4325 /* Forward declaration. */
4326 static tree find_decl_and_mark_needed (tree decl, tree target);
4328 /* Emit any pending weak declarations. */
4330 void
4331 weak_finish (void)
4333 tree t;
4335 for (t = weakref_targets; t; t = TREE_CHAIN (t))
4337 tree alias_decl = TREE_PURPOSE (t);
4338 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
4340 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
4341 /* Remove alias_decl from the weak list, but leave entries for
4342 the target alone. */
4343 target = NULL_TREE;
4344 #ifndef ASM_OUTPUT_WEAKREF
4345 else if (! TREE_SYMBOL_REFERENCED (target))
4347 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
4348 defined, otherwise we and weak_finish_1 would use a
4349 different macros. */
4350 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
4351 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
4352 # else
4353 tree decl = find_decl_and_mark_needed (alias_decl, target);
4355 if (! decl)
4357 decl = build_decl (TREE_CODE (alias_decl), target,
4358 TREE_TYPE (alias_decl));
4360 DECL_EXTERNAL (decl) = 1;
4361 TREE_PUBLIC (decl) = 1;
4362 DECL_ARTIFICIAL (decl) = 1;
4363 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
4364 TREE_USED (decl) = 1;
4367 weak_finish_1 (decl);
4368 # endif
4370 #endif
4373 tree *p;
4374 tree t2;
4376 /* Remove the alias and the target from the pending weak list
4377 so that we do not emit any .weak directives for the former,
4378 nor multiple .weak directives for the latter. */
4379 for (p = &weak_decls; (t2 = *p) ; )
4381 if (TREE_VALUE (t2) == alias_decl
4382 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
4383 *p = TREE_CHAIN (t2);
4384 else
4385 p = &TREE_CHAIN (t2);
4388 /* Remove other weakrefs to the same target, to speed things up. */
4389 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
4391 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
4392 *p = TREE_CHAIN (t2);
4393 else
4394 p = &TREE_CHAIN (t2);
4399 for (t = weak_decls; t; t = TREE_CHAIN (t))
4401 tree decl = TREE_VALUE (t);
4403 weak_finish_1 (decl);
4407 /* Emit the assembly bits to indicate that DECL is globally visible. */
4409 static void
4410 globalize_decl (tree decl)
4412 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4414 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4415 if (DECL_WEAK (decl))
4417 tree *p, t;
4419 #ifdef ASM_WEAKEN_DECL
4420 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4421 #else
4422 ASM_WEAKEN_LABEL (asm_out_file, name);
4423 #endif
4425 /* Remove this function from the pending weak list so that
4426 we do not emit multiple .weak directives for it. */
4427 for (p = &weak_decls; (t = *p) ; )
4429 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4430 *p = TREE_CHAIN (t);
4431 else
4432 p = &TREE_CHAIN (t);
4435 /* Remove weakrefs to the same target from the pending weakref
4436 list, for the same reason. */
4437 for (p = &weakref_targets; (t = *p) ; )
4439 if (DECL_ASSEMBLER_NAME (decl)
4440 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4441 *p = TREE_CHAIN (t);
4442 else
4443 p = &TREE_CHAIN (t);
4446 return;
4448 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4449 if (DECL_ONE_ONLY (decl))
4450 ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4451 #endif
4453 targetm.asm_out.globalize_label (asm_out_file, name);
4456 /* We have to be able to tell cgraph about the needed-ness of the target
4457 of an alias. This requires that the decl have been defined. Aliases
4458 that precede their definition have to be queued for later processing. */
4460 typedef struct alias_pair GTY(())
4462 tree decl;
4463 tree target;
4464 } alias_pair;
4466 /* Define gc'd vector type. */
4467 DEF_VEC_O(alias_pair);
4468 DEF_VEC_ALLOC_O(alias_pair,gc);
4470 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
4472 /* Given an assembly name, find the decl it is associated with. At the
4473 same time, mark it needed for cgraph. */
4475 static tree
4476 find_decl_and_mark_needed (tree decl, tree target)
4478 struct cgraph_node *fnode = NULL;
4479 struct cgraph_varpool_node *vnode = NULL;
4481 if (TREE_CODE (decl) == FUNCTION_DECL)
4483 fnode = cgraph_node_for_asm (target);
4484 if (fnode == NULL)
4485 vnode = cgraph_varpool_node_for_asm (target);
4487 else
4489 vnode = cgraph_varpool_node_for_asm (target);
4490 if (vnode == NULL)
4491 fnode = cgraph_node_for_asm (target);
4494 if (fnode)
4496 /* We can't mark function nodes as used after cgraph global info
4497 is finished. This wouldn't generally be necessary, but C++
4498 virtual table thunks are introduced late in the game and
4499 might seem like they need marking, although in fact they
4500 don't. */
4501 if (! cgraph_global_info_ready)
4502 cgraph_mark_needed_node (fnode);
4503 return fnode->decl;
4505 else if (vnode)
4507 cgraph_varpool_mark_needed_node (vnode);
4508 return vnode->decl;
4510 else
4511 return NULL_TREE;
4514 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4515 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
4516 tree node is DECL to have the value of the tree node TARGET. */
4518 static void
4519 do_assemble_alias (tree decl, tree target)
4521 if (TREE_ASM_WRITTEN (decl))
4522 return;
4524 TREE_ASM_WRITTEN (decl) = 1;
4525 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4527 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4529 ultimate_transparent_alias_target (&target);
4531 if (!TREE_SYMBOL_REFERENCED (target))
4532 weakref_targets = tree_cons (decl, target, weakref_targets);
4534 #ifdef ASM_OUTPUT_WEAKREF
4535 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4536 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4537 IDENTIFIER_POINTER (target));
4538 #else
4539 if (!SUPPORTS_WEAK)
4541 error ("%Jweakref is not supported in this configuration", decl);
4542 return;
4544 #endif
4545 return;
4548 #ifdef ASM_OUTPUT_DEF
4549 /* Make name accessible from other files, if appropriate. */
4551 if (TREE_PUBLIC (decl))
4553 globalize_decl (decl);
4554 maybe_assemble_visibility (decl);
4557 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4558 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4559 # else
4560 ASM_OUTPUT_DEF (asm_out_file,
4561 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4562 IDENTIFIER_POINTER (target));
4563 # endif
4564 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4566 const char *name;
4567 tree *p, t;
4569 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4570 # ifdef ASM_WEAKEN_DECL
4571 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4572 # else
4573 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4574 # endif
4575 /* Remove this function from the pending weak list so that
4576 we do not emit multiple .weak directives for it. */
4577 for (p = &weak_decls; (t = *p) ; )
4578 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4579 *p = TREE_CHAIN (t);
4580 else
4581 p = &TREE_CHAIN (t);
4583 /* Remove weakrefs to the same target from the pending weakref
4584 list, for the same reason. */
4585 for (p = &weakref_targets; (t = *p) ; )
4587 if (DECL_ASSEMBLER_NAME (decl)
4588 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4589 *p = TREE_CHAIN (t);
4590 else
4591 p = &TREE_CHAIN (t);
4594 #endif
4597 /* First pass of completing pending aliases. Make sure that cgraph knows
4598 which symbols will be required. */
4600 void
4601 finish_aliases_1 (void)
4603 unsigned i;
4604 alias_pair *p;
4606 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4608 tree target_decl;
4610 target_decl = find_decl_and_mark_needed (p->decl, p->target);
4611 if (target_decl == NULL)
4613 if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4614 error ("%q+D aliased to undefined symbol %qs",
4615 p->decl, IDENTIFIER_POINTER (p->target));
4617 else if (DECL_EXTERNAL (target_decl)
4618 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4619 error ("%q+D aliased to external symbol %qs",
4620 p->decl, IDENTIFIER_POINTER (p->target));
4624 /* Second pass of completing pending aliases. Emit the actual assembly.
4625 This happens at the end of compilation and thus it is assured that the
4626 target symbol has been emitted. */
4628 void
4629 finish_aliases_2 (void)
4631 unsigned i;
4632 alias_pair *p;
4634 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4635 do_assemble_alias (p->decl, p->target);
4637 VEC_truncate (alias_pair, alias_pairs, 0);
4640 /* Emit an assembler directive to make the symbol for DECL an alias to
4641 the symbol for TARGET. */
4643 void
4644 assemble_alias (tree decl, tree target)
4646 tree target_decl;
4647 bool is_weakref = false;
4649 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4651 tree alias = DECL_ASSEMBLER_NAME (decl);
4653 is_weakref = true;
4655 ultimate_transparent_alias_target (&target);
4657 if (alias == target)
4658 error ("weakref %q+D ultimately targets itself", decl);
4659 else
4661 #ifndef ASM_OUTPUT_WEAKREF
4662 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
4663 TREE_CHAIN (alias) = target;
4664 #endif
4666 if (TREE_PUBLIC (decl))
4667 error ("weakref %q+D must have static linkage", decl);
4669 else
4671 #if !defined (ASM_OUTPUT_DEF)
4672 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4673 error ("%Jalias definitions not supported in this configuration", decl);
4674 return;
4675 # else
4676 if (!DECL_WEAK (decl))
4678 error ("%Jonly weak aliases are supported in this configuration", decl);
4679 return;
4681 # endif
4682 #endif
4685 /* We must force creation of DECL_RTL for debug info generation, even though
4686 we don't use it here. */
4687 make_decl_rtl (decl);
4688 TREE_USED (decl) = 1;
4690 /* A quirk of the initial implementation of aliases required that the user
4691 add "extern" to all of them. Which is silly, but now historical. Do
4692 note that the symbol is in fact locally defined. */
4693 if (! is_weakref)
4694 DECL_EXTERNAL (decl) = 0;
4696 /* Allow aliases to aliases. */
4697 if (TREE_CODE (decl) == FUNCTION_DECL)
4698 cgraph_node (decl)->alias = true;
4699 else
4700 cgraph_varpool_node (decl)->alias = true;
4702 /* If the target has already been emitted, we don't have to queue the
4703 alias. This saves a tad o memory. */
4704 target_decl = find_decl_and_mark_needed (decl, target);
4705 if (target_decl && TREE_ASM_WRITTEN (target_decl))
4706 do_assemble_alias (decl, target);
4707 else
4709 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
4710 p->decl = decl;
4711 p->target = target;
4715 /* Emit an assembler directive to set symbol for DECL visibility to
4716 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
4718 void
4719 default_assemble_visibility (tree decl, int vis)
4721 static const char * const visibility_types[] = {
4722 NULL, "internal", "hidden", "protected"
4725 const char *name, *type;
4727 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4728 type = visibility_types[vis];
4730 #ifdef HAVE_GAS_HIDDEN
4731 fprintf (asm_out_file, "\t.%s\t", type);
4732 assemble_name (asm_out_file, name);
4733 fprintf (asm_out_file, "\n");
4734 #else
4735 warning (OPT_Wattributes, "visibility attribute not supported "
4736 "in this configuration; ignored");
4737 #endif
4740 /* A helper function to call assemble_visibility when needed for a decl. */
4742 static void
4743 maybe_assemble_visibility (tree decl)
4745 enum symbol_visibility vis = DECL_VISIBILITY (decl);
4747 if (vis != VISIBILITY_DEFAULT)
4748 targetm.asm_out.visibility (decl, vis);
4751 /* Returns 1 if the target configuration supports defining public symbols
4752 so that one of them will be chosen at link time instead of generating a
4753 multiply-defined symbol error, whether through the use of weak symbols or
4754 a target-specific mechanism for having duplicates discarded. */
4757 supports_one_only (void)
4759 if (SUPPORTS_ONE_ONLY)
4760 return 1;
4761 return SUPPORTS_WEAK;
4764 /* Set up DECL as a public symbol that can be defined in multiple
4765 translation units without generating a linker error. */
4767 void
4768 make_decl_one_only (tree decl)
4770 gcc_assert (TREE_CODE (decl) == VAR_DECL
4771 || TREE_CODE (decl) == FUNCTION_DECL);
4773 TREE_PUBLIC (decl) = 1;
4775 if (SUPPORTS_ONE_ONLY)
4777 #ifdef MAKE_DECL_ONE_ONLY
4778 MAKE_DECL_ONE_ONLY (decl);
4779 #endif
4780 DECL_ONE_ONLY (decl) = 1;
4782 else if (TREE_CODE (decl) == VAR_DECL
4783 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4784 DECL_COMMON (decl) = 1;
4785 else
4787 gcc_assert (SUPPORTS_WEAK);
4788 DECL_WEAK (decl) = 1;
4792 void
4793 init_varasm_once (void)
4795 section_htab = htab_create_ggc (31, section_entry_hash,
4796 section_entry_eq, NULL);
4797 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4798 const_desc_eq, NULL);
4800 const_alias_set = new_alias_set ();
4802 #ifdef TEXT_SECTION_ASM_OP
4803 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
4804 TEXT_SECTION_ASM_OP);
4805 #endif
4807 #ifdef DATA_SECTION_ASM_OP
4808 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
4809 DATA_SECTION_ASM_OP);
4810 #endif
4812 #ifdef SDATA_SECTION_ASM_OP
4813 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
4814 SDATA_SECTION_ASM_OP);
4815 #endif
4817 #ifdef READONLY_DATA_SECTION_ASM_OP
4818 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
4819 READONLY_DATA_SECTION_ASM_OP);
4820 #endif
4822 #ifdef CTORS_SECTION_ASM_OP
4823 ctors_section = get_unnamed_section (0, output_section_asm_op,
4824 CTORS_SECTION_ASM_OP);
4825 #endif
4827 #ifdef DTORS_SECTION_ASM_OP
4828 dtors_section = get_unnamed_section (0, output_section_asm_op,
4829 DTORS_SECTION_ASM_OP);
4830 #endif
4832 #ifdef BSS_SECTION_ASM_OP
4833 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
4834 output_section_asm_op,
4835 BSS_SECTION_ASM_OP);
4836 #endif
4838 #ifdef SBSS_SECTION_ASM_OP
4839 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
4840 output_section_asm_op,
4841 SBSS_SECTION_ASM_OP);
4842 #endif
4844 targetm.asm_out.init_sections ();
4846 if (readonly_data_section == NULL)
4847 readonly_data_section = text_section;
4850 enum tls_model
4851 decl_default_tls_model (tree decl)
4853 enum tls_model kind;
4854 bool is_local;
4856 is_local = targetm.binds_local_p (decl);
4857 if (!flag_shlib)
4859 if (is_local)
4860 kind = TLS_MODEL_LOCAL_EXEC;
4861 else
4862 kind = TLS_MODEL_INITIAL_EXEC;
4865 /* Local dynamic is inefficient when we're not combining the
4866 parts of the address. */
4867 else if (optimize && is_local)
4868 kind = TLS_MODEL_LOCAL_DYNAMIC;
4869 else
4870 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4871 if (kind < flag_tls_default)
4872 kind = flag_tls_default;
4874 return kind;
4877 /* Select a set of attributes for section NAME based on the properties
4878 of DECL and whether or not RELOC indicates that DECL's initializer
4879 might contain runtime relocations.
4881 We make the section read-only and executable for a function decl,
4882 read-only for a const data decl, and writable for a non-const data decl. */
4884 unsigned int
4885 default_section_type_flags (tree decl, const char *name, int reloc)
4887 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4890 unsigned int
4891 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4892 int shlib)
4894 unsigned int flags;
4896 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4897 flags = SECTION_CODE;
4898 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4899 flags = 0;
4900 else if (current_function_decl
4901 && cfun
4902 && cfun->unlikely_text_section_name
4903 && strcmp (name, cfun->unlikely_text_section_name) == 0)
4904 flags = SECTION_CODE;
4905 else if (!decl
4906 && (!current_function_decl || !cfun)
4907 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
4908 flags = SECTION_CODE;
4909 else
4910 flags = SECTION_WRITE;
4912 if (decl && DECL_ONE_ONLY (decl))
4913 flags |= SECTION_LINKONCE;
4915 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
4916 flags |= SECTION_TLS | SECTION_WRITE;
4918 if (strcmp (name, ".bss") == 0
4919 || strncmp (name, ".bss.", 5) == 0
4920 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4921 || strcmp (name, ".sbss") == 0
4922 || strncmp (name, ".sbss.", 6) == 0
4923 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
4924 flags |= SECTION_BSS;
4926 if (strcmp (name, ".tdata") == 0
4927 || strncmp (name, ".tdata.", 7) == 0
4928 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
4929 flags |= SECTION_TLS;
4931 if (strcmp (name, ".tbss") == 0
4932 || strncmp (name, ".tbss.", 6) == 0
4933 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4934 flags |= SECTION_TLS | SECTION_BSS;
4936 /* These three sections have special ELF types. They are neither
4937 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4938 want to print a section type (@progbits or @nobits). If someone
4939 is silly enough to emit code or TLS variables to one of these
4940 sections, then don't handle them specially. */
4941 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4942 && (strcmp (name, ".init_array") == 0
4943 || strcmp (name, ".fini_array") == 0
4944 || strcmp (name, ".preinit_array") == 0))
4945 flags |= SECTION_NOTYPE;
4947 return flags;
4950 /* Output assembly to switch to section NAME with attribute FLAGS.
4951 Four variants for common object file formats. */
4953 void
4954 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4955 unsigned int flags ATTRIBUTE_UNUSED,
4956 tree decl ATTRIBUTE_UNUSED)
4958 /* Some object formats don't support named sections at all. The
4959 front-end should already have flagged this as an error. */
4960 gcc_unreachable ();
4963 void
4964 default_elf_asm_named_section (const char *name, unsigned int flags,
4965 tree decl ATTRIBUTE_UNUSED)
4967 char flagchars[10], *f = flagchars;
4969 /* If we have already declared this section, we can use an
4970 abbreviated form to switch back to it -- unless this section is
4971 part of a COMDAT groups, in which case GAS requires the full
4972 declaration every time. */
4973 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4974 && (flags & SECTION_DECLARED))
4976 fprintf (asm_out_file, "\t.section\t%s\n", name);
4977 return;
4980 if (!(flags & SECTION_DEBUG))
4981 *f++ = 'a';
4982 if (flags & SECTION_WRITE)
4983 *f++ = 'w';
4984 if (flags & SECTION_CODE)
4985 *f++ = 'x';
4986 if (flags & SECTION_SMALL)
4987 *f++ = 's';
4988 if (flags & SECTION_MERGE)
4989 *f++ = 'M';
4990 if (flags & SECTION_STRINGS)
4991 *f++ = 'S';
4992 if (flags & SECTION_TLS)
4993 *f++ = 'T';
4994 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4995 *f++ = 'G';
4996 *f = '\0';
4998 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5000 if (!(flags & SECTION_NOTYPE))
5002 const char *type;
5003 const char *format;
5005 if (flags & SECTION_BSS)
5006 type = "nobits";
5007 else
5008 type = "progbits";
5010 format = ",@%s";
5011 #ifdef ASM_COMMENT_START
5012 /* On platforms that use "@" as the assembly comment character,
5013 use "%" instead. */
5014 if (strcmp (ASM_COMMENT_START, "@") == 0)
5015 format = ",%%%s";
5016 #endif
5017 fprintf (asm_out_file, format, type);
5019 if (flags & SECTION_ENTSIZE)
5020 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5021 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5022 fprintf (asm_out_file, ",%s,comdat",
5023 lang_hooks.decls.comdat_group (decl));
5026 putc ('\n', asm_out_file);
5029 void
5030 default_coff_asm_named_section (const char *name, unsigned int flags,
5031 tree decl ATTRIBUTE_UNUSED)
5033 char flagchars[8], *f = flagchars;
5035 if (flags & SECTION_WRITE)
5036 *f++ = 'w';
5037 if (flags & SECTION_CODE)
5038 *f++ = 'x';
5039 *f = '\0';
5041 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5044 void
5045 default_pe_asm_named_section (const char *name, unsigned int flags,
5046 tree decl)
5048 default_coff_asm_named_section (name, flags, decl);
5050 if (flags & SECTION_LINKONCE)
5052 /* Functions may have been compiled at various levels of
5053 optimization so we can't use `same_size' here.
5054 Instead, have the linker pick one. */
5055 fprintf (asm_out_file, "\t.linkonce %s\n",
5056 (flags & SECTION_CODE ? "discard" : "same_size"));
5060 /* The lame default section selector. */
5062 section *
5063 default_select_section (tree decl, int reloc,
5064 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5066 if (DECL_P (decl))
5068 if (decl_readonly_section (decl, reloc))
5069 return readonly_data_section;
5071 else if (TREE_CODE (decl) == CONSTRUCTOR)
5073 if (! ((flag_pic && reloc)
5074 || !TREE_READONLY (decl)
5075 || TREE_SIDE_EFFECTS (decl)
5076 || !TREE_CONSTANT (decl)))
5077 return readonly_data_section;
5079 else if (TREE_CODE (decl) == STRING_CST)
5080 return readonly_data_section;
5081 else if (! (flag_pic && reloc))
5082 return readonly_data_section;
5084 return data_section;
5087 enum section_category
5088 categorize_decl_for_section (tree decl, int reloc, int shlib)
5090 enum section_category ret;
5092 if (TREE_CODE (decl) == FUNCTION_DECL)
5093 return SECCAT_TEXT;
5094 else if (TREE_CODE (decl) == STRING_CST)
5096 if (flag_mudflap) /* or !flag_merge_constants */
5097 return SECCAT_RODATA;
5098 else
5099 return SECCAT_RODATA_MERGE_STR;
5101 else if (TREE_CODE (decl) == VAR_DECL)
5103 if (DECL_INITIAL (decl) == NULL
5104 || DECL_INITIAL (decl) == error_mark_node
5105 || (flag_zero_initialized_in_bss
5106 /* Leave constant zeroes in .rodata so they can be shared. */
5107 && !TREE_READONLY (decl)
5108 && initializer_zerop (DECL_INITIAL (decl))))
5109 ret = SECCAT_BSS;
5110 else if (! TREE_READONLY (decl)
5111 || TREE_SIDE_EFFECTS (decl)
5112 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5114 if (shlib && (reloc & 2))
5115 ret = SECCAT_DATA_REL;
5116 else if (shlib && reloc)
5117 ret = SECCAT_DATA_REL_LOCAL;
5118 else
5119 ret = SECCAT_DATA;
5121 else if (shlib && (reloc & 2))
5122 ret = SECCAT_DATA_REL_RO;
5123 else if (shlib && reloc)
5124 ret = SECCAT_DATA_REL_RO_LOCAL;
5125 else if (reloc || flag_merge_constants < 2)
5126 /* C and C++ don't allow different variables to share the same
5127 location. -fmerge-all-constants allows even that (at the
5128 expense of not conforming). */
5129 ret = SECCAT_RODATA;
5130 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5131 ret = SECCAT_RODATA_MERGE_STR_INIT;
5132 else
5133 ret = SECCAT_RODATA_MERGE_CONST;
5135 else if (TREE_CODE (decl) == CONSTRUCTOR)
5137 if ((shlib && reloc)
5138 || TREE_SIDE_EFFECTS (decl)
5139 || ! TREE_CONSTANT (decl))
5140 ret = SECCAT_DATA;
5141 else
5142 ret = SECCAT_RODATA;
5144 else
5145 ret = SECCAT_RODATA;
5147 /* There are no read-only thread-local sections. */
5148 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5150 /* Note that this would be *just* SECCAT_BSS, except that there's
5151 no concept of a read-only thread-local-data section. */
5152 if (ret == SECCAT_BSS
5153 || (flag_zero_initialized_in_bss
5154 && initializer_zerop (DECL_INITIAL (decl))))
5155 ret = SECCAT_TBSS;
5156 else
5157 ret = SECCAT_TDATA;
5160 /* If the target uses small data sections, select it. */
5161 else if (targetm.in_small_data_p (decl))
5163 if (ret == SECCAT_BSS)
5164 ret = SECCAT_SBSS;
5165 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5166 ret = SECCAT_SRODATA;
5167 else
5168 ret = SECCAT_SDATA;
5171 return ret;
5174 bool
5175 decl_readonly_section (tree decl, int reloc)
5177 return decl_readonly_section_1 (decl, reloc, flag_pic);
5180 bool
5181 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5183 switch (categorize_decl_for_section (decl, reloc, shlib))
5185 case SECCAT_RODATA:
5186 case SECCAT_RODATA_MERGE_STR:
5187 case SECCAT_RODATA_MERGE_STR_INIT:
5188 case SECCAT_RODATA_MERGE_CONST:
5189 case SECCAT_SRODATA:
5190 return true;
5191 break;
5192 default:
5193 return false;
5194 break;
5198 /* Select a section based on the above categorization. */
5200 section *
5201 default_elf_select_section (tree decl, int reloc,
5202 unsigned HOST_WIDE_INT align)
5204 return default_elf_select_section_1 (decl, reloc, align, flag_pic);
5207 section *
5208 default_elf_select_section_1 (tree decl, int reloc,
5209 unsigned HOST_WIDE_INT align, int shlib)
5211 const char *sname;
5212 switch (categorize_decl_for_section (decl, reloc, shlib))
5214 case SECCAT_TEXT:
5215 /* We're not supposed to be called on FUNCTION_DECLs. */
5216 gcc_unreachable ();
5217 case SECCAT_RODATA:
5218 return readonly_data_section;
5219 case SECCAT_RODATA_MERGE_STR:
5220 return mergeable_string_section (decl, align, 0);
5221 case SECCAT_RODATA_MERGE_STR_INIT:
5222 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5223 case SECCAT_RODATA_MERGE_CONST:
5224 return mergeable_constant_section (DECL_MODE (decl), align, 0);
5225 case SECCAT_SRODATA:
5226 sname = ".sdata2";
5227 break;
5228 case SECCAT_DATA:
5229 return data_section;
5230 case SECCAT_DATA_REL:
5231 sname = ".data.rel";
5232 break;
5233 case SECCAT_DATA_REL_LOCAL:
5234 sname = ".data.rel.local";
5235 break;
5236 case SECCAT_DATA_REL_RO:
5237 sname = ".data.rel.ro";
5238 break;
5239 case SECCAT_DATA_REL_RO_LOCAL:
5240 sname = ".data.rel.ro.local";
5241 break;
5242 case SECCAT_SDATA:
5243 sname = ".sdata";
5244 break;
5245 case SECCAT_TDATA:
5246 sname = ".tdata";
5247 break;
5248 case SECCAT_BSS:
5249 if (bss_section)
5250 return bss_section;
5251 sname = ".bss";
5252 break;
5253 case SECCAT_SBSS:
5254 sname = ".sbss";
5255 break;
5256 case SECCAT_TBSS:
5257 sname = ".tbss";
5258 break;
5259 default:
5260 gcc_unreachable ();
5263 if (!DECL_P (decl))
5264 decl = NULL_TREE;
5265 return get_named_section (decl, sname, reloc);
5268 /* Construct a unique section name based on the decl name and the
5269 categorization performed above. */
5271 void
5272 default_unique_section (tree decl, int reloc)
5274 default_unique_section_1 (decl, reloc, flag_pic);
5277 void
5278 default_unique_section_1 (tree decl, int reloc, int shlib)
5280 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
5281 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5282 const char *prefix, *name;
5283 size_t nlen, plen;
5284 char *string;
5286 switch (categorize_decl_for_section (decl, reloc, shlib))
5288 case SECCAT_TEXT:
5289 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5290 break;
5291 case SECCAT_RODATA:
5292 case SECCAT_RODATA_MERGE_STR:
5293 case SECCAT_RODATA_MERGE_STR_INIT:
5294 case SECCAT_RODATA_MERGE_CONST:
5295 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5296 break;
5297 case SECCAT_SRODATA:
5298 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5299 break;
5300 case SECCAT_DATA:
5301 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5302 break;
5303 case SECCAT_DATA_REL:
5304 prefix = one_only ? ".gnu.linkonce.d.rel." : ".data.rel.";
5305 break;
5306 case SECCAT_DATA_REL_LOCAL:
5307 prefix = one_only ? ".gnu.linkonce.d.rel.local." : ".data.rel.local.";
5308 break;
5309 case SECCAT_DATA_REL_RO:
5310 prefix = one_only ? ".gnu.linkonce.d.rel.ro." : ".data.rel.ro.";
5311 break;
5312 case SECCAT_DATA_REL_RO_LOCAL:
5313 prefix = one_only ? ".gnu.linkonce.d.rel.ro.local."
5314 : ".data.rel.ro.local.";
5315 break;
5316 case SECCAT_SDATA:
5317 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5318 break;
5319 case SECCAT_BSS:
5320 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5321 break;
5322 case SECCAT_SBSS:
5323 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5324 break;
5325 case SECCAT_TDATA:
5326 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5327 break;
5328 case SECCAT_TBSS:
5329 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5330 break;
5331 default:
5332 gcc_unreachable ();
5334 plen = strlen (prefix);
5336 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5337 name = targetm.strip_name_encoding (name);
5338 nlen = strlen (name);
5340 string = alloca (nlen + plen + 1);
5341 memcpy (string, prefix, plen);
5342 memcpy (string + plen, name, nlen + 1);
5344 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5347 section *
5348 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5349 rtx x,
5350 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5352 if (flag_pic)
5353 switch (GET_CODE (x))
5355 case CONST:
5356 case SYMBOL_REF:
5357 case LABEL_REF:
5358 return data_section;
5360 default:
5361 break;
5364 return readonly_data_section;
5367 section *
5368 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5369 unsigned HOST_WIDE_INT align)
5371 /* ??? Handle small data here somehow. */
5373 if (flag_pic)
5374 switch (GET_CODE (x))
5376 case CONST:
5377 case SYMBOL_REF:
5378 return get_named_section (NULL, ".data.rel.ro", 3);
5380 case LABEL_REF:
5381 return get_named_section (NULL, ".data.rel.ro.local", 1);
5383 default:
5384 break;
5387 return mergeable_constant_section (mode, align, 0);
5390 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
5392 void
5393 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5395 rtx symbol;
5396 int flags;
5398 /* Careful not to prod global register variables. */
5399 if (!MEM_P (rtl))
5400 return;
5401 symbol = XEXP (rtl, 0);
5402 if (GET_CODE (symbol) != SYMBOL_REF)
5403 return;
5405 flags = 0;
5406 if (TREE_CODE (decl) == FUNCTION_DECL)
5407 flags |= SYMBOL_FLAG_FUNCTION;
5408 if (targetm.binds_local_p (decl))
5409 flags |= SYMBOL_FLAG_LOCAL;
5410 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5411 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5412 else if (targetm.in_small_data_p (decl))
5413 flags |= SYMBOL_FLAG_SMALL;
5414 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
5415 being PUBLIC, the thing *must* be defined in this translation unit.
5416 Prevent this buglet from being propagated into rtl code as well. */
5417 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5418 flags |= SYMBOL_FLAG_EXTERNAL;
5420 SYMBOL_REF_FLAGS (symbol) = flags;
5423 /* By default, we do nothing for encode_section_info, so we need not
5424 do anything but discard the '*' marker. */
5426 const char *
5427 default_strip_name_encoding (const char *str)
5429 return str + (*str == '*');
5432 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5433 wrt cross-module name binding. */
5435 bool
5436 default_binds_local_p (tree exp)
5438 return default_binds_local_p_1 (exp, flag_shlib);
5441 bool
5442 default_binds_local_p_1 (tree exp, int shlib)
5444 bool local_p;
5446 /* A non-decl is an entry in the constant pool. */
5447 if (!DECL_P (exp))
5448 local_p = true;
5449 /* Weakrefs may not bind locally, even though the weakref itself is
5450 always static and therefore local. */
5451 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
5452 local_p = false;
5453 /* Static variables are always local. */
5454 else if (! TREE_PUBLIC (exp))
5455 local_p = true;
5456 /* A variable is local if the user has said explicitly that it will
5457 be. */
5458 else if (DECL_VISIBILITY_SPECIFIED (exp)
5459 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5460 local_p = true;
5461 /* Variables defined outside this object might not be local. */
5462 else if (DECL_EXTERNAL (exp))
5463 local_p = false;
5464 /* If defined in this object and visibility is not default, must be
5465 local. */
5466 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5467 local_p = true;
5468 /* Default visibility weak data can be overridden by a strong symbol
5469 in another module and so are not local. */
5470 else if (DECL_WEAK (exp))
5471 local_p = false;
5472 /* If PIC, then assume that any global name can be overridden by
5473 symbols resolved from other modules. */
5474 else if (shlib)
5475 local_p = false;
5476 /* Uninitialized COMMON variable may be unified with symbols
5477 resolved from other modules. */
5478 else if (DECL_COMMON (exp)
5479 && (DECL_INITIAL (exp) == NULL
5480 || DECL_INITIAL (exp) == error_mark_node))
5481 local_p = false;
5482 /* Otherwise we're left with initialized (or non-common) global data
5483 which is of necessity defined locally. */
5484 else
5485 local_p = true;
5487 return local_p;
5490 /* Determine whether or not a pointer mode is valid. Assume defaults
5491 of ptr_mode or Pmode - can be overridden. */
5492 bool
5493 default_valid_pointer_mode (enum machine_mode mode)
5495 return (mode == ptr_mode || mode == Pmode);
5498 /* Default function to output code that will globalize a label. A
5499 target must define GLOBAL_ASM_OP or provide its own function to
5500 globalize a label. */
5501 #ifdef GLOBAL_ASM_OP
5502 void
5503 default_globalize_label (FILE * stream, const char *name)
5505 fputs (GLOBAL_ASM_OP, stream);
5506 assemble_name (stream, name);
5507 putc ('\n', stream);
5509 #endif /* GLOBAL_ASM_OP */
5511 /* Default function to output a label for unwind information. The
5512 default is to do nothing. A target that needs nonlocal labels for
5513 unwind information must provide its own function to do this. */
5514 void
5515 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5516 tree decl ATTRIBUTE_UNUSED,
5517 int for_eh ATTRIBUTE_UNUSED,
5518 int empty ATTRIBUTE_UNUSED)
5522 /* Default function to output a label to divide up the exception table.
5523 The default is to do nothing. A target that needs/wants to divide
5524 up the table must provide it's own function to do this. */
5525 void
5526 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
5530 /* This is how to output an internal numbered label where PREFIX is
5531 the class of label and LABELNO is the number within the class. */
5533 void
5534 default_internal_label (FILE *stream, const char *prefix,
5535 unsigned long labelno)
5537 char *const buf = alloca (40 + strlen (prefix));
5538 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5539 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5542 /* This is the default behavior at the beginning of a file. It's
5543 controlled by two other target-hook toggles. */
5544 void
5545 default_file_start (void)
5547 if (targetm.file_start_app_off && !flag_verbose_asm)
5548 fputs (ASM_APP_OFF, asm_out_file);
5550 if (targetm.file_start_file_directive)
5551 output_file_directive (asm_out_file, main_input_filename);
5554 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5555 which emits a special section directive used to indicate whether or
5556 not this object file needs an executable stack. This is primarily
5557 a GNU extension to ELF but could be used on other targets. */
5559 int trampolines_created;
5561 void
5562 file_end_indicate_exec_stack (void)
5564 unsigned int flags = SECTION_DEBUG;
5565 if (trampolines_created)
5566 flags |= SECTION_CODE;
5568 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
5571 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
5572 a get_unnamed_section callback. */
5574 void
5575 output_section_asm_op (const void *directive)
5577 fprintf (asm_out_file, "%s\n", (const char *) directive);
5580 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
5581 the current section is NEW_SECTION. */
5583 void
5584 switch_to_section (section *new_section)
5586 if (in_section == new_section)
5587 return;
5589 if (new_section->common.flags & SECTION_FORGET)
5590 in_section = NULL;
5591 else
5592 in_section = new_section;
5594 if (new_section->common.flags & SECTION_NAMED)
5596 if (cfun
5597 && !cfun->unlikely_text_section_name
5598 && strcmp (new_section->named.name,
5599 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5600 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
5602 targetm.asm_out.named_section (new_section->named.name,
5603 new_section->named.common.flags,
5604 new_section->named.decl);
5606 else
5607 new_section->unnamed.callback (new_section->unnamed.data);
5609 new_section->common.flags |= SECTION_DECLARED;
5612 #include "gt-varasm.h"