fix entries ordering
[official-gcc.git] / gcc / varasm.c
blobb398b59a6b6192d039fa85c7dc79e7162b8c6190
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
24 /* This file handles generation of all the assembler code
25 *except* the instructions of a function.
26 This includes declarations of variables and their initial values.
28 We also output the assembler code for constants stored in memory
29 and are responsible for combining constants with the same value. */
31 #include "config.h"
32 #include "system.h"
33 #include "coretypes.h"
34 #include "tm.h"
35 #include "rtl.h"
36 #include "tree.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "hard-reg-set.h"
41 #include "regs.h"
42 #include "real.h"
43 #include "output.h"
44 #include "toplev.h"
45 #include "hashtab.h"
46 #include "c-pragma.h"
47 #include "ggc.h"
48 #include "langhooks.h"
49 #include "tm_p.h"
50 #include "debug.h"
51 #include "target.h"
52 #include "tree-mudflap.h"
53 #include "cgraph.h"
54 #include "cfglayout.h"
55 #include "basic-block.h"
57 #ifdef XCOFF_DEBUGGING_INFO
58 #include "xcoffout.h" /* Needed for external data
59 declarations for e.g. AIX 4.x. */
60 #endif
62 /* The (assembler) name of the first globally-visible object output. */
63 extern GTY(()) const char *first_global_object_name;
64 extern GTY(()) const char *weak_global_object_name;
66 const char *first_global_object_name;
67 const char *weak_global_object_name;
69 struct addr_const;
70 struct constant_descriptor_rtx;
71 struct rtx_constant_pool;
73 struct varasm_status GTY(())
75 /* If we're using a per-function constant pool, this is it. */
76 struct rtx_constant_pool *pool;
78 /* Number of tree-constants deferred during the expansion of this
79 function. */
80 unsigned int deferred_constants;
83 #define n_deferred_constants (cfun->varasm->deferred_constants)
85 /* Number for making the label on the next
86 constant that is stored in memory. */
88 static GTY(()) int const_labelno;
90 /* Carry information from ASM_DECLARE_OBJECT_NAME
91 to ASM_FINISH_DECLARE_OBJECT. */
93 int size_directive_output;
95 /* The last decl for which assemble_variable was called,
96 if it did ASM_DECLARE_OBJECT_NAME.
97 If the last call to assemble_variable didn't do that,
98 this holds 0. */
100 tree last_assemble_variable_decl;
102 /* The following global variable indicates if the first basic block
103 in a function belongs to the cold partition or not. */
105 bool first_function_block_is_cold;
107 /* We give all constants their own alias set. Perhaps redundant with
108 MEM_READONLY_P, but pre-dates it. */
110 static HOST_WIDE_INT const_alias_set;
112 static const char *strip_reg_name (const char *);
113 static int contains_pointers_p (tree);
114 #ifdef ASM_OUTPUT_EXTERNAL
115 static bool incorporeal_function_p (tree);
116 #endif
117 static void decode_addr_const (tree, struct addr_const *);
118 static hashval_t const_desc_hash (const void *);
119 static int const_desc_eq (const void *, const void *);
120 static hashval_t const_hash_1 (const tree);
121 static int compare_constant (const tree, const tree);
122 static tree copy_constant (tree);
123 static void output_constant_def_contents (rtx);
124 static void output_addressed_constants (tree);
125 static unsigned HOST_WIDE_INT array_size_for_constructor (tree);
126 static unsigned min_align (unsigned, unsigned);
127 static void output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int);
128 static void globalize_decl (tree);
129 static void maybe_assemble_visibility (tree);
130 #ifdef BSS_SECTION_ASM_OP
131 #ifdef ASM_OUTPUT_BSS
132 static void asm_output_bss (FILE *, tree, const char *,
133 unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT);
134 #endif
135 #ifdef ASM_OUTPUT_ALIGNED_BSS
136 static void asm_output_aligned_bss (FILE *, tree, const char *,
137 unsigned HOST_WIDE_INT, int)
138 ATTRIBUTE_UNUSED;
139 #endif
140 #endif /* BSS_SECTION_ASM_OP */
141 static bool asm_emit_uninitialised (tree, const char*,
142 unsigned HOST_WIDE_INT,
143 unsigned HOST_WIDE_INT);
144 static void mark_weak (tree);
146 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
147 section *text_section;
148 section *data_section;
149 section *readonly_data_section;
150 section *sdata_section;
151 section *ctors_section;
152 section *dtors_section;
153 section *bss_section;
154 section *sbss_section;
156 /* The section that holds the main exception table, when known. The section
157 is set either by the target's init_sections hook or by the first call to
158 switch_to_exception_section. */
159 section *exception_section;
161 /* The section that holds the DWARF2 frame unwind information, when known.
162 The section is set either by the target's init_sections hook or by the
163 first call to switch_to_eh_frame_section. */
164 section *eh_frame_section;
166 /* asm_out_file's current section. This is NULL if no section has yet
167 been selected or if we lose track of what the current section is. */
168 section *in_section;
170 /* 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 CHAR_TYPE:
3752 case BOOLEAN_TYPE:
3753 case INTEGER_TYPE:
3754 case ENUMERAL_TYPE:
3755 case POINTER_TYPE:
3756 case REFERENCE_TYPE:
3757 case OFFSET_TYPE:
3758 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
3759 EXPAND_INITIALIZER),
3760 MIN (size, thissize), align, 0))
3761 error ("initializer for integer value is too complicated");
3762 break;
3764 case REAL_TYPE:
3765 if (TREE_CODE (exp) != REAL_CST)
3766 error ("initializer for floating value is not a floating constant");
3768 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
3769 break;
3771 case COMPLEX_TYPE:
3772 output_constant (TREE_REALPART (exp), thissize / 2, align);
3773 output_constant (TREE_IMAGPART (exp), thissize / 2,
3774 min_align (align, BITS_PER_UNIT * (thissize / 2)));
3775 break;
3777 case ARRAY_TYPE:
3778 case VECTOR_TYPE:
3779 switch (TREE_CODE (exp))
3781 case CONSTRUCTOR:
3782 output_constructor (exp, size, align);
3783 return;
3784 case STRING_CST:
3785 thissize = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp),
3786 size);
3787 assemble_string (TREE_STRING_POINTER (exp), thissize);
3788 break;
3790 case VECTOR_CST:
3792 int elt_size;
3793 tree link;
3794 unsigned int nalign;
3795 enum machine_mode inner;
3797 inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
3798 nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
3800 elt_size = GET_MODE_SIZE (inner);
3802 link = TREE_VECTOR_CST_ELTS (exp);
3803 output_constant (TREE_VALUE (link), elt_size, align);
3804 while ((link = TREE_CHAIN (link)) != NULL)
3805 output_constant (TREE_VALUE (link), elt_size, nalign);
3806 break;
3808 default:
3809 gcc_unreachable ();
3811 break;
3813 case RECORD_TYPE:
3814 case UNION_TYPE:
3815 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
3816 output_constructor (exp, size, align);
3817 return;
3819 case ERROR_MARK:
3820 return;
3822 default:
3823 gcc_unreachable ();
3826 if (size > thissize)
3827 assemble_zeros (size - thissize);
3831 /* Subroutine of output_constructor, used for computing the size of
3832 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
3833 type with an unspecified upper bound. */
3835 static unsigned HOST_WIDE_INT
3836 array_size_for_constructor (tree val)
3838 tree max_index, i;
3839 unsigned HOST_WIDE_INT cnt;
3840 tree index, value;
3842 /* This code used to attempt to handle string constants that are not
3843 arrays of single-bytes, but nothing else does, so there's no point in
3844 doing it here. */
3845 if (TREE_CODE (val) == STRING_CST)
3846 return TREE_STRING_LENGTH (val);
3848 max_index = NULL_TREE;
3849 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
3851 if (TREE_CODE (index) == RANGE_EXPR)
3852 index = TREE_OPERAND (index, 1);
3853 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
3854 max_index = index;
3857 if (max_index == NULL_TREE)
3858 return 0;
3860 /* Compute the total number of array elements. */
3861 i = size_binop (MINUS_EXPR, convert (sizetype, max_index),
3862 convert (sizetype,
3863 TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)))));
3864 i = size_binop (PLUS_EXPR, i, build_int_cst (sizetype, 1));
3866 /* Multiply by the array element unit size to find number of bytes. */
3867 i = size_binop (MULT_EXPR, i, TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
3869 return tree_low_cst (i, 1);
3872 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
3873 Generate at least SIZE bytes, padding if necessary. */
3875 static void
3876 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
3877 unsigned int align)
3879 tree type = TREE_TYPE (exp);
3880 tree field = 0;
3881 tree min_index = 0;
3882 /* Number of bytes output or skipped so far.
3883 In other words, current position within the constructor. */
3884 HOST_WIDE_INT total_bytes = 0;
3885 /* Nonzero means BYTE contains part of a byte, to be output. */
3886 int byte_buffer_in_use = 0;
3887 int byte = 0;
3888 unsigned HOST_WIDE_INT cnt;
3889 constructor_elt *ce;
3891 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
3893 if (TREE_CODE (type) == RECORD_TYPE)
3894 field = TYPE_FIELDS (type);
3896 if (TREE_CODE (type) == ARRAY_TYPE
3897 && TYPE_DOMAIN (type) != 0)
3898 min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (type));
3900 /* As LINK goes through the elements of the constant,
3901 FIELD goes through the structure fields, if the constant is a structure.
3902 if the constant is a union, then we override this,
3903 by getting the field from the TREE_LIST element.
3904 But the constant could also be an array. Then FIELD is zero.
3906 There is always a maximum of one element in the chain LINK for unions
3907 (even if the initializer in a source program incorrectly contains
3908 more one). */
3909 for (cnt = 0;
3910 VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), cnt, ce);
3911 cnt++, field = field ? TREE_CHAIN (field) : 0)
3913 tree val = ce->value;
3914 tree index = 0;
3916 /* The element in a union constructor specifies the proper field
3917 or index. */
3918 if ((TREE_CODE (type) == RECORD_TYPE || TREE_CODE (type) == UNION_TYPE
3919 || TREE_CODE (type) == QUAL_UNION_TYPE)
3920 && ce->index != 0)
3921 field = ce->index;
3923 else if (TREE_CODE (type) == ARRAY_TYPE)
3924 index = ce->index;
3926 #ifdef ASM_COMMENT_START
3927 if (field && flag_verbose_asm)
3928 fprintf (asm_out_file, "%s %s:\n",
3929 ASM_COMMENT_START,
3930 DECL_NAME (field)
3931 ? IDENTIFIER_POINTER (DECL_NAME (field))
3932 : "<anonymous>");
3933 #endif
3935 /* Eliminate the marker that makes a cast not be an lvalue. */
3936 if (val != 0)
3937 STRIP_NOPS (val);
3939 if (index && TREE_CODE (index) == RANGE_EXPR)
3941 unsigned HOST_WIDE_INT fieldsize
3942 = int_size_in_bytes (TREE_TYPE (type));
3943 HOST_WIDE_INT lo_index = tree_low_cst (TREE_OPERAND (index, 0), 0);
3944 HOST_WIDE_INT hi_index = tree_low_cst (TREE_OPERAND (index, 1), 0);
3945 HOST_WIDE_INT index;
3946 unsigned int align2 = min_align (align, fieldsize * BITS_PER_UNIT);
3948 for (index = lo_index; index <= hi_index; index++)
3950 /* Output the element's initial value. */
3951 if (val == 0)
3952 assemble_zeros (fieldsize);
3953 else
3954 output_constant (val, fieldsize, align2);
3956 /* Count its size. */
3957 total_bytes += fieldsize;
3960 else if (field == 0 || !DECL_BIT_FIELD (field))
3962 /* An element that is not a bit-field. */
3964 unsigned HOST_WIDE_INT fieldsize;
3965 /* Since this structure is static,
3966 we know the positions are constant. */
3967 HOST_WIDE_INT pos = field ? int_byte_position (field) : 0;
3968 unsigned int align2;
3970 if (index != 0)
3971 pos = (tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (val)), 1)
3972 * (tree_low_cst (index, 0) - tree_low_cst (min_index, 0)));
3974 /* Output any buffered-up bit-fields preceding this element. */
3975 if (byte_buffer_in_use)
3977 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
3978 total_bytes++;
3979 byte_buffer_in_use = 0;
3982 /* Advance to offset of this element.
3983 Note no alignment needed in an array, since that is guaranteed
3984 if each element has the proper size. */
3985 if ((field != 0 || index != 0) && pos != total_bytes)
3987 gcc_assert (pos >= total_bytes);
3988 assemble_zeros (pos - total_bytes);
3989 total_bytes = pos;
3992 /* Find the alignment of this element. */
3993 align2 = min_align (align, BITS_PER_UNIT * pos);
3995 /* Determine size this element should occupy. */
3996 if (field)
3998 fieldsize = 0;
4000 /* If this is an array with an unspecified upper bound,
4001 the initializer determines the size. */
4002 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4003 but we cannot do this until the deprecated support for
4004 initializing zero-length array members is removed. */
4005 if (TREE_CODE (TREE_TYPE (field)) == ARRAY_TYPE
4006 && TYPE_DOMAIN (TREE_TYPE (field))
4007 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (field))))
4009 fieldsize = array_size_for_constructor (val);
4010 /* Given a non-empty initialization, this field had
4011 better be last. */
4012 gcc_assert (!fieldsize || !TREE_CHAIN (field));
4014 else if (DECL_SIZE_UNIT (field))
4016 /* ??? This can't be right. If the decl size overflows
4017 a host integer we will silently emit no data. */
4018 if (host_integerp (DECL_SIZE_UNIT (field), 1))
4019 fieldsize = tree_low_cst (DECL_SIZE_UNIT (field), 1);
4022 else
4023 fieldsize = int_size_in_bytes (TREE_TYPE (type));
4025 /* Output the element's initial value. */
4026 if (val == 0)
4027 assemble_zeros (fieldsize);
4028 else
4029 output_constant (val, fieldsize, align2);
4031 /* Count its size. */
4032 total_bytes += fieldsize;
4034 else if (val != 0 && TREE_CODE (val) != INTEGER_CST)
4035 error ("invalid initial value for member %qs",
4036 IDENTIFIER_POINTER (DECL_NAME (field)));
4037 else
4039 /* Element that is a bit-field. */
4041 HOST_WIDE_INT next_offset = int_bit_position (field);
4042 HOST_WIDE_INT end_offset
4043 = (next_offset + tree_low_cst (DECL_SIZE (field), 1));
4045 if (val == 0)
4046 val = integer_zero_node;
4048 /* If this field does not start in this (or, next) byte,
4049 skip some bytes. */
4050 if (next_offset / BITS_PER_UNIT != total_bytes)
4052 /* Output remnant of any bit field in previous bytes. */
4053 if (byte_buffer_in_use)
4055 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4056 total_bytes++;
4057 byte_buffer_in_use = 0;
4060 /* If still not at proper byte, advance to there. */
4061 if (next_offset / BITS_PER_UNIT != total_bytes)
4063 gcc_assert (next_offset / BITS_PER_UNIT >= total_bytes);
4064 assemble_zeros (next_offset / BITS_PER_UNIT - total_bytes);
4065 total_bytes = next_offset / BITS_PER_UNIT;
4069 if (! byte_buffer_in_use)
4070 byte = 0;
4072 /* We must split the element into pieces that fall within
4073 separate bytes, and combine each byte with previous or
4074 following bit-fields. */
4076 /* next_offset is the offset n fbits from the beginning of
4077 the structure to the next bit of this element to be processed.
4078 end_offset is the offset of the first bit past the end of
4079 this element. */
4080 while (next_offset < end_offset)
4082 int this_time;
4083 int shift;
4084 HOST_WIDE_INT value;
4085 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
4086 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
4088 /* Advance from byte to byte
4089 within this element when necessary. */
4090 while (next_byte != total_bytes)
4092 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4093 total_bytes++;
4094 byte = 0;
4097 /* Number of bits we can process at once
4098 (all part of the same byte). */
4099 this_time = MIN (end_offset - next_offset,
4100 BITS_PER_UNIT - next_bit);
4101 if (BYTES_BIG_ENDIAN)
4103 /* On big-endian machine, take the most significant bits
4104 first (of the bits that are significant)
4105 and put them into bytes from the most significant end. */
4106 shift = end_offset - next_offset - this_time;
4108 /* Don't try to take a bunch of bits that cross
4109 the word boundary in the INTEGER_CST. We can
4110 only select bits from the LOW or HIGH part
4111 not from both. */
4112 if (shift < HOST_BITS_PER_WIDE_INT
4113 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4115 this_time = shift + this_time - HOST_BITS_PER_WIDE_INT;
4116 shift = HOST_BITS_PER_WIDE_INT;
4119 /* Now get the bits from the appropriate constant word. */
4120 if (shift < HOST_BITS_PER_WIDE_INT)
4121 value = TREE_INT_CST_LOW (val);
4122 else
4124 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4125 value = TREE_INT_CST_HIGH (val);
4126 shift -= HOST_BITS_PER_WIDE_INT;
4129 /* Get the result. This works only when:
4130 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4131 byte |= (((value >> shift)
4132 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4133 << (BITS_PER_UNIT - this_time - next_bit));
4135 else
4137 /* On little-endian machines,
4138 take first the least significant bits of the value
4139 and pack them starting at the least significant
4140 bits of the bytes. */
4141 shift = next_offset - int_bit_position (field);
4143 /* Don't try to take a bunch of bits that cross
4144 the word boundary in the INTEGER_CST. We can
4145 only select bits from the LOW or HIGH part
4146 not from both. */
4147 if (shift < HOST_BITS_PER_WIDE_INT
4148 && shift + this_time > HOST_BITS_PER_WIDE_INT)
4149 this_time = (HOST_BITS_PER_WIDE_INT - shift);
4151 /* Now get the bits from the appropriate constant word. */
4152 if (shift < HOST_BITS_PER_WIDE_INT)
4153 value = TREE_INT_CST_LOW (val);
4154 else
4156 gcc_assert (shift < 2 * HOST_BITS_PER_WIDE_INT);
4157 value = TREE_INT_CST_HIGH (val);
4158 shift -= HOST_BITS_PER_WIDE_INT;
4161 /* Get the result. This works only when:
4162 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
4163 byte |= (((value >> shift)
4164 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
4165 << next_bit);
4168 next_offset += this_time;
4169 byte_buffer_in_use = 1;
4174 if (byte_buffer_in_use)
4176 assemble_integer (GEN_INT (byte), 1, BITS_PER_UNIT, 1);
4177 total_bytes++;
4180 if ((unsigned HOST_WIDE_INT)total_bytes < size)
4181 assemble_zeros (size - total_bytes);
4184 /* This TREE_LIST contains any weak symbol declarations waiting
4185 to be emitted. */
4186 static GTY(()) tree weak_decls;
4188 /* Mark DECL as weak. */
4190 static void
4191 mark_weak (tree decl)
4193 DECL_WEAK (decl) = 1;
4195 if (DECL_RTL_SET_P (decl)
4196 && MEM_P (DECL_RTL (decl))
4197 && XEXP (DECL_RTL (decl), 0)
4198 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
4199 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
4202 /* Merge weak status between NEWDECL and OLDDECL. */
4204 void
4205 merge_weak (tree newdecl, tree olddecl)
4207 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
4209 if (DECL_WEAK (newdecl) && SUPPORTS_WEAK)
4211 tree *pwd;
4212 /* We put the NEWDECL on the weak_decls list at some point
4213 and OLDDECL as well. Keep just OLDDECL on the list. */
4214 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
4215 if (TREE_VALUE (*pwd) == newdecl)
4217 *pwd = TREE_CHAIN (*pwd);
4218 break;
4221 return;
4224 if (DECL_WEAK (newdecl))
4226 tree wd;
4228 /* NEWDECL is weak, but OLDDECL is not. */
4230 /* If we already output the OLDDECL, we're in trouble; we can't
4231 go back and make it weak. This error cannot caught in
4232 declare_weak because the NEWDECL and OLDDECL was not yet
4233 been merged; therefore, TREE_ASM_WRITTEN was not set. */
4234 if (TREE_ASM_WRITTEN (olddecl))
4235 error ("weak declaration of %q+D must precede definition",
4236 newdecl);
4238 /* If we've already generated rtl referencing OLDDECL, we may
4239 have done so in a way that will not function properly with
4240 a weak symbol. */
4241 else if (TREE_USED (olddecl)
4242 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)))
4243 warning (0, "weak declaration of %q+D after first use results "
4244 "in unspecified behavior", newdecl);
4246 if (SUPPORTS_WEAK)
4248 /* We put the NEWDECL on the weak_decls list at some point.
4249 Replace it with the OLDDECL. */
4250 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
4251 if (TREE_VALUE (wd) == newdecl)
4253 TREE_VALUE (wd) = olddecl;
4254 break;
4256 /* We may not find the entry on the list. If NEWDECL is a
4257 weak alias, then we will have already called
4258 globalize_decl to remove the entry; in that case, we do
4259 not need to do anything. */
4262 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
4263 mark_weak (olddecl);
4265 else
4266 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
4267 weak. Just update NEWDECL to indicate that it's weak too. */
4268 mark_weak (newdecl);
4271 /* Declare DECL to be a weak symbol. */
4273 void
4274 declare_weak (tree decl)
4276 if (! TREE_PUBLIC (decl))
4277 error ("weak declaration of %q+D must be public", decl);
4278 else if (TREE_CODE (decl) == FUNCTION_DECL && TREE_ASM_WRITTEN (decl))
4279 error ("weak declaration of %q+D must precede definition", decl);
4280 else if (SUPPORTS_WEAK)
4282 if (! DECL_WEAK (decl))
4283 weak_decls = tree_cons (NULL, decl, weak_decls);
4285 else
4286 warning (0, "weak declaration of %q+D not supported", decl);
4288 mark_weak (decl);
4291 static void
4292 weak_finish_1 (tree decl)
4294 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
4295 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4296 #endif
4298 if (! TREE_USED (decl))
4299 return;
4301 #ifdef ASM_WEAKEN_DECL
4302 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
4303 #else
4304 #ifdef ASM_WEAKEN_LABEL
4305 ASM_WEAKEN_LABEL (asm_out_file, name);
4306 #else
4307 #ifdef ASM_OUTPUT_WEAK_ALIAS
4309 static bool warn_once = 0;
4310 if (! warn_once)
4312 warning (0, "only weak aliases are supported in this configuration");
4313 warn_once = 1;
4315 return;
4317 #endif
4318 #endif
4319 #endif
4322 /* This TREE_LIST contains weakref targets. */
4324 static GTY(()) tree weakref_targets;
4326 /* Forward declaration. */
4327 static tree find_decl_and_mark_needed (tree decl, tree target);
4329 /* Emit any pending weak declarations. */
4331 void
4332 weak_finish (void)
4334 tree t;
4336 for (t = weakref_targets; t; t = TREE_CHAIN (t))
4338 tree alias_decl = TREE_PURPOSE (t);
4339 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
4341 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
4342 /* Remove alias_decl from the weak list, but leave entries for
4343 the target alone. */
4344 target = NULL_TREE;
4345 #ifndef ASM_OUTPUT_WEAKREF
4346 else if (! TREE_SYMBOL_REFERENCED (target))
4348 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
4349 defined, otherwise we and weak_finish_1 would use a
4350 different macros. */
4351 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
4352 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
4353 # else
4354 tree decl = find_decl_and_mark_needed (alias_decl, target);
4356 if (! decl)
4358 decl = build_decl (TREE_CODE (alias_decl), target,
4359 TREE_TYPE (alias_decl));
4361 DECL_EXTERNAL (decl) = 1;
4362 TREE_PUBLIC (decl) = 1;
4363 DECL_ARTIFICIAL (decl) = 1;
4364 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
4365 TREE_USED (decl) = 1;
4368 weak_finish_1 (decl);
4369 # endif
4371 #endif
4374 tree *p;
4375 tree t2;
4377 /* Remove the alias and the target from the pending weak list
4378 so that we do not emit any .weak directives for the former,
4379 nor multiple .weak directives for the latter. */
4380 for (p = &weak_decls; (t2 = *p) ; )
4382 if (TREE_VALUE (t2) == alias_decl
4383 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
4384 *p = TREE_CHAIN (t2);
4385 else
4386 p = &TREE_CHAIN (t2);
4389 /* Remove other weakrefs to the same target, to speed things up. */
4390 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
4392 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
4393 *p = TREE_CHAIN (t2);
4394 else
4395 p = &TREE_CHAIN (t2);
4400 for (t = weak_decls; t; t = TREE_CHAIN (t))
4402 tree decl = TREE_VALUE (t);
4404 weak_finish_1 (decl);
4408 /* Emit the assembly bits to indicate that DECL is globally visible. */
4410 static void
4411 globalize_decl (tree decl)
4413 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
4415 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
4416 if (DECL_WEAK (decl))
4418 tree *p, t;
4420 #ifdef ASM_WEAKEN_DECL
4421 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
4422 #else
4423 ASM_WEAKEN_LABEL (asm_out_file, name);
4424 #endif
4426 /* Remove this function from the pending weak list so that
4427 we do not emit multiple .weak directives for it. */
4428 for (p = &weak_decls; (t = *p) ; )
4430 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4431 *p = TREE_CHAIN (t);
4432 else
4433 p = &TREE_CHAIN (t);
4436 /* Remove weakrefs to the same target from the pending weakref
4437 list, for the same reason. */
4438 for (p = &weakref_targets; (t = *p) ; )
4440 if (DECL_ASSEMBLER_NAME (decl)
4441 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4442 *p = TREE_CHAIN (t);
4443 else
4444 p = &TREE_CHAIN (t);
4447 return;
4449 #elif defined(ASM_MAKE_LABEL_LINKONCE)
4450 if (DECL_ONE_ONLY (decl))
4451 ASM_MAKE_LABEL_LINKONCE (asm_out_file, name);
4452 #endif
4454 targetm.asm_out.globalize_label (asm_out_file, name);
4457 /* We have to be able to tell cgraph about the needed-ness of the target
4458 of an alias. This requires that the decl have been defined. Aliases
4459 that precede their definition have to be queued for later processing. */
4461 typedef struct alias_pair GTY(())
4463 tree decl;
4464 tree target;
4465 } alias_pair;
4467 /* Define gc'd vector type. */
4468 DEF_VEC_O(alias_pair);
4469 DEF_VEC_ALLOC_O(alias_pair,gc);
4471 static GTY(()) VEC(alias_pair,gc) *alias_pairs;
4473 /* Given an assembly name, find the decl it is associated with. At the
4474 same time, mark it needed for cgraph. */
4476 static tree
4477 find_decl_and_mark_needed (tree decl, tree target)
4479 struct cgraph_node *fnode = NULL;
4480 struct cgraph_varpool_node *vnode = NULL;
4482 if (TREE_CODE (decl) == FUNCTION_DECL)
4484 fnode = cgraph_node_for_asm (target);
4485 if (fnode == NULL)
4486 vnode = cgraph_varpool_node_for_asm (target);
4488 else
4490 vnode = cgraph_varpool_node_for_asm (target);
4491 if (vnode == NULL)
4492 fnode = cgraph_node_for_asm (target);
4495 if (fnode)
4497 /* We can't mark function nodes as used after cgraph global info
4498 is finished. This wouldn't generally be necessary, but C++
4499 virtual table thunks are introduced late in the game and
4500 might seem like they need marking, although in fact they
4501 don't. */
4502 if (! cgraph_global_info_ready)
4503 cgraph_mark_needed_node (fnode);
4504 return fnode->decl;
4506 else if (vnode)
4508 cgraph_varpool_mark_needed_node (vnode);
4509 return vnode->decl;
4511 else
4512 return NULL_TREE;
4515 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
4516 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
4517 tree node is DECL to have the value of the tree node TARGET. */
4519 static void
4520 do_assemble_alias (tree decl, tree target)
4522 if (TREE_ASM_WRITTEN (decl))
4523 return;
4525 TREE_ASM_WRITTEN (decl) = 1;
4526 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
4528 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4530 ultimate_transparent_alias_target (&target);
4532 if (!TREE_SYMBOL_REFERENCED (target))
4533 weakref_targets = tree_cons (decl, target, weakref_targets);
4535 #ifdef ASM_OUTPUT_WEAKREF
4536 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
4537 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4538 IDENTIFIER_POINTER (target));
4539 #else
4540 if (!SUPPORTS_WEAK)
4542 error ("%Jweakref is not supported in this configuration", decl);
4543 return;
4545 #endif
4546 return;
4549 #ifdef ASM_OUTPUT_DEF
4550 /* Make name accessible from other files, if appropriate. */
4552 if (TREE_PUBLIC (decl))
4554 globalize_decl (decl);
4555 maybe_assemble_visibility (decl);
4558 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
4559 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
4560 # else
4561 ASM_OUTPUT_DEF (asm_out_file,
4562 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)),
4563 IDENTIFIER_POINTER (target));
4564 # endif
4565 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
4567 const char *name;
4568 tree *p, t;
4570 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4571 # ifdef ASM_WEAKEN_DECL
4572 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
4573 # else
4574 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
4575 # endif
4576 /* Remove this function from the pending weak list so that
4577 we do not emit multiple .weak directives for it. */
4578 for (p = &weak_decls; (t = *p) ; )
4579 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
4580 *p = TREE_CHAIN (t);
4581 else
4582 p = &TREE_CHAIN (t);
4584 /* Remove weakrefs to the same target from the pending weakref
4585 list, for the same reason. */
4586 for (p = &weakref_targets; (t = *p) ; )
4588 if (DECL_ASSEMBLER_NAME (decl)
4589 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
4590 *p = TREE_CHAIN (t);
4591 else
4592 p = &TREE_CHAIN (t);
4595 #endif
4598 /* First pass of completing pending aliases. Make sure that cgraph knows
4599 which symbols will be required. */
4601 void
4602 finish_aliases_1 (void)
4604 unsigned i;
4605 alias_pair *p;
4607 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4609 tree target_decl;
4611 target_decl = find_decl_and_mark_needed (p->decl, p->target);
4612 if (target_decl == NULL)
4614 if (! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4615 error ("%q+D aliased to undefined symbol %qs",
4616 p->decl, IDENTIFIER_POINTER (p->target));
4618 else if (DECL_EXTERNAL (target_decl)
4619 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (p->decl)))
4620 error ("%q+D aliased to external symbol %qs",
4621 p->decl, IDENTIFIER_POINTER (p->target));
4625 /* Second pass of completing pending aliases. Emit the actual assembly.
4626 This happens at the end of compilation and thus it is assured that the
4627 target symbol has been emitted. */
4629 void
4630 finish_aliases_2 (void)
4632 unsigned i;
4633 alias_pair *p;
4635 for (i = 0; VEC_iterate (alias_pair, alias_pairs, i, p); i++)
4636 do_assemble_alias (p->decl, p->target);
4638 VEC_truncate (alias_pair, alias_pairs, 0);
4641 /* Emit an assembler directive to make the symbol for DECL an alias to
4642 the symbol for TARGET. */
4644 void
4645 assemble_alias (tree decl, tree target)
4647 tree target_decl;
4648 bool is_weakref = false;
4650 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
4652 tree alias = DECL_ASSEMBLER_NAME (decl);
4654 is_weakref = true;
4656 ultimate_transparent_alias_target (&target);
4658 if (alias == target)
4659 error ("weakref %q+D ultimately targets itself", decl);
4660 else
4662 #ifndef ASM_OUTPUT_WEAKREF
4663 IDENTIFIER_TRANSPARENT_ALIAS (alias) = 1;
4664 TREE_CHAIN (alias) = target;
4665 #endif
4667 if (TREE_PUBLIC (decl))
4668 error ("weakref %q+D must have static linkage", decl);
4670 else
4672 #if !defined (ASM_OUTPUT_DEF)
4673 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
4674 error ("%Jalias definitions not supported in this configuration", decl);
4675 return;
4676 # else
4677 if (!DECL_WEAK (decl))
4679 error ("%Jonly weak aliases are supported in this configuration", decl);
4680 return;
4682 # endif
4683 #endif
4686 /* We must force creation of DECL_RTL for debug info generation, even though
4687 we don't use it here. */
4688 make_decl_rtl (decl);
4689 TREE_USED (decl) = 1;
4691 /* A quirk of the initial implementation of aliases required that the user
4692 add "extern" to all of them. Which is silly, but now historical. Do
4693 note that the symbol is in fact locally defined. */
4694 if (! is_weakref)
4695 DECL_EXTERNAL (decl) = 0;
4697 /* Allow aliases to aliases. */
4698 if (TREE_CODE (decl) == FUNCTION_DECL)
4699 cgraph_node (decl)->alias = true;
4700 else
4701 cgraph_varpool_node (decl)->alias = true;
4703 /* If the target has already been emitted, we don't have to queue the
4704 alias. This saves a tad o memory. */
4705 target_decl = find_decl_and_mark_needed (decl, target);
4706 if (target_decl && TREE_ASM_WRITTEN (target_decl))
4707 do_assemble_alias (decl, target);
4708 else
4710 alias_pair *p = VEC_safe_push (alias_pair, gc, alias_pairs, NULL);
4711 p->decl = decl;
4712 p->target = target;
4716 /* Emit an assembler directive to set symbol for DECL visibility to
4717 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
4719 void
4720 default_assemble_visibility (tree decl, int vis)
4722 static const char * const visibility_types[] = {
4723 NULL, "internal", "hidden", "protected"
4726 const char *name, *type;
4728 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
4729 type = visibility_types[vis];
4731 #ifdef HAVE_GAS_HIDDEN
4732 fprintf (asm_out_file, "\t.%s\t", type);
4733 assemble_name (asm_out_file, name);
4734 fprintf (asm_out_file, "\n");
4735 #else
4736 warning (OPT_Wattributes, "visibility attribute not supported "
4737 "in this configuration; ignored");
4738 #endif
4741 /* A helper function to call assemble_visibility when needed for a decl. */
4743 static void
4744 maybe_assemble_visibility (tree decl)
4746 enum symbol_visibility vis = DECL_VISIBILITY (decl);
4748 if (vis != VISIBILITY_DEFAULT)
4749 targetm.asm_out.visibility (decl, vis);
4752 /* Returns 1 if the target configuration supports defining public symbols
4753 so that one of them will be chosen at link time instead of generating a
4754 multiply-defined symbol error, whether through the use of weak symbols or
4755 a target-specific mechanism for having duplicates discarded. */
4758 supports_one_only (void)
4760 if (SUPPORTS_ONE_ONLY)
4761 return 1;
4762 return SUPPORTS_WEAK;
4765 /* Set up DECL as a public symbol that can be defined in multiple
4766 translation units without generating a linker error. */
4768 void
4769 make_decl_one_only (tree decl)
4771 gcc_assert (TREE_CODE (decl) == VAR_DECL
4772 || TREE_CODE (decl) == FUNCTION_DECL);
4774 TREE_PUBLIC (decl) = 1;
4776 if (SUPPORTS_ONE_ONLY)
4778 #ifdef MAKE_DECL_ONE_ONLY
4779 MAKE_DECL_ONE_ONLY (decl);
4780 #endif
4781 DECL_ONE_ONLY (decl) = 1;
4783 else if (TREE_CODE (decl) == VAR_DECL
4784 && (DECL_INITIAL (decl) == 0 || DECL_INITIAL (decl) == error_mark_node))
4785 DECL_COMMON (decl) = 1;
4786 else
4788 gcc_assert (SUPPORTS_WEAK);
4789 DECL_WEAK (decl) = 1;
4793 void
4794 init_varasm_once (void)
4796 section_htab = htab_create_ggc (31, section_entry_hash,
4797 section_entry_eq, NULL);
4798 const_desc_htab = htab_create_ggc (1009, const_desc_hash,
4799 const_desc_eq, NULL);
4801 const_alias_set = new_alias_set ();
4803 #ifdef TEXT_SECTION_ASM_OP
4804 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
4805 TEXT_SECTION_ASM_OP);
4806 #endif
4808 #ifdef DATA_SECTION_ASM_OP
4809 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
4810 DATA_SECTION_ASM_OP);
4811 #endif
4813 #ifdef SDATA_SECTION_ASM_OP
4814 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
4815 SDATA_SECTION_ASM_OP);
4816 #endif
4818 #ifdef READONLY_DATA_SECTION_ASM_OP
4819 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
4820 READONLY_DATA_SECTION_ASM_OP);
4821 #endif
4823 #ifdef CTORS_SECTION_ASM_OP
4824 ctors_section = get_unnamed_section (0, output_section_asm_op,
4825 CTORS_SECTION_ASM_OP);
4826 #endif
4828 #ifdef DTORS_SECTION_ASM_OP
4829 dtors_section = get_unnamed_section (0, output_section_asm_op,
4830 DTORS_SECTION_ASM_OP);
4831 #endif
4833 #ifdef BSS_SECTION_ASM_OP
4834 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
4835 output_section_asm_op,
4836 BSS_SECTION_ASM_OP);
4837 #endif
4839 #ifdef SBSS_SECTION_ASM_OP
4840 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
4841 output_section_asm_op,
4842 SBSS_SECTION_ASM_OP);
4843 #endif
4845 targetm.asm_out.init_sections ();
4847 if (readonly_data_section == NULL)
4848 readonly_data_section = text_section;
4851 enum tls_model
4852 decl_default_tls_model (tree decl)
4854 enum tls_model kind;
4855 bool is_local;
4857 is_local = targetm.binds_local_p (decl);
4858 if (!flag_shlib)
4860 if (is_local)
4861 kind = TLS_MODEL_LOCAL_EXEC;
4862 else
4863 kind = TLS_MODEL_INITIAL_EXEC;
4866 /* Local dynamic is inefficient when we're not combining the
4867 parts of the address. */
4868 else if (optimize && is_local)
4869 kind = TLS_MODEL_LOCAL_DYNAMIC;
4870 else
4871 kind = TLS_MODEL_GLOBAL_DYNAMIC;
4872 if (kind < flag_tls_default)
4873 kind = flag_tls_default;
4875 return kind;
4878 /* Select a set of attributes for section NAME based on the properties
4879 of DECL and whether or not RELOC indicates that DECL's initializer
4880 might contain runtime relocations.
4882 We make the section read-only and executable for a function decl,
4883 read-only for a const data decl, and writable for a non-const data decl. */
4885 unsigned int
4886 default_section_type_flags (tree decl, const char *name, int reloc)
4888 return default_section_type_flags_1 (decl, name, reloc, flag_pic);
4891 unsigned int
4892 default_section_type_flags_1 (tree decl, const char *name, int reloc,
4893 int shlib)
4895 unsigned int flags;
4897 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
4898 flags = SECTION_CODE;
4899 else if (decl && decl_readonly_section_1 (decl, reloc, shlib))
4900 flags = 0;
4901 else if (current_function_decl
4902 && cfun
4903 && cfun->unlikely_text_section_name
4904 && strcmp (name, cfun->unlikely_text_section_name) == 0)
4905 flags = SECTION_CODE;
4906 else if (!decl
4907 && (!current_function_decl || !cfun)
4908 && strcmp (name, UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
4909 flags = SECTION_CODE;
4910 else
4911 flags = SECTION_WRITE;
4913 if (decl && DECL_ONE_ONLY (decl))
4914 flags |= SECTION_LINKONCE;
4916 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
4917 flags |= SECTION_TLS | SECTION_WRITE;
4919 if (strcmp (name, ".bss") == 0
4920 || strncmp (name, ".bss.", 5) == 0
4921 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
4922 || strcmp (name, ".sbss") == 0
4923 || strncmp (name, ".sbss.", 6) == 0
4924 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
4925 flags |= SECTION_BSS;
4927 if (strcmp (name, ".tdata") == 0
4928 || strncmp (name, ".tdata.", 7) == 0
4929 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
4930 flags |= SECTION_TLS;
4932 if (strcmp (name, ".tbss") == 0
4933 || strncmp (name, ".tbss.", 6) == 0
4934 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
4935 flags |= SECTION_TLS | SECTION_BSS;
4937 /* These three sections have special ELF types. They are neither
4938 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
4939 want to print a section type (@progbits or @nobits). If someone
4940 is silly enough to emit code or TLS variables to one of these
4941 sections, then don't handle them specially. */
4942 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
4943 && (strcmp (name, ".init_array") == 0
4944 || strcmp (name, ".fini_array") == 0
4945 || strcmp (name, ".preinit_array") == 0))
4946 flags |= SECTION_NOTYPE;
4948 return flags;
4951 /* Output assembly to switch to section NAME with attribute FLAGS.
4952 Four variants for common object file formats. */
4954 void
4955 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
4956 unsigned int flags ATTRIBUTE_UNUSED,
4957 tree decl ATTRIBUTE_UNUSED)
4959 /* Some object formats don't support named sections at all. The
4960 front-end should already have flagged this as an error. */
4961 gcc_unreachable ();
4964 void
4965 default_elf_asm_named_section (const char *name, unsigned int flags,
4966 tree decl ATTRIBUTE_UNUSED)
4968 char flagchars[10], *f = flagchars;
4970 /* If we have already declared this section, we can use an
4971 abbreviated form to switch back to it -- unless this section is
4972 part of a COMDAT groups, in which case GAS requires the full
4973 declaration every time. */
4974 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4975 && (flags & SECTION_DECLARED))
4977 fprintf (asm_out_file, "\t.section\t%s\n", name);
4978 return;
4981 if (!(flags & SECTION_DEBUG))
4982 *f++ = 'a';
4983 if (flags & SECTION_WRITE)
4984 *f++ = 'w';
4985 if (flags & SECTION_CODE)
4986 *f++ = 'x';
4987 if (flags & SECTION_SMALL)
4988 *f++ = 's';
4989 if (flags & SECTION_MERGE)
4990 *f++ = 'M';
4991 if (flags & SECTION_STRINGS)
4992 *f++ = 'S';
4993 if (flags & SECTION_TLS)
4994 *f++ = 'T';
4995 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
4996 *f++ = 'G';
4997 *f = '\0';
4999 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
5001 if (!(flags & SECTION_NOTYPE))
5003 const char *type;
5004 const char *format;
5006 if (flags & SECTION_BSS)
5007 type = "nobits";
5008 else
5009 type = "progbits";
5011 format = ",@%s";
5012 #ifdef ASM_COMMENT_START
5013 /* On platforms that use "@" as the assembly comment character,
5014 use "%" instead. */
5015 if (strcmp (ASM_COMMENT_START, "@") == 0)
5016 format = ",%%%s";
5017 #endif
5018 fprintf (asm_out_file, format, type);
5020 if (flags & SECTION_ENTSIZE)
5021 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
5022 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
5023 fprintf (asm_out_file, ",%s,comdat",
5024 lang_hooks.decls.comdat_group (decl));
5027 putc ('\n', asm_out_file);
5030 void
5031 default_coff_asm_named_section (const char *name, unsigned int flags,
5032 tree decl ATTRIBUTE_UNUSED)
5034 char flagchars[8], *f = flagchars;
5036 if (flags & SECTION_WRITE)
5037 *f++ = 'w';
5038 if (flags & SECTION_CODE)
5039 *f++ = 'x';
5040 *f = '\0';
5042 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
5045 void
5046 default_pe_asm_named_section (const char *name, unsigned int flags,
5047 tree decl)
5049 default_coff_asm_named_section (name, flags, decl);
5051 if (flags & SECTION_LINKONCE)
5053 /* Functions may have been compiled at various levels of
5054 optimization so we can't use `same_size' here.
5055 Instead, have the linker pick one. */
5056 fprintf (asm_out_file, "\t.linkonce %s\n",
5057 (flags & SECTION_CODE ? "discard" : "same_size"));
5061 /* The lame default section selector. */
5063 section *
5064 default_select_section (tree decl, int reloc,
5065 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5067 if (DECL_P (decl))
5069 if (decl_readonly_section (decl, reloc))
5070 return readonly_data_section;
5072 else if (TREE_CODE (decl) == CONSTRUCTOR)
5074 if (! ((flag_pic && reloc)
5075 || !TREE_READONLY (decl)
5076 || TREE_SIDE_EFFECTS (decl)
5077 || !TREE_CONSTANT (decl)))
5078 return readonly_data_section;
5080 else if (TREE_CODE (decl) == STRING_CST)
5081 return readonly_data_section;
5082 else if (! (flag_pic && reloc))
5083 return readonly_data_section;
5085 return data_section;
5088 enum section_category
5089 categorize_decl_for_section (tree decl, int reloc, int shlib)
5091 enum section_category ret;
5093 if (TREE_CODE (decl) == FUNCTION_DECL)
5094 return SECCAT_TEXT;
5095 else if (TREE_CODE (decl) == STRING_CST)
5097 if (flag_mudflap) /* or !flag_merge_constants */
5098 return SECCAT_RODATA;
5099 else
5100 return SECCAT_RODATA_MERGE_STR;
5102 else if (TREE_CODE (decl) == VAR_DECL)
5104 if (DECL_INITIAL (decl) == NULL
5105 || DECL_INITIAL (decl) == error_mark_node
5106 || (flag_zero_initialized_in_bss
5107 /* Leave constant zeroes in .rodata so they can be shared. */
5108 && !TREE_READONLY (decl)
5109 && initializer_zerop (DECL_INITIAL (decl))))
5110 ret = SECCAT_BSS;
5111 else if (! TREE_READONLY (decl)
5112 || TREE_SIDE_EFFECTS (decl)
5113 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
5115 if (shlib && (reloc & 2))
5116 ret = SECCAT_DATA_REL;
5117 else if (shlib && reloc)
5118 ret = SECCAT_DATA_REL_LOCAL;
5119 else
5120 ret = SECCAT_DATA;
5122 else if (shlib && (reloc & 2))
5123 ret = SECCAT_DATA_REL_RO;
5124 else if (shlib && reloc)
5125 ret = SECCAT_DATA_REL_RO_LOCAL;
5126 else if (reloc || flag_merge_constants < 2)
5127 /* C and C++ don't allow different variables to share the same
5128 location. -fmerge-all-constants allows even that (at the
5129 expense of not conforming). */
5130 ret = SECCAT_RODATA;
5131 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
5132 ret = SECCAT_RODATA_MERGE_STR_INIT;
5133 else
5134 ret = SECCAT_RODATA_MERGE_CONST;
5136 else if (TREE_CODE (decl) == CONSTRUCTOR)
5138 if ((shlib && reloc)
5139 || TREE_SIDE_EFFECTS (decl)
5140 || ! TREE_CONSTANT (decl))
5141 ret = SECCAT_DATA;
5142 else
5143 ret = SECCAT_RODATA;
5145 else
5146 ret = SECCAT_RODATA;
5148 /* There are no read-only thread-local sections. */
5149 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5151 /* Note that this would be *just* SECCAT_BSS, except that there's
5152 no concept of a read-only thread-local-data section. */
5153 if (ret == SECCAT_BSS
5154 || (flag_zero_initialized_in_bss
5155 && initializer_zerop (DECL_INITIAL (decl))))
5156 ret = SECCAT_TBSS;
5157 else
5158 ret = SECCAT_TDATA;
5161 /* If the target uses small data sections, select it. */
5162 else if (targetm.in_small_data_p (decl))
5164 if (ret == SECCAT_BSS)
5165 ret = SECCAT_SBSS;
5166 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
5167 ret = SECCAT_SRODATA;
5168 else
5169 ret = SECCAT_SDATA;
5172 return ret;
5175 bool
5176 decl_readonly_section (tree decl, int reloc)
5178 return decl_readonly_section_1 (decl, reloc, flag_pic);
5181 bool
5182 decl_readonly_section_1 (tree decl, int reloc, int shlib)
5184 switch (categorize_decl_for_section (decl, reloc, shlib))
5186 case SECCAT_RODATA:
5187 case SECCAT_RODATA_MERGE_STR:
5188 case SECCAT_RODATA_MERGE_STR_INIT:
5189 case SECCAT_RODATA_MERGE_CONST:
5190 case SECCAT_SRODATA:
5191 return true;
5192 break;
5193 default:
5194 return false;
5195 break;
5199 /* Select a section based on the above categorization. */
5201 section *
5202 default_elf_select_section (tree decl, int reloc,
5203 unsigned HOST_WIDE_INT align)
5205 return default_elf_select_section_1 (decl, reloc, align, flag_pic);
5208 section *
5209 default_elf_select_section_1 (tree decl, int reloc,
5210 unsigned HOST_WIDE_INT align, int shlib)
5212 const char *sname;
5213 switch (categorize_decl_for_section (decl, reloc, shlib))
5215 case SECCAT_TEXT:
5216 /* We're not supposed to be called on FUNCTION_DECLs. */
5217 gcc_unreachable ();
5218 case SECCAT_RODATA:
5219 return readonly_data_section;
5220 case SECCAT_RODATA_MERGE_STR:
5221 return mergeable_string_section (decl, align, 0);
5222 case SECCAT_RODATA_MERGE_STR_INIT:
5223 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
5224 case SECCAT_RODATA_MERGE_CONST:
5225 return mergeable_constant_section (DECL_MODE (decl), align, 0);
5226 case SECCAT_SRODATA:
5227 sname = ".sdata2";
5228 break;
5229 case SECCAT_DATA:
5230 return data_section;
5231 case SECCAT_DATA_REL:
5232 sname = ".data.rel";
5233 break;
5234 case SECCAT_DATA_REL_LOCAL:
5235 sname = ".data.rel.local";
5236 break;
5237 case SECCAT_DATA_REL_RO:
5238 sname = ".data.rel.ro";
5239 break;
5240 case SECCAT_DATA_REL_RO_LOCAL:
5241 sname = ".data.rel.ro.local";
5242 break;
5243 case SECCAT_SDATA:
5244 sname = ".sdata";
5245 break;
5246 case SECCAT_TDATA:
5247 sname = ".tdata";
5248 break;
5249 case SECCAT_BSS:
5250 if (bss_section)
5251 return bss_section;
5252 sname = ".bss";
5253 break;
5254 case SECCAT_SBSS:
5255 sname = ".sbss";
5256 break;
5257 case SECCAT_TBSS:
5258 sname = ".tbss";
5259 break;
5260 default:
5261 gcc_unreachable ();
5264 if (!DECL_P (decl))
5265 decl = NULL_TREE;
5266 return get_named_section (decl, sname, reloc);
5269 /* Construct a unique section name based on the decl name and the
5270 categorization performed above. */
5272 void
5273 default_unique_section (tree decl, int reloc)
5275 default_unique_section_1 (decl, reloc, flag_pic);
5278 void
5279 default_unique_section_1 (tree decl, int reloc, int shlib)
5281 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
5282 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
5283 const char *prefix, *name;
5284 size_t nlen, plen;
5285 char *string;
5287 switch (categorize_decl_for_section (decl, reloc, shlib))
5289 case SECCAT_TEXT:
5290 prefix = one_only ? ".gnu.linkonce.t." : ".text.";
5291 break;
5292 case SECCAT_RODATA:
5293 case SECCAT_RODATA_MERGE_STR:
5294 case SECCAT_RODATA_MERGE_STR_INIT:
5295 case SECCAT_RODATA_MERGE_CONST:
5296 prefix = one_only ? ".gnu.linkonce.r." : ".rodata.";
5297 break;
5298 case SECCAT_SRODATA:
5299 prefix = one_only ? ".gnu.linkonce.s2." : ".sdata2.";
5300 break;
5301 case SECCAT_DATA:
5302 prefix = one_only ? ".gnu.linkonce.d." : ".data.";
5303 break;
5304 case SECCAT_DATA_REL:
5305 prefix = one_only ? ".gnu.linkonce.d.rel." : ".data.rel.";
5306 break;
5307 case SECCAT_DATA_REL_LOCAL:
5308 prefix = one_only ? ".gnu.linkonce.d.rel.local." : ".data.rel.local.";
5309 break;
5310 case SECCAT_DATA_REL_RO:
5311 prefix = one_only ? ".gnu.linkonce.d.rel.ro." : ".data.rel.ro.";
5312 break;
5313 case SECCAT_DATA_REL_RO_LOCAL:
5314 prefix = one_only ? ".gnu.linkonce.d.rel.ro.local."
5315 : ".data.rel.ro.local.";
5316 break;
5317 case SECCAT_SDATA:
5318 prefix = one_only ? ".gnu.linkonce.s." : ".sdata.";
5319 break;
5320 case SECCAT_BSS:
5321 prefix = one_only ? ".gnu.linkonce.b." : ".bss.";
5322 break;
5323 case SECCAT_SBSS:
5324 prefix = one_only ? ".gnu.linkonce.sb." : ".sbss.";
5325 break;
5326 case SECCAT_TDATA:
5327 prefix = one_only ? ".gnu.linkonce.td." : ".tdata.";
5328 break;
5329 case SECCAT_TBSS:
5330 prefix = one_only ? ".gnu.linkonce.tb." : ".tbss.";
5331 break;
5332 default:
5333 gcc_unreachable ();
5335 plen = strlen (prefix);
5337 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5338 name = targetm.strip_name_encoding (name);
5339 nlen = strlen (name);
5341 string = alloca (nlen + plen + 1);
5342 memcpy (string, prefix, plen);
5343 memcpy (string + plen, name, nlen + 1);
5345 DECL_SECTION_NAME (decl) = build_string (nlen + plen, string);
5348 section *
5349 default_select_rtx_section (enum machine_mode mode ATTRIBUTE_UNUSED,
5350 rtx x,
5351 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
5353 if (flag_pic)
5354 switch (GET_CODE (x))
5356 case CONST:
5357 case SYMBOL_REF:
5358 case LABEL_REF:
5359 return data_section;
5361 default:
5362 break;
5365 return readonly_data_section;
5368 section *
5369 default_elf_select_rtx_section (enum machine_mode mode, rtx x,
5370 unsigned HOST_WIDE_INT align)
5372 /* ??? Handle small data here somehow. */
5374 if (flag_pic)
5375 switch (GET_CODE (x))
5377 case CONST:
5378 case SYMBOL_REF:
5379 return get_named_section (NULL, ".data.rel.ro", 3);
5381 case LABEL_REF:
5382 return get_named_section (NULL, ".data.rel.ro.local", 1);
5384 default:
5385 break;
5388 return mergeable_constant_section (mode, align, 0);
5391 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
5393 void
5394 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
5396 rtx symbol;
5397 int flags;
5399 /* Careful not to prod global register variables. */
5400 if (!MEM_P (rtl))
5401 return;
5402 symbol = XEXP (rtl, 0);
5403 if (GET_CODE (symbol) != SYMBOL_REF)
5404 return;
5406 flags = 0;
5407 if (TREE_CODE (decl) == FUNCTION_DECL)
5408 flags |= SYMBOL_FLAG_FUNCTION;
5409 if (targetm.binds_local_p (decl))
5410 flags |= SYMBOL_FLAG_LOCAL;
5411 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
5412 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
5413 else if (targetm.in_small_data_p (decl))
5414 flags |= SYMBOL_FLAG_SMALL;
5415 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
5416 being PUBLIC, the thing *must* be defined in this translation unit.
5417 Prevent this buglet from being propagated into rtl code as well. */
5418 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
5419 flags |= SYMBOL_FLAG_EXTERNAL;
5421 SYMBOL_REF_FLAGS (symbol) = flags;
5424 /* By default, we do nothing for encode_section_info, so we need not
5425 do anything but discard the '*' marker. */
5427 const char *
5428 default_strip_name_encoding (const char *str)
5430 return str + (*str == '*');
5433 /* Assume ELF-ish defaults, since that's pretty much the most liberal
5434 wrt cross-module name binding. */
5436 bool
5437 default_binds_local_p (tree exp)
5439 return default_binds_local_p_1 (exp, flag_shlib);
5442 bool
5443 default_binds_local_p_1 (tree exp, int shlib)
5445 bool local_p;
5447 /* A non-decl is an entry in the constant pool. */
5448 if (!DECL_P (exp))
5449 local_p = true;
5450 /* Weakrefs may not bind locally, even though the weakref itself is
5451 always static and therefore local. */
5452 else if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp)))
5453 local_p = false;
5454 /* Static variables are always local. */
5455 else if (! TREE_PUBLIC (exp))
5456 local_p = true;
5457 /* A variable is local if the user has said explicitly that it will
5458 be. */
5459 else if (DECL_VISIBILITY_SPECIFIED (exp)
5460 && DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5461 local_p = true;
5462 /* Variables defined outside this object might not be local. */
5463 else if (DECL_EXTERNAL (exp))
5464 local_p = false;
5465 /* If defined in this object and visibility is not default, must be
5466 local. */
5467 else if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT)
5468 local_p = true;
5469 /* Default visibility weak data can be overridden by a strong symbol
5470 in another module and so are not local. */
5471 else if (DECL_WEAK (exp))
5472 local_p = false;
5473 /* If PIC, then assume that any global name can be overridden by
5474 symbols resolved from other modules. */
5475 else if (shlib)
5476 local_p = false;
5477 /* Uninitialized COMMON variable may be unified with symbols
5478 resolved from other modules. */
5479 else if (DECL_COMMON (exp)
5480 && (DECL_INITIAL (exp) == NULL
5481 || DECL_INITIAL (exp) == error_mark_node))
5482 local_p = false;
5483 /* Otherwise we're left with initialized (or non-common) global data
5484 which is of necessity defined locally. */
5485 else
5486 local_p = true;
5488 return local_p;
5491 /* Determine whether or not a pointer mode is valid. Assume defaults
5492 of ptr_mode or Pmode - can be overridden. */
5493 bool
5494 default_valid_pointer_mode (enum machine_mode mode)
5496 return (mode == ptr_mode || mode == Pmode);
5499 /* Default function to output code that will globalize a label. A
5500 target must define GLOBAL_ASM_OP or provide its own function to
5501 globalize a label. */
5502 #ifdef GLOBAL_ASM_OP
5503 void
5504 default_globalize_label (FILE * stream, const char *name)
5506 fputs (GLOBAL_ASM_OP, stream);
5507 assemble_name (stream, name);
5508 putc ('\n', stream);
5510 #endif /* GLOBAL_ASM_OP */
5512 /* Default function to output a label for unwind information. The
5513 default is to do nothing. A target that needs nonlocal labels for
5514 unwind information must provide its own function to do this. */
5515 void
5516 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
5517 tree decl ATTRIBUTE_UNUSED,
5518 int for_eh ATTRIBUTE_UNUSED,
5519 int empty ATTRIBUTE_UNUSED)
5523 /* Default function to output a label to divide up the exception table.
5524 The default is to do nothing. A target that needs/wants to divide
5525 up the table must provide it's own function to do this. */
5526 void
5527 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
5531 /* This is how to output an internal numbered label where PREFIX is
5532 the class of label and LABELNO is the number within the class. */
5534 void
5535 default_internal_label (FILE *stream, const char *prefix,
5536 unsigned long labelno)
5538 char *const buf = alloca (40 + strlen (prefix));
5539 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
5540 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
5543 /* This is the default behavior at the beginning of a file. It's
5544 controlled by two other target-hook toggles. */
5545 void
5546 default_file_start (void)
5548 if (targetm.file_start_app_off && !flag_verbose_asm)
5549 fputs (ASM_APP_OFF, asm_out_file);
5551 if (targetm.file_start_file_directive)
5552 output_file_directive (asm_out_file, main_input_filename);
5555 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
5556 which emits a special section directive used to indicate whether or
5557 not this object file needs an executable stack. This is primarily
5558 a GNU extension to ELF but could be used on other targets. */
5560 int trampolines_created;
5562 void
5563 file_end_indicate_exec_stack (void)
5565 unsigned int flags = SECTION_DEBUG;
5566 if (trampolines_created)
5567 flags |= SECTION_CODE;
5569 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
5572 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
5573 a get_unnamed_section callback. */
5575 void
5576 output_section_asm_op (const void *directive)
5578 fprintf (asm_out_file, "%s\n", (const char *) directive);
5581 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
5582 the current section is NEW_SECTION. */
5584 void
5585 switch_to_section (section *new_section)
5587 if (in_section == new_section)
5588 return;
5590 if (new_section->common.flags & SECTION_FORGET)
5591 in_section = NULL;
5592 else
5593 in_section = new_section;
5595 if (new_section->common.flags & SECTION_NAMED)
5597 if (cfun
5598 && !cfun->unlikely_text_section_name
5599 && strcmp (new_section->named.name,
5600 UNLIKELY_EXECUTED_TEXT_SECTION_NAME) == 0)
5601 cfun->unlikely_text_section_name = UNLIKELY_EXECUTED_TEXT_SECTION_NAME;
5603 targetm.asm_out.named_section (new_section->named.name,
5604 new_section->named.common.flags,
5605 new_section->named.decl);
5607 else
5608 new_section->unnamed.callback (new_section->unnamed.data);
5610 new_section->common.flags |= SECTION_DECLARED;
5613 #include "gt-varasm.h"