match_asm_constraints: Use copy_rtx where needed (PR88001)
[official-gcc.git] / gcc / varasm.c
blobdcce541d6432dd57657b3cbba6beedf13df871c2
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2018 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. */
368 static struct object_block *
369 get_block_for_section (section *sect)
371 struct object_block *block;
373 if (sect == NULL)
374 return NULL;
376 object_block **slot
377 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
378 INSERT);
379 block = *slot;
380 if (block == NULL)
382 block = ggc_cleared_alloc<object_block> ();
383 block->sect = sect;
384 *slot = block;
386 return block;
389 /* Create a symbol with label LABEL and place it at byte offset
390 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
391 is not yet known. LABEL must be a garbage-collected string. */
393 static rtx
394 create_block_symbol (const char *label, struct object_block *block,
395 HOST_WIDE_INT offset)
397 rtx symbol;
398 unsigned int size;
400 /* Create the extended SYMBOL_REF. */
401 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
402 symbol = (rtx) ggc_internal_alloc (size);
404 /* Initialize the normal SYMBOL_REF fields. */
405 memset (symbol, 0, size);
406 PUT_CODE (symbol, SYMBOL_REF);
407 PUT_MODE (symbol, Pmode);
408 XSTR (symbol, 0) = label;
409 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
411 /* Initialize the block_symbol stuff. */
412 SYMBOL_REF_BLOCK (symbol) = block;
413 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
415 return symbol;
418 /* Return a section with a particular name and with whatever SECTION_*
419 flags section_type_flags deems appropriate. The name of the section
420 is taken from NAME if nonnull, otherwise it is taken from DECL's
421 DECL_SECTION_NAME. DECL is the decl associated with the section
422 (see the section comment for details) and RELOC is as for
423 section_type_flags. */
425 section *
426 get_named_section (tree decl, const char *name, int reloc)
428 unsigned int flags;
430 if (name == NULL)
432 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
433 name = DECL_SECTION_NAME (decl);
436 flags = targetm.section_type_flags (decl, name, reloc);
437 return get_section (name, flags, decl);
440 /* Worker for resolve_unique_section. */
442 static bool
443 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
445 n->implicit_section = true;
446 return false;
449 /* If required, set DECL_SECTION_NAME to a unique name. */
451 void
452 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
453 int flag_function_or_data_sections)
455 if (DECL_SECTION_NAME (decl) == NULL
456 && targetm_common.have_named_sections
457 && (flag_function_or_data_sections
458 || DECL_COMDAT_GROUP (decl)))
460 targetm.asm_out.unique_section (decl, reloc);
461 if (DECL_SECTION_NAME (decl))
462 symtab_node::get (decl)->call_for_symbol_and_aliases
463 (set_implicit_section, NULL, true);
467 #ifdef BSS_SECTION_ASM_OP
469 #ifdef ASM_OUTPUT_ALIGNED_BSS
471 /* Utility function for targets to use in implementing
472 ASM_OUTPUT_ALIGNED_BSS.
473 ??? It is believed that this function will work in most cases so such
474 support is localized here. */
476 static void
477 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
478 const char *name, unsigned HOST_WIDE_INT size,
479 int align)
481 switch_to_section (bss_section);
482 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
483 #ifdef ASM_DECLARE_OBJECT_NAME
484 last_assemble_variable_decl = decl;
485 ASM_DECLARE_OBJECT_NAME (file, name, decl);
486 #else
487 /* Standard thing is just output label for the object. */
488 ASM_OUTPUT_LABEL (file, name);
489 #endif /* ASM_DECLARE_OBJECT_NAME */
490 ASM_OUTPUT_SKIP (file, size ? size : 1);
493 #endif
495 #endif /* BSS_SECTION_ASM_OP */
497 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
498 /* Return the hot section for function DECL. Return text_section for
499 null DECLs. */
501 static section *
502 hot_function_section (tree decl)
504 if (decl != NULL_TREE
505 && DECL_SECTION_NAME (decl) != NULL
506 && targetm_common.have_named_sections)
507 return get_named_section (decl, NULL, 0);
508 else
509 return text_section;
511 #endif
513 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
514 is NULL.
516 When DECL_SECTION_NAME is non-NULL and it is implicit section and
517 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
518 concatenate the name with NAMED_SECTION_SUFFIX.
519 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
521 section *
522 get_named_text_section (tree decl,
523 const char *text_section_name,
524 const char *named_section_suffix)
526 if (decl && DECL_SECTION_NAME (decl))
528 if (named_section_suffix)
530 const char *dsn = DECL_SECTION_NAME (decl);
531 const char *stripped_name;
532 char *name, *buffer;
534 name = (char *) alloca (strlen (dsn) + 1);
535 memcpy (name, dsn,
536 strlen (dsn) + 1);
538 stripped_name = targetm.strip_name_encoding (name);
540 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
541 return get_named_section (decl, buffer, 0);
543 else if (symtab_node::get (decl)->implicit_section)
545 const char *name;
547 /* Do not try to split gnu_linkonce functions. This gets somewhat
548 slipperly. */
549 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
550 return NULL;
551 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
552 name = targetm.strip_name_encoding (name);
553 return get_named_section (decl, ACONCAT ((text_section_name, ".",
554 name, NULL)), 0);
556 else
557 return NULL;
559 return get_named_section (decl, text_section_name, 0);
562 /* Choose named function section based on its frequency. */
564 section *
565 default_function_section (tree decl, enum node_frequency freq,
566 bool startup, bool exit)
568 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
569 /* Old GNU linkers have buggy --gc-section support, which sometimes
570 results in .gcc_except_table* sections being garbage collected. */
571 if (decl
572 && symtab_node::get (decl)->implicit_section)
573 return NULL;
574 #endif
576 if (!flag_reorder_functions
577 || !targetm_common.have_named_sections)
578 return NULL;
579 /* Startup code should go to startup subsection unless it is
580 unlikely executed (this happens especially with function splitting
581 where we can split away unnecessary parts of static constructors. */
582 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
584 /* If we do have a profile or(and) LTO phase is executed, we do not need
585 these ELF section. */
586 if (!in_lto_p || !flag_profile_values)
587 return get_named_text_section (decl, ".text.startup", NULL);
588 else
589 return NULL;
592 /* Similarly for exit. */
593 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
594 return get_named_text_section (decl, ".text.exit", NULL);
596 /* Group cold functions together, similarly for hot code. */
597 switch (freq)
599 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
600 return get_named_text_section (decl, ".text.unlikely", NULL);
601 case NODE_FREQUENCY_HOT:
602 /* If we do have a profile or(and) LTO phase is executed, we do not need
603 these ELF section. */
604 if (!in_lto_p || !flag_profile_values)
605 return get_named_text_section (decl, ".text.hot", NULL);
606 /* FALLTHRU */
607 default:
608 return NULL;
612 /* Return the section for function DECL.
614 If DECL is NULL_TREE, return the text section. We can be passed
615 NULL_TREE under some circumstances by dbxout.c at least.
617 If FORCE_COLD is true, return cold function section ignoring
618 the frequency info of cgraph_node. */
620 static section *
621 function_section_1 (tree decl, bool force_cold)
623 section *section = NULL;
624 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
625 bool startup = false, exit = false;
627 if (decl)
629 struct cgraph_node *node = cgraph_node::get (decl);
631 if (node)
633 freq = node->frequency;
634 startup = node->only_called_at_startup;
635 exit = node->only_called_at_exit;
638 if (force_cold)
639 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
641 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
642 if (decl != NULL_TREE
643 && DECL_SECTION_NAME (decl) != NULL)
645 if (targetm.asm_out.function_section)
646 section = targetm.asm_out.function_section (decl, freq,
647 startup, exit);
648 if (section)
649 return section;
650 return get_named_section (decl, NULL, 0);
652 else
653 return targetm.asm_out.select_section
654 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
655 symtab_node::get (decl)->definition_alignment ());
656 #else
657 if (targetm.asm_out.function_section)
658 section = targetm.asm_out.function_section (decl, freq, startup, exit);
659 if (section)
660 return section;
661 return hot_function_section (decl);
662 #endif
665 /* Return the section for function DECL.
667 If DECL is NULL_TREE, return the text section. We can be passed
668 NULL_TREE under some circumstances by dbxout.c at least. */
670 section *
671 function_section (tree decl)
673 /* Handle cases where function splitting code decides
674 to put function entry point into unlikely executed section
675 despite the fact that the function itself is not cold
676 (i.e. it is called rarely but contains a hot loop that is
677 better to live in hot subsection for the code locality). */
678 return function_section_1 (decl,
679 first_function_block_is_cold);
682 /* Return the section for the current function, take IN_COLD_SECTION_P
683 into account. */
685 section *
686 current_function_section (void)
688 return function_section_1 (current_function_decl, in_cold_section_p);
691 /* Tell assembler to switch to unlikely-to-be-executed text section. */
693 section *
694 unlikely_text_section (void)
696 return function_section_1 (current_function_decl, true);
699 /* When called within a function context, return true if the function
700 has been assigned a cold text section and if SECT is that section.
701 When called outside a function context, return true if SECT is the
702 default cold section. */
704 bool
705 unlikely_text_section_p (section *sect)
707 return sect == function_section_1 (current_function_decl, true);
710 /* Switch to the other function partition (if inside of hot section
711 into cold section, otherwise into the hot section). */
713 void
714 switch_to_other_text_partition (void)
716 in_cold_section_p = !in_cold_section_p;
717 switch_to_section (current_function_section ());
720 /* Return the read-only data section associated with function DECL. */
722 section *
723 default_function_rodata_section (tree decl)
725 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
727 const char *name = DECL_SECTION_NAME (decl);
729 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
731 const char *dot;
732 size_t len;
733 char* rname;
735 dot = strchr (name + 1, '.');
736 if (!dot)
737 dot = name;
738 len = strlen (dot) + 8;
739 rname = (char *) alloca (len);
741 strcpy (rname, ".rodata");
742 strcat (rname, dot);
743 return get_section (rname, SECTION_LINKONCE, decl);
745 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
746 else if (DECL_COMDAT_GROUP (decl)
747 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
749 size_t len = strlen (name) + 1;
750 char *rname = (char *) alloca (len);
752 memcpy (rname, name, len);
753 rname[14] = 'r';
754 return get_section (rname, SECTION_LINKONCE, decl);
756 /* For .text.foo we want to use .rodata.foo. */
757 else if (flag_function_sections && flag_data_sections
758 && strncmp (name, ".text.", 6) == 0)
760 size_t len = strlen (name) + 1;
761 char *rname = (char *) alloca (len + 2);
763 memcpy (rname, ".rodata", 7);
764 memcpy (rname + 7, name + 5, len - 5);
765 return get_section (rname, 0, decl);
769 return readonly_data_section;
772 /* Return the read-only data section associated with function DECL
773 for targets where that section should be always the single
774 readonly data section. */
776 section *
777 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
779 return readonly_data_section;
782 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
784 static const char *
785 function_mergeable_rodata_prefix (void)
787 section *s = targetm.asm_out.function_rodata_section (current_function_decl);
788 if (SECTION_STYLE (s) == SECTION_NAMED)
789 return s->named.name;
790 else
791 return targetm.asm_out.mergeable_rodata_prefix;
794 /* Return the section to use for string merging. */
796 static section *
797 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
798 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
799 unsigned int flags ATTRIBUTE_UNUSED)
801 HOST_WIDE_INT len;
803 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
804 && TREE_CODE (decl) == STRING_CST
805 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
806 && align <= 256
807 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
808 && TREE_STRING_LENGTH (decl) == len)
810 scalar_int_mode mode;
811 unsigned int modesize;
812 const char *str;
813 HOST_WIDE_INT i;
814 int j, unit;
815 const char *prefix = function_mergeable_rodata_prefix ();
816 char *name = (char *) alloca (strlen (prefix) + 30);
818 mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
819 modesize = GET_MODE_BITSIZE (mode);
820 if (modesize >= 8 && modesize <= 256
821 && (modesize & (modesize - 1)) == 0)
823 if (align < modesize)
824 align = modesize;
826 if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
827 return readonly_data_section;
829 str = TREE_STRING_POINTER (decl);
830 unit = GET_MODE_SIZE (mode);
832 /* Check for embedded NUL characters. */
833 for (i = 0; i < len; i += unit)
835 for (j = 0; j < unit; j++)
836 if (str[i + j] != '\0')
837 break;
838 if (j == unit)
839 break;
841 if (i == len - unit || (unit == 1 && i == len))
843 sprintf (name, "%s.str%d.%d", prefix,
844 modesize / 8, (int) (align / 8));
845 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
846 return get_section (name, flags, NULL);
851 return readonly_data_section;
854 /* Return the section to use for constant merging. */
856 section *
857 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
858 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
859 unsigned int flags ATTRIBUTE_UNUSED)
861 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
862 && mode != VOIDmode
863 && mode != BLKmode
864 && known_le (GET_MODE_BITSIZE (mode), align)
865 && align >= 8
866 && align <= 256
867 && (align & (align - 1)) == 0
868 && (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
870 const char *prefix = function_mergeable_rodata_prefix ();
871 char *name = (char *) alloca (strlen (prefix) + 30);
873 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
874 flags |= (align / 8) | SECTION_MERGE;
875 return get_section (name, flags, NULL);
877 return readonly_data_section;
880 /* Given NAME, a putative register name, discard any customary prefixes. */
882 static const char *
883 strip_reg_name (const char *name)
885 #ifdef REGISTER_PREFIX
886 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
887 name += strlen (REGISTER_PREFIX);
888 #endif
889 if (name[0] == '%' || name[0] == '#')
890 name++;
891 return name;
894 /* The user has asked for a DECL to have a particular name. Set (or
895 change) it in such a way that we don't prefix an underscore to
896 it. */
897 void
898 set_user_assembler_name (tree decl, const char *name)
900 char *starred = (char *) alloca (strlen (name) + 2);
901 starred[0] = '*';
902 strcpy (starred + 1, name);
903 symtab->change_decl_assembler_name (decl, get_identifier (starred));
904 SET_DECL_RTL (decl, NULL_RTX);
907 /* Decode an `asm' spec for a declaration as a register name.
908 Return the register number, or -1 if nothing specified,
909 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
910 or -3 if ASMSPEC is `cc' and is not recognized,
911 or -4 if ASMSPEC is `memory' and is not recognized.
912 Accept an exact spelling or a decimal number.
913 Prefixes such as % are optional. */
916 decode_reg_name_and_count (const char *asmspec, int *pnregs)
918 /* Presume just one register is clobbered. */
919 *pnregs = 1;
921 if (asmspec != 0)
923 int i;
925 /* Get rid of confusing prefixes. */
926 asmspec = strip_reg_name (asmspec);
928 /* Allow a decimal number as a "register name". */
929 for (i = strlen (asmspec) - 1; i >= 0; i--)
930 if (! ISDIGIT (asmspec[i]))
931 break;
932 if (asmspec[0] != 0 && i < 0)
934 i = atoi (asmspec);
935 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
936 return i;
937 else
938 return -2;
941 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
942 if (reg_names[i][0]
943 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
944 return i;
946 #ifdef OVERLAPPING_REGISTER_NAMES
948 static const struct
950 const char *const name;
951 const int number;
952 const int nregs;
953 } table[] = OVERLAPPING_REGISTER_NAMES;
955 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
956 if (table[i].name[0]
957 && ! strcmp (asmspec, table[i].name))
959 *pnregs = table[i].nregs;
960 return table[i].number;
963 #endif /* OVERLAPPING_REGISTER_NAMES */
965 #ifdef ADDITIONAL_REGISTER_NAMES
967 static const struct { const char *const name; const int number; } table[]
968 = ADDITIONAL_REGISTER_NAMES;
970 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
971 if (table[i].name[0]
972 && ! strcmp (asmspec, table[i].name)
973 && reg_names[table[i].number][0])
974 return table[i].number;
976 #endif /* ADDITIONAL_REGISTER_NAMES */
978 if (!strcmp (asmspec, "memory"))
979 return -4;
981 if (!strcmp (asmspec, "cc"))
982 return -3;
984 return -2;
987 return -1;
991 decode_reg_name (const char *name)
993 int count;
994 return decode_reg_name_and_count (name, &count);
998 /* Return true if DECL's initializer is suitable for a BSS section. */
1000 bool
1001 bss_initializer_p (const_tree decl, bool named)
1003 /* Do not put non-common constants into the .bss section, they belong in
1004 a readonly section, except when NAMED is true. */
1005 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1006 && (DECL_INITIAL (decl) == NULL
1007 /* In LTO we have no errors in program; error_mark_node is used
1008 to mark offlined constructors. */
1009 || (DECL_INITIAL (decl) == error_mark_node
1010 && !in_lto_p)
1011 || (flag_zero_initialized_in_bss
1012 && initializer_zerop (DECL_INITIAL (decl)))));
1015 /* Compute the alignment of variable specified by DECL.
1016 DONT_OUTPUT_DATA is from assemble_variable. */
1018 void
1019 align_variable (tree decl, bool dont_output_data)
1021 unsigned int align = DECL_ALIGN (decl);
1023 /* In the case for initialing an array whose length isn't specified,
1024 where we have not yet been able to do the layout,
1025 figure out the proper alignment now. */
1026 if (dont_output_data && DECL_SIZE (decl) == 0
1027 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1028 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1030 /* Some object file formats have a maximum alignment which they support.
1031 In particular, a.out format supports a maximum alignment of 4. */
1032 if (align > MAX_OFILE_ALIGNMENT)
1034 error ("alignment of %q+D is greater than maximum object "
1035 "file alignment %d", decl,
1036 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1037 align = MAX_OFILE_ALIGNMENT;
1040 if (! DECL_USER_ALIGN (decl))
1042 #ifdef DATA_ABI_ALIGNMENT
1043 unsigned int data_abi_align
1044 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1045 /* For backwards compatibility, don't assume the ABI alignment for
1046 TLS variables. */
1047 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1048 align = data_abi_align;
1049 #endif
1051 /* On some machines, it is good to increase alignment sometimes.
1052 But as DECL_ALIGN is used both for actually emitting the variable
1053 and for code accessing the variable as guaranteed alignment, we
1054 can only increase the alignment if it is a performance optimization
1055 if the references to it must bind to the current definition. */
1056 if (decl_binds_to_current_def_p (decl)
1057 && !DECL_VIRTUAL_P (decl))
1059 #ifdef DATA_ALIGNMENT
1060 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1061 /* Don't increase alignment too much for TLS variables - TLS space
1062 is too precious. */
1063 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1064 align = data_align;
1065 #endif
1066 if (DECL_INITIAL (decl) != 0
1067 /* In LTO we have no errors in program; error_mark_node is used
1068 to mark offlined constructors. */
1069 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1071 unsigned int const_align
1072 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1073 /* Don't increase alignment too much for TLS variables - TLS
1074 space is too precious. */
1075 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1076 align = const_align;
1081 /* Reset the alignment in case we have made it tighter, so we can benefit
1082 from it in get_pointer_alignment. */
1083 SET_DECL_ALIGN (decl, align);
1086 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1087 beyond what align_variable returned. */
1089 static unsigned int
1090 get_variable_align (tree decl)
1092 unsigned int align = DECL_ALIGN (decl);
1094 /* For user aligned vars or static vars align_variable already did
1095 everything. */
1096 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1097 return align;
1099 #ifdef DATA_ABI_ALIGNMENT
1100 if (DECL_THREAD_LOCAL_P (decl))
1101 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1102 #endif
1104 /* For decls that bind to the current definition, align_variable
1105 did also everything, except for not assuming ABI required alignment
1106 of TLS variables. For other vars, increase the alignment here
1107 as an optimization. */
1108 if (!decl_binds_to_current_def_p (decl))
1110 /* On some machines, it is good to increase alignment sometimes. */
1111 #ifdef DATA_ALIGNMENT
1112 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1113 /* Don't increase alignment too much for TLS variables - TLS space
1114 is too precious. */
1115 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1116 align = data_align;
1117 #endif
1118 if (DECL_INITIAL (decl) != 0
1119 /* In LTO we have no errors in program; error_mark_node is used
1120 to mark offlined constructors. */
1121 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1123 unsigned int const_align
1124 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1125 /* Don't increase alignment too much for TLS variables - TLS space
1126 is too precious. */
1127 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1128 align = const_align;
1132 return align;
1135 /* Return the section into which the given VAR_DECL or CONST_DECL
1136 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1137 section should be used wherever possible. */
1139 section *
1140 get_variable_section (tree decl, bool prefer_noswitch_p)
1142 addr_space_t as = ADDR_SPACE_GENERIC;
1143 int reloc;
1144 varpool_node *vnode = varpool_node::get (decl);
1145 if (vnode)
1147 vnode = vnode->ultimate_alias_target ();
1148 decl = vnode->decl;
1151 if (TREE_TYPE (decl) != error_mark_node)
1152 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1154 /* We need the constructor to figure out reloc flag. */
1155 if (vnode)
1156 vnode->get_constructor ();
1158 if (DECL_COMMON (decl))
1160 /* If the decl has been given an explicit section name, or it resides
1161 in a non-generic address space, then it isn't common, and shouldn't
1162 be handled as such. */
1163 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1164 && ADDR_SPACE_GENERIC_P (as));
1165 if (DECL_THREAD_LOCAL_P (decl))
1166 return tls_comm_section;
1167 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1168 return comm_section;
1171 if (DECL_INITIAL (decl) == error_mark_node)
1172 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1173 else if (DECL_INITIAL (decl))
1174 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1175 else
1176 reloc = 0;
1178 resolve_unique_section (decl, reloc, flag_data_sections);
1179 if (IN_NAMED_SECTION (decl))
1181 section *sect = get_named_section (decl, NULL, reloc);
1183 if ((sect->common.flags & SECTION_BSS)
1184 && !bss_initializer_p (decl, true))
1186 error_at (DECL_SOURCE_LOCATION (decl),
1187 "only zero initializers are allowed in section %qs",
1188 sect->named.name);
1189 DECL_INITIAL (decl) = error_mark_node;
1191 return sect;
1194 if (ADDR_SPACE_GENERIC_P (as)
1195 && !DECL_THREAD_LOCAL_P (decl)
1196 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1197 && bss_initializer_p (decl))
1199 if (!TREE_PUBLIC (decl)
1200 && !((flag_sanitize & SANITIZE_ADDRESS)
1201 && asan_protect_global (decl)))
1202 return lcomm_section;
1203 if (bss_noswitch_section)
1204 return bss_noswitch_section;
1207 return targetm.asm_out.select_section (decl, reloc,
1208 get_variable_align (decl));
1211 /* Return the block into which object_block DECL should be placed. */
1213 static struct object_block *
1214 get_block_for_decl (tree decl)
1216 section *sect;
1218 if (VAR_P (decl))
1220 /* The object must be defined in this translation unit. */
1221 if (DECL_EXTERNAL (decl))
1222 return NULL;
1224 /* There's no point using object blocks for something that is
1225 isolated by definition. */
1226 if (DECL_COMDAT_GROUP (decl))
1227 return NULL;
1230 /* We can only calculate block offsets if the decl has a known
1231 constant size. */
1232 if (DECL_SIZE_UNIT (decl) == NULL)
1233 return NULL;
1234 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1235 return NULL;
1237 /* Find out which section should contain DECL. We cannot put it into
1238 an object block if it requires a standalone definition. */
1239 if (VAR_P (decl))
1240 align_variable (decl, 0);
1241 sect = get_variable_section (decl, true);
1242 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1243 return NULL;
1245 return get_block_for_section (sect);
1248 /* Make sure block symbol SYMBOL is in block BLOCK. */
1250 static void
1251 change_symbol_block (rtx symbol, struct object_block *block)
1253 if (block != SYMBOL_REF_BLOCK (symbol))
1255 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1256 SYMBOL_REF_BLOCK (symbol) = block;
1260 /* Return true if it is possible to put DECL in an object_block. */
1262 static bool
1263 use_blocks_for_decl_p (tree decl)
1265 struct symtab_node *snode;
1267 /* Only data DECLs can be placed into object blocks. */
1268 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1269 return false;
1271 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1272 are never used from code directly and we never want object block handling
1273 for those. */
1274 if (DECL_INITIAL (decl) == decl)
1275 return false;
1277 /* If this decl is an alias, then we don't want to emit a
1278 definition. */
1279 if (VAR_P (decl)
1280 && (snode = symtab_node::get (decl)) != NULL
1281 && snode->alias)
1282 return false;
1284 return targetm.use_blocks_for_decl_p (decl);
1287 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1288 until we find an identifier that is not itself a transparent alias.
1289 Modify the alias passed to it by reference (and all aliases on the
1290 way to the ultimate target), such that they do not have to be
1291 followed again, and return the ultimate target of the alias
1292 chain. */
1294 static inline tree
1295 ultimate_transparent_alias_target (tree *alias)
1297 tree target = *alias;
1299 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1301 gcc_assert (TREE_CHAIN (target));
1302 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1303 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1304 && ! TREE_CHAIN (target));
1305 *alias = target;
1308 return target;
1311 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1312 have static storage duration. In other words, it should not be an
1313 automatic variable, including PARM_DECLs.
1315 There is, however, one exception: this function handles variables
1316 explicitly placed in a particular register by the user.
1318 This is never called for PARM_DECL nodes. */
1320 void
1321 make_decl_rtl (tree decl)
1323 const char *name = 0;
1324 int reg_number;
1325 tree id;
1326 rtx x;
1328 /* Check that we are not being given an automatic variable. */
1329 gcc_assert (TREE_CODE (decl) != PARM_DECL
1330 && TREE_CODE (decl) != RESULT_DECL);
1332 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1333 gcc_assert (!VAR_P (decl)
1334 || TREE_STATIC (decl)
1335 || TREE_PUBLIC (decl)
1336 || DECL_EXTERNAL (decl)
1337 || DECL_REGISTER (decl));
1339 /* And that we were not given a type or a label. */
1340 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1341 && TREE_CODE (decl) != LABEL_DECL);
1343 /* For a duplicate declaration, we can be called twice on the
1344 same DECL node. Don't discard the RTL already made. */
1345 if (DECL_RTL_SET_P (decl))
1347 /* If the old RTL had the wrong mode, fix the mode. */
1348 x = DECL_RTL (decl);
1349 if (GET_MODE (x) != DECL_MODE (decl))
1350 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1352 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1353 return;
1355 /* ??? Another way to do this would be to maintain a hashed
1356 table of such critters. Instead of adding stuff to a DECL
1357 to give certain attributes to it, we could use an external
1358 hash map from DECL to set of attributes. */
1360 /* Let the target reassign the RTL if it wants.
1361 This is necessary, for example, when one machine specific
1362 decl attribute overrides another. */
1363 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1365 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1366 on the new decl information. */
1367 if (MEM_P (x)
1368 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1369 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1370 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1372 return;
1375 /* If this variable belongs to the global constant pool, retrieve the
1376 pre-computed RTL or recompute it in LTO mode. */
1377 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1379 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1380 return;
1383 id = DECL_ASSEMBLER_NAME (decl);
1384 name = IDENTIFIER_POINTER (id);
1386 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1387 && DECL_REGISTER (decl))
1389 error ("register name not specified for %q+D", decl);
1391 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1393 const char *asmspec = name+1;
1394 machine_mode mode = DECL_MODE (decl);
1395 reg_number = decode_reg_name (asmspec);
1396 /* First detect errors in declaring global registers. */
1397 if (reg_number == -1)
1398 error ("register name not specified for %q+D", decl);
1399 else if (reg_number < 0)
1400 error ("invalid register name for %q+D", decl);
1401 else if (mode == BLKmode)
1402 error ("data type of %q+D isn%'t suitable for a register",
1403 decl);
1404 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1405 error ("the register specified for %q+D cannot be accessed"
1406 " by the current target", decl);
1407 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1408 error ("the register specified for %q+D is not general enough"
1409 " to be used as a register variable", decl);
1410 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1411 error ("register specified for %q+D isn%'t suitable for data type",
1412 decl);
1413 /* Now handle properly declared static register variables. */
1414 else
1416 int nregs;
1418 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1420 DECL_INITIAL (decl) = 0;
1421 error ("global register variable has initial value");
1423 if (TREE_THIS_VOLATILE (decl))
1424 warning (OPT_Wvolatile_register_var,
1425 "optimization may eliminate reads and/or "
1426 "writes to register variables");
1428 /* If the user specified one of the eliminables registers here,
1429 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1430 confused with that register and be eliminated. This usage is
1431 somewhat suspect... */
1433 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1434 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1435 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1437 if (TREE_STATIC (decl))
1439 /* Make this register global, so not usable for anything
1440 else. */
1441 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1442 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1443 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1444 #endif
1445 nregs = hard_regno_nregs (reg_number, mode);
1446 while (nregs > 0)
1447 globalize_reg (decl, reg_number + --nregs);
1450 /* As a register variable, it has no section. */
1451 return;
1453 /* Avoid internal errors from invalid register
1454 specifications. */
1455 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1456 DECL_HARD_REGISTER (decl) = 0;
1457 /* Also avoid SSA inconsistencies by pretending this is an external
1458 decl now. */
1459 DECL_EXTERNAL (decl) = 1;
1460 return;
1462 /* Now handle ordinary static variables and functions (in memory).
1463 Also handle vars declared register invalidly. */
1464 else if (name[0] == '*')
1466 #ifdef REGISTER_PREFIX
1467 if (strlen (REGISTER_PREFIX) != 0)
1469 reg_number = decode_reg_name (name);
1470 if (reg_number >= 0 || reg_number == -3)
1471 error ("register name given for non-register variable %q+D", decl);
1473 #endif
1476 /* Specifying a section attribute on a variable forces it into a
1477 non-.bss section, and thus it cannot be common. */
1478 /* FIXME: In general this code should not be necessary because
1479 visibility pass is doing the same work. But notice_global_symbol
1480 is called early and it needs to make DECL_RTL to get the name.
1481 we take care of recomputing the DECL_RTL after visibility is changed. */
1482 if (VAR_P (decl)
1483 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1484 && DECL_SECTION_NAME (decl) != NULL
1485 && DECL_INITIAL (decl) == NULL_TREE
1486 && DECL_COMMON (decl))
1487 DECL_COMMON (decl) = 0;
1489 /* Variables can't be both common and weak. */
1490 if (VAR_P (decl) && DECL_WEAK (decl))
1491 DECL_COMMON (decl) = 0;
1493 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1494 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1495 else
1497 machine_mode address_mode = Pmode;
1498 if (TREE_TYPE (decl) != error_mark_node)
1500 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1501 address_mode = targetm.addr_space.address_mode (as);
1503 x = gen_rtx_SYMBOL_REF (address_mode, name);
1505 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1506 SET_SYMBOL_REF_DECL (x, decl);
1508 x = gen_rtx_MEM (DECL_MODE (decl), x);
1509 if (TREE_CODE (decl) != FUNCTION_DECL)
1510 set_mem_attributes (x, decl, 1);
1511 SET_DECL_RTL (decl, x);
1513 /* Optionally set flags or add text to the name to record information
1514 such as that it is a function name.
1515 If the name is changed, the macro ASM_OUTPUT_LABELREF
1516 will have to know how to strip this information. */
1517 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1520 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1521 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1522 rtl. */
1525 make_decl_rtl_for_debug (tree decl)
1527 unsigned int save_aliasing_flag;
1528 rtx rtl;
1530 if (DECL_RTL_SET_P (decl))
1531 return DECL_RTL (decl);
1533 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1534 call new_alias_set. If running with -fcompare-debug, sometimes
1535 we do not want to create alias sets that will throw the alias
1536 numbers off in the comparison dumps. So... clearing
1537 flag_strict_aliasing will keep new_alias_set() from creating a
1538 new set. */
1539 save_aliasing_flag = flag_strict_aliasing;
1540 flag_strict_aliasing = 0;
1542 rtl = DECL_RTL (decl);
1543 /* Reset DECL_RTL back, as various parts of the compiler expects
1544 DECL_RTL set meaning it is actually going to be output. */
1545 SET_DECL_RTL (decl, NULL);
1547 flag_strict_aliasing = save_aliasing_flag;
1548 return rtl;
1551 /* Output a string of literal assembler code
1552 for an `asm' keyword used between functions. */
1554 void
1555 assemble_asm (tree string)
1557 const char *p;
1558 app_enable ();
1560 if (TREE_CODE (string) == ADDR_EXPR)
1561 string = TREE_OPERAND (string, 0);
1563 p = TREE_STRING_POINTER (string);
1564 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1567 /* Write the address of the entity given by SYMBOL to SEC. */
1568 void
1569 assemble_addr_to_section (rtx symbol, section *sec)
1571 switch_to_section (sec);
1572 assemble_align (POINTER_SIZE);
1573 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1576 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1577 not) section for PRIORITY. */
1578 section *
1579 get_cdtor_priority_section (int priority, bool constructor_p)
1581 /* Buffer conservatively large enough for the full range of a 32-bit
1582 int plus the text below. */
1583 char buf[18];
1585 /* ??? This only works reliably with the GNU linker. */
1586 sprintf (buf, "%s.%.5u",
1587 constructor_p ? ".ctors" : ".dtors",
1588 /* Invert the numbering so the linker puts us in the proper
1589 order; constructors are run from right to left, and the
1590 linker sorts in increasing order. */
1591 MAX_INIT_PRIORITY - priority);
1592 return get_section (buf, SECTION_WRITE, NULL);
1595 void
1596 default_named_section_asm_out_destructor (rtx symbol, int priority)
1598 section *sec;
1600 if (priority != DEFAULT_INIT_PRIORITY)
1601 sec = get_cdtor_priority_section (priority,
1602 /*constructor_p=*/false);
1603 else
1604 sec = get_section (".dtors", SECTION_WRITE, NULL);
1606 assemble_addr_to_section (symbol, sec);
1609 #ifdef DTORS_SECTION_ASM_OP
1610 void
1611 default_dtor_section_asm_out_destructor (rtx symbol,
1612 int priority ATTRIBUTE_UNUSED)
1614 assemble_addr_to_section (symbol, dtors_section);
1616 #endif
1618 void
1619 default_named_section_asm_out_constructor (rtx symbol, int priority)
1621 section *sec;
1623 if (priority != DEFAULT_INIT_PRIORITY)
1624 sec = get_cdtor_priority_section (priority,
1625 /*constructor_p=*/true);
1626 else
1627 sec = get_section (".ctors", SECTION_WRITE, NULL);
1629 assemble_addr_to_section (symbol, sec);
1632 #ifdef CTORS_SECTION_ASM_OP
1633 void
1634 default_ctor_section_asm_out_constructor (rtx symbol,
1635 int priority ATTRIBUTE_UNUSED)
1637 assemble_addr_to_section (symbol, ctors_section);
1639 #endif
1641 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1642 a nonzero value if the constant pool should be output before the
1643 start of the function, or a zero value if the pool should output
1644 after the end of the function. The default is to put it before the
1645 start. */
1647 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1648 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1649 #endif
1651 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1652 to be output to assembler.
1653 Set first_global_object_name and weak_global_object_name as appropriate. */
1655 void
1656 notice_global_symbol (tree decl)
1658 const char **t = &first_global_object_name;
1660 if (first_global_object_name
1661 || !TREE_PUBLIC (decl)
1662 || DECL_EXTERNAL (decl)
1663 || !DECL_NAME (decl)
1664 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1665 || (TREE_CODE (decl) != FUNCTION_DECL
1666 && (!VAR_P (decl)
1667 || (DECL_COMMON (decl)
1668 && (DECL_INITIAL (decl) == 0
1669 || DECL_INITIAL (decl) == error_mark_node)))))
1670 return;
1672 /* We win when global object is found, but it is useful to know about weak
1673 symbol as well so we can produce nicer unique names. */
1674 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1675 t = &weak_global_object_name;
1677 if (!*t)
1679 tree id = DECL_ASSEMBLER_NAME (decl);
1680 ultimate_transparent_alias_target (&id);
1681 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1685 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1686 current function goes into the cold section, so that targets can use
1687 current_function_section during RTL expansion. DECL describes the
1688 function. */
1690 void
1691 decide_function_section (tree decl)
1693 first_function_block_is_cold = false;
1695 if (DECL_SECTION_NAME (decl))
1697 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1698 /* Calls to function_section rely on first_function_block_is_cold
1699 being accurate. */
1700 first_function_block_is_cold = (node
1701 && node->frequency
1702 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1705 in_cold_section_p = first_function_block_is_cold;
1708 /* Get the function's name, as described by its RTL. This may be
1709 different from the DECL_NAME name used in the source file. */
1710 const char *
1711 get_fnname_from_decl (tree decl)
1713 rtx x = DECL_RTL (decl);
1714 gcc_assert (MEM_P (x));
1715 x = XEXP (x, 0);
1716 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1717 return XSTR (x, 0);
1720 /* Output assembler code for the constant pool of a function and associated
1721 with defining the name of the function. DECL describes the function.
1722 NAME is the function's name. For the constant pool, we use the current
1723 constant pool data. */
1725 void
1726 assemble_start_function (tree decl, const char *fnname)
1728 int align;
1729 char tmp_label[100];
1730 bool hot_label_written = false;
1732 if (crtl->has_bb_partition)
1734 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1735 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1736 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1737 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1738 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1739 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1740 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1741 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1742 const_labelno++;
1743 cold_function_name = NULL_TREE;
1745 else
1747 crtl->subsections.hot_section_label = NULL;
1748 crtl->subsections.cold_section_label = NULL;
1749 crtl->subsections.hot_section_end_label = NULL;
1750 crtl->subsections.cold_section_end_label = NULL;
1753 /* The following code does not need preprocessing in the assembler. */
1755 app_disable ();
1757 if (CONSTANT_POOL_BEFORE_FUNCTION)
1758 output_constant_pool (fnname, decl);
1760 align = symtab_node::get (decl)->definition_alignment ();
1762 /* Make sure the not and cold text (code) sections are properly
1763 aligned. This is necessary here in the case where the function
1764 has both hot and cold sections, because we don't want to re-set
1765 the alignment when the section switch happens mid-function. */
1767 if (crtl->has_bb_partition)
1769 first_function_block_is_cold = false;
1771 switch_to_section (unlikely_text_section ());
1772 assemble_align (align);
1773 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1775 /* When the function starts with a cold section, we need to explicitly
1776 align the hot section and write out the hot section label.
1777 But if the current function is a thunk, we do not have a CFG. */
1778 if (!cfun->is_thunk
1779 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1781 switch_to_section (text_section);
1782 assemble_align (align);
1783 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1784 hot_label_written = true;
1785 first_function_block_is_cold = true;
1787 in_cold_section_p = first_function_block_is_cold;
1791 /* Switch to the correct text section for the start of the function. */
1793 switch_to_section (function_section (decl));
1794 if (crtl->has_bb_partition && !hot_label_written)
1795 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1797 /* Tell assembler to move to target machine's alignment for functions. */
1798 align = floor_log2 (align / BITS_PER_UNIT);
1799 if (align > 0)
1801 ASM_OUTPUT_ALIGN (asm_out_file, align);
1804 /* Handle a user-specified function alignment.
1805 Note that we still need to align to DECL_ALIGN, as above,
1806 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1807 if (! DECL_USER_ALIGN (decl)
1808 && align_functions.levels[0].log > align
1809 && optimize_function_for_speed_p (cfun))
1811 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1812 int align_log = align_functions.levels[0].log;
1813 #endif
1814 int max_skip = align_functions.levels[0].maxskip;
1815 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1816 && max_skip >= crtl->max_insn_address)
1817 max_skip = crtl->max_insn_address - 1;
1819 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1820 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1821 if (max_skip == align_functions.levels[0].maxskip)
1822 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1823 align_functions.levels[1].log,
1824 align_functions.levels[1].maxskip);
1825 #else
1826 ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
1827 #endif
1830 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1831 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1832 #endif
1834 if (!DECL_IGNORED_P (decl))
1835 (*debug_hooks->begin_function) (decl);
1837 /* Make function name accessible from other files, if appropriate. */
1839 if (TREE_PUBLIC (decl))
1841 notice_global_symbol (decl);
1843 globalize_decl (decl);
1845 maybe_assemble_visibility (decl);
1848 if (DECL_PRESERVE_P (decl))
1849 targetm.asm_out.mark_decl_preserved (fnname);
1851 unsigned HOST_WIDE_INT patch_area_size = function_entry_patch_area_size;
1852 unsigned HOST_WIDE_INT patch_area_entry = function_entry_patch_area_start;
1854 tree patchable_function_entry_attr
1855 = lookup_attribute ("patchable_function_entry", DECL_ATTRIBUTES (decl));
1856 if (patchable_function_entry_attr)
1858 tree pp_val = TREE_VALUE (patchable_function_entry_attr);
1859 tree patchable_function_entry_value1 = TREE_VALUE (pp_val);
1861 if (tree_fits_uhwi_p (patchable_function_entry_value1))
1862 patch_area_size = tree_to_uhwi (patchable_function_entry_value1);
1863 else
1864 gcc_unreachable ();
1866 patch_area_entry = 0;
1867 if (list_length (pp_val) > 1)
1869 tree patchable_function_entry_value2 =
1870 TREE_VALUE (TREE_CHAIN (pp_val));
1872 if (tree_fits_uhwi_p (patchable_function_entry_value2))
1873 patch_area_entry = tree_to_uhwi (patchable_function_entry_value2);
1874 else
1875 gcc_unreachable ();
1879 if (patch_area_entry > patch_area_size)
1881 if (patch_area_size > 0)
1882 warning (OPT_Wattributes, "Patchable function entry > size");
1883 patch_area_entry = 0;
1886 /* Emit the patching area before the entry label, if any. */
1887 if (patch_area_entry > 0)
1888 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1889 patch_area_entry, true);
1891 /* Do any machine/system dependent processing of the function name. */
1892 #ifdef ASM_DECLARE_FUNCTION_NAME
1893 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1894 #else
1895 /* Standard thing is just output label for the function. */
1896 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1897 #endif /* ASM_DECLARE_FUNCTION_NAME */
1899 /* And the area after the label. Record it if we haven't done so yet. */
1900 if (patch_area_size > patch_area_entry)
1901 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1902 patch_area_size-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 = output_constant_def (target, 1);
2965 break;
2967 case INDIRECT_REF:
2968 /* This deals with absolute addresses. */
2969 offset += tree_to_shwi (TREE_OPERAND (target, 0));
2970 x = gen_rtx_MEM (QImode,
2971 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
2972 break;
2974 case COMPOUND_LITERAL_EXPR:
2975 gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
2976 x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
2977 break;
2979 default:
2980 gcc_unreachable ();
2983 gcc_assert (MEM_P (x));
2984 x = XEXP (x, 0);
2986 value->base = x;
2987 value->offset = offset;
2990 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2992 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2994 /* Constant pool accessor function. */
2996 hash_table<tree_descriptor_hasher> *
2997 constant_pool_htab (void)
2999 return const_desc_htab;
3002 /* Compute a hash code for a constant expression. */
3004 hashval_t
3005 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3007 return ptr->hash;
3010 static hashval_t
3011 const_hash_1 (const tree exp)
3013 const char *p;
3014 hashval_t hi;
3015 int len, i;
3016 enum tree_code code = TREE_CODE (exp);
3018 /* Either set P and LEN to the address and len of something to hash and
3019 exit the switch or return a value. */
3021 switch (code)
3023 case INTEGER_CST:
3024 p = (char *) &TREE_INT_CST_ELT (exp, 0);
3025 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3026 break;
3028 case REAL_CST:
3029 return real_hash (TREE_REAL_CST_PTR (exp));
3031 case FIXED_CST:
3032 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3034 case STRING_CST:
3035 p = TREE_STRING_POINTER (exp);
3036 len = TREE_STRING_LENGTH (exp);
3037 break;
3039 case COMPLEX_CST:
3040 return (const_hash_1 (TREE_REALPART (exp)) * 5
3041 + const_hash_1 (TREE_IMAGPART (exp)));
3043 case VECTOR_CST:
3045 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3046 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3047 unsigned int count = vector_cst_encoded_nelts (exp);
3048 for (unsigned int i = 0; i < count; ++i)
3049 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3050 return hi;
3053 case CONSTRUCTOR:
3055 unsigned HOST_WIDE_INT idx;
3056 tree value;
3058 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3060 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3061 if (value)
3062 hi = hi * 603 + const_hash_1 (value);
3064 return hi;
3067 case ADDR_EXPR:
3068 if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3069 return const_hash_1 (TREE_OPERAND (exp, 0));
3071 /* Fallthru. */
3072 case FDESC_EXPR:
3074 struct addr_const value;
3076 decode_addr_const (exp, &value);
3077 switch (GET_CODE (value.base))
3079 case SYMBOL_REF:
3080 /* Don't hash the address of the SYMBOL_REF;
3081 only use the offset and the symbol name. */
3082 hi = value.offset.coeffs[0];
3083 p = XSTR (value.base, 0);
3084 for (i = 0; p[i] != 0; i++)
3085 hi = ((hi * 613) + (unsigned) (p[i]));
3086 break;
3088 case LABEL_REF:
3089 hi = (value.offset.coeffs[0]
3090 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3091 break;
3093 default:
3094 gcc_unreachable ();
3097 return hi;
3099 case PLUS_EXPR:
3100 case POINTER_PLUS_EXPR:
3101 case MINUS_EXPR:
3102 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3103 + const_hash_1 (TREE_OPERAND (exp, 1)));
3105 CASE_CONVERT:
3106 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3108 default:
3109 /* A language specific constant. Just hash the code. */
3110 return code;
3113 /* Compute hashing function. */
3114 hi = len;
3115 for (i = 0; i < len; i++)
3116 hi = ((hi * 613) + (unsigned) (p[i]));
3118 return hi;
3121 /* Wrapper of compare_constant, for the htab interface. */
3122 bool
3123 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3124 constant_descriptor_tree *c2)
3126 if (c1->hash != c2->hash)
3127 return 0;
3128 return compare_constant (c1->value, c2->value);
3131 /* Compare t1 and t2, and return 1 only if they are known to result in
3132 the same bit pattern on output. */
3134 static int
3135 compare_constant (const tree t1, const tree t2)
3137 enum tree_code typecode;
3139 if (t1 == NULL_TREE)
3140 return t2 == NULL_TREE;
3141 if (t2 == NULL_TREE)
3142 return 0;
3144 if (TREE_CODE (t1) != TREE_CODE (t2))
3145 return 0;
3147 switch (TREE_CODE (t1))
3149 case INTEGER_CST:
3150 /* Integer constants are the same only if the same width of type. */
3151 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3152 return 0;
3153 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3154 return 0;
3155 return tree_int_cst_equal (t1, t2);
3157 case REAL_CST:
3158 /* Real constants are the same only if the same width of type. In
3159 addition to the same width, we need to check whether the modes are the
3160 same. There might be two floating point modes that are the same size
3161 but have different representations, such as the PowerPC that has 2
3162 different 128-bit floating point types (IBM extended double and IEEE
3163 128-bit floating point). */
3164 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3165 return 0;
3166 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3167 return 0;
3168 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3170 case FIXED_CST:
3171 /* Fixed constants are the same only if the same width of type. */
3172 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3173 return 0;
3175 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3177 case STRING_CST:
3178 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3179 || int_size_in_bytes (TREE_TYPE (t1))
3180 != int_size_in_bytes (TREE_TYPE (t2)))
3181 return 0;
3183 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3184 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3185 TREE_STRING_LENGTH (t1)));
3187 case COMPLEX_CST:
3188 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3189 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3191 case VECTOR_CST:
3193 if (VECTOR_CST_NPATTERNS (t1)
3194 != VECTOR_CST_NPATTERNS (t2))
3195 return 0;
3197 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3198 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3199 return 0;
3201 unsigned int count = vector_cst_encoded_nelts (t1);
3202 for (unsigned int i = 0; i < count; ++i)
3203 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3204 VECTOR_CST_ENCODED_ELT (t2, i)))
3205 return 0;
3207 return 1;
3210 case CONSTRUCTOR:
3212 vec<constructor_elt, va_gc> *v1, *v2;
3213 unsigned HOST_WIDE_INT idx;
3215 typecode = TREE_CODE (TREE_TYPE (t1));
3216 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3217 return 0;
3219 if (typecode == ARRAY_TYPE)
3221 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3222 /* For arrays, check that mode, size and storage order match. */
3223 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3224 || size_1 == -1
3225 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3226 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3227 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3228 return 0;
3230 else
3232 /* For record and union constructors, require exact type
3233 equality. */
3234 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3235 return 0;
3238 v1 = CONSTRUCTOR_ELTS (t1);
3239 v2 = CONSTRUCTOR_ELTS (t2);
3240 if (vec_safe_length (v1) != vec_safe_length (v2))
3241 return 0;
3243 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3245 constructor_elt *c1 = &(*v1)[idx];
3246 constructor_elt *c2 = &(*v2)[idx];
3248 /* Check that each value is the same... */
3249 if (!compare_constant (c1->value, c2->value))
3250 return 0;
3251 /* ... and that they apply to the same fields! */
3252 if (typecode == ARRAY_TYPE)
3254 if (!compare_constant (c1->index, c2->index))
3255 return 0;
3257 else
3259 if (c1->index != c2->index)
3260 return 0;
3264 return 1;
3267 case ADDR_EXPR:
3268 case FDESC_EXPR:
3270 struct addr_const value1, value2;
3271 enum rtx_code code;
3272 int ret;
3274 decode_addr_const (t1, &value1);
3275 decode_addr_const (t2, &value2);
3277 if (maybe_ne (value1.offset, value2.offset))
3278 return 0;
3280 code = GET_CODE (value1.base);
3281 if (code != GET_CODE (value2.base))
3282 return 0;
3284 switch (code)
3286 case SYMBOL_REF:
3287 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3288 break;
3290 case LABEL_REF:
3291 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3292 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3293 break;
3295 default:
3296 gcc_unreachable ();
3298 return ret;
3301 case PLUS_EXPR:
3302 case POINTER_PLUS_EXPR:
3303 case MINUS_EXPR:
3304 case RANGE_EXPR:
3305 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3306 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3308 CASE_CONVERT:
3309 case VIEW_CONVERT_EXPR:
3310 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3312 default:
3313 return 0;
3316 gcc_unreachable ();
3319 /* Return the section into which constant EXP should be placed. */
3321 static section *
3322 get_constant_section (tree exp, unsigned int align)
3324 return targetm.asm_out.select_section (exp,
3325 compute_reloc_for_constant (exp),
3326 align);
3329 /* Return the size of constant EXP in bytes. */
3331 static HOST_WIDE_INT
3332 get_constant_size (tree exp)
3334 HOST_WIDE_INT size;
3336 size = int_size_in_bytes (TREE_TYPE (exp));
3337 gcc_checking_assert (size >= 0);
3338 gcc_checking_assert (TREE_CODE (exp) != STRING_CST
3339 || size >= TREE_STRING_LENGTH (exp));
3340 return size;
3343 /* Subroutine of output_constant_def:
3344 No constant equal to EXP is known to have been output.
3345 Make a constant descriptor to enter EXP in the hash table.
3346 Assign the label number and construct RTL to refer to the
3347 constant's location in memory.
3348 Caller is responsible for updating the hash table. */
3350 static struct constant_descriptor_tree *
3351 build_constant_desc (tree exp)
3353 struct constant_descriptor_tree *desc;
3354 rtx symbol, rtl;
3355 char label[256];
3356 int labelno;
3357 tree decl;
3359 desc = ggc_alloc<constant_descriptor_tree> ();
3360 desc->value = exp;
3362 /* Create a string containing the label name, in LABEL. */
3363 labelno = const_labelno++;
3364 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3366 /* Construct the VAR_DECL associated with the constant. */
3367 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3368 TREE_TYPE (exp));
3369 DECL_ARTIFICIAL (decl) = 1;
3370 DECL_IGNORED_P (decl) = 1;
3371 TREE_READONLY (decl) = 1;
3372 TREE_STATIC (decl) = 1;
3373 TREE_ADDRESSABLE (decl) = 1;
3374 /* We don't set the RTL yet as this would cause varpool to assume that the
3375 variable is referenced. Moreover, it would just be dropped in LTO mode.
3376 Instead we set the flag that will be recognized in make_decl_rtl. */
3377 DECL_IN_CONSTANT_POOL (decl) = 1;
3378 DECL_INITIAL (decl) = desc->value;
3379 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3380 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3381 if (TREE_CODE (exp) == STRING_CST)
3382 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3383 else
3384 align_variable (decl, 0);
3386 /* Now construct the SYMBOL_REF and the MEM. */
3387 if (use_object_blocks_p ())
3389 int align = (TREE_CODE (decl) == CONST_DECL
3390 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3391 ? DECL_ALIGN (decl)
3392 : symtab_node::get (decl)->definition_alignment ());
3393 section *sect = get_constant_section (exp, align);
3394 symbol = create_block_symbol (ggc_strdup (label),
3395 get_block_for_section (sect), -1);
3397 else
3398 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3399 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3400 SET_SYMBOL_REF_DECL (symbol, decl);
3401 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3403 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3404 set_mem_attributes (rtl, exp, 1);
3405 set_mem_alias_set (rtl, 0);
3407 /* Putting EXP into the literal pool might have imposed a different
3408 alignment which should be visible in the RTX as well. */
3409 set_mem_align (rtl, DECL_ALIGN (decl));
3411 /* We cannot share RTX'es in pool entries.
3412 Mark this piece of RTL as required for unsharing. */
3413 RTX_FLAG (rtl, used) = 1;
3415 /* Set flags or add text to the name to record information, such as
3416 that it is a local symbol. If the name is changed, the macro
3417 ASM_OUTPUT_LABELREF will have to know how to strip this
3418 information. This call might invalidate our local variable
3419 SYMBOL; we can't use it afterward. */
3420 targetm.encode_section_info (exp, rtl, true);
3422 desc->rtl = rtl;
3424 return desc;
3427 /* Return an rtx representing a reference to constant data in memory
3428 for the constant expression EXP.
3430 If assembler code for such a constant has already been output,
3431 return an rtx to refer to it.
3432 Otherwise, output such a constant in memory
3433 and generate an rtx for it.
3435 If DEFER is nonzero, this constant can be deferred and output only
3436 if referenced in the function after all optimizations.
3438 `const_desc_table' records which constants already have label strings. */
3441 output_constant_def (tree exp, int defer)
3443 struct constant_descriptor_tree *desc;
3444 struct constant_descriptor_tree key;
3446 /* Look up EXP in the table of constant descriptors. If we didn't find
3447 it, create a new one. */
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 desc = *loc;
3454 if (desc == 0)
3456 desc = build_constant_desc (exp);
3457 desc->hash = key.hash;
3458 *loc = desc;
3461 maybe_output_constant_def_contents (desc, defer);
3462 return desc->rtl;
3465 /* Subroutine of output_constant_def: Decide whether or not we need to
3466 output the constant DESC now, and if so, do it. */
3467 static void
3468 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3469 int defer)
3471 rtx symbol = XEXP (desc->rtl, 0);
3472 tree exp = desc->value;
3474 if (flag_syntax_only)
3475 return;
3477 if (TREE_ASM_WRITTEN (exp))
3478 /* Already output; don't do it again. */
3479 return;
3481 /* We can always defer constants as long as the context allows
3482 doing so. */
3483 if (defer)
3485 /* Increment n_deferred_constants if it exists. It needs to be at
3486 least as large as the number of constants actually referred to
3487 by the function. If it's too small we'll stop looking too early
3488 and fail to emit constants; if it's too large we'll only look
3489 through the entire function when we could have stopped earlier. */
3490 if (cfun)
3491 n_deferred_constants++;
3492 return;
3495 output_constant_def_contents (symbol);
3498 /* Subroutine of output_constant_def_contents. Output the definition
3499 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3500 constant's alignment in bits. */
3502 static void
3503 assemble_constant_contents (tree exp, const char *label, unsigned int align,
3504 bool merge_strings)
3506 HOST_WIDE_INT size;
3508 size = get_constant_size (exp);
3510 /* Do any machine/system dependent processing of the constant. */
3511 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3513 /* Output the value of EXP. */
3514 output_constant (exp, size, align, false, merge_strings);
3516 targetm.asm_out.decl_end ();
3519 /* We must output the constant data referred to by SYMBOL; do so. */
3521 static void
3522 output_constant_def_contents (rtx symbol)
3524 tree decl = SYMBOL_REF_DECL (symbol);
3525 tree exp = DECL_INITIAL (decl);
3526 bool asan_protected = false;
3528 /* Make sure any other constants whose addresses appear in EXP
3529 are assigned label numbers. */
3530 output_addressed_constants (exp);
3532 /* We are no longer deferring this constant. */
3533 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3535 if ((flag_sanitize & SANITIZE_ADDRESS)
3536 && TREE_CODE (exp) == STRING_CST
3537 && asan_protect_global (exp))
3539 asan_protected = true;
3540 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3541 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3544 /* If the constant is part of an object block, make sure that the
3545 decl has been positioned within its block, but do not write out
3546 its definition yet. output_object_blocks will do that later. */
3547 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3548 place_block_symbol (symbol);
3549 else
3551 int align = (TREE_CODE (decl) == CONST_DECL
3552 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3553 ? DECL_ALIGN (decl)
3554 : symtab_node::get (decl)->definition_alignment ());
3555 section *sect = get_constant_section (exp, align);
3556 switch_to_section (sect);
3557 if (align > BITS_PER_UNIT)
3558 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3559 assemble_constant_contents (exp, XSTR (symbol, 0), align,
3560 (sect->common.flags & SECTION_MERGE)
3561 && (sect->common.flags & SECTION_STRINGS));
3562 if (asan_protected)
3564 HOST_WIDE_INT size = get_constant_size (exp);
3565 assemble_zeros (asan_red_zone_size (size));
3570 /* Look up EXP in the table of constant descriptors. Return the rtl
3571 if it has been emitted, else null. */
3574 lookup_constant_def (tree exp)
3576 struct constant_descriptor_tree key;
3578 key.value = exp;
3579 key.hash = const_hash_1 (exp);
3580 constant_descriptor_tree *desc
3581 = const_desc_htab->find_with_hash (&key, key.hash);
3583 return (desc ? desc->rtl : NULL_RTX);
3586 /* Return a tree representing a reference to constant data in memory
3587 for the constant expression EXP.
3589 This is the counterpart of output_constant_def at the Tree level. */
3591 tree
3592 tree_output_constant_def (tree exp)
3594 struct constant_descriptor_tree *desc, key;
3595 tree decl;
3597 /* Look up EXP in the table of constant descriptors. If we didn't find
3598 it, create a new one. */
3599 key.value = exp;
3600 key.hash = const_hash_1 (exp);
3601 constant_descriptor_tree **loc
3602 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3604 desc = *loc;
3605 if (desc == 0)
3607 desc = build_constant_desc (exp);
3608 desc->hash = key.hash;
3609 *loc = desc;
3612 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3613 varpool_node::finalize_decl (decl);
3614 return decl;
3617 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3618 struct constant_descriptor_rtx *next;
3619 rtx mem;
3620 rtx sym;
3621 rtx constant;
3622 HOST_WIDE_INT offset;
3623 hashval_t hash;
3624 fixed_size_mode mode;
3625 unsigned int align;
3626 int labelno;
3627 int mark;
3630 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3632 static hashval_t hash (constant_descriptor_rtx *);
3633 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3636 /* Used in the hash tables to avoid outputting the same constant
3637 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3638 are output once per function, not once per file. */
3639 /* ??? Only a few targets need per-function constant pools. Most
3640 can use one per-file pool. Should add a targetm bit to tell the
3641 difference. */
3643 struct GTY(()) rtx_constant_pool {
3644 /* Pointers to first and last constant in pool, as ordered by offset. */
3645 struct constant_descriptor_rtx *first;
3646 struct constant_descriptor_rtx *last;
3648 /* Hash facility for making memory-constants from constant rtl-expressions.
3649 It is used on RISC machines where immediate integer arguments and
3650 constant addresses are restricted so that such constants must be stored
3651 in memory. */
3652 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3654 /* Current offset in constant pool (does not include any
3655 machine-specific header). */
3656 HOST_WIDE_INT offset;
3659 /* Hash and compare functions for const_rtx_htab. */
3661 hashval_t
3662 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3664 return desc->hash;
3667 bool
3668 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3669 constant_descriptor_rtx *y)
3671 if (x->mode != y->mode)
3672 return 0;
3673 return rtx_equal_p (x->constant, y->constant);
3676 /* Hash one component of a constant. */
3678 static hashval_t
3679 const_rtx_hash_1 (const_rtx x)
3681 unsigned HOST_WIDE_INT hwi;
3682 machine_mode mode;
3683 enum rtx_code code;
3684 hashval_t h;
3685 int i;
3687 code = GET_CODE (x);
3688 mode = GET_MODE (x);
3689 h = (hashval_t) code * 1048573 + mode;
3691 switch (code)
3693 case CONST_INT:
3694 hwi = INTVAL (x);
3696 fold_hwi:
3698 int shift = sizeof (hashval_t) * CHAR_BIT;
3699 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3701 h ^= (hashval_t) hwi;
3702 for (i = 1; i < n; ++i)
3704 hwi >>= shift;
3705 h ^= (hashval_t) hwi;
3708 break;
3710 case CONST_WIDE_INT:
3711 hwi = 0;
3713 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3714 hwi ^= CONST_WIDE_INT_ELT (x, i);
3715 goto fold_hwi;
3718 case CONST_DOUBLE:
3719 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3721 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3722 goto fold_hwi;
3724 else
3725 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3726 break;
3728 case CONST_FIXED:
3729 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3730 break;
3732 case SYMBOL_REF:
3733 h ^= htab_hash_string (XSTR (x, 0));
3734 break;
3736 case LABEL_REF:
3737 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3738 break;
3740 case UNSPEC:
3741 case UNSPEC_VOLATILE:
3742 h = h * 251 + XINT (x, 1);
3743 break;
3745 default:
3746 break;
3749 return h;
3752 /* Compute a hash value for X, which should be a constant. */
3754 static hashval_t
3755 const_rtx_hash (rtx x)
3757 hashval_t h = 0;
3758 subrtx_iterator::array_type array;
3759 FOR_EACH_SUBRTX (iter, array, x, ALL)
3760 h = h * 509 + const_rtx_hash_1 (*iter);
3761 return h;
3765 /* Create and return a new rtx constant pool. */
3767 static struct rtx_constant_pool *
3768 create_constant_pool (void)
3770 struct rtx_constant_pool *pool;
3772 pool = ggc_alloc<rtx_constant_pool> ();
3773 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3774 pool->first = NULL;
3775 pool->last = NULL;
3776 pool->offset = 0;
3777 return pool;
3780 /* Initialize constant pool hashing for a new function. */
3782 void
3783 init_varasm_status (void)
3785 crtl->varasm.pool = create_constant_pool ();
3786 crtl->varasm.deferred_constants = 0;
3789 /* Given a MINUS expression, simplify it if both sides
3790 include the same symbol. */
3793 simplify_subtraction (rtx x)
3795 rtx r = simplify_rtx (x);
3796 return r ? r : x;
3799 /* Given a constant rtx X, make (or find) a memory constant for its value
3800 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
3801 of X. */
3804 force_const_mem (machine_mode in_mode, rtx x)
3806 struct constant_descriptor_rtx *desc, tmp;
3807 struct rtx_constant_pool *pool;
3808 char label[256];
3809 rtx def, symbol;
3810 hashval_t hash;
3811 unsigned int align;
3812 constant_descriptor_rtx **slot;
3813 fixed_size_mode mode;
3815 /* We can't force variable-sized objects to memory. */
3816 if (!is_a <fixed_size_mode> (in_mode, &mode))
3817 return NULL_RTX;
3819 /* If we're not allowed to drop X into the constant pool, don't. */
3820 if (targetm.cannot_force_const_mem (mode, x))
3821 return NULL_RTX;
3823 /* Record that this function has used a constant pool entry. */
3824 crtl->uses_const_pool = 1;
3826 /* Decide which pool to use. */
3827 pool = (targetm.use_blocks_for_constant_p (mode, x)
3828 ? shared_constant_pool
3829 : crtl->varasm.pool);
3831 /* Lookup the value in the hashtable. */
3832 tmp.constant = x;
3833 tmp.mode = mode;
3834 hash = const_rtx_hash (x);
3835 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3836 desc = *slot;
3838 /* If the constant was already present, return its memory. */
3839 if (desc)
3840 return copy_rtx (desc->mem);
3842 /* Otherwise, create a new descriptor. */
3843 desc = ggc_alloc<constant_descriptor_rtx> ();
3844 *slot = desc;
3846 /* Align the location counter as required by EXP's data type. */
3847 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3848 align = targetm.static_rtx_alignment (align_mode);
3850 pool->offset += (align / BITS_PER_UNIT) - 1;
3851 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3853 desc->next = NULL;
3854 desc->constant = copy_rtx (tmp.constant);
3855 desc->offset = pool->offset;
3856 desc->hash = hash;
3857 desc->mode = mode;
3858 desc->align = align;
3859 desc->labelno = const_labelno;
3860 desc->mark = 0;
3862 pool->offset += GET_MODE_SIZE (mode);
3863 if (pool->last)
3864 pool->last->next = desc;
3865 else
3866 pool->first = pool->last = desc;
3867 pool->last = desc;
3869 /* Create a string containing the label name, in LABEL. */
3870 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3871 ++const_labelno;
3873 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3874 the constants pool. */
3875 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3877 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3878 symbol = create_block_symbol (ggc_strdup (label),
3879 get_block_for_section (sect), -1);
3881 else
3882 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3883 desc->sym = symbol;
3884 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3885 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3886 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3888 /* Construct the MEM. */
3889 desc->mem = def = gen_const_mem (mode, symbol);
3890 set_mem_align (def, align);
3892 /* If we're dropping a label to the constant pool, make sure we
3893 don't delete it. */
3894 if (GET_CODE (x) == LABEL_REF)
3895 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3897 return copy_rtx (def);
3900 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3903 get_pool_constant (const_rtx addr)
3905 return SYMBOL_REF_CONSTANT (addr)->constant;
3908 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3909 and whether it has been output or not. */
3912 get_pool_constant_mark (rtx addr, bool *pmarked)
3914 struct constant_descriptor_rtx *desc;
3916 desc = SYMBOL_REF_CONSTANT (addr);
3917 *pmarked = (desc->mark != 0);
3918 return desc->constant;
3921 /* Similar, return the mode. */
3923 fixed_size_mode
3924 get_pool_mode (const_rtx addr)
3926 return SYMBOL_REF_CONSTANT (addr)->mode;
3929 /* Return TRUE if and only if the constant pool has no entries. Note
3930 that even entries we might end up choosing not to emit are counted
3931 here, so there is the potential for missed optimizations. */
3933 bool
3934 constant_pool_empty_p (void)
3936 return crtl->varasm.pool->first == NULL;
3939 /* Worker function for output_constant_pool_1. Emit assembly for X
3940 in MODE with known alignment ALIGN. */
3942 static void
3943 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
3945 switch (GET_MODE_CLASS (mode))
3947 case MODE_FLOAT:
3948 case MODE_DECIMAL_FLOAT:
3950 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3951 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
3952 as_a <scalar_float_mode> (mode), align, false);
3953 break;
3956 case MODE_INT:
3957 case MODE_PARTIAL_INT:
3958 case MODE_FRACT:
3959 case MODE_UFRACT:
3960 case MODE_ACCUM:
3961 case MODE_UACCUM:
3962 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3963 break;
3965 case MODE_VECTOR_BOOL:
3967 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3969 /* Pick the smallest integer mode that contains at least one
3970 whole element. Often this is byte_mode and contains more
3971 than one element. */
3972 unsigned int nelts = GET_MODE_NUNITS (mode);
3973 unsigned int elt_bits = GET_MODE_BITSIZE (mode) / nelts;
3974 unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
3975 scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
3977 /* Build the constant up one integer at a time. */
3978 unsigned int elts_per_int = int_bits / elt_bits;
3979 for (unsigned int i = 0; i < nelts; i += elts_per_int)
3981 unsigned HOST_WIDE_INT value = 0;
3982 unsigned int limit = MIN (nelts - i, elts_per_int);
3983 for (unsigned int j = 0; j < limit; ++j)
3984 if (INTVAL (CONST_VECTOR_ELT (x, i + j)) != 0)
3985 value |= 1 << (j * elt_bits);
3986 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
3987 i != 0 ? MIN (align, int_bits) : align);
3989 break;
3991 case MODE_VECTOR_FLOAT:
3992 case MODE_VECTOR_INT:
3993 case MODE_VECTOR_FRACT:
3994 case MODE_VECTOR_UFRACT:
3995 case MODE_VECTOR_ACCUM:
3996 case MODE_VECTOR_UACCUM:
3998 int i, units;
3999 scalar_mode submode = GET_MODE_INNER (mode);
4000 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4002 gcc_assert (GET_CODE (x) == CONST_VECTOR);
4003 units = GET_MODE_NUNITS (mode);
4005 for (i = 0; i < units; i++)
4007 rtx elt = CONST_VECTOR_ELT (x, i);
4008 output_constant_pool_2 (submode, elt, i ? subalign : align);
4011 break;
4013 default:
4014 gcc_unreachable ();
4018 /* Worker function for output_constant_pool. Emit constant DESC,
4019 giving it ALIGN bits of alignment. */
4021 static void
4022 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
4023 unsigned int align)
4025 rtx x, tmp;
4027 x = desc->constant;
4029 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4030 whose CODE_LABEL has been deleted. This can occur if a jump table
4031 is eliminated by optimization. If so, write a constant of zero
4032 instead. Note that this can also happen by turning the
4033 CODE_LABEL into a NOTE. */
4034 /* ??? This seems completely and utterly wrong. Certainly it's
4035 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4036 functioning even with rtx_insn::deleted and friends. */
4038 tmp = x;
4039 switch (GET_CODE (tmp))
4041 case CONST:
4042 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4043 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4044 break;
4045 tmp = XEXP (XEXP (tmp, 0), 0);
4046 /* FALLTHRU */
4048 case LABEL_REF:
4050 rtx_insn *insn = label_ref_label (tmp);
4051 gcc_assert (!insn->deleted ());
4052 gcc_assert (!NOTE_P (insn)
4053 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4054 break;
4057 default:
4058 break;
4061 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4062 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4063 align, desc->labelno, done);
4064 #endif
4066 assemble_align (align);
4068 /* Output the label. */
4069 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4071 /* Output the data.
4072 Pass actual alignment value while emitting string constant to asm code
4073 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4074 assuming that the data is already aligned which prevents the generation
4075 of fix-up table entries. */
4076 output_constant_pool_2 (desc->mode, x, desc->align);
4078 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4079 sections have proper size. */
4080 if (align > GET_MODE_BITSIZE (desc->mode)
4081 && in_section
4082 && (in_section->common.flags & SECTION_MERGE))
4083 assemble_align (align);
4085 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4086 done:
4087 #endif
4088 return;
4091 /* Recompute the offsets of entries in POOL, and the overall size of
4092 POOL. Do this after calling mark_constant_pool to ensure that we
4093 are computing the offset values for the pool which we will actually
4094 emit. */
4096 static void
4097 recompute_pool_offsets (struct rtx_constant_pool *pool)
4099 struct constant_descriptor_rtx *desc;
4100 pool->offset = 0;
4102 for (desc = pool->first; desc ; desc = desc->next)
4103 if (desc->mark)
4105 /* Recalculate offset. */
4106 unsigned int align = desc->align;
4107 pool->offset += (align / BITS_PER_UNIT) - 1;
4108 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4109 desc->offset = pool->offset;
4110 pool->offset += GET_MODE_SIZE (desc->mode);
4114 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4115 Emit referenced deferred strings. */
4117 static void
4118 mark_constants_in_pattern (rtx insn)
4120 subrtx_iterator::array_type array;
4121 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4123 const_rtx x = *iter;
4124 if (GET_CODE (x) == SYMBOL_REF)
4126 if (CONSTANT_POOL_ADDRESS_P (x))
4128 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4129 if (desc->mark == 0)
4131 desc->mark = 1;
4132 iter.substitute (desc->constant);
4135 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4137 tree decl = SYMBOL_REF_DECL (x);
4138 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4140 n_deferred_constants--;
4141 output_constant_def_contents (CONST_CAST_RTX (x));
4148 /* Look through appropriate parts of INSN, marking all entries in the
4149 constant pool which are actually being used. Entries that are only
4150 referenced by other constants are also marked as used. Emit
4151 deferred strings that are used. */
4153 static void
4154 mark_constants (rtx_insn *insn)
4156 if (!INSN_P (insn))
4157 return;
4159 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4160 insns, not any notes that may be attached. We don't want to mark
4161 a constant just because it happens to appear in a REG_EQUIV note. */
4162 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4164 int i, n = seq->len ();
4165 for (i = 0; i < n; ++i)
4167 rtx subinsn = seq->element (i);
4168 if (INSN_P (subinsn))
4169 mark_constants_in_pattern (subinsn);
4172 else
4173 mark_constants_in_pattern (insn);
4176 /* Look through the instructions for this function, and mark all the
4177 entries in POOL which are actually being used. Emit deferred constants
4178 which have indeed been used. */
4180 static void
4181 mark_constant_pool (void)
4183 rtx_insn *insn;
4185 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4186 return;
4188 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4189 mark_constants (insn);
4192 /* Write all the constants in POOL. */
4194 static void
4195 output_constant_pool_contents (struct rtx_constant_pool *pool)
4197 struct constant_descriptor_rtx *desc;
4199 for (desc = pool->first; desc ; desc = desc->next)
4200 if (desc->mark)
4202 /* If the constant is part of an object_block, make sure that
4203 the constant has been positioned within its block, but do not
4204 write out its definition yet. output_object_blocks will do
4205 that later. */
4206 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4207 && SYMBOL_REF_BLOCK (desc->sym))
4208 place_block_symbol (desc->sym);
4209 else
4211 switch_to_section (targetm.asm_out.select_rtx_section
4212 (desc->mode, desc->constant, desc->align));
4213 output_constant_pool_1 (desc, desc->align);
4218 /* Mark all constants that are used in the current function, then write
4219 out the function's private constant pool. */
4221 static void
4222 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4223 tree fndecl ATTRIBUTE_UNUSED)
4225 struct rtx_constant_pool *pool = crtl->varasm.pool;
4227 /* It is possible for gcc to call force_const_mem and then to later
4228 discard the instructions which refer to the constant. In such a
4229 case we do not need to output the constant. */
4230 mark_constant_pool ();
4232 /* Having marked the constant pool entries we'll actually emit, we
4233 now need to rebuild the offset information, which may have become
4234 stale. */
4235 recompute_pool_offsets (pool);
4237 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4238 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4239 #endif
4241 output_constant_pool_contents (pool);
4243 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4244 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4245 #endif
4248 /* Write the contents of the shared constant pool. */
4250 void
4251 output_shared_constant_pool (void)
4253 output_constant_pool_contents (shared_constant_pool);
4256 /* Determine what kind of relocations EXP may need. */
4259 compute_reloc_for_constant (tree exp)
4261 int reloc = 0, reloc2;
4262 tree tem;
4264 switch (TREE_CODE (exp))
4266 case ADDR_EXPR:
4267 case FDESC_EXPR:
4268 /* Go inside any operations that get_inner_reference can handle and see
4269 if what's inside is a constant: no need to do anything here for
4270 addresses of variables or functions. */
4271 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4272 tem = TREE_OPERAND (tem, 0))
4275 if (TREE_CODE (tem) == MEM_REF
4276 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4278 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4279 break;
4282 if (!targetm.binds_local_p (tem))
4283 reloc |= 2;
4284 else
4285 reloc |= 1;
4286 break;
4288 case PLUS_EXPR:
4289 case POINTER_PLUS_EXPR:
4290 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4291 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4292 break;
4294 case MINUS_EXPR:
4295 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4296 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4297 /* The difference of two local labels is computable at link time. */
4298 if (reloc == 1 && reloc2 == 1)
4299 reloc = 0;
4300 else
4301 reloc |= reloc2;
4302 break;
4304 CASE_CONVERT:
4305 case VIEW_CONVERT_EXPR:
4306 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4307 break;
4309 case CONSTRUCTOR:
4311 unsigned HOST_WIDE_INT idx;
4312 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4313 if (tem != 0)
4314 reloc |= compute_reloc_for_constant (tem);
4316 break;
4318 default:
4319 break;
4321 return reloc;
4324 /* Find all the constants whose addresses are referenced inside of EXP,
4325 and make sure assembler code with a label has been output for each one.
4326 Indicate whether an ADDR_EXPR has been encountered. */
4328 static void
4329 output_addressed_constants (tree exp)
4331 tree tem;
4333 switch (TREE_CODE (exp))
4335 case ADDR_EXPR:
4336 case FDESC_EXPR:
4337 /* Go inside any operations that get_inner_reference can handle and see
4338 if what's inside is a constant: no need to do anything here for
4339 addresses of variables or functions. */
4340 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4341 tem = TREE_OPERAND (tem, 0))
4344 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4345 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4346 tem = DECL_INITIAL (tem);
4348 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4349 output_constant_def (tem, 0);
4351 if (TREE_CODE (tem) == MEM_REF)
4352 output_addressed_constants (TREE_OPERAND (tem, 0));
4353 break;
4355 case PLUS_EXPR:
4356 case POINTER_PLUS_EXPR:
4357 case MINUS_EXPR:
4358 output_addressed_constants (TREE_OPERAND (exp, 1));
4359 gcc_fallthrough ();
4361 CASE_CONVERT:
4362 case VIEW_CONVERT_EXPR:
4363 output_addressed_constants (TREE_OPERAND (exp, 0));
4364 break;
4366 case CONSTRUCTOR:
4368 unsigned HOST_WIDE_INT idx;
4369 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4370 if (tem != 0)
4371 output_addressed_constants (tem);
4373 break;
4375 default:
4376 break;
4380 /* Whether a constructor CTOR is a valid static constant initializer if all
4381 its elements are. This used to be internal to initializer_constant_valid_p
4382 and has been exposed to let other functions like categorize_ctor_elements
4383 evaluate the property while walking a constructor for other purposes. */
4385 bool
4386 constructor_static_from_elts_p (const_tree ctor)
4388 return (TREE_CONSTANT (ctor)
4389 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4390 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4391 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4394 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4395 tree *cache);
4397 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4398 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4399 which are valid when ENDTYPE is an integer of any size; in
4400 particular, this does not accept a pointer minus a constant. This
4401 returns null_pointer_node if the VALUE is an absolute constant
4402 which can be used to initialize a static variable. Otherwise it
4403 returns NULL. */
4405 static tree
4406 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4408 tree op0, op1;
4410 if (!INTEGRAL_TYPE_P (endtype))
4411 return NULL_TREE;
4413 op0 = TREE_OPERAND (value, 0);
4414 op1 = TREE_OPERAND (value, 1);
4416 /* Like STRIP_NOPS except allow the operand mode to widen. This
4417 works around a feature of fold that simplifies (int)(p1 - p2) to
4418 ((int)p1 - (int)p2) under the theory that the narrower operation
4419 is cheaper. */
4421 while (CONVERT_EXPR_P (op0)
4422 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4424 tree inner = TREE_OPERAND (op0, 0);
4425 if (inner == error_mark_node
4426 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4427 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4428 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4429 break;
4430 op0 = inner;
4433 while (CONVERT_EXPR_P (op1)
4434 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4436 tree inner = TREE_OPERAND (op1, 0);
4437 if (inner == error_mark_node
4438 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4439 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4440 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4441 break;
4442 op1 = inner;
4445 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4446 if (!op0)
4447 return NULL_TREE;
4449 op1 = initializer_constant_valid_p_1 (op1, endtype,
4450 cache ? cache + 2 : NULL);
4451 /* Both initializers must be known. */
4452 if (op1)
4454 if (op0 == op1
4455 && (op0 == null_pointer_node
4456 || TREE_CODE (value) == MINUS_EXPR))
4457 return null_pointer_node;
4459 /* Support differences between labels. */
4460 if (TREE_CODE (op0) == LABEL_DECL
4461 && TREE_CODE (op1) == LABEL_DECL)
4462 return null_pointer_node;
4464 if (TREE_CODE (op0) == STRING_CST
4465 && TREE_CODE (op1) == STRING_CST
4466 && operand_equal_p (op0, op1, 1))
4467 return null_pointer_node;
4470 return NULL_TREE;
4473 /* Helper function of initializer_constant_valid_p.
4474 Return nonzero if VALUE is a valid constant-valued expression
4475 for use in initializing a static variable; one that can be an
4476 element of a "constant" initializer.
4478 Return null_pointer_node if the value is absolute;
4479 if it is relocatable, return the variable that determines the relocation.
4480 We assume that VALUE has been folded as much as possible;
4481 therefore, we do not need to check for such things as
4482 arithmetic-combinations of integers.
4484 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4486 static tree
4487 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4489 tree ret;
4491 switch (TREE_CODE (value))
4493 case CONSTRUCTOR:
4494 if (constructor_static_from_elts_p (value))
4496 unsigned HOST_WIDE_INT idx;
4497 tree elt;
4498 bool absolute = true;
4500 if (cache && cache[0] == value)
4501 return cache[1];
4502 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4504 tree reloc;
4505 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4506 NULL);
4507 if (!reloc
4508 /* An absolute value is required with reverse SSO. */
4509 || (reloc != null_pointer_node
4510 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4511 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4513 if (cache)
4515 cache[0] = value;
4516 cache[1] = NULL_TREE;
4518 return NULL_TREE;
4520 if (reloc != null_pointer_node)
4521 absolute = false;
4523 /* For a non-absolute relocation, there is no single
4524 variable that can be "the variable that determines the
4525 relocation." */
4526 if (cache)
4528 cache[0] = value;
4529 cache[1] = absolute ? null_pointer_node : error_mark_node;
4531 return absolute ? null_pointer_node : error_mark_node;
4534 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4536 case INTEGER_CST:
4537 case VECTOR_CST:
4538 case REAL_CST:
4539 case FIXED_CST:
4540 case STRING_CST:
4541 case COMPLEX_CST:
4542 return null_pointer_node;
4544 case ADDR_EXPR:
4545 case FDESC_EXPR:
4547 tree op0 = staticp (TREE_OPERAND (value, 0));
4548 if (op0)
4550 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4551 to be a constant, this is old-skool offsetof-like nonsense. */
4552 if (TREE_CODE (op0) == INDIRECT_REF
4553 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4554 return null_pointer_node;
4555 /* Taking the address of a nested function involves a trampoline,
4556 unless we don't need or want one. */
4557 if (TREE_CODE (op0) == FUNCTION_DECL
4558 && DECL_STATIC_CHAIN (op0)
4559 && !TREE_NO_TRAMPOLINE (value))
4560 return NULL_TREE;
4561 /* "&{...}" requires a temporary to hold the constructed
4562 object. */
4563 if (TREE_CODE (op0) == CONSTRUCTOR)
4564 return NULL_TREE;
4566 return op0;
4569 case NON_LVALUE_EXPR:
4570 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4571 endtype, cache);
4573 case VIEW_CONVERT_EXPR:
4575 tree src = TREE_OPERAND (value, 0);
4576 tree src_type = TREE_TYPE (src);
4577 tree dest_type = TREE_TYPE (value);
4579 /* Allow view-conversions from aggregate to non-aggregate type only
4580 if the bit pattern is fully preserved afterwards; otherwise, the
4581 RTL expander won't be able to apply a subsequent transformation
4582 to the underlying constructor. */
4583 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4585 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4586 return initializer_constant_valid_p_1 (src, endtype, cache);
4587 else
4588 return NULL_TREE;
4591 /* Allow all other kinds of view-conversion. */
4592 return initializer_constant_valid_p_1 (src, endtype, cache);
4595 CASE_CONVERT:
4597 tree src = TREE_OPERAND (value, 0);
4598 tree src_type = TREE_TYPE (src);
4599 tree dest_type = TREE_TYPE (value);
4601 /* Allow conversions between pointer types, floating-point
4602 types, and offset types. */
4603 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4604 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4605 || (TREE_CODE (dest_type) == OFFSET_TYPE
4606 && TREE_CODE (src_type) == OFFSET_TYPE))
4607 return initializer_constant_valid_p_1 (src, endtype, cache);
4609 /* Allow length-preserving conversions between integer types. */
4610 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4611 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4612 return initializer_constant_valid_p_1 (src, endtype, cache);
4614 /* Allow conversions between other integer types only if
4615 explicit value. Don't allow sign-extension to a type larger
4616 than word and pointer, there aren't relocations that would
4617 allow to sign extend it to a wider type. */
4618 if (INTEGRAL_TYPE_P (dest_type)
4619 && INTEGRAL_TYPE_P (src_type)
4620 && (TYPE_UNSIGNED (src_type)
4621 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4622 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4623 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4625 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4626 if (inner == null_pointer_node)
4627 return null_pointer_node;
4628 break;
4631 /* Allow (int) &foo provided int is as wide as a pointer. */
4632 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4633 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4634 return initializer_constant_valid_p_1 (src, endtype, cache);
4636 /* Likewise conversions from int to pointers, but also allow
4637 conversions from 0. */
4638 if ((POINTER_TYPE_P (dest_type)
4639 || TREE_CODE (dest_type) == OFFSET_TYPE)
4640 && INTEGRAL_TYPE_P (src_type))
4642 if (TREE_CODE (src) == INTEGER_CST
4643 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4644 return null_pointer_node;
4645 if (integer_zerop (src))
4646 return null_pointer_node;
4647 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4648 return initializer_constant_valid_p_1 (src, endtype, cache);
4651 /* Allow conversions to struct or union types if the value
4652 inside is okay. */
4653 if (TREE_CODE (dest_type) == RECORD_TYPE
4654 || TREE_CODE (dest_type) == UNION_TYPE)
4655 return initializer_constant_valid_p_1 (src, endtype, cache);
4657 break;
4659 case POINTER_PLUS_EXPR:
4660 case PLUS_EXPR:
4661 /* Any valid floating-point constants will have been folded by now;
4662 with -frounding-math we hit this with addition of two constants. */
4663 if (TREE_CODE (endtype) == REAL_TYPE)
4664 return NULL_TREE;
4665 if (cache && cache[0] == value)
4666 return cache[1];
4667 if (! INTEGRAL_TYPE_P (endtype)
4668 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4670 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4671 tree valid0
4672 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4673 endtype, ncache);
4674 tree valid1
4675 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4676 endtype, ncache + 2);
4677 /* If either term is absolute, use the other term's relocation. */
4678 if (valid0 == null_pointer_node)
4679 ret = valid1;
4680 else if (valid1 == null_pointer_node)
4681 ret = valid0;
4682 /* Support narrowing pointer differences. */
4683 else
4684 ret = narrowing_initializer_constant_valid_p (value, endtype,
4685 ncache);
4687 else
4688 /* Support narrowing pointer differences. */
4689 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4690 if (cache)
4692 cache[0] = value;
4693 cache[1] = ret;
4695 return ret;
4697 case POINTER_DIFF_EXPR:
4698 case MINUS_EXPR:
4699 if (TREE_CODE (endtype) == REAL_TYPE)
4700 return NULL_TREE;
4701 if (cache && cache[0] == value)
4702 return cache[1];
4703 if (! INTEGRAL_TYPE_P (endtype)
4704 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4706 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4707 tree valid0
4708 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4709 endtype, ncache);
4710 tree valid1
4711 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4712 endtype, ncache + 2);
4713 /* Win if second argument is absolute. */
4714 if (valid1 == null_pointer_node)
4715 ret = valid0;
4716 /* Win if both arguments have the same relocation.
4717 Then the value is absolute. */
4718 else if (valid0 == valid1 && valid0 != 0)
4719 ret = null_pointer_node;
4720 /* Since GCC guarantees that string constants are unique in the
4721 generated code, a subtraction between two copies of the same
4722 constant string is absolute. */
4723 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4724 && valid1 && TREE_CODE (valid1) == STRING_CST
4725 && operand_equal_p (valid0, valid1, 1))
4726 ret = null_pointer_node;
4727 /* Support narrowing differences. */
4728 else
4729 ret = narrowing_initializer_constant_valid_p (value, endtype,
4730 ncache);
4732 else
4733 /* Support narrowing differences. */
4734 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4735 if (cache)
4737 cache[0] = value;
4738 cache[1] = ret;
4740 return ret;
4742 default:
4743 break;
4746 return NULL_TREE;
4749 /* Return nonzero if VALUE is a valid constant-valued expression
4750 for use in initializing a static variable; one that can be an
4751 element of a "constant" initializer.
4753 Return null_pointer_node if the value is absolute;
4754 if it is relocatable, return the variable that determines the relocation.
4755 We assume that VALUE has been folded as much as possible;
4756 therefore, we do not need to check for such things as
4757 arithmetic-combinations of integers. */
4758 tree
4759 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
4761 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
4763 /* An absolute value is required with reverse storage order. */
4764 if (reloc
4765 && reloc != null_pointer_node
4766 && reverse
4767 && !AGGREGATE_TYPE_P (endtype)
4768 && !VECTOR_TYPE_P (endtype))
4769 reloc = NULL_TREE;
4771 return reloc;
4774 /* Return true if VALUE is a valid constant-valued expression
4775 for use in initializing a static bit-field; one that can be
4776 an element of a "constant" initializer. */
4778 bool
4779 initializer_constant_valid_for_bitfield_p (tree value)
4781 /* For bitfields we support integer constants or possibly nested aggregates
4782 of such. */
4783 switch (TREE_CODE (value))
4785 case CONSTRUCTOR:
4787 unsigned HOST_WIDE_INT idx;
4788 tree elt;
4790 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4791 if (!initializer_constant_valid_for_bitfield_p (elt))
4792 return false;
4793 return true;
4796 case INTEGER_CST:
4797 case REAL_CST:
4798 return true;
4800 case VIEW_CONVERT_EXPR:
4801 case NON_LVALUE_EXPR:
4802 return
4803 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4805 default:
4806 break;
4809 return false;
4812 /* Check if a STRING_CST fits into the field.
4813 Tolerate only the case when the NUL termination
4814 does not fit into the field. */
4816 static bool
4817 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
4819 tree type = TREE_TYPE (string);
4820 tree eltype = TREE_TYPE (type);
4821 unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
4822 unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
4823 int len = TREE_STRING_LENGTH (string);
4825 if (elts != 1 && elts != 2 && elts != 4)
4826 return false;
4827 if (len < 0 || len % elts != 0)
4828 return false;
4829 if (size < (unsigned)len)
4830 return false;
4831 if (mem_size != size)
4832 return false;
4833 return true;
4836 /* output_constructor outer state of relevance in recursive calls, typically
4837 for nested aggregate bitfields. */
4839 struct oc_outer_state {
4840 unsigned int bit_offset; /* current position in ... */
4841 int byte; /* ... the outer byte buffer. */
4844 static unsigned HOST_WIDE_INT
4845 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
4846 oc_outer_state *);
4848 /* Output assembler code for constant EXP, with no label.
4849 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4850 Assumes output_addressed_constants has been done on EXP already.
4852 Generate at least SIZE bytes of assembler data, padding at the end
4853 with zeros if necessary. SIZE must always be specified. The returned
4854 value is the actual number of bytes of assembler data generated, which
4855 may be bigger than SIZE if the object contains a variable length field.
4857 SIZE is important for structure constructors,
4858 since trailing members may have been omitted from the constructor.
4859 It is also important for initialization of arrays from string constants
4860 since the full length of the string constant might not be wanted.
4861 It is also needed for initialization of unions, where the initializer's
4862 type is just one member, and that may not be as long as the union.
4864 There a case in which we would fail to output exactly SIZE bytes:
4865 for a structure constructor that wants to produce more than SIZE bytes.
4866 But such constructors will never be generated for any possible input.
4868 ALIGN is the alignment of the data in bits.
4870 If REVERSE is true, EXP is output in reverse storage order. */
4872 static unsigned HOST_WIDE_INT
4873 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
4874 bool reverse, bool merge_strings)
4876 enum tree_code code;
4877 unsigned HOST_WIDE_INT thissize;
4878 rtx cst;
4880 if (size == 0 || flag_syntax_only)
4881 return size;
4883 /* See if we're trying to initialize a pointer in a non-default mode
4884 to the address of some declaration somewhere. If the target says
4885 the mode is valid for pointers, assume the target has a way of
4886 resolving it. */
4887 if (TREE_CODE (exp) == NOP_EXPR
4888 && POINTER_TYPE_P (TREE_TYPE (exp))
4889 && targetm.addr_space.valid_pointer_mode
4890 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4891 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4893 tree saved_type = TREE_TYPE (exp);
4895 /* Peel off any intermediate conversions-to-pointer for valid
4896 pointer modes. */
4897 while (TREE_CODE (exp) == NOP_EXPR
4898 && POINTER_TYPE_P (TREE_TYPE (exp))
4899 && targetm.addr_space.valid_pointer_mode
4900 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4901 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4902 exp = TREE_OPERAND (exp, 0);
4904 /* If what we're left with is the address of something, we can
4905 convert the address to the final type and output it that
4906 way. */
4907 if (TREE_CODE (exp) == ADDR_EXPR)
4908 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4909 /* Likewise for constant ints. */
4910 else if (TREE_CODE (exp) == INTEGER_CST)
4911 exp = fold_convert (saved_type, exp);
4915 /* Eliminate any conversions since we'll be outputting the underlying
4916 constant. */
4917 while (CONVERT_EXPR_P (exp)
4918 || TREE_CODE (exp) == NON_LVALUE_EXPR
4919 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4921 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4922 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4924 /* Make sure eliminating the conversion is really a no-op, except with
4925 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4926 union types to allow for Ada unchecked unions. */
4927 if (type_size > op_size
4928 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4929 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4930 /* Keep the conversion. */
4931 break;
4932 else
4933 exp = TREE_OPERAND (exp, 0);
4936 code = TREE_CODE (TREE_TYPE (exp));
4937 thissize = int_size_in_bytes (TREE_TYPE (exp));
4939 /* Allow a constructor with no elements for any data type.
4940 This means to fill the space with zeros. */
4941 if (TREE_CODE (exp) == CONSTRUCTOR
4942 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4944 assemble_zeros (size);
4945 return size;
4948 if (TREE_CODE (exp) == FDESC_EXPR)
4950 #ifdef ASM_OUTPUT_FDESC
4951 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4952 tree decl = TREE_OPERAND (exp, 0);
4953 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4954 #else
4955 gcc_unreachable ();
4956 #endif
4957 return size;
4960 /* Now output the underlying data. If we've handling the padding, return.
4961 Otherwise, break and ensure SIZE is the size written. */
4962 switch (code)
4964 case BOOLEAN_TYPE:
4965 case INTEGER_TYPE:
4966 case ENUMERAL_TYPE:
4967 case POINTER_TYPE:
4968 case REFERENCE_TYPE:
4969 case OFFSET_TYPE:
4970 case FIXED_POINT_TYPE:
4971 case NULLPTR_TYPE:
4972 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
4973 if (reverse)
4974 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
4975 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
4976 error ("initializer for integer/fixed-point value is too complicated");
4977 break;
4979 case REAL_TYPE:
4980 if (TREE_CODE (exp) != REAL_CST)
4981 error ("initializer for floating value is not a floating constant");
4982 else
4983 assemble_real (TREE_REAL_CST (exp),
4984 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
4985 align, reverse);
4986 break;
4988 case COMPLEX_TYPE:
4989 output_constant (TREE_REALPART (exp), thissize / 2, align,
4990 reverse, false);
4991 output_constant (TREE_IMAGPART (exp), thissize / 2,
4992 min_align (align, BITS_PER_UNIT * (thissize / 2)),
4993 reverse, false);
4994 break;
4996 case ARRAY_TYPE:
4997 case VECTOR_TYPE:
4998 switch (TREE_CODE (exp))
5000 case CONSTRUCTOR:
5001 return output_constructor (exp, size, align, reverse, NULL);
5002 case STRING_CST:
5003 thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5004 if (merge_strings
5005 && (thissize == 0
5006 || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5007 thissize++;
5008 gcc_checking_assert (check_string_literal (exp, size));
5009 assemble_string (TREE_STRING_POINTER (exp), thissize);
5010 break;
5011 case VECTOR_CST:
5013 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5014 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5015 int elt_size = GET_MODE_SIZE (inner);
5016 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5017 reverse, false);
5018 thissize = elt_size;
5019 /* Static constants must have a fixed size. */
5020 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5021 for (unsigned int i = 1; i < nunits; i++)
5023 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5024 reverse, false);
5025 thissize += elt_size;
5027 break;
5029 default:
5030 gcc_unreachable ();
5032 break;
5034 case RECORD_TYPE:
5035 case UNION_TYPE:
5036 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5037 return output_constructor (exp, size, align, reverse, NULL);
5039 case ERROR_MARK:
5040 return 0;
5042 default:
5043 gcc_unreachable ();
5046 if (size > thissize)
5047 assemble_zeros (size - thissize);
5049 return size;
5052 /* Subroutine of output_constructor, used for computing the size of
5053 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
5054 type with an unspecified upper bound. */
5056 static unsigned HOST_WIDE_INT
5057 array_size_for_constructor (tree val)
5059 tree max_index;
5060 unsigned HOST_WIDE_INT cnt;
5061 tree index, value, tmp;
5062 offset_int i;
5064 /* This code used to attempt to handle string constants that are not
5065 arrays of single-bytes, but nothing else does, so there's no point in
5066 doing it here. */
5067 if (TREE_CODE (val) == STRING_CST)
5068 return TREE_STRING_LENGTH (val);
5070 max_index = NULL_TREE;
5071 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5073 if (TREE_CODE (index) == RANGE_EXPR)
5074 index = TREE_OPERAND (index, 1);
5075 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5076 max_index = index;
5079 if (max_index == NULL_TREE)
5080 return 0;
5082 /* Compute the total number of array elements. */
5083 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5084 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5086 /* Multiply by the array element unit size to find number of bytes. */
5087 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5089 gcc_assert (wi::fits_uhwi_p (i));
5090 return i.to_uhwi ();
5093 /* Other datastructures + helpers for output_constructor. */
5095 /* output_constructor local state to support interaction with helpers. */
5097 struct oc_local_state {
5099 /* Received arguments. */
5100 tree exp; /* Constructor expression. */
5101 tree type; /* Type of constructor expression. */
5102 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5103 unsigned int align; /* Known initial alignment. */
5104 tree min_index; /* Lower bound if specified for an array. */
5106 /* Output processing state. */
5107 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5108 int byte; /* Part of a bitfield byte yet to be output. */
5109 int last_relative_index; /* Implicit or explicit index of the last
5110 array element output within a bitfield. */
5111 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5112 bool reverse; /* Whether reverse storage order is in use. */
5114 /* Current element. */
5115 tree field; /* Current field decl in a record. */
5116 tree val; /* Current element value. */
5117 tree index; /* Current element index. */
5121 /* Helper for output_constructor. From the current LOCAL state, output a
5122 RANGE_EXPR element. */
5124 static void
5125 output_constructor_array_range (oc_local_state *local)
5127 unsigned HOST_WIDE_INT fieldsize
5128 = int_size_in_bytes (TREE_TYPE (local->type));
5130 HOST_WIDE_INT lo_index
5131 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5132 HOST_WIDE_INT hi_index
5133 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5134 HOST_WIDE_INT index;
5136 unsigned int align2
5137 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5139 for (index = lo_index; index <= hi_index; index++)
5141 /* Output the element's initial value. */
5142 if (local->val == NULL_TREE)
5143 assemble_zeros (fieldsize);
5144 else
5145 fieldsize = output_constant (local->val, fieldsize, align2,
5146 local->reverse, false);
5148 /* Count its size. */
5149 local->total_bytes += fieldsize;
5153 /* Helper for output_constructor. From the current LOCAL state, output a
5154 field element that is not true bitfield or part of an outer one. */
5156 static void
5157 output_constructor_regular_field (oc_local_state *local)
5159 /* Field size and position. Since this structure is static, we know the
5160 positions are constant. */
5161 unsigned HOST_WIDE_INT fieldsize;
5162 HOST_WIDE_INT fieldpos;
5164 unsigned int align2;
5166 /* Output any buffered-up bit-fields preceding this element. */
5167 if (local->byte_buffer_in_use)
5169 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5170 local->total_bytes++;
5171 local->byte_buffer_in_use = false;
5174 if (local->index != NULL_TREE)
5176 /* Perform the index calculation in modulo arithmetic but
5177 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5178 but we are using an unsigned sizetype. */
5179 unsigned prec = TYPE_PRECISION (sizetype);
5180 offset_int idx = wi::sext (wi::to_offset (local->index)
5181 - wi::to_offset (local->min_index), prec);
5182 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5183 .to_short_addr ();
5185 else if (local->field != NULL_TREE)
5186 fieldpos = int_byte_position (local->field);
5187 else
5188 fieldpos = 0;
5190 /* Advance to offset of this element.
5191 Note no alignment needed in an array, since that is guaranteed
5192 if each element has the proper size. */
5193 if (local->field != NULL_TREE || local->index != NULL_TREE)
5195 if (fieldpos > local->total_bytes)
5197 assemble_zeros (fieldpos - local->total_bytes);
5198 local->total_bytes = fieldpos;
5200 else
5201 /* Must not go backwards. */
5202 gcc_assert (fieldpos == local->total_bytes);
5205 /* Find the alignment of this element. */
5206 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5208 /* Determine size this element should occupy. */
5209 if (local->field)
5211 fieldsize = 0;
5213 /* If this is an array with an unspecified upper bound,
5214 the initializer determines the size. */
5215 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5216 but we cannot do this until the deprecated support for
5217 initializing zero-length array members is removed. */
5218 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5219 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5220 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5222 fieldsize = array_size_for_constructor (local->val);
5223 /* Given a non-empty initialization, this field had better
5224 be last. Given a flexible array member, the next field
5225 on the chain is a TYPE_DECL of the enclosing struct. */
5226 const_tree next = DECL_CHAIN (local->field);
5227 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5228 tree size = TYPE_SIZE_UNIT (TREE_TYPE (local->val));
5229 gcc_checking_assert (compare_tree_int (size, fieldsize) == 0);
5231 else
5232 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5234 else
5235 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5237 /* Output the element's initial value. */
5238 if (local->val == NULL_TREE)
5239 assemble_zeros (fieldsize);
5240 else
5241 fieldsize = output_constant (local->val, fieldsize, align2,
5242 local->reverse, false);
5244 /* Count its size. */
5245 local->total_bytes += fieldsize;
5248 /* Helper for output_constructor. From the LOCAL state, output an element
5249 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5250 from the start of a possibly ongoing outer byte buffer. */
5252 static void
5253 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5255 /* Bit size of this element. */
5256 HOST_WIDE_INT ebitsize
5257 = (local->field
5258 ? tree_to_uhwi (DECL_SIZE (local->field))
5259 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5261 /* Relative index of this element if this is an array component. */
5262 HOST_WIDE_INT relative_index
5263 = (!local->field
5264 ? (local->index
5265 ? (tree_to_shwi (local->index)
5266 - tree_to_shwi (local->min_index))
5267 : local->last_relative_index + 1)
5268 : 0);
5270 /* Bit position of this element from the start of the containing
5271 constructor. */
5272 HOST_WIDE_INT constructor_relative_ebitpos
5273 = (local->field
5274 ? int_bit_position (local->field)
5275 : ebitsize * relative_index);
5277 /* Bit position of this element from the start of a possibly ongoing
5278 outer byte buffer. */
5279 HOST_WIDE_INT byte_relative_ebitpos
5280 = bit_offset + constructor_relative_ebitpos;
5282 /* From the start of a possibly ongoing outer byte buffer, offsets to
5283 the first bit of this element and to the first bit past the end of
5284 this element. */
5285 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5286 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5288 local->last_relative_index = relative_index;
5290 if (local->val == NULL_TREE)
5291 local->val = integer_zero_node;
5293 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5294 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5295 local->val = TREE_OPERAND (local->val, 0);
5297 if (TREE_CODE (local->val) != INTEGER_CST
5298 && TREE_CODE (local->val) != CONSTRUCTOR)
5300 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5301 return;
5304 /* If this field does not start in this (or next) byte, skip some bytes. */
5305 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5307 /* Output remnant of any bit field in previous bytes. */
5308 if (local->byte_buffer_in_use)
5310 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5311 local->total_bytes++;
5312 local->byte_buffer_in_use = false;
5315 /* If still not at proper byte, advance to there. */
5316 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5318 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5319 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5320 local->total_bytes = next_offset / BITS_PER_UNIT;
5324 /* Set up the buffer if necessary. */
5325 if (!local->byte_buffer_in_use)
5327 local->byte = 0;
5328 if (ebitsize > 0)
5329 local->byte_buffer_in_use = true;
5332 /* If this is nested constructor, recurse passing the bit offset and the
5333 pending data, then retrieve the new pending data afterwards. */
5334 if (TREE_CODE (local->val) == CONSTRUCTOR)
5336 oc_outer_state temp_state;
5337 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5338 temp_state.byte = local->byte;
5339 local->total_bytes
5340 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5341 local->byte = temp_state.byte;
5342 return;
5345 /* Otherwise, we must split the element into pieces that fall within
5346 separate bytes, and combine each byte with previous or following
5347 bit-fields. */
5348 while (next_offset < end_offset)
5350 int this_time;
5351 int shift;
5352 HOST_WIDE_INT value;
5353 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5354 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5356 /* Advance from byte to byte within this element when necessary. */
5357 while (next_byte != local->total_bytes)
5359 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5360 local->total_bytes++;
5361 local->byte = 0;
5364 /* Number of bits we can process at once (all part of the same byte). */
5365 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5366 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5368 /* For big-endian data, take the most significant bits (of the
5369 bits that are significant) first and put them into bytes from
5370 the most significant end. */
5371 shift = end_offset - next_offset - this_time;
5373 /* Don't try to take a bunch of bits that cross
5374 the word boundary in the INTEGER_CST. We can
5375 only select bits from one element. */
5376 if ((shift / HOST_BITS_PER_WIDE_INT)
5377 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5379 const int end = shift + this_time - 1;
5380 shift = end & -HOST_BITS_PER_WIDE_INT;
5381 this_time = end - shift + 1;
5384 /* Now get the bits from the appropriate constant word. */
5385 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5386 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5388 /* Get the result. This works only when:
5389 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5390 local->byte |= (((value >> shift)
5391 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5392 << (BITS_PER_UNIT - this_time - next_bit));
5394 else
5396 /* On little-endian machines, take the least significant bits of
5397 the value first and pack them starting at the least significant
5398 bits of the bytes. */
5399 shift = next_offset - byte_relative_ebitpos;
5401 /* Don't try to take a bunch of bits that cross
5402 the word boundary in the INTEGER_CST. We can
5403 only select bits from one element. */
5404 if ((shift / HOST_BITS_PER_WIDE_INT)
5405 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5406 this_time
5407 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5409 /* Now get the bits from the appropriate constant word. */
5410 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5411 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5413 /* Get the result. This works only when:
5414 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5415 local->byte |= (((value >> shift)
5416 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5417 << next_bit);
5420 next_offset += this_time;
5421 local->byte_buffer_in_use = true;
5425 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5426 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5427 caller output state of relevance in recursive invocations. */
5429 static unsigned HOST_WIDE_INT
5430 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5431 bool reverse, oc_outer_state *outer)
5433 unsigned HOST_WIDE_INT cnt;
5434 constructor_elt *ce;
5435 oc_local_state local;
5437 /* Setup our local state to communicate with helpers. */
5438 local.exp = exp;
5439 local.type = TREE_TYPE (exp);
5440 local.size = size;
5441 local.align = align;
5442 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5443 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5444 else
5445 local.min_index = integer_zero_node;
5447 local.total_bytes = 0;
5448 local.byte_buffer_in_use = outer != NULL;
5449 local.byte = outer ? outer->byte : 0;
5450 local.last_relative_index = -1;
5451 /* The storage order is specified for every aggregate type. */
5452 if (AGGREGATE_TYPE_P (local.type))
5453 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5454 else
5455 local.reverse = reverse;
5457 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5459 /* As CE goes through the elements of the constant, FIELD goes through the
5460 structure fields if the constant is a structure. If the constant is a
5461 union, we override this by getting the field from the TREE_LIST element.
5462 But the constant could also be an array. Then FIELD is zero.
5464 There is always a maximum of one element in the chain LINK for unions
5465 (even if the initializer in a source program incorrectly contains
5466 more one). */
5468 if (TREE_CODE (local.type) == RECORD_TYPE)
5469 local.field = TYPE_FIELDS (local.type);
5470 else
5471 local.field = NULL_TREE;
5473 for (cnt = 0;
5474 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5475 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5477 local.val = ce->value;
5478 local.index = NULL_TREE;
5480 /* The element in a union constructor specifies the proper field
5481 or index. */
5482 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5483 local.field = ce->index;
5485 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5486 local.index = ce->index;
5488 if (local.field && flag_verbose_asm)
5489 fprintf (asm_out_file, "%s %s:\n",
5490 ASM_COMMENT_START,
5491 DECL_NAME (local.field)
5492 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5493 : "<anonymous>");
5495 /* Eliminate the marker that makes a cast not be an lvalue. */
5496 if (local.val != NULL_TREE)
5497 STRIP_NOPS (local.val);
5499 /* Output the current element, using the appropriate helper ... */
5501 /* For an array slice not part of an outer bitfield. */
5502 if (!outer
5503 && local.index != NULL_TREE
5504 && TREE_CODE (local.index) == RANGE_EXPR)
5505 output_constructor_array_range (&local);
5507 /* For a field that is neither a true bitfield nor part of an outer one,
5508 known to be at least byte aligned and multiple-of-bytes long. */
5509 else if (!outer
5510 && (local.field == NULL_TREE
5511 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5512 output_constructor_regular_field (&local);
5514 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5515 supported for scalar fields, so we may need to convert first. */
5516 else
5518 if (TREE_CODE (local.val) == REAL_CST)
5519 local.val
5520 = fold_unary (VIEW_CONVERT_EXPR,
5521 build_nonstandard_integer_type
5522 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5523 local.val);
5524 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5528 /* If we are not at toplevel, save the pending data for our caller.
5529 Otherwise output the pending data and padding zeros as needed. */
5530 if (outer)
5531 outer->byte = local.byte;
5532 else
5534 if (local.byte_buffer_in_use)
5536 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5537 local.total_bytes++;
5540 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5542 assemble_zeros (local.size - local.total_bytes);
5543 local.total_bytes = local.size;
5547 return local.total_bytes;
5550 /* Mark DECL as weak. */
5552 static void
5553 mark_weak (tree decl)
5555 if (DECL_WEAK (decl))
5556 return;
5558 struct symtab_node *n = symtab_node::get (decl);
5559 if (n && n->refuse_visibility_changes)
5560 error ("%+qD declared weak after being used", decl);
5561 DECL_WEAK (decl) = 1;
5563 if (DECL_RTL_SET_P (decl)
5564 && MEM_P (DECL_RTL (decl))
5565 && XEXP (DECL_RTL (decl), 0)
5566 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5567 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5570 /* Merge weak status between NEWDECL and OLDDECL. */
5572 void
5573 merge_weak (tree newdecl, tree olddecl)
5575 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5577 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5579 tree *pwd;
5580 /* We put the NEWDECL on the weak_decls list at some point
5581 and OLDDECL as well. Keep just OLDDECL on the list. */
5582 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5583 if (TREE_VALUE (*pwd) == newdecl)
5585 *pwd = TREE_CHAIN (*pwd);
5586 break;
5589 return;
5592 if (DECL_WEAK (newdecl))
5594 tree wd;
5596 /* NEWDECL is weak, but OLDDECL is not. */
5598 /* If we already output the OLDDECL, we're in trouble; we can't
5599 go back and make it weak. This should never happen in
5600 unit-at-a-time compilation. */
5601 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5603 /* If we've already generated rtl referencing OLDDECL, we may
5604 have done so in a way that will not function properly with
5605 a weak symbol. Again in unit-at-a-time this should be
5606 impossible. */
5607 gcc_assert (!TREE_USED (olddecl)
5608 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5610 /* PR 49899: You cannot convert a static function into a weak, public function. */
5611 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
5612 error ("weak declaration of %q+D being applied to a already "
5613 "existing, static definition", newdecl);
5615 if (TARGET_SUPPORTS_WEAK)
5617 /* We put the NEWDECL on the weak_decls list at some point.
5618 Replace it with the OLDDECL. */
5619 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5620 if (TREE_VALUE (wd) == newdecl)
5622 TREE_VALUE (wd) = olddecl;
5623 break;
5625 /* We may not find the entry on the list. If NEWDECL is a
5626 weak alias, then we will have already called
5627 globalize_decl to remove the entry; in that case, we do
5628 not need to do anything. */
5631 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5632 mark_weak (olddecl);
5634 else
5635 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5636 weak. Just update NEWDECL to indicate that it's weak too. */
5637 mark_weak (newdecl);
5640 /* Declare DECL to be a weak symbol. */
5642 void
5643 declare_weak (tree decl)
5645 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5646 if (! TREE_PUBLIC (decl))
5648 error ("weak declaration of %q+D must be public", decl);
5649 return;
5651 else if (!TARGET_SUPPORTS_WEAK)
5652 warning (0, "weak declaration of %q+D not supported", decl);
5654 mark_weak (decl);
5655 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5656 DECL_ATTRIBUTES (decl)
5657 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5660 static void
5661 weak_finish_1 (tree decl)
5663 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5664 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5665 #endif
5667 if (! TREE_USED (decl))
5668 return;
5670 #ifdef ASM_WEAKEN_DECL
5671 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5672 #else
5673 #ifdef ASM_WEAKEN_LABEL
5674 ASM_WEAKEN_LABEL (asm_out_file, name);
5675 #else
5676 #ifdef ASM_OUTPUT_WEAK_ALIAS
5678 static bool warn_once = 0;
5679 if (! warn_once)
5681 warning (0, "only weak aliases are supported in this configuration");
5682 warn_once = 1;
5684 return;
5686 #endif
5687 #endif
5688 #endif
5691 /* Fiven an assembly name, find the decl it is associated with. */
5692 static tree
5693 find_decl (tree target)
5695 symtab_node *node = symtab_node::get_for_asmname (target);
5696 if (node)
5697 return node->decl;
5698 return NULL_TREE;
5701 /* This TREE_LIST contains weakref targets. */
5703 static GTY(()) tree weakref_targets;
5705 /* Emit any pending weak declarations. */
5707 void
5708 weak_finish (void)
5710 tree t;
5712 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5714 tree alias_decl = TREE_PURPOSE (t);
5715 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5717 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
5718 || TREE_SYMBOL_REFERENCED (target))
5719 /* Remove alias_decl from the weak list, but leave entries for
5720 the target alone. */
5721 target = NULL_TREE;
5722 #ifndef ASM_OUTPUT_WEAKREF
5723 else if (! TREE_SYMBOL_REFERENCED (target))
5725 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5726 defined, otherwise we and weak_finish_1 would use
5727 different macros. */
5728 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5729 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5730 # else
5731 tree decl = find_decl (target);
5733 if (! decl)
5735 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5736 TREE_CODE (alias_decl), target,
5737 TREE_TYPE (alias_decl));
5739 DECL_EXTERNAL (decl) = 1;
5740 TREE_PUBLIC (decl) = 1;
5741 DECL_ARTIFICIAL (decl) = 1;
5742 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5743 TREE_USED (decl) = 1;
5746 weak_finish_1 (decl);
5747 # endif
5749 #endif
5752 tree *p;
5753 tree t2;
5755 /* Remove the alias and the target from the pending weak list
5756 so that we do not emit any .weak directives for the former,
5757 nor multiple .weak directives for the latter. */
5758 for (p = &weak_decls; (t2 = *p) ; )
5760 if (TREE_VALUE (t2) == alias_decl
5761 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5762 *p = TREE_CHAIN (t2);
5763 else
5764 p = &TREE_CHAIN (t2);
5767 /* Remove other weakrefs to the same target, to speed things up. */
5768 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5770 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5771 *p = TREE_CHAIN (t2);
5772 else
5773 p = &TREE_CHAIN (t2);
5778 for (t = weak_decls; t; t = TREE_CHAIN (t))
5780 tree decl = TREE_VALUE (t);
5782 weak_finish_1 (decl);
5786 /* Emit the assembly bits to indicate that DECL is globally visible. */
5788 static void
5789 globalize_decl (tree decl)
5792 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5793 if (DECL_WEAK (decl))
5795 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5796 tree *p, t;
5798 #ifdef ASM_WEAKEN_DECL
5799 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5800 #else
5801 ASM_WEAKEN_LABEL (asm_out_file, name);
5802 #endif
5804 /* Remove this function from the pending weak list so that
5805 we do not emit multiple .weak directives for it. */
5806 for (p = &weak_decls; (t = *p) ; )
5808 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5809 *p = TREE_CHAIN (t);
5810 else
5811 p = &TREE_CHAIN (t);
5814 /* Remove weakrefs to the same target from the pending weakref
5815 list, for the same reason. */
5816 for (p = &weakref_targets; (t = *p) ; )
5818 if (DECL_ASSEMBLER_NAME (decl)
5819 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5820 *p = TREE_CHAIN (t);
5821 else
5822 p = &TREE_CHAIN (t);
5825 return;
5827 #endif
5829 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5832 vec<alias_pair, va_gc> *alias_pairs;
5834 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5835 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5836 tree node is DECL to have the value of the tree node TARGET. */
5838 void
5839 do_assemble_alias (tree decl, tree target)
5841 tree id;
5843 /* Emulated TLS had better not get this var. */
5844 gcc_assert (!(!targetm.have_tls
5845 && VAR_P (decl)
5846 && DECL_THREAD_LOCAL_P (decl)));
5848 if (TREE_ASM_WRITTEN (decl))
5849 return;
5851 id = DECL_ASSEMBLER_NAME (decl);
5852 ultimate_transparent_alias_target (&id);
5853 ultimate_transparent_alias_target (&target);
5855 /* We must force creation of DECL_RTL for debug info generation, even though
5856 we don't use it here. */
5857 make_decl_rtl (decl);
5859 TREE_ASM_WRITTEN (decl) = 1;
5860 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5861 TREE_ASM_WRITTEN (id) = 1;
5863 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5865 if (!TREE_SYMBOL_REFERENCED (target))
5866 weakref_targets = tree_cons (decl, target, weakref_targets);
5868 #ifdef ASM_OUTPUT_WEAKREF
5869 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5870 IDENTIFIER_POINTER (id),
5871 IDENTIFIER_POINTER (target));
5872 #else
5873 if (!TARGET_SUPPORTS_WEAK)
5875 error_at (DECL_SOURCE_LOCATION (decl),
5876 "weakref is not supported in this configuration");
5877 return;
5879 #endif
5880 return;
5883 #ifdef ASM_OUTPUT_DEF
5884 tree orig_decl = decl;
5886 /* Make name accessible from other files, if appropriate. */
5888 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5890 globalize_decl (decl);
5891 maybe_assemble_visibility (decl);
5893 if (TREE_CODE (decl) == FUNCTION_DECL
5894 && cgraph_node::get (decl)->ifunc_resolver)
5896 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5897 if (targetm.has_ifunc_p ())
5898 ASM_OUTPUT_TYPE_DIRECTIVE
5899 (asm_out_file, IDENTIFIER_POINTER (id),
5900 IFUNC_ASM_TYPE);
5901 else
5902 #endif
5903 error_at (DECL_SOURCE_LOCATION (decl),
5904 "ifunc is not supported on this target");
5907 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5908 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5909 # else
5910 ASM_OUTPUT_DEF (asm_out_file,
5911 IDENTIFIER_POINTER (id),
5912 IDENTIFIER_POINTER (target));
5913 # endif
5914 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5916 const char *name;
5917 tree *p, t;
5919 name = IDENTIFIER_POINTER (id);
5920 # ifdef ASM_WEAKEN_DECL
5921 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5922 # else
5923 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5924 # endif
5925 /* Remove this function from the pending weak list so that
5926 we do not emit multiple .weak directives for it. */
5927 for (p = &weak_decls; (t = *p) ; )
5928 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5929 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5930 *p = TREE_CHAIN (t);
5931 else
5932 p = &TREE_CHAIN (t);
5934 /* Remove weakrefs to the same target from the pending weakref
5935 list, for the same reason. */
5936 for (p = &weakref_targets; (t = *p) ; )
5938 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5939 *p = TREE_CHAIN (t);
5940 else
5941 p = &TREE_CHAIN (t);
5944 #endif
5947 /* Emit an assembler directive to make the symbol for DECL an alias to
5948 the symbol for TARGET. */
5950 void
5951 assemble_alias (tree decl, tree target)
5953 tree target_decl;
5955 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5957 tree alias = DECL_ASSEMBLER_NAME (decl);
5959 ultimate_transparent_alias_target (&target);
5961 if (alias == target)
5962 error ("weakref %q+D ultimately targets itself", decl);
5963 if (TREE_PUBLIC (decl))
5964 error ("weakref %q+D must have static linkage", decl);
5966 else
5968 #if !defined (ASM_OUTPUT_DEF)
5969 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5970 error_at (DECL_SOURCE_LOCATION (decl),
5971 "alias definitions not supported in this configuration");
5972 TREE_ASM_WRITTEN (decl) = 1;
5973 return;
5974 # else
5975 if (!DECL_WEAK (decl))
5977 /* NB: ifunc_resolver isn't set when an error is detected. */
5978 if (TREE_CODE (decl) == FUNCTION_DECL
5979 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5980 error_at (DECL_SOURCE_LOCATION (decl),
5981 "ifunc is not supported in this configuration");
5982 else
5983 error_at (DECL_SOURCE_LOCATION (decl),
5984 "only weak aliases are supported in this configuration");
5985 TREE_ASM_WRITTEN (decl) = 1;
5986 return;
5988 # endif
5989 #endif
5991 TREE_USED (decl) = 1;
5993 /* Allow aliases to aliases. */
5994 if (TREE_CODE (decl) == FUNCTION_DECL)
5995 cgraph_node::get_create (decl)->alias = true;
5996 else
5997 varpool_node::get_create (decl)->alias = true;
5999 /* If the target has already been emitted, we don't have to queue the
6000 alias. This saves a tad of memory. */
6001 if (symtab->global_info_ready)
6002 target_decl = find_decl (target);
6003 else
6004 target_decl= NULL;
6005 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6006 || symtab->state >= EXPANSION)
6007 do_assemble_alias (decl, target);
6008 else
6010 alias_pair p = {decl, target};
6011 vec_safe_push (alias_pairs, p);
6015 /* Record and output a table of translations from original function
6016 to its transaction aware clone. Note that tm_pure functions are
6017 considered to be their own clone. */
6019 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6021 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6022 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6024 static int
6025 keep_cache_entry (tree_map *&e)
6027 return ggc_marked_p (e->base.from);
6031 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6033 void
6034 record_tm_clone_pair (tree o, tree n)
6036 struct tree_map **slot, *h;
6038 if (tm_clone_hash == NULL)
6039 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6041 h = ggc_alloc<tree_map> ();
6042 h->hash = htab_hash_pointer (o);
6043 h->base.from = o;
6044 h->to = n;
6046 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6047 *slot = h;
6050 tree
6051 get_tm_clone_pair (tree o)
6053 if (tm_clone_hash)
6055 struct tree_map *h, in;
6057 in.base.from = o;
6058 in.hash = htab_hash_pointer (o);
6059 h = tm_clone_hash->find_with_hash (&in, in.hash);
6060 if (h)
6061 return h->to;
6063 return NULL_TREE;
6066 struct tm_alias_pair
6068 unsigned int uid;
6069 tree from;
6070 tree to;
6074 /* Dump the actual pairs to the .tm_clone_table section. */
6076 static void
6077 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6079 unsigned i;
6080 tm_alias_pair *p;
6081 bool switched = false;
6083 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6085 tree src = p->from;
6086 tree dst = p->to;
6087 struct cgraph_node *src_n = cgraph_node::get (src);
6088 struct cgraph_node *dst_n = cgraph_node::get (dst);
6090 /* The function ipa_tm_create_version() marks the clone as needed if
6091 the original function was needed. But we also mark the clone as
6092 needed if we ever called the clone indirectly through
6093 TM_GETTMCLONE. If neither of these are true, we didn't generate
6094 a clone, and we didn't call it indirectly... no sense keeping it
6095 in the clone table. */
6096 if (!dst_n || !dst_n->definition)
6097 continue;
6099 /* This covers the case where we have optimized the original
6100 function away, and only access the transactional clone. */
6101 if (!src_n || !src_n->definition)
6102 continue;
6104 if (!switched)
6106 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6107 assemble_align (POINTER_SIZE);
6108 switched = true;
6111 assemble_integer (XEXP (DECL_RTL (src), 0),
6112 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6113 assemble_integer (XEXP (DECL_RTL (dst), 0),
6114 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6118 /* Provide a default for the tm_clone_table section. */
6120 section *
6121 default_clone_table_section (void)
6123 return get_named_section (NULL, ".tm_clone_table", 3);
6126 /* Helper comparison function for qsorting by the DECL_UID stored in
6127 alias_pair->emitted_diags. */
6129 static int
6130 tm_alias_pair_cmp (const void *x, const void *y)
6132 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6133 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6134 if (p1->uid < p2->uid)
6135 return -1;
6136 if (p1->uid > p2->uid)
6137 return 1;
6138 return 0;
6141 void
6142 finish_tm_clone_pairs (void)
6144 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6146 if (tm_clone_hash == NULL)
6147 return;
6149 /* We need a determenistic order for the .tm_clone_table, otherwise
6150 we will get bootstrap comparison failures, so dump the hash table
6151 to a vector, sort it, and dump the vector. */
6153 /* Dump the hashtable to a vector. */
6154 tree_map *map;
6155 hash_table<tm_clone_hasher>::iterator iter;
6156 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6158 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6159 tm_alias_pairs.safe_push (p);
6161 /* Sort it. */
6162 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6164 /* Dump it. */
6165 dump_tm_clone_pairs (tm_alias_pairs);
6167 tm_clone_hash->empty ();
6168 tm_clone_hash = NULL;
6169 tm_alias_pairs.release ();
6173 /* Emit an assembler directive to set symbol for DECL visibility to
6174 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6176 void
6177 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6178 int vis ATTRIBUTE_UNUSED)
6180 #ifdef HAVE_GAS_HIDDEN
6181 static const char * const visibility_types[] = {
6182 NULL, "protected", "hidden", "internal"
6185 const char *name, *type;
6186 tree id;
6188 id = DECL_ASSEMBLER_NAME (decl);
6189 ultimate_transparent_alias_target (&id);
6190 name = IDENTIFIER_POINTER (id);
6192 type = visibility_types[vis];
6194 fprintf (asm_out_file, "\t.%s\t", type);
6195 assemble_name (asm_out_file, name);
6196 fprintf (asm_out_file, "\n");
6197 #else
6198 if (!DECL_ARTIFICIAL (decl))
6199 warning (OPT_Wattributes, "visibility attribute not supported "
6200 "in this configuration; ignored");
6201 #endif
6204 /* A helper function to call assemble_visibility when needed for a decl. */
6207 maybe_assemble_visibility (tree decl)
6209 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6210 if (vis != VISIBILITY_DEFAULT)
6212 targetm.asm_out.assemble_visibility (decl, vis);
6213 return 1;
6215 else
6216 return 0;
6219 /* Returns 1 if the target configuration supports defining public symbols
6220 so that one of them will be chosen at link time instead of generating a
6221 multiply-defined symbol error, whether through the use of weak symbols or
6222 a target-specific mechanism for having duplicates discarded. */
6225 supports_one_only (void)
6227 if (SUPPORTS_ONE_ONLY)
6228 return 1;
6229 return TARGET_SUPPORTS_WEAK;
6232 /* Set up DECL as a public symbol that can be defined in multiple
6233 translation units without generating a linker error. */
6235 void
6236 make_decl_one_only (tree decl, tree comdat_group)
6238 struct symtab_node *symbol;
6239 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6241 TREE_PUBLIC (decl) = 1;
6243 if (VAR_P (decl))
6244 symbol = varpool_node::get_create (decl);
6245 else
6246 symbol = cgraph_node::get_create (decl);
6248 if (SUPPORTS_ONE_ONLY)
6250 #ifdef MAKE_DECL_ONE_ONLY
6251 MAKE_DECL_ONE_ONLY (decl);
6252 #endif
6253 symbol->set_comdat_group (comdat_group);
6255 else if (VAR_P (decl)
6256 && (DECL_INITIAL (decl) == 0
6257 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6258 DECL_COMMON (decl) = 1;
6259 else
6261 gcc_assert (TARGET_SUPPORTS_WEAK);
6262 DECL_WEAK (decl) = 1;
6266 void
6267 init_varasm_once (void)
6269 section_htab = hash_table<section_hasher>::create_ggc (31);
6270 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6271 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6273 shared_constant_pool = create_constant_pool ();
6275 #ifdef TEXT_SECTION_ASM_OP
6276 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6277 TEXT_SECTION_ASM_OP);
6278 #endif
6280 #ifdef DATA_SECTION_ASM_OP
6281 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6282 DATA_SECTION_ASM_OP);
6283 #endif
6285 #ifdef SDATA_SECTION_ASM_OP
6286 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6287 SDATA_SECTION_ASM_OP);
6288 #endif
6290 #ifdef READONLY_DATA_SECTION_ASM_OP
6291 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6292 READONLY_DATA_SECTION_ASM_OP);
6293 #endif
6295 #ifdef CTORS_SECTION_ASM_OP
6296 ctors_section = get_unnamed_section (0, output_section_asm_op,
6297 CTORS_SECTION_ASM_OP);
6298 #endif
6300 #ifdef DTORS_SECTION_ASM_OP
6301 dtors_section = get_unnamed_section (0, output_section_asm_op,
6302 DTORS_SECTION_ASM_OP);
6303 #endif
6305 #ifdef BSS_SECTION_ASM_OP
6306 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6307 output_section_asm_op,
6308 BSS_SECTION_ASM_OP);
6309 #endif
6311 #ifdef SBSS_SECTION_ASM_OP
6312 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6313 output_section_asm_op,
6314 SBSS_SECTION_ASM_OP);
6315 #endif
6317 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6318 | SECTION_COMMON, emit_tls_common);
6319 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6320 | SECTION_COMMON, emit_local);
6321 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6322 | SECTION_COMMON, emit_common);
6324 #if defined ASM_OUTPUT_ALIGNED_BSS
6325 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6326 emit_bss);
6327 #endif
6329 targetm.asm_out.init_sections ();
6331 if (readonly_data_section == NULL)
6332 readonly_data_section = text_section;
6334 #ifdef ASM_OUTPUT_EXTERNAL
6335 pending_assemble_externals_set = new hash_set<tree>;
6336 #endif
6339 enum tls_model
6340 decl_default_tls_model (const_tree decl)
6342 enum tls_model kind;
6343 bool is_local;
6345 is_local = targetm.binds_local_p (decl);
6346 if (!flag_shlib)
6348 if (is_local)
6349 kind = TLS_MODEL_LOCAL_EXEC;
6350 else
6351 kind = TLS_MODEL_INITIAL_EXEC;
6354 /* Local dynamic is inefficient when we're not combining the
6355 parts of the address. */
6356 else if (optimize && is_local)
6357 kind = TLS_MODEL_LOCAL_DYNAMIC;
6358 else
6359 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6360 if (kind < flag_tls_default)
6361 kind = flag_tls_default;
6363 return kind;
6366 /* Select a set of attributes for section NAME based on the properties
6367 of DECL and whether or not RELOC indicates that DECL's initializer
6368 might contain runtime relocations.
6370 We make the section read-only and executable for a function decl,
6371 read-only for a const data decl, and writable for a non-const data decl. */
6373 unsigned int
6374 default_section_type_flags (tree decl, const char *name, int reloc)
6376 unsigned int flags;
6378 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6379 flags = SECTION_CODE;
6380 else if (decl)
6382 enum section_category category
6383 = categorize_decl_for_section (decl, reloc);
6384 if (decl_readonly_section_1 (category))
6385 flags = 0;
6386 else if (category == SECCAT_DATA_REL_RO
6387 || category == SECCAT_DATA_REL_RO_LOCAL)
6388 flags = SECTION_WRITE | SECTION_RELRO;
6389 else
6390 flags = SECTION_WRITE;
6392 else
6394 flags = SECTION_WRITE;
6395 if (strcmp (name, ".data.rel.ro") == 0
6396 || strcmp (name, ".data.rel.ro.local") == 0)
6397 flags |= SECTION_RELRO;
6400 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6401 flags |= SECTION_LINKONCE;
6403 if (strcmp (name, ".vtable_map_vars") == 0)
6404 flags |= SECTION_LINKONCE;
6406 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6407 flags |= SECTION_TLS | SECTION_WRITE;
6409 if (strcmp (name, ".bss") == 0
6410 || strncmp (name, ".bss.", 5) == 0
6411 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6412 || strcmp (name, ".persistent.bss") == 0
6413 || strcmp (name, ".sbss") == 0
6414 || strncmp (name, ".sbss.", 6) == 0
6415 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6416 flags |= SECTION_BSS;
6418 if (strcmp (name, ".tdata") == 0
6419 || strncmp (name, ".tdata.", 7) == 0
6420 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6421 flags |= SECTION_TLS;
6423 if (strcmp (name, ".tbss") == 0
6424 || strncmp (name, ".tbss.", 6) == 0
6425 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6426 flags |= SECTION_TLS | SECTION_BSS;
6428 /* Various sections have special ELF types that the assembler will
6429 assign by default based on the name. They are neither SHT_PROGBITS
6430 nor SHT_NOBITS, so when changing sections we don't want to print a
6431 section type (@progbits or @nobits). Rather than duplicating the
6432 assembler's knowledge of what those special name patterns are, just
6433 let the assembler choose the type if we don't know a specific
6434 reason to set it to something other than the default. SHT_PROGBITS
6435 is the default for sections whose name is not specially known to
6436 the assembler, so it does no harm to leave the choice to the
6437 assembler when @progbits is the best thing we know to use. If
6438 someone is silly enough to emit code or TLS variables to one of
6439 these sections, then don't handle them specially.
6441 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6442 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
6443 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6444 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6445 flags |= SECTION_NOTYPE;
6447 return flags;
6450 /* Return true if the target supports some form of global BSS,
6451 either through bss_noswitch_section, or by selecting a BSS
6452 section in TARGET_ASM_SELECT_SECTION. */
6454 bool
6455 have_global_bss_p (void)
6457 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6460 /* Output assembly to switch to section NAME with attribute FLAGS.
6461 Four variants for common object file formats. */
6463 void
6464 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6465 unsigned int flags ATTRIBUTE_UNUSED,
6466 tree decl ATTRIBUTE_UNUSED)
6468 /* Some object formats don't support named sections at all. The
6469 front-end should already have flagged this as an error. */
6470 gcc_unreachable ();
6473 #ifndef TLS_SECTION_ASM_FLAG
6474 #define TLS_SECTION_ASM_FLAG 'T'
6475 #endif
6477 void
6478 default_elf_asm_named_section (const char *name, unsigned int flags,
6479 tree decl)
6481 char flagchars[11], *f = flagchars;
6482 unsigned int numeric_value = 0;
6484 /* If we have already declared this section, we can use an
6485 abbreviated form to switch back to it -- unless this section is
6486 part of a COMDAT groups, in which case GAS requires the full
6487 declaration every time. */
6488 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6489 && (flags & SECTION_DECLARED))
6491 fprintf (asm_out_file, "\t.section\t%s\n", name);
6492 return;
6495 /* If we have a machine specific flag, then use the numeric value to pass
6496 this on to GAS. */
6497 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6498 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6499 else
6501 if (!(flags & SECTION_DEBUG))
6502 *f++ = 'a';
6503 #if HAVE_GAS_SECTION_EXCLUDE
6504 if (flags & SECTION_EXCLUDE)
6505 *f++ = 'e';
6506 #endif
6507 if (flags & SECTION_WRITE)
6508 *f++ = 'w';
6509 if (flags & SECTION_CODE)
6510 *f++ = 'x';
6511 if (flags & SECTION_SMALL)
6512 *f++ = 's';
6513 if (flags & SECTION_MERGE)
6514 *f++ = 'M';
6515 if (flags & SECTION_STRINGS)
6516 *f++ = 'S';
6517 if (flags & SECTION_TLS)
6518 *f++ = TLS_SECTION_ASM_FLAG;
6519 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6520 *f++ = 'G';
6521 #ifdef MACH_DEP_SECTION_ASM_FLAG
6522 if (flags & SECTION_MACH_DEP)
6523 *f++ = MACH_DEP_SECTION_ASM_FLAG;
6524 #endif
6525 *f = '\0';
6528 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6530 /* default_section_type_flags (above) knows which flags need special
6531 handling here, and sets NOTYPE when none of these apply so that the
6532 assembler's logic for default types can apply to user-chosen
6533 section names. */
6534 if (!(flags & SECTION_NOTYPE))
6536 const char *type;
6537 const char *format;
6539 if (flags & SECTION_BSS)
6540 type = "nobits";
6541 else
6542 type = "progbits";
6544 format = ",@%s";
6545 /* On platforms that use "@" as the assembly comment character,
6546 use "%" instead. */
6547 if (strcmp (ASM_COMMENT_START, "@") == 0)
6548 format = ",%%%s";
6549 fprintf (asm_out_file, format, type);
6551 if (flags & SECTION_ENTSIZE)
6552 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6553 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6555 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6556 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6557 else
6558 fprintf (asm_out_file, ",%s,comdat",
6559 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6563 putc ('\n', asm_out_file);
6566 void
6567 default_coff_asm_named_section (const char *name, unsigned int flags,
6568 tree decl ATTRIBUTE_UNUSED)
6570 char flagchars[8], *f = flagchars;
6572 if (flags & SECTION_WRITE)
6573 *f++ = 'w';
6574 if (flags & SECTION_CODE)
6575 *f++ = 'x';
6576 *f = '\0';
6578 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6581 void
6582 default_pe_asm_named_section (const char *name, unsigned int flags,
6583 tree decl)
6585 default_coff_asm_named_section (name, flags, decl);
6587 if (flags & SECTION_LINKONCE)
6589 /* Functions may have been compiled at various levels of
6590 optimization so we can't use `same_size' here.
6591 Instead, have the linker pick one. */
6592 fprintf (asm_out_file, "\t.linkonce %s\n",
6593 (flags & SECTION_CODE ? "discard" : "same_size"));
6597 /* The lame default section selector. */
6599 section *
6600 default_select_section (tree decl, int reloc,
6601 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6603 if (DECL_P (decl))
6605 if (decl_readonly_section (decl, reloc))
6606 return readonly_data_section;
6608 else if (TREE_CODE (decl) == CONSTRUCTOR)
6610 if (! ((flag_pic && reloc)
6611 || !TREE_READONLY (decl)
6612 || TREE_SIDE_EFFECTS (decl)
6613 || !TREE_CONSTANT (decl)))
6614 return readonly_data_section;
6616 else if (TREE_CODE (decl) == STRING_CST)
6617 return readonly_data_section;
6618 else if (! (flag_pic && reloc))
6619 return readonly_data_section;
6621 return data_section;
6624 enum section_category
6625 categorize_decl_for_section (const_tree decl, int reloc)
6627 enum section_category ret;
6629 if (TREE_CODE (decl) == FUNCTION_DECL)
6630 return SECCAT_TEXT;
6631 else if (TREE_CODE (decl) == STRING_CST)
6633 if ((flag_sanitize & SANITIZE_ADDRESS)
6634 && asan_protect_global (CONST_CAST_TREE (decl)))
6635 /* or !flag_merge_constants */
6636 return SECCAT_RODATA;
6637 else
6638 return SECCAT_RODATA_MERGE_STR;
6640 else if (VAR_P (decl))
6642 tree d = CONST_CAST_TREE (decl);
6643 if (bss_initializer_p (decl))
6644 ret = SECCAT_BSS;
6645 else if (! TREE_READONLY (decl)
6646 || TREE_SIDE_EFFECTS (decl)
6647 || (DECL_INITIAL (decl)
6648 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
6650 /* Here the reloc_rw_mask is not testing whether the section should
6651 be read-only or not, but whether the dynamic link will have to
6652 do something. If so, we wish to segregate the data in order to
6653 minimize cache misses inside the dynamic linker. */
6654 if (reloc & targetm.asm_out.reloc_rw_mask ())
6655 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6656 else
6657 ret = SECCAT_DATA;
6659 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6660 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6661 else if (reloc || flag_merge_constants < 2
6662 || ((flag_sanitize & SANITIZE_ADDRESS)
6663 /* PR 81697: for architectures that use section anchors we
6664 need to ignore DECL_RTL_SET_P (decl) for string constants
6665 inside this asan_protect_global call because otherwise
6666 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
6667 section, set DECL_RTL (decl) later on and add DECL to
6668 protected globals via successive asan_protect_global
6669 calls. In this scenario we'll end up with wrong
6670 alignment of these strings at runtime and possible ASan
6671 false positives. */
6672 && asan_protect_global (d, use_object_blocks_p ()
6673 && use_blocks_for_decl_p (d))))
6674 /* C and C++ don't allow different variables to share the same
6675 location. -fmerge-all-constants allows even that (at the
6676 expense of not conforming). */
6677 ret = SECCAT_RODATA;
6678 else if (DECL_INITIAL (decl)
6679 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6680 ret = SECCAT_RODATA_MERGE_STR_INIT;
6681 else
6682 ret = SECCAT_RODATA_MERGE_CONST;
6684 else if (TREE_CODE (decl) == CONSTRUCTOR)
6686 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6687 || TREE_SIDE_EFFECTS (decl)
6688 || ! TREE_CONSTANT (decl))
6689 ret = SECCAT_DATA;
6690 else
6691 ret = SECCAT_RODATA;
6693 else
6694 ret = SECCAT_RODATA;
6696 /* There are no read-only thread-local sections. */
6697 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6699 /* Note that this would be *just* SECCAT_BSS, except that there's
6700 no concept of a read-only thread-local-data section. */
6701 if (ret == SECCAT_BSS
6702 || DECL_INITIAL (decl) == NULL
6703 || (flag_zero_initialized_in_bss
6704 && initializer_zerop (DECL_INITIAL (decl))))
6705 ret = SECCAT_TBSS;
6706 else
6707 ret = SECCAT_TDATA;
6710 /* If the target uses small data sections, select it. */
6711 else if (targetm.in_small_data_p (decl))
6713 if (ret == SECCAT_BSS)
6714 ret = SECCAT_SBSS;
6715 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6716 ret = SECCAT_SRODATA;
6717 else
6718 ret = SECCAT_SDATA;
6721 return ret;
6724 static bool
6725 decl_readonly_section_1 (enum section_category category)
6727 switch (category)
6729 case SECCAT_RODATA:
6730 case SECCAT_RODATA_MERGE_STR:
6731 case SECCAT_RODATA_MERGE_STR_INIT:
6732 case SECCAT_RODATA_MERGE_CONST:
6733 case SECCAT_SRODATA:
6734 return true;
6735 default:
6736 return false;
6740 bool
6741 decl_readonly_section (const_tree decl, int reloc)
6743 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6746 /* Select a section based on the above categorization. */
6748 section *
6749 default_elf_select_section (tree decl, int reloc,
6750 unsigned HOST_WIDE_INT align)
6752 const char *sname;
6753 switch (categorize_decl_for_section (decl, reloc))
6755 case SECCAT_TEXT:
6756 /* We're not supposed to be called on FUNCTION_DECLs. */
6757 gcc_unreachable ();
6758 case SECCAT_RODATA:
6759 return readonly_data_section;
6760 case SECCAT_RODATA_MERGE_STR:
6761 return mergeable_string_section (decl, align, 0);
6762 case SECCAT_RODATA_MERGE_STR_INIT:
6763 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6764 case SECCAT_RODATA_MERGE_CONST:
6765 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6766 case SECCAT_SRODATA:
6767 sname = ".sdata2";
6768 break;
6769 case SECCAT_DATA:
6770 return data_section;
6771 case SECCAT_DATA_REL:
6772 sname = ".data.rel";
6773 break;
6774 case SECCAT_DATA_REL_LOCAL:
6775 sname = ".data.rel.local";
6776 break;
6777 case SECCAT_DATA_REL_RO:
6778 sname = ".data.rel.ro";
6779 break;
6780 case SECCAT_DATA_REL_RO_LOCAL:
6781 sname = ".data.rel.ro.local";
6782 break;
6783 case SECCAT_SDATA:
6784 sname = ".sdata";
6785 break;
6786 case SECCAT_TDATA:
6787 sname = ".tdata";
6788 break;
6789 case SECCAT_BSS:
6790 if (bss_section)
6791 return bss_section;
6792 sname = ".bss";
6793 break;
6794 case SECCAT_SBSS:
6795 sname = ".sbss";
6796 break;
6797 case SECCAT_TBSS:
6798 sname = ".tbss";
6799 break;
6800 default:
6801 gcc_unreachable ();
6804 return get_named_section (decl, sname, reloc);
6807 /* Construct a unique section name based on the decl name and the
6808 categorization performed above. */
6810 void
6811 default_unique_section (tree decl, int reloc)
6813 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6814 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6815 const char *prefix, *name, *linkonce;
6816 char *string;
6817 tree id;
6819 switch (categorize_decl_for_section (decl, reloc))
6821 case SECCAT_TEXT:
6822 prefix = one_only ? ".t" : ".text";
6823 break;
6824 case SECCAT_RODATA:
6825 case SECCAT_RODATA_MERGE_STR:
6826 case SECCAT_RODATA_MERGE_STR_INIT:
6827 case SECCAT_RODATA_MERGE_CONST:
6828 prefix = one_only ? ".r" : ".rodata";
6829 break;
6830 case SECCAT_SRODATA:
6831 prefix = one_only ? ".s2" : ".sdata2";
6832 break;
6833 case SECCAT_DATA:
6834 prefix = one_only ? ".d" : ".data";
6835 break;
6836 case SECCAT_DATA_REL:
6837 prefix = one_only ? ".d.rel" : ".data.rel";
6838 break;
6839 case SECCAT_DATA_REL_LOCAL:
6840 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6841 break;
6842 case SECCAT_DATA_REL_RO:
6843 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6844 break;
6845 case SECCAT_DATA_REL_RO_LOCAL:
6846 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6847 break;
6848 case SECCAT_SDATA:
6849 prefix = one_only ? ".s" : ".sdata";
6850 break;
6851 case SECCAT_BSS:
6852 prefix = one_only ? ".b" : ".bss";
6853 break;
6854 case SECCAT_SBSS:
6855 prefix = one_only ? ".sb" : ".sbss";
6856 break;
6857 case SECCAT_TDATA:
6858 prefix = one_only ? ".td" : ".tdata";
6859 break;
6860 case SECCAT_TBSS:
6861 prefix = one_only ? ".tb" : ".tbss";
6862 break;
6863 default:
6864 gcc_unreachable ();
6867 id = DECL_ASSEMBLER_NAME (decl);
6868 ultimate_transparent_alias_target (&id);
6869 name = IDENTIFIER_POINTER (id);
6870 name = targetm.strip_name_encoding (name);
6872 /* If we're using one_only, then there needs to be a .gnu.linkonce
6873 prefix to the section name. */
6874 linkonce = one_only ? ".gnu.linkonce" : "";
6876 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6878 set_decl_section_name (decl, string);
6881 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6883 static int
6884 compute_reloc_for_rtx_1 (const_rtx x)
6886 switch (GET_CODE (x))
6888 case SYMBOL_REF:
6889 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6890 case LABEL_REF:
6891 return 1;
6892 default:
6893 return 0;
6897 /* Like compute_reloc_for_constant, except for an RTX. The return value
6898 is a mask for which bit 1 indicates a global relocation, and bit 0
6899 indicates a local relocation. */
6901 static int
6902 compute_reloc_for_rtx (const_rtx x)
6904 switch (GET_CODE (x))
6906 case SYMBOL_REF:
6907 case LABEL_REF:
6908 return compute_reloc_for_rtx_1 (x);
6910 case CONST:
6912 int reloc = 0;
6913 subrtx_iterator::array_type array;
6914 FOR_EACH_SUBRTX (iter, array, x, ALL)
6915 reloc |= compute_reloc_for_rtx_1 (*iter);
6916 return reloc;
6919 default:
6920 return 0;
6924 section *
6925 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6926 rtx x,
6927 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6929 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6930 return data_section;
6931 else
6932 return readonly_data_section;
6935 section *
6936 default_elf_select_rtx_section (machine_mode mode, rtx x,
6937 unsigned HOST_WIDE_INT align)
6939 int reloc = compute_reloc_for_rtx (x);
6941 /* ??? Handle small data here somehow. */
6943 if (reloc & targetm.asm_out.reloc_rw_mask ())
6945 if (reloc == 1)
6946 return get_named_section (NULL, ".data.rel.ro.local", 1);
6947 else
6948 return get_named_section (NULL, ".data.rel.ro", 3);
6951 return mergeable_constant_section (mode, align, 0);
6954 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6956 void
6957 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6959 rtx symbol;
6960 int flags;
6962 /* Careful not to prod global register variables. */
6963 if (!MEM_P (rtl))
6964 return;
6965 symbol = XEXP (rtl, 0);
6966 if (GET_CODE (symbol) != SYMBOL_REF)
6967 return;
6969 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6970 if (TREE_CODE (decl) == FUNCTION_DECL)
6971 flags |= SYMBOL_FLAG_FUNCTION;
6972 if (targetm.binds_local_p (decl))
6973 flags |= SYMBOL_FLAG_LOCAL;
6974 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6975 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6976 else if (targetm.in_small_data_p (decl))
6977 flags |= SYMBOL_FLAG_SMALL;
6978 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6979 being PUBLIC, the thing *must* be defined in this translation unit.
6980 Prevent this buglet from being propagated into rtl code as well. */
6981 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6982 flags |= SYMBOL_FLAG_EXTERNAL;
6984 SYMBOL_REF_FLAGS (symbol) = flags;
6987 /* By default, we do nothing for encode_section_info, so we need not
6988 do anything but discard the '*' marker. */
6990 const char *
6991 default_strip_name_encoding (const char *str)
6993 return str + (*str == '*');
6996 #ifdef ASM_OUTPUT_DEF
6997 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6998 anchor relative to ".", the current section position. */
7000 void
7001 default_asm_output_anchor (rtx symbol)
7003 char buffer[100];
7005 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7006 SYMBOL_REF_BLOCK_OFFSET (symbol));
7007 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7009 #endif
7011 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
7013 bool
7014 default_use_anchors_for_symbol_p (const_rtx symbol)
7016 section *sect;
7017 tree decl;
7019 /* Don't use anchors for mergeable sections. The linker might move
7020 the objects around. */
7021 sect = SYMBOL_REF_BLOCK (symbol)->sect;
7022 if (sect->common.flags & SECTION_MERGE)
7023 return false;
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 assemble_align (block->alignment);
7634 /* Define the values of all anchors relative to the current section
7635 position. */
7636 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7637 targetm.asm_out.output_anchor (symbol);
7639 /* Output the objects themselves. */
7640 offset = 0;
7641 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7643 /* Move to the object's offset, padding with zeros if necessary. */
7644 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7645 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7646 if (CONSTANT_POOL_ADDRESS_P (symbol))
7648 desc = SYMBOL_REF_CONSTANT (symbol);
7649 /* Pass 1 for align as we have already laid out everything in the block.
7650 So aligning shouldn't be necessary. */
7651 output_constant_pool_1 (desc, 1);
7652 offset += GET_MODE_SIZE (desc->mode);
7654 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7656 HOST_WIDE_INT size;
7657 decl = SYMBOL_REF_DECL (symbol);
7658 assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7659 DECL_ALIGN (decl), false);
7661 size = get_constant_size (DECL_INITIAL (decl));
7662 offset += size;
7663 if ((flag_sanitize & SANITIZE_ADDRESS)
7664 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7665 && asan_protect_global (DECL_INITIAL (decl)))
7667 size = asan_red_zone_size (size);
7668 assemble_zeros (size);
7669 offset += size;
7672 else
7674 HOST_WIDE_INT size;
7675 decl = SYMBOL_REF_DECL (symbol);
7676 assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
7677 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7678 offset += size;
7679 if ((flag_sanitize & SANITIZE_ADDRESS)
7680 && asan_protect_global (decl))
7682 size = asan_red_zone_size (size);
7683 assemble_zeros (size);
7684 offset += size;
7690 /* A callback for qsort to compare object_blocks. */
7692 static int
7693 output_object_block_compare (const void *x, const void *y)
7695 object_block *p1 = *(object_block * const*)x;
7696 object_block *p2 = *(object_block * const*)y;
7698 if (p1->sect->common.flags & SECTION_NAMED
7699 && !(p2->sect->common.flags & SECTION_NAMED))
7700 return 1;
7702 if (!(p1->sect->common.flags & SECTION_NAMED)
7703 && p2->sect->common.flags & SECTION_NAMED)
7704 return -1;
7706 if (p1->sect->common.flags & SECTION_NAMED
7707 && p2->sect->common.flags & SECTION_NAMED)
7708 return strcmp (p1->sect->named.name, p2->sect->named.name);
7710 unsigned f1 = p1->sect->common.flags;
7711 unsigned f2 = p2->sect->common.flags;
7712 if (f1 == f2)
7713 return 0;
7714 return f1 < f2 ? -1 : 1;
7717 /* Output the definitions of all object_blocks. */
7719 void
7720 output_object_blocks (void)
7722 vec<object_block *, va_heap> v;
7723 v.create (object_block_htab->elements ());
7724 object_block *obj;
7725 hash_table<object_block_hasher>::iterator hi;
7727 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
7728 v.quick_push (obj);
7730 /* Sort them in order to output them in a deterministic manner,
7731 otherwise we may get .rodata sections in different orders with
7732 and without -g. */
7733 v.qsort (output_object_block_compare);
7734 unsigned i;
7735 FOR_EACH_VEC_ELT (v, i, obj)
7736 output_object_block (obj);
7738 v.release ();
7741 /* This function provides a possible implementation of the
7742 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7743 by -frecord-gcc-switches it creates a new mergeable, string section in the
7744 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7745 contains the switches in ASCII format.
7747 FIXME: This code does not correctly handle double quote characters
7748 that appear inside strings, (it strips them rather than preserving them).
7749 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7750 characters - instead it treats them as sub-string separators. Since
7751 we want to emit NUL strings terminators into the object file we have to use
7752 ASM_OUTPUT_SKIP. */
7755 elf_record_gcc_switches (print_switch_type type, const char * name)
7757 switch (type)
7759 case SWITCH_TYPE_PASSED:
7760 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7761 ASM_OUTPUT_SKIP (asm_out_file, HOST_WIDE_INT_1U);
7762 break;
7764 case SWITCH_TYPE_DESCRIPTIVE:
7765 if (name == NULL)
7767 /* Distinguish between invocations where name is NULL. */
7768 static bool started = false;
7770 if (!started)
7772 section * sec;
7774 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7775 SECTION_DEBUG
7776 | SECTION_MERGE
7777 | SECTION_STRINGS
7778 | (SECTION_ENTSIZE & 1),
7779 NULL);
7780 switch_to_section (sec);
7781 started = true;
7785 default:
7786 break;
7789 /* The return value is currently ignored by the caller, but must be 0.
7790 For -fverbose-asm the return value would be the number of characters
7791 emitted into the assembler file. */
7792 return 0;
7795 /* Emit text to declare externally defined symbols. It is needed to
7796 properly support non-default visibility. */
7797 void
7798 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7799 tree decl,
7800 const char *name ATTRIBUTE_UNUSED)
7802 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7803 set in order to avoid putting out names that are never really
7804 used. Always output visibility specified in the source. */
7805 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7806 && (DECL_VISIBILITY_SPECIFIED (decl)
7807 || targetm.binds_local_p (decl)))
7808 maybe_assemble_visibility (decl);
7811 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7813 void
7814 default_asm_output_source_filename (FILE *file, const char *name)
7816 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7817 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7818 #else
7819 fprintf (file, "\t.file\t");
7820 output_quoted_string (file, name);
7821 putc ('\n', file);
7822 #endif
7825 /* Output a file name in the form wanted by System V. */
7827 void
7828 output_file_directive (FILE *asm_file, const char *input_name)
7830 int len;
7831 const char *na;
7833 if (input_name == NULL)
7834 input_name = "<stdin>";
7835 else
7836 input_name = remap_debug_filename (input_name);
7838 len = strlen (input_name);
7839 na = input_name + len;
7841 /* NA gets INPUT_NAME sans directory names. */
7842 while (na > input_name)
7844 if (IS_DIR_SEPARATOR (na[-1]))
7845 break;
7846 na--;
7849 targetm.asm_out.output_source_filename (asm_file, na);
7852 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7853 EXP. */
7855 make_debug_expr_from_rtl (const_rtx exp)
7857 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7858 machine_mode mode = GET_MODE (exp);
7859 rtx dval;
7861 DECL_ARTIFICIAL (ddecl) = 1;
7862 if (REG_P (exp) && REG_EXPR (exp))
7863 type = TREE_TYPE (REG_EXPR (exp));
7864 else if (MEM_P (exp) && MEM_EXPR (exp))
7865 type = TREE_TYPE (MEM_EXPR (exp));
7866 else
7867 type = NULL_TREE;
7868 if (type && TYPE_MODE (type) == mode)
7869 TREE_TYPE (ddecl) = type;
7870 else
7871 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7872 SET_DECL_MODE (ddecl, mode);
7873 dval = gen_rtx_DEBUG_EXPR (mode);
7874 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7875 SET_DECL_RTL (ddecl, dval);
7876 return dval;
7879 #ifdef ELF_ASCII_ESCAPES
7880 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7882 void
7883 default_elf_asm_output_limited_string (FILE *f, const char *s)
7885 int escape;
7886 unsigned char c;
7888 fputs (STRING_ASM_OP, f);
7889 putc ('"', f);
7890 while (*s != '\0')
7892 c = *s;
7893 escape = ELF_ASCII_ESCAPES[c];
7894 switch (escape)
7896 case 0:
7897 putc (c, f);
7898 break;
7899 case 1:
7900 putc ('\\', f);
7901 putc ('0'+((c>>6)&7), f);
7902 putc ('0'+((c>>3)&7), f);
7903 putc ('0'+(c&7), f);
7904 break;
7905 default:
7906 putc ('\\', f);
7907 putc (escape, f);
7908 break;
7910 s++;
7912 putc ('\"', f);
7913 putc ('\n', f);
7916 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7918 void
7919 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7921 const char *limit = s + len;
7922 const char *last_null = NULL;
7923 unsigned bytes_in_chunk = 0;
7924 unsigned char c;
7925 int escape;
7927 for (; s < limit; s++)
7929 const char *p;
7931 if (bytes_in_chunk >= 60)
7933 putc ('\"', f);
7934 putc ('\n', f);
7935 bytes_in_chunk = 0;
7938 if (s > last_null)
7940 for (p = s; p < limit && *p != '\0'; p++)
7941 continue;
7942 last_null = p;
7944 else
7945 p = last_null;
7947 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7949 if (bytes_in_chunk > 0)
7951 putc ('\"', f);
7952 putc ('\n', f);
7953 bytes_in_chunk = 0;
7956 default_elf_asm_output_limited_string (f, s);
7957 s = p;
7959 else
7961 if (bytes_in_chunk == 0)
7962 fputs (ASCII_DATA_ASM_OP "\"", f);
7964 c = *s;
7965 escape = ELF_ASCII_ESCAPES[c];
7966 switch (escape)
7968 case 0:
7969 putc (c, f);
7970 bytes_in_chunk++;
7971 break;
7972 case 1:
7973 putc ('\\', f);
7974 putc ('0'+((c>>6)&7), f);
7975 putc ('0'+((c>>3)&7), f);
7976 putc ('0'+(c&7), f);
7977 bytes_in_chunk += 4;
7978 break;
7979 default:
7980 putc ('\\', f);
7981 putc (escape, f);
7982 bytes_in_chunk += 2;
7983 break;
7989 if (bytes_in_chunk > 0)
7991 putc ('\"', f);
7992 putc ('\n', f);
7995 #endif
7997 static GTY(()) section *elf_init_array_section;
7998 static GTY(()) section *elf_fini_array_section;
8000 static section *
8001 get_elf_initfini_array_priority_section (int priority,
8002 bool constructor_p)
8004 section *sec;
8005 if (priority != DEFAULT_INIT_PRIORITY)
8007 char buf[18];
8008 sprintf (buf, "%s.%.5u",
8009 constructor_p ? ".init_array" : ".fini_array",
8010 priority);
8011 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8013 else
8015 if (constructor_p)
8017 if (elf_init_array_section == NULL)
8018 elf_init_array_section
8019 = get_section (".init_array",
8020 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8021 sec = elf_init_array_section;
8023 else
8025 if (elf_fini_array_section == NULL)
8026 elf_fini_array_section
8027 = get_section (".fini_array",
8028 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8029 sec = elf_fini_array_section;
8032 return sec;
8035 /* Use .init_array section for constructors. */
8037 void
8038 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8040 section *sec = get_elf_initfini_array_priority_section (priority,
8041 true);
8042 assemble_addr_to_section (symbol, sec);
8045 /* Use .fini_array section for destructors. */
8047 void
8048 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8050 section *sec = get_elf_initfini_array_priority_section (priority,
8051 false);
8052 assemble_addr_to_section (symbol, sec);
8055 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8057 This is a bit of a cheat. The real default is a no-op, but this
8058 hook is the default for all targets with a .ident directive. */
8060 void
8061 default_asm_output_ident_directive (const char *ident_str)
8063 const char *ident_asm_op = "\t.ident\t";
8065 /* If we are still in the front end, do not write out the string
8066 to asm_out_file. Instead, add a fake top-level asm statement.
8067 This allows the front ends to use this hook without actually
8068 writing to asm_out_file, to handle #ident or Pragma Ident. */
8069 if (symtab->state == PARSING)
8071 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8072 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8074 else
8075 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8079 /* This function ensures that vtable_map variables are not only
8080 in the comdat section, but that each variable has its own unique
8081 comdat name. Without this the variables end up in the same section
8082 with a single comdat name.
8084 FIXME: resolve_unique_section needs to deal better with
8085 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8086 that is fixed, this if-else statement can be replaced with
8087 a single call to "switch_to_section (sect)". */
8089 static void
8090 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8092 #if defined (OBJECT_FORMAT_ELF)
8093 targetm.asm_out.named_section (sect->named.name,
8094 sect->named.common.flags
8095 | SECTION_LINKONCE,
8096 DECL_NAME (decl));
8097 in_section = sect;
8098 #else
8099 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8100 Therefore the following check is used.
8101 In case a the target is PE or COFF a comdat group section
8102 is created, e.g. .vtable_map_vars$foo. The linker places
8103 everything in .vtable_map_vars at the end.
8105 A fix could be made in
8106 gcc/config/i386/winnt.c: i386_pe_unique_section. */
8107 if (TARGET_PECOFF)
8109 char *name;
8111 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
8112 name = ACONCAT ((sect->named.name, "$",
8113 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
8114 else
8115 name = ACONCAT ((sect->named.name, "$",
8116 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
8117 NULL));
8119 targetm.asm_out.named_section (name,
8120 sect->named.common.flags
8121 | SECTION_LINKONCE,
8122 DECL_NAME (decl));
8123 in_section = sect;
8125 else
8126 switch_to_section (sect);
8127 #endif
8130 #include "gt-varasm.h"