Fix xfail for 32-bit hppa*-*-* in gcc.dg/pr84877.c
[official-gcc.git] / gcc / varasm.cc
blobfa17eff551e87e8dbc97687601ddef22c3e1a5d3
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "backend.h"
32 #include "target.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "predict.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "stringpool.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "varasm.h"
46 #include "version.h"
47 #include "flags.h"
48 #include "stmt.h"
49 #include "expr.h"
50 #include "expmed.h"
51 #include "optabs.h"
52 #include "output.h"
53 #include "langhooks.h"
54 #include "debug.h"
55 #include "common/common-target.h"
56 #include "stringpool.h"
57 #include "attribs.h"
58 #include "asan.h"
59 #include "rtl-iter.h"
60 #include "file-prefix-map.h" /* remap_debug_filename() */
61 #include "alloc-pool.h"
62 #include "toplev.h"
63 #include "opts.h"
64 #include "asan.h"
66 /* The (assembler) name of the first globally-visible object output. */
67 extern GTY(()) const char *first_global_object_name;
68 extern GTY(()) const char *weak_global_object_name;
70 const char *first_global_object_name;
71 const char *weak_global_object_name;
73 class addr_const;
74 class constant_descriptor_rtx;
75 struct rtx_constant_pool;
77 #define n_deferred_constants (crtl->varasm.deferred_constants)
79 /* Number for making the label on the next
80 constant that is stored in memory. */
82 static GTY(()) int const_labelno;
84 /* Carry information from ASM_DECLARE_OBJECT_NAME
85 to ASM_FINISH_DECLARE_OBJECT. */
87 int size_directive_output;
89 /* The last decl for which assemble_variable was called,
90 if it did ASM_DECLARE_OBJECT_NAME.
91 If the last call to assemble_variable didn't do that,
92 this holds 0. */
94 tree last_assemble_variable_decl;
96 /* The following global variable indicates if the first basic block
97 in a function belongs to the cold partition or not. */
99 bool first_function_block_is_cold;
101 /* Whether we saw any functions with no_split_stack. */
103 static bool saw_no_split_stack;
105 static const char *strip_reg_name (const char *);
106 static bool contains_pointers_p (tree);
107 #ifdef ASM_OUTPUT_EXTERNAL
108 static bool incorporeal_function_p (tree);
109 #endif
110 static void decode_addr_const (tree, class addr_const *);
111 static hashval_t const_hash_1 (const tree);
112 static bool compare_constant (const tree, const tree);
113 static void output_constant_def_contents (rtx);
114 static void output_addressed_constants (tree, int);
115 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
116 unsigned int, bool, bool);
117 static void globalize_decl (tree);
118 static bool decl_readonly_section_1 (enum section_category);
119 #ifdef BSS_SECTION_ASM_OP
120 #ifdef ASM_OUTPUT_ALIGNED_BSS
121 static void asm_output_aligned_bss (FILE *, tree, const char *,
122 unsigned HOST_WIDE_INT, int)
123 ATTRIBUTE_UNUSED;
124 #endif
125 #endif /* BSS_SECTION_ASM_OP */
126 static void mark_weak (tree);
127 static void output_constant_pool (const char *, tree);
128 static void handle_vtv_comdat_section (section *, const_tree);
130 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
131 section *text_section;
132 section *data_section;
133 section *readonly_data_section;
134 section *sdata_section;
135 section *ctors_section;
136 section *dtors_section;
137 section *bss_section;
138 section *sbss_section;
140 /* Various forms of common section. All are guaranteed to be nonnull. */
141 section *tls_comm_section;
142 section *comm_section;
143 section *lcomm_section;
145 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
146 May be null. */
147 section *bss_noswitch_section;
149 /* The section that holds the main exception table, when known. The section
150 is set either by the target's init_sections hook or by the first call to
151 switch_to_exception_section. */
152 section *exception_section;
154 /* The section that holds the DWARF2 frame unwind information, when known.
155 The section is set either by the target's init_sections hook or by the
156 first call to switch_to_eh_frame_section. */
157 section *eh_frame_section;
159 /* asm_out_file's current section. This is NULL if no section has yet
160 been selected or if we lose track of what the current section is. */
161 section *in_section;
163 /* True if code for the current function is currently being directed
164 at the cold section. */
165 bool in_cold_section_p;
167 /* The following global holds the "function name" for the code in the
168 cold section of a function, if hot/cold function splitting is enabled
169 and there was actually code that went into the cold section. A
170 pseudo function name is needed for the cold section of code for some
171 debugging tools that perform symbolization. */
172 tree cold_function_name = NULL_TREE;
174 /* A linked list of all the unnamed sections. */
175 static GTY(()) section *unnamed_sections;
177 /* Return a nonzero value if DECL has a section attribute. */
178 #define IN_NAMED_SECTION(DECL) \
179 (VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
181 struct section_hasher : ggc_ptr_hash<section>
183 typedef const char *compare_type;
185 static hashval_t hash (section *);
186 static bool equal (section *, const char *);
189 /* Hash table of named sections. */
190 static GTY(()) hash_table<section_hasher> *section_htab;
192 struct object_block_hasher : ggc_ptr_hash<object_block>
194 typedef const section *compare_type;
196 static hashval_t hash (object_block *);
197 static bool equal (object_block *, const section *);
200 /* A table of object_blocks, indexed by section. */
201 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
203 /* The next number to use for internal anchor labels. */
204 static GTY(()) int anchor_labelno;
206 /* A pool of constants that can be shared between functions. */
207 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
209 /* Helper routines for maintaining section_htab. */
211 bool
212 section_hasher::equal (section *old, const char *new_name)
214 return strcmp (old->named.name, new_name) == 0;
217 hashval_t
218 section_hasher::hash (section *old)
220 return htab_hash_string (old->named.name);
223 /* Return a hash value for section SECT. */
225 static hashval_t
226 hash_section (section *sect)
228 if (sect->common.flags & SECTION_NAMED)
229 return htab_hash_string (sect->named.name);
230 return sect->common.flags & ~SECTION_DECLARED;
233 /* Helper routines for maintaining object_block_htab. */
235 inline bool
236 object_block_hasher::equal (object_block *old, const section *new_section)
238 return old->sect == new_section;
241 hashval_t
242 object_block_hasher::hash (object_block *old)
244 return hash_section (old->sect);
247 /* Return a new unnamed section with the given fields. */
249 section *
250 get_unnamed_section (unsigned int flags, void (*callback) (const char *),
251 const char *data)
253 section *sect;
255 sect = ggc_alloc<section> ();
256 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
257 sect->unnamed.callback = callback;
258 sect->unnamed.data = data;
259 sect->unnamed.next = unnamed_sections;
261 unnamed_sections = sect;
262 return sect;
265 /* Return a SECTION_NOSWITCH section with the given fields. */
267 static section *
268 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
270 section *sect;
272 sect = ggc_alloc<section> ();
273 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
274 sect->noswitch.callback = callback;
276 return sect;
279 /* Return the named section structure associated with NAME. Create
280 a new section with the given fields if no such structure exists.
281 When NOT_EXISTING, then fail if the section already exists. Return
282 the existing section if the SECTION_RETAIN bit doesn't match. Set
283 the SECTION_WRITE | SECTION_RELRO bits on the existing section
284 if one of the section flags is SECTION_WRITE | SECTION_RELRO and the
285 other has none of these flags in named sections and either the section
286 hasn't been declared yet or has been declared as writable. */
288 section *
289 get_section (const char *name, unsigned int flags, tree decl,
290 bool not_existing)
292 section *sect, **slot;
294 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
295 INSERT);
296 flags |= SECTION_NAMED;
297 if (decl != nullptr
298 && DECL_P (decl)
299 && lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
300 flags |= SECTION_RETAIN;
301 if (*slot == NULL)
303 sect = ggc_alloc<section> ();
304 sect->named.common.flags = flags;
305 sect->named.name = ggc_strdup (name);
306 sect->named.decl = decl;
307 *slot = sect;
309 else
311 if (not_existing)
312 internal_error ("section already exists: %qs", name);
314 sect = *slot;
315 /* It is fine if one of the sections has SECTION_NOTYPE as long as
316 the other has none of the contrary flags (see the logic at the end
317 of default_section_type_flags, below). */
318 if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
319 && !((sect->common.flags | flags)
320 & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
321 | (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
323 sect->common.flags |= SECTION_NOTYPE;
324 flags |= SECTION_NOTYPE;
326 if ((sect->common.flags & ~SECTION_DECLARED) != flags
327 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
329 /* It is fine if one of the section flags is
330 SECTION_WRITE | SECTION_RELRO and the other has none of these
331 flags (i.e. read-only) in named sections and either the
332 section hasn't been declared yet or has been declared as writable.
333 In that case just make sure the resulting flags are
334 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
335 relocations. */
336 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
337 == (SECTION_WRITE | SECTION_RELRO)
338 && (sect->common.flags
339 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
340 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
341 && ((sect->common.flags & SECTION_DECLARED) == 0
342 || (sect->common.flags & SECTION_WRITE)))
344 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
345 return sect;
347 /* If the SECTION_RETAIN bit doesn't match, return and switch
348 to a new section later. */
349 if ((sect->common.flags & SECTION_RETAIN)
350 != (flags & SECTION_RETAIN))
351 return sect;
352 /* Sanity check user variables for flag changes. */
353 if (sect->named.decl != NULL
354 && DECL_P (sect->named.decl)
355 && decl != sect->named.decl)
357 if (decl != NULL && DECL_P (decl))
358 error ("%+qD causes a section type conflict with %qD",
359 decl, sect->named.decl);
360 else
361 error ("section type conflict with %qD", sect->named.decl);
362 inform (DECL_SOURCE_LOCATION (sect->named.decl),
363 "%qD was declared here", sect->named.decl);
365 else if (decl != NULL && DECL_P (decl))
366 error ("%+qD causes a section type conflict", decl);
367 else
368 error ("section type conflict");
369 /* Make sure we don't error about one section multiple times. */
370 sect->common.flags |= SECTION_OVERRIDE;
373 return sect;
376 /* Return true if the current compilation mode benefits from having
377 objects grouped into blocks. */
379 static bool
380 use_object_blocks_p (void)
382 return flag_section_anchors;
385 /* Return the object_block structure for section SECT. Create a new
386 structure if we haven't created one already. Return null if SECT
387 itself is null. Return also null for mergeable sections since
388 section anchors can't be used in mergeable sections anyway,
389 because the linker might move objects around, and using the
390 object blocks infrastructure in that case is both a waste and a
391 maintenance burden. */
393 static struct object_block *
394 get_block_for_section (section *sect)
396 struct object_block *block;
398 if (sect == NULL)
399 return NULL;
401 if (sect->common.flags & SECTION_MERGE)
402 return NULL;
404 object_block **slot
405 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
406 INSERT);
407 block = *slot;
408 if (block == NULL)
410 block = ggc_cleared_alloc<object_block> ();
411 block->sect = sect;
412 *slot = block;
414 return block;
417 /* Create a symbol with label LABEL and place it at byte offset
418 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
419 is not yet known. LABEL must be a garbage-collected string. */
421 static rtx
422 create_block_symbol (const char *label, struct object_block *block,
423 HOST_WIDE_INT offset)
425 rtx symbol;
426 unsigned int size;
428 /* Create the extended SYMBOL_REF. */
429 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
430 symbol = (rtx) ggc_internal_alloc (size);
432 /* Initialize the normal SYMBOL_REF fields. */
433 memset (symbol, 0, size);
434 PUT_CODE (symbol, SYMBOL_REF);
435 PUT_MODE (symbol, Pmode);
436 XSTR (symbol, 0) = label;
437 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
439 /* Initialize the block_symbol stuff. */
440 SYMBOL_REF_BLOCK (symbol) = block;
441 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
443 return symbol;
446 /* Return a section with a particular name and with whatever SECTION_*
447 flags section_type_flags deems appropriate. The name of the section
448 is taken from NAME if nonnull, otherwise it is taken from DECL's
449 DECL_SECTION_NAME. DECL is the decl associated with the section
450 (see the section comment for details) and RELOC is as for
451 section_type_flags. */
453 section *
454 get_named_section (tree decl, const char *name, int reloc)
456 unsigned int flags;
458 if (name == NULL)
460 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
461 name = DECL_SECTION_NAME (decl);
464 flags = targetm.section_type_flags (decl, name, reloc);
465 return get_section (name, flags, decl);
468 /* Worker for resolve_unique_section. */
470 static bool
471 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
473 n->implicit_section = true;
474 return false;
477 /* If required, set DECL_SECTION_NAME to a unique name. */
479 void
480 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
481 int flag_function_or_data_sections)
483 if (DECL_SECTION_NAME (decl) == NULL
484 && targetm_common.have_named_sections
485 && (flag_function_or_data_sections
486 || lookup_attribute ("retain", DECL_ATTRIBUTES (decl))
487 || DECL_COMDAT_GROUP (decl)))
489 targetm.asm_out.unique_section (decl, reloc);
490 if (DECL_SECTION_NAME (decl))
491 symtab_node::get (decl)->call_for_symbol_and_aliases
492 (set_implicit_section, NULL, true);
496 #ifdef BSS_SECTION_ASM_OP
498 #ifdef ASM_OUTPUT_ALIGNED_BSS
500 /* Utility function for targets to use in implementing
501 ASM_OUTPUT_ALIGNED_BSS.
502 ??? It is believed that this function will work in most cases so such
503 support is localized here. */
505 static void
506 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
507 const char *name, unsigned HOST_WIDE_INT size,
508 int align)
510 switch_to_section (bss_section);
511 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
512 #ifdef ASM_DECLARE_OBJECT_NAME
513 last_assemble_variable_decl = decl;
514 ASM_DECLARE_OBJECT_NAME (file, name, decl);
515 #else
516 /* Standard thing is just output label for the object. */
517 ASM_OUTPUT_LABEL (file, name);
518 #endif /* ASM_DECLARE_OBJECT_NAME */
519 ASM_OUTPUT_SKIP (file, size ? size : 1);
522 #endif
524 #endif /* BSS_SECTION_ASM_OP */
526 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
527 /* Return the hot section for function DECL. Return text_section for
528 null DECLs. */
530 static section *
531 hot_function_section (tree decl)
533 if (decl != NULL_TREE
534 && DECL_SECTION_NAME (decl) != NULL
535 && targetm_common.have_named_sections)
536 return get_named_section (decl, NULL, 0);
537 else
538 return text_section;
540 #endif
542 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
543 is NULL.
545 When DECL_SECTION_NAME is non-NULL and it is implicit section and
546 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
547 concatenate the name with NAMED_SECTION_SUFFIX.
548 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
550 section *
551 get_named_text_section (tree decl,
552 const char *text_section_name,
553 const char *named_section_suffix)
555 if (decl && DECL_SECTION_NAME (decl))
557 if (named_section_suffix)
559 const char *dsn = DECL_SECTION_NAME (decl);
560 const char *stripped_name;
561 char *name, *buffer;
563 name = (char *) alloca (strlen (dsn) + 1);
564 memcpy (name, dsn,
565 strlen (dsn) + 1);
567 stripped_name = targetm.strip_name_encoding (name);
569 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
570 return get_named_section (decl, buffer, 0);
572 else if (symtab_node::get (decl)->implicit_section)
574 const char *name;
576 /* Do not try to split gnu_linkonce functions. This gets somewhat
577 slipperly. */
578 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
579 return NULL;
580 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
581 name = targetm.strip_name_encoding (name);
582 return get_named_section (decl, ACONCAT ((text_section_name, ".",
583 name, NULL)), 0);
585 else
586 return NULL;
588 return get_named_section (decl, text_section_name, 0);
591 /* Choose named function section based on its frequency. */
593 section *
594 default_function_section (tree decl, enum node_frequency freq,
595 bool startup, bool exit)
597 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
598 /* Old GNU linkers have buggy --gc-section support, which sometimes
599 results in .gcc_except_table* sections being garbage collected. */
600 if (decl
601 && symtab_node::get (decl)->implicit_section)
602 return NULL;
603 #endif
605 if (!flag_reorder_functions
606 || !targetm_common.have_named_sections)
607 return NULL;
608 /* Startup code should go to startup subsection unless it is
609 unlikely executed (this happens especially with function splitting
610 where we can split away unnecessary parts of static constructors. */
611 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
613 /* During LTO the tp_first_run profiling will naturally place all
614 initialization code first. Using separate section is counter-productive
615 because startup only code may call functions which are no longer
616 startup only. */
617 if (!in_lto_p
618 || !cgraph_node::get (decl)->tp_first_run
619 || !opt_for_fn (decl, flag_profile_reorder_functions))
620 return get_named_text_section (decl, ".text.startup", NULL);
621 else
622 return NULL;
625 /* Similarly for exit. */
626 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
627 return get_named_text_section (decl, ".text.exit", NULL);
629 /* Group cold functions together, similarly for hot code. */
630 switch (freq)
632 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
633 return get_named_text_section (decl, ".text.unlikely", NULL);
634 case NODE_FREQUENCY_HOT:
635 return get_named_text_section (decl, ".text.hot", NULL);
636 /* FALLTHRU */
637 default:
638 return NULL;
642 /* Return the section for function DECL.
644 If DECL is NULL_TREE, return the text section. We can be passed
645 NULL_TREE under some circumstances by dbxout.cc at least.
647 If FORCE_COLD is true, return cold function section ignoring
648 the frequency info of cgraph_node. */
650 static section *
651 function_section_1 (tree decl, bool force_cold)
653 section *section = NULL;
654 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
655 bool startup = false, exit = false;
657 if (decl)
659 struct cgraph_node *node = cgraph_node::get (decl);
661 if (node)
663 freq = node->frequency;
664 startup = node->only_called_at_startup;
665 exit = node->only_called_at_exit;
668 if (force_cold)
669 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
671 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
672 if (decl != NULL_TREE
673 && DECL_SECTION_NAME (decl) != NULL)
675 if (targetm.asm_out.function_section)
676 section = targetm.asm_out.function_section (decl, freq,
677 startup, exit);
678 if (section)
679 return section;
680 return get_named_section (decl, NULL, 0);
682 else
683 return targetm.asm_out.select_section
684 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
685 symtab_node::get (decl)->definition_alignment ());
686 #else
687 if (targetm.asm_out.function_section)
688 section = targetm.asm_out.function_section (decl, freq, startup, exit);
689 if (section)
690 return section;
691 return hot_function_section (decl);
692 #endif
695 /* Return the section for function DECL.
697 If DECL is NULL_TREE, return the text section. We can be passed
698 NULL_TREE under some circumstances by dbxout.cc at least. */
700 section *
701 function_section (tree decl)
703 /* Handle cases where function splitting code decides
704 to put function entry point into unlikely executed section
705 despite the fact that the function itself is not cold
706 (i.e. it is called rarely but contains a hot loop that is
707 better to live in hot subsection for the code locality). */
708 return function_section_1 (decl,
709 first_function_block_is_cold);
712 /* Return the section for the current function, take IN_COLD_SECTION_P
713 into account. */
715 section *
716 current_function_section (void)
718 return function_section_1 (current_function_decl, in_cold_section_p);
721 /* Tell assembler to switch to unlikely-to-be-executed text section. */
723 section *
724 unlikely_text_section (void)
726 return function_section_1 (current_function_decl, true);
729 /* When called within a function context, return true if the function
730 has been assigned a cold text section and if SECT is that section.
731 When called outside a function context, return true if SECT is the
732 default cold section. */
734 bool
735 unlikely_text_section_p (section *sect)
737 return sect == function_section_1 (current_function_decl, true);
740 /* Switch to the other function partition (if inside of hot section
741 into cold section, otherwise into the hot section). */
743 void
744 switch_to_other_text_partition (void)
746 in_cold_section_p = !in_cold_section_p;
747 switch_to_section (current_function_section ());
750 /* Return the read-only or relocated read-only data section
751 associated with function DECL. */
753 section *
754 default_function_rodata_section (tree decl, bool relocatable)
756 const char* sname;
757 unsigned int flags;
759 flags = 0;
761 if (relocatable)
763 sname = ".data.rel.ro.local";
764 flags = (SECTION_WRITE | SECTION_RELRO);
766 else
767 sname = ".rodata";
769 if (decl && DECL_SECTION_NAME (decl))
771 const char *name = DECL_SECTION_NAME (decl);
773 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
775 const char *dot;
776 size_t len;
777 char* rname;
779 dot = strchr (name + 1, '.');
780 if (!dot)
781 dot = name;
782 len = strlen (dot) + strlen (sname) + 1;
783 rname = (char *) alloca (len);
785 strcpy (rname, sname);
786 strcat (rname, dot);
787 return get_section (rname, (SECTION_LINKONCE | flags), decl);
789 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo or
790 .gnu.linkonce.d.rel.ro.local.foo if the jump table is relocatable. */
791 else if (DECL_COMDAT_GROUP (decl)
792 && startswith (name, ".gnu.linkonce.t."))
794 size_t len;
795 char *rname;
797 if (relocatable)
799 len = strlen (name) + strlen (".rel.ro.local") + 1;
800 rname = (char *) alloca (len);
802 strcpy (rname, ".gnu.linkonce.d.rel.ro.local");
803 strcat (rname, name + 15);
805 else
807 len = strlen (name) + 1;
808 rname = (char *) alloca (len);
810 memcpy (rname, name, len);
811 rname[14] = 'r';
813 return get_section (rname, (SECTION_LINKONCE | flags), decl);
815 /* For .text.foo we want to use .rodata.foo. */
816 else if (flag_function_sections && flag_data_sections
817 && startswith (name, ".text."))
819 size_t len = strlen (name) + 1;
820 char *rname = (char *) alloca (len + strlen (sname) - 5);
822 memcpy (rname, sname, strlen (sname));
823 memcpy (rname + strlen (sname), name + 5, len - 5);
824 return get_section (rname, flags, decl);
828 if (relocatable)
829 return get_section (sname, flags, decl);
830 else
831 return readonly_data_section;
834 /* Return the read-only data section associated with function DECL
835 for targets where that section should be always the single
836 readonly data section. */
838 section *
839 default_no_function_rodata_section (tree, bool)
841 return readonly_data_section;
844 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
846 static const char *
847 function_mergeable_rodata_prefix (void)
849 section *s = targetm.asm_out.function_rodata_section (current_function_decl,
850 false);
851 if (SECTION_STYLE (s) == SECTION_NAMED)
852 return s->named.name;
853 else
854 return targetm.asm_out.mergeable_rodata_prefix;
857 /* Return the section to use for string merging. */
859 static section *
860 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
861 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
862 unsigned int flags ATTRIBUTE_UNUSED)
864 HOST_WIDE_INT len;
866 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
867 && TREE_CODE (decl) == STRING_CST
868 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
869 && align <= 256
870 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
871 && TREE_STRING_LENGTH (decl) == len)
873 scalar_int_mode mode;
874 unsigned int modesize;
875 const char *str;
876 HOST_WIDE_INT i;
877 int j, unit;
878 const char *prefix = function_mergeable_rodata_prefix ();
879 char *name = (char *) alloca (strlen (prefix) + 30);
881 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
882 modesize = GET_MODE_BITSIZE (mode);
883 if (modesize >= 8 && modesize <= 256
884 && (modesize & (modesize - 1)) == 0)
886 if (align < modesize)
887 align = modesize;
889 if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
890 return readonly_data_section;
892 str = TREE_STRING_POINTER (decl);
893 unit = GET_MODE_SIZE (mode);
895 /* Check for embedded NUL characters. */
896 for (i = 0; i < len; i += unit)
898 for (j = 0; j < unit; j++)
899 if (str[i + j] != '\0')
900 break;
901 if (j == unit)
902 break;
904 if (i == len - unit || (unit == 1 && i == len))
906 sprintf (name, "%s.str%d.%d", prefix,
907 modesize / 8, (int) (align / 8));
908 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
909 return get_section (name, flags, NULL);
914 return readonly_data_section;
917 /* Return the section to use for constant merging. */
919 section *
920 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
921 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
922 unsigned int flags ATTRIBUTE_UNUSED)
924 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
925 && mode != VOIDmode
926 && mode != BLKmode
927 && known_le (GET_MODE_BITSIZE (mode), align)
928 && align >= 8
929 && align <= 256
930 && (align & (align - 1)) == 0
931 && (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
933 const char *prefix = function_mergeable_rodata_prefix ();
934 char *name = (char *) alloca (strlen (prefix) + 30);
936 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
937 flags |= (align / 8) | SECTION_MERGE;
938 return get_section (name, flags, NULL);
940 return readonly_data_section;
943 /* Given NAME, a putative register name, discard any customary prefixes. */
945 static const char *
946 strip_reg_name (const char *name)
948 #ifdef REGISTER_PREFIX
949 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
950 name += strlen (REGISTER_PREFIX);
951 #endif
952 if (name[0] == '%' || name[0] == '#')
953 name++;
954 return name;
957 /* The user has asked for a DECL to have a particular name. Set (or
958 change) it in such a way that we don't prefix an underscore to
959 it. */
960 void
961 set_user_assembler_name (tree decl, const char *name)
963 char *starred = (char *) alloca (strlen (name) + 2);
964 starred[0] = '*';
965 strcpy (starred + 1, name);
966 symtab->change_decl_assembler_name (decl, get_identifier (starred));
967 SET_DECL_RTL (decl, NULL_RTX);
970 /* Decode an `asm' spec for a declaration as a register name.
971 Return the register number, or -1 if nothing specified,
972 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
973 or -3 if ASMSPEC is `cc' and is not recognized,
974 or -4 if ASMSPEC is `memory' and is not recognized.
975 Accept an exact spelling or a decimal number.
976 Prefixes such as % are optional. */
979 decode_reg_name_and_count (const char *asmspec, int *pnregs)
981 /* Presume just one register is clobbered. */
982 *pnregs = 1;
984 if (asmspec != 0)
986 int i;
988 /* Get rid of confusing prefixes. */
989 asmspec = strip_reg_name (asmspec);
991 /* Allow a decimal number as a "register name". */
992 for (i = strlen (asmspec) - 1; i >= 0; i--)
993 if (! ISDIGIT (asmspec[i]))
994 break;
995 if (asmspec[0] != 0 && i < 0)
997 i = atoi (asmspec);
998 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
999 return i;
1000 else
1001 return -2;
1004 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1005 if (reg_names[i][0]
1006 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
1007 return i;
1009 #ifdef OVERLAPPING_REGISTER_NAMES
1011 static const struct
1013 const char *const name;
1014 const int number;
1015 const int nregs;
1016 } table[] = OVERLAPPING_REGISTER_NAMES;
1018 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1019 if (table[i].name[0]
1020 && ! strcmp (asmspec, table[i].name))
1022 *pnregs = table[i].nregs;
1023 return table[i].number;
1026 #endif /* OVERLAPPING_REGISTER_NAMES */
1028 #ifdef ADDITIONAL_REGISTER_NAMES
1030 static const struct { const char *const name; const int number; } table[]
1031 = ADDITIONAL_REGISTER_NAMES;
1033 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1034 if (table[i].name[0]
1035 && ! strcmp (asmspec, table[i].name)
1036 && reg_names[table[i].number][0])
1037 return table[i].number;
1039 #endif /* ADDITIONAL_REGISTER_NAMES */
1041 if (!strcmp (asmspec, "memory"))
1042 return -4;
1044 if (!strcmp (asmspec, "cc"))
1045 return -3;
1047 return -2;
1050 return -1;
1054 decode_reg_name (const char *name)
1056 int count;
1057 return decode_reg_name_and_count (name, &count);
1061 /* Return true if DECL's initializer is suitable for a BSS section. */
1063 bool
1064 bss_initializer_p (const_tree decl, bool named)
1066 /* Do not put non-common constants into the .bss section, they belong in
1067 a readonly section, except when NAMED is true. */
1068 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1069 && (DECL_INITIAL (decl) == NULL
1070 /* In LTO we have no errors in program; error_mark_node is used
1071 to mark offlined constructors. */
1072 || (DECL_INITIAL (decl) == error_mark_node
1073 && !in_lto_p)
1074 || (flag_zero_initialized_in_bss
1075 && initializer_zerop (DECL_INITIAL (decl))
1076 /* A decl with the "persistent" attribute applied and
1077 explicitly initialized to 0 should not be treated as a BSS
1078 variable. */
1079 && !DECL_PERSISTENT_P (decl))));
1082 /* Compute the alignment of variable specified by DECL.
1083 DONT_OUTPUT_DATA is from assemble_variable. */
1085 void
1086 align_variable (tree decl, bool dont_output_data)
1088 unsigned int align = DECL_ALIGN (decl);
1090 /* In the case for initialing an array whose length isn't specified,
1091 where we have not yet been able to do the layout,
1092 figure out the proper alignment now. */
1093 if (dont_output_data && DECL_SIZE (decl) == 0
1094 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1095 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1097 /* Some object file formats have a maximum alignment which they support.
1098 In particular, a.out format supports a maximum alignment of 4. */
1099 if (align > MAX_OFILE_ALIGNMENT)
1101 error ("alignment of %q+D is greater than maximum object "
1102 "file alignment %d", decl,
1103 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1104 align = MAX_OFILE_ALIGNMENT;
1107 if (! DECL_USER_ALIGN (decl))
1109 #ifdef DATA_ABI_ALIGNMENT
1110 unsigned int data_abi_align
1111 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1112 /* For backwards compatibility, don't assume the ABI alignment for
1113 TLS variables. */
1114 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1115 align = data_abi_align;
1116 #endif
1118 /* On some machines, it is good to increase alignment sometimes.
1119 But as DECL_ALIGN is used both for actually emitting the variable
1120 and for code accessing the variable as guaranteed alignment, we
1121 can only increase the alignment if it is a performance optimization
1122 if the references to it must bind to the current definition. */
1123 if (decl_binds_to_current_def_p (decl)
1124 && !DECL_VIRTUAL_P (decl))
1126 #ifdef DATA_ALIGNMENT
1127 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1128 /* Don't increase alignment too much for TLS variables - TLS space
1129 is too precious. */
1130 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1131 align = data_align;
1132 #endif
1133 if (DECL_INITIAL (decl) != 0
1134 /* In LTO we have no errors in program; error_mark_node is used
1135 to mark offlined constructors. */
1136 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1138 unsigned int const_align
1139 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1140 /* Don't increase alignment too much for TLS variables - TLS
1141 space is too precious. */
1142 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1143 align = const_align;
1148 /* Reset the alignment in case we have made it tighter, so we can benefit
1149 from it in get_pointer_alignment. */
1150 SET_DECL_ALIGN (decl, align);
1153 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1154 beyond what align_variable returned. */
1156 static unsigned int
1157 get_variable_align (tree decl)
1159 unsigned int align = DECL_ALIGN (decl);
1161 /* For user aligned vars or static vars align_variable already did
1162 everything. */
1163 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1164 return align;
1166 #ifdef DATA_ABI_ALIGNMENT
1167 if (DECL_THREAD_LOCAL_P (decl))
1168 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1169 #endif
1171 /* For decls that bind to the current definition, align_variable
1172 did also everything, except for not assuming ABI required alignment
1173 of TLS variables. For other vars, increase the alignment here
1174 as an optimization. */
1175 if (!decl_binds_to_current_def_p (decl))
1177 /* On some machines, it is good to increase alignment sometimes. */
1178 #ifdef DATA_ALIGNMENT
1179 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1180 /* Don't increase alignment too much for TLS variables - TLS space
1181 is too precious. */
1182 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1183 align = data_align;
1184 #endif
1185 if (DECL_INITIAL (decl) != 0
1186 /* In LTO we have no errors in program; error_mark_node is used
1187 to mark offlined constructors. */
1188 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1190 unsigned int const_align
1191 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1192 /* Don't increase alignment too much for TLS variables - TLS space
1193 is too precious. */
1194 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1195 align = const_align;
1199 return align;
1202 /* Compute reloc for get_variable_section. The return value
1203 is a mask for which bit 1 indicates a global relocation, and bit 0
1204 indicates a local relocation. */
1207 compute_reloc_for_var (tree decl)
1209 int reloc;
1211 if (DECL_INITIAL (decl) == error_mark_node)
1212 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1213 else if (DECL_INITIAL (decl))
1214 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1215 else
1216 reloc = 0;
1218 return reloc;
1221 /* Return the section into which the given VAR_DECL or CONST_DECL
1222 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1223 section should be used wherever possible. */
1225 section *
1226 get_variable_section (tree decl, bool prefer_noswitch_p)
1228 addr_space_t as = ADDR_SPACE_GENERIC;
1229 int reloc;
1230 varpool_node *vnode = varpool_node::get (decl);
1231 if (vnode)
1233 vnode = vnode->ultimate_alias_target ();
1234 decl = vnode->decl;
1237 if (TREE_TYPE (decl) != error_mark_node)
1238 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1240 /* We need the constructor to figure out reloc flag. */
1241 if (vnode)
1242 vnode->get_constructor ();
1244 if (DECL_COMMON (decl)
1245 && !lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1247 /* If the decl has been given an explicit section name, or it resides
1248 in a non-generic address space, then it isn't common, and shouldn't
1249 be handled as such. */
1250 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1251 && ADDR_SPACE_GENERIC_P (as));
1252 if (DECL_THREAD_LOCAL_P (decl))
1253 return tls_comm_section;
1254 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1255 return comm_section;
1258 reloc = compute_reloc_for_var (decl);
1260 resolve_unique_section (decl, reloc, flag_data_sections);
1261 if (IN_NAMED_SECTION (decl))
1263 section *sect = get_named_section (decl, NULL, reloc);
1265 if ((sect->common.flags & SECTION_BSS)
1266 && !bss_initializer_p (decl, true))
1268 error_at (DECL_SOURCE_LOCATION (decl),
1269 "only zero initializers are allowed in section %qs",
1270 sect->named.name);
1271 DECL_INITIAL (decl) = error_mark_node;
1273 return sect;
1276 if (ADDR_SPACE_GENERIC_P (as)
1277 && !DECL_THREAD_LOCAL_P (decl)
1278 && !DECL_NOINIT_P (decl)
1279 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1280 && bss_initializer_p (decl))
1282 if (!TREE_PUBLIC (decl)
1283 && !((flag_sanitize & SANITIZE_ADDRESS)
1284 && asan_protect_global (decl)))
1285 return lcomm_section;
1286 if (bss_noswitch_section)
1287 return bss_noswitch_section;
1290 return targetm.asm_out.select_section (decl, reloc,
1291 get_variable_align (decl));
1294 /* Return the block into which object_block DECL should be placed. */
1296 static struct object_block *
1297 get_block_for_decl (tree decl)
1299 section *sect;
1301 if (VAR_P (decl))
1303 /* The object must be defined in this translation unit. */
1304 if (DECL_EXTERNAL (decl))
1305 return NULL;
1307 /* There's no point using object blocks for something that is
1308 isolated by definition. */
1309 if (DECL_COMDAT_GROUP (decl))
1310 return NULL;
1313 /* We can only calculate block offsets if the decl has a known
1314 constant size. */
1315 if (DECL_SIZE_UNIT (decl) == NULL)
1316 return NULL;
1317 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1318 return NULL;
1320 /* Find out which section should contain DECL. We cannot put it into
1321 an object block if it requires a standalone definition. */
1322 if (VAR_P (decl))
1323 align_variable (decl, 0);
1324 sect = get_variable_section (decl, true);
1325 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1326 return NULL;
1328 if (bool (lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1329 != bool (sect->common.flags & SECTION_RETAIN))
1330 return NULL;
1332 return get_block_for_section (sect);
1335 /* Make sure block symbol SYMBOL is in block BLOCK. */
1337 static void
1338 change_symbol_block (rtx symbol, struct object_block *block)
1340 if (block != SYMBOL_REF_BLOCK (symbol))
1342 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1343 SYMBOL_REF_BLOCK (symbol) = block;
1347 /* Return true if it is possible to put DECL in an object_block. */
1349 static bool
1350 use_blocks_for_decl_p (tree decl)
1352 struct symtab_node *snode;
1354 /* Don't create object blocks if each DECL is placed into a separate
1355 section because that will uselessly create a section anchor for
1356 each DECL. */
1357 if (flag_data_sections)
1358 return false;
1360 /* Only data DECLs can be placed into object blocks. */
1361 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1362 return false;
1364 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1365 are never used from code directly and we never want object block handling
1366 for those. */
1367 if (DECL_INITIAL (decl) == decl)
1368 return false;
1370 /* If this decl is an alias, then we don't want to emit a
1371 definition. */
1372 if (VAR_P (decl)
1373 && (snode = symtab_node::get (decl)) != NULL
1374 && snode->alias)
1375 return false;
1377 return targetm.use_blocks_for_decl_p (decl);
1380 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1381 until we find an identifier that is not itself a transparent alias.
1382 Modify the alias passed to it by reference (and all aliases on the
1383 way to the ultimate target), such that they do not have to be
1384 followed again, and return the ultimate target of the alias
1385 chain. */
1387 static inline tree
1388 ultimate_transparent_alias_target (tree *alias)
1390 tree target = *alias;
1392 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1394 gcc_assert (TREE_CHAIN (target));
1395 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1396 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1397 && ! TREE_CHAIN (target));
1398 *alias = target;
1401 return target;
1404 /* Return true if REGNUM is mentioned in ELIMINABLE_REGS as a from
1405 register number. */
1407 static bool
1408 eliminable_regno_p (int regnum)
1410 static const struct
1412 const int from;
1413 const int to;
1414 } eliminables[] = ELIMINABLE_REGS;
1415 for (size_t i = 0; i < ARRAY_SIZE (eliminables); i++)
1416 if (regnum == eliminables[i].from)
1417 return true;
1418 return false;
1421 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1422 have static storage duration. In other words, it should not be an
1423 automatic variable, including PARM_DECLs.
1425 There is, however, one exception: this function handles variables
1426 explicitly placed in a particular register by the user.
1428 This is never called for PARM_DECL nodes. */
1430 void
1431 make_decl_rtl (tree decl)
1433 const char *name = 0;
1434 int reg_number;
1435 tree id;
1436 rtx x;
1438 /* Check that we are not being given an automatic variable. */
1439 gcc_assert (TREE_CODE (decl) != PARM_DECL
1440 && TREE_CODE (decl) != RESULT_DECL);
1442 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1443 gcc_assert (!VAR_P (decl)
1444 || TREE_STATIC (decl)
1445 || TREE_PUBLIC (decl)
1446 || DECL_EXTERNAL (decl)
1447 || DECL_REGISTER (decl));
1449 /* And that we were not given a type or a label. */
1450 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1451 && TREE_CODE (decl) != LABEL_DECL);
1453 /* For a duplicate declaration, we can be called twice on the
1454 same DECL node. Don't discard the RTL already made. */
1455 if (DECL_RTL_SET_P (decl))
1457 /* If the old RTL had the wrong mode, fix the mode. */
1458 x = DECL_RTL (decl);
1459 if (GET_MODE (x) != DECL_MODE (decl))
1460 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1462 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1463 return;
1465 /* ??? Another way to do this would be to maintain a hashed
1466 table of such critters. Instead of adding stuff to a DECL
1467 to give certain attributes to it, we could use an external
1468 hash map from DECL to set of attributes. */
1470 /* Let the target reassign the RTL if it wants.
1471 This is necessary, for example, when one machine specific
1472 decl attribute overrides another. */
1473 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1475 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1476 on the new decl information. */
1477 if (MEM_P (x)
1478 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1479 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1480 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1482 return;
1485 /* If this variable belongs to the global constant pool, retrieve the
1486 pre-computed RTL or recompute it in LTO mode. */
1487 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1489 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1490 return;
1493 id = DECL_ASSEMBLER_NAME (decl);
1494 name = IDENTIFIER_POINTER (id);
1496 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1497 && DECL_REGISTER (decl))
1499 error ("register name not specified for %q+D", decl);
1501 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1503 const char *asmspec = name+1;
1504 machine_mode mode = DECL_MODE (decl);
1505 reg_number = decode_reg_name (asmspec);
1506 /* First detect errors in declaring global registers. */
1507 if (reg_number == -1)
1508 error ("register name not specified for %q+D", decl);
1509 else if (reg_number < 0)
1510 error ("invalid register name for %q+D", decl);
1511 else if (mode == BLKmode)
1512 error ("data type of %q+D isn%'t suitable for a register",
1513 decl);
1514 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1515 error ("the register specified for %q+D cannot be accessed"
1516 " by the current target", decl);
1517 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1518 error ("the register specified for %q+D is not general enough"
1519 " to be used as a register variable", decl);
1520 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1521 error ("register specified for %q+D isn%'t suitable for data type",
1522 decl);
1523 else if (reg_number != HARD_FRAME_POINTER_REGNUM
1524 && (reg_number == FRAME_POINTER_REGNUM
1525 #ifdef RETURN_ADDRESS_POINTER_REGNUM
1526 || reg_number == RETURN_ADDRESS_POINTER_REGNUM
1527 #endif
1528 || reg_number == ARG_POINTER_REGNUM)
1529 && eliminable_regno_p (reg_number))
1530 error ("register specified for %q+D is an internal GCC "
1531 "implementation detail", decl);
1532 /* Now handle properly declared static register variables. */
1533 else
1535 int nregs;
1537 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1539 DECL_INITIAL (decl) = 0;
1540 error ("global register variable has initial value");
1542 if (TREE_THIS_VOLATILE (decl))
1543 warning (OPT_Wvolatile_register_var,
1544 "optimization may eliminate reads and/or "
1545 "writes to register variables");
1547 /* If the user specified one of the eliminables registers here,
1548 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1549 confused with that register and be eliminated. This usage is
1550 somewhat suspect... */
1552 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1553 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1554 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1556 if (TREE_STATIC (decl))
1558 /* Make this register global, so not usable for anything
1559 else. */
1560 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1561 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1562 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1563 #endif
1564 nregs = hard_regno_nregs (reg_number, mode);
1565 while (nregs > 0)
1566 globalize_reg (decl, reg_number + --nregs);
1569 /* As a register variable, it has no section. */
1570 return;
1572 /* Avoid internal errors from invalid register
1573 specifications. */
1574 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1575 DECL_HARD_REGISTER (decl) = 0;
1576 /* Also avoid SSA inconsistencies by pretending this is an external
1577 decl now. */
1578 DECL_EXTERNAL (decl) = 1;
1579 return;
1581 /* Now handle ordinary static variables and functions (in memory).
1582 Also handle vars declared register invalidly. */
1583 else if (name[0] == '*')
1585 #ifdef REGISTER_PREFIX
1586 if (strlen (REGISTER_PREFIX) != 0)
1588 reg_number = decode_reg_name (name);
1589 if (reg_number >= 0 || reg_number == -3)
1590 error ("register name given for non-register variable %q+D", decl);
1592 #endif
1595 /* Specifying a section attribute on a variable forces it into a
1596 non-.bss section, and thus it cannot be common. */
1597 /* FIXME: In general this code should not be necessary because
1598 visibility pass is doing the same work. But notice_global_symbol
1599 is called early and it needs to make DECL_RTL to get the name.
1600 we take care of recomputing the DECL_RTL after visibility is changed. */
1601 if (VAR_P (decl)
1602 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1603 && DECL_SECTION_NAME (decl) != NULL
1604 && DECL_INITIAL (decl) == NULL_TREE
1605 && DECL_COMMON (decl))
1606 DECL_COMMON (decl) = 0;
1608 /* Variables can't be both common and weak. */
1609 if (VAR_P (decl) && DECL_WEAK (decl))
1610 DECL_COMMON (decl) = 0;
1612 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1613 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1614 else
1616 machine_mode address_mode = Pmode;
1617 if (TREE_TYPE (decl) != error_mark_node)
1619 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1620 address_mode = targetm.addr_space.address_mode (as);
1622 x = gen_rtx_SYMBOL_REF (address_mode, name);
1624 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1625 SET_SYMBOL_REF_DECL (x, decl);
1627 x = gen_rtx_MEM (DECL_MODE (decl), x);
1628 if (TREE_CODE (decl) != FUNCTION_DECL)
1629 set_mem_attributes (x, decl, 1);
1630 SET_DECL_RTL (decl, x);
1632 /* Optionally set flags or add text to the name to record information
1633 such as that it is a function name.
1634 If the name is changed, the macro ASM_OUTPUT_LABELREF
1635 will have to know how to strip this information. */
1636 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1639 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1640 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1641 rtl. */
1644 make_decl_rtl_for_debug (tree decl)
1646 unsigned int save_aliasing_flag;
1647 rtx rtl;
1649 if (DECL_RTL_SET_P (decl))
1650 return DECL_RTL (decl);
1652 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1653 call new_alias_set. If running with -fcompare-debug, sometimes
1654 we do not want to create alias sets that will throw the alias
1655 numbers off in the comparison dumps. So... clearing
1656 flag_strict_aliasing will keep new_alias_set() from creating a
1657 new set. */
1658 save_aliasing_flag = flag_strict_aliasing;
1659 flag_strict_aliasing = 0;
1661 rtl = DECL_RTL (decl);
1662 /* Reset DECL_RTL back, as various parts of the compiler expects
1663 DECL_RTL set meaning it is actually going to be output. */
1664 SET_DECL_RTL (decl, NULL);
1666 flag_strict_aliasing = save_aliasing_flag;
1667 return rtl;
1670 /* Output a string of literal assembler code
1671 for an `asm' keyword used between functions. */
1673 void
1674 assemble_asm (tree string)
1676 const char *p;
1677 app_enable ();
1679 if (TREE_CODE (string) == ADDR_EXPR)
1680 string = TREE_OPERAND (string, 0);
1682 p = TREE_STRING_POINTER (string);
1683 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1686 /* Write the address of the entity given by SYMBOL to SEC. */
1687 void
1688 assemble_addr_to_section (rtx symbol, section *sec)
1690 switch_to_section (sec);
1691 assemble_align (POINTER_SIZE);
1692 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1695 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1696 not) section for PRIORITY. */
1697 section *
1698 get_cdtor_priority_section (int priority, bool constructor_p)
1700 /* Buffer conservatively large enough for the full range of a 32-bit
1701 int plus the text below. */
1702 char buf[18];
1704 /* ??? This only works reliably with the GNU linker. */
1705 sprintf (buf, "%s.%.5u",
1706 constructor_p ? ".ctors" : ".dtors",
1707 /* Invert the numbering so the linker puts us in the proper
1708 order; constructors are run from right to left, and the
1709 linker sorts in increasing order. */
1710 MAX_INIT_PRIORITY - priority);
1711 return get_section (buf, SECTION_WRITE, NULL);
1714 void
1715 default_named_section_asm_out_destructor (rtx symbol, int priority)
1717 section *sec;
1719 if (priority != DEFAULT_INIT_PRIORITY)
1720 sec = get_cdtor_priority_section (priority,
1721 /*constructor_p=*/false);
1722 else
1723 sec = get_section (".dtors", SECTION_WRITE, NULL);
1725 assemble_addr_to_section (symbol, sec);
1728 #ifdef DTORS_SECTION_ASM_OP
1729 void
1730 default_dtor_section_asm_out_destructor (rtx symbol,
1731 int priority ATTRIBUTE_UNUSED)
1733 assemble_addr_to_section (symbol, dtors_section);
1735 #endif
1737 void
1738 default_named_section_asm_out_constructor (rtx symbol, int priority)
1740 section *sec;
1742 if (priority != DEFAULT_INIT_PRIORITY)
1743 sec = get_cdtor_priority_section (priority,
1744 /*constructor_p=*/true);
1745 else
1746 sec = get_section (".ctors", SECTION_WRITE, NULL);
1748 assemble_addr_to_section (symbol, sec);
1751 #ifdef CTORS_SECTION_ASM_OP
1752 void
1753 default_ctor_section_asm_out_constructor (rtx symbol,
1754 int priority ATTRIBUTE_UNUSED)
1756 assemble_addr_to_section (symbol, ctors_section);
1758 #endif
1760 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1761 a nonzero value if the constant pool should be output before the
1762 start of the function, or a zero value if the pool should output
1763 after the end of the function. The default is to put it before the
1764 start. */
1766 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1767 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1768 #endif
1770 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1771 to be output to assembler.
1772 Set first_global_object_name and weak_global_object_name as appropriate. */
1774 void
1775 notice_global_symbol (tree decl)
1777 const char **t = &first_global_object_name;
1779 if (first_global_object_name
1780 || !TREE_PUBLIC (decl)
1781 || DECL_EXTERNAL (decl)
1782 || !DECL_NAME (decl)
1783 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1784 || (TREE_CODE (decl) != FUNCTION_DECL
1785 && (!VAR_P (decl)
1786 || (DECL_COMMON (decl)
1787 && (DECL_INITIAL (decl) == 0
1788 || DECL_INITIAL (decl) == error_mark_node)))))
1789 return;
1791 /* We win when global object is found, but it is useful to know about weak
1792 symbol as well so we can produce nicer unique names. */
1793 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1794 t = &weak_global_object_name;
1796 if (!*t)
1798 tree id = DECL_ASSEMBLER_NAME (decl);
1799 ultimate_transparent_alias_target (&id);
1800 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1804 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1805 current function goes into the cold section, so that targets can use
1806 current_function_section during RTL expansion. DECL describes the
1807 function. */
1809 void
1810 decide_function_section (tree decl)
1812 first_function_block_is_cold = false;
1814 if (DECL_SECTION_NAME (decl))
1816 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1817 /* Calls to function_section rely on first_function_block_is_cold
1818 being accurate. */
1819 first_function_block_is_cold = (node
1820 && node->frequency
1821 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1824 in_cold_section_p = first_function_block_is_cold;
1827 /* Get the function's name, as described by its RTL. This may be
1828 different from the DECL_NAME name used in the source file. */
1829 const char *
1830 get_fnname_from_decl (tree decl)
1832 rtx x = DECL_RTL (decl);
1833 gcc_assert (MEM_P (x));
1834 x = XEXP (x, 0);
1835 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1836 return XSTR (x, 0);
1839 /* Output function label, possibly with accompanying metadata. No additional
1840 code or data is output after the label. */
1842 void
1843 assemble_function_label_raw (FILE *file, const char *name)
1845 ASM_OUTPUT_LABEL (file, name);
1846 assemble_function_label_final ();
1849 /* Finish outputting function label. Needs to be called when outputting
1850 function label without using assemble_function_label_raw (). */
1852 void
1853 assemble_function_label_final (void)
1855 if ((flag_sanitize & SANITIZE_ADDRESS)
1856 /* Notify ASAN only about the first function label. */
1857 && (in_cold_section_p == first_function_block_is_cold)
1858 /* Do not notify ASAN when called from, e.g., code_end (). */
1859 && cfun)
1860 asan_function_start ();
1863 /* Output assembler code for the constant pool of a function and associated
1864 with defining the name of the function. DECL describes the function.
1865 NAME is the function's name. For the constant pool, we use the current
1866 constant pool data. */
1868 void
1869 assemble_start_function (tree decl, const char *fnname)
1871 int align;
1872 char tmp_label[100];
1873 bool hot_label_written = false;
1875 if (crtl->has_bb_partition)
1877 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1878 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1879 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1880 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1881 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1882 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1883 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1884 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1885 const_labelno++;
1886 cold_function_name = NULL_TREE;
1888 else
1890 crtl->subsections.hot_section_label = NULL;
1891 crtl->subsections.cold_section_label = NULL;
1892 crtl->subsections.hot_section_end_label = NULL;
1893 crtl->subsections.cold_section_end_label = NULL;
1896 /* The following code does not need preprocessing in the assembler. */
1898 app_disable ();
1900 if (CONSTANT_POOL_BEFORE_FUNCTION)
1901 output_constant_pool (fnname, decl);
1903 align = symtab_node::get (decl)->definition_alignment ();
1905 /* Make sure the not and cold text (code) sections are properly
1906 aligned. This is necessary here in the case where the function
1907 has both hot and cold sections, because we don't want to re-set
1908 the alignment when the section switch happens mid-function. */
1910 if (crtl->has_bb_partition)
1912 first_function_block_is_cold = false;
1914 switch_to_section (unlikely_text_section ());
1915 assemble_align (align);
1916 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1918 /* When the function starts with a cold section, we need to explicitly
1919 align the hot section and write out the hot section label.
1920 But if the current function is a thunk, we do not have a CFG. */
1921 if (!cfun->is_thunk
1922 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1924 switch_to_section (text_section);
1925 assemble_align (align);
1926 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1927 hot_label_written = true;
1928 first_function_block_is_cold = true;
1930 in_cold_section_p = first_function_block_is_cold;
1934 /* Switch to the correct text section for the start of the function. */
1936 switch_to_section (function_section (decl), decl);
1937 if (crtl->has_bb_partition && !hot_label_written)
1938 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1940 /* Tell assembler to move to target machine's alignment for functions. */
1941 align = floor_log2 (align / BITS_PER_UNIT);
1942 /* Handle forced alignment. This really ought to apply to all functions,
1943 since it is used by patchable entries. */
1944 if (flag_min_function_alignment)
1945 align = MAX (align, floor_log2 (flag_min_function_alignment));
1947 if (align > 0)
1949 ASM_OUTPUT_ALIGN (asm_out_file, align);
1952 /* Handle a user-specified function alignment.
1953 Note that we still need to align to DECL_ALIGN, as above,
1954 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1955 if (! DECL_USER_ALIGN (decl)
1956 && align_functions.levels[0].log > align
1957 && optimize_function_for_speed_p (cfun))
1959 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1960 int align_log = align_functions.levels[0].log;
1961 #endif
1962 int max_skip = align_functions.levels[0].maxskip;
1963 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1964 && max_skip >= crtl->max_insn_address)
1965 max_skip = crtl->max_insn_address - 1;
1967 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1968 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1969 if (max_skip == align_functions.levels[0].maxskip)
1970 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1971 align_functions.levels[1].log,
1972 align_functions.levels[1].maxskip);
1973 #else
1974 ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
1975 #endif
1978 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1979 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1980 #endif
1982 if (!DECL_IGNORED_P (decl))
1983 (*debug_hooks->begin_function) (decl);
1985 /* Make function name accessible from other files, if appropriate. */
1987 if (TREE_PUBLIC (decl))
1989 notice_global_symbol (decl);
1991 globalize_decl (decl);
1993 maybe_assemble_visibility (decl);
1996 if (DECL_PRESERVE_P (decl))
1997 targetm.asm_out.mark_decl_preserved (fnname);
1999 unsigned short patch_area_size = crtl->patch_area_size;
2000 unsigned short patch_area_entry = crtl->patch_area_entry;
2002 /* Emit the patching area before the entry label, if any. */
2003 if (patch_area_entry > 0)
2004 targetm.asm_out.print_patchable_function_entry (asm_out_file,
2005 patch_area_entry, true);
2007 /* Do any machine/system dependent processing of the function name. */
2008 #ifdef ASM_DECLARE_FUNCTION_NAME
2009 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
2010 #else
2011 /* Standard thing is just output label for the function. */
2012 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
2013 #endif /* ASM_DECLARE_FUNCTION_NAME */
2015 /* And the area after the label. Record it if we haven't done so yet. */
2016 if (patch_area_size > patch_area_entry)
2017 targetm.asm_out.print_patchable_function_entry (asm_out_file,
2018 patch_area_size
2019 - patch_area_entry,
2020 patch_area_entry == 0);
2022 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
2023 saw_no_split_stack = true;
2026 /* Output assembler code associated with defining the size of the
2027 function. DECL describes the function. NAME is the function's name. */
2029 void
2030 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
2032 #ifdef ASM_DECLARE_FUNCTION_SIZE
2033 /* We could have switched section in the middle of the function. */
2034 if (crtl->has_bb_partition)
2035 switch_to_section (function_section (decl));
2036 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
2037 #endif
2038 if (! CONSTANT_POOL_BEFORE_FUNCTION)
2040 output_constant_pool (fnname, decl);
2041 switch_to_section (function_section (decl)); /* need to switch back */
2043 /* Output labels for end of hot/cold text sections (to be used by
2044 debug info.) */
2045 if (crtl->has_bb_partition)
2047 section *save_text_section;
2049 save_text_section = in_section;
2050 switch_to_section (unlikely_text_section ());
2051 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
2052 if (cold_function_name != NULL_TREE)
2053 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
2054 IDENTIFIER_POINTER (cold_function_name),
2055 decl);
2056 #endif
2057 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
2058 if (first_function_block_is_cold)
2059 switch_to_section (text_section);
2060 else
2061 switch_to_section (function_section (decl));
2062 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
2063 switch_to_section (save_text_section);
2067 /* Assemble code to leave SIZE bytes of zeros. */
2069 void
2070 assemble_zeros (unsigned HOST_WIDE_INT size)
2072 /* Do no output if -fsyntax-only. */
2073 if (flag_syntax_only)
2074 return;
2076 #ifdef ASM_NO_SKIP_IN_TEXT
2077 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
2078 so we must output 0s explicitly in the text section. */
2079 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
2081 unsigned HOST_WIDE_INT i;
2082 for (i = 0; i < size; i++)
2083 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
2085 else
2086 #endif
2087 if (size > 0)
2088 ASM_OUTPUT_SKIP (asm_out_file, size);
2091 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
2093 void
2094 assemble_align (unsigned int align)
2096 if (align > BITS_PER_UNIT)
2098 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2102 /* Assemble a string constant with the specified C string as contents. */
2104 void
2105 assemble_string (const char *p, int size)
2107 int pos = 0;
2108 int maximum = 2000;
2110 /* If the string is very long, split it up. */
2112 while (pos < size)
2114 int thissize = size - pos;
2115 if (thissize > maximum)
2116 thissize = maximum;
2118 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
2120 pos += thissize;
2121 p += thissize;
2126 /* A noswitch_section_callback for lcomm_section. */
2128 static bool
2129 emit_local (tree decl ATTRIBUTE_UNUSED,
2130 const char *name ATTRIBUTE_UNUSED,
2131 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2132 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2134 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2135 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2136 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2137 size, align);
2138 return true;
2139 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2140 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2141 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2142 return true;
2143 #else
2144 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2145 return false;
2146 #endif
2149 /* A noswitch_section_callback for bss_noswitch_section. */
2151 #if defined ASM_OUTPUT_ALIGNED_BSS
2152 static bool
2153 emit_bss (tree decl ATTRIBUTE_UNUSED,
2154 const char *name ATTRIBUTE_UNUSED,
2155 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2156 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2158 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2159 get_variable_align (decl));
2160 return true;
2162 #endif
2164 /* A noswitch_section_callback for comm_section. */
2166 static bool
2167 emit_common (tree decl ATTRIBUTE_UNUSED,
2168 const char *name ATTRIBUTE_UNUSED,
2169 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2170 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2172 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2173 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2174 size, get_variable_align (decl));
2175 return true;
2176 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2177 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2178 get_variable_align (decl));
2179 return true;
2180 #else
2181 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2182 return false;
2183 #endif
2186 /* A noswitch_section_callback for tls_comm_section. */
2188 static bool
2189 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2190 const char *name ATTRIBUTE_UNUSED,
2191 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2192 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2194 #ifdef ASM_OUTPUT_TLS_COMMON
2195 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2196 return true;
2197 #else
2198 sorry ("thread-local COMMON data not implemented");
2199 return true;
2200 #endif
2203 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2204 NAME is the name of DECL's SYMBOL_REF. */
2206 static void
2207 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2208 unsigned int align)
2210 unsigned HOST_WIDE_INT size, rounded;
2212 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2213 rounded = size;
2215 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2216 size += asan_red_zone_size (size);
2218 /* Don't allocate zero bytes of common,
2219 since that means "undefined external" in the linker. */
2220 if (size == 0)
2221 rounded = 1;
2223 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2224 so that each uninitialized object starts on such a boundary. */
2225 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2226 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2227 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2229 if (!sect->noswitch.callback (decl, name, size, rounded)
2230 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2231 error ("requested alignment for %q+D is greater than "
2232 "implemented alignment of %wu", decl, rounded);
2235 /* A subroutine of assemble_variable. Output the label and contents of
2236 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2237 is as for assemble_variable. */
2239 static void
2240 assemble_variable_contents (tree decl, const char *name,
2241 bool dont_output_data, bool merge_strings)
2243 /* Do any machine/system dependent processing of the object. */
2244 #ifdef ASM_DECLARE_OBJECT_NAME
2245 last_assemble_variable_decl = decl;
2246 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2247 #else
2248 /* Standard thing is just output label for the object. */
2249 ASM_OUTPUT_LABEL (asm_out_file, name);
2250 #endif /* ASM_DECLARE_OBJECT_NAME */
2252 if (!dont_output_data)
2254 /* Caller is supposed to use varpool_get_constructor when it wants
2255 to output the body. */
2256 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2257 if (DECL_INITIAL (decl)
2258 && DECL_INITIAL (decl) != error_mark_node
2259 && !initializer_zerop (DECL_INITIAL (decl)))
2260 /* Output the actual data. */
2261 output_constant (DECL_INITIAL (decl),
2262 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2263 get_variable_align (decl),
2264 false, merge_strings);
2265 else
2266 /* Leave space for it. */
2267 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2268 targetm.asm_out.decl_end ();
2272 /* Write out assembly for the variable DECL, which is not defined in
2273 the current translation unit. */
2274 void
2275 assemble_undefined_decl (tree decl)
2277 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2278 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2281 /* Assemble everything that is needed for a variable or function declaration.
2282 Not used for automatic variables, and not used for function definitions.
2283 Should not be called for variables of incomplete structure type.
2285 TOP_LEVEL is nonzero if this variable has file scope.
2286 AT_END is nonzero if this is the special handling, at end of compilation,
2287 to define things that have had only tentative definitions.
2288 DONT_OUTPUT_DATA if nonzero means don't actually output the
2289 initial value (that will be done by the caller). */
2291 void
2292 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2293 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2295 const char *name;
2296 rtx decl_rtl, symbol;
2297 section *sect;
2298 unsigned int align;
2299 bool asan_protected = false;
2301 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2302 gcc_assert (VAR_P (decl));
2304 /* Emulated TLS had better not get this far. */
2305 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2307 last_assemble_variable_decl = 0;
2309 /* Normally no need to say anything here for external references,
2310 since assemble_external is called by the language-specific code
2311 when a declaration is first seen. */
2313 if (DECL_EXTERNAL (decl))
2314 return;
2316 /* Do nothing for global register variables. */
2317 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2319 TREE_ASM_WRITTEN (decl) = 1;
2320 return;
2323 /* If type was incomplete when the variable was declared,
2324 see if it is complete now. */
2326 if (DECL_SIZE (decl) == 0)
2327 layout_decl (decl, 0);
2329 /* Still incomplete => don't allocate it; treat the tentative defn
2330 (which is what it must have been) as an `extern' reference. */
2332 if (!dont_output_data && DECL_SIZE (decl) == 0)
2334 error ("storage size of %q+D isn%'t known", decl);
2335 TREE_ASM_WRITTEN (decl) = 1;
2336 return;
2339 /* The first declaration of a variable that comes through this function
2340 decides whether it is global (in C, has external linkage)
2341 or local (in C, has internal linkage). So do nothing more
2342 if this function has already run. */
2344 if (TREE_ASM_WRITTEN (decl))
2345 return;
2347 /* Make sure targetm.encode_section_info is invoked before we set
2348 ASM_WRITTEN. */
2349 decl_rtl = DECL_RTL (decl);
2351 TREE_ASM_WRITTEN (decl) = 1;
2353 /* Do no output if -fsyntax-only. */
2354 if (flag_syntax_only)
2355 return;
2357 if (! dont_output_data
2358 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2360 error ("size of variable %q+D is too large", decl);
2361 return;
2364 gcc_assert (MEM_P (decl_rtl));
2365 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2366 symbol = XEXP (decl_rtl, 0);
2368 /* If this symbol belongs to the tree constant pool, output the constant
2369 if it hasn't already been written. */
2370 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2372 tree decl = SYMBOL_REF_DECL (symbol);
2373 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2374 output_constant_def_contents (symbol);
2375 return;
2378 app_disable ();
2380 name = XSTR (symbol, 0);
2381 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2382 notice_global_symbol (decl);
2384 /* Compute the alignment of this data. */
2386 align_variable (decl, dont_output_data);
2388 if ((flag_sanitize & SANITIZE_ADDRESS)
2389 && asan_protect_global (decl))
2391 asan_protected = true;
2392 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2393 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2396 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2398 align = get_variable_align (decl);
2400 if (TREE_PUBLIC (decl))
2401 maybe_assemble_visibility (decl);
2403 if (DECL_PRESERVE_P (decl))
2404 targetm.asm_out.mark_decl_preserved (name);
2406 /* First make the assembler name(s) global if appropriate. */
2407 sect = get_variable_section (decl, false);
2408 if (TREE_PUBLIC (decl)
2409 && (sect->common.flags & SECTION_COMMON) == 0)
2410 globalize_decl (decl);
2412 /* Output any data that we will need to use the address of. */
2413 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2414 output_addressed_constants (DECL_INITIAL (decl), 0);
2416 /* dbxout.cc needs to know this. */
2417 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2418 DECL_IN_TEXT_SECTION (decl) = 1;
2420 /* If the decl is part of an object_block, make sure that the decl
2421 has been positioned within its block, but do not write out its
2422 definition yet. output_object_blocks will do that later. */
2423 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2425 gcc_assert (!dont_output_data);
2426 place_block_symbol (symbol);
2428 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2429 assemble_noswitch_variable (decl, name, sect, align);
2430 else
2432 /* Special-case handling of vtv comdat sections. */
2433 if (SECTION_STYLE (sect) == SECTION_NAMED
2434 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2435 handle_vtv_comdat_section (sect, decl);
2436 else
2437 switch_to_section (sect, decl);
2438 if (align > BITS_PER_UNIT)
2439 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2440 assemble_variable_contents (decl, name, dont_output_data,
2441 (sect->common.flags & SECTION_MERGE)
2442 && (sect->common.flags & SECTION_STRINGS));
2443 if (asan_protected)
2445 unsigned HOST_WIDE_INT int size
2446 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2447 assemble_zeros (asan_red_zone_size (size));
2452 /* Return true if type TYPE contains any pointers. */
2454 static bool
2455 contains_pointers_p (tree type)
2457 switch (TREE_CODE (type))
2459 case POINTER_TYPE:
2460 case REFERENCE_TYPE:
2461 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2462 so I'll play safe and return 1. */
2463 case OFFSET_TYPE:
2464 return true;
2466 case RECORD_TYPE:
2467 case UNION_TYPE:
2468 case QUAL_UNION_TYPE:
2470 tree fields;
2471 /* For a type that has fields, see if the fields have pointers. */
2472 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2473 if (TREE_CODE (fields) == FIELD_DECL
2474 && contains_pointers_p (TREE_TYPE (fields)))
2475 return true;
2476 return false;
2479 case ARRAY_TYPE:
2480 /* An array type contains pointers if its element type does. */
2481 return contains_pointers_p (TREE_TYPE (type));
2483 default:
2484 return false;
2488 /* We delay assemble_external processing until
2489 the compilation unit is finalized. This is the best we can do for
2490 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2491 it all the way to final. See PR 17982 for further discussion. */
2492 static GTY(()) tree pending_assemble_externals;
2494 /* A similar list of pending libcall symbols. We only want to declare
2495 symbols that are actually used in the final assembly. */
2496 static GTY(()) rtx pending_libcall_symbols;
2498 #ifdef ASM_OUTPUT_EXTERNAL
2499 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2500 As a result, assemble_external can be called after the list of externals
2501 is processed and the pointer set destroyed. */
2502 static bool pending_assemble_externals_processed;
2504 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2505 TREE_LIST in assemble_external. */
2506 static hash_set<tree> *pending_assemble_externals_set;
2508 /* True if DECL is a function decl for which no out-of-line copy exists.
2509 It is assumed that DECL's assembler name has been set. */
2511 static bool
2512 incorporeal_function_p (tree decl)
2514 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl))
2516 const char *name;
2518 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2519 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2520 return true;
2522 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2523 /* Atomic or sync builtins which have survived this far will be
2524 resolved externally and therefore are not incorporeal. */
2525 if (startswith (name, "__builtin_"))
2526 return true;
2528 return false;
2531 /* Actually do the tests to determine if this is necessary, and invoke
2532 ASM_OUTPUT_EXTERNAL. */
2533 static void
2534 assemble_external_real (tree decl)
2536 rtx rtl = DECL_RTL (decl);
2538 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2539 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2540 && !incorporeal_function_p (decl))
2542 /* Some systems do require some output. */
2543 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2544 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2547 #endif
2549 void
2550 process_pending_assemble_externals (void)
2552 #ifdef ASM_OUTPUT_EXTERNAL
2553 tree list;
2554 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2555 assemble_external_real (TREE_VALUE (list));
2557 for (rtx list = pending_libcall_symbols; list; list = XEXP (list, 1))
2559 rtx symbol = XEXP (list, 0);
2560 const char *name = targetm.strip_name_encoding (XSTR (symbol, 0));
2561 tree id = get_identifier (name);
2562 if (TREE_SYMBOL_REFERENCED (id))
2563 targetm.asm_out.external_libcall (symbol);
2566 pending_assemble_externals = 0;
2567 pending_assemble_externals_processed = true;
2568 pending_libcall_symbols = NULL_RTX;
2569 delete pending_assemble_externals_set;
2570 #endif
2573 /* This TREE_LIST contains any weak symbol declarations waiting
2574 to be emitted. */
2575 static GTY(()) tree weak_decls;
2577 /* Output something to declare an external symbol to the assembler,
2578 and qualifiers such as weakness. (Most assemblers don't need
2579 extern declaration, so we normally output nothing.) Do nothing if
2580 DECL is not external. */
2582 void
2583 assemble_external (tree decl ATTRIBUTE_UNUSED)
2585 /* Make sure that the ASM_OUT_FILE is open.
2586 If it's not, we should not be calling this function. */
2587 gcc_assert (asm_out_file);
2589 /* In a perfect world, the following condition would be true.
2590 Sadly, the Go front end emit assembly *from the front end*,
2591 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2592 #if 0
2593 /* This function should only be called if we are expanding, or have
2594 expanded, to RTL.
2595 Ideally, only final.cc would be calling this function, but it is
2596 not clear whether that would break things somehow. See PR 17982
2597 for further discussion. */
2598 gcc_assert (state == EXPANSION
2599 || state == FINISHED);
2600 #endif
2602 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2603 return;
2605 /* We want to output annotation for weak and external symbols at
2606 very last to check if they are references or not. */
2608 if (TARGET_SUPPORTS_WEAK
2609 && DECL_WEAK (decl)
2610 /* TREE_STATIC is a weird and abused creature which is not
2611 generally the right test for whether an entity has been
2612 locally emitted, inlined or otherwise not-really-extern, but
2613 for declarations that can be weak, it happens to be
2614 match. */
2615 && !TREE_STATIC (decl)
2616 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2617 && value_member (decl, weak_decls) == NULL_TREE)
2618 weak_decls = tree_cons (NULL, decl, weak_decls);
2620 #ifdef ASM_OUTPUT_EXTERNAL
2621 if (pending_assemble_externals_processed)
2623 assemble_external_real (decl);
2624 return;
2627 if (! pending_assemble_externals_set->add (decl))
2628 pending_assemble_externals = tree_cons (NULL, decl,
2629 pending_assemble_externals);
2630 #endif
2633 /* Similar, for calling a library function FUN. */
2635 void
2636 assemble_external_libcall (rtx fun)
2638 /* Declare library function name external when first used, if nec. */
2639 if (! SYMBOL_REF_USED (fun))
2641 #ifdef ASM_OUTPUT_EXTERNAL
2642 gcc_assert (!pending_assemble_externals_processed);
2643 #endif
2644 SYMBOL_REF_USED (fun) = 1;
2645 /* Make sure the libcall symbol is in the symtab so any
2646 reference to it will mark its tree node as referenced, via
2647 assemble_name_resolve. These are eventually emitted, if
2648 used, in process_pending_assemble_externals. */
2649 const char *name = targetm.strip_name_encoding (XSTR (fun, 0));
2650 get_identifier (name);
2651 pending_libcall_symbols = gen_rtx_EXPR_LIST (VOIDmode, fun,
2652 pending_libcall_symbols);
2656 /* Assemble a label named NAME. */
2658 void
2659 assemble_label (FILE *file, const char *name)
2661 ASM_OUTPUT_LABEL (file, name);
2664 /* Set the symbol_referenced flag for ID. */
2665 void
2666 mark_referenced (tree id)
2668 TREE_SYMBOL_REFERENCED (id) = 1;
2671 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2672 void
2673 mark_decl_referenced (tree decl)
2675 if (TREE_CODE (decl) == FUNCTION_DECL)
2677 /* Extern inline functions don't become needed when referenced.
2678 If we know a method will be emitted in other TU and no new
2679 functions can be marked reachable, just use the external
2680 definition. */
2681 struct cgraph_node *node = cgraph_node::get_create (decl);
2682 if (!DECL_EXTERNAL (decl)
2683 && !node->definition)
2684 node->mark_force_output ();
2686 else if (VAR_P (decl))
2688 varpool_node *node = varpool_node::get_create (decl);
2689 /* C++ frontend use mark_decl_references to force COMDAT variables
2690 to be output that might appear dead otherwise. */
2691 node->force_output = true;
2693 /* else do nothing - we can get various sorts of CST nodes here,
2694 which do not need to be marked. */
2698 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2699 starts with a *, the rest of NAME is output verbatim. Otherwise
2700 NAME is transformed in a target-specific way (usually by the
2701 addition of an underscore). */
2703 void
2704 assemble_name_raw (FILE *file, const char *name)
2706 if (name[0] == '*')
2707 fputs (&name[1], file);
2708 else
2709 ASM_OUTPUT_LABELREF (file, name);
2712 /* Return NAME that should actually be emitted, looking through
2713 transparent aliases. If NAME refers to an entity that is also
2714 represented as a tree (like a function or variable), mark the entity
2715 as referenced. */
2716 const char *
2717 assemble_name_resolve (const char *name)
2719 const char *real_name = targetm.strip_name_encoding (name);
2720 tree id = maybe_get_identifier (real_name);
2722 if (id)
2724 tree id_orig = id;
2726 mark_referenced (id);
2727 ultimate_transparent_alias_target (&id);
2728 if (id != id_orig)
2729 name = IDENTIFIER_POINTER (id);
2730 gcc_assert (! TREE_CHAIN (id));
2733 return name;
2736 /* Like assemble_name_raw, but should be used when NAME might refer to
2737 an entity that is also represented as a tree (like a function or
2738 variable). If NAME does refer to such an entity, that entity will
2739 be marked as referenced. */
2741 void
2742 assemble_name (FILE *file, const char *name)
2744 assemble_name_raw (file, assemble_name_resolve (name));
2747 /* Allocate SIZE bytes writable static space with a gensym name
2748 and return an RTX to refer to its address. */
2751 assemble_static_space (unsigned HOST_WIDE_INT size)
2753 char name[17];
2754 const char *namestring;
2755 rtx x;
2757 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2758 ++const_labelno;
2759 namestring = ggc_strdup (name);
2761 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2762 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2764 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2765 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2766 BIGGEST_ALIGNMENT);
2767 #else
2768 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2769 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2770 #else
2772 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2773 so that each uninitialized object starts on such a boundary. */
2774 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2775 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2776 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2777 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2778 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2779 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2781 #endif
2782 #endif
2783 return x;
2786 /* Assemble the static constant template for function entry trampolines.
2787 This is done at most once per compilation.
2788 Returns an RTX for the address of the template. */
2790 static GTY(()) rtx initial_trampoline;
2793 assemble_trampoline_template (void)
2795 char label[256];
2796 const char *name;
2797 int align;
2798 rtx symbol;
2800 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2802 if (initial_trampoline)
2803 return initial_trampoline;
2805 /* By default, put trampoline templates in read-only data section. */
2807 #ifdef TRAMPOLINE_SECTION
2808 switch_to_section (TRAMPOLINE_SECTION);
2809 #else
2810 switch_to_section (readonly_data_section);
2811 #endif
2813 /* Write the assembler code to define one. */
2814 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2815 if (align > 0)
2816 ASM_OUTPUT_ALIGN (asm_out_file, align);
2818 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2819 targetm.asm_out.trampoline_template (asm_out_file);
2821 /* Record the rtl to refer to it. */
2822 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2823 name = ggc_strdup (label);
2824 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2825 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2827 initial_trampoline = gen_const_mem (BLKmode, symbol);
2828 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2829 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2831 return initial_trampoline;
2834 /* A and B are either alignments or offsets. Return the minimum alignment
2835 that may be assumed after adding the two together. */
2837 static inline unsigned
2838 min_align (unsigned int a, unsigned int b)
2840 return least_bit_hwi (a | b);
2843 /* Return the assembler directive for creating a given kind of integer
2844 object. SIZE is the number of bytes in the object and ALIGNED_P
2845 indicates whether it is known to be aligned. Return NULL if the
2846 assembly dialect has no such directive.
2848 The returned string should be printed at the start of a new line and
2849 be followed immediately by the object's initial value. */
2851 const char *
2852 integer_asm_op (int size, int aligned_p)
2854 struct asm_int_op *ops;
2856 if (aligned_p)
2857 ops = &targetm.asm_out.aligned_op;
2858 else
2859 ops = &targetm.asm_out.unaligned_op;
2861 switch (size)
2863 case 1:
2864 return targetm.asm_out.byte_op;
2865 case 2:
2866 return ops->hi;
2867 case 3:
2868 return ops->psi;
2869 case 4:
2870 return ops->si;
2871 case 5:
2872 case 6:
2873 case 7:
2874 return ops->pdi;
2875 case 8:
2876 return ops->di;
2877 case 9:
2878 case 10:
2879 case 11:
2880 case 12:
2881 case 13:
2882 case 14:
2883 case 15:
2884 return ops->pti;
2885 case 16:
2886 return ops->ti;
2887 default:
2888 return NULL;
2892 /* Use directive OP to assemble an integer object X. Print OP at the
2893 start of the line, followed immediately by the value of X. */
2895 void
2896 assemble_integer_with_op (const char *op, rtx x)
2898 fputs (op, asm_out_file);
2899 output_addr_const (asm_out_file, x);
2900 fputc ('\n', asm_out_file);
2903 /* The default implementation of the asm_out.integer target hook. */
2905 bool
2906 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2907 unsigned int size ATTRIBUTE_UNUSED,
2908 int aligned_p ATTRIBUTE_UNUSED)
2910 const char *op = integer_asm_op (size, aligned_p);
2911 /* Avoid GAS bugs for large values. Specifically negative values whose
2912 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2913 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2914 return false;
2915 return op && (assemble_integer_with_op (op, x), true);
2918 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2919 the alignment of the integer in bits. Return 1 if we were able to output
2920 the constant, otherwise 0. We must be able to output the constant,
2921 if FORCE is nonzero. */
2923 bool
2924 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2926 int aligned_p;
2928 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2930 /* See if the target hook can handle this kind of object. */
2931 if (targetm.asm_out.integer (x, size, aligned_p))
2932 return true;
2934 /* If the object is a multi-byte one, try splitting it up. Split
2935 it into words it if is multi-word, otherwise split it into bytes. */
2936 if (size > 1)
2938 machine_mode omode, imode;
2939 unsigned int subalign;
2940 unsigned int subsize, i;
2941 enum mode_class mclass;
2943 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2944 subalign = MIN (align, subsize * BITS_PER_UNIT);
2945 if (GET_CODE (x) == CONST_FIXED)
2946 mclass = GET_MODE_CLASS (GET_MODE (x));
2947 else
2948 mclass = MODE_INT;
2950 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2951 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2953 for (i = 0; i < size; i += subsize)
2955 rtx partial = simplify_subreg (omode, x, imode, i);
2956 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2957 break;
2959 if (i == size)
2960 return true;
2962 /* If we've printed some of it, but not all of it, there's no going
2963 back now. */
2964 gcc_assert (!i);
2967 gcc_assert (!force);
2969 return false;
2972 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
2973 is the alignment of the constant in bits. If REVERSE is true, D is output
2974 in reverse storage order. */
2976 void
2977 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2978 bool reverse)
2980 long data[4] = {0, 0, 0, 0};
2981 int bitsize, nelts, nunits, units_per;
2982 rtx elt;
2984 /* This is hairy. We have a quantity of known size. real_to_target
2985 will put it into an array of *host* longs, 32 bits per element
2986 (even if long is more than 32 bits). We need to determine the
2987 number of array elements that are occupied (nelts) and the number
2988 of *target* min-addressable units that will be occupied in the
2989 object file (nunits). We cannot assume that 32 divides the
2990 mode's bitsize (size * BITS_PER_UNIT) evenly.
2992 size * BITS_PER_UNIT is used here to make sure that padding bits
2993 (which might appear at either end of the value; real_to_target
2994 will include the padding bits in its output array) are included. */
2996 nunits = GET_MODE_SIZE (mode);
2997 bitsize = nunits * BITS_PER_UNIT;
2998 nelts = CEIL (bitsize, 32);
2999 units_per = 32 / BITS_PER_UNIT;
3001 real_to_target (data, &d, mode);
3003 /* Put out the first word with the specified alignment. */
3004 unsigned int chunk_nunits = MIN (nunits, units_per);
3005 if (reverse)
3006 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
3007 else
3008 elt = GEN_INT (sext_hwi (data[0], chunk_nunits * BITS_PER_UNIT));
3009 assemble_integer (elt, chunk_nunits, align, 1);
3010 nunits -= chunk_nunits;
3012 /* Subsequent words need only 32-bit alignment. */
3013 align = min_align (align, 32);
3015 for (int i = 1; i < nelts; i++)
3017 chunk_nunits = MIN (nunits, units_per);
3018 if (reverse)
3019 elt = flip_storage_order (SImode,
3020 gen_int_mode (data[nelts - 1 - i], SImode));
3021 else
3022 elt = GEN_INT (sext_hwi (data[i], chunk_nunits * BITS_PER_UNIT));
3023 assemble_integer (elt, chunk_nunits, align, 1);
3024 nunits -= chunk_nunits;
3028 /* Given an expression EXP with a constant value,
3029 reduce it to the sum of an assembler symbol and an integer.
3030 Store them both in the structure *VALUE.
3031 EXP must be reducible. */
3033 class addr_const {
3034 public:
3035 rtx base;
3036 poly_int64 offset;
3039 static void
3040 decode_addr_const (tree exp, class addr_const *value)
3042 tree target = TREE_OPERAND (exp, 0);
3043 poly_int64 offset = 0;
3044 rtx x;
3046 while (1)
3048 poly_int64 bytepos;
3049 if (TREE_CODE (target) == COMPONENT_REF
3050 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
3051 &bytepos))
3053 offset += bytepos;
3054 target = TREE_OPERAND (target, 0);
3056 else if (TREE_CODE (target) == ARRAY_REF
3057 || TREE_CODE (target) == ARRAY_RANGE_REF)
3059 /* Truncate big offset. */
3060 offset
3061 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
3062 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
3063 target = TREE_OPERAND (target, 0);
3065 else if (TREE_CODE (target) == MEM_REF
3066 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
3068 offset += mem_ref_offset (target).force_shwi ();
3069 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
3071 else if (INDIRECT_REF_P (target)
3072 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
3073 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
3074 == ADDR_EXPR)
3075 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
3076 else
3077 break;
3080 switch (TREE_CODE (target))
3082 case VAR_DECL:
3083 case FUNCTION_DECL:
3084 x = DECL_RTL (target);
3085 break;
3087 case LABEL_DECL:
3088 x = gen_rtx_MEM (FUNCTION_MODE,
3089 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
3090 break;
3092 case REAL_CST:
3093 case FIXED_CST:
3094 case STRING_CST:
3095 case COMPLEX_CST:
3096 case CONSTRUCTOR:
3097 case INTEGER_CST:
3098 x = lookup_constant_def (target);
3099 /* Should have been added by output_addressed_constants. */
3100 gcc_assert (x);
3101 break;
3103 case INDIRECT_REF:
3104 /* This deals with absolute addresses. */
3105 offset += tree_to_shwi (TREE_OPERAND (target, 0));
3106 x = gen_rtx_MEM (QImode,
3107 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
3108 break;
3110 case COMPOUND_LITERAL_EXPR:
3111 gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
3112 x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
3113 break;
3115 default:
3116 gcc_unreachable ();
3119 gcc_assert (MEM_P (x));
3120 x = XEXP (x, 0);
3122 value->base = x;
3123 value->offset = offset;
3126 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
3128 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
3130 /* Constant pool accessor function. */
3132 hash_table<tree_descriptor_hasher> *
3133 constant_pool_htab (void)
3135 return const_desc_htab;
3138 /* Compute a hash code for a constant expression. */
3140 hashval_t
3141 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3143 return ptr->hash;
3146 static hashval_t
3147 const_hash_1 (const tree exp)
3149 const char *p;
3150 hashval_t hi;
3151 int len, i;
3152 enum tree_code code = TREE_CODE (exp);
3154 /* Either set P and LEN to the address and len of something to hash and
3155 exit the switch or return a value. */
3157 switch (code)
3159 case INTEGER_CST:
3160 p = (char *) &TREE_INT_CST_ELT (exp, 0);
3161 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3162 break;
3164 case REAL_CST:
3165 return real_hash (TREE_REAL_CST_PTR (exp));
3167 case FIXED_CST:
3168 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3170 case STRING_CST:
3171 p = TREE_STRING_POINTER (exp);
3172 len = TREE_STRING_LENGTH (exp);
3173 break;
3175 case COMPLEX_CST:
3176 return (const_hash_1 (TREE_REALPART (exp)) * 5
3177 + const_hash_1 (TREE_IMAGPART (exp)));
3179 case VECTOR_CST:
3181 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3182 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3183 unsigned int count = vector_cst_encoded_nelts (exp);
3184 for (unsigned int i = 0; i < count; ++i)
3185 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3186 return hi;
3189 case CONSTRUCTOR:
3191 unsigned HOST_WIDE_INT idx;
3192 tree value;
3194 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3196 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3197 if (value)
3198 hi = hi * 603 + const_hash_1 (value);
3200 return hi;
3203 case ADDR_EXPR:
3204 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3205 return const_hash_1 (TREE_OPERAND (exp, 0));
3207 /* Fallthru. */
3208 case FDESC_EXPR:
3210 class addr_const value;
3212 decode_addr_const (exp, &value);
3213 switch (GET_CODE (value.base))
3215 case SYMBOL_REF:
3216 /* Don't hash the address of the SYMBOL_REF;
3217 only use the offset and the symbol name. */
3218 hi = value.offset.coeffs[0];
3219 p = XSTR (value.base, 0);
3220 for (i = 0; p[i] != 0; i++)
3221 hi = ((hi * 613) + (unsigned) (p[i]));
3222 break;
3224 case LABEL_REF:
3225 hi = (value.offset.coeffs[0]
3226 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3227 break;
3229 default:
3230 gcc_unreachable ();
3233 return hi;
3235 case PLUS_EXPR:
3236 case POINTER_PLUS_EXPR:
3237 case MINUS_EXPR:
3238 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3239 + const_hash_1 (TREE_OPERAND (exp, 1)));
3241 CASE_CONVERT:
3242 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3244 default:
3245 /* A language specific constant. Just hash the code. */
3246 return code;
3249 /* Compute hashing function. */
3250 hi = len;
3251 for (i = 0; i < len; i++)
3252 hi = ((hi * 613) + (unsigned) (p[i]));
3254 return hi;
3257 /* Wrapper of compare_constant, for the htab interface. */
3258 bool
3259 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3260 constant_descriptor_tree *c2)
3262 if (c1->hash != c2->hash)
3263 return false;
3264 return compare_constant (c1->value, c2->value);
3267 /* Compare t1 and t2, and return true only if they are known to result in
3268 the same bit pattern on output. */
3270 static bool
3271 compare_constant (const tree t1, const tree t2)
3273 enum tree_code typecode;
3275 if (t1 == NULL_TREE)
3276 return t2 == NULL_TREE;
3277 if (t2 == NULL_TREE)
3278 return false;
3280 if (TREE_CODE (t1) != TREE_CODE (t2))
3281 return false;
3283 switch (TREE_CODE (t1))
3285 case INTEGER_CST:
3286 /* Integer constants are the same only if the same width of type. */
3287 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3288 return false;
3289 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3290 return false;
3291 return tree_int_cst_equal (t1, t2);
3293 case REAL_CST:
3294 /* Real constants are the same only if the same width of type. In
3295 addition to the same width, we need to check whether the modes are the
3296 same. There might be two floating point modes that are the same size
3297 but have different representations, such as the PowerPC that has 2
3298 different 128-bit floating point types (IBM extended double and IEEE
3299 128-bit floating point). */
3300 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3301 return false;
3302 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3303 return false;
3304 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3306 case FIXED_CST:
3307 /* Fixed constants are the same only if the same width of type. */
3308 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3309 return false;
3311 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3313 case STRING_CST:
3314 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3315 || int_size_in_bytes (TREE_TYPE (t1))
3316 != int_size_in_bytes (TREE_TYPE (t2)))
3317 return false;
3319 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3320 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3321 TREE_STRING_LENGTH (t1)));
3323 case COMPLEX_CST:
3324 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3325 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3327 case VECTOR_CST:
3329 if (VECTOR_CST_NPATTERNS (t1)
3330 != VECTOR_CST_NPATTERNS (t2))
3331 return false;
3333 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3334 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3335 return false;
3337 unsigned int count = vector_cst_encoded_nelts (t1);
3338 for (unsigned int i = 0; i < count; ++i)
3339 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3340 VECTOR_CST_ENCODED_ELT (t2, i)))
3341 return false;
3343 return true;
3346 case CONSTRUCTOR:
3348 vec<constructor_elt, va_gc> *v1, *v2;
3349 unsigned HOST_WIDE_INT idx;
3351 typecode = TREE_CODE (TREE_TYPE (t1));
3352 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3353 return false;
3355 if (typecode == ARRAY_TYPE)
3357 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3358 /* For arrays, check that mode, size and storage order match. */
3359 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3360 || size_1 == -1
3361 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3362 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3363 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3364 return false;
3366 else
3368 /* For record and union constructors, require exact type
3369 equality. */
3370 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3371 return false;
3374 v1 = CONSTRUCTOR_ELTS (t1);
3375 v2 = CONSTRUCTOR_ELTS (t2);
3376 if (vec_safe_length (v1) != vec_safe_length (v2))
3377 return false;
3379 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3381 constructor_elt *c1 = &(*v1)[idx];
3382 constructor_elt *c2 = &(*v2)[idx];
3384 /* Check that each value is the same... */
3385 if (!compare_constant (c1->value, c2->value))
3386 return false;
3387 /* ... and that they apply to the same fields! */
3388 if (typecode == ARRAY_TYPE)
3390 if (!compare_constant (c1->index, c2->index))
3391 return false;
3393 else
3395 if (c1->index != c2->index)
3396 return false;
3400 return true;
3403 case ADDR_EXPR:
3404 case FDESC_EXPR:
3406 class addr_const value1, value2;
3407 enum rtx_code code;
3408 bool ret;
3410 decode_addr_const (t1, &value1);
3411 decode_addr_const (t2, &value2);
3413 if (maybe_ne (value1.offset, value2.offset))
3414 return false;
3416 code = GET_CODE (value1.base);
3417 if (code != GET_CODE (value2.base))
3418 return false;
3420 switch (code)
3422 case SYMBOL_REF:
3423 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3424 break;
3426 case LABEL_REF:
3427 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3428 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3429 break;
3431 default:
3432 gcc_unreachable ();
3434 return ret;
3437 case PLUS_EXPR:
3438 case POINTER_PLUS_EXPR:
3439 case MINUS_EXPR:
3440 case RANGE_EXPR:
3441 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3442 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3444 CASE_CONVERT:
3445 case VIEW_CONVERT_EXPR:
3446 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3448 default:
3449 return false;
3453 /* Return the section into which constant EXP should be placed. */
3455 static section *
3456 get_constant_section (tree exp, unsigned int align)
3458 return targetm.asm_out.select_section (exp,
3459 compute_reloc_for_constant (exp),
3460 align);
3463 /* Return the size of constant EXP in bytes. */
3465 static HOST_WIDE_INT
3466 get_constant_size (tree exp)
3468 HOST_WIDE_INT size;
3470 size = int_size_in_bytes (TREE_TYPE (exp));
3471 gcc_checking_assert (size >= 0);
3472 gcc_checking_assert (TREE_CODE (exp) != STRING_CST
3473 || size >= TREE_STRING_LENGTH (exp));
3474 return size;
3477 /* Subroutine of output_constant_def:
3478 No constant equal to EXP is known to have been output.
3479 Make a constant descriptor to enter EXP in the hash table.
3480 Assign the label number and construct RTL to refer to the
3481 constant's location in memory.
3482 Caller is responsible for updating the hash table. */
3484 static struct constant_descriptor_tree *
3485 build_constant_desc (tree exp)
3487 struct constant_descriptor_tree *desc;
3488 rtx symbol, rtl;
3489 char label[256];
3490 int labelno;
3491 tree decl;
3493 desc = ggc_alloc<constant_descriptor_tree> ();
3494 desc->value = exp;
3496 /* Create a string containing the label name, in LABEL. */
3497 labelno = const_labelno++;
3498 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3500 /* Construct the VAR_DECL associated with the constant. */
3501 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3502 TREE_TYPE (exp));
3503 DECL_ARTIFICIAL (decl) = 1;
3504 DECL_IGNORED_P (decl) = 1;
3505 TREE_READONLY (decl) = 1;
3506 TREE_STATIC (decl) = 1;
3507 TREE_ADDRESSABLE (decl) = 1;
3508 /* We don't set the RTL yet as this would cause varpool to assume that the
3509 variable is referenced. Moreover, it would just be dropped in LTO mode.
3510 Instead we set the flag that will be recognized in make_decl_rtl. */
3511 DECL_IN_CONSTANT_POOL (decl) = 1;
3512 DECL_INITIAL (decl) = desc->value;
3513 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3514 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3515 if (TREE_CODE (exp) == STRING_CST)
3516 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3517 else
3519 align_variable (decl, 0);
3520 if (DECL_ALIGN (decl) < GET_MODE_ALIGNMENT (DECL_MODE (decl))
3521 && ((optab_handler (movmisalign_optab, DECL_MODE (decl))
3522 != CODE_FOR_nothing)
3523 || targetm.slow_unaligned_access (DECL_MODE (decl),
3524 DECL_ALIGN (decl))))
3525 SET_DECL_ALIGN (decl, GET_MODE_ALIGNMENT (DECL_MODE (decl)));
3528 /* Now construct the SYMBOL_REF and the MEM. */
3529 if (use_object_blocks_p ())
3531 int align = (TREE_CODE (decl) == CONST_DECL
3532 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3533 ? DECL_ALIGN (decl)
3534 : symtab_node::get (decl)->definition_alignment ());
3535 section *sect = get_constant_section (exp, align);
3536 symbol = create_block_symbol (ggc_strdup (label),
3537 get_block_for_section (sect), -1);
3539 else
3540 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3541 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3542 SET_SYMBOL_REF_DECL (symbol, decl);
3543 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3545 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3546 set_mem_alias_set (rtl, 0);
3548 /* Putting EXP into the literal pool might have imposed a different
3549 alignment which should be visible in the RTX as well. */
3550 set_mem_align (rtl, DECL_ALIGN (decl));
3552 /* We cannot share RTX'es in pool entries.
3553 Mark this piece of RTL as required for unsharing. */
3554 RTX_FLAG (rtl, used) = 1;
3556 /* Set flags or add text to the name to record information, such as
3557 that it is a local symbol. If the name is changed, the macro
3558 ASM_OUTPUT_LABELREF will have to know how to strip this
3559 information. This call might invalidate our local variable
3560 SYMBOL; we can't use it afterward. */
3561 targetm.encode_section_info (exp, rtl, true);
3563 desc->rtl = rtl;
3565 return desc;
3568 /* Subroutine of output_constant_def and tree_output_constant_def:
3569 Add a constant to the hash table that tracks which constants
3570 already have labels. */
3572 static constant_descriptor_tree *
3573 add_constant_to_table (tree exp, int defer)
3575 /* The hash table methods may call output_constant_def for addressed
3576 constants, so handle them first. */
3577 output_addressed_constants (exp, defer);
3579 /* Sanity check to catch recursive insertion. */
3580 static bool inserting;
3581 gcc_assert (!inserting);
3582 inserting = true;
3584 /* Look up EXP in the table of constant descriptors. If we didn't
3585 find it, create a new one. */
3586 struct constant_descriptor_tree key;
3587 key.value = exp;
3588 key.hash = const_hash_1 (exp);
3589 constant_descriptor_tree **loc
3590 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3592 inserting = false;
3594 struct constant_descriptor_tree *desc = *loc;
3595 if (!desc)
3597 desc = build_constant_desc (exp);
3598 desc->hash = key.hash;
3599 *loc = desc;
3602 return desc;
3605 /* Return an rtx representing a reference to constant data in memory
3606 for the constant expression EXP.
3608 If assembler code for such a constant has already been output,
3609 return an rtx to refer to it.
3610 Otherwise, output such a constant in memory
3611 and generate an rtx for it.
3613 If DEFER is nonzero, this constant can be deferred and output only
3614 if referenced in the function after all optimizations.
3616 `const_desc_table' records which constants already have label strings. */
3619 output_constant_def (tree exp, int defer)
3621 struct constant_descriptor_tree *desc = add_constant_to_table (exp, defer);
3622 maybe_output_constant_def_contents (desc, defer);
3623 return desc->rtl;
3626 /* Subroutine of output_constant_def: Decide whether or not we need to
3627 output the constant DESC now, and if so, do it. */
3628 static void
3629 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3630 int defer)
3632 rtx symbol = XEXP (desc->rtl, 0);
3633 tree exp = desc->value;
3635 if (flag_syntax_only)
3636 return;
3638 if (TREE_ASM_WRITTEN (exp))
3639 /* Already output; don't do it again. */
3640 return;
3642 /* We can always defer constants as long as the context allows
3643 doing so. */
3644 if (defer)
3646 /* Increment n_deferred_constants if it exists. It needs to be at
3647 least as large as the number of constants actually referred to
3648 by the function. If it's too small we'll stop looking too early
3649 and fail to emit constants; if it's too large we'll only look
3650 through the entire function when we could have stopped earlier. */
3651 if (cfun)
3652 n_deferred_constants++;
3653 return;
3656 output_constant_def_contents (symbol);
3659 /* Subroutine of output_constant_def_contents. Output the definition
3660 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3661 constant's alignment in bits. */
3663 static void
3664 assemble_constant_contents (tree exp, const char *label, unsigned int align,
3665 bool merge_strings)
3667 HOST_WIDE_INT size;
3669 size = get_constant_size (exp);
3671 /* Do any machine/system dependent processing of the constant. */
3672 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3674 /* Output the value of EXP. */
3675 output_constant (exp, size, align, false, merge_strings);
3677 targetm.asm_out.decl_end ();
3680 /* We must output the constant data referred to by SYMBOL; do so. */
3682 static void
3683 output_constant_def_contents (rtx symbol)
3685 tree decl = SYMBOL_REF_DECL (symbol);
3686 tree exp = DECL_INITIAL (decl);
3687 bool asan_protected = false;
3689 /* Make sure any other constants whose addresses appear in EXP
3690 are assigned label numbers. */
3691 output_addressed_constants (exp, 0);
3693 /* We are no longer deferring this constant. */
3694 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3696 if ((flag_sanitize & SANITIZE_ADDRESS)
3697 && TREE_CODE (exp) == STRING_CST
3698 && asan_protect_global (exp))
3700 asan_protected = true;
3701 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3702 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3705 /* If the constant is part of an object block, make sure that the
3706 decl has been positioned within its block, but do not write out
3707 its definition yet. output_object_blocks will do that later. */
3708 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3709 place_block_symbol (symbol);
3710 else
3712 int align = (TREE_CODE (decl) == CONST_DECL
3713 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3714 ? DECL_ALIGN (decl)
3715 : symtab_node::get (decl)->definition_alignment ());
3716 section *sect = get_constant_section (exp, align);
3717 switch_to_section (sect);
3718 if (align > BITS_PER_UNIT)
3719 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3720 assemble_constant_contents (exp, XSTR (symbol, 0), align,
3721 (sect->common.flags & SECTION_MERGE)
3722 && (sect->common.flags & SECTION_STRINGS));
3723 if (asan_protected)
3725 HOST_WIDE_INT size = get_constant_size (exp);
3726 assemble_zeros (asan_red_zone_size (size));
3731 /* Look up EXP in the table of constant descriptors. Return the rtl
3732 if it has been emitted, else null. */
3735 lookup_constant_def (tree exp)
3737 struct constant_descriptor_tree key;
3739 key.value = exp;
3740 key.hash = const_hash_1 (exp);
3741 constant_descriptor_tree *desc
3742 = const_desc_htab->find_with_hash (&key, key.hash);
3744 return (desc ? desc->rtl : NULL_RTX);
3747 /* Return a tree representing a reference to constant data in memory
3748 for the constant expression EXP.
3750 This is the counterpart of output_constant_def at the Tree level. */
3752 tree
3753 tree_output_constant_def (tree exp)
3755 struct constant_descriptor_tree *desc = add_constant_to_table (exp, 1);
3756 tree decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3757 varpool_node::finalize_decl (decl);
3758 return decl;
3761 class GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3762 public:
3763 class constant_descriptor_rtx *next;
3764 rtx mem;
3765 rtx sym;
3766 rtx constant;
3767 HOST_WIDE_INT offset;
3768 hashval_t hash;
3769 fixed_size_mode mode;
3770 unsigned int align;
3771 int labelno;
3772 int mark;
3775 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3777 static hashval_t hash (constant_descriptor_rtx *);
3778 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3781 /* Used in the hash tables to avoid outputting the same constant
3782 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3783 are output once per function, not once per file. */
3784 /* ??? Only a few targets need per-function constant pools. Most
3785 can use one per-file pool. Should add a targetm bit to tell the
3786 difference. */
3788 struct GTY(()) rtx_constant_pool {
3789 /* Pointers to first and last constant in pool, as ordered by offset. */
3790 class constant_descriptor_rtx *first;
3791 class constant_descriptor_rtx *last;
3793 /* Hash facility for making memory-constants from constant rtl-expressions.
3794 It is used on RISC machines where immediate integer arguments and
3795 constant addresses are restricted so that such constants must be stored
3796 in memory. */
3797 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3799 /* Current offset in constant pool (does not include any
3800 machine-specific header). */
3801 HOST_WIDE_INT offset;
3804 /* Hash and compare functions for const_rtx_htab. */
3806 hashval_t
3807 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3809 return desc->hash;
3812 bool
3813 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3814 constant_descriptor_rtx *y)
3816 if (x->mode != y->mode)
3817 return false;
3818 return rtx_equal_p (x->constant, y->constant);
3821 /* Hash one component of a constant. */
3823 static hashval_t
3824 const_rtx_hash_1 (const_rtx x)
3826 unsigned HOST_WIDE_INT hwi;
3827 machine_mode mode;
3828 enum rtx_code code;
3829 hashval_t h;
3830 int i;
3832 code = GET_CODE (x);
3833 mode = GET_MODE (x);
3834 h = (hashval_t) code * 1048573 + mode;
3836 switch (code)
3838 case CONST_INT:
3839 hwi = INTVAL (x);
3841 fold_hwi:
3843 int shift = sizeof (hashval_t) * CHAR_BIT;
3844 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3846 h ^= (hashval_t) hwi;
3847 for (i = 1; i < n; ++i)
3849 hwi >>= shift;
3850 h ^= (hashval_t) hwi;
3853 break;
3855 case CONST_WIDE_INT:
3856 hwi = 0;
3858 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3859 hwi ^= CONST_WIDE_INT_ELT (x, i);
3860 goto fold_hwi;
3863 case CONST_DOUBLE:
3864 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3866 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3867 goto fold_hwi;
3869 else
3870 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3871 break;
3873 case CONST_FIXED:
3874 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3875 break;
3877 case SYMBOL_REF:
3878 h ^= htab_hash_string (XSTR (x, 0));
3879 break;
3881 case LABEL_REF:
3882 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3883 break;
3885 case UNSPEC:
3886 case UNSPEC_VOLATILE:
3887 h = h * 251 + XINT (x, 1);
3888 break;
3890 default:
3891 break;
3894 return h;
3897 /* Compute a hash value for X, which should be a constant. */
3899 static hashval_t
3900 const_rtx_hash (rtx x)
3902 hashval_t h = 0;
3903 subrtx_iterator::array_type array;
3904 FOR_EACH_SUBRTX (iter, array, x, ALL)
3905 h = h * 509 + const_rtx_hash_1 (*iter);
3906 return h;
3910 /* Create and return a new rtx constant pool. */
3912 static struct rtx_constant_pool *
3913 create_constant_pool (void)
3915 struct rtx_constant_pool *pool;
3917 pool = ggc_alloc<rtx_constant_pool> ();
3918 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3919 pool->first = NULL;
3920 pool->last = NULL;
3921 pool->offset = 0;
3922 return pool;
3925 /* Initialize constant pool hashing for a new function. */
3927 void
3928 init_varasm_status (void)
3930 crtl->varasm.pool = create_constant_pool ();
3931 crtl->varasm.deferred_constants = 0;
3934 /* Given a MINUS expression, simplify it if both sides
3935 include the same symbol. */
3938 simplify_subtraction (rtx x)
3940 rtx r = simplify_rtx (x);
3941 return r ? r : x;
3944 /* Given a constant rtx X, make (or find) a memory constant for its value
3945 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
3946 of X. */
3949 force_const_mem (machine_mode in_mode, rtx x)
3951 class constant_descriptor_rtx *desc, tmp;
3952 struct rtx_constant_pool *pool;
3953 char label[256];
3954 rtx def, symbol;
3955 hashval_t hash;
3956 unsigned int align;
3957 constant_descriptor_rtx **slot;
3958 fixed_size_mode mode;
3960 /* We can't force variable-sized objects to memory. */
3961 if (!is_a <fixed_size_mode> (in_mode, &mode))
3962 return NULL_RTX;
3964 /* If we're not allowed to drop X into the constant pool, don't. */
3965 if (targetm.cannot_force_const_mem (mode, x))
3966 return NULL_RTX;
3968 /* Record that this function has used a constant pool entry. */
3969 crtl->uses_const_pool = 1;
3971 /* Decide which pool to use. */
3972 pool = (targetm.use_blocks_for_constant_p (mode, x)
3973 ? shared_constant_pool
3974 : crtl->varasm.pool);
3976 /* Lookup the value in the hashtable. */
3977 tmp.constant = x;
3978 tmp.mode = mode;
3979 hash = const_rtx_hash (x);
3980 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3981 desc = *slot;
3983 /* If the constant was already present, return its memory. */
3984 if (desc)
3985 return copy_rtx (desc->mem);
3987 /* Otherwise, create a new descriptor. */
3988 desc = ggc_alloc<constant_descriptor_rtx> ();
3989 *slot = desc;
3991 /* Align the location counter as required by EXP's data type. */
3992 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3993 align = targetm.static_rtx_alignment (align_mode);
3995 pool->offset += (align / BITS_PER_UNIT) - 1;
3996 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3998 desc->next = NULL;
3999 desc->constant = copy_rtx (tmp.constant);
4000 desc->offset = pool->offset;
4001 desc->hash = hash;
4002 desc->mode = mode;
4003 desc->align = align;
4004 desc->labelno = const_labelno;
4005 desc->mark = 0;
4007 pool->offset += GET_MODE_SIZE (mode);
4008 if (pool->last)
4009 pool->last->next = desc;
4010 else
4011 pool->first = pool->last = desc;
4012 pool->last = desc;
4014 /* Create a string containing the label name, in LABEL. */
4015 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
4016 ++const_labelno;
4018 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
4019 the constants pool. */
4020 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
4022 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
4023 symbol = create_block_symbol (ggc_strdup (label),
4024 get_block_for_section (sect), -1);
4026 else
4027 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
4028 desc->sym = symbol;
4029 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
4030 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
4031 SET_SYMBOL_REF_CONSTANT (symbol, desc);
4033 /* Construct the MEM. */
4034 desc->mem = def = gen_const_mem (mode, symbol);
4035 set_mem_align (def, align);
4037 /* If we're dropping a label to the constant pool, make sure we
4038 don't delete it. */
4039 if (GET_CODE (x) == LABEL_REF)
4040 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
4042 return copy_rtx (def);
4045 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
4048 get_pool_constant (const_rtx addr)
4050 return SYMBOL_REF_CONSTANT (addr)->constant;
4053 /* Given a constant pool SYMBOL_REF, return the corresponding constant
4054 and whether it has been output or not. */
4057 get_pool_constant_mark (rtx addr, bool *pmarked)
4059 class constant_descriptor_rtx *desc;
4061 desc = SYMBOL_REF_CONSTANT (addr);
4062 *pmarked = (desc->mark != 0);
4063 return desc->constant;
4066 /* Similar, return the mode. */
4068 fixed_size_mode
4069 get_pool_mode (const_rtx addr)
4071 return SYMBOL_REF_CONSTANT (addr)->mode;
4074 /* Return TRUE if and only if the constant pool has no entries. Note
4075 that even entries we might end up choosing not to emit are counted
4076 here, so there is the potential for missed optimizations. */
4078 bool
4079 constant_pool_empty_p (void)
4081 return crtl->varasm.pool->first == NULL;
4084 /* Worker function for output_constant_pool_1. Emit assembly for X
4085 in MODE with known alignment ALIGN. */
4087 static void
4088 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
4090 switch (GET_MODE_CLASS (mode))
4092 case MODE_FLOAT:
4093 case MODE_DECIMAL_FLOAT:
4095 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
4096 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
4097 as_a <scalar_float_mode> (mode), align, false);
4098 break;
4101 case MODE_INT:
4102 case MODE_PARTIAL_INT:
4103 case MODE_FRACT:
4104 case MODE_UFRACT:
4105 case MODE_ACCUM:
4106 case MODE_UACCUM:
4107 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
4108 break;
4110 case MODE_VECTOR_BOOL:
4112 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4114 /* Pick the smallest integer mode that contains at least one
4115 whole element. Often this is byte_mode and contains more
4116 than one element. */
4117 unsigned int nelts = GET_MODE_NUNITS (mode);
4118 unsigned int elt_bits = GET_MODE_PRECISION (mode) / nelts;
4119 unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
4120 scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
4121 unsigned int mask = GET_MODE_MASK (GET_MODE_INNER (mode));
4123 /* We allow GET_MODE_PRECISION (mode) <= GET_MODE_BITSIZE (mode) but
4124 only properly handle cases where the difference is less than a
4125 byte. */
4126 gcc_assert (GET_MODE_BITSIZE (mode) - GET_MODE_PRECISION (mode) <
4127 BITS_PER_UNIT);
4129 /* Build the constant up one integer at a time. */
4130 unsigned int elts_per_int = int_bits / elt_bits;
4131 for (unsigned int i = 0; i < nelts; i += elts_per_int)
4133 unsigned HOST_WIDE_INT value = 0;
4134 unsigned int limit = MIN (nelts - i, elts_per_int);
4135 for (unsigned int j = 0; j < limit; ++j)
4137 auto elt = INTVAL (CONST_VECTOR_ELT (x, i + j));
4138 value |= (elt & mask) << (j * elt_bits);
4140 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
4141 i != 0 ? MIN (align, int_bits) : align);
4143 break;
4145 case MODE_VECTOR_FLOAT:
4146 case MODE_VECTOR_INT:
4147 case MODE_VECTOR_FRACT:
4148 case MODE_VECTOR_UFRACT:
4149 case MODE_VECTOR_ACCUM:
4150 case MODE_VECTOR_UACCUM:
4152 int i, units;
4153 scalar_mode submode = GET_MODE_INNER (mode);
4154 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4156 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4157 units = GET_MODE_NUNITS (mode);
4159 for (i = 0; i < units; i++)
4161 rtx elt = CONST_VECTOR_ELT (x, i);
4162 output_constant_pool_2 (submode, elt, i ? subalign : align);
4165 break;
4167 default:
4168 gcc_unreachable ();
4172 /* Worker function for output_constant_pool. Emit constant DESC,
4173 giving it ALIGN bits of alignment. */
4175 static void
4176 output_constant_pool_1 (class constant_descriptor_rtx *desc,
4177 unsigned int align)
4179 rtx x, tmp;
4181 x = desc->constant;
4183 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4184 whose CODE_LABEL has been deleted. This can occur if a jump table
4185 is eliminated by optimization. If so, write a constant of zero
4186 instead. Note that this can also happen by turning the
4187 CODE_LABEL into a NOTE. */
4188 /* ??? This seems completely and utterly wrong. Certainly it's
4189 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4190 functioning even with rtx_insn::deleted and friends. */
4192 tmp = x;
4193 switch (GET_CODE (tmp))
4195 case CONST:
4196 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4197 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4198 break;
4199 tmp = XEXP (XEXP (tmp, 0), 0);
4200 /* FALLTHRU */
4202 case LABEL_REF:
4204 rtx_insn *insn = label_ref_label (tmp);
4205 gcc_assert (!insn->deleted ());
4206 gcc_assert (!NOTE_P (insn)
4207 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4208 break;
4211 default:
4212 break;
4215 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4216 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4217 align, desc->labelno, done);
4218 #endif
4220 assemble_align (align);
4222 /* Output the label. */
4223 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4225 /* Output the data.
4226 Pass actual alignment value while emitting string constant to asm code
4227 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4228 assuming that the data is already aligned which prevents the generation
4229 of fix-up table entries. */
4230 output_constant_pool_2 (desc->mode, x, desc->align);
4232 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4233 sections have proper size. */
4234 if (align > GET_MODE_BITSIZE (desc->mode)
4235 && in_section
4236 && (in_section->common.flags & SECTION_MERGE))
4237 assemble_align (align);
4239 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4240 done:
4241 #endif
4242 return;
4245 /* Recompute the offsets of entries in POOL, and the overall size of
4246 POOL. Do this after calling mark_constant_pool to ensure that we
4247 are computing the offset values for the pool which we will actually
4248 emit. */
4250 static void
4251 recompute_pool_offsets (struct rtx_constant_pool *pool)
4253 class constant_descriptor_rtx *desc;
4254 pool->offset = 0;
4256 for (desc = pool->first; desc ; desc = desc->next)
4257 if (desc->mark)
4259 /* Recalculate offset. */
4260 unsigned int align = desc->align;
4261 pool->offset += (align / BITS_PER_UNIT) - 1;
4262 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4263 desc->offset = pool->offset;
4264 pool->offset += GET_MODE_SIZE (desc->mode);
4268 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4269 Emit referenced deferred strings. */
4271 static void
4272 mark_constants_in_pattern (rtx insn)
4274 subrtx_iterator::array_type array;
4275 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4277 const_rtx x = *iter;
4278 if (GET_CODE (x) == SYMBOL_REF)
4280 if (CONSTANT_POOL_ADDRESS_P (x))
4282 class constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4283 if (desc->mark == 0)
4285 desc->mark = 1;
4286 iter.substitute (desc->constant);
4289 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4291 tree decl = SYMBOL_REF_DECL (x);
4292 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4294 n_deferred_constants--;
4295 output_constant_def_contents (CONST_CAST_RTX (x));
4302 /* Look through appropriate parts of INSN, marking all entries in the
4303 constant pool which are actually being used. Entries that are only
4304 referenced by other constants are also marked as used. Emit
4305 deferred strings that are used. */
4307 static void
4308 mark_constants (rtx_insn *insn)
4310 if (!INSN_P (insn))
4311 return;
4313 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4314 insns, not any notes that may be attached. We don't want to mark
4315 a constant just because it happens to appear in a REG_EQUIV note. */
4316 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4318 int i, n = seq->len ();
4319 for (i = 0; i < n; ++i)
4321 rtx subinsn = seq->element (i);
4322 if (INSN_P (subinsn))
4323 mark_constants_in_pattern (subinsn);
4326 else
4327 mark_constants_in_pattern (insn);
4330 /* Look through the instructions for this function, and mark all the
4331 entries in POOL which are actually being used. Emit deferred constants
4332 which have indeed been used. */
4334 static void
4335 mark_constant_pool (void)
4337 rtx_insn *insn;
4339 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4340 return;
4342 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4343 mark_constants (insn);
4346 /* Write all the constants in POOL. */
4348 static void
4349 output_constant_pool_contents (struct rtx_constant_pool *pool)
4351 class constant_descriptor_rtx *desc;
4353 for (desc = pool->first; desc ; desc = desc->next)
4354 if (desc->mark < 0)
4356 #ifdef ASM_OUTPUT_DEF
4357 gcc_checking_assert (TARGET_SUPPORTS_ALIASES);
4359 const char *name = XSTR (desc->sym, 0);
4360 char label[256];
4361 char buffer[256 + 32];
4362 const char *p;
4364 ASM_GENERATE_INTERNAL_LABEL (label, "LC", ~desc->mark);
4365 p = label;
4366 if (desc->offset)
4368 sprintf (buffer, "%s+%ld", p, (long) (desc->offset));
4369 p = buffer;
4371 ASM_OUTPUT_DEF (asm_out_file, name, p);
4372 #else
4373 gcc_unreachable ();
4374 #endif
4376 else if (desc->mark)
4378 /* If the constant is part of an object_block, make sure that
4379 the constant has been positioned within its block, but do not
4380 write out its definition yet. output_object_blocks will do
4381 that later. */
4382 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4383 && SYMBOL_REF_BLOCK (desc->sym))
4384 place_block_symbol (desc->sym);
4385 else
4387 switch_to_section (targetm.asm_out.select_rtx_section
4388 (desc->mode, desc->constant, desc->align));
4389 output_constant_pool_1 (desc, desc->align);
4394 struct constant_descriptor_rtx_data {
4395 constant_descriptor_rtx *desc;
4396 target_unit *bytes;
4397 unsigned short size;
4398 unsigned short offset;
4399 unsigned int hash;
4402 /* qsort callback to sort constant_descriptor_rtx_data * vector by
4403 decreasing size. */
4405 static int
4406 constant_descriptor_rtx_data_cmp (const void *p1, const void *p2)
4408 constant_descriptor_rtx_data *const data1
4409 = *(constant_descriptor_rtx_data * const *) p1;
4410 constant_descriptor_rtx_data *const data2
4411 = *(constant_descriptor_rtx_data * const *) p2;
4412 if (data1->size > data2->size)
4413 return -1;
4414 if (data1->size < data2->size)
4415 return 1;
4416 if (data1->hash < data2->hash)
4417 return -1;
4418 gcc_assert (data1->hash > data2->hash);
4419 return 1;
4422 struct const_rtx_data_hasher : nofree_ptr_hash<constant_descriptor_rtx_data>
4424 static hashval_t hash (constant_descriptor_rtx_data *);
4425 static bool equal (constant_descriptor_rtx_data *,
4426 constant_descriptor_rtx_data *);
4429 /* Hash and compare functions for const_rtx_data_htab. */
4431 hashval_t
4432 const_rtx_data_hasher::hash (constant_descriptor_rtx_data *data)
4434 return data->hash;
4437 bool
4438 const_rtx_data_hasher::equal (constant_descriptor_rtx_data *x,
4439 constant_descriptor_rtx_data *y)
4441 if (x->hash != y->hash || x->size != y->size)
4442 return false;
4443 unsigned int align1 = x->desc->align;
4444 unsigned int align2 = y->desc->align;
4445 unsigned int offset1 = (x->offset * BITS_PER_UNIT) & (align1 - 1);
4446 unsigned int offset2 = (y->offset * BITS_PER_UNIT) & (align2 - 1);
4447 if (offset1)
4448 align1 = least_bit_hwi (offset1);
4449 if (offset2)
4450 align2 = least_bit_hwi (offset2);
4451 if (align2 > align1)
4452 return false;
4453 if (memcmp (x->bytes, y->bytes, x->size * sizeof (target_unit)) != 0)
4454 return false;
4455 return true;
4458 /* Attempt to optimize constant pool POOL. If it contains both CONST_VECTOR
4459 constants and scalar constants with the values of CONST_VECTOR elements,
4460 try to alias the scalar constants with the CONST_VECTOR elements. */
4462 static void
4463 optimize_constant_pool (struct rtx_constant_pool *pool)
4465 auto_vec<target_unit, 128> buffer;
4466 auto_vec<constant_descriptor_rtx_data *, 128> vec;
4467 object_allocator<constant_descriptor_rtx_data>
4468 data_pool ("constant_descriptor_rtx_data_pool");
4469 int idx = 0;
4470 size_t size = 0;
4471 for (constant_descriptor_rtx *desc = pool->first; desc; desc = desc->next)
4472 if (desc->mark > 0
4473 && ! (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4474 && SYMBOL_REF_BLOCK (desc->sym)))
4476 buffer.truncate (0);
4477 buffer.reserve (GET_MODE_SIZE (desc->mode));
4478 if (native_encode_rtx (desc->mode, desc->constant, buffer, 0,
4479 GET_MODE_SIZE (desc->mode)))
4481 constant_descriptor_rtx_data *data = data_pool.allocate ();
4482 data->desc = desc;
4483 data->bytes = NULL;
4484 data->size = GET_MODE_SIZE (desc->mode);
4485 data->offset = 0;
4486 data->hash = idx++;
4487 size += data->size;
4488 vec.safe_push (data);
4491 if (idx)
4493 vec.qsort (constant_descriptor_rtx_data_cmp);
4494 unsigned min_size = vec.last ()->size;
4495 target_unit *bytes = XNEWVEC (target_unit, size);
4496 unsigned int i;
4497 constant_descriptor_rtx_data *data;
4498 hash_table<const_rtx_data_hasher> * htab
4499 = new hash_table<const_rtx_data_hasher> (31);
4500 size = 0;
4501 FOR_EACH_VEC_ELT (vec, i, data)
4503 buffer.truncate (0);
4504 native_encode_rtx (data->desc->mode, data->desc->constant,
4505 buffer, 0, data->size);
4506 memcpy (bytes + size, buffer.address (), data->size);
4507 data->bytes = bytes + size;
4508 data->hash = iterative_hash (data->bytes,
4509 data->size * sizeof (target_unit), 0);
4510 size += data->size;
4511 constant_descriptor_rtx_data **slot
4512 = htab->find_slot_with_hash (data, data->hash, INSERT);
4513 if (*slot)
4515 data->desc->mark = ~(*slot)->desc->labelno;
4516 data->desc->offset = (*slot)->offset;
4518 else
4520 unsigned int sz = 1 << floor_log2 (data->size);
4522 *slot = data;
4523 for (sz >>= 1; sz >= min_size; sz >>= 1)
4524 for (unsigned off = 0; off + sz <= data->size; off += sz)
4526 constant_descriptor_rtx_data tmp;
4527 tmp.desc = data->desc;
4528 tmp.bytes = data->bytes + off;
4529 tmp.size = sz;
4530 tmp.offset = off;
4531 tmp.hash = iterative_hash (tmp.bytes,
4532 sz * sizeof (target_unit), 0);
4533 slot = htab->find_slot_with_hash (&tmp, tmp.hash, INSERT);
4534 if (*slot == NULL)
4536 *slot = data_pool.allocate ();
4537 **slot = tmp;
4542 delete htab;
4543 XDELETE (bytes);
4545 data_pool.release ();
4548 /* Mark all constants that are used in the current function, then write
4549 out the function's private constant pool. */
4551 static void
4552 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4553 tree fndecl ATTRIBUTE_UNUSED)
4555 struct rtx_constant_pool *pool = crtl->varasm.pool;
4557 /* It is possible for gcc to call force_const_mem and then to later
4558 discard the instructions which refer to the constant. In such a
4559 case we do not need to output the constant. */
4560 mark_constant_pool ();
4562 /* Having marked the constant pool entries we'll actually emit, we
4563 now need to rebuild the offset information, which may have become
4564 stale. */
4565 recompute_pool_offsets (pool);
4567 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4568 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4569 #endif
4571 output_constant_pool_contents (pool);
4573 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4574 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4575 #endif
4578 /* Write the contents of the shared constant pool. */
4580 void
4581 output_shared_constant_pool (void)
4583 if (optimize
4584 && TARGET_SUPPORTS_ALIASES)
4585 optimize_constant_pool (shared_constant_pool);
4587 output_constant_pool_contents (shared_constant_pool);
4590 /* Determine what kind of relocations EXP may need. */
4593 compute_reloc_for_constant (tree exp)
4595 int reloc = 0, reloc2;
4596 tree tem;
4598 switch (TREE_CODE (exp))
4600 case ADDR_EXPR:
4601 case FDESC_EXPR:
4602 /* Go inside any operations that get_inner_reference can handle and see
4603 if what's inside is a constant: no need to do anything here for
4604 addresses of variables or functions. */
4605 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4606 tem = TREE_OPERAND (tem, 0))
4609 if (TREE_CODE (tem) == MEM_REF
4610 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4612 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4613 break;
4616 if (!targetm.binds_local_p (tem))
4617 reloc |= 2;
4618 else
4619 reloc |= 1;
4620 break;
4622 case PLUS_EXPR:
4623 case POINTER_PLUS_EXPR:
4624 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4625 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4626 break;
4628 case MINUS_EXPR:
4629 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4630 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4631 /* The difference of two local labels is computable at link time. */
4632 if (reloc == 1 && reloc2 == 1)
4633 reloc = 0;
4634 else
4635 reloc |= reloc2;
4636 break;
4638 CASE_CONVERT:
4639 case VIEW_CONVERT_EXPR:
4640 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4641 break;
4643 case CONSTRUCTOR:
4645 unsigned HOST_WIDE_INT idx;
4646 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4647 if (tem != 0)
4648 reloc |= compute_reloc_for_constant (tem);
4650 break;
4652 default:
4653 break;
4655 return reloc;
4658 /* Find all the constants whose addresses are referenced inside of EXP,
4659 and make sure assembler code with a label has been output for each one.
4660 Indicate whether an ADDR_EXPR has been encountered. */
4662 static void
4663 output_addressed_constants (tree exp, int defer)
4665 tree tem;
4667 switch (TREE_CODE (exp))
4669 case ADDR_EXPR:
4670 case FDESC_EXPR:
4671 /* Go inside any operations that get_inner_reference can handle and see
4672 if what's inside is a constant: no need to do anything here for
4673 addresses of variables or functions. */
4674 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4675 tem = TREE_OPERAND (tem, 0))
4678 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4679 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4680 tem = DECL_INITIAL (tem);
4682 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4683 output_constant_def (tem, defer);
4685 if (TREE_CODE (tem) == MEM_REF)
4686 output_addressed_constants (TREE_OPERAND (tem, 0), defer);
4687 break;
4689 case PLUS_EXPR:
4690 case POINTER_PLUS_EXPR:
4691 case MINUS_EXPR:
4692 output_addressed_constants (TREE_OPERAND (exp, 1), defer);
4693 gcc_fallthrough ();
4695 CASE_CONVERT:
4696 case VIEW_CONVERT_EXPR:
4697 output_addressed_constants (TREE_OPERAND (exp, 0), defer);
4698 break;
4700 case CONSTRUCTOR:
4702 unsigned HOST_WIDE_INT idx;
4703 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4704 if (tem != 0)
4705 output_addressed_constants (tem, defer);
4707 break;
4709 default:
4710 break;
4714 /* Whether a constructor CTOR is a valid static constant initializer if all
4715 its elements are. This used to be internal to initializer_constant_valid_p
4716 and has been exposed to let other functions like categorize_ctor_elements
4717 evaluate the property while walking a constructor for other purposes. */
4719 bool
4720 constructor_static_from_elts_p (const_tree ctor)
4722 return (TREE_CONSTANT (ctor)
4723 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4724 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4725 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4728 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4729 tree *cache);
4731 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4732 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4733 which are valid when ENDTYPE is an integer of any size; in
4734 particular, this does not accept a pointer minus a constant. This
4735 returns null_pointer_node if the VALUE is an absolute constant
4736 which can be used to initialize a static variable. Otherwise it
4737 returns NULL. */
4739 static tree
4740 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4742 tree op0, op1;
4744 if (!INTEGRAL_TYPE_P (endtype))
4745 return NULL_TREE;
4747 op0 = TREE_OPERAND (value, 0);
4748 op1 = TREE_OPERAND (value, 1);
4750 /* Like STRIP_NOPS except allow the operand mode to widen. This
4751 works around a feature of fold that simplifies (int)(p1 - p2) to
4752 ((int)p1 - (int)p2) under the theory that the narrower operation
4753 is cheaper. */
4755 while (CONVERT_EXPR_P (op0)
4756 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4758 tree inner = TREE_OPERAND (op0, 0);
4759 if (inner == error_mark_node
4760 || ! INTEGRAL_TYPE_P (TREE_TYPE (op0))
4761 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (op0)))
4762 || ! INTEGRAL_TYPE_P (TREE_TYPE (inner))
4763 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4764 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4765 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4766 break;
4767 op0 = inner;
4770 while (CONVERT_EXPR_P (op1)
4771 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4773 tree inner = TREE_OPERAND (op1, 0);
4774 if (inner == error_mark_node
4775 || ! INTEGRAL_TYPE_P (TREE_TYPE (op1))
4776 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (op1)))
4777 || ! INTEGRAL_TYPE_P (TREE_TYPE (inner))
4778 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4779 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4780 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4781 break;
4782 op1 = inner;
4785 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4786 if (!op0)
4787 return NULL_TREE;
4789 op1 = initializer_constant_valid_p_1 (op1, endtype,
4790 cache ? cache + 2 : NULL);
4791 /* Both initializers must be known. */
4792 if (op1)
4794 if (op0 == op1
4795 && (op0 == null_pointer_node
4796 || TREE_CODE (value) == MINUS_EXPR))
4797 return null_pointer_node;
4799 /* Support differences between labels. */
4800 if (TREE_CODE (op0) == LABEL_DECL
4801 && TREE_CODE (op1) == LABEL_DECL)
4802 return null_pointer_node;
4804 if (TREE_CODE (op0) == STRING_CST
4805 && TREE_CODE (op1) == STRING_CST
4806 && operand_equal_p (op0, op1, 1))
4807 return null_pointer_node;
4810 return NULL_TREE;
4813 /* Helper function of initializer_constant_valid_p.
4814 Return nonzero if VALUE is a valid constant-valued expression
4815 for use in initializing a static variable; one that can be an
4816 element of a "constant" initializer.
4818 Return null_pointer_node if the value is absolute;
4819 if it is relocatable, return the variable that determines the relocation.
4820 We assume that VALUE has been folded as much as possible;
4821 therefore, we do not need to check for such things as
4822 arithmetic-combinations of integers.
4824 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4826 static tree
4827 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4829 tree ret;
4831 switch (TREE_CODE (value))
4833 case CONSTRUCTOR:
4834 if (constructor_static_from_elts_p (value))
4836 unsigned HOST_WIDE_INT idx;
4837 tree elt;
4838 bool absolute = true;
4840 if (cache && cache[0] == value)
4841 return cache[1];
4842 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4844 tree reloc;
4845 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4846 NULL);
4847 if (!reloc
4848 /* An absolute value is required with reverse SSO. */
4849 || (reloc != null_pointer_node
4850 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4851 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4853 if (cache)
4855 cache[0] = value;
4856 cache[1] = NULL_TREE;
4858 return NULL_TREE;
4860 if (reloc != null_pointer_node)
4861 absolute = false;
4863 /* For a non-absolute relocation, there is no single
4864 variable that can be "the variable that determines the
4865 relocation." */
4866 if (cache)
4868 cache[0] = value;
4869 cache[1] = absolute ? null_pointer_node : error_mark_node;
4871 return absolute ? null_pointer_node : error_mark_node;
4874 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4876 case INTEGER_CST:
4877 case VECTOR_CST:
4878 case REAL_CST:
4879 case FIXED_CST:
4880 case STRING_CST:
4881 case COMPLEX_CST:
4882 return null_pointer_node;
4884 case ADDR_EXPR:
4885 case FDESC_EXPR:
4887 tree op0 = staticp (TREE_OPERAND (value, 0));
4888 if (op0)
4890 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4891 to be a constant, this is old-skool offsetof-like nonsense. */
4892 if (TREE_CODE (op0) == INDIRECT_REF
4893 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4894 return null_pointer_node;
4895 /* Taking the address of a nested function involves a trampoline,
4896 unless we don't need or want one. */
4897 if (TREE_CODE (op0) == FUNCTION_DECL
4898 && DECL_STATIC_CHAIN (op0)
4899 && !TREE_NO_TRAMPOLINE (value))
4900 return NULL_TREE;
4901 /* "&{...}" requires a temporary to hold the constructed
4902 object. */
4903 if (TREE_CODE (op0) == CONSTRUCTOR)
4904 return NULL_TREE;
4906 return op0;
4909 case NON_LVALUE_EXPR:
4910 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4911 endtype, cache);
4913 case VIEW_CONVERT_EXPR:
4915 tree src = TREE_OPERAND (value, 0);
4916 tree src_type = TREE_TYPE (src);
4917 tree dest_type = TREE_TYPE (value);
4919 /* Allow view-conversions from aggregate to non-aggregate type only
4920 if the bit pattern is fully preserved afterwards; otherwise, the
4921 RTL expander won't be able to apply a subsequent transformation
4922 to the underlying constructor. */
4923 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4925 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4926 return initializer_constant_valid_p_1 (src, endtype, cache);
4927 else
4928 return NULL_TREE;
4931 /* Allow all other kinds of view-conversion. */
4932 return initializer_constant_valid_p_1 (src, endtype, cache);
4935 CASE_CONVERT:
4937 tree src = TREE_OPERAND (value, 0);
4938 tree src_type = TREE_TYPE (src);
4939 tree dest_type = TREE_TYPE (value);
4941 /* Allow conversions between pointer types and offset types. */
4942 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4943 || (TREE_CODE (dest_type) == OFFSET_TYPE
4944 && TREE_CODE (src_type) == OFFSET_TYPE))
4945 return initializer_constant_valid_p_1 (src, endtype, cache);
4947 /* Allow length-preserving conversions between integer types and
4948 floating-point types. */
4949 if (((INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4950 || (SCALAR_FLOAT_TYPE_P (dest_type)
4951 && SCALAR_FLOAT_TYPE_P (src_type)))
4952 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4953 return initializer_constant_valid_p_1 (src, endtype, cache);
4955 /* Allow conversions between other integer types only if
4956 explicit value. Don't allow sign-extension to a type larger
4957 than word and pointer, there aren't relocations that would
4958 allow to sign extend it to a wider type. */
4959 if (INTEGRAL_TYPE_P (dest_type)
4960 && INTEGRAL_TYPE_P (src_type)
4961 && (TYPE_UNSIGNED (src_type)
4962 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4963 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4964 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4966 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4967 if (inner == null_pointer_node)
4968 return null_pointer_node;
4969 break;
4972 /* Allow (int) &foo provided int is as wide as a pointer. */
4973 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4974 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4975 return initializer_constant_valid_p_1 (src, endtype, cache);
4977 /* Likewise conversions from int to pointers, but also allow
4978 conversions from 0. */
4979 if ((POINTER_TYPE_P (dest_type)
4980 || TREE_CODE (dest_type) == OFFSET_TYPE)
4981 && INTEGRAL_TYPE_P (src_type))
4983 if (TREE_CODE (src) == INTEGER_CST
4984 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4985 return null_pointer_node;
4986 if (integer_zerop (src))
4987 return null_pointer_node;
4988 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4989 return initializer_constant_valid_p_1 (src, endtype, cache);
4992 /* Allow conversions to struct or union types if the value
4993 inside is okay. */
4994 if (TREE_CODE (dest_type) == RECORD_TYPE
4995 || TREE_CODE (dest_type) == UNION_TYPE)
4996 return initializer_constant_valid_p_1 (src, endtype, cache);
4998 break;
5000 case POINTER_PLUS_EXPR:
5001 case PLUS_EXPR:
5002 /* Any valid floating-point constants will have been folded by now;
5003 with -frounding-math we hit this with addition of two constants. */
5004 if (TREE_CODE (endtype) == REAL_TYPE)
5005 return NULL_TREE;
5006 if (cache && cache[0] == value)
5007 return cache[1];
5008 if (! INTEGRAL_TYPE_P (endtype)
5009 || ! INTEGRAL_TYPE_P (TREE_TYPE (value))
5010 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
5012 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
5013 tree valid0
5014 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
5015 endtype, ncache);
5016 tree valid1
5017 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
5018 endtype, ncache + 2);
5019 /* If either term is absolute, use the other term's relocation. */
5020 if (valid0 == null_pointer_node)
5021 ret = valid1;
5022 else if (valid1 == null_pointer_node)
5023 ret = valid0;
5024 /* Support narrowing pointer differences. */
5025 else
5026 ret = narrowing_initializer_constant_valid_p (value, endtype,
5027 ncache);
5029 else
5030 /* Support narrowing pointer differences. */
5031 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
5032 if (cache)
5034 cache[0] = value;
5035 cache[1] = ret;
5037 return ret;
5039 case POINTER_DIFF_EXPR:
5040 case MINUS_EXPR:
5041 if (TREE_CODE (endtype) == REAL_TYPE)
5042 return NULL_TREE;
5043 if (cache && cache[0] == value)
5044 return cache[1];
5045 if (! INTEGRAL_TYPE_P (endtype)
5046 || ! INTEGRAL_TYPE_P (TREE_TYPE (value))
5047 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
5049 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
5050 tree valid0
5051 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
5052 endtype, ncache);
5053 tree valid1
5054 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
5055 endtype, ncache + 2);
5056 /* Win if second argument is absolute. */
5057 if (valid1 == null_pointer_node)
5058 ret = valid0;
5059 /* Win if both arguments have the same relocation.
5060 Then the value is absolute. */
5061 else if (valid0 == valid1 && valid0 != 0)
5062 ret = null_pointer_node;
5063 /* Since GCC guarantees that string constants are unique in the
5064 generated code, a subtraction between two copies of the same
5065 constant string is absolute. */
5066 else if (valid0 && TREE_CODE (valid0) == STRING_CST
5067 && valid1 && TREE_CODE (valid1) == STRING_CST
5068 && operand_equal_p (valid0, valid1, 1))
5069 ret = null_pointer_node;
5070 /* Support narrowing differences. */
5071 else
5072 ret = narrowing_initializer_constant_valid_p (value, endtype,
5073 ncache);
5075 else
5076 /* Support narrowing differences. */
5077 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
5078 if (cache)
5080 cache[0] = value;
5081 cache[1] = ret;
5083 return ret;
5085 default:
5086 break;
5089 return NULL_TREE;
5092 /* Return nonzero if VALUE is a valid constant-valued expression
5093 for use in initializing a static variable; one that can be an
5094 element of a "constant" initializer.
5096 Return null_pointer_node if the value is absolute;
5097 if it is relocatable, return the variable that determines the relocation.
5098 We assume that VALUE has been folded as much as possible;
5099 therefore, we do not need to check for such things as
5100 arithmetic-combinations of integers. */
5101 tree
5102 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
5104 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
5106 /* An absolute value is required with reverse storage order. */
5107 if (reloc
5108 && reloc != null_pointer_node
5109 && reverse
5110 && !AGGREGATE_TYPE_P (endtype)
5111 && !VECTOR_TYPE_P (endtype))
5112 reloc = NULL_TREE;
5114 return reloc;
5117 /* Return true if VALUE is a valid constant-valued expression
5118 for use in initializing a static bit-field; one that can be
5119 an element of a "constant" initializer. */
5121 bool
5122 initializer_constant_valid_for_bitfield_p (const_tree value)
5124 /* For bitfields we support integer constants or possibly nested aggregates
5125 of such. */
5126 switch (TREE_CODE (value))
5128 case CONSTRUCTOR:
5130 unsigned HOST_WIDE_INT idx;
5131 const_tree elt;
5133 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
5134 if (!initializer_constant_valid_for_bitfield_p (elt))
5135 return false;
5136 return true;
5139 case INTEGER_CST:
5140 case REAL_CST:
5141 return true;
5143 case VIEW_CONVERT_EXPR:
5144 case NON_LVALUE_EXPR:
5145 return
5146 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
5148 default:
5149 break;
5152 return false;
5155 /* Check if a STRING_CST fits into the field.
5156 Tolerate only the case when the NUL termination
5157 does not fit into the field. */
5159 static bool
5160 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
5162 tree type = TREE_TYPE (string);
5163 tree eltype = TREE_TYPE (type);
5164 unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
5165 unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5166 int len = TREE_STRING_LENGTH (string);
5168 if (elts != 1 && elts != 2 && elts != 4)
5169 return false;
5170 if (len < 0 || len % elts != 0)
5171 return false;
5172 if (size < (unsigned)len)
5173 return false;
5174 if (mem_size != size)
5175 return false;
5176 return true;
5179 /* output_constructor outer state of relevance in recursive calls, typically
5180 for nested aggregate bitfields. */
5182 struct oc_outer_state {
5183 unsigned int bit_offset; /* current position in ... */
5184 int byte; /* ... the outer byte buffer. */
5187 static unsigned HOST_WIDE_INT
5188 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
5189 oc_outer_state *);
5191 /* Output assembler code for constant EXP, with no label.
5192 This includes the pseudo-op such as ".int" or ".byte", and a newline.
5193 Assumes output_addressed_constants has been done on EXP already.
5195 Generate at least SIZE bytes of assembler data, padding at the end
5196 with zeros if necessary. SIZE must always be specified. The returned
5197 value is the actual number of bytes of assembler data generated, which
5198 may be bigger than SIZE if the object contains a variable length field.
5200 SIZE is important for structure constructors,
5201 since trailing members may have been omitted from the constructor.
5202 It is also important for initialization of arrays from string constants
5203 since the full length of the string constant might not be wanted.
5204 It is also needed for initialization of unions, where the initializer's
5205 type is just one member, and that may not be as long as the union.
5207 There a case in which we would fail to output exactly SIZE bytes:
5208 for a structure constructor that wants to produce more than SIZE bytes.
5209 But such constructors will never be generated for any possible input.
5211 ALIGN is the alignment of the data in bits.
5213 If REVERSE is true, EXP is output in reverse storage order. */
5215 static unsigned HOST_WIDE_INT
5216 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5217 bool reverse, bool merge_strings)
5219 enum tree_code code;
5220 unsigned HOST_WIDE_INT thissize;
5221 rtx cst;
5223 if (size == 0 || flag_syntax_only)
5224 return size;
5226 /* See if we're trying to initialize a pointer in a non-default mode
5227 to the address of some declaration somewhere. If the target says
5228 the mode is valid for pointers, assume the target has a way of
5229 resolving it. */
5230 if (TREE_CODE (exp) == NOP_EXPR
5231 && POINTER_TYPE_P (TREE_TYPE (exp))
5232 && targetm.addr_space.valid_pointer_mode
5233 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5234 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5236 tree saved_type = TREE_TYPE (exp);
5238 /* Peel off any intermediate conversions-to-pointer for valid
5239 pointer modes. */
5240 while (TREE_CODE (exp) == NOP_EXPR
5241 && POINTER_TYPE_P (TREE_TYPE (exp))
5242 && targetm.addr_space.valid_pointer_mode
5243 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5244 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5245 exp = TREE_OPERAND (exp, 0);
5247 /* If what we're left with is the address of something, we can
5248 convert the address to the final type and output it that
5249 way. */
5250 if (TREE_CODE (exp) == ADDR_EXPR)
5251 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
5252 /* Likewise for constant ints. */
5253 else if (TREE_CODE (exp) == INTEGER_CST)
5254 exp = fold_convert (saved_type, exp);
5258 /* Eliminate any conversions since we'll be outputting the underlying
5259 constant. */
5260 while (CONVERT_EXPR_P (exp)
5261 || TREE_CODE (exp) == NON_LVALUE_EXPR
5262 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
5264 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
5265 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
5267 /* Make sure eliminating the conversion is really a no-op, except with
5268 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
5269 union types to allow for Ada unchecked unions. */
5270 if (type_size > op_size
5271 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
5272 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
5273 /* Keep the conversion. */
5274 break;
5275 else
5276 exp = TREE_OPERAND (exp, 0);
5279 code = TREE_CODE (TREE_TYPE (exp));
5280 thissize = int_size_in_bytes (TREE_TYPE (exp));
5282 /* Allow a constructor with no elements for any data type.
5283 This means to fill the space with zeros. */
5284 if (TREE_CODE (exp) == CONSTRUCTOR
5285 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
5287 assemble_zeros (size);
5288 return size;
5291 if (TREE_CODE (exp) == FDESC_EXPR)
5293 #ifdef ASM_OUTPUT_FDESC
5294 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
5295 tree decl = TREE_OPERAND (exp, 0);
5296 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
5297 #else
5298 gcc_unreachable ();
5299 #endif
5300 return size;
5303 /* Now output the underlying data. If we've handling the padding, return.
5304 Otherwise, break and ensure SIZE is the size written. */
5305 switch (code)
5307 case BOOLEAN_TYPE:
5308 case INTEGER_TYPE:
5309 case ENUMERAL_TYPE:
5310 case POINTER_TYPE:
5311 case REFERENCE_TYPE:
5312 case OFFSET_TYPE:
5313 case FIXED_POINT_TYPE:
5314 case NULLPTR_TYPE:
5315 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5316 if (reverse)
5317 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5318 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5319 error ("initializer for integer/fixed-point value is too complicated");
5320 break;
5322 case REAL_TYPE:
5323 gcc_assert (size == thissize);
5324 if (TREE_CODE (exp) != REAL_CST)
5325 error ("initializer for floating value is not a floating constant");
5326 else
5327 assemble_real (TREE_REAL_CST (exp),
5328 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
5329 align, reverse);
5330 break;
5332 case COMPLEX_TYPE:
5333 output_constant (TREE_REALPART (exp), thissize / 2, align,
5334 reverse, false);
5335 output_constant (TREE_IMAGPART (exp), thissize / 2,
5336 min_align (align, BITS_PER_UNIT * (thissize / 2)),
5337 reverse, false);
5338 break;
5340 case BITINT_TYPE:
5341 if (TREE_CODE (exp) != INTEGER_CST)
5342 error ("initializer for %<_BitInt(%d)%> value is not an integer "
5343 "constant", TYPE_PRECISION (TREE_TYPE (exp)));
5344 else
5346 struct bitint_info info;
5347 tree type = TREE_TYPE (exp);
5348 bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
5349 gcc_assert (ok);
5350 scalar_int_mode limb_mode
5351 = as_a <scalar_int_mode> (info.abi_limb_mode);
5352 if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode))
5354 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5355 if (reverse)
5356 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5357 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5358 error ("initializer for integer/fixed-point value is too "
5359 "complicated");
5360 break;
5362 int prec = GET_MODE_PRECISION (limb_mode);
5363 int cnt = CEIL (TYPE_PRECISION (type), prec);
5364 tree limb_type = build_nonstandard_integer_type (prec, 1);
5365 int elt_size = GET_MODE_SIZE (limb_mode);
5366 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (limb_mode));
5367 thissize = 0;
5368 if (prec == HOST_BITS_PER_WIDE_INT)
5369 for (int i = 0; i < cnt; i++)
5371 int idx = (info.big_endian ^ reverse) ? cnt - 1 - i : i;
5372 tree c;
5373 if (idx >= TREE_INT_CST_EXT_NUNITS (exp))
5374 c = build_int_cst (limb_type,
5375 tree_int_cst_sgn (exp) < 0 ? -1 : 0);
5376 else
5377 c = build_int_cst (limb_type,
5378 TREE_INT_CST_ELT (exp, idx));
5379 output_constant (c, elt_size, nalign, reverse, false);
5380 thissize += elt_size;
5382 else
5383 for (int i = 0; i < cnt; i++)
5385 int idx = (info.big_endian ^ reverse) ? cnt - 1 - i : i;
5386 wide_int w = wi::rshift (wi::to_wide (exp), idx * prec,
5387 TYPE_SIGN (TREE_TYPE (exp)));
5388 tree c = wide_int_to_tree (limb_type,
5389 wide_int::from (w, prec, UNSIGNED));
5390 output_constant (c, elt_size, nalign, reverse, false);
5391 thissize += elt_size;
5394 break;
5396 case ARRAY_TYPE:
5397 case VECTOR_TYPE:
5398 switch (TREE_CODE (exp))
5400 case CONSTRUCTOR:
5401 return output_constructor (exp, size, align, reverse, NULL);
5402 case STRING_CST:
5403 thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5404 if (merge_strings
5405 && (thissize == 0
5406 || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5407 thissize++;
5408 gcc_checking_assert (check_string_literal (exp, size));
5409 assemble_string (TREE_STRING_POINTER (exp), thissize);
5410 break;
5411 case VECTOR_CST:
5413 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5414 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5415 int elt_size = GET_MODE_SIZE (inner);
5416 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5417 reverse, false);
5418 thissize = elt_size;
5419 /* Static constants must have a fixed size. */
5420 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5421 for (unsigned int i = 1; i < nunits; i++)
5423 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5424 reverse, false);
5425 thissize += elt_size;
5427 break;
5429 default:
5430 gcc_unreachable ();
5432 break;
5434 case RECORD_TYPE:
5435 case UNION_TYPE:
5436 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5437 return output_constructor (exp, size, align, reverse, NULL);
5439 case ERROR_MARK:
5440 return 0;
5442 default:
5443 gcc_unreachable ();
5446 if (size > thissize)
5447 assemble_zeros (size - thissize);
5449 return size;
5452 /* Subroutine of output_constructor, used for computing the size of
5453 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
5454 type with an unspecified upper bound. */
5456 static unsigned HOST_WIDE_INT
5457 array_size_for_constructor (tree val)
5459 tree max_index;
5460 unsigned HOST_WIDE_INT cnt;
5461 tree index, value, tmp;
5462 offset_int i;
5464 /* This code used to attempt to handle string constants that are not
5465 arrays of single-bytes, but nothing else does, so there's no point in
5466 doing it here. */
5467 if (TREE_CODE (val) == STRING_CST)
5468 return TREE_STRING_LENGTH (val);
5470 max_index = NULL_TREE;
5471 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5473 if (TREE_CODE (index) == RANGE_EXPR)
5474 index = TREE_OPERAND (index, 1);
5475 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5476 max_index = index;
5479 if (max_index == NULL_TREE)
5480 return 0;
5482 /* Compute the total number of array elements. */
5483 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5484 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5486 /* Multiply by the array element unit size to find number of bytes. */
5487 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5489 gcc_assert (wi::fits_uhwi_p (i));
5490 return i.to_uhwi ();
5493 /* Other datastructures + helpers for output_constructor. */
5495 /* output_constructor local state to support interaction with helpers. */
5497 struct oc_local_state {
5499 /* Received arguments. */
5500 tree exp; /* Constructor expression. */
5501 tree type; /* Type of constructor expression. */
5502 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5503 unsigned int align; /* Known initial alignment. */
5504 tree min_index; /* Lower bound if specified for an array. */
5506 /* Output processing state. */
5507 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5508 int byte; /* Part of a bitfield byte yet to be output. */
5509 int last_relative_index; /* Implicit or explicit index of the last
5510 array element output within a bitfield. */
5511 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5512 bool reverse; /* Whether reverse storage order is in use. */
5514 /* Current element. */
5515 tree field; /* Current field decl in a record. */
5516 tree val; /* Current element value. */
5517 tree index; /* Current element index. */
5521 /* Helper for output_constructor. From the current LOCAL state, output a
5522 RANGE_EXPR element. */
5524 static void
5525 output_constructor_array_range (oc_local_state *local)
5527 /* Perform the index calculation in modulo arithmetic but
5528 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5529 but we are using an unsigned sizetype. */
5530 unsigned prec = TYPE_PRECISION (sizetype);
5531 offset_int idx = wi::sext (wi::to_offset (TREE_OPERAND (local->index, 0))
5532 - wi::to_offset (local->min_index), prec);
5533 tree valtype = TREE_TYPE (local->val);
5534 HOST_WIDE_INT fieldpos
5535 = (idx * wi::to_offset (TYPE_SIZE_UNIT (valtype))).to_short_addr ();
5537 /* Advance to offset of this element. */
5538 if (fieldpos > local->total_bytes)
5540 assemble_zeros (fieldpos - local->total_bytes);
5541 local->total_bytes = fieldpos;
5543 else
5544 /* Must not go backwards. */
5545 gcc_assert (fieldpos == local->total_bytes);
5547 unsigned HOST_WIDE_INT fieldsize
5548 = int_size_in_bytes (TREE_TYPE (local->type));
5550 HOST_WIDE_INT lo_index
5551 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5552 HOST_WIDE_INT hi_index
5553 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5554 HOST_WIDE_INT index;
5556 unsigned int align2
5557 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5559 for (index = lo_index; index <= hi_index; index++)
5561 /* Output the element's initial value. */
5562 if (local->val == NULL_TREE)
5563 assemble_zeros (fieldsize);
5564 else
5565 fieldsize = output_constant (local->val, fieldsize, align2,
5566 local->reverse, false);
5568 /* Count its size. */
5569 local->total_bytes += fieldsize;
5573 /* Helper for output_constructor. From the current LOCAL state, output a
5574 field element that is not true bitfield or part of an outer one. */
5576 static void
5577 output_constructor_regular_field (oc_local_state *local)
5579 /* Field size and position. Since this structure is static, we know the
5580 positions are constant. */
5581 unsigned HOST_WIDE_INT fieldsize;
5582 HOST_WIDE_INT fieldpos;
5584 unsigned int align2;
5586 /* Output any buffered-up bit-fields preceding this element. */
5587 if (local->byte_buffer_in_use)
5589 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5590 local->total_bytes++;
5591 local->byte_buffer_in_use = false;
5594 if (local->index != NULL_TREE)
5596 /* Perform the index calculation in modulo arithmetic but
5597 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5598 but we are using an unsigned sizetype. */
5599 unsigned prec = TYPE_PRECISION (sizetype);
5600 offset_int idx = wi::sext (wi::to_offset (local->index)
5601 - wi::to_offset (local->min_index), prec);
5602 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5603 .to_short_addr ();
5605 else if (local->field != NULL_TREE)
5606 fieldpos = int_byte_position (local->field);
5607 else
5608 fieldpos = 0;
5610 /* Advance to offset of this element.
5611 Note no alignment needed in an array, since that is guaranteed
5612 if each element has the proper size. */
5613 if (local->field != NULL_TREE || local->index != NULL_TREE)
5615 if (fieldpos > local->total_bytes)
5617 assemble_zeros (fieldpos - local->total_bytes);
5618 local->total_bytes = fieldpos;
5620 else
5621 /* Must not go backwards. */
5622 gcc_assert (fieldpos == local->total_bytes);
5625 /* Find the alignment of this element. */
5626 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5628 /* Determine size this element should occupy. */
5629 if (local->field)
5631 fieldsize = 0;
5633 /* If this is an array with an unspecified upper bound,
5634 the initializer determines the size. */
5635 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5636 but we cannot do this until the deprecated support for
5637 initializing zero-length array members is removed. */
5638 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5639 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5640 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5642 unsigned HOST_WIDE_INT fldsize
5643 = array_size_for_constructor (local->val);
5644 fieldsize = int_size_in_bytes (TREE_TYPE (local->val));
5645 /* In most cases fieldsize == fldsize as the size of the initializer
5646 determines how many elements the flexible array member has. For
5647 C++ fldsize can be smaller though, if the last or several last or
5648 all initializers of the flexible array member have side-effects
5649 and the FE splits them into dynamic initialization. */
5650 gcc_checking_assert (fieldsize >= fldsize);
5651 /* Given a non-empty initialization, this field had better
5652 be last. Given a flexible array member, the next field
5653 on the chain is a TYPE_DECL of the enclosing struct. */
5654 const_tree next = DECL_CHAIN (local->field);
5655 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5657 else
5658 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5660 else
5661 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5663 /* Output the element's initial value. */
5664 if (local->val == NULL_TREE)
5665 assemble_zeros (fieldsize);
5666 else
5667 fieldsize = output_constant (local->val, fieldsize, align2,
5668 local->reverse, false);
5670 /* Count its size. */
5671 local->total_bytes += fieldsize;
5674 /* Helper for output_constructor. From the LOCAL state, output an element
5675 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5676 from the start of a possibly ongoing outer byte buffer. */
5678 static void
5679 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5681 /* Bit size of this element. */
5682 HOST_WIDE_INT ebitsize
5683 = (local->field
5684 ? tree_to_uhwi (DECL_SIZE (local->field))
5685 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5687 /* Relative index of this element if this is an array component. */
5688 HOST_WIDE_INT relative_index
5689 = (local->field
5691 : (local->index
5692 ? tree_to_uhwi (local->index) - tree_to_uhwi (local->min_index)
5693 : local->last_relative_index + 1));
5695 /* Bit position of this element from the start of the containing
5696 constructor. */
5697 HOST_WIDE_INT constructor_relative_ebitpos
5698 = (local->field
5699 ? int_bit_position (local->field)
5700 : ebitsize * relative_index);
5702 /* Bit position of this element from the start of a possibly ongoing
5703 outer byte buffer. */
5704 HOST_WIDE_INT byte_relative_ebitpos
5705 = bit_offset + constructor_relative_ebitpos;
5707 /* From the start of a possibly ongoing outer byte buffer, offsets to
5708 the first bit of this element and to the first bit past the end of
5709 this element. */
5710 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5711 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5713 local->last_relative_index = relative_index;
5715 if (local->val == NULL_TREE)
5716 local->val = integer_zero_node;
5718 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5719 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5720 local->val = TREE_OPERAND (local->val, 0);
5722 if (TREE_CODE (local->val) != INTEGER_CST
5723 && TREE_CODE (local->val) != CONSTRUCTOR)
5725 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5726 return;
5729 /* If this field does not start in this (or next) byte, skip some bytes. */
5730 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5732 /* Output remnant of any bit field in previous bytes. */
5733 if (local->byte_buffer_in_use)
5735 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5736 local->total_bytes++;
5737 local->byte_buffer_in_use = false;
5740 /* If still not at proper byte, advance to there. */
5741 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5743 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5744 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5745 local->total_bytes = next_offset / BITS_PER_UNIT;
5749 /* Set up the buffer if necessary. */
5750 if (!local->byte_buffer_in_use)
5752 local->byte = 0;
5753 if (ebitsize > 0)
5754 local->byte_buffer_in_use = true;
5757 /* If this is nested constructor, recurse passing the bit offset and the
5758 pending data, then retrieve the new pending data afterwards. */
5759 if (TREE_CODE (local->val) == CONSTRUCTOR)
5761 oc_outer_state temp_state;
5762 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5763 temp_state.byte = local->byte;
5764 local->total_bytes
5765 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5766 local->byte = temp_state.byte;
5767 return;
5770 /* Otherwise, we must split the element into pieces that fall within
5771 separate bytes, and combine each byte with previous or following
5772 bit-fields. */
5773 while (next_offset < end_offset)
5775 int this_time;
5776 int shift;
5777 unsigned HOST_WIDE_INT value;
5778 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5779 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5781 /* Advance from byte to byte within this element when necessary. */
5782 while (next_byte != local->total_bytes)
5784 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5785 local->total_bytes++;
5786 local->byte = 0;
5789 /* Number of bits we can process at once (all part of the same byte). */
5790 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5791 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5793 /* For big-endian data, take the most significant bits (of the
5794 bits that are significant) first and put them into bytes from
5795 the most significant end. */
5796 shift = end_offset - next_offset - this_time;
5798 /* Don't try to take a bunch of bits that cross
5799 the word boundary in the INTEGER_CST. We can
5800 only select bits from one element. */
5801 if ((shift / HOST_BITS_PER_WIDE_INT)
5802 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5804 const int end = shift + this_time - 1;
5805 shift = end & -HOST_BITS_PER_WIDE_INT;
5806 this_time = end - shift + 1;
5809 /* Now get the bits we want to insert. */
5810 value = wi::extract_uhwi (wi::to_widest (local->val),
5811 shift, this_time);
5813 /* Get the result. This works only when:
5814 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5815 local->byte |= value << (BITS_PER_UNIT - this_time - next_bit);
5817 else
5819 /* On little-endian machines, take the least significant bits of
5820 the value first and pack them starting at the least significant
5821 bits of the bytes. */
5822 shift = next_offset - byte_relative_ebitpos;
5824 /* Don't try to take a bunch of bits that cross
5825 the word boundary in the INTEGER_CST. We can
5826 only select bits from one element. */
5827 if ((shift / HOST_BITS_PER_WIDE_INT)
5828 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5829 this_time
5830 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5832 /* Now get the bits we want to insert. */
5833 value = wi::extract_uhwi (wi::to_widest (local->val),
5834 shift, this_time);
5836 /* Get the result. This works only when:
5837 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5838 local->byte |= value << next_bit;
5841 next_offset += this_time;
5842 local->byte_buffer_in_use = true;
5846 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5847 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5848 caller output state of relevance in recursive invocations. */
5850 static unsigned HOST_WIDE_INT
5851 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5852 bool reverse, oc_outer_state *outer)
5854 unsigned HOST_WIDE_INT cnt;
5855 constructor_elt *ce;
5856 oc_local_state local;
5858 /* Setup our local state to communicate with helpers. */
5859 local.exp = exp;
5860 local.type = TREE_TYPE (exp);
5861 local.size = size;
5862 local.align = align;
5863 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5864 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5865 else
5866 local.min_index = integer_zero_node;
5868 local.total_bytes = 0;
5869 local.byte_buffer_in_use = outer != NULL;
5870 local.byte = outer ? outer->byte : 0;
5871 local.last_relative_index = -1;
5872 /* The storage order is specified for every aggregate type. */
5873 if (AGGREGATE_TYPE_P (local.type))
5874 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5875 else
5876 local.reverse = reverse;
5878 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5880 /* As CE goes through the elements of the constant, FIELD goes through the
5881 structure fields if the constant is a structure. If the constant is a
5882 union, we override this by getting the field from the TREE_LIST element.
5883 But the constant could also be an array. Then FIELD is zero.
5885 There is always a maximum of one element in the chain LINK for unions
5886 (even if the initializer in a source program incorrectly contains
5887 more one). */
5889 if (TREE_CODE (local.type) == RECORD_TYPE)
5890 local.field = TYPE_FIELDS (local.type);
5891 else
5892 local.field = NULL_TREE;
5894 for (cnt = 0;
5895 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5896 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5898 local.val = ce->value;
5899 local.index = NULL_TREE;
5901 /* The element in a union constructor specifies the proper field
5902 or index. */
5903 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5904 local.field = ce->index;
5906 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5907 local.index = ce->index;
5909 if (local.field && flag_verbose_asm)
5910 fprintf (asm_out_file, "%s %s:\n",
5911 ASM_COMMENT_START,
5912 DECL_NAME (local.field)
5913 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5914 : "<anonymous>");
5916 /* Eliminate the marker that makes a cast not be an lvalue. */
5917 if (local.val != NULL_TREE)
5918 STRIP_NOPS (local.val);
5920 /* Output the current element, using the appropriate helper ... */
5922 /* For an array slice not part of an outer bitfield. */
5923 if (!outer
5924 && local.index != NULL_TREE
5925 && TREE_CODE (local.index) == RANGE_EXPR)
5926 output_constructor_array_range (&local);
5928 /* For a field that is neither a true bitfield nor part of an outer one,
5929 known to be at least byte aligned and multiple-of-bytes long. */
5930 else if (!outer
5931 && (local.field == NULL_TREE
5932 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5933 output_constructor_regular_field (&local);
5935 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5936 supported for scalar fields, so we may need to convert first. */
5937 else
5939 if (TREE_CODE (local.val) == REAL_CST)
5940 local.val
5941 = fold_unary (VIEW_CONVERT_EXPR,
5942 build_nonstandard_integer_type
5943 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5944 local.val);
5945 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5949 /* If we are not at toplevel, save the pending data for our caller.
5950 Otherwise output the pending data and padding zeros as needed. */
5951 if (outer)
5952 outer->byte = local.byte;
5953 else
5955 if (local.byte_buffer_in_use)
5957 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5958 local.total_bytes++;
5961 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5963 assemble_zeros (local.size - local.total_bytes);
5964 local.total_bytes = local.size;
5968 return local.total_bytes;
5971 /* Mark DECL as weak. */
5973 static void
5974 mark_weak (tree decl)
5976 if (DECL_WEAK (decl))
5977 return;
5979 struct symtab_node *n = symtab_node::get (decl);
5980 if (n && n->refuse_visibility_changes)
5981 error ("%qD declared weak after being used", decl);
5982 DECL_WEAK (decl) = 1;
5984 if (DECL_RTL_SET_P (decl)
5985 && MEM_P (DECL_RTL (decl))
5986 && XEXP (DECL_RTL (decl), 0)
5987 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5988 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5991 /* Merge weak status between NEWDECL and OLDDECL. */
5993 void
5994 merge_weak (tree newdecl, tree olddecl)
5996 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5998 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
6000 tree *pwd;
6001 /* We put the NEWDECL on the weak_decls list at some point
6002 and OLDDECL as well. Keep just OLDDECL on the list. */
6003 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
6004 if (TREE_VALUE (*pwd) == newdecl)
6006 *pwd = TREE_CHAIN (*pwd);
6007 break;
6010 return;
6013 if (DECL_WEAK (newdecl))
6015 tree wd;
6017 /* NEWDECL is weak, but OLDDECL is not. */
6019 /* If we already output the OLDDECL, we're in trouble; we can't
6020 go back and make it weak. This should never happen in
6021 unit-at-a-time compilation. */
6022 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
6024 /* If we've already generated rtl referencing OLDDECL, we may
6025 have done so in a way that will not function properly with
6026 a weak symbol. Again in unit-at-a-time this should be
6027 impossible. */
6028 gcc_assert (!TREE_USED (olddecl)
6029 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
6031 /* PR 49899: You cannot convert a static function into a weak, public function. */
6032 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
6033 error ("weak declaration of %q+D being applied to a already "
6034 "existing, static definition", newdecl);
6036 if (TARGET_SUPPORTS_WEAK)
6038 /* We put the NEWDECL on the weak_decls list at some point.
6039 Replace it with the OLDDECL. */
6040 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
6041 if (TREE_VALUE (wd) == newdecl)
6043 TREE_VALUE (wd) = olddecl;
6044 break;
6046 /* We may not find the entry on the list. If NEWDECL is a
6047 weak alias, then we will have already called
6048 globalize_decl to remove the entry; in that case, we do
6049 not need to do anything. */
6052 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
6053 mark_weak (olddecl);
6055 else
6056 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
6057 weak. Just update NEWDECL to indicate that it's weak too. */
6058 mark_weak (newdecl);
6061 /* Declare DECL to be a weak symbol. */
6063 void
6064 declare_weak (tree decl)
6066 /* With -fsyntax-only, TREE_ASM_WRITTEN might be set on certain function
6067 decls earlier than normally, but as with -fsyntax-only nothing is really
6068 emitted, there is no harm in marking it weak later. */
6069 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL
6070 || !TREE_ASM_WRITTEN (decl)
6071 || flag_syntax_only);
6072 if (! TREE_PUBLIC (decl))
6074 error ("weak declaration of %q+D must be public", decl);
6075 return;
6077 else if (!TARGET_SUPPORTS_WEAK)
6078 warning (0, "weak declaration of %q+D not supported", decl);
6080 mark_weak (decl);
6081 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
6082 DECL_ATTRIBUTES (decl)
6083 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
6086 static void
6087 weak_finish_1 (tree decl)
6089 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
6090 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6091 #endif
6093 if (! TREE_USED (decl))
6094 return;
6096 #ifdef ASM_WEAKEN_DECL
6097 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
6098 #else
6099 #ifdef ASM_WEAKEN_LABEL
6100 ASM_WEAKEN_LABEL (asm_out_file, name);
6101 #else
6102 #ifdef ASM_OUTPUT_WEAK_ALIAS
6104 static bool warn_once = 0;
6105 if (! warn_once)
6107 warning (0, "only weak aliases are supported in this configuration");
6108 warn_once = 1;
6110 return;
6112 #endif
6113 #endif
6114 #endif
6117 /* Fiven an assembly name, find the decl it is associated with. */
6118 static tree
6119 find_decl (tree target)
6121 symtab_node *node = symtab_node::get_for_asmname (target);
6122 if (node)
6123 return node->decl;
6124 return NULL_TREE;
6127 /* This TREE_LIST contains weakref targets. */
6129 static GTY(()) tree weakref_targets;
6131 /* Emit any pending weak declarations. */
6133 void
6134 weak_finish (void)
6136 tree t;
6138 for (t = weakref_targets; t; t = TREE_CHAIN (t))
6140 tree alias_decl = TREE_PURPOSE (t);
6141 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
6143 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
6144 || TREE_SYMBOL_REFERENCED (target))
6145 /* Remove alias_decl from the weak list, but leave entries for
6146 the target alone. */
6147 target = NULL_TREE;
6148 #ifndef ASM_OUTPUT_WEAKREF
6149 else if (! TREE_SYMBOL_REFERENCED (target))
6151 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
6152 defined, otherwise we and weak_finish_1 would use
6153 different macros. */
6154 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
6155 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
6156 # else
6157 tree decl = find_decl (target);
6159 if (! decl)
6161 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
6162 TREE_CODE (alias_decl), target,
6163 TREE_TYPE (alias_decl));
6165 DECL_EXTERNAL (decl) = 1;
6166 TREE_PUBLIC (decl) = 1;
6167 DECL_ARTIFICIAL (decl) = 1;
6168 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
6169 TREE_USED (decl) = 1;
6172 weak_finish_1 (decl);
6173 # endif
6175 #endif
6178 tree *p;
6179 tree t2;
6181 /* Remove the alias and the target from the pending weak list
6182 so that we do not emit any .weak directives for the former,
6183 nor multiple .weak directives for the latter. */
6184 for (p = &weak_decls; (t2 = *p) ; )
6186 if (TREE_VALUE (t2) == alias_decl
6187 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
6188 *p = TREE_CHAIN (t2);
6189 else
6190 p = &TREE_CHAIN (t2);
6193 /* Remove other weakrefs to the same target, to speed things up. */
6194 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
6196 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
6197 *p = TREE_CHAIN (t2);
6198 else
6199 p = &TREE_CHAIN (t2);
6204 for (t = weak_decls; t; t = TREE_CHAIN (t))
6206 tree decl = TREE_VALUE (t);
6208 weak_finish_1 (decl);
6212 /* Emit the assembly bits to indicate that DECL is globally visible. */
6214 static void
6215 globalize_decl (tree decl)
6218 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
6219 if (DECL_WEAK (decl))
6221 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6222 tree *p, t;
6224 #ifdef ASM_WEAKEN_DECL
6225 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
6226 #else
6227 ASM_WEAKEN_LABEL (asm_out_file, name);
6228 #endif
6230 /* Remove this function from the pending weak list so that
6231 we do not emit multiple .weak directives for it. */
6232 for (p = &weak_decls; (t = *p) ; )
6234 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6235 *p = TREE_CHAIN (t);
6236 else
6237 p = &TREE_CHAIN (t);
6240 /* Remove weakrefs to the same target from the pending weakref
6241 list, for the same reason. */
6242 for (p = &weakref_targets; (t = *p) ; )
6244 if (DECL_ASSEMBLER_NAME (decl)
6245 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6246 *p = TREE_CHAIN (t);
6247 else
6248 p = &TREE_CHAIN (t);
6251 return;
6253 #endif
6255 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
6258 vec<alias_pair, va_gc> *alias_pairs;
6260 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
6261 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
6262 tree node is DECL to have the value of the tree node TARGET. */
6264 void
6265 do_assemble_alias (tree decl, tree target)
6267 tree id;
6269 /* Emulated TLS had better not get this var. */
6270 gcc_assert (!(!targetm.have_tls
6271 && VAR_P (decl)
6272 && DECL_THREAD_LOCAL_P (decl)));
6274 if (TREE_ASM_WRITTEN (decl))
6275 return;
6277 id = DECL_ASSEMBLER_NAME (decl);
6278 ultimate_transparent_alias_target (&id);
6279 ultimate_transparent_alias_target (&target);
6281 /* We must force creation of DECL_RTL for debug info generation, even though
6282 we don't use it here. */
6283 make_decl_rtl (decl);
6285 TREE_ASM_WRITTEN (decl) = 1;
6286 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
6287 TREE_ASM_WRITTEN (id) = 1;
6289 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6291 if (!TREE_SYMBOL_REFERENCED (target))
6292 weakref_targets = tree_cons (decl, target, weakref_targets);
6294 #ifdef ASM_OUTPUT_WEAKREF
6295 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
6296 IDENTIFIER_POINTER (id),
6297 IDENTIFIER_POINTER (target));
6298 #else
6299 if (!TARGET_SUPPORTS_WEAK)
6301 error_at (DECL_SOURCE_LOCATION (decl),
6302 "%qs is not supported in this configuration", "weakref ");
6303 return;
6305 #endif
6306 return;
6309 #ifdef ASM_OUTPUT_DEF
6310 tree orig_decl = decl;
6312 /* Make name accessible from other files, if appropriate. */
6314 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
6316 globalize_decl (decl);
6317 maybe_assemble_visibility (decl);
6319 if (TREE_CODE (decl) == FUNCTION_DECL
6320 && cgraph_node::get (decl)->ifunc_resolver)
6322 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
6323 if (targetm.has_ifunc_p ())
6324 ASM_OUTPUT_TYPE_DIRECTIVE
6325 (asm_out_file, IDENTIFIER_POINTER (id),
6326 IFUNC_ASM_TYPE);
6327 else
6328 #endif
6329 error_at (DECL_SOURCE_LOCATION (decl),
6330 "%qs is not supported on this target", "ifunc");
6333 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
6334 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
6335 # else
6336 ASM_OUTPUT_DEF (asm_out_file,
6337 IDENTIFIER_POINTER (id),
6338 IDENTIFIER_POINTER (target));
6339 # endif
6340 /* If symbol aliases aren't actually supported... */
6341 if (!TARGET_SUPPORTS_ALIASES)
6342 /* ..., 'ASM_OUTPUT_DEF{,_FROM_DECLS}' better have raised an error. */
6343 gcc_checking_assert (seen_error ());
6344 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
6346 const char *name;
6347 tree *p, t;
6349 name = IDENTIFIER_POINTER (id);
6350 # ifdef ASM_WEAKEN_DECL
6351 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
6352 # else
6353 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
6354 # endif
6355 /* Remove this function from the pending weak list so that
6356 we do not emit multiple .weak directives for it. */
6357 for (p = &weak_decls; (t = *p) ; )
6358 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
6359 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6360 *p = TREE_CHAIN (t);
6361 else
6362 p = &TREE_CHAIN (t);
6364 /* Remove weakrefs to the same target from the pending weakref
6365 list, for the same reason. */
6366 for (p = &weakref_targets; (t = *p) ; )
6368 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6369 *p = TREE_CHAIN (t);
6370 else
6371 p = &TREE_CHAIN (t);
6374 #endif
6377 /* Output .symver directive. */
6379 void
6380 do_assemble_symver (tree decl, tree target)
6382 tree id = DECL_ASSEMBLER_NAME (decl);
6383 ultimate_transparent_alias_target (&id);
6384 ultimate_transparent_alias_target (&target);
6385 #ifdef ASM_OUTPUT_SYMVER_DIRECTIVE
6386 ASM_OUTPUT_SYMVER_DIRECTIVE (asm_out_file,
6387 IDENTIFIER_POINTER (target),
6388 IDENTIFIER_POINTER (id));
6389 #else
6390 error ("symver is only supported on ELF platforms");
6391 #endif
6394 /* Emit an assembler directive to make the symbol for DECL an alias to
6395 the symbol for TARGET. */
6397 void
6398 assemble_alias (tree decl, tree target)
6400 tree target_decl;
6402 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6404 tree alias = DECL_ASSEMBLER_NAME (decl);
6406 ultimate_transparent_alias_target (&target);
6408 if (alias == target)
6409 error ("%qs symbol %q+D ultimately targets itself", "weakref", decl);
6410 if (TREE_PUBLIC (decl))
6411 error ("%qs symbol %q+D must have static linkage", "weakref", decl);
6413 else if (!TARGET_SUPPORTS_ALIASES)
6415 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
6416 error_at (DECL_SOURCE_LOCATION (decl),
6417 "alias definitions not supported in this configuration");
6418 TREE_ASM_WRITTEN (decl) = 1;
6419 return;
6420 # else
6421 if (!DECL_WEAK (decl))
6423 /* NB: ifunc_resolver isn't set when an error is detected. */
6424 if (TREE_CODE (decl) == FUNCTION_DECL
6425 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
6426 error_at (DECL_SOURCE_LOCATION (decl),
6427 "%qs is not supported in this configuration", "ifunc");
6428 else
6429 error_at (DECL_SOURCE_LOCATION (decl),
6430 "only weak aliases are supported in this configuration");
6431 TREE_ASM_WRITTEN (decl) = 1;
6432 return;
6434 # endif
6435 gcc_unreachable ();
6437 TREE_USED (decl) = 1;
6439 /* Allow aliases to aliases. */
6440 if (TREE_CODE (decl) == FUNCTION_DECL)
6441 cgraph_node::get_create (decl)->alias = true;
6442 else
6443 varpool_node::get_create (decl)->alias = true;
6445 /* If the target has already been emitted, we don't have to queue the
6446 alias. This saves a tad of memory. */
6447 if (symtab->global_info_ready)
6448 target_decl = find_decl (target);
6449 else
6450 target_decl= NULL;
6451 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6452 || symtab->state >= EXPANSION)
6453 do_assemble_alias (decl, target);
6454 else
6456 alias_pair p = {decl, target};
6457 vec_safe_push (alias_pairs, p);
6461 /* Record and output a table of translations from original function
6462 to its transaction aware clone. Note that tm_pure functions are
6463 considered to be their own clone. */
6465 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6467 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6468 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6470 static int
6471 keep_cache_entry (tree_map *&e)
6473 return ggc_marked_p (e->base.from);
6477 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6479 void
6480 record_tm_clone_pair (tree o, tree n)
6482 struct tree_map **slot, *h;
6484 if (tm_clone_hash == NULL)
6485 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6487 h = ggc_alloc<tree_map> ();
6488 h->hash = htab_hash_pointer (o);
6489 h->base.from = o;
6490 h->to = n;
6492 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6493 *slot = h;
6496 tree
6497 get_tm_clone_pair (tree o)
6499 if (tm_clone_hash)
6501 struct tree_map *h, in;
6503 in.base.from = o;
6504 in.hash = htab_hash_pointer (o);
6505 h = tm_clone_hash->find_with_hash (&in, in.hash);
6506 if (h)
6507 return h->to;
6509 return NULL_TREE;
6512 struct tm_alias_pair
6514 unsigned int uid;
6515 tree from;
6516 tree to;
6520 /* Dump the actual pairs to the .tm_clone_table section. */
6522 static void
6523 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6525 unsigned i;
6526 tm_alias_pair *p;
6527 bool switched = false;
6529 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6531 tree src = p->from;
6532 tree dst = p->to;
6533 struct cgraph_node *src_n = cgraph_node::get (src);
6534 struct cgraph_node *dst_n = cgraph_node::get (dst);
6536 /* The function ipa_tm_create_version() marks the clone as needed if
6537 the original function was needed. But we also mark the clone as
6538 needed if we ever called the clone indirectly through
6539 TM_GETTMCLONE. If neither of these are true, we didn't generate
6540 a clone, and we didn't call it indirectly... no sense keeping it
6541 in the clone table. */
6542 if (!dst_n || !dst_n->definition)
6543 continue;
6545 /* This covers the case where we have optimized the original
6546 function away, and only access the transactional clone. */
6547 if (!src_n || !src_n->definition)
6548 continue;
6550 if (!switched)
6552 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6553 assemble_align (POINTER_SIZE);
6554 switched = true;
6557 assemble_integer (XEXP (DECL_RTL (src), 0),
6558 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6559 assemble_integer (XEXP (DECL_RTL (dst), 0),
6560 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6564 /* Provide a default for the tm_clone_table section. */
6566 section *
6567 default_clone_table_section (void)
6569 return get_named_section (NULL, ".tm_clone_table", 3);
6572 /* Helper comparison function for qsorting by the DECL_UID stored in
6573 alias_pair->emitted_diags. */
6575 static int
6576 tm_alias_pair_cmp (const void *x, const void *y)
6578 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6579 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6580 if (p1->uid < p2->uid)
6581 return -1;
6582 if (p1->uid > p2->uid)
6583 return 1;
6584 return 0;
6587 void
6588 finish_tm_clone_pairs (void)
6590 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6592 if (tm_clone_hash == NULL)
6593 return;
6595 /* We need a determenistic order for the .tm_clone_table, otherwise
6596 we will get bootstrap comparison failures, so dump the hash table
6597 to a vector, sort it, and dump the vector. */
6599 /* Dump the hashtable to a vector. */
6600 tree_map *map;
6601 hash_table<tm_clone_hasher>::iterator iter;
6602 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6604 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6605 tm_alias_pairs.safe_push (p);
6607 /* Sort it. */
6608 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6610 /* Dump it. */
6611 dump_tm_clone_pairs (tm_alias_pairs);
6613 tm_clone_hash->empty ();
6614 tm_clone_hash = NULL;
6615 tm_alias_pairs.release ();
6619 /* Emit an assembler directive to set symbol for DECL visibility to
6620 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6622 void
6623 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6624 int vis ATTRIBUTE_UNUSED)
6626 #ifdef HAVE_GAS_HIDDEN
6627 static const char * const visibility_types[] = {
6628 NULL, "protected", "hidden", "internal"
6631 const char *name, *type;
6632 tree id;
6634 id = DECL_ASSEMBLER_NAME (decl);
6635 ultimate_transparent_alias_target (&id);
6636 name = IDENTIFIER_POINTER (id);
6638 type = visibility_types[vis];
6640 fprintf (asm_out_file, "\t.%s\t", type);
6641 assemble_name (asm_out_file, name);
6642 fprintf (asm_out_file, "\n");
6643 #else
6644 if (!DECL_ARTIFICIAL (decl))
6645 warning (OPT_Wattributes, "visibility attribute not supported "
6646 "in this configuration; ignored");
6647 #endif
6650 /* A helper function to call assemble_visibility when needed for a decl. */
6652 bool
6653 maybe_assemble_visibility (tree decl)
6655 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6656 if (vis != VISIBILITY_DEFAULT)
6658 targetm.asm_out.assemble_visibility (decl, vis);
6659 return true;
6661 else
6662 return false;
6665 /* Returns true if the target configuration supports defining public symbols
6666 so that one of them will be chosen at link time instead of generating a
6667 multiply-defined symbol error, whether through the use of weak symbols or
6668 a target-specific mechanism for having duplicates discarded. */
6670 bool
6671 supports_one_only (void)
6673 if (SUPPORTS_ONE_ONLY)
6674 return true;
6675 if (TARGET_SUPPORTS_WEAK)
6676 return true;
6677 return false;
6680 /* Set up DECL as a public symbol that can be defined in multiple
6681 translation units without generating a linker error. */
6683 void
6684 make_decl_one_only (tree decl, tree comdat_group)
6686 struct symtab_node *symbol;
6687 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6689 TREE_PUBLIC (decl) = 1;
6691 if (VAR_P (decl))
6692 symbol = varpool_node::get_create (decl);
6693 else
6694 symbol = cgraph_node::get_create (decl);
6696 if (SUPPORTS_ONE_ONLY)
6698 #ifdef MAKE_DECL_ONE_ONLY
6699 MAKE_DECL_ONE_ONLY (decl);
6700 #endif
6701 symbol->set_comdat_group (comdat_group);
6703 else if (VAR_P (decl)
6704 && (DECL_INITIAL (decl) == 0
6705 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6706 DECL_COMMON (decl) = 1;
6707 else
6709 gcc_assert (TARGET_SUPPORTS_WEAK);
6710 DECL_WEAK (decl) = 1;
6714 void
6715 init_varasm_once (void)
6717 section_htab = hash_table<section_hasher>::create_ggc (31);
6718 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6719 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6721 shared_constant_pool = create_constant_pool ();
6723 #ifdef TEXT_SECTION_ASM_OP
6724 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6725 TEXT_SECTION_ASM_OP);
6726 #endif
6728 #ifdef DATA_SECTION_ASM_OP
6729 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6730 DATA_SECTION_ASM_OP);
6731 #endif
6733 #ifdef SDATA_SECTION_ASM_OP
6734 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6735 SDATA_SECTION_ASM_OP);
6736 #endif
6738 #ifdef READONLY_DATA_SECTION_ASM_OP
6739 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6740 READONLY_DATA_SECTION_ASM_OP);
6741 #endif
6743 #ifdef CTORS_SECTION_ASM_OP
6744 ctors_section = get_unnamed_section (0, output_section_asm_op,
6745 CTORS_SECTION_ASM_OP);
6746 #endif
6748 #ifdef DTORS_SECTION_ASM_OP
6749 dtors_section = get_unnamed_section (0, output_section_asm_op,
6750 DTORS_SECTION_ASM_OP);
6751 #endif
6753 #ifdef BSS_SECTION_ASM_OP
6754 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6755 output_section_asm_op,
6756 BSS_SECTION_ASM_OP);
6757 #endif
6759 #ifdef SBSS_SECTION_ASM_OP
6760 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6761 output_section_asm_op,
6762 SBSS_SECTION_ASM_OP);
6763 #endif
6765 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6766 | SECTION_COMMON, emit_tls_common);
6767 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6768 | SECTION_COMMON, emit_local);
6769 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6770 | SECTION_COMMON, emit_common);
6772 #if defined ASM_OUTPUT_ALIGNED_BSS
6773 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6774 emit_bss);
6775 #endif
6777 targetm.asm_out.init_sections ();
6779 if (readonly_data_section == NULL)
6780 readonly_data_section = text_section;
6782 #ifdef ASM_OUTPUT_EXTERNAL
6783 pending_assemble_externals_set = new hash_set<tree>;
6784 #endif
6787 /* Determine whether SYMBOL is used in any optimized function. */
6789 static bool
6790 have_optimized_refs (struct symtab_node *symbol)
6792 struct ipa_ref *ref;
6794 for (int i = 0; symbol->iterate_referring (i, ref); i++)
6796 cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
6798 if (cnode && opt_for_fn (cnode->decl, optimize))
6799 return true;
6802 return false;
6805 /* Check if promoting general-dynamic TLS access model to local-dynamic is
6806 desirable for DECL. */
6808 static bool
6809 optimize_dyn_tls_for_decl_p (const_tree decl)
6811 if (cfun)
6812 return optimize;
6813 return symtab->state >= IPA && have_optimized_refs (symtab_node::get (decl));
6817 enum tls_model
6818 decl_default_tls_model (const_tree decl)
6820 enum tls_model kind;
6821 bool is_local;
6823 is_local = targetm.binds_local_p (decl);
6824 if (!flag_shlib)
6826 if (is_local)
6827 kind = TLS_MODEL_LOCAL_EXEC;
6828 else
6829 kind = TLS_MODEL_INITIAL_EXEC;
6832 /* Local dynamic is inefficient when we're not combining the
6833 parts of the address. */
6834 else if (is_local && optimize_dyn_tls_for_decl_p (decl))
6835 kind = TLS_MODEL_LOCAL_DYNAMIC;
6836 else
6837 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6838 if (kind < flag_tls_default)
6839 kind = flag_tls_default;
6841 return kind;
6844 /* Select a set of attributes for section NAME based on the properties
6845 of DECL and whether or not RELOC indicates that DECL's initializer
6846 might contain runtime relocations.
6848 We make the section read-only and executable for a function decl,
6849 read-only for a const data decl, and writable for a non-const data decl. */
6851 unsigned int
6852 default_section_type_flags (tree decl, const char *name, int reloc)
6854 unsigned int flags;
6856 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6857 flags = SECTION_CODE;
6858 else if (decl)
6860 enum section_category category
6861 = categorize_decl_for_section (decl, reloc);
6862 if (decl_readonly_section_1 (category))
6863 flags = 0;
6864 else if (category == SECCAT_DATA_REL_RO
6865 || category == SECCAT_DATA_REL_RO_LOCAL)
6866 flags = SECTION_WRITE | SECTION_RELRO;
6867 else
6868 flags = SECTION_WRITE;
6870 else
6872 flags = SECTION_WRITE;
6873 if (strcmp (name, ".data.rel.ro") == 0
6874 || strcmp (name, ".data.rel.ro.local") == 0)
6875 flags |= SECTION_RELRO;
6878 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6879 flags |= SECTION_LINKONCE;
6881 if (strcmp (name, ".vtable_map_vars") == 0)
6882 flags |= SECTION_LINKONCE;
6884 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6885 flags |= SECTION_TLS | SECTION_WRITE;
6887 if (strcmp (name, ".bss") == 0
6888 || startswith (name, ".bss.")
6889 || startswith (name, ".gnu.linkonce.b.")
6890 || strcmp (name, ".persistent.bss") == 0
6891 || strcmp (name, ".sbss") == 0
6892 || startswith (name, ".sbss.")
6893 || startswith (name, ".gnu.linkonce.sb."))
6894 flags |= SECTION_BSS;
6896 if (strcmp (name, ".tdata") == 0
6897 || startswith (name, ".tdata.")
6898 || startswith (name, ".gnu.linkonce.td."))
6899 flags |= SECTION_TLS;
6901 if (strcmp (name, ".tbss") == 0
6902 || startswith (name, ".tbss.")
6903 || startswith (name, ".gnu.linkonce.tb."))
6904 flags |= SECTION_TLS | SECTION_BSS;
6906 if (strcmp (name, ".noinit") == 0)
6907 flags |= SECTION_WRITE | SECTION_BSS | SECTION_NOTYPE;
6909 if (strcmp (name, ".persistent") == 0)
6910 flags |= SECTION_WRITE | SECTION_NOTYPE;
6912 /* Various sections have special ELF types that the assembler will
6913 assign by default based on the name. They are neither SHT_PROGBITS
6914 nor SHT_NOBITS, so when changing sections we don't want to print a
6915 section type (@progbits or @nobits). Rather than duplicating the
6916 assembler's knowledge of what those special name patterns are, just
6917 let the assembler choose the type if we don't know a specific
6918 reason to set it to something other than the default. SHT_PROGBITS
6919 is the default for sections whose name is not specially known to
6920 the assembler, so it does no harm to leave the choice to the
6921 assembler when @progbits is the best thing we know to use. If
6922 someone is silly enough to emit code or TLS variables to one of
6923 these sections, then don't handle them specially.
6925 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6926 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
6927 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6928 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6929 flags |= SECTION_NOTYPE;
6931 return flags;
6934 /* Return true if the target supports some form of global BSS,
6935 either through bss_noswitch_section, or by selecting a BSS
6936 section in TARGET_ASM_SELECT_SECTION. */
6938 bool
6939 have_global_bss_p (void)
6941 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6944 /* Output assembly to switch to section NAME with attribute FLAGS.
6945 Four variants for common object file formats. */
6947 void
6948 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6949 unsigned int flags ATTRIBUTE_UNUSED,
6950 tree decl ATTRIBUTE_UNUSED)
6952 /* Some object formats don't support named sections at all. The
6953 front-end should already have flagged this as an error. */
6954 gcc_unreachable ();
6957 #ifndef TLS_SECTION_ASM_FLAG
6958 #define TLS_SECTION_ASM_FLAG 'T'
6959 #endif
6961 void
6962 default_elf_asm_named_section (const char *name, unsigned int flags,
6963 tree decl)
6965 char flagchars[11], *f = flagchars;
6966 unsigned int numeric_value = 0;
6968 /* If we have already declared this section, we can use an
6969 abbreviated form to switch back to it -- unless this section is
6970 part of a COMDAT groups or with SHF_GNU_RETAIN or with SHF_LINK_ORDER,
6971 in which case GAS requires the full declaration every time. */
6972 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6973 && !(flags & (SECTION_RETAIN | SECTION_LINK_ORDER))
6974 && (flags & SECTION_DECLARED))
6976 fprintf (asm_out_file, "\t.section\t%s\n", name);
6977 return;
6980 /* If we have a machine specific flag, then use the numeric value to pass
6981 this on to GAS. */
6982 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6983 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6984 else
6986 if (!(flags & SECTION_DEBUG))
6987 *f++ = 'a';
6988 #if HAVE_GAS_SECTION_EXCLUDE
6989 if (flags & SECTION_EXCLUDE)
6990 *f++ = 'e';
6991 #endif
6992 if (flags & SECTION_WRITE)
6993 *f++ = 'w';
6994 if (flags & SECTION_CODE)
6995 *f++ = 'x';
6996 if (flags & SECTION_SMALL)
6997 *f++ = 's';
6998 if (flags & SECTION_MERGE)
6999 *f++ = 'M';
7000 if (flags & SECTION_STRINGS)
7001 *f++ = 'S';
7002 if (flags & SECTION_TLS)
7003 *f++ = TLS_SECTION_ASM_FLAG;
7004 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
7005 *f++ = 'G';
7006 if (flags & SECTION_RETAIN)
7007 *f++ = 'R';
7008 if (flags & SECTION_LINK_ORDER)
7009 *f++ = 'o';
7010 #ifdef MACH_DEP_SECTION_ASM_FLAG
7011 if (flags & SECTION_MACH_DEP)
7012 *f++ = MACH_DEP_SECTION_ASM_FLAG;
7013 #endif
7014 *f = '\0';
7017 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
7019 /* default_section_type_flags (above) knows which flags need special
7020 handling here, and sets NOTYPE when none of these apply so that the
7021 assembler's logic for default types can apply to user-chosen
7022 section names. */
7023 if (!(flags & SECTION_NOTYPE))
7025 const char *type;
7026 const char *format;
7028 if (flags & SECTION_BSS)
7029 type = "nobits";
7030 else
7031 type = "progbits";
7033 format = ",@%s";
7034 /* On platforms that use "@" as the assembly comment character,
7035 use "%" instead. */
7036 if (strcmp (ASM_COMMENT_START, "@") == 0)
7037 format = ",%%%s";
7038 fprintf (asm_out_file, format, type);
7040 if (flags & SECTION_ENTSIZE)
7041 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
7042 if (flags & SECTION_LINK_ORDER)
7044 /* For now, only section "__patchable_function_entries"
7045 adopts flag SECTION_LINK_ORDER, internal label LPFE*
7046 was emitted in default_print_patchable_function_entry,
7047 just place it here for linked_to section. */
7048 gcc_assert (!strcmp (name, "__patchable_function_entries"));
7049 fprintf (asm_out_file, ",");
7050 char buf[256];
7051 ASM_GENERATE_INTERNAL_LABEL (buf, "LPFE",
7052 current_function_funcdef_no);
7053 assemble_name_raw (asm_out_file, buf);
7055 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
7057 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7058 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
7059 else
7060 fprintf (asm_out_file, ",%s,comdat",
7061 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
7065 putc ('\n', asm_out_file);
7068 void
7069 default_coff_asm_named_section (const char *name, unsigned int flags,
7070 tree decl ATTRIBUTE_UNUSED)
7072 char flagchars[8], *f = flagchars;
7074 if (flags & SECTION_WRITE)
7075 *f++ = 'w';
7076 if (flags & SECTION_CODE)
7077 *f++ = 'x';
7078 *f = '\0';
7080 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
7083 void
7084 default_pe_asm_named_section (const char *name, unsigned int flags,
7085 tree decl)
7087 default_coff_asm_named_section (name, flags, decl);
7089 if (flags & SECTION_LINKONCE)
7091 /* Functions may have been compiled at various levels of
7092 optimization so we can't use `same_size' here.
7093 Instead, have the linker pick one. */
7094 fprintf (asm_out_file, "\t.linkonce %s\n",
7095 (flags & SECTION_CODE ? "discard" : "same_size"));
7099 /* The lame default section selector. */
7101 section *
7102 default_select_section (tree decl, int reloc,
7103 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7105 if (DECL_P (decl))
7107 if (decl_readonly_section (decl, reloc))
7108 return readonly_data_section;
7110 else if (TREE_CODE (decl) == CONSTRUCTOR)
7112 if (! ((flag_pic && reloc)
7113 || !TREE_READONLY (decl)
7114 || !TREE_CONSTANT (decl)))
7115 return readonly_data_section;
7117 else if (TREE_CODE (decl) == STRING_CST)
7118 return readonly_data_section;
7119 else if (! (flag_pic && reloc))
7120 return readonly_data_section;
7122 return data_section;
7125 enum section_category
7126 categorize_decl_for_section (const_tree decl, int reloc)
7128 enum section_category ret;
7130 if (TREE_CODE (decl) == FUNCTION_DECL)
7131 return SECCAT_TEXT;
7132 else if (TREE_CODE (decl) == STRING_CST)
7134 if ((flag_sanitize & SANITIZE_ADDRESS)
7135 && asan_protect_global (CONST_CAST_TREE (decl)))
7136 /* or !flag_merge_constants */
7137 return SECCAT_RODATA;
7138 else
7139 return SECCAT_RODATA_MERGE_STR;
7141 else if (VAR_P (decl))
7143 tree d = CONST_CAST_TREE (decl);
7144 if (bss_initializer_p (decl))
7145 ret = SECCAT_BSS;
7146 else if (! TREE_READONLY (decl)
7147 || (DECL_INITIAL (decl)
7148 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
7150 /* Here the reloc_rw_mask is not testing whether the section should
7151 be read-only or not, but whether the dynamic link will have to
7152 do something. If so, we wish to segregate the data in order to
7153 minimize cache misses inside the dynamic linker. */
7154 if (reloc & targetm.asm_out.reloc_rw_mask ())
7155 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
7156 else
7157 ret = SECCAT_DATA;
7159 else if (reloc & targetm.asm_out.reloc_rw_mask ())
7160 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
7161 else if (reloc || (flag_merge_constants < 2 && !DECL_MERGEABLE (decl))
7162 || ((flag_sanitize & SANITIZE_ADDRESS)
7163 /* PR 81697: for architectures that use section anchors we
7164 need to ignore DECL_RTL_SET_P (decl) for string constants
7165 inside this asan_protect_global call because otherwise
7166 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
7167 section, set DECL_RTL (decl) later on and add DECL to
7168 protected globals via successive asan_protect_global
7169 calls. In this scenario we'll end up with wrong
7170 alignment of these strings at runtime and possible ASan
7171 false positives. */
7172 && asan_protect_global (d, use_object_blocks_p ()
7173 && use_blocks_for_decl_p (d))))
7174 /* C and C++ don't allow different variables to share the same
7175 location. -fmerge-all-constants allows even that (at the
7176 expense of not conforming). */
7177 ret = SECCAT_RODATA;
7178 else if (DECL_INITIAL (decl)
7179 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
7180 ret = SECCAT_RODATA_MERGE_STR_INIT;
7181 else
7182 ret = SECCAT_RODATA_MERGE_CONST;
7184 else if (TREE_CODE (decl) == CONSTRUCTOR)
7186 if ((reloc & targetm.asm_out.reloc_rw_mask ())
7187 || ! TREE_CONSTANT (decl))
7188 ret = SECCAT_DATA;
7189 else
7190 ret = SECCAT_RODATA;
7192 else
7193 ret = SECCAT_RODATA;
7195 /* There are no read-only thread-local sections. */
7196 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7198 /* Note that this would be *just* SECCAT_BSS, except that there's
7199 no concept of a read-only thread-local-data section. */
7200 if (ret == SECCAT_BSS
7201 || DECL_INITIAL (decl) == NULL
7202 || (flag_zero_initialized_in_bss
7203 && initializer_zerop (DECL_INITIAL (decl))))
7204 ret = SECCAT_TBSS;
7205 else
7206 ret = SECCAT_TDATA;
7209 /* If the target uses small data sections, select it. */
7210 else if (targetm.in_small_data_p (decl))
7212 if (ret == SECCAT_BSS)
7213 ret = SECCAT_SBSS;
7214 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
7215 ret = SECCAT_SRODATA;
7216 else
7217 ret = SECCAT_SDATA;
7220 return ret;
7223 static bool
7224 decl_readonly_section_1 (enum section_category category)
7226 switch (category)
7228 case SECCAT_RODATA:
7229 case SECCAT_RODATA_MERGE_STR:
7230 case SECCAT_RODATA_MERGE_STR_INIT:
7231 case SECCAT_RODATA_MERGE_CONST:
7232 case SECCAT_SRODATA:
7233 return true;
7234 default:
7235 return false;
7239 bool
7240 decl_readonly_section (const_tree decl, int reloc)
7242 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
7245 /* Select a section based on the above categorization. */
7247 section *
7248 default_elf_select_section (tree decl, int reloc,
7249 unsigned HOST_WIDE_INT align)
7251 const char *sname;
7253 switch (categorize_decl_for_section (decl, reloc))
7255 case SECCAT_TEXT:
7256 /* We're not supposed to be called on FUNCTION_DECLs. */
7257 gcc_unreachable ();
7258 case SECCAT_RODATA:
7259 return readonly_data_section;
7260 case SECCAT_RODATA_MERGE_STR:
7261 return mergeable_string_section (decl, align, 0);
7262 case SECCAT_RODATA_MERGE_STR_INIT:
7263 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
7264 case SECCAT_RODATA_MERGE_CONST:
7265 return mergeable_constant_section (DECL_MODE (decl), align, 0);
7266 case SECCAT_SRODATA:
7267 sname = ".sdata2";
7268 break;
7269 case SECCAT_DATA:
7270 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7272 sname = ".persistent";
7273 break;
7275 return data_section;
7276 case SECCAT_DATA_REL:
7277 sname = ".data.rel";
7278 break;
7279 case SECCAT_DATA_REL_LOCAL:
7280 sname = ".data.rel.local";
7281 break;
7282 case SECCAT_DATA_REL_RO:
7283 sname = ".data.rel.ro";
7284 break;
7285 case SECCAT_DATA_REL_RO_LOCAL:
7286 sname = ".data.rel.ro.local";
7287 break;
7288 case SECCAT_SDATA:
7289 sname = ".sdata";
7290 break;
7291 case SECCAT_TDATA:
7292 sname = ".tdata";
7293 break;
7294 case SECCAT_BSS:
7295 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7297 sname = ".noinit";
7298 break;
7300 if (bss_section)
7301 return bss_section;
7302 sname = ".bss";
7303 break;
7304 case SECCAT_SBSS:
7305 sname = ".sbss";
7306 break;
7307 case SECCAT_TBSS:
7308 sname = ".tbss";
7309 break;
7310 default:
7311 gcc_unreachable ();
7314 return get_named_section (decl, sname, reloc);
7317 /* Construct a unique section name based on the decl name and the
7318 categorization performed above. */
7320 void
7321 default_unique_section (tree decl, int reloc)
7323 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
7324 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
7325 const char *prefix, *name, *linkonce;
7326 char *string;
7327 tree id;
7329 switch (categorize_decl_for_section (decl, reloc))
7331 case SECCAT_TEXT:
7332 prefix = one_only ? ".t" : ".text";
7333 break;
7334 case SECCAT_RODATA:
7335 case SECCAT_RODATA_MERGE_STR:
7336 case SECCAT_RODATA_MERGE_STR_INIT:
7337 case SECCAT_RODATA_MERGE_CONST:
7338 prefix = one_only ? ".r" : ".rodata";
7339 break;
7340 case SECCAT_SRODATA:
7341 prefix = one_only ? ".s2" : ".sdata2";
7342 break;
7343 case SECCAT_DATA:
7344 prefix = one_only ? ".d" : ".data";
7345 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7347 prefix = one_only ? ".p" : ".persistent";
7348 break;
7350 break;
7351 case SECCAT_DATA_REL:
7352 prefix = one_only ? ".d.rel" : ".data.rel";
7353 break;
7354 case SECCAT_DATA_REL_LOCAL:
7355 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
7356 break;
7357 case SECCAT_DATA_REL_RO:
7358 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
7359 break;
7360 case SECCAT_DATA_REL_RO_LOCAL:
7361 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
7362 break;
7363 case SECCAT_SDATA:
7364 prefix = one_only ? ".s" : ".sdata";
7365 break;
7366 case SECCAT_BSS:
7367 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7369 prefix = one_only ? ".n" : ".noinit";
7370 break;
7372 prefix = one_only ? ".b" : ".bss";
7373 break;
7374 case SECCAT_SBSS:
7375 prefix = one_only ? ".sb" : ".sbss";
7376 break;
7377 case SECCAT_TDATA:
7378 prefix = one_only ? ".td" : ".tdata";
7379 break;
7380 case SECCAT_TBSS:
7381 prefix = one_only ? ".tb" : ".tbss";
7382 break;
7383 default:
7384 gcc_unreachable ();
7387 id = DECL_ASSEMBLER_NAME (decl);
7388 ultimate_transparent_alias_target (&id);
7389 name = IDENTIFIER_POINTER (id);
7390 name = targetm.strip_name_encoding (name);
7392 /* If we're using one_only, then there needs to be a .gnu.linkonce
7393 prefix to the section name. */
7394 linkonce = one_only ? ".gnu.linkonce" : "";
7396 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
7398 set_decl_section_name (decl, string);
7401 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
7403 static int
7404 compute_reloc_for_rtx_1 (const_rtx x)
7406 switch (GET_CODE (x))
7408 case SYMBOL_REF:
7409 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
7410 case LABEL_REF:
7411 return 1;
7412 default:
7413 return 0;
7417 /* Like compute_reloc_for_constant, except for an RTX. The return value
7418 is a mask for which bit 1 indicates a global relocation, and bit 0
7419 indicates a local relocation. Used by default_select_rtx_section
7420 and default_elf_select_rtx_section. */
7422 static int
7423 compute_reloc_for_rtx (const_rtx x)
7425 switch (GET_CODE (x))
7427 case SYMBOL_REF:
7428 case LABEL_REF:
7429 return compute_reloc_for_rtx_1 (x);
7431 case CONST:
7433 int reloc = 0;
7434 subrtx_iterator::array_type array;
7435 FOR_EACH_SUBRTX (iter, array, x, ALL)
7436 reloc |= compute_reloc_for_rtx_1 (*iter);
7437 return reloc;
7440 default:
7441 return 0;
7445 section *
7446 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
7447 rtx x,
7448 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7450 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
7451 return data_section;
7452 else
7453 return readonly_data_section;
7456 section *
7457 default_elf_select_rtx_section (machine_mode mode, rtx x,
7458 unsigned HOST_WIDE_INT align)
7460 int reloc = compute_reloc_for_rtx (x);
7462 /* ??? Handle small data here somehow. */
7464 if (reloc & targetm.asm_out.reloc_rw_mask ())
7466 if (reloc == 1)
7467 return get_named_section (NULL, ".data.rel.ro.local", 1);
7468 else
7469 return get_named_section (NULL, ".data.rel.ro", 3);
7472 return mergeable_constant_section (mode, align, 0);
7475 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
7477 void
7478 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
7480 rtx symbol;
7481 int flags;
7483 /* Careful not to prod global register variables. */
7484 if (!MEM_P (rtl))
7485 return;
7486 symbol = XEXP (rtl, 0);
7487 if (GET_CODE (symbol) != SYMBOL_REF)
7488 return;
7490 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
7491 if (TREE_CODE (decl) == FUNCTION_DECL)
7492 flags |= SYMBOL_FLAG_FUNCTION;
7493 if (targetm.binds_local_p (decl))
7494 flags |= SYMBOL_FLAG_LOCAL;
7495 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7496 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
7497 else if (targetm.in_small_data_p (decl))
7498 flags |= SYMBOL_FLAG_SMALL;
7499 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
7500 being PUBLIC, the thing *must* be defined in this translation unit.
7501 Prevent this buglet from being propagated into rtl code as well. */
7502 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
7503 flags |= SYMBOL_FLAG_EXTERNAL;
7505 SYMBOL_REF_FLAGS (symbol) = flags;
7508 /* By default, we do nothing for encode_section_info, so we need not
7509 do anything but discard the '*' marker. */
7511 const char *
7512 default_strip_name_encoding (const char *str)
7514 return str + (*str == '*');
7517 #ifdef ASM_OUTPUT_DEF
7518 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
7519 anchor relative to ".", the current section position. */
7521 void
7522 default_asm_output_anchor (rtx symbol)
7524 gcc_checking_assert (TARGET_SUPPORTS_ALIASES);
7526 char buffer[100];
7528 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7529 SYMBOL_REF_BLOCK_OFFSET (symbol));
7530 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7532 #endif
7534 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
7536 bool
7537 default_use_anchors_for_symbol_p (const_rtx symbol)
7539 tree decl;
7540 section *sect = SYMBOL_REF_BLOCK (symbol)->sect;
7542 /* This function should only be called with non-zero SYMBOL_REF_BLOCK,
7543 furthermore get_block_for_section should not create object blocks
7544 for mergeable sections. */
7545 gcc_checking_assert (sect && !(sect->common.flags & SECTION_MERGE));
7547 /* Don't use anchors for small data sections. The small data register
7548 acts as an anchor for such sections. */
7549 if (sect->common.flags & SECTION_SMALL)
7550 return false;
7552 decl = SYMBOL_REF_DECL (symbol);
7553 if (decl && DECL_P (decl))
7555 /* Don't use section anchors for decls that might be defined or
7556 usurped by other modules. */
7557 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
7558 return false;
7560 /* Don't use section anchors for decls that will be placed in a
7561 small data section. */
7562 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
7563 one above. The problem is that we only use SECTION_SMALL for
7564 sections that should be marked as small in the section directive. */
7565 if (targetm.in_small_data_p (decl))
7566 return false;
7568 /* Don't use section anchors for decls that won't fit inside a single
7569 anchor range to reduce the amount of instructions required to refer
7570 to the entire declaration. */
7571 if (DECL_SIZE_UNIT (decl) == NULL_TREE
7572 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
7573 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
7574 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
7575 return false;
7578 return true;
7581 /* Return true when RESOLUTION indicate that symbol will be bound to the
7582 definition provided by current .o file. */
7584 static bool
7585 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
7587 return (resolution == LDPR_PREVAILING_DEF
7588 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7589 || resolution == LDPR_PREVAILING_DEF_IRONLY);
7592 /* Return true when RESOLUTION indicate that symbol will be bound locally
7593 within current executable or DSO. */
7595 static bool
7596 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7598 return (resolution == LDPR_PREVAILING_DEF
7599 || resolution == LDPR_PREVAILING_DEF_IRONLY
7600 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7601 || resolution == LDPR_PREEMPTED_REG
7602 || resolution == LDPR_PREEMPTED_IR
7603 || resolution == LDPR_RESOLVED_IR
7604 || resolution == LDPR_RESOLVED_EXEC);
7607 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7608 uninitialized common symbol in the executable will still be defined
7609 (through COPY relocation) in the executable. */
7611 bool
7612 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7613 bool extern_protected_data, bool common_local_p)
7615 /* A non-decl is an entry in the constant pool. */
7616 if (!DECL_P (exp))
7617 return true;
7619 /* Weakrefs may not bind locally, even though the weakref itself is always
7620 static and therefore local. Similarly, the resolver for ifunc functions
7621 might resolve to a non-local function.
7622 FIXME: We can resolve the weakref case more curefuly by looking at the
7623 weakref alias. */
7624 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7625 || (!targetm.ifunc_ref_local_ok ()
7626 && TREE_CODE (exp) == FUNCTION_DECL
7627 && cgraph_node::get (exp)
7628 && cgraph_node::get (exp)->ifunc_resolver))
7629 return false;
7631 /* Static variables are always local. */
7632 if (! TREE_PUBLIC (exp))
7633 return true;
7635 /* With resolution file in hand, take look into resolutions.
7636 We can't just return true for resolved_locally symbols,
7637 because dynamic linking might overwrite symbols
7638 in shared libraries. */
7639 bool resolved_locally = false;
7641 bool uninited_common = (DECL_COMMON (exp)
7642 && (DECL_INITIAL (exp) == NULL
7643 || (!in_lto_p
7644 && DECL_INITIAL (exp) == error_mark_node)));
7646 /* A non-external variable is defined locally only if it isn't
7647 uninitialized COMMON variable or common_local_p is true. */
7648 bool defined_locally = (!DECL_EXTERNAL (exp)
7649 && (!uninited_common || common_local_p));
7650 if (symtab_node *node = symtab_node::get (exp))
7652 if (node->in_other_partition)
7653 defined_locally = true;
7654 if (node->can_be_discarded_p ())
7656 else if (resolution_to_local_definition_p (node->resolution))
7657 defined_locally = resolved_locally = true;
7658 else if (resolution_local_p (node->resolution))
7659 resolved_locally = true;
7661 if (defined_locally && weak_dominate && !shlib)
7662 resolved_locally = true;
7664 /* Undefined weak symbols are never defined locally. */
7665 if (DECL_WEAK (exp) && !defined_locally)
7666 return false;
7668 /* A symbol is local if the user has said explicitly that it will be,
7669 or if we have a definition for the symbol. We cannot infer visibility
7670 for undefined symbols. */
7671 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7672 && (TREE_CODE (exp) == FUNCTION_DECL
7673 || !extern_protected_data
7674 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7675 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7676 return true;
7678 /* If PIC, then assume that any global name can be overridden by
7679 symbols resolved from other modules. */
7680 if (shlib)
7681 return false;
7683 /* Variables defined outside this object might not be local. */
7684 if (DECL_EXTERNAL (exp) && !resolved_locally)
7685 return false;
7687 /* Non-dominant weak symbols are not defined locally. */
7688 if (DECL_WEAK (exp) && !resolved_locally)
7689 return false;
7691 /* Uninitialized COMMON variable may be unified with symbols
7692 resolved from other modules. */
7693 if (uninited_common && !resolved_locally)
7694 return false;
7696 /* Otherwise we're left with initialized (or non-common) global data
7697 which is of necessity defined locally. */
7698 return true;
7701 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7702 wrt cross-module name binding. */
7704 bool
7705 default_binds_local_p (const_tree exp)
7707 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7710 /* Similar to default_binds_local_p, but common symbol may be local and
7711 extern protected data is non-local. */
7713 bool
7714 default_binds_local_p_2 (const_tree exp)
7716 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7717 !flag_pic);
7720 bool
7721 default_binds_local_p_1 (const_tree exp, int shlib)
7723 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7726 /* Return true when references to DECL must bind to current definition in
7727 final executable.
7729 The condition is usually equivalent to whether the function binds to the
7730 current module (shared library or executable), that is to binds_local_p.
7731 We use this fact to avoid need for another target hook and implement
7732 the logic using binds_local_p and just special cases where
7733 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7734 the weak definitions (that can be overwritten at linktime by other
7735 definition from different object file) and when resolution info is available
7736 we simply use the knowledge passed to us by linker plugin. */
7737 bool
7738 decl_binds_to_current_def_p (const_tree decl)
7740 gcc_assert (DECL_P (decl));
7741 if (!targetm.binds_local_p (decl))
7742 return false;
7743 if (!TREE_PUBLIC (decl))
7744 return true;
7746 /* When resolution is available, just use it. */
7747 if (symtab_node *node = symtab_node::get (decl))
7749 if (node->resolution != LDPR_UNKNOWN
7750 && !node->can_be_discarded_p ())
7751 return resolution_to_local_definition_p (node->resolution);
7754 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7755 binds locally but still can be overwritten), DECL_COMMON (can be merged
7756 with a non-common definition somewhere in the same module) or
7757 DECL_EXTERNAL.
7758 This rely on fact that binds_local_p behave as decl_replaceable_p
7759 for all other declaration types. */
7760 if (DECL_WEAK (decl))
7761 return false;
7762 if (DECL_COMMON (decl)
7763 && (DECL_INITIAL (decl) == NULL
7764 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7765 return false;
7766 if (DECL_EXTERNAL (decl))
7767 return false;
7768 return true;
7771 /* A replaceable function or variable is one which may be replaced
7772 at link-time with an entirely different definition, provided that the
7773 replacement has the same type. For example, functions declared
7774 with __attribute__((weak)) on most systems are replaceable.
7775 If SEMANTIC_INTERPOSITION_P is false allow interposition only on
7776 symbols explicitly declared weak.
7778 COMDAT functions are not replaceable, since all definitions of the
7779 function must be equivalent. It is important that COMDAT functions
7780 not be treated as replaceable so that use of C++ template
7781 instantiations is not penalized. */
7783 bool
7784 decl_replaceable_p (tree decl, bool semantic_interposition_p)
7786 gcc_assert (DECL_P (decl));
7787 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7788 return false;
7789 if (!semantic_interposition_p
7790 && !DECL_WEAK (decl))
7791 return false;
7792 return !decl_binds_to_current_def_p (decl);
7795 /* Default function to output code that will globalize a label. A
7796 target must define GLOBAL_ASM_OP or provide its own function to
7797 globalize a label. */
7798 #ifdef GLOBAL_ASM_OP
7799 void
7800 default_globalize_label (FILE * stream, const char *name)
7802 fputs (GLOBAL_ASM_OP, stream);
7803 assemble_name (stream, name);
7804 putc ('\n', stream);
7806 #endif /* GLOBAL_ASM_OP */
7808 /* Default function to output code that will globalize a declaration. */
7809 void
7810 default_globalize_decl_name (FILE * stream, tree decl)
7812 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7813 targetm.asm_out.globalize_label (stream, name);
7816 /* Default function to output a label for unwind information. The
7817 default is to do nothing. A target that needs nonlocal labels for
7818 unwind information must provide its own function to do this. */
7819 void
7820 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7821 tree decl ATTRIBUTE_UNUSED,
7822 int for_eh ATTRIBUTE_UNUSED,
7823 int empty ATTRIBUTE_UNUSED)
7827 /* Default function to output a label to divide up the exception table.
7828 The default is to do nothing. A target that needs/wants to divide
7829 up the table must provide it's own function to do this. */
7830 void
7831 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7835 /* This is how to output an internal numbered label where PREFIX is
7836 the class of label and LABELNO is the number within the class. */
7838 void
7839 default_generate_internal_label (char *buf, const char *prefix,
7840 unsigned long labelno)
7842 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7845 /* This is how to output an internal numbered label where PREFIX is
7846 the class of label and LABELNO is the number within the class. */
7848 void
7849 default_internal_label (FILE *stream, const char *prefix,
7850 unsigned long labelno)
7852 char *const buf = (char *) alloca (40 + strlen (prefix));
7853 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7854 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7858 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7860 void
7861 default_asm_declare_constant_name (FILE *file, const char *name,
7862 const_tree exp ATTRIBUTE_UNUSED,
7863 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7865 assemble_label (file, name);
7868 /* This is the default behavior at the beginning of a file. It's
7869 controlled by two other target-hook toggles. */
7870 void
7871 default_file_start (void)
7873 if (targetm.asm_file_start_app_off
7874 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7875 fputs (ASM_APP_OFF, asm_out_file);
7877 if (targetm.asm_file_start_file_directive)
7879 /* LTO produced units have no meaningful main_input_filename. */
7880 if (in_lto_p)
7881 output_file_directive (asm_out_file, "<artificial>");
7882 else
7883 output_file_directive (asm_out_file, main_input_filename);
7887 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7888 which emits a special section directive used to indicate whether or
7889 not this object file needs an executable stack. This is primarily
7890 a GNU extension to ELF but could be used on other targets. */
7892 int trampolines_created;
7894 void
7895 file_end_indicate_exec_stack (void)
7897 unsigned int flags = SECTION_DEBUG;
7898 if (trampolines_created)
7899 flags |= SECTION_CODE;
7901 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7904 /* Emit a special section directive to indicate that this object file
7905 was compiled with -fsplit-stack. This is used to let the linker
7906 detect calls between split-stack code and non-split-stack code, so
7907 that it can modify the split-stack code to allocate a sufficiently
7908 large stack. We emit another special section if there are any
7909 functions in this file which have the no_split_stack attribute, to
7910 prevent the linker from warning about being unable to convert the
7911 functions if they call non-split-stack code. */
7913 void
7914 file_end_indicate_split_stack (void)
7916 if (flag_split_stack)
7918 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7919 NULL));
7920 if (saw_no_split_stack)
7921 switch_to_section (get_section (".note.GNU-no-split-stack",
7922 SECTION_DEBUG, NULL));
7926 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7927 a get_unnamed_section callback. */
7929 void
7930 output_section_asm_op (const char *directive)
7932 fprintf (asm_out_file, "%s\n", directive);
7935 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7936 the current section is NEW_SECTION. */
7938 void
7939 switch_to_section (section *new_section, tree decl)
7941 bool retain_p;
7942 if ((new_section->common.flags & SECTION_NAMED)
7943 && decl != nullptr
7944 && DECL_P (decl)
7945 && ((retain_p = !!lookup_attribute ("retain",
7946 DECL_ATTRIBUTES (decl)))
7947 != !!(new_section->common.flags & SECTION_RETAIN)))
7949 /* If the SECTION_RETAIN bit doesn't match, switch to a new
7950 section. */
7951 tree used_decl, no_used_decl;
7953 if (retain_p)
7955 new_section->common.flags |= SECTION_RETAIN;
7956 used_decl = decl;
7957 no_used_decl = new_section->named.decl;
7959 else
7961 new_section->common.flags &= ~(SECTION_RETAIN
7962 | SECTION_DECLARED);
7963 used_decl = new_section->named.decl;
7964 no_used_decl = decl;
7966 if (no_used_decl != used_decl)
7968 warning (OPT_Wattributes,
7969 "%+qD without %<retain%> attribute and %qD with "
7970 "%<retain%> attribute are placed in a section with "
7971 "the same name", no_used_decl, used_decl);
7972 inform (DECL_SOURCE_LOCATION (used_decl),
7973 "%qD was declared here", used_decl);
7976 else if (in_section == new_section)
7977 return;
7979 in_section = new_section;
7981 switch (SECTION_STYLE (new_section))
7983 case SECTION_NAMED:
7984 targetm.asm_out.named_section (new_section->named.name,
7985 new_section->named.common.flags,
7986 new_section->named.decl);
7987 break;
7989 case SECTION_UNNAMED:
7990 new_section->unnamed.callback (new_section->unnamed.data);
7991 break;
7993 case SECTION_NOSWITCH:
7994 gcc_unreachable ();
7995 break;
7998 new_section->common.flags |= SECTION_DECLARED;
8001 /* If block symbol SYMBOL has not yet been assigned an offset, place
8002 it at the end of its block. */
8004 void
8005 place_block_symbol (rtx symbol)
8007 unsigned HOST_WIDE_INT size, mask, offset;
8008 class constant_descriptor_rtx *desc;
8009 unsigned int alignment;
8010 struct object_block *block;
8011 tree decl;
8013 gcc_assert (SYMBOL_REF_BLOCK (symbol));
8014 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
8015 return;
8017 /* Work out the symbol's size and alignment. */
8018 if (CONSTANT_POOL_ADDRESS_P (symbol))
8020 desc = SYMBOL_REF_CONSTANT (symbol);
8021 alignment = desc->align;
8022 size = GET_MODE_SIZE (desc->mode);
8024 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
8026 decl = SYMBOL_REF_DECL (symbol);
8027 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
8028 alignment = DECL_ALIGN (decl);
8029 size = get_constant_size (DECL_INITIAL (decl));
8030 if ((flag_sanitize & SANITIZE_ADDRESS)
8031 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8032 && asan_protect_global (DECL_INITIAL (decl)))
8034 size += asan_red_zone_size (size);
8035 alignment = MAX (alignment,
8036 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
8039 else
8041 struct symtab_node *snode;
8042 decl = SYMBOL_REF_DECL (symbol);
8044 snode = symtab_node::get (decl);
8045 if (snode->alias)
8047 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
8049 gcc_assert (MEM_P (target)
8050 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
8051 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
8052 target = XEXP (target, 0);
8053 place_block_symbol (target);
8054 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
8055 return;
8057 alignment = get_variable_align (decl);
8058 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8059 if ((flag_sanitize & SANITIZE_ADDRESS)
8060 && asan_protect_global (decl))
8062 size += asan_red_zone_size (size);
8063 alignment = MAX (alignment,
8064 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
8068 /* Calculate the object's offset from the start of the block. */
8069 block = SYMBOL_REF_BLOCK (symbol);
8070 mask = alignment / BITS_PER_UNIT - 1;
8071 offset = (block->size + mask) & ~mask;
8072 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
8074 /* Record the block's new alignment and size. */
8075 block->alignment = MAX (block->alignment, alignment);
8076 block->size = offset + size;
8078 vec_safe_push (block->objects, symbol);
8081 /* Return the anchor that should be used to address byte offset OFFSET
8082 from the first object in BLOCK. MODEL is the TLS model used
8083 to access it. */
8086 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
8087 enum tls_model model)
8089 char label[100];
8090 unsigned int begin, middle, end;
8091 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
8092 rtx anchor;
8094 /* Work out the anchor's offset. Use an offset of 0 for the first
8095 anchor so that we don't pessimize the case where we take the address
8096 of a variable at the beginning of the block. This is particularly
8097 useful when a block has only one variable assigned to it.
8099 We try to place anchors RANGE bytes apart, so there can then be
8100 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
8101 a ptr_mode offset. With some target settings, the lowest such
8102 anchor might be out of range for the lowest ptr_mode offset;
8103 likewise the highest anchor for the highest offset. Use anchors
8104 at the extreme ends of the ptr_mode range in such cases.
8106 All arithmetic uses unsigned integers in order to avoid
8107 signed overflow. */
8108 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
8109 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
8110 range = max_offset - min_offset + 1;
8111 if (range == 0)
8112 offset = 0;
8113 else
8115 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
8116 if (offset < 0)
8118 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
8119 delta -= delta % range;
8120 if (delta > bias)
8121 delta = bias;
8122 offset = (HOST_WIDE_INT) (-delta);
8124 else
8126 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
8127 delta -= delta % range;
8128 if (delta > bias - 1)
8129 delta = bias - 1;
8130 offset = (HOST_WIDE_INT) delta;
8134 /* Do a binary search to see if there's already an anchor we can use.
8135 Set BEGIN to the new anchor's index if not. */
8136 begin = 0;
8137 end = vec_safe_length (block->anchors);
8138 while (begin != end)
8140 middle = (end + begin) / 2;
8141 anchor = (*block->anchors)[middle];
8142 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
8143 end = middle;
8144 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
8145 begin = middle + 1;
8146 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
8147 end = middle;
8148 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
8149 begin = middle + 1;
8150 else
8151 return anchor;
8154 /* Create a new anchor with a unique label. */
8155 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
8156 anchor = create_block_symbol (ggc_strdup (label), block, offset);
8157 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
8158 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
8160 /* Insert it at index BEGIN. */
8161 vec_safe_insert (block->anchors, begin, anchor);
8162 return anchor;
8165 /* Output the objects in BLOCK. */
8167 static void
8168 output_object_block (struct object_block *block)
8170 class constant_descriptor_rtx *desc;
8171 unsigned int i;
8172 HOST_WIDE_INT offset;
8173 tree decl;
8174 rtx symbol;
8176 if (!block->objects)
8177 return;
8179 /* Switch to the section and make sure that the first byte is
8180 suitably aligned. */
8181 /* Special case VTV comdat sections similar to assemble_variable. */
8182 if (SECTION_STYLE (block->sect) == SECTION_NAMED
8183 && block->sect->named.name
8184 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
8185 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
8186 else
8187 switch_to_section (block->sect, SYMBOL_REF_DECL ((*block->objects)[0]));
8189 gcc_checking_assert (!(block->sect->common.flags & SECTION_MERGE));
8190 assemble_align (block->alignment);
8192 /* Define the values of all anchors relative to the current section
8193 position. */
8194 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
8195 targetm.asm_out.output_anchor (symbol);
8197 /* Output the objects themselves. */
8198 offset = 0;
8199 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
8201 /* Move to the object's offset, padding with zeros if necessary. */
8202 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
8203 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
8204 if (CONSTANT_POOL_ADDRESS_P (symbol))
8206 desc = SYMBOL_REF_CONSTANT (symbol);
8207 /* Pass 1 for align as we have already laid out everything in the block.
8208 So aligning shouldn't be necessary. */
8209 output_constant_pool_1 (desc, 1);
8210 offset += GET_MODE_SIZE (desc->mode);
8212 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
8214 HOST_WIDE_INT size;
8215 decl = SYMBOL_REF_DECL (symbol);
8216 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
8217 DECL_ALIGN (decl), false);
8219 size = get_constant_size (DECL_INITIAL (decl));
8220 offset += size;
8221 if ((flag_sanitize & SANITIZE_ADDRESS)
8222 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8223 && asan_protect_global (DECL_INITIAL (decl)))
8225 size = asan_red_zone_size (size);
8226 assemble_zeros (size);
8227 offset += size;
8230 else
8232 HOST_WIDE_INT size;
8233 decl = SYMBOL_REF_DECL (symbol);
8234 assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
8235 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8236 offset += size;
8237 if ((flag_sanitize & SANITIZE_ADDRESS)
8238 && asan_protect_global (decl))
8240 size = asan_red_zone_size (size);
8241 assemble_zeros (size);
8242 offset += size;
8248 /* A callback for qsort to compare object_blocks. */
8250 static int
8251 output_object_block_compare (const void *x, const void *y)
8253 object_block *p1 = *(object_block * const*)x;
8254 object_block *p2 = *(object_block * const*)y;
8256 if (p1->sect->common.flags & SECTION_NAMED
8257 && !(p2->sect->common.flags & SECTION_NAMED))
8258 return 1;
8260 if (!(p1->sect->common.flags & SECTION_NAMED)
8261 && p2->sect->common.flags & SECTION_NAMED)
8262 return -1;
8264 if (p1->sect->common.flags & SECTION_NAMED
8265 && p2->sect->common.flags & SECTION_NAMED)
8266 return strcmp (p1->sect->named.name, p2->sect->named.name);
8268 unsigned f1 = p1->sect->common.flags;
8269 unsigned f2 = p2->sect->common.flags;
8270 if (f1 == f2)
8271 return 0;
8272 return f1 < f2 ? -1 : 1;
8275 /* Output the definitions of all object_blocks. */
8277 void
8278 output_object_blocks (void)
8280 vec<object_block *, va_heap> v;
8281 v.create (object_block_htab->elements ());
8282 object_block *obj;
8283 hash_table<object_block_hasher>::iterator hi;
8285 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
8286 v.quick_push (obj);
8288 /* Sort them in order to output them in a deterministic manner,
8289 otherwise we may get .rodata sections in different orders with
8290 and without -g. */
8291 v.qsort (output_object_block_compare);
8292 unsigned i;
8293 FOR_EACH_VEC_ELT (v, i, obj)
8294 output_object_block (obj);
8296 v.release ();
8299 /* This function provides a possible implementation of the
8300 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
8301 by -frecord-gcc-switches it creates a new mergeable, string section in the
8302 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
8303 contains the switches in ASCII format.
8305 FIXME: This code does not correctly handle double quote characters
8306 that appear inside strings, (it strips them rather than preserving them).
8307 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
8308 characters - instead it treats them as sub-string separators. Since
8309 we want to emit NUL strings terminators into the object file we have to use
8310 ASM_OUTPUT_SKIP. */
8312 void
8313 elf_record_gcc_switches (const char *options)
8315 section *sec = get_section (targetm.asm_out.record_gcc_switches_section,
8316 SECTION_DEBUG | SECTION_MERGE
8317 | SECTION_STRINGS | (SECTION_ENTSIZE & 1), NULL);
8318 switch_to_section (sec);
8319 ASM_OUTPUT_ASCII (asm_out_file, options, strlen (options) + 1);
8322 /* Emit text to declare externally defined symbols. It is needed to
8323 properly support non-default visibility. */
8324 void
8325 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
8326 tree decl,
8327 const char *name ATTRIBUTE_UNUSED)
8329 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8330 set in order to avoid putting out names that are never really
8331 used. Always output visibility specified in the source. */
8332 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
8333 && (DECL_VISIBILITY_SPECIFIED (decl)
8334 || targetm.binds_local_p (decl)))
8335 maybe_assemble_visibility (decl);
8338 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8340 void
8341 default_asm_output_source_filename (FILE *file, const char *name)
8343 #ifdef ASM_OUTPUT_SOURCE_FILENAME
8344 ASM_OUTPUT_SOURCE_FILENAME (file, name);
8345 #else
8346 fprintf (file, "\t.file\t");
8347 output_quoted_string (file, name);
8348 putc ('\n', file);
8349 #endif
8352 /* Output a file name in the form wanted by System V. */
8354 void
8355 output_file_directive (FILE *asm_file, const char *input_name)
8357 int len;
8358 const char *na;
8360 if (input_name == NULL)
8361 input_name = "<stdin>";
8362 else
8363 input_name = remap_debug_filename (input_name);
8365 len = strlen (input_name);
8366 na = input_name + len;
8368 /* NA gets INPUT_NAME sans directory names. */
8369 while (na > input_name)
8371 if (IS_DIR_SEPARATOR (na[-1]))
8372 break;
8373 na--;
8376 targetm.asm_out.output_source_filename (asm_file, na);
8379 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
8380 EXP. */
8382 make_debug_expr_from_rtl (const_rtx exp)
8384 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
8385 machine_mode mode = GET_MODE (exp);
8386 rtx dval;
8388 DECL_ARTIFICIAL (ddecl) = 1;
8389 if (REG_P (exp) && REG_EXPR (exp))
8390 type = TREE_TYPE (REG_EXPR (exp));
8391 else if (MEM_P (exp) && MEM_EXPR (exp))
8392 type = TREE_TYPE (MEM_EXPR (exp));
8393 else
8394 type = NULL_TREE;
8395 if (type && TYPE_MODE (type) == mode)
8396 TREE_TYPE (ddecl) = type;
8397 else
8398 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
8399 SET_DECL_MODE (ddecl, mode);
8400 dval = gen_rtx_DEBUG_EXPR (mode);
8401 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
8402 SET_DECL_RTL (ddecl, dval);
8403 return dval;
8406 #ifdef ELF_ASCII_ESCAPES
8407 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
8409 void
8410 default_elf_asm_output_limited_string (FILE *f, const char *s)
8412 int escape;
8413 unsigned char c;
8415 fputs (STRING_ASM_OP, f);
8416 putc ('"', f);
8417 while (*s != '\0')
8419 c = *s;
8420 escape = ELF_ASCII_ESCAPES[c];
8421 switch (escape)
8423 case 0:
8424 putc (c, f);
8425 break;
8426 case 1:
8427 putc ('\\', f);
8428 putc ('0'+((c>>6)&7), f);
8429 putc ('0'+((c>>3)&7), f);
8430 putc ('0'+(c&7), f);
8431 break;
8432 default:
8433 putc ('\\', f);
8434 putc (escape, f);
8435 break;
8437 s++;
8439 putc ('\"', f);
8440 putc ('\n', f);
8443 /* Default ASM_OUTPUT_ASCII for ELF targets. */
8445 void
8446 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
8448 const char *limit = s + len;
8449 const char *last_null = NULL;
8450 unsigned bytes_in_chunk = 0;
8451 unsigned char c;
8452 int escape;
8454 for (; s < limit; s++)
8456 const char *p;
8458 if (bytes_in_chunk >= 60)
8460 putc ('\"', f);
8461 putc ('\n', f);
8462 bytes_in_chunk = 0;
8465 if (s > last_null)
8467 for (p = s; p < limit && *p != '\0'; p++)
8468 continue;
8469 last_null = p;
8471 else
8472 p = last_null;
8474 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
8476 if (bytes_in_chunk > 0)
8478 putc ('\"', f);
8479 putc ('\n', f);
8480 bytes_in_chunk = 0;
8483 default_elf_asm_output_limited_string (f, s);
8484 s = p;
8486 else
8488 if (bytes_in_chunk == 0)
8489 fputs (ASCII_DATA_ASM_OP "\"", f);
8491 c = *s;
8492 escape = ELF_ASCII_ESCAPES[c];
8493 switch (escape)
8495 case 0:
8496 putc (c, f);
8497 bytes_in_chunk++;
8498 break;
8499 case 1:
8500 putc ('\\', f);
8501 putc ('0'+((c>>6)&7), f);
8502 putc ('0'+((c>>3)&7), f);
8503 putc ('0'+(c&7), f);
8504 bytes_in_chunk += 4;
8505 break;
8506 default:
8507 putc ('\\', f);
8508 putc (escape, f);
8509 bytes_in_chunk += 2;
8510 break;
8516 if (bytes_in_chunk > 0)
8518 putc ('\"', f);
8519 putc ('\n', f);
8522 #endif
8524 static GTY(()) section *elf_init_array_section;
8525 static GTY(()) section *elf_fini_array_section;
8527 static section *
8528 get_elf_initfini_array_priority_section (int priority,
8529 bool constructor_p)
8531 section *sec;
8532 if (priority != DEFAULT_INIT_PRIORITY)
8534 char buf[18];
8535 sprintf (buf, "%s.%.5u",
8536 constructor_p ? ".init_array" : ".fini_array",
8537 priority);
8538 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8540 else
8542 if (constructor_p)
8544 if (elf_init_array_section == NULL)
8545 elf_init_array_section
8546 = get_section (".init_array",
8547 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8548 sec = elf_init_array_section;
8550 else
8552 if (elf_fini_array_section == NULL)
8553 elf_fini_array_section
8554 = get_section (".fini_array",
8555 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8556 sec = elf_fini_array_section;
8559 return sec;
8562 /* Use .init_array section for constructors. */
8564 void
8565 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8567 section *sec = get_elf_initfini_array_priority_section (priority,
8568 true);
8569 assemble_addr_to_section (symbol, sec);
8572 /* Use .fini_array section for destructors. */
8574 void
8575 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8577 section *sec = get_elf_initfini_array_priority_section (priority,
8578 false);
8579 assemble_addr_to_section (symbol, sec);
8582 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8584 This is a bit of a cheat. The real default is a no-op, but this
8585 hook is the default for all targets with a .ident directive. */
8587 void
8588 default_asm_output_ident_directive (const char *ident_str)
8590 const char *ident_asm_op = "\t.ident\t";
8592 /* If we are still in the front end, do not write out the string
8593 to asm_out_file. Instead, add a fake top-level asm statement.
8594 This allows the front ends to use this hook without actually
8595 writing to asm_out_file, to handle #ident or Pragma Ident. */
8596 if (symtab->state == PARSING)
8598 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8599 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8601 else
8602 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8605 /* Switch to a COMDAT section with COMDAT name of decl.
8607 FIXME: resolve_unique_section needs to deal better with
8608 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8609 that is fixed, this if-else statement can be replaced with
8610 a single call to "switch_to_section (sect)". */
8612 void
8613 switch_to_comdat_section (section *sect, tree decl)
8615 #if defined (OBJECT_FORMAT_ELF)
8616 targetm.asm_out.named_section (sect->named.name,
8617 sect->named.common.flags
8618 | SECTION_LINKONCE,
8619 decl);
8620 in_section = sect;
8621 #else
8622 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8623 Therefore the following check is used.
8624 In case a the target is PE or COFF a comdat group section
8625 is created, e.g. .vtable_map_vars$foo. The linker places
8626 everything in .vtable_map_vars at the end.
8628 A fix could be made in
8629 gcc/config/i386/winnt.cc: i386_pe_unique_section. */
8630 if (TARGET_PECOFF)
8632 char *name;
8634 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8635 name = ACONCAT ((sect->named.name, "$",
8636 IDENTIFIER_POINTER (decl), NULL));
8637 else
8638 name = ACONCAT ((sect->named.name, "$",
8639 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)),
8640 NULL));
8642 targetm.asm_out.named_section (name,
8643 sect->named.common.flags
8644 | SECTION_LINKONCE,
8645 decl);
8646 in_section = sect;
8648 else
8649 switch_to_section (sect);
8650 #endif
8653 /* This function ensures that vtable_map variables are not only
8654 in the comdat section, but that each variable has its own unique
8655 comdat name. Without this the variables end up in the same section
8656 with a single comdat name. */
8658 static void
8659 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8661 switch_to_comdat_section(sect, DECL_NAME (decl));
8664 #include "gt-varasm.h"