middle-end: Disable section anchors for VAR_DECLs if -fdata-sections
[official-gcc.git] / gcc / varasm.c
blobac256ef65e5c309216ea98d0a607d6369bc1c4c1
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2021 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "backend.h"
32 #include "target.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "predict.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "stringpool.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "varasm.h"
46 #include "version.h"
47 #include "flags.h"
48 #include "stmt.h"
49 #include "expr.h"
50 #include "expmed.h"
51 #include "optabs.h"
52 #include "output.h"
53 #include "langhooks.h"
54 #include "debug.h"
55 #include "common/common-target.h"
56 #include "stringpool.h"
57 #include "attribs.h"
58 #include "asan.h"
59 #include "rtl-iter.h"
60 #include "file-prefix-map.h" /* remap_debug_filename() */
61 #include "alloc-pool.h"
62 #include "toplev.h"
63 #include "opts.h"
65 #ifdef XCOFF_DEBUGGING_INFO
66 #include "xcoffout.h" /* Needed for external data declarations. */
67 #endif
69 /* The (assembler) name of the first globally-visible object output. */
70 extern GTY(()) const char *first_global_object_name;
71 extern GTY(()) const char *weak_global_object_name;
73 const char *first_global_object_name;
74 const char *weak_global_object_name;
76 class addr_const;
77 class constant_descriptor_rtx;
78 struct rtx_constant_pool;
80 #define n_deferred_constants (crtl->varasm.deferred_constants)
82 /* Number for making the label on the next
83 constant that is stored in memory. */
85 static GTY(()) int const_labelno;
87 /* Carry information from ASM_DECLARE_OBJECT_NAME
88 to ASM_FINISH_DECLARE_OBJECT. */
90 int size_directive_output;
92 /* The last decl for which assemble_variable was called,
93 if it did ASM_DECLARE_OBJECT_NAME.
94 If the last call to assemble_variable didn't do that,
95 this holds 0. */
97 tree last_assemble_variable_decl;
99 /* The following global variable indicates if the first basic block
100 in a function belongs to the cold partition or not. */
102 bool first_function_block_is_cold;
104 /* Whether we saw any functions with no_split_stack. */
106 static bool saw_no_split_stack;
108 static const char *strip_reg_name (const char *);
109 static int contains_pointers_p (tree);
110 #ifdef ASM_OUTPUT_EXTERNAL
111 static bool incorporeal_function_p (tree);
112 #endif
113 static void decode_addr_const (tree, class addr_const *);
114 static hashval_t const_hash_1 (const tree);
115 static int compare_constant (const tree, const tree);
116 static void output_constant_def_contents (rtx);
117 static void output_addressed_constants (tree, int);
118 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
119 unsigned int, bool, bool);
120 static void globalize_decl (tree);
121 static bool decl_readonly_section_1 (enum section_category);
122 #ifdef BSS_SECTION_ASM_OP
123 #ifdef ASM_OUTPUT_ALIGNED_BSS
124 static void asm_output_aligned_bss (FILE *, tree, const char *,
125 unsigned HOST_WIDE_INT, int)
126 ATTRIBUTE_UNUSED;
127 #endif
128 #endif /* BSS_SECTION_ASM_OP */
129 static void mark_weak (tree);
130 static void output_constant_pool (const char *, tree);
131 static void handle_vtv_comdat_section (section *, const_tree);
133 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
134 section *text_section;
135 section *data_section;
136 section *readonly_data_section;
137 section *sdata_section;
138 section *ctors_section;
139 section *dtors_section;
140 section *bss_section;
141 section *sbss_section;
143 /* Various forms of common section. All are guaranteed to be nonnull. */
144 section *tls_comm_section;
145 section *comm_section;
146 section *lcomm_section;
148 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
149 May be null. */
150 section *bss_noswitch_section;
152 /* The section that holds the main exception table, when known. The section
153 is set either by the target's init_sections hook or by the first call to
154 switch_to_exception_section. */
155 section *exception_section;
157 /* The section that holds the DWARF2 frame unwind information, when known.
158 The section is set either by the target's init_sections hook or by the
159 first call to switch_to_eh_frame_section. */
160 section *eh_frame_section;
162 /* asm_out_file's current section. This is NULL if no section has yet
163 been selected or if we lose track of what the current section is. */
164 section *in_section;
166 /* True if code for the current function is currently being directed
167 at the cold section. */
168 bool in_cold_section_p;
170 /* The following global holds the "function name" for the code in the
171 cold section of a function, if hot/cold function splitting is enabled
172 and there was actually code that went into the cold section. A
173 pseudo function name is needed for the cold section of code for some
174 debugging tools that perform symbolization. */
175 tree cold_function_name = NULL_TREE;
177 /* A linked list of all the unnamed sections. */
178 static GTY(()) section *unnamed_sections;
180 /* Return a nonzero value if DECL has a section attribute. */
181 #define IN_NAMED_SECTION(DECL) \
182 (VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
184 struct section_hasher : ggc_ptr_hash<section>
186 typedef const char *compare_type;
188 static hashval_t hash (section *);
189 static bool equal (section *, const char *);
192 /* Hash table of named sections. */
193 static GTY(()) hash_table<section_hasher> *section_htab;
195 struct object_block_hasher : ggc_ptr_hash<object_block>
197 typedef const section *compare_type;
199 static hashval_t hash (object_block *);
200 static bool equal (object_block *, const section *);
203 /* A table of object_blocks, indexed by section. */
204 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
206 /* The next number to use for internal anchor labels. */
207 static GTY(()) int anchor_labelno;
209 /* A pool of constants that can be shared between functions. */
210 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
212 /* Helper routines for maintaining section_htab. */
214 bool
215 section_hasher::equal (section *old, const char *new_name)
217 return strcmp (old->named.name, new_name) == 0;
220 hashval_t
221 section_hasher::hash (section *old)
223 return htab_hash_string (old->named.name);
226 /* Return a hash value for section SECT. */
228 static hashval_t
229 hash_section (section *sect)
231 if (sect->common.flags & SECTION_NAMED)
232 return htab_hash_string (sect->named.name);
233 return sect->common.flags & ~SECTION_DECLARED;
236 /* Helper routines for maintaining object_block_htab. */
238 inline bool
239 object_block_hasher::equal (object_block *old, const section *new_section)
241 return old->sect == new_section;
244 hashval_t
245 object_block_hasher::hash (object_block *old)
247 return hash_section (old->sect);
250 /* Return a new unnamed section with the given fields. */
252 section *
253 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
254 const void *data)
256 section *sect;
258 sect = ggc_alloc<section> ();
259 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
260 sect->unnamed.callback = callback;
261 sect->unnamed.data = data;
262 sect->unnamed.next = unnamed_sections;
264 unnamed_sections = sect;
265 return sect;
268 /* Return a SECTION_NOSWITCH section with the given fields. */
270 static section *
271 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
273 section *sect;
275 sect = ggc_alloc<section> ();
276 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
277 sect->noswitch.callback = callback;
279 return sect;
282 /* Return the named section structure associated with NAME. Create
283 a new section with the given fields if no such structure exists.
284 When NOT_EXISTING, then fail if the section already exists. Return
285 the existing section if the SECTION_RETAIN bit doesn't match. Set
286 the SECTION_WRITE | SECTION_RELRO bits on the the existing section
287 if one of the section flags is SECTION_WRITE | SECTION_RELRO and the
288 other has none of these flags in named sections and either the section
289 hasn't been declared yet or has been declared as writable. */
291 section *
292 get_section (const char *name, unsigned int flags, tree decl,
293 bool not_existing)
295 section *sect, **slot;
297 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
298 INSERT);
299 flags |= SECTION_NAMED;
300 if (decl != nullptr
301 && DECL_P (decl)
302 && lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
303 flags |= SECTION_RETAIN;
304 if (*slot == NULL)
306 sect = ggc_alloc<section> ();
307 sect->named.common.flags = flags;
308 sect->named.name = ggc_strdup (name);
309 sect->named.decl = decl;
310 *slot = sect;
312 else
314 if (not_existing)
315 internal_error ("Section already exists: %qs", name);
317 sect = *slot;
318 /* It is fine if one of the sections has SECTION_NOTYPE as long as
319 the other has none of the contrary flags (see the logic at the end
320 of default_section_type_flags, below). */
321 if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
322 && !((sect->common.flags | flags)
323 & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
324 | (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
326 sect->common.flags |= SECTION_NOTYPE;
327 flags |= SECTION_NOTYPE;
329 if ((sect->common.flags & ~SECTION_DECLARED) != flags
330 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
332 /* It is fine if one of the section flags is
333 SECTION_WRITE | SECTION_RELRO and the other has none of these
334 flags (i.e. read-only) in named sections and either the
335 section hasn't been declared yet or has been declared as writable.
336 In that case just make sure the resulting flags are
337 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
338 relocations. */
339 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
340 == (SECTION_WRITE | SECTION_RELRO)
341 && (sect->common.flags
342 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
343 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
344 && ((sect->common.flags & SECTION_DECLARED) == 0
345 || (sect->common.flags & SECTION_WRITE)))
347 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
348 return sect;
350 /* If the SECTION_RETAIN bit doesn't match, return and switch
351 to a new section later. */
352 if ((sect->common.flags & SECTION_RETAIN)
353 != (flags & SECTION_RETAIN))
354 return sect;
355 /* Sanity check user variables for flag changes. */
356 if (sect->named.decl != NULL
357 && DECL_P (sect->named.decl)
358 && decl != sect->named.decl)
360 if (decl != NULL && DECL_P (decl))
361 error ("%+qD causes a section type conflict with %qD",
362 decl, sect->named.decl);
363 else
364 error ("section type conflict with %qD", sect->named.decl);
365 inform (DECL_SOURCE_LOCATION (sect->named.decl),
366 "%qD was declared here", sect->named.decl);
368 else if (decl != NULL && DECL_P (decl))
369 error ("%+qD causes a section type conflict", decl);
370 else
371 error ("section type conflict");
372 /* Make sure we don't error about one section multiple times. */
373 sect->common.flags |= SECTION_OVERRIDE;
376 return sect;
379 /* Return true if the current compilation mode benefits from having
380 objects grouped into blocks. */
382 static bool
383 use_object_blocks_p (void)
385 return flag_section_anchors;
388 /* Return the object_block structure for section SECT. Create a new
389 structure if we haven't created one already. Return null if SECT
390 itself is null. Return also null for mergeable sections since
391 section anchors can't be used in mergeable sections anyway,
392 because the linker might move objects around, and using the
393 object blocks infrastructure in that case is both a waste and a
394 maintenance burden. */
396 static struct object_block *
397 get_block_for_section (section *sect)
399 struct object_block *block;
401 if (sect == NULL)
402 return NULL;
404 if (sect->common.flags & SECTION_MERGE)
405 return NULL;
407 object_block **slot
408 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
409 INSERT);
410 block = *slot;
411 if (block == NULL)
413 block = ggc_cleared_alloc<object_block> ();
414 block->sect = sect;
415 *slot = block;
417 return block;
420 /* Create a symbol with label LABEL and place it at byte offset
421 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
422 is not yet known. LABEL must be a garbage-collected string. */
424 static rtx
425 create_block_symbol (const char *label, struct object_block *block,
426 HOST_WIDE_INT offset)
428 rtx symbol;
429 unsigned int size;
431 /* Create the extended SYMBOL_REF. */
432 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
433 symbol = (rtx) ggc_internal_alloc (size);
435 /* Initialize the normal SYMBOL_REF fields. */
436 memset (symbol, 0, size);
437 PUT_CODE (symbol, SYMBOL_REF);
438 PUT_MODE (symbol, Pmode);
439 XSTR (symbol, 0) = label;
440 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
442 /* Initialize the block_symbol stuff. */
443 SYMBOL_REF_BLOCK (symbol) = block;
444 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
446 return symbol;
449 /* Return a section with a particular name and with whatever SECTION_*
450 flags section_type_flags deems appropriate. The name of the section
451 is taken from NAME if nonnull, otherwise it is taken from DECL's
452 DECL_SECTION_NAME. DECL is the decl associated with the section
453 (see the section comment for details) and RELOC is as for
454 section_type_flags. */
456 section *
457 get_named_section (tree decl, const char *name, int reloc)
459 unsigned int flags;
461 if (name == NULL)
463 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
464 name = DECL_SECTION_NAME (decl);
467 flags = targetm.section_type_flags (decl, name, reloc);
468 return get_section (name, flags, decl);
471 /* Worker for resolve_unique_section. */
473 static bool
474 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
476 n->implicit_section = true;
477 return false;
480 /* If required, set DECL_SECTION_NAME to a unique name. */
482 void
483 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
484 int flag_function_or_data_sections)
486 if (DECL_SECTION_NAME (decl) == NULL
487 && targetm_common.have_named_sections
488 && (flag_function_or_data_sections
489 || lookup_attribute ("retain", DECL_ATTRIBUTES (decl))
490 || DECL_COMDAT_GROUP (decl)))
492 targetm.asm_out.unique_section (decl, reloc);
493 if (DECL_SECTION_NAME (decl))
494 symtab_node::get (decl)->call_for_symbol_and_aliases
495 (set_implicit_section, NULL, true);
499 #ifdef BSS_SECTION_ASM_OP
501 #ifdef ASM_OUTPUT_ALIGNED_BSS
503 /* Utility function for targets to use in implementing
504 ASM_OUTPUT_ALIGNED_BSS.
505 ??? It is believed that this function will work in most cases so such
506 support is localized here. */
508 static void
509 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
510 const char *name, unsigned HOST_WIDE_INT size,
511 int align)
513 switch_to_section (bss_section);
514 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
515 #ifdef ASM_DECLARE_OBJECT_NAME
516 last_assemble_variable_decl = decl;
517 ASM_DECLARE_OBJECT_NAME (file, name, decl);
518 #else
519 /* Standard thing is just output label for the object. */
520 ASM_OUTPUT_LABEL (file, name);
521 #endif /* ASM_DECLARE_OBJECT_NAME */
522 ASM_OUTPUT_SKIP (file, size ? size : 1);
525 #endif
527 #endif /* BSS_SECTION_ASM_OP */
529 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
530 /* Return the hot section for function DECL. Return text_section for
531 null DECLs. */
533 static section *
534 hot_function_section (tree decl)
536 if (decl != NULL_TREE
537 && DECL_SECTION_NAME (decl) != NULL
538 && targetm_common.have_named_sections)
539 return get_named_section (decl, NULL, 0);
540 else
541 return text_section;
543 #endif
545 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
546 is NULL.
548 When DECL_SECTION_NAME is non-NULL and it is implicit section and
549 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
550 concatenate the name with NAMED_SECTION_SUFFIX.
551 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
553 section *
554 get_named_text_section (tree decl,
555 const char *text_section_name,
556 const char *named_section_suffix)
558 if (decl && DECL_SECTION_NAME (decl))
560 if (named_section_suffix)
562 const char *dsn = DECL_SECTION_NAME (decl);
563 const char *stripped_name;
564 char *name, *buffer;
566 name = (char *) alloca (strlen (dsn) + 1);
567 memcpy (name, dsn,
568 strlen (dsn) + 1);
570 stripped_name = targetm.strip_name_encoding (name);
572 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
573 return get_named_section (decl, buffer, 0);
575 else if (symtab_node::get (decl)->implicit_section)
577 const char *name;
579 /* Do not try to split gnu_linkonce functions. This gets somewhat
580 slipperly. */
581 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
582 return NULL;
583 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
584 name = targetm.strip_name_encoding (name);
585 return get_named_section (decl, ACONCAT ((text_section_name, ".",
586 name, NULL)), 0);
588 else
589 return NULL;
591 return get_named_section (decl, text_section_name, 0);
594 /* Choose named function section based on its frequency. */
596 section *
597 default_function_section (tree decl, enum node_frequency freq,
598 bool startup, bool exit)
600 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
601 /* Old GNU linkers have buggy --gc-section support, which sometimes
602 results in .gcc_except_table* sections being garbage collected. */
603 if (decl
604 && symtab_node::get (decl)->implicit_section)
605 return NULL;
606 #endif
608 if (!flag_reorder_functions
609 || !targetm_common.have_named_sections)
610 return NULL;
611 /* Startup code should go to startup subsection unless it is
612 unlikely executed (this happens especially with function splitting
613 where we can split away unnecessary parts of static constructors. */
614 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
616 /* During LTO the tp_first_run profiling will naturally place all
617 initialization code first. Using separate section is counter-productive
618 because startup only code may call functions which are no longer
619 startup only. */
620 if (!in_lto_p
621 || !cgraph_node::get (decl)->tp_first_run
622 || !opt_for_fn (decl, flag_profile_reorder_functions))
623 return get_named_text_section (decl, ".text.startup", NULL);
624 else
625 return NULL;
628 /* Similarly for exit. */
629 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
630 return get_named_text_section (decl, ".text.exit", NULL);
632 /* Group cold functions together, similarly for hot code. */
633 switch (freq)
635 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
636 return get_named_text_section (decl, ".text.unlikely", NULL);
637 case NODE_FREQUENCY_HOT:
638 return get_named_text_section (decl, ".text.hot", NULL);
639 /* FALLTHRU */
640 default:
641 return NULL;
645 /* Return the section for function DECL.
647 If DECL is NULL_TREE, return the text section. We can be passed
648 NULL_TREE under some circumstances by dbxout.c at least.
650 If FORCE_COLD is true, return cold function section ignoring
651 the frequency info of cgraph_node. */
653 static section *
654 function_section_1 (tree decl, bool force_cold)
656 section *section = NULL;
657 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
658 bool startup = false, exit = false;
660 if (decl)
662 struct cgraph_node *node = cgraph_node::get (decl);
664 if (node)
666 freq = node->frequency;
667 startup = node->only_called_at_startup;
668 exit = node->only_called_at_exit;
671 if (force_cold)
672 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
674 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
675 if (decl != NULL_TREE
676 && DECL_SECTION_NAME (decl) != NULL)
678 if (targetm.asm_out.function_section)
679 section = targetm.asm_out.function_section (decl, freq,
680 startup, exit);
681 if (section)
682 return section;
683 return get_named_section (decl, NULL, 0);
685 else
686 return targetm.asm_out.select_section
687 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
688 symtab_node::get (decl)->definition_alignment ());
689 #else
690 if (targetm.asm_out.function_section)
691 section = targetm.asm_out.function_section (decl, freq, startup, exit);
692 if (section)
693 return section;
694 return hot_function_section (decl);
695 #endif
698 /* Return the section for function DECL.
700 If DECL is NULL_TREE, return the text section. We can be passed
701 NULL_TREE under some circumstances by dbxout.c at least. */
703 section *
704 function_section (tree decl)
706 /* Handle cases where function splitting code decides
707 to put function entry point into unlikely executed section
708 despite the fact that the function itself is not cold
709 (i.e. it is called rarely but contains a hot loop that is
710 better to live in hot subsection for the code locality). */
711 return function_section_1 (decl,
712 first_function_block_is_cold);
715 /* Return the section for the current function, take IN_COLD_SECTION_P
716 into account. */
718 section *
719 current_function_section (void)
721 return function_section_1 (current_function_decl, in_cold_section_p);
724 /* Tell assembler to switch to unlikely-to-be-executed text section. */
726 section *
727 unlikely_text_section (void)
729 return function_section_1 (current_function_decl, true);
732 /* When called within a function context, return true if the function
733 has been assigned a cold text section and if SECT is that section.
734 When called outside a function context, return true if SECT is the
735 default cold section. */
737 bool
738 unlikely_text_section_p (section *sect)
740 return sect == function_section_1 (current_function_decl, true);
743 /* Switch to the other function partition (if inside of hot section
744 into cold section, otherwise into the hot section). */
746 void
747 switch_to_other_text_partition (void)
749 in_cold_section_p = !in_cold_section_p;
750 switch_to_section (current_function_section ());
753 /* Return the read-only or relocated read-only data section
754 associated with function DECL. */
756 section *
757 default_function_rodata_section (tree decl, bool relocatable)
759 const char* sname;
760 unsigned int flags;
762 flags = 0;
764 if (relocatable)
766 sname = ".data.rel.ro.local";
767 flags = (SECTION_WRITE | SECTION_RELRO);
769 else
770 sname = ".rodata";
772 if (decl && DECL_SECTION_NAME (decl))
774 const char *name = DECL_SECTION_NAME (decl);
776 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
778 const char *dot;
779 size_t len;
780 char* rname;
782 dot = strchr (name + 1, '.');
783 if (!dot)
784 dot = name;
785 len = strlen (dot) + strlen (sname) + 1;
786 rname = (char *) alloca (len);
788 strcpy (rname, sname);
789 strcat (rname, dot);
790 return get_section (rname, (SECTION_LINKONCE | flags), decl);
792 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo or
793 .gnu.linkonce.d.rel.ro.local.foo if the jump table is relocatable. */
794 else if (DECL_COMDAT_GROUP (decl)
795 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
797 size_t len;
798 char *rname;
800 if (relocatable)
802 len = strlen (name) + strlen (".rel.ro.local") + 1;
803 rname = (char *) alloca (len);
805 strcpy (rname, ".gnu.linkonce.d.rel.ro.local");
806 strcat (rname, name + 15);
808 else
810 len = strlen (name) + 1;
811 rname = (char *) alloca (len);
813 memcpy (rname, name, len);
814 rname[14] = 'r';
816 return get_section (rname, (SECTION_LINKONCE | flags), decl);
818 /* For .text.foo we want to use .rodata.foo. */
819 else if (flag_function_sections && flag_data_sections
820 && strncmp (name, ".text.", 6) == 0)
822 size_t len = strlen (name) + 1;
823 char *rname = (char *) alloca (len + strlen (sname) - 5);
825 memcpy (rname, sname, strlen (sname));
826 memcpy (rname + strlen (sname), name + 5, len - 5);
827 return get_section (rname, flags, decl);
831 if (relocatable)
832 return get_section (sname, flags, decl);
833 else
834 return readonly_data_section;
837 /* Return the read-only data section associated with function DECL
838 for targets where that section should be always the single
839 readonly data section. */
841 section *
842 default_no_function_rodata_section (tree, bool)
844 return readonly_data_section;
847 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
849 static const char *
850 function_mergeable_rodata_prefix (void)
852 section *s = targetm.asm_out.function_rodata_section (current_function_decl,
853 false);
854 if (SECTION_STYLE (s) == SECTION_NAMED)
855 return s->named.name;
856 else
857 return targetm.asm_out.mergeable_rodata_prefix;
860 /* Return the section to use for string merging. */
862 static section *
863 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
864 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
865 unsigned int flags ATTRIBUTE_UNUSED)
867 HOST_WIDE_INT len;
869 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
870 && TREE_CODE (decl) == STRING_CST
871 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
872 && align <= 256
873 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
874 && TREE_STRING_LENGTH (decl) == len)
876 scalar_int_mode mode;
877 unsigned int modesize;
878 const char *str;
879 HOST_WIDE_INT i;
880 int j, unit;
881 const char *prefix = function_mergeable_rodata_prefix ();
882 char *name = (char *) alloca (strlen (prefix) + 30);
884 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
885 modesize = GET_MODE_BITSIZE (mode);
886 if (modesize >= 8 && modesize <= 256
887 && (modesize & (modesize - 1)) == 0)
889 if (align < modesize)
890 align = modesize;
892 if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
893 return readonly_data_section;
895 str = TREE_STRING_POINTER (decl);
896 unit = GET_MODE_SIZE (mode);
898 /* Check for embedded NUL characters. */
899 for (i = 0; i < len; i += unit)
901 for (j = 0; j < unit; j++)
902 if (str[i + j] != '\0')
903 break;
904 if (j == unit)
905 break;
907 if (i == len - unit || (unit == 1 && i == len))
909 sprintf (name, "%s.str%d.%d", prefix,
910 modesize / 8, (int) (align / 8));
911 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
912 return get_section (name, flags, NULL);
917 return readonly_data_section;
920 /* Return the section to use for constant merging. */
922 section *
923 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
924 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
925 unsigned int flags ATTRIBUTE_UNUSED)
927 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
928 && mode != VOIDmode
929 && mode != BLKmode
930 && known_le (GET_MODE_BITSIZE (mode), align)
931 && align >= 8
932 && align <= 256
933 && (align & (align - 1)) == 0
934 && (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
936 const char *prefix = function_mergeable_rodata_prefix ();
937 char *name = (char *) alloca (strlen (prefix) + 30);
939 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
940 flags |= (align / 8) | SECTION_MERGE;
941 return get_section (name, flags, NULL);
943 return readonly_data_section;
946 /* Given NAME, a putative register name, discard any customary prefixes. */
948 static const char *
949 strip_reg_name (const char *name)
951 #ifdef REGISTER_PREFIX
952 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
953 name += strlen (REGISTER_PREFIX);
954 #endif
955 if (name[0] == '%' || name[0] == '#')
956 name++;
957 return name;
960 /* The user has asked for a DECL to have a particular name. Set (or
961 change) it in such a way that we don't prefix an underscore to
962 it. */
963 void
964 set_user_assembler_name (tree decl, const char *name)
966 char *starred = (char *) alloca (strlen (name) + 2);
967 starred[0] = '*';
968 strcpy (starred + 1, name);
969 symtab->change_decl_assembler_name (decl, get_identifier (starred));
970 SET_DECL_RTL (decl, NULL_RTX);
973 /* Decode an `asm' spec for a declaration as a register name.
974 Return the register number, or -1 if nothing specified,
975 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
976 or -3 if ASMSPEC is `cc' and is not recognized,
977 or -4 if ASMSPEC is `memory' and is not recognized.
978 Accept an exact spelling or a decimal number.
979 Prefixes such as % are optional. */
982 decode_reg_name_and_count (const char *asmspec, int *pnregs)
984 /* Presume just one register is clobbered. */
985 *pnregs = 1;
987 if (asmspec != 0)
989 int i;
991 /* Get rid of confusing prefixes. */
992 asmspec = strip_reg_name (asmspec);
994 /* Allow a decimal number as a "register name". */
995 for (i = strlen (asmspec) - 1; i >= 0; i--)
996 if (! ISDIGIT (asmspec[i]))
997 break;
998 if (asmspec[0] != 0 && i < 0)
1000 i = atoi (asmspec);
1001 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
1002 return i;
1003 else
1004 return -2;
1007 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1008 if (reg_names[i][0]
1009 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
1010 return i;
1012 #ifdef OVERLAPPING_REGISTER_NAMES
1014 static const struct
1016 const char *const name;
1017 const int number;
1018 const int nregs;
1019 } table[] = OVERLAPPING_REGISTER_NAMES;
1021 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1022 if (table[i].name[0]
1023 && ! strcmp (asmspec, table[i].name))
1025 *pnregs = table[i].nregs;
1026 return table[i].number;
1029 #endif /* OVERLAPPING_REGISTER_NAMES */
1031 #ifdef ADDITIONAL_REGISTER_NAMES
1033 static const struct { const char *const name; const int number; } table[]
1034 = ADDITIONAL_REGISTER_NAMES;
1036 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1037 if (table[i].name[0]
1038 && ! strcmp (asmspec, table[i].name)
1039 && reg_names[table[i].number][0])
1040 return table[i].number;
1042 #endif /* ADDITIONAL_REGISTER_NAMES */
1044 if (!strcmp (asmspec, "memory"))
1045 return -4;
1047 if (!strcmp (asmspec, "cc"))
1048 return -3;
1050 return -2;
1053 return -1;
1057 decode_reg_name (const char *name)
1059 int count;
1060 return decode_reg_name_and_count (name, &count);
1064 /* Return true if DECL's initializer is suitable for a BSS section. */
1066 bool
1067 bss_initializer_p (const_tree decl, bool named)
1069 /* Do not put non-common constants into the .bss section, they belong in
1070 a readonly section, except when NAMED is true. */
1071 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1072 && (DECL_INITIAL (decl) == NULL
1073 /* In LTO we have no errors in program; error_mark_node is used
1074 to mark offlined constructors. */
1075 || (DECL_INITIAL (decl) == error_mark_node
1076 && !in_lto_p)
1077 || (flag_zero_initialized_in_bss
1078 && initializer_zerop (DECL_INITIAL (decl))
1079 /* A decl with the "persistent" attribute applied and
1080 explicitly initialized to 0 should not be treated as a BSS
1081 variable. */
1082 && !DECL_PERSISTENT_P (decl))));
1085 /* Compute the alignment of variable specified by DECL.
1086 DONT_OUTPUT_DATA is from assemble_variable. */
1088 void
1089 align_variable (tree decl, bool dont_output_data)
1091 unsigned int align = DECL_ALIGN (decl);
1093 /* In the case for initialing an array whose length isn't specified,
1094 where we have not yet been able to do the layout,
1095 figure out the proper alignment now. */
1096 if (dont_output_data && DECL_SIZE (decl) == 0
1097 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1098 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1100 /* Some object file formats have a maximum alignment which they support.
1101 In particular, a.out format supports a maximum alignment of 4. */
1102 if (align > MAX_OFILE_ALIGNMENT)
1104 error ("alignment of %q+D is greater than maximum object "
1105 "file alignment %d", decl,
1106 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1107 align = MAX_OFILE_ALIGNMENT;
1110 if (! DECL_USER_ALIGN (decl))
1112 #ifdef DATA_ABI_ALIGNMENT
1113 unsigned int data_abi_align
1114 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1115 /* For backwards compatibility, don't assume the ABI alignment for
1116 TLS variables. */
1117 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1118 align = data_abi_align;
1119 #endif
1121 /* On some machines, it is good to increase alignment sometimes.
1122 But as DECL_ALIGN is used both for actually emitting the variable
1123 and for code accessing the variable as guaranteed alignment, we
1124 can only increase the alignment if it is a performance optimization
1125 if the references to it must bind to the current definition. */
1126 if (decl_binds_to_current_def_p (decl)
1127 && !DECL_VIRTUAL_P (decl))
1129 #ifdef DATA_ALIGNMENT
1130 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1131 /* Don't increase alignment too much for TLS variables - TLS space
1132 is too precious. */
1133 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1134 align = data_align;
1135 #endif
1136 if (DECL_INITIAL (decl) != 0
1137 /* In LTO we have no errors in program; error_mark_node is used
1138 to mark offlined constructors. */
1139 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1141 unsigned int const_align
1142 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1143 /* Don't increase alignment too much for TLS variables - TLS
1144 space is too precious. */
1145 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1146 align = const_align;
1151 /* Reset the alignment in case we have made it tighter, so we can benefit
1152 from it in get_pointer_alignment. */
1153 SET_DECL_ALIGN (decl, align);
1156 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1157 beyond what align_variable returned. */
1159 static unsigned int
1160 get_variable_align (tree decl)
1162 unsigned int align = DECL_ALIGN (decl);
1164 /* For user aligned vars or static vars align_variable already did
1165 everything. */
1166 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1167 return align;
1169 #ifdef DATA_ABI_ALIGNMENT
1170 if (DECL_THREAD_LOCAL_P (decl))
1171 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1172 #endif
1174 /* For decls that bind to the current definition, align_variable
1175 did also everything, except for not assuming ABI required alignment
1176 of TLS variables. For other vars, increase the alignment here
1177 as an optimization. */
1178 if (!decl_binds_to_current_def_p (decl))
1180 /* On some machines, it is good to increase alignment sometimes. */
1181 #ifdef DATA_ALIGNMENT
1182 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1183 /* Don't increase alignment too much for TLS variables - TLS space
1184 is too precious. */
1185 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1186 align = data_align;
1187 #endif
1188 if (DECL_INITIAL (decl) != 0
1189 /* In LTO we have no errors in program; error_mark_node is used
1190 to mark offlined constructors. */
1191 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1193 unsigned int const_align
1194 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1195 /* Don't increase alignment too much for TLS variables - TLS space
1196 is too precious. */
1197 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1198 align = const_align;
1202 return align;
1205 /* Return the section into which the given VAR_DECL or CONST_DECL
1206 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1207 section should be used wherever possible. */
1209 section *
1210 get_variable_section (tree decl, bool prefer_noswitch_p)
1212 addr_space_t as = ADDR_SPACE_GENERIC;
1213 int reloc;
1214 varpool_node *vnode = varpool_node::get (decl);
1215 if (vnode)
1217 vnode = vnode->ultimate_alias_target ();
1218 decl = vnode->decl;
1221 if (TREE_TYPE (decl) != error_mark_node)
1222 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1224 /* We need the constructor to figure out reloc flag. */
1225 if (vnode)
1226 vnode->get_constructor ();
1228 if (DECL_COMMON (decl)
1229 && !lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1231 /* If the decl has been given an explicit section name, or it resides
1232 in a non-generic address space, then it isn't common, and shouldn't
1233 be handled as such. */
1234 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1235 && ADDR_SPACE_GENERIC_P (as));
1236 if (DECL_THREAD_LOCAL_P (decl))
1237 return tls_comm_section;
1238 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1239 return comm_section;
1242 if (DECL_INITIAL (decl) == error_mark_node)
1243 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1244 else if (DECL_INITIAL (decl))
1245 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1246 else
1247 reloc = 0;
1249 resolve_unique_section (decl, reloc, flag_data_sections);
1250 if (IN_NAMED_SECTION (decl))
1252 section *sect = get_named_section (decl, NULL, reloc);
1254 if ((sect->common.flags & SECTION_BSS)
1255 && !bss_initializer_p (decl, true))
1257 error_at (DECL_SOURCE_LOCATION (decl),
1258 "only zero initializers are allowed in section %qs",
1259 sect->named.name);
1260 DECL_INITIAL (decl) = error_mark_node;
1262 return sect;
1265 if (ADDR_SPACE_GENERIC_P (as)
1266 && !DECL_THREAD_LOCAL_P (decl)
1267 && !DECL_NOINIT_P (decl)
1268 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1269 && bss_initializer_p (decl))
1271 if (!TREE_PUBLIC (decl)
1272 && !((flag_sanitize & SANITIZE_ADDRESS)
1273 && asan_protect_global (decl)))
1274 return lcomm_section;
1275 if (bss_noswitch_section)
1276 return bss_noswitch_section;
1279 return targetm.asm_out.select_section (decl, reloc,
1280 get_variable_align (decl));
1283 /* Return the block into which object_block DECL should be placed. */
1285 static struct object_block *
1286 get_block_for_decl (tree decl)
1288 section *sect;
1290 if (VAR_P (decl))
1292 /* The object must be defined in this translation unit. */
1293 if (DECL_EXTERNAL (decl))
1294 return NULL;
1296 /* There's no point using object blocks for something that is
1297 isolated by definition. */
1298 if (DECL_COMDAT_GROUP (decl))
1299 return NULL;
1302 /* We can only calculate block offsets if the decl has a known
1303 constant size. */
1304 if (DECL_SIZE_UNIT (decl) == NULL)
1305 return NULL;
1306 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1307 return NULL;
1309 /* Find out which section should contain DECL. We cannot put it into
1310 an object block if it requires a standalone definition. */
1311 if (VAR_P (decl))
1312 align_variable (decl, 0);
1313 sect = get_variable_section (decl, true);
1314 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1315 return NULL;
1317 if (bool (lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1318 != bool (sect->common.flags & SECTION_RETAIN))
1319 return NULL;
1321 return get_block_for_section (sect);
1324 /* Make sure block symbol SYMBOL is in block BLOCK. */
1326 static void
1327 change_symbol_block (rtx symbol, struct object_block *block)
1329 if (block != SYMBOL_REF_BLOCK (symbol))
1331 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1332 SYMBOL_REF_BLOCK (symbol) = block;
1336 /* Return true if it is possible to put DECL in an object_block. */
1338 static bool
1339 use_blocks_for_decl_p (tree decl)
1341 struct symtab_node *snode;
1343 /* Don't create object blocks if each DECL is placed into a separate
1344 section because that will uselessly create a section anchor for
1345 each DECL. */
1346 if (flag_data_sections)
1347 return false;
1349 /* Only data DECLs can be placed into object blocks. */
1350 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1351 return false;
1353 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1354 are never used from code directly and we never want object block handling
1355 for those. */
1356 if (DECL_INITIAL (decl) == decl)
1357 return false;
1359 /* If this decl is an alias, then we don't want to emit a
1360 definition. */
1361 if (VAR_P (decl)
1362 && (snode = symtab_node::get (decl)) != NULL
1363 && snode->alias)
1364 return false;
1366 return targetm.use_blocks_for_decl_p (decl);
1369 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1370 until we find an identifier that is not itself a transparent alias.
1371 Modify the alias passed to it by reference (and all aliases on the
1372 way to the ultimate target), such that they do not have to be
1373 followed again, and return the ultimate target of the alias
1374 chain. */
1376 static inline tree
1377 ultimate_transparent_alias_target (tree *alias)
1379 tree target = *alias;
1381 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1383 gcc_assert (TREE_CHAIN (target));
1384 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1385 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1386 && ! TREE_CHAIN (target));
1387 *alias = target;
1390 return target;
1393 /* Return true if REGNUM is mentioned in ELIMINABLE_REGS as a from
1394 register number. */
1396 static bool
1397 eliminable_regno_p (int regnum)
1399 static const struct
1401 const int from;
1402 const int to;
1403 } eliminables[] = ELIMINABLE_REGS;
1404 for (size_t i = 0; i < ARRAY_SIZE (eliminables); i++)
1405 if (regnum == eliminables[i].from)
1406 return true;
1407 return false;
1410 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1411 have static storage duration. In other words, it should not be an
1412 automatic variable, including PARM_DECLs.
1414 There is, however, one exception: this function handles variables
1415 explicitly placed in a particular register by the user.
1417 This is never called for PARM_DECL nodes. */
1419 void
1420 make_decl_rtl (tree decl)
1422 const char *name = 0;
1423 int reg_number;
1424 tree id;
1425 rtx x;
1427 /* Check that we are not being given an automatic variable. */
1428 gcc_assert (TREE_CODE (decl) != PARM_DECL
1429 && TREE_CODE (decl) != RESULT_DECL);
1431 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1432 gcc_assert (!VAR_P (decl)
1433 || TREE_STATIC (decl)
1434 || TREE_PUBLIC (decl)
1435 || DECL_EXTERNAL (decl)
1436 || DECL_REGISTER (decl));
1438 /* And that we were not given a type or a label. */
1439 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1440 && TREE_CODE (decl) != LABEL_DECL);
1442 /* For a duplicate declaration, we can be called twice on the
1443 same DECL node. Don't discard the RTL already made. */
1444 if (DECL_RTL_SET_P (decl))
1446 /* If the old RTL had the wrong mode, fix the mode. */
1447 x = DECL_RTL (decl);
1448 if (GET_MODE (x) != DECL_MODE (decl))
1449 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1451 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1452 return;
1454 /* ??? Another way to do this would be to maintain a hashed
1455 table of such critters. Instead of adding stuff to a DECL
1456 to give certain attributes to it, we could use an external
1457 hash map from DECL to set of attributes. */
1459 /* Let the target reassign the RTL if it wants.
1460 This is necessary, for example, when one machine specific
1461 decl attribute overrides another. */
1462 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1464 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1465 on the new decl information. */
1466 if (MEM_P (x)
1467 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1468 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1469 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1471 return;
1474 /* If this variable belongs to the global constant pool, retrieve the
1475 pre-computed RTL or recompute it in LTO mode. */
1476 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1478 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1479 return;
1482 id = DECL_ASSEMBLER_NAME (decl);
1483 name = IDENTIFIER_POINTER (id);
1485 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1486 && DECL_REGISTER (decl))
1488 error ("register name not specified for %q+D", decl);
1490 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1492 const char *asmspec = name+1;
1493 machine_mode mode = DECL_MODE (decl);
1494 reg_number = decode_reg_name (asmspec);
1495 /* First detect errors in declaring global registers. */
1496 if (reg_number == -1)
1497 error ("register name not specified for %q+D", decl);
1498 else if (reg_number < 0)
1499 error ("invalid register name for %q+D", decl);
1500 else if (mode == BLKmode)
1501 error ("data type of %q+D isn%'t suitable for a register",
1502 decl);
1503 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1504 error ("the register specified for %q+D cannot be accessed"
1505 " by the current target", decl);
1506 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1507 error ("the register specified for %q+D is not general enough"
1508 " to be used as a register variable", decl);
1509 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1510 error ("register specified for %q+D isn%'t suitable for data type",
1511 decl);
1512 else if (reg_number != HARD_FRAME_POINTER_REGNUM
1513 && (reg_number == FRAME_POINTER_REGNUM
1514 #ifdef RETURN_ADDRESS_POINTER_REGNUM
1515 || reg_number == RETURN_ADDRESS_POINTER_REGNUM
1516 #endif
1517 || reg_number == ARG_POINTER_REGNUM)
1518 && eliminable_regno_p (reg_number))
1519 error ("register specified for %q+D is an internal GCC "
1520 "implementation detail", decl);
1521 /* Now handle properly declared static register variables. */
1522 else
1524 int nregs;
1526 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1528 DECL_INITIAL (decl) = 0;
1529 error ("global register variable has initial value");
1531 if (TREE_THIS_VOLATILE (decl))
1532 warning (OPT_Wvolatile_register_var,
1533 "optimization may eliminate reads and/or "
1534 "writes to register variables");
1536 /* If the user specified one of the eliminables registers here,
1537 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1538 confused with that register and be eliminated. This usage is
1539 somewhat suspect... */
1541 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1542 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1543 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1545 if (TREE_STATIC (decl))
1547 /* Make this register global, so not usable for anything
1548 else. */
1549 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1550 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1551 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1552 #endif
1553 nregs = hard_regno_nregs (reg_number, mode);
1554 while (nregs > 0)
1555 globalize_reg (decl, reg_number + --nregs);
1558 /* As a register variable, it has no section. */
1559 return;
1561 /* Avoid internal errors from invalid register
1562 specifications. */
1563 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1564 DECL_HARD_REGISTER (decl) = 0;
1565 /* Also avoid SSA inconsistencies by pretending this is an external
1566 decl now. */
1567 DECL_EXTERNAL (decl) = 1;
1568 return;
1570 /* Now handle ordinary static variables and functions (in memory).
1571 Also handle vars declared register invalidly. */
1572 else if (name[0] == '*')
1574 #ifdef REGISTER_PREFIX
1575 if (strlen (REGISTER_PREFIX) != 0)
1577 reg_number = decode_reg_name (name);
1578 if (reg_number >= 0 || reg_number == -3)
1579 error ("register name given for non-register variable %q+D", decl);
1581 #endif
1584 /* Specifying a section attribute on a variable forces it into a
1585 non-.bss section, and thus it cannot be common. */
1586 /* FIXME: In general this code should not be necessary because
1587 visibility pass is doing the same work. But notice_global_symbol
1588 is called early and it needs to make DECL_RTL to get the name.
1589 we take care of recomputing the DECL_RTL after visibility is changed. */
1590 if (VAR_P (decl)
1591 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1592 && DECL_SECTION_NAME (decl) != NULL
1593 && DECL_INITIAL (decl) == NULL_TREE
1594 && DECL_COMMON (decl))
1595 DECL_COMMON (decl) = 0;
1597 /* Variables can't be both common and weak. */
1598 if (VAR_P (decl) && DECL_WEAK (decl))
1599 DECL_COMMON (decl) = 0;
1601 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1602 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1603 else
1605 machine_mode address_mode = Pmode;
1606 if (TREE_TYPE (decl) != error_mark_node)
1608 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1609 address_mode = targetm.addr_space.address_mode (as);
1611 x = gen_rtx_SYMBOL_REF (address_mode, name);
1613 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1614 SET_SYMBOL_REF_DECL (x, decl);
1616 x = gen_rtx_MEM (DECL_MODE (decl), x);
1617 if (TREE_CODE (decl) != FUNCTION_DECL)
1618 set_mem_attributes (x, decl, 1);
1619 SET_DECL_RTL (decl, x);
1621 /* Optionally set flags or add text to the name to record information
1622 such as that it is a function name.
1623 If the name is changed, the macro ASM_OUTPUT_LABELREF
1624 will have to know how to strip this information. */
1625 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1628 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1629 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1630 rtl. */
1633 make_decl_rtl_for_debug (tree decl)
1635 unsigned int save_aliasing_flag;
1636 rtx rtl;
1638 if (DECL_RTL_SET_P (decl))
1639 return DECL_RTL (decl);
1641 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1642 call new_alias_set. If running with -fcompare-debug, sometimes
1643 we do not want to create alias sets that will throw the alias
1644 numbers off in the comparison dumps. So... clearing
1645 flag_strict_aliasing will keep new_alias_set() from creating a
1646 new set. */
1647 save_aliasing_flag = flag_strict_aliasing;
1648 flag_strict_aliasing = 0;
1650 rtl = DECL_RTL (decl);
1651 /* Reset DECL_RTL back, as various parts of the compiler expects
1652 DECL_RTL set meaning it is actually going to be output. */
1653 SET_DECL_RTL (decl, NULL);
1655 flag_strict_aliasing = save_aliasing_flag;
1656 return rtl;
1659 /* Output a string of literal assembler code
1660 for an `asm' keyword used between functions. */
1662 void
1663 assemble_asm (tree string)
1665 const char *p;
1666 app_enable ();
1668 if (TREE_CODE (string) == ADDR_EXPR)
1669 string = TREE_OPERAND (string, 0);
1671 p = TREE_STRING_POINTER (string);
1672 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1675 /* Write the address of the entity given by SYMBOL to SEC. */
1676 void
1677 assemble_addr_to_section (rtx symbol, section *sec)
1679 switch_to_section (sec);
1680 assemble_align (POINTER_SIZE);
1681 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1684 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1685 not) section for PRIORITY. */
1686 section *
1687 get_cdtor_priority_section (int priority, bool constructor_p)
1689 /* Buffer conservatively large enough for the full range of a 32-bit
1690 int plus the text below. */
1691 char buf[18];
1693 /* ??? This only works reliably with the GNU linker. */
1694 sprintf (buf, "%s.%.5u",
1695 constructor_p ? ".ctors" : ".dtors",
1696 /* Invert the numbering so the linker puts us in the proper
1697 order; constructors are run from right to left, and the
1698 linker sorts in increasing order. */
1699 MAX_INIT_PRIORITY - priority);
1700 return get_section (buf, SECTION_WRITE, NULL);
1703 void
1704 default_named_section_asm_out_destructor (rtx symbol, int priority)
1706 section *sec;
1708 if (priority != DEFAULT_INIT_PRIORITY)
1709 sec = get_cdtor_priority_section (priority,
1710 /*constructor_p=*/false);
1711 else
1712 sec = get_section (".dtors", SECTION_WRITE, NULL);
1714 assemble_addr_to_section (symbol, sec);
1717 #ifdef DTORS_SECTION_ASM_OP
1718 void
1719 default_dtor_section_asm_out_destructor (rtx symbol,
1720 int priority ATTRIBUTE_UNUSED)
1722 assemble_addr_to_section (symbol, dtors_section);
1724 #endif
1726 void
1727 default_named_section_asm_out_constructor (rtx symbol, int priority)
1729 section *sec;
1731 if (priority != DEFAULT_INIT_PRIORITY)
1732 sec = get_cdtor_priority_section (priority,
1733 /*constructor_p=*/true);
1734 else
1735 sec = get_section (".ctors", SECTION_WRITE, NULL);
1737 assemble_addr_to_section (symbol, sec);
1740 #ifdef CTORS_SECTION_ASM_OP
1741 void
1742 default_ctor_section_asm_out_constructor (rtx symbol,
1743 int priority ATTRIBUTE_UNUSED)
1745 assemble_addr_to_section (symbol, ctors_section);
1747 #endif
1749 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1750 a nonzero value if the constant pool should be output before the
1751 start of the function, or a zero value if the pool should output
1752 after the end of the function. The default is to put it before the
1753 start. */
1755 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1756 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1757 #endif
1759 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1760 to be output to assembler.
1761 Set first_global_object_name and weak_global_object_name as appropriate. */
1763 void
1764 notice_global_symbol (tree decl)
1766 const char **t = &first_global_object_name;
1768 if (first_global_object_name
1769 || !TREE_PUBLIC (decl)
1770 || DECL_EXTERNAL (decl)
1771 || !DECL_NAME (decl)
1772 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1773 || (TREE_CODE (decl) != FUNCTION_DECL
1774 && (!VAR_P (decl)
1775 || (DECL_COMMON (decl)
1776 && (DECL_INITIAL (decl) == 0
1777 || DECL_INITIAL (decl) == error_mark_node)))))
1778 return;
1780 /* We win when global object is found, but it is useful to know about weak
1781 symbol as well so we can produce nicer unique names. */
1782 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1783 t = &weak_global_object_name;
1785 if (!*t)
1787 tree id = DECL_ASSEMBLER_NAME (decl);
1788 ultimate_transparent_alias_target (&id);
1789 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1793 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1794 current function goes into the cold section, so that targets can use
1795 current_function_section during RTL expansion. DECL describes the
1796 function. */
1798 void
1799 decide_function_section (tree decl)
1801 first_function_block_is_cold = false;
1803 if (DECL_SECTION_NAME (decl))
1805 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1806 /* Calls to function_section rely on first_function_block_is_cold
1807 being accurate. */
1808 first_function_block_is_cold = (node
1809 && node->frequency
1810 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1813 in_cold_section_p = first_function_block_is_cold;
1816 /* Get the function's name, as described by its RTL. This may be
1817 different from the DECL_NAME name used in the source file. */
1818 const char *
1819 get_fnname_from_decl (tree decl)
1821 rtx x = DECL_RTL (decl);
1822 gcc_assert (MEM_P (x));
1823 x = XEXP (x, 0);
1824 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1825 return XSTR (x, 0);
1828 /* Output assembler code for the constant pool of a function and associated
1829 with defining the name of the function. DECL describes the function.
1830 NAME is the function's name. For the constant pool, we use the current
1831 constant pool data. */
1833 void
1834 assemble_start_function (tree decl, const char *fnname)
1836 int align;
1837 char tmp_label[100];
1838 bool hot_label_written = false;
1840 if (crtl->has_bb_partition)
1842 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1843 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1844 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1845 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1846 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1847 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1848 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1849 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1850 const_labelno++;
1851 cold_function_name = NULL_TREE;
1853 else
1855 crtl->subsections.hot_section_label = NULL;
1856 crtl->subsections.cold_section_label = NULL;
1857 crtl->subsections.hot_section_end_label = NULL;
1858 crtl->subsections.cold_section_end_label = NULL;
1861 /* The following code does not need preprocessing in the assembler. */
1863 app_disable ();
1865 if (CONSTANT_POOL_BEFORE_FUNCTION)
1866 output_constant_pool (fnname, decl);
1868 align = symtab_node::get (decl)->definition_alignment ();
1870 /* Make sure the not and cold text (code) sections are properly
1871 aligned. This is necessary here in the case where the function
1872 has both hot and cold sections, because we don't want to re-set
1873 the alignment when the section switch happens mid-function. */
1875 if (crtl->has_bb_partition)
1877 first_function_block_is_cold = false;
1879 switch_to_section (unlikely_text_section ());
1880 assemble_align (align);
1881 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1883 /* When the function starts with a cold section, we need to explicitly
1884 align the hot section and write out the hot section label.
1885 But if the current function is a thunk, we do not have a CFG. */
1886 if (!cfun->is_thunk
1887 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1889 switch_to_section (text_section);
1890 assemble_align (align);
1891 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1892 hot_label_written = true;
1893 first_function_block_is_cold = true;
1895 in_cold_section_p = first_function_block_is_cold;
1899 /* Switch to the correct text section for the start of the function. */
1901 switch_to_section (function_section (decl), decl);
1902 if (crtl->has_bb_partition && !hot_label_written)
1903 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1905 /* Tell assembler to move to target machine's alignment for functions. */
1906 align = floor_log2 (align / BITS_PER_UNIT);
1907 if (align > 0)
1909 ASM_OUTPUT_ALIGN (asm_out_file, align);
1912 /* Handle a user-specified function alignment.
1913 Note that we still need to align to DECL_ALIGN, as above,
1914 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1915 if (! DECL_USER_ALIGN (decl)
1916 && align_functions.levels[0].log > align
1917 && optimize_function_for_speed_p (cfun))
1919 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1920 int align_log = align_functions.levels[0].log;
1921 #endif
1922 int max_skip = align_functions.levels[0].maxskip;
1923 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1924 && max_skip >= crtl->max_insn_address)
1925 max_skip = crtl->max_insn_address - 1;
1927 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1928 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1929 if (max_skip == align_functions.levels[0].maxskip)
1930 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1931 align_functions.levels[1].log,
1932 align_functions.levels[1].maxskip);
1933 #else
1934 ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
1935 #endif
1938 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1939 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1940 #endif
1942 if (!DECL_IGNORED_P (decl))
1943 (*debug_hooks->begin_function) (decl);
1945 /* Make function name accessible from other files, if appropriate. */
1947 if (TREE_PUBLIC (decl))
1949 notice_global_symbol (decl);
1951 globalize_decl (decl);
1953 maybe_assemble_visibility (decl);
1956 if (DECL_PRESERVE_P (decl))
1957 targetm.asm_out.mark_decl_preserved (fnname);
1959 unsigned short patch_area_size = crtl->patch_area_size;
1960 unsigned short patch_area_entry = crtl->patch_area_entry;
1962 /* Emit the patching area before the entry label, if any. */
1963 if (patch_area_entry > 0)
1964 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1965 patch_area_entry, true);
1967 /* Do any machine/system dependent processing of the function name. */
1968 #ifdef ASM_DECLARE_FUNCTION_NAME
1969 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1970 #else
1971 /* Standard thing is just output label for the function. */
1972 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1973 #endif /* ASM_DECLARE_FUNCTION_NAME */
1975 /* And the area after the label. Record it if we haven't done so yet. */
1976 if (patch_area_size > patch_area_entry)
1977 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1978 patch_area_size
1979 - patch_area_entry,
1980 patch_area_entry == 0);
1982 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1983 saw_no_split_stack = true;
1986 /* Output assembler code associated with defining the size of the
1987 function. DECL describes the function. NAME is the function's name. */
1989 void
1990 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1992 #ifdef ASM_DECLARE_FUNCTION_SIZE
1993 /* We could have switched section in the middle of the function. */
1994 if (crtl->has_bb_partition)
1995 switch_to_section (function_section (decl));
1996 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1997 #endif
1998 if (! CONSTANT_POOL_BEFORE_FUNCTION)
2000 output_constant_pool (fnname, decl);
2001 switch_to_section (function_section (decl)); /* need to switch back */
2003 /* Output labels for end of hot/cold text sections (to be used by
2004 debug info.) */
2005 if (crtl->has_bb_partition)
2007 section *save_text_section;
2009 save_text_section = in_section;
2010 switch_to_section (unlikely_text_section ());
2011 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
2012 if (cold_function_name != NULL_TREE)
2013 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
2014 IDENTIFIER_POINTER (cold_function_name),
2015 decl);
2016 #endif
2017 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
2018 if (first_function_block_is_cold)
2019 switch_to_section (text_section);
2020 else
2021 switch_to_section (function_section (decl));
2022 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
2023 switch_to_section (save_text_section);
2027 /* Assemble code to leave SIZE bytes of zeros. */
2029 void
2030 assemble_zeros (unsigned HOST_WIDE_INT size)
2032 /* Do no output if -fsyntax-only. */
2033 if (flag_syntax_only)
2034 return;
2036 #ifdef ASM_NO_SKIP_IN_TEXT
2037 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
2038 so we must output 0s explicitly in the text section. */
2039 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
2041 unsigned HOST_WIDE_INT i;
2042 for (i = 0; i < size; i++)
2043 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
2045 else
2046 #endif
2047 if (size > 0)
2048 ASM_OUTPUT_SKIP (asm_out_file, size);
2051 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
2053 void
2054 assemble_align (unsigned int align)
2056 if (align > BITS_PER_UNIT)
2058 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2062 /* Assemble a string constant with the specified C string as contents. */
2064 void
2065 assemble_string (const char *p, int size)
2067 int pos = 0;
2068 int maximum = 2000;
2070 /* If the string is very long, split it up. */
2072 while (pos < size)
2074 int thissize = size - pos;
2075 if (thissize > maximum)
2076 thissize = maximum;
2078 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
2080 pos += thissize;
2081 p += thissize;
2086 /* A noswitch_section_callback for lcomm_section. */
2088 static bool
2089 emit_local (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 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2095 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2096 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2097 size, align);
2098 return true;
2099 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2100 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2101 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2102 return true;
2103 #else
2104 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2105 return false;
2106 #endif
2109 /* A noswitch_section_callback for bss_noswitch_section. */
2111 #if defined ASM_OUTPUT_ALIGNED_BSS
2112 static bool
2113 emit_bss (tree decl ATTRIBUTE_UNUSED,
2114 const char *name ATTRIBUTE_UNUSED,
2115 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2116 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2118 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2119 get_variable_align (decl));
2120 return true;
2122 #endif
2124 /* A noswitch_section_callback for comm_section. */
2126 static bool
2127 emit_common (tree decl ATTRIBUTE_UNUSED,
2128 const char *name ATTRIBUTE_UNUSED,
2129 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2130 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2132 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2133 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2134 size, get_variable_align (decl));
2135 return true;
2136 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2137 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2138 get_variable_align (decl));
2139 return true;
2140 #else
2141 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2142 return false;
2143 #endif
2146 /* A noswitch_section_callback for tls_comm_section. */
2148 static bool
2149 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2150 const char *name ATTRIBUTE_UNUSED,
2151 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2152 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2154 #ifdef ASM_OUTPUT_TLS_COMMON
2155 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2156 return true;
2157 #else
2158 sorry ("thread-local COMMON data not implemented");
2159 return true;
2160 #endif
2163 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2164 NAME is the name of DECL's SYMBOL_REF. */
2166 static void
2167 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2168 unsigned int align)
2170 unsigned HOST_WIDE_INT size, rounded;
2172 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2173 rounded = size;
2175 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2176 size += asan_red_zone_size (size);
2178 /* Don't allocate zero bytes of common,
2179 since that means "undefined external" in the linker. */
2180 if (size == 0)
2181 rounded = 1;
2183 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2184 so that each uninitialized object starts on such a boundary. */
2185 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2186 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2187 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2189 if (!sect->noswitch.callback (decl, name, size, rounded)
2190 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2191 error ("requested alignment for %q+D is greater than "
2192 "implemented alignment of %wu", decl, rounded);
2195 /* A subroutine of assemble_variable. Output the label and contents of
2196 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2197 is as for assemble_variable. */
2199 static void
2200 assemble_variable_contents (tree decl, const char *name,
2201 bool dont_output_data, bool merge_strings)
2203 /* Do any machine/system dependent processing of the object. */
2204 #ifdef ASM_DECLARE_OBJECT_NAME
2205 last_assemble_variable_decl = decl;
2206 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2207 #else
2208 /* Standard thing is just output label for the object. */
2209 ASM_OUTPUT_LABEL (asm_out_file, name);
2210 #endif /* ASM_DECLARE_OBJECT_NAME */
2212 if (!dont_output_data)
2214 /* Caller is supposed to use varpool_get_constructor when it wants
2215 to output the body. */
2216 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2217 if (DECL_INITIAL (decl)
2218 && DECL_INITIAL (decl) != error_mark_node
2219 && !initializer_zerop (DECL_INITIAL (decl)))
2220 /* Output the actual data. */
2221 output_constant (DECL_INITIAL (decl),
2222 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2223 get_variable_align (decl),
2224 false, merge_strings);
2225 else
2226 /* Leave space for it. */
2227 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2228 targetm.asm_out.decl_end ();
2232 /* Write out assembly for the variable DECL, which is not defined in
2233 the current translation unit. */
2234 void
2235 assemble_undefined_decl (tree decl)
2237 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2238 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2241 /* Assemble everything that is needed for a variable or function declaration.
2242 Not used for automatic variables, and not used for function definitions.
2243 Should not be called for variables of incomplete structure type.
2245 TOP_LEVEL is nonzero if this variable has file scope.
2246 AT_END is nonzero if this is the special handling, at end of compilation,
2247 to define things that have had only tentative definitions.
2248 DONT_OUTPUT_DATA if nonzero means don't actually output the
2249 initial value (that will be done by the caller). */
2251 void
2252 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2253 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2255 const char *name;
2256 rtx decl_rtl, symbol;
2257 section *sect;
2258 unsigned int align;
2259 bool asan_protected = false;
2261 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2262 gcc_assert (VAR_P (decl));
2264 /* Emulated TLS had better not get this far. */
2265 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2267 last_assemble_variable_decl = 0;
2269 /* Normally no need to say anything here for external references,
2270 since assemble_external is called by the language-specific code
2271 when a declaration is first seen. */
2273 if (DECL_EXTERNAL (decl))
2274 return;
2276 /* Do nothing for global register variables. */
2277 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2279 TREE_ASM_WRITTEN (decl) = 1;
2280 return;
2283 /* If type was incomplete when the variable was declared,
2284 see if it is complete now. */
2286 if (DECL_SIZE (decl) == 0)
2287 layout_decl (decl, 0);
2289 /* Still incomplete => don't allocate it; treat the tentative defn
2290 (which is what it must have been) as an `extern' reference. */
2292 if (!dont_output_data && DECL_SIZE (decl) == 0)
2294 error ("storage size of %q+D isn%'t known", decl);
2295 TREE_ASM_WRITTEN (decl) = 1;
2296 return;
2299 /* The first declaration of a variable that comes through this function
2300 decides whether it is global (in C, has external linkage)
2301 or local (in C, has internal linkage). So do nothing more
2302 if this function has already run. */
2304 if (TREE_ASM_WRITTEN (decl))
2305 return;
2307 /* Make sure targetm.encode_section_info is invoked before we set
2308 ASM_WRITTEN. */
2309 decl_rtl = DECL_RTL (decl);
2311 TREE_ASM_WRITTEN (decl) = 1;
2313 /* Do no output if -fsyntax-only. */
2314 if (flag_syntax_only)
2315 return;
2317 if (! dont_output_data
2318 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2320 error ("size of variable %q+D is too large", decl);
2321 return;
2324 gcc_assert (MEM_P (decl_rtl));
2325 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2326 symbol = XEXP (decl_rtl, 0);
2328 /* If this symbol belongs to the tree constant pool, output the constant
2329 if it hasn't already been written. */
2330 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2332 tree decl = SYMBOL_REF_DECL (symbol);
2333 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2334 output_constant_def_contents (symbol);
2335 return;
2338 app_disable ();
2340 name = XSTR (symbol, 0);
2341 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2342 notice_global_symbol (decl);
2344 /* Compute the alignment of this data. */
2346 align_variable (decl, dont_output_data);
2348 if ((flag_sanitize & SANITIZE_ADDRESS)
2349 && asan_protect_global (decl))
2351 asan_protected = true;
2352 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2353 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2356 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2358 align = get_variable_align (decl);
2360 if (TREE_PUBLIC (decl))
2361 maybe_assemble_visibility (decl);
2363 if (DECL_PRESERVE_P (decl))
2364 targetm.asm_out.mark_decl_preserved (name);
2366 /* First make the assembler name(s) global if appropriate. */
2367 sect = get_variable_section (decl, false);
2368 if (TREE_PUBLIC (decl)
2369 && (sect->common.flags & SECTION_COMMON) == 0)
2370 globalize_decl (decl);
2372 /* Output any data that we will need to use the address of. */
2373 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2374 output_addressed_constants (DECL_INITIAL (decl), 0);
2376 /* dbxout.c needs to know this. */
2377 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2378 DECL_IN_TEXT_SECTION (decl) = 1;
2380 /* If the decl is part of an object_block, make sure that the decl
2381 has been positioned within its block, but do not write out its
2382 definition yet. output_object_blocks will do that later. */
2383 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2385 gcc_assert (!dont_output_data);
2386 place_block_symbol (symbol);
2388 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2389 assemble_noswitch_variable (decl, name, sect, align);
2390 else
2392 /* Special-case handling of vtv comdat sections. */
2393 if (sect->named.name
2394 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2395 handle_vtv_comdat_section (sect, decl);
2396 else
2397 switch_to_section (sect, decl);
2398 if (align > BITS_PER_UNIT)
2399 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2400 assemble_variable_contents (decl, name, dont_output_data,
2401 (sect->common.flags & SECTION_MERGE)
2402 && (sect->common.flags & SECTION_STRINGS));
2403 if (asan_protected)
2405 unsigned HOST_WIDE_INT int size
2406 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2407 assemble_zeros (asan_red_zone_size (size));
2413 /* Given a function declaration (FN_DECL), this function assembles the
2414 function into the .preinit_array section. */
2416 void
2417 assemble_vtv_preinit_initializer (tree fn_decl)
2419 section *sect;
2420 unsigned flags = SECTION_WRITE;
2421 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2423 flags |= SECTION_NOTYPE;
2424 sect = get_section (".preinit_array", flags, fn_decl);
2425 switch_to_section (sect);
2426 assemble_addr_to_section (symbol, sect);
2429 /* Return 1 if type TYPE contains any pointers. */
2431 static int
2432 contains_pointers_p (tree type)
2434 switch (TREE_CODE (type))
2436 case POINTER_TYPE:
2437 case REFERENCE_TYPE:
2438 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2439 so I'll play safe and return 1. */
2440 case OFFSET_TYPE:
2441 return 1;
2443 case RECORD_TYPE:
2444 case UNION_TYPE:
2445 case QUAL_UNION_TYPE:
2447 tree fields;
2448 /* For a type that has fields, see if the fields have pointers. */
2449 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2450 if (TREE_CODE (fields) == FIELD_DECL
2451 && contains_pointers_p (TREE_TYPE (fields)))
2452 return 1;
2453 return 0;
2456 case ARRAY_TYPE:
2457 /* An array type contains pointers if its element type does. */
2458 return contains_pointers_p (TREE_TYPE (type));
2460 default:
2461 return 0;
2465 /* We delay assemble_external processing until
2466 the compilation unit is finalized. This is the best we can do for
2467 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2468 it all the way to final. See PR 17982 for further discussion. */
2469 static GTY(()) tree pending_assemble_externals;
2471 #ifdef ASM_OUTPUT_EXTERNAL
2472 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2473 As a result, assemble_external can be called after the list of externals
2474 is processed and the pointer set destroyed. */
2475 static bool pending_assemble_externals_processed;
2477 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2478 TREE_LIST in assemble_external. */
2479 static hash_set<tree> *pending_assemble_externals_set;
2481 /* True if DECL is a function decl for which no out-of-line copy exists.
2482 It is assumed that DECL's assembler name has been set. */
2484 static bool
2485 incorporeal_function_p (tree decl)
2487 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl))
2489 const char *name;
2491 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2492 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2493 return true;
2495 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2496 /* Atomic or sync builtins which have survived this far will be
2497 resolved externally and therefore are not incorporeal. */
2498 if (strncmp (name, "__builtin_", 10) == 0)
2499 return true;
2501 return false;
2504 /* Actually do the tests to determine if this is necessary, and invoke
2505 ASM_OUTPUT_EXTERNAL. */
2506 static void
2507 assemble_external_real (tree decl)
2509 rtx rtl = DECL_RTL (decl);
2511 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2512 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2513 && !incorporeal_function_p (decl))
2515 /* Some systems do require some output. */
2516 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2517 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2520 #endif
2522 void
2523 process_pending_assemble_externals (void)
2525 #ifdef ASM_OUTPUT_EXTERNAL
2526 tree list;
2527 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2528 assemble_external_real (TREE_VALUE (list));
2530 pending_assemble_externals = 0;
2531 pending_assemble_externals_processed = true;
2532 delete pending_assemble_externals_set;
2533 #endif
2536 /* This TREE_LIST contains any weak symbol declarations waiting
2537 to be emitted. */
2538 static GTY(()) tree weak_decls;
2540 /* Output something to declare an external symbol to the assembler,
2541 and qualifiers such as weakness. (Most assemblers don't need
2542 extern declaration, so we normally output nothing.) Do nothing if
2543 DECL is not external. */
2545 void
2546 assemble_external (tree decl ATTRIBUTE_UNUSED)
2548 /* Make sure that the ASM_OUT_FILE is open.
2549 If it's not, we should not be calling this function. */
2550 gcc_assert (asm_out_file);
2552 /* In a perfect world, the following condition would be true.
2553 Sadly, the Go front end emit assembly *from the front end*,
2554 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2555 #if 0
2556 /* This function should only be called if we are expanding, or have
2557 expanded, to RTL.
2558 Ideally, only final.c would be calling this function, but it is
2559 not clear whether that would break things somehow. See PR 17982
2560 for further discussion. */
2561 gcc_assert (state == EXPANSION
2562 || state == FINISHED);
2563 #endif
2565 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2566 return;
2568 /* We want to output annotation for weak and external symbols at
2569 very last to check if they are references or not. */
2571 if (TARGET_SUPPORTS_WEAK
2572 && DECL_WEAK (decl)
2573 /* TREE_STATIC is a weird and abused creature which is not
2574 generally the right test for whether an entity has been
2575 locally emitted, inlined or otherwise not-really-extern, but
2576 for declarations that can be weak, it happens to be
2577 match. */
2578 && !TREE_STATIC (decl)
2579 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2580 && value_member (decl, weak_decls) == NULL_TREE)
2581 weak_decls = tree_cons (NULL, decl, weak_decls);
2583 #ifdef ASM_OUTPUT_EXTERNAL
2584 if (pending_assemble_externals_processed)
2586 assemble_external_real (decl);
2587 return;
2590 if (! pending_assemble_externals_set->add (decl))
2591 pending_assemble_externals = tree_cons (NULL, decl,
2592 pending_assemble_externals);
2593 #endif
2596 /* Similar, for calling a library function FUN. */
2598 void
2599 assemble_external_libcall (rtx fun)
2601 /* Declare library function name external when first used, if nec. */
2602 if (! SYMBOL_REF_USED (fun))
2604 SYMBOL_REF_USED (fun) = 1;
2605 targetm.asm_out.external_libcall (fun);
2609 /* Assemble a label named NAME. */
2611 void
2612 assemble_label (FILE *file, const char *name)
2614 ASM_OUTPUT_LABEL (file, name);
2617 /* Set the symbol_referenced flag for ID. */
2618 void
2619 mark_referenced (tree id)
2621 TREE_SYMBOL_REFERENCED (id) = 1;
2624 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2625 void
2626 mark_decl_referenced (tree decl)
2628 if (TREE_CODE (decl) == FUNCTION_DECL)
2630 /* Extern inline functions don't become needed when referenced.
2631 If we know a method will be emitted in other TU and no new
2632 functions can be marked reachable, just use the external
2633 definition. */
2634 struct cgraph_node *node = cgraph_node::get_create (decl);
2635 if (!DECL_EXTERNAL (decl)
2636 && !node->definition)
2637 node->mark_force_output ();
2639 else if (VAR_P (decl))
2641 varpool_node *node = varpool_node::get_create (decl);
2642 /* C++ frontend use mark_decl_references to force COMDAT variables
2643 to be output that might appear dead otherwise. */
2644 node->force_output = true;
2646 /* else do nothing - we can get various sorts of CST nodes here,
2647 which do not need to be marked. */
2651 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2652 starts with a *, the rest of NAME is output verbatim. Otherwise
2653 NAME is transformed in a target-specific way (usually by the
2654 addition of an underscore). */
2656 void
2657 assemble_name_raw (FILE *file, const char *name)
2659 if (name[0] == '*')
2660 fputs (&name[1], file);
2661 else
2662 ASM_OUTPUT_LABELREF (file, name);
2665 /* Return NAME that should actually be emitted, looking through
2666 transparent aliases. If NAME refers to an entity that is also
2667 represented as a tree (like a function or variable), mark the entity
2668 as referenced. */
2669 const char *
2670 assemble_name_resolve (const char *name)
2672 const char *real_name = targetm.strip_name_encoding (name);
2673 tree id = maybe_get_identifier (real_name);
2675 if (id)
2677 tree id_orig = id;
2679 mark_referenced (id);
2680 ultimate_transparent_alias_target (&id);
2681 if (id != id_orig)
2682 name = IDENTIFIER_POINTER (id);
2683 gcc_assert (! TREE_CHAIN (id));
2686 return name;
2689 /* Like assemble_name_raw, but should be used when NAME might refer to
2690 an entity that is also represented as a tree (like a function or
2691 variable). If NAME does refer to such an entity, that entity will
2692 be marked as referenced. */
2694 void
2695 assemble_name (FILE *file, const char *name)
2697 assemble_name_raw (file, assemble_name_resolve (name));
2700 /* Allocate SIZE bytes writable static space with a gensym name
2701 and return an RTX to refer to its address. */
2704 assemble_static_space (unsigned HOST_WIDE_INT size)
2706 char name[17];
2707 const char *namestring;
2708 rtx x;
2710 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2711 ++const_labelno;
2712 namestring = ggc_strdup (name);
2714 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2715 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2717 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2718 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2719 BIGGEST_ALIGNMENT);
2720 #else
2721 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2722 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2723 #else
2725 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2726 so that each uninitialized object starts on such a boundary. */
2727 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2728 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2729 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2730 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2731 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2732 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2734 #endif
2735 #endif
2736 return x;
2739 /* Assemble the static constant template for function entry trampolines.
2740 This is done at most once per compilation.
2741 Returns an RTX for the address of the template. */
2743 static GTY(()) rtx initial_trampoline;
2746 assemble_trampoline_template (void)
2748 char label[256];
2749 const char *name;
2750 int align;
2751 rtx symbol;
2753 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2755 if (initial_trampoline)
2756 return initial_trampoline;
2758 /* By default, put trampoline templates in read-only data section. */
2760 #ifdef TRAMPOLINE_SECTION
2761 switch_to_section (TRAMPOLINE_SECTION);
2762 #else
2763 switch_to_section (readonly_data_section);
2764 #endif
2766 /* Write the assembler code to define one. */
2767 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2768 if (align > 0)
2769 ASM_OUTPUT_ALIGN (asm_out_file, align);
2771 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2772 targetm.asm_out.trampoline_template (asm_out_file);
2774 /* Record the rtl to refer to it. */
2775 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2776 name = ggc_strdup (label);
2777 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2778 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2780 initial_trampoline = gen_const_mem (BLKmode, symbol);
2781 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2782 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2784 return initial_trampoline;
2787 /* A and B are either alignments or offsets. Return the minimum alignment
2788 that may be assumed after adding the two together. */
2790 static inline unsigned
2791 min_align (unsigned int a, unsigned int b)
2793 return least_bit_hwi (a | b);
2796 /* Return the assembler directive for creating a given kind of integer
2797 object. SIZE is the number of bytes in the object and ALIGNED_P
2798 indicates whether it is known to be aligned. Return NULL if the
2799 assembly dialect has no such directive.
2801 The returned string should be printed at the start of a new line and
2802 be followed immediately by the object's initial value. */
2804 const char *
2805 integer_asm_op (int size, int aligned_p)
2807 struct asm_int_op *ops;
2809 if (aligned_p)
2810 ops = &targetm.asm_out.aligned_op;
2811 else
2812 ops = &targetm.asm_out.unaligned_op;
2814 switch (size)
2816 case 1:
2817 return targetm.asm_out.byte_op;
2818 case 2:
2819 return ops->hi;
2820 case 3:
2821 return ops->psi;
2822 case 4:
2823 return ops->si;
2824 case 5:
2825 case 6:
2826 case 7:
2827 return ops->pdi;
2828 case 8:
2829 return ops->di;
2830 case 9:
2831 case 10:
2832 case 11:
2833 case 12:
2834 case 13:
2835 case 14:
2836 case 15:
2837 return ops->pti;
2838 case 16:
2839 return ops->ti;
2840 default:
2841 return NULL;
2845 /* Use directive OP to assemble an integer object X. Print OP at the
2846 start of the line, followed immediately by the value of X. */
2848 void
2849 assemble_integer_with_op (const char *op, rtx x)
2851 fputs (op, asm_out_file);
2852 output_addr_const (asm_out_file, x);
2853 fputc ('\n', asm_out_file);
2856 /* The default implementation of the asm_out.integer target hook. */
2858 bool
2859 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2860 unsigned int size ATTRIBUTE_UNUSED,
2861 int aligned_p ATTRIBUTE_UNUSED)
2863 const char *op = integer_asm_op (size, aligned_p);
2864 /* Avoid GAS bugs for large values. Specifically negative values whose
2865 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2866 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2867 return false;
2868 return op && (assemble_integer_with_op (op, x), true);
2871 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2872 the alignment of the integer in bits. Return 1 if we were able to output
2873 the constant, otherwise 0. We must be able to output the constant,
2874 if FORCE is nonzero. */
2876 bool
2877 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2879 int aligned_p;
2881 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2883 /* See if the target hook can handle this kind of object. */
2884 if (targetm.asm_out.integer (x, size, aligned_p))
2885 return true;
2887 /* If the object is a multi-byte one, try splitting it up. Split
2888 it into words it if is multi-word, otherwise split it into bytes. */
2889 if (size > 1)
2891 machine_mode omode, imode;
2892 unsigned int subalign;
2893 unsigned int subsize, i;
2894 enum mode_class mclass;
2896 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2897 subalign = MIN (align, subsize * BITS_PER_UNIT);
2898 if (GET_CODE (x) == CONST_FIXED)
2899 mclass = GET_MODE_CLASS (GET_MODE (x));
2900 else
2901 mclass = MODE_INT;
2903 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2904 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2906 for (i = 0; i < size; i += subsize)
2908 rtx partial = simplify_subreg (omode, x, imode, i);
2909 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2910 break;
2912 if (i == size)
2913 return true;
2915 /* If we've printed some of it, but not all of it, there's no going
2916 back now. */
2917 gcc_assert (!i);
2920 gcc_assert (!force);
2922 return false;
2925 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
2926 is the alignment of the constant in bits. If REVERSE is true, D is output
2927 in reverse storage order. */
2929 void
2930 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2931 bool reverse)
2933 long data[4] = {0, 0, 0, 0};
2934 int bitsize, nelts, nunits, units_per;
2935 rtx elt;
2937 /* This is hairy. We have a quantity of known size. real_to_target
2938 will put it into an array of *host* longs, 32 bits per element
2939 (even if long is more than 32 bits). We need to determine the
2940 number of array elements that are occupied (nelts) and the number
2941 of *target* min-addressable units that will be occupied in the
2942 object file (nunits). We cannot assume that 32 divides the
2943 mode's bitsize (size * BITS_PER_UNIT) evenly.
2945 size * BITS_PER_UNIT is used here to make sure that padding bits
2946 (which might appear at either end of the value; real_to_target
2947 will include the padding bits in its output array) are included. */
2949 nunits = GET_MODE_SIZE (mode);
2950 bitsize = nunits * BITS_PER_UNIT;
2951 nelts = CEIL (bitsize, 32);
2952 units_per = 32 / BITS_PER_UNIT;
2954 real_to_target (data, &d, mode);
2956 /* Put out the first word with the specified alignment. */
2957 unsigned int chunk_nunits = MIN (nunits, units_per);
2958 if (reverse)
2959 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
2960 else
2961 elt = GEN_INT (sext_hwi (data[0], chunk_nunits * BITS_PER_UNIT));
2962 assemble_integer (elt, chunk_nunits, align, 1);
2963 nunits -= chunk_nunits;
2965 /* Subsequent words need only 32-bit alignment. */
2966 align = min_align (align, 32);
2968 for (int i = 1; i < nelts; i++)
2970 chunk_nunits = MIN (nunits, units_per);
2971 if (reverse)
2972 elt = flip_storage_order (SImode,
2973 gen_int_mode (data[nelts - 1 - i], SImode));
2974 else
2975 elt = GEN_INT (sext_hwi (data[i], chunk_nunits * BITS_PER_UNIT));
2976 assemble_integer (elt, chunk_nunits, align, 1);
2977 nunits -= chunk_nunits;
2981 /* Given an expression EXP with a constant value,
2982 reduce it to the sum of an assembler symbol and an integer.
2983 Store them both in the structure *VALUE.
2984 EXP must be reducible. */
2986 class addr_const {
2987 public:
2988 rtx base;
2989 poly_int64 offset;
2992 static void
2993 decode_addr_const (tree exp, class addr_const *value)
2995 tree target = TREE_OPERAND (exp, 0);
2996 poly_int64 offset = 0;
2997 rtx x;
2999 while (1)
3001 poly_int64 bytepos;
3002 if (TREE_CODE (target) == COMPONENT_REF
3003 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
3004 &bytepos))
3006 offset += bytepos;
3007 target = TREE_OPERAND (target, 0);
3009 else if (TREE_CODE (target) == ARRAY_REF
3010 || TREE_CODE (target) == ARRAY_RANGE_REF)
3012 /* Truncate big offset. */
3013 offset
3014 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
3015 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
3016 target = TREE_OPERAND (target, 0);
3018 else if (TREE_CODE (target) == MEM_REF
3019 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
3021 offset += mem_ref_offset (target).force_shwi ();
3022 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
3024 else if (TREE_CODE (target) == INDIRECT_REF
3025 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
3026 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
3027 == ADDR_EXPR)
3028 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
3029 else
3030 break;
3033 switch (TREE_CODE (target))
3035 case VAR_DECL:
3036 case FUNCTION_DECL:
3037 x = DECL_RTL (target);
3038 break;
3040 case LABEL_DECL:
3041 x = gen_rtx_MEM (FUNCTION_MODE,
3042 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
3043 break;
3045 case REAL_CST:
3046 case FIXED_CST:
3047 case STRING_CST:
3048 case COMPLEX_CST:
3049 case CONSTRUCTOR:
3050 case INTEGER_CST:
3051 x = lookup_constant_def (target);
3052 /* Should have been added by output_addressed_constants. */
3053 gcc_assert (x);
3054 break;
3056 case INDIRECT_REF:
3057 /* This deals with absolute addresses. */
3058 offset += tree_to_shwi (TREE_OPERAND (target, 0));
3059 x = gen_rtx_MEM (QImode,
3060 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
3061 break;
3063 case COMPOUND_LITERAL_EXPR:
3064 gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
3065 x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
3066 break;
3068 default:
3069 gcc_unreachable ();
3072 gcc_assert (MEM_P (x));
3073 x = XEXP (x, 0);
3075 value->base = x;
3076 value->offset = offset;
3079 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
3081 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
3083 /* Constant pool accessor function. */
3085 hash_table<tree_descriptor_hasher> *
3086 constant_pool_htab (void)
3088 return const_desc_htab;
3091 /* Compute a hash code for a constant expression. */
3093 hashval_t
3094 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3096 return ptr->hash;
3099 static hashval_t
3100 const_hash_1 (const tree exp)
3102 const char *p;
3103 hashval_t hi;
3104 int len, i;
3105 enum tree_code code = TREE_CODE (exp);
3107 /* Either set P and LEN to the address and len of something to hash and
3108 exit the switch or return a value. */
3110 switch (code)
3112 case INTEGER_CST:
3113 p = (char *) &TREE_INT_CST_ELT (exp, 0);
3114 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3115 break;
3117 case REAL_CST:
3118 return real_hash (TREE_REAL_CST_PTR (exp));
3120 case FIXED_CST:
3121 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3123 case STRING_CST:
3124 p = TREE_STRING_POINTER (exp);
3125 len = TREE_STRING_LENGTH (exp);
3126 break;
3128 case COMPLEX_CST:
3129 return (const_hash_1 (TREE_REALPART (exp)) * 5
3130 + const_hash_1 (TREE_IMAGPART (exp)));
3132 case VECTOR_CST:
3134 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3135 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3136 unsigned int count = vector_cst_encoded_nelts (exp);
3137 for (unsigned int i = 0; i < count; ++i)
3138 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3139 return hi;
3142 case CONSTRUCTOR:
3144 unsigned HOST_WIDE_INT idx;
3145 tree value;
3147 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3149 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3150 if (value)
3151 hi = hi * 603 + const_hash_1 (value);
3153 return hi;
3156 case ADDR_EXPR:
3157 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3158 return const_hash_1 (TREE_OPERAND (exp, 0));
3160 /* Fallthru. */
3161 case FDESC_EXPR:
3163 class addr_const value;
3165 decode_addr_const (exp, &value);
3166 switch (GET_CODE (value.base))
3168 case SYMBOL_REF:
3169 /* Don't hash the address of the SYMBOL_REF;
3170 only use the offset and the symbol name. */
3171 hi = value.offset.coeffs[0];
3172 p = XSTR (value.base, 0);
3173 for (i = 0; p[i] != 0; i++)
3174 hi = ((hi * 613) + (unsigned) (p[i]));
3175 break;
3177 case LABEL_REF:
3178 hi = (value.offset.coeffs[0]
3179 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3180 break;
3182 default:
3183 gcc_unreachable ();
3186 return hi;
3188 case PLUS_EXPR:
3189 case POINTER_PLUS_EXPR:
3190 case MINUS_EXPR:
3191 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3192 + const_hash_1 (TREE_OPERAND (exp, 1)));
3194 CASE_CONVERT:
3195 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3197 default:
3198 /* A language specific constant. Just hash the code. */
3199 return code;
3202 /* Compute hashing function. */
3203 hi = len;
3204 for (i = 0; i < len; i++)
3205 hi = ((hi * 613) + (unsigned) (p[i]));
3207 return hi;
3210 /* Wrapper of compare_constant, for the htab interface. */
3211 bool
3212 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3213 constant_descriptor_tree *c2)
3215 if (c1->hash != c2->hash)
3216 return 0;
3217 return compare_constant (c1->value, c2->value);
3220 /* Compare t1 and t2, and return 1 only if they are known to result in
3221 the same bit pattern on output. */
3223 static int
3224 compare_constant (const tree t1, const tree t2)
3226 enum tree_code typecode;
3228 if (t1 == NULL_TREE)
3229 return t2 == NULL_TREE;
3230 if (t2 == NULL_TREE)
3231 return 0;
3233 if (TREE_CODE (t1) != TREE_CODE (t2))
3234 return 0;
3236 switch (TREE_CODE (t1))
3238 case INTEGER_CST:
3239 /* Integer constants are the same only if the same width of type. */
3240 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3241 return 0;
3242 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3243 return 0;
3244 return tree_int_cst_equal (t1, t2);
3246 case REAL_CST:
3247 /* Real constants are the same only if the same width of type. In
3248 addition to the same width, we need to check whether the modes are the
3249 same. There might be two floating point modes that are the same size
3250 but have different representations, such as the PowerPC that has 2
3251 different 128-bit floating point types (IBM extended double and IEEE
3252 128-bit floating point). */
3253 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3254 return 0;
3255 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3256 return 0;
3257 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3259 case FIXED_CST:
3260 /* Fixed constants are the same only if the same width of type. */
3261 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3262 return 0;
3264 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3266 case STRING_CST:
3267 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3268 || int_size_in_bytes (TREE_TYPE (t1))
3269 != int_size_in_bytes (TREE_TYPE (t2)))
3270 return 0;
3272 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3273 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3274 TREE_STRING_LENGTH (t1)));
3276 case COMPLEX_CST:
3277 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3278 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3280 case VECTOR_CST:
3282 if (VECTOR_CST_NPATTERNS (t1)
3283 != VECTOR_CST_NPATTERNS (t2))
3284 return 0;
3286 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3287 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3288 return 0;
3290 unsigned int count = vector_cst_encoded_nelts (t1);
3291 for (unsigned int i = 0; i < count; ++i)
3292 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3293 VECTOR_CST_ENCODED_ELT (t2, i)))
3294 return 0;
3296 return 1;
3299 case CONSTRUCTOR:
3301 vec<constructor_elt, va_gc> *v1, *v2;
3302 unsigned HOST_WIDE_INT idx;
3304 typecode = TREE_CODE (TREE_TYPE (t1));
3305 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3306 return 0;
3308 if (typecode == ARRAY_TYPE)
3310 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3311 /* For arrays, check that mode, size and storage order match. */
3312 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3313 || size_1 == -1
3314 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3315 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3316 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3317 return 0;
3319 else
3321 /* For record and union constructors, require exact type
3322 equality. */
3323 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3324 return 0;
3327 v1 = CONSTRUCTOR_ELTS (t1);
3328 v2 = CONSTRUCTOR_ELTS (t2);
3329 if (vec_safe_length (v1) != vec_safe_length (v2))
3330 return 0;
3332 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3334 constructor_elt *c1 = &(*v1)[idx];
3335 constructor_elt *c2 = &(*v2)[idx];
3337 /* Check that each value is the same... */
3338 if (!compare_constant (c1->value, c2->value))
3339 return 0;
3340 /* ... and that they apply to the same fields! */
3341 if (typecode == ARRAY_TYPE)
3343 if (!compare_constant (c1->index, c2->index))
3344 return 0;
3346 else
3348 if (c1->index != c2->index)
3349 return 0;
3353 return 1;
3356 case ADDR_EXPR:
3357 case FDESC_EXPR:
3359 class addr_const value1, value2;
3360 enum rtx_code code;
3361 int ret;
3363 decode_addr_const (t1, &value1);
3364 decode_addr_const (t2, &value2);
3366 if (maybe_ne (value1.offset, value2.offset))
3367 return 0;
3369 code = GET_CODE (value1.base);
3370 if (code != GET_CODE (value2.base))
3371 return 0;
3373 switch (code)
3375 case SYMBOL_REF:
3376 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3377 break;
3379 case LABEL_REF:
3380 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3381 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3382 break;
3384 default:
3385 gcc_unreachable ();
3387 return ret;
3390 case PLUS_EXPR:
3391 case POINTER_PLUS_EXPR:
3392 case MINUS_EXPR:
3393 case RANGE_EXPR:
3394 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3395 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3397 CASE_CONVERT:
3398 case VIEW_CONVERT_EXPR:
3399 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3401 default:
3402 return 0;
3405 gcc_unreachable ();
3408 /* Return the section into which constant EXP should be placed. */
3410 static section *
3411 get_constant_section (tree exp, unsigned int align)
3413 return targetm.asm_out.select_section (exp,
3414 compute_reloc_for_constant (exp),
3415 align);
3418 /* Return the size of constant EXP in bytes. */
3420 static HOST_WIDE_INT
3421 get_constant_size (tree exp)
3423 HOST_WIDE_INT size;
3425 size = int_size_in_bytes (TREE_TYPE (exp));
3426 gcc_checking_assert (size >= 0);
3427 gcc_checking_assert (TREE_CODE (exp) != STRING_CST
3428 || size >= TREE_STRING_LENGTH (exp));
3429 return size;
3432 /* Subroutine of output_constant_def:
3433 No constant equal to EXP is known to have been output.
3434 Make a constant descriptor to enter EXP in the hash table.
3435 Assign the label number and construct RTL to refer to the
3436 constant's location in memory.
3437 Caller is responsible for updating the hash table. */
3439 static struct constant_descriptor_tree *
3440 build_constant_desc (tree exp)
3442 struct constant_descriptor_tree *desc;
3443 rtx symbol, rtl;
3444 char label[256];
3445 int labelno;
3446 tree decl;
3448 desc = ggc_alloc<constant_descriptor_tree> ();
3449 desc->value = exp;
3451 /* Create a string containing the label name, in LABEL. */
3452 labelno = const_labelno++;
3453 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3455 /* Construct the VAR_DECL associated with the constant. */
3456 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3457 TREE_TYPE (exp));
3458 DECL_ARTIFICIAL (decl) = 1;
3459 DECL_IGNORED_P (decl) = 1;
3460 TREE_READONLY (decl) = 1;
3461 TREE_STATIC (decl) = 1;
3462 TREE_ADDRESSABLE (decl) = 1;
3463 /* We don't set the RTL yet as this would cause varpool to assume that the
3464 variable is referenced. Moreover, it would just be dropped in LTO mode.
3465 Instead we set the flag that will be recognized in make_decl_rtl. */
3466 DECL_IN_CONSTANT_POOL (decl) = 1;
3467 DECL_INITIAL (decl) = desc->value;
3468 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3469 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3470 if (TREE_CODE (exp) == STRING_CST)
3471 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3472 else
3474 align_variable (decl, 0);
3475 if (DECL_ALIGN (decl) < GET_MODE_ALIGNMENT (DECL_MODE (decl))
3476 && ((optab_handler (movmisalign_optab, DECL_MODE (decl))
3477 != CODE_FOR_nothing)
3478 || targetm.slow_unaligned_access (DECL_MODE (decl),
3479 DECL_ALIGN (decl))))
3480 SET_DECL_ALIGN (decl, GET_MODE_ALIGNMENT (DECL_MODE (decl)));
3483 /* Now construct the SYMBOL_REF and the MEM. */
3484 if (use_object_blocks_p ())
3486 int align = (TREE_CODE (decl) == CONST_DECL
3487 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3488 ? DECL_ALIGN (decl)
3489 : symtab_node::get (decl)->definition_alignment ());
3490 section *sect = get_constant_section (exp, align);
3491 symbol = create_block_symbol (ggc_strdup (label),
3492 get_block_for_section (sect), -1);
3494 else
3495 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3496 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3497 SET_SYMBOL_REF_DECL (symbol, decl);
3498 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3500 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3501 set_mem_alias_set (rtl, 0);
3503 /* Putting EXP into the literal pool might have imposed a different
3504 alignment which should be visible in the RTX as well. */
3505 set_mem_align (rtl, DECL_ALIGN (decl));
3507 /* We cannot share RTX'es in pool entries.
3508 Mark this piece of RTL as required for unsharing. */
3509 RTX_FLAG (rtl, used) = 1;
3511 /* Set flags or add text to the name to record information, such as
3512 that it is a local symbol. If the name is changed, the macro
3513 ASM_OUTPUT_LABELREF will have to know how to strip this
3514 information. This call might invalidate our local variable
3515 SYMBOL; we can't use it afterward. */
3516 targetm.encode_section_info (exp, rtl, true);
3518 desc->rtl = rtl;
3520 return desc;
3523 /* Subroutine of output_constant_def and tree_output_constant_def:
3524 Add a constant to the hash table that tracks which constants
3525 already have labels. */
3527 static constant_descriptor_tree *
3528 add_constant_to_table (tree exp, int defer)
3530 /* The hash table methods may call output_constant_def for addressed
3531 constants, so handle them first. */
3532 output_addressed_constants (exp, defer);
3534 /* Sanity check to catch recursive insertion. */
3535 static bool inserting;
3536 gcc_assert (!inserting);
3537 inserting = true;
3539 /* Look up EXP in the table of constant descriptors. If we didn't
3540 find it, create a new one. */
3541 struct constant_descriptor_tree key;
3542 key.value = exp;
3543 key.hash = const_hash_1 (exp);
3544 constant_descriptor_tree **loc
3545 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3547 inserting = false;
3549 struct constant_descriptor_tree *desc = *loc;
3550 if (!desc)
3552 desc = build_constant_desc (exp);
3553 desc->hash = key.hash;
3554 *loc = desc;
3557 return desc;
3560 /* Return an rtx representing a reference to constant data in memory
3561 for the constant expression EXP.
3563 If assembler code for such a constant has already been output,
3564 return an rtx to refer to it.
3565 Otherwise, output such a constant in memory
3566 and generate an rtx for it.
3568 If DEFER is nonzero, this constant can be deferred and output only
3569 if referenced in the function after all optimizations.
3571 `const_desc_table' records which constants already have label strings. */
3574 output_constant_def (tree exp, int defer)
3576 struct constant_descriptor_tree *desc = add_constant_to_table (exp, defer);
3577 maybe_output_constant_def_contents (desc, defer);
3578 return desc->rtl;
3581 /* Subroutine of output_constant_def: Decide whether or not we need to
3582 output the constant DESC now, and if so, do it. */
3583 static void
3584 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3585 int defer)
3587 rtx symbol = XEXP (desc->rtl, 0);
3588 tree exp = desc->value;
3590 if (flag_syntax_only)
3591 return;
3593 if (TREE_ASM_WRITTEN (exp))
3594 /* Already output; don't do it again. */
3595 return;
3597 /* We can always defer constants as long as the context allows
3598 doing so. */
3599 if (defer)
3601 /* Increment n_deferred_constants if it exists. It needs to be at
3602 least as large as the number of constants actually referred to
3603 by the function. If it's too small we'll stop looking too early
3604 and fail to emit constants; if it's too large we'll only look
3605 through the entire function when we could have stopped earlier. */
3606 if (cfun)
3607 n_deferred_constants++;
3608 return;
3611 output_constant_def_contents (symbol);
3614 /* Subroutine of output_constant_def_contents. Output the definition
3615 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3616 constant's alignment in bits. */
3618 static void
3619 assemble_constant_contents (tree exp, const char *label, unsigned int align,
3620 bool merge_strings)
3622 HOST_WIDE_INT size;
3624 size = get_constant_size (exp);
3626 /* Do any machine/system dependent processing of the constant. */
3627 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3629 /* Output the value of EXP. */
3630 output_constant (exp, size, align, false, merge_strings);
3632 targetm.asm_out.decl_end ();
3635 /* We must output the constant data referred to by SYMBOL; do so. */
3637 static void
3638 output_constant_def_contents (rtx symbol)
3640 tree decl = SYMBOL_REF_DECL (symbol);
3641 tree exp = DECL_INITIAL (decl);
3642 bool asan_protected = false;
3644 /* Make sure any other constants whose addresses appear in EXP
3645 are assigned label numbers. */
3646 output_addressed_constants (exp, 0);
3648 /* We are no longer deferring this constant. */
3649 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3651 if ((flag_sanitize & SANITIZE_ADDRESS)
3652 && TREE_CODE (exp) == STRING_CST
3653 && asan_protect_global (exp))
3655 asan_protected = true;
3656 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3657 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3660 /* If the constant is part of an object block, make sure that the
3661 decl has been positioned within its block, but do not write out
3662 its definition yet. output_object_blocks will do that later. */
3663 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3664 place_block_symbol (symbol);
3665 else
3667 int align = (TREE_CODE (decl) == CONST_DECL
3668 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3669 ? DECL_ALIGN (decl)
3670 : symtab_node::get (decl)->definition_alignment ());
3671 section *sect = get_constant_section (exp, align);
3672 switch_to_section (sect);
3673 if (align > BITS_PER_UNIT)
3674 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3675 assemble_constant_contents (exp, XSTR (symbol, 0), align,
3676 (sect->common.flags & SECTION_MERGE)
3677 && (sect->common.flags & SECTION_STRINGS));
3678 if (asan_protected)
3680 HOST_WIDE_INT size = get_constant_size (exp);
3681 assemble_zeros (asan_red_zone_size (size));
3686 /* Look up EXP in the table of constant descriptors. Return the rtl
3687 if it has been emitted, else null. */
3690 lookup_constant_def (tree exp)
3692 struct constant_descriptor_tree key;
3694 key.value = exp;
3695 key.hash = const_hash_1 (exp);
3696 constant_descriptor_tree *desc
3697 = const_desc_htab->find_with_hash (&key, key.hash);
3699 return (desc ? desc->rtl : NULL_RTX);
3702 /* Return a tree representing a reference to constant data in memory
3703 for the constant expression EXP.
3705 This is the counterpart of output_constant_def at the Tree level. */
3707 tree
3708 tree_output_constant_def (tree exp)
3710 struct constant_descriptor_tree *desc = add_constant_to_table (exp, 1);
3711 tree decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3712 varpool_node::finalize_decl (decl);
3713 return decl;
3716 class GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3717 public:
3718 class constant_descriptor_rtx *next;
3719 rtx mem;
3720 rtx sym;
3721 rtx constant;
3722 HOST_WIDE_INT offset;
3723 hashval_t hash;
3724 fixed_size_mode mode;
3725 unsigned int align;
3726 int labelno;
3727 int mark;
3730 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3732 static hashval_t hash (constant_descriptor_rtx *);
3733 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3736 /* Used in the hash tables to avoid outputting the same constant
3737 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3738 are output once per function, not once per file. */
3739 /* ??? Only a few targets need per-function constant pools. Most
3740 can use one per-file pool. Should add a targetm bit to tell the
3741 difference. */
3743 struct GTY(()) rtx_constant_pool {
3744 /* Pointers to first and last constant in pool, as ordered by offset. */
3745 class constant_descriptor_rtx *first;
3746 class constant_descriptor_rtx *last;
3748 /* Hash facility for making memory-constants from constant rtl-expressions.
3749 It is used on RISC machines where immediate integer arguments and
3750 constant addresses are restricted so that such constants must be stored
3751 in memory. */
3752 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3754 /* Current offset in constant pool (does not include any
3755 machine-specific header). */
3756 HOST_WIDE_INT offset;
3759 /* Hash and compare functions for const_rtx_htab. */
3761 hashval_t
3762 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3764 return desc->hash;
3767 bool
3768 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3769 constant_descriptor_rtx *y)
3771 if (x->mode != y->mode)
3772 return 0;
3773 return rtx_equal_p (x->constant, y->constant);
3776 /* Hash one component of a constant. */
3778 static hashval_t
3779 const_rtx_hash_1 (const_rtx x)
3781 unsigned HOST_WIDE_INT hwi;
3782 machine_mode mode;
3783 enum rtx_code code;
3784 hashval_t h;
3785 int i;
3787 code = GET_CODE (x);
3788 mode = GET_MODE (x);
3789 h = (hashval_t) code * 1048573 + mode;
3791 switch (code)
3793 case CONST_INT:
3794 hwi = INTVAL (x);
3796 fold_hwi:
3798 int shift = sizeof (hashval_t) * CHAR_BIT;
3799 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3801 h ^= (hashval_t) hwi;
3802 for (i = 1; i < n; ++i)
3804 hwi >>= shift;
3805 h ^= (hashval_t) hwi;
3808 break;
3810 case CONST_WIDE_INT:
3811 hwi = 0;
3813 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3814 hwi ^= CONST_WIDE_INT_ELT (x, i);
3815 goto fold_hwi;
3818 case CONST_DOUBLE:
3819 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3821 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3822 goto fold_hwi;
3824 else
3825 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3826 break;
3828 case CONST_FIXED:
3829 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3830 break;
3832 case SYMBOL_REF:
3833 h ^= htab_hash_string (XSTR (x, 0));
3834 break;
3836 case LABEL_REF:
3837 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3838 break;
3840 case UNSPEC:
3841 case UNSPEC_VOLATILE:
3842 h = h * 251 + XINT (x, 1);
3843 break;
3845 default:
3846 break;
3849 return h;
3852 /* Compute a hash value for X, which should be a constant. */
3854 static hashval_t
3855 const_rtx_hash (rtx x)
3857 hashval_t h = 0;
3858 subrtx_iterator::array_type array;
3859 FOR_EACH_SUBRTX (iter, array, x, ALL)
3860 h = h * 509 + const_rtx_hash_1 (*iter);
3861 return h;
3865 /* Create and return a new rtx constant pool. */
3867 static struct rtx_constant_pool *
3868 create_constant_pool (void)
3870 struct rtx_constant_pool *pool;
3872 pool = ggc_alloc<rtx_constant_pool> ();
3873 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3874 pool->first = NULL;
3875 pool->last = NULL;
3876 pool->offset = 0;
3877 return pool;
3880 /* Initialize constant pool hashing for a new function. */
3882 void
3883 init_varasm_status (void)
3885 crtl->varasm.pool = create_constant_pool ();
3886 crtl->varasm.deferred_constants = 0;
3889 /* Given a MINUS expression, simplify it if both sides
3890 include the same symbol. */
3893 simplify_subtraction (rtx x)
3895 rtx r = simplify_rtx (x);
3896 return r ? r : x;
3899 /* Given a constant rtx X, make (or find) a memory constant for its value
3900 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
3901 of X. */
3904 force_const_mem (machine_mode in_mode, rtx x)
3906 class constant_descriptor_rtx *desc, tmp;
3907 struct rtx_constant_pool *pool;
3908 char label[256];
3909 rtx def, symbol;
3910 hashval_t hash;
3911 unsigned int align;
3912 constant_descriptor_rtx **slot;
3913 fixed_size_mode mode;
3915 /* We can't force variable-sized objects to memory. */
3916 if (!is_a <fixed_size_mode> (in_mode, &mode))
3917 return NULL_RTX;
3919 /* If we're not allowed to drop X into the constant pool, don't. */
3920 if (targetm.cannot_force_const_mem (mode, x))
3921 return NULL_RTX;
3923 /* Record that this function has used a constant pool entry. */
3924 crtl->uses_const_pool = 1;
3926 /* Decide which pool to use. */
3927 pool = (targetm.use_blocks_for_constant_p (mode, x)
3928 ? shared_constant_pool
3929 : crtl->varasm.pool);
3931 /* Lookup the value in the hashtable. */
3932 tmp.constant = x;
3933 tmp.mode = mode;
3934 hash = const_rtx_hash (x);
3935 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3936 desc = *slot;
3938 /* If the constant was already present, return its memory. */
3939 if (desc)
3940 return copy_rtx (desc->mem);
3942 /* Otherwise, create a new descriptor. */
3943 desc = ggc_alloc<constant_descriptor_rtx> ();
3944 *slot = desc;
3946 /* Align the location counter as required by EXP's data type. */
3947 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3948 align = targetm.static_rtx_alignment (align_mode);
3950 pool->offset += (align / BITS_PER_UNIT) - 1;
3951 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3953 desc->next = NULL;
3954 desc->constant = copy_rtx (tmp.constant);
3955 desc->offset = pool->offset;
3956 desc->hash = hash;
3957 desc->mode = mode;
3958 desc->align = align;
3959 desc->labelno = const_labelno;
3960 desc->mark = 0;
3962 pool->offset += GET_MODE_SIZE (mode);
3963 if (pool->last)
3964 pool->last->next = desc;
3965 else
3966 pool->first = pool->last = desc;
3967 pool->last = desc;
3969 /* Create a string containing the label name, in LABEL. */
3970 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3971 ++const_labelno;
3973 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3974 the constants pool. */
3975 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3977 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3978 symbol = create_block_symbol (ggc_strdup (label),
3979 get_block_for_section (sect), -1);
3981 else
3982 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3983 desc->sym = symbol;
3984 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3985 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3986 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3988 /* Construct the MEM. */
3989 desc->mem = def = gen_const_mem (mode, symbol);
3990 set_mem_align (def, align);
3992 /* If we're dropping a label to the constant pool, make sure we
3993 don't delete it. */
3994 if (GET_CODE (x) == LABEL_REF)
3995 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3997 return copy_rtx (def);
4000 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
4003 get_pool_constant (const_rtx addr)
4005 return SYMBOL_REF_CONSTANT (addr)->constant;
4008 /* Given a constant pool SYMBOL_REF, return the corresponding constant
4009 and whether it has been output or not. */
4012 get_pool_constant_mark (rtx addr, bool *pmarked)
4014 class constant_descriptor_rtx *desc;
4016 desc = SYMBOL_REF_CONSTANT (addr);
4017 *pmarked = (desc->mark != 0);
4018 return desc->constant;
4021 /* Similar, return the mode. */
4023 fixed_size_mode
4024 get_pool_mode (const_rtx addr)
4026 return SYMBOL_REF_CONSTANT (addr)->mode;
4029 /* Return TRUE if and only if the constant pool has no entries. Note
4030 that even entries we might end up choosing not to emit are counted
4031 here, so there is the potential for missed optimizations. */
4033 bool
4034 constant_pool_empty_p (void)
4036 return crtl->varasm.pool->first == NULL;
4039 /* Worker function for output_constant_pool_1. Emit assembly for X
4040 in MODE with known alignment ALIGN. */
4042 static void
4043 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
4045 switch (GET_MODE_CLASS (mode))
4047 case MODE_FLOAT:
4048 case MODE_DECIMAL_FLOAT:
4050 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
4051 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
4052 as_a <scalar_float_mode> (mode), align, false);
4053 break;
4056 case MODE_INT:
4057 case MODE_PARTIAL_INT:
4058 case MODE_FRACT:
4059 case MODE_UFRACT:
4060 case MODE_ACCUM:
4061 case MODE_UACCUM:
4062 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
4063 break;
4065 case MODE_VECTOR_BOOL:
4067 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4069 /* Pick the smallest integer mode that contains at least one
4070 whole element. Often this is byte_mode and contains more
4071 than one element. */
4072 unsigned int nelts = GET_MODE_NUNITS (mode);
4073 unsigned int elt_bits = GET_MODE_BITSIZE (mode) / nelts;
4074 unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
4075 scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
4077 /* Build the constant up one integer at a time. */
4078 unsigned int elts_per_int = int_bits / elt_bits;
4079 for (unsigned int i = 0; i < nelts; i += elts_per_int)
4081 unsigned HOST_WIDE_INT value = 0;
4082 unsigned int limit = MIN (nelts - i, elts_per_int);
4083 for (unsigned int j = 0; j < limit; ++j)
4084 if (INTVAL (CONST_VECTOR_ELT (x, i + j)) != 0)
4085 value |= 1 << (j * elt_bits);
4086 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
4087 i != 0 ? MIN (align, int_bits) : align);
4089 break;
4091 case MODE_VECTOR_FLOAT:
4092 case MODE_VECTOR_INT:
4093 case MODE_VECTOR_FRACT:
4094 case MODE_VECTOR_UFRACT:
4095 case MODE_VECTOR_ACCUM:
4096 case MODE_VECTOR_UACCUM:
4098 int i, units;
4099 scalar_mode submode = GET_MODE_INNER (mode);
4100 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4102 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4103 units = GET_MODE_NUNITS (mode);
4105 for (i = 0; i < units; i++)
4107 rtx elt = CONST_VECTOR_ELT (x, i);
4108 output_constant_pool_2 (submode, elt, i ? subalign : align);
4111 break;
4113 default:
4114 gcc_unreachable ();
4118 /* Worker function for output_constant_pool. Emit constant DESC,
4119 giving it ALIGN bits of alignment. */
4121 static void
4122 output_constant_pool_1 (class constant_descriptor_rtx *desc,
4123 unsigned int align)
4125 rtx x, tmp;
4127 x = desc->constant;
4129 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4130 whose CODE_LABEL has been deleted. This can occur if a jump table
4131 is eliminated by optimization. If so, write a constant of zero
4132 instead. Note that this can also happen by turning the
4133 CODE_LABEL into a NOTE. */
4134 /* ??? This seems completely and utterly wrong. Certainly it's
4135 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4136 functioning even with rtx_insn::deleted and friends. */
4138 tmp = x;
4139 switch (GET_CODE (tmp))
4141 case CONST:
4142 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4143 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4144 break;
4145 tmp = XEXP (XEXP (tmp, 0), 0);
4146 /* FALLTHRU */
4148 case LABEL_REF:
4150 rtx_insn *insn = label_ref_label (tmp);
4151 gcc_assert (!insn->deleted ());
4152 gcc_assert (!NOTE_P (insn)
4153 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4154 break;
4157 default:
4158 break;
4161 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4162 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4163 align, desc->labelno, done);
4164 #endif
4166 assemble_align (align);
4168 /* Output the label. */
4169 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4171 /* Output the data.
4172 Pass actual alignment value while emitting string constant to asm code
4173 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4174 assuming that the data is already aligned which prevents the generation
4175 of fix-up table entries. */
4176 output_constant_pool_2 (desc->mode, x, desc->align);
4178 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4179 sections have proper size. */
4180 if (align > GET_MODE_BITSIZE (desc->mode)
4181 && in_section
4182 && (in_section->common.flags & SECTION_MERGE))
4183 assemble_align (align);
4185 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4186 done:
4187 #endif
4188 return;
4191 /* Recompute the offsets of entries in POOL, and the overall size of
4192 POOL. Do this after calling mark_constant_pool to ensure that we
4193 are computing the offset values for the pool which we will actually
4194 emit. */
4196 static void
4197 recompute_pool_offsets (struct rtx_constant_pool *pool)
4199 class constant_descriptor_rtx *desc;
4200 pool->offset = 0;
4202 for (desc = pool->first; desc ; desc = desc->next)
4203 if (desc->mark)
4205 /* Recalculate offset. */
4206 unsigned int align = desc->align;
4207 pool->offset += (align / BITS_PER_UNIT) - 1;
4208 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4209 desc->offset = pool->offset;
4210 pool->offset += GET_MODE_SIZE (desc->mode);
4214 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4215 Emit referenced deferred strings. */
4217 static void
4218 mark_constants_in_pattern (rtx insn)
4220 subrtx_iterator::array_type array;
4221 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4223 const_rtx x = *iter;
4224 if (GET_CODE (x) == SYMBOL_REF)
4226 if (CONSTANT_POOL_ADDRESS_P (x))
4228 class constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4229 if (desc->mark == 0)
4231 desc->mark = 1;
4232 iter.substitute (desc->constant);
4235 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4237 tree decl = SYMBOL_REF_DECL (x);
4238 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4240 n_deferred_constants--;
4241 output_constant_def_contents (CONST_CAST_RTX (x));
4248 /* Look through appropriate parts of INSN, marking all entries in the
4249 constant pool which are actually being used. Entries that are only
4250 referenced by other constants are also marked as used. Emit
4251 deferred strings that are used. */
4253 static void
4254 mark_constants (rtx_insn *insn)
4256 if (!INSN_P (insn))
4257 return;
4259 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4260 insns, not any notes that may be attached. We don't want to mark
4261 a constant just because it happens to appear in a REG_EQUIV note. */
4262 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4264 int i, n = seq->len ();
4265 for (i = 0; i < n; ++i)
4267 rtx subinsn = seq->element (i);
4268 if (INSN_P (subinsn))
4269 mark_constants_in_pattern (subinsn);
4272 else
4273 mark_constants_in_pattern (insn);
4276 /* Look through the instructions for this function, and mark all the
4277 entries in POOL which are actually being used. Emit deferred constants
4278 which have indeed been used. */
4280 static void
4281 mark_constant_pool (void)
4283 rtx_insn *insn;
4285 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4286 return;
4288 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4289 mark_constants (insn);
4292 /* Write all the constants in POOL. */
4294 static void
4295 output_constant_pool_contents (struct rtx_constant_pool *pool)
4297 class constant_descriptor_rtx *desc;
4299 for (desc = pool->first; desc ; desc = desc->next)
4300 if (desc->mark < 0)
4302 #ifdef ASM_OUTPUT_DEF
4303 const char *name = XSTR (desc->sym, 0);
4304 char label[256];
4305 char buffer[256 + 32];
4306 const char *p;
4308 ASM_GENERATE_INTERNAL_LABEL (label, "LC", ~desc->mark);
4309 p = label;
4310 if (desc->offset)
4312 sprintf (buffer, "%s+%ld", p, (long) (desc->offset));
4313 p = buffer;
4315 ASM_OUTPUT_DEF (asm_out_file, name, p);
4316 #else
4317 gcc_unreachable ();
4318 #endif
4320 else if (desc->mark)
4322 /* If the constant is part of an object_block, make sure that
4323 the constant has been positioned within its block, but do not
4324 write out its definition yet. output_object_blocks will do
4325 that later. */
4326 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4327 && SYMBOL_REF_BLOCK (desc->sym))
4328 place_block_symbol (desc->sym);
4329 else
4331 switch_to_section (targetm.asm_out.select_rtx_section
4332 (desc->mode, desc->constant, desc->align));
4333 output_constant_pool_1 (desc, desc->align);
4338 struct constant_descriptor_rtx_data {
4339 constant_descriptor_rtx *desc;
4340 target_unit *bytes;
4341 unsigned short size;
4342 unsigned short offset;
4343 unsigned int hash;
4346 /* qsort callback to sort constant_descriptor_rtx_data * vector by
4347 decreasing size. */
4349 static int
4350 constant_descriptor_rtx_data_cmp (const void *p1, const void *p2)
4352 constant_descriptor_rtx_data *const data1
4353 = *(constant_descriptor_rtx_data * const *) p1;
4354 constant_descriptor_rtx_data *const data2
4355 = *(constant_descriptor_rtx_data * const *) p2;
4356 if (data1->size > data2->size)
4357 return -1;
4358 if (data1->size < data2->size)
4359 return 1;
4360 if (data1->hash < data2->hash)
4361 return -1;
4362 gcc_assert (data1->hash > data2->hash);
4363 return 1;
4366 struct const_rtx_data_hasher : nofree_ptr_hash<constant_descriptor_rtx_data>
4368 static hashval_t hash (constant_descriptor_rtx_data *);
4369 static bool equal (constant_descriptor_rtx_data *,
4370 constant_descriptor_rtx_data *);
4373 /* Hash and compare functions for const_rtx_data_htab. */
4375 hashval_t
4376 const_rtx_data_hasher::hash (constant_descriptor_rtx_data *data)
4378 return data->hash;
4381 bool
4382 const_rtx_data_hasher::equal (constant_descriptor_rtx_data *x,
4383 constant_descriptor_rtx_data *y)
4385 if (x->hash != y->hash || x->size != y->size)
4386 return 0;
4387 unsigned int align1 = x->desc->align;
4388 unsigned int align2 = y->desc->align;
4389 unsigned int offset1 = (x->offset * BITS_PER_UNIT) & (align1 - 1);
4390 unsigned int offset2 = (y->offset * BITS_PER_UNIT) & (align2 - 1);
4391 if (offset1)
4392 align1 = least_bit_hwi (offset1);
4393 if (offset2)
4394 align2 = least_bit_hwi (offset2);
4395 if (align2 > align1)
4396 return 0;
4397 if (memcmp (x->bytes, y->bytes, x->size * sizeof (target_unit)) != 0)
4398 return 0;
4399 return 1;
4402 /* Attempt to optimize constant pool POOL. If it contains both CONST_VECTOR
4403 constants and scalar constants with the values of CONST_VECTOR elements,
4404 try to alias the scalar constants with the CONST_VECTOR elements. */
4406 static void
4407 optimize_constant_pool (struct rtx_constant_pool *pool)
4409 auto_vec<target_unit, 128> buffer;
4410 auto_vec<constant_descriptor_rtx_data *, 128> vec;
4411 object_allocator<constant_descriptor_rtx_data>
4412 data_pool ("constant_descriptor_rtx_data_pool");
4413 int idx = 0;
4414 size_t size = 0;
4415 for (constant_descriptor_rtx *desc = pool->first; desc; desc = desc->next)
4416 if (desc->mark > 0
4417 && ! (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4418 && SYMBOL_REF_BLOCK (desc->sym)))
4420 buffer.truncate (0);
4421 buffer.reserve (GET_MODE_SIZE (desc->mode));
4422 if (native_encode_rtx (desc->mode, desc->constant, buffer, 0,
4423 GET_MODE_SIZE (desc->mode)))
4425 constant_descriptor_rtx_data *data = data_pool.allocate ();
4426 data->desc = desc;
4427 data->bytes = NULL;
4428 data->size = GET_MODE_SIZE (desc->mode);
4429 data->offset = 0;
4430 data->hash = idx++;
4431 size += data->size;
4432 vec.safe_push (data);
4435 if (idx)
4437 vec.qsort (constant_descriptor_rtx_data_cmp);
4438 unsigned min_size = vec.last ()->size;
4439 target_unit *bytes = XNEWVEC (target_unit, size);
4440 unsigned int i;
4441 constant_descriptor_rtx_data *data;
4442 hash_table<const_rtx_data_hasher> * htab
4443 = new hash_table<const_rtx_data_hasher> (31);
4444 size = 0;
4445 FOR_EACH_VEC_ELT (vec, i, data)
4447 buffer.truncate (0);
4448 native_encode_rtx (data->desc->mode, data->desc->constant,
4449 buffer, 0, data->size);
4450 memcpy (bytes + size, buffer.address (), data->size);
4451 data->bytes = bytes + size;
4452 data->hash = iterative_hash (data->bytes,
4453 data->size * sizeof (target_unit), 0);
4454 size += data->size;
4455 constant_descriptor_rtx_data **slot
4456 = htab->find_slot_with_hash (data, data->hash, INSERT);
4457 if (*slot)
4459 data->desc->mark = ~(*slot)->desc->labelno;
4460 data->desc->offset = (*slot)->offset;
4462 else
4464 unsigned int sz = 1 << floor_log2 (data->size);
4466 *slot = data;
4467 for (sz >>= 1; sz >= min_size; sz >>= 1)
4468 for (unsigned off = 0; off + sz <= data->size; off += sz)
4470 constant_descriptor_rtx_data tmp;
4471 tmp.desc = data->desc;
4472 tmp.bytes = data->bytes + off;
4473 tmp.size = sz;
4474 tmp.offset = off;
4475 tmp.hash = iterative_hash (tmp.bytes,
4476 sz * sizeof (target_unit), 0);
4477 slot = htab->find_slot_with_hash (&tmp, tmp.hash, INSERT);
4478 if (*slot == NULL)
4480 *slot = data_pool.allocate ();
4481 **slot = tmp;
4486 delete htab;
4487 XDELETE (bytes);
4489 data_pool.release ();
4492 /* Mark all constants that are used in the current function, then write
4493 out the function's private constant pool. */
4495 static void
4496 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4497 tree fndecl ATTRIBUTE_UNUSED)
4499 struct rtx_constant_pool *pool = crtl->varasm.pool;
4501 /* It is possible for gcc to call force_const_mem and then to later
4502 discard the instructions which refer to the constant. In such a
4503 case we do not need to output the constant. */
4504 mark_constant_pool ();
4506 /* Having marked the constant pool entries we'll actually emit, we
4507 now need to rebuild the offset information, which may have become
4508 stale. */
4509 recompute_pool_offsets (pool);
4511 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4512 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4513 #endif
4515 output_constant_pool_contents (pool);
4517 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4518 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4519 #endif
4522 /* Write the contents of the shared constant pool. */
4524 void
4525 output_shared_constant_pool (void)
4527 if (optimize
4528 && TARGET_SUPPORTS_ALIASES)
4529 optimize_constant_pool (shared_constant_pool);
4531 output_constant_pool_contents (shared_constant_pool);
4534 /* Determine what kind of relocations EXP may need. */
4537 compute_reloc_for_constant (tree exp)
4539 int reloc = 0, reloc2;
4540 tree tem;
4542 switch (TREE_CODE (exp))
4544 case ADDR_EXPR:
4545 case FDESC_EXPR:
4546 /* Go inside any operations that get_inner_reference can handle and see
4547 if what's inside is a constant: no need to do anything here for
4548 addresses of variables or functions. */
4549 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4550 tem = TREE_OPERAND (tem, 0))
4553 if (TREE_CODE (tem) == MEM_REF
4554 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4556 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4557 break;
4560 if (!targetm.binds_local_p (tem))
4561 reloc |= 2;
4562 else
4563 reloc |= 1;
4564 break;
4566 case PLUS_EXPR:
4567 case POINTER_PLUS_EXPR:
4568 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4569 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4570 break;
4572 case MINUS_EXPR:
4573 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4574 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4575 /* The difference of two local labels is computable at link time. */
4576 if (reloc == 1 && reloc2 == 1)
4577 reloc = 0;
4578 else
4579 reloc |= reloc2;
4580 break;
4582 CASE_CONVERT:
4583 case VIEW_CONVERT_EXPR:
4584 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4585 break;
4587 case CONSTRUCTOR:
4589 unsigned HOST_WIDE_INT idx;
4590 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4591 if (tem != 0)
4592 reloc |= compute_reloc_for_constant (tem);
4594 break;
4596 default:
4597 break;
4599 return reloc;
4602 /* Find all the constants whose addresses are referenced inside of EXP,
4603 and make sure assembler code with a label has been output for each one.
4604 Indicate whether an ADDR_EXPR has been encountered. */
4606 static void
4607 output_addressed_constants (tree exp, int defer)
4609 tree tem;
4611 switch (TREE_CODE (exp))
4613 case ADDR_EXPR:
4614 case FDESC_EXPR:
4615 /* Go inside any operations that get_inner_reference can handle and see
4616 if what's inside is a constant: no need to do anything here for
4617 addresses of variables or functions. */
4618 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4619 tem = TREE_OPERAND (tem, 0))
4622 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4623 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4624 tem = DECL_INITIAL (tem);
4626 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4627 output_constant_def (tem, defer);
4629 if (TREE_CODE (tem) == MEM_REF)
4630 output_addressed_constants (TREE_OPERAND (tem, 0), defer);
4631 break;
4633 case PLUS_EXPR:
4634 case POINTER_PLUS_EXPR:
4635 case MINUS_EXPR:
4636 output_addressed_constants (TREE_OPERAND (exp, 1), defer);
4637 gcc_fallthrough ();
4639 CASE_CONVERT:
4640 case VIEW_CONVERT_EXPR:
4641 output_addressed_constants (TREE_OPERAND (exp, 0), defer);
4642 break;
4644 case CONSTRUCTOR:
4646 unsigned HOST_WIDE_INT idx;
4647 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4648 if (tem != 0)
4649 output_addressed_constants (tem, defer);
4651 break;
4653 default:
4654 break;
4658 /* Whether a constructor CTOR is a valid static constant initializer if all
4659 its elements are. This used to be internal to initializer_constant_valid_p
4660 and has been exposed to let other functions like categorize_ctor_elements
4661 evaluate the property while walking a constructor for other purposes. */
4663 bool
4664 constructor_static_from_elts_p (const_tree ctor)
4666 return (TREE_CONSTANT (ctor)
4667 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4668 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4669 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4672 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4673 tree *cache);
4675 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4676 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4677 which are valid when ENDTYPE is an integer of any size; in
4678 particular, this does not accept a pointer minus a constant. This
4679 returns null_pointer_node if the VALUE is an absolute constant
4680 which can be used to initialize a static variable. Otherwise it
4681 returns NULL. */
4683 static tree
4684 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4686 tree op0, op1;
4688 if (!INTEGRAL_TYPE_P (endtype))
4689 return NULL_TREE;
4691 op0 = TREE_OPERAND (value, 0);
4692 op1 = TREE_OPERAND (value, 1);
4694 /* Like STRIP_NOPS except allow the operand mode to widen. This
4695 works around a feature of fold that simplifies (int)(p1 - p2) to
4696 ((int)p1 - (int)p2) under the theory that the narrower operation
4697 is cheaper. */
4699 while (CONVERT_EXPR_P (op0)
4700 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4702 tree inner = TREE_OPERAND (op0, 0);
4703 if (inner == error_mark_node
4704 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4705 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4706 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4707 break;
4708 op0 = inner;
4711 while (CONVERT_EXPR_P (op1)
4712 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4714 tree inner = TREE_OPERAND (op1, 0);
4715 if (inner == error_mark_node
4716 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4717 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4718 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4719 break;
4720 op1 = inner;
4723 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4724 if (!op0)
4725 return NULL_TREE;
4727 op1 = initializer_constant_valid_p_1 (op1, endtype,
4728 cache ? cache + 2 : NULL);
4729 /* Both initializers must be known. */
4730 if (op1)
4732 if (op0 == op1
4733 && (op0 == null_pointer_node
4734 || TREE_CODE (value) == MINUS_EXPR))
4735 return null_pointer_node;
4737 /* Support differences between labels. */
4738 if (TREE_CODE (op0) == LABEL_DECL
4739 && TREE_CODE (op1) == LABEL_DECL)
4740 return null_pointer_node;
4742 if (TREE_CODE (op0) == STRING_CST
4743 && TREE_CODE (op1) == STRING_CST
4744 && operand_equal_p (op0, op1, 1))
4745 return null_pointer_node;
4748 return NULL_TREE;
4751 /* Helper function of initializer_constant_valid_p.
4752 Return nonzero if VALUE is a valid constant-valued expression
4753 for use in initializing a static variable; one that can be an
4754 element of a "constant" initializer.
4756 Return null_pointer_node if the value is absolute;
4757 if it is relocatable, return the variable that determines the relocation.
4758 We assume that VALUE has been folded as much as possible;
4759 therefore, we do not need to check for such things as
4760 arithmetic-combinations of integers.
4762 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4764 static tree
4765 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4767 tree ret;
4769 switch (TREE_CODE (value))
4771 case CONSTRUCTOR:
4772 if (constructor_static_from_elts_p (value))
4774 unsigned HOST_WIDE_INT idx;
4775 tree elt;
4776 bool absolute = true;
4778 if (cache && cache[0] == value)
4779 return cache[1];
4780 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4782 tree reloc;
4783 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4784 NULL);
4785 if (!reloc
4786 /* An absolute value is required with reverse SSO. */
4787 || (reloc != null_pointer_node
4788 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4789 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4791 if (cache)
4793 cache[0] = value;
4794 cache[1] = NULL_TREE;
4796 return NULL_TREE;
4798 if (reloc != null_pointer_node)
4799 absolute = false;
4801 /* For a non-absolute relocation, there is no single
4802 variable that can be "the variable that determines the
4803 relocation." */
4804 if (cache)
4806 cache[0] = value;
4807 cache[1] = absolute ? null_pointer_node : error_mark_node;
4809 return absolute ? null_pointer_node : error_mark_node;
4812 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4814 case INTEGER_CST:
4815 case VECTOR_CST:
4816 case REAL_CST:
4817 case FIXED_CST:
4818 case STRING_CST:
4819 case COMPLEX_CST:
4820 return null_pointer_node;
4822 case ADDR_EXPR:
4823 case FDESC_EXPR:
4825 tree op0 = staticp (TREE_OPERAND (value, 0));
4826 if (op0)
4828 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4829 to be a constant, this is old-skool offsetof-like nonsense. */
4830 if (TREE_CODE (op0) == INDIRECT_REF
4831 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4832 return null_pointer_node;
4833 /* Taking the address of a nested function involves a trampoline,
4834 unless we don't need or want one. */
4835 if (TREE_CODE (op0) == FUNCTION_DECL
4836 && DECL_STATIC_CHAIN (op0)
4837 && !TREE_NO_TRAMPOLINE (value))
4838 return NULL_TREE;
4839 /* "&{...}" requires a temporary to hold the constructed
4840 object. */
4841 if (TREE_CODE (op0) == CONSTRUCTOR)
4842 return NULL_TREE;
4844 return op0;
4847 case NON_LVALUE_EXPR:
4848 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4849 endtype, cache);
4851 case VIEW_CONVERT_EXPR:
4853 tree src = TREE_OPERAND (value, 0);
4854 tree src_type = TREE_TYPE (src);
4855 tree dest_type = TREE_TYPE (value);
4857 /* Allow view-conversions from aggregate to non-aggregate type only
4858 if the bit pattern is fully preserved afterwards; otherwise, the
4859 RTL expander won't be able to apply a subsequent transformation
4860 to the underlying constructor. */
4861 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4863 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4864 return initializer_constant_valid_p_1 (src, endtype, cache);
4865 else
4866 return NULL_TREE;
4869 /* Allow all other kinds of view-conversion. */
4870 return initializer_constant_valid_p_1 (src, endtype, cache);
4873 CASE_CONVERT:
4875 tree src = TREE_OPERAND (value, 0);
4876 tree src_type = TREE_TYPE (src);
4877 tree dest_type = TREE_TYPE (value);
4879 /* Allow conversions between pointer types, floating-point
4880 types, and offset types. */
4881 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4882 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4883 || (TREE_CODE (dest_type) == OFFSET_TYPE
4884 && TREE_CODE (src_type) == OFFSET_TYPE))
4885 return initializer_constant_valid_p_1 (src, endtype, cache);
4887 /* Allow length-preserving conversions between integer types. */
4888 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4889 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4890 return initializer_constant_valid_p_1 (src, endtype, cache);
4892 /* Allow conversions between other integer types only if
4893 explicit value. Don't allow sign-extension to a type larger
4894 than word and pointer, there aren't relocations that would
4895 allow to sign extend it to a wider type. */
4896 if (INTEGRAL_TYPE_P (dest_type)
4897 && INTEGRAL_TYPE_P (src_type)
4898 && (TYPE_UNSIGNED (src_type)
4899 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4900 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4901 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4903 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4904 if (inner == null_pointer_node)
4905 return null_pointer_node;
4906 break;
4909 /* Allow (int) &foo provided int is as wide as a pointer. */
4910 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4911 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4912 return initializer_constant_valid_p_1 (src, endtype, cache);
4914 /* Likewise conversions from int to pointers, but also allow
4915 conversions from 0. */
4916 if ((POINTER_TYPE_P (dest_type)
4917 || TREE_CODE (dest_type) == OFFSET_TYPE)
4918 && INTEGRAL_TYPE_P (src_type))
4920 if (TREE_CODE (src) == INTEGER_CST
4921 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4922 return null_pointer_node;
4923 if (integer_zerop (src))
4924 return null_pointer_node;
4925 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4926 return initializer_constant_valid_p_1 (src, endtype, cache);
4929 /* Allow conversions to struct or union types if the value
4930 inside is okay. */
4931 if (TREE_CODE (dest_type) == RECORD_TYPE
4932 || TREE_CODE (dest_type) == UNION_TYPE)
4933 return initializer_constant_valid_p_1 (src, endtype, cache);
4935 break;
4937 case POINTER_PLUS_EXPR:
4938 case PLUS_EXPR:
4939 /* Any valid floating-point constants will have been folded by now;
4940 with -frounding-math we hit this with addition of two constants. */
4941 if (TREE_CODE (endtype) == REAL_TYPE)
4942 return NULL_TREE;
4943 if (cache && cache[0] == value)
4944 return cache[1];
4945 if (! INTEGRAL_TYPE_P (endtype)
4946 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4948 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4949 tree valid0
4950 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4951 endtype, ncache);
4952 tree valid1
4953 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4954 endtype, ncache + 2);
4955 /* If either term is absolute, use the other term's relocation. */
4956 if (valid0 == null_pointer_node)
4957 ret = valid1;
4958 else if (valid1 == null_pointer_node)
4959 ret = valid0;
4960 /* Support narrowing pointer differences. */
4961 else
4962 ret = narrowing_initializer_constant_valid_p (value, endtype,
4963 ncache);
4965 else
4966 /* Support narrowing pointer differences. */
4967 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4968 if (cache)
4970 cache[0] = value;
4971 cache[1] = ret;
4973 return ret;
4975 case POINTER_DIFF_EXPR:
4976 case MINUS_EXPR:
4977 if (TREE_CODE (endtype) == REAL_TYPE)
4978 return NULL_TREE;
4979 if (cache && cache[0] == value)
4980 return cache[1];
4981 if (! INTEGRAL_TYPE_P (endtype)
4982 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4984 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4985 tree valid0
4986 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4987 endtype, ncache);
4988 tree valid1
4989 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4990 endtype, ncache + 2);
4991 /* Win if second argument is absolute. */
4992 if (valid1 == null_pointer_node)
4993 ret = valid0;
4994 /* Win if both arguments have the same relocation.
4995 Then the value is absolute. */
4996 else if (valid0 == valid1 && valid0 != 0)
4997 ret = null_pointer_node;
4998 /* Since GCC guarantees that string constants are unique in the
4999 generated code, a subtraction between two copies of the same
5000 constant string is absolute. */
5001 else if (valid0 && TREE_CODE (valid0) == STRING_CST
5002 && valid1 && TREE_CODE (valid1) == STRING_CST
5003 && operand_equal_p (valid0, valid1, 1))
5004 ret = null_pointer_node;
5005 /* Support narrowing differences. */
5006 else
5007 ret = narrowing_initializer_constant_valid_p (value, endtype,
5008 ncache);
5010 else
5011 /* Support narrowing differences. */
5012 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
5013 if (cache)
5015 cache[0] = value;
5016 cache[1] = ret;
5018 return ret;
5020 default:
5021 break;
5024 return NULL_TREE;
5027 /* Return nonzero if VALUE is a valid constant-valued expression
5028 for use in initializing a static variable; one that can be an
5029 element of a "constant" initializer.
5031 Return null_pointer_node if the value is absolute;
5032 if it is relocatable, return the variable that determines the relocation.
5033 We assume that VALUE has been folded as much as possible;
5034 therefore, we do not need to check for such things as
5035 arithmetic-combinations of integers. */
5036 tree
5037 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
5039 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
5041 /* An absolute value is required with reverse storage order. */
5042 if (reloc
5043 && reloc != null_pointer_node
5044 && reverse
5045 && !AGGREGATE_TYPE_P (endtype)
5046 && !VECTOR_TYPE_P (endtype))
5047 reloc = NULL_TREE;
5049 return reloc;
5052 /* Return true if VALUE is a valid constant-valued expression
5053 for use in initializing a static bit-field; one that can be
5054 an element of a "constant" initializer. */
5056 bool
5057 initializer_constant_valid_for_bitfield_p (tree value)
5059 /* For bitfields we support integer constants or possibly nested aggregates
5060 of such. */
5061 switch (TREE_CODE (value))
5063 case CONSTRUCTOR:
5065 unsigned HOST_WIDE_INT idx;
5066 tree elt;
5068 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
5069 if (!initializer_constant_valid_for_bitfield_p (elt))
5070 return false;
5071 return true;
5074 case INTEGER_CST:
5075 case REAL_CST:
5076 return true;
5078 case VIEW_CONVERT_EXPR:
5079 case NON_LVALUE_EXPR:
5080 return
5081 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
5083 default:
5084 break;
5087 return false;
5090 /* Check if a STRING_CST fits into the field.
5091 Tolerate only the case when the NUL termination
5092 does not fit into the field. */
5094 static bool
5095 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
5097 tree type = TREE_TYPE (string);
5098 tree eltype = TREE_TYPE (type);
5099 unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
5100 unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5101 int len = TREE_STRING_LENGTH (string);
5103 if (elts != 1 && elts != 2 && elts != 4)
5104 return false;
5105 if (len < 0 || len % elts != 0)
5106 return false;
5107 if (size < (unsigned)len)
5108 return false;
5109 if (mem_size != size)
5110 return false;
5111 return true;
5114 /* output_constructor outer state of relevance in recursive calls, typically
5115 for nested aggregate bitfields. */
5117 struct oc_outer_state {
5118 unsigned int bit_offset; /* current position in ... */
5119 int byte; /* ... the outer byte buffer. */
5122 static unsigned HOST_WIDE_INT
5123 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
5124 oc_outer_state *);
5126 /* Output assembler code for constant EXP, with no label.
5127 This includes the pseudo-op such as ".int" or ".byte", and a newline.
5128 Assumes output_addressed_constants has been done on EXP already.
5130 Generate at least SIZE bytes of assembler data, padding at the end
5131 with zeros if necessary. SIZE must always be specified. The returned
5132 value is the actual number of bytes of assembler data generated, which
5133 may be bigger than SIZE if the object contains a variable length field.
5135 SIZE is important for structure constructors,
5136 since trailing members may have been omitted from the constructor.
5137 It is also important for initialization of arrays from string constants
5138 since the full length of the string constant might not be wanted.
5139 It is also needed for initialization of unions, where the initializer's
5140 type is just one member, and that may not be as long as the union.
5142 There a case in which we would fail to output exactly SIZE bytes:
5143 for a structure constructor that wants to produce more than SIZE bytes.
5144 But such constructors will never be generated for any possible input.
5146 ALIGN is the alignment of the data in bits.
5148 If REVERSE is true, EXP is output in reverse storage order. */
5150 static unsigned HOST_WIDE_INT
5151 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5152 bool reverse, bool merge_strings)
5154 enum tree_code code;
5155 unsigned HOST_WIDE_INT thissize;
5156 rtx cst;
5158 if (size == 0 || flag_syntax_only)
5159 return size;
5161 /* See if we're trying to initialize a pointer in a non-default mode
5162 to the address of some declaration somewhere. If the target says
5163 the mode is valid for pointers, assume the target has a way of
5164 resolving it. */
5165 if (TREE_CODE (exp) == NOP_EXPR
5166 && POINTER_TYPE_P (TREE_TYPE (exp))
5167 && targetm.addr_space.valid_pointer_mode
5168 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5169 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5171 tree saved_type = TREE_TYPE (exp);
5173 /* Peel off any intermediate conversions-to-pointer for valid
5174 pointer modes. */
5175 while (TREE_CODE (exp) == NOP_EXPR
5176 && POINTER_TYPE_P (TREE_TYPE (exp))
5177 && targetm.addr_space.valid_pointer_mode
5178 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5179 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5180 exp = TREE_OPERAND (exp, 0);
5182 /* If what we're left with is the address of something, we can
5183 convert the address to the final type and output it that
5184 way. */
5185 if (TREE_CODE (exp) == ADDR_EXPR)
5186 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
5187 /* Likewise for constant ints. */
5188 else if (TREE_CODE (exp) == INTEGER_CST)
5189 exp = fold_convert (saved_type, exp);
5193 /* Eliminate any conversions since we'll be outputting the underlying
5194 constant. */
5195 while (CONVERT_EXPR_P (exp)
5196 || TREE_CODE (exp) == NON_LVALUE_EXPR
5197 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
5199 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
5200 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
5202 /* Make sure eliminating the conversion is really a no-op, except with
5203 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
5204 union types to allow for Ada unchecked unions. */
5205 if (type_size > op_size
5206 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
5207 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
5208 /* Keep the conversion. */
5209 break;
5210 else
5211 exp = TREE_OPERAND (exp, 0);
5214 code = TREE_CODE (TREE_TYPE (exp));
5215 thissize = int_size_in_bytes (TREE_TYPE (exp));
5217 /* Allow a constructor with no elements for any data type.
5218 This means to fill the space with zeros. */
5219 if (TREE_CODE (exp) == CONSTRUCTOR
5220 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
5222 assemble_zeros (size);
5223 return size;
5226 if (TREE_CODE (exp) == FDESC_EXPR)
5228 #ifdef ASM_OUTPUT_FDESC
5229 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
5230 tree decl = TREE_OPERAND (exp, 0);
5231 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
5232 #else
5233 gcc_unreachable ();
5234 #endif
5235 return size;
5238 /* Now output the underlying data. If we've handling the padding, return.
5239 Otherwise, break and ensure SIZE is the size written. */
5240 switch (code)
5242 case BOOLEAN_TYPE:
5243 case INTEGER_TYPE:
5244 case ENUMERAL_TYPE:
5245 case POINTER_TYPE:
5246 case REFERENCE_TYPE:
5247 case OFFSET_TYPE:
5248 case FIXED_POINT_TYPE:
5249 case NULLPTR_TYPE:
5250 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5251 if (reverse)
5252 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5253 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5254 error ("initializer for integer/fixed-point value is too complicated");
5255 break;
5257 case REAL_TYPE:
5258 if (TREE_CODE (exp) != REAL_CST)
5259 error ("initializer for floating value is not a floating constant");
5260 else
5261 assemble_real (TREE_REAL_CST (exp),
5262 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
5263 align, reverse);
5264 break;
5266 case COMPLEX_TYPE:
5267 output_constant (TREE_REALPART (exp), thissize / 2, align,
5268 reverse, false);
5269 output_constant (TREE_IMAGPART (exp), thissize / 2,
5270 min_align (align, BITS_PER_UNIT * (thissize / 2)),
5271 reverse, false);
5272 break;
5274 case ARRAY_TYPE:
5275 case VECTOR_TYPE:
5276 switch (TREE_CODE (exp))
5278 case CONSTRUCTOR:
5279 return output_constructor (exp, size, align, reverse, NULL);
5280 case STRING_CST:
5281 thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5282 if (merge_strings
5283 && (thissize == 0
5284 || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5285 thissize++;
5286 gcc_checking_assert (check_string_literal (exp, size));
5287 assemble_string (TREE_STRING_POINTER (exp), thissize);
5288 break;
5289 case VECTOR_CST:
5291 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5292 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5293 int elt_size = GET_MODE_SIZE (inner);
5294 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5295 reverse, false);
5296 thissize = elt_size;
5297 /* Static constants must have a fixed size. */
5298 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5299 for (unsigned int i = 1; i < nunits; i++)
5301 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5302 reverse, false);
5303 thissize += elt_size;
5305 break;
5307 default:
5308 gcc_unreachable ();
5310 break;
5312 case RECORD_TYPE:
5313 case UNION_TYPE:
5314 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5315 return output_constructor (exp, size, align, reverse, NULL);
5317 case ERROR_MARK:
5318 return 0;
5320 default:
5321 gcc_unreachable ();
5324 if (size > thissize)
5325 assemble_zeros (size - thissize);
5327 return size;
5330 /* Subroutine of output_constructor, used for computing the size of
5331 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
5332 type with an unspecified upper bound. */
5334 static unsigned HOST_WIDE_INT
5335 array_size_for_constructor (tree val)
5337 tree max_index;
5338 unsigned HOST_WIDE_INT cnt;
5339 tree index, value, tmp;
5340 offset_int i;
5342 /* This code used to attempt to handle string constants that are not
5343 arrays of single-bytes, but nothing else does, so there's no point in
5344 doing it here. */
5345 if (TREE_CODE (val) == STRING_CST)
5346 return TREE_STRING_LENGTH (val);
5348 max_index = NULL_TREE;
5349 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5351 if (TREE_CODE (index) == RANGE_EXPR)
5352 index = TREE_OPERAND (index, 1);
5353 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5354 max_index = index;
5357 if (max_index == NULL_TREE)
5358 return 0;
5360 /* Compute the total number of array elements. */
5361 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5362 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5364 /* Multiply by the array element unit size to find number of bytes. */
5365 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5367 gcc_assert (wi::fits_uhwi_p (i));
5368 return i.to_uhwi ();
5371 /* Other datastructures + helpers for output_constructor. */
5373 /* output_constructor local state to support interaction with helpers. */
5375 struct oc_local_state {
5377 /* Received arguments. */
5378 tree exp; /* Constructor expression. */
5379 tree type; /* Type of constructor expression. */
5380 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5381 unsigned int align; /* Known initial alignment. */
5382 tree min_index; /* Lower bound if specified for an array. */
5384 /* Output processing state. */
5385 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5386 int byte; /* Part of a bitfield byte yet to be output. */
5387 int last_relative_index; /* Implicit or explicit index of the last
5388 array element output within a bitfield. */
5389 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5390 bool reverse; /* Whether reverse storage order is in use. */
5392 /* Current element. */
5393 tree field; /* Current field decl in a record. */
5394 tree val; /* Current element value. */
5395 tree index; /* Current element index. */
5399 /* Helper for output_constructor. From the current LOCAL state, output a
5400 RANGE_EXPR element. */
5402 static void
5403 output_constructor_array_range (oc_local_state *local)
5405 /* Perform the index calculation in modulo arithmetic but
5406 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5407 but we are using an unsigned sizetype. */
5408 unsigned prec = TYPE_PRECISION (sizetype);
5409 offset_int idx = wi::sext (wi::to_offset (TREE_OPERAND (local->index, 0))
5410 - wi::to_offset (local->min_index), prec);
5411 tree valtype = TREE_TYPE (local->val);
5412 HOST_WIDE_INT fieldpos
5413 = (idx * wi::to_offset (TYPE_SIZE_UNIT (valtype))).to_short_addr ();
5415 /* Advance to offset of this element. */
5416 if (fieldpos > local->total_bytes)
5418 assemble_zeros (fieldpos - local->total_bytes);
5419 local->total_bytes = fieldpos;
5421 else
5422 /* Must not go backwards. */
5423 gcc_assert (fieldpos == local->total_bytes);
5425 unsigned HOST_WIDE_INT fieldsize
5426 = int_size_in_bytes (TREE_TYPE (local->type));
5428 HOST_WIDE_INT lo_index
5429 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5430 HOST_WIDE_INT hi_index
5431 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5432 HOST_WIDE_INT index;
5434 unsigned int align2
5435 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5437 for (index = lo_index; index <= hi_index; index++)
5439 /* Output the element's initial value. */
5440 if (local->val == NULL_TREE)
5441 assemble_zeros (fieldsize);
5442 else
5443 fieldsize = output_constant (local->val, fieldsize, align2,
5444 local->reverse, false);
5446 /* Count its size. */
5447 local->total_bytes += fieldsize;
5451 /* Helper for output_constructor. From the current LOCAL state, output a
5452 field element that is not true bitfield or part of an outer one. */
5454 static void
5455 output_constructor_regular_field (oc_local_state *local)
5457 /* Field size and position. Since this structure is static, we know the
5458 positions are constant. */
5459 unsigned HOST_WIDE_INT fieldsize;
5460 HOST_WIDE_INT fieldpos;
5462 unsigned int align2;
5464 /* Output any buffered-up bit-fields preceding this element. */
5465 if (local->byte_buffer_in_use)
5467 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5468 local->total_bytes++;
5469 local->byte_buffer_in_use = false;
5472 if (local->index != NULL_TREE)
5474 /* Perform the index calculation in modulo arithmetic but
5475 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5476 but we are using an unsigned sizetype. */
5477 unsigned prec = TYPE_PRECISION (sizetype);
5478 offset_int idx = wi::sext (wi::to_offset (local->index)
5479 - wi::to_offset (local->min_index), prec);
5480 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5481 .to_short_addr ();
5483 else if (local->field != NULL_TREE)
5484 fieldpos = int_byte_position (local->field);
5485 else
5486 fieldpos = 0;
5488 /* Advance to offset of this element.
5489 Note no alignment needed in an array, since that is guaranteed
5490 if each element has the proper size. */
5491 if (local->field != NULL_TREE || local->index != NULL_TREE)
5493 if (fieldpos > local->total_bytes)
5495 assemble_zeros (fieldpos - local->total_bytes);
5496 local->total_bytes = fieldpos;
5498 else
5499 /* Must not go backwards. */
5500 gcc_assert (fieldpos == local->total_bytes);
5503 /* Find the alignment of this element. */
5504 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5506 /* Determine size this element should occupy. */
5507 if (local->field)
5509 fieldsize = 0;
5511 /* If this is an array with an unspecified upper bound,
5512 the initializer determines the size. */
5513 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5514 but we cannot do this until the deprecated support for
5515 initializing zero-length array members is removed. */
5516 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5517 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5518 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5520 fieldsize = array_size_for_constructor (local->val);
5521 /* Given a non-empty initialization, this field had better
5522 be last. Given a flexible array member, the next field
5523 on the chain is a TYPE_DECL of the enclosing struct. */
5524 const_tree next = DECL_CHAIN (local->field);
5525 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5526 tree size = TYPE_SIZE_UNIT (TREE_TYPE (local->val));
5527 gcc_checking_assert (compare_tree_int (size, fieldsize) == 0);
5529 else
5530 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5532 else
5533 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5535 /* Output the element's initial value. */
5536 if (local->val == NULL_TREE)
5537 assemble_zeros (fieldsize);
5538 else
5539 fieldsize = output_constant (local->val, fieldsize, align2,
5540 local->reverse, false);
5542 /* Count its size. */
5543 local->total_bytes += fieldsize;
5546 /* Helper for output_constructor. From the LOCAL state, output an element
5547 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5548 from the start of a possibly ongoing outer byte buffer. */
5550 static void
5551 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5553 /* Bit size of this element. */
5554 HOST_WIDE_INT ebitsize
5555 = (local->field
5556 ? tree_to_uhwi (DECL_SIZE (local->field))
5557 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5559 /* Relative index of this element if this is an array component. */
5560 HOST_WIDE_INT relative_index
5561 = (!local->field
5562 ? (local->index
5563 ? (tree_to_shwi (local->index)
5564 - tree_to_shwi (local->min_index))
5565 : local->last_relative_index + 1)
5566 : 0);
5568 /* Bit position of this element from the start of the containing
5569 constructor. */
5570 HOST_WIDE_INT constructor_relative_ebitpos
5571 = (local->field
5572 ? int_bit_position (local->field)
5573 : ebitsize * relative_index);
5575 /* Bit position of this element from the start of a possibly ongoing
5576 outer byte buffer. */
5577 HOST_WIDE_INT byte_relative_ebitpos
5578 = bit_offset + constructor_relative_ebitpos;
5580 /* From the start of a possibly ongoing outer byte buffer, offsets to
5581 the first bit of this element and to the first bit past the end of
5582 this element. */
5583 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5584 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5586 local->last_relative_index = relative_index;
5588 if (local->val == NULL_TREE)
5589 local->val = integer_zero_node;
5591 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5592 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5593 local->val = TREE_OPERAND (local->val, 0);
5595 if (TREE_CODE (local->val) != INTEGER_CST
5596 && TREE_CODE (local->val) != CONSTRUCTOR)
5598 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5599 return;
5602 /* If this field does not start in this (or next) byte, skip some bytes. */
5603 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5605 /* Output remnant of any bit field in previous bytes. */
5606 if (local->byte_buffer_in_use)
5608 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5609 local->total_bytes++;
5610 local->byte_buffer_in_use = false;
5613 /* If still not at proper byte, advance to there. */
5614 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5616 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5617 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5618 local->total_bytes = next_offset / BITS_PER_UNIT;
5622 /* Set up the buffer if necessary. */
5623 if (!local->byte_buffer_in_use)
5625 local->byte = 0;
5626 if (ebitsize > 0)
5627 local->byte_buffer_in_use = true;
5630 /* If this is nested constructor, recurse passing the bit offset and the
5631 pending data, then retrieve the new pending data afterwards. */
5632 if (TREE_CODE (local->val) == CONSTRUCTOR)
5634 oc_outer_state temp_state;
5635 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5636 temp_state.byte = local->byte;
5637 local->total_bytes
5638 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5639 local->byte = temp_state.byte;
5640 return;
5643 /* Otherwise, we must split the element into pieces that fall within
5644 separate bytes, and combine each byte with previous or following
5645 bit-fields. */
5646 while (next_offset < end_offset)
5648 int this_time;
5649 int shift;
5650 unsigned HOST_WIDE_INT value;
5651 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5652 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5654 /* Advance from byte to byte within this element when necessary. */
5655 while (next_byte != local->total_bytes)
5657 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5658 local->total_bytes++;
5659 local->byte = 0;
5662 /* Number of bits we can process at once (all part of the same byte). */
5663 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5664 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5666 /* For big-endian data, take the most significant bits (of the
5667 bits that are significant) first and put them into bytes from
5668 the most significant end. */
5669 shift = end_offset - next_offset - this_time;
5671 /* Don't try to take a bunch of bits that cross
5672 the word boundary in the INTEGER_CST. We can
5673 only select bits from one element. */
5674 if ((shift / HOST_BITS_PER_WIDE_INT)
5675 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5677 const int end = shift + this_time - 1;
5678 shift = end & -HOST_BITS_PER_WIDE_INT;
5679 this_time = end - shift + 1;
5682 /* Now get the bits we want to insert. */
5683 value = wi::extract_uhwi (wi::to_widest (local->val),
5684 shift, this_time);
5686 /* Get the result. This works only when:
5687 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5688 local->byte |= value << (BITS_PER_UNIT - this_time - next_bit);
5690 else
5692 /* On little-endian machines, take the least significant bits of
5693 the value first and pack them starting at the least significant
5694 bits of the bytes. */
5695 shift = next_offset - byte_relative_ebitpos;
5697 /* Don't try to take a bunch of bits that cross
5698 the word boundary in the INTEGER_CST. We can
5699 only select bits from one element. */
5700 if ((shift / HOST_BITS_PER_WIDE_INT)
5701 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5702 this_time
5703 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5705 /* Now get the bits we want to insert. */
5706 value = wi::extract_uhwi (wi::to_widest (local->val),
5707 shift, this_time);
5709 /* Get the result. This works only when:
5710 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5711 local->byte |= value << next_bit;
5714 next_offset += this_time;
5715 local->byte_buffer_in_use = true;
5719 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5720 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5721 caller output state of relevance in recursive invocations. */
5723 static unsigned HOST_WIDE_INT
5724 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5725 bool reverse, oc_outer_state *outer)
5727 unsigned HOST_WIDE_INT cnt;
5728 constructor_elt *ce;
5729 oc_local_state local;
5731 /* Setup our local state to communicate with helpers. */
5732 local.exp = exp;
5733 local.type = TREE_TYPE (exp);
5734 local.size = size;
5735 local.align = align;
5736 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5737 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5738 else
5739 local.min_index = integer_zero_node;
5741 local.total_bytes = 0;
5742 local.byte_buffer_in_use = outer != NULL;
5743 local.byte = outer ? outer->byte : 0;
5744 local.last_relative_index = -1;
5745 /* The storage order is specified for every aggregate type. */
5746 if (AGGREGATE_TYPE_P (local.type))
5747 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5748 else
5749 local.reverse = reverse;
5751 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5753 /* As CE goes through the elements of the constant, FIELD goes through the
5754 structure fields if the constant is a structure. If the constant is a
5755 union, we override this by getting the field from the TREE_LIST element.
5756 But the constant could also be an array. Then FIELD is zero.
5758 There is always a maximum of one element in the chain LINK for unions
5759 (even if the initializer in a source program incorrectly contains
5760 more one). */
5762 if (TREE_CODE (local.type) == RECORD_TYPE)
5763 local.field = TYPE_FIELDS (local.type);
5764 else
5765 local.field = NULL_TREE;
5767 for (cnt = 0;
5768 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5769 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5771 local.val = ce->value;
5772 local.index = NULL_TREE;
5774 /* The element in a union constructor specifies the proper field
5775 or index. */
5776 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5777 local.field = ce->index;
5779 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5780 local.index = ce->index;
5782 if (local.field && flag_verbose_asm)
5783 fprintf (asm_out_file, "%s %s:\n",
5784 ASM_COMMENT_START,
5785 DECL_NAME (local.field)
5786 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5787 : "<anonymous>");
5789 /* Eliminate the marker that makes a cast not be an lvalue. */
5790 if (local.val != NULL_TREE)
5791 STRIP_NOPS (local.val);
5793 /* Output the current element, using the appropriate helper ... */
5795 /* For an array slice not part of an outer bitfield. */
5796 if (!outer
5797 && local.index != NULL_TREE
5798 && TREE_CODE (local.index) == RANGE_EXPR)
5799 output_constructor_array_range (&local);
5801 /* For a field that is neither a true bitfield nor part of an outer one,
5802 known to be at least byte aligned and multiple-of-bytes long. */
5803 else if (!outer
5804 && (local.field == NULL_TREE
5805 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5806 output_constructor_regular_field (&local);
5808 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5809 supported for scalar fields, so we may need to convert first. */
5810 else
5812 if (TREE_CODE (local.val) == REAL_CST)
5813 local.val
5814 = fold_unary (VIEW_CONVERT_EXPR,
5815 build_nonstandard_integer_type
5816 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5817 local.val);
5818 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5822 /* If we are not at toplevel, save the pending data for our caller.
5823 Otherwise output the pending data and padding zeros as needed. */
5824 if (outer)
5825 outer->byte = local.byte;
5826 else
5828 if (local.byte_buffer_in_use)
5830 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5831 local.total_bytes++;
5834 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5836 assemble_zeros (local.size - local.total_bytes);
5837 local.total_bytes = local.size;
5841 return local.total_bytes;
5844 /* Mark DECL as weak. */
5846 static void
5847 mark_weak (tree decl)
5849 if (DECL_WEAK (decl))
5850 return;
5852 struct symtab_node *n = symtab_node::get (decl);
5853 if (n && n->refuse_visibility_changes)
5854 error ("%+qD declared weak after being used", decl);
5855 DECL_WEAK (decl) = 1;
5857 if (DECL_RTL_SET_P (decl)
5858 && MEM_P (DECL_RTL (decl))
5859 && XEXP (DECL_RTL (decl), 0)
5860 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5861 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5864 /* Merge weak status between NEWDECL and OLDDECL. */
5866 void
5867 merge_weak (tree newdecl, tree olddecl)
5869 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5871 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5873 tree *pwd;
5874 /* We put the NEWDECL on the weak_decls list at some point
5875 and OLDDECL as well. Keep just OLDDECL on the list. */
5876 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5877 if (TREE_VALUE (*pwd) == newdecl)
5879 *pwd = TREE_CHAIN (*pwd);
5880 break;
5883 return;
5886 if (DECL_WEAK (newdecl))
5888 tree wd;
5890 /* NEWDECL is weak, but OLDDECL is not. */
5892 /* If we already output the OLDDECL, we're in trouble; we can't
5893 go back and make it weak. This should never happen in
5894 unit-at-a-time compilation. */
5895 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5897 /* If we've already generated rtl referencing OLDDECL, we may
5898 have done so in a way that will not function properly with
5899 a weak symbol. Again in unit-at-a-time this should be
5900 impossible. */
5901 gcc_assert (!TREE_USED (olddecl)
5902 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5904 /* PR 49899: You cannot convert a static function into a weak, public function. */
5905 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
5906 error ("weak declaration of %q+D being applied to a already "
5907 "existing, static definition", newdecl);
5909 if (TARGET_SUPPORTS_WEAK)
5911 /* We put the NEWDECL on the weak_decls list at some point.
5912 Replace it with the OLDDECL. */
5913 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5914 if (TREE_VALUE (wd) == newdecl)
5916 TREE_VALUE (wd) = olddecl;
5917 break;
5919 /* We may not find the entry on the list. If NEWDECL is a
5920 weak alias, then we will have already called
5921 globalize_decl to remove the entry; in that case, we do
5922 not need to do anything. */
5925 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5926 mark_weak (olddecl);
5928 else
5929 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5930 weak. Just update NEWDECL to indicate that it's weak too. */
5931 mark_weak (newdecl);
5934 /* Declare DECL to be a weak symbol. */
5936 void
5937 declare_weak (tree decl)
5939 /* With -fsyntax-only, TREE_ASM_WRITTEN might be set on certain function
5940 decls earlier than normally, but as with -fsyntax-only nothing is really
5941 emitted, there is no harm in marking it weak later. */
5942 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL
5943 || !TREE_ASM_WRITTEN (decl)
5944 || flag_syntax_only);
5945 if (! TREE_PUBLIC (decl))
5947 error ("weak declaration of %q+D must be public", decl);
5948 return;
5950 else if (!TARGET_SUPPORTS_WEAK)
5951 warning (0, "weak declaration of %q+D not supported", decl);
5953 mark_weak (decl);
5954 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5955 DECL_ATTRIBUTES (decl)
5956 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5959 static void
5960 weak_finish_1 (tree decl)
5962 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5963 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5964 #endif
5966 if (! TREE_USED (decl))
5967 return;
5969 #ifdef ASM_WEAKEN_DECL
5970 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5971 #else
5972 #ifdef ASM_WEAKEN_LABEL
5973 ASM_WEAKEN_LABEL (asm_out_file, name);
5974 #else
5975 #ifdef ASM_OUTPUT_WEAK_ALIAS
5977 static bool warn_once = 0;
5978 if (! warn_once)
5980 warning (0, "only weak aliases are supported in this configuration");
5981 warn_once = 1;
5983 return;
5985 #endif
5986 #endif
5987 #endif
5990 /* Fiven an assembly name, find the decl it is associated with. */
5991 static tree
5992 find_decl (tree target)
5994 symtab_node *node = symtab_node::get_for_asmname (target);
5995 if (node)
5996 return node->decl;
5997 return NULL_TREE;
6000 /* This TREE_LIST contains weakref targets. */
6002 static GTY(()) tree weakref_targets;
6004 /* Emit any pending weak declarations. */
6006 void
6007 weak_finish (void)
6009 tree t;
6011 for (t = weakref_targets; t; t = TREE_CHAIN (t))
6013 tree alias_decl = TREE_PURPOSE (t);
6014 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
6016 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
6017 || TREE_SYMBOL_REFERENCED (target))
6018 /* Remove alias_decl from the weak list, but leave entries for
6019 the target alone. */
6020 target = NULL_TREE;
6021 #ifndef ASM_OUTPUT_WEAKREF
6022 else if (! TREE_SYMBOL_REFERENCED (target))
6024 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
6025 defined, otherwise we and weak_finish_1 would use
6026 different macros. */
6027 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
6028 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
6029 # else
6030 tree decl = find_decl (target);
6032 if (! decl)
6034 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
6035 TREE_CODE (alias_decl), target,
6036 TREE_TYPE (alias_decl));
6038 DECL_EXTERNAL (decl) = 1;
6039 TREE_PUBLIC (decl) = 1;
6040 DECL_ARTIFICIAL (decl) = 1;
6041 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
6042 TREE_USED (decl) = 1;
6045 weak_finish_1 (decl);
6046 # endif
6048 #endif
6051 tree *p;
6052 tree t2;
6054 /* Remove the alias and the target from the pending weak list
6055 so that we do not emit any .weak directives for the former,
6056 nor multiple .weak directives for the latter. */
6057 for (p = &weak_decls; (t2 = *p) ; )
6059 if (TREE_VALUE (t2) == alias_decl
6060 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
6061 *p = TREE_CHAIN (t2);
6062 else
6063 p = &TREE_CHAIN (t2);
6066 /* Remove other weakrefs to the same target, to speed things up. */
6067 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
6069 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
6070 *p = TREE_CHAIN (t2);
6071 else
6072 p = &TREE_CHAIN (t2);
6077 for (t = weak_decls; t; t = TREE_CHAIN (t))
6079 tree decl = TREE_VALUE (t);
6081 weak_finish_1 (decl);
6085 /* Emit the assembly bits to indicate that DECL is globally visible. */
6087 static void
6088 globalize_decl (tree decl)
6091 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
6092 if (DECL_WEAK (decl))
6094 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6095 tree *p, t;
6097 #ifdef ASM_WEAKEN_DECL
6098 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
6099 #else
6100 ASM_WEAKEN_LABEL (asm_out_file, name);
6101 #endif
6103 /* Remove this function from the pending weak list so that
6104 we do not emit multiple .weak directives for it. */
6105 for (p = &weak_decls; (t = *p) ; )
6107 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6108 *p = TREE_CHAIN (t);
6109 else
6110 p = &TREE_CHAIN (t);
6113 /* Remove weakrefs to the same target from the pending weakref
6114 list, for the same reason. */
6115 for (p = &weakref_targets; (t = *p) ; )
6117 if (DECL_ASSEMBLER_NAME (decl)
6118 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6119 *p = TREE_CHAIN (t);
6120 else
6121 p = &TREE_CHAIN (t);
6124 return;
6126 #endif
6128 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
6131 vec<alias_pair, va_gc> *alias_pairs;
6133 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
6134 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
6135 tree node is DECL to have the value of the tree node TARGET. */
6137 void
6138 do_assemble_alias (tree decl, tree target)
6140 tree id;
6142 /* Emulated TLS had better not get this var. */
6143 gcc_assert (!(!targetm.have_tls
6144 && VAR_P (decl)
6145 && DECL_THREAD_LOCAL_P (decl)));
6147 if (TREE_ASM_WRITTEN (decl))
6148 return;
6150 id = DECL_ASSEMBLER_NAME (decl);
6151 ultimate_transparent_alias_target (&id);
6152 ultimate_transparent_alias_target (&target);
6154 /* We must force creation of DECL_RTL for debug info generation, even though
6155 we don't use it here. */
6156 make_decl_rtl (decl);
6158 TREE_ASM_WRITTEN (decl) = 1;
6159 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
6160 TREE_ASM_WRITTEN (id) = 1;
6162 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6164 if (!TREE_SYMBOL_REFERENCED (target))
6165 weakref_targets = tree_cons (decl, target, weakref_targets);
6167 #ifdef ASM_OUTPUT_WEAKREF
6168 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
6169 IDENTIFIER_POINTER (id),
6170 IDENTIFIER_POINTER (target));
6171 #else
6172 if (!TARGET_SUPPORTS_WEAK)
6174 error_at (DECL_SOURCE_LOCATION (decl),
6175 "weakref is not supported in this configuration");
6176 return;
6178 #endif
6179 return;
6182 #ifdef ASM_OUTPUT_DEF
6183 tree orig_decl = decl;
6185 /* Make name accessible from other files, if appropriate. */
6187 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
6189 globalize_decl (decl);
6190 maybe_assemble_visibility (decl);
6192 if (TREE_CODE (decl) == FUNCTION_DECL
6193 && cgraph_node::get (decl)->ifunc_resolver)
6195 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
6196 if (targetm.has_ifunc_p ())
6197 ASM_OUTPUT_TYPE_DIRECTIVE
6198 (asm_out_file, IDENTIFIER_POINTER (id),
6199 IFUNC_ASM_TYPE);
6200 else
6201 #endif
6202 error_at (DECL_SOURCE_LOCATION (decl),
6203 "%qs is not supported on this target", "ifunc");
6206 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
6207 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
6208 # else
6209 ASM_OUTPUT_DEF (asm_out_file,
6210 IDENTIFIER_POINTER (id),
6211 IDENTIFIER_POINTER (target));
6212 # endif
6213 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
6215 const char *name;
6216 tree *p, t;
6218 name = IDENTIFIER_POINTER (id);
6219 # ifdef ASM_WEAKEN_DECL
6220 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
6221 # else
6222 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
6223 # endif
6224 /* Remove this function from the pending weak list so that
6225 we do not emit multiple .weak directives for it. */
6226 for (p = &weak_decls; (t = *p) ; )
6227 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
6228 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6229 *p = TREE_CHAIN (t);
6230 else
6231 p = &TREE_CHAIN (t);
6233 /* Remove weakrefs to the same target from the pending weakref
6234 list, for the same reason. */
6235 for (p = &weakref_targets; (t = *p) ; )
6237 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6238 *p = TREE_CHAIN (t);
6239 else
6240 p = &TREE_CHAIN (t);
6243 #endif
6246 /* Output .symver directive. */
6248 void
6249 do_assemble_symver (tree decl, tree target)
6251 tree id = DECL_ASSEMBLER_NAME (decl);
6252 ultimate_transparent_alias_target (&id);
6253 ultimate_transparent_alias_target (&target);
6254 #ifdef ASM_OUTPUT_SYMVER_DIRECTIVE
6255 ASM_OUTPUT_SYMVER_DIRECTIVE (asm_out_file,
6256 IDENTIFIER_POINTER (target),
6257 IDENTIFIER_POINTER (id));
6258 #else
6259 error ("symver is only supported on ELF platforms");
6260 #endif
6263 /* Emit an assembler directive to make the symbol for DECL an alias to
6264 the symbol for TARGET. */
6266 void
6267 assemble_alias (tree decl, tree target)
6269 tree target_decl;
6271 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6273 tree alias = DECL_ASSEMBLER_NAME (decl);
6275 ultimate_transparent_alias_target (&target);
6277 if (alias == target)
6278 error ("%qs symbol %q+D ultimately targets itself", "weakref", decl);
6279 if (TREE_PUBLIC (decl))
6280 error ("%qs symbol %q+D must have static linkage", "weakref", decl);
6282 else
6284 #if !defined (ASM_OUTPUT_DEF)
6285 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
6286 error_at (DECL_SOURCE_LOCATION (decl),
6287 "alias definitions not supported in this configuration");
6288 TREE_ASM_WRITTEN (decl) = 1;
6289 return;
6290 # else
6291 if (!DECL_WEAK (decl))
6293 /* NB: ifunc_resolver isn't set when an error is detected. */
6294 if (TREE_CODE (decl) == FUNCTION_DECL
6295 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
6296 error_at (DECL_SOURCE_LOCATION (decl),
6297 "%qs is not supported in this configuration", "ifunc");
6298 else
6299 error_at (DECL_SOURCE_LOCATION (decl),
6300 "only weak aliases are supported in this configuration");
6301 TREE_ASM_WRITTEN (decl) = 1;
6302 return;
6304 # endif
6305 #endif
6307 TREE_USED (decl) = 1;
6309 /* Allow aliases to aliases. */
6310 if (TREE_CODE (decl) == FUNCTION_DECL)
6311 cgraph_node::get_create (decl)->alias = true;
6312 else
6313 varpool_node::get_create (decl)->alias = true;
6315 /* If the target has already been emitted, we don't have to queue the
6316 alias. This saves a tad of memory. */
6317 if (symtab->global_info_ready)
6318 target_decl = find_decl (target);
6319 else
6320 target_decl= NULL;
6321 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6322 || symtab->state >= EXPANSION)
6323 do_assemble_alias (decl, target);
6324 else
6326 alias_pair p = {decl, target};
6327 vec_safe_push (alias_pairs, p);
6331 /* Record and output a table of translations from original function
6332 to its transaction aware clone. Note that tm_pure functions are
6333 considered to be their own clone. */
6335 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6337 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6338 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6340 static int
6341 keep_cache_entry (tree_map *&e)
6343 return ggc_marked_p (e->base.from);
6347 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6349 void
6350 record_tm_clone_pair (tree o, tree n)
6352 struct tree_map **slot, *h;
6354 if (tm_clone_hash == NULL)
6355 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6357 h = ggc_alloc<tree_map> ();
6358 h->hash = htab_hash_pointer (o);
6359 h->base.from = o;
6360 h->to = n;
6362 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6363 *slot = h;
6366 tree
6367 get_tm_clone_pair (tree o)
6369 if (tm_clone_hash)
6371 struct tree_map *h, in;
6373 in.base.from = o;
6374 in.hash = htab_hash_pointer (o);
6375 h = tm_clone_hash->find_with_hash (&in, in.hash);
6376 if (h)
6377 return h->to;
6379 return NULL_TREE;
6382 struct tm_alias_pair
6384 unsigned int uid;
6385 tree from;
6386 tree to;
6390 /* Dump the actual pairs to the .tm_clone_table section. */
6392 static void
6393 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6395 unsigned i;
6396 tm_alias_pair *p;
6397 bool switched = false;
6399 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6401 tree src = p->from;
6402 tree dst = p->to;
6403 struct cgraph_node *src_n = cgraph_node::get (src);
6404 struct cgraph_node *dst_n = cgraph_node::get (dst);
6406 /* The function ipa_tm_create_version() marks the clone as needed if
6407 the original function was needed. But we also mark the clone as
6408 needed if we ever called the clone indirectly through
6409 TM_GETTMCLONE. If neither of these are true, we didn't generate
6410 a clone, and we didn't call it indirectly... no sense keeping it
6411 in the clone table. */
6412 if (!dst_n || !dst_n->definition)
6413 continue;
6415 /* This covers the case where we have optimized the original
6416 function away, and only access the transactional clone. */
6417 if (!src_n || !src_n->definition)
6418 continue;
6420 if (!switched)
6422 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6423 assemble_align (POINTER_SIZE);
6424 switched = true;
6427 assemble_integer (XEXP (DECL_RTL (src), 0),
6428 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6429 assemble_integer (XEXP (DECL_RTL (dst), 0),
6430 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6434 /* Provide a default for the tm_clone_table section. */
6436 section *
6437 default_clone_table_section (void)
6439 return get_named_section (NULL, ".tm_clone_table", 3);
6442 /* Helper comparison function for qsorting by the DECL_UID stored in
6443 alias_pair->emitted_diags. */
6445 static int
6446 tm_alias_pair_cmp (const void *x, const void *y)
6448 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6449 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6450 if (p1->uid < p2->uid)
6451 return -1;
6452 if (p1->uid > p2->uid)
6453 return 1;
6454 return 0;
6457 void
6458 finish_tm_clone_pairs (void)
6460 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6462 if (tm_clone_hash == NULL)
6463 return;
6465 /* We need a determenistic order for the .tm_clone_table, otherwise
6466 we will get bootstrap comparison failures, so dump the hash table
6467 to a vector, sort it, and dump the vector. */
6469 /* Dump the hashtable to a vector. */
6470 tree_map *map;
6471 hash_table<tm_clone_hasher>::iterator iter;
6472 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6474 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6475 tm_alias_pairs.safe_push (p);
6477 /* Sort it. */
6478 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6480 /* Dump it. */
6481 dump_tm_clone_pairs (tm_alias_pairs);
6483 tm_clone_hash->empty ();
6484 tm_clone_hash = NULL;
6485 tm_alias_pairs.release ();
6489 /* Emit an assembler directive to set symbol for DECL visibility to
6490 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6492 void
6493 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6494 int vis ATTRIBUTE_UNUSED)
6496 #ifdef HAVE_GAS_HIDDEN
6497 static const char * const visibility_types[] = {
6498 NULL, "protected", "hidden", "internal"
6501 const char *name, *type;
6502 tree id;
6504 id = DECL_ASSEMBLER_NAME (decl);
6505 ultimate_transparent_alias_target (&id);
6506 name = IDENTIFIER_POINTER (id);
6508 type = visibility_types[vis];
6510 fprintf (asm_out_file, "\t.%s\t", type);
6511 assemble_name (asm_out_file, name);
6512 fprintf (asm_out_file, "\n");
6513 #else
6514 if (!DECL_ARTIFICIAL (decl))
6515 warning (OPT_Wattributes, "visibility attribute not supported "
6516 "in this configuration; ignored");
6517 #endif
6520 /* A helper function to call assemble_visibility when needed for a decl. */
6523 maybe_assemble_visibility (tree decl)
6525 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6526 if (vis != VISIBILITY_DEFAULT)
6528 targetm.asm_out.assemble_visibility (decl, vis);
6529 return 1;
6531 else
6532 return 0;
6535 /* Returns 1 if the target configuration supports defining public symbols
6536 so that one of them will be chosen at link time instead of generating a
6537 multiply-defined symbol error, whether through the use of weak symbols or
6538 a target-specific mechanism for having duplicates discarded. */
6541 supports_one_only (void)
6543 if (SUPPORTS_ONE_ONLY)
6544 return 1;
6545 return TARGET_SUPPORTS_WEAK;
6548 /* Set up DECL as a public symbol that can be defined in multiple
6549 translation units without generating a linker error. */
6551 void
6552 make_decl_one_only (tree decl, tree comdat_group)
6554 struct symtab_node *symbol;
6555 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6557 TREE_PUBLIC (decl) = 1;
6559 if (VAR_P (decl))
6560 symbol = varpool_node::get_create (decl);
6561 else
6562 symbol = cgraph_node::get_create (decl);
6564 if (SUPPORTS_ONE_ONLY)
6566 #ifdef MAKE_DECL_ONE_ONLY
6567 MAKE_DECL_ONE_ONLY (decl);
6568 #endif
6569 symbol->set_comdat_group (comdat_group);
6571 else if (VAR_P (decl)
6572 && (DECL_INITIAL (decl) == 0
6573 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6574 DECL_COMMON (decl) = 1;
6575 else
6577 gcc_assert (TARGET_SUPPORTS_WEAK);
6578 DECL_WEAK (decl) = 1;
6582 void
6583 init_varasm_once (void)
6585 section_htab = hash_table<section_hasher>::create_ggc (31);
6586 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6587 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6589 shared_constant_pool = create_constant_pool ();
6591 #ifdef TEXT_SECTION_ASM_OP
6592 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6593 TEXT_SECTION_ASM_OP);
6594 #endif
6596 #ifdef DATA_SECTION_ASM_OP
6597 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6598 DATA_SECTION_ASM_OP);
6599 #endif
6601 #ifdef SDATA_SECTION_ASM_OP
6602 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6603 SDATA_SECTION_ASM_OP);
6604 #endif
6606 #ifdef READONLY_DATA_SECTION_ASM_OP
6607 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6608 READONLY_DATA_SECTION_ASM_OP);
6609 #endif
6611 #ifdef CTORS_SECTION_ASM_OP
6612 ctors_section = get_unnamed_section (0, output_section_asm_op,
6613 CTORS_SECTION_ASM_OP);
6614 #endif
6616 #ifdef DTORS_SECTION_ASM_OP
6617 dtors_section = get_unnamed_section (0, output_section_asm_op,
6618 DTORS_SECTION_ASM_OP);
6619 #endif
6621 #ifdef BSS_SECTION_ASM_OP
6622 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6623 output_section_asm_op,
6624 BSS_SECTION_ASM_OP);
6625 #endif
6627 #ifdef SBSS_SECTION_ASM_OP
6628 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6629 output_section_asm_op,
6630 SBSS_SECTION_ASM_OP);
6631 #endif
6633 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6634 | SECTION_COMMON, emit_tls_common);
6635 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6636 | SECTION_COMMON, emit_local);
6637 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6638 | SECTION_COMMON, emit_common);
6640 #if defined ASM_OUTPUT_ALIGNED_BSS
6641 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6642 emit_bss);
6643 #endif
6645 targetm.asm_out.init_sections ();
6647 if (readonly_data_section == NULL)
6648 readonly_data_section = text_section;
6650 #ifdef ASM_OUTPUT_EXTERNAL
6651 pending_assemble_externals_set = new hash_set<tree>;
6652 #endif
6655 enum tls_model
6656 decl_default_tls_model (const_tree decl)
6658 enum tls_model kind;
6659 bool is_local;
6661 is_local = targetm.binds_local_p (decl);
6662 if (!flag_shlib)
6664 if (is_local)
6665 kind = TLS_MODEL_LOCAL_EXEC;
6666 else
6667 kind = TLS_MODEL_INITIAL_EXEC;
6670 /* Local dynamic is inefficient when we're not combining the
6671 parts of the address. */
6672 else if (optimize && is_local)
6673 kind = TLS_MODEL_LOCAL_DYNAMIC;
6674 else
6675 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6676 if (kind < flag_tls_default)
6677 kind = flag_tls_default;
6679 return kind;
6682 /* Select a set of attributes for section NAME based on the properties
6683 of DECL and whether or not RELOC indicates that DECL's initializer
6684 might contain runtime relocations.
6686 We make the section read-only and executable for a function decl,
6687 read-only for a const data decl, and writable for a non-const data decl. */
6689 unsigned int
6690 default_section_type_flags (tree decl, const char *name, int reloc)
6692 unsigned int flags;
6694 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6695 flags = SECTION_CODE;
6696 else if (decl)
6698 enum section_category category
6699 = categorize_decl_for_section (decl, reloc);
6700 if (decl_readonly_section_1 (category))
6701 flags = 0;
6702 else if (category == SECCAT_DATA_REL_RO
6703 || category == SECCAT_DATA_REL_RO_LOCAL)
6704 flags = SECTION_WRITE | SECTION_RELRO;
6705 else
6706 flags = SECTION_WRITE;
6708 else
6710 flags = SECTION_WRITE;
6711 if (strcmp (name, ".data.rel.ro") == 0
6712 || strcmp (name, ".data.rel.ro.local") == 0)
6713 flags |= SECTION_RELRO;
6716 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6717 flags |= SECTION_LINKONCE;
6719 if (strcmp (name, ".vtable_map_vars") == 0)
6720 flags |= SECTION_LINKONCE;
6722 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6723 flags |= SECTION_TLS | SECTION_WRITE;
6725 if (strcmp (name, ".bss") == 0
6726 || strncmp (name, ".bss.", 5) == 0
6727 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6728 || strcmp (name, ".persistent.bss") == 0
6729 || strcmp (name, ".sbss") == 0
6730 || strncmp (name, ".sbss.", 6) == 0
6731 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6732 flags |= SECTION_BSS;
6734 if (strcmp (name, ".tdata") == 0
6735 || strncmp (name, ".tdata.", 7) == 0
6736 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6737 flags |= SECTION_TLS;
6739 if (strcmp (name, ".tbss") == 0
6740 || strncmp (name, ".tbss.", 6) == 0
6741 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6742 flags |= SECTION_TLS | SECTION_BSS;
6744 if (strcmp (name, ".noinit") == 0)
6745 flags |= SECTION_WRITE | SECTION_BSS | SECTION_NOTYPE;
6747 if (strcmp (name, ".persistent") == 0)
6748 flags |= SECTION_WRITE | SECTION_NOTYPE;
6750 /* Various sections have special ELF types that the assembler will
6751 assign by default based on the name. They are neither SHT_PROGBITS
6752 nor SHT_NOBITS, so when changing sections we don't want to print a
6753 section type (@progbits or @nobits). Rather than duplicating the
6754 assembler's knowledge of what those special name patterns are, just
6755 let the assembler choose the type if we don't know a specific
6756 reason to set it to something other than the default. SHT_PROGBITS
6757 is the default for sections whose name is not specially known to
6758 the assembler, so it does no harm to leave the choice to the
6759 assembler when @progbits is the best thing we know to use. If
6760 someone is silly enough to emit code or TLS variables to one of
6761 these sections, then don't handle them specially.
6763 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6764 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
6765 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6766 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6767 flags |= SECTION_NOTYPE;
6769 return flags;
6772 /* Return true if the target supports some form of global BSS,
6773 either through bss_noswitch_section, or by selecting a BSS
6774 section in TARGET_ASM_SELECT_SECTION. */
6776 bool
6777 have_global_bss_p (void)
6779 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6782 /* Output assembly to switch to section NAME with attribute FLAGS.
6783 Four variants for common object file formats. */
6785 void
6786 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6787 unsigned int flags ATTRIBUTE_UNUSED,
6788 tree decl ATTRIBUTE_UNUSED)
6790 /* Some object formats don't support named sections at all. The
6791 front-end should already have flagged this as an error. */
6792 gcc_unreachable ();
6795 #ifndef TLS_SECTION_ASM_FLAG
6796 #define TLS_SECTION_ASM_FLAG 'T'
6797 #endif
6799 void
6800 default_elf_asm_named_section (const char *name, unsigned int flags,
6801 tree decl)
6803 char flagchars[11], *f = flagchars;
6804 unsigned int numeric_value = 0;
6806 /* If we have already declared this section, we can use an
6807 abbreviated form to switch back to it -- unless this section is
6808 part of a COMDAT groups or with SHF_GNU_RETAIN or with SHF_LINK_ORDER,
6809 in which case GAS requires the full declaration every time. */
6810 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6811 && !(flags & (SECTION_RETAIN | SECTION_LINK_ORDER))
6812 && (flags & SECTION_DECLARED))
6814 fprintf (asm_out_file, "\t.section\t%s\n", name);
6815 return;
6818 /* If we have a machine specific flag, then use the numeric value to pass
6819 this on to GAS. */
6820 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6821 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6822 else
6824 if (!(flags & SECTION_DEBUG))
6825 *f++ = 'a';
6826 #if HAVE_GAS_SECTION_EXCLUDE
6827 if (flags & SECTION_EXCLUDE)
6828 *f++ = 'e';
6829 #endif
6830 if (flags & SECTION_WRITE)
6831 *f++ = 'w';
6832 if (flags & SECTION_CODE)
6833 *f++ = 'x';
6834 if (flags & SECTION_SMALL)
6835 *f++ = 's';
6836 if (flags & SECTION_MERGE)
6837 *f++ = 'M';
6838 if (flags & SECTION_STRINGS)
6839 *f++ = 'S';
6840 if (flags & SECTION_TLS)
6841 *f++ = TLS_SECTION_ASM_FLAG;
6842 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6843 *f++ = 'G';
6844 if (flags & SECTION_RETAIN)
6845 *f++ = 'R';
6846 if (flags & SECTION_LINK_ORDER)
6847 *f++ = 'o';
6848 #ifdef MACH_DEP_SECTION_ASM_FLAG
6849 if (flags & SECTION_MACH_DEP)
6850 *f++ = MACH_DEP_SECTION_ASM_FLAG;
6851 #endif
6852 *f = '\0';
6855 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6857 /* default_section_type_flags (above) knows which flags need special
6858 handling here, and sets NOTYPE when none of these apply so that the
6859 assembler's logic for default types can apply to user-chosen
6860 section names. */
6861 if (!(flags & SECTION_NOTYPE))
6863 const char *type;
6864 const char *format;
6866 if (flags & SECTION_BSS)
6867 type = "nobits";
6868 else
6869 type = "progbits";
6871 format = ",@%s";
6872 /* On platforms that use "@" as the assembly comment character,
6873 use "%" instead. */
6874 if (strcmp (ASM_COMMENT_START, "@") == 0)
6875 format = ",%%%s";
6876 fprintf (asm_out_file, format, type);
6878 if (flags & SECTION_ENTSIZE)
6879 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6880 if (flags & SECTION_LINK_ORDER)
6882 tree id = DECL_ASSEMBLER_NAME (decl);
6883 ultimate_transparent_alias_target (&id);
6884 const char *name = IDENTIFIER_POINTER (id);
6885 name = targetm.strip_name_encoding (name);
6886 fprintf (asm_out_file, ",%s", name);
6888 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6890 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6891 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6892 else
6893 fprintf (asm_out_file, ",%s,comdat",
6894 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6898 putc ('\n', asm_out_file);
6901 void
6902 default_coff_asm_named_section (const char *name, unsigned int flags,
6903 tree decl ATTRIBUTE_UNUSED)
6905 char flagchars[8], *f = flagchars;
6907 if (flags & SECTION_WRITE)
6908 *f++ = 'w';
6909 if (flags & SECTION_CODE)
6910 *f++ = 'x';
6911 *f = '\0';
6913 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6916 void
6917 default_pe_asm_named_section (const char *name, unsigned int flags,
6918 tree decl)
6920 default_coff_asm_named_section (name, flags, decl);
6922 if (flags & SECTION_LINKONCE)
6924 /* Functions may have been compiled at various levels of
6925 optimization so we can't use `same_size' here.
6926 Instead, have the linker pick one. */
6927 fprintf (asm_out_file, "\t.linkonce %s\n",
6928 (flags & SECTION_CODE ? "discard" : "same_size"));
6932 /* The lame default section selector. */
6934 section *
6935 default_select_section (tree decl, int reloc,
6936 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6938 if (DECL_P (decl))
6940 if (decl_readonly_section (decl, reloc))
6941 return readonly_data_section;
6943 else if (TREE_CODE (decl) == CONSTRUCTOR)
6945 if (! ((flag_pic && reloc)
6946 || !TREE_READONLY (decl)
6947 || TREE_SIDE_EFFECTS (decl)
6948 || !TREE_CONSTANT (decl)))
6949 return readonly_data_section;
6951 else if (TREE_CODE (decl) == STRING_CST)
6952 return readonly_data_section;
6953 else if (! (flag_pic && reloc))
6954 return readonly_data_section;
6956 return data_section;
6959 enum section_category
6960 categorize_decl_for_section (const_tree decl, int reloc)
6962 enum section_category ret;
6964 if (TREE_CODE (decl) == FUNCTION_DECL)
6965 return SECCAT_TEXT;
6966 else if (TREE_CODE (decl) == STRING_CST)
6968 if ((flag_sanitize & SANITIZE_ADDRESS)
6969 && asan_protect_global (CONST_CAST_TREE (decl)))
6970 /* or !flag_merge_constants */
6971 return SECCAT_RODATA;
6972 else
6973 return SECCAT_RODATA_MERGE_STR;
6975 else if (VAR_P (decl))
6977 tree d = CONST_CAST_TREE (decl);
6978 if (bss_initializer_p (decl))
6979 ret = SECCAT_BSS;
6980 else if (! TREE_READONLY (decl)
6981 || TREE_SIDE_EFFECTS (decl)
6982 || (DECL_INITIAL (decl)
6983 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
6985 /* Here the reloc_rw_mask is not testing whether the section should
6986 be read-only or not, but whether the dynamic link will have to
6987 do something. If so, we wish to segregate the data in order to
6988 minimize cache misses inside the dynamic linker. */
6989 if (reloc & targetm.asm_out.reloc_rw_mask ())
6990 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6991 else
6992 ret = SECCAT_DATA;
6994 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6995 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6996 else if (reloc || flag_merge_constants < 2
6997 || ((flag_sanitize & SANITIZE_ADDRESS)
6998 /* PR 81697: for architectures that use section anchors we
6999 need to ignore DECL_RTL_SET_P (decl) for string constants
7000 inside this asan_protect_global call because otherwise
7001 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
7002 section, set DECL_RTL (decl) later on and add DECL to
7003 protected globals via successive asan_protect_global
7004 calls. In this scenario we'll end up with wrong
7005 alignment of these strings at runtime and possible ASan
7006 false positives. */
7007 && asan_protect_global (d, use_object_blocks_p ()
7008 && use_blocks_for_decl_p (d))))
7009 /* C and C++ don't allow different variables to share the same
7010 location. -fmerge-all-constants allows even that (at the
7011 expense of not conforming). */
7012 ret = SECCAT_RODATA;
7013 else if (DECL_INITIAL (decl)
7014 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
7015 ret = SECCAT_RODATA_MERGE_STR_INIT;
7016 else
7017 ret = SECCAT_RODATA_MERGE_CONST;
7019 else if (TREE_CODE (decl) == CONSTRUCTOR)
7021 if ((reloc & targetm.asm_out.reloc_rw_mask ())
7022 || TREE_SIDE_EFFECTS (decl)
7023 || ! TREE_CONSTANT (decl))
7024 ret = SECCAT_DATA;
7025 else
7026 ret = SECCAT_RODATA;
7028 else
7029 ret = SECCAT_RODATA;
7031 /* There are no read-only thread-local sections. */
7032 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7034 /* Note that this would be *just* SECCAT_BSS, except that there's
7035 no concept of a read-only thread-local-data section. */
7036 if (ret == SECCAT_BSS
7037 || DECL_INITIAL (decl) == NULL
7038 || (flag_zero_initialized_in_bss
7039 && initializer_zerop (DECL_INITIAL (decl))))
7040 ret = SECCAT_TBSS;
7041 else
7042 ret = SECCAT_TDATA;
7045 /* If the target uses small data sections, select it. */
7046 else if (targetm.in_small_data_p (decl))
7048 if (ret == SECCAT_BSS)
7049 ret = SECCAT_SBSS;
7050 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
7051 ret = SECCAT_SRODATA;
7052 else
7053 ret = SECCAT_SDATA;
7056 return ret;
7059 static bool
7060 decl_readonly_section_1 (enum section_category category)
7062 switch (category)
7064 case SECCAT_RODATA:
7065 case SECCAT_RODATA_MERGE_STR:
7066 case SECCAT_RODATA_MERGE_STR_INIT:
7067 case SECCAT_RODATA_MERGE_CONST:
7068 case SECCAT_SRODATA:
7069 return true;
7070 default:
7071 return false;
7075 bool
7076 decl_readonly_section (const_tree decl, int reloc)
7078 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
7081 /* Select a section based on the above categorization. */
7083 section *
7084 default_elf_select_section (tree decl, int reloc,
7085 unsigned HOST_WIDE_INT align)
7087 const char *sname;
7089 switch (categorize_decl_for_section (decl, reloc))
7091 case SECCAT_TEXT:
7092 /* We're not supposed to be called on FUNCTION_DECLs. */
7093 gcc_unreachable ();
7094 case SECCAT_RODATA:
7095 return readonly_data_section;
7096 case SECCAT_RODATA_MERGE_STR:
7097 return mergeable_string_section (decl, align, 0);
7098 case SECCAT_RODATA_MERGE_STR_INIT:
7099 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
7100 case SECCAT_RODATA_MERGE_CONST:
7101 return mergeable_constant_section (DECL_MODE (decl), align, 0);
7102 case SECCAT_SRODATA:
7103 sname = ".sdata2";
7104 break;
7105 case SECCAT_DATA:
7106 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7108 sname = ".persistent";
7109 break;
7111 return data_section;
7112 case SECCAT_DATA_REL:
7113 sname = ".data.rel";
7114 break;
7115 case SECCAT_DATA_REL_LOCAL:
7116 sname = ".data.rel.local";
7117 break;
7118 case SECCAT_DATA_REL_RO:
7119 sname = ".data.rel.ro";
7120 break;
7121 case SECCAT_DATA_REL_RO_LOCAL:
7122 sname = ".data.rel.ro.local";
7123 break;
7124 case SECCAT_SDATA:
7125 sname = ".sdata";
7126 break;
7127 case SECCAT_TDATA:
7128 sname = ".tdata";
7129 break;
7130 case SECCAT_BSS:
7131 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7133 sname = ".noinit";
7134 break;
7136 if (bss_section)
7137 return bss_section;
7138 sname = ".bss";
7139 break;
7140 case SECCAT_SBSS:
7141 sname = ".sbss";
7142 break;
7143 case SECCAT_TBSS:
7144 sname = ".tbss";
7145 break;
7146 default:
7147 gcc_unreachable ();
7150 return get_named_section (decl, sname, reloc);
7153 /* Construct a unique section name based on the decl name and the
7154 categorization performed above. */
7156 void
7157 default_unique_section (tree decl, int reloc)
7159 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
7160 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
7161 const char *prefix, *name, *linkonce;
7162 char *string;
7163 tree id;
7165 switch (categorize_decl_for_section (decl, reloc))
7167 case SECCAT_TEXT:
7168 prefix = one_only ? ".t" : ".text";
7169 break;
7170 case SECCAT_RODATA:
7171 case SECCAT_RODATA_MERGE_STR:
7172 case SECCAT_RODATA_MERGE_STR_INIT:
7173 case SECCAT_RODATA_MERGE_CONST:
7174 prefix = one_only ? ".r" : ".rodata";
7175 break;
7176 case SECCAT_SRODATA:
7177 prefix = one_only ? ".s2" : ".sdata2";
7178 break;
7179 case SECCAT_DATA:
7180 prefix = one_only ? ".d" : ".data";
7181 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7183 prefix = one_only ? ".p" : ".persistent";
7184 break;
7186 break;
7187 case SECCAT_DATA_REL:
7188 prefix = one_only ? ".d.rel" : ".data.rel";
7189 break;
7190 case SECCAT_DATA_REL_LOCAL:
7191 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
7192 break;
7193 case SECCAT_DATA_REL_RO:
7194 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
7195 break;
7196 case SECCAT_DATA_REL_RO_LOCAL:
7197 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
7198 break;
7199 case SECCAT_SDATA:
7200 prefix = one_only ? ".s" : ".sdata";
7201 break;
7202 case SECCAT_BSS:
7203 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7205 prefix = one_only ? ".n" : ".noinit";
7206 break;
7208 prefix = one_only ? ".b" : ".bss";
7209 break;
7210 case SECCAT_SBSS:
7211 prefix = one_only ? ".sb" : ".sbss";
7212 break;
7213 case SECCAT_TDATA:
7214 prefix = one_only ? ".td" : ".tdata";
7215 break;
7216 case SECCAT_TBSS:
7217 prefix = one_only ? ".tb" : ".tbss";
7218 break;
7219 default:
7220 gcc_unreachable ();
7223 id = DECL_ASSEMBLER_NAME (decl);
7224 ultimate_transparent_alias_target (&id);
7225 name = IDENTIFIER_POINTER (id);
7226 name = targetm.strip_name_encoding (name);
7228 /* If we're using one_only, then there needs to be a .gnu.linkonce
7229 prefix to the section name. */
7230 linkonce = one_only ? ".gnu.linkonce" : "";
7232 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
7234 set_decl_section_name (decl, string);
7237 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
7239 static int
7240 compute_reloc_for_rtx_1 (const_rtx x)
7242 switch (GET_CODE (x))
7244 case SYMBOL_REF:
7245 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
7246 case LABEL_REF:
7247 return 1;
7248 default:
7249 return 0;
7253 /* Like compute_reloc_for_constant, except for an RTX. The return value
7254 is a mask for which bit 1 indicates a global relocation, and bit 0
7255 indicates a local relocation. */
7257 static int
7258 compute_reloc_for_rtx (const_rtx x)
7260 switch (GET_CODE (x))
7262 case SYMBOL_REF:
7263 case LABEL_REF:
7264 return compute_reloc_for_rtx_1 (x);
7266 case CONST:
7268 int reloc = 0;
7269 subrtx_iterator::array_type array;
7270 FOR_EACH_SUBRTX (iter, array, x, ALL)
7271 reloc |= compute_reloc_for_rtx_1 (*iter);
7272 return reloc;
7275 default:
7276 return 0;
7280 section *
7281 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
7282 rtx x,
7283 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7285 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
7286 return data_section;
7287 else
7288 return readonly_data_section;
7291 section *
7292 default_elf_select_rtx_section (machine_mode mode, rtx x,
7293 unsigned HOST_WIDE_INT align)
7295 int reloc = compute_reloc_for_rtx (x);
7297 /* ??? Handle small data here somehow. */
7299 if (reloc & targetm.asm_out.reloc_rw_mask ())
7301 if (reloc == 1)
7302 return get_named_section (NULL, ".data.rel.ro.local", 1);
7303 else
7304 return get_named_section (NULL, ".data.rel.ro", 3);
7307 return mergeable_constant_section (mode, align, 0);
7310 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
7312 void
7313 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
7315 rtx symbol;
7316 int flags;
7318 /* Careful not to prod global register variables. */
7319 if (!MEM_P (rtl))
7320 return;
7321 symbol = XEXP (rtl, 0);
7322 if (GET_CODE (symbol) != SYMBOL_REF)
7323 return;
7325 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
7326 if (TREE_CODE (decl) == FUNCTION_DECL)
7327 flags |= SYMBOL_FLAG_FUNCTION;
7328 if (targetm.binds_local_p (decl))
7329 flags |= SYMBOL_FLAG_LOCAL;
7330 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7331 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
7332 else if (targetm.in_small_data_p (decl))
7333 flags |= SYMBOL_FLAG_SMALL;
7334 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
7335 being PUBLIC, the thing *must* be defined in this translation unit.
7336 Prevent this buglet from being propagated into rtl code as well. */
7337 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
7338 flags |= SYMBOL_FLAG_EXTERNAL;
7340 SYMBOL_REF_FLAGS (symbol) = flags;
7343 /* By default, we do nothing for encode_section_info, so we need not
7344 do anything but discard the '*' marker. */
7346 const char *
7347 default_strip_name_encoding (const char *str)
7349 return str + (*str == '*');
7352 #ifdef ASM_OUTPUT_DEF
7353 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
7354 anchor relative to ".", the current section position. */
7356 void
7357 default_asm_output_anchor (rtx symbol)
7359 char buffer[100];
7361 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7362 SYMBOL_REF_BLOCK_OFFSET (symbol));
7363 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7365 #endif
7367 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
7369 bool
7370 default_use_anchors_for_symbol_p (const_rtx symbol)
7372 tree decl;
7373 section *sect = SYMBOL_REF_BLOCK (symbol)->sect;
7375 /* This function should only be called with non-zero SYMBOL_REF_BLOCK,
7376 furthermore get_block_for_section should not create object blocks
7377 for mergeable sections. */
7378 gcc_checking_assert (sect && !(sect->common.flags & SECTION_MERGE));
7380 /* Don't use anchors for small data sections. The small data register
7381 acts as an anchor for such sections. */
7382 if (sect->common.flags & SECTION_SMALL)
7383 return false;
7385 decl = SYMBOL_REF_DECL (symbol);
7386 if (decl && DECL_P (decl))
7388 /* Don't use section anchors for decls that might be defined or
7389 usurped by other modules. */
7390 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
7391 return false;
7393 /* Don't use section anchors for decls that will be placed in a
7394 small data section. */
7395 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
7396 one above. The problem is that we only use SECTION_SMALL for
7397 sections that should be marked as small in the section directive. */
7398 if (targetm.in_small_data_p (decl))
7399 return false;
7401 /* Don't use section anchors for decls that won't fit inside a single
7402 anchor range to reduce the amount of instructions required to refer
7403 to the entire declaration. */
7404 if (DECL_SIZE_UNIT (decl) == NULL_TREE
7405 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
7406 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
7407 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
7408 return false;
7411 return true;
7414 /* Return true when RESOLUTION indicate that symbol will be bound to the
7415 definition provided by current .o file. */
7417 static bool
7418 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
7420 return (resolution == LDPR_PREVAILING_DEF
7421 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7422 || resolution == LDPR_PREVAILING_DEF_IRONLY);
7425 /* Return true when RESOLUTION indicate that symbol will be bound locally
7426 within current executable or DSO. */
7428 static bool
7429 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7431 return (resolution == LDPR_PREVAILING_DEF
7432 || resolution == LDPR_PREVAILING_DEF_IRONLY
7433 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7434 || resolution == LDPR_PREEMPTED_REG
7435 || resolution == LDPR_PREEMPTED_IR
7436 || resolution == LDPR_RESOLVED_IR
7437 || resolution == LDPR_RESOLVED_EXEC);
7440 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7441 uninitialized common symbol in the executable will still be defined
7442 (through COPY relocation) in the executable. */
7444 bool
7445 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7446 bool extern_protected_data, bool common_local_p)
7448 /* A non-decl is an entry in the constant pool. */
7449 if (!DECL_P (exp))
7450 return true;
7452 /* Weakrefs may not bind locally, even though the weakref itself is always
7453 static and therefore local. Similarly, the resolver for ifunc functions
7454 might resolve to a non-local function.
7455 FIXME: We can resolve the weakref case more curefuly by looking at the
7456 weakref alias. */
7457 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7458 || (TREE_CODE (exp) == FUNCTION_DECL
7459 && cgraph_node::get (exp)
7460 && cgraph_node::get (exp)->ifunc_resolver))
7461 return false;
7463 /* Static variables are always local. */
7464 if (! TREE_PUBLIC (exp))
7465 return true;
7467 /* With resolution file in hand, take look into resolutions.
7468 We can't just return true for resolved_locally symbols,
7469 because dynamic linking might overwrite symbols
7470 in shared libraries. */
7471 bool resolved_locally = false;
7473 bool uninited_common = (DECL_COMMON (exp)
7474 && (DECL_INITIAL (exp) == NULL
7475 || (!in_lto_p
7476 && DECL_INITIAL (exp) == error_mark_node)));
7478 /* A non-external variable is defined locally only if it isn't
7479 uninitialized COMMON variable or common_local_p is true. */
7480 bool defined_locally = (!DECL_EXTERNAL (exp)
7481 && (!uninited_common || common_local_p));
7482 if (symtab_node *node = symtab_node::get (exp))
7484 if (node->in_other_partition)
7485 defined_locally = true;
7486 if (node->can_be_discarded_p ())
7488 else if (resolution_to_local_definition_p (node->resolution))
7489 defined_locally = resolved_locally = true;
7490 else if (resolution_local_p (node->resolution))
7491 resolved_locally = true;
7493 if (defined_locally && weak_dominate && !shlib)
7494 resolved_locally = true;
7496 /* Undefined weak symbols are never defined locally. */
7497 if (DECL_WEAK (exp) && !defined_locally)
7498 return false;
7500 /* A symbol is local if the user has said explicitly that it will be,
7501 or if we have a definition for the symbol. We cannot infer visibility
7502 for undefined symbols. */
7503 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7504 && (TREE_CODE (exp) == FUNCTION_DECL
7505 || !extern_protected_data
7506 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7507 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7508 return true;
7510 /* If PIC, then assume that any global name can be overridden by
7511 symbols resolved from other modules. */
7512 if (shlib)
7513 return false;
7515 /* Variables defined outside this object might not be local. */
7516 if (DECL_EXTERNAL (exp) && !resolved_locally)
7517 return false;
7519 /* Non-dominant weak symbols are not defined locally. */
7520 if (DECL_WEAK (exp) && !resolved_locally)
7521 return false;
7523 /* Uninitialized COMMON variable may be unified with symbols
7524 resolved from other modules. */
7525 if (uninited_common && !resolved_locally)
7526 return false;
7528 /* Otherwise we're left with initialized (or non-common) global data
7529 which is of necessity defined locally. */
7530 return true;
7533 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7534 wrt cross-module name binding. */
7536 bool
7537 default_binds_local_p (const_tree exp)
7539 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7542 /* Similar to default_binds_local_p, but common symbol may be local and
7543 extern protected data is non-local. */
7545 bool
7546 default_binds_local_p_2 (const_tree exp)
7548 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7549 !flag_pic);
7552 bool
7553 default_binds_local_p_1 (const_tree exp, int shlib)
7555 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7558 /* Return true when references to DECL must bind to current definition in
7559 final executable.
7561 The condition is usually equivalent to whether the function binds to the
7562 current module (shared library or executable), that is to binds_local_p.
7563 We use this fact to avoid need for another target hook and implement
7564 the logic using binds_local_p and just special cases where
7565 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7566 the weak definitions (that can be overwritten at linktime by other
7567 definition from different object file) and when resolution info is available
7568 we simply use the knowledge passed to us by linker plugin. */
7569 bool
7570 decl_binds_to_current_def_p (const_tree decl)
7572 gcc_assert (DECL_P (decl));
7573 if (!targetm.binds_local_p (decl))
7574 return false;
7575 if (!TREE_PUBLIC (decl))
7576 return true;
7578 /* When resolution is available, just use it. */
7579 if (symtab_node *node = symtab_node::get (decl))
7581 if (node->resolution != LDPR_UNKNOWN
7582 && !node->can_be_discarded_p ())
7583 return resolution_to_local_definition_p (node->resolution);
7586 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7587 binds locally but still can be overwritten), DECL_COMMON (can be merged
7588 with a non-common definition somewhere in the same module) or
7589 DECL_EXTERNAL.
7590 This rely on fact that binds_local_p behave as decl_replaceable_p
7591 for all other declaration types. */
7592 if (DECL_WEAK (decl))
7593 return false;
7594 if (DECL_COMMON (decl)
7595 && (DECL_INITIAL (decl) == NULL
7596 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7597 return false;
7598 if (DECL_EXTERNAL (decl))
7599 return false;
7600 return true;
7603 /* A replaceable function or variable is one which may be replaced
7604 at link-time with an entirely different definition, provided that the
7605 replacement has the same type. For example, functions declared
7606 with __attribute__((weak)) on most systems are replaceable.
7608 COMDAT functions are not replaceable, since all definitions of the
7609 function must be equivalent. It is important that COMDAT functions
7610 not be treated as replaceable so that use of C++ template
7611 instantiations is not penalized. */
7613 bool
7614 decl_replaceable_p (tree decl)
7616 gcc_assert (DECL_P (decl));
7617 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7618 return false;
7619 if (!flag_semantic_interposition
7620 && !DECL_WEAK (decl))
7621 return false;
7622 return !decl_binds_to_current_def_p (decl);
7625 /* Default function to output code that will globalize a label. A
7626 target must define GLOBAL_ASM_OP or provide its own function to
7627 globalize a label. */
7628 #ifdef GLOBAL_ASM_OP
7629 void
7630 default_globalize_label (FILE * stream, const char *name)
7632 fputs (GLOBAL_ASM_OP, stream);
7633 assemble_name (stream, name);
7634 putc ('\n', stream);
7636 #endif /* GLOBAL_ASM_OP */
7638 /* Default function to output code that will globalize a declaration. */
7639 void
7640 default_globalize_decl_name (FILE * stream, tree decl)
7642 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7643 targetm.asm_out.globalize_label (stream, name);
7646 /* Default function to output a label for unwind information. The
7647 default is to do nothing. A target that needs nonlocal labels for
7648 unwind information must provide its own function to do this. */
7649 void
7650 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7651 tree decl ATTRIBUTE_UNUSED,
7652 int for_eh ATTRIBUTE_UNUSED,
7653 int empty ATTRIBUTE_UNUSED)
7657 /* Default function to output a label to divide up the exception table.
7658 The default is to do nothing. A target that needs/wants to divide
7659 up the table must provide it's own function to do this. */
7660 void
7661 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7665 /* This is how to output an internal numbered label where PREFIX is
7666 the class of label and LABELNO is the number within the class. */
7668 void
7669 default_generate_internal_label (char *buf, const char *prefix,
7670 unsigned long labelno)
7672 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7675 /* This is how to output an internal numbered label where PREFIX is
7676 the class of label and LABELNO is the number within the class. */
7678 void
7679 default_internal_label (FILE *stream, const char *prefix,
7680 unsigned long labelno)
7682 char *const buf = (char *) alloca (40 + strlen (prefix));
7683 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7684 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7688 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7690 void
7691 default_asm_declare_constant_name (FILE *file, const char *name,
7692 const_tree exp ATTRIBUTE_UNUSED,
7693 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7695 assemble_label (file, name);
7698 /* This is the default behavior at the beginning of a file. It's
7699 controlled by two other target-hook toggles. */
7700 void
7701 default_file_start (void)
7703 if (targetm.asm_file_start_app_off
7704 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7705 fputs (ASM_APP_OFF, asm_out_file);
7707 if (targetm.asm_file_start_file_directive)
7709 /* LTO produced units have no meaningful main_input_filename. */
7710 if (in_lto_p)
7711 output_file_directive (asm_out_file, "<artificial>");
7712 else
7713 output_file_directive (asm_out_file, main_input_filename);
7717 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7718 which emits a special section directive used to indicate whether or
7719 not this object file needs an executable stack. This is primarily
7720 a GNU extension to ELF but could be used on other targets. */
7722 int trampolines_created;
7724 void
7725 file_end_indicate_exec_stack (void)
7727 unsigned int flags = SECTION_DEBUG;
7728 if (trampolines_created)
7729 flags |= SECTION_CODE;
7731 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7734 /* Emit a special section directive to indicate that this object file
7735 was compiled with -fsplit-stack. This is used to let the linker
7736 detect calls between split-stack code and non-split-stack code, so
7737 that it can modify the split-stack code to allocate a sufficiently
7738 large stack. We emit another special section if there are any
7739 functions in this file which have the no_split_stack attribute, to
7740 prevent the linker from warning about being unable to convert the
7741 functions if they call non-split-stack code. */
7743 void
7744 file_end_indicate_split_stack (void)
7746 if (flag_split_stack)
7748 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7749 NULL));
7750 if (saw_no_split_stack)
7751 switch_to_section (get_section (".note.GNU-no-split-stack",
7752 SECTION_DEBUG, NULL));
7756 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7757 a get_unnamed_section callback. */
7759 void
7760 output_section_asm_op (const void *directive)
7762 fprintf (asm_out_file, "%s\n", (const char *) directive);
7765 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7766 the current section is NEW_SECTION. */
7768 void
7769 switch_to_section (section *new_section, tree decl)
7771 bool retain_p;
7772 if ((new_section->common.flags & SECTION_NAMED)
7773 && decl != nullptr
7774 && DECL_P (decl)
7775 && ((retain_p = !!lookup_attribute ("retain",
7776 DECL_ATTRIBUTES (decl)))
7777 != !!(new_section->common.flags & SECTION_RETAIN)))
7779 /* If the SECTION_RETAIN bit doesn't match, switch to a new
7780 section. */
7781 tree used_decl, no_used_decl;
7783 if (retain_p)
7785 new_section->common.flags |= SECTION_RETAIN;
7786 used_decl = decl;
7787 no_used_decl = new_section->named.decl;
7789 else
7791 new_section->common.flags &= ~(SECTION_RETAIN
7792 | SECTION_DECLARED);
7793 used_decl = new_section->named.decl;
7794 no_used_decl = decl;
7796 if (no_used_decl != used_decl)
7798 warning (OPT_Wattributes,
7799 "%+qD without %<retain%> attribute and %qD with "
7800 "%<retain%> attribute are placed in a section with "
7801 "the same name", no_used_decl, used_decl);
7802 inform (DECL_SOURCE_LOCATION (used_decl),
7803 "%qD was declared here", used_decl);
7806 else if (in_section == new_section)
7807 return;
7809 if (new_section->common.flags & SECTION_FORGET)
7810 in_section = NULL;
7811 else
7812 in_section = new_section;
7814 switch (SECTION_STYLE (new_section))
7816 case SECTION_NAMED:
7817 targetm.asm_out.named_section (new_section->named.name,
7818 new_section->named.common.flags,
7819 new_section->named.decl);
7820 break;
7822 case SECTION_UNNAMED:
7823 new_section->unnamed.callback (new_section->unnamed.data);
7824 break;
7826 case SECTION_NOSWITCH:
7827 gcc_unreachable ();
7828 break;
7831 new_section->common.flags |= SECTION_DECLARED;
7834 /* If block symbol SYMBOL has not yet been assigned an offset, place
7835 it at the end of its block. */
7837 void
7838 place_block_symbol (rtx symbol)
7840 unsigned HOST_WIDE_INT size, mask, offset;
7841 class constant_descriptor_rtx *desc;
7842 unsigned int alignment;
7843 struct object_block *block;
7844 tree decl;
7846 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7847 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7848 return;
7850 /* Work out the symbol's size and alignment. */
7851 if (CONSTANT_POOL_ADDRESS_P (symbol))
7853 desc = SYMBOL_REF_CONSTANT (symbol);
7854 alignment = desc->align;
7855 size = GET_MODE_SIZE (desc->mode);
7857 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7859 decl = SYMBOL_REF_DECL (symbol);
7860 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7861 alignment = DECL_ALIGN (decl);
7862 size = get_constant_size (DECL_INITIAL (decl));
7863 if ((flag_sanitize & SANITIZE_ADDRESS)
7864 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7865 && asan_protect_global (DECL_INITIAL (decl)))
7867 size += asan_red_zone_size (size);
7868 alignment = MAX (alignment,
7869 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7872 else
7874 struct symtab_node *snode;
7875 decl = SYMBOL_REF_DECL (symbol);
7877 snode = symtab_node::get (decl);
7878 if (snode->alias)
7880 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7882 gcc_assert (MEM_P (target)
7883 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7884 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7885 target = XEXP (target, 0);
7886 place_block_symbol (target);
7887 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7888 return;
7890 alignment = get_variable_align (decl);
7891 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7892 if ((flag_sanitize & SANITIZE_ADDRESS)
7893 && asan_protect_global (decl))
7895 size += asan_red_zone_size (size);
7896 alignment = MAX (alignment,
7897 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7901 /* Calculate the object's offset from the start of the block. */
7902 block = SYMBOL_REF_BLOCK (symbol);
7903 mask = alignment / BITS_PER_UNIT - 1;
7904 offset = (block->size + mask) & ~mask;
7905 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7907 /* Record the block's new alignment and size. */
7908 block->alignment = MAX (block->alignment, alignment);
7909 block->size = offset + size;
7911 vec_safe_push (block->objects, symbol);
7914 /* Return the anchor that should be used to address byte offset OFFSET
7915 from the first object in BLOCK. MODEL is the TLS model used
7916 to access it. */
7919 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7920 enum tls_model model)
7922 char label[100];
7923 unsigned int begin, middle, end;
7924 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7925 rtx anchor;
7927 /* Work out the anchor's offset. Use an offset of 0 for the first
7928 anchor so that we don't pessimize the case where we take the address
7929 of a variable at the beginning of the block. This is particularly
7930 useful when a block has only one variable assigned to it.
7932 We try to place anchors RANGE bytes apart, so there can then be
7933 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7934 a ptr_mode offset. With some target settings, the lowest such
7935 anchor might be out of range for the lowest ptr_mode offset;
7936 likewise the highest anchor for the highest offset. Use anchors
7937 at the extreme ends of the ptr_mode range in such cases.
7939 All arithmetic uses unsigned integers in order to avoid
7940 signed overflow. */
7941 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7942 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7943 range = max_offset - min_offset + 1;
7944 if (range == 0)
7945 offset = 0;
7946 else
7948 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7949 if (offset < 0)
7951 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7952 delta -= delta % range;
7953 if (delta > bias)
7954 delta = bias;
7955 offset = (HOST_WIDE_INT) (-delta);
7957 else
7959 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7960 delta -= delta % range;
7961 if (delta > bias - 1)
7962 delta = bias - 1;
7963 offset = (HOST_WIDE_INT) delta;
7967 /* Do a binary search to see if there's already an anchor we can use.
7968 Set BEGIN to the new anchor's index if not. */
7969 begin = 0;
7970 end = vec_safe_length (block->anchors);
7971 while (begin != end)
7973 middle = (end + begin) / 2;
7974 anchor = (*block->anchors)[middle];
7975 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7976 end = middle;
7977 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7978 begin = middle + 1;
7979 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7980 end = middle;
7981 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7982 begin = middle + 1;
7983 else
7984 return anchor;
7987 /* Create a new anchor with a unique label. */
7988 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7989 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7990 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7991 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7993 /* Insert it at index BEGIN. */
7994 vec_safe_insert (block->anchors, begin, anchor);
7995 return anchor;
7998 /* Output the objects in BLOCK. */
8000 static void
8001 output_object_block (struct object_block *block)
8003 class constant_descriptor_rtx *desc;
8004 unsigned int i;
8005 HOST_WIDE_INT offset;
8006 tree decl;
8007 rtx symbol;
8009 if (!block->objects)
8010 return;
8012 /* Switch to the section and make sure that the first byte is
8013 suitably aligned. */
8014 /* Special case VTV comdat sections similar to assemble_variable. */
8015 if (SECTION_STYLE (block->sect) == SECTION_NAMED
8016 && block->sect->named.name
8017 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
8018 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
8019 else
8020 switch_to_section (block->sect, SYMBOL_REF_DECL ((*block->objects)[0]));
8022 gcc_checking_assert (!(block->sect->common.flags & SECTION_MERGE));
8023 assemble_align (block->alignment);
8025 /* Define the values of all anchors relative to the current section
8026 position. */
8027 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
8028 targetm.asm_out.output_anchor (symbol);
8030 /* Output the objects themselves. */
8031 offset = 0;
8032 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
8034 /* Move to the object's offset, padding with zeros if necessary. */
8035 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
8036 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
8037 if (CONSTANT_POOL_ADDRESS_P (symbol))
8039 desc = SYMBOL_REF_CONSTANT (symbol);
8040 /* Pass 1 for align as we have already laid out everything in the block.
8041 So aligning shouldn't be necessary. */
8042 output_constant_pool_1 (desc, 1);
8043 offset += GET_MODE_SIZE (desc->mode);
8045 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
8047 HOST_WIDE_INT size;
8048 decl = SYMBOL_REF_DECL (symbol);
8049 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
8050 DECL_ALIGN (decl), false);
8052 size = get_constant_size (DECL_INITIAL (decl));
8053 offset += size;
8054 if ((flag_sanitize & SANITIZE_ADDRESS)
8055 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8056 && asan_protect_global (DECL_INITIAL (decl)))
8058 size = asan_red_zone_size (size);
8059 assemble_zeros (size);
8060 offset += size;
8063 else
8065 HOST_WIDE_INT size;
8066 decl = SYMBOL_REF_DECL (symbol);
8067 assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
8068 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8069 offset += size;
8070 if ((flag_sanitize & SANITIZE_ADDRESS)
8071 && asan_protect_global (decl))
8073 size = asan_red_zone_size (size);
8074 assemble_zeros (size);
8075 offset += size;
8081 /* A callback for qsort to compare object_blocks. */
8083 static int
8084 output_object_block_compare (const void *x, const void *y)
8086 object_block *p1 = *(object_block * const*)x;
8087 object_block *p2 = *(object_block * const*)y;
8089 if (p1->sect->common.flags & SECTION_NAMED
8090 && !(p2->sect->common.flags & SECTION_NAMED))
8091 return 1;
8093 if (!(p1->sect->common.flags & SECTION_NAMED)
8094 && p2->sect->common.flags & SECTION_NAMED)
8095 return -1;
8097 if (p1->sect->common.flags & SECTION_NAMED
8098 && p2->sect->common.flags & SECTION_NAMED)
8099 return strcmp (p1->sect->named.name, p2->sect->named.name);
8101 unsigned f1 = p1->sect->common.flags;
8102 unsigned f2 = p2->sect->common.flags;
8103 if (f1 == f2)
8104 return 0;
8105 return f1 < f2 ? -1 : 1;
8108 /* Output the definitions of all object_blocks. */
8110 void
8111 output_object_blocks (void)
8113 vec<object_block *, va_heap> v;
8114 v.create (object_block_htab->elements ());
8115 object_block *obj;
8116 hash_table<object_block_hasher>::iterator hi;
8118 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
8119 v.quick_push (obj);
8121 /* Sort them in order to output them in a deterministic manner,
8122 otherwise we may get .rodata sections in different orders with
8123 and without -g. */
8124 v.qsort (output_object_block_compare);
8125 unsigned i;
8126 FOR_EACH_VEC_ELT (v, i, obj)
8127 output_object_block (obj);
8129 v.release ();
8132 /* This function provides a possible implementation of the
8133 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
8134 by -frecord-gcc-switches it creates a new mergeable, string section in the
8135 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
8136 contains the switches in ASCII format.
8138 FIXME: This code does not correctly handle double quote characters
8139 that appear inside strings, (it strips them rather than preserving them).
8140 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
8141 characters - instead it treats them as sub-string separators. Since
8142 we want to emit NUL strings terminators into the object file we have to use
8143 ASM_OUTPUT_SKIP. */
8145 void
8146 elf_record_gcc_switches (const char *options)
8148 section *sec = get_section (targetm.asm_out.record_gcc_switches_section,
8149 SECTION_DEBUG | SECTION_MERGE
8150 | SECTION_STRINGS | (SECTION_ENTSIZE & 1), NULL);
8151 switch_to_section (sec);
8152 ASM_OUTPUT_ASCII (asm_out_file, options, strlen (options) + 1);
8155 /* Emit text to declare externally defined symbols. It is needed to
8156 properly support non-default visibility. */
8157 void
8158 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
8159 tree decl,
8160 const char *name ATTRIBUTE_UNUSED)
8162 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8163 set in order to avoid putting out names that are never really
8164 used. Always output visibility specified in the source. */
8165 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
8166 && (DECL_VISIBILITY_SPECIFIED (decl)
8167 || targetm.binds_local_p (decl)))
8168 maybe_assemble_visibility (decl);
8171 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8173 void
8174 default_asm_output_source_filename (FILE *file, const char *name)
8176 #ifdef ASM_OUTPUT_SOURCE_FILENAME
8177 ASM_OUTPUT_SOURCE_FILENAME (file, name);
8178 #else
8179 fprintf (file, "\t.file\t");
8180 output_quoted_string (file, name);
8181 putc ('\n', file);
8182 #endif
8185 /* Output a file name in the form wanted by System V. */
8187 void
8188 output_file_directive (FILE *asm_file, const char *input_name)
8190 int len;
8191 const char *na;
8193 if (input_name == NULL)
8194 input_name = "<stdin>";
8195 else
8196 input_name = remap_debug_filename (input_name);
8198 len = strlen (input_name);
8199 na = input_name + len;
8201 /* NA gets INPUT_NAME sans directory names. */
8202 while (na > input_name)
8204 if (IS_DIR_SEPARATOR (na[-1]))
8205 break;
8206 na--;
8209 targetm.asm_out.output_source_filename (asm_file, na);
8212 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
8213 EXP. */
8215 make_debug_expr_from_rtl (const_rtx exp)
8217 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
8218 machine_mode mode = GET_MODE (exp);
8219 rtx dval;
8221 DECL_ARTIFICIAL (ddecl) = 1;
8222 if (REG_P (exp) && REG_EXPR (exp))
8223 type = TREE_TYPE (REG_EXPR (exp));
8224 else if (MEM_P (exp) && MEM_EXPR (exp))
8225 type = TREE_TYPE (MEM_EXPR (exp));
8226 else
8227 type = NULL_TREE;
8228 if (type && TYPE_MODE (type) == mode)
8229 TREE_TYPE (ddecl) = type;
8230 else
8231 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
8232 SET_DECL_MODE (ddecl, mode);
8233 dval = gen_rtx_DEBUG_EXPR (mode);
8234 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
8235 SET_DECL_RTL (ddecl, dval);
8236 return dval;
8239 #ifdef ELF_ASCII_ESCAPES
8240 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
8242 void
8243 default_elf_asm_output_limited_string (FILE *f, const char *s)
8245 int escape;
8246 unsigned char c;
8248 fputs (STRING_ASM_OP, f);
8249 putc ('"', f);
8250 while (*s != '\0')
8252 c = *s;
8253 escape = ELF_ASCII_ESCAPES[c];
8254 switch (escape)
8256 case 0:
8257 putc (c, f);
8258 break;
8259 case 1:
8260 putc ('\\', f);
8261 putc ('0'+((c>>6)&7), f);
8262 putc ('0'+((c>>3)&7), f);
8263 putc ('0'+(c&7), f);
8264 break;
8265 default:
8266 putc ('\\', f);
8267 putc (escape, f);
8268 break;
8270 s++;
8272 putc ('\"', f);
8273 putc ('\n', f);
8276 /* Default ASM_OUTPUT_ASCII for ELF targets. */
8278 void
8279 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
8281 const char *limit = s + len;
8282 const char *last_null = NULL;
8283 unsigned bytes_in_chunk = 0;
8284 unsigned char c;
8285 int escape;
8287 for (; s < limit; s++)
8289 const char *p;
8291 if (bytes_in_chunk >= 60)
8293 putc ('\"', f);
8294 putc ('\n', f);
8295 bytes_in_chunk = 0;
8298 if (s > last_null)
8300 for (p = s; p < limit && *p != '\0'; p++)
8301 continue;
8302 last_null = p;
8304 else
8305 p = last_null;
8307 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
8309 if (bytes_in_chunk > 0)
8311 putc ('\"', f);
8312 putc ('\n', f);
8313 bytes_in_chunk = 0;
8316 default_elf_asm_output_limited_string (f, s);
8317 s = p;
8319 else
8321 if (bytes_in_chunk == 0)
8322 fputs (ASCII_DATA_ASM_OP "\"", f);
8324 c = *s;
8325 escape = ELF_ASCII_ESCAPES[c];
8326 switch (escape)
8328 case 0:
8329 putc (c, f);
8330 bytes_in_chunk++;
8331 break;
8332 case 1:
8333 putc ('\\', f);
8334 putc ('0'+((c>>6)&7), f);
8335 putc ('0'+((c>>3)&7), f);
8336 putc ('0'+(c&7), f);
8337 bytes_in_chunk += 4;
8338 break;
8339 default:
8340 putc ('\\', f);
8341 putc (escape, f);
8342 bytes_in_chunk += 2;
8343 break;
8349 if (bytes_in_chunk > 0)
8351 putc ('\"', f);
8352 putc ('\n', f);
8355 #endif
8357 static GTY(()) section *elf_init_array_section;
8358 static GTY(()) section *elf_fini_array_section;
8360 static section *
8361 get_elf_initfini_array_priority_section (int priority,
8362 bool constructor_p)
8364 section *sec;
8365 if (priority != DEFAULT_INIT_PRIORITY)
8367 char buf[18];
8368 sprintf (buf, "%s.%.5u",
8369 constructor_p ? ".init_array" : ".fini_array",
8370 priority);
8371 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8373 else
8375 if (constructor_p)
8377 if (elf_init_array_section == NULL)
8378 elf_init_array_section
8379 = get_section (".init_array",
8380 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8381 sec = elf_init_array_section;
8383 else
8385 if (elf_fini_array_section == NULL)
8386 elf_fini_array_section
8387 = get_section (".fini_array",
8388 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8389 sec = elf_fini_array_section;
8392 return sec;
8395 /* Use .init_array section for constructors. */
8397 void
8398 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8400 section *sec = get_elf_initfini_array_priority_section (priority,
8401 true);
8402 assemble_addr_to_section (symbol, sec);
8405 /* Use .fini_array section for destructors. */
8407 void
8408 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8410 section *sec = get_elf_initfini_array_priority_section (priority,
8411 false);
8412 assemble_addr_to_section (symbol, sec);
8415 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8417 This is a bit of a cheat. The real default is a no-op, but this
8418 hook is the default for all targets with a .ident directive. */
8420 void
8421 default_asm_output_ident_directive (const char *ident_str)
8423 const char *ident_asm_op = "\t.ident\t";
8425 /* If we are still in the front end, do not write out the string
8426 to asm_out_file. Instead, add a fake top-level asm statement.
8427 This allows the front ends to use this hook without actually
8428 writing to asm_out_file, to handle #ident or Pragma Ident. */
8429 if (symtab->state == PARSING)
8431 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8432 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8434 else
8435 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8439 /* This function ensures that vtable_map variables are not only
8440 in the comdat section, but that each variable has its own unique
8441 comdat name. Without this the variables end up in the same section
8442 with a single comdat name.
8444 FIXME: resolve_unique_section needs to deal better with
8445 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8446 that is fixed, this if-else statement can be replaced with
8447 a single call to "switch_to_section (sect)". */
8449 static void
8450 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8452 #if defined (OBJECT_FORMAT_ELF)
8453 targetm.asm_out.named_section (sect->named.name,
8454 sect->named.common.flags
8455 | SECTION_LINKONCE,
8456 DECL_NAME (decl));
8457 in_section = sect;
8458 #else
8459 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8460 Therefore the following check is used.
8461 In case a the target is PE or COFF a comdat group section
8462 is created, e.g. .vtable_map_vars$foo. The linker places
8463 everything in .vtable_map_vars at the end.
8465 A fix could be made in
8466 gcc/config/i386/winnt.c: i386_pe_unique_section. */
8467 if (TARGET_PECOFF)
8469 char *name;
8471 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
8472 name = ACONCAT ((sect->named.name, "$",
8473 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
8474 else
8475 name = ACONCAT ((sect->named.name, "$",
8476 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
8477 NULL));
8479 targetm.asm_out.named_section (name,
8480 sect->named.common.flags
8481 | SECTION_LINKONCE,
8482 DECL_NAME (decl));
8483 in_section = sect;
8485 else
8486 switch_to_section (sect);
8487 #endif
8490 #include "gt-varasm.h"