Document gcov-io (PR gcov-profile/84735).
[official-gcc.git] / gcc / varasm.c
blob6e345d39d31ff48e2080ac62707640bc94c1253e
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "backend.h"
32 #include "target.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "predict.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "stringpool.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "varasm.h"
46 #include "flags.h"
47 #include "stmt.h"
48 #include "expr.h"
49 #include "expmed.h"
50 #include "output.h"
51 #include "langhooks.h"
52 #include "debug.h"
53 #include "common/common-target.h"
54 #include "stringpool.h"
55 #include "attribs.h"
56 #include "asan.h"
57 #include "rtl-iter.h"
58 #include "file-prefix-map.h" /* remap_debug_filename() */
60 #ifdef XCOFF_DEBUGGING_INFO
61 #include "xcoffout.h" /* Needed for external data declarations. */
62 #endif
64 /* The (assembler) name of the first globally-visible object output. */
65 extern GTY(()) const char *first_global_object_name;
66 extern GTY(()) const char *weak_global_object_name;
68 const char *first_global_object_name;
69 const char *weak_global_object_name;
71 struct addr_const;
72 struct constant_descriptor_rtx;
73 struct rtx_constant_pool;
75 #define n_deferred_constants (crtl->varasm.deferred_constants)
77 /* Number for making the label on the next
78 constant that is stored in memory. */
80 static GTY(()) int const_labelno;
82 /* Carry information from ASM_DECLARE_OBJECT_NAME
83 to ASM_FINISH_DECLARE_OBJECT. */
85 int size_directive_output;
87 /* The last decl for which assemble_variable was called,
88 if it did ASM_DECLARE_OBJECT_NAME.
89 If the last call to assemble_variable didn't do that,
90 this holds 0. */
92 tree last_assemble_variable_decl;
94 /* The following global variable indicates if the first basic block
95 in a function belongs to the cold partition or not. */
97 bool first_function_block_is_cold;
99 /* Whether we saw any functions with no_split_stack. */
101 static bool saw_no_split_stack;
103 static const char *strip_reg_name (const char *);
104 static int contains_pointers_p (tree);
105 #ifdef ASM_OUTPUT_EXTERNAL
106 static bool incorporeal_function_p (tree);
107 #endif
108 static void decode_addr_const (tree, struct addr_const *);
109 static hashval_t const_hash_1 (const tree);
110 static int compare_constant (const tree, const tree);
111 static void output_constant_def_contents (rtx);
112 static void output_addressed_constants (tree);
113 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
114 unsigned int, bool);
115 static void globalize_decl (tree);
116 static bool decl_readonly_section_1 (enum section_category);
117 #ifdef BSS_SECTION_ASM_OP
118 #ifdef ASM_OUTPUT_ALIGNED_BSS
119 static void asm_output_aligned_bss (FILE *, tree, const char *,
120 unsigned HOST_WIDE_INT, int)
121 ATTRIBUTE_UNUSED;
122 #endif
123 #endif /* BSS_SECTION_ASM_OP */
124 static void mark_weak (tree);
125 static void output_constant_pool (const char *, tree);
126 static void handle_vtv_comdat_section (section *, const_tree);
128 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
129 section *text_section;
130 section *data_section;
131 section *readonly_data_section;
132 section *sdata_section;
133 section *ctors_section;
134 section *dtors_section;
135 section *bss_section;
136 section *sbss_section;
138 /* Various forms of common section. All are guaranteed to be nonnull. */
139 section *tls_comm_section;
140 section *comm_section;
141 section *lcomm_section;
143 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
144 May be null. */
145 section *bss_noswitch_section;
147 /* The section that holds the main exception table, when known. The section
148 is set either by the target's init_sections hook or by the first call to
149 switch_to_exception_section. */
150 section *exception_section;
152 /* The section that holds the DWARF2 frame unwind information, when known.
153 The section is set either by the target's init_sections hook or by the
154 first call to switch_to_eh_frame_section. */
155 section *eh_frame_section;
157 /* asm_out_file's current section. This is NULL if no section has yet
158 been selected or if we lose track of what the current section is. */
159 section *in_section;
161 /* True if code for the current function is currently being directed
162 at the cold section. */
163 bool in_cold_section_p;
165 /* The following global holds the "function name" for the code in the
166 cold section of a function, if hot/cold function splitting is enabled
167 and there was actually code that went into the cold section. A
168 pseudo function name is needed for the cold section of code for some
169 debugging tools that perform symbolization. */
170 tree cold_function_name = NULL_TREE;
172 /* A linked list of all the unnamed sections. */
173 static GTY(()) section *unnamed_sections;
175 /* Return a nonzero value if DECL has a section attribute. */
176 #define IN_NAMED_SECTION(DECL) \
177 (VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
179 struct section_hasher : ggc_ptr_hash<section>
181 typedef const char *compare_type;
183 static hashval_t hash (section *);
184 static bool equal (section *, const char *);
187 /* Hash table of named sections. */
188 static GTY(()) hash_table<section_hasher> *section_htab;
190 struct object_block_hasher : ggc_ptr_hash<object_block>
192 typedef const section *compare_type;
194 static hashval_t hash (object_block *);
195 static bool equal (object_block *, const section *);
198 /* A table of object_blocks, indexed by section. */
199 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
201 /* The next number to use for internal anchor labels. */
202 static GTY(()) int anchor_labelno;
204 /* A pool of constants that can be shared between functions. */
205 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
207 /* Helper routines for maintaining section_htab. */
209 bool
210 section_hasher::equal (section *old, const char *new_name)
212 return strcmp (old->named.name, new_name) == 0;
215 hashval_t
216 section_hasher::hash (section *old)
218 return htab_hash_string (old->named.name);
221 /* Return a hash value for section SECT. */
223 static hashval_t
224 hash_section (section *sect)
226 if (sect->common.flags & SECTION_NAMED)
227 return htab_hash_string (sect->named.name);
228 return sect->common.flags;
231 /* Helper routines for maintaining object_block_htab. */
233 inline bool
234 object_block_hasher::equal (object_block *old, const section *new_section)
236 return old->sect == new_section;
239 hashval_t
240 object_block_hasher::hash (object_block *old)
242 return hash_section (old->sect);
245 /* Return a new unnamed section with the given fields. */
247 section *
248 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
249 const void *data)
251 section *sect;
253 sect = ggc_alloc<section> ();
254 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
255 sect->unnamed.callback = callback;
256 sect->unnamed.data = data;
257 sect->unnamed.next = unnamed_sections;
259 unnamed_sections = sect;
260 return sect;
263 /* Return a SECTION_NOSWITCH section with the given fields. */
265 static section *
266 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
268 section *sect;
270 sect = ggc_alloc<section> ();
271 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
272 sect->noswitch.callback = callback;
274 return sect;
277 /* Return the named section structure associated with NAME. Create
278 a new section with the given fields if no such structure exists. */
280 section *
281 get_section (const char *name, unsigned int flags, tree decl)
283 section *sect, **slot;
285 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
286 INSERT);
287 flags |= SECTION_NAMED;
288 if (*slot == NULL)
290 sect = ggc_alloc<section> ();
291 sect->named.common.flags = flags;
292 sect->named.name = ggc_strdup (name);
293 sect->named.decl = decl;
294 *slot = sect;
296 else
298 sect = *slot;
299 if ((sect->common.flags & ~SECTION_DECLARED) != flags
300 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
302 /* It is fine if one of the section flags is
303 SECTION_WRITE | SECTION_RELRO and the other has none of these
304 flags (i.e. read-only) in named sections and either the
305 section hasn't been declared yet or has been declared as writable.
306 In that case just make sure the resulting flags are
307 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
308 relocations. */
309 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
310 == (SECTION_WRITE | SECTION_RELRO)
311 && (sect->common.flags
312 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
313 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
314 && ((sect->common.flags & SECTION_DECLARED) == 0
315 || (sect->common.flags & SECTION_WRITE)))
317 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
318 return sect;
320 /* Sanity check user variables for flag changes. */
321 if (sect->named.decl != NULL
322 && DECL_P (sect->named.decl)
323 && decl != sect->named.decl)
325 if (decl != NULL && DECL_P (decl))
326 error ("%+qD causes a section type conflict with %qD",
327 decl, sect->named.decl);
328 else
329 error ("section type conflict with %qD", sect->named.decl);
330 inform (DECL_SOURCE_LOCATION (sect->named.decl),
331 "%qD was declared here", sect->named.decl);
333 else if (decl != NULL && DECL_P (decl))
334 error ("%+qD causes a section type conflict", decl);
335 else
336 error ("section type conflict");
337 /* Make sure we don't error about one section multiple times. */
338 sect->common.flags |= SECTION_OVERRIDE;
341 return sect;
344 /* Return true if the current compilation mode benefits from having
345 objects grouped into blocks. */
347 static bool
348 use_object_blocks_p (void)
350 return flag_section_anchors;
353 /* Return the object_block structure for section SECT. Create a new
354 structure if we haven't created one already. Return null if SECT
355 itself is null. */
357 static struct object_block *
358 get_block_for_section (section *sect)
360 struct object_block *block;
362 if (sect == NULL)
363 return NULL;
365 object_block **slot
366 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
367 INSERT);
368 block = *slot;
369 if (block == NULL)
371 block = ggc_cleared_alloc<object_block> ();
372 block->sect = sect;
373 *slot = block;
375 return block;
378 /* Create a symbol with label LABEL and place it at byte offset
379 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
380 is not yet known. LABEL must be a garbage-collected string. */
382 static rtx
383 create_block_symbol (const char *label, struct object_block *block,
384 HOST_WIDE_INT offset)
386 rtx symbol;
387 unsigned int size;
389 /* Create the extended SYMBOL_REF. */
390 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
391 symbol = (rtx) ggc_internal_alloc (size);
393 /* Initialize the normal SYMBOL_REF fields. */
394 memset (symbol, 0, size);
395 PUT_CODE (symbol, SYMBOL_REF);
396 PUT_MODE (symbol, Pmode);
397 XSTR (symbol, 0) = label;
398 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
400 /* Initialize the block_symbol stuff. */
401 SYMBOL_REF_BLOCK (symbol) = block;
402 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
404 return symbol;
407 /* Return a section with a particular name and with whatever SECTION_*
408 flags section_type_flags deems appropriate. The name of the section
409 is taken from NAME if nonnull, otherwise it is taken from DECL's
410 DECL_SECTION_NAME. DECL is the decl associated with the section
411 (see the section comment for details) and RELOC is as for
412 section_type_flags. */
414 section *
415 get_named_section (tree decl, const char *name, int reloc)
417 unsigned int flags;
419 if (name == NULL)
421 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
422 name = DECL_SECTION_NAME (decl);
425 flags = targetm.section_type_flags (decl, name, reloc);
426 return get_section (name, flags, decl);
429 /* Worker for resolve_unique_section. */
431 static bool
432 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
434 n->implicit_section = true;
435 return false;
438 /* If required, set DECL_SECTION_NAME to a unique name. */
440 void
441 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
442 int flag_function_or_data_sections)
444 if (DECL_SECTION_NAME (decl) == NULL
445 && targetm_common.have_named_sections
446 && (flag_function_or_data_sections
447 || DECL_COMDAT_GROUP (decl)))
449 targetm.asm_out.unique_section (decl, reloc);
450 if (DECL_SECTION_NAME (decl))
451 symtab_node::get (decl)->call_for_symbol_and_aliases
452 (set_implicit_section, NULL, true);
456 #ifdef BSS_SECTION_ASM_OP
458 #ifdef ASM_OUTPUT_ALIGNED_BSS
460 /* Utility function for targets to use in implementing
461 ASM_OUTPUT_ALIGNED_BSS.
462 ??? It is believed that this function will work in most cases so such
463 support is localized here. */
465 static void
466 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
467 const char *name, unsigned HOST_WIDE_INT size,
468 int align)
470 switch_to_section (bss_section);
471 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
472 #ifdef ASM_DECLARE_OBJECT_NAME
473 last_assemble_variable_decl = decl;
474 ASM_DECLARE_OBJECT_NAME (file, name, decl);
475 #else
476 /* Standard thing is just output label for the object. */
477 ASM_OUTPUT_LABEL (file, name);
478 #endif /* ASM_DECLARE_OBJECT_NAME */
479 ASM_OUTPUT_SKIP (file, size ? size : 1);
482 #endif
484 #endif /* BSS_SECTION_ASM_OP */
486 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
487 /* Return the hot section for function DECL. Return text_section for
488 null DECLs. */
490 static section *
491 hot_function_section (tree decl)
493 if (decl != NULL_TREE
494 && DECL_SECTION_NAME (decl) != NULL
495 && targetm_common.have_named_sections)
496 return get_named_section (decl, NULL, 0);
497 else
498 return text_section;
500 #endif
502 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
503 is NULL.
505 When DECL_SECTION_NAME is non-NULL and it is implicit section and
506 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
507 concatenate the name with NAMED_SECTION_SUFFIX.
508 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
510 section *
511 get_named_text_section (tree decl,
512 const char *text_section_name,
513 const char *named_section_suffix)
515 if (decl && DECL_SECTION_NAME (decl))
517 if (named_section_suffix)
519 const char *dsn = DECL_SECTION_NAME (decl);
520 const char *stripped_name;
521 char *name, *buffer;
523 name = (char *) alloca (strlen (dsn) + 1);
524 memcpy (name, dsn,
525 strlen (dsn) + 1);
527 stripped_name = targetm.strip_name_encoding (name);
529 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
530 return get_named_section (decl, buffer, 0);
532 else if (symtab_node::get (decl)->implicit_section)
534 const char *name;
536 /* Do not try to split gnu_linkonce functions. This gets somewhat
537 slipperly. */
538 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
539 return NULL;
540 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
541 name = targetm.strip_name_encoding (name);
542 return get_named_section (decl, ACONCAT ((text_section_name, ".",
543 name, NULL)), 0);
545 else
546 return NULL;
548 return get_named_section (decl, text_section_name, 0);
551 /* Choose named function section based on its frequency. */
553 section *
554 default_function_section (tree decl, enum node_frequency freq,
555 bool startup, bool exit)
557 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
558 /* Old GNU linkers have buggy --gc-section support, which sometimes
559 results in .gcc_except_table* sections being garbage collected. */
560 if (decl
561 && symtab_node::get (decl)->implicit_section)
562 return NULL;
563 #endif
565 if (!flag_reorder_functions
566 || !targetm_common.have_named_sections)
567 return NULL;
568 /* Startup code should go to startup subsection unless it is
569 unlikely executed (this happens especially with function splitting
570 where we can split away unnecessary parts of static constructors. */
571 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
573 /* If we do have a profile or(and) LTO phase is executed, we do not need
574 these ELF section. */
575 if (!in_lto_p || !flag_profile_values)
576 return get_named_text_section (decl, ".text.startup", NULL);
577 else
578 return NULL;
581 /* Similarly for exit. */
582 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
583 return get_named_text_section (decl, ".text.exit", NULL);
585 /* Group cold functions together, similarly for hot code. */
586 switch (freq)
588 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
589 return get_named_text_section (decl, ".text.unlikely", NULL);
590 case NODE_FREQUENCY_HOT:
591 /* If we do have a profile or(and) LTO phase is executed, we do not need
592 these ELF section. */
593 if (!in_lto_p || !flag_profile_values)
594 return get_named_text_section (decl, ".text.hot", NULL);
595 /* FALLTHRU */
596 default:
597 return NULL;
601 /* Return the section for function DECL.
603 If DECL is NULL_TREE, return the text section. We can be passed
604 NULL_TREE under some circumstances by dbxout.c at least.
606 If FORCE_COLD is true, return cold function section ignoring
607 the frequency info of cgraph_node. */
609 static section *
610 function_section_1 (tree decl, bool force_cold)
612 section *section = NULL;
613 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
614 bool startup = false, exit = false;
616 if (decl)
618 struct cgraph_node *node = cgraph_node::get (decl);
620 if (node)
622 freq = node->frequency;
623 startup = node->only_called_at_startup;
624 exit = node->only_called_at_exit;
627 if (force_cold)
628 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
630 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
631 if (decl != NULL_TREE
632 && DECL_SECTION_NAME (decl) != NULL)
634 if (targetm.asm_out.function_section)
635 section = targetm.asm_out.function_section (decl, freq,
636 startup, exit);
637 if (section)
638 return section;
639 return get_named_section (decl, NULL, 0);
641 else
642 return targetm.asm_out.select_section
643 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
644 symtab_node::get (decl)->definition_alignment ());
645 #else
646 if (targetm.asm_out.function_section)
647 section = targetm.asm_out.function_section (decl, freq, startup, exit);
648 if (section)
649 return section;
650 return hot_function_section (decl);
651 #endif
654 /* Return the section for function DECL.
656 If DECL is NULL_TREE, return the text section. We can be passed
657 NULL_TREE under some circumstances by dbxout.c at least. */
659 section *
660 function_section (tree decl)
662 /* Handle cases where function splitting code decides
663 to put function entry point into unlikely executed section
664 despite the fact that the function itself is not cold
665 (i.e. it is called rarely but contains a hot loop that is
666 better to live in hot subsection for the code locality). */
667 return function_section_1 (decl,
668 first_function_block_is_cold);
671 /* Return the section for the current function, take IN_COLD_SECTION_P
672 into account. */
674 section *
675 current_function_section (void)
677 return function_section_1 (current_function_decl, in_cold_section_p);
680 /* Tell assembler to switch to unlikely-to-be-executed text section. */
682 section *
683 unlikely_text_section (void)
685 return function_section_1 (current_function_decl, true);
688 /* When called within a function context, return true if the function
689 has been assigned a cold text section and if SECT is that section.
690 When called outside a function context, return true if SECT is the
691 default cold section. */
693 bool
694 unlikely_text_section_p (section *sect)
696 return sect == function_section_1 (current_function_decl, true);
699 /* Switch to the other function partition (if inside of hot section
700 into cold section, otherwise into the hot section). */
702 void
703 switch_to_other_text_partition (void)
705 in_cold_section_p = !in_cold_section_p;
706 switch_to_section (current_function_section ());
709 /* Return the read-only data section associated with function DECL. */
711 section *
712 default_function_rodata_section (tree decl)
714 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
716 const char *name = DECL_SECTION_NAME (decl);
718 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
720 const char *dot;
721 size_t len;
722 char* rname;
724 dot = strchr (name + 1, '.');
725 if (!dot)
726 dot = name;
727 len = strlen (dot) + 8;
728 rname = (char *) alloca (len);
730 strcpy (rname, ".rodata");
731 strcat (rname, dot);
732 return get_section (rname, SECTION_LINKONCE, decl);
734 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
735 else if (DECL_COMDAT_GROUP (decl)
736 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
738 size_t len = strlen (name) + 1;
739 char *rname = (char *) alloca (len);
741 memcpy (rname, name, len);
742 rname[14] = 'r';
743 return get_section (rname, SECTION_LINKONCE, decl);
745 /* For .text.foo we want to use .rodata.foo. */
746 else if (flag_function_sections && flag_data_sections
747 && strncmp (name, ".text.", 6) == 0)
749 size_t len = strlen (name) + 1;
750 char *rname = (char *) alloca (len + 2);
752 memcpy (rname, ".rodata", 7);
753 memcpy (rname + 7, name + 5, len - 5);
754 return get_section (rname, 0, decl);
758 return readonly_data_section;
761 /* Return the read-only data section associated with function DECL
762 for targets where that section should be always the single
763 readonly data section. */
765 section *
766 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
768 return readonly_data_section;
771 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
773 static const char *
774 function_mergeable_rodata_prefix (void)
776 section *s = targetm.asm_out.function_rodata_section (current_function_decl);
777 if (SECTION_STYLE (s) == SECTION_NAMED)
778 return s->named.name;
779 else
780 return targetm.asm_out.mergeable_rodata_prefix;
783 /* Return the section to use for string merging. */
785 static section *
786 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
787 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
788 unsigned int flags ATTRIBUTE_UNUSED)
790 HOST_WIDE_INT len;
792 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
793 && TREE_CODE (decl) == STRING_CST
794 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
795 && align <= 256
796 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
797 && TREE_STRING_LENGTH (decl) >= len)
799 scalar_int_mode mode;
800 unsigned int modesize;
801 const char *str;
802 HOST_WIDE_INT i;
803 int j, unit;
804 const char *prefix = function_mergeable_rodata_prefix ();
805 char *name = (char *) alloca (strlen (prefix) + 30);
807 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
808 modesize = GET_MODE_BITSIZE (mode);
809 if (modesize >= 8 && modesize <= 256
810 && (modesize & (modesize - 1)) == 0)
812 if (align < modesize)
813 align = modesize;
815 str = TREE_STRING_POINTER (decl);
816 unit = GET_MODE_SIZE (mode);
818 /* Check for embedded NUL characters. */
819 for (i = 0; i < len; i += unit)
821 for (j = 0; j < unit; j++)
822 if (str[i + j] != '\0')
823 break;
824 if (j == unit)
825 break;
827 if (i == len - unit)
829 sprintf (name, "%s.str%d.%d", prefix,
830 modesize / 8, (int) (align / 8));
831 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
832 return get_section (name, flags, NULL);
837 return readonly_data_section;
840 /* Return the section to use for constant merging. */
842 section *
843 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
844 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
845 unsigned int flags ATTRIBUTE_UNUSED)
847 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
848 && mode != VOIDmode
849 && mode != BLKmode
850 && known_le (GET_MODE_BITSIZE (mode), align)
851 && align >= 8
852 && align <= 256
853 && (align & (align - 1)) == 0)
855 const char *prefix = function_mergeable_rodata_prefix ();
856 char *name = (char *) alloca (strlen (prefix) + 30);
858 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
859 flags |= (align / 8) | SECTION_MERGE;
860 return get_section (name, flags, NULL);
862 return readonly_data_section;
865 /* Given NAME, a putative register name, discard any customary prefixes. */
867 static const char *
868 strip_reg_name (const char *name)
870 #ifdef REGISTER_PREFIX
871 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
872 name += strlen (REGISTER_PREFIX);
873 #endif
874 if (name[0] == '%' || name[0] == '#')
875 name++;
876 return name;
879 /* The user has asked for a DECL to have a particular name. Set (or
880 change) it in such a way that we don't prefix an underscore to
881 it. */
882 void
883 set_user_assembler_name (tree decl, const char *name)
885 char *starred = (char *) alloca (strlen (name) + 2);
886 starred[0] = '*';
887 strcpy (starred + 1, name);
888 symtab->change_decl_assembler_name (decl, get_identifier (starred));
889 SET_DECL_RTL (decl, NULL_RTX);
892 /* Decode an `asm' spec for a declaration as a register name.
893 Return the register number, or -1 if nothing specified,
894 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
895 or -3 if ASMSPEC is `cc' and is not recognized,
896 or -4 if ASMSPEC is `memory' and is not recognized.
897 Accept an exact spelling or a decimal number.
898 Prefixes such as % are optional. */
901 decode_reg_name_and_count (const char *asmspec, int *pnregs)
903 /* Presume just one register is clobbered. */
904 *pnregs = 1;
906 if (asmspec != 0)
908 int i;
910 /* Get rid of confusing prefixes. */
911 asmspec = strip_reg_name (asmspec);
913 /* Allow a decimal number as a "register name". */
914 for (i = strlen (asmspec) - 1; i >= 0; i--)
915 if (! ISDIGIT (asmspec[i]))
916 break;
917 if (asmspec[0] != 0 && i < 0)
919 i = atoi (asmspec);
920 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
921 return i;
922 else
923 return -2;
926 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
927 if (reg_names[i][0]
928 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
929 return i;
931 #ifdef OVERLAPPING_REGISTER_NAMES
933 static const struct
935 const char *const name;
936 const int number;
937 const int nregs;
938 } table[] = OVERLAPPING_REGISTER_NAMES;
940 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
941 if (table[i].name[0]
942 && ! strcmp (asmspec, table[i].name))
944 *pnregs = table[i].nregs;
945 return table[i].number;
948 #endif /* OVERLAPPING_REGISTER_NAMES */
950 #ifdef ADDITIONAL_REGISTER_NAMES
952 static const struct { const char *const name; const int number; } table[]
953 = ADDITIONAL_REGISTER_NAMES;
955 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
956 if (table[i].name[0]
957 && ! strcmp (asmspec, table[i].name)
958 && reg_names[table[i].number][0])
959 return table[i].number;
961 #endif /* ADDITIONAL_REGISTER_NAMES */
963 if (!strcmp (asmspec, "memory"))
964 return -4;
966 if (!strcmp (asmspec, "cc"))
967 return -3;
969 return -2;
972 return -1;
976 decode_reg_name (const char *name)
978 int count;
979 return decode_reg_name_and_count (name, &count);
983 /* Return true if DECL's initializer is suitable for a BSS section. */
985 bool
986 bss_initializer_p (const_tree decl, bool named)
988 /* Do not put non-common constants into the .bss section, they belong in
989 a readonly section, except when NAMED is true. */
990 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
991 && (DECL_INITIAL (decl) == NULL
992 /* In LTO we have no errors in program; error_mark_node is used
993 to mark offlined constructors. */
994 || (DECL_INITIAL (decl) == error_mark_node
995 && !in_lto_p)
996 || (flag_zero_initialized_in_bss
997 && initializer_zerop (DECL_INITIAL (decl)))));
1000 /* Compute the alignment of variable specified by DECL.
1001 DONT_OUTPUT_DATA is from assemble_variable. */
1003 void
1004 align_variable (tree decl, bool dont_output_data)
1006 unsigned int align = DECL_ALIGN (decl);
1008 /* In the case for initialing an array whose length isn't specified,
1009 where we have not yet been able to do the layout,
1010 figure out the proper alignment now. */
1011 if (dont_output_data && DECL_SIZE (decl) == 0
1012 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1013 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1015 /* Some object file formats have a maximum alignment which they support.
1016 In particular, a.out format supports a maximum alignment of 4. */
1017 if (align > MAX_OFILE_ALIGNMENT)
1019 error ("alignment of %q+D is greater than maximum object "
1020 "file alignment %d", decl,
1021 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1022 align = MAX_OFILE_ALIGNMENT;
1025 if (! DECL_USER_ALIGN (decl))
1027 #ifdef DATA_ABI_ALIGNMENT
1028 unsigned int data_abi_align
1029 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1030 /* For backwards compatibility, don't assume the ABI alignment for
1031 TLS variables. */
1032 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1033 align = data_abi_align;
1034 #endif
1036 /* On some machines, it is good to increase alignment sometimes.
1037 But as DECL_ALIGN is used both for actually emitting the variable
1038 and for code accessing the variable as guaranteed alignment, we
1039 can only increase the alignment if it is a performance optimization
1040 if the references to it must bind to the current definition. */
1041 if (decl_binds_to_current_def_p (decl)
1042 && !DECL_VIRTUAL_P (decl))
1044 #ifdef DATA_ALIGNMENT
1045 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1046 /* Don't increase alignment too much for TLS variables - TLS space
1047 is too precious. */
1048 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1049 align = data_align;
1050 #endif
1051 if (DECL_INITIAL (decl) != 0
1052 /* In LTO we have no errors in program; error_mark_node is used
1053 to mark offlined constructors. */
1054 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1056 unsigned int const_align
1057 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1058 /* Don't increase alignment too much for TLS variables - TLS
1059 space is too precious. */
1060 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1061 align = const_align;
1066 /* Reset the alignment in case we have made it tighter, so we can benefit
1067 from it in get_pointer_alignment. */
1068 SET_DECL_ALIGN (decl, align);
1071 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1072 beyond what align_variable returned. */
1074 static unsigned int
1075 get_variable_align (tree decl)
1077 unsigned int align = DECL_ALIGN (decl);
1079 /* For user aligned vars or static vars align_variable already did
1080 everything. */
1081 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1082 return align;
1084 #ifdef DATA_ABI_ALIGNMENT
1085 if (DECL_THREAD_LOCAL_P (decl))
1086 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1087 #endif
1089 /* For decls that bind to the current definition, align_variable
1090 did also everything, except for not assuming ABI required alignment
1091 of TLS variables. For other vars, increase the alignment here
1092 as an optimization. */
1093 if (!decl_binds_to_current_def_p (decl))
1095 /* On some machines, it is good to increase alignment sometimes. */
1096 #ifdef DATA_ALIGNMENT
1097 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1098 /* Don't increase alignment too much for TLS variables - TLS space
1099 is too precious. */
1100 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1101 align = data_align;
1102 #endif
1103 if (DECL_INITIAL (decl) != 0
1104 /* In LTO we have no errors in program; error_mark_node is used
1105 to mark offlined constructors. */
1106 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1108 unsigned int const_align
1109 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1110 /* Don't increase alignment too much for TLS variables - TLS space
1111 is too precious. */
1112 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1113 align = const_align;
1117 return align;
1120 /* Return the section into which the given VAR_DECL or CONST_DECL
1121 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1122 section should be used wherever possible. */
1124 section *
1125 get_variable_section (tree decl, bool prefer_noswitch_p)
1127 addr_space_t as = ADDR_SPACE_GENERIC;
1128 int reloc;
1129 varpool_node *vnode = varpool_node::get (decl);
1130 if (vnode)
1132 vnode = vnode->ultimate_alias_target ();
1133 decl = vnode->decl;
1136 if (TREE_TYPE (decl) != error_mark_node)
1137 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1139 /* We need the constructor to figure out reloc flag. */
1140 if (vnode)
1141 vnode->get_constructor ();
1143 if (DECL_COMMON (decl))
1145 /* If the decl has been given an explicit section name, or it resides
1146 in a non-generic address space, then it isn't common, and shouldn't
1147 be handled as such. */
1148 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1149 && ADDR_SPACE_GENERIC_P (as));
1150 if (DECL_THREAD_LOCAL_P (decl))
1151 return tls_comm_section;
1152 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1153 return comm_section;
1156 if (DECL_INITIAL (decl) == error_mark_node)
1157 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1158 else if (DECL_INITIAL (decl))
1159 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1160 else
1161 reloc = 0;
1163 resolve_unique_section (decl, reloc, flag_data_sections);
1164 if (IN_NAMED_SECTION (decl))
1166 section *sect = get_named_section (decl, NULL, reloc);
1168 if ((sect->common.flags & SECTION_BSS)
1169 && !bss_initializer_p (decl, true))
1171 error_at (DECL_SOURCE_LOCATION (decl),
1172 "only zero initializers are allowed in section %qs",
1173 sect->named.name);
1174 DECL_INITIAL (decl) = error_mark_node;
1176 return sect;
1179 if (ADDR_SPACE_GENERIC_P (as)
1180 && !DECL_THREAD_LOCAL_P (decl)
1181 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1182 && bss_initializer_p (decl))
1184 if (!TREE_PUBLIC (decl)
1185 && !((flag_sanitize & SANITIZE_ADDRESS)
1186 && asan_protect_global (decl)))
1187 return lcomm_section;
1188 if (bss_noswitch_section)
1189 return bss_noswitch_section;
1192 return targetm.asm_out.select_section (decl, reloc,
1193 get_variable_align (decl));
1196 /* Return the block into which object_block DECL should be placed. */
1198 static struct object_block *
1199 get_block_for_decl (tree decl)
1201 section *sect;
1203 if (VAR_P (decl))
1205 /* The object must be defined in this translation unit. */
1206 if (DECL_EXTERNAL (decl))
1207 return NULL;
1209 /* There's no point using object blocks for something that is
1210 isolated by definition. */
1211 if (DECL_COMDAT_GROUP (decl))
1212 return NULL;
1215 /* We can only calculate block offsets if the decl has a known
1216 constant size. */
1217 if (DECL_SIZE_UNIT (decl) == NULL)
1218 return NULL;
1219 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1220 return NULL;
1222 /* Find out which section should contain DECL. We cannot put it into
1223 an object block if it requires a standalone definition. */
1224 if (VAR_P (decl))
1225 align_variable (decl, 0);
1226 sect = get_variable_section (decl, true);
1227 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1228 return NULL;
1230 return get_block_for_section (sect);
1233 /* Make sure block symbol SYMBOL is in block BLOCK. */
1235 static void
1236 change_symbol_block (rtx symbol, struct object_block *block)
1238 if (block != SYMBOL_REF_BLOCK (symbol))
1240 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1241 SYMBOL_REF_BLOCK (symbol) = block;
1245 /* Return true if it is possible to put DECL in an object_block. */
1247 static bool
1248 use_blocks_for_decl_p (tree decl)
1250 struct symtab_node *snode;
1252 /* Only data DECLs can be placed into object blocks. */
1253 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1254 return false;
1256 /* Detect decls created by dw2_force_const_mem. Such decls are
1257 special because DECL_INITIAL doesn't specify the decl's true value.
1258 dw2_output_indirect_constants will instead call assemble_variable
1259 with dont_output_data set to 1 and then print the contents itself. */
1260 if (DECL_INITIAL (decl) == decl)
1261 return false;
1263 /* If this decl is an alias, then we don't want to emit a
1264 definition. */
1265 if (VAR_P (decl)
1266 && (snode = symtab_node::get (decl)) != NULL
1267 && snode->alias)
1268 return false;
1270 return targetm.use_blocks_for_decl_p (decl);
1273 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1274 until we find an identifier that is not itself a transparent alias.
1275 Modify the alias passed to it by reference (and all aliases on the
1276 way to the ultimate target), such that they do not have to be
1277 followed again, and return the ultimate target of the alias
1278 chain. */
1280 static inline tree
1281 ultimate_transparent_alias_target (tree *alias)
1283 tree target = *alias;
1285 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1287 gcc_assert (TREE_CHAIN (target));
1288 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1289 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1290 && ! TREE_CHAIN (target));
1291 *alias = target;
1294 return target;
1297 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1298 have static storage duration. In other words, it should not be an
1299 automatic variable, including PARM_DECLs.
1301 There is, however, one exception: this function handles variables
1302 explicitly placed in a particular register by the user.
1304 This is never called for PARM_DECL nodes. */
1306 void
1307 make_decl_rtl (tree decl)
1309 const char *name = 0;
1310 int reg_number;
1311 tree id;
1312 rtx x;
1314 /* Check that we are not being given an automatic variable. */
1315 gcc_assert (TREE_CODE (decl) != PARM_DECL
1316 && TREE_CODE (decl) != RESULT_DECL);
1318 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1319 gcc_assert (!VAR_P (decl)
1320 || TREE_STATIC (decl)
1321 || TREE_PUBLIC (decl)
1322 || DECL_EXTERNAL (decl)
1323 || DECL_REGISTER (decl));
1325 /* And that we were not given a type or a label. */
1326 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1327 && TREE_CODE (decl) != LABEL_DECL);
1329 /* For a duplicate declaration, we can be called twice on the
1330 same DECL node. Don't discard the RTL already made. */
1331 if (DECL_RTL_SET_P (decl))
1333 /* If the old RTL had the wrong mode, fix the mode. */
1334 x = DECL_RTL (decl);
1335 if (GET_MODE (x) != DECL_MODE (decl))
1336 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1338 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1339 return;
1341 /* ??? Another way to do this would be to maintain a hashed
1342 table of such critters. Instead of adding stuff to a DECL
1343 to give certain attributes to it, we could use an external
1344 hash map from DECL to set of attributes. */
1346 /* Let the target reassign the RTL if it wants.
1347 This is necessary, for example, when one machine specific
1348 decl attribute overrides another. */
1349 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1351 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1352 on the new decl information. */
1353 if (MEM_P (x)
1354 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1355 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1356 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1358 return;
1361 /* If this variable belongs to the global constant pool, retrieve the
1362 pre-computed RTL or recompute it in LTO mode. */
1363 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1365 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1366 return;
1369 id = DECL_ASSEMBLER_NAME (decl);
1370 if (TREE_CODE (decl) == FUNCTION_DECL
1371 && cgraph_node::get (decl)
1372 && cgraph_node::get (decl)->instrumentation_clone)
1373 ultimate_transparent_alias_target (&id);
1374 name = IDENTIFIER_POINTER (id);
1376 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1377 && DECL_REGISTER (decl))
1379 error ("register name not specified for %q+D", decl);
1381 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1383 const char *asmspec = name+1;
1384 machine_mode mode = DECL_MODE (decl);
1385 reg_number = decode_reg_name (asmspec);
1386 /* First detect errors in declaring global registers. */
1387 if (reg_number == -1)
1388 error ("register name not specified for %q+D", decl);
1389 else if (reg_number < 0)
1390 error ("invalid register name for %q+D", decl);
1391 else if (mode == BLKmode)
1392 error ("data type of %q+D isn%'t suitable for a register",
1393 decl);
1394 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1395 error ("the register specified for %q+D cannot be accessed"
1396 " by the current target", decl);
1397 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1398 error ("the register specified for %q+D is not general enough"
1399 " to be used as a register variable", decl);
1400 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1401 error ("register specified for %q+D isn%'t suitable for data type",
1402 decl);
1403 /* Now handle properly declared static register variables. */
1404 else
1406 int nregs;
1408 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1410 DECL_INITIAL (decl) = 0;
1411 error ("global register variable has initial value");
1413 if (TREE_THIS_VOLATILE (decl))
1414 warning (OPT_Wvolatile_register_var,
1415 "optimization may eliminate reads and/or "
1416 "writes to register variables");
1418 /* If the user specified one of the eliminables registers here,
1419 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1420 confused with that register and be eliminated. This usage is
1421 somewhat suspect... */
1423 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1424 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1425 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1427 if (TREE_STATIC (decl))
1429 /* Make this register global, so not usable for anything
1430 else. */
1431 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1432 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1433 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1434 #endif
1435 nregs = hard_regno_nregs (reg_number, mode);
1436 while (nregs > 0)
1437 globalize_reg (decl, reg_number + --nregs);
1440 /* As a register variable, it has no section. */
1441 return;
1443 /* Avoid internal errors from invalid register
1444 specifications. */
1445 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1446 DECL_HARD_REGISTER (decl) = 0;
1447 /* Also avoid SSA inconsistencies by pretending this is an external
1448 decl now. */
1449 DECL_EXTERNAL (decl) = 1;
1450 return;
1452 /* Now handle ordinary static variables and functions (in memory).
1453 Also handle vars declared register invalidly. */
1454 else if (name[0] == '*')
1456 #ifdef REGISTER_PREFIX
1457 if (strlen (REGISTER_PREFIX) != 0)
1459 reg_number = decode_reg_name (name);
1460 if (reg_number >= 0 || reg_number == -3)
1461 error ("register name given for non-register variable %q+D", decl);
1463 #endif
1466 /* Specifying a section attribute on a variable forces it into a
1467 non-.bss section, and thus it cannot be common. */
1468 /* FIXME: In general this code should not be necessary because
1469 visibility pass is doing the same work. But notice_global_symbol
1470 is called early and it needs to make DECL_RTL to get the name.
1471 we take care of recomputing the DECL_RTL after visibility is changed. */
1472 if (VAR_P (decl)
1473 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1474 && DECL_SECTION_NAME (decl) != NULL
1475 && DECL_INITIAL (decl) == NULL_TREE
1476 && DECL_COMMON (decl))
1477 DECL_COMMON (decl) = 0;
1479 /* Variables can't be both common and weak. */
1480 if (VAR_P (decl) && DECL_WEAK (decl))
1481 DECL_COMMON (decl) = 0;
1483 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1484 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1485 else
1487 machine_mode address_mode = Pmode;
1488 if (TREE_TYPE (decl) != error_mark_node)
1490 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1491 address_mode = targetm.addr_space.address_mode (as);
1493 x = gen_rtx_SYMBOL_REF (address_mode, name);
1495 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1496 SET_SYMBOL_REF_DECL (x, decl);
1498 x = gen_rtx_MEM (DECL_MODE (decl), x);
1499 if (TREE_CODE (decl) != FUNCTION_DECL)
1500 set_mem_attributes (x, decl, 1);
1501 SET_DECL_RTL (decl, x);
1503 /* Optionally set flags or add text to the name to record information
1504 such as that it is a function name.
1505 If the name is changed, the macro ASM_OUTPUT_LABELREF
1506 will have to know how to strip this information. */
1507 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1510 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1511 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1512 rtl. */
1515 make_decl_rtl_for_debug (tree decl)
1517 unsigned int save_aliasing_flag;
1518 rtx rtl;
1520 if (DECL_RTL_SET_P (decl))
1521 return DECL_RTL (decl);
1523 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1524 call new_alias_set. If running with -fcompare-debug, sometimes
1525 we do not want to create alias sets that will throw the alias
1526 numbers off in the comparison dumps. So... clearing
1527 flag_strict_aliasing will keep new_alias_set() from creating a
1528 new set. */
1529 save_aliasing_flag = flag_strict_aliasing;
1530 flag_strict_aliasing = 0;
1532 rtl = DECL_RTL (decl);
1533 /* Reset DECL_RTL back, as various parts of the compiler expects
1534 DECL_RTL set meaning it is actually going to be output. */
1535 SET_DECL_RTL (decl, NULL);
1537 flag_strict_aliasing = save_aliasing_flag;
1538 return rtl;
1541 /* Output a string of literal assembler code
1542 for an `asm' keyword used between functions. */
1544 void
1545 assemble_asm (tree string)
1547 const char *p;
1548 app_enable ();
1550 if (TREE_CODE (string) == ADDR_EXPR)
1551 string = TREE_OPERAND (string, 0);
1553 p = TREE_STRING_POINTER (string);
1554 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1557 /* Write the address of the entity given by SYMBOL to SEC. */
1558 void
1559 assemble_addr_to_section (rtx symbol, section *sec)
1561 switch_to_section (sec);
1562 assemble_align (POINTER_SIZE);
1563 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1566 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1567 not) section for PRIORITY. */
1568 section *
1569 get_cdtor_priority_section (int priority, bool constructor_p)
1571 /* Buffer conservatively large enough for the full range of a 32-bit
1572 int plus the text below. */
1573 char buf[18];
1575 /* ??? This only works reliably with the GNU linker. */
1576 sprintf (buf, "%s.%.5u",
1577 constructor_p ? ".ctors" : ".dtors",
1578 /* Invert the numbering so the linker puts us in the proper
1579 order; constructors are run from right to left, and the
1580 linker sorts in increasing order. */
1581 MAX_INIT_PRIORITY - priority);
1582 return get_section (buf, SECTION_WRITE, NULL);
1585 void
1586 default_named_section_asm_out_destructor (rtx symbol, int priority)
1588 section *sec;
1590 if (priority != DEFAULT_INIT_PRIORITY)
1591 sec = get_cdtor_priority_section (priority,
1592 /*constructor_p=*/false);
1593 else
1594 sec = get_section (".dtors", SECTION_WRITE, NULL);
1596 assemble_addr_to_section (symbol, sec);
1599 #ifdef DTORS_SECTION_ASM_OP
1600 void
1601 default_dtor_section_asm_out_destructor (rtx symbol,
1602 int priority ATTRIBUTE_UNUSED)
1604 assemble_addr_to_section (symbol, dtors_section);
1606 #endif
1608 void
1609 default_named_section_asm_out_constructor (rtx symbol, int priority)
1611 section *sec;
1613 if (priority != DEFAULT_INIT_PRIORITY)
1614 sec = get_cdtor_priority_section (priority,
1615 /*constructor_p=*/true);
1616 else
1617 sec = get_section (".ctors", SECTION_WRITE, NULL);
1619 assemble_addr_to_section (symbol, sec);
1622 #ifdef CTORS_SECTION_ASM_OP
1623 void
1624 default_ctor_section_asm_out_constructor (rtx symbol,
1625 int priority ATTRIBUTE_UNUSED)
1627 assemble_addr_to_section (symbol, ctors_section);
1629 #endif
1631 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1632 a nonzero value if the constant pool should be output before the
1633 start of the function, or a zero value if the pool should output
1634 after the end of the function. The default is to put it before the
1635 start. */
1637 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1638 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1639 #endif
1641 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1642 to be output to assembler.
1643 Set first_global_object_name and weak_global_object_name as appropriate. */
1645 void
1646 notice_global_symbol (tree decl)
1648 const char **t = &first_global_object_name;
1650 if (first_global_object_name
1651 || !TREE_PUBLIC (decl)
1652 || DECL_EXTERNAL (decl)
1653 || !DECL_NAME (decl)
1654 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1655 || (TREE_CODE (decl) != FUNCTION_DECL
1656 && (!VAR_P (decl)
1657 || (DECL_COMMON (decl)
1658 && (DECL_INITIAL (decl) == 0
1659 || DECL_INITIAL (decl) == error_mark_node)))))
1660 return;
1662 /* We win when global object is found, but it is useful to know about weak
1663 symbol as well so we can produce nicer unique names. */
1664 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1665 t = &weak_global_object_name;
1667 if (!*t)
1669 tree id = DECL_ASSEMBLER_NAME (decl);
1670 ultimate_transparent_alias_target (&id);
1671 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1675 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1676 current function goes into the cold section, so that targets can use
1677 current_function_section during RTL expansion. DECL describes the
1678 function. */
1680 void
1681 decide_function_section (tree decl)
1683 first_function_block_is_cold = false;
1685 if (DECL_SECTION_NAME (decl))
1687 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1688 /* Calls to function_section rely on first_function_block_is_cold
1689 being accurate. */
1690 first_function_block_is_cold = (node
1691 && node->frequency
1692 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1695 in_cold_section_p = first_function_block_is_cold;
1698 /* Get the function's name, as described by its RTL. This may be
1699 different from the DECL_NAME name used in the source file. */
1700 const char *
1701 get_fnname_from_decl (tree decl)
1703 rtx x = DECL_RTL (decl);
1704 gcc_assert (MEM_P (x));
1705 x = XEXP (x, 0);
1706 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1707 return XSTR (x, 0);
1710 /* Output assembler code for the constant pool of a function and associated
1711 with defining the name of the function. DECL describes the function.
1712 NAME is the function's name. For the constant pool, we use the current
1713 constant pool data. */
1715 void
1716 assemble_start_function (tree decl, const char *fnname)
1718 int align;
1719 char tmp_label[100];
1720 bool hot_label_written = false;
1722 if (crtl->has_bb_partition)
1724 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1725 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1726 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1727 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1728 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1729 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1730 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1731 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1732 const_labelno++;
1733 cold_function_name = NULL_TREE;
1735 else
1737 crtl->subsections.hot_section_label = NULL;
1738 crtl->subsections.cold_section_label = NULL;
1739 crtl->subsections.hot_section_end_label = NULL;
1740 crtl->subsections.cold_section_end_label = NULL;
1743 /* The following code does not need preprocessing in the assembler. */
1745 app_disable ();
1747 if (CONSTANT_POOL_BEFORE_FUNCTION)
1748 output_constant_pool (fnname, decl);
1750 align = symtab_node::get (decl)->definition_alignment ();
1752 /* Make sure the not and cold text (code) sections are properly
1753 aligned. This is necessary here in the case where the function
1754 has both hot and cold sections, because we don't want to re-set
1755 the alignment when the section switch happens mid-function. */
1757 if (crtl->has_bb_partition)
1759 first_function_block_is_cold = false;
1761 switch_to_section (unlikely_text_section ());
1762 assemble_align (align);
1763 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1765 /* When the function starts with a cold section, we need to explicitly
1766 align the hot section and write out the hot section label.
1767 But if the current function is a thunk, we do not have a CFG. */
1768 if (!cfun->is_thunk
1769 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1771 switch_to_section (text_section);
1772 assemble_align (align);
1773 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1774 hot_label_written = true;
1775 first_function_block_is_cold = true;
1777 in_cold_section_p = first_function_block_is_cold;
1781 /* Switch to the correct text section for the start of the function. */
1783 switch_to_section (function_section (decl));
1784 if (crtl->has_bb_partition && !hot_label_written)
1785 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1787 /* Tell assembler to move to target machine's alignment for functions. */
1788 align = floor_log2 (align / BITS_PER_UNIT);
1789 if (align > 0)
1791 ASM_OUTPUT_ALIGN (asm_out_file, align);
1794 /* Handle a user-specified function alignment.
1795 Note that we still need to align to DECL_ALIGN, as above,
1796 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1797 if (! DECL_USER_ALIGN (decl)
1798 && align_functions_log > align
1799 && optimize_function_for_speed_p (cfun))
1801 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1802 int align_log = align_functions_log;
1803 #endif
1804 int max_skip = align_functions - 1;
1805 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1806 && max_skip >= crtl->max_insn_address)
1807 max_skip = crtl->max_insn_address - 1;
1809 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1810 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1811 #else
1812 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1813 #endif
1816 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1817 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1818 #endif
1820 if (!DECL_IGNORED_P (decl))
1821 (*debug_hooks->begin_function) (decl);
1823 /* Make function name accessible from other files, if appropriate. */
1825 if (TREE_PUBLIC (decl)
1826 || (cgraph_node::get (decl)->instrumentation_clone
1827 && cgraph_node::get (decl)->instrumented_version
1828 && TREE_PUBLIC (cgraph_node::get (decl)->instrumented_version->decl)))
1830 notice_global_symbol (decl);
1832 globalize_decl (decl);
1834 maybe_assemble_visibility (decl);
1837 if (DECL_PRESERVE_P (decl))
1838 targetm.asm_out.mark_decl_preserved (fnname);
1840 unsigned HOST_WIDE_INT patch_area_size = function_entry_patch_area_size;
1841 unsigned HOST_WIDE_INT patch_area_entry = function_entry_patch_area_start;
1843 tree patchable_function_entry_attr
1844 = lookup_attribute ("patchable_function_entry", DECL_ATTRIBUTES (decl));
1845 if (patchable_function_entry_attr)
1847 tree pp_val = TREE_VALUE (patchable_function_entry_attr);
1848 tree patchable_function_entry_value1 = TREE_VALUE (pp_val);
1850 if (tree_fits_uhwi_p (patchable_function_entry_value1))
1851 patch_area_size = tree_to_uhwi (patchable_function_entry_value1);
1852 else
1853 gcc_unreachable ();
1855 patch_area_entry = 0;
1856 if (list_length (pp_val) > 1)
1858 tree patchable_function_entry_value2 =
1859 TREE_VALUE (TREE_CHAIN (pp_val));
1861 if (tree_fits_uhwi_p (patchable_function_entry_value2))
1862 patch_area_entry = tree_to_uhwi (patchable_function_entry_value2);
1863 else
1864 gcc_unreachable ();
1868 if (patch_area_entry > patch_area_size)
1870 if (patch_area_size > 0)
1871 warning (OPT_Wattributes, "Patchable function entry > size");
1872 patch_area_entry = 0;
1875 /* Emit the patching area before the entry label, if any. */
1876 if (patch_area_entry > 0)
1877 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1878 patch_area_entry, true);
1880 /* Do any machine/system dependent processing of the function name. */
1881 #ifdef ASM_DECLARE_FUNCTION_NAME
1882 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1883 #else
1884 /* Standard thing is just output label for the function. */
1885 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1886 #endif /* ASM_DECLARE_FUNCTION_NAME */
1888 /* And the area after the label. Record it if we haven't done so yet. */
1889 if (patch_area_size > patch_area_entry)
1890 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1891 patch_area_size-patch_area_entry,
1892 patch_area_entry == 0);
1894 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1895 saw_no_split_stack = true;
1898 /* Output assembler code associated with defining the size of the
1899 function. DECL describes the function. NAME is the function's name. */
1901 void
1902 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1904 #ifdef ASM_DECLARE_FUNCTION_SIZE
1905 /* We could have switched section in the middle of the function. */
1906 if (crtl->has_bb_partition)
1907 switch_to_section (function_section (decl));
1908 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1909 #endif
1910 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1912 output_constant_pool (fnname, decl);
1913 switch_to_section (function_section (decl)); /* need to switch back */
1915 /* Output labels for end of hot/cold text sections (to be used by
1916 debug info.) */
1917 if (crtl->has_bb_partition)
1919 section *save_text_section;
1921 save_text_section = in_section;
1922 switch_to_section (unlikely_text_section ());
1923 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
1924 if (cold_function_name != NULL_TREE)
1925 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
1926 IDENTIFIER_POINTER (cold_function_name),
1927 decl);
1928 #endif
1929 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1930 if (first_function_block_is_cold)
1931 switch_to_section (text_section);
1932 else
1933 switch_to_section (function_section (decl));
1934 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1935 switch_to_section (save_text_section);
1939 /* Assemble code to leave SIZE bytes of zeros. */
1941 void
1942 assemble_zeros (unsigned HOST_WIDE_INT size)
1944 /* Do no output if -fsyntax-only. */
1945 if (flag_syntax_only)
1946 return;
1948 #ifdef ASM_NO_SKIP_IN_TEXT
1949 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1950 so we must output 0s explicitly in the text section. */
1951 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1953 unsigned HOST_WIDE_INT i;
1954 for (i = 0; i < size; i++)
1955 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1957 else
1958 #endif
1959 if (size > 0)
1960 ASM_OUTPUT_SKIP (asm_out_file, size);
1963 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1965 void
1966 assemble_align (int align)
1968 if (align > BITS_PER_UNIT)
1970 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1974 /* Assemble a string constant with the specified C string as contents. */
1976 void
1977 assemble_string (const char *p, int size)
1979 int pos = 0;
1980 int maximum = 2000;
1982 /* If the string is very long, split it up. */
1984 while (pos < size)
1986 int thissize = size - pos;
1987 if (thissize > maximum)
1988 thissize = maximum;
1990 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1992 pos += thissize;
1993 p += thissize;
1998 /* A noswitch_section_callback for lcomm_section. */
2000 static bool
2001 emit_local (tree decl ATTRIBUTE_UNUSED,
2002 const char *name ATTRIBUTE_UNUSED,
2003 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2004 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2006 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2007 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2008 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2009 size, align);
2010 return true;
2011 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2012 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2013 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2014 return true;
2015 #else
2016 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2017 return false;
2018 #endif
2021 /* A noswitch_section_callback for bss_noswitch_section. */
2023 #if defined ASM_OUTPUT_ALIGNED_BSS
2024 static bool
2025 emit_bss (tree decl ATTRIBUTE_UNUSED,
2026 const char *name ATTRIBUTE_UNUSED,
2027 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2028 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2030 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2031 get_variable_align (decl));
2032 return true;
2034 #endif
2036 /* A noswitch_section_callback for comm_section. */
2038 static bool
2039 emit_common (tree decl ATTRIBUTE_UNUSED,
2040 const char *name ATTRIBUTE_UNUSED,
2041 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2042 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2044 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2045 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2046 size, get_variable_align (decl));
2047 return true;
2048 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2049 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2050 get_variable_align (decl));
2051 return true;
2052 #else
2053 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2054 return false;
2055 #endif
2058 /* A noswitch_section_callback for tls_comm_section. */
2060 static bool
2061 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2062 const char *name ATTRIBUTE_UNUSED,
2063 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2064 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2066 #ifdef ASM_OUTPUT_TLS_COMMON
2067 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2068 return true;
2069 #else
2070 sorry ("thread-local COMMON data not implemented");
2071 return true;
2072 #endif
2075 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2076 NAME is the name of DECL's SYMBOL_REF. */
2078 static void
2079 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2080 unsigned int align)
2082 unsigned HOST_WIDE_INT size, rounded;
2084 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2085 rounded = size;
2087 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2088 size += asan_red_zone_size (size);
2090 /* Don't allocate zero bytes of common,
2091 since that means "undefined external" in the linker. */
2092 if (size == 0)
2093 rounded = 1;
2095 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2096 so that each uninitialized object starts on such a boundary. */
2097 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2098 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2099 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2101 if (!sect->noswitch.callback (decl, name, size, rounded)
2102 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2103 error ("requested alignment for %q+D is greater than "
2104 "implemented alignment of %wu", decl, rounded);
2107 /* A subroutine of assemble_variable. Output the label and contents of
2108 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2109 is as for assemble_variable. */
2111 static void
2112 assemble_variable_contents (tree decl, const char *name,
2113 bool dont_output_data)
2115 /* Do any machine/system dependent processing of the object. */
2116 #ifdef ASM_DECLARE_OBJECT_NAME
2117 last_assemble_variable_decl = decl;
2118 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2119 #else
2120 /* Standard thing is just output label for the object. */
2121 ASM_OUTPUT_LABEL (asm_out_file, name);
2122 #endif /* ASM_DECLARE_OBJECT_NAME */
2124 if (!dont_output_data)
2126 /* Caller is supposed to use varpool_get_constructor when it wants
2127 to output the body. */
2128 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2129 if (DECL_INITIAL (decl)
2130 && DECL_INITIAL (decl) != error_mark_node
2131 && !initializer_zerop (DECL_INITIAL (decl)))
2132 /* Output the actual data. */
2133 output_constant (DECL_INITIAL (decl),
2134 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2135 get_variable_align (decl),
2136 false);
2137 else
2138 /* Leave space for it. */
2139 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2140 targetm.asm_out.decl_end ();
2144 /* Write out assembly for the variable DECL, which is not defined in
2145 the current translation unit. */
2146 void
2147 assemble_undefined_decl (tree decl)
2149 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2150 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2153 /* Assemble everything that is needed for a variable or function declaration.
2154 Not used for automatic variables, and not used for function definitions.
2155 Should not be called for variables of incomplete structure type.
2157 TOP_LEVEL is nonzero if this variable has file scope.
2158 AT_END is nonzero if this is the special handling, at end of compilation,
2159 to define things that have had only tentative definitions.
2160 DONT_OUTPUT_DATA if nonzero means don't actually output the
2161 initial value (that will be done by the caller). */
2163 void
2164 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2165 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2167 const char *name;
2168 rtx decl_rtl, symbol;
2169 section *sect;
2170 unsigned int align;
2171 bool asan_protected = false;
2173 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2174 gcc_assert (VAR_P (decl));
2176 /* Emulated TLS had better not get this far. */
2177 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2179 last_assemble_variable_decl = 0;
2181 /* Normally no need to say anything here for external references,
2182 since assemble_external is called by the language-specific code
2183 when a declaration is first seen. */
2185 if (DECL_EXTERNAL (decl))
2186 return;
2188 /* Do nothing for global register variables. */
2189 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2191 TREE_ASM_WRITTEN (decl) = 1;
2192 return;
2195 /* If type was incomplete when the variable was declared,
2196 see if it is complete now. */
2198 if (DECL_SIZE (decl) == 0)
2199 layout_decl (decl, 0);
2201 /* Still incomplete => don't allocate it; treat the tentative defn
2202 (which is what it must have been) as an `extern' reference. */
2204 if (!dont_output_data && DECL_SIZE (decl) == 0)
2206 error ("storage size of %q+D isn%'t known", decl);
2207 TREE_ASM_WRITTEN (decl) = 1;
2208 return;
2211 /* The first declaration of a variable that comes through this function
2212 decides whether it is global (in C, has external linkage)
2213 or local (in C, has internal linkage). So do nothing more
2214 if this function has already run. */
2216 if (TREE_ASM_WRITTEN (decl))
2217 return;
2219 /* Make sure targetm.encode_section_info is invoked before we set
2220 ASM_WRITTEN. */
2221 decl_rtl = DECL_RTL (decl);
2223 TREE_ASM_WRITTEN (decl) = 1;
2225 /* Do no output if -fsyntax-only. */
2226 if (flag_syntax_only)
2227 return;
2229 if (! dont_output_data
2230 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2232 error ("size of variable %q+D is too large", decl);
2233 return;
2236 gcc_assert (MEM_P (decl_rtl));
2237 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2238 symbol = XEXP (decl_rtl, 0);
2240 /* If this symbol belongs to the tree constant pool, output the constant
2241 if it hasn't already been written. */
2242 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2244 tree decl = SYMBOL_REF_DECL (symbol);
2245 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2246 output_constant_def_contents (symbol);
2247 return;
2250 app_disable ();
2252 name = XSTR (symbol, 0);
2253 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2254 notice_global_symbol (decl);
2256 /* Compute the alignment of this data. */
2258 align_variable (decl, dont_output_data);
2260 if ((flag_sanitize & SANITIZE_ADDRESS)
2261 && asan_protect_global (decl))
2263 asan_protected = true;
2264 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2265 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2268 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2270 align = get_variable_align (decl);
2272 if (TREE_PUBLIC (decl))
2273 maybe_assemble_visibility (decl);
2275 if (DECL_PRESERVE_P (decl))
2276 targetm.asm_out.mark_decl_preserved (name);
2278 /* First make the assembler name(s) global if appropriate. */
2279 sect = get_variable_section (decl, false);
2280 if (TREE_PUBLIC (decl)
2281 && (sect->common.flags & SECTION_COMMON) == 0)
2282 globalize_decl (decl);
2284 /* Output any data that we will need to use the address of. */
2285 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2286 output_addressed_constants (DECL_INITIAL (decl));
2288 /* dbxout.c needs to know this. */
2289 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2290 DECL_IN_TEXT_SECTION (decl) = 1;
2292 /* If the decl is part of an object_block, make sure that the decl
2293 has been positioned within its block, but do not write out its
2294 definition yet. output_object_blocks will do that later. */
2295 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2297 gcc_assert (!dont_output_data);
2298 place_block_symbol (symbol);
2300 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2301 assemble_noswitch_variable (decl, name, sect, align);
2302 else
2304 /* Special-case handling of vtv comdat sections. */
2305 if (sect->named.name
2306 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2307 handle_vtv_comdat_section (sect, decl);
2308 else
2309 switch_to_section (sect);
2310 if (align > BITS_PER_UNIT)
2311 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2312 assemble_variable_contents (decl, name, dont_output_data);
2313 if (asan_protected)
2315 unsigned HOST_WIDE_INT int size
2316 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2317 assemble_zeros (asan_red_zone_size (size));
2323 /* Given a function declaration (FN_DECL), this function assembles the
2324 function into the .preinit_array section. */
2326 void
2327 assemble_vtv_preinit_initializer (tree fn_decl)
2329 section *sect;
2330 unsigned flags = SECTION_WRITE;
2331 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2333 flags |= SECTION_NOTYPE;
2334 sect = get_section (".preinit_array", flags, fn_decl);
2335 switch_to_section (sect);
2336 assemble_addr_to_section (symbol, sect);
2339 /* Return 1 if type TYPE contains any pointers. */
2341 static int
2342 contains_pointers_p (tree type)
2344 switch (TREE_CODE (type))
2346 case POINTER_TYPE:
2347 case REFERENCE_TYPE:
2348 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2349 so I'll play safe and return 1. */
2350 case OFFSET_TYPE:
2351 return 1;
2353 case RECORD_TYPE:
2354 case UNION_TYPE:
2355 case QUAL_UNION_TYPE:
2357 tree fields;
2358 /* For a type that has fields, see if the fields have pointers. */
2359 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2360 if (TREE_CODE (fields) == FIELD_DECL
2361 && contains_pointers_p (TREE_TYPE (fields)))
2362 return 1;
2363 return 0;
2366 case ARRAY_TYPE:
2367 /* An array type contains pointers if its element type does. */
2368 return contains_pointers_p (TREE_TYPE (type));
2370 default:
2371 return 0;
2375 /* We delay assemble_external processing until
2376 the compilation unit is finalized. This is the best we can do for
2377 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2378 it all the way to final. See PR 17982 for further discussion. */
2379 static GTY(()) tree pending_assemble_externals;
2381 #ifdef ASM_OUTPUT_EXTERNAL
2382 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2383 As a result, assemble_external can be called after the list of externals
2384 is processed and the pointer set destroyed. */
2385 static bool pending_assemble_externals_processed;
2387 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2388 TREE_LIST in assemble_external. */
2389 static hash_set<tree> *pending_assemble_externals_set;
2391 /* True if DECL is a function decl for which no out-of-line copy exists.
2392 It is assumed that DECL's assembler name has been set. */
2394 static bool
2395 incorporeal_function_p (tree decl)
2397 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2399 const char *name;
2401 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2402 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2403 return true;
2405 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2406 /* Atomic or sync builtins which have survived this far will be
2407 resolved externally and therefore are not incorporeal. */
2408 if (strncmp (name, "__builtin_", 10) == 0)
2409 return true;
2411 return false;
2414 /* Actually do the tests to determine if this is necessary, and invoke
2415 ASM_OUTPUT_EXTERNAL. */
2416 static void
2417 assemble_external_real (tree decl)
2419 rtx rtl = DECL_RTL (decl);
2421 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2422 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2423 && !incorporeal_function_p (decl))
2425 /* Some systems do require some output. */
2426 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2427 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2430 #endif
2432 void
2433 process_pending_assemble_externals (void)
2435 #ifdef ASM_OUTPUT_EXTERNAL
2436 tree list;
2437 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2438 assemble_external_real (TREE_VALUE (list));
2440 pending_assemble_externals = 0;
2441 pending_assemble_externals_processed = true;
2442 delete pending_assemble_externals_set;
2443 #endif
2446 /* This TREE_LIST contains any weak symbol declarations waiting
2447 to be emitted. */
2448 static GTY(()) tree weak_decls;
2450 /* Output something to declare an external symbol to the assembler,
2451 and qualifiers such as weakness. (Most assemblers don't need
2452 extern declaration, so we normally output nothing.) Do nothing if
2453 DECL is not external. */
2455 void
2456 assemble_external (tree decl ATTRIBUTE_UNUSED)
2458 /* Make sure that the ASM_OUT_FILE is open.
2459 If it's not, we should not be calling this function. */
2460 gcc_assert (asm_out_file);
2462 /* In a perfect world, the following condition would be true.
2463 Sadly, the Go front end emit assembly *from the front end*,
2464 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2465 #if 0
2466 /* This function should only be called if we are expanding, or have
2467 expanded, to RTL.
2468 Ideally, only final.c would be calling this function, but it is
2469 not clear whether that would break things somehow. See PR 17982
2470 for further discussion. */
2471 gcc_assert (state == EXPANSION
2472 || state == FINISHED);
2473 #endif
2475 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2476 return;
2478 /* We want to output annotation for weak and external symbols at
2479 very last to check if they are references or not. */
2481 if (TARGET_SUPPORTS_WEAK
2482 && DECL_WEAK (decl)
2483 /* TREE_STATIC is a weird and abused creature which is not
2484 generally the right test for whether an entity has been
2485 locally emitted, inlined or otherwise not-really-extern, but
2486 for declarations that can be weak, it happens to be
2487 match. */
2488 && !TREE_STATIC (decl)
2489 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2490 && value_member (decl, weak_decls) == NULL_TREE)
2491 weak_decls = tree_cons (NULL, decl, weak_decls);
2493 #ifdef ASM_OUTPUT_EXTERNAL
2494 if (pending_assemble_externals_processed)
2496 assemble_external_real (decl);
2497 return;
2500 if (! pending_assemble_externals_set->add (decl))
2501 pending_assemble_externals = tree_cons (NULL, decl,
2502 pending_assemble_externals);
2503 #endif
2506 /* Similar, for calling a library function FUN. */
2508 void
2509 assemble_external_libcall (rtx fun)
2511 /* Declare library function name external when first used, if nec. */
2512 if (! SYMBOL_REF_USED (fun))
2514 SYMBOL_REF_USED (fun) = 1;
2515 targetm.asm_out.external_libcall (fun);
2519 /* Assemble a label named NAME. */
2521 void
2522 assemble_label (FILE *file, const char *name)
2524 ASM_OUTPUT_LABEL (file, name);
2527 /* Set the symbol_referenced flag for ID. */
2528 void
2529 mark_referenced (tree id)
2531 TREE_SYMBOL_REFERENCED (id) = 1;
2534 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2535 void
2536 mark_decl_referenced (tree decl)
2538 if (TREE_CODE (decl) == FUNCTION_DECL)
2540 /* Extern inline functions don't become needed when referenced.
2541 If we know a method will be emitted in other TU and no new
2542 functions can be marked reachable, just use the external
2543 definition. */
2544 struct cgraph_node *node = cgraph_node::get_create (decl);
2545 if (!DECL_EXTERNAL (decl)
2546 && !node->definition)
2547 node->mark_force_output ();
2549 else if (VAR_P (decl))
2551 varpool_node *node = varpool_node::get_create (decl);
2552 /* C++ frontend use mark_decl_references to force COMDAT variables
2553 to be output that might appear dead otherwise. */
2554 node->force_output = true;
2556 /* else do nothing - we can get various sorts of CST nodes here,
2557 which do not need to be marked. */
2561 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2562 starts with a *, the rest of NAME is output verbatim. Otherwise
2563 NAME is transformed in a target-specific way (usually by the
2564 addition of an underscore). */
2566 void
2567 assemble_name_raw (FILE *file, const char *name)
2569 if (name[0] == '*')
2570 fputs (&name[1], file);
2571 else
2572 ASM_OUTPUT_LABELREF (file, name);
2575 /* Like assemble_name_raw, but should be used when NAME might refer to
2576 an entity that is also represented as a tree (like a function or
2577 variable). If NAME does refer to such an entity, that entity will
2578 be marked as referenced. */
2580 void
2581 assemble_name (FILE *file, const char *name)
2583 const char *real_name;
2584 tree id;
2586 real_name = targetm.strip_name_encoding (name);
2588 id = maybe_get_identifier (real_name);
2589 if (id)
2591 tree id_orig = id;
2593 mark_referenced (id);
2594 ultimate_transparent_alias_target (&id);
2595 if (id != id_orig)
2596 name = IDENTIFIER_POINTER (id);
2597 gcc_assert (! TREE_CHAIN (id));
2600 assemble_name_raw (file, name);
2603 /* Allocate SIZE bytes writable static space with a gensym name
2604 and return an RTX to refer to its address. */
2607 assemble_static_space (unsigned HOST_WIDE_INT size)
2609 char name[17];
2610 const char *namestring;
2611 rtx x;
2613 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2614 ++const_labelno;
2615 namestring = ggc_strdup (name);
2617 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2618 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2620 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2621 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2622 BIGGEST_ALIGNMENT);
2623 #else
2624 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2625 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2626 #else
2628 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2629 so that each uninitialized object starts on such a boundary. */
2630 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2631 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2632 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2633 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2634 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2635 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2637 #endif
2638 #endif
2639 return x;
2642 /* Assemble the static constant template for function entry trampolines.
2643 This is done at most once per compilation.
2644 Returns an RTX for the address of the template. */
2646 static GTY(()) rtx initial_trampoline;
2649 assemble_trampoline_template (void)
2651 char label[256];
2652 const char *name;
2653 int align;
2654 rtx symbol;
2656 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2658 if (initial_trampoline)
2659 return initial_trampoline;
2661 /* By default, put trampoline templates in read-only data section. */
2663 #ifdef TRAMPOLINE_SECTION
2664 switch_to_section (TRAMPOLINE_SECTION);
2665 #else
2666 switch_to_section (readonly_data_section);
2667 #endif
2669 /* Write the assembler code to define one. */
2670 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2671 if (align > 0)
2672 ASM_OUTPUT_ALIGN (asm_out_file, align);
2674 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2675 targetm.asm_out.trampoline_template (asm_out_file);
2677 /* Record the rtl to refer to it. */
2678 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2679 name = ggc_strdup (label);
2680 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2681 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2683 initial_trampoline = gen_const_mem (BLKmode, symbol);
2684 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2685 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2687 return initial_trampoline;
2690 /* A and B are either alignments or offsets. Return the minimum alignment
2691 that may be assumed after adding the two together. */
2693 static inline unsigned
2694 min_align (unsigned int a, unsigned int b)
2696 return least_bit_hwi (a | b);
2699 /* Return the assembler directive for creating a given kind of integer
2700 object. SIZE is the number of bytes in the object and ALIGNED_P
2701 indicates whether it is known to be aligned. Return NULL if the
2702 assembly dialect has no such directive.
2704 The returned string should be printed at the start of a new line and
2705 be followed immediately by the object's initial value. */
2707 const char *
2708 integer_asm_op (int size, int aligned_p)
2710 struct asm_int_op *ops;
2712 if (aligned_p)
2713 ops = &targetm.asm_out.aligned_op;
2714 else
2715 ops = &targetm.asm_out.unaligned_op;
2717 switch (size)
2719 case 1:
2720 return targetm.asm_out.byte_op;
2721 case 2:
2722 return ops->hi;
2723 case 4:
2724 return ops->si;
2725 case 8:
2726 return ops->di;
2727 case 16:
2728 return ops->ti;
2729 default:
2730 return NULL;
2734 /* Use directive OP to assemble an integer object X. Print OP at the
2735 start of the line, followed immediately by the value of X. */
2737 void
2738 assemble_integer_with_op (const char *op, rtx x)
2740 fputs (op, asm_out_file);
2741 output_addr_const (asm_out_file, x);
2742 fputc ('\n', asm_out_file);
2745 /* The default implementation of the asm_out.integer target hook. */
2747 bool
2748 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2749 unsigned int size ATTRIBUTE_UNUSED,
2750 int aligned_p ATTRIBUTE_UNUSED)
2752 const char *op = integer_asm_op (size, aligned_p);
2753 /* Avoid GAS bugs for large values. Specifically negative values whose
2754 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2755 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2756 return false;
2757 return op && (assemble_integer_with_op (op, x), true);
2760 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2761 the alignment of the integer in bits. Return 1 if we were able to output
2762 the constant, otherwise 0. We must be able to output the constant,
2763 if FORCE is nonzero. */
2765 bool
2766 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2768 int aligned_p;
2770 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2772 /* See if the target hook can handle this kind of object. */
2773 if (targetm.asm_out.integer (x, size, aligned_p))
2774 return true;
2776 /* If the object is a multi-byte one, try splitting it up. Split
2777 it into words it if is multi-word, otherwise split it into bytes. */
2778 if (size > 1)
2780 machine_mode omode, imode;
2781 unsigned int subalign;
2782 unsigned int subsize, i;
2783 enum mode_class mclass;
2785 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2786 subalign = MIN (align, subsize * BITS_PER_UNIT);
2787 if (GET_CODE (x) == CONST_FIXED)
2788 mclass = GET_MODE_CLASS (GET_MODE (x));
2789 else
2790 mclass = MODE_INT;
2792 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2793 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2795 for (i = 0; i < size; i += subsize)
2797 rtx partial = simplify_subreg (omode, x, imode, i);
2798 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2799 break;
2801 if (i == size)
2802 return true;
2804 /* If we've printed some of it, but not all of it, there's no going
2805 back now. */
2806 gcc_assert (!i);
2809 gcc_assert (!force);
2811 return false;
2814 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
2815 is the alignment of the constant in bits. If REVERSE is true, D is output
2816 in reverse storage order. */
2818 void
2819 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2820 bool reverse)
2822 long data[4] = {0, 0, 0, 0};
2823 int bitsize, nelts, nunits, units_per;
2824 rtx elt;
2826 /* This is hairy. We have a quantity of known size. real_to_target
2827 will put it into an array of *host* longs, 32 bits per element
2828 (even if long is more than 32 bits). We need to determine the
2829 number of array elements that are occupied (nelts) and the number
2830 of *target* min-addressable units that will be occupied in the
2831 object file (nunits). We cannot assume that 32 divides the
2832 mode's bitsize (size * BITS_PER_UNIT) evenly.
2834 size * BITS_PER_UNIT is used here to make sure that padding bits
2835 (which might appear at either end of the value; real_to_target
2836 will include the padding bits in its output array) are included. */
2838 nunits = GET_MODE_SIZE (mode);
2839 bitsize = nunits * BITS_PER_UNIT;
2840 nelts = CEIL (bitsize, 32);
2841 units_per = 32 / BITS_PER_UNIT;
2843 real_to_target (data, &d, mode);
2845 /* Put out the first word with the specified alignment. */
2846 if (reverse)
2847 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
2848 else
2849 elt = GEN_INT (data[0]);
2850 assemble_integer (elt, MIN (nunits, units_per), align, 1);
2851 nunits -= units_per;
2853 /* Subsequent words need only 32-bit alignment. */
2854 align = min_align (align, 32);
2856 for (int i = 1; i < nelts; i++)
2858 if (reverse)
2859 elt = flip_storage_order (SImode,
2860 gen_int_mode (data[nelts - 1 - i], SImode));
2861 else
2862 elt = GEN_INT (data[i]);
2863 assemble_integer (elt, MIN (nunits, units_per), align, 1);
2864 nunits -= units_per;
2868 /* Given an expression EXP with a constant value,
2869 reduce it to the sum of an assembler symbol and an integer.
2870 Store them both in the structure *VALUE.
2871 EXP must be reducible. */
2873 struct addr_const {
2874 rtx base;
2875 poly_int64 offset;
2878 static void
2879 decode_addr_const (tree exp, struct addr_const *value)
2881 tree target = TREE_OPERAND (exp, 0);
2882 poly_int64 offset = 0;
2883 rtx x;
2885 while (1)
2887 poly_int64 bytepos;
2888 if (TREE_CODE (target) == COMPONENT_REF
2889 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
2890 &bytepos))
2892 offset += bytepos;
2893 target = TREE_OPERAND (target, 0);
2895 else if (TREE_CODE (target) == ARRAY_REF
2896 || TREE_CODE (target) == ARRAY_RANGE_REF)
2898 /* Truncate big offset. */
2899 offset
2900 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2901 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
2902 target = TREE_OPERAND (target, 0);
2904 else if (TREE_CODE (target) == MEM_REF
2905 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2907 offset += mem_ref_offset (target).force_shwi ();
2908 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2910 else if (TREE_CODE (target) == INDIRECT_REF
2911 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2912 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2913 == ADDR_EXPR)
2914 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2915 else
2916 break;
2919 switch (TREE_CODE (target))
2921 case VAR_DECL:
2922 case FUNCTION_DECL:
2923 x = DECL_RTL (target);
2924 break;
2926 case LABEL_DECL:
2927 x = gen_rtx_MEM (FUNCTION_MODE,
2928 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2929 break;
2931 case REAL_CST:
2932 case FIXED_CST:
2933 case STRING_CST:
2934 case COMPLEX_CST:
2935 case CONSTRUCTOR:
2936 case INTEGER_CST:
2937 x = output_constant_def (target, 1);
2938 break;
2940 case INDIRECT_REF:
2941 /* This deals with absolute addresses. */
2942 offset += tree_to_shwi (TREE_OPERAND (target, 0));
2943 x = gen_rtx_MEM (QImode,
2944 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
2945 break;
2947 default:
2948 gcc_unreachable ();
2951 gcc_assert (MEM_P (x));
2952 x = XEXP (x, 0);
2954 value->base = x;
2955 value->offset = offset;
2958 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2960 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2962 /* Constant pool accessor function. */
2964 hash_table<tree_descriptor_hasher> *
2965 constant_pool_htab (void)
2967 return const_desc_htab;
2970 /* Compute a hash code for a constant expression. */
2972 hashval_t
2973 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2975 return ptr->hash;
2978 static hashval_t
2979 const_hash_1 (const tree exp)
2981 const char *p;
2982 hashval_t hi;
2983 int len, i;
2984 enum tree_code code = TREE_CODE (exp);
2986 /* Either set P and LEN to the address and len of something to hash and
2987 exit the switch or return a value. */
2989 switch (code)
2991 case INTEGER_CST:
2992 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2993 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2994 break;
2996 case REAL_CST:
2997 return real_hash (TREE_REAL_CST_PTR (exp));
2999 case FIXED_CST:
3000 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3002 case STRING_CST:
3003 p = TREE_STRING_POINTER (exp);
3004 len = TREE_STRING_LENGTH (exp);
3005 break;
3007 case COMPLEX_CST:
3008 return (const_hash_1 (TREE_REALPART (exp)) * 5
3009 + const_hash_1 (TREE_IMAGPART (exp)));
3011 case VECTOR_CST:
3013 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3014 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3015 unsigned int count = vector_cst_encoded_nelts (exp);
3016 for (unsigned int i = 0; i < count; ++i)
3017 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3018 return hi;
3021 case CONSTRUCTOR:
3023 unsigned HOST_WIDE_INT idx;
3024 tree value;
3026 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3028 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3029 if (value)
3030 hi = hi * 603 + const_hash_1 (value);
3032 return hi;
3035 case ADDR_EXPR:
3036 case FDESC_EXPR:
3038 struct addr_const value;
3040 decode_addr_const (exp, &value);
3041 switch (GET_CODE (value.base))
3043 case SYMBOL_REF:
3044 /* Don't hash the address of the SYMBOL_REF;
3045 only use the offset and the symbol name. */
3046 hi = value.offset.coeffs[0];
3047 p = XSTR (value.base, 0);
3048 for (i = 0; p[i] != 0; i++)
3049 hi = ((hi * 613) + (unsigned) (p[i]));
3050 break;
3052 case LABEL_REF:
3053 hi = (value.offset.coeffs[0]
3054 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3055 break;
3057 default:
3058 gcc_unreachable ();
3061 return hi;
3063 case PLUS_EXPR:
3064 case POINTER_PLUS_EXPR:
3065 case MINUS_EXPR:
3066 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3067 + const_hash_1 (TREE_OPERAND (exp, 1)));
3069 CASE_CONVERT:
3070 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3072 default:
3073 /* A language specific constant. Just hash the code. */
3074 return code;
3077 /* Compute hashing function. */
3078 hi = len;
3079 for (i = 0; i < len; i++)
3080 hi = ((hi * 613) + (unsigned) (p[i]));
3082 return hi;
3085 /* Wrapper of compare_constant, for the htab interface. */
3086 bool
3087 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3088 constant_descriptor_tree *c2)
3090 if (c1->hash != c2->hash)
3091 return 0;
3092 return compare_constant (c1->value, c2->value);
3095 /* Compare t1 and t2, and return 1 only if they are known to result in
3096 the same bit pattern on output. */
3098 static int
3099 compare_constant (const tree t1, const tree t2)
3101 enum tree_code typecode;
3103 if (t1 == NULL_TREE)
3104 return t2 == NULL_TREE;
3105 if (t2 == NULL_TREE)
3106 return 0;
3108 if (TREE_CODE (t1) != TREE_CODE (t2))
3109 return 0;
3111 switch (TREE_CODE (t1))
3113 case INTEGER_CST:
3114 /* Integer constants are the same only if the same width of type. */
3115 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3116 return 0;
3117 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3118 return 0;
3119 return tree_int_cst_equal (t1, t2);
3121 case REAL_CST:
3122 /* Real constants are the same only if the same width of type. In
3123 addition to the same width, we need to check whether the modes are the
3124 same. There might be two floating point modes that are the same size
3125 but have different representations, such as the PowerPC that has 2
3126 different 128-bit floating point types (IBM extended double and IEEE
3127 128-bit floating point). */
3128 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3129 return 0;
3130 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3131 return 0;
3132 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3134 case FIXED_CST:
3135 /* Fixed constants are the same only if the same width of type. */
3136 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3137 return 0;
3139 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3141 case STRING_CST:
3142 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3143 return 0;
3145 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3146 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3147 TREE_STRING_LENGTH (t1)));
3149 case COMPLEX_CST:
3150 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3151 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3153 case VECTOR_CST:
3155 if (VECTOR_CST_NPATTERNS (t1)
3156 != VECTOR_CST_NPATTERNS (t2))
3157 return 0;
3159 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3160 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3161 return 0;
3163 unsigned int count = vector_cst_encoded_nelts (t1);
3164 for (unsigned int i = 0; i < count; ++i)
3165 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3166 VECTOR_CST_ENCODED_ELT (t2, i)))
3167 return 0;
3169 return 1;
3172 case CONSTRUCTOR:
3174 vec<constructor_elt, va_gc> *v1, *v2;
3175 unsigned HOST_WIDE_INT idx;
3177 typecode = TREE_CODE (TREE_TYPE (t1));
3178 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3179 return 0;
3181 if (typecode == ARRAY_TYPE)
3183 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3184 /* For arrays, check that mode, size and storage order match. */
3185 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3186 || size_1 == -1
3187 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3188 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3189 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3190 return 0;
3192 else
3194 /* For record and union constructors, require exact type
3195 equality. */
3196 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3197 return 0;
3200 v1 = CONSTRUCTOR_ELTS (t1);
3201 v2 = CONSTRUCTOR_ELTS (t2);
3202 if (vec_safe_length (v1) != vec_safe_length (v2))
3203 return 0;
3205 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3207 constructor_elt *c1 = &(*v1)[idx];
3208 constructor_elt *c2 = &(*v2)[idx];
3210 /* Check that each value is the same... */
3211 if (!compare_constant (c1->value, c2->value))
3212 return 0;
3213 /* ... and that they apply to the same fields! */
3214 if (typecode == ARRAY_TYPE)
3216 if (!compare_constant (c1->index, c2->index))
3217 return 0;
3219 else
3221 if (c1->index != c2->index)
3222 return 0;
3226 return 1;
3229 case ADDR_EXPR:
3230 case FDESC_EXPR:
3232 struct addr_const value1, value2;
3233 enum rtx_code code;
3234 int ret;
3236 decode_addr_const (t1, &value1);
3237 decode_addr_const (t2, &value2);
3239 if (maybe_ne (value1.offset, value2.offset))
3240 return 0;
3242 code = GET_CODE (value1.base);
3243 if (code != GET_CODE (value2.base))
3244 return 0;
3246 switch (code)
3248 case SYMBOL_REF:
3249 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3250 break;
3252 case LABEL_REF:
3253 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3254 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3255 break;
3257 default:
3258 gcc_unreachable ();
3260 return ret;
3263 case PLUS_EXPR:
3264 case POINTER_PLUS_EXPR:
3265 case MINUS_EXPR:
3266 case RANGE_EXPR:
3267 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3268 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3270 CASE_CONVERT:
3271 case VIEW_CONVERT_EXPR:
3272 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3274 default:
3275 return 0;
3278 gcc_unreachable ();
3281 /* Return the section into which constant EXP should be placed. */
3283 static section *
3284 get_constant_section (tree exp, unsigned int align)
3286 return targetm.asm_out.select_section (exp,
3287 compute_reloc_for_constant (exp),
3288 align);
3291 /* Return the size of constant EXP in bytes. */
3293 static HOST_WIDE_INT
3294 get_constant_size (tree exp)
3296 HOST_WIDE_INT size;
3298 size = int_size_in_bytes (TREE_TYPE (exp));
3299 if (TREE_CODE (exp) == STRING_CST)
3300 size = MAX (TREE_STRING_LENGTH (exp), size);
3301 return size;
3304 /* Subroutine of output_constant_def:
3305 No constant equal to EXP is known to have been output.
3306 Make a constant descriptor to enter EXP in the hash table.
3307 Assign the label number and construct RTL to refer to the
3308 constant's location in memory.
3309 Caller is responsible for updating the hash table. */
3311 static struct constant_descriptor_tree *
3312 build_constant_desc (tree exp)
3314 struct constant_descriptor_tree *desc;
3315 rtx symbol, rtl;
3316 char label[256];
3317 int labelno;
3318 tree decl;
3320 desc = ggc_alloc<constant_descriptor_tree> ();
3321 desc->value = exp;
3323 /* Create a string containing the label name, in LABEL. */
3324 labelno = const_labelno++;
3325 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3327 /* Construct the VAR_DECL associated with the constant. */
3328 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3329 TREE_TYPE (exp));
3330 DECL_ARTIFICIAL (decl) = 1;
3331 DECL_IGNORED_P (decl) = 1;
3332 TREE_READONLY (decl) = 1;
3333 TREE_STATIC (decl) = 1;
3334 TREE_ADDRESSABLE (decl) = 1;
3335 /* We don't set the RTL yet as this would cause varpool to assume that the
3336 variable is referenced. Moreover, it would just be dropped in LTO mode.
3337 Instead we set the flag that will be recognized in make_decl_rtl. */
3338 DECL_IN_CONSTANT_POOL (decl) = 1;
3339 DECL_INITIAL (decl) = desc->value;
3340 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3341 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3342 if (TREE_CODE (exp) == STRING_CST)
3343 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3344 else
3345 align_variable (decl, 0);
3347 /* Now construct the SYMBOL_REF and the MEM. */
3348 if (use_object_blocks_p ())
3350 int align = (TREE_CODE (decl) == CONST_DECL
3351 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3352 ? DECL_ALIGN (decl)
3353 : symtab_node::get (decl)->definition_alignment ());
3354 section *sect = get_constant_section (exp, align);
3355 symbol = create_block_symbol (ggc_strdup (label),
3356 get_block_for_section (sect), -1);
3358 else
3359 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3360 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3361 SET_SYMBOL_REF_DECL (symbol, decl);
3362 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3364 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3365 set_mem_attributes (rtl, exp, 1);
3366 set_mem_alias_set (rtl, 0);
3368 /* Putting EXP into the literal pool might have imposed a different
3369 alignment which should be visible in the RTX as well. */
3370 set_mem_align (rtl, DECL_ALIGN (decl));
3372 /* We cannot share RTX'es in pool entries.
3373 Mark this piece of RTL as required for unsharing. */
3374 RTX_FLAG (rtl, used) = 1;
3376 /* Set flags or add text to the name to record information, such as
3377 that it is a local symbol. If the name is changed, the macro
3378 ASM_OUTPUT_LABELREF will have to know how to strip this
3379 information. This call might invalidate our local variable
3380 SYMBOL; we can't use it afterward. */
3381 targetm.encode_section_info (exp, rtl, true);
3383 desc->rtl = rtl;
3385 return desc;
3388 /* Return an rtx representing a reference to constant data in memory
3389 for the constant expression EXP.
3391 If assembler code for such a constant has already been output,
3392 return an rtx to refer to it.
3393 Otherwise, output such a constant in memory
3394 and generate an rtx for it.
3396 If DEFER is nonzero, this constant can be deferred and output only
3397 if referenced in the function after all optimizations.
3399 `const_desc_table' records which constants already have label strings. */
3402 output_constant_def (tree exp, int defer)
3404 struct constant_descriptor_tree *desc;
3405 struct constant_descriptor_tree key;
3407 /* Look up EXP in the table of constant descriptors. If we didn't find
3408 it, create a new one. */
3409 key.value = exp;
3410 key.hash = const_hash_1 (exp);
3411 constant_descriptor_tree **loc
3412 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3414 desc = *loc;
3415 if (desc == 0)
3417 desc = build_constant_desc (exp);
3418 desc->hash = key.hash;
3419 *loc = desc;
3422 maybe_output_constant_def_contents (desc, defer);
3423 return desc->rtl;
3426 /* Subroutine of output_constant_def: Decide whether or not we need to
3427 output the constant DESC now, and if so, do it. */
3428 static void
3429 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3430 int defer)
3432 rtx symbol = XEXP (desc->rtl, 0);
3433 tree exp = desc->value;
3435 if (flag_syntax_only)
3436 return;
3438 if (TREE_ASM_WRITTEN (exp))
3439 /* Already output; don't do it again. */
3440 return;
3442 /* We can always defer constants as long as the context allows
3443 doing so. */
3444 if (defer)
3446 /* Increment n_deferred_constants if it exists. It needs to be at
3447 least as large as the number of constants actually referred to
3448 by the function. If it's too small we'll stop looking too early
3449 and fail to emit constants; if it's too large we'll only look
3450 through the entire function when we could have stopped earlier. */
3451 if (cfun)
3452 n_deferred_constants++;
3453 return;
3456 output_constant_def_contents (symbol);
3459 /* Subroutine of output_constant_def_contents. Output the definition
3460 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3461 constant's alignment in bits. */
3463 static void
3464 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3466 HOST_WIDE_INT size;
3468 size = get_constant_size (exp);
3470 /* Do any machine/system dependent processing of the constant. */
3471 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3473 /* Output the value of EXP. */
3474 output_constant (exp, size, align, false);
3476 targetm.asm_out.decl_end ();
3479 /* We must output the constant data referred to by SYMBOL; do so. */
3481 static void
3482 output_constant_def_contents (rtx symbol)
3484 tree decl = SYMBOL_REF_DECL (symbol);
3485 tree exp = DECL_INITIAL (decl);
3486 bool asan_protected = false;
3488 /* Make sure any other constants whose addresses appear in EXP
3489 are assigned label numbers. */
3490 output_addressed_constants (exp);
3492 /* We are no longer deferring this constant. */
3493 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3495 if ((flag_sanitize & SANITIZE_ADDRESS)
3496 && TREE_CODE (exp) == STRING_CST
3497 && asan_protect_global (exp))
3499 asan_protected = true;
3500 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3501 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3504 /* If the constant is part of an object block, make sure that the
3505 decl has been positioned within its block, but do not write out
3506 its definition yet. output_object_blocks will do that later. */
3507 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3508 place_block_symbol (symbol);
3509 else
3511 int align = (TREE_CODE (decl) == CONST_DECL
3512 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3513 ? DECL_ALIGN (decl)
3514 : symtab_node::get (decl)->definition_alignment ());
3515 switch_to_section (get_constant_section (exp, align));
3516 if (align > BITS_PER_UNIT)
3517 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3518 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3519 if (asan_protected)
3521 HOST_WIDE_INT size = get_constant_size (exp);
3522 assemble_zeros (asan_red_zone_size (size));
3527 /* Look up EXP in the table of constant descriptors. Return the rtl
3528 if it has been emitted, else null. */
3531 lookup_constant_def (tree exp)
3533 struct constant_descriptor_tree key;
3535 key.value = exp;
3536 key.hash = const_hash_1 (exp);
3537 constant_descriptor_tree *desc
3538 = const_desc_htab->find_with_hash (&key, key.hash);
3540 return (desc ? desc->rtl : NULL_RTX);
3543 /* Return a tree representing a reference to constant data in memory
3544 for the constant expression EXP.
3546 This is the counterpart of output_constant_def at the Tree level. */
3548 tree
3549 tree_output_constant_def (tree exp)
3551 struct constant_descriptor_tree *desc, key;
3552 tree decl;
3554 /* Look up EXP in the table of constant descriptors. If we didn't find
3555 it, create a new one. */
3556 key.value = exp;
3557 key.hash = const_hash_1 (exp);
3558 constant_descriptor_tree **loc
3559 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3561 desc = *loc;
3562 if (desc == 0)
3564 desc = build_constant_desc (exp);
3565 desc->hash = key.hash;
3566 *loc = desc;
3569 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3570 varpool_node::finalize_decl (decl);
3571 return decl;
3574 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3575 struct constant_descriptor_rtx *next;
3576 rtx mem;
3577 rtx sym;
3578 rtx constant;
3579 HOST_WIDE_INT offset;
3580 hashval_t hash;
3581 fixed_size_mode mode;
3582 unsigned int align;
3583 int labelno;
3584 int mark;
3587 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3589 static hashval_t hash (constant_descriptor_rtx *);
3590 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3593 /* Used in the hash tables to avoid outputting the same constant
3594 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3595 are output once per function, not once per file. */
3596 /* ??? Only a few targets need per-function constant pools. Most
3597 can use one per-file pool. Should add a targetm bit to tell the
3598 difference. */
3600 struct GTY(()) rtx_constant_pool {
3601 /* Pointers to first and last constant in pool, as ordered by offset. */
3602 struct constant_descriptor_rtx *first;
3603 struct constant_descriptor_rtx *last;
3605 /* Hash facility for making memory-constants from constant rtl-expressions.
3606 It is used on RISC machines where immediate integer arguments and
3607 constant addresses are restricted so that such constants must be stored
3608 in memory. */
3609 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3611 /* Current offset in constant pool (does not include any
3612 machine-specific header). */
3613 HOST_WIDE_INT offset;
3616 /* Hash and compare functions for const_rtx_htab. */
3618 hashval_t
3619 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3621 return desc->hash;
3624 bool
3625 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3626 constant_descriptor_rtx *y)
3628 if (x->mode != y->mode)
3629 return 0;
3630 return rtx_equal_p (x->constant, y->constant);
3633 /* Hash one component of a constant. */
3635 static hashval_t
3636 const_rtx_hash_1 (const_rtx x)
3638 unsigned HOST_WIDE_INT hwi;
3639 machine_mode mode;
3640 enum rtx_code code;
3641 hashval_t h;
3642 int i;
3644 code = GET_CODE (x);
3645 mode = GET_MODE (x);
3646 h = (hashval_t) code * 1048573 + mode;
3648 switch (code)
3650 case CONST_INT:
3651 hwi = INTVAL (x);
3653 fold_hwi:
3655 int shift = sizeof (hashval_t) * CHAR_BIT;
3656 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3658 h ^= (hashval_t) hwi;
3659 for (i = 1; i < n; ++i)
3661 hwi >>= shift;
3662 h ^= (hashval_t) hwi;
3665 break;
3667 case CONST_WIDE_INT:
3668 hwi = 0;
3670 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3671 hwi ^= CONST_WIDE_INT_ELT (x, i);
3672 goto fold_hwi;
3675 case CONST_DOUBLE:
3676 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3678 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3679 goto fold_hwi;
3681 else
3682 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3683 break;
3685 case CONST_FIXED:
3686 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3687 break;
3689 case SYMBOL_REF:
3690 h ^= htab_hash_string (XSTR (x, 0));
3691 break;
3693 case LABEL_REF:
3694 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3695 break;
3697 case UNSPEC:
3698 case UNSPEC_VOLATILE:
3699 h = h * 251 + XINT (x, 1);
3700 break;
3702 default:
3703 break;
3706 return h;
3709 /* Compute a hash value for X, which should be a constant. */
3711 static hashval_t
3712 const_rtx_hash (rtx x)
3714 hashval_t h = 0;
3715 subrtx_iterator::array_type array;
3716 FOR_EACH_SUBRTX (iter, array, x, ALL)
3717 h = h * 509 + const_rtx_hash_1 (*iter);
3718 return h;
3722 /* Create and return a new rtx constant pool. */
3724 static struct rtx_constant_pool *
3725 create_constant_pool (void)
3727 struct rtx_constant_pool *pool;
3729 pool = ggc_alloc<rtx_constant_pool> ();
3730 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3731 pool->first = NULL;
3732 pool->last = NULL;
3733 pool->offset = 0;
3734 return pool;
3737 /* Initialize constant pool hashing for a new function. */
3739 void
3740 init_varasm_status (void)
3742 crtl->varasm.pool = create_constant_pool ();
3743 crtl->varasm.deferred_constants = 0;
3746 /* Given a MINUS expression, simplify it if both sides
3747 include the same symbol. */
3750 simplify_subtraction (rtx x)
3752 rtx r = simplify_rtx (x);
3753 return r ? r : x;
3756 /* Given a constant rtx X, make (or find) a memory constant for its value
3757 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
3758 of X. */
3761 force_const_mem (machine_mode in_mode, rtx x)
3763 struct constant_descriptor_rtx *desc, tmp;
3764 struct rtx_constant_pool *pool;
3765 char label[256];
3766 rtx def, symbol;
3767 hashval_t hash;
3768 unsigned int align;
3769 constant_descriptor_rtx **slot;
3770 fixed_size_mode mode;
3772 /* We can't force variable-sized objects to memory. */
3773 if (!is_a <fixed_size_mode> (in_mode, &mode))
3774 return NULL_RTX;
3776 /* If we're not allowed to drop X into the constant pool, don't. */
3777 if (targetm.cannot_force_const_mem (mode, x))
3778 return NULL_RTX;
3780 /* Record that this function has used a constant pool entry. */
3781 crtl->uses_const_pool = 1;
3783 /* Decide which pool to use. */
3784 pool = (targetm.use_blocks_for_constant_p (mode, x)
3785 ? shared_constant_pool
3786 : crtl->varasm.pool);
3788 /* Lookup the value in the hashtable. */
3789 tmp.constant = x;
3790 tmp.mode = mode;
3791 hash = const_rtx_hash (x);
3792 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3793 desc = *slot;
3795 /* If the constant was already present, return its memory. */
3796 if (desc)
3797 return copy_rtx (desc->mem);
3799 /* Otherwise, create a new descriptor. */
3800 desc = ggc_alloc<constant_descriptor_rtx> ();
3801 *slot = desc;
3803 /* Align the location counter as required by EXP's data type. */
3804 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3805 align = targetm.static_rtx_alignment (align_mode);
3807 pool->offset += (align / BITS_PER_UNIT) - 1;
3808 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3810 desc->next = NULL;
3811 desc->constant = copy_rtx (tmp.constant);
3812 desc->offset = pool->offset;
3813 desc->hash = hash;
3814 desc->mode = mode;
3815 desc->align = align;
3816 desc->labelno = const_labelno;
3817 desc->mark = 0;
3819 pool->offset += GET_MODE_SIZE (mode);
3820 if (pool->last)
3821 pool->last->next = desc;
3822 else
3823 pool->first = pool->last = desc;
3824 pool->last = desc;
3826 /* Create a string containing the label name, in LABEL. */
3827 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3828 ++const_labelno;
3830 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3831 the constants pool. */
3832 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3834 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3835 symbol = create_block_symbol (ggc_strdup (label),
3836 get_block_for_section (sect), -1);
3838 else
3839 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3840 desc->sym = symbol;
3841 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3842 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3843 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3845 /* Construct the MEM. */
3846 desc->mem = def = gen_const_mem (mode, symbol);
3847 set_mem_align (def, align);
3849 /* If we're dropping a label to the constant pool, make sure we
3850 don't delete it. */
3851 if (GET_CODE (x) == LABEL_REF)
3852 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3854 return copy_rtx (def);
3857 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3860 get_pool_constant (const_rtx addr)
3862 return SYMBOL_REF_CONSTANT (addr)->constant;
3865 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3866 and whether it has been output or not. */
3869 get_pool_constant_mark (rtx addr, bool *pmarked)
3871 struct constant_descriptor_rtx *desc;
3873 desc = SYMBOL_REF_CONSTANT (addr);
3874 *pmarked = (desc->mark != 0);
3875 return desc->constant;
3878 /* Similar, return the mode. */
3880 fixed_size_mode
3881 get_pool_mode (const_rtx addr)
3883 return SYMBOL_REF_CONSTANT (addr)->mode;
3886 /* Return TRUE if and only if the constant pool has no entries. Note
3887 that even entries we might end up choosing not to emit are counted
3888 here, so there is the potential for missed optimizations. */
3890 bool
3891 constant_pool_empty_p (void)
3893 return crtl->varasm.pool->first == NULL;
3896 /* Worker function for output_constant_pool_1. Emit assembly for X
3897 in MODE with known alignment ALIGN. */
3899 static void
3900 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
3902 switch (GET_MODE_CLASS (mode))
3904 case MODE_FLOAT:
3905 case MODE_DECIMAL_FLOAT:
3907 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3908 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
3909 as_a <scalar_float_mode> (mode), align, false);
3910 break;
3913 case MODE_INT:
3914 case MODE_PARTIAL_INT:
3915 case MODE_FRACT:
3916 case MODE_UFRACT:
3917 case MODE_ACCUM:
3918 case MODE_UACCUM:
3919 case MODE_POINTER_BOUNDS:
3920 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3921 break;
3923 case MODE_VECTOR_BOOL:
3925 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3927 /* Pick the smallest integer mode that contains at least one
3928 whole element. Often this is byte_mode and contains more
3929 than one element. */
3930 unsigned int nelts = GET_MODE_NUNITS (mode);
3931 unsigned int elt_bits = GET_MODE_BITSIZE (mode) / nelts;
3932 unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
3933 scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
3935 /* Build the constant up one integer at a time. */
3936 unsigned int elts_per_int = int_bits / elt_bits;
3937 for (unsigned int i = 0; i < nelts; i += elts_per_int)
3939 unsigned HOST_WIDE_INT value = 0;
3940 unsigned int limit = MIN (nelts - i, elts_per_int);
3941 for (unsigned int j = 0; j < limit; ++j)
3942 if (INTVAL (CONST_VECTOR_ELT (x, i + j)) != 0)
3943 value |= 1 << (j * elt_bits);
3944 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
3945 i != 0 ? MIN (align, int_bits) : align);
3947 break;
3949 case MODE_VECTOR_FLOAT:
3950 case MODE_VECTOR_INT:
3951 case MODE_VECTOR_FRACT:
3952 case MODE_VECTOR_UFRACT:
3953 case MODE_VECTOR_ACCUM:
3954 case MODE_VECTOR_UACCUM:
3956 int i, units;
3957 scalar_mode submode = GET_MODE_INNER (mode);
3958 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3960 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3961 units = GET_MODE_NUNITS (mode);
3963 for (i = 0; i < units; i++)
3965 rtx elt = CONST_VECTOR_ELT (x, i);
3966 output_constant_pool_2 (submode, elt, i ? subalign : align);
3969 break;
3971 default:
3972 gcc_unreachable ();
3976 /* Worker function for output_constant_pool. Emit constant DESC,
3977 giving it ALIGN bits of alignment. */
3979 static void
3980 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3981 unsigned int align)
3983 rtx x, tmp;
3985 x = desc->constant;
3987 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3988 whose CODE_LABEL has been deleted. This can occur if a jump table
3989 is eliminated by optimization. If so, write a constant of zero
3990 instead. Note that this can also happen by turning the
3991 CODE_LABEL into a NOTE. */
3992 /* ??? This seems completely and utterly wrong. Certainly it's
3993 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3994 functioning even with rtx_insn::deleted and friends. */
3996 tmp = x;
3997 switch (GET_CODE (tmp))
3999 case CONST:
4000 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4001 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4002 break;
4003 tmp = XEXP (XEXP (tmp, 0), 0);
4004 /* FALLTHRU */
4006 case LABEL_REF:
4008 rtx_insn *insn = label_ref_label (tmp);
4009 gcc_assert (!insn->deleted ());
4010 gcc_assert (!NOTE_P (insn)
4011 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4012 break;
4015 default:
4016 break;
4019 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4020 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4021 align, desc->labelno, done);
4022 #endif
4024 assemble_align (align);
4026 /* Output the label. */
4027 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4029 /* Output the data.
4030 Pass actual alignment value while emitting string constant to asm code
4031 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4032 assuming that the data is already aligned which prevents the generation
4033 of fix-up table entries. */
4034 output_constant_pool_2 (desc->mode, x, desc->align);
4036 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4037 sections have proper size. */
4038 if (align > GET_MODE_BITSIZE (desc->mode)
4039 && in_section
4040 && (in_section->common.flags & SECTION_MERGE))
4041 assemble_align (align);
4043 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4044 done:
4045 #endif
4046 return;
4049 /* Recompute the offsets of entries in POOL, and the overall size of
4050 POOL. Do this after calling mark_constant_pool to ensure that we
4051 are computing the offset values for the pool which we will actually
4052 emit. */
4054 static void
4055 recompute_pool_offsets (struct rtx_constant_pool *pool)
4057 struct constant_descriptor_rtx *desc;
4058 pool->offset = 0;
4060 for (desc = pool->first; desc ; desc = desc->next)
4061 if (desc->mark)
4063 /* Recalculate offset. */
4064 unsigned int align = desc->align;
4065 pool->offset += (align / BITS_PER_UNIT) - 1;
4066 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4067 desc->offset = pool->offset;
4068 pool->offset += GET_MODE_SIZE (desc->mode);
4072 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4073 Emit referenced deferred strings. */
4075 static void
4076 mark_constants_in_pattern (rtx insn)
4078 subrtx_iterator::array_type array;
4079 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4081 const_rtx x = *iter;
4082 if (GET_CODE (x) == SYMBOL_REF)
4084 if (CONSTANT_POOL_ADDRESS_P (x))
4086 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4087 if (desc->mark == 0)
4089 desc->mark = 1;
4090 iter.substitute (desc->constant);
4093 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4095 tree decl = SYMBOL_REF_DECL (x);
4096 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4098 n_deferred_constants--;
4099 output_constant_def_contents (CONST_CAST_RTX (x));
4106 /* Look through appropriate parts of INSN, marking all entries in the
4107 constant pool which are actually being used. Entries that are only
4108 referenced by other constants are also marked as used. Emit
4109 deferred strings that are used. */
4111 static void
4112 mark_constants (rtx_insn *insn)
4114 if (!INSN_P (insn))
4115 return;
4117 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4118 insns, not any notes that may be attached. We don't want to mark
4119 a constant just because it happens to appear in a REG_EQUIV note. */
4120 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4122 int i, n = seq->len ();
4123 for (i = 0; i < n; ++i)
4125 rtx subinsn = seq->element (i);
4126 if (INSN_P (subinsn))
4127 mark_constants_in_pattern (subinsn);
4130 else
4131 mark_constants_in_pattern (insn);
4134 /* Look through the instructions for this function, and mark all the
4135 entries in POOL which are actually being used. Emit deferred constants
4136 which have indeed been used. */
4138 static void
4139 mark_constant_pool (void)
4141 rtx_insn *insn;
4143 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4144 return;
4146 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4147 mark_constants (insn);
4150 /* Write all the constants in POOL. */
4152 static void
4153 output_constant_pool_contents (struct rtx_constant_pool *pool)
4155 struct constant_descriptor_rtx *desc;
4157 for (desc = pool->first; desc ; desc = desc->next)
4158 if (desc->mark)
4160 /* If the constant is part of an object_block, make sure that
4161 the constant has been positioned within its block, but do not
4162 write out its definition yet. output_object_blocks will do
4163 that later. */
4164 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4165 && SYMBOL_REF_BLOCK (desc->sym))
4166 place_block_symbol (desc->sym);
4167 else
4169 switch_to_section (targetm.asm_out.select_rtx_section
4170 (desc->mode, desc->constant, desc->align));
4171 output_constant_pool_1 (desc, desc->align);
4176 /* Mark all constants that are used in the current function, then write
4177 out the function's private constant pool. */
4179 static void
4180 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4181 tree fndecl ATTRIBUTE_UNUSED)
4183 struct rtx_constant_pool *pool = crtl->varasm.pool;
4185 /* It is possible for gcc to call force_const_mem and then to later
4186 discard the instructions which refer to the constant. In such a
4187 case we do not need to output the constant. */
4188 mark_constant_pool ();
4190 /* Having marked the constant pool entries we'll actually emit, we
4191 now need to rebuild the offset information, which may have become
4192 stale. */
4193 recompute_pool_offsets (pool);
4195 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4196 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4197 #endif
4199 output_constant_pool_contents (pool);
4201 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4202 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4203 #endif
4206 /* Write the contents of the shared constant pool. */
4208 void
4209 output_shared_constant_pool (void)
4211 output_constant_pool_contents (shared_constant_pool);
4214 /* Determine what kind of relocations EXP may need. */
4217 compute_reloc_for_constant (tree exp)
4219 int reloc = 0, reloc2;
4220 tree tem;
4222 switch (TREE_CODE (exp))
4224 case ADDR_EXPR:
4225 case FDESC_EXPR:
4226 /* Go inside any operations that get_inner_reference can handle and see
4227 if what's inside is a constant: no need to do anything here for
4228 addresses of variables or functions. */
4229 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4230 tem = TREE_OPERAND (tem, 0))
4233 if (TREE_CODE (tem) == MEM_REF
4234 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4236 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4237 break;
4240 if (!targetm.binds_local_p (tem))
4241 reloc |= 2;
4242 else
4243 reloc |= 1;
4244 break;
4246 case PLUS_EXPR:
4247 case POINTER_PLUS_EXPR:
4248 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4249 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4250 break;
4252 case MINUS_EXPR:
4253 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4254 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4255 /* The difference of two local labels is computable at link time. */
4256 if (reloc == 1 && reloc2 == 1)
4257 reloc = 0;
4258 else
4259 reloc |= reloc2;
4260 break;
4262 CASE_CONVERT:
4263 case VIEW_CONVERT_EXPR:
4264 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4265 break;
4267 case CONSTRUCTOR:
4269 unsigned HOST_WIDE_INT idx;
4270 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4271 if (tem != 0)
4272 reloc |= compute_reloc_for_constant (tem);
4274 break;
4276 default:
4277 break;
4279 return reloc;
4282 /* Find all the constants whose addresses are referenced inside of EXP,
4283 and make sure assembler code with a label has been output for each one.
4284 Indicate whether an ADDR_EXPR has been encountered. */
4286 static void
4287 output_addressed_constants (tree exp)
4289 tree tem;
4291 switch (TREE_CODE (exp))
4293 case ADDR_EXPR:
4294 case FDESC_EXPR:
4295 /* Go inside any operations that get_inner_reference can handle and see
4296 if what's inside is a constant: no need to do anything here for
4297 addresses of variables or functions. */
4298 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4299 tem = TREE_OPERAND (tem, 0))
4302 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4303 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4304 tem = DECL_INITIAL (tem);
4306 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4307 output_constant_def (tem, 0);
4309 if (TREE_CODE (tem) == MEM_REF)
4310 output_addressed_constants (TREE_OPERAND (tem, 0));
4311 break;
4313 case PLUS_EXPR:
4314 case POINTER_PLUS_EXPR:
4315 case MINUS_EXPR:
4316 output_addressed_constants (TREE_OPERAND (exp, 1));
4317 gcc_fallthrough ();
4319 CASE_CONVERT:
4320 case VIEW_CONVERT_EXPR:
4321 output_addressed_constants (TREE_OPERAND (exp, 0));
4322 break;
4324 case CONSTRUCTOR:
4326 unsigned HOST_WIDE_INT idx;
4327 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4328 if (tem != 0)
4329 output_addressed_constants (tem);
4331 break;
4333 default:
4334 break;
4338 /* Whether a constructor CTOR is a valid static constant initializer if all
4339 its elements are. This used to be internal to initializer_constant_valid_p
4340 and has been exposed to let other functions like categorize_ctor_elements
4341 evaluate the property while walking a constructor for other purposes. */
4343 bool
4344 constructor_static_from_elts_p (const_tree ctor)
4346 return (TREE_CONSTANT (ctor)
4347 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4348 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4349 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4352 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4353 tree *cache);
4355 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4356 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4357 which are valid when ENDTYPE is an integer of any size; in
4358 particular, this does not accept a pointer minus a constant. This
4359 returns null_pointer_node if the VALUE is an absolute constant
4360 which can be used to initialize a static variable. Otherwise it
4361 returns NULL. */
4363 static tree
4364 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4366 tree op0, op1;
4368 if (!INTEGRAL_TYPE_P (endtype))
4369 return NULL_TREE;
4371 op0 = TREE_OPERAND (value, 0);
4372 op1 = TREE_OPERAND (value, 1);
4374 /* Like STRIP_NOPS except allow the operand mode to widen. This
4375 works around a feature of fold that simplifies (int)(p1 - p2) to
4376 ((int)p1 - (int)p2) under the theory that the narrower operation
4377 is cheaper. */
4379 while (CONVERT_EXPR_P (op0)
4380 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4382 tree inner = TREE_OPERAND (op0, 0);
4383 if (inner == error_mark_node
4384 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4385 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4386 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4387 break;
4388 op0 = inner;
4391 while (CONVERT_EXPR_P (op1)
4392 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4394 tree inner = TREE_OPERAND (op1, 0);
4395 if (inner == error_mark_node
4396 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4397 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4398 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4399 break;
4400 op1 = inner;
4403 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4404 if (!op0)
4405 return NULL_TREE;
4407 op1 = initializer_constant_valid_p_1 (op1, endtype,
4408 cache ? cache + 2 : NULL);
4409 /* Both initializers must be known. */
4410 if (op1)
4412 if (op0 == op1
4413 && (op0 == null_pointer_node
4414 || TREE_CODE (value) == MINUS_EXPR))
4415 return null_pointer_node;
4417 /* Support differences between labels. */
4418 if (TREE_CODE (op0) == LABEL_DECL
4419 && TREE_CODE (op1) == LABEL_DECL)
4420 return null_pointer_node;
4422 if (TREE_CODE (op0) == STRING_CST
4423 && TREE_CODE (op1) == STRING_CST
4424 && operand_equal_p (op0, op1, 1))
4425 return null_pointer_node;
4428 return NULL_TREE;
4431 /* Helper function of initializer_constant_valid_p.
4432 Return nonzero if VALUE is a valid constant-valued expression
4433 for use in initializing a static variable; one that can be an
4434 element of a "constant" initializer.
4436 Return null_pointer_node if the value is absolute;
4437 if it is relocatable, return the variable that determines the relocation.
4438 We assume that VALUE has been folded as much as possible;
4439 therefore, we do not need to check for such things as
4440 arithmetic-combinations of integers.
4442 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4444 static tree
4445 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4447 tree ret;
4449 switch (TREE_CODE (value))
4451 case CONSTRUCTOR:
4452 if (constructor_static_from_elts_p (value))
4454 unsigned HOST_WIDE_INT idx;
4455 tree elt;
4456 bool absolute = true;
4458 if (cache && cache[0] == value)
4459 return cache[1];
4460 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4462 tree reloc;
4463 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4464 NULL);
4465 if (!reloc
4466 /* An absolute value is required with reverse SSO. */
4467 || (reloc != null_pointer_node
4468 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4469 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4471 if (cache)
4473 cache[0] = value;
4474 cache[1] = NULL_TREE;
4476 return NULL_TREE;
4478 if (reloc != null_pointer_node)
4479 absolute = false;
4481 /* For a non-absolute relocation, there is no single
4482 variable that can be "the variable that determines the
4483 relocation." */
4484 if (cache)
4486 cache[0] = value;
4487 cache[1] = absolute ? null_pointer_node : error_mark_node;
4489 return absolute ? null_pointer_node : error_mark_node;
4492 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4494 case INTEGER_CST:
4495 case VECTOR_CST:
4496 case REAL_CST:
4497 case FIXED_CST:
4498 case STRING_CST:
4499 case COMPLEX_CST:
4500 return null_pointer_node;
4502 case ADDR_EXPR:
4503 case FDESC_EXPR:
4505 tree op0 = staticp (TREE_OPERAND (value, 0));
4506 if (op0)
4508 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4509 to be a constant, this is old-skool offsetof-like nonsense. */
4510 if (TREE_CODE (op0) == INDIRECT_REF
4511 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4512 return null_pointer_node;
4513 /* Taking the address of a nested function involves a trampoline,
4514 unless we don't need or want one. */
4515 if (TREE_CODE (op0) == FUNCTION_DECL
4516 && DECL_STATIC_CHAIN (op0)
4517 && !TREE_NO_TRAMPOLINE (value))
4518 return NULL_TREE;
4519 /* "&{...}" requires a temporary to hold the constructed
4520 object. */
4521 if (TREE_CODE (op0) == CONSTRUCTOR)
4522 return NULL_TREE;
4524 return op0;
4527 case NON_LVALUE_EXPR:
4528 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4529 endtype, cache);
4531 case VIEW_CONVERT_EXPR:
4533 tree src = TREE_OPERAND (value, 0);
4534 tree src_type = TREE_TYPE (src);
4535 tree dest_type = TREE_TYPE (value);
4537 /* Allow view-conversions from aggregate to non-aggregate type only
4538 if the bit pattern is fully preserved afterwards; otherwise, the
4539 RTL expander won't be able to apply a subsequent transformation
4540 to the underlying constructor. */
4541 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4543 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4544 return initializer_constant_valid_p_1 (src, endtype, cache);
4545 else
4546 return NULL_TREE;
4549 /* Allow all other kinds of view-conversion. */
4550 return initializer_constant_valid_p_1 (src, endtype, cache);
4553 CASE_CONVERT:
4555 tree src = TREE_OPERAND (value, 0);
4556 tree src_type = TREE_TYPE (src);
4557 tree dest_type = TREE_TYPE (value);
4559 /* Allow conversions between pointer types, floating-point
4560 types, and offset types. */
4561 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4562 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4563 || (TREE_CODE (dest_type) == OFFSET_TYPE
4564 && TREE_CODE (src_type) == OFFSET_TYPE))
4565 return initializer_constant_valid_p_1 (src, endtype, cache);
4567 /* Allow length-preserving conversions between integer types. */
4568 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4569 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4570 return initializer_constant_valid_p_1 (src, endtype, cache);
4572 /* Allow conversions between other integer types only if
4573 explicit value. Don't allow sign-extension to a type larger
4574 than word and pointer, there aren't relocations that would
4575 allow to sign extend it to a wider type. */
4576 if (INTEGRAL_TYPE_P (dest_type)
4577 && INTEGRAL_TYPE_P (src_type)
4578 && (TYPE_UNSIGNED (src_type)
4579 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4580 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4581 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4583 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4584 if (inner == null_pointer_node)
4585 return null_pointer_node;
4586 break;
4589 /* Allow (int) &foo provided int is as wide as a pointer. */
4590 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4591 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4592 return initializer_constant_valid_p_1 (src, endtype, cache);
4594 /* Likewise conversions from int to pointers, but also allow
4595 conversions from 0. */
4596 if ((POINTER_TYPE_P (dest_type)
4597 || TREE_CODE (dest_type) == OFFSET_TYPE)
4598 && INTEGRAL_TYPE_P (src_type))
4600 if (TREE_CODE (src) == INTEGER_CST
4601 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4602 return null_pointer_node;
4603 if (integer_zerop (src))
4604 return null_pointer_node;
4605 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4606 return initializer_constant_valid_p_1 (src, endtype, cache);
4609 /* Allow conversions to struct or union types if the value
4610 inside is okay. */
4611 if (TREE_CODE (dest_type) == RECORD_TYPE
4612 || TREE_CODE (dest_type) == UNION_TYPE)
4613 return initializer_constant_valid_p_1 (src, endtype, cache);
4615 break;
4617 case POINTER_PLUS_EXPR:
4618 case PLUS_EXPR:
4619 /* Any valid floating-point constants will have been folded by now;
4620 with -frounding-math we hit this with addition of two constants. */
4621 if (TREE_CODE (endtype) == REAL_TYPE)
4622 return NULL_TREE;
4623 if (cache && cache[0] == value)
4624 return cache[1];
4625 if (! INTEGRAL_TYPE_P (endtype)
4626 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4628 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4629 tree valid0
4630 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4631 endtype, ncache);
4632 tree valid1
4633 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4634 endtype, ncache + 2);
4635 /* If either term is absolute, use the other term's relocation. */
4636 if (valid0 == null_pointer_node)
4637 ret = valid1;
4638 else if (valid1 == null_pointer_node)
4639 ret = valid0;
4640 /* Support narrowing pointer differences. */
4641 else
4642 ret = narrowing_initializer_constant_valid_p (value, endtype,
4643 ncache);
4645 else
4646 /* Support narrowing pointer differences. */
4647 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4648 if (cache)
4650 cache[0] = value;
4651 cache[1] = ret;
4653 return ret;
4655 case POINTER_DIFF_EXPR:
4656 case MINUS_EXPR:
4657 if (TREE_CODE (endtype) == REAL_TYPE)
4658 return NULL_TREE;
4659 if (cache && cache[0] == value)
4660 return cache[1];
4661 if (! INTEGRAL_TYPE_P (endtype)
4662 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4664 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4665 tree valid0
4666 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4667 endtype, ncache);
4668 tree valid1
4669 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4670 endtype, ncache + 2);
4671 /* Win if second argument is absolute. */
4672 if (valid1 == null_pointer_node)
4673 ret = valid0;
4674 /* Win if both arguments have the same relocation.
4675 Then the value is absolute. */
4676 else if (valid0 == valid1 && valid0 != 0)
4677 ret = null_pointer_node;
4678 /* Since GCC guarantees that string constants are unique in the
4679 generated code, a subtraction between two copies of the same
4680 constant string is absolute. */
4681 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4682 && valid1 && TREE_CODE (valid1) == STRING_CST
4683 && operand_equal_p (valid0, valid1, 1))
4684 ret = null_pointer_node;
4685 /* Support narrowing differences. */
4686 else
4687 ret = narrowing_initializer_constant_valid_p (value, endtype,
4688 ncache);
4690 else
4691 /* Support narrowing differences. */
4692 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4693 if (cache)
4695 cache[0] = value;
4696 cache[1] = ret;
4698 return ret;
4700 default:
4701 break;
4704 return NULL_TREE;
4707 /* Return nonzero if VALUE is a valid constant-valued expression
4708 for use in initializing a static variable; one that can be an
4709 element of a "constant" initializer.
4711 Return null_pointer_node if the value is absolute;
4712 if it is relocatable, return the variable that determines the relocation.
4713 We assume that VALUE has been folded as much as possible;
4714 therefore, we do not need to check for such things as
4715 arithmetic-combinations of integers. */
4716 tree
4717 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
4719 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
4721 /* An absolute value is required with reverse storage order. */
4722 if (reloc
4723 && reloc != null_pointer_node
4724 && reverse
4725 && !AGGREGATE_TYPE_P (endtype)
4726 && !VECTOR_TYPE_P (endtype))
4727 reloc = NULL_TREE;
4729 return reloc;
4732 /* Return true if VALUE is a valid constant-valued expression
4733 for use in initializing a static bit-field; one that can be
4734 an element of a "constant" initializer. */
4736 bool
4737 initializer_constant_valid_for_bitfield_p (tree value)
4739 /* For bitfields we support integer constants or possibly nested aggregates
4740 of such. */
4741 switch (TREE_CODE (value))
4743 case CONSTRUCTOR:
4745 unsigned HOST_WIDE_INT idx;
4746 tree elt;
4748 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4749 if (!initializer_constant_valid_for_bitfield_p (elt))
4750 return false;
4751 return true;
4754 case INTEGER_CST:
4755 case REAL_CST:
4756 return true;
4758 case VIEW_CONVERT_EXPR:
4759 case NON_LVALUE_EXPR:
4760 return
4761 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4763 default:
4764 break;
4767 return false;
4770 /* output_constructor outer state of relevance in recursive calls, typically
4771 for nested aggregate bitfields. */
4773 struct oc_outer_state {
4774 unsigned int bit_offset; /* current position in ... */
4775 int byte; /* ... the outer byte buffer. */
4778 static unsigned HOST_WIDE_INT
4779 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
4780 oc_outer_state *);
4782 /* Output assembler code for constant EXP, with no label.
4783 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4784 Assumes output_addressed_constants has been done on EXP already.
4786 Generate at least SIZE bytes of assembler data, padding at the end
4787 with zeros if necessary. SIZE must always be specified. The returned
4788 value is the actual number of bytes of assembler data generated, which
4789 may be bigger than SIZE if the object contains a variable length field.
4791 SIZE is important for structure constructors,
4792 since trailing members may have been omitted from the constructor.
4793 It is also important for initialization of arrays from string constants
4794 since the full length of the string constant might not be wanted.
4795 It is also needed for initialization of unions, where the initializer's
4796 type is just one member, and that may not be as long as the union.
4798 There a case in which we would fail to output exactly SIZE bytes:
4799 for a structure constructor that wants to produce more than SIZE bytes.
4800 But such constructors will never be generated for any possible input.
4802 ALIGN is the alignment of the data in bits.
4804 If REVERSE is true, EXP is output in reverse storage order. */
4806 static unsigned HOST_WIDE_INT
4807 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
4808 bool reverse)
4810 enum tree_code code;
4811 unsigned HOST_WIDE_INT thissize;
4812 rtx cst;
4814 if (size == 0 || flag_syntax_only)
4815 return size;
4817 /* See if we're trying to initialize a pointer in a non-default mode
4818 to the address of some declaration somewhere. If the target says
4819 the mode is valid for pointers, assume the target has a way of
4820 resolving it. */
4821 if (TREE_CODE (exp) == NOP_EXPR
4822 && POINTER_TYPE_P (TREE_TYPE (exp))
4823 && targetm.addr_space.valid_pointer_mode
4824 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4825 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4827 tree saved_type = TREE_TYPE (exp);
4829 /* Peel off any intermediate conversions-to-pointer for valid
4830 pointer modes. */
4831 while (TREE_CODE (exp) == NOP_EXPR
4832 && POINTER_TYPE_P (TREE_TYPE (exp))
4833 && targetm.addr_space.valid_pointer_mode
4834 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4835 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4836 exp = TREE_OPERAND (exp, 0);
4838 /* If what we're left with is the address of something, we can
4839 convert the address to the final type and output it that
4840 way. */
4841 if (TREE_CODE (exp) == ADDR_EXPR)
4842 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4843 /* Likewise for constant ints. */
4844 else if (TREE_CODE (exp) == INTEGER_CST)
4845 exp = fold_convert (saved_type, exp);
4849 /* Eliminate any conversions since we'll be outputting the underlying
4850 constant. */
4851 while (CONVERT_EXPR_P (exp)
4852 || TREE_CODE (exp) == NON_LVALUE_EXPR
4853 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4855 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4856 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4858 /* Make sure eliminating the conversion is really a no-op, except with
4859 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4860 union types to allow for Ada unchecked unions. */
4861 if (type_size > op_size
4862 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4863 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4864 /* Keep the conversion. */
4865 break;
4866 else
4867 exp = TREE_OPERAND (exp, 0);
4870 code = TREE_CODE (TREE_TYPE (exp));
4871 thissize = int_size_in_bytes (TREE_TYPE (exp));
4873 /* Allow a constructor with no elements for any data type.
4874 This means to fill the space with zeros. */
4875 if (TREE_CODE (exp) == CONSTRUCTOR
4876 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4878 assemble_zeros (size);
4879 return size;
4882 if (TREE_CODE (exp) == FDESC_EXPR)
4884 #ifdef ASM_OUTPUT_FDESC
4885 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4886 tree decl = TREE_OPERAND (exp, 0);
4887 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4888 #else
4889 gcc_unreachable ();
4890 #endif
4891 return size;
4894 /* Now output the underlying data. If we've handling the padding, return.
4895 Otherwise, break and ensure SIZE is the size written. */
4896 switch (code)
4898 case BOOLEAN_TYPE:
4899 case INTEGER_TYPE:
4900 case ENUMERAL_TYPE:
4901 case POINTER_TYPE:
4902 case REFERENCE_TYPE:
4903 case OFFSET_TYPE:
4904 case FIXED_POINT_TYPE:
4905 case POINTER_BOUNDS_TYPE:
4906 case NULLPTR_TYPE:
4907 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
4908 if (reverse)
4909 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
4910 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
4911 error ("initializer for integer/fixed-point value is too complicated");
4912 break;
4914 case REAL_TYPE:
4915 if (TREE_CODE (exp) != REAL_CST)
4916 error ("initializer for floating value is not a floating constant");
4917 else
4918 assemble_real (TREE_REAL_CST (exp),
4919 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
4920 align, reverse);
4921 break;
4923 case COMPLEX_TYPE:
4924 output_constant (TREE_REALPART (exp), thissize / 2, align, reverse);
4925 output_constant (TREE_IMAGPART (exp), thissize / 2,
4926 min_align (align, BITS_PER_UNIT * (thissize / 2)),
4927 reverse);
4928 break;
4930 case ARRAY_TYPE:
4931 case VECTOR_TYPE:
4932 switch (TREE_CODE (exp))
4934 case CONSTRUCTOR:
4935 return output_constructor (exp, size, align, reverse, NULL);
4936 case STRING_CST:
4937 thissize
4938 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4939 assemble_string (TREE_STRING_POINTER (exp), thissize);
4940 break;
4941 case VECTOR_CST:
4943 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4944 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4945 int elt_size = GET_MODE_SIZE (inner);
4946 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
4947 reverse);
4948 thissize = elt_size;
4949 /* Static constants must have a fixed size. */
4950 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
4951 for (unsigned int i = 1; i < nunits; i++)
4953 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
4954 reverse);
4955 thissize += elt_size;
4957 break;
4959 default:
4960 gcc_unreachable ();
4962 break;
4964 case RECORD_TYPE:
4965 case UNION_TYPE:
4966 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4967 return output_constructor (exp, size, align, reverse, NULL);
4969 case ERROR_MARK:
4970 return 0;
4972 default:
4973 gcc_unreachable ();
4976 if (size > thissize)
4977 assemble_zeros (size - thissize);
4979 return size;
4982 /* Subroutine of output_constructor, used for computing the size of
4983 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4984 type with an unspecified upper bound. */
4986 static unsigned HOST_WIDE_INT
4987 array_size_for_constructor (tree val)
4989 tree max_index;
4990 unsigned HOST_WIDE_INT cnt;
4991 tree index, value, tmp;
4992 offset_int i;
4994 /* This code used to attempt to handle string constants that are not
4995 arrays of single-bytes, but nothing else does, so there's no point in
4996 doing it here. */
4997 if (TREE_CODE (val) == STRING_CST)
4998 return TREE_STRING_LENGTH (val);
5000 max_index = NULL_TREE;
5001 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5003 if (TREE_CODE (index) == RANGE_EXPR)
5004 index = TREE_OPERAND (index, 1);
5005 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5006 max_index = index;
5009 if (max_index == NULL_TREE)
5010 return 0;
5012 /* Compute the total number of array elements. */
5013 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5014 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5016 /* Multiply by the array element unit size to find number of bytes. */
5017 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5019 gcc_assert (wi::fits_uhwi_p (i));
5020 return i.to_uhwi ();
5023 /* Other datastructures + helpers for output_constructor. */
5025 /* output_constructor local state to support interaction with helpers. */
5027 struct oc_local_state {
5029 /* Received arguments. */
5030 tree exp; /* Constructor expression. */
5031 tree type; /* Type of constructor expression. */
5032 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5033 unsigned int align; /* Known initial alignment. */
5034 tree min_index; /* Lower bound if specified for an array. */
5036 /* Output processing state. */
5037 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5038 int byte; /* Part of a bitfield byte yet to be output. */
5039 int last_relative_index; /* Implicit or explicit index of the last
5040 array element output within a bitfield. */
5041 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5042 bool reverse; /* Whether reverse storage order is in use. */
5044 /* Current element. */
5045 tree field; /* Current field decl in a record. */
5046 tree val; /* Current element value. */
5047 tree index; /* Current element index. */
5051 /* Helper for output_constructor. From the current LOCAL state, output a
5052 RANGE_EXPR element. */
5054 static void
5055 output_constructor_array_range (oc_local_state *local)
5057 unsigned HOST_WIDE_INT fieldsize
5058 = int_size_in_bytes (TREE_TYPE (local->type));
5060 HOST_WIDE_INT lo_index
5061 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5062 HOST_WIDE_INT hi_index
5063 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5064 HOST_WIDE_INT index;
5066 unsigned int align2
5067 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5069 for (index = lo_index; index <= hi_index; index++)
5071 /* Output the element's initial value. */
5072 if (local->val == NULL_TREE)
5073 assemble_zeros (fieldsize);
5074 else
5075 fieldsize
5076 = output_constant (local->val, fieldsize, align2, local->reverse);
5078 /* Count its size. */
5079 local->total_bytes += fieldsize;
5083 /* Helper for output_constructor. From the current LOCAL state, output a
5084 field element that is not true bitfield or part of an outer one. */
5086 static void
5087 output_constructor_regular_field (oc_local_state *local)
5089 /* Field size and position. Since this structure is static, we know the
5090 positions are constant. */
5091 unsigned HOST_WIDE_INT fieldsize;
5092 HOST_WIDE_INT fieldpos;
5094 unsigned int align2;
5096 /* Output any buffered-up bit-fields preceding this element. */
5097 if (local->byte_buffer_in_use)
5099 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5100 local->total_bytes++;
5101 local->byte_buffer_in_use = false;
5104 if (local->index != NULL_TREE)
5106 /* Perform the index calculation in modulo arithmetic but
5107 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5108 but we are using an unsigned sizetype. */
5109 unsigned prec = TYPE_PRECISION (sizetype);
5110 offset_int idx = wi::sext (wi::to_offset (local->index)
5111 - wi::to_offset (local->min_index), prec);
5112 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5113 .to_short_addr ();
5115 else if (local->field != NULL_TREE)
5116 fieldpos = int_byte_position (local->field);
5117 else
5118 fieldpos = 0;
5120 /* Advance to offset of this element.
5121 Note no alignment needed in an array, since that is guaranteed
5122 if each element has the proper size. */
5123 if (local->field != NULL_TREE || local->index != NULL_TREE)
5125 if (fieldpos > local->total_bytes)
5127 assemble_zeros (fieldpos - local->total_bytes);
5128 local->total_bytes = fieldpos;
5130 else
5131 /* Must not go backwards. */
5132 gcc_assert (fieldpos == local->total_bytes);
5135 /* Find the alignment of this element. */
5136 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5138 /* Determine size this element should occupy. */
5139 if (local->field)
5141 fieldsize = 0;
5143 /* If this is an array with an unspecified upper bound,
5144 the initializer determines the size. */
5145 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5146 but we cannot do this until the deprecated support for
5147 initializing zero-length array members is removed. */
5148 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5149 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5150 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5152 fieldsize = array_size_for_constructor (local->val);
5153 /* Given a non-empty initialization, this field had better
5154 be last. Given a flexible array member, the next field
5155 on the chain is a TYPE_DECL of the enclosing struct. */
5156 const_tree next = DECL_CHAIN (local->field);
5157 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5159 else
5160 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5162 else
5163 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5165 /* Output the element's initial value. */
5166 if (local->val == NULL_TREE)
5167 assemble_zeros (fieldsize);
5168 else
5169 fieldsize
5170 = output_constant (local->val, fieldsize, align2, local->reverse);
5172 /* Count its size. */
5173 local->total_bytes += fieldsize;
5176 /* Helper for output_constructor. From the LOCAL state, output an element
5177 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5178 from the start of a possibly ongoing outer byte buffer. */
5180 static void
5181 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5183 /* Bit size of this element. */
5184 HOST_WIDE_INT ebitsize
5185 = (local->field
5186 ? tree_to_uhwi (DECL_SIZE (local->field))
5187 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5189 /* Relative index of this element if this is an array component. */
5190 HOST_WIDE_INT relative_index
5191 = (!local->field
5192 ? (local->index
5193 ? (tree_to_shwi (local->index)
5194 - tree_to_shwi (local->min_index))
5195 : local->last_relative_index + 1)
5196 : 0);
5198 /* Bit position of this element from the start of the containing
5199 constructor. */
5200 HOST_WIDE_INT constructor_relative_ebitpos
5201 = (local->field
5202 ? int_bit_position (local->field)
5203 : ebitsize * relative_index);
5205 /* Bit position of this element from the start of a possibly ongoing
5206 outer byte buffer. */
5207 HOST_WIDE_INT byte_relative_ebitpos
5208 = bit_offset + constructor_relative_ebitpos;
5210 /* From the start of a possibly ongoing outer byte buffer, offsets to
5211 the first bit of this element and to the first bit past the end of
5212 this element. */
5213 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5214 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5216 local->last_relative_index = relative_index;
5218 if (local->val == NULL_TREE)
5219 local->val = integer_zero_node;
5221 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5222 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5223 local->val = TREE_OPERAND (local->val, 0);
5225 if (TREE_CODE (local->val) != INTEGER_CST
5226 && TREE_CODE (local->val) != CONSTRUCTOR)
5228 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5229 return;
5232 /* If this field does not start in this (or next) byte, skip some bytes. */
5233 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5235 /* Output remnant of any bit field in previous bytes. */
5236 if (local->byte_buffer_in_use)
5238 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5239 local->total_bytes++;
5240 local->byte_buffer_in_use = false;
5243 /* If still not at proper byte, advance to there. */
5244 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5246 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5247 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5248 local->total_bytes = next_offset / BITS_PER_UNIT;
5252 /* Set up the buffer if necessary. */
5253 if (!local->byte_buffer_in_use)
5255 local->byte = 0;
5256 if (ebitsize > 0)
5257 local->byte_buffer_in_use = true;
5260 /* If this is nested constructor, recurse passing the bit offset and the
5261 pending data, then retrieve the new pending data afterwards. */
5262 if (TREE_CODE (local->val) == CONSTRUCTOR)
5264 oc_outer_state temp_state;
5265 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5266 temp_state.byte = local->byte;
5267 local->total_bytes
5268 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5269 local->byte = temp_state.byte;
5270 return;
5273 /* Otherwise, we must split the element into pieces that fall within
5274 separate bytes, and combine each byte with previous or following
5275 bit-fields. */
5276 while (next_offset < end_offset)
5278 int this_time;
5279 int shift;
5280 HOST_WIDE_INT value;
5281 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5282 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5284 /* Advance from byte to byte within this element when necessary. */
5285 while (next_byte != local->total_bytes)
5287 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5288 local->total_bytes++;
5289 local->byte = 0;
5292 /* Number of bits we can process at once (all part of the same byte). */
5293 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5294 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5296 /* For big-endian data, take the most significant bits (of the
5297 bits that are significant) first and put them into bytes from
5298 the most significant end. */
5299 shift = end_offset - next_offset - this_time;
5301 /* Don't try to take a bunch of bits that cross
5302 the word boundary in the INTEGER_CST. We can
5303 only select bits from one element. */
5304 if ((shift / HOST_BITS_PER_WIDE_INT)
5305 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5307 const int end = shift + this_time - 1;
5308 shift = end & -HOST_BITS_PER_WIDE_INT;
5309 this_time = end - shift + 1;
5312 /* Now get the bits from the appropriate constant word. */
5313 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5314 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5316 /* Get the result. This works only when:
5317 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5318 local->byte |= (((value >> shift)
5319 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5320 << (BITS_PER_UNIT - this_time - next_bit));
5322 else
5324 /* On little-endian machines, take the least significant bits of
5325 the value first and pack them starting at the least significant
5326 bits of the bytes. */
5327 shift = next_offset - byte_relative_ebitpos;
5329 /* Don't try to take a bunch of bits that cross
5330 the word boundary in the INTEGER_CST. We can
5331 only select bits from one element. */
5332 if ((shift / HOST_BITS_PER_WIDE_INT)
5333 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5334 this_time
5335 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5337 /* Now get the bits from the appropriate constant word. */
5338 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5339 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5341 /* Get the result. This works only when:
5342 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5343 local->byte |= (((value >> shift)
5344 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5345 << next_bit);
5348 next_offset += this_time;
5349 local->byte_buffer_in_use = true;
5353 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5354 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5355 caller output state of relevance in recursive invocations. */
5357 static unsigned HOST_WIDE_INT
5358 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5359 bool reverse, oc_outer_state *outer)
5361 unsigned HOST_WIDE_INT cnt;
5362 constructor_elt *ce;
5363 oc_local_state local;
5365 /* Setup our local state to communicate with helpers. */
5366 local.exp = exp;
5367 local.type = TREE_TYPE (exp);
5368 local.size = size;
5369 local.align = align;
5370 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5371 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5372 else
5373 local.min_index = integer_zero_node;
5375 local.total_bytes = 0;
5376 local.byte_buffer_in_use = outer != NULL;
5377 local.byte = outer ? outer->byte : 0;
5378 local.last_relative_index = -1;
5379 /* The storage order is specified for every aggregate type. */
5380 if (AGGREGATE_TYPE_P (local.type))
5381 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5382 else
5383 local.reverse = reverse;
5385 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5387 /* As CE goes through the elements of the constant, FIELD goes through the
5388 structure fields if the constant is a structure. If the constant is a
5389 union, we override this by getting the field from the TREE_LIST element.
5390 But the constant could also be an array. Then FIELD is zero.
5392 There is always a maximum of one element in the chain LINK for unions
5393 (even if the initializer in a source program incorrectly contains
5394 more one). */
5396 if (TREE_CODE (local.type) == RECORD_TYPE)
5397 local.field = TYPE_FIELDS (local.type);
5398 else
5399 local.field = NULL_TREE;
5401 for (cnt = 0;
5402 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5403 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5405 local.val = ce->value;
5406 local.index = NULL_TREE;
5408 /* The element in a union constructor specifies the proper field
5409 or index. */
5410 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5411 local.field = ce->index;
5413 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5414 local.index = ce->index;
5416 if (local.field && flag_verbose_asm)
5417 fprintf (asm_out_file, "%s %s:\n",
5418 ASM_COMMENT_START,
5419 DECL_NAME (local.field)
5420 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5421 : "<anonymous>");
5423 /* Eliminate the marker that makes a cast not be an lvalue. */
5424 if (local.val != NULL_TREE)
5425 STRIP_NOPS (local.val);
5427 /* Output the current element, using the appropriate helper ... */
5429 /* For an array slice not part of an outer bitfield. */
5430 if (!outer
5431 && local.index != NULL_TREE
5432 && TREE_CODE (local.index) == RANGE_EXPR)
5433 output_constructor_array_range (&local);
5435 /* For a field that is neither a true bitfield nor part of an outer one,
5436 known to be at least byte aligned and multiple-of-bytes long. */
5437 else if (!outer
5438 && (local.field == NULL_TREE
5439 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5440 output_constructor_regular_field (&local);
5442 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5443 supported for scalar fields, so we may need to convert first. */
5444 else
5446 if (TREE_CODE (local.val) == REAL_CST)
5447 local.val
5448 = fold_unary (VIEW_CONVERT_EXPR,
5449 build_nonstandard_integer_type
5450 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5451 local.val);
5452 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5456 /* If we are not at toplevel, save the pending data for our caller.
5457 Otherwise output the pending data and padding zeros as needed. */
5458 if (outer)
5459 outer->byte = local.byte;
5460 else
5462 if (local.byte_buffer_in_use)
5464 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5465 local.total_bytes++;
5468 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5470 assemble_zeros (local.size - local.total_bytes);
5471 local.total_bytes = local.size;
5475 return local.total_bytes;
5478 /* Mark DECL as weak. */
5480 static void
5481 mark_weak (tree decl)
5483 if (DECL_WEAK (decl))
5484 return;
5486 struct symtab_node *n = symtab_node::get (decl);
5487 if (n && n->refuse_visibility_changes)
5488 error ("%+qD declared weak after being used", decl);
5489 DECL_WEAK (decl) = 1;
5491 if (DECL_RTL_SET_P (decl)
5492 && MEM_P (DECL_RTL (decl))
5493 && XEXP (DECL_RTL (decl), 0)
5494 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5495 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5498 /* Merge weak status between NEWDECL and OLDDECL. */
5500 void
5501 merge_weak (tree newdecl, tree olddecl)
5503 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5505 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5507 tree *pwd;
5508 /* We put the NEWDECL on the weak_decls list at some point
5509 and OLDDECL as well. Keep just OLDDECL on the list. */
5510 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5511 if (TREE_VALUE (*pwd) == newdecl)
5513 *pwd = TREE_CHAIN (*pwd);
5514 break;
5517 return;
5520 if (DECL_WEAK (newdecl))
5522 tree wd;
5524 /* NEWDECL is weak, but OLDDECL is not. */
5526 /* If we already output the OLDDECL, we're in trouble; we can't
5527 go back and make it weak. This should never happen in
5528 unit-at-a-time compilation. */
5529 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5531 /* If we've already generated rtl referencing OLDDECL, we may
5532 have done so in a way that will not function properly with
5533 a weak symbol. Again in unit-at-a-time this should be
5534 impossible. */
5535 gcc_assert (!TREE_USED (olddecl)
5536 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5538 /* PR 49899: You cannot convert a static function into a weak, public function. */
5539 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
5540 error ("weak declaration of %q+D being applied to a already "
5541 "existing, static definition", newdecl);
5543 if (TARGET_SUPPORTS_WEAK)
5545 /* We put the NEWDECL on the weak_decls list at some point.
5546 Replace it with the OLDDECL. */
5547 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5548 if (TREE_VALUE (wd) == newdecl)
5550 TREE_VALUE (wd) = olddecl;
5551 break;
5553 /* We may not find the entry on the list. If NEWDECL is a
5554 weak alias, then we will have already called
5555 globalize_decl to remove the entry; in that case, we do
5556 not need to do anything. */
5559 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5560 mark_weak (olddecl);
5562 else
5563 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5564 weak. Just update NEWDECL to indicate that it's weak too. */
5565 mark_weak (newdecl);
5568 /* Declare DECL to be a weak symbol. */
5570 void
5571 declare_weak (tree decl)
5573 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5574 if (! TREE_PUBLIC (decl))
5576 error ("weak declaration of %q+D must be public", decl);
5577 return;
5579 else if (!TARGET_SUPPORTS_WEAK)
5580 warning (0, "weak declaration of %q+D not supported", decl);
5582 mark_weak (decl);
5583 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5584 DECL_ATTRIBUTES (decl)
5585 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5588 static void
5589 weak_finish_1 (tree decl)
5591 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5592 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5593 #endif
5595 if (! TREE_USED (decl))
5596 return;
5598 #ifdef ASM_WEAKEN_DECL
5599 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5600 #else
5601 #ifdef ASM_WEAKEN_LABEL
5602 ASM_WEAKEN_LABEL (asm_out_file, name);
5603 #else
5604 #ifdef ASM_OUTPUT_WEAK_ALIAS
5606 static bool warn_once = 0;
5607 if (! warn_once)
5609 warning (0, "only weak aliases are supported in this configuration");
5610 warn_once = 1;
5612 return;
5614 #endif
5615 #endif
5616 #endif
5619 /* Fiven an assembly name, find the decl it is associated with. */
5620 static tree
5621 find_decl (tree target)
5623 symtab_node *node = symtab_node::get_for_asmname (target);
5624 if (node)
5625 return node->decl;
5626 return NULL_TREE;
5629 /* This TREE_LIST contains weakref targets. */
5631 static GTY(()) tree weakref_targets;
5633 /* Emit any pending weak declarations. */
5635 void
5636 weak_finish (void)
5638 tree t;
5640 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5642 tree alias_decl = TREE_PURPOSE (t);
5643 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5645 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5646 /* Remove alias_decl from the weak list, but leave entries for
5647 the target alone. */
5648 target = NULL_TREE;
5649 #ifndef ASM_OUTPUT_WEAKREF
5650 else if (! TREE_SYMBOL_REFERENCED (target))
5652 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5653 defined, otherwise we and weak_finish_1 would use
5654 different macros. */
5655 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5656 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5657 # else
5658 tree decl = find_decl (target);
5660 if (! decl)
5662 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5663 TREE_CODE (alias_decl), target,
5664 TREE_TYPE (alias_decl));
5666 DECL_EXTERNAL (decl) = 1;
5667 TREE_PUBLIC (decl) = 1;
5668 DECL_ARTIFICIAL (decl) = 1;
5669 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5670 TREE_USED (decl) = 1;
5673 weak_finish_1 (decl);
5674 # endif
5676 #endif
5679 tree *p;
5680 tree t2;
5682 /* Remove the alias and the target from the pending weak list
5683 so that we do not emit any .weak directives for the former,
5684 nor multiple .weak directives for the latter. */
5685 for (p = &weak_decls; (t2 = *p) ; )
5687 if (TREE_VALUE (t2) == alias_decl
5688 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5689 *p = TREE_CHAIN (t2);
5690 else
5691 p = &TREE_CHAIN (t2);
5694 /* Remove other weakrefs to the same target, to speed things up. */
5695 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5697 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5698 *p = TREE_CHAIN (t2);
5699 else
5700 p = &TREE_CHAIN (t2);
5705 for (t = weak_decls; t; t = TREE_CHAIN (t))
5707 tree decl = TREE_VALUE (t);
5709 weak_finish_1 (decl);
5713 /* Emit the assembly bits to indicate that DECL is globally visible. */
5715 static void
5716 globalize_decl (tree decl)
5719 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5720 if (DECL_WEAK (decl))
5722 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5723 tree *p, t;
5725 #ifdef ASM_WEAKEN_DECL
5726 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5727 #else
5728 ASM_WEAKEN_LABEL (asm_out_file, name);
5729 #endif
5731 /* Remove this function from the pending weak list so that
5732 we do not emit multiple .weak directives for it. */
5733 for (p = &weak_decls; (t = *p) ; )
5735 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5736 *p = TREE_CHAIN (t);
5737 else
5738 p = &TREE_CHAIN (t);
5741 /* Remove weakrefs to the same target from the pending weakref
5742 list, for the same reason. */
5743 for (p = &weakref_targets; (t = *p) ; )
5745 if (DECL_ASSEMBLER_NAME (decl)
5746 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5747 *p = TREE_CHAIN (t);
5748 else
5749 p = &TREE_CHAIN (t);
5752 return;
5754 #endif
5756 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5759 vec<alias_pair, va_gc> *alias_pairs;
5761 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5762 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5763 tree node is DECL to have the value of the tree node TARGET. */
5765 void
5766 do_assemble_alias (tree decl, tree target)
5768 tree id;
5770 /* Emulated TLS had better not get this var. */
5771 gcc_assert (!(!targetm.have_tls
5772 && VAR_P (decl)
5773 && DECL_THREAD_LOCAL_P (decl)));
5775 if (TREE_ASM_WRITTEN (decl))
5776 return;
5778 id = DECL_ASSEMBLER_NAME (decl);
5779 ultimate_transparent_alias_target (&id);
5780 ultimate_transparent_alias_target (&target);
5782 /* We must force creation of DECL_RTL for debug info generation, even though
5783 we don't use it here. */
5784 make_decl_rtl (decl);
5786 TREE_ASM_WRITTEN (decl) = 1;
5787 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5788 TREE_ASM_WRITTEN (id) = 1;
5790 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5792 if (!TREE_SYMBOL_REFERENCED (target))
5793 weakref_targets = tree_cons (decl, target, weakref_targets);
5795 #ifdef ASM_OUTPUT_WEAKREF
5796 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5797 IDENTIFIER_POINTER (id),
5798 IDENTIFIER_POINTER (target));
5799 #else
5800 if (!TARGET_SUPPORTS_WEAK)
5802 error_at (DECL_SOURCE_LOCATION (decl),
5803 "weakref is not supported in this configuration");
5804 return;
5806 #endif
5807 return;
5810 #ifdef ASM_OUTPUT_DEF
5811 tree orig_decl = decl;
5813 if (TREE_CODE (decl) == FUNCTION_DECL
5814 && cgraph_node::get (decl)->instrumentation_clone
5815 && cgraph_node::get (decl)->instrumented_version)
5816 orig_decl = cgraph_node::get (decl)->instrumented_version->decl;
5818 /* Make name accessible from other files, if appropriate. */
5820 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5822 globalize_decl (decl);
5823 maybe_assemble_visibility (decl);
5825 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5827 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5828 if (targetm.has_ifunc_p ())
5829 ASM_OUTPUT_TYPE_DIRECTIVE
5830 (asm_out_file, IDENTIFIER_POINTER (id),
5831 IFUNC_ASM_TYPE);
5832 else
5833 #endif
5834 error_at (DECL_SOURCE_LOCATION (decl),
5835 "ifunc is not supported on this target");
5838 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5839 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5840 # else
5841 ASM_OUTPUT_DEF (asm_out_file,
5842 IDENTIFIER_POINTER (id),
5843 IDENTIFIER_POINTER (target));
5844 # endif
5845 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5847 const char *name;
5848 tree *p, t;
5850 name = IDENTIFIER_POINTER (id);
5851 # ifdef ASM_WEAKEN_DECL
5852 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5853 # else
5854 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5855 # endif
5856 /* Remove this function from the pending weak list so that
5857 we do not emit multiple .weak directives for it. */
5858 for (p = &weak_decls; (t = *p) ; )
5859 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5860 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5861 *p = TREE_CHAIN (t);
5862 else
5863 p = &TREE_CHAIN (t);
5865 /* Remove weakrefs to the same target from the pending weakref
5866 list, for the same reason. */
5867 for (p = &weakref_targets; (t = *p) ; )
5869 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5870 *p = TREE_CHAIN (t);
5871 else
5872 p = &TREE_CHAIN (t);
5875 #endif
5878 /* Emit an assembler directive to make the symbol for DECL an alias to
5879 the symbol for TARGET. */
5881 void
5882 assemble_alias (tree decl, tree target)
5884 tree target_decl;
5886 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5888 tree alias = DECL_ASSEMBLER_NAME (decl);
5890 ultimate_transparent_alias_target (&target);
5892 if (alias == target)
5893 error ("weakref %q+D ultimately targets itself", decl);
5894 if (TREE_PUBLIC (decl))
5895 error ("weakref %q+D must have static linkage", decl);
5897 else
5899 #if !defined (ASM_OUTPUT_DEF)
5900 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5901 error_at (DECL_SOURCE_LOCATION (decl),
5902 "alias definitions not supported in this configuration");
5903 TREE_ASM_WRITTEN (decl) = 1;
5904 return;
5905 # else
5906 if (!DECL_WEAK (decl))
5908 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5909 error_at (DECL_SOURCE_LOCATION (decl),
5910 "ifunc is not supported in this configuration");
5911 else
5912 error_at (DECL_SOURCE_LOCATION (decl),
5913 "only weak aliases are supported in this configuration");
5914 TREE_ASM_WRITTEN (decl) = 1;
5915 return;
5917 # endif
5918 #endif
5920 TREE_USED (decl) = 1;
5922 /* Allow aliases to aliases. */
5923 if (TREE_CODE (decl) == FUNCTION_DECL)
5924 cgraph_node::get_create (decl)->alias = true;
5925 else
5926 varpool_node::get_create (decl)->alias = true;
5928 /* If the target has already been emitted, we don't have to queue the
5929 alias. This saves a tad of memory. */
5930 if (symtab->global_info_ready)
5931 target_decl = find_decl (target);
5932 else
5933 target_decl= NULL;
5934 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5935 || symtab->state >= EXPANSION)
5936 do_assemble_alias (decl, target);
5937 else
5939 alias_pair p = {decl, target};
5940 vec_safe_push (alias_pairs, p);
5944 /* Record and output a table of translations from original function
5945 to its transaction aware clone. Note that tm_pure functions are
5946 considered to be their own clone. */
5948 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
5950 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
5951 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
5953 static int
5954 keep_cache_entry (tree_map *&e)
5956 return ggc_marked_p (e->base.from);
5960 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
5962 void
5963 record_tm_clone_pair (tree o, tree n)
5965 struct tree_map **slot, *h;
5967 if (tm_clone_hash == NULL)
5968 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
5970 h = ggc_alloc<tree_map> ();
5971 h->hash = htab_hash_pointer (o);
5972 h->base.from = o;
5973 h->to = n;
5975 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
5976 *slot = h;
5979 tree
5980 get_tm_clone_pair (tree o)
5982 if (tm_clone_hash)
5984 struct tree_map *h, in;
5986 in.base.from = o;
5987 in.hash = htab_hash_pointer (o);
5988 h = tm_clone_hash->find_with_hash (&in, in.hash);
5989 if (h)
5990 return h->to;
5992 return NULL_TREE;
5995 struct tm_alias_pair
5997 unsigned int uid;
5998 tree from;
5999 tree to;
6003 /* Dump the actual pairs to the .tm_clone_table section. */
6005 static void
6006 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6008 unsigned i;
6009 tm_alias_pair *p;
6010 bool switched = false;
6012 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6014 tree src = p->from;
6015 tree dst = p->to;
6016 struct cgraph_node *src_n = cgraph_node::get (src);
6017 struct cgraph_node *dst_n = cgraph_node::get (dst);
6019 /* The function ipa_tm_create_version() marks the clone as needed if
6020 the original function was needed. But we also mark the clone as
6021 needed if we ever called the clone indirectly through
6022 TM_GETTMCLONE. If neither of these are true, we didn't generate
6023 a clone, and we didn't call it indirectly... no sense keeping it
6024 in the clone table. */
6025 if (!dst_n || !dst_n->definition)
6026 continue;
6028 /* This covers the case where we have optimized the original
6029 function away, and only access the transactional clone. */
6030 if (!src_n || !src_n->definition)
6031 continue;
6033 if (!switched)
6035 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6036 assemble_align (POINTER_SIZE);
6037 switched = true;
6040 assemble_integer (XEXP (DECL_RTL (src), 0),
6041 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6042 assemble_integer (XEXP (DECL_RTL (dst), 0),
6043 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6047 /* Provide a default for the tm_clone_table section. */
6049 section *
6050 default_clone_table_section (void)
6052 return get_named_section (NULL, ".tm_clone_table", 3);
6055 /* Helper comparison function for qsorting by the DECL_UID stored in
6056 alias_pair->emitted_diags. */
6058 static int
6059 tm_alias_pair_cmp (const void *x, const void *y)
6061 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6062 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6063 if (p1->uid < p2->uid)
6064 return -1;
6065 if (p1->uid > p2->uid)
6066 return 1;
6067 return 0;
6070 void
6071 finish_tm_clone_pairs (void)
6073 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6075 if (tm_clone_hash == NULL)
6076 return;
6078 /* We need a determenistic order for the .tm_clone_table, otherwise
6079 we will get bootstrap comparison failures, so dump the hash table
6080 to a vector, sort it, and dump the vector. */
6082 /* Dump the hashtable to a vector. */
6083 tree_map *map;
6084 hash_table<tm_clone_hasher>::iterator iter;
6085 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6087 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6088 tm_alias_pairs.safe_push (p);
6090 /* Sort it. */
6091 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6093 /* Dump it. */
6094 dump_tm_clone_pairs (tm_alias_pairs);
6096 tm_clone_hash->empty ();
6097 tm_clone_hash = NULL;
6098 tm_alias_pairs.release ();
6102 /* Emit an assembler directive to set symbol for DECL visibility to
6103 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6105 void
6106 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6107 int vis ATTRIBUTE_UNUSED)
6109 #ifdef HAVE_GAS_HIDDEN
6110 static const char * const visibility_types[] = {
6111 NULL, "protected", "hidden", "internal"
6114 const char *name, *type;
6115 tree id;
6117 id = DECL_ASSEMBLER_NAME (decl);
6118 ultimate_transparent_alias_target (&id);
6119 name = IDENTIFIER_POINTER (id);
6121 type = visibility_types[vis];
6123 fprintf (asm_out_file, "\t.%s\t", type);
6124 assemble_name (asm_out_file, name);
6125 fprintf (asm_out_file, "\n");
6126 #else
6127 if (!DECL_ARTIFICIAL (decl))
6128 warning (OPT_Wattributes, "visibility attribute not supported "
6129 "in this configuration; ignored");
6130 #endif
6133 /* A helper function to call assemble_visibility when needed for a decl. */
6136 maybe_assemble_visibility (tree decl)
6138 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6140 if (TREE_CODE (decl) == FUNCTION_DECL
6141 && cgraph_node::get (decl)
6142 && cgraph_node::get (decl)->instrumentation_clone
6143 && cgraph_node::get (decl)->instrumented_version)
6144 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
6146 if (vis != VISIBILITY_DEFAULT)
6148 targetm.asm_out.assemble_visibility (decl, vis);
6149 return 1;
6151 else
6152 return 0;
6155 /* Returns 1 if the target configuration supports defining public symbols
6156 so that one of them will be chosen at link time instead of generating a
6157 multiply-defined symbol error, whether through the use of weak symbols or
6158 a target-specific mechanism for having duplicates discarded. */
6161 supports_one_only (void)
6163 if (SUPPORTS_ONE_ONLY)
6164 return 1;
6165 return TARGET_SUPPORTS_WEAK;
6168 /* Set up DECL as a public symbol that can be defined in multiple
6169 translation units without generating a linker error. */
6171 void
6172 make_decl_one_only (tree decl, tree comdat_group)
6174 struct symtab_node *symbol;
6175 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6177 TREE_PUBLIC (decl) = 1;
6179 if (VAR_P (decl))
6180 symbol = varpool_node::get_create (decl);
6181 else
6182 symbol = cgraph_node::get_create (decl);
6184 if (SUPPORTS_ONE_ONLY)
6186 #ifdef MAKE_DECL_ONE_ONLY
6187 MAKE_DECL_ONE_ONLY (decl);
6188 #endif
6189 symbol->set_comdat_group (comdat_group);
6191 else if (VAR_P (decl)
6192 && (DECL_INITIAL (decl) == 0
6193 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6194 DECL_COMMON (decl) = 1;
6195 else
6197 gcc_assert (TARGET_SUPPORTS_WEAK);
6198 DECL_WEAK (decl) = 1;
6202 void
6203 init_varasm_once (void)
6205 section_htab = hash_table<section_hasher>::create_ggc (31);
6206 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6207 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6209 shared_constant_pool = create_constant_pool ();
6211 #ifdef TEXT_SECTION_ASM_OP
6212 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6213 TEXT_SECTION_ASM_OP);
6214 #endif
6216 #ifdef DATA_SECTION_ASM_OP
6217 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6218 DATA_SECTION_ASM_OP);
6219 #endif
6221 #ifdef SDATA_SECTION_ASM_OP
6222 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6223 SDATA_SECTION_ASM_OP);
6224 #endif
6226 #ifdef READONLY_DATA_SECTION_ASM_OP
6227 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6228 READONLY_DATA_SECTION_ASM_OP);
6229 #endif
6231 #ifdef CTORS_SECTION_ASM_OP
6232 ctors_section = get_unnamed_section (0, output_section_asm_op,
6233 CTORS_SECTION_ASM_OP);
6234 #endif
6236 #ifdef DTORS_SECTION_ASM_OP
6237 dtors_section = get_unnamed_section (0, output_section_asm_op,
6238 DTORS_SECTION_ASM_OP);
6239 #endif
6241 #ifdef BSS_SECTION_ASM_OP
6242 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6243 output_section_asm_op,
6244 BSS_SECTION_ASM_OP);
6245 #endif
6247 #ifdef SBSS_SECTION_ASM_OP
6248 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6249 output_section_asm_op,
6250 SBSS_SECTION_ASM_OP);
6251 #endif
6253 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6254 | SECTION_COMMON, emit_tls_common);
6255 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6256 | SECTION_COMMON, emit_local);
6257 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6258 | SECTION_COMMON, emit_common);
6260 #if defined ASM_OUTPUT_ALIGNED_BSS
6261 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6262 emit_bss);
6263 #endif
6265 targetm.asm_out.init_sections ();
6267 if (readonly_data_section == NULL)
6268 readonly_data_section = text_section;
6270 #ifdef ASM_OUTPUT_EXTERNAL
6271 pending_assemble_externals_set = new hash_set<tree>;
6272 #endif
6275 enum tls_model
6276 decl_default_tls_model (const_tree decl)
6278 enum tls_model kind;
6279 bool is_local;
6281 is_local = targetm.binds_local_p (decl);
6282 if (!flag_shlib)
6284 if (is_local)
6285 kind = TLS_MODEL_LOCAL_EXEC;
6286 else
6287 kind = TLS_MODEL_INITIAL_EXEC;
6290 /* Local dynamic is inefficient when we're not combining the
6291 parts of the address. */
6292 else if (optimize && is_local)
6293 kind = TLS_MODEL_LOCAL_DYNAMIC;
6294 else
6295 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6296 if (kind < flag_tls_default)
6297 kind = flag_tls_default;
6299 return kind;
6302 /* Select a set of attributes for section NAME based on the properties
6303 of DECL and whether or not RELOC indicates that DECL's initializer
6304 might contain runtime relocations.
6306 We make the section read-only and executable for a function decl,
6307 read-only for a const data decl, and writable for a non-const data decl. */
6309 unsigned int
6310 default_section_type_flags (tree decl, const char *name, int reloc)
6312 unsigned int flags;
6314 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6315 flags = SECTION_CODE;
6316 else if (decl)
6318 enum section_category category
6319 = categorize_decl_for_section (decl, reloc);
6320 if (decl_readonly_section_1 (category))
6321 flags = 0;
6322 else if (category == SECCAT_DATA_REL_RO
6323 || category == SECCAT_DATA_REL_RO_LOCAL)
6324 flags = SECTION_WRITE | SECTION_RELRO;
6325 else
6326 flags = SECTION_WRITE;
6328 else
6330 flags = SECTION_WRITE;
6331 if (strcmp (name, ".data.rel.ro") == 0
6332 || strcmp (name, ".data.rel.ro.local") == 0)
6333 flags |= SECTION_RELRO;
6336 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6337 flags |= SECTION_LINKONCE;
6339 if (strcmp (name, ".vtable_map_vars") == 0)
6340 flags |= SECTION_LINKONCE;
6342 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6343 flags |= SECTION_TLS | SECTION_WRITE;
6345 if (strcmp (name, ".bss") == 0
6346 || strncmp (name, ".bss.", 5) == 0
6347 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6348 || strcmp (name, ".persistent.bss") == 0
6349 || strcmp (name, ".sbss") == 0
6350 || strncmp (name, ".sbss.", 6) == 0
6351 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6352 flags |= SECTION_BSS;
6354 if (strcmp (name, ".tdata") == 0
6355 || strncmp (name, ".tdata.", 7) == 0
6356 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6357 flags |= SECTION_TLS;
6359 if (strcmp (name, ".tbss") == 0
6360 || strncmp (name, ".tbss.", 6) == 0
6361 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6362 flags |= SECTION_TLS | SECTION_BSS;
6364 /* These three sections have special ELF types. They are neither
6365 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6366 want to print a section type (@progbits or @nobits). If someone
6367 is silly enough to emit code or TLS variables to one of these
6368 sections, then don't handle them specially. */
6369 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6370 && (strcmp (name, ".init_array") == 0
6371 || strcmp (name, ".fini_array") == 0
6372 || strcmp (name, ".preinit_array") == 0))
6373 flags |= SECTION_NOTYPE;
6375 return flags;
6378 /* Return true if the target supports some form of global BSS,
6379 either through bss_noswitch_section, or by selecting a BSS
6380 section in TARGET_ASM_SELECT_SECTION. */
6382 bool
6383 have_global_bss_p (void)
6385 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6388 /* Output assembly to switch to section NAME with attribute FLAGS.
6389 Four variants for common object file formats. */
6391 void
6392 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6393 unsigned int flags ATTRIBUTE_UNUSED,
6394 tree decl ATTRIBUTE_UNUSED)
6396 /* Some object formats don't support named sections at all. The
6397 front-end should already have flagged this as an error. */
6398 gcc_unreachable ();
6401 #ifndef TLS_SECTION_ASM_FLAG
6402 #define TLS_SECTION_ASM_FLAG 'T'
6403 #endif
6405 void
6406 default_elf_asm_named_section (const char *name, unsigned int flags,
6407 tree decl)
6409 char flagchars[11], *f = flagchars;
6410 unsigned int numeric_value = 0;
6412 /* If we have already declared this section, we can use an
6413 abbreviated form to switch back to it -- unless this section is
6414 part of a COMDAT groups, in which case GAS requires the full
6415 declaration every time. */
6416 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6417 && (flags & SECTION_DECLARED))
6419 fprintf (asm_out_file, "\t.section\t%s\n", name);
6420 return;
6423 /* If we have a machine specific flag, then use the numeric value to pass
6424 this on to GAS. */
6425 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6426 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6427 else
6429 if (!(flags & SECTION_DEBUG))
6430 *f++ = 'a';
6431 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6432 if (flags & SECTION_EXCLUDE)
6433 *f++ = 'e';
6434 #endif
6435 if (flags & SECTION_WRITE)
6436 *f++ = 'w';
6437 if (flags & SECTION_CODE)
6438 *f++ = 'x';
6439 if (flags & SECTION_SMALL)
6440 *f++ = 's';
6441 if (flags & SECTION_MERGE)
6442 *f++ = 'M';
6443 if (flags & SECTION_STRINGS)
6444 *f++ = 'S';
6445 if (flags & SECTION_TLS)
6446 *f++ = TLS_SECTION_ASM_FLAG;
6447 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6448 *f++ = 'G';
6449 #ifdef MACH_DEP_SECTION_ASM_FLAG
6450 if (flags & SECTION_MACH_DEP)
6451 *f++ = MACH_DEP_SECTION_ASM_FLAG;
6452 #endif
6453 *f = '\0';
6456 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6458 if (!(flags & SECTION_NOTYPE))
6460 const char *type;
6461 const char *format;
6463 if (flags & SECTION_BSS)
6464 type = "nobits";
6465 else
6466 type = "progbits";
6468 format = ",@%s";
6469 /* On platforms that use "@" as the assembly comment character,
6470 use "%" instead. */
6471 if (strcmp (ASM_COMMENT_START, "@") == 0)
6472 format = ",%%%s";
6473 fprintf (asm_out_file, format, type);
6475 if (flags & SECTION_ENTSIZE)
6476 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6477 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6479 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6480 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6481 else
6482 fprintf (asm_out_file, ",%s,comdat",
6483 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6487 putc ('\n', asm_out_file);
6490 void
6491 default_coff_asm_named_section (const char *name, unsigned int flags,
6492 tree decl ATTRIBUTE_UNUSED)
6494 char flagchars[8], *f = flagchars;
6496 if (flags & SECTION_WRITE)
6497 *f++ = 'w';
6498 if (flags & SECTION_CODE)
6499 *f++ = 'x';
6500 *f = '\0';
6502 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6505 void
6506 default_pe_asm_named_section (const char *name, unsigned int flags,
6507 tree decl)
6509 default_coff_asm_named_section (name, flags, decl);
6511 if (flags & SECTION_LINKONCE)
6513 /* Functions may have been compiled at various levels of
6514 optimization so we can't use `same_size' here.
6515 Instead, have the linker pick one. */
6516 fprintf (asm_out_file, "\t.linkonce %s\n",
6517 (flags & SECTION_CODE ? "discard" : "same_size"));
6521 /* The lame default section selector. */
6523 section *
6524 default_select_section (tree decl, int reloc,
6525 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6527 if (DECL_P (decl))
6529 if (decl_readonly_section (decl, reloc))
6530 return readonly_data_section;
6532 else if (TREE_CODE (decl) == CONSTRUCTOR)
6534 if (! ((flag_pic && reloc)
6535 || !TREE_READONLY (decl)
6536 || TREE_SIDE_EFFECTS (decl)
6537 || !TREE_CONSTANT (decl)))
6538 return readonly_data_section;
6540 else if (TREE_CODE (decl) == STRING_CST)
6541 return readonly_data_section;
6542 else if (! (flag_pic && reloc))
6543 return readonly_data_section;
6545 return data_section;
6548 enum section_category
6549 categorize_decl_for_section (const_tree decl, int reloc)
6551 enum section_category ret;
6553 if (TREE_CODE (decl) == FUNCTION_DECL)
6554 return SECCAT_TEXT;
6555 else if (TREE_CODE (decl) == STRING_CST)
6557 if ((flag_sanitize & SANITIZE_ADDRESS)
6558 && asan_protect_global (CONST_CAST_TREE (decl)))
6559 /* or !flag_merge_constants */
6560 return SECCAT_RODATA;
6561 else
6562 return SECCAT_RODATA_MERGE_STR;
6564 else if (VAR_P (decl))
6566 tree d = CONST_CAST_TREE (decl);
6567 if (bss_initializer_p (decl))
6568 ret = SECCAT_BSS;
6569 else if (! TREE_READONLY (decl)
6570 || TREE_SIDE_EFFECTS (decl)
6571 || (DECL_INITIAL (decl)
6572 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
6574 /* Here the reloc_rw_mask is not testing whether the section should
6575 be read-only or not, but whether the dynamic link will have to
6576 do something. If so, we wish to segregate the data in order to
6577 minimize cache misses inside the dynamic linker. */
6578 if (reloc & targetm.asm_out.reloc_rw_mask ())
6579 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6580 else
6581 ret = SECCAT_DATA;
6583 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6584 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6585 else if (reloc || flag_merge_constants < 2
6586 || ((flag_sanitize & SANITIZE_ADDRESS)
6587 /* PR 81697: for architectures that use section anchors we
6588 need to ignore DECL_RTL_SET_P (decl) for string constants
6589 inside this asan_protect_global call because otherwise
6590 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
6591 section, set DECL_RTL (decl) later on and add DECL to
6592 protected globals via successive asan_protect_global
6593 calls. In this scenario we'll end up with wrong
6594 alignment of these strings at runtime and possible ASan
6595 false positives. */
6596 && asan_protect_global (d, use_object_blocks_p ()
6597 && use_blocks_for_decl_p (d))))
6598 /* C and C++ don't allow different variables to share the same
6599 location. -fmerge-all-constants allows even that (at the
6600 expense of not conforming). */
6601 ret = SECCAT_RODATA;
6602 else if (DECL_INITIAL (decl)
6603 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6604 ret = SECCAT_RODATA_MERGE_STR_INIT;
6605 else
6606 ret = SECCAT_RODATA_MERGE_CONST;
6608 else if (TREE_CODE (decl) == CONSTRUCTOR)
6610 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6611 || TREE_SIDE_EFFECTS (decl)
6612 || ! TREE_CONSTANT (decl))
6613 ret = SECCAT_DATA;
6614 else
6615 ret = SECCAT_RODATA;
6617 else
6618 ret = SECCAT_RODATA;
6620 /* There are no read-only thread-local sections. */
6621 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6623 /* Note that this would be *just* SECCAT_BSS, except that there's
6624 no concept of a read-only thread-local-data section. */
6625 if (ret == SECCAT_BSS
6626 || DECL_INITIAL (decl) == NULL
6627 || (flag_zero_initialized_in_bss
6628 && initializer_zerop (DECL_INITIAL (decl))))
6629 ret = SECCAT_TBSS;
6630 else
6631 ret = SECCAT_TDATA;
6634 /* If the target uses small data sections, select it. */
6635 else if (targetm.in_small_data_p (decl))
6637 if (ret == SECCAT_BSS)
6638 ret = SECCAT_SBSS;
6639 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6640 ret = SECCAT_SRODATA;
6641 else
6642 ret = SECCAT_SDATA;
6645 return ret;
6648 static bool
6649 decl_readonly_section_1 (enum section_category category)
6651 switch (category)
6653 case SECCAT_RODATA:
6654 case SECCAT_RODATA_MERGE_STR:
6655 case SECCAT_RODATA_MERGE_STR_INIT:
6656 case SECCAT_RODATA_MERGE_CONST:
6657 case SECCAT_SRODATA:
6658 return true;
6659 default:
6660 return false;
6664 bool
6665 decl_readonly_section (const_tree decl, int reloc)
6667 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6670 /* Select a section based on the above categorization. */
6672 section *
6673 default_elf_select_section (tree decl, int reloc,
6674 unsigned HOST_WIDE_INT align)
6676 const char *sname;
6677 switch (categorize_decl_for_section (decl, reloc))
6679 case SECCAT_TEXT:
6680 /* We're not supposed to be called on FUNCTION_DECLs. */
6681 gcc_unreachable ();
6682 case SECCAT_RODATA:
6683 return readonly_data_section;
6684 case SECCAT_RODATA_MERGE_STR:
6685 return mergeable_string_section (decl, align, 0);
6686 case SECCAT_RODATA_MERGE_STR_INIT:
6687 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6688 case SECCAT_RODATA_MERGE_CONST:
6689 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6690 case SECCAT_SRODATA:
6691 sname = ".sdata2";
6692 break;
6693 case SECCAT_DATA:
6694 return data_section;
6695 case SECCAT_DATA_REL:
6696 sname = ".data.rel";
6697 break;
6698 case SECCAT_DATA_REL_LOCAL:
6699 sname = ".data.rel.local";
6700 break;
6701 case SECCAT_DATA_REL_RO:
6702 sname = ".data.rel.ro";
6703 break;
6704 case SECCAT_DATA_REL_RO_LOCAL:
6705 sname = ".data.rel.ro.local";
6706 break;
6707 case SECCAT_SDATA:
6708 sname = ".sdata";
6709 break;
6710 case SECCAT_TDATA:
6711 sname = ".tdata";
6712 break;
6713 case SECCAT_BSS:
6714 if (bss_section)
6715 return bss_section;
6716 sname = ".bss";
6717 break;
6718 case SECCAT_SBSS:
6719 sname = ".sbss";
6720 break;
6721 case SECCAT_TBSS:
6722 sname = ".tbss";
6723 break;
6724 default:
6725 gcc_unreachable ();
6728 return get_named_section (decl, sname, reloc);
6731 /* Construct a unique section name based on the decl name and the
6732 categorization performed above. */
6734 void
6735 default_unique_section (tree decl, int reloc)
6737 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6738 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6739 const char *prefix, *name, *linkonce;
6740 char *string;
6741 tree id;
6743 switch (categorize_decl_for_section (decl, reloc))
6745 case SECCAT_TEXT:
6746 prefix = one_only ? ".t" : ".text";
6747 break;
6748 case SECCAT_RODATA:
6749 case SECCAT_RODATA_MERGE_STR:
6750 case SECCAT_RODATA_MERGE_STR_INIT:
6751 case SECCAT_RODATA_MERGE_CONST:
6752 prefix = one_only ? ".r" : ".rodata";
6753 break;
6754 case SECCAT_SRODATA:
6755 prefix = one_only ? ".s2" : ".sdata2";
6756 break;
6757 case SECCAT_DATA:
6758 prefix = one_only ? ".d" : ".data";
6759 break;
6760 case SECCAT_DATA_REL:
6761 prefix = one_only ? ".d.rel" : ".data.rel";
6762 break;
6763 case SECCAT_DATA_REL_LOCAL:
6764 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6765 break;
6766 case SECCAT_DATA_REL_RO:
6767 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6768 break;
6769 case SECCAT_DATA_REL_RO_LOCAL:
6770 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6771 break;
6772 case SECCAT_SDATA:
6773 prefix = one_only ? ".s" : ".sdata";
6774 break;
6775 case SECCAT_BSS:
6776 prefix = one_only ? ".b" : ".bss";
6777 break;
6778 case SECCAT_SBSS:
6779 prefix = one_only ? ".sb" : ".sbss";
6780 break;
6781 case SECCAT_TDATA:
6782 prefix = one_only ? ".td" : ".tdata";
6783 break;
6784 case SECCAT_TBSS:
6785 prefix = one_only ? ".tb" : ".tbss";
6786 break;
6787 default:
6788 gcc_unreachable ();
6791 id = DECL_ASSEMBLER_NAME (decl);
6792 ultimate_transparent_alias_target (&id);
6793 name = IDENTIFIER_POINTER (id);
6794 name = targetm.strip_name_encoding (name);
6796 /* If we're using one_only, then there needs to be a .gnu.linkonce
6797 prefix to the section name. */
6798 linkonce = one_only ? ".gnu.linkonce" : "";
6800 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6802 set_decl_section_name (decl, string);
6805 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6807 static int
6808 compute_reloc_for_rtx_1 (const_rtx x)
6810 switch (GET_CODE (x))
6812 case SYMBOL_REF:
6813 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6814 case LABEL_REF:
6815 return 1;
6816 default:
6817 return 0;
6821 /* Like compute_reloc_for_constant, except for an RTX. The return value
6822 is a mask for which bit 1 indicates a global relocation, and bit 0
6823 indicates a local relocation. */
6825 static int
6826 compute_reloc_for_rtx (const_rtx x)
6828 switch (GET_CODE (x))
6830 case SYMBOL_REF:
6831 case LABEL_REF:
6832 return compute_reloc_for_rtx_1 (x);
6834 case CONST:
6836 int reloc = 0;
6837 subrtx_iterator::array_type array;
6838 FOR_EACH_SUBRTX (iter, array, x, ALL)
6839 reloc |= compute_reloc_for_rtx_1 (*iter);
6840 return reloc;
6843 default:
6844 return 0;
6848 section *
6849 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6850 rtx x,
6851 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6853 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6854 return data_section;
6855 else
6856 return readonly_data_section;
6859 section *
6860 default_elf_select_rtx_section (machine_mode mode, rtx x,
6861 unsigned HOST_WIDE_INT align)
6863 int reloc = compute_reloc_for_rtx (x);
6865 /* ??? Handle small data here somehow. */
6867 if (reloc & targetm.asm_out.reloc_rw_mask ())
6869 if (reloc == 1)
6870 return get_named_section (NULL, ".data.rel.ro.local", 1);
6871 else
6872 return get_named_section (NULL, ".data.rel.ro", 3);
6875 return mergeable_constant_section (mode, align, 0);
6878 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6880 void
6881 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6883 rtx symbol;
6884 int flags;
6886 /* Careful not to prod global register variables. */
6887 if (!MEM_P (rtl))
6888 return;
6889 symbol = XEXP (rtl, 0);
6890 if (GET_CODE (symbol) != SYMBOL_REF)
6891 return;
6893 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6894 if (TREE_CODE (decl) == FUNCTION_DECL)
6895 flags |= SYMBOL_FLAG_FUNCTION;
6896 if (targetm.binds_local_p (decl))
6897 flags |= SYMBOL_FLAG_LOCAL;
6898 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6899 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6900 else if (targetm.in_small_data_p (decl))
6901 flags |= SYMBOL_FLAG_SMALL;
6902 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6903 being PUBLIC, the thing *must* be defined in this translation unit.
6904 Prevent this buglet from being propagated into rtl code as well. */
6905 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6906 flags |= SYMBOL_FLAG_EXTERNAL;
6908 SYMBOL_REF_FLAGS (symbol) = flags;
6911 /* By default, we do nothing for encode_section_info, so we need not
6912 do anything but discard the '*' marker. */
6914 const char *
6915 default_strip_name_encoding (const char *str)
6917 return str + (*str == '*');
6920 #ifdef ASM_OUTPUT_DEF
6921 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6922 anchor relative to ".", the current section position. */
6924 void
6925 default_asm_output_anchor (rtx symbol)
6927 char buffer[100];
6929 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6930 SYMBOL_REF_BLOCK_OFFSET (symbol));
6931 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6933 #endif
6935 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6937 bool
6938 default_use_anchors_for_symbol_p (const_rtx symbol)
6940 section *sect;
6941 tree decl;
6943 /* Don't use anchors for mergeable sections. The linker might move
6944 the objects around. */
6945 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6946 if (sect->common.flags & SECTION_MERGE)
6947 return false;
6949 /* Don't use anchors for small data sections. The small data register
6950 acts as an anchor for such sections. */
6951 if (sect->common.flags & SECTION_SMALL)
6952 return false;
6954 decl = SYMBOL_REF_DECL (symbol);
6955 if (decl && DECL_P (decl))
6957 /* Don't use section anchors for decls that might be defined or
6958 usurped by other modules. */
6959 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6960 return false;
6962 /* Don't use section anchors for decls that will be placed in a
6963 small data section. */
6964 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6965 one above. The problem is that we only use SECTION_SMALL for
6966 sections that should be marked as small in the section directive. */
6967 if (targetm.in_small_data_p (decl))
6968 return false;
6970 /* Don't use section anchors for decls that won't fit inside a single
6971 anchor range to reduce the amount of instructions required to refer
6972 to the entire declaration. */
6973 if (DECL_SIZE_UNIT (decl) == NULL_TREE
6974 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
6975 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
6976 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
6977 return false;
6980 return true;
6983 /* Return true when RESOLUTION indicate that symbol will be bound to the
6984 definition provided by current .o file. */
6986 static bool
6987 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6989 return (resolution == LDPR_PREVAILING_DEF
6990 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6991 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6994 /* Return true when RESOLUTION indicate that symbol will be bound locally
6995 within current executable or DSO. */
6997 static bool
6998 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7000 return (resolution == LDPR_PREVAILING_DEF
7001 || resolution == LDPR_PREVAILING_DEF_IRONLY
7002 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7003 || resolution == LDPR_PREEMPTED_REG
7004 || resolution == LDPR_PREEMPTED_IR
7005 || resolution == LDPR_RESOLVED_IR
7006 || resolution == LDPR_RESOLVED_EXEC);
7009 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7010 uninitialized common symbol in the executable will still be defined
7011 (through COPY relocation) in the executable. */
7013 bool
7014 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7015 bool extern_protected_data, bool common_local_p)
7017 /* A non-decl is an entry in the constant pool. */
7018 if (!DECL_P (exp))
7019 return true;
7021 /* Weakrefs may not bind locally, even though the weakref itself is always
7022 static and therefore local. Similarly, the resolver for ifunc functions
7023 might resolve to a non-local function.
7024 FIXME: We can resolve the weakref case more curefuly by looking at the
7025 weakref alias. */
7026 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7027 || (TREE_CODE (exp) == FUNCTION_DECL
7028 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
7029 return false;
7031 /* Static variables are always local. */
7032 if (! TREE_PUBLIC (exp))
7033 return true;
7035 /* With resolution file in hand, take look into resolutions.
7036 We can't just return true for resolved_locally symbols,
7037 because dynamic linking might overwrite symbols
7038 in shared libraries. */
7039 bool resolved_locally = false;
7041 bool uninited_common = (DECL_COMMON (exp)
7042 && (DECL_INITIAL (exp) == NULL
7043 || (!in_lto_p
7044 && DECL_INITIAL (exp) == error_mark_node)));
7046 /* A non-external variable is defined locally only if it isn't
7047 uninitialized COMMON variable or common_local_p is true. */
7048 bool defined_locally = (!DECL_EXTERNAL (exp)
7049 && (!uninited_common || common_local_p));
7050 if (symtab_node *node = symtab_node::get (exp))
7052 if (node->in_other_partition)
7053 defined_locally = true;
7054 if (node->can_be_discarded_p ())
7056 else if (resolution_to_local_definition_p (node->resolution))
7057 defined_locally = resolved_locally = true;
7058 else if (resolution_local_p (node->resolution))
7059 resolved_locally = true;
7061 if (defined_locally && weak_dominate && !shlib)
7062 resolved_locally = true;
7064 /* Undefined weak symbols are never defined locally. */
7065 if (DECL_WEAK (exp) && !defined_locally)
7066 return false;
7068 /* A symbol is local if the user has said explicitly that it will be,
7069 or if we have a definition for the symbol. We cannot infer visibility
7070 for undefined symbols. */
7071 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7072 && (TREE_CODE (exp) == FUNCTION_DECL
7073 || !extern_protected_data
7074 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7075 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7076 return true;
7078 /* If PIC, then assume that any global name can be overridden by
7079 symbols resolved from other modules. */
7080 if (shlib)
7081 return false;
7083 /* Variables defined outside this object might not be local. */
7084 if (DECL_EXTERNAL (exp) && !resolved_locally)
7085 return false;
7087 /* Non-dominant weak symbols are not defined locally. */
7088 if (DECL_WEAK (exp) && !resolved_locally)
7089 return false;
7091 /* Uninitialized COMMON variable may be unified with symbols
7092 resolved from other modules. */
7093 if (uninited_common && !resolved_locally)
7094 return false;
7096 /* Otherwise we're left with initialized (or non-common) global data
7097 which is of necessity defined locally. */
7098 return true;
7101 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7102 wrt cross-module name binding. */
7104 bool
7105 default_binds_local_p (const_tree exp)
7107 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7110 /* Similar to default_binds_local_p, but common symbol may be local and
7111 extern protected data is non-local. */
7113 bool
7114 default_binds_local_p_2 (const_tree exp)
7116 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7117 !flag_pic);
7120 bool
7121 default_binds_local_p_1 (const_tree exp, int shlib)
7123 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7126 /* Return true when references to DECL must bind to current definition in
7127 final executable.
7129 The condition is usually equivalent to whether the function binds to the
7130 current module (shared library or executable), that is to binds_local_p.
7131 We use this fact to avoid need for another target hook and implement
7132 the logic using binds_local_p and just special cases where
7133 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7134 the weak definitions (that can be overwritten at linktime by other
7135 definition from different object file) and when resolution info is available
7136 we simply use the knowledge passed to us by linker plugin. */
7137 bool
7138 decl_binds_to_current_def_p (const_tree decl)
7140 gcc_assert (DECL_P (decl));
7141 if (!targetm.binds_local_p (decl))
7142 return false;
7143 if (!TREE_PUBLIC (decl))
7144 return true;
7146 /* When resolution is available, just use it. */
7147 if (symtab_node *node = symtab_node::get (decl))
7149 if (node->resolution != LDPR_UNKNOWN
7150 && !node->can_be_discarded_p ())
7151 return resolution_to_local_definition_p (node->resolution);
7154 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7155 binds locally but still can be overwritten), DECL_COMMON (can be merged
7156 with a non-common definition somewhere in the same module) or
7157 DECL_EXTERNAL.
7158 This rely on fact that binds_local_p behave as decl_replaceable_p
7159 for all other declaration types. */
7160 if (DECL_WEAK (decl))
7161 return false;
7162 if (DECL_COMMON (decl)
7163 && (DECL_INITIAL (decl) == NULL
7164 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7165 return false;
7166 if (DECL_EXTERNAL (decl))
7167 return false;
7168 return true;
7171 /* A replaceable function or variable is one which may be replaced
7172 at link-time with an entirely different definition, provided that the
7173 replacement has the same type. For example, functions declared
7174 with __attribute__((weak)) on most systems are replaceable.
7176 COMDAT functions are not replaceable, since all definitions of the
7177 function must be equivalent. It is important that COMDAT functions
7178 not be treated as replaceable so that use of C++ template
7179 instantiations is not penalized. */
7181 bool
7182 decl_replaceable_p (tree decl)
7184 gcc_assert (DECL_P (decl));
7185 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7186 return false;
7187 if (!flag_semantic_interposition
7188 && !DECL_WEAK (decl))
7189 return false;
7190 return !decl_binds_to_current_def_p (decl);
7193 /* Default function to output code that will globalize a label. A
7194 target must define GLOBAL_ASM_OP or provide its own function to
7195 globalize a label. */
7196 #ifdef GLOBAL_ASM_OP
7197 void
7198 default_globalize_label (FILE * stream, const char *name)
7200 fputs (GLOBAL_ASM_OP, stream);
7201 assemble_name (stream, name);
7202 putc ('\n', stream);
7204 #endif /* GLOBAL_ASM_OP */
7206 /* Default function to output code that will globalize a declaration. */
7207 void
7208 default_globalize_decl_name (FILE * stream, tree decl)
7210 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7211 targetm.asm_out.globalize_label (stream, name);
7214 /* Default function to output a label for unwind information. The
7215 default is to do nothing. A target that needs nonlocal labels for
7216 unwind information must provide its own function to do this. */
7217 void
7218 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7219 tree decl ATTRIBUTE_UNUSED,
7220 int for_eh ATTRIBUTE_UNUSED,
7221 int empty ATTRIBUTE_UNUSED)
7225 /* Default function to output a label to divide up the exception table.
7226 The default is to do nothing. A target that needs/wants to divide
7227 up the table must provide it's own function to do this. */
7228 void
7229 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7233 /* This is how to output an internal numbered label where PREFIX is
7234 the class of label and LABELNO is the number within the class. */
7236 void
7237 default_generate_internal_label (char *buf, const char *prefix,
7238 unsigned long labelno)
7240 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7243 /* This is how to output an internal numbered label where PREFIX is
7244 the class of label and LABELNO is the number within the class. */
7246 void
7247 default_internal_label (FILE *stream, const char *prefix,
7248 unsigned long labelno)
7250 char *const buf = (char *) alloca (40 + strlen (prefix));
7251 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7252 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7256 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7258 void
7259 default_asm_declare_constant_name (FILE *file, const char *name,
7260 const_tree exp ATTRIBUTE_UNUSED,
7261 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7263 assemble_label (file, name);
7266 /* This is the default behavior at the beginning of a file. It's
7267 controlled by two other target-hook toggles. */
7268 void
7269 default_file_start (void)
7271 if (targetm.asm_file_start_app_off
7272 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7273 fputs (ASM_APP_OFF, asm_out_file);
7275 if (targetm.asm_file_start_file_directive)
7277 /* LTO produced units have no meaningful main_input_filename. */
7278 if (in_lto_p)
7279 output_file_directive (asm_out_file, "<artificial>");
7280 else
7281 output_file_directive (asm_out_file, main_input_filename);
7285 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7286 which emits a special section directive used to indicate whether or
7287 not this object file needs an executable stack. This is primarily
7288 a GNU extension to ELF but could be used on other targets. */
7290 int trampolines_created;
7292 void
7293 file_end_indicate_exec_stack (void)
7295 unsigned int flags = SECTION_DEBUG;
7296 if (trampolines_created)
7297 flags |= SECTION_CODE;
7299 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7302 /* Emit a special section directive to indicate that this object file
7303 was compiled with -fsplit-stack. This is used to let the linker
7304 detect calls between split-stack code and non-split-stack code, so
7305 that it can modify the split-stack code to allocate a sufficiently
7306 large stack. We emit another special section if there are any
7307 functions in this file which have the no_split_stack attribute, to
7308 prevent the linker from warning about being unable to convert the
7309 functions if they call non-split-stack code. */
7311 void
7312 file_end_indicate_split_stack (void)
7314 if (flag_split_stack)
7316 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7317 NULL));
7318 if (saw_no_split_stack)
7319 switch_to_section (get_section (".note.GNU-no-split-stack",
7320 SECTION_DEBUG, NULL));
7324 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7325 a get_unnamed_section callback. */
7327 void
7328 output_section_asm_op (const void *directive)
7330 fprintf (asm_out_file, "%s\n", (const char *) directive);
7333 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7334 the current section is NEW_SECTION. */
7336 void
7337 switch_to_section (section *new_section)
7339 if (in_section == new_section)
7340 return;
7342 if (new_section->common.flags & SECTION_FORGET)
7343 in_section = NULL;
7344 else
7345 in_section = new_section;
7347 switch (SECTION_STYLE (new_section))
7349 case SECTION_NAMED:
7350 targetm.asm_out.named_section (new_section->named.name,
7351 new_section->named.common.flags,
7352 new_section->named.decl);
7353 break;
7355 case SECTION_UNNAMED:
7356 new_section->unnamed.callback (new_section->unnamed.data);
7357 break;
7359 case SECTION_NOSWITCH:
7360 gcc_unreachable ();
7361 break;
7364 new_section->common.flags |= SECTION_DECLARED;
7367 /* If block symbol SYMBOL has not yet been assigned an offset, place
7368 it at the end of its block. */
7370 void
7371 place_block_symbol (rtx symbol)
7373 unsigned HOST_WIDE_INT size, mask, offset;
7374 struct constant_descriptor_rtx *desc;
7375 unsigned int alignment;
7376 struct object_block *block;
7377 tree decl;
7379 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7380 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7381 return;
7383 /* Work out the symbol's size and alignment. */
7384 if (CONSTANT_POOL_ADDRESS_P (symbol))
7386 desc = SYMBOL_REF_CONSTANT (symbol);
7387 alignment = desc->align;
7388 size = GET_MODE_SIZE (desc->mode);
7390 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7392 decl = SYMBOL_REF_DECL (symbol);
7393 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7394 alignment = DECL_ALIGN (decl);
7395 size = get_constant_size (DECL_INITIAL (decl));
7396 if ((flag_sanitize & SANITIZE_ADDRESS)
7397 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7398 && asan_protect_global (DECL_INITIAL (decl)))
7400 size += asan_red_zone_size (size);
7401 alignment = MAX (alignment,
7402 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7405 else
7407 struct symtab_node *snode;
7408 decl = SYMBOL_REF_DECL (symbol);
7410 snode = symtab_node::get (decl);
7411 if (snode->alias)
7413 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7415 gcc_assert (MEM_P (target)
7416 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7417 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7418 target = XEXP (target, 0);
7419 place_block_symbol (target);
7420 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7421 return;
7423 alignment = get_variable_align (decl);
7424 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7425 if ((flag_sanitize & SANITIZE_ADDRESS)
7426 && asan_protect_global (decl))
7428 size += asan_red_zone_size (size);
7429 alignment = MAX (alignment,
7430 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7434 /* Calculate the object's offset from the start of the block. */
7435 block = SYMBOL_REF_BLOCK (symbol);
7436 mask = alignment / BITS_PER_UNIT - 1;
7437 offset = (block->size + mask) & ~mask;
7438 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7440 /* Record the block's new alignment and size. */
7441 block->alignment = MAX (block->alignment, alignment);
7442 block->size = offset + size;
7444 vec_safe_push (block->objects, symbol);
7447 /* Return the anchor that should be used to address byte offset OFFSET
7448 from the first object in BLOCK. MODEL is the TLS model used
7449 to access it. */
7452 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7453 enum tls_model model)
7455 char label[100];
7456 unsigned int begin, middle, end;
7457 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7458 rtx anchor;
7460 /* Work out the anchor's offset. Use an offset of 0 for the first
7461 anchor so that we don't pessimize the case where we take the address
7462 of a variable at the beginning of the block. This is particularly
7463 useful when a block has only one variable assigned to it.
7465 We try to place anchors RANGE bytes apart, so there can then be
7466 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7467 a ptr_mode offset. With some target settings, the lowest such
7468 anchor might be out of range for the lowest ptr_mode offset;
7469 likewise the highest anchor for the highest offset. Use anchors
7470 at the extreme ends of the ptr_mode range in such cases.
7472 All arithmetic uses unsigned integers in order to avoid
7473 signed overflow. */
7474 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7475 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7476 range = max_offset - min_offset + 1;
7477 if (range == 0)
7478 offset = 0;
7479 else
7481 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7482 if (offset < 0)
7484 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7485 delta -= delta % range;
7486 if (delta > bias)
7487 delta = bias;
7488 offset = (HOST_WIDE_INT) (-delta);
7490 else
7492 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7493 delta -= delta % range;
7494 if (delta > bias - 1)
7495 delta = bias - 1;
7496 offset = (HOST_WIDE_INT) delta;
7500 /* Do a binary search to see if there's already an anchor we can use.
7501 Set BEGIN to the new anchor's index if not. */
7502 begin = 0;
7503 end = vec_safe_length (block->anchors);
7504 while (begin != end)
7506 middle = (end + begin) / 2;
7507 anchor = (*block->anchors)[middle];
7508 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7509 end = middle;
7510 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7511 begin = middle + 1;
7512 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7513 end = middle;
7514 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7515 begin = middle + 1;
7516 else
7517 return anchor;
7520 /* Create a new anchor with a unique label. */
7521 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7522 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7523 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7524 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7526 /* Insert it at index BEGIN. */
7527 vec_safe_insert (block->anchors, begin, anchor);
7528 return anchor;
7531 /* Output the objects in BLOCK. */
7533 static void
7534 output_object_block (struct object_block *block)
7536 struct constant_descriptor_rtx *desc;
7537 unsigned int i;
7538 HOST_WIDE_INT offset;
7539 tree decl;
7540 rtx symbol;
7542 if (!block->objects)
7543 return;
7545 /* Switch to the section and make sure that the first byte is
7546 suitably aligned. */
7547 /* Special case VTV comdat sections similar to assemble_variable. */
7548 if (SECTION_STYLE (block->sect) == SECTION_NAMED
7549 && block->sect->named.name
7550 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
7551 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
7552 else
7553 switch_to_section (block->sect);
7555 assemble_align (block->alignment);
7557 /* Define the values of all anchors relative to the current section
7558 position. */
7559 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7560 targetm.asm_out.output_anchor (symbol);
7562 /* Output the objects themselves. */
7563 offset = 0;
7564 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7566 /* Move to the object's offset, padding with zeros if necessary. */
7567 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7568 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7569 if (CONSTANT_POOL_ADDRESS_P (symbol))
7571 desc = SYMBOL_REF_CONSTANT (symbol);
7572 /* Pass 1 for align as we have already laid out everything in the block.
7573 So aligning shouldn't be necessary. */
7574 output_constant_pool_1 (desc, 1);
7575 offset += GET_MODE_SIZE (desc->mode);
7577 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7579 HOST_WIDE_INT size;
7580 decl = SYMBOL_REF_DECL (symbol);
7581 assemble_constant_contents
7582 (DECL_INITIAL (decl), XSTR (symbol, 0), DECL_ALIGN (decl));
7584 size = get_constant_size (DECL_INITIAL (decl));
7585 offset += size;
7586 if ((flag_sanitize & SANITIZE_ADDRESS)
7587 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7588 && asan_protect_global (DECL_INITIAL (decl)))
7590 size = asan_red_zone_size (size);
7591 assemble_zeros (size);
7592 offset += size;
7595 else
7597 HOST_WIDE_INT size;
7598 decl = SYMBOL_REF_DECL (symbol);
7599 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7600 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7601 offset += size;
7602 if ((flag_sanitize & SANITIZE_ADDRESS)
7603 && asan_protect_global (decl))
7605 size = asan_red_zone_size (size);
7606 assemble_zeros (size);
7607 offset += size;
7613 /* A callback for qsort to compare object_blocks. */
7615 static int
7616 output_object_block_compare (const void *x, const void *y)
7618 object_block *p1 = *(object_block * const*)x;
7619 object_block *p2 = *(object_block * const*)y;
7621 if (p1->sect->common.flags & SECTION_NAMED
7622 && !(p2->sect->common.flags & SECTION_NAMED))
7623 return 1;
7625 if (!(p1->sect->common.flags & SECTION_NAMED)
7626 && p2->sect->common.flags & SECTION_NAMED)
7627 return -1;
7629 if (p1->sect->common.flags & SECTION_NAMED
7630 && p2->sect->common.flags & SECTION_NAMED)
7631 return strcmp (p1->sect->named.name, p2->sect->named.name);
7633 unsigned f1 = p1->sect->common.flags;
7634 unsigned f2 = p2->sect->common.flags;
7635 if (f1 == f2)
7636 return 0;
7637 return f1 < f2 ? -1 : 1;
7640 /* Output the definitions of all object_blocks. */
7642 void
7643 output_object_blocks (void)
7645 vec<object_block *, va_heap> v;
7646 v.create (object_block_htab->elements ());
7647 object_block *obj;
7648 hash_table<object_block_hasher>::iterator hi;
7650 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
7651 v.quick_push (obj);
7653 /* Sort them in order to output them in a deterministic manner,
7654 otherwise we may get .rodata sections in different orders with
7655 and without -g. */
7656 v.qsort (output_object_block_compare);
7657 unsigned i;
7658 FOR_EACH_VEC_ELT (v, i, obj)
7659 output_object_block (obj);
7661 v.release ();
7664 /* This function provides a possible implementation of the
7665 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7666 by -frecord-gcc-switches it creates a new mergeable, string section in the
7667 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7668 contains the switches in ASCII format.
7670 FIXME: This code does not correctly handle double quote characters
7671 that appear inside strings, (it strips them rather than preserving them).
7672 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7673 characters - instead it treats them as sub-string separators. Since
7674 we want to emit NUL strings terminators into the object file we have to use
7675 ASM_OUTPUT_SKIP. */
7678 elf_record_gcc_switches (print_switch_type type, const char * name)
7680 switch (type)
7682 case SWITCH_TYPE_PASSED:
7683 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7684 ASM_OUTPUT_SKIP (asm_out_file, HOST_WIDE_INT_1U);
7685 break;
7687 case SWITCH_TYPE_DESCRIPTIVE:
7688 if (name == NULL)
7690 /* Distinguish between invocations where name is NULL. */
7691 static bool started = false;
7693 if (!started)
7695 section * sec;
7697 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7698 SECTION_DEBUG
7699 | SECTION_MERGE
7700 | SECTION_STRINGS
7701 | (SECTION_ENTSIZE & 1),
7702 NULL);
7703 switch_to_section (sec);
7704 started = true;
7708 default:
7709 break;
7712 /* The return value is currently ignored by the caller, but must be 0.
7713 For -fverbose-asm the return value would be the number of characters
7714 emitted into the assembler file. */
7715 return 0;
7718 /* Emit text to declare externally defined symbols. It is needed to
7719 properly support non-default visibility. */
7720 void
7721 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7722 tree decl,
7723 const char *name ATTRIBUTE_UNUSED)
7725 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7726 set in order to avoid putting out names that are never really
7727 used. Always output visibility specified in the source. */
7728 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7729 && (DECL_VISIBILITY_SPECIFIED (decl)
7730 || targetm.binds_local_p (decl)))
7731 maybe_assemble_visibility (decl);
7734 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7736 void
7737 default_asm_output_source_filename (FILE *file, const char *name)
7739 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7740 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7741 #else
7742 fprintf (file, "\t.file\t");
7743 output_quoted_string (file, name);
7744 putc ('\n', file);
7745 #endif
7748 /* Output a file name in the form wanted by System V. */
7750 void
7751 output_file_directive (FILE *asm_file, const char *input_name)
7753 int len;
7754 const char *na;
7756 if (input_name == NULL)
7757 input_name = "<stdin>";
7758 else
7759 input_name = remap_debug_filename (input_name);
7761 len = strlen (input_name);
7762 na = input_name + len;
7764 /* NA gets INPUT_NAME sans directory names. */
7765 while (na > input_name)
7767 if (IS_DIR_SEPARATOR (na[-1]))
7768 break;
7769 na--;
7772 targetm.asm_out.output_source_filename (asm_file, na);
7775 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7776 EXP. */
7778 make_debug_expr_from_rtl (const_rtx exp)
7780 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7781 machine_mode mode = GET_MODE (exp);
7782 rtx dval;
7784 DECL_ARTIFICIAL (ddecl) = 1;
7785 if (REG_P (exp) && REG_EXPR (exp))
7786 type = TREE_TYPE (REG_EXPR (exp));
7787 else if (MEM_P (exp) && MEM_EXPR (exp))
7788 type = TREE_TYPE (MEM_EXPR (exp));
7789 else
7790 type = NULL_TREE;
7791 if (type && TYPE_MODE (type) == mode)
7792 TREE_TYPE (ddecl) = type;
7793 else
7794 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7795 SET_DECL_MODE (ddecl, mode);
7796 dval = gen_rtx_DEBUG_EXPR (mode);
7797 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7798 SET_DECL_RTL (ddecl, dval);
7799 return dval;
7802 #ifdef ELF_ASCII_ESCAPES
7803 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7805 void
7806 default_elf_asm_output_limited_string (FILE *f, const char *s)
7808 int escape;
7809 unsigned char c;
7811 fputs (STRING_ASM_OP, f);
7812 putc ('"', f);
7813 while (*s != '\0')
7815 c = *s;
7816 escape = ELF_ASCII_ESCAPES[c];
7817 switch (escape)
7819 case 0:
7820 putc (c, f);
7821 break;
7822 case 1:
7823 putc ('\\', f);
7824 putc ('0'+((c>>6)&7), f);
7825 putc ('0'+((c>>3)&7), f);
7826 putc ('0'+(c&7), f);
7827 break;
7828 default:
7829 putc ('\\', f);
7830 putc (escape, f);
7831 break;
7833 s++;
7835 putc ('\"', f);
7836 putc ('\n', f);
7839 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7841 void
7842 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7844 const char *limit = s + len;
7845 const char *last_null = NULL;
7846 unsigned bytes_in_chunk = 0;
7847 unsigned char c;
7848 int escape;
7850 for (; s < limit; s++)
7852 const char *p;
7854 if (bytes_in_chunk >= 60)
7856 putc ('\"', f);
7857 putc ('\n', f);
7858 bytes_in_chunk = 0;
7861 if (s > last_null)
7863 for (p = s; p < limit && *p != '\0'; p++)
7864 continue;
7865 last_null = p;
7867 else
7868 p = last_null;
7870 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7872 if (bytes_in_chunk > 0)
7874 putc ('\"', f);
7875 putc ('\n', f);
7876 bytes_in_chunk = 0;
7879 default_elf_asm_output_limited_string (f, s);
7880 s = p;
7882 else
7884 if (bytes_in_chunk == 0)
7885 fputs (ASCII_DATA_ASM_OP "\"", f);
7887 c = *s;
7888 escape = ELF_ASCII_ESCAPES[c];
7889 switch (escape)
7891 case 0:
7892 putc (c, f);
7893 bytes_in_chunk++;
7894 break;
7895 case 1:
7896 putc ('\\', f);
7897 putc ('0'+((c>>6)&7), f);
7898 putc ('0'+((c>>3)&7), f);
7899 putc ('0'+(c&7), f);
7900 bytes_in_chunk += 4;
7901 break;
7902 default:
7903 putc ('\\', f);
7904 putc (escape, f);
7905 bytes_in_chunk += 2;
7906 break;
7912 if (bytes_in_chunk > 0)
7914 putc ('\"', f);
7915 putc ('\n', f);
7918 #endif
7920 static GTY(()) section *elf_init_array_section;
7921 static GTY(()) section *elf_fini_array_section;
7923 static section *
7924 get_elf_initfini_array_priority_section (int priority,
7925 bool constructor_p)
7927 section *sec;
7928 if (priority != DEFAULT_INIT_PRIORITY)
7930 char buf[18];
7931 sprintf (buf, "%s.%.5u",
7932 constructor_p ? ".init_array" : ".fini_array",
7933 priority);
7934 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7936 else
7938 if (constructor_p)
7940 if (elf_init_array_section == NULL)
7941 elf_init_array_section
7942 = get_section (".init_array",
7943 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7944 sec = elf_init_array_section;
7946 else
7948 if (elf_fini_array_section == NULL)
7949 elf_fini_array_section
7950 = get_section (".fini_array",
7951 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7952 sec = elf_fini_array_section;
7955 return sec;
7958 /* Use .init_array section for constructors. */
7960 void
7961 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7963 section *sec = get_elf_initfini_array_priority_section (priority,
7964 true);
7965 assemble_addr_to_section (symbol, sec);
7968 /* Use .fini_array section for destructors. */
7970 void
7971 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7973 section *sec = get_elf_initfini_array_priority_section (priority,
7974 false);
7975 assemble_addr_to_section (symbol, sec);
7978 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7980 This is a bit of a cheat. The real default is a no-op, but this
7981 hook is the default for all targets with a .ident directive. */
7983 void
7984 default_asm_output_ident_directive (const char *ident_str)
7986 const char *ident_asm_op = "\t.ident\t";
7988 /* If we are still in the front end, do not write out the string
7989 to asm_out_file. Instead, add a fake top-level asm statement.
7990 This allows the front ends to use this hook without actually
7991 writing to asm_out_file, to handle #ident or Pragma Ident. */
7992 if (symtab->state == PARSING)
7994 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7995 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7997 else
7998 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8002 /* This function ensures that vtable_map variables are not only
8003 in the comdat section, but that each variable has its own unique
8004 comdat name. Without this the variables end up in the same section
8005 with a single comdat name.
8007 FIXME: resolve_unique_section needs to deal better with
8008 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8009 that is fixed, this if-else statement can be replaced with
8010 a single call to "switch_to_section (sect)". */
8012 static void
8013 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8015 #if defined (OBJECT_FORMAT_ELF)
8016 targetm.asm_out.named_section (sect->named.name,
8017 sect->named.common.flags
8018 | SECTION_LINKONCE,
8019 DECL_NAME (decl));
8020 in_section = sect;
8021 #else
8022 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8023 Therefore the following check is used.
8024 In case a the target is PE or COFF a comdat group section
8025 is created, e.g. .vtable_map_vars$foo. The linker places
8026 everything in .vtable_map_vars at the end.
8028 A fix could be made in
8029 gcc/config/i386/winnt.c: i386_pe_unique_section. */
8030 if (TARGET_PECOFF)
8032 char *name;
8034 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
8035 name = ACONCAT ((sect->named.name, "$",
8036 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
8037 else
8038 name = ACONCAT ((sect->named.name, "$",
8039 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
8040 NULL));
8042 targetm.asm_out.named_section (name,
8043 sect->named.common.flags
8044 | SECTION_LINKONCE,
8045 DECL_NAME (decl));
8046 in_section = sect;
8048 else
8049 switch_to_section (sect);
8050 #endif
8053 #include "gt-varasm.h"