re PR fortran/90166 (Compiler Fails at Assembler)
[official-gcc.git] / gcc / varasm.c
blobcb43248ec492d8eb3287f24947b9c17b70cd18ac
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2019 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 /* This file handles generation of all the assembler code
22 *except* the instructions of a function.
23 This includes declarations of variables and their initial values.
25 We also output the assembler code for constants stored in memory
26 and are responsible for combining constants with the same value. */
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "backend.h"
32 #include "target.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "predict.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "stringpool.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "varasm.h"
46 #include "flags.h"
47 #include "stmt.h"
48 #include "expr.h"
49 #include "expmed.h"
50 #include "output.h"
51 #include "langhooks.h"
52 #include "debug.h"
53 #include "common/common-target.h"
54 #include "stringpool.h"
55 #include "attribs.h"
56 #include "asan.h"
57 #include "rtl-iter.h"
58 #include "file-prefix-map.h" /* remap_debug_filename() */
60 #ifdef XCOFF_DEBUGGING_INFO
61 #include "xcoffout.h" /* Needed for external data declarations. */
62 #endif
64 /* The (assembler) name of the first globally-visible object output. */
65 extern GTY(()) const char *first_global_object_name;
66 extern GTY(()) const char *weak_global_object_name;
68 const char *first_global_object_name;
69 const char *weak_global_object_name;
71 struct addr_const;
72 struct constant_descriptor_rtx;
73 struct rtx_constant_pool;
75 #define n_deferred_constants (crtl->varasm.deferred_constants)
77 /* Number for making the label on the next
78 constant that is stored in memory. */
80 static GTY(()) int const_labelno;
82 /* Carry information from ASM_DECLARE_OBJECT_NAME
83 to ASM_FINISH_DECLARE_OBJECT. */
85 int size_directive_output;
87 /* The last decl for which assemble_variable was called,
88 if it did ASM_DECLARE_OBJECT_NAME.
89 If the last call to assemble_variable didn't do that,
90 this holds 0. */
92 tree last_assemble_variable_decl;
94 /* The following global variable indicates if the first basic block
95 in a function belongs to the cold partition or not. */
97 bool first_function_block_is_cold;
99 /* Whether we saw any functions with no_split_stack. */
101 static bool saw_no_split_stack;
103 static const char *strip_reg_name (const char *);
104 static int contains_pointers_p (tree);
105 #ifdef ASM_OUTPUT_EXTERNAL
106 static bool incorporeal_function_p (tree);
107 #endif
108 static void decode_addr_const (tree, struct addr_const *);
109 static hashval_t const_hash_1 (const tree);
110 static int compare_constant (const tree, const tree);
111 static void output_constant_def_contents (rtx);
112 static void output_addressed_constants (tree);
113 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
114 unsigned int, bool, bool);
115 static void globalize_decl (tree);
116 static bool decl_readonly_section_1 (enum section_category);
117 #ifdef BSS_SECTION_ASM_OP
118 #ifdef ASM_OUTPUT_ALIGNED_BSS
119 static void asm_output_aligned_bss (FILE *, tree, const char *,
120 unsigned HOST_WIDE_INT, int)
121 ATTRIBUTE_UNUSED;
122 #endif
123 #endif /* BSS_SECTION_ASM_OP */
124 static void mark_weak (tree);
125 static void output_constant_pool (const char *, tree);
126 static void handle_vtv_comdat_section (section *, const_tree);
128 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
129 section *text_section;
130 section *data_section;
131 section *readonly_data_section;
132 section *sdata_section;
133 section *ctors_section;
134 section *dtors_section;
135 section *bss_section;
136 section *sbss_section;
138 /* Various forms of common section. All are guaranteed to be nonnull. */
139 section *tls_comm_section;
140 section *comm_section;
141 section *lcomm_section;
143 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
144 May be null. */
145 section *bss_noswitch_section;
147 /* The section that holds the main exception table, when known. The section
148 is set either by the target's init_sections hook or by the first call to
149 switch_to_exception_section. */
150 section *exception_section;
152 /* The section that holds the DWARF2 frame unwind information, when known.
153 The section is set either by the target's init_sections hook or by the
154 first call to switch_to_eh_frame_section. */
155 section *eh_frame_section;
157 /* asm_out_file's current section. This is NULL if no section has yet
158 been selected or if we lose track of what the current section is. */
159 section *in_section;
161 /* True if code for the current function is currently being directed
162 at the cold section. */
163 bool in_cold_section_p;
165 /* The following global holds the "function name" for the code in the
166 cold section of a function, if hot/cold function splitting is enabled
167 and there was actually code that went into the cold section. A
168 pseudo function name is needed for the cold section of code for some
169 debugging tools that perform symbolization. */
170 tree cold_function_name = NULL_TREE;
172 /* A linked list of all the unnamed sections. */
173 static GTY(()) section *unnamed_sections;
175 /* Return a nonzero value if DECL has a section attribute. */
176 #define IN_NAMED_SECTION(DECL) \
177 (VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
179 struct section_hasher : ggc_ptr_hash<section>
181 typedef const char *compare_type;
183 static hashval_t hash (section *);
184 static bool equal (section *, const char *);
187 /* Hash table of named sections. */
188 static GTY(()) hash_table<section_hasher> *section_htab;
190 struct object_block_hasher : ggc_ptr_hash<object_block>
192 typedef const section *compare_type;
194 static hashval_t hash (object_block *);
195 static bool equal (object_block *, const section *);
198 /* A table of object_blocks, indexed by section. */
199 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
201 /* The next number to use for internal anchor labels. */
202 static GTY(()) int anchor_labelno;
204 /* A pool of constants that can be shared between functions. */
205 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
207 /* Helper routines for maintaining section_htab. */
209 bool
210 section_hasher::equal (section *old, const char *new_name)
212 return strcmp (old->named.name, new_name) == 0;
215 hashval_t
216 section_hasher::hash (section *old)
218 return htab_hash_string (old->named.name);
221 /* Return a hash value for section SECT. */
223 static hashval_t
224 hash_section (section *sect)
226 if (sect->common.flags & SECTION_NAMED)
227 return htab_hash_string (sect->named.name);
228 return sect->common.flags & ~SECTION_DECLARED;
231 /* Helper routines for maintaining object_block_htab. */
233 inline bool
234 object_block_hasher::equal (object_block *old, const section *new_section)
236 return old->sect == new_section;
239 hashval_t
240 object_block_hasher::hash (object_block *old)
242 return hash_section (old->sect);
245 /* Return a new unnamed section with the given fields. */
247 section *
248 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
249 const void *data)
251 section *sect;
253 sect = ggc_alloc<section> ();
254 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
255 sect->unnamed.callback = callback;
256 sect->unnamed.data = data;
257 sect->unnamed.next = unnamed_sections;
259 unnamed_sections = sect;
260 return sect;
263 /* Return a SECTION_NOSWITCH section with the given fields. */
265 static section *
266 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
268 section *sect;
270 sect = ggc_alloc<section> ();
271 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
272 sect->noswitch.callback = callback;
274 return sect;
277 /* Return the named section structure associated with NAME. Create
278 a new section with the given fields if no such structure exists. */
280 section *
281 get_section (const char *name, unsigned int flags, tree decl)
283 section *sect, **slot;
285 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
286 INSERT);
287 flags |= SECTION_NAMED;
288 if (*slot == NULL)
290 sect = ggc_alloc<section> ();
291 sect->named.common.flags = flags;
292 sect->named.name = ggc_strdup (name);
293 sect->named.decl = decl;
294 *slot = sect;
296 else
298 sect = *slot;
299 /* It is fine if one of the sections has SECTION_NOTYPE as long as
300 the other has none of the contrary flags (see the logic at the end
301 of default_section_type_flags, below). */
302 if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
303 && !((sect->common.flags | flags)
304 & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
305 | (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
307 sect->common.flags |= SECTION_NOTYPE;
308 flags |= SECTION_NOTYPE;
310 if ((sect->common.flags & ~SECTION_DECLARED) != flags
311 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
313 /* It is fine if one of the section flags is
314 SECTION_WRITE | SECTION_RELRO and the other has none of these
315 flags (i.e. read-only) in named sections and either the
316 section hasn't been declared yet or has been declared as writable.
317 In that case just make sure the resulting flags are
318 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
319 relocations. */
320 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
321 == (SECTION_WRITE | SECTION_RELRO)
322 && (sect->common.flags
323 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
324 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
325 && ((sect->common.flags & SECTION_DECLARED) == 0
326 || (sect->common.flags & SECTION_WRITE)))
328 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
329 return sect;
331 /* Sanity check user variables for flag changes. */
332 if (sect->named.decl != NULL
333 && DECL_P (sect->named.decl)
334 && decl != sect->named.decl)
336 if (decl != NULL && DECL_P (decl))
337 error ("%+qD causes a section type conflict with %qD",
338 decl, sect->named.decl);
339 else
340 error ("section type conflict with %qD", sect->named.decl);
341 inform (DECL_SOURCE_LOCATION (sect->named.decl),
342 "%qD was declared here", sect->named.decl);
344 else if (decl != NULL && DECL_P (decl))
345 error ("%+qD causes a section type conflict", decl);
346 else
347 error ("section type conflict");
348 /* Make sure we don't error about one section multiple times. */
349 sect->common.flags |= SECTION_OVERRIDE;
352 return sect;
355 /* Return true if the current compilation mode benefits from having
356 objects grouped into blocks. */
358 static bool
359 use_object_blocks_p (void)
361 return flag_section_anchors;
364 /* Return the object_block structure for section SECT. Create a new
365 structure if we haven't created one already. Return null if SECT
366 itself is null. Return also null for mergeable sections since
367 section anchors can't be used in mergeable sections anyway,
368 because the linker might move objects around, and using the
369 object blocks infrastructure in that case is both a waste and a
370 maintenance burden. */
372 static struct object_block *
373 get_block_for_section (section *sect)
375 struct object_block *block;
377 if (sect == NULL)
378 return NULL;
380 if (sect->common.flags & SECTION_MERGE)
381 return NULL;
383 object_block **slot
384 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
385 INSERT);
386 block = *slot;
387 if (block == NULL)
389 block = ggc_cleared_alloc<object_block> ();
390 block->sect = sect;
391 *slot = block;
393 return block;
396 /* Create a symbol with label LABEL and place it at byte offset
397 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
398 is not yet known. LABEL must be a garbage-collected string. */
400 static rtx
401 create_block_symbol (const char *label, struct object_block *block,
402 HOST_WIDE_INT offset)
404 rtx symbol;
405 unsigned int size;
407 /* Create the extended SYMBOL_REF. */
408 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
409 symbol = (rtx) ggc_internal_alloc (size);
411 /* Initialize the normal SYMBOL_REF fields. */
412 memset (symbol, 0, size);
413 PUT_CODE (symbol, SYMBOL_REF);
414 PUT_MODE (symbol, Pmode);
415 XSTR (symbol, 0) = label;
416 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
418 /* Initialize the block_symbol stuff. */
419 SYMBOL_REF_BLOCK (symbol) = block;
420 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
422 return symbol;
425 /* Return a section with a particular name and with whatever SECTION_*
426 flags section_type_flags deems appropriate. The name of the section
427 is taken from NAME if nonnull, otherwise it is taken from DECL's
428 DECL_SECTION_NAME. DECL is the decl associated with the section
429 (see the section comment for details) and RELOC is as for
430 section_type_flags. */
432 section *
433 get_named_section (tree decl, const char *name, int reloc)
435 unsigned int flags;
437 if (name == NULL)
439 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
440 name = DECL_SECTION_NAME (decl);
443 flags = targetm.section_type_flags (decl, name, reloc);
444 return get_section (name, flags, decl);
447 /* Worker for resolve_unique_section. */
449 static bool
450 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
452 n->implicit_section = true;
453 return false;
456 /* If required, set DECL_SECTION_NAME to a unique name. */
458 void
459 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
460 int flag_function_or_data_sections)
462 if (DECL_SECTION_NAME (decl) == NULL
463 && targetm_common.have_named_sections
464 && (flag_function_or_data_sections
465 || DECL_COMDAT_GROUP (decl)))
467 targetm.asm_out.unique_section (decl, reloc);
468 if (DECL_SECTION_NAME (decl))
469 symtab_node::get (decl)->call_for_symbol_and_aliases
470 (set_implicit_section, NULL, true);
474 #ifdef BSS_SECTION_ASM_OP
476 #ifdef ASM_OUTPUT_ALIGNED_BSS
478 /* Utility function for targets to use in implementing
479 ASM_OUTPUT_ALIGNED_BSS.
480 ??? It is believed that this function will work in most cases so such
481 support is localized here. */
483 static void
484 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
485 const char *name, unsigned HOST_WIDE_INT size,
486 int align)
488 switch_to_section (bss_section);
489 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
490 #ifdef ASM_DECLARE_OBJECT_NAME
491 last_assemble_variable_decl = decl;
492 ASM_DECLARE_OBJECT_NAME (file, name, decl);
493 #else
494 /* Standard thing is just output label for the object. */
495 ASM_OUTPUT_LABEL (file, name);
496 #endif /* ASM_DECLARE_OBJECT_NAME */
497 ASM_OUTPUT_SKIP (file, size ? size : 1);
500 #endif
502 #endif /* BSS_SECTION_ASM_OP */
504 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
505 /* Return the hot section for function DECL. Return text_section for
506 null DECLs. */
508 static section *
509 hot_function_section (tree decl)
511 if (decl != NULL_TREE
512 && DECL_SECTION_NAME (decl) != NULL
513 && targetm_common.have_named_sections)
514 return get_named_section (decl, NULL, 0);
515 else
516 return text_section;
518 #endif
520 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
521 is NULL.
523 When DECL_SECTION_NAME is non-NULL and it is implicit section and
524 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
525 concatenate the name with NAMED_SECTION_SUFFIX.
526 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
528 section *
529 get_named_text_section (tree decl,
530 const char *text_section_name,
531 const char *named_section_suffix)
533 if (decl && DECL_SECTION_NAME (decl))
535 if (named_section_suffix)
537 const char *dsn = DECL_SECTION_NAME (decl);
538 const char *stripped_name;
539 char *name, *buffer;
541 name = (char *) alloca (strlen (dsn) + 1);
542 memcpy (name, dsn,
543 strlen (dsn) + 1);
545 stripped_name = targetm.strip_name_encoding (name);
547 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
548 return get_named_section (decl, buffer, 0);
550 else if (symtab_node::get (decl)->implicit_section)
552 const char *name;
554 /* Do not try to split gnu_linkonce functions. This gets somewhat
555 slipperly. */
556 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
557 return NULL;
558 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
559 name = targetm.strip_name_encoding (name);
560 return get_named_section (decl, ACONCAT ((text_section_name, ".",
561 name, NULL)), 0);
563 else
564 return NULL;
566 return get_named_section (decl, text_section_name, 0);
569 /* Choose named function section based on its frequency. */
571 section *
572 default_function_section (tree decl, enum node_frequency freq,
573 bool startup, bool exit)
575 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
576 /* Old GNU linkers have buggy --gc-section support, which sometimes
577 results in .gcc_except_table* sections being garbage collected. */
578 if (decl
579 && symtab_node::get (decl)->implicit_section)
580 return NULL;
581 #endif
583 if (!flag_reorder_functions
584 || !targetm_common.have_named_sections)
585 return NULL;
586 /* Startup code should go to startup subsection unless it is
587 unlikely executed (this happens especially with function splitting
588 where we can split away unnecessary parts of static constructors. */
589 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
591 /* If we do have a profile or(and) LTO phase is executed, we do not need
592 these ELF section. */
593 if (!in_lto_p || !flag_profile_values)
594 return get_named_text_section (decl, ".text.startup", NULL);
595 else
596 return NULL;
599 /* Similarly for exit. */
600 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
601 return get_named_text_section (decl, ".text.exit", NULL);
603 /* Group cold functions together, similarly for hot code. */
604 switch (freq)
606 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
607 return get_named_text_section (decl, ".text.unlikely", NULL);
608 case NODE_FREQUENCY_HOT:
609 /* If we do have a profile or(and) LTO phase is executed, we do not need
610 these ELF section. */
611 if (!in_lto_p || !flag_profile_values)
612 return get_named_text_section (decl, ".text.hot", NULL);
613 /* FALLTHRU */
614 default:
615 return NULL;
619 /* Return the section for function DECL.
621 If DECL is NULL_TREE, return the text section. We can be passed
622 NULL_TREE under some circumstances by dbxout.c at least.
624 If FORCE_COLD is true, return cold function section ignoring
625 the frequency info of cgraph_node. */
627 static section *
628 function_section_1 (tree decl, bool force_cold)
630 section *section = NULL;
631 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
632 bool startup = false, exit = false;
634 if (decl)
636 struct cgraph_node *node = cgraph_node::get (decl);
638 if (node)
640 freq = node->frequency;
641 startup = node->only_called_at_startup;
642 exit = node->only_called_at_exit;
645 if (force_cold)
646 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
648 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
649 if (decl != NULL_TREE
650 && DECL_SECTION_NAME (decl) != NULL)
652 if (targetm.asm_out.function_section)
653 section = targetm.asm_out.function_section (decl, freq,
654 startup, exit);
655 if (section)
656 return section;
657 return get_named_section (decl, NULL, 0);
659 else
660 return targetm.asm_out.select_section
661 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
662 symtab_node::get (decl)->definition_alignment ());
663 #else
664 if (targetm.asm_out.function_section)
665 section = targetm.asm_out.function_section (decl, freq, startup, exit);
666 if (section)
667 return section;
668 return hot_function_section (decl);
669 #endif
672 /* Return the section for function DECL.
674 If DECL is NULL_TREE, return the text section. We can be passed
675 NULL_TREE under some circumstances by dbxout.c at least. */
677 section *
678 function_section (tree decl)
680 /* Handle cases where function splitting code decides
681 to put function entry point into unlikely executed section
682 despite the fact that the function itself is not cold
683 (i.e. it is called rarely but contains a hot loop that is
684 better to live in hot subsection for the code locality). */
685 return function_section_1 (decl,
686 first_function_block_is_cold);
689 /* Return the section for the current function, take IN_COLD_SECTION_P
690 into account. */
692 section *
693 current_function_section (void)
695 return function_section_1 (current_function_decl, in_cold_section_p);
698 /* Tell assembler to switch to unlikely-to-be-executed text section. */
700 section *
701 unlikely_text_section (void)
703 return function_section_1 (current_function_decl, true);
706 /* When called within a function context, return true if the function
707 has been assigned a cold text section and if SECT is that section.
708 When called outside a function context, return true if SECT is the
709 default cold section. */
711 bool
712 unlikely_text_section_p (section *sect)
714 return sect == function_section_1 (current_function_decl, true);
717 /* Switch to the other function partition (if inside of hot section
718 into cold section, otherwise into the hot section). */
720 void
721 switch_to_other_text_partition (void)
723 in_cold_section_p = !in_cold_section_p;
724 switch_to_section (current_function_section ());
727 /* Return the read-only data section associated with function DECL. */
729 section *
730 default_function_rodata_section (tree decl)
732 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
734 const char *name = DECL_SECTION_NAME (decl);
736 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
738 const char *dot;
739 size_t len;
740 char* rname;
742 dot = strchr (name + 1, '.');
743 if (!dot)
744 dot = name;
745 len = strlen (dot) + 8;
746 rname = (char *) alloca (len);
748 strcpy (rname, ".rodata");
749 strcat (rname, dot);
750 return get_section (rname, SECTION_LINKONCE, decl);
752 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
753 else if (DECL_COMDAT_GROUP (decl)
754 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
756 size_t len = strlen (name) + 1;
757 char *rname = (char *) alloca (len);
759 memcpy (rname, name, len);
760 rname[14] = 'r';
761 return get_section (rname, SECTION_LINKONCE, decl);
763 /* For .text.foo we want to use .rodata.foo. */
764 else if (flag_function_sections && flag_data_sections
765 && strncmp (name, ".text.", 6) == 0)
767 size_t len = strlen (name) + 1;
768 char *rname = (char *) alloca (len + 2);
770 memcpy (rname, ".rodata", 7);
771 memcpy (rname + 7, name + 5, len - 5);
772 return get_section (rname, 0, decl);
776 return readonly_data_section;
779 /* Return the read-only data section associated with function DECL
780 for targets where that section should be always the single
781 readonly data section. */
783 section *
784 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
786 return readonly_data_section;
789 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
791 static const char *
792 function_mergeable_rodata_prefix (void)
794 section *s = targetm.asm_out.function_rodata_section (current_function_decl);
795 if (SECTION_STYLE (s) == SECTION_NAMED)
796 return s->named.name;
797 else
798 return targetm.asm_out.mergeable_rodata_prefix;
801 /* Return the section to use for string merging. */
803 static section *
804 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
805 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
806 unsigned int flags ATTRIBUTE_UNUSED)
808 HOST_WIDE_INT len;
810 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
811 && TREE_CODE (decl) == STRING_CST
812 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
813 && align <= 256
814 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
815 && TREE_STRING_LENGTH (decl) == len)
817 scalar_int_mode mode;
818 unsigned int modesize;
819 const char *str;
820 HOST_WIDE_INT i;
821 int j, unit;
822 const char *prefix = function_mergeable_rodata_prefix ();
823 char *name = (char *) alloca (strlen (prefix) + 30);
825 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
826 modesize = GET_MODE_BITSIZE (mode);
827 if (modesize >= 8 && modesize <= 256
828 && (modesize & (modesize - 1)) == 0)
830 if (align < modesize)
831 align = modesize;
833 if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
834 return readonly_data_section;
836 str = TREE_STRING_POINTER (decl);
837 unit = GET_MODE_SIZE (mode);
839 /* Check for embedded NUL characters. */
840 for (i = 0; i < len; i += unit)
842 for (j = 0; j < unit; j++)
843 if (str[i + j] != '\0')
844 break;
845 if (j == unit)
846 break;
848 if (i == len - unit || (unit == 1 && i == len))
850 sprintf (name, "%s.str%d.%d", prefix,
851 modesize / 8, (int) (align / 8));
852 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
853 return get_section (name, flags, NULL);
858 return readonly_data_section;
861 /* Return the section to use for constant merging. */
863 section *
864 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
865 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
866 unsigned int flags ATTRIBUTE_UNUSED)
868 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
869 && mode != VOIDmode
870 && mode != BLKmode
871 && known_le (GET_MODE_BITSIZE (mode), align)
872 && align >= 8
873 && align <= 256
874 && (align & (align - 1)) == 0
875 && (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
877 const char *prefix = function_mergeable_rodata_prefix ();
878 char *name = (char *) alloca (strlen (prefix) + 30);
880 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
881 flags |= (align / 8) | SECTION_MERGE;
882 return get_section (name, flags, NULL);
884 return readonly_data_section;
887 /* Given NAME, a putative register name, discard any customary prefixes. */
889 static const char *
890 strip_reg_name (const char *name)
892 #ifdef REGISTER_PREFIX
893 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
894 name += strlen (REGISTER_PREFIX);
895 #endif
896 if (name[0] == '%' || name[0] == '#')
897 name++;
898 return name;
901 /* The user has asked for a DECL to have a particular name. Set (or
902 change) it in such a way that we don't prefix an underscore to
903 it. */
904 void
905 set_user_assembler_name (tree decl, const char *name)
907 char *starred = (char *) alloca (strlen (name) + 2);
908 starred[0] = '*';
909 strcpy (starred + 1, name);
910 symtab->change_decl_assembler_name (decl, get_identifier (starred));
911 SET_DECL_RTL (decl, NULL_RTX);
914 /* Decode an `asm' spec for a declaration as a register name.
915 Return the register number, or -1 if nothing specified,
916 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
917 or -3 if ASMSPEC is `cc' and is not recognized,
918 or -4 if ASMSPEC is `memory' and is not recognized.
919 Accept an exact spelling or a decimal number.
920 Prefixes such as % are optional. */
923 decode_reg_name_and_count (const char *asmspec, int *pnregs)
925 /* Presume just one register is clobbered. */
926 *pnregs = 1;
928 if (asmspec != 0)
930 int i;
932 /* Get rid of confusing prefixes. */
933 asmspec = strip_reg_name (asmspec);
935 /* Allow a decimal number as a "register name". */
936 for (i = strlen (asmspec) - 1; i >= 0; i--)
937 if (! ISDIGIT (asmspec[i]))
938 break;
939 if (asmspec[0] != 0 && i < 0)
941 i = atoi (asmspec);
942 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
943 return i;
944 else
945 return -2;
948 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
949 if (reg_names[i][0]
950 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
951 return i;
953 #ifdef OVERLAPPING_REGISTER_NAMES
955 static const struct
957 const char *const name;
958 const int number;
959 const int nregs;
960 } table[] = OVERLAPPING_REGISTER_NAMES;
962 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
963 if (table[i].name[0]
964 && ! strcmp (asmspec, table[i].name))
966 *pnregs = table[i].nregs;
967 return table[i].number;
970 #endif /* OVERLAPPING_REGISTER_NAMES */
972 #ifdef ADDITIONAL_REGISTER_NAMES
974 static const struct { const char *const name; const int number; } table[]
975 = ADDITIONAL_REGISTER_NAMES;
977 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
978 if (table[i].name[0]
979 && ! strcmp (asmspec, table[i].name)
980 && reg_names[table[i].number][0])
981 return table[i].number;
983 #endif /* ADDITIONAL_REGISTER_NAMES */
985 if (!strcmp (asmspec, "memory"))
986 return -4;
988 if (!strcmp (asmspec, "cc"))
989 return -3;
991 return -2;
994 return -1;
998 decode_reg_name (const char *name)
1000 int count;
1001 return decode_reg_name_and_count (name, &count);
1005 /* Return true if DECL's initializer is suitable for a BSS section. */
1007 bool
1008 bss_initializer_p (const_tree decl, bool named)
1010 /* Do not put non-common constants into the .bss section, they belong in
1011 a readonly section, except when NAMED is true. */
1012 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1013 && (DECL_INITIAL (decl) == NULL
1014 /* In LTO we have no errors in program; error_mark_node is used
1015 to mark offlined constructors. */
1016 || (DECL_INITIAL (decl) == error_mark_node
1017 && !in_lto_p)
1018 || (flag_zero_initialized_in_bss
1019 && initializer_zerop (DECL_INITIAL (decl)))));
1022 /* Compute the alignment of variable specified by DECL.
1023 DONT_OUTPUT_DATA is from assemble_variable. */
1025 void
1026 align_variable (tree decl, bool dont_output_data)
1028 unsigned int align = DECL_ALIGN (decl);
1030 /* In the case for initialing an array whose length isn't specified,
1031 where we have not yet been able to do the layout,
1032 figure out the proper alignment now. */
1033 if (dont_output_data && DECL_SIZE (decl) == 0
1034 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1035 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1037 /* Some object file formats have a maximum alignment which they support.
1038 In particular, a.out format supports a maximum alignment of 4. */
1039 if (align > MAX_OFILE_ALIGNMENT)
1041 error ("alignment of %q+D is greater than maximum object "
1042 "file alignment %d", decl,
1043 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1044 align = MAX_OFILE_ALIGNMENT;
1047 if (! DECL_USER_ALIGN (decl))
1049 #ifdef DATA_ABI_ALIGNMENT
1050 unsigned int data_abi_align
1051 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1052 /* For backwards compatibility, don't assume the ABI alignment for
1053 TLS variables. */
1054 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1055 align = data_abi_align;
1056 #endif
1058 /* On some machines, it is good to increase alignment sometimes.
1059 But as DECL_ALIGN is used both for actually emitting the variable
1060 and for code accessing the variable as guaranteed alignment, we
1061 can only increase the alignment if it is a performance optimization
1062 if the references to it must bind to the current definition. */
1063 if (decl_binds_to_current_def_p (decl)
1064 && !DECL_VIRTUAL_P (decl))
1066 #ifdef DATA_ALIGNMENT
1067 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1068 /* Don't increase alignment too much for TLS variables - TLS space
1069 is too precious. */
1070 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1071 align = data_align;
1072 #endif
1073 if (DECL_INITIAL (decl) != 0
1074 /* In LTO we have no errors in program; error_mark_node is used
1075 to mark offlined constructors. */
1076 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1078 unsigned int const_align
1079 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1080 /* Don't increase alignment too much for TLS variables - TLS
1081 space is too precious. */
1082 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1083 align = const_align;
1088 /* Reset the alignment in case we have made it tighter, so we can benefit
1089 from it in get_pointer_alignment. */
1090 SET_DECL_ALIGN (decl, align);
1093 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1094 beyond what align_variable returned. */
1096 static unsigned int
1097 get_variable_align (tree decl)
1099 unsigned int align = DECL_ALIGN (decl);
1101 /* For user aligned vars or static vars align_variable already did
1102 everything. */
1103 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1104 return align;
1106 #ifdef DATA_ABI_ALIGNMENT
1107 if (DECL_THREAD_LOCAL_P (decl))
1108 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1109 #endif
1111 /* For decls that bind to the current definition, align_variable
1112 did also everything, except for not assuming ABI required alignment
1113 of TLS variables. For other vars, increase the alignment here
1114 as an optimization. */
1115 if (!decl_binds_to_current_def_p (decl))
1117 /* On some machines, it is good to increase alignment sometimes. */
1118 #ifdef DATA_ALIGNMENT
1119 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1120 /* Don't increase alignment too much for TLS variables - TLS space
1121 is too precious. */
1122 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1123 align = data_align;
1124 #endif
1125 if (DECL_INITIAL (decl) != 0
1126 /* In LTO we have no errors in program; error_mark_node is used
1127 to mark offlined constructors. */
1128 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1130 unsigned int const_align
1131 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1132 /* Don't increase alignment too much for TLS variables - TLS space
1133 is too precious. */
1134 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1135 align = const_align;
1139 return align;
1142 /* Return the section into which the given VAR_DECL or CONST_DECL
1143 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1144 section should be used wherever possible. */
1146 section *
1147 get_variable_section (tree decl, bool prefer_noswitch_p)
1149 addr_space_t as = ADDR_SPACE_GENERIC;
1150 int reloc;
1151 varpool_node *vnode = varpool_node::get (decl);
1152 if (vnode)
1154 vnode = vnode->ultimate_alias_target ();
1155 decl = vnode->decl;
1158 if (TREE_TYPE (decl) != error_mark_node)
1159 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1161 /* We need the constructor to figure out reloc flag. */
1162 if (vnode)
1163 vnode->get_constructor ();
1165 if (DECL_COMMON (decl))
1167 /* If the decl has been given an explicit section name, or it resides
1168 in a non-generic address space, then it isn't common, and shouldn't
1169 be handled as such. */
1170 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1171 && ADDR_SPACE_GENERIC_P (as));
1172 if (DECL_THREAD_LOCAL_P (decl))
1173 return tls_comm_section;
1174 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1175 return comm_section;
1178 if (DECL_INITIAL (decl) == error_mark_node)
1179 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1180 else if (DECL_INITIAL (decl))
1181 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1182 else
1183 reloc = 0;
1185 resolve_unique_section (decl, reloc, flag_data_sections);
1186 if (IN_NAMED_SECTION (decl))
1188 section *sect = get_named_section (decl, NULL, reloc);
1190 if ((sect->common.flags & SECTION_BSS)
1191 && !bss_initializer_p (decl, true))
1193 error_at (DECL_SOURCE_LOCATION (decl),
1194 "only zero initializers are allowed in section %qs",
1195 sect->named.name);
1196 DECL_INITIAL (decl) = error_mark_node;
1198 return sect;
1201 if (ADDR_SPACE_GENERIC_P (as)
1202 && !DECL_THREAD_LOCAL_P (decl)
1203 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1204 && bss_initializer_p (decl))
1206 if (!TREE_PUBLIC (decl)
1207 && !((flag_sanitize & SANITIZE_ADDRESS)
1208 && asan_protect_global (decl)))
1209 return lcomm_section;
1210 if (bss_noswitch_section)
1211 return bss_noswitch_section;
1214 return targetm.asm_out.select_section (decl, reloc,
1215 get_variable_align (decl));
1218 /* Return the block into which object_block DECL should be placed. */
1220 static struct object_block *
1221 get_block_for_decl (tree decl)
1223 section *sect;
1225 if (VAR_P (decl))
1227 /* The object must be defined in this translation unit. */
1228 if (DECL_EXTERNAL (decl))
1229 return NULL;
1231 /* There's no point using object blocks for something that is
1232 isolated by definition. */
1233 if (DECL_COMDAT_GROUP (decl))
1234 return NULL;
1237 /* We can only calculate block offsets if the decl has a known
1238 constant size. */
1239 if (DECL_SIZE_UNIT (decl) == NULL)
1240 return NULL;
1241 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1242 return NULL;
1244 /* Find out which section should contain DECL. We cannot put it into
1245 an object block if it requires a standalone definition. */
1246 if (VAR_P (decl))
1247 align_variable (decl, 0);
1248 sect = get_variable_section (decl, true);
1249 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1250 return NULL;
1252 return get_block_for_section (sect);
1255 /* Make sure block symbol SYMBOL is in block BLOCK. */
1257 static void
1258 change_symbol_block (rtx symbol, struct object_block *block)
1260 if (block != SYMBOL_REF_BLOCK (symbol))
1262 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1263 SYMBOL_REF_BLOCK (symbol) = block;
1267 /* Return true if it is possible to put DECL in an object_block. */
1269 static bool
1270 use_blocks_for_decl_p (tree decl)
1272 struct symtab_node *snode;
1274 /* Only data DECLs can be placed into object blocks. */
1275 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1276 return false;
1278 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1279 are never used from code directly and we never want object block handling
1280 for those. */
1281 if (DECL_INITIAL (decl) == decl)
1282 return false;
1284 /* If this decl is an alias, then we don't want to emit a
1285 definition. */
1286 if (VAR_P (decl)
1287 && (snode = symtab_node::get (decl)) != NULL
1288 && snode->alias)
1289 return false;
1291 return targetm.use_blocks_for_decl_p (decl);
1294 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1295 until we find an identifier that is not itself a transparent alias.
1296 Modify the alias passed to it by reference (and all aliases on the
1297 way to the ultimate target), such that they do not have to be
1298 followed again, and return the ultimate target of the alias
1299 chain. */
1301 static inline tree
1302 ultimate_transparent_alias_target (tree *alias)
1304 tree target = *alias;
1306 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1308 gcc_assert (TREE_CHAIN (target));
1309 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1310 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1311 && ! TREE_CHAIN (target));
1312 *alias = target;
1315 return target;
1318 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1319 have static storage duration. In other words, it should not be an
1320 automatic variable, including PARM_DECLs.
1322 There is, however, one exception: this function handles variables
1323 explicitly placed in a particular register by the user.
1325 This is never called for PARM_DECL nodes. */
1327 void
1328 make_decl_rtl (tree decl)
1330 const char *name = 0;
1331 int reg_number;
1332 tree id;
1333 rtx x;
1335 /* Check that we are not being given an automatic variable. */
1336 gcc_assert (TREE_CODE (decl) != PARM_DECL
1337 && TREE_CODE (decl) != RESULT_DECL);
1339 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1340 gcc_assert (!VAR_P (decl)
1341 || TREE_STATIC (decl)
1342 || TREE_PUBLIC (decl)
1343 || DECL_EXTERNAL (decl)
1344 || DECL_REGISTER (decl));
1346 /* And that we were not given a type or a label. */
1347 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1348 && TREE_CODE (decl) != LABEL_DECL);
1350 /* For a duplicate declaration, we can be called twice on the
1351 same DECL node. Don't discard the RTL already made. */
1352 if (DECL_RTL_SET_P (decl))
1354 /* If the old RTL had the wrong mode, fix the mode. */
1355 x = DECL_RTL (decl);
1356 if (GET_MODE (x) != DECL_MODE (decl))
1357 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1359 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1360 return;
1362 /* ??? Another way to do this would be to maintain a hashed
1363 table of such critters. Instead of adding stuff to a DECL
1364 to give certain attributes to it, we could use an external
1365 hash map from DECL to set of attributes. */
1367 /* Let the target reassign the RTL if it wants.
1368 This is necessary, for example, when one machine specific
1369 decl attribute overrides another. */
1370 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1372 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1373 on the new decl information. */
1374 if (MEM_P (x)
1375 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1376 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1377 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1379 return;
1382 /* If this variable belongs to the global constant pool, retrieve the
1383 pre-computed RTL or recompute it in LTO mode. */
1384 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1386 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1387 return;
1390 id = DECL_ASSEMBLER_NAME (decl);
1391 name = IDENTIFIER_POINTER (id);
1393 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1394 && DECL_REGISTER (decl))
1396 error ("register name not specified for %q+D", decl);
1398 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1400 const char *asmspec = name+1;
1401 machine_mode mode = DECL_MODE (decl);
1402 reg_number = decode_reg_name (asmspec);
1403 /* First detect errors in declaring global registers. */
1404 if (reg_number == -1)
1405 error ("register name not specified for %q+D", decl);
1406 else if (reg_number < 0)
1407 error ("invalid register name for %q+D", decl);
1408 else if (mode == BLKmode)
1409 error ("data type of %q+D isn%'t suitable for a register",
1410 decl);
1411 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1412 error ("the register specified for %q+D cannot be accessed"
1413 " by the current target", decl);
1414 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1415 error ("the register specified for %q+D is not general enough"
1416 " to be used as a register variable", decl);
1417 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1418 error ("register specified for %q+D isn%'t suitable for data type",
1419 decl);
1420 /* Now handle properly declared static register variables. */
1421 else
1423 int nregs;
1425 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1427 DECL_INITIAL (decl) = 0;
1428 error ("global register variable has initial value");
1430 if (TREE_THIS_VOLATILE (decl))
1431 warning (OPT_Wvolatile_register_var,
1432 "optimization may eliminate reads and/or "
1433 "writes to register variables");
1435 /* If the user specified one of the eliminables registers here,
1436 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1437 confused with that register and be eliminated. This usage is
1438 somewhat suspect... */
1440 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1441 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1442 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1444 if (TREE_STATIC (decl))
1446 /* Make this register global, so not usable for anything
1447 else. */
1448 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1449 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1450 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1451 #endif
1452 nregs = hard_regno_nregs (reg_number, mode);
1453 while (nregs > 0)
1454 globalize_reg (decl, reg_number + --nregs);
1457 /* As a register variable, it has no section. */
1458 return;
1460 /* Avoid internal errors from invalid register
1461 specifications. */
1462 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1463 DECL_HARD_REGISTER (decl) = 0;
1464 /* Also avoid SSA inconsistencies by pretending this is an external
1465 decl now. */
1466 DECL_EXTERNAL (decl) = 1;
1467 return;
1469 /* Now handle ordinary static variables and functions (in memory).
1470 Also handle vars declared register invalidly. */
1471 else if (name[0] == '*')
1473 #ifdef REGISTER_PREFIX
1474 if (strlen (REGISTER_PREFIX) != 0)
1476 reg_number = decode_reg_name (name);
1477 if (reg_number >= 0 || reg_number == -3)
1478 error ("register name given for non-register variable %q+D", decl);
1480 #endif
1483 /* Specifying a section attribute on a variable forces it into a
1484 non-.bss section, and thus it cannot be common. */
1485 /* FIXME: In general this code should not be necessary because
1486 visibility pass is doing the same work. But notice_global_symbol
1487 is called early and it needs to make DECL_RTL to get the name.
1488 we take care of recomputing the DECL_RTL after visibility is changed. */
1489 if (VAR_P (decl)
1490 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1491 && DECL_SECTION_NAME (decl) != NULL
1492 && DECL_INITIAL (decl) == NULL_TREE
1493 && DECL_COMMON (decl))
1494 DECL_COMMON (decl) = 0;
1496 /* Variables can't be both common and weak. */
1497 if (VAR_P (decl) && DECL_WEAK (decl))
1498 DECL_COMMON (decl) = 0;
1500 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1501 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1502 else
1504 machine_mode address_mode = Pmode;
1505 if (TREE_TYPE (decl) != error_mark_node)
1507 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1508 address_mode = targetm.addr_space.address_mode (as);
1510 x = gen_rtx_SYMBOL_REF (address_mode, name);
1512 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1513 SET_SYMBOL_REF_DECL (x, decl);
1515 x = gen_rtx_MEM (DECL_MODE (decl), x);
1516 if (TREE_CODE (decl) != FUNCTION_DECL)
1517 set_mem_attributes (x, decl, 1);
1518 SET_DECL_RTL (decl, x);
1520 /* Optionally set flags or add text to the name to record information
1521 such as that it is a function name.
1522 If the name is changed, the macro ASM_OUTPUT_LABELREF
1523 will have to know how to strip this information. */
1524 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1527 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1528 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1529 rtl. */
1532 make_decl_rtl_for_debug (tree decl)
1534 unsigned int save_aliasing_flag;
1535 rtx rtl;
1537 if (DECL_RTL_SET_P (decl))
1538 return DECL_RTL (decl);
1540 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1541 call new_alias_set. If running with -fcompare-debug, sometimes
1542 we do not want to create alias sets that will throw the alias
1543 numbers off in the comparison dumps. So... clearing
1544 flag_strict_aliasing will keep new_alias_set() from creating a
1545 new set. */
1546 save_aliasing_flag = flag_strict_aliasing;
1547 flag_strict_aliasing = 0;
1549 rtl = DECL_RTL (decl);
1550 /* Reset DECL_RTL back, as various parts of the compiler expects
1551 DECL_RTL set meaning it is actually going to be output. */
1552 SET_DECL_RTL (decl, NULL);
1554 flag_strict_aliasing = save_aliasing_flag;
1555 return rtl;
1558 /* Output a string of literal assembler code
1559 for an `asm' keyword used between functions. */
1561 void
1562 assemble_asm (tree string)
1564 const char *p;
1565 app_enable ();
1567 if (TREE_CODE (string) == ADDR_EXPR)
1568 string = TREE_OPERAND (string, 0);
1570 p = TREE_STRING_POINTER (string);
1571 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1574 /* Write the address of the entity given by SYMBOL to SEC. */
1575 void
1576 assemble_addr_to_section (rtx symbol, section *sec)
1578 switch_to_section (sec);
1579 assemble_align (POINTER_SIZE);
1580 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1583 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1584 not) section for PRIORITY. */
1585 section *
1586 get_cdtor_priority_section (int priority, bool constructor_p)
1588 /* Buffer conservatively large enough for the full range of a 32-bit
1589 int plus the text below. */
1590 char buf[18];
1592 /* ??? This only works reliably with the GNU linker. */
1593 sprintf (buf, "%s.%.5u",
1594 constructor_p ? ".ctors" : ".dtors",
1595 /* Invert the numbering so the linker puts us in the proper
1596 order; constructors are run from right to left, and the
1597 linker sorts in increasing order. */
1598 MAX_INIT_PRIORITY - priority);
1599 return get_section (buf, SECTION_WRITE, NULL);
1602 void
1603 default_named_section_asm_out_destructor (rtx symbol, int priority)
1605 section *sec;
1607 if (priority != DEFAULT_INIT_PRIORITY)
1608 sec = get_cdtor_priority_section (priority,
1609 /*constructor_p=*/false);
1610 else
1611 sec = get_section (".dtors", SECTION_WRITE, NULL);
1613 assemble_addr_to_section (symbol, sec);
1616 #ifdef DTORS_SECTION_ASM_OP
1617 void
1618 default_dtor_section_asm_out_destructor (rtx symbol,
1619 int priority ATTRIBUTE_UNUSED)
1621 assemble_addr_to_section (symbol, dtors_section);
1623 #endif
1625 void
1626 default_named_section_asm_out_constructor (rtx symbol, int priority)
1628 section *sec;
1630 if (priority != DEFAULT_INIT_PRIORITY)
1631 sec = get_cdtor_priority_section (priority,
1632 /*constructor_p=*/true);
1633 else
1634 sec = get_section (".ctors", SECTION_WRITE, NULL);
1636 assemble_addr_to_section (symbol, sec);
1639 #ifdef CTORS_SECTION_ASM_OP
1640 void
1641 default_ctor_section_asm_out_constructor (rtx symbol,
1642 int priority ATTRIBUTE_UNUSED)
1644 assemble_addr_to_section (symbol, ctors_section);
1646 #endif
1648 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1649 a nonzero value if the constant pool should be output before the
1650 start of the function, or a zero value if the pool should output
1651 after the end of the function. The default is to put it before the
1652 start. */
1654 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1655 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1656 #endif
1658 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1659 to be output to assembler.
1660 Set first_global_object_name and weak_global_object_name as appropriate. */
1662 void
1663 notice_global_symbol (tree decl)
1665 const char **t = &first_global_object_name;
1667 if (first_global_object_name
1668 || !TREE_PUBLIC (decl)
1669 || DECL_EXTERNAL (decl)
1670 || !DECL_NAME (decl)
1671 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1672 || (TREE_CODE (decl) != FUNCTION_DECL
1673 && (!VAR_P (decl)
1674 || (DECL_COMMON (decl)
1675 && (DECL_INITIAL (decl) == 0
1676 || DECL_INITIAL (decl) == error_mark_node)))))
1677 return;
1679 /* We win when global object is found, but it is useful to know about weak
1680 symbol as well so we can produce nicer unique names. */
1681 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1682 t = &weak_global_object_name;
1684 if (!*t)
1686 tree id = DECL_ASSEMBLER_NAME (decl);
1687 ultimate_transparent_alias_target (&id);
1688 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1692 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1693 current function goes into the cold section, so that targets can use
1694 current_function_section during RTL expansion. DECL describes the
1695 function. */
1697 void
1698 decide_function_section (tree decl)
1700 first_function_block_is_cold = false;
1702 if (DECL_SECTION_NAME (decl))
1704 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1705 /* Calls to function_section rely on first_function_block_is_cold
1706 being accurate. */
1707 first_function_block_is_cold = (node
1708 && node->frequency
1709 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1712 in_cold_section_p = first_function_block_is_cold;
1715 /* Get the function's name, as described by its RTL. This may be
1716 different from the DECL_NAME name used in the source file. */
1717 const char *
1718 get_fnname_from_decl (tree decl)
1720 rtx x = DECL_RTL (decl);
1721 gcc_assert (MEM_P (x));
1722 x = XEXP (x, 0);
1723 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1724 return XSTR (x, 0);
1727 /* Output assembler code for the constant pool of a function and associated
1728 with defining the name of the function. DECL describes the function.
1729 NAME is the function's name. For the constant pool, we use the current
1730 constant pool data. */
1732 void
1733 assemble_start_function (tree decl, const char *fnname)
1735 int align;
1736 char tmp_label[100];
1737 bool hot_label_written = false;
1739 if (crtl->has_bb_partition)
1741 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1742 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1743 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1744 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1745 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1746 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1747 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1748 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1749 const_labelno++;
1750 cold_function_name = NULL_TREE;
1752 else
1754 crtl->subsections.hot_section_label = NULL;
1755 crtl->subsections.cold_section_label = NULL;
1756 crtl->subsections.hot_section_end_label = NULL;
1757 crtl->subsections.cold_section_end_label = NULL;
1760 /* The following code does not need preprocessing in the assembler. */
1762 app_disable ();
1764 if (CONSTANT_POOL_BEFORE_FUNCTION)
1765 output_constant_pool (fnname, decl);
1767 align = symtab_node::get (decl)->definition_alignment ();
1769 /* Make sure the not and cold text (code) sections are properly
1770 aligned. This is necessary here in the case where the function
1771 has both hot and cold sections, because we don't want to re-set
1772 the alignment when the section switch happens mid-function. */
1774 if (crtl->has_bb_partition)
1776 first_function_block_is_cold = false;
1778 switch_to_section (unlikely_text_section ());
1779 assemble_align (align);
1780 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1782 /* When the function starts with a cold section, we need to explicitly
1783 align the hot section and write out the hot section label.
1784 But if the current function is a thunk, we do not have a CFG. */
1785 if (!cfun->is_thunk
1786 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1788 switch_to_section (text_section);
1789 assemble_align (align);
1790 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1791 hot_label_written = true;
1792 first_function_block_is_cold = true;
1794 in_cold_section_p = first_function_block_is_cold;
1798 /* Switch to the correct text section for the start of the function. */
1800 switch_to_section (function_section (decl));
1801 if (crtl->has_bb_partition && !hot_label_written)
1802 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1804 /* Tell assembler to move to target machine's alignment for functions. */
1805 align = floor_log2 (align / BITS_PER_UNIT);
1806 if (align > 0)
1808 ASM_OUTPUT_ALIGN (asm_out_file, align);
1811 /* Handle a user-specified function alignment.
1812 Note that we still need to align to DECL_ALIGN, as above,
1813 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1814 if (! DECL_USER_ALIGN (decl)
1815 && align_functions.levels[0].log > align
1816 && optimize_function_for_speed_p (cfun))
1818 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1819 int align_log = align_functions.levels[0].log;
1820 #endif
1821 int max_skip = align_functions.levels[0].maxskip;
1822 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1823 && max_skip >= crtl->max_insn_address)
1824 max_skip = crtl->max_insn_address - 1;
1826 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1827 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1828 if (max_skip == align_functions.levels[0].maxskip)
1829 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1830 align_functions.levels[1].log,
1831 align_functions.levels[1].maxskip);
1832 #else
1833 ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
1834 #endif
1837 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1838 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1839 #endif
1841 if (!DECL_IGNORED_P (decl))
1842 (*debug_hooks->begin_function) (decl);
1844 /* Make function name accessible from other files, if appropriate. */
1846 if (TREE_PUBLIC (decl))
1848 notice_global_symbol (decl);
1850 globalize_decl (decl);
1852 maybe_assemble_visibility (decl);
1855 if (DECL_PRESERVE_P (decl))
1856 targetm.asm_out.mark_decl_preserved (fnname);
1858 unsigned HOST_WIDE_INT patch_area_size = function_entry_patch_area_size;
1859 unsigned HOST_WIDE_INT patch_area_entry = function_entry_patch_area_start;
1861 tree patchable_function_entry_attr
1862 = lookup_attribute ("patchable_function_entry", DECL_ATTRIBUTES (decl));
1863 if (patchable_function_entry_attr)
1865 tree pp_val = TREE_VALUE (patchable_function_entry_attr);
1866 tree patchable_function_entry_value1 = TREE_VALUE (pp_val);
1868 patch_area_size = tree_to_uhwi (patchable_function_entry_value1);
1869 patch_area_entry = 0;
1870 if (TREE_CHAIN (pp_val) != NULL_TREE)
1872 tree patchable_function_entry_value2
1873 = TREE_VALUE (TREE_CHAIN (pp_val));
1874 patch_area_entry = tree_to_uhwi (patchable_function_entry_value2);
1878 if (patch_area_entry > patch_area_size)
1880 if (patch_area_size > 0)
1881 warning (OPT_Wattributes, "patchable function entry > size");
1882 patch_area_entry = 0;
1885 /* Emit the patching area before the entry label, if any. */
1886 if (patch_area_entry > 0)
1887 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1888 patch_area_entry, true);
1890 /* Do any machine/system dependent processing of the function name. */
1891 #ifdef ASM_DECLARE_FUNCTION_NAME
1892 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1893 #else
1894 /* Standard thing is just output label for the function. */
1895 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1896 #endif /* ASM_DECLARE_FUNCTION_NAME */
1898 /* And the area after the label. Record it if we haven't done so yet. */
1899 if (patch_area_size > patch_area_entry)
1900 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1901 patch_area_size
1902 - patch_area_entry,
1903 patch_area_entry == 0);
1905 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1906 saw_no_split_stack = true;
1909 /* Output assembler code associated with defining the size of the
1910 function. DECL describes the function. NAME is the function's name. */
1912 void
1913 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1915 #ifdef ASM_DECLARE_FUNCTION_SIZE
1916 /* We could have switched section in the middle of the function. */
1917 if (crtl->has_bb_partition)
1918 switch_to_section (function_section (decl));
1919 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1920 #endif
1921 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1923 output_constant_pool (fnname, decl);
1924 switch_to_section (function_section (decl)); /* need to switch back */
1926 /* Output labels for end of hot/cold text sections (to be used by
1927 debug info.) */
1928 if (crtl->has_bb_partition)
1930 section *save_text_section;
1932 save_text_section = in_section;
1933 switch_to_section (unlikely_text_section ());
1934 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
1935 if (cold_function_name != NULL_TREE)
1936 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
1937 IDENTIFIER_POINTER (cold_function_name),
1938 decl);
1939 #endif
1940 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1941 if (first_function_block_is_cold)
1942 switch_to_section (text_section);
1943 else
1944 switch_to_section (function_section (decl));
1945 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1946 switch_to_section (save_text_section);
1950 /* Assemble code to leave SIZE bytes of zeros. */
1952 void
1953 assemble_zeros (unsigned HOST_WIDE_INT size)
1955 /* Do no output if -fsyntax-only. */
1956 if (flag_syntax_only)
1957 return;
1959 #ifdef ASM_NO_SKIP_IN_TEXT
1960 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1961 so we must output 0s explicitly in the text section. */
1962 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1964 unsigned HOST_WIDE_INT i;
1965 for (i = 0; i < size; i++)
1966 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1968 else
1969 #endif
1970 if (size > 0)
1971 ASM_OUTPUT_SKIP (asm_out_file, size);
1974 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1976 void
1977 assemble_align (unsigned int align)
1979 if (align > BITS_PER_UNIT)
1981 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1985 /* Assemble a string constant with the specified C string as contents. */
1987 void
1988 assemble_string (const char *p, int size)
1990 int pos = 0;
1991 int maximum = 2000;
1993 /* If the string is very long, split it up. */
1995 while (pos < size)
1997 int thissize = size - pos;
1998 if (thissize > maximum)
1999 thissize = maximum;
2001 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
2003 pos += thissize;
2004 p += thissize;
2009 /* A noswitch_section_callback for lcomm_section. */
2011 static bool
2012 emit_local (tree decl ATTRIBUTE_UNUSED,
2013 const char *name ATTRIBUTE_UNUSED,
2014 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2015 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2017 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2018 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2019 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2020 size, align);
2021 return true;
2022 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2023 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2024 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2025 return true;
2026 #else
2027 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2028 return false;
2029 #endif
2032 /* A noswitch_section_callback for bss_noswitch_section. */
2034 #if defined ASM_OUTPUT_ALIGNED_BSS
2035 static bool
2036 emit_bss (tree decl ATTRIBUTE_UNUSED,
2037 const char *name ATTRIBUTE_UNUSED,
2038 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2039 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2041 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2042 get_variable_align (decl));
2043 return true;
2045 #endif
2047 /* A noswitch_section_callback for comm_section. */
2049 static bool
2050 emit_common (tree decl ATTRIBUTE_UNUSED,
2051 const char *name ATTRIBUTE_UNUSED,
2052 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2053 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2055 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2056 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2057 size, get_variable_align (decl));
2058 return true;
2059 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2060 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2061 get_variable_align (decl));
2062 return true;
2063 #else
2064 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2065 return false;
2066 #endif
2069 /* A noswitch_section_callback for tls_comm_section. */
2071 static bool
2072 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2073 const char *name ATTRIBUTE_UNUSED,
2074 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2075 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2077 #ifdef ASM_OUTPUT_TLS_COMMON
2078 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2079 return true;
2080 #else
2081 sorry ("thread-local COMMON data not implemented");
2082 return true;
2083 #endif
2086 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2087 NAME is the name of DECL's SYMBOL_REF. */
2089 static void
2090 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2091 unsigned int align)
2093 unsigned HOST_WIDE_INT size, rounded;
2095 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2096 rounded = size;
2098 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2099 size += asan_red_zone_size (size);
2101 /* Don't allocate zero bytes of common,
2102 since that means "undefined external" in the linker. */
2103 if (size == 0)
2104 rounded = 1;
2106 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2107 so that each uninitialized object starts on such a boundary. */
2108 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2109 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2110 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2112 if (!sect->noswitch.callback (decl, name, size, rounded)
2113 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2114 error ("requested alignment for %q+D is greater than "
2115 "implemented alignment of %wu", decl, rounded);
2118 /* A subroutine of assemble_variable. Output the label and contents of
2119 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2120 is as for assemble_variable. */
2122 static void
2123 assemble_variable_contents (tree decl, const char *name,
2124 bool dont_output_data, bool merge_strings)
2126 /* Do any machine/system dependent processing of the object. */
2127 #ifdef ASM_DECLARE_OBJECT_NAME
2128 last_assemble_variable_decl = decl;
2129 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2130 #else
2131 /* Standard thing is just output label for the object. */
2132 ASM_OUTPUT_LABEL (asm_out_file, name);
2133 #endif /* ASM_DECLARE_OBJECT_NAME */
2135 if (!dont_output_data)
2137 /* Caller is supposed to use varpool_get_constructor when it wants
2138 to output the body. */
2139 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2140 if (DECL_INITIAL (decl)
2141 && DECL_INITIAL (decl) != error_mark_node
2142 && !initializer_zerop (DECL_INITIAL (decl)))
2143 /* Output the actual data. */
2144 output_constant (DECL_INITIAL (decl),
2145 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2146 get_variable_align (decl),
2147 false, merge_strings);
2148 else
2149 /* Leave space for it. */
2150 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2151 targetm.asm_out.decl_end ();
2155 /* Write out assembly for the variable DECL, which is not defined in
2156 the current translation unit. */
2157 void
2158 assemble_undefined_decl (tree decl)
2160 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2161 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2164 /* Assemble everything that is needed for a variable or function declaration.
2165 Not used for automatic variables, and not used for function definitions.
2166 Should not be called for variables of incomplete structure type.
2168 TOP_LEVEL is nonzero if this variable has file scope.
2169 AT_END is nonzero if this is the special handling, at end of compilation,
2170 to define things that have had only tentative definitions.
2171 DONT_OUTPUT_DATA if nonzero means don't actually output the
2172 initial value (that will be done by the caller). */
2174 void
2175 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2176 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2178 const char *name;
2179 rtx decl_rtl, symbol;
2180 section *sect;
2181 unsigned int align;
2182 bool asan_protected = false;
2184 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2185 gcc_assert (VAR_P (decl));
2187 /* Emulated TLS had better not get this far. */
2188 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2190 last_assemble_variable_decl = 0;
2192 /* Normally no need to say anything here for external references,
2193 since assemble_external is called by the language-specific code
2194 when a declaration is first seen. */
2196 if (DECL_EXTERNAL (decl))
2197 return;
2199 /* Do nothing for global register variables. */
2200 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2202 TREE_ASM_WRITTEN (decl) = 1;
2203 return;
2206 /* If type was incomplete when the variable was declared,
2207 see if it is complete now. */
2209 if (DECL_SIZE (decl) == 0)
2210 layout_decl (decl, 0);
2212 /* Still incomplete => don't allocate it; treat the tentative defn
2213 (which is what it must have been) as an `extern' reference. */
2215 if (!dont_output_data && DECL_SIZE (decl) == 0)
2217 error ("storage size of %q+D isn%'t known", decl);
2218 TREE_ASM_WRITTEN (decl) = 1;
2219 return;
2222 /* The first declaration of a variable that comes through this function
2223 decides whether it is global (in C, has external linkage)
2224 or local (in C, has internal linkage). So do nothing more
2225 if this function has already run. */
2227 if (TREE_ASM_WRITTEN (decl))
2228 return;
2230 /* Make sure targetm.encode_section_info is invoked before we set
2231 ASM_WRITTEN. */
2232 decl_rtl = DECL_RTL (decl);
2234 TREE_ASM_WRITTEN (decl) = 1;
2236 /* Do no output if -fsyntax-only. */
2237 if (flag_syntax_only)
2238 return;
2240 if (! dont_output_data
2241 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2243 error ("size of variable %q+D is too large", decl);
2244 return;
2247 gcc_assert (MEM_P (decl_rtl));
2248 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2249 symbol = XEXP (decl_rtl, 0);
2251 /* If this symbol belongs to the tree constant pool, output the constant
2252 if it hasn't already been written. */
2253 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2255 tree decl = SYMBOL_REF_DECL (symbol);
2256 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2257 output_constant_def_contents (symbol);
2258 return;
2261 app_disable ();
2263 name = XSTR (symbol, 0);
2264 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2265 notice_global_symbol (decl);
2267 /* Compute the alignment of this data. */
2269 align_variable (decl, dont_output_data);
2271 if ((flag_sanitize & SANITIZE_ADDRESS)
2272 && asan_protect_global (decl))
2274 asan_protected = true;
2275 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2276 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2279 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2281 align = get_variable_align (decl);
2283 if (TREE_PUBLIC (decl))
2284 maybe_assemble_visibility (decl);
2286 if (DECL_PRESERVE_P (decl))
2287 targetm.asm_out.mark_decl_preserved (name);
2289 /* First make the assembler name(s) global if appropriate. */
2290 sect = get_variable_section (decl, false);
2291 if (TREE_PUBLIC (decl)
2292 && (sect->common.flags & SECTION_COMMON) == 0)
2293 globalize_decl (decl);
2295 /* Output any data that we will need to use the address of. */
2296 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2297 output_addressed_constants (DECL_INITIAL (decl));
2299 /* dbxout.c needs to know this. */
2300 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2301 DECL_IN_TEXT_SECTION (decl) = 1;
2303 /* If the decl is part of an object_block, make sure that the decl
2304 has been positioned within its block, but do not write out its
2305 definition yet. output_object_blocks will do that later. */
2306 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2308 gcc_assert (!dont_output_data);
2309 place_block_symbol (symbol);
2311 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2312 assemble_noswitch_variable (decl, name, sect, align);
2313 else
2315 /* Special-case handling of vtv comdat sections. */
2316 if (sect->named.name
2317 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2318 handle_vtv_comdat_section (sect, decl);
2319 else
2320 switch_to_section (sect);
2321 if (align > BITS_PER_UNIT)
2322 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2323 assemble_variable_contents (decl, name, dont_output_data,
2324 (sect->common.flags & SECTION_MERGE)
2325 && (sect->common.flags & SECTION_STRINGS));
2326 if (asan_protected)
2328 unsigned HOST_WIDE_INT int size
2329 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2330 assemble_zeros (asan_red_zone_size (size));
2336 /* Given a function declaration (FN_DECL), this function assembles the
2337 function into the .preinit_array section. */
2339 void
2340 assemble_vtv_preinit_initializer (tree fn_decl)
2342 section *sect;
2343 unsigned flags = SECTION_WRITE;
2344 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2346 flags |= SECTION_NOTYPE;
2347 sect = get_section (".preinit_array", flags, fn_decl);
2348 switch_to_section (sect);
2349 assemble_addr_to_section (symbol, sect);
2352 /* Return 1 if type TYPE contains any pointers. */
2354 static int
2355 contains_pointers_p (tree type)
2357 switch (TREE_CODE (type))
2359 case POINTER_TYPE:
2360 case REFERENCE_TYPE:
2361 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2362 so I'll play safe and return 1. */
2363 case OFFSET_TYPE:
2364 return 1;
2366 case RECORD_TYPE:
2367 case UNION_TYPE:
2368 case QUAL_UNION_TYPE:
2370 tree fields;
2371 /* For a type that has fields, see if the fields have pointers. */
2372 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2373 if (TREE_CODE (fields) == FIELD_DECL
2374 && contains_pointers_p (TREE_TYPE (fields)))
2375 return 1;
2376 return 0;
2379 case ARRAY_TYPE:
2380 /* An array type contains pointers if its element type does. */
2381 return contains_pointers_p (TREE_TYPE (type));
2383 default:
2384 return 0;
2388 /* We delay assemble_external processing until
2389 the compilation unit is finalized. This is the best we can do for
2390 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2391 it all the way to final. See PR 17982 for further discussion. */
2392 static GTY(()) tree pending_assemble_externals;
2394 #ifdef ASM_OUTPUT_EXTERNAL
2395 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2396 As a result, assemble_external can be called after the list of externals
2397 is processed and the pointer set destroyed. */
2398 static bool pending_assemble_externals_processed;
2400 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2401 TREE_LIST in assemble_external. */
2402 static hash_set<tree> *pending_assemble_externals_set;
2404 /* True if DECL is a function decl for which no out-of-line copy exists.
2405 It is assumed that DECL's assembler name has been set. */
2407 static bool
2408 incorporeal_function_p (tree decl)
2410 if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl))
2412 const char *name;
2414 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2415 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2416 return true;
2418 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2419 /* Atomic or sync builtins which have survived this far will be
2420 resolved externally and therefore are not incorporeal. */
2421 if (strncmp (name, "__builtin_", 10) == 0)
2422 return true;
2424 return false;
2427 /* Actually do the tests to determine if this is necessary, and invoke
2428 ASM_OUTPUT_EXTERNAL. */
2429 static void
2430 assemble_external_real (tree decl)
2432 rtx rtl = DECL_RTL (decl);
2434 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2435 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2436 && !incorporeal_function_p (decl))
2438 /* Some systems do require some output. */
2439 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2440 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2443 #endif
2445 void
2446 process_pending_assemble_externals (void)
2448 #ifdef ASM_OUTPUT_EXTERNAL
2449 tree list;
2450 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2451 assemble_external_real (TREE_VALUE (list));
2453 pending_assemble_externals = 0;
2454 pending_assemble_externals_processed = true;
2455 delete pending_assemble_externals_set;
2456 #endif
2459 /* This TREE_LIST contains any weak symbol declarations waiting
2460 to be emitted. */
2461 static GTY(()) tree weak_decls;
2463 /* Output something to declare an external symbol to the assembler,
2464 and qualifiers such as weakness. (Most assemblers don't need
2465 extern declaration, so we normally output nothing.) Do nothing if
2466 DECL is not external. */
2468 void
2469 assemble_external (tree decl ATTRIBUTE_UNUSED)
2471 /* Make sure that the ASM_OUT_FILE is open.
2472 If it's not, we should not be calling this function. */
2473 gcc_assert (asm_out_file);
2475 /* In a perfect world, the following condition would be true.
2476 Sadly, the Go front end emit assembly *from the front end*,
2477 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2478 #if 0
2479 /* This function should only be called if we are expanding, or have
2480 expanded, to RTL.
2481 Ideally, only final.c would be calling this function, but it is
2482 not clear whether that would break things somehow. See PR 17982
2483 for further discussion. */
2484 gcc_assert (state == EXPANSION
2485 || state == FINISHED);
2486 #endif
2488 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2489 return;
2491 /* We want to output annotation for weak and external symbols at
2492 very last to check if they are references or not. */
2494 if (TARGET_SUPPORTS_WEAK
2495 && DECL_WEAK (decl)
2496 /* TREE_STATIC is a weird and abused creature which is not
2497 generally the right test for whether an entity has been
2498 locally emitted, inlined or otherwise not-really-extern, but
2499 for declarations that can be weak, it happens to be
2500 match. */
2501 && !TREE_STATIC (decl)
2502 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2503 && value_member (decl, weak_decls) == NULL_TREE)
2504 weak_decls = tree_cons (NULL, decl, weak_decls);
2506 #ifdef ASM_OUTPUT_EXTERNAL
2507 if (pending_assemble_externals_processed)
2509 assemble_external_real (decl);
2510 return;
2513 if (! pending_assemble_externals_set->add (decl))
2514 pending_assemble_externals = tree_cons (NULL, decl,
2515 pending_assemble_externals);
2516 #endif
2519 /* Similar, for calling a library function FUN. */
2521 void
2522 assemble_external_libcall (rtx fun)
2524 /* Declare library function name external when first used, if nec. */
2525 if (! SYMBOL_REF_USED (fun))
2527 SYMBOL_REF_USED (fun) = 1;
2528 targetm.asm_out.external_libcall (fun);
2532 /* Assemble a label named NAME. */
2534 void
2535 assemble_label (FILE *file, const char *name)
2537 ASM_OUTPUT_LABEL (file, name);
2540 /* Set the symbol_referenced flag for ID. */
2541 void
2542 mark_referenced (tree id)
2544 TREE_SYMBOL_REFERENCED (id) = 1;
2547 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2548 void
2549 mark_decl_referenced (tree decl)
2551 if (TREE_CODE (decl) == FUNCTION_DECL)
2553 /* Extern inline functions don't become needed when referenced.
2554 If we know a method will be emitted in other TU and no new
2555 functions can be marked reachable, just use the external
2556 definition. */
2557 struct cgraph_node *node = cgraph_node::get_create (decl);
2558 if (!DECL_EXTERNAL (decl)
2559 && !node->definition)
2560 node->mark_force_output ();
2562 else if (VAR_P (decl))
2564 varpool_node *node = varpool_node::get_create (decl);
2565 /* C++ frontend use mark_decl_references to force COMDAT variables
2566 to be output that might appear dead otherwise. */
2567 node->force_output = true;
2569 /* else do nothing - we can get various sorts of CST nodes here,
2570 which do not need to be marked. */
2574 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2575 starts with a *, the rest of NAME is output verbatim. Otherwise
2576 NAME is transformed in a target-specific way (usually by the
2577 addition of an underscore). */
2579 void
2580 assemble_name_raw (FILE *file, const char *name)
2582 if (name[0] == '*')
2583 fputs (&name[1], file);
2584 else
2585 ASM_OUTPUT_LABELREF (file, name);
2588 /* Like assemble_name_raw, but should be used when NAME might refer to
2589 an entity that is also represented as a tree (like a function or
2590 variable). If NAME does refer to such an entity, that entity will
2591 be marked as referenced. */
2593 void
2594 assemble_name (FILE *file, const char *name)
2596 const char *real_name;
2597 tree id;
2599 real_name = targetm.strip_name_encoding (name);
2601 id = maybe_get_identifier (real_name);
2602 if (id)
2604 tree id_orig = id;
2606 mark_referenced (id);
2607 ultimate_transparent_alias_target (&id);
2608 if (id != id_orig)
2609 name = IDENTIFIER_POINTER (id);
2610 gcc_assert (! TREE_CHAIN (id));
2613 assemble_name_raw (file, name);
2616 /* Allocate SIZE bytes writable static space with a gensym name
2617 and return an RTX to refer to its address. */
2620 assemble_static_space (unsigned HOST_WIDE_INT size)
2622 char name[17];
2623 const char *namestring;
2624 rtx x;
2626 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2627 ++const_labelno;
2628 namestring = ggc_strdup (name);
2630 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2631 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2633 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2634 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2635 BIGGEST_ALIGNMENT);
2636 #else
2637 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2638 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2639 #else
2641 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2642 so that each uninitialized object starts on such a boundary. */
2643 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2644 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2645 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2646 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2647 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2648 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2650 #endif
2651 #endif
2652 return x;
2655 /* Assemble the static constant template for function entry trampolines.
2656 This is done at most once per compilation.
2657 Returns an RTX for the address of the template. */
2659 static GTY(()) rtx initial_trampoline;
2662 assemble_trampoline_template (void)
2664 char label[256];
2665 const char *name;
2666 int align;
2667 rtx symbol;
2669 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2671 if (initial_trampoline)
2672 return initial_trampoline;
2674 /* By default, put trampoline templates in read-only data section. */
2676 #ifdef TRAMPOLINE_SECTION
2677 switch_to_section (TRAMPOLINE_SECTION);
2678 #else
2679 switch_to_section (readonly_data_section);
2680 #endif
2682 /* Write the assembler code to define one. */
2683 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2684 if (align > 0)
2685 ASM_OUTPUT_ALIGN (asm_out_file, align);
2687 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2688 targetm.asm_out.trampoline_template (asm_out_file);
2690 /* Record the rtl to refer to it. */
2691 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2692 name = ggc_strdup (label);
2693 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2694 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2696 initial_trampoline = gen_const_mem (BLKmode, symbol);
2697 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2698 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2700 return initial_trampoline;
2703 /* A and B are either alignments or offsets. Return the minimum alignment
2704 that may be assumed after adding the two together. */
2706 static inline unsigned
2707 min_align (unsigned int a, unsigned int b)
2709 return least_bit_hwi (a | b);
2712 /* Return the assembler directive for creating a given kind of integer
2713 object. SIZE is the number of bytes in the object and ALIGNED_P
2714 indicates whether it is known to be aligned. Return NULL if the
2715 assembly dialect has no such directive.
2717 The returned string should be printed at the start of a new line and
2718 be followed immediately by the object's initial value. */
2720 const char *
2721 integer_asm_op (int size, int aligned_p)
2723 struct asm_int_op *ops;
2725 if (aligned_p)
2726 ops = &targetm.asm_out.aligned_op;
2727 else
2728 ops = &targetm.asm_out.unaligned_op;
2730 switch (size)
2732 case 1:
2733 return targetm.asm_out.byte_op;
2734 case 2:
2735 return ops->hi;
2736 case 3:
2737 return ops->psi;
2738 case 4:
2739 return ops->si;
2740 case 5:
2741 case 6:
2742 case 7:
2743 return ops->pdi;
2744 case 8:
2745 return ops->di;
2746 case 9:
2747 case 10:
2748 case 11:
2749 case 12:
2750 case 13:
2751 case 14:
2752 case 15:
2753 return ops->pti;
2754 case 16:
2755 return ops->ti;
2756 default:
2757 return NULL;
2761 /* Use directive OP to assemble an integer object X. Print OP at the
2762 start of the line, followed immediately by the value of X. */
2764 void
2765 assemble_integer_with_op (const char *op, rtx x)
2767 fputs (op, asm_out_file);
2768 output_addr_const (asm_out_file, x);
2769 fputc ('\n', asm_out_file);
2772 /* The default implementation of the asm_out.integer target hook. */
2774 bool
2775 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2776 unsigned int size ATTRIBUTE_UNUSED,
2777 int aligned_p ATTRIBUTE_UNUSED)
2779 const char *op = integer_asm_op (size, aligned_p);
2780 /* Avoid GAS bugs for large values. Specifically negative values whose
2781 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2782 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2783 return false;
2784 return op && (assemble_integer_with_op (op, x), true);
2787 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2788 the alignment of the integer in bits. Return 1 if we were able to output
2789 the constant, otherwise 0. We must be able to output the constant,
2790 if FORCE is nonzero. */
2792 bool
2793 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2795 int aligned_p;
2797 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2799 /* See if the target hook can handle this kind of object. */
2800 if (targetm.asm_out.integer (x, size, aligned_p))
2801 return true;
2803 /* If the object is a multi-byte one, try splitting it up. Split
2804 it into words it if is multi-word, otherwise split it into bytes. */
2805 if (size > 1)
2807 machine_mode omode, imode;
2808 unsigned int subalign;
2809 unsigned int subsize, i;
2810 enum mode_class mclass;
2812 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2813 subalign = MIN (align, subsize * BITS_PER_UNIT);
2814 if (GET_CODE (x) == CONST_FIXED)
2815 mclass = GET_MODE_CLASS (GET_MODE (x));
2816 else
2817 mclass = MODE_INT;
2819 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2820 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2822 for (i = 0; i < size; i += subsize)
2824 rtx partial = simplify_subreg (omode, x, imode, i);
2825 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2826 break;
2828 if (i == size)
2829 return true;
2831 /* If we've printed some of it, but not all of it, there's no going
2832 back now. */
2833 gcc_assert (!i);
2836 gcc_assert (!force);
2838 return false;
2841 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
2842 is the alignment of the constant in bits. If REVERSE is true, D is output
2843 in reverse storage order. */
2845 void
2846 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2847 bool reverse)
2849 long data[4] = {0, 0, 0, 0};
2850 int bitsize, nelts, nunits, units_per;
2851 rtx elt;
2853 /* This is hairy. We have a quantity of known size. real_to_target
2854 will put it into an array of *host* longs, 32 bits per element
2855 (even if long is more than 32 bits). We need to determine the
2856 number of array elements that are occupied (nelts) and the number
2857 of *target* min-addressable units that will be occupied in the
2858 object file (nunits). We cannot assume that 32 divides the
2859 mode's bitsize (size * BITS_PER_UNIT) evenly.
2861 size * BITS_PER_UNIT is used here to make sure that padding bits
2862 (which might appear at either end of the value; real_to_target
2863 will include the padding bits in its output array) are included. */
2865 nunits = GET_MODE_SIZE (mode);
2866 bitsize = nunits * BITS_PER_UNIT;
2867 nelts = CEIL (bitsize, 32);
2868 units_per = 32 / BITS_PER_UNIT;
2870 real_to_target (data, &d, mode);
2872 /* Put out the first word with the specified alignment. */
2873 if (reverse)
2874 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
2875 else
2876 elt = GEN_INT (data[0]);
2877 assemble_integer (elt, MIN (nunits, units_per), align, 1);
2878 nunits -= units_per;
2880 /* Subsequent words need only 32-bit alignment. */
2881 align = min_align (align, 32);
2883 for (int i = 1; i < nelts; i++)
2885 if (reverse)
2886 elt = flip_storage_order (SImode,
2887 gen_int_mode (data[nelts - 1 - i], SImode));
2888 else
2889 elt = GEN_INT (data[i]);
2890 assemble_integer (elt, MIN (nunits, units_per), align, 1);
2891 nunits -= units_per;
2895 /* Given an expression EXP with a constant value,
2896 reduce it to the sum of an assembler symbol and an integer.
2897 Store them both in the structure *VALUE.
2898 EXP must be reducible. */
2900 struct addr_const {
2901 rtx base;
2902 poly_int64 offset;
2905 static void
2906 decode_addr_const (tree exp, struct addr_const *value)
2908 tree target = TREE_OPERAND (exp, 0);
2909 poly_int64 offset = 0;
2910 rtx x;
2912 while (1)
2914 poly_int64 bytepos;
2915 if (TREE_CODE (target) == COMPONENT_REF
2916 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
2917 &bytepos))
2919 offset += bytepos;
2920 target = TREE_OPERAND (target, 0);
2922 else if (TREE_CODE (target) == ARRAY_REF
2923 || TREE_CODE (target) == ARRAY_RANGE_REF)
2925 /* Truncate big offset. */
2926 offset
2927 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2928 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
2929 target = TREE_OPERAND (target, 0);
2931 else if (TREE_CODE (target) == MEM_REF
2932 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2934 offset += mem_ref_offset (target).force_shwi ();
2935 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2937 else if (TREE_CODE (target) == INDIRECT_REF
2938 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2939 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2940 == ADDR_EXPR)
2941 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2942 else
2943 break;
2946 switch (TREE_CODE (target))
2948 case VAR_DECL:
2949 case FUNCTION_DECL:
2950 x = DECL_RTL (target);
2951 break;
2953 case LABEL_DECL:
2954 x = gen_rtx_MEM (FUNCTION_MODE,
2955 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2956 break;
2958 case REAL_CST:
2959 case FIXED_CST:
2960 case STRING_CST:
2961 case COMPLEX_CST:
2962 case CONSTRUCTOR:
2963 case INTEGER_CST:
2964 x = lookup_constant_def (target);
2965 /* Should have been added by output_addressed_constants. */
2966 gcc_assert (x);
2967 break;
2969 case INDIRECT_REF:
2970 /* This deals with absolute addresses. */
2971 offset += tree_to_shwi (TREE_OPERAND (target, 0));
2972 x = gen_rtx_MEM (QImode,
2973 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
2974 break;
2976 case COMPOUND_LITERAL_EXPR:
2977 gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
2978 x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
2979 break;
2981 default:
2982 gcc_unreachable ();
2985 gcc_assert (MEM_P (x));
2986 x = XEXP (x, 0);
2988 value->base = x;
2989 value->offset = offset;
2992 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2994 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2996 /* Constant pool accessor function. */
2998 hash_table<tree_descriptor_hasher> *
2999 constant_pool_htab (void)
3001 return const_desc_htab;
3004 /* Compute a hash code for a constant expression. */
3006 hashval_t
3007 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3009 return ptr->hash;
3012 static hashval_t
3013 const_hash_1 (const tree exp)
3015 const char *p;
3016 hashval_t hi;
3017 int len, i;
3018 enum tree_code code = TREE_CODE (exp);
3020 /* Either set P and LEN to the address and len of something to hash and
3021 exit the switch or return a value. */
3023 switch (code)
3025 case INTEGER_CST:
3026 p = (char *) &TREE_INT_CST_ELT (exp, 0);
3027 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3028 break;
3030 case REAL_CST:
3031 return real_hash (TREE_REAL_CST_PTR (exp));
3033 case FIXED_CST:
3034 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3036 case STRING_CST:
3037 p = TREE_STRING_POINTER (exp);
3038 len = TREE_STRING_LENGTH (exp);
3039 break;
3041 case COMPLEX_CST:
3042 return (const_hash_1 (TREE_REALPART (exp)) * 5
3043 + const_hash_1 (TREE_IMAGPART (exp)));
3045 case VECTOR_CST:
3047 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3048 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3049 unsigned int count = vector_cst_encoded_nelts (exp);
3050 for (unsigned int i = 0; i < count; ++i)
3051 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3052 return hi;
3055 case CONSTRUCTOR:
3057 unsigned HOST_WIDE_INT idx;
3058 tree value;
3060 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3062 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3063 if (value)
3064 hi = hi * 603 + const_hash_1 (value);
3066 return hi;
3069 case ADDR_EXPR:
3070 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3071 return const_hash_1 (TREE_OPERAND (exp, 0));
3073 /* Fallthru. */
3074 case FDESC_EXPR:
3076 struct addr_const value;
3078 decode_addr_const (exp, &value);
3079 switch (GET_CODE (value.base))
3081 case SYMBOL_REF:
3082 /* Don't hash the address of the SYMBOL_REF;
3083 only use the offset and the symbol name. */
3084 hi = value.offset.coeffs[0];
3085 p = XSTR (value.base, 0);
3086 for (i = 0; p[i] != 0; i++)
3087 hi = ((hi * 613) + (unsigned) (p[i]));
3088 break;
3090 case LABEL_REF:
3091 hi = (value.offset.coeffs[0]
3092 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3093 break;
3095 default:
3096 gcc_unreachable ();
3099 return hi;
3101 case PLUS_EXPR:
3102 case POINTER_PLUS_EXPR:
3103 case MINUS_EXPR:
3104 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3105 + const_hash_1 (TREE_OPERAND (exp, 1)));
3107 CASE_CONVERT:
3108 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3110 default:
3111 /* A language specific constant. Just hash the code. */
3112 return code;
3115 /* Compute hashing function. */
3116 hi = len;
3117 for (i = 0; i < len; i++)
3118 hi = ((hi * 613) + (unsigned) (p[i]));
3120 return hi;
3123 /* Wrapper of compare_constant, for the htab interface. */
3124 bool
3125 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3126 constant_descriptor_tree *c2)
3128 if (c1->hash != c2->hash)
3129 return 0;
3130 return compare_constant (c1->value, c2->value);
3133 /* Compare t1 and t2, and return 1 only if they are known to result in
3134 the same bit pattern on output. */
3136 static int
3137 compare_constant (const tree t1, const tree t2)
3139 enum tree_code typecode;
3141 if (t1 == NULL_TREE)
3142 return t2 == NULL_TREE;
3143 if (t2 == NULL_TREE)
3144 return 0;
3146 if (TREE_CODE (t1) != TREE_CODE (t2))
3147 return 0;
3149 switch (TREE_CODE (t1))
3151 case INTEGER_CST:
3152 /* Integer constants are the same only if the same width of type. */
3153 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3154 return 0;
3155 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3156 return 0;
3157 return tree_int_cst_equal (t1, t2);
3159 case REAL_CST:
3160 /* Real constants are the same only if the same width of type. In
3161 addition to the same width, we need to check whether the modes are the
3162 same. There might be two floating point modes that are the same size
3163 but have different representations, such as the PowerPC that has 2
3164 different 128-bit floating point types (IBM extended double and IEEE
3165 128-bit floating point). */
3166 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3167 return 0;
3168 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3169 return 0;
3170 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3172 case FIXED_CST:
3173 /* Fixed constants are the same only if the same width of type. */
3174 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3175 return 0;
3177 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3179 case STRING_CST:
3180 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3181 || int_size_in_bytes (TREE_TYPE (t1))
3182 != int_size_in_bytes (TREE_TYPE (t2)))
3183 return 0;
3185 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3186 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3187 TREE_STRING_LENGTH (t1)));
3189 case COMPLEX_CST:
3190 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3191 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3193 case VECTOR_CST:
3195 if (VECTOR_CST_NPATTERNS (t1)
3196 != VECTOR_CST_NPATTERNS (t2))
3197 return 0;
3199 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3200 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3201 return 0;
3203 unsigned int count = vector_cst_encoded_nelts (t1);
3204 for (unsigned int i = 0; i < count; ++i)
3205 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3206 VECTOR_CST_ENCODED_ELT (t2, i)))
3207 return 0;
3209 return 1;
3212 case CONSTRUCTOR:
3214 vec<constructor_elt, va_gc> *v1, *v2;
3215 unsigned HOST_WIDE_INT idx;
3217 typecode = TREE_CODE (TREE_TYPE (t1));
3218 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3219 return 0;
3221 if (typecode == ARRAY_TYPE)
3223 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3224 /* For arrays, check that mode, size and storage order match. */
3225 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3226 || size_1 == -1
3227 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3228 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3229 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3230 return 0;
3232 else
3234 /* For record and union constructors, require exact type
3235 equality. */
3236 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3237 return 0;
3240 v1 = CONSTRUCTOR_ELTS (t1);
3241 v2 = CONSTRUCTOR_ELTS (t2);
3242 if (vec_safe_length (v1) != vec_safe_length (v2))
3243 return 0;
3245 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3247 constructor_elt *c1 = &(*v1)[idx];
3248 constructor_elt *c2 = &(*v2)[idx];
3250 /* Check that each value is the same... */
3251 if (!compare_constant (c1->value, c2->value))
3252 return 0;
3253 /* ... and that they apply to the same fields! */
3254 if (typecode == ARRAY_TYPE)
3256 if (!compare_constant (c1->index, c2->index))
3257 return 0;
3259 else
3261 if (c1->index != c2->index)
3262 return 0;
3266 return 1;
3269 case ADDR_EXPR:
3270 case FDESC_EXPR:
3272 struct addr_const value1, value2;
3273 enum rtx_code code;
3274 int ret;
3276 decode_addr_const (t1, &value1);
3277 decode_addr_const (t2, &value2);
3279 if (maybe_ne (value1.offset, value2.offset))
3280 return 0;
3282 code = GET_CODE (value1.base);
3283 if (code != GET_CODE (value2.base))
3284 return 0;
3286 switch (code)
3288 case SYMBOL_REF:
3289 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3290 break;
3292 case LABEL_REF:
3293 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3294 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3295 break;
3297 default:
3298 gcc_unreachable ();
3300 return ret;
3303 case PLUS_EXPR:
3304 case POINTER_PLUS_EXPR:
3305 case MINUS_EXPR:
3306 case RANGE_EXPR:
3307 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3308 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3310 CASE_CONVERT:
3311 case VIEW_CONVERT_EXPR:
3312 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3314 default:
3315 return 0;
3318 gcc_unreachable ();
3321 /* Return the section into which constant EXP should be placed. */
3323 static section *
3324 get_constant_section (tree exp, unsigned int align)
3326 return targetm.asm_out.select_section (exp,
3327 compute_reloc_for_constant (exp),
3328 align);
3331 /* Return the size of constant EXP in bytes. */
3333 static HOST_WIDE_INT
3334 get_constant_size (tree exp)
3336 HOST_WIDE_INT size;
3338 size = int_size_in_bytes (TREE_TYPE (exp));
3339 gcc_checking_assert (size >= 0);
3340 gcc_checking_assert (TREE_CODE (exp) != STRING_CST
3341 || size >= TREE_STRING_LENGTH (exp));
3342 return size;
3345 /* Subroutine of output_constant_def:
3346 No constant equal to EXP is known to have been output.
3347 Make a constant descriptor to enter EXP in the hash table.
3348 Assign the label number and construct RTL to refer to the
3349 constant's location in memory.
3350 Caller is responsible for updating the hash table. */
3352 static struct constant_descriptor_tree *
3353 build_constant_desc (tree exp)
3355 struct constant_descriptor_tree *desc;
3356 rtx symbol, rtl;
3357 char label[256];
3358 int labelno;
3359 tree decl;
3361 desc = ggc_alloc<constant_descriptor_tree> ();
3362 desc->value = exp;
3364 /* Create a string containing the label name, in LABEL. */
3365 labelno = const_labelno++;
3366 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3368 /* Construct the VAR_DECL associated with the constant. */
3369 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3370 TREE_TYPE (exp));
3371 DECL_ARTIFICIAL (decl) = 1;
3372 DECL_IGNORED_P (decl) = 1;
3373 TREE_READONLY (decl) = 1;
3374 TREE_STATIC (decl) = 1;
3375 TREE_ADDRESSABLE (decl) = 1;
3376 /* We don't set the RTL yet as this would cause varpool to assume that the
3377 variable is referenced. Moreover, it would just be dropped in LTO mode.
3378 Instead we set the flag that will be recognized in make_decl_rtl. */
3379 DECL_IN_CONSTANT_POOL (decl) = 1;
3380 DECL_INITIAL (decl) = desc->value;
3381 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3382 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3383 if (TREE_CODE (exp) == STRING_CST)
3384 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3385 else
3386 align_variable (decl, 0);
3388 /* Now construct the SYMBOL_REF and the MEM. */
3389 if (use_object_blocks_p ())
3391 int align = (TREE_CODE (decl) == CONST_DECL
3392 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3393 ? DECL_ALIGN (decl)
3394 : symtab_node::get (decl)->definition_alignment ());
3395 section *sect = get_constant_section (exp, align);
3396 symbol = create_block_symbol (ggc_strdup (label),
3397 get_block_for_section (sect), -1);
3399 else
3400 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3401 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3402 SET_SYMBOL_REF_DECL (symbol, decl);
3403 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3405 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3406 set_mem_attributes (rtl, exp, 1);
3407 set_mem_alias_set (rtl, 0);
3409 /* Putting EXP into the literal pool might have imposed a different
3410 alignment which should be visible in the RTX as well. */
3411 set_mem_align (rtl, DECL_ALIGN (decl));
3413 /* We cannot share RTX'es in pool entries.
3414 Mark this piece of RTL as required for unsharing. */
3415 RTX_FLAG (rtl, used) = 1;
3417 /* Set flags or add text to the name to record information, such as
3418 that it is a local symbol. If the name is changed, the macro
3419 ASM_OUTPUT_LABELREF will have to know how to strip this
3420 information. This call might invalidate our local variable
3421 SYMBOL; we can't use it afterward. */
3422 targetm.encode_section_info (exp, rtl, true);
3424 desc->rtl = rtl;
3426 return desc;
3429 /* Subroutine of output_constant_def and tree_output_constant_def:
3430 Add a constant to the hash table that tracks which constants
3431 already have labels. */
3433 static constant_descriptor_tree *
3434 add_constant_to_table (tree exp)
3436 /* The hash table methods may call output_constant_def for addressed
3437 constants, so handle them first. */
3438 output_addressed_constants (exp);
3440 /* Sanity check to catch recursive insertion. */
3441 static bool inserting;
3442 gcc_assert (!inserting);
3443 inserting = true;
3445 /* Look up EXP in the table of constant descriptors. If we didn't
3446 find it, create a new one. */
3447 struct constant_descriptor_tree key;
3448 key.value = exp;
3449 key.hash = const_hash_1 (exp);
3450 constant_descriptor_tree **loc
3451 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3453 inserting = false;
3455 struct constant_descriptor_tree *desc = *loc;
3456 if (!desc)
3458 desc = build_constant_desc (exp);
3459 desc->hash = key.hash;
3460 *loc = desc;
3463 return desc;
3466 /* Return an rtx representing a reference to constant data in memory
3467 for the constant expression EXP.
3469 If assembler code for such a constant has already been output,
3470 return an rtx to refer to it.
3471 Otherwise, output such a constant in memory
3472 and generate an rtx for it.
3474 If DEFER is nonzero, this constant can be deferred and output only
3475 if referenced in the function after all optimizations.
3477 `const_desc_table' records which constants already have label strings. */
3480 output_constant_def (tree exp, int defer)
3482 struct constant_descriptor_tree *desc = add_constant_to_table (exp);
3483 maybe_output_constant_def_contents (desc, defer);
3484 return desc->rtl;
3487 /* Subroutine of output_constant_def: Decide whether or not we need to
3488 output the constant DESC now, and if so, do it. */
3489 static void
3490 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3491 int defer)
3493 rtx symbol = XEXP (desc->rtl, 0);
3494 tree exp = desc->value;
3496 if (flag_syntax_only)
3497 return;
3499 if (TREE_ASM_WRITTEN (exp))
3500 /* Already output; don't do it again. */
3501 return;
3503 /* We can always defer constants as long as the context allows
3504 doing so. */
3505 if (defer)
3507 /* Increment n_deferred_constants if it exists. It needs to be at
3508 least as large as the number of constants actually referred to
3509 by the function. If it's too small we'll stop looking too early
3510 and fail to emit constants; if it's too large we'll only look
3511 through the entire function when we could have stopped earlier. */
3512 if (cfun)
3513 n_deferred_constants++;
3514 return;
3517 output_constant_def_contents (symbol);
3520 /* Subroutine of output_constant_def_contents. Output the definition
3521 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3522 constant's alignment in bits. */
3524 static void
3525 assemble_constant_contents (tree exp, const char *label, unsigned int align,
3526 bool merge_strings)
3528 HOST_WIDE_INT size;
3530 size = get_constant_size (exp);
3532 /* Do any machine/system dependent processing of the constant. */
3533 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3535 /* Output the value of EXP. */
3536 output_constant (exp, size, align, false, merge_strings);
3538 targetm.asm_out.decl_end ();
3541 /* We must output the constant data referred to by SYMBOL; do so. */
3543 static void
3544 output_constant_def_contents (rtx symbol)
3546 tree decl = SYMBOL_REF_DECL (symbol);
3547 tree exp = DECL_INITIAL (decl);
3548 bool asan_protected = false;
3550 /* Make sure any other constants whose addresses appear in EXP
3551 are assigned label numbers. */
3552 output_addressed_constants (exp);
3554 /* We are no longer deferring this constant. */
3555 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3557 if ((flag_sanitize & SANITIZE_ADDRESS)
3558 && TREE_CODE (exp) == STRING_CST
3559 && asan_protect_global (exp))
3561 asan_protected = true;
3562 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3563 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3566 /* If the constant is part of an object block, make sure that the
3567 decl has been positioned within its block, but do not write out
3568 its definition yet. output_object_blocks will do that later. */
3569 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3570 place_block_symbol (symbol);
3571 else
3573 int align = (TREE_CODE (decl) == CONST_DECL
3574 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3575 ? DECL_ALIGN (decl)
3576 : symtab_node::get (decl)->definition_alignment ());
3577 section *sect = get_constant_section (exp, align);
3578 switch_to_section (sect);
3579 if (align > BITS_PER_UNIT)
3580 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3581 assemble_constant_contents (exp, XSTR (symbol, 0), align,
3582 (sect->common.flags & SECTION_MERGE)
3583 && (sect->common.flags & SECTION_STRINGS));
3584 if (asan_protected)
3586 HOST_WIDE_INT size = get_constant_size (exp);
3587 assemble_zeros (asan_red_zone_size (size));
3592 /* Look up EXP in the table of constant descriptors. Return the rtl
3593 if it has been emitted, else null. */
3596 lookup_constant_def (tree exp)
3598 struct constant_descriptor_tree key;
3600 key.value = exp;
3601 key.hash = const_hash_1 (exp);
3602 constant_descriptor_tree *desc
3603 = const_desc_htab->find_with_hash (&key, key.hash);
3605 return (desc ? desc->rtl : NULL_RTX);
3608 /* Return a tree representing a reference to constant data in memory
3609 for the constant expression EXP.
3611 This is the counterpart of output_constant_def at the Tree level. */
3613 tree
3614 tree_output_constant_def (tree exp)
3616 struct constant_descriptor_tree *desc = add_constant_to_table (exp);
3617 tree decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3618 varpool_node::finalize_decl (decl);
3619 return decl;
3622 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3623 struct constant_descriptor_rtx *next;
3624 rtx mem;
3625 rtx sym;
3626 rtx constant;
3627 HOST_WIDE_INT offset;
3628 hashval_t hash;
3629 fixed_size_mode mode;
3630 unsigned int align;
3631 int labelno;
3632 int mark;
3635 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3637 static hashval_t hash (constant_descriptor_rtx *);
3638 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3641 /* Used in the hash tables to avoid outputting the same constant
3642 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3643 are output once per function, not once per file. */
3644 /* ??? Only a few targets need per-function constant pools. Most
3645 can use one per-file pool. Should add a targetm bit to tell the
3646 difference. */
3648 struct GTY(()) rtx_constant_pool {
3649 /* Pointers to first and last constant in pool, as ordered by offset. */
3650 struct constant_descriptor_rtx *first;
3651 struct constant_descriptor_rtx *last;
3653 /* Hash facility for making memory-constants from constant rtl-expressions.
3654 It is used on RISC machines where immediate integer arguments and
3655 constant addresses are restricted so that such constants must be stored
3656 in memory. */
3657 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3659 /* Current offset in constant pool (does not include any
3660 machine-specific header). */
3661 HOST_WIDE_INT offset;
3664 /* Hash and compare functions for const_rtx_htab. */
3666 hashval_t
3667 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3669 return desc->hash;
3672 bool
3673 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3674 constant_descriptor_rtx *y)
3676 if (x->mode != y->mode)
3677 return 0;
3678 return rtx_equal_p (x->constant, y->constant);
3681 /* Hash one component of a constant. */
3683 static hashval_t
3684 const_rtx_hash_1 (const_rtx x)
3686 unsigned HOST_WIDE_INT hwi;
3687 machine_mode mode;
3688 enum rtx_code code;
3689 hashval_t h;
3690 int i;
3692 code = GET_CODE (x);
3693 mode = GET_MODE (x);
3694 h = (hashval_t) code * 1048573 + mode;
3696 switch (code)
3698 case CONST_INT:
3699 hwi = INTVAL (x);
3701 fold_hwi:
3703 int shift = sizeof (hashval_t) * CHAR_BIT;
3704 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3706 h ^= (hashval_t) hwi;
3707 for (i = 1; i < n; ++i)
3709 hwi >>= shift;
3710 h ^= (hashval_t) hwi;
3713 break;
3715 case CONST_WIDE_INT:
3716 hwi = 0;
3718 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3719 hwi ^= CONST_WIDE_INT_ELT (x, i);
3720 goto fold_hwi;
3723 case CONST_DOUBLE:
3724 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3726 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3727 goto fold_hwi;
3729 else
3730 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3731 break;
3733 case CONST_FIXED:
3734 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3735 break;
3737 case SYMBOL_REF:
3738 h ^= htab_hash_string (XSTR (x, 0));
3739 break;
3741 case LABEL_REF:
3742 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3743 break;
3745 case UNSPEC:
3746 case UNSPEC_VOLATILE:
3747 h = h * 251 + XINT (x, 1);
3748 break;
3750 default:
3751 break;
3754 return h;
3757 /* Compute a hash value for X, which should be a constant. */
3759 static hashval_t
3760 const_rtx_hash (rtx x)
3762 hashval_t h = 0;
3763 subrtx_iterator::array_type array;
3764 FOR_EACH_SUBRTX (iter, array, x, ALL)
3765 h = h * 509 + const_rtx_hash_1 (*iter);
3766 return h;
3770 /* Create and return a new rtx constant pool. */
3772 static struct rtx_constant_pool *
3773 create_constant_pool (void)
3775 struct rtx_constant_pool *pool;
3777 pool = ggc_alloc<rtx_constant_pool> ();
3778 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3779 pool->first = NULL;
3780 pool->last = NULL;
3781 pool->offset = 0;
3782 return pool;
3785 /* Initialize constant pool hashing for a new function. */
3787 void
3788 init_varasm_status (void)
3790 crtl->varasm.pool = create_constant_pool ();
3791 crtl->varasm.deferred_constants = 0;
3794 /* Given a MINUS expression, simplify it if both sides
3795 include the same symbol. */
3798 simplify_subtraction (rtx x)
3800 rtx r = simplify_rtx (x);
3801 return r ? r : x;
3804 /* Given a constant rtx X, make (or find) a memory constant for its value
3805 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
3806 of X. */
3809 force_const_mem (machine_mode in_mode, rtx x)
3811 struct constant_descriptor_rtx *desc, tmp;
3812 struct rtx_constant_pool *pool;
3813 char label[256];
3814 rtx def, symbol;
3815 hashval_t hash;
3816 unsigned int align;
3817 constant_descriptor_rtx **slot;
3818 fixed_size_mode mode;
3820 /* We can't force variable-sized objects to memory. */
3821 if (!is_a <fixed_size_mode> (in_mode, &mode))
3822 return NULL_RTX;
3824 /* If we're not allowed to drop X into the constant pool, don't. */
3825 if (targetm.cannot_force_const_mem (mode, x))
3826 return NULL_RTX;
3828 /* Record that this function has used a constant pool entry. */
3829 crtl->uses_const_pool = 1;
3831 /* Decide which pool to use. */
3832 pool = (targetm.use_blocks_for_constant_p (mode, x)
3833 ? shared_constant_pool
3834 : crtl->varasm.pool);
3836 /* Lookup the value in the hashtable. */
3837 tmp.constant = x;
3838 tmp.mode = mode;
3839 hash = const_rtx_hash (x);
3840 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3841 desc = *slot;
3843 /* If the constant was already present, return its memory. */
3844 if (desc)
3845 return copy_rtx (desc->mem);
3847 /* Otherwise, create a new descriptor. */
3848 desc = ggc_alloc<constant_descriptor_rtx> ();
3849 *slot = desc;
3851 /* Align the location counter as required by EXP's data type. */
3852 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3853 align = targetm.static_rtx_alignment (align_mode);
3855 pool->offset += (align / BITS_PER_UNIT) - 1;
3856 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3858 desc->next = NULL;
3859 desc->constant = copy_rtx (tmp.constant);
3860 desc->offset = pool->offset;
3861 desc->hash = hash;
3862 desc->mode = mode;
3863 desc->align = align;
3864 desc->labelno = const_labelno;
3865 desc->mark = 0;
3867 pool->offset += GET_MODE_SIZE (mode);
3868 if (pool->last)
3869 pool->last->next = desc;
3870 else
3871 pool->first = pool->last = desc;
3872 pool->last = desc;
3874 /* Create a string containing the label name, in LABEL. */
3875 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3876 ++const_labelno;
3878 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3879 the constants pool. */
3880 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3882 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3883 symbol = create_block_symbol (ggc_strdup (label),
3884 get_block_for_section (sect), -1);
3886 else
3887 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3888 desc->sym = symbol;
3889 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3890 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3891 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3893 /* Construct the MEM. */
3894 desc->mem = def = gen_const_mem (mode, symbol);
3895 set_mem_align (def, align);
3897 /* If we're dropping a label to the constant pool, make sure we
3898 don't delete it. */
3899 if (GET_CODE (x) == LABEL_REF)
3900 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3902 return copy_rtx (def);
3905 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3908 get_pool_constant (const_rtx addr)
3910 return SYMBOL_REF_CONSTANT (addr)->constant;
3913 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3914 and whether it has been output or not. */
3917 get_pool_constant_mark (rtx addr, bool *pmarked)
3919 struct constant_descriptor_rtx *desc;
3921 desc = SYMBOL_REF_CONSTANT (addr);
3922 *pmarked = (desc->mark != 0);
3923 return desc->constant;
3926 /* Similar, return the mode. */
3928 fixed_size_mode
3929 get_pool_mode (const_rtx addr)
3931 return SYMBOL_REF_CONSTANT (addr)->mode;
3934 /* Return TRUE if and only if the constant pool has no entries. Note
3935 that even entries we might end up choosing not to emit are counted
3936 here, so there is the potential for missed optimizations. */
3938 bool
3939 constant_pool_empty_p (void)
3941 return crtl->varasm.pool->first == NULL;
3944 /* Worker function for output_constant_pool_1. Emit assembly for X
3945 in MODE with known alignment ALIGN. */
3947 static void
3948 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
3950 switch (GET_MODE_CLASS (mode))
3952 case MODE_FLOAT:
3953 case MODE_DECIMAL_FLOAT:
3955 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3956 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
3957 as_a <scalar_float_mode> (mode), align, false);
3958 break;
3961 case MODE_INT:
3962 case MODE_PARTIAL_INT:
3963 case MODE_FRACT:
3964 case MODE_UFRACT:
3965 case MODE_ACCUM:
3966 case MODE_UACCUM:
3967 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3968 break;
3970 case MODE_VECTOR_BOOL:
3972 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3974 /* Pick the smallest integer mode that contains at least one
3975 whole element. Often this is byte_mode and contains more
3976 than one element. */
3977 unsigned int nelts = GET_MODE_NUNITS (mode);
3978 unsigned int elt_bits = GET_MODE_BITSIZE (mode) / nelts;
3979 unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
3980 scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
3982 /* Build the constant up one integer at a time. */
3983 unsigned int elts_per_int = int_bits / elt_bits;
3984 for (unsigned int i = 0; i < nelts; i += elts_per_int)
3986 unsigned HOST_WIDE_INT value = 0;
3987 unsigned int limit = MIN (nelts - i, elts_per_int);
3988 for (unsigned int j = 0; j < limit; ++j)
3989 if (INTVAL (CONST_VECTOR_ELT (x, i + j)) != 0)
3990 value |= 1 << (j * elt_bits);
3991 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
3992 i != 0 ? MIN (align, int_bits) : align);
3994 break;
3996 case MODE_VECTOR_FLOAT:
3997 case MODE_VECTOR_INT:
3998 case MODE_VECTOR_FRACT:
3999 case MODE_VECTOR_UFRACT:
4000 case MODE_VECTOR_ACCUM:
4001 case MODE_VECTOR_UACCUM:
4003 int i, units;
4004 scalar_mode submode = GET_MODE_INNER (mode);
4005 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4007 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4008 units = GET_MODE_NUNITS (mode);
4010 for (i = 0; i < units; i++)
4012 rtx elt = CONST_VECTOR_ELT (x, i);
4013 output_constant_pool_2 (submode, elt, i ? subalign : align);
4016 break;
4018 default:
4019 gcc_unreachable ();
4023 /* Worker function for output_constant_pool. Emit constant DESC,
4024 giving it ALIGN bits of alignment. */
4026 static void
4027 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
4028 unsigned int align)
4030 rtx x, tmp;
4032 x = desc->constant;
4034 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4035 whose CODE_LABEL has been deleted. This can occur if a jump table
4036 is eliminated by optimization. If so, write a constant of zero
4037 instead. Note that this can also happen by turning the
4038 CODE_LABEL into a NOTE. */
4039 /* ??? This seems completely and utterly wrong. Certainly it's
4040 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4041 functioning even with rtx_insn::deleted and friends. */
4043 tmp = x;
4044 switch (GET_CODE (tmp))
4046 case CONST:
4047 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4048 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4049 break;
4050 tmp = XEXP (XEXP (tmp, 0), 0);
4051 /* FALLTHRU */
4053 case LABEL_REF:
4055 rtx_insn *insn = label_ref_label (tmp);
4056 gcc_assert (!insn->deleted ());
4057 gcc_assert (!NOTE_P (insn)
4058 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4059 break;
4062 default:
4063 break;
4066 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4067 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4068 align, desc->labelno, done);
4069 #endif
4071 assemble_align (align);
4073 /* Output the label. */
4074 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4076 /* Output the data.
4077 Pass actual alignment value while emitting string constant to asm code
4078 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4079 assuming that the data is already aligned which prevents the generation
4080 of fix-up table entries. */
4081 output_constant_pool_2 (desc->mode, x, desc->align);
4083 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4084 sections have proper size. */
4085 if (align > GET_MODE_BITSIZE (desc->mode)
4086 && in_section
4087 && (in_section->common.flags & SECTION_MERGE))
4088 assemble_align (align);
4090 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4091 done:
4092 #endif
4093 return;
4096 /* Recompute the offsets of entries in POOL, and the overall size of
4097 POOL. Do this after calling mark_constant_pool to ensure that we
4098 are computing the offset values for the pool which we will actually
4099 emit. */
4101 static void
4102 recompute_pool_offsets (struct rtx_constant_pool *pool)
4104 struct constant_descriptor_rtx *desc;
4105 pool->offset = 0;
4107 for (desc = pool->first; desc ; desc = desc->next)
4108 if (desc->mark)
4110 /* Recalculate offset. */
4111 unsigned int align = desc->align;
4112 pool->offset += (align / BITS_PER_UNIT) - 1;
4113 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4114 desc->offset = pool->offset;
4115 pool->offset += GET_MODE_SIZE (desc->mode);
4119 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4120 Emit referenced deferred strings. */
4122 static void
4123 mark_constants_in_pattern (rtx insn)
4125 subrtx_iterator::array_type array;
4126 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4128 const_rtx x = *iter;
4129 if (GET_CODE (x) == SYMBOL_REF)
4131 if (CONSTANT_POOL_ADDRESS_P (x))
4133 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4134 if (desc->mark == 0)
4136 desc->mark = 1;
4137 iter.substitute (desc->constant);
4140 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4142 tree decl = SYMBOL_REF_DECL (x);
4143 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4145 n_deferred_constants--;
4146 output_constant_def_contents (CONST_CAST_RTX (x));
4153 /* Look through appropriate parts of INSN, marking all entries in the
4154 constant pool which are actually being used. Entries that are only
4155 referenced by other constants are also marked as used. Emit
4156 deferred strings that are used. */
4158 static void
4159 mark_constants (rtx_insn *insn)
4161 if (!INSN_P (insn))
4162 return;
4164 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4165 insns, not any notes that may be attached. We don't want to mark
4166 a constant just because it happens to appear in a REG_EQUIV note. */
4167 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4169 int i, n = seq->len ();
4170 for (i = 0; i < n; ++i)
4172 rtx subinsn = seq->element (i);
4173 if (INSN_P (subinsn))
4174 mark_constants_in_pattern (subinsn);
4177 else
4178 mark_constants_in_pattern (insn);
4181 /* Look through the instructions for this function, and mark all the
4182 entries in POOL which are actually being used. Emit deferred constants
4183 which have indeed been used. */
4185 static void
4186 mark_constant_pool (void)
4188 rtx_insn *insn;
4190 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4191 return;
4193 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4194 mark_constants (insn);
4197 /* Write all the constants in POOL. */
4199 static void
4200 output_constant_pool_contents (struct rtx_constant_pool *pool)
4202 struct constant_descriptor_rtx *desc;
4204 for (desc = pool->first; desc ; desc = desc->next)
4205 if (desc->mark)
4207 /* If the constant is part of an object_block, make sure that
4208 the constant has been positioned within its block, but do not
4209 write out its definition yet. output_object_blocks will do
4210 that later. */
4211 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4212 && SYMBOL_REF_BLOCK (desc->sym))
4213 place_block_symbol (desc->sym);
4214 else
4216 switch_to_section (targetm.asm_out.select_rtx_section
4217 (desc->mode, desc->constant, desc->align));
4218 output_constant_pool_1 (desc, desc->align);
4223 /* Mark all constants that are used in the current function, then write
4224 out the function's private constant pool. */
4226 static void
4227 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4228 tree fndecl ATTRIBUTE_UNUSED)
4230 struct rtx_constant_pool *pool = crtl->varasm.pool;
4232 /* It is possible for gcc to call force_const_mem and then to later
4233 discard the instructions which refer to the constant. In such a
4234 case we do not need to output the constant. */
4235 mark_constant_pool ();
4237 /* Having marked the constant pool entries we'll actually emit, we
4238 now need to rebuild the offset information, which may have become
4239 stale. */
4240 recompute_pool_offsets (pool);
4242 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4243 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4244 #endif
4246 output_constant_pool_contents (pool);
4248 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4249 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4250 #endif
4253 /* Write the contents of the shared constant pool. */
4255 void
4256 output_shared_constant_pool (void)
4258 output_constant_pool_contents (shared_constant_pool);
4261 /* Determine what kind of relocations EXP may need. */
4264 compute_reloc_for_constant (tree exp)
4266 int reloc = 0, reloc2;
4267 tree tem;
4269 switch (TREE_CODE (exp))
4271 case ADDR_EXPR:
4272 case FDESC_EXPR:
4273 /* Go inside any operations that get_inner_reference can handle and see
4274 if what's inside is a constant: no need to do anything here for
4275 addresses of variables or functions. */
4276 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4277 tem = TREE_OPERAND (tem, 0))
4280 if (TREE_CODE (tem) == MEM_REF
4281 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4283 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4284 break;
4287 if (!targetm.binds_local_p (tem))
4288 reloc |= 2;
4289 else
4290 reloc |= 1;
4291 break;
4293 case PLUS_EXPR:
4294 case POINTER_PLUS_EXPR:
4295 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4296 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4297 break;
4299 case MINUS_EXPR:
4300 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4301 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4302 /* The difference of two local labels is computable at link time. */
4303 if (reloc == 1 && reloc2 == 1)
4304 reloc = 0;
4305 else
4306 reloc |= reloc2;
4307 break;
4309 CASE_CONVERT:
4310 case VIEW_CONVERT_EXPR:
4311 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4312 break;
4314 case CONSTRUCTOR:
4316 unsigned HOST_WIDE_INT idx;
4317 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4318 if (tem != 0)
4319 reloc |= compute_reloc_for_constant (tem);
4321 break;
4323 default:
4324 break;
4326 return reloc;
4329 /* Find all the constants whose addresses are referenced inside of EXP,
4330 and make sure assembler code with a label has been output for each one.
4331 Indicate whether an ADDR_EXPR has been encountered. */
4333 static void
4334 output_addressed_constants (tree exp)
4336 tree tem;
4338 switch (TREE_CODE (exp))
4340 case ADDR_EXPR:
4341 case FDESC_EXPR:
4342 /* Go inside any operations that get_inner_reference can handle and see
4343 if what's inside is a constant: no need to do anything here for
4344 addresses of variables or functions. */
4345 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4346 tem = TREE_OPERAND (tem, 0))
4349 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4350 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4351 tem = DECL_INITIAL (tem);
4353 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4354 output_constant_def (tem, 0);
4356 if (TREE_CODE (tem) == MEM_REF)
4357 output_addressed_constants (TREE_OPERAND (tem, 0));
4358 break;
4360 case PLUS_EXPR:
4361 case POINTER_PLUS_EXPR:
4362 case MINUS_EXPR:
4363 output_addressed_constants (TREE_OPERAND (exp, 1));
4364 gcc_fallthrough ();
4366 CASE_CONVERT:
4367 case VIEW_CONVERT_EXPR:
4368 output_addressed_constants (TREE_OPERAND (exp, 0));
4369 break;
4371 case CONSTRUCTOR:
4373 unsigned HOST_WIDE_INT idx;
4374 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4375 if (tem != 0)
4376 output_addressed_constants (tem);
4378 break;
4380 default:
4381 break;
4385 /* Whether a constructor CTOR is a valid static constant initializer if all
4386 its elements are. This used to be internal to initializer_constant_valid_p
4387 and has been exposed to let other functions like categorize_ctor_elements
4388 evaluate the property while walking a constructor for other purposes. */
4390 bool
4391 constructor_static_from_elts_p (const_tree ctor)
4393 return (TREE_CONSTANT (ctor)
4394 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4395 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4396 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4399 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4400 tree *cache);
4402 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4403 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4404 which are valid when ENDTYPE is an integer of any size; in
4405 particular, this does not accept a pointer minus a constant. This
4406 returns null_pointer_node if the VALUE is an absolute constant
4407 which can be used to initialize a static variable. Otherwise it
4408 returns NULL. */
4410 static tree
4411 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4413 tree op0, op1;
4415 if (!INTEGRAL_TYPE_P (endtype))
4416 return NULL_TREE;
4418 op0 = TREE_OPERAND (value, 0);
4419 op1 = TREE_OPERAND (value, 1);
4421 /* Like STRIP_NOPS except allow the operand mode to widen. This
4422 works around a feature of fold that simplifies (int)(p1 - p2) to
4423 ((int)p1 - (int)p2) under the theory that the narrower operation
4424 is cheaper. */
4426 while (CONVERT_EXPR_P (op0)
4427 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4429 tree inner = TREE_OPERAND (op0, 0);
4430 if (inner == error_mark_node
4431 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4432 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4433 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4434 break;
4435 op0 = inner;
4438 while (CONVERT_EXPR_P (op1)
4439 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4441 tree inner = TREE_OPERAND (op1, 0);
4442 if (inner == error_mark_node
4443 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4444 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4445 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4446 break;
4447 op1 = inner;
4450 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4451 if (!op0)
4452 return NULL_TREE;
4454 op1 = initializer_constant_valid_p_1 (op1, endtype,
4455 cache ? cache + 2 : NULL);
4456 /* Both initializers must be known. */
4457 if (op1)
4459 if (op0 == op1
4460 && (op0 == null_pointer_node
4461 || TREE_CODE (value) == MINUS_EXPR))
4462 return null_pointer_node;
4464 /* Support differences between labels. */
4465 if (TREE_CODE (op0) == LABEL_DECL
4466 && TREE_CODE (op1) == LABEL_DECL)
4467 return null_pointer_node;
4469 if (TREE_CODE (op0) == STRING_CST
4470 && TREE_CODE (op1) == STRING_CST
4471 && operand_equal_p (op0, op1, 1))
4472 return null_pointer_node;
4475 return NULL_TREE;
4478 /* Helper function of initializer_constant_valid_p.
4479 Return nonzero if VALUE is a valid constant-valued expression
4480 for use in initializing a static variable; one that can be an
4481 element of a "constant" initializer.
4483 Return null_pointer_node if the value is absolute;
4484 if it is relocatable, return the variable that determines the relocation.
4485 We assume that VALUE has been folded as much as possible;
4486 therefore, we do not need to check for such things as
4487 arithmetic-combinations of integers.
4489 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4491 static tree
4492 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4494 tree ret;
4496 switch (TREE_CODE (value))
4498 case CONSTRUCTOR:
4499 if (constructor_static_from_elts_p (value))
4501 unsigned HOST_WIDE_INT idx;
4502 tree elt;
4503 bool absolute = true;
4505 if (cache && cache[0] == value)
4506 return cache[1];
4507 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4509 tree reloc;
4510 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4511 NULL);
4512 if (!reloc
4513 /* An absolute value is required with reverse SSO. */
4514 || (reloc != null_pointer_node
4515 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4516 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4518 if (cache)
4520 cache[0] = value;
4521 cache[1] = NULL_TREE;
4523 return NULL_TREE;
4525 if (reloc != null_pointer_node)
4526 absolute = false;
4528 /* For a non-absolute relocation, there is no single
4529 variable that can be "the variable that determines the
4530 relocation." */
4531 if (cache)
4533 cache[0] = value;
4534 cache[1] = absolute ? null_pointer_node : error_mark_node;
4536 return absolute ? null_pointer_node : error_mark_node;
4539 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4541 case INTEGER_CST:
4542 case VECTOR_CST:
4543 case REAL_CST:
4544 case FIXED_CST:
4545 case STRING_CST:
4546 case COMPLEX_CST:
4547 return null_pointer_node;
4549 case ADDR_EXPR:
4550 case FDESC_EXPR:
4552 tree op0 = staticp (TREE_OPERAND (value, 0));
4553 if (op0)
4555 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4556 to be a constant, this is old-skool offsetof-like nonsense. */
4557 if (TREE_CODE (op0) == INDIRECT_REF
4558 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4559 return null_pointer_node;
4560 /* Taking the address of a nested function involves a trampoline,
4561 unless we don't need or want one. */
4562 if (TREE_CODE (op0) == FUNCTION_DECL
4563 && DECL_STATIC_CHAIN (op0)
4564 && !TREE_NO_TRAMPOLINE (value))
4565 return NULL_TREE;
4566 /* "&{...}" requires a temporary to hold the constructed
4567 object. */
4568 if (TREE_CODE (op0) == CONSTRUCTOR)
4569 return NULL_TREE;
4571 return op0;
4574 case NON_LVALUE_EXPR:
4575 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4576 endtype, cache);
4578 case VIEW_CONVERT_EXPR:
4580 tree src = TREE_OPERAND (value, 0);
4581 tree src_type = TREE_TYPE (src);
4582 tree dest_type = TREE_TYPE (value);
4584 /* Allow view-conversions from aggregate to non-aggregate type only
4585 if the bit pattern is fully preserved afterwards; otherwise, the
4586 RTL expander won't be able to apply a subsequent transformation
4587 to the underlying constructor. */
4588 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4590 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4591 return initializer_constant_valid_p_1 (src, endtype, cache);
4592 else
4593 return NULL_TREE;
4596 /* Allow all other kinds of view-conversion. */
4597 return initializer_constant_valid_p_1 (src, endtype, cache);
4600 CASE_CONVERT:
4602 tree src = TREE_OPERAND (value, 0);
4603 tree src_type = TREE_TYPE (src);
4604 tree dest_type = TREE_TYPE (value);
4606 /* Allow conversions between pointer types, floating-point
4607 types, and offset types. */
4608 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4609 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4610 || (TREE_CODE (dest_type) == OFFSET_TYPE
4611 && TREE_CODE (src_type) == OFFSET_TYPE))
4612 return initializer_constant_valid_p_1 (src, endtype, cache);
4614 /* Allow length-preserving conversions between integer types. */
4615 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4616 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4617 return initializer_constant_valid_p_1 (src, endtype, cache);
4619 /* Allow conversions between other integer types only if
4620 explicit value. Don't allow sign-extension to a type larger
4621 than word and pointer, there aren't relocations that would
4622 allow to sign extend it to a wider type. */
4623 if (INTEGRAL_TYPE_P (dest_type)
4624 && INTEGRAL_TYPE_P (src_type)
4625 && (TYPE_UNSIGNED (src_type)
4626 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4627 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4628 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4630 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4631 if (inner == null_pointer_node)
4632 return null_pointer_node;
4633 break;
4636 /* Allow (int) &foo provided int is as wide as a pointer. */
4637 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4638 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4639 return initializer_constant_valid_p_1 (src, endtype, cache);
4641 /* Likewise conversions from int to pointers, but also allow
4642 conversions from 0. */
4643 if ((POINTER_TYPE_P (dest_type)
4644 || TREE_CODE (dest_type) == OFFSET_TYPE)
4645 && INTEGRAL_TYPE_P (src_type))
4647 if (TREE_CODE (src) == INTEGER_CST
4648 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4649 return null_pointer_node;
4650 if (integer_zerop (src))
4651 return null_pointer_node;
4652 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4653 return initializer_constant_valid_p_1 (src, endtype, cache);
4656 /* Allow conversions to struct or union types if the value
4657 inside is okay. */
4658 if (TREE_CODE (dest_type) == RECORD_TYPE
4659 || TREE_CODE (dest_type) == UNION_TYPE)
4660 return initializer_constant_valid_p_1 (src, endtype, cache);
4662 break;
4664 case POINTER_PLUS_EXPR:
4665 case PLUS_EXPR:
4666 /* Any valid floating-point constants will have been folded by now;
4667 with -frounding-math we hit this with addition of two constants. */
4668 if (TREE_CODE (endtype) == REAL_TYPE)
4669 return NULL_TREE;
4670 if (cache && cache[0] == value)
4671 return cache[1];
4672 if (! INTEGRAL_TYPE_P (endtype)
4673 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4675 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4676 tree valid0
4677 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4678 endtype, ncache);
4679 tree valid1
4680 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4681 endtype, ncache + 2);
4682 /* If either term is absolute, use the other term's relocation. */
4683 if (valid0 == null_pointer_node)
4684 ret = valid1;
4685 else if (valid1 == null_pointer_node)
4686 ret = valid0;
4687 /* Support narrowing pointer differences. */
4688 else
4689 ret = narrowing_initializer_constant_valid_p (value, endtype,
4690 ncache);
4692 else
4693 /* Support narrowing pointer differences. */
4694 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4695 if (cache)
4697 cache[0] = value;
4698 cache[1] = ret;
4700 return ret;
4702 case POINTER_DIFF_EXPR:
4703 case MINUS_EXPR:
4704 if (TREE_CODE (endtype) == REAL_TYPE)
4705 return NULL_TREE;
4706 if (cache && cache[0] == value)
4707 return cache[1];
4708 if (! INTEGRAL_TYPE_P (endtype)
4709 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4711 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4712 tree valid0
4713 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4714 endtype, ncache);
4715 tree valid1
4716 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4717 endtype, ncache + 2);
4718 /* Win if second argument is absolute. */
4719 if (valid1 == null_pointer_node)
4720 ret = valid0;
4721 /* Win if both arguments have the same relocation.
4722 Then the value is absolute. */
4723 else if (valid0 == valid1 && valid0 != 0)
4724 ret = null_pointer_node;
4725 /* Since GCC guarantees that string constants are unique in the
4726 generated code, a subtraction between two copies of the same
4727 constant string is absolute. */
4728 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4729 && valid1 && TREE_CODE (valid1) == STRING_CST
4730 && operand_equal_p (valid0, valid1, 1))
4731 ret = null_pointer_node;
4732 /* Support narrowing differences. */
4733 else
4734 ret = narrowing_initializer_constant_valid_p (value, endtype,
4735 ncache);
4737 else
4738 /* Support narrowing differences. */
4739 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4740 if (cache)
4742 cache[0] = value;
4743 cache[1] = ret;
4745 return ret;
4747 default:
4748 break;
4751 return NULL_TREE;
4754 /* Return nonzero if VALUE is a valid constant-valued expression
4755 for use in initializing a static variable; one that can be an
4756 element of a "constant" initializer.
4758 Return null_pointer_node if the value is absolute;
4759 if it is relocatable, return the variable that determines the relocation.
4760 We assume that VALUE has been folded as much as possible;
4761 therefore, we do not need to check for such things as
4762 arithmetic-combinations of integers. */
4763 tree
4764 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
4766 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
4768 /* An absolute value is required with reverse storage order. */
4769 if (reloc
4770 && reloc != null_pointer_node
4771 && reverse
4772 && !AGGREGATE_TYPE_P (endtype)
4773 && !VECTOR_TYPE_P (endtype))
4774 reloc = NULL_TREE;
4776 return reloc;
4779 /* Return true if VALUE is a valid constant-valued expression
4780 for use in initializing a static bit-field; one that can be
4781 an element of a "constant" initializer. */
4783 bool
4784 initializer_constant_valid_for_bitfield_p (tree value)
4786 /* For bitfields we support integer constants or possibly nested aggregates
4787 of such. */
4788 switch (TREE_CODE (value))
4790 case CONSTRUCTOR:
4792 unsigned HOST_WIDE_INT idx;
4793 tree elt;
4795 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4796 if (!initializer_constant_valid_for_bitfield_p (elt))
4797 return false;
4798 return true;
4801 case INTEGER_CST:
4802 case REAL_CST:
4803 return true;
4805 case VIEW_CONVERT_EXPR:
4806 case NON_LVALUE_EXPR:
4807 return
4808 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4810 default:
4811 break;
4814 return false;
4817 /* Check if a STRING_CST fits into the field.
4818 Tolerate only the case when the NUL termination
4819 does not fit into the field. */
4821 static bool
4822 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
4824 tree type = TREE_TYPE (string);
4825 tree eltype = TREE_TYPE (type);
4826 unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
4827 unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
4828 int len = TREE_STRING_LENGTH (string);
4830 if (elts != 1 && elts != 2 && elts != 4)
4831 return false;
4832 if (len < 0 || len % elts != 0)
4833 return false;
4834 if (size < (unsigned)len)
4835 return false;
4836 if (mem_size != size)
4837 return false;
4838 return true;
4841 /* output_constructor outer state of relevance in recursive calls, typically
4842 for nested aggregate bitfields. */
4844 struct oc_outer_state {
4845 unsigned int bit_offset; /* current position in ... */
4846 int byte; /* ... the outer byte buffer. */
4849 static unsigned HOST_WIDE_INT
4850 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
4851 oc_outer_state *);
4853 /* Output assembler code for constant EXP, with no label.
4854 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4855 Assumes output_addressed_constants has been done on EXP already.
4857 Generate at least SIZE bytes of assembler data, padding at the end
4858 with zeros if necessary. SIZE must always be specified. The returned
4859 value is the actual number of bytes of assembler data generated, which
4860 may be bigger than SIZE if the object contains a variable length field.
4862 SIZE is important for structure constructors,
4863 since trailing members may have been omitted from the constructor.
4864 It is also important for initialization of arrays from string constants
4865 since the full length of the string constant might not be wanted.
4866 It is also needed for initialization of unions, where the initializer's
4867 type is just one member, and that may not be as long as the union.
4869 There a case in which we would fail to output exactly SIZE bytes:
4870 for a structure constructor that wants to produce more than SIZE bytes.
4871 But such constructors will never be generated for any possible input.
4873 ALIGN is the alignment of the data in bits.
4875 If REVERSE is true, EXP is output in reverse storage order. */
4877 static unsigned HOST_WIDE_INT
4878 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
4879 bool reverse, bool merge_strings)
4881 enum tree_code code;
4882 unsigned HOST_WIDE_INT thissize;
4883 rtx cst;
4885 if (size == 0 || flag_syntax_only)
4886 return size;
4888 /* See if we're trying to initialize a pointer in a non-default mode
4889 to the address of some declaration somewhere. If the target says
4890 the mode is valid for pointers, assume the target has a way of
4891 resolving it. */
4892 if (TREE_CODE (exp) == NOP_EXPR
4893 && POINTER_TYPE_P (TREE_TYPE (exp))
4894 && targetm.addr_space.valid_pointer_mode
4895 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4896 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4898 tree saved_type = TREE_TYPE (exp);
4900 /* Peel off any intermediate conversions-to-pointer for valid
4901 pointer modes. */
4902 while (TREE_CODE (exp) == NOP_EXPR
4903 && POINTER_TYPE_P (TREE_TYPE (exp))
4904 && targetm.addr_space.valid_pointer_mode
4905 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4906 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4907 exp = TREE_OPERAND (exp, 0);
4909 /* If what we're left with is the address of something, we can
4910 convert the address to the final type and output it that
4911 way. */
4912 if (TREE_CODE (exp) == ADDR_EXPR)
4913 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4914 /* Likewise for constant ints. */
4915 else if (TREE_CODE (exp) == INTEGER_CST)
4916 exp = fold_convert (saved_type, exp);
4920 /* Eliminate any conversions since we'll be outputting the underlying
4921 constant. */
4922 while (CONVERT_EXPR_P (exp)
4923 || TREE_CODE (exp) == NON_LVALUE_EXPR
4924 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4926 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4927 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4929 /* Make sure eliminating the conversion is really a no-op, except with
4930 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4931 union types to allow for Ada unchecked unions. */
4932 if (type_size > op_size
4933 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4934 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4935 /* Keep the conversion. */
4936 break;
4937 else
4938 exp = TREE_OPERAND (exp, 0);
4941 code = TREE_CODE (TREE_TYPE (exp));
4942 thissize = int_size_in_bytes (TREE_TYPE (exp));
4944 /* Allow a constructor with no elements for any data type.
4945 This means to fill the space with zeros. */
4946 if (TREE_CODE (exp) == CONSTRUCTOR
4947 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4949 assemble_zeros (size);
4950 return size;
4953 if (TREE_CODE (exp) == FDESC_EXPR)
4955 #ifdef ASM_OUTPUT_FDESC
4956 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4957 tree decl = TREE_OPERAND (exp, 0);
4958 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4959 #else
4960 gcc_unreachable ();
4961 #endif
4962 return size;
4965 /* Now output the underlying data. If we've handling the padding, return.
4966 Otherwise, break and ensure SIZE is the size written. */
4967 switch (code)
4969 case BOOLEAN_TYPE:
4970 case INTEGER_TYPE:
4971 case ENUMERAL_TYPE:
4972 case POINTER_TYPE:
4973 case REFERENCE_TYPE:
4974 case OFFSET_TYPE:
4975 case FIXED_POINT_TYPE:
4976 case NULLPTR_TYPE:
4977 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
4978 if (reverse)
4979 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
4980 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
4981 error ("initializer for integer/fixed-point value is too complicated");
4982 break;
4984 case REAL_TYPE:
4985 if (TREE_CODE (exp) != REAL_CST)
4986 error ("initializer for floating value is not a floating constant");
4987 else
4988 assemble_real (TREE_REAL_CST (exp),
4989 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
4990 align, reverse);
4991 break;
4993 case COMPLEX_TYPE:
4994 output_constant (TREE_REALPART (exp), thissize / 2, align,
4995 reverse, false);
4996 output_constant (TREE_IMAGPART (exp), thissize / 2,
4997 min_align (align, BITS_PER_UNIT * (thissize / 2)),
4998 reverse, false);
4999 break;
5001 case ARRAY_TYPE:
5002 case VECTOR_TYPE:
5003 switch (TREE_CODE (exp))
5005 case CONSTRUCTOR:
5006 return output_constructor (exp, size, align, reverse, NULL);
5007 case STRING_CST:
5008 thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5009 if (merge_strings
5010 && (thissize == 0
5011 || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5012 thissize++;
5013 gcc_checking_assert (check_string_literal (exp, size));
5014 assemble_string (TREE_STRING_POINTER (exp), thissize);
5015 break;
5016 case VECTOR_CST:
5018 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5019 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5020 int elt_size = GET_MODE_SIZE (inner);
5021 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5022 reverse, false);
5023 thissize = elt_size;
5024 /* Static constants must have a fixed size. */
5025 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5026 for (unsigned int i = 1; i < nunits; i++)
5028 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5029 reverse, false);
5030 thissize += elt_size;
5032 break;
5034 default:
5035 gcc_unreachable ();
5037 break;
5039 case RECORD_TYPE:
5040 case UNION_TYPE:
5041 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5042 return output_constructor (exp, size, align, reverse, NULL);
5044 case ERROR_MARK:
5045 return 0;
5047 default:
5048 gcc_unreachable ();
5051 if (size > thissize)
5052 assemble_zeros (size - thissize);
5054 return size;
5057 /* Subroutine of output_constructor, used for computing the size of
5058 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
5059 type with an unspecified upper bound. */
5061 static unsigned HOST_WIDE_INT
5062 array_size_for_constructor (tree val)
5064 tree max_index;
5065 unsigned HOST_WIDE_INT cnt;
5066 tree index, value, tmp;
5067 offset_int i;
5069 /* This code used to attempt to handle string constants that are not
5070 arrays of single-bytes, but nothing else does, so there's no point in
5071 doing it here. */
5072 if (TREE_CODE (val) == STRING_CST)
5073 return TREE_STRING_LENGTH (val);
5075 max_index = NULL_TREE;
5076 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5078 if (TREE_CODE (index) == RANGE_EXPR)
5079 index = TREE_OPERAND (index, 1);
5080 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5081 max_index = index;
5084 if (max_index == NULL_TREE)
5085 return 0;
5087 /* Compute the total number of array elements. */
5088 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5089 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5091 /* Multiply by the array element unit size to find number of bytes. */
5092 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5094 gcc_assert (wi::fits_uhwi_p (i));
5095 return i.to_uhwi ();
5098 /* Other datastructures + helpers for output_constructor. */
5100 /* output_constructor local state to support interaction with helpers. */
5102 struct oc_local_state {
5104 /* Received arguments. */
5105 tree exp; /* Constructor expression. */
5106 tree type; /* Type of constructor expression. */
5107 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5108 unsigned int align; /* Known initial alignment. */
5109 tree min_index; /* Lower bound if specified for an array. */
5111 /* Output processing state. */
5112 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5113 int byte; /* Part of a bitfield byte yet to be output. */
5114 int last_relative_index; /* Implicit or explicit index of the last
5115 array element output within a bitfield. */
5116 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5117 bool reverse; /* Whether reverse storage order is in use. */
5119 /* Current element. */
5120 tree field; /* Current field decl in a record. */
5121 tree val; /* Current element value. */
5122 tree index; /* Current element index. */
5126 /* Helper for output_constructor. From the current LOCAL state, output a
5127 RANGE_EXPR element. */
5129 static void
5130 output_constructor_array_range (oc_local_state *local)
5132 unsigned HOST_WIDE_INT fieldsize
5133 = int_size_in_bytes (TREE_TYPE (local->type));
5135 HOST_WIDE_INT lo_index
5136 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5137 HOST_WIDE_INT hi_index
5138 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5139 HOST_WIDE_INT index;
5141 unsigned int align2
5142 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5144 for (index = lo_index; index <= hi_index; index++)
5146 /* Output the element's initial value. */
5147 if (local->val == NULL_TREE)
5148 assemble_zeros (fieldsize);
5149 else
5150 fieldsize = output_constant (local->val, fieldsize, align2,
5151 local->reverse, false);
5153 /* Count its size. */
5154 local->total_bytes += fieldsize;
5158 /* Helper for output_constructor. From the current LOCAL state, output a
5159 field element that is not true bitfield or part of an outer one. */
5161 static void
5162 output_constructor_regular_field (oc_local_state *local)
5164 /* Field size and position. Since this structure is static, we know the
5165 positions are constant. */
5166 unsigned HOST_WIDE_INT fieldsize;
5167 HOST_WIDE_INT fieldpos;
5169 unsigned int align2;
5171 /* Output any buffered-up bit-fields preceding this element. */
5172 if (local->byte_buffer_in_use)
5174 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5175 local->total_bytes++;
5176 local->byte_buffer_in_use = false;
5179 if (local->index != NULL_TREE)
5181 /* Perform the index calculation in modulo arithmetic but
5182 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5183 but we are using an unsigned sizetype. */
5184 unsigned prec = TYPE_PRECISION (sizetype);
5185 offset_int idx = wi::sext (wi::to_offset (local->index)
5186 - wi::to_offset (local->min_index), prec);
5187 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5188 .to_short_addr ();
5190 else if (local->field != NULL_TREE)
5191 fieldpos = int_byte_position (local->field);
5192 else
5193 fieldpos = 0;
5195 /* Advance to offset of this element.
5196 Note no alignment needed in an array, since that is guaranteed
5197 if each element has the proper size. */
5198 if (local->field != NULL_TREE || local->index != NULL_TREE)
5200 if (fieldpos > local->total_bytes)
5202 assemble_zeros (fieldpos - local->total_bytes);
5203 local->total_bytes = fieldpos;
5205 else
5206 /* Must not go backwards. */
5207 gcc_assert (fieldpos == local->total_bytes);
5210 /* Find the alignment of this element. */
5211 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5213 /* Determine size this element should occupy. */
5214 if (local->field)
5216 fieldsize = 0;
5218 /* If this is an array with an unspecified upper bound,
5219 the initializer determines the size. */
5220 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5221 but we cannot do this until the deprecated support for
5222 initializing zero-length array members is removed. */
5223 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5224 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5225 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5227 fieldsize = array_size_for_constructor (local->val);
5228 /* Given a non-empty initialization, this field had better
5229 be last. Given a flexible array member, the next field
5230 on the chain is a TYPE_DECL of the enclosing struct. */
5231 const_tree next = DECL_CHAIN (local->field);
5232 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5233 tree size = TYPE_SIZE_UNIT (TREE_TYPE (local->val));
5234 gcc_checking_assert (compare_tree_int (size, fieldsize) == 0);
5236 else
5237 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5239 else
5240 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5242 /* Output the element's initial value. */
5243 if (local->val == NULL_TREE)
5244 assemble_zeros (fieldsize);
5245 else
5246 fieldsize = output_constant (local->val, fieldsize, align2,
5247 local->reverse, false);
5249 /* Count its size. */
5250 local->total_bytes += fieldsize;
5253 /* Helper for output_constructor. From the LOCAL state, output an element
5254 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5255 from the start of a possibly ongoing outer byte buffer. */
5257 static void
5258 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5260 /* Bit size of this element. */
5261 HOST_WIDE_INT ebitsize
5262 = (local->field
5263 ? tree_to_uhwi (DECL_SIZE (local->field))
5264 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5266 /* Relative index of this element if this is an array component. */
5267 HOST_WIDE_INT relative_index
5268 = (!local->field
5269 ? (local->index
5270 ? (tree_to_shwi (local->index)
5271 - tree_to_shwi (local->min_index))
5272 : local->last_relative_index + 1)
5273 : 0);
5275 /* Bit position of this element from the start of the containing
5276 constructor. */
5277 HOST_WIDE_INT constructor_relative_ebitpos
5278 = (local->field
5279 ? int_bit_position (local->field)
5280 : ebitsize * relative_index);
5282 /* Bit position of this element from the start of a possibly ongoing
5283 outer byte buffer. */
5284 HOST_WIDE_INT byte_relative_ebitpos
5285 = bit_offset + constructor_relative_ebitpos;
5287 /* From the start of a possibly ongoing outer byte buffer, offsets to
5288 the first bit of this element and to the first bit past the end of
5289 this element. */
5290 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5291 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5293 local->last_relative_index = relative_index;
5295 if (local->val == NULL_TREE)
5296 local->val = integer_zero_node;
5298 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5299 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5300 local->val = TREE_OPERAND (local->val, 0);
5302 if (TREE_CODE (local->val) != INTEGER_CST
5303 && TREE_CODE (local->val) != CONSTRUCTOR)
5305 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5306 return;
5309 /* If this field does not start in this (or next) byte, skip some bytes. */
5310 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5312 /* Output remnant of any bit field in previous bytes. */
5313 if (local->byte_buffer_in_use)
5315 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5316 local->total_bytes++;
5317 local->byte_buffer_in_use = false;
5320 /* If still not at proper byte, advance to there. */
5321 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5323 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5324 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5325 local->total_bytes = next_offset / BITS_PER_UNIT;
5329 /* Set up the buffer if necessary. */
5330 if (!local->byte_buffer_in_use)
5332 local->byte = 0;
5333 if (ebitsize > 0)
5334 local->byte_buffer_in_use = true;
5337 /* If this is nested constructor, recurse passing the bit offset and the
5338 pending data, then retrieve the new pending data afterwards. */
5339 if (TREE_CODE (local->val) == CONSTRUCTOR)
5341 oc_outer_state temp_state;
5342 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5343 temp_state.byte = local->byte;
5344 local->total_bytes
5345 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5346 local->byte = temp_state.byte;
5347 return;
5350 /* Otherwise, we must split the element into pieces that fall within
5351 separate bytes, and combine each byte with previous or following
5352 bit-fields. */
5353 while (next_offset < end_offset)
5355 int this_time;
5356 int shift;
5357 unsigned HOST_WIDE_INT value;
5358 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5359 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5361 /* Advance from byte to byte within this element when necessary. */
5362 while (next_byte != local->total_bytes)
5364 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5365 local->total_bytes++;
5366 local->byte = 0;
5369 /* Number of bits we can process at once (all part of the same byte). */
5370 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5371 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5373 /* For big-endian data, take the most significant bits (of the
5374 bits that are significant) first and put them into bytes from
5375 the most significant end. */
5376 shift = end_offset - next_offset - this_time;
5378 /* Don't try to take a bunch of bits that cross
5379 the word boundary in the INTEGER_CST. We can
5380 only select bits from one element. */
5381 if ((shift / HOST_BITS_PER_WIDE_INT)
5382 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5384 const int end = shift + this_time - 1;
5385 shift = end & -HOST_BITS_PER_WIDE_INT;
5386 this_time = end - shift + 1;
5389 /* Now get the bits we want to insert. */
5390 value = wi::extract_uhwi (wi::to_widest (local->val),
5391 shift, this_time);
5393 /* Get the result. This works only when:
5394 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5395 local->byte |= value << (BITS_PER_UNIT - this_time - next_bit);
5397 else
5399 /* On little-endian machines, take the least significant bits of
5400 the value first and pack them starting at the least significant
5401 bits of the bytes. */
5402 shift = next_offset - byte_relative_ebitpos;
5404 /* Don't try to take a bunch of bits that cross
5405 the word boundary in the INTEGER_CST. We can
5406 only select bits from one element. */
5407 if ((shift / HOST_BITS_PER_WIDE_INT)
5408 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5409 this_time
5410 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5412 /* Now get the bits we want to insert. */
5413 value = wi::extract_uhwi (wi::to_widest (local->val),
5414 shift, this_time);
5416 /* Get the result. This works only when:
5417 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5418 local->byte |= value << next_bit;
5421 next_offset += this_time;
5422 local->byte_buffer_in_use = true;
5426 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5427 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5428 caller output state of relevance in recursive invocations. */
5430 static unsigned HOST_WIDE_INT
5431 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5432 bool reverse, oc_outer_state *outer)
5434 unsigned HOST_WIDE_INT cnt;
5435 constructor_elt *ce;
5436 oc_local_state local;
5438 /* Setup our local state to communicate with helpers. */
5439 local.exp = exp;
5440 local.type = TREE_TYPE (exp);
5441 local.size = size;
5442 local.align = align;
5443 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5444 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5445 else
5446 local.min_index = integer_zero_node;
5448 local.total_bytes = 0;
5449 local.byte_buffer_in_use = outer != NULL;
5450 local.byte = outer ? outer->byte : 0;
5451 local.last_relative_index = -1;
5452 /* The storage order is specified for every aggregate type. */
5453 if (AGGREGATE_TYPE_P (local.type))
5454 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5455 else
5456 local.reverse = reverse;
5458 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5460 /* As CE goes through the elements of the constant, FIELD goes through the
5461 structure fields if the constant is a structure. If the constant is a
5462 union, we override this by getting the field from the TREE_LIST element.
5463 But the constant could also be an array. Then FIELD is zero.
5465 There is always a maximum of one element in the chain LINK for unions
5466 (even if the initializer in a source program incorrectly contains
5467 more one). */
5469 if (TREE_CODE (local.type) == RECORD_TYPE)
5470 local.field = TYPE_FIELDS (local.type);
5471 else
5472 local.field = NULL_TREE;
5474 for (cnt = 0;
5475 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5476 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5478 local.val = ce->value;
5479 local.index = NULL_TREE;
5481 /* The element in a union constructor specifies the proper field
5482 or index. */
5483 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5484 local.field = ce->index;
5486 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5487 local.index = ce->index;
5489 if (local.field && flag_verbose_asm)
5490 fprintf (asm_out_file, "%s %s:\n",
5491 ASM_COMMENT_START,
5492 DECL_NAME (local.field)
5493 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5494 : "<anonymous>");
5496 /* Eliminate the marker that makes a cast not be an lvalue. */
5497 if (local.val != NULL_TREE)
5498 STRIP_NOPS (local.val);
5500 /* Output the current element, using the appropriate helper ... */
5502 /* For an array slice not part of an outer bitfield. */
5503 if (!outer
5504 && local.index != NULL_TREE
5505 && TREE_CODE (local.index) == RANGE_EXPR)
5506 output_constructor_array_range (&local);
5508 /* For a field that is neither a true bitfield nor part of an outer one,
5509 known to be at least byte aligned and multiple-of-bytes long. */
5510 else if (!outer
5511 && (local.field == NULL_TREE
5512 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5513 output_constructor_regular_field (&local);
5515 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5516 supported for scalar fields, so we may need to convert first. */
5517 else
5519 if (TREE_CODE (local.val) == REAL_CST)
5520 local.val
5521 = fold_unary (VIEW_CONVERT_EXPR,
5522 build_nonstandard_integer_type
5523 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5524 local.val);
5525 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5529 /* If we are not at toplevel, save the pending data for our caller.
5530 Otherwise output the pending data and padding zeros as needed. */
5531 if (outer)
5532 outer->byte = local.byte;
5533 else
5535 if (local.byte_buffer_in_use)
5537 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5538 local.total_bytes++;
5541 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5543 assemble_zeros (local.size - local.total_bytes);
5544 local.total_bytes = local.size;
5548 return local.total_bytes;
5551 /* Mark DECL as weak. */
5553 static void
5554 mark_weak (tree decl)
5556 if (DECL_WEAK (decl))
5557 return;
5559 struct symtab_node *n = symtab_node::get (decl);
5560 if (n && n->refuse_visibility_changes)
5561 error ("%+qD declared weak after being used", decl);
5562 DECL_WEAK (decl) = 1;
5564 if (DECL_RTL_SET_P (decl)
5565 && MEM_P (DECL_RTL (decl))
5566 && XEXP (DECL_RTL (decl), 0)
5567 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5568 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5571 /* Merge weak status between NEWDECL and OLDDECL. */
5573 void
5574 merge_weak (tree newdecl, tree olddecl)
5576 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5578 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5580 tree *pwd;
5581 /* We put the NEWDECL on the weak_decls list at some point
5582 and OLDDECL as well. Keep just OLDDECL on the list. */
5583 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5584 if (TREE_VALUE (*pwd) == newdecl)
5586 *pwd = TREE_CHAIN (*pwd);
5587 break;
5590 return;
5593 if (DECL_WEAK (newdecl))
5595 tree wd;
5597 /* NEWDECL is weak, but OLDDECL is not. */
5599 /* If we already output the OLDDECL, we're in trouble; we can't
5600 go back and make it weak. This should never happen in
5601 unit-at-a-time compilation. */
5602 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5604 /* If we've already generated rtl referencing OLDDECL, we may
5605 have done so in a way that will not function properly with
5606 a weak symbol. Again in unit-at-a-time this should be
5607 impossible. */
5608 gcc_assert (!TREE_USED (olddecl)
5609 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5611 /* PR 49899: You cannot convert a static function into a weak, public function. */
5612 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
5613 error ("weak declaration of %q+D being applied to a already "
5614 "existing, static definition", newdecl);
5616 if (TARGET_SUPPORTS_WEAK)
5618 /* We put the NEWDECL on the weak_decls list at some point.
5619 Replace it with the OLDDECL. */
5620 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5621 if (TREE_VALUE (wd) == newdecl)
5623 TREE_VALUE (wd) = olddecl;
5624 break;
5626 /* We may not find the entry on the list. If NEWDECL is a
5627 weak alias, then we will have already called
5628 globalize_decl to remove the entry; in that case, we do
5629 not need to do anything. */
5632 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5633 mark_weak (olddecl);
5635 else
5636 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5637 weak. Just update NEWDECL to indicate that it's weak too. */
5638 mark_weak (newdecl);
5641 /* Declare DECL to be a weak symbol. */
5643 void
5644 declare_weak (tree decl)
5646 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5647 if (! TREE_PUBLIC (decl))
5649 error ("weak declaration of %q+D must be public", decl);
5650 return;
5652 else if (!TARGET_SUPPORTS_WEAK)
5653 warning (0, "weak declaration of %q+D not supported", decl);
5655 mark_weak (decl);
5656 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5657 DECL_ATTRIBUTES (decl)
5658 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5661 static void
5662 weak_finish_1 (tree decl)
5664 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5665 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5666 #endif
5668 if (! TREE_USED (decl))
5669 return;
5671 #ifdef ASM_WEAKEN_DECL
5672 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5673 #else
5674 #ifdef ASM_WEAKEN_LABEL
5675 ASM_WEAKEN_LABEL (asm_out_file, name);
5676 #else
5677 #ifdef ASM_OUTPUT_WEAK_ALIAS
5679 static bool warn_once = 0;
5680 if (! warn_once)
5682 warning (0, "only weak aliases are supported in this configuration");
5683 warn_once = 1;
5685 return;
5687 #endif
5688 #endif
5689 #endif
5692 /* Fiven an assembly name, find the decl it is associated with. */
5693 static tree
5694 find_decl (tree target)
5696 symtab_node *node = symtab_node::get_for_asmname (target);
5697 if (node)
5698 return node->decl;
5699 return NULL_TREE;
5702 /* This TREE_LIST contains weakref targets. */
5704 static GTY(()) tree weakref_targets;
5706 /* Emit any pending weak declarations. */
5708 void
5709 weak_finish (void)
5711 tree t;
5713 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5715 tree alias_decl = TREE_PURPOSE (t);
5716 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5718 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
5719 || TREE_SYMBOL_REFERENCED (target))
5720 /* Remove alias_decl from the weak list, but leave entries for
5721 the target alone. */
5722 target = NULL_TREE;
5723 #ifndef ASM_OUTPUT_WEAKREF
5724 else if (! TREE_SYMBOL_REFERENCED (target))
5726 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5727 defined, otherwise we and weak_finish_1 would use
5728 different macros. */
5729 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5730 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5731 # else
5732 tree decl = find_decl (target);
5734 if (! decl)
5736 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5737 TREE_CODE (alias_decl), target,
5738 TREE_TYPE (alias_decl));
5740 DECL_EXTERNAL (decl) = 1;
5741 TREE_PUBLIC (decl) = 1;
5742 DECL_ARTIFICIAL (decl) = 1;
5743 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5744 TREE_USED (decl) = 1;
5747 weak_finish_1 (decl);
5748 # endif
5750 #endif
5753 tree *p;
5754 tree t2;
5756 /* Remove the alias and the target from the pending weak list
5757 so that we do not emit any .weak directives for the former,
5758 nor multiple .weak directives for the latter. */
5759 for (p = &weak_decls; (t2 = *p) ; )
5761 if (TREE_VALUE (t2) == alias_decl
5762 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5763 *p = TREE_CHAIN (t2);
5764 else
5765 p = &TREE_CHAIN (t2);
5768 /* Remove other weakrefs to the same target, to speed things up. */
5769 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5771 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5772 *p = TREE_CHAIN (t2);
5773 else
5774 p = &TREE_CHAIN (t2);
5779 for (t = weak_decls; t; t = TREE_CHAIN (t))
5781 tree decl = TREE_VALUE (t);
5783 weak_finish_1 (decl);
5787 /* Emit the assembly bits to indicate that DECL is globally visible. */
5789 static void
5790 globalize_decl (tree decl)
5793 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5794 if (DECL_WEAK (decl))
5796 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5797 tree *p, t;
5799 #ifdef ASM_WEAKEN_DECL
5800 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5801 #else
5802 ASM_WEAKEN_LABEL (asm_out_file, name);
5803 #endif
5805 /* Remove this function from the pending weak list so that
5806 we do not emit multiple .weak directives for it. */
5807 for (p = &weak_decls; (t = *p) ; )
5809 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5810 *p = TREE_CHAIN (t);
5811 else
5812 p = &TREE_CHAIN (t);
5815 /* Remove weakrefs to the same target from the pending weakref
5816 list, for the same reason. */
5817 for (p = &weakref_targets; (t = *p) ; )
5819 if (DECL_ASSEMBLER_NAME (decl)
5820 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5821 *p = TREE_CHAIN (t);
5822 else
5823 p = &TREE_CHAIN (t);
5826 return;
5828 #endif
5830 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5833 vec<alias_pair, va_gc> *alias_pairs;
5835 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5836 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5837 tree node is DECL to have the value of the tree node TARGET. */
5839 void
5840 do_assemble_alias (tree decl, tree target)
5842 tree id;
5844 /* Emulated TLS had better not get this var. */
5845 gcc_assert (!(!targetm.have_tls
5846 && VAR_P (decl)
5847 && DECL_THREAD_LOCAL_P (decl)));
5849 if (TREE_ASM_WRITTEN (decl))
5850 return;
5852 id = DECL_ASSEMBLER_NAME (decl);
5853 ultimate_transparent_alias_target (&id);
5854 ultimate_transparent_alias_target (&target);
5856 /* We must force creation of DECL_RTL for debug info generation, even though
5857 we don't use it here. */
5858 make_decl_rtl (decl);
5860 TREE_ASM_WRITTEN (decl) = 1;
5861 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5862 TREE_ASM_WRITTEN (id) = 1;
5864 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5866 if (!TREE_SYMBOL_REFERENCED (target))
5867 weakref_targets = tree_cons (decl, target, weakref_targets);
5869 #ifdef ASM_OUTPUT_WEAKREF
5870 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5871 IDENTIFIER_POINTER (id),
5872 IDENTIFIER_POINTER (target));
5873 #else
5874 if (!TARGET_SUPPORTS_WEAK)
5876 error_at (DECL_SOURCE_LOCATION (decl),
5877 "weakref is not supported in this configuration");
5878 return;
5880 #endif
5881 return;
5884 #ifdef ASM_OUTPUT_DEF
5885 tree orig_decl = decl;
5887 /* Make name accessible from other files, if appropriate. */
5889 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5891 globalize_decl (decl);
5892 maybe_assemble_visibility (decl);
5894 if (TREE_CODE (decl) == FUNCTION_DECL
5895 && cgraph_node::get (decl)->ifunc_resolver)
5897 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5898 if (targetm.has_ifunc_p ())
5899 ASM_OUTPUT_TYPE_DIRECTIVE
5900 (asm_out_file, IDENTIFIER_POINTER (id),
5901 IFUNC_ASM_TYPE);
5902 else
5903 #endif
5904 error_at (DECL_SOURCE_LOCATION (decl),
5905 "ifunc is not supported on this target");
5908 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5909 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5910 # else
5911 ASM_OUTPUT_DEF (asm_out_file,
5912 IDENTIFIER_POINTER (id),
5913 IDENTIFIER_POINTER (target));
5914 # endif
5915 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5917 const char *name;
5918 tree *p, t;
5920 name = IDENTIFIER_POINTER (id);
5921 # ifdef ASM_WEAKEN_DECL
5922 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5923 # else
5924 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5925 # endif
5926 /* Remove this function from the pending weak list so that
5927 we do not emit multiple .weak directives for it. */
5928 for (p = &weak_decls; (t = *p) ; )
5929 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5930 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5931 *p = TREE_CHAIN (t);
5932 else
5933 p = &TREE_CHAIN (t);
5935 /* Remove weakrefs to the same target from the pending weakref
5936 list, for the same reason. */
5937 for (p = &weakref_targets; (t = *p) ; )
5939 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5940 *p = TREE_CHAIN (t);
5941 else
5942 p = &TREE_CHAIN (t);
5945 #endif
5948 /* Emit an assembler directive to make the symbol for DECL an alias to
5949 the symbol for TARGET. */
5951 void
5952 assemble_alias (tree decl, tree target)
5954 tree target_decl;
5956 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5958 tree alias = DECL_ASSEMBLER_NAME (decl);
5960 ultimate_transparent_alias_target (&target);
5962 if (alias == target)
5963 error ("weakref %q+D ultimately targets itself", decl);
5964 if (TREE_PUBLIC (decl))
5965 error ("weakref %q+D must have static linkage", decl);
5967 else
5969 #if !defined (ASM_OUTPUT_DEF)
5970 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5971 error_at (DECL_SOURCE_LOCATION (decl),
5972 "alias definitions not supported in this configuration");
5973 TREE_ASM_WRITTEN (decl) = 1;
5974 return;
5975 # else
5976 if (!DECL_WEAK (decl))
5978 /* NB: ifunc_resolver isn't set when an error is detected. */
5979 if (TREE_CODE (decl) == FUNCTION_DECL
5980 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5981 error_at (DECL_SOURCE_LOCATION (decl),
5982 "ifunc is not supported in this configuration");
5983 else
5984 error_at (DECL_SOURCE_LOCATION (decl),
5985 "only weak aliases are supported in this configuration");
5986 TREE_ASM_WRITTEN (decl) = 1;
5987 return;
5989 # endif
5990 #endif
5992 TREE_USED (decl) = 1;
5994 /* Allow aliases to aliases. */
5995 if (TREE_CODE (decl) == FUNCTION_DECL)
5996 cgraph_node::get_create (decl)->alias = true;
5997 else
5998 varpool_node::get_create (decl)->alias = true;
6000 /* If the target has already been emitted, we don't have to queue the
6001 alias. This saves a tad of memory. */
6002 if (symtab->global_info_ready)
6003 target_decl = find_decl (target);
6004 else
6005 target_decl= NULL;
6006 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6007 || symtab->state >= EXPANSION)
6008 do_assemble_alias (decl, target);
6009 else
6011 alias_pair p = {decl, target};
6012 vec_safe_push (alias_pairs, p);
6016 /* Record and output a table of translations from original function
6017 to its transaction aware clone. Note that tm_pure functions are
6018 considered to be their own clone. */
6020 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6022 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6023 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6025 static int
6026 keep_cache_entry (tree_map *&e)
6028 return ggc_marked_p (e->base.from);
6032 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6034 void
6035 record_tm_clone_pair (tree o, tree n)
6037 struct tree_map **slot, *h;
6039 if (tm_clone_hash == NULL)
6040 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6042 h = ggc_alloc<tree_map> ();
6043 h->hash = htab_hash_pointer (o);
6044 h->base.from = o;
6045 h->to = n;
6047 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6048 *slot = h;
6051 tree
6052 get_tm_clone_pair (tree o)
6054 if (tm_clone_hash)
6056 struct tree_map *h, in;
6058 in.base.from = o;
6059 in.hash = htab_hash_pointer (o);
6060 h = tm_clone_hash->find_with_hash (&in, in.hash);
6061 if (h)
6062 return h->to;
6064 return NULL_TREE;
6067 struct tm_alias_pair
6069 unsigned int uid;
6070 tree from;
6071 tree to;
6075 /* Dump the actual pairs to the .tm_clone_table section. */
6077 static void
6078 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6080 unsigned i;
6081 tm_alias_pair *p;
6082 bool switched = false;
6084 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6086 tree src = p->from;
6087 tree dst = p->to;
6088 struct cgraph_node *src_n = cgraph_node::get (src);
6089 struct cgraph_node *dst_n = cgraph_node::get (dst);
6091 /* The function ipa_tm_create_version() marks the clone as needed if
6092 the original function was needed. But we also mark the clone as
6093 needed if we ever called the clone indirectly through
6094 TM_GETTMCLONE. If neither of these are true, we didn't generate
6095 a clone, and we didn't call it indirectly... no sense keeping it
6096 in the clone table. */
6097 if (!dst_n || !dst_n->definition)
6098 continue;
6100 /* This covers the case where we have optimized the original
6101 function away, and only access the transactional clone. */
6102 if (!src_n || !src_n->definition)
6103 continue;
6105 if (!switched)
6107 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6108 assemble_align (POINTER_SIZE);
6109 switched = true;
6112 assemble_integer (XEXP (DECL_RTL (src), 0),
6113 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6114 assemble_integer (XEXP (DECL_RTL (dst), 0),
6115 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6119 /* Provide a default for the tm_clone_table section. */
6121 section *
6122 default_clone_table_section (void)
6124 return get_named_section (NULL, ".tm_clone_table", 3);
6127 /* Helper comparison function for qsorting by the DECL_UID stored in
6128 alias_pair->emitted_diags. */
6130 static int
6131 tm_alias_pair_cmp (const void *x, const void *y)
6133 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6134 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6135 if (p1->uid < p2->uid)
6136 return -1;
6137 if (p1->uid > p2->uid)
6138 return 1;
6139 return 0;
6142 void
6143 finish_tm_clone_pairs (void)
6145 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6147 if (tm_clone_hash == NULL)
6148 return;
6150 /* We need a determenistic order for the .tm_clone_table, otherwise
6151 we will get bootstrap comparison failures, so dump the hash table
6152 to a vector, sort it, and dump the vector. */
6154 /* Dump the hashtable to a vector. */
6155 tree_map *map;
6156 hash_table<tm_clone_hasher>::iterator iter;
6157 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6159 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6160 tm_alias_pairs.safe_push (p);
6162 /* Sort it. */
6163 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6165 /* Dump it. */
6166 dump_tm_clone_pairs (tm_alias_pairs);
6168 tm_clone_hash->empty ();
6169 tm_clone_hash = NULL;
6170 tm_alias_pairs.release ();
6174 /* Emit an assembler directive to set symbol for DECL visibility to
6175 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6177 void
6178 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6179 int vis ATTRIBUTE_UNUSED)
6181 #ifdef HAVE_GAS_HIDDEN
6182 static const char * const visibility_types[] = {
6183 NULL, "protected", "hidden", "internal"
6186 const char *name, *type;
6187 tree id;
6189 id = DECL_ASSEMBLER_NAME (decl);
6190 ultimate_transparent_alias_target (&id);
6191 name = IDENTIFIER_POINTER (id);
6193 type = visibility_types[vis];
6195 fprintf (asm_out_file, "\t.%s\t", type);
6196 assemble_name (asm_out_file, name);
6197 fprintf (asm_out_file, "\n");
6198 #else
6199 if (!DECL_ARTIFICIAL (decl))
6200 warning (OPT_Wattributes, "visibility attribute not supported "
6201 "in this configuration; ignored");
6202 #endif
6205 /* A helper function to call assemble_visibility when needed for a decl. */
6208 maybe_assemble_visibility (tree decl)
6210 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6211 if (vis != VISIBILITY_DEFAULT)
6213 targetm.asm_out.assemble_visibility (decl, vis);
6214 return 1;
6216 else
6217 return 0;
6220 /* Returns 1 if the target configuration supports defining public symbols
6221 so that one of them will be chosen at link time instead of generating a
6222 multiply-defined symbol error, whether through the use of weak symbols or
6223 a target-specific mechanism for having duplicates discarded. */
6226 supports_one_only (void)
6228 if (SUPPORTS_ONE_ONLY)
6229 return 1;
6230 return TARGET_SUPPORTS_WEAK;
6233 /* Set up DECL as a public symbol that can be defined in multiple
6234 translation units without generating a linker error. */
6236 void
6237 make_decl_one_only (tree decl, tree comdat_group)
6239 struct symtab_node *symbol;
6240 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6242 TREE_PUBLIC (decl) = 1;
6244 if (VAR_P (decl))
6245 symbol = varpool_node::get_create (decl);
6246 else
6247 symbol = cgraph_node::get_create (decl);
6249 if (SUPPORTS_ONE_ONLY)
6251 #ifdef MAKE_DECL_ONE_ONLY
6252 MAKE_DECL_ONE_ONLY (decl);
6253 #endif
6254 symbol->set_comdat_group (comdat_group);
6256 else if (VAR_P (decl)
6257 && (DECL_INITIAL (decl) == 0
6258 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6259 DECL_COMMON (decl) = 1;
6260 else
6262 gcc_assert (TARGET_SUPPORTS_WEAK);
6263 DECL_WEAK (decl) = 1;
6267 void
6268 init_varasm_once (void)
6270 section_htab = hash_table<section_hasher>::create_ggc (31);
6271 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6272 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6274 shared_constant_pool = create_constant_pool ();
6276 #ifdef TEXT_SECTION_ASM_OP
6277 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6278 TEXT_SECTION_ASM_OP);
6279 #endif
6281 #ifdef DATA_SECTION_ASM_OP
6282 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6283 DATA_SECTION_ASM_OP);
6284 #endif
6286 #ifdef SDATA_SECTION_ASM_OP
6287 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6288 SDATA_SECTION_ASM_OP);
6289 #endif
6291 #ifdef READONLY_DATA_SECTION_ASM_OP
6292 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6293 READONLY_DATA_SECTION_ASM_OP);
6294 #endif
6296 #ifdef CTORS_SECTION_ASM_OP
6297 ctors_section = get_unnamed_section (0, output_section_asm_op,
6298 CTORS_SECTION_ASM_OP);
6299 #endif
6301 #ifdef DTORS_SECTION_ASM_OP
6302 dtors_section = get_unnamed_section (0, output_section_asm_op,
6303 DTORS_SECTION_ASM_OP);
6304 #endif
6306 #ifdef BSS_SECTION_ASM_OP
6307 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6308 output_section_asm_op,
6309 BSS_SECTION_ASM_OP);
6310 #endif
6312 #ifdef SBSS_SECTION_ASM_OP
6313 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6314 output_section_asm_op,
6315 SBSS_SECTION_ASM_OP);
6316 #endif
6318 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6319 | SECTION_COMMON, emit_tls_common);
6320 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6321 | SECTION_COMMON, emit_local);
6322 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6323 | SECTION_COMMON, emit_common);
6325 #if defined ASM_OUTPUT_ALIGNED_BSS
6326 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6327 emit_bss);
6328 #endif
6330 targetm.asm_out.init_sections ();
6332 if (readonly_data_section == NULL)
6333 readonly_data_section = text_section;
6335 #ifdef ASM_OUTPUT_EXTERNAL
6336 pending_assemble_externals_set = new hash_set<tree>;
6337 #endif
6340 enum tls_model
6341 decl_default_tls_model (const_tree decl)
6343 enum tls_model kind;
6344 bool is_local;
6346 is_local = targetm.binds_local_p (decl);
6347 if (!flag_shlib)
6349 if (is_local)
6350 kind = TLS_MODEL_LOCAL_EXEC;
6351 else
6352 kind = TLS_MODEL_INITIAL_EXEC;
6355 /* Local dynamic is inefficient when we're not combining the
6356 parts of the address. */
6357 else if (optimize && is_local)
6358 kind = TLS_MODEL_LOCAL_DYNAMIC;
6359 else
6360 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6361 if (kind < flag_tls_default)
6362 kind = flag_tls_default;
6364 return kind;
6367 /* Select a set of attributes for section NAME based on the properties
6368 of DECL and whether or not RELOC indicates that DECL's initializer
6369 might contain runtime relocations.
6371 We make the section read-only and executable for a function decl,
6372 read-only for a const data decl, and writable for a non-const data decl. */
6374 unsigned int
6375 default_section_type_flags (tree decl, const char *name, int reloc)
6377 unsigned int flags;
6379 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6380 flags = SECTION_CODE;
6381 else if (decl)
6383 enum section_category category
6384 = categorize_decl_for_section (decl, reloc);
6385 if (decl_readonly_section_1 (category))
6386 flags = 0;
6387 else if (category == SECCAT_DATA_REL_RO
6388 || category == SECCAT_DATA_REL_RO_LOCAL)
6389 flags = SECTION_WRITE | SECTION_RELRO;
6390 else
6391 flags = SECTION_WRITE;
6393 else
6395 flags = SECTION_WRITE;
6396 if (strcmp (name, ".data.rel.ro") == 0
6397 || strcmp (name, ".data.rel.ro.local") == 0)
6398 flags |= SECTION_RELRO;
6401 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6402 flags |= SECTION_LINKONCE;
6404 if (strcmp (name, ".vtable_map_vars") == 0)
6405 flags |= SECTION_LINKONCE;
6407 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6408 flags |= SECTION_TLS | SECTION_WRITE;
6410 if (strcmp (name, ".bss") == 0
6411 || strncmp (name, ".bss.", 5) == 0
6412 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6413 || strcmp (name, ".persistent.bss") == 0
6414 || strcmp (name, ".sbss") == 0
6415 || strncmp (name, ".sbss.", 6) == 0
6416 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6417 flags |= SECTION_BSS;
6419 if (strcmp (name, ".tdata") == 0
6420 || strncmp (name, ".tdata.", 7) == 0
6421 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6422 flags |= SECTION_TLS;
6424 if (strcmp (name, ".tbss") == 0
6425 || strncmp (name, ".tbss.", 6) == 0
6426 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6427 flags |= SECTION_TLS | SECTION_BSS;
6429 /* Various sections have special ELF types that the assembler will
6430 assign by default based on the name. They are neither SHT_PROGBITS
6431 nor SHT_NOBITS, so when changing sections we don't want to print a
6432 section type (@progbits or @nobits). Rather than duplicating the
6433 assembler's knowledge of what those special name patterns are, just
6434 let the assembler choose the type if we don't know a specific
6435 reason to set it to something other than the default. SHT_PROGBITS
6436 is the default for sections whose name is not specially known to
6437 the assembler, so it does no harm to leave the choice to the
6438 assembler when @progbits is the best thing we know to use. If
6439 someone is silly enough to emit code or TLS variables to one of
6440 these sections, then don't handle them specially.
6442 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6443 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
6444 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6445 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6446 flags |= SECTION_NOTYPE;
6448 return flags;
6451 /* Return true if the target supports some form of global BSS,
6452 either through bss_noswitch_section, or by selecting a BSS
6453 section in TARGET_ASM_SELECT_SECTION. */
6455 bool
6456 have_global_bss_p (void)
6458 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6461 /* Output assembly to switch to section NAME with attribute FLAGS.
6462 Four variants for common object file formats. */
6464 void
6465 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6466 unsigned int flags ATTRIBUTE_UNUSED,
6467 tree decl ATTRIBUTE_UNUSED)
6469 /* Some object formats don't support named sections at all. The
6470 front-end should already have flagged this as an error. */
6471 gcc_unreachable ();
6474 #ifndef TLS_SECTION_ASM_FLAG
6475 #define TLS_SECTION_ASM_FLAG 'T'
6476 #endif
6478 void
6479 default_elf_asm_named_section (const char *name, unsigned int flags,
6480 tree decl)
6482 char flagchars[11], *f = flagchars;
6483 unsigned int numeric_value = 0;
6485 /* If we have already declared this section, we can use an
6486 abbreviated form to switch back to it -- unless this section is
6487 part of a COMDAT groups, in which case GAS requires the full
6488 declaration every time. */
6489 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6490 && (flags & SECTION_DECLARED))
6492 fprintf (asm_out_file, "\t.section\t%s\n", name);
6493 return;
6496 /* If we have a machine specific flag, then use the numeric value to pass
6497 this on to GAS. */
6498 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6499 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6500 else
6502 if (!(flags & SECTION_DEBUG))
6503 *f++ = 'a';
6504 #if HAVE_GAS_SECTION_EXCLUDE
6505 if (flags & SECTION_EXCLUDE)
6506 *f++ = 'e';
6507 #endif
6508 if (flags & SECTION_WRITE)
6509 *f++ = 'w';
6510 if (flags & SECTION_CODE)
6511 *f++ = 'x';
6512 if (flags & SECTION_SMALL)
6513 *f++ = 's';
6514 if (flags & SECTION_MERGE)
6515 *f++ = 'M';
6516 if (flags & SECTION_STRINGS)
6517 *f++ = 'S';
6518 if (flags & SECTION_TLS)
6519 *f++ = TLS_SECTION_ASM_FLAG;
6520 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6521 *f++ = 'G';
6522 #ifdef MACH_DEP_SECTION_ASM_FLAG
6523 if (flags & SECTION_MACH_DEP)
6524 *f++ = MACH_DEP_SECTION_ASM_FLAG;
6525 #endif
6526 *f = '\0';
6529 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6531 /* default_section_type_flags (above) knows which flags need special
6532 handling here, and sets NOTYPE when none of these apply so that the
6533 assembler's logic for default types can apply to user-chosen
6534 section names. */
6535 if (!(flags & SECTION_NOTYPE))
6537 const char *type;
6538 const char *format;
6540 if (flags & SECTION_BSS)
6541 type = "nobits";
6542 else
6543 type = "progbits";
6545 format = ",@%s";
6546 /* On platforms that use "@" as the assembly comment character,
6547 use "%" instead. */
6548 if (strcmp (ASM_COMMENT_START, "@") == 0)
6549 format = ",%%%s";
6550 fprintf (asm_out_file, format, type);
6552 if (flags & SECTION_ENTSIZE)
6553 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6554 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6556 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6557 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6558 else
6559 fprintf (asm_out_file, ",%s,comdat",
6560 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6564 putc ('\n', asm_out_file);
6567 void
6568 default_coff_asm_named_section (const char *name, unsigned int flags,
6569 tree decl ATTRIBUTE_UNUSED)
6571 char flagchars[8], *f = flagchars;
6573 if (flags & SECTION_WRITE)
6574 *f++ = 'w';
6575 if (flags & SECTION_CODE)
6576 *f++ = 'x';
6577 *f = '\0';
6579 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6582 void
6583 default_pe_asm_named_section (const char *name, unsigned int flags,
6584 tree decl)
6586 default_coff_asm_named_section (name, flags, decl);
6588 if (flags & SECTION_LINKONCE)
6590 /* Functions may have been compiled at various levels of
6591 optimization so we can't use `same_size' here.
6592 Instead, have the linker pick one. */
6593 fprintf (asm_out_file, "\t.linkonce %s\n",
6594 (flags & SECTION_CODE ? "discard" : "same_size"));
6598 /* The lame default section selector. */
6600 section *
6601 default_select_section (tree decl, int reloc,
6602 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6604 if (DECL_P (decl))
6606 if (decl_readonly_section (decl, reloc))
6607 return readonly_data_section;
6609 else if (TREE_CODE (decl) == CONSTRUCTOR)
6611 if (! ((flag_pic && reloc)
6612 || !TREE_READONLY (decl)
6613 || TREE_SIDE_EFFECTS (decl)
6614 || !TREE_CONSTANT (decl)))
6615 return readonly_data_section;
6617 else if (TREE_CODE (decl) == STRING_CST)
6618 return readonly_data_section;
6619 else if (! (flag_pic && reloc))
6620 return readonly_data_section;
6622 return data_section;
6625 enum section_category
6626 categorize_decl_for_section (const_tree decl, int reloc)
6628 enum section_category ret;
6630 if (TREE_CODE (decl) == FUNCTION_DECL)
6631 return SECCAT_TEXT;
6632 else if (TREE_CODE (decl) == STRING_CST)
6634 if ((flag_sanitize & SANITIZE_ADDRESS)
6635 && asan_protect_global (CONST_CAST_TREE (decl)))
6636 /* or !flag_merge_constants */
6637 return SECCAT_RODATA;
6638 else
6639 return SECCAT_RODATA_MERGE_STR;
6641 else if (VAR_P (decl))
6643 tree d = CONST_CAST_TREE (decl);
6644 if (bss_initializer_p (decl))
6645 ret = SECCAT_BSS;
6646 else if (! TREE_READONLY (decl)
6647 || TREE_SIDE_EFFECTS (decl)
6648 || (DECL_INITIAL (decl)
6649 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
6651 /* Here the reloc_rw_mask is not testing whether the section should
6652 be read-only or not, but whether the dynamic link will have to
6653 do something. If so, we wish to segregate the data in order to
6654 minimize cache misses inside the dynamic linker. */
6655 if (reloc & targetm.asm_out.reloc_rw_mask ())
6656 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6657 else
6658 ret = SECCAT_DATA;
6660 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6661 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6662 else if (reloc || flag_merge_constants < 2
6663 || ((flag_sanitize & SANITIZE_ADDRESS)
6664 /* PR 81697: for architectures that use section anchors we
6665 need to ignore DECL_RTL_SET_P (decl) for string constants
6666 inside this asan_protect_global call because otherwise
6667 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
6668 section, set DECL_RTL (decl) later on and add DECL to
6669 protected globals via successive asan_protect_global
6670 calls. In this scenario we'll end up with wrong
6671 alignment of these strings at runtime and possible ASan
6672 false positives. */
6673 && asan_protect_global (d, use_object_blocks_p ()
6674 && use_blocks_for_decl_p (d))))
6675 /* C and C++ don't allow different variables to share the same
6676 location. -fmerge-all-constants allows even that (at the
6677 expense of not conforming). */
6678 ret = SECCAT_RODATA;
6679 else if (DECL_INITIAL (decl)
6680 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6681 ret = SECCAT_RODATA_MERGE_STR_INIT;
6682 else
6683 ret = SECCAT_RODATA_MERGE_CONST;
6685 else if (TREE_CODE (decl) == CONSTRUCTOR)
6687 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6688 || TREE_SIDE_EFFECTS (decl)
6689 || ! TREE_CONSTANT (decl))
6690 ret = SECCAT_DATA;
6691 else
6692 ret = SECCAT_RODATA;
6694 else
6695 ret = SECCAT_RODATA;
6697 /* There are no read-only thread-local sections. */
6698 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6700 /* Note that this would be *just* SECCAT_BSS, except that there's
6701 no concept of a read-only thread-local-data section. */
6702 if (ret == SECCAT_BSS
6703 || DECL_INITIAL (decl) == NULL
6704 || (flag_zero_initialized_in_bss
6705 && initializer_zerop (DECL_INITIAL (decl))))
6706 ret = SECCAT_TBSS;
6707 else
6708 ret = SECCAT_TDATA;
6711 /* If the target uses small data sections, select it. */
6712 else if (targetm.in_small_data_p (decl))
6714 if (ret == SECCAT_BSS)
6715 ret = SECCAT_SBSS;
6716 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6717 ret = SECCAT_SRODATA;
6718 else
6719 ret = SECCAT_SDATA;
6722 return ret;
6725 static bool
6726 decl_readonly_section_1 (enum section_category category)
6728 switch (category)
6730 case SECCAT_RODATA:
6731 case SECCAT_RODATA_MERGE_STR:
6732 case SECCAT_RODATA_MERGE_STR_INIT:
6733 case SECCAT_RODATA_MERGE_CONST:
6734 case SECCAT_SRODATA:
6735 return true;
6736 default:
6737 return false;
6741 bool
6742 decl_readonly_section (const_tree decl, int reloc)
6744 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6747 /* Select a section based on the above categorization. */
6749 section *
6750 default_elf_select_section (tree decl, int reloc,
6751 unsigned HOST_WIDE_INT align)
6753 const char *sname;
6754 switch (categorize_decl_for_section (decl, reloc))
6756 case SECCAT_TEXT:
6757 /* We're not supposed to be called on FUNCTION_DECLs. */
6758 gcc_unreachable ();
6759 case SECCAT_RODATA:
6760 return readonly_data_section;
6761 case SECCAT_RODATA_MERGE_STR:
6762 return mergeable_string_section (decl, align, 0);
6763 case SECCAT_RODATA_MERGE_STR_INIT:
6764 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6765 case SECCAT_RODATA_MERGE_CONST:
6766 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6767 case SECCAT_SRODATA:
6768 sname = ".sdata2";
6769 break;
6770 case SECCAT_DATA:
6771 return data_section;
6772 case SECCAT_DATA_REL:
6773 sname = ".data.rel";
6774 break;
6775 case SECCAT_DATA_REL_LOCAL:
6776 sname = ".data.rel.local";
6777 break;
6778 case SECCAT_DATA_REL_RO:
6779 sname = ".data.rel.ro";
6780 break;
6781 case SECCAT_DATA_REL_RO_LOCAL:
6782 sname = ".data.rel.ro.local";
6783 break;
6784 case SECCAT_SDATA:
6785 sname = ".sdata";
6786 break;
6787 case SECCAT_TDATA:
6788 sname = ".tdata";
6789 break;
6790 case SECCAT_BSS:
6791 if (bss_section)
6792 return bss_section;
6793 sname = ".bss";
6794 break;
6795 case SECCAT_SBSS:
6796 sname = ".sbss";
6797 break;
6798 case SECCAT_TBSS:
6799 sname = ".tbss";
6800 break;
6801 default:
6802 gcc_unreachable ();
6805 return get_named_section (decl, sname, reloc);
6808 /* Construct a unique section name based on the decl name and the
6809 categorization performed above. */
6811 void
6812 default_unique_section (tree decl, int reloc)
6814 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6815 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6816 const char *prefix, *name, *linkonce;
6817 char *string;
6818 tree id;
6820 switch (categorize_decl_for_section (decl, reloc))
6822 case SECCAT_TEXT:
6823 prefix = one_only ? ".t" : ".text";
6824 break;
6825 case SECCAT_RODATA:
6826 case SECCAT_RODATA_MERGE_STR:
6827 case SECCAT_RODATA_MERGE_STR_INIT:
6828 case SECCAT_RODATA_MERGE_CONST:
6829 prefix = one_only ? ".r" : ".rodata";
6830 break;
6831 case SECCAT_SRODATA:
6832 prefix = one_only ? ".s2" : ".sdata2";
6833 break;
6834 case SECCAT_DATA:
6835 prefix = one_only ? ".d" : ".data";
6836 break;
6837 case SECCAT_DATA_REL:
6838 prefix = one_only ? ".d.rel" : ".data.rel";
6839 break;
6840 case SECCAT_DATA_REL_LOCAL:
6841 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6842 break;
6843 case SECCAT_DATA_REL_RO:
6844 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6845 break;
6846 case SECCAT_DATA_REL_RO_LOCAL:
6847 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6848 break;
6849 case SECCAT_SDATA:
6850 prefix = one_only ? ".s" : ".sdata";
6851 break;
6852 case SECCAT_BSS:
6853 prefix = one_only ? ".b" : ".bss";
6854 break;
6855 case SECCAT_SBSS:
6856 prefix = one_only ? ".sb" : ".sbss";
6857 break;
6858 case SECCAT_TDATA:
6859 prefix = one_only ? ".td" : ".tdata";
6860 break;
6861 case SECCAT_TBSS:
6862 prefix = one_only ? ".tb" : ".tbss";
6863 break;
6864 default:
6865 gcc_unreachable ();
6868 id = DECL_ASSEMBLER_NAME (decl);
6869 ultimate_transparent_alias_target (&id);
6870 name = IDENTIFIER_POINTER (id);
6871 name = targetm.strip_name_encoding (name);
6873 /* If we're using one_only, then there needs to be a .gnu.linkonce
6874 prefix to the section name. */
6875 linkonce = one_only ? ".gnu.linkonce" : "";
6877 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6879 set_decl_section_name (decl, string);
6882 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6884 static int
6885 compute_reloc_for_rtx_1 (const_rtx x)
6887 switch (GET_CODE (x))
6889 case SYMBOL_REF:
6890 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6891 case LABEL_REF:
6892 return 1;
6893 default:
6894 return 0;
6898 /* Like compute_reloc_for_constant, except for an RTX. The return value
6899 is a mask for which bit 1 indicates a global relocation, and bit 0
6900 indicates a local relocation. */
6902 static int
6903 compute_reloc_for_rtx (const_rtx x)
6905 switch (GET_CODE (x))
6907 case SYMBOL_REF:
6908 case LABEL_REF:
6909 return compute_reloc_for_rtx_1 (x);
6911 case CONST:
6913 int reloc = 0;
6914 subrtx_iterator::array_type array;
6915 FOR_EACH_SUBRTX (iter, array, x, ALL)
6916 reloc |= compute_reloc_for_rtx_1 (*iter);
6917 return reloc;
6920 default:
6921 return 0;
6925 section *
6926 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6927 rtx x,
6928 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6930 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6931 return data_section;
6932 else
6933 return readonly_data_section;
6936 section *
6937 default_elf_select_rtx_section (machine_mode mode, rtx x,
6938 unsigned HOST_WIDE_INT align)
6940 int reloc = compute_reloc_for_rtx (x);
6942 /* ??? Handle small data here somehow. */
6944 if (reloc & targetm.asm_out.reloc_rw_mask ())
6946 if (reloc == 1)
6947 return get_named_section (NULL, ".data.rel.ro.local", 1);
6948 else
6949 return get_named_section (NULL, ".data.rel.ro", 3);
6952 return mergeable_constant_section (mode, align, 0);
6955 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6957 void
6958 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6960 rtx symbol;
6961 int flags;
6963 /* Careful not to prod global register variables. */
6964 if (!MEM_P (rtl))
6965 return;
6966 symbol = XEXP (rtl, 0);
6967 if (GET_CODE (symbol) != SYMBOL_REF)
6968 return;
6970 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6971 if (TREE_CODE (decl) == FUNCTION_DECL)
6972 flags |= SYMBOL_FLAG_FUNCTION;
6973 if (targetm.binds_local_p (decl))
6974 flags |= SYMBOL_FLAG_LOCAL;
6975 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6976 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6977 else if (targetm.in_small_data_p (decl))
6978 flags |= SYMBOL_FLAG_SMALL;
6979 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6980 being PUBLIC, the thing *must* be defined in this translation unit.
6981 Prevent this buglet from being propagated into rtl code as well. */
6982 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6983 flags |= SYMBOL_FLAG_EXTERNAL;
6985 SYMBOL_REF_FLAGS (symbol) = flags;
6988 /* By default, we do nothing for encode_section_info, so we need not
6989 do anything but discard the '*' marker. */
6991 const char *
6992 default_strip_name_encoding (const char *str)
6994 return str + (*str == '*');
6997 #ifdef ASM_OUTPUT_DEF
6998 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6999 anchor relative to ".", the current section position. */
7001 void
7002 default_asm_output_anchor (rtx symbol)
7004 char buffer[100];
7006 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7007 SYMBOL_REF_BLOCK_OFFSET (symbol));
7008 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7010 #endif
7012 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
7014 bool
7015 default_use_anchors_for_symbol_p (const_rtx symbol)
7017 tree decl;
7018 section *sect = SYMBOL_REF_BLOCK (symbol)->sect;
7020 /* This function should only be called with non-zero SYMBOL_REF_BLOCK,
7021 furthermore get_block_for_section should not create object blocks
7022 for mergeable sections. */
7023 gcc_checking_assert (sect && !(sect->common.flags & SECTION_MERGE));
7025 /* Don't use anchors for small data sections. The small data register
7026 acts as an anchor for such sections. */
7027 if (sect->common.flags & SECTION_SMALL)
7028 return false;
7030 decl = SYMBOL_REF_DECL (symbol);
7031 if (decl && DECL_P (decl))
7033 /* Don't use section anchors for decls that might be defined or
7034 usurped by other modules. */
7035 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
7036 return false;
7038 /* Don't use section anchors for decls that will be placed in a
7039 small data section. */
7040 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
7041 one above. The problem is that we only use SECTION_SMALL for
7042 sections that should be marked as small in the section directive. */
7043 if (targetm.in_small_data_p (decl))
7044 return false;
7046 /* Don't use section anchors for decls that won't fit inside a single
7047 anchor range to reduce the amount of instructions required to refer
7048 to the entire declaration. */
7049 if (DECL_SIZE_UNIT (decl) == NULL_TREE
7050 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
7051 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
7052 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
7053 return false;
7056 return true;
7059 /* Return true when RESOLUTION indicate that symbol will be bound to the
7060 definition provided by current .o file. */
7062 static bool
7063 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
7065 return (resolution == LDPR_PREVAILING_DEF
7066 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7067 || resolution == LDPR_PREVAILING_DEF_IRONLY);
7070 /* Return true when RESOLUTION indicate that symbol will be bound locally
7071 within current executable or DSO. */
7073 static bool
7074 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7076 return (resolution == LDPR_PREVAILING_DEF
7077 || resolution == LDPR_PREVAILING_DEF_IRONLY
7078 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7079 || resolution == LDPR_PREEMPTED_REG
7080 || resolution == LDPR_PREEMPTED_IR
7081 || resolution == LDPR_RESOLVED_IR
7082 || resolution == LDPR_RESOLVED_EXEC);
7085 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7086 uninitialized common symbol in the executable will still be defined
7087 (through COPY relocation) in the executable. */
7089 bool
7090 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7091 bool extern_protected_data, bool common_local_p)
7093 /* A non-decl is an entry in the constant pool. */
7094 if (!DECL_P (exp))
7095 return true;
7097 /* Weakrefs may not bind locally, even though the weakref itself is always
7098 static and therefore local. Similarly, the resolver for ifunc functions
7099 might resolve to a non-local function.
7100 FIXME: We can resolve the weakref case more curefuly by looking at the
7101 weakref alias. */
7102 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7103 || (TREE_CODE (exp) == FUNCTION_DECL
7104 && cgraph_node::get (exp)
7105 && cgraph_node::get (exp)->ifunc_resolver))
7106 return false;
7108 /* Static variables are always local. */
7109 if (! TREE_PUBLIC (exp))
7110 return true;
7112 /* With resolution file in hand, take look into resolutions.
7113 We can't just return true for resolved_locally symbols,
7114 because dynamic linking might overwrite symbols
7115 in shared libraries. */
7116 bool resolved_locally = false;
7118 bool uninited_common = (DECL_COMMON (exp)
7119 && (DECL_INITIAL (exp) == NULL
7120 || (!in_lto_p
7121 && DECL_INITIAL (exp) == error_mark_node)));
7123 /* A non-external variable is defined locally only if it isn't
7124 uninitialized COMMON variable or common_local_p is true. */
7125 bool defined_locally = (!DECL_EXTERNAL (exp)
7126 && (!uninited_common || common_local_p));
7127 if (symtab_node *node = symtab_node::get (exp))
7129 if (node->in_other_partition)
7130 defined_locally = true;
7131 if (node->can_be_discarded_p ())
7133 else if (resolution_to_local_definition_p (node->resolution))
7134 defined_locally = resolved_locally = true;
7135 else if (resolution_local_p (node->resolution))
7136 resolved_locally = true;
7138 if (defined_locally && weak_dominate && !shlib)
7139 resolved_locally = true;
7141 /* Undefined weak symbols are never defined locally. */
7142 if (DECL_WEAK (exp) && !defined_locally)
7143 return false;
7145 /* A symbol is local if the user has said explicitly that it will be,
7146 or if we have a definition for the symbol. We cannot infer visibility
7147 for undefined symbols. */
7148 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7149 && (TREE_CODE (exp) == FUNCTION_DECL
7150 || !extern_protected_data
7151 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7152 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7153 return true;
7155 /* If PIC, then assume that any global name can be overridden by
7156 symbols resolved from other modules. */
7157 if (shlib)
7158 return false;
7160 /* Variables defined outside this object might not be local. */
7161 if (DECL_EXTERNAL (exp) && !resolved_locally)
7162 return false;
7164 /* Non-dominant weak symbols are not defined locally. */
7165 if (DECL_WEAK (exp) && !resolved_locally)
7166 return false;
7168 /* Uninitialized COMMON variable may be unified with symbols
7169 resolved from other modules. */
7170 if (uninited_common && !resolved_locally)
7171 return false;
7173 /* Otherwise we're left with initialized (or non-common) global data
7174 which is of necessity defined locally. */
7175 return true;
7178 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7179 wrt cross-module name binding. */
7181 bool
7182 default_binds_local_p (const_tree exp)
7184 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7187 /* Similar to default_binds_local_p, but common symbol may be local and
7188 extern protected data is non-local. */
7190 bool
7191 default_binds_local_p_2 (const_tree exp)
7193 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7194 !flag_pic);
7197 bool
7198 default_binds_local_p_1 (const_tree exp, int shlib)
7200 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7203 /* Return true when references to DECL must bind to current definition in
7204 final executable.
7206 The condition is usually equivalent to whether the function binds to the
7207 current module (shared library or executable), that is to binds_local_p.
7208 We use this fact to avoid need for another target hook and implement
7209 the logic using binds_local_p and just special cases where
7210 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7211 the weak definitions (that can be overwritten at linktime by other
7212 definition from different object file) and when resolution info is available
7213 we simply use the knowledge passed to us by linker plugin. */
7214 bool
7215 decl_binds_to_current_def_p (const_tree decl)
7217 gcc_assert (DECL_P (decl));
7218 if (!targetm.binds_local_p (decl))
7219 return false;
7220 if (!TREE_PUBLIC (decl))
7221 return true;
7223 /* When resolution is available, just use it. */
7224 if (symtab_node *node = symtab_node::get (decl))
7226 if (node->resolution != LDPR_UNKNOWN
7227 && !node->can_be_discarded_p ())
7228 return resolution_to_local_definition_p (node->resolution);
7231 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7232 binds locally but still can be overwritten), DECL_COMMON (can be merged
7233 with a non-common definition somewhere in the same module) or
7234 DECL_EXTERNAL.
7235 This rely on fact that binds_local_p behave as decl_replaceable_p
7236 for all other declaration types. */
7237 if (DECL_WEAK (decl))
7238 return false;
7239 if (DECL_COMMON (decl)
7240 && (DECL_INITIAL (decl) == NULL
7241 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7242 return false;
7243 if (DECL_EXTERNAL (decl))
7244 return false;
7245 return true;
7248 /* A replaceable function or variable is one which may be replaced
7249 at link-time with an entirely different definition, provided that the
7250 replacement has the same type. For example, functions declared
7251 with __attribute__((weak)) on most systems are replaceable.
7253 COMDAT functions are not replaceable, since all definitions of the
7254 function must be equivalent. It is important that COMDAT functions
7255 not be treated as replaceable so that use of C++ template
7256 instantiations is not penalized. */
7258 bool
7259 decl_replaceable_p (tree decl)
7261 gcc_assert (DECL_P (decl));
7262 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7263 return false;
7264 if (!flag_semantic_interposition
7265 && !DECL_WEAK (decl))
7266 return false;
7267 return !decl_binds_to_current_def_p (decl);
7270 /* Default function to output code that will globalize a label. A
7271 target must define GLOBAL_ASM_OP or provide its own function to
7272 globalize a label. */
7273 #ifdef GLOBAL_ASM_OP
7274 void
7275 default_globalize_label (FILE * stream, const char *name)
7277 fputs (GLOBAL_ASM_OP, stream);
7278 assemble_name (stream, name);
7279 putc ('\n', stream);
7281 #endif /* GLOBAL_ASM_OP */
7283 /* Default function to output code that will globalize a declaration. */
7284 void
7285 default_globalize_decl_name (FILE * stream, tree decl)
7287 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7288 targetm.asm_out.globalize_label (stream, name);
7291 /* Default function to output a label for unwind information. The
7292 default is to do nothing. A target that needs nonlocal labels for
7293 unwind information must provide its own function to do this. */
7294 void
7295 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7296 tree decl ATTRIBUTE_UNUSED,
7297 int for_eh ATTRIBUTE_UNUSED,
7298 int empty ATTRIBUTE_UNUSED)
7302 /* Default function to output a label to divide up the exception table.
7303 The default is to do nothing. A target that needs/wants to divide
7304 up the table must provide it's own function to do this. */
7305 void
7306 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7310 /* This is how to output an internal numbered label where PREFIX is
7311 the class of label and LABELNO is the number within the class. */
7313 void
7314 default_generate_internal_label (char *buf, const char *prefix,
7315 unsigned long labelno)
7317 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7320 /* This is how to output an internal numbered label where PREFIX is
7321 the class of label and LABELNO is the number within the class. */
7323 void
7324 default_internal_label (FILE *stream, const char *prefix,
7325 unsigned long labelno)
7327 char *const buf = (char *) alloca (40 + strlen (prefix));
7328 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7329 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7333 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7335 void
7336 default_asm_declare_constant_name (FILE *file, const char *name,
7337 const_tree exp ATTRIBUTE_UNUSED,
7338 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7340 assemble_label (file, name);
7343 /* This is the default behavior at the beginning of a file. It's
7344 controlled by two other target-hook toggles. */
7345 void
7346 default_file_start (void)
7348 if (targetm.asm_file_start_app_off
7349 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7350 fputs (ASM_APP_OFF, asm_out_file);
7352 if (targetm.asm_file_start_file_directive)
7354 /* LTO produced units have no meaningful main_input_filename. */
7355 if (in_lto_p)
7356 output_file_directive (asm_out_file, "<artificial>");
7357 else
7358 output_file_directive (asm_out_file, main_input_filename);
7362 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7363 which emits a special section directive used to indicate whether or
7364 not this object file needs an executable stack. This is primarily
7365 a GNU extension to ELF but could be used on other targets. */
7367 int trampolines_created;
7369 void
7370 file_end_indicate_exec_stack (void)
7372 unsigned int flags = SECTION_DEBUG;
7373 if (trampolines_created)
7374 flags |= SECTION_CODE;
7376 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7379 /* Emit a special section directive to indicate that this object file
7380 was compiled with -fsplit-stack. This is used to let the linker
7381 detect calls between split-stack code and non-split-stack code, so
7382 that it can modify the split-stack code to allocate a sufficiently
7383 large stack. We emit another special section if there are any
7384 functions in this file which have the no_split_stack attribute, to
7385 prevent the linker from warning about being unable to convert the
7386 functions if they call non-split-stack code. */
7388 void
7389 file_end_indicate_split_stack (void)
7391 if (flag_split_stack)
7393 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7394 NULL));
7395 if (saw_no_split_stack)
7396 switch_to_section (get_section (".note.GNU-no-split-stack",
7397 SECTION_DEBUG, NULL));
7401 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7402 a get_unnamed_section callback. */
7404 void
7405 output_section_asm_op (const void *directive)
7407 fprintf (asm_out_file, "%s\n", (const char *) directive);
7410 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7411 the current section is NEW_SECTION. */
7413 void
7414 switch_to_section (section *new_section)
7416 if (in_section == new_section)
7417 return;
7419 if (new_section->common.flags & SECTION_FORGET)
7420 in_section = NULL;
7421 else
7422 in_section = new_section;
7424 switch (SECTION_STYLE (new_section))
7426 case SECTION_NAMED:
7427 targetm.asm_out.named_section (new_section->named.name,
7428 new_section->named.common.flags,
7429 new_section->named.decl);
7430 break;
7432 case SECTION_UNNAMED:
7433 new_section->unnamed.callback (new_section->unnamed.data);
7434 break;
7436 case SECTION_NOSWITCH:
7437 gcc_unreachable ();
7438 break;
7441 new_section->common.flags |= SECTION_DECLARED;
7444 /* If block symbol SYMBOL has not yet been assigned an offset, place
7445 it at the end of its block. */
7447 void
7448 place_block_symbol (rtx symbol)
7450 unsigned HOST_WIDE_INT size, mask, offset;
7451 struct constant_descriptor_rtx *desc;
7452 unsigned int alignment;
7453 struct object_block *block;
7454 tree decl;
7456 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7457 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7458 return;
7460 /* Work out the symbol's size and alignment. */
7461 if (CONSTANT_POOL_ADDRESS_P (symbol))
7463 desc = SYMBOL_REF_CONSTANT (symbol);
7464 alignment = desc->align;
7465 size = GET_MODE_SIZE (desc->mode);
7467 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7469 decl = SYMBOL_REF_DECL (symbol);
7470 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7471 alignment = DECL_ALIGN (decl);
7472 size = get_constant_size (DECL_INITIAL (decl));
7473 if ((flag_sanitize & SANITIZE_ADDRESS)
7474 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7475 && asan_protect_global (DECL_INITIAL (decl)))
7477 size += asan_red_zone_size (size);
7478 alignment = MAX (alignment,
7479 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7482 else
7484 struct symtab_node *snode;
7485 decl = SYMBOL_REF_DECL (symbol);
7487 snode = symtab_node::get (decl);
7488 if (snode->alias)
7490 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7492 gcc_assert (MEM_P (target)
7493 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7494 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7495 target = XEXP (target, 0);
7496 place_block_symbol (target);
7497 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7498 return;
7500 alignment = get_variable_align (decl);
7501 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7502 if ((flag_sanitize & SANITIZE_ADDRESS)
7503 && asan_protect_global (decl))
7505 size += asan_red_zone_size (size);
7506 alignment = MAX (alignment,
7507 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7511 /* Calculate the object's offset from the start of the block. */
7512 block = SYMBOL_REF_BLOCK (symbol);
7513 mask = alignment / BITS_PER_UNIT - 1;
7514 offset = (block->size + mask) & ~mask;
7515 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7517 /* Record the block's new alignment and size. */
7518 block->alignment = MAX (block->alignment, alignment);
7519 block->size = offset + size;
7521 vec_safe_push (block->objects, symbol);
7524 /* Return the anchor that should be used to address byte offset OFFSET
7525 from the first object in BLOCK. MODEL is the TLS model used
7526 to access it. */
7529 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7530 enum tls_model model)
7532 char label[100];
7533 unsigned int begin, middle, end;
7534 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7535 rtx anchor;
7537 /* Work out the anchor's offset. Use an offset of 0 for the first
7538 anchor so that we don't pessimize the case where we take the address
7539 of a variable at the beginning of the block. This is particularly
7540 useful when a block has only one variable assigned to it.
7542 We try to place anchors RANGE bytes apart, so there can then be
7543 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7544 a ptr_mode offset. With some target settings, the lowest such
7545 anchor might be out of range for the lowest ptr_mode offset;
7546 likewise the highest anchor for the highest offset. Use anchors
7547 at the extreme ends of the ptr_mode range in such cases.
7549 All arithmetic uses unsigned integers in order to avoid
7550 signed overflow. */
7551 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7552 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7553 range = max_offset - min_offset + 1;
7554 if (range == 0)
7555 offset = 0;
7556 else
7558 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7559 if (offset < 0)
7561 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7562 delta -= delta % range;
7563 if (delta > bias)
7564 delta = bias;
7565 offset = (HOST_WIDE_INT) (-delta);
7567 else
7569 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7570 delta -= delta % range;
7571 if (delta > bias - 1)
7572 delta = bias - 1;
7573 offset = (HOST_WIDE_INT) delta;
7577 /* Do a binary search to see if there's already an anchor we can use.
7578 Set BEGIN to the new anchor's index if not. */
7579 begin = 0;
7580 end = vec_safe_length (block->anchors);
7581 while (begin != end)
7583 middle = (end + begin) / 2;
7584 anchor = (*block->anchors)[middle];
7585 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7586 end = middle;
7587 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7588 begin = middle + 1;
7589 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7590 end = middle;
7591 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7592 begin = middle + 1;
7593 else
7594 return anchor;
7597 /* Create a new anchor with a unique label. */
7598 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7599 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7600 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7601 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7603 /* Insert it at index BEGIN. */
7604 vec_safe_insert (block->anchors, begin, anchor);
7605 return anchor;
7608 /* Output the objects in BLOCK. */
7610 static void
7611 output_object_block (struct object_block *block)
7613 struct constant_descriptor_rtx *desc;
7614 unsigned int i;
7615 HOST_WIDE_INT offset;
7616 tree decl;
7617 rtx symbol;
7619 if (!block->objects)
7620 return;
7622 /* Switch to the section and make sure that the first byte is
7623 suitably aligned. */
7624 /* Special case VTV comdat sections similar to assemble_variable. */
7625 if (SECTION_STYLE (block->sect) == SECTION_NAMED
7626 && block->sect->named.name
7627 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
7628 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
7629 else
7630 switch_to_section (block->sect);
7632 gcc_checking_assert (!(block->sect->common.flags & SECTION_MERGE));
7633 assemble_align (block->alignment);
7635 /* Define the values of all anchors relative to the current section
7636 position. */
7637 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7638 targetm.asm_out.output_anchor (symbol);
7640 /* Output the objects themselves. */
7641 offset = 0;
7642 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7644 /* Move to the object's offset, padding with zeros if necessary. */
7645 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7646 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7647 if (CONSTANT_POOL_ADDRESS_P (symbol))
7649 desc = SYMBOL_REF_CONSTANT (symbol);
7650 /* Pass 1 for align as we have already laid out everything in the block.
7651 So aligning shouldn't be necessary. */
7652 output_constant_pool_1 (desc, 1);
7653 offset += GET_MODE_SIZE (desc->mode);
7655 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7657 HOST_WIDE_INT size;
7658 decl = SYMBOL_REF_DECL (symbol);
7659 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7660 DECL_ALIGN (decl), false);
7662 size = get_constant_size (DECL_INITIAL (decl));
7663 offset += size;
7664 if ((flag_sanitize & SANITIZE_ADDRESS)
7665 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7666 && asan_protect_global (DECL_INITIAL (decl)))
7668 size = asan_red_zone_size (size);
7669 assemble_zeros (size);
7670 offset += size;
7673 else
7675 HOST_WIDE_INT size;
7676 decl = SYMBOL_REF_DECL (symbol);
7677 assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
7678 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7679 offset += size;
7680 if ((flag_sanitize & SANITIZE_ADDRESS)
7681 && asan_protect_global (decl))
7683 size = asan_red_zone_size (size);
7684 assemble_zeros (size);
7685 offset += size;
7691 /* A callback for qsort to compare object_blocks. */
7693 static int
7694 output_object_block_compare (const void *x, const void *y)
7696 object_block *p1 = *(object_block * const*)x;
7697 object_block *p2 = *(object_block * const*)y;
7699 if (p1->sect->common.flags & SECTION_NAMED
7700 && !(p2->sect->common.flags & SECTION_NAMED))
7701 return 1;
7703 if (!(p1->sect->common.flags & SECTION_NAMED)
7704 && p2->sect->common.flags & SECTION_NAMED)
7705 return -1;
7707 if (p1->sect->common.flags & SECTION_NAMED
7708 && p2->sect->common.flags & SECTION_NAMED)
7709 return strcmp (p1->sect->named.name, p2->sect->named.name);
7711 unsigned f1 = p1->sect->common.flags;
7712 unsigned f2 = p2->sect->common.flags;
7713 if (f1 == f2)
7714 return 0;
7715 return f1 < f2 ? -1 : 1;
7718 /* Output the definitions of all object_blocks. */
7720 void
7721 output_object_blocks (void)
7723 vec<object_block *, va_heap> v;
7724 v.create (object_block_htab->elements ());
7725 object_block *obj;
7726 hash_table<object_block_hasher>::iterator hi;
7728 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
7729 v.quick_push (obj);
7731 /* Sort them in order to output them in a deterministic manner,
7732 otherwise we may get .rodata sections in different orders with
7733 and without -g. */
7734 v.qsort (output_object_block_compare);
7735 unsigned i;
7736 FOR_EACH_VEC_ELT (v, i, obj)
7737 output_object_block (obj);
7739 v.release ();
7742 /* This function provides a possible implementation of the
7743 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7744 by -frecord-gcc-switches it creates a new mergeable, string section in the
7745 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7746 contains the switches in ASCII format.
7748 FIXME: This code does not correctly handle double quote characters
7749 that appear inside strings, (it strips them rather than preserving them).
7750 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7751 characters - instead it treats them as sub-string separators. Since
7752 we want to emit NUL strings terminators into the object file we have to use
7753 ASM_OUTPUT_SKIP. */
7756 elf_record_gcc_switches (print_switch_type type, const char * name)
7758 switch (type)
7760 case SWITCH_TYPE_PASSED:
7761 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7762 ASM_OUTPUT_SKIP (asm_out_file, HOST_WIDE_INT_1U);
7763 break;
7765 case SWITCH_TYPE_DESCRIPTIVE:
7766 if (name == NULL)
7768 /* Distinguish between invocations where name is NULL. */
7769 static bool started = false;
7771 if (!started)
7773 section * sec;
7775 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7776 SECTION_DEBUG
7777 | SECTION_MERGE
7778 | SECTION_STRINGS
7779 | (SECTION_ENTSIZE & 1),
7780 NULL);
7781 switch_to_section (sec);
7782 started = true;
7786 default:
7787 break;
7790 /* The return value is currently ignored by the caller, but must be 0.
7791 For -fverbose-asm the return value would be the number of characters
7792 emitted into the assembler file. */
7793 return 0;
7796 /* Emit text to declare externally defined symbols. It is needed to
7797 properly support non-default visibility. */
7798 void
7799 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7800 tree decl,
7801 const char *name ATTRIBUTE_UNUSED)
7803 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7804 set in order to avoid putting out names that are never really
7805 used. Always output visibility specified in the source. */
7806 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7807 && (DECL_VISIBILITY_SPECIFIED (decl)
7808 || targetm.binds_local_p (decl)))
7809 maybe_assemble_visibility (decl);
7812 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7814 void
7815 default_asm_output_source_filename (FILE *file, const char *name)
7817 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7818 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7819 #else
7820 fprintf (file, "\t.file\t");
7821 output_quoted_string (file, name);
7822 putc ('\n', file);
7823 #endif
7826 /* Output a file name in the form wanted by System V. */
7828 void
7829 output_file_directive (FILE *asm_file, const char *input_name)
7831 int len;
7832 const char *na;
7834 if (input_name == NULL)
7835 input_name = "<stdin>";
7836 else
7837 input_name = remap_debug_filename (input_name);
7839 len = strlen (input_name);
7840 na = input_name + len;
7842 /* NA gets INPUT_NAME sans directory names. */
7843 while (na > input_name)
7845 if (IS_DIR_SEPARATOR (na[-1]))
7846 break;
7847 na--;
7850 targetm.asm_out.output_source_filename (asm_file, na);
7853 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7854 EXP. */
7856 make_debug_expr_from_rtl (const_rtx exp)
7858 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7859 machine_mode mode = GET_MODE (exp);
7860 rtx dval;
7862 DECL_ARTIFICIAL (ddecl) = 1;
7863 if (REG_P (exp) && REG_EXPR (exp))
7864 type = TREE_TYPE (REG_EXPR (exp));
7865 else if (MEM_P (exp) && MEM_EXPR (exp))
7866 type = TREE_TYPE (MEM_EXPR (exp));
7867 else
7868 type = NULL_TREE;
7869 if (type && TYPE_MODE (type) == mode)
7870 TREE_TYPE (ddecl) = type;
7871 else
7872 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7873 SET_DECL_MODE (ddecl, mode);
7874 dval = gen_rtx_DEBUG_EXPR (mode);
7875 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7876 SET_DECL_RTL (ddecl, dval);
7877 return dval;
7880 #ifdef ELF_ASCII_ESCAPES
7881 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7883 void
7884 default_elf_asm_output_limited_string (FILE *f, const char *s)
7886 int escape;
7887 unsigned char c;
7889 fputs (STRING_ASM_OP, f);
7890 putc ('"', f);
7891 while (*s != '\0')
7893 c = *s;
7894 escape = ELF_ASCII_ESCAPES[c];
7895 switch (escape)
7897 case 0:
7898 putc (c, f);
7899 break;
7900 case 1:
7901 putc ('\\', f);
7902 putc ('0'+((c>>6)&7), f);
7903 putc ('0'+((c>>3)&7), f);
7904 putc ('0'+(c&7), f);
7905 break;
7906 default:
7907 putc ('\\', f);
7908 putc (escape, f);
7909 break;
7911 s++;
7913 putc ('\"', f);
7914 putc ('\n', f);
7917 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7919 void
7920 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7922 const char *limit = s + len;
7923 const char *last_null = NULL;
7924 unsigned bytes_in_chunk = 0;
7925 unsigned char c;
7926 int escape;
7928 for (; s < limit; s++)
7930 const char *p;
7932 if (bytes_in_chunk >= 60)
7934 putc ('\"', f);
7935 putc ('\n', f);
7936 bytes_in_chunk = 0;
7939 if (s > last_null)
7941 for (p = s; p < limit && *p != '\0'; p++)
7942 continue;
7943 last_null = p;
7945 else
7946 p = last_null;
7948 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7950 if (bytes_in_chunk > 0)
7952 putc ('\"', f);
7953 putc ('\n', f);
7954 bytes_in_chunk = 0;
7957 default_elf_asm_output_limited_string (f, s);
7958 s = p;
7960 else
7962 if (bytes_in_chunk == 0)
7963 fputs (ASCII_DATA_ASM_OP "\"", f);
7965 c = *s;
7966 escape = ELF_ASCII_ESCAPES[c];
7967 switch (escape)
7969 case 0:
7970 putc (c, f);
7971 bytes_in_chunk++;
7972 break;
7973 case 1:
7974 putc ('\\', f);
7975 putc ('0'+((c>>6)&7), f);
7976 putc ('0'+((c>>3)&7), f);
7977 putc ('0'+(c&7), f);
7978 bytes_in_chunk += 4;
7979 break;
7980 default:
7981 putc ('\\', f);
7982 putc (escape, f);
7983 bytes_in_chunk += 2;
7984 break;
7990 if (bytes_in_chunk > 0)
7992 putc ('\"', f);
7993 putc ('\n', f);
7996 #endif
7998 static GTY(()) section *elf_init_array_section;
7999 static GTY(()) section *elf_fini_array_section;
8001 static section *
8002 get_elf_initfini_array_priority_section (int priority,
8003 bool constructor_p)
8005 section *sec;
8006 if (priority != DEFAULT_INIT_PRIORITY)
8008 char buf[18];
8009 sprintf (buf, "%s.%.5u",
8010 constructor_p ? ".init_array" : ".fini_array",
8011 priority);
8012 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8014 else
8016 if (constructor_p)
8018 if (elf_init_array_section == NULL)
8019 elf_init_array_section
8020 = get_section (".init_array",
8021 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8022 sec = elf_init_array_section;
8024 else
8026 if (elf_fini_array_section == NULL)
8027 elf_fini_array_section
8028 = get_section (".fini_array",
8029 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8030 sec = elf_fini_array_section;
8033 return sec;
8036 /* Use .init_array section for constructors. */
8038 void
8039 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8041 section *sec = get_elf_initfini_array_priority_section (priority,
8042 true);
8043 assemble_addr_to_section (symbol, sec);
8046 /* Use .fini_array section for destructors. */
8048 void
8049 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8051 section *sec = get_elf_initfini_array_priority_section (priority,
8052 false);
8053 assemble_addr_to_section (symbol, sec);
8056 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8058 This is a bit of a cheat. The real default is a no-op, but this
8059 hook is the default for all targets with a .ident directive. */
8061 void
8062 default_asm_output_ident_directive (const char *ident_str)
8064 const char *ident_asm_op = "\t.ident\t";
8066 /* If we are still in the front end, do not write out the string
8067 to asm_out_file. Instead, add a fake top-level asm statement.
8068 This allows the front ends to use this hook without actually
8069 writing to asm_out_file, to handle #ident or Pragma Ident. */
8070 if (symtab->state == PARSING)
8072 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8073 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8075 else
8076 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8080 /* This function ensures that vtable_map variables are not only
8081 in the comdat section, but that each variable has its own unique
8082 comdat name. Without this the variables end up in the same section
8083 with a single comdat name.
8085 FIXME: resolve_unique_section needs to deal better with
8086 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8087 that is fixed, this if-else statement can be replaced with
8088 a single call to "switch_to_section (sect)". */
8090 static void
8091 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8093 #if defined (OBJECT_FORMAT_ELF)
8094 targetm.asm_out.named_section (sect->named.name,
8095 sect->named.common.flags
8096 | SECTION_LINKONCE,
8097 DECL_NAME (decl));
8098 in_section = sect;
8099 #else
8100 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8101 Therefore the following check is used.
8102 In case a the target is PE or COFF a comdat group section
8103 is created, e.g. .vtable_map_vars$foo. The linker places
8104 everything in .vtable_map_vars at the end.
8106 A fix could be made in
8107 gcc/config/i386/winnt.c: i386_pe_unique_section. */
8108 if (TARGET_PECOFF)
8110 char *name;
8112 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
8113 name = ACONCAT ((sect->named.name, "$",
8114 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
8115 else
8116 name = ACONCAT ((sect->named.name, "$",
8117 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
8118 NULL));
8120 targetm.asm_out.named_section (name,
8121 sect->named.common.flags
8122 | SECTION_LINKONCE,
8123 DECL_NAME (decl));
8124 in_section = sect;
8126 else
8127 switch_to_section (sect);
8128 #endif
8131 #include "gt-varasm.h"