* genfixes: exit 1 when autogen not found.
[official-gcc.git] / gcc / varasm.c
blob5769bc6d63e9779b46cdc64f201c6317878e4f30
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);
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 str = TREE_STRING_POINTER (decl);
827 unit = GET_MODE_SIZE (mode);
829 /* Check for embedded NUL characters. */
830 for (i = 0; i < len; i += unit)
832 for (j = 0; j < unit; j++)
833 if (str[i + j] != '\0')
834 break;
835 if (j == unit)
836 break;
838 if (i == len - unit)
840 sprintf (name, "%s.str%d.%d", prefix,
841 modesize / 8, (int) (align / 8));
842 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
843 return get_section (name, flags, NULL);
848 return readonly_data_section;
851 /* Return the section to use for constant merging. */
853 section *
854 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
855 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
856 unsigned int flags ATTRIBUTE_UNUSED)
858 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
859 && mode != VOIDmode
860 && mode != BLKmode
861 && known_le (GET_MODE_BITSIZE (mode), align)
862 && align >= 8
863 && align <= 256
864 && (align & (align - 1)) == 0)
866 const char *prefix = function_mergeable_rodata_prefix ();
867 char *name = (char *) alloca (strlen (prefix) + 30);
869 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
870 flags |= (align / 8) | SECTION_MERGE;
871 return get_section (name, flags, NULL);
873 return readonly_data_section;
876 /* Given NAME, a putative register name, discard any customary prefixes. */
878 static const char *
879 strip_reg_name (const char *name)
881 #ifdef REGISTER_PREFIX
882 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
883 name += strlen (REGISTER_PREFIX);
884 #endif
885 if (name[0] == '%' || name[0] == '#')
886 name++;
887 return name;
890 /* The user has asked for a DECL to have a particular name. Set (or
891 change) it in such a way that we don't prefix an underscore to
892 it. */
893 void
894 set_user_assembler_name (tree decl, const char *name)
896 char *starred = (char *) alloca (strlen (name) + 2);
897 starred[0] = '*';
898 strcpy (starred + 1, name);
899 symtab->change_decl_assembler_name (decl, get_identifier (starred));
900 SET_DECL_RTL (decl, NULL_RTX);
903 /* Decode an `asm' spec for a declaration as a register name.
904 Return the register number, or -1 if nothing specified,
905 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
906 or -3 if ASMSPEC is `cc' and is not recognized,
907 or -4 if ASMSPEC is `memory' and is not recognized.
908 Accept an exact spelling or a decimal number.
909 Prefixes such as % are optional. */
912 decode_reg_name_and_count (const char *asmspec, int *pnregs)
914 /* Presume just one register is clobbered. */
915 *pnregs = 1;
917 if (asmspec != 0)
919 int i;
921 /* Get rid of confusing prefixes. */
922 asmspec = strip_reg_name (asmspec);
924 /* Allow a decimal number as a "register name". */
925 for (i = strlen (asmspec) - 1; i >= 0; i--)
926 if (! ISDIGIT (asmspec[i]))
927 break;
928 if (asmspec[0] != 0 && i < 0)
930 i = atoi (asmspec);
931 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
932 return i;
933 else
934 return -2;
937 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
938 if (reg_names[i][0]
939 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
940 return i;
942 #ifdef OVERLAPPING_REGISTER_NAMES
944 static const struct
946 const char *const name;
947 const int number;
948 const int nregs;
949 } table[] = OVERLAPPING_REGISTER_NAMES;
951 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
952 if (table[i].name[0]
953 && ! strcmp (asmspec, table[i].name))
955 *pnregs = table[i].nregs;
956 return table[i].number;
959 #endif /* OVERLAPPING_REGISTER_NAMES */
961 #ifdef ADDITIONAL_REGISTER_NAMES
963 static const struct { const char *const name; const int number; } table[]
964 = ADDITIONAL_REGISTER_NAMES;
966 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
967 if (table[i].name[0]
968 && ! strcmp (asmspec, table[i].name)
969 && reg_names[table[i].number][0])
970 return table[i].number;
972 #endif /* ADDITIONAL_REGISTER_NAMES */
974 if (!strcmp (asmspec, "memory"))
975 return -4;
977 if (!strcmp (asmspec, "cc"))
978 return -3;
980 return -2;
983 return -1;
987 decode_reg_name (const char *name)
989 int count;
990 return decode_reg_name_and_count (name, &count);
994 /* Return true if DECL's initializer is suitable for a BSS section. */
996 bool
997 bss_initializer_p (const_tree decl, bool named)
999 /* Do not put non-common constants into the .bss section, they belong in
1000 a readonly section, except when NAMED is true. */
1001 return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1002 && (DECL_INITIAL (decl) == NULL
1003 /* In LTO we have no errors in program; error_mark_node is used
1004 to mark offlined constructors. */
1005 || (DECL_INITIAL (decl) == error_mark_node
1006 && !in_lto_p)
1007 || (flag_zero_initialized_in_bss
1008 && initializer_zerop (DECL_INITIAL (decl)))));
1011 /* Compute the alignment of variable specified by DECL.
1012 DONT_OUTPUT_DATA is from assemble_variable. */
1014 void
1015 align_variable (tree decl, bool dont_output_data)
1017 unsigned int align = DECL_ALIGN (decl);
1019 /* In the case for initialing an array whose length isn't specified,
1020 where we have not yet been able to do the layout,
1021 figure out the proper alignment now. */
1022 if (dont_output_data && DECL_SIZE (decl) == 0
1023 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1024 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1026 /* Some object file formats have a maximum alignment which they support.
1027 In particular, a.out format supports a maximum alignment of 4. */
1028 if (align > MAX_OFILE_ALIGNMENT)
1030 error ("alignment of %q+D is greater than maximum object "
1031 "file alignment %d", decl,
1032 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1033 align = MAX_OFILE_ALIGNMENT;
1036 if (! DECL_USER_ALIGN (decl))
1038 #ifdef DATA_ABI_ALIGNMENT
1039 unsigned int data_abi_align
1040 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1041 /* For backwards compatibility, don't assume the ABI alignment for
1042 TLS variables. */
1043 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1044 align = data_abi_align;
1045 #endif
1047 /* On some machines, it is good to increase alignment sometimes.
1048 But as DECL_ALIGN is used both for actually emitting the variable
1049 and for code accessing the variable as guaranteed alignment, we
1050 can only increase the alignment if it is a performance optimization
1051 if the references to it must bind to the current definition. */
1052 if (decl_binds_to_current_def_p (decl)
1053 && !DECL_VIRTUAL_P (decl))
1055 #ifdef DATA_ALIGNMENT
1056 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1057 /* Don't increase alignment too much for TLS variables - TLS space
1058 is too precious. */
1059 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1060 align = data_align;
1061 #endif
1062 if (DECL_INITIAL (decl) != 0
1063 /* In LTO we have no errors in program; error_mark_node is used
1064 to mark offlined constructors. */
1065 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1067 unsigned int const_align
1068 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1069 /* Don't increase alignment too much for TLS variables - TLS
1070 space is too precious. */
1071 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1072 align = const_align;
1077 /* Reset the alignment in case we have made it tighter, so we can benefit
1078 from it in get_pointer_alignment. */
1079 SET_DECL_ALIGN (decl, align);
1082 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1083 beyond what align_variable returned. */
1085 static unsigned int
1086 get_variable_align (tree decl)
1088 unsigned int align = DECL_ALIGN (decl);
1090 /* For user aligned vars or static vars align_variable already did
1091 everything. */
1092 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1093 return align;
1095 #ifdef DATA_ABI_ALIGNMENT
1096 if (DECL_THREAD_LOCAL_P (decl))
1097 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1098 #endif
1100 /* For decls that bind to the current definition, align_variable
1101 did also everything, except for not assuming ABI required alignment
1102 of TLS variables. For other vars, increase the alignment here
1103 as an optimization. */
1104 if (!decl_binds_to_current_def_p (decl))
1106 /* On some machines, it is good to increase alignment sometimes. */
1107 #ifdef DATA_ALIGNMENT
1108 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1109 /* Don't increase alignment too much for TLS variables - TLS space
1110 is too precious. */
1111 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1112 align = data_align;
1113 #endif
1114 if (DECL_INITIAL (decl) != 0
1115 /* In LTO we have no errors in program; error_mark_node is used
1116 to mark offlined constructors. */
1117 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1119 unsigned int const_align
1120 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1121 /* Don't increase alignment too much for TLS variables - TLS space
1122 is too precious. */
1123 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1124 align = const_align;
1128 return align;
1131 /* Return the section into which the given VAR_DECL or CONST_DECL
1132 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1133 section should be used wherever possible. */
1135 section *
1136 get_variable_section (tree decl, bool prefer_noswitch_p)
1138 addr_space_t as = ADDR_SPACE_GENERIC;
1139 int reloc;
1140 varpool_node *vnode = varpool_node::get (decl);
1141 if (vnode)
1143 vnode = vnode->ultimate_alias_target ();
1144 decl = vnode->decl;
1147 if (TREE_TYPE (decl) != error_mark_node)
1148 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1150 /* We need the constructor to figure out reloc flag. */
1151 if (vnode)
1152 vnode->get_constructor ();
1154 if (DECL_COMMON (decl))
1156 /* If the decl has been given an explicit section name, or it resides
1157 in a non-generic address space, then it isn't common, and shouldn't
1158 be handled as such. */
1159 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1160 && ADDR_SPACE_GENERIC_P (as));
1161 if (DECL_THREAD_LOCAL_P (decl))
1162 return tls_comm_section;
1163 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1164 return comm_section;
1167 if (DECL_INITIAL (decl) == error_mark_node)
1168 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1169 else if (DECL_INITIAL (decl))
1170 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1171 else
1172 reloc = 0;
1174 resolve_unique_section (decl, reloc, flag_data_sections);
1175 if (IN_NAMED_SECTION (decl))
1177 section *sect = get_named_section (decl, NULL, reloc);
1179 if ((sect->common.flags & SECTION_BSS)
1180 && !bss_initializer_p (decl, true))
1182 error_at (DECL_SOURCE_LOCATION (decl),
1183 "only zero initializers are allowed in section %qs",
1184 sect->named.name);
1185 DECL_INITIAL (decl) = error_mark_node;
1187 return sect;
1190 if (ADDR_SPACE_GENERIC_P (as)
1191 && !DECL_THREAD_LOCAL_P (decl)
1192 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1193 && bss_initializer_p (decl))
1195 if (!TREE_PUBLIC (decl)
1196 && !((flag_sanitize & SANITIZE_ADDRESS)
1197 && asan_protect_global (decl)))
1198 return lcomm_section;
1199 if (bss_noswitch_section)
1200 return bss_noswitch_section;
1203 return targetm.asm_out.select_section (decl, reloc,
1204 get_variable_align (decl));
1207 /* Return the block into which object_block DECL should be placed. */
1209 static struct object_block *
1210 get_block_for_decl (tree decl)
1212 section *sect;
1214 if (VAR_P (decl))
1216 /* The object must be defined in this translation unit. */
1217 if (DECL_EXTERNAL (decl))
1218 return NULL;
1220 /* There's no point using object blocks for something that is
1221 isolated by definition. */
1222 if (DECL_COMDAT_GROUP (decl))
1223 return NULL;
1226 /* We can only calculate block offsets if the decl has a known
1227 constant size. */
1228 if (DECL_SIZE_UNIT (decl) == NULL)
1229 return NULL;
1230 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1231 return NULL;
1233 /* Find out which section should contain DECL. We cannot put it into
1234 an object block if it requires a standalone definition. */
1235 if (VAR_P (decl))
1236 align_variable (decl, 0);
1237 sect = get_variable_section (decl, true);
1238 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1239 return NULL;
1241 return get_block_for_section (sect);
1244 /* Make sure block symbol SYMBOL is in block BLOCK. */
1246 static void
1247 change_symbol_block (rtx symbol, struct object_block *block)
1249 if (block != SYMBOL_REF_BLOCK (symbol))
1251 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1252 SYMBOL_REF_BLOCK (symbol) = block;
1256 /* Return true if it is possible to put DECL in an object_block. */
1258 static bool
1259 use_blocks_for_decl_p (tree decl)
1261 struct symtab_node *snode;
1263 /* Only data DECLs can be placed into object blocks. */
1264 if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1265 return false;
1267 /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1268 are never used from code directly and we never want object block handling
1269 for those. */
1270 if (DECL_INITIAL (decl) == decl)
1271 return false;
1273 /* If this decl is an alias, then we don't want to emit a
1274 definition. */
1275 if (VAR_P (decl)
1276 && (snode = symtab_node::get (decl)) != NULL
1277 && snode->alias)
1278 return false;
1280 return targetm.use_blocks_for_decl_p (decl);
1283 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1284 until we find an identifier that is not itself a transparent alias.
1285 Modify the alias passed to it by reference (and all aliases on the
1286 way to the ultimate target), such that they do not have to be
1287 followed again, and return the ultimate target of the alias
1288 chain. */
1290 static inline tree
1291 ultimate_transparent_alias_target (tree *alias)
1293 tree target = *alias;
1295 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1297 gcc_assert (TREE_CHAIN (target));
1298 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1299 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1300 && ! TREE_CHAIN (target));
1301 *alias = target;
1304 return target;
1307 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1308 have static storage duration. In other words, it should not be an
1309 automatic variable, including PARM_DECLs.
1311 There is, however, one exception: this function handles variables
1312 explicitly placed in a particular register by the user.
1314 This is never called for PARM_DECL nodes. */
1316 void
1317 make_decl_rtl (tree decl)
1319 const char *name = 0;
1320 int reg_number;
1321 tree id;
1322 rtx x;
1324 /* Check that we are not being given an automatic variable. */
1325 gcc_assert (TREE_CODE (decl) != PARM_DECL
1326 && TREE_CODE (decl) != RESULT_DECL);
1328 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1329 gcc_assert (!VAR_P (decl)
1330 || TREE_STATIC (decl)
1331 || TREE_PUBLIC (decl)
1332 || DECL_EXTERNAL (decl)
1333 || DECL_REGISTER (decl));
1335 /* And that we were not given a type or a label. */
1336 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1337 && TREE_CODE (decl) != LABEL_DECL);
1339 /* For a duplicate declaration, we can be called twice on the
1340 same DECL node. Don't discard the RTL already made. */
1341 if (DECL_RTL_SET_P (decl))
1343 /* If the old RTL had the wrong mode, fix the mode. */
1344 x = DECL_RTL (decl);
1345 if (GET_MODE (x) != DECL_MODE (decl))
1346 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1348 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1349 return;
1351 /* ??? Another way to do this would be to maintain a hashed
1352 table of such critters. Instead of adding stuff to a DECL
1353 to give certain attributes to it, we could use an external
1354 hash map from DECL to set of attributes. */
1356 /* Let the target reassign the RTL if it wants.
1357 This is necessary, for example, when one machine specific
1358 decl attribute overrides another. */
1359 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1361 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1362 on the new decl information. */
1363 if (MEM_P (x)
1364 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1365 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1366 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1368 return;
1371 /* If this variable belongs to the global constant pool, retrieve the
1372 pre-computed RTL or recompute it in LTO mode. */
1373 if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1375 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1376 return;
1379 id = DECL_ASSEMBLER_NAME (decl);
1380 name = IDENTIFIER_POINTER (id);
1382 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1383 && DECL_REGISTER (decl))
1385 error ("register name not specified for %q+D", decl);
1387 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1389 const char *asmspec = name+1;
1390 machine_mode mode = DECL_MODE (decl);
1391 reg_number = decode_reg_name (asmspec);
1392 /* First detect errors in declaring global registers. */
1393 if (reg_number == -1)
1394 error ("register name not specified for %q+D", decl);
1395 else if (reg_number < 0)
1396 error ("invalid register name for %q+D", decl);
1397 else if (mode == BLKmode)
1398 error ("data type of %q+D isn%'t suitable for a register",
1399 decl);
1400 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1401 error ("the register specified for %q+D cannot be accessed"
1402 " by the current target", decl);
1403 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1404 error ("the register specified for %q+D is not general enough"
1405 " to be used as a register variable", decl);
1406 else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1407 error ("register specified for %q+D isn%'t suitable for data type",
1408 decl);
1409 /* Now handle properly declared static register variables. */
1410 else
1412 int nregs;
1414 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1416 DECL_INITIAL (decl) = 0;
1417 error ("global register variable has initial value");
1419 if (TREE_THIS_VOLATILE (decl))
1420 warning (OPT_Wvolatile_register_var,
1421 "optimization may eliminate reads and/or "
1422 "writes to register variables");
1424 /* If the user specified one of the eliminables registers here,
1425 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1426 confused with that register and be eliminated. This usage is
1427 somewhat suspect... */
1429 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1430 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1431 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1433 if (TREE_STATIC (decl))
1435 /* Make this register global, so not usable for anything
1436 else. */
1437 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1438 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1439 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1440 #endif
1441 nregs = hard_regno_nregs (reg_number, mode);
1442 while (nregs > 0)
1443 globalize_reg (decl, reg_number + --nregs);
1446 /* As a register variable, it has no section. */
1447 return;
1449 /* Avoid internal errors from invalid register
1450 specifications. */
1451 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1452 DECL_HARD_REGISTER (decl) = 0;
1453 /* Also avoid SSA inconsistencies by pretending this is an external
1454 decl now. */
1455 DECL_EXTERNAL (decl) = 1;
1456 return;
1458 /* Now handle ordinary static variables and functions (in memory).
1459 Also handle vars declared register invalidly. */
1460 else if (name[0] == '*')
1462 #ifdef REGISTER_PREFIX
1463 if (strlen (REGISTER_PREFIX) != 0)
1465 reg_number = decode_reg_name (name);
1466 if (reg_number >= 0 || reg_number == -3)
1467 error ("register name given for non-register variable %q+D", decl);
1469 #endif
1472 /* Specifying a section attribute on a variable forces it into a
1473 non-.bss section, and thus it cannot be common. */
1474 /* FIXME: In general this code should not be necessary because
1475 visibility pass is doing the same work. But notice_global_symbol
1476 is called early and it needs to make DECL_RTL to get the name.
1477 we take care of recomputing the DECL_RTL after visibility is changed. */
1478 if (VAR_P (decl)
1479 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1480 && DECL_SECTION_NAME (decl) != NULL
1481 && DECL_INITIAL (decl) == NULL_TREE
1482 && DECL_COMMON (decl))
1483 DECL_COMMON (decl) = 0;
1485 /* Variables can't be both common and weak. */
1486 if (VAR_P (decl) && DECL_WEAK (decl))
1487 DECL_COMMON (decl) = 0;
1489 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1490 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1491 else
1493 machine_mode address_mode = Pmode;
1494 if (TREE_TYPE (decl) != error_mark_node)
1496 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1497 address_mode = targetm.addr_space.address_mode (as);
1499 x = gen_rtx_SYMBOL_REF (address_mode, name);
1501 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1502 SET_SYMBOL_REF_DECL (x, decl);
1504 x = gen_rtx_MEM (DECL_MODE (decl), x);
1505 if (TREE_CODE (decl) != FUNCTION_DECL)
1506 set_mem_attributes (x, decl, 1);
1507 SET_DECL_RTL (decl, x);
1509 /* Optionally set flags or add text to the name to record information
1510 such as that it is a function name.
1511 If the name is changed, the macro ASM_OUTPUT_LABELREF
1512 will have to know how to strip this information. */
1513 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1516 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1517 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1518 rtl. */
1521 make_decl_rtl_for_debug (tree decl)
1523 unsigned int save_aliasing_flag;
1524 rtx rtl;
1526 if (DECL_RTL_SET_P (decl))
1527 return DECL_RTL (decl);
1529 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1530 call new_alias_set. If running with -fcompare-debug, sometimes
1531 we do not want to create alias sets that will throw the alias
1532 numbers off in the comparison dumps. So... clearing
1533 flag_strict_aliasing will keep new_alias_set() from creating a
1534 new set. */
1535 save_aliasing_flag = flag_strict_aliasing;
1536 flag_strict_aliasing = 0;
1538 rtl = DECL_RTL (decl);
1539 /* Reset DECL_RTL back, as various parts of the compiler expects
1540 DECL_RTL set meaning it is actually going to be output. */
1541 SET_DECL_RTL (decl, NULL);
1543 flag_strict_aliasing = save_aliasing_flag;
1544 return rtl;
1547 /* Output a string of literal assembler code
1548 for an `asm' keyword used between functions. */
1550 void
1551 assemble_asm (tree string)
1553 const char *p;
1554 app_enable ();
1556 if (TREE_CODE (string) == ADDR_EXPR)
1557 string = TREE_OPERAND (string, 0);
1559 p = TREE_STRING_POINTER (string);
1560 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1563 /* Write the address of the entity given by SYMBOL to SEC. */
1564 void
1565 assemble_addr_to_section (rtx symbol, section *sec)
1567 switch_to_section (sec);
1568 assemble_align (POINTER_SIZE);
1569 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1572 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1573 not) section for PRIORITY. */
1574 section *
1575 get_cdtor_priority_section (int priority, bool constructor_p)
1577 /* Buffer conservatively large enough for the full range of a 32-bit
1578 int plus the text below. */
1579 char buf[18];
1581 /* ??? This only works reliably with the GNU linker. */
1582 sprintf (buf, "%s.%.5u",
1583 constructor_p ? ".ctors" : ".dtors",
1584 /* Invert the numbering so the linker puts us in the proper
1585 order; constructors are run from right to left, and the
1586 linker sorts in increasing order. */
1587 MAX_INIT_PRIORITY - priority);
1588 return get_section (buf, SECTION_WRITE, NULL);
1591 void
1592 default_named_section_asm_out_destructor (rtx symbol, int priority)
1594 section *sec;
1596 if (priority != DEFAULT_INIT_PRIORITY)
1597 sec = get_cdtor_priority_section (priority,
1598 /*constructor_p=*/false);
1599 else
1600 sec = get_section (".dtors", SECTION_WRITE, NULL);
1602 assemble_addr_to_section (symbol, sec);
1605 #ifdef DTORS_SECTION_ASM_OP
1606 void
1607 default_dtor_section_asm_out_destructor (rtx symbol,
1608 int priority ATTRIBUTE_UNUSED)
1610 assemble_addr_to_section (symbol, dtors_section);
1612 #endif
1614 void
1615 default_named_section_asm_out_constructor (rtx symbol, int priority)
1617 section *sec;
1619 if (priority != DEFAULT_INIT_PRIORITY)
1620 sec = get_cdtor_priority_section (priority,
1621 /*constructor_p=*/true);
1622 else
1623 sec = get_section (".ctors", SECTION_WRITE, NULL);
1625 assemble_addr_to_section (symbol, sec);
1628 #ifdef CTORS_SECTION_ASM_OP
1629 void
1630 default_ctor_section_asm_out_constructor (rtx symbol,
1631 int priority ATTRIBUTE_UNUSED)
1633 assemble_addr_to_section (symbol, ctors_section);
1635 #endif
1637 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1638 a nonzero value if the constant pool should be output before the
1639 start of the function, or a zero value if the pool should output
1640 after the end of the function. The default is to put it before the
1641 start. */
1643 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1644 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1645 #endif
1647 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1648 to be output to assembler.
1649 Set first_global_object_name and weak_global_object_name as appropriate. */
1651 void
1652 notice_global_symbol (tree decl)
1654 const char **t = &first_global_object_name;
1656 if (first_global_object_name
1657 || !TREE_PUBLIC (decl)
1658 || DECL_EXTERNAL (decl)
1659 || !DECL_NAME (decl)
1660 || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1661 || (TREE_CODE (decl) != FUNCTION_DECL
1662 && (!VAR_P (decl)
1663 || (DECL_COMMON (decl)
1664 && (DECL_INITIAL (decl) == 0
1665 || DECL_INITIAL (decl) == error_mark_node)))))
1666 return;
1668 /* We win when global object is found, but it is useful to know about weak
1669 symbol as well so we can produce nicer unique names. */
1670 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1671 t = &weak_global_object_name;
1673 if (!*t)
1675 tree id = DECL_ASSEMBLER_NAME (decl);
1676 ultimate_transparent_alias_target (&id);
1677 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1681 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1682 current function goes into the cold section, so that targets can use
1683 current_function_section during RTL expansion. DECL describes the
1684 function. */
1686 void
1687 decide_function_section (tree decl)
1689 first_function_block_is_cold = false;
1691 if (DECL_SECTION_NAME (decl))
1693 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1694 /* Calls to function_section rely on first_function_block_is_cold
1695 being accurate. */
1696 first_function_block_is_cold = (node
1697 && node->frequency
1698 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1701 in_cold_section_p = first_function_block_is_cold;
1704 /* Get the function's name, as described by its RTL. This may be
1705 different from the DECL_NAME name used in the source file. */
1706 const char *
1707 get_fnname_from_decl (tree decl)
1709 rtx x = DECL_RTL (decl);
1710 gcc_assert (MEM_P (x));
1711 x = XEXP (x, 0);
1712 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1713 return XSTR (x, 0);
1716 /* Output assembler code for the constant pool of a function and associated
1717 with defining the name of the function. DECL describes the function.
1718 NAME is the function's name. For the constant pool, we use the current
1719 constant pool data. */
1721 void
1722 assemble_start_function (tree decl, const char *fnname)
1724 int align;
1725 char tmp_label[100];
1726 bool hot_label_written = false;
1728 if (crtl->has_bb_partition)
1730 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1731 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1732 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1733 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1734 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1735 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1736 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1737 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1738 const_labelno++;
1739 cold_function_name = NULL_TREE;
1741 else
1743 crtl->subsections.hot_section_label = NULL;
1744 crtl->subsections.cold_section_label = NULL;
1745 crtl->subsections.hot_section_end_label = NULL;
1746 crtl->subsections.cold_section_end_label = NULL;
1749 /* The following code does not need preprocessing in the assembler. */
1751 app_disable ();
1753 if (CONSTANT_POOL_BEFORE_FUNCTION)
1754 output_constant_pool (fnname, decl);
1756 align = symtab_node::get (decl)->definition_alignment ();
1758 /* Make sure the not and cold text (code) sections are properly
1759 aligned. This is necessary here in the case where the function
1760 has both hot and cold sections, because we don't want to re-set
1761 the alignment when the section switch happens mid-function. */
1763 if (crtl->has_bb_partition)
1765 first_function_block_is_cold = false;
1767 switch_to_section (unlikely_text_section ());
1768 assemble_align (align);
1769 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1771 /* When the function starts with a cold section, we need to explicitly
1772 align the hot section and write out the hot section label.
1773 But if the current function is a thunk, we do not have a CFG. */
1774 if (!cfun->is_thunk
1775 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1777 switch_to_section (text_section);
1778 assemble_align (align);
1779 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1780 hot_label_written = true;
1781 first_function_block_is_cold = true;
1783 in_cold_section_p = first_function_block_is_cold;
1787 /* Switch to the correct text section for the start of the function. */
1789 switch_to_section (function_section (decl));
1790 if (crtl->has_bb_partition && !hot_label_written)
1791 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1793 /* Tell assembler to move to target machine's alignment for functions. */
1794 align = floor_log2 (align / BITS_PER_UNIT);
1795 if (align > 0)
1797 ASM_OUTPUT_ALIGN (asm_out_file, align);
1800 /* Handle a user-specified function alignment.
1801 Note that we still need to align to DECL_ALIGN, as above,
1802 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1803 if (! DECL_USER_ALIGN (decl)
1804 && align_functions_log > align
1805 && optimize_function_for_speed_p (cfun))
1807 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1808 int align_log = align_functions_log;
1809 #endif
1810 int max_skip = align_functions - 1;
1811 if (flag_limit_function_alignment && crtl->max_insn_address > 0
1812 && max_skip >= crtl->max_insn_address)
1813 max_skip = crtl->max_insn_address - 1;
1815 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1816 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1817 #else
1818 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1819 #endif
1822 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1823 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1824 #endif
1826 if (!DECL_IGNORED_P (decl))
1827 (*debug_hooks->begin_function) (decl);
1829 /* Make function name accessible from other files, if appropriate. */
1831 if (TREE_PUBLIC (decl))
1833 notice_global_symbol (decl);
1835 globalize_decl (decl);
1837 maybe_assemble_visibility (decl);
1840 if (DECL_PRESERVE_P (decl))
1841 targetm.asm_out.mark_decl_preserved (fnname);
1843 unsigned HOST_WIDE_INT patch_area_size = function_entry_patch_area_size;
1844 unsigned HOST_WIDE_INT patch_area_entry = function_entry_patch_area_start;
1846 tree patchable_function_entry_attr
1847 = lookup_attribute ("patchable_function_entry", DECL_ATTRIBUTES (decl));
1848 if (patchable_function_entry_attr)
1850 tree pp_val = TREE_VALUE (patchable_function_entry_attr);
1851 tree patchable_function_entry_value1 = TREE_VALUE (pp_val);
1853 if (tree_fits_uhwi_p (patchable_function_entry_value1))
1854 patch_area_size = tree_to_uhwi (patchable_function_entry_value1);
1855 else
1856 gcc_unreachable ();
1858 patch_area_entry = 0;
1859 if (list_length (pp_val) > 1)
1861 tree patchable_function_entry_value2 =
1862 TREE_VALUE (TREE_CHAIN (pp_val));
1864 if (tree_fits_uhwi_p (patchable_function_entry_value2))
1865 patch_area_entry = tree_to_uhwi (patchable_function_entry_value2);
1866 else
1867 gcc_unreachable ();
1871 if (patch_area_entry > patch_area_size)
1873 if (patch_area_size > 0)
1874 warning (OPT_Wattributes, "Patchable function entry > size");
1875 patch_area_entry = 0;
1878 /* Emit the patching area before the entry label, if any. */
1879 if (patch_area_entry > 0)
1880 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1881 patch_area_entry, true);
1883 /* Do any machine/system dependent processing of the function name. */
1884 #ifdef ASM_DECLARE_FUNCTION_NAME
1885 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1886 #else
1887 /* Standard thing is just output label for the function. */
1888 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1889 #endif /* ASM_DECLARE_FUNCTION_NAME */
1891 /* And the area after the label. Record it if we haven't done so yet. */
1892 if (patch_area_size > patch_area_entry)
1893 targetm.asm_out.print_patchable_function_entry (asm_out_file,
1894 patch_area_size-patch_area_entry,
1895 patch_area_entry == 0);
1897 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1898 saw_no_split_stack = true;
1901 /* Output assembler code associated with defining the size of the
1902 function. DECL describes the function. NAME is the function's name. */
1904 void
1905 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1907 #ifdef ASM_DECLARE_FUNCTION_SIZE
1908 /* We could have switched section in the middle of the function. */
1909 if (crtl->has_bb_partition)
1910 switch_to_section (function_section (decl));
1911 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1912 #endif
1913 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1915 output_constant_pool (fnname, decl);
1916 switch_to_section (function_section (decl)); /* need to switch back */
1918 /* Output labels for end of hot/cold text sections (to be used by
1919 debug info.) */
1920 if (crtl->has_bb_partition)
1922 section *save_text_section;
1924 save_text_section = in_section;
1925 switch_to_section (unlikely_text_section ());
1926 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
1927 if (cold_function_name != NULL_TREE)
1928 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
1929 IDENTIFIER_POINTER (cold_function_name),
1930 decl);
1931 #endif
1932 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1933 if (first_function_block_is_cold)
1934 switch_to_section (text_section);
1935 else
1936 switch_to_section (function_section (decl));
1937 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1938 switch_to_section (save_text_section);
1942 /* Assemble code to leave SIZE bytes of zeros. */
1944 void
1945 assemble_zeros (unsigned HOST_WIDE_INT size)
1947 /* Do no output if -fsyntax-only. */
1948 if (flag_syntax_only)
1949 return;
1951 #ifdef ASM_NO_SKIP_IN_TEXT
1952 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1953 so we must output 0s explicitly in the text section. */
1954 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1956 unsigned HOST_WIDE_INT i;
1957 for (i = 0; i < size; i++)
1958 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1960 else
1961 #endif
1962 if (size > 0)
1963 ASM_OUTPUT_SKIP (asm_out_file, size);
1966 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1968 void
1969 assemble_align (int align)
1971 if (align > BITS_PER_UNIT)
1973 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1977 /* Assemble a string constant with the specified C string as contents. */
1979 void
1980 assemble_string (const char *p, int size)
1982 int pos = 0;
1983 int maximum = 2000;
1985 /* If the string is very long, split it up. */
1987 while (pos < size)
1989 int thissize = size - pos;
1990 if (thissize > maximum)
1991 thissize = maximum;
1993 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1995 pos += thissize;
1996 p += thissize;
2001 /* A noswitch_section_callback for lcomm_section. */
2003 static bool
2004 emit_local (tree decl ATTRIBUTE_UNUSED,
2005 const char *name ATTRIBUTE_UNUSED,
2006 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2007 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2009 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2010 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2011 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2012 size, align);
2013 return true;
2014 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2015 unsigned int align = symtab_node::get (decl)->definition_alignment ();
2016 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2017 return true;
2018 #else
2019 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2020 return false;
2021 #endif
2024 /* A noswitch_section_callback for bss_noswitch_section. */
2026 #if defined ASM_OUTPUT_ALIGNED_BSS
2027 static bool
2028 emit_bss (tree decl ATTRIBUTE_UNUSED,
2029 const char *name ATTRIBUTE_UNUSED,
2030 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2031 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2033 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2034 get_variable_align (decl));
2035 return true;
2037 #endif
2039 /* A noswitch_section_callback for comm_section. */
2041 static bool
2042 emit_common (tree decl ATTRIBUTE_UNUSED,
2043 const char *name ATTRIBUTE_UNUSED,
2044 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2045 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2047 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2048 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2049 size, get_variable_align (decl));
2050 return true;
2051 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2052 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2053 get_variable_align (decl));
2054 return true;
2055 #else
2056 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2057 return false;
2058 #endif
2061 /* A noswitch_section_callback for tls_comm_section. */
2063 static bool
2064 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2065 const char *name ATTRIBUTE_UNUSED,
2066 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2067 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2069 #ifdef ASM_OUTPUT_TLS_COMMON
2070 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2071 return true;
2072 #else
2073 sorry ("thread-local COMMON data not implemented");
2074 return true;
2075 #endif
2078 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2079 NAME is the name of DECL's SYMBOL_REF. */
2081 static void
2082 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2083 unsigned int align)
2085 unsigned HOST_WIDE_INT size, rounded;
2087 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2088 rounded = size;
2090 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2091 size += asan_red_zone_size (size);
2093 /* Don't allocate zero bytes of common,
2094 since that means "undefined external" in the linker. */
2095 if (size == 0)
2096 rounded = 1;
2098 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2099 so that each uninitialized object starts on such a boundary. */
2100 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2101 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2102 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2104 if (!sect->noswitch.callback (decl, name, size, rounded)
2105 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2106 error ("requested alignment for %q+D is greater than "
2107 "implemented alignment of %wu", decl, rounded);
2110 /* A subroutine of assemble_variable. Output the label and contents of
2111 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2112 is as for assemble_variable. */
2114 static void
2115 assemble_variable_contents (tree decl, const char *name,
2116 bool dont_output_data)
2118 /* Do any machine/system dependent processing of the object. */
2119 #ifdef ASM_DECLARE_OBJECT_NAME
2120 last_assemble_variable_decl = decl;
2121 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2122 #else
2123 /* Standard thing is just output label for the object. */
2124 ASM_OUTPUT_LABEL (asm_out_file, name);
2125 #endif /* ASM_DECLARE_OBJECT_NAME */
2127 if (!dont_output_data)
2129 /* Caller is supposed to use varpool_get_constructor when it wants
2130 to output the body. */
2131 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2132 if (DECL_INITIAL (decl)
2133 && DECL_INITIAL (decl) != error_mark_node
2134 && !initializer_zerop (DECL_INITIAL (decl)))
2135 /* Output the actual data. */
2136 output_constant (DECL_INITIAL (decl),
2137 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2138 get_variable_align (decl),
2139 false);
2140 else
2141 /* Leave space for it. */
2142 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2143 targetm.asm_out.decl_end ();
2147 /* Write out assembly for the variable DECL, which is not defined in
2148 the current translation unit. */
2149 void
2150 assemble_undefined_decl (tree decl)
2152 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2153 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2156 /* Assemble everything that is needed for a variable or function declaration.
2157 Not used for automatic variables, and not used for function definitions.
2158 Should not be called for variables of incomplete structure type.
2160 TOP_LEVEL is nonzero if this variable has file scope.
2161 AT_END is nonzero if this is the special handling, at end of compilation,
2162 to define things that have had only tentative definitions.
2163 DONT_OUTPUT_DATA if nonzero means don't actually output the
2164 initial value (that will be done by the caller). */
2166 void
2167 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2168 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2170 const char *name;
2171 rtx decl_rtl, symbol;
2172 section *sect;
2173 unsigned int align;
2174 bool asan_protected = false;
2176 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2177 gcc_assert (VAR_P (decl));
2179 /* Emulated TLS had better not get this far. */
2180 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2182 last_assemble_variable_decl = 0;
2184 /* Normally no need to say anything here for external references,
2185 since assemble_external is called by the language-specific code
2186 when a declaration is first seen. */
2188 if (DECL_EXTERNAL (decl))
2189 return;
2191 /* Do nothing for global register variables. */
2192 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2194 TREE_ASM_WRITTEN (decl) = 1;
2195 return;
2198 /* If type was incomplete when the variable was declared,
2199 see if it is complete now. */
2201 if (DECL_SIZE (decl) == 0)
2202 layout_decl (decl, 0);
2204 /* Still incomplete => don't allocate it; treat the tentative defn
2205 (which is what it must have been) as an `extern' reference. */
2207 if (!dont_output_data && DECL_SIZE (decl) == 0)
2209 error ("storage size of %q+D isn%'t known", decl);
2210 TREE_ASM_WRITTEN (decl) = 1;
2211 return;
2214 /* The first declaration of a variable that comes through this function
2215 decides whether it is global (in C, has external linkage)
2216 or local (in C, has internal linkage). So do nothing more
2217 if this function has already run. */
2219 if (TREE_ASM_WRITTEN (decl))
2220 return;
2222 /* Make sure targetm.encode_section_info is invoked before we set
2223 ASM_WRITTEN. */
2224 decl_rtl = DECL_RTL (decl);
2226 TREE_ASM_WRITTEN (decl) = 1;
2228 /* Do no output if -fsyntax-only. */
2229 if (flag_syntax_only)
2230 return;
2232 if (! dont_output_data
2233 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2235 error ("size of variable %q+D is too large", decl);
2236 return;
2239 gcc_assert (MEM_P (decl_rtl));
2240 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2241 symbol = XEXP (decl_rtl, 0);
2243 /* If this symbol belongs to the tree constant pool, output the constant
2244 if it hasn't already been written. */
2245 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2247 tree decl = SYMBOL_REF_DECL (symbol);
2248 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2249 output_constant_def_contents (symbol);
2250 return;
2253 app_disable ();
2255 name = XSTR (symbol, 0);
2256 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2257 notice_global_symbol (decl);
2259 /* Compute the alignment of this data. */
2261 align_variable (decl, dont_output_data);
2263 if ((flag_sanitize & SANITIZE_ADDRESS)
2264 && asan_protect_global (decl))
2266 asan_protected = true;
2267 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2268 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2271 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2273 align = get_variable_align (decl);
2275 if (TREE_PUBLIC (decl))
2276 maybe_assemble_visibility (decl);
2278 if (DECL_PRESERVE_P (decl))
2279 targetm.asm_out.mark_decl_preserved (name);
2281 /* First make the assembler name(s) global if appropriate. */
2282 sect = get_variable_section (decl, false);
2283 if (TREE_PUBLIC (decl)
2284 && (sect->common.flags & SECTION_COMMON) == 0)
2285 globalize_decl (decl);
2287 /* Output any data that we will need to use the address of. */
2288 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2289 output_addressed_constants (DECL_INITIAL (decl));
2291 /* dbxout.c needs to know this. */
2292 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2293 DECL_IN_TEXT_SECTION (decl) = 1;
2295 /* If the decl is part of an object_block, make sure that the decl
2296 has been positioned within its block, but do not write out its
2297 definition yet. output_object_blocks will do that later. */
2298 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2300 gcc_assert (!dont_output_data);
2301 place_block_symbol (symbol);
2303 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2304 assemble_noswitch_variable (decl, name, sect, align);
2305 else
2307 /* Special-case handling of vtv comdat sections. */
2308 if (sect->named.name
2309 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2310 handle_vtv_comdat_section (sect, decl);
2311 else
2312 switch_to_section (sect);
2313 if (align > BITS_PER_UNIT)
2314 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2315 assemble_variable_contents (decl, name, dont_output_data);
2316 if (asan_protected)
2318 unsigned HOST_WIDE_INT int size
2319 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2320 assemble_zeros (asan_red_zone_size (size));
2326 /* Given a function declaration (FN_DECL), this function assembles the
2327 function into the .preinit_array section. */
2329 void
2330 assemble_vtv_preinit_initializer (tree fn_decl)
2332 section *sect;
2333 unsigned flags = SECTION_WRITE;
2334 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2336 flags |= SECTION_NOTYPE;
2337 sect = get_section (".preinit_array", flags, fn_decl);
2338 switch_to_section (sect);
2339 assemble_addr_to_section (symbol, sect);
2342 /* Return 1 if type TYPE contains any pointers. */
2344 static int
2345 contains_pointers_p (tree type)
2347 switch (TREE_CODE (type))
2349 case POINTER_TYPE:
2350 case REFERENCE_TYPE:
2351 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2352 so I'll play safe and return 1. */
2353 case OFFSET_TYPE:
2354 return 1;
2356 case RECORD_TYPE:
2357 case UNION_TYPE:
2358 case QUAL_UNION_TYPE:
2360 tree fields;
2361 /* For a type that has fields, see if the fields have pointers. */
2362 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2363 if (TREE_CODE (fields) == FIELD_DECL
2364 && contains_pointers_p (TREE_TYPE (fields)))
2365 return 1;
2366 return 0;
2369 case ARRAY_TYPE:
2370 /* An array type contains pointers if its element type does. */
2371 return contains_pointers_p (TREE_TYPE (type));
2373 default:
2374 return 0;
2378 /* We delay assemble_external processing until
2379 the compilation unit is finalized. This is the best we can do for
2380 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2381 it all the way to final. See PR 17982 for further discussion. */
2382 static GTY(()) tree pending_assemble_externals;
2384 #ifdef ASM_OUTPUT_EXTERNAL
2385 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2386 As a result, assemble_external can be called after the list of externals
2387 is processed and the pointer set destroyed. */
2388 static bool pending_assemble_externals_processed;
2390 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2391 TREE_LIST in assemble_external. */
2392 static hash_set<tree> *pending_assemble_externals_set;
2394 /* True if DECL is a function decl for which no out-of-line copy exists.
2395 It is assumed that DECL's assembler name has been set. */
2397 static bool
2398 incorporeal_function_p (tree decl)
2400 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2402 const char *name;
2404 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2405 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2406 return true;
2408 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2409 /* Atomic or sync builtins which have survived this far will be
2410 resolved externally and therefore are not incorporeal. */
2411 if (strncmp (name, "__builtin_", 10) == 0)
2412 return true;
2414 return false;
2417 /* Actually do the tests to determine if this is necessary, and invoke
2418 ASM_OUTPUT_EXTERNAL. */
2419 static void
2420 assemble_external_real (tree decl)
2422 rtx rtl = DECL_RTL (decl);
2424 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2425 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2426 && !incorporeal_function_p (decl))
2428 /* Some systems do require some output. */
2429 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2430 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2433 #endif
2435 void
2436 process_pending_assemble_externals (void)
2438 #ifdef ASM_OUTPUT_EXTERNAL
2439 tree list;
2440 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2441 assemble_external_real (TREE_VALUE (list));
2443 pending_assemble_externals = 0;
2444 pending_assemble_externals_processed = true;
2445 delete pending_assemble_externals_set;
2446 #endif
2449 /* This TREE_LIST contains any weak symbol declarations waiting
2450 to be emitted. */
2451 static GTY(()) tree weak_decls;
2453 /* Output something to declare an external symbol to the assembler,
2454 and qualifiers such as weakness. (Most assemblers don't need
2455 extern declaration, so we normally output nothing.) Do nothing if
2456 DECL is not external. */
2458 void
2459 assemble_external (tree decl ATTRIBUTE_UNUSED)
2461 /* Make sure that the ASM_OUT_FILE is open.
2462 If it's not, we should not be calling this function. */
2463 gcc_assert (asm_out_file);
2465 /* In a perfect world, the following condition would be true.
2466 Sadly, the Go front end emit assembly *from the front end*,
2467 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2468 #if 0
2469 /* This function should only be called if we are expanding, or have
2470 expanded, to RTL.
2471 Ideally, only final.c would be calling this function, but it is
2472 not clear whether that would break things somehow. See PR 17982
2473 for further discussion. */
2474 gcc_assert (state == EXPANSION
2475 || state == FINISHED);
2476 #endif
2478 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2479 return;
2481 /* We want to output annotation for weak and external symbols at
2482 very last to check if they are references or not. */
2484 if (TARGET_SUPPORTS_WEAK
2485 && DECL_WEAK (decl)
2486 /* TREE_STATIC is a weird and abused creature which is not
2487 generally the right test for whether an entity has been
2488 locally emitted, inlined or otherwise not-really-extern, but
2489 for declarations that can be weak, it happens to be
2490 match. */
2491 && !TREE_STATIC (decl)
2492 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2493 && value_member (decl, weak_decls) == NULL_TREE)
2494 weak_decls = tree_cons (NULL, decl, weak_decls);
2496 #ifdef ASM_OUTPUT_EXTERNAL
2497 if (pending_assemble_externals_processed)
2499 assemble_external_real (decl);
2500 return;
2503 if (! pending_assemble_externals_set->add (decl))
2504 pending_assemble_externals = tree_cons (NULL, decl,
2505 pending_assemble_externals);
2506 #endif
2509 /* Similar, for calling a library function FUN. */
2511 void
2512 assemble_external_libcall (rtx fun)
2514 /* Declare library function name external when first used, if nec. */
2515 if (! SYMBOL_REF_USED (fun))
2517 SYMBOL_REF_USED (fun) = 1;
2518 targetm.asm_out.external_libcall (fun);
2522 /* Assemble a label named NAME. */
2524 void
2525 assemble_label (FILE *file, const char *name)
2527 ASM_OUTPUT_LABEL (file, name);
2530 /* Set the symbol_referenced flag for ID. */
2531 void
2532 mark_referenced (tree id)
2534 TREE_SYMBOL_REFERENCED (id) = 1;
2537 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2538 void
2539 mark_decl_referenced (tree decl)
2541 if (TREE_CODE (decl) == FUNCTION_DECL)
2543 /* Extern inline functions don't become needed when referenced.
2544 If we know a method will be emitted in other TU and no new
2545 functions can be marked reachable, just use the external
2546 definition. */
2547 struct cgraph_node *node = cgraph_node::get_create (decl);
2548 if (!DECL_EXTERNAL (decl)
2549 && !node->definition)
2550 node->mark_force_output ();
2552 else if (VAR_P (decl))
2554 varpool_node *node = varpool_node::get_create (decl);
2555 /* C++ frontend use mark_decl_references to force COMDAT variables
2556 to be output that might appear dead otherwise. */
2557 node->force_output = true;
2559 /* else do nothing - we can get various sorts of CST nodes here,
2560 which do not need to be marked. */
2564 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2565 starts with a *, the rest of NAME is output verbatim. Otherwise
2566 NAME is transformed in a target-specific way (usually by the
2567 addition of an underscore). */
2569 void
2570 assemble_name_raw (FILE *file, const char *name)
2572 if (name[0] == '*')
2573 fputs (&name[1], file);
2574 else
2575 ASM_OUTPUT_LABELREF (file, name);
2578 /* Like assemble_name_raw, but should be used when NAME might refer to
2579 an entity that is also represented as a tree (like a function or
2580 variable). If NAME does refer to such an entity, that entity will
2581 be marked as referenced. */
2583 void
2584 assemble_name (FILE *file, const char *name)
2586 const char *real_name;
2587 tree id;
2589 real_name = targetm.strip_name_encoding (name);
2591 id = maybe_get_identifier (real_name);
2592 if (id)
2594 tree id_orig = id;
2596 mark_referenced (id);
2597 ultimate_transparent_alias_target (&id);
2598 if (id != id_orig)
2599 name = IDENTIFIER_POINTER (id);
2600 gcc_assert (! TREE_CHAIN (id));
2603 assemble_name_raw (file, name);
2606 /* Allocate SIZE bytes writable static space with a gensym name
2607 and return an RTX to refer to its address. */
2610 assemble_static_space (unsigned HOST_WIDE_INT size)
2612 char name[17];
2613 const char *namestring;
2614 rtx x;
2616 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2617 ++const_labelno;
2618 namestring = ggc_strdup (name);
2620 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2621 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2623 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2624 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2625 BIGGEST_ALIGNMENT);
2626 #else
2627 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2628 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2629 #else
2631 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2632 so that each uninitialized object starts on such a boundary. */
2633 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2634 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2635 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2636 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2637 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2638 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2640 #endif
2641 #endif
2642 return x;
2645 /* Assemble the static constant template for function entry trampolines.
2646 This is done at most once per compilation.
2647 Returns an RTX for the address of the template. */
2649 static GTY(()) rtx initial_trampoline;
2652 assemble_trampoline_template (void)
2654 char label[256];
2655 const char *name;
2656 int align;
2657 rtx symbol;
2659 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2661 if (initial_trampoline)
2662 return initial_trampoline;
2664 /* By default, put trampoline templates in read-only data section. */
2666 #ifdef TRAMPOLINE_SECTION
2667 switch_to_section (TRAMPOLINE_SECTION);
2668 #else
2669 switch_to_section (readonly_data_section);
2670 #endif
2672 /* Write the assembler code to define one. */
2673 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2674 if (align > 0)
2675 ASM_OUTPUT_ALIGN (asm_out_file, align);
2677 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2678 targetm.asm_out.trampoline_template (asm_out_file);
2680 /* Record the rtl to refer to it. */
2681 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2682 name = ggc_strdup (label);
2683 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2684 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2686 initial_trampoline = gen_const_mem (BLKmode, symbol);
2687 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2688 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2690 return initial_trampoline;
2693 /* A and B are either alignments or offsets. Return the minimum alignment
2694 that may be assumed after adding the two together. */
2696 static inline unsigned
2697 min_align (unsigned int a, unsigned int b)
2699 return least_bit_hwi (a | b);
2702 /* Return the assembler directive for creating a given kind of integer
2703 object. SIZE is the number of bytes in the object and ALIGNED_P
2704 indicates whether it is known to be aligned. Return NULL if the
2705 assembly dialect has no such directive.
2707 The returned string should be printed at the start of a new line and
2708 be followed immediately by the object's initial value. */
2710 const char *
2711 integer_asm_op (int size, int aligned_p)
2713 struct asm_int_op *ops;
2715 if (aligned_p)
2716 ops = &targetm.asm_out.aligned_op;
2717 else
2718 ops = &targetm.asm_out.unaligned_op;
2720 switch (size)
2722 case 1:
2723 return targetm.asm_out.byte_op;
2724 case 2:
2725 return ops->hi;
2726 case 4:
2727 return ops->si;
2728 case 8:
2729 return ops->di;
2730 case 16:
2731 return ops->ti;
2732 default:
2733 return NULL;
2737 /* Use directive OP to assemble an integer object X. Print OP at the
2738 start of the line, followed immediately by the value of X. */
2740 void
2741 assemble_integer_with_op (const char *op, rtx x)
2743 fputs (op, asm_out_file);
2744 output_addr_const (asm_out_file, x);
2745 fputc ('\n', asm_out_file);
2748 /* The default implementation of the asm_out.integer target hook. */
2750 bool
2751 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2752 unsigned int size ATTRIBUTE_UNUSED,
2753 int aligned_p ATTRIBUTE_UNUSED)
2755 const char *op = integer_asm_op (size, aligned_p);
2756 /* Avoid GAS bugs for large values. Specifically negative values whose
2757 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2758 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2759 return false;
2760 return op && (assemble_integer_with_op (op, x), true);
2763 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2764 the alignment of the integer in bits. Return 1 if we were able to output
2765 the constant, otherwise 0. We must be able to output the constant,
2766 if FORCE is nonzero. */
2768 bool
2769 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2771 int aligned_p;
2773 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2775 /* See if the target hook can handle this kind of object. */
2776 if (targetm.asm_out.integer (x, size, aligned_p))
2777 return true;
2779 /* If the object is a multi-byte one, try splitting it up. Split
2780 it into words it if is multi-word, otherwise split it into bytes. */
2781 if (size > 1)
2783 machine_mode omode, imode;
2784 unsigned int subalign;
2785 unsigned int subsize, i;
2786 enum mode_class mclass;
2788 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2789 subalign = MIN (align, subsize * BITS_PER_UNIT);
2790 if (GET_CODE (x) == CONST_FIXED)
2791 mclass = GET_MODE_CLASS (GET_MODE (x));
2792 else
2793 mclass = MODE_INT;
2795 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2796 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2798 for (i = 0; i < size; i += subsize)
2800 rtx partial = simplify_subreg (omode, x, imode, i);
2801 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2802 break;
2804 if (i == size)
2805 return true;
2807 /* If we've printed some of it, but not all of it, there's no going
2808 back now. */
2809 gcc_assert (!i);
2812 gcc_assert (!force);
2814 return false;
2817 /* Assemble the floating-point constant D into an object of size MODE. ALIGN
2818 is the alignment of the constant in bits. If REVERSE is true, D is output
2819 in reverse storage order. */
2821 void
2822 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2823 bool reverse)
2825 long data[4] = {0, 0, 0, 0};
2826 int bitsize, nelts, nunits, units_per;
2827 rtx elt;
2829 /* This is hairy. We have a quantity of known size. real_to_target
2830 will put it into an array of *host* longs, 32 bits per element
2831 (even if long is more than 32 bits). We need to determine the
2832 number of array elements that are occupied (nelts) and the number
2833 of *target* min-addressable units that will be occupied in the
2834 object file (nunits). We cannot assume that 32 divides the
2835 mode's bitsize (size * BITS_PER_UNIT) evenly.
2837 size * BITS_PER_UNIT is used here to make sure that padding bits
2838 (which might appear at either end of the value; real_to_target
2839 will include the padding bits in its output array) are included. */
2841 nunits = GET_MODE_SIZE (mode);
2842 bitsize = nunits * BITS_PER_UNIT;
2843 nelts = CEIL (bitsize, 32);
2844 units_per = 32 / BITS_PER_UNIT;
2846 real_to_target (data, &d, mode);
2848 /* Put out the first word with the specified alignment. */
2849 if (reverse)
2850 elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
2851 else
2852 elt = GEN_INT (data[0]);
2853 assemble_integer (elt, MIN (nunits, units_per), align, 1);
2854 nunits -= units_per;
2856 /* Subsequent words need only 32-bit alignment. */
2857 align = min_align (align, 32);
2859 for (int i = 1; i < nelts; i++)
2861 if (reverse)
2862 elt = flip_storage_order (SImode,
2863 gen_int_mode (data[nelts - 1 - i], SImode));
2864 else
2865 elt = GEN_INT (data[i]);
2866 assemble_integer (elt, MIN (nunits, units_per), align, 1);
2867 nunits -= units_per;
2871 /* Given an expression EXP with a constant value,
2872 reduce it to the sum of an assembler symbol and an integer.
2873 Store them both in the structure *VALUE.
2874 EXP must be reducible. */
2876 struct addr_const {
2877 rtx base;
2878 poly_int64 offset;
2881 static void
2882 decode_addr_const (tree exp, struct addr_const *value)
2884 tree target = TREE_OPERAND (exp, 0);
2885 poly_int64 offset = 0;
2886 rtx x;
2888 while (1)
2890 poly_int64 bytepos;
2891 if (TREE_CODE (target) == COMPONENT_REF
2892 && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
2893 &bytepos))
2895 offset += bytepos;
2896 target = TREE_OPERAND (target, 0);
2898 else if (TREE_CODE (target) == ARRAY_REF
2899 || TREE_CODE (target) == ARRAY_RANGE_REF)
2901 /* Truncate big offset. */
2902 offset
2903 += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2904 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
2905 target = TREE_OPERAND (target, 0);
2907 else if (TREE_CODE (target) == MEM_REF
2908 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2910 offset += mem_ref_offset (target).force_shwi ();
2911 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2913 else if (TREE_CODE (target) == INDIRECT_REF
2914 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2915 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2916 == ADDR_EXPR)
2917 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2918 else
2919 break;
2922 switch (TREE_CODE (target))
2924 case VAR_DECL:
2925 case FUNCTION_DECL:
2926 x = DECL_RTL (target);
2927 break;
2929 case LABEL_DECL:
2930 x = gen_rtx_MEM (FUNCTION_MODE,
2931 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2932 break;
2934 case REAL_CST:
2935 case FIXED_CST:
2936 case STRING_CST:
2937 case COMPLEX_CST:
2938 case CONSTRUCTOR:
2939 case INTEGER_CST:
2940 x = output_constant_def (target, 1);
2941 break;
2943 case INDIRECT_REF:
2944 /* This deals with absolute addresses. */
2945 offset += tree_to_shwi (TREE_OPERAND (target, 0));
2946 x = gen_rtx_MEM (QImode,
2947 gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
2948 break;
2950 default:
2951 gcc_unreachable ();
2954 gcc_assert (MEM_P (x));
2955 x = XEXP (x, 0);
2957 value->base = x;
2958 value->offset = offset;
2961 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2963 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2965 /* Constant pool accessor function. */
2967 hash_table<tree_descriptor_hasher> *
2968 constant_pool_htab (void)
2970 return const_desc_htab;
2973 /* Compute a hash code for a constant expression. */
2975 hashval_t
2976 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2978 return ptr->hash;
2981 static hashval_t
2982 const_hash_1 (const tree exp)
2984 const char *p;
2985 hashval_t hi;
2986 int len, i;
2987 enum tree_code code = TREE_CODE (exp);
2989 /* Either set P and LEN to the address and len of something to hash and
2990 exit the switch or return a value. */
2992 switch (code)
2994 case INTEGER_CST:
2995 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2996 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2997 break;
2999 case REAL_CST:
3000 return real_hash (TREE_REAL_CST_PTR (exp));
3002 case FIXED_CST:
3003 return fixed_hash (TREE_FIXED_CST_PTR (exp));
3005 case STRING_CST:
3006 p = TREE_STRING_POINTER (exp);
3007 len = TREE_STRING_LENGTH (exp);
3008 break;
3010 case COMPLEX_CST:
3011 return (const_hash_1 (TREE_REALPART (exp)) * 5
3012 + const_hash_1 (TREE_IMAGPART (exp)));
3014 case VECTOR_CST:
3016 hi = 7 + VECTOR_CST_NPATTERNS (exp);
3017 hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3018 unsigned int count = vector_cst_encoded_nelts (exp);
3019 for (unsigned int i = 0; i < count; ++i)
3020 hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3021 return hi;
3024 case CONSTRUCTOR:
3026 unsigned HOST_WIDE_INT idx;
3027 tree value;
3029 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3031 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3032 if (value)
3033 hi = hi * 603 + const_hash_1 (value);
3035 return hi;
3038 case ADDR_EXPR:
3039 case FDESC_EXPR:
3041 struct addr_const value;
3043 decode_addr_const (exp, &value);
3044 switch (GET_CODE (value.base))
3046 case SYMBOL_REF:
3047 /* Don't hash the address of the SYMBOL_REF;
3048 only use the offset and the symbol name. */
3049 hi = value.offset.coeffs[0];
3050 p = XSTR (value.base, 0);
3051 for (i = 0; p[i] != 0; i++)
3052 hi = ((hi * 613) + (unsigned) (p[i]));
3053 break;
3055 case LABEL_REF:
3056 hi = (value.offset.coeffs[0]
3057 + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3058 break;
3060 default:
3061 gcc_unreachable ();
3064 return hi;
3066 case PLUS_EXPR:
3067 case POINTER_PLUS_EXPR:
3068 case MINUS_EXPR:
3069 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3070 + const_hash_1 (TREE_OPERAND (exp, 1)));
3072 CASE_CONVERT:
3073 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3075 default:
3076 /* A language specific constant. Just hash the code. */
3077 return code;
3080 /* Compute hashing function. */
3081 hi = len;
3082 for (i = 0; i < len; i++)
3083 hi = ((hi * 613) + (unsigned) (p[i]));
3085 return hi;
3088 /* Wrapper of compare_constant, for the htab interface. */
3089 bool
3090 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3091 constant_descriptor_tree *c2)
3093 if (c1->hash != c2->hash)
3094 return 0;
3095 return compare_constant (c1->value, c2->value);
3098 /* Compare t1 and t2, and return 1 only if they are known to result in
3099 the same bit pattern on output. */
3101 static int
3102 compare_constant (const tree t1, const tree t2)
3104 enum tree_code typecode;
3106 if (t1 == NULL_TREE)
3107 return t2 == NULL_TREE;
3108 if (t2 == NULL_TREE)
3109 return 0;
3111 if (TREE_CODE (t1) != TREE_CODE (t2))
3112 return 0;
3114 switch (TREE_CODE (t1))
3116 case INTEGER_CST:
3117 /* Integer constants are the same only if the same width of type. */
3118 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3119 return 0;
3120 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3121 return 0;
3122 return tree_int_cst_equal (t1, t2);
3124 case REAL_CST:
3125 /* Real constants are the same only if the same width of type. In
3126 addition to the same width, we need to check whether the modes are the
3127 same. There might be two floating point modes that are the same size
3128 but have different representations, such as the PowerPC that has 2
3129 different 128-bit floating point types (IBM extended double and IEEE
3130 128-bit floating point). */
3131 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3132 return 0;
3133 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3134 return 0;
3135 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3137 case FIXED_CST:
3138 /* Fixed constants are the same only if the same width of type. */
3139 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3140 return 0;
3142 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3144 case STRING_CST:
3145 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3146 return 0;
3148 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3149 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3150 TREE_STRING_LENGTH (t1)));
3152 case COMPLEX_CST:
3153 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3154 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3156 case VECTOR_CST:
3158 if (VECTOR_CST_NPATTERNS (t1)
3159 != VECTOR_CST_NPATTERNS (t2))
3160 return 0;
3162 if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3163 != VECTOR_CST_NELTS_PER_PATTERN (t2))
3164 return 0;
3166 unsigned int count = vector_cst_encoded_nelts (t1);
3167 for (unsigned int i = 0; i < count; ++i)
3168 if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3169 VECTOR_CST_ENCODED_ELT (t2, i)))
3170 return 0;
3172 return 1;
3175 case CONSTRUCTOR:
3177 vec<constructor_elt, va_gc> *v1, *v2;
3178 unsigned HOST_WIDE_INT idx;
3180 typecode = TREE_CODE (TREE_TYPE (t1));
3181 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3182 return 0;
3184 if (typecode == ARRAY_TYPE)
3186 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3187 /* For arrays, check that mode, size and storage order match. */
3188 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3189 || size_1 == -1
3190 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3191 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3192 != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3193 return 0;
3195 else
3197 /* For record and union constructors, require exact type
3198 equality. */
3199 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3200 return 0;
3203 v1 = CONSTRUCTOR_ELTS (t1);
3204 v2 = CONSTRUCTOR_ELTS (t2);
3205 if (vec_safe_length (v1) != vec_safe_length (v2))
3206 return 0;
3208 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3210 constructor_elt *c1 = &(*v1)[idx];
3211 constructor_elt *c2 = &(*v2)[idx];
3213 /* Check that each value is the same... */
3214 if (!compare_constant (c1->value, c2->value))
3215 return 0;
3216 /* ... and that they apply to the same fields! */
3217 if (typecode == ARRAY_TYPE)
3219 if (!compare_constant (c1->index, c2->index))
3220 return 0;
3222 else
3224 if (c1->index != c2->index)
3225 return 0;
3229 return 1;
3232 case ADDR_EXPR:
3233 case FDESC_EXPR:
3235 struct addr_const value1, value2;
3236 enum rtx_code code;
3237 int ret;
3239 decode_addr_const (t1, &value1);
3240 decode_addr_const (t2, &value2);
3242 if (maybe_ne (value1.offset, value2.offset))
3243 return 0;
3245 code = GET_CODE (value1.base);
3246 if (code != GET_CODE (value2.base))
3247 return 0;
3249 switch (code)
3251 case SYMBOL_REF:
3252 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3253 break;
3255 case LABEL_REF:
3256 ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3257 == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3258 break;
3260 default:
3261 gcc_unreachable ();
3263 return ret;
3266 case PLUS_EXPR:
3267 case POINTER_PLUS_EXPR:
3268 case MINUS_EXPR:
3269 case RANGE_EXPR:
3270 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3271 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3273 CASE_CONVERT:
3274 case VIEW_CONVERT_EXPR:
3275 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3277 default:
3278 return 0;
3281 gcc_unreachable ();
3284 /* Return the section into which constant EXP should be placed. */
3286 static section *
3287 get_constant_section (tree exp, unsigned int align)
3289 return targetm.asm_out.select_section (exp,
3290 compute_reloc_for_constant (exp),
3291 align);
3294 /* Return the size of constant EXP in bytes. */
3296 static HOST_WIDE_INT
3297 get_constant_size (tree exp)
3299 HOST_WIDE_INT size;
3301 size = int_size_in_bytes (TREE_TYPE (exp));
3302 if (TREE_CODE (exp) == STRING_CST)
3303 size = MAX (TREE_STRING_LENGTH (exp), size);
3304 return size;
3307 /* Subroutine of output_constant_def:
3308 No constant equal to EXP is known to have been output.
3309 Make a constant descriptor to enter EXP in the hash table.
3310 Assign the label number and construct RTL to refer to the
3311 constant's location in memory.
3312 Caller is responsible for updating the hash table. */
3314 static struct constant_descriptor_tree *
3315 build_constant_desc (tree exp)
3317 struct constant_descriptor_tree *desc;
3318 rtx symbol, rtl;
3319 char label[256];
3320 int labelno;
3321 tree decl;
3323 desc = ggc_alloc<constant_descriptor_tree> ();
3324 desc->value = exp;
3326 /* Create a string containing the label name, in LABEL. */
3327 labelno = const_labelno++;
3328 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3330 /* Construct the VAR_DECL associated with the constant. */
3331 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3332 TREE_TYPE (exp));
3333 DECL_ARTIFICIAL (decl) = 1;
3334 DECL_IGNORED_P (decl) = 1;
3335 TREE_READONLY (decl) = 1;
3336 TREE_STATIC (decl) = 1;
3337 TREE_ADDRESSABLE (decl) = 1;
3338 /* We don't set the RTL yet as this would cause varpool to assume that the
3339 variable is referenced. Moreover, it would just be dropped in LTO mode.
3340 Instead we set the flag that will be recognized in make_decl_rtl. */
3341 DECL_IN_CONSTANT_POOL (decl) = 1;
3342 DECL_INITIAL (decl) = desc->value;
3343 /* ??? targetm.constant_alignment hasn't been updated for vector types on
3344 most architectures so use DATA_ALIGNMENT as well, except for strings. */
3345 if (TREE_CODE (exp) == STRING_CST)
3346 SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3347 else
3348 align_variable (decl, 0);
3350 /* Now construct the SYMBOL_REF and the MEM. */
3351 if (use_object_blocks_p ())
3353 int align = (TREE_CODE (decl) == CONST_DECL
3354 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3355 ? DECL_ALIGN (decl)
3356 : symtab_node::get (decl)->definition_alignment ());
3357 section *sect = get_constant_section (exp, align);
3358 symbol = create_block_symbol (ggc_strdup (label),
3359 get_block_for_section (sect), -1);
3361 else
3362 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3363 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3364 SET_SYMBOL_REF_DECL (symbol, decl);
3365 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3367 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3368 set_mem_attributes (rtl, exp, 1);
3369 set_mem_alias_set (rtl, 0);
3371 /* Putting EXP into the literal pool might have imposed a different
3372 alignment which should be visible in the RTX as well. */
3373 set_mem_align (rtl, DECL_ALIGN (decl));
3375 /* We cannot share RTX'es in pool entries.
3376 Mark this piece of RTL as required for unsharing. */
3377 RTX_FLAG (rtl, used) = 1;
3379 /* Set flags or add text to the name to record information, such as
3380 that it is a local symbol. If the name is changed, the macro
3381 ASM_OUTPUT_LABELREF will have to know how to strip this
3382 information. This call might invalidate our local variable
3383 SYMBOL; we can't use it afterward. */
3384 targetm.encode_section_info (exp, rtl, true);
3386 desc->rtl = rtl;
3388 return desc;
3391 /* Return an rtx representing a reference to constant data in memory
3392 for the constant expression EXP.
3394 If assembler code for such a constant has already been output,
3395 return an rtx to refer to it.
3396 Otherwise, output such a constant in memory
3397 and generate an rtx for it.
3399 If DEFER is nonzero, this constant can be deferred and output only
3400 if referenced in the function after all optimizations.
3402 `const_desc_table' records which constants already have label strings. */
3405 output_constant_def (tree exp, int defer)
3407 struct constant_descriptor_tree *desc;
3408 struct constant_descriptor_tree key;
3410 /* Look up EXP in the table of constant descriptors. If we didn't find
3411 it, create a new one. */
3412 key.value = exp;
3413 key.hash = const_hash_1 (exp);
3414 constant_descriptor_tree **loc
3415 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3417 desc = *loc;
3418 if (desc == 0)
3420 desc = build_constant_desc (exp);
3421 desc->hash = key.hash;
3422 *loc = desc;
3425 maybe_output_constant_def_contents (desc, defer);
3426 return desc->rtl;
3429 /* Subroutine of output_constant_def: Decide whether or not we need to
3430 output the constant DESC now, and if so, do it. */
3431 static void
3432 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3433 int defer)
3435 rtx symbol = XEXP (desc->rtl, 0);
3436 tree exp = desc->value;
3438 if (flag_syntax_only)
3439 return;
3441 if (TREE_ASM_WRITTEN (exp))
3442 /* Already output; don't do it again. */
3443 return;
3445 /* We can always defer constants as long as the context allows
3446 doing so. */
3447 if (defer)
3449 /* Increment n_deferred_constants if it exists. It needs to be at
3450 least as large as the number of constants actually referred to
3451 by the function. If it's too small we'll stop looking too early
3452 and fail to emit constants; if it's too large we'll only look
3453 through the entire function when we could have stopped earlier. */
3454 if (cfun)
3455 n_deferred_constants++;
3456 return;
3459 output_constant_def_contents (symbol);
3462 /* Subroutine of output_constant_def_contents. Output the definition
3463 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3464 constant's alignment in bits. */
3466 static void
3467 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3469 HOST_WIDE_INT size;
3471 size = get_constant_size (exp);
3473 /* Do any machine/system dependent processing of the constant. */
3474 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3476 /* Output the value of EXP. */
3477 output_constant (exp, size, align, false);
3479 targetm.asm_out.decl_end ();
3482 /* We must output the constant data referred to by SYMBOL; do so. */
3484 static void
3485 output_constant_def_contents (rtx symbol)
3487 tree decl = SYMBOL_REF_DECL (symbol);
3488 tree exp = DECL_INITIAL (decl);
3489 bool asan_protected = false;
3491 /* Make sure any other constants whose addresses appear in EXP
3492 are assigned label numbers. */
3493 output_addressed_constants (exp);
3495 /* We are no longer deferring this constant. */
3496 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3498 if ((flag_sanitize & SANITIZE_ADDRESS)
3499 && TREE_CODE (exp) == STRING_CST
3500 && asan_protect_global (exp))
3502 asan_protected = true;
3503 SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3504 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3507 /* If the constant is part of an object block, make sure that the
3508 decl has been positioned within its block, but do not write out
3509 its definition yet. output_object_blocks will do that later. */
3510 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3511 place_block_symbol (symbol);
3512 else
3514 int align = (TREE_CODE (decl) == CONST_DECL
3515 || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3516 ? DECL_ALIGN (decl)
3517 : symtab_node::get (decl)->definition_alignment ());
3518 switch_to_section (get_constant_section (exp, align));
3519 if (align > BITS_PER_UNIT)
3520 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3521 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3522 if (asan_protected)
3524 HOST_WIDE_INT size = get_constant_size (exp);
3525 assemble_zeros (asan_red_zone_size (size));
3530 /* Look up EXP in the table of constant descriptors. Return the rtl
3531 if it has been emitted, else null. */
3534 lookup_constant_def (tree exp)
3536 struct constant_descriptor_tree key;
3538 key.value = exp;
3539 key.hash = const_hash_1 (exp);
3540 constant_descriptor_tree *desc
3541 = const_desc_htab->find_with_hash (&key, key.hash);
3543 return (desc ? desc->rtl : NULL_RTX);
3546 /* Return a tree representing a reference to constant data in memory
3547 for the constant expression EXP.
3549 This is the counterpart of output_constant_def at the Tree level. */
3551 tree
3552 tree_output_constant_def (tree exp)
3554 struct constant_descriptor_tree *desc, key;
3555 tree decl;
3557 /* Look up EXP in the table of constant descriptors. If we didn't find
3558 it, create a new one. */
3559 key.value = exp;
3560 key.hash = const_hash_1 (exp);
3561 constant_descriptor_tree **loc
3562 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3564 desc = *loc;
3565 if (desc == 0)
3567 desc = build_constant_desc (exp);
3568 desc->hash = key.hash;
3569 *loc = desc;
3572 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3573 varpool_node::finalize_decl (decl);
3574 return decl;
3577 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3578 struct constant_descriptor_rtx *next;
3579 rtx mem;
3580 rtx sym;
3581 rtx constant;
3582 HOST_WIDE_INT offset;
3583 hashval_t hash;
3584 fixed_size_mode mode;
3585 unsigned int align;
3586 int labelno;
3587 int mark;
3590 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3592 static hashval_t hash (constant_descriptor_rtx *);
3593 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3596 /* Used in the hash tables to avoid outputting the same constant
3597 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3598 are output once per function, not once per file. */
3599 /* ??? Only a few targets need per-function constant pools. Most
3600 can use one per-file pool. Should add a targetm bit to tell the
3601 difference. */
3603 struct GTY(()) rtx_constant_pool {
3604 /* Pointers to first and last constant in pool, as ordered by offset. */
3605 struct constant_descriptor_rtx *first;
3606 struct constant_descriptor_rtx *last;
3608 /* Hash facility for making memory-constants from constant rtl-expressions.
3609 It is used on RISC machines where immediate integer arguments and
3610 constant addresses are restricted so that such constants must be stored
3611 in memory. */
3612 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3614 /* Current offset in constant pool (does not include any
3615 machine-specific header). */
3616 HOST_WIDE_INT offset;
3619 /* Hash and compare functions for const_rtx_htab. */
3621 hashval_t
3622 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3624 return desc->hash;
3627 bool
3628 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3629 constant_descriptor_rtx *y)
3631 if (x->mode != y->mode)
3632 return 0;
3633 return rtx_equal_p (x->constant, y->constant);
3636 /* Hash one component of a constant. */
3638 static hashval_t
3639 const_rtx_hash_1 (const_rtx x)
3641 unsigned HOST_WIDE_INT hwi;
3642 machine_mode mode;
3643 enum rtx_code code;
3644 hashval_t h;
3645 int i;
3647 code = GET_CODE (x);
3648 mode = GET_MODE (x);
3649 h = (hashval_t) code * 1048573 + mode;
3651 switch (code)
3653 case CONST_INT:
3654 hwi = INTVAL (x);
3656 fold_hwi:
3658 int shift = sizeof (hashval_t) * CHAR_BIT;
3659 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3661 h ^= (hashval_t) hwi;
3662 for (i = 1; i < n; ++i)
3664 hwi >>= shift;
3665 h ^= (hashval_t) hwi;
3668 break;
3670 case CONST_WIDE_INT:
3671 hwi = 0;
3673 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3674 hwi ^= CONST_WIDE_INT_ELT (x, i);
3675 goto fold_hwi;
3678 case CONST_DOUBLE:
3679 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3681 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3682 goto fold_hwi;
3684 else
3685 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3686 break;
3688 case CONST_FIXED:
3689 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3690 break;
3692 case SYMBOL_REF:
3693 h ^= htab_hash_string (XSTR (x, 0));
3694 break;
3696 case LABEL_REF:
3697 h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3698 break;
3700 case UNSPEC:
3701 case UNSPEC_VOLATILE:
3702 h = h * 251 + XINT (x, 1);
3703 break;
3705 default:
3706 break;
3709 return h;
3712 /* Compute a hash value for X, which should be a constant. */
3714 static hashval_t
3715 const_rtx_hash (rtx x)
3717 hashval_t h = 0;
3718 subrtx_iterator::array_type array;
3719 FOR_EACH_SUBRTX (iter, array, x, ALL)
3720 h = h * 509 + const_rtx_hash_1 (*iter);
3721 return h;
3725 /* Create and return a new rtx constant pool. */
3727 static struct rtx_constant_pool *
3728 create_constant_pool (void)
3730 struct rtx_constant_pool *pool;
3732 pool = ggc_alloc<rtx_constant_pool> ();
3733 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3734 pool->first = NULL;
3735 pool->last = NULL;
3736 pool->offset = 0;
3737 return pool;
3740 /* Initialize constant pool hashing for a new function. */
3742 void
3743 init_varasm_status (void)
3745 crtl->varasm.pool = create_constant_pool ();
3746 crtl->varasm.deferred_constants = 0;
3749 /* Given a MINUS expression, simplify it if both sides
3750 include the same symbol. */
3753 simplify_subtraction (rtx x)
3755 rtx r = simplify_rtx (x);
3756 return r ? r : x;
3759 /* Given a constant rtx X, make (or find) a memory constant for its value
3760 and return a MEM rtx to refer to it in memory. IN_MODE is the mode
3761 of X. */
3764 force_const_mem (machine_mode in_mode, rtx x)
3766 struct constant_descriptor_rtx *desc, tmp;
3767 struct rtx_constant_pool *pool;
3768 char label[256];
3769 rtx def, symbol;
3770 hashval_t hash;
3771 unsigned int align;
3772 constant_descriptor_rtx **slot;
3773 fixed_size_mode mode;
3775 /* We can't force variable-sized objects to memory. */
3776 if (!is_a <fixed_size_mode> (in_mode, &mode))
3777 return NULL_RTX;
3779 /* If we're not allowed to drop X into the constant pool, don't. */
3780 if (targetm.cannot_force_const_mem (mode, x))
3781 return NULL_RTX;
3783 /* Record that this function has used a constant pool entry. */
3784 crtl->uses_const_pool = 1;
3786 /* Decide which pool to use. */
3787 pool = (targetm.use_blocks_for_constant_p (mode, x)
3788 ? shared_constant_pool
3789 : crtl->varasm.pool);
3791 /* Lookup the value in the hashtable. */
3792 tmp.constant = x;
3793 tmp.mode = mode;
3794 hash = const_rtx_hash (x);
3795 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3796 desc = *slot;
3798 /* If the constant was already present, return its memory. */
3799 if (desc)
3800 return copy_rtx (desc->mem);
3802 /* Otherwise, create a new descriptor. */
3803 desc = ggc_alloc<constant_descriptor_rtx> ();
3804 *slot = desc;
3806 /* Align the location counter as required by EXP's data type. */
3807 machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3808 align = targetm.static_rtx_alignment (align_mode);
3810 pool->offset += (align / BITS_PER_UNIT) - 1;
3811 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3813 desc->next = NULL;
3814 desc->constant = copy_rtx (tmp.constant);
3815 desc->offset = pool->offset;
3816 desc->hash = hash;
3817 desc->mode = mode;
3818 desc->align = align;
3819 desc->labelno = const_labelno;
3820 desc->mark = 0;
3822 pool->offset += GET_MODE_SIZE (mode);
3823 if (pool->last)
3824 pool->last->next = desc;
3825 else
3826 pool->first = pool->last = desc;
3827 pool->last = desc;
3829 /* Create a string containing the label name, in LABEL. */
3830 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3831 ++const_labelno;
3833 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3834 the constants pool. */
3835 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3837 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3838 symbol = create_block_symbol (ggc_strdup (label),
3839 get_block_for_section (sect), -1);
3841 else
3842 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3843 desc->sym = symbol;
3844 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3845 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3846 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3848 /* Construct the MEM. */
3849 desc->mem = def = gen_const_mem (mode, symbol);
3850 set_mem_align (def, align);
3852 /* If we're dropping a label to the constant pool, make sure we
3853 don't delete it. */
3854 if (GET_CODE (x) == LABEL_REF)
3855 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3857 return copy_rtx (def);
3860 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3863 get_pool_constant (const_rtx addr)
3865 return SYMBOL_REF_CONSTANT (addr)->constant;
3868 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3869 and whether it has been output or not. */
3872 get_pool_constant_mark (rtx addr, bool *pmarked)
3874 struct constant_descriptor_rtx *desc;
3876 desc = SYMBOL_REF_CONSTANT (addr);
3877 *pmarked = (desc->mark != 0);
3878 return desc->constant;
3881 /* Similar, return the mode. */
3883 fixed_size_mode
3884 get_pool_mode (const_rtx addr)
3886 return SYMBOL_REF_CONSTANT (addr)->mode;
3889 /* Return TRUE if and only if the constant pool has no entries. Note
3890 that even entries we might end up choosing not to emit are counted
3891 here, so there is the potential for missed optimizations. */
3893 bool
3894 constant_pool_empty_p (void)
3896 return crtl->varasm.pool->first == NULL;
3899 /* Worker function for output_constant_pool_1. Emit assembly for X
3900 in MODE with known alignment ALIGN. */
3902 static void
3903 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
3905 switch (GET_MODE_CLASS (mode))
3907 case MODE_FLOAT:
3908 case MODE_DECIMAL_FLOAT:
3910 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3911 assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
3912 as_a <scalar_float_mode> (mode), align, false);
3913 break;
3916 case MODE_INT:
3917 case MODE_PARTIAL_INT:
3918 case MODE_FRACT:
3919 case MODE_UFRACT:
3920 case MODE_ACCUM:
3921 case MODE_UACCUM:
3922 case MODE_POINTER_BOUNDS:
3923 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3924 break;
3926 case MODE_VECTOR_BOOL:
3928 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3930 /* Pick the smallest integer mode that contains at least one
3931 whole element. Often this is byte_mode and contains more
3932 than one element. */
3933 unsigned int nelts = GET_MODE_NUNITS (mode);
3934 unsigned int elt_bits = GET_MODE_BITSIZE (mode) / nelts;
3935 unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
3936 scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
3938 /* Build the constant up one integer at a time. */
3939 unsigned int elts_per_int = int_bits / elt_bits;
3940 for (unsigned int i = 0; i < nelts; i += elts_per_int)
3942 unsigned HOST_WIDE_INT value = 0;
3943 unsigned int limit = MIN (nelts - i, elts_per_int);
3944 for (unsigned int j = 0; j < limit; ++j)
3945 if (INTVAL (CONST_VECTOR_ELT (x, i + j)) != 0)
3946 value |= 1 << (j * elt_bits);
3947 output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
3948 i != 0 ? MIN (align, int_bits) : align);
3950 break;
3952 case MODE_VECTOR_FLOAT:
3953 case MODE_VECTOR_INT:
3954 case MODE_VECTOR_FRACT:
3955 case MODE_VECTOR_UFRACT:
3956 case MODE_VECTOR_ACCUM:
3957 case MODE_VECTOR_UACCUM:
3959 int i, units;
3960 scalar_mode submode = GET_MODE_INNER (mode);
3961 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3963 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3964 units = GET_MODE_NUNITS (mode);
3966 for (i = 0; i < units; i++)
3968 rtx elt = CONST_VECTOR_ELT (x, i);
3969 output_constant_pool_2 (submode, elt, i ? subalign : align);
3972 break;
3974 default:
3975 gcc_unreachable ();
3979 /* Worker function for output_constant_pool. Emit constant DESC,
3980 giving it ALIGN bits of alignment. */
3982 static void
3983 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3984 unsigned int align)
3986 rtx x, tmp;
3988 x = desc->constant;
3990 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3991 whose CODE_LABEL has been deleted. This can occur if a jump table
3992 is eliminated by optimization. If so, write a constant of zero
3993 instead. Note that this can also happen by turning the
3994 CODE_LABEL into a NOTE. */
3995 /* ??? This seems completely and utterly wrong. Certainly it's
3996 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3997 functioning even with rtx_insn::deleted and friends. */
3999 tmp = x;
4000 switch (GET_CODE (tmp))
4002 case CONST:
4003 if (GET_CODE (XEXP (tmp, 0)) != PLUS
4004 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4005 break;
4006 tmp = XEXP (XEXP (tmp, 0), 0);
4007 /* FALLTHRU */
4009 case LABEL_REF:
4011 rtx_insn *insn = label_ref_label (tmp);
4012 gcc_assert (!insn->deleted ());
4013 gcc_assert (!NOTE_P (insn)
4014 || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4015 break;
4018 default:
4019 break;
4022 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4023 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4024 align, desc->labelno, done);
4025 #endif
4027 assemble_align (align);
4029 /* Output the label. */
4030 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4032 /* Output the data.
4033 Pass actual alignment value while emitting string constant to asm code
4034 as function 'output_constant_pool_1' explicitly passes the alignment as 1
4035 assuming that the data is already aligned which prevents the generation
4036 of fix-up table entries. */
4037 output_constant_pool_2 (desc->mode, x, desc->align);
4039 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4040 sections have proper size. */
4041 if (align > GET_MODE_BITSIZE (desc->mode)
4042 && in_section
4043 && (in_section->common.flags & SECTION_MERGE))
4044 assemble_align (align);
4046 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4047 done:
4048 #endif
4049 return;
4052 /* Recompute the offsets of entries in POOL, and the overall size of
4053 POOL. Do this after calling mark_constant_pool to ensure that we
4054 are computing the offset values for the pool which we will actually
4055 emit. */
4057 static void
4058 recompute_pool_offsets (struct rtx_constant_pool *pool)
4060 struct constant_descriptor_rtx *desc;
4061 pool->offset = 0;
4063 for (desc = pool->first; desc ; desc = desc->next)
4064 if (desc->mark)
4066 /* Recalculate offset. */
4067 unsigned int align = desc->align;
4068 pool->offset += (align / BITS_PER_UNIT) - 1;
4069 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4070 desc->offset = pool->offset;
4071 pool->offset += GET_MODE_SIZE (desc->mode);
4075 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4076 Emit referenced deferred strings. */
4078 static void
4079 mark_constants_in_pattern (rtx insn)
4081 subrtx_iterator::array_type array;
4082 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4084 const_rtx x = *iter;
4085 if (GET_CODE (x) == SYMBOL_REF)
4087 if (CONSTANT_POOL_ADDRESS_P (x))
4089 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4090 if (desc->mark == 0)
4092 desc->mark = 1;
4093 iter.substitute (desc->constant);
4096 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4098 tree decl = SYMBOL_REF_DECL (x);
4099 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4101 n_deferred_constants--;
4102 output_constant_def_contents (CONST_CAST_RTX (x));
4109 /* Look through appropriate parts of INSN, marking all entries in the
4110 constant pool which are actually being used. Entries that are only
4111 referenced by other constants are also marked as used. Emit
4112 deferred strings that are used. */
4114 static void
4115 mark_constants (rtx_insn *insn)
4117 if (!INSN_P (insn))
4118 return;
4120 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4121 insns, not any notes that may be attached. We don't want to mark
4122 a constant just because it happens to appear in a REG_EQUIV note. */
4123 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4125 int i, n = seq->len ();
4126 for (i = 0; i < n; ++i)
4128 rtx subinsn = seq->element (i);
4129 if (INSN_P (subinsn))
4130 mark_constants_in_pattern (subinsn);
4133 else
4134 mark_constants_in_pattern (insn);
4137 /* Look through the instructions for this function, and mark all the
4138 entries in POOL which are actually being used. Emit deferred constants
4139 which have indeed been used. */
4141 static void
4142 mark_constant_pool (void)
4144 rtx_insn *insn;
4146 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4147 return;
4149 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4150 mark_constants (insn);
4153 /* Write all the constants in POOL. */
4155 static void
4156 output_constant_pool_contents (struct rtx_constant_pool *pool)
4158 struct constant_descriptor_rtx *desc;
4160 for (desc = pool->first; desc ; desc = desc->next)
4161 if (desc->mark)
4163 /* If the constant is part of an object_block, make sure that
4164 the constant has been positioned within its block, but do not
4165 write out its definition yet. output_object_blocks will do
4166 that later. */
4167 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4168 && SYMBOL_REF_BLOCK (desc->sym))
4169 place_block_symbol (desc->sym);
4170 else
4172 switch_to_section (targetm.asm_out.select_rtx_section
4173 (desc->mode, desc->constant, desc->align));
4174 output_constant_pool_1 (desc, desc->align);
4179 /* Mark all constants that are used in the current function, then write
4180 out the function's private constant pool. */
4182 static void
4183 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4184 tree fndecl ATTRIBUTE_UNUSED)
4186 struct rtx_constant_pool *pool = crtl->varasm.pool;
4188 /* It is possible for gcc to call force_const_mem and then to later
4189 discard the instructions which refer to the constant. In such a
4190 case we do not need to output the constant. */
4191 mark_constant_pool ();
4193 /* Having marked the constant pool entries we'll actually emit, we
4194 now need to rebuild the offset information, which may have become
4195 stale. */
4196 recompute_pool_offsets (pool);
4198 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4199 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4200 #endif
4202 output_constant_pool_contents (pool);
4204 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4205 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4206 #endif
4209 /* Write the contents of the shared constant pool. */
4211 void
4212 output_shared_constant_pool (void)
4214 output_constant_pool_contents (shared_constant_pool);
4217 /* Determine what kind of relocations EXP may need. */
4220 compute_reloc_for_constant (tree exp)
4222 int reloc = 0, reloc2;
4223 tree tem;
4225 switch (TREE_CODE (exp))
4227 case ADDR_EXPR:
4228 case FDESC_EXPR:
4229 /* Go inside any operations that get_inner_reference can handle and see
4230 if what's inside is a constant: no need to do anything here for
4231 addresses of variables or functions. */
4232 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4233 tem = TREE_OPERAND (tem, 0))
4236 if (TREE_CODE (tem) == MEM_REF
4237 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4239 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4240 break;
4243 if (!targetm.binds_local_p (tem))
4244 reloc |= 2;
4245 else
4246 reloc |= 1;
4247 break;
4249 case PLUS_EXPR:
4250 case POINTER_PLUS_EXPR:
4251 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4252 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4253 break;
4255 case MINUS_EXPR:
4256 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4257 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4258 /* The difference of two local labels is computable at link time. */
4259 if (reloc == 1 && reloc2 == 1)
4260 reloc = 0;
4261 else
4262 reloc |= reloc2;
4263 break;
4265 CASE_CONVERT:
4266 case VIEW_CONVERT_EXPR:
4267 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4268 break;
4270 case CONSTRUCTOR:
4272 unsigned HOST_WIDE_INT idx;
4273 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4274 if (tem != 0)
4275 reloc |= compute_reloc_for_constant (tem);
4277 break;
4279 default:
4280 break;
4282 return reloc;
4285 /* Find all the constants whose addresses are referenced inside of EXP,
4286 and make sure assembler code with a label has been output for each one.
4287 Indicate whether an ADDR_EXPR has been encountered. */
4289 static void
4290 output_addressed_constants (tree exp)
4292 tree tem;
4294 switch (TREE_CODE (exp))
4296 case ADDR_EXPR:
4297 case FDESC_EXPR:
4298 /* Go inside any operations that get_inner_reference can handle and see
4299 if what's inside is a constant: no need to do anything here for
4300 addresses of variables or functions. */
4301 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4302 tem = TREE_OPERAND (tem, 0))
4305 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4306 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4307 tem = DECL_INITIAL (tem);
4309 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4310 output_constant_def (tem, 0);
4312 if (TREE_CODE (tem) == MEM_REF)
4313 output_addressed_constants (TREE_OPERAND (tem, 0));
4314 break;
4316 case PLUS_EXPR:
4317 case POINTER_PLUS_EXPR:
4318 case MINUS_EXPR:
4319 output_addressed_constants (TREE_OPERAND (exp, 1));
4320 gcc_fallthrough ();
4322 CASE_CONVERT:
4323 case VIEW_CONVERT_EXPR:
4324 output_addressed_constants (TREE_OPERAND (exp, 0));
4325 break;
4327 case CONSTRUCTOR:
4329 unsigned HOST_WIDE_INT idx;
4330 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4331 if (tem != 0)
4332 output_addressed_constants (tem);
4334 break;
4336 default:
4337 break;
4341 /* Whether a constructor CTOR is a valid static constant initializer if all
4342 its elements are. This used to be internal to initializer_constant_valid_p
4343 and has been exposed to let other functions like categorize_ctor_elements
4344 evaluate the property while walking a constructor for other purposes. */
4346 bool
4347 constructor_static_from_elts_p (const_tree ctor)
4349 return (TREE_CONSTANT (ctor)
4350 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4351 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4352 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4355 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4356 tree *cache);
4358 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4359 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4360 which are valid when ENDTYPE is an integer of any size; in
4361 particular, this does not accept a pointer minus a constant. This
4362 returns null_pointer_node if the VALUE is an absolute constant
4363 which can be used to initialize a static variable. Otherwise it
4364 returns NULL. */
4366 static tree
4367 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4369 tree op0, op1;
4371 if (!INTEGRAL_TYPE_P (endtype))
4372 return NULL_TREE;
4374 op0 = TREE_OPERAND (value, 0);
4375 op1 = TREE_OPERAND (value, 1);
4377 /* Like STRIP_NOPS except allow the operand mode to widen. This
4378 works around a feature of fold that simplifies (int)(p1 - p2) to
4379 ((int)p1 - (int)p2) under the theory that the narrower operation
4380 is cheaper. */
4382 while (CONVERT_EXPR_P (op0)
4383 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4385 tree inner = TREE_OPERAND (op0, 0);
4386 if (inner == error_mark_node
4387 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4388 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4389 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4390 break;
4391 op0 = inner;
4394 while (CONVERT_EXPR_P (op1)
4395 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4397 tree inner = TREE_OPERAND (op1, 0);
4398 if (inner == error_mark_node
4399 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4400 || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4401 > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4402 break;
4403 op1 = inner;
4406 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4407 if (!op0)
4408 return NULL_TREE;
4410 op1 = initializer_constant_valid_p_1 (op1, endtype,
4411 cache ? cache + 2 : NULL);
4412 /* Both initializers must be known. */
4413 if (op1)
4415 if (op0 == op1
4416 && (op0 == null_pointer_node
4417 || TREE_CODE (value) == MINUS_EXPR))
4418 return null_pointer_node;
4420 /* Support differences between labels. */
4421 if (TREE_CODE (op0) == LABEL_DECL
4422 && TREE_CODE (op1) == LABEL_DECL)
4423 return null_pointer_node;
4425 if (TREE_CODE (op0) == STRING_CST
4426 && TREE_CODE (op1) == STRING_CST
4427 && operand_equal_p (op0, op1, 1))
4428 return null_pointer_node;
4431 return NULL_TREE;
4434 /* Helper function of initializer_constant_valid_p.
4435 Return nonzero if VALUE is a valid constant-valued expression
4436 for use in initializing a static variable; one that can be an
4437 element of a "constant" initializer.
4439 Return null_pointer_node if the value is absolute;
4440 if it is relocatable, return the variable that determines the relocation.
4441 We assume that VALUE has been folded as much as possible;
4442 therefore, we do not need to check for such things as
4443 arithmetic-combinations of integers.
4445 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4447 static tree
4448 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4450 tree ret;
4452 switch (TREE_CODE (value))
4454 case CONSTRUCTOR:
4455 if (constructor_static_from_elts_p (value))
4457 unsigned HOST_WIDE_INT idx;
4458 tree elt;
4459 bool absolute = true;
4461 if (cache && cache[0] == value)
4462 return cache[1];
4463 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4465 tree reloc;
4466 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4467 NULL);
4468 if (!reloc
4469 /* An absolute value is required with reverse SSO. */
4470 || (reloc != null_pointer_node
4471 && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4472 && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4474 if (cache)
4476 cache[0] = value;
4477 cache[1] = NULL_TREE;
4479 return NULL_TREE;
4481 if (reloc != null_pointer_node)
4482 absolute = false;
4484 /* For a non-absolute relocation, there is no single
4485 variable that can be "the variable that determines the
4486 relocation." */
4487 if (cache)
4489 cache[0] = value;
4490 cache[1] = absolute ? null_pointer_node : error_mark_node;
4492 return absolute ? null_pointer_node : error_mark_node;
4495 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4497 case INTEGER_CST:
4498 case VECTOR_CST:
4499 case REAL_CST:
4500 case FIXED_CST:
4501 case STRING_CST:
4502 case COMPLEX_CST:
4503 return null_pointer_node;
4505 case ADDR_EXPR:
4506 case FDESC_EXPR:
4508 tree op0 = staticp (TREE_OPERAND (value, 0));
4509 if (op0)
4511 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4512 to be a constant, this is old-skool offsetof-like nonsense. */
4513 if (TREE_CODE (op0) == INDIRECT_REF
4514 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4515 return null_pointer_node;
4516 /* Taking the address of a nested function involves a trampoline,
4517 unless we don't need or want one. */
4518 if (TREE_CODE (op0) == FUNCTION_DECL
4519 && DECL_STATIC_CHAIN (op0)
4520 && !TREE_NO_TRAMPOLINE (value))
4521 return NULL_TREE;
4522 /* "&{...}" requires a temporary to hold the constructed
4523 object. */
4524 if (TREE_CODE (op0) == CONSTRUCTOR)
4525 return NULL_TREE;
4527 return op0;
4530 case NON_LVALUE_EXPR:
4531 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4532 endtype, cache);
4534 case VIEW_CONVERT_EXPR:
4536 tree src = TREE_OPERAND (value, 0);
4537 tree src_type = TREE_TYPE (src);
4538 tree dest_type = TREE_TYPE (value);
4540 /* Allow view-conversions from aggregate to non-aggregate type only
4541 if the bit pattern is fully preserved afterwards; otherwise, the
4542 RTL expander won't be able to apply a subsequent transformation
4543 to the underlying constructor. */
4544 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4546 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4547 return initializer_constant_valid_p_1 (src, endtype, cache);
4548 else
4549 return NULL_TREE;
4552 /* Allow all other kinds of view-conversion. */
4553 return initializer_constant_valid_p_1 (src, endtype, cache);
4556 CASE_CONVERT:
4558 tree src = TREE_OPERAND (value, 0);
4559 tree src_type = TREE_TYPE (src);
4560 tree dest_type = TREE_TYPE (value);
4562 /* Allow conversions between pointer types, floating-point
4563 types, and offset types. */
4564 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4565 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4566 || (TREE_CODE (dest_type) == OFFSET_TYPE
4567 && TREE_CODE (src_type) == OFFSET_TYPE))
4568 return initializer_constant_valid_p_1 (src, endtype, cache);
4570 /* Allow length-preserving conversions between integer types. */
4571 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4572 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4573 return initializer_constant_valid_p_1 (src, endtype, cache);
4575 /* Allow conversions between other integer types only if
4576 explicit value. Don't allow sign-extension to a type larger
4577 than word and pointer, there aren't relocations that would
4578 allow to sign extend it to a wider type. */
4579 if (INTEGRAL_TYPE_P (dest_type)
4580 && INTEGRAL_TYPE_P (src_type)
4581 && (TYPE_UNSIGNED (src_type)
4582 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4583 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4584 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4586 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4587 if (inner == null_pointer_node)
4588 return null_pointer_node;
4589 break;
4592 /* Allow (int) &foo provided int is as wide as a pointer. */
4593 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4594 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4595 return initializer_constant_valid_p_1 (src, endtype, cache);
4597 /* Likewise conversions from int to pointers, but also allow
4598 conversions from 0. */
4599 if ((POINTER_TYPE_P (dest_type)
4600 || TREE_CODE (dest_type) == OFFSET_TYPE)
4601 && INTEGRAL_TYPE_P (src_type))
4603 if (TREE_CODE (src) == INTEGER_CST
4604 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4605 return null_pointer_node;
4606 if (integer_zerop (src))
4607 return null_pointer_node;
4608 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4609 return initializer_constant_valid_p_1 (src, endtype, cache);
4612 /* Allow conversions to struct or union types if the value
4613 inside is okay. */
4614 if (TREE_CODE (dest_type) == RECORD_TYPE
4615 || TREE_CODE (dest_type) == UNION_TYPE)
4616 return initializer_constant_valid_p_1 (src, endtype, cache);
4618 break;
4620 case POINTER_PLUS_EXPR:
4621 case PLUS_EXPR:
4622 /* Any valid floating-point constants will have been folded by now;
4623 with -frounding-math we hit this with addition of two constants. */
4624 if (TREE_CODE (endtype) == REAL_TYPE)
4625 return NULL_TREE;
4626 if (cache && cache[0] == value)
4627 return cache[1];
4628 if (! INTEGRAL_TYPE_P (endtype)
4629 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4631 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4632 tree valid0
4633 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4634 endtype, ncache);
4635 tree valid1
4636 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4637 endtype, ncache + 2);
4638 /* If either term is absolute, use the other term's relocation. */
4639 if (valid0 == null_pointer_node)
4640 ret = valid1;
4641 else if (valid1 == null_pointer_node)
4642 ret = valid0;
4643 /* Support narrowing pointer differences. */
4644 else
4645 ret = narrowing_initializer_constant_valid_p (value, endtype,
4646 ncache);
4648 else
4649 /* Support narrowing pointer differences. */
4650 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4651 if (cache)
4653 cache[0] = value;
4654 cache[1] = ret;
4656 return ret;
4658 case POINTER_DIFF_EXPR:
4659 case MINUS_EXPR:
4660 if (TREE_CODE (endtype) == REAL_TYPE)
4661 return NULL_TREE;
4662 if (cache && cache[0] == value)
4663 return cache[1];
4664 if (! INTEGRAL_TYPE_P (endtype)
4665 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4667 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4668 tree valid0
4669 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4670 endtype, ncache);
4671 tree valid1
4672 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4673 endtype, ncache + 2);
4674 /* Win if second argument is absolute. */
4675 if (valid1 == null_pointer_node)
4676 ret = valid0;
4677 /* Win if both arguments have the same relocation.
4678 Then the value is absolute. */
4679 else if (valid0 == valid1 && valid0 != 0)
4680 ret = null_pointer_node;
4681 /* Since GCC guarantees that string constants are unique in the
4682 generated code, a subtraction between two copies of the same
4683 constant string is absolute. */
4684 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4685 && valid1 && TREE_CODE (valid1) == STRING_CST
4686 && operand_equal_p (valid0, valid1, 1))
4687 ret = null_pointer_node;
4688 /* Support narrowing differences. */
4689 else
4690 ret = narrowing_initializer_constant_valid_p (value, endtype,
4691 ncache);
4693 else
4694 /* Support narrowing differences. */
4695 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4696 if (cache)
4698 cache[0] = value;
4699 cache[1] = ret;
4701 return ret;
4703 default:
4704 break;
4707 return NULL_TREE;
4710 /* Return nonzero if VALUE is a valid constant-valued expression
4711 for use in initializing a static variable; one that can be an
4712 element of a "constant" initializer.
4714 Return null_pointer_node if the value is absolute;
4715 if it is relocatable, return the variable that determines the relocation.
4716 We assume that VALUE has been folded as much as possible;
4717 therefore, we do not need to check for such things as
4718 arithmetic-combinations of integers. */
4719 tree
4720 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
4722 tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
4724 /* An absolute value is required with reverse storage order. */
4725 if (reloc
4726 && reloc != null_pointer_node
4727 && reverse
4728 && !AGGREGATE_TYPE_P (endtype)
4729 && !VECTOR_TYPE_P (endtype))
4730 reloc = NULL_TREE;
4732 return reloc;
4735 /* Return true if VALUE is a valid constant-valued expression
4736 for use in initializing a static bit-field; one that can be
4737 an element of a "constant" initializer. */
4739 bool
4740 initializer_constant_valid_for_bitfield_p (tree value)
4742 /* For bitfields we support integer constants or possibly nested aggregates
4743 of such. */
4744 switch (TREE_CODE (value))
4746 case CONSTRUCTOR:
4748 unsigned HOST_WIDE_INT idx;
4749 tree elt;
4751 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4752 if (!initializer_constant_valid_for_bitfield_p (elt))
4753 return false;
4754 return true;
4757 case INTEGER_CST:
4758 case REAL_CST:
4759 return true;
4761 case VIEW_CONVERT_EXPR:
4762 case NON_LVALUE_EXPR:
4763 return
4764 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4766 default:
4767 break;
4770 return false;
4773 /* output_constructor outer state of relevance in recursive calls, typically
4774 for nested aggregate bitfields. */
4776 struct oc_outer_state {
4777 unsigned int bit_offset; /* current position in ... */
4778 int byte; /* ... the outer byte buffer. */
4781 static unsigned HOST_WIDE_INT
4782 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
4783 oc_outer_state *);
4785 /* Output assembler code for constant EXP, with no label.
4786 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4787 Assumes output_addressed_constants has been done on EXP already.
4789 Generate at least SIZE bytes of assembler data, padding at the end
4790 with zeros if necessary. SIZE must always be specified. The returned
4791 value is the actual number of bytes of assembler data generated, which
4792 may be bigger than SIZE if the object contains a variable length field.
4794 SIZE is important for structure constructors,
4795 since trailing members may have been omitted from the constructor.
4796 It is also important for initialization of arrays from string constants
4797 since the full length of the string constant might not be wanted.
4798 It is also needed for initialization of unions, where the initializer's
4799 type is just one member, and that may not be as long as the union.
4801 There a case in which we would fail to output exactly SIZE bytes:
4802 for a structure constructor that wants to produce more than SIZE bytes.
4803 But such constructors will never be generated for any possible input.
4805 ALIGN is the alignment of the data in bits.
4807 If REVERSE is true, EXP is output in reverse storage order. */
4809 static unsigned HOST_WIDE_INT
4810 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
4811 bool reverse)
4813 enum tree_code code;
4814 unsigned HOST_WIDE_INT thissize;
4815 rtx cst;
4817 if (size == 0 || flag_syntax_only)
4818 return size;
4820 /* See if we're trying to initialize a pointer in a non-default mode
4821 to the address of some declaration somewhere. If the target says
4822 the mode is valid for pointers, assume the target has a way of
4823 resolving it. */
4824 if (TREE_CODE (exp) == NOP_EXPR
4825 && POINTER_TYPE_P (TREE_TYPE (exp))
4826 && targetm.addr_space.valid_pointer_mode
4827 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4828 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4830 tree saved_type = TREE_TYPE (exp);
4832 /* Peel off any intermediate conversions-to-pointer for valid
4833 pointer modes. */
4834 while (TREE_CODE (exp) == NOP_EXPR
4835 && POINTER_TYPE_P (TREE_TYPE (exp))
4836 && targetm.addr_space.valid_pointer_mode
4837 (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4838 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4839 exp = TREE_OPERAND (exp, 0);
4841 /* If what we're left with is the address of something, we can
4842 convert the address to the final type and output it that
4843 way. */
4844 if (TREE_CODE (exp) == ADDR_EXPR)
4845 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4846 /* Likewise for constant ints. */
4847 else if (TREE_CODE (exp) == INTEGER_CST)
4848 exp = fold_convert (saved_type, exp);
4852 /* Eliminate any conversions since we'll be outputting the underlying
4853 constant. */
4854 while (CONVERT_EXPR_P (exp)
4855 || TREE_CODE (exp) == NON_LVALUE_EXPR
4856 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4858 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4859 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4861 /* Make sure eliminating the conversion is really a no-op, except with
4862 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4863 union types to allow for Ada unchecked unions. */
4864 if (type_size > op_size
4865 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4866 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4867 /* Keep the conversion. */
4868 break;
4869 else
4870 exp = TREE_OPERAND (exp, 0);
4873 code = TREE_CODE (TREE_TYPE (exp));
4874 thissize = int_size_in_bytes (TREE_TYPE (exp));
4876 /* Allow a constructor with no elements for any data type.
4877 This means to fill the space with zeros. */
4878 if (TREE_CODE (exp) == CONSTRUCTOR
4879 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4881 assemble_zeros (size);
4882 return size;
4885 if (TREE_CODE (exp) == FDESC_EXPR)
4887 #ifdef ASM_OUTPUT_FDESC
4888 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4889 tree decl = TREE_OPERAND (exp, 0);
4890 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4891 #else
4892 gcc_unreachable ();
4893 #endif
4894 return size;
4897 /* Now output the underlying data. If we've handling the padding, return.
4898 Otherwise, break and ensure SIZE is the size written. */
4899 switch (code)
4901 case BOOLEAN_TYPE:
4902 case INTEGER_TYPE:
4903 case ENUMERAL_TYPE:
4904 case POINTER_TYPE:
4905 case REFERENCE_TYPE:
4906 case OFFSET_TYPE:
4907 case FIXED_POINT_TYPE:
4908 case NULLPTR_TYPE:
4909 cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
4910 if (reverse)
4911 cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
4912 if (!assemble_integer (cst, MIN (size, thissize), align, 0))
4913 error ("initializer for integer/fixed-point value is too complicated");
4914 break;
4916 case REAL_TYPE:
4917 if (TREE_CODE (exp) != REAL_CST)
4918 error ("initializer for floating value is not a floating constant");
4919 else
4920 assemble_real (TREE_REAL_CST (exp),
4921 SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
4922 align, reverse);
4923 break;
4925 case COMPLEX_TYPE:
4926 output_constant (TREE_REALPART (exp), thissize / 2, align, reverse);
4927 output_constant (TREE_IMAGPART (exp), thissize / 2,
4928 min_align (align, BITS_PER_UNIT * (thissize / 2)),
4929 reverse);
4930 break;
4932 case ARRAY_TYPE:
4933 case VECTOR_TYPE:
4934 switch (TREE_CODE (exp))
4936 case CONSTRUCTOR:
4937 return output_constructor (exp, size, align, reverse, NULL);
4938 case STRING_CST:
4939 thissize
4940 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4941 assemble_string (TREE_STRING_POINTER (exp), thissize);
4942 break;
4943 case VECTOR_CST:
4945 scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4946 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4947 int elt_size = GET_MODE_SIZE (inner);
4948 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
4949 reverse);
4950 thissize = elt_size;
4951 /* Static constants must have a fixed size. */
4952 unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
4953 for (unsigned int i = 1; i < nunits; i++)
4955 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
4956 reverse);
4957 thissize += elt_size;
4959 break;
4961 default:
4962 gcc_unreachable ();
4964 break;
4966 case RECORD_TYPE:
4967 case UNION_TYPE:
4968 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4969 return output_constructor (exp, size, align, reverse, NULL);
4971 case ERROR_MARK:
4972 return 0;
4974 default:
4975 gcc_unreachable ();
4978 if (size > thissize)
4979 assemble_zeros (size - thissize);
4981 return size;
4984 /* Subroutine of output_constructor, used for computing the size of
4985 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4986 type with an unspecified upper bound. */
4988 static unsigned HOST_WIDE_INT
4989 array_size_for_constructor (tree val)
4991 tree max_index;
4992 unsigned HOST_WIDE_INT cnt;
4993 tree index, value, tmp;
4994 offset_int i;
4996 /* This code used to attempt to handle string constants that are not
4997 arrays of single-bytes, but nothing else does, so there's no point in
4998 doing it here. */
4999 if (TREE_CODE (val) == STRING_CST)
5000 return TREE_STRING_LENGTH (val);
5002 max_index = NULL_TREE;
5003 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5005 if (TREE_CODE (index) == RANGE_EXPR)
5006 index = TREE_OPERAND (index, 1);
5007 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5008 max_index = index;
5011 if (max_index == NULL_TREE)
5012 return 0;
5014 /* Compute the total number of array elements. */
5015 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5016 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5018 /* Multiply by the array element unit size to find number of bytes. */
5019 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5021 gcc_assert (wi::fits_uhwi_p (i));
5022 return i.to_uhwi ();
5025 /* Other datastructures + helpers for output_constructor. */
5027 /* output_constructor local state to support interaction with helpers. */
5029 struct oc_local_state {
5031 /* Received arguments. */
5032 tree exp; /* Constructor expression. */
5033 tree type; /* Type of constructor expression. */
5034 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
5035 unsigned int align; /* Known initial alignment. */
5036 tree min_index; /* Lower bound if specified for an array. */
5038 /* Output processing state. */
5039 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
5040 int byte; /* Part of a bitfield byte yet to be output. */
5041 int last_relative_index; /* Implicit or explicit index of the last
5042 array element output within a bitfield. */
5043 bool byte_buffer_in_use; /* Whether BYTE is in use. */
5044 bool reverse; /* Whether reverse storage order is in use. */
5046 /* Current element. */
5047 tree field; /* Current field decl in a record. */
5048 tree val; /* Current element value. */
5049 tree index; /* Current element index. */
5053 /* Helper for output_constructor. From the current LOCAL state, output a
5054 RANGE_EXPR element. */
5056 static void
5057 output_constructor_array_range (oc_local_state *local)
5059 unsigned HOST_WIDE_INT fieldsize
5060 = int_size_in_bytes (TREE_TYPE (local->type));
5062 HOST_WIDE_INT lo_index
5063 = tree_to_shwi (TREE_OPERAND (local->index, 0));
5064 HOST_WIDE_INT hi_index
5065 = tree_to_shwi (TREE_OPERAND (local->index, 1));
5066 HOST_WIDE_INT index;
5068 unsigned int align2
5069 = min_align (local->align, fieldsize * BITS_PER_UNIT);
5071 for (index = lo_index; index <= hi_index; index++)
5073 /* Output the element's initial value. */
5074 if (local->val == NULL_TREE)
5075 assemble_zeros (fieldsize);
5076 else
5077 fieldsize
5078 = output_constant (local->val, fieldsize, align2, local->reverse);
5080 /* Count its size. */
5081 local->total_bytes += fieldsize;
5085 /* Helper for output_constructor. From the current LOCAL state, output a
5086 field element that is not true bitfield or part of an outer one. */
5088 static void
5089 output_constructor_regular_field (oc_local_state *local)
5091 /* Field size and position. Since this structure is static, we know the
5092 positions are constant. */
5093 unsigned HOST_WIDE_INT fieldsize;
5094 HOST_WIDE_INT fieldpos;
5096 unsigned int align2;
5098 /* Output any buffered-up bit-fields preceding this element. */
5099 if (local->byte_buffer_in_use)
5101 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5102 local->total_bytes++;
5103 local->byte_buffer_in_use = false;
5106 if (local->index != NULL_TREE)
5108 /* Perform the index calculation in modulo arithmetic but
5109 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5110 but we are using an unsigned sizetype. */
5111 unsigned prec = TYPE_PRECISION (sizetype);
5112 offset_int idx = wi::sext (wi::to_offset (local->index)
5113 - wi::to_offset (local->min_index), prec);
5114 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5115 .to_short_addr ();
5117 else if (local->field != NULL_TREE)
5118 fieldpos = int_byte_position (local->field);
5119 else
5120 fieldpos = 0;
5122 /* Advance to offset of this element.
5123 Note no alignment needed in an array, since that is guaranteed
5124 if each element has the proper size. */
5125 if (local->field != NULL_TREE || local->index != NULL_TREE)
5127 if (fieldpos > local->total_bytes)
5129 assemble_zeros (fieldpos - local->total_bytes);
5130 local->total_bytes = fieldpos;
5132 else
5133 /* Must not go backwards. */
5134 gcc_assert (fieldpos == local->total_bytes);
5137 /* Find the alignment of this element. */
5138 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5140 /* Determine size this element should occupy. */
5141 if (local->field)
5143 fieldsize = 0;
5145 /* If this is an array with an unspecified upper bound,
5146 the initializer determines the size. */
5147 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5148 but we cannot do this until the deprecated support for
5149 initializing zero-length array members is removed. */
5150 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5151 && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5152 || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5154 fieldsize = array_size_for_constructor (local->val);
5155 /* Given a non-empty initialization, this field had better
5156 be last. Given a flexible array member, the next field
5157 on the chain is a TYPE_DECL of the enclosing struct. */
5158 const_tree next = DECL_CHAIN (local->field);
5159 gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5161 else
5162 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5164 else
5165 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5167 /* Output the element's initial value. */
5168 if (local->val == NULL_TREE)
5169 assemble_zeros (fieldsize);
5170 else
5171 fieldsize
5172 = output_constant (local->val, fieldsize, align2, local->reverse);
5174 /* Count its size. */
5175 local->total_bytes += fieldsize;
5178 /* Helper for output_constructor. From the LOCAL state, output an element
5179 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5180 from the start of a possibly ongoing outer byte buffer. */
5182 static void
5183 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5185 /* Bit size of this element. */
5186 HOST_WIDE_INT ebitsize
5187 = (local->field
5188 ? tree_to_uhwi (DECL_SIZE (local->field))
5189 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5191 /* Relative index of this element if this is an array component. */
5192 HOST_WIDE_INT relative_index
5193 = (!local->field
5194 ? (local->index
5195 ? (tree_to_shwi (local->index)
5196 - tree_to_shwi (local->min_index))
5197 : local->last_relative_index + 1)
5198 : 0);
5200 /* Bit position of this element from the start of the containing
5201 constructor. */
5202 HOST_WIDE_INT constructor_relative_ebitpos
5203 = (local->field
5204 ? int_bit_position (local->field)
5205 : ebitsize * relative_index);
5207 /* Bit position of this element from the start of a possibly ongoing
5208 outer byte buffer. */
5209 HOST_WIDE_INT byte_relative_ebitpos
5210 = bit_offset + constructor_relative_ebitpos;
5212 /* From the start of a possibly ongoing outer byte buffer, offsets to
5213 the first bit of this element and to the first bit past the end of
5214 this element. */
5215 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5216 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5218 local->last_relative_index = relative_index;
5220 if (local->val == NULL_TREE)
5221 local->val = integer_zero_node;
5223 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5224 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5225 local->val = TREE_OPERAND (local->val, 0);
5227 if (TREE_CODE (local->val) != INTEGER_CST
5228 && TREE_CODE (local->val) != CONSTRUCTOR)
5230 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5231 return;
5234 /* If this field does not start in this (or next) byte, skip some bytes. */
5235 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5237 /* Output remnant of any bit field in previous bytes. */
5238 if (local->byte_buffer_in_use)
5240 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5241 local->total_bytes++;
5242 local->byte_buffer_in_use = false;
5245 /* If still not at proper byte, advance to there. */
5246 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5248 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5249 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5250 local->total_bytes = next_offset / BITS_PER_UNIT;
5254 /* Set up the buffer if necessary. */
5255 if (!local->byte_buffer_in_use)
5257 local->byte = 0;
5258 if (ebitsize > 0)
5259 local->byte_buffer_in_use = true;
5262 /* If this is nested constructor, recurse passing the bit offset and the
5263 pending data, then retrieve the new pending data afterwards. */
5264 if (TREE_CODE (local->val) == CONSTRUCTOR)
5266 oc_outer_state temp_state;
5267 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5268 temp_state.byte = local->byte;
5269 local->total_bytes
5270 += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5271 local->byte = temp_state.byte;
5272 return;
5275 /* Otherwise, we must split the element into pieces that fall within
5276 separate bytes, and combine each byte with previous or following
5277 bit-fields. */
5278 while (next_offset < end_offset)
5280 int this_time;
5281 int shift;
5282 HOST_WIDE_INT value;
5283 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5284 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5286 /* Advance from byte to byte within this element when necessary. */
5287 while (next_byte != local->total_bytes)
5289 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5290 local->total_bytes++;
5291 local->byte = 0;
5294 /* Number of bits we can process at once (all part of the same byte). */
5295 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5296 if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5298 /* For big-endian data, take the most significant bits (of the
5299 bits that are significant) first and put them into bytes from
5300 the most significant end. */
5301 shift = end_offset - next_offset - this_time;
5303 /* Don't try to take a bunch of bits that cross
5304 the word boundary in the INTEGER_CST. We can
5305 only select bits from one element. */
5306 if ((shift / HOST_BITS_PER_WIDE_INT)
5307 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5309 const int end = shift + this_time - 1;
5310 shift = end & -HOST_BITS_PER_WIDE_INT;
5311 this_time = end - shift + 1;
5314 /* Now get the bits from the appropriate constant word. */
5315 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5316 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5318 /* Get the result. This works only when:
5319 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5320 local->byte |= (((value >> shift)
5321 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5322 << (BITS_PER_UNIT - this_time - next_bit));
5324 else
5326 /* On little-endian machines, take the least significant bits of
5327 the value first and pack them starting at the least significant
5328 bits of the bytes. */
5329 shift = next_offset - byte_relative_ebitpos;
5331 /* Don't try to take a bunch of bits that cross
5332 the word boundary in the INTEGER_CST. We can
5333 only select bits from one element. */
5334 if ((shift / HOST_BITS_PER_WIDE_INT)
5335 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5336 this_time
5337 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5339 /* Now get the bits from the appropriate constant word. */
5340 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5341 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5343 /* Get the result. This works only when:
5344 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5345 local->byte |= (((value >> shift)
5346 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5347 << next_bit);
5350 next_offset += this_time;
5351 local->byte_buffer_in_use = true;
5355 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5356 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5357 caller output state of relevance in recursive invocations. */
5359 static unsigned HOST_WIDE_INT
5360 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5361 bool reverse, oc_outer_state *outer)
5363 unsigned HOST_WIDE_INT cnt;
5364 constructor_elt *ce;
5365 oc_local_state local;
5367 /* Setup our local state to communicate with helpers. */
5368 local.exp = exp;
5369 local.type = TREE_TYPE (exp);
5370 local.size = size;
5371 local.align = align;
5372 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5373 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5374 else
5375 local.min_index = integer_zero_node;
5377 local.total_bytes = 0;
5378 local.byte_buffer_in_use = outer != NULL;
5379 local.byte = outer ? outer->byte : 0;
5380 local.last_relative_index = -1;
5381 /* The storage order is specified for every aggregate type. */
5382 if (AGGREGATE_TYPE_P (local.type))
5383 local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5384 else
5385 local.reverse = reverse;
5387 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5389 /* As CE goes through the elements of the constant, FIELD goes through the
5390 structure fields if the constant is a structure. If the constant is a
5391 union, we override this by getting the field from the TREE_LIST element.
5392 But the constant could also be an array. Then FIELD is zero.
5394 There is always a maximum of one element in the chain LINK for unions
5395 (even if the initializer in a source program incorrectly contains
5396 more one). */
5398 if (TREE_CODE (local.type) == RECORD_TYPE)
5399 local.field = TYPE_FIELDS (local.type);
5400 else
5401 local.field = NULL_TREE;
5403 for (cnt = 0;
5404 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5405 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5407 local.val = ce->value;
5408 local.index = NULL_TREE;
5410 /* The element in a union constructor specifies the proper field
5411 or index. */
5412 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5413 local.field = ce->index;
5415 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5416 local.index = ce->index;
5418 if (local.field && flag_verbose_asm)
5419 fprintf (asm_out_file, "%s %s:\n",
5420 ASM_COMMENT_START,
5421 DECL_NAME (local.field)
5422 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5423 : "<anonymous>");
5425 /* Eliminate the marker that makes a cast not be an lvalue. */
5426 if (local.val != NULL_TREE)
5427 STRIP_NOPS (local.val);
5429 /* Output the current element, using the appropriate helper ... */
5431 /* For an array slice not part of an outer bitfield. */
5432 if (!outer
5433 && local.index != NULL_TREE
5434 && TREE_CODE (local.index) == RANGE_EXPR)
5435 output_constructor_array_range (&local);
5437 /* For a field that is neither a true bitfield nor part of an outer one,
5438 known to be at least byte aligned and multiple-of-bytes long. */
5439 else if (!outer
5440 && (local.field == NULL_TREE
5441 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5442 output_constructor_regular_field (&local);
5444 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5445 supported for scalar fields, so we may need to convert first. */
5446 else
5448 if (TREE_CODE (local.val) == REAL_CST)
5449 local.val
5450 = fold_unary (VIEW_CONVERT_EXPR,
5451 build_nonstandard_integer_type
5452 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5453 local.val);
5454 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5458 /* If we are not at toplevel, save the pending data for our caller.
5459 Otherwise output the pending data and padding zeros as needed. */
5460 if (outer)
5461 outer->byte = local.byte;
5462 else
5464 if (local.byte_buffer_in_use)
5466 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5467 local.total_bytes++;
5470 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5472 assemble_zeros (local.size - local.total_bytes);
5473 local.total_bytes = local.size;
5477 return local.total_bytes;
5480 /* Mark DECL as weak. */
5482 static void
5483 mark_weak (tree decl)
5485 if (DECL_WEAK (decl))
5486 return;
5488 struct symtab_node *n = symtab_node::get (decl);
5489 if (n && n->refuse_visibility_changes)
5490 error ("%+qD declared weak after being used", decl);
5491 DECL_WEAK (decl) = 1;
5493 if (DECL_RTL_SET_P (decl)
5494 && MEM_P (DECL_RTL (decl))
5495 && XEXP (DECL_RTL (decl), 0)
5496 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5497 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5500 /* Merge weak status between NEWDECL and OLDDECL. */
5502 void
5503 merge_weak (tree newdecl, tree olddecl)
5505 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5507 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5509 tree *pwd;
5510 /* We put the NEWDECL on the weak_decls list at some point
5511 and OLDDECL as well. Keep just OLDDECL on the list. */
5512 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5513 if (TREE_VALUE (*pwd) == newdecl)
5515 *pwd = TREE_CHAIN (*pwd);
5516 break;
5519 return;
5522 if (DECL_WEAK (newdecl))
5524 tree wd;
5526 /* NEWDECL is weak, but OLDDECL is not. */
5528 /* If we already output the OLDDECL, we're in trouble; we can't
5529 go back and make it weak. This should never happen in
5530 unit-at-a-time compilation. */
5531 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5533 /* If we've already generated rtl referencing OLDDECL, we may
5534 have done so in a way that will not function properly with
5535 a weak symbol. Again in unit-at-a-time this should be
5536 impossible. */
5537 gcc_assert (!TREE_USED (olddecl)
5538 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5540 /* PR 49899: You cannot convert a static function into a weak, public function. */
5541 if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
5542 error ("weak declaration of %q+D being applied to a already "
5543 "existing, static definition", newdecl);
5545 if (TARGET_SUPPORTS_WEAK)
5547 /* We put the NEWDECL on the weak_decls list at some point.
5548 Replace it with the OLDDECL. */
5549 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5550 if (TREE_VALUE (wd) == newdecl)
5552 TREE_VALUE (wd) = olddecl;
5553 break;
5555 /* We may not find the entry on the list. If NEWDECL is a
5556 weak alias, then we will have already called
5557 globalize_decl to remove the entry; in that case, we do
5558 not need to do anything. */
5561 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5562 mark_weak (olddecl);
5564 else
5565 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5566 weak. Just update NEWDECL to indicate that it's weak too. */
5567 mark_weak (newdecl);
5570 /* Declare DECL to be a weak symbol. */
5572 void
5573 declare_weak (tree decl)
5575 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5576 if (! TREE_PUBLIC (decl))
5578 error ("weak declaration of %q+D must be public", decl);
5579 return;
5581 else if (!TARGET_SUPPORTS_WEAK)
5582 warning (0, "weak declaration of %q+D not supported", decl);
5584 mark_weak (decl);
5585 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5586 DECL_ATTRIBUTES (decl)
5587 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5590 static void
5591 weak_finish_1 (tree decl)
5593 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5594 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5595 #endif
5597 if (! TREE_USED (decl))
5598 return;
5600 #ifdef ASM_WEAKEN_DECL
5601 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5602 #else
5603 #ifdef ASM_WEAKEN_LABEL
5604 ASM_WEAKEN_LABEL (asm_out_file, name);
5605 #else
5606 #ifdef ASM_OUTPUT_WEAK_ALIAS
5608 static bool warn_once = 0;
5609 if (! warn_once)
5611 warning (0, "only weak aliases are supported in this configuration");
5612 warn_once = 1;
5614 return;
5616 #endif
5617 #endif
5618 #endif
5621 /* Fiven an assembly name, find the decl it is associated with. */
5622 static tree
5623 find_decl (tree target)
5625 symtab_node *node = symtab_node::get_for_asmname (target);
5626 if (node)
5627 return node->decl;
5628 return NULL_TREE;
5631 /* This TREE_LIST contains weakref targets. */
5633 static GTY(()) tree weakref_targets;
5635 /* Emit any pending weak declarations. */
5637 void
5638 weak_finish (void)
5640 tree t;
5642 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5644 tree alias_decl = TREE_PURPOSE (t);
5645 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5647 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
5648 || TREE_SYMBOL_REFERENCED (target))
5649 /* Remove alias_decl from the weak list, but leave entries for
5650 the target alone. */
5651 target = NULL_TREE;
5652 #ifndef ASM_OUTPUT_WEAKREF
5653 else if (! TREE_SYMBOL_REFERENCED (target))
5655 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5656 defined, otherwise we and weak_finish_1 would use
5657 different macros. */
5658 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5659 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5660 # else
5661 tree decl = find_decl (target);
5663 if (! decl)
5665 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5666 TREE_CODE (alias_decl), target,
5667 TREE_TYPE (alias_decl));
5669 DECL_EXTERNAL (decl) = 1;
5670 TREE_PUBLIC (decl) = 1;
5671 DECL_ARTIFICIAL (decl) = 1;
5672 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5673 TREE_USED (decl) = 1;
5676 weak_finish_1 (decl);
5677 # endif
5679 #endif
5682 tree *p;
5683 tree t2;
5685 /* Remove the alias and the target from the pending weak list
5686 so that we do not emit any .weak directives for the former,
5687 nor multiple .weak directives for the latter. */
5688 for (p = &weak_decls; (t2 = *p) ; )
5690 if (TREE_VALUE (t2) == alias_decl
5691 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5692 *p = TREE_CHAIN (t2);
5693 else
5694 p = &TREE_CHAIN (t2);
5697 /* Remove other weakrefs to the same target, to speed things up. */
5698 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5700 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5701 *p = TREE_CHAIN (t2);
5702 else
5703 p = &TREE_CHAIN (t2);
5708 for (t = weak_decls; t; t = TREE_CHAIN (t))
5710 tree decl = TREE_VALUE (t);
5712 weak_finish_1 (decl);
5716 /* Emit the assembly bits to indicate that DECL is globally visible. */
5718 static void
5719 globalize_decl (tree decl)
5722 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5723 if (DECL_WEAK (decl))
5725 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5726 tree *p, t;
5728 #ifdef ASM_WEAKEN_DECL
5729 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5730 #else
5731 ASM_WEAKEN_LABEL (asm_out_file, name);
5732 #endif
5734 /* Remove this function from the pending weak list so that
5735 we do not emit multiple .weak directives for it. */
5736 for (p = &weak_decls; (t = *p) ; )
5738 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5739 *p = TREE_CHAIN (t);
5740 else
5741 p = &TREE_CHAIN (t);
5744 /* Remove weakrefs to the same target from the pending weakref
5745 list, for the same reason. */
5746 for (p = &weakref_targets; (t = *p) ; )
5748 if (DECL_ASSEMBLER_NAME (decl)
5749 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5750 *p = TREE_CHAIN (t);
5751 else
5752 p = &TREE_CHAIN (t);
5755 return;
5757 #endif
5759 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5762 vec<alias_pair, va_gc> *alias_pairs;
5764 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5765 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5766 tree node is DECL to have the value of the tree node TARGET. */
5768 void
5769 do_assemble_alias (tree decl, tree target)
5771 tree id;
5773 /* Emulated TLS had better not get this var. */
5774 gcc_assert (!(!targetm.have_tls
5775 && VAR_P (decl)
5776 && DECL_THREAD_LOCAL_P (decl)));
5778 if (TREE_ASM_WRITTEN (decl))
5779 return;
5781 id = DECL_ASSEMBLER_NAME (decl);
5782 ultimate_transparent_alias_target (&id);
5783 ultimate_transparent_alias_target (&target);
5785 /* We must force creation of DECL_RTL for debug info generation, even though
5786 we don't use it here. */
5787 make_decl_rtl (decl);
5789 TREE_ASM_WRITTEN (decl) = 1;
5790 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5791 TREE_ASM_WRITTEN (id) = 1;
5793 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5795 if (!TREE_SYMBOL_REFERENCED (target))
5796 weakref_targets = tree_cons (decl, target, weakref_targets);
5798 #ifdef ASM_OUTPUT_WEAKREF
5799 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5800 IDENTIFIER_POINTER (id),
5801 IDENTIFIER_POINTER (target));
5802 #else
5803 if (!TARGET_SUPPORTS_WEAK)
5805 error_at (DECL_SOURCE_LOCATION (decl),
5806 "weakref is not supported in this configuration");
5807 return;
5809 #endif
5810 return;
5813 #ifdef ASM_OUTPUT_DEF
5814 tree orig_decl = decl;
5816 /* Make name accessible from other files, if appropriate. */
5818 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5820 globalize_decl (decl);
5821 maybe_assemble_visibility (decl);
5823 if (TREE_CODE (decl) == FUNCTION_DECL
5824 && cgraph_node::get (decl)->ifunc_resolver)
5826 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5827 if (targetm.has_ifunc_p ())
5828 ASM_OUTPUT_TYPE_DIRECTIVE
5829 (asm_out_file, IDENTIFIER_POINTER (id),
5830 IFUNC_ASM_TYPE);
5831 else
5832 #endif
5833 error_at (DECL_SOURCE_LOCATION (decl),
5834 "ifunc is not supported on this target");
5837 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5838 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5839 # else
5840 ASM_OUTPUT_DEF (asm_out_file,
5841 IDENTIFIER_POINTER (id),
5842 IDENTIFIER_POINTER (target));
5843 # endif
5844 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5846 const char *name;
5847 tree *p, t;
5849 name = IDENTIFIER_POINTER (id);
5850 # ifdef ASM_WEAKEN_DECL
5851 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5852 # else
5853 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5854 # endif
5855 /* Remove this function from the pending weak list so that
5856 we do not emit multiple .weak directives for it. */
5857 for (p = &weak_decls; (t = *p) ; )
5858 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5859 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5860 *p = TREE_CHAIN (t);
5861 else
5862 p = &TREE_CHAIN (t);
5864 /* Remove weakrefs to the same target from the pending weakref
5865 list, for the same reason. */
5866 for (p = &weakref_targets; (t = *p) ; )
5868 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5869 *p = TREE_CHAIN (t);
5870 else
5871 p = &TREE_CHAIN (t);
5874 #endif
5877 /* Emit an assembler directive to make the symbol for DECL an alias to
5878 the symbol for TARGET. */
5880 void
5881 assemble_alias (tree decl, tree target)
5883 tree target_decl;
5885 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5887 tree alias = DECL_ASSEMBLER_NAME (decl);
5889 ultimate_transparent_alias_target (&target);
5891 if (alias == target)
5892 error ("weakref %q+D ultimately targets itself", decl);
5893 if (TREE_PUBLIC (decl))
5894 error ("weakref %q+D must have static linkage", decl);
5896 else
5898 #if !defined (ASM_OUTPUT_DEF)
5899 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5900 error_at (DECL_SOURCE_LOCATION (decl),
5901 "alias definitions not supported in this configuration");
5902 TREE_ASM_WRITTEN (decl) = 1;
5903 return;
5904 # else
5905 if (!DECL_WEAK (decl))
5907 /* NB: ifunc_resolver isn't set when an error is detected. */
5908 if (TREE_CODE (decl) == FUNCTION_DECL
5909 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5910 error_at (DECL_SOURCE_LOCATION (decl),
5911 "ifunc is not supported in this configuration");
5912 else
5913 error_at (DECL_SOURCE_LOCATION (decl),
5914 "only weak aliases are supported in this configuration");
5915 TREE_ASM_WRITTEN (decl) = 1;
5916 return;
5918 # endif
5919 #endif
5921 TREE_USED (decl) = 1;
5923 /* Allow aliases to aliases. */
5924 if (TREE_CODE (decl) == FUNCTION_DECL)
5925 cgraph_node::get_create (decl)->alias = true;
5926 else
5927 varpool_node::get_create (decl)->alias = true;
5929 /* If the target has already been emitted, we don't have to queue the
5930 alias. This saves a tad of memory. */
5931 if (symtab->global_info_ready)
5932 target_decl = find_decl (target);
5933 else
5934 target_decl= NULL;
5935 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5936 || symtab->state >= EXPANSION)
5937 do_assemble_alias (decl, target);
5938 else
5940 alias_pair p = {decl, target};
5941 vec_safe_push (alias_pairs, p);
5945 /* Record and output a table of translations from original function
5946 to its transaction aware clone. Note that tm_pure functions are
5947 considered to be their own clone. */
5949 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
5951 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
5952 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
5954 static int
5955 keep_cache_entry (tree_map *&e)
5957 return ggc_marked_p (e->base.from);
5961 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
5963 void
5964 record_tm_clone_pair (tree o, tree n)
5966 struct tree_map **slot, *h;
5968 if (tm_clone_hash == NULL)
5969 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
5971 h = ggc_alloc<tree_map> ();
5972 h->hash = htab_hash_pointer (o);
5973 h->base.from = o;
5974 h->to = n;
5976 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
5977 *slot = h;
5980 tree
5981 get_tm_clone_pair (tree o)
5983 if (tm_clone_hash)
5985 struct tree_map *h, in;
5987 in.base.from = o;
5988 in.hash = htab_hash_pointer (o);
5989 h = tm_clone_hash->find_with_hash (&in, in.hash);
5990 if (h)
5991 return h->to;
5993 return NULL_TREE;
5996 struct tm_alias_pair
5998 unsigned int uid;
5999 tree from;
6000 tree to;
6004 /* Dump the actual pairs to the .tm_clone_table section. */
6006 static void
6007 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6009 unsigned i;
6010 tm_alias_pair *p;
6011 bool switched = false;
6013 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6015 tree src = p->from;
6016 tree dst = p->to;
6017 struct cgraph_node *src_n = cgraph_node::get (src);
6018 struct cgraph_node *dst_n = cgraph_node::get (dst);
6020 /* The function ipa_tm_create_version() marks the clone as needed if
6021 the original function was needed. But we also mark the clone as
6022 needed if we ever called the clone indirectly through
6023 TM_GETTMCLONE. If neither of these are true, we didn't generate
6024 a clone, and we didn't call it indirectly... no sense keeping it
6025 in the clone table. */
6026 if (!dst_n || !dst_n->definition)
6027 continue;
6029 /* This covers the case where we have optimized the original
6030 function away, and only access the transactional clone. */
6031 if (!src_n || !src_n->definition)
6032 continue;
6034 if (!switched)
6036 switch_to_section (targetm.asm_out.tm_clone_table_section ());
6037 assemble_align (POINTER_SIZE);
6038 switched = true;
6041 assemble_integer (XEXP (DECL_RTL (src), 0),
6042 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6043 assemble_integer (XEXP (DECL_RTL (dst), 0),
6044 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6048 /* Provide a default for the tm_clone_table section. */
6050 section *
6051 default_clone_table_section (void)
6053 return get_named_section (NULL, ".tm_clone_table", 3);
6056 /* Helper comparison function for qsorting by the DECL_UID stored in
6057 alias_pair->emitted_diags. */
6059 static int
6060 tm_alias_pair_cmp (const void *x, const void *y)
6062 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6063 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6064 if (p1->uid < p2->uid)
6065 return -1;
6066 if (p1->uid > p2->uid)
6067 return 1;
6068 return 0;
6071 void
6072 finish_tm_clone_pairs (void)
6074 vec<tm_alias_pair> tm_alias_pairs = vNULL;
6076 if (tm_clone_hash == NULL)
6077 return;
6079 /* We need a determenistic order for the .tm_clone_table, otherwise
6080 we will get bootstrap comparison failures, so dump the hash table
6081 to a vector, sort it, and dump the vector. */
6083 /* Dump the hashtable to a vector. */
6084 tree_map *map;
6085 hash_table<tm_clone_hasher>::iterator iter;
6086 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6088 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6089 tm_alias_pairs.safe_push (p);
6091 /* Sort it. */
6092 tm_alias_pairs.qsort (tm_alias_pair_cmp);
6094 /* Dump it. */
6095 dump_tm_clone_pairs (tm_alias_pairs);
6097 tm_clone_hash->empty ();
6098 tm_clone_hash = NULL;
6099 tm_alias_pairs.release ();
6103 /* Emit an assembler directive to set symbol for DECL visibility to
6104 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
6106 void
6107 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6108 int vis ATTRIBUTE_UNUSED)
6110 #ifdef HAVE_GAS_HIDDEN
6111 static const char * const visibility_types[] = {
6112 NULL, "protected", "hidden", "internal"
6115 const char *name, *type;
6116 tree id;
6118 id = DECL_ASSEMBLER_NAME (decl);
6119 ultimate_transparent_alias_target (&id);
6120 name = IDENTIFIER_POINTER (id);
6122 type = visibility_types[vis];
6124 fprintf (asm_out_file, "\t.%s\t", type);
6125 assemble_name (asm_out_file, name);
6126 fprintf (asm_out_file, "\n");
6127 #else
6128 if (!DECL_ARTIFICIAL (decl))
6129 warning (OPT_Wattributes, "visibility attribute not supported "
6130 "in this configuration; ignored");
6131 #endif
6134 /* A helper function to call assemble_visibility when needed for a decl. */
6137 maybe_assemble_visibility (tree decl)
6139 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6140 if (vis != VISIBILITY_DEFAULT)
6142 targetm.asm_out.assemble_visibility (decl, vis);
6143 return 1;
6145 else
6146 return 0;
6149 /* Returns 1 if the target configuration supports defining public symbols
6150 so that one of them will be chosen at link time instead of generating a
6151 multiply-defined symbol error, whether through the use of weak symbols or
6152 a target-specific mechanism for having duplicates discarded. */
6155 supports_one_only (void)
6157 if (SUPPORTS_ONE_ONLY)
6158 return 1;
6159 return TARGET_SUPPORTS_WEAK;
6162 /* Set up DECL as a public symbol that can be defined in multiple
6163 translation units without generating a linker error. */
6165 void
6166 make_decl_one_only (tree decl, tree comdat_group)
6168 struct symtab_node *symbol;
6169 gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6171 TREE_PUBLIC (decl) = 1;
6173 if (VAR_P (decl))
6174 symbol = varpool_node::get_create (decl);
6175 else
6176 symbol = cgraph_node::get_create (decl);
6178 if (SUPPORTS_ONE_ONLY)
6180 #ifdef MAKE_DECL_ONE_ONLY
6181 MAKE_DECL_ONE_ONLY (decl);
6182 #endif
6183 symbol->set_comdat_group (comdat_group);
6185 else if (VAR_P (decl)
6186 && (DECL_INITIAL (decl) == 0
6187 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6188 DECL_COMMON (decl) = 1;
6189 else
6191 gcc_assert (TARGET_SUPPORTS_WEAK);
6192 DECL_WEAK (decl) = 1;
6196 void
6197 init_varasm_once (void)
6199 section_htab = hash_table<section_hasher>::create_ggc (31);
6200 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6201 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6203 shared_constant_pool = create_constant_pool ();
6205 #ifdef TEXT_SECTION_ASM_OP
6206 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6207 TEXT_SECTION_ASM_OP);
6208 #endif
6210 #ifdef DATA_SECTION_ASM_OP
6211 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6212 DATA_SECTION_ASM_OP);
6213 #endif
6215 #ifdef SDATA_SECTION_ASM_OP
6216 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6217 SDATA_SECTION_ASM_OP);
6218 #endif
6220 #ifdef READONLY_DATA_SECTION_ASM_OP
6221 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6222 READONLY_DATA_SECTION_ASM_OP);
6223 #endif
6225 #ifdef CTORS_SECTION_ASM_OP
6226 ctors_section = get_unnamed_section (0, output_section_asm_op,
6227 CTORS_SECTION_ASM_OP);
6228 #endif
6230 #ifdef DTORS_SECTION_ASM_OP
6231 dtors_section = get_unnamed_section (0, output_section_asm_op,
6232 DTORS_SECTION_ASM_OP);
6233 #endif
6235 #ifdef BSS_SECTION_ASM_OP
6236 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6237 output_section_asm_op,
6238 BSS_SECTION_ASM_OP);
6239 #endif
6241 #ifdef SBSS_SECTION_ASM_OP
6242 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6243 output_section_asm_op,
6244 SBSS_SECTION_ASM_OP);
6245 #endif
6247 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6248 | SECTION_COMMON, emit_tls_common);
6249 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6250 | SECTION_COMMON, emit_local);
6251 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6252 | SECTION_COMMON, emit_common);
6254 #if defined ASM_OUTPUT_ALIGNED_BSS
6255 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6256 emit_bss);
6257 #endif
6259 targetm.asm_out.init_sections ();
6261 if (readonly_data_section == NULL)
6262 readonly_data_section = text_section;
6264 #ifdef ASM_OUTPUT_EXTERNAL
6265 pending_assemble_externals_set = new hash_set<tree>;
6266 #endif
6269 enum tls_model
6270 decl_default_tls_model (const_tree decl)
6272 enum tls_model kind;
6273 bool is_local;
6275 is_local = targetm.binds_local_p (decl);
6276 if (!flag_shlib)
6278 if (is_local)
6279 kind = TLS_MODEL_LOCAL_EXEC;
6280 else
6281 kind = TLS_MODEL_INITIAL_EXEC;
6284 /* Local dynamic is inefficient when we're not combining the
6285 parts of the address. */
6286 else if (optimize && is_local)
6287 kind = TLS_MODEL_LOCAL_DYNAMIC;
6288 else
6289 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6290 if (kind < flag_tls_default)
6291 kind = flag_tls_default;
6293 return kind;
6296 /* Select a set of attributes for section NAME based on the properties
6297 of DECL and whether or not RELOC indicates that DECL's initializer
6298 might contain runtime relocations.
6300 We make the section read-only and executable for a function decl,
6301 read-only for a const data decl, and writable for a non-const data decl. */
6303 unsigned int
6304 default_section_type_flags (tree decl, const char *name, int reloc)
6306 unsigned int flags;
6308 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6309 flags = SECTION_CODE;
6310 else if (decl)
6312 enum section_category category
6313 = categorize_decl_for_section (decl, reloc);
6314 if (decl_readonly_section_1 (category))
6315 flags = 0;
6316 else if (category == SECCAT_DATA_REL_RO
6317 || category == SECCAT_DATA_REL_RO_LOCAL)
6318 flags = SECTION_WRITE | SECTION_RELRO;
6319 else
6320 flags = SECTION_WRITE;
6322 else
6324 flags = SECTION_WRITE;
6325 if (strcmp (name, ".data.rel.ro") == 0
6326 || strcmp (name, ".data.rel.ro.local") == 0)
6327 flags |= SECTION_RELRO;
6330 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6331 flags |= SECTION_LINKONCE;
6333 if (strcmp (name, ".vtable_map_vars") == 0)
6334 flags |= SECTION_LINKONCE;
6336 if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6337 flags |= SECTION_TLS | SECTION_WRITE;
6339 if (strcmp (name, ".bss") == 0
6340 || strncmp (name, ".bss.", 5) == 0
6341 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6342 || strcmp (name, ".persistent.bss") == 0
6343 || strcmp (name, ".sbss") == 0
6344 || strncmp (name, ".sbss.", 6) == 0
6345 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6346 flags |= SECTION_BSS;
6348 if (strcmp (name, ".tdata") == 0
6349 || strncmp (name, ".tdata.", 7) == 0
6350 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6351 flags |= SECTION_TLS;
6353 if (strcmp (name, ".tbss") == 0
6354 || strncmp (name, ".tbss.", 6) == 0
6355 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6356 flags |= SECTION_TLS | SECTION_BSS;
6358 /* Various sections have special ELF types that the assembler will
6359 assign by default based on the name. They are neither SHT_PROGBITS
6360 nor SHT_NOBITS, so when changing sections we don't want to print a
6361 section type (@progbits or @nobits). Rather than duplicating the
6362 assembler's knowledge of what those special name patterns are, just
6363 let the assembler choose the type if we don't know a specific
6364 reason to set it to something other than the default. SHT_PROGBITS
6365 is the default for sections whose name is not specially known to
6366 the assembler, so it does no harm to leave the choice to the
6367 assembler when @progbits is the best thing we know to use. If
6368 someone is silly enough to emit code or TLS variables to one of
6369 these sections, then don't handle them specially.
6371 default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6372 LINKONCE cases when NOTYPE is not set, so leave those to its logic. */
6373 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6374 && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6375 flags |= SECTION_NOTYPE;
6377 return flags;
6380 /* Return true if the target supports some form of global BSS,
6381 either through bss_noswitch_section, or by selecting a BSS
6382 section in TARGET_ASM_SELECT_SECTION. */
6384 bool
6385 have_global_bss_p (void)
6387 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6390 /* Output assembly to switch to section NAME with attribute FLAGS.
6391 Four variants for common object file formats. */
6393 void
6394 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6395 unsigned int flags ATTRIBUTE_UNUSED,
6396 tree decl ATTRIBUTE_UNUSED)
6398 /* Some object formats don't support named sections at all. The
6399 front-end should already have flagged this as an error. */
6400 gcc_unreachable ();
6403 #ifndef TLS_SECTION_ASM_FLAG
6404 #define TLS_SECTION_ASM_FLAG 'T'
6405 #endif
6407 void
6408 default_elf_asm_named_section (const char *name, unsigned int flags,
6409 tree decl)
6411 char flagchars[11], *f = flagchars;
6412 unsigned int numeric_value = 0;
6414 /* If we have already declared this section, we can use an
6415 abbreviated form to switch back to it -- unless this section is
6416 part of a COMDAT groups, in which case GAS requires the full
6417 declaration every time. */
6418 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6419 && (flags & SECTION_DECLARED))
6421 fprintf (asm_out_file, "\t.section\t%s\n", name);
6422 return;
6425 /* If we have a machine specific flag, then use the numeric value to pass
6426 this on to GAS. */
6427 if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6428 snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6429 else
6431 if (!(flags & SECTION_DEBUG))
6432 *f++ = 'a';
6433 #if HAVE_GAS_SECTION_EXCLUDE
6434 if (flags & SECTION_EXCLUDE)
6435 *f++ = 'e';
6436 #endif
6437 if (flags & SECTION_WRITE)
6438 *f++ = 'w';
6439 if (flags & SECTION_CODE)
6440 *f++ = 'x';
6441 if (flags & SECTION_SMALL)
6442 *f++ = 's';
6443 if (flags & SECTION_MERGE)
6444 *f++ = 'M';
6445 if (flags & SECTION_STRINGS)
6446 *f++ = 'S';
6447 if (flags & SECTION_TLS)
6448 *f++ = TLS_SECTION_ASM_FLAG;
6449 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6450 *f++ = 'G';
6451 #ifdef MACH_DEP_SECTION_ASM_FLAG
6452 if (flags & SECTION_MACH_DEP)
6453 *f++ = MACH_DEP_SECTION_ASM_FLAG;
6454 #endif
6455 *f = '\0';
6458 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6460 /* default_section_type_flags (above) knows which flags need special
6461 handling here, and sets NOTYPE when none of these apply so that the
6462 assembler's logic for default types can apply to user-chosen
6463 section names. */
6464 if (!(flags & SECTION_NOTYPE))
6466 const char *type;
6467 const char *format;
6469 if (flags & SECTION_BSS)
6470 type = "nobits";
6471 else
6472 type = "progbits";
6474 format = ",@%s";
6475 /* On platforms that use "@" as the assembly comment character,
6476 use "%" instead. */
6477 if (strcmp (ASM_COMMENT_START, "@") == 0)
6478 format = ",%%%s";
6479 fprintf (asm_out_file, format, type);
6481 if (flags & SECTION_ENTSIZE)
6482 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6483 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6485 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6486 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6487 else
6488 fprintf (asm_out_file, ",%s,comdat",
6489 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6493 putc ('\n', asm_out_file);
6496 void
6497 default_coff_asm_named_section (const char *name, unsigned int flags,
6498 tree decl ATTRIBUTE_UNUSED)
6500 char flagchars[8], *f = flagchars;
6502 if (flags & SECTION_WRITE)
6503 *f++ = 'w';
6504 if (flags & SECTION_CODE)
6505 *f++ = 'x';
6506 *f = '\0';
6508 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6511 void
6512 default_pe_asm_named_section (const char *name, unsigned int flags,
6513 tree decl)
6515 default_coff_asm_named_section (name, flags, decl);
6517 if (flags & SECTION_LINKONCE)
6519 /* Functions may have been compiled at various levels of
6520 optimization so we can't use `same_size' here.
6521 Instead, have the linker pick one. */
6522 fprintf (asm_out_file, "\t.linkonce %s\n",
6523 (flags & SECTION_CODE ? "discard" : "same_size"));
6527 /* The lame default section selector. */
6529 section *
6530 default_select_section (tree decl, int reloc,
6531 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6533 if (DECL_P (decl))
6535 if (decl_readonly_section (decl, reloc))
6536 return readonly_data_section;
6538 else if (TREE_CODE (decl) == CONSTRUCTOR)
6540 if (! ((flag_pic && reloc)
6541 || !TREE_READONLY (decl)
6542 || TREE_SIDE_EFFECTS (decl)
6543 || !TREE_CONSTANT (decl)))
6544 return readonly_data_section;
6546 else if (TREE_CODE (decl) == STRING_CST)
6547 return readonly_data_section;
6548 else if (! (flag_pic && reloc))
6549 return readonly_data_section;
6551 return data_section;
6554 enum section_category
6555 categorize_decl_for_section (const_tree decl, int reloc)
6557 enum section_category ret;
6559 if (TREE_CODE (decl) == FUNCTION_DECL)
6560 return SECCAT_TEXT;
6561 else if (TREE_CODE (decl) == STRING_CST)
6563 if ((flag_sanitize & SANITIZE_ADDRESS)
6564 && asan_protect_global (CONST_CAST_TREE (decl)))
6565 /* or !flag_merge_constants */
6566 return SECCAT_RODATA;
6567 else
6568 return SECCAT_RODATA_MERGE_STR;
6570 else if (VAR_P (decl))
6572 tree d = CONST_CAST_TREE (decl);
6573 if (bss_initializer_p (decl))
6574 ret = SECCAT_BSS;
6575 else if (! TREE_READONLY (decl)
6576 || TREE_SIDE_EFFECTS (decl)
6577 || (DECL_INITIAL (decl)
6578 && ! TREE_CONSTANT (DECL_INITIAL (decl))))
6580 /* Here the reloc_rw_mask is not testing whether the section should
6581 be read-only or not, but whether the dynamic link will have to
6582 do something. If so, we wish to segregate the data in order to
6583 minimize cache misses inside the dynamic linker. */
6584 if (reloc & targetm.asm_out.reloc_rw_mask ())
6585 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6586 else
6587 ret = SECCAT_DATA;
6589 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6590 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6591 else if (reloc || flag_merge_constants < 2
6592 || ((flag_sanitize & SANITIZE_ADDRESS)
6593 /* PR 81697: for architectures that use section anchors we
6594 need to ignore DECL_RTL_SET_P (decl) for string constants
6595 inside this asan_protect_global call because otherwise
6596 we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
6597 section, set DECL_RTL (decl) later on and add DECL to
6598 protected globals via successive asan_protect_global
6599 calls. In this scenario we'll end up with wrong
6600 alignment of these strings at runtime and possible ASan
6601 false positives. */
6602 && asan_protect_global (d, use_object_blocks_p ()
6603 && use_blocks_for_decl_p (d))))
6604 /* C and C++ don't allow different variables to share the same
6605 location. -fmerge-all-constants allows even that (at the
6606 expense of not conforming). */
6607 ret = SECCAT_RODATA;
6608 else if (DECL_INITIAL (decl)
6609 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6610 ret = SECCAT_RODATA_MERGE_STR_INIT;
6611 else
6612 ret = SECCAT_RODATA_MERGE_CONST;
6614 else if (TREE_CODE (decl) == CONSTRUCTOR)
6616 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6617 || TREE_SIDE_EFFECTS (decl)
6618 || ! TREE_CONSTANT (decl))
6619 ret = SECCAT_DATA;
6620 else
6621 ret = SECCAT_RODATA;
6623 else
6624 ret = SECCAT_RODATA;
6626 /* There are no read-only thread-local sections. */
6627 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6629 /* Note that this would be *just* SECCAT_BSS, except that there's
6630 no concept of a read-only thread-local-data section. */
6631 if (ret == SECCAT_BSS
6632 || DECL_INITIAL (decl) == NULL
6633 || (flag_zero_initialized_in_bss
6634 && initializer_zerop (DECL_INITIAL (decl))))
6635 ret = SECCAT_TBSS;
6636 else
6637 ret = SECCAT_TDATA;
6640 /* If the target uses small data sections, select it. */
6641 else if (targetm.in_small_data_p (decl))
6643 if (ret == SECCAT_BSS)
6644 ret = SECCAT_SBSS;
6645 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6646 ret = SECCAT_SRODATA;
6647 else
6648 ret = SECCAT_SDATA;
6651 return ret;
6654 static bool
6655 decl_readonly_section_1 (enum section_category category)
6657 switch (category)
6659 case SECCAT_RODATA:
6660 case SECCAT_RODATA_MERGE_STR:
6661 case SECCAT_RODATA_MERGE_STR_INIT:
6662 case SECCAT_RODATA_MERGE_CONST:
6663 case SECCAT_SRODATA:
6664 return true;
6665 default:
6666 return false;
6670 bool
6671 decl_readonly_section (const_tree decl, int reloc)
6673 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6676 /* Select a section based on the above categorization. */
6678 section *
6679 default_elf_select_section (tree decl, int reloc,
6680 unsigned HOST_WIDE_INT align)
6682 const char *sname;
6683 switch (categorize_decl_for_section (decl, reloc))
6685 case SECCAT_TEXT:
6686 /* We're not supposed to be called on FUNCTION_DECLs. */
6687 gcc_unreachable ();
6688 case SECCAT_RODATA:
6689 return readonly_data_section;
6690 case SECCAT_RODATA_MERGE_STR:
6691 return mergeable_string_section (decl, align, 0);
6692 case SECCAT_RODATA_MERGE_STR_INIT:
6693 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6694 case SECCAT_RODATA_MERGE_CONST:
6695 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6696 case SECCAT_SRODATA:
6697 sname = ".sdata2";
6698 break;
6699 case SECCAT_DATA:
6700 return data_section;
6701 case SECCAT_DATA_REL:
6702 sname = ".data.rel";
6703 break;
6704 case SECCAT_DATA_REL_LOCAL:
6705 sname = ".data.rel.local";
6706 break;
6707 case SECCAT_DATA_REL_RO:
6708 sname = ".data.rel.ro";
6709 break;
6710 case SECCAT_DATA_REL_RO_LOCAL:
6711 sname = ".data.rel.ro.local";
6712 break;
6713 case SECCAT_SDATA:
6714 sname = ".sdata";
6715 break;
6716 case SECCAT_TDATA:
6717 sname = ".tdata";
6718 break;
6719 case SECCAT_BSS:
6720 if (bss_section)
6721 return bss_section;
6722 sname = ".bss";
6723 break;
6724 case SECCAT_SBSS:
6725 sname = ".sbss";
6726 break;
6727 case SECCAT_TBSS:
6728 sname = ".tbss";
6729 break;
6730 default:
6731 gcc_unreachable ();
6734 return get_named_section (decl, sname, reloc);
6737 /* Construct a unique section name based on the decl name and the
6738 categorization performed above. */
6740 void
6741 default_unique_section (tree decl, int reloc)
6743 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6744 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6745 const char *prefix, *name, *linkonce;
6746 char *string;
6747 tree id;
6749 switch (categorize_decl_for_section (decl, reloc))
6751 case SECCAT_TEXT:
6752 prefix = one_only ? ".t" : ".text";
6753 break;
6754 case SECCAT_RODATA:
6755 case SECCAT_RODATA_MERGE_STR:
6756 case SECCAT_RODATA_MERGE_STR_INIT:
6757 case SECCAT_RODATA_MERGE_CONST:
6758 prefix = one_only ? ".r" : ".rodata";
6759 break;
6760 case SECCAT_SRODATA:
6761 prefix = one_only ? ".s2" : ".sdata2";
6762 break;
6763 case SECCAT_DATA:
6764 prefix = one_only ? ".d" : ".data";
6765 break;
6766 case SECCAT_DATA_REL:
6767 prefix = one_only ? ".d.rel" : ".data.rel";
6768 break;
6769 case SECCAT_DATA_REL_LOCAL:
6770 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6771 break;
6772 case SECCAT_DATA_REL_RO:
6773 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6774 break;
6775 case SECCAT_DATA_REL_RO_LOCAL:
6776 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6777 break;
6778 case SECCAT_SDATA:
6779 prefix = one_only ? ".s" : ".sdata";
6780 break;
6781 case SECCAT_BSS:
6782 prefix = one_only ? ".b" : ".bss";
6783 break;
6784 case SECCAT_SBSS:
6785 prefix = one_only ? ".sb" : ".sbss";
6786 break;
6787 case SECCAT_TDATA:
6788 prefix = one_only ? ".td" : ".tdata";
6789 break;
6790 case SECCAT_TBSS:
6791 prefix = one_only ? ".tb" : ".tbss";
6792 break;
6793 default:
6794 gcc_unreachable ();
6797 id = DECL_ASSEMBLER_NAME (decl);
6798 ultimate_transparent_alias_target (&id);
6799 name = IDENTIFIER_POINTER (id);
6800 name = targetm.strip_name_encoding (name);
6802 /* If we're using one_only, then there needs to be a .gnu.linkonce
6803 prefix to the section name. */
6804 linkonce = one_only ? ".gnu.linkonce" : "";
6806 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6808 set_decl_section_name (decl, string);
6811 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6813 static int
6814 compute_reloc_for_rtx_1 (const_rtx x)
6816 switch (GET_CODE (x))
6818 case SYMBOL_REF:
6819 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6820 case LABEL_REF:
6821 return 1;
6822 default:
6823 return 0;
6827 /* Like compute_reloc_for_constant, except for an RTX. The return value
6828 is a mask for which bit 1 indicates a global relocation, and bit 0
6829 indicates a local relocation. */
6831 static int
6832 compute_reloc_for_rtx (const_rtx x)
6834 switch (GET_CODE (x))
6836 case SYMBOL_REF:
6837 case LABEL_REF:
6838 return compute_reloc_for_rtx_1 (x);
6840 case CONST:
6842 int reloc = 0;
6843 subrtx_iterator::array_type array;
6844 FOR_EACH_SUBRTX (iter, array, x, ALL)
6845 reloc |= compute_reloc_for_rtx_1 (*iter);
6846 return reloc;
6849 default:
6850 return 0;
6854 section *
6855 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6856 rtx x,
6857 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6859 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6860 return data_section;
6861 else
6862 return readonly_data_section;
6865 section *
6866 default_elf_select_rtx_section (machine_mode mode, rtx x,
6867 unsigned HOST_WIDE_INT align)
6869 int reloc = compute_reloc_for_rtx (x);
6871 /* ??? Handle small data here somehow. */
6873 if (reloc & targetm.asm_out.reloc_rw_mask ())
6875 if (reloc == 1)
6876 return get_named_section (NULL, ".data.rel.ro.local", 1);
6877 else
6878 return get_named_section (NULL, ".data.rel.ro", 3);
6881 return mergeable_constant_section (mode, align, 0);
6884 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6886 void
6887 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6889 rtx symbol;
6890 int flags;
6892 /* Careful not to prod global register variables. */
6893 if (!MEM_P (rtl))
6894 return;
6895 symbol = XEXP (rtl, 0);
6896 if (GET_CODE (symbol) != SYMBOL_REF)
6897 return;
6899 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6900 if (TREE_CODE (decl) == FUNCTION_DECL)
6901 flags |= SYMBOL_FLAG_FUNCTION;
6902 if (targetm.binds_local_p (decl))
6903 flags |= SYMBOL_FLAG_LOCAL;
6904 if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6905 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6906 else if (targetm.in_small_data_p (decl))
6907 flags |= SYMBOL_FLAG_SMALL;
6908 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6909 being PUBLIC, the thing *must* be defined in this translation unit.
6910 Prevent this buglet from being propagated into rtl code as well. */
6911 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6912 flags |= SYMBOL_FLAG_EXTERNAL;
6914 SYMBOL_REF_FLAGS (symbol) = flags;
6917 /* By default, we do nothing for encode_section_info, so we need not
6918 do anything but discard the '*' marker. */
6920 const char *
6921 default_strip_name_encoding (const char *str)
6923 return str + (*str == '*');
6926 #ifdef ASM_OUTPUT_DEF
6927 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6928 anchor relative to ".", the current section position. */
6930 void
6931 default_asm_output_anchor (rtx symbol)
6933 char buffer[100];
6935 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6936 SYMBOL_REF_BLOCK_OFFSET (symbol));
6937 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6939 #endif
6941 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6943 bool
6944 default_use_anchors_for_symbol_p (const_rtx symbol)
6946 section *sect;
6947 tree decl;
6949 /* Don't use anchors for mergeable sections. The linker might move
6950 the objects around. */
6951 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6952 if (sect->common.flags & SECTION_MERGE)
6953 return false;
6955 /* Don't use anchors for small data sections. The small data register
6956 acts as an anchor for such sections. */
6957 if (sect->common.flags & SECTION_SMALL)
6958 return false;
6960 decl = SYMBOL_REF_DECL (symbol);
6961 if (decl && DECL_P (decl))
6963 /* Don't use section anchors for decls that might be defined or
6964 usurped by other modules. */
6965 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6966 return false;
6968 /* Don't use section anchors for decls that will be placed in a
6969 small data section. */
6970 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6971 one above. The problem is that we only use SECTION_SMALL for
6972 sections that should be marked as small in the section directive. */
6973 if (targetm.in_small_data_p (decl))
6974 return false;
6976 /* Don't use section anchors for decls that won't fit inside a single
6977 anchor range to reduce the amount of instructions required to refer
6978 to the entire declaration. */
6979 if (DECL_SIZE_UNIT (decl) == NULL_TREE
6980 || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
6981 || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
6982 >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
6983 return false;
6986 return true;
6989 /* Return true when RESOLUTION indicate that symbol will be bound to the
6990 definition provided by current .o file. */
6992 static bool
6993 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6995 return (resolution == LDPR_PREVAILING_DEF
6996 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6997 || resolution == LDPR_PREVAILING_DEF_IRONLY);
7000 /* Return true when RESOLUTION indicate that symbol will be bound locally
7001 within current executable or DSO. */
7003 static bool
7004 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7006 return (resolution == LDPR_PREVAILING_DEF
7007 || resolution == LDPR_PREVAILING_DEF_IRONLY
7008 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7009 || resolution == LDPR_PREEMPTED_REG
7010 || resolution == LDPR_PREEMPTED_IR
7011 || resolution == LDPR_RESOLVED_IR
7012 || resolution == LDPR_RESOLVED_EXEC);
7015 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7016 uninitialized common symbol in the executable will still be defined
7017 (through COPY relocation) in the executable. */
7019 bool
7020 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7021 bool extern_protected_data, bool common_local_p)
7023 /* A non-decl is an entry in the constant pool. */
7024 if (!DECL_P (exp))
7025 return true;
7027 /* Weakrefs may not bind locally, even though the weakref itself is always
7028 static and therefore local. Similarly, the resolver for ifunc functions
7029 might resolve to a non-local function.
7030 FIXME: We can resolve the weakref case more curefuly by looking at the
7031 weakref alias. */
7032 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7033 || (TREE_CODE (exp) == FUNCTION_DECL
7034 && cgraph_node::get (exp)
7035 && cgraph_node::get (exp)->ifunc_resolver))
7036 return false;
7038 /* Static variables are always local. */
7039 if (! TREE_PUBLIC (exp))
7040 return true;
7042 /* With resolution file in hand, take look into resolutions.
7043 We can't just return true for resolved_locally symbols,
7044 because dynamic linking might overwrite symbols
7045 in shared libraries. */
7046 bool resolved_locally = false;
7048 bool uninited_common = (DECL_COMMON (exp)
7049 && (DECL_INITIAL (exp) == NULL
7050 || (!in_lto_p
7051 && DECL_INITIAL (exp) == error_mark_node)));
7053 /* A non-external variable is defined locally only if it isn't
7054 uninitialized COMMON variable or common_local_p is true. */
7055 bool defined_locally = (!DECL_EXTERNAL (exp)
7056 && (!uninited_common || common_local_p));
7057 if (symtab_node *node = symtab_node::get (exp))
7059 if (node->in_other_partition)
7060 defined_locally = true;
7061 if (node->can_be_discarded_p ())
7063 else if (resolution_to_local_definition_p (node->resolution))
7064 defined_locally = resolved_locally = true;
7065 else if (resolution_local_p (node->resolution))
7066 resolved_locally = true;
7068 if (defined_locally && weak_dominate && !shlib)
7069 resolved_locally = true;
7071 /* Undefined weak symbols are never defined locally. */
7072 if (DECL_WEAK (exp) && !defined_locally)
7073 return false;
7075 /* A symbol is local if the user has said explicitly that it will be,
7076 or if we have a definition for the symbol. We cannot infer visibility
7077 for undefined symbols. */
7078 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7079 && (TREE_CODE (exp) == FUNCTION_DECL
7080 || !extern_protected_data
7081 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7082 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7083 return true;
7085 /* If PIC, then assume that any global name can be overridden by
7086 symbols resolved from other modules. */
7087 if (shlib)
7088 return false;
7090 /* Variables defined outside this object might not be local. */
7091 if (DECL_EXTERNAL (exp) && !resolved_locally)
7092 return false;
7094 /* Non-dominant weak symbols are not defined locally. */
7095 if (DECL_WEAK (exp) && !resolved_locally)
7096 return false;
7098 /* Uninitialized COMMON variable may be unified with symbols
7099 resolved from other modules. */
7100 if (uninited_common && !resolved_locally)
7101 return false;
7103 /* Otherwise we're left with initialized (or non-common) global data
7104 which is of necessity defined locally. */
7105 return true;
7108 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7109 wrt cross-module name binding. */
7111 bool
7112 default_binds_local_p (const_tree exp)
7114 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7117 /* Similar to default_binds_local_p, but common symbol may be local and
7118 extern protected data is non-local. */
7120 bool
7121 default_binds_local_p_2 (const_tree exp)
7123 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7124 !flag_pic);
7127 bool
7128 default_binds_local_p_1 (const_tree exp, int shlib)
7130 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7133 /* Return true when references to DECL must bind to current definition in
7134 final executable.
7136 The condition is usually equivalent to whether the function binds to the
7137 current module (shared library or executable), that is to binds_local_p.
7138 We use this fact to avoid need for another target hook and implement
7139 the logic using binds_local_p and just special cases where
7140 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
7141 the weak definitions (that can be overwritten at linktime by other
7142 definition from different object file) and when resolution info is available
7143 we simply use the knowledge passed to us by linker plugin. */
7144 bool
7145 decl_binds_to_current_def_p (const_tree decl)
7147 gcc_assert (DECL_P (decl));
7148 if (!targetm.binds_local_p (decl))
7149 return false;
7150 if (!TREE_PUBLIC (decl))
7151 return true;
7153 /* When resolution is available, just use it. */
7154 if (symtab_node *node = symtab_node::get (decl))
7156 if (node->resolution != LDPR_UNKNOWN
7157 && !node->can_be_discarded_p ())
7158 return resolution_to_local_definition_p (node->resolution);
7161 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7162 binds locally but still can be overwritten), DECL_COMMON (can be merged
7163 with a non-common definition somewhere in the same module) or
7164 DECL_EXTERNAL.
7165 This rely on fact that binds_local_p behave as decl_replaceable_p
7166 for all other declaration types. */
7167 if (DECL_WEAK (decl))
7168 return false;
7169 if (DECL_COMMON (decl)
7170 && (DECL_INITIAL (decl) == NULL
7171 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7172 return false;
7173 if (DECL_EXTERNAL (decl))
7174 return false;
7175 return true;
7178 /* A replaceable function or variable is one which may be replaced
7179 at link-time with an entirely different definition, provided that the
7180 replacement has the same type. For example, functions declared
7181 with __attribute__((weak)) on most systems are replaceable.
7183 COMDAT functions are not replaceable, since all definitions of the
7184 function must be equivalent. It is important that COMDAT functions
7185 not be treated as replaceable so that use of C++ template
7186 instantiations is not penalized. */
7188 bool
7189 decl_replaceable_p (tree decl)
7191 gcc_assert (DECL_P (decl));
7192 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7193 return false;
7194 if (!flag_semantic_interposition
7195 && !DECL_WEAK (decl))
7196 return false;
7197 return !decl_binds_to_current_def_p (decl);
7200 /* Default function to output code that will globalize a label. A
7201 target must define GLOBAL_ASM_OP or provide its own function to
7202 globalize a label. */
7203 #ifdef GLOBAL_ASM_OP
7204 void
7205 default_globalize_label (FILE * stream, const char *name)
7207 fputs (GLOBAL_ASM_OP, stream);
7208 assemble_name (stream, name);
7209 putc ('\n', stream);
7211 #endif /* GLOBAL_ASM_OP */
7213 /* Default function to output code that will globalize a declaration. */
7214 void
7215 default_globalize_decl_name (FILE * stream, tree decl)
7217 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7218 targetm.asm_out.globalize_label (stream, name);
7221 /* Default function to output a label for unwind information. The
7222 default is to do nothing. A target that needs nonlocal labels for
7223 unwind information must provide its own function to do this. */
7224 void
7225 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7226 tree decl ATTRIBUTE_UNUSED,
7227 int for_eh ATTRIBUTE_UNUSED,
7228 int empty ATTRIBUTE_UNUSED)
7232 /* Default function to output a label to divide up the exception table.
7233 The default is to do nothing. A target that needs/wants to divide
7234 up the table must provide it's own function to do this. */
7235 void
7236 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7240 /* This is how to output an internal numbered label where PREFIX is
7241 the class of label and LABELNO is the number within the class. */
7243 void
7244 default_generate_internal_label (char *buf, const char *prefix,
7245 unsigned long labelno)
7247 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7250 /* This is how to output an internal numbered label where PREFIX is
7251 the class of label and LABELNO is the number within the class. */
7253 void
7254 default_internal_label (FILE *stream, const char *prefix,
7255 unsigned long labelno)
7257 char *const buf = (char *) alloca (40 + strlen (prefix));
7258 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7259 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7263 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7265 void
7266 default_asm_declare_constant_name (FILE *file, const char *name,
7267 const_tree exp ATTRIBUTE_UNUSED,
7268 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7270 assemble_label (file, name);
7273 /* This is the default behavior at the beginning of a file. It's
7274 controlled by two other target-hook toggles. */
7275 void
7276 default_file_start (void)
7278 if (targetm.asm_file_start_app_off
7279 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7280 fputs (ASM_APP_OFF, asm_out_file);
7282 if (targetm.asm_file_start_file_directive)
7284 /* LTO produced units have no meaningful main_input_filename. */
7285 if (in_lto_p)
7286 output_file_directive (asm_out_file, "<artificial>");
7287 else
7288 output_file_directive (asm_out_file, main_input_filename);
7292 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7293 which emits a special section directive used to indicate whether or
7294 not this object file needs an executable stack. This is primarily
7295 a GNU extension to ELF but could be used on other targets. */
7297 int trampolines_created;
7299 void
7300 file_end_indicate_exec_stack (void)
7302 unsigned int flags = SECTION_DEBUG;
7303 if (trampolines_created)
7304 flags |= SECTION_CODE;
7306 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7309 /* Emit a special section directive to indicate that this object file
7310 was compiled with -fsplit-stack. This is used to let the linker
7311 detect calls between split-stack code and non-split-stack code, so
7312 that it can modify the split-stack code to allocate a sufficiently
7313 large stack. We emit another special section if there are any
7314 functions in this file which have the no_split_stack attribute, to
7315 prevent the linker from warning about being unable to convert the
7316 functions if they call non-split-stack code. */
7318 void
7319 file_end_indicate_split_stack (void)
7321 if (flag_split_stack)
7323 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7324 NULL));
7325 if (saw_no_split_stack)
7326 switch_to_section (get_section (".note.GNU-no-split-stack",
7327 SECTION_DEBUG, NULL));
7331 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7332 a get_unnamed_section callback. */
7334 void
7335 output_section_asm_op (const void *directive)
7337 fprintf (asm_out_file, "%s\n", (const char *) directive);
7340 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7341 the current section is NEW_SECTION. */
7343 void
7344 switch_to_section (section *new_section)
7346 if (in_section == new_section)
7347 return;
7349 if (new_section->common.flags & SECTION_FORGET)
7350 in_section = NULL;
7351 else
7352 in_section = new_section;
7354 switch (SECTION_STYLE (new_section))
7356 case SECTION_NAMED:
7357 targetm.asm_out.named_section (new_section->named.name,
7358 new_section->named.common.flags,
7359 new_section->named.decl);
7360 break;
7362 case SECTION_UNNAMED:
7363 new_section->unnamed.callback (new_section->unnamed.data);
7364 break;
7366 case SECTION_NOSWITCH:
7367 gcc_unreachable ();
7368 break;
7371 new_section->common.flags |= SECTION_DECLARED;
7374 /* If block symbol SYMBOL has not yet been assigned an offset, place
7375 it at the end of its block. */
7377 void
7378 place_block_symbol (rtx symbol)
7380 unsigned HOST_WIDE_INT size, mask, offset;
7381 struct constant_descriptor_rtx *desc;
7382 unsigned int alignment;
7383 struct object_block *block;
7384 tree decl;
7386 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7387 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7388 return;
7390 /* Work out the symbol's size and alignment. */
7391 if (CONSTANT_POOL_ADDRESS_P (symbol))
7393 desc = SYMBOL_REF_CONSTANT (symbol);
7394 alignment = desc->align;
7395 size = GET_MODE_SIZE (desc->mode);
7397 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7399 decl = SYMBOL_REF_DECL (symbol);
7400 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7401 alignment = DECL_ALIGN (decl);
7402 size = get_constant_size (DECL_INITIAL (decl));
7403 if ((flag_sanitize & SANITIZE_ADDRESS)
7404 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7405 && asan_protect_global (DECL_INITIAL (decl)))
7407 size += asan_red_zone_size (size);
7408 alignment = MAX (alignment,
7409 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7412 else
7414 struct symtab_node *snode;
7415 decl = SYMBOL_REF_DECL (symbol);
7417 snode = symtab_node::get (decl);
7418 if (snode->alias)
7420 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7422 gcc_assert (MEM_P (target)
7423 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7424 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7425 target = XEXP (target, 0);
7426 place_block_symbol (target);
7427 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7428 return;
7430 alignment = get_variable_align (decl);
7431 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7432 if ((flag_sanitize & SANITIZE_ADDRESS)
7433 && asan_protect_global (decl))
7435 size += asan_red_zone_size (size);
7436 alignment = MAX (alignment,
7437 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7441 /* Calculate the object's offset from the start of the block. */
7442 block = SYMBOL_REF_BLOCK (symbol);
7443 mask = alignment / BITS_PER_UNIT - 1;
7444 offset = (block->size + mask) & ~mask;
7445 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7447 /* Record the block's new alignment and size. */
7448 block->alignment = MAX (block->alignment, alignment);
7449 block->size = offset + size;
7451 vec_safe_push (block->objects, symbol);
7454 /* Return the anchor that should be used to address byte offset OFFSET
7455 from the first object in BLOCK. MODEL is the TLS model used
7456 to access it. */
7459 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7460 enum tls_model model)
7462 char label[100];
7463 unsigned int begin, middle, end;
7464 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7465 rtx anchor;
7467 /* Work out the anchor's offset. Use an offset of 0 for the first
7468 anchor so that we don't pessimize the case where we take the address
7469 of a variable at the beginning of the block. This is particularly
7470 useful when a block has only one variable assigned to it.
7472 We try to place anchors RANGE bytes apart, so there can then be
7473 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7474 a ptr_mode offset. With some target settings, the lowest such
7475 anchor might be out of range for the lowest ptr_mode offset;
7476 likewise the highest anchor for the highest offset. Use anchors
7477 at the extreme ends of the ptr_mode range in such cases.
7479 All arithmetic uses unsigned integers in order to avoid
7480 signed overflow. */
7481 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7482 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7483 range = max_offset - min_offset + 1;
7484 if (range == 0)
7485 offset = 0;
7486 else
7488 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7489 if (offset < 0)
7491 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7492 delta -= delta % range;
7493 if (delta > bias)
7494 delta = bias;
7495 offset = (HOST_WIDE_INT) (-delta);
7497 else
7499 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7500 delta -= delta % range;
7501 if (delta > bias - 1)
7502 delta = bias - 1;
7503 offset = (HOST_WIDE_INT) delta;
7507 /* Do a binary search to see if there's already an anchor we can use.
7508 Set BEGIN to the new anchor's index if not. */
7509 begin = 0;
7510 end = vec_safe_length (block->anchors);
7511 while (begin != end)
7513 middle = (end + begin) / 2;
7514 anchor = (*block->anchors)[middle];
7515 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7516 end = middle;
7517 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7518 begin = middle + 1;
7519 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7520 end = middle;
7521 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7522 begin = middle + 1;
7523 else
7524 return anchor;
7527 /* Create a new anchor with a unique label. */
7528 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7529 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7530 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7531 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7533 /* Insert it at index BEGIN. */
7534 vec_safe_insert (block->anchors, begin, anchor);
7535 return anchor;
7538 /* Output the objects in BLOCK. */
7540 static void
7541 output_object_block (struct object_block *block)
7543 struct constant_descriptor_rtx *desc;
7544 unsigned int i;
7545 HOST_WIDE_INT offset;
7546 tree decl;
7547 rtx symbol;
7549 if (!block->objects)
7550 return;
7552 /* Switch to the section and make sure that the first byte is
7553 suitably aligned. */
7554 /* Special case VTV comdat sections similar to assemble_variable. */
7555 if (SECTION_STYLE (block->sect) == SECTION_NAMED
7556 && block->sect->named.name
7557 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
7558 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
7559 else
7560 switch_to_section (block->sect);
7562 assemble_align (block->alignment);
7564 /* Define the values of all anchors relative to the current section
7565 position. */
7566 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7567 targetm.asm_out.output_anchor (symbol);
7569 /* Output the objects themselves. */
7570 offset = 0;
7571 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7573 /* Move to the object's offset, padding with zeros if necessary. */
7574 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7575 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7576 if (CONSTANT_POOL_ADDRESS_P (symbol))
7578 desc = SYMBOL_REF_CONSTANT (symbol);
7579 /* Pass 1 for align as we have already laid out everything in the block.
7580 So aligning shouldn't be necessary. */
7581 output_constant_pool_1 (desc, 1);
7582 offset += GET_MODE_SIZE (desc->mode);
7584 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7586 HOST_WIDE_INT size;
7587 decl = SYMBOL_REF_DECL (symbol);
7588 assemble_constant_contents
7589 (DECL_INITIAL (decl), XSTR (symbol, 0), DECL_ALIGN (decl));
7591 size = get_constant_size (DECL_INITIAL (decl));
7592 offset += size;
7593 if ((flag_sanitize & SANITIZE_ADDRESS)
7594 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7595 && asan_protect_global (DECL_INITIAL (decl)))
7597 size = asan_red_zone_size (size);
7598 assemble_zeros (size);
7599 offset += size;
7602 else
7604 HOST_WIDE_INT size;
7605 decl = SYMBOL_REF_DECL (symbol);
7606 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7607 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7608 offset += size;
7609 if ((flag_sanitize & SANITIZE_ADDRESS)
7610 && asan_protect_global (decl))
7612 size = asan_red_zone_size (size);
7613 assemble_zeros (size);
7614 offset += size;
7620 /* A callback for qsort to compare object_blocks. */
7622 static int
7623 output_object_block_compare (const void *x, const void *y)
7625 object_block *p1 = *(object_block * const*)x;
7626 object_block *p2 = *(object_block * const*)y;
7628 if (p1->sect->common.flags & SECTION_NAMED
7629 && !(p2->sect->common.flags & SECTION_NAMED))
7630 return 1;
7632 if (!(p1->sect->common.flags & SECTION_NAMED)
7633 && p2->sect->common.flags & SECTION_NAMED)
7634 return -1;
7636 if (p1->sect->common.flags & SECTION_NAMED
7637 && p2->sect->common.flags & SECTION_NAMED)
7638 return strcmp (p1->sect->named.name, p2->sect->named.name);
7640 unsigned f1 = p1->sect->common.flags;
7641 unsigned f2 = p2->sect->common.flags;
7642 if (f1 == f2)
7643 return 0;
7644 return f1 < f2 ? -1 : 1;
7647 /* Output the definitions of all object_blocks. */
7649 void
7650 output_object_blocks (void)
7652 vec<object_block *, va_heap> v;
7653 v.create (object_block_htab->elements ());
7654 object_block *obj;
7655 hash_table<object_block_hasher>::iterator hi;
7657 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
7658 v.quick_push (obj);
7660 /* Sort them in order to output them in a deterministic manner,
7661 otherwise we may get .rodata sections in different orders with
7662 and without -g. */
7663 v.qsort (output_object_block_compare);
7664 unsigned i;
7665 FOR_EACH_VEC_ELT (v, i, obj)
7666 output_object_block (obj);
7668 v.release ();
7671 /* This function provides a possible implementation of the
7672 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7673 by -frecord-gcc-switches it creates a new mergeable, string section in the
7674 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7675 contains the switches in ASCII format.
7677 FIXME: This code does not correctly handle double quote characters
7678 that appear inside strings, (it strips them rather than preserving them).
7679 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7680 characters - instead it treats them as sub-string separators. Since
7681 we want to emit NUL strings terminators into the object file we have to use
7682 ASM_OUTPUT_SKIP. */
7685 elf_record_gcc_switches (print_switch_type type, const char * name)
7687 switch (type)
7689 case SWITCH_TYPE_PASSED:
7690 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7691 ASM_OUTPUT_SKIP (asm_out_file, HOST_WIDE_INT_1U);
7692 break;
7694 case SWITCH_TYPE_DESCRIPTIVE:
7695 if (name == NULL)
7697 /* Distinguish between invocations where name is NULL. */
7698 static bool started = false;
7700 if (!started)
7702 section * sec;
7704 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7705 SECTION_DEBUG
7706 | SECTION_MERGE
7707 | SECTION_STRINGS
7708 | (SECTION_ENTSIZE & 1),
7709 NULL);
7710 switch_to_section (sec);
7711 started = true;
7715 default:
7716 break;
7719 /* The return value is currently ignored by the caller, but must be 0.
7720 For -fverbose-asm the return value would be the number of characters
7721 emitted into the assembler file. */
7722 return 0;
7725 /* Emit text to declare externally defined symbols. It is needed to
7726 properly support non-default visibility. */
7727 void
7728 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7729 tree decl,
7730 const char *name ATTRIBUTE_UNUSED)
7732 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7733 set in order to avoid putting out names that are never really
7734 used. Always output visibility specified in the source. */
7735 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7736 && (DECL_VISIBILITY_SPECIFIED (decl)
7737 || targetm.binds_local_p (decl)))
7738 maybe_assemble_visibility (decl);
7741 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7743 void
7744 default_asm_output_source_filename (FILE *file, const char *name)
7746 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7747 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7748 #else
7749 fprintf (file, "\t.file\t");
7750 output_quoted_string (file, name);
7751 putc ('\n', file);
7752 #endif
7755 /* Output a file name in the form wanted by System V. */
7757 void
7758 output_file_directive (FILE *asm_file, const char *input_name)
7760 int len;
7761 const char *na;
7763 if (input_name == NULL)
7764 input_name = "<stdin>";
7765 else
7766 input_name = remap_debug_filename (input_name);
7768 len = strlen (input_name);
7769 na = input_name + len;
7771 /* NA gets INPUT_NAME sans directory names. */
7772 while (na > input_name)
7774 if (IS_DIR_SEPARATOR (na[-1]))
7775 break;
7776 na--;
7779 targetm.asm_out.output_source_filename (asm_file, na);
7782 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7783 EXP. */
7785 make_debug_expr_from_rtl (const_rtx exp)
7787 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7788 machine_mode mode = GET_MODE (exp);
7789 rtx dval;
7791 DECL_ARTIFICIAL (ddecl) = 1;
7792 if (REG_P (exp) && REG_EXPR (exp))
7793 type = TREE_TYPE (REG_EXPR (exp));
7794 else if (MEM_P (exp) && MEM_EXPR (exp))
7795 type = TREE_TYPE (MEM_EXPR (exp));
7796 else
7797 type = NULL_TREE;
7798 if (type && TYPE_MODE (type) == mode)
7799 TREE_TYPE (ddecl) = type;
7800 else
7801 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7802 SET_DECL_MODE (ddecl, mode);
7803 dval = gen_rtx_DEBUG_EXPR (mode);
7804 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7805 SET_DECL_RTL (ddecl, dval);
7806 return dval;
7809 #ifdef ELF_ASCII_ESCAPES
7810 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7812 void
7813 default_elf_asm_output_limited_string (FILE *f, const char *s)
7815 int escape;
7816 unsigned char c;
7818 fputs (STRING_ASM_OP, f);
7819 putc ('"', f);
7820 while (*s != '\0')
7822 c = *s;
7823 escape = ELF_ASCII_ESCAPES[c];
7824 switch (escape)
7826 case 0:
7827 putc (c, f);
7828 break;
7829 case 1:
7830 putc ('\\', f);
7831 putc ('0'+((c>>6)&7), f);
7832 putc ('0'+((c>>3)&7), f);
7833 putc ('0'+(c&7), f);
7834 break;
7835 default:
7836 putc ('\\', f);
7837 putc (escape, f);
7838 break;
7840 s++;
7842 putc ('\"', f);
7843 putc ('\n', f);
7846 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7848 void
7849 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7851 const char *limit = s + len;
7852 const char *last_null = NULL;
7853 unsigned bytes_in_chunk = 0;
7854 unsigned char c;
7855 int escape;
7857 for (; s < limit; s++)
7859 const char *p;
7861 if (bytes_in_chunk >= 60)
7863 putc ('\"', f);
7864 putc ('\n', f);
7865 bytes_in_chunk = 0;
7868 if (s > last_null)
7870 for (p = s; p < limit && *p != '\0'; p++)
7871 continue;
7872 last_null = p;
7874 else
7875 p = last_null;
7877 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7879 if (bytes_in_chunk > 0)
7881 putc ('\"', f);
7882 putc ('\n', f);
7883 bytes_in_chunk = 0;
7886 default_elf_asm_output_limited_string (f, s);
7887 s = p;
7889 else
7891 if (bytes_in_chunk == 0)
7892 fputs (ASCII_DATA_ASM_OP "\"", f);
7894 c = *s;
7895 escape = ELF_ASCII_ESCAPES[c];
7896 switch (escape)
7898 case 0:
7899 putc (c, f);
7900 bytes_in_chunk++;
7901 break;
7902 case 1:
7903 putc ('\\', f);
7904 putc ('0'+((c>>6)&7), f);
7905 putc ('0'+((c>>3)&7), f);
7906 putc ('0'+(c&7), f);
7907 bytes_in_chunk += 4;
7908 break;
7909 default:
7910 putc ('\\', f);
7911 putc (escape, f);
7912 bytes_in_chunk += 2;
7913 break;
7919 if (bytes_in_chunk > 0)
7921 putc ('\"', f);
7922 putc ('\n', f);
7925 #endif
7927 static GTY(()) section *elf_init_array_section;
7928 static GTY(()) section *elf_fini_array_section;
7930 static section *
7931 get_elf_initfini_array_priority_section (int priority,
7932 bool constructor_p)
7934 section *sec;
7935 if (priority != DEFAULT_INIT_PRIORITY)
7937 char buf[18];
7938 sprintf (buf, "%s.%.5u",
7939 constructor_p ? ".init_array" : ".fini_array",
7940 priority);
7941 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7943 else
7945 if (constructor_p)
7947 if (elf_init_array_section == NULL)
7948 elf_init_array_section
7949 = get_section (".init_array",
7950 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7951 sec = elf_init_array_section;
7953 else
7955 if (elf_fini_array_section == NULL)
7956 elf_fini_array_section
7957 = get_section (".fini_array",
7958 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7959 sec = elf_fini_array_section;
7962 return sec;
7965 /* Use .init_array section for constructors. */
7967 void
7968 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7970 section *sec = get_elf_initfini_array_priority_section (priority,
7971 true);
7972 assemble_addr_to_section (symbol, sec);
7975 /* Use .fini_array section for destructors. */
7977 void
7978 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7980 section *sec = get_elf_initfini_array_priority_section (priority,
7981 false);
7982 assemble_addr_to_section (symbol, sec);
7985 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7987 This is a bit of a cheat. The real default is a no-op, but this
7988 hook is the default for all targets with a .ident directive. */
7990 void
7991 default_asm_output_ident_directive (const char *ident_str)
7993 const char *ident_asm_op = "\t.ident\t";
7995 /* If we are still in the front end, do not write out the string
7996 to asm_out_file. Instead, add a fake top-level asm statement.
7997 This allows the front ends to use this hook without actually
7998 writing to asm_out_file, to handle #ident or Pragma Ident. */
7999 if (symtab->state == PARSING)
8001 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8002 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8004 else
8005 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8009 /* This function ensures that vtable_map variables are not only
8010 in the comdat section, but that each variable has its own unique
8011 comdat name. Without this the variables end up in the same section
8012 with a single comdat name.
8014 FIXME: resolve_unique_section needs to deal better with
8015 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
8016 that is fixed, this if-else statement can be replaced with
8017 a single call to "switch_to_section (sect)". */
8019 static void
8020 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8022 #if defined (OBJECT_FORMAT_ELF)
8023 targetm.asm_out.named_section (sect->named.name,
8024 sect->named.common.flags
8025 | SECTION_LINKONCE,
8026 DECL_NAME (decl));
8027 in_section = sect;
8028 #else
8029 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8030 Therefore the following check is used.
8031 In case a the target is PE or COFF a comdat group section
8032 is created, e.g. .vtable_map_vars$foo. The linker places
8033 everything in .vtable_map_vars at the end.
8035 A fix could be made in
8036 gcc/config/i386/winnt.c: i386_pe_unique_section. */
8037 if (TARGET_PECOFF)
8039 char *name;
8041 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
8042 name = ACONCAT ((sect->named.name, "$",
8043 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
8044 else
8045 name = ACONCAT ((sect->named.name, "$",
8046 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
8047 NULL));
8049 targetm.asm_out.named_section (name,
8050 sect->named.common.flags
8051 | SECTION_LINKONCE,
8052 DECL_NAME (decl));
8053 in_section = sect;
8055 else
8056 switch_to_section (sect);
8057 #endif
8060 #include "gt-varasm.h"