libsanitizer merge from upstream r250806, compiler part.
[official-gcc.git] / gcc / varasm.c
blob9cb7ac1e76331d83e248cfcf90428a370c9b5973
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 "backend.h"
32 #include "predict.h"
33 #include "rtl.h"
34 #include "alias.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 "insn-config.h"
42 #include "expmed.h"
43 #include "dojump.h"
44 #include "explow.h"
45 #include "calls.h"
46 #include "emit-rtl.h"
47 #include "stmt.h"
48 #include "expr.h"
49 #include "regs.h"
50 #include "output.h"
51 #include "diagnostic-core.h"
52 #include "langhooks.h"
53 #include "tm_p.h"
54 #include "debug.h"
55 #include "target.h"
56 #include "common/common-target.h"
57 #include "targhooks.h"
58 #include "cgraph.h"
59 #include "asan.h"
60 #include "rtl-iter.h"
61 #include "tree-chkp.h"
63 #ifdef XCOFF_DEBUGGING_INFO
64 #include "xcoffout.h" /* Needed for external data
65 declarations for e.g. AIX 4.x. */
66 #endif
68 /* The (assembler) name of the first globally-visible object output. */
69 extern GTY(()) const char *first_global_object_name;
70 extern GTY(()) const char *weak_global_object_name;
72 const char *first_global_object_name;
73 const char *weak_global_object_name;
75 struct addr_const;
76 struct constant_descriptor_rtx;
77 struct rtx_constant_pool;
79 #define n_deferred_constants (crtl->varasm.deferred_constants)
81 /* Number for making the label on the next
82 constant that is stored in memory. */
84 static GTY(()) int const_labelno;
86 /* Carry information from ASM_DECLARE_OBJECT_NAME
87 to ASM_FINISH_DECLARE_OBJECT. */
89 int size_directive_output;
91 /* The last decl for which assemble_variable was called,
92 if it did ASM_DECLARE_OBJECT_NAME.
93 If the last call to assemble_variable didn't do that,
94 this holds 0. */
96 tree last_assemble_variable_decl;
98 /* The following global variable indicates if the first basic block
99 in a function belongs to the cold partition or not. */
101 bool first_function_block_is_cold;
103 /* Whether we saw any functions with no_split_stack. */
105 static bool saw_no_split_stack;
107 static const char *strip_reg_name (const char *);
108 static int contains_pointers_p (tree);
109 #ifdef ASM_OUTPUT_EXTERNAL
110 static bool incorporeal_function_p (tree);
111 #endif
112 static void decode_addr_const (tree, struct addr_const *);
113 static hashval_t const_hash_1 (const tree);
114 static int compare_constant (const tree, const tree);
115 static void output_constant_def_contents (rtx);
116 static void output_addressed_constants (tree);
117 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
118 unsigned int);
119 static void globalize_decl (tree);
120 static bool decl_readonly_section_1 (enum section_category);
121 #ifdef BSS_SECTION_ASM_OP
122 #ifdef ASM_OUTPUT_ALIGNED_BSS
123 static void asm_output_aligned_bss (FILE *, tree, const char *,
124 unsigned HOST_WIDE_INT, int)
125 ATTRIBUTE_UNUSED;
126 #endif
127 #endif /* BSS_SECTION_ASM_OP */
128 static void mark_weak (tree);
129 static void output_constant_pool (const char *, tree);
130 static void handle_vtv_comdat_section (section *, const_tree);
132 /* Well-known sections, each one associated with some sort of *_ASM_OP. */
133 section *text_section;
134 section *data_section;
135 section *readonly_data_section;
136 section *sdata_section;
137 section *ctors_section;
138 section *dtors_section;
139 section *bss_section;
140 section *sbss_section;
142 /* Various forms of common section. All are guaranteed to be nonnull. */
143 section *tls_comm_section;
144 section *comm_section;
145 section *lcomm_section;
147 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
148 May be null. */
149 section *bss_noswitch_section;
151 /* The section that holds the main exception table, when known. The section
152 is set either by the target's init_sections hook or by the first call to
153 switch_to_exception_section. */
154 section *exception_section;
156 /* The section that holds the DWARF2 frame unwind information, when known.
157 The section is set either by the target's init_sections hook or by the
158 first call to switch_to_eh_frame_section. */
159 section *eh_frame_section;
161 /* asm_out_file's current section. This is NULL if no section has yet
162 been selected or if we lose track of what the current section is. */
163 section *in_section;
165 /* True if code for the current function is currently being directed
166 at the cold section. */
167 bool in_cold_section_p;
169 /* The following global holds the "function name" for the code in the
170 cold section of a function, if hot/cold function splitting is enabled
171 and there was actually code that went into the cold section. A
172 pseudo function name is needed for the cold section of code for some
173 debugging tools that perform symbolization. */
174 tree cold_function_name = NULL_TREE;
176 /* A linked list of all the unnamed sections. */
177 static GTY(()) section *unnamed_sections;
179 /* Return a nonzero value if DECL has a section attribute. */
180 #define IN_NAMED_SECTION(DECL) \
181 ((TREE_CODE (DECL) == FUNCTION_DECL || TREE_CODE (DECL) == VAR_DECL) \
182 && DECL_SECTION_NAME (DECL) != NULL)
184 struct section_hasher : ggc_ptr_hash<section>
186 typedef const char *compare_type;
188 static hashval_t hash (section *);
189 static bool equal (section *, const char *);
192 /* Hash table of named sections. */
193 static GTY(()) hash_table<section_hasher> *section_htab;
195 struct object_block_hasher : ggc_ptr_hash<object_block>
197 typedef const section *compare_type;
199 static hashval_t hash (object_block *);
200 static bool equal (object_block *, const section *);
203 /* A table of object_blocks, indexed by section. */
204 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
206 /* The next number to use for internal anchor labels. */
207 static GTY(()) int anchor_labelno;
209 /* A pool of constants that can be shared between functions. */
210 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
212 /* Helper routines for maintaining section_htab. */
214 bool
215 section_hasher::equal (section *old, const char *new_name)
217 return strcmp (old->named.name, new_name) == 0;
220 hashval_t
221 section_hasher::hash (section *old)
223 return htab_hash_string (old->named.name);
226 /* Return a hash value for section SECT. */
228 static hashval_t
229 hash_section (section *sect)
231 if (sect->common.flags & SECTION_NAMED)
232 return htab_hash_string (sect->named.name);
233 return sect->common.flags;
236 /* Helper routines for maintaining object_block_htab. */
238 inline bool
239 object_block_hasher::equal (object_block *old, const section *new_section)
241 return old->sect == new_section;
244 hashval_t
245 object_block_hasher::hash (object_block *old)
247 return hash_section (old->sect);
250 /* Return a new unnamed section with the given fields. */
252 section *
253 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
254 const void *data)
256 section *sect;
258 sect = ggc_alloc<section> ();
259 sect->unnamed.common.flags = flags | SECTION_UNNAMED;
260 sect->unnamed.callback = callback;
261 sect->unnamed.data = data;
262 sect->unnamed.next = unnamed_sections;
264 unnamed_sections = sect;
265 return sect;
268 /* Return a SECTION_NOSWITCH section with the given fields. */
270 static section *
271 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
273 section *sect;
275 sect = ggc_alloc<section> ();
276 sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
277 sect->noswitch.callback = callback;
279 return sect;
282 /* Return the named section structure associated with NAME. Create
283 a new section with the given fields if no such structure exists. */
285 section *
286 get_section (const char *name, unsigned int flags, tree decl)
288 section *sect, **slot;
290 slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
291 INSERT);
292 flags |= SECTION_NAMED;
293 if (*slot == NULL)
295 sect = ggc_alloc<section> ();
296 sect->named.common.flags = flags;
297 sect->named.name = ggc_strdup (name);
298 sect->named.decl = decl;
299 *slot = sect;
301 else
303 sect = *slot;
304 if ((sect->common.flags & ~SECTION_DECLARED) != flags
305 && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
307 /* It is fine if one of the section flags is
308 SECTION_WRITE | SECTION_RELRO and the other has none of these
309 flags (i.e. read-only) in named sections and either the
310 section hasn't been declared yet or has been declared as writable.
311 In that case just make sure the resulting flags are
312 SECTION_WRITE | SECTION_RELRO, ie. writable only because of
313 relocations. */
314 if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
315 == (SECTION_WRITE | SECTION_RELRO)
316 && (sect->common.flags
317 & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
318 == (flags & ~(SECTION_WRITE | SECTION_RELRO))
319 && ((sect->common.flags & SECTION_DECLARED) == 0
320 || (sect->common.flags & SECTION_WRITE)))
322 sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
323 return sect;
325 /* Sanity check user variables for flag changes. */
326 if (sect->named.decl != NULL
327 && DECL_P (sect->named.decl)
328 && decl != sect->named.decl)
330 if (decl != NULL && DECL_P (decl))
331 error ("%+D causes a section type conflict with %D",
332 decl, sect->named.decl);
333 else
334 error ("section type conflict with %D", sect->named.decl);
335 inform (DECL_SOURCE_LOCATION (sect->named.decl),
336 "%qD was declared here", sect->named.decl);
338 else if (decl != NULL && DECL_P (decl))
339 error ("%+D causes a section type conflict", decl);
340 else
341 error ("section type conflict");
342 /* Make sure we don't error about one section multiple times. */
343 sect->common.flags |= SECTION_OVERRIDE;
346 return sect;
349 /* Return true if the current compilation mode benefits from having
350 objects grouped into blocks. */
352 static bool
353 use_object_blocks_p (void)
355 return flag_section_anchors;
358 /* Return the object_block structure for section SECT. Create a new
359 structure if we haven't created one already. Return null if SECT
360 itself is null. */
362 static struct object_block *
363 get_block_for_section (section *sect)
365 struct object_block *block;
367 if (sect == NULL)
368 return NULL;
370 object_block **slot
371 = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
372 INSERT);
373 block = *slot;
374 if (block == NULL)
376 block = ggc_cleared_alloc<object_block> ();
377 block->sect = sect;
378 *slot = block;
380 return block;
383 /* Create a symbol with label LABEL and place it at byte offset
384 OFFSET in BLOCK. OFFSET can be negative if the symbol's offset
385 is not yet known. LABEL must be a garbage-collected string. */
387 static rtx
388 create_block_symbol (const char *label, struct object_block *block,
389 HOST_WIDE_INT offset)
391 rtx symbol;
392 unsigned int size;
394 /* Create the extended SYMBOL_REF. */
395 size = RTX_HDR_SIZE + sizeof (struct block_symbol);
396 symbol = (rtx) ggc_internal_alloc (size);
398 /* Initialize the normal SYMBOL_REF fields. */
399 memset (symbol, 0, size);
400 PUT_CODE (symbol, SYMBOL_REF);
401 PUT_MODE (symbol, Pmode);
402 XSTR (symbol, 0) = label;
403 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
405 /* Initialize the block_symbol stuff. */
406 SYMBOL_REF_BLOCK (symbol) = block;
407 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
409 return symbol;
412 /* Return a section with a particular name and with whatever SECTION_*
413 flags section_type_flags deems appropriate. The name of the section
414 is taken from NAME if nonnull, otherwise it is taken from DECL's
415 DECL_SECTION_NAME. DECL is the decl associated with the section
416 (see the section comment for details) and RELOC is as for
417 section_type_flags. */
419 section *
420 get_named_section (tree decl, const char *name, int reloc)
422 unsigned int flags;
424 if (name == NULL)
426 gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
427 name = DECL_SECTION_NAME (decl);
430 flags = targetm.section_type_flags (decl, name, reloc);
431 return get_section (name, flags, decl);
434 /* Worker for resolve_unique_section. */
436 static bool
437 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
439 n->implicit_section = true;
440 return false;
443 /* If required, set DECL_SECTION_NAME to a unique name. */
445 void
446 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
447 int flag_function_or_data_sections)
449 if (DECL_SECTION_NAME (decl) == NULL
450 && targetm_common.have_named_sections
451 && (flag_function_or_data_sections
452 || DECL_COMDAT_GROUP (decl)))
454 targetm.asm_out.unique_section (decl, reloc);
455 if (DECL_SECTION_NAME (decl))
456 symtab_node::get (decl)->call_for_symbol_and_aliases
457 (set_implicit_section, NULL, true);
461 #ifdef BSS_SECTION_ASM_OP
463 #ifdef ASM_OUTPUT_ALIGNED_BSS
465 /* Utility function for targets to use in implementing
466 ASM_OUTPUT_ALIGNED_BSS.
467 ??? It is believed that this function will work in most cases so such
468 support is localized here. */
470 static void
471 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
472 const char *name, unsigned HOST_WIDE_INT size,
473 int align)
475 switch_to_section (bss_section);
476 ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
477 #ifdef ASM_DECLARE_OBJECT_NAME
478 last_assemble_variable_decl = decl;
479 ASM_DECLARE_OBJECT_NAME (file, name, decl);
480 #else
481 /* Standard thing is just output label for the object. */
482 ASM_OUTPUT_LABEL (file, name);
483 #endif /* ASM_DECLARE_OBJECT_NAME */
484 ASM_OUTPUT_SKIP (file, size ? size : 1);
487 #endif
489 #endif /* BSS_SECTION_ASM_OP */
491 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
492 /* Return the hot section for function DECL. Return text_section for
493 null DECLs. */
495 static section *
496 hot_function_section (tree decl)
498 if (decl != NULL_TREE
499 && DECL_SECTION_NAME (decl) != NULL
500 && targetm_common.have_named_sections)
501 return get_named_section (decl, NULL, 0);
502 else
503 return text_section;
505 #endif
507 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
508 is NULL.
510 When DECL_SECTION_NAME is non-NULL and it is implicit section and
511 NAMED_SECTION_SUFFIX is non-NULL, then produce section called
512 concatenate the name with NAMED_SECTION_SUFFIX.
513 Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME". */
515 section *
516 get_named_text_section (tree decl,
517 const char *text_section_name,
518 const char *named_section_suffix)
520 if (decl && DECL_SECTION_NAME (decl))
522 if (named_section_suffix)
524 const char *dsn = DECL_SECTION_NAME (decl);
525 const char *stripped_name;
526 char *name, *buffer;
528 name = (char *) alloca (strlen (dsn) + 1);
529 memcpy (name, dsn,
530 strlen (dsn) + 1);
532 stripped_name = targetm.strip_name_encoding (name);
534 buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
535 return get_named_section (decl, buffer, 0);
537 else if (symtab_node::get (decl)->implicit_section)
539 const char *name;
541 /* Do not try to split gnu_linkonce functions. This gets somewhat
542 slipperly. */
543 if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
544 return NULL;
545 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
546 name = targetm.strip_name_encoding (name);
547 return get_named_section (decl, ACONCAT ((text_section_name, ".",
548 name, NULL)), 0);
550 else
551 return NULL;
553 return get_named_section (decl, text_section_name, 0);
556 /* Choose named function section based on its frequency. */
558 section *
559 default_function_section (tree decl, enum node_frequency freq,
560 bool startup, bool exit)
562 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
563 /* Old GNU linkers have buggy --gc-section support, which sometimes
564 results in .gcc_except_table* sections being garbage collected. */
565 if (decl
566 && symtab_node::get (decl)->implicit_section)
567 return NULL;
568 #endif
570 if (!flag_reorder_functions
571 || !targetm_common.have_named_sections)
572 return NULL;
573 /* Startup code should go to startup subsection unless it is
574 unlikely executed (this happens especially with function splitting
575 where we can split away unnecessary parts of static constructors. */
576 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
578 /* If we do have a profile or(and) LTO phase is executed, we do not need
579 these ELF section. */
580 if (!in_lto_p || !flag_profile_values)
581 return get_named_text_section (decl, ".text.startup", NULL);
582 else
583 return NULL;
586 /* Similarly for exit. */
587 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
588 return get_named_text_section (decl, ".text.exit", NULL);
590 /* Group cold functions together, similarly for hot code. */
591 switch (freq)
593 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
594 return get_named_text_section (decl, ".text.unlikely", NULL);
595 case NODE_FREQUENCY_HOT:
596 /* If we do have a profile or(and) LTO phase is executed, we do not need
597 these ELF section. */
598 if (!in_lto_p || !flag_profile_values)
599 return get_named_text_section (decl, ".text.hot", NULL);
600 default:
601 return NULL;
605 /* Return the section for function DECL.
607 If DECL is NULL_TREE, return the text section. We can be passed
608 NULL_TREE under some circumstances by dbxout.c at least.
610 If FORCE_COLD is true, return cold function section ignoring
611 the frequency info of cgraph_node. */
613 static section *
614 function_section_1 (tree decl, bool force_cold)
616 section *section = NULL;
617 enum node_frequency freq = NODE_FREQUENCY_NORMAL;
618 bool startup = false, exit = false;
620 if (decl)
622 struct cgraph_node *node = cgraph_node::get (decl);
624 if (node)
626 freq = node->frequency;
627 startup = node->only_called_at_startup;
628 exit = node->only_called_at_exit;
631 if (force_cold)
632 freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
634 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
635 if (decl != NULL_TREE
636 && DECL_SECTION_NAME (decl) != NULL)
638 if (targetm.asm_out.function_section)
639 section = targetm.asm_out.function_section (decl, freq,
640 startup, exit);
641 if (section)
642 return section;
643 return get_named_section (decl, NULL, 0);
645 else
646 return targetm.asm_out.select_section
647 (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
648 symtab_node::get (decl)->definition_alignment ());
649 #else
650 if (targetm.asm_out.function_section)
651 section = targetm.asm_out.function_section (decl, freq, startup, exit);
652 if (section)
653 return section;
654 return hot_function_section (decl);
655 #endif
658 /* Return the section for function DECL.
660 If DECL is NULL_TREE, return the text section. We can be passed
661 NULL_TREE under some circumstances by dbxout.c at least. */
663 section *
664 function_section (tree decl)
666 /* Handle cases where function splitting code decides
667 to put function entry point into unlikely executed section
668 despite the fact that the function itself is not cold
669 (i.e. it is called rarely but contains a hot loop that is
670 better to live in hot subsection for the code locality). */
671 return function_section_1 (decl,
672 first_function_block_is_cold);
675 /* Return the section for the current function, take IN_COLD_SECTION_P
676 into account. */
678 section *
679 current_function_section (void)
681 return function_section_1 (current_function_decl, in_cold_section_p);
684 /* Tell assembler to switch to unlikely-to-be-executed text section. */
686 section *
687 unlikely_text_section (void)
689 return function_section_1 (current_function_decl, true);
692 /* When called within a function context, return true if the function
693 has been assigned a cold text section and if SECT is that section.
694 When called outside a function context, return true if SECT is the
695 default cold section. */
697 bool
698 unlikely_text_section_p (section *sect)
700 return sect == function_section_1 (current_function_decl, true);
703 /* Return the read-only data section associated with function DECL. */
705 section *
706 default_function_rodata_section (tree decl)
708 if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
710 const char *name = DECL_SECTION_NAME (decl);
712 if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
714 const char *dot;
715 size_t len;
716 char* rname;
718 dot = strchr (name + 1, '.');
719 if (!dot)
720 dot = name;
721 len = strlen (dot) + 8;
722 rname = (char *) alloca (len);
724 strcpy (rname, ".rodata");
725 strcat (rname, dot);
726 return get_section (rname, SECTION_LINKONCE, decl);
728 /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo. */
729 else if (DECL_COMDAT_GROUP (decl)
730 && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
732 size_t len = strlen (name) + 1;
733 char *rname = (char *) alloca (len);
735 memcpy (rname, name, len);
736 rname[14] = 'r';
737 return get_section (rname, SECTION_LINKONCE, decl);
739 /* For .text.foo we want to use .rodata.foo. */
740 else if (flag_function_sections && flag_data_sections
741 && strncmp (name, ".text.", 6) == 0)
743 size_t len = strlen (name) + 1;
744 char *rname = (char *) alloca (len + 2);
746 memcpy (rname, ".rodata", 7);
747 memcpy (rname + 7, name + 5, len - 5);
748 return get_section (rname, 0, decl);
752 return readonly_data_section;
755 /* Return the read-only data section associated with function DECL
756 for targets where that section should be always the single
757 readonly data section. */
759 section *
760 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
762 return readonly_data_section;
765 /* A subroutine of mergeable_string_section and mergeable_constant_section. */
767 static const char *
768 function_mergeable_rodata_prefix (void)
770 section *s = targetm.asm_out.function_rodata_section (current_function_decl);
771 if (SECTION_STYLE (s) == SECTION_NAMED)
772 return s->named.name;
773 else
774 return targetm.asm_out.mergeable_rodata_prefix;
777 /* Return the section to use for string merging. */
779 static section *
780 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
781 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
782 unsigned int flags ATTRIBUTE_UNUSED)
784 HOST_WIDE_INT len;
786 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
787 && TREE_CODE (decl) == STRING_CST
788 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
789 && align <= 256
790 && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
791 && TREE_STRING_LENGTH (decl) >= len)
793 machine_mode mode;
794 unsigned int modesize;
795 const char *str;
796 HOST_WIDE_INT i;
797 int j, unit;
798 const char *prefix = function_mergeable_rodata_prefix ();
799 char *name = (char *) alloca (strlen (prefix) + 30);
801 mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
802 modesize = GET_MODE_BITSIZE (mode);
803 if (modesize >= 8 && modesize <= 256
804 && (modesize & (modesize - 1)) == 0)
806 if (align < modesize)
807 align = modesize;
809 str = TREE_STRING_POINTER (decl);
810 unit = GET_MODE_SIZE (mode);
812 /* Check for embedded NUL characters. */
813 for (i = 0; i < len; i += unit)
815 for (j = 0; j < unit; j++)
816 if (str[i + j] != '\0')
817 break;
818 if (j == unit)
819 break;
821 if (i == len - unit)
823 sprintf (name, "%s.str%d.%d", prefix,
824 modesize / 8, (int) (align / 8));
825 flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
826 return get_section (name, flags, NULL);
831 return readonly_data_section;
834 /* Return the section to use for constant merging. */
836 section *
837 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
838 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
839 unsigned int flags ATTRIBUTE_UNUSED)
841 unsigned int modesize = GET_MODE_BITSIZE (mode);
843 if (HAVE_GAS_SHF_MERGE && flag_merge_constants
844 && mode != VOIDmode
845 && mode != BLKmode
846 && modesize <= align
847 && align >= 8
848 && align <= 256
849 && (align & (align - 1)) == 0)
851 const char *prefix = function_mergeable_rodata_prefix ();
852 char *name = (char *) alloca (strlen (prefix) + 30);
854 sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
855 flags |= (align / 8) | SECTION_MERGE;
856 return get_section (name, flags, NULL);
858 return readonly_data_section;
861 /* Given NAME, a putative register name, discard any customary prefixes. */
863 static const char *
864 strip_reg_name (const char *name)
866 #ifdef REGISTER_PREFIX
867 if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
868 name += strlen (REGISTER_PREFIX);
869 #endif
870 if (name[0] == '%' || name[0] == '#')
871 name++;
872 return name;
875 /* The user has asked for a DECL to have a particular name. Set (or
876 change) it in such a way that we don't prefix an underscore to
877 it. */
878 void
879 set_user_assembler_name (tree decl, const char *name)
881 char *starred = (char *) alloca (strlen (name) + 2);
882 starred[0] = '*';
883 strcpy (starred + 1, name);
884 symtab->change_decl_assembler_name (decl, get_identifier (starred));
885 SET_DECL_RTL (decl, NULL_RTX);
888 /* Decode an `asm' spec for a declaration as a register name.
889 Return the register number, or -1 if nothing specified,
890 or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
891 or -3 if ASMSPEC is `cc' and is not recognized,
892 or -4 if ASMSPEC is `memory' and is not recognized.
893 Accept an exact spelling or a decimal number.
894 Prefixes such as % are optional. */
897 decode_reg_name_and_count (const char *asmspec, int *pnregs)
899 /* Presume just one register is clobbered. */
900 *pnregs = 1;
902 if (asmspec != 0)
904 int i;
906 /* Get rid of confusing prefixes. */
907 asmspec = strip_reg_name (asmspec);
909 /* Allow a decimal number as a "register name". */
910 for (i = strlen (asmspec) - 1; i >= 0; i--)
911 if (! ISDIGIT (asmspec[i]))
912 break;
913 if (asmspec[0] != 0 && i < 0)
915 i = atoi (asmspec);
916 if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
917 return i;
918 else
919 return -2;
922 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
923 if (reg_names[i][0]
924 && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
925 return i;
927 #ifdef OVERLAPPING_REGISTER_NAMES
929 static const struct
931 const char *const name;
932 const int number;
933 const int nregs;
934 } table[] = OVERLAPPING_REGISTER_NAMES;
936 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
937 if (table[i].name[0]
938 && ! strcmp (asmspec, table[i].name))
940 *pnregs = table[i].nregs;
941 return table[i].number;
944 #endif /* OVERLAPPING_REGISTER_NAMES */
946 #ifdef ADDITIONAL_REGISTER_NAMES
948 static const struct { const char *const name; const int number; } table[]
949 = ADDITIONAL_REGISTER_NAMES;
951 for (i = 0; i < (int) ARRAY_SIZE (table); i++)
952 if (table[i].name[0]
953 && ! strcmp (asmspec, table[i].name)
954 && reg_names[table[i].number][0])
955 return table[i].number;
957 #endif /* ADDITIONAL_REGISTER_NAMES */
959 if (!strcmp (asmspec, "memory"))
960 return -4;
962 if (!strcmp (asmspec, "cc"))
963 return -3;
965 return -2;
968 return -1;
972 decode_reg_name (const char *name)
974 int count;
975 return decode_reg_name_and_count (name, &count);
979 /* Return true if DECL's initializer is suitable for a BSS section. */
981 bool
982 bss_initializer_p (const_tree decl)
984 return (DECL_INITIAL (decl) == NULL
985 /* In LTO we have no errors in program; error_mark_node is used
986 to mark offlined constructors. */
987 || (DECL_INITIAL (decl) == error_mark_node
988 && !in_lto_p)
989 || (flag_zero_initialized_in_bss
990 /* Leave constant zeroes in .rodata so they
991 can be shared. */
992 && !TREE_READONLY (decl)
993 && initializer_zerop (DECL_INITIAL (decl))));
996 /* Compute the alignment of variable specified by DECL.
997 DONT_OUTPUT_DATA is from assemble_variable. */
999 void
1000 align_variable (tree decl, bool dont_output_data)
1002 unsigned int align = DECL_ALIGN (decl);
1004 /* In the case for initialing an array whose length isn't specified,
1005 where we have not yet been able to do the layout,
1006 figure out the proper alignment now. */
1007 if (dont_output_data && DECL_SIZE (decl) == 0
1008 && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1009 align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1011 /* Some object file formats have a maximum alignment which they support.
1012 In particular, a.out format supports a maximum alignment of 4. */
1013 if (align > MAX_OFILE_ALIGNMENT)
1015 error ("alignment of %q+D is greater than maximum object "
1016 "file alignment %d", decl,
1017 MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1018 align = MAX_OFILE_ALIGNMENT;
1021 if (! DECL_USER_ALIGN (decl))
1023 #ifdef DATA_ABI_ALIGNMENT
1024 unsigned int data_abi_align
1025 = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1026 /* For backwards compatibility, don't assume the ABI alignment for
1027 TLS variables. */
1028 if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1029 align = data_abi_align;
1030 #endif
1032 /* On some machines, it is good to increase alignment sometimes.
1033 But as DECL_ALIGN is used both for actually emitting the variable
1034 and for code accessing the variable as guaranteed alignment, we
1035 can only increase the alignment if it is a performance optimization
1036 if the references to it must bind to the current definition. */
1037 if (decl_binds_to_current_def_p (decl)
1038 && !DECL_VIRTUAL_P (decl))
1040 #ifdef DATA_ALIGNMENT
1041 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1042 /* Don't increase alignment too much for TLS variables - TLS space
1043 is too precious. */
1044 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1045 align = data_align;
1046 #endif
1047 if (DECL_INITIAL (decl) != 0
1048 /* In LTO we have no errors in program; error_mark_node is used
1049 to mark offlined constructors. */
1050 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1052 unsigned int const_align
1053 = CONSTANT_ALIGNMENT (DECL_INITIAL (decl), align);
1054 /* Don't increase alignment too much for TLS variables - TLS
1055 space is too precious. */
1056 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1057 align = const_align;
1062 /* Reset the alignment in case we have made it tighter, so we can benefit
1063 from it in get_pointer_alignment. */
1064 DECL_ALIGN (decl) = align;
1067 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1068 beyond what align_variable returned. */
1070 static unsigned int
1071 get_variable_align (tree decl)
1073 unsigned int align = DECL_ALIGN (decl);
1075 /* For user aligned vars or static vars align_variable already did
1076 everything. */
1077 if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1078 return align;
1080 #ifdef DATA_ABI_ALIGNMENT
1081 if (DECL_THREAD_LOCAL_P (decl))
1082 align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1083 #endif
1085 /* For decls that bind to the current definition, align_variable
1086 did also everything, except for not assuming ABI required alignment
1087 of TLS variables. For other vars, increase the alignment here
1088 as an optimization. */
1089 if (!decl_binds_to_current_def_p (decl))
1091 /* On some machines, it is good to increase alignment sometimes. */
1092 #ifdef DATA_ALIGNMENT
1093 unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1094 /* Don't increase alignment too much for TLS variables - TLS space
1095 is too precious. */
1096 if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1097 align = data_align;
1098 #endif
1099 if (DECL_INITIAL (decl) != 0
1100 /* In LTO we have no errors in program; error_mark_node is used
1101 to mark offlined constructors. */
1102 && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1104 unsigned int const_align = CONSTANT_ALIGNMENT (DECL_INITIAL (decl),
1105 align);
1106 /* Don't increase alignment too much for TLS variables - TLS space
1107 is too precious. */
1108 if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1109 align = const_align;
1113 return align;
1116 /* Return the section into which the given VAR_DECL or CONST_DECL
1117 should be placed. PREFER_NOSWITCH_P is true if a noswitch
1118 section should be used wherever possible. */
1120 section *
1121 get_variable_section (tree decl, bool prefer_noswitch_p)
1123 addr_space_t as = ADDR_SPACE_GENERIC;
1124 int reloc;
1125 varpool_node *vnode = varpool_node::get (decl);
1126 if (vnode)
1128 vnode = vnode->ultimate_alias_target ();
1129 decl = vnode->decl;
1132 if (TREE_TYPE (decl) != error_mark_node)
1133 as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1135 /* We need the constructor to figure out reloc flag. */
1136 if (vnode)
1137 vnode->get_constructor ();
1139 if (DECL_COMMON (decl))
1141 /* If the decl has been given an explicit section name, or it resides
1142 in a non-generic address space, then it isn't common, and shouldn't
1143 be handled as such. */
1144 gcc_assert (DECL_SECTION_NAME (decl) == NULL
1145 && ADDR_SPACE_GENERIC_P (as));
1146 if (DECL_THREAD_LOCAL_P (decl))
1147 return tls_comm_section;
1148 else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1149 return comm_section;
1152 if (DECL_INITIAL (decl) == error_mark_node)
1153 reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1154 else if (DECL_INITIAL (decl))
1155 reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1156 else
1157 reloc = 0;
1159 resolve_unique_section (decl, reloc, flag_data_sections);
1160 if (IN_NAMED_SECTION (decl))
1161 return get_named_section (decl, NULL, reloc);
1163 if (ADDR_SPACE_GENERIC_P (as)
1164 && !DECL_THREAD_LOCAL_P (decl)
1165 && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1166 && bss_initializer_p (decl))
1168 if (!TREE_PUBLIC (decl)
1169 && !((flag_sanitize & SANITIZE_ADDRESS)
1170 && asan_protect_global (decl)))
1171 return lcomm_section;
1172 if (bss_noswitch_section)
1173 return bss_noswitch_section;
1176 return targetm.asm_out.select_section (decl, reloc,
1177 get_variable_align (decl));
1180 /* Return the block into which object_block DECL should be placed. */
1182 static struct object_block *
1183 get_block_for_decl (tree decl)
1185 section *sect;
1187 if (TREE_CODE (decl) == VAR_DECL)
1189 /* The object must be defined in this translation unit. */
1190 if (DECL_EXTERNAL (decl))
1191 return NULL;
1193 /* There's no point using object blocks for something that is
1194 isolated by definition. */
1195 if (DECL_COMDAT_GROUP (decl))
1196 return NULL;
1199 /* We can only calculate block offsets if the decl has a known
1200 constant size. */
1201 if (DECL_SIZE_UNIT (decl) == NULL)
1202 return NULL;
1203 if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1204 return NULL;
1206 /* Find out which section should contain DECL. We cannot put it into
1207 an object block if it requires a standalone definition. */
1208 if (TREE_CODE (decl) == VAR_DECL)
1209 align_variable (decl, 0);
1210 sect = get_variable_section (decl, true);
1211 if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1212 return NULL;
1214 return get_block_for_section (sect);
1217 /* Make sure block symbol SYMBOL is in block BLOCK. */
1219 static void
1220 change_symbol_block (rtx symbol, struct object_block *block)
1222 if (block != SYMBOL_REF_BLOCK (symbol))
1224 gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1225 SYMBOL_REF_BLOCK (symbol) = block;
1229 /* Return true if it is possible to put DECL in an object_block. */
1231 static bool
1232 use_blocks_for_decl_p (tree decl)
1234 struct symtab_node *snode;
1236 /* Only data DECLs can be placed into object blocks. */
1237 if (TREE_CODE (decl) != VAR_DECL && TREE_CODE (decl) != CONST_DECL)
1238 return false;
1240 /* Detect decls created by dw2_force_const_mem. Such decls are
1241 special because DECL_INITIAL doesn't specify the decl's true value.
1242 dw2_output_indirect_constants will instead call assemble_variable
1243 with dont_output_data set to 1 and then print the contents itself. */
1244 if (DECL_INITIAL (decl) == decl)
1245 return false;
1247 /* If this decl is an alias, then we don't want to emit a
1248 definition. */
1249 if (TREE_CODE (decl) == VAR_DECL
1250 && (snode = symtab_node::get (decl)) != NULL
1251 && snode->alias)
1252 return false;
1254 return targetm.use_blocks_for_decl_p (decl);
1257 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1258 until we find an identifier that is not itself a transparent alias.
1259 Modify the alias passed to it by reference (and all aliases on the
1260 way to the ultimate target), such that they do not have to be
1261 followed again, and return the ultimate target of the alias
1262 chain. */
1264 static inline tree
1265 ultimate_transparent_alias_target (tree *alias)
1267 tree target = *alias;
1269 if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1271 gcc_assert (TREE_CHAIN (target));
1272 target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1273 gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1274 && ! TREE_CHAIN (target));
1275 *alias = target;
1278 return target;
1281 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL. DECL should
1282 have static storage duration. In other words, it should not be an
1283 automatic variable, including PARM_DECLs.
1285 There is, however, one exception: this function handles variables
1286 explicitly placed in a particular register by the user.
1288 This is never called for PARM_DECL nodes. */
1290 void
1291 make_decl_rtl (tree decl)
1293 const char *name = 0;
1294 int reg_number;
1295 tree id;
1296 rtx x;
1298 /* Check that we are not being given an automatic variable. */
1299 gcc_assert (TREE_CODE (decl) != PARM_DECL
1300 && TREE_CODE (decl) != RESULT_DECL);
1302 /* A weak alias has TREE_PUBLIC set but not the other bits. */
1303 gcc_assert (TREE_CODE (decl) != VAR_DECL
1304 || TREE_STATIC (decl)
1305 || TREE_PUBLIC (decl)
1306 || DECL_EXTERNAL (decl)
1307 || DECL_REGISTER (decl));
1309 /* And that we were not given a type or a label. */
1310 gcc_assert (TREE_CODE (decl) != TYPE_DECL
1311 && TREE_CODE (decl) != LABEL_DECL);
1313 /* For a duplicate declaration, we can be called twice on the
1314 same DECL node. Don't discard the RTL already made. */
1315 if (DECL_RTL_SET_P (decl))
1317 /* If the old RTL had the wrong mode, fix the mode. */
1318 x = DECL_RTL (decl);
1319 if (GET_MODE (x) != DECL_MODE (decl))
1320 SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1322 if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1323 return;
1325 /* ??? Another way to do this would be to maintain a hashed
1326 table of such critters. Instead of adding stuff to a DECL
1327 to give certain attributes to it, we could use an external
1328 hash map from DECL to set of attributes. */
1330 /* Let the target reassign the RTL if it wants.
1331 This is necessary, for example, when one machine specific
1332 decl attribute overrides another. */
1333 targetm.encode_section_info (decl, DECL_RTL (decl), false);
1335 /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1336 on the new decl information. */
1337 if (MEM_P (x)
1338 && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1339 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1340 change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1342 return;
1345 /* If this variable belongs to the global constant pool, retrieve the
1346 pre-computed RTL or recompute it in LTO mode. */
1347 if (TREE_CODE (decl) == VAR_DECL && DECL_IN_CONSTANT_POOL (decl))
1349 SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1350 return;
1353 id = DECL_ASSEMBLER_NAME (decl);
1354 if (TREE_CODE (decl) == FUNCTION_DECL
1355 && cgraph_node::get (decl)
1356 && cgraph_node::get (decl)->instrumentation_clone)
1357 ultimate_transparent_alias_target (&id);
1358 name = IDENTIFIER_POINTER (id);
1360 if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1361 && DECL_REGISTER (decl))
1363 error ("register name not specified for %q+D", decl);
1365 else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1367 const char *asmspec = name+1;
1368 machine_mode mode = DECL_MODE (decl);
1369 reg_number = decode_reg_name (asmspec);
1370 /* First detect errors in declaring global registers. */
1371 if (reg_number == -1)
1372 error ("register name not specified for %q+D", decl);
1373 else if (reg_number < 0)
1374 error ("invalid register name for %q+D", decl);
1375 else if (mode == BLKmode)
1376 error ("data type of %q+D isn%'t suitable for a register",
1377 decl);
1378 else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1379 error ("the register specified for %q+D cannot be accessed"
1380 " by the current target", decl);
1381 else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1382 error ("the register specified for %q+D is not general enough"
1383 " to be used as a register variable", decl);
1384 else if (!HARD_REGNO_MODE_OK (reg_number, mode))
1385 error ("register specified for %q+D isn%'t suitable for data type",
1386 decl);
1387 /* Now handle properly declared static register variables. */
1388 else
1390 int nregs;
1392 if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1394 DECL_INITIAL (decl) = 0;
1395 error ("global register variable has initial value");
1397 if (TREE_THIS_VOLATILE (decl))
1398 warning (OPT_Wvolatile_register_var,
1399 "optimization may eliminate reads and/or "
1400 "writes to register variables");
1402 /* If the user specified one of the eliminables registers here,
1403 e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1404 confused with that register and be eliminated. This usage is
1405 somewhat suspect... */
1407 SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1408 ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1409 REG_USERVAR_P (DECL_RTL (decl)) = 1;
1411 if (TREE_STATIC (decl))
1413 /* Make this register global, so not usable for anything
1414 else. */
1415 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1416 name = IDENTIFIER_POINTER (DECL_NAME (decl));
1417 ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1418 #endif
1419 nregs = hard_regno_nregs[reg_number][mode];
1420 while (nregs > 0)
1421 globalize_reg (decl, reg_number + --nregs);
1424 /* As a register variable, it has no section. */
1425 return;
1427 /* Avoid internal errors from invalid register
1428 specifications. */
1429 SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1430 DECL_HARD_REGISTER (decl) = 0;
1431 return;
1433 /* Now handle ordinary static variables and functions (in memory).
1434 Also handle vars declared register invalidly. */
1435 else if (name[0] == '*')
1437 #ifdef REGISTER_PREFIX
1438 if (strlen (REGISTER_PREFIX) != 0)
1440 reg_number = decode_reg_name (name);
1441 if (reg_number >= 0 || reg_number == -3)
1442 error ("register name given for non-register variable %q+D", decl);
1444 #endif
1447 /* Specifying a section attribute on a variable forces it into a
1448 non-.bss section, and thus it cannot be common. */
1449 /* FIXME: In general this code should not be necessary because
1450 visibility pass is doing the same work. But notice_global_symbol
1451 is called early and it needs to make DECL_RTL to get the name.
1452 we take care of recomputing the DECL_RTL after visibility is changed. */
1453 if (TREE_CODE (decl) == VAR_DECL
1454 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1455 && DECL_SECTION_NAME (decl) != NULL
1456 && DECL_INITIAL (decl) == NULL_TREE
1457 && DECL_COMMON (decl))
1458 DECL_COMMON (decl) = 0;
1460 /* Variables can't be both common and weak. */
1461 if (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl))
1462 DECL_COMMON (decl) = 0;
1464 if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1465 x = create_block_symbol (name, get_block_for_decl (decl), -1);
1466 else
1468 machine_mode address_mode = Pmode;
1469 if (TREE_TYPE (decl) != error_mark_node)
1471 addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1472 address_mode = targetm.addr_space.address_mode (as);
1474 x = gen_rtx_SYMBOL_REF (address_mode, name);
1476 SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1477 SET_SYMBOL_REF_DECL (x, decl);
1479 x = gen_rtx_MEM (DECL_MODE (decl), x);
1480 if (TREE_CODE (decl) != FUNCTION_DECL)
1481 set_mem_attributes (x, decl, 1);
1482 SET_DECL_RTL (decl, x);
1484 /* Optionally set flags or add text to the name to record information
1485 such as that it is a function name.
1486 If the name is changed, the macro ASM_OUTPUT_LABELREF
1487 will have to know how to strip this information. */
1488 targetm.encode_section_info (decl, DECL_RTL (decl), true);
1491 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1492 calling make_decl_rtl. Also, reset DECL_RTL before returning the
1493 rtl. */
1496 make_decl_rtl_for_debug (tree decl)
1498 unsigned int save_aliasing_flag;
1499 rtx rtl;
1501 if (DECL_RTL_SET_P (decl))
1502 return DECL_RTL (decl);
1504 /* Kludge alert! Somewhere down the call chain, make_decl_rtl will
1505 call new_alias_set. If running with -fcompare-debug, sometimes
1506 we do not want to create alias sets that will throw the alias
1507 numbers off in the comparison dumps. So... clearing
1508 flag_strict_aliasing will keep new_alias_set() from creating a
1509 new set. */
1510 save_aliasing_flag = flag_strict_aliasing;
1511 flag_strict_aliasing = 0;
1513 rtl = DECL_RTL (decl);
1514 /* Reset DECL_RTL back, as various parts of the compiler expects
1515 DECL_RTL set meaning it is actually going to be output. */
1516 SET_DECL_RTL (decl, NULL);
1518 flag_strict_aliasing = save_aliasing_flag;
1519 return rtl;
1522 /* Output a string of literal assembler code
1523 for an `asm' keyword used between functions. */
1525 void
1526 assemble_asm (tree string)
1528 const char *p;
1529 app_enable ();
1531 if (TREE_CODE (string) == ADDR_EXPR)
1532 string = TREE_OPERAND (string, 0);
1534 p = TREE_STRING_POINTER (string);
1535 fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1538 /* Write the address of the entity given by SYMBOL to SEC. */
1539 void
1540 assemble_addr_to_section (rtx symbol, section *sec)
1542 switch_to_section (sec);
1543 assemble_align (POINTER_SIZE);
1544 assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1547 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1548 not) section for PRIORITY. */
1549 section *
1550 get_cdtor_priority_section (int priority, bool constructor_p)
1552 char buf[16];
1554 /* ??? This only works reliably with the GNU linker. */
1555 sprintf (buf, "%s.%.5u",
1556 constructor_p ? ".ctors" : ".dtors",
1557 /* Invert the numbering so the linker puts us in the proper
1558 order; constructors are run from right to left, and the
1559 linker sorts in increasing order. */
1560 MAX_INIT_PRIORITY - priority);
1561 return get_section (buf, SECTION_WRITE, NULL);
1564 void
1565 default_named_section_asm_out_destructor (rtx symbol, int priority)
1567 section *sec;
1569 if (priority != DEFAULT_INIT_PRIORITY)
1570 sec = get_cdtor_priority_section (priority,
1571 /*constructor_p=*/false);
1572 else
1573 sec = get_section (".dtors", SECTION_WRITE, NULL);
1575 assemble_addr_to_section (symbol, sec);
1578 #ifdef DTORS_SECTION_ASM_OP
1579 void
1580 default_dtor_section_asm_out_destructor (rtx symbol,
1581 int priority ATTRIBUTE_UNUSED)
1583 assemble_addr_to_section (symbol, dtors_section);
1585 #endif
1587 void
1588 default_named_section_asm_out_constructor (rtx symbol, int priority)
1590 section *sec;
1592 if (priority != DEFAULT_INIT_PRIORITY)
1593 sec = get_cdtor_priority_section (priority,
1594 /*constructor_p=*/true);
1595 else
1596 sec = get_section (".ctors", SECTION_WRITE, NULL);
1598 assemble_addr_to_section (symbol, sec);
1601 #ifdef CTORS_SECTION_ASM_OP
1602 void
1603 default_ctor_section_asm_out_constructor (rtx symbol,
1604 int priority ATTRIBUTE_UNUSED)
1606 assemble_addr_to_section (symbol, ctors_section);
1608 #endif
1610 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1611 a nonzero value if the constant pool should be output before the
1612 start of the function, or a zero value if the pool should output
1613 after the end of the function. The default is to put it before the
1614 start. */
1616 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1617 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1618 #endif
1620 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1621 to be output to assembler.
1622 Set first_global_object_name and weak_global_object_name as appropriate. */
1624 void
1625 notice_global_symbol (tree decl)
1627 const char **t = &first_global_object_name;
1629 if (first_global_object_name
1630 || !TREE_PUBLIC (decl)
1631 || DECL_EXTERNAL (decl)
1632 || !DECL_NAME (decl)
1633 || (TREE_CODE (decl) == VAR_DECL && DECL_HARD_REGISTER (decl))
1634 || (TREE_CODE (decl) != FUNCTION_DECL
1635 && (TREE_CODE (decl) != VAR_DECL
1636 || (DECL_COMMON (decl)
1637 && (DECL_INITIAL (decl) == 0
1638 || DECL_INITIAL (decl) == error_mark_node)))))
1639 return;
1641 /* We win when global object is found, but it is useful to know about weak
1642 symbol as well so we can produce nicer unique names. */
1643 if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1644 t = &weak_global_object_name;
1646 if (!*t)
1648 tree id = DECL_ASSEMBLER_NAME (decl);
1649 ultimate_transparent_alias_target (&id);
1650 *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1654 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1655 current function goes into the cold section, so that targets can use
1656 current_function_section during RTL expansion. DECL describes the
1657 function. */
1659 void
1660 decide_function_section (tree decl)
1662 first_function_block_is_cold = false;
1664 if (flag_reorder_blocks_and_partition)
1665 /* We will decide in assemble_start_function. */
1666 return;
1668 if (DECL_SECTION_NAME (decl))
1670 struct cgraph_node *node = cgraph_node::get (current_function_decl);
1671 /* Calls to function_section rely on first_function_block_is_cold
1672 being accurate. */
1673 first_function_block_is_cold = (node
1674 && node->frequency
1675 == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1678 in_cold_section_p = first_function_block_is_cold;
1681 /* Get the function's name, as described by its RTL. This may be
1682 different from the DECL_NAME name used in the source file. */
1683 const char *
1684 get_fnname_from_decl (tree decl)
1686 rtx x = DECL_RTL (decl);
1687 gcc_assert (MEM_P (x));
1688 x = XEXP (x, 0);
1689 gcc_assert (GET_CODE (x) == SYMBOL_REF);
1690 return XSTR (x, 0);
1693 /* Output assembler code for the constant pool of a function and associated
1694 with defining the name of the function. DECL describes the function.
1695 NAME is the function's name. For the constant pool, we use the current
1696 constant pool data. */
1698 void
1699 assemble_start_function (tree decl, const char *fnname)
1701 int align;
1702 char tmp_label[100];
1703 bool hot_label_written = false;
1705 if (flag_reorder_blocks_and_partition)
1707 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1708 crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1709 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1710 crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1711 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1712 crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1713 ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1714 crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1715 const_labelno++;
1716 cold_function_name = NULL_TREE;
1718 else
1720 crtl->subsections.hot_section_label = NULL;
1721 crtl->subsections.cold_section_label = NULL;
1722 crtl->subsections.hot_section_end_label = NULL;
1723 crtl->subsections.cold_section_end_label = NULL;
1726 /* The following code does not need preprocessing in the assembler. */
1728 app_disable ();
1730 if (CONSTANT_POOL_BEFORE_FUNCTION)
1731 output_constant_pool (fnname, decl);
1733 align = symtab_node::get (decl)->definition_alignment ();
1735 /* Make sure the not and cold text (code) sections are properly
1736 aligned. This is necessary here in the case where the function
1737 has both hot and cold sections, because we don't want to re-set
1738 the alignment when the section switch happens mid-function. */
1740 if (flag_reorder_blocks_and_partition)
1742 first_function_block_is_cold = false;
1744 switch_to_section (unlikely_text_section ());
1745 assemble_align (align);
1746 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1748 /* When the function starts with a cold section, we need to explicitly
1749 align the hot section and write out the hot section label.
1750 But if the current function is a thunk, we do not have a CFG. */
1751 if (!cfun->is_thunk
1752 && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1754 switch_to_section (text_section);
1755 assemble_align (align);
1756 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1757 hot_label_written = true;
1758 first_function_block_is_cold = true;
1760 in_cold_section_p = first_function_block_is_cold;
1764 /* Switch to the correct text section for the start of the function. */
1766 switch_to_section (function_section (decl));
1767 if (flag_reorder_blocks_and_partition
1768 && !hot_label_written)
1769 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1771 /* Tell assembler to move to target machine's alignment for functions. */
1772 align = floor_log2 (align / BITS_PER_UNIT);
1773 if (align > 0)
1775 ASM_OUTPUT_ALIGN (asm_out_file, align);
1778 /* Handle a user-specified function alignment.
1779 Note that we still need to align to DECL_ALIGN, as above,
1780 because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all. */
1781 if (! DECL_USER_ALIGN (decl)
1782 && align_functions_log > align
1783 && optimize_function_for_speed_p (cfun))
1785 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1786 ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1787 align_functions_log, align_functions - 1);
1788 #else
1789 ASM_OUTPUT_ALIGN (asm_out_file, align_functions_log);
1790 #endif
1793 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1794 ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1795 #endif
1797 if (!DECL_IGNORED_P (decl))
1798 (*debug_hooks->begin_function) (decl);
1800 /* Make function name accessible from other files, if appropriate. */
1802 if (TREE_PUBLIC (decl)
1803 || (cgraph_node::get (decl)->instrumentation_clone
1804 && cgraph_node::get (decl)->instrumented_version
1805 && TREE_PUBLIC (cgraph_node::get (decl)->instrumented_version->decl)))
1807 notice_global_symbol (decl);
1809 globalize_decl (decl);
1811 maybe_assemble_visibility (decl);
1814 if (DECL_PRESERVE_P (decl))
1815 targetm.asm_out.mark_decl_preserved (fnname);
1817 /* Do any machine/system dependent processing of the function name. */
1818 #ifdef ASM_DECLARE_FUNCTION_NAME
1819 ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1820 #else
1821 /* Standard thing is just output label for the function. */
1822 ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1823 #endif /* ASM_DECLARE_FUNCTION_NAME */
1825 if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1826 saw_no_split_stack = true;
1829 /* Output assembler code associated with defining the size of the
1830 function. DECL describes the function. NAME is the function's name. */
1832 void
1833 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1835 #ifdef ASM_DECLARE_FUNCTION_SIZE
1836 /* We could have switched section in the middle of the function. */
1837 if (flag_reorder_blocks_and_partition)
1838 switch_to_section (function_section (decl));
1839 ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1840 #endif
1841 if (! CONSTANT_POOL_BEFORE_FUNCTION)
1843 output_constant_pool (fnname, decl);
1844 switch_to_section (function_section (decl)); /* need to switch back */
1846 /* Output labels for end of hot/cold text sections (to be used by
1847 debug info.) */
1848 if (flag_reorder_blocks_and_partition)
1850 section *save_text_section;
1852 save_text_section = in_section;
1853 switch_to_section (unlikely_text_section ());
1854 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
1855 if (cold_function_name != NULL_TREE)
1856 ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
1857 IDENTIFIER_POINTER (cold_function_name),
1858 decl);
1859 #endif
1860 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1861 if (first_function_block_is_cold)
1862 switch_to_section (text_section);
1863 else
1864 switch_to_section (function_section (decl));
1865 ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1866 switch_to_section (save_text_section);
1870 /* Assemble code to leave SIZE bytes of zeros. */
1872 void
1873 assemble_zeros (unsigned HOST_WIDE_INT size)
1875 /* Do no output if -fsyntax-only. */
1876 if (flag_syntax_only)
1877 return;
1879 #ifdef ASM_NO_SKIP_IN_TEXT
1880 /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1881 so we must output 0s explicitly in the text section. */
1882 if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1884 unsigned HOST_WIDE_INT i;
1885 for (i = 0; i < size; i++)
1886 assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1888 else
1889 #endif
1890 if (size > 0)
1891 ASM_OUTPUT_SKIP (asm_out_file, size);
1894 /* Assemble an alignment pseudo op for an ALIGN-bit boundary. */
1896 void
1897 assemble_align (int align)
1899 if (align > BITS_PER_UNIT)
1901 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1905 /* Assemble a string constant with the specified C string as contents. */
1907 void
1908 assemble_string (const char *p, int size)
1910 int pos = 0;
1911 int maximum = 2000;
1913 /* If the string is very long, split it up. */
1915 while (pos < size)
1917 int thissize = size - pos;
1918 if (thissize > maximum)
1919 thissize = maximum;
1921 ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
1923 pos += thissize;
1924 p += thissize;
1929 /* A noswitch_section_callback for lcomm_section. */
1931 static bool
1932 emit_local (tree decl ATTRIBUTE_UNUSED,
1933 const char *name ATTRIBUTE_UNUSED,
1934 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1935 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1937 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
1938 unsigned int align = symtab_node::get (decl)->definition_alignment ();
1939 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
1940 size, align);
1941 return true;
1942 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
1943 unsigned int align = symtab_node::get (decl)->definition_alignment ();
1944 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
1945 return true;
1946 #else
1947 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
1948 return false;
1949 #endif
1952 /* A noswitch_section_callback for bss_noswitch_section. */
1954 #if defined ASM_OUTPUT_ALIGNED_BSS
1955 static bool
1956 emit_bss (tree decl ATTRIBUTE_UNUSED,
1957 const char *name ATTRIBUTE_UNUSED,
1958 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1959 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1961 ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
1962 get_variable_align (decl));
1963 return true;
1965 #endif
1967 /* A noswitch_section_callback for comm_section. */
1969 static bool
1970 emit_common (tree decl ATTRIBUTE_UNUSED,
1971 const char *name ATTRIBUTE_UNUSED,
1972 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1973 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1975 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
1976 ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
1977 size, get_variable_align (decl));
1978 return true;
1979 #elif defined ASM_OUTPUT_ALIGNED_COMMON
1980 ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
1981 get_variable_align (decl));
1982 return true;
1983 #else
1984 ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
1985 return false;
1986 #endif
1989 /* A noswitch_section_callback for tls_comm_section. */
1991 static bool
1992 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
1993 const char *name ATTRIBUTE_UNUSED,
1994 unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
1995 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
1997 #ifdef ASM_OUTPUT_TLS_COMMON
1998 ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
1999 return true;
2000 #else
2001 sorry ("thread-local COMMON data not implemented");
2002 return true;
2003 #endif
2006 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2007 NAME is the name of DECL's SYMBOL_REF. */
2009 static void
2010 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2011 unsigned int align)
2013 unsigned HOST_WIDE_INT size, rounded;
2015 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2016 rounded = size;
2018 if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2019 size += asan_red_zone_size (size);
2021 /* Don't allocate zero bytes of common,
2022 since that means "undefined external" in the linker. */
2023 if (size == 0)
2024 rounded = 1;
2026 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2027 so that each uninitialized object starts on such a boundary. */
2028 rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2029 rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2030 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2032 if (!sect->noswitch.callback (decl, name, size, rounded)
2033 && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2034 error ("requested alignment for %q+D is greater than "
2035 "implemented alignment of %wu", decl, rounded);
2038 /* A subroutine of assemble_variable. Output the label and contents of
2039 DECL, whose address is a SYMBOL_REF with name NAME. DONT_OUTPUT_DATA
2040 is as for assemble_variable. */
2042 static void
2043 assemble_variable_contents (tree decl, const char *name,
2044 bool dont_output_data)
2046 /* Do any machine/system dependent processing of the object. */
2047 #ifdef ASM_DECLARE_OBJECT_NAME
2048 last_assemble_variable_decl = decl;
2049 ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2050 #else
2051 /* Standard thing is just output label for the object. */
2052 ASM_OUTPUT_LABEL (asm_out_file, name);
2053 #endif /* ASM_DECLARE_OBJECT_NAME */
2055 if (!dont_output_data)
2057 /* Caller is supposed to use varpool_get_constructor when it wants
2058 to output the body. */
2059 gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2060 if (DECL_INITIAL (decl)
2061 && DECL_INITIAL (decl) != error_mark_node
2062 && !initializer_zerop (DECL_INITIAL (decl)))
2063 /* Output the actual data. */
2064 output_constant (DECL_INITIAL (decl),
2065 tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2066 get_variable_align (decl));
2067 else
2068 /* Leave space for it. */
2069 assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2070 targetm.asm_out.decl_end ();
2074 /* Write out assembly for the variable DECL, which is not defined in
2075 the current translation unit. */
2076 void
2077 assemble_undefined_decl (tree decl)
2079 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2080 targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2083 /* Assemble everything that is needed for a variable or function declaration.
2084 Not used for automatic variables, and not used for function definitions.
2085 Should not be called for variables of incomplete structure type.
2087 TOP_LEVEL is nonzero if this variable has file scope.
2088 AT_END is nonzero if this is the special handling, at end of compilation,
2089 to define things that have had only tentative definitions.
2090 DONT_OUTPUT_DATA if nonzero means don't actually output the
2091 initial value (that will be done by the caller). */
2093 void
2094 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2095 int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2097 const char *name;
2098 rtx decl_rtl, symbol;
2099 section *sect;
2100 unsigned int align;
2101 bool asan_protected = false;
2103 /* This function is supposed to handle VARIABLES. Ensure we have one. */
2104 gcc_assert (TREE_CODE (decl) == VAR_DECL);
2106 /* Emulated TLS had better not get this far. */
2107 gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2109 last_assemble_variable_decl = 0;
2111 /* Normally no need to say anything here for external references,
2112 since assemble_external is called by the language-specific code
2113 when a declaration is first seen. */
2115 if (DECL_EXTERNAL (decl))
2116 return;
2118 /* Do nothing for global register variables. */
2119 if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2121 TREE_ASM_WRITTEN (decl) = 1;
2122 return;
2125 /* If type was incomplete when the variable was declared,
2126 see if it is complete now. */
2128 if (DECL_SIZE (decl) == 0)
2129 layout_decl (decl, 0);
2131 /* Still incomplete => don't allocate it; treat the tentative defn
2132 (which is what it must have been) as an `extern' reference. */
2134 if (!dont_output_data && DECL_SIZE (decl) == 0)
2136 error ("storage size of %q+D isn%'t known", decl);
2137 TREE_ASM_WRITTEN (decl) = 1;
2138 return;
2141 /* The first declaration of a variable that comes through this function
2142 decides whether it is global (in C, has external linkage)
2143 or local (in C, has internal linkage). So do nothing more
2144 if this function has already run. */
2146 if (TREE_ASM_WRITTEN (decl))
2147 return;
2149 /* Make sure targetm.encode_section_info is invoked before we set
2150 ASM_WRITTEN. */
2151 decl_rtl = DECL_RTL (decl);
2153 TREE_ASM_WRITTEN (decl) = 1;
2155 /* Do no output if -fsyntax-only. */
2156 if (flag_syntax_only)
2157 return;
2159 if (! dont_output_data
2160 && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2162 error ("size of variable %q+D is too large", decl);
2163 return;
2166 gcc_assert (MEM_P (decl_rtl));
2167 gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2168 symbol = XEXP (decl_rtl, 0);
2170 /* If this symbol belongs to the tree constant pool, output the constant
2171 if it hasn't already been written. */
2172 if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2174 tree decl = SYMBOL_REF_DECL (symbol);
2175 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2176 output_constant_def_contents (symbol);
2177 return;
2180 app_disable ();
2182 name = XSTR (symbol, 0);
2183 if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2184 notice_global_symbol (decl);
2186 /* Compute the alignment of this data. */
2188 align_variable (decl, dont_output_data);
2190 if ((flag_sanitize & SANITIZE_ADDRESS)
2191 && asan_protect_global (decl))
2193 asan_protected = true;
2194 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
2195 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
2198 set_mem_align (decl_rtl, DECL_ALIGN (decl));
2200 align = get_variable_align (decl);
2202 if (TREE_PUBLIC (decl))
2203 maybe_assemble_visibility (decl);
2205 if (DECL_PRESERVE_P (decl))
2206 targetm.asm_out.mark_decl_preserved (name);
2208 /* First make the assembler name(s) global if appropriate. */
2209 sect = get_variable_section (decl, false);
2210 if (TREE_PUBLIC (decl)
2211 && (sect->common.flags & SECTION_COMMON) == 0)
2212 globalize_decl (decl);
2214 /* Output any data that we will need to use the address of. */
2215 if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2216 output_addressed_constants (DECL_INITIAL (decl));
2218 /* dbxout.c needs to know this. */
2219 if (sect && (sect->common.flags & SECTION_CODE) != 0)
2220 DECL_IN_TEXT_SECTION (decl) = 1;
2222 /* If the decl is part of an object_block, make sure that the decl
2223 has been positioned within its block, but do not write out its
2224 definition yet. output_object_blocks will do that later. */
2225 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2227 gcc_assert (!dont_output_data);
2228 place_block_symbol (symbol);
2230 else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2231 assemble_noswitch_variable (decl, name, sect, align);
2232 else
2234 /* Special-case handling of vtv comdat sections. */
2235 if (sect->named.name
2236 && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2237 handle_vtv_comdat_section (sect, decl);
2238 else
2239 switch_to_section (sect);
2240 if (align > BITS_PER_UNIT)
2241 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2242 assemble_variable_contents (decl, name, dont_output_data);
2243 if (asan_protected)
2245 unsigned HOST_WIDE_INT int size
2246 = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2247 assemble_zeros (asan_red_zone_size (size));
2253 /* Given a function declaration (FN_DECL), this function assembles the
2254 function into the .preinit_array section. */
2256 void
2257 assemble_vtv_preinit_initializer (tree fn_decl)
2259 section *sect;
2260 unsigned flags = SECTION_WRITE;
2261 rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2263 flags |= SECTION_NOTYPE;
2264 sect = get_section (".preinit_array", flags, fn_decl);
2265 switch_to_section (sect);
2266 assemble_addr_to_section (symbol, sect);
2269 /* Return 1 if type TYPE contains any pointers. */
2271 static int
2272 contains_pointers_p (tree type)
2274 switch (TREE_CODE (type))
2276 case POINTER_TYPE:
2277 case REFERENCE_TYPE:
2278 /* I'm not sure whether OFFSET_TYPE needs this treatment,
2279 so I'll play safe and return 1. */
2280 case OFFSET_TYPE:
2281 return 1;
2283 case RECORD_TYPE:
2284 case UNION_TYPE:
2285 case QUAL_UNION_TYPE:
2287 tree fields;
2288 /* For a type that has fields, see if the fields have pointers. */
2289 for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2290 if (TREE_CODE (fields) == FIELD_DECL
2291 && contains_pointers_p (TREE_TYPE (fields)))
2292 return 1;
2293 return 0;
2296 case ARRAY_TYPE:
2297 /* An array type contains pointers if its element type does. */
2298 return contains_pointers_p (TREE_TYPE (type));
2300 default:
2301 return 0;
2305 /* We delay assemble_external processing until
2306 the compilation unit is finalized. This is the best we can do for
2307 right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2308 it all the way to final. See PR 17982 for further discussion. */
2309 static GTY(()) tree pending_assemble_externals;
2311 #ifdef ASM_OUTPUT_EXTERNAL
2312 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2313 As a result, assemble_external can be called after the list of externals
2314 is processed and the pointer set destroyed. */
2315 static bool pending_assemble_externals_processed;
2317 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2318 TREE_LIST in assemble_external. */
2319 static hash_set<tree> *pending_assemble_externals_set;
2321 /* True if DECL is a function decl for which no out-of-line copy exists.
2322 It is assumed that DECL's assembler name has been set. */
2324 static bool
2325 incorporeal_function_p (tree decl)
2327 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_BUILT_IN (decl))
2329 const char *name;
2331 if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2332 && (DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA
2333 || DECL_FUNCTION_CODE (decl) == BUILT_IN_ALLOCA_WITH_ALIGN))
2334 return true;
2336 name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2337 /* Atomic or sync builtins which have survived this far will be
2338 resolved externally and therefore are not incorporeal. */
2339 if (strncmp (name, "__builtin_", 10) == 0)
2340 return true;
2342 return false;
2345 /* Actually do the tests to determine if this is necessary, and invoke
2346 ASM_OUTPUT_EXTERNAL. */
2347 static void
2348 assemble_external_real (tree decl)
2350 rtx rtl = DECL_RTL (decl);
2352 if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2353 && !SYMBOL_REF_USED (XEXP (rtl, 0))
2354 && !incorporeal_function_p (decl))
2356 /* Some systems do require some output. */
2357 SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2358 ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2361 #endif
2363 void
2364 process_pending_assemble_externals (void)
2366 #ifdef ASM_OUTPUT_EXTERNAL
2367 tree list;
2368 for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2369 assemble_external_real (TREE_VALUE (list));
2371 pending_assemble_externals = 0;
2372 pending_assemble_externals_processed = true;
2373 delete pending_assemble_externals_set;
2374 #endif
2377 /* This TREE_LIST contains any weak symbol declarations waiting
2378 to be emitted. */
2379 static GTY(()) tree weak_decls;
2381 /* Output something to declare an external symbol to the assembler,
2382 and qualifiers such as weakness. (Most assemblers don't need
2383 extern declaration, so we normally output nothing.) Do nothing if
2384 DECL is not external. */
2386 void
2387 assemble_external (tree decl ATTRIBUTE_UNUSED)
2389 /* Make sure that the ASM_OUT_FILE is open.
2390 If it's not, we should not be calling this function. */
2391 gcc_assert (asm_out_file);
2393 /* In a perfect world, the following condition would be true.
2394 Sadly, the Java and Go front ends emit assembly *from the front end*,
2395 bypassing the call graph. See PR52739. Fix before GCC 4.8. */
2396 #if 0
2397 /* This function should only be called if we are expanding, or have
2398 expanded, to RTL.
2399 Ideally, only final.c would be calling this function, but it is
2400 not clear whether that would break things somehow. See PR 17982
2401 for further discussion. */
2402 gcc_assert (state == EXPANSION
2403 || state == FINISHED);
2404 #endif
2406 if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2407 return;
2409 /* We want to output annotation for weak and external symbols at
2410 very last to check if they are references or not. */
2412 if (TARGET_SUPPORTS_WEAK
2413 && DECL_WEAK (decl)
2414 /* TREE_STATIC is a weird and abused creature which is not
2415 generally the right test for whether an entity has been
2416 locally emitted, inlined or otherwise not-really-extern, but
2417 for declarations that can be weak, it happens to be
2418 match. */
2419 && !TREE_STATIC (decl)
2420 && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2421 && value_member (decl, weak_decls) == NULL_TREE)
2422 weak_decls = tree_cons (NULL, decl, weak_decls);
2424 #ifdef ASM_OUTPUT_EXTERNAL
2425 if (pending_assemble_externals_processed)
2427 assemble_external_real (decl);
2428 return;
2431 if (! pending_assemble_externals_set->add (decl))
2432 pending_assemble_externals = tree_cons (NULL, decl,
2433 pending_assemble_externals);
2434 #endif
2437 /* Similar, for calling a library function FUN. */
2439 void
2440 assemble_external_libcall (rtx fun)
2442 /* Declare library function name external when first used, if nec. */
2443 if (! SYMBOL_REF_USED (fun))
2445 SYMBOL_REF_USED (fun) = 1;
2446 targetm.asm_out.external_libcall (fun);
2450 /* Assemble a label named NAME. */
2452 void
2453 assemble_label (FILE *file, const char *name)
2455 ASM_OUTPUT_LABEL (file, name);
2458 /* Set the symbol_referenced flag for ID. */
2459 void
2460 mark_referenced (tree id)
2462 TREE_SYMBOL_REFERENCED (id) = 1;
2465 /* Set the symbol_referenced flag for DECL and notify callgraph. */
2466 void
2467 mark_decl_referenced (tree decl)
2469 if (TREE_CODE (decl) == FUNCTION_DECL)
2471 /* Extern inline functions don't become needed when referenced.
2472 If we know a method will be emitted in other TU and no new
2473 functions can be marked reachable, just use the external
2474 definition. */
2475 struct cgraph_node *node = cgraph_node::get_create (decl);
2476 if (!DECL_EXTERNAL (decl)
2477 && !node->definition)
2478 node->mark_force_output ();
2480 else if (TREE_CODE (decl) == VAR_DECL)
2482 varpool_node *node = varpool_node::get_create (decl);
2483 /* C++ frontend use mark_decl_references to force COMDAT variables
2484 to be output that might appear dead otherwise. */
2485 node->force_output = true;
2487 /* else do nothing - we can get various sorts of CST nodes here,
2488 which do not need to be marked. */
2492 /* Output to FILE (an assembly file) a reference to NAME. If NAME
2493 starts with a *, the rest of NAME is output verbatim. Otherwise
2494 NAME is transformed in a target-specific way (usually by the
2495 addition of an underscore). */
2497 void
2498 assemble_name_raw (FILE *file, const char *name)
2500 if (name[0] == '*')
2501 fputs (&name[1], file);
2502 else
2503 ASM_OUTPUT_LABELREF (file, name);
2506 /* Like assemble_name_raw, but should be used when NAME might refer to
2507 an entity that is also represented as a tree (like a function or
2508 variable). If NAME does refer to such an entity, that entity will
2509 be marked as referenced. */
2511 void
2512 assemble_name (FILE *file, const char *name)
2514 const char *real_name;
2515 tree id;
2517 real_name = targetm.strip_name_encoding (name);
2519 id = maybe_get_identifier (real_name);
2520 if (id)
2522 tree id_orig = id;
2524 mark_referenced (id);
2525 ultimate_transparent_alias_target (&id);
2526 if (id != id_orig)
2527 name = IDENTIFIER_POINTER (id);
2528 gcc_assert (! TREE_CHAIN (id));
2531 assemble_name_raw (file, name);
2534 /* Allocate SIZE bytes writable static space with a gensym name
2535 and return an RTX to refer to its address. */
2538 assemble_static_space (unsigned HOST_WIDE_INT size)
2540 char name[12];
2541 const char *namestring;
2542 rtx x;
2544 ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2545 ++const_labelno;
2546 namestring = ggc_strdup (name);
2548 x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2549 SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2551 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2552 ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2553 BIGGEST_ALIGNMENT);
2554 #else
2555 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2556 ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2557 #else
2559 /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2560 so that each uninitialized object starts on such a boundary. */
2561 /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL. */
2562 unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2563 = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2564 / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2565 * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2566 ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2568 #endif
2569 #endif
2570 return x;
2573 /* Assemble the static constant template for function entry trampolines.
2574 This is done at most once per compilation.
2575 Returns an RTX for the address of the template. */
2577 static GTY(()) rtx initial_trampoline;
2580 assemble_trampoline_template (void)
2582 char label[256];
2583 const char *name;
2584 int align;
2585 rtx symbol;
2587 gcc_assert (targetm.asm_out.trampoline_template != NULL);
2589 if (initial_trampoline)
2590 return initial_trampoline;
2592 /* By default, put trampoline templates in read-only data section. */
2594 #ifdef TRAMPOLINE_SECTION
2595 switch_to_section (TRAMPOLINE_SECTION);
2596 #else
2597 switch_to_section (readonly_data_section);
2598 #endif
2600 /* Write the assembler code to define one. */
2601 align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2602 if (align > 0)
2603 ASM_OUTPUT_ALIGN (asm_out_file, align);
2605 targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2606 targetm.asm_out.trampoline_template (asm_out_file);
2608 /* Record the rtl to refer to it. */
2609 ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2610 name = ggc_strdup (label);
2611 symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2612 SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2614 initial_trampoline = gen_const_mem (BLKmode, symbol);
2615 set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2616 set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2618 return initial_trampoline;
2621 /* A and B are either alignments or offsets. Return the minimum alignment
2622 that may be assumed after adding the two together. */
2624 static inline unsigned
2625 min_align (unsigned int a, unsigned int b)
2627 return (a | b) & -(a | b);
2630 /* Return the assembler directive for creating a given kind of integer
2631 object. SIZE is the number of bytes in the object and ALIGNED_P
2632 indicates whether it is known to be aligned. Return NULL if the
2633 assembly dialect has no such directive.
2635 The returned string should be printed at the start of a new line and
2636 be followed immediately by the object's initial value. */
2638 const char *
2639 integer_asm_op (int size, int aligned_p)
2641 struct asm_int_op *ops;
2643 if (aligned_p)
2644 ops = &targetm.asm_out.aligned_op;
2645 else
2646 ops = &targetm.asm_out.unaligned_op;
2648 switch (size)
2650 case 1:
2651 return targetm.asm_out.byte_op;
2652 case 2:
2653 return ops->hi;
2654 case 4:
2655 return ops->si;
2656 case 8:
2657 return ops->di;
2658 case 16:
2659 return ops->ti;
2660 default:
2661 return NULL;
2665 /* Use directive OP to assemble an integer object X. Print OP at the
2666 start of the line, followed immediately by the value of X. */
2668 void
2669 assemble_integer_with_op (const char *op, rtx x)
2671 fputs (op, asm_out_file);
2672 output_addr_const (asm_out_file, x);
2673 fputc ('\n', asm_out_file);
2676 /* The default implementation of the asm_out.integer target hook. */
2678 bool
2679 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2680 unsigned int size ATTRIBUTE_UNUSED,
2681 int aligned_p ATTRIBUTE_UNUSED)
2683 const char *op = integer_asm_op (size, aligned_p);
2684 /* Avoid GAS bugs for large values. Specifically negative values whose
2685 absolute value fits in a bfd_vma, but not in a bfd_signed_vma. */
2686 if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2687 return false;
2688 return op && (assemble_integer_with_op (op, x), true);
2691 /* Assemble the integer constant X into an object of SIZE bytes. ALIGN is
2692 the alignment of the integer in bits. Return 1 if we were able to output
2693 the constant, otherwise 0. We must be able to output the constant,
2694 if FORCE is nonzero. */
2696 bool
2697 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2699 int aligned_p;
2701 aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2703 /* See if the target hook can handle this kind of object. */
2704 if (targetm.asm_out.integer (x, size, aligned_p))
2705 return true;
2707 /* If the object is a multi-byte one, try splitting it up. Split
2708 it into words it if is multi-word, otherwise split it into bytes. */
2709 if (size > 1)
2711 machine_mode omode, imode;
2712 unsigned int subalign;
2713 unsigned int subsize, i;
2714 enum mode_class mclass;
2716 subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2717 subalign = MIN (align, subsize * BITS_PER_UNIT);
2718 if (GET_CODE (x) == CONST_FIXED)
2719 mclass = GET_MODE_CLASS (GET_MODE (x));
2720 else
2721 mclass = MODE_INT;
2723 omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0);
2724 imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0);
2726 for (i = 0; i < size; i += subsize)
2728 rtx partial = simplify_subreg (omode, x, imode, i);
2729 if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2730 break;
2732 if (i == size)
2733 return true;
2735 /* If we've printed some of it, but not all of it, there's no going
2736 back now. */
2737 gcc_assert (!i);
2740 gcc_assert (!force);
2742 return false;
2745 void
2746 assemble_real (REAL_VALUE_TYPE d, machine_mode mode, unsigned int align)
2748 long data[4] = {0, 0, 0, 0};
2749 int i;
2750 int bitsize, nelts, nunits, units_per;
2752 /* This is hairy. We have a quantity of known size. real_to_target
2753 will put it into an array of *host* longs, 32 bits per element
2754 (even if long is more than 32 bits). We need to determine the
2755 number of array elements that are occupied (nelts) and the number
2756 of *target* min-addressable units that will be occupied in the
2757 object file (nunits). We cannot assume that 32 divides the
2758 mode's bitsize (size * BITS_PER_UNIT) evenly.
2760 size * BITS_PER_UNIT is used here to make sure that padding bits
2761 (which might appear at either end of the value; real_to_target
2762 will include the padding bits in its output array) are included. */
2764 nunits = GET_MODE_SIZE (mode);
2765 bitsize = nunits * BITS_PER_UNIT;
2766 nelts = CEIL (bitsize, 32);
2767 units_per = 32 / BITS_PER_UNIT;
2769 real_to_target (data, &d, mode);
2771 /* Put out the first word with the specified alignment. */
2772 assemble_integer (GEN_INT (data[0]), MIN (nunits, units_per), align, 1);
2773 nunits -= units_per;
2775 /* Subsequent words need only 32-bit alignment. */
2776 align = min_align (align, 32);
2778 for (i = 1; i < nelts; i++)
2780 assemble_integer (GEN_INT (data[i]), MIN (nunits, units_per), align, 1);
2781 nunits -= units_per;
2785 /* Given an expression EXP with a constant value,
2786 reduce it to the sum of an assembler symbol and an integer.
2787 Store them both in the structure *VALUE.
2788 EXP must be reducible. */
2790 struct addr_const {
2791 rtx base;
2792 HOST_WIDE_INT offset;
2795 static void
2796 decode_addr_const (tree exp, struct addr_const *value)
2798 tree target = TREE_OPERAND (exp, 0);
2799 int offset = 0;
2800 rtx x;
2802 while (1)
2804 if (TREE_CODE (target) == COMPONENT_REF
2805 && tree_fits_shwi_p (byte_position (TREE_OPERAND (target, 1))))
2807 offset += int_byte_position (TREE_OPERAND (target, 1));
2808 target = TREE_OPERAND (target, 0);
2810 else if (TREE_CODE (target) == ARRAY_REF
2811 || TREE_CODE (target) == ARRAY_RANGE_REF)
2813 offset += (tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2814 * tree_to_shwi (TREE_OPERAND (target, 1)));
2815 target = TREE_OPERAND (target, 0);
2817 else if (TREE_CODE (target) == MEM_REF
2818 && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2820 offset += mem_ref_offset (target).to_short_addr ();
2821 target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2823 else if (TREE_CODE (target) == INDIRECT_REF
2824 && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2825 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2826 == ADDR_EXPR)
2827 target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2828 else
2829 break;
2832 switch (TREE_CODE (target))
2834 case VAR_DECL:
2835 case FUNCTION_DECL:
2836 x = DECL_RTL (target);
2837 break;
2839 case LABEL_DECL:
2840 x = gen_rtx_MEM (FUNCTION_MODE,
2841 gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2842 break;
2844 case REAL_CST:
2845 case FIXED_CST:
2846 case STRING_CST:
2847 case COMPLEX_CST:
2848 case CONSTRUCTOR:
2849 case INTEGER_CST:
2850 x = output_constant_def (target, 1);
2851 break;
2853 default:
2854 gcc_unreachable ();
2857 gcc_assert (MEM_P (x));
2858 x = XEXP (x, 0);
2860 value->base = x;
2861 value->offset = offset;
2864 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2866 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
2868 /* Constant pool accessor function. */
2870 hash_table<tree_descriptor_hasher> *
2871 constant_pool_htab (void)
2873 return const_desc_htab;
2876 /* Compute a hash code for a constant expression. */
2878 hashval_t
2879 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
2881 return ptr->hash;
2884 static hashval_t
2885 const_hash_1 (const tree exp)
2887 const char *p;
2888 hashval_t hi;
2889 int len, i;
2890 enum tree_code code = TREE_CODE (exp);
2892 /* Either set P and LEN to the address and len of something to hash and
2893 exit the switch or return a value. */
2895 switch (code)
2897 case INTEGER_CST:
2898 p = (char *) &TREE_INT_CST_ELT (exp, 0);
2899 len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
2900 break;
2902 case REAL_CST:
2903 return real_hash (TREE_REAL_CST_PTR (exp));
2905 case FIXED_CST:
2906 return fixed_hash (TREE_FIXED_CST_PTR (exp));
2908 case STRING_CST:
2909 p = TREE_STRING_POINTER (exp);
2910 len = TREE_STRING_LENGTH (exp);
2911 break;
2913 case COMPLEX_CST:
2914 return (const_hash_1 (TREE_REALPART (exp)) * 5
2915 + const_hash_1 (TREE_IMAGPART (exp)));
2917 case VECTOR_CST:
2919 unsigned i;
2921 hi = 7 + VECTOR_CST_NELTS (exp);
2923 for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
2924 hi = hi * 563 + const_hash_1 (VECTOR_CST_ELT (exp, i));
2926 return hi;
2929 case CONSTRUCTOR:
2931 unsigned HOST_WIDE_INT idx;
2932 tree value;
2934 hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
2936 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
2937 if (value)
2938 hi = hi * 603 + const_hash_1 (value);
2940 return hi;
2943 case ADDR_EXPR:
2944 case FDESC_EXPR:
2946 struct addr_const value;
2948 decode_addr_const (exp, &value);
2949 switch (GET_CODE (value.base))
2951 case SYMBOL_REF:
2952 /* Don't hash the address of the SYMBOL_REF;
2953 only use the offset and the symbol name. */
2954 hi = value.offset;
2955 p = XSTR (value.base, 0);
2956 for (i = 0; p[i] != 0; i++)
2957 hi = ((hi * 613) + (unsigned) (p[i]));
2958 break;
2960 case LABEL_REF:
2961 hi = (value.offset
2962 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (value.base)) * 13);
2963 break;
2965 default:
2966 gcc_unreachable ();
2969 return hi;
2971 case PLUS_EXPR:
2972 case POINTER_PLUS_EXPR:
2973 case MINUS_EXPR:
2974 return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
2975 + const_hash_1 (TREE_OPERAND (exp, 1)));
2977 CASE_CONVERT:
2978 return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
2980 default:
2981 /* A language specific constant. Just hash the code. */
2982 return code;
2985 /* Compute hashing function. */
2986 hi = len;
2987 for (i = 0; i < len; i++)
2988 hi = ((hi * 613) + (unsigned) (p[i]));
2990 return hi;
2993 /* Wrapper of compare_constant, for the htab interface. */
2994 bool
2995 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
2996 constant_descriptor_tree *c2)
2998 if (c1->hash != c2->hash)
2999 return 0;
3000 return compare_constant (c1->value, c2->value);
3003 /* Compare t1 and t2, and return 1 only if they are known to result in
3004 the same bit pattern on output. */
3006 static int
3007 compare_constant (const tree t1, const tree t2)
3009 enum tree_code typecode;
3011 if (t1 == NULL_TREE)
3012 return t2 == NULL_TREE;
3013 if (t2 == NULL_TREE)
3014 return 0;
3016 if (TREE_CODE (t1) != TREE_CODE (t2))
3017 return 0;
3019 switch (TREE_CODE (t1))
3021 case INTEGER_CST:
3022 /* Integer constants are the same only if the same width of type. */
3023 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3024 return 0;
3025 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3026 return 0;
3027 return tree_int_cst_equal (t1, t2);
3029 case REAL_CST:
3030 /* Real constants are the same only if the same width of type. */
3031 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3032 return 0;
3034 return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3036 case FIXED_CST:
3037 /* Fixed constants are the same only if the same width of type. */
3038 if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3039 return 0;
3041 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3043 case STRING_CST:
3044 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3045 return 0;
3047 return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3048 && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3049 TREE_STRING_LENGTH (t1)));
3051 case COMPLEX_CST:
3052 return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3053 && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3055 case VECTOR_CST:
3057 unsigned i;
3059 if (VECTOR_CST_NELTS (t1) != VECTOR_CST_NELTS (t2))
3060 return 0;
3062 for (i = 0; i < VECTOR_CST_NELTS (t1); ++i)
3063 if (!compare_constant (VECTOR_CST_ELT (t1, i),
3064 VECTOR_CST_ELT (t2, i)))
3065 return 0;
3067 return 1;
3070 case CONSTRUCTOR:
3072 vec<constructor_elt, va_gc> *v1, *v2;
3073 unsigned HOST_WIDE_INT idx;
3075 typecode = TREE_CODE (TREE_TYPE (t1));
3076 if (typecode != TREE_CODE (TREE_TYPE (t2)))
3077 return 0;
3079 if (typecode == ARRAY_TYPE)
3081 HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3082 /* For arrays, check that the sizes all match. */
3083 if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3084 || size_1 == -1
3085 || size_1 != int_size_in_bytes (TREE_TYPE (t2)))
3086 return 0;
3088 else
3090 /* For record and union constructors, require exact type
3091 equality. */
3092 if (TREE_TYPE (t1) != TREE_TYPE (t2))
3093 return 0;
3096 v1 = CONSTRUCTOR_ELTS (t1);
3097 v2 = CONSTRUCTOR_ELTS (t2);
3098 if (vec_safe_length (v1) != vec_safe_length (v2))
3099 return 0;
3101 for (idx = 0; idx < vec_safe_length (v1); ++idx)
3103 constructor_elt *c1 = &(*v1)[idx];
3104 constructor_elt *c2 = &(*v2)[idx];
3106 /* Check that each value is the same... */
3107 if (!compare_constant (c1->value, c2->value))
3108 return 0;
3109 /* ... and that they apply to the same fields! */
3110 if (typecode == ARRAY_TYPE)
3112 if (!compare_constant (c1->index, c2->index))
3113 return 0;
3115 else
3117 if (c1->index != c2->index)
3118 return 0;
3122 return 1;
3125 case ADDR_EXPR:
3126 case FDESC_EXPR:
3128 struct addr_const value1, value2;
3129 enum rtx_code code;
3130 int ret;
3132 decode_addr_const (t1, &value1);
3133 decode_addr_const (t2, &value2);
3135 if (value1.offset != value2.offset)
3136 return 0;
3138 code = GET_CODE (value1.base);
3139 if (code != GET_CODE (value2.base))
3140 return 0;
3142 switch (code)
3144 case SYMBOL_REF:
3145 ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3146 break;
3148 case LABEL_REF:
3149 ret = (CODE_LABEL_NUMBER (LABEL_REF_LABEL (value1.base))
3150 == CODE_LABEL_NUMBER (LABEL_REF_LABEL (value2.base)));
3151 break;
3153 default:
3154 gcc_unreachable ();
3156 return ret;
3159 case PLUS_EXPR:
3160 case POINTER_PLUS_EXPR:
3161 case MINUS_EXPR:
3162 case RANGE_EXPR:
3163 return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3164 && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3166 CASE_CONVERT:
3167 case VIEW_CONVERT_EXPR:
3168 return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3170 default:
3171 return 0;
3174 gcc_unreachable ();
3177 /* Return the section into which constant EXP should be placed. */
3179 static section *
3180 get_constant_section (tree exp, unsigned int align)
3182 return targetm.asm_out.select_section (exp,
3183 compute_reloc_for_constant (exp),
3184 align);
3187 /* Return the size of constant EXP in bytes. */
3189 static HOST_WIDE_INT
3190 get_constant_size (tree exp)
3192 HOST_WIDE_INT size;
3194 size = int_size_in_bytes (TREE_TYPE (exp));
3195 if (TREE_CODE (exp) == STRING_CST)
3196 size = MAX (TREE_STRING_LENGTH (exp), size);
3197 return size;
3200 /* Subroutine of output_constant_def:
3201 No constant equal to EXP is known to have been output.
3202 Make a constant descriptor to enter EXP in the hash table.
3203 Assign the label number and construct RTL to refer to the
3204 constant's location in memory.
3205 Caller is responsible for updating the hash table. */
3207 static struct constant_descriptor_tree *
3208 build_constant_desc (tree exp)
3210 struct constant_descriptor_tree *desc;
3211 rtx symbol, rtl;
3212 char label[256];
3213 int labelno;
3214 tree decl;
3216 desc = ggc_alloc<constant_descriptor_tree> ();
3217 desc->value = exp;
3219 /* Create a string containing the label name, in LABEL. */
3220 labelno = const_labelno++;
3221 ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3223 /* Construct the VAR_DECL associated with the constant. */
3224 decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3225 TREE_TYPE (exp));
3226 DECL_ARTIFICIAL (decl) = 1;
3227 DECL_IGNORED_P (decl) = 1;
3228 TREE_READONLY (decl) = 1;
3229 TREE_STATIC (decl) = 1;
3230 TREE_ADDRESSABLE (decl) = 1;
3231 /* We don't set the RTL yet as this would cause varpool to assume that the
3232 variable is referenced. Moreover, it would just be dropped in LTO mode.
3233 Instead we set the flag that will be recognized in make_decl_rtl. */
3234 DECL_IN_CONSTANT_POOL (decl) = 1;
3235 DECL_INITIAL (decl) = desc->value;
3236 /* ??? CONSTANT_ALIGNMENT hasn't been updated for vector types on most
3237 architectures so use DATA_ALIGNMENT as well, except for strings. */
3238 if (TREE_CODE (exp) == STRING_CST)
3240 DECL_ALIGN (decl) = CONSTANT_ALIGNMENT (exp, DECL_ALIGN (decl));
3242 else
3243 align_variable (decl, 0);
3245 /* Now construct the SYMBOL_REF and the MEM. */
3246 if (use_object_blocks_p ())
3248 int align = (TREE_CODE (decl) == CONST_DECL
3249 || (TREE_CODE (decl) == VAR_DECL
3250 && DECL_IN_CONSTANT_POOL (decl))
3251 ? DECL_ALIGN (decl)
3252 : symtab_node::get (decl)->definition_alignment ());
3253 section *sect = get_constant_section (exp, align);
3254 symbol = create_block_symbol (ggc_strdup (label),
3255 get_block_for_section (sect), -1);
3257 else
3258 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3259 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3260 SET_SYMBOL_REF_DECL (symbol, decl);
3261 TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3263 rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3264 set_mem_attributes (rtl, exp, 1);
3265 set_mem_alias_set (rtl, 0);
3267 /* We cannot share RTX'es in pool entries.
3268 Mark this piece of RTL as required for unsharing. */
3269 RTX_FLAG (rtl, used) = 1;
3271 /* Set flags or add text to the name to record information, such as
3272 that it is a local symbol. If the name is changed, the macro
3273 ASM_OUTPUT_LABELREF will have to know how to strip this
3274 information. This call might invalidate our local variable
3275 SYMBOL; we can't use it afterward. */
3276 targetm.encode_section_info (exp, rtl, true);
3278 desc->rtl = rtl;
3280 return desc;
3283 /* Return an rtx representing a reference to constant data in memory
3284 for the constant expression EXP.
3286 If assembler code for such a constant has already been output,
3287 return an rtx to refer to it.
3288 Otherwise, output such a constant in memory
3289 and generate an rtx for it.
3291 If DEFER is nonzero, this constant can be deferred and output only
3292 if referenced in the function after all optimizations.
3294 `const_desc_table' records which constants already have label strings. */
3297 output_constant_def (tree exp, int defer)
3299 struct constant_descriptor_tree *desc;
3300 struct constant_descriptor_tree key;
3302 /* Look up EXP in the table of constant descriptors. If we didn't find
3303 it, create a new one. */
3304 key.value = exp;
3305 key.hash = const_hash_1 (exp);
3306 constant_descriptor_tree **loc
3307 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3309 desc = *loc;
3310 if (desc == 0)
3312 desc = build_constant_desc (exp);
3313 desc->hash = key.hash;
3314 *loc = desc;
3317 maybe_output_constant_def_contents (desc, defer);
3318 return desc->rtl;
3321 /* Subroutine of output_constant_def: Decide whether or not we need to
3322 output the constant DESC now, and if so, do it. */
3323 static void
3324 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3325 int defer)
3327 rtx symbol = XEXP (desc->rtl, 0);
3328 tree exp = desc->value;
3330 if (flag_syntax_only)
3331 return;
3333 if (TREE_ASM_WRITTEN (exp))
3334 /* Already output; don't do it again. */
3335 return;
3337 /* We can always defer constants as long as the context allows
3338 doing so. */
3339 if (defer)
3341 /* Increment n_deferred_constants if it exists. It needs to be at
3342 least as large as the number of constants actually referred to
3343 by the function. If it's too small we'll stop looking too early
3344 and fail to emit constants; if it's too large we'll only look
3345 through the entire function when we could have stopped earlier. */
3346 if (cfun)
3347 n_deferred_constants++;
3348 return;
3351 output_constant_def_contents (symbol);
3354 /* Subroutine of output_constant_def_contents. Output the definition
3355 of constant EXP, which is pointed to by label LABEL. ALIGN is the
3356 constant's alignment in bits. */
3358 static void
3359 assemble_constant_contents (tree exp, const char *label, unsigned int align)
3361 HOST_WIDE_INT size;
3363 size = get_constant_size (exp);
3365 /* Do any machine/system dependent processing of the constant. */
3366 targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3368 /* Output the value of EXP. */
3369 output_constant (exp, size, align);
3371 targetm.asm_out.decl_end ();
3374 /* We must output the constant data referred to by SYMBOL; do so. */
3376 static void
3377 output_constant_def_contents (rtx symbol)
3379 tree decl = SYMBOL_REF_DECL (symbol);
3380 tree exp = DECL_INITIAL (decl);
3381 bool asan_protected = false;
3383 /* Make sure any other constants whose addresses appear in EXP
3384 are assigned label numbers. */
3385 output_addressed_constants (exp);
3387 /* We are no longer deferring this constant. */
3388 TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3390 if ((flag_sanitize & SANITIZE_ADDRESS)
3391 && TREE_CODE (exp) == STRING_CST
3392 && asan_protect_global (exp))
3394 asan_protected = true;
3395 DECL_ALIGN (decl) = MAX (DECL_ALIGN (decl),
3396 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
3399 /* If the constant is part of an object block, make sure that the
3400 decl has been positioned within its block, but do not write out
3401 its definition yet. output_object_blocks will do that later. */
3402 if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3403 place_block_symbol (symbol);
3404 else
3406 int align = (TREE_CODE (decl) == CONST_DECL
3407 || (TREE_CODE (decl) == VAR_DECL
3408 && DECL_IN_CONSTANT_POOL (decl))
3409 ? DECL_ALIGN (decl)
3410 : symtab_node::get (decl)->definition_alignment ());
3411 switch_to_section (get_constant_section (exp, align));
3412 if (align > BITS_PER_UNIT)
3413 ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3414 assemble_constant_contents (exp, XSTR (symbol, 0), align);
3415 if (asan_protected)
3417 HOST_WIDE_INT size = get_constant_size (exp);
3418 assemble_zeros (asan_red_zone_size (size));
3423 /* Look up EXP in the table of constant descriptors. Return the rtl
3424 if it has been emitted, else null. */
3427 lookup_constant_def (tree exp)
3429 struct constant_descriptor_tree key;
3431 key.value = exp;
3432 key.hash = const_hash_1 (exp);
3433 constant_descriptor_tree *desc
3434 = const_desc_htab->find_with_hash (&key, key.hash);
3436 return (desc ? desc->rtl : NULL_RTX);
3439 /* Return a tree representing a reference to constant data in memory
3440 for the constant expression EXP.
3442 This is the counterpart of output_constant_def at the Tree level. */
3444 tree
3445 tree_output_constant_def (tree exp)
3447 struct constant_descriptor_tree *desc, key;
3448 tree decl;
3450 /* Look up EXP in the table of constant descriptors. If we didn't find
3451 it, create a new one. */
3452 key.value = exp;
3453 key.hash = const_hash_1 (exp);
3454 constant_descriptor_tree **loc
3455 = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3457 desc = *loc;
3458 if (desc == 0)
3460 desc = build_constant_desc (exp);
3461 desc->hash = key.hash;
3462 *loc = desc;
3465 decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3466 varpool_node::finalize_decl (decl);
3467 return decl;
3470 struct GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3471 struct constant_descriptor_rtx *next;
3472 rtx mem;
3473 rtx sym;
3474 rtx constant;
3475 HOST_WIDE_INT offset;
3476 hashval_t hash;
3477 machine_mode mode;
3478 unsigned int align;
3479 int labelno;
3480 int mark;
3483 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3485 static hashval_t hash (constant_descriptor_rtx *);
3486 static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3489 /* Used in the hash tables to avoid outputting the same constant
3490 twice. Unlike 'struct constant_descriptor_tree', RTX constants
3491 are output once per function, not once per file. */
3492 /* ??? Only a few targets need per-function constant pools. Most
3493 can use one per-file pool. Should add a targetm bit to tell the
3494 difference. */
3496 struct GTY(()) rtx_constant_pool {
3497 /* Pointers to first and last constant in pool, as ordered by offset. */
3498 struct constant_descriptor_rtx *first;
3499 struct constant_descriptor_rtx *last;
3501 /* Hash facility for making memory-constants from constant rtl-expressions.
3502 It is used on RISC machines where immediate integer arguments and
3503 constant addresses are restricted so that such constants must be stored
3504 in memory. */
3505 hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3507 /* Current offset in constant pool (does not include any
3508 machine-specific header). */
3509 HOST_WIDE_INT offset;
3512 /* Hash and compare functions for const_rtx_htab. */
3514 hashval_t
3515 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3517 return desc->hash;
3520 bool
3521 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3522 constant_descriptor_rtx *y)
3524 if (x->mode != y->mode)
3525 return 0;
3526 return rtx_equal_p (x->constant, y->constant);
3529 /* Hash one component of a constant. */
3531 static hashval_t
3532 const_rtx_hash_1 (const_rtx x)
3534 unsigned HOST_WIDE_INT hwi;
3535 machine_mode mode;
3536 enum rtx_code code;
3537 hashval_t h;
3538 int i;
3540 code = GET_CODE (x);
3541 mode = GET_MODE (x);
3542 h = (hashval_t) code * 1048573 + mode;
3544 switch (code)
3546 case CONST_INT:
3547 hwi = INTVAL (x);
3549 fold_hwi:
3551 int shift = sizeof (hashval_t) * CHAR_BIT;
3552 const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3554 h ^= (hashval_t) hwi;
3555 for (i = 1; i < n; ++i)
3557 hwi >>= shift;
3558 h ^= (hashval_t) hwi;
3561 break;
3563 case CONST_WIDE_INT:
3564 hwi = GET_MODE_PRECISION (mode);
3566 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3567 hwi ^= CONST_WIDE_INT_ELT (x, i);
3568 goto fold_hwi;
3571 case CONST_DOUBLE:
3572 if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3574 hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3575 goto fold_hwi;
3577 else
3578 h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3579 break;
3581 case CONST_FIXED:
3582 h ^= fixed_hash (CONST_FIXED_VALUE (x));
3583 break;
3585 case SYMBOL_REF:
3586 h ^= htab_hash_string (XSTR (x, 0));
3587 break;
3589 case LABEL_REF:
3590 h = h * 251 + CODE_LABEL_NUMBER (LABEL_REF_LABEL (x));
3591 break;
3593 case UNSPEC:
3594 case UNSPEC_VOLATILE:
3595 h = h * 251 + XINT (x, 1);
3596 break;
3598 default:
3599 break;
3602 return h;
3605 /* Compute a hash value for X, which should be a constant. */
3607 static hashval_t
3608 const_rtx_hash (rtx x)
3610 hashval_t h = 0;
3611 subrtx_iterator::array_type array;
3612 FOR_EACH_SUBRTX (iter, array, x, ALL)
3613 h = h * 509 + const_rtx_hash_1 (*iter);
3614 return h;
3618 /* Create and return a new rtx constant pool. */
3620 static struct rtx_constant_pool *
3621 create_constant_pool (void)
3623 struct rtx_constant_pool *pool;
3625 pool = ggc_alloc<rtx_constant_pool> ();
3626 pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3627 pool->first = NULL;
3628 pool->last = NULL;
3629 pool->offset = 0;
3630 return pool;
3633 /* Initialize constant pool hashing for a new function. */
3635 void
3636 init_varasm_status (void)
3638 crtl->varasm.pool = create_constant_pool ();
3639 crtl->varasm.deferred_constants = 0;
3642 /* Given a MINUS expression, simplify it if both sides
3643 include the same symbol. */
3646 simplify_subtraction (rtx x)
3648 rtx r = simplify_rtx (x);
3649 return r ? r : x;
3652 /* Given a constant rtx X, make (or find) a memory constant for its value
3653 and return a MEM rtx to refer to it in memory. */
3656 force_const_mem (machine_mode mode, rtx x)
3658 struct constant_descriptor_rtx *desc, tmp;
3659 struct rtx_constant_pool *pool;
3660 char label[256];
3661 rtx def, symbol;
3662 hashval_t hash;
3663 unsigned int align;
3664 constant_descriptor_rtx **slot;
3666 /* If we're not allowed to drop X into the constant pool, don't. */
3667 if (targetm.cannot_force_const_mem (mode, x))
3668 return NULL_RTX;
3670 /* Record that this function has used a constant pool entry. */
3671 crtl->uses_const_pool = 1;
3673 /* Decide which pool to use. */
3674 pool = (targetm.use_blocks_for_constant_p (mode, x)
3675 ? shared_constant_pool
3676 : crtl->varasm.pool);
3678 /* Lookup the value in the hashtable. */
3679 tmp.constant = x;
3680 tmp.mode = mode;
3681 hash = const_rtx_hash (x);
3682 slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3683 desc = *slot;
3685 /* If the constant was already present, return its memory. */
3686 if (desc)
3687 return copy_rtx (desc->mem);
3689 /* Otherwise, create a new descriptor. */
3690 desc = ggc_alloc<constant_descriptor_rtx> ();
3691 *slot = desc;
3693 /* Align the location counter as required by EXP's data type. */
3694 align = GET_MODE_ALIGNMENT (mode == VOIDmode ? word_mode : mode);
3696 tree type = lang_hooks.types.type_for_mode (mode, 0);
3697 if (type != NULL_TREE)
3698 align = CONSTANT_ALIGNMENT (make_tree (type, x), align);
3700 pool->offset += (align / BITS_PER_UNIT) - 1;
3701 pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3703 desc->next = NULL;
3704 desc->constant = copy_rtx (tmp.constant);
3705 desc->offset = pool->offset;
3706 desc->hash = hash;
3707 desc->mode = mode;
3708 desc->align = align;
3709 desc->labelno = const_labelno;
3710 desc->mark = 0;
3712 pool->offset += GET_MODE_SIZE (mode);
3713 if (pool->last)
3714 pool->last->next = desc;
3715 else
3716 pool->first = pool->last = desc;
3717 pool->last = desc;
3719 /* Create a string containing the label name, in LABEL. */
3720 ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3721 ++const_labelno;
3723 /* Construct the SYMBOL_REF. Make sure to mark it as belonging to
3724 the constants pool. */
3725 if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3727 section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3728 symbol = create_block_symbol (ggc_strdup (label),
3729 get_block_for_section (sect), -1);
3731 else
3732 symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3733 desc->sym = symbol;
3734 SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3735 CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3736 SET_SYMBOL_REF_CONSTANT (symbol, desc);
3738 /* Construct the MEM. */
3739 desc->mem = def = gen_const_mem (mode, symbol);
3740 set_mem_attributes (def, lang_hooks.types.type_for_mode (mode, 0), 1);
3741 set_mem_align (def, align);
3743 /* If we're dropping a label to the constant pool, make sure we
3744 don't delete it. */
3745 if (GET_CODE (x) == LABEL_REF)
3746 LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3748 return copy_rtx (def);
3751 /* Given a constant pool SYMBOL_REF, return the corresponding constant. */
3754 get_pool_constant (const_rtx addr)
3756 return SYMBOL_REF_CONSTANT (addr)->constant;
3759 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3760 and whether it has been output or not. */
3763 get_pool_constant_mark (rtx addr, bool *pmarked)
3765 struct constant_descriptor_rtx *desc;
3767 desc = SYMBOL_REF_CONSTANT (addr);
3768 *pmarked = (desc->mark != 0);
3769 return desc->constant;
3772 /* Similar, return the mode. */
3774 machine_mode
3775 get_pool_mode (const_rtx addr)
3777 return SYMBOL_REF_CONSTANT (addr)->mode;
3780 /* Return the size of the constant pool. */
3783 get_pool_size (void)
3785 return crtl->varasm.pool->offset;
3788 /* Worker function for output_constant_pool_1. Emit assembly for X
3789 in MODE with known alignment ALIGN. */
3791 static void
3792 output_constant_pool_2 (machine_mode mode, rtx x, unsigned int align)
3794 switch (GET_MODE_CLASS (mode))
3796 case MODE_FLOAT:
3797 case MODE_DECIMAL_FLOAT:
3799 gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3800 assemble_real (*CONST_DOUBLE_REAL_VALUE (x), mode, align);
3801 break;
3804 case MODE_INT:
3805 case MODE_PARTIAL_INT:
3806 case MODE_FRACT:
3807 case MODE_UFRACT:
3808 case MODE_ACCUM:
3809 case MODE_UACCUM:
3810 case MODE_POINTER_BOUNDS:
3811 assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3812 break;
3814 case MODE_VECTOR_FLOAT:
3815 case MODE_VECTOR_INT:
3816 case MODE_VECTOR_FRACT:
3817 case MODE_VECTOR_UFRACT:
3818 case MODE_VECTOR_ACCUM:
3819 case MODE_VECTOR_UACCUM:
3821 int i, units;
3822 machine_mode submode = GET_MODE_INNER (mode);
3823 unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
3825 gcc_assert (GET_CODE (x) == CONST_VECTOR);
3826 units = CONST_VECTOR_NUNITS (x);
3828 for (i = 0; i < units; i++)
3830 rtx elt = CONST_VECTOR_ELT (x, i);
3831 output_constant_pool_2 (submode, elt, i ? subalign : align);
3834 break;
3836 default:
3837 gcc_unreachable ();
3841 /* Worker function for output_constant_pool. Emit constant DESC,
3842 giving it ALIGN bits of alignment. */
3844 static void
3845 output_constant_pool_1 (struct constant_descriptor_rtx *desc,
3846 unsigned int align)
3848 rtx x, tmp;
3850 x = desc->constant;
3852 /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
3853 whose CODE_LABEL has been deleted. This can occur if a jump table
3854 is eliminated by optimization. If so, write a constant of zero
3855 instead. Note that this can also happen by turning the
3856 CODE_LABEL into a NOTE. */
3857 /* ??? This seems completely and utterly wrong. Certainly it's
3858 not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
3859 functioning even with rtx_insn::deleted and friends. */
3861 tmp = x;
3862 switch (GET_CODE (tmp))
3864 case CONST:
3865 if (GET_CODE (XEXP (tmp, 0)) != PLUS
3866 || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
3867 break;
3868 tmp = XEXP (XEXP (tmp, 0), 0);
3869 /* FALLTHRU */
3871 case LABEL_REF:
3872 tmp = LABEL_REF_LABEL (tmp);
3873 gcc_assert (!as_a<rtx_insn *> (tmp)->deleted ());
3874 gcc_assert (!NOTE_P (tmp)
3875 || NOTE_KIND (tmp) != NOTE_INSN_DELETED);
3876 break;
3878 default:
3879 break;
3882 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3883 ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
3884 align, desc->labelno, done);
3885 #endif
3887 assemble_align (align);
3889 /* Output the label. */
3890 targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
3892 /* Output the data.
3893 Pass actual alignment value while emitting string constant to asm code
3894 as function 'output_constant_pool_1' explicitly passes the alignment as 1
3895 assuming that the data is already aligned which prevents the generation
3896 of fix-up table entries. */
3897 output_constant_pool_2 (desc->mode, x, desc->align);
3899 /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
3900 sections have proper size. */
3901 if (align > GET_MODE_BITSIZE (desc->mode)
3902 && in_section
3903 && (in_section->common.flags & SECTION_MERGE))
3904 assemble_align (align);
3906 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
3907 done:
3908 #endif
3909 return;
3912 /* Mark all constants that are referenced by SYMBOL_REFs in X.
3913 Emit referenced deferred strings. */
3915 static void
3916 mark_constants_in_pattern (rtx insn)
3918 subrtx_iterator::array_type array;
3919 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
3921 const_rtx x = *iter;
3922 if (GET_CODE (x) == SYMBOL_REF)
3924 if (CONSTANT_POOL_ADDRESS_P (x))
3926 struct constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
3927 if (desc->mark == 0)
3929 desc->mark = 1;
3930 iter.substitute (desc->constant);
3933 else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
3935 tree decl = SYMBOL_REF_DECL (x);
3936 if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
3938 n_deferred_constants--;
3939 output_constant_def_contents (CONST_CAST_RTX (x));
3946 /* Look through appropriate parts of INSN, marking all entries in the
3947 constant pool which are actually being used. Entries that are only
3948 referenced by other constants are also marked as used. Emit
3949 deferred strings that are used. */
3951 static void
3952 mark_constants (rtx_insn *insn)
3954 if (!INSN_P (insn))
3955 return;
3957 /* Insns may appear inside a SEQUENCE. Only check the patterns of
3958 insns, not any notes that may be attached. We don't want to mark
3959 a constant just because it happens to appear in a REG_EQUIV note. */
3960 if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
3962 int i, n = seq->len ();
3963 for (i = 0; i < n; ++i)
3965 rtx subinsn = seq->element (i);
3966 if (INSN_P (subinsn))
3967 mark_constants_in_pattern (subinsn);
3970 else
3971 mark_constants_in_pattern (insn);
3974 /* Look through the instructions for this function, and mark all the
3975 entries in POOL which are actually being used. Emit deferred constants
3976 which have indeed been used. */
3978 static void
3979 mark_constant_pool (void)
3981 rtx_insn *insn;
3983 if (!crtl->uses_const_pool && n_deferred_constants == 0)
3984 return;
3986 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3987 mark_constants (insn);
3990 /* Write all the constants in POOL. */
3992 static void
3993 output_constant_pool_contents (struct rtx_constant_pool *pool)
3995 struct constant_descriptor_rtx *desc;
3997 for (desc = pool->first; desc ; desc = desc->next)
3998 if (desc->mark)
4000 /* If the constant is part of an object_block, make sure that
4001 the constant has been positioned within its block, but do not
4002 write out its definition yet. output_object_blocks will do
4003 that later. */
4004 if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4005 && SYMBOL_REF_BLOCK (desc->sym))
4006 place_block_symbol (desc->sym);
4007 else
4009 switch_to_section (targetm.asm_out.select_rtx_section
4010 (desc->mode, desc->constant, desc->align));
4011 output_constant_pool_1 (desc, desc->align);
4016 /* Mark all constants that are used in the current function, then write
4017 out the function's private constant pool. */
4019 static void
4020 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4021 tree fndecl ATTRIBUTE_UNUSED)
4023 struct rtx_constant_pool *pool = crtl->varasm.pool;
4025 /* It is possible for gcc to call force_const_mem and then to later
4026 discard the instructions which refer to the constant. In such a
4027 case we do not need to output the constant. */
4028 mark_constant_pool ();
4030 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4031 ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4032 #endif
4034 output_constant_pool_contents (pool);
4036 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4037 ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4038 #endif
4041 /* Write the contents of the shared constant pool. */
4043 void
4044 output_shared_constant_pool (void)
4046 output_constant_pool_contents (shared_constant_pool);
4049 /* Determine what kind of relocations EXP may need. */
4052 compute_reloc_for_constant (tree exp)
4054 int reloc = 0, reloc2;
4055 tree tem;
4057 switch (TREE_CODE (exp))
4059 case ADDR_EXPR:
4060 case FDESC_EXPR:
4061 /* Go inside any operations that get_inner_reference can handle and see
4062 if what's inside is a constant: no need to do anything here for
4063 addresses of variables or functions. */
4064 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4065 tem = TREE_OPERAND (tem, 0))
4068 if (TREE_CODE (tem) == MEM_REF
4069 && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4071 reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4072 break;
4075 if (!targetm.binds_local_p (tem))
4076 reloc |= 2;
4077 else
4078 reloc |= 1;
4079 break;
4081 case PLUS_EXPR:
4082 case POINTER_PLUS_EXPR:
4083 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4084 reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4085 break;
4087 case MINUS_EXPR:
4088 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4089 reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4090 /* The difference of two local labels is computable at link time. */
4091 if (reloc == 1 && reloc2 == 1)
4092 reloc = 0;
4093 else
4094 reloc |= reloc2;
4095 break;
4097 CASE_CONVERT:
4098 case VIEW_CONVERT_EXPR:
4099 reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4100 break;
4102 case CONSTRUCTOR:
4104 unsigned HOST_WIDE_INT idx;
4105 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4106 if (tem != 0)
4107 reloc |= compute_reloc_for_constant (tem);
4109 break;
4111 default:
4112 break;
4114 return reloc;
4117 /* Find all the constants whose addresses are referenced inside of EXP,
4118 and make sure assembler code with a label has been output for each one.
4119 Indicate whether an ADDR_EXPR has been encountered. */
4121 static void
4122 output_addressed_constants (tree exp)
4124 tree tem;
4126 switch (TREE_CODE (exp))
4128 case ADDR_EXPR:
4129 case FDESC_EXPR:
4130 /* Go inside any operations that get_inner_reference can handle and see
4131 if what's inside is a constant: no need to do anything here for
4132 addresses of variables or functions. */
4133 for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4134 tem = TREE_OPERAND (tem, 0))
4137 /* If we have an initialized CONST_DECL, retrieve the initializer. */
4138 if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4139 tem = DECL_INITIAL (tem);
4141 if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4142 output_constant_def (tem, 0);
4144 if (TREE_CODE (tem) == MEM_REF)
4145 output_addressed_constants (TREE_OPERAND (tem, 0));
4146 break;
4148 case PLUS_EXPR:
4149 case POINTER_PLUS_EXPR:
4150 case MINUS_EXPR:
4151 output_addressed_constants (TREE_OPERAND (exp, 1));
4152 /* Fall through. */
4154 CASE_CONVERT:
4155 case VIEW_CONVERT_EXPR:
4156 output_addressed_constants (TREE_OPERAND (exp, 0));
4157 break;
4159 case CONSTRUCTOR:
4161 unsigned HOST_WIDE_INT idx;
4162 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4163 if (tem != 0)
4164 output_addressed_constants (tem);
4166 break;
4168 default:
4169 break;
4173 /* Whether a constructor CTOR is a valid static constant initializer if all
4174 its elements are. This used to be internal to initializer_constant_valid_p
4175 and has been exposed to let other functions like categorize_ctor_elements
4176 evaluate the property while walking a constructor for other purposes. */
4178 bool
4179 constructor_static_from_elts_p (const_tree ctor)
4181 return (TREE_CONSTANT (ctor)
4182 && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4183 || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4184 || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4187 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4188 tree *cache);
4190 /* A subroutine of initializer_constant_valid_p. VALUE is a MINUS_EXPR,
4191 PLUS_EXPR or POINTER_PLUS_EXPR. This looks for cases of VALUE
4192 which are valid when ENDTYPE is an integer of any size; in
4193 particular, this does not accept a pointer minus a constant. This
4194 returns null_pointer_node if the VALUE is an absolute constant
4195 which can be used to initialize a static variable. Otherwise it
4196 returns NULL. */
4198 static tree
4199 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4201 tree op0, op1;
4203 if (!INTEGRAL_TYPE_P (endtype))
4204 return NULL_TREE;
4206 op0 = TREE_OPERAND (value, 0);
4207 op1 = TREE_OPERAND (value, 1);
4209 /* Like STRIP_NOPS except allow the operand mode to widen. This
4210 works around a feature of fold that simplifies (int)(p1 - p2) to
4211 ((int)p1 - (int)p2) under the theory that the narrower operation
4212 is cheaper. */
4214 while (CONVERT_EXPR_P (op0)
4215 || TREE_CODE (op0) == NON_LVALUE_EXPR)
4217 tree inner = TREE_OPERAND (op0, 0);
4218 if (inner == error_mark_node
4219 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4220 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))
4221 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4222 break;
4223 op0 = inner;
4226 while (CONVERT_EXPR_P (op1)
4227 || TREE_CODE (op1) == NON_LVALUE_EXPR)
4229 tree inner = TREE_OPERAND (op1, 0);
4230 if (inner == error_mark_node
4231 || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4232 || (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op1)))
4233 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (inner)))))
4234 break;
4235 op1 = inner;
4238 op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4239 if (!op0)
4240 return NULL_TREE;
4242 op1 = initializer_constant_valid_p_1 (op1, endtype,
4243 cache ? cache + 2 : NULL);
4244 /* Both initializers must be known. */
4245 if (op1)
4247 if (op0 == op1
4248 && (op0 == null_pointer_node
4249 || TREE_CODE (value) == MINUS_EXPR))
4250 return null_pointer_node;
4252 /* Support differences between labels. */
4253 if (TREE_CODE (op0) == LABEL_DECL
4254 && TREE_CODE (op1) == LABEL_DECL)
4255 return null_pointer_node;
4257 if (TREE_CODE (op0) == STRING_CST
4258 && TREE_CODE (op1) == STRING_CST
4259 && operand_equal_p (op0, op1, 1))
4260 return null_pointer_node;
4263 return NULL_TREE;
4266 /* Helper function of initializer_constant_valid_p.
4267 Return nonzero if VALUE is a valid constant-valued expression
4268 for use in initializing a static variable; one that can be an
4269 element of a "constant" initializer.
4271 Return null_pointer_node if the value is absolute;
4272 if it is relocatable, return the variable that determines the relocation.
4273 We assume that VALUE has been folded as much as possible;
4274 therefore, we do not need to check for such things as
4275 arithmetic-combinations of integers.
4277 Use CACHE (pointer to 2 tree values) for caching if non-NULL. */
4279 static tree
4280 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4282 tree ret;
4284 switch (TREE_CODE (value))
4286 case CONSTRUCTOR:
4287 if (constructor_static_from_elts_p (value))
4289 unsigned HOST_WIDE_INT idx;
4290 tree elt;
4291 bool absolute = true;
4293 if (cache && cache[0] == value)
4294 return cache[1];
4295 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4297 tree reloc;
4298 reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4299 NULL);
4300 if (!reloc)
4302 if (cache)
4304 cache[0] = value;
4305 cache[1] = NULL_TREE;
4307 return NULL_TREE;
4309 if (reloc != null_pointer_node)
4310 absolute = false;
4312 /* For a non-absolute relocation, there is no single
4313 variable that can be "the variable that determines the
4314 relocation." */
4315 if (cache)
4317 cache[0] = value;
4318 cache[1] = absolute ? null_pointer_node : error_mark_node;
4320 return absolute ? null_pointer_node : error_mark_node;
4323 return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4325 case INTEGER_CST:
4326 case VECTOR_CST:
4327 case REAL_CST:
4328 case FIXED_CST:
4329 case STRING_CST:
4330 case COMPLEX_CST:
4331 return null_pointer_node;
4333 case ADDR_EXPR:
4334 case FDESC_EXPR:
4336 tree op0 = staticp (TREE_OPERAND (value, 0));
4337 if (op0)
4339 /* "&(*a).f" is like unto pointer arithmetic. If "a" turns out
4340 to be a constant, this is old-skool offsetof-like nonsense. */
4341 if (TREE_CODE (op0) == INDIRECT_REF
4342 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4343 return null_pointer_node;
4344 /* Taking the address of a nested function involves a trampoline,
4345 unless we don't need or want one. */
4346 if (TREE_CODE (op0) == FUNCTION_DECL
4347 && DECL_STATIC_CHAIN (op0)
4348 && !TREE_NO_TRAMPOLINE (value))
4349 return NULL_TREE;
4350 /* "&{...}" requires a temporary to hold the constructed
4351 object. */
4352 if (TREE_CODE (op0) == CONSTRUCTOR)
4353 return NULL_TREE;
4355 return op0;
4358 case NON_LVALUE_EXPR:
4359 return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4360 endtype, cache);
4362 case VIEW_CONVERT_EXPR:
4364 tree src = TREE_OPERAND (value, 0);
4365 tree src_type = TREE_TYPE (src);
4366 tree dest_type = TREE_TYPE (value);
4368 /* Allow view-conversions from aggregate to non-aggregate type only
4369 if the bit pattern is fully preserved afterwards; otherwise, the
4370 RTL expander won't be able to apply a subsequent transformation
4371 to the underlying constructor. */
4372 if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4374 if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4375 return initializer_constant_valid_p_1 (src, endtype, cache);
4376 else
4377 return NULL_TREE;
4380 /* Allow all other kinds of view-conversion. */
4381 return initializer_constant_valid_p_1 (src, endtype, cache);
4384 CASE_CONVERT:
4386 tree src = TREE_OPERAND (value, 0);
4387 tree src_type = TREE_TYPE (src);
4388 tree dest_type = TREE_TYPE (value);
4390 /* Allow conversions between pointer types, floating-point
4391 types, and offset types. */
4392 if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4393 || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4394 || (TREE_CODE (dest_type) == OFFSET_TYPE
4395 && TREE_CODE (src_type) == OFFSET_TYPE))
4396 return initializer_constant_valid_p_1 (src, endtype, cache);
4398 /* Allow length-preserving conversions between integer types. */
4399 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4400 && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4401 return initializer_constant_valid_p_1 (src, endtype, cache);
4403 /* Allow conversions between other integer types only if
4404 explicit value. */
4405 if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type))
4407 tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4408 if (inner == null_pointer_node)
4409 return null_pointer_node;
4410 break;
4413 /* Allow (int) &foo provided int is as wide as a pointer. */
4414 if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4415 && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4416 return initializer_constant_valid_p_1 (src, endtype, cache);
4418 /* Likewise conversions from int to pointers, but also allow
4419 conversions from 0. */
4420 if ((POINTER_TYPE_P (dest_type)
4421 || TREE_CODE (dest_type) == OFFSET_TYPE)
4422 && INTEGRAL_TYPE_P (src_type))
4424 if (TREE_CODE (src) == INTEGER_CST
4425 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4426 return null_pointer_node;
4427 if (integer_zerop (src))
4428 return null_pointer_node;
4429 else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4430 return initializer_constant_valid_p_1 (src, endtype, cache);
4433 /* Allow conversions to struct or union types if the value
4434 inside is okay. */
4435 if (TREE_CODE (dest_type) == RECORD_TYPE
4436 || TREE_CODE (dest_type) == UNION_TYPE)
4437 return initializer_constant_valid_p_1 (src, endtype, cache);
4439 break;
4441 case POINTER_PLUS_EXPR:
4442 case PLUS_EXPR:
4443 /* Any valid floating-point constants will have been folded by now;
4444 with -frounding-math we hit this with addition of two constants. */
4445 if (TREE_CODE (endtype) == REAL_TYPE)
4446 return NULL_TREE;
4447 if (cache && cache[0] == value)
4448 return cache[1];
4449 if (! INTEGRAL_TYPE_P (endtype)
4450 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4452 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4453 tree valid0
4454 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4455 endtype, ncache);
4456 tree valid1
4457 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4458 endtype, ncache + 2);
4459 /* If either term is absolute, use the other term's relocation. */
4460 if (valid0 == null_pointer_node)
4461 ret = valid1;
4462 else if (valid1 == null_pointer_node)
4463 ret = valid0;
4464 /* Support narrowing pointer differences. */
4465 else
4466 ret = narrowing_initializer_constant_valid_p (value, endtype,
4467 ncache);
4469 else
4470 /* Support narrowing pointer differences. */
4471 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4472 if (cache)
4474 cache[0] = value;
4475 cache[1] = ret;
4477 return ret;
4479 case MINUS_EXPR:
4480 if (TREE_CODE (endtype) == REAL_TYPE)
4481 return NULL_TREE;
4482 if (cache && cache[0] == value)
4483 return cache[1];
4484 if (! INTEGRAL_TYPE_P (endtype)
4485 || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4487 tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4488 tree valid0
4489 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4490 endtype, ncache);
4491 tree valid1
4492 = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4493 endtype, ncache + 2);
4494 /* Win if second argument is absolute. */
4495 if (valid1 == null_pointer_node)
4496 ret = valid0;
4497 /* Win if both arguments have the same relocation.
4498 Then the value is absolute. */
4499 else if (valid0 == valid1 && valid0 != 0)
4500 ret = null_pointer_node;
4501 /* Since GCC guarantees that string constants are unique in the
4502 generated code, a subtraction between two copies of the same
4503 constant string is absolute. */
4504 else if (valid0 && TREE_CODE (valid0) == STRING_CST
4505 && valid1 && TREE_CODE (valid1) == STRING_CST
4506 && operand_equal_p (valid0, valid1, 1))
4507 ret = null_pointer_node;
4508 /* Support narrowing differences. */
4509 else
4510 ret = narrowing_initializer_constant_valid_p (value, endtype,
4511 ncache);
4513 else
4514 /* Support narrowing differences. */
4515 ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4516 if (cache)
4518 cache[0] = value;
4519 cache[1] = ret;
4521 return ret;
4523 default:
4524 break;
4527 return NULL_TREE;
4530 /* Return nonzero if VALUE is a valid constant-valued expression
4531 for use in initializing a static variable; one that can be an
4532 element of a "constant" initializer.
4534 Return null_pointer_node if the value is absolute;
4535 if it is relocatable, return the variable that determines the relocation.
4536 We assume that VALUE has been folded as much as possible;
4537 therefore, we do not need to check for such things as
4538 arithmetic-combinations of integers. */
4539 tree
4540 initializer_constant_valid_p (tree value, tree endtype)
4542 return initializer_constant_valid_p_1 (value, endtype, NULL);
4545 /* Return true if VALUE is a valid constant-valued expression
4546 for use in initializing a static bit-field; one that can be
4547 an element of a "constant" initializer. */
4549 bool
4550 initializer_constant_valid_for_bitfield_p (tree value)
4552 /* For bitfields we support integer constants or possibly nested aggregates
4553 of such. */
4554 switch (TREE_CODE (value))
4556 case CONSTRUCTOR:
4558 unsigned HOST_WIDE_INT idx;
4559 tree elt;
4561 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4562 if (!initializer_constant_valid_for_bitfield_p (elt))
4563 return false;
4564 return true;
4567 case INTEGER_CST:
4568 case REAL_CST:
4569 return true;
4571 case VIEW_CONVERT_EXPR:
4572 case NON_LVALUE_EXPR:
4573 return
4574 initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4576 default:
4577 break;
4580 return false;
4583 /* output_constructor outer state of relevance in recursive calls, typically
4584 for nested aggregate bitfields. */
4586 struct oc_outer_state {
4587 unsigned int bit_offset; /* current position in ... */
4588 int byte; /* ... the outer byte buffer. */
4591 static unsigned HOST_WIDE_INT
4592 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int,
4593 oc_outer_state *);
4595 /* Output assembler code for constant EXP, with no label.
4596 This includes the pseudo-op such as ".int" or ".byte", and a newline.
4597 Assumes output_addressed_constants has been done on EXP already.
4599 Generate at least SIZE bytes of assembler data, padding at the end
4600 with zeros if necessary. SIZE must always be specified. The returned
4601 value is the actual number of bytes of assembler data generated, which
4602 may be bigger than SIZE if the object contains a variable length field.
4604 SIZE is important for structure constructors,
4605 since trailing members may have been omitted from the constructor.
4606 It is also important for initialization of arrays from string constants
4607 since the full length of the string constant might not be wanted.
4608 It is also needed for initialization of unions, where the initializer's
4609 type is just one member, and that may not be as long as the union.
4611 There a case in which we would fail to output exactly SIZE bytes:
4612 for a structure constructor that wants to produce more than SIZE bytes.
4613 But such constructors will never be generated for any possible input.
4615 ALIGN is the alignment of the data in bits. */
4617 static unsigned HOST_WIDE_INT
4618 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align)
4620 enum tree_code code;
4621 unsigned HOST_WIDE_INT thissize;
4623 if (size == 0 || flag_syntax_only)
4624 return size;
4626 /* See if we're trying to initialize a pointer in a non-default mode
4627 to the address of some declaration somewhere. If the target says
4628 the mode is valid for pointers, assume the target has a way of
4629 resolving it. */
4630 if (TREE_CODE (exp) == NOP_EXPR
4631 && POINTER_TYPE_P (TREE_TYPE (exp))
4632 && targetm.addr_space.valid_pointer_mode
4633 (TYPE_MODE (TREE_TYPE (exp)),
4634 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4636 tree saved_type = TREE_TYPE (exp);
4638 /* Peel off any intermediate conversions-to-pointer for valid
4639 pointer modes. */
4640 while (TREE_CODE (exp) == NOP_EXPR
4641 && POINTER_TYPE_P (TREE_TYPE (exp))
4642 && targetm.addr_space.valid_pointer_mode
4643 (TYPE_MODE (TREE_TYPE (exp)),
4644 TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4645 exp = TREE_OPERAND (exp, 0);
4647 /* If what we're left with is the address of something, we can
4648 convert the address to the final type and output it that
4649 way. */
4650 if (TREE_CODE (exp) == ADDR_EXPR)
4651 exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4652 /* Likewise for constant ints. */
4653 else if (TREE_CODE (exp) == INTEGER_CST)
4654 exp = fold_convert (saved_type, exp);
4658 /* Eliminate any conversions since we'll be outputting the underlying
4659 constant. */
4660 while (CONVERT_EXPR_P (exp)
4661 || TREE_CODE (exp) == NON_LVALUE_EXPR
4662 || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4664 HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4665 HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4667 /* Make sure eliminating the conversion is really a no-op, except with
4668 VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4669 union types to allow for Ada unchecked unions. */
4670 if (type_size > op_size
4671 && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4672 && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4673 /* Keep the conversion. */
4674 break;
4675 else
4676 exp = TREE_OPERAND (exp, 0);
4679 code = TREE_CODE (TREE_TYPE (exp));
4680 thissize = int_size_in_bytes (TREE_TYPE (exp));
4682 /* Allow a constructor with no elements for any data type.
4683 This means to fill the space with zeros. */
4684 if (TREE_CODE (exp) == CONSTRUCTOR
4685 && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4687 assemble_zeros (size);
4688 return size;
4691 if (TREE_CODE (exp) == FDESC_EXPR)
4693 #ifdef ASM_OUTPUT_FDESC
4694 HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4695 tree decl = TREE_OPERAND (exp, 0);
4696 ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4697 #else
4698 gcc_unreachable ();
4699 #endif
4700 return size;
4703 /* Now output the underlying data. If we've handling the padding, return.
4704 Otherwise, break and ensure SIZE is the size written. */
4705 switch (code)
4707 case BOOLEAN_TYPE:
4708 case INTEGER_TYPE:
4709 case ENUMERAL_TYPE:
4710 case POINTER_TYPE:
4711 case REFERENCE_TYPE:
4712 case OFFSET_TYPE:
4713 case FIXED_POINT_TYPE:
4714 case POINTER_BOUNDS_TYPE:
4715 case NULLPTR_TYPE:
4716 if (! assemble_integer (expand_expr (exp, NULL_RTX, VOIDmode,
4717 EXPAND_INITIALIZER),
4718 MIN (size, thissize), align, 0))
4719 error ("initializer for integer/fixed-point value is too complicated");
4720 break;
4722 case REAL_TYPE:
4723 if (TREE_CODE (exp) != REAL_CST)
4724 error ("initializer for floating value is not a floating constant");
4725 else
4726 assemble_real (TREE_REAL_CST (exp), TYPE_MODE (TREE_TYPE (exp)), align);
4727 break;
4729 case COMPLEX_TYPE:
4730 output_constant (TREE_REALPART (exp), thissize / 2, align);
4731 output_constant (TREE_IMAGPART (exp), thissize / 2,
4732 min_align (align, BITS_PER_UNIT * (thissize / 2)));
4733 break;
4735 case ARRAY_TYPE:
4736 case VECTOR_TYPE:
4737 switch (TREE_CODE (exp))
4739 case CONSTRUCTOR:
4740 return output_constructor (exp, size, align, NULL);
4741 case STRING_CST:
4742 thissize
4743 = MIN ((unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp), size);
4744 assemble_string (TREE_STRING_POINTER (exp), thissize);
4745 break;
4746 case VECTOR_CST:
4748 machine_mode inner = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
4749 unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
4750 int elt_size = GET_MODE_SIZE (inner);
4751 output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align);
4752 thissize = elt_size;
4753 for (unsigned int i = 1; i < VECTOR_CST_NELTS (exp); i++)
4755 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign);
4756 thissize += elt_size;
4758 break;
4760 default:
4761 gcc_unreachable ();
4763 break;
4765 case RECORD_TYPE:
4766 case UNION_TYPE:
4767 gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
4768 return output_constructor (exp, size, align, NULL);
4770 case ERROR_MARK:
4771 return 0;
4773 default:
4774 gcc_unreachable ();
4777 if (size > thissize)
4778 assemble_zeros (size - thissize);
4780 return size;
4784 /* Subroutine of output_constructor, used for computing the size of
4785 arrays of unspecified length. VAL must be a CONSTRUCTOR of an array
4786 type with an unspecified upper bound. */
4788 static unsigned HOST_WIDE_INT
4789 array_size_for_constructor (tree val)
4791 tree max_index;
4792 unsigned HOST_WIDE_INT cnt;
4793 tree index, value, tmp;
4794 offset_int i;
4796 /* This code used to attempt to handle string constants that are not
4797 arrays of single-bytes, but nothing else does, so there's no point in
4798 doing it here. */
4799 if (TREE_CODE (val) == STRING_CST)
4800 return TREE_STRING_LENGTH (val);
4802 max_index = NULL_TREE;
4803 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
4805 if (TREE_CODE (index) == RANGE_EXPR)
4806 index = TREE_OPERAND (index, 1);
4807 if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
4808 max_index = index;
4811 if (max_index == NULL_TREE)
4812 return 0;
4814 /* Compute the total number of array elements. */
4815 tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
4816 i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
4818 /* Multiply by the array element unit size to find number of bytes. */
4819 i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
4821 gcc_assert (wi::fits_uhwi_p (i));
4822 return i.to_uhwi ();
4825 /* Other datastructures + helpers for output_constructor. */
4827 /* output_constructor local state to support interaction with helpers. */
4829 struct oc_local_state {
4831 /* Received arguments. */
4832 tree exp; /* Constructor expression. */
4833 tree type; /* Type of constructor expression. */
4834 unsigned HOST_WIDE_INT size; /* # bytes to output - pad if necessary. */
4835 unsigned int align; /* Known initial alignment. */
4836 tree min_index; /* Lower bound if specified for an array. */
4838 /* Output processing state. */
4839 HOST_WIDE_INT total_bytes; /* # bytes output so far / current position. */
4840 int byte; /* Part of a bitfield byte yet to be output. */
4841 int last_relative_index; /* Implicit or explicit index of the last
4842 array element output within a bitfield. */
4843 bool byte_buffer_in_use; /* Whether BYTE is in use. */
4845 /* Current element. */
4846 tree field; /* Current field decl in a record. */
4847 tree val; /* Current element value. */
4848 tree index; /* Current element index. */
4852 /* Helper for output_constructor. From the current LOCAL state, output a
4853 RANGE_EXPR element. */
4855 static void
4856 output_constructor_array_range (oc_local_state *local)
4858 unsigned HOST_WIDE_INT fieldsize
4859 = int_size_in_bytes (TREE_TYPE (local->type));
4861 HOST_WIDE_INT lo_index
4862 = tree_to_shwi (TREE_OPERAND (local->index, 0));
4863 HOST_WIDE_INT hi_index
4864 = tree_to_shwi (TREE_OPERAND (local->index, 1));
4865 HOST_WIDE_INT index;
4867 unsigned int align2
4868 = min_align (local->align, fieldsize * BITS_PER_UNIT);
4870 for (index = lo_index; index <= hi_index; index++)
4872 /* Output the element's initial value. */
4873 if (local->val == NULL_TREE)
4874 assemble_zeros (fieldsize);
4875 else
4876 fieldsize = output_constant (local->val, fieldsize, align2);
4878 /* Count its size. */
4879 local->total_bytes += fieldsize;
4883 /* Helper for output_constructor. From the current LOCAL state, output a
4884 field element that is not true bitfield or part of an outer one. */
4886 static void
4887 output_constructor_regular_field (oc_local_state *local)
4889 /* Field size and position. Since this structure is static, we know the
4890 positions are constant. */
4891 unsigned HOST_WIDE_INT fieldsize;
4892 HOST_WIDE_INT fieldpos;
4894 unsigned int align2;
4896 if (local->index != NULL_TREE)
4898 /* Perform the index calculation in modulo arithmetic but
4899 sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
4900 but we are using an unsigned sizetype. */
4901 unsigned prec = TYPE_PRECISION (sizetype);
4902 offset_int idx = wi::sext (wi::to_offset (local->index)
4903 - wi::to_offset (local->min_index), prec);
4904 fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
4905 .to_short_addr ();
4907 else if (local->field != NULL_TREE)
4908 fieldpos = int_byte_position (local->field);
4909 else
4910 fieldpos = 0;
4912 /* Output any buffered-up bit-fields preceding this element. */
4913 if (local->byte_buffer_in_use)
4915 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
4916 local->total_bytes++;
4917 local->byte_buffer_in_use = false;
4920 /* Advance to offset of this element.
4921 Note no alignment needed in an array, since that is guaranteed
4922 if each element has the proper size. */
4923 if ((local->field != NULL_TREE || local->index != NULL_TREE)
4924 && fieldpos > local->total_bytes)
4926 assemble_zeros (fieldpos - local->total_bytes);
4927 local->total_bytes = fieldpos;
4930 /* Find the alignment of this element. */
4931 align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
4933 /* Determine size this element should occupy. */
4934 if (local->field)
4936 fieldsize = 0;
4938 /* If this is an array with an unspecified upper bound,
4939 the initializer determines the size. */
4940 /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
4941 but we cannot do this until the deprecated support for
4942 initializing zero-length array members is removed. */
4943 if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
4944 && TYPE_DOMAIN (TREE_TYPE (local->field))
4945 && ! TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field))))
4947 fieldsize = array_size_for_constructor (local->val);
4948 /* Given a non-empty initialization, this field had
4949 better be last. */
4950 gcc_assert (!fieldsize || !DECL_CHAIN (local->field));
4952 else
4953 fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
4955 else
4956 fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
4958 /* Output the element's initial value. */
4959 if (local->val == NULL_TREE)
4960 assemble_zeros (fieldsize);
4961 else
4962 fieldsize = output_constant (local->val, fieldsize, align2);
4964 /* Count its size. */
4965 local->total_bytes += fieldsize;
4968 /* Helper for output_constructor. From the LOCAL state, output an element
4969 that is a true bitfield or part of an outer one. BIT_OFFSET is the offset
4970 from the start of a possibly ongoing outer byte buffer. */
4972 static void
4973 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
4975 /* Bit size of this element. */
4976 HOST_WIDE_INT ebitsize
4977 = (local->field
4978 ? tree_to_uhwi (DECL_SIZE (local->field))
4979 : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
4981 /* Relative index of this element if this is an array component. */
4982 HOST_WIDE_INT relative_index
4983 = (!local->field
4984 ? (local->index
4985 ? (tree_to_shwi (local->index)
4986 - tree_to_shwi (local->min_index))
4987 : local->last_relative_index + 1)
4988 : 0);
4990 /* Bit position of this element from the start of the containing
4991 constructor. */
4992 HOST_WIDE_INT constructor_relative_ebitpos
4993 = (local->field
4994 ? int_bit_position (local->field)
4995 : ebitsize * relative_index);
4997 /* Bit position of this element from the start of a possibly ongoing
4998 outer byte buffer. */
4999 HOST_WIDE_INT byte_relative_ebitpos
5000 = bit_offset + constructor_relative_ebitpos;
5002 /* From the start of a possibly ongoing outer byte buffer, offsets to
5003 the first bit of this element and to the first bit past the end of
5004 this element. */
5005 HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5006 HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5008 local->last_relative_index = relative_index;
5010 if (local->val == NULL_TREE)
5011 local->val = integer_zero_node;
5013 while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5014 || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5015 local->val = TREE_OPERAND (local->val, 0);
5017 if (TREE_CODE (local->val) != INTEGER_CST
5018 && TREE_CODE (local->val) != CONSTRUCTOR)
5020 error ("invalid initial value for member %qE", DECL_NAME (local->field));
5021 return;
5024 /* If this field does not start in this (or next) byte, skip some bytes. */
5025 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5027 /* Output remnant of any bit field in previous bytes. */
5028 if (local->byte_buffer_in_use)
5030 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5031 local->total_bytes++;
5032 local->byte_buffer_in_use = false;
5035 /* If still not at proper byte, advance to there. */
5036 if (next_offset / BITS_PER_UNIT != local->total_bytes)
5038 gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5039 assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5040 local->total_bytes = next_offset / BITS_PER_UNIT;
5044 /* Set up the buffer if necessary. */
5045 if (!local->byte_buffer_in_use)
5047 local->byte = 0;
5048 if (ebitsize > 0)
5049 local->byte_buffer_in_use = true;
5052 /* If this is nested constructor, recurse passing the bit offset and the
5053 pending data, then retrieve the new pending data afterwards. */
5054 if (TREE_CODE (local->val) == CONSTRUCTOR)
5056 oc_outer_state temp_state;
5057 temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5058 temp_state.byte = local->byte;
5059 local->total_bytes
5060 += output_constructor (local->val, 0, 0, &temp_state);
5061 local->byte = temp_state.byte;
5062 return;
5065 /* Otherwise, we must split the element into pieces that fall within
5066 separate bytes, and combine each byte with previous or following
5067 bit-fields. */
5068 while (next_offset < end_offset)
5070 int this_time;
5071 int shift;
5072 HOST_WIDE_INT value;
5073 HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5074 HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5076 /* Advance from byte to byte within this element when necessary. */
5077 while (next_byte != local->total_bytes)
5079 assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5080 local->total_bytes++;
5081 local->byte = 0;
5084 /* Number of bits we can process at once (all part of the same byte). */
5085 this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5086 if (BYTES_BIG_ENDIAN)
5088 /* On big-endian machine, take the most significant bits (of the
5089 bits that are significant) first and put them into bytes from
5090 the most significant end. */
5091 shift = end_offset - next_offset - this_time;
5093 /* Don't try to take a bunch of bits that cross
5094 the word boundary in the INTEGER_CST. We can
5095 only select bits from one element. */
5096 if ((shift / HOST_BITS_PER_WIDE_INT)
5097 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5099 const int end = shift + this_time - 1;
5100 shift = end & -HOST_BITS_PER_WIDE_INT;
5101 this_time = end - shift + 1;
5104 /* Now get the bits from the appropriate constant word. */
5105 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5106 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5108 /* Get the result. This works only when:
5109 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5110 local->byte |= (((value >> shift)
5111 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5112 << (BITS_PER_UNIT - this_time - next_bit));
5114 else
5116 /* On little-endian machines, take the least significant bits of
5117 the value first and pack them starting at the least significant
5118 bits of the bytes. */
5119 shift = next_offset - byte_relative_ebitpos;
5121 /* Don't try to take a bunch of bits that cross
5122 the word boundary in the INTEGER_CST. We can
5123 only select bits from one element. */
5124 if ((shift / HOST_BITS_PER_WIDE_INT)
5125 != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5126 this_time
5127 = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5129 /* Now get the bits from the appropriate constant word. */
5130 value = TREE_INT_CST_ELT (local->val, shift / HOST_BITS_PER_WIDE_INT);
5131 shift = shift & (HOST_BITS_PER_WIDE_INT - 1);
5133 /* Get the result. This works only when:
5134 1 <= this_time <= HOST_BITS_PER_WIDE_INT. */
5135 local->byte |= (((value >> shift)
5136 & (((HOST_WIDE_INT) 2 << (this_time - 1)) - 1))
5137 << next_bit);
5140 next_offset += this_time;
5141 local->byte_buffer_in_use = true;
5145 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5146 Generate at least SIZE bytes, padding if necessary. OUTER designates the
5147 caller output state of relevance in recursive invocations. */
5149 static unsigned HOST_WIDE_INT
5150 output_constructor (tree exp, unsigned HOST_WIDE_INT size,
5151 unsigned int align, oc_outer_state *outer)
5153 unsigned HOST_WIDE_INT cnt;
5154 constructor_elt *ce;
5156 oc_local_state local;
5158 /* Setup our local state to communicate with helpers. */
5159 local.exp = exp;
5160 local.type = TREE_TYPE (exp);
5161 local.size = size;
5162 local.align = align;
5163 if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5164 local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5165 else
5166 local.min_index = NULL_TREE;
5168 local.total_bytes = 0;
5169 local.byte_buffer_in_use = outer != NULL;
5170 local.byte = outer ? outer->byte : 0;
5171 local.last_relative_index = -1;
5173 gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5175 /* As CE goes through the elements of the constant, FIELD goes through the
5176 structure fields if the constant is a structure. If the constant is a
5177 union, we override this by getting the field from the TREE_LIST element.
5178 But the constant could also be an array. Then FIELD is zero.
5180 There is always a maximum of one element in the chain LINK for unions
5181 (even if the initializer in a source program incorrectly contains
5182 more one). */
5184 if (TREE_CODE (local.type) == RECORD_TYPE)
5185 local.field = TYPE_FIELDS (local.type);
5186 else
5187 local.field = NULL_TREE;
5189 for (cnt = 0;
5190 vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5191 cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5193 local.val = ce->value;
5194 local.index = NULL_TREE;
5196 /* The element in a union constructor specifies the proper field
5197 or index. */
5198 if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5199 local.field = ce->index;
5201 else if (TREE_CODE (local.type) == ARRAY_TYPE)
5202 local.index = ce->index;
5204 if (local.field && flag_verbose_asm)
5205 fprintf (asm_out_file, "%s %s:\n",
5206 ASM_COMMENT_START,
5207 DECL_NAME (local.field)
5208 ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5209 : "<anonymous>");
5211 /* Eliminate the marker that makes a cast not be an lvalue. */
5212 if (local.val != NULL_TREE)
5213 STRIP_NOPS (local.val);
5215 /* Output the current element, using the appropriate helper ... */
5217 /* For an array slice not part of an outer bitfield. */
5218 if (!outer
5219 && local.index != NULL_TREE
5220 && TREE_CODE (local.index) == RANGE_EXPR)
5221 output_constructor_array_range (&local);
5223 /* For a field that is neither a true bitfield nor part of an outer one,
5224 known to be at least byte aligned and multiple-of-bytes long. */
5225 else if (!outer
5226 && (local.field == NULL_TREE
5227 || !CONSTRUCTOR_BITFIELD_P (local.field)))
5228 output_constructor_regular_field (&local);
5230 /* For a true bitfield or part of an outer one. Only INTEGER_CSTs are
5231 supported for scalar fields, so we may need to convert first. */
5232 else
5234 if (TREE_CODE (local.val) == REAL_CST)
5235 local.val
5236 = fold_unary (VIEW_CONVERT_EXPR,
5237 build_nonstandard_integer_type
5238 (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5239 local.val);
5240 output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5244 /* If we are not at toplevel, save the pending data for our caller.
5245 Otherwise output the pending data and padding zeros as needed. */
5246 if (outer)
5247 outer->byte = local.byte;
5248 else
5250 if (local.byte_buffer_in_use)
5252 assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5253 local.total_bytes++;
5256 if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5258 assemble_zeros (local.size - local.total_bytes);
5259 local.total_bytes = local.size;
5263 return local.total_bytes;
5266 /* Mark DECL as weak. */
5268 static void
5269 mark_weak (tree decl)
5271 if (DECL_WEAK (decl))
5272 return;
5274 struct symtab_node *n = symtab_node::get (decl);
5275 if (n && n->refuse_visibility_changes)
5276 error ("%+D declared weak after being used", decl);
5277 DECL_WEAK (decl) = 1;
5279 if (DECL_RTL_SET_P (decl)
5280 && MEM_P (DECL_RTL (decl))
5281 && XEXP (DECL_RTL (decl), 0)
5282 && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5283 SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5286 /* Merge weak status between NEWDECL and OLDDECL. */
5288 void
5289 merge_weak (tree newdecl, tree olddecl)
5291 if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5293 if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5295 tree *pwd;
5296 /* We put the NEWDECL on the weak_decls list at some point
5297 and OLDDECL as well. Keep just OLDDECL on the list. */
5298 for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5299 if (TREE_VALUE (*pwd) == newdecl)
5301 *pwd = TREE_CHAIN (*pwd);
5302 break;
5305 return;
5308 if (DECL_WEAK (newdecl))
5310 tree wd;
5312 /* NEWDECL is weak, but OLDDECL is not. */
5314 /* If we already output the OLDDECL, we're in trouble; we can't
5315 go back and make it weak. This should never happen in
5316 unit-at-a-time compilation. */
5317 gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5319 /* If we've already generated rtl referencing OLDDECL, we may
5320 have done so in a way that will not function properly with
5321 a weak symbol. Again in unit-at-a-time this should be
5322 impossible. */
5323 gcc_assert (!TREE_USED (olddecl)
5324 || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5326 if (TARGET_SUPPORTS_WEAK)
5328 /* We put the NEWDECL on the weak_decls list at some point.
5329 Replace it with the OLDDECL. */
5330 for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5331 if (TREE_VALUE (wd) == newdecl)
5333 TREE_VALUE (wd) = olddecl;
5334 break;
5336 /* We may not find the entry on the list. If NEWDECL is a
5337 weak alias, then we will have already called
5338 globalize_decl to remove the entry; in that case, we do
5339 not need to do anything. */
5342 /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping. */
5343 mark_weak (olddecl);
5345 else
5346 /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5347 weak. Just update NEWDECL to indicate that it's weak too. */
5348 mark_weak (newdecl);
5351 /* Declare DECL to be a weak symbol. */
5353 void
5354 declare_weak (tree decl)
5356 gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5357 if (! TREE_PUBLIC (decl))
5359 error ("weak declaration of %q+D must be public", decl);
5360 return;
5362 else if (!TARGET_SUPPORTS_WEAK)
5363 warning (0, "weak declaration of %q+D not supported", decl);
5365 mark_weak (decl);
5366 if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5367 DECL_ATTRIBUTES (decl)
5368 = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5371 static void
5372 weak_finish_1 (tree decl)
5374 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5375 const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5376 #endif
5378 if (! TREE_USED (decl))
5379 return;
5381 #ifdef ASM_WEAKEN_DECL
5382 ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5383 #else
5384 #ifdef ASM_WEAKEN_LABEL
5385 ASM_WEAKEN_LABEL (asm_out_file, name);
5386 #else
5387 #ifdef ASM_OUTPUT_WEAK_ALIAS
5389 static bool warn_once = 0;
5390 if (! warn_once)
5392 warning (0, "only weak aliases are supported in this configuration");
5393 warn_once = 1;
5395 return;
5397 #endif
5398 #endif
5399 #endif
5402 /* Fiven an assembly name, find the decl it is associated with. */
5403 static tree
5404 find_decl (tree target)
5406 symtab_node *node = symtab_node::get_for_asmname (target);
5407 if (node)
5408 return node->decl;
5409 return NULL_TREE;
5412 /* This TREE_LIST contains weakref targets. */
5414 static GTY(()) tree weakref_targets;
5416 /* Emit any pending weak declarations. */
5418 void
5419 weak_finish (void)
5421 tree t;
5423 for (t = weakref_targets; t; t = TREE_CHAIN (t))
5425 tree alias_decl = TREE_PURPOSE (t);
5426 tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5428 if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl)))
5429 /* Remove alias_decl from the weak list, but leave entries for
5430 the target alone. */
5431 target = NULL_TREE;
5432 #ifndef ASM_OUTPUT_WEAKREF
5433 else if (! TREE_SYMBOL_REFERENCED (target))
5435 /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5436 defined, otherwise we and weak_finish_1 would use
5437 different macros. */
5438 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5439 ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5440 # else
5441 tree decl = find_decl (target);
5443 if (! decl)
5445 decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5446 TREE_CODE (alias_decl), target,
5447 TREE_TYPE (alias_decl));
5449 DECL_EXTERNAL (decl) = 1;
5450 TREE_PUBLIC (decl) = 1;
5451 DECL_ARTIFICIAL (decl) = 1;
5452 TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5453 TREE_USED (decl) = 1;
5456 weak_finish_1 (decl);
5457 # endif
5459 #endif
5462 tree *p;
5463 tree t2;
5465 /* Remove the alias and the target from the pending weak list
5466 so that we do not emit any .weak directives for the former,
5467 nor multiple .weak directives for the latter. */
5468 for (p = &weak_decls; (t2 = *p) ; )
5470 if (TREE_VALUE (t2) == alias_decl
5471 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5472 *p = TREE_CHAIN (t2);
5473 else
5474 p = &TREE_CHAIN (t2);
5477 /* Remove other weakrefs to the same target, to speed things up. */
5478 for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5480 if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5481 *p = TREE_CHAIN (t2);
5482 else
5483 p = &TREE_CHAIN (t2);
5488 for (t = weak_decls; t; t = TREE_CHAIN (t))
5490 tree decl = TREE_VALUE (t);
5492 weak_finish_1 (decl);
5496 /* Emit the assembly bits to indicate that DECL is globally visible. */
5498 static void
5499 globalize_decl (tree decl)
5502 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5503 if (DECL_WEAK (decl))
5505 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5506 tree *p, t;
5508 #ifdef ASM_WEAKEN_DECL
5509 ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5510 #else
5511 ASM_WEAKEN_LABEL (asm_out_file, name);
5512 #endif
5514 /* Remove this function from the pending weak list so that
5515 we do not emit multiple .weak directives for it. */
5516 for (p = &weak_decls; (t = *p) ; )
5518 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5519 *p = TREE_CHAIN (t);
5520 else
5521 p = &TREE_CHAIN (t);
5524 /* Remove weakrefs to the same target from the pending weakref
5525 list, for the same reason. */
5526 for (p = &weakref_targets; (t = *p) ; )
5528 if (DECL_ASSEMBLER_NAME (decl)
5529 == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5530 *p = TREE_CHAIN (t);
5531 else
5532 p = &TREE_CHAIN (t);
5535 return;
5537 #endif
5539 targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5542 vec<alias_pair, va_gc> *alias_pairs;
5544 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5545 or ASM_OUTPUT_DEF_FROM_DECLS. The function defines the symbol whose
5546 tree node is DECL to have the value of the tree node TARGET. */
5548 void
5549 do_assemble_alias (tree decl, tree target)
5551 tree id;
5553 /* Emulated TLS had better not get this var. */
5554 gcc_assert (!(!targetm.have_tls
5555 && TREE_CODE (decl) == VAR_DECL
5556 && DECL_THREAD_LOCAL_P (decl)));
5558 if (TREE_ASM_WRITTEN (decl))
5559 return;
5561 id = DECL_ASSEMBLER_NAME (decl);
5562 ultimate_transparent_alias_target (&id);
5563 ultimate_transparent_alias_target (&target);
5565 /* We must force creation of DECL_RTL for debug info generation, even though
5566 we don't use it here. */
5567 make_decl_rtl (decl);
5569 TREE_ASM_WRITTEN (decl) = 1;
5570 TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5571 TREE_ASM_WRITTEN (id) = 1;
5573 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5575 if (!TREE_SYMBOL_REFERENCED (target))
5576 weakref_targets = tree_cons (decl, target, weakref_targets);
5578 #ifdef ASM_OUTPUT_WEAKREF
5579 ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5580 IDENTIFIER_POINTER (id),
5581 IDENTIFIER_POINTER (target));
5582 #else
5583 if (!TARGET_SUPPORTS_WEAK)
5585 error_at (DECL_SOURCE_LOCATION (decl),
5586 "weakref is not supported in this configuration");
5587 return;
5589 #endif
5590 return;
5593 #ifdef ASM_OUTPUT_DEF
5594 tree orig_decl = decl;
5596 if (TREE_CODE (decl) == FUNCTION_DECL
5597 && cgraph_node::get (decl)->instrumentation_clone
5598 && cgraph_node::get (decl)->instrumented_version)
5599 orig_decl = cgraph_node::get (decl)->instrumented_version->decl;
5601 /* Make name accessible from other files, if appropriate. */
5603 if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5605 globalize_decl (decl);
5606 maybe_assemble_visibility (decl);
5608 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5610 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5611 if (targetm.has_ifunc_p ())
5612 ASM_OUTPUT_TYPE_DIRECTIVE
5613 (asm_out_file, IDENTIFIER_POINTER (id),
5614 IFUNC_ASM_TYPE);
5615 else
5616 #endif
5617 error_at (DECL_SOURCE_LOCATION (decl),
5618 "ifunc is not supported on this target");
5621 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5622 ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5623 # else
5624 ASM_OUTPUT_DEF (asm_out_file,
5625 IDENTIFIER_POINTER (id),
5626 IDENTIFIER_POINTER (target));
5627 # endif
5628 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5630 const char *name;
5631 tree *p, t;
5633 name = IDENTIFIER_POINTER (id);
5634 # ifdef ASM_WEAKEN_DECL
5635 ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5636 # else
5637 ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5638 # endif
5639 /* Remove this function from the pending weak list so that
5640 we do not emit multiple .weak directives for it. */
5641 for (p = &weak_decls; (t = *p) ; )
5642 if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5643 || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5644 *p = TREE_CHAIN (t);
5645 else
5646 p = &TREE_CHAIN (t);
5648 /* Remove weakrefs to the same target from the pending weakref
5649 list, for the same reason. */
5650 for (p = &weakref_targets; (t = *p) ; )
5652 if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5653 *p = TREE_CHAIN (t);
5654 else
5655 p = &TREE_CHAIN (t);
5658 #endif
5661 /* Emit an assembler directive to make the symbol for DECL an alias to
5662 the symbol for TARGET. */
5664 void
5665 assemble_alias (tree decl, tree target)
5667 tree target_decl;
5669 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5671 tree alias = DECL_ASSEMBLER_NAME (decl);
5673 ultimate_transparent_alias_target (&target);
5675 if (alias == target)
5676 error ("weakref %q+D ultimately targets itself", decl);
5677 if (TREE_PUBLIC (decl))
5678 error ("weakref %q+D must have static linkage", decl);
5680 else
5682 #if !defined (ASM_OUTPUT_DEF)
5683 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
5684 error_at (DECL_SOURCE_LOCATION (decl),
5685 "alias definitions not supported in this configuration");
5686 TREE_ASM_WRITTEN (decl) = 1;
5687 return;
5688 # else
5689 if (!DECL_WEAK (decl))
5691 if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
5692 error_at (DECL_SOURCE_LOCATION (decl),
5693 "ifunc is not supported in this configuration");
5694 else
5695 error_at (DECL_SOURCE_LOCATION (decl),
5696 "only weak aliases are supported in this configuration");
5697 TREE_ASM_WRITTEN (decl) = 1;
5698 return;
5700 # endif
5701 #endif
5703 TREE_USED (decl) = 1;
5705 /* Allow aliases to aliases. */
5706 if (TREE_CODE (decl) == FUNCTION_DECL)
5707 cgraph_node::get_create (decl)->alias = true;
5708 else
5709 varpool_node::get_create (decl)->alias = true;
5711 /* If the target has already been emitted, we don't have to queue the
5712 alias. This saves a tad of memory. */
5713 if (symtab->global_info_ready)
5714 target_decl = find_decl (target);
5715 else
5716 target_decl= NULL;
5717 if ((target_decl && TREE_ASM_WRITTEN (target_decl))
5718 || symtab->state >= EXPANSION)
5719 do_assemble_alias (decl, target);
5720 else
5722 alias_pair p = {decl, target};
5723 vec_safe_push (alias_pairs, p);
5727 /* Record and output a table of translations from original function
5728 to its transaction aware clone. Note that tm_pure functions are
5729 considered to be their own clone. */
5731 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
5733 static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
5734 static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
5736 static int
5737 keep_cache_entry (tree_map *&e)
5739 return ggc_marked_p (e->base.from);
5743 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
5745 void
5746 record_tm_clone_pair (tree o, tree n)
5748 struct tree_map **slot, *h;
5750 if (tm_clone_hash == NULL)
5751 tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
5753 h = ggc_alloc<tree_map> ();
5754 h->hash = htab_hash_pointer (o);
5755 h->base.from = o;
5756 h->to = n;
5758 slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
5759 *slot = h;
5762 tree
5763 get_tm_clone_pair (tree o)
5765 if (tm_clone_hash)
5767 struct tree_map *h, in;
5769 in.base.from = o;
5770 in.hash = htab_hash_pointer (o);
5771 h = tm_clone_hash->find_with_hash (&in, in.hash);
5772 if (h)
5773 return h->to;
5775 return NULL_TREE;
5778 struct tm_alias_pair
5780 unsigned int uid;
5781 tree from;
5782 tree to;
5786 /* Dump the actual pairs to the .tm_clone_table section. */
5788 static void
5789 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
5791 unsigned i;
5792 tm_alias_pair *p;
5793 bool switched = false;
5795 FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
5797 tree src = p->from;
5798 tree dst = p->to;
5799 struct cgraph_node *src_n = cgraph_node::get (src);
5800 struct cgraph_node *dst_n = cgraph_node::get (dst);
5802 /* The function ipa_tm_create_version() marks the clone as needed if
5803 the original function was needed. But we also mark the clone as
5804 needed if we ever called the clone indirectly through
5805 TM_GETTMCLONE. If neither of these are true, we didn't generate
5806 a clone, and we didn't call it indirectly... no sense keeping it
5807 in the clone table. */
5808 if (!dst_n || !dst_n->definition)
5809 continue;
5811 /* This covers the case where we have optimized the original
5812 function away, and only access the transactional clone. */
5813 if (!src_n || !src_n->definition)
5814 continue;
5816 if (!switched)
5818 switch_to_section (targetm.asm_out.tm_clone_table_section ());
5819 assemble_align (POINTER_SIZE);
5820 switched = true;
5823 assemble_integer (XEXP (DECL_RTL (src), 0),
5824 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5825 assemble_integer (XEXP (DECL_RTL (dst), 0),
5826 POINTER_SIZE_UNITS, POINTER_SIZE, 1);
5830 /* Provide a default for the tm_clone_table section. */
5832 section *
5833 default_clone_table_section (void)
5835 return get_named_section (NULL, ".tm_clone_table", 3);
5838 /* Helper comparison function for qsorting by the DECL_UID stored in
5839 alias_pair->emitted_diags. */
5841 static int
5842 tm_alias_pair_cmp (const void *x, const void *y)
5844 const tm_alias_pair *p1 = (const tm_alias_pair *) x;
5845 const tm_alias_pair *p2 = (const tm_alias_pair *) y;
5846 if (p1->uid < p2->uid)
5847 return -1;
5848 if (p1->uid > p2->uid)
5849 return 1;
5850 return 0;
5853 void
5854 finish_tm_clone_pairs (void)
5856 vec<tm_alias_pair> tm_alias_pairs = vNULL;
5858 if (tm_clone_hash == NULL)
5859 return;
5861 /* We need a determenistic order for the .tm_clone_table, otherwise
5862 we will get bootstrap comparison failures, so dump the hash table
5863 to a vector, sort it, and dump the vector. */
5865 /* Dump the hashtable to a vector. */
5866 tree_map *map;
5867 hash_table<tm_clone_hasher>::iterator iter;
5868 FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
5870 tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
5871 tm_alias_pairs.safe_push (p);
5873 /* Sort it. */
5874 tm_alias_pairs.qsort (tm_alias_pair_cmp);
5876 /* Dump it. */
5877 dump_tm_clone_pairs (tm_alias_pairs);
5879 tm_clone_hash->empty ();
5880 tm_clone_hash = NULL;
5881 tm_alias_pairs.release ();
5885 /* Emit an assembler directive to set symbol for DECL visibility to
5886 the visibility type VIS, which must not be VISIBILITY_DEFAULT. */
5888 void
5889 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
5890 int vis ATTRIBUTE_UNUSED)
5892 #ifdef HAVE_GAS_HIDDEN
5893 static const char * const visibility_types[] = {
5894 NULL, "protected", "hidden", "internal"
5897 const char *name, *type;
5898 tree id;
5900 id = DECL_ASSEMBLER_NAME (decl);
5901 ultimate_transparent_alias_target (&id);
5902 name = IDENTIFIER_POINTER (id);
5904 type = visibility_types[vis];
5906 fprintf (asm_out_file, "\t.%s\t", type);
5907 assemble_name (asm_out_file, name);
5908 fprintf (asm_out_file, "\n");
5909 #else
5910 if (!DECL_ARTIFICIAL (decl))
5911 warning (OPT_Wattributes, "visibility attribute not supported "
5912 "in this configuration; ignored");
5913 #endif
5916 /* A helper function to call assemble_visibility when needed for a decl. */
5919 maybe_assemble_visibility (tree decl)
5921 enum symbol_visibility vis = DECL_VISIBILITY (decl);
5923 if (TREE_CODE (decl) == FUNCTION_DECL
5924 && cgraph_node::get (decl)
5925 && cgraph_node::get (decl)->instrumentation_clone
5926 && cgraph_node::get (decl)->instrumented_version)
5927 vis = DECL_VISIBILITY (cgraph_node::get (decl)->instrumented_version->decl);
5929 if (vis != VISIBILITY_DEFAULT)
5931 targetm.asm_out.assemble_visibility (decl, vis);
5932 return 1;
5934 else
5935 return 0;
5938 /* Returns 1 if the target configuration supports defining public symbols
5939 so that one of them will be chosen at link time instead of generating a
5940 multiply-defined symbol error, whether through the use of weak symbols or
5941 a target-specific mechanism for having duplicates discarded. */
5944 supports_one_only (void)
5946 if (SUPPORTS_ONE_ONLY)
5947 return 1;
5948 return TARGET_SUPPORTS_WEAK;
5951 /* Set up DECL as a public symbol that can be defined in multiple
5952 translation units without generating a linker error. */
5954 void
5955 make_decl_one_only (tree decl, tree comdat_group)
5957 struct symtab_node *symbol;
5958 gcc_assert (TREE_CODE (decl) == VAR_DECL
5959 || TREE_CODE (decl) == FUNCTION_DECL);
5961 TREE_PUBLIC (decl) = 1;
5963 if (TREE_CODE (decl) == VAR_DECL)
5964 symbol = varpool_node::get_create (decl);
5965 else
5966 symbol = cgraph_node::get_create (decl);
5968 if (SUPPORTS_ONE_ONLY)
5970 #ifdef MAKE_DECL_ONE_ONLY
5971 MAKE_DECL_ONE_ONLY (decl);
5972 #endif
5973 symbol->set_comdat_group (comdat_group);
5975 else if (TREE_CODE (decl) == VAR_DECL
5976 && (DECL_INITIAL (decl) == 0
5977 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
5978 DECL_COMMON (decl) = 1;
5979 else
5981 gcc_assert (TARGET_SUPPORTS_WEAK);
5982 DECL_WEAK (decl) = 1;
5986 void
5987 init_varasm_once (void)
5989 section_htab = hash_table<section_hasher>::create_ggc (31);
5990 object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
5991 const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
5993 shared_constant_pool = create_constant_pool ();
5995 #ifdef TEXT_SECTION_ASM_OP
5996 text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
5997 TEXT_SECTION_ASM_OP);
5998 #endif
6000 #ifdef DATA_SECTION_ASM_OP
6001 data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6002 DATA_SECTION_ASM_OP);
6003 #endif
6005 #ifdef SDATA_SECTION_ASM_OP
6006 sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6007 SDATA_SECTION_ASM_OP);
6008 #endif
6010 #ifdef READONLY_DATA_SECTION_ASM_OP
6011 readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6012 READONLY_DATA_SECTION_ASM_OP);
6013 #endif
6015 #ifdef CTORS_SECTION_ASM_OP
6016 ctors_section = get_unnamed_section (0, output_section_asm_op,
6017 CTORS_SECTION_ASM_OP);
6018 #endif
6020 #ifdef DTORS_SECTION_ASM_OP
6021 dtors_section = get_unnamed_section (0, output_section_asm_op,
6022 DTORS_SECTION_ASM_OP);
6023 #endif
6025 #ifdef BSS_SECTION_ASM_OP
6026 bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6027 output_section_asm_op,
6028 BSS_SECTION_ASM_OP);
6029 #endif
6031 #ifdef SBSS_SECTION_ASM_OP
6032 sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6033 output_section_asm_op,
6034 SBSS_SECTION_ASM_OP);
6035 #endif
6037 tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6038 | SECTION_COMMON, emit_tls_common);
6039 lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6040 | SECTION_COMMON, emit_local);
6041 comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6042 | SECTION_COMMON, emit_common);
6044 #if defined ASM_OUTPUT_ALIGNED_BSS
6045 bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6046 emit_bss);
6047 #endif
6049 targetm.asm_out.init_sections ();
6051 if (readonly_data_section == NULL)
6052 readonly_data_section = text_section;
6054 #ifdef ASM_OUTPUT_EXTERNAL
6055 pending_assemble_externals_set = new hash_set<tree>;
6056 #endif
6059 enum tls_model
6060 decl_default_tls_model (const_tree decl)
6062 enum tls_model kind;
6063 bool is_local;
6065 is_local = targetm.binds_local_p (decl);
6066 if (!flag_shlib)
6068 if (is_local)
6069 kind = TLS_MODEL_LOCAL_EXEC;
6070 else
6071 kind = TLS_MODEL_INITIAL_EXEC;
6074 /* Local dynamic is inefficient when we're not combining the
6075 parts of the address. */
6076 else if (optimize && is_local)
6077 kind = TLS_MODEL_LOCAL_DYNAMIC;
6078 else
6079 kind = TLS_MODEL_GLOBAL_DYNAMIC;
6080 if (kind < flag_tls_default)
6081 kind = flag_tls_default;
6083 return kind;
6086 /* Select a set of attributes for section NAME based on the properties
6087 of DECL and whether or not RELOC indicates that DECL's initializer
6088 might contain runtime relocations.
6090 We make the section read-only and executable for a function decl,
6091 read-only for a const data decl, and writable for a non-const data decl. */
6093 unsigned int
6094 default_section_type_flags (tree decl, const char *name, int reloc)
6096 unsigned int flags;
6098 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6099 flags = SECTION_CODE;
6100 else if (decl)
6102 enum section_category category
6103 = categorize_decl_for_section (decl, reloc);
6104 if (decl_readonly_section_1 (category))
6105 flags = 0;
6106 else if (category == SECCAT_DATA_REL_RO
6107 || category == SECCAT_DATA_REL_RO_LOCAL)
6108 flags = SECTION_WRITE | SECTION_RELRO;
6109 else
6110 flags = SECTION_WRITE;
6112 else
6114 flags = SECTION_WRITE;
6115 if (strcmp (name, ".data.rel.ro") == 0
6116 || strcmp (name, ".data.rel.ro.local") == 0)
6117 flags |= SECTION_RELRO;
6120 if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6121 flags |= SECTION_LINKONCE;
6123 if (strcmp (name, ".vtable_map_vars") == 0)
6124 flags |= SECTION_LINKONCE;
6126 if (decl && TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6127 flags |= SECTION_TLS | SECTION_WRITE;
6129 if (strcmp (name, ".bss") == 0
6130 || strncmp (name, ".bss.", 5) == 0
6131 || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6132 || strcmp (name, ".persistent.bss") == 0
6133 || strcmp (name, ".sbss") == 0
6134 || strncmp (name, ".sbss.", 6) == 0
6135 || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6136 flags |= SECTION_BSS;
6138 if (strcmp (name, ".tdata") == 0
6139 || strncmp (name, ".tdata.", 7) == 0
6140 || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6141 flags |= SECTION_TLS;
6143 if (strcmp (name, ".tbss") == 0
6144 || strncmp (name, ".tbss.", 6) == 0
6145 || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6146 flags |= SECTION_TLS | SECTION_BSS;
6148 /* These three sections have special ELF types. They are neither
6149 SHT_PROGBITS nor SHT_NOBITS, so when changing sections we don't
6150 want to print a section type (@progbits or @nobits). If someone
6151 is silly enough to emit code or TLS variables to one of these
6152 sections, then don't handle them specially. */
6153 if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS))
6154 && (strcmp (name, ".init_array") == 0
6155 || strcmp (name, ".fini_array") == 0
6156 || strcmp (name, ".preinit_array") == 0))
6157 flags |= SECTION_NOTYPE;
6159 return flags;
6162 /* Return true if the target supports some form of global BSS,
6163 either through bss_noswitch_section, or by selecting a BSS
6164 section in TARGET_ASM_SELECT_SECTION. */
6166 bool
6167 have_global_bss_p (void)
6169 return bss_noswitch_section || targetm.have_switchable_bss_sections;
6172 /* Output assembly to switch to section NAME with attribute FLAGS.
6173 Four variants for common object file formats. */
6175 void
6176 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6177 unsigned int flags ATTRIBUTE_UNUSED,
6178 tree decl ATTRIBUTE_UNUSED)
6180 /* Some object formats don't support named sections at all. The
6181 front-end should already have flagged this as an error. */
6182 gcc_unreachable ();
6185 #ifndef TLS_SECTION_ASM_FLAG
6186 #define TLS_SECTION_ASM_FLAG 'T'
6187 #endif
6189 void
6190 default_elf_asm_named_section (const char *name, unsigned int flags,
6191 tree decl)
6193 char flagchars[10], *f = flagchars;
6195 /* If we have already declared this section, we can use an
6196 abbreviated form to switch back to it -- unless this section is
6197 part of a COMDAT groups, in which case GAS requires the full
6198 declaration every time. */
6199 if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6200 && (flags & SECTION_DECLARED))
6202 fprintf (asm_out_file, "\t.section\t%s\n", name);
6203 return;
6206 if (!(flags & SECTION_DEBUG))
6207 *f++ = 'a';
6208 #if defined (HAVE_GAS_SECTION_EXCLUDE) && HAVE_GAS_SECTION_EXCLUDE == 1
6209 if (flags & SECTION_EXCLUDE)
6210 *f++ = 'e';
6211 #endif
6212 if (flags & SECTION_WRITE)
6213 *f++ = 'w';
6214 if (flags & SECTION_CODE)
6215 *f++ = 'x';
6216 if (flags & SECTION_SMALL)
6217 *f++ = 's';
6218 if (flags & SECTION_MERGE)
6219 *f++ = 'M';
6220 if (flags & SECTION_STRINGS)
6221 *f++ = 'S';
6222 if (flags & SECTION_TLS)
6223 *f++ = TLS_SECTION_ASM_FLAG;
6224 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6225 *f++ = 'G';
6226 *f = '\0';
6228 fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6230 if (!(flags & SECTION_NOTYPE))
6232 const char *type;
6233 const char *format;
6235 if (flags & SECTION_BSS)
6236 type = "nobits";
6237 else
6238 type = "progbits";
6240 format = ",@%s";
6241 /* On platforms that use "@" as the assembly comment character,
6242 use "%" instead. */
6243 if (strcmp (ASM_COMMENT_START, "@") == 0)
6244 format = ",%%%s";
6245 fprintf (asm_out_file, format, type);
6247 if (flags & SECTION_ENTSIZE)
6248 fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6249 if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6251 if (TREE_CODE (decl) == IDENTIFIER_NODE)
6252 fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6253 else
6254 fprintf (asm_out_file, ",%s,comdat",
6255 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6259 putc ('\n', asm_out_file);
6262 void
6263 default_coff_asm_named_section (const char *name, unsigned int flags,
6264 tree decl ATTRIBUTE_UNUSED)
6266 char flagchars[8], *f = flagchars;
6268 if (flags & SECTION_WRITE)
6269 *f++ = 'w';
6270 if (flags & SECTION_CODE)
6271 *f++ = 'x';
6272 *f = '\0';
6274 fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6277 void
6278 default_pe_asm_named_section (const char *name, unsigned int flags,
6279 tree decl)
6281 default_coff_asm_named_section (name, flags, decl);
6283 if (flags & SECTION_LINKONCE)
6285 /* Functions may have been compiled at various levels of
6286 optimization so we can't use `same_size' here.
6287 Instead, have the linker pick one. */
6288 fprintf (asm_out_file, "\t.linkonce %s\n",
6289 (flags & SECTION_CODE ? "discard" : "same_size"));
6293 /* The lame default section selector. */
6295 section *
6296 default_select_section (tree decl, int reloc,
6297 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6299 if (DECL_P (decl))
6301 if (decl_readonly_section (decl, reloc))
6302 return readonly_data_section;
6304 else if (TREE_CODE (decl) == CONSTRUCTOR)
6306 if (! ((flag_pic && reloc)
6307 || !TREE_READONLY (decl)
6308 || TREE_SIDE_EFFECTS (decl)
6309 || !TREE_CONSTANT (decl)))
6310 return readonly_data_section;
6312 else if (TREE_CODE (decl) == STRING_CST)
6313 return readonly_data_section;
6314 else if (! (flag_pic && reloc))
6315 return readonly_data_section;
6317 return data_section;
6320 enum section_category
6321 categorize_decl_for_section (const_tree decl, int reloc)
6323 enum section_category ret;
6325 if (TREE_CODE (decl) == FUNCTION_DECL)
6326 return SECCAT_TEXT;
6327 else if (TREE_CODE (decl) == STRING_CST)
6329 if ((flag_sanitize & SANITIZE_ADDRESS)
6330 && asan_protect_global (CONST_CAST_TREE (decl)))
6331 /* or !flag_merge_constants */
6332 return SECCAT_RODATA;
6333 else
6334 return SECCAT_RODATA_MERGE_STR;
6336 else if (TREE_CODE (decl) == VAR_DECL)
6338 if (bss_initializer_p (decl))
6339 ret = SECCAT_BSS;
6340 else if (! TREE_READONLY (decl)
6341 || TREE_SIDE_EFFECTS (decl)
6342 || ! TREE_CONSTANT (DECL_INITIAL (decl)))
6344 /* Here the reloc_rw_mask is not testing whether the section should
6345 be read-only or not, but whether the dynamic link will have to
6346 do something. If so, we wish to segregate the data in order to
6347 minimize cache misses inside the dynamic linker. */
6348 if (reloc & targetm.asm_out.reloc_rw_mask ())
6349 ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6350 else
6351 ret = SECCAT_DATA;
6353 else if (reloc & targetm.asm_out.reloc_rw_mask ())
6354 ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6355 else if (reloc || flag_merge_constants < 2
6356 || ((flag_sanitize & SANITIZE_ADDRESS)
6357 && asan_protect_global (CONST_CAST_TREE (decl))))
6358 /* C and C++ don't allow different variables to share the same
6359 location. -fmerge-all-constants allows even that (at the
6360 expense of not conforming). */
6361 ret = SECCAT_RODATA;
6362 else if (TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6363 ret = SECCAT_RODATA_MERGE_STR_INIT;
6364 else
6365 ret = SECCAT_RODATA_MERGE_CONST;
6367 else if (TREE_CODE (decl) == CONSTRUCTOR)
6369 if ((reloc & targetm.asm_out.reloc_rw_mask ())
6370 || TREE_SIDE_EFFECTS (decl)
6371 || ! TREE_CONSTANT (decl))
6372 ret = SECCAT_DATA;
6373 else
6374 ret = SECCAT_RODATA;
6376 else
6377 ret = SECCAT_RODATA;
6379 /* There are no read-only thread-local sections. */
6380 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6382 /* Note that this would be *just* SECCAT_BSS, except that there's
6383 no concept of a read-only thread-local-data section. */
6384 if (ret == SECCAT_BSS
6385 || (flag_zero_initialized_in_bss
6386 && initializer_zerop (DECL_INITIAL (decl))))
6387 ret = SECCAT_TBSS;
6388 else
6389 ret = SECCAT_TDATA;
6392 /* If the target uses small data sections, select it. */
6393 else if (targetm.in_small_data_p (decl))
6395 if (ret == SECCAT_BSS)
6396 ret = SECCAT_SBSS;
6397 else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6398 ret = SECCAT_SRODATA;
6399 else
6400 ret = SECCAT_SDATA;
6403 return ret;
6406 static bool
6407 decl_readonly_section_1 (enum section_category category)
6409 switch (category)
6411 case SECCAT_RODATA:
6412 case SECCAT_RODATA_MERGE_STR:
6413 case SECCAT_RODATA_MERGE_STR_INIT:
6414 case SECCAT_RODATA_MERGE_CONST:
6415 case SECCAT_SRODATA:
6416 return true;
6417 default:
6418 return false;
6422 bool
6423 decl_readonly_section (const_tree decl, int reloc)
6425 return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6428 /* Select a section based on the above categorization. */
6430 section *
6431 default_elf_select_section (tree decl, int reloc,
6432 unsigned HOST_WIDE_INT align)
6434 const char *sname;
6435 switch (categorize_decl_for_section (decl, reloc))
6437 case SECCAT_TEXT:
6438 /* We're not supposed to be called on FUNCTION_DECLs. */
6439 gcc_unreachable ();
6440 case SECCAT_RODATA:
6441 return readonly_data_section;
6442 case SECCAT_RODATA_MERGE_STR:
6443 return mergeable_string_section (decl, align, 0);
6444 case SECCAT_RODATA_MERGE_STR_INIT:
6445 return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6446 case SECCAT_RODATA_MERGE_CONST:
6447 return mergeable_constant_section (DECL_MODE (decl), align, 0);
6448 case SECCAT_SRODATA:
6449 sname = ".sdata2";
6450 break;
6451 case SECCAT_DATA:
6452 return data_section;
6453 case SECCAT_DATA_REL:
6454 sname = ".data.rel";
6455 break;
6456 case SECCAT_DATA_REL_LOCAL:
6457 sname = ".data.rel.local";
6458 break;
6459 case SECCAT_DATA_REL_RO:
6460 sname = ".data.rel.ro";
6461 break;
6462 case SECCAT_DATA_REL_RO_LOCAL:
6463 sname = ".data.rel.ro.local";
6464 break;
6465 case SECCAT_SDATA:
6466 sname = ".sdata";
6467 break;
6468 case SECCAT_TDATA:
6469 sname = ".tdata";
6470 break;
6471 case SECCAT_BSS:
6472 if (bss_section)
6473 return bss_section;
6474 sname = ".bss";
6475 break;
6476 case SECCAT_SBSS:
6477 sname = ".sbss";
6478 break;
6479 case SECCAT_TBSS:
6480 sname = ".tbss";
6481 break;
6482 default:
6483 gcc_unreachable ();
6486 return get_named_section (decl, sname, reloc);
6489 /* Construct a unique section name based on the decl name and the
6490 categorization performed above. */
6492 void
6493 default_unique_section (tree decl, int reloc)
6495 /* We only need to use .gnu.linkonce if we don't have COMDAT groups. */
6496 bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6497 const char *prefix, *name, *linkonce;
6498 char *string;
6499 tree id;
6501 switch (categorize_decl_for_section (decl, reloc))
6503 case SECCAT_TEXT:
6504 prefix = one_only ? ".t" : ".text";
6505 break;
6506 case SECCAT_RODATA:
6507 case SECCAT_RODATA_MERGE_STR:
6508 case SECCAT_RODATA_MERGE_STR_INIT:
6509 case SECCAT_RODATA_MERGE_CONST:
6510 prefix = one_only ? ".r" : ".rodata";
6511 break;
6512 case SECCAT_SRODATA:
6513 prefix = one_only ? ".s2" : ".sdata2";
6514 break;
6515 case SECCAT_DATA:
6516 prefix = one_only ? ".d" : ".data";
6517 break;
6518 case SECCAT_DATA_REL:
6519 prefix = one_only ? ".d.rel" : ".data.rel";
6520 break;
6521 case SECCAT_DATA_REL_LOCAL:
6522 prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6523 break;
6524 case SECCAT_DATA_REL_RO:
6525 prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6526 break;
6527 case SECCAT_DATA_REL_RO_LOCAL:
6528 prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6529 break;
6530 case SECCAT_SDATA:
6531 prefix = one_only ? ".s" : ".sdata";
6532 break;
6533 case SECCAT_BSS:
6534 prefix = one_only ? ".b" : ".bss";
6535 break;
6536 case SECCAT_SBSS:
6537 prefix = one_only ? ".sb" : ".sbss";
6538 break;
6539 case SECCAT_TDATA:
6540 prefix = one_only ? ".td" : ".tdata";
6541 break;
6542 case SECCAT_TBSS:
6543 prefix = one_only ? ".tb" : ".tbss";
6544 break;
6545 default:
6546 gcc_unreachable ();
6549 id = DECL_ASSEMBLER_NAME (decl);
6550 ultimate_transparent_alias_target (&id);
6551 name = IDENTIFIER_POINTER (id);
6552 name = targetm.strip_name_encoding (name);
6554 /* If we're using one_only, then there needs to be a .gnu.linkonce
6555 prefix to the section name. */
6556 linkonce = one_only ? ".gnu.linkonce" : "";
6558 string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6560 set_decl_section_name (decl, string);
6563 /* Subroutine of compute_reloc_for_rtx for leaf rtxes. */
6565 static int
6566 compute_reloc_for_rtx_1 (const_rtx x)
6568 switch (GET_CODE (x))
6570 case SYMBOL_REF:
6571 return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6572 case LABEL_REF:
6573 return 1;
6574 default:
6575 return 0;
6579 /* Like compute_reloc_for_constant, except for an RTX. The return value
6580 is a mask for which bit 1 indicates a global relocation, and bit 0
6581 indicates a local relocation. */
6583 static int
6584 compute_reloc_for_rtx (const_rtx x)
6586 switch (GET_CODE (x))
6588 case SYMBOL_REF:
6589 case LABEL_REF:
6590 return compute_reloc_for_rtx_1 (x);
6592 case CONST:
6594 int reloc = 0;
6595 subrtx_iterator::array_type array;
6596 FOR_EACH_SUBRTX (iter, array, x, ALL)
6597 reloc |= compute_reloc_for_rtx_1 (*iter);
6598 return reloc;
6601 default:
6602 return 0;
6606 section *
6607 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6608 rtx x,
6609 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6611 if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6612 return data_section;
6613 else
6614 return readonly_data_section;
6617 section *
6618 default_elf_select_rtx_section (machine_mode mode, rtx x,
6619 unsigned HOST_WIDE_INT align)
6621 int reloc = compute_reloc_for_rtx (x);
6623 /* ??? Handle small data here somehow. */
6625 if (reloc & targetm.asm_out.reloc_rw_mask ())
6627 if (reloc == 1)
6628 return get_named_section (NULL, ".data.rel.ro.local", 1);
6629 else
6630 return get_named_section (NULL, ".data.rel.ro", 3);
6633 return mergeable_constant_section (mode, align, 0);
6636 /* Set the generally applicable flags on the SYMBOL_REF for EXP. */
6638 void
6639 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
6641 rtx symbol;
6642 int flags;
6644 /* Careful not to prod global register variables. */
6645 if (!MEM_P (rtl))
6646 return;
6647 symbol = XEXP (rtl, 0);
6648 if (GET_CODE (symbol) != SYMBOL_REF)
6649 return;
6651 flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
6652 if (TREE_CODE (decl) == FUNCTION_DECL)
6653 flags |= SYMBOL_FLAG_FUNCTION;
6654 if (targetm.binds_local_p (decl))
6655 flags |= SYMBOL_FLAG_LOCAL;
6656 if (TREE_CODE (decl) == VAR_DECL && DECL_THREAD_LOCAL_P (decl))
6657 flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
6658 else if (targetm.in_small_data_p (decl))
6659 flags |= SYMBOL_FLAG_SMALL;
6660 /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names? Without
6661 being PUBLIC, the thing *must* be defined in this translation unit.
6662 Prevent this buglet from being propagated into rtl code as well. */
6663 if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
6664 flags |= SYMBOL_FLAG_EXTERNAL;
6666 SYMBOL_REF_FLAGS (symbol) = flags;
6669 /* By default, we do nothing for encode_section_info, so we need not
6670 do anything but discard the '*' marker. */
6672 const char *
6673 default_strip_name_encoding (const char *str)
6675 return str + (*str == '*');
6678 #ifdef ASM_OUTPUT_DEF
6679 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
6680 anchor relative to ".", the current section position. */
6682 void
6683 default_asm_output_anchor (rtx symbol)
6685 char buffer[100];
6687 sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
6688 SYMBOL_REF_BLOCK_OFFSET (symbol));
6689 ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
6691 #endif
6693 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P. */
6695 bool
6696 default_use_anchors_for_symbol_p (const_rtx symbol)
6698 section *sect;
6699 tree decl;
6701 /* Don't use anchors for mergeable sections. The linker might move
6702 the objects around. */
6703 sect = SYMBOL_REF_BLOCK (symbol)->sect;
6704 if (sect->common.flags & SECTION_MERGE)
6705 return false;
6707 /* Don't use anchors for small data sections. The small data register
6708 acts as an anchor for such sections. */
6709 if (sect->common.flags & SECTION_SMALL)
6710 return false;
6712 decl = SYMBOL_REF_DECL (symbol);
6713 if (decl && DECL_P (decl))
6715 /* Don't use section anchors for decls that might be defined or
6716 usurped by other modules. */
6717 if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
6718 return false;
6720 /* Don't use section anchors for decls that will be placed in a
6721 small data section. */
6722 /* ??? Ideally, this check would be redundant with the SECTION_SMALL
6723 one above. The problem is that we only use SECTION_SMALL for
6724 sections that should be marked as small in the section directive. */
6725 if (targetm.in_small_data_p (decl))
6726 return false;
6728 return true;
6731 /* Return true when RESOLUTION indicate that symbol will be bound to the
6732 definition provided by current .o file. */
6734 static bool
6735 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
6737 return (resolution == LDPR_PREVAILING_DEF
6738 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6739 || resolution == LDPR_PREVAILING_DEF_IRONLY);
6742 /* Return true when RESOLUTION indicate that symbol will be bound locally
6743 within current executable or DSO. */
6745 static bool
6746 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
6748 return (resolution == LDPR_PREVAILING_DEF
6749 || resolution == LDPR_PREVAILING_DEF_IRONLY
6750 || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
6751 || resolution == LDPR_PREEMPTED_REG
6752 || resolution == LDPR_PREEMPTED_IR
6753 || resolution == LDPR_RESOLVED_IR
6754 || resolution == LDPR_RESOLVED_EXEC);
6757 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
6758 uninitialized common symbol in the executable will still be defined
6759 (through COPY relocation) in the executable. */
6761 bool
6762 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
6763 bool extern_protected_data, bool common_local_p)
6765 /* A non-decl is an entry in the constant pool. */
6766 if (!DECL_P (exp))
6767 return true;
6769 /* Weakrefs may not bind locally, even though the weakref itself is always
6770 static and therefore local. Similarly, the resolver for ifunc functions
6771 might resolve to a non-local function.
6772 FIXME: We can resolve the weakref case more curefuly by looking at the
6773 weakref alias. */
6774 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
6775 || (TREE_CODE (exp) == FUNCTION_DECL
6776 && lookup_attribute ("ifunc", DECL_ATTRIBUTES (exp))))
6777 return false;
6779 /* Static variables are always local. */
6780 if (! TREE_PUBLIC (exp))
6781 return true;
6783 /* With resolution file in hand, take look into resolutions.
6784 We can't just return true for resolved_locally symbols,
6785 because dynamic linking might overwrite symbols
6786 in shared libraries. */
6787 bool resolved_locally = false;
6789 bool uninited_common = (DECL_COMMON (exp)
6790 && (DECL_INITIAL (exp) == NULL
6791 || (!in_lto_p
6792 && DECL_INITIAL (exp) == error_mark_node)));
6794 /* A non-external variable is defined locally only if it isn't
6795 uninitialized COMMON variable or common_local_p is true. */
6796 bool defined_locally = (!DECL_EXTERNAL (exp)
6797 && (!uninited_common || common_local_p));
6798 if (symtab_node *node = symtab_node::get (exp))
6800 if (node->in_other_partition)
6801 defined_locally = true;
6802 if (resolution_to_local_definition_p (node->resolution))
6803 defined_locally = resolved_locally = true;
6804 else if (resolution_local_p (node->resolution))
6805 resolved_locally = true;
6807 if (defined_locally && weak_dominate && !shlib)
6808 resolved_locally = true;
6810 /* Undefined weak symbols are never defined locally. */
6811 if (DECL_WEAK (exp) && !defined_locally)
6812 return false;
6814 /* A symbol is local if the user has said explicitly that it will be,
6815 or if we have a definition for the symbol. We cannot infer visibility
6816 for undefined symbols. */
6817 if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
6818 && (TREE_CODE (exp) == FUNCTION_DECL
6819 || !extern_protected_data
6820 || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
6821 && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
6822 return true;
6824 /* If PIC, then assume that any global name can be overridden by
6825 symbols resolved from other modules. */
6826 if (shlib)
6827 return false;
6829 /* Variables defined outside this object might not be local. */
6830 if (DECL_EXTERNAL (exp) && !resolved_locally)
6831 return false;
6833 /* Non-dominant weak symbols are not defined locally. */
6834 if (DECL_WEAK (exp) && !resolved_locally)
6835 return false;
6837 /* Uninitialized COMMON variable may be unified with symbols
6838 resolved from other modules. */
6839 if (uninited_common && !resolved_locally)
6840 return false;
6842 /* Otherwise we're left with initialized (or non-common) global data
6843 which is of necessity defined locally. */
6844 return true;
6847 /* Assume ELF-ish defaults, since that's pretty much the most liberal
6848 wrt cross-module name binding. */
6850 bool
6851 default_binds_local_p (const_tree exp)
6853 return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
6856 /* Similar to default_binds_local_p, but common symbol may be local and
6857 extern protected data is non-local. */
6859 bool
6860 default_binds_local_p_2 (const_tree exp)
6862 return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
6863 !flag_pic);
6866 bool
6867 default_binds_local_p_1 (const_tree exp, int shlib)
6869 return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
6872 /* Return true when references to DECL must bind to current definition in
6873 final executable.
6875 The condition is usually equivalent to whether the function binds to the
6876 current module (shared library or executable), that is to binds_local_p.
6877 We use this fact to avoid need for another target hook and implement
6878 the logic using binds_local_p and just special cases where
6879 decl_binds_to_current_def_p is stronger than binds_local_p. In particular
6880 the weak definitions (that can be overwritten at linktime by other
6881 definition from different object file) and when resolution info is available
6882 we simply use the knowledge passed to us by linker plugin. */
6883 bool
6884 decl_binds_to_current_def_p (const_tree decl)
6886 gcc_assert (DECL_P (decl));
6887 if (!targetm.binds_local_p (decl))
6888 return false;
6889 if (!TREE_PUBLIC (decl))
6890 return true;
6892 /* When resolution is available, just use it. */
6893 if (symtab_node *node = symtab_node::get (decl))
6895 if (node->resolution != LDPR_UNKNOWN)
6896 return resolution_to_local_definition_p (node->resolution);
6899 /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
6900 binds locally but still can be overwritten), DECL_COMMON (can be merged
6901 with a non-common definition somewhere in the same module) or
6902 DECL_EXTERNAL.
6903 This rely on fact that binds_local_p behave as decl_replaceable_p
6904 for all other declaration types. */
6905 if (DECL_WEAK (decl))
6906 return false;
6907 if (DECL_COMMON (decl)
6908 && (DECL_INITIAL (decl) == NULL
6909 || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6910 return false;
6911 if (DECL_EXTERNAL (decl))
6912 return false;
6913 return true;
6916 /* A replaceable function or variable is one which may be replaced
6917 at link-time with an entirely different definition, provided that the
6918 replacement has the same type. For example, functions declared
6919 with __attribute__((weak)) on most systems are replaceable.
6921 COMDAT functions are not replaceable, since all definitions of the
6922 function must be equivalent. It is important that COMDAT functions
6923 not be treated as replaceable so that use of C++ template
6924 instantiations is not penalized. */
6926 bool
6927 decl_replaceable_p (tree decl)
6929 gcc_assert (DECL_P (decl));
6930 if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
6931 return false;
6932 if (!flag_semantic_interposition
6933 && !DECL_WEAK (decl))
6934 return false;
6935 return !decl_binds_to_current_def_p (decl);
6938 /* Default function to output code that will globalize a label. A
6939 target must define GLOBAL_ASM_OP or provide its own function to
6940 globalize a label. */
6941 #ifdef GLOBAL_ASM_OP
6942 void
6943 default_globalize_label (FILE * stream, const char *name)
6945 fputs (GLOBAL_ASM_OP, stream);
6946 assemble_name (stream, name);
6947 putc ('\n', stream);
6949 #endif /* GLOBAL_ASM_OP */
6951 /* Default function to output code that will globalize a declaration. */
6952 void
6953 default_globalize_decl_name (FILE * stream, tree decl)
6955 const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
6956 targetm.asm_out.globalize_label (stream, name);
6959 /* Default function to output a label for unwind information. The
6960 default is to do nothing. A target that needs nonlocal labels for
6961 unwind information must provide its own function to do this. */
6962 void
6963 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
6964 tree decl ATTRIBUTE_UNUSED,
6965 int for_eh ATTRIBUTE_UNUSED,
6966 int empty ATTRIBUTE_UNUSED)
6970 /* Default function to output a label to divide up the exception table.
6971 The default is to do nothing. A target that needs/wants to divide
6972 up the table must provide it's own function to do this. */
6973 void
6974 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
6978 /* This is how to output an internal numbered label where PREFIX is
6979 the class of label and LABELNO is the number within the class. */
6981 void
6982 default_generate_internal_label (char *buf, const char *prefix,
6983 unsigned long labelno)
6985 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6988 /* This is how to output an internal numbered label where PREFIX is
6989 the class of label and LABELNO is the number within the class. */
6991 void
6992 default_internal_label (FILE *stream, const char *prefix,
6993 unsigned long labelno)
6995 char *const buf = (char *) alloca (40 + strlen (prefix));
6996 ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
6997 ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7001 /* The default implementation of ASM_DECLARE_CONSTANT_NAME. */
7003 void
7004 default_asm_declare_constant_name (FILE *file, const char *name,
7005 const_tree exp ATTRIBUTE_UNUSED,
7006 HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7008 assemble_label (file, name);
7011 /* This is the default behavior at the beginning of a file. It's
7012 controlled by two other target-hook toggles. */
7013 void
7014 default_file_start (void)
7016 if (targetm.asm_file_start_app_off
7017 && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7018 fputs (ASM_APP_OFF, asm_out_file);
7020 if (targetm.asm_file_start_file_directive)
7022 /* LTO produced units have no meaningful main_input_filename. */
7023 if (in_lto_p)
7024 output_file_directive (asm_out_file, "<artificial>");
7025 else
7026 output_file_directive (asm_out_file, main_input_filename);
7030 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7031 which emits a special section directive used to indicate whether or
7032 not this object file needs an executable stack. This is primarily
7033 a GNU extension to ELF but could be used on other targets. */
7035 int trampolines_created;
7037 void
7038 file_end_indicate_exec_stack (void)
7040 unsigned int flags = SECTION_DEBUG;
7041 if (trampolines_created)
7042 flags |= SECTION_CODE;
7044 switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7047 /* Emit a special section directive to indicate that this object file
7048 was compiled with -fsplit-stack. This is used to let the linker
7049 detect calls between split-stack code and non-split-stack code, so
7050 that it can modify the split-stack code to allocate a sufficiently
7051 large stack. We emit another special section if there are any
7052 functions in this file which have the no_split_stack attribute, to
7053 prevent the linker from warning about being unable to convert the
7054 functions if they call non-split-stack code. */
7056 void
7057 file_end_indicate_split_stack (void)
7059 if (flag_split_stack)
7061 switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7062 NULL));
7063 if (saw_no_split_stack)
7064 switch_to_section (get_section (".note.GNU-no-split-stack",
7065 SECTION_DEBUG, NULL));
7069 /* Output DIRECTIVE (a C string) followed by a newline. This is used as
7070 a get_unnamed_section callback. */
7072 void
7073 output_section_asm_op (const void *directive)
7075 fprintf (asm_out_file, "%s\n", (const char *) directive);
7078 /* Emit assembly code to switch to section NEW_SECTION. Do nothing if
7079 the current section is NEW_SECTION. */
7081 void
7082 switch_to_section (section *new_section)
7084 if (in_section == new_section)
7085 return;
7087 if (new_section->common.flags & SECTION_FORGET)
7088 in_section = NULL;
7089 else
7090 in_section = new_section;
7092 switch (SECTION_STYLE (new_section))
7094 case SECTION_NAMED:
7095 targetm.asm_out.named_section (new_section->named.name,
7096 new_section->named.common.flags,
7097 new_section->named.decl);
7098 break;
7100 case SECTION_UNNAMED:
7101 new_section->unnamed.callback (new_section->unnamed.data);
7102 break;
7104 case SECTION_NOSWITCH:
7105 gcc_unreachable ();
7106 break;
7109 new_section->common.flags |= SECTION_DECLARED;
7112 /* If block symbol SYMBOL has not yet been assigned an offset, place
7113 it at the end of its block. */
7115 void
7116 place_block_symbol (rtx symbol)
7118 unsigned HOST_WIDE_INT size, mask, offset;
7119 struct constant_descriptor_rtx *desc;
7120 unsigned int alignment;
7121 struct object_block *block;
7122 tree decl;
7124 gcc_assert (SYMBOL_REF_BLOCK (symbol));
7125 if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7126 return;
7128 /* Work out the symbol's size and alignment. */
7129 if (CONSTANT_POOL_ADDRESS_P (symbol))
7131 desc = SYMBOL_REF_CONSTANT (symbol);
7132 alignment = desc->align;
7133 size = GET_MODE_SIZE (desc->mode);
7135 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7137 decl = SYMBOL_REF_DECL (symbol);
7138 gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7139 alignment = DECL_ALIGN (decl);
7140 size = get_constant_size (DECL_INITIAL (decl));
7141 if ((flag_sanitize & SANITIZE_ADDRESS)
7142 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7143 && asan_protect_global (DECL_INITIAL (decl)))
7144 size += asan_red_zone_size (size);
7146 else
7148 struct symtab_node *snode;
7149 decl = SYMBOL_REF_DECL (symbol);
7151 snode = symtab_node::get (decl);
7152 if (snode->alias)
7154 rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7156 gcc_assert (MEM_P (target)
7157 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7158 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7159 target = XEXP (target, 0);
7160 place_block_symbol (target);
7161 SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7162 return;
7164 alignment = get_variable_align (decl);
7165 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7166 if ((flag_sanitize & SANITIZE_ADDRESS)
7167 && asan_protect_global (decl))
7169 size += asan_red_zone_size (size);
7170 alignment = MAX (alignment,
7171 ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7175 /* Calculate the object's offset from the start of the block. */
7176 block = SYMBOL_REF_BLOCK (symbol);
7177 mask = alignment / BITS_PER_UNIT - 1;
7178 offset = (block->size + mask) & ~mask;
7179 SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7181 /* Record the block's new alignment and size. */
7182 block->alignment = MAX (block->alignment, alignment);
7183 block->size = offset + size;
7185 vec_safe_push (block->objects, symbol);
7188 /* Return the anchor that should be used to address byte offset OFFSET
7189 from the first object in BLOCK. MODEL is the TLS model used
7190 to access it. */
7193 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7194 enum tls_model model)
7196 char label[100];
7197 unsigned int begin, middle, end;
7198 unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7199 rtx anchor;
7201 /* Work out the anchor's offset. Use an offset of 0 for the first
7202 anchor so that we don't pessimize the case where we take the address
7203 of a variable at the beginning of the block. This is particularly
7204 useful when a block has only one variable assigned to it.
7206 We try to place anchors RANGE bytes apart, so there can then be
7207 anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7208 a ptr_mode offset. With some target settings, the lowest such
7209 anchor might be out of range for the lowest ptr_mode offset;
7210 likewise the highest anchor for the highest offset. Use anchors
7211 at the extreme ends of the ptr_mode range in such cases.
7213 All arithmetic uses unsigned integers in order to avoid
7214 signed overflow. */
7215 max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7216 min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7217 range = max_offset - min_offset + 1;
7218 if (range == 0)
7219 offset = 0;
7220 else
7222 bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7223 if (offset < 0)
7225 delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7226 delta -= delta % range;
7227 if (delta > bias)
7228 delta = bias;
7229 offset = (HOST_WIDE_INT) (-delta);
7231 else
7233 delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7234 delta -= delta % range;
7235 if (delta > bias - 1)
7236 delta = bias - 1;
7237 offset = (HOST_WIDE_INT) delta;
7241 /* Do a binary search to see if there's already an anchor we can use.
7242 Set BEGIN to the new anchor's index if not. */
7243 begin = 0;
7244 end = vec_safe_length (block->anchors);
7245 while (begin != end)
7247 middle = (end + begin) / 2;
7248 anchor = (*block->anchors)[middle];
7249 if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7250 end = middle;
7251 else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7252 begin = middle + 1;
7253 else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7254 end = middle;
7255 else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7256 begin = middle + 1;
7257 else
7258 return anchor;
7261 /* Create a new anchor with a unique label. */
7262 ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7263 anchor = create_block_symbol (ggc_strdup (label), block, offset);
7264 SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7265 SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7267 /* Insert it at index BEGIN. */
7268 vec_safe_insert (block->anchors, begin, anchor);
7269 return anchor;
7272 /* Output the objects in BLOCK. */
7274 static void
7275 output_object_block (struct object_block *block)
7277 struct constant_descriptor_rtx *desc;
7278 unsigned int i;
7279 HOST_WIDE_INT offset;
7280 tree decl;
7281 rtx symbol;
7283 if (!block->objects)
7284 return;
7286 /* Switch to the section and make sure that the first byte is
7287 suitably aligned. */
7288 /* Special case VTV comdat sections similar to assemble_variable. */
7289 if (SECTION_STYLE (block->sect) == SECTION_NAMED
7290 && block->sect->named.name
7291 && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
7292 handle_vtv_comdat_section (block->sect, block->sect->named.decl);
7293 else
7294 switch_to_section (block->sect);
7296 assemble_align (block->alignment);
7298 /* Define the values of all anchors relative to the current section
7299 position. */
7300 FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7301 targetm.asm_out.output_anchor (symbol);
7303 /* Output the objects themselves. */
7304 offset = 0;
7305 FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7307 /* Move to the object's offset, padding with zeros if necessary. */
7308 assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7309 offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7310 if (CONSTANT_POOL_ADDRESS_P (symbol))
7312 desc = SYMBOL_REF_CONSTANT (symbol);
7313 /* Pass 1 for align as we have already laid out everything in the block.
7314 So aligning shouldn't be necessary. */
7315 output_constant_pool_1 (desc, 1);
7316 offset += GET_MODE_SIZE (desc->mode);
7318 else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7320 HOST_WIDE_INT size;
7321 decl = SYMBOL_REF_DECL (symbol);
7322 assemble_constant_contents
7323 (DECL_INITIAL (decl), XSTR (symbol, 0), DECL_ALIGN (decl));
7325 size = get_constant_size (DECL_INITIAL (decl));
7326 offset += size;
7327 if ((flag_sanitize & SANITIZE_ADDRESS)
7328 && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7329 && asan_protect_global (DECL_INITIAL (decl)))
7331 size = asan_red_zone_size (size);
7332 assemble_zeros (size);
7333 offset += size;
7336 else
7338 HOST_WIDE_INT size;
7339 decl = SYMBOL_REF_DECL (symbol);
7340 assemble_variable_contents (decl, XSTR (symbol, 0), false);
7341 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7342 offset += size;
7343 if ((flag_sanitize & SANITIZE_ADDRESS)
7344 && asan_protect_global (decl))
7346 size = asan_red_zone_size (size);
7347 assemble_zeros (size);
7348 offset += size;
7354 /* A callback for qsort to compare object_blocks. */
7356 static int
7357 output_object_block_compare (const void *x, const void *y)
7359 object_block *p1 = *(object_block * const*)x;
7360 object_block *p2 = *(object_block * const*)y;
7362 if (p1->sect->common.flags & SECTION_NAMED
7363 && !(p2->sect->common.flags & SECTION_NAMED))
7364 return 1;
7366 if (!(p1->sect->common.flags & SECTION_NAMED)
7367 && p2->sect->common.flags & SECTION_NAMED)
7368 return -1;
7370 if (p1->sect->common.flags & SECTION_NAMED
7371 && p2->sect->common.flags & SECTION_NAMED)
7372 return strcmp (p1->sect->named.name, p2->sect->named.name);
7374 unsigned f1 = p1->sect->common.flags;
7375 unsigned f2 = p2->sect->common.flags;
7376 if (f1 == f2)
7377 return 0;
7378 return f1 < f2 ? -1 : 1;
7381 /* Output the definitions of all object_blocks. */
7383 void
7384 output_object_blocks (void)
7386 vec<object_block *, va_heap> v;
7387 v.create (object_block_htab->elements ());
7388 object_block *obj;
7389 hash_table<object_block_hasher>::iterator hi;
7391 FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
7392 v.quick_push (obj);
7394 /* Sort them in order to output them in a deterministic manner,
7395 otherwise we may get .rodata sections in different orders with
7396 and without -g. */
7397 v.qsort (output_object_block_compare);
7398 unsigned i;
7399 FOR_EACH_VEC_ELT (v, i, obj)
7400 output_object_block (obj);
7402 v.release ();
7405 /* This function provides a possible implementation of the
7406 TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets. When triggered
7407 by -frecord-gcc-switches it creates a new mergeable, string section in the
7408 assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7409 contains the switches in ASCII format.
7411 FIXME: This code does not correctly handle double quote characters
7412 that appear inside strings, (it strips them rather than preserving them).
7413 FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7414 characters - instead it treats them as sub-string separators. Since
7415 we want to emit NUL strings terminators into the object file we have to use
7416 ASM_OUTPUT_SKIP. */
7419 elf_record_gcc_switches (print_switch_type type, const char * name)
7421 switch (type)
7423 case SWITCH_TYPE_PASSED:
7424 ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7425 ASM_OUTPUT_SKIP (asm_out_file, (unsigned HOST_WIDE_INT) 1);
7426 break;
7428 case SWITCH_TYPE_DESCRIPTIVE:
7429 if (name == NULL)
7431 /* Distinguish between invocations where name is NULL. */
7432 static bool started = false;
7434 if (!started)
7436 section * sec;
7438 sec = get_section (targetm.asm_out.record_gcc_switches_section,
7439 SECTION_DEBUG
7440 | SECTION_MERGE
7441 | SECTION_STRINGS
7442 | (SECTION_ENTSIZE & 1),
7443 NULL);
7444 switch_to_section (sec);
7445 started = true;
7449 default:
7450 break;
7453 /* The return value is currently ignored by the caller, but must be 0.
7454 For -fverbose-asm the return value would be the number of characters
7455 emitted into the assembler file. */
7456 return 0;
7459 /* Emit text to declare externally defined symbols. It is needed to
7460 properly support non-default visibility. */
7461 void
7462 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7463 tree decl,
7464 const char *name ATTRIBUTE_UNUSED)
7466 /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7467 set in order to avoid putting out names that are never really
7468 used. Always output visibility specified in the source. */
7469 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7470 && (DECL_VISIBILITY_SPECIFIED (decl)
7471 || targetm.binds_local_p (decl)))
7472 maybe_assemble_visibility (decl);
7475 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME. */
7477 void
7478 default_asm_output_source_filename (FILE *file, const char *name)
7480 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7481 ASM_OUTPUT_SOURCE_FILENAME (file, name);
7482 #else
7483 fprintf (file, "\t.file\t");
7484 output_quoted_string (file, name);
7485 putc ('\n', file);
7486 #endif
7489 /* Output a file name in the form wanted by System V. */
7491 void
7492 output_file_directive (FILE *asm_file, const char *input_name)
7494 int len;
7495 const char *na;
7497 if (input_name == NULL)
7498 input_name = "<stdin>";
7499 else
7500 input_name = remap_debug_filename (input_name);
7502 len = strlen (input_name);
7503 na = input_name + len;
7505 /* NA gets INPUT_NAME sans directory names. */
7506 while (na > input_name)
7508 if (IS_DIR_SEPARATOR (na[-1]))
7509 break;
7510 na--;
7513 targetm.asm_out.output_source_filename (asm_file, na);
7516 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7517 EXP. */
7519 make_debug_expr_from_rtl (const_rtx exp)
7521 tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7522 machine_mode mode = GET_MODE (exp);
7523 rtx dval;
7525 DECL_ARTIFICIAL (ddecl) = 1;
7526 if (REG_P (exp) && REG_EXPR (exp))
7527 type = TREE_TYPE (REG_EXPR (exp));
7528 else if (MEM_P (exp) && MEM_EXPR (exp))
7529 type = TREE_TYPE (MEM_EXPR (exp));
7530 else
7531 type = NULL_TREE;
7532 if (type && TYPE_MODE (type) == mode)
7533 TREE_TYPE (ddecl) = type;
7534 else
7535 TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7536 DECL_MODE (ddecl) = mode;
7537 dval = gen_rtx_DEBUG_EXPR (mode);
7538 DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7539 SET_DECL_RTL (ddecl, dval);
7540 return dval;
7543 #ifdef ELF_ASCII_ESCAPES
7544 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets. */
7546 void
7547 default_elf_asm_output_limited_string (FILE *f, const char *s)
7549 int escape;
7550 unsigned char c;
7552 fputs (STRING_ASM_OP, f);
7553 putc ('"', f);
7554 while (*s != '\0')
7556 c = *s;
7557 escape = ELF_ASCII_ESCAPES[c];
7558 switch (escape)
7560 case 0:
7561 putc (c, f);
7562 break;
7563 case 1:
7564 /* TODO: Print in hex with fast function, important for -flto. */
7565 fprintf (f, "\\%03o", c);
7566 break;
7567 default:
7568 putc ('\\', f);
7569 putc (escape, f);
7570 break;
7572 s++;
7574 putc ('\"', f);
7575 putc ('\n', f);
7578 /* Default ASM_OUTPUT_ASCII for ELF targets. */
7580 void
7581 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7583 const char *limit = s + len;
7584 const char *last_null = NULL;
7585 unsigned bytes_in_chunk = 0;
7586 unsigned char c;
7587 int escape;
7589 for (; s < limit; s++)
7591 const char *p;
7593 if (bytes_in_chunk >= 60)
7595 putc ('\"', f);
7596 putc ('\n', f);
7597 bytes_in_chunk = 0;
7600 if (s > last_null)
7602 for (p = s; p < limit && *p != '\0'; p++)
7603 continue;
7604 last_null = p;
7606 else
7607 p = last_null;
7609 if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7611 if (bytes_in_chunk > 0)
7613 putc ('\"', f);
7614 putc ('\n', f);
7615 bytes_in_chunk = 0;
7618 default_elf_asm_output_limited_string (f, s);
7619 s = p;
7621 else
7623 if (bytes_in_chunk == 0)
7624 fputs (ASCII_DATA_ASM_OP "\"", f);
7626 c = *s;
7627 escape = ELF_ASCII_ESCAPES[c];
7628 switch (escape)
7630 case 0:
7631 putc (c, f);
7632 bytes_in_chunk++;
7633 break;
7634 case 1:
7635 /* TODO: Print in hex with fast function, important for -flto. */
7636 fprintf (f, "\\%03o", c);
7637 bytes_in_chunk += 4;
7638 break;
7639 default:
7640 putc ('\\', f);
7641 putc (escape, f);
7642 bytes_in_chunk += 2;
7643 break;
7649 if (bytes_in_chunk > 0)
7651 putc ('\"', f);
7652 putc ('\n', f);
7655 #endif
7657 static GTY(()) section *elf_init_array_section;
7658 static GTY(()) section *elf_fini_array_section;
7660 static section *
7661 get_elf_initfini_array_priority_section (int priority,
7662 bool constructor_p)
7664 section *sec;
7665 if (priority != DEFAULT_INIT_PRIORITY)
7667 char buf[18];
7668 sprintf (buf, "%s.%.5u",
7669 constructor_p ? ".init_array" : ".fini_array",
7670 priority);
7671 sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7673 else
7675 if (constructor_p)
7677 if (elf_init_array_section == NULL)
7678 elf_init_array_section
7679 = get_section (".init_array",
7680 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7681 sec = elf_init_array_section;
7683 else
7685 if (elf_fini_array_section == NULL)
7686 elf_fini_array_section
7687 = get_section (".fini_array",
7688 SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
7689 sec = elf_fini_array_section;
7692 return sec;
7695 /* Use .init_array section for constructors. */
7697 void
7698 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
7700 section *sec = get_elf_initfini_array_priority_section (priority,
7701 true);
7702 assemble_addr_to_section (symbol, sec);
7705 /* Use .fini_array section for destructors. */
7707 void
7708 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
7710 section *sec = get_elf_initfini_array_priority_section (priority,
7711 false);
7712 assemble_addr_to_section (symbol, sec);
7715 /* Default TARGET_ASM_OUTPUT_IDENT hook.
7717 This is a bit of a cheat. The real default is a no-op, but this
7718 hook is the default for all targets with a .ident directive. */
7720 void
7721 default_asm_output_ident_directive (const char *ident_str)
7723 const char *ident_asm_op = "\t.ident\t";
7725 /* If we are still in the front end, do not write out the string
7726 to asm_out_file. Instead, add a fake top-level asm statement.
7727 This allows the front ends to use this hook without actually
7728 writing to asm_out_file, to handle #ident or Pragma Ident. */
7729 if (symtab->state == PARSING)
7731 char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
7732 symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
7734 else
7735 fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
7739 /* This function ensures that vtable_map variables are not only
7740 in the comdat section, but that each variable has its own unique
7741 comdat name. Without this the variables end up in the same section
7742 with a single comdat name.
7744 FIXME: resolve_unique_section needs to deal better with
7745 decls with both DECL_SECTION_NAME and DECL_ONE_ONLY. Once
7746 that is fixed, this if-else statement can be replaced with
7747 a single call to "switch_to_section (sect)". */
7749 static void
7750 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
7752 #if defined (OBJECT_FORMAT_ELF)
7753 targetm.asm_out.named_section (sect->named.name,
7754 sect->named.common.flags
7755 | SECTION_LINKONCE,
7756 DECL_NAME (decl));
7757 in_section = sect;
7758 #elif defined (TARGET_PECOFF)
7759 /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
7760 Therefore the following check is used.
7761 In case a the target is PE or COFF a comdat group section
7762 is created, e.g. .vtable_map_vars$foo. The linker places
7763 everything in .vtable_map_vars at the end.
7765 A fix could be made in
7766 gcc/config/i386/winnt.c: i386_pe_unique_section. */
7767 if (TARGET_PECOFF)
7769 char *name;
7771 if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
7772 name = ACONCAT ((sect->named.name, "$",
7773 IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
7774 else
7775 name = ACONCAT ((sect->named.name, "$",
7776 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
7777 NULL));
7779 targetm.asm_out.named_section (name,
7780 sect->named.common.flags
7781 | SECTION_LINKONCE,
7782 DECL_NAME (decl));
7783 in_section = sect;
7785 #else
7786 switch_to_section (sect);
7787 #endif
7790 #include "gt-varasm.h"