[PATCH] RISC-V: Move UNSPEC_SSP_SET and UNSPEC_SSP_TEST to correct enum
[gcc.git] / gcc / varasm.cc
blob6d93fe97d7bf67b75dc41ef0b994332e5392f587
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2025 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 "version.h"
47 #include "flags.h"
48 #include "stmt.h"
49 #include "expr.h"
50 #include "expmed.h"
51 #include "optabs.h"
52 #include "output.h"
53 #include "langhooks.h"
54 #include "debug.h"
55 #include "common/common-target.h"
56 #include "stringpool.h"
57 #include "attribs.h"
58 #include "asan.h"
59 #include "rtl-iter.h"
60 #include "file-prefix-map.h" /* remap_debug_filename() */
61 #include "alloc-pool.h"
62 #include "toplev.h"
63 #include "opts.h"
64 #include "asan.h"
65 #include "recog.h"
66 #include "gimple-expr.h"
68 /* The (assembler) name of the first globally-visible object output. */
69 extern GTY(()) const char *first_global_object_name;
70 extern GTY(()) const char *weak_global_object_name;
72 const char *first_global_object_name;
73 const char *weak_global_object_name;
75 class addr_const;
76 class constant_descriptor_rtx;
77 struct rtx_constant_pool;
79 #define n_deferred_constants (crtl->varasm.deferred_constants)
81 /* Number for making the label on the next
82 constant that is stored in memory. */
84 static GTY(()) int const_labelno;
86 /* Carry information from ASM_DECLARE_OBJECT_NAME
87 to ASM_FINISH_DECLARE_OBJECT. */
89 int size_directive_output;
91 /* The last decl for which assemble_variable was called,
92 if it did ASM_DECLARE_OBJECT_NAME.
93 If the last call to assemble_variable didn't do that,
94 this holds 0. */
96 tree last_assemble_variable_decl;
98 /* The following global variable indicates if the first basic block
99 in a function belongs to the cold partition or not. */
101 bool first_function_block_is_cold;
103 /* Whether we saw any functions with no_split_stack. */
105 static bool saw_no_split_stack;
107 static const char *strip_reg_name (const char *);
108 static bool contains_pointers_p (tree);
109 #ifdef ASM_OUTPUT_EXTERNAL
110 static bool incorporeal_function_p (tree);
111 #endif
112 static void decode_addr_const (tree, class addr_const *);
113 static hashval_t const_hash_1 (const tree);
114 static bool compare_constant (const tree, const tree);
115 static void output_constant_def_contents (rtx);
116 static void output_addressed_constants (tree, int);
117 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
118 unsigned int, bool, bool);
119 static void globalize_decl (tree);
120 static bool decl_readonly_section_1 (enum section_category);
121 #ifdef BSS_SECTION_ASM_OP
122 #ifdef ASM_OUTPUT_ALIGNED_BSS
123 static void asm_output_aligned_bss (FILE *, tree, const char *,
124 unsigned HOST_WIDE_INT, int)
125 ATTRIBUTE_UNUSED;
126 #endif
127 #endif /* BSS_SECTION_ASM_OP */
128 static void mark_weak (tree);
129 static void output_constant_pool (const char *, tree);
130 static void handle_vtv_comdat_section (section *, const_tree);
132 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
133 section *text_section;
134 section *data_section;
135 section *readonly_data_section;
136 section *sdata_section;
137 section *ctors_section;
138 section *dtors_section;
139 section *bss_section;
140 section *sbss_section;
142 /* Various forms of common section. All are guaranteed to be nonnull. */
143 section *tls_comm_section;
144 section *comm_section;
145 section *lcomm_section;
147 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
148 May be null. */
149 section *bss_noswitch_section;
151 /* The section that holds the main exception table, when known. The section
152 is set either by the target's init_sections hook or by the first call to
153 switch_to_exception_section. */
154 section *exception_section;
156 /* The section that holds the DWARF2 frame unwind information, when known.
157 The section is set either by the target's init_sections hook or by the
158 first call to switch_to_eh_frame_section. */
159 section *eh_frame_section;
161 /* asm_out_file's current section. This is NULL if no section has yet
162 been selected or if we lose track of what the current section is. */
163 section *in_section;
165 /* True if code for the current function is currently being directed
166 at the cold section. */
167 bool in_cold_section_p;
169 /* The following global holds the "function name" for the code in the
170 cold section of a function, if hot/cold function splitting is enabled
171 and there was actually code that went into the cold section. A
172 pseudo function name is needed for the cold section of code for some
173 debugging tools that perform symbolization. */
174 tree cold_function_name = NULL_TREE;
176 /* A linked list of all the unnamed sections. */
177 static GTY(()) section *unnamed_sections;
179 /* Return a nonzero value if DECL has a section attribute. */
180 #define IN_NAMED_SECTION(DECL) \
181 (VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
183 struct section_hasher : ggc_ptr_hash<section>
185 typedef const char *compare_type;
187 static hashval_t hash (section *);
188 static bool equal (section *, const char *);
191 /* Hash table of named sections. */
192 static GTY(()) hash_table<section_hasher> *section_htab;
194 struct object_block_hasher : ggc_ptr_hash<object_block>
196 typedef const section *compare_type;
198 static hashval_t hash (object_block *);
199 static bool equal (object_block *, const section *);
202 /* A table of object_blocks, indexed by section. */
203 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
205 /* The next number to use for internal anchor labels. */
206 static GTY(()) int anchor_labelno;
208 /* A pool of constants that can be shared between functions. */
209 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
211 /* Helper routines for maintaining section_htab. */
213 bool
214 section_hasher::equal (section *old, const char *new_name)
216 return strcmp (old->named.name, new_name) == 0;
219 hashval_t
220 section_hasher::hash (section *old)
222 return htab_hash_string (old->named.name);
225 /* Return a hash value for section SECT. */
227 static hashval_t
228 hash_section (section *sect)
230 if (sect->common.flags & SECTION_NAMED)
231 return htab_hash_string (sect->named.name);
232 return sect->common.flags & ~SECTION_DECLARED;
235 /* Helper routines for maintaining object_block_htab. */
237 inline bool
238 object_block_hasher::equal (object_block *old, const section *new_section)
240 return old->sect == new_section;
243 hashval_t
244 object_block_hasher::hash (object_block *old)
246 return hash_section (old->sect);
249 /* Return a new unnamed section with the given fields. */
251 section *
252 get_unnamed_section (unsigned int flags, void (*callback) (const char *),
253 const char *data)
255 section *sect;
257 sect = ggc_alloc<section> ();
258 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
259 sect->unnamed.callback = callback;
260 sect->unnamed.data = data;
261 sect->unnamed.next = unnamed_sections;
263 unnamed_sections = sect;
264 return sect;
267 /* Return a SECTION_NOSWITCH section with the given fields. */
269 static section *
270 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
272 section *sect;
274 sect = ggc_alloc<section> ();
275 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
276 sect->noswitch.callback = callback;
278 return sect;
281 /* Return the named section structure associated with NAME. Create
282 a new section with the given fields if no such structure exists.
283 When NOT_EXISTING, then fail if the section already exists. Return
284 the existing section if the SECTION_RETAIN bit doesn't match. Set
285 the SECTION_WRITE | SECTION_RELRO bits on the existing section
286 if one of the section flags is SECTION_WRITE | SECTION_RELRO and the
287 other has none of these flags in named sections and either the section
288 hasn't been declared yet or has been declared as writable. */
290 section *
291 get_section (const char *name, unsigned int flags, tree decl,
292 bool not_existing)
294 section *sect, **slot;
296 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
297 INSERT);
298 flags |= SECTION_NAMED;
299 if (decl != nullptr
300 && DECL_P (decl)
301 && lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
302 flags |= SECTION_RETAIN;
303 if (*slot == NULL)
305 sect = ggc_alloc<section> ();
306 sect->named.common.flags = flags;
307 sect->named.name = ggc_strdup (name);
308 sect->named.decl = decl;
309 *slot = sect;
311 else
313 if (not_existing)
314 internal_error ("section already exists: %qs", name);
316 sect = *slot;
317 /* It is fine if one of the sections has SECTION_NOTYPE as long as
318 the other has none of the contrary flags (see the logic at the end
319 of default_section_type_flags, below). */
320 if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
321 && !((sect->common.flags | flags)
322 & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
323 | (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
325 sect->common.flags |= SECTION_NOTYPE;
326 flags |= SECTION_NOTYPE;
328 if ((sect->common.flags & ~SECTION_DECLARED) != flags
329 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
331 /* It is fine if one of the section flags is
332 SECTION_WRITE | SECTION_RELRO and the other has none of these
333 flags (i.e. read-only) in named sections and either the
334 section hasn't been declared yet or has been declared as writable.
335 In that case just make sure the resulting flags are
336 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
337 relocations. */
338 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
339 == (SECTION_WRITE | SECTION_RELRO)
340 && (sect->common.flags
341 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
342 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
343 && ((sect->common.flags & SECTION_DECLARED) == 0
344 || (sect->common.flags & SECTION_WRITE)))
346 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
347 return sect;
349 /* If the SECTION_RETAIN bit doesn't match, return and switch
350 to a new section later. */
351 if ((sect->common.flags & SECTION_RETAIN)
352 != (flags & SECTION_RETAIN))
353 return sect;
354 /* Sanity check user variables for flag changes. */
355 if (sect->named.decl != NULL
356 && DECL_P (sect->named.decl)
357 && decl != sect->named.decl)
359 if (decl != NULL && DECL_P (decl))
360 error ("%+qD causes a section type conflict with %qD"
361 " in section %qs",
362 decl, sect->named.decl, name);
363 else
364 error ("section type conflict with %qD in section %qs",
365 sect->named.decl, name);
366 inform (DECL_SOURCE_LOCATION (sect->named.decl),
367 "%qD was declared here", sect->named.decl);
369 else if (decl != NULL && DECL_P (decl))
370 error ("%+qD causes a section type conflict for section %qs",
371 decl, name);
372 else
373 error ("section type conflict for section %qs", name);
374 /* Make sure we don't error about one section multiple times. */
375 sect->common.flags |= SECTION_OVERRIDE;
378 return sect;
381 /* Return true if the current compilation mode benefits from having
382 objects grouped into blocks. */
384 static bool
385 use_object_blocks_p (void)
387 return flag_section_anchors;
390 /* Return the object_block structure for section SECT. Create a new
391 structure if we haven't created one already. Return null if SECT
392 itself is null. Return also null for mergeable sections since
393 section anchors can't be used in mergeable sections anyway,
394 because the linker might move objects around, and using the
395 object blocks infrastructure in that case is both a waste and a
396 maintenance burden. */
398 static struct object_block *
399 get_block_for_section (section *sect)
401 struct object_block *block;
403 if (sect == NULL)
404 return NULL;
406 if (sect->common.flags & SECTION_MERGE)
407 return NULL;
409 object_block **slot
410 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
411 INSERT);
412 block = *slot;
413 if (block == NULL)
415 block = ggc_cleared_alloc<object_block> ();
416 block->sect = sect;
417 *slot = block;
419 return block;
422 /* Create a symbol with label LABEL and place it at byte offset
423 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
424 is not yet known. LABEL must be a garbage-collected string. */
426 static rtx
427 create_block_symbol (const char *label, struct object_block *block,
428 HOST_WIDE_INT offset)
430 rtx symbol;
431 unsigned int size;
433 /* Create the extended SYMBOL_REF. */
434 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
435 symbol = (rtx) ggc_internal_alloc (size);
437 /* Initialize the normal SYMBOL_REF fields. */
438 memset (symbol, 0, size);
439 PUT_CODE (symbol, SYMBOL_REF);
440 PUT_MODE (symbol, Pmode);
441 XSTR (symbol, 0) = label;
442 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
444 /* Initialize the block_symbol stuff. */
445 SYMBOL_REF_BLOCK (symbol) = block;
446 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
448 return symbol;
451 /* Return a section with a particular name and with whatever SECTION_*
452 flags section_type_flags deems appropriate. The name of the section
453 is taken from NAME if nonnull, otherwise it is taken from DECL's
454 DECL_SECTION_NAME. DECL is the decl associated with the section
455 (see the section comment for details) and RELOC is as for
456 section_type_flags. */
458 section *
459 get_named_section (tree decl, const char *name, int reloc)
461 unsigned int flags;
463 if (name == NULL)
465 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
466 name = DECL_SECTION_NAME (decl);
469 flags = targetm.section_type_flags (decl, name, reloc);
470 return get_section (name, flags, decl);
473 /* Worker for resolve_unique_section. */
475 static bool
476 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
478 n->implicit_section = true;
479 return false;
482 /* If required, set DECL_SECTION_NAME to a unique name. */
484 void
485 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
486 int flag_function_or_data_sections)
488 if (DECL_SECTION_NAME (decl) == NULL
489 && targetm_common.have_named_sections
490 && (flag_function_or_data_sections
491 || lookup_attribute ("retain", DECL_ATTRIBUTES (decl))
492 || DECL_COMDAT_GROUP (decl)))
494 targetm.asm_out.unique_section (decl, reloc);
495 if (DECL_SECTION_NAME (decl))
496 symtab_node::get (decl)->call_for_symbol_and_aliases
497 (set_implicit_section, NULL, true);
501 #ifdef BSS_SECTION_ASM_OP
503 #ifdef ASM_OUTPUT_ALIGNED_BSS
505 /* Utility function for targets to use in implementing
506 ASM_OUTPUT_ALIGNED_BSS.
507 ??? It is believed that this function will work in most cases so such
508 support is localized here. */
510 static void
511 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
512 const char *name, unsigned HOST_WIDE_INT size,
513 int align)
515 switch_to_section (bss_section);
516 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
517 #ifdef ASM_DECLARE_OBJECT_NAME
518 last_assemble_variable_decl = decl;
519 ASM_DECLARE_OBJECT_NAME (file, name, decl);
520 #else
521 /* Standard thing is just output label for the object. */
522 ASM_OUTPUT_LABEL (file, name);
523 #endif /* ASM_DECLARE_OBJECT_NAME */
524 ASM_OUTPUT_SKIP (file, size ? size : 1);
527 #endif
529 #endif /* BSS_SECTION_ASM_OP */
531 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
532 /* Return the hot section for function DECL. Return text_section for
533 null DECLs. */
535 static section *
536 hot_function_section (tree decl)
538 if (decl != NULL_TREE
539 && DECL_SECTION_NAME (decl) != NULL
540 && targetm_common.have_named_sections)
541 return get_named_section (decl, NULL, 0);
542 else
543 return text_section;
545 #endif
547 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
548 is NULL.
550 When DECL_SECTION_NAME is non-NULL and it is implicit section and
551 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
552 concatenate the name with NAMED_SECTION_SUFFIX.
553 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
555 section *
556 get_named_text_section (tree decl,
557 const char *text_section_name,
558 const char *named_section_suffix)
560 if (decl && DECL_SECTION_NAME (decl))
562 if (named_section_suffix)
564 const char *dsn = DECL_SECTION_NAME (decl);
565 const char *stripped_name;
566 char *name, *buffer;
568 name = (char *) alloca (strlen (dsn) + 1);
569 memcpy (name, dsn,
570 strlen (dsn) + 1);
572 stripped_name = targetm.strip_name_encoding (name);
574 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
575 return get_named_section (decl, buffer, 0);
577 else if (symtab_node::get (decl)->implicit_section)
579 const char *name;
581 /* Do not try to split gnu_linkonce functions. This gets somewhat
582 slipperly. */
583 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
584 return NULL;
585 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
586 name = targetm.strip_name_encoding (name);
587 return get_named_section (decl, ACONCAT ((text_section_name, ".",
588 name, NULL)), 0);
590 else
591 return NULL;
593 return get_named_section (decl, text_section_name, 0);
596 /* Choose named function section based on its frequency. */
598 section *
599 default_function_section (tree decl, enum node_frequency freq,
600 bool startup, bool exit)
602 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
603 /* Old GNU linkers have buggy --gc-section support, which sometimes
604 results in .gcc_except_table* sections being garbage collected. */
605 if (decl
606 && symtab_node::get (decl)->implicit_section)
607 return NULL;
608 #endif
610 if (!flag_reorder_functions
611 || !targetm_common.have_named_sections)
612 return NULL;
613 /* Startup code should go to startup subsection unless it is
614 unlikely executed (this happens especially with function splitting
615 where we can split away unnecessary parts of static constructors. */
616 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
618 /* During LTO the tp_first_run profiling will naturally place all
619 initialization code first. Using separate section is counter-productive
620 because startup only code may call functions which are no longer
621 startup only. */
622 if (!in_lto_p
623 || !cgraph_node::get (decl)->tp_first_run
624 || !opt_for_fn (decl, flag_profile_reorder_functions))
625 return get_named_text_section (decl, ".text.startup", NULL);
626 else
627 return NULL;
630 /* Similarly for exit. */
631 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
632 return get_named_text_section (decl, ".text.exit", NULL);
634 /* Group cold functions together, similarly for hot code. */
635 switch (freq)
637 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
638 return get_named_text_section (decl, ".text.unlikely", NULL);
639 case NODE_FREQUENCY_HOT:
640 return get_named_text_section (decl, ".text.hot", NULL);
641 /* FALLTHRU */
642 default:
643 return NULL;
647 /* Return the section for function DECL.
649 If DECL is NULL_TREE, return the text section. We can be passed
650 NULL_TREE under some circumstances by dbxout.cc at least.
652 If FORCE_COLD is true, return cold function section ignoring
653 the frequency info of cgraph_node. */
655 static section *
656 function_section_1 (tree decl, bool force_cold)
658 section *section = NULL;
659 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
660 bool startup = false, exit = false;
662 if (decl)
664 struct cgraph_node *node = cgraph_node::get (decl);
666 if (node)
668 freq = node->frequency;
669 startup = node->only_called_at_startup;
670 exit = node->only_called_at_exit;
673 if (force_cold)
674 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
676 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
677 if (decl != NULL_TREE
678 && DECL_SECTION_NAME (decl) != NULL)
680 if (targetm.asm_out.function_section)
681 section = targetm.asm_out.function_section (decl, freq,
682 startup, exit);
683 if (section)
684 return section;
685 return get_named_section (decl, NULL, 0);
687 else
688 return targetm.asm_out.select_section
689 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
690 symtab_node::get (decl)->definition_alignment ());
691 #else
692 if (targetm.asm_out.function_section)
693 section = targetm.asm_out.function_section (decl, freq, startup, exit);
694 if (section)
695 return section;
696 return hot_function_section (decl);
697 #endif
700 /* Return the section for function DECL.
702 If DECL is NULL_TREE, return the text section. We can be passed
703 NULL_TREE under some circumstances by dbxout.cc at least. */
705 section *
706 function_section (tree decl)
708 /* Handle cases where function splitting code decides
709 to put function entry point into unlikely executed section
710 despite the fact that the function itself is not cold
711 (i.e. it is called rarely but contains a hot loop that is
712 better to live in hot subsection for the code locality). */
713 return function_section_1 (decl,
714 first_function_block_is_cold);
717 /* Return the section for the current function, take IN_COLD_SECTION_P
718 into account. */
720 section *
721 current_function_section (void)
723 return function_section_1 (current_function_decl, in_cold_section_p);
726 /* Tell assembler to switch to unlikely-to-be-executed text section. */
728 section *
729 unlikely_text_section (void)
731 return function_section_1 (current_function_decl, true);
734 /* When called within a function context, return true if the function
735 has been assigned a cold text section and if SECT is that section.
736 When called outside a function context, return true if SECT is the
737 default cold section. */
739 bool
740 unlikely_text_section_p (section *sect)
742 return sect == function_section_1 (current_function_decl, true);
745 /* Switch to the other function partition (if inside of hot section
746 into cold section, otherwise into the hot section). */
748 void
749 switch_to_other_text_partition (void)
751 in_cold_section_p = !in_cold_section_p;
752 switch_to_section (current_function_section ());
755 /* Return the read-only or relocated read-only data section
756 associated with function DECL. */
758 section *
759 default_function_rodata_section (tree decl, bool relocatable)
761 const char* sname;
762 unsigned int flags;
764 flags = 0;
766 if (relocatable)
768 sname = ".data.rel.ro.local";
769 flags = (SECTION_WRITE | SECTION_RELRO);
771 else
772 sname = ".rodata";
774 if (decl && DECL_SECTION_NAME (decl))
776 const char *name = DECL_SECTION_NAME (decl);
778 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
780 const char *dot;
781 size_t len;
782 char* rname;
784 dot = strchr (name + 1, '.');
785 if (!dot)
786 dot = name;
787 len = strlen (dot) + strlen (sname) + 1;
788 rname = (char *) alloca (len);
790 strcpy (rname, sname);
791 strcat (rname, dot);
792 return get_section (rname, (SECTION_LINKONCE | flags), decl);
794 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo or
795 .gnu.linkonce.d.rel.ro.local.foo if the jump table is relocatable. */
796 else if (DECL_COMDAT_GROUP (decl)
797 && startswith (name, ".gnu.linkonce.t."))
799 size_t len;
800 char *rname;
802 if (relocatable)
804 len = strlen (name) + strlen (".rel.ro.local") + 1;
805 rname = (char *) alloca (len);
807 strcpy (rname, ".gnu.linkonce.d.rel.ro.local");
808 strcat (rname, name + 15);
810 else
812 len = strlen (name) + 1;
813 rname = (char *) alloca (len);
815 memcpy (rname, name, len);
816 rname[14] = 'r';
818 return get_section (rname, (SECTION_LINKONCE | flags), decl);
820 /* For .text.foo we want to use .rodata.foo. */
821 else if (flag_function_sections && flag_data_sections
822 && startswith (name, ".text."))
824 size_t len = strlen (name) + 1;
825 char *rname = (char *) alloca (len + strlen (sname) - 5);
827 memcpy (rname, sname, strlen (sname));
828 memcpy (rname + strlen (sname), name + 5, len - 5);
829 return get_section (rname, flags, decl);
833 if (relocatable)
834 return get_section (sname, flags, decl);
835 else
836 return readonly_data_section;
839 /* Return the read-only data section associated with function DECL
840 for targets where that section should be always the single
841 readonly data section. */
843 section *
844 default_no_function_rodata_section (tree, bool)
846 return readonly_data_section;
849 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
851 static const char *
852 function_mergeable_rodata_prefix (void)
854 section *s = targetm.asm_out.function_rodata_section (current_function_decl,
855 false);
856 if (SECTION_STYLE (s) == SECTION_NAMED)
857 return s->named.name;
858 else
859 return targetm.asm_out.mergeable_rodata_prefix;
862 /* Return the section to use for string merging. */
864 static section *
865 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
866 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
867 unsigned int flags ATTRIBUTE_UNUSED)
869 HOST_WIDE_INT len;
871 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
872 && TREE_CODE (decl) == STRING_CST
873 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
874 && align <= 256
875 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
876 && TREE_STRING_LENGTH (decl) == len)
878 scalar_int_mode mode;
879 unsigned int modesize;
880 const char *str;
881 HOST_WIDE_INT i;
882 int j, unit;
883 const char *prefix = function_mergeable_rodata_prefix ();
884 char *name = (char *) alloca (strlen (prefix) + 30);
886 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
887 modesize = GET_MODE_BITSIZE (mode);
888 if (modesize >= 8 && modesize <= 256
889 && (modesize & (modesize - 1)) == 0)
891 if (align < modesize)
892 align = modesize;
894 str = TREE_STRING_POINTER (decl);
895 unit = GET_MODE_SIZE (mode);
897 /* Check for embedded NUL characters. */
898 for (i = 0; i < len; i += unit)
900 for (j = 0; j < unit; j++)
901 if (str[i + j] != '\0')
902 break;
903 if (j == unit)
904 break;
906 if (i == len - unit || (unit == 1 && i == len))
908 sprintf (name, "%s.str%d.%d", prefix,
909 modesize / 8, (int) (align / 8));
910 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
911 return get_section (name, flags, NULL);
916 return readonly_data_section;
919 /* Return the section to use for constant merging. */
921 section *
922 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
923 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
924 unsigned int flags ATTRIBUTE_UNUSED)
926 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
927 && mode != VOIDmode
928 && mode != BLKmode
929 && known_le (GET_MODE_BITSIZE (mode), align)
930 && align >= 8
931 && align <= 256
932 && (align & (align - 1)) == 0)
934 const char *prefix = function_mergeable_rodata_prefix ();
935 char *name = (char *) alloca (strlen (prefix) + 30);
937 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
938 flags |= (align / 8) | SECTION_MERGE;
939 return get_section (name, flags, NULL);
941 return readonly_data_section;
944 /* Given NAME, a putative register name, discard any customary prefixes. */
946 static const char *
947 strip_reg_name (const char *name)
949 #ifdef REGISTER_PREFIX
950 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
951 name += strlen (REGISTER_PREFIX);
952 #endif
953 if (name[0] == '%' || name[0] == '#')
954 name++;
955 return name;
958 /* The user has asked for a DECL to have a particular name. Set (or
959 change) it in such a way that we don't prefix an underscore to
960 it. */
961 void
962 set_user_assembler_name (tree decl, const char *name)
964 char *starred = (char *) alloca (strlen (name) + 2);
965 starred[0] = '*';
966 strcpy (starred + 1, name);
967 symtab->change_decl_assembler_name (decl, get_identifier (starred));
968 SET_DECL_RTL (decl, NULL_RTX);
971 /* Decode an `asm' spec for a declaration as a register name.
972 Return the register number, or -1 if nothing specified,
973 or -2 if the ASMSPEC is not `cc' or `memory' or `redzone' and is not
974 recognized,
975 or -3 if ASMSPEC is `cc' and is not recognized,
976 or -4 if ASMSPEC is `memory' and is not recognized,
977 or -5 if ASMSPEC is `redzone' and is not recognized.
978 Accept an exact spelling or a decimal number.
979 Prefixes such as % are optional. */
982 decode_reg_name_and_count (const char *asmspec, int *pnregs)
984 /* Presume just one register is clobbered. */
985 *pnregs = 1;
987 if (asmspec != 0)
989 int i;
991 /* Get rid of confusing prefixes. */
992 asmspec = strip_reg_name (asmspec);
994 /* Allow a decimal number as a "register name". */
995 if (ISDIGIT (asmspec[0]))
997 char *pend;
998 errno = 0;
999 unsigned long j = strtoul (asmspec, &pend, 10);
1000 if (*pend == '\0')
1002 static_assert (FIRST_PSEUDO_REGISTER <= INT_MAX, "");
1003 if (errno != ERANGE
1004 && j < FIRST_PSEUDO_REGISTER
1005 && reg_names[j][0])
1006 return j;
1007 else
1008 return -2;
1012 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1013 if (reg_names[i][0]
1014 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
1015 return i;
1017 #ifdef OVERLAPPING_REGISTER_NAMES
1019 static const struct
1021 const char *const name;
1022 const int number;
1023 const int nregs;
1024 } table[] = OVERLAPPING_REGISTER_NAMES;
1026 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1027 if (table[i].name[0]
1028 && ! strcmp (asmspec, table[i].name))
1030 *pnregs = table[i].nregs;
1031 return table[i].number;
1034 #endif /* OVERLAPPING_REGISTER_NAMES */
1036 #ifdef ADDITIONAL_REGISTER_NAMES
1038 static const struct { const char *const name; const int number; } table[]
1039 = ADDITIONAL_REGISTER_NAMES;
1041 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1042 if (table[i].name[0]
1043 && ! strcmp (asmspec, table[i].name)
1044 && reg_names[table[i].number][0])
1045 return table[i].number;
1047 #endif /* ADDITIONAL_REGISTER_NAMES */
1049 if (!strcmp (asmspec, "redzone"))
1050 return -5;
1052 if (!strcmp (asmspec, "memory"))
1053 return -4;
1055 if (!strcmp (asmspec, "cc"))
1056 return -3;
1058 return -2;
1061 return -1;
1065 decode_reg_name (const char *name)
1067 int count;
1068 return decode_reg_name_and_count (name, &count);
1072 /* Return true if DECL's initializer is suitable for a BSS section. */
1074 bool
1075 bss_initializer_p (const_tree decl, bool named)
1077 /* Do not put non-common constants into the .bss section, they belong in
1078 a readonly section, except when NAMED is true. */
1079 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1080 && (DECL_INITIAL (decl) == NULL
1081 /* In LTO we have no errors in program; error_mark_node is used
1082 to mark offlined constructors. */
1083 || (DECL_INITIAL (decl) == error_mark_node
1084 && !in_lto_p)
1085 || (flag_zero_initialized_in_bss
1086 && initializer_zerop (DECL_INITIAL (decl))
1087 /* A decl with the "persistent" attribute applied and
1088 explicitly initialized to 0 should not be treated as a BSS
1089 variable. */
1090 && !DECL_PERSISTENT_P (decl))));
1093 /* Compute the alignment of variable specified by DECL.
1094 DONT_OUTPUT_DATA is from assemble_variable. */
1096 void
1097 align_variable (tree decl, bool dont_output_data)
1099 unsigned int align = DECL_ALIGN (decl);
1101 /* In the case for initialing an array whose length isn't specified,
1102 where we have not yet been able to do the layout,
1103 figure out the proper alignment now. */
1104 if (dont_output_data && DECL_SIZE (decl) == 0
1105 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1106 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1108 /* Some object file formats have a maximum alignment which they support.
1109 In particular, a.out format supports a maximum alignment of 4. */
1110 if (align > MAX_OFILE_ALIGNMENT)
1112 error ("alignment of %q+D is greater than maximum object "
1113 "file alignment %d", decl,
1114 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1115 align = MAX_OFILE_ALIGNMENT;
1118 if (! DECL_USER_ALIGN (decl))
1120 #ifdef DATA_ABI_ALIGNMENT
1121 unsigned int data_abi_align
1122 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1123 /* For backwards compatibility, don't assume the ABI alignment for
1124 TLS variables. */
1125 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1126 align = data_abi_align;
1127 #endif
1129 /* On some machines, it is good to increase alignment sometimes.
1130 But as DECL_ALIGN is used both for actually emitting the variable
1131 and for code accessing the variable as guaranteed alignment, we
1132 can only increase the alignment if it is a performance optimization
1133 if the references to it must bind to the current definition. */
1134 if (decl_binds_to_current_def_p (decl)
1135 && !DECL_VIRTUAL_P (decl))
1137 #ifdef DATA_ALIGNMENT
1138 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1139 /* Don't increase alignment too much for TLS variables - TLS space
1140 is too precious. */
1141 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1142 align = data_align;
1143 #endif
1144 if (DECL_INITIAL (decl) != 0
1145 /* In LTO we have no errors in program; error_mark_node is used
1146 to mark offlined constructors. */
1147 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1149 unsigned int const_align
1150 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1151 /* Don't increase alignment too much for TLS variables - TLS
1152 space is too precious. */
1153 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1154 align = const_align;
1159 /* Reset the alignment in case we have made it tighter, so we can benefit
1160 from it in get_pointer_alignment. */
1161 SET_DECL_ALIGN (decl, align);
1164 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1165 beyond what align_variable returned. */
1167 static unsigned int
1168 get_variable_align (tree decl)
1170 unsigned int align = DECL_ALIGN (decl);
1172 /* For user aligned vars or static vars align_variable already did
1173 everything. */
1174 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1175 return align;
1177 #ifdef DATA_ABI_ALIGNMENT
1178 if (DECL_THREAD_LOCAL_P (decl))
1179 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1180 #endif
1182 /* For decls that bind to the current definition, align_variable
1183 did also everything, except for not assuming ABI required alignment
1184 of TLS variables. For other vars, increase the alignment here
1185 as an optimization. */
1186 if (!decl_binds_to_current_def_p (decl))
1188 /* On some machines, it is good to increase alignment sometimes. */
1189 #ifdef DATA_ALIGNMENT
1190 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1191 /* Don't increase alignment too much for TLS variables - TLS space
1192 is too precious. */
1193 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1194 align = data_align;
1195 #endif
1196 if (DECL_INITIAL (decl) != 0
1197 /* In LTO we have no errors in program; error_mark_node is used
1198 to mark offlined constructors. */
1199 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1201 unsigned int const_align
1202 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1203 /* Don't increase alignment too much for TLS variables - TLS space
1204 is too precious. */
1205 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1206 align = const_align;
1210 return align;
1213 /* Compute reloc for get_variable_section. The return value
1214 is a mask for which bit 1 indicates a global relocation, and bit 0
1215 indicates a local relocation. */
1218 compute_reloc_for_var (tree decl)
1220 int reloc;
1222 if (DECL_INITIAL (decl) == error_mark_node)
1223 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1224 else if (DECL_INITIAL (decl))
1225 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1226 else
1227 reloc = 0;
1229 return reloc;
1232 /* Return the section into which the given VAR_DECL or CONST_DECL
1233 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1234 section should be used wherever possible. */
1236 section *
1237 get_variable_section (tree decl, bool prefer_noswitch_p)
1239 addr_space_t as = ADDR_SPACE_GENERIC;
1240 int reloc;
1241 varpool_node *vnode = varpool_node::get (decl);
1242 if (vnode)
1244 vnode = vnode->ultimate_alias_target ();
1245 decl = vnode->decl;
1248 if (TREE_TYPE (decl) != error_mark_node)
1249 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1251 /* We need the constructor to figure out reloc flag. */
1252 if (vnode)
1253 vnode->get_constructor ();
1255 if (DECL_COMMON (decl)
1256 && !lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1258 /* If the decl has been given an explicit section name, or it resides
1259 in a non-generic address space, then it isn't common, and shouldn't
1260 be handled as such. */
1261 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1262 && ADDR_SPACE_GENERIC_P (as));
1263 if (DECL_THREAD_LOCAL_P (decl))
1264 return tls_comm_section;
1265 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1266 return comm_section;
1269 reloc = compute_reloc_for_var (decl);
1271 resolve_unique_section (decl, reloc, flag_data_sections);
1272 if (IN_NAMED_SECTION (decl))
1274 section *sect = get_named_section (decl, NULL, reloc);
1276 if ((sect->common.flags & SECTION_BSS)
1277 && !bss_initializer_p (decl, true))
1279 if (flag_zero_initialized_in_bss)
1280 error_at (DECL_SOURCE_LOCATION (decl),
1281 "only zero initializers are allowed in section %qs",
1282 sect->named.name);
1283 else
1284 error_at (DECL_SOURCE_LOCATION (decl),
1285 "no initializers are allowed in section %qs",
1286 sect->named.name);
1287 DECL_INITIAL (decl) = error_mark_node;
1289 return sect;
1292 if (ADDR_SPACE_GENERIC_P (as)
1293 && !DECL_THREAD_LOCAL_P (decl)
1294 && !DECL_NOINIT_P (decl)
1295 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1296 && bss_initializer_p (decl))
1298 if (!TREE_PUBLIC (decl)
1299 && !((flag_sanitize & SANITIZE_ADDRESS)
1300 && asan_protect_global (decl)))
1301 return lcomm_section;
1302 if (bss_noswitch_section)
1303 return bss_noswitch_section;
1306 return targetm.asm_out.select_section (decl, reloc,
1307 get_variable_align (decl));
1310 /* Return the block into which object_block DECL should be placed. */
1312 static struct object_block *
1313 get_block_for_decl (tree decl)
1315 section *sect;
1317 if (VAR_P (decl))
1319 /* The object must be defined in this translation unit. */
1320 if (DECL_EXTERNAL (decl))
1321 return NULL;
1323 /* There's no point using object blocks for something that is
1324 isolated by definition. */
1325 if (DECL_COMDAT_GROUP (decl))
1326 return NULL;
1329 /* We can only calculate block offsets if the decl has a known
1330 constant size. */
1331 if (DECL_SIZE_UNIT (decl) == NULL)
1332 return NULL;
1333 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1334 return NULL;
1336 /* Find out which section should contain DECL. We cannot put it into
1337 an object block if it requires a standalone definition. */
1338 if (VAR_P (decl))
1339 align_variable (decl, 0);
1340 sect = get_variable_section (decl, true);
1341 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1342 return NULL;
1344 if (bool (lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1345 != bool (sect->common.flags & SECTION_RETAIN))
1346 return NULL;
1348 return get_block_for_section (sect);
1351 /* Make sure block symbol SYMBOL is in block BLOCK. */
1353 static void
1354 change_symbol_block (rtx symbol, struct object_block *block)
1356 if (block != SYMBOL_REF_BLOCK (symbol))
1358 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1359 SYMBOL_REF_BLOCK (symbol) = block;
1363 /* Return true if it is possible to put DECL in an object_block. */
1365 static bool
1366 use_blocks_for_decl_p (tree decl)
1368 struct symtab_node *snode;
1370 /* Don't create object blocks if each DECL is placed into a separate
1371 section because that will uselessly create a section anchor for
1372 each DECL. */
1373 if (flag_data_sections)
1374 return false;
1376 /* Only data DECLs can be placed into object blocks. */
1377 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1378 return false;
1380 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1381 are never used from code directly and we never want object block handling
1382 for those. */
1383 if (DECL_INITIAL (decl) == decl)
1384 return false;
1386 /* If this decl is an alias, then we don't want to emit a
1387 definition. */
1388 if (VAR_P (decl)
1389 && (snode = symtab_node::get (decl)) != NULL
1390 && snode->alias)
1391 return false;
1393 return targetm.use_blocks_for_decl_p (decl);
1396 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1397 until we find an identifier that is not itself a transparent alias.
1398 Modify the alias passed to it by reference (and all aliases on the
1399 way to the ultimate target), such that they do not have to be
1400 followed again, and return the ultimate target of the alias
1401 chain. */
1403 static inline tree
1404 ultimate_transparent_alias_target (tree *alias)
1406 tree target = *alias;
1408 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1410 gcc_assert (TREE_CHAIN (target));
1411 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1412 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1413 && ! TREE_CHAIN (target));
1414 *alias = target;
1417 return target;
1420 /* Return true if REGNUM is mentioned in ELIMINABLE_REGS as a from
1421 register number. */
1423 static bool
1424 eliminable_regno_p (int regnum)
1426 static const struct
1428 const int from;
1429 const int to;
1430 } eliminables[] = ELIMINABLE_REGS;
1431 for (size_t i = 0; i < ARRAY_SIZE (eliminables); i++)
1432 if (regnum == eliminables[i].from)
1433 return true;
1434 return false;
1437 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1438 have static storage duration. In other words, it should not be an
1439 automatic variable, including PARM_DECLs.
1441 There is, however, one exception: this function handles variables
1442 explicitly placed in a particular register by the user.
1444 This is never called for PARM_DECL nodes. */
1446 void
1447 make_decl_rtl (tree decl)
1449 const char *name = 0;
1450 int reg_number;
1451 tree id;
1452 rtx x;
1454 /* Check that we are not being given an automatic variable. */
1455 gcc_assert (TREE_CODE (decl) != PARM_DECL
1456 && TREE_CODE (decl) != RESULT_DECL);
1458 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1459 gcc_assert (!VAR_P (decl)
1460 || TREE_STATIC (decl)
1461 || TREE_PUBLIC (decl)
1462 || DECL_EXTERNAL (decl)
1463 || DECL_REGISTER (decl));
1465 /* And that we were not given a type or a label. */
1466 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1467 && TREE_CODE (decl) != LABEL_DECL);
1469 /* For a duplicate declaration, we can be called twice on the
1470 same DECL node. Don't discard the RTL already made. */
1471 if (DECL_RTL_SET_P (decl))
1473 /* If the old RTL had the wrong mode, fix the mode. */
1474 x = DECL_RTL (decl);
1475 if (GET_MODE (x) != DECL_MODE (decl))
1476 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1478 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1479 return;
1481 /* ??? Another way to do this would be to maintain a hashed
1482 table of such critters. Instead of adding stuff to a DECL
1483 to give certain attributes to it, we could use an external
1484 hash map from DECL to set of attributes. */
1486 /* Let the target reassign the RTL if it wants.
1487 This is necessary, for example, when one machine specific
1488 decl attribute overrides another. */
1489 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1491 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1492 on the new decl information. */
1493 if (MEM_P (x)
1494 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1495 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1496 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1498 return;
1501 /* If this variable belongs to the global constant pool, retrieve the
1502 pre-computed RTL or recompute it in LTO mode. */
1503 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1505 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1506 return;
1509 id = DECL_ASSEMBLER_NAME (decl);
1510 name = IDENTIFIER_POINTER (id);
1512 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1513 && DECL_REGISTER (decl))
1515 error ("register name not specified for %q+D", decl);
1517 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1519 const char *asmspec = name+1;
1520 machine_mode mode = DECL_MODE (decl);
1521 reg_number = decode_reg_name (asmspec);
1522 /* First detect errors in declaring global registers. */
1523 if (reg_number == -1)
1524 error ("register name not specified for %q+D", decl);
1525 else if (reg_number < 0)
1526 error ("invalid register name for %q+D", decl);
1527 else if (mode == BLKmode)
1528 error ("data type of %q+D isn%'t suitable for a register",
1529 decl);
1530 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1531 error ("the register specified for %q+D cannot be accessed"
1532 " by the current target", decl);
1533 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1534 error ("the register specified for %q+D is not general enough"
1535 " to be used as a register variable", decl);
1536 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1537 error ("register specified for %q+D isn%'t suitable for data type",
1538 decl);
1539 else if (reg_number != HARD_FRAME_POINTER_REGNUM
1540 && (reg_number == FRAME_POINTER_REGNUM
1541 #ifdef RETURN_ADDRESS_POINTER_REGNUM
1542 || reg_number == RETURN_ADDRESS_POINTER_REGNUM
1543 #endif
1544 || reg_number == ARG_POINTER_REGNUM)
1545 && eliminable_regno_p (reg_number))
1546 error ("register specified for %q+D is an internal GCC "
1547 "implementation detail", decl);
1548 /* Now handle properly declared static register variables. */
1549 else
1551 int nregs;
1553 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1555 DECL_INITIAL (decl) = 0;
1556 error ("global register variable has initial value");
1558 if (TREE_THIS_VOLATILE (decl))
1559 warning (OPT_Wvolatile_register_var,
1560 "optimization may eliminate reads and/or "
1561 "writes to register variables");
1563 /* If the user specified one of the eliminables registers here,
1564 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1565 confused with that register and be eliminated. This usage is
1566 somewhat suspect... */
1568 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1569 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1570 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1572 if (TREE_STATIC (decl))
1574 /* Make this register global, so not usable for anything
1575 else. */
1576 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1577 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1578 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1579 #endif
1580 nregs = hard_regno_nregs (reg_number, mode);
1581 while (nregs > 0)
1582 globalize_reg (decl, reg_number + --nregs);
1585 /* As a register variable, it has no section. */
1586 return;
1588 /* Avoid internal errors from invalid register
1589 specifications. */
1590 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1591 DECL_HARD_REGISTER (decl) = 0;
1592 /* Also avoid SSA inconsistencies by pretending this is an external
1593 decl now. */
1594 DECL_EXTERNAL (decl) = 1;
1595 return;
1597 /* Now handle ordinary static variables and functions (in memory).
1598 Also handle vars declared register invalidly. */
1599 else if (name[0] == '*')
1601 #ifdef REGISTER_PREFIX
1602 if (strlen (REGISTER_PREFIX) != 0)
1604 reg_number = decode_reg_name (name);
1605 if (reg_number >= 0 || reg_number == -3)
1606 error ("register name given for non-register variable %q+D", decl);
1608 #endif
1611 /* Specifying a section attribute on a variable forces it into a
1612 non-.bss section, and thus it cannot be common. */
1613 /* FIXME: In general this code should not be necessary because
1614 visibility pass is doing the same work. But notice_global_symbol
1615 is called early and it needs to make DECL_RTL to get the name.
1616 we take care of recomputing the DECL_RTL after visibility is changed. */
1617 if (VAR_P (decl)
1618 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1619 && DECL_SECTION_NAME (decl) != NULL
1620 && DECL_INITIAL (decl) == NULL_TREE
1621 && DECL_COMMON (decl))
1622 DECL_COMMON (decl) = 0;
1624 /* Variables can't be both common and weak. */
1625 if (VAR_P (decl) && DECL_WEAK (decl))
1626 DECL_COMMON (decl) = 0;
1628 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1629 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1630 else
1632 machine_mode address_mode = Pmode;
1633 if (TREE_TYPE (decl) != error_mark_node)
1635 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1636 address_mode = targetm.addr_space.address_mode (as);
1638 x = gen_rtx_SYMBOL_REF (address_mode, name);
1640 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1641 SET_SYMBOL_REF_DECL (x, decl);
1643 x = gen_rtx_MEM (DECL_MODE (decl), x);
1644 if (TREE_CODE (decl) != FUNCTION_DECL)
1645 set_mem_attributes (x, decl, 1);
1646 SET_DECL_RTL (decl, x);
1648 /* Optionally set flags or add text to the name to record information
1649 such as that it is a function name.
1650 If the name is changed, the macro ASM_OUTPUT_LABELREF
1651 will have to know how to strip this information. */
1652 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1655 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1656 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1657 rtl. */
1660 make_decl_rtl_for_debug (tree decl)
1662 unsigned int save_aliasing_flag;
1663 rtx rtl;
1665 if (DECL_RTL_SET_P (decl))
1666 return DECL_RTL (decl);
1668 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1669 call new_alias_set. If running with -fcompare-debug, sometimes
1670 we do not want to create alias sets that will throw the alias
1671 numbers off in the comparison dumps. So... clearing
1672 flag_strict_aliasing will keep new_alias_set() from creating a
1673 new set. */
1674 save_aliasing_flag = flag_strict_aliasing;
1675 flag_strict_aliasing = 0;
1677 rtl = DECL_RTL (decl);
1678 /* Reset DECL_RTL back, as various parts of the compiler expects
1679 DECL_RTL set meaning it is actually going to be output. */
1680 SET_DECL_RTL (decl, NULL);
1682 flag_strict_aliasing = save_aliasing_flag;
1683 return rtl;
1686 /* Output a string of literal assembler code
1687 for an `asm' keyword used between functions. */
1689 void
1690 assemble_asm (tree asm_str)
1692 const char *p;
1694 if (TREE_CODE (asm_str) != ASM_EXPR)
1696 app_enable ();
1697 if (TREE_CODE (asm_str) == ADDR_EXPR)
1698 asm_str = TREE_OPERAND (asm_str, 0);
1700 p = TREE_STRING_POINTER (asm_str);
1701 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1703 else
1705 location_t save_loc = input_location;
1706 int save_reload_completed = reload_completed;
1707 int save_cse_not_expected = cse_not_expected;
1708 input_location = EXPR_LOCATION (asm_str);
1709 int noutputs = list_length (ASM_OUTPUTS (asm_str));
1710 int ninputs = list_length (ASM_INPUTS (asm_str));
1711 const char **constraints = NULL;
1712 int i;
1713 tree tail;
1714 bool allows_mem, allows_reg, is_inout;
1715 rtx *ops = NULL;
1716 if (noutputs + ninputs > MAX_RECOG_OPERANDS)
1718 error ("more than %d operands in %<asm%>", MAX_RECOG_OPERANDS);
1719 goto done;
1721 constraints = XALLOCAVEC (const char *, noutputs + ninputs);
1722 ops = XALLOCAVEC (rtx, noutputs + ninputs);
1723 memset (&recog_data, 0, sizeof (recog_data));
1724 recog_data.n_operands = ninputs + noutputs;
1725 recog_data.is_asm = true;
1726 reload_completed = 0;
1727 cse_not_expected = 1;
1728 for (i = 0, tail = ASM_OUTPUTS (asm_str); tail;
1729 ++i, tail = TREE_CHAIN (tail))
1731 tree output = TREE_VALUE (tail);
1732 if (output == error_mark_node)
1733 goto done;
1734 constraints[i]
1735 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
1736 if (!parse_output_constraint (&constraints[i], i, ninputs, noutputs,
1737 &allows_mem, &allows_reg, &is_inout))
1738 goto done;
1739 if (is_inout)
1741 error ("%qc in output operand outside of a function", '+');
1742 goto done;
1744 if (strchr (constraints[i], '&'))
1746 error ("%qc in output operand outside of a function", '&');
1747 goto done;
1749 if (strchr (constraints[i], '%'))
1751 error ("%qc in output operand outside of a function", '%');
1752 goto done;
1754 output_addressed_constants (output, 0);
1755 if (!is_gimple_addressable (output))
1757 error ("output number %d not directly addressable", i);
1758 goto done;
1760 ops[i] = expand_expr (build_fold_addr_expr (output), NULL_RTX,
1761 VOIDmode, EXPAND_INITIALIZER);
1762 ops[i] = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (output)), ops[i]);
1764 recog_data.operand[i] = ops[i];
1765 recog_data.operand_loc[i] = &ops[i];
1766 recog_data.constraints[i] = constraints[i];
1767 recog_data.operand_mode[i] = TYPE_MODE (TREE_TYPE (output));
1769 for (i = 0, tail = ASM_INPUTS (asm_str); tail;
1770 ++i, tail = TREE_CHAIN (tail))
1772 tree input = TREE_VALUE (tail);
1773 if (input == error_mark_node)
1774 goto done;
1775 constraints[i + noutputs]
1776 = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (tail)));
1777 if (!parse_input_constraint (&constraints[i + noutputs], i,
1778 ninputs, noutputs, 0, constraints,
1779 &allows_mem, &allows_reg))
1780 goto done;
1781 if (strchr (constraints[i], '%'))
1783 error ("%qc in input operand outside of a function", '%');
1784 goto done;
1786 const char *constraint = constraints[i + noutputs];
1787 size_t c_len = strlen (constraint);
1788 for (size_t j = 0; j < c_len;
1789 j += CONSTRAINT_LEN (constraint[j], constraint + j))
1790 if (constraint[j] >= '0' && constraint[j] <= '9')
1792 error ("matching constraint outside of a function");
1793 goto done;
1795 output_addressed_constants (input, 0);
1796 if (allows_mem && is_gimple_addressable (input))
1798 ops[i + noutputs]
1799 = expand_expr (build_fold_addr_expr (input), NULL_RTX,
1800 VOIDmode, EXPAND_INITIALIZER);
1801 ops[i + noutputs] = gen_rtx_MEM (TYPE_MODE (TREE_TYPE (input)),
1802 ops[i + noutputs]);
1804 else
1805 ops[i + noutputs] = expand_expr (input, NULL_RTX, VOIDmode,
1806 EXPAND_INITIALIZER);
1807 if (asm_operand_ok (ops[i + noutputs], constraint, NULL) <= 0)
1809 if (!allows_mem)
1810 warning (0, "%<asm%> operand %d probably does not "
1811 "match constraints", i + noutputs);
1813 recog_data.operand[i + noutputs] = ops[i + noutputs];
1814 recog_data.operand_loc[i + noutputs] = &ops[i + noutputs];
1815 recog_data.constraints[i + noutputs] = constraints[i + noutputs];
1816 recog_data.operand_mode[i + noutputs]
1817 = TYPE_MODE (TREE_TYPE (input));
1819 if (recog_data.n_operands > 0)
1821 const char *p = recog_data.constraints[0];
1822 recog_data.n_alternatives = 1;
1823 while (*p)
1824 recog_data.n_alternatives += (*p++ == ',');
1826 for (i = 0; i < recog_data.n_operands; i++)
1827 recog_data.operand_type[i]
1828 = recog_data.constraints[i][0] == '=' ? OP_OUT : OP_IN;
1829 reload_completed = 1;
1830 constrain_operands (1, ALL_ALTERNATIVES);
1831 if (which_alternative < 0)
1833 error ("impossible constraint in %<asm%>");
1834 goto done;
1836 this_is_asm_operands = make_insn_raw (gen_nop ());
1837 insn_noperands = recog_data.n_operands;
1838 if (TREE_STRING_POINTER (ASM_STRING (asm_str))[0])
1840 app_enable ();
1841 output_asm_insn (TREE_STRING_POINTER (ASM_STRING (asm_str)), ops);
1843 insn_noperands = 0;
1844 this_is_asm_operands = NULL;
1846 done:
1847 input_location = save_loc;
1848 reload_completed = save_reload_completed;
1849 cse_not_expected = save_cse_not_expected;
1853 /* Write the address of the entity given by SYMBOL to SEC. */
1854 void
1855 assemble_addr_to_section (rtx symbol, section *sec)
1857 switch_to_section (sec);
1858 assemble_align (POINTER_SIZE);
1859 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1862 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1863 not) section for PRIORITY. */
1864 section *
1865 get_cdtor_priority_section (int priority, bool constructor_p)
1867 /* Buffer conservatively large enough for the full range of a 32-bit
1868 int plus the text below. */
1869 char buf[18];
1871 /* ??? This only works reliably with the GNU linker. */
1872 sprintf (buf, "%s.%.5u",
1873 constructor_p ? ".ctors" : ".dtors",
1874 /* Invert the numbering so the linker puts us in the proper
1875 order; constructors are run from right to left, and the
1876 linker sorts in increasing order. */
1877 MAX_INIT_PRIORITY - priority);
1878 return get_section (buf, SECTION_WRITE, NULL);
1881 void
1882 default_named_section_asm_out_destructor (rtx symbol, int priority)
1884 section *sec;
1886 if (priority != DEFAULT_INIT_PRIORITY)
1887 sec = get_cdtor_priority_section (priority,
1888 /*constructor_p=*/false);
1889 else
1890 sec = get_section (".dtors", SECTION_WRITE, NULL);
1892 assemble_addr_to_section (symbol, sec);
1895 #ifdef DTORS_SECTION_ASM_OP
1896 void
1897 default_dtor_section_asm_out_destructor (rtx symbol,
1898 int priority ATTRIBUTE_UNUSED)
1900 assemble_addr_to_section (symbol, dtors_section);
1902 #endif
1904 void
1905 default_named_section_asm_out_constructor (rtx symbol, int priority)
1907 section *sec;
1909 if (priority != DEFAULT_INIT_PRIORITY)
1910 sec = get_cdtor_priority_section (priority,
1911 /*constructor_p=*/true);
1912 else
1913 sec = get_section (".ctors", SECTION_WRITE, NULL);
1915 assemble_addr_to_section (symbol, sec);
1918 #ifdef CTORS_SECTION_ASM_OP
1919 void
1920 default_ctor_section_asm_out_constructor (rtx symbol,
1921 int priority ATTRIBUTE_UNUSED)
1923 assemble_addr_to_section (symbol, ctors_section);
1925 #endif
1927 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1928 a nonzero value if the constant pool should be output before the
1929 start of the function, or a zero value if the pool should output
1930 after the end of the function. The default is to put it before the
1931 start. */
1933 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1934 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1935 #endif
1937 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1938 to be output to assembler.
1939 Set first_global_object_name and weak_global_object_name as appropriate. */
1941 void
1942 notice_global_symbol (tree decl)
1944 const char **t = &first_global_object_name;
1946 if (first_global_object_name
1947 || !TREE_PUBLIC (decl)
1948 || DECL_EXTERNAL (decl)
1949 || !DECL_NAME (decl)
1950 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1951 || (TREE_CODE (decl) != FUNCTION_DECL
1952 && (!VAR_P (decl)
1953 || (DECL_COMMON (decl)
1954 && (DECL_INITIAL (decl) == 0
1955 || DECL_INITIAL (decl) == error_mark_node)))))
1956 return;
1958 /* We win when global object is found, but it is useful to know about weak
1959 symbol as well so we can produce nicer unique names. */
1960 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1961 t = &weak_global_object_name;
1963 if (!*t)
1965 tree id = DECL_ASSEMBLER_NAME (decl);
1966 ultimate_transparent_alias_target (&id);
1967 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1971 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1972 current function goes into the cold section, so that targets can use
1973 current_function_section during RTL expansion. DECL describes the
1974 function. */
1976 void
1977 decide_function_section (tree decl)
1979 first_function_block_is_cold = false;
1981 if (DECL_SECTION_NAME (decl))
1983 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1984 /* Calls to function_section rely on first_function_block_is_cold
1985 being accurate. */
1986 first_function_block_is_cold = (node
1987 && node->frequency
1988 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1991 in_cold_section_p = first_function_block_is_cold;
1994 /* Get the function's name, as described by its RTL. This may be
1995 different from the DECL_NAME name used in the source file. */
1996 const char *
1997 get_fnname_from_decl (tree decl)
1999 rtx x = DECL_RTL (decl);
2000 gcc_assert (MEM_P (x));
2001 x = XEXP (x, 0);
2002 gcc_assert (GET_CODE (x) == SYMBOL_REF);
2003 return XSTR (x, 0);
2006 /* Output function label, possibly with accompanying metadata. No additional
2007 code or data is output after the label. */
2009 void
2010 assemble_function_label_raw (FILE *file, const char *name)
2012 ASM_OUTPUT_LABEL (file, name);
2013 assemble_function_label_final ();
2016 /* Finish outputting function label. Needs to be called when outputting
2017 function label without using assemble_function_label_raw (). */
2019 void
2020 assemble_function_label_final (void)
2022 if ((flag_sanitize & SANITIZE_ADDRESS)
2023 /* Notify ASAN only about the first function label. */
2024 && (in_cold_section_p == first_function_block_is_cold)
2025 /* Do not notify ASAN when called from, e.g., code_end (). */
2026 && cfun)
2027 asan_function_start ();
2030 /* Output assembler code for the constant pool of a function and associated
2031 with defining the name of the function. DECL describes the function.
2032 NAME is the function's name. For the constant pool, we use the current
2033 constant pool data. */
2035 void
2036 assemble_start_function (tree decl, const char *fnname)
2038 int align;
2039 char tmp_label[100];
2040 bool hot_label_written = false;
2042 if (crtl->has_bb_partition)
2044 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
2045 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
2046 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
2047 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
2048 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
2049 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
2050 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
2051 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
2052 const_labelno++;
2053 cold_function_name = NULL_TREE;
2055 else
2057 crtl->subsections.hot_section_label = NULL;
2058 crtl->subsections.cold_section_label = NULL;
2059 crtl->subsections.hot_section_end_label = NULL;
2060 crtl->subsections.cold_section_end_label = NULL;
2063 /* The following code does not need preprocessing in the assembler. */
2065 app_disable ();
2067 if (CONSTANT_POOL_BEFORE_FUNCTION)
2068 output_constant_pool (fnname, decl);
2070 align = symtab_node::get (decl)->definition_alignment ();
2072 /* Make sure the not and cold text (code) sections are properly
2073 aligned. This is necessary here in the case where the function
2074 has both hot and cold sections, because we don't want to re-set
2075 the alignment when the section switch happens mid-function. */
2077 if (crtl->has_bb_partition)
2079 first_function_block_is_cold = false;
2081 switch_to_section (unlikely_text_section ());
2082 assemble_align (align);
2083 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
2085 /* When the function starts with a cold section, we need to explicitly
2086 align the hot section and write out the hot section label.
2087 But if the current function is a thunk, we do not have a CFG. */
2088 if (!cfun->is_thunk
2089 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
2091 switch_to_section (text_section);
2092 assemble_align (align);
2093 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
2094 hot_label_written = true;
2095 first_function_block_is_cold = true;
2097 in_cold_section_p = first_function_block_is_cold;
2101 /* Switch to the correct text section for the start of the function. */
2103 switch_to_section (function_section (decl), decl);
2104 if (crtl->has_bb_partition && !hot_label_written)
2105 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
2107 /* Tell assembler to move to target machine's alignment for functions. */
2108 align = floor_log2 (align / BITS_PER_UNIT);
2109 /* Handle forced alignment. This really ought to apply to all functions,
2110 since it is used by patchable entries. */
2111 if (flag_min_function_alignment)
2112 align = MAX (align, floor_log2 (flag_min_function_alignment));
2114 if (align > 0)
2116 ASM_OUTPUT_ALIGN (asm_out_file, align);
2119 /* Handle a user-specified function alignment.
2120 Note that we still need to align to DECL_ALIGN, as above,
2121 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
2122 if (! DECL_USER_ALIGN (decl)
2123 && align_functions.levels[0].log > align
2124 && optimize_function_for_speed_p (cfun))
2126 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
2127 int align_log = align_functions.levels[0].log;
2128 #endif
2129 int max_skip = align_functions.levels[0].maxskip;
2130 if (flag_limit_function_alignment && crtl->max_insn_address > 0
2131 && max_skip >= crtl->max_insn_address)
2132 max_skip = crtl->max_insn_address - 1;
2134 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
2135 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
2136 if (max_skip == align_functions.levels[0].maxskip)
2137 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
2138 align_functions.levels[1].log,
2139 align_functions.levels[1].maxskip);
2140 #else
2141 ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
2142 #endif
2145 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
2146 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
2147 #endif
2149 if (!DECL_IGNORED_P (decl))
2150 (*debug_hooks->begin_function) (decl);
2152 /* Make function name accessible from other files, if appropriate. */
2154 if (TREE_PUBLIC (decl))
2156 notice_global_symbol (decl);
2158 globalize_decl (decl);
2160 maybe_assemble_visibility (decl);
2163 if (DECL_PRESERVE_P (decl))
2164 targetm.asm_out.mark_decl_preserved (fnname);
2166 unsigned short patch_area_size = crtl->patch_area_size;
2167 unsigned short patch_area_entry = crtl->patch_area_entry;
2169 /* Emit the patching area before the entry label, if any. */
2170 if (patch_area_entry > 0)
2171 targetm.asm_out.print_patchable_function_entry (asm_out_file,
2172 patch_area_entry, true);
2174 /* Do any machine/system dependent processing of the function name. */
2175 #ifdef ASM_DECLARE_FUNCTION_NAME
2176 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
2177 #else
2178 /* Standard thing is just output label for the function. */
2179 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
2180 #endif /* ASM_DECLARE_FUNCTION_NAME */
2182 /* And the area after the label. Record it if we haven't done so yet. */
2183 if (patch_area_size > patch_area_entry)
2184 targetm.asm_out.print_patchable_function_entry (asm_out_file,
2185 patch_area_size
2186 - patch_area_entry,
2187 patch_area_entry == 0);
2189 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
2190 saw_no_split_stack = true;
2193 /* Output assembler code associated with defining the size of the
2194 function. DECL describes the function. NAME is the function's name. */
2196 void
2197 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
2199 #ifdef ASM_DECLARE_FUNCTION_SIZE
2200 /* We could have switched section in the middle of the function. */
2201 if (crtl->has_bb_partition)
2202 switch_to_section (function_section (decl));
2203 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
2204 #endif
2205 if (! CONSTANT_POOL_BEFORE_FUNCTION)
2207 output_constant_pool (fnname, decl);
2208 switch_to_section (function_section (decl)); /* need to switch back */
2210 /* Output labels for end of hot/cold text sections (to be used by
2211 debug info.) */
2212 if (crtl->has_bb_partition)
2214 section *save_text_section;
2216 save_text_section = in_section;
2217 switch_to_section (unlikely_text_section ());
2218 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
2219 if (cold_function_name != NULL_TREE)
2220 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
2221 IDENTIFIER_POINTER (cold_function_name),
2222 decl);
2223 #endif
2224 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
2225 if (first_function_block_is_cold)
2226 switch_to_section (text_section);
2227 else
2228 switch_to_section (function_section (decl));
2229 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
2230 switch_to_section (save_text_section);
2234 /* Assemble code to leave SIZE bytes of zeros. */
2236 void
2237 assemble_zeros (unsigned HOST_WIDE_INT size)
2239 /* Do no output if -fsyntax-only. */
2240 if (flag_syntax_only)
2241 return;
2243 #ifdef ASM_NO_SKIP_IN_TEXT
2244 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
2245 so we must output 0s explicitly in the text section. */
2246 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
2248 unsigned HOST_WIDE_INT i;
2249 for (i = 0; i < size; i++)
2250 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
2252 else
2253 #endif
2254 if (size > 0)
2255 ASM_OUTPUT_SKIP (asm_out_file, size);
2258 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
2260 void
2261 assemble_align (unsigned int align)
2263 if (align > BITS_PER_UNIT)
2265 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2269 /* Assemble a string constant with the specified C string as contents. */
2271 void
2272 assemble_string (const char *p, int size)
2274 int pos = 0;
2275 #if defined(BASE64_ASM_OP) \
2276 && BITS_PER_UNIT == 8 \
2277 && CHAR_BIT == 8 \
2278 && 'A' == 65 \
2279 && 'a' == 97 \
2280 && '0' == 48 \
2281 && '+' == 43 \
2282 && '/' == 47 \
2283 && '=' == 61
2284 int maximum = 16384;
2285 #else
2286 int maximum = 2000;
2287 #endif
2289 /* If the string is very long, split it up. */
2291 while (pos < size)
2293 int thissize = size - pos;
2294 if (thissize > maximum)
2295 thissize = maximum;
2297 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
2299 pos += thissize;
2300 p += thissize;
2305 /* A noswitch_section_callback for lcomm_section. */
2307 static bool
2308 emit_local (tree decl ATTRIBUTE_UNUSED,
2309 const char *name ATTRIBUTE_UNUSED,
2310 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2311 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2313 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2314 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2315 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2316 size, align);
2317 return true;
2318 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2319 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2320 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2321 return true;
2322 #else
2323 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2324 return false;
2325 #endif
2328 /* A noswitch_section_callback for bss_noswitch_section. */
2330 #if defined ASM_OUTPUT_ALIGNED_BSS
2331 static bool
2332 emit_bss (tree decl ATTRIBUTE_UNUSED,
2333 const char *name ATTRIBUTE_UNUSED,
2334 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2335 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2337 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2338 get_variable_align (decl));
2339 return true;
2341 #endif
2343 /* A noswitch_section_callback for comm_section. */
2345 static bool
2346 emit_common (tree decl ATTRIBUTE_UNUSED,
2347 const char *name ATTRIBUTE_UNUSED,
2348 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2349 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2351 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2352 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2353 size, get_variable_align (decl));
2354 return true;
2355 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2356 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2357 get_variable_align (decl));
2358 return true;
2359 #else
2360 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2361 return false;
2362 #endif
2365 /* A noswitch_section_callback for tls_comm_section. */
2367 static bool
2368 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2369 const char *name ATTRIBUTE_UNUSED,
2370 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2371 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2373 #ifdef ASM_OUTPUT_TLS_COMMON
2374 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2375 return true;
2376 #else
2377 sorry ("thread-local COMMON data not implemented");
2378 return true;
2379 #endif
2382 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2383 NAME is the name of DECL's SYMBOL_REF. */
2385 static void
2386 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2387 unsigned int align)
2389 unsigned HOST_WIDE_INT size, rounded;
2391 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2392 rounded = size;
2394 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2395 size += asan_red_zone_size (size);
2397 /* Don't allocate zero bytes of common,
2398 since that means "undefined external" in the linker. */
2399 if (size == 0)
2400 rounded = 1;
2402 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2403 so that each uninitialized object starts on such a boundary. */
2404 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2405 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2406 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2408 if (!sect->noswitch.callback (decl, name, size, rounded)
2409 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2410 error ("requested alignment for %q+D is greater than "
2411 "implemented alignment of %wu", decl, rounded);
2414 /* A subroutine of assemble_variable. Output the label and contents of
2415 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2416 is as for assemble_variable. */
2418 static void
2419 assemble_variable_contents (tree decl, const char *name,
2420 bool dont_output_data, bool merge_strings)
2422 /* Do any machine/system dependent processing of the object. */
2423 #ifdef ASM_DECLARE_OBJECT_NAME
2424 last_assemble_variable_decl = decl;
2425 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2426 #else
2427 /* Standard thing is just output label for the object. */
2428 ASM_OUTPUT_LABEL (asm_out_file, name);
2429 #endif /* ASM_DECLARE_OBJECT_NAME */
2431 if (!dont_output_data)
2433 /* Caller is supposed to use varpool_get_constructor when it wants
2434 to output the body. */
2435 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2436 if (DECL_INITIAL (decl)
2437 && DECL_INITIAL (decl) != error_mark_node
2438 && !initializer_zerop (DECL_INITIAL (decl)))
2439 /* Output the actual data. */
2440 output_constant (DECL_INITIAL (decl),
2441 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2442 get_variable_align (decl),
2443 false, merge_strings);
2444 else
2445 /* Leave space for it. */
2446 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2447 targetm.asm_out.decl_end ();
2451 /* Write out assembly for the variable DECL, which is not defined in
2452 the current translation unit. */
2453 void
2454 assemble_undefined_decl (tree decl)
2456 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2457 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2460 /* Assemble everything that is needed for a variable or function declaration.
2461 Not used for automatic variables, and not used for function definitions.
2462 Should not be called for variables of incomplete structure type.
2464 TOP_LEVEL is nonzero if this variable has file scope.
2465 AT_END is nonzero if this is the special handling, at end of compilation,
2466 to define things that have had only tentative definitions.
2467 DONT_OUTPUT_DATA if nonzero means don't actually output the
2468 initial value (that will be done by the caller). */
2470 void
2471 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2472 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2474 const char *name;
2475 rtx decl_rtl, symbol;
2476 section *sect;
2477 unsigned int align;
2478 bool asan_protected = false;
2480 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2481 gcc_assert (VAR_P (decl));
2483 /* Emulated TLS had better not get this far. */
2484 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2486 last_assemble_variable_decl = 0;
2488 /* Normally no need to say anything here for external references,
2489 since assemble_external is called by the language-specific code
2490 when a declaration is first seen. */
2492 if (DECL_EXTERNAL (decl))
2493 return;
2495 /* Do nothing for global register variables. */
2496 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2498 TREE_ASM_WRITTEN (decl) = 1;
2499 return;
2502 /* If type was incomplete when the variable was declared,
2503 see if it is complete now. */
2505 if (DECL_SIZE (decl) == 0)
2506 layout_decl (decl, 0);
2508 /* Still incomplete => don't allocate it; treat the tentative defn
2509 (which is what it must have been) as an `extern' reference. */
2511 if (!dont_output_data && DECL_SIZE (decl) == 0)
2513 error ("storage size of %q+D isn%'t known", decl);
2514 TREE_ASM_WRITTEN (decl) = 1;
2515 return;
2518 /* The first declaration of a variable that comes through this function
2519 decides whether it is global (in C, has external linkage)
2520 or local (in C, has internal linkage). So do nothing more
2521 if this function has already run. */
2523 if (TREE_ASM_WRITTEN (decl))
2524 return;
2526 /* Make sure targetm.encode_section_info is invoked before we set
2527 ASM_WRITTEN. */
2528 decl_rtl = DECL_RTL (decl);
2530 TREE_ASM_WRITTEN (decl) = 1;
2532 /* Do no output if -fsyntax-only. */
2533 if (flag_syntax_only)
2534 return;
2536 if (! dont_output_data
2537 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2539 error ("size of variable %q+D is too large", decl);
2540 return;
2543 gcc_assert (MEM_P (decl_rtl));
2544 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2545 symbol = XEXP (decl_rtl, 0);
2547 /* If this symbol belongs to the tree constant pool, output the constant
2548 if it hasn't already been written. */
2549 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2551 tree decl = SYMBOL_REF_DECL (symbol);
2552 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2553 output_constant_def_contents (symbol);
2554 return;
2557 app_disable ();
2559 name = XSTR (symbol, 0);
2560 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2561 notice_global_symbol (decl);
2563 /* Compute the alignment of this data. */
2565 align_variable (decl, dont_output_data);
2567 if ((flag_sanitize & SANITIZE_ADDRESS)
2568 && asan_protect_global (decl))
2570 asan_protected = true;
2571 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2572 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2575 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2577 align = get_variable_align (decl);
2579 if (TREE_PUBLIC (decl))
2580 maybe_assemble_visibility (decl);
2582 if (DECL_PRESERVE_P (decl))
2583 targetm.asm_out.mark_decl_preserved (name);
2585 /* First make the assembler name(s) global if appropriate. */
2586 sect = get_variable_section (decl, false);
2587 if (TREE_PUBLIC (decl)
2588 && (sect->common.flags & SECTION_COMMON) == 0)
2589 globalize_decl (decl);
2591 /* Output any data that we will need to use the address of. */
2592 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2593 output_addressed_constants (DECL_INITIAL (decl), 0);
2595 /* dbxout.cc needs to know this. */
2596 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2597 DECL_IN_TEXT_SECTION (decl) = 1;
2599 /* If the decl is part of an object_block, make sure that the decl
2600 has been positioned within its block, but do not write out its
2601 definition yet. output_object_blocks will do that later. */
2602 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2604 gcc_assert (!dont_output_data);
2605 place_block_symbol (symbol);
2607 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2608 assemble_noswitch_variable (decl, name, sect, align);
2609 else
2611 /* Special-case handling of vtv comdat sections. */
2612 if (SECTION_STYLE (sect) == SECTION_NAMED
2613 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2614 handle_vtv_comdat_section (sect, decl);
2615 else
2616 switch_to_section (sect, decl);
2617 if (align > BITS_PER_UNIT)
2618 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2619 assemble_variable_contents (decl, name, dont_output_data,
2620 (sect->common.flags & SECTION_MERGE)
2621 && (sect->common.flags & SECTION_STRINGS));
2622 if (asan_protected)
2624 unsigned HOST_WIDE_INT int size
2625 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2626 assemble_zeros (asan_red_zone_size (size));
2631 /* Return true if type TYPE contains any pointers. */
2633 static bool
2634 contains_pointers_p (tree type)
2636 switch (TREE_CODE (type))
2638 case POINTER_TYPE:
2639 case REFERENCE_TYPE:
2640 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2641 so I'll play safe and return 1. */
2642 case OFFSET_TYPE:
2643 return true;
2645 case RECORD_TYPE:
2646 case UNION_TYPE:
2647 case QUAL_UNION_TYPE:
2649 tree fields;
2650 /* For a type that has fields, see if the fields have pointers. */
2651 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2652 if (TREE_CODE (fields) == FIELD_DECL
2653 && contains_pointers_p (TREE_TYPE (fields)))
2654 return true;
2655 return false;
2658 case ARRAY_TYPE:
2659 /* An array type contains pointers if its element type does. */
2660 return contains_pointers_p (TREE_TYPE (type));
2662 default:
2663 return false;
2667 /* We delay assemble_external processing until
2668 the compilation unit is finalized. This is the best we can do for
2669 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2670 it all the way to final. See PR 17982 for further discussion. */
2671 static GTY(()) tree pending_assemble_externals;
2673 /* A similar list of pending libcall symbols. We only want to declare
2674 symbols that are actually used in the final assembly. */
2675 static GTY(()) rtx pending_libcall_symbols;
2677 #ifdef ASM_OUTPUT_EXTERNAL
2678 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2679 As a result, assemble_external can be called after the list of externals
2680 is processed and the pointer set destroyed. */
2681 static bool pending_assemble_externals_processed;
2683 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2684 TREE_LIST in assemble_external. */
2685 static hash_set<tree> *pending_assemble_externals_set;
2687 /* True if DECL is a function decl for which no out-of-line copy exists.
2688 It is assumed that DECL's assembler name has been set. */
2690 static bool
2691 incorporeal_function_p (tree decl)
2693 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl))
2695 const char *name;
2697 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2698 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2699 return true;
2701 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2702 /* Atomic or sync builtins which have survived this far will be
2703 resolved externally and therefore are not incorporeal. */
2704 if (startswith (name, "__builtin_"))
2705 return true;
2707 return false;
2710 /* Actually do the tests to determine if this is necessary, and invoke
2711 ASM_OUTPUT_EXTERNAL. */
2712 static void
2713 assemble_external_real (tree decl)
2715 rtx rtl = DECL_RTL (decl);
2717 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2718 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2719 && !incorporeal_function_p (decl))
2721 /* Some systems do require some output. */
2722 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2723 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2726 #endif
2728 void
2729 process_pending_assemble_externals (void)
2731 #ifdef ASM_OUTPUT_EXTERNAL
2732 tree list;
2733 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2734 assemble_external_real (TREE_VALUE (list));
2736 for (rtx list = pending_libcall_symbols; list; list = XEXP (list, 1))
2738 rtx symbol = XEXP (list, 0);
2739 const char *name = targetm.strip_name_encoding (XSTR (symbol, 0));
2740 tree id = get_identifier (name);
2741 if (TREE_SYMBOL_REFERENCED (id))
2742 targetm.asm_out.external_libcall (symbol);
2745 pending_assemble_externals = 0;
2746 pending_assemble_externals_processed = true;
2747 pending_libcall_symbols = NULL_RTX;
2748 delete pending_assemble_externals_set;
2749 pending_assemble_externals_set = nullptr;
2750 #endif
2753 /* This TREE_LIST contains any weak symbol declarations waiting
2754 to be emitted. */
2755 static GTY(()) tree weak_decls;
2757 /* Output something to declare an external symbol to the assembler,
2758 and qualifiers such as weakness. (Most assemblers don't need
2759 extern declaration, so we normally output nothing.) Do nothing if
2760 DECL is not external. */
2762 void
2763 assemble_external (tree decl ATTRIBUTE_UNUSED)
2765 /* Make sure that the ASM_OUT_FILE is open.
2766 If it's not, we should not be calling this function. */
2767 gcc_assert (asm_out_file);
2769 /* In a perfect world, the following condition would be true.
2770 Sadly, the Go front end emit assembly *from the front end*,
2771 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2772 #if 0
2773 /* This function should only be called if we are expanding, or have
2774 expanded, to RTL.
2775 Ideally, only final.cc would be calling this function, but it is
2776 not clear whether that would break things somehow. See PR 17982
2777 for further discussion. */
2778 gcc_assert (state == EXPANSION
2779 || state == FINISHED);
2780 #endif
2782 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2783 return;
2785 /* We want to output annotation for weak and external symbols at
2786 very last to check if they are references or not. */
2788 if (TARGET_SUPPORTS_WEAK
2789 && DECL_WEAK (decl)
2790 /* TREE_STATIC is a weird and abused creature which is not
2791 generally the right test for whether an entity has been
2792 locally emitted, inlined or otherwise not-really-extern, but
2793 for declarations that can be weak, it happens to be
2794 match. */
2795 && !TREE_STATIC (decl)
2796 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2797 && value_member (decl, weak_decls) == NULL_TREE)
2798 weak_decls = tree_cons (NULL, decl, weak_decls);
2800 #ifdef ASM_OUTPUT_EXTERNAL
2801 if (pending_assemble_externals_processed)
2803 assemble_external_real (decl);
2804 return;
2807 if (! pending_assemble_externals_set->add (decl))
2808 pending_assemble_externals = tree_cons (NULL, decl,
2809 pending_assemble_externals);
2810 #endif
2813 /* Similar, for calling a library function FUN. */
2815 void
2816 assemble_external_libcall (rtx fun)
2818 /* Declare library function name external when first used, if nec. */
2819 if (! SYMBOL_REF_USED (fun))
2821 #ifdef ASM_OUTPUT_EXTERNAL
2822 gcc_assert (!pending_assemble_externals_processed);
2823 #endif
2824 SYMBOL_REF_USED (fun) = 1;
2825 /* Make sure the libcall symbol is in the symtab so any
2826 reference to it will mark its tree node as referenced, via
2827 assemble_name_resolve. These are eventually emitted, if
2828 used, in process_pending_assemble_externals. */
2829 const char *name = targetm.strip_name_encoding (XSTR (fun, 0));
2830 get_identifier (name);
2831 pending_libcall_symbols = gen_rtx_EXPR_LIST (VOIDmode, fun,
2832 pending_libcall_symbols);
2836 /* Assemble a label named NAME. */
2838 void
2839 assemble_label (FILE *file, const char *name)
2841 ASM_OUTPUT_LABEL (file, name);
2844 /* Set the symbol_referenced flag for ID. */
2845 void
2846 mark_referenced (tree id)
2848 TREE_SYMBOL_REFERENCED (id) = 1;
2851 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2852 void
2853 mark_decl_referenced (tree decl)
2855 if (TREE_CODE (decl) == FUNCTION_DECL)
2857 /* Extern inline functions don't become needed when referenced.
2858 If we know a method will be emitted in other TU and no new
2859 functions can be marked reachable, just use the external
2860 definition. */
2861 struct cgraph_node *node = cgraph_node::get_create (decl);
2862 if (!DECL_EXTERNAL (decl)
2863 && !node->definition)
2864 node->mark_force_output ();
2866 else if (VAR_P (decl))
2868 varpool_node *node = varpool_node::get_create (decl);
2869 /* C++ frontend use mark_decl_references to force COMDAT variables
2870 to be output that might appear dead otherwise. */
2871 node->force_output = true;
2873 /* else do nothing - we can get various sorts of CST nodes here,
2874 which do not need to be marked. */
2878 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2879 starts with a *, the rest of NAME is output verbatim. Otherwise
2880 NAME is transformed in a target-specific way (usually by the
2881 addition of an underscore). */
2883 void
2884 assemble_name_raw (FILE *file, const char *name)
2886 if (name[0] == '*')
2887 fputs (&name[1], file);
2888 else
2889 ASM_OUTPUT_LABELREF (file, name);
2892 /* Return NAME that should actually be emitted, looking through
2893 transparent aliases. If NAME refers to an entity that is also
2894 represented as a tree (like a function or variable), mark the entity
2895 as referenced. */
2896 const char *
2897 assemble_name_resolve (const char *name)
2899 const char *real_name = targetm.strip_name_encoding (name);
2900 tree id = maybe_get_identifier (real_name);
2902 if (id)
2904 tree id_orig = id;
2906 mark_referenced (id);
2907 ultimate_transparent_alias_target (&id);
2908 if (id != id_orig)
2909 name = IDENTIFIER_POINTER (id);
2910 gcc_assert (! TREE_CHAIN (id));
2913 return name;
2916 /* Like assemble_name_raw, but should be used when NAME might refer to
2917 an entity that is also represented as a tree (like a function or
2918 variable). If NAME does refer to such an entity, that entity will
2919 be marked as referenced. */
2921 void
2922 assemble_name (FILE *file, const char *name)
2924 assemble_name_raw (file, assemble_name_resolve (name));
2927 /* Allocate SIZE bytes writable static space with a gensym name
2928 and return an RTX to refer to its address. */
2931 assemble_static_space (unsigned HOST_WIDE_INT size)
2933 char name[17];
2934 const char *namestring;
2935 rtx x;
2937 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2938 ++const_labelno;
2939 namestring = ggc_strdup (name);
2941 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2942 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2944 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2945 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2946 BIGGEST_ALIGNMENT);
2947 #else
2948 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2949 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2950 #else
2952 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2953 so that each uninitialized object starts on such a boundary. */
2954 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2955 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2956 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2957 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2958 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2959 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2961 #endif
2962 #endif
2963 return x;
2966 /* Assemble the static constant template for function entry trampolines.
2967 This is done at most once per compilation.
2968 Returns an RTX for the address of the template. */
2970 static GTY(()) rtx initial_trampoline;
2973 assemble_trampoline_template (void)
2975 char label[256];
2976 const char *name;
2977 int align;
2978 rtx symbol;
2980 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2982 if (initial_trampoline)
2983 return initial_trampoline;
2985 /* By default, put trampoline templates in read-only data section. */
2987 #ifdef TRAMPOLINE_SECTION
2988 switch_to_section (TRAMPOLINE_SECTION);
2989 #else
2990 switch_to_section (readonly_data_section);
2991 #endif
2993 /* Write the assembler code to define one. */
2994 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2995 if (align > 0)
2996 ASM_OUTPUT_ALIGN (asm_out_file, align);
2998 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2999 targetm.asm_out.trampoline_template (asm_out_file);
3001 /* Record the rtl to refer to it. */
3002 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
3003 name = ggc_strdup (label);
3004 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
3005 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
3007 initial_trampoline = gen_const_mem (BLKmode, symbol);
3008 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
3009 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
3011 return initial_trampoline;
3014 /* A and B are either alignments or offsets. Return the minimum alignment
3015 that may be assumed after adding the two together. */
3017 static inline unsigned
3018 min_align (unsigned int a, unsigned int b)
3020 return least_bit_hwi (a | b);
3023 /* Return the assembler directive for creating a given kind of integer
3024 object. SIZE is the number of bytes in the object and ALIGNED_P
3025 indicates whether it is known to be aligned. Return NULL if the
3026 assembly dialect has no such directive.
3028 The returned string should be printed at the start of a new line and
3029 be followed immediately by the object's initial value. */
3031 const char *
3032 integer_asm_op (int size, int aligned_p)
3034 struct asm_int_op *ops;
3036 if (aligned_p)
3037 ops = &targetm.asm_out.aligned_op;
3038 else
3039 ops = &targetm.asm_out.unaligned_op;
3041 switch (size)
3043 case 1:
3044 return targetm.asm_out.byte_op;
3045 case 2:
3046 return ops->hi;
3047 case 3:
3048 return ops->psi;
3049 case 4:
3050 return ops->si;
3051 case 5:
3052 case 6:
3053 case 7:
3054 return ops->pdi;
3055 case 8:
3056 return ops->di;
3057 case 9:
3058 case 10:
3059 case 11:
3060 case 12:
3061 case 13:
3062 case 14:
3063 case 15:
3064 return ops->pti;
3065 case 16:
3066 return ops->ti;
3067 default:
3068 return NULL;
3072 /* Use directive OP to assemble an integer object X. Print OP at the
3073 start of the line, followed immediately by the value of X. */
3075 void
3076 assemble_integer_with_op (const char *op, rtx x)
3078 fputs (op, asm_out_file);
3079 output_addr_const (asm_out_file, x);
3080 fputc ('\n', asm_out_file);
3083 /* The default implementation of the asm_out.integer target hook. */
3085 bool
3086 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
3087 unsigned int size ATTRIBUTE_UNUSED,
3088 int aligned_p ATTRIBUTE_UNUSED)
3090 const char *op = integer_asm_op (size, aligned_p);
3091 /* Avoid GAS bugs for large values. Specifically negative values whose
3092 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
3093 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
3094 return false;
3095 return op && (assemble_integer_with_op (op, x), true);
3098 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
3099 the alignment of the integer in bits. Return 1 if we were able to output
3100 the constant, otherwise 0. We must be able to output the constant,
3101 if FORCE is nonzero. */
3103 bool
3104 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
3106 int aligned_p;
3108 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
3110 /* See if the target hook can handle this kind of object. */
3111 if (targetm.asm_out.integer (x, size, aligned_p))
3112 return true;
3114 /* If the object is a multi-byte one, try splitting it up. Split
3115 it into words it if is multi-word, otherwise split it into bytes. */
3116 if (size > 1)
3118 machine_mode omode, imode;
3119 unsigned int subalign;
3120 unsigned int subsize, i;
3121 enum mode_class mclass;
3123 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
3124 subalign = MIN (align, subsize * BITS_PER_UNIT);
3125 if (GET_CODE (x) == CONST_FIXED)
3126 mclass = GET_MODE_CLASS (GET_MODE (x));
3127 else
3128 mclass = MODE_INT;
3130 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
3131 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
3133 for (i = 0; i < size; i += subsize)
3135 rtx partial = simplify_subreg (omode, x, imode, i);
3136 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
3137 break;
3139 if (i == size)
3140 return true;
3142 /* If we've printed some of it, but not all of it, there's no going
3143 back now. */
3144 gcc_assert (!i);
3147 gcc_assert (!force);
3149 return false;
3152 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
3153 is the alignment of the constant in bits. If REVERSE is true, D is output
3154 in reverse storage order. */
3156 void
3157 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
3158 bool reverse)
3160 long data[4] = {0, 0, 0, 0};
3161 int bitsize, nelts, nunits, units_per;
3162 rtx elt;
3164 /* This is hairy. We have a quantity of known size. real_to_target
3165 will put it into an array of *host* longs, 32 bits per element
3166 (even if long is more than 32 bits). We need to determine the
3167 number of array elements that are occupied (nelts) and the number
3168 of *target* min-addressable units that will be occupied in the
3169 object file (nunits). We cannot assume that 32 divides the
3170 mode's bitsize (size * BITS_PER_UNIT) evenly.
3172 size * BITS_PER_UNIT is used here to make sure that padding bits
3173 (which might appear at either end of the value; real_to_target
3174 will include the padding bits in its output array) are included. */
3176 nunits = GET_MODE_SIZE (mode);
3177 bitsize = nunits * BITS_PER_UNIT;
3178 nelts = CEIL (bitsize, 32);
3179 units_per = 32 / BITS_PER_UNIT;
3181 real_to_target (data, &d, mode);
3183 /* Put out the first word with the specified alignment. */
3184 unsigned int chunk_nunits = MIN (nunits, units_per);
3185 if (reverse)
3186 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
3187 else
3188 elt = GEN_INT (sext_hwi (data[0], chunk_nunits * BITS_PER_UNIT));
3189 assemble_integer (elt, chunk_nunits, align, 1);
3190 nunits -= chunk_nunits;
3192 /* Subsequent words need only 32-bit alignment. */
3193 align = min_align (align, 32);
3195 for (int i = 1; i < nelts; i++)
3197 chunk_nunits = MIN (nunits, units_per);
3198 if (reverse)
3199 elt = flip_storage_order (SImode,
3200 gen_int_mode (data[nelts - 1 - i], SImode));
3201 else
3202 elt = GEN_INT (sext_hwi (data[i], chunk_nunits * BITS_PER_UNIT));
3203 assemble_integer (elt, chunk_nunits, align, 1);
3204 nunits -= chunk_nunits;
3208 /* Given an expression EXP with a constant value,
3209 reduce it to the sum of an assembler symbol and an integer.
3210 Store them both in the structure *VALUE.
3211 EXP must be reducible. */
3213 class addr_const {
3214 public:
3215 rtx base;
3216 poly_int64 offset;
3219 static void
3220 decode_addr_const (tree exp, class addr_const *value)
3222 tree target = TREE_OPERAND (exp, 0);
3223 poly_int64 offset = 0;
3224 rtx x;
3226 while (1)
3228 poly_int64 bytepos;
3229 if (TREE_CODE (target) == COMPONENT_REF
3230 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
3231 &bytepos))
3233 offset += bytepos;
3234 target = TREE_OPERAND (target, 0);
3236 else if (TREE_CODE (target) == ARRAY_REF
3237 || TREE_CODE (target) == ARRAY_RANGE_REF)
3239 /* Truncate big offset. */
3240 offset
3241 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
3242 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
3243 target = TREE_OPERAND (target, 0);
3245 else if (TREE_CODE (target) == MEM_REF
3246 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
3248 offset += mem_ref_offset (target).force_shwi ();
3249 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
3251 else if (INDIRECT_REF_P (target)
3252 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
3253 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
3254 == ADDR_EXPR)
3255 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
3256 else
3257 break;
3260 switch (TREE_CODE (target))
3262 case VAR_DECL:
3263 case FUNCTION_DECL:
3264 x = DECL_RTL (target);
3265 break;
3267 case LABEL_DECL:
3268 x = gen_rtx_MEM (FUNCTION_MODE,
3269 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
3270 break;
3272 case REAL_CST:
3273 case FIXED_CST:
3274 case STRING_CST:
3275 case COMPLEX_CST:
3276 case CONSTRUCTOR:
3277 case INTEGER_CST:
3278 x = lookup_constant_def (target);
3279 /* Should have been added by output_addressed_constants. */
3280 gcc_assert (x);
3281 break;
3283 case INDIRECT_REF:
3284 /* This deals with absolute addresses. */
3285 offset += tree_to_shwi (TREE_OPERAND (target, 0));
3286 x = gen_rtx_MEM (QImode,
3287 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
3288 break;
3290 case COMPOUND_LITERAL_EXPR:
3291 gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
3292 x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
3293 break;
3295 default:
3296 gcc_unreachable ();
3299 gcc_assert (MEM_P (x));
3300 x = XEXP (x, 0);
3302 value->base = x;
3303 value->offset = offset;
3306 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
3308 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
3310 /* Constant pool accessor function. */
3312 hash_table<tree_descriptor_hasher> *
3313 constant_pool_htab (void)
3315 return const_desc_htab;
3318 /* Compute a hash code for a constant expression. */
3320 hashval_t
3321 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3323 return ptr->hash;
3326 static hashval_t
3327 const_hash_1 (const tree exp)
3329 const char *p;
3330 hashval_t hi;
3331 int len, i;
3332 enum tree_code code = TREE_CODE (exp);
3334 /* Either set P and LEN to the address and len of something to hash and
3335 exit the switch or return a value. */
3337 switch (code)
3339 case INTEGER_CST:
3340 p = (char *) &TREE_INT_CST_ELT (exp, 0);
3341 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3342 break;
3344 case REAL_CST:
3345 return real_hash (TREE_REAL_CST_PTR (exp));
3347 case FIXED_CST:
3348 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3350 case STRING_CST:
3351 p = TREE_STRING_POINTER (exp);
3352 len = TREE_STRING_LENGTH (exp);
3353 break;
3355 case COMPLEX_CST:
3356 return (const_hash_1 (TREE_REALPART (exp)) * 5
3357 + const_hash_1 (TREE_IMAGPART (exp)));
3359 case VECTOR_CST:
3361 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3362 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3363 unsigned int count = vector_cst_encoded_nelts (exp);
3364 for (unsigned int i = 0; i < count; ++i)
3365 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3366 return hi;
3369 case RAW_DATA_CST:
3370 p = RAW_DATA_POINTER (exp);
3371 len = RAW_DATA_LENGTH (exp);
3372 break;
3374 case CONSTRUCTOR:
3376 unsigned HOST_WIDE_INT idx;
3377 tree value;
3379 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3381 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3382 if (value)
3383 hi = hi * 603 + const_hash_1 (value);
3385 return hi;
3388 case ADDR_EXPR:
3389 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3390 return const_hash_1 (TREE_OPERAND (exp, 0));
3392 /* Fallthru. */
3393 case FDESC_EXPR:
3395 class addr_const value;
3397 decode_addr_const (exp, &value);
3398 switch (GET_CODE (value.base))
3400 case SYMBOL_REF:
3401 /* Don't hash the address of the SYMBOL_REF;
3402 only use the offset and the symbol name. */
3403 hi = value.offset.coeffs[0];
3404 p = XSTR (value.base, 0);
3405 for (i = 0; p[i] != 0; i++)
3406 hi = ((hi * 613) + (unsigned) (p[i]));
3407 break;
3409 case LABEL_REF:
3410 hi = (value.offset.coeffs[0]
3411 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3412 break;
3414 default:
3415 gcc_unreachable ();
3418 return hi;
3420 case PLUS_EXPR:
3421 case POINTER_PLUS_EXPR:
3422 case MINUS_EXPR:
3423 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3424 + const_hash_1 (TREE_OPERAND (exp, 1)));
3426 CASE_CONVERT:
3427 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3429 default:
3430 /* A language specific constant. Just hash the code. */
3431 return code;
3434 /* Compute hashing function. */
3435 hi = len;
3436 for (i = 0; i < len; i++)
3437 hi = ((hi * 613) + (unsigned) (p[i]));
3439 return hi;
3442 /* Wrapper of compare_constant, for the htab interface. */
3443 bool
3444 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3445 constant_descriptor_tree *c2)
3447 if (c1->hash != c2->hash)
3448 return false;
3449 return compare_constant (c1->value, c2->value);
3452 /* Compare t1 and t2, and return true only if they are known to result in
3453 the same bit pattern on output. */
3455 static bool
3456 compare_constant (const tree t1, const tree t2)
3458 enum tree_code typecode;
3460 if (t1 == NULL_TREE)
3461 return t2 == NULL_TREE;
3462 if (t2 == NULL_TREE)
3463 return false;
3465 if (TREE_CODE (t1) != TREE_CODE (t2))
3466 return false;
3468 switch (TREE_CODE (t1))
3470 case INTEGER_CST:
3471 /* Integer constants are the same only if the same width of type. */
3472 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3473 return false;
3474 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3475 return false;
3476 return tree_int_cst_equal (t1, t2);
3478 case REAL_CST:
3479 /* Real constants are the same only if the same width of type. In
3480 addition to the same width, we need to check whether the modes are the
3481 same. There might be two floating point modes that are the same size
3482 but have different representations, such as the PowerPC that has 2
3483 different 128-bit floating point types (IBM extended double and IEEE
3484 128-bit floating point). */
3485 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3486 return false;
3487 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3488 return false;
3489 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3491 case FIXED_CST:
3492 /* Fixed constants are the same only if the same width of type. */
3493 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3494 return false;
3496 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3498 case STRING_CST:
3499 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3500 || int_size_in_bytes (TREE_TYPE (t1))
3501 != int_size_in_bytes (TREE_TYPE (t2)))
3502 return false;
3504 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3505 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3506 TREE_STRING_LENGTH (t1)));
3508 case COMPLEX_CST:
3509 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3510 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3512 case VECTOR_CST:
3514 if (VECTOR_CST_NPATTERNS (t1)
3515 != VECTOR_CST_NPATTERNS (t2))
3516 return false;
3518 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3519 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3520 return false;
3522 unsigned int count = vector_cst_encoded_nelts (t1);
3523 for (unsigned int i = 0; i < count; ++i)
3524 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3525 VECTOR_CST_ENCODED_ELT (t2, i)))
3526 return false;
3528 return true;
3531 case RAW_DATA_CST:
3532 return (RAW_DATA_LENGTH (t1) == RAW_DATA_LENGTH (t2)
3533 && ! memcmp (RAW_DATA_POINTER (t1), RAW_DATA_POINTER (t2),
3534 RAW_DATA_LENGTH (t1)));
3536 case CONSTRUCTOR:
3538 vec<constructor_elt, va_gc> *v1, *v2;
3539 unsigned HOST_WIDE_INT idx;
3541 typecode = TREE_CODE (TREE_TYPE (t1));
3542 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3543 return false;
3545 if (typecode == ARRAY_TYPE)
3547 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3548 /* For arrays, check that mode, size and storage order match. */
3549 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3550 || size_1 == -1
3551 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3552 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3553 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3554 return false;
3556 else
3558 /* For record and union constructors, require exact type
3559 equality. */
3560 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3561 return false;
3564 v1 = CONSTRUCTOR_ELTS (t1);
3565 v2 = CONSTRUCTOR_ELTS (t2);
3566 if (vec_safe_length (v1) != vec_safe_length (v2))
3567 return false;
3569 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3571 constructor_elt *c1 = &(*v1)[idx];
3572 constructor_elt *c2 = &(*v2)[idx];
3574 /* Check that each value is the same... */
3575 if (!compare_constant (c1->value, c2->value))
3576 return false;
3577 /* ... and that they apply to the same fields! */
3578 if (typecode == ARRAY_TYPE)
3580 if (!compare_constant (c1->index, c2->index))
3581 return false;
3583 else
3585 if (c1->index != c2->index)
3586 return false;
3590 return true;
3593 case ADDR_EXPR:
3594 case FDESC_EXPR:
3596 class addr_const value1, value2;
3597 enum rtx_code code;
3598 bool ret;
3600 decode_addr_const (t1, &value1);
3601 decode_addr_const (t2, &value2);
3603 if (maybe_ne (value1.offset, value2.offset))
3604 return false;
3606 code = GET_CODE (value1.base);
3607 if (code != GET_CODE (value2.base))
3608 return false;
3610 switch (code)
3612 case SYMBOL_REF:
3613 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3614 break;
3616 case LABEL_REF:
3617 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3618 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3619 break;
3621 default:
3622 gcc_unreachable ();
3624 return ret;
3627 case PLUS_EXPR:
3628 case POINTER_PLUS_EXPR:
3629 case MINUS_EXPR:
3630 case RANGE_EXPR:
3631 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3632 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3634 CASE_CONVERT:
3635 case VIEW_CONVERT_EXPR:
3636 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3638 default:
3639 return false;
3643 /* Return the section into which constant EXP should be placed. */
3645 static section *
3646 get_constant_section (tree exp, unsigned int align)
3648 return targetm.asm_out.select_section (exp,
3649 compute_reloc_for_constant (exp),
3650 align);
3653 /* Return the size of constant EXP in bytes. */
3655 static HOST_WIDE_INT
3656 get_constant_size (tree exp)
3658 HOST_WIDE_INT size;
3660 size = int_size_in_bytes (TREE_TYPE (exp));
3661 gcc_checking_assert (size >= 0);
3662 gcc_checking_assert (TREE_CODE (exp) != STRING_CST
3663 || size >= TREE_STRING_LENGTH (exp));
3664 return size;
3667 /* Subroutine of output_constant_def:
3668 No constant equal to EXP is known to have been output.
3669 Make a constant descriptor to enter EXP in the hash table.
3670 Assign the label number and construct RTL to refer to the
3671 constant's location in memory.
3672 Caller is responsible for updating the hash table. */
3674 static struct constant_descriptor_tree *
3675 build_constant_desc (tree exp)
3677 struct constant_descriptor_tree *desc;
3678 rtx symbol, rtl;
3679 char label[256];
3680 int labelno;
3681 tree decl;
3683 desc = ggc_alloc<constant_descriptor_tree> ();
3684 desc->value = exp;
3686 /* Create a string containing the label name, in LABEL. */
3687 labelno = const_labelno++;
3688 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3690 /* Construct the VAR_DECL associated with the constant. */
3691 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3692 TREE_TYPE (exp));
3693 DECL_ARTIFICIAL (decl) = 1;
3694 DECL_IGNORED_P (decl) = 1;
3695 TREE_READONLY (decl) = 1;
3696 TREE_STATIC (decl) = 1;
3697 TREE_ADDRESSABLE (decl) = 1;
3698 /* We don't set the RTL yet as this would cause varpool to assume that the
3699 variable is referenced. Moreover, it would just be dropped in LTO mode.
3700 Instead we set the flag that will be recognized in make_decl_rtl. */
3701 DECL_IN_CONSTANT_POOL (decl) = 1;
3702 DECL_INITIAL (decl) = desc->value;
3703 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3704 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3705 if (TREE_CODE (exp) == STRING_CST)
3706 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3707 else
3709 align_variable (decl, 0);
3710 if (DECL_ALIGN (decl) < GET_MODE_ALIGNMENT (DECL_MODE (decl))
3711 && ((optab_handler (movmisalign_optab, DECL_MODE (decl))
3712 != CODE_FOR_nothing)
3713 || targetm.slow_unaligned_access (DECL_MODE (decl),
3714 DECL_ALIGN (decl))))
3715 SET_DECL_ALIGN (decl, GET_MODE_ALIGNMENT (DECL_MODE (decl)));
3718 /* Now construct the SYMBOL_REF and the MEM. */
3719 if (use_object_blocks_p ())
3721 int align = (TREE_CODE (decl) == CONST_DECL
3722 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3723 ? DECL_ALIGN (decl)
3724 : symtab_node::get (decl)->definition_alignment ());
3725 section *sect = get_constant_section (exp, align);
3726 symbol = create_block_symbol (ggc_strdup (label),
3727 get_block_for_section (sect), -1);
3729 else
3730 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3731 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3732 SET_SYMBOL_REF_DECL (symbol, decl);
3733 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3735 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3736 set_mem_alias_set (rtl, 0);
3738 /* Putting EXP into the literal pool might have imposed a different
3739 alignment which should be visible in the RTX as well. */
3740 set_mem_align (rtl, DECL_ALIGN (decl));
3742 /* We cannot share RTX'es in pool entries.
3743 Mark this piece of RTL as required for unsharing. */
3744 RTX_FLAG (rtl, used) = 1;
3746 /* Set flags or add text to the name to record information, such as
3747 that it is a local symbol. If the name is changed, the macro
3748 ASM_OUTPUT_LABELREF will have to know how to strip this
3749 information. This call might invalidate our local variable
3750 SYMBOL; we can't use it afterward. */
3751 targetm.encode_section_info (exp, rtl, true);
3753 desc->rtl = rtl;
3755 return desc;
3758 /* Subroutine of output_constant_def and tree_output_constant_def:
3759 Add a constant to the hash table that tracks which constants
3760 already have labels. */
3762 static constant_descriptor_tree *
3763 add_constant_to_table (tree exp, int defer)
3765 /* The hash table methods may call output_constant_def for addressed
3766 constants, so handle them first. */
3767 output_addressed_constants (exp, defer);
3769 /* Sanity check to catch recursive insertion. */
3770 static bool inserting;
3771 gcc_assert (!inserting);
3772 inserting = true;
3774 /* Look up EXP in the table of constant descriptors. If we didn't
3775 find it, create a new one. */
3776 struct constant_descriptor_tree key;
3777 key.value = exp;
3778 key.hash = const_hash_1 (exp);
3779 constant_descriptor_tree **loc
3780 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3782 inserting = false;
3784 struct constant_descriptor_tree *desc = *loc;
3785 if (!desc)
3787 desc = build_constant_desc (exp);
3788 desc->hash = key.hash;
3789 *loc = desc;
3792 return desc;
3795 /* Return an rtx representing a reference to constant data in memory
3796 for the constant expression EXP.
3798 If assembler code for such a constant has already been output,
3799 return an rtx to refer to it.
3800 Otherwise, output such a constant in memory
3801 and generate an rtx for it.
3803 If DEFER is nonzero, this constant can be deferred and output only
3804 if referenced in the function after all optimizations.
3806 `const_desc_table' records which constants already have label strings. */
3809 output_constant_def (tree exp, int defer)
3811 struct constant_descriptor_tree *desc = add_constant_to_table (exp, defer);
3812 maybe_output_constant_def_contents (desc, defer);
3813 return desc->rtl;
3816 /* Subroutine of output_constant_def: Decide whether or not we need to
3817 output the constant DESC now, and if so, do it. */
3818 static void
3819 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3820 int defer)
3822 rtx symbol = XEXP (desc->rtl, 0);
3823 tree exp = desc->value;
3825 if (flag_syntax_only)
3826 return;
3828 if (TREE_ASM_WRITTEN (exp))
3829 /* Already output; don't do it again. */
3830 return;
3832 /* We can always defer constants as long as the context allows
3833 doing so. */
3834 if (defer)
3836 /* Increment n_deferred_constants if it exists. It needs to be at
3837 least as large as the number of constants actually referred to
3838 by the function. If it's too small we'll stop looking too early
3839 and fail to emit constants; if it's too large we'll only look
3840 through the entire function when we could have stopped earlier. */
3841 if (cfun)
3842 n_deferred_constants++;
3843 return;
3846 output_constant_def_contents (symbol);
3849 /* Subroutine of output_constant_def_contents. Output the definition
3850 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3851 constant's alignment in bits. */
3853 static void
3854 assemble_constant_contents (tree exp, const char *label, unsigned int align,
3855 bool merge_strings)
3857 HOST_WIDE_INT size;
3859 size = get_constant_size (exp);
3861 /* Do any machine/system dependent processing of the constant. */
3862 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3864 /* Output the value of EXP. */
3865 output_constant (exp, size, align, false, merge_strings);
3867 targetm.asm_out.decl_end ();
3870 /* We must output the constant data referred to by SYMBOL; do so. */
3872 static void
3873 output_constant_def_contents (rtx symbol)
3875 tree decl = SYMBOL_REF_DECL (symbol);
3876 tree exp = DECL_INITIAL (decl);
3877 bool asan_protected = false;
3879 /* Make sure any other constants whose addresses appear in EXP
3880 are assigned label numbers. */
3881 output_addressed_constants (exp, 0);
3883 /* We are no longer deferring this constant. */
3884 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3886 if ((flag_sanitize & SANITIZE_ADDRESS)
3887 && TREE_CODE (exp) == STRING_CST
3888 && asan_protect_global (exp))
3890 asan_protected = true;
3891 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3892 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3895 /* If the constant is part of an object block, make sure that the
3896 decl has been positioned within its block, but do not write out
3897 its definition yet. output_object_blocks will do that later. */
3898 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3899 place_block_symbol (symbol);
3900 else
3902 int align = (TREE_CODE (decl) == CONST_DECL
3903 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3904 ? DECL_ALIGN (decl)
3905 : symtab_node::get (decl)->definition_alignment ());
3906 section *sect = get_constant_section (exp, align);
3907 switch_to_section (sect);
3908 if (align > BITS_PER_UNIT)
3909 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3910 assemble_constant_contents (exp, XSTR (symbol, 0), align,
3911 (sect->common.flags & SECTION_MERGE)
3912 && (sect->common.flags & SECTION_STRINGS));
3913 if (asan_protected)
3915 HOST_WIDE_INT size = get_constant_size (exp);
3916 assemble_zeros (asan_red_zone_size (size));
3921 /* Look up EXP in the table of constant descriptors. Return the rtl
3922 if it has been emitted, else null. */
3925 lookup_constant_def (tree exp)
3927 struct constant_descriptor_tree key;
3929 key.value = exp;
3930 key.hash = const_hash_1 (exp);
3931 constant_descriptor_tree *desc
3932 = const_desc_htab->find_with_hash (&key, key.hash);
3934 return (desc ? desc->rtl : NULL_RTX);
3937 /* Return a tree representing a reference to constant data in memory
3938 for the constant expression EXP.
3940 This is the counterpart of output_constant_def at the Tree level. */
3942 tree
3943 tree_output_constant_def (tree exp)
3945 struct constant_descriptor_tree *desc = add_constant_to_table (exp, 1);
3946 tree decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3947 varpool_node::finalize_decl (decl);
3948 return decl;
3951 class GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3952 public:
3953 class constant_descriptor_rtx *next;
3954 rtx mem;
3955 rtx sym;
3956 rtx constant;
3957 HOST_WIDE_INT offset;
3958 hashval_t hash;
3959 fixed_size_mode mode;
3960 unsigned int align;
3961 int labelno;
3962 int mark;
3965 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3967 static hashval_t hash (constant_descriptor_rtx *);
3968 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3971 /* Used in the hash tables to avoid outputting the same constant
3972 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3973 are output once per function, not once per file. */
3974 /* ??? Only a few targets need per-function constant pools. Most
3975 can use one per-file pool. Should add a targetm bit to tell the
3976 difference. */
3978 struct GTY(()) rtx_constant_pool {
3979 /* Pointers to first and last constant in pool, as ordered by offset. */
3980 class constant_descriptor_rtx *first;
3981 class constant_descriptor_rtx *last;
3983 /* Hash facility for making memory-constants from constant rtl-expressions.
3984 It is used on RISC machines where immediate integer arguments and
3985 constant addresses are restricted so that such constants must be stored
3986 in memory. */
3987 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3989 /* Current offset in constant pool (does not include any
3990 machine-specific header). */
3991 HOST_WIDE_INT offset;
3994 /* Hash and compare functions for const_rtx_htab. */
3996 hashval_t
3997 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3999 return desc->hash;
4002 bool
4003 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
4004 constant_descriptor_rtx *y)
4006 if (x->mode != y->mode)
4007 return false;
4008 return rtx_equal_p (x->constant, y->constant);
4011 /* Hash one component of a constant. */
4013 static hashval_t
4014 const_rtx_hash_1 (const_rtx x)
4016 unsigned HOST_WIDE_INT hwi;
4017 machine_mode mode;
4018 enum rtx_code code;
4019 hashval_t h;
4020 int i;
4022 code = GET_CODE (x);
4023 mode = GET_MODE (x);
4024 h = (hashval_t) code * 1048573 + mode;
4026 switch (code)
4028 case CONST_INT:
4029 hwi = INTVAL (x);
4031 fold_hwi:
4033 int shift = sizeof (hashval_t) * CHAR_BIT;
4034 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
4036 h ^= (hashval_t) hwi;
4037 for (i = 1; i < n; ++i)
4039 hwi >>= shift;
4040 h ^= (hashval_t) hwi;
4043 break;
4045 case CONST_WIDE_INT:
4046 hwi = 0;
4048 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
4049 hwi ^= CONST_WIDE_INT_ELT (x, i);
4050 goto fold_hwi;
4053 case CONST_DOUBLE:
4054 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
4056 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
4057 goto fold_hwi;
4059 else
4060 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
4061 break;
4063 case CONST_FIXED:
4064 h ^= fixed_hash (CONST_FIXED_VALUE (x));
4065 break;
4067 case SYMBOL_REF:
4068 h ^= htab_hash_string (XSTR (x, 0));
4069 break;
4071 case LABEL_REF:
4072 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
4073 break;
4075 case UNSPEC:
4076 case UNSPEC_VOLATILE:
4077 h = h * 251 + XINT (x, 1);
4078 break;
4080 default:
4081 break;
4084 return h;
4087 /* Compute a hash value for X, which should be a constant. */
4089 static hashval_t
4090 const_rtx_hash (rtx x)
4092 hashval_t h = 0;
4093 subrtx_iterator::array_type array;
4094 FOR_EACH_SUBRTX (iter, array, x, ALL)
4095 h = h * 509 + const_rtx_hash_1 (*iter);
4096 return h;
4100 /* Create and return a new rtx constant pool. */
4102 static struct rtx_constant_pool *
4103 create_constant_pool (void)
4105 struct rtx_constant_pool *pool;
4107 pool = ggc_alloc<rtx_constant_pool> ();
4108 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
4109 pool->first = NULL;
4110 pool->last = NULL;
4111 pool->offset = 0;
4112 return pool;
4115 /* Initialize constant pool hashing for a new function. */
4117 void
4118 init_varasm_status (void)
4120 crtl->varasm.pool = create_constant_pool ();
4121 crtl->varasm.deferred_constants = 0;
4124 /* Given a MINUS expression, simplify it if both sides
4125 include the same symbol. */
4128 simplify_subtraction (rtx x)
4130 rtx r = simplify_rtx (x);
4131 return r ? r : x;
4134 /* Given a constant rtx X, make (or find) a memory constant for its value
4135 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
4136 of X. */
4139 force_const_mem (machine_mode in_mode, rtx x)
4141 class constant_descriptor_rtx *desc, tmp;
4142 struct rtx_constant_pool *pool;
4143 char label[256];
4144 rtx def, symbol;
4145 hashval_t hash;
4146 unsigned int align;
4147 constant_descriptor_rtx **slot;
4148 fixed_size_mode mode;
4150 /* We can't force variable-sized objects to memory. */
4151 if (!is_a <fixed_size_mode> (in_mode, &mode))
4152 return NULL_RTX;
4154 /* If we're not allowed to drop X into the constant pool, don't. */
4155 if (targetm.cannot_force_const_mem (mode, x))
4156 return NULL_RTX;
4158 /* Record that this function has used a constant pool entry. */
4159 crtl->uses_const_pool = 1;
4161 /* Decide which pool to use. */
4162 pool = (targetm.use_blocks_for_constant_p (mode, x)
4163 ? shared_constant_pool
4164 : crtl->varasm.pool);
4166 /* Lookup the value in the hashtable. */
4167 tmp.constant = x;
4168 tmp.mode = mode;
4169 hash = const_rtx_hash (x);
4170 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
4171 desc = *slot;
4173 /* If the constant was already present, return its memory. */
4174 if (desc)
4175 return copy_rtx (desc->mem);
4177 /* Otherwise, create a new descriptor. */
4178 desc = ggc_alloc<constant_descriptor_rtx> ();
4179 *slot = desc;
4181 /* Align the location counter as required by EXP's data type. */
4182 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
4183 align = targetm.static_rtx_alignment (align_mode);
4185 pool->offset += (align / BITS_PER_UNIT) - 1;
4186 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4188 desc->next = NULL;
4189 desc->constant = copy_rtx (tmp.constant);
4190 desc->offset = pool->offset;
4191 desc->hash = hash;
4192 desc->mode = mode;
4193 desc->align = align;
4194 desc->labelno = const_labelno;
4195 desc->mark = 0;
4197 pool->offset += GET_MODE_SIZE (mode);
4198 if (pool->last)
4199 pool->last->next = desc;
4200 else
4201 pool->first = pool->last = desc;
4202 pool->last = desc;
4204 /* Create a string containing the label name, in LABEL. */
4205 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
4206 ++const_labelno;
4208 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
4209 the constants pool. */
4210 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
4212 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
4213 symbol = create_block_symbol (ggc_strdup (label),
4214 get_block_for_section (sect), -1);
4216 else
4217 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
4218 desc->sym = symbol;
4219 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
4220 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
4221 SET_SYMBOL_REF_CONSTANT (symbol, desc);
4223 /* Construct the MEM. */
4224 desc->mem = def = gen_const_mem (mode, symbol);
4225 set_mem_align (def, align);
4227 /* If we're dropping a label to the constant pool, make sure we
4228 don't delete it. */
4229 if (GET_CODE (x) == LABEL_REF)
4230 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
4232 return copy_rtx (def);
4235 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
4238 get_pool_constant (const_rtx addr)
4240 return SYMBOL_REF_CONSTANT (addr)->constant;
4243 /* Given a constant pool SYMBOL_REF, return the corresponding constant
4244 and whether it has been output or not. */
4247 get_pool_constant_mark (rtx addr, bool *pmarked)
4249 class constant_descriptor_rtx *desc;
4251 desc = SYMBOL_REF_CONSTANT (addr);
4252 *pmarked = (desc->mark != 0);
4253 return desc->constant;
4256 /* Similar, return the mode. */
4258 fixed_size_mode
4259 get_pool_mode (const_rtx addr)
4261 return SYMBOL_REF_CONSTANT (addr)->mode;
4264 /* Return TRUE if and only if the constant pool has no entries. Note
4265 that even entries we might end up choosing not to emit are counted
4266 here, so there is the potential for missed optimizations. */
4268 bool
4269 constant_pool_empty_p (void)
4271 return crtl->varasm.pool->first == NULL;
4274 /* Worker function for output_constant_pool_1. Emit assembly for X
4275 in MODE with known alignment ALIGN. */
4277 static void
4278 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
4280 switch (GET_MODE_CLASS (mode))
4282 case MODE_FLOAT:
4283 case MODE_DECIMAL_FLOAT:
4285 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
4286 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
4287 as_a <scalar_float_mode> (mode), align, false);
4288 break;
4291 case MODE_INT:
4292 case MODE_PARTIAL_INT:
4293 case MODE_FRACT:
4294 case MODE_UFRACT:
4295 case MODE_ACCUM:
4296 case MODE_UACCUM:
4297 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
4298 break;
4300 case MODE_VECTOR_BOOL:
4302 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4304 auto_vec<target_unit, 128> buffer;
4305 buffer.reserve (GET_MODE_SIZE (mode));
4307 bool ok = native_encode_rtx (mode, x, buffer, 0, GET_MODE_SIZE (mode));
4308 gcc_assert (ok);
4310 for (unsigned i = 0; i < GET_MODE_SIZE (mode); i++)
4312 unsigned HOST_WIDE_INT value = buffer[i];
4313 output_constant_pool_2 (byte_mode, gen_int_mode (value, byte_mode),
4314 i == 0 ? align : 1);
4316 break;
4318 case MODE_VECTOR_FLOAT:
4319 case MODE_VECTOR_INT:
4320 case MODE_VECTOR_FRACT:
4321 case MODE_VECTOR_UFRACT:
4322 case MODE_VECTOR_ACCUM:
4323 case MODE_VECTOR_UACCUM:
4325 int i, units;
4326 scalar_mode submode = GET_MODE_INNER (mode);
4327 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4329 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4330 units = GET_MODE_NUNITS (mode);
4332 for (i = 0; i < units; i++)
4334 rtx elt = CONST_VECTOR_ELT (x, i);
4335 output_constant_pool_2 (submode, elt, i ? subalign : align);
4338 break;
4340 default:
4341 gcc_unreachable ();
4345 /* Worker function for output_constant_pool. Emit constant DESC,
4346 giving it ALIGN bits of alignment. */
4348 static void
4349 output_constant_pool_1 (class constant_descriptor_rtx *desc,
4350 unsigned int align)
4352 rtx x, tmp;
4354 x = desc->constant;
4356 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4357 whose CODE_LABEL has been deleted. This can occur if a jump table
4358 is eliminated by optimization. If so, write a constant of zero
4359 instead. Note that this can also happen by turning the
4360 CODE_LABEL into a NOTE. */
4361 /* ??? This seems completely and utterly wrong. Certainly it's
4362 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4363 functioning even with rtx_insn::deleted and friends. */
4365 tmp = x;
4366 switch (GET_CODE (tmp))
4368 case CONST:
4369 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4370 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4371 break;
4372 tmp = XEXP (XEXP (tmp, 0), 0);
4373 /* FALLTHRU */
4375 case LABEL_REF:
4377 rtx_insn *insn = label_ref_label (tmp);
4378 gcc_assert (!insn->deleted ());
4379 gcc_assert (!NOTE_P (insn)
4380 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4381 break;
4384 default:
4385 break;
4388 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4389 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4390 align, desc->labelno, done);
4391 #endif
4393 assemble_align (align);
4395 /* Output the label. */
4396 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4398 /* Output the data.
4399 Pass actual alignment value while emitting string constant to asm code
4400 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4401 assuming that the data is already aligned which prevents the generation
4402 of fix-up table entries. */
4403 output_constant_pool_2 (desc->mode, x, desc->align);
4405 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4406 sections have proper size. */
4407 if (align > GET_MODE_BITSIZE (desc->mode)
4408 && in_section
4409 && (in_section->common.flags & SECTION_MERGE))
4410 assemble_align (align);
4412 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4413 done:
4414 #endif
4415 return;
4418 /* Recompute the offsets of entries in POOL, and the overall size of
4419 POOL. Do this after calling mark_constant_pool to ensure that we
4420 are computing the offset values for the pool which we will actually
4421 emit. */
4423 static void
4424 recompute_pool_offsets (struct rtx_constant_pool *pool)
4426 class constant_descriptor_rtx *desc;
4427 pool->offset = 0;
4429 for (desc = pool->first; desc ; desc = desc->next)
4430 if (desc->mark)
4432 /* Recalculate offset. */
4433 unsigned int align = desc->align;
4434 pool->offset += (align / BITS_PER_UNIT) - 1;
4435 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4436 desc->offset = pool->offset;
4437 pool->offset += GET_MODE_SIZE (desc->mode);
4441 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4442 Emit referenced deferred strings. */
4444 static void
4445 mark_constants_in_pattern (rtx insn)
4447 subrtx_iterator::array_type array;
4448 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4450 const_rtx x = *iter;
4451 if (GET_CODE (x) == SYMBOL_REF)
4453 if (CONSTANT_POOL_ADDRESS_P (x))
4455 class constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4456 if (desc->mark == 0)
4458 desc->mark = 1;
4459 iter.substitute (desc->constant);
4462 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4464 tree decl = SYMBOL_REF_DECL (x);
4465 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4467 n_deferred_constants--;
4468 output_constant_def_contents (CONST_CAST_RTX (x));
4475 /* Look through appropriate parts of INSN, marking all entries in the
4476 constant pool which are actually being used. Entries that are only
4477 referenced by other constants are also marked as used. Emit
4478 deferred strings that are used. */
4480 static void
4481 mark_constants (rtx_insn *insn)
4483 if (!INSN_P (insn))
4484 return;
4486 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4487 insns, not any notes that may be attached. We don't want to mark
4488 a constant just because it happens to appear in a REG_EQUIV note. */
4489 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4491 int i, n = seq->len ();
4492 for (i = 0; i < n; ++i)
4494 rtx subinsn = seq->element (i);
4495 if (INSN_P (subinsn))
4496 mark_constants_in_pattern (subinsn);
4499 else
4500 mark_constants_in_pattern (insn);
4503 /* Look through the instructions for this function, and mark all the
4504 entries in POOL which are actually being used. Emit deferred constants
4505 which have indeed been used. */
4507 static void
4508 mark_constant_pool (void)
4510 rtx_insn *insn;
4512 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4513 return;
4515 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4516 mark_constants (insn);
4519 /* Write all the constants in POOL. */
4521 static void
4522 output_constant_pool_contents (struct rtx_constant_pool *pool)
4524 class constant_descriptor_rtx *desc;
4526 for (desc = pool->first; desc ; desc = desc->next)
4527 if (desc->mark < 0)
4529 #ifdef ASM_OUTPUT_DEF
4530 gcc_checking_assert (TARGET_SUPPORTS_ALIASES);
4532 const char *name = XSTR (desc->sym, 0);
4533 char label[256];
4534 char buffer[256 + 32];
4535 const char *p;
4537 ASM_GENERATE_INTERNAL_LABEL (label, "LC", ~desc->mark);
4538 p = label;
4539 if (desc->offset)
4541 sprintf (buffer, "%s+" HOST_WIDE_INT_PRINT_DEC, p, desc->offset);
4542 p = buffer;
4544 ASM_OUTPUT_DEF (asm_out_file, name, p);
4545 #else
4546 gcc_unreachable ();
4547 #endif
4549 else if (desc->mark)
4551 /* If the constant is part of an object_block, make sure that
4552 the constant has been positioned within its block, but do not
4553 write out its definition yet. output_object_blocks will do
4554 that later. */
4555 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4556 && SYMBOL_REF_BLOCK (desc->sym))
4557 place_block_symbol (desc->sym);
4558 else
4560 switch_to_section (targetm.asm_out.select_rtx_section
4561 (desc->mode, desc->constant, desc->align));
4562 output_constant_pool_1 (desc, desc->align);
4567 struct constant_descriptor_rtx_data {
4568 constant_descriptor_rtx *desc;
4569 target_unit *bytes;
4570 unsigned short size;
4571 unsigned short offset;
4572 unsigned int hash;
4575 /* qsort callback to sort constant_descriptor_rtx_data * vector by
4576 decreasing size. */
4578 static int
4579 constant_descriptor_rtx_data_cmp (const void *p1, const void *p2)
4581 constant_descriptor_rtx_data *const data1
4582 = *(constant_descriptor_rtx_data * const *) p1;
4583 constant_descriptor_rtx_data *const data2
4584 = *(constant_descriptor_rtx_data * const *) p2;
4585 if (data1->size > data2->size)
4586 return -1;
4587 if (data1->size < data2->size)
4588 return 1;
4589 if (data1->hash < data2->hash)
4590 return -1;
4591 gcc_assert (data1->hash > data2->hash);
4592 return 1;
4595 struct const_rtx_data_hasher : nofree_ptr_hash<constant_descriptor_rtx_data>
4597 static hashval_t hash (constant_descriptor_rtx_data *);
4598 static bool equal (constant_descriptor_rtx_data *,
4599 constant_descriptor_rtx_data *);
4602 /* Hash and compare functions for const_rtx_data_htab. */
4604 hashval_t
4605 const_rtx_data_hasher::hash (constant_descriptor_rtx_data *data)
4607 return data->hash;
4610 bool
4611 const_rtx_data_hasher::equal (constant_descriptor_rtx_data *x,
4612 constant_descriptor_rtx_data *y)
4614 if (x->hash != y->hash || x->size != y->size)
4615 return false;
4616 unsigned int align1 = x->desc->align;
4617 unsigned int align2 = y->desc->align;
4618 unsigned int offset1 = (x->offset * BITS_PER_UNIT) & (align1 - 1);
4619 unsigned int offset2 = (y->offset * BITS_PER_UNIT) & (align2 - 1);
4620 if (offset1)
4621 align1 = least_bit_hwi (offset1);
4622 if (offset2)
4623 align2 = least_bit_hwi (offset2);
4624 if (align2 > align1)
4625 return false;
4626 if (memcmp (x->bytes, y->bytes, x->size * sizeof (target_unit)) != 0)
4627 return false;
4628 return true;
4631 /* Attempt to optimize constant pool POOL. If it contains both CONST_VECTOR
4632 constants and scalar constants with the values of CONST_VECTOR elements,
4633 try to alias the scalar constants with the CONST_VECTOR elements. */
4635 static void
4636 optimize_constant_pool (struct rtx_constant_pool *pool)
4638 auto_vec<target_unit, 128> buffer;
4639 auto_vec<constant_descriptor_rtx_data *, 128> vec;
4640 object_allocator<constant_descriptor_rtx_data>
4641 data_pool ("constant_descriptor_rtx_data_pool");
4642 int idx = 0;
4643 size_t size = 0;
4644 for (constant_descriptor_rtx *desc = pool->first; desc; desc = desc->next)
4645 if (desc->mark > 0
4646 && ! (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4647 && SYMBOL_REF_BLOCK (desc->sym)))
4649 buffer.truncate (0);
4650 buffer.reserve (GET_MODE_SIZE (desc->mode));
4651 if (native_encode_rtx (desc->mode, desc->constant, buffer, 0,
4652 GET_MODE_SIZE (desc->mode)))
4654 constant_descriptor_rtx_data *data = data_pool.allocate ();
4655 data->desc = desc;
4656 data->bytes = NULL;
4657 data->size = GET_MODE_SIZE (desc->mode);
4658 data->offset = 0;
4659 data->hash = idx++;
4660 size += data->size;
4661 vec.safe_push (data);
4664 if (idx)
4666 vec.qsort (constant_descriptor_rtx_data_cmp);
4667 unsigned min_size = vec.last ()->size;
4668 target_unit *bytes = XNEWVEC (target_unit, size);
4669 unsigned int i;
4670 constant_descriptor_rtx_data *data;
4671 hash_table<const_rtx_data_hasher> * htab
4672 = new hash_table<const_rtx_data_hasher> (31);
4673 size = 0;
4674 FOR_EACH_VEC_ELT (vec, i, data)
4676 buffer.truncate (0);
4677 native_encode_rtx (data->desc->mode, data->desc->constant,
4678 buffer, 0, data->size);
4679 memcpy (bytes + size, buffer.address (), data->size);
4680 data->bytes = bytes + size;
4681 data->hash = iterative_hash (data->bytes,
4682 data->size * sizeof (target_unit), 0);
4683 size += data->size;
4684 constant_descriptor_rtx_data **slot
4685 = htab->find_slot_with_hash (data, data->hash, INSERT);
4686 if (*slot)
4688 data->desc->mark = ~(*slot)->desc->labelno;
4689 data->desc->offset = (*slot)->offset;
4691 else
4693 unsigned int sz = 1 << floor_log2 (data->size);
4695 *slot = data;
4696 for (sz >>= 1; sz >= min_size; sz >>= 1)
4697 for (unsigned off = 0; off + sz <= data->size; off += sz)
4699 constant_descriptor_rtx_data tmp;
4700 tmp.desc = data->desc;
4701 tmp.bytes = data->bytes + off;
4702 tmp.size = sz;
4703 tmp.offset = off;
4704 tmp.hash = iterative_hash (tmp.bytes,
4705 sz * sizeof (target_unit), 0);
4706 slot = htab->find_slot_with_hash (&tmp, tmp.hash, INSERT);
4707 if (*slot == NULL)
4709 *slot = data_pool.allocate ();
4710 **slot = tmp;
4715 delete htab;
4716 XDELETE (bytes);
4718 data_pool.release ();
4721 /* Mark all constants that are used in the current function, then write
4722 out the function's private constant pool. */
4724 static void
4725 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4726 tree fndecl ATTRIBUTE_UNUSED)
4728 struct rtx_constant_pool *pool = crtl->varasm.pool;
4730 /* It is possible for gcc to call force_const_mem and then to later
4731 discard the instructions which refer to the constant. In such a
4732 case we do not need to output the constant. */
4733 mark_constant_pool ();
4735 /* Having marked the constant pool entries we'll actually emit, we
4736 now need to rebuild the offset information, which may have become
4737 stale. */
4738 recompute_pool_offsets (pool);
4740 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4741 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4742 #endif
4744 output_constant_pool_contents (pool);
4746 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4747 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4748 #endif
4751 /* Write the contents of the shared constant pool. */
4753 void
4754 output_shared_constant_pool (void)
4756 if (optimize
4757 && TARGET_SUPPORTS_ALIASES)
4758 optimize_constant_pool (shared_constant_pool);
4760 output_constant_pool_contents (shared_constant_pool);
4763 /* Determine what kind of relocations EXP may need. */
4766 compute_reloc_for_constant (tree exp)
4768 int reloc = 0, reloc2;
4769 tree tem;
4771 switch (TREE_CODE (exp))
4773 case ADDR_EXPR:
4774 case FDESC_EXPR:
4775 /* Go inside any operations that get_inner_reference can handle and see
4776 if what's inside is a constant: no need to do anything here for
4777 addresses of variables or functions. */
4778 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4779 tem = TREE_OPERAND (tem, 0))
4782 if (TREE_CODE (tem) == MEM_REF
4783 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4785 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4786 break;
4789 if (!targetm.binds_local_p (tem))
4790 reloc |= 2;
4791 else
4792 reloc |= 1;
4793 break;
4795 case PLUS_EXPR:
4796 case POINTER_PLUS_EXPR:
4797 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4798 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4799 break;
4801 case MINUS_EXPR:
4802 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4803 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4804 /* The difference of two local labels is computable at link time. */
4805 if (reloc == 1 && reloc2 == 1)
4806 reloc = 0;
4807 else
4808 reloc |= reloc2;
4809 break;
4811 CASE_CONVERT:
4812 case VIEW_CONVERT_EXPR:
4813 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4814 break;
4816 case CONSTRUCTOR:
4818 unsigned HOST_WIDE_INT idx;
4819 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4820 if (tem != 0)
4821 reloc |= compute_reloc_for_constant (tem);
4823 break;
4825 default:
4826 break;
4828 return reloc;
4831 /* Find all the constants whose addresses are referenced inside of EXP,
4832 and make sure assembler code with a label has been output for each one.
4833 Indicate whether an ADDR_EXPR has been encountered. */
4835 static void
4836 output_addressed_constants (tree exp, int defer)
4838 tree tem;
4840 switch (TREE_CODE (exp))
4842 case ADDR_EXPR:
4843 case FDESC_EXPR:
4844 /* Go inside any operations that get_inner_reference can handle and see
4845 if what's inside is a constant: no need to do anything here for
4846 addresses of variables or functions. */
4847 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4848 tem = TREE_OPERAND (tem, 0))
4851 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4852 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4853 tem = DECL_INITIAL (tem);
4855 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4856 output_constant_def (tem, defer);
4858 if (TREE_CODE (tem) == MEM_REF)
4859 output_addressed_constants (TREE_OPERAND (tem, 0), defer);
4860 break;
4862 case PLUS_EXPR:
4863 case POINTER_PLUS_EXPR:
4864 case MINUS_EXPR:
4865 output_addressed_constants (TREE_OPERAND (exp, 1), defer);
4866 gcc_fallthrough ();
4868 CASE_CONVERT:
4869 case VIEW_CONVERT_EXPR:
4870 output_addressed_constants (TREE_OPERAND (exp, 0), defer);
4871 break;
4873 case CONSTRUCTOR:
4875 unsigned HOST_WIDE_INT idx;
4876 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4877 if (tem != 0)
4878 output_addressed_constants (tem, defer);
4880 break;
4882 default:
4883 break;
4887 /* Whether a constructor CTOR is a valid static constant initializer if all
4888 its elements are. This used to be internal to initializer_constant_valid_p
4889 and has been exposed to let other functions like categorize_ctor_elements
4890 evaluate the property while walking a constructor for other purposes. */
4892 bool
4893 constructor_static_from_elts_p (const_tree ctor)
4895 return (TREE_CONSTANT (ctor)
4896 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4897 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4898 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4901 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4902 tree *cache);
4904 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4905 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4906 which are valid when ENDTYPE is an integer of any size; in
4907 particular, this does not accept a pointer minus a constant. This
4908 returns null_pointer_node if the VALUE is an absolute constant
4909 which can be used to initialize a static variable. Otherwise it
4910 returns NULL. */
4912 static tree
4913 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4915 tree op0, op1;
4917 if (!INTEGRAL_TYPE_P (endtype))
4918 return NULL_TREE;
4920 op0 = TREE_OPERAND (value, 0);
4921 op1 = TREE_OPERAND (value, 1);
4923 /* Like STRIP_NOPS except allow the operand mode to widen. This
4924 works around a feature of fold that simplifies (int)(p1 - p2) to
4925 ((int)p1 - (int)p2) under the theory that the narrower operation
4926 is cheaper. */
4928 while (CONVERT_EXPR_P (op0)
4929 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4931 tree inner = TREE_OPERAND (op0, 0);
4932 if (inner == error_mark_node
4933 || ! INTEGRAL_TYPE_P (TREE_TYPE (op0))
4934 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (op0)))
4935 || ! INTEGRAL_TYPE_P (TREE_TYPE (inner))
4936 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4937 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4938 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4939 break;
4940 op0 = inner;
4943 while (CONVERT_EXPR_P (op1)
4944 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4946 tree inner = TREE_OPERAND (op1, 0);
4947 if (inner == error_mark_node
4948 || ! INTEGRAL_TYPE_P (TREE_TYPE (op1))
4949 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (op1)))
4950 || ! INTEGRAL_TYPE_P (TREE_TYPE (inner))
4951 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4952 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4953 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4954 break;
4955 op1 = inner;
4958 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4959 if (!op0)
4960 return NULL_TREE;
4962 op1 = initializer_constant_valid_p_1 (op1, endtype,
4963 cache ? cache + 2 : NULL);
4964 /* Both initializers must be known. */
4965 if (op1)
4967 if (op0 == op1
4968 && (op0 == null_pointer_node
4969 || TREE_CODE (value) == MINUS_EXPR))
4970 return null_pointer_node;
4972 /* Support differences between labels. */
4973 if (TREE_CODE (op0) == LABEL_DECL
4974 && TREE_CODE (op1) == LABEL_DECL)
4975 return null_pointer_node;
4977 if (TREE_CODE (op0) == STRING_CST
4978 && TREE_CODE (op1) == STRING_CST
4979 && operand_equal_p (op0, op1, 1))
4980 return null_pointer_node;
4983 return NULL_TREE;
4986 /* Helper function of initializer_constant_valid_p.
4987 Return nonzero if VALUE is a valid constant-valued expression
4988 for use in initializing a static variable; one that can be an
4989 element of a "constant" initializer.
4991 Return null_pointer_node if the value is absolute;
4992 if it is relocatable, return the variable that determines the relocation.
4993 We assume that VALUE has been folded as much as possible;
4994 therefore, we do not need to check for such things as
4995 arithmetic-combinations of integers.
4997 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4999 static tree
5000 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
5002 tree ret;
5004 switch (TREE_CODE (value))
5006 case CONSTRUCTOR:
5007 if (constructor_static_from_elts_p (value))
5009 unsigned HOST_WIDE_INT idx;
5010 tree elt;
5011 bool absolute = true;
5013 if (cache && cache[0] == value)
5014 return cache[1];
5015 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
5017 tree reloc;
5018 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
5019 NULL);
5020 if (!reloc
5021 /* An absolute value is required with reverse SSO. */
5022 || (reloc != null_pointer_node
5023 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
5024 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
5026 if (cache)
5028 cache[0] = value;
5029 cache[1] = NULL_TREE;
5031 return NULL_TREE;
5033 if (reloc != null_pointer_node)
5034 absolute = false;
5036 /* For a non-absolute relocation, there is no single
5037 variable that can be "the variable that determines the
5038 relocation." */
5039 if (cache)
5041 cache[0] = value;
5042 cache[1] = absolute ? null_pointer_node : error_mark_node;
5044 return absolute ? null_pointer_node : error_mark_node;
5047 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
5049 case INTEGER_CST:
5050 case VECTOR_CST:
5051 case REAL_CST:
5052 case FIXED_CST:
5053 case STRING_CST:
5054 case COMPLEX_CST:
5055 case RAW_DATA_CST:
5056 return null_pointer_node;
5058 case ADDR_EXPR:
5059 case FDESC_EXPR:
5061 tree op0 = staticp (TREE_OPERAND (value, 0));
5062 if (op0)
5064 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
5065 to be a constant, this is old-skool offsetof-like nonsense. */
5066 if (TREE_CODE (op0) == INDIRECT_REF
5067 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
5068 return null_pointer_node;
5069 /* Taking the address of a nested function involves a trampoline,
5070 unless we don't need or want one. */
5071 if (TREE_CODE (op0) == FUNCTION_DECL
5072 && DECL_STATIC_CHAIN (op0)
5073 && !TREE_NO_TRAMPOLINE (value))
5074 return NULL_TREE;
5075 /* "&{...}" requires a temporary to hold the constructed
5076 object. */
5077 if (TREE_CODE (op0) == CONSTRUCTOR)
5078 return NULL_TREE;
5080 return op0;
5083 case NON_LVALUE_EXPR:
5084 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
5085 endtype, cache);
5087 case VIEW_CONVERT_EXPR:
5089 tree src = TREE_OPERAND (value, 0);
5090 tree src_type = TREE_TYPE (src);
5091 tree dest_type = TREE_TYPE (value);
5093 /* Allow view-conversions from aggregate to non-aggregate type only
5094 if the bit pattern is fully preserved afterwards; otherwise, the
5095 RTL expander won't be able to apply a subsequent transformation
5096 to the underlying constructor. */
5097 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
5099 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
5100 return initializer_constant_valid_p_1 (src, endtype, cache);
5101 else
5102 return NULL_TREE;
5105 /* Allow all other kinds of view-conversion. */
5106 return initializer_constant_valid_p_1 (src, endtype, cache);
5109 CASE_CONVERT:
5111 tree src = TREE_OPERAND (value, 0);
5112 tree src_type = TREE_TYPE (src);
5113 tree dest_type = TREE_TYPE (value);
5115 /* Allow conversions between pointer types and offset types. */
5116 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
5117 || (TREE_CODE (dest_type) == OFFSET_TYPE
5118 && TREE_CODE (src_type) == OFFSET_TYPE))
5119 return initializer_constant_valid_p_1 (src, endtype, cache);
5121 /* Allow length-preserving conversions between integer types and
5122 floating-point types. */
5123 if (((INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
5124 || (SCALAR_FLOAT_TYPE_P (dest_type)
5125 && SCALAR_FLOAT_TYPE_P (src_type)))
5126 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
5127 return initializer_constant_valid_p_1 (src, endtype, cache);
5129 /* Allow conversions between other integer types only if
5130 explicit value. Don't allow sign-extension to a type larger
5131 than word and pointer, there aren't relocations that would
5132 allow to sign extend it to a wider type. */
5133 if (INTEGRAL_TYPE_P (dest_type)
5134 && INTEGRAL_TYPE_P (src_type)
5135 && (TYPE_UNSIGNED (src_type)
5136 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
5137 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
5138 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
5140 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
5141 if (inner == null_pointer_node)
5142 return null_pointer_node;
5143 break;
5146 /* Allow (int) &foo provided int is as wide as a pointer. */
5147 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
5148 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
5149 return initializer_constant_valid_p_1 (src, endtype, cache);
5151 /* Likewise conversions from int to pointers, but also allow
5152 conversions from 0. */
5153 if ((POINTER_TYPE_P (dest_type)
5154 || TREE_CODE (dest_type) == OFFSET_TYPE)
5155 && INTEGRAL_TYPE_P (src_type))
5157 if (TREE_CODE (src) == INTEGER_CST
5158 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
5159 return null_pointer_node;
5160 if (integer_zerop (src))
5161 return null_pointer_node;
5162 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
5163 return initializer_constant_valid_p_1 (src, endtype, cache);
5166 /* Allow conversions to struct or union types if the value
5167 inside is okay. */
5168 if (TREE_CODE (dest_type) == RECORD_TYPE
5169 || TREE_CODE (dest_type) == UNION_TYPE)
5170 return initializer_constant_valid_p_1 (src, endtype, cache);
5172 break;
5174 case POINTER_PLUS_EXPR:
5175 case PLUS_EXPR:
5176 /* Any valid floating-point constants will have been folded by now;
5177 with -frounding-math we hit this with addition of two constants. */
5178 if (TREE_CODE (endtype) == REAL_TYPE)
5179 return NULL_TREE;
5180 if (cache && cache[0] == value)
5181 return cache[1];
5182 if (! INTEGRAL_TYPE_P (endtype)
5183 || ! INTEGRAL_TYPE_P (TREE_TYPE (value))
5184 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
5186 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
5187 tree valid0
5188 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
5189 endtype, ncache);
5190 tree valid1
5191 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
5192 endtype, ncache + 2);
5193 /* If either term is absolute, use the other term's relocation. */
5194 if (valid0 == null_pointer_node)
5195 ret = valid1;
5196 else if (valid1 == null_pointer_node)
5197 ret = valid0;
5198 /* Support narrowing pointer differences. */
5199 else
5200 ret = narrowing_initializer_constant_valid_p (value, endtype,
5201 ncache);
5203 else
5204 /* Support narrowing pointer differences. */
5205 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
5206 if (cache)
5208 cache[0] = value;
5209 cache[1] = ret;
5211 return ret;
5213 case POINTER_DIFF_EXPR:
5214 case MINUS_EXPR:
5215 if (TREE_CODE (endtype) == REAL_TYPE)
5216 return NULL_TREE;
5217 if (cache && cache[0] == value)
5218 return cache[1];
5219 if (! INTEGRAL_TYPE_P (endtype)
5220 || ! INTEGRAL_TYPE_P (TREE_TYPE (value))
5221 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
5223 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
5224 tree valid0
5225 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
5226 endtype, ncache);
5227 tree valid1
5228 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
5229 endtype, ncache + 2);
5230 /* Win if second argument is absolute. */
5231 if (valid1 == null_pointer_node)
5232 ret = valid0;
5233 /* Win if both arguments have the same relocation.
5234 Then the value is absolute. */
5235 else if (valid0 == valid1 && valid0 != 0)
5236 ret = null_pointer_node;
5237 /* Since GCC guarantees that string constants are unique in the
5238 generated code, a subtraction between two copies of the same
5239 constant string is absolute. */
5240 else if (valid0 && TREE_CODE (valid0) == STRING_CST
5241 && valid1 && TREE_CODE (valid1) == STRING_CST
5242 && operand_equal_p (valid0, valid1, 1))
5243 ret = null_pointer_node;
5244 /* Support narrowing differences. */
5245 else
5246 ret = narrowing_initializer_constant_valid_p (value, endtype,
5247 ncache);
5249 else
5250 /* Support narrowing differences. */
5251 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
5252 if (cache)
5254 cache[0] = value;
5255 cache[1] = ret;
5257 return ret;
5259 default:
5260 break;
5263 return NULL_TREE;
5266 /* Return nonzero if VALUE is a valid constant-valued expression
5267 for use in initializing a static variable; one that can be an
5268 element of a "constant" initializer.
5270 Return null_pointer_node if the value is absolute;
5271 if it is relocatable, return the variable that determines the relocation.
5272 We assume that VALUE has been folded as much as possible;
5273 therefore, we do not need to check for such things as
5274 arithmetic-combinations of integers. */
5275 tree
5276 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
5278 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
5280 /* An absolute value is required with reverse storage order. */
5281 if (reloc
5282 && reloc != null_pointer_node
5283 && reverse
5284 && !AGGREGATE_TYPE_P (endtype)
5285 && !VECTOR_TYPE_P (endtype))
5286 reloc = NULL_TREE;
5288 return reloc;
5291 /* Return true if VALUE is a valid constant-valued expression
5292 for use in initializing a static bit-field; one that can be
5293 an element of a "constant" initializer. */
5295 bool
5296 initializer_constant_valid_for_bitfield_p (const_tree value)
5298 /* For bitfields we support integer constants or possibly nested aggregates
5299 of such. */
5300 switch (TREE_CODE (value))
5302 case CONSTRUCTOR:
5304 unsigned HOST_WIDE_INT idx;
5305 const_tree elt;
5307 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
5308 if (!initializer_constant_valid_for_bitfield_p (elt))
5309 return false;
5310 return true;
5313 case INTEGER_CST:
5314 case REAL_CST:
5315 return true;
5317 case VIEW_CONVERT_EXPR:
5318 case NON_LVALUE_EXPR:
5319 return
5320 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
5322 default:
5323 break;
5326 return false;
5329 /* Check if a STRING_CST fits into the field.
5330 Tolerate only the case when the NUL termination
5331 does not fit into the field. */
5333 static bool
5334 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
5336 tree type = TREE_TYPE (string);
5337 tree eltype = TREE_TYPE (type);
5338 unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
5339 unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5340 int len = TREE_STRING_LENGTH (string);
5342 if (elts != 1 && elts != 2 && elts != 4)
5343 return false;
5344 if (len < 0 || len % elts != 0)
5345 return false;
5346 if (size < (unsigned)len)
5347 return false;
5348 if (mem_size != size)
5349 return false;
5350 return true;
5353 /* output_constructor outer state of relevance in recursive calls, typically
5354 for nested aggregate bitfields. */
5356 struct oc_outer_state {
5357 unsigned int bit_offset; /* current position in ... */
5358 int byte; /* ... the outer byte buffer. */
5361 static unsigned HOST_WIDE_INT
5362 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
5363 oc_outer_state *);
5365 /* Output assembler code for constant EXP, with no label.
5366 This includes the pseudo-op such as ".int" or ".byte", and a newline.
5367 Assumes output_addressed_constants has been done on EXP already.
5369 Generate at least SIZE bytes of assembler data, padding at the end
5370 with zeros if necessary. SIZE must always be specified. The returned
5371 value is the actual number of bytes of assembler data generated, which
5372 may be bigger than SIZE if the object contains a variable length field.
5374 SIZE is important for structure constructors,
5375 since trailing members may have been omitted from the constructor.
5376 It is also important for initialization of arrays from string constants
5377 since the full length of the string constant might not be wanted.
5378 It is also needed for initialization of unions, where the initializer's
5379 type is just one member, and that may not be as long as the union.
5381 There a case in which we would fail to output exactly SIZE bytes:
5382 for a structure constructor that wants to produce more than SIZE bytes.
5383 But such constructors will never be generated for any possible input.
5385 ALIGN is the alignment of the data in bits.
5387 If REVERSE is true, EXP is output in reverse storage order. */
5389 static unsigned HOST_WIDE_INT
5390 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5391 bool reverse, bool merge_strings)
5393 enum tree_code code;
5394 unsigned HOST_WIDE_INT thissize;
5395 rtx cst;
5397 if (size == 0 || flag_syntax_only)
5398 return size;
5400 /* See if we're trying to initialize a pointer in a non-default mode
5401 to the address of some declaration somewhere. If the target says
5402 the mode is valid for pointers, assume the target has a way of
5403 resolving it. */
5404 if (TREE_CODE (exp) == NOP_EXPR
5405 && POINTER_TYPE_P (TREE_TYPE (exp))
5406 && targetm.addr_space.valid_pointer_mode
5407 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5408 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5410 tree saved_type = TREE_TYPE (exp);
5412 /* Peel off any intermediate conversions-to-pointer for valid
5413 pointer modes. */
5414 while (TREE_CODE (exp) == NOP_EXPR
5415 && POINTER_TYPE_P (TREE_TYPE (exp))
5416 && targetm.addr_space.valid_pointer_mode
5417 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5418 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5419 exp = TREE_OPERAND (exp, 0);
5421 /* If what we're left with is the address of something, we can
5422 convert the address to the final type and output it that
5423 way. */
5424 if (TREE_CODE (exp) == ADDR_EXPR)
5425 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
5426 /* Likewise for constant ints. */
5427 else if (TREE_CODE (exp) == INTEGER_CST)
5428 exp = fold_convert (saved_type, exp);
5432 /* Eliminate any conversions since we'll be outputting the underlying
5433 constant. */
5434 while (CONVERT_EXPR_P (exp)
5435 || TREE_CODE (exp) == NON_LVALUE_EXPR
5436 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
5438 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
5439 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
5441 /* Make sure eliminating the conversion is really a no-op, except with
5442 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
5443 union types to allow for Ada unchecked unions. */
5444 if (type_size > op_size
5445 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
5446 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
5447 /* Keep the conversion. */
5448 break;
5449 else
5450 exp = TREE_OPERAND (exp, 0);
5453 code = TREE_CODE (TREE_TYPE (exp));
5454 thissize = int_size_in_bytes (TREE_TYPE (exp));
5456 /* Allow a constructor with no elements for any data type.
5457 This means to fill the space with zeros. */
5458 if (TREE_CODE (exp) == CONSTRUCTOR
5459 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
5461 assemble_zeros (size);
5462 return size;
5465 if (TREE_CODE (exp) == FDESC_EXPR)
5467 #ifdef ASM_OUTPUT_FDESC
5468 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
5469 tree decl = TREE_OPERAND (exp, 0);
5470 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
5471 #else
5472 gcc_unreachable ();
5473 #endif
5474 return size;
5477 /* Now output the underlying data. If we've handling the padding, return.
5478 Otherwise, break and ensure SIZE is the size written. */
5479 switch (code)
5481 case BOOLEAN_TYPE:
5482 case INTEGER_TYPE:
5483 case ENUMERAL_TYPE:
5484 case POINTER_TYPE:
5485 case REFERENCE_TYPE:
5486 case OFFSET_TYPE:
5487 case FIXED_POINT_TYPE:
5488 case NULLPTR_TYPE:
5489 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5490 if (reverse)
5491 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5492 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5493 error ("initializer for integer/fixed-point value is too complicated");
5494 break;
5496 case REAL_TYPE:
5497 gcc_assert (size == thissize);
5498 if (TREE_CODE (exp) != REAL_CST)
5499 error ("initializer for floating value is not a floating constant");
5500 else
5501 assemble_real (TREE_REAL_CST (exp),
5502 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
5503 align, reverse);
5504 break;
5506 case COMPLEX_TYPE:
5507 output_constant (TREE_REALPART (exp), thissize / 2, align,
5508 reverse, false);
5509 output_constant (TREE_IMAGPART (exp), thissize / 2,
5510 min_align (align, BITS_PER_UNIT * (thissize / 2)),
5511 reverse, false);
5512 break;
5514 case BITINT_TYPE:
5515 if (TREE_CODE (exp) != INTEGER_CST)
5516 error ("initializer for %<_BitInt(%d)%> value is not an integer "
5517 "constant", TYPE_PRECISION (TREE_TYPE (exp)));
5518 else
5520 struct bitint_info info;
5521 tree type = TREE_TYPE (exp);
5522 bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
5523 gcc_assert (ok);
5524 scalar_int_mode limb_mode
5525 = as_a <scalar_int_mode> (info.abi_limb_mode);
5526 if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode))
5528 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5529 if (reverse)
5530 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5531 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5532 error ("initializer for integer/fixed-point value is too "
5533 "complicated");
5534 break;
5536 int prec = GET_MODE_PRECISION (limb_mode);
5537 int cnt = CEIL (TYPE_PRECISION (type), prec);
5538 tree limb_type = build_nonstandard_integer_type (prec, 1);
5539 int elt_size = GET_MODE_SIZE (limb_mode);
5540 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (limb_mode));
5541 thissize = 0;
5542 if (prec == HOST_BITS_PER_WIDE_INT)
5543 for (int i = 0; i < cnt; i++)
5545 int idx = (info.big_endian ^ reverse) ? cnt - 1 - i : i;
5546 tree c;
5547 if (idx >= TREE_INT_CST_EXT_NUNITS (exp))
5548 c = build_int_cst (limb_type,
5549 tree_int_cst_sgn (exp) < 0 ? -1 : 0);
5550 else
5551 c = build_int_cst (limb_type,
5552 TREE_INT_CST_ELT (exp, idx));
5553 output_constant (c, elt_size, nalign, reverse, false);
5554 thissize += elt_size;
5556 else
5557 for (int i = 0; i < cnt; i++)
5559 int idx = (info.big_endian ^ reverse) ? cnt - 1 - i : i;
5560 wide_int w = wi::rshift (wi::to_wide (exp), idx * prec,
5561 TYPE_SIGN (TREE_TYPE (exp)));
5562 tree c = wide_int_to_tree (limb_type,
5563 wide_int::from (w, prec, UNSIGNED));
5564 output_constant (c, elt_size, nalign, reverse, false);
5565 thissize += elt_size;
5568 break;
5570 case ARRAY_TYPE:
5571 case VECTOR_TYPE:
5572 switch (TREE_CODE (exp))
5574 case CONSTRUCTOR:
5575 return output_constructor (exp, size, align, reverse, NULL);
5576 case STRING_CST:
5577 thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5578 if (merge_strings
5579 && (thissize == 0
5580 || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5581 thissize++;
5582 gcc_checking_assert (check_string_literal (exp, size));
5583 assemble_string (TREE_STRING_POINTER (exp), thissize);
5584 break;
5585 case VECTOR_CST:
5587 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5588 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5589 int elt_size = GET_MODE_SIZE (inner);
5590 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5591 reverse, false);
5592 thissize = elt_size;
5593 /* Static constants must have a fixed size. */
5594 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5595 for (unsigned int i = 1; i < nunits; i++)
5597 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5598 reverse, false);
5599 thissize += elt_size;
5601 break;
5603 default:
5604 gcc_unreachable ();
5606 break;
5608 case RECORD_TYPE:
5609 case UNION_TYPE:
5610 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5611 return output_constructor (exp, size, align, reverse, NULL);
5613 case ERROR_MARK:
5614 return 0;
5616 default:
5617 gcc_unreachable ();
5620 if (size > thissize)
5621 assemble_zeros (size - thissize);
5623 return size;
5626 /* Subroutine of output_constructor, used for computing the size of
5627 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
5628 type with an unspecified upper bound. */
5630 static unsigned HOST_WIDE_INT
5631 array_size_for_constructor (tree val)
5633 tree max_index;
5634 unsigned HOST_WIDE_INT cnt;
5635 tree index, value, tmp;
5636 offset_int i;
5638 /* This code used to attempt to handle string constants that are not
5639 arrays of single-bytes, but nothing else does, so there's no point in
5640 doing it here. */
5641 if (TREE_CODE (val) == STRING_CST)
5642 return TREE_STRING_LENGTH (val);
5644 max_index = NULL_TREE;
5645 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5647 if (TREE_CODE (index) == RANGE_EXPR)
5648 index = TREE_OPERAND (index, 1);
5649 if (value && TREE_CODE (value) == RAW_DATA_CST)
5650 index = size_binop (PLUS_EXPR, index,
5651 build_int_cst (TREE_TYPE (index),
5652 RAW_DATA_LENGTH (value) - 1));
5653 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5654 max_index = index;
5657 if (max_index == NULL_TREE)
5658 return 0;
5660 /* Compute the total number of array elements. */
5661 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5662 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5664 /* Multiply by the array element unit size to find number of bytes. */
5665 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5667 gcc_assert (wi::fits_uhwi_p (i));
5668 return i.to_uhwi ();
5671 /* Other datastructures + helpers for output_constructor. */
5673 /* output_constructor local state to support interaction with helpers. */
5675 struct oc_local_state {
5677 /* Received arguments. */
5678 tree exp; /* Constructor expression. */
5679 tree type; /* Type of constructor expression. */
5680 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5681 unsigned int align; /* Known initial alignment. */
5682 tree min_index; /* Lower bound if specified for an array. */
5684 /* Output processing state. */
5685 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5686 int byte; /* Part of a bitfield byte yet to be output. */
5687 int last_relative_index; /* Implicit or explicit index of the last
5688 array element output within a bitfield. */
5689 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5690 bool reverse; /* Whether reverse storage order is in use. */
5692 /* Current element. */
5693 tree field; /* Current field decl in a record. */
5694 tree val; /* Current element value. */
5695 tree index; /* Current element index. */
5699 /* Helper for output_constructor. From the current LOCAL state, output a
5700 RANGE_EXPR element. */
5702 static void
5703 output_constructor_array_range (oc_local_state *local)
5705 /* Perform the index calculation in modulo arithmetic but
5706 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5707 but we are using an unsigned sizetype. */
5708 unsigned prec = TYPE_PRECISION (sizetype);
5709 offset_int idx = wi::sext (wi::to_offset (TREE_OPERAND (local->index, 0))
5710 - wi::to_offset (local->min_index), prec);
5711 tree valtype = TREE_TYPE (local->val);
5712 HOST_WIDE_INT fieldpos
5713 = (idx * wi::to_offset (TYPE_SIZE_UNIT (valtype))).to_short_addr ();
5715 /* Advance to offset of this element. */
5716 if (fieldpos > local->total_bytes)
5718 assemble_zeros (fieldpos - local->total_bytes);
5719 local->total_bytes = fieldpos;
5721 else
5722 /* Must not go backwards. */
5723 gcc_assert (fieldpos == local->total_bytes);
5725 unsigned HOST_WIDE_INT fieldsize
5726 = int_size_in_bytes (TREE_TYPE (local->type));
5728 HOST_WIDE_INT lo_index
5729 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5730 HOST_WIDE_INT hi_index
5731 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5732 HOST_WIDE_INT index;
5734 unsigned int align2
5735 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5737 for (index = lo_index; index <= hi_index; index++)
5739 /* Output the element's initial value. */
5740 if (local->val == NULL_TREE)
5741 assemble_zeros (fieldsize);
5742 else
5743 fieldsize = output_constant (local->val, fieldsize, align2,
5744 local->reverse, false);
5746 /* Count its size. */
5747 local->total_bytes += fieldsize;
5751 /* Helper for output_constructor. From the current LOCAL state, output a
5752 field element that is not true bitfield or part of an outer one. */
5754 static void
5755 output_constructor_regular_field (oc_local_state *local)
5757 /* Field size and position. Since this structure is static, we know the
5758 positions are constant. */
5759 unsigned HOST_WIDE_INT fieldsize;
5760 HOST_WIDE_INT fieldpos;
5762 unsigned int align2;
5764 /* Output any buffered-up bit-fields preceding this element. */
5765 if (local->byte_buffer_in_use)
5767 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5768 local->total_bytes++;
5769 local->byte_buffer_in_use = false;
5772 if (local->index != NULL_TREE)
5774 /* Perform the index calculation in modulo arithmetic but
5775 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5776 but we are using an unsigned sizetype. */
5777 unsigned prec = TYPE_PRECISION (sizetype);
5778 offset_int idx = wi::sext (wi::to_offset (local->index)
5779 - wi::to_offset (local->min_index), prec);
5780 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5781 .to_short_addr ();
5783 else if (local->field != NULL_TREE)
5784 fieldpos = int_byte_position (local->field);
5785 else
5786 fieldpos = 0;
5788 /* Advance to offset of this element.
5789 Note no alignment needed in an array, since that is guaranteed
5790 if each element has the proper size. */
5791 if (local->field != NULL_TREE || local->index != NULL_TREE)
5793 if (fieldpos > local->total_bytes)
5795 assemble_zeros (fieldpos - local->total_bytes);
5796 local->total_bytes = fieldpos;
5798 else
5799 /* Must not go backwards. */
5800 gcc_assert (fieldpos == local->total_bytes);
5803 /* Find the alignment of this element. */
5804 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5806 /* Determine size this element should occupy. */
5807 if (local->field)
5809 fieldsize = 0;
5811 /* If this is an array with an unspecified upper bound,
5812 the initializer determines the size. */
5813 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5814 but we cannot do this until the deprecated support for
5815 initializing zero-length array members is removed. */
5816 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5817 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5818 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5820 unsigned HOST_WIDE_INT fldsize
5821 = array_size_for_constructor (local->val);
5822 fieldsize = int_size_in_bytes (TREE_TYPE (local->val));
5823 /* In most cases fieldsize == fldsize as the size of the initializer
5824 determines how many elements the flexible array member has. For
5825 C++ fldsize can be smaller though, if the last or several last or
5826 all initializers of the flexible array member have side-effects
5827 and the FE splits them into dynamic initialization. */
5828 gcc_checking_assert (fieldsize >= fldsize);
5829 /* Given a non-empty initialization, this field had better
5830 be last. Given a flexible array member, the next field
5831 on the chain is a TYPE_DECL of the enclosing struct. */
5832 const_tree next = DECL_CHAIN (local->field);
5833 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5835 else
5836 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5838 else
5839 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5841 /* Output the element's initial value. */
5842 if (local->val == NULL_TREE)
5843 assemble_zeros (fieldsize);
5844 else if (local->val && TREE_CODE (local->val) == RAW_DATA_CST)
5846 fieldsize *= RAW_DATA_LENGTH (local->val);
5847 assemble_string (RAW_DATA_POINTER (local->val),
5848 RAW_DATA_LENGTH (local->val));
5850 else
5851 fieldsize = output_constant (local->val, fieldsize, align2,
5852 local->reverse, false);
5854 /* Count its size. */
5855 local->total_bytes += fieldsize;
5858 /* Helper for output_constructor. From the LOCAL state, output an element
5859 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5860 from the start of a possibly ongoing outer byte buffer. */
5862 static void
5863 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5865 /* Bit size of this element. */
5866 HOST_WIDE_INT ebitsize
5867 = (local->field
5868 ? tree_to_uhwi (DECL_SIZE (local->field))
5869 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5871 /* Relative index of this element if this is an array component. */
5872 HOST_WIDE_INT relative_index
5873 = (local->field
5875 : (local->index
5876 ? tree_to_uhwi (local->index) - tree_to_uhwi (local->min_index)
5877 : local->last_relative_index + 1));
5879 /* Bit position of this element from the start of the containing
5880 constructor. */
5881 HOST_WIDE_INT constructor_relative_ebitpos
5882 = (local->field
5883 ? int_bit_position (local->field)
5884 : ebitsize * relative_index);
5886 /* Bit position of this element from the start of a possibly ongoing
5887 outer byte buffer. */
5888 HOST_WIDE_INT byte_relative_ebitpos
5889 = bit_offset + constructor_relative_ebitpos;
5891 /* From the start of a possibly ongoing outer byte buffer, offsets to
5892 the first bit of this element and to the first bit past the end of
5893 this element. */
5894 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5895 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5897 local->last_relative_index = relative_index;
5899 if (local->val == NULL_TREE)
5900 local->val = integer_zero_node;
5902 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5903 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5904 local->val = TREE_OPERAND (local->val, 0);
5906 if (TREE_CODE (local->val) != INTEGER_CST
5907 && TREE_CODE (local->val) != CONSTRUCTOR)
5909 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5910 return;
5913 /* If this field does not start in this (or next) byte, skip some bytes. */
5914 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5916 /* Output remnant of any bit field in previous bytes. */
5917 if (local->byte_buffer_in_use)
5919 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5920 local->total_bytes++;
5921 local->byte_buffer_in_use = false;
5924 /* If still not at proper byte, advance to there. */
5925 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5927 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5928 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5929 local->total_bytes = next_offset / BITS_PER_UNIT;
5933 /* Set up the buffer if necessary. */
5934 if (!local->byte_buffer_in_use)
5936 local->byte = 0;
5937 if (ebitsize > 0)
5938 local->byte_buffer_in_use = true;
5941 /* If this is nested constructor, recurse passing the bit offset and the
5942 pending data, then retrieve the new pending data afterwards. */
5943 if (TREE_CODE (local->val) == CONSTRUCTOR)
5945 oc_outer_state temp_state;
5946 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5947 temp_state.byte = local->byte;
5948 local->total_bytes
5949 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5950 local->byte = temp_state.byte;
5951 return;
5954 /* Otherwise, we must split the element into pieces that fall within
5955 separate bytes, and combine each byte with previous or following
5956 bit-fields. */
5957 while (next_offset < end_offset)
5959 int this_time;
5960 int shift;
5961 unsigned HOST_WIDE_INT value;
5962 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5963 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5965 /* Advance from byte to byte within this element when necessary. */
5966 while (next_byte != local->total_bytes)
5968 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5969 local->total_bytes++;
5970 local->byte = 0;
5973 /* Number of bits we can process at once (all part of the same byte). */
5974 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5975 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5977 /* For big-endian data, take the most significant bits (of the
5978 bits that are significant) first and put them into bytes from
5979 the most significant end. */
5980 shift = end_offset - next_offset - this_time;
5982 /* Don't try to take a bunch of bits that cross
5983 the word boundary in the INTEGER_CST. We can
5984 only select bits from one element. */
5985 if ((shift / HOST_BITS_PER_WIDE_INT)
5986 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5988 const int end = shift + this_time - 1;
5989 shift = end & -HOST_BITS_PER_WIDE_INT;
5990 this_time = end - shift + 1;
5993 /* Now get the bits we want to insert. */
5994 value = wi::extract_uhwi (wi::to_widest (local->val),
5995 shift, this_time);
5997 /* Get the result. This works only when:
5998 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5999 local->byte |= value << (BITS_PER_UNIT - this_time - next_bit);
6001 else
6003 /* On little-endian machines, take the least significant bits of
6004 the value first and pack them starting at the least significant
6005 bits of the bytes. */
6006 shift = next_offset - byte_relative_ebitpos;
6008 /* Don't try to take a bunch of bits that cross
6009 the word boundary in the INTEGER_CST. We can
6010 only select bits from one element. */
6011 if ((shift / HOST_BITS_PER_WIDE_INT)
6012 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
6013 this_time
6014 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
6016 /* Now get the bits we want to insert. */
6017 value = wi::extract_uhwi (wi::to_widest (local->val),
6018 shift, this_time);
6020 /* Get the result. This works only when:
6021 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
6022 local->byte |= value << next_bit;
6025 next_offset += this_time;
6026 local->byte_buffer_in_use = true;
6030 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
6031 Generate at least SIZE bytes, padding if necessary. OUTER designates the
6032 caller output state of relevance in recursive invocations. */
6034 static unsigned HOST_WIDE_INT
6035 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
6036 bool reverse, oc_outer_state *outer)
6038 unsigned HOST_WIDE_INT cnt;
6039 constructor_elt *ce;
6040 oc_local_state local;
6042 /* Setup our local state to communicate with helpers. */
6043 local.exp = exp;
6044 local.type = TREE_TYPE (exp);
6045 local.size = size;
6046 local.align = align;
6047 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
6048 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
6049 else
6050 local.min_index = integer_zero_node;
6052 local.total_bytes = 0;
6053 local.byte_buffer_in_use = outer != NULL;
6054 local.byte = outer ? outer->byte : 0;
6055 local.last_relative_index = -1;
6056 /* The storage order is specified for every aggregate type. */
6057 if (AGGREGATE_TYPE_P (local.type))
6058 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
6059 else
6060 local.reverse = reverse;
6062 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
6064 /* As CE goes through the elements of the constant, FIELD goes through the
6065 structure fields if the constant is a structure. If the constant is a
6066 union, we override this by getting the field from the TREE_LIST element.
6067 But the constant could also be an array. Then FIELD is zero.
6069 There is always a maximum of one element in the chain LINK for unions
6070 (even if the initializer in a source program incorrectly contains
6071 more one). */
6073 if (TREE_CODE (local.type) == RECORD_TYPE)
6074 local.field = TYPE_FIELDS (local.type);
6075 else
6076 local.field = NULL_TREE;
6078 for (cnt = 0;
6079 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
6080 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
6082 local.val = ce->value;
6083 local.index = NULL_TREE;
6085 /* The element in a union constructor specifies the proper field
6086 or index. */
6087 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
6088 local.field = ce->index;
6090 else if (TREE_CODE (local.type) == ARRAY_TYPE)
6091 local.index = ce->index;
6093 if (local.field && flag_verbose_asm)
6094 fprintf (asm_out_file, "%s %s:\n",
6095 ASM_COMMENT_START,
6096 DECL_NAME (local.field)
6097 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
6098 : "<anonymous>");
6100 /* Eliminate the marker that makes a cast not be an lvalue. */
6101 if (local.val != NULL_TREE)
6102 STRIP_NOPS (local.val);
6104 /* Output the current element, using the appropriate helper ... */
6106 /* For an array slice not part of an outer bitfield. */
6107 if (!outer
6108 && local.index != NULL_TREE
6109 && TREE_CODE (local.index) == RANGE_EXPR)
6110 output_constructor_array_range (&local);
6112 /* For a field that is neither a true bitfield nor part of an outer one,
6113 known to be at least byte aligned and multiple-of-bytes long. */
6114 else if (!outer
6115 && (local.field == NULL_TREE
6116 || !CONSTRUCTOR_BITFIELD_P (local.field)))
6117 output_constructor_regular_field (&local);
6119 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
6120 supported for scalar fields, so we may need to convert first. */
6121 else
6123 if (TREE_CODE (local.val) == REAL_CST)
6124 local.val
6125 = fold_unary (VIEW_CONVERT_EXPR,
6126 build_nonstandard_integer_type
6127 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
6128 local.val);
6129 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
6133 /* If we are not at toplevel, save the pending data for our caller.
6134 Otherwise output the pending data and padding zeros as needed. */
6135 if (outer)
6136 outer->byte = local.byte;
6137 else
6139 if (local.byte_buffer_in_use)
6141 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
6142 local.total_bytes++;
6145 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
6147 assemble_zeros (local.size - local.total_bytes);
6148 local.total_bytes = local.size;
6152 return local.total_bytes;
6155 /* Mark DECL as weak. */
6157 static void
6158 mark_weak (tree decl)
6160 if (DECL_WEAK (decl))
6161 return;
6163 struct symtab_node *n = symtab_node::get (decl);
6164 if (n && n->refuse_visibility_changes)
6165 error ("%qD declared weak after being used", decl);
6166 DECL_WEAK (decl) = 1;
6168 if (DECL_RTL_SET_P (decl)
6169 && MEM_P (DECL_RTL (decl))
6170 && XEXP (DECL_RTL (decl), 0)
6171 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
6172 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
6175 /* Merge weak status between NEWDECL and OLDDECL. */
6177 void
6178 merge_weak (tree newdecl, tree olddecl)
6180 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
6182 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
6184 tree *pwd;
6185 /* We put the NEWDECL on the weak_decls list at some point
6186 and OLDDECL as well. Keep just OLDDECL on the list. */
6187 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
6188 if (TREE_VALUE (*pwd) == newdecl)
6190 *pwd = TREE_CHAIN (*pwd);
6191 break;
6194 return;
6197 if (DECL_WEAK (newdecl))
6199 tree wd;
6201 /* NEWDECL is weak, but OLDDECL is not. */
6203 /* If we already output the OLDDECL, we're in trouble; we can't
6204 go back and make it weak. This should never happen in
6205 unit-at-a-time compilation. */
6206 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
6208 /* If we've already generated rtl referencing OLDDECL, we may
6209 have done so in a way that will not function properly with
6210 a weak symbol. Again in unit-at-a-time this should be
6211 impossible. */
6212 gcc_assert (!TREE_USED (olddecl)
6213 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
6215 /* PR 49899: You cannot convert a static function into a weak, public function. */
6216 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
6217 error ("weak declaration of %q+D being applied to a already "
6218 "existing, static definition", newdecl);
6220 if (TARGET_SUPPORTS_WEAK)
6222 /* We put the NEWDECL on the weak_decls list at some point.
6223 Replace it with the OLDDECL. */
6224 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
6225 if (TREE_VALUE (wd) == newdecl)
6227 TREE_VALUE (wd) = olddecl;
6228 break;
6230 /* We may not find the entry on the list. If NEWDECL is a
6231 weak alias, then we will have already called
6232 globalize_decl to remove the entry; in that case, we do
6233 not need to do anything. */
6236 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
6237 mark_weak (olddecl);
6239 else
6240 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
6241 weak. Just update NEWDECL to indicate that it's weak too. */
6242 mark_weak (newdecl);
6245 /* Declare DECL to be a weak symbol. */
6247 void
6248 declare_weak (tree decl)
6250 /* With -fsyntax-only, TREE_ASM_WRITTEN might be set on certain function
6251 decls earlier than normally, but as with -fsyntax-only nothing is really
6252 emitted, there is no harm in marking it weak later. */
6253 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL
6254 || !TREE_ASM_WRITTEN (decl)
6255 || flag_syntax_only);
6256 if (! TREE_PUBLIC (decl))
6258 error ("weak declaration of %q+D must be public", decl);
6259 return;
6261 else if (!TARGET_SUPPORTS_WEAK)
6262 warning (0, "weak declaration of %q+D not supported", decl);
6264 mark_weak (decl);
6265 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
6266 DECL_ATTRIBUTES (decl)
6267 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
6270 static void
6271 weak_finish_1 (tree decl)
6273 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
6274 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6275 #endif
6277 if (! TREE_USED (decl))
6278 return;
6280 #ifdef ASM_WEAKEN_DECL
6281 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
6282 #else
6283 #ifdef ASM_WEAKEN_LABEL
6284 ASM_WEAKEN_LABEL (asm_out_file, name);
6285 #else
6286 #ifdef ASM_OUTPUT_WEAK_ALIAS
6288 static bool warn_once = 0;
6289 if (! warn_once)
6291 warning (0, "only weak aliases are supported in this configuration");
6292 warn_once = 1;
6294 return;
6296 #endif
6297 #endif
6298 #endif
6301 /* Fiven an assembly name, find the decl it is associated with. */
6302 static tree
6303 find_decl (tree target)
6305 symtab_node *node = symtab_node::get_for_asmname (target);
6306 if (node)
6307 return node->decl;
6308 return NULL_TREE;
6311 /* This TREE_LIST contains weakref targets. */
6313 static GTY(()) tree weakref_targets;
6315 /* Emit any pending weak declarations. */
6317 void
6318 weak_finish (void)
6320 tree t;
6322 for (t = weakref_targets; t; t = TREE_CHAIN (t))
6324 tree alias_decl = TREE_PURPOSE (t);
6325 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
6327 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
6328 || TREE_SYMBOL_REFERENCED (target))
6329 /* Remove alias_decl from the weak list, but leave entries for
6330 the target alone. */
6331 target = NULL_TREE;
6332 #ifndef ASM_OUTPUT_WEAKREF
6333 else if (! TREE_SYMBOL_REFERENCED (target))
6335 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
6336 defined, otherwise we and weak_finish_1 would use
6337 different macros. */
6338 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
6339 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
6340 # else
6341 tree decl = find_decl (target);
6343 if (! decl)
6345 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
6346 TREE_CODE (alias_decl), target,
6347 TREE_TYPE (alias_decl));
6349 DECL_EXTERNAL (decl) = 1;
6350 TREE_PUBLIC (decl) = 1;
6351 DECL_ARTIFICIAL (decl) = 1;
6352 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
6353 TREE_USED (decl) = 1;
6356 weak_finish_1 (decl);
6357 # endif
6359 #endif
6362 tree *p;
6363 tree t2;
6365 /* Remove the alias and the target from the pending weak list
6366 so that we do not emit any .weak directives for the former,
6367 nor multiple .weak directives for the latter. */
6368 for (p = &weak_decls; (t2 = *p) ; )
6370 if (TREE_VALUE (t2) == alias_decl
6371 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
6372 *p = TREE_CHAIN (t2);
6373 else
6374 p = &TREE_CHAIN (t2);
6377 /* Remove other weakrefs to the same target, to speed things up. */
6378 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
6380 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
6381 *p = TREE_CHAIN (t2);
6382 else
6383 p = &TREE_CHAIN (t2);
6388 for (t = weak_decls; t; t = TREE_CHAIN (t))
6390 tree decl = TREE_VALUE (t);
6392 weak_finish_1 (decl);
6396 /* Emit the assembly bits to indicate that DECL is globally visible. */
6398 static void
6399 globalize_decl (tree decl)
6402 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
6403 if (DECL_WEAK (decl))
6405 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6406 tree *p, t;
6408 #ifdef ASM_WEAKEN_DECL
6409 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
6410 #else
6411 ASM_WEAKEN_LABEL (asm_out_file, name);
6412 #endif
6414 /* Remove this function from the pending weak list so that
6415 we do not emit multiple .weak directives for it. */
6416 for (p = &weak_decls; (t = *p) ; )
6418 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6419 *p = TREE_CHAIN (t);
6420 else
6421 p = &TREE_CHAIN (t);
6424 /* Remove weakrefs to the same target from the pending weakref
6425 list, for the same reason. */
6426 for (p = &weakref_targets; (t = *p) ; )
6428 if (DECL_ASSEMBLER_NAME (decl)
6429 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6430 *p = TREE_CHAIN (t);
6431 else
6432 p = &TREE_CHAIN (t);
6435 return;
6437 #endif
6439 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
6442 vec<alias_pair, va_gc> *alias_pairs;
6444 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
6445 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
6446 tree node is DECL to have the value of the tree node TARGET. */
6448 void
6449 do_assemble_alias (tree decl, tree target)
6451 tree id;
6453 /* Emulated TLS had better not get this var. */
6454 gcc_assert (!(!targetm.have_tls
6455 && VAR_P (decl)
6456 && DECL_THREAD_LOCAL_P (decl)));
6458 if (TREE_ASM_WRITTEN (decl))
6459 return;
6461 id = DECL_ASSEMBLER_NAME (decl);
6462 ultimate_transparent_alias_target (&id);
6463 ultimate_transparent_alias_target (&target);
6465 /* We must force creation of DECL_RTL for debug info generation, even though
6466 we don't use it here. */
6467 make_decl_rtl (decl);
6469 TREE_ASM_WRITTEN (decl) = 1;
6470 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
6471 TREE_ASM_WRITTEN (id) = 1;
6473 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6475 if (!TREE_SYMBOL_REFERENCED (target))
6476 weakref_targets = tree_cons (decl, target, weakref_targets);
6478 #ifdef ASM_OUTPUT_WEAKREF
6479 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
6480 IDENTIFIER_POINTER (id),
6481 IDENTIFIER_POINTER (target));
6482 #else
6483 if (!TARGET_SUPPORTS_WEAK)
6485 error_at (DECL_SOURCE_LOCATION (decl),
6486 "%qs is not supported in this configuration", "weakref ");
6487 return;
6489 #endif
6490 return;
6493 #ifdef ASM_OUTPUT_DEF
6494 tree orig_decl = decl;
6496 /* Make name accessible from other files, if appropriate. */
6498 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
6500 globalize_decl (decl);
6501 maybe_assemble_visibility (decl);
6503 if (TREE_CODE (decl) == FUNCTION_DECL
6504 && cgraph_node::get (decl)->ifunc_resolver)
6506 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
6507 if (targetm.has_ifunc_p ())
6508 ASM_OUTPUT_TYPE_DIRECTIVE
6509 (asm_out_file, IDENTIFIER_POINTER (id),
6510 IFUNC_ASM_TYPE);
6511 else
6512 #endif
6513 error_at (DECL_SOURCE_LOCATION (decl),
6514 "%qs is not supported on this target", "ifunc");
6517 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
6518 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
6519 # else
6520 ASM_OUTPUT_DEF (asm_out_file,
6521 IDENTIFIER_POINTER (id),
6522 IDENTIFIER_POINTER (target));
6523 # endif
6524 /* If symbol aliases aren't actually supported... */
6525 if (!TARGET_SUPPORTS_ALIASES)
6526 /* ..., 'ASM_OUTPUT_DEF{,_FROM_DECLS}' better have raised an error. */
6527 gcc_checking_assert (seen_error ());
6528 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
6530 const char *name;
6531 tree *p, t;
6533 name = IDENTIFIER_POINTER (id);
6534 # ifdef ASM_WEAKEN_DECL
6535 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
6536 # else
6537 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
6538 # endif
6539 /* Remove this function from the pending weak list so that
6540 we do not emit multiple .weak directives for it. */
6541 for (p = &weak_decls; (t = *p) ; )
6542 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
6543 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6544 *p = TREE_CHAIN (t);
6545 else
6546 p = &TREE_CHAIN (t);
6548 /* Remove weakrefs to the same target from the pending weakref
6549 list, for the same reason. */
6550 for (p = &weakref_targets; (t = *p) ; )
6552 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6553 *p = TREE_CHAIN (t);
6554 else
6555 p = &TREE_CHAIN (t);
6558 #endif
6561 /* Output .symver directive. */
6563 void
6564 do_assemble_symver (tree decl, tree target)
6566 tree id = DECL_ASSEMBLER_NAME (decl);
6567 ultimate_transparent_alias_target (&id);
6568 ultimate_transparent_alias_target (&target);
6569 #ifdef ASM_OUTPUT_SYMVER_DIRECTIVE
6570 ASM_OUTPUT_SYMVER_DIRECTIVE (asm_out_file,
6571 IDENTIFIER_POINTER (target),
6572 IDENTIFIER_POINTER (id));
6573 #else
6574 error ("symver is only supported on ELF platforms");
6575 #endif
6578 /* Emit an assembler directive to make the symbol for DECL an alias to
6579 the symbol for TARGET. */
6581 void
6582 assemble_alias (tree decl, tree target)
6584 tree target_decl;
6586 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6588 tree alias = DECL_ASSEMBLER_NAME (decl);
6590 ultimate_transparent_alias_target (&target);
6592 if (alias == target)
6593 error ("%qs symbol %q+D ultimately targets itself", "weakref", decl);
6594 if (TREE_PUBLIC (decl))
6595 error ("%qs symbol %q+D must have static linkage", "weakref", decl);
6597 else if (!TARGET_SUPPORTS_ALIASES)
6599 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
6600 error_at (DECL_SOURCE_LOCATION (decl),
6601 "alias definitions not supported in this configuration");
6602 TREE_ASM_WRITTEN (decl) = 1;
6603 return;
6604 # else
6605 if (!DECL_WEAK (decl))
6607 /* NB: ifunc_resolver isn't set when an error is detected. */
6608 if (TREE_CODE (decl) == FUNCTION_DECL
6609 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
6610 error_at (DECL_SOURCE_LOCATION (decl),
6611 "%qs is not supported in this configuration", "ifunc");
6612 else
6613 error_at (DECL_SOURCE_LOCATION (decl),
6614 "only weak aliases are supported in this configuration");
6615 TREE_ASM_WRITTEN (decl) = 1;
6616 return;
6618 # endif
6619 gcc_unreachable ();
6621 TREE_USED (decl) = 1;
6623 /* Allow aliases to aliases. */
6624 if (TREE_CODE (decl) == FUNCTION_DECL)
6625 cgraph_node::get_create (decl)->alias = true;
6626 else
6627 varpool_node::get_create (decl)->alias = true;
6629 /* If the target has already been emitted, we don't have to queue the
6630 alias. This saves a tad of memory. */
6631 if (symtab->global_info_ready)
6632 target_decl = find_decl (target);
6633 else
6634 target_decl= NULL;
6635 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6636 || symtab->state >= EXPANSION)
6637 do_assemble_alias (decl, target);
6638 else
6640 alias_pair p = {decl, target};
6641 vec_safe_push (alias_pairs, p);
6645 /* Record and output a table of translations from original function
6646 to its transaction aware clone. Note that tm_pure functions are
6647 considered to be their own clone. */
6649 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6651 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6652 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6654 static int
6655 keep_cache_entry (tree_map *&e)
6657 return ggc_marked_p (e->base.from);
6661 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6663 void
6664 record_tm_clone_pair (tree o, tree n)
6666 struct tree_map **slot, *h;
6668 if (tm_clone_hash == NULL)
6669 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6671 h = ggc_alloc<tree_map> ();
6672 h->hash = htab_hash_pointer (o);
6673 h->base.from = o;
6674 h->to = n;
6676 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6677 *slot = h;
6680 tree
6681 get_tm_clone_pair (tree o)
6683 if (tm_clone_hash)
6685 struct tree_map *h, in;
6687 in.base.from = o;
6688 in.hash = htab_hash_pointer (o);
6689 h = tm_clone_hash->find_with_hash (&in, in.hash);
6690 if (h)
6691 return h->to;
6693 return NULL_TREE;
6696 struct tm_alias_pair
6698 unsigned int uid;
6699 tree from;
6700 tree to;
6704 /* Dump the actual pairs to the .tm_clone_table section. */
6706 static void
6707 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6709 unsigned i;
6710 tm_alias_pair *p;
6711 bool switched = false;
6713 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6715 tree src = p->from;
6716 tree dst = p->to;
6717 struct cgraph_node *src_n = cgraph_node::get (src);
6718 struct cgraph_node *dst_n = cgraph_node::get (dst);
6720 /* The function ipa_tm_create_version() marks the clone as needed if
6721 the original function was needed. But we also mark the clone as
6722 needed if we ever called the clone indirectly through
6723 TM_GETTMCLONE. If neither of these are true, we didn't generate
6724 a clone, and we didn't call it indirectly... no sense keeping it
6725 in the clone table. */
6726 if (!dst_n || !dst_n->definition)
6727 continue;
6729 /* This covers the case where we have optimized the original
6730 function away, and only access the transactional clone. */
6731 if (!src_n || !src_n->definition)
6732 continue;
6734 if (!switched)
6736 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6737 assemble_align (POINTER_SIZE);
6738 switched = true;
6741 assemble_integer (XEXP (DECL_RTL (src), 0),
6742 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6743 assemble_integer (XEXP (DECL_RTL (dst), 0),
6744 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6748 /* Provide a default for the tm_clone_table section. */
6750 section *
6751 default_clone_table_section (void)
6753 return get_named_section (NULL, ".tm_clone_table", 3);
6756 /* Helper comparison function for qsorting by the DECL_UID stored in
6757 alias_pair->emitted_diags. */
6759 static int
6760 tm_alias_pair_cmp (const void *x, const void *y)
6762 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6763 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6764 if (p1->uid < p2->uid)
6765 return -1;
6766 if (p1->uid > p2->uid)
6767 return 1;
6768 return 0;
6771 void
6772 finish_tm_clone_pairs (void)
6774 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6776 if (tm_clone_hash == NULL)
6777 return;
6779 /* We need a determenistic order for the .tm_clone_table, otherwise
6780 we will get bootstrap comparison failures, so dump the hash table
6781 to a vector, sort it, and dump the vector. */
6783 /* Dump the hashtable to a vector. */
6784 tree_map *map;
6785 hash_table<tm_clone_hasher>::iterator iter;
6786 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6788 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6789 tm_alias_pairs.safe_push (p);
6791 /* Sort it. */
6792 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6794 /* Dump it. */
6795 dump_tm_clone_pairs (tm_alias_pairs);
6797 tm_clone_hash->empty ();
6798 tm_clone_hash = NULL;
6799 tm_alias_pairs.release ();
6803 /* Emit an assembler directive to set symbol for DECL visibility to
6804 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6806 void
6807 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6808 int vis ATTRIBUTE_UNUSED)
6810 #ifdef HAVE_GAS_HIDDEN
6811 static const char * const visibility_types[] = {
6812 NULL, "protected", "hidden", "internal"
6815 const char *name, *type;
6816 tree id;
6818 id = DECL_ASSEMBLER_NAME (decl);
6819 ultimate_transparent_alias_target (&id);
6820 name = IDENTIFIER_POINTER (id);
6822 type = visibility_types[vis];
6824 fprintf (asm_out_file, "\t.%s\t", type);
6825 assemble_name (asm_out_file, name);
6826 fprintf (asm_out_file, "\n");
6827 #else
6828 if (!DECL_ARTIFICIAL (decl))
6829 warning (OPT_Wattributes, "visibility attribute not supported "
6830 "in this configuration; ignored");
6831 #endif
6834 /* A helper function to call assemble_visibility when needed for a decl. */
6836 bool
6837 maybe_assemble_visibility (tree decl)
6839 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6840 if (vis != VISIBILITY_DEFAULT)
6842 targetm.asm_out.assemble_visibility (decl, vis);
6843 return true;
6845 else
6846 return false;
6849 /* Returns true if the target configuration supports defining public symbols
6850 so that one of them will be chosen at link time instead of generating a
6851 multiply-defined symbol error, whether through the use of weak symbols or
6852 a target-specific mechanism for having duplicates discarded. */
6854 bool
6855 supports_one_only (void)
6857 if (SUPPORTS_ONE_ONLY)
6858 return true;
6859 if (TARGET_SUPPORTS_WEAK)
6860 return true;
6861 return false;
6864 /* Set up DECL as a public symbol that can be defined in multiple
6865 translation units without generating a linker error. */
6867 void
6868 make_decl_one_only (tree decl, tree comdat_group)
6870 struct symtab_node *symbol;
6871 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6873 TREE_PUBLIC (decl) = 1;
6875 if (VAR_P (decl))
6876 symbol = varpool_node::get_create (decl);
6877 else
6878 symbol = cgraph_node::get_create (decl);
6880 if (SUPPORTS_ONE_ONLY)
6882 #ifdef MAKE_DECL_ONE_ONLY
6883 MAKE_DECL_ONE_ONLY (decl);
6884 #endif
6885 symbol->set_comdat_group (comdat_group);
6887 else if (VAR_P (decl)
6888 && (DECL_INITIAL (decl) == 0
6889 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6890 DECL_COMMON (decl) = 1;
6891 else
6893 gcc_assert (TARGET_SUPPORTS_WEAK);
6894 DECL_WEAK (decl) = 1;
6898 void
6899 init_varasm_once (void)
6901 section_htab = hash_table<section_hasher>::create_ggc (31);
6902 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6903 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6905 shared_constant_pool = create_constant_pool ();
6907 #ifdef TEXT_SECTION_ASM_OP
6908 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6909 TEXT_SECTION_ASM_OP);
6910 #endif
6912 #ifdef DATA_SECTION_ASM_OP
6913 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6914 DATA_SECTION_ASM_OP);
6915 #endif
6917 #ifdef SDATA_SECTION_ASM_OP
6918 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6919 SDATA_SECTION_ASM_OP);
6920 #endif
6922 #ifdef READONLY_DATA_SECTION_ASM_OP
6923 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6924 READONLY_DATA_SECTION_ASM_OP);
6925 #endif
6927 #ifdef CTORS_SECTION_ASM_OP
6928 ctors_section = get_unnamed_section (0, output_section_asm_op,
6929 CTORS_SECTION_ASM_OP);
6930 #endif
6932 #ifdef DTORS_SECTION_ASM_OP
6933 dtors_section = get_unnamed_section (0, output_section_asm_op,
6934 DTORS_SECTION_ASM_OP);
6935 #endif
6937 #ifdef BSS_SECTION_ASM_OP
6938 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6939 output_section_asm_op,
6940 BSS_SECTION_ASM_OP);
6941 #endif
6943 #ifdef SBSS_SECTION_ASM_OP
6944 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6945 output_section_asm_op,
6946 SBSS_SECTION_ASM_OP);
6947 #endif
6949 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6950 | SECTION_COMMON, emit_tls_common);
6951 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6952 | SECTION_COMMON, emit_local);
6953 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6954 | SECTION_COMMON, emit_common);
6956 #if defined ASM_OUTPUT_ALIGNED_BSS
6957 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6958 emit_bss);
6959 #endif
6961 targetm.asm_out.init_sections ();
6963 if (readonly_data_section == NULL)
6964 readonly_data_section = text_section;
6966 #ifdef ASM_OUTPUT_EXTERNAL
6967 pending_assemble_externals_set = new hash_set<tree>;
6968 #endif
6971 /* Determine whether SYMBOL is used in any optimized function. */
6973 static bool
6974 have_optimized_refs (struct symtab_node *symbol)
6976 struct ipa_ref *ref;
6978 for (int i = 0; symbol->iterate_referring (i, ref); i++)
6980 cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
6982 if (cnode && opt_for_fn (cnode->decl, optimize))
6983 return true;
6986 return false;
6989 /* Check if promoting general-dynamic TLS access model to local-dynamic is
6990 desirable for DECL. */
6992 static bool
6993 optimize_dyn_tls_for_decl_p (const_tree decl)
6995 if (cfun)
6996 return optimize;
6997 return symtab->state >= IPA && have_optimized_refs (symtab_node::get (decl));
7001 enum tls_model
7002 decl_default_tls_model (const_tree decl)
7004 enum tls_model kind;
7005 bool is_local;
7007 is_local = targetm.binds_local_p (decl);
7008 if (!flag_shlib)
7010 if (is_local)
7011 kind = TLS_MODEL_LOCAL_EXEC;
7012 else
7013 kind = TLS_MODEL_INITIAL_EXEC;
7016 /* Local dynamic is inefficient when we're not combining the
7017 parts of the address. */
7018 else if (is_local && optimize_dyn_tls_for_decl_p (decl))
7019 kind = TLS_MODEL_LOCAL_DYNAMIC;
7020 else
7021 kind = TLS_MODEL_GLOBAL_DYNAMIC;
7022 if (kind < flag_tls_default)
7023 kind = flag_tls_default;
7025 return kind;
7028 /* Select a set of attributes for section NAME based on the properties
7029 of DECL and whether or not RELOC indicates that DECL's initializer
7030 might contain runtime relocations.
7032 We make the section read-only and executable for a function decl,
7033 read-only for a const data decl, and writable for a non-const data decl. */
7035 unsigned int
7036 default_section_type_flags (tree decl, const char *name, int reloc)
7038 unsigned int flags;
7040 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
7041 flags = SECTION_CODE;
7042 else if (strcmp (name, ".data.rel.ro") == 0
7043 || strcmp (name, ".data.rel.ro.local") == 0)
7044 flags = SECTION_WRITE | SECTION_RELRO;
7045 else if (decl)
7047 enum section_category category
7048 = categorize_decl_for_section (decl, reloc);
7049 if (decl_readonly_section_1 (category))
7050 flags = 0;
7051 else if (category == SECCAT_DATA_REL_RO
7052 || category == SECCAT_DATA_REL_RO_LOCAL)
7053 flags = SECTION_WRITE | SECTION_RELRO;
7054 else
7055 flags = SECTION_WRITE;
7057 else
7058 flags = SECTION_WRITE;
7060 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
7061 flags |= SECTION_LINKONCE;
7063 if (strcmp (name, ".vtable_map_vars") == 0)
7064 flags |= SECTION_LINKONCE;
7066 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7067 flags |= SECTION_TLS | SECTION_WRITE;
7069 if (strcmp (name, ".bss") == 0
7070 || startswith (name, ".bss.")
7071 || startswith (name, ".gnu.linkonce.b.")
7072 || strcmp (name, ".persistent.bss") == 0
7073 || strcmp (name, ".sbss") == 0
7074 || startswith (name, ".sbss.")
7075 || startswith (name, ".gnu.linkonce.sb."))
7076 flags |= SECTION_BSS;
7078 if (strcmp (name, ".tdata") == 0
7079 || startswith (name, ".tdata.")
7080 || startswith (name, ".gnu.linkonce.td."))
7081 flags |= SECTION_TLS;
7083 if (strcmp (name, ".tbss") == 0
7084 || startswith (name, ".tbss.")
7085 || startswith (name, ".gnu.linkonce.tb."))
7086 flags |= SECTION_TLS | SECTION_BSS;
7088 if (strcmp (name, ".noinit") == 0)
7089 flags |= SECTION_WRITE | SECTION_BSS | SECTION_NOTYPE;
7091 if (strcmp (name, ".persistent") == 0)
7092 flags |= SECTION_WRITE | SECTION_NOTYPE;
7094 /* Various sections have special ELF types that the assembler will
7095 assign by default based on the name. They are neither SHT_PROGBITS
7096 nor SHT_NOBITS, so when changing sections we don't want to print a
7097 section type (@progbits or @nobits). Rather than duplicating the
7098 assembler's knowledge of what those special name patterns are, just
7099 let the assembler choose the type if we don't know a specific
7100 reason to set it to something other than the default. SHT_PROGBITS
7101 is the default for sections whose name is not specially known to
7102 the assembler, so it does no harm to leave the choice to the
7103 assembler when @progbits is the best thing we know to use. If
7104 someone is silly enough to emit code or TLS variables to one of
7105 these sections, then don't handle them specially.
7107 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
7108 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
7109 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
7110 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
7111 flags |= SECTION_NOTYPE;
7113 return flags;
7116 /* Return true if the target supports some form of global BSS,
7117 either through bss_noswitch_section, or by selecting a BSS
7118 section in TARGET_ASM_SELECT_SECTION. */
7120 bool
7121 have_global_bss_p (void)
7123 return bss_noswitch_section || targetm.have_switchable_bss_sections;
7126 /* Output assembly to switch to section NAME with attribute FLAGS.
7127 Four variants for common object file formats. */
7129 void
7130 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
7131 unsigned int flags ATTRIBUTE_UNUSED,
7132 tree decl ATTRIBUTE_UNUSED)
7134 /* Some object formats don't support named sections at all. The
7135 front-end should already have flagged this as an error. */
7136 gcc_unreachable ();
7139 #ifndef TLS_SECTION_ASM_FLAG
7140 #define TLS_SECTION_ASM_FLAG 'T'
7141 #endif
7143 void
7144 default_elf_asm_named_section (const char *name, unsigned int flags,
7145 tree decl)
7147 char flagchars[11], *f = flagchars;
7148 unsigned int numeric_value = 0;
7150 /* If we have already declared this section, we can use an
7151 abbreviated form to switch back to it -- unless this section is
7152 part of a COMDAT groups or with SHF_GNU_RETAIN or with SHF_LINK_ORDER,
7153 in which case GAS requires the full declaration every time. */
7154 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
7155 && !(flags & (SECTION_RETAIN | SECTION_LINK_ORDER))
7156 && (flags & SECTION_DECLARED))
7158 fprintf (asm_out_file, "\t.section\t%s\n", name);
7159 return;
7162 /* If we have a machine specific flag, then use the numeric value to pass
7163 this on to GAS. */
7164 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
7165 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
7166 else
7168 if (!(flags & SECTION_DEBUG))
7169 *f++ = 'a';
7170 #if HAVE_GAS_SECTION_EXCLUDE
7171 if (flags & SECTION_EXCLUDE)
7172 *f++ = 'e';
7173 #endif
7174 if (flags & SECTION_WRITE)
7175 *f++ = 'w';
7176 if (flags & SECTION_CODE)
7177 *f++ = 'x';
7178 if (flags & SECTION_SMALL)
7179 *f++ = 's';
7180 if (flags & SECTION_MERGE)
7181 *f++ = 'M';
7182 if (flags & SECTION_STRINGS)
7183 *f++ = 'S';
7184 if (flags & SECTION_TLS)
7185 *f++ = TLS_SECTION_ASM_FLAG;
7186 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
7187 *f++ = 'G';
7188 if (flags & SECTION_RETAIN)
7189 *f++ = 'R';
7190 if (flags & SECTION_LINK_ORDER)
7191 *f++ = 'o';
7192 #ifdef MACH_DEP_SECTION_ASM_FLAG
7193 if (flags & SECTION_MACH_DEP)
7194 *f++ = MACH_DEP_SECTION_ASM_FLAG;
7195 #endif
7196 *f = '\0';
7199 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
7201 /* default_section_type_flags (above) knows which flags need special
7202 handling here, and sets NOTYPE when none of these apply so that the
7203 assembler's logic for default types can apply to user-chosen
7204 section names. */
7205 if (!(flags & SECTION_NOTYPE))
7207 const char *type;
7208 const char *format;
7210 if (flags & SECTION_BSS)
7211 type = "nobits";
7212 else
7213 type = "progbits";
7215 format = ",@%s";
7216 /* On platforms that use "@" as the assembly comment character,
7217 use "%" instead. */
7218 if (strcmp (ASM_COMMENT_START, "@") == 0)
7219 format = ",%%%s";
7220 fprintf (asm_out_file, format, type);
7222 if (flags & SECTION_ENTSIZE)
7223 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
7224 if (flags & SECTION_LINK_ORDER)
7226 /* For now, only section "__patchable_function_entries"
7227 adopts flag SECTION_LINK_ORDER, internal label LPFE*
7228 was emitted in default_print_patchable_function_entry,
7229 just place it here for linked_to section. */
7230 gcc_assert (!strcmp (name, "__patchable_function_entries"));
7231 fprintf (asm_out_file, ",");
7232 char buf[256];
7233 ASM_GENERATE_INTERNAL_LABEL (buf, "LPFE",
7234 current_function_funcdef_no);
7235 assemble_name_raw (asm_out_file, buf);
7237 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
7239 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7240 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
7241 else
7242 fprintf (asm_out_file, ",%s,comdat",
7243 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
7247 putc ('\n', asm_out_file);
7250 void
7251 default_coff_asm_named_section (const char *name, unsigned int flags,
7252 tree decl ATTRIBUTE_UNUSED)
7254 char flagchars[8], *f = flagchars;
7256 if (flags & SECTION_WRITE)
7257 *f++ = 'w';
7258 if (flags & SECTION_CODE)
7259 *f++ = 'x';
7260 *f = '\0';
7262 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
7265 void
7266 default_pe_asm_named_section (const char *name, unsigned int flags,
7267 tree decl)
7269 default_coff_asm_named_section (name, flags, decl);
7271 if (flags & SECTION_LINKONCE)
7273 /* Functions may have been compiled at various levels of
7274 optimization so we can't use `same_size' here.
7275 Instead, have the linker pick one. */
7276 fprintf (asm_out_file, "\t.linkonce %s\n",
7277 (flags & SECTION_CODE ? "discard" : "same_size"));
7281 /* The lame default section selector. */
7283 section *
7284 default_select_section (tree decl, int reloc,
7285 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7287 if (DECL_P (decl))
7289 if (decl_readonly_section (decl, reloc))
7290 return readonly_data_section;
7292 else if (TREE_CODE (decl) == CONSTRUCTOR)
7294 if (! ((flag_pic && reloc)
7295 || !TREE_READONLY (decl)
7296 || !TREE_CONSTANT (decl)))
7297 return readonly_data_section;
7299 else if (TREE_CODE (decl) == STRING_CST)
7300 return readonly_data_section;
7301 else if (! (flag_pic && reloc))
7302 return readonly_data_section;
7304 return data_section;
7307 enum section_category
7308 categorize_decl_for_section (const_tree decl, int reloc)
7310 enum section_category ret;
7312 if (TREE_CODE (decl) == FUNCTION_DECL)
7313 return SECCAT_TEXT;
7314 else if (TREE_CODE (decl) == STRING_CST)
7316 if ((flag_sanitize & SANITIZE_ADDRESS)
7317 && asan_protect_global (CONST_CAST_TREE (decl)))
7318 /* or !flag_merge_constants */
7319 return SECCAT_RODATA;
7320 else
7321 return SECCAT_RODATA_MERGE_STR;
7323 else if (VAR_P (decl))
7325 tree d = CONST_CAST_TREE (decl);
7326 if (bss_initializer_p (decl))
7327 ret = SECCAT_BSS;
7328 else if (! TREE_READONLY (decl)
7329 || (DECL_INITIAL (decl)
7330 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
7332 /* Here the reloc_rw_mask is not testing whether the section should
7333 be read-only or not, but whether the dynamic link will have to
7334 do something. If so, we wish to segregate the data in order to
7335 minimize cache misses inside the dynamic linker. */
7336 if (reloc & targetm.asm_out.reloc_rw_mask ())
7337 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
7338 else
7339 ret = SECCAT_DATA;
7341 else if (reloc & targetm.asm_out.reloc_rw_mask ())
7342 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
7343 else if (reloc || (flag_merge_constants < 2 && !DECL_MERGEABLE (decl))
7344 || ((flag_sanitize & SANITIZE_ADDRESS)
7345 /* PR 81697: for architectures that use section anchors we
7346 need to ignore DECL_RTL_SET_P (decl) for string constants
7347 inside this asan_protect_global call because otherwise
7348 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
7349 section, set DECL_RTL (decl) later on and add DECL to
7350 protected globals via successive asan_protect_global
7351 calls. In this scenario we'll end up with wrong
7352 alignment of these strings at runtime and possible ASan
7353 false positives. */
7354 && asan_protect_global (d, use_object_blocks_p ()
7355 && use_blocks_for_decl_p (d))))
7356 /* C and C++ don't allow different variables to share the same
7357 location. -fmerge-all-constants allows even that (at the
7358 expense of not conforming). */
7359 ret = SECCAT_RODATA;
7360 else if (DECL_INITIAL (decl)
7361 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
7362 ret = SECCAT_RODATA_MERGE_STR_INIT;
7363 else
7364 ret = SECCAT_RODATA_MERGE_CONST;
7366 else if (TREE_CODE (decl) == CONSTRUCTOR)
7368 if ((reloc & targetm.asm_out.reloc_rw_mask ())
7369 || ! TREE_CONSTANT (decl))
7370 ret = SECCAT_DATA;
7371 else
7372 ret = SECCAT_RODATA;
7374 else
7375 ret = SECCAT_RODATA;
7377 /* There are no read-only thread-local sections. */
7378 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7380 /* Note that this would be *just* SECCAT_BSS, except that there's
7381 no concept of a read-only thread-local-data section. */
7382 if (ret == SECCAT_BSS
7383 || DECL_INITIAL (decl) == NULL
7384 || (flag_zero_initialized_in_bss
7385 && initializer_zerop (DECL_INITIAL (decl))))
7386 ret = SECCAT_TBSS;
7387 else
7388 ret = SECCAT_TDATA;
7391 /* If the target uses small data sections, select it. */
7392 else if (targetm.in_small_data_p (decl))
7394 if (ret == SECCAT_BSS)
7395 ret = SECCAT_SBSS;
7396 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
7397 ret = SECCAT_SRODATA;
7398 else
7399 ret = SECCAT_SDATA;
7402 return ret;
7405 static bool
7406 decl_readonly_section_1 (enum section_category category)
7408 switch (category)
7410 case SECCAT_RODATA:
7411 case SECCAT_RODATA_MERGE_STR:
7412 case SECCAT_RODATA_MERGE_STR_INIT:
7413 case SECCAT_RODATA_MERGE_CONST:
7414 case SECCAT_SRODATA:
7415 return true;
7416 default:
7417 return false;
7421 bool
7422 decl_readonly_section (const_tree decl, int reloc)
7424 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
7427 /* Select a section based on the above categorization. */
7429 section *
7430 default_elf_select_section (tree decl, int reloc,
7431 unsigned HOST_WIDE_INT align)
7433 const char *sname;
7435 switch (categorize_decl_for_section (decl, reloc))
7437 case SECCAT_TEXT:
7438 /* We're not supposed to be called on FUNCTION_DECLs. */
7439 gcc_unreachable ();
7440 case SECCAT_RODATA:
7441 return readonly_data_section;
7442 case SECCAT_RODATA_MERGE_STR:
7443 return mergeable_string_section (decl, align, 0);
7444 case SECCAT_RODATA_MERGE_STR_INIT:
7445 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
7446 case SECCAT_RODATA_MERGE_CONST:
7447 return mergeable_constant_section (DECL_MODE (decl), align, 0);
7448 case SECCAT_SRODATA:
7449 sname = ".sdata2";
7450 break;
7451 case SECCAT_DATA:
7452 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7454 sname = ".persistent";
7455 break;
7457 return data_section;
7458 case SECCAT_DATA_REL:
7459 sname = ".data.rel";
7460 break;
7461 case SECCAT_DATA_REL_LOCAL:
7462 sname = ".data.rel.local";
7463 break;
7464 case SECCAT_DATA_REL_RO:
7465 sname = ".data.rel.ro";
7466 break;
7467 case SECCAT_DATA_REL_RO_LOCAL:
7468 sname = ".data.rel.ro.local";
7469 break;
7470 case SECCAT_SDATA:
7471 sname = ".sdata";
7472 break;
7473 case SECCAT_TDATA:
7474 sname = ".tdata";
7475 break;
7476 case SECCAT_BSS:
7477 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7479 sname = ".noinit";
7480 break;
7482 if (bss_section)
7483 return bss_section;
7484 sname = ".bss";
7485 break;
7486 case SECCAT_SBSS:
7487 sname = ".sbss";
7488 break;
7489 case SECCAT_TBSS:
7490 sname = ".tbss";
7491 break;
7492 default:
7493 gcc_unreachable ();
7496 return get_named_section (decl, sname, reloc);
7499 /* Construct a unique section name based on the decl name and the
7500 categorization performed above. */
7502 void
7503 default_unique_section (tree decl, int reloc)
7505 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
7506 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
7507 const char *prefix, *name, *linkonce;
7508 char *string;
7509 tree id;
7511 switch (categorize_decl_for_section (decl, reloc))
7513 case SECCAT_TEXT:
7514 prefix = one_only ? ".t" : ".text";
7515 break;
7516 case SECCAT_RODATA:
7517 case SECCAT_RODATA_MERGE_STR:
7518 case SECCAT_RODATA_MERGE_STR_INIT:
7519 case SECCAT_RODATA_MERGE_CONST:
7520 prefix = one_only ? ".r" : ".rodata";
7521 break;
7522 case SECCAT_SRODATA:
7523 prefix = one_only ? ".s2" : ".sdata2";
7524 break;
7525 case SECCAT_DATA:
7526 prefix = one_only ? ".d" : ".data";
7527 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7529 prefix = one_only ? ".p" : ".persistent";
7530 break;
7532 break;
7533 case SECCAT_DATA_REL:
7534 prefix = one_only ? ".d.rel" : ".data.rel";
7535 break;
7536 case SECCAT_DATA_REL_LOCAL:
7537 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
7538 break;
7539 case SECCAT_DATA_REL_RO:
7540 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
7541 break;
7542 case SECCAT_DATA_REL_RO_LOCAL:
7543 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
7544 break;
7545 case SECCAT_SDATA:
7546 prefix = one_only ? ".s" : ".sdata";
7547 break;
7548 case SECCAT_BSS:
7549 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7551 prefix = one_only ? ".n" : ".noinit";
7552 break;
7554 prefix = one_only ? ".b" : ".bss";
7555 break;
7556 case SECCAT_SBSS:
7557 prefix = one_only ? ".sb" : ".sbss";
7558 break;
7559 case SECCAT_TDATA:
7560 prefix = one_only ? ".td" : ".tdata";
7561 break;
7562 case SECCAT_TBSS:
7563 prefix = one_only ? ".tb" : ".tbss";
7564 break;
7565 default:
7566 gcc_unreachable ();
7569 id = DECL_ASSEMBLER_NAME (decl);
7570 ultimate_transparent_alias_target (&id);
7571 name = IDENTIFIER_POINTER (id);
7572 name = targetm.strip_name_encoding (name);
7574 /* If we're using one_only, then there needs to be a .gnu.linkonce
7575 prefix to the section name. */
7576 linkonce = one_only ? ".gnu.linkonce" : "";
7578 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
7580 set_decl_section_name (decl, string);
7583 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
7585 static int
7586 compute_reloc_for_rtx_1 (const_rtx x)
7588 switch (GET_CODE (x))
7590 case SYMBOL_REF:
7591 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
7592 case LABEL_REF:
7593 return 1;
7594 default:
7595 return 0;
7599 /* Like compute_reloc_for_constant, except for an RTX. The return value
7600 is a mask for which bit 1 indicates a global relocation, and bit 0
7601 indicates a local relocation. Used by default_select_rtx_section
7602 and default_elf_select_rtx_section. */
7604 static int
7605 compute_reloc_for_rtx (const_rtx x)
7607 switch (GET_CODE (x))
7609 case SYMBOL_REF:
7610 case LABEL_REF:
7611 return compute_reloc_for_rtx_1 (x);
7613 case CONST:
7615 int reloc = 0;
7616 subrtx_iterator::array_type array;
7617 FOR_EACH_SUBRTX (iter, array, x, ALL)
7618 reloc |= compute_reloc_for_rtx_1 (*iter);
7619 return reloc;
7622 default:
7623 return 0;
7627 section *
7628 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
7629 rtx x,
7630 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7632 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
7633 return data_section;
7634 else
7635 return readonly_data_section;
7638 section *
7639 default_elf_select_rtx_section (machine_mode mode, rtx x,
7640 unsigned HOST_WIDE_INT align)
7642 int reloc = compute_reloc_for_rtx (x);
7643 tree decl = nullptr;
7644 const char *prefix = nullptr;
7645 int flags = 0;
7647 /* If it is a private COMDAT function symbol reference, call
7648 function_rodata_section for the read-only or relocated read-only
7649 data section associated with function DECL so that the COMDAT
7650 section will be used for the private COMDAT function symbol. */
7651 if (HAVE_COMDAT_GROUP)
7653 if (GET_CODE (x) == CONST
7654 && GET_CODE (XEXP (x, 0)) == PLUS
7655 && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
7656 x = XEXP (XEXP (x, 0), 0);
7658 if (GET_CODE (x) == SYMBOL_REF)
7660 decl = SYMBOL_REF_DECL (x);
7661 if (decl
7662 && (TREE_CODE (decl) != FUNCTION_DECL
7663 || !DECL_COMDAT_GROUP (decl)
7664 || TREE_PUBLIC (decl)))
7665 decl = nullptr;
7669 /* ??? Handle small data here somehow. */
7671 if (reloc & targetm.asm_out.reloc_rw_mask ())
7673 if (decl)
7675 prefix = reloc == 1 ? ".data.rel.ro.local" : ".data.rel.ro";
7676 flags = SECTION_WRITE | SECTION_RELRO;
7678 else if (reloc == 1)
7679 return get_named_section (NULL, ".data.rel.ro.local", 1);
7680 else
7681 return get_named_section (NULL, ".data.rel.ro", 3);
7684 if (decl)
7686 const char *comdat = IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl));
7687 if (!prefix)
7688 prefix = ".rodata";
7689 size_t prefix_len = strlen (prefix);
7690 size_t comdat_len = strlen (comdat);
7691 size_t len = prefix_len + sizeof (".pool.") + comdat_len;
7692 char *name = XALLOCAVEC (char, len);
7693 memcpy (name, prefix, prefix_len);
7694 memcpy (name + prefix_len, ".pool.", sizeof (".pool.") - 1);
7695 memcpy (name + prefix_len + sizeof (".pool.") - 1, comdat,
7696 comdat_len + 1);
7697 return get_section (name, flags | SECTION_LINKONCE, decl);
7700 return mergeable_constant_section (mode, align, 0);
7703 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
7705 void
7706 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
7708 rtx symbol;
7709 int flags;
7711 /* Careful not to prod global register variables. */
7712 if (!MEM_P (rtl))
7713 return;
7714 symbol = XEXP (rtl, 0);
7715 if (GET_CODE (symbol) != SYMBOL_REF)
7716 return;
7718 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
7719 if (TREE_CODE (decl) == FUNCTION_DECL)
7720 flags |= SYMBOL_FLAG_FUNCTION;
7721 if (targetm.binds_local_p (decl))
7722 flags |= SYMBOL_FLAG_LOCAL;
7723 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7724 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
7725 else if (targetm.in_small_data_p (decl))
7726 flags |= SYMBOL_FLAG_SMALL;
7727 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
7728 being PUBLIC, the thing *must* be defined in this translation unit.
7729 Prevent this buglet from being propagated into rtl code as well. */
7730 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
7731 flags |= SYMBOL_FLAG_EXTERNAL;
7733 SYMBOL_REF_FLAGS (symbol) = flags;
7736 /* By default, we do nothing for encode_section_info, so we need not
7737 do anything but discard the '*' marker. */
7739 const char *
7740 default_strip_name_encoding (const char *str)
7742 return str + (*str == '*');
7745 #ifdef ASM_OUTPUT_DEF
7746 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
7747 anchor relative to ".", the current section position. */
7749 void
7750 default_asm_output_anchor (rtx symbol)
7752 gcc_checking_assert (TARGET_SUPPORTS_ALIASES);
7754 char buffer[100];
7756 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7757 SYMBOL_REF_BLOCK_OFFSET (symbol));
7758 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7760 #endif
7762 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
7764 bool
7765 default_use_anchors_for_symbol_p (const_rtx symbol)
7767 tree decl;
7768 section *sect = SYMBOL_REF_BLOCK (symbol)->sect;
7770 /* This function should only be called with non-zero SYMBOL_REF_BLOCK,
7771 furthermore get_block_for_section should not create object blocks
7772 for mergeable sections. */
7773 gcc_checking_assert (sect && !(sect->common.flags & SECTION_MERGE));
7775 /* Don't use anchors for small data sections. The small data register
7776 acts as an anchor for such sections. */
7777 if (sect->common.flags & SECTION_SMALL)
7778 return false;
7780 decl = SYMBOL_REF_DECL (symbol);
7781 if (decl && DECL_P (decl))
7783 /* Don't use section anchors for decls that might be defined or
7784 usurped by other modules. */
7785 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
7786 return false;
7788 /* Don't use section anchors for decls that will be placed in a
7789 small data section. */
7790 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
7791 one above. The problem is that we only use SECTION_SMALL for
7792 sections that should be marked as small in the section directive. */
7793 if (targetm.in_small_data_p (decl))
7794 return false;
7796 /* Don't use section anchors for decls that won't fit inside a single
7797 anchor range to reduce the amount of instructions required to refer
7798 to the entire declaration. */
7799 if (DECL_SIZE_UNIT (decl) == NULL_TREE
7800 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
7801 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
7802 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
7803 return false;
7806 return true;
7809 /* Return true when RESOLUTION indicate that symbol will be bound to the
7810 definition provided by current .o file. */
7812 static bool
7813 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
7815 return (resolution == LDPR_PREVAILING_DEF
7816 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7817 || resolution == LDPR_PREVAILING_DEF_IRONLY);
7820 /* Return true when RESOLUTION indicate that symbol will be bound locally
7821 within current executable or DSO. */
7823 static bool
7824 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7826 return (resolution == LDPR_PREVAILING_DEF
7827 || resolution == LDPR_PREVAILING_DEF_IRONLY
7828 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7829 || resolution == LDPR_PREEMPTED_REG
7830 || resolution == LDPR_PREEMPTED_IR
7831 || resolution == LDPR_RESOLVED_IR
7832 || resolution == LDPR_RESOLVED_EXEC);
7835 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7836 uninitialized common symbol in the executable will still be defined
7837 (through COPY relocation) in the executable. */
7839 bool
7840 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7841 bool extern_protected_data, bool common_local_p)
7843 /* A non-decl is an entry in the constant pool. */
7844 if (!DECL_P (exp))
7845 return true;
7847 /* Weakrefs may not bind locally, even though the weakref itself is always
7848 static and therefore local. Similarly, the resolver for ifunc functions
7849 might resolve to a non-local function.
7850 FIXME: We can resolve the weakref case more curefuly by looking at the
7851 weakref alias. */
7852 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7853 || (!targetm.ifunc_ref_local_ok ()
7854 && TREE_CODE (exp) == FUNCTION_DECL
7855 && cgraph_node::get (exp)
7856 && cgraph_node::get (exp)->ifunc_resolver))
7857 return false;
7859 /* Static variables are always local. */
7860 if (! TREE_PUBLIC (exp))
7861 return true;
7863 /* With resolution file in hand, take look into resolutions.
7864 We can't just return true for resolved_locally symbols,
7865 because dynamic linking might overwrite symbols
7866 in shared libraries. */
7867 bool resolved_locally = false;
7869 bool uninited_common = (DECL_COMMON (exp)
7870 && (DECL_INITIAL (exp) == NULL
7871 || (!in_lto_p
7872 && DECL_INITIAL (exp) == error_mark_node)));
7874 /* A non-external variable is defined locally only if it isn't
7875 uninitialized COMMON variable or common_local_p is true. */
7876 bool defined_locally = (!DECL_EXTERNAL (exp)
7877 && (!uninited_common || common_local_p));
7878 if (symtab_node *node = symtab_node::get (exp))
7880 if (node->in_other_partition)
7881 defined_locally = true;
7882 if (node->can_be_discarded_p ())
7884 else if (resolution_to_local_definition_p (node->resolution))
7885 defined_locally = resolved_locally = true;
7886 else if (resolution_local_p (node->resolution))
7887 resolved_locally = true;
7889 if (defined_locally && weak_dominate && !shlib)
7890 resolved_locally = true;
7892 /* Undefined weak symbols are never defined locally. */
7893 if (DECL_WEAK (exp) && !defined_locally)
7894 return false;
7896 /* A symbol is local if the user has said explicitly that it will be,
7897 or if we have a definition for the symbol. We cannot infer visibility
7898 for undefined symbols. */
7899 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7900 && (TREE_CODE (exp) == FUNCTION_DECL
7901 || !extern_protected_data
7902 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7903 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7904 return true;
7906 /* If PIC, then assume that any global name can be overridden by
7907 symbols resolved from other modules. */
7908 if (shlib)
7909 return false;
7911 /* Variables defined outside this object might not be local. */
7912 if (DECL_EXTERNAL (exp) && !resolved_locally)
7913 return false;
7915 /* Non-dominant weak symbols are not defined locally. */
7916 if (DECL_WEAK (exp) && !resolved_locally)
7917 return false;
7919 /* Uninitialized COMMON variable may be unified with symbols
7920 resolved from other modules. */
7921 if (uninited_common && !resolved_locally)
7922 return false;
7924 /* Otherwise we're left with initialized (or non-common) global data
7925 which is of necessity defined locally. */
7926 return true;
7929 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7930 wrt cross-module name binding. */
7932 bool
7933 default_binds_local_p (const_tree exp)
7935 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7938 /* Similar to default_binds_local_p, but common symbol may be local and
7939 extern protected data is non-local. */
7941 bool
7942 default_binds_local_p_2 (const_tree exp)
7944 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7945 !flag_pic);
7948 bool
7949 default_binds_local_p_1 (const_tree exp, int shlib)
7951 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7954 /* Return true when references to DECL must bind to current definition in
7955 final executable.
7957 The condition is usually equivalent to whether the function binds to the
7958 current module (shared library or executable), that is to binds_local_p.
7959 We use this fact to avoid need for another target hook and implement
7960 the logic using binds_local_p and just special cases where
7961 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7962 the weak definitions (that can be overwritten at linktime by other
7963 definition from different object file) and when resolution info is available
7964 we simply use the knowledge passed to us by linker plugin. */
7965 bool
7966 decl_binds_to_current_def_p (const_tree decl)
7968 gcc_assert (DECL_P (decl));
7969 if (!targetm.binds_local_p (decl))
7970 return false;
7971 if (!TREE_PUBLIC (decl))
7972 return true;
7974 /* When resolution is available, just use it. */
7975 if (symtab_node *node = symtab_node::get (decl))
7977 if (node->resolution != LDPR_UNKNOWN
7978 && !node->can_be_discarded_p ())
7979 return resolution_to_local_definition_p (node->resolution);
7982 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7983 binds locally but still can be overwritten), DECL_COMMON (can be merged
7984 with a non-common definition somewhere in the same module) or
7985 DECL_EXTERNAL.
7986 This rely on fact that binds_local_p behave as decl_replaceable_p
7987 for all other declaration types. */
7988 if (DECL_WEAK (decl))
7989 return false;
7990 if (DECL_COMDAT_GROUP (decl))
7991 return false;
7992 if (DECL_COMMON (decl)
7993 && (DECL_INITIAL (decl) == NULL
7994 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7995 return false;
7996 if (DECL_EXTERNAL (decl))
7997 return false;
7998 return true;
8001 /* A replaceable function or variable is one which may be replaced
8002 at link-time with an entirely different definition, provided that the
8003 replacement has the same type. For example, functions declared
8004 with __attribute__((weak)) on most systems are replaceable.
8005 If SEMANTIC_INTERPOSITION_P is false allow interposition only on
8006 symbols explicitly declared weak.
8008 COMDAT functions are not replaceable, since all definitions of the
8009 function must be equivalent. It is important that COMDAT functions
8010 not be treated as replaceable so that use of C++ template
8011 instantiations is not penalized. */
8013 bool
8014 decl_replaceable_p (tree decl, bool semantic_interposition_p)
8016 gcc_assert (DECL_P (decl));
8017 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
8018 return false;
8019 if (!semantic_interposition_p
8020 && !DECL_WEAK (decl))
8021 return false;
8022 return !decl_binds_to_current_def_p (decl);
8025 /* Default function to output code that will globalize a label. A
8026 target must define GLOBAL_ASM_OP or provide its own function to
8027 globalize a label. */
8028 #ifdef GLOBAL_ASM_OP
8029 void
8030 default_globalize_label (FILE * stream, const char *name)
8032 fputs (GLOBAL_ASM_OP, stream);
8033 assemble_name (stream, name);
8034 putc ('\n', stream);
8036 #endif /* GLOBAL_ASM_OP */
8038 /* Default function to output code that will globalize a declaration. */
8039 void
8040 default_globalize_decl_name (FILE * stream, tree decl)
8042 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
8043 targetm.asm_out.globalize_label (stream, name);
8046 /* Default function to output a label for unwind information. The
8047 default is to do nothing. A target that needs nonlocal labels for
8048 unwind information must provide its own function to do this. */
8049 void
8050 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
8051 tree decl ATTRIBUTE_UNUSED,
8052 int for_eh ATTRIBUTE_UNUSED,
8053 int empty ATTRIBUTE_UNUSED)
8057 /* Default function to output a label to divide up the exception table.
8058 The default is to do nothing. A target that needs/wants to divide
8059 up the table must provide it's own function to do this. */
8060 void
8061 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
8065 /* This is how to output an internal numbered label where PREFIX is
8066 the class of label and LABELNO is the number within the class. */
8068 void
8069 default_generate_internal_label (char *buf, const char *prefix,
8070 unsigned long labelno)
8072 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
8075 /* This is how to output an internal numbered label where PREFIX is
8076 the class of label and LABELNO is the number within the class. */
8078 void
8079 default_internal_label (FILE *stream, const char *prefix,
8080 unsigned long labelno)
8082 char *const buf = (char *) alloca (40 + strlen (prefix));
8083 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
8084 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
8088 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
8090 void
8091 default_asm_declare_constant_name (FILE *file, const char *name,
8092 const_tree exp ATTRIBUTE_UNUSED,
8093 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
8095 assemble_label (file, name);
8098 /* This is the default behavior at the beginning of a file. It's
8099 controlled by two other target-hook toggles. */
8100 void
8101 default_file_start (void)
8103 if (targetm.asm_file_start_app_off
8104 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
8105 fputs (ASM_APP_OFF, asm_out_file);
8107 if (targetm.asm_file_start_file_directive)
8109 /* LTO produced units have no meaningful main_input_filename. */
8110 if (in_lto_p)
8111 output_file_directive (asm_out_file, "<artificial>");
8112 else
8113 output_file_directive (asm_out_file, main_input_filename);
8117 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
8118 which emits a special section directive used to indicate whether or
8119 not this object file needs an executable stack. This is primarily
8120 a GNU extension to ELF but could be used on other targets. */
8122 int trampolines_created;
8124 void
8125 file_end_indicate_exec_stack (void)
8127 unsigned int flags = SECTION_DEBUG;
8128 if (trampolines_created)
8129 flags |= SECTION_CODE;
8131 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
8134 /* Emit a special section directive to indicate that this object file
8135 was compiled with -fsplit-stack. This is used to let the linker
8136 detect calls between split-stack code and non-split-stack code, so
8137 that it can modify the split-stack code to allocate a sufficiently
8138 large stack. We emit another special section if there are any
8139 functions in this file which have the no_split_stack attribute, to
8140 prevent the linker from warning about being unable to convert the
8141 functions if they call non-split-stack code. */
8143 void
8144 file_end_indicate_split_stack (void)
8146 if (flag_split_stack)
8148 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
8149 NULL));
8150 if (saw_no_split_stack)
8151 switch_to_section (get_section (".note.GNU-no-split-stack",
8152 SECTION_DEBUG, NULL));
8156 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
8157 a get_unnamed_section callback. */
8159 void
8160 output_section_asm_op (const char *directive)
8162 fprintf (asm_out_file, "%s\n", directive);
8165 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
8166 the current section is NEW_SECTION. */
8168 void
8169 switch_to_section (section *new_section, tree decl)
8171 bool retain_p;
8172 if ((new_section->common.flags & SECTION_NAMED)
8173 && decl != nullptr
8174 && DECL_P (decl)
8175 && ((retain_p = !!lookup_attribute ("retain",
8176 DECL_ATTRIBUTES (decl)))
8177 != !!(new_section->common.flags & SECTION_RETAIN)))
8179 /* If the SECTION_RETAIN bit doesn't match, switch to a new
8180 section. */
8181 tree used_decl, no_used_decl;
8183 if (retain_p)
8185 new_section->common.flags |= SECTION_RETAIN;
8186 used_decl = decl;
8187 no_used_decl = new_section->named.decl;
8189 else
8191 new_section->common.flags &= ~(SECTION_RETAIN
8192 | SECTION_DECLARED);
8193 used_decl = new_section->named.decl;
8194 no_used_decl = decl;
8196 if (no_used_decl != used_decl)
8198 warning (OPT_Wattributes,
8199 "%+qD without %<retain%> attribute and %qD with "
8200 "%<retain%> attribute are placed in a section with "
8201 "the same name", no_used_decl, used_decl);
8202 inform (DECL_SOURCE_LOCATION (used_decl),
8203 "%qD was declared here", used_decl);
8206 else if (in_section == new_section)
8207 return;
8209 in_section = new_section;
8211 switch (SECTION_STYLE (new_section))
8213 case SECTION_NAMED:
8214 targetm.asm_out.named_section (new_section->named.name,
8215 new_section->named.common.flags,
8216 new_section->named.decl);
8217 break;
8219 case SECTION_UNNAMED:
8220 new_section->unnamed.callback (new_section->unnamed.data);
8221 break;
8223 case SECTION_NOSWITCH:
8224 gcc_unreachable ();
8225 break;
8228 new_section->common.flags |= SECTION_DECLARED;
8231 /* If block symbol SYMBOL has not yet been assigned an offset, place
8232 it at the end of its block. */
8234 void
8235 place_block_symbol (rtx symbol)
8237 unsigned HOST_WIDE_INT size, mask, offset;
8238 class constant_descriptor_rtx *desc;
8239 unsigned int alignment;
8240 struct object_block *block;
8241 tree decl;
8243 gcc_assert (SYMBOL_REF_BLOCK (symbol));
8244 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
8245 return;
8247 /* Work out the symbol's size and alignment. */
8248 if (CONSTANT_POOL_ADDRESS_P (symbol))
8250 desc = SYMBOL_REF_CONSTANT (symbol);
8251 alignment = desc->align;
8252 size = GET_MODE_SIZE (desc->mode);
8254 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
8256 decl = SYMBOL_REF_DECL (symbol);
8257 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
8258 alignment = DECL_ALIGN (decl);
8259 size = get_constant_size (DECL_INITIAL (decl));
8260 if ((flag_sanitize & SANITIZE_ADDRESS)
8261 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8262 && asan_protect_global (DECL_INITIAL (decl)))
8264 size += asan_red_zone_size (size);
8265 alignment = MAX (alignment,
8266 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
8269 else
8271 struct symtab_node *snode;
8272 decl = SYMBOL_REF_DECL (symbol);
8274 snode = symtab_node::get (decl);
8275 if (snode->alias)
8277 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
8279 gcc_assert (MEM_P (target)
8280 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
8281 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
8282 target = XEXP (target, 0);
8283 place_block_symbol (target);
8284 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
8285 return;
8287 alignment = get_variable_align (decl);
8288 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8289 if ((flag_sanitize & SANITIZE_ADDRESS)
8290 && asan_protect_global (decl))
8292 size += asan_red_zone_size (size);
8293 alignment = MAX (alignment,
8294 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
8298 /* Calculate the object's offset from the start of the block. */
8299 block = SYMBOL_REF_BLOCK (symbol);
8300 mask = alignment / BITS_PER_UNIT - 1;
8301 offset = (block->size + mask) & ~mask;
8302 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
8304 /* Record the block's new alignment and size. */
8305 block->alignment = MAX (block->alignment, alignment);
8306 block->size = offset + size;
8308 vec_safe_push (block->objects, symbol);
8311 /* Return the anchor that should be used to address byte offset OFFSET
8312 from the first object in BLOCK. MODEL is the TLS model used
8313 to access it. */
8316 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
8317 enum tls_model model)
8319 char label[100];
8320 unsigned int begin, middle, end;
8321 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
8322 rtx anchor;
8324 /* Work out the anchor's offset. Use an offset of 0 for the first
8325 anchor so that we don't pessimize the case where we take the address
8326 of a variable at the beginning of the block. This is particularly
8327 useful when a block has only one variable assigned to it.
8329 We try to place anchors RANGE bytes apart, so there can then be
8330 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
8331 a ptr_mode offset. With some target settings, the lowest such
8332 anchor might be out of range for the lowest ptr_mode offset;
8333 likewise the highest anchor for the highest offset. Use anchors
8334 at the extreme ends of the ptr_mode range in such cases.
8336 All arithmetic uses unsigned integers in order to avoid
8337 signed overflow. */
8338 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
8339 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
8340 range = max_offset - min_offset + 1;
8341 if (range == 0)
8342 offset = 0;
8343 else
8345 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
8346 if (offset < 0)
8348 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
8349 delta -= delta % range;
8350 if (delta > bias)
8351 delta = bias;
8352 offset = (HOST_WIDE_INT) (-delta);
8354 else
8356 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
8357 delta -= delta % range;
8358 if (delta > bias - 1)
8359 delta = bias - 1;
8360 offset = (HOST_WIDE_INT) delta;
8364 /* Do a binary search to see if there's already an anchor we can use.
8365 Set BEGIN to the new anchor's index if not. */
8366 begin = 0;
8367 end = vec_safe_length (block->anchors);
8368 while (begin != end)
8370 middle = (end + begin) / 2;
8371 anchor = (*block->anchors)[middle];
8372 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
8373 end = middle;
8374 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
8375 begin = middle + 1;
8376 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
8377 end = middle;
8378 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
8379 begin = middle + 1;
8380 else
8381 return anchor;
8384 /* Create a new anchor with a unique label. */
8385 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
8386 anchor = create_block_symbol (ggc_strdup (label), block, offset);
8387 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
8388 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
8390 /* Insert it at index BEGIN. */
8391 vec_safe_insert (block->anchors, begin, anchor);
8392 return anchor;
8395 /* Output the objects in BLOCK. */
8397 static void
8398 output_object_block (struct object_block *block)
8400 class constant_descriptor_rtx *desc;
8401 unsigned int i;
8402 HOST_WIDE_INT offset;
8403 tree decl;
8404 rtx symbol;
8406 if (!block->objects)
8407 return;
8409 /* Switch to the section and make sure that the first byte is
8410 suitably aligned. */
8411 /* Special case VTV comdat sections similar to assemble_variable. */
8412 if (SECTION_STYLE (block->sect) == SECTION_NAMED
8413 && block->sect->named.name
8414 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
8415 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
8416 else
8417 switch_to_section (block->sect, SYMBOL_REF_DECL ((*block->objects)[0]));
8419 gcc_checking_assert (!(block->sect->common.flags & SECTION_MERGE));
8420 assemble_align (block->alignment);
8422 /* Define the values of all anchors relative to the current section
8423 position. */
8424 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
8425 targetm.asm_out.output_anchor (symbol);
8427 /* Output the objects themselves. */
8428 offset = 0;
8429 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
8431 /* Move to the object's offset, padding with zeros if necessary. */
8432 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
8433 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
8434 if (CONSTANT_POOL_ADDRESS_P (symbol))
8436 desc = SYMBOL_REF_CONSTANT (symbol);
8437 /* Pass 1 for align as we have already laid out everything in the block.
8438 So aligning shouldn't be necessary. */
8439 output_constant_pool_1 (desc, 1);
8440 offset += GET_MODE_SIZE (desc->mode);
8442 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
8444 HOST_WIDE_INT size;
8445 decl = SYMBOL_REF_DECL (symbol);
8446 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
8447 DECL_ALIGN (decl), false);
8449 size = get_constant_size (DECL_INITIAL (decl));
8450 offset += size;
8451 if ((flag_sanitize & SANITIZE_ADDRESS)
8452 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8453 && asan_protect_global (DECL_INITIAL (decl)))
8455 size = asan_red_zone_size (size);
8456 assemble_zeros (size);
8457 offset += size;
8460 else
8462 HOST_WIDE_INT size;
8463 decl = SYMBOL_REF_DECL (symbol);
8464 assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
8465 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8466 offset += size;
8467 if ((flag_sanitize & SANITIZE_ADDRESS)
8468 && asan_protect_global (decl))
8470 size = asan_red_zone_size (size);
8471 assemble_zeros (size);
8472 offset += size;
8478 /* A callback for qsort to compare object_blocks. */
8480 static int
8481 output_object_block_compare (const void *x, const void *y)
8483 object_block *p1 = *(object_block * const*)x;
8484 object_block *p2 = *(object_block * const*)y;
8486 if (p1->sect->common.flags & SECTION_NAMED
8487 && !(p2->sect->common.flags & SECTION_NAMED))
8488 return 1;
8490 if (!(p1->sect->common.flags & SECTION_NAMED)
8491 && p2->sect->common.flags & SECTION_NAMED)
8492 return -1;
8494 if (p1->sect->common.flags & SECTION_NAMED
8495 && p2->sect->common.flags & SECTION_NAMED)
8496 return strcmp (p1->sect->named.name, p2->sect->named.name);
8498 unsigned f1 = p1->sect->common.flags;
8499 unsigned f2 = p2->sect->common.flags;
8500 if (f1 == f2)
8501 return 0;
8502 return f1 < f2 ? -1 : 1;
8505 /* Output the definitions of all object_blocks. */
8507 void
8508 output_object_blocks (void)
8510 vec<object_block *, va_heap> v;
8511 v.create (object_block_htab->elements ());
8512 object_block *obj;
8513 hash_table<object_block_hasher>::iterator hi;
8515 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
8516 v.quick_push (obj);
8518 /* Sort them in order to output them in a deterministic manner,
8519 otherwise we may get .rodata sections in different orders with
8520 and without -g. */
8521 v.qsort (output_object_block_compare);
8522 unsigned i;
8523 FOR_EACH_VEC_ELT (v, i, obj)
8524 output_object_block (obj);
8526 v.release ();
8529 /* This function provides a possible implementation of the
8530 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
8531 by -frecord-gcc-switches it creates a new mergeable, string section in the
8532 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
8533 contains the switches in ASCII format.
8535 FIXME: This code does not correctly handle double quote characters
8536 that appear inside strings, (it strips them rather than preserving them).
8537 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
8538 characters - instead it treats them as sub-string separators. Since
8539 we want to emit NUL strings terminators into the object file we have to use
8540 ASM_OUTPUT_SKIP. */
8542 void
8543 elf_record_gcc_switches (const char *options)
8545 section *sec = get_section (targetm.asm_out.record_gcc_switches_section,
8546 SECTION_DEBUG | SECTION_MERGE
8547 | SECTION_STRINGS | (SECTION_ENTSIZE & 1), NULL);
8548 switch_to_section (sec);
8549 ASM_OUTPUT_ASCII (asm_out_file, options, strlen (options) + 1);
8552 /* Emit text to declare externally defined symbols. It is needed to
8553 properly support non-default visibility. */
8554 void
8555 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
8556 tree decl,
8557 const char *name ATTRIBUTE_UNUSED)
8559 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8560 set in order to avoid putting out names that are never really
8561 used. Always output visibility specified in the source. */
8562 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
8563 && (DECL_VISIBILITY_SPECIFIED (decl)
8564 || targetm.binds_local_p (decl)))
8565 maybe_assemble_visibility (decl);
8568 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8570 void
8571 default_asm_output_source_filename (FILE *file, const char *name)
8573 #ifdef ASM_OUTPUT_SOURCE_FILENAME
8574 ASM_OUTPUT_SOURCE_FILENAME (file, name);
8575 #else
8576 fprintf (file, "\t.file\t");
8577 output_quoted_string (file, name);
8578 putc ('\n', file);
8579 #endif
8582 /* Output a file name in the form wanted by System V. */
8584 void
8585 output_file_directive (FILE *asm_file, const char *input_name)
8587 int len;
8588 const char *na;
8590 if (input_name == NULL)
8591 input_name = "<stdin>";
8592 else
8593 input_name = remap_debug_filename (input_name);
8595 len = strlen (input_name);
8596 na = input_name + len;
8598 /* NA gets INPUT_NAME sans directory names. */
8599 while (na > input_name)
8601 if (IS_DIR_SEPARATOR (na[-1]))
8602 break;
8603 na--;
8606 targetm.asm_out.output_source_filename (asm_file, na);
8609 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
8610 EXP. */
8612 make_debug_expr_from_rtl (const_rtx exp)
8614 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
8615 machine_mode mode = GET_MODE (exp);
8616 rtx dval;
8618 DECL_ARTIFICIAL (ddecl) = 1;
8619 if (REG_P (exp) && REG_EXPR (exp))
8620 type = TREE_TYPE (REG_EXPR (exp));
8621 else if (MEM_P (exp) && MEM_EXPR (exp))
8622 type = TREE_TYPE (MEM_EXPR (exp));
8623 else
8624 type = NULL_TREE;
8625 if (type && TYPE_MODE (type) == mode)
8626 TREE_TYPE (ddecl) = type;
8627 else
8628 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
8629 SET_DECL_MODE (ddecl, mode);
8630 dval = gen_rtx_DEBUG_EXPR (mode);
8631 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
8632 SET_DECL_RTL (ddecl, dval);
8633 return dval;
8636 #ifdef ELF_ASCII_ESCAPES
8637 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
8639 void
8640 default_elf_asm_output_limited_string (FILE *f, const char *s)
8642 int escape;
8643 unsigned char c;
8645 fputs (STRING_ASM_OP "\"", f);
8646 while (*s != '\0')
8648 c = *s;
8649 escape = ELF_ASCII_ESCAPES[c];
8650 switch (escape)
8652 case 0:
8653 putc (c, f);
8654 break;
8655 case 1:
8656 putc ('\\', f);
8657 putc ('0'+((c>>6)&7), f);
8658 putc ('0'+((c>>3)&7), f);
8659 putc ('0'+(c&7), f);
8660 break;
8661 default:
8662 putc ('\\', f);
8663 putc (escape, f);
8664 break;
8666 s++;
8668 putc ('\"', f);
8669 putc ('\n', f);
8672 /* Default ASM_OUTPUT_ASCII for ELF targets. */
8674 void
8675 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
8677 const char *limit = s + len;
8678 const char *last_null = NULL;
8679 const char *last_base64 = s;
8680 unsigned bytes_in_chunk = 0;
8681 unsigned char c;
8682 int escape;
8683 bool prev_base64 = false;
8685 for (; s < limit; s++)
8687 const char *p;
8689 if (bytes_in_chunk >= 60)
8691 putc ('\"', f);
8692 putc ('\n', f);
8693 bytes_in_chunk = 0;
8696 if ((uintptr_t) s > (uintptr_t) last_null)
8698 for (p = s; p < limit && *p != '\0'; p++)
8699 continue;
8700 last_null = p;
8702 else
8703 p = last_null;
8705 #if defined(BASE64_ASM_OP) \
8706 && BITS_PER_UNIT == 8 \
8707 && CHAR_BIT == 8 \
8708 && 'A' == 65 \
8709 && 'a' == 97 \
8710 && '0' == 48 \
8711 && '+' == 43 \
8712 && '/' == 47 \
8713 && '=' == 61
8714 if (s >= last_base64)
8716 unsigned cnt = 0;
8717 unsigned char prev_c = ' ';
8718 const char *t;
8719 for (t = s; t < limit && (t - s) < (long) ELF_STRING_LIMIT - 1; t++)
8721 if (t == p && t != s)
8723 if (cnt <= ((unsigned) (t - s) + 1 + 2) / 3 * 4
8724 && (!prev_base64 || (t - s) >= 16)
8725 && ((t - s) > 1 || cnt <= 2))
8727 last_base64 = p;
8728 goto no_base64;
8731 c = *t;
8732 escape = ELF_ASCII_ESCAPES[c];
8733 switch (escape)
8735 case 0:
8736 ++cnt;
8737 break;
8738 case 1:
8739 if (c == 0)
8741 if (prev_c == 0
8742 && t + 1 < limit
8743 && (t + 1 - s) < (long) ELF_STRING_LIMIT - 1)
8744 break;
8745 cnt += 2 + strlen (STRING_ASM_OP) + 1;
8747 else
8748 cnt += 4;
8749 break;
8750 default:
8751 cnt += 2;
8752 break;
8754 prev_c = c;
8756 if (cnt > ((unsigned) (t - s) + 2) / 3 * 4 && (t - s) >= 3)
8758 if (bytes_in_chunk > 0)
8760 putc ('\"', f);
8761 putc ('\n', f);
8762 bytes_in_chunk = 0;
8765 unsigned char buf[(ELF_STRING_LIMIT + 2) / 3 * 4 + 3];
8766 unsigned j = 0;
8767 static const char base64_enc[] =
8768 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
8769 "0123456789+/";
8771 fputs (BASE64_ASM_OP "\"", f);
8772 while (s < t)
8774 unsigned char a = *s;
8775 unsigned char b = 0, c = 0;
8776 if (s < t - 1)
8777 b = s[1];
8778 if (s < t - 2)
8779 c = s[2];
8780 unsigned long v = ((((unsigned long) a) << 16)
8781 | (((unsigned long) b) << 8)
8782 | c);
8783 buf[j++] = base64_enc[(v >> 18) & 63];
8784 buf[j++] = base64_enc[(v >> 12) & 63];
8785 buf[j++] = base64_enc[(v >> 6) & 63];
8786 buf[j++] = base64_enc[v & 63];
8787 if (s >= t - 2)
8789 buf[j - 1] = '=';
8790 if (s >= t - 1)
8791 buf[j - 2] = '=';
8792 break;
8794 s += 3;
8796 memcpy (buf + j, "\"\n", 3);
8797 fputs ((const char *) buf, f);
8798 s = t - 1;
8799 prev_base64 = true;
8800 continue;
8802 last_base64 = t;
8803 no_base64:
8804 prev_base64 = false;
8806 #else
8807 (void) last_base64;
8808 (void) prev_base64;
8809 #endif
8811 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
8813 if (bytes_in_chunk > 0)
8815 putc ('\"', f);
8816 putc ('\n', f);
8817 bytes_in_chunk = 0;
8820 if (p == s && p + 1 < limit && p[1] == '\0')
8822 for (p = s + 2; p < limit && *p == '\0'; p++)
8823 continue;
8824 ASM_OUTPUT_SKIP (f, (unsigned HOST_WIDE_INT) (p - s));
8825 s = p - 1;
8827 else
8829 default_elf_asm_output_limited_string (f, s);
8830 s = p;
8833 else
8835 if (bytes_in_chunk == 0)
8836 fputs (ASCII_DATA_ASM_OP "\"", f);
8838 c = *s;
8839 escape = ELF_ASCII_ESCAPES[c];
8840 switch (escape)
8842 case 0:
8843 putc (c, f);
8844 bytes_in_chunk++;
8845 break;
8846 case 1:
8847 putc ('\\', f);
8848 putc ('0'+((c>>6)&7), f);
8849 putc ('0'+((c>>3)&7), f);
8850 putc ('0'+(c&7), f);
8851 bytes_in_chunk += 4;
8852 break;
8853 default:
8854 putc ('\\', f);
8855 putc (escape, f);
8856 bytes_in_chunk += 2;
8857 break;
8863 if (bytes_in_chunk > 0)
8865 putc ('\"', f);
8866 putc ('\n', f);
8869 #endif
8871 static GTY(()) section *elf_init_array_section;
8872 static GTY(()) section *elf_fini_array_section;
8874 static section *
8875 get_elf_initfini_array_priority_section (int priority,
8876 bool constructor_p)
8878 section *sec;
8879 if (priority != DEFAULT_INIT_PRIORITY)
8881 char buf[18];
8882 sprintf (buf, "%s.%.5u",
8883 constructor_p ? ".init_array" : ".fini_array",
8884 priority);
8885 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8887 else
8889 if (constructor_p)
8891 if (elf_init_array_section == NULL)
8892 elf_init_array_section
8893 = get_section (".init_array",
8894 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8895 sec = elf_init_array_section;
8897 else
8899 if (elf_fini_array_section == NULL)
8900 elf_fini_array_section
8901 = get_section (".fini_array",
8902 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8903 sec = elf_fini_array_section;
8906 return sec;
8909 /* Use .init_array section for constructors. */
8911 void
8912 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8914 section *sec = get_elf_initfini_array_priority_section (priority,
8915 true);
8916 assemble_addr_to_section (symbol, sec);
8919 /* Use .fini_array section for destructors. */
8921 void
8922 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8924 section *sec = get_elf_initfini_array_priority_section (priority,
8925 false);
8926 assemble_addr_to_section (symbol, sec);
8929 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8931 This is a bit of a cheat. The real default is a no-op, but this
8932 hook is the default for all targets with a .ident directive. */
8934 void
8935 default_asm_output_ident_directive (const char *ident_str)
8937 const char *ident_asm_op = "\t.ident\t";
8939 /* If we are still in the front end, do not write out the string
8940 to asm_out_file. Instead, add a fake top-level asm statement.
8941 This allows the front ends to use this hook without actually
8942 writing to asm_out_file, to handle #ident or Pragma Ident. */
8943 if (symtab->state == PARSING)
8945 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8946 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8948 else
8949 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8952 /* Switch to a COMDAT section with COMDAT name of decl.
8954 FIXME: resolve_unique_section needs to deal better with
8955 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8956 that is fixed, this if-else statement can be replaced with
8957 a single call to "switch_to_section (sect)". */
8959 void
8960 switch_to_comdat_section (section *sect, tree decl)
8962 #if defined (OBJECT_FORMAT_ELF)
8963 targetm.asm_out.named_section (sect->named.name,
8964 sect->named.common.flags
8965 | SECTION_LINKONCE,
8966 decl);
8967 in_section = sect;
8968 #else
8969 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8970 Therefore the following check is used.
8971 In case a the target is PE or COFF a comdat group section
8972 is created, e.g. .vtable_map_vars$foo. The linker places
8973 everything in .vtable_map_vars at the end.
8975 A fix could be made in
8976 gcc/config/i386/winnt.cc: mingw_pe_unique_section. */
8977 if (TARGET_PECOFF)
8979 char *name;
8981 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8982 name = ACONCAT ((sect->named.name, "$",
8983 IDENTIFIER_POINTER (decl), NULL));
8984 else
8985 name = ACONCAT ((sect->named.name, "$",
8986 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)),
8987 NULL));
8989 targetm.asm_out.named_section (name,
8990 sect->named.common.flags
8991 | SECTION_LINKONCE,
8992 decl);
8993 in_section = sect;
8995 else
8996 switch_to_section (sect);
8997 #endif
9000 /* This function ensures that vtable_map variables are not only
9001 in the comdat section, but that each variable has its own unique
9002 comdat name. Without this the variables end up in the same section
9003 with a single comdat name. */
9005 static void
9006 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
9008 switch_to_comdat_section(sect, DECL_NAME (decl));
9011 void
9012 varasm_cc_finalize ()
9014 first_global_object_name = nullptr;
9015 weak_global_object_name = nullptr;
9017 const_labelno = 0;
9018 size_directive_output = 0;
9020 last_assemble_variable_decl = NULL_TREE;
9021 first_function_block_is_cold = false;
9022 saw_no_split_stack = false;
9023 text_section = nullptr;
9024 data_section = nullptr;
9025 readonly_data_section = nullptr;
9026 sdata_section = nullptr;
9027 ctors_section = nullptr;
9028 dtors_section = nullptr;
9029 bss_section = nullptr;
9030 sbss_section = nullptr;
9031 tls_comm_section = nullptr;
9032 comm_section = nullptr;
9033 lcomm_section = nullptr;
9034 bss_noswitch_section = nullptr;
9035 exception_section = nullptr;
9036 eh_frame_section = nullptr;
9037 in_section = nullptr;
9038 in_cold_section_p = false;
9039 cold_function_name = NULL_TREE;
9040 unnamed_sections = nullptr;
9041 section_htab = nullptr;
9042 object_block_htab = nullptr;
9043 anchor_labelno = 0;
9044 shared_constant_pool = nullptr;
9045 pending_assemble_externals = NULL_TREE;
9046 pending_libcall_symbols = nullptr;
9048 #ifdef ASM_OUTPUT_EXTERNAL
9049 pending_assemble_externals_processed = false;
9050 delete pending_assemble_externals_set;
9051 pending_assemble_externals_set = nullptr;
9052 #endif
9054 weak_decls = NULL_TREE;
9055 initial_trampoline = nullptr;
9056 const_desc_htab = nullptr;
9057 weakref_targets = NULL_TREE;
9058 alias_pairs = nullptr;
9059 tm_clone_hash = nullptr;
9060 trampolines_created = 0;
9061 elf_init_array_section = nullptr;
9062 elf_fini_array_section = nullptr;
9065 #include "gt-varasm.h"