testsuite: Fix up a testcase to find the right ISO_Fortran_binding.h.
[official-gcc.git] / gcc / varasm.c
blob21be03aeba2cb1ee78eb6bc4077133ba356615de
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2021 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"
65 #ifdef XCOFF_DEBUGGING_INFO
66 #include "xcoffout.h" /* Needed for external data declarations. */
67 #endif
69 /* The (assembler) name of the first globally-visible object output. */
70 extern GTY(()) const char *first_global_object_name;
71 extern GTY(()) const char *weak_global_object_name;
73 const char *first_global_object_name;
74 const char *weak_global_object_name;
76 class addr_const;
77 class constant_descriptor_rtx;
78 struct rtx_constant_pool;
80 #define n_deferred_constants (crtl->varasm.deferred_constants)
82 /* Number for making the label on the next
83 constant that is stored in memory. */
85 static GTY(()) int const_labelno;
87 /* Carry information from ASM_DECLARE_OBJECT_NAME
88 to ASM_FINISH_DECLARE_OBJECT. */
90 int size_directive_output;
92 /* The last decl for which assemble_variable was called,
93 if it did ASM_DECLARE_OBJECT_NAME.
94 If the last call to assemble_variable didn't do that,
95 this holds 0. */
97 tree last_assemble_variable_decl;
99 /* The following global variable indicates if the first basic block
100 in a function belongs to the cold partition or not. */
102 bool first_function_block_is_cold;
104 /* Whether we saw any functions with no_split_stack. */
106 static bool saw_no_split_stack;
108 static const char *strip_reg_name (const char *);
109 static int contains_pointers_p (tree);
110 #ifdef ASM_OUTPUT_EXTERNAL
111 static bool incorporeal_function_p (tree);
112 #endif
113 static void decode_addr_const (tree, class addr_const *);
114 static hashval_t const_hash_1 (const tree);
115 static int compare_constant (const tree, const tree);
116 static void output_constant_def_contents (rtx);
117 static void output_addressed_constants (tree, int);
118 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
119 unsigned int, bool, bool);
120 static void globalize_decl (tree);
121 static bool decl_readonly_section_1 (enum section_category);
122 #ifdef BSS_SECTION_ASM_OP
123 #ifdef ASM_OUTPUT_ALIGNED_BSS
124 static void asm_output_aligned_bss (FILE *, tree, const char *,
125 unsigned HOST_WIDE_INT, int)
126 ATTRIBUTE_UNUSED;
127 #endif
128 #endif /* BSS_SECTION_ASM_OP */
129 static void mark_weak (tree);
130 static void output_constant_pool (const char *, tree);
131 static void handle_vtv_comdat_section (section *, const_tree);
133 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
134 section *text_section;
135 section *data_section;
136 section *readonly_data_section;
137 section *sdata_section;
138 section *ctors_section;
139 section *dtors_section;
140 section *bss_section;
141 section *sbss_section;
143 /* Various forms of common section. All are guaranteed to be nonnull. */
144 section *tls_comm_section;
145 section *comm_section;
146 section *lcomm_section;
148 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
149 May be null. */
150 section *bss_noswitch_section;
152 /* The section that holds the main exception table, when known. The section
153 is set either by the target's init_sections hook or by the first call to
154 switch_to_exception_section. */
155 section *exception_section;
157 /* The section that holds the DWARF2 frame unwind information, when known.
158 The section is set either by the target's init_sections hook or by the
159 first call to switch_to_eh_frame_section. */
160 section *eh_frame_section;
162 /* asm_out_file's current section. This is NULL if no section has yet
163 been selected or if we lose track of what the current section is. */
164 section *in_section;
166 /* True if code for the current function is currently being directed
167 at the cold section. */
168 bool in_cold_section_p;
170 /* The following global holds the "function name" for the code in the
171 cold section of a function, if hot/cold function splitting is enabled
172 and there was actually code that went into the cold section. A
173 pseudo function name is needed for the cold section of code for some
174 debugging tools that perform symbolization. */
175 tree cold_function_name = NULL_TREE;
177 /* A linked list of all the unnamed sections. */
178 static GTY(()) section *unnamed_sections;
180 /* Return a nonzero value if DECL has a section attribute. */
181 #define IN_NAMED_SECTION(DECL) \
182 (VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
184 struct section_hasher : ggc_ptr_hash<section>
186 typedef const char *compare_type;
188 static hashval_t hash (section *);
189 static bool equal (section *, const char *);
192 /* Hash table of named sections. */
193 static GTY(()) hash_table<section_hasher> *section_htab;
195 struct object_block_hasher : ggc_ptr_hash<object_block>
197 typedef const section *compare_type;
199 static hashval_t hash (object_block *);
200 static bool equal (object_block *, const section *);
203 /* A table of object_blocks, indexed by section. */
204 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
206 /* The next number to use for internal anchor labels. */
207 static GTY(()) int anchor_labelno;
209 /* A pool of constants that can be shared between functions. */
210 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
212 /* Helper routines for maintaining section_htab. */
214 bool
215 section_hasher::equal (section *old, const char *new_name)
217 return strcmp (old->named.name, new_name) == 0;
220 hashval_t
221 section_hasher::hash (section *old)
223 return htab_hash_string (old->named.name);
226 /* Return a hash value for section SECT. */
228 static hashval_t
229 hash_section (section *sect)
231 if (sect->common.flags & SECTION_NAMED)
232 return htab_hash_string (sect->named.name);
233 return sect->common.flags & ~SECTION_DECLARED;
236 /* Helper routines for maintaining object_block_htab. */
238 inline bool
239 object_block_hasher::equal (object_block *old, const section *new_section)
241 return old->sect == new_section;
244 hashval_t
245 object_block_hasher::hash (object_block *old)
247 return hash_section (old->sect);
250 /* Return a new unnamed section with the given fields. */
252 section *
253 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
254 const void *data)
256 section *sect;
258 sect = ggc_alloc<section> ();
259 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
260 sect->unnamed.callback = callback;
261 sect->unnamed.data = data;
262 sect->unnamed.next = unnamed_sections;
264 unnamed_sections = sect;
265 return sect;
268 /* Return a SECTION_NOSWITCH section with the given fields. */
270 static section *
271 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
273 section *sect;
275 sect = ggc_alloc<section> ();
276 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
277 sect->noswitch.callback = callback;
279 return sect;
282 /* Return the named section structure associated with NAME. Create
283 a new section with the given fields if no such structure exists.
284 When NOT_EXISTING, then fail if the section already exists. Return
285 the existing section if the SECTION_RETAIN bit doesn't match. Set
286 the SECTION_WRITE | SECTION_RELRO bits on the the existing section
287 if one of the section flags is SECTION_WRITE | SECTION_RELRO and the
288 other has none of these flags in named sections and either the section
289 hasn't been declared yet or has been declared as writable. */
291 section *
292 get_section (const char *name, unsigned int flags, tree decl,
293 bool not_existing)
295 section *sect, **slot;
297 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
298 INSERT);
299 flags |= SECTION_NAMED;
300 if (SUPPORTS_SHF_GNU_RETAIN
301 && decl != nullptr
302 && DECL_P (decl)
303 && DECL_PRESERVE_P (decl))
304 flags |= SECTION_RETAIN;
305 if (*slot == NULL)
307 sect = ggc_alloc<section> ();
308 sect->named.common.flags = flags;
309 sect->named.name = ggc_strdup (name);
310 sect->named.decl = decl;
311 *slot = sect;
313 else
315 if (not_existing)
316 internal_error ("Section already exists: %qs", name);
318 sect = *slot;
319 /* It is fine if one of the sections has SECTION_NOTYPE as long as
320 the other has none of the contrary flags (see the logic at the end
321 of default_section_type_flags, below). */
322 if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
323 && !((sect->common.flags | flags)
324 & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
325 | (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
327 sect->common.flags |= SECTION_NOTYPE;
328 flags |= SECTION_NOTYPE;
330 if ((sect->common.flags & ~SECTION_DECLARED) != flags
331 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
333 /* It is fine if one of the section flags is
334 SECTION_WRITE | SECTION_RELRO and the other has none of these
335 flags (i.e. read-only) in named sections and either the
336 section hasn't been declared yet or has been declared as writable.
337 In that case just make sure the resulting flags are
338 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
339 relocations. */
340 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
341 == (SECTION_WRITE | SECTION_RELRO)
342 && (sect->common.flags
343 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
344 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
345 && ((sect->common.flags & SECTION_DECLARED) == 0
346 || (sect->common.flags & SECTION_WRITE)))
348 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
349 return sect;
351 /* If the SECTION_RETAIN bit doesn't match, return and switch
352 to a new section later. */
353 if ((sect->common.flags & SECTION_RETAIN)
354 != (flags & SECTION_RETAIN))
355 return sect;
356 /* Sanity check user variables for flag changes. */
357 if (sect->named.decl != NULL
358 && DECL_P (sect->named.decl)
359 && decl != sect->named.decl)
361 if (decl != NULL && DECL_P (decl))
362 error ("%+qD causes a section type conflict with %qD",
363 decl, sect->named.decl);
364 else
365 error ("section type conflict with %qD", sect->named.decl);
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", decl);
371 else
372 error ("section type conflict");
373 /* Make sure we don't error about one section multiple times. */
374 sect->common.flags |= SECTION_OVERRIDE;
377 return sect;
380 /* Return true if the current compilation mode benefits from having
381 objects grouped into blocks. */
383 static bool
384 use_object_blocks_p (void)
386 return flag_section_anchors;
389 /* Return the object_block structure for section SECT. Create a new
390 structure if we haven't created one already. Return null if SECT
391 itself is null. Return also null for mergeable sections since
392 section anchors can't be used in mergeable sections anyway,
393 because the linker might move objects around, and using the
394 object blocks infrastructure in that case is both a waste and a
395 maintenance burden. */
397 static struct object_block *
398 get_block_for_section (section *sect)
400 struct object_block *block;
402 if (sect == NULL)
403 return NULL;
405 if (sect->common.flags & SECTION_MERGE)
406 return NULL;
408 object_block **slot
409 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
410 INSERT);
411 block = *slot;
412 if (block == NULL)
414 block = ggc_cleared_alloc<object_block> ();
415 block->sect = sect;
416 *slot = block;
418 return block;
421 /* Create a symbol with label LABEL and place it at byte offset
422 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
423 is not yet known. LABEL must be a garbage-collected string. */
425 static rtx
426 create_block_symbol (const char *label, struct object_block *block,
427 HOST_WIDE_INT offset)
429 rtx symbol;
430 unsigned int size;
432 /* Create the extended SYMBOL_REF. */
433 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
434 symbol = (rtx) ggc_internal_alloc (size);
436 /* Initialize the normal SYMBOL_REF fields. */
437 memset (symbol, 0, size);
438 PUT_CODE (symbol, SYMBOL_REF);
439 PUT_MODE (symbol, Pmode);
440 XSTR (symbol, 0) = label;
441 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
443 /* Initialize the block_symbol stuff. */
444 SYMBOL_REF_BLOCK (symbol) = block;
445 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
447 return symbol;
450 /* Return a section with a particular name and with whatever SECTION_*
451 flags section_type_flags deems appropriate. The name of the section
452 is taken from NAME if nonnull, otherwise it is taken from DECL's
453 DECL_SECTION_NAME. DECL is the decl associated with the section
454 (see the section comment for details) and RELOC is as for
455 section_type_flags. */
457 section *
458 get_named_section (tree decl, const char *name, int reloc)
460 unsigned int flags;
462 if (name == NULL)
464 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
465 name = DECL_SECTION_NAME (decl);
468 flags = targetm.section_type_flags (decl, name, reloc);
469 return get_section (name, flags, decl);
472 /* Worker for resolve_unique_section. */
474 static bool
475 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
477 n->implicit_section = true;
478 return false;
481 /* If required, set DECL_SECTION_NAME to a unique name. */
483 void
484 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
485 int flag_function_or_data_sections)
487 if (DECL_SECTION_NAME (decl) == NULL
488 && targetm_common.have_named_sections
489 && (flag_function_or_data_sections
490 || (SUPPORTS_SHF_GNU_RETAIN && DECL_PRESERVE_P (decl))
491 || DECL_COMDAT_GROUP (decl)))
493 targetm.asm_out.unique_section (decl, reloc);
494 if (DECL_SECTION_NAME (decl))
495 symtab_node::get (decl)->call_for_symbol_and_aliases
496 (set_implicit_section, NULL, true);
500 #ifdef BSS_SECTION_ASM_OP
502 #ifdef ASM_OUTPUT_ALIGNED_BSS
504 /* Utility function for targets to use in implementing
505 ASM_OUTPUT_ALIGNED_BSS.
506 ??? It is believed that this function will work in most cases so such
507 support is localized here. */
509 static void
510 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
511 const char *name, unsigned HOST_WIDE_INT size,
512 int align)
514 switch_to_section (bss_section);
515 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
516 #ifdef ASM_DECLARE_OBJECT_NAME
517 last_assemble_variable_decl = decl;
518 ASM_DECLARE_OBJECT_NAME (file, name, decl);
519 #else
520 /* Standard thing is just output label for the object. */
521 ASM_OUTPUT_LABEL (file, name);
522 #endif /* ASM_DECLARE_OBJECT_NAME */
523 ASM_OUTPUT_SKIP (file, size ? size : 1);
526 #endif
528 #endif /* BSS_SECTION_ASM_OP */
530 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
531 /* Return the hot section for function DECL. Return text_section for
532 null DECLs. */
534 static section *
535 hot_function_section (tree decl)
537 if (decl != NULL_TREE
538 && DECL_SECTION_NAME (decl) != NULL
539 && targetm_common.have_named_sections)
540 return get_named_section (decl, NULL, 0);
541 else
542 return text_section;
544 #endif
546 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
547 is NULL.
549 When DECL_SECTION_NAME is non-NULL and it is implicit section and
550 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
551 concatenate the name with NAMED_SECTION_SUFFIX.
552 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
554 section *
555 get_named_text_section (tree decl,
556 const char *text_section_name,
557 const char *named_section_suffix)
559 if (decl && DECL_SECTION_NAME (decl))
561 if (named_section_suffix)
563 const char *dsn = DECL_SECTION_NAME (decl);
564 const char *stripped_name;
565 char *name, *buffer;
567 name = (char *) alloca (strlen (dsn) + 1);
568 memcpy (name, dsn,
569 strlen (dsn) + 1);
571 stripped_name = targetm.strip_name_encoding (name);
573 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
574 return get_named_section (decl, buffer, 0);
576 else if (symtab_node::get (decl)->implicit_section)
578 const char *name;
580 /* Do not try to split gnu_linkonce functions. This gets somewhat
581 slipperly. */
582 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
583 return NULL;
584 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
585 name = targetm.strip_name_encoding (name);
586 return get_named_section (decl, ACONCAT ((text_section_name, ".",
587 name, NULL)), 0);
589 else
590 return NULL;
592 return get_named_section (decl, text_section_name, 0);
595 /* Choose named function section based on its frequency. */
597 section *
598 default_function_section (tree decl, enum node_frequency freq,
599 bool startup, bool exit)
601 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
602 /* Old GNU linkers have buggy --gc-section support, which sometimes
603 results in .gcc_except_table* sections being garbage collected. */
604 if (decl
605 && symtab_node::get (decl)->implicit_section)
606 return NULL;
607 #endif
609 if (!flag_reorder_functions
610 || !targetm_common.have_named_sections)
611 return NULL;
612 /* Startup code should go to startup subsection unless it is
613 unlikely executed (this happens especially with function splitting
614 where we can split away unnecessary parts of static constructors. */
615 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
617 /* During LTO the tp_first_run profiling will naturally place all
618 initialization code first. Using separate section is counter-productive
619 because startup only code may call functions which are no longer
620 startup only. */
621 if (!in_lto_p
622 || !cgraph_node::get (decl)->tp_first_run
623 || !opt_for_fn (decl, flag_profile_reorder_functions))
624 return get_named_text_section (decl, ".text.startup", NULL);
625 else
626 return NULL;
629 /* Similarly for exit. */
630 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
631 return get_named_text_section (decl, ".text.exit", NULL);
633 /* Group cold functions together, similarly for hot code. */
634 switch (freq)
636 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
637 return get_named_text_section (decl, ".text.unlikely", NULL);
638 case NODE_FREQUENCY_HOT:
639 return get_named_text_section (decl, ".text.hot", NULL);
640 /* FALLTHRU */
641 default:
642 return NULL;
646 /* Return the section for function DECL.
648 If DECL is NULL_TREE, return the text section. We can be passed
649 NULL_TREE under some circumstances by dbxout.c at least.
651 If FORCE_COLD is true, return cold function section ignoring
652 the frequency info of cgraph_node. */
654 static section *
655 function_section_1 (tree decl, bool force_cold)
657 section *section = NULL;
658 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
659 bool startup = false, exit = false;
661 if (decl)
663 struct cgraph_node *node = cgraph_node::get (decl);
665 if (node)
667 freq = node->frequency;
668 startup = node->only_called_at_startup;
669 exit = node->only_called_at_exit;
672 if (force_cold)
673 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
675 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
676 if (decl != NULL_TREE
677 && DECL_SECTION_NAME (decl) != NULL)
679 if (targetm.asm_out.function_section)
680 section = targetm.asm_out.function_section (decl, freq,
681 startup, exit);
682 if (section)
683 return section;
684 return get_named_section (decl, NULL, 0);
686 else
687 return targetm.asm_out.select_section
688 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
689 symtab_node::get (decl)->definition_alignment ());
690 #else
691 if (targetm.asm_out.function_section)
692 section = targetm.asm_out.function_section (decl, freq, startup, exit);
693 if (section)
694 return section;
695 return hot_function_section (decl);
696 #endif
699 /* Return the section for function DECL.
701 If DECL is NULL_TREE, return the text section. We can be passed
702 NULL_TREE under some circumstances by dbxout.c at least. */
704 section *
705 function_section (tree decl)
707 /* Handle cases where function splitting code decides
708 to put function entry point into unlikely executed section
709 despite the fact that the function itself is not cold
710 (i.e. it is called rarely but contains a hot loop that is
711 better to live in hot subsection for the code locality). */
712 return function_section_1 (decl,
713 first_function_block_is_cold);
716 /* Return the section for the current function, take IN_COLD_SECTION_P
717 into account. */
719 section *
720 current_function_section (void)
722 return function_section_1 (current_function_decl, in_cold_section_p);
725 /* Tell assembler to switch to unlikely-to-be-executed text section. */
727 section *
728 unlikely_text_section (void)
730 return function_section_1 (current_function_decl, true);
733 /* When called within a function context, return true if the function
734 has been assigned a cold text section and if SECT is that section.
735 When called outside a function context, return true if SECT is the
736 default cold section. */
738 bool
739 unlikely_text_section_p (section *sect)
741 return sect == function_section_1 (current_function_decl, true);
744 /* Switch to the other function partition (if inside of hot section
745 into cold section, otherwise into the hot section). */
747 void
748 switch_to_other_text_partition (void)
750 in_cold_section_p = !in_cold_section_p;
751 switch_to_section (current_function_section ());
754 /* Return the read-only or relocated read-only data section
755 associated with function DECL. */
757 section *
758 default_function_rodata_section (tree decl, bool relocatable)
760 const char* sname;
761 unsigned int flags;
763 flags = 0;
765 if (relocatable)
767 sname = ".data.rel.ro.local";
768 flags = (SECTION_WRITE | SECTION_RELRO);
770 else
771 sname = ".rodata";
773 if (decl && DECL_SECTION_NAME (decl))
775 const char *name = DECL_SECTION_NAME (decl);
777 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
779 const char *dot;
780 size_t len;
781 char* rname;
783 dot = strchr (name + 1, '.');
784 if (!dot)
785 dot = name;
786 len = strlen (dot) + strlen (sname) + 1;
787 rname = (char *) alloca (len);
789 strcpy (rname, sname);
790 strcat (rname, dot);
791 return get_section (rname, (SECTION_LINKONCE | flags), decl);
793 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo or
794 .gnu.linkonce.d.rel.ro.local.foo if the jump table is relocatable. */
795 else if (DECL_COMDAT_GROUP (decl)
796 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
798 size_t len;
799 char *rname;
801 if (relocatable)
803 len = strlen (name) + strlen (".rel.ro.local") + 1;
804 rname = (char *) alloca (len);
806 strcpy (rname, ".gnu.linkonce.d.rel.ro.local");
807 strcat (rname, name + 15);
809 else
811 len = strlen (name) + 1;
812 rname = (char *) alloca (len);
814 memcpy (rname, name, len);
815 rname[14] = 'r';
817 return get_section (rname, (SECTION_LINKONCE | flags), decl);
819 /* For .text.foo we want to use .rodata.foo. */
820 else if (flag_function_sections && flag_data_sections
821 && strncmp (name, ".text.", 6) == 0)
823 size_t len = strlen (name) + 1;
824 char *rname = (char *) alloca (len + strlen (sname) - 5);
826 memcpy (rname, sname, strlen (sname));
827 memcpy (rname + strlen (sname), name + 5, len - 5);
828 return get_section (rname, flags, decl);
832 if (relocatable)
833 return get_section (sname, flags, decl);
834 else
835 return readonly_data_section;
838 /* Return the read-only data section associated with function DECL
839 for targets where that section should be always the single
840 readonly data section. */
842 section *
843 default_no_function_rodata_section (tree, bool)
845 return readonly_data_section;
848 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
850 static const char *
851 function_mergeable_rodata_prefix (void)
853 section *s = targetm.asm_out.function_rodata_section (current_function_decl,
854 false);
855 if (SECTION_STYLE (s) == SECTION_NAMED)
856 return s->named.name;
857 else
858 return targetm.asm_out.mergeable_rodata_prefix;
861 /* Return the section to use for string merging. */
863 static section *
864 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
865 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
866 unsigned int flags ATTRIBUTE_UNUSED)
868 HOST_WIDE_INT len;
870 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
871 && TREE_CODE (decl) == STRING_CST
872 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
873 && align <= 256
874 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
875 && TREE_STRING_LENGTH (decl) == len)
877 scalar_int_mode mode;
878 unsigned int modesize;
879 const char *str;
880 HOST_WIDE_INT i;
881 int j, unit;
882 const char *prefix = function_mergeable_rodata_prefix ();
883 char *name = (char *) alloca (strlen (prefix) + 30);
885 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
886 modesize = GET_MODE_BITSIZE (mode);
887 if (modesize >= 8 && modesize <= 256
888 && (modesize & (modesize - 1)) == 0)
890 if (align < modesize)
891 align = modesize;
893 if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
894 return readonly_data_section;
896 str = TREE_STRING_POINTER (decl);
897 unit = GET_MODE_SIZE (mode);
899 /* Check for embedded NUL characters. */
900 for (i = 0; i < len; i += unit)
902 for (j = 0; j < unit; j++)
903 if (str[i + j] != '\0')
904 break;
905 if (j == unit)
906 break;
908 if (i == len - unit || (unit == 1 && i == len))
910 sprintf (name, "%s.str%d.%d", prefix,
911 modesize / 8, (int) (align / 8));
912 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
913 return get_section (name, flags, NULL);
918 return readonly_data_section;
921 /* Return the section to use for constant merging. */
923 section *
924 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
925 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
926 unsigned int flags ATTRIBUTE_UNUSED)
928 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
929 && mode != VOIDmode
930 && mode != BLKmode
931 && known_le (GET_MODE_BITSIZE (mode), align)
932 && align >= 8
933 && align <= 256
934 && (align & (align - 1)) == 0
935 && (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
937 const char *prefix = function_mergeable_rodata_prefix ();
938 char *name = (char *) alloca (strlen (prefix) + 30);
940 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
941 flags |= (align / 8) | SECTION_MERGE;
942 return get_section (name, flags, NULL);
944 return readonly_data_section;
947 /* Given NAME, a putative register name, discard any customary prefixes. */
949 static const char *
950 strip_reg_name (const char *name)
952 #ifdef REGISTER_PREFIX
953 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
954 name += strlen (REGISTER_PREFIX);
955 #endif
956 if (name[0] == '%' || name[0] == '#')
957 name++;
958 return name;
961 /* The user has asked for a DECL to have a particular name. Set (or
962 change) it in such a way that we don't prefix an underscore to
963 it. */
964 void
965 set_user_assembler_name (tree decl, const char *name)
967 char *starred = (char *) alloca (strlen (name) + 2);
968 starred[0] = '*';
969 strcpy (starred + 1, name);
970 symtab->change_decl_assembler_name (decl, get_identifier (starred));
971 SET_DECL_RTL (decl, NULL_RTX);
974 /* Decode an `asm' spec for a declaration as a register name.
975 Return the register number, or -1 if nothing specified,
976 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
977 or -3 if ASMSPEC is `cc' and is not recognized,
978 or -4 if ASMSPEC is `memory' and is not recognized.
979 Accept an exact spelling or a decimal number.
980 Prefixes such as % are optional. */
983 decode_reg_name_and_count (const char *asmspec, int *pnregs)
985 /* Presume just one register is clobbered. */
986 *pnregs = 1;
988 if (asmspec != 0)
990 int i;
992 /* Get rid of confusing prefixes. */
993 asmspec = strip_reg_name (asmspec);
995 /* Allow a decimal number as a "register name". */
996 for (i = strlen (asmspec) - 1; i >= 0; i--)
997 if (! ISDIGIT (asmspec[i]))
998 break;
999 if (asmspec[0] != 0 && i < 0)
1001 i = atoi (asmspec);
1002 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
1003 return i;
1004 else
1005 return -2;
1008 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1009 if (reg_names[i][0]
1010 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
1011 return i;
1013 #ifdef OVERLAPPING_REGISTER_NAMES
1015 static const struct
1017 const char *const name;
1018 const int number;
1019 const int nregs;
1020 } table[] = OVERLAPPING_REGISTER_NAMES;
1022 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1023 if (table[i].name[0]
1024 && ! strcmp (asmspec, table[i].name))
1026 *pnregs = table[i].nregs;
1027 return table[i].number;
1030 #endif /* OVERLAPPING_REGISTER_NAMES */
1032 #ifdef ADDITIONAL_REGISTER_NAMES
1034 static const struct { const char *const name; const int number; } table[]
1035 = ADDITIONAL_REGISTER_NAMES;
1037 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1038 if (table[i].name[0]
1039 && ! strcmp (asmspec, table[i].name)
1040 && reg_names[table[i].number][0])
1041 return table[i].number;
1043 #endif /* ADDITIONAL_REGISTER_NAMES */
1045 if (!strcmp (asmspec, "memory"))
1046 return -4;
1048 if (!strcmp (asmspec, "cc"))
1049 return -3;
1051 return -2;
1054 return -1;
1058 decode_reg_name (const char *name)
1060 int count;
1061 return decode_reg_name_and_count (name, &count);
1065 /* Return true if DECL's initializer is suitable for a BSS section. */
1067 bool
1068 bss_initializer_p (const_tree decl, bool named)
1070 /* Do not put non-common constants into the .bss section, they belong in
1071 a readonly section, except when NAMED is true. */
1072 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1073 && (DECL_INITIAL (decl) == NULL
1074 /* In LTO we have no errors in program; error_mark_node is used
1075 to mark offlined constructors. */
1076 || (DECL_INITIAL (decl) == error_mark_node
1077 && !in_lto_p)
1078 || (flag_zero_initialized_in_bss
1079 && initializer_zerop (DECL_INITIAL (decl))
1080 /* A decl with the "persistent" attribute applied and
1081 explicitly initialized to 0 should not be treated as a BSS
1082 variable. */
1083 && !DECL_PERSISTENT_P (decl))));
1086 /* Compute the alignment of variable specified by DECL.
1087 DONT_OUTPUT_DATA is from assemble_variable. */
1089 void
1090 align_variable (tree decl, bool dont_output_data)
1092 unsigned int align = DECL_ALIGN (decl);
1094 /* In the case for initialing an array whose length isn't specified,
1095 where we have not yet been able to do the layout,
1096 figure out the proper alignment now. */
1097 if (dont_output_data && DECL_SIZE (decl) == 0
1098 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1099 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1101 /* Some object file formats have a maximum alignment which they support.
1102 In particular, a.out format supports a maximum alignment of 4. */
1103 if (align > MAX_OFILE_ALIGNMENT)
1105 error ("alignment of %q+D is greater than maximum object "
1106 "file alignment %d", decl,
1107 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1108 align = MAX_OFILE_ALIGNMENT;
1111 if (! DECL_USER_ALIGN (decl))
1113 #ifdef DATA_ABI_ALIGNMENT
1114 unsigned int data_abi_align
1115 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1116 /* For backwards compatibility, don't assume the ABI alignment for
1117 TLS variables. */
1118 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1119 align = data_abi_align;
1120 #endif
1122 /* On some machines, it is good to increase alignment sometimes.
1123 But as DECL_ALIGN is used both for actually emitting the variable
1124 and for code accessing the variable as guaranteed alignment, we
1125 can only increase the alignment if it is a performance optimization
1126 if the references to it must bind to the current definition. */
1127 if (decl_binds_to_current_def_p (decl)
1128 && !DECL_VIRTUAL_P (decl))
1130 #ifdef DATA_ALIGNMENT
1131 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1132 /* Don't increase alignment too much for TLS variables - TLS space
1133 is too precious. */
1134 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1135 align = data_align;
1136 #endif
1137 if (DECL_INITIAL (decl) != 0
1138 /* In LTO we have no errors in program; error_mark_node is used
1139 to mark offlined constructors. */
1140 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1142 unsigned int const_align
1143 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1144 /* Don't increase alignment too much for TLS variables - TLS
1145 space is too precious. */
1146 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1147 align = const_align;
1152 /* Reset the alignment in case we have made it tighter, so we can benefit
1153 from it in get_pointer_alignment. */
1154 SET_DECL_ALIGN (decl, align);
1157 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1158 beyond what align_variable returned. */
1160 static unsigned int
1161 get_variable_align (tree decl)
1163 unsigned int align = DECL_ALIGN (decl);
1165 /* For user aligned vars or static vars align_variable already did
1166 everything. */
1167 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1168 return align;
1170 #ifdef DATA_ABI_ALIGNMENT
1171 if (DECL_THREAD_LOCAL_P (decl))
1172 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1173 #endif
1175 /* For decls that bind to the current definition, align_variable
1176 did also everything, except for not assuming ABI required alignment
1177 of TLS variables. For other vars, increase the alignment here
1178 as an optimization. */
1179 if (!decl_binds_to_current_def_p (decl))
1181 /* On some machines, it is good to increase alignment sometimes. */
1182 #ifdef DATA_ALIGNMENT
1183 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1184 /* Don't increase alignment too much for TLS variables - TLS space
1185 is too precious. */
1186 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1187 align = data_align;
1188 #endif
1189 if (DECL_INITIAL (decl) != 0
1190 /* In LTO we have no errors in program; error_mark_node is used
1191 to mark offlined constructors. */
1192 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1194 unsigned int const_align
1195 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1196 /* Don't increase alignment too much for TLS variables - TLS space
1197 is too precious. */
1198 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1199 align = const_align;
1203 return align;
1206 /* Return the section into which the given VAR_DECL or CONST_DECL
1207 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1208 section should be used wherever possible. */
1210 section *
1211 get_variable_section (tree decl, bool prefer_noswitch_p)
1213 addr_space_t as = ADDR_SPACE_GENERIC;
1214 int reloc;
1215 varpool_node *vnode = varpool_node::get (decl);
1216 if (vnode)
1218 vnode = vnode->ultimate_alias_target ();
1219 decl = vnode->decl;
1222 if (TREE_TYPE (decl) != error_mark_node)
1223 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1225 /* We need the constructor to figure out reloc flag. */
1226 if (vnode)
1227 vnode->get_constructor ();
1229 if (DECL_COMMON (decl)
1230 && !(SUPPORTS_SHF_GNU_RETAIN && DECL_PRESERVE_P (decl)))
1232 /* If the decl has been given an explicit section name, or it resides
1233 in a non-generic address space, then it isn't common, and shouldn't
1234 be handled as such. */
1235 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1236 && ADDR_SPACE_GENERIC_P (as));
1237 if (DECL_THREAD_LOCAL_P (decl))
1238 return tls_comm_section;
1239 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1240 return comm_section;
1243 if (DECL_INITIAL (decl) == error_mark_node)
1244 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1245 else if (DECL_INITIAL (decl))
1246 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1247 else
1248 reloc = 0;
1250 resolve_unique_section (decl, reloc, flag_data_sections);
1251 if (IN_NAMED_SECTION (decl))
1253 section *sect = get_named_section (decl, NULL, reloc);
1255 if ((sect->common.flags & SECTION_BSS)
1256 && !bss_initializer_p (decl, true))
1258 error_at (DECL_SOURCE_LOCATION (decl),
1259 "only zero initializers are allowed in section %qs",
1260 sect->named.name);
1261 DECL_INITIAL (decl) = error_mark_node;
1263 return sect;
1266 if (ADDR_SPACE_GENERIC_P (as)
1267 && !DECL_THREAD_LOCAL_P (decl)
1268 && !DECL_NOINIT_P (decl)
1269 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1270 && bss_initializer_p (decl))
1272 if (!TREE_PUBLIC (decl)
1273 && !((flag_sanitize & SANITIZE_ADDRESS)
1274 && asan_protect_global (decl)))
1275 return lcomm_section;
1276 if (bss_noswitch_section)
1277 return bss_noswitch_section;
1280 return targetm.asm_out.select_section (decl, reloc,
1281 get_variable_align (decl));
1284 /* Return the block into which object_block DECL should be placed. */
1286 static struct object_block *
1287 get_block_for_decl (tree decl)
1289 section *sect;
1291 if (VAR_P (decl))
1293 /* The object must be defined in this translation unit. */
1294 if (DECL_EXTERNAL (decl))
1295 return NULL;
1297 /* There's no point using object blocks for something that is
1298 isolated by definition. */
1299 if (DECL_COMDAT_GROUP (decl))
1300 return NULL;
1303 /* We can only calculate block offsets if the decl has a known
1304 constant size. */
1305 if (DECL_SIZE_UNIT (decl) == NULL)
1306 return NULL;
1307 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1308 return NULL;
1310 /* Find out which section should contain DECL. We cannot put it into
1311 an object block if it requires a standalone definition. */
1312 if (VAR_P (decl))
1313 align_variable (decl, 0);
1314 sect = get_variable_section (decl, true);
1315 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1316 return NULL;
1318 return get_block_for_section (sect);
1321 /* Make sure block symbol SYMBOL is in block BLOCK. */
1323 static void
1324 change_symbol_block (rtx symbol, struct object_block *block)
1326 if (block != SYMBOL_REF_BLOCK (symbol))
1328 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1329 SYMBOL_REF_BLOCK (symbol) = block;
1333 /* Return true if it is possible to put DECL in an object_block. */
1335 static bool
1336 use_blocks_for_decl_p (tree decl)
1338 struct symtab_node *snode;
1340 /* Only data DECLs can be placed into object blocks. */
1341 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1342 return false;
1344 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1345 are never used from code directly and we never want object block handling
1346 for those. */
1347 if (DECL_INITIAL (decl) == decl)
1348 return false;
1350 /* If this decl is an alias, then we don't want to emit a
1351 definition. */
1352 if (VAR_P (decl)
1353 && (snode = symtab_node::get (decl)) != NULL
1354 && snode->alias)
1355 return false;
1357 return targetm.use_blocks_for_decl_p (decl);
1360 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1361 until we find an identifier that is not itself a transparent alias.
1362 Modify the alias passed to it by reference (and all aliases on the
1363 way to the ultimate target), such that they do not have to be
1364 followed again, and return the ultimate target of the alias
1365 chain. */
1367 static inline tree
1368 ultimate_transparent_alias_target (tree *alias)
1370 tree target = *alias;
1372 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1374 gcc_assert (TREE_CHAIN (target));
1375 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1376 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1377 && ! TREE_CHAIN (target));
1378 *alias = target;
1381 return target;
1384 /* Return true if REGNUM is mentioned in ELIMINABLE_REGS as a from
1385 register number. */
1387 static bool
1388 eliminable_regno_p (int regnum)
1390 static const struct
1392 const int from;
1393 const int to;
1394 } eliminables[] = ELIMINABLE_REGS;
1395 for (size_t i = 0; i < ARRAY_SIZE (eliminables); i++)
1396 if (regnum == eliminables[i].from)
1397 return true;
1398 return false;
1401 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1402 have static storage duration. In other words, it should not be an
1403 automatic variable, including PARM_DECLs.
1405 There is, however, one exception: this function handles variables
1406 explicitly placed in a particular register by the user.
1408 This is never called for PARM_DECL nodes. */
1410 void
1411 make_decl_rtl (tree decl)
1413 const char *name = 0;
1414 int reg_number;
1415 tree id;
1416 rtx x;
1418 /* Check that we are not being given an automatic variable. */
1419 gcc_assert (TREE_CODE (decl) != PARM_DECL
1420 && TREE_CODE (decl) != RESULT_DECL);
1422 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1423 gcc_assert (!VAR_P (decl)
1424 || TREE_STATIC (decl)
1425 || TREE_PUBLIC (decl)
1426 || DECL_EXTERNAL (decl)
1427 || DECL_REGISTER (decl));
1429 /* And that we were not given a type or a label. */
1430 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1431 && TREE_CODE (decl) != LABEL_DECL);
1433 /* For a duplicate declaration, we can be called twice on the
1434 same DECL node. Don't discard the RTL already made. */
1435 if (DECL_RTL_SET_P (decl))
1437 /* If the old RTL had the wrong mode, fix the mode. */
1438 x = DECL_RTL (decl);
1439 if (GET_MODE (x) != DECL_MODE (decl))
1440 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1442 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1443 return;
1445 /* ??? Another way to do this would be to maintain a hashed
1446 table of such critters. Instead of adding stuff to a DECL
1447 to give certain attributes to it, we could use an external
1448 hash map from DECL to set of attributes. */
1450 /* Let the target reassign the RTL if it wants.
1451 This is necessary, for example, when one machine specific
1452 decl attribute overrides another. */
1453 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1455 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1456 on the new decl information. */
1457 if (MEM_P (x)
1458 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1459 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1460 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1462 return;
1465 /* If this variable belongs to the global constant pool, retrieve the
1466 pre-computed RTL or recompute it in LTO mode. */
1467 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1469 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1470 return;
1473 id = DECL_ASSEMBLER_NAME (decl);
1474 name = IDENTIFIER_POINTER (id);
1476 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1477 && DECL_REGISTER (decl))
1479 error ("register name not specified for %q+D", decl);
1481 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1483 const char *asmspec = name+1;
1484 machine_mode mode = DECL_MODE (decl);
1485 reg_number = decode_reg_name (asmspec);
1486 /* First detect errors in declaring global registers. */
1487 if (reg_number == -1)
1488 error ("register name not specified for %q+D", decl);
1489 else if (reg_number < 0)
1490 error ("invalid register name for %q+D", decl);
1491 else if (mode == BLKmode)
1492 error ("data type of %q+D isn%'t suitable for a register",
1493 decl);
1494 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1495 error ("the register specified for %q+D cannot be accessed"
1496 " by the current target", decl);
1497 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1498 error ("the register specified for %q+D is not general enough"
1499 " to be used as a register variable", decl);
1500 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1501 error ("register specified for %q+D isn%'t suitable for data type",
1502 decl);
1503 else if (reg_number != HARD_FRAME_POINTER_REGNUM
1504 && (reg_number == FRAME_POINTER_REGNUM
1505 #ifdef RETURN_ADDRESS_POINTER_REGNUM
1506 || reg_number == RETURN_ADDRESS_POINTER_REGNUM
1507 #endif
1508 || reg_number == ARG_POINTER_REGNUM)
1509 && eliminable_regno_p (reg_number))
1510 error ("register specified for %q+D is an internal GCC "
1511 "implementation detail", decl);
1512 /* Now handle properly declared static register variables. */
1513 else
1515 int nregs;
1517 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1519 DECL_INITIAL (decl) = 0;
1520 error ("global register variable has initial value");
1522 if (TREE_THIS_VOLATILE (decl))
1523 warning (OPT_Wvolatile_register_var,
1524 "optimization may eliminate reads and/or "
1525 "writes to register variables");
1527 /* If the user specified one of the eliminables registers here,
1528 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1529 confused with that register and be eliminated. This usage is
1530 somewhat suspect... */
1532 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1533 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1534 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1536 if (TREE_STATIC (decl))
1538 /* Make this register global, so not usable for anything
1539 else. */
1540 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1541 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1542 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1543 #endif
1544 nregs = hard_regno_nregs (reg_number, mode);
1545 while (nregs > 0)
1546 globalize_reg (decl, reg_number + --nregs);
1549 /* As a register variable, it has no section. */
1550 return;
1552 /* Avoid internal errors from invalid register
1553 specifications. */
1554 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1555 DECL_HARD_REGISTER (decl) = 0;
1556 /* Also avoid SSA inconsistencies by pretending this is an external
1557 decl now. */
1558 DECL_EXTERNAL (decl) = 1;
1559 return;
1561 /* Now handle ordinary static variables and functions (in memory).
1562 Also handle vars declared register invalidly. */
1563 else if (name[0] == '*')
1565 #ifdef REGISTER_PREFIX
1566 if (strlen (REGISTER_PREFIX) != 0)
1568 reg_number = decode_reg_name (name);
1569 if (reg_number >= 0 || reg_number == -3)
1570 error ("register name given for non-register variable %q+D", decl);
1572 #endif
1575 /* Specifying a section attribute on a variable forces it into a
1576 non-.bss section, and thus it cannot be common. */
1577 /* FIXME: In general this code should not be necessary because
1578 visibility pass is doing the same work. But notice_global_symbol
1579 is called early and it needs to make DECL_RTL to get the name.
1580 we take care of recomputing the DECL_RTL after visibility is changed. */
1581 if (VAR_P (decl)
1582 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1583 && DECL_SECTION_NAME (decl) != NULL
1584 && DECL_INITIAL (decl) == NULL_TREE
1585 && DECL_COMMON (decl))
1586 DECL_COMMON (decl) = 0;
1588 /* Variables can't be both common and weak. */
1589 if (VAR_P (decl) && DECL_WEAK (decl))
1590 DECL_COMMON (decl) = 0;
1592 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1593 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1594 else
1596 machine_mode address_mode = Pmode;
1597 if (TREE_TYPE (decl) != error_mark_node)
1599 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1600 address_mode = targetm.addr_space.address_mode (as);
1602 x = gen_rtx_SYMBOL_REF (address_mode, name);
1604 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1605 SET_SYMBOL_REF_DECL (x, decl);
1607 x = gen_rtx_MEM (DECL_MODE (decl), x);
1608 if (TREE_CODE (decl) != FUNCTION_DECL)
1609 set_mem_attributes (x, decl, 1);
1610 SET_DECL_RTL (decl, x);
1612 /* Optionally set flags or add text to the name to record information
1613 such as that it is a function name.
1614 If the name is changed, the macro ASM_OUTPUT_LABELREF
1615 will have to know how to strip this information. */
1616 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1619 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1620 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1621 rtl. */
1624 make_decl_rtl_for_debug (tree decl)
1626 unsigned int save_aliasing_flag;
1627 rtx rtl;
1629 if (DECL_RTL_SET_P (decl))
1630 return DECL_RTL (decl);
1632 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1633 call new_alias_set. If running with -fcompare-debug, sometimes
1634 we do not want to create alias sets that will throw the alias
1635 numbers off in the comparison dumps. So... clearing
1636 flag_strict_aliasing will keep new_alias_set() from creating a
1637 new set. */
1638 save_aliasing_flag = flag_strict_aliasing;
1639 flag_strict_aliasing = 0;
1641 rtl = DECL_RTL (decl);
1642 /* Reset DECL_RTL back, as various parts of the compiler expects
1643 DECL_RTL set meaning it is actually going to be output. */
1644 SET_DECL_RTL (decl, NULL);
1646 flag_strict_aliasing = save_aliasing_flag;
1647 return rtl;
1650 /* Output a string of literal assembler code
1651 for an `asm' keyword used between functions. */
1653 void
1654 assemble_asm (tree string)
1656 const char *p;
1657 app_enable ();
1659 if (TREE_CODE (string) == ADDR_EXPR)
1660 string = TREE_OPERAND (string, 0);
1662 p = TREE_STRING_POINTER (string);
1663 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1666 /* Write the address of the entity given by SYMBOL to SEC. */
1667 void
1668 assemble_addr_to_section (rtx symbol, section *sec)
1670 switch_to_section (sec);
1671 assemble_align (POINTER_SIZE);
1672 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1675 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1676 not) section for PRIORITY. */
1677 section *
1678 get_cdtor_priority_section (int priority, bool constructor_p)
1680 /* Buffer conservatively large enough for the full range of a 32-bit
1681 int plus the text below. */
1682 char buf[18];
1684 /* ??? This only works reliably with the GNU linker. */
1685 sprintf (buf, "%s.%.5u",
1686 constructor_p ? ".ctors" : ".dtors",
1687 /* Invert the numbering so the linker puts us in the proper
1688 order; constructors are run from right to left, and the
1689 linker sorts in increasing order. */
1690 MAX_INIT_PRIORITY - priority);
1691 return get_section (buf, SECTION_WRITE, NULL);
1694 void
1695 default_named_section_asm_out_destructor (rtx symbol, int priority)
1697 section *sec;
1699 if (priority != DEFAULT_INIT_PRIORITY)
1700 sec = get_cdtor_priority_section (priority,
1701 /*constructor_p=*/false);
1702 else
1703 sec = get_section (".dtors", SECTION_WRITE, NULL);
1705 assemble_addr_to_section (symbol, sec);
1708 #ifdef DTORS_SECTION_ASM_OP
1709 void
1710 default_dtor_section_asm_out_destructor (rtx symbol,
1711 int priority ATTRIBUTE_UNUSED)
1713 assemble_addr_to_section (symbol, dtors_section);
1715 #endif
1717 void
1718 default_named_section_asm_out_constructor (rtx symbol, int priority)
1720 section *sec;
1722 if (priority != DEFAULT_INIT_PRIORITY)
1723 sec = get_cdtor_priority_section (priority,
1724 /*constructor_p=*/true);
1725 else
1726 sec = get_section (".ctors", SECTION_WRITE, NULL);
1728 assemble_addr_to_section (symbol, sec);
1731 #ifdef CTORS_SECTION_ASM_OP
1732 void
1733 default_ctor_section_asm_out_constructor (rtx symbol,
1734 int priority ATTRIBUTE_UNUSED)
1736 assemble_addr_to_section (symbol, ctors_section);
1738 #endif
1740 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1741 a nonzero value if the constant pool should be output before the
1742 start of the function, or a zero value if the pool should output
1743 after the end of the function. The default is to put it before the
1744 start. */
1746 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1747 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1748 #endif
1750 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1751 to be output to assembler.
1752 Set first_global_object_name and weak_global_object_name as appropriate. */
1754 void
1755 notice_global_symbol (tree decl)
1757 const char **t = &first_global_object_name;
1759 if (first_global_object_name
1760 || !TREE_PUBLIC (decl)
1761 || DECL_EXTERNAL (decl)
1762 || !DECL_NAME (decl)
1763 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1764 || (TREE_CODE (decl) != FUNCTION_DECL
1765 && (!VAR_P (decl)
1766 || (DECL_COMMON (decl)
1767 && (DECL_INITIAL (decl) == 0
1768 || DECL_INITIAL (decl) == error_mark_node)))))
1769 return;
1771 /* We win when global object is found, but it is useful to know about weak
1772 symbol as well so we can produce nicer unique names. */
1773 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1774 t = &weak_global_object_name;
1776 if (!*t)
1778 tree id = DECL_ASSEMBLER_NAME (decl);
1779 ultimate_transparent_alias_target (&id);
1780 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1784 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1785 current function goes into the cold section, so that targets can use
1786 current_function_section during RTL expansion. DECL describes the
1787 function. */
1789 void
1790 decide_function_section (tree decl)
1792 first_function_block_is_cold = false;
1794 if (DECL_SECTION_NAME (decl))
1796 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1797 /* Calls to function_section rely on first_function_block_is_cold
1798 being accurate. */
1799 first_function_block_is_cold = (node
1800 && node->frequency
1801 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1804 in_cold_section_p = first_function_block_is_cold;
1807 /* Get the function's name, as described by its RTL. This may be
1808 different from the DECL_NAME name used in the source file. */
1809 const char *
1810 get_fnname_from_decl (tree decl)
1812 rtx x = DECL_RTL (decl);
1813 gcc_assert (MEM_P (x));
1814 x = XEXP (x, 0);
1815 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1816 return XSTR (x, 0);
1819 /* Output assembler code for the constant pool of a function and associated
1820 with defining the name of the function. DECL describes the function.
1821 NAME is the function's name. For the constant pool, we use the current
1822 constant pool data. */
1824 void
1825 assemble_start_function (tree decl, const char *fnname)
1827 int align;
1828 char tmp_label[100];
1829 bool hot_label_written = false;
1831 if (crtl->has_bb_partition)
1833 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1834 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1835 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1836 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1837 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1838 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1839 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1840 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1841 const_labelno++;
1842 cold_function_name = NULL_TREE;
1844 else
1846 crtl->subsections.hot_section_label = NULL;
1847 crtl->subsections.cold_section_label = NULL;
1848 crtl->subsections.hot_section_end_label = NULL;
1849 crtl->subsections.cold_section_end_label = NULL;
1852 /* The following code does not need preprocessing in the assembler. */
1854 app_disable ();
1856 if (CONSTANT_POOL_BEFORE_FUNCTION)
1857 output_constant_pool (fnname, decl);
1859 align = symtab_node::get (decl)->definition_alignment ();
1861 /* Make sure the not and cold text (code) sections are properly
1862 aligned. This is necessary here in the case where the function
1863 has both hot and cold sections, because we don't want to re-set
1864 the alignment when the section switch happens mid-function. */
1866 if (crtl->has_bb_partition)
1868 first_function_block_is_cold = false;
1870 switch_to_section (unlikely_text_section ());
1871 assemble_align (align);
1872 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1874 /* When the function starts with a cold section, we need to explicitly
1875 align the hot section and write out the hot section label.
1876 But if the current function is a thunk, we do not have a CFG. */
1877 if (!cfun->is_thunk
1878 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1880 switch_to_section (text_section);
1881 assemble_align (align);
1882 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1883 hot_label_written = true;
1884 first_function_block_is_cold = true;
1886 in_cold_section_p = first_function_block_is_cold;
1890 /* Switch to the correct text section for the start of the function. */
1892 switch_to_section (function_section (decl), decl);
1893 if (crtl->has_bb_partition && !hot_label_written)
1894 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1896 /* Tell assembler to move to target machine's alignment for functions. */
1897 align = floor_log2 (align / BITS_PER_UNIT);
1898 if (align > 0)
1900 ASM_OUTPUT_ALIGN (asm_out_file, align);
1903 /* Handle a user-specified function alignment.
1904 Note that we still need to align to DECL_ALIGN, as above,
1905 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1906 if (! DECL_USER_ALIGN (decl)
1907 && align_functions.levels[0].log > align
1908 && optimize_function_for_speed_p (cfun))
1910 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1911 int align_log = align_functions.levels[0].log;
1912 #endif
1913 int max_skip = align_functions.levels[0].maxskip;
1914 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1915 && max_skip >= crtl->max_insn_address)
1916 max_skip = crtl->max_insn_address - 1;
1918 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1919 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1920 if (max_skip == align_functions.levels[0].maxskip)
1921 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1922 align_functions.levels[1].log,
1923 align_functions.levels[1].maxskip);
1924 #else
1925 ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
1926 #endif
1929 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1930 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1931 #endif
1933 if (!DECL_IGNORED_P (decl))
1934 (*debug_hooks->begin_function) (decl);
1936 /* Make function name accessible from other files, if appropriate. */
1938 if (TREE_PUBLIC (decl))
1940 notice_global_symbol (decl);
1942 globalize_decl (decl);
1944 maybe_assemble_visibility (decl);
1947 if (DECL_PRESERVE_P (decl))
1948 targetm.asm_out.mark_decl_preserved (fnname);
1950 unsigned short patch_area_size = crtl->patch_area_size;
1951 unsigned short patch_area_entry = crtl->patch_area_entry;
1953 /* Emit the patching area before the entry label, if any. */
1954 if (patch_area_entry > 0)
1955 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1956 patch_area_entry, true);
1958 /* Do any machine/system dependent processing of the function name. */
1959 #ifdef ASM_DECLARE_FUNCTION_NAME
1960 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1961 #else
1962 /* Standard thing is just output label for the function. */
1963 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1964 #endif /* ASM_DECLARE_FUNCTION_NAME */
1966 /* And the area after the label. Record it if we haven't done so yet. */
1967 if (patch_area_size > patch_area_entry)
1968 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1969 patch_area_size
1970 - patch_area_entry,
1971 patch_area_entry == 0);
1973 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1974 saw_no_split_stack = true;
1977 /* Output assembler code associated with defining the size of the
1978 function. DECL describes the function. NAME is the function's name. */
1980 void
1981 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1983 #ifdef ASM_DECLARE_FUNCTION_SIZE
1984 /* We could have switched section in the middle of the function. */
1985 if (crtl->has_bb_partition)
1986 switch_to_section (function_section (decl));
1987 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1988 #endif
1989 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1991 output_constant_pool (fnname, decl);
1992 switch_to_section (function_section (decl)); /* need to switch back */
1994 /* Output labels for end of hot/cold text sections (to be used by
1995 debug info.) */
1996 if (crtl->has_bb_partition)
1998 section *save_text_section;
2000 save_text_section = in_section;
2001 switch_to_section (unlikely_text_section ());
2002 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
2003 if (cold_function_name != NULL_TREE)
2004 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
2005 IDENTIFIER_POINTER (cold_function_name),
2006 decl);
2007 #endif
2008 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
2009 if (first_function_block_is_cold)
2010 switch_to_section (text_section);
2011 else
2012 switch_to_section (function_section (decl));
2013 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
2014 switch_to_section (save_text_section);
2018 /* Assemble code to leave SIZE bytes of zeros. */
2020 void
2021 assemble_zeros (unsigned HOST_WIDE_INT size)
2023 /* Do no output if -fsyntax-only. */
2024 if (flag_syntax_only)
2025 return;
2027 #ifdef ASM_NO_SKIP_IN_TEXT
2028 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
2029 so we must output 0s explicitly in the text section. */
2030 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
2032 unsigned HOST_WIDE_INT i;
2033 for (i = 0; i < size; i++)
2034 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
2036 else
2037 #endif
2038 if (size > 0)
2039 ASM_OUTPUT_SKIP (asm_out_file, size);
2042 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
2044 void
2045 assemble_align (unsigned int align)
2047 if (align > BITS_PER_UNIT)
2049 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2053 /* Assemble a string constant with the specified C string as contents. */
2055 void
2056 assemble_string (const char *p, int size)
2058 int pos = 0;
2059 int maximum = 2000;
2061 /* If the string is very long, split it up. */
2063 while (pos < size)
2065 int thissize = size - pos;
2066 if (thissize > maximum)
2067 thissize = maximum;
2069 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
2071 pos += thissize;
2072 p += thissize;
2077 /* A noswitch_section_callback for lcomm_section. */
2079 static bool
2080 emit_local (tree decl ATTRIBUTE_UNUSED,
2081 const char *name ATTRIBUTE_UNUSED,
2082 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2083 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2085 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2086 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2087 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2088 size, align);
2089 return true;
2090 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2091 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2092 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2093 return true;
2094 #else
2095 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2096 return false;
2097 #endif
2100 /* A noswitch_section_callback for bss_noswitch_section. */
2102 #if defined ASM_OUTPUT_ALIGNED_BSS
2103 static bool
2104 emit_bss (tree decl ATTRIBUTE_UNUSED,
2105 const char *name ATTRIBUTE_UNUSED,
2106 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2107 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2109 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2110 get_variable_align (decl));
2111 return true;
2113 #endif
2115 /* A noswitch_section_callback for comm_section. */
2117 static bool
2118 emit_common (tree decl ATTRIBUTE_UNUSED,
2119 const char *name ATTRIBUTE_UNUSED,
2120 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2121 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2123 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2124 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2125 size, get_variable_align (decl));
2126 return true;
2127 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2128 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2129 get_variable_align (decl));
2130 return true;
2131 #else
2132 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2133 return false;
2134 #endif
2137 /* A noswitch_section_callback for tls_comm_section. */
2139 static bool
2140 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2141 const char *name ATTRIBUTE_UNUSED,
2142 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2143 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2145 #ifdef ASM_OUTPUT_TLS_COMMON
2146 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2147 return true;
2148 #else
2149 sorry ("thread-local COMMON data not implemented");
2150 return true;
2151 #endif
2154 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2155 NAME is the name of DECL's SYMBOL_REF. */
2157 static void
2158 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2159 unsigned int align)
2161 unsigned HOST_WIDE_INT size, rounded;
2163 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2164 rounded = size;
2166 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2167 size += asan_red_zone_size (size);
2169 /* Don't allocate zero bytes of common,
2170 since that means "undefined external" in the linker. */
2171 if (size == 0)
2172 rounded = 1;
2174 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2175 so that each uninitialized object starts on such a boundary. */
2176 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2177 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2178 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2180 if (!sect->noswitch.callback (decl, name, size, rounded)
2181 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2182 error ("requested alignment for %q+D is greater than "
2183 "implemented alignment of %wu", decl, rounded);
2186 /* A subroutine of assemble_variable. Output the label and contents of
2187 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2188 is as for assemble_variable. */
2190 static void
2191 assemble_variable_contents (tree decl, const char *name,
2192 bool dont_output_data, bool merge_strings)
2194 /* Do any machine/system dependent processing of the object. */
2195 #ifdef ASM_DECLARE_OBJECT_NAME
2196 last_assemble_variable_decl = decl;
2197 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2198 #else
2199 /* Standard thing is just output label for the object. */
2200 ASM_OUTPUT_LABEL (asm_out_file, name);
2201 #endif /* ASM_DECLARE_OBJECT_NAME */
2203 if (!dont_output_data)
2205 /* Caller is supposed to use varpool_get_constructor when it wants
2206 to output the body. */
2207 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2208 if (DECL_INITIAL (decl)
2209 && DECL_INITIAL (decl) != error_mark_node
2210 && !initializer_zerop (DECL_INITIAL (decl)))
2211 /* Output the actual data. */
2212 output_constant (DECL_INITIAL (decl),
2213 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2214 get_variable_align (decl),
2215 false, merge_strings);
2216 else
2217 /* Leave space for it. */
2218 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2219 targetm.asm_out.decl_end ();
2223 /* Write out assembly for the variable DECL, which is not defined in
2224 the current translation unit. */
2225 void
2226 assemble_undefined_decl (tree decl)
2228 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2229 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2232 /* Assemble everything that is needed for a variable or function declaration.
2233 Not used for automatic variables, and not used for function definitions.
2234 Should not be called for variables of incomplete structure type.
2236 TOP_LEVEL is nonzero if this variable has file scope.
2237 AT_END is nonzero if this is the special handling, at end of compilation,
2238 to define things that have had only tentative definitions.
2239 DONT_OUTPUT_DATA if nonzero means don't actually output the
2240 initial value (that will be done by the caller). */
2242 void
2243 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2244 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2246 const char *name;
2247 rtx decl_rtl, symbol;
2248 section *sect;
2249 unsigned int align;
2250 bool asan_protected = false;
2252 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2253 gcc_assert (VAR_P (decl));
2255 /* Emulated TLS had better not get this far. */
2256 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2258 last_assemble_variable_decl = 0;
2260 /* Normally no need to say anything here for external references,
2261 since assemble_external is called by the language-specific code
2262 when a declaration is first seen. */
2264 if (DECL_EXTERNAL (decl))
2265 return;
2267 /* Do nothing for global register variables. */
2268 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2270 TREE_ASM_WRITTEN (decl) = 1;
2271 return;
2274 /* If type was incomplete when the variable was declared,
2275 see if it is complete now. */
2277 if (DECL_SIZE (decl) == 0)
2278 layout_decl (decl, 0);
2280 /* Still incomplete => don't allocate it; treat the tentative defn
2281 (which is what it must have been) as an `extern' reference. */
2283 if (!dont_output_data && DECL_SIZE (decl) == 0)
2285 error ("storage size of %q+D isn%'t known", decl);
2286 TREE_ASM_WRITTEN (decl) = 1;
2287 return;
2290 /* The first declaration of a variable that comes through this function
2291 decides whether it is global (in C, has external linkage)
2292 or local (in C, has internal linkage). So do nothing more
2293 if this function has already run. */
2295 if (TREE_ASM_WRITTEN (decl))
2296 return;
2298 /* Make sure targetm.encode_section_info is invoked before we set
2299 ASM_WRITTEN. */
2300 decl_rtl = DECL_RTL (decl);
2302 TREE_ASM_WRITTEN (decl) = 1;
2304 /* Do no output if -fsyntax-only. */
2305 if (flag_syntax_only)
2306 return;
2308 if (! dont_output_data
2309 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2311 error ("size of variable %q+D is too large", decl);
2312 return;
2315 gcc_assert (MEM_P (decl_rtl));
2316 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2317 symbol = XEXP (decl_rtl, 0);
2319 /* If this symbol belongs to the tree constant pool, output the constant
2320 if it hasn't already been written. */
2321 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2323 tree decl = SYMBOL_REF_DECL (symbol);
2324 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2325 output_constant_def_contents (symbol);
2326 return;
2329 app_disable ();
2331 name = XSTR (symbol, 0);
2332 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2333 notice_global_symbol (decl);
2335 /* Compute the alignment of this data. */
2337 align_variable (decl, dont_output_data);
2339 if ((flag_sanitize & SANITIZE_ADDRESS)
2340 && asan_protect_global (decl))
2342 asan_protected = true;
2343 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2344 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2347 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2349 align = get_variable_align (decl);
2351 if (TREE_PUBLIC (decl))
2352 maybe_assemble_visibility (decl);
2354 if (DECL_PRESERVE_P (decl))
2355 targetm.asm_out.mark_decl_preserved (name);
2357 /* First make the assembler name(s) global if appropriate. */
2358 sect = get_variable_section (decl, false);
2359 if (TREE_PUBLIC (decl)
2360 && (sect->common.flags & SECTION_COMMON) == 0)
2361 globalize_decl (decl);
2363 /* Output any data that we will need to use the address of. */
2364 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2365 output_addressed_constants (DECL_INITIAL (decl), 0);
2367 /* dbxout.c needs to know this. */
2368 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2369 DECL_IN_TEXT_SECTION (decl) = 1;
2371 /* If the decl is part of an object_block, make sure that the decl
2372 has been positioned within its block, but do not write out its
2373 definition yet. output_object_blocks will do that later. */
2374 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2376 gcc_assert (!dont_output_data);
2377 place_block_symbol (symbol);
2379 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2380 assemble_noswitch_variable (decl, name, sect, align);
2381 else
2383 /* Special-case handling of vtv comdat sections. */
2384 if (sect->named.name
2385 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2386 handle_vtv_comdat_section (sect, decl);
2387 else
2388 switch_to_section (sect, decl);
2389 if (align > BITS_PER_UNIT)
2390 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2391 assemble_variable_contents (decl, name, dont_output_data,
2392 (sect->common.flags & SECTION_MERGE)
2393 && (sect->common.flags & SECTION_STRINGS));
2394 if (asan_protected)
2396 unsigned HOST_WIDE_INT int size
2397 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2398 assemble_zeros (asan_red_zone_size (size));
2404 /* Given a function declaration (FN_DECL), this function assembles the
2405 function into the .preinit_array section. */
2407 void
2408 assemble_vtv_preinit_initializer (tree fn_decl)
2410 section *sect;
2411 unsigned flags = SECTION_WRITE;
2412 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2414 flags |= SECTION_NOTYPE;
2415 sect = get_section (".preinit_array", flags, fn_decl);
2416 switch_to_section (sect);
2417 assemble_addr_to_section (symbol, sect);
2420 /* Return 1 if type TYPE contains any pointers. */
2422 static int
2423 contains_pointers_p (tree type)
2425 switch (TREE_CODE (type))
2427 case POINTER_TYPE:
2428 case REFERENCE_TYPE:
2429 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2430 so I'll play safe and return 1. */
2431 case OFFSET_TYPE:
2432 return 1;
2434 case RECORD_TYPE:
2435 case UNION_TYPE:
2436 case QUAL_UNION_TYPE:
2438 tree fields;
2439 /* For a type that has fields, see if the fields have pointers. */
2440 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2441 if (TREE_CODE (fields) == FIELD_DECL
2442 && contains_pointers_p (TREE_TYPE (fields)))
2443 return 1;
2444 return 0;
2447 case ARRAY_TYPE:
2448 /* An array type contains pointers if its element type does. */
2449 return contains_pointers_p (TREE_TYPE (type));
2451 default:
2452 return 0;
2456 /* We delay assemble_external processing until
2457 the compilation unit is finalized. This is the best we can do for
2458 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2459 it all the way to final. See PR 17982 for further discussion. */
2460 static GTY(()) tree pending_assemble_externals;
2462 #ifdef ASM_OUTPUT_EXTERNAL
2463 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2464 As a result, assemble_external can be called after the list of externals
2465 is processed and the pointer set destroyed. */
2466 static bool pending_assemble_externals_processed;
2468 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2469 TREE_LIST in assemble_external. */
2470 static hash_set<tree> *pending_assemble_externals_set;
2472 /* True if DECL is a function decl for which no out-of-line copy exists.
2473 It is assumed that DECL's assembler name has been set. */
2475 static bool
2476 incorporeal_function_p (tree decl)
2478 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl))
2480 const char *name;
2482 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2483 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2484 return true;
2486 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2487 /* Atomic or sync builtins which have survived this far will be
2488 resolved externally and therefore are not incorporeal. */
2489 if (strncmp (name, "__builtin_", 10) == 0)
2490 return true;
2492 return false;
2495 /* Actually do the tests to determine if this is necessary, and invoke
2496 ASM_OUTPUT_EXTERNAL. */
2497 static void
2498 assemble_external_real (tree decl)
2500 rtx rtl = DECL_RTL (decl);
2502 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2503 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2504 && !incorporeal_function_p (decl))
2506 /* Some systems do require some output. */
2507 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2508 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2511 #endif
2513 void
2514 process_pending_assemble_externals (void)
2516 #ifdef ASM_OUTPUT_EXTERNAL
2517 tree list;
2518 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2519 assemble_external_real (TREE_VALUE (list));
2521 pending_assemble_externals = 0;
2522 pending_assemble_externals_processed = true;
2523 delete pending_assemble_externals_set;
2524 #endif
2527 /* This TREE_LIST contains any weak symbol declarations waiting
2528 to be emitted. */
2529 static GTY(()) tree weak_decls;
2531 /* Output something to declare an external symbol to the assembler,
2532 and qualifiers such as weakness. (Most assemblers don't need
2533 extern declaration, so we normally output nothing.) Do nothing if
2534 DECL is not external. */
2536 void
2537 assemble_external (tree decl ATTRIBUTE_UNUSED)
2539 /* Make sure that the ASM_OUT_FILE is open.
2540 If it's not, we should not be calling this function. */
2541 gcc_assert (asm_out_file);
2543 /* In a perfect world, the following condition would be true.
2544 Sadly, the Go front end emit assembly *from the front end*,
2545 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2546 #if 0
2547 /* This function should only be called if we are expanding, or have
2548 expanded, to RTL.
2549 Ideally, only final.c would be calling this function, but it is
2550 not clear whether that would break things somehow. See PR 17982
2551 for further discussion. */
2552 gcc_assert (state == EXPANSION
2553 || state == FINISHED);
2554 #endif
2556 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2557 return;
2559 /* We want to output annotation for weak and external symbols at
2560 very last to check if they are references or not. */
2562 if (TARGET_SUPPORTS_WEAK
2563 && DECL_WEAK (decl)
2564 /* TREE_STATIC is a weird and abused creature which is not
2565 generally the right test for whether an entity has been
2566 locally emitted, inlined or otherwise not-really-extern, but
2567 for declarations that can be weak, it happens to be
2568 match. */
2569 && !TREE_STATIC (decl)
2570 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2571 && value_member (decl, weak_decls) == NULL_TREE)
2572 weak_decls = tree_cons (NULL, decl, weak_decls);
2574 #ifdef ASM_OUTPUT_EXTERNAL
2575 if (pending_assemble_externals_processed)
2577 assemble_external_real (decl);
2578 return;
2581 if (! pending_assemble_externals_set->add (decl))
2582 pending_assemble_externals = tree_cons (NULL, decl,
2583 pending_assemble_externals);
2584 #endif
2587 /* Similar, for calling a library function FUN. */
2589 void
2590 assemble_external_libcall (rtx fun)
2592 /* Declare library function name external when first used, if nec. */
2593 if (! SYMBOL_REF_USED (fun))
2595 SYMBOL_REF_USED (fun) = 1;
2596 targetm.asm_out.external_libcall (fun);
2600 /* Assemble a label named NAME. */
2602 void
2603 assemble_label (FILE *file, const char *name)
2605 ASM_OUTPUT_LABEL (file, name);
2608 /* Set the symbol_referenced flag for ID. */
2609 void
2610 mark_referenced (tree id)
2612 TREE_SYMBOL_REFERENCED (id) = 1;
2615 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2616 void
2617 mark_decl_referenced (tree decl)
2619 if (TREE_CODE (decl) == FUNCTION_DECL)
2621 /* Extern inline functions don't become needed when referenced.
2622 If we know a method will be emitted in other TU and no new
2623 functions can be marked reachable, just use the external
2624 definition. */
2625 struct cgraph_node *node = cgraph_node::get_create (decl);
2626 if (!DECL_EXTERNAL (decl)
2627 && !node->definition)
2628 node->mark_force_output ();
2630 else if (VAR_P (decl))
2632 varpool_node *node = varpool_node::get_create (decl);
2633 /* C++ frontend use mark_decl_references to force COMDAT variables
2634 to be output that might appear dead otherwise. */
2635 node->force_output = true;
2637 /* else do nothing - we can get various sorts of CST nodes here,
2638 which do not need to be marked. */
2642 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2643 starts with a *, the rest of NAME is output verbatim. Otherwise
2644 NAME is transformed in a target-specific way (usually by the
2645 addition of an underscore). */
2647 void
2648 assemble_name_raw (FILE *file, const char *name)
2650 if (name[0] == '*')
2651 fputs (&name[1], file);
2652 else
2653 ASM_OUTPUT_LABELREF (file, name);
2656 /* Return NAME that should actually be emitted, looking through
2657 transparent aliases. If NAME refers to an entity that is also
2658 represented as a tree (like a function or variable), mark the entity
2659 as referenced. */
2660 const char *
2661 assemble_name_resolve (const char *name)
2663 const char *real_name = targetm.strip_name_encoding (name);
2664 tree id = maybe_get_identifier (real_name);
2666 if (id)
2668 tree id_orig = id;
2670 mark_referenced (id);
2671 ultimate_transparent_alias_target (&id);
2672 if (id != id_orig)
2673 name = IDENTIFIER_POINTER (id);
2674 gcc_assert (! TREE_CHAIN (id));
2677 return name;
2680 /* Like assemble_name_raw, but should be used when NAME might refer to
2681 an entity that is also represented as a tree (like a function or
2682 variable). If NAME does refer to such an entity, that entity will
2683 be marked as referenced. */
2685 void
2686 assemble_name (FILE *file, const char *name)
2688 assemble_name_raw (file, assemble_name_resolve (name));
2691 /* Allocate SIZE bytes writable static space with a gensym name
2692 and return an RTX to refer to its address. */
2695 assemble_static_space (unsigned HOST_WIDE_INT size)
2697 char name[17];
2698 const char *namestring;
2699 rtx x;
2701 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2702 ++const_labelno;
2703 namestring = ggc_strdup (name);
2705 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2706 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2708 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2709 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2710 BIGGEST_ALIGNMENT);
2711 #else
2712 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2713 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2714 #else
2716 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2717 so that each uninitialized object starts on such a boundary. */
2718 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2719 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2720 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2721 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2722 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2723 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2725 #endif
2726 #endif
2727 return x;
2730 /* Assemble the static constant template for function entry trampolines.
2731 This is done at most once per compilation.
2732 Returns an RTX for the address of the template. */
2734 static GTY(()) rtx initial_trampoline;
2737 assemble_trampoline_template (void)
2739 char label[256];
2740 const char *name;
2741 int align;
2742 rtx symbol;
2744 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2746 if (initial_trampoline)
2747 return initial_trampoline;
2749 /* By default, put trampoline templates in read-only data section. */
2751 #ifdef TRAMPOLINE_SECTION
2752 switch_to_section (TRAMPOLINE_SECTION);
2753 #else
2754 switch_to_section (readonly_data_section);
2755 #endif
2757 /* Write the assembler code to define one. */
2758 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2759 if (align > 0)
2760 ASM_OUTPUT_ALIGN (asm_out_file, align);
2762 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2763 targetm.asm_out.trampoline_template (asm_out_file);
2765 /* Record the rtl to refer to it. */
2766 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2767 name = ggc_strdup (label);
2768 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2769 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2771 initial_trampoline = gen_const_mem (BLKmode, symbol);
2772 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2773 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2775 return initial_trampoline;
2778 /* A and B are either alignments or offsets. Return the minimum alignment
2779 that may be assumed after adding the two together. */
2781 static inline unsigned
2782 min_align (unsigned int a, unsigned int b)
2784 return least_bit_hwi (a | b);
2787 /* Return the assembler directive for creating a given kind of integer
2788 object. SIZE is the number of bytes in the object and ALIGNED_P
2789 indicates whether it is known to be aligned. Return NULL if the
2790 assembly dialect has no such directive.
2792 The returned string should be printed at the start of a new line and
2793 be followed immediately by the object's initial value. */
2795 const char *
2796 integer_asm_op (int size, int aligned_p)
2798 struct asm_int_op *ops;
2800 if (aligned_p)
2801 ops = &targetm.asm_out.aligned_op;
2802 else
2803 ops = &targetm.asm_out.unaligned_op;
2805 switch (size)
2807 case 1:
2808 return targetm.asm_out.byte_op;
2809 case 2:
2810 return ops->hi;
2811 case 3:
2812 return ops->psi;
2813 case 4:
2814 return ops->si;
2815 case 5:
2816 case 6:
2817 case 7:
2818 return ops->pdi;
2819 case 8:
2820 return ops->di;
2821 case 9:
2822 case 10:
2823 case 11:
2824 case 12:
2825 case 13:
2826 case 14:
2827 case 15:
2828 return ops->pti;
2829 case 16:
2830 return ops->ti;
2831 default:
2832 return NULL;
2836 /* Use directive OP to assemble an integer object X. Print OP at the
2837 start of the line, followed immediately by the value of X. */
2839 void
2840 assemble_integer_with_op (const char *op, rtx x)
2842 fputs (op, asm_out_file);
2843 output_addr_const (asm_out_file, x);
2844 fputc ('\n', asm_out_file);
2847 /* The default implementation of the asm_out.integer target hook. */
2849 bool
2850 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2851 unsigned int size ATTRIBUTE_UNUSED,
2852 int aligned_p ATTRIBUTE_UNUSED)
2854 const char *op = integer_asm_op (size, aligned_p);
2855 /* Avoid GAS bugs for large values. Specifically negative values whose
2856 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2857 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2858 return false;
2859 return op && (assemble_integer_with_op (op, x), true);
2862 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2863 the alignment of the integer in bits. Return 1 if we were able to output
2864 the constant, otherwise 0. We must be able to output the constant,
2865 if FORCE is nonzero. */
2867 bool
2868 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2870 int aligned_p;
2872 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2874 /* See if the target hook can handle this kind of object. */
2875 if (targetm.asm_out.integer (x, size, aligned_p))
2876 return true;
2878 /* If the object is a multi-byte one, try splitting it up. Split
2879 it into words it if is multi-word, otherwise split it into bytes. */
2880 if (size > 1)
2882 machine_mode omode, imode;
2883 unsigned int subalign;
2884 unsigned int subsize, i;
2885 enum mode_class mclass;
2887 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2888 subalign = MIN (align, subsize * BITS_PER_UNIT);
2889 if (GET_CODE (x) == CONST_FIXED)
2890 mclass = GET_MODE_CLASS (GET_MODE (x));
2891 else
2892 mclass = MODE_INT;
2894 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2895 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2897 for (i = 0; i < size; i += subsize)
2899 rtx partial = simplify_subreg (omode, x, imode, i);
2900 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2901 break;
2903 if (i == size)
2904 return true;
2906 /* If we've printed some of it, but not all of it, there's no going
2907 back now. */
2908 gcc_assert (!i);
2911 gcc_assert (!force);
2913 return false;
2916 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
2917 is the alignment of the constant in bits. If REVERSE is true, D is output
2918 in reverse storage order. */
2920 void
2921 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2922 bool reverse)
2924 long data[4] = {0, 0, 0, 0};
2925 int bitsize, nelts, nunits, units_per;
2926 rtx elt;
2928 /* This is hairy. We have a quantity of known size. real_to_target
2929 will put it into an array of *host* longs, 32 bits per element
2930 (even if long is more than 32 bits). We need to determine the
2931 number of array elements that are occupied (nelts) and the number
2932 of *target* min-addressable units that will be occupied in the
2933 object file (nunits). We cannot assume that 32 divides the
2934 mode's bitsize (size * BITS_PER_UNIT) evenly.
2936 size * BITS_PER_UNIT is used here to make sure that padding bits
2937 (which might appear at either end of the value; real_to_target
2938 will include the padding bits in its output array) are included. */
2940 nunits = GET_MODE_SIZE (mode);
2941 bitsize = nunits * BITS_PER_UNIT;
2942 nelts = CEIL (bitsize, 32);
2943 units_per = 32 / BITS_PER_UNIT;
2945 real_to_target (data, &d, mode);
2947 /* Put out the first word with the specified alignment. */
2948 unsigned int chunk_nunits = MIN (nunits, units_per);
2949 if (reverse)
2950 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
2951 else
2952 elt = GEN_INT (sext_hwi (data[0], chunk_nunits * BITS_PER_UNIT));
2953 assemble_integer (elt, chunk_nunits, align, 1);
2954 nunits -= chunk_nunits;
2956 /* Subsequent words need only 32-bit alignment. */
2957 align = min_align (align, 32);
2959 for (int i = 1; i < nelts; i++)
2961 chunk_nunits = MIN (nunits, units_per);
2962 if (reverse)
2963 elt = flip_storage_order (SImode,
2964 gen_int_mode (data[nelts - 1 - i], SImode));
2965 else
2966 elt = GEN_INT (sext_hwi (data[i], chunk_nunits * BITS_PER_UNIT));
2967 assemble_integer (elt, chunk_nunits, align, 1);
2968 nunits -= chunk_nunits;
2972 /* Given an expression EXP with a constant value,
2973 reduce it to the sum of an assembler symbol and an integer.
2974 Store them both in the structure *VALUE.
2975 EXP must be reducible. */
2977 class addr_const {
2978 public:
2979 rtx base;
2980 poly_int64 offset;
2983 static void
2984 decode_addr_const (tree exp, class addr_const *value)
2986 tree target = TREE_OPERAND (exp, 0);
2987 poly_int64 offset = 0;
2988 rtx x;
2990 while (1)
2992 poly_int64 bytepos;
2993 if (TREE_CODE (target) == COMPONENT_REF
2994 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
2995 &bytepos))
2997 offset += bytepos;
2998 target = TREE_OPERAND (target, 0);
3000 else if (TREE_CODE (target) == ARRAY_REF
3001 || TREE_CODE (target) == ARRAY_RANGE_REF)
3003 /* Truncate big offset. */
3004 offset
3005 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
3006 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
3007 target = TREE_OPERAND (target, 0);
3009 else if (TREE_CODE (target) == MEM_REF
3010 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
3012 offset += mem_ref_offset (target).force_shwi ();
3013 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
3015 else if (TREE_CODE (target) == INDIRECT_REF
3016 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
3017 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
3018 == ADDR_EXPR)
3019 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
3020 else
3021 break;
3024 switch (TREE_CODE (target))
3026 case VAR_DECL:
3027 case FUNCTION_DECL:
3028 x = DECL_RTL (target);
3029 break;
3031 case LABEL_DECL:
3032 x = gen_rtx_MEM (FUNCTION_MODE,
3033 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
3034 break;
3036 case REAL_CST:
3037 case FIXED_CST:
3038 case STRING_CST:
3039 case COMPLEX_CST:
3040 case CONSTRUCTOR:
3041 case INTEGER_CST:
3042 x = lookup_constant_def (target);
3043 /* Should have been added by output_addressed_constants. */
3044 gcc_assert (x);
3045 break;
3047 case INDIRECT_REF:
3048 /* This deals with absolute addresses. */
3049 offset += tree_to_shwi (TREE_OPERAND (target, 0));
3050 x = gen_rtx_MEM (QImode,
3051 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
3052 break;
3054 case COMPOUND_LITERAL_EXPR:
3055 gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
3056 x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
3057 break;
3059 default:
3060 gcc_unreachable ();
3063 gcc_assert (MEM_P (x));
3064 x = XEXP (x, 0);
3066 value->base = x;
3067 value->offset = offset;
3070 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
3072 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
3074 /* Constant pool accessor function. */
3076 hash_table<tree_descriptor_hasher> *
3077 constant_pool_htab (void)
3079 return const_desc_htab;
3082 /* Compute a hash code for a constant expression. */
3084 hashval_t
3085 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3087 return ptr->hash;
3090 static hashval_t
3091 const_hash_1 (const tree exp)
3093 const char *p;
3094 hashval_t hi;
3095 int len, i;
3096 enum tree_code code = TREE_CODE (exp);
3098 /* Either set P and LEN to the address and len of something to hash and
3099 exit the switch or return a value. */
3101 switch (code)
3103 case INTEGER_CST:
3104 p = (char *) &TREE_INT_CST_ELT (exp, 0);
3105 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3106 break;
3108 case REAL_CST:
3109 return real_hash (TREE_REAL_CST_PTR (exp));
3111 case FIXED_CST:
3112 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3114 case STRING_CST:
3115 p = TREE_STRING_POINTER (exp);
3116 len = TREE_STRING_LENGTH (exp);
3117 break;
3119 case COMPLEX_CST:
3120 return (const_hash_1 (TREE_REALPART (exp)) * 5
3121 + const_hash_1 (TREE_IMAGPART (exp)));
3123 case VECTOR_CST:
3125 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3126 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3127 unsigned int count = vector_cst_encoded_nelts (exp);
3128 for (unsigned int i = 0; i < count; ++i)
3129 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3130 return hi;
3133 case CONSTRUCTOR:
3135 unsigned HOST_WIDE_INT idx;
3136 tree value;
3138 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3140 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3141 if (value)
3142 hi = hi * 603 + const_hash_1 (value);
3144 return hi;
3147 case ADDR_EXPR:
3148 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3149 return const_hash_1 (TREE_OPERAND (exp, 0));
3151 /* Fallthru. */
3152 case FDESC_EXPR:
3154 class addr_const value;
3156 decode_addr_const (exp, &value);
3157 switch (GET_CODE (value.base))
3159 case SYMBOL_REF:
3160 /* Don't hash the address of the SYMBOL_REF;
3161 only use the offset and the symbol name. */
3162 hi = value.offset.coeffs[0];
3163 p = XSTR (value.base, 0);
3164 for (i = 0; p[i] != 0; i++)
3165 hi = ((hi * 613) + (unsigned) (p[i]));
3166 break;
3168 case LABEL_REF:
3169 hi = (value.offset.coeffs[0]
3170 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3171 break;
3173 default:
3174 gcc_unreachable ();
3177 return hi;
3179 case PLUS_EXPR:
3180 case POINTER_PLUS_EXPR:
3181 case MINUS_EXPR:
3182 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3183 + const_hash_1 (TREE_OPERAND (exp, 1)));
3185 CASE_CONVERT:
3186 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3188 default:
3189 /* A language specific constant. Just hash the code. */
3190 return code;
3193 /* Compute hashing function. */
3194 hi = len;
3195 for (i = 0; i < len; i++)
3196 hi = ((hi * 613) + (unsigned) (p[i]));
3198 return hi;
3201 /* Wrapper of compare_constant, for the htab interface. */
3202 bool
3203 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3204 constant_descriptor_tree *c2)
3206 if (c1->hash != c2->hash)
3207 return 0;
3208 return compare_constant (c1->value, c2->value);
3211 /* Compare t1 and t2, and return 1 only if they are known to result in
3212 the same bit pattern on output. */
3214 static int
3215 compare_constant (const tree t1, const tree t2)
3217 enum tree_code typecode;
3219 if (t1 == NULL_TREE)
3220 return t2 == NULL_TREE;
3221 if (t2 == NULL_TREE)
3222 return 0;
3224 if (TREE_CODE (t1) != TREE_CODE (t2))
3225 return 0;
3227 switch (TREE_CODE (t1))
3229 case INTEGER_CST:
3230 /* Integer constants are the same only if the same width of type. */
3231 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3232 return 0;
3233 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3234 return 0;
3235 return tree_int_cst_equal (t1, t2);
3237 case REAL_CST:
3238 /* Real constants are the same only if the same width of type. In
3239 addition to the same width, we need to check whether the modes are the
3240 same. There might be two floating point modes that are the same size
3241 but have different representations, such as the PowerPC that has 2
3242 different 128-bit floating point types (IBM extended double and IEEE
3243 128-bit floating point). */
3244 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3245 return 0;
3246 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3247 return 0;
3248 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3250 case FIXED_CST:
3251 /* Fixed constants are the same only if the same width of type. */
3252 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3253 return 0;
3255 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3257 case STRING_CST:
3258 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3259 || int_size_in_bytes (TREE_TYPE (t1))
3260 != int_size_in_bytes (TREE_TYPE (t2)))
3261 return 0;
3263 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3264 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3265 TREE_STRING_LENGTH (t1)));
3267 case COMPLEX_CST:
3268 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3269 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3271 case VECTOR_CST:
3273 if (VECTOR_CST_NPATTERNS (t1)
3274 != VECTOR_CST_NPATTERNS (t2))
3275 return 0;
3277 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3278 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3279 return 0;
3281 unsigned int count = vector_cst_encoded_nelts (t1);
3282 for (unsigned int i = 0; i < count; ++i)
3283 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3284 VECTOR_CST_ENCODED_ELT (t2, i)))
3285 return 0;
3287 return 1;
3290 case CONSTRUCTOR:
3292 vec<constructor_elt, va_gc> *v1, *v2;
3293 unsigned HOST_WIDE_INT idx;
3295 typecode = TREE_CODE (TREE_TYPE (t1));
3296 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3297 return 0;
3299 if (typecode == ARRAY_TYPE)
3301 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3302 /* For arrays, check that mode, size and storage order match. */
3303 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3304 || size_1 == -1
3305 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3306 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3307 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3308 return 0;
3310 else
3312 /* For record and union constructors, require exact type
3313 equality. */
3314 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3315 return 0;
3318 v1 = CONSTRUCTOR_ELTS (t1);
3319 v2 = CONSTRUCTOR_ELTS (t2);
3320 if (vec_safe_length (v1) != vec_safe_length (v2))
3321 return 0;
3323 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3325 constructor_elt *c1 = &(*v1)[idx];
3326 constructor_elt *c2 = &(*v2)[idx];
3328 /* Check that each value is the same... */
3329 if (!compare_constant (c1->value, c2->value))
3330 return 0;
3331 /* ... and that they apply to the same fields! */
3332 if (typecode == ARRAY_TYPE)
3334 if (!compare_constant (c1->index, c2->index))
3335 return 0;
3337 else
3339 if (c1->index != c2->index)
3340 return 0;
3344 return 1;
3347 case ADDR_EXPR:
3348 case FDESC_EXPR:
3350 class addr_const value1, value2;
3351 enum rtx_code code;
3352 int ret;
3354 decode_addr_const (t1, &value1);
3355 decode_addr_const (t2, &value2);
3357 if (maybe_ne (value1.offset, value2.offset))
3358 return 0;
3360 code = GET_CODE (value1.base);
3361 if (code != GET_CODE (value2.base))
3362 return 0;
3364 switch (code)
3366 case SYMBOL_REF:
3367 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3368 break;
3370 case LABEL_REF:
3371 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3372 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3373 break;
3375 default:
3376 gcc_unreachable ();
3378 return ret;
3381 case PLUS_EXPR:
3382 case POINTER_PLUS_EXPR:
3383 case MINUS_EXPR:
3384 case RANGE_EXPR:
3385 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3386 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3388 CASE_CONVERT:
3389 case VIEW_CONVERT_EXPR:
3390 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3392 default:
3393 return 0;
3396 gcc_unreachable ();
3399 /* Return the section into which constant EXP should be placed. */
3401 static section *
3402 get_constant_section (tree exp, unsigned int align)
3404 return targetm.asm_out.select_section (exp,
3405 compute_reloc_for_constant (exp),
3406 align);
3409 /* Return the size of constant EXP in bytes. */
3411 static HOST_WIDE_INT
3412 get_constant_size (tree exp)
3414 HOST_WIDE_INT size;
3416 size = int_size_in_bytes (TREE_TYPE (exp));
3417 gcc_checking_assert (size >= 0);
3418 gcc_checking_assert (TREE_CODE (exp) != STRING_CST
3419 || size >= TREE_STRING_LENGTH (exp));
3420 return size;
3423 /* Subroutine of output_constant_def:
3424 No constant equal to EXP is known to have been output.
3425 Make a constant descriptor to enter EXP in the hash table.
3426 Assign the label number and construct RTL to refer to the
3427 constant's location in memory.
3428 Caller is responsible for updating the hash table. */
3430 static struct constant_descriptor_tree *
3431 build_constant_desc (tree exp)
3433 struct constant_descriptor_tree *desc;
3434 rtx symbol, rtl;
3435 char label[256];
3436 int labelno;
3437 tree decl;
3439 desc = ggc_alloc<constant_descriptor_tree> ();
3440 desc->value = exp;
3442 /* Create a string containing the label name, in LABEL. */
3443 labelno = const_labelno++;
3444 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3446 /* Construct the VAR_DECL associated with the constant. */
3447 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3448 TREE_TYPE (exp));
3449 DECL_ARTIFICIAL (decl) = 1;
3450 DECL_IGNORED_P (decl) = 1;
3451 TREE_READONLY (decl) = 1;
3452 TREE_STATIC (decl) = 1;
3453 TREE_ADDRESSABLE (decl) = 1;
3454 /* We don't set the RTL yet as this would cause varpool to assume that the
3455 variable is referenced. Moreover, it would just be dropped in LTO mode.
3456 Instead we set the flag that will be recognized in make_decl_rtl. */
3457 DECL_IN_CONSTANT_POOL (decl) = 1;
3458 DECL_INITIAL (decl) = desc->value;
3459 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3460 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3461 if (TREE_CODE (exp) == STRING_CST)
3462 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3463 else
3465 align_variable (decl, 0);
3466 if (DECL_ALIGN (decl) < GET_MODE_ALIGNMENT (DECL_MODE (decl))
3467 && ((optab_handler (movmisalign_optab, DECL_MODE (decl))
3468 != CODE_FOR_nothing)
3469 || targetm.slow_unaligned_access (DECL_MODE (decl),
3470 DECL_ALIGN (decl))))
3471 SET_DECL_ALIGN (decl, GET_MODE_ALIGNMENT (DECL_MODE (decl)));
3474 /* Now construct the SYMBOL_REF and the MEM. */
3475 if (use_object_blocks_p ())
3477 int align = (TREE_CODE (decl) == CONST_DECL
3478 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3479 ? DECL_ALIGN (decl)
3480 : symtab_node::get (decl)->definition_alignment ());
3481 section *sect = get_constant_section (exp, align);
3482 symbol = create_block_symbol (ggc_strdup (label),
3483 get_block_for_section (sect), -1);
3485 else
3486 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3487 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3488 SET_SYMBOL_REF_DECL (symbol, decl);
3489 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3491 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3492 set_mem_alias_set (rtl, 0);
3494 /* Putting EXP into the literal pool might have imposed a different
3495 alignment which should be visible in the RTX as well. */
3496 set_mem_align (rtl, DECL_ALIGN (decl));
3498 /* We cannot share RTX'es in pool entries.
3499 Mark this piece of RTL as required for unsharing. */
3500 RTX_FLAG (rtl, used) = 1;
3502 /* Set flags or add text to the name to record information, such as
3503 that it is a local symbol. If the name is changed, the macro
3504 ASM_OUTPUT_LABELREF will have to know how to strip this
3505 information. This call might invalidate our local variable
3506 SYMBOL; we can't use it afterward. */
3507 targetm.encode_section_info (exp, rtl, true);
3509 desc->rtl = rtl;
3511 return desc;
3514 /* Subroutine of output_constant_def and tree_output_constant_def:
3515 Add a constant to the hash table that tracks which constants
3516 already have labels. */
3518 static constant_descriptor_tree *
3519 add_constant_to_table (tree exp, int defer)
3521 /* The hash table methods may call output_constant_def for addressed
3522 constants, so handle them first. */
3523 output_addressed_constants (exp, defer);
3525 /* Sanity check to catch recursive insertion. */
3526 static bool inserting;
3527 gcc_assert (!inserting);
3528 inserting = true;
3530 /* Look up EXP in the table of constant descriptors. If we didn't
3531 find it, create a new one. */
3532 struct constant_descriptor_tree key;
3533 key.value = exp;
3534 key.hash = const_hash_1 (exp);
3535 constant_descriptor_tree **loc
3536 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3538 inserting = false;
3540 struct constant_descriptor_tree *desc = *loc;
3541 if (!desc)
3543 desc = build_constant_desc (exp);
3544 desc->hash = key.hash;
3545 *loc = desc;
3548 return desc;
3551 /* Return an rtx representing a reference to constant data in memory
3552 for the constant expression EXP.
3554 If assembler code for such a constant has already been output,
3555 return an rtx to refer to it.
3556 Otherwise, output such a constant in memory
3557 and generate an rtx for it.
3559 If DEFER is nonzero, this constant can be deferred and output only
3560 if referenced in the function after all optimizations.
3562 `const_desc_table' records which constants already have label strings. */
3565 output_constant_def (tree exp, int defer)
3567 struct constant_descriptor_tree *desc = add_constant_to_table (exp, defer);
3568 maybe_output_constant_def_contents (desc, defer);
3569 return desc->rtl;
3572 /* Subroutine of output_constant_def: Decide whether or not we need to
3573 output the constant DESC now, and if so, do it. */
3574 static void
3575 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3576 int defer)
3578 rtx symbol = XEXP (desc->rtl, 0);
3579 tree exp = desc->value;
3581 if (flag_syntax_only)
3582 return;
3584 if (TREE_ASM_WRITTEN (exp))
3585 /* Already output; don't do it again. */
3586 return;
3588 /* We can always defer constants as long as the context allows
3589 doing so. */
3590 if (defer)
3592 /* Increment n_deferred_constants if it exists. It needs to be at
3593 least as large as the number of constants actually referred to
3594 by the function. If it's too small we'll stop looking too early
3595 and fail to emit constants; if it's too large we'll only look
3596 through the entire function when we could have stopped earlier. */
3597 if (cfun)
3598 n_deferred_constants++;
3599 return;
3602 output_constant_def_contents (symbol);
3605 /* Subroutine of output_constant_def_contents. Output the definition
3606 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3607 constant's alignment in bits. */
3609 static void
3610 assemble_constant_contents (tree exp, const char *label, unsigned int align,
3611 bool merge_strings)
3613 HOST_WIDE_INT size;
3615 size = get_constant_size (exp);
3617 /* Do any machine/system dependent processing of the constant. */
3618 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3620 /* Output the value of EXP. */
3621 output_constant (exp, size, align, false, merge_strings);
3623 targetm.asm_out.decl_end ();
3626 /* We must output the constant data referred to by SYMBOL; do so. */
3628 static void
3629 output_constant_def_contents (rtx symbol)
3631 tree decl = SYMBOL_REF_DECL (symbol);
3632 tree exp = DECL_INITIAL (decl);
3633 bool asan_protected = false;
3635 /* Make sure any other constants whose addresses appear in EXP
3636 are assigned label numbers. */
3637 output_addressed_constants (exp, 0);
3639 /* We are no longer deferring this constant. */
3640 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3642 if ((flag_sanitize & SANITIZE_ADDRESS)
3643 && TREE_CODE (exp) == STRING_CST
3644 && asan_protect_global (exp))
3646 asan_protected = true;
3647 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3648 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3651 /* If the constant is part of an object block, make sure that the
3652 decl has been positioned within its block, but do not write out
3653 its definition yet. output_object_blocks will do that later. */
3654 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3655 place_block_symbol (symbol);
3656 else
3658 int align = (TREE_CODE (decl) == CONST_DECL
3659 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3660 ? DECL_ALIGN (decl)
3661 : symtab_node::get (decl)->definition_alignment ());
3662 section *sect = get_constant_section (exp, align);
3663 switch_to_section (sect);
3664 if (align > BITS_PER_UNIT)
3665 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3666 assemble_constant_contents (exp, XSTR (symbol, 0), align,
3667 (sect->common.flags & SECTION_MERGE)
3668 && (sect->common.flags & SECTION_STRINGS));
3669 if (asan_protected)
3671 HOST_WIDE_INT size = get_constant_size (exp);
3672 assemble_zeros (asan_red_zone_size (size));
3677 /* Look up EXP in the table of constant descriptors. Return the rtl
3678 if it has been emitted, else null. */
3681 lookup_constant_def (tree exp)
3683 struct constant_descriptor_tree key;
3685 key.value = exp;
3686 key.hash = const_hash_1 (exp);
3687 constant_descriptor_tree *desc
3688 = const_desc_htab->find_with_hash (&key, key.hash);
3690 return (desc ? desc->rtl : NULL_RTX);
3693 /* Return a tree representing a reference to constant data in memory
3694 for the constant expression EXP.
3696 This is the counterpart of output_constant_def at the Tree level. */
3698 tree
3699 tree_output_constant_def (tree exp)
3701 struct constant_descriptor_tree *desc = add_constant_to_table (exp, 1);
3702 tree decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3703 varpool_node::finalize_decl (decl);
3704 return decl;
3707 class GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3708 public:
3709 class constant_descriptor_rtx *next;
3710 rtx mem;
3711 rtx sym;
3712 rtx constant;
3713 HOST_WIDE_INT offset;
3714 hashval_t hash;
3715 fixed_size_mode mode;
3716 unsigned int align;
3717 int labelno;
3718 int mark;
3721 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3723 static hashval_t hash (constant_descriptor_rtx *);
3724 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3727 /* Used in the hash tables to avoid outputting the same constant
3728 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3729 are output once per function, not once per file. */
3730 /* ??? Only a few targets need per-function constant pools. Most
3731 can use one per-file pool. Should add a targetm bit to tell the
3732 difference. */
3734 struct GTY(()) rtx_constant_pool {
3735 /* Pointers to first and last constant in pool, as ordered by offset. */
3736 class constant_descriptor_rtx *first;
3737 class constant_descriptor_rtx *last;
3739 /* Hash facility for making memory-constants from constant rtl-expressions.
3740 It is used on RISC machines where immediate integer arguments and
3741 constant addresses are restricted so that such constants must be stored
3742 in memory. */
3743 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3745 /* Current offset in constant pool (does not include any
3746 machine-specific header). */
3747 HOST_WIDE_INT offset;
3750 /* Hash and compare functions for const_rtx_htab. */
3752 hashval_t
3753 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3755 return desc->hash;
3758 bool
3759 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3760 constant_descriptor_rtx *y)
3762 if (x->mode != y->mode)
3763 return 0;
3764 return rtx_equal_p (x->constant, y->constant);
3767 /* Hash one component of a constant. */
3769 static hashval_t
3770 const_rtx_hash_1 (const_rtx x)
3772 unsigned HOST_WIDE_INT hwi;
3773 machine_mode mode;
3774 enum rtx_code code;
3775 hashval_t h;
3776 int i;
3778 code = GET_CODE (x);
3779 mode = GET_MODE (x);
3780 h = (hashval_t) code * 1048573 + mode;
3782 switch (code)
3784 case CONST_INT:
3785 hwi = INTVAL (x);
3787 fold_hwi:
3789 int shift = sizeof (hashval_t) * CHAR_BIT;
3790 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3792 h ^= (hashval_t) hwi;
3793 for (i = 1; i < n; ++i)
3795 hwi >>= shift;
3796 h ^= (hashval_t) hwi;
3799 break;
3801 case CONST_WIDE_INT:
3802 hwi = 0;
3804 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3805 hwi ^= CONST_WIDE_INT_ELT (x, i);
3806 goto fold_hwi;
3809 case CONST_DOUBLE:
3810 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3812 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3813 goto fold_hwi;
3815 else
3816 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3817 break;
3819 case CONST_FIXED:
3820 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3821 break;
3823 case SYMBOL_REF:
3824 h ^= htab_hash_string (XSTR (x, 0));
3825 break;
3827 case LABEL_REF:
3828 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3829 break;
3831 case UNSPEC:
3832 case UNSPEC_VOLATILE:
3833 h = h * 251 + XINT (x, 1);
3834 break;
3836 default:
3837 break;
3840 return h;
3843 /* Compute a hash value for X, which should be a constant. */
3845 static hashval_t
3846 const_rtx_hash (rtx x)
3848 hashval_t h = 0;
3849 subrtx_iterator::array_type array;
3850 FOR_EACH_SUBRTX (iter, array, x, ALL)
3851 h = h * 509 + const_rtx_hash_1 (*iter);
3852 return h;
3856 /* Create and return a new rtx constant pool. */
3858 static struct rtx_constant_pool *
3859 create_constant_pool (void)
3861 struct rtx_constant_pool *pool;
3863 pool = ggc_alloc<rtx_constant_pool> ();
3864 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3865 pool->first = NULL;
3866 pool->last = NULL;
3867 pool->offset = 0;
3868 return pool;
3871 /* Initialize constant pool hashing for a new function. */
3873 void
3874 init_varasm_status (void)
3876 crtl->varasm.pool = create_constant_pool ();
3877 crtl->varasm.deferred_constants = 0;
3880 /* Given a MINUS expression, simplify it if both sides
3881 include the same symbol. */
3884 simplify_subtraction (rtx x)
3886 rtx r = simplify_rtx (x);
3887 return r ? r : x;
3890 /* Given a constant rtx X, make (or find) a memory constant for its value
3891 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
3892 of X. */
3895 force_const_mem (machine_mode in_mode, rtx x)
3897 class constant_descriptor_rtx *desc, tmp;
3898 struct rtx_constant_pool *pool;
3899 char label[256];
3900 rtx def, symbol;
3901 hashval_t hash;
3902 unsigned int align;
3903 constant_descriptor_rtx **slot;
3904 fixed_size_mode mode;
3906 /* We can't force variable-sized objects to memory. */
3907 if (!is_a <fixed_size_mode> (in_mode, &mode))
3908 return NULL_RTX;
3910 /* If we're not allowed to drop X into the constant pool, don't. */
3911 if (targetm.cannot_force_const_mem (mode, x))
3912 return NULL_RTX;
3914 /* Record that this function has used a constant pool entry. */
3915 crtl->uses_const_pool = 1;
3917 /* Decide which pool to use. */
3918 pool = (targetm.use_blocks_for_constant_p (mode, x)
3919 ? shared_constant_pool
3920 : crtl->varasm.pool);
3922 /* Lookup the value in the hashtable. */
3923 tmp.constant = x;
3924 tmp.mode = mode;
3925 hash = const_rtx_hash (x);
3926 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3927 desc = *slot;
3929 /* If the constant was already present, return its memory. */
3930 if (desc)
3931 return copy_rtx (desc->mem);
3933 /* Otherwise, create a new descriptor. */
3934 desc = ggc_alloc<constant_descriptor_rtx> ();
3935 *slot = desc;
3937 /* Align the location counter as required by EXP's data type. */
3938 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3939 align = targetm.static_rtx_alignment (align_mode);
3941 pool->offset += (align / BITS_PER_UNIT) - 1;
3942 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3944 desc->next = NULL;
3945 desc->constant = copy_rtx (tmp.constant);
3946 desc->offset = pool->offset;
3947 desc->hash = hash;
3948 desc->mode = mode;
3949 desc->align = align;
3950 desc->labelno = const_labelno;
3951 desc->mark = 0;
3953 pool->offset += GET_MODE_SIZE (mode);
3954 if (pool->last)
3955 pool->last->next = desc;
3956 else
3957 pool->first = pool->last = desc;
3958 pool->last = desc;
3960 /* Create a string containing the label name, in LABEL. */
3961 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3962 ++const_labelno;
3964 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3965 the constants pool. */
3966 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3968 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3969 symbol = create_block_symbol (ggc_strdup (label),
3970 get_block_for_section (sect), -1);
3972 else
3973 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3974 desc->sym = symbol;
3975 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3976 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3977 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3979 /* Construct the MEM. */
3980 desc->mem = def = gen_const_mem (mode, symbol);
3981 set_mem_align (def, align);
3983 /* If we're dropping a label to the constant pool, make sure we
3984 don't delete it. */
3985 if (GET_CODE (x) == LABEL_REF)
3986 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3988 return copy_rtx (def);
3991 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3994 get_pool_constant (const_rtx addr)
3996 return SYMBOL_REF_CONSTANT (addr)->constant;
3999 /* Given a constant pool SYMBOL_REF, return the corresponding constant
4000 and whether it has been output or not. */
4003 get_pool_constant_mark (rtx addr, bool *pmarked)
4005 class constant_descriptor_rtx *desc;
4007 desc = SYMBOL_REF_CONSTANT (addr);
4008 *pmarked = (desc->mark != 0);
4009 return desc->constant;
4012 /* Similar, return the mode. */
4014 fixed_size_mode
4015 get_pool_mode (const_rtx addr)
4017 return SYMBOL_REF_CONSTANT (addr)->mode;
4020 /* Return TRUE if and only if the constant pool has no entries. Note
4021 that even entries we might end up choosing not to emit are counted
4022 here, so there is the potential for missed optimizations. */
4024 bool
4025 constant_pool_empty_p (void)
4027 return crtl->varasm.pool->first == NULL;
4030 /* Worker function for output_constant_pool_1. Emit assembly for X
4031 in MODE with known alignment ALIGN. */
4033 static void
4034 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
4036 switch (GET_MODE_CLASS (mode))
4038 case MODE_FLOAT:
4039 case MODE_DECIMAL_FLOAT:
4041 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
4042 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
4043 as_a <scalar_float_mode> (mode), align, false);
4044 break;
4047 case MODE_INT:
4048 case MODE_PARTIAL_INT:
4049 case MODE_FRACT:
4050 case MODE_UFRACT:
4051 case MODE_ACCUM:
4052 case MODE_UACCUM:
4053 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
4054 break;
4056 case MODE_VECTOR_BOOL:
4058 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4060 /* Pick the smallest integer mode that contains at least one
4061 whole element. Often this is byte_mode and contains more
4062 than one element. */
4063 unsigned int nelts = GET_MODE_NUNITS (mode);
4064 unsigned int elt_bits = GET_MODE_BITSIZE (mode) / nelts;
4065 unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
4066 scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
4068 /* Build the constant up one integer at a time. */
4069 unsigned int elts_per_int = int_bits / elt_bits;
4070 for (unsigned int i = 0; i < nelts; i += elts_per_int)
4072 unsigned HOST_WIDE_INT value = 0;
4073 unsigned int limit = MIN (nelts - i, elts_per_int);
4074 for (unsigned int j = 0; j < limit; ++j)
4075 if (INTVAL (CONST_VECTOR_ELT (x, i + j)) != 0)
4076 value |= 1 << (j * elt_bits);
4077 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
4078 i != 0 ? MIN (align, int_bits) : align);
4080 break;
4082 case MODE_VECTOR_FLOAT:
4083 case MODE_VECTOR_INT:
4084 case MODE_VECTOR_FRACT:
4085 case MODE_VECTOR_UFRACT:
4086 case MODE_VECTOR_ACCUM:
4087 case MODE_VECTOR_UACCUM:
4089 int i, units;
4090 scalar_mode submode = GET_MODE_INNER (mode);
4091 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4093 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4094 units = GET_MODE_NUNITS (mode);
4096 for (i = 0; i < units; i++)
4098 rtx elt = CONST_VECTOR_ELT (x, i);
4099 output_constant_pool_2 (submode, elt, i ? subalign : align);
4102 break;
4104 default:
4105 gcc_unreachable ();
4109 /* Worker function for output_constant_pool. Emit constant DESC,
4110 giving it ALIGN bits of alignment. */
4112 static void
4113 output_constant_pool_1 (class constant_descriptor_rtx *desc,
4114 unsigned int align)
4116 rtx x, tmp;
4118 x = desc->constant;
4120 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4121 whose CODE_LABEL has been deleted. This can occur if a jump table
4122 is eliminated by optimization. If so, write a constant of zero
4123 instead. Note that this can also happen by turning the
4124 CODE_LABEL into a NOTE. */
4125 /* ??? This seems completely and utterly wrong. Certainly it's
4126 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4127 functioning even with rtx_insn::deleted and friends. */
4129 tmp = x;
4130 switch (GET_CODE (tmp))
4132 case CONST:
4133 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4134 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4135 break;
4136 tmp = XEXP (XEXP (tmp, 0), 0);
4137 /* FALLTHRU */
4139 case LABEL_REF:
4141 rtx_insn *insn = label_ref_label (tmp);
4142 gcc_assert (!insn->deleted ());
4143 gcc_assert (!NOTE_P (insn)
4144 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4145 break;
4148 default:
4149 break;
4152 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4153 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4154 align, desc->labelno, done);
4155 #endif
4157 assemble_align (align);
4159 /* Output the label. */
4160 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4162 /* Output the data.
4163 Pass actual alignment value while emitting string constant to asm code
4164 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4165 assuming that the data is already aligned which prevents the generation
4166 of fix-up table entries. */
4167 output_constant_pool_2 (desc->mode, x, desc->align);
4169 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4170 sections have proper size. */
4171 if (align > GET_MODE_BITSIZE (desc->mode)
4172 && in_section
4173 && (in_section->common.flags & SECTION_MERGE))
4174 assemble_align (align);
4176 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4177 done:
4178 #endif
4179 return;
4182 /* Recompute the offsets of entries in POOL, and the overall size of
4183 POOL. Do this after calling mark_constant_pool to ensure that we
4184 are computing the offset values for the pool which we will actually
4185 emit. */
4187 static void
4188 recompute_pool_offsets (struct rtx_constant_pool *pool)
4190 class constant_descriptor_rtx *desc;
4191 pool->offset = 0;
4193 for (desc = pool->first; desc ; desc = desc->next)
4194 if (desc->mark)
4196 /* Recalculate offset. */
4197 unsigned int align = desc->align;
4198 pool->offset += (align / BITS_PER_UNIT) - 1;
4199 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4200 desc->offset = pool->offset;
4201 pool->offset += GET_MODE_SIZE (desc->mode);
4205 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4206 Emit referenced deferred strings. */
4208 static void
4209 mark_constants_in_pattern (rtx insn)
4211 subrtx_iterator::array_type array;
4212 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4214 const_rtx x = *iter;
4215 if (GET_CODE (x) == SYMBOL_REF)
4217 if (CONSTANT_POOL_ADDRESS_P (x))
4219 class constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4220 if (desc->mark == 0)
4222 desc->mark = 1;
4223 iter.substitute (desc->constant);
4226 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4228 tree decl = SYMBOL_REF_DECL (x);
4229 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4231 n_deferred_constants--;
4232 output_constant_def_contents (CONST_CAST_RTX (x));
4239 /* Look through appropriate parts of INSN, marking all entries in the
4240 constant pool which are actually being used. Entries that are only
4241 referenced by other constants are also marked as used. Emit
4242 deferred strings that are used. */
4244 static void
4245 mark_constants (rtx_insn *insn)
4247 if (!INSN_P (insn))
4248 return;
4250 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4251 insns, not any notes that may be attached. We don't want to mark
4252 a constant just because it happens to appear in a REG_EQUIV note. */
4253 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4255 int i, n = seq->len ();
4256 for (i = 0; i < n; ++i)
4258 rtx subinsn = seq->element (i);
4259 if (INSN_P (subinsn))
4260 mark_constants_in_pattern (subinsn);
4263 else
4264 mark_constants_in_pattern (insn);
4267 /* Look through the instructions for this function, and mark all the
4268 entries in POOL which are actually being used. Emit deferred constants
4269 which have indeed been used. */
4271 static void
4272 mark_constant_pool (void)
4274 rtx_insn *insn;
4276 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4277 return;
4279 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4280 mark_constants (insn);
4283 /* Write all the constants in POOL. */
4285 static void
4286 output_constant_pool_contents (struct rtx_constant_pool *pool)
4288 class constant_descriptor_rtx *desc;
4290 for (desc = pool->first; desc ; desc = desc->next)
4291 if (desc->mark < 0)
4293 #ifdef ASM_OUTPUT_DEF
4294 const char *name = targetm.strip_name_encoding (XSTR (desc->sym, 0));
4295 char label[256];
4296 char buffer[256 + 32];
4297 const char *p;
4299 ASM_GENERATE_INTERNAL_LABEL (label, "LC", ~desc->mark);
4300 p = targetm.strip_name_encoding (label);
4301 if (desc->offset)
4303 sprintf (buffer, "%s+%ld", p, (long) (desc->offset));
4304 p = buffer;
4306 ASM_OUTPUT_DEF (asm_out_file, name, p);
4307 #else
4308 gcc_unreachable ();
4309 #endif
4311 else if (desc->mark)
4313 /* If the constant is part of an object_block, make sure that
4314 the constant has been positioned within its block, but do not
4315 write out its definition yet. output_object_blocks will do
4316 that later. */
4317 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4318 && SYMBOL_REF_BLOCK (desc->sym))
4319 place_block_symbol (desc->sym);
4320 else
4322 switch_to_section (targetm.asm_out.select_rtx_section
4323 (desc->mode, desc->constant, desc->align));
4324 output_constant_pool_1 (desc, desc->align);
4329 struct constant_descriptor_rtx_data {
4330 constant_descriptor_rtx *desc;
4331 target_unit *bytes;
4332 unsigned short size;
4333 unsigned short offset;
4334 unsigned int hash;
4337 /* qsort callback to sort constant_descriptor_rtx_data * vector by
4338 decreasing size. */
4340 static int
4341 constant_descriptor_rtx_data_cmp (const void *p1, const void *p2)
4343 constant_descriptor_rtx_data *const data1
4344 = *(constant_descriptor_rtx_data * const *) p1;
4345 constant_descriptor_rtx_data *const data2
4346 = *(constant_descriptor_rtx_data * const *) p2;
4347 if (data1->size > data2->size)
4348 return -1;
4349 if (data1->size < data2->size)
4350 return 1;
4351 if (data1->hash < data2->hash)
4352 return -1;
4353 gcc_assert (data1->hash > data2->hash);
4354 return 1;
4357 struct const_rtx_data_hasher : nofree_ptr_hash<constant_descriptor_rtx_data>
4359 static hashval_t hash (constant_descriptor_rtx_data *);
4360 static bool equal (constant_descriptor_rtx_data *,
4361 constant_descriptor_rtx_data *);
4364 /* Hash and compare functions for const_rtx_data_htab. */
4366 hashval_t
4367 const_rtx_data_hasher::hash (constant_descriptor_rtx_data *data)
4369 return data->hash;
4372 bool
4373 const_rtx_data_hasher::equal (constant_descriptor_rtx_data *x,
4374 constant_descriptor_rtx_data *y)
4376 if (x->hash != y->hash || x->size != y->size)
4377 return 0;
4378 unsigned int align1 = x->desc->align;
4379 unsigned int align2 = y->desc->align;
4380 unsigned int offset1 = (x->offset * BITS_PER_UNIT) & (align1 - 1);
4381 unsigned int offset2 = (y->offset * BITS_PER_UNIT) & (align2 - 1);
4382 if (offset1)
4383 align1 = least_bit_hwi (offset1);
4384 if (offset2)
4385 align2 = least_bit_hwi (offset2);
4386 if (align2 > align1)
4387 return 0;
4388 if (memcmp (x->bytes, y->bytes, x->size * sizeof (target_unit)) != 0)
4389 return 0;
4390 return 1;
4393 /* Attempt to optimize constant pool POOL. If it contains both CONST_VECTOR
4394 constants and scalar constants with the values of CONST_VECTOR elements,
4395 try to alias the scalar constants with the CONST_VECTOR elements. */
4397 static void
4398 optimize_constant_pool (struct rtx_constant_pool *pool)
4400 auto_vec<target_unit, 128> buffer;
4401 auto_vec<constant_descriptor_rtx_data *, 128> vec;
4402 object_allocator<constant_descriptor_rtx_data>
4403 data_pool ("constant_descriptor_rtx_data_pool");
4404 int idx = 0;
4405 size_t size = 0;
4406 for (constant_descriptor_rtx *desc = pool->first; desc; desc = desc->next)
4407 if (desc->mark > 0
4408 && ! (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4409 && SYMBOL_REF_BLOCK (desc->sym)))
4411 buffer.truncate (0);
4412 buffer.reserve (GET_MODE_SIZE (desc->mode));
4413 if (native_encode_rtx (desc->mode, desc->constant, buffer, 0,
4414 GET_MODE_SIZE (desc->mode)))
4416 constant_descriptor_rtx_data *data = data_pool.allocate ();
4417 data->desc = desc;
4418 data->bytes = NULL;
4419 data->size = GET_MODE_SIZE (desc->mode);
4420 data->offset = 0;
4421 data->hash = idx++;
4422 size += data->size;
4423 vec.safe_push (data);
4426 if (idx)
4428 vec.qsort (constant_descriptor_rtx_data_cmp);
4429 unsigned min_size = vec.last ()->size;
4430 target_unit *bytes = XNEWVEC (target_unit, size);
4431 unsigned int i;
4432 constant_descriptor_rtx_data *data;
4433 hash_table<const_rtx_data_hasher> * htab
4434 = new hash_table<const_rtx_data_hasher> (31);
4435 size = 0;
4436 FOR_EACH_VEC_ELT (vec, i, data)
4438 buffer.truncate (0);
4439 native_encode_rtx (data->desc->mode, data->desc->constant,
4440 buffer, 0, data->size);
4441 memcpy (bytes + size, buffer.address (), data->size);
4442 data->bytes = bytes + size;
4443 data->hash = iterative_hash (data->bytes,
4444 data->size * sizeof (target_unit), 0);
4445 size += data->size;
4446 constant_descriptor_rtx_data **slot
4447 = htab->find_slot_with_hash (data, data->hash, INSERT);
4448 if (*slot)
4450 data->desc->mark = ~(*slot)->desc->labelno;
4451 data->desc->offset = (*slot)->offset;
4453 else
4455 unsigned int sz = 1 << floor_log2 (data->size);
4457 *slot = data;
4458 for (sz >>= 1; sz >= min_size; sz >>= 1)
4459 for (unsigned off = 0; off + sz <= data->size; off += sz)
4461 constant_descriptor_rtx_data tmp;
4462 tmp.desc = data->desc;
4463 tmp.bytes = data->bytes + off;
4464 tmp.size = sz;
4465 tmp.offset = off;
4466 tmp.hash = iterative_hash (tmp.bytes,
4467 sz * sizeof (target_unit), 0);
4468 slot = htab->find_slot_with_hash (&tmp, tmp.hash, INSERT);
4469 if (*slot == NULL)
4471 *slot = data_pool.allocate ();
4472 **slot = tmp;
4477 delete htab;
4478 XDELETE (bytes);
4480 data_pool.release ();
4483 /* Mark all constants that are used in the current function, then write
4484 out the function's private constant pool. */
4486 static void
4487 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4488 tree fndecl ATTRIBUTE_UNUSED)
4490 struct rtx_constant_pool *pool = crtl->varasm.pool;
4492 /* It is possible for gcc to call force_const_mem and then to later
4493 discard the instructions which refer to the constant. In such a
4494 case we do not need to output the constant. */
4495 mark_constant_pool ();
4497 /* Having marked the constant pool entries we'll actually emit, we
4498 now need to rebuild the offset information, which may have become
4499 stale. */
4500 recompute_pool_offsets (pool);
4502 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4503 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4504 #endif
4506 output_constant_pool_contents (pool);
4508 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4509 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4510 #endif
4513 /* Write the contents of the shared constant pool. */
4515 void
4516 output_shared_constant_pool (void)
4518 if (optimize
4519 && TARGET_SUPPORTS_ALIASES)
4520 optimize_constant_pool (shared_constant_pool);
4522 output_constant_pool_contents (shared_constant_pool);
4525 /* Determine what kind of relocations EXP may need. */
4528 compute_reloc_for_constant (tree exp)
4530 int reloc = 0, reloc2;
4531 tree tem;
4533 switch (TREE_CODE (exp))
4535 case ADDR_EXPR:
4536 case FDESC_EXPR:
4537 /* Go inside any operations that get_inner_reference can handle and see
4538 if what's inside is a constant: no need to do anything here for
4539 addresses of variables or functions. */
4540 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4541 tem = TREE_OPERAND (tem, 0))
4544 if (TREE_CODE (tem) == MEM_REF
4545 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4547 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4548 break;
4551 if (!targetm.binds_local_p (tem))
4552 reloc |= 2;
4553 else
4554 reloc |= 1;
4555 break;
4557 case PLUS_EXPR:
4558 case POINTER_PLUS_EXPR:
4559 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4560 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4561 break;
4563 case MINUS_EXPR:
4564 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4565 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4566 /* The difference of two local labels is computable at link time. */
4567 if (reloc == 1 && reloc2 == 1)
4568 reloc = 0;
4569 else
4570 reloc |= reloc2;
4571 break;
4573 CASE_CONVERT:
4574 case VIEW_CONVERT_EXPR:
4575 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4576 break;
4578 case CONSTRUCTOR:
4580 unsigned HOST_WIDE_INT idx;
4581 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4582 if (tem != 0)
4583 reloc |= compute_reloc_for_constant (tem);
4585 break;
4587 default:
4588 break;
4590 return reloc;
4593 /* Find all the constants whose addresses are referenced inside of EXP,
4594 and make sure assembler code with a label has been output for each one.
4595 Indicate whether an ADDR_EXPR has been encountered. */
4597 static void
4598 output_addressed_constants (tree exp, int defer)
4600 tree tem;
4602 switch (TREE_CODE (exp))
4604 case ADDR_EXPR:
4605 case FDESC_EXPR:
4606 /* Go inside any operations that get_inner_reference can handle and see
4607 if what's inside is a constant: no need to do anything here for
4608 addresses of variables or functions. */
4609 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4610 tem = TREE_OPERAND (tem, 0))
4613 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4614 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4615 tem = DECL_INITIAL (tem);
4617 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4618 output_constant_def (tem, defer);
4620 if (TREE_CODE (tem) == MEM_REF)
4621 output_addressed_constants (TREE_OPERAND (tem, 0), defer);
4622 break;
4624 case PLUS_EXPR:
4625 case POINTER_PLUS_EXPR:
4626 case MINUS_EXPR:
4627 output_addressed_constants (TREE_OPERAND (exp, 1), defer);
4628 gcc_fallthrough ();
4630 CASE_CONVERT:
4631 case VIEW_CONVERT_EXPR:
4632 output_addressed_constants (TREE_OPERAND (exp, 0), defer);
4633 break;
4635 case CONSTRUCTOR:
4637 unsigned HOST_WIDE_INT idx;
4638 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4639 if (tem != 0)
4640 output_addressed_constants (tem, defer);
4642 break;
4644 default:
4645 break;
4649 /* Whether a constructor CTOR is a valid static constant initializer if all
4650 its elements are. This used to be internal to initializer_constant_valid_p
4651 and has been exposed to let other functions like categorize_ctor_elements
4652 evaluate the property while walking a constructor for other purposes. */
4654 bool
4655 constructor_static_from_elts_p (const_tree ctor)
4657 return (TREE_CONSTANT (ctor)
4658 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4659 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4660 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4663 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4664 tree *cache);
4666 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4667 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4668 which are valid when ENDTYPE is an integer of any size; in
4669 particular, this does not accept a pointer minus a constant. This
4670 returns null_pointer_node if the VALUE is an absolute constant
4671 which can be used to initialize a static variable. Otherwise it
4672 returns NULL. */
4674 static tree
4675 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4677 tree op0, op1;
4679 if (!INTEGRAL_TYPE_P (endtype))
4680 return NULL_TREE;
4682 op0 = TREE_OPERAND (value, 0);
4683 op1 = TREE_OPERAND (value, 1);
4685 /* Like STRIP_NOPS except allow the operand mode to widen. This
4686 works around a feature of fold that simplifies (int)(p1 - p2) to
4687 ((int)p1 - (int)p2) under the theory that the narrower operation
4688 is cheaper. */
4690 while (CONVERT_EXPR_P (op0)
4691 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4693 tree inner = TREE_OPERAND (op0, 0);
4694 if (inner == error_mark_node
4695 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4696 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4697 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4698 break;
4699 op0 = inner;
4702 while (CONVERT_EXPR_P (op1)
4703 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4705 tree inner = TREE_OPERAND (op1, 0);
4706 if (inner == error_mark_node
4707 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4708 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4709 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4710 break;
4711 op1 = inner;
4714 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4715 if (!op0)
4716 return NULL_TREE;
4718 op1 = initializer_constant_valid_p_1 (op1, endtype,
4719 cache ? cache + 2 : NULL);
4720 /* Both initializers must be known. */
4721 if (op1)
4723 if (op0 == op1
4724 && (op0 == null_pointer_node
4725 || TREE_CODE (value) == MINUS_EXPR))
4726 return null_pointer_node;
4728 /* Support differences between labels. */
4729 if (TREE_CODE (op0) == LABEL_DECL
4730 && TREE_CODE (op1) == LABEL_DECL)
4731 return null_pointer_node;
4733 if (TREE_CODE (op0) == STRING_CST
4734 && TREE_CODE (op1) == STRING_CST
4735 && operand_equal_p (op0, op1, 1))
4736 return null_pointer_node;
4739 return NULL_TREE;
4742 /* Helper function of initializer_constant_valid_p.
4743 Return nonzero if VALUE is a valid constant-valued expression
4744 for use in initializing a static variable; one that can be an
4745 element of a "constant" initializer.
4747 Return null_pointer_node if the value is absolute;
4748 if it is relocatable, return the variable that determines the relocation.
4749 We assume that VALUE has been folded as much as possible;
4750 therefore, we do not need to check for such things as
4751 arithmetic-combinations of integers.
4753 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4755 static tree
4756 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4758 tree ret;
4760 switch (TREE_CODE (value))
4762 case CONSTRUCTOR:
4763 if (constructor_static_from_elts_p (value))
4765 unsigned HOST_WIDE_INT idx;
4766 tree elt;
4767 bool absolute = true;
4769 if (cache && cache[0] == value)
4770 return cache[1];
4771 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4773 tree reloc;
4774 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4775 NULL);
4776 if (!reloc
4777 /* An absolute value is required with reverse SSO. */
4778 || (reloc != null_pointer_node
4779 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4780 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4782 if (cache)
4784 cache[0] = value;
4785 cache[1] = NULL_TREE;
4787 return NULL_TREE;
4789 if (reloc != null_pointer_node)
4790 absolute = false;
4792 /* For a non-absolute relocation, there is no single
4793 variable that can be "the variable that determines the
4794 relocation." */
4795 if (cache)
4797 cache[0] = value;
4798 cache[1] = absolute ? null_pointer_node : error_mark_node;
4800 return absolute ? null_pointer_node : error_mark_node;
4803 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4805 case INTEGER_CST:
4806 case VECTOR_CST:
4807 case REAL_CST:
4808 case FIXED_CST:
4809 case STRING_CST:
4810 case COMPLEX_CST:
4811 return null_pointer_node;
4813 case ADDR_EXPR:
4814 case FDESC_EXPR:
4816 tree op0 = staticp (TREE_OPERAND (value, 0));
4817 if (op0)
4819 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4820 to be a constant, this is old-skool offsetof-like nonsense. */
4821 if (TREE_CODE (op0) == INDIRECT_REF
4822 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4823 return null_pointer_node;
4824 /* Taking the address of a nested function involves a trampoline,
4825 unless we don't need or want one. */
4826 if (TREE_CODE (op0) == FUNCTION_DECL
4827 && DECL_STATIC_CHAIN (op0)
4828 && !TREE_NO_TRAMPOLINE (value))
4829 return NULL_TREE;
4830 /* "&{...}" requires a temporary to hold the constructed
4831 object. */
4832 if (TREE_CODE (op0) == CONSTRUCTOR)
4833 return NULL_TREE;
4835 return op0;
4838 case NON_LVALUE_EXPR:
4839 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4840 endtype, cache);
4842 case VIEW_CONVERT_EXPR:
4844 tree src = TREE_OPERAND (value, 0);
4845 tree src_type = TREE_TYPE (src);
4846 tree dest_type = TREE_TYPE (value);
4848 /* Allow view-conversions from aggregate to non-aggregate type only
4849 if the bit pattern is fully preserved afterwards; otherwise, the
4850 RTL expander won't be able to apply a subsequent transformation
4851 to the underlying constructor. */
4852 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4854 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4855 return initializer_constant_valid_p_1 (src, endtype, cache);
4856 else
4857 return NULL_TREE;
4860 /* Allow all other kinds of view-conversion. */
4861 return initializer_constant_valid_p_1 (src, endtype, cache);
4864 CASE_CONVERT:
4866 tree src = TREE_OPERAND (value, 0);
4867 tree src_type = TREE_TYPE (src);
4868 tree dest_type = TREE_TYPE (value);
4870 /* Allow conversions between pointer types, floating-point
4871 types, and offset types. */
4872 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4873 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4874 || (TREE_CODE (dest_type) == OFFSET_TYPE
4875 && TREE_CODE (src_type) == OFFSET_TYPE))
4876 return initializer_constant_valid_p_1 (src, endtype, cache);
4878 /* Allow length-preserving conversions between integer types. */
4879 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4880 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4881 return initializer_constant_valid_p_1 (src, endtype, cache);
4883 /* Allow conversions between other integer types only if
4884 explicit value. Don't allow sign-extension to a type larger
4885 than word and pointer, there aren't relocations that would
4886 allow to sign extend it to a wider type. */
4887 if (INTEGRAL_TYPE_P (dest_type)
4888 && INTEGRAL_TYPE_P (src_type)
4889 && (TYPE_UNSIGNED (src_type)
4890 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4891 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4892 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4894 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4895 if (inner == null_pointer_node)
4896 return null_pointer_node;
4897 break;
4900 /* Allow (int) &foo provided int is as wide as a pointer. */
4901 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4902 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4903 return initializer_constant_valid_p_1 (src, endtype, cache);
4905 /* Likewise conversions from int to pointers, but also allow
4906 conversions from 0. */
4907 if ((POINTER_TYPE_P (dest_type)
4908 || TREE_CODE (dest_type) == OFFSET_TYPE)
4909 && INTEGRAL_TYPE_P (src_type))
4911 if (TREE_CODE (src) == INTEGER_CST
4912 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4913 return null_pointer_node;
4914 if (integer_zerop (src))
4915 return null_pointer_node;
4916 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4917 return initializer_constant_valid_p_1 (src, endtype, cache);
4920 /* Allow conversions to struct or union types if the value
4921 inside is okay. */
4922 if (TREE_CODE (dest_type) == RECORD_TYPE
4923 || TREE_CODE (dest_type) == UNION_TYPE)
4924 return initializer_constant_valid_p_1 (src, endtype, cache);
4926 break;
4928 case POINTER_PLUS_EXPR:
4929 case PLUS_EXPR:
4930 /* Any valid floating-point constants will have been folded by now;
4931 with -frounding-math we hit this with addition of two constants. */
4932 if (TREE_CODE (endtype) == REAL_TYPE)
4933 return NULL_TREE;
4934 if (cache && cache[0] == value)
4935 return cache[1];
4936 if (! INTEGRAL_TYPE_P (endtype)
4937 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4939 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4940 tree valid0
4941 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4942 endtype, ncache);
4943 tree valid1
4944 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4945 endtype, ncache + 2);
4946 /* If either term is absolute, use the other term's relocation. */
4947 if (valid0 == null_pointer_node)
4948 ret = valid1;
4949 else if (valid1 == null_pointer_node)
4950 ret = valid0;
4951 /* Support narrowing pointer differences. */
4952 else
4953 ret = narrowing_initializer_constant_valid_p (value, endtype,
4954 ncache);
4956 else
4957 /* Support narrowing pointer differences. */
4958 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4959 if (cache)
4961 cache[0] = value;
4962 cache[1] = ret;
4964 return ret;
4966 case POINTER_DIFF_EXPR:
4967 case MINUS_EXPR:
4968 if (TREE_CODE (endtype) == REAL_TYPE)
4969 return NULL_TREE;
4970 if (cache && cache[0] == value)
4971 return cache[1];
4972 if (! INTEGRAL_TYPE_P (endtype)
4973 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4975 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4976 tree valid0
4977 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4978 endtype, ncache);
4979 tree valid1
4980 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4981 endtype, ncache + 2);
4982 /* Win if second argument is absolute. */
4983 if (valid1 == null_pointer_node)
4984 ret = valid0;
4985 /* Win if both arguments have the same relocation.
4986 Then the value is absolute. */
4987 else if (valid0 == valid1 && valid0 != 0)
4988 ret = null_pointer_node;
4989 /* Since GCC guarantees that string constants are unique in the
4990 generated code, a subtraction between two copies of the same
4991 constant string is absolute. */
4992 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4993 && valid1 && TREE_CODE (valid1) == STRING_CST
4994 && operand_equal_p (valid0, valid1, 1))
4995 ret = null_pointer_node;
4996 /* Support narrowing differences. */
4997 else
4998 ret = narrowing_initializer_constant_valid_p (value, endtype,
4999 ncache);
5001 else
5002 /* Support narrowing differences. */
5003 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
5004 if (cache)
5006 cache[0] = value;
5007 cache[1] = ret;
5009 return ret;
5011 default:
5012 break;
5015 return NULL_TREE;
5018 /* Return nonzero if VALUE is a valid constant-valued expression
5019 for use in initializing a static variable; one that can be an
5020 element of a "constant" initializer.
5022 Return null_pointer_node if the value is absolute;
5023 if it is relocatable, return the variable that determines the relocation.
5024 We assume that VALUE has been folded as much as possible;
5025 therefore, we do not need to check for such things as
5026 arithmetic-combinations of integers. */
5027 tree
5028 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
5030 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
5032 /* An absolute value is required with reverse storage order. */
5033 if (reloc
5034 && reloc != null_pointer_node
5035 && reverse
5036 && !AGGREGATE_TYPE_P (endtype)
5037 && !VECTOR_TYPE_P (endtype))
5038 reloc = NULL_TREE;
5040 return reloc;
5043 /* Return true if VALUE is a valid constant-valued expression
5044 for use in initializing a static bit-field; one that can be
5045 an element of a "constant" initializer. */
5047 bool
5048 initializer_constant_valid_for_bitfield_p (tree value)
5050 /* For bitfields we support integer constants or possibly nested aggregates
5051 of such. */
5052 switch (TREE_CODE (value))
5054 case CONSTRUCTOR:
5056 unsigned HOST_WIDE_INT idx;
5057 tree elt;
5059 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
5060 if (!initializer_constant_valid_for_bitfield_p (elt))
5061 return false;
5062 return true;
5065 case INTEGER_CST:
5066 case REAL_CST:
5067 return true;
5069 case VIEW_CONVERT_EXPR:
5070 case NON_LVALUE_EXPR:
5071 return
5072 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
5074 default:
5075 break;
5078 return false;
5081 /* Check if a STRING_CST fits into the field.
5082 Tolerate only the case when the NUL termination
5083 does not fit into the field. */
5085 static bool
5086 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
5088 tree type = TREE_TYPE (string);
5089 tree eltype = TREE_TYPE (type);
5090 unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
5091 unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5092 int len = TREE_STRING_LENGTH (string);
5094 if (elts != 1 && elts != 2 && elts != 4)
5095 return false;
5096 if (len < 0 || len % elts != 0)
5097 return false;
5098 if (size < (unsigned)len)
5099 return false;
5100 if (mem_size != size)
5101 return false;
5102 return true;
5105 /* output_constructor outer state of relevance in recursive calls, typically
5106 for nested aggregate bitfields. */
5108 struct oc_outer_state {
5109 unsigned int bit_offset; /* current position in ... */
5110 int byte; /* ... the outer byte buffer. */
5113 static unsigned HOST_WIDE_INT
5114 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
5115 oc_outer_state *);
5117 /* Output assembler code for constant EXP, with no label.
5118 This includes the pseudo-op such as ".int" or ".byte", and a newline.
5119 Assumes output_addressed_constants has been done on EXP already.
5121 Generate at least SIZE bytes of assembler data, padding at the end
5122 with zeros if necessary. SIZE must always be specified. The returned
5123 value is the actual number of bytes of assembler data generated, which
5124 may be bigger than SIZE if the object contains a variable length field.
5126 SIZE is important for structure constructors,
5127 since trailing members may have been omitted from the constructor.
5128 It is also important for initialization of arrays from string constants
5129 since the full length of the string constant might not be wanted.
5130 It is also needed for initialization of unions, where the initializer's
5131 type is just one member, and that may not be as long as the union.
5133 There a case in which we would fail to output exactly SIZE bytes:
5134 for a structure constructor that wants to produce more than SIZE bytes.
5135 But such constructors will never be generated for any possible input.
5137 ALIGN is the alignment of the data in bits.
5139 If REVERSE is true, EXP is output in reverse storage order. */
5141 static unsigned HOST_WIDE_INT
5142 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5143 bool reverse, bool merge_strings)
5145 enum tree_code code;
5146 unsigned HOST_WIDE_INT thissize;
5147 rtx cst;
5149 if (size == 0 || flag_syntax_only)
5150 return size;
5152 /* See if we're trying to initialize a pointer in a non-default mode
5153 to the address of some declaration somewhere. If the target says
5154 the mode is valid for pointers, assume the target has a way of
5155 resolving it. */
5156 if (TREE_CODE (exp) == NOP_EXPR
5157 && POINTER_TYPE_P (TREE_TYPE (exp))
5158 && targetm.addr_space.valid_pointer_mode
5159 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5160 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5162 tree saved_type = TREE_TYPE (exp);
5164 /* Peel off any intermediate conversions-to-pointer for valid
5165 pointer modes. */
5166 while (TREE_CODE (exp) == NOP_EXPR
5167 && POINTER_TYPE_P (TREE_TYPE (exp))
5168 && targetm.addr_space.valid_pointer_mode
5169 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5170 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5171 exp = TREE_OPERAND (exp, 0);
5173 /* If what we're left with is the address of something, we can
5174 convert the address to the final type and output it that
5175 way. */
5176 if (TREE_CODE (exp) == ADDR_EXPR)
5177 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
5178 /* Likewise for constant ints. */
5179 else if (TREE_CODE (exp) == INTEGER_CST)
5180 exp = fold_convert (saved_type, exp);
5184 /* Eliminate any conversions since we'll be outputting the underlying
5185 constant. */
5186 while (CONVERT_EXPR_P (exp)
5187 || TREE_CODE (exp) == NON_LVALUE_EXPR
5188 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
5190 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
5191 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
5193 /* Make sure eliminating the conversion is really a no-op, except with
5194 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
5195 union types to allow for Ada unchecked unions. */
5196 if (type_size > op_size
5197 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
5198 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
5199 /* Keep the conversion. */
5200 break;
5201 else
5202 exp = TREE_OPERAND (exp, 0);
5205 code = TREE_CODE (TREE_TYPE (exp));
5206 thissize = int_size_in_bytes (TREE_TYPE (exp));
5208 /* Allow a constructor with no elements for any data type.
5209 This means to fill the space with zeros. */
5210 if (TREE_CODE (exp) == CONSTRUCTOR
5211 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
5213 assemble_zeros (size);
5214 return size;
5217 if (TREE_CODE (exp) == FDESC_EXPR)
5219 #ifdef ASM_OUTPUT_FDESC
5220 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
5221 tree decl = TREE_OPERAND (exp, 0);
5222 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
5223 #else
5224 gcc_unreachable ();
5225 #endif
5226 return size;
5229 /* Now output the underlying data. If we've handling the padding, return.
5230 Otherwise, break and ensure SIZE is the size written. */
5231 switch (code)
5233 case BOOLEAN_TYPE:
5234 case INTEGER_TYPE:
5235 case ENUMERAL_TYPE:
5236 case POINTER_TYPE:
5237 case REFERENCE_TYPE:
5238 case OFFSET_TYPE:
5239 case FIXED_POINT_TYPE:
5240 case NULLPTR_TYPE:
5241 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5242 if (reverse)
5243 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5244 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5245 error ("initializer for integer/fixed-point value is too complicated");
5246 break;
5248 case REAL_TYPE:
5249 if (TREE_CODE (exp) != REAL_CST)
5250 error ("initializer for floating value is not a floating constant");
5251 else
5252 assemble_real (TREE_REAL_CST (exp),
5253 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
5254 align, reverse);
5255 break;
5257 case COMPLEX_TYPE:
5258 output_constant (TREE_REALPART (exp), thissize / 2, align,
5259 reverse, false);
5260 output_constant (TREE_IMAGPART (exp), thissize / 2,
5261 min_align (align, BITS_PER_UNIT * (thissize / 2)),
5262 reverse, false);
5263 break;
5265 case ARRAY_TYPE:
5266 case VECTOR_TYPE:
5267 switch (TREE_CODE (exp))
5269 case CONSTRUCTOR:
5270 return output_constructor (exp, size, align, reverse, NULL);
5271 case STRING_CST:
5272 thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5273 if (merge_strings
5274 && (thissize == 0
5275 || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5276 thissize++;
5277 gcc_checking_assert (check_string_literal (exp, size));
5278 assemble_string (TREE_STRING_POINTER (exp), thissize);
5279 break;
5280 case VECTOR_CST:
5282 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5283 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5284 int elt_size = GET_MODE_SIZE (inner);
5285 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5286 reverse, false);
5287 thissize = elt_size;
5288 /* Static constants must have a fixed size. */
5289 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5290 for (unsigned int i = 1; i < nunits; i++)
5292 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5293 reverse, false);
5294 thissize += elt_size;
5296 break;
5298 default:
5299 gcc_unreachable ();
5301 break;
5303 case RECORD_TYPE:
5304 case UNION_TYPE:
5305 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5306 return output_constructor (exp, size, align, reverse, NULL);
5308 case ERROR_MARK:
5309 return 0;
5311 default:
5312 gcc_unreachable ();
5315 if (size > thissize)
5316 assemble_zeros (size - thissize);
5318 return size;
5321 /* Subroutine of output_constructor, used for computing the size of
5322 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
5323 type with an unspecified upper bound. */
5325 static unsigned HOST_WIDE_INT
5326 array_size_for_constructor (tree val)
5328 tree max_index;
5329 unsigned HOST_WIDE_INT cnt;
5330 tree index, value, tmp;
5331 offset_int i;
5333 /* This code used to attempt to handle string constants that are not
5334 arrays of single-bytes, but nothing else does, so there's no point in
5335 doing it here. */
5336 if (TREE_CODE (val) == STRING_CST)
5337 return TREE_STRING_LENGTH (val);
5339 max_index = NULL_TREE;
5340 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5342 if (TREE_CODE (index) == RANGE_EXPR)
5343 index = TREE_OPERAND (index, 1);
5344 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5345 max_index = index;
5348 if (max_index == NULL_TREE)
5349 return 0;
5351 /* Compute the total number of array elements. */
5352 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5353 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5355 /* Multiply by the array element unit size to find number of bytes. */
5356 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5358 gcc_assert (wi::fits_uhwi_p (i));
5359 return i.to_uhwi ();
5362 /* Other datastructures + helpers for output_constructor. */
5364 /* output_constructor local state to support interaction with helpers. */
5366 struct oc_local_state {
5368 /* Received arguments. */
5369 tree exp; /* Constructor expression. */
5370 tree type; /* Type of constructor expression. */
5371 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5372 unsigned int align; /* Known initial alignment. */
5373 tree min_index; /* Lower bound if specified for an array. */
5375 /* Output processing state. */
5376 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5377 int byte; /* Part of a bitfield byte yet to be output. */
5378 int last_relative_index; /* Implicit or explicit index of the last
5379 array element output within a bitfield. */
5380 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5381 bool reverse; /* Whether reverse storage order is in use. */
5383 /* Current element. */
5384 tree field; /* Current field decl in a record. */
5385 tree val; /* Current element value. */
5386 tree index; /* Current element index. */
5390 /* Helper for output_constructor. From the current LOCAL state, output a
5391 RANGE_EXPR element. */
5393 static void
5394 output_constructor_array_range (oc_local_state *local)
5396 /* Perform the index calculation in modulo arithmetic but
5397 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5398 but we are using an unsigned sizetype. */
5399 unsigned prec = TYPE_PRECISION (sizetype);
5400 offset_int idx = wi::sext (wi::to_offset (TREE_OPERAND (local->index, 0))
5401 - wi::to_offset (local->min_index), prec);
5402 tree valtype = TREE_TYPE (local->val);
5403 HOST_WIDE_INT fieldpos
5404 = (idx * wi::to_offset (TYPE_SIZE_UNIT (valtype))).to_short_addr ();
5406 /* Advance to offset of this element. */
5407 if (fieldpos > local->total_bytes)
5409 assemble_zeros (fieldpos - local->total_bytes);
5410 local->total_bytes = fieldpos;
5412 else
5413 /* Must not go backwards. */
5414 gcc_assert (fieldpos == local->total_bytes);
5416 unsigned HOST_WIDE_INT fieldsize
5417 = int_size_in_bytes (TREE_TYPE (local->type));
5419 HOST_WIDE_INT lo_index
5420 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5421 HOST_WIDE_INT hi_index
5422 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5423 HOST_WIDE_INT index;
5425 unsigned int align2
5426 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5428 for (index = lo_index; index <= hi_index; index++)
5430 /* Output the element's initial value. */
5431 if (local->val == NULL_TREE)
5432 assemble_zeros (fieldsize);
5433 else
5434 fieldsize = output_constant (local->val, fieldsize, align2,
5435 local->reverse, false);
5437 /* Count its size. */
5438 local->total_bytes += fieldsize;
5442 /* Helper for output_constructor. From the current LOCAL state, output a
5443 field element that is not true bitfield or part of an outer one. */
5445 static void
5446 output_constructor_regular_field (oc_local_state *local)
5448 /* Field size and position. Since this structure is static, we know the
5449 positions are constant. */
5450 unsigned HOST_WIDE_INT fieldsize;
5451 HOST_WIDE_INT fieldpos;
5453 unsigned int align2;
5455 /* Output any buffered-up bit-fields preceding this element. */
5456 if (local->byte_buffer_in_use)
5458 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5459 local->total_bytes++;
5460 local->byte_buffer_in_use = false;
5463 if (local->index != NULL_TREE)
5465 /* Perform the index calculation in modulo arithmetic but
5466 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5467 but we are using an unsigned sizetype. */
5468 unsigned prec = TYPE_PRECISION (sizetype);
5469 offset_int idx = wi::sext (wi::to_offset (local->index)
5470 - wi::to_offset (local->min_index), prec);
5471 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5472 .to_short_addr ();
5474 else if (local->field != NULL_TREE)
5475 fieldpos = int_byte_position (local->field);
5476 else
5477 fieldpos = 0;
5479 /* Advance to offset of this element.
5480 Note no alignment needed in an array, since that is guaranteed
5481 if each element has the proper size. */
5482 if (local->field != NULL_TREE || local->index != NULL_TREE)
5484 if (fieldpos > local->total_bytes)
5486 assemble_zeros (fieldpos - local->total_bytes);
5487 local->total_bytes = fieldpos;
5489 else
5490 /* Must not go backwards. */
5491 gcc_assert (fieldpos == local->total_bytes);
5494 /* Find the alignment of this element. */
5495 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5497 /* Determine size this element should occupy. */
5498 if (local->field)
5500 fieldsize = 0;
5502 /* If this is an array with an unspecified upper bound,
5503 the initializer determines the size. */
5504 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5505 but we cannot do this until the deprecated support for
5506 initializing zero-length array members is removed. */
5507 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5508 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5509 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5511 fieldsize = array_size_for_constructor (local->val);
5512 /* Given a non-empty initialization, this field had better
5513 be last. Given a flexible array member, the next field
5514 on the chain is a TYPE_DECL of the enclosing struct. */
5515 const_tree next = DECL_CHAIN (local->field);
5516 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5517 tree size = TYPE_SIZE_UNIT (TREE_TYPE (local->val));
5518 gcc_checking_assert (compare_tree_int (size, fieldsize) == 0);
5520 else
5521 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5523 else
5524 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5526 /* Output the element's initial value. */
5527 if (local->val == NULL_TREE)
5528 assemble_zeros (fieldsize);
5529 else
5530 fieldsize = output_constant (local->val, fieldsize, align2,
5531 local->reverse, false);
5533 /* Count its size. */
5534 local->total_bytes += fieldsize;
5537 /* Helper for output_constructor. From the LOCAL state, output an element
5538 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5539 from the start of a possibly ongoing outer byte buffer. */
5541 static void
5542 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5544 /* Bit size of this element. */
5545 HOST_WIDE_INT ebitsize
5546 = (local->field
5547 ? tree_to_uhwi (DECL_SIZE (local->field))
5548 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5550 /* Relative index of this element if this is an array component. */
5551 HOST_WIDE_INT relative_index
5552 = (!local->field
5553 ? (local->index
5554 ? (tree_to_shwi (local->index)
5555 - tree_to_shwi (local->min_index))
5556 : local->last_relative_index + 1)
5557 : 0);
5559 /* Bit position of this element from the start of the containing
5560 constructor. */
5561 HOST_WIDE_INT constructor_relative_ebitpos
5562 = (local->field
5563 ? int_bit_position (local->field)
5564 : ebitsize * relative_index);
5566 /* Bit position of this element from the start of a possibly ongoing
5567 outer byte buffer. */
5568 HOST_WIDE_INT byte_relative_ebitpos
5569 = bit_offset + constructor_relative_ebitpos;
5571 /* From the start of a possibly ongoing outer byte buffer, offsets to
5572 the first bit of this element and to the first bit past the end of
5573 this element. */
5574 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5575 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5577 local->last_relative_index = relative_index;
5579 if (local->val == NULL_TREE)
5580 local->val = integer_zero_node;
5582 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5583 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5584 local->val = TREE_OPERAND (local->val, 0);
5586 if (TREE_CODE (local->val) != INTEGER_CST
5587 && TREE_CODE (local->val) != CONSTRUCTOR)
5589 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5590 return;
5593 /* If this field does not start in this (or next) byte, skip some bytes. */
5594 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5596 /* Output remnant of any bit field in previous bytes. */
5597 if (local->byte_buffer_in_use)
5599 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5600 local->total_bytes++;
5601 local->byte_buffer_in_use = false;
5604 /* If still not at proper byte, advance to there. */
5605 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5607 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5608 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5609 local->total_bytes = next_offset / BITS_PER_UNIT;
5613 /* Set up the buffer if necessary. */
5614 if (!local->byte_buffer_in_use)
5616 local->byte = 0;
5617 if (ebitsize > 0)
5618 local->byte_buffer_in_use = true;
5621 /* If this is nested constructor, recurse passing the bit offset and the
5622 pending data, then retrieve the new pending data afterwards. */
5623 if (TREE_CODE (local->val) == CONSTRUCTOR)
5625 oc_outer_state temp_state;
5626 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5627 temp_state.byte = local->byte;
5628 local->total_bytes
5629 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5630 local->byte = temp_state.byte;
5631 return;
5634 /* Otherwise, we must split the element into pieces that fall within
5635 separate bytes, and combine each byte with previous or following
5636 bit-fields. */
5637 while (next_offset < end_offset)
5639 int this_time;
5640 int shift;
5641 unsigned HOST_WIDE_INT value;
5642 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5643 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5645 /* Advance from byte to byte within this element when necessary. */
5646 while (next_byte != local->total_bytes)
5648 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5649 local->total_bytes++;
5650 local->byte = 0;
5653 /* Number of bits we can process at once (all part of the same byte). */
5654 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5655 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5657 /* For big-endian data, take the most significant bits (of the
5658 bits that are significant) first and put them into bytes from
5659 the most significant end. */
5660 shift = end_offset - next_offset - this_time;
5662 /* Don't try to take a bunch of bits that cross
5663 the word boundary in the INTEGER_CST. We can
5664 only select bits from one element. */
5665 if ((shift / HOST_BITS_PER_WIDE_INT)
5666 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5668 const int end = shift + this_time - 1;
5669 shift = end & -HOST_BITS_PER_WIDE_INT;
5670 this_time = end - shift + 1;
5673 /* Now get the bits we want to insert. */
5674 value = wi::extract_uhwi (wi::to_widest (local->val),
5675 shift, this_time);
5677 /* Get the result. This works only when:
5678 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5679 local->byte |= value << (BITS_PER_UNIT - this_time - next_bit);
5681 else
5683 /* On little-endian machines, take the least significant bits of
5684 the value first and pack them starting at the least significant
5685 bits of the bytes. */
5686 shift = next_offset - byte_relative_ebitpos;
5688 /* Don't try to take a bunch of bits that cross
5689 the word boundary in the INTEGER_CST. We can
5690 only select bits from one element. */
5691 if ((shift / HOST_BITS_PER_WIDE_INT)
5692 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5693 this_time
5694 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5696 /* Now get the bits we want to insert. */
5697 value = wi::extract_uhwi (wi::to_widest (local->val),
5698 shift, this_time);
5700 /* Get the result. This works only when:
5701 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5702 local->byte |= value << next_bit;
5705 next_offset += this_time;
5706 local->byte_buffer_in_use = true;
5710 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5711 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5712 caller output state of relevance in recursive invocations. */
5714 static unsigned HOST_WIDE_INT
5715 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5716 bool reverse, oc_outer_state *outer)
5718 unsigned HOST_WIDE_INT cnt;
5719 constructor_elt *ce;
5720 oc_local_state local;
5722 /* Setup our local state to communicate with helpers. */
5723 local.exp = exp;
5724 local.type = TREE_TYPE (exp);
5725 local.size = size;
5726 local.align = align;
5727 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5728 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5729 else
5730 local.min_index = integer_zero_node;
5732 local.total_bytes = 0;
5733 local.byte_buffer_in_use = outer != NULL;
5734 local.byte = outer ? outer->byte : 0;
5735 local.last_relative_index = -1;
5736 /* The storage order is specified for every aggregate type. */
5737 if (AGGREGATE_TYPE_P (local.type))
5738 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5739 else
5740 local.reverse = reverse;
5742 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5744 /* As CE goes through the elements of the constant, FIELD goes through the
5745 structure fields if the constant is a structure. If the constant is a
5746 union, we override this by getting the field from the TREE_LIST element.
5747 But the constant could also be an array. Then FIELD is zero.
5749 There is always a maximum of one element in the chain LINK for unions
5750 (even if the initializer in a source program incorrectly contains
5751 more one). */
5753 if (TREE_CODE (local.type) == RECORD_TYPE)
5754 local.field = TYPE_FIELDS (local.type);
5755 else
5756 local.field = NULL_TREE;
5758 for (cnt = 0;
5759 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5760 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5762 local.val = ce->value;
5763 local.index = NULL_TREE;
5765 /* The element in a union constructor specifies the proper field
5766 or index. */
5767 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5768 local.field = ce->index;
5770 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5771 local.index = ce->index;
5773 if (local.field && flag_verbose_asm)
5774 fprintf (asm_out_file, "%s %s:\n",
5775 ASM_COMMENT_START,
5776 DECL_NAME (local.field)
5777 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5778 : "<anonymous>");
5780 /* Eliminate the marker that makes a cast not be an lvalue. */
5781 if (local.val != NULL_TREE)
5782 STRIP_NOPS (local.val);
5784 /* Output the current element, using the appropriate helper ... */
5786 /* For an array slice not part of an outer bitfield. */
5787 if (!outer
5788 && local.index != NULL_TREE
5789 && TREE_CODE (local.index) == RANGE_EXPR)
5790 output_constructor_array_range (&local);
5792 /* For a field that is neither a true bitfield nor part of an outer one,
5793 known to be at least byte aligned and multiple-of-bytes long. */
5794 else if (!outer
5795 && (local.field == NULL_TREE
5796 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5797 output_constructor_regular_field (&local);
5799 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5800 supported for scalar fields, so we may need to convert first. */
5801 else
5803 if (TREE_CODE (local.val) == REAL_CST)
5804 local.val
5805 = fold_unary (VIEW_CONVERT_EXPR,
5806 build_nonstandard_integer_type
5807 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5808 local.val);
5809 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5813 /* If we are not at toplevel, save the pending data for our caller.
5814 Otherwise output the pending data and padding zeros as needed. */
5815 if (outer)
5816 outer->byte = local.byte;
5817 else
5819 if (local.byte_buffer_in_use)
5821 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5822 local.total_bytes++;
5825 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5827 assemble_zeros (local.size - local.total_bytes);
5828 local.total_bytes = local.size;
5832 return local.total_bytes;
5835 /* Mark DECL as weak. */
5837 static void
5838 mark_weak (tree decl)
5840 if (DECL_WEAK (decl))
5841 return;
5843 struct symtab_node *n = symtab_node::get (decl);
5844 if (n && n->refuse_visibility_changes)
5845 error ("%+qD declared weak after being used", decl);
5846 DECL_WEAK (decl) = 1;
5848 if (DECL_RTL_SET_P (decl)
5849 && MEM_P (DECL_RTL (decl))
5850 && XEXP (DECL_RTL (decl), 0)
5851 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5852 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5855 /* Merge weak status between NEWDECL and OLDDECL. */
5857 void
5858 merge_weak (tree newdecl, tree olddecl)
5860 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5862 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5864 tree *pwd;
5865 /* We put the NEWDECL on the weak_decls list at some point
5866 and OLDDECL as well. Keep just OLDDECL on the list. */
5867 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5868 if (TREE_VALUE (*pwd) == newdecl)
5870 *pwd = TREE_CHAIN (*pwd);
5871 break;
5874 return;
5877 if (DECL_WEAK (newdecl))
5879 tree wd;
5881 /* NEWDECL is weak, but OLDDECL is not. */
5883 /* If we already output the OLDDECL, we're in trouble; we can't
5884 go back and make it weak. This should never happen in
5885 unit-at-a-time compilation. */
5886 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5888 /* If we've already generated rtl referencing OLDDECL, we may
5889 have done so in a way that will not function properly with
5890 a weak symbol. Again in unit-at-a-time this should be
5891 impossible. */
5892 gcc_assert (!TREE_USED (olddecl)
5893 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5895 /* PR 49899: You cannot convert a static function into a weak, public function. */
5896 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
5897 error ("weak declaration of %q+D being applied to a already "
5898 "existing, static definition", newdecl);
5900 if (TARGET_SUPPORTS_WEAK)
5902 /* We put the NEWDECL on the weak_decls list at some point.
5903 Replace it with the OLDDECL. */
5904 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5905 if (TREE_VALUE (wd) == newdecl)
5907 TREE_VALUE (wd) = olddecl;
5908 break;
5910 /* We may not find the entry on the list. If NEWDECL is a
5911 weak alias, then we will have already called
5912 globalize_decl to remove the entry; in that case, we do
5913 not need to do anything. */
5916 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5917 mark_weak (olddecl);
5919 else
5920 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5921 weak. Just update NEWDECL to indicate that it's weak too. */
5922 mark_weak (newdecl);
5925 /* Declare DECL to be a weak symbol. */
5927 void
5928 declare_weak (tree decl)
5930 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5931 if (! TREE_PUBLIC (decl))
5933 error ("weak declaration of %q+D must be public", decl);
5934 return;
5936 else if (!TARGET_SUPPORTS_WEAK)
5937 warning (0, "weak declaration of %q+D not supported", decl);
5939 mark_weak (decl);
5940 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5941 DECL_ATTRIBUTES (decl)
5942 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5945 static void
5946 weak_finish_1 (tree decl)
5948 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5949 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5950 #endif
5952 if (! TREE_USED (decl))
5953 return;
5955 #ifdef ASM_WEAKEN_DECL
5956 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5957 #else
5958 #ifdef ASM_WEAKEN_LABEL
5959 ASM_WEAKEN_LABEL (asm_out_file, name);
5960 #else
5961 #ifdef ASM_OUTPUT_WEAK_ALIAS
5963 static bool warn_once = 0;
5964 if (! warn_once)
5966 warning (0, "only weak aliases are supported in this configuration");
5967 warn_once = 1;
5969 return;
5971 #endif
5972 #endif
5973 #endif
5976 /* Fiven an assembly name, find the decl it is associated with. */
5977 static tree
5978 find_decl (tree target)
5980 symtab_node *node = symtab_node::get_for_asmname (target);
5981 if (node)
5982 return node->decl;
5983 return NULL_TREE;
5986 /* This TREE_LIST contains weakref targets. */
5988 static GTY(()) tree weakref_targets;
5990 /* Emit any pending weak declarations. */
5992 void
5993 weak_finish (void)
5995 tree t;
5997 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5999 tree alias_decl = TREE_PURPOSE (t);
6000 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
6002 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
6003 || TREE_SYMBOL_REFERENCED (target))
6004 /* Remove alias_decl from the weak list, but leave entries for
6005 the target alone. */
6006 target = NULL_TREE;
6007 #ifndef ASM_OUTPUT_WEAKREF
6008 else if (! TREE_SYMBOL_REFERENCED (target))
6010 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
6011 defined, otherwise we and weak_finish_1 would use
6012 different macros. */
6013 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
6014 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
6015 # else
6016 tree decl = find_decl (target);
6018 if (! decl)
6020 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
6021 TREE_CODE (alias_decl), target,
6022 TREE_TYPE (alias_decl));
6024 DECL_EXTERNAL (decl) = 1;
6025 TREE_PUBLIC (decl) = 1;
6026 DECL_ARTIFICIAL (decl) = 1;
6027 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
6028 TREE_USED (decl) = 1;
6031 weak_finish_1 (decl);
6032 # endif
6034 #endif
6037 tree *p;
6038 tree t2;
6040 /* Remove the alias and the target from the pending weak list
6041 so that we do not emit any .weak directives for the former,
6042 nor multiple .weak directives for the latter. */
6043 for (p = &weak_decls; (t2 = *p) ; )
6045 if (TREE_VALUE (t2) == alias_decl
6046 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
6047 *p = TREE_CHAIN (t2);
6048 else
6049 p = &TREE_CHAIN (t2);
6052 /* Remove other weakrefs to the same target, to speed things up. */
6053 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
6055 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
6056 *p = TREE_CHAIN (t2);
6057 else
6058 p = &TREE_CHAIN (t2);
6063 for (t = weak_decls; t; t = TREE_CHAIN (t))
6065 tree decl = TREE_VALUE (t);
6067 weak_finish_1 (decl);
6071 /* Emit the assembly bits to indicate that DECL is globally visible. */
6073 static void
6074 globalize_decl (tree decl)
6077 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
6078 if (DECL_WEAK (decl))
6080 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6081 tree *p, t;
6083 #ifdef ASM_WEAKEN_DECL
6084 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
6085 #else
6086 ASM_WEAKEN_LABEL (asm_out_file, name);
6087 #endif
6089 /* Remove this function from the pending weak list so that
6090 we do not emit multiple .weak directives for it. */
6091 for (p = &weak_decls; (t = *p) ; )
6093 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6094 *p = TREE_CHAIN (t);
6095 else
6096 p = &TREE_CHAIN (t);
6099 /* Remove weakrefs to the same target from the pending weakref
6100 list, for the same reason. */
6101 for (p = &weakref_targets; (t = *p) ; )
6103 if (DECL_ASSEMBLER_NAME (decl)
6104 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6105 *p = TREE_CHAIN (t);
6106 else
6107 p = &TREE_CHAIN (t);
6110 return;
6112 #endif
6114 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
6117 vec<alias_pair, va_gc> *alias_pairs;
6119 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
6120 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
6121 tree node is DECL to have the value of the tree node TARGET. */
6123 void
6124 do_assemble_alias (tree decl, tree target)
6126 tree id;
6128 /* Emulated TLS had better not get this var. */
6129 gcc_assert (!(!targetm.have_tls
6130 && VAR_P (decl)
6131 && DECL_THREAD_LOCAL_P (decl)));
6133 if (TREE_ASM_WRITTEN (decl))
6134 return;
6136 id = DECL_ASSEMBLER_NAME (decl);
6137 ultimate_transparent_alias_target (&id);
6138 ultimate_transparent_alias_target (&target);
6140 /* We must force creation of DECL_RTL for debug info generation, even though
6141 we don't use it here. */
6142 make_decl_rtl (decl);
6144 TREE_ASM_WRITTEN (decl) = 1;
6145 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
6146 TREE_ASM_WRITTEN (id) = 1;
6148 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6150 if (!TREE_SYMBOL_REFERENCED (target))
6151 weakref_targets = tree_cons (decl, target, weakref_targets);
6153 #ifdef ASM_OUTPUT_WEAKREF
6154 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
6155 IDENTIFIER_POINTER (id),
6156 IDENTIFIER_POINTER (target));
6157 #else
6158 if (!TARGET_SUPPORTS_WEAK)
6160 error_at (DECL_SOURCE_LOCATION (decl),
6161 "weakref is not supported in this configuration");
6162 return;
6164 #endif
6165 return;
6168 #ifdef ASM_OUTPUT_DEF
6169 tree orig_decl = decl;
6171 /* Make name accessible from other files, if appropriate. */
6173 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
6175 globalize_decl (decl);
6176 maybe_assemble_visibility (decl);
6178 if (TREE_CODE (decl) == FUNCTION_DECL
6179 && cgraph_node::get (decl)->ifunc_resolver)
6181 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
6182 if (targetm.has_ifunc_p ())
6183 ASM_OUTPUT_TYPE_DIRECTIVE
6184 (asm_out_file, IDENTIFIER_POINTER (id),
6185 IFUNC_ASM_TYPE);
6186 else
6187 #endif
6188 error_at (DECL_SOURCE_LOCATION (decl),
6189 "%qs is not supported on this target", "ifunc");
6192 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
6193 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
6194 # else
6195 ASM_OUTPUT_DEF (asm_out_file,
6196 IDENTIFIER_POINTER (id),
6197 IDENTIFIER_POINTER (target));
6198 # endif
6199 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
6201 const char *name;
6202 tree *p, t;
6204 name = IDENTIFIER_POINTER (id);
6205 # ifdef ASM_WEAKEN_DECL
6206 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
6207 # else
6208 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
6209 # endif
6210 /* Remove this function from the pending weak list so that
6211 we do not emit multiple .weak directives for it. */
6212 for (p = &weak_decls; (t = *p) ; )
6213 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
6214 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6215 *p = TREE_CHAIN (t);
6216 else
6217 p = &TREE_CHAIN (t);
6219 /* Remove weakrefs to the same target from the pending weakref
6220 list, for the same reason. */
6221 for (p = &weakref_targets; (t = *p) ; )
6223 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6224 *p = TREE_CHAIN (t);
6225 else
6226 p = &TREE_CHAIN (t);
6229 #endif
6232 /* Output .symver directive. */
6234 void
6235 do_assemble_symver (tree decl, tree target)
6237 tree id = DECL_ASSEMBLER_NAME (decl);
6238 ultimate_transparent_alias_target (&id);
6239 ultimate_transparent_alias_target (&target);
6240 #ifdef ASM_OUTPUT_SYMVER_DIRECTIVE
6241 ASM_OUTPUT_SYMVER_DIRECTIVE (asm_out_file,
6242 IDENTIFIER_POINTER (target),
6243 IDENTIFIER_POINTER (id));
6244 #else
6245 error ("symver is only supported on ELF platforms");
6246 #endif
6249 /* Emit an assembler directive to make the symbol for DECL an alias to
6250 the symbol for TARGET. */
6252 void
6253 assemble_alias (tree decl, tree target)
6255 tree target_decl;
6257 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6259 tree alias = DECL_ASSEMBLER_NAME (decl);
6261 ultimate_transparent_alias_target (&target);
6263 if (alias == target)
6264 error ("%qs symbol %q+D ultimately targets itself", "weakref", decl);
6265 if (TREE_PUBLIC (decl))
6266 error ("%qs symbol %q+D must have static linkage", "weakref", decl);
6268 else
6270 #if !defined (ASM_OUTPUT_DEF)
6271 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
6272 error_at (DECL_SOURCE_LOCATION (decl),
6273 "alias definitions not supported in this configuration");
6274 TREE_ASM_WRITTEN (decl) = 1;
6275 return;
6276 # else
6277 if (!DECL_WEAK (decl))
6279 /* NB: ifunc_resolver isn't set when an error is detected. */
6280 if (TREE_CODE (decl) == FUNCTION_DECL
6281 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
6282 error_at (DECL_SOURCE_LOCATION (decl),
6283 "%qs is not supported in this configuration", "ifunc");
6284 else
6285 error_at (DECL_SOURCE_LOCATION (decl),
6286 "only weak aliases are supported in this configuration");
6287 TREE_ASM_WRITTEN (decl) = 1;
6288 return;
6290 # endif
6291 #endif
6293 TREE_USED (decl) = 1;
6295 /* Allow aliases to aliases. */
6296 if (TREE_CODE (decl) == FUNCTION_DECL)
6297 cgraph_node::get_create (decl)->alias = true;
6298 else
6299 varpool_node::get_create (decl)->alias = true;
6301 /* If the target has already been emitted, we don't have to queue the
6302 alias. This saves a tad of memory. */
6303 if (symtab->global_info_ready)
6304 target_decl = find_decl (target);
6305 else
6306 target_decl= NULL;
6307 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6308 || symtab->state >= EXPANSION)
6309 do_assemble_alias (decl, target);
6310 else
6312 alias_pair p = {decl, target};
6313 vec_safe_push (alias_pairs, p);
6317 /* Record and output a table of translations from original function
6318 to its transaction aware clone. Note that tm_pure functions are
6319 considered to be their own clone. */
6321 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6323 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6324 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6326 static int
6327 keep_cache_entry (tree_map *&e)
6329 return ggc_marked_p (e->base.from);
6333 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6335 void
6336 record_tm_clone_pair (tree o, tree n)
6338 struct tree_map **slot, *h;
6340 if (tm_clone_hash == NULL)
6341 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6343 h = ggc_alloc<tree_map> ();
6344 h->hash = htab_hash_pointer (o);
6345 h->base.from = o;
6346 h->to = n;
6348 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6349 *slot = h;
6352 tree
6353 get_tm_clone_pair (tree o)
6355 if (tm_clone_hash)
6357 struct tree_map *h, in;
6359 in.base.from = o;
6360 in.hash = htab_hash_pointer (o);
6361 h = tm_clone_hash->find_with_hash (&in, in.hash);
6362 if (h)
6363 return h->to;
6365 return NULL_TREE;
6368 struct tm_alias_pair
6370 unsigned int uid;
6371 tree from;
6372 tree to;
6376 /* Dump the actual pairs to the .tm_clone_table section. */
6378 static void
6379 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6381 unsigned i;
6382 tm_alias_pair *p;
6383 bool switched = false;
6385 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6387 tree src = p->from;
6388 tree dst = p->to;
6389 struct cgraph_node *src_n = cgraph_node::get (src);
6390 struct cgraph_node *dst_n = cgraph_node::get (dst);
6392 /* The function ipa_tm_create_version() marks the clone as needed if
6393 the original function was needed. But we also mark the clone as
6394 needed if we ever called the clone indirectly through
6395 TM_GETTMCLONE. If neither of these are true, we didn't generate
6396 a clone, and we didn't call it indirectly... no sense keeping it
6397 in the clone table. */
6398 if (!dst_n || !dst_n->definition)
6399 continue;
6401 /* This covers the case where we have optimized the original
6402 function away, and only access the transactional clone. */
6403 if (!src_n || !src_n->definition)
6404 continue;
6406 if (!switched)
6408 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6409 assemble_align (POINTER_SIZE);
6410 switched = true;
6413 assemble_integer (XEXP (DECL_RTL (src), 0),
6414 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6415 assemble_integer (XEXP (DECL_RTL (dst), 0),
6416 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6420 /* Provide a default for the tm_clone_table section. */
6422 section *
6423 default_clone_table_section (void)
6425 return get_named_section (NULL, ".tm_clone_table", 3);
6428 /* Helper comparison function for qsorting by the DECL_UID stored in
6429 alias_pair->emitted_diags. */
6431 static int
6432 tm_alias_pair_cmp (const void *x, const void *y)
6434 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6435 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6436 if (p1->uid < p2->uid)
6437 return -1;
6438 if (p1->uid > p2->uid)
6439 return 1;
6440 return 0;
6443 void
6444 finish_tm_clone_pairs (void)
6446 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6448 if (tm_clone_hash == NULL)
6449 return;
6451 /* We need a determenistic order for the .tm_clone_table, otherwise
6452 we will get bootstrap comparison failures, so dump the hash table
6453 to a vector, sort it, and dump the vector. */
6455 /* Dump the hashtable to a vector. */
6456 tree_map *map;
6457 hash_table<tm_clone_hasher>::iterator iter;
6458 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6460 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6461 tm_alias_pairs.safe_push (p);
6463 /* Sort it. */
6464 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6466 /* Dump it. */
6467 dump_tm_clone_pairs (tm_alias_pairs);
6469 tm_clone_hash->empty ();
6470 tm_clone_hash = NULL;
6471 tm_alias_pairs.release ();
6475 /* Emit an assembler directive to set symbol for DECL visibility to
6476 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6478 void
6479 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6480 int vis ATTRIBUTE_UNUSED)
6482 #ifdef HAVE_GAS_HIDDEN
6483 static const char * const visibility_types[] = {
6484 NULL, "protected", "hidden", "internal"
6487 const char *name, *type;
6488 tree id;
6490 id = DECL_ASSEMBLER_NAME (decl);
6491 ultimate_transparent_alias_target (&id);
6492 name = IDENTIFIER_POINTER (id);
6494 type = visibility_types[vis];
6496 fprintf (asm_out_file, "\t.%s\t", type);
6497 assemble_name (asm_out_file, name);
6498 fprintf (asm_out_file, "\n");
6499 #else
6500 if (!DECL_ARTIFICIAL (decl))
6501 warning (OPT_Wattributes, "visibility attribute not supported "
6502 "in this configuration; ignored");
6503 #endif
6506 /* A helper function to call assemble_visibility when needed for a decl. */
6509 maybe_assemble_visibility (tree decl)
6511 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6512 if (vis != VISIBILITY_DEFAULT)
6514 targetm.asm_out.assemble_visibility (decl, vis);
6515 return 1;
6517 else
6518 return 0;
6521 /* Returns 1 if the target configuration supports defining public symbols
6522 so that one of them will be chosen at link time instead of generating a
6523 multiply-defined symbol error, whether through the use of weak symbols or
6524 a target-specific mechanism for having duplicates discarded. */
6527 supports_one_only (void)
6529 if (SUPPORTS_ONE_ONLY)
6530 return 1;
6531 return TARGET_SUPPORTS_WEAK;
6534 /* Set up DECL as a public symbol that can be defined in multiple
6535 translation units without generating a linker error. */
6537 void
6538 make_decl_one_only (tree decl, tree comdat_group)
6540 struct symtab_node *symbol;
6541 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6543 TREE_PUBLIC (decl) = 1;
6545 if (VAR_P (decl))
6546 symbol = varpool_node::get_create (decl);
6547 else
6548 symbol = cgraph_node::get_create (decl);
6550 if (SUPPORTS_ONE_ONLY)
6552 #ifdef MAKE_DECL_ONE_ONLY
6553 MAKE_DECL_ONE_ONLY (decl);
6554 #endif
6555 symbol->set_comdat_group (comdat_group);
6557 else if (VAR_P (decl)
6558 && (DECL_INITIAL (decl) == 0
6559 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6560 DECL_COMMON (decl) = 1;
6561 else
6563 gcc_assert (TARGET_SUPPORTS_WEAK);
6564 DECL_WEAK (decl) = 1;
6568 void
6569 init_varasm_once (void)
6571 section_htab = hash_table<section_hasher>::create_ggc (31);
6572 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6573 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6575 shared_constant_pool = create_constant_pool ();
6577 #ifdef TEXT_SECTION_ASM_OP
6578 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6579 TEXT_SECTION_ASM_OP);
6580 #endif
6582 #ifdef DATA_SECTION_ASM_OP
6583 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6584 DATA_SECTION_ASM_OP);
6585 #endif
6587 #ifdef SDATA_SECTION_ASM_OP
6588 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6589 SDATA_SECTION_ASM_OP);
6590 #endif
6592 #ifdef READONLY_DATA_SECTION_ASM_OP
6593 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6594 READONLY_DATA_SECTION_ASM_OP);
6595 #endif
6597 #ifdef CTORS_SECTION_ASM_OP
6598 ctors_section = get_unnamed_section (0, output_section_asm_op,
6599 CTORS_SECTION_ASM_OP);
6600 #endif
6602 #ifdef DTORS_SECTION_ASM_OP
6603 dtors_section = get_unnamed_section (0, output_section_asm_op,
6604 DTORS_SECTION_ASM_OP);
6605 #endif
6607 #ifdef BSS_SECTION_ASM_OP
6608 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6609 output_section_asm_op,
6610 BSS_SECTION_ASM_OP);
6611 #endif
6613 #ifdef SBSS_SECTION_ASM_OP
6614 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6615 output_section_asm_op,
6616 SBSS_SECTION_ASM_OP);
6617 #endif
6619 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6620 | SECTION_COMMON, emit_tls_common);
6621 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6622 | SECTION_COMMON, emit_local);
6623 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6624 | SECTION_COMMON, emit_common);
6626 #if defined ASM_OUTPUT_ALIGNED_BSS
6627 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6628 emit_bss);
6629 #endif
6631 targetm.asm_out.init_sections ();
6633 if (readonly_data_section == NULL)
6634 readonly_data_section = text_section;
6636 #ifdef ASM_OUTPUT_EXTERNAL
6637 pending_assemble_externals_set = new hash_set<tree>;
6638 #endif
6641 enum tls_model
6642 decl_default_tls_model (const_tree decl)
6644 enum tls_model kind;
6645 bool is_local;
6647 is_local = targetm.binds_local_p (decl);
6648 if (!flag_shlib)
6650 if (is_local)
6651 kind = TLS_MODEL_LOCAL_EXEC;
6652 else
6653 kind = TLS_MODEL_INITIAL_EXEC;
6656 /* Local dynamic is inefficient when we're not combining the
6657 parts of the address. */
6658 else if (optimize && is_local)
6659 kind = TLS_MODEL_LOCAL_DYNAMIC;
6660 else
6661 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6662 if (kind < flag_tls_default)
6663 kind = flag_tls_default;
6665 return kind;
6668 /* Select a set of attributes for section NAME based on the properties
6669 of DECL and whether or not RELOC indicates that DECL's initializer
6670 might contain runtime relocations.
6672 We make the section read-only and executable for a function decl,
6673 read-only for a const data decl, and writable for a non-const data decl. */
6675 unsigned int
6676 default_section_type_flags (tree decl, const char *name, int reloc)
6678 unsigned int flags;
6680 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6681 flags = SECTION_CODE;
6682 else if (decl)
6684 enum section_category category
6685 = categorize_decl_for_section (decl, reloc);
6686 if (decl_readonly_section_1 (category))
6687 flags = 0;
6688 else if (category == SECCAT_DATA_REL_RO
6689 || category == SECCAT_DATA_REL_RO_LOCAL)
6690 flags = SECTION_WRITE | SECTION_RELRO;
6691 else
6692 flags = SECTION_WRITE;
6694 else
6696 flags = SECTION_WRITE;
6697 if (strcmp (name, ".data.rel.ro") == 0
6698 || strcmp (name, ".data.rel.ro.local") == 0)
6699 flags |= SECTION_RELRO;
6702 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6703 flags |= SECTION_LINKONCE;
6705 if (strcmp (name, ".vtable_map_vars") == 0)
6706 flags |= SECTION_LINKONCE;
6708 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6709 flags |= SECTION_TLS | SECTION_WRITE;
6711 if (strcmp (name, ".bss") == 0
6712 || strncmp (name, ".bss.", 5) == 0
6713 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6714 || strcmp (name, ".persistent.bss") == 0
6715 || strcmp (name, ".sbss") == 0
6716 || strncmp (name, ".sbss.", 6) == 0
6717 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6718 flags |= SECTION_BSS;
6720 if (strcmp (name, ".tdata") == 0
6721 || strncmp (name, ".tdata.", 7) == 0
6722 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6723 flags |= SECTION_TLS;
6725 if (strcmp (name, ".tbss") == 0
6726 || strncmp (name, ".tbss.", 6) == 0
6727 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6728 flags |= SECTION_TLS | SECTION_BSS;
6730 if (strcmp (name, ".noinit") == 0)
6731 flags |= SECTION_WRITE | SECTION_BSS | SECTION_NOTYPE;
6733 if (strcmp (name, ".persistent") == 0)
6734 flags |= SECTION_WRITE | SECTION_NOTYPE;
6736 /* Various sections have special ELF types that the assembler will
6737 assign by default based on the name. They are neither SHT_PROGBITS
6738 nor SHT_NOBITS, so when changing sections we don't want to print a
6739 section type (@progbits or @nobits). Rather than duplicating the
6740 assembler's knowledge of what those special name patterns are, just
6741 let the assembler choose the type if we don't know a specific
6742 reason to set it to something other than the default. SHT_PROGBITS
6743 is the default for sections whose name is not specially known to
6744 the assembler, so it does no harm to leave the choice to the
6745 assembler when @progbits is the best thing we know to use. If
6746 someone is silly enough to emit code or TLS variables to one of
6747 these sections, then don't handle them specially.
6749 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6750 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
6751 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6752 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6753 flags |= SECTION_NOTYPE;
6755 return flags;
6758 /* Return true if the target supports some form of global BSS,
6759 either through bss_noswitch_section, or by selecting a BSS
6760 section in TARGET_ASM_SELECT_SECTION. */
6762 bool
6763 have_global_bss_p (void)
6765 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6768 /* Output assembly to switch to section NAME with attribute FLAGS.
6769 Four variants for common object file formats. */
6771 void
6772 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6773 unsigned int flags ATTRIBUTE_UNUSED,
6774 tree decl ATTRIBUTE_UNUSED)
6776 /* Some object formats don't support named sections at all. The
6777 front-end should already have flagged this as an error. */
6778 gcc_unreachable ();
6781 #ifndef TLS_SECTION_ASM_FLAG
6782 #define TLS_SECTION_ASM_FLAG 'T'
6783 #endif
6785 void
6786 default_elf_asm_named_section (const char *name, unsigned int flags,
6787 tree decl)
6789 char flagchars[11], *f = flagchars;
6790 unsigned int numeric_value = 0;
6792 /* If we have already declared this section, we can use an
6793 abbreviated form to switch back to it -- unless this section is
6794 part of a COMDAT groups or with SHF_GNU_RETAIN or with SHF_LINK_ORDER,
6795 in which case GAS requires the full declaration every time. */
6796 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6797 && !(flags & (SECTION_RETAIN | SECTION_LINK_ORDER))
6798 && (flags & SECTION_DECLARED))
6800 fprintf (asm_out_file, "\t.section\t%s\n", name);
6801 return;
6804 /* If we have a machine specific flag, then use the numeric value to pass
6805 this on to GAS. */
6806 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6807 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6808 else
6810 if (!(flags & SECTION_DEBUG))
6811 *f++ = 'a';
6812 #if HAVE_GAS_SECTION_EXCLUDE
6813 if (flags & SECTION_EXCLUDE)
6814 *f++ = 'e';
6815 #endif
6816 if (flags & SECTION_WRITE)
6817 *f++ = 'w';
6818 if (flags & SECTION_CODE)
6819 *f++ = 'x';
6820 if (flags & SECTION_SMALL)
6821 *f++ = 's';
6822 if (flags & SECTION_MERGE)
6823 *f++ = 'M';
6824 if (flags & SECTION_STRINGS)
6825 *f++ = 'S';
6826 if (flags & SECTION_TLS)
6827 *f++ = TLS_SECTION_ASM_FLAG;
6828 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6829 *f++ = 'G';
6830 if (flags & SECTION_RETAIN)
6831 *f++ = 'R';
6832 if (flags & SECTION_LINK_ORDER)
6833 *f++ = 'o';
6834 #ifdef MACH_DEP_SECTION_ASM_FLAG
6835 if (flags & SECTION_MACH_DEP)
6836 *f++ = MACH_DEP_SECTION_ASM_FLAG;
6837 #endif
6838 *f = '\0';
6841 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6843 /* default_section_type_flags (above) knows which flags need special
6844 handling here, and sets NOTYPE when none of these apply so that the
6845 assembler's logic for default types can apply to user-chosen
6846 section names. */
6847 if (!(flags & SECTION_NOTYPE))
6849 const char *type;
6850 const char *format;
6852 if (flags & SECTION_BSS)
6853 type = "nobits";
6854 else
6855 type = "progbits";
6857 format = ",@%s";
6858 /* On platforms that use "@" as the assembly comment character,
6859 use "%" instead. */
6860 if (strcmp (ASM_COMMENT_START, "@") == 0)
6861 format = ",%%%s";
6862 fprintf (asm_out_file, format, type);
6864 if (flags & SECTION_ENTSIZE)
6865 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6866 if (flags & SECTION_LINK_ORDER)
6868 tree id = DECL_ASSEMBLER_NAME (decl);
6869 ultimate_transparent_alias_target (&id);
6870 const char *name = IDENTIFIER_POINTER (id);
6871 name = targetm.strip_name_encoding (name);
6872 fprintf (asm_out_file, ",%s", name);
6874 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6876 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6877 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6878 else
6879 fprintf (asm_out_file, ",%s,comdat",
6880 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6884 putc ('\n', asm_out_file);
6887 void
6888 default_coff_asm_named_section (const char *name, unsigned int flags,
6889 tree decl ATTRIBUTE_UNUSED)
6891 char flagchars[8], *f = flagchars;
6893 if (flags & SECTION_WRITE)
6894 *f++ = 'w';
6895 if (flags & SECTION_CODE)
6896 *f++ = 'x';
6897 *f = '\0';
6899 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6902 void
6903 default_pe_asm_named_section (const char *name, unsigned int flags,
6904 tree decl)
6906 default_coff_asm_named_section (name, flags, decl);
6908 if (flags & SECTION_LINKONCE)
6910 /* Functions may have been compiled at various levels of
6911 optimization so we can't use `same_size' here.
6912 Instead, have the linker pick one. */
6913 fprintf (asm_out_file, "\t.linkonce %s\n",
6914 (flags & SECTION_CODE ? "discard" : "same_size"));
6918 /* The lame default section selector. */
6920 section *
6921 default_select_section (tree decl, int reloc,
6922 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6924 if (DECL_P (decl))
6926 if (decl_readonly_section (decl, reloc))
6927 return readonly_data_section;
6929 else if (TREE_CODE (decl) == CONSTRUCTOR)
6931 if (! ((flag_pic && reloc)
6932 || !TREE_READONLY (decl)
6933 || TREE_SIDE_EFFECTS (decl)
6934 || !TREE_CONSTANT (decl)))
6935 return readonly_data_section;
6937 else if (TREE_CODE (decl) == STRING_CST)
6938 return readonly_data_section;
6939 else if (! (flag_pic && reloc))
6940 return readonly_data_section;
6942 return data_section;
6945 enum section_category
6946 categorize_decl_for_section (const_tree decl, int reloc)
6948 enum section_category ret;
6950 if (TREE_CODE (decl) == FUNCTION_DECL)
6951 return SECCAT_TEXT;
6952 else if (TREE_CODE (decl) == STRING_CST)
6954 if ((flag_sanitize & SANITIZE_ADDRESS)
6955 && asan_protect_global (CONST_CAST_TREE (decl)))
6956 /* or !flag_merge_constants */
6957 return SECCAT_RODATA;
6958 else
6959 return SECCAT_RODATA_MERGE_STR;
6961 else if (VAR_P (decl))
6963 tree d = CONST_CAST_TREE (decl);
6964 if (bss_initializer_p (decl))
6965 ret = SECCAT_BSS;
6966 else if (! TREE_READONLY (decl)
6967 || TREE_SIDE_EFFECTS (decl)
6968 || (DECL_INITIAL (decl)
6969 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
6971 /* Here the reloc_rw_mask is not testing whether the section should
6972 be read-only or not, but whether the dynamic link will have to
6973 do something. If so, we wish to segregate the data in order to
6974 minimize cache misses inside the dynamic linker. */
6975 if (reloc & targetm.asm_out.reloc_rw_mask ())
6976 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6977 else
6978 ret = SECCAT_DATA;
6980 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6981 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6982 else if (reloc || flag_merge_constants < 2
6983 || ((flag_sanitize & SANITIZE_ADDRESS)
6984 /* PR 81697: for architectures that use section anchors we
6985 need to ignore DECL_RTL_SET_P (decl) for string constants
6986 inside this asan_protect_global call because otherwise
6987 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
6988 section, set DECL_RTL (decl) later on and add DECL to
6989 protected globals via successive asan_protect_global
6990 calls. In this scenario we'll end up with wrong
6991 alignment of these strings at runtime and possible ASan
6992 false positives. */
6993 && asan_protect_global (d, use_object_blocks_p ()
6994 && use_blocks_for_decl_p (d))))
6995 /* C and C++ don't allow different variables to share the same
6996 location. -fmerge-all-constants allows even that (at the
6997 expense of not conforming). */
6998 ret = SECCAT_RODATA;
6999 else if (DECL_INITIAL (decl)
7000 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
7001 ret = SECCAT_RODATA_MERGE_STR_INIT;
7002 else
7003 ret = SECCAT_RODATA_MERGE_CONST;
7005 else if (TREE_CODE (decl) == CONSTRUCTOR)
7007 if ((reloc & targetm.asm_out.reloc_rw_mask ())
7008 || TREE_SIDE_EFFECTS (decl)
7009 || ! TREE_CONSTANT (decl))
7010 ret = SECCAT_DATA;
7011 else
7012 ret = SECCAT_RODATA;
7014 else
7015 ret = SECCAT_RODATA;
7017 /* There are no read-only thread-local sections. */
7018 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7020 /* Note that this would be *just* SECCAT_BSS, except that there's
7021 no concept of a read-only thread-local-data section. */
7022 if (ret == SECCAT_BSS
7023 || DECL_INITIAL (decl) == NULL
7024 || (flag_zero_initialized_in_bss
7025 && initializer_zerop (DECL_INITIAL (decl))))
7026 ret = SECCAT_TBSS;
7027 else
7028 ret = SECCAT_TDATA;
7031 /* If the target uses small data sections, select it. */
7032 else if (targetm.in_small_data_p (decl))
7034 if (ret == SECCAT_BSS)
7035 ret = SECCAT_SBSS;
7036 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
7037 ret = SECCAT_SRODATA;
7038 else
7039 ret = SECCAT_SDATA;
7042 return ret;
7045 static bool
7046 decl_readonly_section_1 (enum section_category category)
7048 switch (category)
7050 case SECCAT_RODATA:
7051 case SECCAT_RODATA_MERGE_STR:
7052 case SECCAT_RODATA_MERGE_STR_INIT:
7053 case SECCAT_RODATA_MERGE_CONST:
7054 case SECCAT_SRODATA:
7055 return true;
7056 default:
7057 return false;
7061 bool
7062 decl_readonly_section (const_tree decl, int reloc)
7064 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
7067 /* Select a section based on the above categorization. */
7069 section *
7070 default_elf_select_section (tree decl, int reloc,
7071 unsigned HOST_WIDE_INT align)
7073 const char *sname;
7075 switch (categorize_decl_for_section (decl, reloc))
7077 case SECCAT_TEXT:
7078 /* We're not supposed to be called on FUNCTION_DECLs. */
7079 gcc_unreachable ();
7080 case SECCAT_RODATA:
7081 return readonly_data_section;
7082 case SECCAT_RODATA_MERGE_STR:
7083 return mergeable_string_section (decl, align, 0);
7084 case SECCAT_RODATA_MERGE_STR_INIT:
7085 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
7086 case SECCAT_RODATA_MERGE_CONST:
7087 return mergeable_constant_section (DECL_MODE (decl), align, 0);
7088 case SECCAT_SRODATA:
7089 sname = ".sdata2";
7090 break;
7091 case SECCAT_DATA:
7092 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7094 sname = ".persistent";
7095 break;
7097 return data_section;
7098 case SECCAT_DATA_REL:
7099 sname = ".data.rel";
7100 break;
7101 case SECCAT_DATA_REL_LOCAL:
7102 sname = ".data.rel.local";
7103 break;
7104 case SECCAT_DATA_REL_RO:
7105 sname = ".data.rel.ro";
7106 break;
7107 case SECCAT_DATA_REL_RO_LOCAL:
7108 sname = ".data.rel.ro.local";
7109 break;
7110 case SECCAT_SDATA:
7111 sname = ".sdata";
7112 break;
7113 case SECCAT_TDATA:
7114 sname = ".tdata";
7115 break;
7116 case SECCAT_BSS:
7117 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7119 sname = ".noinit";
7120 break;
7122 if (bss_section)
7123 return bss_section;
7124 sname = ".bss";
7125 break;
7126 case SECCAT_SBSS:
7127 sname = ".sbss";
7128 break;
7129 case SECCAT_TBSS:
7130 sname = ".tbss";
7131 break;
7132 default:
7133 gcc_unreachable ();
7136 return get_named_section (decl, sname, reloc);
7139 /* Construct a unique section name based on the decl name and the
7140 categorization performed above. */
7142 void
7143 default_unique_section (tree decl, int reloc)
7145 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
7146 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
7147 const char *prefix, *name, *linkonce;
7148 char *string;
7149 tree id;
7151 switch (categorize_decl_for_section (decl, reloc))
7153 case SECCAT_TEXT:
7154 prefix = one_only ? ".t" : ".text";
7155 break;
7156 case SECCAT_RODATA:
7157 case SECCAT_RODATA_MERGE_STR:
7158 case SECCAT_RODATA_MERGE_STR_INIT:
7159 case SECCAT_RODATA_MERGE_CONST:
7160 prefix = one_only ? ".r" : ".rodata";
7161 break;
7162 case SECCAT_SRODATA:
7163 prefix = one_only ? ".s2" : ".sdata2";
7164 break;
7165 case SECCAT_DATA:
7166 prefix = one_only ? ".d" : ".data";
7167 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7169 prefix = one_only ? ".p" : ".persistent";
7170 break;
7172 break;
7173 case SECCAT_DATA_REL:
7174 prefix = one_only ? ".d.rel" : ".data.rel";
7175 break;
7176 case SECCAT_DATA_REL_LOCAL:
7177 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
7178 break;
7179 case SECCAT_DATA_REL_RO:
7180 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
7181 break;
7182 case SECCAT_DATA_REL_RO_LOCAL:
7183 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
7184 break;
7185 case SECCAT_SDATA:
7186 prefix = one_only ? ".s" : ".sdata";
7187 break;
7188 case SECCAT_BSS:
7189 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7191 prefix = one_only ? ".n" : ".noinit";
7192 break;
7194 prefix = one_only ? ".b" : ".bss";
7195 break;
7196 case SECCAT_SBSS:
7197 prefix = one_only ? ".sb" : ".sbss";
7198 break;
7199 case SECCAT_TDATA:
7200 prefix = one_only ? ".td" : ".tdata";
7201 break;
7202 case SECCAT_TBSS:
7203 prefix = one_only ? ".tb" : ".tbss";
7204 break;
7205 default:
7206 gcc_unreachable ();
7209 id = DECL_ASSEMBLER_NAME (decl);
7210 ultimate_transparent_alias_target (&id);
7211 name = IDENTIFIER_POINTER (id);
7212 name = targetm.strip_name_encoding (name);
7214 /* If we're using one_only, then there needs to be a .gnu.linkonce
7215 prefix to the section name. */
7216 linkonce = one_only ? ".gnu.linkonce" : "";
7218 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
7220 set_decl_section_name (decl, string);
7223 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
7225 static int
7226 compute_reloc_for_rtx_1 (const_rtx x)
7228 switch (GET_CODE (x))
7230 case SYMBOL_REF:
7231 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
7232 case LABEL_REF:
7233 return 1;
7234 default:
7235 return 0;
7239 /* Like compute_reloc_for_constant, except for an RTX. The return value
7240 is a mask for which bit 1 indicates a global relocation, and bit 0
7241 indicates a local relocation. */
7243 static int
7244 compute_reloc_for_rtx (const_rtx x)
7246 switch (GET_CODE (x))
7248 case SYMBOL_REF:
7249 case LABEL_REF:
7250 return compute_reloc_for_rtx_1 (x);
7252 case CONST:
7254 int reloc = 0;
7255 subrtx_iterator::array_type array;
7256 FOR_EACH_SUBRTX (iter, array, x, ALL)
7257 reloc |= compute_reloc_for_rtx_1 (*iter);
7258 return reloc;
7261 default:
7262 return 0;
7266 section *
7267 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
7268 rtx x,
7269 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7271 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
7272 return data_section;
7273 else
7274 return readonly_data_section;
7277 section *
7278 default_elf_select_rtx_section (machine_mode mode, rtx x,
7279 unsigned HOST_WIDE_INT align)
7281 int reloc = compute_reloc_for_rtx (x);
7283 /* ??? Handle small data here somehow. */
7285 if (reloc & targetm.asm_out.reloc_rw_mask ())
7287 if (reloc == 1)
7288 return get_named_section (NULL, ".data.rel.ro.local", 1);
7289 else
7290 return get_named_section (NULL, ".data.rel.ro", 3);
7293 return mergeable_constant_section (mode, align, 0);
7296 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
7298 void
7299 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
7301 rtx symbol;
7302 int flags;
7304 /* Careful not to prod global register variables. */
7305 if (!MEM_P (rtl))
7306 return;
7307 symbol = XEXP (rtl, 0);
7308 if (GET_CODE (symbol) != SYMBOL_REF)
7309 return;
7311 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
7312 if (TREE_CODE (decl) == FUNCTION_DECL)
7313 flags |= SYMBOL_FLAG_FUNCTION;
7314 if (targetm.binds_local_p (decl))
7315 flags |= SYMBOL_FLAG_LOCAL;
7316 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7317 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
7318 else if (targetm.in_small_data_p (decl))
7319 flags |= SYMBOL_FLAG_SMALL;
7320 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
7321 being PUBLIC, the thing *must* be defined in this translation unit.
7322 Prevent this buglet from being propagated into rtl code as well. */
7323 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
7324 flags |= SYMBOL_FLAG_EXTERNAL;
7326 SYMBOL_REF_FLAGS (symbol) = flags;
7329 /* By default, we do nothing for encode_section_info, so we need not
7330 do anything but discard the '*' marker. */
7332 const char *
7333 default_strip_name_encoding (const char *str)
7335 return str + (*str == '*');
7338 #ifdef ASM_OUTPUT_DEF
7339 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
7340 anchor relative to ".", the current section position. */
7342 void
7343 default_asm_output_anchor (rtx symbol)
7345 char buffer[100];
7347 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7348 SYMBOL_REF_BLOCK_OFFSET (symbol));
7349 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7351 #endif
7353 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
7355 bool
7356 default_use_anchors_for_symbol_p (const_rtx symbol)
7358 tree decl;
7359 section *sect = SYMBOL_REF_BLOCK (symbol)->sect;
7361 /* This function should only be called with non-zero SYMBOL_REF_BLOCK,
7362 furthermore get_block_for_section should not create object blocks
7363 for mergeable sections. */
7364 gcc_checking_assert (sect && !(sect->common.flags & SECTION_MERGE));
7366 /* Don't use anchors for small data sections. The small data register
7367 acts as an anchor for such sections. */
7368 if (sect->common.flags & SECTION_SMALL)
7369 return false;
7371 decl = SYMBOL_REF_DECL (symbol);
7372 if (decl && DECL_P (decl))
7374 /* Don't use section anchors for decls that might be defined or
7375 usurped by other modules. */
7376 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
7377 return false;
7379 /* Don't use section anchors for decls that will be placed in a
7380 small data section. */
7381 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
7382 one above. The problem is that we only use SECTION_SMALL for
7383 sections that should be marked as small in the section directive. */
7384 if (targetm.in_small_data_p (decl))
7385 return false;
7387 /* Don't use section anchors for decls that won't fit inside a single
7388 anchor range to reduce the amount of instructions required to refer
7389 to the entire declaration. */
7390 if (DECL_SIZE_UNIT (decl) == NULL_TREE
7391 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
7392 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
7393 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
7394 return false;
7397 return true;
7400 /* Return true when RESOLUTION indicate that symbol will be bound to the
7401 definition provided by current .o file. */
7403 static bool
7404 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
7406 return (resolution == LDPR_PREVAILING_DEF
7407 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7408 || resolution == LDPR_PREVAILING_DEF_IRONLY);
7411 /* Return true when RESOLUTION indicate that symbol will be bound locally
7412 within current executable or DSO. */
7414 static bool
7415 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7417 return (resolution == LDPR_PREVAILING_DEF
7418 || resolution == LDPR_PREVAILING_DEF_IRONLY
7419 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7420 || resolution == LDPR_PREEMPTED_REG
7421 || resolution == LDPR_PREEMPTED_IR
7422 || resolution == LDPR_RESOLVED_IR
7423 || resolution == LDPR_RESOLVED_EXEC);
7426 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7427 uninitialized common symbol in the executable will still be defined
7428 (through COPY relocation) in the executable. */
7430 bool
7431 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7432 bool extern_protected_data, bool common_local_p)
7434 /* A non-decl is an entry in the constant pool. */
7435 if (!DECL_P (exp))
7436 return true;
7438 /* Weakrefs may not bind locally, even though the weakref itself is always
7439 static and therefore local. Similarly, the resolver for ifunc functions
7440 might resolve to a non-local function.
7441 FIXME: We can resolve the weakref case more curefuly by looking at the
7442 weakref alias. */
7443 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7444 || (TREE_CODE (exp) == FUNCTION_DECL
7445 && cgraph_node::get (exp)
7446 && cgraph_node::get (exp)->ifunc_resolver))
7447 return false;
7449 /* Static variables are always local. */
7450 if (! TREE_PUBLIC (exp))
7451 return true;
7453 /* With resolution file in hand, take look into resolutions.
7454 We can't just return true for resolved_locally symbols,
7455 because dynamic linking might overwrite symbols
7456 in shared libraries. */
7457 bool resolved_locally = false;
7459 bool uninited_common = (DECL_COMMON (exp)
7460 && (DECL_INITIAL (exp) == NULL
7461 || (!in_lto_p
7462 && DECL_INITIAL (exp) == error_mark_node)));
7464 /* A non-external variable is defined locally only if it isn't
7465 uninitialized COMMON variable or common_local_p is true. */
7466 bool defined_locally = (!DECL_EXTERNAL (exp)
7467 && (!uninited_common || common_local_p));
7468 if (symtab_node *node = symtab_node::get (exp))
7470 if (node->in_other_partition)
7471 defined_locally = true;
7472 if (node->can_be_discarded_p ())
7474 else if (resolution_to_local_definition_p (node->resolution))
7475 defined_locally = resolved_locally = true;
7476 else if (resolution_local_p (node->resolution))
7477 resolved_locally = true;
7479 if (defined_locally && weak_dominate && !shlib)
7480 resolved_locally = true;
7482 /* Undefined weak symbols are never defined locally. */
7483 if (DECL_WEAK (exp) && !defined_locally)
7484 return false;
7486 /* A symbol is local if the user has said explicitly that it will be,
7487 or if we have a definition for the symbol. We cannot infer visibility
7488 for undefined symbols. */
7489 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7490 && (TREE_CODE (exp) == FUNCTION_DECL
7491 || !extern_protected_data
7492 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7493 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7494 return true;
7496 /* If PIC, then assume that any global name can be overridden by
7497 symbols resolved from other modules. */
7498 if (shlib)
7499 return false;
7501 /* Variables defined outside this object might not be local. */
7502 if (DECL_EXTERNAL (exp) && !resolved_locally)
7503 return false;
7505 /* Non-dominant weak symbols are not defined locally. */
7506 if (DECL_WEAK (exp) && !resolved_locally)
7507 return false;
7509 /* Uninitialized COMMON variable may be unified with symbols
7510 resolved from other modules. */
7511 if (uninited_common && !resolved_locally)
7512 return false;
7514 /* Otherwise we're left with initialized (or non-common) global data
7515 which is of necessity defined locally. */
7516 return true;
7519 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7520 wrt cross-module name binding. */
7522 bool
7523 default_binds_local_p (const_tree exp)
7525 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7528 /* Similar to default_binds_local_p, but common symbol may be local and
7529 extern protected data is non-local. */
7531 bool
7532 default_binds_local_p_2 (const_tree exp)
7534 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7535 !flag_pic);
7538 bool
7539 default_binds_local_p_1 (const_tree exp, int shlib)
7541 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7544 /* Return true when references to DECL must bind to current definition in
7545 final executable.
7547 The condition is usually equivalent to whether the function binds to the
7548 current module (shared library or executable), that is to binds_local_p.
7549 We use this fact to avoid need for another target hook and implement
7550 the logic using binds_local_p and just special cases where
7551 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7552 the weak definitions (that can be overwritten at linktime by other
7553 definition from different object file) and when resolution info is available
7554 we simply use the knowledge passed to us by linker plugin. */
7555 bool
7556 decl_binds_to_current_def_p (const_tree decl)
7558 gcc_assert (DECL_P (decl));
7559 if (!targetm.binds_local_p (decl))
7560 return false;
7561 if (!TREE_PUBLIC (decl))
7562 return true;
7564 /* When resolution is available, just use it. */
7565 if (symtab_node *node = symtab_node::get (decl))
7567 if (node->resolution != LDPR_UNKNOWN
7568 && !node->can_be_discarded_p ())
7569 return resolution_to_local_definition_p (node->resolution);
7572 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7573 binds locally but still can be overwritten), DECL_COMMON (can be merged
7574 with a non-common definition somewhere in the same module) or
7575 DECL_EXTERNAL.
7576 This rely on fact that binds_local_p behave as decl_replaceable_p
7577 for all other declaration types. */
7578 if (DECL_WEAK (decl))
7579 return false;
7580 if (DECL_COMMON (decl)
7581 && (DECL_INITIAL (decl) == NULL
7582 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7583 return false;
7584 if (DECL_EXTERNAL (decl))
7585 return false;
7586 return true;
7589 /* A replaceable function or variable is one which may be replaced
7590 at link-time with an entirely different definition, provided that the
7591 replacement has the same type. For example, functions declared
7592 with __attribute__((weak)) on most systems are replaceable.
7594 COMDAT functions are not replaceable, since all definitions of the
7595 function must be equivalent. It is important that COMDAT functions
7596 not be treated as replaceable so that use of C++ template
7597 instantiations is not penalized. */
7599 bool
7600 decl_replaceable_p (tree decl)
7602 gcc_assert (DECL_P (decl));
7603 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7604 return false;
7605 if (!flag_semantic_interposition
7606 && !DECL_WEAK (decl))
7607 return false;
7608 return !decl_binds_to_current_def_p (decl);
7611 /* Default function to output code that will globalize a label. A
7612 target must define GLOBAL_ASM_OP or provide its own function to
7613 globalize a label. */
7614 #ifdef GLOBAL_ASM_OP
7615 void
7616 default_globalize_label (FILE * stream, const char *name)
7618 fputs (GLOBAL_ASM_OP, stream);
7619 assemble_name (stream, name);
7620 putc ('\n', stream);
7622 #endif /* GLOBAL_ASM_OP */
7624 /* Default function to output code that will globalize a declaration. */
7625 void
7626 default_globalize_decl_name (FILE * stream, tree decl)
7628 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7629 targetm.asm_out.globalize_label (stream, name);
7632 /* Default function to output a label for unwind information. The
7633 default is to do nothing. A target that needs nonlocal labels for
7634 unwind information must provide its own function to do this. */
7635 void
7636 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7637 tree decl ATTRIBUTE_UNUSED,
7638 int for_eh ATTRIBUTE_UNUSED,
7639 int empty ATTRIBUTE_UNUSED)
7643 /* Default function to output a label to divide up the exception table.
7644 The default is to do nothing. A target that needs/wants to divide
7645 up the table must provide it's own function to do this. */
7646 void
7647 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7651 /* This is how to output an internal numbered label where PREFIX is
7652 the class of label and LABELNO is the number within the class. */
7654 void
7655 default_generate_internal_label (char *buf, const char *prefix,
7656 unsigned long labelno)
7658 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7661 /* This is how to output an internal numbered label where PREFIX is
7662 the class of label and LABELNO is the number within the class. */
7664 void
7665 default_internal_label (FILE *stream, const char *prefix,
7666 unsigned long labelno)
7668 char *const buf = (char *) alloca (40 + strlen (prefix));
7669 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7670 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7674 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7676 void
7677 default_asm_declare_constant_name (FILE *file, const char *name,
7678 const_tree exp ATTRIBUTE_UNUSED,
7679 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7681 assemble_label (file, name);
7684 /* This is the default behavior at the beginning of a file. It's
7685 controlled by two other target-hook toggles. */
7686 void
7687 default_file_start (void)
7689 if (targetm.asm_file_start_app_off
7690 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7691 fputs (ASM_APP_OFF, asm_out_file);
7693 if (targetm.asm_file_start_file_directive)
7695 /* LTO produced units have no meaningful main_input_filename. */
7696 if (in_lto_p)
7697 output_file_directive (asm_out_file, "<artificial>");
7698 else
7699 output_file_directive (asm_out_file, main_input_filename);
7703 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7704 which emits a special section directive used to indicate whether or
7705 not this object file needs an executable stack. This is primarily
7706 a GNU extension to ELF but could be used on other targets. */
7708 int trampolines_created;
7710 void
7711 file_end_indicate_exec_stack (void)
7713 unsigned int flags = SECTION_DEBUG;
7714 if (trampolines_created)
7715 flags |= SECTION_CODE;
7717 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7720 /* Emit a special section directive to indicate that this object file
7721 was compiled with -fsplit-stack. This is used to let the linker
7722 detect calls between split-stack code and non-split-stack code, so
7723 that it can modify the split-stack code to allocate a sufficiently
7724 large stack. We emit another special section if there are any
7725 functions in this file which have the no_split_stack attribute, to
7726 prevent the linker from warning about being unable to convert the
7727 functions if they call non-split-stack code. */
7729 void
7730 file_end_indicate_split_stack (void)
7732 if (flag_split_stack)
7734 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7735 NULL));
7736 if (saw_no_split_stack)
7737 switch_to_section (get_section (".note.GNU-no-split-stack",
7738 SECTION_DEBUG, NULL));
7742 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7743 a get_unnamed_section callback. */
7745 void
7746 output_section_asm_op (const void *directive)
7748 fprintf (asm_out_file, "%s\n", (const char *) directive);
7751 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7752 the current section is NEW_SECTION. */
7754 void
7755 switch_to_section (section *new_section, tree decl)
7757 if (in_section == new_section)
7759 if (SUPPORTS_SHF_GNU_RETAIN
7760 && (new_section->common.flags & SECTION_NAMED)
7761 && decl != nullptr
7762 && DECL_P (decl)
7763 && (!!DECL_PRESERVE_P (decl)
7764 != !!(new_section->common.flags & SECTION_RETAIN)))
7766 /* If the SECTION_RETAIN bit doesn't match, switch to a new
7767 section. */
7768 tree used_decl, no_used_decl;
7770 if (DECL_PRESERVE_P (decl))
7772 new_section->common.flags |= SECTION_RETAIN;
7773 used_decl = decl;
7774 no_used_decl = new_section->named.decl;
7776 else
7778 new_section->common.flags &= ~(SECTION_RETAIN
7779 | SECTION_DECLARED);
7780 used_decl = new_section->named.decl;
7781 no_used_decl = decl;
7783 warning (OPT_Wattributes,
7784 "%+qD without %<used%> attribute and %qD with "
7785 "%<used%> attribute are placed in a section with "
7786 "the same name", no_used_decl, used_decl);
7787 inform (DECL_SOURCE_LOCATION (used_decl),
7788 "%qD was declared here", used_decl);
7790 else
7791 return;
7794 if (new_section->common.flags & SECTION_FORGET)
7795 in_section = NULL;
7796 else
7797 in_section = new_section;
7799 switch (SECTION_STYLE (new_section))
7801 case SECTION_NAMED:
7802 targetm.asm_out.named_section (new_section->named.name,
7803 new_section->named.common.flags,
7804 new_section->named.decl);
7805 break;
7807 case SECTION_UNNAMED:
7808 new_section->unnamed.callback (new_section->unnamed.data);
7809 break;
7811 case SECTION_NOSWITCH:
7812 gcc_unreachable ();
7813 break;
7816 new_section->common.flags |= SECTION_DECLARED;
7819 /* If block symbol SYMBOL has not yet been assigned an offset, place
7820 it at the end of its block. */
7822 void
7823 place_block_symbol (rtx symbol)
7825 unsigned HOST_WIDE_INT size, mask, offset;
7826 class constant_descriptor_rtx *desc;
7827 unsigned int alignment;
7828 struct object_block *block;
7829 tree decl;
7831 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7832 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7833 return;
7835 /* Work out the symbol's size and alignment. */
7836 if (CONSTANT_POOL_ADDRESS_P (symbol))
7838 desc = SYMBOL_REF_CONSTANT (symbol);
7839 alignment = desc->align;
7840 size = GET_MODE_SIZE (desc->mode);
7842 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7844 decl = SYMBOL_REF_DECL (symbol);
7845 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7846 alignment = DECL_ALIGN (decl);
7847 size = get_constant_size (DECL_INITIAL (decl));
7848 if ((flag_sanitize & SANITIZE_ADDRESS)
7849 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7850 && asan_protect_global (DECL_INITIAL (decl)))
7852 size += asan_red_zone_size (size);
7853 alignment = MAX (alignment,
7854 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7857 else
7859 struct symtab_node *snode;
7860 decl = SYMBOL_REF_DECL (symbol);
7862 snode = symtab_node::get (decl);
7863 if (snode->alias)
7865 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7867 gcc_assert (MEM_P (target)
7868 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7869 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7870 target = XEXP (target, 0);
7871 place_block_symbol (target);
7872 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7873 return;
7875 alignment = get_variable_align (decl);
7876 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7877 if ((flag_sanitize & SANITIZE_ADDRESS)
7878 && asan_protect_global (decl))
7880 size += asan_red_zone_size (size);
7881 alignment = MAX (alignment,
7882 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7886 /* Calculate the object's offset from the start of the block. */
7887 block = SYMBOL_REF_BLOCK (symbol);
7888 mask = alignment / BITS_PER_UNIT - 1;
7889 offset = (block->size + mask) & ~mask;
7890 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7892 /* Record the block's new alignment and size. */
7893 block->alignment = MAX (block->alignment, alignment);
7894 block->size = offset + size;
7896 vec_safe_push (block->objects, symbol);
7899 /* Return the anchor that should be used to address byte offset OFFSET
7900 from the first object in BLOCK. MODEL is the TLS model used
7901 to access it. */
7904 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7905 enum tls_model model)
7907 char label[100];
7908 unsigned int begin, middle, end;
7909 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7910 rtx anchor;
7912 /* Work out the anchor's offset. Use an offset of 0 for the first
7913 anchor so that we don't pessimize the case where we take the address
7914 of a variable at the beginning of the block. This is particularly
7915 useful when a block has only one variable assigned to it.
7917 We try to place anchors RANGE bytes apart, so there can then be
7918 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7919 a ptr_mode offset. With some target settings, the lowest such
7920 anchor might be out of range for the lowest ptr_mode offset;
7921 likewise the highest anchor for the highest offset. Use anchors
7922 at the extreme ends of the ptr_mode range in such cases.
7924 All arithmetic uses unsigned integers in order to avoid
7925 signed overflow. */
7926 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7927 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7928 range = max_offset - min_offset + 1;
7929 if (range == 0)
7930 offset = 0;
7931 else
7933 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7934 if (offset < 0)
7936 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7937 delta -= delta % range;
7938 if (delta > bias)
7939 delta = bias;
7940 offset = (HOST_WIDE_INT) (-delta);
7942 else
7944 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7945 delta -= delta % range;
7946 if (delta > bias - 1)
7947 delta = bias - 1;
7948 offset = (HOST_WIDE_INT) delta;
7952 /* Do a binary search to see if there's already an anchor we can use.
7953 Set BEGIN to the new anchor's index if not. */
7954 begin = 0;
7955 end = vec_safe_length (block->anchors);
7956 while (begin != end)
7958 middle = (end + begin) / 2;
7959 anchor = (*block->anchors)[middle];
7960 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7961 end = middle;
7962 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7963 begin = middle + 1;
7964 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7965 end = middle;
7966 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7967 begin = middle + 1;
7968 else
7969 return anchor;
7972 /* Create a new anchor with a unique label. */
7973 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7974 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7975 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7976 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7978 /* Insert it at index BEGIN. */
7979 vec_safe_insert (block->anchors, begin, anchor);
7980 return anchor;
7983 /* Output the objects in BLOCK. */
7985 static void
7986 output_object_block (struct object_block *block)
7988 class constant_descriptor_rtx *desc;
7989 unsigned int i;
7990 HOST_WIDE_INT offset;
7991 tree decl;
7992 rtx symbol;
7994 if (!block->objects)
7995 return;
7997 /* Switch to the section and make sure that the first byte is
7998 suitably aligned. */
7999 /* Special case VTV comdat sections similar to assemble_variable. */
8000 if (SECTION_STYLE (block->sect) == SECTION_NAMED
8001 && block->sect->named.name
8002 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
8003 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
8004 else
8005 switch_to_section (block->sect);
8007 gcc_checking_assert (!(block->sect->common.flags & SECTION_MERGE));
8008 assemble_align (block->alignment);
8010 /* Define the values of all anchors relative to the current section
8011 position. */
8012 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
8013 targetm.asm_out.output_anchor (symbol);
8015 /* Output the objects themselves. */
8016 offset = 0;
8017 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
8019 /* Move to the object's offset, padding with zeros if necessary. */
8020 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
8021 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
8022 if (CONSTANT_POOL_ADDRESS_P (symbol))
8024 desc = SYMBOL_REF_CONSTANT (symbol);
8025 /* Pass 1 for align as we have already laid out everything in the block.
8026 So aligning shouldn't be necessary. */
8027 output_constant_pool_1 (desc, 1);
8028 offset += GET_MODE_SIZE (desc->mode);
8030 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
8032 HOST_WIDE_INT size;
8033 decl = SYMBOL_REF_DECL (symbol);
8034 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
8035 DECL_ALIGN (decl), false);
8037 size = get_constant_size (DECL_INITIAL (decl));
8038 offset += size;
8039 if ((flag_sanitize & SANITIZE_ADDRESS)
8040 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8041 && asan_protect_global (DECL_INITIAL (decl)))
8043 size = asan_red_zone_size (size);
8044 assemble_zeros (size);
8045 offset += size;
8048 else
8050 HOST_WIDE_INT size;
8051 decl = SYMBOL_REF_DECL (symbol);
8052 assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
8053 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8054 offset += size;
8055 if ((flag_sanitize & SANITIZE_ADDRESS)
8056 && asan_protect_global (decl))
8058 size = asan_red_zone_size (size);
8059 assemble_zeros (size);
8060 offset += size;
8066 /* A callback for qsort to compare object_blocks. */
8068 static int
8069 output_object_block_compare (const void *x, const void *y)
8071 object_block *p1 = *(object_block * const*)x;
8072 object_block *p2 = *(object_block * const*)y;
8074 if (p1->sect->common.flags & SECTION_NAMED
8075 && !(p2->sect->common.flags & SECTION_NAMED))
8076 return 1;
8078 if (!(p1->sect->common.flags & SECTION_NAMED)
8079 && p2->sect->common.flags & SECTION_NAMED)
8080 return -1;
8082 if (p1->sect->common.flags & SECTION_NAMED
8083 && p2->sect->common.flags & SECTION_NAMED)
8084 return strcmp (p1->sect->named.name, p2->sect->named.name);
8086 unsigned f1 = p1->sect->common.flags;
8087 unsigned f2 = p2->sect->common.flags;
8088 if (f1 == f2)
8089 return 0;
8090 return f1 < f2 ? -1 : 1;
8093 /* Output the definitions of all object_blocks. */
8095 void
8096 output_object_blocks (void)
8098 vec<object_block *, va_heap> v;
8099 v.create (object_block_htab->elements ());
8100 object_block *obj;
8101 hash_table<object_block_hasher>::iterator hi;
8103 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
8104 v.quick_push (obj);
8106 /* Sort them in order to output them in a deterministic manner,
8107 otherwise we may get .rodata sections in different orders with
8108 and without -g. */
8109 v.qsort (output_object_block_compare);
8110 unsigned i;
8111 FOR_EACH_VEC_ELT (v, i, obj)
8112 output_object_block (obj);
8114 v.release ();
8117 /* This function provides a possible implementation of the
8118 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
8119 by -frecord-gcc-switches it creates a new mergeable, string section in the
8120 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
8121 contains the switches in ASCII format.
8123 FIXME: This code does not correctly handle double quote characters
8124 that appear inside strings, (it strips them rather than preserving them).
8125 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
8126 characters - instead it treats them as sub-string separators. Since
8127 we want to emit NUL strings terminators into the object file we have to use
8128 ASM_OUTPUT_SKIP. */
8130 void
8131 elf_record_gcc_switches (const char *options)
8133 section *sec = get_section (targetm.asm_out.record_gcc_switches_section,
8134 SECTION_DEBUG | SECTION_MERGE
8135 | SECTION_STRINGS | (SECTION_ENTSIZE & 1), NULL);
8136 switch_to_section (sec);
8137 ASM_OUTPUT_ASCII (asm_out_file, options, strlen (options) + 1);
8140 /* Emit text to declare externally defined symbols. It is needed to
8141 properly support non-default visibility. */
8142 void
8143 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
8144 tree decl,
8145 const char *name ATTRIBUTE_UNUSED)
8147 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8148 set in order to avoid putting out names that are never really
8149 used. Always output visibility specified in the source. */
8150 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
8151 && (DECL_VISIBILITY_SPECIFIED (decl)
8152 || targetm.binds_local_p (decl)))
8153 maybe_assemble_visibility (decl);
8156 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8158 void
8159 default_asm_output_source_filename (FILE *file, const char *name)
8161 #ifdef ASM_OUTPUT_SOURCE_FILENAME
8162 ASM_OUTPUT_SOURCE_FILENAME (file, name);
8163 #else
8164 fprintf (file, "\t.file\t");
8165 output_quoted_string (file, name);
8166 putc ('\n', file);
8167 #endif
8170 /* Output a file name in the form wanted by System V. */
8172 void
8173 output_file_directive (FILE *asm_file, const char *input_name)
8175 int len;
8176 const char *na;
8178 if (input_name == NULL)
8179 input_name = "<stdin>";
8180 else
8181 input_name = remap_debug_filename (input_name);
8183 len = strlen (input_name);
8184 na = input_name + len;
8186 /* NA gets INPUT_NAME sans directory names. */
8187 while (na > input_name)
8189 if (IS_DIR_SEPARATOR (na[-1]))
8190 break;
8191 na--;
8194 targetm.asm_out.output_source_filename (asm_file, na);
8197 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
8198 EXP. */
8200 make_debug_expr_from_rtl (const_rtx exp)
8202 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
8203 machine_mode mode = GET_MODE (exp);
8204 rtx dval;
8206 DECL_ARTIFICIAL (ddecl) = 1;
8207 if (REG_P (exp) && REG_EXPR (exp))
8208 type = TREE_TYPE (REG_EXPR (exp));
8209 else if (MEM_P (exp) && MEM_EXPR (exp))
8210 type = TREE_TYPE (MEM_EXPR (exp));
8211 else
8212 type = NULL_TREE;
8213 if (type && TYPE_MODE (type) == mode)
8214 TREE_TYPE (ddecl) = type;
8215 else
8216 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
8217 SET_DECL_MODE (ddecl, mode);
8218 dval = gen_rtx_DEBUG_EXPR (mode);
8219 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
8220 SET_DECL_RTL (ddecl, dval);
8221 return dval;
8224 #ifdef ELF_ASCII_ESCAPES
8225 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
8227 void
8228 default_elf_asm_output_limited_string (FILE *f, const char *s)
8230 int escape;
8231 unsigned char c;
8233 fputs (STRING_ASM_OP, f);
8234 putc ('"', f);
8235 while (*s != '\0')
8237 c = *s;
8238 escape = ELF_ASCII_ESCAPES[c];
8239 switch (escape)
8241 case 0:
8242 putc (c, f);
8243 break;
8244 case 1:
8245 putc ('\\', f);
8246 putc ('0'+((c>>6)&7), f);
8247 putc ('0'+((c>>3)&7), f);
8248 putc ('0'+(c&7), f);
8249 break;
8250 default:
8251 putc ('\\', f);
8252 putc (escape, f);
8253 break;
8255 s++;
8257 putc ('\"', f);
8258 putc ('\n', f);
8261 /* Default ASM_OUTPUT_ASCII for ELF targets. */
8263 void
8264 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
8266 const char *limit = s + len;
8267 const char *last_null = NULL;
8268 unsigned bytes_in_chunk = 0;
8269 unsigned char c;
8270 int escape;
8272 for (; s < limit; s++)
8274 const char *p;
8276 if (bytes_in_chunk >= 60)
8278 putc ('\"', f);
8279 putc ('\n', f);
8280 bytes_in_chunk = 0;
8283 if (s > last_null)
8285 for (p = s; p < limit && *p != '\0'; p++)
8286 continue;
8287 last_null = p;
8289 else
8290 p = last_null;
8292 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
8294 if (bytes_in_chunk > 0)
8296 putc ('\"', f);
8297 putc ('\n', f);
8298 bytes_in_chunk = 0;
8301 default_elf_asm_output_limited_string (f, s);
8302 s = p;
8304 else
8306 if (bytes_in_chunk == 0)
8307 fputs (ASCII_DATA_ASM_OP "\"", f);
8309 c = *s;
8310 escape = ELF_ASCII_ESCAPES[c];
8311 switch (escape)
8313 case 0:
8314 putc (c, f);
8315 bytes_in_chunk++;
8316 break;
8317 case 1:
8318 putc ('\\', f);
8319 putc ('0'+((c>>6)&7), f);
8320 putc ('0'+((c>>3)&7), f);
8321 putc ('0'+(c&7), f);
8322 bytes_in_chunk += 4;
8323 break;
8324 default:
8325 putc ('\\', f);
8326 putc (escape, f);
8327 bytes_in_chunk += 2;
8328 break;
8334 if (bytes_in_chunk > 0)
8336 putc ('\"', f);
8337 putc ('\n', f);
8340 #endif
8342 static GTY(()) section *elf_init_array_section;
8343 static GTY(()) section *elf_fini_array_section;
8345 static section *
8346 get_elf_initfini_array_priority_section (int priority,
8347 bool constructor_p)
8349 section *sec;
8350 if (priority != DEFAULT_INIT_PRIORITY)
8352 char buf[18];
8353 sprintf (buf, "%s.%.5u",
8354 constructor_p ? ".init_array" : ".fini_array",
8355 priority);
8356 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8358 else
8360 if (constructor_p)
8362 if (elf_init_array_section == NULL)
8363 elf_init_array_section
8364 = get_section (".init_array",
8365 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8366 sec = elf_init_array_section;
8368 else
8370 if (elf_fini_array_section == NULL)
8371 elf_fini_array_section
8372 = get_section (".fini_array",
8373 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8374 sec = elf_fini_array_section;
8377 return sec;
8380 /* Use .init_array section for constructors. */
8382 void
8383 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8385 section *sec = get_elf_initfini_array_priority_section (priority,
8386 true);
8387 assemble_addr_to_section (symbol, sec);
8390 /* Use .fini_array section for destructors. */
8392 void
8393 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8395 section *sec = get_elf_initfini_array_priority_section (priority,
8396 false);
8397 assemble_addr_to_section (symbol, sec);
8400 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8402 This is a bit of a cheat. The real default is a no-op, but this
8403 hook is the default for all targets with a .ident directive. */
8405 void
8406 default_asm_output_ident_directive (const char *ident_str)
8408 const char *ident_asm_op = "\t.ident\t";
8410 /* If we are still in the front end, do not write out the string
8411 to asm_out_file. Instead, add a fake top-level asm statement.
8412 This allows the front ends to use this hook without actually
8413 writing to asm_out_file, to handle #ident or Pragma Ident. */
8414 if (symtab->state == PARSING)
8416 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8417 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8419 else
8420 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8424 /* This function ensures that vtable_map variables are not only
8425 in the comdat section, but that each variable has its own unique
8426 comdat name. Without this the variables end up in the same section
8427 with a single comdat name.
8429 FIXME: resolve_unique_section needs to deal better with
8430 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8431 that is fixed, this if-else statement can be replaced with
8432 a single call to "switch_to_section (sect)". */
8434 static void
8435 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8437 #if defined (OBJECT_FORMAT_ELF)
8438 targetm.asm_out.named_section (sect->named.name,
8439 sect->named.common.flags
8440 | SECTION_LINKONCE,
8441 DECL_NAME (decl));
8442 in_section = sect;
8443 #else
8444 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8445 Therefore the following check is used.
8446 In case a the target is PE or COFF a comdat group section
8447 is created, e.g. .vtable_map_vars$foo. The linker places
8448 everything in .vtable_map_vars at the end.
8450 A fix could be made in
8451 gcc/config/i386/winnt.c: i386_pe_unique_section. */
8452 if (TARGET_PECOFF)
8454 char *name;
8456 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
8457 name = ACONCAT ((sect->named.name, "$",
8458 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
8459 else
8460 name = ACONCAT ((sect->named.name, "$",
8461 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
8462 NULL));
8464 targetm.asm_out.named_section (name,
8465 sect->named.common.flags
8466 | SECTION_LINKONCE,
8467 DECL_NAME (decl));
8468 in_section = sect;
8470 else
8471 switch_to_section (sect);
8472 #endif
8475 #include "gt-varasm.h"