PR target/66563
[official-gcc.git] / gcc / varasm.c
blob056d8c77219fddbc75adeb7df6b4b04d9ddda353
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 "alias.h"
34 #include "symtab.h"
35 #include "tree.h"
36 #include "fold-const.h"
37 #include "stor-layout.h"
38 #include "stringpool.h"
39 #include "varasm.h"
40 #include "flags.h"
41 #include "hard-reg-set.h"
42 #include "function.h"
43 #include "insn-config.h"
44 #include "expmed.h"
45 #include "dojump.h"
46 #include "explow.h"
47 #include "calls.h"
48 #include "emit-rtl.h"
49 #include "stmt.h"
50 #include "expr.h"
51 #include "regs.h"
52 #include "output.h"
53 #include "diagnostic-core.h"
54 #include "langhooks.h"
55 #include "tm_p.h"
56 #include "debug.h"
57 #include "target.h"
58 #include "common/common-target.h"
59 #include "targhooks.h"
60 #include "predict.h"
61 #include "dominance.h"
62 #include "cfg.h"
63 #include "basic-block.h"
64 #include "plugin-api.h"
65 #include "ipa-ref.h"
66 #include "cgraph.h"
67 #include "asan.h"
68 #include "rtl-iter.h"
69 #include "tree-chkp.h"
71 #ifdef XCOFF_DEBUGGING_INFO
72 #include "xcoffout.h" /* Needed for external data
73 declarations for e.g. AIX 4.x. */
74 #endif
76 /* The (assembler) name of the first globally-visible object output. */
77 extern GTY(()) const char *first_global_object_name;
78 extern GTY(()) const char *weak_global_object_name;
80 const char *first_global_object_name;
81 const char *weak_global_object_name;
83 struct addr_const;
84 struct constant_descriptor_rtx;
85 struct rtx_constant_pool;
87 #define n_deferred_constants (crtl->varasm.deferred_constants)
89 /* Number for making the label on the next
90 constant that is stored in memory. */
92 static GTY(()) int const_labelno;
94 /* Carry information from ASM_DECLARE_OBJECT_NAME
95 to ASM_FINISH_DECLARE_OBJECT. */
97 int size_directive_output;
99 /* The last decl for which assemble_variable was called,
100 if it did ASM_DECLARE_OBJECT_NAME.
101 If the last call to assemble_variable didn't do that,
102 this holds 0. */
104 tree last_assemble_variable_decl;
106 /* The following global variable indicates if the first basic block
107 in a function belongs to the cold partition or not. */
109 bool first_function_block_is_cold;
111 /* Whether we saw any functions with no_split_stack. */
113 static bool saw_no_split_stack;
115 static const char *strip_reg_name (const char *);
116 static int contains_pointers_p (tree);
117 #ifdef ASM_OUTPUT_EXTERNAL
118 static bool incorporeal_function_p (tree);
119 #endif
120 static void decode_addr_const (tree, struct addr_const *);
121 static hashval_t const_hash_1 (const tree);
122 static int compare_constant (const tree, const tree);
123 static void output_constant_def_contents (rtx);
124 static void output_addressed_constants (tree);
125 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
126 unsigned int);
127 static void globalize_decl (tree);
128 static bool decl_readonly_section_1 (enum section_category);
129 #ifdef BSS_SECTION_ASM_OP
130 #ifdef ASM_OUTPUT_ALIGNED_BSS
131 static void asm_output_aligned_bss (FILE *, tree, const char *,
132 unsigned HOST_WIDE_INT, int)
133 ATTRIBUTE_UNUSED;
134 #endif
135 #endif /* BSS_SECTION_ASM_OP */
136 static void mark_weak (tree);
137 static void output_constant_pool (const char *, tree);
139 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
140 section *text_section;
141 section *data_section;
142 section *readonly_data_section;
143 section *sdata_section;
144 section *ctors_section;
145 section *dtors_section;
146 section *bss_section;
147 section *sbss_section;
149 /* Various forms of common section. All are guaranteed to be nonnull. */
150 section *tls_comm_section;
151 section *comm_section;
152 section *lcomm_section;
154 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
155 May be null. */
156 section *bss_noswitch_section;
158 /* The section that holds the main exception table, when known. The section
159 is set either by the target's init_sections hook or by the first call to
160 switch_to_exception_section. */
161 section *exception_section;
163 /* The section that holds the DWARF2 frame unwind information, when known.
164 The section is set either by the target's init_sections hook or by the
165 first call to switch_to_eh_frame_section. */
166 section *eh_frame_section;
168 /* asm_out_file's current section. This is NULL if no section has yet
169 been selected or if we lose track of what the current section is. */
170 section *in_section;
172 /* True if code for the current function is currently being directed
173 at the cold section. */
174 bool in_cold_section_p;
176 /* The following global holds the "function name" for the code in the
177 cold section of a function, if hot/cold function splitting is enabled
178 and there was actually code that went into the cold section. A
179 pseudo function name is needed for the cold section of code for some
180 debugging tools that perform symbolization. */
181 tree cold_function_name = NULL_TREE;
183 /* A linked list of all the unnamed sections. */
184 static GTY(()) section *unnamed_sections;
186 /* Return a nonzero value if DECL has a section attribute. */
187 #define IN_NAMED_SECTION(DECL) \
188 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
189 && DECL_SECTION_NAME (DECL) != NULL)
191 struct section_hasher : ggc_hasher<section *>
193 typedef const char *compare_type;
195 static hashval_t hash (section *);
196 static bool equal (section *, const char *);
199 /* Hash table of named sections. */
200 static GTY(()) hash_table<section_hasher> *section_htab;
202 struct object_block_hasher : ggc_hasher<object_block *>
204 typedef const section *compare_type;
206 static hashval_t hash (object_block *);
207 static bool equal (object_block *, const section *);
210 /* A table of object_blocks, indexed by section. */
211 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
213 /* The next number to use for internal anchor labels. */
214 static GTY(()) int anchor_labelno;
216 /* A pool of constants that can be shared between functions. */
217 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
219 /* Helper routines for maintaining section_htab. */
221 bool
222 section_hasher::equal (section *old, const char *new_name)
224 return strcmp (old->named.name, new_name) == 0;
227 hashval_t
228 section_hasher::hash (section *old)
230 return htab_hash_string (old->named.name);
233 /* Return a hash value for section SECT. */
235 static hashval_t
236 hash_section (section *sect)
238 if (sect->common.flags & SECTION_NAMED)
239 return htab_hash_string (sect->named.name);
240 return sect->common.flags;
243 /* Helper routines for maintaining object_block_htab. */
245 inline bool
246 object_block_hasher::equal (object_block *old, const section *new_section)
248 return old->sect == new_section;
251 hashval_t
252 object_block_hasher::hash (object_block *old)
254 return hash_section (old->sect);
257 /* Return a new unnamed section with the given fields. */
259 section *
260 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
261 const void *data)
263 section *sect;
265 sect = ggc_alloc<section> ();
266 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
267 sect->unnamed.callback = callback;
268 sect->unnamed.data = data;
269 sect->unnamed.next = unnamed_sections;
271 unnamed_sections = sect;
272 return sect;
275 /* Return a SECTION_NOSWITCH section with the given fields. */
277 static section *
278 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
280 section *sect;
282 sect = ggc_alloc<section> ();
283 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
284 sect->noswitch.callback = callback;
286 return sect;
289 /* Return the named section structure associated with NAME. Create
290 a new section with the given fields if no such structure exists. */
292 section *
293 get_section (const char *name, unsigned int flags, tree decl)
295 section *sect, **slot;
297 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
298 INSERT);
299 flags |= SECTION_NAMED;
300 if (*slot == NULL)
302 sect = ggc_alloc<section> ();
303 sect->named.common.flags = flags;
304 sect->named.name = ggc_strdup (name);
305 sect->named.decl = decl;
306 *slot = sect;
308 else
310 sect = *slot;
311 if ((sect->common.flags & ~SECTION_DECLARED) != flags
312 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
314 /* It is fine if one of the section flags is
315 SECTION_WRITE | SECTION_RELRO and the other has none of these
316 flags (i.e. read-only) in named sections and either the
317 section hasn't been declared yet or has been declared as writable.
318 In that case just make sure the resulting flags are
319 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
320 relocations. */
321 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
322 == (SECTION_WRITE | SECTION_RELRO)
323 && (sect->common.flags
324 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
325 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
326 && ((sect->common.flags & SECTION_DECLARED) == 0
327 || (sect->common.flags & SECTION_WRITE)))
329 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
330 return sect;
332 /* Sanity check user variables for flag changes. */
333 if (sect->named.decl != NULL
334 && DECL_P (sect->named.decl)
335 && decl != sect->named.decl)
337 if (decl != NULL && DECL_P (decl))
338 error ("%+D causes a section type conflict with %D",
339 decl, sect->named.decl);
340 else
341 error ("section type conflict with %D", sect->named.decl);
342 inform (DECL_SOURCE_LOCATION (sect->named.decl),
343 "%qD was declared here", sect->named.decl);
345 else if (decl != NULL && DECL_P (decl))
346 error ("%+D causes a section type conflict", decl);
347 else
348 error ("section type conflict");
349 /* Make sure we don't error about one section multiple times. */
350 sect->common.flags |= SECTION_OVERRIDE;
353 return sect;
356 /* Return true if the current compilation mode benefits from having
357 objects grouped into blocks. */
359 static bool
360 use_object_blocks_p (void)
362 return flag_section_anchors;
365 /* Return the object_block structure for section SECT. Create a new
366 structure if we haven't created one already. Return null if SECT
367 itself is null. */
369 static struct object_block *
370 get_block_for_section (section *sect)
372 struct object_block *block;
374 if (sect == NULL)
375 return NULL;
377 object_block **slot
378 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
379 INSERT);
380 block = *slot;
381 if (block == NULL)
383 block = ggc_cleared_alloc<object_block> ();
384 block->sect = sect;
385 *slot = block;
387 return block;
390 /* Create a symbol with label LABEL and place it at byte offset
391 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
392 is not yet known. LABEL must be a garbage-collected string. */
394 static rtx
395 create_block_symbol (const char *label, struct object_block *block,
396 HOST_WIDE_INT offset)
398 rtx symbol;
399 unsigned int size;
401 /* Create the extended SYMBOL_REF. */
402 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
403 symbol = (rtx) ggc_internal_alloc (size);
405 /* Initialize the normal SYMBOL_REF fields. */
406 memset (symbol, 0, size);
407 PUT_CODE (symbol, SYMBOL_REF);
408 PUT_MODE (symbol, Pmode);
409 XSTR (symbol, 0) = label;
410 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
412 /* Initialize the block_symbol stuff. */
413 SYMBOL_REF_BLOCK (symbol) = block;
414 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
416 return symbol;
419 /* Return a section with a particular name and with whatever SECTION_*
420 flags section_type_flags deems appropriate. The name of the section
421 is taken from NAME if nonnull, otherwise it is taken from DECL's
422 DECL_SECTION_NAME. DECL is the decl associated with the section
423 (see the section comment for details) and RELOC is as for
424 section_type_flags. */
426 section *
427 get_named_section (tree decl, const char *name, int reloc)
429 unsigned int flags;
431 if (name == NULL)
433 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
434 name = DECL_SECTION_NAME (decl);
437 flags = targetm.section_type_flags (decl, name, reloc);
438 return get_section (name, flags, decl);
441 /* Worker for resolve_unique_section. */
443 static bool
444 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
446 n->implicit_section = true;
447 return false;
450 /* If required, set DECL_SECTION_NAME to a unique name. */
452 void
453 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
454 int flag_function_or_data_sections)
456 if (DECL_SECTION_NAME (decl) == NULL
457 && targetm_common.have_named_sections
458 && (flag_function_or_data_sections
459 || DECL_COMDAT_GROUP (decl)))
461 targetm.asm_out.unique_section (decl, reloc);
462 if (DECL_SECTION_NAME (decl))
463 symtab_node::get (decl)->call_for_symbol_and_aliases
464 (set_implicit_section, NULL, true);
468 #ifdef BSS_SECTION_ASM_OP
470 #ifdef ASM_OUTPUT_ALIGNED_BSS
472 /* Utility function for targets to use in implementing
473 ASM_OUTPUT_ALIGNED_BSS.
474 ??? It is believed that this function will work in most cases so such
475 support is localized here. */
477 static void
478 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
479 const char *name, unsigned HOST_WIDE_INT size,
480 int align)
482 switch_to_section (bss_section);
483 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
484 #ifdef ASM_DECLARE_OBJECT_NAME
485 last_assemble_variable_decl = decl;
486 ASM_DECLARE_OBJECT_NAME (file, name, decl);
487 #else
488 /* Standard thing is just output label for the object. */
489 ASM_OUTPUT_LABEL (file, name);
490 #endif /* ASM_DECLARE_OBJECT_NAME */
491 ASM_OUTPUT_SKIP (file, size ? size : 1);
494 #endif
496 #endif /* BSS_SECTION_ASM_OP */
498 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
499 /* Return the hot section for function DECL. Return text_section for
500 null DECLs. */
502 static section *
503 hot_function_section (tree decl)
505 if (decl != NULL_TREE
506 && DECL_SECTION_NAME (decl) != NULL
507 && targetm_common.have_named_sections)
508 return get_named_section (decl, NULL, 0);
509 else
510 return text_section;
512 #endif
514 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
515 is NULL.
517 When DECL_SECTION_NAME is non-NULL and it is implicit section and
518 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
519 concatenate the name with NAMED_SECTION_SUFFIX.
520 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
522 section *
523 get_named_text_section (tree decl,
524 const char *text_section_name,
525 const char *named_section_suffix)
527 if (decl && DECL_SECTION_NAME (decl))
529 if (named_section_suffix)
531 const char *dsn = DECL_SECTION_NAME (decl);
532 const char *stripped_name;
533 char *name, *buffer;
535 name = (char *) alloca (strlen (dsn) + 1);
536 memcpy (name, dsn,
537 strlen (dsn) + 1);
539 stripped_name = targetm.strip_name_encoding (name);
541 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
542 return get_named_section (decl, buffer, 0);
544 else if (symtab_node::get (decl)->implicit_section)
546 const char *name;
548 /* Do not try to split gnu_linkonce functions. This gets somewhat
549 slipperly. */
550 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
551 return NULL;
552 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
553 name = targetm.strip_name_encoding (name);
554 return get_named_section (decl, ACONCAT ((text_section_name, ".",
555 name, NULL)), 0);
557 else
558 return NULL;
560 return get_named_section (decl, text_section_name, 0);
563 /* Choose named function section based on its frequency. */
565 section *
566 default_function_section (tree decl, enum node_frequency freq,
567 bool startup, bool exit)
569 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
570 /* Old GNU linkers have buggy --gc-section support, which sometimes
571 results in .gcc_except_table* sections being garbage collected. */
572 if (decl
573 && symtab_node::get (decl)->implicit_section)
574 return NULL;
575 #endif
577 if (!flag_reorder_functions
578 || !targetm_common.have_named_sections)
579 return NULL;
580 /* Startup code should go to startup subsection unless it is
581 unlikely executed (this happens especially with function splitting
582 where we can split away unnecessary parts of static constructors. */
583 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
585 /* If we do have a profile or(and) LTO phase is executed, we do not need
586 these ELF section. */
587 if (!in_lto_p || !flag_profile_values)
588 return get_named_text_section (decl, ".text.startup", NULL);
589 else
590 return NULL;
593 /* Similarly for exit. */
594 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
595 return get_named_text_section (decl, ".text.exit", NULL);
597 /* Group cold functions together, similarly for hot code. */
598 switch (freq)
600 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
601 return get_named_text_section (decl, ".text.unlikely", NULL);
602 case NODE_FREQUENCY_HOT:
603 /* If we do have a profile or(and) LTO phase is executed, we do not need
604 these ELF section. */
605 if (!in_lto_p || !flag_profile_values)
606 return get_named_text_section (decl, ".text.hot", NULL);
607 default:
608 return NULL;
612 /* Return the section for function DECL.
614 If DECL is NULL_TREE, return the text section. We can be passed
615 NULL_TREE under some circumstances by dbxout.c at least.
617 If FORCE_COLD is true, return cold function section ignoring
618 the frequency info of cgraph_node. */
620 static section *
621 function_section_1 (tree decl, bool force_cold)
623 section *section = NULL;
624 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
625 bool startup = false, exit = false;
627 if (decl)
629 struct cgraph_node *node = cgraph_node::get (decl);
631 if (node)
633 freq = node->frequency;
634 startup = node->only_called_at_startup;
635 exit = node->only_called_at_exit;
638 if (force_cold)
639 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
641 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
642 if (decl != NULL_TREE
643 && DECL_SECTION_NAME (decl) != NULL)
645 if (targetm.asm_out.function_section)
646 section = targetm.asm_out.function_section (decl, freq,
647 startup, exit);
648 if (section)
649 return section;
650 return get_named_section (decl, NULL, 0);
652 else
653 return targetm.asm_out.select_section
654 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
655 symtab_node::get (decl)->definition_alignment ());
656 #else
657 if (targetm.asm_out.function_section)
658 section = targetm.asm_out.function_section (decl, freq, startup, exit);
659 if (section)
660 return section;
661 return hot_function_section (decl);
662 #endif
665 /* Return the section for function DECL.
667 If DECL is NULL_TREE, return the text section. We can be passed
668 NULL_TREE under some circumstances by dbxout.c at least. */
670 section *
671 function_section (tree decl)
673 /* Handle cases where function splitting code decides
674 to put function entry point into unlikely executed section
675 despite the fact that the function itself is not cold
676 (i.e. it is called rarely but contains a hot loop that is
677 better to live in hot subsection for the code locality). */
678 return function_section_1 (decl,
679 first_function_block_is_cold);
682 /* Return the section for the current function, take IN_COLD_SECTION_P
683 into account. */
685 section *
686 current_function_section (void)
688 return function_section_1 (current_function_decl, in_cold_section_p);
691 /* Tell assembler to switch to unlikely-to-be-executed text section. */
693 section *
694 unlikely_text_section (void)
696 return function_section_1 (current_function_decl, true);
699 /* When called within a function context, return true if the function
700 has been assigned a cold text section and if SECT is that section.
701 When called outside a function context, return true if SECT is the
702 default cold section. */
704 bool
705 unlikely_text_section_p (section *sect)
707 return sect == function_section_1 (current_function_decl, true);
710 /* Return the read-only data section associated with function DECL. */
712 section *
713 default_function_rodata_section (tree decl)
715 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
717 const char *name = DECL_SECTION_NAME (decl);
719 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
721 const char *dot;
722 size_t len;
723 char* rname;
725 dot = strchr (name + 1, '.');
726 if (!dot)
727 dot = name;
728 len = strlen (dot) + 8;
729 rname = (char *) alloca (len);
731 strcpy (rname, ".rodata");
732 strcat (rname, dot);
733 return get_section (rname, SECTION_LINKONCE, decl);
735 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
736 else if (DECL_COMDAT_GROUP (decl)
737 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
739 size_t len = strlen (name) + 1;
740 char *rname = (char *) alloca (len);
742 memcpy (rname, name, len);
743 rname[14] = 'r';
744 return get_section (rname, SECTION_LINKONCE, decl);
746 /* For .text.foo we want to use .rodata.foo. */
747 else if (flag_function_sections && flag_data_sections
748 && strncmp (name, ".text.", 6) == 0)
750 size_t len = strlen (name) + 1;
751 char *rname = (char *) alloca (len + 2);
753 memcpy (rname, ".rodata", 7);
754 memcpy (rname + 7, name + 5, len - 5);
755 return get_section (rname, 0, decl);
759 return readonly_data_section;
762 /* Return the read-only data section associated with function DECL
763 for targets where that section should be always the single
764 readonly data section. */
766 section *
767 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
769 return readonly_data_section;
772 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
774 static const char *
775 function_mergeable_rodata_prefix (void)
777 section *s = targetm.asm_out.function_rodata_section (current_function_decl);
778 if (SECTION_STYLE (s) == SECTION_NAMED)
779 return s->named.name;
780 else
781 return targetm.asm_out.mergeable_rodata_prefix;
784 /* Return the section to use for string merging. */
786 static section *
787 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
788 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
789 unsigned int flags ATTRIBUTE_UNUSED)
791 HOST_WIDE_INT len;
793 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
794 && TREE_CODE (decl) == STRING_CST
795 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
796 && align <= 256
797 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
798 && TREE_STRING_LENGTH (decl) >= len)
800 machine_mode mode;
801 unsigned int modesize;
802 const char *str;
803 HOST_WIDE_INT i;
804 int j, unit;
805 const char *prefix = function_mergeable_rodata_prefix ();
806 char *name = (char *) alloca (strlen (prefix) + 30);
808 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
809 modesize = GET_MODE_BITSIZE (mode);
810 if (modesize >= 8 && modesize <= 256
811 && (modesize & (modesize - 1)) == 0)
813 if (align < modesize)
814 align = modesize;
816 str = TREE_STRING_POINTER (decl);
817 unit = GET_MODE_SIZE (mode);
819 /* Check for embedded NUL characters. */
820 for (i = 0; i < len; i += unit)
822 for (j = 0; j < unit; j++)
823 if (str[i + j] != '\0')
824 break;
825 if (j == unit)
826 break;
828 if (i == len - unit)
830 sprintf (name, "%s.str%d.%d", prefix,
831 modesize / 8, (int) (align / 8));
832 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
833 return get_section (name, flags, NULL);
838 return readonly_data_section;
841 /* Return the section to use for constant merging. */
843 section *
844 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
845 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
846 unsigned int flags ATTRIBUTE_UNUSED)
848 unsigned int modesize = GET_MODE_BITSIZE (mode);
850 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
851 && mode != VOIDmode
852 && mode != BLKmode
853 && modesize <= align
854 && align >= 8
855 && align <= 256
856 && (align & (align - 1)) == 0)
858 const char *prefix = function_mergeable_rodata_prefix ();
859 char *name = (char *) alloca (strlen (prefix) + 30);
861 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
862 flags |= (align / 8) | SECTION_MERGE;
863 return get_section (name, flags, NULL);
865 return readonly_data_section;
868 /* Given NAME, a putative register name, discard any customary prefixes. */
870 static const char *
871 strip_reg_name (const char *name)
873 #ifdef REGISTER_PREFIX
874 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
875 name += strlen (REGISTER_PREFIX);
876 #endif
877 if (name[0] == '%' || name[0] == '#')
878 name++;
879 return name;
882 /* The user has asked for a DECL to have a particular name. Set (or
883 change) it in such a way that we don't prefix an underscore to
884 it. */
885 void
886 set_user_assembler_name (tree decl, const char *name)
888 char *starred = (char *) alloca (strlen (name) + 2);
889 starred[0] = '*';
890 strcpy (starred + 1, name);
891 symtab->change_decl_assembler_name (decl, get_identifier (starred));
892 SET_DECL_RTL (decl, NULL_RTX);
895 /* Decode an `asm' spec for a declaration as a register name.
896 Return the register number, or -1 if nothing specified,
897 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
898 or -3 if ASMSPEC is `cc' and is not recognized,
899 or -4 if ASMSPEC is `memory' and is not recognized.
900 Accept an exact spelling or a decimal number.
901 Prefixes such as % are optional. */
904 decode_reg_name_and_count (const char *asmspec, int *pnregs)
906 /* Presume just one register is clobbered. */
907 *pnregs = 1;
909 if (asmspec != 0)
911 int i;
913 /* Get rid of confusing prefixes. */
914 asmspec = strip_reg_name (asmspec);
916 /* Allow a decimal number as a "register name". */
917 for (i = strlen (asmspec) - 1; i >= 0; i--)
918 if (! ISDIGIT (asmspec[i]))
919 break;
920 if (asmspec[0] != 0 && i < 0)
922 i = atoi (asmspec);
923 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
924 return i;
925 else
926 return -2;
929 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
930 if (reg_names[i][0]
931 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
932 return i;
934 #ifdef OVERLAPPING_REGISTER_NAMES
936 static const struct
938 const char *const name;
939 const int number;
940 const int nregs;
941 } table[] = OVERLAPPING_REGISTER_NAMES;
943 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
944 if (table[i].name[0]
945 && ! strcmp (asmspec, table[i].name))
947 *pnregs = table[i].nregs;
948 return table[i].number;
951 #endif /* OVERLAPPING_REGISTER_NAMES */
953 #ifdef ADDITIONAL_REGISTER_NAMES
955 static const struct { const char *const name; const int number; } table[]
956 = ADDITIONAL_REGISTER_NAMES;
958 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
959 if (table[i].name[0]
960 && ! strcmp (asmspec, table[i].name)
961 && reg_names[table[i].number][0])
962 return table[i].number;
964 #endif /* ADDITIONAL_REGISTER_NAMES */
966 if (!strcmp (asmspec, "memory"))
967 return -4;
969 if (!strcmp (asmspec, "cc"))
970 return -3;
972 return -2;
975 return -1;
979 decode_reg_name (const char *name)
981 int count;
982 return decode_reg_name_and_count (name, &count);
986 /* Return true if DECL's initializer is suitable for a BSS section. */
988 bool
989 bss_initializer_p (const_tree decl)
991 return (DECL_INITIAL (decl) == NULL
992 /* In LTO we have no errors in program; error_mark_node is used
993 to mark offlined constructors. */
994 || (DECL_INITIAL (decl) == error_mark_node
995 && !in_lto_p)
996 || (flag_zero_initialized_in_bss
997 /* Leave constant zeroes in .rodata so they
998 can be shared. */
999 && !TREE_READONLY (decl)
1000 && initializer_zerop (DECL_INITIAL (decl))));
1003 /* Compute the alignment of variable specified by DECL.
1004 DONT_OUTPUT_DATA is from assemble_variable. */
1006 void
1007 align_variable (tree decl, bool dont_output_data)
1009 unsigned int align = DECL_ALIGN (decl);
1011 /* In the case for initialing an array whose length isn't specified,
1012 where we have not yet been able to do the layout,
1013 figure out the proper alignment now. */
1014 if (dont_output_data && DECL_SIZE (decl) == 0
1015 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1016 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1018 /* Some object file formats have a maximum alignment which they support.
1019 In particular, a.out format supports a maximum alignment of 4. */
1020 if (align > MAX_OFILE_ALIGNMENT)
1022 error ("alignment of %q+D is greater than maximum object "
1023 "file alignment %d", decl,
1024 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1025 align = MAX_OFILE_ALIGNMENT;
1028 if (! DECL_USER_ALIGN (decl))
1030 #ifdef DATA_ABI_ALIGNMENT
1031 unsigned int data_abi_align
1032 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1033 /* For backwards compatibility, don't assume the ABI alignment for
1034 TLS variables. */
1035 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1036 align = data_abi_align;
1037 #endif
1039 /* On some machines, it is good to increase alignment sometimes.
1040 But as DECL_ALIGN is used both for actually emitting the variable
1041 and for code accessing the variable as guaranteed alignment, we
1042 can only increase the alignment if it is a performance optimization
1043 if the references to it must bind to the current definition. */
1044 if (decl_binds_to_current_def_p (decl)
1045 && !DECL_VIRTUAL_P (decl))
1047 #ifdef DATA_ALIGNMENT
1048 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1049 /* Don't increase alignment too much for TLS variables - TLS space
1050 is too precious. */
1051 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1052 align = data_align;
1053 #endif
1054 #ifdef CONSTANT_ALIGNMENT
1055 if (DECL_INITIAL (decl) != 0
1056 /* In LTO we have no errors in program; error_mark_node is used
1057 to mark offlined constructors. */
1058 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1060 unsigned int const_align
1061 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1062 /* Don't increase alignment too much for TLS variables - TLS
1063 space is too precious. */
1064 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1065 align = const_align;
1067 #endif
1071 /* Reset the alignment in case we have made it tighter, so we can benefit
1072 from it in get_pointer_alignment. */
1073 DECL_ALIGN (decl) = align;
1076 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1077 beyond what align_variable returned. */
1079 static unsigned int
1080 get_variable_align (tree decl)
1082 unsigned int align = DECL_ALIGN (decl);
1084 /* For user aligned vars or static vars align_variable already did
1085 everything. */
1086 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1087 return align;
1089 #ifdef DATA_ABI_ALIGNMENT
1090 if (DECL_THREAD_LOCAL_P (decl))
1091 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1092 #endif
1094 /* For decls that bind to the current definition, align_variable
1095 did also everything, except for not assuming ABI required alignment
1096 of TLS variables. For other vars, increase the alignment here
1097 as an optimization. */
1098 if (!decl_binds_to_current_def_p (decl))
1100 /* On some machines, it is good to increase alignment sometimes. */
1101 #ifdef DATA_ALIGNMENT
1102 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1103 /* Don't increase alignment too much for TLS variables - TLS space
1104 is too precious. */
1105 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1106 align = data_align;
1107 #endif
1108 #ifdef CONSTANT_ALIGNMENT
1109 if (DECL_INITIAL (decl) != 0
1110 /* In LTO we have no errors in program; error_mark_node is used
1111 to mark offlined constructors. */
1112 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1114 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1115 align);
1116 /* Don't increase alignment too much for TLS variables - TLS space
1117 is too precious. */
1118 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1119 align = const_align;
1121 #endif
1124 return align;
1127 /* Return the section into which the given VAR_DECL or CONST_DECL
1128 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1129 section should be used wherever possible. */
1131 section *
1132 get_variable_section (tree decl, bool prefer_noswitch_p)
1134 addr_space_t as = ADDR_SPACE_GENERIC;
1135 int reloc;
1136 varpool_node *vnode = varpool_node::get (decl);
1137 if (vnode)
1139 vnode = vnode->ultimate_alias_target ();
1140 decl = vnode->decl;
1143 if (TREE_TYPE (decl) != error_mark_node)
1144 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1146 /* We need the constructor to figure out reloc flag. */
1147 if (vnode)
1148 vnode->get_constructor ();
1150 if (DECL_COMMON (decl))
1152 /* If the decl has been given an explicit section name, or it resides
1153 in a non-generic address space, then it isn't common, and shouldn't
1154 be handled as such. */
1155 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1156 && ADDR_SPACE_GENERIC_P (as));
1157 if (DECL_THREAD_LOCAL_P (decl))
1158 return tls_comm_section;
1159 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1160 return comm_section;
1163 if (DECL_INITIAL (decl) == error_mark_node)
1164 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1165 else if (DECL_INITIAL (decl))
1166 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1167 else
1168 reloc = 0;
1170 resolve_unique_section (decl, reloc, flag_data_sections);
1171 if (IN_NAMED_SECTION (decl))
1172 return get_named_section (decl, NULL, reloc);
1174 if (ADDR_SPACE_GENERIC_P (as)
1175 && !DECL_THREAD_LOCAL_P (decl)
1176 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1177 && bss_initializer_p (decl))
1179 if (!TREE_PUBLIC (decl)
1180 && !((flag_sanitize & SANITIZE_ADDRESS)
1181 && asan_protect_global (decl)))
1182 return lcomm_section;
1183 if (bss_noswitch_section)
1184 return bss_noswitch_section;
1187 return targetm.asm_out.select_section (decl, reloc,
1188 get_variable_align (decl));
1191 /* Return the block into which object_block DECL should be placed. */
1193 static struct object_block *
1194 get_block_for_decl (tree decl)
1196 section *sect;
1198 if (TREE_CODE (decl) == VAR_DECL)
1200 /* The object must be defined in this translation unit. */
1201 if (DECL_EXTERNAL (decl))
1202 return NULL;
1204 /* There's no point using object blocks for something that is
1205 isolated by definition. */
1206 if (DECL_COMDAT_GROUP (decl))
1207 return NULL;
1210 /* We can only calculate block offsets if the decl has a known
1211 constant size. */
1212 if (DECL_SIZE_UNIT (decl) == NULL)
1213 return NULL;
1214 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1215 return NULL;
1217 /* Find out which section should contain DECL. We cannot put it into
1218 an object block if it requires a standalone definition. */
1219 if (TREE_CODE (decl) == VAR_DECL)
1220 align_variable (decl, 0);
1221 sect = get_variable_section (decl, true);
1222 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1223 return NULL;
1225 return get_block_for_section (sect);
1228 /* Make sure block symbol SYMBOL is in block BLOCK. */
1230 static void
1231 change_symbol_block (rtx symbol, struct object_block *block)
1233 if (block != SYMBOL_REF_BLOCK (symbol))
1235 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1236 SYMBOL_REF_BLOCK (symbol) = block;
1240 /* Return true if it is possible to put DECL in an object_block. */
1242 static bool
1243 use_blocks_for_decl_p (tree decl)
1245 struct symtab_node *snode;
1247 /* Only data DECLs can be placed into object blocks. */
1248 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1249 return false;
1251 /* Detect decls created by dw2_force_const_mem. Such decls are
1252 special because DECL_INITIAL doesn't specify the decl's true value.
1253 dw2_output_indirect_constants will instead call assemble_variable
1254 with dont_output_data set to 1 and then print the contents itself. */
1255 if (DECL_INITIAL (decl) == decl)
1256 return false;
1258 /* If this decl is an alias, then we don't want to emit a
1259 definition. */
1260 if (TREE_CODE (decl) == VAR_DECL
1261 && (snode = symtab_node::get (decl)) != NULL
1262 && snode->alias)
1263 return false;
1265 return targetm.use_blocks_for_decl_p (decl);
1268 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1269 until we find an identifier that is not itself a transparent alias.
1270 Modify the alias passed to it by reference (and all aliases on the
1271 way to the ultimate target), such that they do not have to be
1272 followed again, and return the ultimate target of the alias
1273 chain. */
1275 static inline tree
1276 ultimate_transparent_alias_target (tree *alias)
1278 tree target = *alias;
1280 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1282 gcc_assert (TREE_CHAIN (target));
1283 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1284 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1285 && ! TREE_CHAIN (target));
1286 *alias = target;
1289 return target;
1292 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1293 have static storage duration. In other words, it should not be an
1294 automatic variable, including PARM_DECLs.
1296 There is, however, one exception: this function handles variables
1297 explicitly placed in a particular register by the user.
1299 This is never called for PARM_DECL nodes. */
1301 void
1302 make_decl_rtl (tree decl)
1304 const char *name = 0;
1305 int reg_number;
1306 tree id;
1307 rtx x;
1309 /* Check that we are not being given an automatic variable. */
1310 gcc_assert (TREE_CODE (decl) != PARM_DECL
1311 && TREE_CODE (decl) != RESULT_DECL);
1313 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1314 gcc_assert (TREE_CODE (decl) != VAR_DECL
1315 || TREE_STATIC (decl)
1316 || TREE_PUBLIC (decl)
1317 || DECL_EXTERNAL (decl)
1318 || DECL_REGISTER (decl));
1320 /* And that we were not given a type or a label. */
1321 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1322 && TREE_CODE (decl) != LABEL_DECL);
1324 /* For a duplicate declaration, we can be called twice on the
1325 same DECL node. Don't discard the RTL already made. */
1326 if (DECL_RTL_SET_P (decl))
1328 /* If the old RTL had the wrong mode, fix the mode. */
1329 x = DECL_RTL (decl);
1330 if (GET_MODE (x) != DECL_MODE (decl))
1331 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1333 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1334 return;
1336 /* ??? Another way to do this would be to maintain a hashed
1337 table of such critters. Instead of adding stuff to a DECL
1338 to give certain attributes to it, we could use an external
1339 hash map from DECL to set of attributes. */
1341 /* Let the target reassign the RTL if it wants.
1342 This is necessary, for example, when one machine specific
1343 decl attribute overrides another. */
1344 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1346 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1347 on the new decl information. */
1348 if (MEM_P (x)
1349 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1350 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1351 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1353 return;
1356 /* If this variable belongs to the global constant pool, retrieve the
1357 pre-computed RTL or recompute it in LTO mode. */
1358 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1360 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1361 return;
1364 id = DECL_ASSEMBLER_NAME (decl);
1365 if (TREE_CODE (decl) == FUNCTION_DECL
1366 && cgraph_node::get (decl)
1367 && cgraph_node::get (decl)->instrumentation_clone)
1368 ultimate_transparent_alias_target (&id);
1369 name = IDENTIFIER_POINTER (id);
1371 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1372 && DECL_REGISTER (decl))
1374 error ("register name not specified for %q+D", decl);
1376 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1378 const char *asmspec = name+1;
1379 machine_mode mode = DECL_MODE (decl);
1380 reg_number = decode_reg_name (asmspec);
1381 /* First detect errors in declaring global registers. */
1382 if (reg_number == -1)
1383 error ("register name not specified for %q+D", decl);
1384 else if (reg_number < 0)
1385 error ("invalid register name for %q+D", decl);
1386 else if (mode == BLKmode)
1387 error ("data type of %q+D isn%'t suitable for a register",
1388 decl);
1389 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1390 error ("the register specified for %q+D cannot be accessed"
1391 " by the current target", decl);
1392 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1393 error ("the register specified for %q+D is not general enough"
1394 " to be used as a register variable", decl);
1395 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1396 error ("register specified for %q+D isn%'t suitable for data type",
1397 decl);
1398 /* Now handle properly declared static register variables. */
1399 else
1401 int nregs;
1403 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1405 DECL_INITIAL (decl) = 0;
1406 error ("global register variable has initial value");
1408 if (TREE_THIS_VOLATILE (decl))
1409 warning (OPT_Wvolatile_register_var,
1410 "optimization may eliminate reads and/or "
1411 "writes to register variables");
1413 /* If the user specified one of the eliminables registers here,
1414 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1415 confused with that register and be eliminated. This usage is
1416 somewhat suspect... */
1418 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1419 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1420 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1422 if (TREE_STATIC (decl))
1424 /* Make this register global, so not usable for anything
1425 else. */
1426 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1427 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1428 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1429 #endif
1430 nregs = hard_regno_nregs[reg_number][mode];
1431 while (nregs > 0)
1432 globalize_reg (decl, reg_number + --nregs);
1435 /* As a register variable, it has no section. */
1436 return;
1438 /* Avoid internal errors from invalid register
1439 specifications. */
1440 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1441 DECL_HARD_REGISTER (decl) = 0;
1442 return;
1444 /* Now handle ordinary static variables and functions (in memory).
1445 Also handle vars declared register invalidly. */
1446 else if (name[0] == '*')
1448 #ifdef REGISTER_PREFIX
1449 if (strlen (REGISTER_PREFIX) != 0)
1451 reg_number = decode_reg_name (name);
1452 if (reg_number >= 0 || reg_number == -3)
1453 error ("register name given for non-register variable %q+D", decl);
1455 #endif
1458 /* Specifying a section attribute on a variable forces it into a
1459 non-.bss section, and thus it cannot be common. */
1460 /* FIXME: In general this code should not be necessary because
1461 visibility pass is doing the same work. But notice_global_symbol
1462 is called early and it needs to make DECL_RTL to get the name.
1463 we take care of recomputing the DECL_RTL after visibility is changed. */
1464 if (TREE_CODE (decl) == VAR_DECL
1465 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1466 && DECL_SECTION_NAME (decl) != NULL
1467 && DECL_INITIAL (decl) == NULL_TREE
1468 && DECL_COMMON (decl))
1469 DECL_COMMON (decl) = 0;
1471 /* Variables can't be both common and weak. */
1472 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1473 DECL_COMMON (decl) = 0;
1475 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1476 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1477 else
1479 machine_mode address_mode = Pmode;
1480 if (TREE_TYPE (decl) != error_mark_node)
1482 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1483 address_mode = targetm.addr_space.address_mode (as);
1485 x = gen_rtx_SYMBOL_REF (address_mode, name);
1487 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1488 SET_SYMBOL_REF_DECL (x, decl);
1490 x = gen_rtx_MEM (DECL_MODE (decl), x);
1491 if (TREE_CODE (decl) != FUNCTION_DECL)
1492 set_mem_attributes (x, decl, 1);
1493 SET_DECL_RTL (decl, x);
1495 /* Optionally set flags or add text to the name to record information
1496 such as that it is a function name.
1497 If the name is changed, the macro ASM_OUTPUT_LABELREF
1498 will have to know how to strip this information. */
1499 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1502 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1503 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1504 rtl. */
1507 make_decl_rtl_for_debug (tree decl)
1509 unsigned int save_aliasing_flag;
1510 rtx rtl;
1512 if (DECL_RTL_SET_P (decl))
1513 return DECL_RTL (decl);
1515 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1516 call new_alias_set. If running with -fcompare-debug, sometimes
1517 we do not want to create alias sets that will throw the alias
1518 numbers off in the comparison dumps. So... clearing
1519 flag_strict_aliasing will keep new_alias_set() from creating a
1520 new set. */
1521 save_aliasing_flag = flag_strict_aliasing;
1522 flag_strict_aliasing = 0;
1524 rtl = DECL_RTL (decl);
1525 /* Reset DECL_RTL back, as various parts of the compiler expects
1526 DECL_RTL set meaning it is actually going to be output. */
1527 SET_DECL_RTL (decl, NULL);
1529 flag_strict_aliasing = save_aliasing_flag;
1530 return rtl;
1533 /* Output a string of literal assembler code
1534 for an `asm' keyword used between functions. */
1536 void
1537 assemble_asm (tree string)
1539 const char *p;
1540 app_enable ();
1542 if (TREE_CODE (string) == ADDR_EXPR)
1543 string = TREE_OPERAND (string, 0);
1545 p = TREE_STRING_POINTER (string);
1546 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1549 /* Write the address of the entity given by SYMBOL to SEC. */
1550 void
1551 assemble_addr_to_section (rtx symbol, section *sec)
1553 switch_to_section (sec);
1554 assemble_align (POINTER_SIZE);
1555 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1558 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1559 not) section for PRIORITY. */
1560 section *
1561 get_cdtor_priority_section (int priority, bool constructor_p)
1563 char buf[16];
1565 /* ??? This only works reliably with the GNU linker. */
1566 sprintf (buf, "%s.%.5u",
1567 constructor_p ? ".ctors" : ".dtors",
1568 /* Invert the numbering so the linker puts us in the proper
1569 order; constructors are run from right to left, and the
1570 linker sorts in increasing order. */
1571 MAX_INIT_PRIORITY - priority);
1572 return get_section (buf, SECTION_WRITE, NULL);
1575 void
1576 default_named_section_asm_out_destructor (rtx symbol, int priority)
1578 section *sec;
1580 if (priority != DEFAULT_INIT_PRIORITY)
1581 sec = get_cdtor_priority_section (priority,
1582 /*constructor_p=*/false);
1583 else
1584 sec = get_section (".dtors", SECTION_WRITE, NULL);
1586 assemble_addr_to_section (symbol, sec);
1589 #ifdef DTORS_SECTION_ASM_OP
1590 void
1591 default_dtor_section_asm_out_destructor (rtx symbol,
1592 int priority ATTRIBUTE_UNUSED)
1594 assemble_addr_to_section (symbol, dtors_section);
1596 #endif
1598 void
1599 default_named_section_asm_out_constructor (rtx symbol, int priority)
1601 section *sec;
1603 if (priority != DEFAULT_INIT_PRIORITY)
1604 sec = get_cdtor_priority_section (priority,
1605 /*constructor_p=*/true);
1606 else
1607 sec = get_section (".ctors", SECTION_WRITE, NULL);
1609 assemble_addr_to_section (symbol, sec);
1612 #ifdef CTORS_SECTION_ASM_OP
1613 void
1614 default_ctor_section_asm_out_constructor (rtx symbol,
1615 int priority ATTRIBUTE_UNUSED)
1617 assemble_addr_to_section (symbol, ctors_section);
1619 #endif
1621 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1622 a nonzero value if the constant pool should be output before the
1623 start of the function, or a zero value if the pool should output
1624 after the end of the function. The default is to put it before the
1625 start. */
1627 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1628 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1629 #endif
1631 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1632 to be output to assembler.
1633 Set first_global_object_name and weak_global_object_name as appropriate. */
1635 void
1636 notice_global_symbol (tree decl)
1638 const char **t = &first_global_object_name;
1640 if (first_global_object_name
1641 || !TREE_PUBLIC (decl)
1642 || DECL_EXTERNAL (decl)
1643 || !DECL_NAME (decl)
1644 || (TREE_CODE (decl) == VAR_DECL && DECL_HARD_REGISTER (decl))
1645 || (TREE_CODE (decl) != FUNCTION_DECL
1646 && (TREE_CODE (decl) != VAR_DECL
1647 || (DECL_COMMON (decl)
1648 && (DECL_INITIAL (decl) == 0
1649 || DECL_INITIAL (decl) == error_mark_node)))))
1650 return;
1652 /* We win when global object is found, but it is useful to know about weak
1653 symbol as well so we can produce nicer unique names. */
1654 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1655 t = &weak_global_object_name;
1657 if (!*t)
1659 tree id = DECL_ASSEMBLER_NAME (decl);
1660 ultimate_transparent_alias_target (&id);
1661 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1665 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1666 current function goes into the cold section, so that targets can use
1667 current_function_section during RTL expansion. DECL describes the
1668 function. */
1670 void
1671 decide_function_section (tree decl)
1673 first_function_block_is_cold = false;
1675 if (flag_reorder_blocks_and_partition)
1676 /* We will decide in assemble_start_function. */
1677 return;
1679 if (DECL_SECTION_NAME (decl))
1681 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1682 /* Calls to function_section rely on first_function_block_is_cold
1683 being accurate. */
1684 first_function_block_is_cold = (node
1685 && node->frequency
1686 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1689 in_cold_section_p = first_function_block_is_cold;
1692 /* Get the function's name, as described by its RTL. This may be
1693 different from the DECL_NAME name used in the source file. */
1694 const char *
1695 get_fnname_from_decl (tree decl)
1697 rtx x = DECL_RTL (decl);
1698 gcc_assert (MEM_P (x));
1699 x = XEXP (x, 0);
1700 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1701 return XSTR (x, 0);
1704 /* Output assembler code for the constant pool of a function and associated
1705 with defining the name of the function. DECL describes the function.
1706 NAME is the function's name. For the constant pool, we use the current
1707 constant pool data. */
1709 void
1710 assemble_start_function (tree decl, const char *fnname)
1712 int align;
1713 char tmp_label[100];
1714 bool hot_label_written = false;
1716 if (flag_reorder_blocks_and_partition)
1718 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1719 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1720 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1721 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1722 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1723 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1724 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1725 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1726 const_labelno++;
1727 cold_function_name = NULL_TREE;
1729 else
1731 crtl->subsections.hot_section_label = NULL;
1732 crtl->subsections.cold_section_label = NULL;
1733 crtl->subsections.hot_section_end_label = NULL;
1734 crtl->subsections.cold_section_end_label = NULL;
1737 /* The following code does not need preprocessing in the assembler. */
1739 app_disable ();
1741 if (CONSTANT_POOL_BEFORE_FUNCTION)
1742 output_constant_pool (fnname, decl);
1744 align = symtab_node::get (decl)->definition_alignment ();
1746 /* Make sure the not and cold text (code) sections are properly
1747 aligned. This is necessary here in the case where the function
1748 has both hot and cold sections, because we don't want to re-set
1749 the alignment when the section switch happens mid-function. */
1751 if (flag_reorder_blocks_and_partition)
1753 first_function_block_is_cold = false;
1755 switch_to_section (unlikely_text_section ());
1756 assemble_align (align);
1757 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1759 /* When the function starts with a cold section, we need to explicitly
1760 align the hot section and write out the hot section label.
1761 But if the current function is a thunk, we do not have a CFG. */
1762 if (!cfun->is_thunk
1763 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1765 switch_to_section (text_section);
1766 assemble_align (align);
1767 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1768 hot_label_written = true;
1769 first_function_block_is_cold = true;
1771 in_cold_section_p = first_function_block_is_cold;
1775 /* Switch to the correct text section for the start of the function. */
1777 switch_to_section (function_section (decl));
1778 if (flag_reorder_blocks_and_partition
1779 && !hot_label_written)
1780 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1782 /* Tell assembler to move to target machine's alignment for functions. */
1783 align = floor_log2 (align / BITS_PER_UNIT);
1784 if (align > 0)
1786 ASM_OUTPUT_ALIGN (asm_out_file, align);
1789 /* Handle a user-specified function alignment.
1790 Note that we still need to align to DECL_ALIGN, as above,
1791 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1792 if (! DECL_USER_ALIGN (decl)
1793 && align_functions_log > align
1794 && optimize_function_for_speed_p (cfun))
1796 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1797 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1798 align_functions_log, align_functions - 1);
1799 #else
1800 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1801 #endif
1804 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1805 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1806 #endif
1808 if (!DECL_IGNORED_P (decl))
1809 (*debug_hooks->begin_function) (decl);
1811 /* Make function name accessible from other files, if appropriate. */
1813 if (TREE_PUBLIC (decl)
1814 || (cgraph_node::get (decl)->instrumentation_clone
1815 && cgraph_node::get (decl)->instrumented_version
1816 && TREE_PUBLIC (cgraph_node::get (decl)->instrumented_version->decl)))
1818 notice_global_symbol (decl);
1820 globalize_decl (decl);
1822 maybe_assemble_visibility (decl);
1825 if (DECL_PRESERVE_P (decl))
1826 targetm.asm_out.mark_decl_preserved (fnname);
1828 /* Do any machine/system dependent processing of the function name. */
1829 #ifdef ASM_DECLARE_FUNCTION_NAME
1830 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1831 #else
1832 /* Standard thing is just output label for the function. */
1833 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1834 #endif /* ASM_DECLARE_FUNCTION_NAME */
1836 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1837 saw_no_split_stack = true;
1840 /* Output assembler code associated with defining the size of the
1841 function. DECL describes the function. NAME is the function's name. */
1843 void
1844 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1846 #ifdef ASM_DECLARE_FUNCTION_SIZE
1847 /* We could have switched section in the middle of the function. */
1848 if (flag_reorder_blocks_and_partition)
1849 switch_to_section (function_section (decl));
1850 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1851 #endif
1852 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1854 output_constant_pool (fnname, decl);
1855 switch_to_section (function_section (decl)); /* need to switch back */
1857 /* Output labels for end of hot/cold text sections (to be used by
1858 debug info.) */
1859 if (flag_reorder_blocks_and_partition)
1861 section *save_text_section;
1863 save_text_section = in_section;
1864 switch_to_section (unlikely_text_section ());
1865 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
1866 if (cold_function_name != NULL_TREE)
1867 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
1868 IDENTIFIER_POINTER (cold_function_name),
1869 decl);
1870 #endif
1871 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1872 if (first_function_block_is_cold)
1873 switch_to_section (text_section);
1874 else
1875 switch_to_section (function_section (decl));
1876 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1877 switch_to_section (save_text_section);
1881 /* Assemble code to leave SIZE bytes of zeros. */
1883 void
1884 assemble_zeros (unsigned HOST_WIDE_INT size)
1886 /* Do no output if -fsyntax-only. */
1887 if (flag_syntax_only)
1888 return;
1890 #ifdef ASM_NO_SKIP_IN_TEXT
1891 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1892 so we must output 0s explicitly in the text section. */
1893 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1895 unsigned HOST_WIDE_INT i;
1896 for (i = 0; i < size; i++)
1897 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1899 else
1900 #endif
1901 if (size > 0)
1902 ASM_OUTPUT_SKIP (asm_out_file, size);
1905 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1907 void
1908 assemble_align (int align)
1910 if (align > BITS_PER_UNIT)
1912 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1916 /* Assemble a string constant with the specified C string as contents. */
1918 void
1919 assemble_string (const char *p, int size)
1921 int pos = 0;
1922 int maximum = 2000;
1924 /* If the string is very long, split it up. */
1926 while (pos < size)
1928 int thissize = size - pos;
1929 if (thissize > maximum)
1930 thissize = maximum;
1932 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1934 pos += thissize;
1935 p += thissize;
1940 /* A noswitch_section_callback for lcomm_section. */
1942 static bool
1943 emit_local (tree decl ATTRIBUTE_UNUSED,
1944 const char *name ATTRIBUTE_UNUSED,
1945 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1946 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1948 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1949 unsigned int align = symtab_node::get (decl)->definition_alignment ();
1950 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1951 size, align);
1952 return true;
1953 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1954 unsigned int align = symtab_node::get (decl)->definition_alignment ();
1955 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
1956 return true;
1957 #else
1958 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1959 return false;
1960 #endif
1963 /* A noswitch_section_callback for bss_noswitch_section. */
1965 #if defined ASM_OUTPUT_ALIGNED_BSS
1966 static bool
1967 emit_bss (tree decl ATTRIBUTE_UNUSED,
1968 const char *name ATTRIBUTE_UNUSED,
1969 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1970 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1972 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1973 get_variable_align (decl));
1974 return true;
1976 #endif
1978 /* A noswitch_section_callback for comm_section. */
1980 static bool
1981 emit_common (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 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1987 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1988 size, get_variable_align (decl));
1989 return true;
1990 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1991 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1992 get_variable_align (decl));
1993 return true;
1994 #else
1995 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1996 return false;
1997 #endif
2000 /* A noswitch_section_callback for tls_comm_section. */
2002 static bool
2003 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2004 const char *name ATTRIBUTE_UNUSED,
2005 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2006 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2008 #ifdef ASM_OUTPUT_TLS_COMMON
2009 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2010 return true;
2011 #else
2012 sorry ("thread-local COMMON data not implemented");
2013 return true;
2014 #endif
2017 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2018 NAME is the name of DECL's SYMBOL_REF. */
2020 static void
2021 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2022 unsigned int align)
2024 unsigned HOST_WIDE_INT size, rounded;
2026 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2027 rounded = size;
2029 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2030 size += asan_red_zone_size (size);
2032 /* Don't allocate zero bytes of common,
2033 since that means "undefined external" in the linker. */
2034 if (size == 0)
2035 rounded = 1;
2037 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2038 so that each uninitialized object starts on such a boundary. */
2039 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2040 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2041 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2043 if (!sect->noswitch.callback (decl, name, size, rounded)
2044 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2045 error ("requested alignment for %q+D is greater than "
2046 "implemented alignment of %wu", decl, rounded);
2049 /* A subroutine of assemble_variable. Output the label and contents of
2050 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2051 is as for assemble_variable. */
2053 static void
2054 assemble_variable_contents (tree decl, const char *name,
2055 bool dont_output_data)
2057 /* Do any machine/system dependent processing of the object. */
2058 #ifdef ASM_DECLARE_OBJECT_NAME
2059 last_assemble_variable_decl = decl;
2060 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2061 #else
2062 /* Standard thing is just output label for the object. */
2063 ASM_OUTPUT_LABEL (asm_out_file, name);
2064 #endif /* ASM_DECLARE_OBJECT_NAME */
2066 if (!dont_output_data)
2068 /* Caller is supposed to use varpool_get_constructor when it wants
2069 to output the body. */
2070 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2071 if (DECL_INITIAL (decl)
2072 && DECL_INITIAL (decl) != error_mark_node
2073 && !initializer_zerop (DECL_INITIAL (decl)))
2074 /* Output the actual data. */
2075 output_constant (DECL_INITIAL (decl),
2076 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2077 get_variable_align (decl));
2078 else
2079 /* Leave space for it. */
2080 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2081 targetm.asm_out.decl_end ();
2085 /* Write out assembly for the variable DECL, which is not defined in
2086 the current translation unit. */
2087 void
2088 assemble_undefined_decl (tree decl)
2090 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2091 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2094 /* Assemble everything that is needed for a variable or function declaration.
2095 Not used for automatic variables, and not used for function definitions.
2096 Should not be called for variables of incomplete structure type.
2098 TOP_LEVEL is nonzero if this variable has file scope.
2099 AT_END is nonzero if this is the special handling, at end of compilation,
2100 to define things that have had only tentative definitions.
2101 DONT_OUTPUT_DATA if nonzero means don't actually output the
2102 initial value (that will be done by the caller). */
2104 void
2105 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2106 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2108 const char *name;
2109 rtx decl_rtl, symbol;
2110 section *sect;
2111 unsigned int align;
2112 bool asan_protected = false;
2114 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2115 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2117 /* Emulated TLS had better not get this far. */
2118 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2120 last_assemble_variable_decl = 0;
2122 /* Normally no need to say anything here for external references,
2123 since assemble_external is called by the language-specific code
2124 when a declaration is first seen. */
2126 if (DECL_EXTERNAL (decl))
2127 return;
2129 /* Do nothing for global register variables. */
2130 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2132 TREE_ASM_WRITTEN (decl) = 1;
2133 return;
2136 /* If type was incomplete when the variable was declared,
2137 see if it is complete now. */
2139 if (DECL_SIZE (decl) == 0)
2140 layout_decl (decl, 0);
2142 /* Still incomplete => don't allocate it; treat the tentative defn
2143 (which is what it must have been) as an `extern' reference. */
2145 if (!dont_output_data && DECL_SIZE (decl) == 0)
2147 error ("storage size of %q+D isn%'t known", decl);
2148 TREE_ASM_WRITTEN (decl) = 1;
2149 return;
2152 /* The first declaration of a variable that comes through this function
2153 decides whether it is global (in C, has external linkage)
2154 or local (in C, has internal linkage). So do nothing more
2155 if this function has already run. */
2157 if (TREE_ASM_WRITTEN (decl))
2158 return;
2160 /* Make sure targetm.encode_section_info is invoked before we set
2161 ASM_WRITTEN. */
2162 decl_rtl = DECL_RTL (decl);
2164 TREE_ASM_WRITTEN (decl) = 1;
2166 /* Do no output if -fsyntax-only. */
2167 if (flag_syntax_only)
2168 return;
2170 if (! dont_output_data
2171 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2173 error ("size of variable %q+D is too large", decl);
2174 return;
2177 gcc_assert (MEM_P (decl_rtl));
2178 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2179 symbol = XEXP (decl_rtl, 0);
2181 /* If this symbol belongs to the tree constant pool, output the constant
2182 if it hasn't already been written. */
2183 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2185 tree decl = SYMBOL_REF_DECL (symbol);
2186 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2187 output_constant_def_contents (symbol);
2188 return;
2191 app_disable ();
2193 name = XSTR (symbol, 0);
2194 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2195 notice_global_symbol (decl);
2197 /* Compute the alignment of this data. */
2199 align_variable (decl, dont_output_data);
2201 if ((flag_sanitize & SANITIZE_ADDRESS)
2202 && asan_protect_global (decl))
2204 asan_protected = true;
2205 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2206 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2209 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2211 align = get_variable_align (decl);
2213 if (TREE_PUBLIC (decl))
2214 maybe_assemble_visibility (decl);
2216 if (DECL_PRESERVE_P (decl))
2217 targetm.asm_out.mark_decl_preserved (name);
2219 /* First make the assembler name(s) global if appropriate. */
2220 sect = get_variable_section (decl, false);
2221 if (TREE_PUBLIC (decl)
2222 && (sect->common.flags & SECTION_COMMON) == 0)
2223 globalize_decl (decl);
2225 /* Output any data that we will need to use the address of. */
2226 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2227 output_addressed_constants (DECL_INITIAL (decl));
2229 /* dbxout.c needs to know this. */
2230 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2231 DECL_IN_TEXT_SECTION (decl) = 1;
2233 /* If the decl is part of an object_block, make sure that the decl
2234 has been positioned within its block, but do not write out its
2235 definition yet. output_object_blocks will do that later. */
2236 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2238 gcc_assert (!dont_output_data);
2239 place_block_symbol (symbol);
2241 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2242 assemble_noswitch_variable (decl, name, sect, align);
2243 else
2245 /* The following bit of code ensures that vtable_map
2246 variables are not only in the comdat section, but that
2247 each variable has its own unique comdat name. If this
2248 code is removed, the variables end up in the same section
2249 with a single comdat name.
2251 FIXME: resolve_unique_section needs to deal better with
2252 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
2253 that is fixed, this if-else statement can be replaced with
2254 a single call to "switch_to_section (sect)". */
2255 if (sect->named.name
2256 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2258 #if defined (OBJECT_FORMAT_ELF)
2259 targetm.asm_out.named_section (sect->named.name,
2260 sect->named.common.flags
2261 | SECTION_LINKONCE,
2262 DECL_NAME (decl));
2263 in_section = sect;
2264 #elif defined (TARGET_PECOFF)
2265 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
2266 Therefore the following check is used.
2267 In case a the target is PE or COFF a comdat group section
2268 is created, e.g. .vtable_map_vars$foo. The linker places
2269 everything in .vtable_map_vars at the end.
2271 A fix could be made in
2272 gcc/config/i386/winnt.c: i386_pe_unique_section. */
2273 if (TARGET_PECOFF)
2275 char *name;
2277 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
2278 name = ACONCAT ((sect->named.name, "$",
2279 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
2280 else
2281 name = ACONCAT ((sect->named.name, "$",
2282 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
2283 NULL));
2285 targetm.asm_out.named_section (name,
2286 sect->named.common.flags
2287 | SECTION_LINKONCE,
2288 DECL_NAME (decl));
2289 in_section = sect;
2291 #else
2292 switch_to_section (sect);
2293 #endif
2295 else
2296 switch_to_section (sect);
2297 if (align > BITS_PER_UNIT)
2298 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2299 assemble_variable_contents (decl, name, dont_output_data);
2300 if (asan_protected)
2302 unsigned HOST_WIDE_INT int size
2303 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2304 assemble_zeros (asan_red_zone_size (size));
2310 /* Given a function declaration (FN_DECL), this function assembles the
2311 function into the .preinit_array section. */
2313 void
2314 assemble_vtv_preinit_initializer (tree fn_decl)
2316 section *sect;
2317 unsigned flags = SECTION_WRITE;
2318 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2320 flags |= SECTION_NOTYPE;
2321 sect = get_section (".preinit_array", flags, fn_decl);
2322 switch_to_section (sect);
2323 assemble_addr_to_section (symbol, sect);
2326 /* Return 1 if type TYPE contains any pointers. */
2328 static int
2329 contains_pointers_p (tree type)
2331 switch (TREE_CODE (type))
2333 case POINTER_TYPE:
2334 case REFERENCE_TYPE:
2335 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2336 so I'll play safe and return 1. */
2337 case OFFSET_TYPE:
2338 return 1;
2340 case RECORD_TYPE:
2341 case UNION_TYPE:
2342 case QUAL_UNION_TYPE:
2344 tree fields;
2345 /* For a type that has fields, see if the fields have pointers. */
2346 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2347 if (TREE_CODE (fields) == FIELD_DECL
2348 && contains_pointers_p (TREE_TYPE (fields)))
2349 return 1;
2350 return 0;
2353 case ARRAY_TYPE:
2354 /* An array type contains pointers if its element type does. */
2355 return contains_pointers_p (TREE_TYPE (type));
2357 default:
2358 return 0;
2362 /* We delay assemble_external processing until
2363 the compilation unit is finalized. This is the best we can do for
2364 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2365 it all the way to final. See PR 17982 for further discussion. */
2366 static GTY(()) tree pending_assemble_externals;
2368 #ifdef ASM_OUTPUT_EXTERNAL
2369 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2370 As a result, assemble_external can be called after the list of externals
2371 is processed and the pointer set destroyed. */
2372 static bool pending_assemble_externals_processed;
2374 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2375 TREE_LIST in assemble_external. */
2376 static hash_set<tree> *pending_assemble_externals_set;
2378 /* True if DECL is a function decl for which no out-of-line copy exists.
2379 It is assumed that DECL's assembler name has been set. */
2381 static bool
2382 incorporeal_function_p (tree decl)
2384 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2386 const char *name;
2388 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2389 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2390 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2391 return true;
2393 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2394 /* Atomic or sync builtins which have survived this far will be
2395 resolved externally and therefore are not incorporeal. */
2396 if (strncmp (name, "__builtin_", 10) == 0)
2397 return true;
2399 return false;
2402 /* Actually do the tests to determine if this is necessary, and invoke
2403 ASM_OUTPUT_EXTERNAL. */
2404 static void
2405 assemble_external_real (tree decl)
2407 rtx rtl = DECL_RTL (decl);
2409 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2410 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2411 && !incorporeal_function_p (decl))
2413 /* Some systems do require some output. */
2414 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2415 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2418 #endif
2420 void
2421 process_pending_assemble_externals (void)
2423 #ifdef ASM_OUTPUT_EXTERNAL
2424 tree list;
2425 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2426 assemble_external_real (TREE_VALUE (list));
2428 pending_assemble_externals = 0;
2429 pending_assemble_externals_processed = true;
2430 delete pending_assemble_externals_set;
2431 #endif
2434 /* This TREE_LIST contains any weak symbol declarations waiting
2435 to be emitted. */
2436 static GTY(()) tree weak_decls;
2438 /* Output something to declare an external symbol to the assembler,
2439 and qualifiers such as weakness. (Most assemblers don't need
2440 extern declaration, so we normally output nothing.) Do nothing if
2441 DECL is not external. */
2443 void
2444 assemble_external (tree decl ATTRIBUTE_UNUSED)
2446 /* Make sure that the ASM_OUT_FILE is open.
2447 If it's not, we should not be calling this function. */
2448 gcc_assert (asm_out_file);
2450 /* In a perfect world, the following condition would be true.
2451 Sadly, the Java and Go front ends emit assembly *from the front end*,
2452 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2453 #if 0
2454 /* This function should only be called if we are expanding, or have
2455 expanded, to RTL.
2456 Ideally, only final.c would be calling this function, but it is
2457 not clear whether that would break things somehow. See PR 17982
2458 for further discussion. */
2459 gcc_assert (state == EXPANSION
2460 || state == FINISHED);
2461 #endif
2463 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2464 return;
2466 /* We want to output annotation for weak and external symbols at
2467 very last to check if they are references or not. */
2469 if (TARGET_SUPPORTS_WEAK
2470 && DECL_WEAK (decl)
2471 /* TREE_STATIC is a weird and abused creature which is not
2472 generally the right test for whether an entity has been
2473 locally emitted, inlined or otherwise not-really-extern, but
2474 for declarations that can be weak, it happens to be
2475 match. */
2476 && !TREE_STATIC (decl)
2477 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2478 && value_member (decl, weak_decls) == NULL_TREE)
2479 weak_decls = tree_cons (NULL, decl, weak_decls);
2481 #ifdef ASM_OUTPUT_EXTERNAL
2482 if (pending_assemble_externals_processed)
2484 assemble_external_real (decl);
2485 return;
2488 if (! pending_assemble_externals_set->add (decl))
2489 pending_assemble_externals = tree_cons (NULL, decl,
2490 pending_assemble_externals);
2491 #endif
2494 /* Similar, for calling a library function FUN. */
2496 void
2497 assemble_external_libcall (rtx fun)
2499 /* Declare library function name external when first used, if nec. */
2500 if (! SYMBOL_REF_USED (fun))
2502 SYMBOL_REF_USED (fun) = 1;
2503 targetm.asm_out.external_libcall (fun);
2507 /* Assemble a label named NAME. */
2509 void
2510 assemble_label (FILE *file, const char *name)
2512 ASM_OUTPUT_LABEL (file, name);
2515 /* Set the symbol_referenced flag for ID. */
2516 void
2517 mark_referenced (tree id)
2519 TREE_SYMBOL_REFERENCED (id) = 1;
2522 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2523 void
2524 mark_decl_referenced (tree decl)
2526 if (TREE_CODE (decl) == FUNCTION_DECL)
2528 /* Extern inline functions don't become needed when referenced.
2529 If we know a method will be emitted in other TU and no new
2530 functions can be marked reachable, just use the external
2531 definition. */
2532 struct cgraph_node *node = cgraph_node::get_create (decl);
2533 if (!DECL_EXTERNAL (decl)
2534 && !node->definition)
2535 node->mark_force_output ();
2537 else if (TREE_CODE (decl) == VAR_DECL)
2539 varpool_node *node = varpool_node::get_create (decl);
2540 /* C++ frontend use mark_decl_references to force COMDAT variables
2541 to be output that might appear dead otherwise. */
2542 node->force_output = true;
2544 /* else do nothing - we can get various sorts of CST nodes here,
2545 which do not need to be marked. */
2549 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2550 starts with a *, the rest of NAME is output verbatim. Otherwise
2551 NAME is transformed in a target-specific way (usually by the
2552 addition of an underscore). */
2554 void
2555 assemble_name_raw (FILE *file, const char *name)
2557 if (name[0] == '*')
2558 fputs (&name[1], file);
2559 else
2560 ASM_OUTPUT_LABELREF (file, name);
2563 /* Like assemble_name_raw, but should be used when NAME might refer to
2564 an entity that is also represented as a tree (like a function or
2565 variable). If NAME does refer to such an entity, that entity will
2566 be marked as referenced. */
2568 void
2569 assemble_name (FILE *file, const char *name)
2571 const char *real_name;
2572 tree id;
2574 real_name = targetm.strip_name_encoding (name);
2576 id = maybe_get_identifier (real_name);
2577 if (id)
2579 tree id_orig = id;
2581 mark_referenced (id);
2582 ultimate_transparent_alias_target (&id);
2583 if (id != id_orig)
2584 name = IDENTIFIER_POINTER (id);
2585 gcc_assert (! TREE_CHAIN (id));
2588 assemble_name_raw (file, name);
2591 /* Allocate SIZE bytes writable static space with a gensym name
2592 and return an RTX to refer to its address. */
2595 assemble_static_space (unsigned HOST_WIDE_INT size)
2597 char name[12];
2598 const char *namestring;
2599 rtx x;
2601 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2602 ++const_labelno;
2603 namestring = ggc_strdup (name);
2605 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2606 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2608 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2609 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2610 BIGGEST_ALIGNMENT);
2611 #else
2612 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2613 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2614 #else
2616 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2617 so that each uninitialized object starts on such a boundary. */
2618 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2619 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2620 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2621 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2622 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2623 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2625 #endif
2626 #endif
2627 return x;
2630 /* Assemble the static constant template for function entry trampolines.
2631 This is done at most once per compilation.
2632 Returns an RTX for the address of the template. */
2634 static GTY(()) rtx initial_trampoline;
2637 assemble_trampoline_template (void)
2639 char label[256];
2640 const char *name;
2641 int align;
2642 rtx symbol;
2644 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2646 if (initial_trampoline)
2647 return initial_trampoline;
2649 /* By default, put trampoline templates in read-only data section. */
2651 #ifdef TRAMPOLINE_SECTION
2652 switch_to_section (TRAMPOLINE_SECTION);
2653 #else
2654 switch_to_section (readonly_data_section);
2655 #endif
2657 /* Write the assembler code to define one. */
2658 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2659 if (align > 0)
2660 ASM_OUTPUT_ALIGN (asm_out_file, align);
2662 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2663 targetm.asm_out.trampoline_template (asm_out_file);
2665 /* Record the rtl to refer to it. */
2666 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2667 name = ggc_strdup (label);
2668 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2669 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2671 initial_trampoline = gen_const_mem (BLKmode, symbol);
2672 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2673 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2675 return initial_trampoline;
2678 /* A and B are either alignments or offsets. Return the minimum alignment
2679 that may be assumed after adding the two together. */
2681 static inline unsigned
2682 min_align (unsigned int a, unsigned int b)
2684 return (a | b) & -(a | b);
2687 /* Return the assembler directive for creating a given kind of integer
2688 object. SIZE is the number of bytes in the object and ALIGNED_P
2689 indicates whether it is known to be aligned. Return NULL if the
2690 assembly dialect has no such directive.
2692 The returned string should be printed at the start of a new line and
2693 be followed immediately by the object's initial value. */
2695 const char *
2696 integer_asm_op (int size, int aligned_p)
2698 struct asm_int_op *ops;
2700 if (aligned_p)
2701 ops = &targetm.asm_out.aligned_op;
2702 else
2703 ops = &targetm.asm_out.unaligned_op;
2705 switch (size)
2707 case 1:
2708 return targetm.asm_out.byte_op;
2709 case 2:
2710 return ops->hi;
2711 case 4:
2712 return ops->si;
2713 case 8:
2714 return ops->di;
2715 case 16:
2716 return ops->ti;
2717 default:
2718 return NULL;
2722 /* Use directive OP to assemble an integer object X. Print OP at the
2723 start of the line, followed immediately by the value of X. */
2725 void
2726 assemble_integer_with_op (const char *op, rtx x)
2728 fputs (op, asm_out_file);
2729 output_addr_const (asm_out_file, x);
2730 fputc ('\n', asm_out_file);
2733 /* The default implementation of the asm_out.integer target hook. */
2735 bool
2736 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2737 unsigned int size ATTRIBUTE_UNUSED,
2738 int aligned_p ATTRIBUTE_UNUSED)
2740 const char *op = integer_asm_op (size, aligned_p);
2741 /* Avoid GAS bugs for large values. Specifically negative values whose
2742 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2743 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2744 return false;
2745 return op && (assemble_integer_with_op (op, x), true);
2748 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2749 the alignment of the integer in bits. Return 1 if we were able to output
2750 the constant, otherwise 0. We must be able to output the constant,
2751 if FORCE is nonzero. */
2753 bool
2754 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2756 int aligned_p;
2758 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2760 /* See if the target hook can handle this kind of object. */
2761 if (targetm.asm_out.integer (x, size, aligned_p))
2762 return true;
2764 /* If the object is a multi-byte one, try splitting it up. Split
2765 it into words it if is multi-word, otherwise split it into bytes. */
2766 if (size > 1)
2768 machine_mode omode, imode;
2769 unsigned int subalign;
2770 unsigned int subsize, i;
2771 enum mode_class mclass;
2773 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2774 subalign = MIN (align, subsize * BITS_PER_UNIT);
2775 if (GET_CODE (x) == CONST_FIXED)
2776 mclass = GET_MODE_CLASS (GET_MODE (x));
2777 else
2778 mclass = MODE_INT;
2780 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2781 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2783 for (i = 0; i < size; i += subsize)
2785 rtx partial = simplify_subreg (omode, x, imode, i);
2786 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2787 break;
2789 if (i == size)
2790 return true;
2792 /* If we've printed some of it, but not all of it, there's no going
2793 back now. */
2794 gcc_assert (!i);
2797 gcc_assert (!force);
2799 return false;
2802 void
2803 assemble_real (REAL_VALUE_TYPE d, machine_mode mode, unsigned int align)
2805 long data[4] = {0, 0, 0, 0};
2806 int i;
2807 int bitsize, nelts, nunits, units_per;
2809 /* This is hairy. We have a quantity of known size. real_to_target
2810 will put it into an array of *host* longs, 32 bits per element
2811 (even if long is more than 32 bits). We need to determine the
2812 number of array elements that are occupied (nelts) and the number
2813 of *target* min-addressable units that will be occupied in the
2814 object file (nunits). We cannot assume that 32 divides the
2815 mode's bitsize (size * BITS_PER_UNIT) evenly.
2817 size * BITS_PER_UNIT is used here to make sure that padding bits
2818 (which might appear at either end of the value; real_to_target
2819 will include the padding bits in its output array) are included. */
2821 nunits = GET_MODE_SIZE (mode);
2822 bitsize = nunits * BITS_PER_UNIT;
2823 nelts = CEIL (bitsize, 32);
2824 units_per = 32 / BITS_PER_UNIT;
2826 real_to_target (data, &d, mode);
2828 /* Put out the first word with the specified alignment. */
2829 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2830 nunits -= units_per;
2832 /* Subsequent words need only 32-bit alignment. */
2833 align = min_align (align, 32);
2835 for (i = 1; i < nelts; i++)
2837 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2838 nunits -= units_per;
2842 /* Given an expression EXP with a constant value,
2843 reduce it to the sum of an assembler symbol and an integer.
2844 Store them both in the structure *VALUE.
2845 EXP must be reducible. */
2847 struct addr_const {
2848 rtx base;
2849 HOST_WIDE_INT offset;
2852 static void
2853 decode_addr_const (tree exp, struct addr_const *value)
2855 tree target = TREE_OPERAND (exp, 0);
2856 int offset = 0;
2857 rtx x;
2859 while (1)
2861 if (TREE_CODE (target) == COMPONENT_REF
2862 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2864 offset += int_byte_position (TREE_OPERAND (target, 1));
2865 target = TREE_OPERAND (target, 0);
2867 else if (TREE_CODE (target) == ARRAY_REF
2868 || TREE_CODE (target) == ARRAY_RANGE_REF)
2870 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2871 * tree_to_shwi (TREE_OPERAND (target, 1)));
2872 target = TREE_OPERAND (target, 0);
2874 else if (TREE_CODE (target) == MEM_REF
2875 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2877 offset += mem_ref_offset (target).to_short_addr ();
2878 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2880 else if (TREE_CODE (target) == INDIRECT_REF
2881 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2882 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2883 == ADDR_EXPR)
2884 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2885 else
2886 break;
2889 switch (TREE_CODE (target))
2891 case VAR_DECL:
2892 case FUNCTION_DECL:
2893 x = DECL_RTL (target);
2894 break;
2896 case LABEL_DECL:
2897 x = gen_rtx_MEM (FUNCTION_MODE,
2898 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2899 break;
2901 case REAL_CST:
2902 case FIXED_CST:
2903 case STRING_CST:
2904 case COMPLEX_CST:
2905 case CONSTRUCTOR:
2906 case INTEGER_CST:
2907 x = output_constant_def (target, 1);
2908 break;
2910 default:
2911 gcc_unreachable ();
2914 gcc_assert (MEM_P (x));
2915 x = XEXP (x, 0);
2917 value->base = x;
2918 value->offset = offset;
2921 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2923 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2925 /* Constant pool accessor function. */
2927 hash_table<tree_descriptor_hasher> *
2928 constant_pool_htab (void)
2930 return const_desc_htab;
2933 /* Compute a hash code for a constant expression. */
2935 hashval_t
2936 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2938 return ptr->hash;
2941 static hashval_t
2942 const_hash_1 (const tree exp)
2944 const char *p;
2945 hashval_t hi;
2946 int len, i;
2947 enum tree_code code = TREE_CODE (exp);
2949 /* Either set P and LEN to the address and len of something to hash and
2950 exit the switch or return a value. */
2952 switch (code)
2954 case INTEGER_CST:
2955 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2956 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2957 break;
2959 case REAL_CST:
2960 return real_hash (TREE_REAL_CST_PTR (exp));
2962 case FIXED_CST:
2963 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2965 case STRING_CST:
2966 p = TREE_STRING_POINTER (exp);
2967 len = TREE_STRING_LENGTH (exp);
2968 break;
2970 case COMPLEX_CST:
2971 return (const_hash_1 (TREE_REALPART (exp)) * 5
2972 + const_hash_1 (TREE_IMAGPART (exp)));
2974 case VECTOR_CST:
2976 unsigned i;
2978 hi = 7 + VECTOR_CST_NELTS (exp);
2980 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2981 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2983 return hi;
2986 case CONSTRUCTOR:
2988 unsigned HOST_WIDE_INT idx;
2989 tree value;
2991 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2993 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2994 if (value)
2995 hi = hi * 603 + const_hash_1 (value);
2997 return hi;
3000 case ADDR_EXPR:
3001 case FDESC_EXPR:
3003 struct addr_const value;
3005 decode_addr_const (exp, &value);
3006 switch (GET_CODE (value.base))
3008 case SYMBOL_REF:
3009 /* Don't hash the address of the SYMBOL_REF;
3010 only use the offset and the symbol name. */
3011 hi = value.offset;
3012 p = XSTR (value.base, 0);
3013 for (i = 0; p[i] != 0; i++)
3014 hi = ((hi * 613) + (unsigned) (p[i]));
3015 break;
3017 case LABEL_REF:
3018 hi = (value.offset
3019 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
3020 break;
3022 default:
3023 gcc_unreachable ();
3026 return hi;
3028 case PLUS_EXPR:
3029 case POINTER_PLUS_EXPR:
3030 case MINUS_EXPR:
3031 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3032 + const_hash_1 (TREE_OPERAND (exp, 1)));
3034 CASE_CONVERT:
3035 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3037 default:
3038 /* A language specific constant. Just hash the code. */
3039 return code;
3042 /* Compute hashing function. */
3043 hi = len;
3044 for (i = 0; i < len; i++)
3045 hi = ((hi * 613) + (unsigned) (p[i]));
3047 return hi;
3050 /* Wrapper of compare_constant, for the htab interface. */
3051 bool
3052 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3053 constant_descriptor_tree *c2)
3055 if (c1->hash != c2->hash)
3056 return 0;
3057 return compare_constant (c1->value, c2->value);
3060 /* Compare t1 and t2, and return 1 only if they are known to result in
3061 the same bit pattern on output. */
3063 static int
3064 compare_constant (const tree t1, const tree t2)
3066 enum tree_code typecode;
3068 if (t1 == NULL_TREE)
3069 return t2 == NULL_TREE;
3070 if (t2 == NULL_TREE)
3071 return 0;
3073 if (TREE_CODE (t1) != TREE_CODE (t2))
3074 return 0;
3076 switch (TREE_CODE (t1))
3078 case INTEGER_CST:
3079 /* Integer constants are the same only if the same width of type. */
3080 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3081 return 0;
3082 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3083 return 0;
3084 return tree_int_cst_equal (t1, t2);
3086 case REAL_CST:
3087 /* Real constants are the same only if the same width of type. */
3088 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3089 return 0;
3091 return REAL_VALUES_IDENTICAL (TREE_REAL_CST (t1), TREE_REAL_CST (t2));
3093 case FIXED_CST:
3094 /* Fixed constants are the same only if the same width of type. */
3095 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3096 return 0;
3098 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3100 case STRING_CST:
3101 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3102 return 0;
3104 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3105 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3106 TREE_STRING_LENGTH (t1)));
3108 case COMPLEX_CST:
3109 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3110 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3112 case VECTOR_CST:
3114 unsigned i;
3116 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3117 return 0;
3119 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3120 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3121 VECTOR_CST_ELT (t2, i)))
3122 return 0;
3124 return 1;
3127 case CONSTRUCTOR:
3129 vec<constructor_elt, va_gc> *v1, *v2;
3130 unsigned HOST_WIDE_INT idx;
3132 typecode = TREE_CODE (TREE_TYPE (t1));
3133 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3134 return 0;
3136 if (typecode == ARRAY_TYPE)
3138 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3139 /* For arrays, check that the sizes all match. */
3140 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3141 || size_1 == -1
3142 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3143 return 0;
3145 else
3147 /* For record and union constructors, require exact type
3148 equality. */
3149 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3150 return 0;
3153 v1 = CONSTRUCTOR_ELTS (t1);
3154 v2 = CONSTRUCTOR_ELTS (t2);
3155 if (vec_safe_length (v1) != vec_safe_length (v2))
3156 return 0;
3158 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3160 constructor_elt *c1 = &(*v1)[idx];
3161 constructor_elt *c2 = &(*v2)[idx];
3163 /* Check that each value is the same... */
3164 if (!compare_constant (c1->value, c2->value))
3165 return 0;
3166 /* ... and that they apply to the same fields! */
3167 if (typecode == ARRAY_TYPE)
3169 if (!compare_constant (c1->index, c2->index))
3170 return 0;
3172 else
3174 if (c1->index != c2->index)
3175 return 0;
3179 return 1;
3182 case ADDR_EXPR:
3183 case FDESC_EXPR:
3185 struct addr_const value1, value2;
3186 enum rtx_code code;
3187 int ret;
3189 decode_addr_const (t1, &value1);
3190 decode_addr_const (t2, &value2);
3192 if (value1.offset != value2.offset)
3193 return 0;
3195 code = GET_CODE (value1.base);
3196 if (code != GET_CODE (value2.base))
3197 return 0;
3199 switch (code)
3201 case SYMBOL_REF:
3202 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3203 break;
3205 case LABEL_REF:
3206 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3207 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3208 break;
3210 default:
3211 gcc_unreachable ();
3213 return ret;
3216 case PLUS_EXPR:
3217 case POINTER_PLUS_EXPR:
3218 case MINUS_EXPR:
3219 case RANGE_EXPR:
3220 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3221 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3223 CASE_CONVERT:
3224 case VIEW_CONVERT_EXPR:
3225 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3227 default:
3228 return 0;
3231 gcc_unreachable ();
3234 /* Return the section into which constant EXP should be placed. */
3236 static section *
3237 get_constant_section (tree exp, unsigned int align)
3239 return targetm.asm_out.select_section (exp,
3240 compute_reloc_for_constant (exp),
3241 align);
3244 /* Return the size of constant EXP in bytes. */
3246 static HOST_WIDE_INT
3247 get_constant_size (tree exp)
3249 HOST_WIDE_INT size;
3251 size = int_size_in_bytes (TREE_TYPE (exp));
3252 if (TREE_CODE (exp) == STRING_CST)
3253 size = MAX (TREE_STRING_LENGTH (exp), size);
3254 return size;
3257 /* Subroutine of output_constant_def:
3258 No constant equal to EXP is known to have been output.
3259 Make a constant descriptor to enter EXP in the hash table.
3260 Assign the label number and construct RTL to refer to the
3261 constant's location in memory.
3262 Caller is responsible for updating the hash table. */
3264 static struct constant_descriptor_tree *
3265 build_constant_desc (tree exp)
3267 struct constant_descriptor_tree *desc;
3268 rtx symbol, rtl;
3269 char label[256];
3270 int labelno;
3271 tree decl;
3273 desc = ggc_alloc<constant_descriptor_tree> ();
3274 desc->value = exp;
3276 /* Create a string containing the label name, in LABEL. */
3277 labelno = const_labelno++;
3278 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3280 /* Construct the VAR_DECL associated with the constant. */
3281 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3282 TREE_TYPE (exp));
3283 DECL_ARTIFICIAL (decl) = 1;
3284 DECL_IGNORED_P (decl) = 1;
3285 TREE_READONLY (decl) = 1;
3286 TREE_STATIC (decl) = 1;
3287 TREE_ADDRESSABLE (decl) = 1;
3288 /* We don't set the RTL yet as this would cause varpool to assume that the
3289 variable is referenced. Moreover, it would just be dropped in LTO mode.
3290 Instead we set the flag that will be recognized in make_decl_rtl. */
3291 DECL_IN_CONSTANT_POOL (decl) = 1;
3292 DECL_INITIAL (decl) = desc->value;
3293 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3294 architectures so use DATA_ALIGNMENT as well, except for strings. */
3295 if (TREE_CODE (exp) == STRING_CST)
3297 #ifdef CONSTANT_ALIGNMENT
3298 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3299 #endif
3301 else
3302 align_variable (decl, 0);
3304 /* Now construct the SYMBOL_REF and the MEM. */
3305 if (use_object_blocks_p ())
3307 int align = (TREE_CODE (decl) == CONST_DECL
3308 || (TREE_CODE (decl) == VAR_DECL
3309 && DECL_IN_CONSTANT_POOL (decl))
3310 ? DECL_ALIGN (decl)
3311 : symtab_node::get (decl)->definition_alignment ());
3312 section *sect = get_constant_section (exp, align);
3313 symbol = create_block_symbol (ggc_strdup (label),
3314 get_block_for_section (sect), -1);
3316 else
3317 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3318 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3319 SET_SYMBOL_REF_DECL (symbol, decl);
3320 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3322 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3323 set_mem_attributes (rtl, exp, 1);
3324 set_mem_alias_set (rtl, 0);
3326 /* We cannot share RTX'es in pool entries.
3327 Mark this piece of RTL as required for unsharing. */
3328 RTX_FLAG (rtl, used) = 1;
3330 /* Set flags or add text to the name to record information, such as
3331 that it is a local symbol. If the name is changed, the macro
3332 ASM_OUTPUT_LABELREF will have to know how to strip this
3333 information. This call might invalidate our local variable
3334 SYMBOL; we can't use it afterward. */
3335 targetm.encode_section_info (exp, rtl, true);
3337 desc->rtl = rtl;
3339 return desc;
3342 /* Return an rtx representing a reference to constant data in memory
3343 for the constant expression EXP.
3345 If assembler code for such a constant has already been output,
3346 return an rtx to refer to it.
3347 Otherwise, output such a constant in memory
3348 and generate an rtx for it.
3350 If DEFER is nonzero, this constant can be deferred and output only
3351 if referenced in the function after all optimizations.
3353 `const_desc_table' records which constants already have label strings. */
3356 output_constant_def (tree exp, int defer)
3358 struct constant_descriptor_tree *desc;
3359 struct constant_descriptor_tree key;
3361 /* Look up EXP in the table of constant descriptors. If we didn't find
3362 it, create a new one. */
3363 key.value = exp;
3364 key.hash = const_hash_1 (exp);
3365 constant_descriptor_tree **loc
3366 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3368 desc = *loc;
3369 if (desc == 0)
3371 desc = build_constant_desc (exp);
3372 desc->hash = key.hash;
3373 *loc = desc;
3376 maybe_output_constant_def_contents (desc, defer);
3377 return desc->rtl;
3380 /* Subroutine of output_constant_def: Decide whether or not we need to
3381 output the constant DESC now, and if so, do it. */
3382 static void
3383 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3384 int defer)
3386 rtx symbol = XEXP (desc->rtl, 0);
3387 tree exp = desc->value;
3389 if (flag_syntax_only)
3390 return;
3392 if (TREE_ASM_WRITTEN (exp))
3393 /* Already output; don't do it again. */
3394 return;
3396 /* We can always defer constants as long as the context allows
3397 doing so. */
3398 if (defer)
3400 /* Increment n_deferred_constants if it exists. It needs to be at
3401 least as large as the number of constants actually referred to
3402 by the function. If it's too small we'll stop looking too early
3403 and fail to emit constants; if it's too large we'll only look
3404 through the entire function when we could have stopped earlier. */
3405 if (cfun)
3406 n_deferred_constants++;
3407 return;
3410 output_constant_def_contents (symbol);
3413 /* Subroutine of output_constant_def_contents. Output the definition
3414 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3415 constant's alignment in bits. */
3417 static void
3418 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3420 HOST_WIDE_INT size;
3422 size = get_constant_size (exp);
3424 /* Do any machine/system dependent processing of the constant. */
3425 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3427 /* Output the value of EXP. */
3428 output_constant (exp, size, align);
3430 targetm.asm_out.decl_end ();
3433 /* We must output the constant data referred to by SYMBOL; do so. */
3435 static void
3436 output_constant_def_contents (rtx symbol)
3438 tree decl = SYMBOL_REF_DECL (symbol);
3439 tree exp = DECL_INITIAL (decl);
3440 bool asan_protected = false;
3442 /* Make sure any other constants whose addresses appear in EXP
3443 are assigned label numbers. */
3444 output_addressed_constants (exp);
3446 /* We are no longer deferring this constant. */
3447 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3449 if ((flag_sanitize & SANITIZE_ADDRESS)
3450 && TREE_CODE (exp) == STRING_CST
3451 && asan_protect_global (exp))
3453 asan_protected = true;
3454 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3455 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3458 /* If the constant is part of an object block, make sure that the
3459 decl has been positioned within its block, but do not write out
3460 its definition yet. output_object_blocks will do that later. */
3461 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3462 place_block_symbol (symbol);
3463 else
3465 int align = (TREE_CODE (decl) == CONST_DECL
3466 || (TREE_CODE (decl) == VAR_DECL
3467 && DECL_IN_CONSTANT_POOL (decl))
3468 ? DECL_ALIGN (decl)
3469 : symtab_node::get (decl)->definition_alignment ());
3470 switch_to_section (get_constant_section (exp, align));
3471 if (align > BITS_PER_UNIT)
3472 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3473 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3474 if (asan_protected)
3476 HOST_WIDE_INT size = get_constant_size (exp);
3477 assemble_zeros (asan_red_zone_size (size));
3482 /* Look up EXP in the table of constant descriptors. Return the rtl
3483 if it has been emitted, else null. */
3486 lookup_constant_def (tree exp)
3488 struct constant_descriptor_tree key;
3490 key.value = exp;
3491 key.hash = const_hash_1 (exp);
3492 constant_descriptor_tree *desc
3493 = const_desc_htab->find_with_hash (&key, key.hash);
3495 return (desc ? desc->rtl : NULL_RTX);
3498 /* Return a tree representing a reference to constant data in memory
3499 for the constant expression EXP.
3501 This is the counterpart of output_constant_def at the Tree level. */
3503 tree
3504 tree_output_constant_def (tree exp)
3506 struct constant_descriptor_tree *desc, key;
3507 tree decl;
3509 /* Look up EXP in the table of constant descriptors. If we didn't find
3510 it, create a new one. */
3511 key.value = exp;
3512 key.hash = const_hash_1 (exp);
3513 constant_descriptor_tree **loc
3514 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3516 desc = *loc;
3517 if (desc == 0)
3519 desc = build_constant_desc (exp);
3520 desc->hash = key.hash;
3521 *loc = desc;
3524 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3525 varpool_node::finalize_decl (decl);
3526 return decl;
3529 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3530 struct constant_descriptor_rtx *next;
3531 rtx mem;
3532 rtx sym;
3533 rtx constant;
3534 HOST_WIDE_INT offset;
3535 hashval_t hash;
3536 machine_mode mode;
3537 unsigned int align;
3538 int labelno;
3539 int mark;
3542 struct const_rtx_desc_hasher : ggc_hasher<constant_descriptor_rtx *>
3544 static hashval_t hash (constant_descriptor_rtx *);
3545 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3548 /* Used in the hash tables to avoid outputting the same constant
3549 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3550 are output once per function, not once per file. */
3551 /* ??? Only a few targets need per-function constant pools. Most
3552 can use one per-file pool. Should add a targetm bit to tell the
3553 difference. */
3555 struct GTY(()) rtx_constant_pool {
3556 /* Pointers to first and last constant in pool, as ordered by offset. */
3557 struct constant_descriptor_rtx *first;
3558 struct constant_descriptor_rtx *last;
3560 /* Hash facility for making memory-constants from constant rtl-expressions.
3561 It is used on RISC machines where immediate integer arguments and
3562 constant addresses are restricted so that such constants must be stored
3563 in memory. */
3564 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3566 /* Current offset in constant pool (does not include any
3567 machine-specific header). */
3568 HOST_WIDE_INT offset;
3571 /* Hash and compare functions for const_rtx_htab. */
3573 hashval_t
3574 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3576 return desc->hash;
3579 bool
3580 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3581 constant_descriptor_rtx *y)
3583 if (x->mode != y->mode)
3584 return 0;
3585 return rtx_equal_p (x->constant, y->constant);
3588 /* Hash one component of a constant. */
3590 static hashval_t
3591 const_rtx_hash_1 (const_rtx x)
3593 unsigned HOST_WIDE_INT hwi;
3594 machine_mode mode;
3595 enum rtx_code code;
3596 hashval_t h;
3597 int i;
3599 code = GET_CODE (x);
3600 mode = GET_MODE (x);
3601 h = (hashval_t) code * 1048573 + mode;
3603 switch (code)
3605 case CONST_INT:
3606 hwi = INTVAL (x);
3608 fold_hwi:
3610 int shift = sizeof (hashval_t) * CHAR_BIT;
3611 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3613 h ^= (hashval_t) hwi;
3614 for (i = 1; i < n; ++i)
3616 hwi >>= shift;
3617 h ^= (hashval_t) hwi;
3620 break;
3622 case CONST_WIDE_INT:
3623 hwi = GET_MODE_PRECISION (mode);
3625 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3626 hwi ^= CONST_WIDE_INT_ELT (x, i);
3627 goto fold_hwi;
3630 case CONST_DOUBLE:
3631 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3633 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3634 goto fold_hwi;
3636 else
3637 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3638 break;
3640 case CONST_FIXED:
3641 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3642 break;
3644 case SYMBOL_REF:
3645 h ^= htab_hash_string (XSTR (x, 0));
3646 break;
3648 case LABEL_REF:
3649 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3650 break;
3652 case UNSPEC:
3653 case UNSPEC_VOLATILE:
3654 h = h * 251 + XINT (x, 1);
3655 break;
3657 default:
3658 break;
3661 return h;
3664 /* Compute a hash value for X, which should be a constant. */
3666 static hashval_t
3667 const_rtx_hash (rtx x)
3669 hashval_t h = 0;
3670 subrtx_iterator::array_type array;
3671 FOR_EACH_SUBRTX (iter, array, x, ALL)
3672 h = h * 509 + const_rtx_hash_1 (*iter);
3673 return h;
3677 /* Create and return a new rtx constant pool. */
3679 static struct rtx_constant_pool *
3680 create_constant_pool (void)
3682 struct rtx_constant_pool *pool;
3684 pool = ggc_alloc<rtx_constant_pool> ();
3685 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3686 pool->first = NULL;
3687 pool->last = NULL;
3688 pool->offset = 0;
3689 return pool;
3692 /* Initialize constant pool hashing for a new function. */
3694 void
3695 init_varasm_status (void)
3697 crtl->varasm.pool = create_constant_pool ();
3698 crtl->varasm.deferred_constants = 0;
3701 /* Given a MINUS expression, simplify it if both sides
3702 include the same symbol. */
3705 simplify_subtraction (rtx x)
3707 rtx r = simplify_rtx (x);
3708 return r ? r : x;
3711 /* Given a constant rtx X, make (or find) a memory constant for its value
3712 and return a MEM rtx to refer to it in memory. */
3715 force_const_mem (machine_mode mode, rtx x)
3717 struct constant_descriptor_rtx *desc, tmp;
3718 struct rtx_constant_pool *pool;
3719 char label[256];
3720 rtx def, symbol;
3721 hashval_t hash;
3722 unsigned int align;
3723 constant_descriptor_rtx **slot;
3725 /* If we're not allowed to drop X into the constant pool, don't. */
3726 if (targetm.cannot_force_const_mem (mode, x))
3727 return NULL_RTX;
3729 /* Record that this function has used a constant pool entry. */
3730 crtl->uses_const_pool = 1;
3732 /* Decide which pool to use. */
3733 pool = (targetm.use_blocks_for_constant_p (mode, x)
3734 ? shared_constant_pool
3735 : crtl->varasm.pool);
3737 /* Lookup the value in the hashtable. */
3738 tmp.constant = x;
3739 tmp.mode = mode;
3740 hash = const_rtx_hash (x);
3741 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3742 desc = *slot;
3744 /* If the constant was already present, return its memory. */
3745 if (desc)
3746 return copy_rtx (desc->mem);
3748 /* Otherwise, create a new descriptor. */
3749 desc = ggc_alloc<constant_descriptor_rtx> ();
3750 *slot = desc;
3752 /* Align the location counter as required by EXP's data type. */
3753 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3754 #ifdef CONSTANT_ALIGNMENT
3756 tree type = lang_hooks.types.type_for_mode (mode, 0);
3757 if (type != NULL_TREE)
3758 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3760 #endif
3762 pool->offset += (align / BITS_PER_UNIT) - 1;
3763 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3765 desc->next = NULL;
3766 desc->constant = copy_rtx (tmp.constant);
3767 desc->offset = pool->offset;
3768 desc->hash = hash;
3769 desc->mode = mode;
3770 desc->align = align;
3771 desc->labelno = const_labelno;
3772 desc->mark = 0;
3774 pool->offset += GET_MODE_SIZE (mode);
3775 if (pool->last)
3776 pool->last->next = desc;
3777 else
3778 pool->first = pool->last = desc;
3779 pool->last = desc;
3781 /* Create a string containing the label name, in LABEL. */
3782 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3783 ++const_labelno;
3785 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3786 the constants pool. */
3787 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3789 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3790 symbol = create_block_symbol (ggc_strdup (label),
3791 get_block_for_section (sect), -1);
3793 else
3794 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3795 desc->sym = symbol;
3796 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3797 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3798 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3800 /* Construct the MEM. */
3801 desc->mem = def = gen_const_mem (mode, symbol);
3802 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3803 set_mem_align (def, align);
3805 /* If we're dropping a label to the constant pool, make sure we
3806 don't delete it. */
3807 if (GET_CODE (x) == LABEL_REF)
3808 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3810 return copy_rtx (def);
3813 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3816 get_pool_constant (const_rtx addr)
3818 return SYMBOL_REF_CONSTANT (addr)->constant;
3821 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3822 and whether it has been output or not. */
3825 get_pool_constant_mark (rtx addr, bool *pmarked)
3827 struct constant_descriptor_rtx *desc;
3829 desc = SYMBOL_REF_CONSTANT (addr);
3830 *pmarked = (desc->mark != 0);
3831 return desc->constant;
3834 /* Similar, return the mode. */
3836 machine_mode
3837 get_pool_mode (const_rtx addr)
3839 return SYMBOL_REF_CONSTANT (addr)->mode;
3842 /* Return the size of the constant pool. */
3845 get_pool_size (void)
3847 return crtl->varasm.pool->offset;
3850 /* Worker function for output_constant_pool_1. Emit assembly for X
3851 in MODE with known alignment ALIGN. */
3853 static void
3854 output_constant_pool_2 (machine_mode mode, rtx x, unsigned int align)
3856 switch (GET_MODE_CLASS (mode))
3858 case MODE_FLOAT:
3859 case MODE_DECIMAL_FLOAT:
3861 REAL_VALUE_TYPE r;
3863 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3864 REAL_VALUE_FROM_CONST_DOUBLE (r, x);
3865 assemble_real (r, mode, align);
3866 break;
3869 case MODE_INT:
3870 case MODE_PARTIAL_INT:
3871 case MODE_FRACT:
3872 case MODE_UFRACT:
3873 case MODE_ACCUM:
3874 case MODE_UACCUM:
3875 case MODE_POINTER_BOUNDS:
3876 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3877 break;
3879 case MODE_VECTOR_FLOAT:
3880 case MODE_VECTOR_INT:
3881 case MODE_VECTOR_FRACT:
3882 case MODE_VECTOR_UFRACT:
3883 case MODE_VECTOR_ACCUM:
3884 case MODE_VECTOR_UACCUM:
3886 int i, units;
3887 machine_mode submode = GET_MODE_INNER (mode);
3888 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3890 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3891 units = CONST_VECTOR_NUNITS (x);
3893 for (i = 0; i < units; i++)
3895 rtx elt = CONST_VECTOR_ELT (x, i);
3896 output_constant_pool_2 (submode, elt, i ? subalign : align);
3899 break;
3901 default:
3902 gcc_unreachable ();
3906 /* Worker function for output_constant_pool. Emit constant DESC,
3907 giving it ALIGN bits of alignment. */
3909 static void
3910 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3911 unsigned int align)
3913 rtx x, tmp;
3915 x = desc->constant;
3917 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3918 whose CODE_LABEL has been deleted. This can occur if a jump table
3919 is eliminated by optimization. If so, write a constant of zero
3920 instead. Note that this can also happen by turning the
3921 CODE_LABEL into a NOTE. */
3922 /* ??? This seems completely and utterly wrong. Certainly it's
3923 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3924 functioning even with rtx_insn::deleted and friends. */
3926 tmp = x;
3927 switch (GET_CODE (tmp))
3929 case CONST:
3930 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3931 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3932 break;
3933 tmp = XEXP (XEXP (tmp, 0), 0);
3934 /* FALLTHRU */
3936 case LABEL_REF:
3937 tmp = LABEL_REF_LABEL (tmp);
3938 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3939 gcc_assert (!NOTE_P (tmp)
3940 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3941 break;
3943 default:
3944 break;
3947 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3948 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3949 align, desc->labelno, done);
3950 #endif
3952 assemble_align (align);
3954 /* Output the label. */
3955 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3957 /* Output the data.
3958 Pass actual alignment value while emitting string constant to asm code
3959 as function 'output_constant_pool_1' explicitly passes the alignment as 1
3960 assuming that the data is already aligned which prevents the generation
3961 of fix-up table entries. */
3962 output_constant_pool_2 (desc->mode, x, desc->align);
3964 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3965 sections have proper size. */
3966 if (align > GET_MODE_BITSIZE (desc->mode)
3967 && in_section
3968 && (in_section->common.flags & SECTION_MERGE))
3969 assemble_align (align);
3971 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3972 done:
3973 #endif
3974 return;
3977 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3978 Emit referenced deferred strings. */
3980 static void
3981 mark_constants_in_pattern (rtx insn)
3983 subrtx_iterator::array_type array;
3984 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3986 const_rtx x = *iter;
3987 if (GET_CODE (x) == SYMBOL_REF)
3989 if (CONSTANT_POOL_ADDRESS_P (x))
3991 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3992 if (desc->mark == 0)
3994 desc->mark = 1;
3995 iter.substitute (desc->constant);
3998 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4000 tree decl = SYMBOL_REF_DECL (x);
4001 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4003 n_deferred_constants--;
4004 output_constant_def_contents (CONST_CAST_RTX (x));
4011 /* Look through appropriate parts of INSN, marking all entries in the
4012 constant pool which are actually being used. Entries that are only
4013 referenced by other constants are also marked as used. Emit
4014 deferred strings that are used. */
4016 static void
4017 mark_constants (rtx_insn *insn)
4019 if (!INSN_P (insn))
4020 return;
4022 /* Insns may appear inside a SEQUENCE. Only check the patterns of
4023 insns, not any notes that may be attached. We don't want to mark
4024 a constant just because it happens to appear in a REG_EQUIV note. */
4025 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4027 int i, n = seq->len ();
4028 for (i = 0; i < n; ++i)
4030 rtx subinsn = seq->element (i);
4031 if (INSN_P (subinsn))
4032 mark_constants_in_pattern (subinsn);
4035 else
4036 mark_constants_in_pattern (insn);
4039 /* Look through the instructions for this function, and mark all the
4040 entries in POOL which are actually being used. Emit deferred constants
4041 which have indeed been used. */
4043 static void
4044 mark_constant_pool (void)
4046 rtx_insn *insn;
4048 if (!crtl->uses_const_pool && n_deferred_constants == 0)
4049 return;
4051 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4052 mark_constants (insn);
4055 /* Write all the constants in POOL. */
4057 static void
4058 output_constant_pool_contents (struct rtx_constant_pool *pool)
4060 struct constant_descriptor_rtx *desc;
4062 for (desc = pool->first; desc ; desc = desc->next)
4063 if (desc->mark)
4065 /* If the constant is part of an object_block, make sure that
4066 the constant has been positioned within its block, but do not
4067 write out its definition yet. output_object_blocks will do
4068 that later. */
4069 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4070 && SYMBOL_REF_BLOCK (desc->sym))
4071 place_block_symbol (desc->sym);
4072 else
4074 switch_to_section (targetm.asm_out.select_rtx_section
4075 (desc->mode, desc->constant, desc->align));
4076 output_constant_pool_1 (desc, desc->align);
4081 /* Mark all constants that are used in the current function, then write
4082 out the function's private constant pool. */
4084 static void
4085 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4086 tree fndecl ATTRIBUTE_UNUSED)
4088 struct rtx_constant_pool *pool = crtl->varasm.pool;
4090 /* It is possible for gcc to call force_const_mem and then to later
4091 discard the instructions which refer to the constant. In such a
4092 case we do not need to output the constant. */
4093 mark_constant_pool ();
4095 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4096 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4097 #endif
4099 output_constant_pool_contents (pool);
4101 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4102 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4103 #endif
4106 /* Write the contents of the shared constant pool. */
4108 void
4109 output_shared_constant_pool (void)
4111 output_constant_pool_contents (shared_constant_pool);
4114 /* Determine what kind of relocations EXP may need. */
4117 compute_reloc_for_constant (tree exp)
4119 int reloc = 0, reloc2;
4120 tree tem;
4122 switch (TREE_CODE (exp))
4124 case ADDR_EXPR:
4125 case FDESC_EXPR:
4126 /* Go inside any operations that get_inner_reference can handle and see
4127 if what's inside is a constant: no need to do anything here for
4128 addresses of variables or functions. */
4129 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4130 tem = TREE_OPERAND (tem, 0))
4133 if (TREE_CODE (tem) == MEM_REF
4134 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4136 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4137 break;
4140 if (!targetm.binds_local_p (tem))
4141 reloc |= 2;
4142 else
4143 reloc |= 1;
4144 break;
4146 case PLUS_EXPR:
4147 case POINTER_PLUS_EXPR:
4148 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4149 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4150 break;
4152 case MINUS_EXPR:
4153 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4154 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4155 /* The difference of two local labels is computable at link time. */
4156 if (reloc == 1 && reloc2 == 1)
4157 reloc = 0;
4158 else
4159 reloc |= reloc2;
4160 break;
4162 CASE_CONVERT:
4163 case VIEW_CONVERT_EXPR:
4164 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4165 break;
4167 case CONSTRUCTOR:
4169 unsigned HOST_WIDE_INT idx;
4170 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4171 if (tem != 0)
4172 reloc |= compute_reloc_for_constant (tem);
4174 break;
4176 default:
4177 break;
4179 return reloc;
4182 /* Find all the constants whose addresses are referenced inside of EXP,
4183 and make sure assembler code with a label has been output for each one.
4184 Indicate whether an ADDR_EXPR has been encountered. */
4186 static void
4187 output_addressed_constants (tree exp)
4189 tree tem;
4191 switch (TREE_CODE (exp))
4193 case ADDR_EXPR:
4194 case FDESC_EXPR:
4195 /* Go inside any operations that get_inner_reference can handle and see
4196 if what's inside is a constant: no need to do anything here for
4197 addresses of variables or functions. */
4198 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4199 tem = TREE_OPERAND (tem, 0))
4202 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4203 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4204 tem = DECL_INITIAL (tem);
4206 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4207 output_constant_def (tem, 0);
4209 if (TREE_CODE (tem) == MEM_REF)
4210 output_addressed_constants (TREE_OPERAND (tem, 0));
4211 break;
4213 case PLUS_EXPR:
4214 case POINTER_PLUS_EXPR:
4215 case MINUS_EXPR:
4216 output_addressed_constants (TREE_OPERAND (exp, 1));
4217 /* Fall through. */
4219 CASE_CONVERT:
4220 case VIEW_CONVERT_EXPR:
4221 output_addressed_constants (TREE_OPERAND (exp, 0));
4222 break;
4224 case CONSTRUCTOR:
4226 unsigned HOST_WIDE_INT idx;
4227 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4228 if (tem != 0)
4229 output_addressed_constants (tem);
4231 break;
4233 default:
4234 break;
4238 /* Whether a constructor CTOR is a valid static constant initializer if all
4239 its elements are. This used to be internal to initializer_constant_valid_p
4240 and has been exposed to let other functions like categorize_ctor_elements
4241 evaluate the property while walking a constructor for other purposes. */
4243 bool
4244 constructor_static_from_elts_p (const_tree ctor)
4246 return (TREE_CONSTANT (ctor)
4247 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4248 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4249 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4252 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4253 tree *cache);
4255 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4256 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4257 which are valid when ENDTYPE is an integer of any size; in
4258 particular, this does not accept a pointer minus a constant. This
4259 returns null_pointer_node if the VALUE is an absolute constant
4260 which can be used to initialize a static variable. Otherwise it
4261 returns NULL. */
4263 static tree
4264 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4266 tree op0, op1;
4268 if (!INTEGRAL_TYPE_P (endtype))
4269 return NULL_TREE;
4271 op0 = TREE_OPERAND (value, 0);
4272 op1 = TREE_OPERAND (value, 1);
4274 /* Like STRIP_NOPS except allow the operand mode to widen. This
4275 works around a feature of fold that simplifies (int)(p1 - p2) to
4276 ((int)p1 - (int)p2) under the theory that the narrower operation
4277 is cheaper. */
4279 while (CONVERT_EXPR_P (op0)
4280 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4282 tree inner = TREE_OPERAND (op0, 0);
4283 if (inner == error_mark_node
4284 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4285 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4286 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4287 break;
4288 op0 = inner;
4291 while (CONVERT_EXPR_P (op1)
4292 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4294 tree inner = TREE_OPERAND (op1, 0);
4295 if (inner == error_mark_node
4296 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4297 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4298 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4299 break;
4300 op1 = inner;
4303 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4304 if (!op0)
4305 return NULL_TREE;
4307 op1 = initializer_constant_valid_p_1 (op1, endtype,
4308 cache ? cache + 2 : NULL);
4309 /* Both initializers must be known. */
4310 if (op1)
4312 if (op0 == op1
4313 && (op0 == null_pointer_node
4314 || TREE_CODE (value) == MINUS_EXPR))
4315 return null_pointer_node;
4317 /* Support differences between labels. */
4318 if (TREE_CODE (op0) == LABEL_DECL
4319 && TREE_CODE (op1) == LABEL_DECL)
4320 return null_pointer_node;
4322 if (TREE_CODE (op0) == STRING_CST
4323 && TREE_CODE (op1) == STRING_CST
4324 && operand_equal_p (op0, op1, 1))
4325 return null_pointer_node;
4328 return NULL_TREE;
4331 /* Helper function of initializer_constant_valid_p.
4332 Return nonzero if VALUE is a valid constant-valued expression
4333 for use in initializing a static variable; one that can be an
4334 element of a "constant" initializer.
4336 Return null_pointer_node if the value is absolute;
4337 if it is relocatable, return the variable that determines the relocation.
4338 We assume that VALUE has been folded as much as possible;
4339 therefore, we do not need to check for such things as
4340 arithmetic-combinations of integers.
4342 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4344 static tree
4345 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4347 tree ret;
4349 switch (TREE_CODE (value))
4351 case CONSTRUCTOR:
4352 if (constructor_static_from_elts_p (value))
4354 unsigned HOST_WIDE_INT idx;
4355 tree elt;
4356 bool absolute = true;
4358 if (cache && cache[0] == value)
4359 return cache[1];
4360 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4362 tree reloc;
4363 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4364 NULL);
4365 if (!reloc)
4367 if (cache)
4369 cache[0] = value;
4370 cache[1] = NULL_TREE;
4372 return NULL_TREE;
4374 if (reloc != null_pointer_node)
4375 absolute = false;
4377 /* For a non-absolute relocation, there is no single
4378 variable that can be "the variable that determines the
4379 relocation." */
4380 if (cache)
4382 cache[0] = value;
4383 cache[1] = absolute ? null_pointer_node : error_mark_node;
4385 return absolute ? null_pointer_node : error_mark_node;
4388 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4390 case INTEGER_CST:
4391 case VECTOR_CST:
4392 case REAL_CST:
4393 case FIXED_CST:
4394 case STRING_CST:
4395 case COMPLEX_CST:
4396 return null_pointer_node;
4398 case ADDR_EXPR:
4399 case FDESC_EXPR:
4401 tree op0 = staticp (TREE_OPERAND (value, 0));
4402 if (op0)
4404 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4405 to be a constant, this is old-skool offsetof-like nonsense. */
4406 if (TREE_CODE (op0) == INDIRECT_REF
4407 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4408 return null_pointer_node;
4409 /* Taking the address of a nested function involves a trampoline,
4410 unless we don't need or want one. */
4411 if (TREE_CODE (op0) == FUNCTION_DECL
4412 && DECL_STATIC_CHAIN (op0)
4413 && !TREE_NO_TRAMPOLINE (value))
4414 return NULL_TREE;
4415 /* "&{...}" requires a temporary to hold the constructed
4416 object. */
4417 if (TREE_CODE (op0) == CONSTRUCTOR)
4418 return NULL_TREE;
4420 return op0;
4423 case NON_LVALUE_EXPR:
4424 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4425 endtype, cache);
4427 case VIEW_CONVERT_EXPR:
4429 tree src = TREE_OPERAND (value, 0);
4430 tree src_type = TREE_TYPE (src);
4431 tree dest_type = TREE_TYPE (value);
4433 /* Allow view-conversions from aggregate to non-aggregate type only
4434 if the bit pattern is fully preserved afterwards; otherwise, the
4435 RTL expander won't be able to apply a subsequent transformation
4436 to the underlying constructor. */
4437 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4439 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4440 return initializer_constant_valid_p_1 (src, endtype, cache);
4441 else
4442 return NULL_TREE;
4445 /* Allow all other kinds of view-conversion. */
4446 return initializer_constant_valid_p_1 (src, endtype, cache);
4449 CASE_CONVERT:
4451 tree src = TREE_OPERAND (value, 0);
4452 tree src_type = TREE_TYPE (src);
4453 tree dest_type = TREE_TYPE (value);
4455 /* Allow conversions between pointer types, floating-point
4456 types, and offset types. */
4457 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4458 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4459 || (TREE_CODE (dest_type) == OFFSET_TYPE
4460 && TREE_CODE (src_type) == OFFSET_TYPE))
4461 return initializer_constant_valid_p_1 (src, endtype, cache);
4463 /* Allow length-preserving conversions between integer types. */
4464 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4465 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4466 return initializer_constant_valid_p_1 (src, endtype, cache);
4468 /* Allow conversions between other integer types only if
4469 explicit value. */
4470 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4472 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4473 if (inner == null_pointer_node)
4474 return null_pointer_node;
4475 break;
4478 /* Allow (int) &foo provided int is as wide as a pointer. */
4479 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4480 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4481 return initializer_constant_valid_p_1 (src, endtype, cache);
4483 /* Likewise conversions from int to pointers, but also allow
4484 conversions from 0. */
4485 if ((POINTER_TYPE_P (dest_type)
4486 || TREE_CODE (dest_type) == OFFSET_TYPE)
4487 && INTEGRAL_TYPE_P (src_type))
4489 if (TREE_CODE (src) == INTEGER_CST
4490 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4491 return null_pointer_node;
4492 if (integer_zerop (src))
4493 return null_pointer_node;
4494 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4495 return initializer_constant_valid_p_1 (src, endtype, cache);
4498 /* Allow conversions to struct or union types if the value
4499 inside is okay. */
4500 if (TREE_CODE (dest_type) == RECORD_TYPE
4501 || TREE_CODE (dest_type) == UNION_TYPE)
4502 return initializer_constant_valid_p_1 (src, endtype, cache);
4504 break;
4506 case POINTER_PLUS_EXPR:
4507 case PLUS_EXPR:
4508 /* Any valid floating-point constants will have been folded by now;
4509 with -frounding-math we hit this with addition of two constants. */
4510 if (TREE_CODE (endtype) == REAL_TYPE)
4511 return NULL_TREE;
4512 if (cache && cache[0] == value)
4513 return cache[1];
4514 if (! INTEGRAL_TYPE_P (endtype)
4515 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4517 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4518 tree valid0
4519 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4520 endtype, ncache);
4521 tree valid1
4522 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4523 endtype, ncache + 2);
4524 /* If either term is absolute, use the other term's relocation. */
4525 if (valid0 == null_pointer_node)
4526 ret = valid1;
4527 else if (valid1 == null_pointer_node)
4528 ret = valid0;
4529 /* Support narrowing pointer differences. */
4530 else
4531 ret = narrowing_initializer_constant_valid_p (value, endtype,
4532 ncache);
4534 else
4535 /* Support narrowing pointer differences. */
4536 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4537 if (cache)
4539 cache[0] = value;
4540 cache[1] = ret;
4542 return ret;
4544 case MINUS_EXPR:
4545 if (TREE_CODE (endtype) == REAL_TYPE)
4546 return NULL_TREE;
4547 if (cache && cache[0] == value)
4548 return cache[1];
4549 if (! INTEGRAL_TYPE_P (endtype)
4550 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4552 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4553 tree valid0
4554 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4555 endtype, ncache);
4556 tree valid1
4557 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4558 endtype, ncache + 2);
4559 /* Win if second argument is absolute. */
4560 if (valid1 == null_pointer_node)
4561 ret = valid0;
4562 /* Win if both arguments have the same relocation.
4563 Then the value is absolute. */
4564 else if (valid0 == valid1 && valid0 != 0)
4565 ret = null_pointer_node;
4566 /* Since GCC guarantees that string constants are unique in the
4567 generated code, a subtraction between two copies of the same
4568 constant string is absolute. */
4569 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4570 && valid1 && TREE_CODE (valid1) == STRING_CST
4571 && operand_equal_p (valid0, valid1, 1))
4572 ret = null_pointer_node;
4573 /* Support narrowing differences. */
4574 else
4575 ret = narrowing_initializer_constant_valid_p (value, endtype,
4576 ncache);
4578 else
4579 /* Support narrowing differences. */
4580 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4581 if (cache)
4583 cache[0] = value;
4584 cache[1] = ret;
4586 return ret;
4588 default:
4589 break;
4592 return NULL_TREE;
4595 /* Return nonzero if VALUE is a valid constant-valued expression
4596 for use in initializing a static variable; one that can be an
4597 element of a "constant" initializer.
4599 Return null_pointer_node if the value is absolute;
4600 if it is relocatable, return the variable that determines the relocation.
4601 We assume that VALUE has been folded as much as possible;
4602 therefore, we do not need to check for such things as
4603 arithmetic-combinations of integers. */
4604 tree
4605 initializer_constant_valid_p (tree value, tree endtype)
4607 return initializer_constant_valid_p_1 (value, endtype, NULL);
4610 /* Return true if VALUE is a valid constant-valued expression
4611 for use in initializing a static bit-field; one that can be
4612 an element of a "constant" initializer. */
4614 bool
4615 initializer_constant_valid_for_bitfield_p (tree value)
4617 /* For bitfields we support integer constants or possibly nested aggregates
4618 of such. */
4619 switch (TREE_CODE (value))
4621 case CONSTRUCTOR:
4623 unsigned HOST_WIDE_INT idx;
4624 tree elt;
4626 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4627 if (!initializer_constant_valid_for_bitfield_p (elt))
4628 return false;
4629 return true;
4632 case INTEGER_CST:
4633 case REAL_CST:
4634 return true;
4636 case VIEW_CONVERT_EXPR:
4637 case NON_LVALUE_EXPR:
4638 return
4639 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4641 default:
4642 break;
4645 return false;
4648 /* output_constructor outer state of relevance in recursive calls, typically
4649 for nested aggregate bitfields. */
4651 typedef struct {
4652 unsigned int bit_offset; /* current position in ... */
4653 int byte; /* ... the outer byte buffer. */
4654 } oc_outer_state;
4656 static unsigned HOST_WIDE_INT
4657 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4658 oc_outer_state *);
4660 /* Output assembler code for constant EXP, with no label.
4661 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4662 Assumes output_addressed_constants has been done on EXP already.
4664 Generate at least SIZE bytes of assembler data, padding at the end
4665 with zeros if necessary. SIZE must always be specified. The returned
4666 value is the actual number of bytes of assembler data generated, which
4667 may be bigger than SIZE if the object contains a variable length field.
4669 SIZE is important for structure constructors,
4670 since trailing members may have been omitted from the constructor.
4671 It is also important for initialization of arrays from string constants
4672 since the full length of the string constant might not be wanted.
4673 It is also needed for initialization of unions, where the initializer's
4674 type is just one member, and that may not be as long as the union.
4676 There a case in which we would fail to output exactly SIZE bytes:
4677 for a structure constructor that wants to produce more than SIZE bytes.
4678 But such constructors will never be generated for any possible input.
4680 ALIGN is the alignment of the data in bits. */
4682 static unsigned HOST_WIDE_INT
4683 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4685 enum tree_code code;
4686 unsigned HOST_WIDE_INT thissize;
4688 if (size == 0 || flag_syntax_only)
4689 return size;
4691 /* See if we're trying to initialize a pointer in a non-default mode
4692 to the address of some declaration somewhere. If the target says
4693 the mode is valid for pointers, assume the target has a way of
4694 resolving it. */
4695 if (TREE_CODE (exp) == NOP_EXPR
4696 && POINTER_TYPE_P (TREE_TYPE (exp))
4697 && targetm.addr_space.valid_pointer_mode
4698 (TYPE_MODE (TREE_TYPE (exp)),
4699 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4701 tree saved_type = TREE_TYPE (exp);
4703 /* Peel off any intermediate conversions-to-pointer for valid
4704 pointer modes. */
4705 while (TREE_CODE (exp) == NOP_EXPR
4706 && POINTER_TYPE_P (TREE_TYPE (exp))
4707 && targetm.addr_space.valid_pointer_mode
4708 (TYPE_MODE (TREE_TYPE (exp)),
4709 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4710 exp = TREE_OPERAND (exp, 0);
4712 /* If what we're left with is the address of something, we can
4713 convert the address to the final type and output it that
4714 way. */
4715 if (TREE_CODE (exp) == ADDR_EXPR)
4716 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4717 /* Likewise for constant ints. */
4718 else if (TREE_CODE (exp) == INTEGER_CST)
4719 exp = wide_int_to_tree (saved_type, exp);
4723 /* Eliminate any conversions since we'll be outputting the underlying
4724 constant. */
4725 while (CONVERT_EXPR_P (exp)
4726 || TREE_CODE (exp) == NON_LVALUE_EXPR
4727 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4729 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4730 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4732 /* Make sure eliminating the conversion is really a no-op, except with
4733 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4734 union types to allow for Ada unchecked unions. */
4735 if (type_size > op_size
4736 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4737 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4738 /* Keep the conversion. */
4739 break;
4740 else
4741 exp = TREE_OPERAND (exp, 0);
4744 code = TREE_CODE (TREE_TYPE (exp));
4745 thissize = int_size_in_bytes (TREE_TYPE (exp));
4747 /* Allow a constructor with no elements for any data type.
4748 This means to fill the space with zeros. */
4749 if (TREE_CODE (exp) == CONSTRUCTOR
4750 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4752 assemble_zeros (size);
4753 return size;
4756 if (TREE_CODE (exp) == FDESC_EXPR)
4758 #ifdef ASM_OUTPUT_FDESC
4759 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4760 tree decl = TREE_OPERAND (exp, 0);
4761 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4762 #else
4763 gcc_unreachable ();
4764 #endif
4765 return size;
4768 /* Now output the underlying data. If we've handling the padding, return.
4769 Otherwise, break and ensure SIZE is the size written. */
4770 switch (code)
4772 case BOOLEAN_TYPE:
4773 case INTEGER_TYPE:
4774 case ENUMERAL_TYPE:
4775 case POINTER_TYPE:
4776 case REFERENCE_TYPE:
4777 case OFFSET_TYPE:
4778 case FIXED_POINT_TYPE:
4779 case POINTER_BOUNDS_TYPE:
4780 case NULLPTR_TYPE:
4781 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4782 EXPAND_INITIALIZER),
4783 MIN (size, thissize), align, 0))
4784 error ("initializer for integer/fixed-point value is too complicated");
4785 break;
4787 case REAL_TYPE:
4788 if (TREE_CODE (exp) != REAL_CST)
4789 error ("initializer for floating value is not a floating constant");
4790 else
4791 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4792 break;
4794 case COMPLEX_TYPE:
4795 output_constant (TREE_REALPART (exp), thissize / 2, align);
4796 output_constant (TREE_IMAGPART (exp), thissize / 2,
4797 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4798 break;
4800 case ARRAY_TYPE:
4801 case VECTOR_TYPE:
4802 switch (TREE_CODE (exp))
4804 case CONSTRUCTOR:
4805 return output_constructor (exp, size, align, NULL);
4806 case STRING_CST:
4807 thissize
4808 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4809 assemble_string (TREE_STRING_POINTER (exp), thissize);
4810 break;
4811 case VECTOR_CST:
4813 machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4814 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4815 int elt_size = GET_MODE_SIZE (inner);
4816 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4817 thissize = elt_size;
4818 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4820 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4821 thissize += elt_size;
4823 break;
4825 default:
4826 gcc_unreachable ();
4828 break;
4830 case RECORD_TYPE:
4831 case UNION_TYPE:
4832 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4833 return output_constructor (exp, size, align, NULL);
4835 case ERROR_MARK:
4836 return 0;
4838 default:
4839 gcc_unreachable ();
4842 if (size > thissize)
4843 assemble_zeros (size - thissize);
4845 return size;
4849 /* Subroutine of output_constructor, used for computing the size of
4850 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4851 type with an unspecified upper bound. */
4853 static unsigned HOST_WIDE_INT
4854 array_size_for_constructor (tree val)
4856 tree max_index;
4857 unsigned HOST_WIDE_INT cnt;
4858 tree index, value, tmp;
4859 offset_int i;
4861 /* This code used to attempt to handle string constants that are not
4862 arrays of single-bytes, but nothing else does, so there's no point in
4863 doing it here. */
4864 if (TREE_CODE (val) == STRING_CST)
4865 return TREE_STRING_LENGTH (val);
4867 max_index = NULL_TREE;
4868 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4870 if (TREE_CODE (index) == RANGE_EXPR)
4871 index = TREE_OPERAND (index, 1);
4872 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4873 max_index = index;
4876 if (max_index == NULL_TREE)
4877 return 0;
4879 /* Compute the total number of array elements. */
4880 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4881 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4883 /* Multiply by the array element unit size to find number of bytes. */
4884 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4886 gcc_assert (wi::fits_uhwi_p (i));
4887 return i.to_uhwi ();
4890 /* Other datastructures + helpers for output_constructor. */
4892 /* output_constructor local state to support interaction with helpers. */
4894 typedef struct {
4896 /* Received arguments. */
4897 tree exp; /* Constructor expression. */
4898 tree type; /* Type of constructor expression. */
4899 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4900 unsigned int align; /* Known initial alignment. */
4901 tree min_index; /* Lower bound if specified for an array. */
4903 /* Output processing state. */
4904 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4905 int byte; /* Part of a bitfield byte yet to be output. */
4906 int last_relative_index; /* Implicit or explicit index of the last
4907 array element output within a bitfield. */
4908 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4910 /* Current element. */
4911 tree field; /* Current field decl in a record. */
4912 tree val; /* Current element value. */
4913 tree index; /* Current element index. */
4915 } oc_local_state;
4917 /* Helper for output_constructor. From the current LOCAL state, output a
4918 RANGE_EXPR element. */
4920 static void
4921 output_constructor_array_range (oc_local_state *local)
4923 unsigned HOST_WIDE_INT fieldsize
4924 = int_size_in_bytes (TREE_TYPE (local->type));
4926 HOST_WIDE_INT lo_index
4927 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4928 HOST_WIDE_INT hi_index
4929 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4930 HOST_WIDE_INT index;
4932 unsigned int align2
4933 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4935 for (index = lo_index; index <= hi_index; index++)
4937 /* Output the element's initial value. */
4938 if (local->val == NULL_TREE)
4939 assemble_zeros (fieldsize);
4940 else
4941 fieldsize = output_constant (local->val, fieldsize, align2);
4943 /* Count its size. */
4944 local->total_bytes += fieldsize;
4948 /* Helper for output_constructor. From the current LOCAL state, output a
4949 field element that is not true bitfield or part of an outer one. */
4951 static void
4952 output_constructor_regular_field (oc_local_state *local)
4954 /* Field size and position. Since this structure is static, we know the
4955 positions are constant. */
4956 unsigned HOST_WIDE_INT fieldsize;
4957 HOST_WIDE_INT fieldpos;
4959 unsigned int align2;
4961 if (local->index != NULL_TREE)
4963 /* Perform the index calculation in modulo arithmetic but
4964 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4965 but we are using an unsigned sizetype. */
4966 unsigned prec = TYPE_PRECISION (sizetype);
4967 offset_int idx = wi::sext (wi::to_offset (local->index)
4968 - wi::to_offset (local->min_index), prec);
4969 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4970 .to_short_addr ();
4972 else if (local->field != NULL_TREE)
4973 fieldpos = int_byte_position (local->field);
4974 else
4975 fieldpos = 0;
4977 /* Output any buffered-up bit-fields preceding this element. */
4978 if (local->byte_buffer_in_use)
4980 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4981 local->total_bytes++;
4982 local->byte_buffer_in_use = false;
4985 /* Advance to offset of this element.
4986 Note no alignment needed in an array, since that is guaranteed
4987 if each element has the proper size. */
4988 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4989 && fieldpos > local->total_bytes)
4991 assemble_zeros (fieldpos - local->total_bytes);
4992 local->total_bytes = fieldpos;
4995 /* Find the alignment of this element. */
4996 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4998 /* Determine size this element should occupy. */
4999 if (local->field)
5001 fieldsize = 0;
5003 /* If this is an array with an unspecified upper bound,
5004 the initializer determines the size. */
5005 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5006 but we cannot do this until the deprecated support for
5007 initializing zero-length array members is removed. */
5008 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5009 && TYPE_DOMAIN (TREE_TYPE (local->field))
5010 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
5012 fieldsize = array_size_for_constructor (local->val);
5013 /* Given a non-empty initialization, this field had
5014 better be last. */
5015 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
5017 else
5018 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5020 else
5021 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5023 /* Output the element's initial value. */
5024 if (local->val == NULL_TREE)
5025 assemble_zeros (fieldsize);
5026 else
5027 fieldsize = output_constant (local->val, fieldsize, align2);
5029 /* Count its size. */
5030 local->total_bytes += fieldsize;
5033 /* Helper for output_constructor. From the LOCAL state, output an element
5034 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
5035 from the start of a possibly ongoing outer byte buffer. */
5037 static void
5038 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5040 /* Bit size of this element. */
5041 HOST_WIDE_INT ebitsize
5042 = (local->field
5043 ? tree_to_uhwi (DECL_SIZE (local->field))
5044 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5046 /* Relative index of this element if this is an array component. */
5047 HOST_WIDE_INT relative_index
5048 = (!local->field
5049 ? (local->index
5050 ? (tree_to_shwi (local->index)
5051 - tree_to_shwi (local->min_index))
5052 : local->last_relative_index + 1)
5053 : 0);
5055 /* Bit position of this element from the start of the containing
5056 constructor. */
5057 HOST_WIDE_INT constructor_relative_ebitpos
5058 = (local->field
5059 ? int_bit_position (local->field)
5060 : ebitsize * relative_index);
5062 /* Bit position of this element from the start of a possibly ongoing
5063 outer byte buffer. */
5064 HOST_WIDE_INT byte_relative_ebitpos
5065 = bit_offset + constructor_relative_ebitpos;
5067 /* From the start of a possibly ongoing outer byte buffer, offsets to
5068 the first bit of this element and to the first bit past the end of
5069 this element. */
5070 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5071 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5073 local->last_relative_index = relative_index;
5075 if (local->val == NULL_TREE)
5076 local->val = integer_zero_node;
5078 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5079 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5080 local->val = TREE_OPERAND (local->val, 0);
5082 if (TREE_CODE (local->val) != INTEGER_CST
5083 && TREE_CODE (local->val) != CONSTRUCTOR)
5085 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5086 return;
5089 /* If this field does not start in this (or next) byte, skip some bytes. */
5090 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5092 /* Output remnant of any bit field in previous bytes. */
5093 if (local->byte_buffer_in_use)
5095 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5096 local->total_bytes++;
5097 local->byte_buffer_in_use = false;
5100 /* If still not at proper byte, advance to there. */
5101 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5103 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5104 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5105 local->total_bytes = next_offset / BITS_PER_UNIT;
5109 /* Set up the buffer if necessary. */
5110 if (!local->byte_buffer_in_use)
5112 local->byte = 0;
5113 if (ebitsize > 0)
5114 local->byte_buffer_in_use = true;
5117 /* If this is nested constructor, recurse passing the bit offset and the
5118 pending data, then retrieve the new pending data afterwards. */
5119 if (TREE_CODE (local->val) == CONSTRUCTOR)
5121 oc_outer_state temp_state;
5122 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5123 temp_state.byte = local->byte;
5124 local->total_bytes
5125 += output_constructor (local->val, 0, 0, &temp_state);
5126 local->byte = temp_state.byte;
5127 return;
5130 /* Otherwise, we must split the element into pieces that fall within
5131 separate bytes, and combine each byte with previous or following
5132 bit-fields. */
5133 while (next_offset < end_offset)
5135 int this_time;
5136 int shift;
5137 HOST_WIDE_INT value;
5138 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5139 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5141 /* Advance from byte to byte within this element when necessary. */
5142 while (next_byte != local->total_bytes)
5144 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5145 local->total_bytes++;
5146 local->byte = 0;
5149 /* Number of bits we can process at once (all part of the same byte). */
5150 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5151 if (BYTES_BIG_ENDIAN)
5153 /* On big-endian machine, take the most significant bits (of the
5154 bits that are significant) first and put them into bytes from
5155 the most significant end. */
5156 shift = end_offset - next_offset - this_time;
5158 /* Don't try to take a bunch of bits that cross
5159 the word boundary in the INTEGER_CST. We can
5160 only select bits from one element. */
5161 if ((shift / HOST_BITS_PER_WIDE_INT)
5162 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5164 const int end = shift + this_time - 1;
5165 shift = end & -HOST_BITS_PER_WIDE_INT;
5166 this_time = end - shift + 1;
5169 /* Now get the bits from the appropriate constant word. */
5170 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5171 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5173 /* Get the result. This works only when:
5174 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5175 local->byte |= (((value >> shift)
5176 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5177 << (BITS_PER_UNIT - this_time - next_bit));
5179 else
5181 /* On little-endian machines, take the least significant bits of
5182 the value first and pack them starting at the least significant
5183 bits of the bytes. */
5184 shift = next_offset - byte_relative_ebitpos;
5186 /* Don't try to take a bunch of bits that cross
5187 the word boundary in the INTEGER_CST. We can
5188 only select bits from one element. */
5189 if ((shift / HOST_BITS_PER_WIDE_INT)
5190 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5191 this_time
5192 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5194 /* Now get the bits from the appropriate constant word. */
5195 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5196 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5198 /* Get the result. This works only when:
5199 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5200 local->byte |= (((value >> shift)
5201 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5202 << next_bit);
5205 next_offset += this_time;
5206 local->byte_buffer_in_use = true;
5210 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5211 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5212 caller output state of relevance in recursive invocations. */
5214 static unsigned HOST_WIDE_INT
5215 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5216 unsigned int align, oc_outer_state *outer)
5218 unsigned HOST_WIDE_INT cnt;
5219 constructor_elt *ce;
5221 oc_local_state local;
5223 /* Setup our local state to communicate with helpers. */
5224 local.exp = exp;
5225 local.type = TREE_TYPE (exp);
5226 local.size = size;
5227 local.align = align;
5228 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5229 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5230 else
5231 local.min_index = NULL_TREE;
5233 local.total_bytes = 0;
5234 local.byte_buffer_in_use = outer != NULL;
5235 local.byte = outer ? outer->byte : 0;
5236 local.last_relative_index = -1;
5238 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5240 /* As CE goes through the elements of the constant, FIELD goes through the
5241 structure fields if the constant is a structure. If the constant is a
5242 union, we override this by getting the field from the TREE_LIST element.
5243 But the constant could also be an array. Then FIELD is zero.
5245 There is always a maximum of one element in the chain LINK for unions
5246 (even if the initializer in a source program incorrectly contains
5247 more one). */
5249 if (TREE_CODE (local.type) == RECORD_TYPE)
5250 local.field = TYPE_FIELDS (local.type);
5251 else
5252 local.field = NULL_TREE;
5254 for (cnt = 0;
5255 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5256 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5258 local.val = ce->value;
5259 local.index = NULL_TREE;
5261 /* The element in a union constructor specifies the proper field
5262 or index. */
5263 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5264 local.field = ce->index;
5266 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5267 local.index = ce->index;
5269 if (local.field && flag_verbose_asm)
5270 fprintf (asm_out_file, "%s %s:\n",
5271 ASM_COMMENT_START,
5272 DECL_NAME (local.field)
5273 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5274 : "<anonymous>");
5276 /* Eliminate the marker that makes a cast not be an lvalue. */
5277 if (local.val != NULL_TREE)
5278 STRIP_NOPS (local.val);
5280 /* Output the current element, using the appropriate helper ... */
5282 /* For an array slice not part of an outer bitfield. */
5283 if (!outer
5284 && local.index != NULL_TREE
5285 && TREE_CODE (local.index) == RANGE_EXPR)
5286 output_constructor_array_range (&local);
5288 /* For a field that is neither a true bitfield nor part of an outer one,
5289 known to be at least byte aligned and multiple-of-bytes long. */
5290 else if (!outer
5291 && (local.field == NULL_TREE
5292 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5293 output_constructor_regular_field (&local);
5295 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5296 supported for scalar fields, so we may need to convert first. */
5297 else
5299 if (TREE_CODE (local.val) == REAL_CST)
5300 local.val
5301 = fold_unary (VIEW_CONVERT_EXPR,
5302 build_nonstandard_integer_type
5303 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5304 local.val);
5305 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5309 /* If we are not at toplevel, save the pending data for our caller.
5310 Otherwise output the pending data and padding zeros as needed. */
5311 if (outer)
5312 outer->byte = local.byte;
5313 else
5315 if (local.byte_buffer_in_use)
5317 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5318 local.total_bytes++;
5321 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5323 assemble_zeros (local.size - local.total_bytes);
5324 local.total_bytes = local.size;
5328 return local.total_bytes;
5331 /* Mark DECL as weak. */
5333 static void
5334 mark_weak (tree decl)
5336 if (DECL_WEAK (decl))
5337 return;
5339 struct symtab_node *n = symtab_node::get (decl);
5340 if (n && n->refuse_visibility_changes)
5341 error ("%+D declared weak after being used", decl);
5342 DECL_WEAK (decl) = 1;
5344 if (DECL_RTL_SET_P (decl)
5345 && MEM_P (DECL_RTL (decl))
5346 && XEXP (DECL_RTL (decl), 0)
5347 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5348 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5351 /* Merge weak status between NEWDECL and OLDDECL. */
5353 void
5354 merge_weak (tree newdecl, tree olddecl)
5356 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5358 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5360 tree *pwd;
5361 /* We put the NEWDECL on the weak_decls list at some point
5362 and OLDDECL as well. Keep just OLDDECL on the list. */
5363 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5364 if (TREE_VALUE (*pwd) == newdecl)
5366 *pwd = TREE_CHAIN (*pwd);
5367 break;
5370 return;
5373 if (DECL_WEAK (newdecl))
5375 tree wd;
5377 /* NEWDECL is weak, but OLDDECL is not. */
5379 /* If we already output the OLDDECL, we're in trouble; we can't
5380 go back and make it weak. This should never happen in
5381 unit-at-a-time compilation. */
5382 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5384 /* If we've already generated rtl referencing OLDDECL, we may
5385 have done so in a way that will not function properly with
5386 a weak symbol. Again in unit-at-a-time this should be
5387 impossible. */
5388 gcc_assert (!TREE_USED (olddecl)
5389 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5391 if (TARGET_SUPPORTS_WEAK)
5393 /* We put the NEWDECL on the weak_decls list at some point.
5394 Replace it with the OLDDECL. */
5395 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5396 if (TREE_VALUE (wd) == newdecl)
5398 TREE_VALUE (wd) = olddecl;
5399 break;
5401 /* We may not find the entry on the list. If NEWDECL is a
5402 weak alias, then we will have already called
5403 globalize_decl to remove the entry; in that case, we do
5404 not need to do anything. */
5407 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5408 mark_weak (olddecl);
5410 else
5411 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5412 weak. Just update NEWDECL to indicate that it's weak too. */
5413 mark_weak (newdecl);
5416 /* Declare DECL to be a weak symbol. */
5418 void
5419 declare_weak (tree decl)
5421 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5422 if (! TREE_PUBLIC (decl))
5423 error ("weak declaration of %q+D must be public", decl);
5424 else if (!TARGET_SUPPORTS_WEAK)
5425 warning (0, "weak declaration of %q+D not supported", decl);
5427 mark_weak (decl);
5428 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5429 DECL_ATTRIBUTES (decl)
5430 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5433 static void
5434 weak_finish_1 (tree decl)
5436 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5437 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5438 #endif
5440 if (! TREE_USED (decl))
5441 return;
5443 #ifdef ASM_WEAKEN_DECL
5444 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5445 #else
5446 #ifdef ASM_WEAKEN_LABEL
5447 ASM_WEAKEN_LABEL (asm_out_file, name);
5448 #else
5449 #ifdef ASM_OUTPUT_WEAK_ALIAS
5451 static bool warn_once = 0;
5452 if (! warn_once)
5454 warning (0, "only weak aliases are supported in this configuration");
5455 warn_once = 1;
5457 return;
5459 #endif
5460 #endif
5461 #endif
5464 /* Fiven an assembly name, find the decl it is associated with. */
5465 static tree
5466 find_decl (tree target)
5468 symtab_node *node = symtab_node::get_for_asmname (target);
5469 if (node)
5470 return node->decl;
5471 return NULL_TREE;
5474 /* This TREE_LIST contains weakref targets. */
5476 static GTY(()) tree weakref_targets;
5478 /* Emit any pending weak declarations. */
5480 void
5481 weak_finish (void)
5483 tree t;
5485 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5487 tree alias_decl = TREE_PURPOSE (t);
5488 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5490 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5491 /* Remove alias_decl from the weak list, but leave entries for
5492 the target alone. */
5493 target = NULL_TREE;
5494 #ifndef ASM_OUTPUT_WEAKREF
5495 else if (! TREE_SYMBOL_REFERENCED (target))
5497 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5498 defined, otherwise we and weak_finish_1 would use
5499 different macros. */
5500 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5501 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5502 # else
5503 tree decl = find_decl (target);
5505 if (! decl)
5507 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5508 TREE_CODE (alias_decl), target,
5509 TREE_TYPE (alias_decl));
5511 DECL_EXTERNAL (decl) = 1;
5512 TREE_PUBLIC (decl) = 1;
5513 DECL_ARTIFICIAL (decl) = 1;
5514 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5515 TREE_USED (decl) = 1;
5518 weak_finish_1 (decl);
5519 # endif
5521 #endif
5524 tree *p;
5525 tree t2;
5527 /* Remove the alias and the target from the pending weak list
5528 so that we do not emit any .weak directives for the former,
5529 nor multiple .weak directives for the latter. */
5530 for (p = &weak_decls; (t2 = *p) ; )
5532 if (TREE_VALUE (t2) == alias_decl
5533 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5534 *p = TREE_CHAIN (t2);
5535 else
5536 p = &TREE_CHAIN (t2);
5539 /* Remove other weakrefs to the same target, to speed things up. */
5540 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5542 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5543 *p = TREE_CHAIN (t2);
5544 else
5545 p = &TREE_CHAIN (t2);
5550 for (t = weak_decls; t; t = TREE_CHAIN (t))
5552 tree decl = TREE_VALUE (t);
5554 weak_finish_1 (decl);
5558 /* Emit the assembly bits to indicate that DECL is globally visible. */
5560 static void
5561 globalize_decl (tree decl)
5564 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5565 if (DECL_WEAK (decl))
5567 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5568 tree *p, t;
5570 #ifdef ASM_WEAKEN_DECL
5571 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5572 #else
5573 ASM_WEAKEN_LABEL (asm_out_file, name);
5574 #endif
5576 /* Remove this function from the pending weak list so that
5577 we do not emit multiple .weak directives for it. */
5578 for (p = &weak_decls; (t = *p) ; )
5580 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5581 *p = TREE_CHAIN (t);
5582 else
5583 p = &TREE_CHAIN (t);
5586 /* Remove weakrefs to the same target from the pending weakref
5587 list, for the same reason. */
5588 for (p = &weakref_targets; (t = *p) ; )
5590 if (DECL_ASSEMBLER_NAME (decl)
5591 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5592 *p = TREE_CHAIN (t);
5593 else
5594 p = &TREE_CHAIN (t);
5597 return;
5599 #endif
5601 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5604 vec<alias_pair, va_gc> *alias_pairs;
5606 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5607 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5608 tree node is DECL to have the value of the tree node TARGET. */
5610 void
5611 do_assemble_alias (tree decl, tree target)
5613 tree id;
5615 /* Emulated TLS had better not get this var. */
5616 gcc_assert (!(!targetm.have_tls
5617 && TREE_CODE (decl) == VAR_DECL
5618 && DECL_THREAD_LOCAL_P (decl)));
5620 if (TREE_ASM_WRITTEN (decl))
5621 return;
5623 id = DECL_ASSEMBLER_NAME (decl);
5624 ultimate_transparent_alias_target (&id);
5625 ultimate_transparent_alias_target (&target);
5627 /* We must force creation of DECL_RTL for debug info generation, even though
5628 we don't use it here. */
5629 make_decl_rtl (decl);
5631 TREE_ASM_WRITTEN (decl) = 1;
5632 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5633 TREE_ASM_WRITTEN (id) = 1;
5635 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5637 if (!TREE_SYMBOL_REFERENCED (target))
5638 weakref_targets = tree_cons (decl, target, weakref_targets);
5640 #ifdef ASM_OUTPUT_WEAKREF
5641 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5642 IDENTIFIER_POINTER (id),
5643 IDENTIFIER_POINTER (target));
5644 #else
5645 if (!TARGET_SUPPORTS_WEAK)
5647 error_at (DECL_SOURCE_LOCATION (decl),
5648 "weakref is not supported in this configuration");
5649 return;
5651 #endif
5652 return;
5655 #ifdef ASM_OUTPUT_DEF
5656 tree orig_decl = decl;
5658 if (TREE_CODE (decl) == FUNCTION_DECL
5659 && cgraph_node::get (decl)->instrumentation_clone
5660 && cgraph_node::get (decl)->instrumented_version)
5661 orig_decl = cgraph_node::get (decl)->instrumented_version->decl;
5663 /* Make name accessible from other files, if appropriate. */
5665 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5667 globalize_decl (decl);
5668 maybe_assemble_visibility (decl);
5670 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5672 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5673 if (targetm.has_ifunc_p ())
5674 ASM_OUTPUT_TYPE_DIRECTIVE
5675 (asm_out_file, IDENTIFIER_POINTER (id),
5676 IFUNC_ASM_TYPE);
5677 else
5678 #endif
5679 error_at (DECL_SOURCE_LOCATION (decl),
5680 "ifunc is not supported on this target");
5683 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5684 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5685 # else
5686 ASM_OUTPUT_DEF (asm_out_file,
5687 IDENTIFIER_POINTER (id),
5688 IDENTIFIER_POINTER (target));
5689 # endif
5690 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5692 const char *name;
5693 tree *p, t;
5695 name = IDENTIFIER_POINTER (id);
5696 # ifdef ASM_WEAKEN_DECL
5697 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5698 # else
5699 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5700 # endif
5701 /* Remove this function from the pending weak list so that
5702 we do not emit multiple .weak directives for it. */
5703 for (p = &weak_decls; (t = *p) ; )
5704 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5705 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5706 *p = TREE_CHAIN (t);
5707 else
5708 p = &TREE_CHAIN (t);
5710 /* Remove weakrefs to the same target from the pending weakref
5711 list, for the same reason. */
5712 for (p = &weakref_targets; (t = *p) ; )
5714 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5715 *p = TREE_CHAIN (t);
5716 else
5717 p = &TREE_CHAIN (t);
5720 #endif
5723 /* Emit an assembler directive to make the symbol for DECL an alias to
5724 the symbol for TARGET. */
5726 void
5727 assemble_alias (tree decl, tree target)
5729 tree target_decl;
5731 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5733 tree alias = DECL_ASSEMBLER_NAME (decl);
5735 ultimate_transparent_alias_target (&target);
5737 if (alias == target)
5738 error ("weakref %q+D ultimately targets itself", decl);
5739 if (TREE_PUBLIC (decl))
5740 error ("weakref %q+D must have static linkage", decl);
5742 else
5744 #if !defined (ASM_OUTPUT_DEF)
5745 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5746 error_at (DECL_SOURCE_LOCATION (decl),
5747 "alias definitions not supported in this configuration");
5748 TREE_ASM_WRITTEN (decl) = 1;
5749 return;
5750 # else
5751 if (!DECL_WEAK (decl))
5753 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5754 error_at (DECL_SOURCE_LOCATION (decl),
5755 "ifunc is not supported in this configuration");
5756 else
5757 error_at (DECL_SOURCE_LOCATION (decl),
5758 "only weak aliases are supported in this configuration");
5759 TREE_ASM_WRITTEN (decl) = 1;
5760 return;
5762 # endif
5763 #endif
5765 TREE_USED (decl) = 1;
5767 /* Allow aliases to aliases. */
5768 if (TREE_CODE (decl) == FUNCTION_DECL)
5769 cgraph_node::get_create (decl)->alias = true;
5770 else
5771 varpool_node::get_create (decl)->alias = true;
5773 /* If the target has already been emitted, we don't have to queue the
5774 alias. This saves a tad of memory. */
5775 if (symtab->global_info_ready)
5776 target_decl = find_decl (target);
5777 else
5778 target_decl= NULL;
5779 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5780 || symtab->state >= EXPANSION)
5781 do_assemble_alias (decl, target);
5782 else
5784 alias_pair p = {decl, target};
5785 vec_safe_push (alias_pairs, p);
5789 /* Record and output a table of translations from original function
5790 to its transaction aware clone. Note that tm_pure functions are
5791 considered to be their own clone. */
5793 struct tm_clone_hasher : ggc_cache_hasher<tree_map *>
5795 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
5796 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
5798 static void
5799 handle_cache_entry (tree_map *&e)
5801 extern void gt_ggc_mx (tree_map *&);
5802 if (e == HTAB_EMPTY_ENTRY || e == HTAB_DELETED_ENTRY)
5803 return;
5804 else if (ggc_marked_p (e->base.from))
5805 gt_ggc_mx (e);
5806 else
5807 e = static_cast<tree_map *> (HTAB_DELETED_ENTRY);
5811 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
5813 void
5814 record_tm_clone_pair (tree o, tree n)
5816 struct tree_map **slot, *h;
5818 if (tm_clone_hash == NULL)
5819 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
5821 h = ggc_alloc<tree_map> ();
5822 h->hash = htab_hash_pointer (o);
5823 h->base.from = o;
5824 h->to = n;
5826 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
5827 *slot = h;
5830 tree
5831 get_tm_clone_pair (tree o)
5833 if (tm_clone_hash)
5835 struct tree_map *h, in;
5837 in.base.from = o;
5838 in.hash = htab_hash_pointer (o);
5839 h = tm_clone_hash->find_with_hash (&in, in.hash);
5840 if (h)
5841 return h->to;
5843 return NULL_TREE;
5846 typedef struct tm_alias_pair
5848 unsigned int uid;
5849 tree from;
5850 tree to;
5851 } tm_alias_pair;
5854 /* Dump the actual pairs to the .tm_clone_table section. */
5856 static void
5857 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5859 unsigned i;
5860 tm_alias_pair *p;
5861 bool switched = false;
5863 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5865 tree src = p->from;
5866 tree dst = p->to;
5867 struct cgraph_node *src_n = cgraph_node::get (src);
5868 struct cgraph_node *dst_n = cgraph_node::get (dst);
5870 /* The function ipa_tm_create_version() marks the clone as needed if
5871 the original function was needed. But we also mark the clone as
5872 needed if we ever called the clone indirectly through
5873 TM_GETTMCLONE. If neither of these are true, we didn't generate
5874 a clone, and we didn't call it indirectly... no sense keeping it
5875 in the clone table. */
5876 if (!dst_n || !dst_n->definition)
5877 continue;
5879 /* This covers the case where we have optimized the original
5880 function away, and only access the transactional clone. */
5881 if (!src_n || !src_n->definition)
5882 continue;
5884 if (!switched)
5886 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5887 assemble_align (POINTER_SIZE);
5888 switched = true;
5891 assemble_integer (XEXP (DECL_RTL (src), 0),
5892 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5893 assemble_integer (XEXP (DECL_RTL (dst), 0),
5894 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5898 /* Provide a default for the tm_clone_table section. */
5900 section *
5901 default_clone_table_section (void)
5903 return get_named_section (NULL, ".tm_clone_table", 3);
5906 /* Helper comparison function for qsorting by the DECL_UID stored in
5907 alias_pair->emitted_diags. */
5909 static int
5910 tm_alias_pair_cmp (const void *x, const void *y)
5912 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5913 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5914 if (p1->uid < p2->uid)
5915 return -1;
5916 if (p1->uid > p2->uid)
5917 return 1;
5918 return 0;
5921 void
5922 finish_tm_clone_pairs (void)
5924 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5926 if (tm_clone_hash == NULL)
5927 return;
5929 /* We need a determenistic order for the .tm_clone_table, otherwise
5930 we will get bootstrap comparison failures, so dump the hash table
5931 to a vector, sort it, and dump the vector. */
5933 /* Dump the hashtable to a vector. */
5934 tree_map *map;
5935 hash_table<tm_clone_hasher>::iterator iter;
5936 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
5938 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5939 tm_alias_pairs.safe_push (p);
5941 /* Sort it. */
5942 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5944 /* Dump it. */
5945 dump_tm_clone_pairs (tm_alias_pairs);
5947 tm_clone_hash->empty ();
5948 tm_clone_hash = NULL;
5949 tm_alias_pairs.release ();
5953 /* Emit an assembler directive to set symbol for DECL visibility to
5954 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5956 void
5957 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5958 int vis ATTRIBUTE_UNUSED)
5960 #ifdef HAVE_GAS_HIDDEN
5961 static const char * const visibility_types[] = {
5962 NULL, "protected", "hidden", "internal"
5965 const char *name, *type;
5966 tree id;
5968 id = DECL_ASSEMBLER_NAME (decl);
5969 ultimate_transparent_alias_target (&id);
5970 name = IDENTIFIER_POINTER (id);
5972 type = visibility_types[vis];
5974 fprintf (asm_out_file, "\t.%s\t", type);
5975 assemble_name (asm_out_file, name);
5976 fprintf (asm_out_file, "\n");
5977 #else
5978 if (!DECL_ARTIFICIAL (decl))
5979 warning (OPT_Wattributes, "visibility attribute not supported "
5980 "in this configuration; ignored");
5981 #endif
5984 /* A helper function to call assemble_visibility when needed for a decl. */
5987 maybe_assemble_visibility (tree decl)
5989 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5991 if (TREE_CODE (decl) == FUNCTION_DECL
5992 && cgraph_node::get (decl)
5993 && cgraph_node::get (decl)->instrumentation_clone
5994 && cgraph_node::get (decl)->instrumented_version)
5995 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
5997 if (vis != VISIBILITY_DEFAULT)
5999 targetm.asm_out.assemble_visibility (decl, vis);
6000 return 1;
6002 else
6003 return 0;
6006 /* Returns 1 if the target configuration supports defining public symbols
6007 so that one of them will be chosen at link time instead of generating a
6008 multiply-defined symbol error, whether through the use of weak symbols or
6009 a target-specific mechanism for having duplicates discarded. */
6012 supports_one_only (void)
6014 if (SUPPORTS_ONE_ONLY)
6015 return 1;
6016 return TARGET_SUPPORTS_WEAK;
6019 /* Set up DECL as a public symbol that can be defined in multiple
6020 translation units without generating a linker error. */
6022 void
6023 make_decl_one_only (tree decl, tree comdat_group)
6025 struct symtab_node *symbol;
6026 gcc_assert (TREE_CODE (decl) == VAR_DECL
6027 || TREE_CODE (decl) == FUNCTION_DECL);
6029 TREE_PUBLIC (decl) = 1;
6031 if (TREE_CODE (decl) == VAR_DECL)
6032 symbol = varpool_node::get_create (decl);
6033 else
6034 symbol = cgraph_node::get_create (decl);
6036 if (SUPPORTS_ONE_ONLY)
6038 #ifdef MAKE_DECL_ONE_ONLY
6039 MAKE_DECL_ONE_ONLY (decl);
6040 #endif
6041 symbol->set_comdat_group (comdat_group);
6043 else if (TREE_CODE (decl) == VAR_DECL
6044 && (DECL_INITIAL (decl) == 0
6045 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6046 DECL_COMMON (decl) = 1;
6047 else
6049 gcc_assert (TARGET_SUPPORTS_WEAK);
6050 DECL_WEAK (decl) = 1;
6054 void
6055 init_varasm_once (void)
6057 section_htab = hash_table<section_hasher>::create_ggc (31);
6058 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6059 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6061 shared_constant_pool = create_constant_pool ();
6063 #ifdef TEXT_SECTION_ASM_OP
6064 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6065 TEXT_SECTION_ASM_OP);
6066 #endif
6068 #ifdef DATA_SECTION_ASM_OP
6069 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6070 DATA_SECTION_ASM_OP);
6071 #endif
6073 #ifdef SDATA_SECTION_ASM_OP
6074 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6075 SDATA_SECTION_ASM_OP);
6076 #endif
6078 #ifdef READONLY_DATA_SECTION_ASM_OP
6079 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6080 READONLY_DATA_SECTION_ASM_OP);
6081 #endif
6083 #ifdef CTORS_SECTION_ASM_OP
6084 ctors_section = get_unnamed_section (0, output_section_asm_op,
6085 CTORS_SECTION_ASM_OP);
6086 #endif
6088 #ifdef DTORS_SECTION_ASM_OP
6089 dtors_section = get_unnamed_section (0, output_section_asm_op,
6090 DTORS_SECTION_ASM_OP);
6091 #endif
6093 #ifdef BSS_SECTION_ASM_OP
6094 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6095 output_section_asm_op,
6096 BSS_SECTION_ASM_OP);
6097 #endif
6099 #ifdef SBSS_SECTION_ASM_OP
6100 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6101 output_section_asm_op,
6102 SBSS_SECTION_ASM_OP);
6103 #endif
6105 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6106 | SECTION_COMMON, emit_tls_common);
6107 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6108 | SECTION_COMMON, emit_local);
6109 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6110 | SECTION_COMMON, emit_common);
6112 #if defined ASM_OUTPUT_ALIGNED_BSS
6113 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6114 emit_bss);
6115 #endif
6117 targetm.asm_out.init_sections ();
6119 if (readonly_data_section == NULL)
6120 readonly_data_section = text_section;
6122 #ifdef ASM_OUTPUT_EXTERNAL
6123 pending_assemble_externals_set = new hash_set<tree>;
6124 #endif
6127 enum tls_model
6128 decl_default_tls_model (const_tree decl)
6130 enum tls_model kind;
6131 bool is_local;
6133 is_local = targetm.binds_local_p (decl);
6134 if (!flag_shlib)
6136 if (is_local)
6137 kind = TLS_MODEL_LOCAL_EXEC;
6138 else
6139 kind = TLS_MODEL_INITIAL_EXEC;
6142 /* Local dynamic is inefficient when we're not combining the
6143 parts of the address. */
6144 else if (optimize && is_local)
6145 kind = TLS_MODEL_LOCAL_DYNAMIC;
6146 else
6147 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6148 if (kind < flag_tls_default)
6149 kind = flag_tls_default;
6151 return kind;
6154 /* Select a set of attributes for section NAME based on the properties
6155 of DECL and whether or not RELOC indicates that DECL's initializer
6156 might contain runtime relocations.
6158 We make the section read-only and executable for a function decl,
6159 read-only for a const data decl, and writable for a non-const data decl. */
6161 unsigned int
6162 default_section_type_flags (tree decl, const char *name, int reloc)
6164 unsigned int flags;
6166 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6167 flags = SECTION_CODE;
6168 else if (decl)
6170 enum section_category category
6171 = categorize_decl_for_section (decl, reloc);
6172 if (decl_readonly_section_1 (category))
6173 flags = 0;
6174 else if (category == SECCAT_DATA_REL_RO
6175 || category == SECCAT_DATA_REL_RO_LOCAL)
6176 flags = SECTION_WRITE | SECTION_RELRO;
6177 else
6178 flags = SECTION_WRITE;
6180 else
6182 flags = SECTION_WRITE;
6183 if (strcmp (name, ".data.rel.ro") == 0
6184 || strcmp (name, ".data.rel.ro.local") == 0)
6185 flags |= SECTION_RELRO;
6188 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6189 flags |= SECTION_LINKONCE;
6191 if (strcmp (name, ".vtable_map_vars") == 0)
6192 flags |= SECTION_LINKONCE;
6194 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6195 flags |= SECTION_TLS | SECTION_WRITE;
6197 if (strcmp (name, ".bss") == 0
6198 || strncmp (name, ".bss.", 5) == 0
6199 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6200 || strcmp (name, ".persistent.bss") == 0
6201 || strcmp (name, ".sbss") == 0
6202 || strncmp (name, ".sbss.", 6) == 0
6203 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6204 flags |= SECTION_BSS;
6206 if (strcmp (name, ".tdata") == 0
6207 || strncmp (name, ".tdata.", 7) == 0
6208 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6209 flags |= SECTION_TLS;
6211 if (strcmp (name, ".tbss") == 0
6212 || strncmp (name, ".tbss.", 6) == 0
6213 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6214 flags |= SECTION_TLS | SECTION_BSS;
6216 /* These three sections have special ELF types. They are neither
6217 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6218 want to print a section type (@progbits or @nobits). If someone
6219 is silly enough to emit code or TLS variables to one of these
6220 sections, then don't handle them specially. */
6221 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6222 && (strcmp (name, ".init_array") == 0
6223 || strcmp (name, ".fini_array") == 0
6224 || strcmp (name, ".preinit_array") == 0))
6225 flags |= SECTION_NOTYPE;
6227 return flags;
6230 /* Return true if the target supports some form of global BSS,
6231 either through bss_noswitch_section, or by selecting a BSS
6232 section in TARGET_ASM_SELECT_SECTION. */
6234 bool
6235 have_global_bss_p (void)
6237 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6240 /* Output assembly to switch to section NAME with attribute FLAGS.
6241 Four variants for common object file formats. */
6243 void
6244 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6245 unsigned int flags ATTRIBUTE_UNUSED,
6246 tree decl ATTRIBUTE_UNUSED)
6248 /* Some object formats don't support named sections at all. The
6249 front-end should already have flagged this as an error. */
6250 gcc_unreachable ();
6253 #ifndef TLS_SECTION_ASM_FLAG
6254 #define TLS_SECTION_ASM_FLAG 'T'
6255 #endif
6257 void
6258 default_elf_asm_named_section (const char *name, unsigned int flags,
6259 tree decl ATTRIBUTE_UNUSED)
6261 char flagchars[10], *f = flagchars;
6263 /* If we have already declared this section, we can use an
6264 abbreviated form to switch back to it -- unless this section is
6265 part of a COMDAT groups, in which case GAS requires the full
6266 declaration every time. */
6267 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6268 && (flags & SECTION_DECLARED))
6270 fprintf (asm_out_file, "\t.section\t%s\n", name);
6271 return;
6274 if (!(flags & SECTION_DEBUG))
6275 *f++ = 'a';
6276 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6277 if (flags & SECTION_EXCLUDE)
6278 *f++ = 'e';
6279 #endif
6280 if (flags & SECTION_WRITE)
6281 *f++ = 'w';
6282 if (flags & SECTION_CODE)
6283 *f++ = 'x';
6284 if (flags & SECTION_SMALL)
6285 *f++ = 's';
6286 if (flags & SECTION_MERGE)
6287 *f++ = 'M';
6288 if (flags & SECTION_STRINGS)
6289 *f++ = 'S';
6290 if (flags & SECTION_TLS)
6291 *f++ = TLS_SECTION_ASM_FLAG;
6292 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6293 *f++ = 'G';
6294 *f = '\0';
6296 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6298 if (!(flags & SECTION_NOTYPE))
6300 const char *type;
6301 const char *format;
6303 if (flags & SECTION_BSS)
6304 type = "nobits";
6305 else
6306 type = "progbits";
6308 format = ",@%s";
6309 /* On platforms that use "@" as the assembly comment character,
6310 use "%" instead. */
6311 if (strcmp (ASM_COMMENT_START, "@") == 0)
6312 format = ",%%%s";
6313 fprintf (asm_out_file, format, type);
6315 if (flags & SECTION_ENTSIZE)
6316 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6317 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6319 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6320 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6321 else
6322 fprintf (asm_out_file, ",%s,comdat",
6323 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6327 putc ('\n', asm_out_file);
6330 void
6331 default_coff_asm_named_section (const char *name, unsigned int flags,
6332 tree decl ATTRIBUTE_UNUSED)
6334 char flagchars[8], *f = flagchars;
6336 if (flags & SECTION_WRITE)
6337 *f++ = 'w';
6338 if (flags & SECTION_CODE)
6339 *f++ = 'x';
6340 *f = '\0';
6342 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6345 void
6346 default_pe_asm_named_section (const char *name, unsigned int flags,
6347 tree decl)
6349 default_coff_asm_named_section (name, flags, decl);
6351 if (flags & SECTION_LINKONCE)
6353 /* Functions may have been compiled at various levels of
6354 optimization so we can't use `same_size' here.
6355 Instead, have the linker pick one. */
6356 fprintf (asm_out_file, "\t.linkonce %s\n",
6357 (flags & SECTION_CODE ? "discard" : "same_size"));
6361 /* The lame default section selector. */
6363 section *
6364 default_select_section (tree decl, int reloc,
6365 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6367 if (DECL_P (decl))
6369 if (decl_readonly_section (decl, reloc))
6370 return readonly_data_section;
6372 else if (TREE_CODE (decl) == CONSTRUCTOR)
6374 if (! ((flag_pic && reloc)
6375 || !TREE_READONLY (decl)
6376 || TREE_SIDE_EFFECTS (decl)
6377 || !TREE_CONSTANT (decl)))
6378 return readonly_data_section;
6380 else if (TREE_CODE (decl) == STRING_CST)
6381 return readonly_data_section;
6382 else if (! (flag_pic && reloc))
6383 return readonly_data_section;
6385 return data_section;
6388 enum section_category
6389 categorize_decl_for_section (const_tree decl, int reloc)
6391 enum section_category ret;
6393 if (TREE_CODE (decl) == FUNCTION_DECL)
6394 return SECCAT_TEXT;
6395 else if (TREE_CODE (decl) == STRING_CST)
6397 if ((flag_sanitize & SANITIZE_ADDRESS)
6398 && asan_protect_global (CONST_CAST_TREE (decl)))
6399 /* or !flag_merge_constants */
6400 return SECCAT_RODATA;
6401 else
6402 return SECCAT_RODATA_MERGE_STR;
6404 else if (TREE_CODE (decl) == VAR_DECL)
6406 if (bss_initializer_p (decl))
6407 ret = SECCAT_BSS;
6408 else if (! TREE_READONLY (decl)
6409 || TREE_SIDE_EFFECTS (decl)
6410 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6412 /* Here the reloc_rw_mask is not testing whether the section should
6413 be read-only or not, but whether the dynamic link will have to
6414 do something. If so, we wish to segregate the data in order to
6415 minimize cache misses inside the dynamic linker. */
6416 if (reloc & targetm.asm_out.reloc_rw_mask ())
6417 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6418 else
6419 ret = SECCAT_DATA;
6421 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6422 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6423 else if (reloc || flag_merge_constants < 2
6424 || ((flag_sanitize & SANITIZE_ADDRESS)
6425 && asan_protect_global (CONST_CAST_TREE (decl))))
6426 /* C and C++ don't allow different variables to share the same
6427 location. -fmerge-all-constants allows even that (at the
6428 expense of not conforming). */
6429 ret = SECCAT_RODATA;
6430 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6431 ret = SECCAT_RODATA_MERGE_STR_INIT;
6432 else
6433 ret = SECCAT_RODATA_MERGE_CONST;
6435 else if (TREE_CODE (decl) == CONSTRUCTOR)
6437 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6438 || TREE_SIDE_EFFECTS (decl)
6439 || ! TREE_CONSTANT (decl))
6440 ret = SECCAT_DATA;
6441 else
6442 ret = SECCAT_RODATA;
6444 else
6445 ret = SECCAT_RODATA;
6447 /* There are no read-only thread-local sections. */
6448 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6450 /* Note that this would be *just* SECCAT_BSS, except that there's
6451 no concept of a read-only thread-local-data section. */
6452 if (ret == SECCAT_BSS
6453 || (flag_zero_initialized_in_bss
6454 && initializer_zerop (DECL_INITIAL (decl))))
6455 ret = SECCAT_TBSS;
6456 else
6457 ret = SECCAT_TDATA;
6460 /* If the target uses small data sections, select it. */
6461 else if (targetm.in_small_data_p (decl))
6463 if (ret == SECCAT_BSS)
6464 ret = SECCAT_SBSS;
6465 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6466 ret = SECCAT_SRODATA;
6467 else
6468 ret = SECCAT_SDATA;
6471 return ret;
6474 static bool
6475 decl_readonly_section_1 (enum section_category category)
6477 switch (category)
6479 case SECCAT_RODATA:
6480 case SECCAT_RODATA_MERGE_STR:
6481 case SECCAT_RODATA_MERGE_STR_INIT:
6482 case SECCAT_RODATA_MERGE_CONST:
6483 case SECCAT_SRODATA:
6484 return true;
6485 default:
6486 return false;
6490 bool
6491 decl_readonly_section (const_tree decl, int reloc)
6493 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6496 /* Select a section based on the above categorization. */
6498 section *
6499 default_elf_select_section (tree decl, int reloc,
6500 unsigned HOST_WIDE_INT align)
6502 const char *sname;
6503 switch (categorize_decl_for_section (decl, reloc))
6505 case SECCAT_TEXT:
6506 /* We're not supposed to be called on FUNCTION_DECLs. */
6507 gcc_unreachable ();
6508 case SECCAT_RODATA:
6509 return readonly_data_section;
6510 case SECCAT_RODATA_MERGE_STR:
6511 return mergeable_string_section (decl, align, 0);
6512 case SECCAT_RODATA_MERGE_STR_INIT:
6513 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6514 case SECCAT_RODATA_MERGE_CONST:
6515 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6516 case SECCAT_SRODATA:
6517 sname = ".sdata2";
6518 break;
6519 case SECCAT_DATA:
6520 return data_section;
6521 case SECCAT_DATA_REL:
6522 sname = ".data.rel";
6523 break;
6524 case SECCAT_DATA_REL_LOCAL:
6525 sname = ".data.rel.local";
6526 break;
6527 case SECCAT_DATA_REL_RO:
6528 sname = ".data.rel.ro";
6529 break;
6530 case SECCAT_DATA_REL_RO_LOCAL:
6531 sname = ".data.rel.ro.local";
6532 break;
6533 case SECCAT_SDATA:
6534 sname = ".sdata";
6535 break;
6536 case SECCAT_TDATA:
6537 sname = ".tdata";
6538 break;
6539 case SECCAT_BSS:
6540 if (bss_section)
6541 return bss_section;
6542 sname = ".bss";
6543 break;
6544 case SECCAT_SBSS:
6545 sname = ".sbss";
6546 break;
6547 case SECCAT_TBSS:
6548 sname = ".tbss";
6549 break;
6550 default:
6551 gcc_unreachable ();
6554 return get_named_section (decl, sname, reloc);
6557 /* Construct a unique section name based on the decl name and the
6558 categorization performed above. */
6560 void
6561 default_unique_section (tree decl, int reloc)
6563 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6564 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6565 const char *prefix, *name, *linkonce;
6566 char *string;
6567 tree id;
6569 switch (categorize_decl_for_section (decl, reloc))
6571 case SECCAT_TEXT:
6572 prefix = one_only ? ".t" : ".text";
6573 break;
6574 case SECCAT_RODATA:
6575 case SECCAT_RODATA_MERGE_STR:
6576 case SECCAT_RODATA_MERGE_STR_INIT:
6577 case SECCAT_RODATA_MERGE_CONST:
6578 prefix = one_only ? ".r" : ".rodata";
6579 break;
6580 case SECCAT_SRODATA:
6581 prefix = one_only ? ".s2" : ".sdata2";
6582 break;
6583 case SECCAT_DATA:
6584 prefix = one_only ? ".d" : ".data";
6585 break;
6586 case SECCAT_DATA_REL:
6587 prefix = one_only ? ".d.rel" : ".data.rel";
6588 break;
6589 case SECCAT_DATA_REL_LOCAL:
6590 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6591 break;
6592 case SECCAT_DATA_REL_RO:
6593 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6594 break;
6595 case SECCAT_DATA_REL_RO_LOCAL:
6596 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6597 break;
6598 case SECCAT_SDATA:
6599 prefix = one_only ? ".s" : ".sdata";
6600 break;
6601 case SECCAT_BSS:
6602 prefix = one_only ? ".b" : ".bss";
6603 break;
6604 case SECCAT_SBSS:
6605 prefix = one_only ? ".sb" : ".sbss";
6606 break;
6607 case SECCAT_TDATA:
6608 prefix = one_only ? ".td" : ".tdata";
6609 break;
6610 case SECCAT_TBSS:
6611 prefix = one_only ? ".tb" : ".tbss";
6612 break;
6613 default:
6614 gcc_unreachable ();
6617 id = DECL_ASSEMBLER_NAME (decl);
6618 ultimate_transparent_alias_target (&id);
6619 name = IDENTIFIER_POINTER (id);
6620 name = targetm.strip_name_encoding (name);
6622 /* If we're using one_only, then there needs to be a .gnu.linkonce
6623 prefix to the section name. */
6624 linkonce = one_only ? ".gnu.linkonce" : "";
6626 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6628 set_decl_section_name (decl, string);
6631 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6633 static int
6634 compute_reloc_for_rtx_1 (const_rtx x)
6636 switch (GET_CODE (x))
6638 case SYMBOL_REF:
6639 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6640 case LABEL_REF:
6641 return 1;
6642 default:
6643 return 0;
6647 /* Like compute_reloc_for_constant, except for an RTX. The return value
6648 is a mask for which bit 1 indicates a global relocation, and bit 0
6649 indicates a local relocation. */
6651 static int
6652 compute_reloc_for_rtx (const_rtx x)
6654 switch (GET_CODE (x))
6656 case SYMBOL_REF:
6657 case LABEL_REF:
6658 return compute_reloc_for_rtx_1 (x);
6660 case CONST:
6662 int reloc = 0;
6663 subrtx_iterator::array_type array;
6664 FOR_EACH_SUBRTX (iter, array, x, ALL)
6665 reloc |= compute_reloc_for_rtx_1 (*iter);
6666 return reloc;
6669 default:
6670 return 0;
6674 section *
6675 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6676 rtx x,
6677 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6679 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6680 return data_section;
6681 else
6682 return readonly_data_section;
6685 section *
6686 default_elf_select_rtx_section (machine_mode mode, rtx x,
6687 unsigned HOST_WIDE_INT align)
6689 int reloc = compute_reloc_for_rtx (x);
6691 /* ??? Handle small data here somehow. */
6693 if (reloc & targetm.asm_out.reloc_rw_mask ())
6695 if (reloc == 1)
6696 return get_named_section (NULL, ".data.rel.ro.local", 1);
6697 else
6698 return get_named_section (NULL, ".data.rel.ro", 3);
6701 return mergeable_constant_section (mode, align, 0);
6704 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6706 void
6707 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6709 rtx symbol;
6710 int flags;
6712 /* Careful not to prod global register variables. */
6713 if (!MEM_P (rtl))
6714 return;
6715 symbol = XEXP (rtl, 0);
6716 if (GET_CODE (symbol) != SYMBOL_REF)
6717 return;
6719 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6720 if (TREE_CODE (decl) == FUNCTION_DECL)
6721 flags |= SYMBOL_FLAG_FUNCTION;
6722 if (targetm.binds_local_p (decl))
6723 flags |= SYMBOL_FLAG_LOCAL;
6724 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6725 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6726 else if (targetm.in_small_data_p (decl))
6727 flags |= SYMBOL_FLAG_SMALL;
6728 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6729 being PUBLIC, the thing *must* be defined in this translation unit.
6730 Prevent this buglet from being propagated into rtl code as well. */
6731 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6732 flags |= SYMBOL_FLAG_EXTERNAL;
6734 SYMBOL_REF_FLAGS (symbol) = flags;
6737 /* By default, we do nothing for encode_section_info, so we need not
6738 do anything but discard the '*' marker. */
6740 const char *
6741 default_strip_name_encoding (const char *str)
6743 return str + (*str == '*');
6746 #ifdef ASM_OUTPUT_DEF
6747 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6748 anchor relative to ".", the current section position. */
6750 void
6751 default_asm_output_anchor (rtx symbol)
6753 char buffer[100];
6755 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6756 SYMBOL_REF_BLOCK_OFFSET (symbol));
6757 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6759 #endif
6761 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6763 bool
6764 default_use_anchors_for_symbol_p (const_rtx symbol)
6766 section *sect;
6767 tree decl;
6769 /* Don't use anchors for mergeable sections. The linker might move
6770 the objects around. */
6771 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6772 if (sect->common.flags & SECTION_MERGE)
6773 return false;
6775 /* Don't use anchors for small data sections. The small data register
6776 acts as an anchor for such sections. */
6777 if (sect->common.flags & SECTION_SMALL)
6778 return false;
6780 decl = SYMBOL_REF_DECL (symbol);
6781 if (decl && DECL_P (decl))
6783 /* Don't use section anchors for decls that might be defined or
6784 usurped by other modules. */
6785 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6786 return false;
6788 /* Don't use section anchors for decls that will be placed in a
6789 small data section. */
6790 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6791 one above. The problem is that we only use SECTION_SMALL for
6792 sections that should be marked as small in the section directive. */
6793 if (targetm.in_small_data_p (decl))
6794 return false;
6796 return true;
6799 /* Return true when RESOLUTION indicate that symbol will be bound to the
6800 definition provided by current .o file. */
6802 static bool
6803 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6805 return (resolution == LDPR_PREVAILING_DEF
6806 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6807 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6810 /* Return true when RESOLUTION indicate that symbol will be bound locally
6811 within current executable or DSO. */
6813 static bool
6814 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6816 return (resolution == LDPR_PREVAILING_DEF
6817 || resolution == LDPR_PREVAILING_DEF_IRONLY
6818 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6819 || resolution == LDPR_PREEMPTED_REG
6820 || resolution == LDPR_PREEMPTED_IR
6821 || resolution == LDPR_RESOLVED_IR
6822 || resolution == LDPR_RESOLVED_EXEC);
6825 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
6826 uninitialized common symbol in the executable will still be defined
6827 (through COPY relocation) in the executable. */
6829 bool
6830 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
6831 bool extern_protected_data, bool common_local_p)
6833 /* A non-decl is an entry in the constant pool. */
6834 if (!DECL_P (exp))
6835 return true;
6837 /* Weakrefs may not bind locally, even though the weakref itself is always
6838 static and therefore local. Similarly, the resolver for ifunc functions
6839 might resolve to a non-local function.
6840 FIXME: We can resolve the weakref case more curefuly by looking at the
6841 weakref alias. */
6842 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6843 || (TREE_CODE (exp) == FUNCTION_DECL
6844 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6845 return false;
6847 /* Static variables are always local. */
6848 if (! TREE_PUBLIC (exp))
6849 return true;
6851 /* With resolution file in hand, take look into resolutions.
6852 We can't just return true for resolved_locally symbols,
6853 because dynamic linking might overwrite symbols
6854 in shared libraries. */
6855 bool resolved_locally = false;
6857 bool uninited_common = (DECL_COMMON (exp)
6858 && (DECL_INITIAL (exp) == NULL
6859 || (!in_lto_p
6860 && DECL_INITIAL (exp) == error_mark_node)));
6862 /* A non-external variable is defined locally only if it isn't
6863 uninitialized COMMON variable or common_local_p is true. */
6864 bool defined_locally = (!DECL_EXTERNAL (exp)
6865 && (!uninited_common || common_local_p));
6866 if (symtab_node *node = symtab_node::get (exp))
6868 if (node->in_other_partition)
6869 defined_locally = true;
6870 if (resolution_to_local_definition_p (node->resolution))
6871 defined_locally = resolved_locally = true;
6872 else if (resolution_local_p (node->resolution))
6873 resolved_locally = true;
6875 if (defined_locally && weak_dominate && !shlib)
6876 resolved_locally = true;
6878 /* Undefined weak symbols are never defined locally. */
6879 if (DECL_WEAK (exp) && !defined_locally)
6880 return false;
6882 /* A symbol is local if the user has said explicitly that it will be,
6883 or if we have a definition for the symbol. We cannot infer visibility
6884 for undefined symbols. */
6885 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
6886 && (TREE_CODE (exp) == FUNCTION_DECL
6887 || !extern_protected_data
6888 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
6889 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
6890 return true;
6892 /* If PIC, then assume that any global name can be overridden by
6893 symbols resolved from other modules. */
6894 if (shlib)
6895 return false;
6897 /* Variables defined outside this object might not be local. */
6898 if (DECL_EXTERNAL (exp) && !resolved_locally)
6899 return false;
6901 /* Non-dominant weak symbols are not defined locally. */
6902 if (DECL_WEAK (exp) && !resolved_locally)
6903 return false;
6905 /* Uninitialized COMMON variable may be unified with symbols
6906 resolved from other modules. */
6907 if (uninited_common && !resolved_locally)
6908 return false;
6910 /* Otherwise we're left with initialized (or non-common) global data
6911 which is of necessity defined locally. */
6912 return true;
6915 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6916 wrt cross-module name binding. */
6918 bool
6919 default_binds_local_p (const_tree exp)
6921 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
6924 /* Similar to default_binds_local_p, but common symbol may be local. */
6926 bool
6927 default_binds_local_p_2 (const_tree exp)
6929 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false,
6930 !flag_pic);
6933 bool
6934 default_binds_local_p_1 (const_tree exp, int shlib)
6936 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
6939 /* Return true when references to DECL must bind to current definition in
6940 final executable.
6942 The condition is usually equivalent to whether the function binds to the
6943 current module (shared library or executable), that is to binds_local_p.
6944 We use this fact to avoid need for another target hook and implement
6945 the logic using binds_local_p and just special cases where
6946 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6947 the weak definitions (that can be overwritten at linktime by other
6948 definition from different object file) and when resolution info is available
6949 we simply use the knowledge passed to us by linker plugin. */
6950 bool
6951 decl_binds_to_current_def_p (const_tree decl)
6953 gcc_assert (DECL_P (decl));
6954 if (!targetm.binds_local_p (decl))
6955 return false;
6956 if (!TREE_PUBLIC (decl))
6957 return true;
6959 /* When resolution is available, just use it. */
6960 if (symtab_node *node = symtab_node::get (decl))
6962 if (node->resolution != LDPR_UNKNOWN)
6963 return resolution_to_local_definition_p (node->resolution);
6966 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6967 binds locally but still can be overwritten), DECL_COMMON (can be merged
6968 with a non-common definition somewhere in the same module) or
6969 DECL_EXTERNAL.
6970 This rely on fact that binds_local_p behave as decl_replaceable_p
6971 for all other declaration types. */
6972 if (DECL_WEAK (decl))
6973 return false;
6974 if (DECL_COMMON (decl)
6975 && (DECL_INITIAL (decl) == NULL
6976 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6977 return false;
6978 if (DECL_EXTERNAL (decl))
6979 return false;
6980 return true;
6983 /* A replaceable function or variable is one which may be replaced
6984 at link-time with an entirely different definition, provided that the
6985 replacement has the same type. For example, functions declared
6986 with __attribute__((weak)) on most systems are replaceable.
6988 COMDAT functions are not replaceable, since all definitions of the
6989 function must be equivalent. It is important that COMDAT functions
6990 not be treated as replaceable so that use of C++ template
6991 instantiations is not penalized. */
6993 bool
6994 decl_replaceable_p (tree decl)
6996 gcc_assert (DECL_P (decl));
6997 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6998 return false;
6999 if (!flag_semantic_interposition
7000 && !DECL_WEAK (decl))
7001 return false;
7002 return !decl_binds_to_current_def_p (decl);
7005 /* Default function to output code that will globalize a label. A
7006 target must define GLOBAL_ASM_OP or provide its own function to
7007 globalize a label. */
7008 #ifdef GLOBAL_ASM_OP
7009 void
7010 default_globalize_label (FILE * stream, const char *name)
7012 fputs (GLOBAL_ASM_OP, stream);
7013 assemble_name (stream, name);
7014 putc ('\n', stream);
7016 #endif /* GLOBAL_ASM_OP */
7018 /* Default function to output code that will globalize a declaration. */
7019 void
7020 default_globalize_decl_name (FILE * stream, tree decl)
7022 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7023 targetm.asm_out.globalize_label (stream, name);
7026 /* Default function to output a label for unwind information. The
7027 default is to do nothing. A target that needs nonlocal labels for
7028 unwind information must provide its own function to do this. */
7029 void
7030 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7031 tree decl ATTRIBUTE_UNUSED,
7032 int for_eh ATTRIBUTE_UNUSED,
7033 int empty ATTRIBUTE_UNUSED)
7037 /* Default function to output a label to divide up the exception table.
7038 The default is to do nothing. A target that needs/wants to divide
7039 up the table must provide it's own function to do this. */
7040 void
7041 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7045 /* This is how to output an internal numbered label where PREFIX is
7046 the class of label and LABELNO is the number within the class. */
7048 void
7049 default_generate_internal_label (char *buf, const char *prefix,
7050 unsigned long labelno)
7052 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7055 /* This is how to output an internal numbered label where PREFIX is
7056 the class of label and LABELNO is the number within the class. */
7058 void
7059 default_internal_label (FILE *stream, const char *prefix,
7060 unsigned long labelno)
7062 char *const buf = (char *) alloca (40 + strlen (prefix));
7063 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7064 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7068 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7070 void
7071 default_asm_declare_constant_name (FILE *file, const char *name,
7072 const_tree exp ATTRIBUTE_UNUSED,
7073 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7075 assemble_label (file, name);
7078 /* This is the default behavior at the beginning of a file. It's
7079 controlled by two other target-hook toggles. */
7080 void
7081 default_file_start (void)
7083 if (targetm.asm_file_start_app_off
7084 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7085 fputs (ASM_APP_OFF, asm_out_file);
7087 if (targetm.asm_file_start_file_directive)
7089 /* LTO produced units have no meaningful main_input_filename. */
7090 if (in_lto_p)
7091 output_file_directive (asm_out_file, "<artificial>");
7092 else
7093 output_file_directive (asm_out_file, main_input_filename);
7097 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7098 which emits a special section directive used to indicate whether or
7099 not this object file needs an executable stack. This is primarily
7100 a GNU extension to ELF but could be used on other targets. */
7102 int trampolines_created;
7104 void
7105 file_end_indicate_exec_stack (void)
7107 unsigned int flags = SECTION_DEBUG;
7108 if (trampolines_created)
7109 flags |= SECTION_CODE;
7111 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7114 /* Emit a special section directive to indicate that this object file
7115 was compiled with -fsplit-stack. This is used to let the linker
7116 detect calls between split-stack code and non-split-stack code, so
7117 that it can modify the split-stack code to allocate a sufficiently
7118 large stack. We emit another special section if there are any
7119 functions in this file which have the no_split_stack attribute, to
7120 prevent the linker from warning about being unable to convert the
7121 functions if they call non-split-stack code. */
7123 void
7124 file_end_indicate_split_stack (void)
7126 if (flag_split_stack)
7128 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7129 NULL));
7130 if (saw_no_split_stack)
7131 switch_to_section (get_section (".note.GNU-no-split-stack",
7132 SECTION_DEBUG, NULL));
7136 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7137 a get_unnamed_section callback. */
7139 void
7140 output_section_asm_op (const void *directive)
7142 fprintf (asm_out_file, "%s\n", (const char *) directive);
7145 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7146 the current section is NEW_SECTION. */
7148 void
7149 switch_to_section (section *new_section)
7151 if (in_section == new_section)
7152 return;
7154 if (new_section->common.flags & SECTION_FORGET)
7155 in_section = NULL;
7156 else
7157 in_section = new_section;
7159 switch (SECTION_STYLE (new_section))
7161 case SECTION_NAMED:
7162 targetm.asm_out.named_section (new_section->named.name,
7163 new_section->named.common.flags,
7164 new_section->named.decl);
7165 break;
7167 case SECTION_UNNAMED:
7168 new_section->unnamed.callback (new_section->unnamed.data);
7169 break;
7171 case SECTION_NOSWITCH:
7172 gcc_unreachable ();
7173 break;
7176 new_section->common.flags |= SECTION_DECLARED;
7179 /* If block symbol SYMBOL has not yet been assigned an offset, place
7180 it at the end of its block. */
7182 void
7183 place_block_symbol (rtx symbol)
7185 unsigned HOST_WIDE_INT size, mask, offset;
7186 struct constant_descriptor_rtx *desc;
7187 unsigned int alignment;
7188 struct object_block *block;
7189 tree decl;
7191 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7192 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7193 return;
7195 /* Work out the symbol's size and alignment. */
7196 if (CONSTANT_POOL_ADDRESS_P (symbol))
7198 desc = SYMBOL_REF_CONSTANT (symbol);
7199 alignment = desc->align;
7200 size = GET_MODE_SIZE (desc->mode);
7202 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7204 decl = SYMBOL_REF_DECL (symbol);
7205 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7206 alignment = DECL_ALIGN (decl);
7207 size = get_constant_size (DECL_INITIAL (decl));
7208 if ((flag_sanitize & SANITIZE_ADDRESS)
7209 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7210 && asan_protect_global (DECL_INITIAL (decl)))
7211 size += asan_red_zone_size (size);
7213 else
7215 struct symtab_node *snode;
7216 decl = SYMBOL_REF_DECL (symbol);
7218 snode = symtab_node::get (decl);
7219 if (snode->alias)
7221 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7223 gcc_assert (MEM_P (target)
7224 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7225 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7226 target = XEXP (target, 0);
7227 place_block_symbol (target);
7228 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7229 return;
7231 alignment = get_variable_align (decl);
7232 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7233 if ((flag_sanitize & SANITIZE_ADDRESS)
7234 && asan_protect_global (decl))
7236 size += asan_red_zone_size (size);
7237 alignment = MAX (alignment,
7238 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7242 /* Calculate the object's offset from the start of the block. */
7243 block = SYMBOL_REF_BLOCK (symbol);
7244 mask = alignment / BITS_PER_UNIT - 1;
7245 offset = (block->size + mask) & ~mask;
7246 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7248 /* Record the block's new alignment and size. */
7249 block->alignment = MAX (block->alignment, alignment);
7250 block->size = offset + size;
7252 vec_safe_push (block->objects, symbol);
7255 /* Return the anchor that should be used to address byte offset OFFSET
7256 from the first object in BLOCK. MODEL is the TLS model used
7257 to access it. */
7260 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7261 enum tls_model model)
7263 char label[100];
7264 unsigned int begin, middle, end;
7265 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7266 rtx anchor;
7268 /* Work out the anchor's offset. Use an offset of 0 for the first
7269 anchor so that we don't pessimize the case where we take the address
7270 of a variable at the beginning of the block. This is particularly
7271 useful when a block has only one variable assigned to it.
7273 We try to place anchors RANGE bytes apart, so there can then be
7274 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7275 a ptr_mode offset. With some target settings, the lowest such
7276 anchor might be out of range for the lowest ptr_mode offset;
7277 likewise the highest anchor for the highest offset. Use anchors
7278 at the extreme ends of the ptr_mode range in such cases.
7280 All arithmetic uses unsigned integers in order to avoid
7281 signed overflow. */
7282 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7283 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7284 range = max_offset - min_offset + 1;
7285 if (range == 0)
7286 offset = 0;
7287 else
7289 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7290 if (offset < 0)
7292 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7293 delta -= delta % range;
7294 if (delta > bias)
7295 delta = bias;
7296 offset = (HOST_WIDE_INT) (-delta);
7298 else
7300 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7301 delta -= delta % range;
7302 if (delta > bias - 1)
7303 delta = bias - 1;
7304 offset = (HOST_WIDE_INT) delta;
7308 /* Do a binary search to see if there's already an anchor we can use.
7309 Set BEGIN to the new anchor's index if not. */
7310 begin = 0;
7311 end = vec_safe_length (block->anchors);
7312 while (begin != end)
7314 middle = (end + begin) / 2;
7315 anchor = (*block->anchors)[middle];
7316 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7317 end = middle;
7318 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7319 begin = middle + 1;
7320 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7321 end = middle;
7322 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7323 begin = middle + 1;
7324 else
7325 return anchor;
7328 /* Create a new anchor with a unique label. */
7329 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7330 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7331 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7332 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7334 /* Insert it at index BEGIN. */
7335 vec_safe_insert (block->anchors, begin, anchor);
7336 return anchor;
7339 /* Output the objects in BLOCK. */
7341 static void
7342 output_object_block (struct object_block *block)
7344 struct constant_descriptor_rtx *desc;
7345 unsigned int i;
7346 HOST_WIDE_INT offset;
7347 tree decl;
7348 rtx symbol;
7350 if (!block->objects)
7351 return;
7353 /* Switch to the section and make sure that the first byte is
7354 suitably aligned. */
7355 switch_to_section (block->sect);
7356 assemble_align (block->alignment);
7358 /* Define the values of all anchors relative to the current section
7359 position. */
7360 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7361 targetm.asm_out.output_anchor (symbol);
7363 /* Output the objects themselves. */
7364 offset = 0;
7365 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7367 /* Move to the object's offset, padding with zeros if necessary. */
7368 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7369 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7370 if (CONSTANT_POOL_ADDRESS_P (symbol))
7372 desc = SYMBOL_REF_CONSTANT (symbol);
7373 /* Pass 1 for align as we have already laid out everything in the block.
7374 So aligning shouldn't be necessary. */
7375 output_constant_pool_1 (desc, 1);
7376 offset += GET_MODE_SIZE (desc->mode);
7378 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7380 HOST_WIDE_INT size;
7381 decl = SYMBOL_REF_DECL (symbol);
7382 assemble_constant_contents
7383 (DECL_INITIAL (decl), XSTR (symbol, 0), DECL_ALIGN (decl));
7385 size = get_constant_size (DECL_INITIAL (decl));
7386 offset += size;
7387 if ((flag_sanitize & SANITIZE_ADDRESS)
7388 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7389 && asan_protect_global (DECL_INITIAL (decl)))
7391 size = asan_red_zone_size (size);
7392 assemble_zeros (size);
7393 offset += size;
7396 else
7398 HOST_WIDE_INT size;
7399 decl = SYMBOL_REF_DECL (symbol);
7400 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7401 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7402 offset += size;
7403 if ((flag_sanitize & SANITIZE_ADDRESS)
7404 && asan_protect_global (decl))
7406 size = asan_red_zone_size (size);
7407 assemble_zeros (size);
7408 offset += size;
7414 /* A callback for qsort to compare object_blocks. */
7416 static int
7417 output_object_block_compare (const void *x, const void *y)
7419 object_block *p1 = *(object_block * const*)x;
7420 object_block *p2 = *(object_block * const*)y;
7422 if (p1->sect->common.flags & SECTION_NAMED
7423 && !(p2->sect->common.flags & SECTION_NAMED))
7424 return 1;
7426 if (!(p1->sect->common.flags & SECTION_NAMED)
7427 && p2->sect->common.flags & SECTION_NAMED)
7428 return -1;
7430 if (p1->sect->common.flags & SECTION_NAMED
7431 && p2->sect->common.flags & SECTION_NAMED)
7432 return strcmp (p1->sect->named.name, p2->sect->named.name);
7434 unsigned f1 = p1->sect->common.flags;
7435 unsigned f2 = p2->sect->common.flags;
7436 if (f1 == f2)
7437 return 0;
7438 return f1 < f2 ? -1 : 1;
7441 /* Output the definitions of all object_blocks. */
7443 void
7444 output_object_blocks (void)
7446 vec<object_block *, va_heap> v;
7447 v.create (object_block_htab->elements ());
7448 object_block *obj;
7449 hash_table<object_block_hasher>::iterator hi;
7451 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
7452 v.quick_push (obj);
7454 /* Sort them in order to output them in a deterministic manner,
7455 otherwise we may get .rodata sections in different orders with
7456 and without -g. */
7457 v.qsort (output_object_block_compare);
7458 unsigned i;
7459 FOR_EACH_VEC_ELT (v, i, obj)
7460 output_object_block (obj);
7462 v.release ();
7465 /* This function provides a possible implementation of the
7466 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7467 by -frecord-gcc-switches it creates a new mergeable, string section in the
7468 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7469 contains the switches in ASCII format.
7471 FIXME: This code does not correctly handle double quote characters
7472 that appear inside strings, (it strips them rather than preserving them).
7473 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7474 characters - instead it treats them as sub-string separators. Since
7475 we want to emit NUL strings terminators into the object file we have to use
7476 ASM_OUTPUT_SKIP. */
7479 elf_record_gcc_switches (print_switch_type type, const char * name)
7481 switch (type)
7483 case SWITCH_TYPE_PASSED:
7484 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7485 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7486 break;
7488 case SWITCH_TYPE_DESCRIPTIVE:
7489 if (name == NULL)
7491 /* Distinguish between invocations where name is NULL. */
7492 static bool started = false;
7494 if (!started)
7496 section * sec;
7498 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7499 SECTION_DEBUG
7500 | SECTION_MERGE
7501 | SECTION_STRINGS
7502 | (SECTION_ENTSIZE & 1),
7503 NULL);
7504 switch_to_section (sec);
7505 started = true;
7509 default:
7510 break;
7513 /* The return value is currently ignored by the caller, but must be 0.
7514 For -fverbose-asm the return value would be the number of characters
7515 emitted into the assembler file. */
7516 return 0;
7519 /* Emit text to declare externally defined symbols. It is needed to
7520 properly support non-default visibility. */
7521 void
7522 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7523 tree decl,
7524 const char *name ATTRIBUTE_UNUSED)
7526 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7527 set in order to avoid putting out names that are never really
7528 used. Always output visibility specified in the source. */
7529 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7530 && (DECL_VISIBILITY_SPECIFIED (decl)
7531 || targetm.binds_local_p (decl)))
7532 maybe_assemble_visibility (decl);
7535 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7537 void
7538 default_asm_output_source_filename (FILE *file, const char *name)
7540 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7541 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7542 #else
7543 fprintf (file, "\t.file\t");
7544 output_quoted_string (file, name);
7545 putc ('\n', file);
7546 #endif
7549 /* Output a file name in the form wanted by System V. */
7551 void
7552 output_file_directive (FILE *asm_file, const char *input_name)
7554 int len;
7555 const char *na;
7557 if (input_name == NULL)
7558 input_name = "<stdin>";
7559 else
7560 input_name = remap_debug_filename (input_name);
7562 len = strlen (input_name);
7563 na = input_name + len;
7565 /* NA gets INPUT_NAME sans directory names. */
7566 while (na > input_name)
7568 if (IS_DIR_SEPARATOR (na[-1]))
7569 break;
7570 na--;
7573 targetm.asm_out.output_source_filename (asm_file, na);
7576 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7577 EXP. */
7579 make_debug_expr_from_rtl (const_rtx exp)
7581 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7582 machine_mode mode = GET_MODE (exp);
7583 rtx dval;
7585 DECL_ARTIFICIAL (ddecl) = 1;
7586 if (REG_P (exp) && REG_EXPR (exp))
7587 type = TREE_TYPE (REG_EXPR (exp));
7588 else if (MEM_P (exp) && MEM_EXPR (exp))
7589 type = TREE_TYPE (MEM_EXPR (exp));
7590 else
7591 type = NULL_TREE;
7592 if (type && TYPE_MODE (type) == mode)
7593 TREE_TYPE (ddecl) = type;
7594 else
7595 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7596 DECL_MODE (ddecl) = mode;
7597 dval = gen_rtx_DEBUG_EXPR (mode);
7598 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7599 SET_DECL_RTL (ddecl, dval);
7600 return dval;
7603 #ifdef ELF_ASCII_ESCAPES
7604 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7606 void
7607 default_elf_asm_output_limited_string (FILE *f, const char *s)
7609 int escape;
7610 unsigned char c;
7612 fputs (STRING_ASM_OP, f);
7613 putc ('"', f);
7614 while (*s != '\0')
7616 c = *s;
7617 escape = ELF_ASCII_ESCAPES[c];
7618 switch (escape)
7620 case 0:
7621 putc (c, f);
7622 break;
7623 case 1:
7624 /* TODO: Print in hex with fast function, important for -flto. */
7625 fprintf (f, "\\%03o", c);
7626 break;
7627 default:
7628 putc ('\\', f);
7629 putc (escape, f);
7630 break;
7632 s++;
7634 putc ('\"', f);
7635 putc ('\n', f);
7638 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7640 void
7641 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7643 const char *limit = s + len;
7644 const char *last_null = NULL;
7645 unsigned bytes_in_chunk = 0;
7646 unsigned char c;
7647 int escape;
7649 for (; s < limit; s++)
7651 const char *p;
7653 if (bytes_in_chunk >= 60)
7655 putc ('\"', f);
7656 putc ('\n', f);
7657 bytes_in_chunk = 0;
7660 if (s > last_null)
7662 for (p = s; p < limit && *p != '\0'; p++)
7663 continue;
7664 last_null = p;
7666 else
7667 p = last_null;
7669 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7671 if (bytes_in_chunk > 0)
7673 putc ('\"', f);
7674 putc ('\n', f);
7675 bytes_in_chunk = 0;
7678 default_elf_asm_output_limited_string (f, s);
7679 s = p;
7681 else
7683 if (bytes_in_chunk == 0)
7684 fputs (ASCII_DATA_ASM_OP "\"", f);
7686 c = *s;
7687 escape = ELF_ASCII_ESCAPES[c];
7688 switch (escape)
7690 case 0:
7691 putc (c, f);
7692 bytes_in_chunk++;
7693 break;
7694 case 1:
7695 /* TODO: Print in hex with fast function, important for -flto. */
7696 fprintf (f, "\\%03o", c);
7697 bytes_in_chunk += 4;
7698 break;
7699 default:
7700 putc ('\\', f);
7701 putc (escape, f);
7702 bytes_in_chunk += 2;
7703 break;
7709 if (bytes_in_chunk > 0)
7711 putc ('\"', f);
7712 putc ('\n', f);
7715 #endif
7717 static GTY(()) section *elf_init_array_section;
7718 static GTY(()) section *elf_fini_array_section;
7720 static section *
7721 get_elf_initfini_array_priority_section (int priority,
7722 bool constructor_p)
7724 section *sec;
7725 if (priority != DEFAULT_INIT_PRIORITY)
7727 char buf[18];
7728 sprintf (buf, "%s.%.5u",
7729 constructor_p ? ".init_array" : ".fini_array",
7730 priority);
7731 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7733 else
7735 if (constructor_p)
7737 if (elf_init_array_section == NULL)
7738 elf_init_array_section
7739 = get_section (".init_array",
7740 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7741 sec = elf_init_array_section;
7743 else
7745 if (elf_fini_array_section == NULL)
7746 elf_fini_array_section
7747 = get_section (".fini_array",
7748 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7749 sec = elf_fini_array_section;
7752 return sec;
7755 /* Use .init_array section for constructors. */
7757 void
7758 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7760 section *sec = get_elf_initfini_array_priority_section (priority,
7761 true);
7762 assemble_addr_to_section (symbol, sec);
7765 /* Use .fini_array section for destructors. */
7767 void
7768 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7770 section *sec = get_elf_initfini_array_priority_section (priority,
7771 false);
7772 assemble_addr_to_section (symbol, sec);
7775 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7777 This is a bit of a cheat. The real default is a no-op, but this
7778 hook is the default for all targets with a .ident directive. */
7780 void
7781 default_asm_output_ident_directive (const char *ident_str)
7783 const char *ident_asm_op = "\t.ident\t";
7785 /* If we are still in the front end, do not write out the string
7786 to asm_out_file. Instead, add a fake top-level asm statement.
7787 This allows the front ends to use this hook without actually
7788 writing to asm_out_file, to handle #ident or Pragma Ident. */
7789 if (symtab->state == PARSING)
7791 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7792 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7794 else
7795 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7798 #include "gt-varasm.h"