* tree-switch-conversion.c (build_one_array): Set DECL_IGNORED_P
[official-gcc.git] / gcc / varasm.c
blob93b9699404df5ec4350d8f2c1679a5689e17569e
1 /* Output variables, constants and external declarations, for GNU compiler.
2 Copyright (C) 1987-2015 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 "tm.h"
32 #include "rtl.h"
33 #include "hash-set.h"
34 #include "machmode.h"
35 #include "vec.h"
36 #include "double-int.h"
37 #include "input.h"
38 #include "alias.h"
39 #include "symtab.h"
40 #include "wide-int.h"
41 #include "inchash.h"
42 #include "tree.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "stringpool.h"
46 #include "varasm.h"
47 #include "flags.h"
48 #include "hard-reg-set.h"
49 #include "function.h"
50 #include "hashtab.h"
51 #include "statistics.h"
52 #include "real.h"
53 #include "fixed-value.h"
54 #include "insn-config.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "stmt.h"
61 #include "expr.h"
62 #include "regs.h"
63 #include "output.h"
64 #include "diagnostic-core.h"
65 #include "ggc.h"
66 #include "langhooks.h"
67 #include "tm_p.h"
68 #include "debug.h"
69 #include "target.h"
70 #include "common/common-target.h"
71 #include "targhooks.h"
72 #include "predict.h"
73 #include "dominance.h"
74 #include "cfg.h"
75 #include "basic-block.h"
76 #include "hash-map.h"
77 #include "is-a.h"
78 #include "plugin-api.h"
79 #include "ipa-ref.h"
80 #include "cgraph.h"
81 #include "asan.h"
82 #include "rtl-iter.h"
83 #include "tree-chkp.h"
85 #ifdef XCOFF_DEBUGGING_INFO
86 #include "xcoffout.h" /* Needed for external data
87 declarations for e.g. AIX 4.x. */
88 #endif
90 /* The (assembler) name of the first globally-visible object output. */
91 extern GTY(()) const char *first_global_object_name;
92 extern GTY(()) const char *weak_global_object_name;
94 const char *first_global_object_name;
95 const char *weak_global_object_name;
97 struct addr_const;
98 struct constant_descriptor_rtx;
99 struct rtx_constant_pool;
101 #define n_deferred_constants (crtl->varasm.deferred_constants)
103 /* Number for making the label on the next
104 constant that is stored in memory. */
106 static GTY(()) int const_labelno;
108 /* Carry information from ASM_DECLARE_OBJECT_NAME
109 to ASM_FINISH_DECLARE_OBJECT. */
111 int size_directive_output;
113 /* The last decl for which assemble_variable was called,
114 if it did ASM_DECLARE_OBJECT_NAME.
115 If the last call to assemble_variable didn't do that,
116 this holds 0. */
118 tree last_assemble_variable_decl;
120 /* The following global variable indicates if the first basic block
121 in a function belongs to the cold partition or not. */
123 bool first_function_block_is_cold;
125 /* Whether we saw any functions with no_split_stack. */
127 static bool saw_no_split_stack;
129 static const char *strip_reg_name (const char *);
130 static int contains_pointers_p (tree);
131 #ifdef ASM_OUTPUT_EXTERNAL
132 static bool incorporeal_function_p (tree);
133 #endif
134 static void decode_addr_const (tree, struct addr_const *);
135 static hashval_t const_hash_1 (const tree);
136 static int compare_constant (const tree, const tree);
137 static void output_constant_def_contents (rtx);
138 static void output_addressed_constants (tree);
139 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
140 unsigned int);
141 static void globalize_decl (tree);
142 static bool decl_readonly_section_1 (enum section_category);
143 #ifdef BSS_SECTION_ASM_OP
144 #ifdef ASM_OUTPUT_ALIGNED_BSS
145 static void asm_output_aligned_bss (FILE *, tree, const char *,
146 unsigned HOST_WIDE_INT, int)
147 ATTRIBUTE_UNUSED;
148 #endif
149 #endif /* BSS_SECTION_ASM_OP */
150 static void mark_weak (tree);
151 static void output_constant_pool (const char *, tree);
153 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
154 section *text_section;
155 section *data_section;
156 section *readonly_data_section;
157 section *sdata_section;
158 section *ctors_section;
159 section *dtors_section;
160 section *bss_section;
161 section *sbss_section;
163 /* Various forms of common section. All are guaranteed to be nonnull. */
164 section *tls_comm_section;
165 section *comm_section;
166 section *lcomm_section;
168 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
169 May be null. */
170 section *bss_noswitch_section;
172 /* The section that holds the main exception table, when known. The section
173 is set either by the target's init_sections hook or by the first call to
174 switch_to_exception_section. */
175 section *exception_section;
177 /* The section that holds the DWARF2 frame unwind information, when known.
178 The section is set either by the target's init_sections hook or by the
179 first call to switch_to_eh_frame_section. */
180 section *eh_frame_section;
182 /* asm_out_file's current section. This is NULL if no section has yet
183 been selected or if we lose track of what the current section is. */
184 section *in_section;
186 /* True if code for the current function is currently being directed
187 at the cold section. */
188 bool in_cold_section_p;
190 /* The following global holds the "function name" for the code in the
191 cold section of a function, if hot/cold function splitting is enabled
192 and there was actually code that went into the cold section. A
193 pseudo function name is needed for the cold section of code for some
194 debugging tools that perform symbolization. */
195 tree cold_function_name = NULL_TREE;
197 /* A linked list of all the unnamed sections. */
198 static GTY(()) section *unnamed_sections;
200 /* Return a nonzero value if DECL has a section attribute. */
201 #define IN_NAMED_SECTION(DECL) \
202 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
203 && DECL_SECTION_NAME (DECL) != NULL)
205 struct section_hasher : ggc_hasher<section *>
207 typedef const char *compare_type;
209 static hashval_t hash (section *);
210 static bool equal (section *, const char *);
213 /* Hash table of named sections. */
214 static GTY(()) hash_table<section_hasher> *section_htab;
216 struct object_block_hasher : ggc_hasher<object_block *>
218 typedef const section *compare_type;
220 static hashval_t hash (object_block *);
221 static bool equal (object_block *, const section *);
224 /* A table of object_blocks, indexed by section. */
225 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
227 /* The next number to use for internal anchor labels. */
228 static GTY(()) int anchor_labelno;
230 /* A pool of constants that can be shared between functions. */
231 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
233 /* Helper routines for maintaining section_htab. */
235 bool
236 section_hasher::equal (section *old, const char *new_name)
238 return strcmp (old->named.name, new_name) == 0;
241 hashval_t
242 section_hasher::hash (section *old)
244 return htab_hash_string (old->named.name);
247 /* Return a hash value for section SECT. */
249 static hashval_t
250 hash_section (section *sect)
252 if (sect->common.flags & SECTION_NAMED)
253 return htab_hash_string (sect->named.name);
254 return sect->common.flags;
257 /* Helper routines for maintaining object_block_htab. */
259 inline bool
260 object_block_hasher::equal (object_block *old, const section *new_section)
262 return old->sect == new_section;
265 hashval_t
266 object_block_hasher::hash (object_block *old)
268 return hash_section (old->sect);
271 /* Return a new unnamed section with the given fields. */
273 section *
274 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
275 const void *data)
277 section *sect;
279 sect = ggc_alloc<section> ();
280 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
281 sect->unnamed.callback = callback;
282 sect->unnamed.data = data;
283 sect->unnamed.next = unnamed_sections;
285 unnamed_sections = sect;
286 return sect;
289 /* Return a SECTION_NOSWITCH section with the given fields. */
291 static section *
292 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
294 section *sect;
296 sect = ggc_alloc<section> ();
297 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
298 sect->noswitch.callback = callback;
300 return sect;
303 /* Return the named section structure associated with NAME. Create
304 a new section with the given fields if no such structure exists. */
306 section *
307 get_section (const char *name, unsigned int flags, tree decl)
309 section *sect, **slot;
311 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
312 INSERT);
313 flags |= SECTION_NAMED;
314 if (*slot == NULL)
316 sect = ggc_alloc<section> ();
317 sect->named.common.flags = flags;
318 sect->named.name = ggc_strdup (name);
319 sect->named.decl = decl;
320 *slot = sect;
322 else
324 sect = *slot;
325 if ((sect->common.flags & ~SECTION_DECLARED) != flags
326 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
328 /* It is fine if one of the section flags is
329 SECTION_WRITE | SECTION_RELRO and the other has none of these
330 flags (i.e. read-only) in named sections and either the
331 section hasn't been declared yet or has been declared as writable.
332 In that case just make sure the resulting flags are
333 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
334 relocations. */
335 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
336 == (SECTION_WRITE | SECTION_RELRO)
337 && (sect->common.flags
338 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
339 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
340 && ((sect->common.flags & SECTION_DECLARED) == 0
341 || (sect->common.flags & SECTION_WRITE)))
343 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
344 return sect;
346 /* Sanity check user variables for flag changes. */
347 if (sect->named.decl != NULL
348 && DECL_P (sect->named.decl)
349 && decl != sect->named.decl)
351 if (decl != NULL && DECL_P (decl))
352 error ("%+D causes a section type conflict with %D",
353 decl, sect->named.decl);
354 else
355 error ("section type conflict with %D", sect->named.decl);
356 inform (DECL_SOURCE_LOCATION (sect->named.decl),
357 "%qD was declared here", sect->named.decl);
359 else if (decl != NULL && DECL_P (decl))
360 error ("%+D causes a section type conflict", decl);
361 else
362 error ("section type conflict");
363 /* Make sure we don't error about one section multiple times. */
364 sect->common.flags |= SECTION_OVERRIDE;
367 return sect;
370 /* Return true if the current compilation mode benefits from having
371 objects grouped into blocks. */
373 static bool
374 use_object_blocks_p (void)
376 return flag_section_anchors;
379 /* Return the object_block structure for section SECT. Create a new
380 structure if we haven't created one already. Return null if SECT
381 itself is null. */
383 static struct object_block *
384 get_block_for_section (section *sect)
386 struct object_block *block;
388 if (sect == NULL)
389 return NULL;
391 object_block **slot
392 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
393 INSERT);
394 block = *slot;
395 if (block == NULL)
397 block = ggc_cleared_alloc<object_block> ();
398 block->sect = sect;
399 *slot = block;
401 return block;
404 /* Create a symbol with label LABEL and place it at byte offset
405 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
406 is not yet known. LABEL must be a garbage-collected string. */
408 static rtx
409 create_block_symbol (const char *label, struct object_block *block,
410 HOST_WIDE_INT offset)
412 rtx symbol;
413 unsigned int size;
415 /* Create the extended SYMBOL_REF. */
416 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
417 symbol = (rtx) ggc_internal_alloc (size);
419 /* Initialize the normal SYMBOL_REF fields. */
420 memset (symbol, 0, size);
421 PUT_CODE (symbol, SYMBOL_REF);
422 PUT_MODE (symbol, Pmode);
423 XSTR (symbol, 0) = label;
424 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
426 /* Initialize the block_symbol stuff. */
427 SYMBOL_REF_BLOCK (symbol) = block;
428 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
430 return symbol;
433 /* Return a section with a particular name and with whatever SECTION_*
434 flags section_type_flags deems appropriate. The name of the section
435 is taken from NAME if nonnull, otherwise it is taken from DECL's
436 DECL_SECTION_NAME. DECL is the decl associated with the section
437 (see the section comment for details) and RELOC is as for
438 section_type_flags. */
440 section *
441 get_named_section (tree decl, const char *name, int reloc)
443 unsigned int flags;
445 if (name == NULL)
447 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
448 name = DECL_SECTION_NAME (decl);
451 flags = targetm.section_type_flags (decl, name, reloc);
452 return get_section (name, flags, decl);
455 /* Worker for resolve_unique_section. */
457 static bool
458 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
460 n->implicit_section = true;
461 return false;
464 /* If required, set DECL_SECTION_NAME to a unique name. */
466 void
467 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
468 int flag_function_or_data_sections)
470 if (DECL_SECTION_NAME (decl) == NULL
471 && targetm_common.have_named_sections
472 && (flag_function_or_data_sections
473 || DECL_COMDAT_GROUP (decl)))
475 targetm.asm_out.unique_section (decl, reloc);
476 if (DECL_SECTION_NAME (decl))
477 symtab_node::get (decl)->call_for_symbol_and_aliases
478 (set_implicit_section, NULL, true);
482 #ifdef BSS_SECTION_ASM_OP
484 #ifdef ASM_OUTPUT_ALIGNED_BSS
486 /* Utility function for targets to use in implementing
487 ASM_OUTPUT_ALIGNED_BSS.
488 ??? It is believed that this function will work in most cases so such
489 support is localized here. */
491 static void
492 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
493 const char *name, unsigned HOST_WIDE_INT size,
494 int align)
496 switch_to_section (bss_section);
497 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
498 #ifdef ASM_DECLARE_OBJECT_NAME
499 last_assemble_variable_decl = decl;
500 ASM_DECLARE_OBJECT_NAME (file, name, decl);
501 #else
502 /* Standard thing is just output label for the object. */
503 ASM_OUTPUT_LABEL (file, name);
504 #endif /* ASM_DECLARE_OBJECT_NAME */
505 ASM_OUTPUT_SKIP (file, size ? size : 1);
508 #endif
510 #endif /* BSS_SECTION_ASM_OP */
512 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
513 /* Return the hot section for function DECL. Return text_section for
514 null DECLs. */
516 static section *
517 hot_function_section (tree decl)
519 if (decl != NULL_TREE
520 && DECL_SECTION_NAME (decl) != NULL
521 && targetm_common.have_named_sections)
522 return get_named_section (decl, NULL, 0);
523 else
524 return text_section;
526 #endif
528 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
529 is NULL.
531 When DECL_SECTION_NAME is non-NULL and it is implicit section and
532 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
533 concatenate the name with NAMED_SECTION_SUFFIX.
534 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
536 section *
537 get_named_text_section (tree decl,
538 const char *text_section_name,
539 const char *named_section_suffix)
541 if (decl && DECL_SECTION_NAME (decl))
543 if (named_section_suffix)
545 const char *dsn = DECL_SECTION_NAME (decl);
546 const char *stripped_name;
547 char *name, *buffer;
549 name = (char *) alloca (strlen (dsn) + 1);
550 memcpy (name, dsn,
551 strlen (dsn) + 1);
553 stripped_name = targetm.strip_name_encoding (name);
555 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
556 return get_named_section (decl, buffer, 0);
558 else if (symtab_node::get (decl)->implicit_section)
560 const char *name;
562 /* Do not try to split gnu_linkonce functions. This gets somewhat
563 slipperly. */
564 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
565 return NULL;
566 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
567 name = targetm.strip_name_encoding (name);
568 return get_named_section (decl, ACONCAT ((text_section_name, ".",
569 name, NULL)), 0);
571 else
572 return NULL;
574 return get_named_section (decl, text_section_name, 0);
577 /* Choose named function section based on its frequency. */
579 section *
580 default_function_section (tree decl, enum node_frequency freq,
581 bool startup, bool exit)
583 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
584 /* Old GNU linkers have buggy --gc-section support, which sometimes
585 results in .gcc_except_table* sections being garbage collected. */
586 if (decl
587 && symtab_node::get (decl)->implicit_section)
588 return NULL;
589 #endif
591 if (!flag_reorder_functions
592 || !targetm_common.have_named_sections)
593 return NULL;
594 /* Startup code should go to startup subsection unless it is
595 unlikely executed (this happens especially with function splitting
596 where we can split away unnecessary parts of static constructors. */
597 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
599 /* If we do have a profile or(and) LTO phase is executed, we do not need
600 these ELF section. */
601 if (!in_lto_p || !flag_profile_values)
602 return get_named_text_section (decl, ".text.startup", NULL);
603 else
604 return NULL;
607 /* Similarly for exit. */
608 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
609 return get_named_text_section (decl, ".text.exit", NULL);
611 /* Group cold functions together, similarly for hot code. */
612 switch (freq)
614 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
615 return get_named_text_section (decl, ".text.unlikely", NULL);
616 case NODE_FREQUENCY_HOT:
617 /* If we do have a profile or(and) LTO phase is executed, we do not need
618 these ELF section. */
619 if (!in_lto_p || !flag_profile_values)
620 return get_named_text_section (decl, ".text.hot", NULL);
621 default:
622 return NULL;
626 /* Return the section for function DECL.
628 If DECL is NULL_TREE, return the text section. We can be passed
629 NULL_TREE under some circumstances by dbxout.c at least.
631 If FORCE_COLD is true, return cold function section ignoring
632 the frequency info of cgraph_node. */
634 static section *
635 function_section_1 (tree decl, bool force_cold)
637 section *section = NULL;
638 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
639 bool startup = false, exit = false;
641 if (decl)
643 struct cgraph_node *node = cgraph_node::get (decl);
645 if (node)
647 freq = node->frequency;
648 startup = node->only_called_at_startup;
649 exit = node->only_called_at_exit;
652 if (force_cold)
653 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
655 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
656 if (decl != NULL_TREE
657 && DECL_SECTION_NAME (decl) != NULL)
659 if (targetm.asm_out.function_section)
660 section = targetm.asm_out.function_section (decl, freq,
661 startup, exit);
662 if (section)
663 return section;
664 return get_named_section (decl, NULL, 0);
666 else
667 return targetm.asm_out.select_section
668 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
669 symtab_node::get (decl)->definition_alignment ());
670 #else
671 if (targetm.asm_out.function_section)
672 section = targetm.asm_out.function_section (decl, freq, startup, exit);
673 if (section)
674 return section;
675 return hot_function_section (decl);
676 #endif
679 /* Return the section for function DECL.
681 If DECL is NULL_TREE, return the text section. We can be passed
682 NULL_TREE under some circumstances by dbxout.c at least. */
684 section *
685 function_section (tree decl)
687 /* Handle cases where function splitting code decides
688 to put function entry point into unlikely executed section
689 despite the fact that the function itself is not cold
690 (i.e. it is called rarely but contains a hot loop that is
691 better to live in hot subsection for the code locality). */
692 return function_section_1 (decl,
693 first_function_block_is_cold);
696 /* Return the section for the current function, take IN_COLD_SECTION_P
697 into account. */
699 section *
700 current_function_section (void)
702 return function_section_1 (current_function_decl, in_cold_section_p);
705 /* Tell assembler to switch to unlikely-to-be-executed text section. */
707 section *
708 unlikely_text_section (void)
710 return function_section_1 (current_function_decl, true);
713 /* When called within a function context, return true if the function
714 has been assigned a cold text section and if SECT is that section.
715 When called outside a function context, return true if SECT is the
716 default cold section. */
718 bool
719 unlikely_text_section_p (section *sect)
721 return sect == function_section_1 (current_function_decl, true);
724 /* Return the read-only data section associated with function DECL. */
726 section *
727 default_function_rodata_section (tree decl)
729 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
731 const char *name = DECL_SECTION_NAME (decl);
733 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
735 const char *dot;
736 size_t len;
737 char* rname;
739 dot = strchr (name + 1, '.');
740 if (!dot)
741 dot = name;
742 len = strlen (dot) + 8;
743 rname = (char *) alloca (len);
745 strcpy (rname, ".rodata");
746 strcat (rname, dot);
747 return get_section (rname, SECTION_LINKONCE, decl);
749 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
750 else if (DECL_COMDAT_GROUP (decl)
751 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
753 size_t len = strlen (name) + 1;
754 char *rname = (char *) alloca (len);
756 memcpy (rname, name, len);
757 rname[14] = 'r';
758 return get_section (rname, SECTION_LINKONCE, decl);
760 /* For .text.foo we want to use .rodata.foo. */
761 else if (flag_function_sections && flag_data_sections
762 && strncmp (name, ".text.", 6) == 0)
764 size_t len = strlen (name) + 1;
765 char *rname = (char *) alloca (len + 2);
767 memcpy (rname, ".rodata", 7);
768 memcpy (rname + 7, name + 5, len - 5);
769 return get_section (rname, 0, decl);
773 return readonly_data_section;
776 /* Return the read-only data section associated with function DECL
777 for targets where that section should be always the single
778 readonly data section. */
780 section *
781 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
783 return readonly_data_section;
786 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
788 static const char *
789 function_mergeable_rodata_prefix (void)
791 section *s = targetm.asm_out.function_rodata_section (current_function_decl);
792 if (SECTION_STYLE (s) == SECTION_NAMED)
793 return s->named.name;
794 else
795 return targetm.asm_out.mergeable_rodata_prefix;
798 /* Return the section to use for string merging. */
800 static section *
801 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
802 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
803 unsigned int flags ATTRIBUTE_UNUSED)
805 HOST_WIDE_INT len;
807 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
808 && TREE_CODE (decl) == STRING_CST
809 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
810 && align <= 256
811 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
812 && TREE_STRING_LENGTH (decl) >= len)
814 machine_mode mode;
815 unsigned int modesize;
816 const char *str;
817 HOST_WIDE_INT i;
818 int j, unit;
819 const char *prefix = function_mergeable_rodata_prefix ();
820 char *name = (char *) alloca (strlen (prefix) + 30);
822 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
823 modesize = GET_MODE_BITSIZE (mode);
824 if (modesize >= 8 && modesize <= 256
825 && (modesize & (modesize - 1)) == 0)
827 if (align < modesize)
828 align = modesize;
830 str = TREE_STRING_POINTER (decl);
831 unit = GET_MODE_SIZE (mode);
833 /* Check for embedded NUL characters. */
834 for (i = 0; i < len; i += unit)
836 for (j = 0; j < unit; j++)
837 if (str[i + j] != '\0')
838 break;
839 if (j == unit)
840 break;
842 if (i == len - unit)
844 sprintf (name, "%s.str%d.%d", prefix,
845 modesize / 8, (int) (align / 8));
846 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
847 return get_section (name, flags, NULL);
852 return readonly_data_section;
855 /* Return the section to use for constant merging. */
857 section *
858 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
859 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
860 unsigned int flags ATTRIBUTE_UNUSED)
862 unsigned int modesize = GET_MODE_BITSIZE (mode);
864 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
865 && mode != VOIDmode
866 && mode != BLKmode
867 && modesize <= align
868 && align >= 8
869 && align <= 256
870 && (align & (align - 1)) == 0)
872 const char *prefix = function_mergeable_rodata_prefix ();
873 char *name = (char *) alloca (strlen (prefix) + 30);
875 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
876 flags |= (align / 8) | SECTION_MERGE;
877 return get_section (name, flags, NULL);
879 return readonly_data_section;
882 /* Given NAME, a putative register name, discard any customary prefixes. */
884 static const char *
885 strip_reg_name (const char *name)
887 #ifdef REGISTER_PREFIX
888 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
889 name += strlen (REGISTER_PREFIX);
890 #endif
891 if (name[0] == '%' || name[0] == '#')
892 name++;
893 return name;
896 /* The user has asked for a DECL to have a particular name. Set (or
897 change) it in such a way that we don't prefix an underscore to
898 it. */
899 void
900 set_user_assembler_name (tree decl, const char *name)
902 char *starred = (char *) alloca (strlen (name) + 2);
903 starred[0] = '*';
904 strcpy (starred + 1, name);
905 symtab->change_decl_assembler_name (decl, get_identifier (starred));
906 SET_DECL_RTL (decl, NULL_RTX);
909 /* Decode an `asm' spec for a declaration as a register name.
910 Return the register number, or -1 if nothing specified,
911 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
912 or -3 if ASMSPEC is `cc' and is not recognized,
913 or -4 if ASMSPEC is `memory' and is not recognized.
914 Accept an exact spelling or a decimal number.
915 Prefixes such as % are optional. */
918 decode_reg_name_and_count (const char *asmspec, int *pnregs)
920 /* Presume just one register is clobbered. */
921 *pnregs = 1;
923 if (asmspec != 0)
925 int i;
927 /* Get rid of confusing prefixes. */
928 asmspec = strip_reg_name (asmspec);
930 /* Allow a decimal number as a "register name". */
931 for (i = strlen (asmspec) - 1; i >= 0; i--)
932 if (! ISDIGIT (asmspec[i]))
933 break;
934 if (asmspec[0] != 0 && i < 0)
936 i = atoi (asmspec);
937 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
938 return i;
939 else
940 return -2;
943 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
944 if (reg_names[i][0]
945 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
946 return i;
948 #ifdef OVERLAPPING_REGISTER_NAMES
950 static const struct
952 const char *const name;
953 const int number;
954 const int nregs;
955 } table[] = OVERLAPPING_REGISTER_NAMES;
957 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
958 if (table[i].name[0]
959 && ! strcmp (asmspec, table[i].name))
961 *pnregs = table[i].nregs;
962 return table[i].number;
965 #endif /* OVERLAPPING_REGISTER_NAMES */
967 #ifdef ADDITIONAL_REGISTER_NAMES
969 static const struct { const char *const name; const int number; } table[]
970 = ADDITIONAL_REGISTER_NAMES;
972 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
973 if (table[i].name[0]
974 && ! strcmp (asmspec, table[i].name)
975 && reg_names[table[i].number][0])
976 return table[i].number;
978 #endif /* ADDITIONAL_REGISTER_NAMES */
980 if (!strcmp (asmspec, "memory"))
981 return -4;
983 if (!strcmp (asmspec, "cc"))
984 return -3;
986 return -2;
989 return -1;
993 decode_reg_name (const char *name)
995 int count;
996 return decode_reg_name_and_count (name, &count);
1000 /* Return true if DECL's initializer is suitable for a BSS section. */
1002 bool
1003 bss_initializer_p (const_tree decl)
1005 return (DECL_INITIAL (decl) == NULL
1006 /* In LTO we have no errors in program; error_mark_node is used
1007 to mark offlined constructors. */
1008 || (DECL_INITIAL (decl) == error_mark_node
1009 && !in_lto_p)
1010 || (flag_zero_initialized_in_bss
1011 /* Leave constant zeroes in .rodata so they
1012 can be shared. */
1013 && !TREE_READONLY (decl)
1014 && initializer_zerop (DECL_INITIAL (decl))));
1017 /* Compute the alignment of variable specified by DECL.
1018 DONT_OUTPUT_DATA is from assemble_variable. */
1020 void
1021 align_variable (tree decl, bool dont_output_data)
1023 unsigned int align = DECL_ALIGN (decl);
1025 /* In the case for initialing an array whose length isn't specified,
1026 where we have not yet been able to do the layout,
1027 figure out the proper alignment now. */
1028 if (dont_output_data && DECL_SIZE (decl) == 0
1029 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1030 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1032 /* Some object file formats have a maximum alignment which they support.
1033 In particular, a.out format supports a maximum alignment of 4. */
1034 if (align > MAX_OFILE_ALIGNMENT)
1036 error ("alignment of %q+D is greater than maximum object "
1037 "file alignment %d", decl,
1038 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1039 align = MAX_OFILE_ALIGNMENT;
1042 if (! DECL_USER_ALIGN (decl))
1044 #ifdef DATA_ABI_ALIGNMENT
1045 unsigned int data_abi_align
1046 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1047 /* For backwards compatibility, don't assume the ABI alignment for
1048 TLS variables. */
1049 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1050 align = data_abi_align;
1051 #endif
1053 /* On some machines, it is good to increase alignment sometimes.
1054 But as DECL_ALIGN is used both for actually emitting the variable
1055 and for code accessing the variable as guaranteed alignment, we
1056 can only increase the alignment if it is a performance optimization
1057 if the references to it must bind to the current definition. */
1058 if (decl_binds_to_current_def_p (decl)
1059 && !DECL_VIRTUAL_P (decl))
1061 #ifdef DATA_ALIGNMENT
1062 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1063 /* Don't increase alignment too much for TLS variables - TLS space
1064 is too precious. */
1065 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1066 align = data_align;
1067 #endif
1068 #ifdef CONSTANT_ALIGNMENT
1069 if (DECL_INITIAL (decl) != 0
1070 /* In LTO we have no errors in program; error_mark_node is used
1071 to mark offlined constructors. */
1072 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1074 unsigned int const_align
1075 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1076 /* Don't increase alignment too much for TLS variables - TLS
1077 space is too precious. */
1078 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1079 align = const_align;
1081 #endif
1085 /* Reset the alignment in case we have made it tighter, so we can benefit
1086 from it in get_pointer_alignment. */
1087 DECL_ALIGN (decl) = align;
1090 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1091 beyond what align_variable returned. */
1093 static unsigned int
1094 get_variable_align (tree decl)
1096 unsigned int align = DECL_ALIGN (decl);
1098 /* For user aligned vars or static vars align_variable already did
1099 everything. */
1100 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1101 return align;
1103 #ifdef DATA_ABI_ALIGNMENT
1104 if (DECL_THREAD_LOCAL_P (decl))
1105 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1106 #endif
1108 /* For decls that bind to the current definition, align_variable
1109 did also everything, except for not assuming ABI required alignment
1110 of TLS variables. For other vars, increase the alignment here
1111 as an optimization. */
1112 if (!decl_binds_to_current_def_p (decl))
1114 /* On some machines, it is good to increase alignment sometimes. */
1115 #ifdef DATA_ALIGNMENT
1116 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1117 /* Don't increase alignment too much for TLS variables - TLS space
1118 is too precious. */
1119 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1120 align = data_align;
1121 #endif
1122 #ifdef CONSTANT_ALIGNMENT
1123 if (DECL_INITIAL (decl) != 0
1124 /* In LTO we have no errors in program; error_mark_node is used
1125 to mark offlined constructors. */
1126 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1128 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1129 align);
1130 /* Don't increase alignment too much for TLS variables - TLS space
1131 is too precious. */
1132 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1133 align = const_align;
1135 #endif
1138 return align;
1141 /* Return the section into which the given VAR_DECL or CONST_DECL
1142 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1143 section should be used wherever possible. */
1145 section *
1146 get_variable_section (tree decl, bool prefer_noswitch_p)
1148 addr_space_t as = ADDR_SPACE_GENERIC;
1149 int reloc;
1150 varpool_node *vnode = varpool_node::get (decl);
1151 if (vnode)
1153 vnode = vnode->ultimate_alias_target ();
1154 decl = vnode->decl;
1157 if (TREE_TYPE (decl) != error_mark_node)
1158 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1160 /* We need the constructor to figure out reloc flag. */
1161 if (vnode)
1162 vnode->get_constructor ();
1164 if (DECL_COMMON (decl))
1166 /* If the decl has been given an explicit section name, or it resides
1167 in a non-generic address space, then it isn't common, and shouldn't
1168 be handled as such. */
1169 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1170 && ADDR_SPACE_GENERIC_P (as));
1171 if (DECL_THREAD_LOCAL_P (decl))
1172 return tls_comm_section;
1173 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1174 return comm_section;
1177 if (DECL_INITIAL (decl) == error_mark_node)
1178 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1179 else if (DECL_INITIAL (decl))
1180 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1181 else
1182 reloc = 0;
1184 resolve_unique_section (decl, reloc, flag_data_sections);
1185 if (IN_NAMED_SECTION (decl))
1186 return get_named_section (decl, NULL, reloc);
1188 if (ADDR_SPACE_GENERIC_P (as)
1189 && !DECL_THREAD_LOCAL_P (decl)
1190 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1191 && bss_initializer_p (decl))
1193 if (!TREE_PUBLIC (decl)
1194 && !((flag_sanitize & SANITIZE_ADDRESS)
1195 && asan_protect_global (decl)))
1196 return lcomm_section;
1197 if (bss_noswitch_section)
1198 return bss_noswitch_section;
1201 return targetm.asm_out.select_section (decl, reloc,
1202 get_variable_align (decl));
1205 /* Return the block into which object_block DECL should be placed. */
1207 static struct object_block *
1208 get_block_for_decl (tree decl)
1210 section *sect;
1212 if (TREE_CODE (decl) == VAR_DECL)
1214 /* The object must be defined in this translation unit. */
1215 if (DECL_EXTERNAL (decl))
1216 return NULL;
1218 /* There's no point using object blocks for something that is
1219 isolated by definition. */
1220 if (DECL_COMDAT_GROUP (decl))
1221 return NULL;
1224 /* We can only calculate block offsets if the decl has a known
1225 constant size. */
1226 if (DECL_SIZE_UNIT (decl) == NULL)
1227 return NULL;
1228 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1229 return NULL;
1231 /* Find out which section should contain DECL. We cannot put it into
1232 an object block if it requires a standalone definition. */
1233 if (TREE_CODE (decl) == VAR_DECL)
1234 align_variable (decl, 0);
1235 sect = get_variable_section (decl, true);
1236 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1237 return NULL;
1239 return get_block_for_section (sect);
1242 /* Make sure block symbol SYMBOL is in block BLOCK. */
1244 static void
1245 change_symbol_block (rtx symbol, struct object_block *block)
1247 if (block != SYMBOL_REF_BLOCK (symbol))
1249 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1250 SYMBOL_REF_BLOCK (symbol) = block;
1254 /* Return true if it is possible to put DECL in an object_block. */
1256 static bool
1257 use_blocks_for_decl_p (tree decl)
1259 struct symtab_node *snode;
1261 /* Only data DECLs can be placed into object blocks. */
1262 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1263 return false;
1265 /* Detect decls created by dw2_force_const_mem. Such decls are
1266 special because DECL_INITIAL doesn't specify the decl's true value.
1267 dw2_output_indirect_constants will instead call assemble_variable
1268 with dont_output_data set to 1 and then print the contents itself. */
1269 if (DECL_INITIAL (decl) == decl)
1270 return false;
1272 /* If this decl is an alias, then we don't want to emit a
1273 definition. */
1274 if (TREE_CODE (decl) == VAR_DECL
1275 && (snode = symtab_node::get (decl)) != NULL
1276 && snode->alias)
1277 return false;
1279 return targetm.use_blocks_for_decl_p (decl);
1282 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1283 until we find an identifier that is not itself a transparent alias.
1284 Modify the alias passed to it by reference (and all aliases on the
1285 way to the ultimate target), such that they do not have to be
1286 followed again, and return the ultimate target of the alias
1287 chain. */
1289 static inline tree
1290 ultimate_transparent_alias_target (tree *alias)
1292 tree target = *alias;
1294 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1296 gcc_assert (TREE_CHAIN (target));
1297 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1298 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1299 && ! TREE_CHAIN (target));
1300 *alias = target;
1303 return target;
1306 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1307 have static storage duration. In other words, it should not be an
1308 automatic variable, including PARM_DECLs.
1310 There is, however, one exception: this function handles variables
1311 explicitly placed in a particular register by the user.
1313 This is never called for PARM_DECL nodes. */
1315 void
1316 make_decl_rtl (tree decl)
1318 const char *name = 0;
1319 int reg_number;
1320 tree id;
1321 rtx x;
1323 /* Check that we are not being given an automatic variable. */
1324 gcc_assert (TREE_CODE (decl) != PARM_DECL
1325 && TREE_CODE (decl) != RESULT_DECL);
1327 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1328 gcc_assert (TREE_CODE (decl) != VAR_DECL
1329 || TREE_STATIC (decl)
1330 || TREE_PUBLIC (decl)
1331 || DECL_EXTERNAL (decl)
1332 || DECL_REGISTER (decl));
1334 /* And that we were not given a type or a label. */
1335 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1336 && TREE_CODE (decl) != LABEL_DECL);
1338 /* For a duplicate declaration, we can be called twice on the
1339 same DECL node. Don't discard the RTL already made. */
1340 if (DECL_RTL_SET_P (decl))
1342 /* If the old RTL had the wrong mode, fix the mode. */
1343 x = DECL_RTL (decl);
1344 if (GET_MODE (x) != DECL_MODE (decl))
1345 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1347 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1348 return;
1350 /* ??? Another way to do this would be to maintain a hashed
1351 table of such critters. Instead of adding stuff to a DECL
1352 to give certain attributes to it, we could use an external
1353 hash map from DECL to set of attributes. */
1355 /* Let the target reassign the RTL if it wants.
1356 This is necessary, for example, when one machine specific
1357 decl attribute overrides another. */
1358 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1360 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1361 on the new decl information. */
1362 if (MEM_P (x)
1363 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1364 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1365 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1367 return;
1370 /* If this variable belongs to the global constant pool, retrieve the
1371 pre-computed RTL or recompute it in LTO mode. */
1372 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1374 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1375 return;
1378 id = DECL_ASSEMBLER_NAME (decl);
1379 if (TREE_CODE (decl) == FUNCTION_DECL
1380 && cgraph_node::get (decl)
1381 && cgraph_node::get (decl)->instrumentation_clone)
1382 ultimate_transparent_alias_target (&id);
1383 name = IDENTIFIER_POINTER (id);
1385 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1386 && DECL_REGISTER (decl))
1388 error ("register name not specified for %q+D", decl);
1390 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1392 const char *asmspec = name+1;
1393 machine_mode mode = DECL_MODE (decl);
1394 reg_number = decode_reg_name (asmspec);
1395 /* First detect errors in declaring global registers. */
1396 if (reg_number == -1)
1397 error ("register name not specified for %q+D", decl);
1398 else if (reg_number < 0)
1399 error ("invalid register name for %q+D", decl);
1400 else if (mode == BLKmode)
1401 error ("data type of %q+D isn%'t suitable for a register",
1402 decl);
1403 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1404 error ("the register specified for %q+D cannot be accessed"
1405 " by the current target", decl);
1406 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1407 error ("the register specified for %q+D is not general enough"
1408 " to be used as a register variable", decl);
1409 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1410 error ("register specified for %q+D isn%'t suitable for data type",
1411 decl);
1412 /* Now handle properly declared static register variables. */
1413 else
1415 int nregs;
1417 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1419 DECL_INITIAL (decl) = 0;
1420 error ("global register variable has initial value");
1422 if (TREE_THIS_VOLATILE (decl))
1423 warning (OPT_Wvolatile_register_var,
1424 "optimization may eliminate reads and/or "
1425 "writes to register variables");
1427 /* If the user specified one of the eliminables registers here,
1428 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1429 confused with that register and be eliminated. This usage is
1430 somewhat suspect... */
1432 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1433 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1434 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1436 if (TREE_STATIC (decl))
1438 /* Make this register global, so not usable for anything
1439 else. */
1440 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1441 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1442 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1443 #endif
1444 nregs = hard_regno_nregs[reg_number][mode];
1445 while (nregs > 0)
1446 globalize_reg (decl, reg_number + --nregs);
1449 /* As a register variable, it has no section. */
1450 return;
1452 /* Avoid internal errors from invalid register
1453 specifications. */
1454 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1455 DECL_HARD_REGISTER (decl) = 0;
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 (TREE_CODE (decl) == VAR_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 (TREE_CODE (decl) == VAR_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 char buf[16];
1579 /* ??? This only works reliably with the GNU linker. */
1580 sprintf (buf, "%s.%.5u",
1581 constructor_p ? ".ctors" : ".dtors",
1582 /* Invert the numbering so the linker puts us in the proper
1583 order; constructors are run from right to left, and the
1584 linker sorts in increasing order. */
1585 MAX_INIT_PRIORITY - priority);
1586 return get_section (buf, SECTION_WRITE, NULL);
1589 void
1590 default_named_section_asm_out_destructor (rtx symbol, int priority)
1592 section *sec;
1594 if (priority != DEFAULT_INIT_PRIORITY)
1595 sec = get_cdtor_priority_section (priority,
1596 /*constructor_p=*/false);
1597 else
1598 sec = get_section (".dtors", SECTION_WRITE, NULL);
1600 assemble_addr_to_section (symbol, sec);
1603 #ifdef DTORS_SECTION_ASM_OP
1604 void
1605 default_dtor_section_asm_out_destructor (rtx symbol,
1606 int priority ATTRIBUTE_UNUSED)
1608 assemble_addr_to_section (symbol, dtors_section);
1610 #endif
1612 void
1613 default_named_section_asm_out_constructor (rtx symbol, int priority)
1615 section *sec;
1617 if (priority != DEFAULT_INIT_PRIORITY)
1618 sec = get_cdtor_priority_section (priority,
1619 /*constructor_p=*/true);
1620 else
1621 sec = get_section (".ctors", SECTION_WRITE, NULL);
1623 assemble_addr_to_section (symbol, sec);
1626 #ifdef CTORS_SECTION_ASM_OP
1627 void
1628 default_ctor_section_asm_out_constructor (rtx symbol,
1629 int priority ATTRIBUTE_UNUSED)
1631 assemble_addr_to_section (symbol, ctors_section);
1633 #endif
1635 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1636 a nonzero value if the constant pool should be output before the
1637 start of the function, or a zero value if the pool should output
1638 after the end of the function. The default is to put it before the
1639 start. */
1641 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1642 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1643 #endif
1645 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1646 to be output to assembler.
1647 Set first_global_object_name and weak_global_object_name as appropriate. */
1649 void
1650 notice_global_symbol (tree decl)
1652 const char **t = &first_global_object_name;
1654 if (first_global_object_name
1655 || !TREE_PUBLIC (decl)
1656 || DECL_EXTERNAL (decl)
1657 || !DECL_NAME (decl)
1658 || (TREE_CODE (decl) == VAR_DECL && DECL_HARD_REGISTER (decl))
1659 || (TREE_CODE (decl) != FUNCTION_DECL
1660 && (TREE_CODE (decl) != VAR_DECL
1661 || (DECL_COMMON (decl)
1662 && (DECL_INITIAL (decl) == 0
1663 || DECL_INITIAL (decl) == error_mark_node)))))
1664 return;
1666 /* We win when global object is found, but it is useful to know about weak
1667 symbol as well so we can produce nicer unique names. */
1668 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1669 t = &weak_global_object_name;
1671 if (!*t)
1673 tree id = DECL_ASSEMBLER_NAME (decl);
1674 ultimate_transparent_alias_target (&id);
1675 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1679 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1680 current function goes into the cold section, so that targets can use
1681 current_function_section during RTL expansion. DECL describes the
1682 function. */
1684 void
1685 decide_function_section (tree decl)
1687 first_function_block_is_cold = false;
1689 if (flag_reorder_blocks_and_partition)
1690 /* We will decide in assemble_start_function. */
1691 return;
1693 if (DECL_SECTION_NAME (decl))
1695 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1696 /* Calls to function_section rely on first_function_block_is_cold
1697 being accurate. */
1698 first_function_block_is_cold = (node
1699 && node->frequency
1700 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1703 in_cold_section_p = first_function_block_is_cold;
1706 /* Get the function's name, as described by its RTL. This may be
1707 different from the DECL_NAME name used in the source file. */
1708 const char *
1709 get_fnname_from_decl (tree decl)
1711 rtx x = DECL_RTL (decl);
1712 gcc_assert (MEM_P (x));
1713 x = XEXP (x, 0);
1714 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1715 return XSTR (x, 0);
1718 /* Output assembler code for the constant pool of a function and associated
1719 with defining the name of the function. DECL describes the function.
1720 NAME is the function's name. For the constant pool, we use the current
1721 constant pool data. */
1723 void
1724 assemble_start_function (tree decl, const char *fnname)
1726 int align;
1727 char tmp_label[100];
1728 bool hot_label_written = false;
1730 if (flag_reorder_blocks_and_partition)
1732 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1733 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1734 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1735 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1736 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1737 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1738 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1739 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1740 const_labelno++;
1741 cold_function_name = NULL_TREE;
1743 else
1745 crtl->subsections.hot_section_label = NULL;
1746 crtl->subsections.cold_section_label = NULL;
1747 crtl->subsections.hot_section_end_label = NULL;
1748 crtl->subsections.cold_section_end_label = NULL;
1751 /* The following code does not need preprocessing in the assembler. */
1753 app_disable ();
1755 if (CONSTANT_POOL_BEFORE_FUNCTION)
1756 output_constant_pool (fnname, decl);
1758 align = symtab_node::get (decl)->definition_alignment ();
1760 /* Make sure the not and cold text (code) sections are properly
1761 aligned. This is necessary here in the case where the function
1762 has both hot and cold sections, because we don't want to re-set
1763 the alignment when the section switch happens mid-function. */
1765 if (flag_reorder_blocks_and_partition)
1767 first_function_block_is_cold = false;
1769 switch_to_section (unlikely_text_section ());
1770 assemble_align (align);
1771 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1773 /* When the function starts with a cold section, we need to explicitly
1774 align the hot section and write out the hot section label.
1775 But if the current function is a thunk, we do not have a CFG. */
1776 if (!cfun->is_thunk
1777 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1779 switch_to_section (text_section);
1780 assemble_align (align);
1781 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1782 hot_label_written = true;
1783 first_function_block_is_cold = true;
1785 in_cold_section_p = first_function_block_is_cold;
1789 /* Switch to the correct text section for the start of the function. */
1791 switch_to_section (function_section (decl));
1792 if (flag_reorder_blocks_and_partition
1793 && !hot_label_written)
1794 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1796 /* Tell assembler to move to target machine's alignment for functions. */
1797 align = floor_log2 (align / BITS_PER_UNIT);
1798 if (align > 0)
1800 ASM_OUTPUT_ALIGN (asm_out_file, align);
1803 /* Handle a user-specified function alignment.
1804 Note that we still need to align to DECL_ALIGN, as above,
1805 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1806 if (! DECL_USER_ALIGN (decl)
1807 && align_functions_log > align
1808 && optimize_function_for_speed_p (cfun))
1810 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1811 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1812 align_functions_log, align_functions - 1);
1813 #else
1814 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1815 #endif
1818 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1819 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1820 #endif
1822 if (!DECL_IGNORED_P (decl))
1823 (*debug_hooks->begin_function) (decl);
1825 /* Make function name accessible from other files, if appropriate. */
1827 if (TREE_PUBLIC (decl)
1828 || (cgraph_node::get (decl)->instrumentation_clone
1829 && cgraph_node::get (decl)->instrumented_version
1830 && TREE_PUBLIC (cgraph_node::get (decl)->instrumented_version->decl)))
1832 notice_global_symbol (decl);
1834 globalize_decl (decl);
1836 maybe_assemble_visibility (decl);
1839 if (DECL_PRESERVE_P (decl))
1840 targetm.asm_out.mark_decl_preserved (fnname);
1842 /* Do any machine/system dependent processing of the function name. */
1843 #ifdef ASM_DECLARE_FUNCTION_NAME
1844 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1845 #else
1846 /* Standard thing is just output label for the function. */
1847 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1848 #endif /* ASM_DECLARE_FUNCTION_NAME */
1850 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1851 saw_no_split_stack = true;
1854 /* Output assembler code associated with defining the size of the
1855 function. DECL describes the function. NAME is the function's name. */
1857 void
1858 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1860 #ifdef ASM_DECLARE_FUNCTION_SIZE
1861 /* We could have switched section in the middle of the function. */
1862 if (flag_reorder_blocks_and_partition)
1863 switch_to_section (function_section (decl));
1864 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1865 #endif
1866 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1868 output_constant_pool (fnname, decl);
1869 switch_to_section (function_section (decl)); /* need to switch back */
1871 /* Output labels for end of hot/cold text sections (to be used by
1872 debug info.) */
1873 if (flag_reorder_blocks_and_partition)
1875 section *save_text_section;
1877 save_text_section = in_section;
1878 switch_to_section (unlikely_text_section ());
1879 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
1880 if (cold_function_name != NULL_TREE)
1881 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
1882 IDENTIFIER_POINTER (cold_function_name),
1883 decl);
1884 #endif
1885 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1886 if (first_function_block_is_cold)
1887 switch_to_section (text_section);
1888 else
1889 switch_to_section (function_section (decl));
1890 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1891 switch_to_section (save_text_section);
1895 /* Assemble code to leave SIZE bytes of zeros. */
1897 void
1898 assemble_zeros (unsigned HOST_WIDE_INT size)
1900 /* Do no output if -fsyntax-only. */
1901 if (flag_syntax_only)
1902 return;
1904 #ifdef ASM_NO_SKIP_IN_TEXT
1905 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1906 so we must output 0s explicitly in the text section. */
1907 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1909 unsigned HOST_WIDE_INT i;
1910 for (i = 0; i < size; i++)
1911 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1913 else
1914 #endif
1915 if (size > 0)
1916 ASM_OUTPUT_SKIP (asm_out_file, size);
1919 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1921 void
1922 assemble_align (int align)
1924 if (align > BITS_PER_UNIT)
1926 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1930 /* Assemble a string constant with the specified C string as contents. */
1932 void
1933 assemble_string (const char *p, int size)
1935 int pos = 0;
1936 int maximum = 2000;
1938 /* If the string is very long, split it up. */
1940 while (pos < size)
1942 int thissize = size - pos;
1943 if (thissize > maximum)
1944 thissize = maximum;
1946 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1948 pos += thissize;
1949 p += thissize;
1954 /* A noswitch_section_callback for lcomm_section. */
1956 static bool
1957 emit_local (tree decl ATTRIBUTE_UNUSED,
1958 const char *name ATTRIBUTE_UNUSED,
1959 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1960 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1962 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1963 int align = symtab_node::get (decl)->definition_alignment ();
1964 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1965 size, align);
1966 return true;
1967 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1968 int align = symtab_node::get (decl)->definition_alignment ();
1969 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
1970 return true;
1971 #else
1972 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1973 return false;
1974 #endif
1977 /* A noswitch_section_callback for bss_noswitch_section. */
1979 #if defined ASM_OUTPUT_ALIGNED_BSS
1980 static bool
1981 emit_bss (tree decl ATTRIBUTE_UNUSED,
1982 const char *name ATTRIBUTE_UNUSED,
1983 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1984 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1986 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1987 get_variable_align (decl));
1988 return true;
1990 #endif
1992 /* A noswitch_section_callback for comm_section. */
1994 static bool
1995 emit_common (tree decl ATTRIBUTE_UNUSED,
1996 const char *name ATTRIBUTE_UNUSED,
1997 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1998 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2000 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2001 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2002 size, get_variable_align (decl));
2003 return true;
2004 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2005 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2006 get_variable_align (decl));
2007 return true;
2008 #else
2009 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2010 return false;
2011 #endif
2014 /* A noswitch_section_callback for tls_comm_section. */
2016 static bool
2017 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2018 const char *name ATTRIBUTE_UNUSED,
2019 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2020 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2022 #ifdef ASM_OUTPUT_TLS_COMMON
2023 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2024 return true;
2025 #else
2026 sorry ("thread-local COMMON data not implemented");
2027 return true;
2028 #endif
2031 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2032 NAME is the name of DECL's SYMBOL_REF. */
2034 static void
2035 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2036 unsigned int align)
2038 unsigned HOST_WIDE_INT size, rounded;
2040 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2041 rounded = size;
2043 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2044 size += asan_red_zone_size (size);
2046 /* Don't allocate zero bytes of common,
2047 since that means "undefined external" in the linker. */
2048 if (size == 0)
2049 rounded = 1;
2051 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2052 so that each uninitialized object starts on such a boundary. */
2053 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2054 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2055 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2057 if (!sect->noswitch.callback (decl, name, size, rounded)
2058 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2059 error ("requested alignment for %q+D is greater than "
2060 "implemented alignment of %wu", decl, rounded);
2063 /* A subroutine of assemble_variable. Output the label and contents of
2064 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2065 is as for assemble_variable. */
2067 static void
2068 assemble_variable_contents (tree decl, const char *name,
2069 bool dont_output_data)
2071 /* Do any machine/system dependent processing of the object. */
2072 #ifdef ASM_DECLARE_OBJECT_NAME
2073 last_assemble_variable_decl = decl;
2074 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2075 #else
2076 /* Standard thing is just output label for the object. */
2077 ASM_OUTPUT_LABEL (asm_out_file, name);
2078 #endif /* ASM_DECLARE_OBJECT_NAME */
2080 if (!dont_output_data)
2082 /* Caller is supposed to use varpool_get_constructor when it wants
2083 to output the body. */
2084 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2085 if (DECL_INITIAL (decl)
2086 && DECL_INITIAL (decl) != error_mark_node
2087 && !initializer_zerop (DECL_INITIAL (decl)))
2088 /* Output the actual data. */
2089 output_constant (DECL_INITIAL (decl),
2090 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2091 get_variable_align (decl));
2092 else
2093 /* Leave space for it. */
2094 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2095 targetm.asm_out.decl_end ();
2099 /* Write out assembly for the variable DECL, which is not defined in
2100 the current translation unit. */
2101 void
2102 assemble_undefined_decl (tree decl)
2104 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2105 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2108 /* Assemble everything that is needed for a variable or function declaration.
2109 Not used for automatic variables, and not used for function definitions.
2110 Should not be called for variables of incomplete structure type.
2112 TOP_LEVEL is nonzero if this variable has file scope.
2113 AT_END is nonzero if this is the special handling, at end of compilation,
2114 to define things that have had only tentative definitions.
2115 DONT_OUTPUT_DATA if nonzero means don't actually output the
2116 initial value (that will be done by the caller). */
2118 void
2119 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2120 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2122 const char *name;
2123 rtx decl_rtl, symbol;
2124 section *sect;
2125 unsigned int align;
2126 bool asan_protected = false;
2128 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2129 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2131 /* Emulated TLS had better not get this far. */
2132 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2134 last_assemble_variable_decl = 0;
2136 /* Normally no need to say anything here for external references,
2137 since assemble_external is called by the language-specific code
2138 when a declaration is first seen. */
2140 if (DECL_EXTERNAL (decl))
2141 return;
2143 /* Do nothing for global register variables. */
2144 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2146 TREE_ASM_WRITTEN (decl) = 1;
2147 return;
2150 /* If type was incomplete when the variable was declared,
2151 see if it is complete now. */
2153 if (DECL_SIZE (decl) == 0)
2154 layout_decl (decl, 0);
2156 /* Still incomplete => don't allocate it; treat the tentative defn
2157 (which is what it must have been) as an `extern' reference. */
2159 if (!dont_output_data && DECL_SIZE (decl) == 0)
2161 error ("storage size of %q+D isn%'t known", decl);
2162 TREE_ASM_WRITTEN (decl) = 1;
2163 return;
2166 /* The first declaration of a variable that comes through this function
2167 decides whether it is global (in C, has external linkage)
2168 or local (in C, has internal linkage). So do nothing more
2169 if this function has already run. */
2171 if (TREE_ASM_WRITTEN (decl))
2172 return;
2174 /* Make sure targetm.encode_section_info is invoked before we set
2175 ASM_WRITTEN. */
2176 decl_rtl = DECL_RTL (decl);
2178 TREE_ASM_WRITTEN (decl) = 1;
2180 /* Do no output if -fsyntax-only. */
2181 if (flag_syntax_only)
2182 return;
2184 if (! dont_output_data
2185 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2187 error ("size of variable %q+D is too large", decl);
2188 return;
2191 gcc_assert (MEM_P (decl_rtl));
2192 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2193 symbol = XEXP (decl_rtl, 0);
2195 /* If this symbol belongs to the tree constant pool, output the constant
2196 if it hasn't already been written. */
2197 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2199 tree decl = SYMBOL_REF_DECL (symbol);
2200 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2201 output_constant_def_contents (symbol);
2202 return;
2205 app_disable ();
2207 name = XSTR (symbol, 0);
2208 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2209 notice_global_symbol (decl);
2211 /* Compute the alignment of this data. */
2213 align_variable (decl, dont_output_data);
2215 if ((flag_sanitize & SANITIZE_ADDRESS)
2216 && asan_protect_global (decl))
2218 asan_protected = true;
2219 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2220 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2223 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2225 align = get_variable_align (decl);
2227 if (TREE_PUBLIC (decl))
2228 maybe_assemble_visibility (decl);
2230 if (DECL_PRESERVE_P (decl))
2231 targetm.asm_out.mark_decl_preserved (name);
2233 /* First make the assembler name(s) global if appropriate. */
2234 sect = get_variable_section (decl, false);
2235 if (TREE_PUBLIC (decl)
2236 && (sect->common.flags & SECTION_COMMON) == 0)
2237 globalize_decl (decl);
2239 /* Output any data that we will need to use the address of. */
2240 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2241 output_addressed_constants (DECL_INITIAL (decl));
2243 /* dbxout.c needs to know this. */
2244 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2245 DECL_IN_TEXT_SECTION (decl) = 1;
2247 /* If the decl is part of an object_block, make sure that the decl
2248 has been positioned within its block, but do not write out its
2249 definition yet. output_object_blocks will do that later. */
2250 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2252 gcc_assert (!dont_output_data);
2253 place_block_symbol (symbol);
2255 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2256 assemble_noswitch_variable (decl, name, sect, align);
2257 else
2259 /* The following bit of code ensures that vtable_map
2260 variables are not only in the comdat section, but that
2261 each variable has its own unique comdat name. If this
2262 code is removed, the variables end up in the same section
2263 with a single comdat name.
2265 FIXME: resolve_unique_section needs to deal better with
2266 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2267 that is fixed, this if-else statement can be replaced with
2268 a single call to "switch_to_section (sect)". */
2269 if (sect->named.name
2270 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2272 #if defined (OBJECT_FORMAT_ELF)
2273 targetm.asm_out.named_section (sect->named.name,
2274 sect->named.common.flags
2275 | SECTION_LINKONCE,
2276 DECL_NAME (decl));
2277 in_section = sect;
2278 #elif defined (TARGET_PECOFF)
2279 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
2280 Therefore the following check is used.
2281 In case a the target is PE or COFF a comdat group section
2282 is created, e.g. .vtable_map_vars$foo. The linker places
2283 everything in .vtable_map_vars at the end.
2285 A fix could be made in
2286 gcc/config/i386/winnt.c: i386_pe_unique_section. */
2287 if (TARGET_PECOFF)
2289 char *name;
2291 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
2292 name = ACONCAT ((sect->named.name, "$",
2293 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
2294 else
2295 name = ACONCAT ((sect->named.name, "$",
2296 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
2297 NULL));
2299 targetm.asm_out.named_section (name,
2300 sect->named.common.flags
2301 | SECTION_LINKONCE,
2302 DECL_NAME (decl));
2303 in_section = sect;
2305 #else
2306 switch_to_section (sect);
2307 #endif
2309 else
2310 switch_to_section (sect);
2311 if (align > BITS_PER_UNIT)
2312 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2313 assemble_variable_contents (decl, name, dont_output_data);
2314 if (asan_protected)
2316 unsigned HOST_WIDE_INT int size
2317 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2318 assemble_zeros (asan_red_zone_size (size));
2324 /* Given a function declaration (FN_DECL), this function assembles the
2325 function into the .preinit_array section. */
2327 void
2328 assemble_vtv_preinit_initializer (tree fn_decl)
2330 section *sect;
2331 unsigned flags = SECTION_WRITE;
2332 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2334 flags |= SECTION_NOTYPE;
2335 sect = get_section (".preinit_array", flags, fn_decl);
2336 switch_to_section (sect);
2337 assemble_addr_to_section (symbol, sect);
2340 /* Return 1 if type TYPE contains any pointers. */
2342 static int
2343 contains_pointers_p (tree type)
2345 switch (TREE_CODE (type))
2347 case POINTER_TYPE:
2348 case REFERENCE_TYPE:
2349 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2350 so I'll play safe and return 1. */
2351 case OFFSET_TYPE:
2352 return 1;
2354 case RECORD_TYPE:
2355 case UNION_TYPE:
2356 case QUAL_UNION_TYPE:
2358 tree fields;
2359 /* For a type that has fields, see if the fields have pointers. */
2360 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2361 if (TREE_CODE (fields) == FIELD_DECL
2362 && contains_pointers_p (TREE_TYPE (fields)))
2363 return 1;
2364 return 0;
2367 case ARRAY_TYPE:
2368 /* An array type contains pointers if its element type does. */
2369 return contains_pointers_p (TREE_TYPE (type));
2371 default:
2372 return 0;
2376 /* We delay assemble_external processing until
2377 the compilation unit is finalized. This is the best we can do for
2378 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2379 it all the way to final. See PR 17982 for further discussion. */
2380 static GTY(()) tree pending_assemble_externals;
2382 #ifdef ASM_OUTPUT_EXTERNAL
2383 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2384 As a result, assemble_external can be called after the list of externals
2385 is processed and the pointer set destroyed. */
2386 static bool pending_assemble_externals_processed;
2388 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2389 TREE_LIST in assemble_external. */
2390 static hash_set<tree> *pending_assemble_externals_set;
2392 /* True if DECL is a function decl for which no out-of-line copy exists.
2393 It is assumed that DECL's assembler name has been set. */
2395 static bool
2396 incorporeal_function_p (tree decl)
2398 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2400 const char *name;
2402 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2403 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2404 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2405 return true;
2407 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2408 /* Atomic or sync builtins which have survived this far will be
2409 resolved externally and therefore are not incorporeal. */
2410 if (strncmp (name, "__builtin_", 10) == 0)
2411 return true;
2413 return false;
2416 /* Actually do the tests to determine if this is necessary, and invoke
2417 ASM_OUTPUT_EXTERNAL. */
2418 static void
2419 assemble_external_real (tree decl)
2421 rtx rtl = DECL_RTL (decl);
2423 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2424 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2425 && !incorporeal_function_p (decl))
2427 /* Some systems do require some output. */
2428 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2429 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2432 #endif
2434 void
2435 process_pending_assemble_externals (void)
2437 #ifdef ASM_OUTPUT_EXTERNAL
2438 tree list;
2439 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2440 assemble_external_real (TREE_VALUE (list));
2442 pending_assemble_externals = 0;
2443 pending_assemble_externals_processed = true;
2444 delete pending_assemble_externals_set;
2445 #endif
2448 /* This TREE_LIST contains any weak symbol declarations waiting
2449 to be emitted. */
2450 static GTY(()) tree weak_decls;
2452 /* Output something to declare an external symbol to the assembler,
2453 and qualifiers such as weakness. (Most assemblers don't need
2454 extern declaration, so we normally output nothing.) Do nothing if
2455 DECL is not external. */
2457 void
2458 assemble_external (tree decl ATTRIBUTE_UNUSED)
2460 /* Make sure that the ASM_OUT_FILE is open.
2461 If it's not, we should not be calling this function. */
2462 gcc_assert (asm_out_file);
2464 /* In a perfect world, the following condition would be true.
2465 Sadly, the Java and Go front ends emit assembly *from the front end*,
2466 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2467 #if 0
2468 /* This function should only be called if we are expanding, or have
2469 expanded, to RTL.
2470 Ideally, only final.c would be calling this function, but it is
2471 not clear whether that would break things somehow. See PR 17982
2472 for further discussion. */
2473 gcc_assert (state == EXPANSION
2474 || state == FINISHED);
2475 #endif
2477 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2478 return;
2480 /* We want to output annotation for weak and external symbols at
2481 very last to check if they are references or not. */
2483 if (TARGET_SUPPORTS_WEAK
2484 && DECL_WEAK (decl)
2485 /* TREE_STATIC is a weird and abused creature which is not
2486 generally the right test for whether an entity has been
2487 locally emitted, inlined or otherwise not-really-extern, but
2488 for declarations that can be weak, it happens to be
2489 match. */
2490 && !TREE_STATIC (decl)
2491 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2492 && value_member (decl, weak_decls) == NULL_TREE)
2493 weak_decls = tree_cons (NULL, decl, weak_decls);
2495 #ifdef ASM_OUTPUT_EXTERNAL
2496 if (pending_assemble_externals_processed)
2498 assemble_external_real (decl);
2499 return;
2502 if (! pending_assemble_externals_set->add (decl))
2503 pending_assemble_externals = tree_cons (NULL, decl,
2504 pending_assemble_externals);
2505 #endif
2508 /* Similar, for calling a library function FUN. */
2510 void
2511 assemble_external_libcall (rtx fun)
2513 /* Declare library function name external when first used, if nec. */
2514 if (! SYMBOL_REF_USED (fun))
2516 SYMBOL_REF_USED (fun) = 1;
2517 targetm.asm_out.external_libcall (fun);
2521 /* Assemble a label named NAME. */
2523 void
2524 assemble_label (FILE *file, const char *name)
2526 ASM_OUTPUT_LABEL (file, name);
2529 /* Set the symbol_referenced flag for ID. */
2530 void
2531 mark_referenced (tree id)
2533 TREE_SYMBOL_REFERENCED (id) = 1;
2536 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2537 void
2538 mark_decl_referenced (tree decl)
2540 if (TREE_CODE (decl) == FUNCTION_DECL)
2542 /* Extern inline functions don't become needed when referenced.
2543 If we know a method will be emitted in other TU and no new
2544 functions can be marked reachable, just use the external
2545 definition. */
2546 struct cgraph_node *node = cgraph_node::get_create (decl);
2547 if (!DECL_EXTERNAL (decl)
2548 && !node->definition)
2549 node->mark_force_output ();
2551 else if (TREE_CODE (decl) == VAR_DECL)
2553 varpool_node *node = varpool_node::get_create (decl);
2554 /* C++ frontend use mark_decl_references to force COMDAT variables
2555 to be output that might appear dead otherwise. */
2556 node->force_output = true;
2558 /* else do nothing - we can get various sorts of CST nodes here,
2559 which do not need to be marked. */
2563 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2564 starts with a *, the rest of NAME is output verbatim. Otherwise
2565 NAME is transformed in a target-specific way (usually by the
2566 addition of an underscore). */
2568 void
2569 assemble_name_raw (FILE *file, const char *name)
2571 if (name[0] == '*')
2572 fputs (&name[1], file);
2573 else
2574 ASM_OUTPUT_LABELREF (file, name);
2577 /* Like assemble_name_raw, but should be used when NAME might refer to
2578 an entity that is also represented as a tree (like a function or
2579 variable). If NAME does refer to such an entity, that entity will
2580 be marked as referenced. */
2582 void
2583 assemble_name (FILE *file, const char *name)
2585 const char *real_name;
2586 tree id;
2588 real_name = targetm.strip_name_encoding (name);
2590 id = maybe_get_identifier (real_name);
2591 if (id)
2593 tree id_orig = id;
2595 mark_referenced (id);
2596 ultimate_transparent_alias_target (&id);
2597 if (id != id_orig)
2598 name = IDENTIFIER_POINTER (id);
2599 gcc_assert (! TREE_CHAIN (id));
2602 assemble_name_raw (file, name);
2605 /* Allocate SIZE bytes writable static space with a gensym name
2606 and return an RTX to refer to its address. */
2609 assemble_static_space (unsigned HOST_WIDE_INT size)
2611 char name[12];
2612 const char *namestring;
2613 rtx x;
2615 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2616 ++const_labelno;
2617 namestring = ggc_strdup (name);
2619 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2620 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2622 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2623 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2624 BIGGEST_ALIGNMENT);
2625 #else
2626 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2627 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2628 #else
2630 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2631 so that each uninitialized object starts on such a boundary. */
2632 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2633 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2634 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2635 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2636 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2637 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2639 #endif
2640 #endif
2641 return x;
2644 /* Assemble the static constant template for function entry trampolines.
2645 This is done at most once per compilation.
2646 Returns an RTX for the address of the template. */
2648 static GTY(()) rtx initial_trampoline;
2651 assemble_trampoline_template (void)
2653 char label[256];
2654 const char *name;
2655 int align;
2656 rtx symbol;
2658 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2660 if (initial_trampoline)
2661 return initial_trampoline;
2663 /* By default, put trampoline templates in read-only data section. */
2665 #ifdef TRAMPOLINE_SECTION
2666 switch_to_section (TRAMPOLINE_SECTION);
2667 #else
2668 switch_to_section (readonly_data_section);
2669 #endif
2671 /* Write the assembler code to define one. */
2672 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2673 if (align > 0)
2674 ASM_OUTPUT_ALIGN (asm_out_file, align);
2676 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2677 targetm.asm_out.trampoline_template (asm_out_file);
2679 /* Record the rtl to refer to it. */
2680 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2681 name = ggc_strdup (label);
2682 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2683 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2685 initial_trampoline = gen_const_mem (BLKmode, symbol);
2686 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2687 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2689 return initial_trampoline;
2692 /* A and B are either alignments or offsets. Return the minimum alignment
2693 that may be assumed after adding the two together. */
2695 static inline unsigned
2696 min_align (unsigned int a, unsigned int b)
2698 return (a | b) & -(a | b);
2701 /* Return the assembler directive for creating a given kind of integer
2702 object. SIZE is the number of bytes in the object and ALIGNED_P
2703 indicates whether it is known to be aligned. Return NULL if the
2704 assembly dialect has no such directive.
2706 The returned string should be printed at the start of a new line and
2707 be followed immediately by the object's initial value. */
2709 const char *
2710 integer_asm_op (int size, int aligned_p)
2712 struct asm_int_op *ops;
2714 if (aligned_p)
2715 ops = &targetm.asm_out.aligned_op;
2716 else
2717 ops = &targetm.asm_out.unaligned_op;
2719 switch (size)
2721 case 1:
2722 return targetm.asm_out.byte_op;
2723 case 2:
2724 return ops->hi;
2725 case 4:
2726 return ops->si;
2727 case 8:
2728 return ops->di;
2729 case 16:
2730 return ops->ti;
2731 default:
2732 return NULL;
2736 /* Use directive OP to assemble an integer object X. Print OP at the
2737 start of the line, followed immediately by the value of X. */
2739 void
2740 assemble_integer_with_op (const char *op, rtx x)
2742 fputs (op, asm_out_file);
2743 output_addr_const (asm_out_file, x);
2744 fputc ('\n', asm_out_file);
2747 /* The default implementation of the asm_out.integer target hook. */
2749 bool
2750 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2751 unsigned int size ATTRIBUTE_UNUSED,
2752 int aligned_p ATTRIBUTE_UNUSED)
2754 const char *op = integer_asm_op (size, aligned_p);
2755 /* Avoid GAS bugs for large values. Specifically negative values whose
2756 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2757 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2758 return false;
2759 return op && (assemble_integer_with_op (op, x), true);
2762 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2763 the alignment of the integer in bits. Return 1 if we were able to output
2764 the constant, otherwise 0. We must be able to output the constant,
2765 if FORCE is nonzero. */
2767 bool
2768 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2770 int aligned_p;
2772 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2774 /* See if the target hook can handle this kind of object. */
2775 if (targetm.asm_out.integer (x, size, aligned_p))
2776 return true;
2778 /* If the object is a multi-byte one, try splitting it up. Split
2779 it into words it if is multi-word, otherwise split it into bytes. */
2780 if (size > 1)
2782 machine_mode omode, imode;
2783 unsigned int subalign;
2784 unsigned int subsize, i;
2785 enum mode_class mclass;
2787 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2788 subalign = MIN (align, subsize * BITS_PER_UNIT);
2789 if (GET_CODE (x) == CONST_FIXED)
2790 mclass = GET_MODE_CLASS (GET_MODE (x));
2791 else
2792 mclass = MODE_INT;
2794 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2795 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2797 for (i = 0; i < size; i += subsize)
2799 rtx partial = simplify_subreg (omode, x, imode, i);
2800 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2801 break;
2803 if (i == size)
2804 return true;
2806 /* If we've printed some of it, but not all of it, there's no going
2807 back now. */
2808 gcc_assert (!i);
2811 gcc_assert (!force);
2813 return false;
2816 void
2817 assemble_real (REAL_VALUE_TYPE d, machine_mode mode, unsigned int align)
2819 long data[4] = {0, 0, 0, 0};
2820 int i;
2821 int bitsize, nelts, nunits, units_per;
2823 /* This is hairy. We have a quantity of known size. real_to_target
2824 will put it into an array of *host* longs, 32 bits per element
2825 (even if long is more than 32 bits). We need to determine the
2826 number of array elements that are occupied (nelts) and the number
2827 of *target* min-addressable units that will be occupied in the
2828 object file (nunits). We cannot assume that 32 divides the
2829 mode's bitsize (size * BITS_PER_UNIT) evenly.
2831 size * BITS_PER_UNIT is used here to make sure that padding bits
2832 (which might appear at either end of the value; real_to_target
2833 will include the padding bits in its output array) are included. */
2835 nunits = GET_MODE_SIZE (mode);
2836 bitsize = nunits * BITS_PER_UNIT;
2837 nelts = CEIL (bitsize, 32);
2838 units_per = 32 / BITS_PER_UNIT;
2840 real_to_target (data, &d, mode);
2842 /* Put out the first word with the specified alignment. */
2843 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2844 nunits -= units_per;
2846 /* Subsequent words need only 32-bit alignment. */
2847 align = min_align (align, 32);
2849 for (i = 1; i < nelts; i++)
2851 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2852 nunits -= units_per;
2856 /* Given an expression EXP with a constant value,
2857 reduce it to the sum of an assembler symbol and an integer.
2858 Store them both in the structure *VALUE.
2859 EXP must be reducible. */
2861 struct addr_const {
2862 rtx base;
2863 HOST_WIDE_INT offset;
2866 static void
2867 decode_addr_const (tree exp, struct addr_const *value)
2869 tree target = TREE_OPERAND (exp, 0);
2870 int offset = 0;
2871 rtx x;
2873 while (1)
2875 if (TREE_CODE (target) == COMPONENT_REF
2876 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2878 offset += int_byte_position (TREE_OPERAND (target, 1));
2879 target = TREE_OPERAND (target, 0);
2881 else if (TREE_CODE (target) == ARRAY_REF
2882 || TREE_CODE (target) == ARRAY_RANGE_REF)
2884 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2885 * tree_to_shwi (TREE_OPERAND (target, 1)));
2886 target = TREE_OPERAND (target, 0);
2888 else if (TREE_CODE (target) == MEM_REF
2889 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2891 offset += mem_ref_offset (target).to_short_addr ();
2892 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2894 else if (TREE_CODE (target) == INDIRECT_REF
2895 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2896 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2897 == ADDR_EXPR)
2898 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2899 else
2900 break;
2903 switch (TREE_CODE (target))
2905 case VAR_DECL:
2906 case FUNCTION_DECL:
2907 x = DECL_RTL (target);
2908 break;
2910 case LABEL_DECL:
2911 x = gen_rtx_MEM (FUNCTION_MODE,
2912 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2913 break;
2915 case REAL_CST:
2916 case FIXED_CST:
2917 case STRING_CST:
2918 case COMPLEX_CST:
2919 case CONSTRUCTOR:
2920 case INTEGER_CST:
2921 x = output_constant_def (target, 1);
2922 break;
2924 default:
2925 gcc_unreachable ();
2928 gcc_assert (MEM_P (x));
2929 x = XEXP (x, 0);
2931 value->base = x;
2932 value->offset = offset;
2935 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2937 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2939 /* Constant pool accessor function. */
2941 hash_table<tree_descriptor_hasher> *
2942 constant_pool_htab (void)
2944 return const_desc_htab;
2947 /* Compute a hash code for a constant expression. */
2949 hashval_t
2950 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2952 return ptr->hash;
2955 static hashval_t
2956 const_hash_1 (const tree exp)
2958 const char *p;
2959 hashval_t hi;
2960 int len, i;
2961 enum tree_code code = TREE_CODE (exp);
2963 /* Either set P and LEN to the address and len of something to hash and
2964 exit the switch or return a value. */
2966 switch (code)
2968 case INTEGER_CST:
2969 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2970 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2971 break;
2973 case REAL_CST:
2974 return real_hash (TREE_REAL_CST_PTR (exp));
2976 case FIXED_CST:
2977 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2979 case STRING_CST:
2980 p = TREE_STRING_POINTER (exp);
2981 len = TREE_STRING_LENGTH (exp);
2982 break;
2984 case COMPLEX_CST:
2985 return (const_hash_1 (TREE_REALPART (exp)) * 5
2986 + const_hash_1 (TREE_IMAGPART (exp)));
2988 case VECTOR_CST:
2990 unsigned i;
2992 hi = 7 + VECTOR_CST_NELTS (exp);
2994 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2995 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2997 return hi;
3000 case CONSTRUCTOR:
3002 unsigned HOST_WIDE_INT idx;
3003 tree value;
3005 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3007 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3008 if (value)
3009 hi = hi * 603 + const_hash_1 (value);
3011 return hi;
3014 case ADDR_EXPR:
3015 case FDESC_EXPR:
3017 struct addr_const value;
3019 decode_addr_const (exp, &value);
3020 switch (GET_CODE (value.base))
3022 case SYMBOL_REF:
3023 /* Don't hash the address of the SYMBOL_REF;
3024 only use the offset and the symbol name. */
3025 hi = value.offset;
3026 p = XSTR (value.base, 0);
3027 for (i = 0; p[i] != 0; i++)
3028 hi = ((hi * 613) + (unsigned) (p[i]));
3029 break;
3031 case LABEL_REF:
3032 hi = (value.offset
3033 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
3034 break;
3036 default:
3037 gcc_unreachable ();
3040 return hi;
3042 case PLUS_EXPR:
3043 case POINTER_PLUS_EXPR:
3044 case MINUS_EXPR:
3045 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3046 + const_hash_1 (TREE_OPERAND (exp, 1)));
3048 CASE_CONVERT:
3049 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3051 default:
3052 /* A language specific constant. Just hash the code. */
3053 return code;
3056 /* Compute hashing function. */
3057 hi = len;
3058 for (i = 0; i < len; i++)
3059 hi = ((hi * 613) + (unsigned) (p[i]));
3061 return hi;
3064 /* Wrapper of compare_constant, for the htab interface. */
3065 bool
3066 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3067 constant_descriptor_tree *c2)
3069 if (c1->hash != c2->hash)
3070 return 0;
3071 return compare_constant (c1->value, c2->value);
3074 /* Compare t1 and t2, and return 1 only if they are known to result in
3075 the same bit pattern on output. */
3077 static int
3078 compare_constant (const tree t1, const tree t2)
3080 enum tree_code typecode;
3082 if (t1 == NULL_TREE)
3083 return t2 == NULL_TREE;
3084 if (t2 == NULL_TREE)
3085 return 0;
3087 if (TREE_CODE (t1) != TREE_CODE (t2))
3088 return 0;
3090 switch (TREE_CODE (t1))
3092 case INTEGER_CST:
3093 /* Integer constants are the same only if the same width of type. */
3094 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3095 return 0;
3096 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3097 return 0;
3098 return tree_int_cst_equal (t1, t2);
3100 case REAL_CST:
3101 /* Real constants are the same only if the same width of type. */
3102 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3103 return 0;
3105 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3107 case FIXED_CST:
3108 /* Fixed constants are the same only if the same width of type. */
3109 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3110 return 0;
3112 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3114 case STRING_CST:
3115 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3116 return 0;
3118 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3119 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3120 TREE_STRING_LENGTH (t1)));
3122 case COMPLEX_CST:
3123 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3124 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3126 case VECTOR_CST:
3128 unsigned i;
3130 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3131 return 0;
3133 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3134 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3135 VECTOR_CST_ELT (t2, i)))
3136 return 0;
3138 return 1;
3141 case CONSTRUCTOR:
3143 vec<constructor_elt, va_gc> *v1, *v2;
3144 unsigned HOST_WIDE_INT idx;
3146 typecode = TREE_CODE (TREE_TYPE (t1));
3147 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3148 return 0;
3150 if (typecode == ARRAY_TYPE)
3152 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3153 /* For arrays, check that the sizes all match. */
3154 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3155 || size_1 == -1
3156 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3157 return 0;
3159 else
3161 /* For record and union constructors, require exact type
3162 equality. */
3163 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3164 return 0;
3167 v1 = CONSTRUCTOR_ELTS (t1);
3168 v2 = CONSTRUCTOR_ELTS (t2);
3169 if (vec_safe_length (v1) != vec_safe_length (v2))
3170 return 0;
3172 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3174 constructor_elt *c1 = &(*v1)[idx];
3175 constructor_elt *c2 = &(*v2)[idx];
3177 /* Check that each value is the same... */
3178 if (!compare_constant (c1->value, c2->value))
3179 return 0;
3180 /* ... and that they apply to the same fields! */
3181 if (typecode == ARRAY_TYPE)
3183 if (!compare_constant (c1->index, c2->index))
3184 return 0;
3186 else
3188 if (c1->index != c2->index)
3189 return 0;
3193 return 1;
3196 case ADDR_EXPR:
3197 case FDESC_EXPR:
3199 struct addr_const value1, value2;
3200 enum rtx_code code;
3201 int ret;
3203 decode_addr_const (t1, &value1);
3204 decode_addr_const (t2, &value2);
3206 if (value1.offset != value2.offset)
3207 return 0;
3209 code = GET_CODE (value1.base);
3210 if (code != GET_CODE (value2.base))
3211 return 0;
3213 switch (code)
3215 case SYMBOL_REF:
3216 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3217 break;
3219 case LABEL_REF:
3220 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3221 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3222 break;
3224 default:
3225 gcc_unreachable ();
3227 return ret;
3230 case PLUS_EXPR:
3231 case POINTER_PLUS_EXPR:
3232 case MINUS_EXPR:
3233 case RANGE_EXPR:
3234 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3235 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3237 CASE_CONVERT:
3238 case VIEW_CONVERT_EXPR:
3239 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3241 default:
3242 return 0;
3245 gcc_unreachable ();
3248 /* Return the section into which constant EXP should be placed. */
3250 static section *
3251 get_constant_section (tree exp, unsigned int align)
3253 return targetm.asm_out.select_section (exp,
3254 compute_reloc_for_constant (exp),
3255 align);
3258 /* Return the size of constant EXP in bytes. */
3260 static HOST_WIDE_INT
3261 get_constant_size (tree exp)
3263 HOST_WIDE_INT size;
3265 size = int_size_in_bytes (TREE_TYPE (exp));
3266 if (TREE_CODE (exp) == STRING_CST)
3267 size = MAX (TREE_STRING_LENGTH (exp), size);
3268 return size;
3271 /* Subroutine of output_constant_def:
3272 No constant equal to EXP is known to have been output.
3273 Make a constant descriptor to enter EXP in the hash table.
3274 Assign the label number and construct RTL to refer to the
3275 constant's location in memory.
3276 Caller is responsible for updating the hash table. */
3278 static struct constant_descriptor_tree *
3279 build_constant_desc (tree exp)
3281 struct constant_descriptor_tree *desc;
3282 rtx symbol, rtl;
3283 char label[256];
3284 int labelno;
3285 tree decl;
3287 desc = ggc_alloc<constant_descriptor_tree> ();
3288 desc->value = exp;
3290 /* Create a string containing the label name, in LABEL. */
3291 labelno = const_labelno++;
3292 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3294 /* Construct the VAR_DECL associated with the constant. */
3295 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3296 TREE_TYPE (exp));
3297 DECL_ARTIFICIAL (decl) = 1;
3298 DECL_IGNORED_P (decl) = 1;
3299 TREE_READONLY (decl) = 1;
3300 TREE_STATIC (decl) = 1;
3301 TREE_ADDRESSABLE (decl) = 1;
3302 /* We don't set the RTL yet as this would cause varpool to assume that the
3303 variable is referenced. Moreover, it would just be dropped in LTO mode.
3304 Instead we set the flag that will be recognized in make_decl_rtl. */
3305 DECL_IN_CONSTANT_POOL (decl) = 1;
3306 DECL_INITIAL (decl) = desc->value;
3307 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3308 architectures so use DATA_ALIGNMENT as well, except for strings. */
3309 if (TREE_CODE (exp) == STRING_CST)
3311 #ifdef CONSTANT_ALIGNMENT
3312 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3313 #endif
3315 else
3316 align_variable (decl, 0);
3318 /* Now construct the SYMBOL_REF and the MEM. */
3319 if (use_object_blocks_p ())
3321 int align = (TREE_CODE (decl) == CONST_DECL
3322 || (TREE_CODE (decl) == VAR_DECL
3323 && DECL_IN_CONSTANT_POOL (decl))
3324 ? DECL_ALIGN (decl)
3325 : symtab_node::get (decl)->definition_alignment ());
3326 section *sect = get_constant_section (exp, align);
3327 symbol = create_block_symbol (ggc_strdup (label),
3328 get_block_for_section (sect), -1);
3330 else
3331 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3332 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3333 SET_SYMBOL_REF_DECL (symbol, decl);
3334 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3336 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3337 set_mem_attributes (rtl, exp, 1);
3338 set_mem_alias_set (rtl, 0);
3340 /* We cannot share RTX'es in pool entries.
3341 Mark this piece of RTL as required for unsharing. */
3342 RTX_FLAG (rtl, used) = 1;
3344 /* Set flags or add text to the name to record information, such as
3345 that it is a local symbol. If the name is changed, the macro
3346 ASM_OUTPUT_LABELREF will have to know how to strip this
3347 information. This call might invalidate our local variable
3348 SYMBOL; we can't use it afterward. */
3349 targetm.encode_section_info (exp, rtl, true);
3351 desc->rtl = rtl;
3353 return desc;
3356 /* Return an rtx representing a reference to constant data in memory
3357 for the constant expression EXP.
3359 If assembler code for such a constant has already been output,
3360 return an rtx to refer to it.
3361 Otherwise, output such a constant in memory
3362 and generate an rtx for it.
3364 If DEFER is nonzero, this constant can be deferred and output only
3365 if referenced in the function after all optimizations.
3367 `const_desc_table' records which constants already have label strings. */
3370 output_constant_def (tree exp, int defer)
3372 struct constant_descriptor_tree *desc;
3373 struct constant_descriptor_tree key;
3375 /* Look up EXP in the table of constant descriptors. If we didn't find
3376 it, create a new one. */
3377 key.value = exp;
3378 key.hash = const_hash_1 (exp);
3379 constant_descriptor_tree **loc
3380 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3382 desc = *loc;
3383 if (desc == 0)
3385 desc = build_constant_desc (exp);
3386 desc->hash = key.hash;
3387 *loc = desc;
3390 maybe_output_constant_def_contents (desc, defer);
3391 return desc->rtl;
3394 /* Subroutine of output_constant_def: Decide whether or not we need to
3395 output the constant DESC now, and if so, do it. */
3396 static void
3397 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3398 int defer)
3400 rtx symbol = XEXP (desc->rtl, 0);
3401 tree exp = desc->value;
3403 if (flag_syntax_only)
3404 return;
3406 if (TREE_ASM_WRITTEN (exp))
3407 /* Already output; don't do it again. */
3408 return;
3410 /* We can always defer constants as long as the context allows
3411 doing so. */
3412 if (defer)
3414 /* Increment n_deferred_constants if it exists. It needs to be at
3415 least as large as the number of constants actually referred to
3416 by the function. If it's too small we'll stop looking too early
3417 and fail to emit constants; if it's too large we'll only look
3418 through the entire function when we could have stopped earlier. */
3419 if (cfun)
3420 n_deferred_constants++;
3421 return;
3424 output_constant_def_contents (symbol);
3427 /* Subroutine of output_constant_def_contents. Output the definition
3428 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3429 constant's alignment in bits. */
3431 static void
3432 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3434 HOST_WIDE_INT size;
3436 size = get_constant_size (exp);
3438 /* Do any machine/system dependent processing of the constant. */
3439 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3441 /* Output the value of EXP. */
3442 output_constant (exp, size, align);
3444 targetm.asm_out.decl_end ();
3447 /* We must output the constant data referred to by SYMBOL; do so. */
3449 static void
3450 output_constant_def_contents (rtx symbol)
3452 tree decl = SYMBOL_REF_DECL (symbol);
3453 tree exp = DECL_INITIAL (decl);
3454 bool asan_protected = false;
3456 /* Make sure any other constants whose addresses appear in EXP
3457 are assigned label numbers. */
3458 output_addressed_constants (exp);
3460 /* We are no longer deferring this constant. */
3461 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3463 if ((flag_sanitize & SANITIZE_ADDRESS)
3464 && TREE_CODE (exp) == STRING_CST
3465 && asan_protect_global (exp))
3467 asan_protected = true;
3468 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3469 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3472 /* If the constant is part of an object block, make sure that the
3473 decl has been positioned within its block, but do not write out
3474 its definition yet. output_object_blocks will do that later. */
3475 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3476 place_block_symbol (symbol);
3477 else
3479 int align = (TREE_CODE (decl) == CONST_DECL
3480 || (TREE_CODE (decl) == VAR_DECL
3481 && DECL_IN_CONSTANT_POOL (decl))
3482 ? DECL_ALIGN (decl)
3483 : symtab_node::get (decl)->definition_alignment ());
3484 switch_to_section (get_constant_section (exp, align));
3485 if (align > BITS_PER_UNIT)
3486 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3487 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3488 if (asan_protected)
3490 HOST_WIDE_INT size = get_constant_size (exp);
3491 assemble_zeros (asan_red_zone_size (size));
3496 /* Look up EXP in the table of constant descriptors. Return the rtl
3497 if it has been emitted, else null. */
3500 lookup_constant_def (tree exp)
3502 struct constant_descriptor_tree key;
3504 key.value = exp;
3505 key.hash = const_hash_1 (exp);
3506 constant_descriptor_tree *desc
3507 = const_desc_htab->find_with_hash (&key, key.hash);
3509 return (desc ? desc->rtl : NULL_RTX);
3512 /* Return a tree representing a reference to constant data in memory
3513 for the constant expression EXP.
3515 This is the counterpart of output_constant_def at the Tree level. */
3517 tree
3518 tree_output_constant_def (tree exp)
3520 struct constant_descriptor_tree *desc, key;
3521 tree decl;
3523 /* Look up EXP in the table of constant descriptors. If we didn't find
3524 it, create a new one. */
3525 key.value = exp;
3526 key.hash = const_hash_1 (exp);
3527 constant_descriptor_tree **loc
3528 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3530 desc = *loc;
3531 if (desc == 0)
3533 desc = build_constant_desc (exp);
3534 desc->hash = key.hash;
3535 *loc = desc;
3538 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3539 varpool_node::finalize_decl (decl);
3540 return decl;
3543 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3544 struct constant_descriptor_rtx *next;
3545 rtx mem;
3546 rtx sym;
3547 rtx constant;
3548 HOST_WIDE_INT offset;
3549 hashval_t hash;
3550 machine_mode mode;
3551 unsigned int align;
3552 int labelno;
3553 int mark;
3556 struct const_rtx_desc_hasher : ggc_hasher<constant_descriptor_rtx *>
3558 static hashval_t hash (constant_descriptor_rtx *);
3559 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3562 /* Used in the hash tables to avoid outputting the same constant
3563 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3564 are output once per function, not once per file. */
3565 /* ??? Only a few targets need per-function constant pools. Most
3566 can use one per-file pool. Should add a targetm bit to tell the
3567 difference. */
3569 struct GTY(()) rtx_constant_pool {
3570 /* Pointers to first and last constant in pool, as ordered by offset. */
3571 struct constant_descriptor_rtx *first;
3572 struct constant_descriptor_rtx *last;
3574 /* Hash facility for making memory-constants from constant rtl-expressions.
3575 It is used on RISC machines where immediate integer arguments and
3576 constant addresses are restricted so that such constants must be stored
3577 in memory. */
3578 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3580 /* Current offset in constant pool (does not include any
3581 machine-specific header). */
3582 HOST_WIDE_INT offset;
3585 /* Hash and compare functions for const_rtx_htab. */
3587 hashval_t
3588 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3590 return desc->hash;
3593 bool
3594 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3595 constant_descriptor_rtx *y)
3597 if (x->mode != y->mode)
3598 return 0;
3599 return rtx_equal_p (x->constant, y->constant);
3602 /* Hash one component of a constant. */
3604 static hashval_t
3605 const_rtx_hash_1 (const_rtx x)
3607 unsigned HOST_WIDE_INT hwi;
3608 machine_mode mode;
3609 enum rtx_code code;
3610 hashval_t h;
3611 int i;
3613 code = GET_CODE (x);
3614 mode = GET_MODE (x);
3615 h = (hashval_t) code * 1048573 + mode;
3617 switch (code)
3619 case CONST_INT:
3620 hwi = INTVAL (x);
3622 fold_hwi:
3624 int shift = sizeof (hashval_t) * CHAR_BIT;
3625 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3627 h ^= (hashval_t) hwi;
3628 for (i = 1; i < n; ++i)
3630 hwi >>= shift;
3631 h ^= (hashval_t) hwi;
3634 break;
3636 case CONST_WIDE_INT:
3637 hwi = GET_MODE_PRECISION (mode);
3639 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3640 hwi ^= CONST_WIDE_INT_ELT (x, i);
3641 goto fold_hwi;
3644 case CONST_DOUBLE:
3645 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3647 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3648 goto fold_hwi;
3650 else
3651 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3652 break;
3654 case CONST_FIXED:
3655 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3656 break;
3658 case SYMBOL_REF:
3659 h ^= htab_hash_string (XSTR (x, 0));
3660 break;
3662 case LABEL_REF:
3663 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3664 break;
3666 case UNSPEC:
3667 case UNSPEC_VOLATILE:
3668 h = h * 251 + XINT (x, 1);
3669 break;
3671 default:
3672 break;
3675 return h;
3678 /* Compute a hash value for X, which should be a constant. */
3680 static hashval_t
3681 const_rtx_hash (rtx x)
3683 hashval_t h = 0;
3684 subrtx_iterator::array_type array;
3685 FOR_EACH_SUBRTX (iter, array, x, ALL)
3686 h = h * 509 + const_rtx_hash_1 (*iter);
3687 return h;
3691 /* Create and return a new rtx constant pool. */
3693 static struct rtx_constant_pool *
3694 create_constant_pool (void)
3696 struct rtx_constant_pool *pool;
3698 pool = ggc_alloc<rtx_constant_pool> ();
3699 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3700 pool->first = NULL;
3701 pool->last = NULL;
3702 pool->offset = 0;
3703 return pool;
3706 /* Initialize constant pool hashing for a new function. */
3708 void
3709 init_varasm_status (void)
3711 crtl->varasm.pool = create_constant_pool ();
3712 crtl->varasm.deferred_constants = 0;
3715 /* Given a MINUS expression, simplify it if both sides
3716 include the same symbol. */
3719 simplify_subtraction (rtx x)
3721 rtx r = simplify_rtx (x);
3722 return r ? r : x;
3725 /* Given a constant rtx X, make (or find) a memory constant for its value
3726 and return a MEM rtx to refer to it in memory. */
3729 force_const_mem (machine_mode mode, rtx x)
3731 struct constant_descriptor_rtx *desc, tmp;
3732 struct rtx_constant_pool *pool;
3733 char label[256];
3734 rtx def, symbol;
3735 hashval_t hash;
3736 unsigned int align;
3737 constant_descriptor_rtx **slot;
3739 /* If we're not allowed to drop X into the constant pool, don't. */
3740 if (targetm.cannot_force_const_mem (mode, x))
3741 return NULL_RTX;
3743 /* Record that this function has used a constant pool entry. */
3744 crtl->uses_const_pool = 1;
3746 /* Decide which pool to use. */
3747 pool = (targetm.use_blocks_for_constant_p (mode, x)
3748 ? shared_constant_pool
3749 : crtl->varasm.pool);
3751 /* Lookup the value in the hashtable. */
3752 tmp.constant = x;
3753 tmp.mode = mode;
3754 hash = const_rtx_hash (x);
3755 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3756 desc = *slot;
3758 /* If the constant was already present, return its memory. */
3759 if (desc)
3760 return copy_rtx (desc->mem);
3762 /* Otherwise, create a new descriptor. */
3763 desc = ggc_alloc<constant_descriptor_rtx> ();
3764 *slot = desc;
3766 /* Align the location counter as required by EXP's data type. */
3767 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3768 #ifdef CONSTANT_ALIGNMENT
3770 tree type = lang_hooks.types.type_for_mode (mode, 0);
3771 if (type != NULL_TREE)
3772 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3774 #endif
3776 pool->offset += (align / BITS_PER_UNIT) - 1;
3777 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3779 desc->next = NULL;
3780 desc->constant = copy_rtx (tmp.constant);
3781 desc->offset = pool->offset;
3782 desc->hash = hash;
3783 desc->mode = mode;
3784 desc->align = align;
3785 desc->labelno = const_labelno;
3786 desc->mark = 0;
3788 pool->offset += GET_MODE_SIZE (mode);
3789 if (pool->last)
3790 pool->last->next = desc;
3791 else
3792 pool->first = pool->last = desc;
3793 pool->last = desc;
3795 /* Create a string containing the label name, in LABEL. */
3796 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3797 ++const_labelno;
3799 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3800 the constants pool. */
3801 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3803 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3804 symbol = create_block_symbol (ggc_strdup (label),
3805 get_block_for_section (sect), -1);
3807 else
3808 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3809 desc->sym = symbol;
3810 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3811 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3812 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3814 /* Construct the MEM. */
3815 desc->mem = def = gen_const_mem (mode, symbol);
3816 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3817 set_mem_align (def, align);
3819 /* If we're dropping a label to the constant pool, make sure we
3820 don't delete it. */
3821 if (GET_CODE (x) == LABEL_REF)
3822 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3824 return copy_rtx (def);
3827 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3830 get_pool_constant (const_rtx addr)
3832 return SYMBOL_REF_CONSTANT (addr)->constant;
3835 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3836 and whether it has been output or not. */
3839 get_pool_constant_mark (rtx addr, bool *pmarked)
3841 struct constant_descriptor_rtx *desc;
3843 desc = SYMBOL_REF_CONSTANT (addr);
3844 *pmarked = (desc->mark != 0);
3845 return desc->constant;
3848 /* Similar, return the mode. */
3850 machine_mode
3851 get_pool_mode (const_rtx addr)
3853 return SYMBOL_REF_CONSTANT (addr)->mode;
3856 /* Return the size of the constant pool. */
3859 get_pool_size (void)
3861 return crtl->varasm.pool->offset;
3864 /* Worker function for output_constant_pool_1. Emit assembly for X
3865 in MODE with known alignment ALIGN. */
3867 static void
3868 output_constant_pool_2 (machine_mode mode, rtx x, unsigned int align)
3870 switch (GET_MODE_CLASS (mode))
3872 case MODE_FLOAT:
3873 case MODE_DECIMAL_FLOAT:
3875 REAL_VALUE_TYPE r;
3877 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3878 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3879 assemble_real (r, mode, align);
3880 break;
3883 case MODE_INT:
3884 case MODE_PARTIAL_INT:
3885 case MODE_FRACT:
3886 case MODE_UFRACT:
3887 case MODE_ACCUM:
3888 case MODE_UACCUM:
3889 case MODE_POINTER_BOUNDS:
3890 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3891 break;
3893 case MODE_VECTOR_FLOAT:
3894 case MODE_VECTOR_INT:
3895 case MODE_VECTOR_FRACT:
3896 case MODE_VECTOR_UFRACT:
3897 case MODE_VECTOR_ACCUM:
3898 case MODE_VECTOR_UACCUM:
3900 int i, units;
3901 machine_mode submode = GET_MODE_INNER (mode);
3902 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3904 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3905 units = CONST_VECTOR_NUNITS (x);
3907 for (i = 0; i < units; i++)
3909 rtx elt = CONST_VECTOR_ELT (x, i);
3910 output_constant_pool_2 (submode, elt, i ? subalign : align);
3913 break;
3915 default:
3916 gcc_unreachable ();
3920 /* Worker function for output_constant_pool. Emit constant DESC,
3921 giving it ALIGN bits of alignment. */
3923 static void
3924 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3925 unsigned int align)
3927 rtx x, tmp;
3929 x = desc->constant;
3931 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3932 whose CODE_LABEL has been deleted. This can occur if a jump table
3933 is eliminated by optimization. If so, write a constant of zero
3934 instead. Note that this can also happen by turning the
3935 CODE_LABEL into a NOTE. */
3936 /* ??? This seems completely and utterly wrong. Certainly it's
3937 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3938 functioning even with rtx_insn::deleted and friends. */
3940 tmp = x;
3941 switch (GET_CODE (tmp))
3943 case CONST:
3944 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3945 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3946 break;
3947 tmp = XEXP (XEXP (tmp, 0), 0);
3948 /* FALLTHRU */
3950 case LABEL_REF:
3951 tmp = LABEL_REF_LABEL (tmp);
3952 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3953 gcc_assert (!NOTE_P (tmp)
3954 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3955 break;
3957 default:
3958 break;
3961 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3962 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3963 align, desc->labelno, done);
3964 #endif
3966 assemble_align (align);
3968 /* Output the label. */
3969 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3971 /* Output the data.
3972 Pass actual alignment value while emitting string constant to asm code
3973 as function 'output_constant_pool_1' explicitly passes the alignment as 1
3974 assuming that the data is already aligned which prevents the generation
3975 of fix-up table entries. */
3976 output_constant_pool_2 (desc->mode, x, desc->align);
3978 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3979 sections have proper size. */
3980 if (align > GET_MODE_BITSIZE (desc->mode)
3981 && in_section
3982 && (in_section->common.flags & SECTION_MERGE))
3983 assemble_align (align);
3985 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3986 done:
3987 #endif
3988 return;
3991 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3992 Emit referenced deferred strings. */
3994 static void
3995 mark_constants_in_pattern (rtx insn)
3997 subrtx_iterator::array_type array;
3998 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4000 const_rtx x = *iter;
4001 if (GET_CODE (x) == SYMBOL_REF)
4003 if (CONSTANT_POOL_ADDRESS_P (x))
4005 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4006 if (desc->mark == 0)
4008 desc->mark = 1;
4009 iter.substitute (desc->constant);
4012 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4014 tree decl = SYMBOL_REF_DECL (x);
4015 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4017 n_deferred_constants--;
4018 output_constant_def_contents (CONST_CAST_RTX (x));
4025 /* Look through appropriate parts of INSN, marking all entries in the
4026 constant pool which are actually being used. Entries that are only
4027 referenced by other constants are also marked as used. Emit
4028 deferred strings that are used. */
4030 static void
4031 mark_constants (rtx_insn *insn)
4033 if (!INSN_P (insn))
4034 return;
4036 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4037 insns, not any notes that may be attached. We don't want to mark
4038 a constant just because it happens to appear in a REG_EQUIV note. */
4039 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4041 int i, n = seq->len ();
4042 for (i = 0; i < n; ++i)
4044 rtx subinsn = seq->element (i);
4045 if (INSN_P (subinsn))
4046 mark_constants_in_pattern (subinsn);
4049 else
4050 mark_constants_in_pattern (insn);
4053 /* Look through the instructions for this function, and mark all the
4054 entries in POOL which are actually being used. Emit deferred constants
4055 which have indeed been used. */
4057 static void
4058 mark_constant_pool (void)
4060 rtx_insn *insn;
4062 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4063 return;
4065 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4066 mark_constants (insn);
4069 /* Write all the constants in POOL. */
4071 static void
4072 output_constant_pool_contents (struct rtx_constant_pool *pool)
4074 struct constant_descriptor_rtx *desc;
4076 for (desc = pool->first; desc ; desc = desc->next)
4077 if (desc->mark)
4079 /* If the constant is part of an object_block, make sure that
4080 the constant has been positioned within its block, but do not
4081 write out its definition yet. output_object_blocks will do
4082 that later. */
4083 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4084 && SYMBOL_REF_BLOCK (desc->sym))
4085 place_block_symbol (desc->sym);
4086 else
4088 switch_to_section (targetm.asm_out.select_rtx_section
4089 (desc->mode, desc->constant, desc->align));
4090 output_constant_pool_1 (desc, desc->align);
4095 /* Mark all constants that are used in the current function, then write
4096 out the function's private constant pool. */
4098 static void
4099 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4100 tree fndecl ATTRIBUTE_UNUSED)
4102 struct rtx_constant_pool *pool = crtl->varasm.pool;
4104 /* It is possible for gcc to call force_const_mem and then to later
4105 discard the instructions which refer to the constant. In such a
4106 case we do not need to output the constant. */
4107 mark_constant_pool ();
4109 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4110 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4111 #endif
4113 output_constant_pool_contents (pool);
4115 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4116 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4117 #endif
4120 /* Write the contents of the shared constant pool. */
4122 void
4123 output_shared_constant_pool (void)
4125 output_constant_pool_contents (shared_constant_pool);
4128 /* Determine what kind of relocations EXP may need. */
4131 compute_reloc_for_constant (tree exp)
4133 int reloc = 0, reloc2;
4134 tree tem;
4136 switch (TREE_CODE (exp))
4138 case ADDR_EXPR:
4139 case FDESC_EXPR:
4140 /* Go inside any operations that get_inner_reference can handle and see
4141 if what's inside is a constant: no need to do anything here for
4142 addresses of variables or functions. */
4143 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4144 tem = TREE_OPERAND (tem, 0))
4147 if (TREE_CODE (tem) == MEM_REF
4148 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4150 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4151 break;
4154 if (!targetm.binds_local_p (tem))
4155 reloc |= 2;
4156 else
4157 reloc |= 1;
4158 break;
4160 case PLUS_EXPR:
4161 case POINTER_PLUS_EXPR:
4162 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4163 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4164 break;
4166 case MINUS_EXPR:
4167 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4168 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4169 /* The difference of two local labels is computable at link time. */
4170 if (reloc == 1 && reloc2 == 1)
4171 reloc = 0;
4172 else
4173 reloc |= reloc2;
4174 break;
4176 CASE_CONVERT:
4177 case VIEW_CONVERT_EXPR:
4178 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4179 break;
4181 case CONSTRUCTOR:
4183 unsigned HOST_WIDE_INT idx;
4184 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4185 if (tem != 0)
4186 reloc |= compute_reloc_for_constant (tem);
4188 break;
4190 default:
4191 break;
4193 return reloc;
4196 /* Find all the constants whose addresses are referenced inside of EXP,
4197 and make sure assembler code with a label has been output for each one.
4198 Indicate whether an ADDR_EXPR has been encountered. */
4200 static void
4201 output_addressed_constants (tree exp)
4203 tree tem;
4205 switch (TREE_CODE (exp))
4207 case ADDR_EXPR:
4208 case FDESC_EXPR:
4209 /* Go inside any operations that get_inner_reference can handle and see
4210 if what's inside is a constant: no need to do anything here for
4211 addresses of variables or functions. */
4212 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4213 tem = TREE_OPERAND (tem, 0))
4216 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4217 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4218 tem = DECL_INITIAL (tem);
4220 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4221 output_constant_def (tem, 0);
4223 if (TREE_CODE (tem) == MEM_REF)
4224 output_addressed_constants (TREE_OPERAND (tem, 0));
4225 break;
4227 case PLUS_EXPR:
4228 case POINTER_PLUS_EXPR:
4229 case MINUS_EXPR:
4230 output_addressed_constants (TREE_OPERAND (exp, 1));
4231 /* Fall through. */
4233 CASE_CONVERT:
4234 case VIEW_CONVERT_EXPR:
4235 output_addressed_constants (TREE_OPERAND (exp, 0));
4236 break;
4238 case CONSTRUCTOR:
4240 unsigned HOST_WIDE_INT idx;
4241 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4242 if (tem != 0)
4243 output_addressed_constants (tem);
4245 break;
4247 default:
4248 break;
4252 /* Whether a constructor CTOR is a valid static constant initializer if all
4253 its elements are. This used to be internal to initializer_constant_valid_p
4254 and has been exposed to let other functions like categorize_ctor_elements
4255 evaluate the property while walking a constructor for other purposes. */
4257 bool
4258 constructor_static_from_elts_p (const_tree ctor)
4260 return (TREE_CONSTANT (ctor)
4261 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4262 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4263 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4266 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4267 tree *cache);
4269 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4270 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4271 which are valid when ENDTYPE is an integer of any size; in
4272 particular, this does not accept a pointer minus a constant. This
4273 returns null_pointer_node if the VALUE is an absolute constant
4274 which can be used to initialize a static variable. Otherwise it
4275 returns NULL. */
4277 static tree
4278 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4280 tree op0, op1;
4282 if (!INTEGRAL_TYPE_P (endtype))
4283 return NULL_TREE;
4285 op0 = TREE_OPERAND (value, 0);
4286 op1 = TREE_OPERAND (value, 1);
4288 /* Like STRIP_NOPS except allow the operand mode to widen. This
4289 works around a feature of fold that simplifies (int)(p1 - p2) to
4290 ((int)p1 - (int)p2) under the theory that the narrower operation
4291 is cheaper. */
4293 while (CONVERT_EXPR_P (op0)
4294 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4296 tree inner = TREE_OPERAND (op0, 0);
4297 if (inner == error_mark_node
4298 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4299 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4300 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4301 break;
4302 op0 = inner;
4305 while (CONVERT_EXPR_P (op1)
4306 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4308 tree inner = TREE_OPERAND (op1, 0);
4309 if (inner == error_mark_node
4310 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4311 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4312 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4313 break;
4314 op1 = inner;
4317 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4318 if (!op0)
4319 return NULL_TREE;
4321 op1 = initializer_constant_valid_p_1 (op1, endtype,
4322 cache ? cache + 2 : NULL);
4323 /* Both initializers must be known. */
4324 if (op1)
4326 if (op0 == op1
4327 && (op0 == null_pointer_node
4328 || TREE_CODE (value) == MINUS_EXPR))
4329 return null_pointer_node;
4331 /* Support differences between labels. */
4332 if (TREE_CODE (op0) == LABEL_DECL
4333 && TREE_CODE (op1) == LABEL_DECL)
4334 return null_pointer_node;
4336 if (TREE_CODE (op0) == STRING_CST
4337 && TREE_CODE (op1) == STRING_CST
4338 && operand_equal_p (op0, op1, 1))
4339 return null_pointer_node;
4342 return NULL_TREE;
4345 /* Helper function of initializer_constant_valid_p.
4346 Return nonzero if VALUE is a valid constant-valued expression
4347 for use in initializing a static variable; one that can be an
4348 element of a "constant" initializer.
4350 Return null_pointer_node if the value is absolute;
4351 if it is relocatable, return the variable that determines the relocation.
4352 We assume that VALUE has been folded as much as possible;
4353 therefore, we do not need to check for such things as
4354 arithmetic-combinations of integers.
4356 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4358 static tree
4359 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4361 tree ret;
4363 switch (TREE_CODE (value))
4365 case CONSTRUCTOR:
4366 if (constructor_static_from_elts_p (value))
4368 unsigned HOST_WIDE_INT idx;
4369 tree elt;
4370 bool absolute = true;
4372 if (cache && cache[0] == value)
4373 return cache[1];
4374 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4376 tree reloc;
4377 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4378 NULL);
4379 if (!reloc)
4381 if (cache)
4383 cache[0] = value;
4384 cache[1] = NULL_TREE;
4386 return NULL_TREE;
4388 if (reloc != null_pointer_node)
4389 absolute = false;
4391 /* For a non-absolute relocation, there is no single
4392 variable that can be "the variable that determines the
4393 relocation." */
4394 if (cache)
4396 cache[0] = value;
4397 cache[1] = absolute ? null_pointer_node : error_mark_node;
4399 return absolute ? null_pointer_node : error_mark_node;
4402 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4404 case INTEGER_CST:
4405 case VECTOR_CST:
4406 case REAL_CST:
4407 case FIXED_CST:
4408 case STRING_CST:
4409 case COMPLEX_CST:
4410 return null_pointer_node;
4412 case ADDR_EXPR:
4413 case FDESC_EXPR:
4415 tree op0 = staticp (TREE_OPERAND (value, 0));
4416 if (op0)
4418 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4419 to be a constant, this is old-skool offsetof-like nonsense. */
4420 if (TREE_CODE (op0) == INDIRECT_REF
4421 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4422 return null_pointer_node;
4423 /* Taking the address of a nested function involves a trampoline,
4424 unless we don't need or want one. */
4425 if (TREE_CODE (op0) == FUNCTION_DECL
4426 && DECL_STATIC_CHAIN (op0)
4427 && !TREE_NO_TRAMPOLINE (value))
4428 return NULL_TREE;
4429 /* "&{...}" requires a temporary to hold the constructed
4430 object. */
4431 if (TREE_CODE (op0) == CONSTRUCTOR)
4432 return NULL_TREE;
4434 return op0;
4437 case NON_LVALUE_EXPR:
4438 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4439 endtype, cache);
4441 case VIEW_CONVERT_EXPR:
4443 tree src = TREE_OPERAND (value, 0);
4444 tree src_type = TREE_TYPE (src);
4445 tree dest_type = TREE_TYPE (value);
4447 /* Allow view-conversions from aggregate to non-aggregate type only
4448 if the bit pattern is fully preserved afterwards; otherwise, the
4449 RTL expander won't be able to apply a subsequent transformation
4450 to the underlying constructor. */
4451 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4453 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4454 return initializer_constant_valid_p_1 (src, endtype, cache);
4455 else
4456 return NULL_TREE;
4459 /* Allow all other kinds of view-conversion. */
4460 return initializer_constant_valid_p_1 (src, endtype, cache);
4463 CASE_CONVERT:
4465 tree src = TREE_OPERAND (value, 0);
4466 tree src_type = TREE_TYPE (src);
4467 tree dest_type = TREE_TYPE (value);
4469 /* Allow conversions between pointer types, floating-point
4470 types, and offset types. */
4471 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4472 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4473 || (TREE_CODE (dest_type) == OFFSET_TYPE
4474 && TREE_CODE (src_type) == OFFSET_TYPE))
4475 return initializer_constant_valid_p_1 (src, endtype, cache);
4477 /* Allow length-preserving conversions between integer types. */
4478 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4479 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4480 return initializer_constant_valid_p_1 (src, endtype, cache);
4482 /* Allow conversions between other integer types only if
4483 explicit value. */
4484 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4486 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4487 if (inner == null_pointer_node)
4488 return null_pointer_node;
4489 break;
4492 /* Allow (int) &foo provided int is as wide as a pointer. */
4493 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4494 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4495 return initializer_constant_valid_p_1 (src, endtype, cache);
4497 /* Likewise conversions from int to pointers, but also allow
4498 conversions from 0. */
4499 if ((POINTER_TYPE_P (dest_type)
4500 || TREE_CODE (dest_type) == OFFSET_TYPE)
4501 && INTEGRAL_TYPE_P (src_type))
4503 if (TREE_CODE (src) == INTEGER_CST
4504 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4505 return null_pointer_node;
4506 if (integer_zerop (src))
4507 return null_pointer_node;
4508 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4509 return initializer_constant_valid_p_1 (src, endtype, cache);
4512 /* Allow conversions to struct or union types if the value
4513 inside is okay. */
4514 if (TREE_CODE (dest_type) == RECORD_TYPE
4515 || TREE_CODE (dest_type) == UNION_TYPE)
4516 return initializer_constant_valid_p_1 (src, endtype, cache);
4518 break;
4520 case POINTER_PLUS_EXPR:
4521 case PLUS_EXPR:
4522 /* Any valid floating-point constants will have been folded by now;
4523 with -frounding-math we hit this with addition of two constants. */
4524 if (TREE_CODE (endtype) == REAL_TYPE)
4525 return NULL_TREE;
4526 if (cache && cache[0] == value)
4527 return cache[1];
4528 if (! INTEGRAL_TYPE_P (endtype)
4529 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4531 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4532 tree valid0
4533 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4534 endtype, ncache);
4535 tree valid1
4536 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4537 endtype, ncache + 2);
4538 /* If either term is absolute, use the other term's relocation. */
4539 if (valid0 == null_pointer_node)
4540 ret = valid1;
4541 else if (valid1 == null_pointer_node)
4542 ret = valid0;
4543 /* Support narrowing pointer differences. */
4544 else
4545 ret = narrowing_initializer_constant_valid_p (value, endtype,
4546 ncache);
4548 else
4549 /* Support narrowing pointer differences. */
4550 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4551 if (cache)
4553 cache[0] = value;
4554 cache[1] = ret;
4556 return ret;
4558 case MINUS_EXPR:
4559 if (TREE_CODE (endtype) == REAL_TYPE)
4560 return NULL_TREE;
4561 if (cache && cache[0] == value)
4562 return cache[1];
4563 if (! INTEGRAL_TYPE_P (endtype)
4564 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4566 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4567 tree valid0
4568 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4569 endtype, ncache);
4570 tree valid1
4571 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4572 endtype, ncache + 2);
4573 /* Win if second argument is absolute. */
4574 if (valid1 == null_pointer_node)
4575 ret = valid0;
4576 /* Win if both arguments have the same relocation.
4577 Then the value is absolute. */
4578 else if (valid0 == valid1 && valid0 != 0)
4579 ret = null_pointer_node;
4580 /* Since GCC guarantees that string constants are unique in the
4581 generated code, a subtraction between two copies of the same
4582 constant string is absolute. */
4583 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4584 && valid1 && TREE_CODE (valid1) == STRING_CST
4585 && operand_equal_p (valid0, valid1, 1))
4586 ret = null_pointer_node;
4587 /* Support narrowing differences. */
4588 else
4589 ret = narrowing_initializer_constant_valid_p (value, endtype,
4590 ncache);
4592 else
4593 /* Support narrowing differences. */
4594 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4595 if (cache)
4597 cache[0] = value;
4598 cache[1] = ret;
4600 return ret;
4602 default:
4603 break;
4606 return NULL_TREE;
4609 /* Return nonzero if VALUE is a valid constant-valued expression
4610 for use in initializing a static variable; one that can be an
4611 element of a "constant" initializer.
4613 Return null_pointer_node if the value is absolute;
4614 if it is relocatable, return the variable that determines the relocation.
4615 We assume that VALUE has been folded as much as possible;
4616 therefore, we do not need to check for such things as
4617 arithmetic-combinations of integers. */
4618 tree
4619 initializer_constant_valid_p (tree value, tree endtype)
4621 return initializer_constant_valid_p_1 (value, endtype, NULL);
4624 /* Return true if VALUE is a valid constant-valued expression
4625 for use in initializing a static bit-field; one that can be
4626 an element of a "constant" initializer. */
4628 bool
4629 initializer_constant_valid_for_bitfield_p (tree value)
4631 /* For bitfields we support integer constants or possibly nested aggregates
4632 of such. */
4633 switch (TREE_CODE (value))
4635 case CONSTRUCTOR:
4637 unsigned HOST_WIDE_INT idx;
4638 tree elt;
4640 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4641 if (!initializer_constant_valid_for_bitfield_p (elt))
4642 return false;
4643 return true;
4646 case INTEGER_CST:
4647 case REAL_CST:
4648 return true;
4650 case VIEW_CONVERT_EXPR:
4651 case NON_LVALUE_EXPR:
4652 return
4653 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4655 default:
4656 break;
4659 return false;
4662 /* output_constructor outer state of relevance in recursive calls, typically
4663 for nested aggregate bitfields. */
4665 typedef struct {
4666 unsigned int bit_offset; /* current position in ... */
4667 int byte; /* ... the outer byte buffer. */
4668 } oc_outer_state;
4670 static unsigned HOST_WIDE_INT
4671 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4672 oc_outer_state *);
4674 /* Output assembler code for constant EXP, with no label.
4675 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4676 Assumes output_addressed_constants has been done on EXP already.
4678 Generate at least SIZE bytes of assembler data, padding at the end
4679 with zeros if necessary. SIZE must always be specified. The returned
4680 value is the actual number of bytes of assembler data generated, which
4681 may be bigger than SIZE if the object contains a variable length field.
4683 SIZE is important for structure constructors,
4684 since trailing members may have been omitted from the constructor.
4685 It is also important for initialization of arrays from string constants
4686 since the full length of the string constant might not be wanted.
4687 It is also needed for initialization of unions, where the initializer's
4688 type is just one member, and that may not be as long as the union.
4690 There a case in which we would fail to output exactly SIZE bytes:
4691 for a structure constructor that wants to produce more than SIZE bytes.
4692 But such constructors will never be generated for any possible input.
4694 ALIGN is the alignment of the data in bits. */
4696 static unsigned HOST_WIDE_INT
4697 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4699 enum tree_code code;
4700 unsigned HOST_WIDE_INT thissize;
4702 if (size == 0 || flag_syntax_only)
4703 return size;
4705 /* See if we're trying to initialize a pointer in a non-default mode
4706 to the address of some declaration somewhere. If the target says
4707 the mode is valid for pointers, assume the target has a way of
4708 resolving it. */
4709 if (TREE_CODE (exp) == NOP_EXPR
4710 && POINTER_TYPE_P (TREE_TYPE (exp))
4711 && targetm.addr_space.valid_pointer_mode
4712 (TYPE_MODE (TREE_TYPE (exp)),
4713 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4715 tree saved_type = TREE_TYPE (exp);
4717 /* Peel off any intermediate conversions-to-pointer for valid
4718 pointer modes. */
4719 while (TREE_CODE (exp) == NOP_EXPR
4720 && POINTER_TYPE_P (TREE_TYPE (exp))
4721 && targetm.addr_space.valid_pointer_mode
4722 (TYPE_MODE (TREE_TYPE (exp)),
4723 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4724 exp = TREE_OPERAND (exp, 0);
4726 /* If what we're left with is the address of something, we can
4727 convert the address to the final type and output it that
4728 way. */
4729 if (TREE_CODE (exp) == ADDR_EXPR)
4730 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4731 /* Likewise for constant ints. */
4732 else if (TREE_CODE (exp) == INTEGER_CST)
4733 exp = wide_int_to_tree (saved_type, exp);
4737 /* Eliminate any conversions since we'll be outputting the underlying
4738 constant. */
4739 while (CONVERT_EXPR_P (exp)
4740 || TREE_CODE (exp) == NON_LVALUE_EXPR
4741 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4743 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4744 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4746 /* Make sure eliminating the conversion is really a no-op, except with
4747 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4748 union types to allow for Ada unchecked unions. */
4749 if (type_size > op_size
4750 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4751 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4752 /* Keep the conversion. */
4753 break;
4754 else
4755 exp = TREE_OPERAND (exp, 0);
4758 code = TREE_CODE (TREE_TYPE (exp));
4759 thissize = int_size_in_bytes (TREE_TYPE (exp));
4761 /* Allow a constructor with no elements for any data type.
4762 This means to fill the space with zeros. */
4763 if (TREE_CODE (exp) == CONSTRUCTOR
4764 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4766 assemble_zeros (size);
4767 return size;
4770 if (TREE_CODE (exp) == FDESC_EXPR)
4772 #ifdef ASM_OUTPUT_FDESC
4773 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4774 tree decl = TREE_OPERAND (exp, 0);
4775 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4776 #else
4777 gcc_unreachable ();
4778 #endif
4779 return size;
4782 /* Now output the underlying data. If we've handling the padding, return.
4783 Otherwise, break and ensure SIZE is the size written. */
4784 switch (code)
4786 case BOOLEAN_TYPE:
4787 case INTEGER_TYPE:
4788 case ENUMERAL_TYPE:
4789 case POINTER_TYPE:
4790 case REFERENCE_TYPE:
4791 case OFFSET_TYPE:
4792 case FIXED_POINT_TYPE:
4793 case POINTER_BOUNDS_TYPE:
4794 case NULLPTR_TYPE:
4795 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4796 EXPAND_INITIALIZER),
4797 MIN (size, thissize), align, 0))
4798 error ("initializer for integer/fixed-point value is too complicated");
4799 break;
4801 case REAL_TYPE:
4802 if (TREE_CODE (exp) != REAL_CST)
4803 error ("initializer for floating value is not a floating constant");
4804 else
4805 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4806 break;
4808 case COMPLEX_TYPE:
4809 output_constant (TREE_REALPART (exp), thissize / 2, align);
4810 output_constant (TREE_IMAGPART (exp), thissize / 2,
4811 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4812 break;
4814 case ARRAY_TYPE:
4815 case VECTOR_TYPE:
4816 switch (TREE_CODE (exp))
4818 case CONSTRUCTOR:
4819 return output_constructor (exp, size, align, NULL);
4820 case STRING_CST:
4821 thissize
4822 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4823 assemble_string (TREE_STRING_POINTER (exp), thissize);
4824 break;
4825 case VECTOR_CST:
4827 machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4828 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4829 int elt_size = GET_MODE_SIZE (inner);
4830 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4831 thissize = elt_size;
4832 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4834 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4835 thissize += elt_size;
4837 break;
4839 default:
4840 gcc_unreachable ();
4842 break;
4844 case RECORD_TYPE:
4845 case UNION_TYPE:
4846 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4847 return output_constructor (exp, size, align, NULL);
4849 case ERROR_MARK:
4850 return 0;
4852 default:
4853 gcc_unreachable ();
4856 if (size > thissize)
4857 assemble_zeros (size - thissize);
4859 return size;
4863 /* Subroutine of output_constructor, used for computing the size of
4864 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4865 type with an unspecified upper bound. */
4867 static unsigned HOST_WIDE_INT
4868 array_size_for_constructor (tree val)
4870 tree max_index;
4871 unsigned HOST_WIDE_INT cnt;
4872 tree index, value, tmp;
4873 offset_int i;
4875 /* This code used to attempt to handle string constants that are not
4876 arrays of single-bytes, but nothing else does, so there's no point in
4877 doing it here. */
4878 if (TREE_CODE (val) == STRING_CST)
4879 return TREE_STRING_LENGTH (val);
4881 max_index = NULL_TREE;
4882 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4884 if (TREE_CODE (index) == RANGE_EXPR)
4885 index = TREE_OPERAND (index, 1);
4886 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4887 max_index = index;
4890 if (max_index == NULL_TREE)
4891 return 0;
4893 /* Compute the total number of array elements. */
4894 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4895 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4897 /* Multiply by the array element unit size to find number of bytes. */
4898 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4900 gcc_assert (wi::fits_uhwi_p (i));
4901 return i.to_uhwi ();
4904 /* Other datastructures + helpers for output_constructor. */
4906 /* output_constructor local state to support interaction with helpers. */
4908 typedef struct {
4910 /* Received arguments. */
4911 tree exp; /* Constructor expression. */
4912 tree type; /* Type of constructor expression. */
4913 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4914 unsigned int align; /* Known initial alignment. */
4915 tree min_index; /* Lower bound if specified for an array. */
4917 /* Output processing state. */
4918 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4919 int byte; /* Part of a bitfield byte yet to be output. */
4920 int last_relative_index; /* Implicit or explicit index of the last
4921 array element output within a bitfield. */
4922 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4924 /* Current element. */
4925 tree field; /* Current field decl in a record. */
4926 tree val; /* Current element value. */
4927 tree index; /* Current element index. */
4929 } oc_local_state;
4931 /* Helper for output_constructor. From the current LOCAL state, output a
4932 RANGE_EXPR element. */
4934 static void
4935 output_constructor_array_range (oc_local_state *local)
4937 unsigned HOST_WIDE_INT fieldsize
4938 = int_size_in_bytes (TREE_TYPE (local->type));
4940 HOST_WIDE_INT lo_index
4941 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4942 HOST_WIDE_INT hi_index
4943 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4944 HOST_WIDE_INT index;
4946 unsigned int align2
4947 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4949 for (index = lo_index; index <= hi_index; index++)
4951 /* Output the element's initial value. */
4952 if (local->val == NULL_TREE)
4953 assemble_zeros (fieldsize);
4954 else
4955 fieldsize = output_constant (local->val, fieldsize, align2);
4957 /* Count its size. */
4958 local->total_bytes += fieldsize;
4962 /* Helper for output_constructor. From the current LOCAL state, output a
4963 field element that is not true bitfield or part of an outer one. */
4965 static void
4966 output_constructor_regular_field (oc_local_state *local)
4968 /* Field size and position. Since this structure is static, we know the
4969 positions are constant. */
4970 unsigned HOST_WIDE_INT fieldsize;
4971 HOST_WIDE_INT fieldpos;
4973 unsigned int align2;
4975 if (local->index != NULL_TREE)
4977 /* Perform the index calculation in modulo arithmetic but
4978 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4979 but we are using an unsigned sizetype. */
4980 unsigned prec = TYPE_PRECISION (sizetype);
4981 offset_int idx = wi::sext (wi::to_offset (local->index)
4982 - wi::to_offset (local->min_index), prec);
4983 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4984 .to_short_addr ();
4986 else if (local->field != NULL_TREE)
4987 fieldpos = int_byte_position (local->field);
4988 else
4989 fieldpos = 0;
4991 /* Output any buffered-up bit-fields preceding this element. */
4992 if (local->byte_buffer_in_use)
4994 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4995 local->total_bytes++;
4996 local->byte_buffer_in_use = false;
4999 /* Advance to offset of this element.
5000 Note no alignment needed in an array, since that is guaranteed
5001 if each element has the proper size. */
5002 if ((local->field != NULL_TREE || local->index != NULL_TREE)
5003 && fieldpos > local->total_bytes)
5005 assemble_zeros (fieldpos - local->total_bytes);
5006 local->total_bytes = fieldpos;
5009 /* Find the alignment of this element. */
5010 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5012 /* Determine size this element should occupy. */
5013 if (local->field)
5015 fieldsize = 0;
5017 /* If this is an array with an unspecified upper bound,
5018 the initializer determines the size. */
5019 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5020 but we cannot do this until the deprecated support for
5021 initializing zero-length array members is removed. */
5022 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5023 && TYPE_DOMAIN (TREE_TYPE (local->field))
5024 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
5026 fieldsize = array_size_for_constructor (local->val);
5027 /* Given a non-empty initialization, this field had
5028 better be last. */
5029 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
5031 else
5032 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5034 else
5035 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5037 /* Output the element's initial value. */
5038 if (local->val == NULL_TREE)
5039 assemble_zeros (fieldsize);
5040 else
5041 fieldsize = output_constant (local->val, fieldsize, align2);
5043 /* Count its size. */
5044 local->total_bytes += fieldsize;
5047 /* Helper for output_constructor. From the LOCAL state, output an element
5048 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5049 from the start of a possibly ongoing outer byte buffer. */
5051 static void
5052 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5054 /* Bit size of this element. */
5055 HOST_WIDE_INT ebitsize
5056 = (local->field
5057 ? tree_to_uhwi (DECL_SIZE (local->field))
5058 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5060 /* Relative index of this element if this is an array component. */
5061 HOST_WIDE_INT relative_index
5062 = (!local->field
5063 ? (local->index
5064 ? (tree_to_shwi (local->index)
5065 - tree_to_shwi (local->min_index))
5066 : local->last_relative_index + 1)
5067 : 0);
5069 /* Bit position of this element from the start of the containing
5070 constructor. */
5071 HOST_WIDE_INT constructor_relative_ebitpos
5072 = (local->field
5073 ? int_bit_position (local->field)
5074 : ebitsize * relative_index);
5076 /* Bit position of this element from the start of a possibly ongoing
5077 outer byte buffer. */
5078 HOST_WIDE_INT byte_relative_ebitpos
5079 = bit_offset + constructor_relative_ebitpos;
5081 /* From the start of a possibly ongoing outer byte buffer, offsets to
5082 the first bit of this element and to the first bit past the end of
5083 this element. */
5084 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5085 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5087 local->last_relative_index = relative_index;
5089 if (local->val == NULL_TREE)
5090 local->val = integer_zero_node;
5092 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5093 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5094 local->val = TREE_OPERAND (local->val, 0);
5096 if (TREE_CODE (local->val) != INTEGER_CST
5097 && TREE_CODE (local->val) != CONSTRUCTOR)
5099 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5100 return;
5103 /* If this field does not start in this (or next) byte, skip some bytes. */
5104 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5106 /* Output remnant of any bit field in previous bytes. */
5107 if (local->byte_buffer_in_use)
5109 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5110 local->total_bytes++;
5111 local->byte_buffer_in_use = false;
5114 /* If still not at proper byte, advance to there. */
5115 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5117 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5118 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5119 local->total_bytes = next_offset / BITS_PER_UNIT;
5123 /* Set up the buffer if necessary. */
5124 if (!local->byte_buffer_in_use)
5126 local->byte = 0;
5127 if (ebitsize > 0)
5128 local->byte_buffer_in_use = true;
5131 /* If this is nested constructor, recurse passing the bit offset and the
5132 pending data, then retrieve the new pending data afterwards. */
5133 if (TREE_CODE (local->val) == CONSTRUCTOR)
5135 oc_outer_state temp_state;
5136 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5137 temp_state.byte = local->byte;
5138 local->total_bytes
5139 += output_constructor (local->val, 0, 0, &temp_state);
5140 local->byte = temp_state.byte;
5141 return;
5144 /* Otherwise, we must split the element into pieces that fall within
5145 separate bytes, and combine each byte with previous or following
5146 bit-fields. */
5147 while (next_offset < end_offset)
5149 int this_time;
5150 int shift;
5151 HOST_WIDE_INT value;
5152 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5153 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5155 /* Advance from byte to byte within this element when necessary. */
5156 while (next_byte != local->total_bytes)
5158 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5159 local->total_bytes++;
5160 local->byte = 0;
5163 /* Number of bits we can process at once (all part of the same byte). */
5164 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5165 if (BYTES_BIG_ENDIAN)
5167 /* On big-endian machine, take the most significant bits (of the
5168 bits that are significant) first and put them into bytes from
5169 the most significant end. */
5170 shift = end_offset - next_offset - this_time;
5172 /* Don't try to take a bunch of bits that cross
5173 the word boundary in the INTEGER_CST. We can
5174 only select bits from one element. */
5175 if ((shift / HOST_BITS_PER_WIDE_INT)
5176 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5178 const int end = shift + this_time - 1;
5179 shift = end & -HOST_BITS_PER_WIDE_INT;
5180 this_time = end - shift + 1;
5183 /* Now get the bits from the appropriate constant word. */
5184 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5185 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5187 /* Get the result. This works only when:
5188 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5189 local->byte |= (((value >> shift)
5190 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5191 << (BITS_PER_UNIT - this_time - next_bit));
5193 else
5195 /* On little-endian machines, take the least significant bits of
5196 the value first and pack them starting at the least significant
5197 bits of the bytes. */
5198 shift = next_offset - byte_relative_ebitpos;
5200 /* Don't try to take a bunch of bits that cross
5201 the word boundary in the INTEGER_CST. We can
5202 only select bits from one element. */
5203 if ((shift / HOST_BITS_PER_WIDE_INT)
5204 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5205 this_time
5206 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5208 /* Now get the bits from the appropriate constant word. */
5209 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5210 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5212 /* Get the result. This works only when:
5213 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5214 local->byte |= (((value >> shift)
5215 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5216 << next_bit);
5219 next_offset += this_time;
5220 local->byte_buffer_in_use = true;
5224 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5225 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5226 caller output state of relevance in recursive invocations. */
5228 static unsigned HOST_WIDE_INT
5229 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5230 unsigned int align, oc_outer_state *outer)
5232 unsigned HOST_WIDE_INT cnt;
5233 constructor_elt *ce;
5235 oc_local_state local;
5237 /* Setup our local state to communicate with helpers. */
5238 local.exp = exp;
5239 local.type = TREE_TYPE (exp);
5240 local.size = size;
5241 local.align = align;
5242 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5243 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5244 else
5245 local.min_index = NULL_TREE;
5247 local.total_bytes = 0;
5248 local.byte_buffer_in_use = outer != NULL;
5249 local.byte = outer ? outer->byte : 0;
5250 local.last_relative_index = -1;
5252 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5254 /* As CE goes through the elements of the constant, FIELD goes through the
5255 structure fields if the constant is a structure. If the constant is a
5256 union, we override this by getting the field from the TREE_LIST element.
5257 But the constant could also be an array. Then FIELD is zero.
5259 There is always a maximum of one element in the chain LINK for unions
5260 (even if the initializer in a source program incorrectly contains
5261 more one). */
5263 if (TREE_CODE (local.type) == RECORD_TYPE)
5264 local.field = TYPE_FIELDS (local.type);
5265 else
5266 local.field = NULL_TREE;
5268 for (cnt = 0;
5269 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5270 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5272 local.val = ce->value;
5273 local.index = NULL_TREE;
5275 /* The element in a union constructor specifies the proper field
5276 or index. */
5277 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5278 local.field = ce->index;
5280 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5281 local.index = ce->index;
5283 if (local.field && flag_verbose_asm)
5284 fprintf (asm_out_file, "%s %s:\n",
5285 ASM_COMMENT_START,
5286 DECL_NAME (local.field)
5287 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5288 : "<anonymous>");
5290 /* Eliminate the marker that makes a cast not be an lvalue. */
5291 if (local.val != NULL_TREE)
5292 STRIP_NOPS (local.val);
5294 /* Output the current element, using the appropriate helper ... */
5296 /* For an array slice not part of an outer bitfield. */
5297 if (!outer
5298 && local.index != NULL_TREE
5299 && TREE_CODE (local.index) == RANGE_EXPR)
5300 output_constructor_array_range (&local);
5302 /* For a field that is neither a true bitfield nor part of an outer one,
5303 known to be at least byte aligned and multiple-of-bytes long. */
5304 else if (!outer
5305 && (local.field == NULL_TREE
5306 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5307 output_constructor_regular_field (&local);
5309 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5310 supported for scalar fields, so we may need to convert first. */
5311 else
5313 if (TREE_CODE (local.val) == REAL_CST)
5314 local.val
5315 = fold_unary (VIEW_CONVERT_EXPR,
5316 build_nonstandard_integer_type
5317 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5318 local.val);
5319 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5323 /* If we are not at toplevel, save the pending data for our caller.
5324 Otherwise output the pending data and padding zeros as needed. */
5325 if (outer)
5326 outer->byte = local.byte;
5327 else
5329 if (local.byte_buffer_in_use)
5331 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5332 local.total_bytes++;
5335 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5337 assemble_zeros (local.size - local.total_bytes);
5338 local.total_bytes = local.size;
5342 return local.total_bytes;
5345 /* Mark DECL as weak. */
5347 static void
5348 mark_weak (tree decl)
5350 if (DECL_WEAK (decl))
5351 return;
5353 struct symtab_node *n = symtab_node::get (decl);
5354 if (n && n->refuse_visibility_changes)
5355 error ("%+D declared weak after being used", decl);
5356 DECL_WEAK (decl) = 1;
5358 if (DECL_RTL_SET_P (decl)
5359 && MEM_P (DECL_RTL (decl))
5360 && XEXP (DECL_RTL (decl), 0)
5361 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5362 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5365 /* Merge weak status between NEWDECL and OLDDECL. */
5367 void
5368 merge_weak (tree newdecl, tree olddecl)
5370 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5372 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5374 tree *pwd;
5375 /* We put the NEWDECL on the weak_decls list at some point
5376 and OLDDECL as well. Keep just OLDDECL on the list. */
5377 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5378 if (TREE_VALUE (*pwd) == newdecl)
5380 *pwd = TREE_CHAIN (*pwd);
5381 break;
5384 return;
5387 if (DECL_WEAK (newdecl))
5389 tree wd;
5391 /* NEWDECL is weak, but OLDDECL is not. */
5393 /* If we already output the OLDDECL, we're in trouble; we can't
5394 go back and make it weak. This should never happen in
5395 unit-at-a-time compilation. */
5396 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5398 /* If we've already generated rtl referencing OLDDECL, we may
5399 have done so in a way that will not function properly with
5400 a weak symbol. Again in unit-at-a-time this should be
5401 impossible. */
5402 gcc_assert (!TREE_USED (olddecl)
5403 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5405 if (TARGET_SUPPORTS_WEAK)
5407 /* We put the NEWDECL on the weak_decls list at some point.
5408 Replace it with the OLDDECL. */
5409 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5410 if (TREE_VALUE (wd) == newdecl)
5412 TREE_VALUE (wd) = olddecl;
5413 break;
5415 /* We may not find the entry on the list. If NEWDECL is a
5416 weak alias, then we will have already called
5417 globalize_decl to remove the entry; in that case, we do
5418 not need to do anything. */
5421 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5422 mark_weak (olddecl);
5424 else
5425 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5426 weak. Just update NEWDECL to indicate that it's weak too. */
5427 mark_weak (newdecl);
5430 /* Declare DECL to be a weak symbol. */
5432 void
5433 declare_weak (tree decl)
5435 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5436 if (! TREE_PUBLIC (decl))
5437 error ("weak declaration of %q+D must be public", decl);
5438 else if (!TARGET_SUPPORTS_WEAK)
5439 warning (0, "weak declaration of %q+D not supported", decl);
5441 mark_weak (decl);
5442 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5443 DECL_ATTRIBUTES (decl)
5444 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5447 static void
5448 weak_finish_1 (tree decl)
5450 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5451 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5452 #endif
5454 if (! TREE_USED (decl))
5455 return;
5457 #ifdef ASM_WEAKEN_DECL
5458 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5459 #else
5460 #ifdef ASM_WEAKEN_LABEL
5461 ASM_WEAKEN_LABEL (asm_out_file, name);
5462 #else
5463 #ifdef ASM_OUTPUT_WEAK_ALIAS
5465 static bool warn_once = 0;
5466 if (! warn_once)
5468 warning (0, "only weak aliases are supported in this configuration");
5469 warn_once = 1;
5471 return;
5473 #endif
5474 #endif
5475 #endif
5478 /* Fiven an assembly name, find the decl it is associated with. */
5479 static tree
5480 find_decl (tree target)
5482 symtab_node *node = symtab_node::get_for_asmname (target);
5483 if (node)
5484 return node->decl;
5485 return NULL_TREE;
5488 /* This TREE_LIST contains weakref targets. */
5490 static GTY(()) tree weakref_targets;
5492 /* Emit any pending weak declarations. */
5494 void
5495 weak_finish (void)
5497 tree t;
5499 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5501 tree alias_decl = TREE_PURPOSE (t);
5502 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5504 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5505 /* Remove alias_decl from the weak list, but leave entries for
5506 the target alone. */
5507 target = NULL_TREE;
5508 #ifndef ASM_OUTPUT_WEAKREF
5509 else if (! TREE_SYMBOL_REFERENCED (target))
5511 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5512 defined, otherwise we and weak_finish_1 would use
5513 different macros. */
5514 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5515 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5516 # else
5517 tree decl = find_decl (target);
5519 if (! decl)
5521 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5522 TREE_CODE (alias_decl), target,
5523 TREE_TYPE (alias_decl));
5525 DECL_EXTERNAL (decl) = 1;
5526 TREE_PUBLIC (decl) = 1;
5527 DECL_ARTIFICIAL (decl) = 1;
5528 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5529 TREE_USED (decl) = 1;
5532 weak_finish_1 (decl);
5533 # endif
5535 #endif
5538 tree *p;
5539 tree t2;
5541 /* Remove the alias and the target from the pending weak list
5542 so that we do not emit any .weak directives for the former,
5543 nor multiple .weak directives for the latter. */
5544 for (p = &weak_decls; (t2 = *p) ; )
5546 if (TREE_VALUE (t2) == alias_decl
5547 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5548 *p = TREE_CHAIN (t2);
5549 else
5550 p = &TREE_CHAIN (t2);
5553 /* Remove other weakrefs to the same target, to speed things up. */
5554 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5556 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5557 *p = TREE_CHAIN (t2);
5558 else
5559 p = &TREE_CHAIN (t2);
5564 for (t = weak_decls; t; t = TREE_CHAIN (t))
5566 tree decl = TREE_VALUE (t);
5568 weak_finish_1 (decl);
5572 /* Emit the assembly bits to indicate that DECL is globally visible. */
5574 static void
5575 globalize_decl (tree decl)
5578 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5579 if (DECL_WEAK (decl))
5581 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5582 tree *p, t;
5584 #ifdef ASM_WEAKEN_DECL
5585 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5586 #else
5587 ASM_WEAKEN_LABEL (asm_out_file, name);
5588 #endif
5590 /* Remove this function from the pending weak list so that
5591 we do not emit multiple .weak directives for it. */
5592 for (p = &weak_decls; (t = *p) ; )
5594 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5595 *p = TREE_CHAIN (t);
5596 else
5597 p = &TREE_CHAIN (t);
5600 /* Remove weakrefs to the same target from the pending weakref
5601 list, for the same reason. */
5602 for (p = &weakref_targets; (t = *p) ; )
5604 if (DECL_ASSEMBLER_NAME (decl)
5605 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5606 *p = TREE_CHAIN (t);
5607 else
5608 p = &TREE_CHAIN (t);
5611 return;
5613 #endif
5615 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5618 vec<alias_pair, va_gc> *alias_pairs;
5620 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5621 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5622 tree node is DECL to have the value of the tree node TARGET. */
5624 void
5625 do_assemble_alias (tree decl, tree target)
5627 tree id;
5629 /* Emulated TLS had better not get this var. */
5630 gcc_assert (!(!targetm.have_tls
5631 && TREE_CODE (decl) == VAR_DECL
5632 && DECL_THREAD_LOCAL_P (decl)));
5634 if (TREE_ASM_WRITTEN (decl))
5635 return;
5637 id = DECL_ASSEMBLER_NAME (decl);
5638 ultimate_transparent_alias_target (&id);
5639 ultimate_transparent_alias_target (&target);
5641 /* We must force creation of DECL_RTL for debug info generation, even though
5642 we don't use it here. */
5643 make_decl_rtl (decl);
5645 TREE_ASM_WRITTEN (decl) = 1;
5646 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5647 TREE_ASM_WRITTEN (id) = 1;
5649 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5651 if (!TREE_SYMBOL_REFERENCED (target))
5652 weakref_targets = tree_cons (decl, target, weakref_targets);
5654 #ifdef ASM_OUTPUT_WEAKREF
5655 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5656 IDENTIFIER_POINTER (id),
5657 IDENTIFIER_POINTER (target));
5658 #else
5659 if (!TARGET_SUPPORTS_WEAK)
5661 error_at (DECL_SOURCE_LOCATION (decl),
5662 "weakref is not supported in this configuration");
5663 return;
5665 #endif
5666 return;
5669 #ifdef ASM_OUTPUT_DEF
5670 tree orig_decl = decl;
5672 if (TREE_CODE (decl) == FUNCTION_DECL
5673 && cgraph_node::get (decl)->instrumentation_clone
5674 && cgraph_node::get (decl)->instrumented_version)
5675 orig_decl = cgraph_node::get (decl)->instrumented_version->decl;
5677 /* Make name accessible from other files, if appropriate. */
5679 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5681 globalize_decl (decl);
5682 maybe_assemble_visibility (decl);
5684 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5686 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5687 if (targetm.has_ifunc_p ())
5688 ASM_OUTPUT_TYPE_DIRECTIVE
5689 (asm_out_file, IDENTIFIER_POINTER (id),
5690 IFUNC_ASM_TYPE);
5691 else
5692 #endif
5693 error_at (DECL_SOURCE_LOCATION (decl),
5694 "ifunc is not supported on this target");
5697 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5698 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5699 # else
5700 ASM_OUTPUT_DEF (asm_out_file,
5701 IDENTIFIER_POINTER (id),
5702 IDENTIFIER_POINTER (target));
5703 # endif
5704 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5706 const char *name;
5707 tree *p, t;
5709 name = IDENTIFIER_POINTER (id);
5710 # ifdef ASM_WEAKEN_DECL
5711 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5712 # else
5713 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5714 # endif
5715 /* Remove this function from the pending weak list so that
5716 we do not emit multiple .weak directives for it. */
5717 for (p = &weak_decls; (t = *p) ; )
5718 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5719 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5720 *p = TREE_CHAIN (t);
5721 else
5722 p = &TREE_CHAIN (t);
5724 /* Remove weakrefs to the same target from the pending weakref
5725 list, for the same reason. */
5726 for (p = &weakref_targets; (t = *p) ; )
5728 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5729 *p = TREE_CHAIN (t);
5730 else
5731 p = &TREE_CHAIN (t);
5734 #endif
5737 /* Emit an assembler directive to make the symbol for DECL an alias to
5738 the symbol for TARGET. */
5740 void
5741 assemble_alias (tree decl, tree target)
5743 tree target_decl;
5745 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5747 tree alias = DECL_ASSEMBLER_NAME (decl);
5749 ultimate_transparent_alias_target (&target);
5751 if (alias == target)
5752 error ("weakref %q+D ultimately targets itself", decl);
5753 if (TREE_PUBLIC (decl))
5754 error ("weakref %q+D must have static linkage", decl);
5756 else
5758 #if !defined (ASM_OUTPUT_DEF)
5759 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5760 error_at (DECL_SOURCE_LOCATION (decl),
5761 "alias definitions not supported in this configuration");
5762 TREE_ASM_WRITTEN (decl) = 1;
5763 return;
5764 # else
5765 if (!DECL_WEAK (decl))
5767 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5768 error_at (DECL_SOURCE_LOCATION (decl),
5769 "ifunc is not supported in this configuration");
5770 else
5771 error_at (DECL_SOURCE_LOCATION (decl),
5772 "only weak aliases are supported in this configuration");
5773 TREE_ASM_WRITTEN (decl) = 1;
5774 return;
5776 # endif
5777 #endif
5779 TREE_USED (decl) = 1;
5781 /* Allow aliases to aliases. */
5782 if (TREE_CODE (decl) == FUNCTION_DECL)
5783 cgraph_node::get_create (decl)->alias = true;
5784 else
5785 varpool_node::get_create (decl)->alias = true;
5787 /* If the target has already been emitted, we don't have to queue the
5788 alias. This saves a tad of memory. */
5789 if (symtab->global_info_ready)
5790 target_decl = find_decl (target);
5791 else
5792 target_decl= NULL;
5793 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5794 || symtab->state >= EXPANSION)
5795 do_assemble_alias (decl, target);
5796 else
5798 alias_pair p = {decl, target};
5799 vec_safe_push (alias_pairs, p);
5803 /* Record and output a table of translations from original function
5804 to its transaction aware clone. Note that tm_pure functions are
5805 considered to be their own clone. */
5807 struct tm_clone_hasher : ggc_cache_hasher<tree_map *>
5809 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
5810 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
5812 static void
5813 handle_cache_entry (tree_map *&e)
5815 extern void gt_ggc_mx (tree_map *&);
5816 if (e == HTAB_EMPTY_ENTRY || e == HTAB_DELETED_ENTRY)
5817 return;
5818 else if (ggc_marked_p (e->base.from))
5819 gt_ggc_mx (e);
5820 else
5821 e = static_cast<tree_map *> (HTAB_DELETED_ENTRY);
5825 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
5827 void
5828 record_tm_clone_pair (tree o, tree n)
5830 struct tree_map **slot, *h;
5832 if (tm_clone_hash == NULL)
5833 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
5835 h = ggc_alloc<tree_map> ();
5836 h->hash = htab_hash_pointer (o);
5837 h->base.from = o;
5838 h->to = n;
5840 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
5841 *slot = h;
5844 tree
5845 get_tm_clone_pair (tree o)
5847 if (tm_clone_hash)
5849 struct tree_map *h, in;
5851 in.base.from = o;
5852 in.hash = htab_hash_pointer (o);
5853 h = tm_clone_hash->find_with_hash (&in, in.hash);
5854 if (h)
5855 return h->to;
5857 return NULL_TREE;
5860 typedef struct tm_alias_pair
5862 unsigned int uid;
5863 tree from;
5864 tree to;
5865 } tm_alias_pair;
5868 /* Dump the actual pairs to the .tm_clone_table section. */
5870 static void
5871 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5873 unsigned i;
5874 tm_alias_pair *p;
5875 bool switched = false;
5877 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5879 tree src = p->from;
5880 tree dst = p->to;
5881 struct cgraph_node *src_n = cgraph_node::get (src);
5882 struct cgraph_node *dst_n = cgraph_node::get (dst);
5884 /* The function ipa_tm_create_version() marks the clone as needed if
5885 the original function was needed. But we also mark the clone as
5886 needed if we ever called the clone indirectly through
5887 TM_GETTMCLONE. If neither of these are true, we didn't generate
5888 a clone, and we didn't call it indirectly... no sense keeping it
5889 in the clone table. */
5890 if (!dst_n || !dst_n->definition)
5891 continue;
5893 /* This covers the case where we have optimized the original
5894 function away, and only access the transactional clone. */
5895 if (!src_n || !src_n->definition)
5896 continue;
5898 if (!switched)
5900 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5901 assemble_align (POINTER_SIZE);
5902 switched = true;
5905 assemble_integer (XEXP (DECL_RTL (src), 0),
5906 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5907 assemble_integer (XEXP (DECL_RTL (dst), 0),
5908 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5912 /* Provide a default for the tm_clone_table section. */
5914 section *
5915 default_clone_table_section (void)
5917 return get_named_section (NULL, ".tm_clone_table", 3);
5920 /* Helper comparison function for qsorting by the DECL_UID stored in
5921 alias_pair->emitted_diags. */
5923 static int
5924 tm_alias_pair_cmp (const void *x, const void *y)
5926 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5927 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5928 if (p1->uid < p2->uid)
5929 return -1;
5930 if (p1->uid > p2->uid)
5931 return 1;
5932 return 0;
5935 void
5936 finish_tm_clone_pairs (void)
5938 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5940 if (tm_clone_hash == NULL)
5941 return;
5943 /* We need a determenistic order for the .tm_clone_table, otherwise
5944 we will get bootstrap comparison failures, so dump the hash table
5945 to a vector, sort it, and dump the vector. */
5947 /* Dump the hashtable to a vector. */
5948 tree_map *map;
5949 hash_table<tm_clone_hasher>::iterator iter;
5950 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
5952 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5953 tm_alias_pairs.safe_push (p);
5955 /* Sort it. */
5956 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5958 /* Dump it. */
5959 dump_tm_clone_pairs (tm_alias_pairs);
5961 tm_clone_hash->empty ();
5962 tm_clone_hash = NULL;
5963 tm_alias_pairs.release ();
5967 /* Emit an assembler directive to set symbol for DECL visibility to
5968 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5970 void
5971 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5972 int vis ATTRIBUTE_UNUSED)
5974 #ifdef HAVE_GAS_HIDDEN
5975 static const char * const visibility_types[] = {
5976 NULL, "protected", "hidden", "internal"
5979 const char *name, *type;
5980 tree id;
5982 id = DECL_ASSEMBLER_NAME (decl);
5983 ultimate_transparent_alias_target (&id);
5984 name = IDENTIFIER_POINTER (id);
5986 type = visibility_types[vis];
5988 fprintf (asm_out_file, "\t.%s\t", type);
5989 assemble_name (asm_out_file, name);
5990 fprintf (asm_out_file, "\n");
5991 #else
5992 if (!DECL_ARTIFICIAL (decl))
5993 warning (OPT_Wattributes, "visibility attribute not supported "
5994 "in this configuration; ignored");
5995 #endif
5998 /* A helper function to call assemble_visibility when needed for a decl. */
6001 maybe_assemble_visibility (tree decl)
6003 enum symbol_visibility vis = DECL_VISIBILITY (decl);
6005 if (TREE_CODE (decl) == FUNCTION_DECL
6006 && cgraph_node::get (decl)
6007 && cgraph_node::get (decl)->instrumentation_clone
6008 && cgraph_node::get (decl)->instrumented_version)
6009 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
6011 if (vis != VISIBILITY_DEFAULT)
6013 targetm.asm_out.assemble_visibility (decl, vis);
6014 return 1;
6016 else
6017 return 0;
6020 /* Returns 1 if the target configuration supports defining public symbols
6021 so that one of them will be chosen at link time instead of generating a
6022 multiply-defined symbol error, whether through the use of weak symbols or
6023 a target-specific mechanism for having duplicates discarded. */
6026 supports_one_only (void)
6028 if (SUPPORTS_ONE_ONLY)
6029 return 1;
6030 return TARGET_SUPPORTS_WEAK;
6033 /* Set up DECL as a public symbol that can be defined in multiple
6034 translation units without generating a linker error. */
6036 void
6037 make_decl_one_only (tree decl, tree comdat_group)
6039 struct symtab_node *symbol;
6040 gcc_assert (TREE_CODE (decl) == VAR_DECL
6041 || TREE_CODE (decl) == FUNCTION_DECL);
6043 TREE_PUBLIC (decl) = 1;
6045 if (TREE_CODE (decl) == VAR_DECL)
6046 symbol = varpool_node::get_create (decl);
6047 else
6048 symbol = cgraph_node::get_create (decl);
6050 if (SUPPORTS_ONE_ONLY)
6052 #ifdef MAKE_DECL_ONE_ONLY
6053 MAKE_DECL_ONE_ONLY (decl);
6054 #endif
6055 symbol->set_comdat_group (comdat_group);
6057 else if (TREE_CODE (decl) == VAR_DECL
6058 && (DECL_INITIAL (decl) == 0
6059 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6060 DECL_COMMON (decl) = 1;
6061 else
6063 gcc_assert (TARGET_SUPPORTS_WEAK);
6064 DECL_WEAK (decl) = 1;
6068 void
6069 init_varasm_once (void)
6071 section_htab = hash_table<section_hasher>::create_ggc (31);
6072 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6073 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6075 shared_constant_pool = create_constant_pool ();
6077 #ifdef TEXT_SECTION_ASM_OP
6078 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6079 TEXT_SECTION_ASM_OP);
6080 #endif
6082 #ifdef DATA_SECTION_ASM_OP
6083 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6084 DATA_SECTION_ASM_OP);
6085 #endif
6087 #ifdef SDATA_SECTION_ASM_OP
6088 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6089 SDATA_SECTION_ASM_OP);
6090 #endif
6092 #ifdef READONLY_DATA_SECTION_ASM_OP
6093 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6094 READONLY_DATA_SECTION_ASM_OP);
6095 #endif
6097 #ifdef CTORS_SECTION_ASM_OP
6098 ctors_section = get_unnamed_section (0, output_section_asm_op,
6099 CTORS_SECTION_ASM_OP);
6100 #endif
6102 #ifdef DTORS_SECTION_ASM_OP
6103 dtors_section = get_unnamed_section (0, output_section_asm_op,
6104 DTORS_SECTION_ASM_OP);
6105 #endif
6107 #ifdef BSS_SECTION_ASM_OP
6108 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6109 output_section_asm_op,
6110 BSS_SECTION_ASM_OP);
6111 #endif
6113 #ifdef SBSS_SECTION_ASM_OP
6114 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6115 output_section_asm_op,
6116 SBSS_SECTION_ASM_OP);
6117 #endif
6119 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6120 | SECTION_COMMON, emit_tls_common);
6121 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6122 | SECTION_COMMON, emit_local);
6123 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6124 | SECTION_COMMON, emit_common);
6126 #if defined ASM_OUTPUT_ALIGNED_BSS
6127 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6128 emit_bss);
6129 #endif
6131 targetm.asm_out.init_sections ();
6133 if (readonly_data_section == NULL)
6134 readonly_data_section = text_section;
6136 #ifdef ASM_OUTPUT_EXTERNAL
6137 pending_assemble_externals_set = new hash_set<tree>;
6138 #endif
6141 enum tls_model
6142 decl_default_tls_model (const_tree decl)
6144 enum tls_model kind;
6145 bool is_local;
6147 is_local = targetm.binds_local_p (decl);
6148 if (!flag_shlib)
6150 if (is_local)
6151 kind = TLS_MODEL_LOCAL_EXEC;
6152 else
6153 kind = TLS_MODEL_INITIAL_EXEC;
6156 /* Local dynamic is inefficient when we're not combining the
6157 parts of the address. */
6158 else if (optimize && is_local)
6159 kind = TLS_MODEL_LOCAL_DYNAMIC;
6160 else
6161 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6162 if (kind < flag_tls_default)
6163 kind = flag_tls_default;
6165 return kind;
6168 /* Select a set of attributes for section NAME based on the properties
6169 of DECL and whether or not RELOC indicates that DECL's initializer
6170 might contain runtime relocations.
6172 We make the section read-only and executable for a function decl,
6173 read-only for a const data decl, and writable for a non-const data decl. */
6175 unsigned int
6176 default_section_type_flags (tree decl, const char *name, int reloc)
6178 unsigned int flags;
6180 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6181 flags = SECTION_CODE;
6182 else if (decl)
6184 enum section_category category
6185 = categorize_decl_for_section (decl, reloc);
6186 if (decl_readonly_section_1 (category))
6187 flags = 0;
6188 else if (category == SECCAT_DATA_REL_RO
6189 || category == SECCAT_DATA_REL_RO_LOCAL)
6190 flags = SECTION_WRITE | SECTION_RELRO;
6191 else
6192 flags = SECTION_WRITE;
6194 else
6196 flags = SECTION_WRITE;
6197 if (strcmp (name, ".data.rel.ro") == 0
6198 || strcmp (name, ".data.rel.ro.local") == 0)
6199 flags |= SECTION_RELRO;
6202 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6203 flags |= SECTION_LINKONCE;
6205 if (strcmp (name, ".vtable_map_vars") == 0)
6206 flags |= SECTION_LINKONCE;
6208 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6209 flags |= SECTION_TLS | SECTION_WRITE;
6211 if (strcmp (name, ".bss") == 0
6212 || strncmp (name, ".bss.", 5) == 0
6213 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6214 || strcmp (name, ".persistent.bss") == 0
6215 || strcmp (name, ".sbss") == 0
6216 || strncmp (name, ".sbss.", 6) == 0
6217 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6218 flags |= SECTION_BSS;
6220 if (strcmp (name, ".tdata") == 0
6221 || strncmp (name, ".tdata.", 7) == 0
6222 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6223 flags |= SECTION_TLS;
6225 if (strcmp (name, ".tbss") == 0
6226 || strncmp (name, ".tbss.", 6) == 0
6227 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6228 flags |= SECTION_TLS | SECTION_BSS;
6230 /* These three sections have special ELF types. They are neither
6231 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6232 want to print a section type (@progbits or @nobits). If someone
6233 is silly enough to emit code or TLS variables to one of these
6234 sections, then don't handle them specially. */
6235 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6236 && (strcmp (name, ".init_array") == 0
6237 || strcmp (name, ".fini_array") == 0
6238 || strcmp (name, ".preinit_array") == 0))
6239 flags |= SECTION_NOTYPE;
6241 return flags;
6244 /* Return true if the target supports some form of global BSS,
6245 either through bss_noswitch_section, or by selecting a BSS
6246 section in TARGET_ASM_SELECT_SECTION. */
6248 bool
6249 have_global_bss_p (void)
6251 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6254 /* Output assembly to switch to section NAME with attribute FLAGS.
6255 Four variants for common object file formats. */
6257 void
6258 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6259 unsigned int flags ATTRIBUTE_UNUSED,
6260 tree decl ATTRIBUTE_UNUSED)
6262 /* Some object formats don't support named sections at all. The
6263 front-end should already have flagged this as an error. */
6264 gcc_unreachable ();
6267 #ifndef TLS_SECTION_ASM_FLAG
6268 #define TLS_SECTION_ASM_FLAG 'T'
6269 #endif
6271 void
6272 default_elf_asm_named_section (const char *name, unsigned int flags,
6273 tree decl ATTRIBUTE_UNUSED)
6275 char flagchars[10], *f = flagchars;
6277 /* If we have already declared this section, we can use an
6278 abbreviated form to switch back to it -- unless this section is
6279 part of a COMDAT groups, in which case GAS requires the full
6280 declaration every time. */
6281 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6282 && (flags & SECTION_DECLARED))
6284 fprintf (asm_out_file, "\t.section\t%s\n", name);
6285 return;
6288 if (!(flags & SECTION_DEBUG))
6289 *f++ = 'a';
6290 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6291 if (flags & SECTION_EXCLUDE)
6292 *f++ = 'e';
6293 #endif
6294 if (flags & SECTION_WRITE)
6295 *f++ = 'w';
6296 if (flags & SECTION_CODE)
6297 *f++ = 'x';
6298 if (flags & SECTION_SMALL)
6299 *f++ = 's';
6300 if (flags & SECTION_MERGE)
6301 *f++ = 'M';
6302 if (flags & SECTION_STRINGS)
6303 *f++ = 'S';
6304 if (flags & SECTION_TLS)
6305 *f++ = TLS_SECTION_ASM_FLAG;
6306 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6307 *f++ = 'G';
6308 *f = '\0';
6310 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6312 if (!(flags & SECTION_NOTYPE))
6314 const char *type;
6315 const char *format;
6317 if (flags & SECTION_BSS)
6318 type = "nobits";
6319 else
6320 type = "progbits";
6322 format = ",@%s";
6323 /* On platforms that use "@" as the assembly comment character,
6324 use "%" instead. */
6325 if (strcmp (ASM_COMMENT_START, "@") == 0)
6326 format = ",%%%s";
6327 fprintf (asm_out_file, format, type);
6329 if (flags & SECTION_ENTSIZE)
6330 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6331 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6333 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6334 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6335 else
6336 fprintf (asm_out_file, ",%s,comdat",
6337 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6341 putc ('\n', asm_out_file);
6344 void
6345 default_coff_asm_named_section (const char *name, unsigned int flags,
6346 tree decl ATTRIBUTE_UNUSED)
6348 char flagchars[8], *f = flagchars;
6350 if (flags & SECTION_WRITE)
6351 *f++ = 'w';
6352 if (flags & SECTION_CODE)
6353 *f++ = 'x';
6354 *f = '\0';
6356 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6359 void
6360 default_pe_asm_named_section (const char *name, unsigned int flags,
6361 tree decl)
6363 default_coff_asm_named_section (name, flags, decl);
6365 if (flags & SECTION_LINKONCE)
6367 /* Functions may have been compiled at various levels of
6368 optimization so we can't use `same_size' here.
6369 Instead, have the linker pick one. */
6370 fprintf (asm_out_file, "\t.linkonce %s\n",
6371 (flags & SECTION_CODE ? "discard" : "same_size"));
6375 /* The lame default section selector. */
6377 section *
6378 default_select_section (tree decl, int reloc,
6379 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6381 if (DECL_P (decl))
6383 if (decl_readonly_section (decl, reloc))
6384 return readonly_data_section;
6386 else if (TREE_CODE (decl) == CONSTRUCTOR)
6388 if (! ((flag_pic && reloc)
6389 || !TREE_READONLY (decl)
6390 || TREE_SIDE_EFFECTS (decl)
6391 || !TREE_CONSTANT (decl)))
6392 return readonly_data_section;
6394 else if (TREE_CODE (decl) == STRING_CST)
6395 return readonly_data_section;
6396 else if (! (flag_pic && reloc))
6397 return readonly_data_section;
6399 return data_section;
6402 enum section_category
6403 categorize_decl_for_section (const_tree decl, int reloc)
6405 enum section_category ret;
6407 if (TREE_CODE (decl) == FUNCTION_DECL)
6408 return SECCAT_TEXT;
6409 else if (TREE_CODE (decl) == STRING_CST)
6411 if ((flag_sanitize & SANITIZE_ADDRESS)
6412 && asan_protect_global (CONST_CAST_TREE (decl)))
6413 /* or !flag_merge_constants */
6414 return SECCAT_RODATA;
6415 else
6416 return SECCAT_RODATA_MERGE_STR;
6418 else if (TREE_CODE (decl) == VAR_DECL)
6420 if (bss_initializer_p (decl))
6421 ret = SECCAT_BSS;
6422 else if (! TREE_READONLY (decl)
6423 || TREE_SIDE_EFFECTS (decl)
6424 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6426 /* Here the reloc_rw_mask is not testing whether the section should
6427 be read-only or not, but whether the dynamic link will have to
6428 do something. If so, we wish to segregate the data in order to
6429 minimize cache misses inside the dynamic linker. */
6430 if (reloc & targetm.asm_out.reloc_rw_mask ())
6431 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6432 else
6433 ret = SECCAT_DATA;
6435 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6436 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6437 else if (reloc || flag_merge_constants < 2
6438 || ((flag_sanitize & SANITIZE_ADDRESS)
6439 && asan_protect_global (CONST_CAST_TREE (decl))))
6440 /* C and C++ don't allow different variables to share the same
6441 location. -fmerge-all-constants allows even that (at the
6442 expense of not conforming). */
6443 ret = SECCAT_RODATA;
6444 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6445 ret = SECCAT_RODATA_MERGE_STR_INIT;
6446 else
6447 ret = SECCAT_RODATA_MERGE_CONST;
6449 else if (TREE_CODE (decl) == CONSTRUCTOR)
6451 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6452 || TREE_SIDE_EFFECTS (decl)
6453 || ! TREE_CONSTANT (decl))
6454 ret = SECCAT_DATA;
6455 else
6456 ret = SECCAT_RODATA;
6458 else
6459 ret = SECCAT_RODATA;
6461 /* There are no read-only thread-local sections. */
6462 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6464 /* Note that this would be *just* SECCAT_BSS, except that there's
6465 no concept of a read-only thread-local-data section. */
6466 if (ret == SECCAT_BSS
6467 || (flag_zero_initialized_in_bss
6468 && initializer_zerop (DECL_INITIAL (decl))))
6469 ret = SECCAT_TBSS;
6470 else
6471 ret = SECCAT_TDATA;
6474 /* If the target uses small data sections, select it. */
6475 else if (targetm.in_small_data_p (decl))
6477 if (ret == SECCAT_BSS)
6478 ret = SECCAT_SBSS;
6479 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6480 ret = SECCAT_SRODATA;
6481 else
6482 ret = SECCAT_SDATA;
6485 return ret;
6488 static bool
6489 decl_readonly_section_1 (enum section_category category)
6491 switch (category)
6493 case SECCAT_RODATA:
6494 case SECCAT_RODATA_MERGE_STR:
6495 case SECCAT_RODATA_MERGE_STR_INIT:
6496 case SECCAT_RODATA_MERGE_CONST:
6497 case SECCAT_SRODATA:
6498 return true;
6499 default:
6500 return false;
6504 bool
6505 decl_readonly_section (const_tree decl, int reloc)
6507 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6510 /* Select a section based on the above categorization. */
6512 section *
6513 default_elf_select_section (tree decl, int reloc,
6514 unsigned HOST_WIDE_INT align)
6516 const char *sname;
6517 switch (categorize_decl_for_section (decl, reloc))
6519 case SECCAT_TEXT:
6520 /* We're not supposed to be called on FUNCTION_DECLs. */
6521 gcc_unreachable ();
6522 case SECCAT_RODATA:
6523 return readonly_data_section;
6524 case SECCAT_RODATA_MERGE_STR:
6525 return mergeable_string_section (decl, align, 0);
6526 case SECCAT_RODATA_MERGE_STR_INIT:
6527 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6528 case SECCAT_RODATA_MERGE_CONST:
6529 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6530 case SECCAT_SRODATA:
6531 sname = ".sdata2";
6532 break;
6533 case SECCAT_DATA:
6534 return data_section;
6535 case SECCAT_DATA_REL:
6536 sname = ".data.rel";
6537 break;
6538 case SECCAT_DATA_REL_LOCAL:
6539 sname = ".data.rel.local";
6540 break;
6541 case SECCAT_DATA_REL_RO:
6542 sname = ".data.rel.ro";
6543 break;
6544 case SECCAT_DATA_REL_RO_LOCAL:
6545 sname = ".data.rel.ro.local";
6546 break;
6547 case SECCAT_SDATA:
6548 sname = ".sdata";
6549 break;
6550 case SECCAT_TDATA:
6551 sname = ".tdata";
6552 break;
6553 case SECCAT_BSS:
6554 if (bss_section)
6555 return bss_section;
6556 sname = ".bss";
6557 break;
6558 case SECCAT_SBSS:
6559 sname = ".sbss";
6560 break;
6561 case SECCAT_TBSS:
6562 sname = ".tbss";
6563 break;
6564 default:
6565 gcc_unreachable ();
6568 return get_named_section (decl, sname, reloc);
6571 /* Construct a unique section name based on the decl name and the
6572 categorization performed above. */
6574 void
6575 default_unique_section (tree decl, int reloc)
6577 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6578 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6579 const char *prefix, *name, *linkonce;
6580 char *string;
6581 tree id;
6583 switch (categorize_decl_for_section (decl, reloc))
6585 case SECCAT_TEXT:
6586 prefix = one_only ? ".t" : ".text";
6587 break;
6588 case SECCAT_RODATA:
6589 case SECCAT_RODATA_MERGE_STR:
6590 case SECCAT_RODATA_MERGE_STR_INIT:
6591 case SECCAT_RODATA_MERGE_CONST:
6592 prefix = one_only ? ".r" : ".rodata";
6593 break;
6594 case SECCAT_SRODATA:
6595 prefix = one_only ? ".s2" : ".sdata2";
6596 break;
6597 case SECCAT_DATA:
6598 prefix = one_only ? ".d" : ".data";
6599 break;
6600 case SECCAT_DATA_REL:
6601 prefix = one_only ? ".d.rel" : ".data.rel";
6602 break;
6603 case SECCAT_DATA_REL_LOCAL:
6604 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6605 break;
6606 case SECCAT_DATA_REL_RO:
6607 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6608 break;
6609 case SECCAT_DATA_REL_RO_LOCAL:
6610 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6611 break;
6612 case SECCAT_SDATA:
6613 prefix = one_only ? ".s" : ".sdata";
6614 break;
6615 case SECCAT_BSS:
6616 prefix = one_only ? ".b" : ".bss";
6617 break;
6618 case SECCAT_SBSS:
6619 prefix = one_only ? ".sb" : ".sbss";
6620 break;
6621 case SECCAT_TDATA:
6622 prefix = one_only ? ".td" : ".tdata";
6623 break;
6624 case SECCAT_TBSS:
6625 prefix = one_only ? ".tb" : ".tbss";
6626 break;
6627 default:
6628 gcc_unreachable ();
6631 id = DECL_ASSEMBLER_NAME (decl);
6632 ultimate_transparent_alias_target (&id);
6633 name = IDENTIFIER_POINTER (id);
6634 name = targetm.strip_name_encoding (name);
6636 /* If we're using one_only, then there needs to be a .gnu.linkonce
6637 prefix to the section name. */
6638 linkonce = one_only ? ".gnu.linkonce" : "";
6640 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6642 set_decl_section_name (decl, string);
6645 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6647 static int
6648 compute_reloc_for_rtx_1 (const_rtx x)
6650 switch (GET_CODE (x))
6652 case SYMBOL_REF:
6653 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6654 case LABEL_REF:
6655 return 1;
6656 default:
6657 return 0;
6661 /* Like compute_reloc_for_constant, except for an RTX. The return value
6662 is a mask for which bit 1 indicates a global relocation, and bit 0
6663 indicates a local relocation. */
6665 static int
6666 compute_reloc_for_rtx (const_rtx x)
6668 switch (GET_CODE (x))
6670 case SYMBOL_REF:
6671 case LABEL_REF:
6672 return compute_reloc_for_rtx_1 (x);
6674 case CONST:
6676 int reloc = 0;
6677 subrtx_iterator::array_type array;
6678 FOR_EACH_SUBRTX (iter, array, x, ALL)
6679 reloc |= compute_reloc_for_rtx_1 (*iter);
6680 return reloc;
6683 default:
6684 return 0;
6688 section *
6689 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6690 rtx x,
6691 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6693 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6694 return data_section;
6695 else
6696 return readonly_data_section;
6699 section *
6700 default_elf_select_rtx_section (machine_mode mode, rtx x,
6701 unsigned HOST_WIDE_INT align)
6703 int reloc = compute_reloc_for_rtx (x);
6705 /* ??? Handle small data here somehow. */
6707 if (reloc & targetm.asm_out.reloc_rw_mask ())
6709 if (reloc == 1)
6710 return get_named_section (NULL, ".data.rel.ro.local", 1);
6711 else
6712 return get_named_section (NULL, ".data.rel.ro", 3);
6715 return mergeable_constant_section (mode, align, 0);
6718 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6720 void
6721 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6723 rtx symbol;
6724 int flags;
6726 /* Careful not to prod global register variables. */
6727 if (!MEM_P (rtl))
6728 return;
6729 symbol = XEXP (rtl, 0);
6730 if (GET_CODE (symbol) != SYMBOL_REF)
6731 return;
6733 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6734 if (TREE_CODE (decl) == FUNCTION_DECL)
6735 flags |= SYMBOL_FLAG_FUNCTION;
6736 if (targetm.binds_local_p (decl))
6737 flags |= SYMBOL_FLAG_LOCAL;
6738 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6739 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6740 else if (targetm.in_small_data_p (decl))
6741 flags |= SYMBOL_FLAG_SMALL;
6742 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6743 being PUBLIC, the thing *must* be defined in this translation unit.
6744 Prevent this buglet from being propagated into rtl code as well. */
6745 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6746 flags |= SYMBOL_FLAG_EXTERNAL;
6748 SYMBOL_REF_FLAGS (symbol) = flags;
6751 /* By default, we do nothing for encode_section_info, so we need not
6752 do anything but discard the '*' marker. */
6754 const char *
6755 default_strip_name_encoding (const char *str)
6757 return str + (*str == '*');
6760 #ifdef ASM_OUTPUT_DEF
6761 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6762 anchor relative to ".", the current section position. */
6764 void
6765 default_asm_output_anchor (rtx symbol)
6767 char buffer[100];
6769 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6770 SYMBOL_REF_BLOCK_OFFSET (symbol));
6771 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6773 #endif
6775 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6777 bool
6778 default_use_anchors_for_symbol_p (const_rtx symbol)
6780 section *sect;
6781 tree decl;
6783 /* Don't use anchors for mergeable sections. The linker might move
6784 the objects around. */
6785 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6786 if (sect->common.flags & SECTION_MERGE)
6787 return false;
6789 /* Don't use anchors for small data sections. The small data register
6790 acts as an anchor for such sections. */
6791 if (sect->common.flags & SECTION_SMALL)
6792 return false;
6794 decl = SYMBOL_REF_DECL (symbol);
6795 if (decl && DECL_P (decl))
6797 /* Don't use section anchors for decls that might be defined or
6798 usurped by other modules. */
6799 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6800 return false;
6802 /* Don't use section anchors for decls that will be placed in a
6803 small data section. */
6804 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6805 one above. The problem is that we only use SECTION_SMALL for
6806 sections that should be marked as small in the section directive. */
6807 if (targetm.in_small_data_p (decl))
6808 return false;
6810 return true;
6813 /* Return true when RESOLUTION indicate that symbol will be bound to the
6814 definition provided by current .o file. */
6816 static bool
6817 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6819 return (resolution == LDPR_PREVAILING_DEF
6820 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6821 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6824 /* Return true when RESOLUTION indicate that symbol will be bound locally
6825 within current executable or DSO. */
6827 static bool
6828 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6830 return (resolution == LDPR_PREVAILING_DEF
6831 || resolution == LDPR_PREVAILING_DEF_IRONLY
6832 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6833 || resolution == LDPR_PREEMPTED_REG
6834 || resolution == LDPR_PREEMPTED_IR
6835 || resolution == LDPR_RESOLVED_IR
6836 || resolution == LDPR_RESOLVED_EXEC);
6839 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
6840 uninitialized common symbol in the executable will still be defined
6841 (through COPY relocation) in the executable. */
6843 bool
6844 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
6845 bool extern_protected_data, bool common_local_p)
6847 /* A non-decl is an entry in the constant pool. */
6848 if (!DECL_P (exp))
6849 return true;
6851 /* Weakrefs may not bind locally, even though the weakref itself is always
6852 static and therefore local. Similarly, the resolver for ifunc functions
6853 might resolve to a non-local function.
6854 FIXME: We can resolve the weakref case more curefuly by looking at the
6855 weakref alias. */
6856 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6857 || (TREE_CODE (exp) == FUNCTION_DECL
6858 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6859 return false;
6861 /* Static variables are always local. */
6862 if (! TREE_PUBLIC (exp))
6863 return true;
6865 /* With resolution file in hand, take look into resolutions.
6866 We can't just return true for resolved_locally symbols,
6867 because dynamic linking might overwrite symbols
6868 in shared libraries. */
6869 bool resolved_locally = false;
6871 bool uninited_common = (DECL_COMMON (exp)
6872 && (DECL_INITIAL (exp) == NULL
6873 || (!in_lto_p
6874 && DECL_INITIAL (exp) == error_mark_node)));
6876 /* A non-external variable is defined locally only if it isn't
6877 uninitialized COMMON variable or common_local_p is true. */
6878 bool defined_locally = (!DECL_EXTERNAL (exp)
6879 && (!uninited_common || common_local_p));
6880 if (symtab_node *node = symtab_node::get (exp))
6882 if (node->in_other_partition)
6883 defined_locally = true;
6884 if (resolution_to_local_definition_p (node->resolution))
6885 defined_locally = resolved_locally = true;
6886 else if (resolution_local_p (node->resolution))
6887 resolved_locally = true;
6889 if (defined_locally && weak_dominate && !shlib)
6890 resolved_locally = true;
6892 /* Undefined weak symbols are never defined locally. */
6893 if (DECL_WEAK (exp) && !defined_locally)
6894 return false;
6896 /* A symbol is local if the user has said explicitly that it will be,
6897 or if we have a definition for the symbol. We cannot infer visibility
6898 for undefined symbols. */
6899 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
6900 && (TREE_CODE (exp) == FUNCTION_DECL
6901 || !extern_protected_data
6902 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
6903 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
6904 return true;
6906 /* If PIC, then assume that any global name can be overridden by
6907 symbols resolved from other modules. */
6908 if (shlib)
6909 return false;
6911 /* Variables defined outside this object might not be local. */
6912 if (DECL_EXTERNAL (exp) && !resolved_locally)
6913 return false;
6915 /* Non-dominant weak symbols are not defined locally. */
6916 if (DECL_WEAK (exp) && !resolved_locally)
6917 return false;
6919 /* Uninitialized COMMON variable may be unified with symbols
6920 resolved from other modules. */
6921 if (uninited_common && !resolved_locally)
6922 return false;
6924 /* Otherwise we're left with initialized (or non-common) global data
6925 which is of necessity defined locally. */
6926 return true;
6929 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6930 wrt cross-module name binding. */
6932 bool
6933 default_binds_local_p (const_tree exp)
6935 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
6938 /* Similar to default_binds_local_p, but common symbol may be local. */
6940 bool
6941 default_binds_local_p_2 (const_tree exp)
6943 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false,
6944 !flag_pic);
6947 bool
6948 default_binds_local_p_1 (const_tree exp, int shlib)
6950 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
6953 /* Return true when references to DECL must bind to current definition in
6954 final executable.
6956 The condition is usually equivalent to whether the function binds to the
6957 current module (shared library or executable), that is to binds_local_p.
6958 We use this fact to avoid need for another target hook and implement
6959 the logic using binds_local_p and just special cases where
6960 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6961 the weak definitions (that can be overwritten at linktime by other
6962 definition from different object file) and when resolution info is available
6963 we simply use the knowledge passed to us by linker plugin. */
6964 bool
6965 decl_binds_to_current_def_p (const_tree decl)
6967 gcc_assert (DECL_P (decl));
6968 if (!targetm.binds_local_p (decl))
6969 return false;
6970 if (!TREE_PUBLIC (decl))
6971 return true;
6973 /* When resolution is available, just use it. */
6974 if (symtab_node *node = symtab_node::get (decl))
6976 if (node->resolution != LDPR_UNKNOWN)
6977 return resolution_to_local_definition_p (node->resolution);
6980 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6981 binds locally but still can be overwritten), DECL_COMMON (can be merged
6982 with a non-common definition somewhere in the same module) or
6983 DECL_EXTERNAL.
6984 This rely on fact that binds_local_p behave as decl_replaceable_p
6985 for all other declaration types. */
6986 if (DECL_WEAK (decl))
6987 return false;
6988 if (DECL_COMMON (decl)
6989 && (DECL_INITIAL (decl) == NULL
6990 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6991 return false;
6992 if (DECL_EXTERNAL (decl))
6993 return false;
6994 return true;
6997 /* A replaceable function or variable is one which may be replaced
6998 at link-time with an entirely different definition, provided that the
6999 replacement has the same type. For example, functions declared
7000 with __attribute__((weak)) on most systems are replaceable.
7002 COMDAT functions are not replaceable, since all definitions of the
7003 function must be equivalent. It is important that COMDAT functions
7004 not be treated as replaceable so that use of C++ template
7005 instantiations is not penalized. */
7007 bool
7008 decl_replaceable_p (tree decl)
7010 gcc_assert (DECL_P (decl));
7011 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7012 return false;
7013 if (!flag_semantic_interposition
7014 && !DECL_WEAK (decl))
7015 return false;
7016 return !decl_binds_to_current_def_p (decl);
7019 /* Default function to output code that will globalize a label. A
7020 target must define GLOBAL_ASM_OP or provide its own function to
7021 globalize a label. */
7022 #ifdef GLOBAL_ASM_OP
7023 void
7024 default_globalize_label (FILE * stream, const char *name)
7026 fputs (GLOBAL_ASM_OP, stream);
7027 assemble_name (stream, name);
7028 putc ('\n', stream);
7030 #endif /* GLOBAL_ASM_OP */
7032 /* Default function to output code that will globalize a declaration. */
7033 void
7034 default_globalize_decl_name (FILE * stream, tree decl)
7036 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7037 targetm.asm_out.globalize_label (stream, name);
7040 /* Default function to output a label for unwind information. The
7041 default is to do nothing. A target that needs nonlocal labels for
7042 unwind information must provide its own function to do this. */
7043 void
7044 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7045 tree decl ATTRIBUTE_UNUSED,
7046 int for_eh ATTRIBUTE_UNUSED,
7047 int empty ATTRIBUTE_UNUSED)
7051 /* Default function to output a label to divide up the exception table.
7052 The default is to do nothing. A target that needs/wants to divide
7053 up the table must provide it's own function to do this. */
7054 void
7055 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7059 /* This is how to output an internal numbered label where PREFIX is
7060 the class of label and LABELNO is the number within the class. */
7062 void
7063 default_generate_internal_label (char *buf, const char *prefix,
7064 unsigned long labelno)
7066 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7069 /* This is how to output an internal numbered label where PREFIX is
7070 the class of label and LABELNO is the number within the class. */
7072 void
7073 default_internal_label (FILE *stream, const char *prefix,
7074 unsigned long labelno)
7076 char *const buf = (char *) alloca (40 + strlen (prefix));
7077 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7078 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7082 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7084 void
7085 default_asm_declare_constant_name (FILE *file, const char *name,
7086 const_tree exp ATTRIBUTE_UNUSED,
7087 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7089 assemble_label (file, name);
7092 /* This is the default behavior at the beginning of a file. It's
7093 controlled by two other target-hook toggles. */
7094 void
7095 default_file_start (void)
7097 if (targetm.asm_file_start_app_off
7098 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7099 fputs (ASM_APP_OFF, asm_out_file);
7101 if (targetm.asm_file_start_file_directive)
7103 /* LTO produced units have no meaningful main_input_filename. */
7104 if (in_lto_p)
7105 output_file_directive (asm_out_file, "<artificial>");
7106 else
7107 output_file_directive (asm_out_file, main_input_filename);
7111 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7112 which emits a special section directive used to indicate whether or
7113 not this object file needs an executable stack. This is primarily
7114 a GNU extension to ELF but could be used on other targets. */
7116 int trampolines_created;
7118 void
7119 file_end_indicate_exec_stack (void)
7121 unsigned int flags = SECTION_DEBUG;
7122 if (trampolines_created)
7123 flags |= SECTION_CODE;
7125 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7128 /* Emit a special section directive to indicate that this object file
7129 was compiled with -fsplit-stack. This is used to let the linker
7130 detect calls between split-stack code and non-split-stack code, so
7131 that it can modify the split-stack code to allocate a sufficiently
7132 large stack. We emit another special section if there are any
7133 functions in this file which have the no_split_stack attribute, to
7134 prevent the linker from warning about being unable to convert the
7135 functions if they call non-split-stack code. */
7137 void
7138 file_end_indicate_split_stack (void)
7140 if (flag_split_stack)
7142 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7143 NULL));
7144 if (saw_no_split_stack)
7145 switch_to_section (get_section (".note.GNU-no-split-stack",
7146 SECTION_DEBUG, NULL));
7150 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7151 a get_unnamed_section callback. */
7153 void
7154 output_section_asm_op (const void *directive)
7156 fprintf (asm_out_file, "%s\n", (const char *) directive);
7159 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7160 the current section is NEW_SECTION. */
7162 void
7163 switch_to_section (section *new_section)
7165 if (in_section == new_section)
7166 return;
7168 if (new_section->common.flags & SECTION_FORGET)
7169 in_section = NULL;
7170 else
7171 in_section = new_section;
7173 switch (SECTION_STYLE (new_section))
7175 case SECTION_NAMED:
7176 targetm.asm_out.named_section (new_section->named.name,
7177 new_section->named.common.flags,
7178 new_section->named.decl);
7179 break;
7181 case SECTION_UNNAMED:
7182 new_section->unnamed.callback (new_section->unnamed.data);
7183 break;
7185 case SECTION_NOSWITCH:
7186 gcc_unreachable ();
7187 break;
7190 new_section->common.flags |= SECTION_DECLARED;
7193 /* If block symbol SYMBOL has not yet been assigned an offset, place
7194 it at the end of its block. */
7196 void
7197 place_block_symbol (rtx symbol)
7199 unsigned HOST_WIDE_INT size, mask, offset;
7200 struct constant_descriptor_rtx *desc;
7201 unsigned int alignment;
7202 struct object_block *block;
7203 tree decl;
7205 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7206 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7207 return;
7209 /* Work out the symbol's size and alignment. */
7210 if (CONSTANT_POOL_ADDRESS_P (symbol))
7212 desc = SYMBOL_REF_CONSTANT (symbol);
7213 alignment = desc->align;
7214 size = GET_MODE_SIZE (desc->mode);
7216 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7218 decl = SYMBOL_REF_DECL (symbol);
7219 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7220 alignment = DECL_ALIGN (decl);
7221 size = get_constant_size (DECL_INITIAL (decl));
7222 if ((flag_sanitize & SANITIZE_ADDRESS)
7223 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7224 && asan_protect_global (DECL_INITIAL (decl)))
7225 size += asan_red_zone_size (size);
7227 else
7229 struct symtab_node *snode;
7230 decl = SYMBOL_REF_DECL (symbol);
7232 snode = symtab_node::get (decl);
7233 if (snode->alias)
7235 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7237 gcc_assert (MEM_P (target)
7238 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7239 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7240 target = XEXP (target, 0);
7241 place_block_symbol (target);
7242 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7243 return;
7245 alignment = get_variable_align (decl);
7246 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7247 if ((flag_sanitize & SANITIZE_ADDRESS)
7248 && asan_protect_global (decl))
7250 size += asan_red_zone_size (size);
7251 alignment = MAX (alignment,
7252 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7256 /* Calculate the object's offset from the start of the block. */
7257 block = SYMBOL_REF_BLOCK (symbol);
7258 mask = alignment / BITS_PER_UNIT - 1;
7259 offset = (block->size + mask) & ~mask;
7260 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7262 /* Record the block's new alignment and size. */
7263 block->alignment = MAX (block->alignment, alignment);
7264 block->size = offset + size;
7266 vec_safe_push (block->objects, symbol);
7269 /* Return the anchor that should be used to address byte offset OFFSET
7270 from the first object in BLOCK. MODEL is the TLS model used
7271 to access it. */
7274 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7275 enum tls_model model)
7277 char label[100];
7278 unsigned int begin, middle, end;
7279 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7280 rtx anchor;
7282 /* Work out the anchor's offset. Use an offset of 0 for the first
7283 anchor so that we don't pessimize the case where we take the address
7284 of a variable at the beginning of the block. This is particularly
7285 useful when a block has only one variable assigned to it.
7287 We try to place anchors RANGE bytes apart, so there can then be
7288 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7289 a ptr_mode offset. With some target settings, the lowest such
7290 anchor might be out of range for the lowest ptr_mode offset;
7291 likewise the highest anchor for the highest offset. Use anchors
7292 at the extreme ends of the ptr_mode range in such cases.
7294 All arithmetic uses unsigned integers in order to avoid
7295 signed overflow. */
7296 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7297 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7298 range = max_offset - min_offset + 1;
7299 if (range == 0)
7300 offset = 0;
7301 else
7303 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7304 if (offset < 0)
7306 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7307 delta -= delta % range;
7308 if (delta > bias)
7309 delta = bias;
7310 offset = (HOST_WIDE_INT) (-delta);
7312 else
7314 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7315 delta -= delta % range;
7316 if (delta > bias - 1)
7317 delta = bias - 1;
7318 offset = (HOST_WIDE_INT) delta;
7322 /* Do a binary search to see if there's already an anchor we can use.
7323 Set BEGIN to the new anchor's index if not. */
7324 begin = 0;
7325 end = vec_safe_length (block->anchors);
7326 while (begin != end)
7328 middle = (end + begin) / 2;
7329 anchor = (*block->anchors)[middle];
7330 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7331 end = middle;
7332 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7333 begin = middle + 1;
7334 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7335 end = middle;
7336 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7337 begin = middle + 1;
7338 else
7339 return anchor;
7342 /* Create a new anchor with a unique label. */
7343 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7344 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7345 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7346 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7348 /* Insert it at index BEGIN. */
7349 vec_safe_insert (block->anchors, begin, anchor);
7350 return anchor;
7353 /* Output the objects in BLOCK. */
7355 static void
7356 output_object_block (struct object_block *block)
7358 struct constant_descriptor_rtx *desc;
7359 unsigned int i;
7360 HOST_WIDE_INT offset;
7361 tree decl;
7362 rtx symbol;
7364 if (!block->objects)
7365 return;
7367 /* Switch to the section and make sure that the first byte is
7368 suitably aligned. */
7369 switch_to_section (block->sect);
7370 assemble_align (block->alignment);
7372 /* Define the values of all anchors relative to the current section
7373 position. */
7374 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7375 targetm.asm_out.output_anchor (symbol);
7377 /* Output the objects themselves. */
7378 offset = 0;
7379 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7381 /* Move to the object's offset, padding with zeros if necessary. */
7382 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7383 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7384 if (CONSTANT_POOL_ADDRESS_P (symbol))
7386 desc = SYMBOL_REF_CONSTANT (symbol);
7387 /* Pass 1 for align as we have already laid out everything in the block.
7388 So aligning shouldn't be necessary. */
7389 output_constant_pool_1 (desc, 1);
7390 offset += GET_MODE_SIZE (desc->mode);
7392 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7394 HOST_WIDE_INT size;
7395 decl = SYMBOL_REF_DECL (symbol);
7396 assemble_constant_contents
7397 (DECL_INITIAL (decl), XSTR (symbol, 0), DECL_ALIGN (decl));
7399 size = get_constant_size (DECL_INITIAL (decl));
7400 offset += size;
7401 if ((flag_sanitize & SANITIZE_ADDRESS)
7402 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7403 && asan_protect_global (DECL_INITIAL (decl)))
7405 size = asan_red_zone_size (size);
7406 assemble_zeros (size);
7407 offset += size;
7410 else
7412 HOST_WIDE_INT size;
7413 decl = SYMBOL_REF_DECL (symbol);
7414 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7415 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7416 offset += size;
7417 if ((flag_sanitize & SANITIZE_ADDRESS)
7418 && asan_protect_global (decl))
7420 size = asan_red_zone_size (size);
7421 assemble_zeros (size);
7422 offset += size;
7428 /* A htab_traverse callback used to call output_object_block for
7429 each member of object_block_htab. */
7432 output_object_block_htab (object_block **slot, void *)
7434 output_object_block (*slot);
7435 return 1;
7438 /* Output the definitions of all object_blocks. */
7440 void
7441 output_object_blocks (void)
7443 object_block_htab->traverse<void *, output_object_block_htab> (NULL);
7446 /* This function provides a possible implementation of the
7447 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7448 by -frecord-gcc-switches it creates a new mergeable, string section in the
7449 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7450 contains the switches in ASCII format.
7452 FIXME: This code does not correctly handle double quote characters
7453 that appear inside strings, (it strips them rather than preserving them).
7454 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7455 characters - instead it treats them as sub-string separators. Since
7456 we want to emit NUL strings terminators into the object file we have to use
7457 ASM_OUTPUT_SKIP. */
7460 elf_record_gcc_switches (print_switch_type type, const char * name)
7462 switch (type)
7464 case SWITCH_TYPE_PASSED:
7465 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7466 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7467 break;
7469 case SWITCH_TYPE_DESCRIPTIVE:
7470 if (name == NULL)
7472 /* Distinguish between invocations where name is NULL. */
7473 static bool started = false;
7475 if (!started)
7477 section * sec;
7479 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7480 SECTION_DEBUG
7481 | SECTION_MERGE
7482 | SECTION_STRINGS
7483 | (SECTION_ENTSIZE & 1),
7484 NULL);
7485 switch_to_section (sec);
7486 started = true;
7490 default:
7491 break;
7494 /* The return value is currently ignored by the caller, but must be 0.
7495 For -fverbose-asm the return value would be the number of characters
7496 emitted into the assembler file. */
7497 return 0;
7500 /* Emit text to declare externally defined symbols. It is needed to
7501 properly support non-default visibility. */
7502 void
7503 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7504 tree decl,
7505 const char *name ATTRIBUTE_UNUSED)
7507 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7508 set in order to avoid putting out names that are never really
7509 used. Always output visibility specified in the source. */
7510 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7511 && (DECL_VISIBILITY_SPECIFIED (decl)
7512 || targetm.binds_local_p (decl)))
7513 maybe_assemble_visibility (decl);
7516 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7518 void
7519 default_asm_output_source_filename (FILE *file, const char *name)
7521 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7522 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7523 #else
7524 fprintf (file, "\t.file\t");
7525 output_quoted_string (file, name);
7526 putc ('\n', file);
7527 #endif
7530 /* Output a file name in the form wanted by System V. */
7532 void
7533 output_file_directive (FILE *asm_file, const char *input_name)
7535 int len;
7536 const char *na;
7538 if (input_name == NULL)
7539 input_name = "<stdin>";
7540 else
7541 input_name = remap_debug_filename (input_name);
7543 len = strlen (input_name);
7544 na = input_name + len;
7546 /* NA gets INPUT_NAME sans directory names. */
7547 while (na > input_name)
7549 if (IS_DIR_SEPARATOR (na[-1]))
7550 break;
7551 na--;
7554 targetm.asm_out.output_source_filename (asm_file, na);
7557 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7558 EXP. */
7560 make_debug_expr_from_rtl (const_rtx exp)
7562 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7563 machine_mode mode = GET_MODE (exp);
7564 rtx dval;
7566 DECL_ARTIFICIAL (ddecl) = 1;
7567 if (REG_P (exp) && REG_EXPR (exp))
7568 type = TREE_TYPE (REG_EXPR (exp));
7569 else if (MEM_P (exp) && MEM_EXPR (exp))
7570 type = TREE_TYPE (MEM_EXPR (exp));
7571 else
7572 type = NULL_TREE;
7573 if (type && TYPE_MODE (type) == mode)
7574 TREE_TYPE (ddecl) = type;
7575 else
7576 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7577 DECL_MODE (ddecl) = mode;
7578 dval = gen_rtx_DEBUG_EXPR (mode);
7579 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7580 SET_DECL_RTL (ddecl, dval);
7581 return dval;
7584 #ifdef ELF_ASCII_ESCAPES
7585 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7587 void
7588 default_elf_asm_output_limited_string (FILE *f, const char *s)
7590 int escape;
7591 unsigned char c;
7593 fputs (STRING_ASM_OP, f);
7594 putc ('"', f);
7595 while (*s != '\0')
7597 c = *s;
7598 escape = ELF_ASCII_ESCAPES[c];
7599 switch (escape)
7601 case 0:
7602 putc (c, f);
7603 break;
7604 case 1:
7605 /* TODO: Print in hex with fast function, important for -flto. */
7606 fprintf (f, "\\%03o", c);
7607 break;
7608 default:
7609 putc ('\\', f);
7610 putc (escape, f);
7611 break;
7613 s++;
7615 putc ('\"', f);
7616 putc ('\n', f);
7619 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7621 void
7622 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7624 const char *limit = s + len;
7625 const char *last_null = NULL;
7626 unsigned bytes_in_chunk = 0;
7627 unsigned char c;
7628 int escape;
7630 for (; s < limit; s++)
7632 const char *p;
7634 if (bytes_in_chunk >= 60)
7636 putc ('\"', f);
7637 putc ('\n', f);
7638 bytes_in_chunk = 0;
7641 if (s > last_null)
7643 for (p = s; p < limit && *p != '\0'; p++)
7644 continue;
7645 last_null = p;
7647 else
7648 p = last_null;
7650 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7652 if (bytes_in_chunk > 0)
7654 putc ('\"', f);
7655 putc ('\n', f);
7656 bytes_in_chunk = 0;
7659 default_elf_asm_output_limited_string (f, s);
7660 s = p;
7662 else
7664 if (bytes_in_chunk == 0)
7665 fputs (ASCII_DATA_ASM_OP "\"", f);
7667 c = *s;
7668 escape = ELF_ASCII_ESCAPES[c];
7669 switch (escape)
7671 case 0:
7672 putc (c, f);
7673 bytes_in_chunk++;
7674 break;
7675 case 1:
7676 /* TODO: Print in hex with fast function, important for -flto. */
7677 fprintf (f, "\\%03o", c);
7678 bytes_in_chunk += 4;
7679 break;
7680 default:
7681 putc ('\\', f);
7682 putc (escape, f);
7683 bytes_in_chunk += 2;
7684 break;
7690 if (bytes_in_chunk > 0)
7692 putc ('\"', f);
7693 putc ('\n', f);
7696 #endif
7698 static GTY(()) section *elf_init_array_section;
7699 static GTY(()) section *elf_fini_array_section;
7701 static section *
7702 get_elf_initfini_array_priority_section (int priority,
7703 bool constructor_p)
7705 section *sec;
7706 if (priority != DEFAULT_INIT_PRIORITY)
7708 char buf[18];
7709 sprintf (buf, "%s.%.5u",
7710 constructor_p ? ".init_array" : ".fini_array",
7711 priority);
7712 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7714 else
7716 if (constructor_p)
7718 if (elf_init_array_section == NULL)
7719 elf_init_array_section
7720 = get_section (".init_array",
7721 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7722 sec = elf_init_array_section;
7724 else
7726 if (elf_fini_array_section == NULL)
7727 elf_fini_array_section
7728 = get_section (".fini_array",
7729 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7730 sec = elf_fini_array_section;
7733 return sec;
7736 /* Use .init_array section for constructors. */
7738 void
7739 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7741 section *sec = get_elf_initfini_array_priority_section (priority,
7742 true);
7743 assemble_addr_to_section (symbol, sec);
7746 /* Use .fini_array section for destructors. */
7748 void
7749 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7751 section *sec = get_elf_initfini_array_priority_section (priority,
7752 false);
7753 assemble_addr_to_section (symbol, sec);
7756 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7758 This is a bit of a cheat. The real default is a no-op, but this
7759 hook is the default for all targets with a .ident directive. */
7761 void
7762 default_asm_output_ident_directive (const char *ident_str)
7764 const char *ident_asm_op = "\t.ident\t";
7766 /* If we are still in the front end, do not write out the string
7767 to asm_out_file. Instead, add a fake top-level asm statement.
7768 This allows the front ends to use this hook without actually
7769 writing to asm_out_file, to handle #ident or Pragma Ident. */
7770 if (symtab->state == PARSING)
7772 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7773 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7775 else
7776 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7779 #include "gt-varasm.h"