Fix couple of endianness issues in fold_ctor_reference
[official-gcc.git] / gcc / varasm.cc
blob8ae0a2555cdab11986fb5b3a17b1cf4297499406
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2023 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 /* The (assembler) name of the first globally-visible object output. */
66 extern GTY(()) const char *first_global_object_name;
67 extern GTY(()) const char *weak_global_object_name;
69 const char *first_global_object_name;
70 const char *weak_global_object_name;
72 class addr_const;
73 class constant_descriptor_rtx;
74 struct rtx_constant_pool;
76 #define n_deferred_constants (crtl->varasm.deferred_constants)
78 /* Number for making the label on the next
79 constant that is stored in memory. */
81 static GTY(()) int const_labelno;
83 /* Carry information from ASM_DECLARE_OBJECT_NAME
84 to ASM_FINISH_DECLARE_OBJECT. */
86 int size_directive_output;
88 /* The last decl for which assemble_variable was called,
89 if it did ASM_DECLARE_OBJECT_NAME.
90 If the last call to assemble_variable didn't do that,
91 this holds 0. */
93 tree last_assemble_variable_decl;
95 /* The following global variable indicates if the first basic block
96 in a function belongs to the cold partition or not. */
98 bool first_function_block_is_cold;
100 /* Whether we saw any functions with no_split_stack. */
102 static bool saw_no_split_stack;
104 static const char *strip_reg_name (const char *);
105 static bool contains_pointers_p (tree);
106 #ifdef ASM_OUTPUT_EXTERNAL
107 static bool incorporeal_function_p (tree);
108 #endif
109 static void decode_addr_const (tree, class addr_const *);
110 static hashval_t const_hash_1 (const tree);
111 static bool compare_constant (const tree, const tree);
112 static void output_constant_def_contents (rtx);
113 static void output_addressed_constants (tree, int);
114 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
115 unsigned int, bool, bool);
116 static void globalize_decl (tree);
117 static bool decl_readonly_section_1 (enum section_category);
118 #ifdef BSS_SECTION_ASM_OP
119 #ifdef ASM_OUTPUT_ALIGNED_BSS
120 static void asm_output_aligned_bss (FILE *, tree, const char *,
121 unsigned HOST_WIDE_INT, int)
122 ATTRIBUTE_UNUSED;
123 #endif
124 #endif /* BSS_SECTION_ASM_OP */
125 static void mark_weak (tree);
126 static void output_constant_pool (const char *, tree);
127 static void handle_vtv_comdat_section (section *, const_tree);
129 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
130 section *text_section;
131 section *data_section;
132 section *readonly_data_section;
133 section *sdata_section;
134 section *ctors_section;
135 section *dtors_section;
136 section *bss_section;
137 section *sbss_section;
139 /* Various forms of common section. All are guaranteed to be nonnull. */
140 section *tls_comm_section;
141 section *comm_section;
142 section *lcomm_section;
144 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
145 May be null. */
146 section *bss_noswitch_section;
148 /* The section that holds the main exception table, when known. The section
149 is set either by the target's init_sections hook or by the first call to
150 switch_to_exception_section. */
151 section *exception_section;
153 /* The section that holds the DWARF2 frame unwind information, when known.
154 The section is set either by the target's init_sections hook or by the
155 first call to switch_to_eh_frame_section. */
156 section *eh_frame_section;
158 /* asm_out_file's current section. This is NULL if no section has yet
159 been selected or if we lose track of what the current section is. */
160 section *in_section;
162 /* True if code for the current function is currently being directed
163 at the cold section. */
164 bool in_cold_section_p;
166 /* The following global holds the "function name" for the code in the
167 cold section of a function, if hot/cold function splitting is enabled
168 and there was actually code that went into the cold section. A
169 pseudo function name is needed for the cold section of code for some
170 debugging tools that perform symbolization. */
171 tree cold_function_name = NULL_TREE;
173 /* A linked list of all the unnamed sections. */
174 static GTY(()) section *unnamed_sections;
176 /* Return a nonzero value if DECL has a section attribute. */
177 #define IN_NAMED_SECTION(DECL) \
178 (VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
180 struct section_hasher : ggc_ptr_hash<section>
182 typedef const char *compare_type;
184 static hashval_t hash (section *);
185 static bool equal (section *, const char *);
188 /* Hash table of named sections. */
189 static GTY(()) hash_table<section_hasher> *section_htab;
191 struct object_block_hasher : ggc_ptr_hash<object_block>
193 typedef const section *compare_type;
195 static hashval_t hash (object_block *);
196 static bool equal (object_block *, const section *);
199 /* A table of object_blocks, indexed by section. */
200 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
202 /* The next number to use for internal anchor labels. */
203 static GTY(()) int anchor_labelno;
205 /* A pool of constants that can be shared between functions. */
206 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
208 /* Helper routines for maintaining section_htab. */
210 bool
211 section_hasher::equal (section *old, const char *new_name)
213 return strcmp (old->named.name, new_name) == 0;
216 hashval_t
217 section_hasher::hash (section *old)
219 return htab_hash_string (old->named.name);
222 /* Return a hash value for section SECT. */
224 static hashval_t
225 hash_section (section *sect)
227 if (sect->common.flags & SECTION_NAMED)
228 return htab_hash_string (sect->named.name);
229 return sect->common.flags & ~SECTION_DECLARED;
232 /* Helper routines for maintaining object_block_htab. */
234 inline bool
235 object_block_hasher::equal (object_block *old, const section *new_section)
237 return old->sect == new_section;
240 hashval_t
241 object_block_hasher::hash (object_block *old)
243 return hash_section (old->sect);
246 /* Return a new unnamed section with the given fields. */
248 section *
249 get_unnamed_section (unsigned int flags, void (*callback) (const char *),
250 const char *data)
252 section *sect;
254 sect = ggc_alloc<section> ();
255 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
256 sect->unnamed.callback = callback;
257 sect->unnamed.data = data;
258 sect->unnamed.next = unnamed_sections;
260 unnamed_sections = sect;
261 return sect;
264 /* Return a SECTION_NOSWITCH section with the given fields. */
266 static section *
267 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
269 section *sect;
271 sect = ggc_alloc<section> ();
272 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
273 sect->noswitch.callback = callback;
275 return sect;
278 /* Return the named section structure associated with NAME. Create
279 a new section with the given fields if no such structure exists.
280 When NOT_EXISTING, then fail if the section already exists. Return
281 the existing section if the SECTION_RETAIN bit doesn't match. Set
282 the SECTION_WRITE | SECTION_RELRO bits on the existing section
283 if one of the section flags is SECTION_WRITE | SECTION_RELRO and the
284 other has none of these flags in named sections and either the section
285 hasn't been declared yet or has been declared as writable. */
287 section *
288 get_section (const char *name, unsigned int flags, tree decl,
289 bool not_existing)
291 section *sect, **slot;
293 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
294 INSERT);
295 flags |= SECTION_NAMED;
296 if (decl != nullptr
297 && DECL_P (decl)
298 && lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
299 flags |= SECTION_RETAIN;
300 if (*slot == NULL)
302 sect = ggc_alloc<section> ();
303 sect->named.common.flags = flags;
304 sect->named.name = ggc_strdup (name);
305 sect->named.decl = decl;
306 *slot = sect;
308 else
310 if (not_existing)
311 internal_error ("section already exists: %qs", name);
313 sect = *slot;
314 /* It is fine if one of the sections has SECTION_NOTYPE as long as
315 the other has none of the contrary flags (see the logic at the end
316 of default_section_type_flags, below). */
317 if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
318 && !((sect->common.flags | flags)
319 & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
320 | (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
322 sect->common.flags |= SECTION_NOTYPE;
323 flags |= SECTION_NOTYPE;
325 if ((sect->common.flags & ~SECTION_DECLARED) != flags
326 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
328 /* It is fine if one of the section flags is
329 SECTION_WRITE | SECTION_RELRO and the other has none of these
330 flags (i.e. read-only) in named sections and either the
331 section hasn't been declared yet or has been declared as writable.
332 In that case just make sure the resulting flags are
333 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
334 relocations. */
335 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
336 == (SECTION_WRITE | SECTION_RELRO)
337 && (sect->common.flags
338 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
339 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
340 && ((sect->common.flags & SECTION_DECLARED) == 0
341 || (sect->common.flags & SECTION_WRITE)))
343 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
344 return sect;
346 /* If the SECTION_RETAIN bit doesn't match, return and switch
347 to a new section later. */
348 if ((sect->common.flags & SECTION_RETAIN)
349 != (flags & SECTION_RETAIN))
350 return sect;
351 /* Sanity check user variables for flag changes. */
352 if (sect->named.decl != NULL
353 && DECL_P (sect->named.decl)
354 && decl != sect->named.decl)
356 if (decl != NULL && DECL_P (decl))
357 error ("%+qD causes a section type conflict with %qD",
358 decl, sect->named.decl);
359 else
360 error ("section type conflict with %qD", sect->named.decl);
361 inform (DECL_SOURCE_LOCATION (sect->named.decl),
362 "%qD was declared here", sect->named.decl);
364 else if (decl != NULL && DECL_P (decl))
365 error ("%+qD causes a section type conflict", decl);
366 else
367 error ("section type conflict");
368 /* Make sure we don't error about one section multiple times. */
369 sect->common.flags |= SECTION_OVERRIDE;
372 return sect;
375 /* Return true if the current compilation mode benefits from having
376 objects grouped into blocks. */
378 static bool
379 use_object_blocks_p (void)
381 return flag_section_anchors;
384 /* Return the object_block structure for section SECT. Create a new
385 structure if we haven't created one already. Return null if SECT
386 itself is null. Return also null for mergeable sections since
387 section anchors can't be used in mergeable sections anyway,
388 because the linker might move objects around, and using the
389 object blocks infrastructure in that case is both a waste and a
390 maintenance burden. */
392 static struct object_block *
393 get_block_for_section (section *sect)
395 struct object_block *block;
397 if (sect == NULL)
398 return NULL;
400 if (sect->common.flags & SECTION_MERGE)
401 return NULL;
403 object_block **slot
404 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
405 INSERT);
406 block = *slot;
407 if (block == NULL)
409 block = ggc_cleared_alloc<object_block> ();
410 block->sect = sect;
411 *slot = block;
413 return block;
416 /* Create a symbol with label LABEL and place it at byte offset
417 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
418 is not yet known. LABEL must be a garbage-collected string. */
420 static rtx
421 create_block_symbol (const char *label, struct object_block *block,
422 HOST_WIDE_INT offset)
424 rtx symbol;
425 unsigned int size;
427 /* Create the extended SYMBOL_REF. */
428 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
429 symbol = (rtx) ggc_internal_alloc (size);
431 /* Initialize the normal SYMBOL_REF fields. */
432 memset (symbol, 0, size);
433 PUT_CODE (symbol, SYMBOL_REF);
434 PUT_MODE (symbol, Pmode);
435 XSTR (symbol, 0) = label;
436 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
438 /* Initialize the block_symbol stuff. */
439 SYMBOL_REF_BLOCK (symbol) = block;
440 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
442 return symbol;
445 /* Return a section with a particular name and with whatever SECTION_*
446 flags section_type_flags deems appropriate. The name of the section
447 is taken from NAME if nonnull, otherwise it is taken from DECL's
448 DECL_SECTION_NAME. DECL is the decl associated with the section
449 (see the section comment for details) and RELOC is as for
450 section_type_flags. */
452 section *
453 get_named_section (tree decl, const char *name, int reloc)
455 unsigned int flags;
457 if (name == NULL)
459 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
460 name = DECL_SECTION_NAME (decl);
463 flags = targetm.section_type_flags (decl, name, reloc);
464 return get_section (name, flags, decl);
467 /* Worker for resolve_unique_section. */
469 static bool
470 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
472 n->implicit_section = true;
473 return false;
476 /* If required, set DECL_SECTION_NAME to a unique name. */
478 void
479 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
480 int flag_function_or_data_sections)
482 if (DECL_SECTION_NAME (decl) == NULL
483 && targetm_common.have_named_sections
484 && (flag_function_or_data_sections
485 || lookup_attribute ("retain", DECL_ATTRIBUTES (decl))
486 || DECL_COMDAT_GROUP (decl)))
488 targetm.asm_out.unique_section (decl, reloc);
489 if (DECL_SECTION_NAME (decl))
490 symtab_node::get (decl)->call_for_symbol_and_aliases
491 (set_implicit_section, NULL, true);
495 #ifdef BSS_SECTION_ASM_OP
497 #ifdef ASM_OUTPUT_ALIGNED_BSS
499 /* Utility function for targets to use in implementing
500 ASM_OUTPUT_ALIGNED_BSS.
501 ??? It is believed that this function will work in most cases so such
502 support is localized here. */
504 static void
505 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
506 const char *name, unsigned HOST_WIDE_INT size,
507 int align)
509 switch_to_section (bss_section);
510 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
511 #ifdef ASM_DECLARE_OBJECT_NAME
512 last_assemble_variable_decl = decl;
513 ASM_DECLARE_OBJECT_NAME (file, name, decl);
514 #else
515 /* Standard thing is just output label for the object. */
516 ASM_OUTPUT_LABEL (file, name);
517 #endif /* ASM_DECLARE_OBJECT_NAME */
518 ASM_OUTPUT_SKIP (file, size ? size : 1);
521 #endif
523 #endif /* BSS_SECTION_ASM_OP */
525 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
526 /* Return the hot section for function DECL. Return text_section for
527 null DECLs. */
529 static section *
530 hot_function_section (tree decl)
532 if (decl != NULL_TREE
533 && DECL_SECTION_NAME (decl) != NULL
534 && targetm_common.have_named_sections)
535 return get_named_section (decl, NULL, 0);
536 else
537 return text_section;
539 #endif
541 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
542 is NULL.
544 When DECL_SECTION_NAME is non-NULL and it is implicit section and
545 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
546 concatenate the name with NAMED_SECTION_SUFFIX.
547 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
549 section *
550 get_named_text_section (tree decl,
551 const char *text_section_name,
552 const char *named_section_suffix)
554 if (decl && DECL_SECTION_NAME (decl))
556 if (named_section_suffix)
558 const char *dsn = DECL_SECTION_NAME (decl);
559 const char *stripped_name;
560 char *name, *buffer;
562 name = (char *) alloca (strlen (dsn) + 1);
563 memcpy (name, dsn,
564 strlen (dsn) + 1);
566 stripped_name = targetm.strip_name_encoding (name);
568 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
569 return get_named_section (decl, buffer, 0);
571 else if (symtab_node::get (decl)->implicit_section)
573 const char *name;
575 /* Do not try to split gnu_linkonce functions. This gets somewhat
576 slipperly. */
577 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
578 return NULL;
579 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
580 name = targetm.strip_name_encoding (name);
581 return get_named_section (decl, ACONCAT ((text_section_name, ".",
582 name, NULL)), 0);
584 else
585 return NULL;
587 return get_named_section (decl, text_section_name, 0);
590 /* Choose named function section based on its frequency. */
592 section *
593 default_function_section (tree decl, enum node_frequency freq,
594 bool startup, bool exit)
596 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
597 /* Old GNU linkers have buggy --gc-section support, which sometimes
598 results in .gcc_except_table* sections being garbage collected. */
599 if (decl
600 && symtab_node::get (decl)->implicit_section)
601 return NULL;
602 #endif
604 if (!flag_reorder_functions
605 || !targetm_common.have_named_sections)
606 return NULL;
607 /* Startup code should go to startup subsection unless it is
608 unlikely executed (this happens especially with function splitting
609 where we can split away unnecessary parts of static constructors. */
610 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
612 /* During LTO the tp_first_run profiling will naturally place all
613 initialization code first. Using separate section is counter-productive
614 because startup only code may call functions which are no longer
615 startup only. */
616 if (!in_lto_p
617 || !cgraph_node::get (decl)->tp_first_run
618 || !opt_for_fn (decl, flag_profile_reorder_functions))
619 return get_named_text_section (decl, ".text.startup", NULL);
620 else
621 return NULL;
624 /* Similarly for exit. */
625 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
626 return get_named_text_section (decl, ".text.exit", NULL);
628 /* Group cold functions together, similarly for hot code. */
629 switch (freq)
631 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
632 return get_named_text_section (decl, ".text.unlikely", NULL);
633 case NODE_FREQUENCY_HOT:
634 return get_named_text_section (decl, ".text.hot", NULL);
635 /* FALLTHRU */
636 default:
637 return NULL;
641 /* Return the section for function DECL.
643 If DECL is NULL_TREE, return the text section. We can be passed
644 NULL_TREE under some circumstances by dbxout.cc at least.
646 If FORCE_COLD is true, return cold function section ignoring
647 the frequency info of cgraph_node. */
649 static section *
650 function_section_1 (tree decl, bool force_cold)
652 section *section = NULL;
653 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
654 bool startup = false, exit = false;
656 if (decl)
658 struct cgraph_node *node = cgraph_node::get (decl);
660 if (node)
662 freq = node->frequency;
663 startup = node->only_called_at_startup;
664 exit = node->only_called_at_exit;
667 if (force_cold)
668 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
670 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
671 if (decl != NULL_TREE
672 && DECL_SECTION_NAME (decl) != NULL)
674 if (targetm.asm_out.function_section)
675 section = targetm.asm_out.function_section (decl, freq,
676 startup, exit);
677 if (section)
678 return section;
679 return get_named_section (decl, NULL, 0);
681 else
682 return targetm.asm_out.select_section
683 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
684 symtab_node::get (decl)->definition_alignment ());
685 #else
686 if (targetm.asm_out.function_section)
687 section = targetm.asm_out.function_section (decl, freq, startup, exit);
688 if (section)
689 return section;
690 return hot_function_section (decl);
691 #endif
694 /* Return the section for function DECL.
696 If DECL is NULL_TREE, return the text section. We can be passed
697 NULL_TREE under some circumstances by dbxout.cc at least. */
699 section *
700 function_section (tree decl)
702 /* Handle cases where function splitting code decides
703 to put function entry point into unlikely executed section
704 despite the fact that the function itself is not cold
705 (i.e. it is called rarely but contains a hot loop that is
706 better to live in hot subsection for the code locality). */
707 return function_section_1 (decl,
708 first_function_block_is_cold);
711 /* Return the section for the current function, take IN_COLD_SECTION_P
712 into account. */
714 section *
715 current_function_section (void)
717 return function_section_1 (current_function_decl, in_cold_section_p);
720 /* Tell assembler to switch to unlikely-to-be-executed text section. */
722 section *
723 unlikely_text_section (void)
725 return function_section_1 (current_function_decl, true);
728 /* When called within a function context, return true if the function
729 has been assigned a cold text section and if SECT is that section.
730 When called outside a function context, return true if SECT is the
731 default cold section. */
733 bool
734 unlikely_text_section_p (section *sect)
736 return sect == function_section_1 (current_function_decl, true);
739 /* Switch to the other function partition (if inside of hot section
740 into cold section, otherwise into the hot section). */
742 void
743 switch_to_other_text_partition (void)
745 in_cold_section_p = !in_cold_section_p;
746 switch_to_section (current_function_section ());
749 /* Return the read-only or relocated read-only data section
750 associated with function DECL. */
752 section *
753 default_function_rodata_section (tree decl, bool relocatable)
755 const char* sname;
756 unsigned int flags;
758 flags = 0;
760 if (relocatable)
762 sname = ".data.rel.ro.local";
763 flags = (SECTION_WRITE | SECTION_RELRO);
765 else
766 sname = ".rodata";
768 if (decl && DECL_SECTION_NAME (decl))
770 const char *name = DECL_SECTION_NAME (decl);
772 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
774 const char *dot;
775 size_t len;
776 char* rname;
778 dot = strchr (name + 1, '.');
779 if (!dot)
780 dot = name;
781 len = strlen (dot) + strlen (sname) + 1;
782 rname = (char *) alloca (len);
784 strcpy (rname, sname);
785 strcat (rname, dot);
786 return get_section (rname, (SECTION_LINKONCE | flags), decl);
788 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo or
789 .gnu.linkonce.d.rel.ro.local.foo if the jump table is relocatable. */
790 else if (DECL_COMDAT_GROUP (decl)
791 && startswith (name, ".gnu.linkonce.t."))
793 size_t len;
794 char *rname;
796 if (relocatable)
798 len = strlen (name) + strlen (".rel.ro.local") + 1;
799 rname = (char *) alloca (len);
801 strcpy (rname, ".gnu.linkonce.d.rel.ro.local");
802 strcat (rname, name + 15);
804 else
806 len = strlen (name) + 1;
807 rname = (char *) alloca (len);
809 memcpy (rname, name, len);
810 rname[14] = 'r';
812 return get_section (rname, (SECTION_LINKONCE | flags), decl);
814 /* For .text.foo we want to use .rodata.foo. */
815 else if (flag_function_sections && flag_data_sections
816 && startswith (name, ".text."))
818 size_t len = strlen (name) + 1;
819 char *rname = (char *) alloca (len + strlen (sname) - 5);
821 memcpy (rname, sname, strlen (sname));
822 memcpy (rname + strlen (sname), name + 5, len - 5);
823 return get_section (rname, flags, decl);
827 if (relocatable)
828 return get_section (sname, flags, decl);
829 else
830 return readonly_data_section;
833 /* Return the read-only data section associated with function DECL
834 for targets where that section should be always the single
835 readonly data section. */
837 section *
838 default_no_function_rodata_section (tree, bool)
840 return readonly_data_section;
843 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
845 static const char *
846 function_mergeable_rodata_prefix (void)
848 section *s = targetm.asm_out.function_rodata_section (current_function_decl,
849 false);
850 if (SECTION_STYLE (s) == SECTION_NAMED)
851 return s->named.name;
852 else
853 return targetm.asm_out.mergeable_rodata_prefix;
856 /* Return the section to use for string merging. */
858 static section *
859 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
860 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
861 unsigned int flags ATTRIBUTE_UNUSED)
863 HOST_WIDE_INT len;
865 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
866 && TREE_CODE (decl) == STRING_CST
867 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
868 && align <= 256
869 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
870 && TREE_STRING_LENGTH (decl) == len)
872 scalar_int_mode mode;
873 unsigned int modesize;
874 const char *str;
875 HOST_WIDE_INT i;
876 int j, unit;
877 const char *prefix = function_mergeable_rodata_prefix ();
878 char *name = (char *) alloca (strlen (prefix) + 30);
880 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
881 modesize = GET_MODE_BITSIZE (mode);
882 if (modesize >= 8 && modesize <= 256
883 && (modesize & (modesize - 1)) == 0)
885 if (align < modesize)
886 align = modesize;
888 if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
889 return readonly_data_section;
891 str = TREE_STRING_POINTER (decl);
892 unit = GET_MODE_SIZE (mode);
894 /* Check for embedded NUL characters. */
895 for (i = 0; i < len; i += unit)
897 for (j = 0; j < unit; j++)
898 if (str[i + j] != '\0')
899 break;
900 if (j == unit)
901 break;
903 if (i == len - unit || (unit == 1 && i == len))
905 sprintf (name, "%s.str%d.%d", prefix,
906 modesize / 8, (int) (align / 8));
907 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
908 return get_section (name, flags, NULL);
913 return readonly_data_section;
916 /* Return the section to use for constant merging. */
918 section *
919 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
920 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
921 unsigned int flags ATTRIBUTE_UNUSED)
923 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
924 && mode != VOIDmode
925 && mode != BLKmode
926 && known_le (GET_MODE_BITSIZE (mode), align)
927 && align >= 8
928 && align <= 256
929 && (align & (align - 1)) == 0
930 && (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
932 const char *prefix = function_mergeable_rodata_prefix ();
933 char *name = (char *) alloca (strlen (prefix) + 30);
935 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
936 flags |= (align / 8) | SECTION_MERGE;
937 return get_section (name, flags, NULL);
939 return readonly_data_section;
942 /* Given NAME, a putative register name, discard any customary prefixes. */
944 static const char *
945 strip_reg_name (const char *name)
947 #ifdef REGISTER_PREFIX
948 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
949 name += strlen (REGISTER_PREFIX);
950 #endif
951 if (name[0] == '%' || name[0] == '#')
952 name++;
953 return name;
956 /* The user has asked for a DECL to have a particular name. Set (or
957 change) it in such a way that we don't prefix an underscore to
958 it. */
959 void
960 set_user_assembler_name (tree decl, const char *name)
962 char *starred = (char *) alloca (strlen (name) + 2);
963 starred[0] = '*';
964 strcpy (starred + 1, name);
965 symtab->change_decl_assembler_name (decl, get_identifier (starred));
966 SET_DECL_RTL (decl, NULL_RTX);
969 /* Decode an `asm' spec for a declaration as a register name.
970 Return the register number, or -1 if nothing specified,
971 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
972 or -3 if ASMSPEC is `cc' and is not recognized,
973 or -4 if ASMSPEC is `memory' and is not recognized.
974 Accept an exact spelling or a decimal number.
975 Prefixes such as % are optional. */
978 decode_reg_name_and_count (const char *asmspec, int *pnregs)
980 /* Presume just one register is clobbered. */
981 *pnregs = 1;
983 if (asmspec != 0)
985 int i;
987 /* Get rid of confusing prefixes. */
988 asmspec = strip_reg_name (asmspec);
990 /* Allow a decimal number as a "register name". */
991 for (i = strlen (asmspec) - 1; i >= 0; i--)
992 if (! ISDIGIT (asmspec[i]))
993 break;
994 if (asmspec[0] != 0 && i < 0)
996 i = atoi (asmspec);
997 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
998 return i;
999 else
1000 return -2;
1003 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1004 if (reg_names[i][0]
1005 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
1006 return i;
1008 #ifdef OVERLAPPING_REGISTER_NAMES
1010 static const struct
1012 const char *const name;
1013 const int number;
1014 const int nregs;
1015 } table[] = OVERLAPPING_REGISTER_NAMES;
1017 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1018 if (table[i].name[0]
1019 && ! strcmp (asmspec, table[i].name))
1021 *pnregs = table[i].nregs;
1022 return table[i].number;
1025 #endif /* OVERLAPPING_REGISTER_NAMES */
1027 #ifdef ADDITIONAL_REGISTER_NAMES
1029 static const struct { const char *const name; const int number; } table[]
1030 = ADDITIONAL_REGISTER_NAMES;
1032 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1033 if (table[i].name[0]
1034 && ! strcmp (asmspec, table[i].name)
1035 && reg_names[table[i].number][0])
1036 return table[i].number;
1038 #endif /* ADDITIONAL_REGISTER_NAMES */
1040 if (!strcmp (asmspec, "memory"))
1041 return -4;
1043 if (!strcmp (asmspec, "cc"))
1044 return -3;
1046 return -2;
1049 return -1;
1053 decode_reg_name (const char *name)
1055 int count;
1056 return decode_reg_name_and_count (name, &count);
1060 /* Return true if DECL's initializer is suitable for a BSS section. */
1062 bool
1063 bss_initializer_p (const_tree decl, bool named)
1065 /* Do not put non-common constants into the .bss section, they belong in
1066 a readonly section, except when NAMED is true. */
1067 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1068 && (DECL_INITIAL (decl) == NULL
1069 /* In LTO we have no errors in program; error_mark_node is used
1070 to mark offlined constructors. */
1071 || (DECL_INITIAL (decl) == error_mark_node
1072 && !in_lto_p)
1073 || (flag_zero_initialized_in_bss
1074 && initializer_zerop (DECL_INITIAL (decl))
1075 /* A decl with the "persistent" attribute applied and
1076 explicitly initialized to 0 should not be treated as a BSS
1077 variable. */
1078 && !DECL_PERSISTENT_P (decl))));
1081 /* Compute the alignment of variable specified by DECL.
1082 DONT_OUTPUT_DATA is from assemble_variable. */
1084 void
1085 align_variable (tree decl, bool dont_output_data)
1087 unsigned int align = DECL_ALIGN (decl);
1089 /* In the case for initialing an array whose length isn't specified,
1090 where we have not yet been able to do the layout,
1091 figure out the proper alignment now. */
1092 if (dont_output_data && DECL_SIZE (decl) == 0
1093 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1094 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1096 /* Some object file formats have a maximum alignment which they support.
1097 In particular, a.out format supports a maximum alignment of 4. */
1098 if (align > MAX_OFILE_ALIGNMENT)
1100 error ("alignment of %q+D is greater than maximum object "
1101 "file alignment %d", decl,
1102 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1103 align = MAX_OFILE_ALIGNMENT;
1106 if (! DECL_USER_ALIGN (decl))
1108 #ifdef DATA_ABI_ALIGNMENT
1109 unsigned int data_abi_align
1110 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1111 /* For backwards compatibility, don't assume the ABI alignment for
1112 TLS variables. */
1113 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1114 align = data_abi_align;
1115 #endif
1117 /* On some machines, it is good to increase alignment sometimes.
1118 But as DECL_ALIGN is used both for actually emitting the variable
1119 and for code accessing the variable as guaranteed alignment, we
1120 can only increase the alignment if it is a performance optimization
1121 if the references to it must bind to the current definition. */
1122 if (decl_binds_to_current_def_p (decl)
1123 && !DECL_VIRTUAL_P (decl))
1125 #ifdef DATA_ALIGNMENT
1126 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1127 /* Don't increase alignment too much for TLS variables - TLS space
1128 is too precious. */
1129 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1130 align = data_align;
1131 #endif
1132 if (DECL_INITIAL (decl) != 0
1133 /* In LTO we have no errors in program; error_mark_node is used
1134 to mark offlined constructors. */
1135 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1137 unsigned int const_align
1138 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1139 /* Don't increase alignment too much for TLS variables - TLS
1140 space is too precious. */
1141 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1142 align = const_align;
1147 /* Reset the alignment in case we have made it tighter, so we can benefit
1148 from it in get_pointer_alignment. */
1149 SET_DECL_ALIGN (decl, align);
1152 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1153 beyond what align_variable returned. */
1155 static unsigned int
1156 get_variable_align (tree decl)
1158 unsigned int align = DECL_ALIGN (decl);
1160 /* For user aligned vars or static vars align_variable already did
1161 everything. */
1162 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1163 return align;
1165 #ifdef DATA_ABI_ALIGNMENT
1166 if (DECL_THREAD_LOCAL_P (decl))
1167 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1168 #endif
1170 /* For decls that bind to the current definition, align_variable
1171 did also everything, except for not assuming ABI required alignment
1172 of TLS variables. For other vars, increase the alignment here
1173 as an optimization. */
1174 if (!decl_binds_to_current_def_p (decl))
1176 /* On some machines, it is good to increase alignment sometimes. */
1177 #ifdef DATA_ALIGNMENT
1178 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1179 /* Don't increase alignment too much for TLS variables - TLS space
1180 is too precious. */
1181 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1182 align = data_align;
1183 #endif
1184 if (DECL_INITIAL (decl) != 0
1185 /* In LTO we have no errors in program; error_mark_node is used
1186 to mark offlined constructors. */
1187 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1189 unsigned int const_align
1190 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1191 /* Don't increase alignment too much for TLS variables - TLS space
1192 is too precious. */
1193 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1194 align = const_align;
1198 return align;
1201 /* Compute reloc for get_variable_section. The return value
1202 is a mask for which bit 1 indicates a global relocation, and bit 0
1203 indicates a local relocation. */
1206 compute_reloc_for_var (tree decl)
1208 int reloc;
1210 if (DECL_INITIAL (decl) == error_mark_node)
1211 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1212 else if (DECL_INITIAL (decl))
1213 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1214 else
1215 reloc = 0;
1217 return reloc;
1220 /* Return the section into which the given VAR_DECL or CONST_DECL
1221 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1222 section should be used wherever possible. */
1224 section *
1225 get_variable_section (tree decl, bool prefer_noswitch_p)
1227 addr_space_t as = ADDR_SPACE_GENERIC;
1228 int reloc;
1229 varpool_node *vnode = varpool_node::get (decl);
1230 if (vnode)
1232 vnode = vnode->ultimate_alias_target ();
1233 decl = vnode->decl;
1236 if (TREE_TYPE (decl) != error_mark_node)
1237 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1239 /* We need the constructor to figure out reloc flag. */
1240 if (vnode)
1241 vnode->get_constructor ();
1243 if (DECL_COMMON (decl)
1244 && !lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1246 /* If the decl has been given an explicit section name, or it resides
1247 in a non-generic address space, then it isn't common, and shouldn't
1248 be handled as such. */
1249 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1250 && ADDR_SPACE_GENERIC_P (as));
1251 if (DECL_THREAD_LOCAL_P (decl))
1252 return tls_comm_section;
1253 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1254 return comm_section;
1257 reloc = compute_reloc_for_var (decl);
1259 resolve_unique_section (decl, reloc, flag_data_sections);
1260 if (IN_NAMED_SECTION (decl))
1262 section *sect = get_named_section (decl, NULL, reloc);
1264 if ((sect->common.flags & SECTION_BSS)
1265 && !bss_initializer_p (decl, true))
1267 error_at (DECL_SOURCE_LOCATION (decl),
1268 "only zero initializers are allowed in section %qs",
1269 sect->named.name);
1270 DECL_INITIAL (decl) = error_mark_node;
1272 return sect;
1275 if (ADDR_SPACE_GENERIC_P (as)
1276 && !DECL_THREAD_LOCAL_P (decl)
1277 && !DECL_NOINIT_P (decl)
1278 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1279 && bss_initializer_p (decl))
1281 if (!TREE_PUBLIC (decl)
1282 && !((flag_sanitize & SANITIZE_ADDRESS)
1283 && asan_protect_global (decl)))
1284 return lcomm_section;
1285 if (bss_noswitch_section)
1286 return bss_noswitch_section;
1289 return targetm.asm_out.select_section (decl, reloc,
1290 get_variable_align (decl));
1293 /* Return the block into which object_block DECL should be placed. */
1295 static struct object_block *
1296 get_block_for_decl (tree decl)
1298 section *sect;
1300 if (VAR_P (decl))
1302 /* The object must be defined in this translation unit. */
1303 if (DECL_EXTERNAL (decl))
1304 return NULL;
1306 /* There's no point using object blocks for something that is
1307 isolated by definition. */
1308 if (DECL_COMDAT_GROUP (decl))
1309 return NULL;
1312 /* We can only calculate block offsets if the decl has a known
1313 constant size. */
1314 if (DECL_SIZE_UNIT (decl) == NULL)
1315 return NULL;
1316 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1317 return NULL;
1319 /* Find out which section should contain DECL. We cannot put it into
1320 an object block if it requires a standalone definition. */
1321 if (VAR_P (decl))
1322 align_variable (decl, 0);
1323 sect = get_variable_section (decl, true);
1324 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1325 return NULL;
1327 if (bool (lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1328 != bool (sect->common.flags & SECTION_RETAIN))
1329 return NULL;
1331 return get_block_for_section (sect);
1334 /* Make sure block symbol SYMBOL is in block BLOCK. */
1336 static void
1337 change_symbol_block (rtx symbol, struct object_block *block)
1339 if (block != SYMBOL_REF_BLOCK (symbol))
1341 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1342 SYMBOL_REF_BLOCK (symbol) = block;
1346 /* Return true if it is possible to put DECL in an object_block. */
1348 static bool
1349 use_blocks_for_decl_p (tree decl)
1351 struct symtab_node *snode;
1353 /* Don't create object blocks if each DECL is placed into a separate
1354 section because that will uselessly create a section anchor for
1355 each DECL. */
1356 if (flag_data_sections)
1357 return false;
1359 /* Only data DECLs can be placed into object blocks. */
1360 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1361 return false;
1363 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1364 are never used from code directly and we never want object block handling
1365 for those. */
1366 if (DECL_INITIAL (decl) == decl)
1367 return false;
1369 /* If this decl is an alias, then we don't want to emit a
1370 definition. */
1371 if (VAR_P (decl)
1372 && (snode = symtab_node::get (decl)) != NULL
1373 && snode->alias)
1374 return false;
1376 return targetm.use_blocks_for_decl_p (decl);
1379 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1380 until we find an identifier that is not itself a transparent alias.
1381 Modify the alias passed to it by reference (and all aliases on the
1382 way to the ultimate target), such that they do not have to be
1383 followed again, and return the ultimate target of the alias
1384 chain. */
1386 static inline tree
1387 ultimate_transparent_alias_target (tree *alias)
1389 tree target = *alias;
1391 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1393 gcc_assert (TREE_CHAIN (target));
1394 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1395 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1396 && ! TREE_CHAIN (target));
1397 *alias = target;
1400 return target;
1403 /* Return true if REGNUM is mentioned in ELIMINABLE_REGS as a from
1404 register number. */
1406 static bool
1407 eliminable_regno_p (int regnum)
1409 static const struct
1411 const int from;
1412 const int to;
1413 } eliminables[] = ELIMINABLE_REGS;
1414 for (size_t i = 0; i < ARRAY_SIZE (eliminables); i++)
1415 if (regnum == eliminables[i].from)
1416 return true;
1417 return false;
1420 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1421 have static storage duration. In other words, it should not be an
1422 automatic variable, including PARM_DECLs.
1424 There is, however, one exception: this function handles variables
1425 explicitly placed in a particular register by the user.
1427 This is never called for PARM_DECL nodes. */
1429 void
1430 make_decl_rtl (tree decl)
1432 const char *name = 0;
1433 int reg_number;
1434 tree id;
1435 rtx x;
1437 /* Check that we are not being given an automatic variable. */
1438 gcc_assert (TREE_CODE (decl) != PARM_DECL
1439 && TREE_CODE (decl) != RESULT_DECL);
1441 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1442 gcc_assert (!VAR_P (decl)
1443 || TREE_STATIC (decl)
1444 || TREE_PUBLIC (decl)
1445 || DECL_EXTERNAL (decl)
1446 || DECL_REGISTER (decl));
1448 /* And that we were not given a type or a label. */
1449 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1450 && TREE_CODE (decl) != LABEL_DECL);
1452 /* For a duplicate declaration, we can be called twice on the
1453 same DECL node. Don't discard the RTL already made. */
1454 if (DECL_RTL_SET_P (decl))
1456 /* If the old RTL had the wrong mode, fix the mode. */
1457 x = DECL_RTL (decl);
1458 if (GET_MODE (x) != DECL_MODE (decl))
1459 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1461 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1462 return;
1464 /* ??? Another way to do this would be to maintain a hashed
1465 table of such critters. Instead of adding stuff to a DECL
1466 to give certain attributes to it, we could use an external
1467 hash map from DECL to set of attributes. */
1469 /* Let the target reassign the RTL if it wants.
1470 This is necessary, for example, when one machine specific
1471 decl attribute overrides another. */
1472 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1474 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1475 on the new decl information. */
1476 if (MEM_P (x)
1477 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1478 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1479 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1481 return;
1484 /* If this variable belongs to the global constant pool, retrieve the
1485 pre-computed RTL or recompute it in LTO mode. */
1486 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1488 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1489 return;
1492 id = DECL_ASSEMBLER_NAME (decl);
1493 name = IDENTIFIER_POINTER (id);
1495 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1496 && DECL_REGISTER (decl))
1498 error ("register name not specified for %q+D", decl);
1500 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1502 const char *asmspec = name+1;
1503 machine_mode mode = DECL_MODE (decl);
1504 reg_number = decode_reg_name (asmspec);
1505 /* First detect errors in declaring global registers. */
1506 if (reg_number == -1)
1507 error ("register name not specified for %q+D", decl);
1508 else if (reg_number < 0)
1509 error ("invalid register name for %q+D", decl);
1510 else if (mode == BLKmode)
1511 error ("data type of %q+D isn%'t suitable for a register",
1512 decl);
1513 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1514 error ("the register specified for %q+D cannot be accessed"
1515 " by the current target", decl);
1516 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1517 error ("the register specified for %q+D is not general enough"
1518 " to be used as a register variable", decl);
1519 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1520 error ("register specified for %q+D isn%'t suitable for data type",
1521 decl);
1522 else if (reg_number != HARD_FRAME_POINTER_REGNUM
1523 && (reg_number == FRAME_POINTER_REGNUM
1524 #ifdef RETURN_ADDRESS_POINTER_REGNUM
1525 || reg_number == RETURN_ADDRESS_POINTER_REGNUM
1526 #endif
1527 || reg_number == ARG_POINTER_REGNUM)
1528 && eliminable_regno_p (reg_number))
1529 error ("register specified for %q+D is an internal GCC "
1530 "implementation detail", decl);
1531 /* Now handle properly declared static register variables. */
1532 else
1534 int nregs;
1536 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1538 DECL_INITIAL (decl) = 0;
1539 error ("global register variable has initial value");
1541 if (TREE_THIS_VOLATILE (decl))
1542 warning (OPT_Wvolatile_register_var,
1543 "optimization may eliminate reads and/or "
1544 "writes to register variables");
1546 /* If the user specified one of the eliminables registers here,
1547 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1548 confused with that register and be eliminated. This usage is
1549 somewhat suspect... */
1551 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1552 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1553 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1555 if (TREE_STATIC (decl))
1557 /* Make this register global, so not usable for anything
1558 else. */
1559 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1560 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1561 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1562 #endif
1563 nregs = hard_regno_nregs (reg_number, mode);
1564 while (nregs > 0)
1565 globalize_reg (decl, reg_number + --nregs);
1568 /* As a register variable, it has no section. */
1569 return;
1571 /* Avoid internal errors from invalid register
1572 specifications. */
1573 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1574 DECL_HARD_REGISTER (decl) = 0;
1575 /* Also avoid SSA inconsistencies by pretending this is an external
1576 decl now. */
1577 DECL_EXTERNAL (decl) = 1;
1578 return;
1580 /* Now handle ordinary static variables and functions (in memory).
1581 Also handle vars declared register invalidly. */
1582 else if (name[0] == '*')
1584 #ifdef REGISTER_PREFIX
1585 if (strlen (REGISTER_PREFIX) != 0)
1587 reg_number = decode_reg_name (name);
1588 if (reg_number >= 0 || reg_number == -3)
1589 error ("register name given for non-register variable %q+D", decl);
1591 #endif
1594 /* Specifying a section attribute on a variable forces it into a
1595 non-.bss section, and thus it cannot be common. */
1596 /* FIXME: In general this code should not be necessary because
1597 visibility pass is doing the same work. But notice_global_symbol
1598 is called early and it needs to make DECL_RTL to get the name.
1599 we take care of recomputing the DECL_RTL after visibility is changed. */
1600 if (VAR_P (decl)
1601 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1602 && DECL_SECTION_NAME (decl) != NULL
1603 && DECL_INITIAL (decl) == NULL_TREE
1604 && DECL_COMMON (decl))
1605 DECL_COMMON (decl) = 0;
1607 /* Variables can't be both common and weak. */
1608 if (VAR_P (decl) && DECL_WEAK (decl))
1609 DECL_COMMON (decl) = 0;
1611 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1612 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1613 else
1615 machine_mode address_mode = Pmode;
1616 if (TREE_TYPE (decl) != error_mark_node)
1618 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1619 address_mode = targetm.addr_space.address_mode (as);
1621 x = gen_rtx_SYMBOL_REF (address_mode, name);
1623 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1624 SET_SYMBOL_REF_DECL (x, decl);
1626 x = gen_rtx_MEM (DECL_MODE (decl), x);
1627 if (TREE_CODE (decl) != FUNCTION_DECL)
1628 set_mem_attributes (x, decl, 1);
1629 SET_DECL_RTL (decl, x);
1631 /* Optionally set flags or add text to the name to record information
1632 such as that it is a function name.
1633 If the name is changed, the macro ASM_OUTPUT_LABELREF
1634 will have to know how to strip this information. */
1635 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1638 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1639 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1640 rtl. */
1643 make_decl_rtl_for_debug (tree decl)
1645 unsigned int save_aliasing_flag;
1646 rtx rtl;
1648 if (DECL_RTL_SET_P (decl))
1649 return DECL_RTL (decl);
1651 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1652 call new_alias_set. If running with -fcompare-debug, sometimes
1653 we do not want to create alias sets that will throw the alias
1654 numbers off in the comparison dumps. So... clearing
1655 flag_strict_aliasing will keep new_alias_set() from creating a
1656 new set. */
1657 save_aliasing_flag = flag_strict_aliasing;
1658 flag_strict_aliasing = 0;
1660 rtl = DECL_RTL (decl);
1661 /* Reset DECL_RTL back, as various parts of the compiler expects
1662 DECL_RTL set meaning it is actually going to be output. */
1663 SET_DECL_RTL (decl, NULL);
1665 flag_strict_aliasing = save_aliasing_flag;
1666 return rtl;
1669 /* Output a string of literal assembler code
1670 for an `asm' keyword used between functions. */
1672 void
1673 assemble_asm (tree string)
1675 const char *p;
1676 app_enable ();
1678 if (TREE_CODE (string) == ADDR_EXPR)
1679 string = TREE_OPERAND (string, 0);
1681 p = TREE_STRING_POINTER (string);
1682 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1685 /* Write the address of the entity given by SYMBOL to SEC. */
1686 void
1687 assemble_addr_to_section (rtx symbol, section *sec)
1689 switch_to_section (sec);
1690 assemble_align (POINTER_SIZE);
1691 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1694 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1695 not) section for PRIORITY. */
1696 section *
1697 get_cdtor_priority_section (int priority, bool constructor_p)
1699 /* Buffer conservatively large enough for the full range of a 32-bit
1700 int plus the text below. */
1701 char buf[18];
1703 /* ??? This only works reliably with the GNU linker. */
1704 sprintf (buf, "%s.%.5u",
1705 constructor_p ? ".ctors" : ".dtors",
1706 /* Invert the numbering so the linker puts us in the proper
1707 order; constructors are run from right to left, and the
1708 linker sorts in increasing order. */
1709 MAX_INIT_PRIORITY - priority);
1710 return get_section (buf, SECTION_WRITE, NULL);
1713 void
1714 default_named_section_asm_out_destructor (rtx symbol, int priority)
1716 section *sec;
1718 if (priority != DEFAULT_INIT_PRIORITY)
1719 sec = get_cdtor_priority_section (priority,
1720 /*constructor_p=*/false);
1721 else
1722 sec = get_section (".dtors", SECTION_WRITE, NULL);
1724 assemble_addr_to_section (symbol, sec);
1727 #ifdef DTORS_SECTION_ASM_OP
1728 void
1729 default_dtor_section_asm_out_destructor (rtx symbol,
1730 int priority ATTRIBUTE_UNUSED)
1732 assemble_addr_to_section (symbol, dtors_section);
1734 #endif
1736 void
1737 default_named_section_asm_out_constructor (rtx symbol, int priority)
1739 section *sec;
1741 if (priority != DEFAULT_INIT_PRIORITY)
1742 sec = get_cdtor_priority_section (priority,
1743 /*constructor_p=*/true);
1744 else
1745 sec = get_section (".ctors", SECTION_WRITE, NULL);
1747 assemble_addr_to_section (symbol, sec);
1750 #ifdef CTORS_SECTION_ASM_OP
1751 void
1752 default_ctor_section_asm_out_constructor (rtx symbol,
1753 int priority ATTRIBUTE_UNUSED)
1755 assemble_addr_to_section (symbol, ctors_section);
1757 #endif
1759 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1760 a nonzero value if the constant pool should be output before the
1761 start of the function, or a zero value if the pool should output
1762 after the end of the function. The default is to put it before the
1763 start. */
1765 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1766 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1767 #endif
1769 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1770 to be output to assembler.
1771 Set first_global_object_name and weak_global_object_name as appropriate. */
1773 void
1774 notice_global_symbol (tree decl)
1776 const char **t = &first_global_object_name;
1778 if (first_global_object_name
1779 || !TREE_PUBLIC (decl)
1780 || DECL_EXTERNAL (decl)
1781 || !DECL_NAME (decl)
1782 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1783 || (TREE_CODE (decl) != FUNCTION_DECL
1784 && (!VAR_P (decl)
1785 || (DECL_COMMON (decl)
1786 && (DECL_INITIAL (decl) == 0
1787 || DECL_INITIAL (decl) == error_mark_node)))))
1788 return;
1790 /* We win when global object is found, but it is useful to know about weak
1791 symbol as well so we can produce nicer unique names. */
1792 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1793 t = &weak_global_object_name;
1795 if (!*t)
1797 tree id = DECL_ASSEMBLER_NAME (decl);
1798 ultimate_transparent_alias_target (&id);
1799 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1803 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1804 current function goes into the cold section, so that targets can use
1805 current_function_section during RTL expansion. DECL describes the
1806 function. */
1808 void
1809 decide_function_section (tree decl)
1811 first_function_block_is_cold = false;
1813 if (DECL_SECTION_NAME (decl))
1815 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1816 /* Calls to function_section rely on first_function_block_is_cold
1817 being accurate. */
1818 first_function_block_is_cold = (node
1819 && node->frequency
1820 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1823 in_cold_section_p = first_function_block_is_cold;
1826 /* Get the function's name, as described by its RTL. This may be
1827 different from the DECL_NAME name used in the source file. */
1828 const char *
1829 get_fnname_from_decl (tree decl)
1831 rtx x = DECL_RTL (decl);
1832 gcc_assert (MEM_P (x));
1833 x = XEXP (x, 0);
1834 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1835 return XSTR (x, 0);
1838 /* Output assembler code for the constant pool of a function and associated
1839 with defining the name of the function. DECL describes the function.
1840 NAME is the function's name. For the constant pool, we use the current
1841 constant pool data. */
1843 void
1844 assemble_start_function (tree decl, const char *fnname)
1846 int align;
1847 char tmp_label[100];
1848 bool hot_label_written = false;
1850 if (crtl->has_bb_partition)
1852 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1853 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1854 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1855 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1856 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1857 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1858 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1859 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1860 const_labelno++;
1861 cold_function_name = NULL_TREE;
1863 else
1865 crtl->subsections.hot_section_label = NULL;
1866 crtl->subsections.cold_section_label = NULL;
1867 crtl->subsections.hot_section_end_label = NULL;
1868 crtl->subsections.cold_section_end_label = NULL;
1871 /* The following code does not need preprocessing in the assembler. */
1873 app_disable ();
1875 if (CONSTANT_POOL_BEFORE_FUNCTION)
1876 output_constant_pool (fnname, decl);
1878 align = symtab_node::get (decl)->definition_alignment ();
1880 /* Make sure the not and cold text (code) sections are properly
1881 aligned. This is necessary here in the case where the function
1882 has both hot and cold sections, because we don't want to re-set
1883 the alignment when the section switch happens mid-function. */
1885 if (crtl->has_bb_partition)
1887 first_function_block_is_cold = false;
1889 switch_to_section (unlikely_text_section ());
1890 assemble_align (align);
1891 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1893 /* When the function starts with a cold section, we need to explicitly
1894 align the hot section and write out the hot section label.
1895 But if the current function is a thunk, we do not have a CFG. */
1896 if (!cfun->is_thunk
1897 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1899 switch_to_section (text_section);
1900 assemble_align (align);
1901 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1902 hot_label_written = true;
1903 first_function_block_is_cold = true;
1905 in_cold_section_p = first_function_block_is_cold;
1909 /* Switch to the correct text section for the start of the function. */
1911 switch_to_section (function_section (decl), decl);
1912 if (crtl->has_bb_partition && !hot_label_written)
1913 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1915 /* Tell assembler to move to target machine's alignment for functions. */
1916 align = floor_log2 (align / BITS_PER_UNIT);
1917 if (align > 0)
1919 ASM_OUTPUT_ALIGN (asm_out_file, align);
1922 /* Handle a user-specified function alignment.
1923 Note that we still need to align to DECL_ALIGN, as above,
1924 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1925 if (! DECL_USER_ALIGN (decl)
1926 && align_functions.levels[0].log > align
1927 && optimize_function_for_speed_p (cfun))
1929 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1930 int align_log = align_functions.levels[0].log;
1931 #endif
1932 int max_skip = align_functions.levels[0].maxskip;
1933 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1934 && max_skip >= crtl->max_insn_address)
1935 max_skip = crtl->max_insn_address - 1;
1937 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1938 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1939 if (max_skip == align_functions.levels[0].maxskip)
1940 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1941 align_functions.levels[1].log,
1942 align_functions.levels[1].maxskip);
1943 #else
1944 ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
1945 #endif
1948 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1949 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1950 #endif
1952 if (!DECL_IGNORED_P (decl))
1953 (*debug_hooks->begin_function) (decl);
1955 /* Make function name accessible from other files, if appropriate. */
1957 if (TREE_PUBLIC (decl))
1959 notice_global_symbol (decl);
1961 globalize_decl (decl);
1963 maybe_assemble_visibility (decl);
1966 if (DECL_PRESERVE_P (decl))
1967 targetm.asm_out.mark_decl_preserved (fnname);
1969 unsigned short patch_area_size = crtl->patch_area_size;
1970 unsigned short patch_area_entry = crtl->patch_area_entry;
1972 /* Emit the patching area before the entry label, if any. */
1973 if (patch_area_entry > 0)
1974 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1975 patch_area_entry, true);
1977 /* Do any machine/system dependent processing of the function name. */
1978 #ifdef ASM_DECLARE_FUNCTION_NAME
1979 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1980 #else
1981 /* Standard thing is just output label for the function. */
1982 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1983 #endif /* ASM_DECLARE_FUNCTION_NAME */
1985 /* And the area after the label. Record it if we haven't done so yet. */
1986 if (patch_area_size > patch_area_entry)
1987 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1988 patch_area_size
1989 - patch_area_entry,
1990 patch_area_entry == 0);
1992 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1993 saw_no_split_stack = true;
1996 /* Output assembler code associated with defining the size of the
1997 function. DECL describes the function. NAME is the function's name. */
1999 void
2000 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
2002 #ifdef ASM_DECLARE_FUNCTION_SIZE
2003 /* We could have switched section in the middle of the function. */
2004 if (crtl->has_bb_partition)
2005 switch_to_section (function_section (decl));
2006 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
2007 #endif
2008 if (! CONSTANT_POOL_BEFORE_FUNCTION)
2010 output_constant_pool (fnname, decl);
2011 switch_to_section (function_section (decl)); /* need to switch back */
2013 /* Output labels for end of hot/cold text sections (to be used by
2014 debug info.) */
2015 if (crtl->has_bb_partition)
2017 section *save_text_section;
2019 save_text_section = in_section;
2020 switch_to_section (unlikely_text_section ());
2021 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
2022 if (cold_function_name != NULL_TREE)
2023 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
2024 IDENTIFIER_POINTER (cold_function_name),
2025 decl);
2026 #endif
2027 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
2028 if (first_function_block_is_cold)
2029 switch_to_section (text_section);
2030 else
2031 switch_to_section (function_section (decl));
2032 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
2033 switch_to_section (save_text_section);
2037 /* Assemble code to leave SIZE bytes of zeros. */
2039 void
2040 assemble_zeros (unsigned HOST_WIDE_INT size)
2042 /* Do no output if -fsyntax-only. */
2043 if (flag_syntax_only)
2044 return;
2046 #ifdef ASM_NO_SKIP_IN_TEXT
2047 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
2048 so we must output 0s explicitly in the text section. */
2049 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
2051 unsigned HOST_WIDE_INT i;
2052 for (i = 0; i < size; i++)
2053 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
2055 else
2056 #endif
2057 if (size > 0)
2058 ASM_OUTPUT_SKIP (asm_out_file, size);
2061 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
2063 void
2064 assemble_align (unsigned int align)
2066 if (align > BITS_PER_UNIT)
2068 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2072 /* Assemble a string constant with the specified C string as contents. */
2074 void
2075 assemble_string (const char *p, int size)
2077 int pos = 0;
2078 int maximum = 2000;
2080 /* If the string is very long, split it up. */
2082 while (pos < size)
2084 int thissize = size - pos;
2085 if (thissize > maximum)
2086 thissize = maximum;
2088 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
2090 pos += thissize;
2091 p += thissize;
2096 /* A noswitch_section_callback for lcomm_section. */
2098 static bool
2099 emit_local (tree decl ATTRIBUTE_UNUSED,
2100 const char *name ATTRIBUTE_UNUSED,
2101 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2102 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2104 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2105 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2106 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2107 size, align);
2108 return true;
2109 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2110 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2111 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2112 return true;
2113 #else
2114 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2115 return false;
2116 #endif
2119 /* A noswitch_section_callback for bss_noswitch_section. */
2121 #if defined ASM_OUTPUT_ALIGNED_BSS
2122 static bool
2123 emit_bss (tree decl ATTRIBUTE_UNUSED,
2124 const char *name ATTRIBUTE_UNUSED,
2125 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2126 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2128 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2129 get_variable_align (decl));
2130 return true;
2132 #endif
2134 /* A noswitch_section_callback for comm_section. */
2136 static bool
2137 emit_common (tree decl ATTRIBUTE_UNUSED,
2138 const char *name ATTRIBUTE_UNUSED,
2139 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2140 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2142 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2143 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2144 size, get_variable_align (decl));
2145 return true;
2146 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2147 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2148 get_variable_align (decl));
2149 return true;
2150 #else
2151 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2152 return false;
2153 #endif
2156 /* A noswitch_section_callback for tls_comm_section. */
2158 static bool
2159 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2160 const char *name ATTRIBUTE_UNUSED,
2161 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2162 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2164 #ifdef ASM_OUTPUT_TLS_COMMON
2165 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2166 return true;
2167 #else
2168 sorry ("thread-local COMMON data not implemented");
2169 return true;
2170 #endif
2173 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2174 NAME is the name of DECL's SYMBOL_REF. */
2176 static void
2177 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2178 unsigned int align)
2180 unsigned HOST_WIDE_INT size, rounded;
2182 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2183 rounded = size;
2185 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2186 size += asan_red_zone_size (size);
2188 /* Don't allocate zero bytes of common,
2189 since that means "undefined external" in the linker. */
2190 if (size == 0)
2191 rounded = 1;
2193 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2194 so that each uninitialized object starts on such a boundary. */
2195 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2196 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2197 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2199 if (!sect->noswitch.callback (decl, name, size, rounded)
2200 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2201 error ("requested alignment for %q+D is greater than "
2202 "implemented alignment of %wu", decl, rounded);
2205 /* A subroutine of assemble_variable. Output the label and contents of
2206 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2207 is as for assemble_variable. */
2209 static void
2210 assemble_variable_contents (tree decl, const char *name,
2211 bool dont_output_data, bool merge_strings)
2213 /* Do any machine/system dependent processing of the object. */
2214 #ifdef ASM_DECLARE_OBJECT_NAME
2215 last_assemble_variable_decl = decl;
2216 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2217 #else
2218 /* Standard thing is just output label for the object. */
2219 ASM_OUTPUT_LABEL (asm_out_file, name);
2220 #endif /* ASM_DECLARE_OBJECT_NAME */
2222 if (!dont_output_data)
2224 /* Caller is supposed to use varpool_get_constructor when it wants
2225 to output the body. */
2226 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2227 if (DECL_INITIAL (decl)
2228 && DECL_INITIAL (decl) != error_mark_node
2229 && !initializer_zerop (DECL_INITIAL (decl)))
2230 /* Output the actual data. */
2231 output_constant (DECL_INITIAL (decl),
2232 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2233 get_variable_align (decl),
2234 false, merge_strings);
2235 else
2236 /* Leave space for it. */
2237 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2238 targetm.asm_out.decl_end ();
2242 /* Write out assembly for the variable DECL, which is not defined in
2243 the current translation unit. */
2244 void
2245 assemble_undefined_decl (tree decl)
2247 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2248 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2251 /* Assemble everything that is needed for a variable or function declaration.
2252 Not used for automatic variables, and not used for function definitions.
2253 Should not be called for variables of incomplete structure type.
2255 TOP_LEVEL is nonzero if this variable has file scope.
2256 AT_END is nonzero if this is the special handling, at end of compilation,
2257 to define things that have had only tentative definitions.
2258 DONT_OUTPUT_DATA if nonzero means don't actually output the
2259 initial value (that will be done by the caller). */
2261 void
2262 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2263 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2265 const char *name;
2266 rtx decl_rtl, symbol;
2267 section *sect;
2268 unsigned int align;
2269 bool asan_protected = false;
2271 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2272 gcc_assert (VAR_P (decl));
2274 /* Emulated TLS had better not get this far. */
2275 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2277 last_assemble_variable_decl = 0;
2279 /* Normally no need to say anything here for external references,
2280 since assemble_external is called by the language-specific code
2281 when a declaration is first seen. */
2283 if (DECL_EXTERNAL (decl))
2284 return;
2286 /* Do nothing for global register variables. */
2287 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2289 TREE_ASM_WRITTEN (decl) = 1;
2290 return;
2293 /* If type was incomplete when the variable was declared,
2294 see if it is complete now. */
2296 if (DECL_SIZE (decl) == 0)
2297 layout_decl (decl, 0);
2299 /* Still incomplete => don't allocate it; treat the tentative defn
2300 (which is what it must have been) as an `extern' reference. */
2302 if (!dont_output_data && DECL_SIZE (decl) == 0)
2304 error ("storage size of %q+D isn%'t known", decl);
2305 TREE_ASM_WRITTEN (decl) = 1;
2306 return;
2309 /* The first declaration of a variable that comes through this function
2310 decides whether it is global (in C, has external linkage)
2311 or local (in C, has internal linkage). So do nothing more
2312 if this function has already run. */
2314 if (TREE_ASM_WRITTEN (decl))
2315 return;
2317 /* Make sure targetm.encode_section_info is invoked before we set
2318 ASM_WRITTEN. */
2319 decl_rtl = DECL_RTL (decl);
2321 TREE_ASM_WRITTEN (decl) = 1;
2323 /* Do no output if -fsyntax-only. */
2324 if (flag_syntax_only)
2325 return;
2327 if (! dont_output_data
2328 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2330 error ("size of variable %q+D is too large", decl);
2331 return;
2334 gcc_assert (MEM_P (decl_rtl));
2335 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2336 symbol = XEXP (decl_rtl, 0);
2338 /* If this symbol belongs to the tree constant pool, output the constant
2339 if it hasn't already been written. */
2340 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2342 tree decl = SYMBOL_REF_DECL (symbol);
2343 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2344 output_constant_def_contents (symbol);
2345 return;
2348 app_disable ();
2350 name = XSTR (symbol, 0);
2351 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2352 notice_global_symbol (decl);
2354 /* Compute the alignment of this data. */
2356 align_variable (decl, dont_output_data);
2358 if ((flag_sanitize & SANITIZE_ADDRESS)
2359 && asan_protect_global (decl))
2361 asan_protected = true;
2362 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2363 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2366 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2368 align = get_variable_align (decl);
2370 if (TREE_PUBLIC (decl))
2371 maybe_assemble_visibility (decl);
2373 if (DECL_PRESERVE_P (decl))
2374 targetm.asm_out.mark_decl_preserved (name);
2376 /* First make the assembler name(s) global if appropriate. */
2377 sect = get_variable_section (decl, false);
2378 if (TREE_PUBLIC (decl)
2379 && (sect->common.flags & SECTION_COMMON) == 0)
2380 globalize_decl (decl);
2382 /* Output any data that we will need to use the address of. */
2383 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2384 output_addressed_constants (DECL_INITIAL (decl), 0);
2386 /* dbxout.cc needs to know this. */
2387 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2388 DECL_IN_TEXT_SECTION (decl) = 1;
2390 /* If the decl is part of an object_block, make sure that the decl
2391 has been positioned within its block, but do not write out its
2392 definition yet. output_object_blocks will do that later. */
2393 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2395 gcc_assert (!dont_output_data);
2396 place_block_symbol (symbol);
2398 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2399 assemble_noswitch_variable (decl, name, sect, align);
2400 else
2402 /* Special-case handling of vtv comdat sections. */
2403 if (SECTION_STYLE (sect) == SECTION_NAMED
2404 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2405 handle_vtv_comdat_section (sect, decl);
2406 else
2407 switch_to_section (sect, decl);
2408 if (align > BITS_PER_UNIT)
2409 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2410 assemble_variable_contents (decl, name, dont_output_data,
2411 (sect->common.flags & SECTION_MERGE)
2412 && (sect->common.flags & SECTION_STRINGS));
2413 if (asan_protected)
2415 unsigned HOST_WIDE_INT int size
2416 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2417 assemble_zeros (asan_red_zone_size (size));
2422 /* Return true if type TYPE contains any pointers. */
2424 static bool
2425 contains_pointers_p (tree type)
2427 switch (TREE_CODE (type))
2429 case POINTER_TYPE:
2430 case REFERENCE_TYPE:
2431 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2432 so I'll play safe and return 1. */
2433 case OFFSET_TYPE:
2434 return true;
2436 case RECORD_TYPE:
2437 case UNION_TYPE:
2438 case QUAL_UNION_TYPE:
2440 tree fields;
2441 /* For a type that has fields, see if the fields have pointers. */
2442 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2443 if (TREE_CODE (fields) == FIELD_DECL
2444 && contains_pointers_p (TREE_TYPE (fields)))
2445 return true;
2446 return false;
2449 case ARRAY_TYPE:
2450 /* An array type contains pointers if its element type does. */
2451 return contains_pointers_p (TREE_TYPE (type));
2453 default:
2454 return false;
2458 /* We delay assemble_external processing until
2459 the compilation unit is finalized. This is the best we can do for
2460 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2461 it all the way to final. See PR 17982 for further discussion. */
2462 static GTY(()) tree pending_assemble_externals;
2464 #ifdef ASM_OUTPUT_EXTERNAL
2465 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2466 As a result, assemble_external can be called after the list of externals
2467 is processed and the pointer set destroyed. */
2468 static bool pending_assemble_externals_processed;
2470 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2471 TREE_LIST in assemble_external. */
2472 static hash_set<tree> *pending_assemble_externals_set;
2474 /* True if DECL is a function decl for which no out-of-line copy exists.
2475 It is assumed that DECL's assembler name has been set. */
2477 static bool
2478 incorporeal_function_p (tree decl)
2480 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl))
2482 const char *name;
2484 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2485 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2486 return true;
2488 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2489 /* Atomic or sync builtins which have survived this far will be
2490 resolved externally and therefore are not incorporeal. */
2491 if (startswith (name, "__builtin_"))
2492 return true;
2494 return false;
2497 /* Actually do the tests to determine if this is necessary, and invoke
2498 ASM_OUTPUT_EXTERNAL. */
2499 static void
2500 assemble_external_real (tree decl)
2502 rtx rtl = DECL_RTL (decl);
2504 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2505 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2506 && !incorporeal_function_p (decl))
2508 /* Some systems do require some output. */
2509 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2510 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2513 #endif
2515 void
2516 process_pending_assemble_externals (void)
2518 #ifdef ASM_OUTPUT_EXTERNAL
2519 tree list;
2520 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2521 assemble_external_real (TREE_VALUE (list));
2523 pending_assemble_externals = 0;
2524 pending_assemble_externals_processed = true;
2525 delete pending_assemble_externals_set;
2526 #endif
2529 /* This TREE_LIST contains any weak symbol declarations waiting
2530 to be emitted. */
2531 static GTY(()) tree weak_decls;
2533 /* Output something to declare an external symbol to the assembler,
2534 and qualifiers such as weakness. (Most assemblers don't need
2535 extern declaration, so we normally output nothing.) Do nothing if
2536 DECL is not external. */
2538 void
2539 assemble_external (tree decl ATTRIBUTE_UNUSED)
2541 /* Make sure that the ASM_OUT_FILE is open.
2542 If it's not, we should not be calling this function. */
2543 gcc_assert (asm_out_file);
2545 /* In a perfect world, the following condition would be true.
2546 Sadly, the Go front end emit assembly *from the front end*,
2547 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2548 #if 0
2549 /* This function should only be called if we are expanding, or have
2550 expanded, to RTL.
2551 Ideally, only final.cc would be calling this function, but it is
2552 not clear whether that would break things somehow. See PR 17982
2553 for further discussion. */
2554 gcc_assert (state == EXPANSION
2555 || state == FINISHED);
2556 #endif
2558 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2559 return;
2561 /* We want to output annotation for weak and external symbols at
2562 very last to check if they are references or not. */
2564 if (TARGET_SUPPORTS_WEAK
2565 && DECL_WEAK (decl)
2566 /* TREE_STATIC is a weird and abused creature which is not
2567 generally the right test for whether an entity has been
2568 locally emitted, inlined or otherwise not-really-extern, but
2569 for declarations that can be weak, it happens to be
2570 match. */
2571 && !TREE_STATIC (decl)
2572 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2573 && value_member (decl, weak_decls) == NULL_TREE)
2574 weak_decls = tree_cons (NULL, decl, weak_decls);
2576 #ifdef ASM_OUTPUT_EXTERNAL
2577 if (pending_assemble_externals_processed)
2579 assemble_external_real (decl);
2580 return;
2583 if (! pending_assemble_externals_set->add (decl))
2584 pending_assemble_externals = tree_cons (NULL, decl,
2585 pending_assemble_externals);
2586 #endif
2589 /* Similar, for calling a library function FUN. */
2591 void
2592 assemble_external_libcall (rtx fun)
2594 /* Declare library function name external when first used, if nec. */
2595 if (! SYMBOL_REF_USED (fun))
2597 SYMBOL_REF_USED (fun) = 1;
2598 targetm.asm_out.external_libcall (fun);
2602 /* Assemble a label named NAME. */
2604 void
2605 assemble_label (FILE *file, const char *name)
2607 ASM_OUTPUT_LABEL (file, name);
2610 /* Set the symbol_referenced flag for ID. */
2611 void
2612 mark_referenced (tree id)
2614 TREE_SYMBOL_REFERENCED (id) = 1;
2617 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2618 void
2619 mark_decl_referenced (tree decl)
2621 if (TREE_CODE (decl) == FUNCTION_DECL)
2623 /* Extern inline functions don't become needed when referenced.
2624 If we know a method will be emitted in other TU and no new
2625 functions can be marked reachable, just use the external
2626 definition. */
2627 struct cgraph_node *node = cgraph_node::get_create (decl);
2628 if (!DECL_EXTERNAL (decl)
2629 && !node->definition)
2630 node->mark_force_output ();
2632 else if (VAR_P (decl))
2634 varpool_node *node = varpool_node::get_create (decl);
2635 /* C++ frontend use mark_decl_references to force COMDAT variables
2636 to be output that might appear dead otherwise. */
2637 node->force_output = true;
2639 /* else do nothing - we can get various sorts of CST nodes here,
2640 which do not need to be marked. */
2644 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2645 starts with a *, the rest of NAME is output verbatim. Otherwise
2646 NAME is transformed in a target-specific way (usually by the
2647 addition of an underscore). */
2649 void
2650 assemble_name_raw (FILE *file, const char *name)
2652 if (name[0] == '*')
2653 fputs (&name[1], file);
2654 else
2655 ASM_OUTPUT_LABELREF (file, name);
2658 /* Return NAME that should actually be emitted, looking through
2659 transparent aliases. If NAME refers to an entity that is also
2660 represented as a tree (like a function or variable), mark the entity
2661 as referenced. */
2662 const char *
2663 assemble_name_resolve (const char *name)
2665 const char *real_name = targetm.strip_name_encoding (name);
2666 tree id = maybe_get_identifier (real_name);
2668 if (id)
2670 tree id_orig = id;
2672 mark_referenced (id);
2673 ultimate_transparent_alias_target (&id);
2674 if (id != id_orig)
2675 name = IDENTIFIER_POINTER (id);
2676 gcc_assert (! TREE_CHAIN (id));
2679 return name;
2682 /* Like assemble_name_raw, but should be used when NAME might refer to
2683 an entity that is also represented as a tree (like a function or
2684 variable). If NAME does refer to such an entity, that entity will
2685 be marked as referenced. */
2687 void
2688 assemble_name (FILE *file, const char *name)
2690 assemble_name_raw (file, assemble_name_resolve (name));
2693 /* Allocate SIZE bytes writable static space with a gensym name
2694 and return an RTX to refer to its address. */
2697 assemble_static_space (unsigned HOST_WIDE_INT size)
2699 char name[17];
2700 const char *namestring;
2701 rtx x;
2703 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2704 ++const_labelno;
2705 namestring = ggc_strdup (name);
2707 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2708 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2710 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2711 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2712 BIGGEST_ALIGNMENT);
2713 #else
2714 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2715 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2716 #else
2718 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2719 so that each uninitialized object starts on such a boundary. */
2720 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2721 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2722 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2723 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2724 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2725 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2727 #endif
2728 #endif
2729 return x;
2732 /* Assemble the static constant template for function entry trampolines.
2733 This is done at most once per compilation.
2734 Returns an RTX for the address of the template. */
2736 static GTY(()) rtx initial_trampoline;
2739 assemble_trampoline_template (void)
2741 char label[256];
2742 const char *name;
2743 int align;
2744 rtx symbol;
2746 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2748 if (initial_trampoline)
2749 return initial_trampoline;
2751 /* By default, put trampoline templates in read-only data section. */
2753 #ifdef TRAMPOLINE_SECTION
2754 switch_to_section (TRAMPOLINE_SECTION);
2755 #else
2756 switch_to_section (readonly_data_section);
2757 #endif
2759 /* Write the assembler code to define one. */
2760 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2761 if (align > 0)
2762 ASM_OUTPUT_ALIGN (asm_out_file, align);
2764 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2765 targetm.asm_out.trampoline_template (asm_out_file);
2767 /* Record the rtl to refer to it. */
2768 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2769 name = ggc_strdup (label);
2770 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2771 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2773 initial_trampoline = gen_const_mem (BLKmode, symbol);
2774 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2775 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2777 return initial_trampoline;
2780 /* A and B are either alignments or offsets. Return the minimum alignment
2781 that may be assumed after adding the two together. */
2783 static inline unsigned
2784 min_align (unsigned int a, unsigned int b)
2786 return least_bit_hwi (a | b);
2789 /* Return the assembler directive for creating a given kind of integer
2790 object. SIZE is the number of bytes in the object and ALIGNED_P
2791 indicates whether it is known to be aligned. Return NULL if the
2792 assembly dialect has no such directive.
2794 The returned string should be printed at the start of a new line and
2795 be followed immediately by the object's initial value. */
2797 const char *
2798 integer_asm_op (int size, int aligned_p)
2800 struct asm_int_op *ops;
2802 if (aligned_p)
2803 ops = &targetm.asm_out.aligned_op;
2804 else
2805 ops = &targetm.asm_out.unaligned_op;
2807 switch (size)
2809 case 1:
2810 return targetm.asm_out.byte_op;
2811 case 2:
2812 return ops->hi;
2813 case 3:
2814 return ops->psi;
2815 case 4:
2816 return ops->si;
2817 case 5:
2818 case 6:
2819 case 7:
2820 return ops->pdi;
2821 case 8:
2822 return ops->di;
2823 case 9:
2824 case 10:
2825 case 11:
2826 case 12:
2827 case 13:
2828 case 14:
2829 case 15:
2830 return ops->pti;
2831 case 16:
2832 return ops->ti;
2833 default:
2834 return NULL;
2838 /* Use directive OP to assemble an integer object X. Print OP at the
2839 start of the line, followed immediately by the value of X. */
2841 void
2842 assemble_integer_with_op (const char *op, rtx x)
2844 fputs (op, asm_out_file);
2845 output_addr_const (asm_out_file, x);
2846 fputc ('\n', asm_out_file);
2849 /* The default implementation of the asm_out.integer target hook. */
2851 bool
2852 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2853 unsigned int size ATTRIBUTE_UNUSED,
2854 int aligned_p ATTRIBUTE_UNUSED)
2856 const char *op = integer_asm_op (size, aligned_p);
2857 /* Avoid GAS bugs for large values. Specifically negative values whose
2858 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2859 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2860 return false;
2861 return op && (assemble_integer_with_op (op, x), true);
2864 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2865 the alignment of the integer in bits. Return 1 if we were able to output
2866 the constant, otherwise 0. We must be able to output the constant,
2867 if FORCE is nonzero. */
2869 bool
2870 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2872 int aligned_p;
2874 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2876 /* See if the target hook can handle this kind of object. */
2877 if (targetm.asm_out.integer (x, size, aligned_p))
2878 return true;
2880 /* If the object is a multi-byte one, try splitting it up. Split
2881 it into words it if is multi-word, otherwise split it into bytes. */
2882 if (size > 1)
2884 machine_mode omode, imode;
2885 unsigned int subalign;
2886 unsigned int subsize, i;
2887 enum mode_class mclass;
2889 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2890 subalign = MIN (align, subsize * BITS_PER_UNIT);
2891 if (GET_CODE (x) == CONST_FIXED)
2892 mclass = GET_MODE_CLASS (GET_MODE (x));
2893 else
2894 mclass = MODE_INT;
2896 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2897 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2899 for (i = 0; i < size; i += subsize)
2901 rtx partial = simplify_subreg (omode, x, imode, i);
2902 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2903 break;
2905 if (i == size)
2906 return true;
2908 /* If we've printed some of it, but not all of it, there's no going
2909 back now. */
2910 gcc_assert (!i);
2913 gcc_assert (!force);
2915 return false;
2918 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
2919 is the alignment of the constant in bits. If REVERSE is true, D is output
2920 in reverse storage order. */
2922 void
2923 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2924 bool reverse)
2926 long data[4] = {0, 0, 0, 0};
2927 int bitsize, nelts, nunits, units_per;
2928 rtx elt;
2930 /* This is hairy. We have a quantity of known size. real_to_target
2931 will put it into an array of *host* longs, 32 bits per element
2932 (even if long is more than 32 bits). We need to determine the
2933 number of array elements that are occupied (nelts) and the number
2934 of *target* min-addressable units that will be occupied in the
2935 object file (nunits). We cannot assume that 32 divides the
2936 mode's bitsize (size * BITS_PER_UNIT) evenly.
2938 size * BITS_PER_UNIT is used here to make sure that padding bits
2939 (which might appear at either end of the value; real_to_target
2940 will include the padding bits in its output array) are included. */
2942 nunits = GET_MODE_SIZE (mode);
2943 bitsize = nunits * BITS_PER_UNIT;
2944 nelts = CEIL (bitsize, 32);
2945 units_per = 32 / BITS_PER_UNIT;
2947 real_to_target (data, &d, mode);
2949 /* Put out the first word with the specified alignment. */
2950 unsigned int chunk_nunits = MIN (nunits, units_per);
2951 if (reverse)
2952 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
2953 else
2954 elt = GEN_INT (sext_hwi (data[0], chunk_nunits * BITS_PER_UNIT));
2955 assemble_integer (elt, chunk_nunits, align, 1);
2956 nunits -= chunk_nunits;
2958 /* Subsequent words need only 32-bit alignment. */
2959 align = min_align (align, 32);
2961 for (int i = 1; i < nelts; i++)
2963 chunk_nunits = MIN (nunits, units_per);
2964 if (reverse)
2965 elt = flip_storage_order (SImode,
2966 gen_int_mode (data[nelts - 1 - i], SImode));
2967 else
2968 elt = GEN_INT (sext_hwi (data[i], chunk_nunits * BITS_PER_UNIT));
2969 assemble_integer (elt, chunk_nunits, align, 1);
2970 nunits -= chunk_nunits;
2974 /* Given an expression EXP with a constant value,
2975 reduce it to the sum of an assembler symbol and an integer.
2976 Store them both in the structure *VALUE.
2977 EXP must be reducible. */
2979 class addr_const {
2980 public:
2981 rtx base;
2982 poly_int64 offset;
2985 static void
2986 decode_addr_const (tree exp, class addr_const *value)
2988 tree target = TREE_OPERAND (exp, 0);
2989 poly_int64 offset = 0;
2990 rtx x;
2992 while (1)
2994 poly_int64 bytepos;
2995 if (TREE_CODE (target) == COMPONENT_REF
2996 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
2997 &bytepos))
2999 offset += bytepos;
3000 target = TREE_OPERAND (target, 0);
3002 else if (TREE_CODE (target) == ARRAY_REF
3003 || TREE_CODE (target) == ARRAY_RANGE_REF)
3005 /* Truncate big offset. */
3006 offset
3007 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
3008 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
3009 target = TREE_OPERAND (target, 0);
3011 else if (TREE_CODE (target) == MEM_REF
3012 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
3014 offset += mem_ref_offset (target).force_shwi ();
3015 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
3017 else if (INDIRECT_REF_P (target)
3018 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
3019 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
3020 == ADDR_EXPR)
3021 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
3022 else
3023 break;
3026 switch (TREE_CODE (target))
3028 case VAR_DECL:
3029 case FUNCTION_DECL:
3030 x = DECL_RTL (target);
3031 break;
3033 case LABEL_DECL:
3034 x = gen_rtx_MEM (FUNCTION_MODE,
3035 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
3036 break;
3038 case REAL_CST:
3039 case FIXED_CST:
3040 case STRING_CST:
3041 case COMPLEX_CST:
3042 case CONSTRUCTOR:
3043 case INTEGER_CST:
3044 x = lookup_constant_def (target);
3045 /* Should have been added by output_addressed_constants. */
3046 gcc_assert (x);
3047 break;
3049 case INDIRECT_REF:
3050 /* This deals with absolute addresses. */
3051 offset += tree_to_shwi (TREE_OPERAND (target, 0));
3052 x = gen_rtx_MEM (QImode,
3053 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
3054 break;
3056 case COMPOUND_LITERAL_EXPR:
3057 gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
3058 x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
3059 break;
3061 default:
3062 gcc_unreachable ();
3065 gcc_assert (MEM_P (x));
3066 x = XEXP (x, 0);
3068 value->base = x;
3069 value->offset = offset;
3072 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
3074 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
3076 /* Constant pool accessor function. */
3078 hash_table<tree_descriptor_hasher> *
3079 constant_pool_htab (void)
3081 return const_desc_htab;
3084 /* Compute a hash code for a constant expression. */
3086 hashval_t
3087 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3089 return ptr->hash;
3092 static hashval_t
3093 const_hash_1 (const tree exp)
3095 const char *p;
3096 hashval_t hi;
3097 int len, i;
3098 enum tree_code code = TREE_CODE (exp);
3100 /* Either set P and LEN to the address and len of something to hash and
3101 exit the switch or return a value. */
3103 switch (code)
3105 case INTEGER_CST:
3106 p = (char *) &TREE_INT_CST_ELT (exp, 0);
3107 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3108 break;
3110 case REAL_CST:
3111 return real_hash (TREE_REAL_CST_PTR (exp));
3113 case FIXED_CST:
3114 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3116 case STRING_CST:
3117 p = TREE_STRING_POINTER (exp);
3118 len = TREE_STRING_LENGTH (exp);
3119 break;
3121 case COMPLEX_CST:
3122 return (const_hash_1 (TREE_REALPART (exp)) * 5
3123 + const_hash_1 (TREE_IMAGPART (exp)));
3125 case VECTOR_CST:
3127 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3128 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3129 unsigned int count = vector_cst_encoded_nelts (exp);
3130 for (unsigned int i = 0; i < count; ++i)
3131 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3132 return hi;
3135 case CONSTRUCTOR:
3137 unsigned HOST_WIDE_INT idx;
3138 tree value;
3140 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3142 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3143 if (value)
3144 hi = hi * 603 + const_hash_1 (value);
3146 return hi;
3149 case ADDR_EXPR:
3150 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3151 return const_hash_1 (TREE_OPERAND (exp, 0));
3153 /* Fallthru. */
3154 case FDESC_EXPR:
3156 class addr_const value;
3158 decode_addr_const (exp, &value);
3159 switch (GET_CODE (value.base))
3161 case SYMBOL_REF:
3162 /* Don't hash the address of the SYMBOL_REF;
3163 only use the offset and the symbol name. */
3164 hi = value.offset.coeffs[0];
3165 p = XSTR (value.base, 0);
3166 for (i = 0; p[i] != 0; i++)
3167 hi = ((hi * 613) + (unsigned) (p[i]));
3168 break;
3170 case LABEL_REF:
3171 hi = (value.offset.coeffs[0]
3172 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3173 break;
3175 default:
3176 gcc_unreachable ();
3179 return hi;
3181 case PLUS_EXPR:
3182 case POINTER_PLUS_EXPR:
3183 case MINUS_EXPR:
3184 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3185 + const_hash_1 (TREE_OPERAND (exp, 1)));
3187 CASE_CONVERT:
3188 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3190 default:
3191 /* A language specific constant. Just hash the code. */
3192 return code;
3195 /* Compute hashing function. */
3196 hi = len;
3197 for (i = 0; i < len; i++)
3198 hi = ((hi * 613) + (unsigned) (p[i]));
3200 return hi;
3203 /* Wrapper of compare_constant, for the htab interface. */
3204 bool
3205 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3206 constant_descriptor_tree *c2)
3208 if (c1->hash != c2->hash)
3209 return false;
3210 return compare_constant (c1->value, c2->value);
3213 /* Compare t1 and t2, and return true only if they are known to result in
3214 the same bit pattern on output. */
3216 static bool
3217 compare_constant (const tree t1, const tree t2)
3219 enum tree_code typecode;
3221 if (t1 == NULL_TREE)
3222 return t2 == NULL_TREE;
3223 if (t2 == NULL_TREE)
3224 return false;
3226 if (TREE_CODE (t1) != TREE_CODE (t2))
3227 return false;
3229 switch (TREE_CODE (t1))
3231 case INTEGER_CST:
3232 /* Integer constants are the same only if the same width of type. */
3233 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3234 return false;
3235 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3236 return false;
3237 return tree_int_cst_equal (t1, t2);
3239 case REAL_CST:
3240 /* Real constants are the same only if the same width of type. In
3241 addition to the same width, we need to check whether the modes are the
3242 same. There might be two floating point modes that are the same size
3243 but have different representations, such as the PowerPC that has 2
3244 different 128-bit floating point types (IBM extended double and IEEE
3245 128-bit floating point). */
3246 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3247 return false;
3248 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3249 return false;
3250 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3252 case FIXED_CST:
3253 /* Fixed constants are the same only if the same width of type. */
3254 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3255 return false;
3257 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3259 case STRING_CST:
3260 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3261 || int_size_in_bytes (TREE_TYPE (t1))
3262 != int_size_in_bytes (TREE_TYPE (t2)))
3263 return false;
3265 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3266 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3267 TREE_STRING_LENGTH (t1)));
3269 case COMPLEX_CST:
3270 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3271 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3273 case VECTOR_CST:
3275 if (VECTOR_CST_NPATTERNS (t1)
3276 != VECTOR_CST_NPATTERNS (t2))
3277 return false;
3279 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3280 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3281 return false;
3283 unsigned int count = vector_cst_encoded_nelts (t1);
3284 for (unsigned int i = 0; i < count; ++i)
3285 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3286 VECTOR_CST_ENCODED_ELT (t2, i)))
3287 return false;
3289 return true;
3292 case CONSTRUCTOR:
3294 vec<constructor_elt, va_gc> *v1, *v2;
3295 unsigned HOST_WIDE_INT idx;
3297 typecode = TREE_CODE (TREE_TYPE (t1));
3298 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3299 return false;
3301 if (typecode == ARRAY_TYPE)
3303 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3304 /* For arrays, check that mode, size and storage order match. */
3305 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3306 || size_1 == -1
3307 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3308 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3309 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3310 return false;
3312 else
3314 /* For record and union constructors, require exact type
3315 equality. */
3316 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3317 return false;
3320 v1 = CONSTRUCTOR_ELTS (t1);
3321 v2 = CONSTRUCTOR_ELTS (t2);
3322 if (vec_safe_length (v1) != vec_safe_length (v2))
3323 return false;
3325 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3327 constructor_elt *c1 = &(*v1)[idx];
3328 constructor_elt *c2 = &(*v2)[idx];
3330 /* Check that each value is the same... */
3331 if (!compare_constant (c1->value, c2->value))
3332 return false;
3333 /* ... and that they apply to the same fields! */
3334 if (typecode == ARRAY_TYPE)
3336 if (!compare_constant (c1->index, c2->index))
3337 return false;
3339 else
3341 if (c1->index != c2->index)
3342 return false;
3346 return true;
3349 case ADDR_EXPR:
3350 case FDESC_EXPR:
3352 class addr_const value1, value2;
3353 enum rtx_code code;
3354 bool ret;
3356 decode_addr_const (t1, &value1);
3357 decode_addr_const (t2, &value2);
3359 if (maybe_ne (value1.offset, value2.offset))
3360 return false;
3362 code = GET_CODE (value1.base);
3363 if (code != GET_CODE (value2.base))
3364 return false;
3366 switch (code)
3368 case SYMBOL_REF:
3369 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3370 break;
3372 case LABEL_REF:
3373 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3374 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3375 break;
3377 default:
3378 gcc_unreachable ();
3380 return ret;
3383 case PLUS_EXPR:
3384 case POINTER_PLUS_EXPR:
3385 case MINUS_EXPR:
3386 case RANGE_EXPR:
3387 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3388 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3390 CASE_CONVERT:
3391 case VIEW_CONVERT_EXPR:
3392 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3394 default:
3395 return false;
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 false;
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 ();
4067 unsigned int mask = GET_MODE_MASK (GET_MODE_INNER (mode));
4069 /* Build the constant up one integer at a time. */
4070 unsigned int elts_per_int = int_bits / elt_bits;
4071 for (unsigned int i = 0; i < nelts; i += elts_per_int)
4073 unsigned HOST_WIDE_INT value = 0;
4074 unsigned int limit = MIN (nelts - i, elts_per_int);
4075 for (unsigned int j = 0; j < limit; ++j)
4077 auto elt = INTVAL (CONST_VECTOR_ELT (x, i + j));
4078 value |= (elt & mask) << (j * elt_bits);
4080 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
4081 i != 0 ? MIN (align, int_bits) : align);
4083 break;
4085 case MODE_VECTOR_FLOAT:
4086 case MODE_VECTOR_INT:
4087 case MODE_VECTOR_FRACT:
4088 case MODE_VECTOR_UFRACT:
4089 case MODE_VECTOR_ACCUM:
4090 case MODE_VECTOR_UACCUM:
4092 int i, units;
4093 scalar_mode submode = GET_MODE_INNER (mode);
4094 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4096 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4097 units = GET_MODE_NUNITS (mode);
4099 for (i = 0; i < units; i++)
4101 rtx elt = CONST_VECTOR_ELT (x, i);
4102 output_constant_pool_2 (submode, elt, i ? subalign : align);
4105 break;
4107 default:
4108 gcc_unreachable ();
4112 /* Worker function for output_constant_pool. Emit constant DESC,
4113 giving it ALIGN bits of alignment. */
4115 static void
4116 output_constant_pool_1 (class constant_descriptor_rtx *desc,
4117 unsigned int align)
4119 rtx x, tmp;
4121 x = desc->constant;
4123 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4124 whose CODE_LABEL has been deleted. This can occur if a jump table
4125 is eliminated by optimization. If so, write a constant of zero
4126 instead. Note that this can also happen by turning the
4127 CODE_LABEL into a NOTE. */
4128 /* ??? This seems completely and utterly wrong. Certainly it's
4129 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4130 functioning even with rtx_insn::deleted and friends. */
4132 tmp = x;
4133 switch (GET_CODE (tmp))
4135 case CONST:
4136 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4137 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4138 break;
4139 tmp = XEXP (XEXP (tmp, 0), 0);
4140 /* FALLTHRU */
4142 case LABEL_REF:
4144 rtx_insn *insn = label_ref_label (tmp);
4145 gcc_assert (!insn->deleted ());
4146 gcc_assert (!NOTE_P (insn)
4147 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4148 break;
4151 default:
4152 break;
4155 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4156 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4157 align, desc->labelno, done);
4158 #endif
4160 assemble_align (align);
4162 /* Output the label. */
4163 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4165 /* Output the data.
4166 Pass actual alignment value while emitting string constant to asm code
4167 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4168 assuming that the data is already aligned which prevents the generation
4169 of fix-up table entries. */
4170 output_constant_pool_2 (desc->mode, x, desc->align);
4172 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4173 sections have proper size. */
4174 if (align > GET_MODE_BITSIZE (desc->mode)
4175 && in_section
4176 && (in_section->common.flags & SECTION_MERGE))
4177 assemble_align (align);
4179 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4180 done:
4181 #endif
4182 return;
4185 /* Recompute the offsets of entries in POOL, and the overall size of
4186 POOL. Do this after calling mark_constant_pool to ensure that we
4187 are computing the offset values for the pool which we will actually
4188 emit. */
4190 static void
4191 recompute_pool_offsets (struct rtx_constant_pool *pool)
4193 class constant_descriptor_rtx *desc;
4194 pool->offset = 0;
4196 for (desc = pool->first; desc ; desc = desc->next)
4197 if (desc->mark)
4199 /* Recalculate offset. */
4200 unsigned int align = desc->align;
4201 pool->offset += (align / BITS_PER_UNIT) - 1;
4202 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4203 desc->offset = pool->offset;
4204 pool->offset += GET_MODE_SIZE (desc->mode);
4208 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4209 Emit referenced deferred strings. */
4211 static void
4212 mark_constants_in_pattern (rtx insn)
4214 subrtx_iterator::array_type array;
4215 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4217 const_rtx x = *iter;
4218 if (GET_CODE (x) == SYMBOL_REF)
4220 if (CONSTANT_POOL_ADDRESS_P (x))
4222 class constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4223 if (desc->mark == 0)
4225 desc->mark = 1;
4226 iter.substitute (desc->constant);
4229 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4231 tree decl = SYMBOL_REF_DECL (x);
4232 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4234 n_deferred_constants--;
4235 output_constant_def_contents (CONST_CAST_RTX (x));
4242 /* Look through appropriate parts of INSN, marking all entries in the
4243 constant pool which are actually being used. Entries that are only
4244 referenced by other constants are also marked as used. Emit
4245 deferred strings that are used. */
4247 static void
4248 mark_constants (rtx_insn *insn)
4250 if (!INSN_P (insn))
4251 return;
4253 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4254 insns, not any notes that may be attached. We don't want to mark
4255 a constant just because it happens to appear in a REG_EQUIV note. */
4256 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4258 int i, n = seq->len ();
4259 for (i = 0; i < n; ++i)
4261 rtx subinsn = seq->element (i);
4262 if (INSN_P (subinsn))
4263 mark_constants_in_pattern (subinsn);
4266 else
4267 mark_constants_in_pattern (insn);
4270 /* Look through the instructions for this function, and mark all the
4271 entries in POOL which are actually being used. Emit deferred constants
4272 which have indeed been used. */
4274 static void
4275 mark_constant_pool (void)
4277 rtx_insn *insn;
4279 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4280 return;
4282 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4283 mark_constants (insn);
4286 /* Write all the constants in POOL. */
4288 static void
4289 output_constant_pool_contents (struct rtx_constant_pool *pool)
4291 class constant_descriptor_rtx *desc;
4293 for (desc = pool->first; desc ; desc = desc->next)
4294 if (desc->mark < 0)
4296 #ifdef ASM_OUTPUT_DEF
4297 const char *name = XSTR (desc->sym, 0);
4298 char label[256];
4299 char buffer[256 + 32];
4300 const char *p;
4302 ASM_GENERATE_INTERNAL_LABEL (label, "LC", ~desc->mark);
4303 p = label;
4304 if (desc->offset)
4306 sprintf (buffer, "%s+%ld", p, (long) (desc->offset));
4307 p = buffer;
4309 ASM_OUTPUT_DEF (asm_out_file, name, p);
4310 #else
4311 gcc_unreachable ();
4312 #endif
4314 else if (desc->mark)
4316 /* If the constant is part of an object_block, make sure that
4317 the constant has been positioned within its block, but do not
4318 write out its definition yet. output_object_blocks will do
4319 that later. */
4320 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4321 && SYMBOL_REF_BLOCK (desc->sym))
4322 place_block_symbol (desc->sym);
4323 else
4325 switch_to_section (targetm.asm_out.select_rtx_section
4326 (desc->mode, desc->constant, desc->align));
4327 output_constant_pool_1 (desc, desc->align);
4332 struct constant_descriptor_rtx_data {
4333 constant_descriptor_rtx *desc;
4334 target_unit *bytes;
4335 unsigned short size;
4336 unsigned short offset;
4337 unsigned int hash;
4340 /* qsort callback to sort constant_descriptor_rtx_data * vector by
4341 decreasing size. */
4343 static int
4344 constant_descriptor_rtx_data_cmp (const void *p1, const void *p2)
4346 constant_descriptor_rtx_data *const data1
4347 = *(constant_descriptor_rtx_data * const *) p1;
4348 constant_descriptor_rtx_data *const data2
4349 = *(constant_descriptor_rtx_data * const *) p2;
4350 if (data1->size > data2->size)
4351 return -1;
4352 if (data1->size < data2->size)
4353 return 1;
4354 if (data1->hash < data2->hash)
4355 return -1;
4356 gcc_assert (data1->hash > data2->hash);
4357 return 1;
4360 struct const_rtx_data_hasher : nofree_ptr_hash<constant_descriptor_rtx_data>
4362 static hashval_t hash (constant_descriptor_rtx_data *);
4363 static bool equal (constant_descriptor_rtx_data *,
4364 constant_descriptor_rtx_data *);
4367 /* Hash and compare functions for const_rtx_data_htab. */
4369 hashval_t
4370 const_rtx_data_hasher::hash (constant_descriptor_rtx_data *data)
4372 return data->hash;
4375 bool
4376 const_rtx_data_hasher::equal (constant_descriptor_rtx_data *x,
4377 constant_descriptor_rtx_data *y)
4379 if (x->hash != y->hash || x->size != y->size)
4380 return false;
4381 unsigned int align1 = x->desc->align;
4382 unsigned int align2 = y->desc->align;
4383 unsigned int offset1 = (x->offset * BITS_PER_UNIT) & (align1 - 1);
4384 unsigned int offset2 = (y->offset * BITS_PER_UNIT) & (align2 - 1);
4385 if (offset1)
4386 align1 = least_bit_hwi (offset1);
4387 if (offset2)
4388 align2 = least_bit_hwi (offset2);
4389 if (align2 > align1)
4390 return false;
4391 if (memcmp (x->bytes, y->bytes, x->size * sizeof (target_unit)) != 0)
4392 return false;
4393 return true;
4396 /* Attempt to optimize constant pool POOL. If it contains both CONST_VECTOR
4397 constants and scalar constants with the values of CONST_VECTOR elements,
4398 try to alias the scalar constants with the CONST_VECTOR elements. */
4400 static void
4401 optimize_constant_pool (struct rtx_constant_pool *pool)
4403 auto_vec<target_unit, 128> buffer;
4404 auto_vec<constant_descriptor_rtx_data *, 128> vec;
4405 object_allocator<constant_descriptor_rtx_data>
4406 data_pool ("constant_descriptor_rtx_data_pool");
4407 int idx = 0;
4408 size_t size = 0;
4409 for (constant_descriptor_rtx *desc = pool->first; desc; desc = desc->next)
4410 if (desc->mark > 0
4411 && ! (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4412 && SYMBOL_REF_BLOCK (desc->sym)))
4414 buffer.truncate (0);
4415 buffer.reserve (GET_MODE_SIZE (desc->mode));
4416 if (native_encode_rtx (desc->mode, desc->constant, buffer, 0,
4417 GET_MODE_SIZE (desc->mode)))
4419 constant_descriptor_rtx_data *data = data_pool.allocate ();
4420 data->desc = desc;
4421 data->bytes = NULL;
4422 data->size = GET_MODE_SIZE (desc->mode);
4423 data->offset = 0;
4424 data->hash = idx++;
4425 size += data->size;
4426 vec.safe_push (data);
4429 if (idx)
4431 vec.qsort (constant_descriptor_rtx_data_cmp);
4432 unsigned min_size = vec.last ()->size;
4433 target_unit *bytes = XNEWVEC (target_unit, size);
4434 unsigned int i;
4435 constant_descriptor_rtx_data *data;
4436 hash_table<const_rtx_data_hasher> * htab
4437 = new hash_table<const_rtx_data_hasher> (31);
4438 size = 0;
4439 FOR_EACH_VEC_ELT (vec, i, data)
4441 buffer.truncate (0);
4442 native_encode_rtx (data->desc->mode, data->desc->constant,
4443 buffer, 0, data->size);
4444 memcpy (bytes + size, buffer.address (), data->size);
4445 data->bytes = bytes + size;
4446 data->hash = iterative_hash (data->bytes,
4447 data->size * sizeof (target_unit), 0);
4448 size += data->size;
4449 constant_descriptor_rtx_data **slot
4450 = htab->find_slot_with_hash (data, data->hash, INSERT);
4451 if (*slot)
4453 data->desc->mark = ~(*slot)->desc->labelno;
4454 data->desc->offset = (*slot)->offset;
4456 else
4458 unsigned int sz = 1 << floor_log2 (data->size);
4460 *slot = data;
4461 for (sz >>= 1; sz >= min_size; sz >>= 1)
4462 for (unsigned off = 0; off + sz <= data->size; off += sz)
4464 constant_descriptor_rtx_data tmp;
4465 tmp.desc = data->desc;
4466 tmp.bytes = data->bytes + off;
4467 tmp.size = sz;
4468 tmp.offset = off;
4469 tmp.hash = iterative_hash (tmp.bytes,
4470 sz * sizeof (target_unit), 0);
4471 slot = htab->find_slot_with_hash (&tmp, tmp.hash, INSERT);
4472 if (*slot == NULL)
4474 *slot = data_pool.allocate ();
4475 **slot = tmp;
4480 delete htab;
4481 XDELETE (bytes);
4483 data_pool.release ();
4486 /* Mark all constants that are used in the current function, then write
4487 out the function's private constant pool. */
4489 static void
4490 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4491 tree fndecl ATTRIBUTE_UNUSED)
4493 struct rtx_constant_pool *pool = crtl->varasm.pool;
4495 /* It is possible for gcc to call force_const_mem and then to later
4496 discard the instructions which refer to the constant. In such a
4497 case we do not need to output the constant. */
4498 mark_constant_pool ();
4500 /* Having marked the constant pool entries we'll actually emit, we
4501 now need to rebuild the offset information, which may have become
4502 stale. */
4503 recompute_pool_offsets (pool);
4505 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4506 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4507 #endif
4509 output_constant_pool_contents (pool);
4511 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4512 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4513 #endif
4516 /* Write the contents of the shared constant pool. */
4518 void
4519 output_shared_constant_pool (void)
4521 if (optimize
4522 && TARGET_SUPPORTS_ALIASES)
4523 optimize_constant_pool (shared_constant_pool);
4525 output_constant_pool_contents (shared_constant_pool);
4528 /* Determine what kind of relocations EXP may need. */
4531 compute_reloc_for_constant (tree exp)
4533 int reloc = 0, reloc2;
4534 tree tem;
4536 switch (TREE_CODE (exp))
4538 case ADDR_EXPR:
4539 case FDESC_EXPR:
4540 /* Go inside any operations that get_inner_reference can handle and see
4541 if what's inside is a constant: no need to do anything here for
4542 addresses of variables or functions. */
4543 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4544 tem = TREE_OPERAND (tem, 0))
4547 if (TREE_CODE (tem) == MEM_REF
4548 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4550 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4551 break;
4554 if (!targetm.binds_local_p (tem))
4555 reloc |= 2;
4556 else
4557 reloc |= 1;
4558 break;
4560 case PLUS_EXPR:
4561 case POINTER_PLUS_EXPR:
4562 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4563 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4564 break;
4566 case MINUS_EXPR:
4567 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4568 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4569 /* The difference of two local labels is computable at link time. */
4570 if (reloc == 1 && reloc2 == 1)
4571 reloc = 0;
4572 else
4573 reloc |= reloc2;
4574 break;
4576 CASE_CONVERT:
4577 case VIEW_CONVERT_EXPR:
4578 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4579 break;
4581 case CONSTRUCTOR:
4583 unsigned HOST_WIDE_INT idx;
4584 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4585 if (tem != 0)
4586 reloc |= compute_reloc_for_constant (tem);
4588 break;
4590 default:
4591 break;
4593 return reloc;
4596 /* Find all the constants whose addresses are referenced inside of EXP,
4597 and make sure assembler code with a label has been output for each one.
4598 Indicate whether an ADDR_EXPR has been encountered. */
4600 static void
4601 output_addressed_constants (tree exp, int defer)
4603 tree tem;
4605 switch (TREE_CODE (exp))
4607 case ADDR_EXPR:
4608 case FDESC_EXPR:
4609 /* Go inside any operations that get_inner_reference can handle and see
4610 if what's inside is a constant: no need to do anything here for
4611 addresses of variables or functions. */
4612 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4613 tem = TREE_OPERAND (tem, 0))
4616 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4617 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4618 tem = DECL_INITIAL (tem);
4620 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4621 output_constant_def (tem, defer);
4623 if (TREE_CODE (tem) == MEM_REF)
4624 output_addressed_constants (TREE_OPERAND (tem, 0), defer);
4625 break;
4627 case PLUS_EXPR:
4628 case POINTER_PLUS_EXPR:
4629 case MINUS_EXPR:
4630 output_addressed_constants (TREE_OPERAND (exp, 1), defer);
4631 gcc_fallthrough ();
4633 CASE_CONVERT:
4634 case VIEW_CONVERT_EXPR:
4635 output_addressed_constants (TREE_OPERAND (exp, 0), defer);
4636 break;
4638 case CONSTRUCTOR:
4640 unsigned HOST_WIDE_INT idx;
4641 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4642 if (tem != 0)
4643 output_addressed_constants (tem, defer);
4645 break;
4647 default:
4648 break;
4652 /* Whether a constructor CTOR is a valid static constant initializer if all
4653 its elements are. This used to be internal to initializer_constant_valid_p
4654 and has been exposed to let other functions like categorize_ctor_elements
4655 evaluate the property while walking a constructor for other purposes. */
4657 bool
4658 constructor_static_from_elts_p (const_tree ctor)
4660 return (TREE_CONSTANT (ctor)
4661 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4662 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4663 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4666 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4667 tree *cache);
4669 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4670 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4671 which are valid when ENDTYPE is an integer of any size; in
4672 particular, this does not accept a pointer minus a constant. This
4673 returns null_pointer_node if the VALUE is an absolute constant
4674 which can be used to initialize a static variable. Otherwise it
4675 returns NULL. */
4677 static tree
4678 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4680 tree op0, op1;
4682 if (!INTEGRAL_TYPE_P (endtype))
4683 return NULL_TREE;
4685 op0 = TREE_OPERAND (value, 0);
4686 op1 = TREE_OPERAND (value, 1);
4688 /* Like STRIP_NOPS except allow the operand mode to widen. This
4689 works around a feature of fold that simplifies (int)(p1 - p2) to
4690 ((int)p1 - (int)p2) under the theory that the narrower operation
4691 is cheaper. */
4693 while (CONVERT_EXPR_P (op0)
4694 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4696 tree inner = TREE_OPERAND (op0, 0);
4697 if (inner == error_mark_node
4698 || ! INTEGRAL_TYPE_P (TREE_TYPE (op0))
4699 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (op0)))
4700 || ! INTEGRAL_TYPE_P (TREE_TYPE (inner))
4701 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4702 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4703 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4704 break;
4705 op0 = inner;
4708 while (CONVERT_EXPR_P (op1)
4709 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4711 tree inner = TREE_OPERAND (op1, 0);
4712 if (inner == error_mark_node
4713 || ! INTEGRAL_TYPE_P (TREE_TYPE (op1))
4714 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (op1)))
4715 || ! INTEGRAL_TYPE_P (TREE_TYPE (inner))
4716 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4717 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4718 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4719 break;
4720 op1 = inner;
4723 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4724 if (!op0)
4725 return NULL_TREE;
4727 op1 = initializer_constant_valid_p_1 (op1, endtype,
4728 cache ? cache + 2 : NULL);
4729 /* Both initializers must be known. */
4730 if (op1)
4732 if (op0 == op1
4733 && (op0 == null_pointer_node
4734 || TREE_CODE (value) == MINUS_EXPR))
4735 return null_pointer_node;
4737 /* Support differences between labels. */
4738 if (TREE_CODE (op0) == LABEL_DECL
4739 && TREE_CODE (op1) == LABEL_DECL)
4740 return null_pointer_node;
4742 if (TREE_CODE (op0) == STRING_CST
4743 && TREE_CODE (op1) == STRING_CST
4744 && operand_equal_p (op0, op1, 1))
4745 return null_pointer_node;
4748 return NULL_TREE;
4751 /* Helper function of initializer_constant_valid_p.
4752 Return nonzero if VALUE is a valid constant-valued expression
4753 for use in initializing a static variable; one that can be an
4754 element of a "constant" initializer.
4756 Return null_pointer_node if the value is absolute;
4757 if it is relocatable, return the variable that determines the relocation.
4758 We assume that VALUE has been folded as much as possible;
4759 therefore, we do not need to check for such things as
4760 arithmetic-combinations of integers.
4762 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4764 static tree
4765 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4767 tree ret;
4769 switch (TREE_CODE (value))
4771 case CONSTRUCTOR:
4772 if (constructor_static_from_elts_p (value))
4774 unsigned HOST_WIDE_INT idx;
4775 tree elt;
4776 bool absolute = true;
4778 if (cache && cache[0] == value)
4779 return cache[1];
4780 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4782 tree reloc;
4783 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4784 NULL);
4785 if (!reloc
4786 /* An absolute value is required with reverse SSO. */
4787 || (reloc != null_pointer_node
4788 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4789 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4791 if (cache)
4793 cache[0] = value;
4794 cache[1] = NULL_TREE;
4796 return NULL_TREE;
4798 if (reloc != null_pointer_node)
4799 absolute = false;
4801 /* For a non-absolute relocation, there is no single
4802 variable that can be "the variable that determines the
4803 relocation." */
4804 if (cache)
4806 cache[0] = value;
4807 cache[1] = absolute ? null_pointer_node : error_mark_node;
4809 return absolute ? null_pointer_node : error_mark_node;
4812 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4814 case INTEGER_CST:
4815 case VECTOR_CST:
4816 case REAL_CST:
4817 case FIXED_CST:
4818 case STRING_CST:
4819 case COMPLEX_CST:
4820 return null_pointer_node;
4822 case ADDR_EXPR:
4823 case FDESC_EXPR:
4825 tree op0 = staticp (TREE_OPERAND (value, 0));
4826 if (op0)
4828 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4829 to be a constant, this is old-skool offsetof-like nonsense. */
4830 if (TREE_CODE (op0) == INDIRECT_REF
4831 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4832 return null_pointer_node;
4833 /* Taking the address of a nested function involves a trampoline,
4834 unless we don't need or want one. */
4835 if (TREE_CODE (op0) == FUNCTION_DECL
4836 && DECL_STATIC_CHAIN (op0)
4837 && !TREE_NO_TRAMPOLINE (value))
4838 return NULL_TREE;
4839 /* "&{...}" requires a temporary to hold the constructed
4840 object. */
4841 if (TREE_CODE (op0) == CONSTRUCTOR)
4842 return NULL_TREE;
4844 return op0;
4847 case NON_LVALUE_EXPR:
4848 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4849 endtype, cache);
4851 case VIEW_CONVERT_EXPR:
4853 tree src = TREE_OPERAND (value, 0);
4854 tree src_type = TREE_TYPE (src);
4855 tree dest_type = TREE_TYPE (value);
4857 /* Allow view-conversions from aggregate to non-aggregate type only
4858 if the bit pattern is fully preserved afterwards; otherwise, the
4859 RTL expander won't be able to apply a subsequent transformation
4860 to the underlying constructor. */
4861 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4863 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4864 return initializer_constant_valid_p_1 (src, endtype, cache);
4865 else
4866 return NULL_TREE;
4869 /* Allow all other kinds of view-conversion. */
4870 return initializer_constant_valid_p_1 (src, endtype, cache);
4873 CASE_CONVERT:
4875 tree src = TREE_OPERAND (value, 0);
4876 tree src_type = TREE_TYPE (src);
4877 tree dest_type = TREE_TYPE (value);
4879 /* Allow conversions between pointer types and offset types. */
4880 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4881 || (TREE_CODE (dest_type) == OFFSET_TYPE
4882 && TREE_CODE (src_type) == OFFSET_TYPE))
4883 return initializer_constant_valid_p_1 (src, endtype, cache);
4885 /* Allow length-preserving conversions between integer types and
4886 floating-point types. */
4887 if (((INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4888 || (SCALAR_FLOAT_TYPE_P (dest_type)
4889 && SCALAR_FLOAT_TYPE_P (src_type)))
4890 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4891 return initializer_constant_valid_p_1 (src, endtype, cache);
4893 /* Allow conversions between other integer types only if
4894 explicit value. Don't allow sign-extension to a type larger
4895 than word and pointer, there aren't relocations that would
4896 allow to sign extend it to a wider type. */
4897 if (INTEGRAL_TYPE_P (dest_type)
4898 && INTEGRAL_TYPE_P (src_type)
4899 && (TYPE_UNSIGNED (src_type)
4900 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4901 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4902 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4904 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4905 if (inner == null_pointer_node)
4906 return null_pointer_node;
4907 break;
4910 /* Allow (int) &foo provided int is as wide as a pointer. */
4911 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4912 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4913 return initializer_constant_valid_p_1 (src, endtype, cache);
4915 /* Likewise conversions from int to pointers, but also allow
4916 conversions from 0. */
4917 if ((POINTER_TYPE_P (dest_type)
4918 || TREE_CODE (dest_type) == OFFSET_TYPE)
4919 && INTEGRAL_TYPE_P (src_type))
4921 if (TREE_CODE (src) == INTEGER_CST
4922 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4923 return null_pointer_node;
4924 if (integer_zerop (src))
4925 return null_pointer_node;
4926 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4927 return initializer_constant_valid_p_1 (src, endtype, cache);
4930 /* Allow conversions to struct or union types if the value
4931 inside is okay. */
4932 if (TREE_CODE (dest_type) == RECORD_TYPE
4933 || TREE_CODE (dest_type) == UNION_TYPE)
4934 return initializer_constant_valid_p_1 (src, endtype, cache);
4936 break;
4938 case POINTER_PLUS_EXPR:
4939 case PLUS_EXPR:
4940 /* Any valid floating-point constants will have been folded by now;
4941 with -frounding-math we hit this with addition of two constants. */
4942 if (TREE_CODE (endtype) == REAL_TYPE)
4943 return NULL_TREE;
4944 if (cache && cache[0] == value)
4945 return cache[1];
4946 if (! INTEGRAL_TYPE_P (endtype)
4947 || ! INTEGRAL_TYPE_P (TREE_TYPE (value))
4948 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4950 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4951 tree valid0
4952 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4953 endtype, ncache);
4954 tree valid1
4955 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4956 endtype, ncache + 2);
4957 /* If either term is absolute, use the other term's relocation. */
4958 if (valid0 == null_pointer_node)
4959 ret = valid1;
4960 else if (valid1 == null_pointer_node)
4961 ret = valid0;
4962 /* Support narrowing pointer differences. */
4963 else
4964 ret = narrowing_initializer_constant_valid_p (value, endtype,
4965 ncache);
4967 else
4968 /* Support narrowing pointer differences. */
4969 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4970 if (cache)
4972 cache[0] = value;
4973 cache[1] = ret;
4975 return ret;
4977 case POINTER_DIFF_EXPR:
4978 case MINUS_EXPR:
4979 if (TREE_CODE (endtype) == REAL_TYPE)
4980 return NULL_TREE;
4981 if (cache && cache[0] == value)
4982 return cache[1];
4983 if (! INTEGRAL_TYPE_P (endtype)
4984 || ! INTEGRAL_TYPE_P (TREE_TYPE (value))
4985 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4987 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4988 tree valid0
4989 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4990 endtype, ncache);
4991 tree valid1
4992 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4993 endtype, ncache + 2);
4994 /* Win if second argument is absolute. */
4995 if (valid1 == null_pointer_node)
4996 ret = valid0;
4997 /* Win if both arguments have the same relocation.
4998 Then the value is absolute. */
4999 else if (valid0 == valid1 && valid0 != 0)
5000 ret = null_pointer_node;
5001 /* Since GCC guarantees that string constants are unique in the
5002 generated code, a subtraction between two copies of the same
5003 constant string is absolute. */
5004 else if (valid0 && TREE_CODE (valid0) == STRING_CST
5005 && valid1 && TREE_CODE (valid1) == STRING_CST
5006 && operand_equal_p (valid0, valid1, 1))
5007 ret = null_pointer_node;
5008 /* Support narrowing differences. */
5009 else
5010 ret = narrowing_initializer_constant_valid_p (value, endtype,
5011 ncache);
5013 else
5014 /* Support narrowing differences. */
5015 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
5016 if (cache)
5018 cache[0] = value;
5019 cache[1] = ret;
5021 return ret;
5023 default:
5024 break;
5027 return NULL_TREE;
5030 /* Return nonzero if VALUE is a valid constant-valued expression
5031 for use in initializing a static variable; one that can be an
5032 element of a "constant" initializer.
5034 Return null_pointer_node if the value is absolute;
5035 if it is relocatable, return the variable that determines the relocation.
5036 We assume that VALUE has been folded as much as possible;
5037 therefore, we do not need to check for such things as
5038 arithmetic-combinations of integers. */
5039 tree
5040 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
5042 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
5044 /* An absolute value is required with reverse storage order. */
5045 if (reloc
5046 && reloc != null_pointer_node
5047 && reverse
5048 && !AGGREGATE_TYPE_P (endtype)
5049 && !VECTOR_TYPE_P (endtype))
5050 reloc = NULL_TREE;
5052 return reloc;
5055 /* Return true if VALUE is a valid constant-valued expression
5056 for use in initializing a static bit-field; one that can be
5057 an element of a "constant" initializer. */
5059 bool
5060 initializer_constant_valid_for_bitfield_p (const_tree value)
5062 /* For bitfields we support integer constants or possibly nested aggregates
5063 of such. */
5064 switch (TREE_CODE (value))
5066 case CONSTRUCTOR:
5068 unsigned HOST_WIDE_INT idx;
5069 const_tree elt;
5071 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
5072 if (!initializer_constant_valid_for_bitfield_p (elt))
5073 return false;
5074 return true;
5077 case INTEGER_CST:
5078 case REAL_CST:
5079 return true;
5081 case VIEW_CONVERT_EXPR:
5082 case NON_LVALUE_EXPR:
5083 return
5084 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
5086 default:
5087 break;
5090 return false;
5093 /* Check if a STRING_CST fits into the field.
5094 Tolerate only the case when the NUL termination
5095 does not fit into the field. */
5097 static bool
5098 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
5100 tree type = TREE_TYPE (string);
5101 tree eltype = TREE_TYPE (type);
5102 unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
5103 unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5104 int len = TREE_STRING_LENGTH (string);
5106 if (elts != 1 && elts != 2 && elts != 4)
5107 return false;
5108 if (len < 0 || len % elts != 0)
5109 return false;
5110 if (size < (unsigned)len)
5111 return false;
5112 if (mem_size != size)
5113 return false;
5114 return true;
5117 /* output_constructor outer state of relevance in recursive calls, typically
5118 for nested aggregate bitfields. */
5120 struct oc_outer_state {
5121 unsigned int bit_offset; /* current position in ... */
5122 int byte; /* ... the outer byte buffer. */
5125 static unsigned HOST_WIDE_INT
5126 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
5127 oc_outer_state *);
5129 /* Output assembler code for constant EXP, with no label.
5130 This includes the pseudo-op such as ".int" or ".byte", and a newline.
5131 Assumes output_addressed_constants has been done on EXP already.
5133 Generate at least SIZE bytes of assembler data, padding at the end
5134 with zeros if necessary. SIZE must always be specified. The returned
5135 value is the actual number of bytes of assembler data generated, which
5136 may be bigger than SIZE if the object contains a variable length field.
5138 SIZE is important for structure constructors,
5139 since trailing members may have been omitted from the constructor.
5140 It is also important for initialization of arrays from string constants
5141 since the full length of the string constant might not be wanted.
5142 It is also needed for initialization of unions, where the initializer's
5143 type is just one member, and that may not be as long as the union.
5145 There a case in which we would fail to output exactly SIZE bytes:
5146 for a structure constructor that wants to produce more than SIZE bytes.
5147 But such constructors will never be generated for any possible input.
5149 ALIGN is the alignment of the data in bits.
5151 If REVERSE is true, EXP is output in reverse storage order. */
5153 static unsigned HOST_WIDE_INT
5154 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5155 bool reverse, bool merge_strings)
5157 enum tree_code code;
5158 unsigned HOST_WIDE_INT thissize;
5159 rtx cst;
5161 if (size == 0 || flag_syntax_only)
5162 return size;
5164 /* See if we're trying to initialize a pointer in a non-default mode
5165 to the address of some declaration somewhere. If the target says
5166 the mode is valid for pointers, assume the target has a way of
5167 resolving it. */
5168 if (TREE_CODE (exp) == NOP_EXPR
5169 && POINTER_TYPE_P (TREE_TYPE (exp))
5170 && targetm.addr_space.valid_pointer_mode
5171 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5172 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5174 tree saved_type = TREE_TYPE (exp);
5176 /* Peel off any intermediate conversions-to-pointer for valid
5177 pointer modes. */
5178 while (TREE_CODE (exp) == NOP_EXPR
5179 && POINTER_TYPE_P (TREE_TYPE (exp))
5180 && targetm.addr_space.valid_pointer_mode
5181 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5182 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5183 exp = TREE_OPERAND (exp, 0);
5185 /* If what we're left with is the address of something, we can
5186 convert the address to the final type and output it that
5187 way. */
5188 if (TREE_CODE (exp) == ADDR_EXPR)
5189 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
5190 /* Likewise for constant ints. */
5191 else if (TREE_CODE (exp) == INTEGER_CST)
5192 exp = fold_convert (saved_type, exp);
5196 /* Eliminate any conversions since we'll be outputting the underlying
5197 constant. */
5198 while (CONVERT_EXPR_P (exp)
5199 || TREE_CODE (exp) == NON_LVALUE_EXPR
5200 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
5202 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
5203 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
5205 /* Make sure eliminating the conversion is really a no-op, except with
5206 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
5207 union types to allow for Ada unchecked unions. */
5208 if (type_size > op_size
5209 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
5210 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
5211 /* Keep the conversion. */
5212 break;
5213 else
5214 exp = TREE_OPERAND (exp, 0);
5217 code = TREE_CODE (TREE_TYPE (exp));
5218 thissize = int_size_in_bytes (TREE_TYPE (exp));
5220 /* Allow a constructor with no elements for any data type.
5221 This means to fill the space with zeros. */
5222 if (TREE_CODE (exp) == CONSTRUCTOR
5223 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
5225 assemble_zeros (size);
5226 return size;
5229 if (TREE_CODE (exp) == FDESC_EXPR)
5231 #ifdef ASM_OUTPUT_FDESC
5232 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
5233 tree decl = TREE_OPERAND (exp, 0);
5234 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
5235 #else
5236 gcc_unreachable ();
5237 #endif
5238 return size;
5241 /* Now output the underlying data. If we've handling the padding, return.
5242 Otherwise, break and ensure SIZE is the size written. */
5243 switch (code)
5245 case BOOLEAN_TYPE:
5246 case INTEGER_TYPE:
5247 case ENUMERAL_TYPE:
5248 case POINTER_TYPE:
5249 case REFERENCE_TYPE:
5250 case OFFSET_TYPE:
5251 case FIXED_POINT_TYPE:
5252 case NULLPTR_TYPE:
5253 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5254 if (reverse)
5255 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5256 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5257 error ("initializer for integer/fixed-point value is too complicated");
5258 break;
5260 case REAL_TYPE:
5261 gcc_assert (size == thissize);
5262 if (TREE_CODE (exp) != REAL_CST)
5263 error ("initializer for floating value is not a floating constant");
5264 else
5265 assemble_real (TREE_REAL_CST (exp),
5266 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
5267 align, reverse);
5268 break;
5270 case COMPLEX_TYPE:
5271 output_constant (TREE_REALPART (exp), thissize / 2, align,
5272 reverse, false);
5273 output_constant (TREE_IMAGPART (exp), thissize / 2,
5274 min_align (align, BITS_PER_UNIT * (thissize / 2)),
5275 reverse, false);
5276 break;
5278 case ARRAY_TYPE:
5279 case VECTOR_TYPE:
5280 switch (TREE_CODE (exp))
5282 case CONSTRUCTOR:
5283 return output_constructor (exp, size, align, reverse, NULL);
5284 case STRING_CST:
5285 thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5286 if (merge_strings
5287 && (thissize == 0
5288 || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5289 thissize++;
5290 gcc_checking_assert (check_string_literal (exp, size));
5291 assemble_string (TREE_STRING_POINTER (exp), thissize);
5292 break;
5293 case VECTOR_CST:
5295 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5296 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5297 int elt_size = GET_MODE_SIZE (inner);
5298 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5299 reverse, false);
5300 thissize = elt_size;
5301 /* Static constants must have a fixed size. */
5302 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5303 for (unsigned int i = 1; i < nunits; i++)
5305 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5306 reverse, false);
5307 thissize += elt_size;
5309 break;
5311 default:
5312 gcc_unreachable ();
5314 break;
5316 case RECORD_TYPE:
5317 case UNION_TYPE:
5318 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5319 return output_constructor (exp, size, align, reverse, NULL);
5321 case ERROR_MARK:
5322 return 0;
5324 default:
5325 gcc_unreachable ();
5328 if (size > thissize)
5329 assemble_zeros (size - thissize);
5331 return size;
5334 /* Subroutine of output_constructor, used for computing the size of
5335 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
5336 type with an unspecified upper bound. */
5338 static unsigned HOST_WIDE_INT
5339 array_size_for_constructor (tree val)
5341 tree max_index;
5342 unsigned HOST_WIDE_INT cnt;
5343 tree index, value, tmp;
5344 offset_int i;
5346 /* This code used to attempt to handle string constants that are not
5347 arrays of single-bytes, but nothing else does, so there's no point in
5348 doing it here. */
5349 if (TREE_CODE (val) == STRING_CST)
5350 return TREE_STRING_LENGTH (val);
5352 max_index = NULL_TREE;
5353 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5355 if (TREE_CODE (index) == RANGE_EXPR)
5356 index = TREE_OPERAND (index, 1);
5357 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5358 max_index = index;
5361 if (max_index == NULL_TREE)
5362 return 0;
5364 /* Compute the total number of array elements. */
5365 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5366 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5368 /* Multiply by the array element unit size to find number of bytes. */
5369 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5371 gcc_assert (wi::fits_uhwi_p (i));
5372 return i.to_uhwi ();
5375 /* Other datastructures + helpers for output_constructor. */
5377 /* output_constructor local state to support interaction with helpers. */
5379 struct oc_local_state {
5381 /* Received arguments. */
5382 tree exp; /* Constructor expression. */
5383 tree type; /* Type of constructor expression. */
5384 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5385 unsigned int align; /* Known initial alignment. */
5386 tree min_index; /* Lower bound if specified for an array. */
5388 /* Output processing state. */
5389 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5390 int byte; /* Part of a bitfield byte yet to be output. */
5391 int last_relative_index; /* Implicit or explicit index of the last
5392 array element output within a bitfield. */
5393 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5394 bool reverse; /* Whether reverse storage order is in use. */
5396 /* Current element. */
5397 tree field; /* Current field decl in a record. */
5398 tree val; /* Current element value. */
5399 tree index; /* Current element index. */
5403 /* Helper for output_constructor. From the current LOCAL state, output a
5404 RANGE_EXPR element. */
5406 static void
5407 output_constructor_array_range (oc_local_state *local)
5409 /* Perform the index calculation in modulo arithmetic but
5410 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5411 but we are using an unsigned sizetype. */
5412 unsigned prec = TYPE_PRECISION (sizetype);
5413 offset_int idx = wi::sext (wi::to_offset (TREE_OPERAND (local->index, 0))
5414 - wi::to_offset (local->min_index), prec);
5415 tree valtype = TREE_TYPE (local->val);
5416 HOST_WIDE_INT fieldpos
5417 = (idx * wi::to_offset (TYPE_SIZE_UNIT (valtype))).to_short_addr ();
5419 /* Advance to offset of this element. */
5420 if (fieldpos > local->total_bytes)
5422 assemble_zeros (fieldpos - local->total_bytes);
5423 local->total_bytes = fieldpos;
5425 else
5426 /* Must not go backwards. */
5427 gcc_assert (fieldpos == local->total_bytes);
5429 unsigned HOST_WIDE_INT fieldsize
5430 = int_size_in_bytes (TREE_TYPE (local->type));
5432 HOST_WIDE_INT lo_index
5433 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5434 HOST_WIDE_INT hi_index
5435 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5436 HOST_WIDE_INT index;
5438 unsigned int align2
5439 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5441 for (index = lo_index; index <= hi_index; index++)
5443 /* Output the element's initial value. */
5444 if (local->val == NULL_TREE)
5445 assemble_zeros (fieldsize);
5446 else
5447 fieldsize = output_constant (local->val, fieldsize, align2,
5448 local->reverse, false);
5450 /* Count its size. */
5451 local->total_bytes += fieldsize;
5455 /* Helper for output_constructor. From the current LOCAL state, output a
5456 field element that is not true bitfield or part of an outer one. */
5458 static void
5459 output_constructor_regular_field (oc_local_state *local)
5461 /* Field size and position. Since this structure is static, we know the
5462 positions are constant. */
5463 unsigned HOST_WIDE_INT fieldsize;
5464 HOST_WIDE_INT fieldpos;
5466 unsigned int align2;
5468 /* Output any buffered-up bit-fields preceding this element. */
5469 if (local->byte_buffer_in_use)
5471 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5472 local->total_bytes++;
5473 local->byte_buffer_in_use = false;
5476 if (local->index != NULL_TREE)
5478 /* Perform the index calculation in modulo arithmetic but
5479 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5480 but we are using an unsigned sizetype. */
5481 unsigned prec = TYPE_PRECISION (sizetype);
5482 offset_int idx = wi::sext (wi::to_offset (local->index)
5483 - wi::to_offset (local->min_index), prec);
5484 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5485 .to_short_addr ();
5487 else if (local->field != NULL_TREE)
5488 fieldpos = int_byte_position (local->field);
5489 else
5490 fieldpos = 0;
5492 /* Advance to offset of this element.
5493 Note no alignment needed in an array, since that is guaranteed
5494 if each element has the proper size. */
5495 if (local->field != NULL_TREE || local->index != NULL_TREE)
5497 if (fieldpos > local->total_bytes)
5499 assemble_zeros (fieldpos - local->total_bytes);
5500 local->total_bytes = fieldpos;
5502 else
5503 /* Must not go backwards. */
5504 gcc_assert (fieldpos == local->total_bytes);
5507 /* Find the alignment of this element. */
5508 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5510 /* Determine size this element should occupy. */
5511 if (local->field)
5513 fieldsize = 0;
5515 /* If this is an array with an unspecified upper bound,
5516 the initializer determines the size. */
5517 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5518 but we cannot do this until the deprecated support for
5519 initializing zero-length array members is removed. */
5520 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5521 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5522 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5524 unsigned HOST_WIDE_INT fldsize
5525 = array_size_for_constructor (local->val);
5526 fieldsize = int_size_in_bytes (TREE_TYPE (local->val));
5527 /* In most cases fieldsize == fldsize as the size of the initializer
5528 determines how many elements the flexible array member has. For
5529 C++ fldsize can be smaller though, if the last or several last or
5530 all initializers of the flexible array member have side-effects
5531 and the FE splits them into dynamic initialization. */
5532 gcc_checking_assert (fieldsize >= fldsize);
5533 /* Given a non-empty initialization, this field had better
5534 be last. Given a flexible array member, the next field
5535 on the chain is a TYPE_DECL of the enclosing struct. */
5536 const_tree next = DECL_CHAIN (local->field);
5537 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5539 else
5540 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5542 else
5543 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5545 /* Output the element's initial value. */
5546 if (local->val == NULL_TREE)
5547 assemble_zeros (fieldsize);
5548 else
5549 fieldsize = output_constant (local->val, fieldsize, align2,
5550 local->reverse, false);
5552 /* Count its size. */
5553 local->total_bytes += fieldsize;
5556 /* Helper for output_constructor. From the LOCAL state, output an element
5557 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5558 from the start of a possibly ongoing outer byte buffer. */
5560 static void
5561 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5563 /* Bit size of this element. */
5564 HOST_WIDE_INT ebitsize
5565 = (local->field
5566 ? tree_to_uhwi (DECL_SIZE (local->field))
5567 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5569 /* Relative index of this element if this is an array component. */
5570 HOST_WIDE_INT relative_index
5571 = (local->field
5573 : (local->index
5574 ? tree_to_uhwi (local->index) - tree_to_uhwi (local->min_index)
5575 : local->last_relative_index + 1));
5577 /* Bit position of this element from the start of the containing
5578 constructor. */
5579 HOST_WIDE_INT constructor_relative_ebitpos
5580 = (local->field
5581 ? int_bit_position (local->field)
5582 : ebitsize * relative_index);
5584 /* Bit position of this element from the start of a possibly ongoing
5585 outer byte buffer. */
5586 HOST_WIDE_INT byte_relative_ebitpos
5587 = bit_offset + constructor_relative_ebitpos;
5589 /* From the start of a possibly ongoing outer byte buffer, offsets to
5590 the first bit of this element and to the first bit past the end of
5591 this element. */
5592 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5593 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5595 local->last_relative_index = relative_index;
5597 if (local->val == NULL_TREE)
5598 local->val = integer_zero_node;
5600 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5601 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5602 local->val = TREE_OPERAND (local->val, 0);
5604 if (TREE_CODE (local->val) != INTEGER_CST
5605 && TREE_CODE (local->val) != CONSTRUCTOR)
5607 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5608 return;
5611 /* If this field does not start in this (or next) byte, skip some bytes. */
5612 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5614 /* Output remnant of any bit field in previous bytes. */
5615 if (local->byte_buffer_in_use)
5617 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5618 local->total_bytes++;
5619 local->byte_buffer_in_use = false;
5622 /* If still not at proper byte, advance to there. */
5623 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5625 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5626 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5627 local->total_bytes = next_offset / BITS_PER_UNIT;
5631 /* Set up the buffer if necessary. */
5632 if (!local->byte_buffer_in_use)
5634 local->byte = 0;
5635 if (ebitsize > 0)
5636 local->byte_buffer_in_use = true;
5639 /* If this is nested constructor, recurse passing the bit offset and the
5640 pending data, then retrieve the new pending data afterwards. */
5641 if (TREE_CODE (local->val) == CONSTRUCTOR)
5643 oc_outer_state temp_state;
5644 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5645 temp_state.byte = local->byte;
5646 local->total_bytes
5647 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5648 local->byte = temp_state.byte;
5649 return;
5652 /* Otherwise, we must split the element into pieces that fall within
5653 separate bytes, and combine each byte with previous or following
5654 bit-fields. */
5655 while (next_offset < end_offset)
5657 int this_time;
5658 int shift;
5659 unsigned HOST_WIDE_INT value;
5660 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5661 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5663 /* Advance from byte to byte within this element when necessary. */
5664 while (next_byte != local->total_bytes)
5666 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5667 local->total_bytes++;
5668 local->byte = 0;
5671 /* Number of bits we can process at once (all part of the same byte). */
5672 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5673 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5675 /* For big-endian data, take the most significant bits (of the
5676 bits that are significant) first and put them into bytes from
5677 the most significant end. */
5678 shift = end_offset - next_offset - this_time;
5680 /* Don't try to take a bunch of bits that cross
5681 the word boundary in the INTEGER_CST. We can
5682 only select bits from one element. */
5683 if ((shift / HOST_BITS_PER_WIDE_INT)
5684 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5686 const int end = shift + this_time - 1;
5687 shift = end & -HOST_BITS_PER_WIDE_INT;
5688 this_time = end - shift + 1;
5691 /* Now get the bits we want to insert. */
5692 value = wi::extract_uhwi (wi::to_widest (local->val),
5693 shift, this_time);
5695 /* Get the result. This works only when:
5696 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5697 local->byte |= value << (BITS_PER_UNIT - this_time - next_bit);
5699 else
5701 /* On little-endian machines, take the least significant bits of
5702 the value first and pack them starting at the least significant
5703 bits of the bytes. */
5704 shift = next_offset - byte_relative_ebitpos;
5706 /* Don't try to take a bunch of bits that cross
5707 the word boundary in the INTEGER_CST. We can
5708 only select bits from one element. */
5709 if ((shift / HOST_BITS_PER_WIDE_INT)
5710 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5711 this_time
5712 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5714 /* Now get the bits we want to insert. */
5715 value = wi::extract_uhwi (wi::to_widest (local->val),
5716 shift, this_time);
5718 /* Get the result. This works only when:
5719 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5720 local->byte |= value << next_bit;
5723 next_offset += this_time;
5724 local->byte_buffer_in_use = true;
5728 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5729 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5730 caller output state of relevance in recursive invocations. */
5732 static unsigned HOST_WIDE_INT
5733 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5734 bool reverse, oc_outer_state *outer)
5736 unsigned HOST_WIDE_INT cnt;
5737 constructor_elt *ce;
5738 oc_local_state local;
5740 /* Setup our local state to communicate with helpers. */
5741 local.exp = exp;
5742 local.type = TREE_TYPE (exp);
5743 local.size = size;
5744 local.align = align;
5745 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5746 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5747 else
5748 local.min_index = integer_zero_node;
5750 local.total_bytes = 0;
5751 local.byte_buffer_in_use = outer != NULL;
5752 local.byte = outer ? outer->byte : 0;
5753 local.last_relative_index = -1;
5754 /* The storage order is specified for every aggregate type. */
5755 if (AGGREGATE_TYPE_P (local.type))
5756 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5757 else
5758 local.reverse = reverse;
5760 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5762 /* As CE goes through the elements of the constant, FIELD goes through the
5763 structure fields if the constant is a structure. If the constant is a
5764 union, we override this by getting the field from the TREE_LIST element.
5765 But the constant could also be an array. Then FIELD is zero.
5767 There is always a maximum of one element in the chain LINK for unions
5768 (even if the initializer in a source program incorrectly contains
5769 more one). */
5771 if (TREE_CODE (local.type) == RECORD_TYPE)
5772 local.field = TYPE_FIELDS (local.type);
5773 else
5774 local.field = NULL_TREE;
5776 for (cnt = 0;
5777 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5778 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5780 local.val = ce->value;
5781 local.index = NULL_TREE;
5783 /* The element in a union constructor specifies the proper field
5784 or index. */
5785 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5786 local.field = ce->index;
5788 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5789 local.index = ce->index;
5791 if (local.field && flag_verbose_asm)
5792 fprintf (asm_out_file, "%s %s:\n",
5793 ASM_COMMENT_START,
5794 DECL_NAME (local.field)
5795 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5796 : "<anonymous>");
5798 /* Eliminate the marker that makes a cast not be an lvalue. */
5799 if (local.val != NULL_TREE)
5800 STRIP_NOPS (local.val);
5802 /* Output the current element, using the appropriate helper ... */
5804 /* For an array slice not part of an outer bitfield. */
5805 if (!outer
5806 && local.index != NULL_TREE
5807 && TREE_CODE (local.index) == RANGE_EXPR)
5808 output_constructor_array_range (&local);
5810 /* For a field that is neither a true bitfield nor part of an outer one,
5811 known to be at least byte aligned and multiple-of-bytes long. */
5812 else if (!outer
5813 && (local.field == NULL_TREE
5814 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5815 output_constructor_regular_field (&local);
5817 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5818 supported for scalar fields, so we may need to convert first. */
5819 else
5821 if (TREE_CODE (local.val) == REAL_CST)
5822 local.val
5823 = fold_unary (VIEW_CONVERT_EXPR,
5824 build_nonstandard_integer_type
5825 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5826 local.val);
5827 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5831 /* If we are not at toplevel, save the pending data for our caller.
5832 Otherwise output the pending data and padding zeros as needed. */
5833 if (outer)
5834 outer->byte = local.byte;
5835 else
5837 if (local.byte_buffer_in_use)
5839 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5840 local.total_bytes++;
5843 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5845 assemble_zeros (local.size - local.total_bytes);
5846 local.total_bytes = local.size;
5850 return local.total_bytes;
5853 /* Mark DECL as weak. */
5855 static void
5856 mark_weak (tree decl)
5858 if (DECL_WEAK (decl))
5859 return;
5861 struct symtab_node *n = symtab_node::get (decl);
5862 if (n && n->refuse_visibility_changes)
5863 error ("%qD declared weak after being used", decl);
5864 DECL_WEAK (decl) = 1;
5866 if (DECL_RTL_SET_P (decl)
5867 && MEM_P (DECL_RTL (decl))
5868 && XEXP (DECL_RTL (decl), 0)
5869 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5870 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5873 /* Merge weak status between NEWDECL and OLDDECL. */
5875 void
5876 merge_weak (tree newdecl, tree olddecl)
5878 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5880 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5882 tree *pwd;
5883 /* We put the NEWDECL on the weak_decls list at some point
5884 and OLDDECL as well. Keep just OLDDECL on the list. */
5885 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5886 if (TREE_VALUE (*pwd) == newdecl)
5888 *pwd = TREE_CHAIN (*pwd);
5889 break;
5892 return;
5895 if (DECL_WEAK (newdecl))
5897 tree wd;
5899 /* NEWDECL is weak, but OLDDECL is not. */
5901 /* If we already output the OLDDECL, we're in trouble; we can't
5902 go back and make it weak. This should never happen in
5903 unit-at-a-time compilation. */
5904 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5906 /* If we've already generated rtl referencing OLDDECL, we may
5907 have done so in a way that will not function properly with
5908 a weak symbol. Again in unit-at-a-time this should be
5909 impossible. */
5910 gcc_assert (!TREE_USED (olddecl)
5911 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5913 /* PR 49899: You cannot convert a static function into a weak, public function. */
5914 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
5915 error ("weak declaration of %q+D being applied to a already "
5916 "existing, static definition", newdecl);
5918 if (TARGET_SUPPORTS_WEAK)
5920 /* We put the NEWDECL on the weak_decls list at some point.
5921 Replace it with the OLDDECL. */
5922 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5923 if (TREE_VALUE (wd) == newdecl)
5925 TREE_VALUE (wd) = olddecl;
5926 break;
5928 /* We may not find the entry on the list. If NEWDECL is a
5929 weak alias, then we will have already called
5930 globalize_decl to remove the entry; in that case, we do
5931 not need to do anything. */
5934 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5935 mark_weak (olddecl);
5937 else
5938 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5939 weak. Just update NEWDECL to indicate that it's weak too. */
5940 mark_weak (newdecl);
5943 /* Declare DECL to be a weak symbol. */
5945 void
5946 declare_weak (tree decl)
5948 /* With -fsyntax-only, TREE_ASM_WRITTEN might be set on certain function
5949 decls earlier than normally, but as with -fsyntax-only nothing is really
5950 emitted, there is no harm in marking it weak later. */
5951 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL
5952 || !TREE_ASM_WRITTEN (decl)
5953 || flag_syntax_only);
5954 if (! TREE_PUBLIC (decl))
5956 error ("weak declaration of %q+D must be public", decl);
5957 return;
5959 else if (!TARGET_SUPPORTS_WEAK)
5960 warning (0, "weak declaration of %q+D not supported", decl);
5962 mark_weak (decl);
5963 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5964 DECL_ATTRIBUTES (decl)
5965 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5968 static void
5969 weak_finish_1 (tree decl)
5971 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5972 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5973 #endif
5975 if (! TREE_USED (decl))
5976 return;
5978 #ifdef ASM_WEAKEN_DECL
5979 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5980 #else
5981 #ifdef ASM_WEAKEN_LABEL
5982 ASM_WEAKEN_LABEL (asm_out_file, name);
5983 #else
5984 #ifdef ASM_OUTPUT_WEAK_ALIAS
5986 static bool warn_once = 0;
5987 if (! warn_once)
5989 warning (0, "only weak aliases are supported in this configuration");
5990 warn_once = 1;
5992 return;
5994 #endif
5995 #endif
5996 #endif
5999 /* Fiven an assembly name, find the decl it is associated with. */
6000 static tree
6001 find_decl (tree target)
6003 symtab_node *node = symtab_node::get_for_asmname (target);
6004 if (node)
6005 return node->decl;
6006 return NULL_TREE;
6009 /* This TREE_LIST contains weakref targets. */
6011 static GTY(()) tree weakref_targets;
6013 /* Emit any pending weak declarations. */
6015 void
6016 weak_finish (void)
6018 tree t;
6020 for (t = weakref_targets; t; t = TREE_CHAIN (t))
6022 tree alias_decl = TREE_PURPOSE (t);
6023 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
6025 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
6026 || TREE_SYMBOL_REFERENCED (target))
6027 /* Remove alias_decl from the weak list, but leave entries for
6028 the target alone. */
6029 target = NULL_TREE;
6030 #ifndef ASM_OUTPUT_WEAKREF
6031 else if (! TREE_SYMBOL_REFERENCED (target))
6033 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
6034 defined, otherwise we and weak_finish_1 would use
6035 different macros. */
6036 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
6037 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
6038 # else
6039 tree decl = find_decl (target);
6041 if (! decl)
6043 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
6044 TREE_CODE (alias_decl), target,
6045 TREE_TYPE (alias_decl));
6047 DECL_EXTERNAL (decl) = 1;
6048 TREE_PUBLIC (decl) = 1;
6049 DECL_ARTIFICIAL (decl) = 1;
6050 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
6051 TREE_USED (decl) = 1;
6054 weak_finish_1 (decl);
6055 # endif
6057 #endif
6060 tree *p;
6061 tree t2;
6063 /* Remove the alias and the target from the pending weak list
6064 so that we do not emit any .weak directives for the former,
6065 nor multiple .weak directives for the latter. */
6066 for (p = &weak_decls; (t2 = *p) ; )
6068 if (TREE_VALUE (t2) == alias_decl
6069 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
6070 *p = TREE_CHAIN (t2);
6071 else
6072 p = &TREE_CHAIN (t2);
6075 /* Remove other weakrefs to the same target, to speed things up. */
6076 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
6078 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
6079 *p = TREE_CHAIN (t2);
6080 else
6081 p = &TREE_CHAIN (t2);
6086 for (t = weak_decls; t; t = TREE_CHAIN (t))
6088 tree decl = TREE_VALUE (t);
6090 weak_finish_1 (decl);
6094 /* Emit the assembly bits to indicate that DECL is globally visible. */
6096 static void
6097 globalize_decl (tree decl)
6100 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
6101 if (DECL_WEAK (decl))
6103 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6104 tree *p, t;
6106 #ifdef ASM_WEAKEN_DECL
6107 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
6108 #else
6109 ASM_WEAKEN_LABEL (asm_out_file, name);
6110 #endif
6112 /* Remove this function from the pending weak list so that
6113 we do not emit multiple .weak directives for it. */
6114 for (p = &weak_decls; (t = *p) ; )
6116 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6117 *p = TREE_CHAIN (t);
6118 else
6119 p = &TREE_CHAIN (t);
6122 /* Remove weakrefs to the same target from the pending weakref
6123 list, for the same reason. */
6124 for (p = &weakref_targets; (t = *p) ; )
6126 if (DECL_ASSEMBLER_NAME (decl)
6127 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6128 *p = TREE_CHAIN (t);
6129 else
6130 p = &TREE_CHAIN (t);
6133 return;
6135 #endif
6137 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
6140 vec<alias_pair, va_gc> *alias_pairs;
6142 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
6143 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
6144 tree node is DECL to have the value of the tree node TARGET. */
6146 void
6147 do_assemble_alias (tree decl, tree target)
6149 tree id;
6151 /* Emulated TLS had better not get this var. */
6152 gcc_assert (!(!targetm.have_tls
6153 && VAR_P (decl)
6154 && DECL_THREAD_LOCAL_P (decl)));
6156 if (TREE_ASM_WRITTEN (decl))
6157 return;
6159 id = DECL_ASSEMBLER_NAME (decl);
6160 ultimate_transparent_alias_target (&id);
6161 ultimate_transparent_alias_target (&target);
6163 /* We must force creation of DECL_RTL for debug info generation, even though
6164 we don't use it here. */
6165 make_decl_rtl (decl);
6167 TREE_ASM_WRITTEN (decl) = 1;
6168 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
6169 TREE_ASM_WRITTEN (id) = 1;
6171 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6173 if (!TREE_SYMBOL_REFERENCED (target))
6174 weakref_targets = tree_cons (decl, target, weakref_targets);
6176 #ifdef ASM_OUTPUT_WEAKREF
6177 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
6178 IDENTIFIER_POINTER (id),
6179 IDENTIFIER_POINTER (target));
6180 #else
6181 if (!TARGET_SUPPORTS_WEAK)
6183 error_at (DECL_SOURCE_LOCATION (decl),
6184 "%qs is not supported in this configuration", "weakref ");
6185 return;
6187 #endif
6188 return;
6191 #ifdef ASM_OUTPUT_DEF
6192 tree orig_decl = decl;
6194 /* Make name accessible from other files, if appropriate. */
6196 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
6198 globalize_decl (decl);
6199 maybe_assemble_visibility (decl);
6201 if (TREE_CODE (decl) == FUNCTION_DECL
6202 && cgraph_node::get (decl)->ifunc_resolver)
6204 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
6205 if (targetm.has_ifunc_p ())
6206 ASM_OUTPUT_TYPE_DIRECTIVE
6207 (asm_out_file, IDENTIFIER_POINTER (id),
6208 IFUNC_ASM_TYPE);
6209 else
6210 #endif
6211 error_at (DECL_SOURCE_LOCATION (decl),
6212 "%qs is not supported on this target", "ifunc");
6215 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
6216 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
6217 # else
6218 ASM_OUTPUT_DEF (asm_out_file,
6219 IDENTIFIER_POINTER (id),
6220 IDENTIFIER_POINTER (target));
6221 # endif
6222 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
6224 const char *name;
6225 tree *p, t;
6227 name = IDENTIFIER_POINTER (id);
6228 # ifdef ASM_WEAKEN_DECL
6229 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
6230 # else
6231 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
6232 # endif
6233 /* Remove this function from the pending weak list so that
6234 we do not emit multiple .weak directives for it. */
6235 for (p = &weak_decls; (t = *p) ; )
6236 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
6237 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6238 *p = TREE_CHAIN (t);
6239 else
6240 p = &TREE_CHAIN (t);
6242 /* Remove weakrefs to the same target from the pending weakref
6243 list, for the same reason. */
6244 for (p = &weakref_targets; (t = *p) ; )
6246 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6247 *p = TREE_CHAIN (t);
6248 else
6249 p = &TREE_CHAIN (t);
6252 #endif
6255 /* Output .symver directive. */
6257 void
6258 do_assemble_symver (tree decl, tree target)
6260 tree id = DECL_ASSEMBLER_NAME (decl);
6261 ultimate_transparent_alias_target (&id);
6262 ultimate_transparent_alias_target (&target);
6263 #ifdef ASM_OUTPUT_SYMVER_DIRECTIVE
6264 ASM_OUTPUT_SYMVER_DIRECTIVE (asm_out_file,
6265 IDENTIFIER_POINTER (target),
6266 IDENTIFIER_POINTER (id));
6267 #else
6268 error ("symver is only supported on ELF platforms");
6269 #endif
6272 /* Emit an assembler directive to make the symbol for DECL an alias to
6273 the symbol for TARGET. */
6275 void
6276 assemble_alias (tree decl, tree target)
6278 tree target_decl;
6280 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6282 tree alias = DECL_ASSEMBLER_NAME (decl);
6284 ultimate_transparent_alias_target (&target);
6286 if (alias == target)
6287 error ("%qs symbol %q+D ultimately targets itself", "weakref", decl);
6288 if (TREE_PUBLIC (decl))
6289 error ("%qs symbol %q+D must have static linkage", "weakref", decl);
6291 else
6293 #if !defined (ASM_OUTPUT_DEF)
6294 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
6295 error_at (DECL_SOURCE_LOCATION (decl),
6296 "alias definitions not supported in this configuration");
6297 TREE_ASM_WRITTEN (decl) = 1;
6298 return;
6299 # else
6300 if (!DECL_WEAK (decl))
6302 /* NB: ifunc_resolver isn't set when an error is detected. */
6303 if (TREE_CODE (decl) == FUNCTION_DECL
6304 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
6305 error_at (DECL_SOURCE_LOCATION (decl),
6306 "%qs is not supported in this configuration", "ifunc");
6307 else
6308 error_at (DECL_SOURCE_LOCATION (decl),
6309 "only weak aliases are supported in this configuration");
6310 TREE_ASM_WRITTEN (decl) = 1;
6311 return;
6313 # endif
6314 #endif
6316 TREE_USED (decl) = 1;
6318 /* Allow aliases to aliases. */
6319 if (TREE_CODE (decl) == FUNCTION_DECL)
6320 cgraph_node::get_create (decl)->alias = true;
6321 else
6322 varpool_node::get_create (decl)->alias = true;
6324 /* If the target has already been emitted, we don't have to queue the
6325 alias. This saves a tad of memory. */
6326 if (symtab->global_info_ready)
6327 target_decl = find_decl (target);
6328 else
6329 target_decl= NULL;
6330 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6331 || symtab->state >= EXPANSION)
6332 do_assemble_alias (decl, target);
6333 else
6335 alias_pair p = {decl, target};
6336 vec_safe_push (alias_pairs, p);
6340 /* Record and output a table of translations from original function
6341 to its transaction aware clone. Note that tm_pure functions are
6342 considered to be their own clone. */
6344 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6346 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6347 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6349 static int
6350 keep_cache_entry (tree_map *&e)
6352 return ggc_marked_p (e->base.from);
6356 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6358 void
6359 record_tm_clone_pair (tree o, tree n)
6361 struct tree_map **slot, *h;
6363 if (tm_clone_hash == NULL)
6364 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6366 h = ggc_alloc<tree_map> ();
6367 h->hash = htab_hash_pointer (o);
6368 h->base.from = o;
6369 h->to = n;
6371 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6372 *slot = h;
6375 tree
6376 get_tm_clone_pair (tree o)
6378 if (tm_clone_hash)
6380 struct tree_map *h, in;
6382 in.base.from = o;
6383 in.hash = htab_hash_pointer (o);
6384 h = tm_clone_hash->find_with_hash (&in, in.hash);
6385 if (h)
6386 return h->to;
6388 return NULL_TREE;
6391 struct tm_alias_pair
6393 unsigned int uid;
6394 tree from;
6395 tree to;
6399 /* Dump the actual pairs to the .tm_clone_table section. */
6401 static void
6402 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6404 unsigned i;
6405 tm_alias_pair *p;
6406 bool switched = false;
6408 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6410 tree src = p->from;
6411 tree dst = p->to;
6412 struct cgraph_node *src_n = cgraph_node::get (src);
6413 struct cgraph_node *dst_n = cgraph_node::get (dst);
6415 /* The function ipa_tm_create_version() marks the clone as needed if
6416 the original function was needed. But we also mark the clone as
6417 needed if we ever called the clone indirectly through
6418 TM_GETTMCLONE. If neither of these are true, we didn't generate
6419 a clone, and we didn't call it indirectly... no sense keeping it
6420 in the clone table. */
6421 if (!dst_n || !dst_n->definition)
6422 continue;
6424 /* This covers the case where we have optimized the original
6425 function away, and only access the transactional clone. */
6426 if (!src_n || !src_n->definition)
6427 continue;
6429 if (!switched)
6431 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6432 assemble_align (POINTER_SIZE);
6433 switched = true;
6436 assemble_integer (XEXP (DECL_RTL (src), 0),
6437 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6438 assemble_integer (XEXP (DECL_RTL (dst), 0),
6439 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6443 /* Provide a default for the tm_clone_table section. */
6445 section *
6446 default_clone_table_section (void)
6448 return get_named_section (NULL, ".tm_clone_table", 3);
6451 /* Helper comparison function for qsorting by the DECL_UID stored in
6452 alias_pair->emitted_diags. */
6454 static int
6455 tm_alias_pair_cmp (const void *x, const void *y)
6457 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6458 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6459 if (p1->uid < p2->uid)
6460 return -1;
6461 if (p1->uid > p2->uid)
6462 return 1;
6463 return 0;
6466 void
6467 finish_tm_clone_pairs (void)
6469 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6471 if (tm_clone_hash == NULL)
6472 return;
6474 /* We need a determenistic order for the .tm_clone_table, otherwise
6475 we will get bootstrap comparison failures, so dump the hash table
6476 to a vector, sort it, and dump the vector. */
6478 /* Dump the hashtable to a vector. */
6479 tree_map *map;
6480 hash_table<tm_clone_hasher>::iterator iter;
6481 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6483 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6484 tm_alias_pairs.safe_push (p);
6486 /* Sort it. */
6487 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6489 /* Dump it. */
6490 dump_tm_clone_pairs (tm_alias_pairs);
6492 tm_clone_hash->empty ();
6493 tm_clone_hash = NULL;
6494 tm_alias_pairs.release ();
6498 /* Emit an assembler directive to set symbol for DECL visibility to
6499 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6501 void
6502 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6503 int vis ATTRIBUTE_UNUSED)
6505 #ifdef HAVE_GAS_HIDDEN
6506 static const char * const visibility_types[] = {
6507 NULL, "protected", "hidden", "internal"
6510 const char *name, *type;
6511 tree id;
6513 id = DECL_ASSEMBLER_NAME (decl);
6514 ultimate_transparent_alias_target (&id);
6515 name = IDENTIFIER_POINTER (id);
6517 type = visibility_types[vis];
6519 fprintf (asm_out_file, "\t.%s\t", type);
6520 assemble_name (asm_out_file, name);
6521 fprintf (asm_out_file, "\n");
6522 #else
6523 if (!DECL_ARTIFICIAL (decl))
6524 warning (OPT_Wattributes, "visibility attribute not supported "
6525 "in this configuration; ignored");
6526 #endif
6529 /* A helper function to call assemble_visibility when needed for a decl. */
6531 bool
6532 maybe_assemble_visibility (tree decl)
6534 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6535 if (vis != VISIBILITY_DEFAULT)
6537 targetm.asm_out.assemble_visibility (decl, vis);
6538 return true;
6540 else
6541 return false;
6544 /* Returns true if the target configuration supports defining public symbols
6545 so that one of them will be chosen at link time instead of generating a
6546 multiply-defined symbol error, whether through the use of weak symbols or
6547 a target-specific mechanism for having duplicates discarded. */
6549 bool
6550 supports_one_only (void)
6552 if (SUPPORTS_ONE_ONLY)
6553 return true;
6554 if (TARGET_SUPPORTS_WEAK)
6555 return true;
6556 return false;
6559 /* Set up DECL as a public symbol that can be defined in multiple
6560 translation units without generating a linker error. */
6562 void
6563 make_decl_one_only (tree decl, tree comdat_group)
6565 struct symtab_node *symbol;
6566 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6568 TREE_PUBLIC (decl) = 1;
6570 if (VAR_P (decl))
6571 symbol = varpool_node::get_create (decl);
6572 else
6573 symbol = cgraph_node::get_create (decl);
6575 if (SUPPORTS_ONE_ONLY)
6577 #ifdef MAKE_DECL_ONE_ONLY
6578 MAKE_DECL_ONE_ONLY (decl);
6579 #endif
6580 symbol->set_comdat_group (comdat_group);
6582 else if (VAR_P (decl)
6583 && (DECL_INITIAL (decl) == 0
6584 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6585 DECL_COMMON (decl) = 1;
6586 else
6588 gcc_assert (TARGET_SUPPORTS_WEAK);
6589 DECL_WEAK (decl) = 1;
6593 void
6594 init_varasm_once (void)
6596 section_htab = hash_table<section_hasher>::create_ggc (31);
6597 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6598 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6600 shared_constant_pool = create_constant_pool ();
6602 #ifdef TEXT_SECTION_ASM_OP
6603 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6604 TEXT_SECTION_ASM_OP);
6605 #endif
6607 #ifdef DATA_SECTION_ASM_OP
6608 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6609 DATA_SECTION_ASM_OP);
6610 #endif
6612 #ifdef SDATA_SECTION_ASM_OP
6613 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6614 SDATA_SECTION_ASM_OP);
6615 #endif
6617 #ifdef READONLY_DATA_SECTION_ASM_OP
6618 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6619 READONLY_DATA_SECTION_ASM_OP);
6620 #endif
6622 #ifdef CTORS_SECTION_ASM_OP
6623 ctors_section = get_unnamed_section (0, output_section_asm_op,
6624 CTORS_SECTION_ASM_OP);
6625 #endif
6627 #ifdef DTORS_SECTION_ASM_OP
6628 dtors_section = get_unnamed_section (0, output_section_asm_op,
6629 DTORS_SECTION_ASM_OP);
6630 #endif
6632 #ifdef BSS_SECTION_ASM_OP
6633 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6634 output_section_asm_op,
6635 BSS_SECTION_ASM_OP);
6636 #endif
6638 #ifdef SBSS_SECTION_ASM_OP
6639 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6640 output_section_asm_op,
6641 SBSS_SECTION_ASM_OP);
6642 #endif
6644 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6645 | SECTION_COMMON, emit_tls_common);
6646 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6647 | SECTION_COMMON, emit_local);
6648 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6649 | SECTION_COMMON, emit_common);
6651 #if defined ASM_OUTPUT_ALIGNED_BSS
6652 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6653 emit_bss);
6654 #endif
6656 targetm.asm_out.init_sections ();
6658 if (readonly_data_section == NULL)
6659 readonly_data_section = text_section;
6661 #ifdef ASM_OUTPUT_EXTERNAL
6662 pending_assemble_externals_set = new hash_set<tree>;
6663 #endif
6666 /* Determine whether SYMBOL is used in any optimized function. */
6668 static bool
6669 have_optimized_refs (struct symtab_node *symbol)
6671 struct ipa_ref *ref;
6673 for (int i = 0; symbol->iterate_referring (i, ref); i++)
6675 cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
6677 if (cnode && opt_for_fn (cnode->decl, optimize))
6678 return true;
6681 return false;
6684 /* Check if promoting general-dynamic TLS access model to local-dynamic is
6685 desirable for DECL. */
6687 static bool
6688 optimize_dyn_tls_for_decl_p (const_tree decl)
6690 if (cfun)
6691 return optimize;
6692 return symtab->state >= IPA && have_optimized_refs (symtab_node::get (decl));
6696 enum tls_model
6697 decl_default_tls_model (const_tree decl)
6699 enum tls_model kind;
6700 bool is_local;
6702 is_local = targetm.binds_local_p (decl);
6703 if (!flag_shlib)
6705 if (is_local)
6706 kind = TLS_MODEL_LOCAL_EXEC;
6707 else
6708 kind = TLS_MODEL_INITIAL_EXEC;
6711 /* Local dynamic is inefficient when we're not combining the
6712 parts of the address. */
6713 else if (is_local && optimize_dyn_tls_for_decl_p (decl))
6714 kind = TLS_MODEL_LOCAL_DYNAMIC;
6715 else
6716 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6717 if (kind < flag_tls_default)
6718 kind = flag_tls_default;
6720 return kind;
6723 /* Select a set of attributes for section NAME based on the properties
6724 of DECL and whether or not RELOC indicates that DECL's initializer
6725 might contain runtime relocations.
6727 We make the section read-only and executable for a function decl,
6728 read-only for a const data decl, and writable for a non-const data decl. */
6730 unsigned int
6731 default_section_type_flags (tree decl, const char *name, int reloc)
6733 unsigned int flags;
6735 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6736 flags = SECTION_CODE;
6737 else if (decl)
6739 enum section_category category
6740 = categorize_decl_for_section (decl, reloc);
6741 if (decl_readonly_section_1 (category))
6742 flags = 0;
6743 else if (category == SECCAT_DATA_REL_RO
6744 || category == SECCAT_DATA_REL_RO_LOCAL)
6745 flags = SECTION_WRITE | SECTION_RELRO;
6746 else
6747 flags = SECTION_WRITE;
6749 else
6751 flags = SECTION_WRITE;
6752 if (strcmp (name, ".data.rel.ro") == 0
6753 || strcmp (name, ".data.rel.ro.local") == 0)
6754 flags |= SECTION_RELRO;
6757 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6758 flags |= SECTION_LINKONCE;
6760 if (strcmp (name, ".vtable_map_vars") == 0)
6761 flags |= SECTION_LINKONCE;
6763 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6764 flags |= SECTION_TLS | SECTION_WRITE;
6766 if (strcmp (name, ".bss") == 0
6767 || startswith (name, ".bss.")
6768 || startswith (name, ".gnu.linkonce.b.")
6769 || strcmp (name, ".persistent.bss") == 0
6770 || strcmp (name, ".sbss") == 0
6771 || startswith (name, ".sbss.")
6772 || startswith (name, ".gnu.linkonce.sb."))
6773 flags |= SECTION_BSS;
6775 if (strcmp (name, ".tdata") == 0
6776 || startswith (name, ".tdata.")
6777 || startswith (name, ".gnu.linkonce.td."))
6778 flags |= SECTION_TLS;
6780 if (strcmp (name, ".tbss") == 0
6781 || startswith (name, ".tbss.")
6782 || startswith (name, ".gnu.linkonce.tb."))
6783 flags |= SECTION_TLS | SECTION_BSS;
6785 if (strcmp (name, ".noinit") == 0)
6786 flags |= SECTION_WRITE | SECTION_BSS | SECTION_NOTYPE;
6788 if (strcmp (name, ".persistent") == 0)
6789 flags |= SECTION_WRITE | SECTION_NOTYPE;
6791 /* Various sections have special ELF types that the assembler will
6792 assign by default based on the name. They are neither SHT_PROGBITS
6793 nor SHT_NOBITS, so when changing sections we don't want to print a
6794 section type (@progbits or @nobits). Rather than duplicating the
6795 assembler's knowledge of what those special name patterns are, just
6796 let the assembler choose the type if we don't know a specific
6797 reason to set it to something other than the default. SHT_PROGBITS
6798 is the default for sections whose name is not specially known to
6799 the assembler, so it does no harm to leave the choice to the
6800 assembler when @progbits is the best thing we know to use. If
6801 someone is silly enough to emit code or TLS variables to one of
6802 these sections, then don't handle them specially.
6804 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6805 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
6806 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6807 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6808 flags |= SECTION_NOTYPE;
6810 return flags;
6813 /* Return true if the target supports some form of global BSS,
6814 either through bss_noswitch_section, or by selecting a BSS
6815 section in TARGET_ASM_SELECT_SECTION. */
6817 bool
6818 have_global_bss_p (void)
6820 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6823 /* Output assembly to switch to section NAME with attribute FLAGS.
6824 Four variants for common object file formats. */
6826 void
6827 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6828 unsigned int flags ATTRIBUTE_UNUSED,
6829 tree decl ATTRIBUTE_UNUSED)
6831 /* Some object formats don't support named sections at all. The
6832 front-end should already have flagged this as an error. */
6833 gcc_unreachable ();
6836 #ifndef TLS_SECTION_ASM_FLAG
6837 #define TLS_SECTION_ASM_FLAG 'T'
6838 #endif
6840 void
6841 default_elf_asm_named_section (const char *name, unsigned int flags,
6842 tree decl)
6844 char flagchars[11], *f = flagchars;
6845 unsigned int numeric_value = 0;
6847 /* If we have already declared this section, we can use an
6848 abbreviated form to switch back to it -- unless this section is
6849 part of a COMDAT groups or with SHF_GNU_RETAIN or with SHF_LINK_ORDER,
6850 in which case GAS requires the full declaration every time. */
6851 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6852 && !(flags & (SECTION_RETAIN | SECTION_LINK_ORDER))
6853 && (flags & SECTION_DECLARED))
6855 fprintf (asm_out_file, "\t.section\t%s\n", name);
6856 return;
6859 /* If we have a machine specific flag, then use the numeric value to pass
6860 this on to GAS. */
6861 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6862 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6863 else
6865 if (!(flags & SECTION_DEBUG))
6866 *f++ = 'a';
6867 #if HAVE_GAS_SECTION_EXCLUDE
6868 if (flags & SECTION_EXCLUDE)
6869 *f++ = 'e';
6870 #endif
6871 if (flags & SECTION_WRITE)
6872 *f++ = 'w';
6873 if (flags & SECTION_CODE)
6874 *f++ = 'x';
6875 if (flags & SECTION_SMALL)
6876 *f++ = 's';
6877 if (flags & SECTION_MERGE)
6878 *f++ = 'M';
6879 if (flags & SECTION_STRINGS)
6880 *f++ = 'S';
6881 if (flags & SECTION_TLS)
6882 *f++ = TLS_SECTION_ASM_FLAG;
6883 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6884 *f++ = 'G';
6885 if (flags & SECTION_RETAIN)
6886 *f++ = 'R';
6887 if (flags & SECTION_LINK_ORDER)
6888 *f++ = 'o';
6889 #ifdef MACH_DEP_SECTION_ASM_FLAG
6890 if (flags & SECTION_MACH_DEP)
6891 *f++ = MACH_DEP_SECTION_ASM_FLAG;
6892 #endif
6893 *f = '\0';
6896 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6898 /* default_section_type_flags (above) knows which flags need special
6899 handling here, and sets NOTYPE when none of these apply so that the
6900 assembler's logic for default types can apply to user-chosen
6901 section names. */
6902 if (!(flags & SECTION_NOTYPE))
6904 const char *type;
6905 const char *format;
6907 if (flags & SECTION_BSS)
6908 type = "nobits";
6909 else
6910 type = "progbits";
6912 format = ",@%s";
6913 /* On platforms that use "@" as the assembly comment character,
6914 use "%" instead. */
6915 if (strcmp (ASM_COMMENT_START, "@") == 0)
6916 format = ",%%%s";
6917 fprintf (asm_out_file, format, type);
6919 if (flags & SECTION_ENTSIZE)
6920 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6921 if (flags & SECTION_LINK_ORDER)
6923 /* For now, only section "__patchable_function_entries"
6924 adopts flag SECTION_LINK_ORDER, internal label LPFE*
6925 was emitted in default_print_patchable_function_entry,
6926 just place it here for linked_to section. */
6927 gcc_assert (!strcmp (name, "__patchable_function_entries"));
6928 fprintf (asm_out_file, ",");
6929 char buf[256];
6930 ASM_GENERATE_INTERNAL_LABEL (buf, "LPFE",
6931 current_function_funcdef_no);
6932 assemble_name_raw (asm_out_file, buf);
6934 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6936 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6937 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6938 else
6939 fprintf (asm_out_file, ",%s,comdat",
6940 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6944 putc ('\n', asm_out_file);
6947 void
6948 default_coff_asm_named_section (const char *name, unsigned int flags,
6949 tree decl ATTRIBUTE_UNUSED)
6951 char flagchars[8], *f = flagchars;
6953 if (flags & SECTION_WRITE)
6954 *f++ = 'w';
6955 if (flags & SECTION_CODE)
6956 *f++ = 'x';
6957 *f = '\0';
6959 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6962 void
6963 default_pe_asm_named_section (const char *name, unsigned int flags,
6964 tree decl)
6966 default_coff_asm_named_section (name, flags, decl);
6968 if (flags & SECTION_LINKONCE)
6970 /* Functions may have been compiled at various levels of
6971 optimization so we can't use `same_size' here.
6972 Instead, have the linker pick one. */
6973 fprintf (asm_out_file, "\t.linkonce %s\n",
6974 (flags & SECTION_CODE ? "discard" : "same_size"));
6978 /* The lame default section selector. */
6980 section *
6981 default_select_section (tree decl, int reloc,
6982 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6984 if (DECL_P (decl))
6986 if (decl_readonly_section (decl, reloc))
6987 return readonly_data_section;
6989 else if (TREE_CODE (decl) == CONSTRUCTOR)
6991 if (! ((flag_pic && reloc)
6992 || !TREE_READONLY (decl)
6993 || !TREE_CONSTANT (decl)))
6994 return readonly_data_section;
6996 else if (TREE_CODE (decl) == STRING_CST)
6997 return readonly_data_section;
6998 else if (! (flag_pic && reloc))
6999 return readonly_data_section;
7001 return data_section;
7004 enum section_category
7005 categorize_decl_for_section (const_tree decl, int reloc)
7007 enum section_category ret;
7009 if (TREE_CODE (decl) == FUNCTION_DECL)
7010 return SECCAT_TEXT;
7011 else if (TREE_CODE (decl) == STRING_CST)
7013 if ((flag_sanitize & SANITIZE_ADDRESS)
7014 && asan_protect_global (CONST_CAST_TREE (decl)))
7015 /* or !flag_merge_constants */
7016 return SECCAT_RODATA;
7017 else
7018 return SECCAT_RODATA_MERGE_STR;
7020 else if (VAR_P (decl))
7022 tree d = CONST_CAST_TREE (decl);
7023 if (bss_initializer_p (decl))
7024 ret = SECCAT_BSS;
7025 else if (! TREE_READONLY (decl)
7026 || (DECL_INITIAL (decl)
7027 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
7029 /* Here the reloc_rw_mask is not testing whether the section should
7030 be read-only or not, but whether the dynamic link will have to
7031 do something. If so, we wish to segregate the data in order to
7032 minimize cache misses inside the dynamic linker. */
7033 if (reloc & targetm.asm_out.reloc_rw_mask ())
7034 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
7035 else
7036 ret = SECCAT_DATA;
7038 else if (reloc & targetm.asm_out.reloc_rw_mask ())
7039 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
7040 else if (reloc || (flag_merge_constants < 2 && !DECL_MERGEABLE (decl))
7041 || ((flag_sanitize & SANITIZE_ADDRESS)
7042 /* PR 81697: for architectures that use section anchors we
7043 need to ignore DECL_RTL_SET_P (decl) for string constants
7044 inside this asan_protect_global call because otherwise
7045 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
7046 section, set DECL_RTL (decl) later on and add DECL to
7047 protected globals via successive asan_protect_global
7048 calls. In this scenario we'll end up with wrong
7049 alignment of these strings at runtime and possible ASan
7050 false positives. */
7051 && asan_protect_global (d, use_object_blocks_p ()
7052 && use_blocks_for_decl_p (d))))
7053 /* C and C++ don't allow different variables to share the same
7054 location. -fmerge-all-constants allows even that (at the
7055 expense of not conforming). */
7056 ret = SECCAT_RODATA;
7057 else if (DECL_INITIAL (decl)
7058 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
7059 ret = SECCAT_RODATA_MERGE_STR_INIT;
7060 else
7061 ret = SECCAT_RODATA_MERGE_CONST;
7063 else if (TREE_CODE (decl) == CONSTRUCTOR)
7065 if ((reloc & targetm.asm_out.reloc_rw_mask ())
7066 || ! TREE_CONSTANT (decl))
7067 ret = SECCAT_DATA;
7068 else
7069 ret = SECCAT_RODATA;
7071 else
7072 ret = SECCAT_RODATA;
7074 /* There are no read-only thread-local sections. */
7075 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7077 /* Note that this would be *just* SECCAT_BSS, except that there's
7078 no concept of a read-only thread-local-data section. */
7079 if (ret == SECCAT_BSS
7080 || DECL_INITIAL (decl) == NULL
7081 || (flag_zero_initialized_in_bss
7082 && initializer_zerop (DECL_INITIAL (decl))))
7083 ret = SECCAT_TBSS;
7084 else
7085 ret = SECCAT_TDATA;
7088 /* If the target uses small data sections, select it. */
7089 else if (targetm.in_small_data_p (decl))
7091 if (ret == SECCAT_BSS)
7092 ret = SECCAT_SBSS;
7093 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
7094 ret = SECCAT_SRODATA;
7095 else
7096 ret = SECCAT_SDATA;
7099 return ret;
7102 static bool
7103 decl_readonly_section_1 (enum section_category category)
7105 switch (category)
7107 case SECCAT_RODATA:
7108 case SECCAT_RODATA_MERGE_STR:
7109 case SECCAT_RODATA_MERGE_STR_INIT:
7110 case SECCAT_RODATA_MERGE_CONST:
7111 case SECCAT_SRODATA:
7112 return true;
7113 default:
7114 return false;
7118 bool
7119 decl_readonly_section (const_tree decl, int reloc)
7121 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
7124 /* Select a section based on the above categorization. */
7126 section *
7127 default_elf_select_section (tree decl, int reloc,
7128 unsigned HOST_WIDE_INT align)
7130 const char *sname;
7132 switch (categorize_decl_for_section (decl, reloc))
7134 case SECCAT_TEXT:
7135 /* We're not supposed to be called on FUNCTION_DECLs. */
7136 gcc_unreachable ();
7137 case SECCAT_RODATA:
7138 return readonly_data_section;
7139 case SECCAT_RODATA_MERGE_STR:
7140 return mergeable_string_section (decl, align, 0);
7141 case SECCAT_RODATA_MERGE_STR_INIT:
7142 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
7143 case SECCAT_RODATA_MERGE_CONST:
7144 return mergeable_constant_section (DECL_MODE (decl), align, 0);
7145 case SECCAT_SRODATA:
7146 sname = ".sdata2";
7147 break;
7148 case SECCAT_DATA:
7149 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7151 sname = ".persistent";
7152 break;
7154 return data_section;
7155 case SECCAT_DATA_REL:
7156 sname = ".data.rel";
7157 break;
7158 case SECCAT_DATA_REL_LOCAL:
7159 sname = ".data.rel.local";
7160 break;
7161 case SECCAT_DATA_REL_RO:
7162 sname = ".data.rel.ro";
7163 break;
7164 case SECCAT_DATA_REL_RO_LOCAL:
7165 sname = ".data.rel.ro.local";
7166 break;
7167 case SECCAT_SDATA:
7168 sname = ".sdata";
7169 break;
7170 case SECCAT_TDATA:
7171 sname = ".tdata";
7172 break;
7173 case SECCAT_BSS:
7174 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7176 sname = ".noinit";
7177 break;
7179 if (bss_section)
7180 return bss_section;
7181 sname = ".bss";
7182 break;
7183 case SECCAT_SBSS:
7184 sname = ".sbss";
7185 break;
7186 case SECCAT_TBSS:
7187 sname = ".tbss";
7188 break;
7189 default:
7190 gcc_unreachable ();
7193 return get_named_section (decl, sname, reloc);
7196 /* Construct a unique section name based on the decl name and the
7197 categorization performed above. */
7199 void
7200 default_unique_section (tree decl, int reloc)
7202 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
7203 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
7204 const char *prefix, *name, *linkonce;
7205 char *string;
7206 tree id;
7208 switch (categorize_decl_for_section (decl, reloc))
7210 case SECCAT_TEXT:
7211 prefix = one_only ? ".t" : ".text";
7212 break;
7213 case SECCAT_RODATA:
7214 case SECCAT_RODATA_MERGE_STR:
7215 case SECCAT_RODATA_MERGE_STR_INIT:
7216 case SECCAT_RODATA_MERGE_CONST:
7217 prefix = one_only ? ".r" : ".rodata";
7218 break;
7219 case SECCAT_SRODATA:
7220 prefix = one_only ? ".s2" : ".sdata2";
7221 break;
7222 case SECCAT_DATA:
7223 prefix = one_only ? ".d" : ".data";
7224 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7226 prefix = one_only ? ".p" : ".persistent";
7227 break;
7229 break;
7230 case SECCAT_DATA_REL:
7231 prefix = one_only ? ".d.rel" : ".data.rel";
7232 break;
7233 case SECCAT_DATA_REL_LOCAL:
7234 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
7235 break;
7236 case SECCAT_DATA_REL_RO:
7237 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
7238 break;
7239 case SECCAT_DATA_REL_RO_LOCAL:
7240 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
7241 break;
7242 case SECCAT_SDATA:
7243 prefix = one_only ? ".s" : ".sdata";
7244 break;
7245 case SECCAT_BSS:
7246 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7248 prefix = one_only ? ".n" : ".noinit";
7249 break;
7251 prefix = one_only ? ".b" : ".bss";
7252 break;
7253 case SECCAT_SBSS:
7254 prefix = one_only ? ".sb" : ".sbss";
7255 break;
7256 case SECCAT_TDATA:
7257 prefix = one_only ? ".td" : ".tdata";
7258 break;
7259 case SECCAT_TBSS:
7260 prefix = one_only ? ".tb" : ".tbss";
7261 break;
7262 default:
7263 gcc_unreachable ();
7266 id = DECL_ASSEMBLER_NAME (decl);
7267 ultimate_transparent_alias_target (&id);
7268 name = IDENTIFIER_POINTER (id);
7269 name = targetm.strip_name_encoding (name);
7271 /* If we're using one_only, then there needs to be a .gnu.linkonce
7272 prefix to the section name. */
7273 linkonce = one_only ? ".gnu.linkonce" : "";
7275 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
7277 set_decl_section_name (decl, string);
7280 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
7282 static int
7283 compute_reloc_for_rtx_1 (const_rtx x)
7285 switch (GET_CODE (x))
7287 case SYMBOL_REF:
7288 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
7289 case LABEL_REF:
7290 return 1;
7291 default:
7292 return 0;
7296 /* Like compute_reloc_for_constant, except for an RTX. The return value
7297 is a mask for which bit 1 indicates a global relocation, and bit 0
7298 indicates a local relocation. Used by default_select_rtx_section
7299 and default_elf_select_rtx_section. */
7301 static int
7302 compute_reloc_for_rtx (const_rtx x)
7304 switch (GET_CODE (x))
7306 case SYMBOL_REF:
7307 case LABEL_REF:
7308 return compute_reloc_for_rtx_1 (x);
7310 case CONST:
7312 int reloc = 0;
7313 subrtx_iterator::array_type array;
7314 FOR_EACH_SUBRTX (iter, array, x, ALL)
7315 reloc |= compute_reloc_for_rtx_1 (*iter);
7316 return reloc;
7319 default:
7320 return 0;
7324 section *
7325 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
7326 rtx x,
7327 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7329 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
7330 return data_section;
7331 else
7332 return readonly_data_section;
7335 section *
7336 default_elf_select_rtx_section (machine_mode mode, rtx x,
7337 unsigned HOST_WIDE_INT align)
7339 int reloc = compute_reloc_for_rtx (x);
7341 /* ??? Handle small data here somehow. */
7343 if (reloc & targetm.asm_out.reloc_rw_mask ())
7345 if (reloc == 1)
7346 return get_named_section (NULL, ".data.rel.ro.local", 1);
7347 else
7348 return get_named_section (NULL, ".data.rel.ro", 3);
7351 return mergeable_constant_section (mode, align, 0);
7354 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
7356 void
7357 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
7359 rtx symbol;
7360 int flags;
7362 /* Careful not to prod global register variables. */
7363 if (!MEM_P (rtl))
7364 return;
7365 symbol = XEXP (rtl, 0);
7366 if (GET_CODE (symbol) != SYMBOL_REF)
7367 return;
7369 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
7370 if (TREE_CODE (decl) == FUNCTION_DECL)
7371 flags |= SYMBOL_FLAG_FUNCTION;
7372 if (targetm.binds_local_p (decl))
7373 flags |= SYMBOL_FLAG_LOCAL;
7374 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7375 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
7376 else if (targetm.in_small_data_p (decl))
7377 flags |= SYMBOL_FLAG_SMALL;
7378 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
7379 being PUBLIC, the thing *must* be defined in this translation unit.
7380 Prevent this buglet from being propagated into rtl code as well. */
7381 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
7382 flags |= SYMBOL_FLAG_EXTERNAL;
7384 SYMBOL_REF_FLAGS (symbol) = flags;
7387 /* By default, we do nothing for encode_section_info, so we need not
7388 do anything but discard the '*' marker. */
7390 const char *
7391 default_strip_name_encoding (const char *str)
7393 return str + (*str == '*');
7396 #ifdef ASM_OUTPUT_DEF
7397 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
7398 anchor relative to ".", the current section position. */
7400 void
7401 default_asm_output_anchor (rtx symbol)
7403 char buffer[100];
7405 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7406 SYMBOL_REF_BLOCK_OFFSET (symbol));
7407 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7409 #endif
7411 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
7413 bool
7414 default_use_anchors_for_symbol_p (const_rtx symbol)
7416 tree decl;
7417 section *sect = SYMBOL_REF_BLOCK (symbol)->sect;
7419 /* This function should only be called with non-zero SYMBOL_REF_BLOCK,
7420 furthermore get_block_for_section should not create object blocks
7421 for mergeable sections. */
7422 gcc_checking_assert (sect && !(sect->common.flags & SECTION_MERGE));
7424 /* Don't use anchors for small data sections. The small data register
7425 acts as an anchor for such sections. */
7426 if (sect->common.flags & SECTION_SMALL)
7427 return false;
7429 decl = SYMBOL_REF_DECL (symbol);
7430 if (decl && DECL_P (decl))
7432 /* Don't use section anchors for decls that might be defined or
7433 usurped by other modules. */
7434 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
7435 return false;
7437 /* Don't use section anchors for decls that will be placed in a
7438 small data section. */
7439 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
7440 one above. The problem is that we only use SECTION_SMALL for
7441 sections that should be marked as small in the section directive. */
7442 if (targetm.in_small_data_p (decl))
7443 return false;
7445 /* Don't use section anchors for decls that won't fit inside a single
7446 anchor range to reduce the amount of instructions required to refer
7447 to the entire declaration. */
7448 if (DECL_SIZE_UNIT (decl) == NULL_TREE
7449 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
7450 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
7451 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
7452 return false;
7455 return true;
7458 /* Return true when RESOLUTION indicate that symbol will be bound to the
7459 definition provided by current .o file. */
7461 static bool
7462 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
7464 return (resolution == LDPR_PREVAILING_DEF
7465 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7466 || resolution == LDPR_PREVAILING_DEF_IRONLY);
7469 /* Return true when RESOLUTION indicate that symbol will be bound locally
7470 within current executable or DSO. */
7472 static bool
7473 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7475 return (resolution == LDPR_PREVAILING_DEF
7476 || resolution == LDPR_PREVAILING_DEF_IRONLY
7477 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7478 || resolution == LDPR_PREEMPTED_REG
7479 || resolution == LDPR_PREEMPTED_IR
7480 || resolution == LDPR_RESOLVED_IR
7481 || resolution == LDPR_RESOLVED_EXEC);
7484 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7485 uninitialized common symbol in the executable will still be defined
7486 (through COPY relocation) in the executable. */
7488 bool
7489 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7490 bool extern_protected_data, bool common_local_p)
7492 /* A non-decl is an entry in the constant pool. */
7493 if (!DECL_P (exp))
7494 return true;
7496 /* Weakrefs may not bind locally, even though the weakref itself is always
7497 static and therefore local. Similarly, the resolver for ifunc functions
7498 might resolve to a non-local function.
7499 FIXME: We can resolve the weakref case more curefuly by looking at the
7500 weakref alias. */
7501 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7502 || (!targetm.ifunc_ref_local_ok ()
7503 && TREE_CODE (exp) == FUNCTION_DECL
7504 && cgraph_node::get (exp)
7505 && cgraph_node::get (exp)->ifunc_resolver))
7506 return false;
7508 /* Static variables are always local. */
7509 if (! TREE_PUBLIC (exp))
7510 return true;
7512 /* With resolution file in hand, take look into resolutions.
7513 We can't just return true for resolved_locally symbols,
7514 because dynamic linking might overwrite symbols
7515 in shared libraries. */
7516 bool resolved_locally = false;
7518 bool uninited_common = (DECL_COMMON (exp)
7519 && (DECL_INITIAL (exp) == NULL
7520 || (!in_lto_p
7521 && DECL_INITIAL (exp) == error_mark_node)));
7523 /* A non-external variable is defined locally only if it isn't
7524 uninitialized COMMON variable or common_local_p is true. */
7525 bool defined_locally = (!DECL_EXTERNAL (exp)
7526 && (!uninited_common || common_local_p));
7527 if (symtab_node *node = symtab_node::get (exp))
7529 if (node->in_other_partition)
7530 defined_locally = true;
7531 if (node->can_be_discarded_p ())
7533 else if (resolution_to_local_definition_p (node->resolution))
7534 defined_locally = resolved_locally = true;
7535 else if (resolution_local_p (node->resolution))
7536 resolved_locally = true;
7538 if (defined_locally && weak_dominate && !shlib)
7539 resolved_locally = true;
7541 /* Undefined weak symbols are never defined locally. */
7542 if (DECL_WEAK (exp) && !defined_locally)
7543 return false;
7545 /* A symbol is local if the user has said explicitly that it will be,
7546 or if we have a definition for the symbol. We cannot infer visibility
7547 for undefined symbols. */
7548 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7549 && (TREE_CODE (exp) == FUNCTION_DECL
7550 || !extern_protected_data
7551 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7552 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7553 return true;
7555 /* If PIC, then assume that any global name can be overridden by
7556 symbols resolved from other modules. */
7557 if (shlib)
7558 return false;
7560 /* Variables defined outside this object might not be local. */
7561 if (DECL_EXTERNAL (exp) && !resolved_locally)
7562 return false;
7564 /* Non-dominant weak symbols are not defined locally. */
7565 if (DECL_WEAK (exp) && !resolved_locally)
7566 return false;
7568 /* Uninitialized COMMON variable may be unified with symbols
7569 resolved from other modules. */
7570 if (uninited_common && !resolved_locally)
7571 return false;
7573 /* Otherwise we're left with initialized (or non-common) global data
7574 which is of necessity defined locally. */
7575 return true;
7578 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7579 wrt cross-module name binding. */
7581 bool
7582 default_binds_local_p (const_tree exp)
7584 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7587 /* Similar to default_binds_local_p, but common symbol may be local and
7588 extern protected data is non-local. */
7590 bool
7591 default_binds_local_p_2 (const_tree exp)
7593 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7594 !flag_pic);
7597 bool
7598 default_binds_local_p_1 (const_tree exp, int shlib)
7600 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7603 /* Return true when references to DECL must bind to current definition in
7604 final executable.
7606 The condition is usually equivalent to whether the function binds to the
7607 current module (shared library or executable), that is to binds_local_p.
7608 We use this fact to avoid need for another target hook and implement
7609 the logic using binds_local_p and just special cases where
7610 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7611 the weak definitions (that can be overwritten at linktime by other
7612 definition from different object file) and when resolution info is available
7613 we simply use the knowledge passed to us by linker plugin. */
7614 bool
7615 decl_binds_to_current_def_p (const_tree decl)
7617 gcc_assert (DECL_P (decl));
7618 if (!targetm.binds_local_p (decl))
7619 return false;
7620 if (!TREE_PUBLIC (decl))
7621 return true;
7623 /* When resolution is available, just use it. */
7624 if (symtab_node *node = symtab_node::get (decl))
7626 if (node->resolution != LDPR_UNKNOWN
7627 && !node->can_be_discarded_p ())
7628 return resolution_to_local_definition_p (node->resolution);
7631 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7632 binds locally but still can be overwritten), DECL_COMMON (can be merged
7633 with a non-common definition somewhere in the same module) or
7634 DECL_EXTERNAL.
7635 This rely on fact that binds_local_p behave as decl_replaceable_p
7636 for all other declaration types. */
7637 if (DECL_WEAK (decl))
7638 return false;
7639 if (DECL_COMMON (decl)
7640 && (DECL_INITIAL (decl) == NULL
7641 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7642 return false;
7643 if (DECL_EXTERNAL (decl))
7644 return false;
7645 return true;
7648 /* A replaceable function or variable is one which may be replaced
7649 at link-time with an entirely different definition, provided that the
7650 replacement has the same type. For example, functions declared
7651 with __attribute__((weak)) on most systems are replaceable.
7652 If SEMANTIC_INTERPOSITION_P is false allow interposition only on
7653 symbols explicitly declared weak.
7655 COMDAT functions are not replaceable, since all definitions of the
7656 function must be equivalent. It is important that COMDAT functions
7657 not be treated as replaceable so that use of C++ template
7658 instantiations is not penalized. */
7660 bool
7661 decl_replaceable_p (tree decl, bool semantic_interposition_p)
7663 gcc_assert (DECL_P (decl));
7664 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7665 return false;
7666 if (!semantic_interposition_p
7667 && !DECL_WEAK (decl))
7668 return false;
7669 return !decl_binds_to_current_def_p (decl);
7672 /* Default function to output code that will globalize a label. A
7673 target must define GLOBAL_ASM_OP or provide its own function to
7674 globalize a label. */
7675 #ifdef GLOBAL_ASM_OP
7676 void
7677 default_globalize_label (FILE * stream, const char *name)
7679 fputs (GLOBAL_ASM_OP, stream);
7680 assemble_name (stream, name);
7681 putc ('\n', stream);
7683 #endif /* GLOBAL_ASM_OP */
7685 /* Default function to output code that will globalize a declaration. */
7686 void
7687 default_globalize_decl_name (FILE * stream, tree decl)
7689 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7690 targetm.asm_out.globalize_label (stream, name);
7693 /* Default function to output a label for unwind information. The
7694 default is to do nothing. A target that needs nonlocal labels for
7695 unwind information must provide its own function to do this. */
7696 void
7697 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7698 tree decl ATTRIBUTE_UNUSED,
7699 int for_eh ATTRIBUTE_UNUSED,
7700 int empty ATTRIBUTE_UNUSED)
7704 /* Default function to output a label to divide up the exception table.
7705 The default is to do nothing. A target that needs/wants to divide
7706 up the table must provide it's own function to do this. */
7707 void
7708 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7712 /* This is how to output an internal numbered label where PREFIX is
7713 the class of label and LABELNO is the number within the class. */
7715 void
7716 default_generate_internal_label (char *buf, const char *prefix,
7717 unsigned long labelno)
7719 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7722 /* This is how to output an internal numbered label where PREFIX is
7723 the class of label and LABELNO is the number within the class. */
7725 void
7726 default_internal_label (FILE *stream, const char *prefix,
7727 unsigned long labelno)
7729 char *const buf = (char *) alloca (40 + strlen (prefix));
7730 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7731 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7735 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7737 void
7738 default_asm_declare_constant_name (FILE *file, const char *name,
7739 const_tree exp ATTRIBUTE_UNUSED,
7740 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7742 assemble_label (file, name);
7745 /* This is the default behavior at the beginning of a file. It's
7746 controlled by two other target-hook toggles. */
7747 void
7748 default_file_start (void)
7750 if (targetm.asm_file_start_app_off
7751 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7752 fputs (ASM_APP_OFF, asm_out_file);
7754 if (targetm.asm_file_start_file_directive)
7756 /* LTO produced units have no meaningful main_input_filename. */
7757 if (in_lto_p)
7758 output_file_directive (asm_out_file, "<artificial>");
7759 else
7760 output_file_directive (asm_out_file, main_input_filename);
7764 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7765 which emits a special section directive used to indicate whether or
7766 not this object file needs an executable stack. This is primarily
7767 a GNU extension to ELF but could be used on other targets. */
7769 int trampolines_created;
7771 void
7772 file_end_indicate_exec_stack (void)
7774 unsigned int flags = SECTION_DEBUG;
7775 if (trampolines_created)
7776 flags |= SECTION_CODE;
7778 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7781 /* Emit a special section directive to indicate that this object file
7782 was compiled with -fsplit-stack. This is used to let the linker
7783 detect calls between split-stack code and non-split-stack code, so
7784 that it can modify the split-stack code to allocate a sufficiently
7785 large stack. We emit another special section if there are any
7786 functions in this file which have the no_split_stack attribute, to
7787 prevent the linker from warning about being unable to convert the
7788 functions if they call non-split-stack code. */
7790 void
7791 file_end_indicate_split_stack (void)
7793 if (flag_split_stack)
7795 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7796 NULL));
7797 if (saw_no_split_stack)
7798 switch_to_section (get_section (".note.GNU-no-split-stack",
7799 SECTION_DEBUG, NULL));
7803 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7804 a get_unnamed_section callback. */
7806 void
7807 output_section_asm_op (const char *directive)
7809 fprintf (asm_out_file, "%s\n", directive);
7812 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7813 the current section is NEW_SECTION. */
7815 void
7816 switch_to_section (section *new_section, tree decl)
7818 bool retain_p;
7819 if ((new_section->common.flags & SECTION_NAMED)
7820 && decl != nullptr
7821 && DECL_P (decl)
7822 && ((retain_p = !!lookup_attribute ("retain",
7823 DECL_ATTRIBUTES (decl)))
7824 != !!(new_section->common.flags & SECTION_RETAIN)))
7826 /* If the SECTION_RETAIN bit doesn't match, switch to a new
7827 section. */
7828 tree used_decl, no_used_decl;
7830 if (retain_p)
7832 new_section->common.flags |= SECTION_RETAIN;
7833 used_decl = decl;
7834 no_used_decl = new_section->named.decl;
7836 else
7838 new_section->common.flags &= ~(SECTION_RETAIN
7839 | SECTION_DECLARED);
7840 used_decl = new_section->named.decl;
7841 no_used_decl = decl;
7843 if (no_used_decl != used_decl)
7845 warning (OPT_Wattributes,
7846 "%+qD without %<retain%> attribute and %qD with "
7847 "%<retain%> attribute are placed in a section with "
7848 "the same name", no_used_decl, used_decl);
7849 inform (DECL_SOURCE_LOCATION (used_decl),
7850 "%qD was declared here", used_decl);
7853 else if (in_section == new_section)
7854 return;
7856 in_section = new_section;
7858 switch (SECTION_STYLE (new_section))
7860 case SECTION_NAMED:
7861 targetm.asm_out.named_section (new_section->named.name,
7862 new_section->named.common.flags,
7863 new_section->named.decl);
7864 break;
7866 case SECTION_UNNAMED:
7867 new_section->unnamed.callback (new_section->unnamed.data);
7868 break;
7870 case SECTION_NOSWITCH:
7871 gcc_unreachable ();
7872 break;
7875 new_section->common.flags |= SECTION_DECLARED;
7878 /* If block symbol SYMBOL has not yet been assigned an offset, place
7879 it at the end of its block. */
7881 void
7882 place_block_symbol (rtx symbol)
7884 unsigned HOST_WIDE_INT size, mask, offset;
7885 class constant_descriptor_rtx *desc;
7886 unsigned int alignment;
7887 struct object_block *block;
7888 tree decl;
7890 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7891 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7892 return;
7894 /* Work out the symbol's size and alignment. */
7895 if (CONSTANT_POOL_ADDRESS_P (symbol))
7897 desc = SYMBOL_REF_CONSTANT (symbol);
7898 alignment = desc->align;
7899 size = GET_MODE_SIZE (desc->mode);
7901 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7903 decl = SYMBOL_REF_DECL (symbol);
7904 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7905 alignment = DECL_ALIGN (decl);
7906 size = get_constant_size (DECL_INITIAL (decl));
7907 if ((flag_sanitize & SANITIZE_ADDRESS)
7908 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7909 && asan_protect_global (DECL_INITIAL (decl)))
7911 size += asan_red_zone_size (size);
7912 alignment = MAX (alignment,
7913 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7916 else
7918 struct symtab_node *snode;
7919 decl = SYMBOL_REF_DECL (symbol);
7921 snode = symtab_node::get (decl);
7922 if (snode->alias)
7924 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7926 gcc_assert (MEM_P (target)
7927 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7928 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7929 target = XEXP (target, 0);
7930 place_block_symbol (target);
7931 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7932 return;
7934 alignment = get_variable_align (decl);
7935 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7936 if ((flag_sanitize & SANITIZE_ADDRESS)
7937 && asan_protect_global (decl))
7939 size += asan_red_zone_size (size);
7940 alignment = MAX (alignment,
7941 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7945 /* Calculate the object's offset from the start of the block. */
7946 block = SYMBOL_REF_BLOCK (symbol);
7947 mask = alignment / BITS_PER_UNIT - 1;
7948 offset = (block->size + mask) & ~mask;
7949 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7951 /* Record the block's new alignment and size. */
7952 block->alignment = MAX (block->alignment, alignment);
7953 block->size = offset + size;
7955 vec_safe_push (block->objects, symbol);
7958 /* Return the anchor that should be used to address byte offset OFFSET
7959 from the first object in BLOCK. MODEL is the TLS model used
7960 to access it. */
7963 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7964 enum tls_model model)
7966 char label[100];
7967 unsigned int begin, middle, end;
7968 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7969 rtx anchor;
7971 /* Work out the anchor's offset. Use an offset of 0 for the first
7972 anchor so that we don't pessimize the case where we take the address
7973 of a variable at the beginning of the block. This is particularly
7974 useful when a block has only one variable assigned to it.
7976 We try to place anchors RANGE bytes apart, so there can then be
7977 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7978 a ptr_mode offset. With some target settings, the lowest such
7979 anchor might be out of range for the lowest ptr_mode offset;
7980 likewise the highest anchor for the highest offset. Use anchors
7981 at the extreme ends of the ptr_mode range in such cases.
7983 All arithmetic uses unsigned integers in order to avoid
7984 signed overflow. */
7985 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7986 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7987 range = max_offset - min_offset + 1;
7988 if (range == 0)
7989 offset = 0;
7990 else
7992 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7993 if (offset < 0)
7995 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7996 delta -= delta % range;
7997 if (delta > bias)
7998 delta = bias;
7999 offset = (HOST_WIDE_INT) (-delta);
8001 else
8003 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
8004 delta -= delta % range;
8005 if (delta > bias - 1)
8006 delta = bias - 1;
8007 offset = (HOST_WIDE_INT) delta;
8011 /* Do a binary search to see if there's already an anchor we can use.
8012 Set BEGIN to the new anchor's index if not. */
8013 begin = 0;
8014 end = vec_safe_length (block->anchors);
8015 while (begin != end)
8017 middle = (end + begin) / 2;
8018 anchor = (*block->anchors)[middle];
8019 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
8020 end = middle;
8021 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
8022 begin = middle + 1;
8023 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
8024 end = middle;
8025 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
8026 begin = middle + 1;
8027 else
8028 return anchor;
8031 /* Create a new anchor with a unique label. */
8032 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
8033 anchor = create_block_symbol (ggc_strdup (label), block, offset);
8034 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
8035 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
8037 /* Insert it at index BEGIN. */
8038 vec_safe_insert (block->anchors, begin, anchor);
8039 return anchor;
8042 /* Output the objects in BLOCK. */
8044 static void
8045 output_object_block (struct object_block *block)
8047 class constant_descriptor_rtx *desc;
8048 unsigned int i;
8049 HOST_WIDE_INT offset;
8050 tree decl;
8051 rtx symbol;
8053 if (!block->objects)
8054 return;
8056 /* Switch to the section and make sure that the first byte is
8057 suitably aligned. */
8058 /* Special case VTV comdat sections similar to assemble_variable. */
8059 if (SECTION_STYLE (block->sect) == SECTION_NAMED
8060 && block->sect->named.name
8061 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
8062 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
8063 else
8064 switch_to_section (block->sect, SYMBOL_REF_DECL ((*block->objects)[0]));
8066 gcc_checking_assert (!(block->sect->common.flags & SECTION_MERGE));
8067 assemble_align (block->alignment);
8069 /* Define the values of all anchors relative to the current section
8070 position. */
8071 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
8072 targetm.asm_out.output_anchor (symbol);
8074 /* Output the objects themselves. */
8075 offset = 0;
8076 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
8078 /* Move to the object's offset, padding with zeros if necessary. */
8079 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
8080 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
8081 if (CONSTANT_POOL_ADDRESS_P (symbol))
8083 desc = SYMBOL_REF_CONSTANT (symbol);
8084 /* Pass 1 for align as we have already laid out everything in the block.
8085 So aligning shouldn't be necessary. */
8086 output_constant_pool_1 (desc, 1);
8087 offset += GET_MODE_SIZE (desc->mode);
8089 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
8091 HOST_WIDE_INT size;
8092 decl = SYMBOL_REF_DECL (symbol);
8093 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
8094 DECL_ALIGN (decl), false);
8096 size = get_constant_size (DECL_INITIAL (decl));
8097 offset += size;
8098 if ((flag_sanitize & SANITIZE_ADDRESS)
8099 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8100 && asan_protect_global (DECL_INITIAL (decl)))
8102 size = asan_red_zone_size (size);
8103 assemble_zeros (size);
8104 offset += size;
8107 else
8109 HOST_WIDE_INT size;
8110 decl = SYMBOL_REF_DECL (symbol);
8111 assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
8112 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8113 offset += size;
8114 if ((flag_sanitize & SANITIZE_ADDRESS)
8115 && asan_protect_global (decl))
8117 size = asan_red_zone_size (size);
8118 assemble_zeros (size);
8119 offset += size;
8125 /* A callback for qsort to compare object_blocks. */
8127 static int
8128 output_object_block_compare (const void *x, const void *y)
8130 object_block *p1 = *(object_block * const*)x;
8131 object_block *p2 = *(object_block * const*)y;
8133 if (p1->sect->common.flags & SECTION_NAMED
8134 && !(p2->sect->common.flags & SECTION_NAMED))
8135 return 1;
8137 if (!(p1->sect->common.flags & SECTION_NAMED)
8138 && p2->sect->common.flags & SECTION_NAMED)
8139 return -1;
8141 if (p1->sect->common.flags & SECTION_NAMED
8142 && p2->sect->common.flags & SECTION_NAMED)
8143 return strcmp (p1->sect->named.name, p2->sect->named.name);
8145 unsigned f1 = p1->sect->common.flags;
8146 unsigned f2 = p2->sect->common.flags;
8147 if (f1 == f2)
8148 return 0;
8149 return f1 < f2 ? -1 : 1;
8152 /* Output the definitions of all object_blocks. */
8154 void
8155 output_object_blocks (void)
8157 vec<object_block *, va_heap> v;
8158 v.create (object_block_htab->elements ());
8159 object_block *obj;
8160 hash_table<object_block_hasher>::iterator hi;
8162 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
8163 v.quick_push (obj);
8165 /* Sort them in order to output them in a deterministic manner,
8166 otherwise we may get .rodata sections in different orders with
8167 and without -g. */
8168 v.qsort (output_object_block_compare);
8169 unsigned i;
8170 FOR_EACH_VEC_ELT (v, i, obj)
8171 output_object_block (obj);
8173 v.release ();
8176 /* This function provides a possible implementation of the
8177 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
8178 by -frecord-gcc-switches it creates a new mergeable, string section in the
8179 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
8180 contains the switches in ASCII format.
8182 FIXME: This code does not correctly handle double quote characters
8183 that appear inside strings, (it strips them rather than preserving them).
8184 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
8185 characters - instead it treats them as sub-string separators. Since
8186 we want to emit NUL strings terminators into the object file we have to use
8187 ASM_OUTPUT_SKIP. */
8189 void
8190 elf_record_gcc_switches (const char *options)
8192 section *sec = get_section (targetm.asm_out.record_gcc_switches_section,
8193 SECTION_DEBUG | SECTION_MERGE
8194 | SECTION_STRINGS | (SECTION_ENTSIZE & 1), NULL);
8195 switch_to_section (sec);
8196 ASM_OUTPUT_ASCII (asm_out_file, options, strlen (options) + 1);
8199 /* Emit text to declare externally defined symbols. It is needed to
8200 properly support non-default visibility. */
8201 void
8202 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
8203 tree decl,
8204 const char *name ATTRIBUTE_UNUSED)
8206 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8207 set in order to avoid putting out names that are never really
8208 used. Always output visibility specified in the source. */
8209 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
8210 && (DECL_VISIBILITY_SPECIFIED (decl)
8211 || targetm.binds_local_p (decl)))
8212 maybe_assemble_visibility (decl);
8215 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8217 void
8218 default_asm_output_source_filename (FILE *file, const char *name)
8220 #ifdef ASM_OUTPUT_SOURCE_FILENAME
8221 ASM_OUTPUT_SOURCE_FILENAME (file, name);
8222 #else
8223 fprintf (file, "\t.file\t");
8224 output_quoted_string (file, name);
8225 putc ('\n', file);
8226 #endif
8229 /* Output a file name in the form wanted by System V. */
8231 void
8232 output_file_directive (FILE *asm_file, const char *input_name)
8234 int len;
8235 const char *na;
8237 if (input_name == NULL)
8238 input_name = "<stdin>";
8239 else
8240 input_name = remap_debug_filename (input_name);
8242 len = strlen (input_name);
8243 na = input_name + len;
8245 /* NA gets INPUT_NAME sans directory names. */
8246 while (na > input_name)
8248 if (IS_DIR_SEPARATOR (na[-1]))
8249 break;
8250 na--;
8253 targetm.asm_out.output_source_filename (asm_file, na);
8256 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
8257 EXP. */
8259 make_debug_expr_from_rtl (const_rtx exp)
8261 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
8262 machine_mode mode = GET_MODE (exp);
8263 rtx dval;
8265 DECL_ARTIFICIAL (ddecl) = 1;
8266 if (REG_P (exp) && REG_EXPR (exp))
8267 type = TREE_TYPE (REG_EXPR (exp));
8268 else if (MEM_P (exp) && MEM_EXPR (exp))
8269 type = TREE_TYPE (MEM_EXPR (exp));
8270 else
8271 type = NULL_TREE;
8272 if (type && TYPE_MODE (type) == mode)
8273 TREE_TYPE (ddecl) = type;
8274 else
8275 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
8276 SET_DECL_MODE (ddecl, mode);
8277 dval = gen_rtx_DEBUG_EXPR (mode);
8278 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
8279 SET_DECL_RTL (ddecl, dval);
8280 return dval;
8283 #ifdef ELF_ASCII_ESCAPES
8284 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
8286 void
8287 default_elf_asm_output_limited_string (FILE *f, const char *s)
8289 int escape;
8290 unsigned char c;
8292 fputs (STRING_ASM_OP, f);
8293 putc ('"', f);
8294 while (*s != '\0')
8296 c = *s;
8297 escape = ELF_ASCII_ESCAPES[c];
8298 switch (escape)
8300 case 0:
8301 putc (c, f);
8302 break;
8303 case 1:
8304 putc ('\\', f);
8305 putc ('0'+((c>>6)&7), f);
8306 putc ('0'+((c>>3)&7), f);
8307 putc ('0'+(c&7), f);
8308 break;
8309 default:
8310 putc ('\\', f);
8311 putc (escape, f);
8312 break;
8314 s++;
8316 putc ('\"', f);
8317 putc ('\n', f);
8320 /* Default ASM_OUTPUT_ASCII for ELF targets. */
8322 void
8323 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
8325 const char *limit = s + len;
8326 const char *last_null = NULL;
8327 unsigned bytes_in_chunk = 0;
8328 unsigned char c;
8329 int escape;
8331 for (; s < limit; s++)
8333 const char *p;
8335 if (bytes_in_chunk >= 60)
8337 putc ('\"', f);
8338 putc ('\n', f);
8339 bytes_in_chunk = 0;
8342 if (s > last_null)
8344 for (p = s; p < limit && *p != '\0'; p++)
8345 continue;
8346 last_null = p;
8348 else
8349 p = last_null;
8351 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
8353 if (bytes_in_chunk > 0)
8355 putc ('\"', f);
8356 putc ('\n', f);
8357 bytes_in_chunk = 0;
8360 default_elf_asm_output_limited_string (f, s);
8361 s = p;
8363 else
8365 if (bytes_in_chunk == 0)
8366 fputs (ASCII_DATA_ASM_OP "\"", f);
8368 c = *s;
8369 escape = ELF_ASCII_ESCAPES[c];
8370 switch (escape)
8372 case 0:
8373 putc (c, f);
8374 bytes_in_chunk++;
8375 break;
8376 case 1:
8377 putc ('\\', f);
8378 putc ('0'+((c>>6)&7), f);
8379 putc ('0'+((c>>3)&7), f);
8380 putc ('0'+(c&7), f);
8381 bytes_in_chunk += 4;
8382 break;
8383 default:
8384 putc ('\\', f);
8385 putc (escape, f);
8386 bytes_in_chunk += 2;
8387 break;
8393 if (bytes_in_chunk > 0)
8395 putc ('\"', f);
8396 putc ('\n', f);
8399 #endif
8401 static GTY(()) section *elf_init_array_section;
8402 static GTY(()) section *elf_fini_array_section;
8404 static section *
8405 get_elf_initfini_array_priority_section (int priority,
8406 bool constructor_p)
8408 section *sec;
8409 if (priority != DEFAULT_INIT_PRIORITY)
8411 char buf[18];
8412 sprintf (buf, "%s.%.5u",
8413 constructor_p ? ".init_array" : ".fini_array",
8414 priority);
8415 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8417 else
8419 if (constructor_p)
8421 if (elf_init_array_section == NULL)
8422 elf_init_array_section
8423 = get_section (".init_array",
8424 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8425 sec = elf_init_array_section;
8427 else
8429 if (elf_fini_array_section == NULL)
8430 elf_fini_array_section
8431 = get_section (".fini_array",
8432 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8433 sec = elf_fini_array_section;
8436 return sec;
8439 /* Use .init_array section for constructors. */
8441 void
8442 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8444 section *sec = get_elf_initfini_array_priority_section (priority,
8445 true);
8446 assemble_addr_to_section (symbol, sec);
8449 /* Use .fini_array section for destructors. */
8451 void
8452 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8454 section *sec = get_elf_initfini_array_priority_section (priority,
8455 false);
8456 assemble_addr_to_section (symbol, sec);
8459 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8461 This is a bit of a cheat. The real default is a no-op, but this
8462 hook is the default for all targets with a .ident directive. */
8464 void
8465 default_asm_output_ident_directive (const char *ident_str)
8467 const char *ident_asm_op = "\t.ident\t";
8469 /* If we are still in the front end, do not write out the string
8470 to asm_out_file. Instead, add a fake top-level asm statement.
8471 This allows the front ends to use this hook without actually
8472 writing to asm_out_file, to handle #ident or Pragma Ident. */
8473 if (symtab->state == PARSING)
8475 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8476 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8478 else
8479 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8482 /* Switch to a COMDAT section with COMDAT name of decl.
8484 FIXME: resolve_unique_section needs to deal better with
8485 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8486 that is fixed, this if-else statement can be replaced with
8487 a single call to "switch_to_section (sect)". */
8489 void
8490 switch_to_comdat_section (section *sect, tree decl)
8492 #if defined (OBJECT_FORMAT_ELF)
8493 targetm.asm_out.named_section (sect->named.name,
8494 sect->named.common.flags
8495 | SECTION_LINKONCE,
8496 decl);
8497 in_section = sect;
8498 #else
8499 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8500 Therefore the following check is used.
8501 In case a the target is PE or COFF a comdat group section
8502 is created, e.g. .vtable_map_vars$foo. The linker places
8503 everything in .vtable_map_vars at the end.
8505 A fix could be made in
8506 gcc/config/i386/winnt.cc: i386_pe_unique_section. */
8507 if (TARGET_PECOFF)
8509 char *name;
8511 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8512 name = ACONCAT ((sect->named.name, "$",
8513 IDENTIFIER_POINTER (decl), NULL));
8514 else
8515 name = ACONCAT ((sect->named.name, "$",
8516 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)),
8517 NULL));
8519 targetm.asm_out.named_section (name,
8520 sect->named.common.flags
8521 | SECTION_LINKONCE,
8522 decl);
8523 in_section = sect;
8525 else
8526 switch_to_section (sect);
8527 #endif
8530 /* This function ensures that vtable_map variables are not only
8531 in the comdat section, but that each variable has its own unique
8532 comdat name. Without this the variables end up in the same section
8533 with a single comdat name. */
8535 static void
8536 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8538 switch_to_comdat_section(sect, DECL_NAME (decl));
8541 #include "gt-varasm.h"