Disable tests for strdup/strndup on __hpux__
[official-gcc.git] / gcc / varasm.cc
blobd2c879b7da44df7c8d09ef1532902c20c73dd841
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2024 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "backend.h"
32 #include "target.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "predict.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "stringpool.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "varasm.h"
46 #include "version.h"
47 #include "flags.h"
48 #include "stmt.h"
49 #include "expr.h"
50 #include "expmed.h"
51 #include "optabs.h"
52 #include "output.h"
53 #include "langhooks.h"
54 #include "debug.h"
55 #include "common/common-target.h"
56 #include "stringpool.h"
57 #include "attribs.h"
58 #include "asan.h"
59 #include "rtl-iter.h"
60 #include "file-prefix-map.h" /* remap_debug_filename() */
61 #include "alloc-pool.h"
62 #include "toplev.h"
63 #include "opts.h"
64 #include "asan.h"
66 /* The (assembler) name of the first globally-visible object output. */
67 extern GTY(()) const char *first_global_object_name;
68 extern GTY(()) const char *weak_global_object_name;
70 const char *first_global_object_name;
71 const char *weak_global_object_name;
73 class addr_const;
74 class constant_descriptor_rtx;
75 struct rtx_constant_pool;
77 #define n_deferred_constants (crtl->varasm.deferred_constants)
79 /* Number for making the label on the next
80 constant that is stored in memory. */
82 static GTY(()) int const_labelno;
84 /* Carry information from ASM_DECLARE_OBJECT_NAME
85 to ASM_FINISH_DECLARE_OBJECT. */
87 int size_directive_output;
89 /* The last decl for which assemble_variable was called,
90 if it did ASM_DECLARE_OBJECT_NAME.
91 If the last call to assemble_variable didn't do that,
92 this holds 0. */
94 tree last_assemble_variable_decl;
96 /* The following global variable indicates if the first basic block
97 in a function belongs to the cold partition or not. */
99 bool first_function_block_is_cold;
101 /* Whether we saw any functions with no_split_stack. */
103 static bool saw_no_split_stack;
105 static const char *strip_reg_name (const char *);
106 static bool contains_pointers_p (tree);
107 #ifdef ASM_OUTPUT_EXTERNAL
108 static bool incorporeal_function_p (tree);
109 #endif
110 static void decode_addr_const (tree, class addr_const *);
111 static hashval_t const_hash_1 (const tree);
112 static bool compare_constant (const tree, const tree);
113 static void output_constant_def_contents (rtx);
114 static void output_addressed_constants (tree, int);
115 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
116 unsigned int, bool, bool);
117 static void globalize_decl (tree);
118 static bool decl_readonly_section_1 (enum section_category);
119 #ifdef BSS_SECTION_ASM_OP
120 #ifdef ASM_OUTPUT_ALIGNED_BSS
121 static void asm_output_aligned_bss (FILE *, tree, const char *,
122 unsigned HOST_WIDE_INT, int)
123 ATTRIBUTE_UNUSED;
124 #endif
125 #endif /* BSS_SECTION_ASM_OP */
126 static void mark_weak (tree);
127 static void output_constant_pool (const char *, tree);
128 static void handle_vtv_comdat_section (section *, const_tree);
130 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
131 section *text_section;
132 section *data_section;
133 section *readonly_data_section;
134 section *sdata_section;
135 section *ctors_section;
136 section *dtors_section;
137 section *bss_section;
138 section *sbss_section;
140 /* Various forms of common section. All are guaranteed to be nonnull. */
141 section *tls_comm_section;
142 section *comm_section;
143 section *lcomm_section;
145 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
146 May be null. */
147 section *bss_noswitch_section;
149 /* The section that holds the main exception table, when known. The section
150 is set either by the target's init_sections hook or by the first call to
151 switch_to_exception_section. */
152 section *exception_section;
154 /* The section that holds the DWARF2 frame unwind information, when known.
155 The section is set either by the target's init_sections hook or by the
156 first call to switch_to_eh_frame_section. */
157 section *eh_frame_section;
159 /* asm_out_file's current section. This is NULL if no section has yet
160 been selected or if we lose track of what the current section is. */
161 section *in_section;
163 /* True if code for the current function is currently being directed
164 at the cold section. */
165 bool in_cold_section_p;
167 /* The following global holds the "function name" for the code in the
168 cold section of a function, if hot/cold function splitting is enabled
169 and there was actually code that went into the cold section. A
170 pseudo function name is needed for the cold section of code for some
171 debugging tools that perform symbolization. */
172 tree cold_function_name = NULL_TREE;
174 /* A linked list of all the unnamed sections. */
175 static GTY(()) section *unnamed_sections;
177 /* Return a nonzero value if DECL has a section attribute. */
178 #define IN_NAMED_SECTION(DECL) \
179 (VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
181 struct section_hasher : ggc_ptr_hash<section>
183 typedef const char *compare_type;
185 static hashval_t hash (section *);
186 static bool equal (section *, const char *);
189 /* Hash table of named sections. */
190 static GTY(()) hash_table<section_hasher> *section_htab;
192 struct object_block_hasher : ggc_ptr_hash<object_block>
194 typedef const section *compare_type;
196 static hashval_t hash (object_block *);
197 static bool equal (object_block *, const section *);
200 /* A table of object_blocks, indexed by section. */
201 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
203 /* The next number to use for internal anchor labels. */
204 static GTY(()) int anchor_labelno;
206 /* A pool of constants that can be shared between functions. */
207 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
209 /* Helper routines for maintaining section_htab. */
211 bool
212 section_hasher::equal (section *old, const char *new_name)
214 return strcmp (old->named.name, new_name) == 0;
217 hashval_t
218 section_hasher::hash (section *old)
220 return htab_hash_string (old->named.name);
223 /* Return a hash value for section SECT. */
225 static hashval_t
226 hash_section (section *sect)
228 if (sect->common.flags & SECTION_NAMED)
229 return htab_hash_string (sect->named.name);
230 return sect->common.flags & ~SECTION_DECLARED;
233 /* Helper routines for maintaining object_block_htab. */
235 inline bool
236 object_block_hasher::equal (object_block *old, const section *new_section)
238 return old->sect == new_section;
241 hashval_t
242 object_block_hasher::hash (object_block *old)
244 return hash_section (old->sect);
247 /* Return a new unnamed section with the given fields. */
249 section *
250 get_unnamed_section (unsigned int flags, void (*callback) (const char *),
251 const char *data)
253 section *sect;
255 sect = ggc_alloc<section> ();
256 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
257 sect->unnamed.callback = callback;
258 sect->unnamed.data = data;
259 sect->unnamed.next = unnamed_sections;
261 unnamed_sections = sect;
262 return sect;
265 /* Return a SECTION_NOSWITCH section with the given fields. */
267 static section *
268 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
270 section *sect;
272 sect = ggc_alloc<section> ();
273 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
274 sect->noswitch.callback = callback;
276 return sect;
279 /* Return the named section structure associated with NAME. Create
280 a new section with the given fields if no such structure exists.
281 When NOT_EXISTING, then fail if the section already exists. Return
282 the existing section if the SECTION_RETAIN bit doesn't match. Set
283 the SECTION_WRITE | SECTION_RELRO bits on the existing section
284 if one of the section flags is SECTION_WRITE | SECTION_RELRO and the
285 other has none of these flags in named sections and either the section
286 hasn't been declared yet or has been declared as writable. */
288 section *
289 get_section (const char *name, unsigned int flags, tree decl,
290 bool not_existing)
292 section *sect, **slot;
294 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
295 INSERT);
296 flags |= SECTION_NAMED;
297 if (decl != nullptr
298 && DECL_P (decl)
299 && lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
300 flags |= SECTION_RETAIN;
301 if (*slot == NULL)
303 sect = ggc_alloc<section> ();
304 sect->named.common.flags = flags;
305 sect->named.name = ggc_strdup (name);
306 sect->named.decl = decl;
307 *slot = sect;
309 else
311 if (not_existing)
312 internal_error ("section already exists: %qs", name);
314 sect = *slot;
315 /* It is fine if one of the sections has SECTION_NOTYPE as long as
316 the other has none of the contrary flags (see the logic at the end
317 of default_section_type_flags, below). */
318 if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
319 && !((sect->common.flags | flags)
320 & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
321 | (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
323 sect->common.flags |= SECTION_NOTYPE;
324 flags |= SECTION_NOTYPE;
326 if ((sect->common.flags & ~SECTION_DECLARED) != flags
327 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
329 /* It is fine if one of the section flags is
330 SECTION_WRITE | SECTION_RELRO and the other has none of these
331 flags (i.e. read-only) in named sections and either the
332 section hasn't been declared yet or has been declared as writable.
333 In that case just make sure the resulting flags are
334 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
335 relocations. */
336 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
337 == (SECTION_WRITE | SECTION_RELRO)
338 && (sect->common.flags
339 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
340 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
341 && ((sect->common.flags & SECTION_DECLARED) == 0
342 || (sect->common.flags & SECTION_WRITE)))
344 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
345 return sect;
347 /* If the SECTION_RETAIN bit doesn't match, return and switch
348 to a new section later. */
349 if ((sect->common.flags & SECTION_RETAIN)
350 != (flags & SECTION_RETAIN))
351 return sect;
352 /* Sanity check user variables for flag changes. */
353 if (sect->named.decl != NULL
354 && DECL_P (sect->named.decl)
355 && decl != sect->named.decl)
357 if (decl != NULL && DECL_P (decl))
358 error ("%+qD causes a section type conflict with %qD",
359 decl, sect->named.decl);
360 else
361 error ("section type conflict with %qD", sect->named.decl);
362 inform (DECL_SOURCE_LOCATION (sect->named.decl),
363 "%qD was declared here", sect->named.decl);
365 else if (decl != NULL && DECL_P (decl))
366 error ("%+qD causes a section type conflict", decl);
367 else
368 error ("section type conflict");
369 /* Make sure we don't error about one section multiple times. */
370 sect->common.flags |= SECTION_OVERRIDE;
373 return sect;
376 /* Return true if the current compilation mode benefits from having
377 objects grouped into blocks. */
379 static bool
380 use_object_blocks_p (void)
382 return flag_section_anchors;
385 /* Return the object_block structure for section SECT. Create a new
386 structure if we haven't created one already. Return null if SECT
387 itself is null. Return also null for mergeable sections since
388 section anchors can't be used in mergeable sections anyway,
389 because the linker might move objects around, and using the
390 object blocks infrastructure in that case is both a waste and a
391 maintenance burden. */
393 static struct object_block *
394 get_block_for_section (section *sect)
396 struct object_block *block;
398 if (sect == NULL)
399 return NULL;
401 if (sect->common.flags & SECTION_MERGE)
402 return NULL;
404 object_block **slot
405 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
406 INSERT);
407 block = *slot;
408 if (block == NULL)
410 block = ggc_cleared_alloc<object_block> ();
411 block->sect = sect;
412 *slot = block;
414 return block;
417 /* Create a symbol with label LABEL and place it at byte offset
418 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
419 is not yet known. LABEL must be a garbage-collected string. */
421 static rtx
422 create_block_symbol (const char *label, struct object_block *block,
423 HOST_WIDE_INT offset)
425 rtx symbol;
426 unsigned int size;
428 /* Create the extended SYMBOL_REF. */
429 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
430 symbol = (rtx) ggc_internal_alloc (size);
432 /* Initialize the normal SYMBOL_REF fields. */
433 memset (symbol, 0, size);
434 PUT_CODE (symbol, SYMBOL_REF);
435 PUT_MODE (symbol, Pmode);
436 XSTR (symbol, 0) = label;
437 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
439 /* Initialize the block_symbol stuff. */
440 SYMBOL_REF_BLOCK (symbol) = block;
441 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
443 return symbol;
446 /* Return a section with a particular name and with whatever SECTION_*
447 flags section_type_flags deems appropriate. The name of the section
448 is taken from NAME if nonnull, otherwise it is taken from DECL's
449 DECL_SECTION_NAME. DECL is the decl associated with the section
450 (see the section comment for details) and RELOC is as for
451 section_type_flags. */
453 section *
454 get_named_section (tree decl, const char *name, int reloc)
456 unsigned int flags;
458 if (name == NULL)
460 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
461 name = DECL_SECTION_NAME (decl);
464 flags = targetm.section_type_flags (decl, name, reloc);
465 return get_section (name, flags, decl);
468 /* Worker for resolve_unique_section. */
470 static bool
471 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
473 n->implicit_section = true;
474 return false;
477 /* If required, set DECL_SECTION_NAME to a unique name. */
479 void
480 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
481 int flag_function_or_data_sections)
483 if (DECL_SECTION_NAME (decl) == NULL
484 && targetm_common.have_named_sections
485 && (flag_function_or_data_sections
486 || lookup_attribute ("retain", DECL_ATTRIBUTES (decl))
487 || DECL_COMDAT_GROUP (decl)))
489 targetm.asm_out.unique_section (decl, reloc);
490 if (DECL_SECTION_NAME (decl))
491 symtab_node::get (decl)->call_for_symbol_and_aliases
492 (set_implicit_section, NULL, true);
496 #ifdef BSS_SECTION_ASM_OP
498 #ifdef ASM_OUTPUT_ALIGNED_BSS
500 /* Utility function for targets to use in implementing
501 ASM_OUTPUT_ALIGNED_BSS.
502 ??? It is believed that this function will work in most cases so such
503 support is localized here. */
505 static void
506 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
507 const char *name, unsigned HOST_WIDE_INT size,
508 int align)
510 switch_to_section (bss_section);
511 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
512 #ifdef ASM_DECLARE_OBJECT_NAME
513 last_assemble_variable_decl = decl;
514 ASM_DECLARE_OBJECT_NAME (file, name, decl);
515 #else
516 /* Standard thing is just output label for the object. */
517 ASM_OUTPUT_LABEL (file, name);
518 #endif /* ASM_DECLARE_OBJECT_NAME */
519 ASM_OUTPUT_SKIP (file, size ? size : 1);
522 #endif
524 #endif /* BSS_SECTION_ASM_OP */
526 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
527 /* Return the hot section for function DECL. Return text_section for
528 null DECLs. */
530 static section *
531 hot_function_section (tree decl)
533 if (decl != NULL_TREE
534 && DECL_SECTION_NAME (decl) != NULL
535 && targetm_common.have_named_sections)
536 return get_named_section (decl, NULL, 0);
537 else
538 return text_section;
540 #endif
542 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
543 is NULL.
545 When DECL_SECTION_NAME is non-NULL and it is implicit section and
546 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
547 concatenate the name with NAMED_SECTION_SUFFIX.
548 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
550 section *
551 get_named_text_section (tree decl,
552 const char *text_section_name,
553 const char *named_section_suffix)
555 if (decl && DECL_SECTION_NAME (decl))
557 if (named_section_suffix)
559 const char *dsn = DECL_SECTION_NAME (decl);
560 const char *stripped_name;
561 char *name, *buffer;
563 name = (char *) alloca (strlen (dsn) + 1);
564 memcpy (name, dsn,
565 strlen (dsn) + 1);
567 stripped_name = targetm.strip_name_encoding (name);
569 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
570 return get_named_section (decl, buffer, 0);
572 else if (symtab_node::get (decl)->implicit_section)
574 const char *name;
576 /* Do not try to split gnu_linkonce functions. This gets somewhat
577 slipperly. */
578 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
579 return NULL;
580 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
581 name = targetm.strip_name_encoding (name);
582 return get_named_section (decl, ACONCAT ((text_section_name, ".",
583 name, NULL)), 0);
585 else
586 return NULL;
588 return get_named_section (decl, text_section_name, 0);
591 /* Choose named function section based on its frequency. */
593 section *
594 default_function_section (tree decl, enum node_frequency freq,
595 bool startup, bool exit)
597 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
598 /* Old GNU linkers have buggy --gc-section support, which sometimes
599 results in .gcc_except_table* sections being garbage collected. */
600 if (decl
601 && symtab_node::get (decl)->implicit_section)
602 return NULL;
603 #endif
605 if (!flag_reorder_functions
606 || !targetm_common.have_named_sections)
607 return NULL;
608 /* Startup code should go to startup subsection unless it is
609 unlikely executed (this happens especially with function splitting
610 where we can split away unnecessary parts of static constructors. */
611 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
613 /* During LTO the tp_first_run profiling will naturally place all
614 initialization code first. Using separate section is counter-productive
615 because startup only code may call functions which are no longer
616 startup only. */
617 if (!in_lto_p
618 || !cgraph_node::get (decl)->tp_first_run
619 || !opt_for_fn (decl, flag_profile_reorder_functions))
620 return get_named_text_section (decl, ".text.startup", NULL);
621 else
622 return NULL;
625 /* Similarly for exit. */
626 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
627 return get_named_text_section (decl, ".text.exit", NULL);
629 /* Group cold functions together, similarly for hot code. */
630 switch (freq)
632 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
633 return get_named_text_section (decl, ".text.unlikely", NULL);
634 case NODE_FREQUENCY_HOT:
635 return get_named_text_section (decl, ".text.hot", NULL);
636 /* FALLTHRU */
637 default:
638 return NULL;
642 /* Return the section for function DECL.
644 If DECL is NULL_TREE, return the text section. We can be passed
645 NULL_TREE under some circumstances by dbxout.cc at least.
647 If FORCE_COLD is true, return cold function section ignoring
648 the frequency info of cgraph_node. */
650 static section *
651 function_section_1 (tree decl, bool force_cold)
653 section *section = NULL;
654 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
655 bool startup = false, exit = false;
657 if (decl)
659 struct cgraph_node *node = cgraph_node::get (decl);
661 if (node)
663 freq = node->frequency;
664 startup = node->only_called_at_startup;
665 exit = node->only_called_at_exit;
668 if (force_cold)
669 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
671 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
672 if (decl != NULL_TREE
673 && DECL_SECTION_NAME (decl) != NULL)
675 if (targetm.asm_out.function_section)
676 section = targetm.asm_out.function_section (decl, freq,
677 startup, exit);
678 if (section)
679 return section;
680 return get_named_section (decl, NULL, 0);
682 else
683 return targetm.asm_out.select_section
684 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
685 symtab_node::get (decl)->definition_alignment ());
686 #else
687 if (targetm.asm_out.function_section)
688 section = targetm.asm_out.function_section (decl, freq, startup, exit);
689 if (section)
690 return section;
691 return hot_function_section (decl);
692 #endif
695 /* Return the section for function DECL.
697 If DECL is NULL_TREE, return the text section. We can be passed
698 NULL_TREE under some circumstances by dbxout.cc at least. */
700 section *
701 function_section (tree decl)
703 /* Handle cases where function splitting code decides
704 to put function entry point into unlikely executed section
705 despite the fact that the function itself is not cold
706 (i.e. it is called rarely but contains a hot loop that is
707 better to live in hot subsection for the code locality). */
708 return function_section_1 (decl,
709 first_function_block_is_cold);
712 /* Return the section for the current function, take IN_COLD_SECTION_P
713 into account. */
715 section *
716 current_function_section (void)
718 return function_section_1 (current_function_decl, in_cold_section_p);
721 /* Tell assembler to switch to unlikely-to-be-executed text section. */
723 section *
724 unlikely_text_section (void)
726 return function_section_1 (current_function_decl, true);
729 /* When called within a function context, return true if the function
730 has been assigned a cold text section and if SECT is that section.
731 When called outside a function context, return true if SECT is the
732 default cold section. */
734 bool
735 unlikely_text_section_p (section *sect)
737 return sect == function_section_1 (current_function_decl, true);
740 /* Switch to the other function partition (if inside of hot section
741 into cold section, otherwise into the hot section). */
743 void
744 switch_to_other_text_partition (void)
746 in_cold_section_p = !in_cold_section_p;
747 switch_to_section (current_function_section ());
750 /* Return the read-only or relocated read-only data section
751 associated with function DECL. */
753 section *
754 default_function_rodata_section (tree decl, bool relocatable)
756 const char* sname;
757 unsigned int flags;
759 flags = 0;
761 if (relocatable)
763 sname = ".data.rel.ro.local";
764 flags = (SECTION_WRITE | SECTION_RELRO);
766 else
767 sname = ".rodata";
769 if (decl && DECL_SECTION_NAME (decl))
771 const char *name = DECL_SECTION_NAME (decl);
773 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
775 const char *dot;
776 size_t len;
777 char* rname;
779 dot = strchr (name + 1, '.');
780 if (!dot)
781 dot = name;
782 len = strlen (dot) + strlen (sname) + 1;
783 rname = (char *) alloca (len);
785 strcpy (rname, sname);
786 strcat (rname, dot);
787 return get_section (rname, (SECTION_LINKONCE | flags), decl);
789 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo or
790 .gnu.linkonce.d.rel.ro.local.foo if the jump table is relocatable. */
791 else if (DECL_COMDAT_GROUP (decl)
792 && startswith (name, ".gnu.linkonce.t."))
794 size_t len;
795 char *rname;
797 if (relocatable)
799 len = strlen (name) + strlen (".rel.ro.local") + 1;
800 rname = (char *) alloca (len);
802 strcpy (rname, ".gnu.linkonce.d.rel.ro.local");
803 strcat (rname, name + 15);
805 else
807 len = strlen (name) + 1;
808 rname = (char *) alloca (len);
810 memcpy (rname, name, len);
811 rname[14] = 'r';
813 return get_section (rname, (SECTION_LINKONCE | flags), decl);
815 /* For .text.foo we want to use .rodata.foo. */
816 else if (flag_function_sections && flag_data_sections
817 && startswith (name, ".text."))
819 size_t len = strlen (name) + 1;
820 char *rname = (char *) alloca (len + strlen (sname) - 5);
822 memcpy (rname, sname, strlen (sname));
823 memcpy (rname + strlen (sname), name + 5, len - 5);
824 return get_section (rname, flags, decl);
828 if (relocatable)
829 return get_section (sname, flags, decl);
830 else
831 return readonly_data_section;
834 /* Return the read-only data section associated with function DECL
835 for targets where that section should be always the single
836 readonly data section. */
838 section *
839 default_no_function_rodata_section (tree, bool)
841 return readonly_data_section;
844 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
846 static const char *
847 function_mergeable_rodata_prefix (void)
849 section *s = targetm.asm_out.function_rodata_section (current_function_decl,
850 false);
851 if (SECTION_STYLE (s) == SECTION_NAMED)
852 return s->named.name;
853 else
854 return targetm.asm_out.mergeable_rodata_prefix;
857 /* Return the section to use for string merging. */
859 static section *
860 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
861 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
862 unsigned int flags ATTRIBUTE_UNUSED)
864 HOST_WIDE_INT len;
866 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
867 && TREE_CODE (decl) == STRING_CST
868 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
869 && align <= 256
870 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
871 && TREE_STRING_LENGTH (decl) == len)
873 scalar_int_mode mode;
874 unsigned int modesize;
875 const char *str;
876 HOST_WIDE_INT i;
877 int j, unit;
878 const char *prefix = function_mergeable_rodata_prefix ();
879 char *name = (char *) alloca (strlen (prefix) + 30);
881 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
882 modesize = GET_MODE_BITSIZE (mode);
883 if (modesize >= 8 && modesize <= 256
884 && (modesize & (modesize - 1)) == 0)
886 if (align < modesize)
887 align = modesize;
889 if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
890 return readonly_data_section;
892 str = TREE_STRING_POINTER (decl);
893 unit = GET_MODE_SIZE (mode);
895 /* Check for embedded NUL characters. */
896 for (i = 0; i < len; i += unit)
898 for (j = 0; j < unit; j++)
899 if (str[i + j] != '\0')
900 break;
901 if (j == unit)
902 break;
904 if (i == len - unit || (unit == 1 && i == len))
906 sprintf (name, "%s.str%d.%d", prefix,
907 modesize / 8, (int) (align / 8));
908 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
909 return get_section (name, flags, NULL);
914 return readonly_data_section;
917 /* Return the section to use for constant merging. */
919 section *
920 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
921 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
922 unsigned int flags ATTRIBUTE_UNUSED)
924 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
925 && mode != VOIDmode
926 && mode != BLKmode
927 && known_le (GET_MODE_BITSIZE (mode), align)
928 && align >= 8
929 && align <= 256
930 && (align & (align - 1)) == 0
931 && (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
933 const char *prefix = function_mergeable_rodata_prefix ();
934 char *name = (char *) alloca (strlen (prefix) + 30);
936 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
937 flags |= (align / 8) | SECTION_MERGE;
938 return get_section (name, flags, NULL);
940 return readonly_data_section;
943 /* Given NAME, a putative register name, discard any customary prefixes. */
945 static const char *
946 strip_reg_name (const char *name)
948 #ifdef REGISTER_PREFIX
949 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
950 name += strlen (REGISTER_PREFIX);
951 #endif
952 if (name[0] == '%' || name[0] == '#')
953 name++;
954 return name;
957 /* The user has asked for a DECL to have a particular name. Set (or
958 change) it in such a way that we don't prefix an underscore to
959 it. */
960 void
961 set_user_assembler_name (tree decl, const char *name)
963 char *starred = (char *) alloca (strlen (name) + 2);
964 starred[0] = '*';
965 strcpy (starred + 1, name);
966 symtab->change_decl_assembler_name (decl, get_identifier (starred));
967 SET_DECL_RTL (decl, NULL_RTX);
970 /* Decode an `asm' spec for a declaration as a register name.
971 Return the register number, or -1 if nothing specified,
972 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
973 or -3 if ASMSPEC is `cc' and is not recognized,
974 or -4 if ASMSPEC is `memory' and is not recognized.
975 Accept an exact spelling or a decimal number.
976 Prefixes such as % are optional. */
979 decode_reg_name_and_count (const char *asmspec, int *pnregs)
981 /* Presume just one register is clobbered. */
982 *pnregs = 1;
984 if (asmspec != 0)
986 int i;
988 /* Get rid of confusing prefixes. */
989 asmspec = strip_reg_name (asmspec);
991 /* Allow a decimal number as a "register name". */
992 for (i = strlen (asmspec) - 1; i >= 0; i--)
993 if (! ISDIGIT (asmspec[i]))
994 break;
995 if (asmspec[0] != 0 && i < 0)
997 i = atoi (asmspec);
998 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
999 return i;
1000 else
1001 return -2;
1004 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1005 if (reg_names[i][0]
1006 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
1007 return i;
1009 #ifdef OVERLAPPING_REGISTER_NAMES
1011 static const struct
1013 const char *const name;
1014 const int number;
1015 const int nregs;
1016 } table[] = OVERLAPPING_REGISTER_NAMES;
1018 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1019 if (table[i].name[0]
1020 && ! strcmp (asmspec, table[i].name))
1022 *pnregs = table[i].nregs;
1023 return table[i].number;
1026 #endif /* OVERLAPPING_REGISTER_NAMES */
1028 #ifdef ADDITIONAL_REGISTER_NAMES
1030 static const struct { const char *const name; const int number; } table[]
1031 = ADDITIONAL_REGISTER_NAMES;
1033 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
1034 if (table[i].name[0]
1035 && ! strcmp (asmspec, table[i].name)
1036 && reg_names[table[i].number][0])
1037 return table[i].number;
1039 #endif /* ADDITIONAL_REGISTER_NAMES */
1041 if (!strcmp (asmspec, "memory"))
1042 return -4;
1044 if (!strcmp (asmspec, "cc"))
1045 return -3;
1047 return -2;
1050 return -1;
1054 decode_reg_name (const char *name)
1056 int count;
1057 return decode_reg_name_and_count (name, &count);
1061 /* Return true if DECL's initializer is suitable for a BSS section. */
1063 bool
1064 bss_initializer_p (const_tree decl, bool named)
1066 /* Do not put non-common constants into the .bss section, they belong in
1067 a readonly section, except when NAMED is true. */
1068 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1069 && (DECL_INITIAL (decl) == NULL
1070 /* In LTO we have no errors in program; error_mark_node is used
1071 to mark offlined constructors. */
1072 || (DECL_INITIAL (decl) == error_mark_node
1073 && !in_lto_p)
1074 || (flag_zero_initialized_in_bss
1075 && initializer_zerop (DECL_INITIAL (decl))
1076 /* A decl with the "persistent" attribute applied and
1077 explicitly initialized to 0 should not be treated as a BSS
1078 variable. */
1079 && !DECL_PERSISTENT_P (decl))));
1082 /* Compute the alignment of variable specified by DECL.
1083 DONT_OUTPUT_DATA is from assemble_variable. */
1085 void
1086 align_variable (tree decl, bool dont_output_data)
1088 unsigned int align = DECL_ALIGN (decl);
1090 /* In the case for initialing an array whose length isn't specified,
1091 where we have not yet been able to do the layout,
1092 figure out the proper alignment now. */
1093 if (dont_output_data && DECL_SIZE (decl) == 0
1094 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1095 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1097 /* Some object file formats have a maximum alignment which they support.
1098 In particular, a.out format supports a maximum alignment of 4. */
1099 if (align > MAX_OFILE_ALIGNMENT)
1101 error ("alignment of %q+D is greater than maximum object "
1102 "file alignment %d", decl,
1103 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1104 align = MAX_OFILE_ALIGNMENT;
1107 if (! DECL_USER_ALIGN (decl))
1109 #ifdef DATA_ABI_ALIGNMENT
1110 unsigned int data_abi_align
1111 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1112 /* For backwards compatibility, don't assume the ABI alignment for
1113 TLS variables. */
1114 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1115 align = data_abi_align;
1116 #endif
1118 /* On some machines, it is good to increase alignment sometimes.
1119 But as DECL_ALIGN is used both for actually emitting the variable
1120 and for code accessing the variable as guaranteed alignment, we
1121 can only increase the alignment if it is a performance optimization
1122 if the references to it must bind to the current definition. */
1123 if (decl_binds_to_current_def_p (decl)
1124 && !DECL_VIRTUAL_P (decl))
1126 #ifdef DATA_ALIGNMENT
1127 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1128 /* Don't increase alignment too much for TLS variables - TLS space
1129 is too precious. */
1130 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1131 align = data_align;
1132 #endif
1133 if (DECL_INITIAL (decl) != 0
1134 /* In LTO we have no errors in program; error_mark_node is used
1135 to mark offlined constructors. */
1136 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1138 unsigned int const_align
1139 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1140 /* Don't increase alignment too much for TLS variables - TLS
1141 space is too precious. */
1142 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1143 align = const_align;
1148 /* Reset the alignment in case we have made it tighter, so we can benefit
1149 from it in get_pointer_alignment. */
1150 SET_DECL_ALIGN (decl, align);
1153 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1154 beyond what align_variable returned. */
1156 static unsigned int
1157 get_variable_align (tree decl)
1159 unsigned int align = DECL_ALIGN (decl);
1161 /* For user aligned vars or static vars align_variable already did
1162 everything. */
1163 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1164 return align;
1166 #ifdef DATA_ABI_ALIGNMENT
1167 if (DECL_THREAD_LOCAL_P (decl))
1168 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1169 #endif
1171 /* For decls that bind to the current definition, align_variable
1172 did also everything, except for not assuming ABI required alignment
1173 of TLS variables. For other vars, increase the alignment here
1174 as an optimization. */
1175 if (!decl_binds_to_current_def_p (decl))
1177 /* On some machines, it is good to increase alignment sometimes. */
1178 #ifdef DATA_ALIGNMENT
1179 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1180 /* Don't increase alignment too much for TLS variables - TLS space
1181 is too precious. */
1182 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1183 align = data_align;
1184 #endif
1185 if (DECL_INITIAL (decl) != 0
1186 /* In LTO we have no errors in program; error_mark_node is used
1187 to mark offlined constructors. */
1188 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1190 unsigned int const_align
1191 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1192 /* Don't increase alignment too much for TLS variables - TLS space
1193 is too precious. */
1194 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1195 align = const_align;
1199 return align;
1202 /* Compute reloc for get_variable_section. The return value
1203 is a mask for which bit 1 indicates a global relocation, and bit 0
1204 indicates a local relocation. */
1207 compute_reloc_for_var (tree decl)
1209 int reloc;
1211 if (DECL_INITIAL (decl) == error_mark_node)
1212 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1213 else if (DECL_INITIAL (decl))
1214 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1215 else
1216 reloc = 0;
1218 return reloc;
1221 /* Return the section into which the given VAR_DECL or CONST_DECL
1222 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1223 section should be used wherever possible. */
1225 section *
1226 get_variable_section (tree decl, bool prefer_noswitch_p)
1228 addr_space_t as = ADDR_SPACE_GENERIC;
1229 int reloc;
1230 varpool_node *vnode = varpool_node::get (decl);
1231 if (vnode)
1233 vnode = vnode->ultimate_alias_target ();
1234 decl = vnode->decl;
1237 if (TREE_TYPE (decl) != error_mark_node)
1238 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1240 /* We need the constructor to figure out reloc flag. */
1241 if (vnode)
1242 vnode->get_constructor ();
1244 if (DECL_COMMON (decl)
1245 && !lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1247 /* If the decl has been given an explicit section name, or it resides
1248 in a non-generic address space, then it isn't common, and shouldn't
1249 be handled as such. */
1250 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1251 && ADDR_SPACE_GENERIC_P (as));
1252 if (DECL_THREAD_LOCAL_P (decl))
1253 return tls_comm_section;
1254 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1255 return comm_section;
1258 reloc = compute_reloc_for_var (decl);
1260 resolve_unique_section (decl, reloc, flag_data_sections);
1261 if (IN_NAMED_SECTION (decl))
1263 section *sect = get_named_section (decl, NULL, reloc);
1265 if ((sect->common.flags & SECTION_BSS)
1266 && !bss_initializer_p (decl, true))
1268 error_at (DECL_SOURCE_LOCATION (decl),
1269 "only zero initializers are allowed in section %qs",
1270 sect->named.name);
1271 DECL_INITIAL (decl) = error_mark_node;
1273 return sect;
1276 if (ADDR_SPACE_GENERIC_P (as)
1277 && !DECL_THREAD_LOCAL_P (decl)
1278 && !DECL_NOINIT_P (decl)
1279 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1280 && bss_initializer_p (decl))
1282 if (!TREE_PUBLIC (decl)
1283 && !((flag_sanitize & SANITIZE_ADDRESS)
1284 && asan_protect_global (decl)))
1285 return lcomm_section;
1286 if (bss_noswitch_section)
1287 return bss_noswitch_section;
1290 return targetm.asm_out.select_section (decl, reloc,
1291 get_variable_align (decl));
1294 /* Return the block into which object_block DECL should be placed. */
1296 static struct object_block *
1297 get_block_for_decl (tree decl)
1299 section *sect;
1301 if (VAR_P (decl))
1303 /* The object must be defined in this translation unit. */
1304 if (DECL_EXTERNAL (decl))
1305 return NULL;
1307 /* There's no point using object blocks for something that is
1308 isolated by definition. */
1309 if (DECL_COMDAT_GROUP (decl))
1310 return NULL;
1313 /* We can only calculate block offsets if the decl has a known
1314 constant size. */
1315 if (DECL_SIZE_UNIT (decl) == NULL)
1316 return NULL;
1317 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1318 return NULL;
1320 /* Find out which section should contain DECL. We cannot put it into
1321 an object block if it requires a standalone definition. */
1322 if (VAR_P (decl))
1323 align_variable (decl, 0);
1324 sect = get_variable_section (decl, true);
1325 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1326 return NULL;
1328 if (bool (lookup_attribute ("retain", DECL_ATTRIBUTES (decl)))
1329 != bool (sect->common.flags & SECTION_RETAIN))
1330 return NULL;
1332 return get_block_for_section (sect);
1335 /* Make sure block symbol SYMBOL is in block BLOCK. */
1337 static void
1338 change_symbol_block (rtx symbol, struct object_block *block)
1340 if (block != SYMBOL_REF_BLOCK (symbol))
1342 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1343 SYMBOL_REF_BLOCK (symbol) = block;
1347 /* Return true if it is possible to put DECL in an object_block. */
1349 static bool
1350 use_blocks_for_decl_p (tree decl)
1352 struct symtab_node *snode;
1354 /* Don't create object blocks if each DECL is placed into a separate
1355 section because that will uselessly create a section anchor for
1356 each DECL. */
1357 if (flag_data_sections)
1358 return false;
1360 /* Only data DECLs can be placed into object blocks. */
1361 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1362 return false;
1364 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1365 are never used from code directly and we never want object block handling
1366 for those. */
1367 if (DECL_INITIAL (decl) == decl)
1368 return false;
1370 /* If this decl is an alias, then we don't want to emit a
1371 definition. */
1372 if (VAR_P (decl)
1373 && (snode = symtab_node::get (decl)) != NULL
1374 && snode->alias)
1375 return false;
1377 return targetm.use_blocks_for_decl_p (decl);
1380 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1381 until we find an identifier that is not itself a transparent alias.
1382 Modify the alias passed to it by reference (and all aliases on the
1383 way to the ultimate target), such that they do not have to be
1384 followed again, and return the ultimate target of the alias
1385 chain. */
1387 static inline tree
1388 ultimate_transparent_alias_target (tree *alias)
1390 tree target = *alias;
1392 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1394 gcc_assert (TREE_CHAIN (target));
1395 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1396 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1397 && ! TREE_CHAIN (target));
1398 *alias = target;
1401 return target;
1404 /* Return true if REGNUM is mentioned in ELIMINABLE_REGS as a from
1405 register number. */
1407 static bool
1408 eliminable_regno_p (int regnum)
1410 static const struct
1412 const int from;
1413 const int to;
1414 } eliminables[] = ELIMINABLE_REGS;
1415 for (size_t i = 0; i < ARRAY_SIZE (eliminables); i++)
1416 if (regnum == eliminables[i].from)
1417 return true;
1418 return false;
1421 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1422 have static storage duration. In other words, it should not be an
1423 automatic variable, including PARM_DECLs.
1425 There is, however, one exception: this function handles variables
1426 explicitly placed in a particular register by the user.
1428 This is never called for PARM_DECL nodes. */
1430 void
1431 make_decl_rtl (tree decl)
1433 const char *name = 0;
1434 int reg_number;
1435 tree id;
1436 rtx x;
1438 /* Check that we are not being given an automatic variable. */
1439 gcc_assert (TREE_CODE (decl) != PARM_DECL
1440 && TREE_CODE (decl) != RESULT_DECL);
1442 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1443 gcc_assert (!VAR_P (decl)
1444 || TREE_STATIC (decl)
1445 || TREE_PUBLIC (decl)
1446 || DECL_EXTERNAL (decl)
1447 || DECL_REGISTER (decl));
1449 /* And that we were not given a type or a label. */
1450 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1451 && TREE_CODE (decl) != LABEL_DECL);
1453 /* For a duplicate declaration, we can be called twice on the
1454 same DECL node. Don't discard the RTL already made. */
1455 if (DECL_RTL_SET_P (decl))
1457 /* If the old RTL had the wrong mode, fix the mode. */
1458 x = DECL_RTL (decl);
1459 if (GET_MODE (x) != DECL_MODE (decl))
1460 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1462 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1463 return;
1465 /* ??? Another way to do this would be to maintain a hashed
1466 table of such critters. Instead of adding stuff to a DECL
1467 to give certain attributes to it, we could use an external
1468 hash map from DECL to set of attributes. */
1470 /* Let the target reassign the RTL if it wants.
1471 This is necessary, for example, when one machine specific
1472 decl attribute overrides another. */
1473 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1475 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1476 on the new decl information. */
1477 if (MEM_P (x)
1478 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1479 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1480 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1482 return;
1485 /* If this variable belongs to the global constant pool, retrieve the
1486 pre-computed RTL or recompute it in LTO mode. */
1487 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1489 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1490 return;
1493 id = DECL_ASSEMBLER_NAME (decl);
1494 name = IDENTIFIER_POINTER (id);
1496 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1497 && DECL_REGISTER (decl))
1499 error ("register name not specified for %q+D", decl);
1501 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1503 const char *asmspec = name+1;
1504 machine_mode mode = DECL_MODE (decl);
1505 reg_number = decode_reg_name (asmspec);
1506 /* First detect errors in declaring global registers. */
1507 if (reg_number == -1)
1508 error ("register name not specified for %q+D", decl);
1509 else if (reg_number < 0)
1510 error ("invalid register name for %q+D", decl);
1511 else if (mode == BLKmode)
1512 error ("data type of %q+D isn%'t suitable for a register",
1513 decl);
1514 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1515 error ("the register specified for %q+D cannot be accessed"
1516 " by the current target", decl);
1517 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1518 error ("the register specified for %q+D is not general enough"
1519 " to be used as a register variable", decl);
1520 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1521 error ("register specified for %q+D isn%'t suitable for data type",
1522 decl);
1523 else if (reg_number != HARD_FRAME_POINTER_REGNUM
1524 && (reg_number == FRAME_POINTER_REGNUM
1525 #ifdef RETURN_ADDRESS_POINTER_REGNUM
1526 || reg_number == RETURN_ADDRESS_POINTER_REGNUM
1527 #endif
1528 || reg_number == ARG_POINTER_REGNUM)
1529 && eliminable_regno_p (reg_number))
1530 error ("register specified for %q+D is an internal GCC "
1531 "implementation detail", decl);
1532 /* Now handle properly declared static register variables. */
1533 else
1535 int nregs;
1537 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1539 DECL_INITIAL (decl) = 0;
1540 error ("global register variable has initial value");
1542 if (TREE_THIS_VOLATILE (decl))
1543 warning (OPT_Wvolatile_register_var,
1544 "optimization may eliminate reads and/or "
1545 "writes to register variables");
1547 /* If the user specified one of the eliminables registers here,
1548 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1549 confused with that register and be eliminated. This usage is
1550 somewhat suspect... */
1552 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1553 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1554 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1556 if (TREE_STATIC (decl))
1558 /* Make this register global, so not usable for anything
1559 else. */
1560 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1561 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1562 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1563 #endif
1564 nregs = hard_regno_nregs (reg_number, mode);
1565 while (nregs > 0)
1566 globalize_reg (decl, reg_number + --nregs);
1569 /* As a register variable, it has no section. */
1570 return;
1572 /* Avoid internal errors from invalid register
1573 specifications. */
1574 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1575 DECL_HARD_REGISTER (decl) = 0;
1576 /* Also avoid SSA inconsistencies by pretending this is an external
1577 decl now. */
1578 DECL_EXTERNAL (decl) = 1;
1579 return;
1581 /* Now handle ordinary static variables and functions (in memory).
1582 Also handle vars declared register invalidly. */
1583 else if (name[0] == '*')
1585 #ifdef REGISTER_PREFIX
1586 if (strlen (REGISTER_PREFIX) != 0)
1588 reg_number = decode_reg_name (name);
1589 if (reg_number >= 0 || reg_number == -3)
1590 error ("register name given for non-register variable %q+D", decl);
1592 #endif
1595 /* Specifying a section attribute on a variable forces it into a
1596 non-.bss section, and thus it cannot be common. */
1597 /* FIXME: In general this code should not be necessary because
1598 visibility pass is doing the same work. But notice_global_symbol
1599 is called early and it needs to make DECL_RTL to get the name.
1600 we take care of recomputing the DECL_RTL after visibility is changed. */
1601 if (VAR_P (decl)
1602 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1603 && DECL_SECTION_NAME (decl) != NULL
1604 && DECL_INITIAL (decl) == NULL_TREE
1605 && DECL_COMMON (decl))
1606 DECL_COMMON (decl) = 0;
1608 /* Variables can't be both common and weak. */
1609 if (VAR_P (decl) && DECL_WEAK (decl))
1610 DECL_COMMON (decl) = 0;
1612 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1613 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1614 else
1616 machine_mode address_mode = Pmode;
1617 if (TREE_TYPE (decl) != error_mark_node)
1619 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1620 address_mode = targetm.addr_space.address_mode (as);
1622 x = gen_rtx_SYMBOL_REF (address_mode, name);
1624 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1625 SET_SYMBOL_REF_DECL (x, decl);
1627 x = gen_rtx_MEM (DECL_MODE (decl), x);
1628 if (TREE_CODE (decl) != FUNCTION_DECL)
1629 set_mem_attributes (x, decl, 1);
1630 SET_DECL_RTL (decl, x);
1632 /* Optionally set flags or add text to the name to record information
1633 such as that it is a function name.
1634 If the name is changed, the macro ASM_OUTPUT_LABELREF
1635 will have to know how to strip this information. */
1636 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1639 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1640 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1641 rtl. */
1644 make_decl_rtl_for_debug (tree decl)
1646 unsigned int save_aliasing_flag;
1647 rtx rtl;
1649 if (DECL_RTL_SET_P (decl))
1650 return DECL_RTL (decl);
1652 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1653 call new_alias_set. If running with -fcompare-debug, sometimes
1654 we do not want to create alias sets that will throw the alias
1655 numbers off in the comparison dumps. So... clearing
1656 flag_strict_aliasing will keep new_alias_set() from creating a
1657 new set. */
1658 save_aliasing_flag = flag_strict_aliasing;
1659 flag_strict_aliasing = 0;
1661 rtl = DECL_RTL (decl);
1662 /* Reset DECL_RTL back, as various parts of the compiler expects
1663 DECL_RTL set meaning it is actually going to be output. */
1664 SET_DECL_RTL (decl, NULL);
1666 flag_strict_aliasing = save_aliasing_flag;
1667 return rtl;
1670 /* Output a string of literal assembler code
1671 for an `asm' keyword used between functions. */
1673 void
1674 assemble_asm (tree string)
1676 const char *p;
1677 app_enable ();
1679 if (TREE_CODE (string) == ADDR_EXPR)
1680 string = TREE_OPERAND (string, 0);
1682 p = TREE_STRING_POINTER (string);
1683 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1686 /* Write the address of the entity given by SYMBOL to SEC. */
1687 void
1688 assemble_addr_to_section (rtx symbol, section *sec)
1690 switch_to_section (sec);
1691 assemble_align (POINTER_SIZE);
1692 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1695 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1696 not) section for PRIORITY. */
1697 section *
1698 get_cdtor_priority_section (int priority, bool constructor_p)
1700 /* Buffer conservatively large enough for the full range of a 32-bit
1701 int plus the text below. */
1702 char buf[18];
1704 /* ??? This only works reliably with the GNU linker. */
1705 sprintf (buf, "%s.%.5u",
1706 constructor_p ? ".ctors" : ".dtors",
1707 /* Invert the numbering so the linker puts us in the proper
1708 order; constructors are run from right to left, and the
1709 linker sorts in increasing order. */
1710 MAX_INIT_PRIORITY - priority);
1711 return get_section (buf, SECTION_WRITE, NULL);
1714 void
1715 default_named_section_asm_out_destructor (rtx symbol, int priority)
1717 section *sec;
1719 if (priority != DEFAULT_INIT_PRIORITY)
1720 sec = get_cdtor_priority_section (priority,
1721 /*constructor_p=*/false);
1722 else
1723 sec = get_section (".dtors", SECTION_WRITE, NULL);
1725 assemble_addr_to_section (symbol, sec);
1728 #ifdef DTORS_SECTION_ASM_OP
1729 void
1730 default_dtor_section_asm_out_destructor (rtx symbol,
1731 int priority ATTRIBUTE_UNUSED)
1733 assemble_addr_to_section (symbol, dtors_section);
1735 #endif
1737 void
1738 default_named_section_asm_out_constructor (rtx symbol, int priority)
1740 section *sec;
1742 if (priority != DEFAULT_INIT_PRIORITY)
1743 sec = get_cdtor_priority_section (priority,
1744 /*constructor_p=*/true);
1745 else
1746 sec = get_section (".ctors", SECTION_WRITE, NULL);
1748 assemble_addr_to_section (symbol, sec);
1751 #ifdef CTORS_SECTION_ASM_OP
1752 void
1753 default_ctor_section_asm_out_constructor (rtx symbol,
1754 int priority ATTRIBUTE_UNUSED)
1756 assemble_addr_to_section (symbol, ctors_section);
1758 #endif
1760 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1761 a nonzero value if the constant pool should be output before the
1762 start of the function, or a zero value if the pool should output
1763 after the end of the function. The default is to put it before the
1764 start. */
1766 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1767 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1768 #endif
1770 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1771 to be output to assembler.
1772 Set first_global_object_name and weak_global_object_name as appropriate. */
1774 void
1775 notice_global_symbol (tree decl)
1777 const char **t = &first_global_object_name;
1779 if (first_global_object_name
1780 || !TREE_PUBLIC (decl)
1781 || DECL_EXTERNAL (decl)
1782 || !DECL_NAME (decl)
1783 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1784 || (TREE_CODE (decl) != FUNCTION_DECL
1785 && (!VAR_P (decl)
1786 || (DECL_COMMON (decl)
1787 && (DECL_INITIAL (decl) == 0
1788 || DECL_INITIAL (decl) == error_mark_node)))))
1789 return;
1791 /* We win when global object is found, but it is useful to know about weak
1792 symbol as well so we can produce nicer unique names. */
1793 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1794 t = &weak_global_object_name;
1796 if (!*t)
1798 tree id = DECL_ASSEMBLER_NAME (decl);
1799 ultimate_transparent_alias_target (&id);
1800 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1804 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1805 current function goes into the cold section, so that targets can use
1806 current_function_section during RTL expansion. DECL describes the
1807 function. */
1809 void
1810 decide_function_section (tree decl)
1812 first_function_block_is_cold = false;
1814 if (DECL_SECTION_NAME (decl))
1816 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1817 /* Calls to function_section rely on first_function_block_is_cold
1818 being accurate. */
1819 first_function_block_is_cold = (node
1820 && node->frequency
1821 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1824 in_cold_section_p = first_function_block_is_cold;
1827 /* Get the function's name, as described by its RTL. This may be
1828 different from the DECL_NAME name used in the source file. */
1829 const char *
1830 get_fnname_from_decl (tree decl)
1832 rtx x = DECL_RTL (decl);
1833 gcc_assert (MEM_P (x));
1834 x = XEXP (x, 0);
1835 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1836 return XSTR (x, 0);
1839 /* Output function label, possibly with accompanying metadata. No additional
1840 code or data is output after the label. */
1842 void
1843 assemble_function_label_raw (FILE *file, const char *name)
1845 ASM_OUTPUT_LABEL (file, name);
1846 assemble_function_label_final ();
1849 /* Finish outputting function label. Needs to be called when outputting
1850 function label without using assemble_function_label_raw (). */
1852 void
1853 assemble_function_label_final (void)
1855 if ((flag_sanitize & SANITIZE_ADDRESS)
1856 /* Notify ASAN only about the first function label. */
1857 && (in_cold_section_p == first_function_block_is_cold)
1858 /* Do not notify ASAN when called from, e.g., code_end (). */
1859 && cfun)
1860 asan_function_start ();
1863 /* Output assembler code for the constant pool of a function and associated
1864 with defining the name of the function. DECL describes the function.
1865 NAME is the function's name. For the constant pool, we use the current
1866 constant pool data. */
1868 void
1869 assemble_start_function (tree decl, const char *fnname)
1871 int align;
1872 char tmp_label[100];
1873 bool hot_label_written = false;
1875 if (crtl->has_bb_partition)
1877 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1878 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1879 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1880 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1881 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1882 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1883 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1884 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1885 const_labelno++;
1886 cold_function_name = NULL_TREE;
1888 else
1890 crtl->subsections.hot_section_label = NULL;
1891 crtl->subsections.cold_section_label = NULL;
1892 crtl->subsections.hot_section_end_label = NULL;
1893 crtl->subsections.cold_section_end_label = NULL;
1896 /* The following code does not need preprocessing in the assembler. */
1898 app_disable ();
1900 if (CONSTANT_POOL_BEFORE_FUNCTION)
1901 output_constant_pool (fnname, decl);
1903 align = symtab_node::get (decl)->definition_alignment ();
1905 /* Make sure the not and cold text (code) sections are properly
1906 aligned. This is necessary here in the case where the function
1907 has both hot and cold sections, because we don't want to re-set
1908 the alignment when the section switch happens mid-function. */
1910 if (crtl->has_bb_partition)
1912 first_function_block_is_cold = false;
1914 switch_to_section (unlikely_text_section ());
1915 assemble_align (align);
1916 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1918 /* When the function starts with a cold section, we need to explicitly
1919 align the hot section and write out the hot section label.
1920 But if the current function is a thunk, we do not have a CFG. */
1921 if (!cfun->is_thunk
1922 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1924 switch_to_section (text_section);
1925 assemble_align (align);
1926 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1927 hot_label_written = true;
1928 first_function_block_is_cold = true;
1930 in_cold_section_p = first_function_block_is_cold;
1934 /* Switch to the correct text section for the start of the function. */
1936 switch_to_section (function_section (decl), decl);
1937 if (crtl->has_bb_partition && !hot_label_written)
1938 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1940 /* Tell assembler to move to target machine's alignment for functions. */
1941 align = floor_log2 (align / BITS_PER_UNIT);
1942 if (align > 0)
1944 ASM_OUTPUT_ALIGN (asm_out_file, align);
1947 /* Handle a user-specified function alignment.
1948 Note that we still need to align to DECL_ALIGN, as above,
1949 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1950 if (! DECL_USER_ALIGN (decl)
1951 && align_functions.levels[0].log > align
1952 && optimize_function_for_speed_p (cfun))
1954 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1955 int align_log = align_functions.levels[0].log;
1956 #endif
1957 int max_skip = align_functions.levels[0].maxskip;
1958 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1959 && max_skip >= crtl->max_insn_address)
1960 max_skip = crtl->max_insn_address - 1;
1962 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1963 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1964 if (max_skip == align_functions.levels[0].maxskip)
1965 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1966 align_functions.levels[1].log,
1967 align_functions.levels[1].maxskip);
1968 #else
1969 ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
1970 #endif
1973 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1974 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1975 #endif
1977 if (!DECL_IGNORED_P (decl))
1978 (*debug_hooks->begin_function) (decl);
1980 /* Make function name accessible from other files, if appropriate. */
1982 if (TREE_PUBLIC (decl))
1984 notice_global_symbol (decl);
1986 globalize_decl (decl);
1988 maybe_assemble_visibility (decl);
1991 if (DECL_PRESERVE_P (decl))
1992 targetm.asm_out.mark_decl_preserved (fnname);
1994 unsigned short patch_area_size = crtl->patch_area_size;
1995 unsigned short patch_area_entry = crtl->patch_area_entry;
1997 /* Emit the patching area before the entry label, if any. */
1998 if (patch_area_entry > 0)
1999 targetm.asm_out.print_patchable_function_entry (asm_out_file,
2000 patch_area_entry, true);
2002 /* Do any machine/system dependent processing of the function name. */
2003 #ifdef ASM_DECLARE_FUNCTION_NAME
2004 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
2005 #else
2006 /* Standard thing is just output label for the function. */
2007 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
2008 #endif /* ASM_DECLARE_FUNCTION_NAME */
2010 /* And the area after the label. Record it if we haven't done so yet. */
2011 if (patch_area_size > patch_area_entry)
2012 targetm.asm_out.print_patchable_function_entry (asm_out_file,
2013 patch_area_size
2014 - patch_area_entry,
2015 patch_area_entry == 0);
2017 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
2018 saw_no_split_stack = true;
2021 /* Output assembler code associated with defining the size of the
2022 function. DECL describes the function. NAME is the function's name. */
2024 void
2025 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
2027 #ifdef ASM_DECLARE_FUNCTION_SIZE
2028 /* We could have switched section in the middle of the function. */
2029 if (crtl->has_bb_partition)
2030 switch_to_section (function_section (decl));
2031 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
2032 #endif
2033 if (! CONSTANT_POOL_BEFORE_FUNCTION)
2035 output_constant_pool (fnname, decl);
2036 switch_to_section (function_section (decl)); /* need to switch back */
2038 /* Output labels for end of hot/cold text sections (to be used by
2039 debug info.) */
2040 if (crtl->has_bb_partition)
2042 section *save_text_section;
2044 save_text_section = in_section;
2045 switch_to_section (unlikely_text_section ());
2046 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
2047 if (cold_function_name != NULL_TREE)
2048 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
2049 IDENTIFIER_POINTER (cold_function_name),
2050 decl);
2051 #endif
2052 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
2053 if (first_function_block_is_cold)
2054 switch_to_section (text_section);
2055 else
2056 switch_to_section (function_section (decl));
2057 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
2058 switch_to_section (save_text_section);
2062 /* Assemble code to leave SIZE bytes of zeros. */
2064 void
2065 assemble_zeros (unsigned HOST_WIDE_INT size)
2067 /* Do no output if -fsyntax-only. */
2068 if (flag_syntax_only)
2069 return;
2071 #ifdef ASM_NO_SKIP_IN_TEXT
2072 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
2073 so we must output 0s explicitly in the text section. */
2074 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
2076 unsigned HOST_WIDE_INT i;
2077 for (i = 0; i < size; i++)
2078 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
2080 else
2081 #endif
2082 if (size > 0)
2083 ASM_OUTPUT_SKIP (asm_out_file, size);
2086 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
2088 void
2089 assemble_align (unsigned int align)
2091 if (align > BITS_PER_UNIT)
2093 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2097 /* Assemble a string constant with the specified C string as contents. */
2099 void
2100 assemble_string (const char *p, int size)
2102 int pos = 0;
2103 int maximum = 2000;
2105 /* If the string is very long, split it up. */
2107 while (pos < size)
2109 int thissize = size - pos;
2110 if (thissize > maximum)
2111 thissize = maximum;
2113 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
2115 pos += thissize;
2116 p += thissize;
2121 /* A noswitch_section_callback for lcomm_section. */
2123 static bool
2124 emit_local (tree decl ATTRIBUTE_UNUSED,
2125 const char *name ATTRIBUTE_UNUSED,
2126 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2127 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2129 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2130 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2131 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2132 size, align);
2133 return true;
2134 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2135 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2136 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2137 return true;
2138 #else
2139 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2140 return false;
2141 #endif
2144 /* A noswitch_section_callback for bss_noswitch_section. */
2146 #if defined ASM_OUTPUT_ALIGNED_BSS
2147 static bool
2148 emit_bss (tree decl ATTRIBUTE_UNUSED,
2149 const char *name ATTRIBUTE_UNUSED,
2150 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2151 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2153 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2154 get_variable_align (decl));
2155 return true;
2157 #endif
2159 /* A noswitch_section_callback for comm_section. */
2161 static bool
2162 emit_common (tree decl ATTRIBUTE_UNUSED,
2163 const char *name ATTRIBUTE_UNUSED,
2164 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2165 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2167 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2168 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2169 size, get_variable_align (decl));
2170 return true;
2171 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2172 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2173 get_variable_align (decl));
2174 return true;
2175 #else
2176 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2177 return false;
2178 #endif
2181 /* A noswitch_section_callback for tls_comm_section. */
2183 static bool
2184 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2185 const char *name ATTRIBUTE_UNUSED,
2186 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2187 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2189 #ifdef ASM_OUTPUT_TLS_COMMON
2190 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2191 return true;
2192 #else
2193 sorry ("thread-local COMMON data not implemented");
2194 return true;
2195 #endif
2198 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2199 NAME is the name of DECL's SYMBOL_REF. */
2201 static void
2202 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2203 unsigned int align)
2205 unsigned HOST_WIDE_INT size, rounded;
2207 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2208 rounded = size;
2210 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2211 size += asan_red_zone_size (size);
2213 /* Don't allocate zero bytes of common,
2214 since that means "undefined external" in the linker. */
2215 if (size == 0)
2216 rounded = 1;
2218 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2219 so that each uninitialized object starts on such a boundary. */
2220 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2221 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2222 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2224 if (!sect->noswitch.callback (decl, name, size, rounded)
2225 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2226 error ("requested alignment for %q+D is greater than "
2227 "implemented alignment of %wu", decl, rounded);
2230 /* A subroutine of assemble_variable. Output the label and contents of
2231 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2232 is as for assemble_variable. */
2234 static void
2235 assemble_variable_contents (tree decl, const char *name,
2236 bool dont_output_data, bool merge_strings)
2238 /* Do any machine/system dependent processing of the object. */
2239 #ifdef ASM_DECLARE_OBJECT_NAME
2240 last_assemble_variable_decl = decl;
2241 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2242 #else
2243 /* Standard thing is just output label for the object. */
2244 ASM_OUTPUT_LABEL (asm_out_file, name);
2245 #endif /* ASM_DECLARE_OBJECT_NAME */
2247 if (!dont_output_data)
2249 /* Caller is supposed to use varpool_get_constructor when it wants
2250 to output the body. */
2251 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2252 if (DECL_INITIAL (decl)
2253 && DECL_INITIAL (decl) != error_mark_node
2254 && !initializer_zerop (DECL_INITIAL (decl)))
2255 /* Output the actual data. */
2256 output_constant (DECL_INITIAL (decl),
2257 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2258 get_variable_align (decl),
2259 false, merge_strings);
2260 else
2261 /* Leave space for it. */
2262 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2263 targetm.asm_out.decl_end ();
2267 /* Write out assembly for the variable DECL, which is not defined in
2268 the current translation unit. */
2269 void
2270 assemble_undefined_decl (tree decl)
2272 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2273 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2276 /* Assemble everything that is needed for a variable or function declaration.
2277 Not used for automatic variables, and not used for function definitions.
2278 Should not be called for variables of incomplete structure type.
2280 TOP_LEVEL is nonzero if this variable has file scope.
2281 AT_END is nonzero if this is the special handling, at end of compilation,
2282 to define things that have had only tentative definitions.
2283 DONT_OUTPUT_DATA if nonzero means don't actually output the
2284 initial value (that will be done by the caller). */
2286 void
2287 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2288 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2290 const char *name;
2291 rtx decl_rtl, symbol;
2292 section *sect;
2293 unsigned int align;
2294 bool asan_protected = false;
2296 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2297 gcc_assert (VAR_P (decl));
2299 /* Emulated TLS had better not get this far. */
2300 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2302 last_assemble_variable_decl = 0;
2304 /* Normally no need to say anything here for external references,
2305 since assemble_external is called by the language-specific code
2306 when a declaration is first seen. */
2308 if (DECL_EXTERNAL (decl))
2309 return;
2311 /* Do nothing for global register variables. */
2312 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2314 TREE_ASM_WRITTEN (decl) = 1;
2315 return;
2318 /* If type was incomplete when the variable was declared,
2319 see if it is complete now. */
2321 if (DECL_SIZE (decl) == 0)
2322 layout_decl (decl, 0);
2324 /* Still incomplete => don't allocate it; treat the tentative defn
2325 (which is what it must have been) as an `extern' reference. */
2327 if (!dont_output_data && DECL_SIZE (decl) == 0)
2329 error ("storage size of %q+D isn%'t known", decl);
2330 TREE_ASM_WRITTEN (decl) = 1;
2331 return;
2334 /* The first declaration of a variable that comes through this function
2335 decides whether it is global (in C, has external linkage)
2336 or local (in C, has internal linkage). So do nothing more
2337 if this function has already run. */
2339 if (TREE_ASM_WRITTEN (decl))
2340 return;
2342 /* Make sure targetm.encode_section_info is invoked before we set
2343 ASM_WRITTEN. */
2344 decl_rtl = DECL_RTL (decl);
2346 TREE_ASM_WRITTEN (decl) = 1;
2348 /* Do no output if -fsyntax-only. */
2349 if (flag_syntax_only)
2350 return;
2352 if (! dont_output_data
2353 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2355 error ("size of variable %q+D is too large", decl);
2356 return;
2359 gcc_assert (MEM_P (decl_rtl));
2360 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2361 symbol = XEXP (decl_rtl, 0);
2363 /* If this symbol belongs to the tree constant pool, output the constant
2364 if it hasn't already been written. */
2365 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2367 tree decl = SYMBOL_REF_DECL (symbol);
2368 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2369 output_constant_def_contents (symbol);
2370 return;
2373 app_disable ();
2375 name = XSTR (symbol, 0);
2376 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2377 notice_global_symbol (decl);
2379 /* Compute the alignment of this data. */
2381 align_variable (decl, dont_output_data);
2383 if ((flag_sanitize & SANITIZE_ADDRESS)
2384 && asan_protect_global (decl))
2386 asan_protected = true;
2387 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2388 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2391 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2393 align = get_variable_align (decl);
2395 if (TREE_PUBLIC (decl))
2396 maybe_assemble_visibility (decl);
2398 if (DECL_PRESERVE_P (decl))
2399 targetm.asm_out.mark_decl_preserved (name);
2401 /* First make the assembler name(s) global if appropriate. */
2402 sect = get_variable_section (decl, false);
2403 if (TREE_PUBLIC (decl)
2404 && (sect->common.flags & SECTION_COMMON) == 0)
2405 globalize_decl (decl);
2407 /* Output any data that we will need to use the address of. */
2408 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2409 output_addressed_constants (DECL_INITIAL (decl), 0);
2411 /* dbxout.cc needs to know this. */
2412 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2413 DECL_IN_TEXT_SECTION (decl) = 1;
2415 /* If the decl is part of an object_block, make sure that the decl
2416 has been positioned within its block, but do not write out its
2417 definition yet. output_object_blocks will do that later. */
2418 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2420 gcc_assert (!dont_output_data);
2421 place_block_symbol (symbol);
2423 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2424 assemble_noswitch_variable (decl, name, sect, align);
2425 else
2427 /* Special-case handling of vtv comdat sections. */
2428 if (SECTION_STYLE (sect) == SECTION_NAMED
2429 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2430 handle_vtv_comdat_section (sect, decl);
2431 else
2432 switch_to_section (sect, decl);
2433 if (align > BITS_PER_UNIT)
2434 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2435 assemble_variable_contents (decl, name, dont_output_data,
2436 (sect->common.flags & SECTION_MERGE)
2437 && (sect->common.flags & SECTION_STRINGS));
2438 if (asan_protected)
2440 unsigned HOST_WIDE_INT int size
2441 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2442 assemble_zeros (asan_red_zone_size (size));
2447 /* Return true if type TYPE contains any pointers. */
2449 static bool
2450 contains_pointers_p (tree type)
2452 switch (TREE_CODE (type))
2454 case POINTER_TYPE:
2455 case REFERENCE_TYPE:
2456 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2457 so I'll play safe and return 1. */
2458 case OFFSET_TYPE:
2459 return true;
2461 case RECORD_TYPE:
2462 case UNION_TYPE:
2463 case QUAL_UNION_TYPE:
2465 tree fields;
2466 /* For a type that has fields, see if the fields have pointers. */
2467 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2468 if (TREE_CODE (fields) == FIELD_DECL
2469 && contains_pointers_p (TREE_TYPE (fields)))
2470 return true;
2471 return false;
2474 case ARRAY_TYPE:
2475 /* An array type contains pointers if its element type does. */
2476 return contains_pointers_p (TREE_TYPE (type));
2478 default:
2479 return false;
2483 /* We delay assemble_external processing until
2484 the compilation unit is finalized. This is the best we can do for
2485 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2486 it all the way to final. See PR 17982 for further discussion. */
2487 static GTY(()) tree pending_assemble_externals;
2489 /* A similar list of pending libcall symbols. We only want to declare
2490 symbols that are actually used in the final assembly. */
2491 static GTY(()) rtx pending_libcall_symbols;
2493 #ifdef ASM_OUTPUT_EXTERNAL
2494 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2495 As a result, assemble_external can be called after the list of externals
2496 is processed and the pointer set destroyed. */
2497 static bool pending_assemble_externals_processed;
2499 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2500 TREE_LIST in assemble_external. */
2501 static hash_set<tree> *pending_assemble_externals_set;
2503 /* True if DECL is a function decl for which no out-of-line copy exists.
2504 It is assumed that DECL's assembler name has been set. */
2506 static bool
2507 incorporeal_function_p (tree decl)
2509 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl))
2511 const char *name;
2513 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2514 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2515 return true;
2517 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2518 /* Atomic or sync builtins which have survived this far will be
2519 resolved externally and therefore are not incorporeal. */
2520 if (startswith (name, "__builtin_"))
2521 return true;
2523 return false;
2526 /* Actually do the tests to determine if this is necessary, and invoke
2527 ASM_OUTPUT_EXTERNAL. */
2528 static void
2529 assemble_external_real (tree decl)
2531 rtx rtl = DECL_RTL (decl);
2533 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2534 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2535 && !incorporeal_function_p (decl))
2537 /* Some systems do require some output. */
2538 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2539 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2542 #endif
2544 void
2545 process_pending_assemble_externals (void)
2547 #ifdef ASM_OUTPUT_EXTERNAL
2548 tree list;
2549 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2550 assemble_external_real (TREE_VALUE (list));
2552 for (rtx list = pending_libcall_symbols; list; list = XEXP (list, 1))
2554 rtx symbol = XEXP (list, 0);
2555 const char *name = targetm.strip_name_encoding (XSTR (symbol, 0));
2556 tree id = get_identifier (name);
2557 if (TREE_SYMBOL_REFERENCED (id))
2558 targetm.asm_out.external_libcall (symbol);
2561 pending_assemble_externals = 0;
2562 pending_assemble_externals_processed = true;
2563 pending_libcall_symbols = NULL_RTX;
2564 delete pending_assemble_externals_set;
2565 #endif
2568 /* This TREE_LIST contains any weak symbol declarations waiting
2569 to be emitted. */
2570 static GTY(()) tree weak_decls;
2572 /* Output something to declare an external symbol to the assembler,
2573 and qualifiers such as weakness. (Most assemblers don't need
2574 extern declaration, so we normally output nothing.) Do nothing if
2575 DECL is not external. */
2577 void
2578 assemble_external (tree decl ATTRIBUTE_UNUSED)
2580 /* Make sure that the ASM_OUT_FILE is open.
2581 If it's not, we should not be calling this function. */
2582 gcc_assert (asm_out_file);
2584 /* In a perfect world, the following condition would be true.
2585 Sadly, the Go front end emit assembly *from the front end*,
2586 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2587 #if 0
2588 /* This function should only be called if we are expanding, or have
2589 expanded, to RTL.
2590 Ideally, only final.cc would be calling this function, but it is
2591 not clear whether that would break things somehow. See PR 17982
2592 for further discussion. */
2593 gcc_assert (state == EXPANSION
2594 || state == FINISHED);
2595 #endif
2597 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2598 return;
2600 /* We want to output annotation for weak and external symbols at
2601 very last to check if they are references or not. */
2603 if (TARGET_SUPPORTS_WEAK
2604 && DECL_WEAK (decl)
2605 /* TREE_STATIC is a weird and abused creature which is not
2606 generally the right test for whether an entity has been
2607 locally emitted, inlined or otherwise not-really-extern, but
2608 for declarations that can be weak, it happens to be
2609 match. */
2610 && !TREE_STATIC (decl)
2611 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2612 && value_member (decl, weak_decls) == NULL_TREE)
2613 weak_decls = tree_cons (NULL, decl, weak_decls);
2615 #ifdef ASM_OUTPUT_EXTERNAL
2616 if (pending_assemble_externals_processed)
2618 assemble_external_real (decl);
2619 return;
2622 if (! pending_assemble_externals_set->add (decl))
2623 pending_assemble_externals = tree_cons (NULL, decl,
2624 pending_assemble_externals);
2625 #endif
2628 /* Similar, for calling a library function FUN. */
2630 void
2631 assemble_external_libcall (rtx fun)
2633 /* Declare library function name external when first used, if nec. */
2634 if (! SYMBOL_REF_USED (fun))
2636 #ifdef ASM_OUTPUT_EXTERNAL
2637 gcc_assert (!pending_assemble_externals_processed);
2638 #endif
2639 SYMBOL_REF_USED (fun) = 1;
2640 /* Make sure the libcall symbol is in the symtab so any
2641 reference to it will mark its tree node as referenced, via
2642 assemble_name_resolve. These are eventually emitted, if
2643 used, in process_pending_assemble_externals. */
2644 const char *name = targetm.strip_name_encoding (XSTR (fun, 0));
2645 get_identifier (name);
2646 pending_libcall_symbols = gen_rtx_EXPR_LIST (VOIDmode, fun,
2647 pending_libcall_symbols);
2651 /* Assemble a label named NAME. */
2653 void
2654 assemble_label (FILE *file, const char *name)
2656 ASM_OUTPUT_LABEL (file, name);
2659 /* Set the symbol_referenced flag for ID. */
2660 void
2661 mark_referenced (tree id)
2663 TREE_SYMBOL_REFERENCED (id) = 1;
2666 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2667 void
2668 mark_decl_referenced (tree decl)
2670 if (TREE_CODE (decl) == FUNCTION_DECL)
2672 /* Extern inline functions don't become needed when referenced.
2673 If we know a method will be emitted in other TU and no new
2674 functions can be marked reachable, just use the external
2675 definition. */
2676 struct cgraph_node *node = cgraph_node::get_create (decl);
2677 if (!DECL_EXTERNAL (decl)
2678 && !node->definition)
2679 node->mark_force_output ();
2681 else if (VAR_P (decl))
2683 varpool_node *node = varpool_node::get_create (decl);
2684 /* C++ frontend use mark_decl_references to force COMDAT variables
2685 to be output that might appear dead otherwise. */
2686 node->force_output = true;
2688 /* else do nothing - we can get various sorts of CST nodes here,
2689 which do not need to be marked. */
2693 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2694 starts with a *, the rest of NAME is output verbatim. Otherwise
2695 NAME is transformed in a target-specific way (usually by the
2696 addition of an underscore). */
2698 void
2699 assemble_name_raw (FILE *file, const char *name)
2701 if (name[0] == '*')
2702 fputs (&name[1], file);
2703 else
2704 ASM_OUTPUT_LABELREF (file, name);
2707 /* Return NAME that should actually be emitted, looking through
2708 transparent aliases. If NAME refers to an entity that is also
2709 represented as a tree (like a function or variable), mark the entity
2710 as referenced. */
2711 const char *
2712 assemble_name_resolve (const char *name)
2714 const char *real_name = targetm.strip_name_encoding (name);
2715 tree id = maybe_get_identifier (real_name);
2717 if (id)
2719 tree id_orig = id;
2721 mark_referenced (id);
2722 ultimate_transparent_alias_target (&id);
2723 if (id != id_orig)
2724 name = IDENTIFIER_POINTER (id);
2725 gcc_assert (! TREE_CHAIN (id));
2728 return name;
2731 /* Like assemble_name_raw, but should be used when NAME might refer to
2732 an entity that is also represented as a tree (like a function or
2733 variable). If NAME does refer to such an entity, that entity will
2734 be marked as referenced. */
2736 void
2737 assemble_name (FILE *file, const char *name)
2739 assemble_name_raw (file, assemble_name_resolve (name));
2742 /* Allocate SIZE bytes writable static space with a gensym name
2743 and return an RTX to refer to its address. */
2746 assemble_static_space (unsigned HOST_WIDE_INT size)
2748 char name[17];
2749 const char *namestring;
2750 rtx x;
2752 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2753 ++const_labelno;
2754 namestring = ggc_strdup (name);
2756 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2757 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2759 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2760 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2761 BIGGEST_ALIGNMENT);
2762 #else
2763 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2764 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2765 #else
2767 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2768 so that each uninitialized object starts on such a boundary. */
2769 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2770 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2771 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2772 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2773 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2774 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2776 #endif
2777 #endif
2778 return x;
2781 /* Assemble the static constant template for function entry trampolines.
2782 This is done at most once per compilation.
2783 Returns an RTX for the address of the template. */
2785 static GTY(()) rtx initial_trampoline;
2788 assemble_trampoline_template (void)
2790 char label[256];
2791 const char *name;
2792 int align;
2793 rtx symbol;
2795 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2797 if (initial_trampoline)
2798 return initial_trampoline;
2800 /* By default, put trampoline templates in read-only data section. */
2802 #ifdef TRAMPOLINE_SECTION
2803 switch_to_section (TRAMPOLINE_SECTION);
2804 #else
2805 switch_to_section (readonly_data_section);
2806 #endif
2808 /* Write the assembler code to define one. */
2809 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2810 if (align > 0)
2811 ASM_OUTPUT_ALIGN (asm_out_file, align);
2813 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2814 targetm.asm_out.trampoline_template (asm_out_file);
2816 /* Record the rtl to refer to it. */
2817 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2818 name = ggc_strdup (label);
2819 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2820 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2822 initial_trampoline = gen_const_mem (BLKmode, symbol);
2823 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2824 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2826 return initial_trampoline;
2829 /* A and B are either alignments or offsets. Return the minimum alignment
2830 that may be assumed after adding the two together. */
2832 static inline unsigned
2833 min_align (unsigned int a, unsigned int b)
2835 return least_bit_hwi (a | b);
2838 /* Return the assembler directive for creating a given kind of integer
2839 object. SIZE is the number of bytes in the object and ALIGNED_P
2840 indicates whether it is known to be aligned. Return NULL if the
2841 assembly dialect has no such directive.
2843 The returned string should be printed at the start of a new line and
2844 be followed immediately by the object's initial value. */
2846 const char *
2847 integer_asm_op (int size, int aligned_p)
2849 struct asm_int_op *ops;
2851 if (aligned_p)
2852 ops = &targetm.asm_out.aligned_op;
2853 else
2854 ops = &targetm.asm_out.unaligned_op;
2856 switch (size)
2858 case 1:
2859 return targetm.asm_out.byte_op;
2860 case 2:
2861 return ops->hi;
2862 case 3:
2863 return ops->psi;
2864 case 4:
2865 return ops->si;
2866 case 5:
2867 case 6:
2868 case 7:
2869 return ops->pdi;
2870 case 8:
2871 return ops->di;
2872 case 9:
2873 case 10:
2874 case 11:
2875 case 12:
2876 case 13:
2877 case 14:
2878 case 15:
2879 return ops->pti;
2880 case 16:
2881 return ops->ti;
2882 default:
2883 return NULL;
2887 /* Use directive OP to assemble an integer object X. Print OP at the
2888 start of the line, followed immediately by the value of X. */
2890 void
2891 assemble_integer_with_op (const char *op, rtx x)
2893 fputs (op, asm_out_file);
2894 output_addr_const (asm_out_file, x);
2895 fputc ('\n', asm_out_file);
2898 /* The default implementation of the asm_out.integer target hook. */
2900 bool
2901 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2902 unsigned int size ATTRIBUTE_UNUSED,
2903 int aligned_p ATTRIBUTE_UNUSED)
2905 const char *op = integer_asm_op (size, aligned_p);
2906 /* Avoid GAS bugs for large values. Specifically negative values whose
2907 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2908 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2909 return false;
2910 return op && (assemble_integer_with_op (op, x), true);
2913 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2914 the alignment of the integer in bits. Return 1 if we were able to output
2915 the constant, otherwise 0. We must be able to output the constant,
2916 if FORCE is nonzero. */
2918 bool
2919 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2921 int aligned_p;
2923 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2925 /* See if the target hook can handle this kind of object. */
2926 if (targetm.asm_out.integer (x, size, aligned_p))
2927 return true;
2929 /* If the object is a multi-byte one, try splitting it up. Split
2930 it into words it if is multi-word, otherwise split it into bytes. */
2931 if (size > 1)
2933 machine_mode omode, imode;
2934 unsigned int subalign;
2935 unsigned int subsize, i;
2936 enum mode_class mclass;
2938 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2939 subalign = MIN (align, subsize * BITS_PER_UNIT);
2940 if (GET_CODE (x) == CONST_FIXED)
2941 mclass = GET_MODE_CLASS (GET_MODE (x));
2942 else
2943 mclass = MODE_INT;
2945 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2946 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2948 for (i = 0; i < size; i += subsize)
2950 rtx partial = simplify_subreg (omode, x, imode, i);
2951 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2952 break;
2954 if (i == size)
2955 return true;
2957 /* If we've printed some of it, but not all of it, there's no going
2958 back now. */
2959 gcc_assert (!i);
2962 gcc_assert (!force);
2964 return false;
2967 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
2968 is the alignment of the constant in bits. If REVERSE is true, D is output
2969 in reverse storage order. */
2971 void
2972 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2973 bool reverse)
2975 long data[4] = {0, 0, 0, 0};
2976 int bitsize, nelts, nunits, units_per;
2977 rtx elt;
2979 /* This is hairy. We have a quantity of known size. real_to_target
2980 will put it into an array of *host* longs, 32 bits per element
2981 (even if long is more than 32 bits). We need to determine the
2982 number of array elements that are occupied (nelts) and the number
2983 of *target* min-addressable units that will be occupied in the
2984 object file (nunits). We cannot assume that 32 divides the
2985 mode's bitsize (size * BITS_PER_UNIT) evenly.
2987 size * BITS_PER_UNIT is used here to make sure that padding bits
2988 (which might appear at either end of the value; real_to_target
2989 will include the padding bits in its output array) are included. */
2991 nunits = GET_MODE_SIZE (mode);
2992 bitsize = nunits * BITS_PER_UNIT;
2993 nelts = CEIL (bitsize, 32);
2994 units_per = 32 / BITS_PER_UNIT;
2996 real_to_target (data, &d, mode);
2998 /* Put out the first word with the specified alignment. */
2999 unsigned int chunk_nunits = MIN (nunits, units_per);
3000 if (reverse)
3001 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
3002 else
3003 elt = GEN_INT (sext_hwi (data[0], chunk_nunits * BITS_PER_UNIT));
3004 assemble_integer (elt, chunk_nunits, align, 1);
3005 nunits -= chunk_nunits;
3007 /* Subsequent words need only 32-bit alignment. */
3008 align = min_align (align, 32);
3010 for (int i = 1; i < nelts; i++)
3012 chunk_nunits = MIN (nunits, units_per);
3013 if (reverse)
3014 elt = flip_storage_order (SImode,
3015 gen_int_mode (data[nelts - 1 - i], SImode));
3016 else
3017 elt = GEN_INT (sext_hwi (data[i], chunk_nunits * BITS_PER_UNIT));
3018 assemble_integer (elt, chunk_nunits, align, 1);
3019 nunits -= chunk_nunits;
3023 /* Given an expression EXP with a constant value,
3024 reduce it to the sum of an assembler symbol and an integer.
3025 Store them both in the structure *VALUE.
3026 EXP must be reducible. */
3028 class addr_const {
3029 public:
3030 rtx base;
3031 poly_int64 offset;
3034 static void
3035 decode_addr_const (tree exp, class addr_const *value)
3037 tree target = TREE_OPERAND (exp, 0);
3038 poly_int64 offset = 0;
3039 rtx x;
3041 while (1)
3043 poly_int64 bytepos;
3044 if (TREE_CODE (target) == COMPONENT_REF
3045 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
3046 &bytepos))
3048 offset += bytepos;
3049 target = TREE_OPERAND (target, 0);
3051 else if (TREE_CODE (target) == ARRAY_REF
3052 || TREE_CODE (target) == ARRAY_RANGE_REF)
3054 /* Truncate big offset. */
3055 offset
3056 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
3057 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
3058 target = TREE_OPERAND (target, 0);
3060 else if (TREE_CODE (target) == MEM_REF
3061 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
3063 offset += mem_ref_offset (target).force_shwi ();
3064 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
3066 else if (INDIRECT_REF_P (target)
3067 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
3068 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
3069 == ADDR_EXPR)
3070 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
3071 else
3072 break;
3075 switch (TREE_CODE (target))
3077 case VAR_DECL:
3078 case FUNCTION_DECL:
3079 x = DECL_RTL (target);
3080 break;
3082 case LABEL_DECL:
3083 x = gen_rtx_MEM (FUNCTION_MODE,
3084 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
3085 break;
3087 case REAL_CST:
3088 case FIXED_CST:
3089 case STRING_CST:
3090 case COMPLEX_CST:
3091 case CONSTRUCTOR:
3092 case INTEGER_CST:
3093 x = lookup_constant_def (target);
3094 /* Should have been added by output_addressed_constants. */
3095 gcc_assert (x);
3096 break;
3098 case INDIRECT_REF:
3099 /* This deals with absolute addresses. */
3100 offset += tree_to_shwi (TREE_OPERAND (target, 0));
3101 x = gen_rtx_MEM (QImode,
3102 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
3103 break;
3105 case COMPOUND_LITERAL_EXPR:
3106 gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
3107 x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
3108 break;
3110 default:
3111 gcc_unreachable ();
3114 gcc_assert (MEM_P (x));
3115 x = XEXP (x, 0);
3117 value->base = x;
3118 value->offset = offset;
3121 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
3123 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
3125 /* Constant pool accessor function. */
3127 hash_table<tree_descriptor_hasher> *
3128 constant_pool_htab (void)
3130 return const_desc_htab;
3133 /* Compute a hash code for a constant expression. */
3135 hashval_t
3136 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3138 return ptr->hash;
3141 static hashval_t
3142 const_hash_1 (const tree exp)
3144 const char *p;
3145 hashval_t hi;
3146 int len, i;
3147 enum tree_code code = TREE_CODE (exp);
3149 /* Either set P and LEN to the address and len of something to hash and
3150 exit the switch or return a value. */
3152 switch (code)
3154 case INTEGER_CST:
3155 p = (char *) &TREE_INT_CST_ELT (exp, 0);
3156 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3157 break;
3159 case REAL_CST:
3160 return real_hash (TREE_REAL_CST_PTR (exp));
3162 case FIXED_CST:
3163 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3165 case STRING_CST:
3166 p = TREE_STRING_POINTER (exp);
3167 len = TREE_STRING_LENGTH (exp);
3168 break;
3170 case COMPLEX_CST:
3171 return (const_hash_1 (TREE_REALPART (exp)) * 5
3172 + const_hash_1 (TREE_IMAGPART (exp)));
3174 case VECTOR_CST:
3176 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3177 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3178 unsigned int count = vector_cst_encoded_nelts (exp);
3179 for (unsigned int i = 0; i < count; ++i)
3180 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3181 return hi;
3184 case CONSTRUCTOR:
3186 unsigned HOST_WIDE_INT idx;
3187 tree value;
3189 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3191 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3192 if (value)
3193 hi = hi * 603 + const_hash_1 (value);
3195 return hi;
3198 case ADDR_EXPR:
3199 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3200 return const_hash_1 (TREE_OPERAND (exp, 0));
3202 /* Fallthru. */
3203 case FDESC_EXPR:
3205 class addr_const value;
3207 decode_addr_const (exp, &value);
3208 switch (GET_CODE (value.base))
3210 case SYMBOL_REF:
3211 /* Don't hash the address of the SYMBOL_REF;
3212 only use the offset and the symbol name. */
3213 hi = value.offset.coeffs[0];
3214 p = XSTR (value.base, 0);
3215 for (i = 0; p[i] != 0; i++)
3216 hi = ((hi * 613) + (unsigned) (p[i]));
3217 break;
3219 case LABEL_REF:
3220 hi = (value.offset.coeffs[0]
3221 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3222 break;
3224 default:
3225 gcc_unreachable ();
3228 return hi;
3230 case PLUS_EXPR:
3231 case POINTER_PLUS_EXPR:
3232 case MINUS_EXPR:
3233 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3234 + const_hash_1 (TREE_OPERAND (exp, 1)));
3236 CASE_CONVERT:
3237 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3239 default:
3240 /* A language specific constant. Just hash the code. */
3241 return code;
3244 /* Compute hashing function. */
3245 hi = len;
3246 for (i = 0; i < len; i++)
3247 hi = ((hi * 613) + (unsigned) (p[i]));
3249 return hi;
3252 /* Wrapper of compare_constant, for the htab interface. */
3253 bool
3254 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3255 constant_descriptor_tree *c2)
3257 if (c1->hash != c2->hash)
3258 return false;
3259 return compare_constant (c1->value, c2->value);
3262 /* Compare t1 and t2, and return true only if they are known to result in
3263 the same bit pattern on output. */
3265 static bool
3266 compare_constant (const tree t1, const tree t2)
3268 enum tree_code typecode;
3270 if (t1 == NULL_TREE)
3271 return t2 == NULL_TREE;
3272 if (t2 == NULL_TREE)
3273 return false;
3275 if (TREE_CODE (t1) != TREE_CODE (t2))
3276 return false;
3278 switch (TREE_CODE (t1))
3280 case INTEGER_CST:
3281 /* Integer constants are the same only if the same width of type. */
3282 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3283 return false;
3284 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3285 return false;
3286 return tree_int_cst_equal (t1, t2);
3288 case REAL_CST:
3289 /* Real constants are the same only if the same width of type. In
3290 addition to the same width, we need to check whether the modes are the
3291 same. There might be two floating point modes that are the same size
3292 but have different representations, such as the PowerPC that has 2
3293 different 128-bit floating point types (IBM extended double and IEEE
3294 128-bit floating point). */
3295 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3296 return false;
3297 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3298 return false;
3299 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3301 case FIXED_CST:
3302 /* Fixed constants are the same only if the same width of type. */
3303 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3304 return false;
3306 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3308 case STRING_CST:
3309 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3310 || int_size_in_bytes (TREE_TYPE (t1))
3311 != int_size_in_bytes (TREE_TYPE (t2)))
3312 return false;
3314 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3315 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3316 TREE_STRING_LENGTH (t1)));
3318 case COMPLEX_CST:
3319 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3320 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3322 case VECTOR_CST:
3324 if (VECTOR_CST_NPATTERNS (t1)
3325 != VECTOR_CST_NPATTERNS (t2))
3326 return false;
3328 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3329 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3330 return false;
3332 unsigned int count = vector_cst_encoded_nelts (t1);
3333 for (unsigned int i = 0; i < count; ++i)
3334 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3335 VECTOR_CST_ENCODED_ELT (t2, i)))
3336 return false;
3338 return true;
3341 case CONSTRUCTOR:
3343 vec<constructor_elt, va_gc> *v1, *v2;
3344 unsigned HOST_WIDE_INT idx;
3346 typecode = TREE_CODE (TREE_TYPE (t1));
3347 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3348 return false;
3350 if (typecode == ARRAY_TYPE)
3352 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3353 /* For arrays, check that mode, size and storage order match. */
3354 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3355 || size_1 == -1
3356 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3357 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3358 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3359 return false;
3361 else
3363 /* For record and union constructors, require exact type
3364 equality. */
3365 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3366 return false;
3369 v1 = CONSTRUCTOR_ELTS (t1);
3370 v2 = CONSTRUCTOR_ELTS (t2);
3371 if (vec_safe_length (v1) != vec_safe_length (v2))
3372 return false;
3374 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3376 constructor_elt *c1 = &(*v1)[idx];
3377 constructor_elt *c2 = &(*v2)[idx];
3379 /* Check that each value is the same... */
3380 if (!compare_constant (c1->value, c2->value))
3381 return false;
3382 /* ... and that they apply to the same fields! */
3383 if (typecode == ARRAY_TYPE)
3385 if (!compare_constant (c1->index, c2->index))
3386 return false;
3388 else
3390 if (c1->index != c2->index)
3391 return false;
3395 return true;
3398 case ADDR_EXPR:
3399 case FDESC_EXPR:
3401 class addr_const value1, value2;
3402 enum rtx_code code;
3403 bool ret;
3405 decode_addr_const (t1, &value1);
3406 decode_addr_const (t2, &value2);
3408 if (maybe_ne (value1.offset, value2.offset))
3409 return false;
3411 code = GET_CODE (value1.base);
3412 if (code != GET_CODE (value2.base))
3413 return false;
3415 switch (code)
3417 case SYMBOL_REF:
3418 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3419 break;
3421 case LABEL_REF:
3422 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3423 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3424 break;
3426 default:
3427 gcc_unreachable ();
3429 return ret;
3432 case PLUS_EXPR:
3433 case POINTER_PLUS_EXPR:
3434 case MINUS_EXPR:
3435 case RANGE_EXPR:
3436 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3437 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3439 CASE_CONVERT:
3440 case VIEW_CONVERT_EXPR:
3441 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3443 default:
3444 return false;
3448 /* Return the section into which constant EXP should be placed. */
3450 static section *
3451 get_constant_section (tree exp, unsigned int align)
3453 return targetm.asm_out.select_section (exp,
3454 compute_reloc_for_constant (exp),
3455 align);
3458 /* Return the size of constant EXP in bytes. */
3460 static HOST_WIDE_INT
3461 get_constant_size (tree exp)
3463 HOST_WIDE_INT size;
3465 size = int_size_in_bytes (TREE_TYPE (exp));
3466 gcc_checking_assert (size >= 0);
3467 gcc_checking_assert (TREE_CODE (exp) != STRING_CST
3468 || size >= TREE_STRING_LENGTH (exp));
3469 return size;
3472 /* Subroutine of output_constant_def:
3473 No constant equal to EXP is known to have been output.
3474 Make a constant descriptor to enter EXP in the hash table.
3475 Assign the label number and construct RTL to refer to the
3476 constant's location in memory.
3477 Caller is responsible for updating the hash table. */
3479 static struct constant_descriptor_tree *
3480 build_constant_desc (tree exp)
3482 struct constant_descriptor_tree *desc;
3483 rtx symbol, rtl;
3484 char label[256];
3485 int labelno;
3486 tree decl;
3488 desc = ggc_alloc<constant_descriptor_tree> ();
3489 desc->value = exp;
3491 /* Create a string containing the label name, in LABEL. */
3492 labelno = const_labelno++;
3493 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3495 /* Construct the VAR_DECL associated with the constant. */
3496 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3497 TREE_TYPE (exp));
3498 DECL_ARTIFICIAL (decl) = 1;
3499 DECL_IGNORED_P (decl) = 1;
3500 TREE_READONLY (decl) = 1;
3501 TREE_STATIC (decl) = 1;
3502 TREE_ADDRESSABLE (decl) = 1;
3503 /* We don't set the RTL yet as this would cause varpool to assume that the
3504 variable is referenced. Moreover, it would just be dropped in LTO mode.
3505 Instead we set the flag that will be recognized in make_decl_rtl. */
3506 DECL_IN_CONSTANT_POOL (decl) = 1;
3507 DECL_INITIAL (decl) = desc->value;
3508 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3509 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3510 if (TREE_CODE (exp) == STRING_CST)
3511 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3512 else
3514 align_variable (decl, 0);
3515 if (DECL_ALIGN (decl) < GET_MODE_ALIGNMENT (DECL_MODE (decl))
3516 && ((optab_handler (movmisalign_optab, DECL_MODE (decl))
3517 != CODE_FOR_nothing)
3518 || targetm.slow_unaligned_access (DECL_MODE (decl),
3519 DECL_ALIGN (decl))))
3520 SET_DECL_ALIGN (decl, GET_MODE_ALIGNMENT (DECL_MODE (decl)));
3523 /* Now construct the SYMBOL_REF and the MEM. */
3524 if (use_object_blocks_p ())
3526 int align = (TREE_CODE (decl) == CONST_DECL
3527 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3528 ? DECL_ALIGN (decl)
3529 : symtab_node::get (decl)->definition_alignment ());
3530 section *sect = get_constant_section (exp, align);
3531 symbol = create_block_symbol (ggc_strdup (label),
3532 get_block_for_section (sect), -1);
3534 else
3535 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3536 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3537 SET_SYMBOL_REF_DECL (symbol, decl);
3538 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3540 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3541 set_mem_alias_set (rtl, 0);
3543 /* Putting EXP into the literal pool might have imposed a different
3544 alignment which should be visible in the RTX as well. */
3545 set_mem_align (rtl, DECL_ALIGN (decl));
3547 /* We cannot share RTX'es in pool entries.
3548 Mark this piece of RTL as required for unsharing. */
3549 RTX_FLAG (rtl, used) = 1;
3551 /* Set flags or add text to the name to record information, such as
3552 that it is a local symbol. If the name is changed, the macro
3553 ASM_OUTPUT_LABELREF will have to know how to strip this
3554 information. This call might invalidate our local variable
3555 SYMBOL; we can't use it afterward. */
3556 targetm.encode_section_info (exp, rtl, true);
3558 desc->rtl = rtl;
3560 return desc;
3563 /* Subroutine of output_constant_def and tree_output_constant_def:
3564 Add a constant to the hash table that tracks which constants
3565 already have labels. */
3567 static constant_descriptor_tree *
3568 add_constant_to_table (tree exp, int defer)
3570 /* The hash table methods may call output_constant_def for addressed
3571 constants, so handle them first. */
3572 output_addressed_constants (exp, defer);
3574 /* Sanity check to catch recursive insertion. */
3575 static bool inserting;
3576 gcc_assert (!inserting);
3577 inserting = true;
3579 /* Look up EXP in the table of constant descriptors. If we didn't
3580 find it, create a new one. */
3581 struct constant_descriptor_tree key;
3582 key.value = exp;
3583 key.hash = const_hash_1 (exp);
3584 constant_descriptor_tree **loc
3585 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3587 inserting = false;
3589 struct constant_descriptor_tree *desc = *loc;
3590 if (!desc)
3592 desc = build_constant_desc (exp);
3593 desc->hash = key.hash;
3594 *loc = desc;
3597 return desc;
3600 /* Return an rtx representing a reference to constant data in memory
3601 for the constant expression EXP.
3603 If assembler code for such a constant has already been output,
3604 return an rtx to refer to it.
3605 Otherwise, output such a constant in memory
3606 and generate an rtx for it.
3608 If DEFER is nonzero, this constant can be deferred and output only
3609 if referenced in the function after all optimizations.
3611 `const_desc_table' records which constants already have label strings. */
3614 output_constant_def (tree exp, int defer)
3616 struct constant_descriptor_tree *desc = add_constant_to_table (exp, defer);
3617 maybe_output_constant_def_contents (desc, defer);
3618 return desc->rtl;
3621 /* Subroutine of output_constant_def: Decide whether or not we need to
3622 output the constant DESC now, and if so, do it. */
3623 static void
3624 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3625 int defer)
3627 rtx symbol = XEXP (desc->rtl, 0);
3628 tree exp = desc->value;
3630 if (flag_syntax_only)
3631 return;
3633 if (TREE_ASM_WRITTEN (exp))
3634 /* Already output; don't do it again. */
3635 return;
3637 /* We can always defer constants as long as the context allows
3638 doing so. */
3639 if (defer)
3641 /* Increment n_deferred_constants if it exists. It needs to be at
3642 least as large as the number of constants actually referred to
3643 by the function. If it's too small we'll stop looking too early
3644 and fail to emit constants; if it's too large we'll only look
3645 through the entire function when we could have stopped earlier. */
3646 if (cfun)
3647 n_deferred_constants++;
3648 return;
3651 output_constant_def_contents (symbol);
3654 /* Subroutine of output_constant_def_contents. Output the definition
3655 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3656 constant's alignment in bits. */
3658 static void
3659 assemble_constant_contents (tree exp, const char *label, unsigned int align,
3660 bool merge_strings)
3662 HOST_WIDE_INT size;
3664 size = get_constant_size (exp);
3666 /* Do any machine/system dependent processing of the constant. */
3667 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3669 /* Output the value of EXP. */
3670 output_constant (exp, size, align, false, merge_strings);
3672 targetm.asm_out.decl_end ();
3675 /* We must output the constant data referred to by SYMBOL; do so. */
3677 static void
3678 output_constant_def_contents (rtx symbol)
3680 tree decl = SYMBOL_REF_DECL (symbol);
3681 tree exp = DECL_INITIAL (decl);
3682 bool asan_protected = false;
3684 /* Make sure any other constants whose addresses appear in EXP
3685 are assigned label numbers. */
3686 output_addressed_constants (exp, 0);
3688 /* We are no longer deferring this constant. */
3689 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3691 if ((flag_sanitize & SANITIZE_ADDRESS)
3692 && TREE_CODE (exp) == STRING_CST
3693 && asan_protect_global (exp))
3695 asan_protected = true;
3696 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3697 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3700 /* If the constant is part of an object block, make sure that the
3701 decl has been positioned within its block, but do not write out
3702 its definition yet. output_object_blocks will do that later. */
3703 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3704 place_block_symbol (symbol);
3705 else
3707 int align = (TREE_CODE (decl) == CONST_DECL
3708 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3709 ? DECL_ALIGN (decl)
3710 : symtab_node::get (decl)->definition_alignment ());
3711 section *sect = get_constant_section (exp, align);
3712 switch_to_section (sect);
3713 if (align > BITS_PER_UNIT)
3714 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3715 assemble_constant_contents (exp, XSTR (symbol, 0), align,
3716 (sect->common.flags & SECTION_MERGE)
3717 && (sect->common.flags & SECTION_STRINGS));
3718 if (asan_protected)
3720 HOST_WIDE_INT size = get_constant_size (exp);
3721 assemble_zeros (asan_red_zone_size (size));
3726 /* Look up EXP in the table of constant descriptors. Return the rtl
3727 if it has been emitted, else null. */
3730 lookup_constant_def (tree exp)
3732 struct constant_descriptor_tree key;
3734 key.value = exp;
3735 key.hash = const_hash_1 (exp);
3736 constant_descriptor_tree *desc
3737 = const_desc_htab->find_with_hash (&key, key.hash);
3739 return (desc ? desc->rtl : NULL_RTX);
3742 /* Return a tree representing a reference to constant data in memory
3743 for the constant expression EXP.
3745 This is the counterpart of output_constant_def at the Tree level. */
3747 tree
3748 tree_output_constant_def (tree exp)
3750 struct constant_descriptor_tree *desc = add_constant_to_table (exp, 1);
3751 tree decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3752 varpool_node::finalize_decl (decl);
3753 return decl;
3756 class GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3757 public:
3758 class constant_descriptor_rtx *next;
3759 rtx mem;
3760 rtx sym;
3761 rtx constant;
3762 HOST_WIDE_INT offset;
3763 hashval_t hash;
3764 fixed_size_mode mode;
3765 unsigned int align;
3766 int labelno;
3767 int mark;
3770 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3772 static hashval_t hash (constant_descriptor_rtx *);
3773 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3776 /* Used in the hash tables to avoid outputting the same constant
3777 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3778 are output once per function, not once per file. */
3779 /* ??? Only a few targets need per-function constant pools. Most
3780 can use one per-file pool. Should add a targetm bit to tell the
3781 difference. */
3783 struct GTY(()) rtx_constant_pool {
3784 /* Pointers to first and last constant in pool, as ordered by offset. */
3785 class constant_descriptor_rtx *first;
3786 class constant_descriptor_rtx *last;
3788 /* Hash facility for making memory-constants from constant rtl-expressions.
3789 It is used on RISC machines where immediate integer arguments and
3790 constant addresses are restricted so that such constants must be stored
3791 in memory. */
3792 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3794 /* Current offset in constant pool (does not include any
3795 machine-specific header). */
3796 HOST_WIDE_INT offset;
3799 /* Hash and compare functions for const_rtx_htab. */
3801 hashval_t
3802 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3804 return desc->hash;
3807 bool
3808 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3809 constant_descriptor_rtx *y)
3811 if (x->mode != y->mode)
3812 return false;
3813 return rtx_equal_p (x->constant, y->constant);
3816 /* Hash one component of a constant. */
3818 static hashval_t
3819 const_rtx_hash_1 (const_rtx x)
3821 unsigned HOST_WIDE_INT hwi;
3822 machine_mode mode;
3823 enum rtx_code code;
3824 hashval_t h;
3825 int i;
3827 code = GET_CODE (x);
3828 mode = GET_MODE (x);
3829 h = (hashval_t) code * 1048573 + mode;
3831 switch (code)
3833 case CONST_INT:
3834 hwi = INTVAL (x);
3836 fold_hwi:
3838 int shift = sizeof (hashval_t) * CHAR_BIT;
3839 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3841 h ^= (hashval_t) hwi;
3842 for (i = 1; i < n; ++i)
3844 hwi >>= shift;
3845 h ^= (hashval_t) hwi;
3848 break;
3850 case CONST_WIDE_INT:
3851 hwi = 0;
3853 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3854 hwi ^= CONST_WIDE_INT_ELT (x, i);
3855 goto fold_hwi;
3858 case CONST_DOUBLE:
3859 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3861 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3862 goto fold_hwi;
3864 else
3865 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3866 break;
3868 case CONST_FIXED:
3869 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3870 break;
3872 case SYMBOL_REF:
3873 h ^= htab_hash_string (XSTR (x, 0));
3874 break;
3876 case LABEL_REF:
3877 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3878 break;
3880 case UNSPEC:
3881 case UNSPEC_VOLATILE:
3882 h = h * 251 + XINT (x, 1);
3883 break;
3885 default:
3886 break;
3889 return h;
3892 /* Compute a hash value for X, which should be a constant. */
3894 static hashval_t
3895 const_rtx_hash (rtx x)
3897 hashval_t h = 0;
3898 subrtx_iterator::array_type array;
3899 FOR_EACH_SUBRTX (iter, array, x, ALL)
3900 h = h * 509 + const_rtx_hash_1 (*iter);
3901 return h;
3905 /* Create and return a new rtx constant pool. */
3907 static struct rtx_constant_pool *
3908 create_constant_pool (void)
3910 struct rtx_constant_pool *pool;
3912 pool = ggc_alloc<rtx_constant_pool> ();
3913 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3914 pool->first = NULL;
3915 pool->last = NULL;
3916 pool->offset = 0;
3917 return pool;
3920 /* Initialize constant pool hashing for a new function. */
3922 void
3923 init_varasm_status (void)
3925 crtl->varasm.pool = create_constant_pool ();
3926 crtl->varasm.deferred_constants = 0;
3929 /* Given a MINUS expression, simplify it if both sides
3930 include the same symbol. */
3933 simplify_subtraction (rtx x)
3935 rtx r = simplify_rtx (x);
3936 return r ? r : x;
3939 /* Given a constant rtx X, make (or find) a memory constant for its value
3940 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
3941 of X. */
3944 force_const_mem (machine_mode in_mode, rtx x)
3946 class constant_descriptor_rtx *desc, tmp;
3947 struct rtx_constant_pool *pool;
3948 char label[256];
3949 rtx def, symbol;
3950 hashval_t hash;
3951 unsigned int align;
3952 constant_descriptor_rtx **slot;
3953 fixed_size_mode mode;
3955 /* We can't force variable-sized objects to memory. */
3956 if (!is_a <fixed_size_mode> (in_mode, &mode))
3957 return NULL_RTX;
3959 /* If we're not allowed to drop X into the constant pool, don't. */
3960 if (targetm.cannot_force_const_mem (mode, x))
3961 return NULL_RTX;
3963 /* Record that this function has used a constant pool entry. */
3964 crtl->uses_const_pool = 1;
3966 /* Decide which pool to use. */
3967 pool = (targetm.use_blocks_for_constant_p (mode, x)
3968 ? shared_constant_pool
3969 : crtl->varasm.pool);
3971 /* Lookup the value in the hashtable. */
3972 tmp.constant = x;
3973 tmp.mode = mode;
3974 hash = const_rtx_hash (x);
3975 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3976 desc = *slot;
3978 /* If the constant was already present, return its memory. */
3979 if (desc)
3980 return copy_rtx (desc->mem);
3982 /* Otherwise, create a new descriptor. */
3983 desc = ggc_alloc<constant_descriptor_rtx> ();
3984 *slot = desc;
3986 /* Align the location counter as required by EXP's data type. */
3987 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3988 align = targetm.static_rtx_alignment (align_mode);
3990 pool->offset += (align / BITS_PER_UNIT) - 1;
3991 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3993 desc->next = NULL;
3994 desc->constant = copy_rtx (tmp.constant);
3995 desc->offset = pool->offset;
3996 desc->hash = hash;
3997 desc->mode = mode;
3998 desc->align = align;
3999 desc->labelno = const_labelno;
4000 desc->mark = 0;
4002 pool->offset += GET_MODE_SIZE (mode);
4003 if (pool->last)
4004 pool->last->next = desc;
4005 else
4006 pool->first = pool->last = desc;
4007 pool->last = desc;
4009 /* Create a string containing the label name, in LABEL. */
4010 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
4011 ++const_labelno;
4013 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
4014 the constants pool. */
4015 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
4017 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
4018 symbol = create_block_symbol (ggc_strdup (label),
4019 get_block_for_section (sect), -1);
4021 else
4022 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
4023 desc->sym = symbol;
4024 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
4025 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
4026 SET_SYMBOL_REF_CONSTANT (symbol, desc);
4028 /* Construct the MEM. */
4029 desc->mem = def = gen_const_mem (mode, symbol);
4030 set_mem_align (def, align);
4032 /* If we're dropping a label to the constant pool, make sure we
4033 don't delete it. */
4034 if (GET_CODE (x) == LABEL_REF)
4035 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
4037 return copy_rtx (def);
4040 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
4043 get_pool_constant (const_rtx addr)
4045 return SYMBOL_REF_CONSTANT (addr)->constant;
4048 /* Given a constant pool SYMBOL_REF, return the corresponding constant
4049 and whether it has been output or not. */
4052 get_pool_constant_mark (rtx addr, bool *pmarked)
4054 class constant_descriptor_rtx *desc;
4056 desc = SYMBOL_REF_CONSTANT (addr);
4057 *pmarked = (desc->mark != 0);
4058 return desc->constant;
4061 /* Similar, return the mode. */
4063 fixed_size_mode
4064 get_pool_mode (const_rtx addr)
4066 return SYMBOL_REF_CONSTANT (addr)->mode;
4069 /* Return TRUE if and only if the constant pool has no entries. Note
4070 that even entries we might end up choosing not to emit are counted
4071 here, so there is the potential for missed optimizations. */
4073 bool
4074 constant_pool_empty_p (void)
4076 return crtl->varasm.pool->first == NULL;
4079 /* Worker function for output_constant_pool_1. Emit assembly for X
4080 in MODE with known alignment ALIGN. */
4082 static void
4083 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
4085 switch (GET_MODE_CLASS (mode))
4087 case MODE_FLOAT:
4088 case MODE_DECIMAL_FLOAT:
4090 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
4091 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
4092 as_a <scalar_float_mode> (mode), align, false);
4093 break;
4096 case MODE_INT:
4097 case MODE_PARTIAL_INT:
4098 case MODE_FRACT:
4099 case MODE_UFRACT:
4100 case MODE_ACCUM:
4101 case MODE_UACCUM:
4102 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
4103 break;
4105 case MODE_VECTOR_BOOL:
4107 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4109 /* Pick the smallest integer mode that contains at least one
4110 whole element. Often this is byte_mode and contains more
4111 than one element. */
4112 unsigned int nelts = GET_MODE_NUNITS (mode);
4113 unsigned int elt_bits = GET_MODE_PRECISION (mode) / nelts;
4114 unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
4115 scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
4116 unsigned int mask = GET_MODE_MASK (GET_MODE_INNER (mode));
4118 /* We allow GET_MODE_PRECISION (mode) <= GET_MODE_BITSIZE (mode) but
4119 only properly handle cases where the difference is less than a
4120 byte. */
4121 gcc_assert (GET_MODE_BITSIZE (mode) - GET_MODE_PRECISION (mode) <
4122 BITS_PER_UNIT);
4124 /* Build the constant up one integer at a time. */
4125 unsigned int elts_per_int = int_bits / elt_bits;
4126 for (unsigned int i = 0; i < nelts; i += elts_per_int)
4128 unsigned HOST_WIDE_INT value = 0;
4129 unsigned int limit = MIN (nelts - i, elts_per_int);
4130 for (unsigned int j = 0; j < limit; ++j)
4132 auto elt = INTVAL (CONST_VECTOR_ELT (x, i + j));
4133 value |= (elt & mask) << (j * elt_bits);
4135 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
4136 i != 0 ? MIN (align, int_bits) : align);
4138 break;
4140 case MODE_VECTOR_FLOAT:
4141 case MODE_VECTOR_INT:
4142 case MODE_VECTOR_FRACT:
4143 case MODE_VECTOR_UFRACT:
4144 case MODE_VECTOR_ACCUM:
4145 case MODE_VECTOR_UACCUM:
4147 int i, units;
4148 scalar_mode submode = GET_MODE_INNER (mode);
4149 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4151 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4152 units = GET_MODE_NUNITS (mode);
4154 for (i = 0; i < units; i++)
4156 rtx elt = CONST_VECTOR_ELT (x, i);
4157 output_constant_pool_2 (submode, elt, i ? subalign : align);
4160 break;
4162 default:
4163 gcc_unreachable ();
4167 /* Worker function for output_constant_pool. Emit constant DESC,
4168 giving it ALIGN bits of alignment. */
4170 static void
4171 output_constant_pool_1 (class constant_descriptor_rtx *desc,
4172 unsigned int align)
4174 rtx x, tmp;
4176 x = desc->constant;
4178 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4179 whose CODE_LABEL has been deleted. This can occur if a jump table
4180 is eliminated by optimization. If so, write a constant of zero
4181 instead. Note that this can also happen by turning the
4182 CODE_LABEL into a NOTE. */
4183 /* ??? This seems completely and utterly wrong. Certainly it's
4184 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4185 functioning even with rtx_insn::deleted and friends. */
4187 tmp = x;
4188 switch (GET_CODE (tmp))
4190 case CONST:
4191 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4192 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4193 break;
4194 tmp = XEXP (XEXP (tmp, 0), 0);
4195 /* FALLTHRU */
4197 case LABEL_REF:
4199 rtx_insn *insn = label_ref_label (tmp);
4200 gcc_assert (!insn->deleted ());
4201 gcc_assert (!NOTE_P (insn)
4202 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4203 break;
4206 default:
4207 break;
4210 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4211 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4212 align, desc->labelno, done);
4213 #endif
4215 assemble_align (align);
4217 /* Output the label. */
4218 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4220 /* Output the data.
4221 Pass actual alignment value while emitting string constant to asm code
4222 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4223 assuming that the data is already aligned which prevents the generation
4224 of fix-up table entries. */
4225 output_constant_pool_2 (desc->mode, x, desc->align);
4227 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4228 sections have proper size. */
4229 if (align > GET_MODE_BITSIZE (desc->mode)
4230 && in_section
4231 && (in_section->common.flags & SECTION_MERGE))
4232 assemble_align (align);
4234 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4235 done:
4236 #endif
4237 return;
4240 /* Recompute the offsets of entries in POOL, and the overall size of
4241 POOL. Do this after calling mark_constant_pool to ensure that we
4242 are computing the offset values for the pool which we will actually
4243 emit. */
4245 static void
4246 recompute_pool_offsets (struct rtx_constant_pool *pool)
4248 class constant_descriptor_rtx *desc;
4249 pool->offset = 0;
4251 for (desc = pool->first; desc ; desc = desc->next)
4252 if (desc->mark)
4254 /* Recalculate offset. */
4255 unsigned int align = desc->align;
4256 pool->offset += (align / BITS_PER_UNIT) - 1;
4257 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4258 desc->offset = pool->offset;
4259 pool->offset += GET_MODE_SIZE (desc->mode);
4263 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4264 Emit referenced deferred strings. */
4266 static void
4267 mark_constants_in_pattern (rtx insn)
4269 subrtx_iterator::array_type array;
4270 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4272 const_rtx x = *iter;
4273 if (GET_CODE (x) == SYMBOL_REF)
4275 if (CONSTANT_POOL_ADDRESS_P (x))
4277 class constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4278 if (desc->mark == 0)
4280 desc->mark = 1;
4281 iter.substitute (desc->constant);
4284 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4286 tree decl = SYMBOL_REF_DECL (x);
4287 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4289 n_deferred_constants--;
4290 output_constant_def_contents (CONST_CAST_RTX (x));
4297 /* Look through appropriate parts of INSN, marking all entries in the
4298 constant pool which are actually being used. Entries that are only
4299 referenced by other constants are also marked as used. Emit
4300 deferred strings that are used. */
4302 static void
4303 mark_constants (rtx_insn *insn)
4305 if (!INSN_P (insn))
4306 return;
4308 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4309 insns, not any notes that may be attached. We don't want to mark
4310 a constant just because it happens to appear in a REG_EQUIV note. */
4311 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4313 int i, n = seq->len ();
4314 for (i = 0; i < n; ++i)
4316 rtx subinsn = seq->element (i);
4317 if (INSN_P (subinsn))
4318 mark_constants_in_pattern (subinsn);
4321 else
4322 mark_constants_in_pattern (insn);
4325 /* Look through the instructions for this function, and mark all the
4326 entries in POOL which are actually being used. Emit deferred constants
4327 which have indeed been used. */
4329 static void
4330 mark_constant_pool (void)
4332 rtx_insn *insn;
4334 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4335 return;
4337 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4338 mark_constants (insn);
4341 /* Write all the constants in POOL. */
4343 static void
4344 output_constant_pool_contents (struct rtx_constant_pool *pool)
4346 class constant_descriptor_rtx *desc;
4348 for (desc = pool->first; desc ; desc = desc->next)
4349 if (desc->mark < 0)
4351 #ifdef ASM_OUTPUT_DEF
4352 gcc_checking_assert (TARGET_SUPPORTS_ALIASES);
4354 const char *name = XSTR (desc->sym, 0);
4355 char label[256];
4356 char buffer[256 + 32];
4357 const char *p;
4359 ASM_GENERATE_INTERNAL_LABEL (label, "LC", ~desc->mark);
4360 p = label;
4361 if (desc->offset)
4363 sprintf (buffer, "%s+%ld", p, (long) (desc->offset));
4364 p = buffer;
4366 ASM_OUTPUT_DEF (asm_out_file, name, p);
4367 #else
4368 gcc_unreachable ();
4369 #endif
4371 else if (desc->mark)
4373 /* If the constant is part of an object_block, make sure that
4374 the constant has been positioned within its block, but do not
4375 write out its definition yet. output_object_blocks will do
4376 that later. */
4377 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4378 && SYMBOL_REF_BLOCK (desc->sym))
4379 place_block_symbol (desc->sym);
4380 else
4382 switch_to_section (targetm.asm_out.select_rtx_section
4383 (desc->mode, desc->constant, desc->align));
4384 output_constant_pool_1 (desc, desc->align);
4389 struct constant_descriptor_rtx_data {
4390 constant_descriptor_rtx *desc;
4391 target_unit *bytes;
4392 unsigned short size;
4393 unsigned short offset;
4394 unsigned int hash;
4397 /* qsort callback to sort constant_descriptor_rtx_data * vector by
4398 decreasing size. */
4400 static int
4401 constant_descriptor_rtx_data_cmp (const void *p1, const void *p2)
4403 constant_descriptor_rtx_data *const data1
4404 = *(constant_descriptor_rtx_data * const *) p1;
4405 constant_descriptor_rtx_data *const data2
4406 = *(constant_descriptor_rtx_data * const *) p2;
4407 if (data1->size > data2->size)
4408 return -1;
4409 if (data1->size < data2->size)
4410 return 1;
4411 if (data1->hash < data2->hash)
4412 return -1;
4413 gcc_assert (data1->hash > data2->hash);
4414 return 1;
4417 struct const_rtx_data_hasher : nofree_ptr_hash<constant_descriptor_rtx_data>
4419 static hashval_t hash (constant_descriptor_rtx_data *);
4420 static bool equal (constant_descriptor_rtx_data *,
4421 constant_descriptor_rtx_data *);
4424 /* Hash and compare functions for const_rtx_data_htab. */
4426 hashval_t
4427 const_rtx_data_hasher::hash (constant_descriptor_rtx_data *data)
4429 return data->hash;
4432 bool
4433 const_rtx_data_hasher::equal (constant_descriptor_rtx_data *x,
4434 constant_descriptor_rtx_data *y)
4436 if (x->hash != y->hash || x->size != y->size)
4437 return false;
4438 unsigned int align1 = x->desc->align;
4439 unsigned int align2 = y->desc->align;
4440 unsigned int offset1 = (x->offset * BITS_PER_UNIT) & (align1 - 1);
4441 unsigned int offset2 = (y->offset * BITS_PER_UNIT) & (align2 - 1);
4442 if (offset1)
4443 align1 = least_bit_hwi (offset1);
4444 if (offset2)
4445 align2 = least_bit_hwi (offset2);
4446 if (align2 > align1)
4447 return false;
4448 if (memcmp (x->bytes, y->bytes, x->size * sizeof (target_unit)) != 0)
4449 return false;
4450 return true;
4453 /* Attempt to optimize constant pool POOL. If it contains both CONST_VECTOR
4454 constants and scalar constants with the values of CONST_VECTOR elements,
4455 try to alias the scalar constants with the CONST_VECTOR elements. */
4457 static void
4458 optimize_constant_pool (struct rtx_constant_pool *pool)
4460 auto_vec<target_unit, 128> buffer;
4461 auto_vec<constant_descriptor_rtx_data *, 128> vec;
4462 object_allocator<constant_descriptor_rtx_data>
4463 data_pool ("constant_descriptor_rtx_data_pool");
4464 int idx = 0;
4465 size_t size = 0;
4466 for (constant_descriptor_rtx *desc = pool->first; desc; desc = desc->next)
4467 if (desc->mark > 0
4468 && ! (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4469 && SYMBOL_REF_BLOCK (desc->sym)))
4471 buffer.truncate (0);
4472 buffer.reserve (GET_MODE_SIZE (desc->mode));
4473 if (native_encode_rtx (desc->mode, desc->constant, buffer, 0,
4474 GET_MODE_SIZE (desc->mode)))
4476 constant_descriptor_rtx_data *data = data_pool.allocate ();
4477 data->desc = desc;
4478 data->bytes = NULL;
4479 data->size = GET_MODE_SIZE (desc->mode);
4480 data->offset = 0;
4481 data->hash = idx++;
4482 size += data->size;
4483 vec.safe_push (data);
4486 if (idx)
4488 vec.qsort (constant_descriptor_rtx_data_cmp);
4489 unsigned min_size = vec.last ()->size;
4490 target_unit *bytes = XNEWVEC (target_unit, size);
4491 unsigned int i;
4492 constant_descriptor_rtx_data *data;
4493 hash_table<const_rtx_data_hasher> * htab
4494 = new hash_table<const_rtx_data_hasher> (31);
4495 size = 0;
4496 FOR_EACH_VEC_ELT (vec, i, data)
4498 buffer.truncate (0);
4499 native_encode_rtx (data->desc->mode, data->desc->constant,
4500 buffer, 0, data->size);
4501 memcpy (bytes + size, buffer.address (), data->size);
4502 data->bytes = bytes + size;
4503 data->hash = iterative_hash (data->bytes,
4504 data->size * sizeof (target_unit), 0);
4505 size += data->size;
4506 constant_descriptor_rtx_data **slot
4507 = htab->find_slot_with_hash (data, data->hash, INSERT);
4508 if (*slot)
4510 data->desc->mark = ~(*slot)->desc->labelno;
4511 data->desc->offset = (*slot)->offset;
4513 else
4515 unsigned int sz = 1 << floor_log2 (data->size);
4517 *slot = data;
4518 for (sz >>= 1; sz >= min_size; sz >>= 1)
4519 for (unsigned off = 0; off + sz <= data->size; off += sz)
4521 constant_descriptor_rtx_data tmp;
4522 tmp.desc = data->desc;
4523 tmp.bytes = data->bytes + off;
4524 tmp.size = sz;
4525 tmp.offset = off;
4526 tmp.hash = iterative_hash (tmp.bytes,
4527 sz * sizeof (target_unit), 0);
4528 slot = htab->find_slot_with_hash (&tmp, tmp.hash, INSERT);
4529 if (*slot == NULL)
4531 *slot = data_pool.allocate ();
4532 **slot = tmp;
4537 delete htab;
4538 XDELETE (bytes);
4540 data_pool.release ();
4543 /* Mark all constants that are used in the current function, then write
4544 out the function's private constant pool. */
4546 static void
4547 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4548 tree fndecl ATTRIBUTE_UNUSED)
4550 struct rtx_constant_pool *pool = crtl->varasm.pool;
4552 /* It is possible for gcc to call force_const_mem and then to later
4553 discard the instructions which refer to the constant. In such a
4554 case we do not need to output the constant. */
4555 mark_constant_pool ();
4557 /* Having marked the constant pool entries we'll actually emit, we
4558 now need to rebuild the offset information, which may have become
4559 stale. */
4560 recompute_pool_offsets (pool);
4562 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4563 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4564 #endif
4566 output_constant_pool_contents (pool);
4568 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4569 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4570 #endif
4573 /* Write the contents of the shared constant pool. */
4575 void
4576 output_shared_constant_pool (void)
4578 if (optimize
4579 && TARGET_SUPPORTS_ALIASES)
4580 optimize_constant_pool (shared_constant_pool);
4582 output_constant_pool_contents (shared_constant_pool);
4585 /* Determine what kind of relocations EXP may need. */
4588 compute_reloc_for_constant (tree exp)
4590 int reloc = 0, reloc2;
4591 tree tem;
4593 switch (TREE_CODE (exp))
4595 case ADDR_EXPR:
4596 case FDESC_EXPR:
4597 /* Go inside any operations that get_inner_reference can handle and see
4598 if what's inside is a constant: no need to do anything here for
4599 addresses of variables or functions. */
4600 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4601 tem = TREE_OPERAND (tem, 0))
4604 if (TREE_CODE (tem) == MEM_REF
4605 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4607 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4608 break;
4611 if (!targetm.binds_local_p (tem))
4612 reloc |= 2;
4613 else
4614 reloc |= 1;
4615 break;
4617 case PLUS_EXPR:
4618 case POINTER_PLUS_EXPR:
4619 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4620 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4621 break;
4623 case MINUS_EXPR:
4624 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4625 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4626 /* The difference of two local labels is computable at link time. */
4627 if (reloc == 1 && reloc2 == 1)
4628 reloc = 0;
4629 else
4630 reloc |= reloc2;
4631 break;
4633 CASE_CONVERT:
4634 case VIEW_CONVERT_EXPR:
4635 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4636 break;
4638 case CONSTRUCTOR:
4640 unsigned HOST_WIDE_INT idx;
4641 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4642 if (tem != 0)
4643 reloc |= compute_reloc_for_constant (tem);
4645 break;
4647 default:
4648 break;
4650 return reloc;
4653 /* Find all the constants whose addresses are referenced inside of EXP,
4654 and make sure assembler code with a label has been output for each one.
4655 Indicate whether an ADDR_EXPR has been encountered. */
4657 static void
4658 output_addressed_constants (tree exp, int defer)
4660 tree tem;
4662 switch (TREE_CODE (exp))
4664 case ADDR_EXPR:
4665 case FDESC_EXPR:
4666 /* Go inside any operations that get_inner_reference can handle and see
4667 if what's inside is a constant: no need to do anything here for
4668 addresses of variables or functions. */
4669 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4670 tem = TREE_OPERAND (tem, 0))
4673 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4674 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4675 tem = DECL_INITIAL (tem);
4677 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4678 output_constant_def (tem, defer);
4680 if (TREE_CODE (tem) == MEM_REF)
4681 output_addressed_constants (TREE_OPERAND (tem, 0), defer);
4682 break;
4684 case PLUS_EXPR:
4685 case POINTER_PLUS_EXPR:
4686 case MINUS_EXPR:
4687 output_addressed_constants (TREE_OPERAND (exp, 1), defer);
4688 gcc_fallthrough ();
4690 CASE_CONVERT:
4691 case VIEW_CONVERT_EXPR:
4692 output_addressed_constants (TREE_OPERAND (exp, 0), defer);
4693 break;
4695 case CONSTRUCTOR:
4697 unsigned HOST_WIDE_INT idx;
4698 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4699 if (tem != 0)
4700 output_addressed_constants (tem, defer);
4702 break;
4704 default:
4705 break;
4709 /* Whether a constructor CTOR is a valid static constant initializer if all
4710 its elements are. This used to be internal to initializer_constant_valid_p
4711 and has been exposed to let other functions like categorize_ctor_elements
4712 evaluate the property while walking a constructor for other purposes. */
4714 bool
4715 constructor_static_from_elts_p (const_tree ctor)
4717 return (TREE_CONSTANT (ctor)
4718 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4719 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4720 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4723 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4724 tree *cache);
4726 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4727 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4728 which are valid when ENDTYPE is an integer of any size; in
4729 particular, this does not accept a pointer minus a constant. This
4730 returns null_pointer_node if the VALUE is an absolute constant
4731 which can be used to initialize a static variable. Otherwise it
4732 returns NULL. */
4734 static tree
4735 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4737 tree op0, op1;
4739 if (!INTEGRAL_TYPE_P (endtype))
4740 return NULL_TREE;
4742 op0 = TREE_OPERAND (value, 0);
4743 op1 = TREE_OPERAND (value, 1);
4745 /* Like STRIP_NOPS except allow the operand mode to widen. This
4746 works around a feature of fold that simplifies (int)(p1 - p2) to
4747 ((int)p1 - (int)p2) under the theory that the narrower operation
4748 is cheaper. */
4750 while (CONVERT_EXPR_P (op0)
4751 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4753 tree inner = TREE_OPERAND (op0, 0);
4754 if (inner == error_mark_node
4755 || ! INTEGRAL_TYPE_P (TREE_TYPE (op0))
4756 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (op0)))
4757 || ! INTEGRAL_TYPE_P (TREE_TYPE (inner))
4758 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4759 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4760 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4761 break;
4762 op0 = inner;
4765 while (CONVERT_EXPR_P (op1)
4766 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4768 tree inner = TREE_OPERAND (op1, 0);
4769 if (inner == error_mark_node
4770 || ! INTEGRAL_TYPE_P (TREE_TYPE (op1))
4771 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (op1)))
4772 || ! INTEGRAL_TYPE_P (TREE_TYPE (inner))
4773 || ! SCALAR_INT_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4774 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4775 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4776 break;
4777 op1 = inner;
4780 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4781 if (!op0)
4782 return NULL_TREE;
4784 op1 = initializer_constant_valid_p_1 (op1, endtype,
4785 cache ? cache + 2 : NULL);
4786 /* Both initializers must be known. */
4787 if (op1)
4789 if (op0 == op1
4790 && (op0 == null_pointer_node
4791 || TREE_CODE (value) == MINUS_EXPR))
4792 return null_pointer_node;
4794 /* Support differences between labels. */
4795 if (TREE_CODE (op0) == LABEL_DECL
4796 && TREE_CODE (op1) == LABEL_DECL)
4797 return null_pointer_node;
4799 if (TREE_CODE (op0) == STRING_CST
4800 && TREE_CODE (op1) == STRING_CST
4801 && operand_equal_p (op0, op1, 1))
4802 return null_pointer_node;
4805 return NULL_TREE;
4808 /* Helper function of initializer_constant_valid_p.
4809 Return nonzero if VALUE is a valid constant-valued expression
4810 for use in initializing a static variable; one that can be an
4811 element of a "constant" initializer.
4813 Return null_pointer_node if the value is absolute;
4814 if it is relocatable, return the variable that determines the relocation.
4815 We assume that VALUE has been folded as much as possible;
4816 therefore, we do not need to check for such things as
4817 arithmetic-combinations of integers.
4819 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4821 static tree
4822 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4824 tree ret;
4826 switch (TREE_CODE (value))
4828 case CONSTRUCTOR:
4829 if (constructor_static_from_elts_p (value))
4831 unsigned HOST_WIDE_INT idx;
4832 tree elt;
4833 bool absolute = true;
4835 if (cache && cache[0] == value)
4836 return cache[1];
4837 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4839 tree reloc;
4840 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4841 NULL);
4842 if (!reloc
4843 /* An absolute value is required with reverse SSO. */
4844 || (reloc != null_pointer_node
4845 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4846 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4848 if (cache)
4850 cache[0] = value;
4851 cache[1] = NULL_TREE;
4853 return NULL_TREE;
4855 if (reloc != null_pointer_node)
4856 absolute = false;
4858 /* For a non-absolute relocation, there is no single
4859 variable that can be "the variable that determines the
4860 relocation." */
4861 if (cache)
4863 cache[0] = value;
4864 cache[1] = absolute ? null_pointer_node : error_mark_node;
4866 return absolute ? null_pointer_node : error_mark_node;
4869 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4871 case INTEGER_CST:
4872 case VECTOR_CST:
4873 case REAL_CST:
4874 case FIXED_CST:
4875 case STRING_CST:
4876 case COMPLEX_CST:
4877 return null_pointer_node;
4879 case ADDR_EXPR:
4880 case FDESC_EXPR:
4882 tree op0 = staticp (TREE_OPERAND (value, 0));
4883 if (op0)
4885 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4886 to be a constant, this is old-skool offsetof-like nonsense. */
4887 if (TREE_CODE (op0) == INDIRECT_REF
4888 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4889 return null_pointer_node;
4890 /* Taking the address of a nested function involves a trampoline,
4891 unless we don't need or want one. */
4892 if (TREE_CODE (op0) == FUNCTION_DECL
4893 && DECL_STATIC_CHAIN (op0)
4894 && !TREE_NO_TRAMPOLINE (value))
4895 return NULL_TREE;
4896 /* "&{...}" requires a temporary to hold the constructed
4897 object. */
4898 if (TREE_CODE (op0) == CONSTRUCTOR)
4899 return NULL_TREE;
4901 return op0;
4904 case NON_LVALUE_EXPR:
4905 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4906 endtype, cache);
4908 case VIEW_CONVERT_EXPR:
4910 tree src = TREE_OPERAND (value, 0);
4911 tree src_type = TREE_TYPE (src);
4912 tree dest_type = TREE_TYPE (value);
4914 /* Allow view-conversions from aggregate to non-aggregate type only
4915 if the bit pattern is fully preserved afterwards; otherwise, the
4916 RTL expander won't be able to apply a subsequent transformation
4917 to the underlying constructor. */
4918 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4920 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4921 return initializer_constant_valid_p_1 (src, endtype, cache);
4922 else
4923 return NULL_TREE;
4926 /* Allow all other kinds of view-conversion. */
4927 return initializer_constant_valid_p_1 (src, endtype, cache);
4930 CASE_CONVERT:
4932 tree src = TREE_OPERAND (value, 0);
4933 tree src_type = TREE_TYPE (src);
4934 tree dest_type = TREE_TYPE (value);
4936 /* Allow conversions between pointer types and offset types. */
4937 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4938 || (TREE_CODE (dest_type) == OFFSET_TYPE
4939 && TREE_CODE (src_type) == OFFSET_TYPE))
4940 return initializer_constant_valid_p_1 (src, endtype, cache);
4942 /* Allow length-preserving conversions between integer types and
4943 floating-point types. */
4944 if (((INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4945 || (SCALAR_FLOAT_TYPE_P (dest_type)
4946 && SCALAR_FLOAT_TYPE_P (src_type)))
4947 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4948 return initializer_constant_valid_p_1 (src, endtype, cache);
4950 /* Allow conversions between other integer types only if
4951 explicit value. Don't allow sign-extension to a type larger
4952 than word and pointer, there aren't relocations that would
4953 allow to sign extend it to a wider type. */
4954 if (INTEGRAL_TYPE_P (dest_type)
4955 && INTEGRAL_TYPE_P (src_type)
4956 && (TYPE_UNSIGNED (src_type)
4957 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4958 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4959 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4961 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4962 if (inner == null_pointer_node)
4963 return null_pointer_node;
4964 break;
4967 /* Allow (int) &foo provided int is as wide as a pointer. */
4968 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4969 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4970 return initializer_constant_valid_p_1 (src, endtype, cache);
4972 /* Likewise conversions from int to pointers, but also allow
4973 conversions from 0. */
4974 if ((POINTER_TYPE_P (dest_type)
4975 || TREE_CODE (dest_type) == OFFSET_TYPE)
4976 && INTEGRAL_TYPE_P (src_type))
4978 if (TREE_CODE (src) == INTEGER_CST
4979 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4980 return null_pointer_node;
4981 if (integer_zerop (src))
4982 return null_pointer_node;
4983 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4984 return initializer_constant_valid_p_1 (src, endtype, cache);
4987 /* Allow conversions to struct or union types if the value
4988 inside is okay. */
4989 if (TREE_CODE (dest_type) == RECORD_TYPE
4990 || TREE_CODE (dest_type) == UNION_TYPE)
4991 return initializer_constant_valid_p_1 (src, endtype, cache);
4993 break;
4995 case POINTER_PLUS_EXPR:
4996 case PLUS_EXPR:
4997 /* Any valid floating-point constants will have been folded by now;
4998 with -frounding-math we hit this with addition of two constants. */
4999 if (TREE_CODE (endtype) == REAL_TYPE)
5000 return NULL_TREE;
5001 if (cache && cache[0] == value)
5002 return cache[1];
5003 if (! INTEGRAL_TYPE_P (endtype)
5004 || ! INTEGRAL_TYPE_P (TREE_TYPE (value))
5005 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
5007 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
5008 tree valid0
5009 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
5010 endtype, ncache);
5011 tree valid1
5012 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
5013 endtype, ncache + 2);
5014 /* If either term is absolute, use the other term's relocation. */
5015 if (valid0 == null_pointer_node)
5016 ret = valid1;
5017 else if (valid1 == null_pointer_node)
5018 ret = valid0;
5019 /* Support narrowing pointer differences. */
5020 else
5021 ret = narrowing_initializer_constant_valid_p (value, endtype,
5022 ncache);
5024 else
5025 /* Support narrowing pointer differences. */
5026 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
5027 if (cache)
5029 cache[0] = value;
5030 cache[1] = ret;
5032 return ret;
5034 case POINTER_DIFF_EXPR:
5035 case MINUS_EXPR:
5036 if (TREE_CODE (endtype) == REAL_TYPE)
5037 return NULL_TREE;
5038 if (cache && cache[0] == value)
5039 return cache[1];
5040 if (! INTEGRAL_TYPE_P (endtype)
5041 || ! INTEGRAL_TYPE_P (TREE_TYPE (value))
5042 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
5044 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
5045 tree valid0
5046 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
5047 endtype, ncache);
5048 tree valid1
5049 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
5050 endtype, ncache + 2);
5051 /* Win if second argument is absolute. */
5052 if (valid1 == null_pointer_node)
5053 ret = valid0;
5054 /* Win if both arguments have the same relocation.
5055 Then the value is absolute. */
5056 else if (valid0 == valid1 && valid0 != 0)
5057 ret = null_pointer_node;
5058 /* Since GCC guarantees that string constants are unique in the
5059 generated code, a subtraction between two copies of the same
5060 constant string is absolute. */
5061 else if (valid0 && TREE_CODE (valid0) == STRING_CST
5062 && valid1 && TREE_CODE (valid1) == STRING_CST
5063 && operand_equal_p (valid0, valid1, 1))
5064 ret = null_pointer_node;
5065 /* Support narrowing differences. */
5066 else
5067 ret = narrowing_initializer_constant_valid_p (value, endtype,
5068 ncache);
5070 else
5071 /* Support narrowing differences. */
5072 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
5073 if (cache)
5075 cache[0] = value;
5076 cache[1] = ret;
5078 return ret;
5080 default:
5081 break;
5084 return NULL_TREE;
5087 /* Return nonzero if VALUE is a valid constant-valued expression
5088 for use in initializing a static variable; one that can be an
5089 element of a "constant" initializer.
5091 Return null_pointer_node if the value is absolute;
5092 if it is relocatable, return the variable that determines the relocation.
5093 We assume that VALUE has been folded as much as possible;
5094 therefore, we do not need to check for such things as
5095 arithmetic-combinations of integers. */
5096 tree
5097 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
5099 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
5101 /* An absolute value is required with reverse storage order. */
5102 if (reloc
5103 && reloc != null_pointer_node
5104 && reverse
5105 && !AGGREGATE_TYPE_P (endtype)
5106 && !VECTOR_TYPE_P (endtype))
5107 reloc = NULL_TREE;
5109 return reloc;
5112 /* Return true if VALUE is a valid constant-valued expression
5113 for use in initializing a static bit-field; one that can be
5114 an element of a "constant" initializer. */
5116 bool
5117 initializer_constant_valid_for_bitfield_p (const_tree value)
5119 /* For bitfields we support integer constants or possibly nested aggregates
5120 of such. */
5121 switch (TREE_CODE (value))
5123 case CONSTRUCTOR:
5125 unsigned HOST_WIDE_INT idx;
5126 const_tree elt;
5128 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
5129 if (!initializer_constant_valid_for_bitfield_p (elt))
5130 return false;
5131 return true;
5134 case INTEGER_CST:
5135 case REAL_CST:
5136 return true;
5138 case VIEW_CONVERT_EXPR:
5139 case NON_LVALUE_EXPR:
5140 return
5141 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
5143 default:
5144 break;
5147 return false;
5150 /* Check if a STRING_CST fits into the field.
5151 Tolerate only the case when the NUL termination
5152 does not fit into the field. */
5154 static bool
5155 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
5157 tree type = TREE_TYPE (string);
5158 tree eltype = TREE_TYPE (type);
5159 unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
5160 unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
5161 int len = TREE_STRING_LENGTH (string);
5163 if (elts != 1 && elts != 2 && elts != 4)
5164 return false;
5165 if (len < 0 || len % elts != 0)
5166 return false;
5167 if (size < (unsigned)len)
5168 return false;
5169 if (mem_size != size)
5170 return false;
5171 return true;
5174 /* output_constructor outer state of relevance in recursive calls, typically
5175 for nested aggregate bitfields. */
5177 struct oc_outer_state {
5178 unsigned int bit_offset; /* current position in ... */
5179 int byte; /* ... the outer byte buffer. */
5182 static unsigned HOST_WIDE_INT
5183 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
5184 oc_outer_state *);
5186 /* Output assembler code for constant EXP, with no label.
5187 This includes the pseudo-op such as ".int" or ".byte", and a newline.
5188 Assumes output_addressed_constants has been done on EXP already.
5190 Generate at least SIZE bytes of assembler data, padding at the end
5191 with zeros if necessary. SIZE must always be specified. The returned
5192 value is the actual number of bytes of assembler data generated, which
5193 may be bigger than SIZE if the object contains a variable length field.
5195 SIZE is important for structure constructors,
5196 since trailing members may have been omitted from the constructor.
5197 It is also important for initialization of arrays from string constants
5198 since the full length of the string constant might not be wanted.
5199 It is also needed for initialization of unions, where the initializer's
5200 type is just one member, and that may not be as long as the union.
5202 There a case in which we would fail to output exactly SIZE bytes:
5203 for a structure constructor that wants to produce more than SIZE bytes.
5204 But such constructors will never be generated for any possible input.
5206 ALIGN is the alignment of the data in bits.
5208 If REVERSE is true, EXP is output in reverse storage order. */
5210 static unsigned HOST_WIDE_INT
5211 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5212 bool reverse, bool merge_strings)
5214 enum tree_code code;
5215 unsigned HOST_WIDE_INT thissize;
5216 rtx cst;
5218 if (size == 0 || flag_syntax_only)
5219 return size;
5221 /* See if we're trying to initialize a pointer in a non-default mode
5222 to the address of some declaration somewhere. If the target says
5223 the mode is valid for pointers, assume the target has a way of
5224 resolving it. */
5225 if (TREE_CODE (exp) == NOP_EXPR
5226 && POINTER_TYPE_P (TREE_TYPE (exp))
5227 && targetm.addr_space.valid_pointer_mode
5228 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5229 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5231 tree saved_type = TREE_TYPE (exp);
5233 /* Peel off any intermediate conversions-to-pointer for valid
5234 pointer modes. */
5235 while (TREE_CODE (exp) == NOP_EXPR
5236 && POINTER_TYPE_P (TREE_TYPE (exp))
5237 && targetm.addr_space.valid_pointer_mode
5238 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
5239 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
5240 exp = TREE_OPERAND (exp, 0);
5242 /* If what we're left with is the address of something, we can
5243 convert the address to the final type and output it that
5244 way. */
5245 if (TREE_CODE (exp) == ADDR_EXPR)
5246 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
5247 /* Likewise for constant ints. */
5248 else if (TREE_CODE (exp) == INTEGER_CST)
5249 exp = fold_convert (saved_type, exp);
5253 /* Eliminate any conversions since we'll be outputting the underlying
5254 constant. */
5255 while (CONVERT_EXPR_P (exp)
5256 || TREE_CODE (exp) == NON_LVALUE_EXPR
5257 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
5259 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
5260 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
5262 /* Make sure eliminating the conversion is really a no-op, except with
5263 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
5264 union types to allow for Ada unchecked unions. */
5265 if (type_size > op_size
5266 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
5267 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
5268 /* Keep the conversion. */
5269 break;
5270 else
5271 exp = TREE_OPERAND (exp, 0);
5274 code = TREE_CODE (TREE_TYPE (exp));
5275 thissize = int_size_in_bytes (TREE_TYPE (exp));
5277 /* Allow a constructor with no elements for any data type.
5278 This means to fill the space with zeros. */
5279 if (TREE_CODE (exp) == CONSTRUCTOR
5280 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
5282 assemble_zeros (size);
5283 return size;
5286 if (TREE_CODE (exp) == FDESC_EXPR)
5288 #ifdef ASM_OUTPUT_FDESC
5289 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
5290 tree decl = TREE_OPERAND (exp, 0);
5291 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
5292 #else
5293 gcc_unreachable ();
5294 #endif
5295 return size;
5298 /* Now output the underlying data. If we've handling the padding, return.
5299 Otherwise, break and ensure SIZE is the size written. */
5300 switch (code)
5302 case BOOLEAN_TYPE:
5303 case INTEGER_TYPE:
5304 case ENUMERAL_TYPE:
5305 case POINTER_TYPE:
5306 case REFERENCE_TYPE:
5307 case OFFSET_TYPE:
5308 case FIXED_POINT_TYPE:
5309 case NULLPTR_TYPE:
5310 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5311 if (reverse)
5312 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5313 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5314 error ("initializer for integer/fixed-point value is too complicated");
5315 break;
5317 case REAL_TYPE:
5318 gcc_assert (size == thissize);
5319 if (TREE_CODE (exp) != REAL_CST)
5320 error ("initializer for floating value is not a floating constant");
5321 else
5322 assemble_real (TREE_REAL_CST (exp),
5323 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
5324 align, reverse);
5325 break;
5327 case COMPLEX_TYPE:
5328 output_constant (TREE_REALPART (exp), thissize / 2, align,
5329 reverse, false);
5330 output_constant (TREE_IMAGPART (exp), thissize / 2,
5331 min_align (align, BITS_PER_UNIT * (thissize / 2)),
5332 reverse, false);
5333 break;
5335 case BITINT_TYPE:
5336 if (TREE_CODE (exp) != INTEGER_CST)
5337 error ("initializer for %<_BitInt(%d)%> value is not an integer "
5338 "constant", TYPE_PRECISION (TREE_TYPE (exp)));
5339 else
5341 struct bitint_info info;
5342 tree type = TREE_TYPE (exp);
5343 bool ok = targetm.c.bitint_type_info (TYPE_PRECISION (type), &info);
5344 gcc_assert (ok);
5345 scalar_int_mode limb_mode
5346 = as_a <scalar_int_mode> (info.abi_limb_mode);
5347 if (TYPE_PRECISION (type) <= GET_MODE_PRECISION (limb_mode))
5349 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
5350 if (reverse)
5351 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
5352 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
5353 error ("initializer for integer/fixed-point value is too "
5354 "complicated");
5355 break;
5357 int prec = GET_MODE_PRECISION (limb_mode);
5358 int cnt = CEIL (TYPE_PRECISION (type), prec);
5359 tree limb_type = build_nonstandard_integer_type (prec, 1);
5360 int elt_size = GET_MODE_SIZE (limb_mode);
5361 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (limb_mode));
5362 thissize = 0;
5363 if (prec == HOST_BITS_PER_WIDE_INT)
5364 for (int i = 0; i < cnt; i++)
5366 int idx = (info.big_endian ^ reverse) ? cnt - 1 - i : i;
5367 tree c;
5368 if (idx >= TREE_INT_CST_EXT_NUNITS (exp))
5369 c = build_int_cst (limb_type,
5370 tree_int_cst_sgn (exp) < 0 ? -1 : 0);
5371 else
5372 c = build_int_cst (limb_type,
5373 TREE_INT_CST_ELT (exp, idx));
5374 output_constant (c, elt_size, nalign, reverse, false);
5375 thissize += elt_size;
5377 else
5378 for (int i = 0; i < cnt; i++)
5380 int idx = (info.big_endian ^ reverse) ? cnt - 1 - i : i;
5381 wide_int w = wi::rshift (wi::to_wide (exp), idx * prec,
5382 TYPE_SIGN (TREE_TYPE (exp)));
5383 tree c = wide_int_to_tree (limb_type,
5384 wide_int::from (w, prec, UNSIGNED));
5385 output_constant (c, elt_size, nalign, reverse, false);
5386 thissize += elt_size;
5389 break;
5391 case ARRAY_TYPE:
5392 case VECTOR_TYPE:
5393 switch (TREE_CODE (exp))
5395 case CONSTRUCTOR:
5396 return output_constructor (exp, size, align, reverse, NULL);
5397 case STRING_CST:
5398 thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5399 if (merge_strings
5400 && (thissize == 0
5401 || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5402 thissize++;
5403 gcc_checking_assert (check_string_literal (exp, size));
5404 assemble_string (TREE_STRING_POINTER (exp), thissize);
5405 break;
5406 case VECTOR_CST:
5408 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5409 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5410 int elt_size = GET_MODE_SIZE (inner);
5411 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5412 reverse, false);
5413 thissize = elt_size;
5414 /* Static constants must have a fixed size. */
5415 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5416 for (unsigned int i = 1; i < nunits; i++)
5418 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5419 reverse, false);
5420 thissize += elt_size;
5422 break;
5424 default:
5425 gcc_unreachable ();
5427 break;
5429 case RECORD_TYPE:
5430 case UNION_TYPE:
5431 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5432 return output_constructor (exp, size, align, reverse, NULL);
5434 case ERROR_MARK:
5435 return 0;
5437 default:
5438 gcc_unreachable ();
5441 if (size > thissize)
5442 assemble_zeros (size - thissize);
5444 return size;
5447 /* Subroutine of output_constructor, used for computing the size of
5448 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
5449 type with an unspecified upper bound. */
5451 static unsigned HOST_WIDE_INT
5452 array_size_for_constructor (tree val)
5454 tree max_index;
5455 unsigned HOST_WIDE_INT cnt;
5456 tree index, value, tmp;
5457 offset_int i;
5459 /* This code used to attempt to handle string constants that are not
5460 arrays of single-bytes, but nothing else does, so there's no point in
5461 doing it here. */
5462 if (TREE_CODE (val) == STRING_CST)
5463 return TREE_STRING_LENGTH (val);
5465 max_index = NULL_TREE;
5466 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5468 if (TREE_CODE (index) == RANGE_EXPR)
5469 index = TREE_OPERAND (index, 1);
5470 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5471 max_index = index;
5474 if (max_index == NULL_TREE)
5475 return 0;
5477 /* Compute the total number of array elements. */
5478 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5479 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5481 /* Multiply by the array element unit size to find number of bytes. */
5482 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5484 gcc_assert (wi::fits_uhwi_p (i));
5485 return i.to_uhwi ();
5488 /* Other datastructures + helpers for output_constructor. */
5490 /* output_constructor local state to support interaction with helpers. */
5492 struct oc_local_state {
5494 /* Received arguments. */
5495 tree exp; /* Constructor expression. */
5496 tree type; /* Type of constructor expression. */
5497 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5498 unsigned int align; /* Known initial alignment. */
5499 tree min_index; /* Lower bound if specified for an array. */
5501 /* Output processing state. */
5502 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5503 int byte; /* Part of a bitfield byte yet to be output. */
5504 int last_relative_index; /* Implicit or explicit index of the last
5505 array element output within a bitfield. */
5506 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5507 bool reverse; /* Whether reverse storage order is in use. */
5509 /* Current element. */
5510 tree field; /* Current field decl in a record. */
5511 tree val; /* Current element value. */
5512 tree index; /* Current element index. */
5516 /* Helper for output_constructor. From the current LOCAL state, output a
5517 RANGE_EXPR element. */
5519 static void
5520 output_constructor_array_range (oc_local_state *local)
5522 /* Perform the index calculation in modulo arithmetic but
5523 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5524 but we are using an unsigned sizetype. */
5525 unsigned prec = TYPE_PRECISION (sizetype);
5526 offset_int idx = wi::sext (wi::to_offset (TREE_OPERAND (local->index, 0))
5527 - wi::to_offset (local->min_index), prec);
5528 tree valtype = TREE_TYPE (local->val);
5529 HOST_WIDE_INT fieldpos
5530 = (idx * wi::to_offset (TYPE_SIZE_UNIT (valtype))).to_short_addr ();
5532 /* Advance to offset of this element. */
5533 if (fieldpos > local->total_bytes)
5535 assemble_zeros (fieldpos - local->total_bytes);
5536 local->total_bytes = fieldpos;
5538 else
5539 /* Must not go backwards. */
5540 gcc_assert (fieldpos == local->total_bytes);
5542 unsigned HOST_WIDE_INT fieldsize
5543 = int_size_in_bytes (TREE_TYPE (local->type));
5545 HOST_WIDE_INT lo_index
5546 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5547 HOST_WIDE_INT hi_index
5548 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5549 HOST_WIDE_INT index;
5551 unsigned int align2
5552 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5554 for (index = lo_index; index <= hi_index; index++)
5556 /* Output the element's initial value. */
5557 if (local->val == NULL_TREE)
5558 assemble_zeros (fieldsize);
5559 else
5560 fieldsize = output_constant (local->val, fieldsize, align2,
5561 local->reverse, false);
5563 /* Count its size. */
5564 local->total_bytes += fieldsize;
5568 /* Helper for output_constructor. From the current LOCAL state, output a
5569 field element that is not true bitfield or part of an outer one. */
5571 static void
5572 output_constructor_regular_field (oc_local_state *local)
5574 /* Field size and position. Since this structure is static, we know the
5575 positions are constant. */
5576 unsigned HOST_WIDE_INT fieldsize;
5577 HOST_WIDE_INT fieldpos;
5579 unsigned int align2;
5581 /* Output any buffered-up bit-fields preceding this element. */
5582 if (local->byte_buffer_in_use)
5584 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5585 local->total_bytes++;
5586 local->byte_buffer_in_use = false;
5589 if (local->index != NULL_TREE)
5591 /* Perform the index calculation in modulo arithmetic but
5592 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5593 but we are using an unsigned sizetype. */
5594 unsigned prec = TYPE_PRECISION (sizetype);
5595 offset_int idx = wi::sext (wi::to_offset (local->index)
5596 - wi::to_offset (local->min_index), prec);
5597 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5598 .to_short_addr ();
5600 else if (local->field != NULL_TREE)
5601 fieldpos = int_byte_position (local->field);
5602 else
5603 fieldpos = 0;
5605 /* Advance to offset of this element.
5606 Note no alignment needed in an array, since that is guaranteed
5607 if each element has the proper size. */
5608 if (local->field != NULL_TREE || local->index != NULL_TREE)
5610 if (fieldpos > local->total_bytes)
5612 assemble_zeros (fieldpos - local->total_bytes);
5613 local->total_bytes = fieldpos;
5615 else
5616 /* Must not go backwards. */
5617 gcc_assert (fieldpos == local->total_bytes);
5620 /* Find the alignment of this element. */
5621 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5623 /* Determine size this element should occupy. */
5624 if (local->field)
5626 fieldsize = 0;
5628 /* If this is an array with an unspecified upper bound,
5629 the initializer determines the size. */
5630 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5631 but we cannot do this until the deprecated support for
5632 initializing zero-length array members is removed. */
5633 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5634 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5635 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5637 unsigned HOST_WIDE_INT fldsize
5638 = array_size_for_constructor (local->val);
5639 fieldsize = int_size_in_bytes (TREE_TYPE (local->val));
5640 /* In most cases fieldsize == fldsize as the size of the initializer
5641 determines how many elements the flexible array member has. For
5642 C++ fldsize can be smaller though, if the last or several last or
5643 all initializers of the flexible array member have side-effects
5644 and the FE splits them into dynamic initialization. */
5645 gcc_checking_assert (fieldsize >= fldsize);
5646 /* Given a non-empty initialization, this field had better
5647 be last. Given a flexible array member, the next field
5648 on the chain is a TYPE_DECL of the enclosing struct. */
5649 const_tree next = DECL_CHAIN (local->field);
5650 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5652 else
5653 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5655 else
5656 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5658 /* Output the element's initial value. */
5659 if (local->val == NULL_TREE)
5660 assemble_zeros (fieldsize);
5661 else
5662 fieldsize = output_constant (local->val, fieldsize, align2,
5663 local->reverse, false);
5665 /* Count its size. */
5666 local->total_bytes += fieldsize;
5669 /* Helper for output_constructor. From the LOCAL state, output an element
5670 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5671 from the start of a possibly ongoing outer byte buffer. */
5673 static void
5674 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5676 /* Bit size of this element. */
5677 HOST_WIDE_INT ebitsize
5678 = (local->field
5679 ? tree_to_uhwi (DECL_SIZE (local->field))
5680 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5682 /* Relative index of this element if this is an array component. */
5683 HOST_WIDE_INT relative_index
5684 = (local->field
5686 : (local->index
5687 ? tree_to_uhwi (local->index) - tree_to_uhwi (local->min_index)
5688 : local->last_relative_index + 1));
5690 /* Bit position of this element from the start of the containing
5691 constructor. */
5692 HOST_WIDE_INT constructor_relative_ebitpos
5693 = (local->field
5694 ? int_bit_position (local->field)
5695 : ebitsize * relative_index);
5697 /* Bit position of this element from the start of a possibly ongoing
5698 outer byte buffer. */
5699 HOST_WIDE_INT byte_relative_ebitpos
5700 = bit_offset + constructor_relative_ebitpos;
5702 /* From the start of a possibly ongoing outer byte buffer, offsets to
5703 the first bit of this element and to the first bit past the end of
5704 this element. */
5705 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5706 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5708 local->last_relative_index = relative_index;
5710 if (local->val == NULL_TREE)
5711 local->val = integer_zero_node;
5713 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5714 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5715 local->val = TREE_OPERAND (local->val, 0);
5717 if (TREE_CODE (local->val) != INTEGER_CST
5718 && TREE_CODE (local->val) != CONSTRUCTOR)
5720 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5721 return;
5724 /* If this field does not start in this (or next) byte, skip some bytes. */
5725 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5727 /* Output remnant of any bit field in previous bytes. */
5728 if (local->byte_buffer_in_use)
5730 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5731 local->total_bytes++;
5732 local->byte_buffer_in_use = false;
5735 /* If still not at proper byte, advance to there. */
5736 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5738 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5739 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5740 local->total_bytes = next_offset / BITS_PER_UNIT;
5744 /* Set up the buffer if necessary. */
5745 if (!local->byte_buffer_in_use)
5747 local->byte = 0;
5748 if (ebitsize > 0)
5749 local->byte_buffer_in_use = true;
5752 /* If this is nested constructor, recurse passing the bit offset and the
5753 pending data, then retrieve the new pending data afterwards. */
5754 if (TREE_CODE (local->val) == CONSTRUCTOR)
5756 oc_outer_state temp_state;
5757 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5758 temp_state.byte = local->byte;
5759 local->total_bytes
5760 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5761 local->byte = temp_state.byte;
5762 return;
5765 /* Otherwise, we must split the element into pieces that fall within
5766 separate bytes, and combine each byte with previous or following
5767 bit-fields. */
5768 while (next_offset < end_offset)
5770 int this_time;
5771 int shift;
5772 unsigned HOST_WIDE_INT value;
5773 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5774 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5776 /* Advance from byte to byte within this element when necessary. */
5777 while (next_byte != local->total_bytes)
5779 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5780 local->total_bytes++;
5781 local->byte = 0;
5784 /* Number of bits we can process at once (all part of the same byte). */
5785 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5786 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5788 /* For big-endian data, take the most significant bits (of the
5789 bits that are significant) first and put them into bytes from
5790 the most significant end. */
5791 shift = end_offset - next_offset - this_time;
5793 /* Don't try to take a bunch of bits that cross
5794 the word boundary in the INTEGER_CST. We can
5795 only select bits from one element. */
5796 if ((shift / HOST_BITS_PER_WIDE_INT)
5797 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5799 const int end = shift + this_time - 1;
5800 shift = end & -HOST_BITS_PER_WIDE_INT;
5801 this_time = end - shift + 1;
5804 /* Now get the bits we want to insert. */
5805 value = wi::extract_uhwi (wi::to_widest (local->val),
5806 shift, this_time);
5808 /* Get the result. This works only when:
5809 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5810 local->byte |= value << (BITS_PER_UNIT - this_time - next_bit);
5812 else
5814 /* On little-endian machines, take the least significant bits of
5815 the value first and pack them starting at the least significant
5816 bits of the bytes. */
5817 shift = next_offset - byte_relative_ebitpos;
5819 /* Don't try to take a bunch of bits that cross
5820 the word boundary in the INTEGER_CST. We can
5821 only select bits from one element. */
5822 if ((shift / HOST_BITS_PER_WIDE_INT)
5823 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5824 this_time
5825 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5827 /* Now get the bits we want to insert. */
5828 value = wi::extract_uhwi (wi::to_widest (local->val),
5829 shift, this_time);
5831 /* Get the result. This works only when:
5832 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5833 local->byte |= value << next_bit;
5836 next_offset += this_time;
5837 local->byte_buffer_in_use = true;
5841 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5842 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5843 caller output state of relevance in recursive invocations. */
5845 static unsigned HOST_WIDE_INT
5846 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5847 bool reverse, oc_outer_state *outer)
5849 unsigned HOST_WIDE_INT cnt;
5850 constructor_elt *ce;
5851 oc_local_state local;
5853 /* Setup our local state to communicate with helpers. */
5854 local.exp = exp;
5855 local.type = TREE_TYPE (exp);
5856 local.size = size;
5857 local.align = align;
5858 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5859 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5860 else
5861 local.min_index = integer_zero_node;
5863 local.total_bytes = 0;
5864 local.byte_buffer_in_use = outer != NULL;
5865 local.byte = outer ? outer->byte : 0;
5866 local.last_relative_index = -1;
5867 /* The storage order is specified for every aggregate type. */
5868 if (AGGREGATE_TYPE_P (local.type))
5869 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5870 else
5871 local.reverse = reverse;
5873 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5875 /* As CE goes through the elements of the constant, FIELD goes through the
5876 structure fields if the constant is a structure. If the constant is a
5877 union, we override this by getting the field from the TREE_LIST element.
5878 But the constant could also be an array. Then FIELD is zero.
5880 There is always a maximum of one element in the chain LINK for unions
5881 (even if the initializer in a source program incorrectly contains
5882 more one). */
5884 if (TREE_CODE (local.type) == RECORD_TYPE)
5885 local.field = TYPE_FIELDS (local.type);
5886 else
5887 local.field = NULL_TREE;
5889 for (cnt = 0;
5890 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5891 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5893 local.val = ce->value;
5894 local.index = NULL_TREE;
5896 /* The element in a union constructor specifies the proper field
5897 or index. */
5898 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5899 local.field = ce->index;
5901 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5902 local.index = ce->index;
5904 if (local.field && flag_verbose_asm)
5905 fprintf (asm_out_file, "%s %s:\n",
5906 ASM_COMMENT_START,
5907 DECL_NAME (local.field)
5908 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5909 : "<anonymous>");
5911 /* Eliminate the marker that makes a cast not be an lvalue. */
5912 if (local.val != NULL_TREE)
5913 STRIP_NOPS (local.val);
5915 /* Output the current element, using the appropriate helper ... */
5917 /* For an array slice not part of an outer bitfield. */
5918 if (!outer
5919 && local.index != NULL_TREE
5920 && TREE_CODE (local.index) == RANGE_EXPR)
5921 output_constructor_array_range (&local);
5923 /* For a field that is neither a true bitfield nor part of an outer one,
5924 known to be at least byte aligned and multiple-of-bytes long. */
5925 else if (!outer
5926 && (local.field == NULL_TREE
5927 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5928 output_constructor_regular_field (&local);
5930 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5931 supported for scalar fields, so we may need to convert first. */
5932 else
5934 if (TREE_CODE (local.val) == REAL_CST)
5935 local.val
5936 = fold_unary (VIEW_CONVERT_EXPR,
5937 build_nonstandard_integer_type
5938 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5939 local.val);
5940 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5944 /* If we are not at toplevel, save the pending data for our caller.
5945 Otherwise output the pending data and padding zeros as needed. */
5946 if (outer)
5947 outer->byte = local.byte;
5948 else
5950 if (local.byte_buffer_in_use)
5952 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5953 local.total_bytes++;
5956 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5958 assemble_zeros (local.size - local.total_bytes);
5959 local.total_bytes = local.size;
5963 return local.total_bytes;
5966 /* Mark DECL as weak. */
5968 static void
5969 mark_weak (tree decl)
5971 if (DECL_WEAK (decl))
5972 return;
5974 struct symtab_node *n = symtab_node::get (decl);
5975 if (n && n->refuse_visibility_changes)
5976 error ("%qD declared weak after being used", decl);
5977 DECL_WEAK (decl) = 1;
5979 if (DECL_RTL_SET_P (decl)
5980 && MEM_P (DECL_RTL (decl))
5981 && XEXP (DECL_RTL (decl), 0)
5982 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5983 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5986 /* Merge weak status between NEWDECL and OLDDECL. */
5988 void
5989 merge_weak (tree newdecl, tree olddecl)
5991 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5993 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5995 tree *pwd;
5996 /* We put the NEWDECL on the weak_decls list at some point
5997 and OLDDECL as well. Keep just OLDDECL on the list. */
5998 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5999 if (TREE_VALUE (*pwd) == newdecl)
6001 *pwd = TREE_CHAIN (*pwd);
6002 break;
6005 return;
6008 if (DECL_WEAK (newdecl))
6010 tree wd;
6012 /* NEWDECL is weak, but OLDDECL is not. */
6014 /* If we already output the OLDDECL, we're in trouble; we can't
6015 go back and make it weak. This should never happen in
6016 unit-at-a-time compilation. */
6017 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
6019 /* If we've already generated rtl referencing OLDDECL, we may
6020 have done so in a way that will not function properly with
6021 a weak symbol. Again in unit-at-a-time this should be
6022 impossible. */
6023 gcc_assert (!TREE_USED (olddecl)
6024 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
6026 /* PR 49899: You cannot convert a static function into a weak, public function. */
6027 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
6028 error ("weak declaration of %q+D being applied to a already "
6029 "existing, static definition", newdecl);
6031 if (TARGET_SUPPORTS_WEAK)
6033 /* We put the NEWDECL on the weak_decls list at some point.
6034 Replace it with the OLDDECL. */
6035 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
6036 if (TREE_VALUE (wd) == newdecl)
6038 TREE_VALUE (wd) = olddecl;
6039 break;
6041 /* We may not find the entry on the list. If NEWDECL is a
6042 weak alias, then we will have already called
6043 globalize_decl to remove the entry; in that case, we do
6044 not need to do anything. */
6047 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
6048 mark_weak (olddecl);
6050 else
6051 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
6052 weak. Just update NEWDECL to indicate that it's weak too. */
6053 mark_weak (newdecl);
6056 /* Declare DECL to be a weak symbol. */
6058 void
6059 declare_weak (tree decl)
6061 /* With -fsyntax-only, TREE_ASM_WRITTEN might be set on certain function
6062 decls earlier than normally, but as with -fsyntax-only nothing is really
6063 emitted, there is no harm in marking it weak later. */
6064 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL
6065 || !TREE_ASM_WRITTEN (decl)
6066 || flag_syntax_only);
6067 if (! TREE_PUBLIC (decl))
6069 error ("weak declaration of %q+D must be public", decl);
6070 return;
6072 else if (!TARGET_SUPPORTS_WEAK)
6073 warning (0, "weak declaration of %q+D not supported", decl);
6075 mark_weak (decl);
6076 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
6077 DECL_ATTRIBUTES (decl)
6078 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
6081 static void
6082 weak_finish_1 (tree decl)
6084 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
6085 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
6086 #endif
6088 if (! TREE_USED (decl))
6089 return;
6091 #ifdef ASM_WEAKEN_DECL
6092 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
6093 #else
6094 #ifdef ASM_WEAKEN_LABEL
6095 ASM_WEAKEN_LABEL (asm_out_file, name);
6096 #else
6097 #ifdef ASM_OUTPUT_WEAK_ALIAS
6099 static bool warn_once = 0;
6100 if (! warn_once)
6102 warning (0, "only weak aliases are supported in this configuration");
6103 warn_once = 1;
6105 return;
6107 #endif
6108 #endif
6109 #endif
6112 /* Fiven an assembly name, find the decl it is associated with. */
6113 static tree
6114 find_decl (tree target)
6116 symtab_node *node = symtab_node::get_for_asmname (target);
6117 if (node)
6118 return node->decl;
6119 return NULL_TREE;
6122 /* This TREE_LIST contains weakref targets. */
6124 static GTY(()) tree weakref_targets;
6126 /* Emit any pending weak declarations. */
6128 void
6129 weak_finish (void)
6131 tree t;
6133 for (t = weakref_targets; t; t = TREE_CHAIN (t))
6135 tree alias_decl = TREE_PURPOSE (t);
6136 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
6138 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
6139 || TREE_SYMBOL_REFERENCED (target))
6140 /* Remove alias_decl from the weak list, but leave entries for
6141 the target alone. */
6142 target = NULL_TREE;
6143 #ifndef ASM_OUTPUT_WEAKREF
6144 else if (! TREE_SYMBOL_REFERENCED (target))
6146 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
6147 defined, otherwise we and weak_finish_1 would use
6148 different macros. */
6149 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
6150 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
6151 # else
6152 tree decl = find_decl (target);
6154 if (! decl)
6156 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
6157 TREE_CODE (alias_decl), target,
6158 TREE_TYPE (alias_decl));
6160 DECL_EXTERNAL (decl) = 1;
6161 TREE_PUBLIC (decl) = 1;
6162 DECL_ARTIFICIAL (decl) = 1;
6163 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
6164 TREE_USED (decl) = 1;
6167 weak_finish_1 (decl);
6168 # endif
6170 #endif
6173 tree *p;
6174 tree t2;
6176 /* Remove the alias and the target from the pending weak list
6177 so that we do not emit any .weak directives for the former,
6178 nor multiple .weak directives for the latter. */
6179 for (p = &weak_decls; (t2 = *p) ; )
6181 if (TREE_VALUE (t2) == alias_decl
6182 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
6183 *p = TREE_CHAIN (t2);
6184 else
6185 p = &TREE_CHAIN (t2);
6188 /* Remove other weakrefs to the same target, to speed things up. */
6189 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
6191 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
6192 *p = TREE_CHAIN (t2);
6193 else
6194 p = &TREE_CHAIN (t2);
6199 for (t = weak_decls; t; t = TREE_CHAIN (t))
6201 tree decl = TREE_VALUE (t);
6203 weak_finish_1 (decl);
6207 /* Emit the assembly bits to indicate that DECL is globally visible. */
6209 static void
6210 globalize_decl (tree decl)
6213 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
6214 if (DECL_WEAK (decl))
6216 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6217 tree *p, t;
6219 #ifdef ASM_WEAKEN_DECL
6220 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
6221 #else
6222 ASM_WEAKEN_LABEL (asm_out_file, name);
6223 #endif
6225 /* Remove this function from the pending weak list so that
6226 we do not emit multiple .weak directives for it. */
6227 for (p = &weak_decls; (t = *p) ; )
6229 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6230 *p = TREE_CHAIN (t);
6231 else
6232 p = &TREE_CHAIN (t);
6235 /* Remove weakrefs to the same target from the pending weakref
6236 list, for the same reason. */
6237 for (p = &weakref_targets; (t = *p) ; )
6239 if (DECL_ASSEMBLER_NAME (decl)
6240 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6241 *p = TREE_CHAIN (t);
6242 else
6243 p = &TREE_CHAIN (t);
6246 return;
6248 #endif
6250 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
6253 vec<alias_pair, va_gc> *alias_pairs;
6255 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
6256 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
6257 tree node is DECL to have the value of the tree node TARGET. */
6259 void
6260 do_assemble_alias (tree decl, tree target)
6262 tree id;
6264 /* Emulated TLS had better not get this var. */
6265 gcc_assert (!(!targetm.have_tls
6266 && VAR_P (decl)
6267 && DECL_THREAD_LOCAL_P (decl)));
6269 if (TREE_ASM_WRITTEN (decl))
6270 return;
6272 id = DECL_ASSEMBLER_NAME (decl);
6273 ultimate_transparent_alias_target (&id);
6274 ultimate_transparent_alias_target (&target);
6276 /* We must force creation of DECL_RTL for debug info generation, even though
6277 we don't use it here. */
6278 make_decl_rtl (decl);
6280 TREE_ASM_WRITTEN (decl) = 1;
6281 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
6282 TREE_ASM_WRITTEN (id) = 1;
6284 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6286 if (!TREE_SYMBOL_REFERENCED (target))
6287 weakref_targets = tree_cons (decl, target, weakref_targets);
6289 #ifdef ASM_OUTPUT_WEAKREF
6290 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
6291 IDENTIFIER_POINTER (id),
6292 IDENTIFIER_POINTER (target));
6293 #else
6294 if (!TARGET_SUPPORTS_WEAK)
6296 error_at (DECL_SOURCE_LOCATION (decl),
6297 "%qs is not supported in this configuration", "weakref ");
6298 return;
6300 #endif
6301 return;
6304 #ifdef ASM_OUTPUT_DEF
6305 tree orig_decl = decl;
6307 /* Make name accessible from other files, if appropriate. */
6309 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
6311 globalize_decl (decl);
6312 maybe_assemble_visibility (decl);
6314 if (TREE_CODE (decl) == FUNCTION_DECL
6315 && cgraph_node::get (decl)->ifunc_resolver)
6317 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
6318 if (targetm.has_ifunc_p ())
6319 ASM_OUTPUT_TYPE_DIRECTIVE
6320 (asm_out_file, IDENTIFIER_POINTER (id),
6321 IFUNC_ASM_TYPE);
6322 else
6323 #endif
6324 error_at (DECL_SOURCE_LOCATION (decl),
6325 "%qs is not supported on this target", "ifunc");
6328 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
6329 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
6330 # else
6331 ASM_OUTPUT_DEF (asm_out_file,
6332 IDENTIFIER_POINTER (id),
6333 IDENTIFIER_POINTER (target));
6334 # endif
6335 /* If symbol aliases aren't actually supported... */
6336 if (!TARGET_SUPPORTS_ALIASES)
6337 /* ..., 'ASM_OUTPUT_DEF{,_FROM_DECLS}' better have raised an error. */
6338 gcc_checking_assert (seen_error ());
6339 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
6341 const char *name;
6342 tree *p, t;
6344 name = IDENTIFIER_POINTER (id);
6345 # ifdef ASM_WEAKEN_DECL
6346 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
6347 # else
6348 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
6349 # endif
6350 /* Remove this function from the pending weak list so that
6351 we do not emit multiple .weak directives for it. */
6352 for (p = &weak_decls; (t = *p) ; )
6353 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
6354 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
6355 *p = TREE_CHAIN (t);
6356 else
6357 p = &TREE_CHAIN (t);
6359 /* Remove weakrefs to the same target from the pending weakref
6360 list, for the same reason. */
6361 for (p = &weakref_targets; (t = *p) ; )
6363 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
6364 *p = TREE_CHAIN (t);
6365 else
6366 p = &TREE_CHAIN (t);
6369 #endif
6372 /* Output .symver directive. */
6374 void
6375 do_assemble_symver (tree decl, tree target)
6377 tree id = DECL_ASSEMBLER_NAME (decl);
6378 ultimate_transparent_alias_target (&id);
6379 ultimate_transparent_alias_target (&target);
6380 #ifdef ASM_OUTPUT_SYMVER_DIRECTIVE
6381 ASM_OUTPUT_SYMVER_DIRECTIVE (asm_out_file,
6382 IDENTIFIER_POINTER (target),
6383 IDENTIFIER_POINTER (id));
6384 #else
6385 error ("symver is only supported on ELF platforms");
6386 #endif
6389 /* Emit an assembler directive to make the symbol for DECL an alias to
6390 the symbol for TARGET. */
6392 void
6393 assemble_alias (tree decl, tree target)
6395 tree target_decl;
6397 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
6399 tree alias = DECL_ASSEMBLER_NAME (decl);
6401 ultimate_transparent_alias_target (&target);
6403 if (alias == target)
6404 error ("%qs symbol %q+D ultimately targets itself", "weakref", decl);
6405 if (TREE_PUBLIC (decl))
6406 error ("%qs symbol %q+D must have static linkage", "weakref", decl);
6408 else if (!TARGET_SUPPORTS_ALIASES)
6410 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
6411 error_at (DECL_SOURCE_LOCATION (decl),
6412 "alias definitions not supported in this configuration");
6413 TREE_ASM_WRITTEN (decl) = 1;
6414 return;
6415 # else
6416 if (!DECL_WEAK (decl))
6418 /* NB: ifunc_resolver isn't set when an error is detected. */
6419 if (TREE_CODE (decl) == FUNCTION_DECL
6420 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
6421 error_at (DECL_SOURCE_LOCATION (decl),
6422 "%qs is not supported in this configuration", "ifunc");
6423 else
6424 error_at (DECL_SOURCE_LOCATION (decl),
6425 "only weak aliases are supported in this configuration");
6426 TREE_ASM_WRITTEN (decl) = 1;
6427 return;
6429 # endif
6430 gcc_unreachable ();
6432 TREE_USED (decl) = 1;
6434 /* Allow aliases to aliases. */
6435 if (TREE_CODE (decl) == FUNCTION_DECL)
6436 cgraph_node::get_create (decl)->alias = true;
6437 else
6438 varpool_node::get_create (decl)->alias = true;
6440 /* If the target has already been emitted, we don't have to queue the
6441 alias. This saves a tad of memory. */
6442 if (symtab->global_info_ready)
6443 target_decl = find_decl (target);
6444 else
6445 target_decl= NULL;
6446 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6447 || symtab->state >= EXPANSION)
6448 do_assemble_alias (decl, target);
6449 else
6451 alias_pair p = {decl, target};
6452 vec_safe_push (alias_pairs, p);
6456 /* Record and output a table of translations from original function
6457 to its transaction aware clone. Note that tm_pure functions are
6458 considered to be their own clone. */
6460 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6462 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6463 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6465 static int
6466 keep_cache_entry (tree_map *&e)
6468 return ggc_marked_p (e->base.from);
6472 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6474 void
6475 record_tm_clone_pair (tree o, tree n)
6477 struct tree_map **slot, *h;
6479 if (tm_clone_hash == NULL)
6480 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6482 h = ggc_alloc<tree_map> ();
6483 h->hash = htab_hash_pointer (o);
6484 h->base.from = o;
6485 h->to = n;
6487 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6488 *slot = h;
6491 tree
6492 get_tm_clone_pair (tree o)
6494 if (tm_clone_hash)
6496 struct tree_map *h, in;
6498 in.base.from = o;
6499 in.hash = htab_hash_pointer (o);
6500 h = tm_clone_hash->find_with_hash (&in, in.hash);
6501 if (h)
6502 return h->to;
6504 return NULL_TREE;
6507 struct tm_alias_pair
6509 unsigned int uid;
6510 tree from;
6511 tree to;
6515 /* Dump the actual pairs to the .tm_clone_table section. */
6517 static void
6518 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6520 unsigned i;
6521 tm_alias_pair *p;
6522 bool switched = false;
6524 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6526 tree src = p->from;
6527 tree dst = p->to;
6528 struct cgraph_node *src_n = cgraph_node::get (src);
6529 struct cgraph_node *dst_n = cgraph_node::get (dst);
6531 /* The function ipa_tm_create_version() marks the clone as needed if
6532 the original function was needed. But we also mark the clone as
6533 needed if we ever called the clone indirectly through
6534 TM_GETTMCLONE. If neither of these are true, we didn't generate
6535 a clone, and we didn't call it indirectly... no sense keeping it
6536 in the clone table. */
6537 if (!dst_n || !dst_n->definition)
6538 continue;
6540 /* This covers the case where we have optimized the original
6541 function away, and only access the transactional clone. */
6542 if (!src_n || !src_n->definition)
6543 continue;
6545 if (!switched)
6547 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6548 assemble_align (POINTER_SIZE);
6549 switched = true;
6552 assemble_integer (XEXP (DECL_RTL (src), 0),
6553 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6554 assemble_integer (XEXP (DECL_RTL (dst), 0),
6555 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6559 /* Provide a default for the tm_clone_table section. */
6561 section *
6562 default_clone_table_section (void)
6564 return get_named_section (NULL, ".tm_clone_table", 3);
6567 /* Helper comparison function for qsorting by the DECL_UID stored in
6568 alias_pair->emitted_diags. */
6570 static int
6571 tm_alias_pair_cmp (const void *x, const void *y)
6573 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6574 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6575 if (p1->uid < p2->uid)
6576 return -1;
6577 if (p1->uid > p2->uid)
6578 return 1;
6579 return 0;
6582 void
6583 finish_tm_clone_pairs (void)
6585 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6587 if (tm_clone_hash == NULL)
6588 return;
6590 /* We need a determenistic order for the .tm_clone_table, otherwise
6591 we will get bootstrap comparison failures, so dump the hash table
6592 to a vector, sort it, and dump the vector. */
6594 /* Dump the hashtable to a vector. */
6595 tree_map *map;
6596 hash_table<tm_clone_hasher>::iterator iter;
6597 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6599 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6600 tm_alias_pairs.safe_push (p);
6602 /* Sort it. */
6603 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6605 /* Dump it. */
6606 dump_tm_clone_pairs (tm_alias_pairs);
6608 tm_clone_hash->empty ();
6609 tm_clone_hash = NULL;
6610 tm_alias_pairs.release ();
6614 /* Emit an assembler directive to set symbol for DECL visibility to
6615 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6617 void
6618 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6619 int vis ATTRIBUTE_UNUSED)
6621 #ifdef HAVE_GAS_HIDDEN
6622 static const char * const visibility_types[] = {
6623 NULL, "protected", "hidden", "internal"
6626 const char *name, *type;
6627 tree id;
6629 id = DECL_ASSEMBLER_NAME (decl);
6630 ultimate_transparent_alias_target (&id);
6631 name = IDENTIFIER_POINTER (id);
6633 type = visibility_types[vis];
6635 fprintf (asm_out_file, "\t.%s\t", type);
6636 assemble_name (asm_out_file, name);
6637 fprintf (asm_out_file, "\n");
6638 #else
6639 if (!DECL_ARTIFICIAL (decl))
6640 warning (OPT_Wattributes, "visibility attribute not supported "
6641 "in this configuration; ignored");
6642 #endif
6645 /* A helper function to call assemble_visibility when needed for a decl. */
6647 bool
6648 maybe_assemble_visibility (tree decl)
6650 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6651 if (vis != VISIBILITY_DEFAULT)
6653 targetm.asm_out.assemble_visibility (decl, vis);
6654 return true;
6656 else
6657 return false;
6660 /* Returns true if the target configuration supports defining public symbols
6661 so that one of them will be chosen at link time instead of generating a
6662 multiply-defined symbol error, whether through the use of weak symbols or
6663 a target-specific mechanism for having duplicates discarded. */
6665 bool
6666 supports_one_only (void)
6668 if (SUPPORTS_ONE_ONLY)
6669 return true;
6670 if (TARGET_SUPPORTS_WEAK)
6671 return true;
6672 return false;
6675 /* Set up DECL as a public symbol that can be defined in multiple
6676 translation units without generating a linker error. */
6678 void
6679 make_decl_one_only (tree decl, tree comdat_group)
6681 struct symtab_node *symbol;
6682 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6684 TREE_PUBLIC (decl) = 1;
6686 if (VAR_P (decl))
6687 symbol = varpool_node::get_create (decl);
6688 else
6689 symbol = cgraph_node::get_create (decl);
6691 if (SUPPORTS_ONE_ONLY)
6693 #ifdef MAKE_DECL_ONE_ONLY
6694 MAKE_DECL_ONE_ONLY (decl);
6695 #endif
6696 symbol->set_comdat_group (comdat_group);
6698 else if (VAR_P (decl)
6699 && (DECL_INITIAL (decl) == 0
6700 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6701 DECL_COMMON (decl) = 1;
6702 else
6704 gcc_assert (TARGET_SUPPORTS_WEAK);
6705 DECL_WEAK (decl) = 1;
6709 void
6710 init_varasm_once (void)
6712 section_htab = hash_table<section_hasher>::create_ggc (31);
6713 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6714 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6716 shared_constant_pool = create_constant_pool ();
6718 #ifdef TEXT_SECTION_ASM_OP
6719 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6720 TEXT_SECTION_ASM_OP);
6721 #endif
6723 #ifdef DATA_SECTION_ASM_OP
6724 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6725 DATA_SECTION_ASM_OP);
6726 #endif
6728 #ifdef SDATA_SECTION_ASM_OP
6729 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6730 SDATA_SECTION_ASM_OP);
6731 #endif
6733 #ifdef READONLY_DATA_SECTION_ASM_OP
6734 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6735 READONLY_DATA_SECTION_ASM_OP);
6736 #endif
6738 #ifdef CTORS_SECTION_ASM_OP
6739 ctors_section = get_unnamed_section (0, output_section_asm_op,
6740 CTORS_SECTION_ASM_OP);
6741 #endif
6743 #ifdef DTORS_SECTION_ASM_OP
6744 dtors_section = get_unnamed_section (0, output_section_asm_op,
6745 DTORS_SECTION_ASM_OP);
6746 #endif
6748 #ifdef BSS_SECTION_ASM_OP
6749 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6750 output_section_asm_op,
6751 BSS_SECTION_ASM_OP);
6752 #endif
6754 #ifdef SBSS_SECTION_ASM_OP
6755 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6756 output_section_asm_op,
6757 SBSS_SECTION_ASM_OP);
6758 #endif
6760 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6761 | SECTION_COMMON, emit_tls_common);
6762 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6763 | SECTION_COMMON, emit_local);
6764 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6765 | SECTION_COMMON, emit_common);
6767 #if defined ASM_OUTPUT_ALIGNED_BSS
6768 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6769 emit_bss);
6770 #endif
6772 targetm.asm_out.init_sections ();
6774 if (readonly_data_section == NULL)
6775 readonly_data_section = text_section;
6777 #ifdef ASM_OUTPUT_EXTERNAL
6778 pending_assemble_externals_set = new hash_set<tree>;
6779 #endif
6782 /* Determine whether SYMBOL is used in any optimized function. */
6784 static bool
6785 have_optimized_refs (struct symtab_node *symbol)
6787 struct ipa_ref *ref;
6789 for (int i = 0; symbol->iterate_referring (i, ref); i++)
6791 cgraph_node *cnode = dyn_cast <cgraph_node *> (ref->referring);
6793 if (cnode && opt_for_fn (cnode->decl, optimize))
6794 return true;
6797 return false;
6800 /* Check if promoting general-dynamic TLS access model to local-dynamic is
6801 desirable for DECL. */
6803 static bool
6804 optimize_dyn_tls_for_decl_p (const_tree decl)
6806 if (cfun)
6807 return optimize;
6808 return symtab->state >= IPA && have_optimized_refs (symtab_node::get (decl));
6812 enum tls_model
6813 decl_default_tls_model (const_tree decl)
6815 enum tls_model kind;
6816 bool is_local;
6818 is_local = targetm.binds_local_p (decl);
6819 if (!flag_shlib)
6821 if (is_local)
6822 kind = TLS_MODEL_LOCAL_EXEC;
6823 else
6824 kind = TLS_MODEL_INITIAL_EXEC;
6827 /* Local dynamic is inefficient when we're not combining the
6828 parts of the address. */
6829 else if (is_local && optimize_dyn_tls_for_decl_p (decl))
6830 kind = TLS_MODEL_LOCAL_DYNAMIC;
6831 else
6832 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6833 if (kind < flag_tls_default)
6834 kind = flag_tls_default;
6836 return kind;
6839 /* Select a set of attributes for section NAME based on the properties
6840 of DECL and whether or not RELOC indicates that DECL's initializer
6841 might contain runtime relocations.
6843 We make the section read-only and executable for a function decl,
6844 read-only for a const data decl, and writable for a non-const data decl. */
6846 unsigned int
6847 default_section_type_flags (tree decl, const char *name, int reloc)
6849 unsigned int flags;
6851 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6852 flags = SECTION_CODE;
6853 else if (decl)
6855 enum section_category category
6856 = categorize_decl_for_section (decl, reloc);
6857 if (decl_readonly_section_1 (category))
6858 flags = 0;
6859 else if (category == SECCAT_DATA_REL_RO
6860 || category == SECCAT_DATA_REL_RO_LOCAL)
6861 flags = SECTION_WRITE | SECTION_RELRO;
6862 else
6863 flags = SECTION_WRITE;
6865 else
6867 flags = SECTION_WRITE;
6868 if (strcmp (name, ".data.rel.ro") == 0
6869 || strcmp (name, ".data.rel.ro.local") == 0)
6870 flags |= SECTION_RELRO;
6873 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6874 flags |= SECTION_LINKONCE;
6876 if (strcmp (name, ".vtable_map_vars") == 0)
6877 flags |= SECTION_LINKONCE;
6879 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6880 flags |= SECTION_TLS | SECTION_WRITE;
6882 if (strcmp (name, ".bss") == 0
6883 || startswith (name, ".bss.")
6884 || startswith (name, ".gnu.linkonce.b.")
6885 || strcmp (name, ".persistent.bss") == 0
6886 || strcmp (name, ".sbss") == 0
6887 || startswith (name, ".sbss.")
6888 || startswith (name, ".gnu.linkonce.sb."))
6889 flags |= SECTION_BSS;
6891 if (strcmp (name, ".tdata") == 0
6892 || startswith (name, ".tdata.")
6893 || startswith (name, ".gnu.linkonce.td."))
6894 flags |= SECTION_TLS;
6896 if (strcmp (name, ".tbss") == 0
6897 || startswith (name, ".tbss.")
6898 || startswith (name, ".gnu.linkonce.tb."))
6899 flags |= SECTION_TLS | SECTION_BSS;
6901 if (strcmp (name, ".noinit") == 0)
6902 flags |= SECTION_WRITE | SECTION_BSS | SECTION_NOTYPE;
6904 if (strcmp (name, ".persistent") == 0)
6905 flags |= SECTION_WRITE | SECTION_NOTYPE;
6907 /* Various sections have special ELF types that the assembler will
6908 assign by default based on the name. They are neither SHT_PROGBITS
6909 nor SHT_NOBITS, so when changing sections we don't want to print a
6910 section type (@progbits or @nobits). Rather than duplicating the
6911 assembler's knowledge of what those special name patterns are, just
6912 let the assembler choose the type if we don't know a specific
6913 reason to set it to something other than the default. SHT_PROGBITS
6914 is the default for sections whose name is not specially known to
6915 the assembler, so it does no harm to leave the choice to the
6916 assembler when @progbits is the best thing we know to use. If
6917 someone is silly enough to emit code or TLS variables to one of
6918 these sections, then don't handle them specially.
6920 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6921 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
6922 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6923 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6924 flags |= SECTION_NOTYPE;
6926 return flags;
6929 /* Return true if the target supports some form of global BSS,
6930 either through bss_noswitch_section, or by selecting a BSS
6931 section in TARGET_ASM_SELECT_SECTION. */
6933 bool
6934 have_global_bss_p (void)
6936 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6939 /* Output assembly to switch to section NAME with attribute FLAGS.
6940 Four variants for common object file formats. */
6942 void
6943 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6944 unsigned int flags ATTRIBUTE_UNUSED,
6945 tree decl ATTRIBUTE_UNUSED)
6947 /* Some object formats don't support named sections at all. The
6948 front-end should already have flagged this as an error. */
6949 gcc_unreachable ();
6952 #ifndef TLS_SECTION_ASM_FLAG
6953 #define TLS_SECTION_ASM_FLAG 'T'
6954 #endif
6956 void
6957 default_elf_asm_named_section (const char *name, unsigned int flags,
6958 tree decl)
6960 char flagchars[11], *f = flagchars;
6961 unsigned int numeric_value = 0;
6963 /* If we have already declared this section, we can use an
6964 abbreviated form to switch back to it -- unless this section is
6965 part of a COMDAT groups or with SHF_GNU_RETAIN or with SHF_LINK_ORDER,
6966 in which case GAS requires the full declaration every time. */
6967 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6968 && !(flags & (SECTION_RETAIN | SECTION_LINK_ORDER))
6969 && (flags & SECTION_DECLARED))
6971 fprintf (asm_out_file, "\t.section\t%s\n", name);
6972 return;
6975 /* If we have a machine specific flag, then use the numeric value to pass
6976 this on to GAS. */
6977 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6978 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6979 else
6981 if (!(flags & SECTION_DEBUG))
6982 *f++ = 'a';
6983 #if HAVE_GAS_SECTION_EXCLUDE
6984 if (flags & SECTION_EXCLUDE)
6985 *f++ = 'e';
6986 #endif
6987 if (flags & SECTION_WRITE)
6988 *f++ = 'w';
6989 if (flags & SECTION_CODE)
6990 *f++ = 'x';
6991 if (flags & SECTION_SMALL)
6992 *f++ = 's';
6993 if (flags & SECTION_MERGE)
6994 *f++ = 'M';
6995 if (flags & SECTION_STRINGS)
6996 *f++ = 'S';
6997 if (flags & SECTION_TLS)
6998 *f++ = TLS_SECTION_ASM_FLAG;
6999 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
7000 *f++ = 'G';
7001 if (flags & SECTION_RETAIN)
7002 *f++ = 'R';
7003 if (flags & SECTION_LINK_ORDER)
7004 *f++ = 'o';
7005 #ifdef MACH_DEP_SECTION_ASM_FLAG
7006 if (flags & SECTION_MACH_DEP)
7007 *f++ = MACH_DEP_SECTION_ASM_FLAG;
7008 #endif
7009 *f = '\0';
7012 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
7014 /* default_section_type_flags (above) knows which flags need special
7015 handling here, and sets NOTYPE when none of these apply so that the
7016 assembler's logic for default types can apply to user-chosen
7017 section names. */
7018 if (!(flags & SECTION_NOTYPE))
7020 const char *type;
7021 const char *format;
7023 if (flags & SECTION_BSS)
7024 type = "nobits";
7025 else
7026 type = "progbits";
7028 format = ",@%s";
7029 /* On platforms that use "@" as the assembly comment character,
7030 use "%" instead. */
7031 if (strcmp (ASM_COMMENT_START, "@") == 0)
7032 format = ",%%%s";
7033 fprintf (asm_out_file, format, type);
7035 if (flags & SECTION_ENTSIZE)
7036 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
7037 if (flags & SECTION_LINK_ORDER)
7039 /* For now, only section "__patchable_function_entries"
7040 adopts flag SECTION_LINK_ORDER, internal label LPFE*
7041 was emitted in default_print_patchable_function_entry,
7042 just place it here for linked_to section. */
7043 gcc_assert (!strcmp (name, "__patchable_function_entries"));
7044 fprintf (asm_out_file, ",");
7045 char buf[256];
7046 ASM_GENERATE_INTERNAL_LABEL (buf, "LPFE",
7047 current_function_funcdef_no);
7048 assemble_name_raw (asm_out_file, buf);
7050 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
7052 if (TREE_CODE (decl) == IDENTIFIER_NODE)
7053 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
7054 else
7055 fprintf (asm_out_file, ",%s,comdat",
7056 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
7060 putc ('\n', asm_out_file);
7063 void
7064 default_coff_asm_named_section (const char *name, unsigned int flags,
7065 tree decl ATTRIBUTE_UNUSED)
7067 char flagchars[8], *f = flagchars;
7069 if (flags & SECTION_WRITE)
7070 *f++ = 'w';
7071 if (flags & SECTION_CODE)
7072 *f++ = 'x';
7073 *f = '\0';
7075 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
7078 void
7079 default_pe_asm_named_section (const char *name, unsigned int flags,
7080 tree decl)
7082 default_coff_asm_named_section (name, flags, decl);
7084 if (flags & SECTION_LINKONCE)
7086 /* Functions may have been compiled at various levels of
7087 optimization so we can't use `same_size' here.
7088 Instead, have the linker pick one. */
7089 fprintf (asm_out_file, "\t.linkonce %s\n",
7090 (flags & SECTION_CODE ? "discard" : "same_size"));
7094 /* The lame default section selector. */
7096 section *
7097 default_select_section (tree decl, int reloc,
7098 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7100 if (DECL_P (decl))
7102 if (decl_readonly_section (decl, reloc))
7103 return readonly_data_section;
7105 else if (TREE_CODE (decl) == CONSTRUCTOR)
7107 if (! ((flag_pic && reloc)
7108 || !TREE_READONLY (decl)
7109 || !TREE_CONSTANT (decl)))
7110 return readonly_data_section;
7112 else if (TREE_CODE (decl) == STRING_CST)
7113 return readonly_data_section;
7114 else if (! (flag_pic && reloc))
7115 return readonly_data_section;
7117 return data_section;
7120 enum section_category
7121 categorize_decl_for_section (const_tree decl, int reloc)
7123 enum section_category ret;
7125 if (TREE_CODE (decl) == FUNCTION_DECL)
7126 return SECCAT_TEXT;
7127 else if (TREE_CODE (decl) == STRING_CST)
7129 if ((flag_sanitize & SANITIZE_ADDRESS)
7130 && asan_protect_global (CONST_CAST_TREE (decl)))
7131 /* or !flag_merge_constants */
7132 return SECCAT_RODATA;
7133 else
7134 return SECCAT_RODATA_MERGE_STR;
7136 else if (VAR_P (decl))
7138 tree d = CONST_CAST_TREE (decl);
7139 if (bss_initializer_p (decl))
7140 ret = SECCAT_BSS;
7141 else if (! TREE_READONLY (decl)
7142 || (DECL_INITIAL (decl)
7143 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
7145 /* Here the reloc_rw_mask is not testing whether the section should
7146 be read-only or not, but whether the dynamic link will have to
7147 do something. If so, we wish to segregate the data in order to
7148 minimize cache misses inside the dynamic linker. */
7149 if (reloc & targetm.asm_out.reloc_rw_mask ())
7150 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
7151 else
7152 ret = SECCAT_DATA;
7154 else if (reloc & targetm.asm_out.reloc_rw_mask ())
7155 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
7156 else if (reloc || (flag_merge_constants < 2 && !DECL_MERGEABLE (decl))
7157 || ((flag_sanitize & SANITIZE_ADDRESS)
7158 /* PR 81697: for architectures that use section anchors we
7159 need to ignore DECL_RTL_SET_P (decl) for string constants
7160 inside this asan_protect_global call because otherwise
7161 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
7162 section, set DECL_RTL (decl) later on and add DECL to
7163 protected globals via successive asan_protect_global
7164 calls. In this scenario we'll end up with wrong
7165 alignment of these strings at runtime and possible ASan
7166 false positives. */
7167 && asan_protect_global (d, use_object_blocks_p ()
7168 && use_blocks_for_decl_p (d))))
7169 /* C and C++ don't allow different variables to share the same
7170 location. -fmerge-all-constants allows even that (at the
7171 expense of not conforming). */
7172 ret = SECCAT_RODATA;
7173 else if (DECL_INITIAL (decl)
7174 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
7175 ret = SECCAT_RODATA_MERGE_STR_INIT;
7176 else
7177 ret = SECCAT_RODATA_MERGE_CONST;
7179 else if (TREE_CODE (decl) == CONSTRUCTOR)
7181 if ((reloc & targetm.asm_out.reloc_rw_mask ())
7182 || ! TREE_CONSTANT (decl))
7183 ret = SECCAT_DATA;
7184 else
7185 ret = SECCAT_RODATA;
7187 else
7188 ret = SECCAT_RODATA;
7190 /* There are no read-only thread-local sections. */
7191 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7193 /* Note that this would be *just* SECCAT_BSS, except that there's
7194 no concept of a read-only thread-local-data section. */
7195 if (ret == SECCAT_BSS
7196 || DECL_INITIAL (decl) == NULL
7197 || (flag_zero_initialized_in_bss
7198 && initializer_zerop (DECL_INITIAL (decl))))
7199 ret = SECCAT_TBSS;
7200 else
7201 ret = SECCAT_TDATA;
7204 /* If the target uses small data sections, select it. */
7205 else if (targetm.in_small_data_p (decl))
7207 if (ret == SECCAT_BSS)
7208 ret = SECCAT_SBSS;
7209 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
7210 ret = SECCAT_SRODATA;
7211 else
7212 ret = SECCAT_SDATA;
7215 return ret;
7218 static bool
7219 decl_readonly_section_1 (enum section_category category)
7221 switch (category)
7223 case SECCAT_RODATA:
7224 case SECCAT_RODATA_MERGE_STR:
7225 case SECCAT_RODATA_MERGE_STR_INIT:
7226 case SECCAT_RODATA_MERGE_CONST:
7227 case SECCAT_SRODATA:
7228 return true;
7229 default:
7230 return false;
7234 bool
7235 decl_readonly_section (const_tree decl, int reloc)
7237 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
7240 /* Select a section based on the above categorization. */
7242 section *
7243 default_elf_select_section (tree decl, int reloc,
7244 unsigned HOST_WIDE_INT align)
7246 const char *sname;
7248 switch (categorize_decl_for_section (decl, reloc))
7250 case SECCAT_TEXT:
7251 /* We're not supposed to be called on FUNCTION_DECLs. */
7252 gcc_unreachable ();
7253 case SECCAT_RODATA:
7254 return readonly_data_section;
7255 case SECCAT_RODATA_MERGE_STR:
7256 return mergeable_string_section (decl, align, 0);
7257 case SECCAT_RODATA_MERGE_STR_INIT:
7258 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
7259 case SECCAT_RODATA_MERGE_CONST:
7260 return mergeable_constant_section (DECL_MODE (decl), align, 0);
7261 case SECCAT_SRODATA:
7262 sname = ".sdata2";
7263 break;
7264 case SECCAT_DATA:
7265 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7267 sname = ".persistent";
7268 break;
7270 return data_section;
7271 case SECCAT_DATA_REL:
7272 sname = ".data.rel";
7273 break;
7274 case SECCAT_DATA_REL_LOCAL:
7275 sname = ".data.rel.local";
7276 break;
7277 case SECCAT_DATA_REL_RO:
7278 sname = ".data.rel.ro";
7279 break;
7280 case SECCAT_DATA_REL_RO_LOCAL:
7281 sname = ".data.rel.ro.local";
7282 break;
7283 case SECCAT_SDATA:
7284 sname = ".sdata";
7285 break;
7286 case SECCAT_TDATA:
7287 sname = ".tdata";
7288 break;
7289 case SECCAT_BSS:
7290 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7292 sname = ".noinit";
7293 break;
7295 if (bss_section)
7296 return bss_section;
7297 sname = ".bss";
7298 break;
7299 case SECCAT_SBSS:
7300 sname = ".sbss";
7301 break;
7302 case SECCAT_TBSS:
7303 sname = ".tbss";
7304 break;
7305 default:
7306 gcc_unreachable ();
7309 return get_named_section (decl, sname, reloc);
7312 /* Construct a unique section name based on the decl name and the
7313 categorization performed above. */
7315 void
7316 default_unique_section (tree decl, int reloc)
7318 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
7319 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
7320 const char *prefix, *name, *linkonce;
7321 char *string;
7322 tree id;
7324 switch (categorize_decl_for_section (decl, reloc))
7326 case SECCAT_TEXT:
7327 prefix = one_only ? ".t" : ".text";
7328 break;
7329 case SECCAT_RODATA:
7330 case SECCAT_RODATA_MERGE_STR:
7331 case SECCAT_RODATA_MERGE_STR_INIT:
7332 case SECCAT_RODATA_MERGE_CONST:
7333 prefix = one_only ? ".r" : ".rodata";
7334 break;
7335 case SECCAT_SRODATA:
7336 prefix = one_only ? ".s2" : ".sdata2";
7337 break;
7338 case SECCAT_DATA:
7339 prefix = one_only ? ".d" : ".data";
7340 if (DECL_P (decl) && DECL_PERSISTENT_P (decl))
7342 prefix = one_only ? ".p" : ".persistent";
7343 break;
7345 break;
7346 case SECCAT_DATA_REL:
7347 prefix = one_only ? ".d.rel" : ".data.rel";
7348 break;
7349 case SECCAT_DATA_REL_LOCAL:
7350 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
7351 break;
7352 case SECCAT_DATA_REL_RO:
7353 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
7354 break;
7355 case SECCAT_DATA_REL_RO_LOCAL:
7356 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
7357 break;
7358 case SECCAT_SDATA:
7359 prefix = one_only ? ".s" : ".sdata";
7360 break;
7361 case SECCAT_BSS:
7362 if (DECL_P (decl) && DECL_NOINIT_P (decl))
7364 prefix = one_only ? ".n" : ".noinit";
7365 break;
7367 prefix = one_only ? ".b" : ".bss";
7368 break;
7369 case SECCAT_SBSS:
7370 prefix = one_only ? ".sb" : ".sbss";
7371 break;
7372 case SECCAT_TDATA:
7373 prefix = one_only ? ".td" : ".tdata";
7374 break;
7375 case SECCAT_TBSS:
7376 prefix = one_only ? ".tb" : ".tbss";
7377 break;
7378 default:
7379 gcc_unreachable ();
7382 id = DECL_ASSEMBLER_NAME (decl);
7383 ultimate_transparent_alias_target (&id);
7384 name = IDENTIFIER_POINTER (id);
7385 name = targetm.strip_name_encoding (name);
7387 /* If we're using one_only, then there needs to be a .gnu.linkonce
7388 prefix to the section name. */
7389 linkonce = one_only ? ".gnu.linkonce" : "";
7391 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
7393 set_decl_section_name (decl, string);
7396 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
7398 static int
7399 compute_reloc_for_rtx_1 (const_rtx x)
7401 switch (GET_CODE (x))
7403 case SYMBOL_REF:
7404 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
7405 case LABEL_REF:
7406 return 1;
7407 default:
7408 return 0;
7412 /* Like compute_reloc_for_constant, except for an RTX. The return value
7413 is a mask for which bit 1 indicates a global relocation, and bit 0
7414 indicates a local relocation. Used by default_select_rtx_section
7415 and default_elf_select_rtx_section. */
7417 static int
7418 compute_reloc_for_rtx (const_rtx x)
7420 switch (GET_CODE (x))
7422 case SYMBOL_REF:
7423 case LABEL_REF:
7424 return compute_reloc_for_rtx_1 (x);
7426 case CONST:
7428 int reloc = 0;
7429 subrtx_iterator::array_type array;
7430 FOR_EACH_SUBRTX (iter, array, x, ALL)
7431 reloc |= compute_reloc_for_rtx_1 (*iter);
7432 return reloc;
7435 default:
7436 return 0;
7440 section *
7441 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
7442 rtx x,
7443 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
7445 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
7446 return data_section;
7447 else
7448 return readonly_data_section;
7451 section *
7452 default_elf_select_rtx_section (machine_mode mode, rtx x,
7453 unsigned HOST_WIDE_INT align)
7455 int reloc = compute_reloc_for_rtx (x);
7457 /* ??? Handle small data here somehow. */
7459 if (reloc & targetm.asm_out.reloc_rw_mask ())
7461 if (reloc == 1)
7462 return get_named_section (NULL, ".data.rel.ro.local", 1);
7463 else
7464 return get_named_section (NULL, ".data.rel.ro", 3);
7467 return mergeable_constant_section (mode, align, 0);
7470 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
7472 void
7473 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
7475 rtx symbol;
7476 int flags;
7478 /* Careful not to prod global register variables. */
7479 if (!MEM_P (rtl))
7480 return;
7481 symbol = XEXP (rtl, 0);
7482 if (GET_CODE (symbol) != SYMBOL_REF)
7483 return;
7485 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
7486 if (TREE_CODE (decl) == FUNCTION_DECL)
7487 flags |= SYMBOL_FLAG_FUNCTION;
7488 if (targetm.binds_local_p (decl))
7489 flags |= SYMBOL_FLAG_LOCAL;
7490 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7491 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
7492 else if (targetm.in_small_data_p (decl))
7493 flags |= SYMBOL_FLAG_SMALL;
7494 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
7495 being PUBLIC, the thing *must* be defined in this translation unit.
7496 Prevent this buglet from being propagated into rtl code as well. */
7497 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
7498 flags |= SYMBOL_FLAG_EXTERNAL;
7500 SYMBOL_REF_FLAGS (symbol) = flags;
7503 /* By default, we do nothing for encode_section_info, so we need not
7504 do anything but discard the '*' marker. */
7506 const char *
7507 default_strip_name_encoding (const char *str)
7509 return str + (*str == '*');
7512 #ifdef ASM_OUTPUT_DEF
7513 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
7514 anchor relative to ".", the current section position. */
7516 void
7517 default_asm_output_anchor (rtx symbol)
7519 gcc_checking_assert (TARGET_SUPPORTS_ALIASES);
7521 char buffer[100];
7523 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7524 SYMBOL_REF_BLOCK_OFFSET (symbol));
7525 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7527 #endif
7529 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
7531 bool
7532 default_use_anchors_for_symbol_p (const_rtx symbol)
7534 tree decl;
7535 section *sect = SYMBOL_REF_BLOCK (symbol)->sect;
7537 /* This function should only be called with non-zero SYMBOL_REF_BLOCK,
7538 furthermore get_block_for_section should not create object blocks
7539 for mergeable sections. */
7540 gcc_checking_assert (sect && !(sect->common.flags & SECTION_MERGE));
7542 /* Don't use anchors for small data sections. The small data register
7543 acts as an anchor for such sections. */
7544 if (sect->common.flags & SECTION_SMALL)
7545 return false;
7547 decl = SYMBOL_REF_DECL (symbol);
7548 if (decl && DECL_P (decl))
7550 /* Don't use section anchors for decls that might be defined or
7551 usurped by other modules. */
7552 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
7553 return false;
7555 /* Don't use section anchors for decls that will be placed in a
7556 small data section. */
7557 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
7558 one above. The problem is that we only use SECTION_SMALL for
7559 sections that should be marked as small in the section directive. */
7560 if (targetm.in_small_data_p (decl))
7561 return false;
7563 /* Don't use section anchors for decls that won't fit inside a single
7564 anchor range to reduce the amount of instructions required to refer
7565 to the entire declaration. */
7566 if (DECL_SIZE_UNIT (decl) == NULL_TREE
7567 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
7568 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
7569 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
7570 return false;
7573 return true;
7576 /* Return true when RESOLUTION indicate that symbol will be bound to the
7577 definition provided by current .o file. */
7579 static bool
7580 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
7582 return (resolution == LDPR_PREVAILING_DEF
7583 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7584 || resolution == LDPR_PREVAILING_DEF_IRONLY);
7587 /* Return true when RESOLUTION indicate that symbol will be bound locally
7588 within current executable or DSO. */
7590 static bool
7591 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7593 return (resolution == LDPR_PREVAILING_DEF
7594 || resolution == LDPR_PREVAILING_DEF_IRONLY
7595 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7596 || resolution == LDPR_PREEMPTED_REG
7597 || resolution == LDPR_PREEMPTED_IR
7598 || resolution == LDPR_RESOLVED_IR
7599 || resolution == LDPR_RESOLVED_EXEC);
7602 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7603 uninitialized common symbol in the executable will still be defined
7604 (through COPY relocation) in the executable. */
7606 bool
7607 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7608 bool extern_protected_data, bool common_local_p)
7610 /* A non-decl is an entry in the constant pool. */
7611 if (!DECL_P (exp))
7612 return true;
7614 /* Weakrefs may not bind locally, even though the weakref itself is always
7615 static and therefore local. Similarly, the resolver for ifunc functions
7616 might resolve to a non-local function.
7617 FIXME: We can resolve the weakref case more curefuly by looking at the
7618 weakref alias. */
7619 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7620 || (!targetm.ifunc_ref_local_ok ()
7621 && TREE_CODE (exp) == FUNCTION_DECL
7622 && cgraph_node::get (exp)
7623 && cgraph_node::get (exp)->ifunc_resolver))
7624 return false;
7626 /* Static variables are always local. */
7627 if (! TREE_PUBLIC (exp))
7628 return true;
7630 /* With resolution file in hand, take look into resolutions.
7631 We can't just return true for resolved_locally symbols,
7632 because dynamic linking might overwrite symbols
7633 in shared libraries. */
7634 bool resolved_locally = false;
7636 bool uninited_common = (DECL_COMMON (exp)
7637 && (DECL_INITIAL (exp) == NULL
7638 || (!in_lto_p
7639 && DECL_INITIAL (exp) == error_mark_node)));
7641 /* A non-external variable is defined locally only if it isn't
7642 uninitialized COMMON variable or common_local_p is true. */
7643 bool defined_locally = (!DECL_EXTERNAL (exp)
7644 && (!uninited_common || common_local_p));
7645 if (symtab_node *node = symtab_node::get (exp))
7647 if (node->in_other_partition)
7648 defined_locally = true;
7649 if (node->can_be_discarded_p ())
7651 else if (resolution_to_local_definition_p (node->resolution))
7652 defined_locally = resolved_locally = true;
7653 else if (resolution_local_p (node->resolution))
7654 resolved_locally = true;
7656 if (defined_locally && weak_dominate && !shlib)
7657 resolved_locally = true;
7659 /* Undefined weak symbols are never defined locally. */
7660 if (DECL_WEAK (exp) && !defined_locally)
7661 return false;
7663 /* A symbol is local if the user has said explicitly that it will be,
7664 or if we have a definition for the symbol. We cannot infer visibility
7665 for undefined symbols. */
7666 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7667 && (TREE_CODE (exp) == FUNCTION_DECL
7668 || !extern_protected_data
7669 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7670 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7671 return true;
7673 /* If PIC, then assume that any global name can be overridden by
7674 symbols resolved from other modules. */
7675 if (shlib)
7676 return false;
7678 /* Variables defined outside this object might not be local. */
7679 if (DECL_EXTERNAL (exp) && !resolved_locally)
7680 return false;
7682 /* Non-dominant weak symbols are not defined locally. */
7683 if (DECL_WEAK (exp) && !resolved_locally)
7684 return false;
7686 /* Uninitialized COMMON variable may be unified with symbols
7687 resolved from other modules. */
7688 if (uninited_common && !resolved_locally)
7689 return false;
7691 /* Otherwise we're left with initialized (or non-common) global data
7692 which is of necessity defined locally. */
7693 return true;
7696 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7697 wrt cross-module name binding. */
7699 bool
7700 default_binds_local_p (const_tree exp)
7702 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7705 /* Similar to default_binds_local_p, but common symbol may be local and
7706 extern protected data is non-local. */
7708 bool
7709 default_binds_local_p_2 (const_tree exp)
7711 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7712 !flag_pic);
7715 bool
7716 default_binds_local_p_1 (const_tree exp, int shlib)
7718 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7721 /* Return true when references to DECL must bind to current definition in
7722 final executable.
7724 The condition is usually equivalent to whether the function binds to the
7725 current module (shared library or executable), that is to binds_local_p.
7726 We use this fact to avoid need for another target hook and implement
7727 the logic using binds_local_p and just special cases where
7728 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7729 the weak definitions (that can be overwritten at linktime by other
7730 definition from different object file) and when resolution info is available
7731 we simply use the knowledge passed to us by linker plugin. */
7732 bool
7733 decl_binds_to_current_def_p (const_tree decl)
7735 gcc_assert (DECL_P (decl));
7736 if (!targetm.binds_local_p (decl))
7737 return false;
7738 if (!TREE_PUBLIC (decl))
7739 return true;
7741 /* When resolution is available, just use it. */
7742 if (symtab_node *node = symtab_node::get (decl))
7744 if (node->resolution != LDPR_UNKNOWN
7745 && !node->can_be_discarded_p ())
7746 return resolution_to_local_definition_p (node->resolution);
7749 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7750 binds locally but still can be overwritten), DECL_COMMON (can be merged
7751 with a non-common definition somewhere in the same module) or
7752 DECL_EXTERNAL.
7753 This rely on fact that binds_local_p behave as decl_replaceable_p
7754 for all other declaration types. */
7755 if (DECL_WEAK (decl))
7756 return false;
7757 if (DECL_COMMON (decl)
7758 && (DECL_INITIAL (decl) == NULL
7759 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7760 return false;
7761 if (DECL_EXTERNAL (decl))
7762 return false;
7763 return true;
7766 /* A replaceable function or variable is one which may be replaced
7767 at link-time with an entirely different definition, provided that the
7768 replacement has the same type. For example, functions declared
7769 with __attribute__((weak)) on most systems are replaceable.
7770 If SEMANTIC_INTERPOSITION_P is false allow interposition only on
7771 symbols explicitly declared weak.
7773 COMDAT functions are not replaceable, since all definitions of the
7774 function must be equivalent. It is important that COMDAT functions
7775 not be treated as replaceable so that use of C++ template
7776 instantiations is not penalized. */
7778 bool
7779 decl_replaceable_p (tree decl, bool semantic_interposition_p)
7781 gcc_assert (DECL_P (decl));
7782 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7783 return false;
7784 if (!semantic_interposition_p
7785 && !DECL_WEAK (decl))
7786 return false;
7787 return !decl_binds_to_current_def_p (decl);
7790 /* Default function to output code that will globalize a label. A
7791 target must define GLOBAL_ASM_OP or provide its own function to
7792 globalize a label. */
7793 #ifdef GLOBAL_ASM_OP
7794 void
7795 default_globalize_label (FILE * stream, const char *name)
7797 fputs (GLOBAL_ASM_OP, stream);
7798 assemble_name (stream, name);
7799 putc ('\n', stream);
7801 #endif /* GLOBAL_ASM_OP */
7803 /* Default function to output code that will globalize a declaration. */
7804 void
7805 default_globalize_decl_name (FILE * stream, tree decl)
7807 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7808 targetm.asm_out.globalize_label (stream, name);
7811 /* Default function to output a label for unwind information. The
7812 default is to do nothing. A target that needs nonlocal labels for
7813 unwind information must provide its own function to do this. */
7814 void
7815 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7816 tree decl ATTRIBUTE_UNUSED,
7817 int for_eh ATTRIBUTE_UNUSED,
7818 int empty ATTRIBUTE_UNUSED)
7822 /* Default function to output a label to divide up the exception table.
7823 The default is to do nothing. A target that needs/wants to divide
7824 up the table must provide it's own function to do this. */
7825 void
7826 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7830 /* This is how to output an internal numbered label where PREFIX is
7831 the class of label and LABELNO is the number within the class. */
7833 void
7834 default_generate_internal_label (char *buf, const char *prefix,
7835 unsigned long labelno)
7837 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7840 /* This is how to output an internal numbered label where PREFIX is
7841 the class of label and LABELNO is the number within the class. */
7843 void
7844 default_internal_label (FILE *stream, const char *prefix,
7845 unsigned long labelno)
7847 char *const buf = (char *) alloca (40 + strlen (prefix));
7848 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7849 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7853 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7855 void
7856 default_asm_declare_constant_name (FILE *file, const char *name,
7857 const_tree exp ATTRIBUTE_UNUSED,
7858 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7860 assemble_label (file, name);
7863 /* This is the default behavior at the beginning of a file. It's
7864 controlled by two other target-hook toggles. */
7865 void
7866 default_file_start (void)
7868 if (targetm.asm_file_start_app_off
7869 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7870 fputs (ASM_APP_OFF, asm_out_file);
7872 if (targetm.asm_file_start_file_directive)
7874 /* LTO produced units have no meaningful main_input_filename. */
7875 if (in_lto_p)
7876 output_file_directive (asm_out_file, "<artificial>");
7877 else
7878 output_file_directive (asm_out_file, main_input_filename);
7882 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7883 which emits a special section directive used to indicate whether or
7884 not this object file needs an executable stack. This is primarily
7885 a GNU extension to ELF but could be used on other targets. */
7887 int trampolines_created;
7889 void
7890 file_end_indicate_exec_stack (void)
7892 unsigned int flags = SECTION_DEBUG;
7893 if (trampolines_created)
7894 flags |= SECTION_CODE;
7896 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7899 /* Emit a special section directive to indicate that this object file
7900 was compiled with -fsplit-stack. This is used to let the linker
7901 detect calls between split-stack code and non-split-stack code, so
7902 that it can modify the split-stack code to allocate a sufficiently
7903 large stack. We emit another special section if there are any
7904 functions in this file which have the no_split_stack attribute, to
7905 prevent the linker from warning about being unable to convert the
7906 functions if they call non-split-stack code. */
7908 void
7909 file_end_indicate_split_stack (void)
7911 if (flag_split_stack)
7913 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7914 NULL));
7915 if (saw_no_split_stack)
7916 switch_to_section (get_section (".note.GNU-no-split-stack",
7917 SECTION_DEBUG, NULL));
7921 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7922 a get_unnamed_section callback. */
7924 void
7925 output_section_asm_op (const char *directive)
7927 fprintf (asm_out_file, "%s\n", directive);
7930 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7931 the current section is NEW_SECTION. */
7933 void
7934 switch_to_section (section *new_section, tree decl)
7936 bool retain_p;
7937 if ((new_section->common.flags & SECTION_NAMED)
7938 && decl != nullptr
7939 && DECL_P (decl)
7940 && ((retain_p = !!lookup_attribute ("retain",
7941 DECL_ATTRIBUTES (decl)))
7942 != !!(new_section->common.flags & SECTION_RETAIN)))
7944 /* If the SECTION_RETAIN bit doesn't match, switch to a new
7945 section. */
7946 tree used_decl, no_used_decl;
7948 if (retain_p)
7950 new_section->common.flags |= SECTION_RETAIN;
7951 used_decl = decl;
7952 no_used_decl = new_section->named.decl;
7954 else
7956 new_section->common.flags &= ~(SECTION_RETAIN
7957 | SECTION_DECLARED);
7958 used_decl = new_section->named.decl;
7959 no_used_decl = decl;
7961 if (no_used_decl != used_decl)
7963 warning (OPT_Wattributes,
7964 "%+qD without %<retain%> attribute and %qD with "
7965 "%<retain%> attribute are placed in a section with "
7966 "the same name", no_used_decl, used_decl);
7967 inform (DECL_SOURCE_LOCATION (used_decl),
7968 "%qD was declared here", used_decl);
7971 else if (in_section == new_section)
7972 return;
7974 in_section = new_section;
7976 switch (SECTION_STYLE (new_section))
7978 case SECTION_NAMED:
7979 targetm.asm_out.named_section (new_section->named.name,
7980 new_section->named.common.flags,
7981 new_section->named.decl);
7982 break;
7984 case SECTION_UNNAMED:
7985 new_section->unnamed.callback (new_section->unnamed.data);
7986 break;
7988 case SECTION_NOSWITCH:
7989 gcc_unreachable ();
7990 break;
7993 new_section->common.flags |= SECTION_DECLARED;
7996 /* If block symbol SYMBOL has not yet been assigned an offset, place
7997 it at the end of its block. */
7999 void
8000 place_block_symbol (rtx symbol)
8002 unsigned HOST_WIDE_INT size, mask, offset;
8003 class constant_descriptor_rtx *desc;
8004 unsigned int alignment;
8005 struct object_block *block;
8006 tree decl;
8008 gcc_assert (SYMBOL_REF_BLOCK (symbol));
8009 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
8010 return;
8012 /* Work out the symbol's size and alignment. */
8013 if (CONSTANT_POOL_ADDRESS_P (symbol))
8015 desc = SYMBOL_REF_CONSTANT (symbol);
8016 alignment = desc->align;
8017 size = GET_MODE_SIZE (desc->mode);
8019 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
8021 decl = SYMBOL_REF_DECL (symbol);
8022 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
8023 alignment = DECL_ALIGN (decl);
8024 size = get_constant_size (DECL_INITIAL (decl));
8025 if ((flag_sanitize & SANITIZE_ADDRESS)
8026 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8027 && asan_protect_global (DECL_INITIAL (decl)))
8029 size += asan_red_zone_size (size);
8030 alignment = MAX (alignment,
8031 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
8034 else
8036 struct symtab_node *snode;
8037 decl = SYMBOL_REF_DECL (symbol);
8039 snode = symtab_node::get (decl);
8040 if (snode->alias)
8042 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
8044 gcc_assert (MEM_P (target)
8045 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
8046 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
8047 target = XEXP (target, 0);
8048 place_block_symbol (target);
8049 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
8050 return;
8052 alignment = get_variable_align (decl);
8053 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8054 if ((flag_sanitize & SANITIZE_ADDRESS)
8055 && asan_protect_global (decl))
8057 size += asan_red_zone_size (size);
8058 alignment = MAX (alignment,
8059 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
8063 /* Calculate the object's offset from the start of the block. */
8064 block = SYMBOL_REF_BLOCK (symbol);
8065 mask = alignment / BITS_PER_UNIT - 1;
8066 offset = (block->size + mask) & ~mask;
8067 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
8069 /* Record the block's new alignment and size. */
8070 block->alignment = MAX (block->alignment, alignment);
8071 block->size = offset + size;
8073 vec_safe_push (block->objects, symbol);
8076 /* Return the anchor that should be used to address byte offset OFFSET
8077 from the first object in BLOCK. MODEL is the TLS model used
8078 to access it. */
8081 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
8082 enum tls_model model)
8084 char label[100];
8085 unsigned int begin, middle, end;
8086 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
8087 rtx anchor;
8089 /* Work out the anchor's offset. Use an offset of 0 for the first
8090 anchor so that we don't pessimize the case where we take the address
8091 of a variable at the beginning of the block. This is particularly
8092 useful when a block has only one variable assigned to it.
8094 We try to place anchors RANGE bytes apart, so there can then be
8095 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
8096 a ptr_mode offset. With some target settings, the lowest such
8097 anchor might be out of range for the lowest ptr_mode offset;
8098 likewise the highest anchor for the highest offset. Use anchors
8099 at the extreme ends of the ptr_mode range in such cases.
8101 All arithmetic uses unsigned integers in order to avoid
8102 signed overflow. */
8103 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
8104 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
8105 range = max_offset - min_offset + 1;
8106 if (range == 0)
8107 offset = 0;
8108 else
8110 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
8111 if (offset < 0)
8113 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
8114 delta -= delta % range;
8115 if (delta > bias)
8116 delta = bias;
8117 offset = (HOST_WIDE_INT) (-delta);
8119 else
8121 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
8122 delta -= delta % range;
8123 if (delta > bias - 1)
8124 delta = bias - 1;
8125 offset = (HOST_WIDE_INT) delta;
8129 /* Do a binary search to see if there's already an anchor we can use.
8130 Set BEGIN to the new anchor's index if not. */
8131 begin = 0;
8132 end = vec_safe_length (block->anchors);
8133 while (begin != end)
8135 middle = (end + begin) / 2;
8136 anchor = (*block->anchors)[middle];
8137 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
8138 end = middle;
8139 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
8140 begin = middle + 1;
8141 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
8142 end = middle;
8143 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
8144 begin = middle + 1;
8145 else
8146 return anchor;
8149 /* Create a new anchor with a unique label. */
8150 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
8151 anchor = create_block_symbol (ggc_strdup (label), block, offset);
8152 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
8153 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
8155 /* Insert it at index BEGIN. */
8156 vec_safe_insert (block->anchors, begin, anchor);
8157 return anchor;
8160 /* Output the objects in BLOCK. */
8162 static void
8163 output_object_block (struct object_block *block)
8165 class constant_descriptor_rtx *desc;
8166 unsigned int i;
8167 HOST_WIDE_INT offset;
8168 tree decl;
8169 rtx symbol;
8171 if (!block->objects)
8172 return;
8174 /* Switch to the section and make sure that the first byte is
8175 suitably aligned. */
8176 /* Special case VTV comdat sections similar to assemble_variable. */
8177 if (SECTION_STYLE (block->sect) == SECTION_NAMED
8178 && block->sect->named.name
8179 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
8180 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
8181 else
8182 switch_to_section (block->sect, SYMBOL_REF_DECL ((*block->objects)[0]));
8184 gcc_checking_assert (!(block->sect->common.flags & SECTION_MERGE));
8185 assemble_align (block->alignment);
8187 /* Define the values of all anchors relative to the current section
8188 position. */
8189 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
8190 targetm.asm_out.output_anchor (symbol);
8192 /* Output the objects themselves. */
8193 offset = 0;
8194 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
8196 /* Move to the object's offset, padding with zeros if necessary. */
8197 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
8198 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
8199 if (CONSTANT_POOL_ADDRESS_P (symbol))
8201 desc = SYMBOL_REF_CONSTANT (symbol);
8202 /* Pass 1 for align as we have already laid out everything in the block.
8203 So aligning shouldn't be necessary. */
8204 output_constant_pool_1 (desc, 1);
8205 offset += GET_MODE_SIZE (desc->mode);
8207 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
8209 HOST_WIDE_INT size;
8210 decl = SYMBOL_REF_DECL (symbol);
8211 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
8212 DECL_ALIGN (decl), false);
8214 size = get_constant_size (DECL_INITIAL (decl));
8215 offset += size;
8216 if ((flag_sanitize & SANITIZE_ADDRESS)
8217 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
8218 && asan_protect_global (DECL_INITIAL (decl)))
8220 size = asan_red_zone_size (size);
8221 assemble_zeros (size);
8222 offset += size;
8225 else
8227 HOST_WIDE_INT size;
8228 decl = SYMBOL_REF_DECL (symbol);
8229 assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
8230 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
8231 offset += size;
8232 if ((flag_sanitize & SANITIZE_ADDRESS)
8233 && asan_protect_global (decl))
8235 size = asan_red_zone_size (size);
8236 assemble_zeros (size);
8237 offset += size;
8243 /* A callback for qsort to compare object_blocks. */
8245 static int
8246 output_object_block_compare (const void *x, const void *y)
8248 object_block *p1 = *(object_block * const*)x;
8249 object_block *p2 = *(object_block * const*)y;
8251 if (p1->sect->common.flags & SECTION_NAMED
8252 && !(p2->sect->common.flags & SECTION_NAMED))
8253 return 1;
8255 if (!(p1->sect->common.flags & SECTION_NAMED)
8256 && p2->sect->common.flags & SECTION_NAMED)
8257 return -1;
8259 if (p1->sect->common.flags & SECTION_NAMED
8260 && p2->sect->common.flags & SECTION_NAMED)
8261 return strcmp (p1->sect->named.name, p2->sect->named.name);
8263 unsigned f1 = p1->sect->common.flags;
8264 unsigned f2 = p2->sect->common.flags;
8265 if (f1 == f2)
8266 return 0;
8267 return f1 < f2 ? -1 : 1;
8270 /* Output the definitions of all object_blocks. */
8272 void
8273 output_object_blocks (void)
8275 vec<object_block *, va_heap> v;
8276 v.create (object_block_htab->elements ());
8277 object_block *obj;
8278 hash_table<object_block_hasher>::iterator hi;
8280 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
8281 v.quick_push (obj);
8283 /* Sort them in order to output them in a deterministic manner,
8284 otherwise we may get .rodata sections in different orders with
8285 and without -g. */
8286 v.qsort (output_object_block_compare);
8287 unsigned i;
8288 FOR_EACH_VEC_ELT (v, i, obj)
8289 output_object_block (obj);
8291 v.release ();
8294 /* This function provides a possible implementation of the
8295 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
8296 by -frecord-gcc-switches it creates a new mergeable, string section in the
8297 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
8298 contains the switches in ASCII format.
8300 FIXME: This code does not correctly handle double quote characters
8301 that appear inside strings, (it strips them rather than preserving them).
8302 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
8303 characters - instead it treats them as sub-string separators. Since
8304 we want to emit NUL strings terminators into the object file we have to use
8305 ASM_OUTPUT_SKIP. */
8307 void
8308 elf_record_gcc_switches (const char *options)
8310 section *sec = get_section (targetm.asm_out.record_gcc_switches_section,
8311 SECTION_DEBUG | SECTION_MERGE
8312 | SECTION_STRINGS | (SECTION_ENTSIZE & 1), NULL);
8313 switch_to_section (sec);
8314 ASM_OUTPUT_ASCII (asm_out_file, options, strlen (options) + 1);
8317 /* Emit text to declare externally defined symbols. It is needed to
8318 properly support non-default visibility. */
8319 void
8320 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
8321 tree decl,
8322 const char *name ATTRIBUTE_UNUSED)
8324 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
8325 set in order to avoid putting out names that are never really
8326 used. Always output visibility specified in the source. */
8327 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
8328 && (DECL_VISIBILITY_SPECIFIED (decl)
8329 || targetm.binds_local_p (decl)))
8330 maybe_assemble_visibility (decl);
8333 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
8335 void
8336 default_asm_output_source_filename (FILE *file, const char *name)
8338 #ifdef ASM_OUTPUT_SOURCE_FILENAME
8339 ASM_OUTPUT_SOURCE_FILENAME (file, name);
8340 #else
8341 fprintf (file, "\t.file\t");
8342 output_quoted_string (file, name);
8343 putc ('\n', file);
8344 #endif
8347 /* Output a file name in the form wanted by System V. */
8349 void
8350 output_file_directive (FILE *asm_file, const char *input_name)
8352 int len;
8353 const char *na;
8355 if (input_name == NULL)
8356 input_name = "<stdin>";
8357 else
8358 input_name = remap_debug_filename (input_name);
8360 len = strlen (input_name);
8361 na = input_name + len;
8363 /* NA gets INPUT_NAME sans directory names. */
8364 while (na > input_name)
8366 if (IS_DIR_SEPARATOR (na[-1]))
8367 break;
8368 na--;
8371 targetm.asm_out.output_source_filename (asm_file, na);
8374 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
8375 EXP. */
8377 make_debug_expr_from_rtl (const_rtx exp)
8379 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
8380 machine_mode mode = GET_MODE (exp);
8381 rtx dval;
8383 DECL_ARTIFICIAL (ddecl) = 1;
8384 if (REG_P (exp) && REG_EXPR (exp))
8385 type = TREE_TYPE (REG_EXPR (exp));
8386 else if (MEM_P (exp) && MEM_EXPR (exp))
8387 type = TREE_TYPE (MEM_EXPR (exp));
8388 else
8389 type = NULL_TREE;
8390 if (type && TYPE_MODE (type) == mode)
8391 TREE_TYPE (ddecl) = type;
8392 else
8393 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
8394 SET_DECL_MODE (ddecl, mode);
8395 dval = gen_rtx_DEBUG_EXPR (mode);
8396 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
8397 SET_DECL_RTL (ddecl, dval);
8398 return dval;
8401 #ifdef ELF_ASCII_ESCAPES
8402 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
8404 void
8405 default_elf_asm_output_limited_string (FILE *f, const char *s)
8407 int escape;
8408 unsigned char c;
8410 fputs (STRING_ASM_OP, f);
8411 putc ('"', f);
8412 while (*s != '\0')
8414 c = *s;
8415 escape = ELF_ASCII_ESCAPES[c];
8416 switch (escape)
8418 case 0:
8419 putc (c, f);
8420 break;
8421 case 1:
8422 putc ('\\', f);
8423 putc ('0'+((c>>6)&7), f);
8424 putc ('0'+((c>>3)&7), f);
8425 putc ('0'+(c&7), f);
8426 break;
8427 default:
8428 putc ('\\', f);
8429 putc (escape, f);
8430 break;
8432 s++;
8434 putc ('\"', f);
8435 putc ('\n', f);
8438 /* Default ASM_OUTPUT_ASCII for ELF targets. */
8440 void
8441 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
8443 const char *limit = s + len;
8444 const char *last_null = NULL;
8445 unsigned bytes_in_chunk = 0;
8446 unsigned char c;
8447 int escape;
8449 for (; s < limit; s++)
8451 const char *p;
8453 if (bytes_in_chunk >= 60)
8455 putc ('\"', f);
8456 putc ('\n', f);
8457 bytes_in_chunk = 0;
8460 if (s > last_null)
8462 for (p = s; p < limit && *p != '\0'; p++)
8463 continue;
8464 last_null = p;
8466 else
8467 p = last_null;
8469 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
8471 if (bytes_in_chunk > 0)
8473 putc ('\"', f);
8474 putc ('\n', f);
8475 bytes_in_chunk = 0;
8478 default_elf_asm_output_limited_string (f, s);
8479 s = p;
8481 else
8483 if (bytes_in_chunk == 0)
8484 fputs (ASCII_DATA_ASM_OP "\"", f);
8486 c = *s;
8487 escape = ELF_ASCII_ESCAPES[c];
8488 switch (escape)
8490 case 0:
8491 putc (c, f);
8492 bytes_in_chunk++;
8493 break;
8494 case 1:
8495 putc ('\\', f);
8496 putc ('0'+((c>>6)&7), f);
8497 putc ('0'+((c>>3)&7), f);
8498 putc ('0'+(c&7), f);
8499 bytes_in_chunk += 4;
8500 break;
8501 default:
8502 putc ('\\', f);
8503 putc (escape, f);
8504 bytes_in_chunk += 2;
8505 break;
8511 if (bytes_in_chunk > 0)
8513 putc ('\"', f);
8514 putc ('\n', f);
8517 #endif
8519 static GTY(()) section *elf_init_array_section;
8520 static GTY(()) section *elf_fini_array_section;
8522 static section *
8523 get_elf_initfini_array_priority_section (int priority,
8524 bool constructor_p)
8526 section *sec;
8527 if (priority != DEFAULT_INIT_PRIORITY)
8529 char buf[18];
8530 sprintf (buf, "%s.%.5u",
8531 constructor_p ? ".init_array" : ".fini_array",
8532 priority);
8533 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8535 else
8537 if (constructor_p)
8539 if (elf_init_array_section == NULL)
8540 elf_init_array_section
8541 = get_section (".init_array",
8542 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8543 sec = elf_init_array_section;
8545 else
8547 if (elf_fini_array_section == NULL)
8548 elf_fini_array_section
8549 = get_section (".fini_array",
8550 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8551 sec = elf_fini_array_section;
8554 return sec;
8557 /* Use .init_array section for constructors. */
8559 void
8560 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8562 section *sec = get_elf_initfini_array_priority_section (priority,
8563 true);
8564 assemble_addr_to_section (symbol, sec);
8567 /* Use .fini_array section for destructors. */
8569 void
8570 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8572 section *sec = get_elf_initfini_array_priority_section (priority,
8573 false);
8574 assemble_addr_to_section (symbol, sec);
8577 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8579 This is a bit of a cheat. The real default is a no-op, but this
8580 hook is the default for all targets with a .ident directive. */
8582 void
8583 default_asm_output_ident_directive (const char *ident_str)
8585 const char *ident_asm_op = "\t.ident\t";
8587 /* If we are still in the front end, do not write out the string
8588 to asm_out_file. Instead, add a fake top-level asm statement.
8589 This allows the front ends to use this hook without actually
8590 writing to asm_out_file, to handle #ident or Pragma Ident. */
8591 if (symtab->state == PARSING)
8593 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8594 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8596 else
8597 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8600 /* Switch to a COMDAT section with COMDAT name of decl.
8602 FIXME: resolve_unique_section needs to deal better with
8603 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8604 that is fixed, this if-else statement can be replaced with
8605 a single call to "switch_to_section (sect)". */
8607 void
8608 switch_to_comdat_section (section *sect, tree decl)
8610 #if defined (OBJECT_FORMAT_ELF)
8611 targetm.asm_out.named_section (sect->named.name,
8612 sect->named.common.flags
8613 | SECTION_LINKONCE,
8614 decl);
8615 in_section = sect;
8616 #else
8617 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8618 Therefore the following check is used.
8619 In case a the target is PE or COFF a comdat group section
8620 is created, e.g. .vtable_map_vars$foo. The linker places
8621 everything in .vtable_map_vars at the end.
8623 A fix could be made in
8624 gcc/config/i386/winnt.cc: i386_pe_unique_section. */
8625 if (TARGET_PECOFF)
8627 char *name;
8629 if (TREE_CODE (decl) == IDENTIFIER_NODE)
8630 name = ACONCAT ((sect->named.name, "$",
8631 IDENTIFIER_POINTER (decl), NULL));
8632 else
8633 name = ACONCAT ((sect->named.name, "$",
8634 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)),
8635 NULL));
8637 targetm.asm_out.named_section (name,
8638 sect->named.common.flags
8639 | SECTION_LINKONCE,
8640 decl);
8641 in_section = sect;
8643 else
8644 switch_to_section (sect);
8645 #endif
8648 /* This function ensures that vtable_map variables are not only
8649 in the comdat section, but that each variable has its own unique
8650 comdat name. Without this the variables end up in the same section
8651 with a single comdat name. */
8653 static void
8654 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8656 switch_to_comdat_section(sect, DECL_NAME (decl));
8659 #include "gt-varasm.h"