testsuite: localclass2 require LTO
[official-gcc.git] / gcc / varasm.c
blobada99940f65b98f171be9c5d369b50742abe234e
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "backend.h"
32 #include "target.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "predict.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "stringpool.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "varasm.h"
46 #include "flags.h"
47 #include "stmt.h"
48 #include "expr.h"
49 #include "expmed.h"
50 #include "optabs.h"
51 #include "output.h"
52 #include "langhooks.h"
53 #include "debug.h"
54 #include "common/common-target.h"
55 #include "stringpool.h"
56 #include "attribs.h"
57 #include "asan.h"
58 #include "rtl-iter.h"
59 #include "file-prefix-map.h" /* remap_debug_filename() */
60 #include "alloc-pool.h"
62 #ifdef XCOFF_DEBUGGING_INFO
63 #include "xcoffout.h" /* Needed for external data declarations. */
64 #endif
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 int 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 int 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 void *),
251 const void *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. */
283 section *
284 get_section (const char *name, unsigned int flags, tree decl,
285 bool not_existing)
287 section *sect, **slot;
289 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
290 INSERT);
291 flags |= SECTION_NAMED;
292 if (*slot == NULL)
294 sect = ggc_alloc<section> ();
295 sect->named.common.flags = flags;
296 sect->named.name = ggc_strdup (name);
297 sect->named.decl = decl;
298 *slot = sect;
300 else
302 if (not_existing)
303 internal_error ("Section already exists: %qs", name);
305 sect = *slot;
306 /* It is fine if one of the sections has SECTION_NOTYPE as long as
307 the other has none of the contrary flags (see the logic at the end
308 of default_section_type_flags, below). */
309 if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
310 && !((sect->common.flags | flags)
311 & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
312 | (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
314 sect->common.flags |= SECTION_NOTYPE;
315 flags |= SECTION_NOTYPE;
317 if ((sect->common.flags & ~SECTION_DECLARED) != flags
318 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
320 /* It is fine if one of the section flags is
321 SECTION_WRITE | SECTION_RELRO and the other has none of these
322 flags (i.e. read-only) in named sections and either the
323 section hasn't been declared yet or has been declared as writable.
324 In that case just make sure the resulting flags are
325 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
326 relocations. */
327 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
328 == (SECTION_WRITE | SECTION_RELRO)
329 && (sect->common.flags
330 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
331 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
332 && ((sect->common.flags & SECTION_DECLARED) == 0
333 || (sect->common.flags & SECTION_WRITE)))
335 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
336 return sect;
338 /* Sanity check user variables for flag changes. */
339 if (sect->named.decl != NULL
340 && DECL_P (sect->named.decl)
341 && decl != sect->named.decl)
343 if (decl != NULL && DECL_P (decl))
344 error ("%+qD causes a section type conflict with %qD",
345 decl, sect->named.decl);
346 else
347 error ("section type conflict with %qD", sect->named.decl);
348 inform (DECL_SOURCE_LOCATION (sect->named.decl),
349 "%qD was declared here", sect->named.decl);
351 else if (decl != NULL && DECL_P (decl))
352 error ("%+qD causes a section type conflict", decl);
353 else
354 error ("section type conflict");
355 /* Make sure we don't error about one section multiple times. */
356 sect->common.flags |= SECTION_OVERRIDE;
359 return sect;
362 /* Return true if the current compilation mode benefits from having
363 objects grouped into blocks. */
365 static bool
366 use_object_blocks_p (void)
368 return flag_section_anchors;
371 /* Return the object_block structure for section SECT. Create a new
372 structure if we haven't created one already. Return null if SECT
373 itself is null. Return also null for mergeable sections since
374 section anchors can't be used in mergeable sections anyway,
375 because the linker might move objects around, and using the
376 object blocks infrastructure in that case is both a waste and a
377 maintenance burden. */
379 static struct object_block *
380 get_block_for_section (section *sect)
382 struct object_block *block;
384 if (sect == NULL)
385 return NULL;
387 if (sect->common.flags & SECTION_MERGE)
388 return NULL;
390 object_block **slot
391 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
392 INSERT);
393 block = *slot;
394 if (block == NULL)
396 block = ggc_cleared_alloc<object_block> ();
397 block->sect = sect;
398 *slot = block;
400 return block;
403 /* Create a symbol with label LABEL and place it at byte offset
404 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
405 is not yet known. LABEL must be a garbage-collected string. */
407 static rtx
408 create_block_symbol (const char *label, struct object_block *block,
409 HOST_WIDE_INT offset)
411 rtx symbol;
412 unsigned int size;
414 /* Create the extended SYMBOL_REF. */
415 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
416 symbol = (rtx) ggc_internal_alloc (size);
418 /* Initialize the normal SYMBOL_REF fields. */
419 memset (symbol, 0, size);
420 PUT_CODE (symbol, SYMBOL_REF);
421 PUT_MODE (symbol, Pmode);
422 XSTR (symbol, 0) = label;
423 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
425 /* Initialize the block_symbol stuff. */
426 SYMBOL_REF_BLOCK (symbol) = block;
427 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
429 return symbol;
432 /* Return a section with a particular name and with whatever SECTION_*
433 flags section_type_flags deems appropriate. The name of the section
434 is taken from NAME if nonnull, otherwise it is taken from DECL's
435 DECL_SECTION_NAME. DECL is the decl associated with the section
436 (see the section comment for details) and RELOC is as for
437 section_type_flags. */
439 section *
440 get_named_section (tree decl, const char *name, int reloc)
442 unsigned int flags;
444 if (name == NULL)
446 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
447 name = DECL_SECTION_NAME (decl);
450 flags = targetm.section_type_flags (decl, name, reloc);
451 return get_section (name, flags, decl);
454 /* Worker for resolve_unique_section. */
456 static bool
457 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
459 n->implicit_section = true;
460 return false;
463 /* If required, set DECL_SECTION_NAME to a unique name. */
465 void
466 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
467 int flag_function_or_data_sections)
469 if (DECL_SECTION_NAME (decl) == NULL
470 && targetm_common.have_named_sections
471 && (flag_function_or_data_sections
472 || DECL_COMDAT_GROUP (decl)))
474 targetm.asm_out.unique_section (decl, reloc);
475 if (DECL_SECTION_NAME (decl))
476 symtab_node::get (decl)->call_for_symbol_and_aliases
477 (set_implicit_section, NULL, true);
481 #ifdef BSS_SECTION_ASM_OP
483 #ifdef ASM_OUTPUT_ALIGNED_BSS
485 /* Utility function for targets to use in implementing
486 ASM_OUTPUT_ALIGNED_BSS.
487 ??? It is believed that this function will work in most cases so such
488 support is localized here. */
490 static void
491 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
492 const char *name, unsigned HOST_WIDE_INT size,
493 int align)
495 switch_to_section (bss_section);
496 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
497 #ifdef ASM_DECLARE_OBJECT_NAME
498 last_assemble_variable_decl = decl;
499 ASM_DECLARE_OBJECT_NAME (file, name, decl);
500 #else
501 /* Standard thing is just output label for the object. */
502 ASM_OUTPUT_LABEL (file, name);
503 #endif /* ASM_DECLARE_OBJECT_NAME */
504 ASM_OUTPUT_SKIP (file, size ? size : 1);
507 #endif
509 #endif /* BSS_SECTION_ASM_OP */
511 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
512 /* Return the hot section for function DECL. Return text_section for
513 null DECLs. */
515 static section *
516 hot_function_section (tree decl)
518 if (decl != NULL_TREE
519 && DECL_SECTION_NAME (decl) != NULL
520 && targetm_common.have_named_sections)
521 return get_named_section (decl, NULL, 0);
522 else
523 return text_section;
525 #endif
527 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
528 is NULL.
530 When DECL_SECTION_NAME is non-NULL and it is implicit section and
531 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
532 concatenate the name with NAMED_SECTION_SUFFIX.
533 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
535 section *
536 get_named_text_section (tree decl,
537 const char *text_section_name,
538 const char *named_section_suffix)
540 if (decl && DECL_SECTION_NAME (decl))
542 if (named_section_suffix)
544 const char *dsn = DECL_SECTION_NAME (decl);
545 const char *stripped_name;
546 char *name, *buffer;
548 name = (char *) alloca (strlen (dsn) + 1);
549 memcpy (name, dsn,
550 strlen (dsn) + 1);
552 stripped_name = targetm.strip_name_encoding (name);
554 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
555 return get_named_section (decl, buffer, 0);
557 else if (symtab_node::get (decl)->implicit_section)
559 const char *name;
561 /* Do not try to split gnu_linkonce functions. This gets somewhat
562 slipperly. */
563 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
564 return NULL;
565 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
566 name = targetm.strip_name_encoding (name);
567 return get_named_section (decl, ACONCAT ((text_section_name, ".",
568 name, NULL)), 0);
570 else
571 return NULL;
573 return get_named_section (decl, text_section_name, 0);
576 /* Choose named function section based on its frequency. */
578 section *
579 default_function_section (tree decl, enum node_frequency freq,
580 bool startup, bool exit)
582 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
583 /* Old GNU linkers have buggy --gc-section support, which sometimes
584 results in .gcc_except_table* sections being garbage collected. */
585 if (decl
586 && symtab_node::get (decl)->implicit_section)
587 return NULL;
588 #endif
590 if (!flag_reorder_functions
591 || !targetm_common.have_named_sections)
592 return NULL;
593 /* Startup code should go to startup subsection unless it is
594 unlikely executed (this happens especially with function splitting
595 where we can split away unnecessary parts of static constructors. */
596 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
598 /* During LTO the tp_first_run profiling will naturally place all
599 initialization code first. Using separate section is counter-productive
600 because startup only code may call functions which are no longer
601 startup only. */
602 if (!in_lto_p
603 || !cgraph_node::get (decl)->tp_first_run
604 || !opt_for_fn (decl, flag_profile_reorder_functions))
605 return get_named_text_section (decl, ".text.startup", NULL);
606 else
607 return NULL;
610 /* Similarly for exit. */
611 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
612 return get_named_text_section (decl, ".text.exit", NULL);
614 /* Group cold functions together, similarly for hot code. */
615 switch (freq)
617 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
618 return get_named_text_section (decl, ".text.unlikely", NULL);
619 case NODE_FREQUENCY_HOT:
620 return get_named_text_section (decl, ".text.hot", NULL);
621 /* FALLTHRU */
622 default:
623 return NULL;
627 /* Return the section for function DECL.
629 If DECL is NULL_TREE, return the text section. We can be passed
630 NULL_TREE under some circumstances by dbxout.c at least.
632 If FORCE_COLD is true, return cold function section ignoring
633 the frequency info of cgraph_node. */
635 static section *
636 function_section_1 (tree decl, bool force_cold)
638 section *section = NULL;
639 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
640 bool startup = false, exit = false;
642 if (decl)
644 struct cgraph_node *node = cgraph_node::get (decl);
646 if (node)
648 freq = node->frequency;
649 startup = node->only_called_at_startup;
650 exit = node->only_called_at_exit;
653 if (force_cold)
654 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
656 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
657 if (decl != NULL_TREE
658 && DECL_SECTION_NAME (decl) != NULL)
660 if (targetm.asm_out.function_section)
661 section = targetm.asm_out.function_section (decl, freq,
662 startup, exit);
663 if (section)
664 return section;
665 return get_named_section (decl, NULL, 0);
667 else
668 return targetm.asm_out.select_section
669 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
670 symtab_node::get (decl)->definition_alignment ());
671 #else
672 if (targetm.asm_out.function_section)
673 section = targetm.asm_out.function_section (decl, freq, startup, exit);
674 if (section)
675 return section;
676 return hot_function_section (decl);
677 #endif
680 /* Return the section for function DECL.
682 If DECL is NULL_TREE, return the text section. We can be passed
683 NULL_TREE under some circumstances by dbxout.c at least. */
685 section *
686 function_section (tree decl)
688 /* Handle cases where function splitting code decides
689 to put function entry point into unlikely executed section
690 despite the fact that the function itself is not cold
691 (i.e. it is called rarely but contains a hot loop that is
692 better to live in hot subsection for the code locality). */
693 return function_section_1 (decl,
694 first_function_block_is_cold);
697 /* Return the section for the current function, take IN_COLD_SECTION_P
698 into account. */
700 section *
701 current_function_section (void)
703 return function_section_1 (current_function_decl, in_cold_section_p);
706 /* Tell assembler to switch to unlikely-to-be-executed text section. */
708 section *
709 unlikely_text_section (void)
711 return function_section_1 (current_function_decl, true);
714 /* When called within a function context, return true if the function
715 has been assigned a cold text section and if SECT is that section.
716 When called outside a function context, return true if SECT is the
717 default cold section. */
719 bool
720 unlikely_text_section_p (section *sect)
722 return sect == function_section_1 (current_function_decl, true);
725 /* Switch to the other function partition (if inside of hot section
726 into cold section, otherwise into the hot section). */
728 void
729 switch_to_other_text_partition (void)
731 in_cold_section_p = !in_cold_section_p;
732 switch_to_section (current_function_section ());
735 /* Return the read-only or relocated read-only data section
736 associated with function DECL. */
738 section *
739 default_function_rodata_section (tree decl, bool relocatable)
741 const char* sname;
742 unsigned int flags;
744 flags = 0;
746 if (relocatable)
748 sname = ".data.rel.ro.local";
749 flags = (SECTION_WRITE | SECTION_RELRO);
751 else
752 sname = ".rodata";
754 if (decl && DECL_SECTION_NAME (decl))
756 const char *name = DECL_SECTION_NAME (decl);
758 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
760 const char *dot;
761 size_t len;
762 char* rname;
764 dot = strchr (name + 1, '.');
765 if (!dot)
766 dot = name;
767 len = strlen (dot) + strlen (sname) + 1;
768 rname = (char *) alloca (len);
770 strcpy (rname, sname);
771 strcat (rname, dot);
772 return get_section (rname, (SECTION_LINKONCE | flags), decl);
774 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo or
775 .gnu.linkonce.d.rel.ro.local.foo if the jump table is relocatable. */
776 else if (DECL_COMDAT_GROUP (decl)
777 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
779 size_t len;
780 char *rname;
782 if (relocatable)
784 len = strlen (name) + strlen (".rel.ro.local") + 1;
785 rname = (char *) alloca (len);
787 strcpy (rname, ".gnu.linkonce.d.rel.ro.local");
788 strcat (rname, name + 15);
790 else
792 len = strlen (name) + 1;
793 rname = (char *) alloca (len);
795 memcpy (rname, name, len);
796 rname[14] = 'r';
798 return get_section (rname, (SECTION_LINKONCE | flags), decl);
800 /* For .text.foo we want to use .rodata.foo. */
801 else if (flag_function_sections && flag_data_sections
802 && strncmp (name, ".text.", 6) == 0)
804 size_t len = strlen (name) + 1;
805 char *rname = (char *) alloca (len + strlen (sname) - 5);
807 memcpy (rname, sname, strlen (sname));
808 memcpy (rname + strlen (sname), name + 5, len - 5);
809 return get_section (rname, flags, decl);
813 if (relocatable)
814 return get_section (sname, flags, decl);
815 else
816 return readonly_data_section;
819 /* Return the read-only data section associated with function DECL
820 for targets where that section should be always the single
821 readonly data section. */
823 section *
824 default_no_function_rodata_section (tree, bool)
826 return readonly_data_section;
829 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
831 static const char *
832 function_mergeable_rodata_prefix (void)
834 section *s = targetm.asm_out.function_rodata_section (current_function_decl,
835 false);
836 if (SECTION_STYLE (s) == SECTION_NAMED)
837 return s->named.name;
838 else
839 return targetm.asm_out.mergeable_rodata_prefix;
842 /* Return the section to use for string merging. */
844 static section *
845 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
846 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
847 unsigned int flags ATTRIBUTE_UNUSED)
849 HOST_WIDE_INT len;
851 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
852 && TREE_CODE (decl) == STRING_CST
853 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
854 && align <= 256
855 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
856 && TREE_STRING_LENGTH (decl) == len)
858 scalar_int_mode mode;
859 unsigned int modesize;
860 const char *str;
861 HOST_WIDE_INT i;
862 int j, unit;
863 const char *prefix = function_mergeable_rodata_prefix ();
864 char *name = (char *) alloca (strlen (prefix) + 30);
866 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
867 modesize = GET_MODE_BITSIZE (mode);
868 if (modesize >= 8 && modesize <= 256
869 && (modesize & (modesize - 1)) == 0)
871 if (align < modesize)
872 align = modesize;
874 if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
875 return readonly_data_section;
877 str = TREE_STRING_POINTER (decl);
878 unit = GET_MODE_SIZE (mode);
880 /* Check for embedded NUL characters. */
881 for (i = 0; i < len; i += unit)
883 for (j = 0; j < unit; j++)
884 if (str[i + j] != '\0')
885 break;
886 if (j == unit)
887 break;
889 if (i == len - unit || (unit == 1 && i == len))
891 sprintf (name, "%s.str%d.%d", prefix,
892 modesize / 8, (int) (align / 8));
893 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
894 return get_section (name, flags, NULL);
899 return readonly_data_section;
902 /* Return the section to use for constant merging. */
904 section *
905 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
906 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
907 unsigned int flags ATTRIBUTE_UNUSED)
909 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
910 && mode != VOIDmode
911 && mode != BLKmode
912 && known_le (GET_MODE_BITSIZE (mode), align)
913 && align >= 8
914 && align <= 256
915 && (align & (align - 1)) == 0
916 && (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
918 const char *prefix = function_mergeable_rodata_prefix ();
919 char *name = (char *) alloca (strlen (prefix) + 30);
921 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
922 flags |= (align / 8) | SECTION_MERGE;
923 return get_section (name, flags, NULL);
925 return readonly_data_section;
928 /* Given NAME, a putative register name, discard any customary prefixes. */
930 static const char *
931 strip_reg_name (const char *name)
933 #ifdef REGISTER_PREFIX
934 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
935 name += strlen (REGISTER_PREFIX);
936 #endif
937 if (name[0] == '%' || name[0] == '#')
938 name++;
939 return name;
942 /* The user has asked for a DECL to have a particular name. Set (or
943 change) it in such a way that we don't prefix an underscore to
944 it. */
945 void
946 set_user_assembler_name (tree decl, const char *name)
948 char *starred = (char *) alloca (strlen (name) + 2);
949 starred[0] = '*';
950 strcpy (starred + 1, name);
951 symtab->change_decl_assembler_name (decl, get_identifier (starred));
952 SET_DECL_RTL (decl, NULL_RTX);
955 /* Decode an `asm' spec for a declaration as a register name.
956 Return the register number, or -1 if nothing specified,
957 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
958 or -3 if ASMSPEC is `cc' and is not recognized,
959 or -4 if ASMSPEC is `memory' and is not recognized.
960 Accept an exact spelling or a decimal number.
961 Prefixes such as % are optional. */
964 decode_reg_name_and_count (const char *asmspec, int *pnregs)
966 /* Presume just one register is clobbered. */
967 *pnregs = 1;
969 if (asmspec != 0)
971 int i;
973 /* Get rid of confusing prefixes. */
974 asmspec = strip_reg_name (asmspec);
976 /* Allow a decimal number as a "register name". */
977 for (i = strlen (asmspec) - 1; i >= 0; i--)
978 if (! ISDIGIT (asmspec[i]))
979 break;
980 if (asmspec[0] != 0 && i < 0)
982 i = atoi (asmspec);
983 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
984 return i;
985 else
986 return -2;
989 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
990 if (reg_names[i][0]
991 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
992 return i;
994 #ifdef OVERLAPPING_REGISTER_NAMES
996 static const struct
998 const char *const name;
999 const int number;
1000 const int nregs;
1001 } table[] = OVERLAPPING_REGISTER_NAMES;
1003 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1004 if (table[i].name[0]
1005 && ! strcmp (asmspec, table[i].name))
1007 *pnregs = table[i].nregs;
1008 return table[i].number;
1011 #endif /* OVERLAPPING_REGISTER_NAMES */
1013 #ifdef ADDITIONAL_REGISTER_NAMES
1015 static const struct { const char *const name; const int number; } table[]
1016 = ADDITIONAL_REGISTER_NAMES;
1018 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1019 if (table[i].name[0]
1020 && ! strcmp (asmspec, table[i].name)
1021 && reg_names[table[i].number][0])
1022 return table[i].number;
1024 #endif /* ADDITIONAL_REGISTER_NAMES */
1026 if (!strcmp (asmspec, "memory"))
1027 return -4;
1029 if (!strcmp (asmspec, "cc"))
1030 return -3;
1032 return -2;
1035 return -1;
1039 decode_reg_name (const char *name)
1041 int count;
1042 return decode_reg_name_and_count (name, &count);
1046 /* Return true if DECL's initializer is suitable for a BSS section. */
1048 bool
1049 bss_initializer_p (const_tree decl, bool named)
1051 /* Do not put non-common constants into the .bss section, they belong in
1052 a readonly section, except when NAMED is true. */
1053 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1054 && (DECL_INITIAL (decl) == NULL
1055 /* In LTO we have no errors in program; error_mark_node is used
1056 to mark offlined constructors. */
1057 || (DECL_INITIAL (decl) == error_mark_node
1058 && !in_lto_p)
1059 || (flag_zero_initialized_in_bss
1060 && initializer_zerop (DECL_INITIAL (decl)))));
1063 /* Compute the alignment of variable specified by DECL.
1064 DONT_OUTPUT_DATA is from assemble_variable. */
1066 void
1067 align_variable (tree decl, bool dont_output_data)
1069 unsigned int align = DECL_ALIGN (decl);
1071 /* In the case for initialing an array whose length isn't specified,
1072 where we have not yet been able to do the layout,
1073 figure out the proper alignment now. */
1074 if (dont_output_data && DECL_SIZE (decl) == 0
1075 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1076 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1078 /* Some object file formats have a maximum alignment which they support.
1079 In particular, a.out format supports a maximum alignment of 4. */
1080 if (align > MAX_OFILE_ALIGNMENT)
1082 error ("alignment of %q+D is greater than maximum object "
1083 "file alignment %d", decl,
1084 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1085 align = MAX_OFILE_ALIGNMENT;
1088 if (! DECL_USER_ALIGN (decl))
1090 #ifdef DATA_ABI_ALIGNMENT
1091 unsigned int data_abi_align
1092 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1093 /* For backwards compatibility, don't assume the ABI alignment for
1094 TLS variables. */
1095 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1096 align = data_abi_align;
1097 #endif
1099 /* On some machines, it is good to increase alignment sometimes.
1100 But as DECL_ALIGN is used both for actually emitting the variable
1101 and for code accessing the variable as guaranteed alignment, we
1102 can only increase the alignment if it is a performance optimization
1103 if the references to it must bind to the current definition. */
1104 if (decl_binds_to_current_def_p (decl)
1105 && !DECL_VIRTUAL_P (decl))
1107 #ifdef DATA_ALIGNMENT
1108 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1109 /* Don't increase alignment too much for TLS variables - TLS space
1110 is too precious. */
1111 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1112 align = data_align;
1113 #endif
1114 if (DECL_INITIAL (decl) != 0
1115 /* In LTO we have no errors in program; error_mark_node is used
1116 to mark offlined constructors. */
1117 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1119 unsigned int const_align
1120 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1121 /* Don't increase alignment too much for TLS variables - TLS
1122 space is too precious. */
1123 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1124 align = const_align;
1129 /* Reset the alignment in case we have made it tighter, so we can benefit
1130 from it in get_pointer_alignment. */
1131 SET_DECL_ALIGN (decl, align);
1134 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1135 beyond what align_variable returned. */
1137 static unsigned int
1138 get_variable_align (tree decl)
1140 unsigned int align = DECL_ALIGN (decl);
1142 /* For user aligned vars or static vars align_variable already did
1143 everything. */
1144 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1145 return align;
1147 #ifdef DATA_ABI_ALIGNMENT
1148 if (DECL_THREAD_LOCAL_P (decl))
1149 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1150 #endif
1152 /* For decls that bind to the current definition, align_variable
1153 did also everything, except for not assuming ABI required alignment
1154 of TLS variables. For other vars, increase the alignment here
1155 as an optimization. */
1156 if (!decl_binds_to_current_def_p (decl))
1158 /* On some machines, it is good to increase alignment sometimes. */
1159 #ifdef DATA_ALIGNMENT
1160 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1161 /* Don't increase alignment too much for TLS variables - TLS space
1162 is too precious. */
1163 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1164 align = data_align;
1165 #endif
1166 if (DECL_INITIAL (decl) != 0
1167 /* In LTO we have no errors in program; error_mark_node is used
1168 to mark offlined constructors. */
1169 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1171 unsigned int const_align
1172 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1173 /* Don't increase alignment too much for TLS variables - TLS space
1174 is too precious. */
1175 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1176 align = const_align;
1180 return align;
1183 /* Return the section into which the given VAR_DECL or CONST_DECL
1184 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1185 section should be used wherever possible. */
1187 section *
1188 get_variable_section (tree decl, bool prefer_noswitch_p)
1190 addr_space_t as = ADDR_SPACE_GENERIC;
1191 int reloc;
1192 varpool_node *vnode = varpool_node::get (decl);
1193 if (vnode)
1195 vnode = vnode->ultimate_alias_target ();
1196 decl = vnode->decl;
1199 if (TREE_TYPE (decl) != error_mark_node)
1200 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1202 /* We need the constructor to figure out reloc flag. */
1203 if (vnode)
1204 vnode->get_constructor ();
1206 if (DECL_COMMON (decl))
1208 /* If the decl has been given an explicit section name, or it resides
1209 in a non-generic address space, then it isn't common, and shouldn't
1210 be handled as such. */
1211 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1212 && ADDR_SPACE_GENERIC_P (as));
1213 if (DECL_THREAD_LOCAL_P (decl))
1214 return tls_comm_section;
1215 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1216 return comm_section;
1219 if (DECL_INITIAL (decl) == error_mark_node)
1220 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1221 else if (DECL_INITIAL (decl))
1222 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1223 else
1224 reloc = 0;
1226 resolve_unique_section (decl, reloc, flag_data_sections);
1227 if (IN_NAMED_SECTION (decl))
1229 section *sect = get_named_section (decl, NULL, reloc);
1231 if ((sect->common.flags & SECTION_BSS)
1232 && !bss_initializer_p (decl, true))
1234 error_at (DECL_SOURCE_LOCATION (decl),
1235 "only zero initializers are allowed in section %qs",
1236 sect->named.name);
1237 DECL_INITIAL (decl) = error_mark_node;
1239 return sect;
1242 if (ADDR_SPACE_GENERIC_P (as)
1243 && !DECL_THREAD_LOCAL_P (decl)
1244 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1245 && bss_initializer_p (decl))
1247 if (!TREE_PUBLIC (decl)
1248 && !((flag_sanitize & SANITIZE_ADDRESS)
1249 && asan_protect_global (decl)))
1250 return lcomm_section;
1251 if (bss_noswitch_section)
1252 return bss_noswitch_section;
1255 return targetm.asm_out.select_section (decl, reloc,
1256 get_variable_align (decl));
1259 /* Return the block into which object_block DECL should be placed. */
1261 static struct object_block *
1262 get_block_for_decl (tree decl)
1264 section *sect;
1266 if (VAR_P (decl))
1268 /* The object must be defined in this translation unit. */
1269 if (DECL_EXTERNAL (decl))
1270 return NULL;
1272 /* There's no point using object blocks for something that is
1273 isolated by definition. */
1274 if (DECL_COMDAT_GROUP (decl))
1275 return NULL;
1278 /* We can only calculate block offsets if the decl has a known
1279 constant size. */
1280 if (DECL_SIZE_UNIT (decl) == NULL)
1281 return NULL;
1282 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1283 return NULL;
1285 /* Find out which section should contain DECL. We cannot put it into
1286 an object block if it requires a standalone definition. */
1287 if (VAR_P (decl))
1288 align_variable (decl, 0);
1289 sect = get_variable_section (decl, true);
1290 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1291 return NULL;
1293 return get_block_for_section (sect);
1296 /* Make sure block symbol SYMBOL is in block BLOCK. */
1298 static void
1299 change_symbol_block (rtx symbol, struct object_block *block)
1301 if (block != SYMBOL_REF_BLOCK (symbol))
1303 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1304 SYMBOL_REF_BLOCK (symbol) = block;
1308 /* Return true if it is possible to put DECL in an object_block. */
1310 static bool
1311 use_blocks_for_decl_p (tree decl)
1313 struct symtab_node *snode;
1315 /* Only data DECLs can be placed into object blocks. */
1316 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1317 return false;
1319 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1320 are never used from code directly and we never want object block handling
1321 for those. */
1322 if (DECL_INITIAL (decl) == decl)
1323 return false;
1325 /* If this decl is an alias, then we don't want to emit a
1326 definition. */
1327 if (VAR_P (decl)
1328 && (snode = symtab_node::get (decl)) != NULL
1329 && snode->alias)
1330 return false;
1332 return targetm.use_blocks_for_decl_p (decl);
1335 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1336 until we find an identifier that is not itself a transparent alias.
1337 Modify the alias passed to it by reference (and all aliases on the
1338 way to the ultimate target), such that they do not have to be
1339 followed again, and return the ultimate target of the alias
1340 chain. */
1342 static inline tree
1343 ultimate_transparent_alias_target (tree *alias)
1345 tree target = *alias;
1347 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1349 gcc_assert (TREE_CHAIN (target));
1350 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1351 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1352 && ! TREE_CHAIN (target));
1353 *alias = target;
1356 return target;
1359 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1360 have static storage duration. In other words, it should not be an
1361 automatic variable, including PARM_DECLs.
1363 There is, however, one exception: this function handles variables
1364 explicitly placed in a particular register by the user.
1366 This is never called for PARM_DECL nodes. */
1368 void
1369 make_decl_rtl (tree decl)
1371 const char *name = 0;
1372 int reg_number;
1373 tree id;
1374 rtx x;
1376 /* Check that we are not being given an automatic variable. */
1377 gcc_assert (TREE_CODE (decl) != PARM_DECL
1378 && TREE_CODE (decl) != RESULT_DECL);
1380 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1381 gcc_assert (!VAR_P (decl)
1382 || TREE_STATIC (decl)
1383 || TREE_PUBLIC (decl)
1384 || DECL_EXTERNAL (decl)
1385 || DECL_REGISTER (decl));
1387 /* And that we were not given a type or a label. */
1388 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1389 && TREE_CODE (decl) != LABEL_DECL);
1391 /* For a duplicate declaration, we can be called twice on the
1392 same DECL node. Don't discard the RTL already made. */
1393 if (DECL_RTL_SET_P (decl))
1395 /* If the old RTL had the wrong mode, fix the mode. */
1396 x = DECL_RTL (decl);
1397 if (GET_MODE (x) != DECL_MODE (decl))
1398 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1400 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1401 return;
1403 /* ??? Another way to do this would be to maintain a hashed
1404 table of such critters. Instead of adding stuff to a DECL
1405 to give certain attributes to it, we could use an external
1406 hash map from DECL to set of attributes. */
1408 /* Let the target reassign the RTL if it wants.
1409 This is necessary, for example, when one machine specific
1410 decl attribute overrides another. */
1411 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1413 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1414 on the new decl information. */
1415 if (MEM_P (x)
1416 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1417 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1418 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1420 return;
1423 /* If this variable belongs to the global constant pool, retrieve the
1424 pre-computed RTL or recompute it in LTO mode. */
1425 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1427 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1428 return;
1431 id = DECL_ASSEMBLER_NAME (decl);
1432 name = IDENTIFIER_POINTER (id);
1434 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1435 && DECL_REGISTER (decl))
1437 error ("register name not specified for %q+D", decl);
1439 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1441 const char *asmspec = name+1;
1442 machine_mode mode = DECL_MODE (decl);
1443 reg_number = decode_reg_name (asmspec);
1444 /* First detect errors in declaring global registers. */
1445 if (reg_number == -1)
1446 error ("register name not specified for %q+D", decl);
1447 else if (reg_number < 0)
1448 error ("invalid register name for %q+D", decl);
1449 else if (mode == BLKmode)
1450 error ("data type of %q+D isn%'t suitable for a register",
1451 decl);
1452 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1453 error ("the register specified for %q+D cannot be accessed"
1454 " by the current target", decl);
1455 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1456 error ("the register specified for %q+D is not general enough"
1457 " to be used as a register variable", decl);
1458 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1459 error ("register specified for %q+D isn%'t suitable for data type",
1460 decl);
1461 /* Now handle properly declared static register variables. */
1462 else
1464 int nregs;
1466 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1468 DECL_INITIAL (decl) = 0;
1469 error ("global register variable has initial value");
1471 if (TREE_THIS_VOLATILE (decl))
1472 warning (OPT_Wvolatile_register_var,
1473 "optimization may eliminate reads and/or "
1474 "writes to register variables");
1476 /* If the user specified one of the eliminables registers here,
1477 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1478 confused with that register and be eliminated. This usage is
1479 somewhat suspect... */
1481 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1482 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1483 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1485 if (TREE_STATIC (decl))
1487 /* Make this register global, so not usable for anything
1488 else. */
1489 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1490 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1491 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1492 #endif
1493 nregs = hard_regno_nregs (reg_number, mode);
1494 while (nregs > 0)
1495 globalize_reg (decl, reg_number + --nregs);
1498 /* As a register variable, it has no section. */
1499 return;
1501 /* Avoid internal errors from invalid register
1502 specifications. */
1503 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1504 DECL_HARD_REGISTER (decl) = 0;
1505 /* Also avoid SSA inconsistencies by pretending this is an external
1506 decl now. */
1507 DECL_EXTERNAL (decl) = 1;
1508 return;
1510 /* Now handle ordinary static variables and functions (in memory).
1511 Also handle vars declared register invalidly. */
1512 else if (name[0] == '*')
1514 #ifdef REGISTER_PREFIX
1515 if (strlen (REGISTER_PREFIX) != 0)
1517 reg_number = decode_reg_name (name);
1518 if (reg_number >= 0 || reg_number == -3)
1519 error ("register name given for non-register variable %q+D", decl);
1521 #endif
1524 /* Specifying a section attribute on a variable forces it into a
1525 non-.bss section, and thus it cannot be common. */
1526 /* FIXME: In general this code should not be necessary because
1527 visibility pass is doing the same work. But notice_global_symbol
1528 is called early and it needs to make DECL_RTL to get the name.
1529 we take care of recomputing the DECL_RTL after visibility is changed. */
1530 if (VAR_P (decl)
1531 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1532 && DECL_SECTION_NAME (decl) != NULL
1533 && DECL_INITIAL (decl) == NULL_TREE
1534 && DECL_COMMON (decl))
1535 DECL_COMMON (decl) = 0;
1537 /* Variables can't be both common and weak. */
1538 if (VAR_P (decl) && DECL_WEAK (decl))
1539 DECL_COMMON (decl) = 0;
1541 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1542 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1543 else
1545 machine_mode address_mode = Pmode;
1546 if (TREE_TYPE (decl) != error_mark_node)
1548 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1549 address_mode = targetm.addr_space.address_mode (as);
1551 x = gen_rtx_SYMBOL_REF (address_mode, name);
1553 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1554 SET_SYMBOL_REF_DECL (x, decl);
1556 x = gen_rtx_MEM (DECL_MODE (decl), x);
1557 if (TREE_CODE (decl) != FUNCTION_DECL)
1558 set_mem_attributes (x, decl, 1);
1559 SET_DECL_RTL (decl, x);
1561 /* Optionally set flags or add text to the name to record information
1562 such as that it is a function name.
1563 If the name is changed, the macro ASM_OUTPUT_LABELREF
1564 will have to know how to strip this information. */
1565 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1568 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1569 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1570 rtl. */
1573 make_decl_rtl_for_debug (tree decl)
1575 unsigned int save_aliasing_flag;
1576 rtx rtl;
1578 if (DECL_RTL_SET_P (decl))
1579 return DECL_RTL (decl);
1581 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1582 call new_alias_set. If running with -fcompare-debug, sometimes
1583 we do not want to create alias sets that will throw the alias
1584 numbers off in the comparison dumps. So... clearing
1585 flag_strict_aliasing will keep new_alias_set() from creating a
1586 new set. */
1587 save_aliasing_flag = flag_strict_aliasing;
1588 flag_strict_aliasing = 0;
1590 rtl = DECL_RTL (decl);
1591 /* Reset DECL_RTL back, as various parts of the compiler expects
1592 DECL_RTL set meaning it is actually going to be output. */
1593 SET_DECL_RTL (decl, NULL);
1595 flag_strict_aliasing = save_aliasing_flag;
1596 return rtl;
1599 /* Output a string of literal assembler code
1600 for an `asm' keyword used between functions. */
1602 void
1603 assemble_asm (tree string)
1605 const char *p;
1606 app_enable ();
1608 if (TREE_CODE (string) == ADDR_EXPR)
1609 string = TREE_OPERAND (string, 0);
1611 p = TREE_STRING_POINTER (string);
1612 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1615 /* Write the address of the entity given by SYMBOL to SEC. */
1616 void
1617 assemble_addr_to_section (rtx symbol, section *sec)
1619 switch_to_section (sec);
1620 assemble_align (POINTER_SIZE);
1621 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1624 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1625 not) section for PRIORITY. */
1626 section *
1627 get_cdtor_priority_section (int priority, bool constructor_p)
1629 /* Buffer conservatively large enough for the full range of a 32-bit
1630 int plus the text below. */
1631 char buf[18];
1633 /* ??? This only works reliably with the GNU linker. */
1634 sprintf (buf, "%s.%.5u",
1635 constructor_p ? ".ctors" : ".dtors",
1636 /* Invert the numbering so the linker puts us in the proper
1637 order; constructors are run from right to left, and the
1638 linker sorts in increasing order. */
1639 MAX_INIT_PRIORITY - priority);
1640 return get_section (buf, SECTION_WRITE, NULL);
1643 void
1644 default_named_section_asm_out_destructor (rtx symbol, int priority)
1646 section *sec;
1648 if (priority != DEFAULT_INIT_PRIORITY)
1649 sec = get_cdtor_priority_section (priority,
1650 /*constructor_p=*/false);
1651 else
1652 sec = get_section (".dtors", SECTION_WRITE, NULL);
1654 assemble_addr_to_section (symbol, sec);
1657 #ifdef DTORS_SECTION_ASM_OP
1658 void
1659 default_dtor_section_asm_out_destructor (rtx symbol,
1660 int priority ATTRIBUTE_UNUSED)
1662 assemble_addr_to_section (symbol, dtors_section);
1664 #endif
1666 void
1667 default_named_section_asm_out_constructor (rtx symbol, int priority)
1669 section *sec;
1671 if (priority != DEFAULT_INIT_PRIORITY)
1672 sec = get_cdtor_priority_section (priority,
1673 /*constructor_p=*/true);
1674 else
1675 sec = get_section (".ctors", SECTION_WRITE, NULL);
1677 assemble_addr_to_section (symbol, sec);
1680 #ifdef CTORS_SECTION_ASM_OP
1681 void
1682 default_ctor_section_asm_out_constructor (rtx symbol,
1683 int priority ATTRIBUTE_UNUSED)
1685 assemble_addr_to_section (symbol, ctors_section);
1687 #endif
1689 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1690 a nonzero value if the constant pool should be output before the
1691 start of the function, or a zero value if the pool should output
1692 after the end of the function. The default is to put it before the
1693 start. */
1695 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1696 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1697 #endif
1699 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1700 to be output to assembler.
1701 Set first_global_object_name and weak_global_object_name as appropriate. */
1703 void
1704 notice_global_symbol (tree decl)
1706 const char **t = &first_global_object_name;
1708 if (first_global_object_name
1709 || !TREE_PUBLIC (decl)
1710 || DECL_EXTERNAL (decl)
1711 || !DECL_NAME (decl)
1712 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1713 || (TREE_CODE (decl) != FUNCTION_DECL
1714 && (!VAR_P (decl)
1715 || (DECL_COMMON (decl)
1716 && (DECL_INITIAL (decl) == 0
1717 || DECL_INITIAL (decl) == error_mark_node)))))
1718 return;
1720 /* We win when global object is found, but it is useful to know about weak
1721 symbol as well so we can produce nicer unique names. */
1722 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1723 t = &weak_global_object_name;
1725 if (!*t)
1727 tree id = DECL_ASSEMBLER_NAME (decl);
1728 ultimate_transparent_alias_target (&id);
1729 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1733 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1734 current function goes into the cold section, so that targets can use
1735 current_function_section during RTL expansion. DECL describes the
1736 function. */
1738 void
1739 decide_function_section (tree decl)
1741 first_function_block_is_cold = false;
1743 if (DECL_SECTION_NAME (decl))
1745 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1746 /* Calls to function_section rely on first_function_block_is_cold
1747 being accurate. */
1748 first_function_block_is_cold = (node
1749 && node->frequency
1750 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1753 in_cold_section_p = first_function_block_is_cold;
1756 /* Get the function's name, as described by its RTL. This may be
1757 different from the DECL_NAME name used in the source file. */
1758 const char *
1759 get_fnname_from_decl (tree decl)
1761 rtx x = DECL_RTL (decl);
1762 gcc_assert (MEM_P (x));
1763 x = XEXP (x, 0);
1764 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1765 return XSTR (x, 0);
1768 /* Output assembler code for the constant pool of a function and associated
1769 with defining the name of the function. DECL describes the function.
1770 NAME is the function's name. For the constant pool, we use the current
1771 constant pool data. */
1773 void
1774 assemble_start_function (tree decl, const char *fnname)
1776 int align;
1777 char tmp_label[100];
1778 bool hot_label_written = false;
1780 if (crtl->has_bb_partition)
1782 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1783 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1784 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1785 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1786 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1787 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1788 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1789 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1790 const_labelno++;
1791 cold_function_name = NULL_TREE;
1793 else
1795 crtl->subsections.hot_section_label = NULL;
1796 crtl->subsections.cold_section_label = NULL;
1797 crtl->subsections.hot_section_end_label = NULL;
1798 crtl->subsections.cold_section_end_label = NULL;
1801 /* The following code does not need preprocessing in the assembler. */
1803 app_disable ();
1805 if (CONSTANT_POOL_BEFORE_FUNCTION)
1806 output_constant_pool (fnname, decl);
1808 align = symtab_node::get (decl)->definition_alignment ();
1810 /* Make sure the not and cold text (code) sections are properly
1811 aligned. This is necessary here in the case where the function
1812 has both hot and cold sections, because we don't want to re-set
1813 the alignment when the section switch happens mid-function. */
1815 if (crtl->has_bb_partition)
1817 first_function_block_is_cold = false;
1819 switch_to_section (unlikely_text_section ());
1820 assemble_align (align);
1821 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1823 /* When the function starts with a cold section, we need to explicitly
1824 align the hot section and write out the hot section label.
1825 But if the current function is a thunk, we do not have a CFG. */
1826 if (!cfun->is_thunk
1827 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1829 switch_to_section (text_section);
1830 assemble_align (align);
1831 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1832 hot_label_written = true;
1833 first_function_block_is_cold = true;
1835 in_cold_section_p = first_function_block_is_cold;
1839 /* Switch to the correct text section for the start of the function. */
1841 switch_to_section (function_section (decl));
1842 if (crtl->has_bb_partition && !hot_label_written)
1843 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1845 /* Tell assembler to move to target machine's alignment for functions. */
1846 align = floor_log2 (align / BITS_PER_UNIT);
1847 if (align > 0)
1849 ASM_OUTPUT_ALIGN (asm_out_file, align);
1852 /* Handle a user-specified function alignment.
1853 Note that we still need to align to DECL_ALIGN, as above,
1854 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1855 if (! DECL_USER_ALIGN (decl)
1856 && align_functions.levels[0].log > align
1857 && optimize_function_for_speed_p (cfun))
1859 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1860 int align_log = align_functions.levels[0].log;
1861 #endif
1862 int max_skip = align_functions.levels[0].maxskip;
1863 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1864 && max_skip >= crtl->max_insn_address)
1865 max_skip = crtl->max_insn_address - 1;
1867 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1868 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1869 if (max_skip == align_functions.levels[0].maxskip)
1870 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1871 align_functions.levels[1].log,
1872 align_functions.levels[1].maxskip);
1873 #else
1874 ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
1875 #endif
1878 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1879 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1880 #endif
1882 if (!DECL_IGNORED_P (decl))
1883 (*debug_hooks->begin_function) (decl);
1885 /* Make function name accessible from other files, if appropriate. */
1887 if (TREE_PUBLIC (decl))
1889 notice_global_symbol (decl);
1891 globalize_decl (decl);
1893 maybe_assemble_visibility (decl);
1896 if (DECL_PRESERVE_P (decl))
1897 targetm.asm_out.mark_decl_preserved (fnname);
1899 unsigned short patch_area_size = crtl->patch_area_size;
1900 unsigned short patch_area_entry = crtl->patch_area_entry;
1902 /* Emit the patching area before the entry label, if any. */
1903 if (patch_area_entry > 0)
1904 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1905 patch_area_entry, true);
1907 /* Do any machine/system dependent processing of the function name. */
1908 #ifdef ASM_DECLARE_FUNCTION_NAME
1909 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1910 #else
1911 /* Standard thing is just output label for the function. */
1912 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1913 #endif /* ASM_DECLARE_FUNCTION_NAME */
1915 /* And the area after the label. Record it if we haven't done so yet. */
1916 if (patch_area_size > patch_area_entry)
1917 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1918 patch_area_size
1919 - patch_area_entry,
1920 patch_area_entry == 0);
1922 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1923 saw_no_split_stack = true;
1926 /* Output assembler code associated with defining the size of the
1927 function. DECL describes the function. NAME is the function's name. */
1929 void
1930 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1932 #ifdef ASM_DECLARE_FUNCTION_SIZE
1933 /* We could have switched section in the middle of the function. */
1934 if (crtl->has_bb_partition)
1935 switch_to_section (function_section (decl));
1936 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1937 #endif
1938 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1940 output_constant_pool (fnname, decl);
1941 switch_to_section (function_section (decl)); /* need to switch back */
1943 /* Output labels for end of hot/cold text sections (to be used by
1944 debug info.) */
1945 if (crtl->has_bb_partition)
1947 section *save_text_section;
1949 save_text_section = in_section;
1950 switch_to_section (unlikely_text_section ());
1951 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
1952 if (cold_function_name != NULL_TREE)
1953 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
1954 IDENTIFIER_POINTER (cold_function_name),
1955 decl);
1956 #endif
1957 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1958 if (first_function_block_is_cold)
1959 switch_to_section (text_section);
1960 else
1961 switch_to_section (function_section (decl));
1962 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1963 switch_to_section (save_text_section);
1967 /* Assemble code to leave SIZE bytes of zeros. */
1969 void
1970 assemble_zeros (unsigned HOST_WIDE_INT size)
1972 /* Do no output if -fsyntax-only. */
1973 if (flag_syntax_only)
1974 return;
1976 #ifdef ASM_NO_SKIP_IN_TEXT
1977 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1978 so we must output 0s explicitly in the text section. */
1979 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1981 unsigned HOST_WIDE_INT i;
1982 for (i = 0; i < size; i++)
1983 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1985 else
1986 #endif
1987 if (size > 0)
1988 ASM_OUTPUT_SKIP (asm_out_file, size);
1991 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1993 void
1994 assemble_align (unsigned int align)
1996 if (align > BITS_PER_UNIT)
1998 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2002 /* Assemble a string constant with the specified C string as contents. */
2004 void
2005 assemble_string (const char *p, int size)
2007 int pos = 0;
2008 int maximum = 2000;
2010 /* If the string is very long, split it up. */
2012 while (pos < size)
2014 int thissize = size - pos;
2015 if (thissize > maximum)
2016 thissize = maximum;
2018 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
2020 pos += thissize;
2021 p += thissize;
2026 /* A noswitch_section_callback for lcomm_section. */
2028 static bool
2029 emit_local (tree decl ATTRIBUTE_UNUSED,
2030 const char *name ATTRIBUTE_UNUSED,
2031 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2032 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2034 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2035 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2036 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2037 size, align);
2038 return true;
2039 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2040 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2041 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2042 return true;
2043 #else
2044 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2045 return false;
2046 #endif
2049 /* A noswitch_section_callback for bss_noswitch_section. */
2051 #if defined ASM_OUTPUT_ALIGNED_BSS
2052 static bool
2053 emit_bss (tree decl ATTRIBUTE_UNUSED,
2054 const char *name ATTRIBUTE_UNUSED,
2055 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2056 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2058 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2059 get_variable_align (decl));
2060 return true;
2062 #endif
2064 /* A noswitch_section_callback for comm_section. */
2066 static bool
2067 emit_common (tree decl ATTRIBUTE_UNUSED,
2068 const char *name ATTRIBUTE_UNUSED,
2069 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2070 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2072 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2073 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2074 size, get_variable_align (decl));
2075 return true;
2076 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2077 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2078 get_variable_align (decl));
2079 return true;
2080 #else
2081 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2082 return false;
2083 #endif
2086 /* A noswitch_section_callback for tls_comm_section. */
2088 static bool
2089 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2090 const char *name ATTRIBUTE_UNUSED,
2091 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2092 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2094 #ifdef ASM_OUTPUT_TLS_COMMON
2095 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2096 return true;
2097 #else
2098 sorry ("thread-local COMMON data not implemented");
2099 return true;
2100 #endif
2103 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2104 NAME is the name of DECL's SYMBOL_REF. */
2106 static void
2107 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2108 unsigned int align)
2110 unsigned HOST_WIDE_INT size, rounded;
2112 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2113 rounded = size;
2115 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2116 size += asan_red_zone_size (size);
2118 /* Don't allocate zero bytes of common,
2119 since that means "undefined external" in the linker. */
2120 if (size == 0)
2121 rounded = 1;
2123 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2124 so that each uninitialized object starts on such a boundary. */
2125 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2126 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2127 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2129 if (!sect->noswitch.callback (decl, name, size, rounded)
2130 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2131 error ("requested alignment for %q+D is greater than "
2132 "implemented alignment of %wu", decl, rounded);
2135 /* A subroutine of assemble_variable. Output the label and contents of
2136 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2137 is as for assemble_variable. */
2139 static void
2140 assemble_variable_contents (tree decl, const char *name,
2141 bool dont_output_data, bool merge_strings)
2143 /* Do any machine/system dependent processing of the object. */
2144 #ifdef ASM_DECLARE_OBJECT_NAME
2145 last_assemble_variable_decl = decl;
2146 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2147 #else
2148 /* Standard thing is just output label for the object. */
2149 ASM_OUTPUT_LABEL (asm_out_file, name);
2150 #endif /* ASM_DECLARE_OBJECT_NAME */
2152 if (!dont_output_data)
2154 /* Caller is supposed to use varpool_get_constructor when it wants
2155 to output the body. */
2156 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2157 if (DECL_INITIAL (decl)
2158 && DECL_INITIAL (decl) != error_mark_node
2159 && !initializer_zerop (DECL_INITIAL (decl)))
2160 /* Output the actual data. */
2161 output_constant (DECL_INITIAL (decl),
2162 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2163 get_variable_align (decl),
2164 false, merge_strings);
2165 else
2166 /* Leave space for it. */
2167 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2168 targetm.asm_out.decl_end ();
2172 /* Write out assembly for the variable DECL, which is not defined in
2173 the current translation unit. */
2174 void
2175 assemble_undefined_decl (tree decl)
2177 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2178 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2181 /* Assemble everything that is needed for a variable or function declaration.
2182 Not used for automatic variables, and not used for function definitions.
2183 Should not be called for variables of incomplete structure type.
2185 TOP_LEVEL is nonzero if this variable has file scope.
2186 AT_END is nonzero if this is the special handling, at end of compilation,
2187 to define things that have had only tentative definitions.
2188 DONT_OUTPUT_DATA if nonzero means don't actually output the
2189 initial value (that will be done by the caller). */
2191 void
2192 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2193 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2195 const char *name;
2196 rtx decl_rtl, symbol;
2197 section *sect;
2198 unsigned int align;
2199 bool asan_protected = false;
2201 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2202 gcc_assert (VAR_P (decl));
2204 /* Emulated TLS had better not get this far. */
2205 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2207 last_assemble_variable_decl = 0;
2209 /* Normally no need to say anything here for external references,
2210 since assemble_external is called by the language-specific code
2211 when a declaration is first seen. */
2213 if (DECL_EXTERNAL (decl))
2214 return;
2216 /* Do nothing for global register variables. */
2217 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2219 TREE_ASM_WRITTEN (decl) = 1;
2220 return;
2223 /* If type was incomplete when the variable was declared,
2224 see if it is complete now. */
2226 if (DECL_SIZE (decl) == 0)
2227 layout_decl (decl, 0);
2229 /* Still incomplete => don't allocate it; treat the tentative defn
2230 (which is what it must have been) as an `extern' reference. */
2232 if (!dont_output_data && DECL_SIZE (decl) == 0)
2234 error ("storage size of %q+D isn%'t known", decl);
2235 TREE_ASM_WRITTEN (decl) = 1;
2236 return;
2239 /* The first declaration of a variable that comes through this function
2240 decides whether it is global (in C, has external linkage)
2241 or local (in C, has internal linkage). So do nothing more
2242 if this function has already run. */
2244 if (TREE_ASM_WRITTEN (decl))
2245 return;
2247 /* Make sure targetm.encode_section_info is invoked before we set
2248 ASM_WRITTEN. */
2249 decl_rtl = DECL_RTL (decl);
2251 TREE_ASM_WRITTEN (decl) = 1;
2253 /* Do no output if -fsyntax-only. */
2254 if (flag_syntax_only)
2255 return;
2257 if (! dont_output_data
2258 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2260 error ("size of variable %q+D is too large", decl);
2261 return;
2264 gcc_assert (MEM_P (decl_rtl));
2265 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2266 symbol = XEXP (decl_rtl, 0);
2268 /* If this symbol belongs to the tree constant pool, output the constant
2269 if it hasn't already been written. */
2270 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2272 tree decl = SYMBOL_REF_DECL (symbol);
2273 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2274 output_constant_def_contents (symbol);
2275 return;
2278 app_disable ();
2280 name = XSTR (symbol, 0);
2281 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2282 notice_global_symbol (decl);
2284 /* Compute the alignment of this data. */
2286 align_variable (decl, dont_output_data);
2288 if ((flag_sanitize & SANITIZE_ADDRESS)
2289 && asan_protect_global (decl))
2291 asan_protected = true;
2292 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2293 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2296 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2298 align = get_variable_align (decl);
2300 if (TREE_PUBLIC (decl))
2301 maybe_assemble_visibility (decl);
2303 if (DECL_PRESERVE_P (decl))
2304 targetm.asm_out.mark_decl_preserved (name);
2306 /* First make the assembler name(s) global if appropriate. */
2307 sect = get_variable_section (decl, false);
2308 if (TREE_PUBLIC (decl)
2309 && (sect->common.flags & SECTION_COMMON) == 0)
2310 globalize_decl (decl);
2312 /* Output any data that we will need to use the address of. */
2313 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2314 output_addressed_constants (DECL_INITIAL (decl), 0);
2316 /* dbxout.c needs to know this. */
2317 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2318 DECL_IN_TEXT_SECTION (decl) = 1;
2320 /* If the decl is part of an object_block, make sure that the decl
2321 has been positioned within its block, but do not write out its
2322 definition yet. output_object_blocks will do that later. */
2323 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2325 gcc_assert (!dont_output_data);
2326 place_block_symbol (symbol);
2328 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2329 assemble_noswitch_variable (decl, name, sect, align);
2330 else
2332 /* Special-case handling of vtv comdat sections. */
2333 if (sect->named.name
2334 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2335 handle_vtv_comdat_section (sect, decl);
2336 else
2337 switch_to_section (sect);
2338 if (align > BITS_PER_UNIT)
2339 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2340 assemble_variable_contents (decl, name, dont_output_data,
2341 (sect->common.flags & SECTION_MERGE)
2342 && (sect->common.flags & SECTION_STRINGS));
2343 if (asan_protected)
2345 unsigned HOST_WIDE_INT int size
2346 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2347 assemble_zeros (asan_red_zone_size (size));
2353 /* Given a function declaration (FN_DECL), this function assembles the
2354 function into the .preinit_array section. */
2356 void
2357 assemble_vtv_preinit_initializer (tree fn_decl)
2359 section *sect;
2360 unsigned flags = SECTION_WRITE;
2361 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2363 flags |= SECTION_NOTYPE;
2364 sect = get_section (".preinit_array", flags, fn_decl);
2365 switch_to_section (sect);
2366 assemble_addr_to_section (symbol, sect);
2369 /* Return 1 if type TYPE contains any pointers. */
2371 static int
2372 contains_pointers_p (tree type)
2374 switch (TREE_CODE (type))
2376 case POINTER_TYPE:
2377 case REFERENCE_TYPE:
2378 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2379 so I'll play safe and return 1. */
2380 case OFFSET_TYPE:
2381 return 1;
2383 case RECORD_TYPE:
2384 case UNION_TYPE:
2385 case QUAL_UNION_TYPE:
2387 tree fields;
2388 /* For a type that has fields, see if the fields have pointers. */
2389 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2390 if (TREE_CODE (fields) == FIELD_DECL
2391 && contains_pointers_p (TREE_TYPE (fields)))
2392 return 1;
2393 return 0;
2396 case ARRAY_TYPE:
2397 /* An array type contains pointers if its element type does. */
2398 return contains_pointers_p (TREE_TYPE (type));
2400 default:
2401 return 0;
2405 /* We delay assemble_external processing until
2406 the compilation unit is finalized. This is the best we can do for
2407 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2408 it all the way to final. See PR 17982 for further discussion. */
2409 static GTY(()) tree pending_assemble_externals;
2411 #ifdef ASM_OUTPUT_EXTERNAL
2412 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2413 As a result, assemble_external can be called after the list of externals
2414 is processed and the pointer set destroyed. */
2415 static bool pending_assemble_externals_processed;
2417 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2418 TREE_LIST in assemble_external. */
2419 static hash_set<tree> *pending_assemble_externals_set;
2421 /* True if DECL is a function decl for which no out-of-line copy exists.
2422 It is assumed that DECL's assembler name has been set. */
2424 static bool
2425 incorporeal_function_p (tree decl)
2427 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl))
2429 const char *name;
2431 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2432 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2433 return true;
2435 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2436 /* Atomic or sync builtins which have survived this far will be
2437 resolved externally and therefore are not incorporeal. */
2438 if (strncmp (name, "__builtin_", 10) == 0)
2439 return true;
2441 return false;
2444 /* Actually do the tests to determine if this is necessary, and invoke
2445 ASM_OUTPUT_EXTERNAL. */
2446 static void
2447 assemble_external_real (tree decl)
2449 rtx rtl = DECL_RTL (decl);
2451 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2452 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2453 && !incorporeal_function_p (decl))
2455 /* Some systems do require some output. */
2456 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2457 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2460 #endif
2462 void
2463 process_pending_assemble_externals (void)
2465 #ifdef ASM_OUTPUT_EXTERNAL
2466 tree list;
2467 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2468 assemble_external_real (TREE_VALUE (list));
2470 pending_assemble_externals = 0;
2471 pending_assemble_externals_processed = true;
2472 delete pending_assemble_externals_set;
2473 #endif
2476 /* This TREE_LIST contains any weak symbol declarations waiting
2477 to be emitted. */
2478 static GTY(()) tree weak_decls;
2480 /* Output something to declare an external symbol to the assembler,
2481 and qualifiers such as weakness. (Most assemblers don't need
2482 extern declaration, so we normally output nothing.) Do nothing if
2483 DECL is not external. */
2485 void
2486 assemble_external (tree decl ATTRIBUTE_UNUSED)
2488 /* Make sure that the ASM_OUT_FILE is open.
2489 If it's not, we should not be calling this function. */
2490 gcc_assert (asm_out_file);
2492 /* In a perfect world, the following condition would be true.
2493 Sadly, the Go front end emit assembly *from the front end*,
2494 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2495 #if 0
2496 /* This function should only be called if we are expanding, or have
2497 expanded, to RTL.
2498 Ideally, only final.c would be calling this function, but it is
2499 not clear whether that would break things somehow. See PR 17982
2500 for further discussion. */
2501 gcc_assert (state == EXPANSION
2502 || state == FINISHED);
2503 #endif
2505 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2506 return;
2508 /* We want to output annotation for weak and external symbols at
2509 very last to check if they are references or not. */
2511 if (TARGET_SUPPORTS_WEAK
2512 && DECL_WEAK (decl)
2513 /* TREE_STATIC is a weird and abused creature which is not
2514 generally the right test for whether an entity has been
2515 locally emitted, inlined or otherwise not-really-extern, but
2516 for declarations that can be weak, it happens to be
2517 match. */
2518 && !TREE_STATIC (decl)
2519 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2520 && value_member (decl, weak_decls) == NULL_TREE)
2521 weak_decls = tree_cons (NULL, decl, weak_decls);
2523 #ifdef ASM_OUTPUT_EXTERNAL
2524 if (pending_assemble_externals_processed)
2526 assemble_external_real (decl);
2527 return;
2530 if (! pending_assemble_externals_set->add (decl))
2531 pending_assemble_externals = tree_cons (NULL, decl,
2532 pending_assemble_externals);
2533 #endif
2536 /* Similar, for calling a library function FUN. */
2538 void
2539 assemble_external_libcall (rtx fun)
2541 /* Declare library function name external when first used, if nec. */
2542 if (! SYMBOL_REF_USED (fun))
2544 SYMBOL_REF_USED (fun) = 1;
2545 targetm.asm_out.external_libcall (fun);
2549 /* Assemble a label named NAME. */
2551 void
2552 assemble_label (FILE *file, const char *name)
2554 ASM_OUTPUT_LABEL (file, name);
2557 /* Set the symbol_referenced flag for ID. */
2558 void
2559 mark_referenced (tree id)
2561 TREE_SYMBOL_REFERENCED (id) = 1;
2564 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2565 void
2566 mark_decl_referenced (tree decl)
2568 if (TREE_CODE (decl) == FUNCTION_DECL)
2570 /* Extern inline functions don't become needed when referenced.
2571 If we know a method will be emitted in other TU and no new
2572 functions can be marked reachable, just use the external
2573 definition. */
2574 struct cgraph_node *node = cgraph_node::get_create (decl);
2575 if (!DECL_EXTERNAL (decl)
2576 && !node->definition)
2577 node->mark_force_output ();
2579 else if (VAR_P (decl))
2581 varpool_node *node = varpool_node::get_create (decl);
2582 /* C++ frontend use mark_decl_references to force COMDAT variables
2583 to be output that might appear dead otherwise. */
2584 node->force_output = true;
2586 /* else do nothing - we can get various sorts of CST nodes here,
2587 which do not need to be marked. */
2591 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2592 starts with a *, the rest of NAME is output verbatim. Otherwise
2593 NAME is transformed in a target-specific way (usually by the
2594 addition of an underscore). */
2596 void
2597 assemble_name_raw (FILE *file, const char *name)
2599 if (name[0] == '*')
2600 fputs (&name[1], file);
2601 else
2602 ASM_OUTPUT_LABELREF (file, name);
2605 /* Return NAME that should actually be emitted, looking through
2606 transparent aliases. If NAME refers to an entity that is also
2607 represented as a tree (like a function or variable), mark the entity
2608 as referenced. */
2609 const char *
2610 assemble_name_resolve (const char *name)
2612 const char *real_name = targetm.strip_name_encoding (name);
2613 tree id = maybe_get_identifier (real_name);
2615 if (id)
2617 tree id_orig = id;
2619 mark_referenced (id);
2620 ultimate_transparent_alias_target (&id);
2621 if (id != id_orig)
2622 name = IDENTIFIER_POINTER (id);
2623 gcc_assert (! TREE_CHAIN (id));
2626 return name;
2629 /* Like assemble_name_raw, but should be used when NAME might refer to
2630 an entity that is also represented as a tree (like a function or
2631 variable). If NAME does refer to such an entity, that entity will
2632 be marked as referenced. */
2634 void
2635 assemble_name (FILE *file, const char *name)
2637 assemble_name_raw (file, assemble_name_resolve (name));
2640 /* Allocate SIZE bytes writable static space with a gensym name
2641 and return an RTX to refer to its address. */
2644 assemble_static_space (unsigned HOST_WIDE_INT size)
2646 char name[17];
2647 const char *namestring;
2648 rtx x;
2650 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2651 ++const_labelno;
2652 namestring = ggc_strdup (name);
2654 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2655 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2657 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2658 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2659 BIGGEST_ALIGNMENT);
2660 #else
2661 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2662 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2663 #else
2665 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2666 so that each uninitialized object starts on such a boundary. */
2667 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2668 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2669 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2670 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2671 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2672 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2674 #endif
2675 #endif
2676 return x;
2679 /* Assemble the static constant template for function entry trampolines.
2680 This is done at most once per compilation.
2681 Returns an RTX for the address of the template. */
2683 static GTY(()) rtx initial_trampoline;
2686 assemble_trampoline_template (void)
2688 char label[256];
2689 const char *name;
2690 int align;
2691 rtx symbol;
2693 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2695 if (initial_trampoline)
2696 return initial_trampoline;
2698 /* By default, put trampoline templates in read-only data section. */
2700 #ifdef TRAMPOLINE_SECTION
2701 switch_to_section (TRAMPOLINE_SECTION);
2702 #else
2703 switch_to_section (readonly_data_section);
2704 #endif
2706 /* Write the assembler code to define one. */
2707 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2708 if (align > 0)
2709 ASM_OUTPUT_ALIGN (asm_out_file, align);
2711 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2712 targetm.asm_out.trampoline_template (asm_out_file);
2714 /* Record the rtl to refer to it. */
2715 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2716 name = ggc_strdup (label);
2717 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2718 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2720 initial_trampoline = gen_const_mem (BLKmode, symbol);
2721 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2722 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2724 return initial_trampoline;
2727 /* A and B are either alignments or offsets. Return the minimum alignment
2728 that may be assumed after adding the two together. */
2730 static inline unsigned
2731 min_align (unsigned int a, unsigned int b)
2733 return least_bit_hwi (a | b);
2736 /* Return the assembler directive for creating a given kind of integer
2737 object. SIZE is the number of bytes in the object and ALIGNED_P
2738 indicates whether it is known to be aligned. Return NULL if the
2739 assembly dialect has no such directive.
2741 The returned string should be printed at the start of a new line and
2742 be followed immediately by the object's initial value. */
2744 const char *
2745 integer_asm_op (int size, int aligned_p)
2747 struct asm_int_op *ops;
2749 if (aligned_p)
2750 ops = &targetm.asm_out.aligned_op;
2751 else
2752 ops = &targetm.asm_out.unaligned_op;
2754 switch (size)
2756 case 1:
2757 return targetm.asm_out.byte_op;
2758 case 2:
2759 return ops->hi;
2760 case 3:
2761 return ops->psi;
2762 case 4:
2763 return ops->si;
2764 case 5:
2765 case 6:
2766 case 7:
2767 return ops->pdi;
2768 case 8:
2769 return ops->di;
2770 case 9:
2771 case 10:
2772 case 11:
2773 case 12:
2774 case 13:
2775 case 14:
2776 case 15:
2777 return ops->pti;
2778 case 16:
2779 return ops->ti;
2780 default:
2781 return NULL;
2785 /* Use directive OP to assemble an integer object X. Print OP at the
2786 start of the line, followed immediately by the value of X. */
2788 void
2789 assemble_integer_with_op (const char *op, rtx x)
2791 fputs (op, asm_out_file);
2792 output_addr_const (asm_out_file, x);
2793 fputc ('\n', asm_out_file);
2796 /* The default implementation of the asm_out.integer target hook. */
2798 bool
2799 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2800 unsigned int size ATTRIBUTE_UNUSED,
2801 int aligned_p ATTRIBUTE_UNUSED)
2803 const char *op = integer_asm_op (size, aligned_p);
2804 /* Avoid GAS bugs for large values. Specifically negative values whose
2805 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2806 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2807 return false;
2808 return op && (assemble_integer_with_op (op, x), true);
2811 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2812 the alignment of the integer in bits. Return 1 if we were able to output
2813 the constant, otherwise 0. We must be able to output the constant,
2814 if FORCE is nonzero. */
2816 bool
2817 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2819 int aligned_p;
2821 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2823 /* See if the target hook can handle this kind of object. */
2824 if (targetm.asm_out.integer (x, size, aligned_p))
2825 return true;
2827 /* If the object is a multi-byte one, try splitting it up. Split
2828 it into words it if is multi-word, otherwise split it into bytes. */
2829 if (size > 1)
2831 machine_mode omode, imode;
2832 unsigned int subalign;
2833 unsigned int subsize, i;
2834 enum mode_class mclass;
2836 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2837 subalign = MIN (align, subsize * BITS_PER_UNIT);
2838 if (GET_CODE (x) == CONST_FIXED)
2839 mclass = GET_MODE_CLASS (GET_MODE (x));
2840 else
2841 mclass = MODE_INT;
2843 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2844 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2846 for (i = 0; i < size; i += subsize)
2848 rtx partial = simplify_subreg (omode, x, imode, i);
2849 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2850 break;
2852 if (i == size)
2853 return true;
2855 /* If we've printed some of it, but not all of it, there's no going
2856 back now. */
2857 gcc_assert (!i);
2860 gcc_assert (!force);
2862 return false;
2865 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
2866 is the alignment of the constant in bits. If REVERSE is true, D is output
2867 in reverse storage order. */
2869 void
2870 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2871 bool reverse)
2873 long data[4] = {0, 0, 0, 0};
2874 int bitsize, nelts, nunits, units_per;
2875 rtx elt;
2877 /* This is hairy. We have a quantity of known size. real_to_target
2878 will put it into an array of *host* longs, 32 bits per element
2879 (even if long is more than 32 bits). We need to determine the
2880 number of array elements that are occupied (nelts) and the number
2881 of *target* min-addressable units that will be occupied in the
2882 object file (nunits). We cannot assume that 32 divides the
2883 mode's bitsize (size * BITS_PER_UNIT) evenly.
2885 size * BITS_PER_UNIT is used here to make sure that padding bits
2886 (which might appear at either end of the value; real_to_target
2887 will include the padding bits in its output array) are included. */
2889 nunits = GET_MODE_SIZE (mode);
2890 bitsize = nunits * BITS_PER_UNIT;
2891 nelts = CEIL (bitsize, 32);
2892 units_per = 32 / BITS_PER_UNIT;
2894 real_to_target (data, &d, mode);
2896 /* Put out the first word with the specified alignment. */
2897 unsigned int chunk_nunits = MIN (nunits, units_per);
2898 if (reverse)
2899 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
2900 else
2901 elt = GEN_INT (sext_hwi (data[0], chunk_nunits * BITS_PER_UNIT));
2902 assemble_integer (elt, chunk_nunits, align, 1);
2903 nunits -= chunk_nunits;
2905 /* Subsequent words need only 32-bit alignment. */
2906 align = min_align (align, 32);
2908 for (int i = 1; i < nelts; i++)
2910 chunk_nunits = MIN (nunits, units_per);
2911 if (reverse)
2912 elt = flip_storage_order (SImode,
2913 gen_int_mode (data[nelts - 1 - i], SImode));
2914 else
2915 elt = GEN_INT (sext_hwi (data[i], chunk_nunits * BITS_PER_UNIT));
2916 assemble_integer (elt, chunk_nunits, align, 1);
2917 nunits -= chunk_nunits;
2921 /* Given an expression EXP with a constant value,
2922 reduce it to the sum of an assembler symbol and an integer.
2923 Store them both in the structure *VALUE.
2924 EXP must be reducible. */
2926 class addr_const {
2927 public:
2928 rtx base;
2929 poly_int64 offset;
2932 static void
2933 decode_addr_const (tree exp, class addr_const *value)
2935 tree target = TREE_OPERAND (exp, 0);
2936 poly_int64 offset = 0;
2937 rtx x;
2939 while (1)
2941 poly_int64 bytepos;
2942 if (TREE_CODE (target) == COMPONENT_REF
2943 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
2944 &bytepos))
2946 offset += bytepos;
2947 target = TREE_OPERAND (target, 0);
2949 else if (TREE_CODE (target) == ARRAY_REF
2950 || TREE_CODE (target) == ARRAY_RANGE_REF)
2952 /* Truncate big offset. */
2953 offset
2954 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2955 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
2956 target = TREE_OPERAND (target, 0);
2958 else if (TREE_CODE (target) == MEM_REF
2959 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2961 offset += mem_ref_offset (target).force_shwi ();
2962 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2964 else if (TREE_CODE (target) == INDIRECT_REF
2965 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2966 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2967 == ADDR_EXPR)
2968 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2969 else
2970 break;
2973 switch (TREE_CODE (target))
2975 case VAR_DECL:
2976 case FUNCTION_DECL:
2977 x = DECL_RTL (target);
2978 break;
2980 case LABEL_DECL:
2981 x = gen_rtx_MEM (FUNCTION_MODE,
2982 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2983 break;
2985 case REAL_CST:
2986 case FIXED_CST:
2987 case STRING_CST:
2988 case COMPLEX_CST:
2989 case CONSTRUCTOR:
2990 case INTEGER_CST:
2991 x = lookup_constant_def (target);
2992 /* Should have been added by output_addressed_constants. */
2993 gcc_assert (x);
2994 break;
2996 case INDIRECT_REF:
2997 /* This deals with absolute addresses. */
2998 offset += tree_to_shwi (TREE_OPERAND (target, 0));
2999 x = gen_rtx_MEM (QImode,
3000 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
3001 break;
3003 case COMPOUND_LITERAL_EXPR:
3004 gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
3005 x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
3006 break;
3008 default:
3009 gcc_unreachable ();
3012 gcc_assert (MEM_P (x));
3013 x = XEXP (x, 0);
3015 value->base = x;
3016 value->offset = offset;
3019 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
3021 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
3023 /* Constant pool accessor function. */
3025 hash_table<tree_descriptor_hasher> *
3026 constant_pool_htab (void)
3028 return const_desc_htab;
3031 /* Compute a hash code for a constant expression. */
3033 hashval_t
3034 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3036 return ptr->hash;
3039 static hashval_t
3040 const_hash_1 (const tree exp)
3042 const char *p;
3043 hashval_t hi;
3044 int len, i;
3045 enum tree_code code = TREE_CODE (exp);
3047 /* Either set P and LEN to the address and len of something to hash and
3048 exit the switch or return a value. */
3050 switch (code)
3052 case INTEGER_CST:
3053 p = (char *) &TREE_INT_CST_ELT (exp, 0);
3054 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3055 break;
3057 case REAL_CST:
3058 return real_hash (TREE_REAL_CST_PTR (exp));
3060 case FIXED_CST:
3061 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3063 case STRING_CST:
3064 p = TREE_STRING_POINTER (exp);
3065 len = TREE_STRING_LENGTH (exp);
3066 break;
3068 case COMPLEX_CST:
3069 return (const_hash_1 (TREE_REALPART (exp)) * 5
3070 + const_hash_1 (TREE_IMAGPART (exp)));
3072 case VECTOR_CST:
3074 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3075 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3076 unsigned int count = vector_cst_encoded_nelts (exp);
3077 for (unsigned int i = 0; i < count; ++i)
3078 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3079 return hi;
3082 case CONSTRUCTOR:
3084 unsigned HOST_WIDE_INT idx;
3085 tree value;
3087 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3089 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3090 if (value)
3091 hi = hi * 603 + const_hash_1 (value);
3093 return hi;
3096 case ADDR_EXPR:
3097 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3098 return const_hash_1 (TREE_OPERAND (exp, 0));
3100 /* Fallthru. */
3101 case FDESC_EXPR:
3103 class addr_const value;
3105 decode_addr_const (exp, &value);
3106 switch (GET_CODE (value.base))
3108 case SYMBOL_REF:
3109 /* Don't hash the address of the SYMBOL_REF;
3110 only use the offset and the symbol name. */
3111 hi = value.offset.coeffs[0];
3112 p = XSTR (value.base, 0);
3113 for (i = 0; p[i] != 0; i++)
3114 hi = ((hi * 613) + (unsigned) (p[i]));
3115 break;
3117 case LABEL_REF:
3118 hi = (value.offset.coeffs[0]
3119 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3120 break;
3122 default:
3123 gcc_unreachable ();
3126 return hi;
3128 case PLUS_EXPR:
3129 case POINTER_PLUS_EXPR:
3130 case MINUS_EXPR:
3131 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3132 + const_hash_1 (TREE_OPERAND (exp, 1)));
3134 CASE_CONVERT:
3135 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3137 default:
3138 /* A language specific constant. Just hash the code. */
3139 return code;
3142 /* Compute hashing function. */
3143 hi = len;
3144 for (i = 0; i < len; i++)
3145 hi = ((hi * 613) + (unsigned) (p[i]));
3147 return hi;
3150 /* Wrapper of compare_constant, for the htab interface. */
3151 bool
3152 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3153 constant_descriptor_tree *c2)
3155 if (c1->hash != c2->hash)
3156 return 0;
3157 return compare_constant (c1->value, c2->value);
3160 /* Compare t1 and t2, and return 1 only if they are known to result in
3161 the same bit pattern on output. */
3163 static int
3164 compare_constant (const tree t1, const tree t2)
3166 enum tree_code typecode;
3168 if (t1 == NULL_TREE)
3169 return t2 == NULL_TREE;
3170 if (t2 == NULL_TREE)
3171 return 0;
3173 if (TREE_CODE (t1) != TREE_CODE (t2))
3174 return 0;
3176 switch (TREE_CODE (t1))
3178 case INTEGER_CST:
3179 /* Integer constants are the same only if the same width of type. */
3180 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3181 return 0;
3182 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3183 return 0;
3184 return tree_int_cst_equal (t1, t2);
3186 case REAL_CST:
3187 /* Real constants are the same only if the same width of type. In
3188 addition to the same width, we need to check whether the modes are the
3189 same. There might be two floating point modes that are the same size
3190 but have different representations, such as the PowerPC that has 2
3191 different 128-bit floating point types (IBM extended double and IEEE
3192 128-bit floating point). */
3193 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3194 return 0;
3195 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3196 return 0;
3197 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3199 case FIXED_CST:
3200 /* Fixed constants are the same only if the same width of type. */
3201 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3202 return 0;
3204 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3206 case STRING_CST:
3207 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3208 || int_size_in_bytes (TREE_TYPE (t1))
3209 != int_size_in_bytes (TREE_TYPE (t2)))
3210 return 0;
3212 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3213 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3214 TREE_STRING_LENGTH (t1)));
3216 case COMPLEX_CST:
3217 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3218 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3220 case VECTOR_CST:
3222 if (VECTOR_CST_NPATTERNS (t1)
3223 != VECTOR_CST_NPATTERNS (t2))
3224 return 0;
3226 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3227 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3228 return 0;
3230 unsigned int count = vector_cst_encoded_nelts (t1);
3231 for (unsigned int i = 0; i < count; ++i)
3232 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3233 VECTOR_CST_ENCODED_ELT (t2, i)))
3234 return 0;
3236 return 1;
3239 case CONSTRUCTOR:
3241 vec<constructor_elt, va_gc> *v1, *v2;
3242 unsigned HOST_WIDE_INT idx;
3244 typecode = TREE_CODE (TREE_TYPE (t1));
3245 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3246 return 0;
3248 if (typecode == ARRAY_TYPE)
3250 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3251 /* For arrays, check that mode, size and storage order match. */
3252 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3253 || size_1 == -1
3254 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3255 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3256 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3257 return 0;
3259 else
3261 /* For record and union constructors, require exact type
3262 equality. */
3263 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3264 return 0;
3267 v1 = CONSTRUCTOR_ELTS (t1);
3268 v2 = CONSTRUCTOR_ELTS (t2);
3269 if (vec_safe_length (v1) != vec_safe_length (v2))
3270 return 0;
3272 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3274 constructor_elt *c1 = &(*v1)[idx];
3275 constructor_elt *c2 = &(*v2)[idx];
3277 /* Check that each value is the same... */
3278 if (!compare_constant (c1->value, c2->value))
3279 return 0;
3280 /* ... and that they apply to the same fields! */
3281 if (typecode == ARRAY_TYPE)
3283 if (!compare_constant (c1->index, c2->index))
3284 return 0;
3286 else
3288 if (c1->index != c2->index)
3289 return 0;
3293 return 1;
3296 case ADDR_EXPR:
3297 case FDESC_EXPR:
3299 class addr_const value1, value2;
3300 enum rtx_code code;
3301 int ret;
3303 decode_addr_const (t1, &value1);
3304 decode_addr_const (t2, &value2);
3306 if (maybe_ne (value1.offset, value2.offset))
3307 return 0;
3309 code = GET_CODE (value1.base);
3310 if (code != GET_CODE (value2.base))
3311 return 0;
3313 switch (code)
3315 case SYMBOL_REF:
3316 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3317 break;
3319 case LABEL_REF:
3320 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3321 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3322 break;
3324 default:
3325 gcc_unreachable ();
3327 return ret;
3330 case PLUS_EXPR:
3331 case POINTER_PLUS_EXPR:
3332 case MINUS_EXPR:
3333 case RANGE_EXPR:
3334 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3335 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3337 CASE_CONVERT:
3338 case VIEW_CONVERT_EXPR:
3339 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3341 default:
3342 return 0;
3345 gcc_unreachable ();
3348 /* Return the section into which constant EXP should be placed. */
3350 static section *
3351 get_constant_section (tree exp, unsigned int align)
3353 return targetm.asm_out.select_section (exp,
3354 compute_reloc_for_constant (exp),
3355 align);
3358 /* Return the size of constant EXP in bytes. */
3360 static HOST_WIDE_INT
3361 get_constant_size (tree exp)
3363 HOST_WIDE_INT size;
3365 size = int_size_in_bytes (TREE_TYPE (exp));
3366 gcc_checking_assert (size >= 0);
3367 gcc_checking_assert (TREE_CODE (exp) != STRING_CST
3368 || size >= TREE_STRING_LENGTH (exp));
3369 return size;
3372 /* Subroutine of output_constant_def:
3373 No constant equal to EXP is known to have been output.
3374 Make a constant descriptor to enter EXP in the hash table.
3375 Assign the label number and construct RTL to refer to the
3376 constant's location in memory.
3377 Caller is responsible for updating the hash table. */
3379 static struct constant_descriptor_tree *
3380 build_constant_desc (tree exp)
3382 struct constant_descriptor_tree *desc;
3383 rtx symbol, rtl;
3384 char label[256];
3385 int labelno;
3386 tree decl;
3388 desc = ggc_alloc<constant_descriptor_tree> ();
3389 desc->value = exp;
3391 /* Create a string containing the label name, in LABEL. */
3392 labelno = const_labelno++;
3393 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3395 /* Construct the VAR_DECL associated with the constant. */
3396 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3397 TREE_TYPE (exp));
3398 DECL_ARTIFICIAL (decl) = 1;
3399 DECL_IGNORED_P (decl) = 1;
3400 TREE_READONLY (decl) = 1;
3401 TREE_STATIC (decl) = 1;
3402 TREE_ADDRESSABLE (decl) = 1;
3403 /* We don't set the RTL yet as this would cause varpool to assume that the
3404 variable is referenced. Moreover, it would just be dropped in LTO mode.
3405 Instead we set the flag that will be recognized in make_decl_rtl. */
3406 DECL_IN_CONSTANT_POOL (decl) = 1;
3407 DECL_INITIAL (decl) = desc->value;
3408 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3409 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3410 if (TREE_CODE (exp) == STRING_CST)
3411 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3412 else
3414 align_variable (decl, 0);
3415 if (DECL_ALIGN (decl) < GET_MODE_ALIGNMENT (DECL_MODE (decl))
3416 && ((optab_handler (movmisalign_optab, DECL_MODE (decl))
3417 != CODE_FOR_nothing)
3418 || targetm.slow_unaligned_access (DECL_MODE (decl),
3419 DECL_ALIGN (decl))))
3420 SET_DECL_ALIGN (decl, GET_MODE_ALIGNMENT (DECL_MODE (decl)));
3423 /* Now construct the SYMBOL_REF and the MEM. */
3424 if (use_object_blocks_p ())
3426 int align = (TREE_CODE (decl) == CONST_DECL
3427 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3428 ? DECL_ALIGN (decl)
3429 : symtab_node::get (decl)->definition_alignment ());
3430 section *sect = get_constant_section (exp, align);
3431 symbol = create_block_symbol (ggc_strdup (label),
3432 get_block_for_section (sect), -1);
3434 else
3435 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3436 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3437 SET_SYMBOL_REF_DECL (symbol, decl);
3438 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3440 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3441 set_mem_alias_set (rtl, 0);
3443 /* Putting EXP into the literal pool might have imposed a different
3444 alignment which should be visible in the RTX as well. */
3445 set_mem_align (rtl, DECL_ALIGN (decl));
3447 /* We cannot share RTX'es in pool entries.
3448 Mark this piece of RTL as required for unsharing. */
3449 RTX_FLAG (rtl, used) = 1;
3451 /* Set flags or add text to the name to record information, such as
3452 that it is a local symbol. If the name is changed, the macro
3453 ASM_OUTPUT_LABELREF will have to know how to strip this
3454 information. This call might invalidate our local variable
3455 SYMBOL; we can't use it afterward. */
3456 targetm.encode_section_info (exp, rtl, true);
3458 desc->rtl = rtl;
3460 return desc;
3463 /* Subroutine of output_constant_def and tree_output_constant_def:
3464 Add a constant to the hash table that tracks which constants
3465 already have labels. */
3467 static constant_descriptor_tree *
3468 add_constant_to_table (tree exp, int defer)
3470 /* The hash table methods may call output_constant_def for addressed
3471 constants, so handle them first. */
3472 output_addressed_constants (exp, defer);
3474 /* Sanity check to catch recursive insertion. */
3475 static bool inserting;
3476 gcc_assert (!inserting);
3477 inserting = true;
3479 /* Look up EXP in the table of constant descriptors. If we didn't
3480 find it, create a new one. */
3481 struct constant_descriptor_tree key;
3482 key.value = exp;
3483 key.hash = const_hash_1 (exp);
3484 constant_descriptor_tree **loc
3485 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3487 inserting = false;
3489 struct constant_descriptor_tree *desc = *loc;
3490 if (!desc)
3492 desc = build_constant_desc (exp);
3493 desc->hash = key.hash;
3494 *loc = desc;
3497 return desc;
3500 /* Return an rtx representing a reference to constant data in memory
3501 for the constant expression EXP.
3503 If assembler code for such a constant has already been output,
3504 return an rtx to refer to it.
3505 Otherwise, output such a constant in memory
3506 and generate an rtx for it.
3508 If DEFER is nonzero, this constant can be deferred and output only
3509 if referenced in the function after all optimizations.
3511 `const_desc_table' records which constants already have label strings. */
3514 output_constant_def (tree exp, int defer)
3516 struct constant_descriptor_tree *desc = add_constant_to_table (exp, defer);
3517 maybe_output_constant_def_contents (desc, defer);
3518 return desc->rtl;
3521 /* Subroutine of output_constant_def: Decide whether or not we need to
3522 output the constant DESC now, and if so, do it. */
3523 static void
3524 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3525 int defer)
3527 rtx symbol = XEXP (desc->rtl, 0);
3528 tree exp = desc->value;
3530 if (flag_syntax_only)
3531 return;
3533 if (TREE_ASM_WRITTEN (exp))
3534 /* Already output; don't do it again. */
3535 return;
3537 /* We can always defer constants as long as the context allows
3538 doing so. */
3539 if (defer)
3541 /* Increment n_deferred_constants if it exists. It needs to be at
3542 least as large as the number of constants actually referred to
3543 by the function. If it's too small we'll stop looking too early
3544 and fail to emit constants; if it's too large we'll only look
3545 through the entire function when we could have stopped earlier. */
3546 if (cfun)
3547 n_deferred_constants++;
3548 return;
3551 output_constant_def_contents (symbol);
3554 /* Subroutine of output_constant_def_contents. Output the definition
3555 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3556 constant's alignment in bits. */
3558 static void
3559 assemble_constant_contents (tree exp, const char *label, unsigned int align,
3560 bool merge_strings)
3562 HOST_WIDE_INT size;
3564 size = get_constant_size (exp);
3566 /* Do any machine/system dependent processing of the constant. */
3567 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3569 /* Output the value of EXP. */
3570 output_constant (exp, size, align, false, merge_strings);
3572 targetm.asm_out.decl_end ();
3575 /* We must output the constant data referred to by SYMBOL; do so. */
3577 static void
3578 output_constant_def_contents (rtx symbol)
3580 tree decl = SYMBOL_REF_DECL (symbol);
3581 tree exp = DECL_INITIAL (decl);
3582 bool asan_protected = false;
3584 /* Make sure any other constants whose addresses appear in EXP
3585 are assigned label numbers. */
3586 output_addressed_constants (exp, 0);
3588 /* We are no longer deferring this constant. */
3589 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3591 if ((flag_sanitize & SANITIZE_ADDRESS)
3592 && TREE_CODE (exp) == STRING_CST
3593 && asan_protect_global (exp))
3595 asan_protected = true;
3596 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3597 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3600 /* If the constant is part of an object block, make sure that the
3601 decl has been positioned within its block, but do not write out
3602 its definition yet. output_object_blocks will do that later. */
3603 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3604 place_block_symbol (symbol);
3605 else
3607 int align = (TREE_CODE (decl) == CONST_DECL
3608 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3609 ? DECL_ALIGN (decl)
3610 : symtab_node::get (decl)->definition_alignment ());
3611 section *sect = get_constant_section (exp, align);
3612 switch_to_section (sect);
3613 if (align > BITS_PER_UNIT)
3614 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3615 assemble_constant_contents (exp, XSTR (symbol, 0), align,
3616 (sect->common.flags & SECTION_MERGE)
3617 && (sect->common.flags & SECTION_STRINGS));
3618 if (asan_protected)
3620 HOST_WIDE_INT size = get_constant_size (exp);
3621 assemble_zeros (asan_red_zone_size (size));
3626 /* Look up EXP in the table of constant descriptors. Return the rtl
3627 if it has been emitted, else null. */
3630 lookup_constant_def (tree exp)
3632 struct constant_descriptor_tree key;
3634 key.value = exp;
3635 key.hash = const_hash_1 (exp);
3636 constant_descriptor_tree *desc
3637 = const_desc_htab->find_with_hash (&key, key.hash);
3639 return (desc ? desc->rtl : NULL_RTX);
3642 /* Return a tree representing a reference to constant data in memory
3643 for the constant expression EXP.
3645 This is the counterpart of output_constant_def at the Tree level. */
3647 tree
3648 tree_output_constant_def (tree exp)
3650 struct constant_descriptor_tree *desc = add_constant_to_table (exp, 1);
3651 tree decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3652 varpool_node::finalize_decl (decl);
3653 return decl;
3656 class GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3657 public:
3658 class constant_descriptor_rtx *next;
3659 rtx mem;
3660 rtx sym;
3661 rtx constant;
3662 HOST_WIDE_INT offset;
3663 hashval_t hash;
3664 fixed_size_mode mode;
3665 unsigned int align;
3666 int labelno;
3667 int mark;
3670 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3672 static hashval_t hash (constant_descriptor_rtx *);
3673 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3676 /* Used in the hash tables to avoid outputting the same constant
3677 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3678 are output once per function, not once per file. */
3679 /* ??? Only a few targets need per-function constant pools. Most
3680 can use one per-file pool. Should add a targetm bit to tell the
3681 difference. */
3683 struct GTY(()) rtx_constant_pool {
3684 /* Pointers to first and last constant in pool, as ordered by offset. */
3685 class constant_descriptor_rtx *first;
3686 class constant_descriptor_rtx *last;
3688 /* Hash facility for making memory-constants from constant rtl-expressions.
3689 It is used on RISC machines where immediate integer arguments and
3690 constant addresses are restricted so that such constants must be stored
3691 in memory. */
3692 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3694 /* Current offset in constant pool (does not include any
3695 machine-specific header). */
3696 HOST_WIDE_INT offset;
3699 /* Hash and compare functions for const_rtx_htab. */
3701 hashval_t
3702 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3704 return desc->hash;
3707 bool
3708 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3709 constant_descriptor_rtx *y)
3711 if (x->mode != y->mode)
3712 return 0;
3713 return rtx_equal_p (x->constant, y->constant);
3716 /* Hash one component of a constant. */
3718 static hashval_t
3719 const_rtx_hash_1 (const_rtx x)
3721 unsigned HOST_WIDE_INT hwi;
3722 machine_mode mode;
3723 enum rtx_code code;
3724 hashval_t h;
3725 int i;
3727 code = GET_CODE (x);
3728 mode = GET_MODE (x);
3729 h = (hashval_t) code * 1048573 + mode;
3731 switch (code)
3733 case CONST_INT:
3734 hwi = INTVAL (x);
3736 fold_hwi:
3738 int shift = sizeof (hashval_t) * CHAR_BIT;
3739 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3741 h ^= (hashval_t) hwi;
3742 for (i = 1; i < n; ++i)
3744 hwi >>= shift;
3745 h ^= (hashval_t) hwi;
3748 break;
3750 case CONST_WIDE_INT:
3751 hwi = 0;
3753 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3754 hwi ^= CONST_WIDE_INT_ELT (x, i);
3755 goto fold_hwi;
3758 case CONST_DOUBLE:
3759 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3761 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3762 goto fold_hwi;
3764 else
3765 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3766 break;
3768 case CONST_FIXED:
3769 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3770 break;
3772 case SYMBOL_REF:
3773 h ^= htab_hash_string (XSTR (x, 0));
3774 break;
3776 case LABEL_REF:
3777 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3778 break;
3780 case UNSPEC:
3781 case UNSPEC_VOLATILE:
3782 h = h * 251 + XINT (x, 1);
3783 break;
3785 default:
3786 break;
3789 return h;
3792 /* Compute a hash value for X, which should be a constant. */
3794 static hashval_t
3795 const_rtx_hash (rtx x)
3797 hashval_t h = 0;
3798 subrtx_iterator::array_type array;
3799 FOR_EACH_SUBRTX (iter, array, x, ALL)
3800 h = h * 509 + const_rtx_hash_1 (*iter);
3801 return h;
3805 /* Create and return a new rtx constant pool. */
3807 static struct rtx_constant_pool *
3808 create_constant_pool (void)
3810 struct rtx_constant_pool *pool;
3812 pool = ggc_alloc<rtx_constant_pool> ();
3813 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3814 pool->first = NULL;
3815 pool->last = NULL;
3816 pool->offset = 0;
3817 return pool;
3820 /* Initialize constant pool hashing for a new function. */
3822 void
3823 init_varasm_status (void)
3825 crtl->varasm.pool = create_constant_pool ();
3826 crtl->varasm.deferred_constants = 0;
3829 /* Given a MINUS expression, simplify it if both sides
3830 include the same symbol. */
3833 simplify_subtraction (rtx x)
3835 rtx r = simplify_rtx (x);
3836 return r ? r : x;
3839 /* Given a constant rtx X, make (or find) a memory constant for its value
3840 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
3841 of X. */
3844 force_const_mem (machine_mode in_mode, rtx x)
3846 class constant_descriptor_rtx *desc, tmp;
3847 struct rtx_constant_pool *pool;
3848 char label[256];
3849 rtx def, symbol;
3850 hashval_t hash;
3851 unsigned int align;
3852 constant_descriptor_rtx **slot;
3853 fixed_size_mode mode;
3855 /* We can't force variable-sized objects to memory. */
3856 if (!is_a <fixed_size_mode> (in_mode, &mode))
3857 return NULL_RTX;
3859 /* If we're not allowed to drop X into the constant pool, don't. */
3860 if (targetm.cannot_force_const_mem (mode, x))
3861 return NULL_RTX;
3863 /* Record that this function has used a constant pool entry. */
3864 crtl->uses_const_pool = 1;
3866 /* Decide which pool to use. */
3867 pool = (targetm.use_blocks_for_constant_p (mode, x)
3868 ? shared_constant_pool
3869 : crtl->varasm.pool);
3871 /* Lookup the value in the hashtable. */
3872 tmp.constant = x;
3873 tmp.mode = mode;
3874 hash = const_rtx_hash (x);
3875 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3876 desc = *slot;
3878 /* If the constant was already present, return its memory. */
3879 if (desc)
3880 return copy_rtx (desc->mem);
3882 /* Otherwise, create a new descriptor. */
3883 desc = ggc_alloc<constant_descriptor_rtx> ();
3884 *slot = desc;
3886 /* Align the location counter as required by EXP's data type. */
3887 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3888 align = targetm.static_rtx_alignment (align_mode);
3890 pool->offset += (align / BITS_PER_UNIT) - 1;
3891 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3893 desc->next = NULL;
3894 desc->constant = copy_rtx (tmp.constant);
3895 desc->offset = pool->offset;
3896 desc->hash = hash;
3897 desc->mode = mode;
3898 desc->align = align;
3899 desc->labelno = const_labelno;
3900 desc->mark = 0;
3902 pool->offset += GET_MODE_SIZE (mode);
3903 if (pool->last)
3904 pool->last->next = desc;
3905 else
3906 pool->first = pool->last = desc;
3907 pool->last = desc;
3909 /* Create a string containing the label name, in LABEL. */
3910 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3911 ++const_labelno;
3913 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3914 the constants pool. */
3915 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3917 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3918 symbol = create_block_symbol (ggc_strdup (label),
3919 get_block_for_section (sect), -1);
3921 else
3922 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3923 desc->sym = symbol;
3924 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3925 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3926 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3928 /* Construct the MEM. */
3929 desc->mem = def = gen_const_mem (mode, symbol);
3930 set_mem_align (def, align);
3932 /* If we're dropping a label to the constant pool, make sure we
3933 don't delete it. */
3934 if (GET_CODE (x) == LABEL_REF)
3935 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3937 return copy_rtx (def);
3940 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3943 get_pool_constant (const_rtx addr)
3945 return SYMBOL_REF_CONSTANT (addr)->constant;
3948 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3949 and whether it has been output or not. */
3952 get_pool_constant_mark (rtx addr, bool *pmarked)
3954 class constant_descriptor_rtx *desc;
3956 desc = SYMBOL_REF_CONSTANT (addr);
3957 *pmarked = (desc->mark != 0);
3958 return desc->constant;
3961 /* Similar, return the mode. */
3963 fixed_size_mode
3964 get_pool_mode (const_rtx addr)
3966 return SYMBOL_REF_CONSTANT (addr)->mode;
3969 /* Return TRUE if and only if the constant pool has no entries. Note
3970 that even entries we might end up choosing not to emit are counted
3971 here, so there is the potential for missed optimizations. */
3973 bool
3974 constant_pool_empty_p (void)
3976 return crtl->varasm.pool->first == NULL;
3979 /* Worker function for output_constant_pool_1. Emit assembly for X
3980 in MODE with known alignment ALIGN. */
3982 static void
3983 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
3985 switch (GET_MODE_CLASS (mode))
3987 case MODE_FLOAT:
3988 case MODE_DECIMAL_FLOAT:
3990 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3991 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
3992 as_a <scalar_float_mode> (mode), align, false);
3993 break;
3996 case MODE_INT:
3997 case MODE_PARTIAL_INT:
3998 case MODE_FRACT:
3999 case MODE_UFRACT:
4000 case MODE_ACCUM:
4001 case MODE_UACCUM:
4002 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
4003 break;
4005 case MODE_VECTOR_BOOL:
4007 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4009 /* Pick the smallest integer mode that contains at least one
4010 whole element. Often this is byte_mode and contains more
4011 than one element. */
4012 unsigned int nelts = GET_MODE_NUNITS (mode);
4013 unsigned int elt_bits = GET_MODE_BITSIZE (mode) / nelts;
4014 unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
4015 scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
4017 /* Build the constant up one integer at a time. */
4018 unsigned int elts_per_int = int_bits / elt_bits;
4019 for (unsigned int i = 0; i < nelts; i += elts_per_int)
4021 unsigned HOST_WIDE_INT value = 0;
4022 unsigned int limit = MIN (nelts - i, elts_per_int);
4023 for (unsigned int j = 0; j < limit; ++j)
4024 if (INTVAL (CONST_VECTOR_ELT (x, i + j)) != 0)
4025 value |= 1 << (j * elt_bits);
4026 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
4027 i != 0 ? MIN (align, int_bits) : align);
4029 break;
4031 case MODE_VECTOR_FLOAT:
4032 case MODE_VECTOR_INT:
4033 case MODE_VECTOR_FRACT:
4034 case MODE_VECTOR_UFRACT:
4035 case MODE_VECTOR_ACCUM:
4036 case MODE_VECTOR_UACCUM:
4038 int i, units;
4039 scalar_mode submode = GET_MODE_INNER (mode);
4040 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4042 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4043 units = GET_MODE_NUNITS (mode);
4045 for (i = 0; i < units; i++)
4047 rtx elt = CONST_VECTOR_ELT (x, i);
4048 output_constant_pool_2 (submode, elt, i ? subalign : align);
4051 break;
4053 default:
4054 gcc_unreachable ();
4058 /* Worker function for output_constant_pool. Emit constant DESC,
4059 giving it ALIGN bits of alignment. */
4061 static void
4062 output_constant_pool_1 (class constant_descriptor_rtx *desc,
4063 unsigned int align)
4065 rtx x, tmp;
4067 x = desc->constant;
4069 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4070 whose CODE_LABEL has been deleted. This can occur if a jump table
4071 is eliminated by optimization. If so, write a constant of zero
4072 instead. Note that this can also happen by turning the
4073 CODE_LABEL into a NOTE. */
4074 /* ??? This seems completely and utterly wrong. Certainly it's
4075 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4076 functioning even with rtx_insn::deleted and friends. */
4078 tmp = x;
4079 switch (GET_CODE (tmp))
4081 case CONST:
4082 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4083 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4084 break;
4085 tmp = XEXP (XEXP (tmp, 0), 0);
4086 /* FALLTHRU */
4088 case LABEL_REF:
4090 rtx_insn *insn = label_ref_label (tmp);
4091 gcc_assert (!insn->deleted ());
4092 gcc_assert (!NOTE_P (insn)
4093 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4094 break;
4097 default:
4098 break;
4101 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4102 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4103 align, desc->labelno, done);
4104 #endif
4106 assemble_align (align);
4108 /* Output the label. */
4109 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4111 /* Output the data.
4112 Pass actual alignment value while emitting string constant to asm code
4113 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4114 assuming that the data is already aligned which prevents the generation
4115 of fix-up table entries. */
4116 output_constant_pool_2 (desc->mode, x, desc->align);
4118 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4119 sections have proper size. */
4120 if (align > GET_MODE_BITSIZE (desc->mode)
4121 && in_section
4122 && (in_section->common.flags & SECTION_MERGE))
4123 assemble_align (align);
4125 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4126 done:
4127 #endif
4128 return;
4131 /* Recompute the offsets of entries in POOL, and the overall size of
4132 POOL. Do this after calling mark_constant_pool to ensure that we
4133 are computing the offset values for the pool which we will actually
4134 emit. */
4136 static void
4137 recompute_pool_offsets (struct rtx_constant_pool *pool)
4139 class constant_descriptor_rtx *desc;
4140 pool->offset = 0;
4142 for (desc = pool->first; desc ; desc = desc->next)
4143 if (desc->mark)
4145 /* Recalculate offset. */
4146 unsigned int align = desc->align;
4147 pool->offset += (align / BITS_PER_UNIT) - 1;
4148 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4149 desc->offset = pool->offset;
4150 pool->offset += GET_MODE_SIZE (desc->mode);
4154 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4155 Emit referenced deferred strings. */
4157 static void
4158 mark_constants_in_pattern (rtx insn)
4160 subrtx_iterator::array_type array;
4161 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4163 const_rtx x = *iter;
4164 if (GET_CODE (x) == SYMBOL_REF)
4166 if (CONSTANT_POOL_ADDRESS_P (x))
4168 class constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4169 if (desc->mark == 0)
4171 desc->mark = 1;
4172 iter.substitute (desc->constant);
4175 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4177 tree decl = SYMBOL_REF_DECL (x);
4178 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4180 n_deferred_constants--;
4181 output_constant_def_contents (CONST_CAST_RTX (x));
4188 /* Look through appropriate parts of INSN, marking all entries in the
4189 constant pool which are actually being used. Entries that are only
4190 referenced by other constants are also marked as used. Emit
4191 deferred strings that are used. */
4193 static void
4194 mark_constants (rtx_insn *insn)
4196 if (!INSN_P (insn))
4197 return;
4199 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4200 insns, not any notes that may be attached. We don't want to mark
4201 a constant just because it happens to appear in a REG_EQUIV note. */
4202 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4204 int i, n = seq->len ();
4205 for (i = 0; i < n; ++i)
4207 rtx subinsn = seq->element (i);
4208 if (INSN_P (subinsn))
4209 mark_constants_in_pattern (subinsn);
4212 else
4213 mark_constants_in_pattern (insn);
4216 /* Look through the instructions for this function, and mark all the
4217 entries in POOL which are actually being used. Emit deferred constants
4218 which have indeed been used. */
4220 static void
4221 mark_constant_pool (void)
4223 rtx_insn *insn;
4225 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4226 return;
4228 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4229 mark_constants (insn);
4232 /* Write all the constants in POOL. */
4234 static void
4235 output_constant_pool_contents (struct rtx_constant_pool *pool)
4237 class constant_descriptor_rtx *desc;
4239 for (desc = pool->first; desc ; desc = desc->next)
4240 if (desc->mark < 0)
4242 #ifdef ASM_OUTPUT_DEF
4243 const char *name = targetm.strip_name_encoding (XSTR (desc->sym, 0));
4244 char label[256];
4245 char buffer[256 + 32];
4246 const char *p;
4248 ASM_GENERATE_INTERNAL_LABEL (label, "LC", ~desc->mark);
4249 p = targetm.strip_name_encoding (label);
4250 if (desc->offset)
4252 sprintf (buffer, "%s+%ld", p, (long) (desc->offset));
4253 p = buffer;
4255 ASM_OUTPUT_DEF (asm_out_file, name, p);
4256 #else
4257 gcc_unreachable ();
4258 #endif
4260 else if (desc->mark)
4262 /* If the constant is part of an object_block, make sure that
4263 the constant has been positioned within its block, but do not
4264 write out its definition yet. output_object_blocks will do
4265 that later. */
4266 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4267 && SYMBOL_REF_BLOCK (desc->sym))
4268 place_block_symbol (desc->sym);
4269 else
4271 switch_to_section (targetm.asm_out.select_rtx_section
4272 (desc->mode, desc->constant, desc->align));
4273 output_constant_pool_1 (desc, desc->align);
4278 struct constant_descriptor_rtx_data {
4279 constant_descriptor_rtx *desc;
4280 target_unit *bytes;
4281 unsigned short size;
4282 unsigned short offset;
4283 unsigned int hash;
4286 /* qsort callback to sort constant_descriptor_rtx_data * vector by
4287 decreasing size. */
4289 static int
4290 constant_descriptor_rtx_data_cmp (const void *p1, const void *p2)
4292 constant_descriptor_rtx_data *const data1
4293 = *(constant_descriptor_rtx_data * const *) p1;
4294 constant_descriptor_rtx_data *const data2
4295 = *(constant_descriptor_rtx_data * const *) p2;
4296 if (data1->size > data2->size)
4297 return -1;
4298 if (data1->size < data2->size)
4299 return 1;
4300 if (data1->hash < data2->hash)
4301 return -1;
4302 gcc_assert (data1->hash > data2->hash);
4303 return 1;
4306 struct const_rtx_data_hasher : nofree_ptr_hash<constant_descriptor_rtx_data>
4308 static hashval_t hash (constant_descriptor_rtx_data *);
4309 static bool equal (constant_descriptor_rtx_data *,
4310 constant_descriptor_rtx_data *);
4313 /* Hash and compare functions for const_rtx_data_htab. */
4315 hashval_t
4316 const_rtx_data_hasher::hash (constant_descriptor_rtx_data *data)
4318 return data->hash;
4321 bool
4322 const_rtx_data_hasher::equal (constant_descriptor_rtx_data *x,
4323 constant_descriptor_rtx_data *y)
4325 if (x->hash != y->hash || x->size != y->size)
4326 return 0;
4327 unsigned int align1 = x->desc->align;
4328 unsigned int align2 = y->desc->align;
4329 unsigned int offset1 = (x->offset * BITS_PER_UNIT) & (align1 - 1);
4330 unsigned int offset2 = (y->offset * BITS_PER_UNIT) & (align2 - 1);
4331 if (offset1)
4332 align1 = least_bit_hwi (offset1);
4333 if (offset2)
4334 align2 = least_bit_hwi (offset2);
4335 if (align2 > align1)
4336 return 0;
4337 if (memcmp (x->bytes, y->bytes, x->size * sizeof (target_unit)) != 0)
4338 return 0;
4339 return 1;
4342 /* Attempt to optimize constant pool POOL. If it contains both CONST_VECTOR
4343 constants and scalar constants with the values of CONST_VECTOR elements,
4344 try to alias the scalar constants with the CONST_VECTOR elements. */
4346 static void
4347 optimize_constant_pool (struct rtx_constant_pool *pool)
4349 auto_vec<target_unit, 128> buffer;
4350 auto_vec<constant_descriptor_rtx_data *, 128> vec;
4351 object_allocator<constant_descriptor_rtx_data>
4352 data_pool ("constant_descriptor_rtx_data_pool");
4353 int idx = 0;
4354 size_t size = 0;
4355 for (constant_descriptor_rtx *desc = pool->first; desc; desc = desc->next)
4356 if (desc->mark > 0
4357 && ! (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4358 && SYMBOL_REF_BLOCK (desc->sym)))
4360 buffer.truncate (0);
4361 buffer.reserve (GET_MODE_SIZE (desc->mode));
4362 if (native_encode_rtx (desc->mode, desc->constant, buffer, 0,
4363 GET_MODE_SIZE (desc->mode)))
4365 constant_descriptor_rtx_data *data = data_pool.allocate ();
4366 data->desc = desc;
4367 data->bytes = NULL;
4368 data->size = GET_MODE_SIZE (desc->mode);
4369 data->offset = 0;
4370 data->hash = idx++;
4371 size += data->size;
4372 vec.safe_push (data);
4375 if (idx)
4377 vec.qsort (constant_descriptor_rtx_data_cmp);
4378 unsigned min_size = vec.last ()->size;
4379 target_unit *bytes = XNEWVEC (target_unit, size);
4380 unsigned int i;
4381 constant_descriptor_rtx_data *data;
4382 hash_table<const_rtx_data_hasher> * htab
4383 = new hash_table<const_rtx_data_hasher> (31);
4384 size = 0;
4385 FOR_EACH_VEC_ELT (vec, i, data)
4387 buffer.truncate (0);
4388 native_encode_rtx (data->desc->mode, data->desc->constant,
4389 buffer, 0, data->size);
4390 memcpy (bytes + size, buffer.address (), data->size);
4391 data->bytes = bytes + size;
4392 data->hash = iterative_hash (data->bytes,
4393 data->size * sizeof (target_unit), 0);
4394 size += data->size;
4395 constant_descriptor_rtx_data **slot
4396 = htab->find_slot_with_hash (data, data->hash, INSERT);
4397 if (*slot)
4399 data->desc->mark = ~(*slot)->desc->labelno;
4400 data->desc->offset = (*slot)->offset;
4402 else
4404 unsigned int sz = 1 << floor_log2 (data->size);
4406 *slot = data;
4407 for (sz >>= 1; sz >= min_size; sz >>= 1)
4408 for (unsigned off = 0; off + sz <= data->size; off += sz)
4410 constant_descriptor_rtx_data tmp;
4411 tmp.desc = data->desc;
4412 tmp.bytes = data->bytes + off;
4413 tmp.size = sz;
4414 tmp.offset = off;
4415 tmp.hash = iterative_hash (tmp.bytes,
4416 sz * sizeof (target_unit), 0);
4417 slot = htab->find_slot_with_hash (&tmp, tmp.hash, INSERT);
4418 if (*slot == NULL)
4420 *slot = data_pool.allocate ();
4421 **slot = tmp;
4426 delete htab;
4427 XDELETE (bytes);
4429 data_pool.release ();
4432 /* Mark all constants that are used in the current function, then write
4433 out the function's private constant pool. */
4435 static void
4436 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4437 tree fndecl ATTRIBUTE_UNUSED)
4439 struct rtx_constant_pool *pool = crtl->varasm.pool;
4441 /* It is possible for gcc to call force_const_mem and then to later
4442 discard the instructions which refer to the constant. In such a
4443 case we do not need to output the constant. */
4444 mark_constant_pool ();
4446 /* Having marked the constant pool entries we'll actually emit, we
4447 now need to rebuild the offset information, which may have become
4448 stale. */
4449 recompute_pool_offsets (pool);
4451 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4452 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4453 #endif
4455 output_constant_pool_contents (pool);
4457 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4458 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4459 #endif
4462 /* Write the contents of the shared constant pool. */
4464 void
4465 output_shared_constant_pool (void)
4467 if (optimize
4468 && TARGET_SUPPORTS_ALIASES)
4469 optimize_constant_pool (shared_constant_pool);
4471 output_constant_pool_contents (shared_constant_pool);
4474 /* Determine what kind of relocations EXP may need. */
4477 compute_reloc_for_constant (tree exp)
4479 int reloc = 0, reloc2;
4480 tree tem;
4482 switch (TREE_CODE (exp))
4484 case ADDR_EXPR:
4485 case FDESC_EXPR:
4486 /* Go inside any operations that get_inner_reference can handle and see
4487 if what's inside is a constant: no need to do anything here for
4488 addresses of variables or functions. */
4489 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4490 tem = TREE_OPERAND (tem, 0))
4493 if (TREE_CODE (tem) == MEM_REF
4494 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4496 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4497 break;
4500 if (!targetm.binds_local_p (tem))
4501 reloc |= 2;
4502 else
4503 reloc |= 1;
4504 break;
4506 case PLUS_EXPR:
4507 case POINTER_PLUS_EXPR:
4508 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4509 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4510 break;
4512 case MINUS_EXPR:
4513 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4514 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4515 /* The difference of two local labels is computable at link time. */
4516 if (reloc == 1 && reloc2 == 1)
4517 reloc = 0;
4518 else
4519 reloc |= reloc2;
4520 break;
4522 CASE_CONVERT:
4523 case VIEW_CONVERT_EXPR:
4524 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4525 break;
4527 case CONSTRUCTOR:
4529 unsigned HOST_WIDE_INT idx;
4530 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4531 if (tem != 0)
4532 reloc |= compute_reloc_for_constant (tem);
4534 break;
4536 default:
4537 break;
4539 return reloc;
4542 /* Find all the constants whose addresses are referenced inside of EXP,
4543 and make sure assembler code with a label has been output for each one.
4544 Indicate whether an ADDR_EXPR has been encountered. */
4546 static void
4547 output_addressed_constants (tree exp, int defer)
4549 tree tem;
4551 switch (TREE_CODE (exp))
4553 case ADDR_EXPR:
4554 case FDESC_EXPR:
4555 /* Go inside any operations that get_inner_reference can handle and see
4556 if what's inside is a constant: no need to do anything here for
4557 addresses of variables or functions. */
4558 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4559 tem = TREE_OPERAND (tem, 0))
4562 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4563 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4564 tem = DECL_INITIAL (tem);
4566 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4567 output_constant_def (tem, defer);
4569 if (TREE_CODE (tem) == MEM_REF)
4570 output_addressed_constants (TREE_OPERAND (tem, 0), defer);
4571 break;
4573 case PLUS_EXPR:
4574 case POINTER_PLUS_EXPR:
4575 case MINUS_EXPR:
4576 output_addressed_constants (TREE_OPERAND (exp, 1), defer);
4577 gcc_fallthrough ();
4579 CASE_CONVERT:
4580 case VIEW_CONVERT_EXPR:
4581 output_addressed_constants (TREE_OPERAND (exp, 0), defer);
4582 break;
4584 case CONSTRUCTOR:
4586 unsigned HOST_WIDE_INT idx;
4587 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4588 if (tem != 0)
4589 output_addressed_constants (tem, defer);
4591 break;
4593 default:
4594 break;
4598 /* Whether a constructor CTOR is a valid static constant initializer if all
4599 its elements are. This used to be internal to initializer_constant_valid_p
4600 and has been exposed to let other functions like categorize_ctor_elements
4601 evaluate the property while walking a constructor for other purposes. */
4603 bool
4604 constructor_static_from_elts_p (const_tree ctor)
4606 return (TREE_CONSTANT (ctor)
4607 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4608 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4609 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4612 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4613 tree *cache);
4615 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4616 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4617 which are valid when ENDTYPE is an integer of any size; in
4618 particular, this does not accept a pointer minus a constant. This
4619 returns null_pointer_node if the VALUE is an absolute constant
4620 which can be used to initialize a static variable. Otherwise it
4621 returns NULL. */
4623 static tree
4624 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4626 tree op0, op1;
4628 if (!INTEGRAL_TYPE_P (endtype))
4629 return NULL_TREE;
4631 op0 = TREE_OPERAND (value, 0);
4632 op1 = TREE_OPERAND (value, 1);
4634 /* Like STRIP_NOPS except allow the operand mode to widen. This
4635 works around a feature of fold that simplifies (int)(p1 - p2) to
4636 ((int)p1 - (int)p2) under the theory that the narrower operation
4637 is cheaper. */
4639 while (CONVERT_EXPR_P (op0)
4640 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4642 tree inner = TREE_OPERAND (op0, 0);
4643 if (inner == error_mark_node
4644 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4645 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4646 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4647 break;
4648 op0 = inner;
4651 while (CONVERT_EXPR_P (op1)
4652 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4654 tree inner = TREE_OPERAND (op1, 0);
4655 if (inner == error_mark_node
4656 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4657 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4658 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4659 break;
4660 op1 = inner;
4663 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4664 if (!op0)
4665 return NULL_TREE;
4667 op1 = initializer_constant_valid_p_1 (op1, endtype,
4668 cache ? cache + 2 : NULL);
4669 /* Both initializers must be known. */
4670 if (op1)
4672 if (op0 == op1
4673 && (op0 == null_pointer_node
4674 || TREE_CODE (value) == MINUS_EXPR))
4675 return null_pointer_node;
4677 /* Support differences between labels. */
4678 if (TREE_CODE (op0) == LABEL_DECL
4679 && TREE_CODE (op1) == LABEL_DECL)
4680 return null_pointer_node;
4682 if (TREE_CODE (op0) == STRING_CST
4683 && TREE_CODE (op1) == STRING_CST
4684 && operand_equal_p (op0, op1, 1))
4685 return null_pointer_node;
4688 return NULL_TREE;
4691 /* Helper function of initializer_constant_valid_p.
4692 Return nonzero if VALUE is a valid constant-valued expression
4693 for use in initializing a static variable; one that can be an
4694 element of a "constant" initializer.
4696 Return null_pointer_node if the value is absolute;
4697 if it is relocatable, return the variable that determines the relocation.
4698 We assume that VALUE has been folded as much as possible;
4699 therefore, we do not need to check for such things as
4700 arithmetic-combinations of integers.
4702 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4704 static tree
4705 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4707 tree ret;
4709 switch (TREE_CODE (value))
4711 case CONSTRUCTOR:
4712 if (constructor_static_from_elts_p (value))
4714 unsigned HOST_WIDE_INT idx;
4715 tree elt;
4716 bool absolute = true;
4718 if (cache && cache[0] == value)
4719 return cache[1];
4720 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4722 tree reloc;
4723 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4724 NULL);
4725 if (!reloc
4726 /* An absolute value is required with reverse SSO. */
4727 || (reloc != null_pointer_node
4728 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4729 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4731 if (cache)
4733 cache[0] = value;
4734 cache[1] = NULL_TREE;
4736 return NULL_TREE;
4738 if (reloc != null_pointer_node)
4739 absolute = false;
4741 /* For a non-absolute relocation, there is no single
4742 variable that can be "the variable that determines the
4743 relocation." */
4744 if (cache)
4746 cache[0] = value;
4747 cache[1] = absolute ? null_pointer_node : error_mark_node;
4749 return absolute ? null_pointer_node : error_mark_node;
4752 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4754 case INTEGER_CST:
4755 case VECTOR_CST:
4756 case REAL_CST:
4757 case FIXED_CST:
4758 case STRING_CST:
4759 case COMPLEX_CST:
4760 return null_pointer_node;
4762 case ADDR_EXPR:
4763 case FDESC_EXPR:
4765 tree op0 = staticp (TREE_OPERAND (value, 0));
4766 if (op0)
4768 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4769 to be a constant, this is old-skool offsetof-like nonsense. */
4770 if (TREE_CODE (op0) == INDIRECT_REF
4771 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4772 return null_pointer_node;
4773 /* Taking the address of a nested function involves a trampoline,
4774 unless we don't need or want one. */
4775 if (TREE_CODE (op0) == FUNCTION_DECL
4776 && DECL_STATIC_CHAIN (op0)
4777 && !TREE_NO_TRAMPOLINE (value))
4778 return NULL_TREE;
4779 /* "&{...}" requires a temporary to hold the constructed
4780 object. */
4781 if (TREE_CODE (op0) == CONSTRUCTOR)
4782 return NULL_TREE;
4784 return op0;
4787 case NON_LVALUE_EXPR:
4788 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4789 endtype, cache);
4791 case VIEW_CONVERT_EXPR:
4793 tree src = TREE_OPERAND (value, 0);
4794 tree src_type = TREE_TYPE (src);
4795 tree dest_type = TREE_TYPE (value);
4797 /* Allow view-conversions from aggregate to non-aggregate type only
4798 if the bit pattern is fully preserved afterwards; otherwise, the
4799 RTL expander won't be able to apply a subsequent transformation
4800 to the underlying constructor. */
4801 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4803 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4804 return initializer_constant_valid_p_1 (src, endtype, cache);
4805 else
4806 return NULL_TREE;
4809 /* Allow all other kinds of view-conversion. */
4810 return initializer_constant_valid_p_1 (src, endtype, cache);
4813 CASE_CONVERT:
4815 tree src = TREE_OPERAND (value, 0);
4816 tree src_type = TREE_TYPE (src);
4817 tree dest_type = TREE_TYPE (value);
4819 /* Allow conversions between pointer types, floating-point
4820 types, and offset types. */
4821 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4822 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4823 || (TREE_CODE (dest_type) == OFFSET_TYPE
4824 && TREE_CODE (src_type) == OFFSET_TYPE))
4825 return initializer_constant_valid_p_1 (src, endtype, cache);
4827 /* Allow length-preserving conversions between integer types. */
4828 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4829 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4830 return initializer_constant_valid_p_1 (src, endtype, cache);
4832 /* Allow conversions between other integer types only if
4833 explicit value. Don't allow sign-extension to a type larger
4834 than word and pointer, there aren't relocations that would
4835 allow to sign extend it to a wider type. */
4836 if (INTEGRAL_TYPE_P (dest_type)
4837 && INTEGRAL_TYPE_P (src_type)
4838 && (TYPE_UNSIGNED (src_type)
4839 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4840 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4841 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4843 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4844 if (inner == null_pointer_node)
4845 return null_pointer_node;
4846 break;
4849 /* Allow (int) &foo provided int is as wide as a pointer. */
4850 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4851 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4852 return initializer_constant_valid_p_1 (src, endtype, cache);
4854 /* Likewise conversions from int to pointers, but also allow
4855 conversions from 0. */
4856 if ((POINTER_TYPE_P (dest_type)
4857 || TREE_CODE (dest_type) == OFFSET_TYPE)
4858 && INTEGRAL_TYPE_P (src_type))
4860 if (TREE_CODE (src) == INTEGER_CST
4861 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4862 return null_pointer_node;
4863 if (integer_zerop (src))
4864 return null_pointer_node;
4865 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4866 return initializer_constant_valid_p_1 (src, endtype, cache);
4869 /* Allow conversions to struct or union types if the value
4870 inside is okay. */
4871 if (TREE_CODE (dest_type) == RECORD_TYPE
4872 || TREE_CODE (dest_type) == UNION_TYPE)
4873 return initializer_constant_valid_p_1 (src, endtype, cache);
4875 break;
4877 case POINTER_PLUS_EXPR:
4878 case PLUS_EXPR:
4879 /* Any valid floating-point constants will have been folded by now;
4880 with -frounding-math we hit this with addition of two constants. */
4881 if (TREE_CODE (endtype) == REAL_TYPE)
4882 return NULL_TREE;
4883 if (cache && cache[0] == value)
4884 return cache[1];
4885 if (! INTEGRAL_TYPE_P (endtype)
4886 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4888 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4889 tree valid0
4890 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4891 endtype, ncache);
4892 tree valid1
4893 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4894 endtype, ncache + 2);
4895 /* If either term is absolute, use the other term's relocation. */
4896 if (valid0 == null_pointer_node)
4897 ret = valid1;
4898 else if (valid1 == null_pointer_node)
4899 ret = valid0;
4900 /* Support narrowing pointer differences. */
4901 else
4902 ret = narrowing_initializer_constant_valid_p (value, endtype,
4903 ncache);
4905 else
4906 /* Support narrowing pointer differences. */
4907 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4908 if (cache)
4910 cache[0] = value;
4911 cache[1] = ret;
4913 return ret;
4915 case POINTER_DIFF_EXPR:
4916 case MINUS_EXPR:
4917 if (TREE_CODE (endtype) == REAL_TYPE)
4918 return NULL_TREE;
4919 if (cache && cache[0] == value)
4920 return cache[1];
4921 if (! INTEGRAL_TYPE_P (endtype)
4922 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4924 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4925 tree valid0
4926 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4927 endtype, ncache);
4928 tree valid1
4929 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4930 endtype, ncache + 2);
4931 /* Win if second argument is absolute. */
4932 if (valid1 == null_pointer_node)
4933 ret = valid0;
4934 /* Win if both arguments have the same relocation.
4935 Then the value is absolute. */
4936 else if (valid0 == valid1 && valid0 != 0)
4937 ret = null_pointer_node;
4938 /* Since GCC guarantees that string constants are unique in the
4939 generated code, a subtraction between two copies of the same
4940 constant string is absolute. */
4941 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4942 && valid1 && TREE_CODE (valid1) == STRING_CST
4943 && operand_equal_p (valid0, valid1, 1))
4944 ret = null_pointer_node;
4945 /* Support narrowing differences. */
4946 else
4947 ret = narrowing_initializer_constant_valid_p (value, endtype,
4948 ncache);
4950 else
4951 /* Support narrowing differences. */
4952 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4953 if (cache)
4955 cache[0] = value;
4956 cache[1] = ret;
4958 return ret;
4960 default:
4961 break;
4964 return NULL_TREE;
4967 /* Return nonzero if VALUE is a valid constant-valued expression
4968 for use in initializing a static variable; one that can be an
4969 element of a "constant" initializer.
4971 Return null_pointer_node if the value is absolute;
4972 if it is relocatable, return the variable that determines the relocation.
4973 We assume that VALUE has been folded as much as possible;
4974 therefore, we do not need to check for such things as
4975 arithmetic-combinations of integers. */
4976 tree
4977 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
4979 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
4981 /* An absolute value is required with reverse storage order. */
4982 if (reloc
4983 && reloc != null_pointer_node
4984 && reverse
4985 && !AGGREGATE_TYPE_P (endtype)
4986 && !VECTOR_TYPE_P (endtype))
4987 reloc = NULL_TREE;
4989 return reloc;
4992 /* Return true if VALUE is a valid constant-valued expression
4993 for use in initializing a static bit-field; one that can be
4994 an element of a "constant" initializer. */
4996 bool
4997 initializer_constant_valid_for_bitfield_p (tree value)
4999 /* For bitfields we support integer constants or possibly nested aggregates
5000 of such. */
5001 switch (TREE_CODE (value))
5003 case CONSTRUCTOR:
5005 unsigned HOST_WIDE_INT idx;
5006 tree elt;
5008 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
5009 if (!initializer_constant_valid_for_bitfield_p (elt))
5010 return false;
5011 return true;
5014 case INTEGER_CST:
5015 case REAL_CST:
5016 return true;
5018 case VIEW_CONVERT_EXPR:
5019 case NON_LVALUE_EXPR:
5020 return
5021 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
5023 default:
5024 break;
5027 return false;
5030 /* Check if a STRING_CST fits into the field.
5031 Tolerate only the case when the NUL termination
5032 does not fit into the field. */
5034 static bool
5035 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
5037 tree type = TREE_TYPE (string);
5038 tree eltype = TREE_TYPE (type);
5039 unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
5040 unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5041 int len = TREE_STRING_LENGTH (string);
5043 if (elts != 1 && elts != 2 && elts != 4)
5044 return false;
5045 if (len < 0 || len % elts != 0)
5046 return false;
5047 if (size < (unsigned)len)
5048 return false;
5049 if (mem_size != size)
5050 return false;
5051 return true;
5054 /* output_constructor outer state of relevance in recursive calls, typically
5055 for nested aggregate bitfields. */
5057 struct oc_outer_state {
5058 unsigned int bit_offset; /* current position in ... */
5059 int byte; /* ... the outer byte buffer. */
5062 static unsigned HOST_WIDE_INT
5063 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
5064 oc_outer_state *);
5066 /* Output assembler code for constant EXP, with no label.
5067 This includes the pseudo-op such as ".int" or ".byte", and a newline.
5068 Assumes output_addressed_constants has been done on EXP already.
5070 Generate at least SIZE bytes of assembler data, padding at the end
5071 with zeros if necessary. SIZE must always be specified. The returned
5072 value is the actual number of bytes of assembler data generated, which
5073 may be bigger than SIZE if the object contains a variable length field.
5075 SIZE is important for structure constructors,
5076 since trailing members may have been omitted from the constructor.
5077 It is also important for initialization of arrays from string constants
5078 since the full length of the string constant might not be wanted.
5079 It is also needed for initialization of unions, where the initializer's
5080 type is just one member, and that may not be as long as the union.
5082 There a case in which we would fail to output exactly SIZE bytes:
5083 for a structure constructor that wants to produce more than SIZE bytes.
5084 But such constructors will never be generated for any possible input.
5086 ALIGN is the alignment of the data in bits.
5088 If REVERSE is true, EXP is output in reverse storage order. */
5090 static unsigned HOST_WIDE_INT
5091 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5092 bool reverse, bool merge_strings)
5094 enum tree_code code;
5095 unsigned HOST_WIDE_INT thissize;
5096 rtx cst;
5098 if (size == 0 || flag_syntax_only)
5099 return size;
5101 /* See if we're trying to initialize a pointer in a non-default mode
5102 to the address of some declaration somewhere. If the target says
5103 the mode is valid for pointers, assume the target has a way of
5104 resolving it. */
5105 if (TREE_CODE (exp) == NOP_EXPR
5106 && POINTER_TYPE_P (TREE_TYPE (exp))
5107 && targetm.addr_space.valid_pointer_mode
5108 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5109 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5111 tree saved_type = TREE_TYPE (exp);
5113 /* Peel off any intermediate conversions-to-pointer for valid
5114 pointer modes. */
5115 while (TREE_CODE (exp) == NOP_EXPR
5116 && POINTER_TYPE_P (TREE_TYPE (exp))
5117 && targetm.addr_space.valid_pointer_mode
5118 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5119 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5120 exp = TREE_OPERAND (exp, 0);
5122 /* If what we're left with is the address of something, we can
5123 convert the address to the final type and output it that
5124 way. */
5125 if (TREE_CODE (exp) == ADDR_EXPR)
5126 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
5127 /* Likewise for constant ints. */
5128 else if (TREE_CODE (exp) == INTEGER_CST)
5129 exp = fold_convert (saved_type, exp);
5133 /* Eliminate any conversions since we'll be outputting the underlying
5134 constant. */
5135 while (CONVERT_EXPR_P (exp)
5136 || TREE_CODE (exp) == NON_LVALUE_EXPR
5137 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
5139 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
5140 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
5142 /* Make sure eliminating the conversion is really a no-op, except with
5143 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
5144 union types to allow for Ada unchecked unions. */
5145 if (type_size > op_size
5146 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
5147 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
5148 /* Keep the conversion. */
5149 break;
5150 else
5151 exp = TREE_OPERAND (exp, 0);
5154 code = TREE_CODE (TREE_TYPE (exp));
5155 thissize = int_size_in_bytes (TREE_TYPE (exp));
5157 /* Allow a constructor with no elements for any data type.
5158 This means to fill the space with zeros. */
5159 if (TREE_CODE (exp) == CONSTRUCTOR
5160 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
5162 assemble_zeros (size);
5163 return size;
5166 if (TREE_CODE (exp) == FDESC_EXPR)
5168 #ifdef ASM_OUTPUT_FDESC
5169 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
5170 tree decl = TREE_OPERAND (exp, 0);
5171 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
5172 #else
5173 gcc_unreachable ();
5174 #endif
5175 return size;
5178 /* Now output the underlying data. If we've handling the padding, return.
5179 Otherwise, break and ensure SIZE is the size written. */
5180 switch (code)
5182 case BOOLEAN_TYPE:
5183 case INTEGER_TYPE:
5184 case ENUMERAL_TYPE:
5185 case POINTER_TYPE:
5186 case REFERENCE_TYPE:
5187 case OFFSET_TYPE:
5188 case FIXED_POINT_TYPE:
5189 case NULLPTR_TYPE:
5190 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5191 if (reverse)
5192 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5193 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5194 error ("initializer for integer/fixed-point value is too complicated");
5195 break;
5197 case REAL_TYPE:
5198 if (TREE_CODE (exp) != REAL_CST)
5199 error ("initializer for floating value is not a floating constant");
5200 else
5201 assemble_real (TREE_REAL_CST (exp),
5202 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
5203 align, reverse);
5204 break;
5206 case COMPLEX_TYPE:
5207 output_constant (TREE_REALPART (exp), thissize / 2, align,
5208 reverse, false);
5209 output_constant (TREE_IMAGPART (exp), thissize / 2,
5210 min_align (align, BITS_PER_UNIT * (thissize / 2)),
5211 reverse, false);
5212 break;
5214 case ARRAY_TYPE:
5215 case VECTOR_TYPE:
5216 switch (TREE_CODE (exp))
5218 case CONSTRUCTOR:
5219 return output_constructor (exp, size, align, reverse, NULL);
5220 case STRING_CST:
5221 thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5222 if (merge_strings
5223 && (thissize == 0
5224 || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5225 thissize++;
5226 gcc_checking_assert (check_string_literal (exp, size));
5227 assemble_string (TREE_STRING_POINTER (exp), thissize);
5228 break;
5229 case VECTOR_CST:
5231 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5232 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5233 int elt_size = GET_MODE_SIZE (inner);
5234 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5235 reverse, false);
5236 thissize = elt_size;
5237 /* Static constants must have a fixed size. */
5238 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5239 for (unsigned int i = 1; i < nunits; i++)
5241 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5242 reverse, false);
5243 thissize += elt_size;
5245 break;
5247 default:
5248 gcc_unreachable ();
5250 break;
5252 case RECORD_TYPE:
5253 case UNION_TYPE:
5254 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5255 return output_constructor (exp, size, align, reverse, NULL);
5257 case ERROR_MARK:
5258 return 0;
5260 default:
5261 gcc_unreachable ();
5264 if (size > thissize)
5265 assemble_zeros (size - thissize);
5267 return size;
5270 /* Subroutine of output_constructor, used for computing the size of
5271 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
5272 type with an unspecified upper bound. */
5274 static unsigned HOST_WIDE_INT
5275 array_size_for_constructor (tree val)
5277 tree max_index;
5278 unsigned HOST_WIDE_INT cnt;
5279 tree index, value, tmp;
5280 offset_int i;
5282 /* This code used to attempt to handle string constants that are not
5283 arrays of single-bytes, but nothing else does, so there's no point in
5284 doing it here. */
5285 if (TREE_CODE (val) == STRING_CST)
5286 return TREE_STRING_LENGTH (val);
5288 max_index = NULL_TREE;
5289 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5291 if (TREE_CODE (index) == RANGE_EXPR)
5292 index = TREE_OPERAND (index, 1);
5293 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5294 max_index = index;
5297 if (max_index == NULL_TREE)
5298 return 0;
5300 /* Compute the total number of array elements. */
5301 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5302 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5304 /* Multiply by the array element unit size to find number of bytes. */
5305 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5307 gcc_assert (wi::fits_uhwi_p (i));
5308 return i.to_uhwi ();
5311 /* Other datastructures + helpers for output_constructor. */
5313 /* output_constructor local state to support interaction with helpers. */
5315 struct oc_local_state {
5317 /* Received arguments. */
5318 tree exp; /* Constructor expression. */
5319 tree type; /* Type of constructor expression. */
5320 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5321 unsigned int align; /* Known initial alignment. */
5322 tree min_index; /* Lower bound if specified for an array. */
5324 /* Output processing state. */
5325 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5326 int byte; /* Part of a bitfield byte yet to be output. */
5327 int last_relative_index; /* Implicit or explicit index of the last
5328 array element output within a bitfield. */
5329 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5330 bool reverse; /* Whether reverse storage order is in use. */
5332 /* Current element. */
5333 tree field; /* Current field decl in a record. */
5334 tree val; /* Current element value. */
5335 tree index; /* Current element index. */
5339 /* Helper for output_constructor. From the current LOCAL state, output a
5340 RANGE_EXPR element. */
5342 static void
5343 output_constructor_array_range (oc_local_state *local)
5345 /* Perform the index calculation in modulo arithmetic but
5346 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5347 but we are using an unsigned sizetype. */
5348 unsigned prec = TYPE_PRECISION (sizetype);
5349 offset_int idx = wi::sext (wi::to_offset (TREE_OPERAND (local->index, 0))
5350 - wi::to_offset (local->min_index), prec);
5351 tree valtype = TREE_TYPE (local->val);
5352 HOST_WIDE_INT fieldpos
5353 = (idx * wi::to_offset (TYPE_SIZE_UNIT (valtype))).to_short_addr ();
5355 /* Advance to offset of this element. */
5356 if (fieldpos > local->total_bytes)
5358 assemble_zeros (fieldpos - local->total_bytes);
5359 local->total_bytes = fieldpos;
5361 else
5362 /* Must not go backwards. */
5363 gcc_assert (fieldpos == local->total_bytes);
5365 unsigned HOST_WIDE_INT fieldsize
5366 = int_size_in_bytes (TREE_TYPE (local->type));
5368 HOST_WIDE_INT lo_index
5369 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5370 HOST_WIDE_INT hi_index
5371 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5372 HOST_WIDE_INT index;
5374 unsigned int align2
5375 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5377 for (index = lo_index; index <= hi_index; index++)
5379 /* Output the element's initial value. */
5380 if (local->val == NULL_TREE)
5381 assemble_zeros (fieldsize);
5382 else
5383 fieldsize = output_constant (local->val, fieldsize, align2,
5384 local->reverse, false);
5386 /* Count its size. */
5387 local->total_bytes += fieldsize;
5391 /* Helper for output_constructor. From the current LOCAL state, output a
5392 field element that is not true bitfield or part of an outer one. */
5394 static void
5395 output_constructor_regular_field (oc_local_state *local)
5397 /* Field size and position. Since this structure is static, we know the
5398 positions are constant. */
5399 unsigned HOST_WIDE_INT fieldsize;
5400 HOST_WIDE_INT fieldpos;
5402 unsigned int align2;
5404 /* Output any buffered-up bit-fields preceding this element. */
5405 if (local->byte_buffer_in_use)
5407 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5408 local->total_bytes++;
5409 local->byte_buffer_in_use = false;
5412 if (local->index != NULL_TREE)
5414 /* Perform the index calculation in modulo arithmetic but
5415 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5416 but we are using an unsigned sizetype. */
5417 unsigned prec = TYPE_PRECISION (sizetype);
5418 offset_int idx = wi::sext (wi::to_offset (local->index)
5419 - wi::to_offset (local->min_index), prec);
5420 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5421 .to_short_addr ();
5423 else if (local->field != NULL_TREE)
5424 fieldpos = int_byte_position (local->field);
5425 else
5426 fieldpos = 0;
5428 /* Advance to offset of this element.
5429 Note no alignment needed in an array, since that is guaranteed
5430 if each element has the proper size. */
5431 if (local->field != NULL_TREE || local->index != NULL_TREE)
5433 if (fieldpos > local->total_bytes)
5435 assemble_zeros (fieldpos - local->total_bytes);
5436 local->total_bytes = fieldpos;
5438 else
5439 /* Must not go backwards. */
5440 gcc_assert (fieldpos == local->total_bytes);
5443 /* Find the alignment of this element. */
5444 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5446 /* Determine size this element should occupy. */
5447 if (local->field)
5449 fieldsize = 0;
5451 /* If this is an array with an unspecified upper bound,
5452 the initializer determines the size. */
5453 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5454 but we cannot do this until the deprecated support for
5455 initializing zero-length array members is removed. */
5456 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5457 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5458 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5460 fieldsize = array_size_for_constructor (local->val);
5461 /* Given a non-empty initialization, this field had better
5462 be last. Given a flexible array member, the next field
5463 on the chain is a TYPE_DECL of the enclosing struct. */
5464 const_tree next = DECL_CHAIN (local->field);
5465 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5466 tree size = TYPE_SIZE_UNIT (TREE_TYPE (local->val));
5467 gcc_checking_assert (compare_tree_int (size, fieldsize) == 0);
5469 else
5470 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5472 else
5473 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5475 /* Output the element's initial value. */
5476 if (local->val == NULL_TREE)
5477 assemble_zeros (fieldsize);
5478 else
5479 fieldsize = output_constant (local->val, fieldsize, align2,
5480 local->reverse, false);
5482 /* Count its size. */
5483 local->total_bytes += fieldsize;
5486 /* Helper for output_constructor. From the LOCAL state, output an element
5487 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5488 from the start of a possibly ongoing outer byte buffer. */
5490 static void
5491 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5493 /* Bit size of this element. */
5494 HOST_WIDE_INT ebitsize
5495 = (local->field
5496 ? tree_to_uhwi (DECL_SIZE (local->field))
5497 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5499 /* Relative index of this element if this is an array component. */
5500 HOST_WIDE_INT relative_index
5501 = (!local->field
5502 ? (local->index
5503 ? (tree_to_shwi (local->index)
5504 - tree_to_shwi (local->min_index))
5505 : local->last_relative_index + 1)
5506 : 0);
5508 /* Bit position of this element from the start of the containing
5509 constructor. */
5510 HOST_WIDE_INT constructor_relative_ebitpos
5511 = (local->field
5512 ? int_bit_position (local->field)
5513 : ebitsize * relative_index);
5515 /* Bit position of this element from the start of a possibly ongoing
5516 outer byte buffer. */
5517 HOST_WIDE_INT byte_relative_ebitpos
5518 = bit_offset + constructor_relative_ebitpos;
5520 /* From the start of a possibly ongoing outer byte buffer, offsets to
5521 the first bit of this element and to the first bit past the end of
5522 this element. */
5523 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5524 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5526 local->last_relative_index = relative_index;
5528 if (local->val == NULL_TREE)
5529 local->val = integer_zero_node;
5531 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5532 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5533 local->val = TREE_OPERAND (local->val, 0);
5535 if (TREE_CODE (local->val) != INTEGER_CST
5536 && TREE_CODE (local->val) != CONSTRUCTOR)
5538 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5539 return;
5542 /* If this field does not start in this (or next) byte, skip some bytes. */
5543 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5545 /* Output remnant of any bit field in previous bytes. */
5546 if (local->byte_buffer_in_use)
5548 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5549 local->total_bytes++;
5550 local->byte_buffer_in_use = false;
5553 /* If still not at proper byte, advance to there. */
5554 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5556 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5557 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5558 local->total_bytes = next_offset / BITS_PER_UNIT;
5562 /* Set up the buffer if necessary. */
5563 if (!local->byte_buffer_in_use)
5565 local->byte = 0;
5566 if (ebitsize > 0)
5567 local->byte_buffer_in_use = true;
5570 /* If this is nested constructor, recurse passing the bit offset and the
5571 pending data, then retrieve the new pending data afterwards. */
5572 if (TREE_CODE (local->val) == CONSTRUCTOR)
5574 oc_outer_state temp_state;
5575 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5576 temp_state.byte = local->byte;
5577 local->total_bytes
5578 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5579 local->byte = temp_state.byte;
5580 return;
5583 /* Otherwise, we must split the element into pieces that fall within
5584 separate bytes, and combine each byte with previous or following
5585 bit-fields. */
5586 while (next_offset < end_offset)
5588 int this_time;
5589 int shift;
5590 unsigned HOST_WIDE_INT value;
5591 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5592 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5594 /* Advance from byte to byte within this element when necessary. */
5595 while (next_byte != local->total_bytes)
5597 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5598 local->total_bytes++;
5599 local->byte = 0;
5602 /* Number of bits we can process at once (all part of the same byte). */
5603 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5604 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5606 /* For big-endian data, take the most significant bits (of the
5607 bits that are significant) first and put them into bytes from
5608 the most significant end. */
5609 shift = end_offset - next_offset - this_time;
5611 /* Don't try to take a bunch of bits that cross
5612 the word boundary in the INTEGER_CST. We can
5613 only select bits from one element. */
5614 if ((shift / HOST_BITS_PER_WIDE_INT)
5615 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5617 const int end = shift + this_time - 1;
5618 shift = end & -HOST_BITS_PER_WIDE_INT;
5619 this_time = end - shift + 1;
5622 /* Now get the bits we want to insert. */
5623 value = wi::extract_uhwi (wi::to_widest (local->val),
5624 shift, this_time);
5626 /* Get the result. This works only when:
5627 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5628 local->byte |= value << (BITS_PER_UNIT - this_time - next_bit);
5630 else
5632 /* On little-endian machines, take the least significant bits of
5633 the value first and pack them starting at the least significant
5634 bits of the bytes. */
5635 shift = next_offset - byte_relative_ebitpos;
5637 /* Don't try to take a bunch of bits that cross
5638 the word boundary in the INTEGER_CST. We can
5639 only select bits from one element. */
5640 if ((shift / HOST_BITS_PER_WIDE_INT)
5641 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5642 this_time
5643 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5645 /* Now get the bits we want to insert. */
5646 value = wi::extract_uhwi (wi::to_widest (local->val),
5647 shift, this_time);
5649 /* Get the result. This works only when:
5650 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5651 local->byte |= value << next_bit;
5654 next_offset += this_time;
5655 local->byte_buffer_in_use = true;
5659 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5660 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5661 caller output state of relevance in recursive invocations. */
5663 static unsigned HOST_WIDE_INT
5664 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5665 bool reverse, oc_outer_state *outer)
5667 unsigned HOST_WIDE_INT cnt;
5668 constructor_elt *ce;
5669 oc_local_state local;
5671 /* Setup our local state to communicate with helpers. */
5672 local.exp = exp;
5673 local.type = TREE_TYPE (exp);
5674 local.size = size;
5675 local.align = align;
5676 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5677 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5678 else
5679 local.min_index = integer_zero_node;
5681 local.total_bytes = 0;
5682 local.byte_buffer_in_use = outer != NULL;
5683 local.byte = outer ? outer->byte : 0;
5684 local.last_relative_index = -1;
5685 /* The storage order is specified for every aggregate type. */
5686 if (AGGREGATE_TYPE_P (local.type))
5687 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5688 else
5689 local.reverse = reverse;
5691 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5693 /* As CE goes through the elements of the constant, FIELD goes through the
5694 structure fields if the constant is a structure. If the constant is a
5695 union, we override this by getting the field from the TREE_LIST element.
5696 But the constant could also be an array. Then FIELD is zero.
5698 There is always a maximum of one element in the chain LINK for unions
5699 (even if the initializer in a source program incorrectly contains
5700 more one). */
5702 if (TREE_CODE (local.type) == RECORD_TYPE)
5703 local.field = TYPE_FIELDS (local.type);
5704 else
5705 local.field = NULL_TREE;
5707 for (cnt = 0;
5708 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5709 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5711 local.val = ce->value;
5712 local.index = NULL_TREE;
5714 /* The element in a union constructor specifies the proper field
5715 or index. */
5716 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5717 local.field = ce->index;
5719 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5720 local.index = ce->index;
5722 if (local.field && flag_verbose_asm)
5723 fprintf (asm_out_file, "%s %s:\n",
5724 ASM_COMMENT_START,
5725 DECL_NAME (local.field)
5726 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5727 : "<anonymous>");
5729 /* Eliminate the marker that makes a cast not be an lvalue. */
5730 if (local.val != NULL_TREE)
5731 STRIP_NOPS (local.val);
5733 /* Output the current element, using the appropriate helper ... */
5735 /* For an array slice not part of an outer bitfield. */
5736 if (!outer
5737 && local.index != NULL_TREE
5738 && TREE_CODE (local.index) == RANGE_EXPR)
5739 output_constructor_array_range (&local);
5741 /* For a field that is neither a true bitfield nor part of an outer one,
5742 known to be at least byte aligned and multiple-of-bytes long. */
5743 else if (!outer
5744 && (local.field == NULL_TREE
5745 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5746 output_constructor_regular_field (&local);
5748 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5749 supported for scalar fields, so we may need to convert first. */
5750 else
5752 if (TREE_CODE (local.val) == REAL_CST)
5753 local.val
5754 = fold_unary (VIEW_CONVERT_EXPR,
5755 build_nonstandard_integer_type
5756 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5757 local.val);
5758 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5762 /* If we are not at toplevel, save the pending data for our caller.
5763 Otherwise output the pending data and padding zeros as needed. */
5764 if (outer)
5765 outer->byte = local.byte;
5766 else
5768 if (local.byte_buffer_in_use)
5770 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5771 local.total_bytes++;
5774 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5776 assemble_zeros (local.size - local.total_bytes);
5777 local.total_bytes = local.size;
5781 return local.total_bytes;
5784 /* Mark DECL as weak. */
5786 static void
5787 mark_weak (tree decl)
5789 if (DECL_WEAK (decl))
5790 return;
5792 struct symtab_node *n = symtab_node::get (decl);
5793 if (n && n->refuse_visibility_changes)
5794 error ("%+qD declared weak after being used", decl);
5795 DECL_WEAK (decl) = 1;
5797 if (DECL_RTL_SET_P (decl)
5798 && MEM_P (DECL_RTL (decl))
5799 && XEXP (DECL_RTL (decl), 0)
5800 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5801 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5804 /* Merge weak status between NEWDECL and OLDDECL. */
5806 void
5807 merge_weak (tree newdecl, tree olddecl)
5809 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5811 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5813 tree *pwd;
5814 /* We put the NEWDECL on the weak_decls list at some point
5815 and OLDDECL as well. Keep just OLDDECL on the list. */
5816 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5817 if (TREE_VALUE (*pwd) == newdecl)
5819 *pwd = TREE_CHAIN (*pwd);
5820 break;
5823 return;
5826 if (DECL_WEAK (newdecl))
5828 tree wd;
5830 /* NEWDECL is weak, but OLDDECL is not. */
5832 /* If we already output the OLDDECL, we're in trouble; we can't
5833 go back and make it weak. This should never happen in
5834 unit-at-a-time compilation. */
5835 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5837 /* If we've already generated rtl referencing OLDDECL, we may
5838 have done so in a way that will not function properly with
5839 a weak symbol. Again in unit-at-a-time this should be
5840 impossible. */
5841 gcc_assert (!TREE_USED (olddecl)
5842 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5844 /* PR 49899: You cannot convert a static function into a weak, public function. */
5845 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
5846 error ("weak declaration of %q+D being applied to a already "
5847 "existing, static definition", newdecl);
5849 if (TARGET_SUPPORTS_WEAK)
5851 /* We put the NEWDECL on the weak_decls list at some point.
5852 Replace it with the OLDDECL. */
5853 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5854 if (TREE_VALUE (wd) == newdecl)
5856 TREE_VALUE (wd) = olddecl;
5857 break;
5859 /* We may not find the entry on the list. If NEWDECL is a
5860 weak alias, then we will have already called
5861 globalize_decl to remove the entry; in that case, we do
5862 not need to do anything. */
5865 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5866 mark_weak (olddecl);
5868 else
5869 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5870 weak. Just update NEWDECL to indicate that it's weak too. */
5871 mark_weak (newdecl);
5874 /* Declare DECL to be a weak symbol. */
5876 void
5877 declare_weak (tree decl)
5879 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5880 if (! TREE_PUBLIC (decl))
5882 error ("weak declaration of %q+D must be public", decl);
5883 return;
5885 else if (!TARGET_SUPPORTS_WEAK)
5886 warning (0, "weak declaration of %q+D not supported", decl);
5888 mark_weak (decl);
5889 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5890 DECL_ATTRIBUTES (decl)
5891 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5894 static void
5895 weak_finish_1 (tree decl)
5897 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5898 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5899 #endif
5901 if (! TREE_USED (decl))
5902 return;
5904 #ifdef ASM_WEAKEN_DECL
5905 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5906 #else
5907 #ifdef ASM_WEAKEN_LABEL
5908 ASM_WEAKEN_LABEL (asm_out_file, name);
5909 #else
5910 #ifdef ASM_OUTPUT_WEAK_ALIAS
5912 static bool warn_once = 0;
5913 if (! warn_once)
5915 warning (0, "only weak aliases are supported in this configuration");
5916 warn_once = 1;
5918 return;
5920 #endif
5921 #endif
5922 #endif
5925 /* Fiven an assembly name, find the decl it is associated with. */
5926 static tree
5927 find_decl (tree target)
5929 symtab_node *node = symtab_node::get_for_asmname (target);
5930 if (node)
5931 return node->decl;
5932 return NULL_TREE;
5935 /* This TREE_LIST contains weakref targets. */
5937 static GTY(()) tree weakref_targets;
5939 /* Emit any pending weak declarations. */
5941 void
5942 weak_finish (void)
5944 tree t;
5946 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5948 tree alias_decl = TREE_PURPOSE (t);
5949 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5951 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
5952 || TREE_SYMBOL_REFERENCED (target))
5953 /* Remove alias_decl from the weak list, but leave entries for
5954 the target alone. */
5955 target = NULL_TREE;
5956 #ifndef ASM_OUTPUT_WEAKREF
5957 else if (! TREE_SYMBOL_REFERENCED (target))
5959 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5960 defined, otherwise we and weak_finish_1 would use
5961 different macros. */
5962 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5963 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5964 # else
5965 tree decl = find_decl (target);
5967 if (! decl)
5969 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5970 TREE_CODE (alias_decl), target,
5971 TREE_TYPE (alias_decl));
5973 DECL_EXTERNAL (decl) = 1;
5974 TREE_PUBLIC (decl) = 1;
5975 DECL_ARTIFICIAL (decl) = 1;
5976 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5977 TREE_USED (decl) = 1;
5980 weak_finish_1 (decl);
5981 # endif
5983 #endif
5986 tree *p;
5987 tree t2;
5989 /* Remove the alias and the target from the pending weak list
5990 so that we do not emit any .weak directives for the former,
5991 nor multiple .weak directives for the latter. */
5992 for (p = &weak_decls; (t2 = *p) ; )
5994 if (TREE_VALUE (t2) == alias_decl
5995 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5996 *p = TREE_CHAIN (t2);
5997 else
5998 p = &TREE_CHAIN (t2);
6001 /* Remove other weakrefs to the same target, to speed things up. */
6002 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
6004 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
6005 *p = TREE_CHAIN (t2);
6006 else
6007 p = &TREE_CHAIN (t2);
6012 for (t = weak_decls; t; t = TREE_CHAIN (t))
6014 tree decl = TREE_VALUE (t);
6016 weak_finish_1 (decl);
6020 /* Emit the assembly bits to indicate that DECL is globally visible. */
6022 static void
6023 globalize_decl (tree decl)
6026 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
6027 if (DECL_WEAK (decl))
6029 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6030 tree *p, t;
6032 #ifdef ASM_WEAKEN_DECL
6033 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
6034 #else
6035 ASM_WEAKEN_LABEL (asm_out_file, name);
6036 #endif
6038 /* Remove this function from the pending weak list so that
6039 we do not emit multiple .weak directives for it. */
6040 for (p = &weak_decls; (t = *p) ; )
6042 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6043 *p = TREE_CHAIN (t);
6044 else
6045 p = &TREE_CHAIN (t);
6048 /* Remove weakrefs to the same target from the pending weakref
6049 list, for the same reason. */
6050 for (p = &weakref_targets; (t = *p) ; )
6052 if (DECL_ASSEMBLER_NAME (decl)
6053 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6054 *p = TREE_CHAIN (t);
6055 else
6056 p = &TREE_CHAIN (t);
6059 return;
6061 #endif
6063 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
6066 vec<alias_pair, va_gc> *alias_pairs;
6068 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
6069 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
6070 tree node is DECL to have the value of the tree node TARGET. */
6072 void
6073 do_assemble_alias (tree decl, tree target)
6075 tree id;
6077 /* Emulated TLS had better not get this var. */
6078 gcc_assert (!(!targetm.have_tls
6079 && VAR_P (decl)
6080 && DECL_THREAD_LOCAL_P (decl)));
6082 if (TREE_ASM_WRITTEN (decl))
6083 return;
6085 id = DECL_ASSEMBLER_NAME (decl);
6086 ultimate_transparent_alias_target (&id);
6087 ultimate_transparent_alias_target (&target);
6089 /* We must force creation of DECL_RTL for debug info generation, even though
6090 we don't use it here. */
6091 make_decl_rtl (decl);
6093 TREE_ASM_WRITTEN (decl) = 1;
6094 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
6095 TREE_ASM_WRITTEN (id) = 1;
6097 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6099 if (!TREE_SYMBOL_REFERENCED (target))
6100 weakref_targets = tree_cons (decl, target, weakref_targets);
6102 #ifdef ASM_OUTPUT_WEAKREF
6103 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
6104 IDENTIFIER_POINTER (id),
6105 IDENTIFIER_POINTER (target));
6106 #else
6107 if (!TARGET_SUPPORTS_WEAK)
6109 error_at (DECL_SOURCE_LOCATION (decl),
6110 "weakref is not supported in this configuration");
6111 return;
6113 #endif
6114 return;
6117 #ifdef ASM_OUTPUT_DEF
6118 tree orig_decl = decl;
6120 /* Make name accessible from other files, if appropriate. */
6122 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
6124 globalize_decl (decl);
6125 maybe_assemble_visibility (decl);
6127 if (TREE_CODE (decl) == FUNCTION_DECL
6128 && cgraph_node::get (decl)->ifunc_resolver)
6130 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
6131 if (targetm.has_ifunc_p ())
6132 ASM_OUTPUT_TYPE_DIRECTIVE
6133 (asm_out_file, IDENTIFIER_POINTER (id),
6134 IFUNC_ASM_TYPE);
6135 else
6136 #endif
6137 error_at (DECL_SOURCE_LOCATION (decl),
6138 "%qs is not supported on this target", "ifunc");
6141 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
6142 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
6143 # else
6144 ASM_OUTPUT_DEF (asm_out_file,
6145 IDENTIFIER_POINTER (id),
6146 IDENTIFIER_POINTER (target));
6147 # endif
6148 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
6150 const char *name;
6151 tree *p, t;
6153 name = IDENTIFIER_POINTER (id);
6154 # ifdef ASM_WEAKEN_DECL
6155 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
6156 # else
6157 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
6158 # endif
6159 /* Remove this function from the pending weak list so that
6160 we do not emit multiple .weak directives for it. */
6161 for (p = &weak_decls; (t = *p) ; )
6162 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
6163 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6164 *p = TREE_CHAIN (t);
6165 else
6166 p = &TREE_CHAIN (t);
6168 /* Remove weakrefs to the same target from the pending weakref
6169 list, for the same reason. */
6170 for (p = &weakref_targets; (t = *p) ; )
6172 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6173 *p = TREE_CHAIN (t);
6174 else
6175 p = &TREE_CHAIN (t);
6178 #endif
6181 /* Output .symver directive. */
6183 void
6184 do_assemble_symver (tree decl, tree target)
6186 tree id = DECL_ASSEMBLER_NAME (decl);
6187 ultimate_transparent_alias_target (&id);
6188 ultimate_transparent_alias_target (&target);
6189 #ifdef ASM_OUTPUT_SYMVER_DIRECTIVE
6190 ASM_OUTPUT_SYMVER_DIRECTIVE (asm_out_file,
6191 IDENTIFIER_POINTER (target),
6192 IDENTIFIER_POINTER (id));
6193 #else
6194 error ("symver is only supported on ELF platforms");
6195 #endif
6198 /* Emit an assembler directive to make the symbol for DECL an alias to
6199 the symbol for TARGET. */
6201 void
6202 assemble_alias (tree decl, tree target)
6204 tree target_decl;
6206 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6208 tree alias = DECL_ASSEMBLER_NAME (decl);
6210 ultimate_transparent_alias_target (&target);
6212 if (alias == target)
6213 error ("%qs symbol %q+D ultimately targets itself", "weakref", decl);
6214 if (TREE_PUBLIC (decl))
6215 error ("%qs symbol %q+D must have static linkage", "weakref", decl);
6217 else
6219 #if !defined (ASM_OUTPUT_DEF)
6220 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
6221 error_at (DECL_SOURCE_LOCATION (decl),
6222 "alias definitions not supported in this configuration");
6223 TREE_ASM_WRITTEN (decl) = 1;
6224 return;
6225 # else
6226 if (!DECL_WEAK (decl))
6228 /* NB: ifunc_resolver isn't set when an error is detected. */
6229 if (TREE_CODE (decl) == FUNCTION_DECL
6230 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
6231 error_at (DECL_SOURCE_LOCATION (decl),
6232 "%qs is not supported in this configuration", "ifunc");
6233 else
6234 error_at (DECL_SOURCE_LOCATION (decl),
6235 "only weak aliases are supported in this configuration");
6236 TREE_ASM_WRITTEN (decl) = 1;
6237 return;
6239 # endif
6240 #endif
6242 TREE_USED (decl) = 1;
6244 /* Allow aliases to aliases. */
6245 if (TREE_CODE (decl) == FUNCTION_DECL)
6246 cgraph_node::get_create (decl)->alias = true;
6247 else
6248 varpool_node::get_create (decl)->alias = true;
6250 /* If the target has already been emitted, we don't have to queue the
6251 alias. This saves a tad of memory. */
6252 if (symtab->global_info_ready)
6253 target_decl = find_decl (target);
6254 else
6255 target_decl= NULL;
6256 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6257 || symtab->state >= EXPANSION)
6258 do_assemble_alias (decl, target);
6259 else
6261 alias_pair p = {decl, target};
6262 vec_safe_push (alias_pairs, p);
6266 /* Record and output a table of translations from original function
6267 to its transaction aware clone. Note that tm_pure functions are
6268 considered to be their own clone. */
6270 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6272 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6273 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6275 static int
6276 keep_cache_entry (tree_map *&e)
6278 return ggc_marked_p (e->base.from);
6282 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6284 void
6285 record_tm_clone_pair (tree o, tree n)
6287 struct tree_map **slot, *h;
6289 if (tm_clone_hash == NULL)
6290 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6292 h = ggc_alloc<tree_map> ();
6293 h->hash = htab_hash_pointer (o);
6294 h->base.from = o;
6295 h->to = n;
6297 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6298 *slot = h;
6301 tree
6302 get_tm_clone_pair (tree o)
6304 if (tm_clone_hash)
6306 struct tree_map *h, in;
6308 in.base.from = o;
6309 in.hash = htab_hash_pointer (o);
6310 h = tm_clone_hash->find_with_hash (&in, in.hash);
6311 if (h)
6312 return h->to;
6314 return NULL_TREE;
6317 struct tm_alias_pair
6319 unsigned int uid;
6320 tree from;
6321 tree to;
6325 /* Dump the actual pairs to the .tm_clone_table section. */
6327 static void
6328 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6330 unsigned i;
6331 tm_alias_pair *p;
6332 bool switched = false;
6334 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6336 tree src = p->from;
6337 tree dst = p->to;
6338 struct cgraph_node *src_n = cgraph_node::get (src);
6339 struct cgraph_node *dst_n = cgraph_node::get (dst);
6341 /* The function ipa_tm_create_version() marks the clone as needed if
6342 the original function was needed. But we also mark the clone as
6343 needed if we ever called the clone indirectly through
6344 TM_GETTMCLONE. If neither of these are true, we didn't generate
6345 a clone, and we didn't call it indirectly... no sense keeping it
6346 in the clone table. */
6347 if (!dst_n || !dst_n->definition)
6348 continue;
6350 /* This covers the case where we have optimized the original
6351 function away, and only access the transactional clone. */
6352 if (!src_n || !src_n->definition)
6353 continue;
6355 if (!switched)
6357 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6358 assemble_align (POINTER_SIZE);
6359 switched = true;
6362 assemble_integer (XEXP (DECL_RTL (src), 0),
6363 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6364 assemble_integer (XEXP (DECL_RTL (dst), 0),
6365 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6369 /* Provide a default for the tm_clone_table section. */
6371 section *
6372 default_clone_table_section (void)
6374 return get_named_section (NULL, ".tm_clone_table", 3);
6377 /* Helper comparison function for qsorting by the DECL_UID stored in
6378 alias_pair->emitted_diags. */
6380 static int
6381 tm_alias_pair_cmp (const void *x, const void *y)
6383 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6384 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6385 if (p1->uid < p2->uid)
6386 return -1;
6387 if (p1->uid > p2->uid)
6388 return 1;
6389 return 0;
6392 void
6393 finish_tm_clone_pairs (void)
6395 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6397 if (tm_clone_hash == NULL)
6398 return;
6400 /* We need a determenistic order for the .tm_clone_table, otherwise
6401 we will get bootstrap comparison failures, so dump the hash table
6402 to a vector, sort it, and dump the vector. */
6404 /* Dump the hashtable to a vector. */
6405 tree_map *map;
6406 hash_table<tm_clone_hasher>::iterator iter;
6407 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6409 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6410 tm_alias_pairs.safe_push (p);
6412 /* Sort it. */
6413 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6415 /* Dump it. */
6416 dump_tm_clone_pairs (tm_alias_pairs);
6418 tm_clone_hash->empty ();
6419 tm_clone_hash = NULL;
6420 tm_alias_pairs.release ();
6424 /* Emit an assembler directive to set symbol for DECL visibility to
6425 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6427 void
6428 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6429 int vis ATTRIBUTE_UNUSED)
6431 #ifdef HAVE_GAS_HIDDEN
6432 static const char * const visibility_types[] = {
6433 NULL, "protected", "hidden", "internal"
6436 const char *name, *type;
6437 tree id;
6439 id = DECL_ASSEMBLER_NAME (decl);
6440 ultimate_transparent_alias_target (&id);
6441 name = IDENTIFIER_POINTER (id);
6443 type = visibility_types[vis];
6445 fprintf (asm_out_file, "\t.%s\t", type);
6446 assemble_name (asm_out_file, name);
6447 fprintf (asm_out_file, "\n");
6448 #else
6449 if (!DECL_ARTIFICIAL (decl))
6450 warning (OPT_Wattributes, "visibility attribute not supported "
6451 "in this configuration; ignored");
6452 #endif
6455 /* A helper function to call assemble_visibility when needed for a decl. */
6458 maybe_assemble_visibility (tree decl)
6460 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6461 if (vis != VISIBILITY_DEFAULT)
6463 targetm.asm_out.assemble_visibility (decl, vis);
6464 return 1;
6466 else
6467 return 0;
6470 /* Returns 1 if the target configuration supports defining public symbols
6471 so that one of them will be chosen at link time instead of generating a
6472 multiply-defined symbol error, whether through the use of weak symbols or
6473 a target-specific mechanism for having duplicates discarded. */
6476 supports_one_only (void)
6478 if (SUPPORTS_ONE_ONLY)
6479 return 1;
6480 return TARGET_SUPPORTS_WEAK;
6483 /* Set up DECL as a public symbol that can be defined in multiple
6484 translation units without generating a linker error. */
6486 void
6487 make_decl_one_only (tree decl, tree comdat_group)
6489 struct symtab_node *symbol;
6490 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6492 TREE_PUBLIC (decl) = 1;
6494 if (VAR_P (decl))
6495 symbol = varpool_node::get_create (decl);
6496 else
6497 symbol = cgraph_node::get_create (decl);
6499 if (SUPPORTS_ONE_ONLY)
6501 #ifdef MAKE_DECL_ONE_ONLY
6502 MAKE_DECL_ONE_ONLY (decl);
6503 #endif
6504 symbol->set_comdat_group (comdat_group);
6506 else if (VAR_P (decl)
6507 && (DECL_INITIAL (decl) == 0
6508 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6509 DECL_COMMON (decl) = 1;
6510 else
6512 gcc_assert (TARGET_SUPPORTS_WEAK);
6513 DECL_WEAK (decl) = 1;
6517 void
6518 init_varasm_once (void)
6520 section_htab = hash_table<section_hasher>::create_ggc (31);
6521 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6522 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6524 shared_constant_pool = create_constant_pool ();
6526 #ifdef TEXT_SECTION_ASM_OP
6527 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6528 TEXT_SECTION_ASM_OP);
6529 #endif
6531 #ifdef DATA_SECTION_ASM_OP
6532 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6533 DATA_SECTION_ASM_OP);
6534 #endif
6536 #ifdef SDATA_SECTION_ASM_OP
6537 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6538 SDATA_SECTION_ASM_OP);
6539 #endif
6541 #ifdef READONLY_DATA_SECTION_ASM_OP
6542 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6543 READONLY_DATA_SECTION_ASM_OP);
6544 #endif
6546 #ifdef CTORS_SECTION_ASM_OP
6547 ctors_section = get_unnamed_section (0, output_section_asm_op,
6548 CTORS_SECTION_ASM_OP);
6549 #endif
6551 #ifdef DTORS_SECTION_ASM_OP
6552 dtors_section = get_unnamed_section (0, output_section_asm_op,
6553 DTORS_SECTION_ASM_OP);
6554 #endif
6556 #ifdef BSS_SECTION_ASM_OP
6557 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6558 output_section_asm_op,
6559 BSS_SECTION_ASM_OP);
6560 #endif
6562 #ifdef SBSS_SECTION_ASM_OP
6563 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6564 output_section_asm_op,
6565 SBSS_SECTION_ASM_OP);
6566 #endif
6568 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6569 | SECTION_COMMON, emit_tls_common);
6570 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6571 | SECTION_COMMON, emit_local);
6572 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6573 | SECTION_COMMON, emit_common);
6575 #if defined ASM_OUTPUT_ALIGNED_BSS
6576 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6577 emit_bss);
6578 #endif
6580 targetm.asm_out.init_sections ();
6582 if (readonly_data_section == NULL)
6583 readonly_data_section = text_section;
6585 #ifdef ASM_OUTPUT_EXTERNAL
6586 pending_assemble_externals_set = new hash_set<tree>;
6587 #endif
6590 enum tls_model
6591 decl_default_tls_model (const_tree decl)
6593 enum tls_model kind;
6594 bool is_local;
6596 is_local = targetm.binds_local_p (decl);
6597 if (!flag_shlib)
6599 if (is_local)
6600 kind = TLS_MODEL_LOCAL_EXEC;
6601 else
6602 kind = TLS_MODEL_INITIAL_EXEC;
6605 /* Local dynamic is inefficient when we're not combining the
6606 parts of the address. */
6607 else if (optimize && is_local)
6608 kind = TLS_MODEL_LOCAL_DYNAMIC;
6609 else
6610 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6611 if (kind < flag_tls_default)
6612 kind = flag_tls_default;
6614 return kind;
6617 /* Select a set of attributes for section NAME based on the properties
6618 of DECL and whether or not RELOC indicates that DECL's initializer
6619 might contain runtime relocations.
6621 We make the section read-only and executable for a function decl,
6622 read-only for a const data decl, and writable for a non-const data decl. */
6624 unsigned int
6625 default_section_type_flags (tree decl, const char *name, int reloc)
6627 unsigned int flags;
6629 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6630 flags = SECTION_CODE;
6631 else if (decl)
6633 enum section_category category
6634 = categorize_decl_for_section (decl, reloc);
6635 if (decl_readonly_section_1 (category))
6636 flags = 0;
6637 else if (category == SECCAT_DATA_REL_RO
6638 || category == SECCAT_DATA_REL_RO_LOCAL)
6639 flags = SECTION_WRITE | SECTION_RELRO;
6640 else
6641 flags = SECTION_WRITE;
6643 else
6645 flags = SECTION_WRITE;
6646 if (strcmp (name, ".data.rel.ro") == 0
6647 || strcmp (name, ".data.rel.ro.local") == 0)
6648 flags |= SECTION_RELRO;
6651 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6652 flags |= SECTION_LINKONCE;
6654 if (strcmp (name, ".vtable_map_vars") == 0)
6655 flags |= SECTION_LINKONCE;
6657 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6658 flags |= SECTION_TLS | SECTION_WRITE;
6660 if (strcmp (name, ".bss") == 0
6661 || strncmp (name, ".bss.", 5) == 0
6662 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6663 || strcmp (name, ".persistent.bss") == 0
6664 || strcmp (name, ".sbss") == 0
6665 || strncmp (name, ".sbss.", 6) == 0
6666 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6667 flags |= SECTION_BSS;
6669 if (strcmp (name, ".tdata") == 0
6670 || strncmp (name, ".tdata.", 7) == 0
6671 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6672 flags |= SECTION_TLS;
6674 if (strcmp (name, ".tbss") == 0
6675 || strncmp (name, ".tbss.", 6) == 0
6676 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6677 flags |= SECTION_TLS | SECTION_BSS;
6679 if (strcmp (name, ".noinit") == 0)
6680 flags |= SECTION_WRITE | SECTION_BSS | SECTION_NOTYPE;
6682 /* Various sections have special ELF types that the assembler will
6683 assign by default based on the name. They are neither SHT_PROGBITS
6684 nor SHT_NOBITS, so when changing sections we don't want to print a
6685 section type (@progbits or @nobits). Rather than duplicating the
6686 assembler's knowledge of what those special name patterns are, just
6687 let the assembler choose the type if we don't know a specific
6688 reason to set it to something other than the default. SHT_PROGBITS
6689 is the default for sections whose name is not specially known to
6690 the assembler, so it does no harm to leave the choice to the
6691 assembler when @progbits is the best thing we know to use. If
6692 someone is silly enough to emit code or TLS variables to one of
6693 these sections, then don't handle them specially.
6695 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6696 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
6697 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6698 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6699 flags |= SECTION_NOTYPE;
6701 return flags;
6704 /* Return true if the target supports some form of global BSS,
6705 either through bss_noswitch_section, or by selecting a BSS
6706 section in TARGET_ASM_SELECT_SECTION. */
6708 bool
6709 have_global_bss_p (void)
6711 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6714 /* Output assembly to switch to section NAME with attribute FLAGS.
6715 Four variants for common object file formats. */
6717 void
6718 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6719 unsigned int flags ATTRIBUTE_UNUSED,
6720 tree decl ATTRIBUTE_UNUSED)
6722 /* Some object formats don't support named sections at all. The
6723 front-end should already have flagged this as an error. */
6724 gcc_unreachable ();
6727 #ifndef TLS_SECTION_ASM_FLAG
6728 #define TLS_SECTION_ASM_FLAG 'T'
6729 #endif
6731 void
6732 default_elf_asm_named_section (const char *name, unsigned int flags,
6733 tree decl)
6735 char flagchars[11], *f = flagchars;
6736 unsigned int numeric_value = 0;
6738 /* If we have already declared this section, we can use an
6739 abbreviated form to switch back to it -- unless this section is
6740 part of a COMDAT groups, in which case GAS requires the full
6741 declaration every time. */
6742 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6743 && (flags & SECTION_DECLARED))
6745 fprintf (asm_out_file, "\t.section\t%s\n", name);
6746 return;
6749 /* If we have a machine specific flag, then use the numeric value to pass
6750 this on to GAS. */
6751 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6752 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6753 else
6755 if (!(flags & SECTION_DEBUG))
6756 *f++ = 'a';
6757 #if HAVE_GAS_SECTION_EXCLUDE
6758 if (flags & SECTION_EXCLUDE)
6759 *f++ = 'e';
6760 #endif
6761 if (flags & SECTION_WRITE)
6762 *f++ = 'w';
6763 if (flags & SECTION_CODE)
6764 *f++ = 'x';
6765 if (flags & SECTION_SMALL)
6766 *f++ = 's';
6767 if (flags & SECTION_MERGE)
6768 *f++ = 'M';
6769 if (flags & SECTION_STRINGS)
6770 *f++ = 'S';
6771 if (flags & SECTION_TLS)
6772 *f++ = TLS_SECTION_ASM_FLAG;
6773 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6774 *f++ = 'G';
6775 #ifdef MACH_DEP_SECTION_ASM_FLAG
6776 if (flags & SECTION_MACH_DEP)
6777 *f++ = MACH_DEP_SECTION_ASM_FLAG;
6778 #endif
6779 *f = '\0';
6782 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6784 /* default_section_type_flags (above) knows which flags need special
6785 handling here, and sets NOTYPE when none of these apply so that the
6786 assembler's logic for default types can apply to user-chosen
6787 section names. */
6788 if (!(flags & SECTION_NOTYPE))
6790 const char *type;
6791 const char *format;
6793 if (flags & SECTION_BSS)
6794 type = "nobits";
6795 else
6796 type = "progbits";
6798 format = ",@%s";
6799 /* On platforms that use "@" as the assembly comment character,
6800 use "%" instead. */
6801 if (strcmp (ASM_COMMENT_START, "@") == 0)
6802 format = ",%%%s";
6803 fprintf (asm_out_file, format, type);
6805 if (flags & SECTION_ENTSIZE)
6806 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6807 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6809 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6810 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6811 else
6812 fprintf (asm_out_file, ",%s,comdat",
6813 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6817 putc ('\n', asm_out_file);
6820 void
6821 default_coff_asm_named_section (const char *name, unsigned int flags,
6822 tree decl ATTRIBUTE_UNUSED)
6824 char flagchars[8], *f = flagchars;
6826 if (flags & SECTION_WRITE)
6827 *f++ = 'w';
6828 if (flags & SECTION_CODE)
6829 *f++ = 'x';
6830 *f = '\0';
6832 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6835 void
6836 default_pe_asm_named_section (const char *name, unsigned int flags,
6837 tree decl)
6839 default_coff_asm_named_section (name, flags, decl);
6841 if (flags & SECTION_LINKONCE)
6843 /* Functions may have been compiled at various levels of
6844 optimization so we can't use `same_size' here.
6845 Instead, have the linker pick one. */
6846 fprintf (asm_out_file, "\t.linkonce %s\n",
6847 (flags & SECTION_CODE ? "discard" : "same_size"));
6851 /* The lame default section selector. */
6853 section *
6854 default_select_section (tree decl, int reloc,
6855 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6857 if (DECL_P (decl))
6859 if (decl_readonly_section (decl, reloc))
6860 return readonly_data_section;
6862 else if (TREE_CODE (decl) == CONSTRUCTOR)
6864 if (! ((flag_pic && reloc)
6865 || !TREE_READONLY (decl)
6866 || TREE_SIDE_EFFECTS (decl)
6867 || !TREE_CONSTANT (decl)))
6868 return readonly_data_section;
6870 else if (TREE_CODE (decl) == STRING_CST)
6871 return readonly_data_section;
6872 else if (! (flag_pic && reloc))
6873 return readonly_data_section;
6875 return data_section;
6878 enum section_category
6879 categorize_decl_for_section (const_tree decl, int reloc)
6881 enum section_category ret;
6883 if (TREE_CODE (decl) == FUNCTION_DECL)
6884 return SECCAT_TEXT;
6885 else if (TREE_CODE (decl) == STRING_CST)
6887 if ((flag_sanitize & SANITIZE_ADDRESS)
6888 && asan_protect_global (CONST_CAST_TREE (decl)))
6889 /* or !flag_merge_constants */
6890 return SECCAT_RODATA;
6891 else
6892 return SECCAT_RODATA_MERGE_STR;
6894 else if (VAR_P (decl))
6896 tree d = CONST_CAST_TREE (decl);
6897 if (bss_initializer_p (decl))
6898 ret = SECCAT_BSS;
6899 else if (! TREE_READONLY (decl)
6900 || TREE_SIDE_EFFECTS (decl)
6901 || (DECL_INITIAL (decl)
6902 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
6904 /* Here the reloc_rw_mask is not testing whether the section should
6905 be read-only or not, but whether the dynamic link will have to
6906 do something. If so, we wish to segregate the data in order to
6907 minimize cache misses inside the dynamic linker. */
6908 if (reloc & targetm.asm_out.reloc_rw_mask ())
6909 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6910 else
6911 ret = SECCAT_DATA;
6913 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6914 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6915 else if (reloc || flag_merge_constants < 2
6916 || ((flag_sanitize & SANITIZE_ADDRESS)
6917 /* PR 81697: for architectures that use section anchors we
6918 need to ignore DECL_RTL_SET_P (decl) for string constants
6919 inside this asan_protect_global call because otherwise
6920 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
6921 section, set DECL_RTL (decl) later on and add DECL to
6922 protected globals via successive asan_protect_global
6923 calls. In this scenario we'll end up with wrong
6924 alignment of these strings at runtime and possible ASan
6925 false positives. */
6926 && asan_protect_global (d, use_object_blocks_p ()
6927 && use_blocks_for_decl_p (d))))
6928 /* C and C++ don't allow different variables to share the same
6929 location. -fmerge-all-constants allows even that (at the
6930 expense of not conforming). */
6931 ret = SECCAT_RODATA;
6932 else if (DECL_INITIAL (decl)
6933 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6934 ret = SECCAT_RODATA_MERGE_STR_INIT;
6935 else
6936 ret = SECCAT_RODATA_MERGE_CONST;
6938 else if (TREE_CODE (decl) == CONSTRUCTOR)
6940 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6941 || TREE_SIDE_EFFECTS (decl)
6942 || ! TREE_CONSTANT (decl))
6943 ret = SECCAT_DATA;
6944 else
6945 ret = SECCAT_RODATA;
6947 else
6948 ret = SECCAT_RODATA;
6950 /* There are no read-only thread-local sections. */
6951 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6953 /* Note that this would be *just* SECCAT_BSS, except that there's
6954 no concept of a read-only thread-local-data section. */
6955 if (ret == SECCAT_BSS
6956 || DECL_INITIAL (decl) == NULL
6957 || (flag_zero_initialized_in_bss
6958 && initializer_zerop (DECL_INITIAL (decl))))
6959 ret = SECCAT_TBSS;
6960 else
6961 ret = SECCAT_TDATA;
6964 /* If the target uses small data sections, select it. */
6965 else if (targetm.in_small_data_p (decl))
6967 if (ret == SECCAT_BSS)
6968 ret = SECCAT_SBSS;
6969 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6970 ret = SECCAT_SRODATA;
6971 else
6972 ret = SECCAT_SDATA;
6975 return ret;
6978 static bool
6979 decl_readonly_section_1 (enum section_category category)
6981 switch (category)
6983 case SECCAT_RODATA:
6984 case SECCAT_RODATA_MERGE_STR:
6985 case SECCAT_RODATA_MERGE_STR_INIT:
6986 case SECCAT_RODATA_MERGE_CONST:
6987 case SECCAT_SRODATA:
6988 return true;
6989 default:
6990 return false;
6994 bool
6995 decl_readonly_section (const_tree decl, int reloc)
6997 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
7000 /* Select a section based on the above categorization. */
7002 section *
7003 default_elf_select_section (tree decl, int reloc,
7004 unsigned HOST_WIDE_INT align)
7006 const char *sname;
7008 switch (categorize_decl_for_section (decl, reloc))
7010 case SECCAT_TEXT:
7011 /* We're not supposed to be called on FUNCTION_DECLs. */
7012 gcc_unreachable ();
7013 case SECCAT_RODATA:
7014 return readonly_data_section;
7015 case SECCAT_RODATA_MERGE_STR:
7016 return mergeable_string_section (decl, align, 0);
7017 case SECCAT_RODATA_MERGE_STR_INIT:
7018 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
7019 case SECCAT_RODATA_MERGE_CONST:
7020 return mergeable_constant_section (DECL_MODE (decl), align, 0);
7021 case SECCAT_SRODATA:
7022 sname = ".sdata2";
7023 break;
7024 case SECCAT_DATA:
7025 return data_section;
7026 case SECCAT_DATA_REL:
7027 sname = ".data.rel";
7028 break;
7029 case SECCAT_DATA_REL_LOCAL:
7030 sname = ".data.rel.local";
7031 break;
7032 case SECCAT_DATA_REL_RO:
7033 sname = ".data.rel.ro";
7034 break;
7035 case SECCAT_DATA_REL_RO_LOCAL:
7036 sname = ".data.rel.ro.local";
7037 break;
7038 case SECCAT_SDATA:
7039 sname = ".sdata";
7040 break;
7041 case SECCAT_TDATA:
7042 sname = ".tdata";
7043 break;
7044 case SECCAT_BSS:
7045 if (DECL_P (decl)
7046 && lookup_attribute ("noinit", DECL_ATTRIBUTES (decl)) != NULL_TREE)
7048 sname = ".noinit";
7049 break;
7052 if (bss_section)
7053 return bss_section;
7054 sname = ".bss";
7055 break;
7056 case SECCAT_SBSS:
7057 sname = ".sbss";
7058 break;
7059 case SECCAT_TBSS:
7060 sname = ".tbss";
7061 break;
7062 default:
7063 gcc_unreachable ();
7066 return get_named_section (decl, sname, reloc);
7069 /* Construct a unique section name based on the decl name and the
7070 categorization performed above. */
7072 void
7073 default_unique_section (tree decl, int reloc)
7075 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
7076 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
7077 const char *prefix, *name, *linkonce;
7078 char *string;
7079 tree id;
7081 switch (categorize_decl_for_section (decl, reloc))
7083 case SECCAT_TEXT:
7084 prefix = one_only ? ".t" : ".text";
7085 break;
7086 case SECCAT_RODATA:
7087 case SECCAT_RODATA_MERGE_STR:
7088 case SECCAT_RODATA_MERGE_STR_INIT:
7089 case SECCAT_RODATA_MERGE_CONST:
7090 prefix = one_only ? ".r" : ".rodata";
7091 break;
7092 case SECCAT_SRODATA:
7093 prefix = one_only ? ".s2" : ".sdata2";
7094 break;
7095 case SECCAT_DATA:
7096 prefix = one_only ? ".d" : ".data";
7097 break;
7098 case SECCAT_DATA_REL:
7099 prefix = one_only ? ".d.rel" : ".data.rel";
7100 break;
7101 case SECCAT_DATA_REL_LOCAL:
7102 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
7103 break;
7104 case SECCAT_DATA_REL_RO:
7105 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
7106 break;
7107 case SECCAT_DATA_REL_RO_LOCAL:
7108 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
7109 break;
7110 case SECCAT_SDATA:
7111 prefix = one_only ? ".s" : ".sdata";
7112 break;
7113 case SECCAT_BSS:
7114 prefix = one_only ? ".b" : ".bss";
7115 break;
7116 case SECCAT_SBSS:
7117 prefix = one_only ? ".sb" : ".sbss";
7118 break;
7119 case SECCAT_TDATA:
7120 prefix = one_only ? ".td" : ".tdata";
7121 break;
7122 case SECCAT_TBSS:
7123 prefix = one_only ? ".tb" : ".tbss";
7124 break;
7125 default:
7126 gcc_unreachable ();
7129 id = DECL_ASSEMBLER_NAME (decl);
7130 ultimate_transparent_alias_target (&id);
7131 name = IDENTIFIER_POINTER (id);
7132 name = targetm.strip_name_encoding (name);
7134 /* If we're using one_only, then there needs to be a .gnu.linkonce
7135 prefix to the section name. */
7136 linkonce = one_only ? ".gnu.linkonce" : "";
7138 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
7140 set_decl_section_name (decl, string);
7143 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
7145 static int
7146 compute_reloc_for_rtx_1 (const_rtx x)
7148 switch (GET_CODE (x))
7150 case SYMBOL_REF:
7151 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
7152 case LABEL_REF:
7153 return 1;
7154 default:
7155 return 0;
7159 /* Like compute_reloc_for_constant, except for an RTX. The return value
7160 is a mask for which bit 1 indicates a global relocation, and bit 0
7161 indicates a local relocation. */
7163 static int
7164 compute_reloc_for_rtx (const_rtx x)
7166 switch (GET_CODE (x))
7168 case SYMBOL_REF:
7169 case LABEL_REF:
7170 return compute_reloc_for_rtx_1 (x);
7172 case CONST:
7174 int reloc = 0;
7175 subrtx_iterator::array_type array;
7176 FOR_EACH_SUBRTX (iter, array, x, ALL)
7177 reloc |= compute_reloc_for_rtx_1 (*iter);
7178 return reloc;
7181 default:
7182 return 0;
7186 section *
7187 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
7188 rtx x,
7189 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7191 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
7192 return data_section;
7193 else
7194 return readonly_data_section;
7197 section *
7198 default_elf_select_rtx_section (machine_mode mode, rtx x,
7199 unsigned HOST_WIDE_INT align)
7201 int reloc = compute_reloc_for_rtx (x);
7203 /* ??? Handle small data here somehow. */
7205 if (reloc & targetm.asm_out.reloc_rw_mask ())
7207 if (reloc == 1)
7208 return get_named_section (NULL, ".data.rel.ro.local", 1);
7209 else
7210 return get_named_section (NULL, ".data.rel.ro", 3);
7213 return mergeable_constant_section (mode, align, 0);
7216 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
7218 void
7219 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
7221 rtx symbol;
7222 int flags;
7224 /* Careful not to prod global register variables. */
7225 if (!MEM_P (rtl))
7226 return;
7227 symbol = XEXP (rtl, 0);
7228 if (GET_CODE (symbol) != SYMBOL_REF)
7229 return;
7231 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
7232 if (TREE_CODE (decl) == FUNCTION_DECL)
7233 flags |= SYMBOL_FLAG_FUNCTION;
7234 if (targetm.binds_local_p (decl))
7235 flags |= SYMBOL_FLAG_LOCAL;
7236 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7237 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
7238 else if (targetm.in_small_data_p (decl))
7239 flags |= SYMBOL_FLAG_SMALL;
7240 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
7241 being PUBLIC, the thing *must* be defined in this translation unit.
7242 Prevent this buglet from being propagated into rtl code as well. */
7243 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
7244 flags |= SYMBOL_FLAG_EXTERNAL;
7246 SYMBOL_REF_FLAGS (symbol) = flags;
7249 /* By default, we do nothing for encode_section_info, so we need not
7250 do anything but discard the '*' marker. */
7252 const char *
7253 default_strip_name_encoding (const char *str)
7255 return str + (*str == '*');
7258 #ifdef ASM_OUTPUT_DEF
7259 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
7260 anchor relative to ".", the current section position. */
7262 void
7263 default_asm_output_anchor (rtx symbol)
7265 char buffer[100];
7267 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7268 SYMBOL_REF_BLOCK_OFFSET (symbol));
7269 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7271 #endif
7273 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
7275 bool
7276 default_use_anchors_for_symbol_p (const_rtx symbol)
7278 tree decl;
7279 section *sect = SYMBOL_REF_BLOCK (symbol)->sect;
7281 /* This function should only be called with non-zero SYMBOL_REF_BLOCK,
7282 furthermore get_block_for_section should not create object blocks
7283 for mergeable sections. */
7284 gcc_checking_assert (sect && !(sect->common.flags & SECTION_MERGE));
7286 /* Don't use anchors for small data sections. The small data register
7287 acts as an anchor for such sections. */
7288 if (sect->common.flags & SECTION_SMALL)
7289 return false;
7291 decl = SYMBOL_REF_DECL (symbol);
7292 if (decl && DECL_P (decl))
7294 /* Don't use section anchors for decls that might be defined or
7295 usurped by other modules. */
7296 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
7297 return false;
7299 /* Don't use section anchors for decls that will be placed in a
7300 small data section. */
7301 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
7302 one above. The problem is that we only use SECTION_SMALL for
7303 sections that should be marked as small in the section directive. */
7304 if (targetm.in_small_data_p (decl))
7305 return false;
7307 /* Don't use section anchors for decls that won't fit inside a single
7308 anchor range to reduce the amount of instructions required to refer
7309 to the entire declaration. */
7310 if (DECL_SIZE_UNIT (decl) == NULL_TREE
7311 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
7312 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
7313 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
7314 return false;
7317 return true;
7320 /* Return true when RESOLUTION indicate that symbol will be bound to the
7321 definition provided by current .o file. */
7323 static bool
7324 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
7326 return (resolution == LDPR_PREVAILING_DEF
7327 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7328 || resolution == LDPR_PREVAILING_DEF_IRONLY);
7331 /* Return true when RESOLUTION indicate that symbol will be bound locally
7332 within current executable or DSO. */
7334 static bool
7335 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7337 return (resolution == LDPR_PREVAILING_DEF
7338 || resolution == LDPR_PREVAILING_DEF_IRONLY
7339 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7340 || resolution == LDPR_PREEMPTED_REG
7341 || resolution == LDPR_PREEMPTED_IR
7342 || resolution == LDPR_RESOLVED_IR
7343 || resolution == LDPR_RESOLVED_EXEC);
7346 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7347 uninitialized common symbol in the executable will still be defined
7348 (through COPY relocation) in the executable. */
7350 bool
7351 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7352 bool extern_protected_data, bool common_local_p)
7354 /* A non-decl is an entry in the constant pool. */
7355 if (!DECL_P (exp))
7356 return true;
7358 /* Weakrefs may not bind locally, even though the weakref itself is always
7359 static and therefore local. Similarly, the resolver for ifunc functions
7360 might resolve to a non-local function.
7361 FIXME: We can resolve the weakref case more curefuly by looking at the
7362 weakref alias. */
7363 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7364 || (TREE_CODE (exp) == FUNCTION_DECL
7365 && cgraph_node::get (exp)
7366 && cgraph_node::get (exp)->ifunc_resolver))
7367 return false;
7369 /* Static variables are always local. */
7370 if (! TREE_PUBLIC (exp))
7371 return true;
7373 /* With resolution file in hand, take look into resolutions.
7374 We can't just return true for resolved_locally symbols,
7375 because dynamic linking might overwrite symbols
7376 in shared libraries. */
7377 bool resolved_locally = false;
7379 bool uninited_common = (DECL_COMMON (exp)
7380 && (DECL_INITIAL (exp) == NULL
7381 || (!in_lto_p
7382 && DECL_INITIAL (exp) == error_mark_node)));
7384 /* A non-external variable is defined locally only if it isn't
7385 uninitialized COMMON variable or common_local_p is true. */
7386 bool defined_locally = (!DECL_EXTERNAL (exp)
7387 && (!uninited_common || common_local_p));
7388 if (symtab_node *node = symtab_node::get (exp))
7390 if (node->in_other_partition)
7391 defined_locally = true;
7392 if (node->can_be_discarded_p ())
7394 else if (resolution_to_local_definition_p (node->resolution))
7395 defined_locally = resolved_locally = true;
7396 else if (resolution_local_p (node->resolution))
7397 resolved_locally = true;
7399 if (defined_locally && weak_dominate && !shlib)
7400 resolved_locally = true;
7402 /* Undefined weak symbols are never defined locally. */
7403 if (DECL_WEAK (exp) && !defined_locally)
7404 return false;
7406 /* A symbol is local if the user has said explicitly that it will be,
7407 or if we have a definition for the symbol. We cannot infer visibility
7408 for undefined symbols. */
7409 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7410 && (TREE_CODE (exp) == FUNCTION_DECL
7411 || !extern_protected_data
7412 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7413 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7414 return true;
7416 /* If PIC, then assume that any global name can be overridden by
7417 symbols resolved from other modules. */
7418 if (shlib)
7419 return false;
7421 /* Variables defined outside this object might not be local. */
7422 if (DECL_EXTERNAL (exp) && !resolved_locally)
7423 return false;
7425 /* Non-dominant weak symbols are not defined locally. */
7426 if (DECL_WEAK (exp) && !resolved_locally)
7427 return false;
7429 /* Uninitialized COMMON variable may be unified with symbols
7430 resolved from other modules. */
7431 if (uninited_common && !resolved_locally)
7432 return false;
7434 /* Otherwise we're left with initialized (or non-common) global data
7435 which is of necessity defined locally. */
7436 return true;
7439 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7440 wrt cross-module name binding. */
7442 bool
7443 default_binds_local_p (const_tree exp)
7445 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7448 /* Similar to default_binds_local_p, but common symbol may be local and
7449 extern protected data is non-local. */
7451 bool
7452 default_binds_local_p_2 (const_tree exp)
7454 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7455 !flag_pic);
7458 bool
7459 default_binds_local_p_1 (const_tree exp, int shlib)
7461 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7464 /* Return true when references to DECL must bind to current definition in
7465 final executable.
7467 The condition is usually equivalent to whether the function binds to the
7468 current module (shared library or executable), that is to binds_local_p.
7469 We use this fact to avoid need for another target hook and implement
7470 the logic using binds_local_p and just special cases where
7471 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7472 the weak definitions (that can be overwritten at linktime by other
7473 definition from different object file) and when resolution info is available
7474 we simply use the knowledge passed to us by linker plugin. */
7475 bool
7476 decl_binds_to_current_def_p (const_tree decl)
7478 gcc_assert (DECL_P (decl));
7479 if (!targetm.binds_local_p (decl))
7480 return false;
7481 if (!TREE_PUBLIC (decl))
7482 return true;
7484 /* When resolution is available, just use it. */
7485 if (symtab_node *node = symtab_node::get (decl))
7487 if (node->resolution != LDPR_UNKNOWN
7488 && !node->can_be_discarded_p ())
7489 return resolution_to_local_definition_p (node->resolution);
7492 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7493 binds locally but still can be overwritten), DECL_COMMON (can be merged
7494 with a non-common definition somewhere in the same module) or
7495 DECL_EXTERNAL.
7496 This rely on fact that binds_local_p behave as decl_replaceable_p
7497 for all other declaration types. */
7498 if (DECL_WEAK (decl))
7499 return false;
7500 if (DECL_COMMON (decl)
7501 && (DECL_INITIAL (decl) == NULL
7502 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7503 return false;
7504 if (DECL_EXTERNAL (decl))
7505 return false;
7506 return true;
7509 /* A replaceable function or variable is one which may be replaced
7510 at link-time with an entirely different definition, provided that the
7511 replacement has the same type. For example, functions declared
7512 with __attribute__((weak)) on most systems are replaceable.
7514 COMDAT functions are not replaceable, since all definitions of the
7515 function must be equivalent. It is important that COMDAT functions
7516 not be treated as replaceable so that use of C++ template
7517 instantiations is not penalized. */
7519 bool
7520 decl_replaceable_p (tree decl)
7522 gcc_assert (DECL_P (decl));
7523 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7524 return false;
7525 if (!flag_semantic_interposition
7526 && !DECL_WEAK (decl))
7527 return false;
7528 return !decl_binds_to_current_def_p (decl);
7531 /* Default function to output code that will globalize a label. A
7532 target must define GLOBAL_ASM_OP or provide its own function to
7533 globalize a label. */
7534 #ifdef GLOBAL_ASM_OP
7535 void
7536 default_globalize_label (FILE * stream, const char *name)
7538 fputs (GLOBAL_ASM_OP, stream);
7539 assemble_name (stream, name);
7540 putc ('\n', stream);
7542 #endif /* GLOBAL_ASM_OP */
7544 /* Default function to output code that will globalize a declaration. */
7545 void
7546 default_globalize_decl_name (FILE * stream, tree decl)
7548 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7549 targetm.asm_out.globalize_label (stream, name);
7552 /* Default function to output a label for unwind information. The
7553 default is to do nothing. A target that needs nonlocal labels for
7554 unwind information must provide its own function to do this. */
7555 void
7556 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7557 tree decl ATTRIBUTE_UNUSED,
7558 int for_eh ATTRIBUTE_UNUSED,
7559 int empty ATTRIBUTE_UNUSED)
7563 /* Default function to output a label to divide up the exception table.
7564 The default is to do nothing. A target that needs/wants to divide
7565 up the table must provide it's own function to do this. */
7566 void
7567 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7571 /* This is how to output an internal numbered label where PREFIX is
7572 the class of label and LABELNO is the number within the class. */
7574 void
7575 default_generate_internal_label (char *buf, const char *prefix,
7576 unsigned long labelno)
7578 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7581 /* This is how to output an internal numbered label where PREFIX is
7582 the class of label and LABELNO is the number within the class. */
7584 void
7585 default_internal_label (FILE *stream, const char *prefix,
7586 unsigned long labelno)
7588 char *const buf = (char *) alloca (40 + strlen (prefix));
7589 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7590 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7594 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7596 void
7597 default_asm_declare_constant_name (FILE *file, const char *name,
7598 const_tree exp ATTRIBUTE_UNUSED,
7599 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7601 assemble_label (file, name);
7604 /* This is the default behavior at the beginning of a file. It's
7605 controlled by two other target-hook toggles. */
7606 void
7607 default_file_start (void)
7609 if (targetm.asm_file_start_app_off
7610 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7611 fputs (ASM_APP_OFF, asm_out_file);
7613 if (targetm.asm_file_start_file_directive)
7615 /* LTO produced units have no meaningful main_input_filename. */
7616 if (in_lto_p)
7617 output_file_directive (asm_out_file, "<artificial>");
7618 else
7619 output_file_directive (asm_out_file, main_input_filename);
7623 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7624 which emits a special section directive used to indicate whether or
7625 not this object file needs an executable stack. This is primarily
7626 a GNU extension to ELF but could be used on other targets. */
7628 int trampolines_created;
7630 void
7631 file_end_indicate_exec_stack (void)
7633 unsigned int flags = SECTION_DEBUG;
7634 if (trampolines_created)
7635 flags |= SECTION_CODE;
7637 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7640 /* Emit a special section directive to indicate that this object file
7641 was compiled with -fsplit-stack. This is used to let the linker
7642 detect calls between split-stack code and non-split-stack code, so
7643 that it can modify the split-stack code to allocate a sufficiently
7644 large stack. We emit another special section if there are any
7645 functions in this file which have the no_split_stack attribute, to
7646 prevent the linker from warning about being unable to convert the
7647 functions if they call non-split-stack code. */
7649 void
7650 file_end_indicate_split_stack (void)
7652 if (flag_split_stack)
7654 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7655 NULL));
7656 if (saw_no_split_stack)
7657 switch_to_section (get_section (".note.GNU-no-split-stack",
7658 SECTION_DEBUG, NULL));
7662 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7663 a get_unnamed_section callback. */
7665 void
7666 output_section_asm_op (const void *directive)
7668 fprintf (asm_out_file, "%s\n", (const char *) directive);
7671 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7672 the current section is NEW_SECTION. */
7674 void
7675 switch_to_section (section *new_section)
7677 if (in_section == new_section)
7678 return;
7680 if (new_section->common.flags & SECTION_FORGET)
7681 in_section = NULL;
7682 else
7683 in_section = new_section;
7685 switch (SECTION_STYLE (new_section))
7687 case SECTION_NAMED:
7688 targetm.asm_out.named_section (new_section->named.name,
7689 new_section->named.common.flags,
7690 new_section->named.decl);
7691 break;
7693 case SECTION_UNNAMED:
7694 new_section->unnamed.callback (new_section->unnamed.data);
7695 break;
7697 case SECTION_NOSWITCH:
7698 gcc_unreachable ();
7699 break;
7702 new_section->common.flags |= SECTION_DECLARED;
7705 /* If block symbol SYMBOL has not yet been assigned an offset, place
7706 it at the end of its block. */
7708 void
7709 place_block_symbol (rtx symbol)
7711 unsigned HOST_WIDE_INT size, mask, offset;
7712 class constant_descriptor_rtx *desc;
7713 unsigned int alignment;
7714 struct object_block *block;
7715 tree decl;
7717 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7718 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7719 return;
7721 /* Work out the symbol's size and alignment. */
7722 if (CONSTANT_POOL_ADDRESS_P (symbol))
7724 desc = SYMBOL_REF_CONSTANT (symbol);
7725 alignment = desc->align;
7726 size = GET_MODE_SIZE (desc->mode);
7728 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7730 decl = SYMBOL_REF_DECL (symbol);
7731 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7732 alignment = DECL_ALIGN (decl);
7733 size = get_constant_size (DECL_INITIAL (decl));
7734 if ((flag_sanitize & SANITIZE_ADDRESS)
7735 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7736 && asan_protect_global (DECL_INITIAL (decl)))
7738 size += asan_red_zone_size (size);
7739 alignment = MAX (alignment,
7740 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7743 else
7745 struct symtab_node *snode;
7746 decl = SYMBOL_REF_DECL (symbol);
7748 snode = symtab_node::get (decl);
7749 if (snode->alias)
7751 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7753 gcc_assert (MEM_P (target)
7754 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7755 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7756 target = XEXP (target, 0);
7757 place_block_symbol (target);
7758 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7759 return;
7761 alignment = get_variable_align (decl);
7762 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7763 if ((flag_sanitize & SANITIZE_ADDRESS)
7764 && asan_protect_global (decl))
7766 size += asan_red_zone_size (size);
7767 alignment = MAX (alignment,
7768 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7772 /* Calculate the object's offset from the start of the block. */
7773 block = SYMBOL_REF_BLOCK (symbol);
7774 mask = alignment / BITS_PER_UNIT - 1;
7775 offset = (block->size + mask) & ~mask;
7776 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7778 /* Record the block's new alignment and size. */
7779 block->alignment = MAX (block->alignment, alignment);
7780 block->size = offset + size;
7782 vec_safe_push (block->objects, symbol);
7785 /* Return the anchor that should be used to address byte offset OFFSET
7786 from the first object in BLOCK. MODEL is the TLS model used
7787 to access it. */
7790 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7791 enum tls_model model)
7793 char label[100];
7794 unsigned int begin, middle, end;
7795 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7796 rtx anchor;
7798 /* Work out the anchor's offset. Use an offset of 0 for the first
7799 anchor so that we don't pessimize the case where we take the address
7800 of a variable at the beginning of the block. This is particularly
7801 useful when a block has only one variable assigned to it.
7803 We try to place anchors RANGE bytes apart, so there can then be
7804 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7805 a ptr_mode offset. With some target settings, the lowest such
7806 anchor might be out of range for the lowest ptr_mode offset;
7807 likewise the highest anchor for the highest offset. Use anchors
7808 at the extreme ends of the ptr_mode range in such cases.
7810 All arithmetic uses unsigned integers in order to avoid
7811 signed overflow. */
7812 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7813 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7814 range = max_offset - min_offset + 1;
7815 if (range == 0)
7816 offset = 0;
7817 else
7819 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7820 if (offset < 0)
7822 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7823 delta -= delta % range;
7824 if (delta > bias)
7825 delta = bias;
7826 offset = (HOST_WIDE_INT) (-delta);
7828 else
7830 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7831 delta -= delta % range;
7832 if (delta > bias - 1)
7833 delta = bias - 1;
7834 offset = (HOST_WIDE_INT) delta;
7838 /* Do a binary search to see if there's already an anchor we can use.
7839 Set BEGIN to the new anchor's index if not. */
7840 begin = 0;
7841 end = vec_safe_length (block->anchors);
7842 while (begin != end)
7844 middle = (end + begin) / 2;
7845 anchor = (*block->anchors)[middle];
7846 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7847 end = middle;
7848 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7849 begin = middle + 1;
7850 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7851 end = middle;
7852 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7853 begin = middle + 1;
7854 else
7855 return anchor;
7858 /* Create a new anchor with a unique label. */
7859 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7860 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7861 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7862 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7864 /* Insert it at index BEGIN. */
7865 vec_safe_insert (block->anchors, begin, anchor);
7866 return anchor;
7869 /* Output the objects in BLOCK. */
7871 static void
7872 output_object_block (struct object_block *block)
7874 class constant_descriptor_rtx *desc;
7875 unsigned int i;
7876 HOST_WIDE_INT offset;
7877 tree decl;
7878 rtx symbol;
7880 if (!block->objects)
7881 return;
7883 /* Switch to the section and make sure that the first byte is
7884 suitably aligned. */
7885 /* Special case VTV comdat sections similar to assemble_variable. */
7886 if (SECTION_STYLE (block->sect) == SECTION_NAMED
7887 && block->sect->named.name
7888 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
7889 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
7890 else
7891 switch_to_section (block->sect);
7893 gcc_checking_assert (!(block->sect->common.flags & SECTION_MERGE));
7894 assemble_align (block->alignment);
7896 /* Define the values of all anchors relative to the current section
7897 position. */
7898 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7899 targetm.asm_out.output_anchor (symbol);
7901 /* Output the objects themselves. */
7902 offset = 0;
7903 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7905 /* Move to the object's offset, padding with zeros if necessary. */
7906 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7907 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7908 if (CONSTANT_POOL_ADDRESS_P (symbol))
7910 desc = SYMBOL_REF_CONSTANT (symbol);
7911 /* Pass 1 for align as we have already laid out everything in the block.
7912 So aligning shouldn't be necessary. */
7913 output_constant_pool_1 (desc, 1);
7914 offset += GET_MODE_SIZE (desc->mode);
7916 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7918 HOST_WIDE_INT size;
7919 decl = SYMBOL_REF_DECL (symbol);
7920 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7921 DECL_ALIGN (decl), false);
7923 size = get_constant_size (DECL_INITIAL (decl));
7924 offset += size;
7925 if ((flag_sanitize & SANITIZE_ADDRESS)
7926 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7927 && asan_protect_global (DECL_INITIAL (decl)))
7929 size = asan_red_zone_size (size);
7930 assemble_zeros (size);
7931 offset += size;
7934 else
7936 HOST_WIDE_INT size;
7937 decl = SYMBOL_REF_DECL (symbol);
7938 assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
7939 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7940 offset += size;
7941 if ((flag_sanitize & SANITIZE_ADDRESS)
7942 && asan_protect_global (decl))
7944 size = asan_red_zone_size (size);
7945 assemble_zeros (size);
7946 offset += size;
7952 /* A callback for qsort to compare object_blocks. */
7954 static int
7955 output_object_block_compare (const void *x, const void *y)
7957 object_block *p1 = *(object_block * const*)x;
7958 object_block *p2 = *(object_block * const*)y;
7960 if (p1->sect->common.flags & SECTION_NAMED
7961 && !(p2->sect->common.flags & SECTION_NAMED))
7962 return 1;
7964 if (!(p1->sect->common.flags & SECTION_NAMED)
7965 && p2->sect->common.flags & SECTION_NAMED)
7966 return -1;
7968 if (p1->sect->common.flags & SECTION_NAMED
7969 && p2->sect->common.flags & SECTION_NAMED)
7970 return strcmp (p1->sect->named.name, p2->sect->named.name);
7972 unsigned f1 = p1->sect->common.flags;
7973 unsigned f2 = p2->sect->common.flags;
7974 if (f1 == f2)
7975 return 0;
7976 return f1 < f2 ? -1 : 1;
7979 /* Output the definitions of all object_blocks. */
7981 void
7982 output_object_blocks (void)
7984 vec<object_block *, va_heap> v;
7985 v.create (object_block_htab->elements ());
7986 object_block *obj;
7987 hash_table<object_block_hasher>::iterator hi;
7989 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
7990 v.quick_push (obj);
7992 /* Sort them in order to output them in a deterministic manner,
7993 otherwise we may get .rodata sections in different orders with
7994 and without -g. */
7995 v.qsort (output_object_block_compare);
7996 unsigned i;
7997 FOR_EACH_VEC_ELT (v, i, obj)
7998 output_object_block (obj);
8000 v.release ();
8003 /* This function provides a possible implementation of the
8004 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
8005 by -frecord-gcc-switches it creates a new mergeable, string section in the
8006 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
8007 contains the switches in ASCII format.
8009 FIXME: This code does not correctly handle double quote characters
8010 that appear inside strings, (it strips them rather than preserving them).
8011 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
8012 characters - instead it treats them as sub-string separators. Since
8013 we want to emit NUL strings terminators into the object file we have to use
8014 ASM_OUTPUT_SKIP. */
8017 elf_record_gcc_switches (print_switch_type type, const char * name)
8019 switch (type)
8021 case SWITCH_TYPE_PASSED:
8022 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
8023 ASM_OUTPUT_SKIP (asm_out_file, HOST_WIDE_INT_1U);
8024 break;
8026 case SWITCH_TYPE_DESCRIPTIVE:
8027 if (name == NULL)
8029 /* Distinguish between invocations where name is NULL. */
8030 static bool started = false;
8032 if (!started)
8034 section * sec;
8036 sec = get_section (targetm.asm_out.record_gcc_switches_section,
8037 SECTION_DEBUG
8038 | SECTION_MERGE
8039 | SECTION_STRINGS
8040 | (SECTION_ENTSIZE & 1),
8041 NULL);
8042 switch_to_section (sec);
8043 started = true;
8047 default:
8048 break;
8051 /* The return value is currently ignored by the caller, but must be 0.
8052 For -fverbose-asm the return value would be the number of characters
8053 emitted into the assembler file. */
8054 return 0;
8057 /* Emit text to declare externally defined symbols. It is needed to
8058 properly support non-default visibility. */
8059 void
8060 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
8061 tree decl,
8062 const char *name ATTRIBUTE_UNUSED)
8064 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8065 set in order to avoid putting out names that are never really
8066 used. Always output visibility specified in the source. */
8067 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
8068 && (DECL_VISIBILITY_SPECIFIED (decl)
8069 || targetm.binds_local_p (decl)))
8070 maybe_assemble_visibility (decl);
8073 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8075 void
8076 default_asm_output_source_filename (FILE *file, const char *name)
8078 #ifdef ASM_OUTPUT_SOURCE_FILENAME
8079 ASM_OUTPUT_SOURCE_FILENAME (file, name);
8080 #else
8081 fprintf (file, "\t.file\t");
8082 output_quoted_string (file, name);
8083 putc ('\n', file);
8084 #endif
8087 /* Output a file name in the form wanted by System V. */
8089 void
8090 output_file_directive (FILE *asm_file, const char *input_name)
8092 int len;
8093 const char *na;
8095 if (input_name == NULL)
8096 input_name = "<stdin>";
8097 else
8098 input_name = remap_debug_filename (input_name);
8100 len = strlen (input_name);
8101 na = input_name + len;
8103 /* NA gets INPUT_NAME sans directory names. */
8104 while (na > input_name)
8106 if (IS_DIR_SEPARATOR (na[-1]))
8107 break;
8108 na--;
8111 targetm.asm_out.output_source_filename (asm_file, na);
8114 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
8115 EXP. */
8117 make_debug_expr_from_rtl (const_rtx exp)
8119 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
8120 machine_mode mode = GET_MODE (exp);
8121 rtx dval;
8123 DECL_ARTIFICIAL (ddecl) = 1;
8124 if (REG_P (exp) && REG_EXPR (exp))
8125 type = TREE_TYPE (REG_EXPR (exp));
8126 else if (MEM_P (exp) && MEM_EXPR (exp))
8127 type = TREE_TYPE (MEM_EXPR (exp));
8128 else
8129 type = NULL_TREE;
8130 if (type && TYPE_MODE (type) == mode)
8131 TREE_TYPE (ddecl) = type;
8132 else
8133 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
8134 SET_DECL_MODE (ddecl, mode);
8135 dval = gen_rtx_DEBUG_EXPR (mode);
8136 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
8137 SET_DECL_RTL (ddecl, dval);
8138 return dval;
8141 #ifdef ELF_ASCII_ESCAPES
8142 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
8144 void
8145 default_elf_asm_output_limited_string (FILE *f, const char *s)
8147 int escape;
8148 unsigned char c;
8150 fputs (STRING_ASM_OP, f);
8151 putc ('"', f);
8152 while (*s != '\0')
8154 c = *s;
8155 escape = ELF_ASCII_ESCAPES[c];
8156 switch (escape)
8158 case 0:
8159 putc (c, f);
8160 break;
8161 case 1:
8162 putc ('\\', f);
8163 putc ('0'+((c>>6)&7), f);
8164 putc ('0'+((c>>3)&7), f);
8165 putc ('0'+(c&7), f);
8166 break;
8167 default:
8168 putc ('\\', f);
8169 putc (escape, f);
8170 break;
8172 s++;
8174 putc ('\"', f);
8175 putc ('\n', f);
8178 /* Default ASM_OUTPUT_ASCII for ELF targets. */
8180 void
8181 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
8183 const char *limit = s + len;
8184 const char *last_null = NULL;
8185 unsigned bytes_in_chunk = 0;
8186 unsigned char c;
8187 int escape;
8189 for (; s < limit; s++)
8191 const char *p;
8193 if (bytes_in_chunk >= 60)
8195 putc ('\"', f);
8196 putc ('\n', f);
8197 bytes_in_chunk = 0;
8200 if (s > last_null)
8202 for (p = s; p < limit && *p != '\0'; p++)
8203 continue;
8204 last_null = p;
8206 else
8207 p = last_null;
8209 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
8211 if (bytes_in_chunk > 0)
8213 putc ('\"', f);
8214 putc ('\n', f);
8215 bytes_in_chunk = 0;
8218 default_elf_asm_output_limited_string (f, s);
8219 s = p;
8221 else
8223 if (bytes_in_chunk == 0)
8224 fputs (ASCII_DATA_ASM_OP "\"", f);
8226 c = *s;
8227 escape = ELF_ASCII_ESCAPES[c];
8228 switch (escape)
8230 case 0:
8231 putc (c, f);
8232 bytes_in_chunk++;
8233 break;
8234 case 1:
8235 putc ('\\', f);
8236 putc ('0'+((c>>6)&7), f);
8237 putc ('0'+((c>>3)&7), f);
8238 putc ('0'+(c&7), f);
8239 bytes_in_chunk += 4;
8240 break;
8241 default:
8242 putc ('\\', f);
8243 putc (escape, f);
8244 bytes_in_chunk += 2;
8245 break;
8251 if (bytes_in_chunk > 0)
8253 putc ('\"', f);
8254 putc ('\n', f);
8257 #endif
8259 static GTY(()) section *elf_init_array_section;
8260 static GTY(()) section *elf_fini_array_section;
8262 static section *
8263 get_elf_initfini_array_priority_section (int priority,
8264 bool constructor_p)
8266 section *sec;
8267 if (priority != DEFAULT_INIT_PRIORITY)
8269 char buf[18];
8270 sprintf (buf, "%s.%.5u",
8271 constructor_p ? ".init_array" : ".fini_array",
8272 priority);
8273 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8275 else
8277 if (constructor_p)
8279 if (elf_init_array_section == NULL)
8280 elf_init_array_section
8281 = get_section (".init_array",
8282 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8283 sec = elf_init_array_section;
8285 else
8287 if (elf_fini_array_section == NULL)
8288 elf_fini_array_section
8289 = get_section (".fini_array",
8290 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8291 sec = elf_fini_array_section;
8294 return sec;
8297 /* Use .init_array section for constructors. */
8299 void
8300 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8302 section *sec = get_elf_initfini_array_priority_section (priority,
8303 true);
8304 assemble_addr_to_section (symbol, sec);
8307 /* Use .fini_array section for destructors. */
8309 void
8310 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8312 section *sec = get_elf_initfini_array_priority_section (priority,
8313 false);
8314 assemble_addr_to_section (symbol, sec);
8317 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8319 This is a bit of a cheat. The real default is a no-op, but this
8320 hook is the default for all targets with a .ident directive. */
8322 void
8323 default_asm_output_ident_directive (const char *ident_str)
8325 const char *ident_asm_op = "\t.ident\t";
8327 /* If we are still in the front end, do not write out the string
8328 to asm_out_file. Instead, add a fake top-level asm statement.
8329 This allows the front ends to use this hook without actually
8330 writing to asm_out_file, to handle #ident or Pragma Ident. */
8331 if (symtab->state == PARSING)
8333 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8334 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8336 else
8337 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8341 /* This function ensures that vtable_map variables are not only
8342 in the comdat section, but that each variable has its own unique
8343 comdat name. Without this the variables end up in the same section
8344 with a single comdat name.
8346 FIXME: resolve_unique_section needs to deal better with
8347 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8348 that is fixed, this if-else statement can be replaced with
8349 a single call to "switch_to_section (sect)". */
8351 static void
8352 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8354 #if defined (OBJECT_FORMAT_ELF)
8355 targetm.asm_out.named_section (sect->named.name,
8356 sect->named.common.flags
8357 | SECTION_LINKONCE,
8358 DECL_NAME (decl));
8359 in_section = sect;
8360 #else
8361 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8362 Therefore the following check is used.
8363 In case a the target is PE or COFF a comdat group section
8364 is created, e.g. .vtable_map_vars$foo. The linker places
8365 everything in .vtable_map_vars at the end.
8367 A fix could be made in
8368 gcc/config/i386/winnt.c: i386_pe_unique_section. */
8369 if (TARGET_PECOFF)
8371 char *name;
8373 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
8374 name = ACONCAT ((sect->named.name, "$",
8375 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
8376 else
8377 name = ACONCAT ((sect->named.name, "$",
8378 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
8379 NULL));
8381 targetm.asm_out.named_section (name,
8382 sect->named.common.flags
8383 | SECTION_LINKONCE,
8384 DECL_NAME (decl));
8385 in_section = sect;
8387 else
8388 switch_to_section (sect);
8389 #endif
8392 #include "gt-varasm.h"